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 eager->tcp_detached = B_FALSE; 2409 2410 ASSERT(eager->tcp_ack_tid == 0); 2411 2412 econnp->conn_dev = aconnp->conn_dev; 2413 if (eager->tcp_cred != NULL) 2414 crfree(eager->tcp_cred); 2415 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2416 econnp->conn_zoneid = aconnp->conn_zoneid; 2417 aconnp->conn_cred = NULL; 2418 2419 econnp->conn_mac_exempt = aconnp->conn_mac_exempt; 2420 aconnp->conn_mac_exempt = B_FALSE; 2421 2422 ASSERT(aconnp->conn_peercred == NULL); 2423 2424 /* Do the IPC initialization */ 2425 CONN_INC_REF(econnp); 2426 2427 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2428 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2429 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2430 econnp->conn_ulp = aconnp->conn_ulp; 2431 2432 /* Done with old IPC. Drop its ref on its connp */ 2433 CONN_DEC_REF(aconnp); 2434 } 2435 2436 2437 /* 2438 * Adapt to the information, such as rtt and rtt_sd, provided from the 2439 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2440 * 2441 * Checks for multicast and broadcast destination address. 2442 * Returns zero on failure; non-zero if ok. 2443 * 2444 * Note that the MSS calculation here is based on the info given in 2445 * the IRE. We do not do any calculation based on TCP options. They 2446 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2447 * knows which options to use. 2448 * 2449 * Note on how TCP gets its parameters for a connection. 2450 * 2451 * When a tcp_t structure is allocated, it gets all the default parameters. 2452 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2453 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2454 * default. But if there is an associated tcp_host_param, it will override 2455 * the metrics. 2456 * 2457 * An incoming SYN with a multicast or broadcast destination address, is dropped 2458 * in 1 of 2 places. 2459 * 2460 * 1. If the packet was received over the wire it is dropped in 2461 * ip_rput_process_broadcast() 2462 * 2463 * 2. If the packet was received through internal IP loopback, i.e. the packet 2464 * was generated and received on the same machine, it is dropped in 2465 * ip_wput_local() 2466 * 2467 * An incoming SYN with a multicast or broadcast source address is always 2468 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2469 * reject an attempt to connect to a broadcast or multicast (destination) 2470 * address. 2471 */ 2472 static int 2473 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2474 { 2475 tcp_hsp_t *hsp; 2476 ire_t *ire; 2477 ire_t *sire = NULL; 2478 iulp_t *ire_uinfo = NULL; 2479 uint32_t mss_max; 2480 uint32_t mss; 2481 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2482 conn_t *connp = tcp->tcp_connp; 2483 boolean_t ire_cacheable = B_FALSE; 2484 zoneid_t zoneid = connp->conn_zoneid; 2485 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT | 2486 MATCH_IRE_SECATTR; 2487 ts_label_t *tsl = crgetlabel(CONN_CRED(connp)); 2488 ill_t *ill = NULL; 2489 boolean_t incoming = (ire_mp == NULL); 2490 2491 ASSERT(connp->conn_ire_cache == NULL); 2492 2493 if (tcp->tcp_ipversion == IPV4_VERSION) { 2494 2495 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2496 BUMP_MIB(&ip_mib, ipInDiscards); 2497 return (0); 2498 } 2499 /* 2500 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2501 * for the destination with the nexthop as gateway. 2502 * ire_ctable_lookup() is used because this particular 2503 * ire, if it exists, will be marked private. 2504 * If that is not available, use the interface ire 2505 * for the nexthop. 2506 * 2507 * TSol: tcp_update_label will detect label mismatches based 2508 * only on the destination's label, but that would not 2509 * detect label mismatches based on the security attributes 2510 * of routes or next hop gateway. Hence we need to pass the 2511 * label to ire_ftable_lookup below in order to locate the 2512 * right prefix (and/or) ire cache. Similarly we also need 2513 * pass the label to the ire_cache_lookup below to locate 2514 * the right ire that also matches on the label. 2515 */ 2516 if (tcp->tcp_connp->conn_nexthop_set) { 2517 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2518 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2519 tsl, MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW); 2520 if (ire == NULL) { 2521 ire = ire_ftable_lookup( 2522 tcp->tcp_connp->conn_nexthop_v4, 2523 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2524 tsl, match_flags); 2525 if (ire == NULL) 2526 return (0); 2527 } else { 2528 ire_uinfo = &ire->ire_uinfo; 2529 } 2530 } else { 2531 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2532 zoneid, tsl); 2533 if (ire != NULL) { 2534 ire_cacheable = B_TRUE; 2535 ire_uinfo = (ire_mp != NULL) ? 2536 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2537 &ire->ire_uinfo; 2538 2539 } else { 2540 if (ire_mp == NULL) { 2541 ire = ire_ftable_lookup( 2542 tcp->tcp_connp->conn_rem, 2543 0, 0, 0, NULL, &sire, zoneid, 0, 2544 tsl, (MATCH_IRE_RECURSIVE | 2545 MATCH_IRE_DEFAULT)); 2546 if (ire == NULL) 2547 return (0); 2548 ire_uinfo = (sire != NULL) ? 2549 &sire->ire_uinfo : 2550 &ire->ire_uinfo; 2551 } else { 2552 ire = (ire_t *)ire_mp->b_rptr; 2553 ire_uinfo = 2554 &((ire_t *) 2555 ire_mp->b_rptr)->ire_uinfo; 2556 } 2557 } 2558 } 2559 ASSERT(ire != NULL); 2560 2561 if ((ire->ire_src_addr == INADDR_ANY) || 2562 (ire->ire_type & IRE_BROADCAST)) { 2563 /* 2564 * ire->ire_mp is non null when ire_mp passed in is used 2565 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2566 */ 2567 if (ire->ire_mp == NULL) 2568 ire_refrele(ire); 2569 if (sire != NULL) 2570 ire_refrele(sire); 2571 return (0); 2572 } 2573 2574 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2575 ipaddr_t src_addr; 2576 2577 /* 2578 * ip_bind_connected() has stored the correct source 2579 * address in conn_src. 2580 */ 2581 src_addr = tcp->tcp_connp->conn_src; 2582 tcp->tcp_ipha->ipha_src = src_addr; 2583 /* 2584 * Copy of the src addr. in tcp_t is needed 2585 * for the lookup funcs. 2586 */ 2587 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2588 } 2589 /* 2590 * Set the fragment bit so that IP will tell us if the MTU 2591 * should change. IP tells us the latest setting of 2592 * ip_path_mtu_discovery through ire_frag_flag. 2593 */ 2594 if (ip_path_mtu_discovery) { 2595 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2596 htons(IPH_DF); 2597 } 2598 /* 2599 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2600 * for IP_NEXTHOP. No cache ire has been found for the 2601 * destination and we are working with the nexthop's 2602 * interface ire. Since we need to forward all packets 2603 * to the nexthop first, we "blindly" set tcp_localnet 2604 * to false, eventhough the destination may also be 2605 * onlink. 2606 */ 2607 if (ire_uinfo == NULL) 2608 tcp->tcp_localnet = 0; 2609 else 2610 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2611 } else { 2612 /* 2613 * For incoming connection ire_mp = NULL 2614 * For outgoing connection ire_mp != NULL 2615 * Technically we should check conn_incoming_ill 2616 * when ire_mp is NULL and conn_outgoing_ill when 2617 * ire_mp is non-NULL. But this is performance 2618 * critical path and for IPV*_BOUND_IF, outgoing 2619 * and incoming ill are always set to the same value. 2620 */ 2621 ill_t *dst_ill = NULL; 2622 ipif_t *dst_ipif = NULL; 2623 2624 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2625 2626 if (connp->conn_outgoing_ill != NULL) { 2627 /* Outgoing or incoming path */ 2628 int err; 2629 2630 dst_ill = conn_get_held_ill(connp, 2631 &connp->conn_outgoing_ill, &err); 2632 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2633 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2634 return (0); 2635 } 2636 match_flags |= MATCH_IRE_ILL; 2637 dst_ipif = dst_ill->ill_ipif; 2638 } 2639 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2640 0, 0, dst_ipif, zoneid, tsl, match_flags); 2641 2642 if (ire != NULL) { 2643 ire_cacheable = B_TRUE; 2644 ire_uinfo = (ire_mp != NULL) ? 2645 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2646 &ire->ire_uinfo; 2647 } else { 2648 if (ire_mp == NULL) { 2649 ire = ire_ftable_lookup_v6( 2650 &tcp->tcp_connp->conn_remv6, 2651 0, 0, 0, dst_ipif, &sire, zoneid, 2652 0, tsl, match_flags); 2653 if (ire == NULL) { 2654 if (dst_ill != NULL) 2655 ill_refrele(dst_ill); 2656 return (0); 2657 } 2658 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2659 &ire->ire_uinfo; 2660 } else { 2661 ire = (ire_t *)ire_mp->b_rptr; 2662 ire_uinfo = 2663 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2664 } 2665 } 2666 if (dst_ill != NULL) 2667 ill_refrele(dst_ill); 2668 2669 ASSERT(ire != NULL); 2670 ASSERT(ire_uinfo != NULL); 2671 2672 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2673 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2674 /* 2675 * ire->ire_mp is non null when ire_mp passed in is used 2676 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2677 */ 2678 if (ire->ire_mp == NULL) 2679 ire_refrele(ire); 2680 if (sire != NULL) 2681 ire_refrele(sire); 2682 return (0); 2683 } 2684 2685 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2686 in6_addr_t src_addr; 2687 2688 /* 2689 * ip_bind_connected_v6() has stored the correct source 2690 * address per IPv6 addr. selection policy in 2691 * conn_src_v6. 2692 */ 2693 src_addr = tcp->tcp_connp->conn_srcv6; 2694 2695 tcp->tcp_ip6h->ip6_src = src_addr; 2696 /* 2697 * Copy of the src addr. in tcp_t is needed 2698 * for the lookup funcs. 2699 */ 2700 tcp->tcp_ip_src_v6 = src_addr; 2701 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2702 &connp->conn_srcv6)); 2703 } 2704 tcp->tcp_localnet = 2705 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2706 } 2707 2708 /* 2709 * This allows applications to fail quickly when connections are made 2710 * to dead hosts. Hosts can be labeled dead by adding a reject route 2711 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2712 */ 2713 if ((ire->ire_flags & RTF_REJECT) && 2714 (ire->ire_flags & RTF_PRIVATE)) 2715 goto error; 2716 2717 /* 2718 * Make use of the cached rtt and rtt_sd values to calculate the 2719 * initial RTO. Note that they are already initialized in 2720 * tcp_init_values(). 2721 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2722 * IP_NEXTHOP, but instead are using the interface ire for the 2723 * nexthop, then we do not use the ire_uinfo from that ire to 2724 * do any initializations. 2725 */ 2726 if (ire_uinfo != NULL) { 2727 if (ire_uinfo->iulp_rtt != 0) { 2728 clock_t rto; 2729 2730 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2731 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2732 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2733 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5); 2734 2735 if (rto > tcp_rexmit_interval_max) { 2736 tcp->tcp_rto = tcp_rexmit_interval_max; 2737 } else if (rto < tcp_rexmit_interval_min) { 2738 tcp->tcp_rto = tcp_rexmit_interval_min; 2739 } else { 2740 tcp->tcp_rto = rto; 2741 } 2742 } 2743 if (ire_uinfo->iulp_ssthresh != 0) 2744 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2745 else 2746 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2747 if (ire_uinfo->iulp_spipe > 0) { 2748 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2749 tcp_max_buf); 2750 if (tcp_snd_lowat_fraction != 0) 2751 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2752 tcp_snd_lowat_fraction; 2753 (void) tcp_maxpsz_set(tcp, B_TRUE); 2754 } 2755 /* 2756 * Note that up till now, acceptor always inherits receive 2757 * window from the listener. But if there is a metrics 2758 * associated with a host, we should use that instead of 2759 * inheriting it from listener. Thus we need to pass this 2760 * info back to the caller. 2761 */ 2762 if (ire_uinfo->iulp_rpipe > 0) { 2763 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, tcp_max_buf); 2764 } 2765 2766 if (ire_uinfo->iulp_rtomax > 0) { 2767 tcp->tcp_second_timer_threshold = 2768 ire_uinfo->iulp_rtomax; 2769 } 2770 2771 /* 2772 * Use the metric option settings, iulp_tstamp_ok and 2773 * iulp_wscale_ok, only for active open. What this means 2774 * is that if the other side uses timestamp or window 2775 * scale option, TCP will also use those options. That 2776 * is for passive open. If the application sets a 2777 * large window, window scale is enabled regardless of 2778 * the value in iulp_wscale_ok. This is the behavior 2779 * since 2.6. So we keep it. 2780 * The only case left in passive open processing is the 2781 * check for SACK. 2782 * For ECN, it should probably be like SACK. But the 2783 * current value is binary, so we treat it like the other 2784 * cases. The metric only controls active open.For passive 2785 * open, the ndd param, tcp_ecn_permitted, controls the 2786 * behavior. 2787 */ 2788 if (!tcp_detached) { 2789 /* 2790 * The if check means that the following can only 2791 * be turned on by the metrics only IRE, but not off. 2792 */ 2793 if (ire_uinfo->iulp_tstamp_ok) 2794 tcp->tcp_snd_ts_ok = B_TRUE; 2795 if (ire_uinfo->iulp_wscale_ok) 2796 tcp->tcp_snd_ws_ok = B_TRUE; 2797 if (ire_uinfo->iulp_sack == 2) 2798 tcp->tcp_snd_sack_ok = B_TRUE; 2799 if (ire_uinfo->iulp_ecn_ok) 2800 tcp->tcp_ecn_ok = B_TRUE; 2801 } else { 2802 /* 2803 * Passive open. 2804 * 2805 * As above, the if check means that SACK can only be 2806 * turned on by the metric only IRE. 2807 */ 2808 if (ire_uinfo->iulp_sack > 0) { 2809 tcp->tcp_snd_sack_ok = B_TRUE; 2810 } 2811 } 2812 } 2813 2814 2815 /* 2816 * XXX: Note that currently, ire_max_frag can be as small as 68 2817 * because of PMTUd. So tcp_mss may go to negative if combined 2818 * length of all those options exceeds 28 bytes. But because 2819 * of the tcp_mss_min check below, we may not have a problem if 2820 * tcp_mss_min is of a reasonable value. The default is 1 so 2821 * the negative problem still exists. And the check defeats PMTUd. 2822 * In fact, if PMTUd finds that the MSS should be smaller than 2823 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2824 * value. 2825 * 2826 * We do not deal with that now. All those problems related to 2827 * PMTUd will be fixed later. 2828 */ 2829 ASSERT(ire->ire_max_frag != 0); 2830 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2831 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2832 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2833 mss = MIN(mss, IPV6_MIN_MTU); 2834 } 2835 } 2836 2837 /* Sanity check for MSS value. */ 2838 if (tcp->tcp_ipversion == IPV4_VERSION) 2839 mss_max = tcp_mss_max_ipv4; 2840 else 2841 mss_max = tcp_mss_max_ipv6; 2842 2843 if (tcp->tcp_ipversion == IPV6_VERSION && 2844 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2845 /* 2846 * After receiving an ICMPv6 "packet too big" message with a 2847 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2848 * will insert a 8-byte fragment header in every packet; we 2849 * reduce the MSS by that amount here. 2850 */ 2851 mss -= sizeof (ip6_frag_t); 2852 } 2853 2854 if (tcp->tcp_ipsec_overhead == 0) 2855 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2856 2857 mss -= tcp->tcp_ipsec_overhead; 2858 2859 if (mss < tcp_mss_min) 2860 mss = tcp_mss_min; 2861 if (mss > mss_max) 2862 mss = mss_max; 2863 2864 /* Note that this is the maximum MSS, excluding all options. */ 2865 tcp->tcp_mss = mss; 2866 2867 /* 2868 * Initialize the ISS here now that we have the full connection ID. 2869 * The RFC 1948 method of initial sequence number generation requires 2870 * knowledge of the full connection ID before setting the ISS. 2871 */ 2872 2873 tcp_iss_init(tcp); 2874 2875 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2876 tcp->tcp_loopback = B_TRUE; 2877 2878 if (tcp->tcp_ipversion == IPV4_VERSION) { 2879 hsp = tcp_hsp_lookup(tcp->tcp_remote); 2880 } else { 2881 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6); 2882 } 2883 2884 if (hsp != NULL) { 2885 /* Only modify if we're going to make them bigger */ 2886 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2887 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2888 if (tcp_snd_lowat_fraction != 0) 2889 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2890 tcp_snd_lowat_fraction; 2891 } 2892 2893 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2894 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2895 } 2896 2897 /* Copy timestamp flag only for active open */ 2898 if (!tcp_detached) 2899 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2900 } 2901 2902 if (sire != NULL) 2903 IRE_REFRELE(sire); 2904 2905 /* 2906 * If we got an IRE_CACHE and an ILL, go through their properties; 2907 * otherwise, this is deferred until later when we have an IRE_CACHE. 2908 */ 2909 if (tcp->tcp_loopback || 2910 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2911 /* 2912 * For incoming, see if this tcp may be MDT-capable. For 2913 * outgoing, this process has been taken care of through 2914 * tcp_rput_other. 2915 */ 2916 tcp_ire_ill_check(tcp, ire, ill, incoming); 2917 tcp->tcp_ire_ill_check_done = B_TRUE; 2918 } 2919 2920 mutex_enter(&connp->conn_lock); 2921 /* 2922 * Make sure that conn is not marked incipient 2923 * for incoming connections. A blind 2924 * removal of incipient flag is cheaper than 2925 * check and removal. 2926 */ 2927 connp->conn_state_flags &= ~CONN_INCIPIENT; 2928 2929 /* Must not cache forwarding table routes. */ 2930 if (ire_cacheable) { 2931 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2932 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2933 connp->conn_ire_cache = ire; 2934 IRE_UNTRACE_REF(ire); 2935 rw_exit(&ire->ire_bucket->irb_lock); 2936 mutex_exit(&connp->conn_lock); 2937 return (1); 2938 } 2939 rw_exit(&ire->ire_bucket->irb_lock); 2940 } 2941 mutex_exit(&connp->conn_lock); 2942 2943 if (ire->ire_mp == NULL) 2944 ire_refrele(ire); 2945 return (1); 2946 2947 error: 2948 if (ire->ire_mp == NULL) 2949 ire_refrele(ire); 2950 if (sire != NULL) 2951 ire_refrele(sire); 2952 return (0); 2953 } 2954 2955 /* 2956 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2957 * O_T_BIND_REQ/T_BIND_REQ message. 2958 */ 2959 static void 2960 tcp_bind(tcp_t *tcp, mblk_t *mp) 2961 { 2962 sin_t *sin; 2963 sin6_t *sin6; 2964 mblk_t *mp1; 2965 in_port_t requested_port; 2966 in_port_t allocated_port; 2967 struct T_bind_req *tbr; 2968 boolean_t bind_to_req_port_only; 2969 boolean_t backlog_update = B_FALSE; 2970 boolean_t user_specified; 2971 in6_addr_t v6addr; 2972 ipaddr_t v4addr; 2973 uint_t origipversion; 2974 int err; 2975 queue_t *q = tcp->tcp_wq; 2976 conn_t *connp; 2977 mlp_type_t addrtype, mlptype; 2978 zone_t *zone; 2979 cred_t *cr; 2980 in_port_t mlp_port; 2981 2982 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 2983 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 2984 if (tcp->tcp_debug) { 2985 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 2986 "tcp_bind: bad req, len %u", 2987 (uint_t)(mp->b_wptr - mp->b_rptr)); 2988 } 2989 tcp_err_ack(tcp, mp, TPROTO, 0); 2990 return; 2991 } 2992 /* Make sure the largest address fits */ 2993 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 2994 if (mp1 == NULL) { 2995 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 2996 return; 2997 } 2998 mp = mp1; 2999 tbr = (struct T_bind_req *)mp->b_rptr; 3000 if (tcp->tcp_state >= TCPS_BOUND) { 3001 if ((tcp->tcp_state == TCPS_BOUND || 3002 tcp->tcp_state == TCPS_LISTEN) && 3003 tcp->tcp_conn_req_max != tbr->CONIND_number && 3004 tbr->CONIND_number > 0) { 3005 /* 3006 * Handle listen() increasing CONIND_number. 3007 * This is more "liberal" then what the TPI spec 3008 * requires but is needed to avoid a t_unbind 3009 * when handling listen() since the port number 3010 * might be "stolen" between the unbind and bind. 3011 */ 3012 backlog_update = B_TRUE; 3013 goto do_bind; 3014 } 3015 if (tcp->tcp_debug) { 3016 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3017 "tcp_bind: bad state, %d", tcp->tcp_state); 3018 } 3019 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 3020 return; 3021 } 3022 origipversion = tcp->tcp_ipversion; 3023 3024 switch (tbr->ADDR_length) { 3025 case 0: /* request for a generic port */ 3026 tbr->ADDR_offset = sizeof (struct T_bind_req); 3027 if (tcp->tcp_family == AF_INET) { 3028 tbr->ADDR_length = sizeof (sin_t); 3029 sin = (sin_t *)&tbr[1]; 3030 *sin = sin_null; 3031 sin->sin_family = AF_INET; 3032 mp->b_wptr = (uchar_t *)&sin[1]; 3033 tcp->tcp_ipversion = IPV4_VERSION; 3034 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3035 } else { 3036 ASSERT(tcp->tcp_family == AF_INET6); 3037 tbr->ADDR_length = sizeof (sin6_t); 3038 sin6 = (sin6_t *)&tbr[1]; 3039 *sin6 = sin6_null; 3040 sin6->sin6_family = AF_INET6; 3041 mp->b_wptr = (uchar_t *)&sin6[1]; 3042 tcp->tcp_ipversion = IPV6_VERSION; 3043 V6_SET_ZERO(v6addr); 3044 } 3045 requested_port = 0; 3046 break; 3047 3048 case sizeof (sin_t): /* Complete IPv4 address */ 3049 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3050 sizeof (sin_t)); 3051 if (sin == NULL || !OK_32PTR((char *)sin)) { 3052 if (tcp->tcp_debug) { 3053 (void) strlog(TCP_MOD_ID, 0, 1, 3054 SL_ERROR|SL_TRACE, 3055 "tcp_bind: bad address parameter, " 3056 "offset %d, len %d", 3057 tbr->ADDR_offset, tbr->ADDR_length); 3058 } 3059 tcp_err_ack(tcp, mp, TPROTO, 0); 3060 return; 3061 } 3062 /* 3063 * With sockets sockfs will accept bogus sin_family in 3064 * bind() and replace it with the family used in the socket 3065 * call. 3066 */ 3067 if (sin->sin_family != AF_INET || 3068 tcp->tcp_family != AF_INET) { 3069 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3070 return; 3071 } 3072 requested_port = ntohs(sin->sin_port); 3073 tcp->tcp_ipversion = IPV4_VERSION; 3074 v4addr = sin->sin_addr.s_addr; 3075 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3076 break; 3077 3078 case sizeof (sin6_t): /* Complete IPv6 address */ 3079 sin6 = (sin6_t *)mi_offset_param(mp, 3080 tbr->ADDR_offset, sizeof (sin6_t)); 3081 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3082 if (tcp->tcp_debug) { 3083 (void) strlog(TCP_MOD_ID, 0, 1, 3084 SL_ERROR|SL_TRACE, 3085 "tcp_bind: bad IPv6 address parameter, " 3086 "offset %d, len %d", tbr->ADDR_offset, 3087 tbr->ADDR_length); 3088 } 3089 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3090 return; 3091 } 3092 if (sin6->sin6_family != AF_INET6 || 3093 tcp->tcp_family != AF_INET6) { 3094 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3095 return; 3096 } 3097 requested_port = ntohs(sin6->sin6_port); 3098 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3099 IPV4_VERSION : IPV6_VERSION; 3100 v6addr = sin6->sin6_addr; 3101 break; 3102 3103 default: 3104 if (tcp->tcp_debug) { 3105 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3106 "tcp_bind: bad address length, %d", 3107 tbr->ADDR_length); 3108 } 3109 tcp_err_ack(tcp, mp, TBADADDR, 0); 3110 return; 3111 } 3112 tcp->tcp_bound_source_v6 = v6addr; 3113 3114 /* Check for change in ipversion */ 3115 if (origipversion != tcp->tcp_ipversion) { 3116 ASSERT(tcp->tcp_family == AF_INET6); 3117 err = tcp->tcp_ipversion == IPV6_VERSION ? 3118 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3119 if (err) { 3120 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3121 return; 3122 } 3123 } 3124 3125 /* 3126 * Initialize family specific fields. Copy of the src addr. 3127 * in tcp_t is needed for the lookup funcs. 3128 */ 3129 if (tcp->tcp_ipversion == IPV6_VERSION) { 3130 tcp->tcp_ip6h->ip6_src = v6addr; 3131 } else { 3132 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3133 } 3134 tcp->tcp_ip_src_v6 = v6addr; 3135 3136 /* 3137 * For O_T_BIND_REQ: 3138 * Verify that the target port/addr is available, or choose 3139 * another. 3140 * For T_BIND_REQ: 3141 * Verify that the target port/addr is available or fail. 3142 * In both cases when it succeeds the tcp is inserted in the 3143 * bind hash table. This ensures that the operation is atomic 3144 * under the lock on the hash bucket. 3145 */ 3146 bind_to_req_port_only = requested_port != 0 && 3147 tbr->PRIM_type != O_T_BIND_REQ; 3148 /* 3149 * Get a valid port (within the anonymous range and should not 3150 * be a privileged one) to use if the user has not given a port. 3151 * If multiple threads are here, they may all start with 3152 * with the same initial port. But, it should be fine as long as 3153 * tcp_bindi will ensure that no two threads will be assigned 3154 * the same port. 3155 * 3156 * NOTE: XXX If a privileged process asks for an anonymous port, we 3157 * still check for ports only in the range > tcp_smallest_non_priv_port, 3158 * unless TCP_ANONPRIVBIND option is set. 3159 */ 3160 mlptype = mlptSingle; 3161 mlp_port = requested_port; 3162 if (requested_port == 0) { 3163 requested_port = tcp->tcp_anon_priv_bind ? 3164 tcp_get_next_priv_port(tcp) : 3165 tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 3166 if (requested_port == 0) { 3167 tcp_err_ack(tcp, mp, TNOADDR, 0); 3168 return; 3169 } 3170 user_specified = B_FALSE; 3171 3172 /* 3173 * If the user went through one of the RPC interfaces to create 3174 * this socket and RPC is MLP in this zone, then give him an 3175 * anonymous MLP. 3176 */ 3177 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3178 connp = tcp->tcp_connp; 3179 if (connp->conn_anon_mlp && is_system_labeled()) { 3180 zone = crgetzone(cr); 3181 addrtype = tsol_mlp_addr_type(zone->zone_id, 3182 IPV6_VERSION, &v6addr); 3183 if (addrtype == mlptSingle) { 3184 tcp_err_ack(tcp, mp, TNOADDR, 0); 3185 return; 3186 } 3187 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3188 PMAPPORT, addrtype); 3189 mlp_port = PMAPPORT; 3190 } 3191 } else { 3192 int i; 3193 boolean_t priv = B_FALSE; 3194 3195 /* 3196 * If the requested_port is in the well-known privileged range, 3197 * verify that the stream was opened by a privileged user. 3198 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3199 * but instead the code relies on: 3200 * - the fact that the address of the array and its size never 3201 * changes 3202 * - the atomic assignment of the elements of the array 3203 */ 3204 cr = DB_CREDDEF(mp, tcp->tcp_cred); 3205 if (requested_port < tcp_smallest_nonpriv_port) { 3206 priv = B_TRUE; 3207 } else { 3208 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 3209 if (requested_port == 3210 tcp_g_epriv_ports[i]) { 3211 priv = B_TRUE; 3212 break; 3213 } 3214 } 3215 } 3216 if (priv) { 3217 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3218 if (tcp->tcp_debug) { 3219 (void) strlog(TCP_MOD_ID, 0, 1, 3220 SL_ERROR|SL_TRACE, 3221 "tcp_bind: no priv for port %d", 3222 requested_port); 3223 } 3224 tcp_err_ack(tcp, mp, TACCES, 0); 3225 return; 3226 } 3227 } 3228 user_specified = B_TRUE; 3229 3230 connp = tcp->tcp_connp; 3231 if (is_system_labeled()) { 3232 zone = crgetzone(cr); 3233 addrtype = tsol_mlp_addr_type(zone->zone_id, 3234 IPV6_VERSION, &v6addr); 3235 if (addrtype == mlptSingle) { 3236 tcp_err_ack(tcp, mp, TNOADDR, 0); 3237 return; 3238 } 3239 mlptype = tsol_mlp_port_type(zone, IPPROTO_TCP, 3240 requested_port, addrtype); 3241 } 3242 } 3243 3244 if (mlptype != mlptSingle) { 3245 if (secpolicy_net_bindmlp(cr) != 0) { 3246 if (tcp->tcp_debug) { 3247 (void) strlog(TCP_MOD_ID, 0, 1, 3248 SL_ERROR|SL_TRACE, 3249 "tcp_bind: no priv for multilevel port %d", 3250 requested_port); 3251 } 3252 tcp_err_ack(tcp, mp, TACCES, 0); 3253 return; 3254 } 3255 3256 /* 3257 * If we're specifically binding a shared IP address and the 3258 * port is MLP on shared addresses, then check to see if this 3259 * zone actually owns the MLP. Reject if not. 3260 */ 3261 if (mlptype == mlptShared && addrtype == mlptShared) { 3262 zoneid_t mlpzone; 3263 3264 mlpzone = tsol_mlp_findzone(IPPROTO_TCP, 3265 htons(mlp_port)); 3266 if (connp->conn_zoneid != mlpzone) { 3267 if (tcp->tcp_debug) { 3268 (void) strlog(TCP_MOD_ID, 0, 1, 3269 SL_ERROR|SL_TRACE, 3270 "tcp_bind: attempt to bind port " 3271 "%d on shared addr in zone %d " 3272 "(should be %d)", 3273 mlp_port, connp->conn_zoneid, 3274 mlpzone); 3275 } 3276 tcp_err_ack(tcp, mp, TACCES, 0); 3277 return; 3278 } 3279 } 3280 3281 if (!user_specified) { 3282 err = tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3283 requested_port, B_TRUE); 3284 if (err != 0) { 3285 if (tcp->tcp_debug) { 3286 (void) strlog(TCP_MOD_ID, 0, 1, 3287 SL_ERROR|SL_TRACE, 3288 "tcp_bind: cannot establish anon " 3289 "MLP for port %d", 3290 requested_port); 3291 } 3292 tcp_err_ack(tcp, mp, TSYSERR, err); 3293 return; 3294 } 3295 connp->conn_anon_port = B_TRUE; 3296 } 3297 connp->conn_mlp_type = mlptype; 3298 } 3299 3300 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3301 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3302 3303 if (allocated_port == 0) { 3304 connp->conn_mlp_type = mlptSingle; 3305 if (connp->conn_anon_port) { 3306 connp->conn_anon_port = B_FALSE; 3307 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3308 requested_port, B_FALSE); 3309 } 3310 if (bind_to_req_port_only) { 3311 if (tcp->tcp_debug) { 3312 (void) strlog(TCP_MOD_ID, 0, 1, 3313 SL_ERROR|SL_TRACE, 3314 "tcp_bind: requested addr busy"); 3315 } 3316 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3317 } else { 3318 /* If we are out of ports, fail the bind. */ 3319 if (tcp->tcp_debug) { 3320 (void) strlog(TCP_MOD_ID, 0, 1, 3321 SL_ERROR|SL_TRACE, 3322 "tcp_bind: out of ports?"); 3323 } 3324 tcp_err_ack(tcp, mp, TNOADDR, 0); 3325 } 3326 return; 3327 } 3328 ASSERT(tcp->tcp_state == TCPS_BOUND); 3329 do_bind: 3330 if (!backlog_update) { 3331 if (tcp->tcp_family == AF_INET) 3332 sin->sin_port = htons(allocated_port); 3333 else 3334 sin6->sin6_port = htons(allocated_port); 3335 } 3336 if (tcp->tcp_family == AF_INET) { 3337 if (tbr->CONIND_number != 0) { 3338 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3339 sizeof (sin_t)); 3340 } else { 3341 /* Just verify the local IP address */ 3342 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3343 } 3344 } else { 3345 if (tbr->CONIND_number != 0) { 3346 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3347 sizeof (sin6_t)); 3348 } else { 3349 /* Just verify the local IP address */ 3350 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3351 IPV6_ADDR_LEN); 3352 } 3353 } 3354 if (mp1 == NULL) { 3355 if (connp->conn_anon_port) { 3356 connp->conn_anon_port = B_FALSE; 3357 (void) tsol_mlp_anon(zone, mlptype, connp->conn_ulp, 3358 requested_port, B_FALSE); 3359 } 3360 connp->conn_mlp_type = mlptSingle; 3361 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3362 return; 3363 } 3364 3365 tbr->PRIM_type = T_BIND_ACK; 3366 mp->b_datap->db_type = M_PCPROTO; 3367 3368 /* Chain in the reply mp for tcp_rput() */ 3369 mp1->b_cont = mp; 3370 mp = mp1; 3371 3372 tcp->tcp_conn_req_max = tbr->CONIND_number; 3373 if (tcp->tcp_conn_req_max) { 3374 if (tcp->tcp_conn_req_max < tcp_conn_req_min) 3375 tcp->tcp_conn_req_max = tcp_conn_req_min; 3376 if (tcp->tcp_conn_req_max > tcp_conn_req_max_q) 3377 tcp->tcp_conn_req_max = tcp_conn_req_max_q; 3378 /* 3379 * If this is a listener, do not reset the eager list 3380 * and other stuffs. Note that we don't check if the 3381 * existing eager list meets the new tcp_conn_req_max 3382 * requirement. 3383 */ 3384 if (tcp->tcp_state != TCPS_LISTEN) { 3385 tcp->tcp_state = TCPS_LISTEN; 3386 /* Initialize the chain. Don't need the eager_lock */ 3387 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3388 tcp->tcp_second_ctimer_threshold = 3389 tcp_ip_abort_linterval; 3390 } 3391 } 3392 3393 /* 3394 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3395 * processing continues in tcp_rput_other(). 3396 */ 3397 if (tcp->tcp_family == AF_INET6) { 3398 ASSERT(tcp->tcp_connp->conn_af_isv6); 3399 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3400 } else { 3401 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3402 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3403 } 3404 /* 3405 * If the bind cannot complete immediately 3406 * IP will arrange to call tcp_rput_other 3407 * when the bind completes. 3408 */ 3409 if (mp != NULL) { 3410 tcp_rput_other(tcp, mp); 3411 } else { 3412 /* 3413 * Bind will be resumed later. Need to ensure 3414 * that conn doesn't disappear when that happens. 3415 * This will be decremented in ip_resume_tcp_bind(). 3416 */ 3417 CONN_INC_REF(tcp->tcp_connp); 3418 } 3419 } 3420 3421 3422 /* 3423 * If the "bind_to_req_port_only" parameter is set, if the requested port 3424 * number is available, return it, If not return 0 3425 * 3426 * If "bind_to_req_port_only" parameter is not set and 3427 * If the requested port number is available, return it. If not, return 3428 * the first anonymous port we happen across. If no anonymous ports are 3429 * available, return 0. addr is the requested local address, if any. 3430 * 3431 * In either case, when succeeding update the tcp_t to record the port number 3432 * and insert it in the bind hash table. 3433 * 3434 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3435 * without setting SO_REUSEADDR. This is needed so that they 3436 * can be viewed as two independent transport protocols. 3437 */ 3438 static in_port_t 3439 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3440 int reuseaddr, boolean_t quick_connect, 3441 boolean_t bind_to_req_port_only, boolean_t user_specified) 3442 { 3443 /* number of times we have run around the loop */ 3444 int count = 0; 3445 /* maximum number of times to run around the loop */ 3446 int loopmax; 3447 conn_t *connp = tcp->tcp_connp; 3448 zoneid_t zoneid = connp->conn_zoneid; 3449 3450 /* 3451 * Lookup for free addresses is done in a loop and "loopmax" 3452 * influences how long we spin in the loop 3453 */ 3454 if (bind_to_req_port_only) { 3455 /* 3456 * If the requested port is busy, don't bother to look 3457 * for a new one. Setting loop maximum count to 1 has 3458 * that effect. 3459 */ 3460 loopmax = 1; 3461 } else { 3462 /* 3463 * If the requested port is busy, look for a free one 3464 * in the anonymous port range. 3465 * Set loopmax appropriately so that one does not look 3466 * forever in the case all of the anonymous ports are in use. 3467 */ 3468 if (tcp->tcp_anon_priv_bind) { 3469 /* 3470 * loopmax = 3471 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3472 */ 3473 loopmax = IPPORT_RESERVED - tcp_min_anonpriv_port; 3474 } else { 3475 loopmax = (tcp_largest_anon_port - 3476 tcp_smallest_anon_port + 1); 3477 } 3478 } 3479 do { 3480 uint16_t lport; 3481 tf_t *tbf; 3482 tcp_t *ltcp; 3483 conn_t *lconnp; 3484 3485 lport = htons(port); 3486 3487 /* 3488 * Ensure that the tcp_t is not currently in the bind hash. 3489 * Hold the lock on the hash bucket to ensure that 3490 * the duplicate check plus the insertion is an atomic 3491 * operation. 3492 * 3493 * This function does an inline lookup on the bind hash list 3494 * Make sure that we access only members of tcp_t 3495 * and that we don't look at tcp_tcp, since we are not 3496 * doing a CONN_INC_REF. 3497 */ 3498 tcp_bind_hash_remove(tcp); 3499 tbf = &tcp_bind_fanout[TCP_BIND_HASH(lport)]; 3500 mutex_enter(&tbf->tf_lock); 3501 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3502 ltcp = ltcp->tcp_bind_hash) { 3503 if (lport != ltcp->tcp_lport) 3504 continue; 3505 3506 lconnp = ltcp->tcp_connp; 3507 3508 /* 3509 * On a labeled system, we must treat bindings to ports 3510 * on shared IP addresses by sockets with MAC exemption 3511 * privilege as being in all zones, as there's 3512 * otherwise no way to identify the right receiver. 3513 */ 3514 if (!IPCL_ZONE_MATCH(ltcp->tcp_connp, zoneid) && 3515 !lconnp->conn_mac_exempt && 3516 !connp->conn_mac_exempt) 3517 continue; 3518 3519 /* 3520 * If TCP_EXCLBIND is set for either the bound or 3521 * binding endpoint, the semantics of bind 3522 * is changed according to the following. 3523 * 3524 * spec = specified address (v4 or v6) 3525 * unspec = unspecified address (v4 or v6) 3526 * A = specified addresses are different for endpoints 3527 * 3528 * bound bind to allowed 3529 * ------------------------------------- 3530 * unspec unspec no 3531 * unspec spec no 3532 * spec unspec no 3533 * spec spec yes if A 3534 * 3535 * For labeled systems, SO_MAC_EXEMPT behaves the same 3536 * as UDP_EXCLBIND, except that zoneid is ignored. 3537 * 3538 * Note: 3539 * 3540 * 1. Because of TLI semantics, an endpoint can go 3541 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3542 * TCPS_BOUND, depending on whether it is originally 3543 * a listener or not. That is why we need to check 3544 * for states greater than or equal to TCPS_BOUND 3545 * here. 3546 * 3547 * 2. Ideally, we should only check for state equals 3548 * to TCPS_LISTEN. And the following check should be 3549 * added. 3550 * 3551 * if (ltcp->tcp_state == TCPS_LISTEN || 3552 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3553 * ... 3554 * } 3555 * 3556 * The semantics will be changed to this. If the 3557 * endpoint on the list is in state not equal to 3558 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3559 * set, let the bind succeed. 3560 * 3561 * But because of (1), we cannot do that now. If 3562 * in future, we can change this going back semantics, 3563 * we can add the above check. 3564 */ 3565 if (ltcp->tcp_exclbind || tcp->tcp_exclbind || 3566 lconnp->conn_mac_exempt || connp->conn_mac_exempt) { 3567 if (V6_OR_V4_INADDR_ANY( 3568 ltcp->tcp_bound_source_v6) || 3569 V6_OR_V4_INADDR_ANY(*laddr) || 3570 IN6_ARE_ADDR_EQUAL(laddr, 3571 <cp->tcp_bound_source_v6)) { 3572 break; 3573 } 3574 continue; 3575 } 3576 3577 /* 3578 * Check ipversion to allow IPv4 and IPv6 sockets to 3579 * have disjoint port number spaces, if *_EXCLBIND 3580 * is not set and only if the application binds to a 3581 * specific port. We use the same autoassigned port 3582 * number space for IPv4 and IPv6 sockets. 3583 */ 3584 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3585 bind_to_req_port_only) 3586 continue; 3587 3588 /* 3589 * Ideally, we should make sure that the source 3590 * address, remote address, and remote port in the 3591 * four tuple for this tcp-connection is unique. 3592 * However, trying to find out the local source 3593 * address would require too much code duplication 3594 * with IP, since IP needs needs to have that code 3595 * to support userland TCP implementations. 3596 */ 3597 if (quick_connect && 3598 (ltcp->tcp_state > TCPS_LISTEN) && 3599 ((tcp->tcp_fport != ltcp->tcp_fport) || 3600 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3601 <cp->tcp_remote_v6))) 3602 continue; 3603 3604 if (!reuseaddr) { 3605 /* 3606 * No socket option SO_REUSEADDR. 3607 * If existing port is bound to 3608 * a non-wildcard IP address 3609 * and the requesting stream is 3610 * bound to a distinct 3611 * different IP addresses 3612 * (non-wildcard, also), keep 3613 * going. 3614 */ 3615 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3616 !V6_OR_V4_INADDR_ANY( 3617 ltcp->tcp_bound_source_v6) && 3618 !IN6_ARE_ADDR_EQUAL(laddr, 3619 <cp->tcp_bound_source_v6)) 3620 continue; 3621 if (ltcp->tcp_state >= TCPS_BOUND) { 3622 /* 3623 * This port is being used and 3624 * its state is >= TCPS_BOUND, 3625 * so we can't bind to it. 3626 */ 3627 break; 3628 } 3629 } else { 3630 /* 3631 * socket option SO_REUSEADDR is set on the 3632 * binding tcp_t. 3633 * 3634 * If two streams are bound to 3635 * same IP address or both addr 3636 * and bound source are wildcards 3637 * (INADDR_ANY), we want to stop 3638 * searching. 3639 * We have found a match of IP source 3640 * address and source port, which is 3641 * refused regardless of the 3642 * SO_REUSEADDR setting, so we break. 3643 */ 3644 if (IN6_ARE_ADDR_EQUAL(laddr, 3645 <cp->tcp_bound_source_v6) && 3646 (ltcp->tcp_state == TCPS_LISTEN || 3647 ltcp->tcp_state == TCPS_BOUND)) 3648 break; 3649 } 3650 } 3651 if (ltcp != NULL) { 3652 /* The port number is busy */ 3653 mutex_exit(&tbf->tf_lock); 3654 } else { 3655 /* 3656 * This port is ours. Insert in fanout and mark as 3657 * bound to prevent others from getting the port 3658 * number. 3659 */ 3660 tcp->tcp_state = TCPS_BOUND; 3661 tcp->tcp_lport = htons(port); 3662 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3663 3664 ASSERT(&tcp_bind_fanout[TCP_BIND_HASH( 3665 tcp->tcp_lport)] == tbf); 3666 tcp_bind_hash_insert(tbf, tcp, 1); 3667 3668 mutex_exit(&tbf->tf_lock); 3669 3670 /* 3671 * We don't want tcp_next_port_to_try to "inherit" 3672 * a port number supplied by the user in a bind. 3673 */ 3674 if (user_specified) 3675 return (port); 3676 3677 /* 3678 * This is the only place where tcp_next_port_to_try 3679 * is updated. After the update, it may or may not 3680 * be in the valid range. 3681 */ 3682 if (!tcp->tcp_anon_priv_bind) 3683 tcp_next_port_to_try = port + 1; 3684 return (port); 3685 } 3686 3687 if (tcp->tcp_anon_priv_bind) { 3688 port = tcp_get_next_priv_port(tcp); 3689 } else { 3690 if (count == 0 && user_specified) { 3691 /* 3692 * We may have to return an anonymous port. So 3693 * get one to start with. 3694 */ 3695 port = 3696 tcp_update_next_port(tcp_next_port_to_try, 3697 tcp, B_TRUE); 3698 user_specified = B_FALSE; 3699 } else { 3700 port = tcp_update_next_port(port + 1, tcp, 3701 B_FALSE); 3702 } 3703 } 3704 if (port == 0) 3705 break; 3706 3707 /* 3708 * Don't let this loop run forever in the case where 3709 * all of the anonymous ports are in use. 3710 */ 3711 } while (++count < loopmax); 3712 return (0); 3713 } 3714 3715 /* 3716 * We are dying for some reason. Try to do it gracefully. (May be called 3717 * as writer.) 3718 * 3719 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3720 * done by a service procedure). 3721 * TBD - Should the return value distinguish between the tcp_t being 3722 * freed and it being reinitialized? 3723 */ 3724 static int 3725 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3726 { 3727 mblk_t *mp; 3728 queue_t *q; 3729 3730 TCP_CLD_STAT(tag); 3731 3732 #if TCP_TAG_CLEAN_DEATH 3733 tcp->tcp_cleandeathtag = tag; 3734 #endif 3735 3736 if (tcp->tcp_fused) 3737 tcp_unfuse(tcp); 3738 3739 if (tcp->tcp_linger_tid != 0 && 3740 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3741 tcp_stop_lingering(tcp); 3742 } 3743 3744 ASSERT(tcp != NULL); 3745 ASSERT((tcp->tcp_family == AF_INET && 3746 tcp->tcp_ipversion == IPV4_VERSION) || 3747 (tcp->tcp_family == AF_INET6 && 3748 (tcp->tcp_ipversion == IPV4_VERSION || 3749 tcp->tcp_ipversion == IPV6_VERSION))); 3750 3751 if (TCP_IS_DETACHED(tcp)) { 3752 if (tcp->tcp_hard_binding) { 3753 /* 3754 * Its an eager that we are dealing with. We close the 3755 * eager but in case a conn_ind has already gone to the 3756 * listener, let tcp_accept_finish() send a discon_ind 3757 * to the listener and drop the last reference. If the 3758 * listener doesn't even know about the eager i.e. the 3759 * conn_ind hasn't gone up, blow away the eager and drop 3760 * the last reference as well. If the conn_ind has gone 3761 * up, state should be BOUND. tcp_accept_finish 3762 * will figure out that the connection has received a 3763 * RST and will send a DISCON_IND to the application. 3764 */ 3765 tcp_closei_local(tcp); 3766 if (tcp->tcp_conn.tcp_eager_conn_ind != NULL) { 3767 CONN_DEC_REF(tcp->tcp_connp); 3768 } else { 3769 tcp->tcp_state = TCPS_BOUND; 3770 } 3771 } else { 3772 tcp_close_detached(tcp); 3773 } 3774 return (0); 3775 } 3776 3777 TCP_STAT(tcp_clean_death_nondetached); 3778 3779 /* 3780 * If T_ORDREL_IND has not been sent yet (done when service routine 3781 * is run) postpone cleaning up the endpoint until service routine 3782 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3783 * client_errno since tcp_close uses the client_errno field. 3784 */ 3785 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3786 if (err != 0) 3787 tcp->tcp_client_errno = err; 3788 3789 tcp->tcp_deferred_clean_death = B_TRUE; 3790 return (-1); 3791 } 3792 3793 q = tcp->tcp_rq; 3794 3795 /* Trash all inbound data */ 3796 flushq(q, FLUSHALL); 3797 3798 /* 3799 * If we are at least part way open and there is error 3800 * (err==0 implies no error) 3801 * notify our client by a T_DISCON_IND. 3802 */ 3803 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3804 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3805 !TCP_IS_SOCKET(tcp)) { 3806 /* 3807 * Send M_FLUSH according to TPI. Because sockets will 3808 * (and must) ignore FLUSHR we do that only for TPI 3809 * endpoints and sockets in STREAMS mode. 3810 */ 3811 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3812 } 3813 if (tcp->tcp_debug) { 3814 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3815 "tcp_clean_death: discon err %d", err); 3816 } 3817 mp = mi_tpi_discon_ind(NULL, err, 0); 3818 if (mp != NULL) { 3819 putnext(q, mp); 3820 } else { 3821 if (tcp->tcp_debug) { 3822 (void) strlog(TCP_MOD_ID, 0, 1, 3823 SL_ERROR|SL_TRACE, 3824 "tcp_clean_death, sending M_ERROR"); 3825 } 3826 (void) putnextctl1(q, M_ERROR, EPROTO); 3827 } 3828 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3829 /* SYN_SENT or SYN_RCVD */ 3830 BUMP_MIB(&tcp_mib, tcpAttemptFails); 3831 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3832 /* ESTABLISHED or CLOSE_WAIT */ 3833 BUMP_MIB(&tcp_mib, tcpEstabResets); 3834 } 3835 } 3836 3837 tcp_reinit(tcp); 3838 return (-1); 3839 } 3840 3841 /* 3842 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3843 * to expire, stop the wait and finish the close. 3844 */ 3845 static void 3846 tcp_stop_lingering(tcp_t *tcp) 3847 { 3848 clock_t delta = 0; 3849 3850 tcp->tcp_linger_tid = 0; 3851 if (tcp->tcp_state > TCPS_LISTEN) { 3852 tcp_acceptor_hash_remove(tcp); 3853 if (tcp->tcp_flow_stopped) { 3854 tcp_clrqfull(tcp); 3855 } 3856 3857 if (tcp->tcp_timer_tid != 0) { 3858 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3859 tcp->tcp_timer_tid = 0; 3860 } 3861 /* 3862 * Need to cancel those timers which will not be used when 3863 * TCP is detached. This has to be done before the tcp_wq 3864 * is set to the global queue. 3865 */ 3866 tcp_timers_stop(tcp); 3867 3868 3869 tcp->tcp_detached = B_TRUE; 3870 tcp->tcp_rq = tcp_g_q; 3871 tcp->tcp_wq = WR(tcp_g_q); 3872 3873 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3874 tcp_time_wait_append(tcp); 3875 TCP_DBGSTAT(tcp_detach_time_wait); 3876 goto finish; 3877 } 3878 3879 /* 3880 * If delta is zero the timer event wasn't executed and was 3881 * successfully canceled. In this case we need to restart it 3882 * with the minimal delta possible. 3883 */ 3884 if (delta >= 0) { 3885 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3886 delta ? delta : 1); 3887 } 3888 } else { 3889 tcp_closei_local(tcp); 3890 CONN_DEC_REF(tcp->tcp_connp); 3891 } 3892 finish: 3893 /* Signal closing thread that it can complete close */ 3894 mutex_enter(&tcp->tcp_closelock); 3895 tcp->tcp_detached = B_TRUE; 3896 tcp->tcp_rq = tcp_g_q; 3897 tcp->tcp_wq = WR(tcp_g_q); 3898 tcp->tcp_closed = 1; 3899 cv_signal(&tcp->tcp_closecv); 3900 mutex_exit(&tcp->tcp_closelock); 3901 } 3902 3903 /* 3904 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3905 * expires. 3906 */ 3907 static void 3908 tcp_close_linger_timeout(void *arg) 3909 { 3910 conn_t *connp = (conn_t *)arg; 3911 tcp_t *tcp = connp->conn_tcp; 3912 3913 tcp->tcp_client_errno = ETIMEDOUT; 3914 tcp_stop_lingering(tcp); 3915 } 3916 3917 static int 3918 tcp_close(queue_t *q, int flags) 3919 { 3920 conn_t *connp = Q_TO_CONN(q); 3921 tcp_t *tcp = connp->conn_tcp; 3922 mblk_t *mp = &tcp->tcp_closemp; 3923 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3924 3925 ASSERT(WR(q)->q_next == NULL); 3926 ASSERT(connp->conn_ref >= 2); 3927 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 3928 3929 /* 3930 * We are being closed as /dev/tcp or /dev/tcp6. 3931 * 3932 * Mark the conn as closing. ill_pending_mp_add will not 3933 * add any mp to the pending mp list, after this conn has 3934 * started closing. Same for sq_pending_mp_add 3935 */ 3936 mutex_enter(&connp->conn_lock); 3937 connp->conn_state_flags |= CONN_CLOSING; 3938 if (connp->conn_oper_pending_ill != NULL) 3939 conn_ioctl_cleanup_reqd = B_TRUE; 3940 CONN_INC_REF_LOCKED(connp); 3941 mutex_exit(&connp->conn_lock); 3942 tcp->tcp_closeflags = (uint8_t)flags; 3943 ASSERT(connp->conn_ref >= 3); 3944 3945 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 3946 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 3947 3948 mutex_enter(&tcp->tcp_closelock); 3949 3950 while (!tcp->tcp_closed) 3951 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3952 mutex_exit(&tcp->tcp_closelock); 3953 /* 3954 * In the case of listener streams that have eagers in the q or q0 3955 * we wait for the eagers to drop their reference to us. tcp_rq and 3956 * tcp_wq of the eagers point to our queues. By waiting for the 3957 * refcnt to drop to 1, we are sure that the eagers have cleaned 3958 * up their queue pointers and also dropped their references to us. 3959 */ 3960 if (tcp->tcp_wait_for_eagers) { 3961 mutex_enter(&connp->conn_lock); 3962 while (connp->conn_ref != 1) { 3963 cv_wait(&connp->conn_cv, &connp->conn_lock); 3964 } 3965 mutex_exit(&connp->conn_lock); 3966 } 3967 /* 3968 * ioctl cleanup. The mp is queued in the 3969 * ill_pending_mp or in the sq_pending_mp. 3970 */ 3971 if (conn_ioctl_cleanup_reqd) 3972 conn_ioctl_cleanup(connp); 3973 3974 qprocsoff(q); 3975 inet_minor_free(ip_minor_arena, connp->conn_dev); 3976 3977 tcp->tcp_cpid = -1; 3978 3979 /* 3980 * Drop IP's reference on the conn. This is the last reference 3981 * on the connp if the state was less than established. If the 3982 * connection has gone into timewait state, then we will have 3983 * one ref for the TCP and one more ref (total of two) for the 3984 * classifier connected hash list (a timewait connections stays 3985 * in connected hash till closed). 3986 * 3987 * We can't assert the references because there might be other 3988 * transient reference places because of some walkers or queued 3989 * packets in squeue for the timewait state. 3990 */ 3991 CONN_DEC_REF(connp); 3992 q->q_ptr = WR(q)->q_ptr = NULL; 3993 return (0); 3994 } 3995 3996 static int 3997 tcpclose_accept(queue_t *q) 3998 { 3999 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 4000 4001 /* 4002 * We had opened an acceptor STREAM for sockfs which is 4003 * now being closed due to some error. 4004 */ 4005 qprocsoff(q); 4006 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 4007 q->q_ptr = WR(q)->q_ptr = NULL; 4008 return (0); 4009 } 4010 4011 4012 /* 4013 * Called by streams close routine via squeues when our client blows off her 4014 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 4015 * connection politely" When SO_LINGER is set (with a non-zero linger time and 4016 * it is not a nonblocking socket) then this routine sleeps until the FIN is 4017 * acked. 4018 * 4019 * NOTE: tcp_close potentially returns error when lingering. 4020 * However, the stream head currently does not pass these errors 4021 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 4022 * errors to the application (from tsleep()) and not errors 4023 * like ECONNRESET caused by receiving a reset packet. 4024 */ 4025 4026 /* ARGSUSED */ 4027 static void 4028 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 4029 { 4030 char *msg; 4031 conn_t *connp = (conn_t *)arg; 4032 tcp_t *tcp = connp->conn_tcp; 4033 clock_t delta = 0; 4034 4035 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 4036 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 4037 4038 /* Cancel any pending timeout */ 4039 if (tcp->tcp_ordrelid != 0) { 4040 if (tcp->tcp_timeout) { 4041 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 4042 } 4043 tcp->tcp_ordrelid = 0; 4044 tcp->tcp_timeout = B_FALSE; 4045 } 4046 4047 mutex_enter(&tcp->tcp_eager_lock); 4048 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 4049 /* Cleanup for listener */ 4050 tcp_eager_cleanup(tcp, 0); 4051 tcp->tcp_wait_for_eagers = 1; 4052 } 4053 mutex_exit(&tcp->tcp_eager_lock); 4054 4055 connp->conn_mdt_ok = B_FALSE; 4056 tcp->tcp_mdt = B_FALSE; 4057 4058 msg = NULL; 4059 switch (tcp->tcp_state) { 4060 case TCPS_CLOSED: 4061 case TCPS_IDLE: 4062 case TCPS_BOUND: 4063 case TCPS_LISTEN: 4064 break; 4065 case TCPS_SYN_SENT: 4066 msg = "tcp_close, during connect"; 4067 break; 4068 case TCPS_SYN_RCVD: 4069 /* 4070 * Close during the connect 3-way handshake 4071 * but here there may or may not be pending data 4072 * already on queue. Process almost same as in 4073 * the ESTABLISHED state. 4074 */ 4075 /* FALLTHRU */ 4076 default: 4077 if (tcp->tcp_fused) 4078 tcp_unfuse(tcp); 4079 4080 /* 4081 * If SO_LINGER has set a zero linger time, abort the 4082 * connection with a reset. 4083 */ 4084 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 4085 msg = "tcp_close, zero lingertime"; 4086 break; 4087 } 4088 4089 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 4090 /* 4091 * Abort connection if there is unread data queued. 4092 */ 4093 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 4094 msg = "tcp_close, unread data"; 4095 break; 4096 } 4097 /* 4098 * tcp_hard_bound is now cleared thus all packets go through 4099 * tcp_lookup. This fact is used by tcp_detach below. 4100 * 4101 * We have done a qwait() above which could have possibly 4102 * drained more messages in turn causing transition to a 4103 * different state. Check whether we have to do the rest 4104 * of the processing or not. 4105 */ 4106 if (tcp->tcp_state <= TCPS_LISTEN) 4107 break; 4108 4109 /* 4110 * Transmit the FIN before detaching the tcp_t. 4111 * After tcp_detach returns this queue/perimeter 4112 * no longer owns the tcp_t thus others can modify it. 4113 */ 4114 (void) tcp_xmit_end(tcp); 4115 4116 /* 4117 * If lingering on close then wait until the fin is acked, 4118 * the SO_LINGER time passes, or a reset is sent/received. 4119 */ 4120 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 4121 !(tcp->tcp_fin_acked) && 4122 tcp->tcp_state >= TCPS_ESTABLISHED) { 4123 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 4124 tcp->tcp_client_errno = EWOULDBLOCK; 4125 } else if (tcp->tcp_client_errno == 0) { 4126 4127 ASSERT(tcp->tcp_linger_tid == 0); 4128 4129 tcp->tcp_linger_tid = TCP_TIMER(tcp, 4130 tcp_close_linger_timeout, 4131 tcp->tcp_lingertime * hz); 4132 4133 /* tcp_close_linger_timeout will finish close */ 4134 if (tcp->tcp_linger_tid == 0) 4135 tcp->tcp_client_errno = ENOSR; 4136 else 4137 return; 4138 } 4139 4140 /* 4141 * Check if we need to detach or just close 4142 * the instance. 4143 */ 4144 if (tcp->tcp_state <= TCPS_LISTEN) 4145 break; 4146 } 4147 4148 /* 4149 * Make sure that no other thread will access the tcp_rq of 4150 * this instance (through lookups etc.) as tcp_rq will go 4151 * away shortly. 4152 */ 4153 tcp_acceptor_hash_remove(tcp); 4154 4155 if (tcp->tcp_flow_stopped) { 4156 tcp_clrqfull(tcp); 4157 } 4158 4159 if (tcp->tcp_timer_tid != 0) { 4160 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4161 tcp->tcp_timer_tid = 0; 4162 } 4163 /* 4164 * Need to cancel those timers which will not be used when 4165 * TCP is detached. This has to be done before the tcp_wq 4166 * is set to the global queue. 4167 */ 4168 tcp_timers_stop(tcp); 4169 4170 tcp->tcp_detached = B_TRUE; 4171 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4172 tcp_time_wait_append(tcp); 4173 TCP_DBGSTAT(tcp_detach_time_wait); 4174 ASSERT(connp->conn_ref >= 3); 4175 goto finish; 4176 } 4177 4178 /* 4179 * If delta is zero the timer event wasn't executed and was 4180 * successfully canceled. In this case we need to restart it 4181 * with the minimal delta possible. 4182 */ 4183 if (delta >= 0) 4184 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4185 delta ? delta : 1); 4186 4187 ASSERT(connp->conn_ref >= 3); 4188 goto finish; 4189 } 4190 4191 /* Detach did not complete. Still need to remove q from stream. */ 4192 if (msg) { 4193 if (tcp->tcp_state == TCPS_ESTABLISHED || 4194 tcp->tcp_state == TCPS_CLOSE_WAIT) 4195 BUMP_MIB(&tcp_mib, tcpEstabResets); 4196 if (tcp->tcp_state == TCPS_SYN_SENT || 4197 tcp->tcp_state == TCPS_SYN_RCVD) 4198 BUMP_MIB(&tcp_mib, tcpAttemptFails); 4199 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4200 } 4201 4202 tcp_closei_local(tcp); 4203 CONN_DEC_REF(connp); 4204 ASSERT(connp->conn_ref >= 2); 4205 4206 finish: 4207 /* 4208 * Although packets are always processed on the correct 4209 * tcp's perimeter and access is serialized via squeue's, 4210 * IP still needs a queue when sending packets in time_wait 4211 * state so use WR(tcp_g_q) till ip_output() can be 4212 * changed to deal with just connp. For read side, we 4213 * could have set tcp_rq to NULL but there are some cases 4214 * in tcp_rput_data() from early days of this code which 4215 * do a putnext without checking if tcp is closed. Those 4216 * need to be identified before both tcp_rq and tcp_wq 4217 * can be set to NULL and tcp_q_q can disappear forever. 4218 */ 4219 mutex_enter(&tcp->tcp_closelock); 4220 /* 4221 * Don't change the queues in the case of a listener that has 4222 * eagers in its q or q0. It could surprise the eagers. 4223 * Instead wait for the eagers outside the squeue. 4224 */ 4225 if (!tcp->tcp_wait_for_eagers) { 4226 tcp->tcp_detached = B_TRUE; 4227 tcp->tcp_rq = tcp_g_q; 4228 tcp->tcp_wq = WR(tcp_g_q); 4229 } 4230 4231 /* Signal tcp_close() to finish closing. */ 4232 tcp->tcp_closed = 1; 4233 cv_signal(&tcp->tcp_closecv); 4234 mutex_exit(&tcp->tcp_closelock); 4235 } 4236 4237 4238 /* 4239 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4240 * Some stream heads get upset if they see these later on as anything but NULL. 4241 */ 4242 static void 4243 tcp_close_mpp(mblk_t **mpp) 4244 { 4245 mblk_t *mp; 4246 4247 if ((mp = *mpp) != NULL) { 4248 do { 4249 mp->b_next = NULL; 4250 mp->b_prev = NULL; 4251 } while ((mp = mp->b_cont) != NULL); 4252 4253 mp = *mpp; 4254 *mpp = NULL; 4255 freemsg(mp); 4256 } 4257 } 4258 4259 /* Do detached close. */ 4260 static void 4261 tcp_close_detached(tcp_t *tcp) 4262 { 4263 if (tcp->tcp_fused) 4264 tcp_unfuse(tcp); 4265 4266 /* 4267 * Clustering code serializes TCP disconnect callbacks and 4268 * cluster tcp list walks by blocking a TCP disconnect callback 4269 * if a cluster tcp list walk is in progress. This ensures 4270 * accurate accounting of TCPs in the cluster code even though 4271 * the TCP list walk itself is not atomic. 4272 */ 4273 tcp_closei_local(tcp); 4274 CONN_DEC_REF(tcp->tcp_connp); 4275 } 4276 4277 /* 4278 * Stop all TCP timers, and free the timer mblks if requested. 4279 */ 4280 void 4281 tcp_timers_stop(tcp_t *tcp) 4282 { 4283 if (tcp->tcp_timer_tid != 0) { 4284 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4285 tcp->tcp_timer_tid = 0; 4286 } 4287 if (tcp->tcp_ka_tid != 0) { 4288 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4289 tcp->tcp_ka_tid = 0; 4290 } 4291 if (tcp->tcp_ack_tid != 0) { 4292 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4293 tcp->tcp_ack_tid = 0; 4294 } 4295 if (tcp->tcp_push_tid != 0) { 4296 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4297 tcp->tcp_push_tid = 0; 4298 } 4299 } 4300 4301 /* 4302 * The tcp_t is going away. Remove it from all lists and set it 4303 * to TCPS_CLOSED. The freeing up of memory is deferred until 4304 * tcp_inactive. This is needed since a thread in tcp_rput might have 4305 * done a CONN_INC_REF on this structure before it was removed from the 4306 * hashes. 4307 */ 4308 static void 4309 tcp_closei_local(tcp_t *tcp) 4310 { 4311 ire_t *ire; 4312 conn_t *connp = tcp->tcp_connp; 4313 4314 if (!TCP_IS_SOCKET(tcp)) 4315 tcp_acceptor_hash_remove(tcp); 4316 4317 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 4318 tcp->tcp_ibsegs = 0; 4319 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 4320 tcp->tcp_obsegs = 0; 4321 4322 /* 4323 * If we are an eager connection hanging off a listener that 4324 * hasn't formally accepted the connection yet, get off his 4325 * list and blow off any data that we have accumulated. 4326 */ 4327 if (tcp->tcp_listener != NULL) { 4328 tcp_t *listener = tcp->tcp_listener; 4329 mutex_enter(&listener->tcp_eager_lock); 4330 /* 4331 * tcp_eager_conn_ind == NULL means that the 4332 * conn_ind has already gone to listener. At 4333 * this point, eager will be closed but we 4334 * leave it in listeners eager list so that 4335 * if listener decides to close without doing 4336 * accept, we can clean this up. In tcp_wput_accept 4337 * we take case of the case of accept on closed 4338 * eager. 4339 */ 4340 if (tcp->tcp_conn.tcp_eager_conn_ind != NULL) { 4341 tcp_eager_unlink(tcp); 4342 mutex_exit(&listener->tcp_eager_lock); 4343 /* 4344 * We don't want to have any pointers to the 4345 * listener queue, after we have released our 4346 * reference on the listener 4347 */ 4348 tcp->tcp_rq = tcp_g_q; 4349 tcp->tcp_wq = WR(tcp_g_q); 4350 CONN_DEC_REF(listener->tcp_connp); 4351 } else { 4352 mutex_exit(&listener->tcp_eager_lock); 4353 } 4354 } 4355 4356 /* Stop all the timers */ 4357 tcp_timers_stop(tcp); 4358 4359 if (tcp->tcp_state == TCPS_LISTEN) { 4360 if (tcp->tcp_ip_addr_cache) { 4361 kmem_free((void *)tcp->tcp_ip_addr_cache, 4362 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4363 tcp->tcp_ip_addr_cache = NULL; 4364 } 4365 } 4366 if (tcp->tcp_flow_stopped) 4367 tcp_clrqfull(tcp); 4368 4369 tcp_bind_hash_remove(tcp); 4370 /* 4371 * If the tcp_time_wait_collector (which runs outside the squeue) 4372 * is trying to remove this tcp from the time wait list, we will 4373 * block in tcp_time_wait_remove while trying to acquire the 4374 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4375 * requires the ipcl_hash_remove to be ordered after the 4376 * tcp_time_wait_remove for the refcnt checks to work correctly. 4377 */ 4378 if (tcp->tcp_state == TCPS_TIME_WAIT) 4379 tcp_time_wait_remove(tcp, NULL); 4380 CL_INET_DISCONNECT(tcp); 4381 ipcl_hash_remove(connp); 4382 4383 /* 4384 * Delete the cached ire in conn_ire_cache and also mark 4385 * the conn as CONDEMNED 4386 */ 4387 mutex_enter(&connp->conn_lock); 4388 connp->conn_state_flags |= CONN_CONDEMNED; 4389 ire = connp->conn_ire_cache; 4390 connp->conn_ire_cache = NULL; 4391 mutex_exit(&connp->conn_lock); 4392 if (ire != NULL) 4393 IRE_REFRELE_NOTR(ire); 4394 4395 /* Need to cleanup any pending ioctls */ 4396 ASSERT(tcp->tcp_time_wait_next == NULL); 4397 ASSERT(tcp->tcp_time_wait_prev == NULL); 4398 ASSERT(tcp->tcp_time_wait_expire == 0); 4399 tcp->tcp_state = TCPS_CLOSED; 4400 4401 /* Release any SSL context */ 4402 if (tcp->tcp_kssl_ent != NULL) { 4403 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4404 tcp->tcp_kssl_ent = NULL; 4405 } 4406 if (tcp->tcp_kssl_ctx != NULL) { 4407 kssl_release_ctx(tcp->tcp_kssl_ctx); 4408 tcp->tcp_kssl_ctx = NULL; 4409 } 4410 tcp->tcp_kssl_pending = B_FALSE; 4411 } 4412 4413 /* 4414 * tcp is dying (called from ipcl_conn_destroy and error cases). 4415 * Free the tcp_t in either case. 4416 */ 4417 void 4418 tcp_free(tcp_t *tcp) 4419 { 4420 mblk_t *mp; 4421 ip6_pkt_t *ipp; 4422 4423 ASSERT(tcp != NULL); 4424 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4425 4426 tcp->tcp_rq = NULL; 4427 tcp->tcp_wq = NULL; 4428 4429 tcp_close_mpp(&tcp->tcp_xmit_head); 4430 tcp_close_mpp(&tcp->tcp_reass_head); 4431 if (tcp->tcp_rcv_list != NULL) { 4432 /* Free b_next chain */ 4433 tcp_close_mpp(&tcp->tcp_rcv_list); 4434 } 4435 if ((mp = tcp->tcp_urp_mp) != NULL) { 4436 freemsg(mp); 4437 } 4438 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4439 freemsg(mp); 4440 } 4441 4442 if (tcp->tcp_fused_sigurg_mp != NULL) { 4443 freeb(tcp->tcp_fused_sigurg_mp); 4444 tcp->tcp_fused_sigurg_mp = NULL; 4445 } 4446 4447 if (tcp->tcp_sack_info != NULL) { 4448 if (tcp->tcp_notsack_list != NULL) { 4449 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4450 } 4451 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4452 } 4453 4454 if (tcp->tcp_hopopts != NULL) { 4455 mi_free(tcp->tcp_hopopts); 4456 tcp->tcp_hopopts = NULL; 4457 tcp->tcp_hopoptslen = 0; 4458 } 4459 ASSERT(tcp->tcp_hopoptslen == 0); 4460 if (tcp->tcp_dstopts != NULL) { 4461 mi_free(tcp->tcp_dstopts); 4462 tcp->tcp_dstopts = NULL; 4463 tcp->tcp_dstoptslen = 0; 4464 } 4465 ASSERT(tcp->tcp_dstoptslen == 0); 4466 if (tcp->tcp_rtdstopts != NULL) { 4467 mi_free(tcp->tcp_rtdstopts); 4468 tcp->tcp_rtdstopts = NULL; 4469 tcp->tcp_rtdstoptslen = 0; 4470 } 4471 ASSERT(tcp->tcp_rtdstoptslen == 0); 4472 if (tcp->tcp_rthdr != NULL) { 4473 mi_free(tcp->tcp_rthdr); 4474 tcp->tcp_rthdr = NULL; 4475 tcp->tcp_rthdrlen = 0; 4476 } 4477 ASSERT(tcp->tcp_rthdrlen == 0); 4478 4479 ipp = &tcp->tcp_sticky_ipp; 4480 if (ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | IPPF_DSTOPTS | 4481 IPPF_RTHDR)) 4482 ip6_pkt_free(ipp); 4483 4484 /* 4485 * Free memory associated with the tcp/ip header template. 4486 */ 4487 4488 if (tcp->tcp_iphc != NULL) 4489 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4490 4491 /* 4492 * Following is really a blowing away a union. 4493 * It happens to have exactly two members of identical size 4494 * the following code is enough. 4495 */ 4496 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4497 4498 if (tcp->tcp_tracebuf != NULL) { 4499 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4500 tcp->tcp_tracebuf = NULL; 4501 } 4502 } 4503 4504 4505 /* 4506 * Put a connection confirmation message upstream built from the 4507 * address information within 'iph' and 'tcph'. Report our success or failure. 4508 */ 4509 static boolean_t 4510 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4511 mblk_t **defermp) 4512 { 4513 sin_t sin; 4514 sin6_t sin6; 4515 mblk_t *mp; 4516 char *optp = NULL; 4517 int optlen = 0; 4518 cred_t *cr; 4519 4520 if (defermp != NULL) 4521 *defermp = NULL; 4522 4523 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4524 /* 4525 * Return in T_CONN_CON results of option negotiation through 4526 * the T_CONN_REQ. Note: If there is an real end-to-end option 4527 * negotiation, then what is received from remote end needs 4528 * to be taken into account but there is no such thing (yet?) 4529 * in our TCP/IP. 4530 * Note: We do not use mi_offset_param() here as 4531 * tcp_opts_conn_req contents do not directly come from 4532 * an application and are either generated in kernel or 4533 * from user input that was already verified. 4534 */ 4535 mp = tcp->tcp_conn.tcp_opts_conn_req; 4536 optp = (char *)(mp->b_rptr + 4537 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4538 optlen = (int) 4539 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4540 } 4541 4542 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4543 ipha_t *ipha = (ipha_t *)iphdr; 4544 4545 /* packet is IPv4 */ 4546 if (tcp->tcp_family == AF_INET) { 4547 sin = sin_null; 4548 sin.sin_addr.s_addr = ipha->ipha_src; 4549 sin.sin_port = *(uint16_t *)tcph->th_lport; 4550 sin.sin_family = AF_INET; 4551 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4552 (int)sizeof (sin_t), optp, optlen); 4553 } else { 4554 sin6 = sin6_null; 4555 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4556 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4557 sin6.sin6_family = AF_INET6; 4558 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4559 (int)sizeof (sin6_t), optp, optlen); 4560 4561 } 4562 } else { 4563 ip6_t *ip6h = (ip6_t *)iphdr; 4564 4565 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4566 ASSERT(tcp->tcp_family == AF_INET6); 4567 sin6 = sin6_null; 4568 sin6.sin6_addr = ip6h->ip6_src; 4569 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4570 sin6.sin6_family = AF_INET6; 4571 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4572 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4573 (int)sizeof (sin6_t), optp, optlen); 4574 } 4575 4576 if (!mp) 4577 return (B_FALSE); 4578 4579 if ((cr = DB_CRED(idmp)) != NULL) { 4580 mblk_setcred(mp, cr); 4581 DB_CPID(mp) = DB_CPID(idmp); 4582 } 4583 4584 if (defermp == NULL) 4585 putnext(tcp->tcp_rq, mp); 4586 else 4587 *defermp = mp; 4588 4589 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4590 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4591 return (B_TRUE); 4592 } 4593 4594 /* 4595 * Defense for the SYN attack - 4596 * 1. When q0 is full, drop from the tail (tcp_eager_prev_q0) the oldest 4597 * one that doesn't have the dontdrop bit set. 4598 * 2. Don't drop a SYN request before its first timeout. This gives every 4599 * request at least til the first timeout to complete its 3-way handshake. 4600 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4601 * requests currently on the queue that has timed out. This will be used 4602 * as an indicator of whether an attack is under way, so that appropriate 4603 * actions can be taken. (It's incremented in tcp_timer() and decremented 4604 * either when eager goes into ESTABLISHED, or gets freed up.) 4605 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4606 * # of timeout drops back to <= q0len/32 => SYN alert off 4607 */ 4608 static boolean_t 4609 tcp_drop_q0(tcp_t *tcp) 4610 { 4611 tcp_t *eager; 4612 4613 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4614 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4615 /* 4616 * New one is added after next_q0 so prev_q0 points to the oldest 4617 * Also do not drop any established connections that are deferred on 4618 * q0 due to q being full 4619 */ 4620 4621 eager = tcp->tcp_eager_prev_q0; 4622 while (eager->tcp_dontdrop || eager->tcp_conn_def_q0) { 4623 eager = eager->tcp_eager_prev_q0; 4624 if (eager == tcp) { 4625 eager = tcp->tcp_eager_prev_q0; 4626 break; 4627 } 4628 } 4629 if (eager->tcp_syn_rcvd_timeout == 0) 4630 return (B_FALSE); 4631 4632 if (tcp->tcp_debug) { 4633 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4634 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4635 " (%d pending) on %s, drop one", tcp_conn_req_max_q0, 4636 tcp->tcp_conn_req_cnt_q0, 4637 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4638 } 4639 4640 BUMP_MIB(&tcp_mib, tcpHalfOpenDrop); 4641 4642 /* 4643 * need to do refhold here because the selected eager could 4644 * be removed by someone else if we release the eager lock. 4645 */ 4646 CONN_INC_REF(eager->tcp_connp); 4647 mutex_exit(&tcp->tcp_eager_lock); 4648 4649 /* Mark the IRE created for this SYN request temporary */ 4650 tcp_ip_ire_mark_advice(eager); 4651 (void) tcp_clean_death(eager, ETIMEDOUT, 5); 4652 CONN_DEC_REF(eager->tcp_connp); 4653 4654 mutex_enter(&tcp->tcp_eager_lock); 4655 return (B_TRUE); 4656 } 4657 4658 int 4659 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4660 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4661 { 4662 tcp_t *ltcp = lconnp->conn_tcp; 4663 tcp_t *tcp = connp->conn_tcp; 4664 mblk_t *tpi_mp; 4665 ipha_t *ipha; 4666 ip6_t *ip6h; 4667 sin6_t sin6; 4668 in6_addr_t v6dst; 4669 int err; 4670 int ifindex = 0; 4671 cred_t *cr; 4672 4673 if (ipvers == IPV4_VERSION) { 4674 ipha = (ipha_t *)mp->b_rptr; 4675 4676 connp->conn_send = ip_output; 4677 connp->conn_recv = tcp_input; 4678 4679 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4680 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4681 4682 sin6 = sin6_null; 4683 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4684 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4685 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4686 sin6.sin6_family = AF_INET6; 4687 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4688 lconnp->conn_zoneid); 4689 if (tcp->tcp_recvdstaddr) { 4690 sin6_t sin6d; 4691 4692 sin6d = sin6_null; 4693 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4694 &sin6d.sin6_addr); 4695 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4696 sin6d.sin6_family = AF_INET; 4697 tpi_mp = mi_tpi_extconn_ind(NULL, 4698 (char *)&sin6d, sizeof (sin6_t), 4699 (char *)&tcp, 4700 (t_scalar_t)sizeof (intptr_t), 4701 (char *)&sin6d, sizeof (sin6_t), 4702 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4703 } else { 4704 tpi_mp = mi_tpi_conn_ind(NULL, 4705 (char *)&sin6, sizeof (sin6_t), 4706 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4707 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4708 } 4709 } else { 4710 ip6h = (ip6_t *)mp->b_rptr; 4711 4712 connp->conn_send = ip_output_v6; 4713 connp->conn_recv = tcp_input; 4714 4715 connp->conn_srcv6 = ip6h->ip6_dst; 4716 connp->conn_remv6 = ip6h->ip6_src; 4717 4718 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4719 ifindex = (int)DB_CKSUMSTUFF(mp); 4720 DB_CKSUMSTUFF(mp) = 0; 4721 4722 sin6 = sin6_null; 4723 sin6.sin6_addr = ip6h->ip6_src; 4724 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4725 sin6.sin6_family = AF_INET6; 4726 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4727 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4728 lconnp->conn_zoneid); 4729 4730 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4731 /* Pass up the scope_id of remote addr */ 4732 sin6.sin6_scope_id = ifindex; 4733 } else { 4734 sin6.sin6_scope_id = 0; 4735 } 4736 if (tcp->tcp_recvdstaddr) { 4737 sin6_t sin6d; 4738 4739 sin6d = sin6_null; 4740 sin6.sin6_addr = ip6h->ip6_dst; 4741 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4742 sin6d.sin6_family = AF_INET; 4743 tpi_mp = mi_tpi_extconn_ind(NULL, 4744 (char *)&sin6d, sizeof (sin6_t), 4745 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4746 (char *)&sin6d, sizeof (sin6_t), 4747 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4748 } else { 4749 tpi_mp = mi_tpi_conn_ind(NULL, 4750 (char *)&sin6, sizeof (sin6_t), 4751 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4752 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4753 } 4754 } 4755 4756 if (tpi_mp == NULL) 4757 return (ENOMEM); 4758 4759 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4760 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4761 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4762 connp->conn_fully_bound = B_FALSE; 4763 4764 if (tcp_trace) 4765 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4766 4767 /* Inherit information from the "parent" */ 4768 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4769 tcp->tcp_family = ltcp->tcp_family; 4770 tcp->tcp_wq = ltcp->tcp_wq; 4771 tcp->tcp_rq = ltcp->tcp_rq; 4772 tcp->tcp_mss = tcp_mss_def_ipv6; 4773 tcp->tcp_detached = B_TRUE; 4774 if ((err = tcp_init_values(tcp)) != 0) { 4775 freemsg(tpi_mp); 4776 return (err); 4777 } 4778 4779 if (ipvers == IPV4_VERSION) { 4780 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4781 freemsg(tpi_mp); 4782 return (err); 4783 } 4784 ASSERT(tcp->tcp_ipha != NULL); 4785 } else { 4786 /* ifindex must be already set */ 4787 ASSERT(ifindex != 0); 4788 4789 if (ltcp->tcp_bound_if != 0) { 4790 /* 4791 * Set newtcp's bound_if equal to 4792 * listener's value. If ifindex is 4793 * not the same as ltcp->tcp_bound_if, 4794 * it must be a packet for the ipmp group 4795 * of interfaces 4796 */ 4797 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4798 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4799 tcp->tcp_bound_if = ifindex; 4800 } 4801 4802 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4803 tcp->tcp_recvifindex = 0; 4804 tcp->tcp_recvhops = 0xffffffffU; 4805 ASSERT(tcp->tcp_ip6h != NULL); 4806 } 4807 4808 tcp->tcp_lport = ltcp->tcp_lport; 4809 4810 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4811 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4812 /* 4813 * Listener had options of some sort; eager inherits. 4814 * Free up the eager template and allocate one 4815 * of the right size. 4816 */ 4817 if (tcp->tcp_hdr_grown) { 4818 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4819 } else { 4820 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4821 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4822 } 4823 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4824 KM_NOSLEEP); 4825 if (tcp->tcp_iphc == NULL) { 4826 tcp->tcp_iphc_len = 0; 4827 freemsg(tpi_mp); 4828 return (ENOMEM); 4829 } 4830 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4831 tcp->tcp_hdr_grown = B_TRUE; 4832 } 4833 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4834 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4835 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4836 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4837 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4838 4839 /* 4840 * Copy the IP+TCP header template from listener to eager 4841 */ 4842 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4843 if (tcp->tcp_ipversion == IPV6_VERSION) { 4844 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4845 IPPROTO_RAW) { 4846 tcp->tcp_ip6h = 4847 (ip6_t *)(tcp->tcp_iphc + 4848 sizeof (ip6i_t)); 4849 } else { 4850 tcp->tcp_ip6h = 4851 (ip6_t *)(tcp->tcp_iphc); 4852 } 4853 tcp->tcp_ipha = NULL; 4854 } else { 4855 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4856 tcp->tcp_ip6h = NULL; 4857 } 4858 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4859 tcp->tcp_ip_hdr_len); 4860 } else { 4861 /* 4862 * only valid case when ipversion of listener and 4863 * eager differ is when listener is IPv6 and 4864 * eager is IPv4. 4865 * Eager header template has been initialized to the 4866 * maximum v4 header sizes, which includes space for 4867 * TCP and IP options. 4868 */ 4869 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4870 (tcp->tcp_ipversion == IPV4_VERSION)); 4871 ASSERT(tcp->tcp_iphc_len >= 4872 TCP_MAX_COMBINED_HEADER_LENGTH); 4873 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4874 /* copy IP header fields individually */ 4875 tcp->tcp_ipha->ipha_ttl = 4876 ltcp->tcp_ip6h->ip6_hops; 4877 bcopy(ltcp->tcp_tcph->th_lport, 4878 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4879 } 4880 4881 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4882 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4883 sizeof (in_port_t)); 4884 4885 if (ltcp->tcp_lport == 0) { 4886 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4887 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4888 sizeof (in_port_t)); 4889 } 4890 4891 if (tcp->tcp_ipversion == IPV4_VERSION) { 4892 ASSERT(ipha != NULL); 4893 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4894 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4895 4896 /* Source routing option copyover (reverse it) */ 4897 if (tcp_rev_src_routes) 4898 tcp_opt_reverse(tcp, ipha); 4899 } else { 4900 ASSERT(ip6h != NULL); 4901 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4902 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4903 } 4904 4905 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4906 /* 4907 * If the SYN contains a credential, it's a loopback packet; attach 4908 * the credential to the TPI message. 4909 */ 4910 if ((cr = DB_CRED(idmp)) != NULL) { 4911 mblk_setcred(tpi_mp, cr); 4912 DB_CPID(tpi_mp) = DB_CPID(idmp); 4913 } 4914 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4915 4916 /* Inherit the listener's SSL protection state */ 4917 4918 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4919 kssl_hold_ent(tcp->tcp_kssl_ent); 4920 tcp->tcp_kssl_pending = B_TRUE; 4921 } 4922 4923 return (0); 4924 } 4925 4926 4927 int 4928 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4929 tcph_t *tcph, mblk_t *idmp) 4930 { 4931 tcp_t *ltcp = lconnp->conn_tcp; 4932 tcp_t *tcp = connp->conn_tcp; 4933 sin_t sin; 4934 mblk_t *tpi_mp = NULL; 4935 int err; 4936 cred_t *cr; 4937 4938 sin = sin_null; 4939 sin.sin_addr.s_addr = ipha->ipha_src; 4940 sin.sin_port = *(uint16_t *)tcph->th_lport; 4941 sin.sin_family = AF_INET; 4942 if (ltcp->tcp_recvdstaddr) { 4943 sin_t sind; 4944 4945 sind = sin_null; 4946 sind.sin_addr.s_addr = ipha->ipha_dst; 4947 sind.sin_port = *(uint16_t *)tcph->th_fport; 4948 sind.sin_family = AF_INET; 4949 tpi_mp = mi_tpi_extconn_ind(NULL, 4950 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4951 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4952 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4953 } else { 4954 tpi_mp = mi_tpi_conn_ind(NULL, 4955 (char *)&sin, sizeof (sin_t), 4956 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4957 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4958 } 4959 4960 if (tpi_mp == NULL) { 4961 return (ENOMEM); 4962 } 4963 4964 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4965 connp->conn_send = ip_output; 4966 connp->conn_recv = tcp_input; 4967 connp->conn_fully_bound = B_FALSE; 4968 4969 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4970 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4971 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4972 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4973 4974 if (tcp_trace) { 4975 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4976 } 4977 4978 /* Inherit information from the "parent" */ 4979 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4980 tcp->tcp_family = ltcp->tcp_family; 4981 tcp->tcp_wq = ltcp->tcp_wq; 4982 tcp->tcp_rq = ltcp->tcp_rq; 4983 tcp->tcp_mss = tcp_mss_def_ipv4; 4984 tcp->tcp_detached = B_TRUE; 4985 if ((err = tcp_init_values(tcp)) != 0) { 4986 freemsg(tpi_mp); 4987 return (err); 4988 } 4989 4990 /* 4991 * Let's make sure that eager tcp template has enough space to 4992 * copy IPv4 listener's tcp template. Since the conn_t structure is 4993 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4994 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4995 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4996 * extension headers or with ip6i_t struct). Note that bcopy() below 4997 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4998 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4999 */ 5000 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 5001 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 5002 5003 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 5004 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 5005 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 5006 tcp->tcp_ttl = ltcp->tcp_ttl; 5007 tcp->tcp_tos = ltcp->tcp_tos; 5008 5009 /* Copy the IP+TCP header template from listener to eager */ 5010 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 5011 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 5012 tcp->tcp_ip6h = NULL; 5013 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 5014 tcp->tcp_ip_hdr_len); 5015 5016 /* Initialize the IP addresses and Ports */ 5017 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 5018 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 5019 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 5020 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 5021 5022 /* Source routing option copyover (reverse it) */ 5023 if (tcp_rev_src_routes) 5024 tcp_opt_reverse(tcp, ipha); 5025 5026 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 5027 5028 /* 5029 * If the SYN contains a credential, it's a loopback packet; attach 5030 * the credential to the TPI message. 5031 */ 5032 if ((cr = DB_CRED(idmp)) != NULL) { 5033 mblk_setcred(tpi_mp, cr); 5034 DB_CPID(tpi_mp) = DB_CPID(idmp); 5035 } 5036 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 5037 5038 /* Inherit the listener's SSL protection state */ 5039 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 5040 kssl_hold_ent(tcp->tcp_kssl_ent); 5041 tcp->tcp_kssl_pending = B_TRUE; 5042 } 5043 5044 return (0); 5045 } 5046 5047 /* 5048 * sets up conn for ipsec. 5049 * if the first mblk is M_CTL it is consumed and mpp is updated. 5050 * in case of error mpp is freed. 5051 */ 5052 conn_t * 5053 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 5054 { 5055 conn_t *connp = tcp->tcp_connp; 5056 conn_t *econnp; 5057 squeue_t *new_sqp; 5058 mblk_t *first_mp = *mpp; 5059 mblk_t *mp = *mpp; 5060 boolean_t mctl_present = B_FALSE; 5061 uint_t ipvers; 5062 5063 econnp = tcp_get_conn(sqp); 5064 if (econnp == NULL) { 5065 freemsg(first_mp); 5066 return (NULL); 5067 } 5068 if (DB_TYPE(mp) == M_CTL) { 5069 if (mp->b_cont == NULL || 5070 mp->b_cont->b_datap->db_type != M_DATA) { 5071 freemsg(first_mp); 5072 return (NULL); 5073 } 5074 mp = mp->b_cont; 5075 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 5076 freemsg(first_mp); 5077 return (NULL); 5078 } 5079 5080 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5081 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5082 mctl_present = B_TRUE; 5083 } else { 5084 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 5085 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 5086 } 5087 5088 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5089 DB_CKSUMSTART(mp) = 0; 5090 5091 ASSERT(OK_32PTR(mp->b_rptr)); 5092 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5093 if (ipvers == IPV4_VERSION) { 5094 uint16_t *up; 5095 uint32_t ports; 5096 ipha_t *ipha; 5097 5098 ipha = (ipha_t *)mp->b_rptr; 5099 up = (uint16_t *)((uchar_t *)ipha + 5100 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 5101 ports = *(uint32_t *)up; 5102 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 5103 ipha->ipha_dst, ipha->ipha_src, ports); 5104 } else { 5105 uint16_t *up; 5106 uint32_t ports; 5107 uint16_t ip_hdr_len; 5108 uint8_t *nexthdrp; 5109 ip6_t *ip6h; 5110 tcph_t *tcph; 5111 5112 ip6h = (ip6_t *)mp->b_rptr; 5113 if (ip6h->ip6_nxt == IPPROTO_TCP) { 5114 ip_hdr_len = IPV6_HDR_LEN; 5115 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 5116 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 5117 CONN_DEC_REF(econnp); 5118 freemsg(first_mp); 5119 return (NULL); 5120 } 5121 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5122 up = (uint16_t *)tcph->th_lport; 5123 ports = *(uint32_t *)up; 5124 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 5125 ip6h->ip6_dst, ip6h->ip6_src, ports); 5126 } 5127 5128 /* 5129 * The caller already ensured that there is a sqp present. 5130 */ 5131 econnp->conn_sqp = new_sqp; 5132 5133 if (connp->conn_policy != NULL) { 5134 ipsec_in_t *ii; 5135 ii = (ipsec_in_t *)(first_mp->b_rptr); 5136 ASSERT(ii->ipsec_in_policy == NULL); 5137 IPPH_REFHOLD(connp->conn_policy); 5138 ii->ipsec_in_policy = connp->conn_policy; 5139 5140 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5141 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5142 CONN_DEC_REF(econnp); 5143 freemsg(first_mp); 5144 return (NULL); 5145 } 5146 } 5147 5148 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5149 CONN_DEC_REF(econnp); 5150 freemsg(first_mp); 5151 return (NULL); 5152 } 5153 5154 /* 5155 * If we know we have some policy, pass the "IPSEC" 5156 * options size TCP uses this adjust the MSS. 5157 */ 5158 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5159 if (mctl_present) { 5160 freeb(first_mp); 5161 *mpp = mp; 5162 } 5163 5164 return (econnp); 5165 } 5166 5167 /* 5168 * tcp_get_conn/tcp_free_conn 5169 * 5170 * tcp_get_conn is used to get a clean tcp connection structure. 5171 * It tries to reuse the connections put on the freelist by the 5172 * time_wait_collector failing which it goes to kmem_cache. This 5173 * way has two benefits compared to just allocating from and 5174 * freeing to kmem_cache. 5175 * 1) The time_wait_collector can free (which includes the cleanup) 5176 * outside the squeue. So when the interrupt comes, we have a clean 5177 * connection sitting in the freelist. Obviously, this buys us 5178 * performance. 5179 * 5180 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5181 * has multiple disadvantages - tying up the squeue during alloc, and the 5182 * fact that IPSec policy initialization has to happen here which 5183 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5184 * But allocating the conn/tcp in IP land is also not the best since 5185 * we can't check the 'q' and 'q0' which are protected by squeue and 5186 * blindly allocate memory which might have to be freed here if we are 5187 * not allowed to accept the connection. By using the freelist and 5188 * putting the conn/tcp back in freelist, we don't pay a penalty for 5189 * allocating memory without checking 'q/q0' and freeing it if we can't 5190 * accept the connection. 5191 * 5192 * Care should be taken to put the conn back in the same squeue's freelist 5193 * from which it was allocated. Best results are obtained if conn is 5194 * allocated from listener's squeue and freed to the same. Time wait 5195 * collector will free up the freelist is the connection ends up sitting 5196 * there for too long. 5197 */ 5198 void * 5199 tcp_get_conn(void *arg) 5200 { 5201 tcp_t *tcp = NULL; 5202 conn_t *connp = NULL; 5203 squeue_t *sqp = (squeue_t *)arg; 5204 tcp_squeue_priv_t *tcp_time_wait; 5205 5206 tcp_time_wait = 5207 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5208 5209 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5210 tcp = tcp_time_wait->tcp_free_list; 5211 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5212 if (tcp != NULL) { 5213 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5214 tcp_time_wait->tcp_free_list_cnt--; 5215 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5216 tcp->tcp_time_wait_next = NULL; 5217 connp = tcp->tcp_connp; 5218 connp->conn_flags |= IPCL_REUSED; 5219 return ((void *)connp); 5220 } 5221 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5222 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL) 5223 return (NULL); 5224 return ((void *)connp); 5225 } 5226 5227 /* 5228 * Update the cached label for the given tcp_t. This should be called once per 5229 * connection, and before any packets are sent or tcp_process_options is 5230 * invoked. Returns B_FALSE if the correct label could not be constructed. 5231 */ 5232 static boolean_t 5233 tcp_update_label(tcp_t *tcp, const cred_t *cr) 5234 { 5235 conn_t *connp = tcp->tcp_connp; 5236 5237 if (tcp->tcp_ipversion == IPV4_VERSION) { 5238 uchar_t optbuf[IP_MAX_OPT_LENGTH]; 5239 int added; 5240 5241 if (tsol_compute_label(cr, tcp->tcp_remote, optbuf, 5242 connp->conn_mac_exempt) != 0) 5243 return (B_FALSE); 5244 5245 added = tsol_remove_secopt(tcp->tcp_ipha, tcp->tcp_hdr_len); 5246 if (added == -1) 5247 return (B_FALSE); 5248 tcp->tcp_hdr_len += added; 5249 tcp->tcp_tcph = (tcph_t *)((uchar_t *)tcp->tcp_tcph + added); 5250 tcp->tcp_ip_hdr_len += added; 5251 if ((tcp->tcp_label_len = optbuf[IPOPT_OLEN]) != 0) { 5252 tcp->tcp_label_len = (tcp->tcp_label_len + 3) & ~3; 5253 added = tsol_prepend_option(optbuf, tcp->tcp_ipha, 5254 tcp->tcp_hdr_len); 5255 if (added == -1) 5256 return (B_FALSE); 5257 tcp->tcp_hdr_len += added; 5258 tcp->tcp_tcph = (tcph_t *) 5259 ((uchar_t *)tcp->tcp_tcph + added); 5260 tcp->tcp_ip_hdr_len += added; 5261 } 5262 } else { 5263 uchar_t optbuf[TSOL_MAX_IPV6_OPTION]; 5264 5265 if (tsol_compute_label_v6(cr, &tcp->tcp_remote_v6, optbuf, 5266 connp->conn_mac_exempt) != 0) 5267 return (B_FALSE); 5268 if (tsol_update_sticky(&tcp->tcp_sticky_ipp, 5269 &tcp->tcp_label_len, optbuf) != 0) 5270 return (B_FALSE); 5271 if (tcp_build_hdrs(tcp->tcp_rq, tcp) != 0) 5272 return (B_FALSE); 5273 } 5274 5275 connp->conn_ulp_labeled = 1; 5276 5277 return (B_TRUE); 5278 } 5279 5280 /* BEGIN CSTYLED */ 5281 /* 5282 * 5283 * The sockfs ACCEPT path: 5284 * ======================= 5285 * 5286 * The eager is now established in its own perimeter as soon as SYN is 5287 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5288 * completes the accept processing on the acceptor STREAM. The sending 5289 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5290 * listener but a TLI/XTI listener completes the accept processing 5291 * on the listener perimeter. 5292 * 5293 * Common control flow for 3 way handshake: 5294 * ---------------------------------------- 5295 * 5296 * incoming SYN (listener perimeter) -> tcp_rput_data() 5297 * -> tcp_conn_request() 5298 * 5299 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5300 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5301 * 5302 * Sockfs ACCEPT Path: 5303 * ------------------- 5304 * 5305 * open acceptor stream (ip_tcpopen allocates tcp_wput_accept() 5306 * as STREAM entry point) 5307 * 5308 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5309 * 5310 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5311 * association (we are not behind eager's squeue but sockfs is protecting us 5312 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5313 * is changed to point at tcp_wput(). 5314 * 5315 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5316 * listener (done on listener's perimeter). 5317 * 5318 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5319 * accept. 5320 * 5321 * TLI/XTI client ACCEPT path: 5322 * --------------------------- 5323 * 5324 * soaccept() sends T_CONN_RES on the listener STREAM. 5325 * 5326 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5327 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5328 * 5329 * Locks: 5330 * ====== 5331 * 5332 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5333 * and listeners->tcp_eager_next_q. 5334 * 5335 * Referencing: 5336 * ============ 5337 * 5338 * 1) We start out in tcp_conn_request by eager placing a ref on 5339 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5340 * 5341 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5342 * doing so we place a ref on the eager. This ref is finally dropped at the 5343 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5344 * reference is dropped by the squeue framework. 5345 * 5346 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5347 * 5348 * The reference must be released by the same entity that added the reference 5349 * In the above scheme, the eager is the entity that adds and releases the 5350 * references. Note that tcp_accept_finish executes in the squeue of the eager 5351 * (albeit after it is attached to the acceptor stream). Though 1. executes 5352 * in the listener's squeue, the eager is nascent at this point and the 5353 * reference can be considered to have been added on behalf of the eager. 5354 * 5355 * Eager getting a Reset or listener closing: 5356 * ========================================== 5357 * 5358 * Once the listener and eager are linked, the listener never does the unlink. 5359 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5360 * a message on all eager perimeter. The eager then does the unlink, clears 5361 * any pointers to the listener's queue and drops the reference to the 5362 * listener. The listener waits in tcp_close outside the squeue until its 5363 * refcount has dropped to 1. This ensures that the listener has waited for 5364 * all eagers to clear their association with the listener. 5365 * 5366 * Similarly, if eager decides to go away, it can unlink itself and close. 5367 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5368 * the reference to eager is still valid because of the extra ref we put 5369 * in tcp_send_conn_ind. 5370 * 5371 * Listener can always locate the eager under the protection 5372 * of the listener->tcp_eager_lock, and then do a refhold 5373 * on the eager during the accept processing. 5374 * 5375 * The acceptor stream accesses the eager in the accept processing 5376 * based on the ref placed on eager before sending T_conn_ind. 5377 * The only entity that can negate this refhold is a listener close 5378 * which is mutually exclusive with an active acceptor stream. 5379 * 5380 * Eager's reference on the listener 5381 * =================================== 5382 * 5383 * If the accept happens (even on a closed eager) the eager drops its 5384 * reference on the listener at the start of tcp_accept_finish. If the 5385 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5386 * the reference is dropped in tcp_closei_local. If the listener closes, 5387 * the reference is dropped in tcp_eager_kill. In all cases the reference 5388 * is dropped while executing in the eager's context (squeue). 5389 */ 5390 /* END CSTYLED */ 5391 5392 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5393 5394 /* 5395 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5396 * tcp_rput_data will not see any SYN packets. 5397 */ 5398 /* ARGSUSED */ 5399 void 5400 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5401 { 5402 tcph_t *tcph; 5403 uint32_t seg_seq; 5404 tcp_t *eager; 5405 uint_t ipvers; 5406 ipha_t *ipha; 5407 ip6_t *ip6h; 5408 int err; 5409 conn_t *econnp = NULL; 5410 squeue_t *new_sqp; 5411 mblk_t *mp1; 5412 uint_t ip_hdr_len; 5413 conn_t *connp = (conn_t *)arg; 5414 tcp_t *tcp = connp->conn_tcp; 5415 ire_t *ire; 5416 cred_t *credp; 5417 5418 if (tcp->tcp_state != TCPS_LISTEN) 5419 goto error2; 5420 5421 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5422 5423 mutex_enter(&tcp->tcp_eager_lock); 5424 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5425 mutex_exit(&tcp->tcp_eager_lock); 5426 TCP_STAT(tcp_listendrop); 5427 BUMP_MIB(&tcp_mib, tcpListenDrop); 5428 if (tcp->tcp_debug) { 5429 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5430 "tcp_conn_request: listen backlog (max=%d) " 5431 "overflow (%d pending) on %s", 5432 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5433 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5434 } 5435 goto error2; 5436 } 5437 5438 if (tcp->tcp_conn_req_cnt_q0 >= 5439 tcp->tcp_conn_req_max + tcp_conn_req_max_q0) { 5440 /* 5441 * Q0 is full. Drop a pending half-open req from the queue 5442 * to make room for the new SYN req. Also mark the time we 5443 * drop a SYN. 5444 * 5445 * A more aggressive defense against SYN attack will 5446 * be to set the "tcp_syn_defense" flag now. 5447 */ 5448 TCP_STAT(tcp_listendropq0); 5449 tcp->tcp_last_rcv_lbolt = lbolt64; 5450 if (!tcp_drop_q0(tcp)) { 5451 mutex_exit(&tcp->tcp_eager_lock); 5452 BUMP_MIB(&tcp_mib, tcpListenDropQ0); 5453 if (tcp->tcp_debug) { 5454 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5455 "tcp_conn_request: listen half-open queue " 5456 "(max=%d) full (%d pending) on %s", 5457 tcp_conn_req_max_q0, 5458 tcp->tcp_conn_req_cnt_q0, 5459 tcp_display(tcp, NULL, 5460 DISP_PORT_ONLY)); 5461 } 5462 goto error2; 5463 } 5464 } 5465 mutex_exit(&tcp->tcp_eager_lock); 5466 5467 /* 5468 * IP adds STRUIO_EAGER and ensures that the received packet is 5469 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5470 * link local address. If IPSec is enabled, db_struioflag has 5471 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5472 * otherwise an error case if neither of them is set. 5473 */ 5474 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5475 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5476 DB_CKSUMSTART(mp) = 0; 5477 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5478 econnp = (conn_t *)tcp_get_conn(arg2); 5479 if (econnp == NULL) 5480 goto error2; 5481 econnp->conn_sqp = new_sqp; 5482 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5483 /* 5484 * mp is updated in tcp_get_ipsec_conn(). 5485 */ 5486 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5487 if (econnp == NULL) { 5488 /* 5489 * mp freed by tcp_get_ipsec_conn. 5490 */ 5491 return; 5492 } 5493 } else { 5494 goto error2; 5495 } 5496 5497 ASSERT(DB_TYPE(mp) == M_DATA); 5498 5499 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5500 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5501 ASSERT(OK_32PTR(mp->b_rptr)); 5502 if (ipvers == IPV4_VERSION) { 5503 ipha = (ipha_t *)mp->b_rptr; 5504 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5505 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5506 } else { 5507 ip6h = (ip6_t *)mp->b_rptr; 5508 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5509 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5510 } 5511 5512 if (tcp->tcp_family == AF_INET) { 5513 ASSERT(ipvers == IPV4_VERSION); 5514 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5515 } else { 5516 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5517 } 5518 5519 if (err) 5520 goto error3; 5521 5522 eager = econnp->conn_tcp; 5523 5524 /* Inherit various TCP parameters from the listener */ 5525 eager->tcp_naglim = tcp->tcp_naglim; 5526 eager->tcp_first_timer_threshold = 5527 tcp->tcp_first_timer_threshold; 5528 eager->tcp_second_timer_threshold = 5529 tcp->tcp_second_timer_threshold; 5530 5531 eager->tcp_first_ctimer_threshold = 5532 tcp->tcp_first_ctimer_threshold; 5533 eager->tcp_second_ctimer_threshold = 5534 tcp->tcp_second_ctimer_threshold; 5535 5536 /* 5537 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5538 * If it does not, the eager's receive window will be set to the 5539 * listener's receive window later in this function. 5540 */ 5541 eager->tcp_rwnd = 0; 5542 5543 /* 5544 * Inherit listener's tcp_init_cwnd. Need to do this before 5545 * calling tcp_process_options() where tcp_mss_set() is called 5546 * to set the initial cwnd. 5547 */ 5548 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5549 5550 /* 5551 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5552 * zone id before the accept is completed in tcp_wput_accept(). 5553 */ 5554 econnp->conn_zoneid = connp->conn_zoneid; 5555 5556 /* Copy nexthop information from listener to eager */ 5557 if (connp->conn_nexthop_set) { 5558 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5559 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5560 } 5561 5562 /* 5563 * TSOL: tsol_input_proc() needs the eager's cred before the 5564 * eager is accepted 5565 */ 5566 econnp->conn_cred = eager->tcp_cred = credp = connp->conn_cred; 5567 crhold(credp); 5568 5569 /* 5570 * If the caller has the process-wide flag set, then default to MAC 5571 * exempt mode. This allows read-down to unlabeled hosts. 5572 */ 5573 if (getpflags(NET_MAC_AWARE, credp) != 0) 5574 econnp->conn_mac_exempt = B_TRUE; 5575 5576 if (is_system_labeled()) { 5577 cred_t *cr; 5578 5579 if (connp->conn_mlp_type != mlptSingle) { 5580 cr = econnp->conn_peercred = DB_CRED(mp); 5581 if (cr != NULL) 5582 crhold(cr); 5583 else 5584 cr = econnp->conn_cred; 5585 DTRACE_PROBE2(mlp_syn_accept, conn_t *, 5586 econnp, cred_t *, cr) 5587 } else { 5588 cr = econnp->conn_cred; 5589 DTRACE_PROBE2(syn_accept, conn_t *, 5590 econnp, cred_t *, cr) 5591 } 5592 5593 if (!tcp_update_label(eager, cr)) { 5594 DTRACE_PROBE3( 5595 tx__ip__log__error__connrequest__tcp, 5596 char *, "eager connp(1) label on SYN mp(2) failed", 5597 conn_t *, econnp, mblk_t *, mp); 5598 goto error3; 5599 } 5600 } 5601 5602 eager->tcp_hard_binding = B_TRUE; 5603 5604 tcp_bind_hash_insert(&tcp_bind_fanout[ 5605 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5606 5607 CL_INET_CONNECT(eager); 5608 5609 /* 5610 * No need to check for multicast destination since ip will only pass 5611 * up multicasts to those that have expressed interest 5612 * TODO: what about rejecting broadcasts? 5613 * Also check that source is not a multicast or broadcast address. 5614 */ 5615 eager->tcp_state = TCPS_SYN_RCVD; 5616 5617 5618 /* 5619 * There should be no ire in the mp as we are being called after 5620 * receiving the SYN. 5621 */ 5622 ASSERT(tcp_ire_mp(mp) == NULL); 5623 5624 /* 5625 * Adapt our mss, ttl, ... according to information provided in IRE. 5626 */ 5627 5628 if (tcp_adapt_ire(eager, NULL) == 0) { 5629 /* Undo the bind_hash_insert */ 5630 tcp_bind_hash_remove(eager); 5631 goto error3; 5632 } 5633 5634 /* Process all TCP options. */ 5635 tcp_process_options(eager, tcph); 5636 5637 /* Is the other end ECN capable? */ 5638 if (tcp_ecn_permitted >= 1 && 5639 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5640 eager->tcp_ecn_ok = B_TRUE; 5641 } 5642 5643 /* 5644 * listener->tcp_rq->q_hiwat should be the default window size or a 5645 * window size changed via SO_RCVBUF option. First round up the 5646 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5647 * scale option value if needed. Call tcp_rwnd_set() to finish the 5648 * setting. 5649 * 5650 * Note if there is a rpipe metric associated with the remote host, 5651 * we should not inherit receive window size from listener. 5652 */ 5653 eager->tcp_rwnd = MSS_ROUNDUP( 5654 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5655 eager->tcp_rwnd), eager->tcp_mss); 5656 if (eager->tcp_snd_ws_ok) 5657 tcp_set_ws_value(eager); 5658 /* 5659 * Note that this is the only place tcp_rwnd_set() is called for 5660 * accepting a connection. We need to call it here instead of 5661 * after the 3-way handshake because we need to tell the other 5662 * side our rwnd in the SYN-ACK segment. 5663 */ 5664 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5665 5666 /* 5667 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5668 * via soaccept()->soinheritoptions() which essentially applies 5669 * all the listener options to the new STREAM. The options that we 5670 * need to take care of are: 5671 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5672 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5673 * SO_SNDBUF, SO_RCVBUF. 5674 * 5675 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5676 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5677 * tcp_maxpsz_set() gets called later from 5678 * tcp_accept_finish(), the option takes effect. 5679 * 5680 */ 5681 /* Set the TCP options */ 5682 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5683 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5684 eager->tcp_oobinline = tcp->tcp_oobinline; 5685 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5686 eager->tcp_broadcast = tcp->tcp_broadcast; 5687 eager->tcp_useloopback = tcp->tcp_useloopback; 5688 eager->tcp_dontroute = tcp->tcp_dontroute; 5689 eager->tcp_linger = tcp->tcp_linger; 5690 eager->tcp_lingertime = tcp->tcp_lingertime; 5691 if (tcp->tcp_ka_enabled) 5692 eager->tcp_ka_enabled = 1; 5693 5694 /* Set the IP options */ 5695 econnp->conn_broadcast = connp->conn_broadcast; 5696 econnp->conn_loopback = connp->conn_loopback; 5697 econnp->conn_dontroute = connp->conn_dontroute; 5698 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5699 5700 /* Put a ref on the listener for the eager. */ 5701 CONN_INC_REF(connp); 5702 mutex_enter(&tcp->tcp_eager_lock); 5703 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5704 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5705 tcp->tcp_eager_next_q0 = eager; 5706 eager->tcp_eager_prev_q0 = tcp; 5707 5708 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5709 eager->tcp_listener = tcp; 5710 eager->tcp_saved_listener = tcp; 5711 5712 /* 5713 * Tag this detached tcp vector for later retrieval 5714 * by our listener client in tcp_accept(). 5715 */ 5716 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5717 tcp->tcp_conn_req_cnt_q0++; 5718 if (++tcp->tcp_conn_req_seqnum == -1) { 5719 /* 5720 * -1 is "special" and defined in TPI as something 5721 * that should never be used in T_CONN_IND 5722 */ 5723 ++tcp->tcp_conn_req_seqnum; 5724 } 5725 mutex_exit(&tcp->tcp_eager_lock); 5726 5727 if (tcp->tcp_syn_defense) { 5728 /* Don't drop the SYN that comes from a good IP source */ 5729 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5730 if (addr_cache != NULL && eager->tcp_remote == 5731 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5732 eager->tcp_dontdrop = B_TRUE; 5733 } 5734 } 5735 5736 /* 5737 * We need to insert the eager in its own perimeter but as soon 5738 * as we do that, we expose the eager to the classifier and 5739 * should not touch any field outside the eager's perimeter. 5740 * So do all the work necessary before inserting the eager 5741 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5742 * will succeed but undo everything if it fails. 5743 */ 5744 seg_seq = ABE32_TO_U32(tcph->th_seq); 5745 eager->tcp_irs = seg_seq; 5746 eager->tcp_rack = seg_seq; 5747 eager->tcp_rnxt = seg_seq + 1; 5748 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5749 BUMP_MIB(&tcp_mib, tcpPassiveOpens); 5750 eager->tcp_state = TCPS_SYN_RCVD; 5751 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5752 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5753 if (mp1 == NULL) 5754 goto error1; 5755 DB_CPID(mp1) = tcp->tcp_cpid; 5756 5757 /* 5758 * We need to start the rto timer. In normal case, we start 5759 * the timer after sending the packet on the wire (or at 5760 * least believing that packet was sent by waiting for 5761 * CALL_IP_WPUT() to return). Since this is the first packet 5762 * being sent on the wire for the eager, our initial tcp_rto 5763 * is at least tcp_rexmit_interval_min which is a fairly 5764 * large value to allow the algorithm to adjust slowly to large 5765 * fluctuations of RTT during first few transmissions. 5766 * 5767 * Starting the timer first and then sending the packet in this 5768 * case shouldn't make much difference since tcp_rexmit_interval_min 5769 * is of the order of several 100ms and starting the timer 5770 * first and then sending the packet will result in difference 5771 * of few micro seconds. 5772 * 5773 * Without this optimization, we are forced to hold the fanout 5774 * lock across the ipcl_bind_insert() and sending the packet 5775 * so that we don't race against an incoming packet (maybe RST) 5776 * for this eager. 5777 */ 5778 5779 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5780 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5781 5782 5783 /* 5784 * Insert the eager in its own perimeter now. We are ready to deal 5785 * with any packets on eager. 5786 */ 5787 if (eager->tcp_ipversion == IPV4_VERSION) { 5788 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5789 goto error; 5790 } 5791 } else { 5792 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5793 goto error; 5794 } 5795 } 5796 5797 /* mark conn as fully-bound */ 5798 econnp->conn_fully_bound = B_TRUE; 5799 5800 /* Send the SYN-ACK */ 5801 tcp_send_data(eager, eager->tcp_wq, mp1); 5802 freemsg(mp); 5803 5804 return; 5805 error: 5806 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 5807 freemsg(mp1); 5808 error1: 5809 /* Undo what we did above */ 5810 mutex_enter(&tcp->tcp_eager_lock); 5811 tcp_eager_unlink(eager); 5812 mutex_exit(&tcp->tcp_eager_lock); 5813 /* Drop eager's reference on the listener */ 5814 CONN_DEC_REF(connp); 5815 5816 /* 5817 * Delete the cached ire in conn_ire_cache and also mark 5818 * the conn as CONDEMNED 5819 */ 5820 mutex_enter(&econnp->conn_lock); 5821 econnp->conn_state_flags |= CONN_CONDEMNED; 5822 ire = econnp->conn_ire_cache; 5823 econnp->conn_ire_cache = NULL; 5824 mutex_exit(&econnp->conn_lock); 5825 if (ire != NULL) 5826 IRE_REFRELE_NOTR(ire); 5827 5828 /* 5829 * tcp_accept_comm inserts the eager to the bind_hash 5830 * we need to remove it from the hash if ipcl_conn_insert 5831 * fails. 5832 */ 5833 tcp_bind_hash_remove(eager); 5834 /* Drop the eager ref placed in tcp_open_detached */ 5835 CONN_DEC_REF(econnp); 5836 5837 /* 5838 * If a connection already exists, send the mp to that connections so 5839 * that it can be appropriately dealt with. 5840 */ 5841 if ((econnp = ipcl_classify(mp, connp->conn_zoneid)) != NULL) { 5842 if (!IPCL_IS_CONNECTED(econnp)) { 5843 /* 5844 * Something bad happened. ipcl_conn_insert() 5845 * failed because a connection already existed 5846 * in connected hash but we can't find it 5847 * anymore (someone blew it away). Just 5848 * free this message and hopefully remote 5849 * will retransmit at which time the SYN can be 5850 * treated as a new connection or dealth with 5851 * a TH_RST if a connection already exists. 5852 */ 5853 freemsg(mp); 5854 } else { 5855 squeue_fill(econnp->conn_sqp, mp, tcp_input, 5856 econnp, SQTAG_TCP_CONN_REQ); 5857 } 5858 } else { 5859 /* Nobody wants this packet */ 5860 freemsg(mp); 5861 } 5862 return; 5863 error2: 5864 freemsg(mp); 5865 return; 5866 error3: 5867 CONN_DEC_REF(econnp); 5868 freemsg(mp); 5869 } 5870 5871 /* 5872 * In an ideal case of vertical partition in NUMA architecture, its 5873 * beneficial to have the listener and all the incoming connections 5874 * tied to the same squeue. The other constraint is that incoming 5875 * connections should be tied to the squeue attached to interrupted 5876 * CPU for obvious locality reason so this leaves the listener to 5877 * be tied to the same squeue. Our only problem is that when listener 5878 * is binding, the CPU that will get interrupted by the NIC whose 5879 * IP address the listener is binding to is not even known. So 5880 * the code below allows us to change that binding at the time the 5881 * CPU is interrupted by virtue of incoming connection's squeue. 5882 * 5883 * This is usefull only in case of a listener bound to a specific IP 5884 * address. For other kind of listeners, they get bound the 5885 * very first time and there is no attempt to rebind them. 5886 */ 5887 void 5888 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5889 { 5890 conn_t *connp = (conn_t *)arg; 5891 squeue_t *sqp = (squeue_t *)arg2; 5892 squeue_t *new_sqp; 5893 uint32_t conn_flags; 5894 5895 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5896 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5897 } else { 5898 goto done; 5899 } 5900 5901 if (connp->conn_fanout == NULL) 5902 goto done; 5903 5904 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5905 mutex_enter(&connp->conn_fanout->connf_lock); 5906 mutex_enter(&connp->conn_lock); 5907 /* 5908 * No one from read or write side can access us now 5909 * except for already queued packets on this squeue. 5910 * But since we haven't changed the squeue yet, they 5911 * can't execute. If they are processed after we have 5912 * changed the squeue, they are sent back to the 5913 * correct squeue down below. 5914 */ 5915 if (connp->conn_sqp != new_sqp) { 5916 while (connp->conn_sqp != new_sqp) 5917 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5918 } 5919 5920 do { 5921 conn_flags = connp->conn_flags; 5922 conn_flags |= IPCL_FULLY_BOUND; 5923 (void) cas32(&connp->conn_flags, connp->conn_flags, 5924 conn_flags); 5925 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5926 5927 mutex_exit(&connp->conn_fanout->connf_lock); 5928 mutex_exit(&connp->conn_lock); 5929 } 5930 5931 done: 5932 if (connp->conn_sqp != sqp) { 5933 CONN_INC_REF(connp); 5934 squeue_fill(connp->conn_sqp, mp, 5935 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 5936 } else { 5937 tcp_conn_request(connp, mp, sqp); 5938 } 5939 } 5940 5941 /* 5942 * Successful connect request processing begins when our client passes 5943 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5944 * our T_OK_ACK reply message upstream. The control flow looks like this: 5945 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 5946 * upstream <- tcp_rput() <- IP 5947 * After various error checks are completed, tcp_connect() lays 5948 * the target address and port into the composite header template, 5949 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5950 * request followed by an IRE request, and passes the three mblk message 5951 * down to IP looking like this: 5952 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5953 * Processing continues in tcp_rput() when we receive the following message: 5954 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5955 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5956 * to fire off the connection request, and then passes the T_OK_ACK mblk 5957 * upstream that we filled in below. There are, of course, numerous 5958 * error conditions along the way which truncate the processing described 5959 * above. 5960 */ 5961 static void 5962 tcp_connect(tcp_t *tcp, mblk_t *mp) 5963 { 5964 sin_t *sin; 5965 sin6_t *sin6; 5966 queue_t *q = tcp->tcp_wq; 5967 struct T_conn_req *tcr; 5968 ipaddr_t *dstaddrp; 5969 in_port_t dstport; 5970 uint_t srcid; 5971 5972 tcr = (struct T_conn_req *)mp->b_rptr; 5973 5974 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5975 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5976 tcp_err_ack(tcp, mp, TPROTO, 0); 5977 return; 5978 } 5979 5980 /* 5981 * Determine packet type based on type of address passed in 5982 * the request should contain an IPv4 or IPv6 address. 5983 * Make sure that address family matches the type of 5984 * family of the the address passed down 5985 */ 5986 switch (tcr->DEST_length) { 5987 default: 5988 tcp_err_ack(tcp, mp, TBADADDR, 0); 5989 return; 5990 5991 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5992 /* 5993 * XXX: The check for valid DEST_length was not there 5994 * in earlier releases and some buggy 5995 * TLI apps (e.g Sybase) got away with not feeding 5996 * in sin_zero part of address. 5997 * We allow that bug to keep those buggy apps humming. 5998 * Test suites require the check on DEST_length. 5999 * We construct a new mblk with valid DEST_length 6000 * free the original so the rest of the code does 6001 * not have to keep track of this special shorter 6002 * length address case. 6003 */ 6004 mblk_t *nmp; 6005 struct T_conn_req *ntcr; 6006 sin_t *nsin; 6007 6008 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 6009 tcr->OPT_length, BPRI_HI); 6010 if (nmp == NULL) { 6011 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 6012 return; 6013 } 6014 ntcr = (struct T_conn_req *)nmp->b_rptr; 6015 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 6016 ntcr->PRIM_type = T_CONN_REQ; 6017 ntcr->DEST_length = sizeof (sin_t); 6018 ntcr->DEST_offset = sizeof (struct T_conn_req); 6019 6020 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 6021 *nsin = sin_null; 6022 /* Get pointer to shorter address to copy from original mp */ 6023 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6024 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 6025 if (sin == NULL || !OK_32PTR((char *)sin)) { 6026 freemsg(nmp); 6027 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6028 return; 6029 } 6030 nsin->sin_family = sin->sin_family; 6031 nsin->sin_port = sin->sin_port; 6032 nsin->sin_addr = sin->sin_addr; 6033 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 6034 nmp->b_wptr = (uchar_t *)&nsin[1]; 6035 if (tcr->OPT_length != 0) { 6036 ntcr->OPT_length = tcr->OPT_length; 6037 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 6038 bcopy((uchar_t *)tcr + tcr->OPT_offset, 6039 (uchar_t *)ntcr + ntcr->OPT_offset, 6040 tcr->OPT_length); 6041 nmp->b_wptr += tcr->OPT_length; 6042 } 6043 freemsg(mp); /* original mp freed */ 6044 mp = nmp; /* re-initialize original variables */ 6045 tcr = ntcr; 6046 } 6047 /* FALLTHRU */ 6048 6049 case sizeof (sin_t): 6050 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 6051 sizeof (sin_t)); 6052 if (sin == NULL || !OK_32PTR((char *)sin)) { 6053 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6054 return; 6055 } 6056 if (tcp->tcp_family != AF_INET || 6057 sin->sin_family != AF_INET) { 6058 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6059 return; 6060 } 6061 if (sin->sin_port == 0) { 6062 tcp_err_ack(tcp, mp, TBADADDR, 0); 6063 return; 6064 } 6065 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 6066 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6067 return; 6068 } 6069 6070 break; 6071 6072 case sizeof (sin6_t): 6073 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 6074 sizeof (sin6_t)); 6075 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 6076 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 6077 return; 6078 } 6079 if (tcp->tcp_family != AF_INET6 || 6080 sin6->sin6_family != AF_INET6) { 6081 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 6082 return; 6083 } 6084 if (sin6->sin6_port == 0) { 6085 tcp_err_ack(tcp, mp, TBADADDR, 0); 6086 return; 6087 } 6088 break; 6089 } 6090 /* 6091 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 6092 * should key on their sequence number and cut them loose. 6093 */ 6094 6095 /* 6096 * If options passed in, feed it for verification and handling 6097 */ 6098 if (tcr->OPT_length != 0) { 6099 mblk_t *ok_mp; 6100 mblk_t *discon_mp; 6101 mblk_t *conn_opts_mp; 6102 int t_error, sys_error, do_disconnect; 6103 6104 conn_opts_mp = NULL; 6105 6106 if (tcp_conprim_opt_process(tcp, mp, 6107 &do_disconnect, &t_error, &sys_error) < 0) { 6108 if (do_disconnect) { 6109 ASSERT(t_error == 0 && sys_error == 0); 6110 discon_mp = mi_tpi_discon_ind(NULL, 6111 ECONNREFUSED, 0); 6112 if (!discon_mp) { 6113 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6114 TSYSERR, ENOMEM); 6115 return; 6116 } 6117 ok_mp = mi_tpi_ok_ack_alloc(mp); 6118 if (!ok_mp) { 6119 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6120 TSYSERR, ENOMEM); 6121 return; 6122 } 6123 qreply(q, ok_mp); 6124 qreply(q, discon_mp); /* no flush! */ 6125 } else { 6126 ASSERT(t_error != 0); 6127 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 6128 sys_error); 6129 } 6130 return; 6131 } 6132 /* 6133 * Success in setting options, the mp option buffer represented 6134 * by OPT_length/offset has been potentially modified and 6135 * contains results of option processing. We copy it in 6136 * another mp to save it for potentially influencing returning 6137 * it in T_CONN_CONN. 6138 */ 6139 if (tcr->OPT_length != 0) { /* there are resulting options */ 6140 conn_opts_mp = copyb(mp); 6141 if (!conn_opts_mp) { 6142 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 6143 TSYSERR, ENOMEM); 6144 return; 6145 } 6146 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 6147 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 6148 /* 6149 * Note: 6150 * These resulting option negotiation can include any 6151 * end-to-end negotiation options but there no such 6152 * thing (yet?) in our TCP/IP. 6153 */ 6154 } 6155 } 6156 6157 /* 6158 * If we're connecting to an IPv4-mapped IPv6 address, we need to 6159 * make sure that the template IP header in the tcp structure is an 6160 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 6161 * need to this before we call tcp_bindi() so that the port lookup 6162 * code will look for ports in the correct port space (IPv4 and 6163 * IPv6 have separate port spaces). 6164 */ 6165 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 6166 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6167 int err = 0; 6168 6169 err = tcp_header_init_ipv4(tcp); 6170 if (err != 0) { 6171 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6172 goto connect_failed; 6173 } 6174 if (tcp->tcp_lport != 0) 6175 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 6176 } 6177 6178 switch (tcp->tcp_state) { 6179 case TCPS_IDLE: 6180 /* 6181 * We support quick connect, refer to comments in 6182 * tcp_connect_*() 6183 */ 6184 /* FALLTHRU */ 6185 case TCPS_BOUND: 6186 case TCPS_LISTEN: 6187 if (tcp->tcp_family == AF_INET6) { 6188 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 6189 tcp_connect_ipv6(tcp, mp, 6190 &sin6->sin6_addr, 6191 sin6->sin6_port, sin6->sin6_flowinfo, 6192 sin6->__sin6_src_id, sin6->sin6_scope_id); 6193 return; 6194 } 6195 /* 6196 * Destination adress is mapped IPv6 address. 6197 * Source bound address should be unspecified or 6198 * IPv6 mapped address as well. 6199 */ 6200 if (!IN6_IS_ADDR_UNSPECIFIED( 6201 &tcp->tcp_bound_source_v6) && 6202 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 6203 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 6204 EADDRNOTAVAIL); 6205 break; 6206 } 6207 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 6208 dstport = sin6->sin6_port; 6209 srcid = sin6->__sin6_src_id; 6210 } else { 6211 dstaddrp = &sin->sin_addr.s_addr; 6212 dstport = sin->sin_port; 6213 srcid = 0; 6214 } 6215 6216 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 6217 return; 6218 default: 6219 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 6220 break; 6221 } 6222 /* 6223 * Note: Code below is the "failure" case 6224 */ 6225 /* return error ack and blow away saved option results if any */ 6226 connect_failed: 6227 if (mp != NULL) 6228 putnext(tcp->tcp_rq, mp); 6229 else { 6230 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6231 TSYSERR, ENOMEM); 6232 } 6233 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6234 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6235 } 6236 6237 /* 6238 * Handle connect to IPv4 destinations, including connections for AF_INET6 6239 * sockets connecting to IPv4 mapped IPv6 destinations. 6240 */ 6241 static void 6242 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6243 uint_t srcid) 6244 { 6245 tcph_t *tcph; 6246 mblk_t *mp1; 6247 ipaddr_t dstaddr = *dstaddrp; 6248 int32_t oldstate; 6249 uint16_t lport; 6250 6251 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6252 6253 /* Check for attempt to connect to INADDR_ANY */ 6254 if (dstaddr == INADDR_ANY) { 6255 /* 6256 * SunOS 4.x and 4.3 BSD allow an application 6257 * to connect a TCP socket to INADDR_ANY. 6258 * When they do this, the kernel picks the 6259 * address of one interface and uses it 6260 * instead. The kernel usually ends up 6261 * picking the address of the loopback 6262 * interface. This is an undocumented feature. 6263 * However, we provide the same thing here 6264 * in order to have source and binary 6265 * compatibility with SunOS 4.x. 6266 * Update the T_CONN_REQ (sin/sin6) since it is used to 6267 * generate the T_CONN_CON. 6268 */ 6269 dstaddr = htonl(INADDR_LOOPBACK); 6270 *dstaddrp = dstaddr; 6271 } 6272 6273 /* Handle __sin6_src_id if socket not bound to an IP address */ 6274 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6275 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6276 tcp->tcp_connp->conn_zoneid); 6277 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6278 tcp->tcp_ipha->ipha_src); 6279 } 6280 6281 /* 6282 * Don't let an endpoint connect to itself. Note that 6283 * the test here does not catch the case where the 6284 * source IP addr was left unspecified by the user. In 6285 * this case, the source addr is set in tcp_adapt_ire() 6286 * using the reply to the T_BIND message that we send 6287 * down to IP here and the check is repeated in tcp_rput_other. 6288 */ 6289 if (dstaddr == tcp->tcp_ipha->ipha_src && 6290 dstport == tcp->tcp_lport) { 6291 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6292 goto failed; 6293 } 6294 6295 tcp->tcp_ipha->ipha_dst = dstaddr; 6296 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6297 6298 /* 6299 * Massage a source route if any putting the first hop 6300 * in iph_dst. Compute a starting value for the checksum which 6301 * takes into account that the original iph_dst should be 6302 * included in the checksum but that ip will include the 6303 * first hop in the source route in the tcp checksum. 6304 */ 6305 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha); 6306 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6307 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6308 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6309 if ((int)tcp->tcp_sum < 0) 6310 tcp->tcp_sum--; 6311 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6312 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6313 (tcp->tcp_sum >> 16)); 6314 tcph = tcp->tcp_tcph; 6315 *(uint16_t *)tcph->th_fport = dstport; 6316 tcp->tcp_fport = dstport; 6317 6318 oldstate = tcp->tcp_state; 6319 /* 6320 * At this point the remote destination address and remote port fields 6321 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6322 * have to see which state tcp was in so we can take apropriate action. 6323 */ 6324 if (oldstate == TCPS_IDLE) { 6325 /* 6326 * We support a quick connect capability here, allowing 6327 * clients to transition directly from IDLE to SYN_SENT 6328 * tcp_bindi will pick an unused port, insert the connection 6329 * in the bind hash and transition to BOUND state. 6330 */ 6331 lport = tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 6332 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6333 B_FALSE, B_FALSE); 6334 if (lport == 0) { 6335 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6336 goto failed; 6337 } 6338 } 6339 tcp->tcp_state = TCPS_SYN_SENT; 6340 6341 /* 6342 * TODO: allow data with connect requests 6343 * by unlinking M_DATA trailers here and 6344 * linking them in behind the T_OK_ACK mblk. 6345 * The tcp_rput() bind ack handler would then 6346 * feed them to tcp_wput_data() rather than call 6347 * tcp_timer(). 6348 */ 6349 mp = mi_tpi_ok_ack_alloc(mp); 6350 if (!mp) { 6351 tcp->tcp_state = oldstate; 6352 goto failed; 6353 } 6354 if (tcp->tcp_family == AF_INET) { 6355 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6356 sizeof (ipa_conn_t)); 6357 } else { 6358 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6359 sizeof (ipa6_conn_t)); 6360 } 6361 if (mp1) { 6362 /* Hang onto the T_OK_ACK for later. */ 6363 linkb(mp1, mp); 6364 mblk_setcred(mp1, tcp->tcp_cred); 6365 if (tcp->tcp_family == AF_INET) 6366 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6367 else { 6368 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6369 &tcp->tcp_sticky_ipp); 6370 } 6371 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6372 tcp->tcp_active_open = 1; 6373 /* 6374 * If the bind cannot complete immediately 6375 * IP will arrange to call tcp_rput_other 6376 * when the bind completes. 6377 */ 6378 if (mp1 != NULL) 6379 tcp_rput_other(tcp, mp1); 6380 return; 6381 } 6382 /* Error case */ 6383 tcp->tcp_state = oldstate; 6384 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6385 6386 failed: 6387 /* return error ack and blow away saved option results if any */ 6388 if (mp != NULL) 6389 putnext(tcp->tcp_rq, mp); 6390 else { 6391 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6392 TSYSERR, ENOMEM); 6393 } 6394 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6395 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6396 6397 } 6398 6399 /* 6400 * Handle connect to IPv6 destinations. 6401 */ 6402 static void 6403 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6404 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6405 { 6406 tcph_t *tcph; 6407 mblk_t *mp1; 6408 ip6_rthdr_t *rth; 6409 int32_t oldstate; 6410 uint16_t lport; 6411 6412 ASSERT(tcp->tcp_family == AF_INET6); 6413 6414 /* 6415 * If we're here, it means that the destination address is a native 6416 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6417 * reason why it might not be IPv6 is if the socket was bound to an 6418 * IPv4-mapped IPv6 address. 6419 */ 6420 if (tcp->tcp_ipversion != IPV6_VERSION) { 6421 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6422 goto failed; 6423 } 6424 6425 /* 6426 * Interpret a zero destination to mean loopback. 6427 * Update the T_CONN_REQ (sin/sin6) since it is used to 6428 * generate the T_CONN_CON. 6429 */ 6430 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6431 *dstaddrp = ipv6_loopback; 6432 } 6433 6434 /* Handle __sin6_src_id if socket not bound to an IP address */ 6435 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6436 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6437 tcp->tcp_connp->conn_zoneid); 6438 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6439 } 6440 6441 /* 6442 * Take care of the scope_id now and add ip6i_t 6443 * if ip6i_t is not already allocated through TCP 6444 * sticky options. At this point tcp_ip6h does not 6445 * have dst info, thus use dstaddrp. 6446 */ 6447 if (scope_id != 0 && 6448 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6449 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6450 ip6i_t *ip6i; 6451 6452 ipp->ipp_ifindex = scope_id; 6453 ip6i = (ip6i_t *)tcp->tcp_iphc; 6454 6455 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6456 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6457 /* Already allocated */ 6458 ip6i->ip6i_flags |= IP6I_IFINDEX; 6459 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6460 ipp->ipp_fields |= IPPF_SCOPE_ID; 6461 } else { 6462 int reterr; 6463 6464 ipp->ipp_fields |= IPPF_SCOPE_ID; 6465 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6466 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6467 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6468 if (reterr != 0) 6469 goto failed; 6470 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6471 } 6472 } 6473 6474 /* 6475 * Don't let an endpoint connect to itself. Note that 6476 * the test here does not catch the case where the 6477 * source IP addr was left unspecified by the user. In 6478 * this case, the source addr is set in tcp_adapt_ire() 6479 * using the reply to the T_BIND message that we send 6480 * down to IP here and the check is repeated in tcp_rput_other. 6481 */ 6482 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6483 (dstport == tcp->tcp_lport)) { 6484 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6485 goto failed; 6486 } 6487 6488 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6489 tcp->tcp_remote_v6 = *dstaddrp; 6490 tcp->tcp_ip6h->ip6_vcf = 6491 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6492 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6493 6494 6495 /* 6496 * Massage a routing header (if present) putting the first hop 6497 * in ip6_dst. Compute a starting value for the checksum which 6498 * takes into account that the original ip6_dst should be 6499 * included in the checksum but that ip will include the 6500 * first hop in the source route in the tcp checksum. 6501 */ 6502 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6503 if (rth != NULL) { 6504 6505 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth); 6506 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6507 (tcp->tcp_sum >> 16)); 6508 } else { 6509 tcp->tcp_sum = 0; 6510 } 6511 6512 tcph = tcp->tcp_tcph; 6513 *(uint16_t *)tcph->th_fport = dstport; 6514 tcp->tcp_fport = dstport; 6515 6516 oldstate = tcp->tcp_state; 6517 /* 6518 * At this point the remote destination address and remote port fields 6519 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6520 * have to see which state tcp was in so we can take apropriate action. 6521 */ 6522 if (oldstate == TCPS_IDLE) { 6523 /* 6524 * We support a quick connect capability here, allowing 6525 * clients to transition directly from IDLE to SYN_SENT 6526 * tcp_bindi will pick an unused port, insert the connection 6527 * in the bind hash and transition to BOUND state. 6528 */ 6529 lport = tcp_update_next_port(tcp_next_port_to_try, tcp, B_TRUE); 6530 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6531 B_FALSE, B_FALSE); 6532 if (lport == 0) { 6533 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6534 goto failed; 6535 } 6536 } 6537 tcp->tcp_state = TCPS_SYN_SENT; 6538 /* 6539 * TODO: allow data with connect requests 6540 * by unlinking M_DATA trailers here and 6541 * linking them in behind the T_OK_ACK mblk. 6542 * The tcp_rput() bind ack handler would then 6543 * feed them to tcp_wput_data() rather than call 6544 * tcp_timer(). 6545 */ 6546 mp = mi_tpi_ok_ack_alloc(mp); 6547 if (!mp) { 6548 tcp->tcp_state = oldstate; 6549 goto failed; 6550 } 6551 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6552 if (mp1) { 6553 /* Hang onto the T_OK_ACK for later. */ 6554 linkb(mp1, mp); 6555 mblk_setcred(mp1, tcp->tcp_cred); 6556 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6557 &tcp->tcp_sticky_ipp); 6558 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6559 tcp->tcp_active_open = 1; 6560 /* ip_bind_v6() may return ACK or ERROR */ 6561 if (mp1 != NULL) 6562 tcp_rput_other(tcp, mp1); 6563 return; 6564 } 6565 /* Error case */ 6566 tcp->tcp_state = oldstate; 6567 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6568 6569 failed: 6570 /* return error ack and blow away saved option results if any */ 6571 if (mp != NULL) 6572 putnext(tcp->tcp_rq, mp); 6573 else { 6574 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6575 TSYSERR, ENOMEM); 6576 } 6577 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6578 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6579 } 6580 6581 /* 6582 * We need a stream q for detached closing tcp connections 6583 * to use. Our client hereby indicates that this q is the 6584 * one to use. 6585 */ 6586 static void 6587 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6588 { 6589 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6590 queue_t *q = tcp->tcp_wq; 6591 6592 mp->b_datap->db_type = M_IOCACK; 6593 iocp->ioc_count = 0; 6594 mutex_enter(&tcp_g_q_lock); 6595 if (tcp_g_q != NULL) { 6596 mutex_exit(&tcp_g_q_lock); 6597 iocp->ioc_error = EALREADY; 6598 } else { 6599 mblk_t *mp1; 6600 6601 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6602 if (mp1 == NULL) { 6603 mutex_exit(&tcp_g_q_lock); 6604 iocp->ioc_error = ENOMEM; 6605 } else { 6606 tcp_g_q = tcp->tcp_rq; 6607 mutex_exit(&tcp_g_q_lock); 6608 iocp->ioc_error = 0; 6609 iocp->ioc_rval = 0; 6610 /* 6611 * We are passing tcp_sticky_ipp as NULL 6612 * as it is not useful for tcp_default queue 6613 */ 6614 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6615 if (mp1 != NULL) 6616 tcp_rput_other(tcp, mp1); 6617 } 6618 } 6619 qreply(q, mp); 6620 } 6621 6622 /* 6623 * Our client hereby directs us to reject the connection request 6624 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6625 * of sending the appropriate RST, not an ICMP error. 6626 */ 6627 static void 6628 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6629 { 6630 tcp_t *ltcp = NULL; 6631 t_scalar_t seqnum; 6632 conn_t *connp; 6633 6634 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6635 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6636 tcp_err_ack(tcp, mp, TPROTO, 0); 6637 return; 6638 } 6639 6640 /* 6641 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6642 * when the stream is in BOUND state. Do not send a reset, 6643 * since the destination IP address is not valid, and it can 6644 * be the initialized value of all zeros (broadcast address). 6645 * 6646 * If TCP has sent down a bind request to IP and has not 6647 * received the reply, reject the request. Otherwise, TCP 6648 * will be confused. 6649 */ 6650 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6651 if (tcp->tcp_debug) { 6652 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6653 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6654 } 6655 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6656 return; 6657 } 6658 6659 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6660 6661 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6662 6663 /* 6664 * According to TPI, for non-listeners, ignore seqnum 6665 * and disconnect. 6666 * Following interpretation of -1 seqnum is historical 6667 * and implied TPI ? (TPI only states that for T_CONN_IND, 6668 * a valid seqnum should not be -1). 6669 * 6670 * -1 means disconnect everything 6671 * regardless even on a listener. 6672 */ 6673 6674 int old_state = tcp->tcp_state; 6675 6676 /* 6677 * The connection can't be on the tcp_time_wait_head list 6678 * since it is not detached. 6679 */ 6680 ASSERT(tcp->tcp_time_wait_next == NULL); 6681 ASSERT(tcp->tcp_time_wait_prev == NULL); 6682 ASSERT(tcp->tcp_time_wait_expire == 0); 6683 ltcp = NULL; 6684 /* 6685 * If it used to be a listener, check to make sure no one else 6686 * has taken the port before switching back to LISTEN state. 6687 */ 6688 if (tcp->tcp_ipversion == IPV4_VERSION) { 6689 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6690 tcp->tcp_ipha->ipha_src, 6691 tcp->tcp_connp->conn_zoneid); 6692 if (connp != NULL) 6693 ltcp = connp->conn_tcp; 6694 } else { 6695 /* Allow tcp_bound_if listeners? */ 6696 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6697 &tcp->tcp_ip6h->ip6_src, 0, 6698 tcp->tcp_connp->conn_zoneid); 6699 if (connp != NULL) 6700 ltcp = connp->conn_tcp; 6701 } 6702 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6703 tcp->tcp_state = TCPS_LISTEN; 6704 } else if (old_state > TCPS_BOUND) { 6705 tcp->tcp_conn_req_max = 0; 6706 tcp->tcp_state = TCPS_BOUND; 6707 } 6708 if (ltcp != NULL) 6709 CONN_DEC_REF(ltcp->tcp_connp); 6710 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6711 BUMP_MIB(&tcp_mib, tcpAttemptFails); 6712 } else if (old_state == TCPS_ESTABLISHED || 6713 old_state == TCPS_CLOSE_WAIT) { 6714 BUMP_MIB(&tcp_mib, tcpEstabResets); 6715 } 6716 6717 if (tcp->tcp_fused) 6718 tcp_unfuse(tcp); 6719 6720 mutex_enter(&tcp->tcp_eager_lock); 6721 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6722 (tcp->tcp_conn_req_cnt_q != 0)) { 6723 tcp_eager_cleanup(tcp, 0); 6724 } 6725 mutex_exit(&tcp->tcp_eager_lock); 6726 6727 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6728 tcp->tcp_rnxt, TH_RST | TH_ACK); 6729 6730 tcp_reinit(tcp); 6731 6732 if (old_state >= TCPS_ESTABLISHED) { 6733 /* Send M_FLUSH according to TPI */ 6734 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6735 } 6736 mp = mi_tpi_ok_ack_alloc(mp); 6737 if (mp) 6738 putnext(tcp->tcp_rq, mp); 6739 return; 6740 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6741 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6742 return; 6743 } 6744 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6745 /* Send M_FLUSH according to TPI */ 6746 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6747 } 6748 mp = mi_tpi_ok_ack_alloc(mp); 6749 if (mp) 6750 putnext(tcp->tcp_rq, mp); 6751 } 6752 6753 /* 6754 * Diagnostic routine used to return a string associated with the tcp state. 6755 * Note that if the caller does not supply a buffer, it will use an internal 6756 * static string. This means that if multiple threads call this function at 6757 * the same time, output can be corrupted... Note also that this function 6758 * does not check the size of the supplied buffer. The caller has to make 6759 * sure that it is big enough. 6760 */ 6761 static char * 6762 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6763 { 6764 char buf1[30]; 6765 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6766 char *buf; 6767 char *cp; 6768 in6_addr_t local, remote; 6769 char local_addrbuf[INET6_ADDRSTRLEN]; 6770 char remote_addrbuf[INET6_ADDRSTRLEN]; 6771 6772 if (sup_buf != NULL) 6773 buf = sup_buf; 6774 else 6775 buf = priv_buf; 6776 6777 if (tcp == NULL) 6778 return ("NULL_TCP"); 6779 switch (tcp->tcp_state) { 6780 case TCPS_CLOSED: 6781 cp = "TCP_CLOSED"; 6782 break; 6783 case TCPS_IDLE: 6784 cp = "TCP_IDLE"; 6785 break; 6786 case TCPS_BOUND: 6787 cp = "TCP_BOUND"; 6788 break; 6789 case TCPS_LISTEN: 6790 cp = "TCP_LISTEN"; 6791 break; 6792 case TCPS_SYN_SENT: 6793 cp = "TCP_SYN_SENT"; 6794 break; 6795 case TCPS_SYN_RCVD: 6796 cp = "TCP_SYN_RCVD"; 6797 break; 6798 case TCPS_ESTABLISHED: 6799 cp = "TCP_ESTABLISHED"; 6800 break; 6801 case TCPS_CLOSE_WAIT: 6802 cp = "TCP_CLOSE_WAIT"; 6803 break; 6804 case TCPS_FIN_WAIT_1: 6805 cp = "TCP_FIN_WAIT_1"; 6806 break; 6807 case TCPS_CLOSING: 6808 cp = "TCP_CLOSING"; 6809 break; 6810 case TCPS_LAST_ACK: 6811 cp = "TCP_LAST_ACK"; 6812 break; 6813 case TCPS_FIN_WAIT_2: 6814 cp = "TCP_FIN_WAIT_2"; 6815 break; 6816 case TCPS_TIME_WAIT: 6817 cp = "TCP_TIME_WAIT"; 6818 break; 6819 default: 6820 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6821 cp = buf1; 6822 break; 6823 } 6824 switch (format) { 6825 case DISP_ADDR_AND_PORT: 6826 if (tcp->tcp_ipversion == IPV4_VERSION) { 6827 /* 6828 * Note that we use the remote address in the tcp_b 6829 * structure. This means that it will print out 6830 * the real destination address, not the next hop's 6831 * address if source routing is used. 6832 */ 6833 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6834 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6835 6836 } else { 6837 local = tcp->tcp_ip_src_v6; 6838 remote = tcp->tcp_remote_v6; 6839 } 6840 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6841 sizeof (local_addrbuf)); 6842 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6843 sizeof (remote_addrbuf)); 6844 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6845 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6846 ntohs(tcp->tcp_fport), cp); 6847 break; 6848 case DISP_PORT_ONLY: 6849 default: 6850 (void) mi_sprintf(buf, "[%u, %u] %s", 6851 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6852 break; 6853 } 6854 6855 return (buf); 6856 } 6857 6858 /* 6859 * Called via squeue to get on to eager's perimeter to send a 6860 * TH_RST. The listener wants the eager to disappear either 6861 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 6862 * being called. 6863 */ 6864 /* ARGSUSED */ 6865 void 6866 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6867 { 6868 conn_t *econnp = (conn_t *)arg; 6869 tcp_t *eager = econnp->conn_tcp; 6870 tcp_t *listener = eager->tcp_listener; 6871 6872 /* 6873 * We could be called because listener is closing. Since 6874 * the eager is using listener's queue's, its not safe. 6875 * Better use the default queue just to send the TH_RST 6876 * out. 6877 */ 6878 eager->tcp_rq = tcp_g_q; 6879 eager->tcp_wq = WR(tcp_g_q); 6880 6881 if (eager->tcp_state > TCPS_LISTEN) { 6882 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6883 eager, eager->tcp_snxt, 0, TH_RST); 6884 } 6885 6886 /* We are here because listener wants this eager gone */ 6887 if (listener != NULL) { 6888 mutex_enter(&listener->tcp_eager_lock); 6889 tcp_eager_unlink(eager); 6890 if (eager->tcp_conn.tcp_eager_conn_ind == NULL) { 6891 /* 6892 * The eager has sent a conn_ind up to the 6893 * listener but listener decides to close 6894 * instead. We need to drop the extra ref 6895 * placed on eager in tcp_rput_data() before 6896 * sending the conn_ind to listener. 6897 */ 6898 CONN_DEC_REF(econnp); 6899 } 6900 mutex_exit(&listener->tcp_eager_lock); 6901 CONN_DEC_REF(listener->tcp_connp); 6902 } 6903 6904 if (eager->tcp_state > TCPS_BOUND) 6905 tcp_close_detached(eager); 6906 } 6907 6908 /* 6909 * Reset any eager connection hanging off this listener marked 6910 * with 'seqnum' and then reclaim it's resources. 6911 */ 6912 static boolean_t 6913 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6914 { 6915 tcp_t *eager; 6916 mblk_t *mp; 6917 6918 TCP_STAT(tcp_eager_blowoff_calls); 6919 eager = listener; 6920 mutex_enter(&listener->tcp_eager_lock); 6921 do { 6922 eager = eager->tcp_eager_next_q; 6923 if (eager == NULL) { 6924 mutex_exit(&listener->tcp_eager_lock); 6925 return (B_FALSE); 6926 } 6927 } while (eager->tcp_conn_req_seqnum != seqnum); 6928 CONN_INC_REF(eager->tcp_connp); 6929 mutex_exit(&listener->tcp_eager_lock); 6930 mp = &eager->tcp_closemp; 6931 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6932 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 6933 return (B_TRUE); 6934 } 6935 6936 /* 6937 * Reset any eager connection hanging off this listener 6938 * and then reclaim it's resources. 6939 */ 6940 static void 6941 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6942 { 6943 tcp_t *eager; 6944 mblk_t *mp; 6945 6946 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6947 6948 if (!q0_only) { 6949 /* First cleanup q */ 6950 TCP_STAT(tcp_eager_blowoff_q); 6951 eager = listener->tcp_eager_next_q; 6952 while (eager != NULL) { 6953 CONN_INC_REF(eager->tcp_connp); 6954 mp = &eager->tcp_closemp; 6955 squeue_fill(eager->tcp_connp->conn_sqp, mp, 6956 tcp_eager_kill, eager->tcp_connp, 6957 SQTAG_TCP_EAGER_CLEANUP); 6958 eager = eager->tcp_eager_next_q; 6959 } 6960 } 6961 /* Then cleanup q0 */ 6962 TCP_STAT(tcp_eager_blowoff_q0); 6963 eager = listener->tcp_eager_next_q0; 6964 while (eager != listener) { 6965 CONN_INC_REF(eager->tcp_connp); 6966 mp = &eager->tcp_closemp; 6967 squeue_fill(eager->tcp_connp->conn_sqp, mp, 6968 tcp_eager_kill, eager->tcp_connp, 6969 SQTAG_TCP_EAGER_CLEANUP_Q0); 6970 eager = eager->tcp_eager_next_q0; 6971 } 6972 } 6973 6974 /* 6975 * If we are an eager connection hanging off a listener that hasn't 6976 * formally accepted the connection yet, get off his list and blow off 6977 * any data that we have accumulated. 6978 */ 6979 static void 6980 tcp_eager_unlink(tcp_t *tcp) 6981 { 6982 tcp_t *listener = tcp->tcp_listener; 6983 6984 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6985 ASSERT(listener != NULL); 6986 if (tcp->tcp_eager_next_q0 != NULL) { 6987 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6988 6989 /* Remove the eager tcp from q0 */ 6990 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6991 tcp->tcp_eager_prev_q0; 6992 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6993 tcp->tcp_eager_next_q0; 6994 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6995 listener->tcp_conn_req_cnt_q0--; 6996 6997 tcp->tcp_eager_next_q0 = NULL; 6998 tcp->tcp_eager_prev_q0 = NULL; 6999 7000 if (tcp->tcp_syn_rcvd_timeout != 0) { 7001 /* we have timed out before */ 7002 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 7003 listener->tcp_syn_rcvd_timeout--; 7004 } 7005 } else { 7006 tcp_t **tcpp = &listener->tcp_eager_next_q; 7007 tcp_t *prev = NULL; 7008 7009 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 7010 if (tcpp[0] == tcp) { 7011 if (listener->tcp_eager_last_q == tcp) { 7012 /* 7013 * If we are unlinking the last 7014 * element on the list, adjust 7015 * tail pointer. Set tail pointer 7016 * to nil when list is empty. 7017 */ 7018 ASSERT(tcp->tcp_eager_next_q == NULL); 7019 if (listener->tcp_eager_last_q == 7020 listener->tcp_eager_next_q) { 7021 listener->tcp_eager_last_q = 7022 NULL; 7023 } else { 7024 /* 7025 * We won't get here if there 7026 * is only one eager in the 7027 * list. 7028 */ 7029 ASSERT(prev != NULL); 7030 listener->tcp_eager_last_q = 7031 prev; 7032 } 7033 } 7034 tcpp[0] = tcp->tcp_eager_next_q; 7035 tcp->tcp_eager_next_q = NULL; 7036 tcp->tcp_eager_last_q = NULL; 7037 ASSERT(listener->tcp_conn_req_cnt_q > 0); 7038 listener->tcp_conn_req_cnt_q--; 7039 break; 7040 } 7041 prev = tcpp[0]; 7042 } 7043 } 7044 tcp->tcp_listener = NULL; 7045 } 7046 7047 /* Shorthand to generate and send TPI error acks to our client */ 7048 static void 7049 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 7050 { 7051 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 7052 putnext(tcp->tcp_rq, mp); 7053 } 7054 7055 /* Shorthand to generate and send TPI error acks to our client */ 7056 static void 7057 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 7058 int t_error, int sys_error) 7059 { 7060 struct T_error_ack *teackp; 7061 7062 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 7063 M_PCPROTO, T_ERROR_ACK)) != NULL) { 7064 teackp = (struct T_error_ack *)mp->b_rptr; 7065 teackp->ERROR_prim = primitive; 7066 teackp->TLI_error = t_error; 7067 teackp->UNIX_error = sys_error; 7068 putnext(tcp->tcp_rq, mp); 7069 } 7070 } 7071 7072 /* 7073 * Note: No locks are held when inspecting tcp_g_*epriv_ports 7074 * but instead the code relies on: 7075 * - the fact that the address of the array and its size never changes 7076 * - the atomic assignment of the elements of the array 7077 */ 7078 /* ARGSUSED */ 7079 static int 7080 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 7081 { 7082 int i; 7083 7084 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7085 if (tcp_g_epriv_ports[i] != 0) 7086 (void) mi_mpprintf(mp, "%d ", tcp_g_epriv_ports[i]); 7087 } 7088 return (0); 7089 } 7090 7091 /* 7092 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7093 * threads from changing it at the same time. 7094 */ 7095 /* ARGSUSED */ 7096 static int 7097 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7098 cred_t *cr) 7099 { 7100 long new_value; 7101 int i; 7102 7103 /* 7104 * Fail the request if the new value does not lie within the 7105 * port number limits. 7106 */ 7107 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 7108 new_value <= 0 || new_value >= 65536) { 7109 return (EINVAL); 7110 } 7111 7112 mutex_enter(&tcp_epriv_port_lock); 7113 /* Check if the value is already in the list */ 7114 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7115 if (new_value == tcp_g_epriv_ports[i]) { 7116 mutex_exit(&tcp_epriv_port_lock); 7117 return (EEXIST); 7118 } 7119 } 7120 /* Find an empty slot */ 7121 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7122 if (tcp_g_epriv_ports[i] == 0) 7123 break; 7124 } 7125 if (i == tcp_g_num_epriv_ports) { 7126 mutex_exit(&tcp_epriv_port_lock); 7127 return (EOVERFLOW); 7128 } 7129 /* Set the new value */ 7130 tcp_g_epriv_ports[i] = (uint16_t)new_value; 7131 mutex_exit(&tcp_epriv_port_lock); 7132 return (0); 7133 } 7134 7135 /* 7136 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 7137 * threads from changing it at the same time. 7138 */ 7139 /* ARGSUSED */ 7140 static int 7141 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 7142 cred_t *cr) 7143 { 7144 long new_value; 7145 int i; 7146 7147 /* 7148 * Fail the request if the new value does not lie within the 7149 * port number limits. 7150 */ 7151 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 7152 new_value >= 65536) { 7153 return (EINVAL); 7154 } 7155 7156 mutex_enter(&tcp_epriv_port_lock); 7157 /* Check that the value is already in the list */ 7158 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 7159 if (tcp_g_epriv_ports[i] == new_value) 7160 break; 7161 } 7162 if (i == tcp_g_num_epriv_ports) { 7163 mutex_exit(&tcp_epriv_port_lock); 7164 return (ESRCH); 7165 } 7166 /* Clear the value */ 7167 tcp_g_epriv_ports[i] = 0; 7168 mutex_exit(&tcp_epriv_port_lock); 7169 return (0); 7170 } 7171 7172 /* Return the TPI/TLI equivalent of our current tcp_state */ 7173 static int 7174 tcp_tpistate(tcp_t *tcp) 7175 { 7176 switch (tcp->tcp_state) { 7177 case TCPS_IDLE: 7178 return (TS_UNBND); 7179 case TCPS_LISTEN: 7180 /* 7181 * Return whether there are outstanding T_CONN_IND waiting 7182 * for the matching T_CONN_RES. Therefore don't count q0. 7183 */ 7184 if (tcp->tcp_conn_req_cnt_q > 0) 7185 return (TS_WRES_CIND); 7186 else 7187 return (TS_IDLE); 7188 case TCPS_BOUND: 7189 return (TS_IDLE); 7190 case TCPS_SYN_SENT: 7191 return (TS_WCON_CREQ); 7192 case TCPS_SYN_RCVD: 7193 /* 7194 * Note: assumption: this has to the active open SYN_RCVD. 7195 * The passive instance is detached in SYN_RCVD stage of 7196 * incoming connection processing so we cannot get request 7197 * for T_info_ack on it. 7198 */ 7199 return (TS_WACK_CRES); 7200 case TCPS_ESTABLISHED: 7201 return (TS_DATA_XFER); 7202 case TCPS_CLOSE_WAIT: 7203 return (TS_WREQ_ORDREL); 7204 case TCPS_FIN_WAIT_1: 7205 return (TS_WIND_ORDREL); 7206 case TCPS_FIN_WAIT_2: 7207 return (TS_WIND_ORDREL); 7208 7209 case TCPS_CLOSING: 7210 case TCPS_LAST_ACK: 7211 case TCPS_TIME_WAIT: 7212 case TCPS_CLOSED: 7213 /* 7214 * Following TS_WACK_DREQ7 is a rendition of "not 7215 * yet TS_IDLE" TPI state. There is no best match to any 7216 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 7217 * choose a value chosen that will map to TLI/XTI level 7218 * state of TSTATECHNG (state is process of changing) which 7219 * captures what this dummy state represents. 7220 */ 7221 return (TS_WACK_DREQ7); 7222 default: 7223 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7224 tcp->tcp_state, tcp_display(tcp, NULL, 7225 DISP_PORT_ONLY)); 7226 return (TS_UNBND); 7227 } 7228 } 7229 7230 static void 7231 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7232 { 7233 if (tcp->tcp_family == AF_INET6) 7234 *tia = tcp_g_t_info_ack_v6; 7235 else 7236 *tia = tcp_g_t_info_ack; 7237 tia->CURRENT_state = tcp_tpistate(tcp); 7238 tia->OPT_size = tcp_max_optsize; 7239 if (tcp->tcp_mss == 0) { 7240 /* Not yet set - tcp_open does not set mss */ 7241 if (tcp->tcp_ipversion == IPV4_VERSION) 7242 tia->TIDU_size = tcp_mss_def_ipv4; 7243 else 7244 tia->TIDU_size = tcp_mss_def_ipv6; 7245 } else { 7246 tia->TIDU_size = tcp->tcp_mss; 7247 } 7248 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7249 } 7250 7251 /* 7252 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7253 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7254 * tcp_g_t_info_ack. The current state of the stream is copied from 7255 * tcp_state. 7256 */ 7257 static void 7258 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7259 { 7260 t_uscalar_t cap_bits1; 7261 struct T_capability_ack *tcap; 7262 7263 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7264 freemsg(mp); 7265 return; 7266 } 7267 7268 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7269 7270 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7271 mp->b_datap->db_type, T_CAPABILITY_ACK); 7272 if (mp == NULL) 7273 return; 7274 7275 tcap = (struct T_capability_ack *)mp->b_rptr; 7276 tcap->CAP_bits1 = 0; 7277 7278 if (cap_bits1 & TC1_INFO) { 7279 tcp_copy_info(&tcap->INFO_ack, tcp); 7280 tcap->CAP_bits1 |= TC1_INFO; 7281 } 7282 7283 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7284 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7285 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7286 } 7287 7288 putnext(tcp->tcp_rq, mp); 7289 } 7290 7291 /* 7292 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7293 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7294 * The current state of the stream is copied from tcp_state. 7295 */ 7296 static void 7297 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7298 { 7299 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7300 T_INFO_ACK); 7301 if (!mp) { 7302 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7303 return; 7304 } 7305 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7306 putnext(tcp->tcp_rq, mp); 7307 } 7308 7309 /* Respond to the TPI addr request */ 7310 static void 7311 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7312 { 7313 sin_t *sin; 7314 mblk_t *ackmp; 7315 struct T_addr_ack *taa; 7316 7317 /* Make it large enough for worst case */ 7318 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7319 2 * sizeof (sin6_t), 1); 7320 if (ackmp == NULL) { 7321 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7322 return; 7323 } 7324 7325 if (tcp->tcp_ipversion == IPV6_VERSION) { 7326 tcp_addr_req_ipv6(tcp, ackmp); 7327 return; 7328 } 7329 taa = (struct T_addr_ack *)ackmp->b_rptr; 7330 7331 bzero(taa, sizeof (struct T_addr_ack)); 7332 ackmp->b_wptr = (uchar_t *)&taa[1]; 7333 7334 taa->PRIM_type = T_ADDR_ACK; 7335 ackmp->b_datap->db_type = M_PCPROTO; 7336 7337 /* 7338 * Note: Following code assumes 32 bit alignment of basic 7339 * data structures like sin_t and struct T_addr_ack. 7340 */ 7341 if (tcp->tcp_state >= TCPS_BOUND) { 7342 /* 7343 * Fill in local address 7344 */ 7345 taa->LOCADDR_length = sizeof (sin_t); 7346 taa->LOCADDR_offset = sizeof (*taa); 7347 7348 sin = (sin_t *)&taa[1]; 7349 7350 /* Fill zeroes and then intialize non-zero fields */ 7351 *sin = sin_null; 7352 7353 sin->sin_family = AF_INET; 7354 7355 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7356 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7357 7358 ackmp->b_wptr = (uchar_t *)&sin[1]; 7359 7360 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7361 /* 7362 * Fill in Remote address 7363 */ 7364 taa->REMADDR_length = sizeof (sin_t); 7365 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7366 taa->LOCADDR_length); 7367 7368 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7369 *sin = sin_null; 7370 sin->sin_family = AF_INET; 7371 sin->sin_addr.s_addr = tcp->tcp_remote; 7372 sin->sin_port = tcp->tcp_fport; 7373 7374 ackmp->b_wptr = (uchar_t *)&sin[1]; 7375 } 7376 } 7377 putnext(tcp->tcp_rq, ackmp); 7378 } 7379 7380 /* Assumes that tcp_addr_req gets enough space and alignment */ 7381 static void 7382 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7383 { 7384 sin6_t *sin6; 7385 struct T_addr_ack *taa; 7386 7387 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7388 ASSERT(OK_32PTR(ackmp->b_rptr)); 7389 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7390 2 * sizeof (sin6_t)); 7391 7392 taa = (struct T_addr_ack *)ackmp->b_rptr; 7393 7394 bzero(taa, sizeof (struct T_addr_ack)); 7395 ackmp->b_wptr = (uchar_t *)&taa[1]; 7396 7397 taa->PRIM_type = T_ADDR_ACK; 7398 ackmp->b_datap->db_type = M_PCPROTO; 7399 7400 /* 7401 * Note: Following code assumes 32 bit alignment of basic 7402 * data structures like sin6_t and struct T_addr_ack. 7403 */ 7404 if (tcp->tcp_state >= TCPS_BOUND) { 7405 /* 7406 * Fill in local address 7407 */ 7408 taa->LOCADDR_length = sizeof (sin6_t); 7409 taa->LOCADDR_offset = sizeof (*taa); 7410 7411 sin6 = (sin6_t *)&taa[1]; 7412 *sin6 = sin6_null; 7413 7414 sin6->sin6_family = AF_INET6; 7415 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7416 sin6->sin6_port = tcp->tcp_lport; 7417 7418 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7419 7420 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7421 /* 7422 * Fill in Remote address 7423 */ 7424 taa->REMADDR_length = sizeof (sin6_t); 7425 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7426 taa->LOCADDR_length); 7427 7428 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7429 *sin6 = sin6_null; 7430 sin6->sin6_family = AF_INET6; 7431 sin6->sin6_flowinfo = 7432 tcp->tcp_ip6h->ip6_vcf & 7433 ~IPV6_VERS_AND_FLOW_MASK; 7434 sin6->sin6_addr = tcp->tcp_remote_v6; 7435 sin6->sin6_port = tcp->tcp_fport; 7436 7437 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7438 } 7439 } 7440 putnext(tcp->tcp_rq, ackmp); 7441 } 7442 7443 /* 7444 * Handle reinitialization of a tcp structure. 7445 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7446 */ 7447 static void 7448 tcp_reinit(tcp_t *tcp) 7449 { 7450 mblk_t *mp; 7451 int err; 7452 7453 TCP_STAT(tcp_reinit_calls); 7454 7455 /* tcp_reinit should never be called for detached tcp_t's */ 7456 ASSERT(tcp->tcp_listener == NULL); 7457 ASSERT((tcp->tcp_family == AF_INET && 7458 tcp->tcp_ipversion == IPV4_VERSION) || 7459 (tcp->tcp_family == AF_INET6 && 7460 (tcp->tcp_ipversion == IPV4_VERSION || 7461 tcp->tcp_ipversion == IPV6_VERSION))); 7462 7463 /* Cancel outstanding timers */ 7464 tcp_timers_stop(tcp); 7465 7466 /* 7467 * Reset everything in the state vector, after updating global 7468 * MIB data from instance counters. 7469 */ 7470 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 7471 tcp->tcp_ibsegs = 0; 7472 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 7473 tcp->tcp_obsegs = 0; 7474 7475 tcp_close_mpp(&tcp->tcp_xmit_head); 7476 if (tcp->tcp_snd_zcopy_aware) 7477 tcp_zcopy_notify(tcp); 7478 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7479 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7480 if (tcp->tcp_flow_stopped && 7481 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7482 tcp_clrqfull(tcp); 7483 } 7484 tcp_close_mpp(&tcp->tcp_reass_head); 7485 tcp->tcp_reass_tail = NULL; 7486 if (tcp->tcp_rcv_list != NULL) { 7487 /* Free b_next chain */ 7488 tcp_close_mpp(&tcp->tcp_rcv_list); 7489 tcp->tcp_rcv_last_head = NULL; 7490 tcp->tcp_rcv_last_tail = NULL; 7491 tcp->tcp_rcv_cnt = 0; 7492 } 7493 tcp->tcp_rcv_last_tail = NULL; 7494 7495 if ((mp = tcp->tcp_urp_mp) != NULL) { 7496 freemsg(mp); 7497 tcp->tcp_urp_mp = NULL; 7498 } 7499 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7500 freemsg(mp); 7501 tcp->tcp_urp_mark_mp = NULL; 7502 } 7503 if (tcp->tcp_fused_sigurg_mp != NULL) { 7504 freeb(tcp->tcp_fused_sigurg_mp); 7505 tcp->tcp_fused_sigurg_mp = NULL; 7506 } 7507 7508 /* 7509 * Following is a union with two members which are 7510 * identical types and size so the following cleanup 7511 * is enough. 7512 */ 7513 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7514 7515 CL_INET_DISCONNECT(tcp); 7516 7517 /* 7518 * The connection can't be on the tcp_time_wait_head list 7519 * since it is not detached. 7520 */ 7521 ASSERT(tcp->tcp_time_wait_next == NULL); 7522 ASSERT(tcp->tcp_time_wait_prev == NULL); 7523 ASSERT(tcp->tcp_time_wait_expire == 0); 7524 7525 if (tcp->tcp_kssl_pending) { 7526 tcp->tcp_kssl_pending = B_FALSE; 7527 7528 /* Don't reset if the initialized by bind. */ 7529 if (tcp->tcp_kssl_ent != NULL) { 7530 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7531 KSSL_NO_PROXY); 7532 } 7533 } 7534 if (tcp->tcp_kssl_ctx != NULL) { 7535 kssl_release_ctx(tcp->tcp_kssl_ctx); 7536 tcp->tcp_kssl_ctx = NULL; 7537 } 7538 7539 /* 7540 * Reset/preserve other values 7541 */ 7542 tcp_reinit_values(tcp); 7543 ipcl_hash_remove(tcp->tcp_connp); 7544 conn_delete_ire(tcp->tcp_connp, NULL); 7545 7546 if (tcp->tcp_conn_req_max != 0) { 7547 /* 7548 * This is the case when a TLI program uses the same 7549 * transport end point to accept a connection. This 7550 * makes the TCP both a listener and acceptor. When 7551 * this connection is closed, we need to set the state 7552 * back to TCPS_LISTEN. Make sure that the eager list 7553 * is reinitialized. 7554 * 7555 * Note that this stream is still bound to the four 7556 * tuples of the previous connection in IP. If a new 7557 * SYN with different foreign address comes in, IP will 7558 * not find it and will send it to the global queue. In 7559 * the global queue, TCP will do a tcp_lookup_listener() 7560 * to find this stream. This works because this stream 7561 * is only removed from connected hash. 7562 * 7563 */ 7564 tcp->tcp_state = TCPS_LISTEN; 7565 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7566 tcp->tcp_connp->conn_recv = tcp_conn_request; 7567 if (tcp->tcp_family == AF_INET6) { 7568 ASSERT(tcp->tcp_connp->conn_af_isv6); 7569 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7570 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7571 } else { 7572 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7573 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7574 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7575 } 7576 } else { 7577 tcp->tcp_state = TCPS_BOUND; 7578 } 7579 7580 /* 7581 * Initialize to default values 7582 * Can't fail since enough header template space already allocated 7583 * at open(). 7584 */ 7585 err = tcp_init_values(tcp); 7586 ASSERT(err == 0); 7587 /* Restore state in tcp_tcph */ 7588 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7589 if (tcp->tcp_ipversion == IPV4_VERSION) 7590 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7591 else 7592 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7593 /* 7594 * Copy of the src addr. in tcp_t is needed in tcp_t 7595 * since the lookup funcs can only lookup on tcp_t 7596 */ 7597 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7598 7599 ASSERT(tcp->tcp_ptpbhn != NULL); 7600 tcp->tcp_rq->q_hiwat = tcp_recv_hiwat; 7601 tcp->tcp_rwnd = tcp_recv_hiwat; 7602 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7603 tcp_mss_def_ipv6 : tcp_mss_def_ipv4; 7604 } 7605 7606 /* 7607 * Force values to zero that need be zero. 7608 * Do not touch values asociated with the BOUND or LISTEN state 7609 * since the connection will end up in that state after the reinit. 7610 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7611 * structure! 7612 */ 7613 static void 7614 tcp_reinit_values(tcp) 7615 tcp_t *tcp; 7616 { 7617 #ifndef lint 7618 #define DONTCARE(x) 7619 #define PRESERVE(x) 7620 #else 7621 #define DONTCARE(x) ((x) = (x)) 7622 #define PRESERVE(x) ((x) = (x)) 7623 #endif /* lint */ 7624 7625 PRESERVE(tcp->tcp_bind_hash); 7626 PRESERVE(tcp->tcp_ptpbhn); 7627 PRESERVE(tcp->tcp_acceptor_hash); 7628 PRESERVE(tcp->tcp_ptpahn); 7629 7630 /* Should be ASSERT NULL on these with new code! */ 7631 ASSERT(tcp->tcp_time_wait_next == NULL); 7632 ASSERT(tcp->tcp_time_wait_prev == NULL); 7633 ASSERT(tcp->tcp_time_wait_expire == 0); 7634 PRESERVE(tcp->tcp_state); 7635 PRESERVE(tcp->tcp_rq); 7636 PRESERVE(tcp->tcp_wq); 7637 7638 ASSERT(tcp->tcp_xmit_head == NULL); 7639 ASSERT(tcp->tcp_xmit_last == NULL); 7640 ASSERT(tcp->tcp_unsent == 0); 7641 ASSERT(tcp->tcp_xmit_tail == NULL); 7642 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7643 7644 tcp->tcp_snxt = 0; /* Displayed in mib */ 7645 tcp->tcp_suna = 0; /* Displayed in mib */ 7646 tcp->tcp_swnd = 0; 7647 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7648 7649 ASSERT(tcp->tcp_ibsegs == 0); 7650 ASSERT(tcp->tcp_obsegs == 0); 7651 7652 if (tcp->tcp_iphc != NULL) { 7653 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7654 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7655 } 7656 7657 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7658 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7659 DONTCARE(tcp->tcp_ipha); 7660 DONTCARE(tcp->tcp_ip6h); 7661 DONTCARE(tcp->tcp_ip_hdr_len); 7662 DONTCARE(tcp->tcp_tcph); 7663 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7664 tcp->tcp_valid_bits = 0; 7665 7666 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7667 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7668 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7669 tcp->tcp_last_rcv_lbolt = 0; 7670 7671 tcp->tcp_init_cwnd = 0; 7672 7673 tcp->tcp_urp_last_valid = 0; 7674 tcp->tcp_hard_binding = 0; 7675 tcp->tcp_hard_bound = 0; 7676 PRESERVE(tcp->tcp_cred); 7677 PRESERVE(tcp->tcp_cpid); 7678 PRESERVE(tcp->tcp_exclbind); 7679 7680 tcp->tcp_fin_acked = 0; 7681 tcp->tcp_fin_rcvd = 0; 7682 tcp->tcp_fin_sent = 0; 7683 tcp->tcp_ordrel_done = 0; 7684 7685 tcp->tcp_debug = 0; 7686 tcp->tcp_dontroute = 0; 7687 tcp->tcp_broadcast = 0; 7688 7689 tcp->tcp_useloopback = 0; 7690 tcp->tcp_reuseaddr = 0; 7691 tcp->tcp_oobinline = 0; 7692 tcp->tcp_dgram_errind = 0; 7693 7694 tcp->tcp_detached = 0; 7695 tcp->tcp_bind_pending = 0; 7696 tcp->tcp_unbind_pending = 0; 7697 tcp->tcp_deferred_clean_death = 0; 7698 7699 tcp->tcp_snd_ws_ok = B_FALSE; 7700 tcp->tcp_snd_ts_ok = B_FALSE; 7701 tcp->tcp_linger = 0; 7702 tcp->tcp_ka_enabled = 0; 7703 tcp->tcp_zero_win_probe = 0; 7704 7705 tcp->tcp_loopback = 0; 7706 tcp->tcp_localnet = 0; 7707 tcp->tcp_syn_defense = 0; 7708 tcp->tcp_set_timer = 0; 7709 7710 tcp->tcp_active_open = 0; 7711 ASSERT(tcp->tcp_timeout == B_FALSE); 7712 tcp->tcp_rexmit = B_FALSE; 7713 tcp->tcp_xmit_zc_clean = B_FALSE; 7714 7715 tcp->tcp_snd_sack_ok = B_FALSE; 7716 PRESERVE(tcp->tcp_recvdstaddr); 7717 tcp->tcp_hwcksum = B_FALSE; 7718 7719 tcp->tcp_ire_ill_check_done = B_FALSE; 7720 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7721 7722 tcp->tcp_mdt = B_FALSE; 7723 tcp->tcp_mdt_hdr_head = 0; 7724 tcp->tcp_mdt_hdr_tail = 0; 7725 7726 tcp->tcp_conn_def_q0 = 0; 7727 tcp->tcp_ip_forward_progress = B_FALSE; 7728 tcp->tcp_anon_priv_bind = 0; 7729 tcp->tcp_ecn_ok = B_FALSE; 7730 7731 tcp->tcp_cwr = B_FALSE; 7732 tcp->tcp_ecn_echo_on = B_FALSE; 7733 7734 if (tcp->tcp_sack_info != NULL) { 7735 if (tcp->tcp_notsack_list != NULL) { 7736 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7737 } 7738 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7739 tcp->tcp_sack_info = NULL; 7740 } 7741 7742 tcp->tcp_rcv_ws = 0; 7743 tcp->tcp_snd_ws = 0; 7744 tcp->tcp_ts_recent = 0; 7745 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7746 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7747 tcp->tcp_if_mtu = 0; 7748 7749 ASSERT(tcp->tcp_reass_head == NULL); 7750 ASSERT(tcp->tcp_reass_tail == NULL); 7751 7752 tcp->tcp_cwnd_cnt = 0; 7753 7754 ASSERT(tcp->tcp_rcv_list == NULL); 7755 ASSERT(tcp->tcp_rcv_last_head == NULL); 7756 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7757 ASSERT(tcp->tcp_rcv_cnt == 0); 7758 7759 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7760 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7761 tcp->tcp_csuna = 0; 7762 7763 tcp->tcp_rto = 0; /* Displayed in MIB */ 7764 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7765 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7766 tcp->tcp_rtt_update = 0; 7767 7768 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7769 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7770 7771 tcp->tcp_rack = 0; /* Displayed in mib */ 7772 tcp->tcp_rack_cnt = 0; 7773 tcp->tcp_rack_cur_max = 0; 7774 tcp->tcp_rack_abs_max = 0; 7775 7776 tcp->tcp_max_swnd = 0; 7777 7778 ASSERT(tcp->tcp_listener == NULL); 7779 7780 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7781 7782 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7783 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7784 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7785 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7786 7787 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7788 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7789 PRESERVE(tcp->tcp_conn_req_max); 7790 PRESERVE(tcp->tcp_conn_req_seqnum); 7791 7792 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7793 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7794 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7795 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7796 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7797 7798 tcp->tcp_lingertime = 0; 7799 7800 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7801 ASSERT(tcp->tcp_urp_mp == NULL); 7802 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7803 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7804 7805 ASSERT(tcp->tcp_eager_next_q == NULL); 7806 ASSERT(tcp->tcp_eager_last_q == NULL); 7807 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7808 tcp->tcp_eager_prev_q0 == NULL) || 7809 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7810 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7811 7812 tcp->tcp_client_errno = 0; 7813 7814 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7815 7816 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7817 7818 PRESERVE(tcp->tcp_bound_source_v6); 7819 tcp->tcp_last_sent_len = 0; 7820 tcp->tcp_dupack_cnt = 0; 7821 7822 tcp->tcp_fport = 0; /* Displayed in MIB */ 7823 PRESERVE(tcp->tcp_lport); 7824 7825 PRESERVE(tcp->tcp_acceptor_lockp); 7826 7827 ASSERT(tcp->tcp_ordrelid == 0); 7828 PRESERVE(tcp->tcp_acceptor_id); 7829 DONTCARE(tcp->tcp_ipsec_overhead); 7830 7831 /* 7832 * If tcp_tracing flag is ON (i.e. We have a trace buffer 7833 * in tcp structure and now tracing), Re-initialize all 7834 * members of tcp_traceinfo. 7835 */ 7836 if (tcp->tcp_tracebuf != NULL) { 7837 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 7838 } 7839 7840 PRESERVE(tcp->tcp_family); 7841 if (tcp->tcp_family == AF_INET6) { 7842 tcp->tcp_ipversion = IPV6_VERSION; 7843 tcp->tcp_mss = tcp_mss_def_ipv6; 7844 } else { 7845 tcp->tcp_ipversion = IPV4_VERSION; 7846 tcp->tcp_mss = tcp_mss_def_ipv4; 7847 } 7848 7849 tcp->tcp_bound_if = 0; 7850 tcp->tcp_ipv6_recvancillary = 0; 7851 tcp->tcp_recvifindex = 0; 7852 tcp->tcp_recvhops = 0; 7853 tcp->tcp_closed = 0; 7854 tcp->tcp_cleandeathtag = 0; 7855 if (tcp->tcp_hopopts != NULL) { 7856 mi_free(tcp->tcp_hopopts); 7857 tcp->tcp_hopopts = NULL; 7858 tcp->tcp_hopoptslen = 0; 7859 } 7860 ASSERT(tcp->tcp_hopoptslen == 0); 7861 if (tcp->tcp_dstopts != NULL) { 7862 mi_free(tcp->tcp_dstopts); 7863 tcp->tcp_dstopts = NULL; 7864 tcp->tcp_dstoptslen = 0; 7865 } 7866 ASSERT(tcp->tcp_dstoptslen == 0); 7867 if (tcp->tcp_rtdstopts != NULL) { 7868 mi_free(tcp->tcp_rtdstopts); 7869 tcp->tcp_rtdstopts = NULL; 7870 tcp->tcp_rtdstoptslen = 0; 7871 } 7872 ASSERT(tcp->tcp_rtdstoptslen == 0); 7873 if (tcp->tcp_rthdr != NULL) { 7874 mi_free(tcp->tcp_rthdr); 7875 tcp->tcp_rthdr = NULL; 7876 tcp->tcp_rthdrlen = 0; 7877 } 7878 ASSERT(tcp->tcp_rthdrlen == 0); 7879 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7880 7881 /* Reset fusion-related fields */ 7882 tcp->tcp_fused = B_FALSE; 7883 tcp->tcp_unfusable = B_FALSE; 7884 tcp->tcp_fused_sigurg = B_FALSE; 7885 tcp->tcp_direct_sockfs = B_FALSE; 7886 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7887 tcp->tcp_loopback_peer = NULL; 7888 tcp->tcp_fuse_rcv_hiwater = 0; 7889 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7890 tcp->tcp_fuse_rcv_unread_cnt = 0; 7891 7892 tcp->tcp_in_ack_unsent = 0; 7893 tcp->tcp_cork = B_FALSE; 7894 7895 PRESERVE(tcp->tcp_squeue_bytes); 7896 7897 ASSERT(tcp->tcp_kssl_ctx == NULL); 7898 ASSERT(!tcp->tcp_kssl_pending); 7899 PRESERVE(tcp->tcp_kssl_ent); 7900 7901 #undef DONTCARE 7902 #undef PRESERVE 7903 } 7904 7905 /* 7906 * Allocate necessary resources and initialize state vector. 7907 * Guaranteed not to fail so that when an error is returned, 7908 * the caller doesn't need to do any additional cleanup. 7909 */ 7910 int 7911 tcp_init(tcp_t *tcp, queue_t *q) 7912 { 7913 int err; 7914 7915 tcp->tcp_rq = q; 7916 tcp->tcp_wq = WR(q); 7917 tcp->tcp_state = TCPS_IDLE; 7918 if ((err = tcp_init_values(tcp)) != 0) 7919 tcp_timers_stop(tcp); 7920 return (err); 7921 } 7922 7923 static int 7924 tcp_init_values(tcp_t *tcp) 7925 { 7926 int err; 7927 7928 ASSERT((tcp->tcp_family == AF_INET && 7929 tcp->tcp_ipversion == IPV4_VERSION) || 7930 (tcp->tcp_family == AF_INET6 && 7931 (tcp->tcp_ipversion == IPV4_VERSION || 7932 tcp->tcp_ipversion == IPV6_VERSION))); 7933 7934 /* 7935 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7936 * will be close to tcp_rexmit_interval_initial. By doing this, we 7937 * allow the algorithm to adjust slowly to large fluctuations of RTT 7938 * during first few transmissions of a connection as seen in slow 7939 * links. 7940 */ 7941 tcp->tcp_rtt_sa = tcp_rexmit_interval_initial << 2; 7942 tcp->tcp_rtt_sd = tcp_rexmit_interval_initial >> 1; 7943 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7944 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7945 tcp_conn_grace_period; 7946 if (tcp->tcp_rto < tcp_rexmit_interval_min) 7947 tcp->tcp_rto = tcp_rexmit_interval_min; 7948 tcp->tcp_timer_backoff = 0; 7949 tcp->tcp_ms_we_have_waited = 0; 7950 tcp->tcp_last_recv_time = lbolt; 7951 tcp->tcp_cwnd_max = tcp_cwnd_max_; 7952 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7953 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7954 7955 tcp->tcp_maxpsz = tcp_maxpsz_multiplier; 7956 7957 tcp->tcp_first_timer_threshold = tcp_ip_notify_interval; 7958 tcp->tcp_first_ctimer_threshold = tcp_ip_notify_cinterval; 7959 tcp->tcp_second_timer_threshold = tcp_ip_abort_interval; 7960 /* 7961 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7962 * passive open. 7963 */ 7964 tcp->tcp_second_ctimer_threshold = tcp_ip_abort_cinterval; 7965 7966 tcp->tcp_naglim = tcp_naglim_def; 7967 7968 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7969 7970 tcp->tcp_mdt_hdr_head = 0; 7971 tcp->tcp_mdt_hdr_tail = 0; 7972 7973 /* Reset fusion-related fields */ 7974 tcp->tcp_fused = B_FALSE; 7975 tcp->tcp_unfusable = B_FALSE; 7976 tcp->tcp_fused_sigurg = B_FALSE; 7977 tcp->tcp_direct_sockfs = B_FALSE; 7978 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7979 tcp->tcp_loopback_peer = NULL; 7980 tcp->tcp_fuse_rcv_hiwater = 0; 7981 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7982 tcp->tcp_fuse_rcv_unread_cnt = 0; 7983 7984 /* Initialize the header template */ 7985 if (tcp->tcp_ipversion == IPV4_VERSION) { 7986 err = tcp_header_init_ipv4(tcp); 7987 } else { 7988 err = tcp_header_init_ipv6(tcp); 7989 } 7990 if (err) 7991 return (err); 7992 7993 /* 7994 * Init the window scale to the max so tcp_rwnd_set() won't pare 7995 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7996 */ 7997 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 7998 tcp->tcp_xmit_lowater = tcp_xmit_lowat; 7999 tcp->tcp_xmit_hiwater = tcp_xmit_hiwat; 8000 8001 tcp->tcp_cork = B_FALSE; 8002 /* 8003 * Init the tcp_debug option. This value determines whether TCP 8004 * calls strlog() to print out debug messages. Doing this 8005 * initialization here means that this value is not inherited thru 8006 * tcp_reinit(). 8007 */ 8008 tcp->tcp_debug = tcp_dbg; 8009 8010 tcp->tcp_ka_interval = tcp_keepalive_interval; 8011 tcp->tcp_ka_abort_thres = tcp_keepalive_abort_interval; 8012 8013 return (0); 8014 } 8015 8016 /* 8017 * Initialize the IPv4 header. Loses any record of any IP options. 8018 */ 8019 static int 8020 tcp_header_init_ipv4(tcp_t *tcp) 8021 { 8022 tcph_t *tcph; 8023 uint32_t sum; 8024 conn_t *connp; 8025 8026 /* 8027 * This is a simple initialization. If there's 8028 * already a template, it should never be too small, 8029 * so reuse it. Otherwise, allocate space for the new one. 8030 */ 8031 if (tcp->tcp_iphc == NULL) { 8032 ASSERT(tcp->tcp_iphc_len == 0); 8033 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8034 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8035 if (tcp->tcp_iphc == NULL) { 8036 tcp->tcp_iphc_len = 0; 8037 return (ENOMEM); 8038 } 8039 } 8040 8041 /* options are gone; may need a new label */ 8042 connp = tcp->tcp_connp; 8043 connp->conn_mlp_type = mlptSingle; 8044 connp->conn_ulp_labeled = !is_system_labeled(); 8045 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8046 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 8047 tcp->tcp_ip6h = NULL; 8048 tcp->tcp_ipversion = IPV4_VERSION; 8049 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 8050 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8051 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 8052 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 8053 tcp->tcp_ipha->ipha_version_and_hdr_length 8054 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 8055 tcp->tcp_ipha->ipha_ident = 0; 8056 8057 tcp->tcp_ttl = (uchar_t)tcp_ipv4_ttl; 8058 tcp->tcp_tos = 0; 8059 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 8060 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 8061 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 8062 8063 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 8064 tcp->tcp_tcph = tcph; 8065 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8066 /* 8067 * IP wants our header length in the checksum field to 8068 * allow it to perform a single pseudo-header+checksum 8069 * calculation on behalf of TCP. 8070 * Include the adjustment for a source route once IP_OPTIONS is set. 8071 */ 8072 sum = sizeof (tcph_t) + tcp->tcp_sum; 8073 sum = (sum >> 16) + (sum & 0xFFFF); 8074 U16_TO_ABE16(sum, tcph->th_sum); 8075 return (0); 8076 } 8077 8078 /* 8079 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 8080 */ 8081 static int 8082 tcp_header_init_ipv6(tcp_t *tcp) 8083 { 8084 tcph_t *tcph; 8085 uint32_t sum; 8086 conn_t *connp; 8087 8088 /* 8089 * This is a simple initialization. If there's 8090 * already a template, it should never be too small, 8091 * so reuse it. Otherwise, allocate space for the new one. 8092 * Ensure that there is enough space to "downgrade" the tcp_t 8093 * to an IPv4 tcp_t. This requires having space for a full load 8094 * of IPv4 options, as well as a full load of TCP options 8095 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 8096 * than a v6 header and a TCP header with a full load of TCP options 8097 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 8098 * We want to avoid reallocation in the "downgraded" case when 8099 * processing outbound IPv4 options. 8100 */ 8101 if (tcp->tcp_iphc == NULL) { 8102 ASSERT(tcp->tcp_iphc_len == 0); 8103 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 8104 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 8105 if (tcp->tcp_iphc == NULL) { 8106 tcp->tcp_iphc_len = 0; 8107 return (ENOMEM); 8108 } 8109 } 8110 8111 /* options are gone; may need a new label */ 8112 connp = tcp->tcp_connp; 8113 connp->conn_mlp_type = mlptSingle; 8114 connp->conn_ulp_labeled = !is_system_labeled(); 8115 8116 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 8117 tcp->tcp_ipversion = IPV6_VERSION; 8118 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 8119 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 8120 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 8121 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 8122 tcp->tcp_ipha = NULL; 8123 8124 /* Initialize the header template */ 8125 8126 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 8127 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 8128 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 8129 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcp_ipv6_hoplimit; 8130 8131 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 8132 tcp->tcp_tcph = tcph; 8133 tcph->th_offset_and_rsrvd[0] = (5 << 4); 8134 /* 8135 * IP wants our header length in the checksum field to 8136 * allow it to perform a single psuedo-header+checksum 8137 * calculation on behalf of TCP. 8138 * Include the adjustment for a source route when IPV6_RTHDR is set. 8139 */ 8140 sum = sizeof (tcph_t) + tcp->tcp_sum; 8141 sum = (sum >> 16) + (sum & 0xFFFF); 8142 U16_TO_ABE16(sum, tcph->th_sum); 8143 return (0); 8144 } 8145 8146 /* At minimum we need 4 bytes in the TCP header for the lookup */ 8147 #define ICMP_MIN_TCP_HDR 12 8148 8149 /* 8150 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 8151 * passed up by IP. The message is always received on the correct tcp_t. 8152 * Assumes that IP has pulled up everything up to and including the ICMP header. 8153 */ 8154 void 8155 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 8156 { 8157 icmph_t *icmph; 8158 ipha_t *ipha; 8159 int iph_hdr_length; 8160 tcph_t *tcph; 8161 boolean_t ipsec_mctl = B_FALSE; 8162 boolean_t secure; 8163 mblk_t *first_mp = mp; 8164 uint32_t new_mss; 8165 uint32_t ratio; 8166 size_t mp_size = MBLKL(mp); 8167 uint32_t seg_ack; 8168 uint32_t seg_seq; 8169 8170 /* Assume IP provides aligned packets - otherwise toss */ 8171 if (!OK_32PTR(mp->b_rptr)) { 8172 freemsg(mp); 8173 return; 8174 } 8175 8176 /* 8177 * Since ICMP errors are normal data marked with M_CTL when sent 8178 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 8179 * packets starting with an ipsec_info_t, see ipsec_info.h. 8180 */ 8181 if ((mp_size == sizeof (ipsec_info_t)) && 8182 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 8183 ASSERT(mp->b_cont != NULL); 8184 mp = mp->b_cont; 8185 /* IP should have done this */ 8186 ASSERT(OK_32PTR(mp->b_rptr)); 8187 mp_size = MBLKL(mp); 8188 ipsec_mctl = B_TRUE; 8189 } 8190 8191 /* 8192 * Verify that we have a complete outer IP header. If not, drop it. 8193 */ 8194 if (mp_size < sizeof (ipha_t)) { 8195 noticmpv4: 8196 freemsg(first_mp); 8197 return; 8198 } 8199 8200 ipha = (ipha_t *)mp->b_rptr; 8201 /* 8202 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 8203 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 8204 */ 8205 switch (IPH_HDR_VERSION(ipha)) { 8206 case IPV6_VERSION: 8207 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 8208 return; 8209 case IPV4_VERSION: 8210 break; 8211 default: 8212 goto noticmpv4; 8213 } 8214 8215 /* Skip past the outer IP and ICMP headers */ 8216 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8217 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 8218 /* 8219 * If we don't have the correct outer IP header length or if the ULP 8220 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 8221 * send it upstream. 8222 */ 8223 if (iph_hdr_length < sizeof (ipha_t) || 8224 ipha->ipha_protocol != IPPROTO_ICMP || 8225 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 8226 goto noticmpv4; 8227 } 8228 ipha = (ipha_t *)&icmph[1]; 8229 8230 /* Skip past the inner IP and find the ULP header */ 8231 iph_hdr_length = IPH_HDR_LENGTH(ipha); 8232 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 8233 /* 8234 * If we don't have the correct inner IP header length or if the ULP 8235 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8236 * bytes of TCP header, drop it. 8237 */ 8238 if (iph_hdr_length < sizeof (ipha_t) || 8239 ipha->ipha_protocol != IPPROTO_TCP || 8240 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8241 goto noticmpv4; 8242 } 8243 8244 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8245 if (ipsec_mctl) { 8246 secure = ipsec_in_is_secure(first_mp); 8247 } else { 8248 secure = B_FALSE; 8249 } 8250 if (secure) { 8251 /* 8252 * If we are willing to accept this in clear 8253 * we don't have to verify policy. 8254 */ 8255 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8256 if (!tcp_check_policy(tcp, first_mp, 8257 ipha, NULL, secure, ipsec_mctl)) { 8258 /* 8259 * tcp_check_policy called 8260 * ip_drop_packet() on failure. 8261 */ 8262 return; 8263 } 8264 } 8265 } 8266 } else if (ipsec_mctl) { 8267 /* 8268 * This is a hard_bound connection. IP has already 8269 * verified policy. We don't have to do it again. 8270 */ 8271 freeb(first_mp); 8272 first_mp = mp; 8273 ipsec_mctl = B_FALSE; 8274 } 8275 8276 seg_ack = ABE32_TO_U32(tcph->th_ack); 8277 seg_seq = ABE32_TO_U32(tcph->th_seq); 8278 /* 8279 * TCP SHOULD check that the TCP sequence number contained in 8280 * payload of the ICMP error message is within the range 8281 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8282 */ 8283 if (SEQ_LT(seg_seq, tcp->tcp_suna) || 8284 SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8285 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8286 /* 8287 * If the ICMP message is bogus, should we kill the 8288 * connection, or should we just drop the bogus ICMP 8289 * message? It would probably make more sense to just 8290 * drop the message so that if this one managed to get 8291 * in, the real connection should not suffer. 8292 */ 8293 goto noticmpv4; 8294 } 8295 8296 switch (icmph->icmph_type) { 8297 case ICMP_DEST_UNREACHABLE: 8298 switch (icmph->icmph_code) { 8299 case ICMP_FRAGMENTATION_NEEDED: 8300 /* 8301 * Reduce the MSS based on the new MTU. This will 8302 * eliminate any fragmentation locally. 8303 * N.B. There may well be some funny side-effects on 8304 * the local send policy and the remote receive policy. 8305 * Pending further research, we provide 8306 * tcp_ignore_path_mtu just in case this proves 8307 * disastrous somewhere. 8308 * 8309 * After updating the MSS, retransmit part of the 8310 * dropped segment using the new mss by calling 8311 * tcp_wput_data(). Need to adjust all those 8312 * params to make sure tcp_wput_data() work properly. 8313 */ 8314 if (tcp_ignore_path_mtu) 8315 break; 8316 8317 /* 8318 * Decrease the MSS by time stamp options 8319 * IP options and IPSEC options. tcp_hdr_len 8320 * includes time stamp option and IP option 8321 * length. 8322 */ 8323 8324 new_mss = ntohs(icmph->icmph_du_mtu) - 8325 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8326 8327 /* 8328 * Only update the MSS if the new one is 8329 * smaller than the previous one. This is 8330 * to avoid problems when getting multiple 8331 * ICMP errors for the same MTU. 8332 */ 8333 if (new_mss >= tcp->tcp_mss) 8334 break; 8335 8336 /* 8337 * Stop doing PMTU if new_mss is less than 68 8338 * or less than tcp_mss_min. 8339 * The value 68 comes from rfc 1191. 8340 */ 8341 if (new_mss < MAX(68, tcp_mss_min)) 8342 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8343 0; 8344 8345 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8346 ASSERT(ratio >= 1); 8347 tcp_mss_set(tcp, new_mss); 8348 8349 /* 8350 * Make sure we have something to 8351 * send. 8352 */ 8353 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8354 (tcp->tcp_xmit_head != NULL)) { 8355 /* 8356 * Shrink tcp_cwnd in 8357 * proportion to the old MSS/new MSS. 8358 */ 8359 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8360 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8361 (tcp->tcp_unsent == 0)) { 8362 tcp->tcp_rexmit_max = tcp->tcp_fss; 8363 } else { 8364 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8365 } 8366 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8367 tcp->tcp_rexmit = B_TRUE; 8368 tcp->tcp_dupack_cnt = 0; 8369 tcp->tcp_snd_burst = TCP_CWND_SS; 8370 tcp_ss_rexmit(tcp); 8371 } 8372 break; 8373 case ICMP_PORT_UNREACHABLE: 8374 case ICMP_PROTOCOL_UNREACHABLE: 8375 switch (tcp->tcp_state) { 8376 case TCPS_SYN_SENT: 8377 case TCPS_SYN_RCVD: 8378 /* 8379 * ICMP can snipe away incipient 8380 * TCP connections as long as 8381 * seq number is same as initial 8382 * send seq number. 8383 */ 8384 if (seg_seq == tcp->tcp_iss) { 8385 (void) tcp_clean_death(tcp, 8386 ECONNREFUSED, 6); 8387 } 8388 break; 8389 } 8390 break; 8391 case ICMP_HOST_UNREACHABLE: 8392 case ICMP_NET_UNREACHABLE: 8393 /* Record the error in case we finally time out. */ 8394 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8395 tcp->tcp_client_errno = EHOSTUNREACH; 8396 else 8397 tcp->tcp_client_errno = ENETUNREACH; 8398 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8399 if (tcp->tcp_listener != NULL && 8400 tcp->tcp_listener->tcp_syn_defense) { 8401 /* 8402 * Ditch the half-open connection if we 8403 * suspect a SYN attack is under way. 8404 */ 8405 tcp_ip_ire_mark_advice(tcp); 8406 (void) tcp_clean_death(tcp, 8407 tcp->tcp_client_errno, 7); 8408 } 8409 } 8410 break; 8411 default: 8412 break; 8413 } 8414 break; 8415 case ICMP_SOURCE_QUENCH: { 8416 /* 8417 * use a global boolean to control 8418 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8419 * The default is false. 8420 */ 8421 if (tcp_icmp_source_quench) { 8422 /* 8423 * Reduce the sending rate as if we got a 8424 * retransmit timeout 8425 */ 8426 uint32_t npkt; 8427 8428 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8429 tcp->tcp_mss; 8430 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8431 tcp->tcp_cwnd = tcp->tcp_mss; 8432 tcp->tcp_cwnd_cnt = 0; 8433 } 8434 break; 8435 } 8436 } 8437 freemsg(first_mp); 8438 } 8439 8440 /* 8441 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8442 * error messages passed up by IP. 8443 * Assumes that IP has pulled up all the extension headers as well 8444 * as the ICMPv6 header. 8445 */ 8446 static void 8447 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8448 { 8449 icmp6_t *icmp6; 8450 ip6_t *ip6h; 8451 uint16_t iph_hdr_length; 8452 tcpha_t *tcpha; 8453 uint8_t *nexthdrp; 8454 uint32_t new_mss; 8455 uint32_t ratio; 8456 boolean_t secure; 8457 mblk_t *first_mp = mp; 8458 size_t mp_size; 8459 uint32_t seg_ack; 8460 uint32_t seg_seq; 8461 8462 /* 8463 * The caller has determined if this is an IPSEC_IN packet and 8464 * set ipsec_mctl appropriately (see tcp_icmp_error). 8465 */ 8466 if (ipsec_mctl) 8467 mp = mp->b_cont; 8468 8469 mp_size = MBLKL(mp); 8470 8471 /* 8472 * Verify that we have a complete IP header. If not, send it upstream. 8473 */ 8474 if (mp_size < sizeof (ip6_t)) { 8475 noticmpv6: 8476 freemsg(first_mp); 8477 return; 8478 } 8479 8480 /* 8481 * Verify this is an ICMPV6 packet, else send it upstream. 8482 */ 8483 ip6h = (ip6_t *)mp->b_rptr; 8484 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8485 iph_hdr_length = IPV6_HDR_LEN; 8486 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8487 &nexthdrp) || 8488 *nexthdrp != IPPROTO_ICMPV6) { 8489 goto noticmpv6; 8490 } 8491 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8492 ip6h = (ip6_t *)&icmp6[1]; 8493 /* 8494 * Verify if we have a complete ICMP and inner IP header. 8495 */ 8496 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8497 goto noticmpv6; 8498 8499 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8500 goto noticmpv6; 8501 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8502 /* 8503 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8504 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8505 * packet. 8506 */ 8507 if ((*nexthdrp != IPPROTO_TCP) || 8508 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8509 goto noticmpv6; 8510 } 8511 8512 /* 8513 * ICMP errors come on the right queue or come on 8514 * listener/global queue for detached connections and 8515 * get switched to the right queue. If it comes on the 8516 * right queue, policy check has already been done by IP 8517 * and thus free the first_mp without verifying the policy. 8518 * If it has come for a non-hard bound connection, we need 8519 * to verify policy as IP may not have done it. 8520 */ 8521 if (!tcp->tcp_hard_bound) { 8522 if (ipsec_mctl) { 8523 secure = ipsec_in_is_secure(first_mp); 8524 } else { 8525 secure = B_FALSE; 8526 } 8527 if (secure) { 8528 /* 8529 * If we are willing to accept this in clear 8530 * we don't have to verify policy. 8531 */ 8532 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8533 if (!tcp_check_policy(tcp, first_mp, 8534 NULL, ip6h, secure, ipsec_mctl)) { 8535 /* 8536 * tcp_check_policy called 8537 * ip_drop_packet() on failure. 8538 */ 8539 return; 8540 } 8541 } 8542 } 8543 } else if (ipsec_mctl) { 8544 /* 8545 * This is a hard_bound connection. IP has already 8546 * verified policy. We don't have to do it again. 8547 */ 8548 freeb(first_mp); 8549 first_mp = mp; 8550 ipsec_mctl = B_FALSE; 8551 } 8552 8553 seg_ack = ntohl(tcpha->tha_ack); 8554 seg_seq = ntohl(tcpha->tha_seq); 8555 /* 8556 * TCP SHOULD check that the TCP sequence number contained in 8557 * payload of the ICMP error message is within the range 8558 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8559 */ 8560 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8561 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8562 /* 8563 * If the ICMP message is bogus, should we kill the 8564 * connection, or should we just drop the bogus ICMP 8565 * message? It would probably make more sense to just 8566 * drop the message so that if this one managed to get 8567 * in, the real connection should not suffer. 8568 */ 8569 goto noticmpv6; 8570 } 8571 8572 switch (icmp6->icmp6_type) { 8573 case ICMP6_PACKET_TOO_BIG: 8574 /* 8575 * Reduce the MSS based on the new MTU. This will 8576 * eliminate any fragmentation locally. 8577 * N.B. There may well be some funny side-effects on 8578 * the local send policy and the remote receive policy. 8579 * Pending further research, we provide 8580 * tcp_ignore_path_mtu just in case this proves 8581 * disastrous somewhere. 8582 * 8583 * After updating the MSS, retransmit part of the 8584 * dropped segment using the new mss by calling 8585 * tcp_wput_data(). Need to adjust all those 8586 * params to make sure tcp_wput_data() work properly. 8587 */ 8588 if (tcp_ignore_path_mtu) 8589 break; 8590 8591 /* 8592 * Decrease the MSS by time stamp options 8593 * IP options and IPSEC options. tcp_hdr_len 8594 * includes time stamp option and IP option 8595 * length. 8596 */ 8597 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8598 tcp->tcp_ipsec_overhead; 8599 8600 /* 8601 * Only update the MSS if the new one is 8602 * smaller than the previous one. This is 8603 * to avoid problems when getting multiple 8604 * ICMP errors for the same MTU. 8605 */ 8606 if (new_mss >= tcp->tcp_mss) 8607 break; 8608 8609 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8610 ASSERT(ratio >= 1); 8611 tcp_mss_set(tcp, new_mss); 8612 8613 /* 8614 * Make sure we have something to 8615 * send. 8616 */ 8617 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8618 (tcp->tcp_xmit_head != NULL)) { 8619 /* 8620 * Shrink tcp_cwnd in 8621 * proportion to the old MSS/new MSS. 8622 */ 8623 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8624 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8625 (tcp->tcp_unsent == 0)) { 8626 tcp->tcp_rexmit_max = tcp->tcp_fss; 8627 } else { 8628 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8629 } 8630 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8631 tcp->tcp_rexmit = B_TRUE; 8632 tcp->tcp_dupack_cnt = 0; 8633 tcp->tcp_snd_burst = TCP_CWND_SS; 8634 tcp_ss_rexmit(tcp); 8635 } 8636 break; 8637 8638 case ICMP6_DST_UNREACH: 8639 switch (icmp6->icmp6_code) { 8640 case ICMP6_DST_UNREACH_NOPORT: 8641 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8642 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8643 (seg_seq == tcp->tcp_iss)) { 8644 (void) tcp_clean_death(tcp, 8645 ECONNREFUSED, 8); 8646 } 8647 break; 8648 8649 case ICMP6_DST_UNREACH_ADMIN: 8650 case ICMP6_DST_UNREACH_NOROUTE: 8651 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8652 case ICMP6_DST_UNREACH_ADDR: 8653 /* Record the error in case we finally time out. */ 8654 tcp->tcp_client_errno = EHOSTUNREACH; 8655 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8656 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8657 (seg_seq == tcp->tcp_iss)) { 8658 if (tcp->tcp_listener != NULL && 8659 tcp->tcp_listener->tcp_syn_defense) { 8660 /* 8661 * Ditch the half-open connection if we 8662 * suspect a SYN attack is under way. 8663 */ 8664 tcp_ip_ire_mark_advice(tcp); 8665 (void) tcp_clean_death(tcp, 8666 tcp->tcp_client_errno, 9); 8667 } 8668 } 8669 8670 8671 break; 8672 default: 8673 break; 8674 } 8675 break; 8676 8677 case ICMP6_PARAM_PROB: 8678 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8679 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8680 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8681 (uchar_t *)nexthdrp) { 8682 if (tcp->tcp_state == TCPS_SYN_SENT || 8683 tcp->tcp_state == TCPS_SYN_RCVD) { 8684 (void) tcp_clean_death(tcp, 8685 ECONNREFUSED, 10); 8686 } 8687 break; 8688 } 8689 break; 8690 8691 case ICMP6_TIME_EXCEEDED: 8692 default: 8693 break; 8694 } 8695 freemsg(first_mp); 8696 } 8697 8698 /* 8699 * IP recognizes seven kinds of bind requests: 8700 * 8701 * - A zero-length address binds only to the protocol number. 8702 * 8703 * - A 4-byte address is treated as a request to 8704 * validate that the address is a valid local IPv4 8705 * address, appropriate for an application to bind to. 8706 * IP does the verification, but does not make any note 8707 * of the address at this time. 8708 * 8709 * - A 16-byte address contains is treated as a request 8710 * to validate a local IPv6 address, as the 4-byte 8711 * address case above. 8712 * 8713 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8714 * use it for the inbound fanout of packets. 8715 * 8716 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8717 * use it for the inbound fanout of packets. 8718 * 8719 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8720 * information consisting of local and remote addresses 8721 * and ports. In this case, the addresses are both 8722 * validated as appropriate for this operation, and, if 8723 * so, the information is retained for use in the 8724 * inbound fanout. 8725 * 8726 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8727 * fanout information, like the 12-byte case above. 8728 * 8729 * IP will also fill in the IRE request mblk with information 8730 * regarding our peer. In all cases, we notify IP of our protocol 8731 * type by appending a single protocol byte to the bind request. 8732 */ 8733 static mblk_t * 8734 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 8735 { 8736 char *cp; 8737 mblk_t *mp; 8738 struct T_bind_req *tbr; 8739 ipa_conn_t *ac; 8740 ipa6_conn_t *ac6; 8741 sin_t *sin; 8742 sin6_t *sin6; 8743 8744 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 8745 ASSERT((tcp->tcp_family == AF_INET && 8746 tcp->tcp_ipversion == IPV4_VERSION) || 8747 (tcp->tcp_family == AF_INET6 && 8748 (tcp->tcp_ipversion == IPV4_VERSION || 8749 tcp->tcp_ipversion == IPV6_VERSION))); 8750 8751 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 8752 if (!mp) 8753 return (mp); 8754 mp->b_datap->db_type = M_PROTO; 8755 tbr = (struct T_bind_req *)mp->b_rptr; 8756 tbr->PRIM_type = bind_prim; 8757 tbr->ADDR_offset = sizeof (*tbr); 8758 tbr->CONIND_number = 0; 8759 tbr->ADDR_length = addr_length; 8760 cp = (char *)&tbr[1]; 8761 switch (addr_length) { 8762 case sizeof (ipa_conn_t): 8763 ASSERT(tcp->tcp_family == AF_INET); 8764 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 8765 8766 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 8767 if (mp->b_cont == NULL) { 8768 freemsg(mp); 8769 return (NULL); 8770 } 8771 mp->b_cont->b_wptr += sizeof (ire_t); 8772 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 8773 8774 /* cp known to be 32 bit aligned */ 8775 ac = (ipa_conn_t *)cp; 8776 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 8777 ac->ac_faddr = tcp->tcp_remote; 8778 ac->ac_fport = tcp->tcp_fport; 8779 ac->ac_lport = tcp->tcp_lport; 8780 tcp->tcp_hard_binding = 1; 8781 break; 8782 8783 case sizeof (ipa6_conn_t): 8784 ASSERT(tcp->tcp_family == AF_INET6); 8785 8786 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 8787 if (mp->b_cont == NULL) { 8788 freemsg(mp); 8789 return (NULL); 8790 } 8791 mp->b_cont->b_wptr += sizeof (ire_t); 8792 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 8793 8794 /* cp known to be 32 bit aligned */ 8795 ac6 = (ipa6_conn_t *)cp; 8796 if (tcp->tcp_ipversion == IPV4_VERSION) { 8797 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 8798 &ac6->ac6_laddr); 8799 } else { 8800 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 8801 } 8802 ac6->ac6_faddr = tcp->tcp_remote_v6; 8803 ac6->ac6_fport = tcp->tcp_fport; 8804 ac6->ac6_lport = tcp->tcp_lport; 8805 tcp->tcp_hard_binding = 1; 8806 break; 8807 8808 case sizeof (sin_t): 8809 /* 8810 * NOTE: IPV6_ADDR_LEN also has same size. 8811 * Use family to discriminate. 8812 */ 8813 if (tcp->tcp_family == AF_INET) { 8814 sin = (sin_t *)cp; 8815 8816 *sin = sin_null; 8817 sin->sin_family = AF_INET; 8818 sin->sin_addr.s_addr = tcp->tcp_bound_source; 8819 sin->sin_port = tcp->tcp_lport; 8820 break; 8821 } else { 8822 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 8823 } 8824 break; 8825 8826 case sizeof (sin6_t): 8827 ASSERT(tcp->tcp_family == AF_INET6); 8828 sin6 = (sin6_t *)cp; 8829 8830 *sin6 = sin6_null; 8831 sin6->sin6_family = AF_INET6; 8832 sin6->sin6_addr = tcp->tcp_bound_source_v6; 8833 sin6->sin6_port = tcp->tcp_lport; 8834 break; 8835 8836 case IP_ADDR_LEN: 8837 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 8838 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 8839 break; 8840 8841 } 8842 /* Add protocol number to end */ 8843 cp[addr_length] = (char)IPPROTO_TCP; 8844 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 8845 return (mp); 8846 } 8847 8848 /* 8849 * Notify IP that we are having trouble with this connection. IP should 8850 * blow the IRE away and start over. 8851 */ 8852 static void 8853 tcp_ip_notify(tcp_t *tcp) 8854 { 8855 struct iocblk *iocp; 8856 ipid_t *ipid; 8857 mblk_t *mp; 8858 8859 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8860 if (tcp->tcp_ipversion == IPV6_VERSION) 8861 return; 8862 8863 mp = mkiocb(IP_IOCTL); 8864 if (mp == NULL) 8865 return; 8866 8867 iocp = (struct iocblk *)mp->b_rptr; 8868 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8869 8870 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8871 if (!mp->b_cont) { 8872 freeb(mp); 8873 return; 8874 } 8875 8876 ipid = (ipid_t *)mp->b_cont->b_rptr; 8877 mp->b_cont->b_wptr += iocp->ioc_count; 8878 bzero(ipid, sizeof (*ipid)); 8879 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8880 ipid->ipid_ire_type = IRE_CACHE; 8881 ipid->ipid_addr_offset = sizeof (ipid_t); 8882 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8883 /* 8884 * Note: in the case of source routing we want to blow away the 8885 * route to the first source route hop. 8886 */ 8887 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8888 sizeof (tcp->tcp_ipha->ipha_dst)); 8889 8890 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8891 } 8892 8893 /* Unlink and return any mblk that looks like it contains an ire */ 8894 static mblk_t * 8895 tcp_ire_mp(mblk_t *mp) 8896 { 8897 mblk_t *prev_mp; 8898 8899 for (;;) { 8900 prev_mp = mp; 8901 mp = mp->b_cont; 8902 if (mp == NULL) 8903 break; 8904 switch (DB_TYPE(mp)) { 8905 case IRE_DB_TYPE: 8906 case IRE_DB_REQ_TYPE: 8907 if (prev_mp != NULL) 8908 prev_mp->b_cont = mp->b_cont; 8909 mp->b_cont = NULL; 8910 return (mp); 8911 default: 8912 break; 8913 } 8914 } 8915 return (mp); 8916 } 8917 8918 /* 8919 * Timer callback routine for keepalive probe. We do a fake resend of 8920 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8921 * check to see if we have heard anything from the other end for the last 8922 * RTO period. If we have, set the timer to expire for another 8923 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8924 * RTO << 1 and check again when it expires. Keep exponentially increasing 8925 * the timeout if we have not heard from the other side. If for more than 8926 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8927 * kill the connection unless the keepalive abort threshold is 0. In 8928 * that case, we will probe "forever." 8929 */ 8930 static void 8931 tcp_keepalive_killer(void *arg) 8932 { 8933 mblk_t *mp; 8934 conn_t *connp = (conn_t *)arg; 8935 tcp_t *tcp = connp->conn_tcp; 8936 int32_t firetime; 8937 int32_t idletime; 8938 int32_t ka_intrvl; 8939 8940 tcp->tcp_ka_tid = 0; 8941 8942 if (tcp->tcp_fused) 8943 return; 8944 8945 BUMP_MIB(&tcp_mib, tcpTimKeepalive); 8946 ka_intrvl = tcp->tcp_ka_interval; 8947 8948 /* 8949 * Keepalive probe should only be sent if the application has not 8950 * done a close on the connection. 8951 */ 8952 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8953 return; 8954 } 8955 /* Timer fired too early, restart it. */ 8956 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8957 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8958 MSEC_TO_TICK(ka_intrvl)); 8959 return; 8960 } 8961 8962 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8963 /* 8964 * If we have not heard from the other side for a long 8965 * time, kill the connection unless the keepalive abort 8966 * threshold is 0. In that case, we will probe "forever." 8967 */ 8968 if (tcp->tcp_ka_abort_thres != 0 && 8969 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8970 BUMP_MIB(&tcp_mib, tcpTimKeepaliveDrop); 8971 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8972 tcp->tcp_client_errno : ETIMEDOUT, 11); 8973 return; 8974 } 8975 8976 if (tcp->tcp_snxt == tcp->tcp_suna && 8977 idletime >= ka_intrvl) { 8978 /* Fake resend of last ACKed byte. */ 8979 mblk_t *mp1 = allocb(1, BPRI_LO); 8980 8981 if (mp1 != NULL) { 8982 *mp1->b_wptr++ = '\0'; 8983 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8984 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8985 freeb(mp1); 8986 /* 8987 * if allocation failed, fall through to start the 8988 * timer back. 8989 */ 8990 if (mp != NULL) { 8991 TCP_RECORD_TRACE(tcp, mp, 8992 TCP_TRACE_SEND_PKT); 8993 tcp_send_data(tcp, tcp->tcp_wq, mp); 8994 BUMP_MIB(&tcp_mib, tcpTimKeepaliveProbe); 8995 if (tcp->tcp_ka_last_intrvl != 0) { 8996 /* 8997 * We should probe again at least 8998 * in ka_intrvl, but not more than 8999 * tcp_rexmit_interval_max. 9000 */ 9001 firetime = MIN(ka_intrvl - 1, 9002 tcp->tcp_ka_last_intrvl << 1); 9003 if (firetime > tcp_rexmit_interval_max) 9004 firetime = 9005 tcp_rexmit_interval_max; 9006 } else { 9007 firetime = tcp->tcp_rto; 9008 } 9009 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9010 tcp_keepalive_killer, 9011 MSEC_TO_TICK(firetime)); 9012 tcp->tcp_ka_last_intrvl = firetime; 9013 return; 9014 } 9015 } 9016 } else { 9017 tcp->tcp_ka_last_intrvl = 0; 9018 } 9019 9020 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 9021 if ((firetime = ka_intrvl - idletime) < 0) { 9022 firetime = ka_intrvl; 9023 } 9024 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 9025 MSEC_TO_TICK(firetime)); 9026 } 9027 9028 int 9029 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 9030 { 9031 queue_t *q = tcp->tcp_rq; 9032 int32_t mss = tcp->tcp_mss; 9033 int maxpsz; 9034 9035 if (TCP_IS_DETACHED(tcp)) 9036 return (mss); 9037 9038 if (tcp->tcp_fused) { 9039 maxpsz = tcp_fuse_maxpsz_set(tcp); 9040 mss = INFPSZ; 9041 } else if (tcp->tcp_mdt || tcp->tcp_maxpsz == 0) { 9042 /* 9043 * Set the sd_qn_maxpsz according to the socket send buffer 9044 * size, and sd_maxblk to INFPSZ (-1). This will essentially 9045 * instruct the stream head to copyin user data into contiguous 9046 * kernel-allocated buffers without breaking it up into smaller 9047 * chunks. We round up the buffer size to the nearest SMSS. 9048 */ 9049 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 9050 if (tcp->tcp_kssl_ctx == NULL) 9051 mss = INFPSZ; 9052 else 9053 mss = SSL3_MAX_RECORD_LEN; 9054 } else { 9055 /* 9056 * Set sd_qn_maxpsz to approx half the (receivers) buffer 9057 * (and a multiple of the mss). This instructs the stream 9058 * head to break down larger than SMSS writes into SMSS- 9059 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 9060 */ 9061 maxpsz = tcp->tcp_maxpsz * mss; 9062 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 9063 maxpsz = tcp->tcp_xmit_hiwater/2; 9064 /* Round up to nearest mss */ 9065 maxpsz = MSS_ROUNDUP(maxpsz, mss); 9066 } 9067 } 9068 (void) setmaxps(q, maxpsz); 9069 tcp->tcp_wq->q_maxpsz = maxpsz; 9070 9071 if (set_maxblk) 9072 (void) mi_set_sth_maxblk(q, mss); 9073 9074 return (mss); 9075 } 9076 9077 /* 9078 * Extract option values from a tcp header. We put any found values into the 9079 * tcpopt struct and return a bitmask saying which options were found. 9080 */ 9081 static int 9082 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 9083 { 9084 uchar_t *endp; 9085 int len; 9086 uint32_t mss; 9087 uchar_t *up = (uchar_t *)tcph; 9088 int found = 0; 9089 int32_t sack_len; 9090 tcp_seq sack_begin, sack_end; 9091 tcp_t *tcp; 9092 9093 endp = up + TCP_HDR_LENGTH(tcph); 9094 up += TCP_MIN_HEADER_LENGTH; 9095 while (up < endp) { 9096 len = endp - up; 9097 switch (*up) { 9098 case TCPOPT_EOL: 9099 break; 9100 9101 case TCPOPT_NOP: 9102 up++; 9103 continue; 9104 9105 case TCPOPT_MAXSEG: 9106 if (len < TCPOPT_MAXSEG_LEN || 9107 up[1] != TCPOPT_MAXSEG_LEN) 9108 break; 9109 9110 mss = BE16_TO_U16(up+2); 9111 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 9112 tcpopt->tcp_opt_mss = mss; 9113 found |= TCP_OPT_MSS_PRESENT; 9114 9115 up += TCPOPT_MAXSEG_LEN; 9116 continue; 9117 9118 case TCPOPT_WSCALE: 9119 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 9120 break; 9121 9122 if (up[2] > TCP_MAX_WINSHIFT) 9123 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 9124 else 9125 tcpopt->tcp_opt_wscale = up[2]; 9126 found |= TCP_OPT_WSCALE_PRESENT; 9127 9128 up += TCPOPT_WS_LEN; 9129 continue; 9130 9131 case TCPOPT_SACK_PERMITTED: 9132 if (len < TCPOPT_SACK_OK_LEN || 9133 up[1] != TCPOPT_SACK_OK_LEN) 9134 break; 9135 found |= TCP_OPT_SACK_OK_PRESENT; 9136 up += TCPOPT_SACK_OK_LEN; 9137 continue; 9138 9139 case TCPOPT_SACK: 9140 if (len <= 2 || up[1] <= 2 || len < up[1]) 9141 break; 9142 9143 /* If TCP is not interested in SACK blks... */ 9144 if ((tcp = tcpopt->tcp) == NULL) { 9145 up += up[1]; 9146 continue; 9147 } 9148 sack_len = up[1] - TCPOPT_HEADER_LEN; 9149 up += TCPOPT_HEADER_LEN; 9150 9151 /* 9152 * If the list is empty, allocate one and assume 9153 * nothing is sack'ed. 9154 */ 9155 ASSERT(tcp->tcp_sack_info != NULL); 9156 if (tcp->tcp_notsack_list == NULL) { 9157 tcp_notsack_update(&(tcp->tcp_notsack_list), 9158 tcp->tcp_suna, tcp->tcp_snxt, 9159 &(tcp->tcp_num_notsack_blk), 9160 &(tcp->tcp_cnt_notsack_list)); 9161 9162 /* 9163 * Make sure tcp_notsack_list is not NULL. 9164 * This happens when kmem_alloc(KM_NOSLEEP) 9165 * returns NULL. 9166 */ 9167 if (tcp->tcp_notsack_list == NULL) { 9168 up += sack_len; 9169 continue; 9170 } 9171 tcp->tcp_fack = tcp->tcp_suna; 9172 } 9173 9174 while (sack_len > 0) { 9175 if (up + 8 > endp) { 9176 up = endp; 9177 break; 9178 } 9179 sack_begin = BE32_TO_U32(up); 9180 up += 4; 9181 sack_end = BE32_TO_U32(up); 9182 up += 4; 9183 sack_len -= 8; 9184 /* 9185 * Bounds checking. Make sure the SACK 9186 * info is within tcp_suna and tcp_snxt. 9187 * If this SACK blk is out of bound, ignore 9188 * it but continue to parse the following 9189 * blks. 9190 */ 9191 if (SEQ_LEQ(sack_end, sack_begin) || 9192 SEQ_LT(sack_begin, tcp->tcp_suna) || 9193 SEQ_GT(sack_end, tcp->tcp_snxt)) { 9194 continue; 9195 } 9196 tcp_notsack_insert(&(tcp->tcp_notsack_list), 9197 sack_begin, sack_end, 9198 &(tcp->tcp_num_notsack_blk), 9199 &(tcp->tcp_cnt_notsack_list)); 9200 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 9201 tcp->tcp_fack = sack_end; 9202 } 9203 } 9204 found |= TCP_OPT_SACK_PRESENT; 9205 continue; 9206 9207 case TCPOPT_TSTAMP: 9208 if (len < TCPOPT_TSTAMP_LEN || 9209 up[1] != TCPOPT_TSTAMP_LEN) 9210 break; 9211 9212 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 9213 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 9214 9215 found |= TCP_OPT_TSTAMP_PRESENT; 9216 9217 up += TCPOPT_TSTAMP_LEN; 9218 continue; 9219 9220 default: 9221 if (len <= 1 || len < (int)up[1] || up[1] == 0) 9222 break; 9223 up += up[1]; 9224 continue; 9225 } 9226 break; 9227 } 9228 return (found); 9229 } 9230 9231 /* 9232 * Set the mss associated with a particular tcp based on its current value, 9233 * and a new one passed in. Observe minimums and maximums, and reset 9234 * other state variables that we want to view as multiples of mss. 9235 * 9236 * This function is called in various places mainly because 9237 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9238 * other side's SYN/SYN-ACK packet arrives. 9239 * 2) PMTUd may get us a new MSS. 9240 * 3) If the other side stops sending us timestamp option, we need to 9241 * increase the MSS size to use the extra bytes available. 9242 */ 9243 static void 9244 tcp_mss_set(tcp_t *tcp, uint32_t mss) 9245 { 9246 uint32_t mss_max; 9247 9248 if (tcp->tcp_ipversion == IPV4_VERSION) 9249 mss_max = tcp_mss_max_ipv4; 9250 else 9251 mss_max = tcp_mss_max_ipv6; 9252 9253 if (mss < tcp_mss_min) 9254 mss = tcp_mss_min; 9255 if (mss > mss_max) 9256 mss = mss_max; 9257 /* 9258 * Unless naglim has been set by our client to 9259 * a non-mss value, force naglim to track mss. 9260 * This can help to aggregate small writes. 9261 */ 9262 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9263 tcp->tcp_naglim = mss; 9264 /* 9265 * TCP should be able to buffer at least 4 MSS data for obvious 9266 * performance reason. 9267 */ 9268 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9269 tcp->tcp_xmit_hiwater = mss << 2; 9270 9271 /* 9272 * Check if we need to apply the tcp_init_cwnd here. If 9273 * it is set and the MSS gets bigger (should not happen 9274 * normally), we need to adjust the resulting tcp_cwnd properly. 9275 * The new tcp_cwnd should not get bigger. 9276 */ 9277 if (tcp->tcp_init_cwnd == 0) { 9278 tcp->tcp_cwnd = MIN(tcp_slow_start_initial * mss, 9279 MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9280 } else { 9281 if (tcp->tcp_mss < mss) { 9282 tcp->tcp_cwnd = MAX(1, 9283 (tcp->tcp_init_cwnd * tcp->tcp_mss / mss)) * mss; 9284 } else { 9285 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9286 } 9287 } 9288 tcp->tcp_mss = mss; 9289 tcp->tcp_cwnd_cnt = 0; 9290 (void) tcp_maxpsz_set(tcp, B_TRUE); 9291 } 9292 9293 static int 9294 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9295 { 9296 tcp_t *tcp = NULL; 9297 conn_t *connp; 9298 int err; 9299 dev_t conn_dev; 9300 zoneid_t zoneid = getzoneid(); 9301 9302 /* 9303 * Special case for install: miniroot needs to be able to access files 9304 * via NFS as though it were always in the global zone. 9305 */ 9306 if (credp == kcred && nfs_global_client_only != 0) 9307 zoneid = GLOBAL_ZONEID; 9308 9309 if (q->q_ptr != NULL) 9310 return (0); 9311 9312 if (sflag == MODOPEN) { 9313 /* 9314 * This is a special case. The purpose of a modopen 9315 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9316 * through for MIB browsers. Everything else is failed. 9317 */ 9318 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9319 9320 if (connp == NULL) 9321 return (ENOMEM); 9322 9323 connp->conn_flags |= IPCL_TCPMOD; 9324 connp->conn_cred = credp; 9325 connp->conn_zoneid = zoneid; 9326 q->q_ptr = WR(q)->q_ptr = connp; 9327 crhold(credp); 9328 q->q_qinfo = &tcp_mod_rinit; 9329 WR(q)->q_qinfo = &tcp_mod_winit; 9330 qprocson(q); 9331 return (0); 9332 } 9333 9334 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) 9335 return (EBUSY); 9336 9337 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9338 9339 if (flag & SO_ACCEPTOR) { 9340 q->q_qinfo = &tcp_acceptor_rinit; 9341 q->q_ptr = (void *)conn_dev; 9342 WR(q)->q_qinfo = &tcp_acceptor_winit; 9343 WR(q)->q_ptr = (void *)conn_dev; 9344 qprocson(q); 9345 return (0); 9346 } 9347 9348 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9349 if (connp == NULL) { 9350 inet_minor_free(ip_minor_arena, conn_dev); 9351 q->q_ptr = NULL; 9352 return (ENOSR); 9353 } 9354 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9355 tcp = connp->conn_tcp; 9356 9357 q->q_ptr = WR(q)->q_ptr = connp; 9358 if (getmajor(*devp) == TCP6_MAJ) { 9359 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9360 connp->conn_send = ip_output_v6; 9361 connp->conn_af_isv6 = B_TRUE; 9362 connp->conn_pkt_isv6 = B_TRUE; 9363 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9364 tcp->tcp_ipversion = IPV6_VERSION; 9365 tcp->tcp_family = AF_INET6; 9366 tcp->tcp_mss = tcp_mss_def_ipv6; 9367 } else { 9368 connp->conn_flags |= IPCL_TCP4; 9369 connp->conn_send = ip_output; 9370 connp->conn_af_isv6 = B_FALSE; 9371 connp->conn_pkt_isv6 = B_FALSE; 9372 tcp->tcp_ipversion = IPV4_VERSION; 9373 tcp->tcp_family = AF_INET; 9374 tcp->tcp_mss = tcp_mss_def_ipv4; 9375 } 9376 9377 /* 9378 * TCP keeps a copy of cred for cache locality reasons but 9379 * we put a reference only once. If connp->conn_cred 9380 * becomes invalid, tcp_cred should also be set to NULL. 9381 */ 9382 tcp->tcp_cred = connp->conn_cred = credp; 9383 crhold(connp->conn_cred); 9384 tcp->tcp_cpid = curproc->p_pid; 9385 connp->conn_zoneid = zoneid; 9386 connp->conn_mlp_type = mlptSingle; 9387 connp->conn_ulp_labeled = !is_system_labeled(); 9388 9389 /* 9390 * If the caller has the process-wide flag set, then default to MAC 9391 * exempt mode. This allows read-down to unlabeled hosts. 9392 */ 9393 if (getpflags(NET_MAC_AWARE, credp) != 0) 9394 connp->conn_mac_exempt = B_TRUE; 9395 9396 connp->conn_dev = conn_dev; 9397 9398 ASSERT(q->q_qinfo == &tcp_rinit); 9399 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9400 9401 if (flag & SO_SOCKSTR) { 9402 /* 9403 * No need to insert a socket in tcp acceptor hash. 9404 * If it was a socket acceptor stream, we dealt with 9405 * it above. A socket listener can never accept a 9406 * connection and doesn't need acceptor_id. 9407 */ 9408 connp->conn_flags |= IPCL_SOCKET; 9409 tcp->tcp_issocket = 1; 9410 WR(q)->q_qinfo = &tcp_sock_winit; 9411 } else { 9412 #ifdef _ILP32 9413 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9414 #else 9415 tcp->tcp_acceptor_id = conn_dev; 9416 #endif /* _ILP32 */ 9417 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9418 } 9419 9420 if (tcp_trace) 9421 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9422 9423 err = tcp_init(tcp, q); 9424 if (err != 0) { 9425 inet_minor_free(ip_minor_arena, connp->conn_dev); 9426 tcp_acceptor_hash_remove(tcp); 9427 CONN_DEC_REF(connp); 9428 q->q_ptr = WR(q)->q_ptr = NULL; 9429 return (err); 9430 } 9431 9432 RD(q)->q_hiwat = tcp_recv_hiwat; 9433 tcp->tcp_rwnd = tcp_recv_hiwat; 9434 9435 /* Non-zero default values */ 9436 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9437 /* 9438 * Put the ref for TCP. Ref for IP was already put 9439 * by ipcl_conn_create. Also Make the conn_t globally 9440 * visible to walkers 9441 */ 9442 mutex_enter(&connp->conn_lock); 9443 CONN_INC_REF_LOCKED(connp); 9444 ASSERT(connp->conn_ref == 2); 9445 connp->conn_state_flags &= ~CONN_INCIPIENT; 9446 mutex_exit(&connp->conn_lock); 9447 9448 qprocson(q); 9449 return (0); 9450 } 9451 9452 /* 9453 * Some TCP options can be "set" by requesting them in the option 9454 * buffer. This is needed for XTI feature test though we do not 9455 * allow it in general. We interpret that this mechanism is more 9456 * applicable to OSI protocols and need not be allowed in general. 9457 * This routine filters out options for which it is not allowed (most) 9458 * and lets through those (few) for which it is. [ The XTI interface 9459 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9460 * ever implemented will have to be allowed here ]. 9461 */ 9462 static boolean_t 9463 tcp_allow_connopt_set(int level, int name) 9464 { 9465 9466 switch (level) { 9467 case IPPROTO_TCP: 9468 switch (name) { 9469 case TCP_NODELAY: 9470 return (B_TRUE); 9471 default: 9472 return (B_FALSE); 9473 } 9474 /*NOTREACHED*/ 9475 default: 9476 return (B_FALSE); 9477 } 9478 /*NOTREACHED*/ 9479 } 9480 9481 /* 9482 * This routine gets default values of certain options whose default 9483 * values are maintained by protocol specific code 9484 */ 9485 /* ARGSUSED */ 9486 int 9487 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9488 { 9489 int32_t *i1 = (int32_t *)ptr; 9490 9491 switch (level) { 9492 case IPPROTO_TCP: 9493 switch (name) { 9494 case TCP_NOTIFY_THRESHOLD: 9495 *i1 = tcp_ip_notify_interval; 9496 break; 9497 case TCP_ABORT_THRESHOLD: 9498 *i1 = tcp_ip_abort_interval; 9499 break; 9500 case TCP_CONN_NOTIFY_THRESHOLD: 9501 *i1 = tcp_ip_notify_cinterval; 9502 break; 9503 case TCP_CONN_ABORT_THRESHOLD: 9504 *i1 = tcp_ip_abort_cinterval; 9505 break; 9506 default: 9507 return (-1); 9508 } 9509 break; 9510 case IPPROTO_IP: 9511 switch (name) { 9512 case IP_TTL: 9513 *i1 = tcp_ipv4_ttl; 9514 break; 9515 default: 9516 return (-1); 9517 } 9518 break; 9519 case IPPROTO_IPV6: 9520 switch (name) { 9521 case IPV6_UNICAST_HOPS: 9522 *i1 = tcp_ipv6_hoplimit; 9523 break; 9524 default: 9525 return (-1); 9526 } 9527 break; 9528 default: 9529 return (-1); 9530 } 9531 return (sizeof (int)); 9532 } 9533 9534 9535 /* 9536 * TCP routine to get the values of options. 9537 */ 9538 int 9539 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9540 { 9541 int *i1 = (int *)ptr; 9542 conn_t *connp = Q_TO_CONN(q); 9543 tcp_t *tcp = connp->conn_tcp; 9544 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9545 9546 switch (level) { 9547 case SOL_SOCKET: 9548 switch (name) { 9549 case SO_LINGER: { 9550 struct linger *lgr = (struct linger *)ptr; 9551 9552 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9553 lgr->l_linger = tcp->tcp_lingertime; 9554 } 9555 return (sizeof (struct linger)); 9556 case SO_DEBUG: 9557 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9558 break; 9559 case SO_KEEPALIVE: 9560 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9561 break; 9562 case SO_DONTROUTE: 9563 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9564 break; 9565 case SO_USELOOPBACK: 9566 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9567 break; 9568 case SO_BROADCAST: 9569 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9570 break; 9571 case SO_REUSEADDR: 9572 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9573 break; 9574 case SO_OOBINLINE: 9575 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9576 break; 9577 case SO_DGRAM_ERRIND: 9578 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9579 break; 9580 case SO_TYPE: 9581 *i1 = SOCK_STREAM; 9582 break; 9583 case SO_SNDBUF: 9584 *i1 = tcp->tcp_xmit_hiwater; 9585 break; 9586 case SO_RCVBUF: 9587 *i1 = RD(q)->q_hiwat; 9588 break; 9589 case SO_SND_COPYAVOID: 9590 *i1 = tcp->tcp_snd_zcopy_on ? 9591 SO_SND_COPYAVOID : 0; 9592 break; 9593 case SO_ALLZONES: 9594 *i1 = connp->conn_allzones ? 1 : 0; 9595 break; 9596 case SO_ANON_MLP: 9597 *i1 = connp->conn_anon_mlp; 9598 break; 9599 case SO_MAC_EXEMPT: 9600 *i1 = connp->conn_mac_exempt; 9601 break; 9602 default: 9603 return (-1); 9604 } 9605 break; 9606 case IPPROTO_TCP: 9607 switch (name) { 9608 case TCP_NODELAY: 9609 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9610 break; 9611 case TCP_MAXSEG: 9612 *i1 = tcp->tcp_mss; 9613 break; 9614 case TCP_NOTIFY_THRESHOLD: 9615 *i1 = (int)tcp->tcp_first_timer_threshold; 9616 break; 9617 case TCP_ABORT_THRESHOLD: 9618 *i1 = tcp->tcp_second_timer_threshold; 9619 break; 9620 case TCP_CONN_NOTIFY_THRESHOLD: 9621 *i1 = tcp->tcp_first_ctimer_threshold; 9622 break; 9623 case TCP_CONN_ABORT_THRESHOLD: 9624 *i1 = tcp->tcp_second_ctimer_threshold; 9625 break; 9626 case TCP_RECVDSTADDR: 9627 *i1 = tcp->tcp_recvdstaddr; 9628 break; 9629 case TCP_ANONPRIVBIND: 9630 *i1 = tcp->tcp_anon_priv_bind; 9631 break; 9632 case TCP_EXCLBIND: 9633 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9634 break; 9635 case TCP_INIT_CWND: 9636 *i1 = tcp->tcp_init_cwnd; 9637 break; 9638 case TCP_KEEPALIVE_THRESHOLD: 9639 *i1 = tcp->tcp_ka_interval; 9640 break; 9641 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9642 *i1 = tcp->tcp_ka_abort_thres; 9643 break; 9644 case TCP_CORK: 9645 *i1 = tcp->tcp_cork; 9646 break; 9647 default: 9648 return (-1); 9649 } 9650 break; 9651 case IPPROTO_IP: 9652 if (tcp->tcp_family != AF_INET) 9653 return (-1); 9654 switch (name) { 9655 case IP_OPTIONS: 9656 case T_IP_OPTIONS: { 9657 /* 9658 * This is compatible with BSD in that in only return 9659 * the reverse source route with the final destination 9660 * as the last entry. The first 4 bytes of the option 9661 * will contain the final destination. 9662 */ 9663 int opt_len; 9664 9665 opt_len = (char *)tcp->tcp_tcph - (char *)tcp->tcp_ipha; 9666 opt_len -= tcp->tcp_label_len + IP_SIMPLE_HDR_LENGTH; 9667 ASSERT(opt_len >= 0); 9668 /* Caller ensures enough space */ 9669 if (opt_len > 0) { 9670 /* 9671 * TODO: Do we have to handle getsockopt on an 9672 * initiator as well? 9673 */ 9674 return (ip_opt_get_user(tcp->tcp_ipha, ptr)); 9675 } 9676 return (0); 9677 } 9678 case IP_TOS: 9679 case T_IP_TOS: 9680 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9681 break; 9682 case IP_TTL: 9683 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9684 break; 9685 case IP_NEXTHOP: 9686 /* Handled at IP level */ 9687 return (-EINVAL); 9688 default: 9689 return (-1); 9690 } 9691 break; 9692 case IPPROTO_IPV6: 9693 /* 9694 * IPPROTO_IPV6 options are only supported for sockets 9695 * that are using IPv6 on the wire. 9696 */ 9697 if (tcp->tcp_ipversion != IPV6_VERSION) { 9698 return (-1); 9699 } 9700 switch (name) { 9701 case IPV6_UNICAST_HOPS: 9702 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9703 break; /* goto sizeof (int) option return */ 9704 case IPV6_BOUND_IF: 9705 /* Zero if not set */ 9706 *i1 = tcp->tcp_bound_if; 9707 break; /* goto sizeof (int) option return */ 9708 case IPV6_RECVPKTINFO: 9709 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9710 *i1 = 1; 9711 else 9712 *i1 = 0; 9713 break; /* goto sizeof (int) option return */ 9714 case IPV6_RECVTCLASS: 9715 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9716 *i1 = 1; 9717 else 9718 *i1 = 0; 9719 break; /* goto sizeof (int) option return */ 9720 case IPV6_RECVHOPLIMIT: 9721 if (tcp->tcp_ipv6_recvancillary & 9722 TCP_IPV6_RECVHOPLIMIT) 9723 *i1 = 1; 9724 else 9725 *i1 = 0; 9726 break; /* goto sizeof (int) option return */ 9727 case IPV6_RECVHOPOPTS: 9728 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9729 *i1 = 1; 9730 else 9731 *i1 = 0; 9732 break; /* goto sizeof (int) option return */ 9733 case IPV6_RECVDSTOPTS: 9734 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9735 *i1 = 1; 9736 else 9737 *i1 = 0; 9738 break; /* goto sizeof (int) option return */ 9739 case _OLD_IPV6_RECVDSTOPTS: 9740 if (tcp->tcp_ipv6_recvancillary & 9741 TCP_OLD_IPV6_RECVDSTOPTS) 9742 *i1 = 1; 9743 else 9744 *i1 = 0; 9745 break; /* goto sizeof (int) option return */ 9746 case IPV6_RECVRTHDR: 9747 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9748 *i1 = 1; 9749 else 9750 *i1 = 0; 9751 break; /* goto sizeof (int) option return */ 9752 case IPV6_RECVRTHDRDSTOPTS: 9753 if (tcp->tcp_ipv6_recvancillary & 9754 TCP_IPV6_RECVRTDSTOPTS) 9755 *i1 = 1; 9756 else 9757 *i1 = 0; 9758 break; /* goto sizeof (int) option return */ 9759 case IPV6_PKTINFO: { 9760 /* XXX assumes that caller has room for max size! */ 9761 struct in6_pktinfo *pkti; 9762 9763 pkti = (struct in6_pktinfo *)ptr; 9764 if (ipp->ipp_fields & IPPF_IFINDEX) 9765 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9766 else 9767 pkti->ipi6_ifindex = 0; 9768 if (ipp->ipp_fields & IPPF_ADDR) 9769 pkti->ipi6_addr = ipp->ipp_addr; 9770 else 9771 pkti->ipi6_addr = ipv6_all_zeros; 9772 return (sizeof (struct in6_pktinfo)); 9773 } 9774 case IPV6_TCLASS: 9775 if (ipp->ipp_fields & IPPF_TCLASS) 9776 *i1 = ipp->ipp_tclass; 9777 else 9778 *i1 = IPV6_FLOW_TCLASS( 9779 IPV6_DEFAULT_VERS_AND_FLOW); 9780 break; /* goto sizeof (int) option return */ 9781 case IPV6_NEXTHOP: { 9782 sin6_t *sin6 = (sin6_t *)ptr; 9783 9784 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9785 return (0); 9786 *sin6 = sin6_null; 9787 sin6->sin6_family = AF_INET6; 9788 sin6->sin6_addr = ipp->ipp_nexthop; 9789 return (sizeof (sin6_t)); 9790 } 9791 case IPV6_HOPOPTS: 9792 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9793 return (0); 9794 if (ipp->ipp_hopoptslen <= tcp->tcp_label_len) 9795 return (0); 9796 bcopy((char *)ipp->ipp_hopopts + tcp->tcp_label_len, 9797 ptr, ipp->ipp_hopoptslen - tcp->tcp_label_len); 9798 if (tcp->tcp_label_len > 0) { 9799 ptr[0] = ((char *)ipp->ipp_hopopts)[0]; 9800 ptr[1] = (ipp->ipp_hopoptslen - 9801 tcp->tcp_label_len + 7) / 8 - 1; 9802 } 9803 return (ipp->ipp_hopoptslen - tcp->tcp_label_len); 9804 case IPV6_RTHDRDSTOPTS: 9805 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9806 return (0); 9807 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9808 return (ipp->ipp_rtdstoptslen); 9809 case IPV6_RTHDR: 9810 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9811 return (0); 9812 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9813 return (ipp->ipp_rthdrlen); 9814 case IPV6_DSTOPTS: 9815 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9816 return (0); 9817 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9818 return (ipp->ipp_dstoptslen); 9819 case IPV6_SRC_PREFERENCES: 9820 return (ip6_get_src_preferences(connp, 9821 (uint32_t *)ptr)); 9822 case IPV6_PATHMTU: { 9823 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9824 9825 if (tcp->tcp_state < TCPS_ESTABLISHED) 9826 return (-1); 9827 9828 return (ip_fill_mtuinfo(&connp->conn_remv6, 9829 connp->conn_fport, mtuinfo)); 9830 } 9831 default: 9832 return (-1); 9833 } 9834 break; 9835 default: 9836 return (-1); 9837 } 9838 return (sizeof (int)); 9839 } 9840 9841 /* 9842 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9843 * Parameters are assumed to be verified by the caller. 9844 */ 9845 /* ARGSUSED */ 9846 int 9847 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 9848 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9849 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9850 { 9851 conn_t *connp = Q_TO_CONN(q); 9852 tcp_t *tcp = connp->conn_tcp; 9853 int *i1 = (int *)invalp; 9854 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9855 boolean_t checkonly; 9856 int reterr; 9857 9858 switch (optset_context) { 9859 case SETFN_OPTCOM_CHECKONLY: 9860 checkonly = B_TRUE; 9861 /* 9862 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9863 * inlen != 0 implies value supplied and 9864 * we have to "pretend" to set it. 9865 * inlen == 0 implies that there is no 9866 * value part in T_CHECK request and just validation 9867 * done elsewhere should be enough, we just return here. 9868 */ 9869 if (inlen == 0) { 9870 *outlenp = 0; 9871 return (0); 9872 } 9873 break; 9874 case SETFN_OPTCOM_NEGOTIATE: 9875 checkonly = B_FALSE; 9876 break; 9877 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9878 case SETFN_CONN_NEGOTIATE: 9879 checkonly = B_FALSE; 9880 /* 9881 * Negotiating local and "association-related" options 9882 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9883 * primitives is allowed by XTI, but we choose 9884 * to not implement this style negotiation for Internet 9885 * protocols (We interpret it is a must for OSI world but 9886 * optional for Internet protocols) for all options. 9887 * [ Will do only for the few options that enable test 9888 * suites that our XTI implementation of this feature 9889 * works for transports that do allow it ] 9890 */ 9891 if (!tcp_allow_connopt_set(level, name)) { 9892 *outlenp = 0; 9893 return (EINVAL); 9894 } 9895 break; 9896 default: 9897 /* 9898 * We should never get here 9899 */ 9900 *outlenp = 0; 9901 return (EINVAL); 9902 } 9903 9904 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9905 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9906 9907 /* 9908 * For TCP, we should have no ancillary data sent down 9909 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9910 * has to be zero. 9911 */ 9912 ASSERT(thisdg_attrs == NULL); 9913 9914 /* 9915 * For fixed length options, no sanity check 9916 * of passed in length is done. It is assumed *_optcom_req() 9917 * routines do the right thing. 9918 */ 9919 9920 switch (level) { 9921 case SOL_SOCKET: 9922 switch (name) { 9923 case SO_LINGER: { 9924 struct linger *lgr = (struct linger *)invalp; 9925 9926 if (!checkonly) { 9927 if (lgr->l_onoff) { 9928 tcp->tcp_linger = 1; 9929 tcp->tcp_lingertime = lgr->l_linger; 9930 } else { 9931 tcp->tcp_linger = 0; 9932 tcp->tcp_lingertime = 0; 9933 } 9934 /* struct copy */ 9935 *(struct linger *)outvalp = *lgr; 9936 } else { 9937 if (!lgr->l_onoff) { 9938 ((struct linger *)outvalp)->l_onoff = 0; 9939 ((struct linger *)outvalp)->l_linger = 0; 9940 } else { 9941 /* struct copy */ 9942 *(struct linger *)outvalp = *lgr; 9943 } 9944 } 9945 *outlenp = sizeof (struct linger); 9946 return (0); 9947 } 9948 case SO_DEBUG: 9949 if (!checkonly) 9950 tcp->tcp_debug = onoff; 9951 break; 9952 case SO_KEEPALIVE: 9953 if (checkonly) { 9954 /* T_CHECK case */ 9955 break; 9956 } 9957 9958 if (!onoff) { 9959 if (tcp->tcp_ka_enabled) { 9960 if (tcp->tcp_ka_tid != 0) { 9961 (void) TCP_TIMER_CANCEL(tcp, 9962 tcp->tcp_ka_tid); 9963 tcp->tcp_ka_tid = 0; 9964 } 9965 tcp->tcp_ka_enabled = 0; 9966 } 9967 break; 9968 } 9969 if (!tcp->tcp_ka_enabled) { 9970 /* Crank up the keepalive timer */ 9971 tcp->tcp_ka_last_intrvl = 0; 9972 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9973 tcp_keepalive_killer, 9974 MSEC_TO_TICK(tcp->tcp_ka_interval)); 9975 tcp->tcp_ka_enabled = 1; 9976 } 9977 break; 9978 case SO_DONTROUTE: 9979 /* 9980 * SO_DONTROUTE, SO_USELOOPBACK, and SO_BROADCAST are 9981 * only of interest to IP. We track them here only so 9982 * that we can report their current value. 9983 */ 9984 if (!checkonly) { 9985 tcp->tcp_dontroute = onoff; 9986 tcp->tcp_connp->conn_dontroute = onoff; 9987 } 9988 break; 9989 case SO_USELOOPBACK: 9990 if (!checkonly) { 9991 tcp->tcp_useloopback = onoff; 9992 tcp->tcp_connp->conn_loopback = onoff; 9993 } 9994 break; 9995 case SO_BROADCAST: 9996 if (!checkonly) { 9997 tcp->tcp_broadcast = onoff; 9998 tcp->tcp_connp->conn_broadcast = onoff; 9999 } 10000 break; 10001 case SO_REUSEADDR: 10002 if (!checkonly) { 10003 tcp->tcp_reuseaddr = onoff; 10004 tcp->tcp_connp->conn_reuseaddr = onoff; 10005 } 10006 break; 10007 case SO_OOBINLINE: 10008 if (!checkonly) 10009 tcp->tcp_oobinline = onoff; 10010 break; 10011 case SO_DGRAM_ERRIND: 10012 if (!checkonly) 10013 tcp->tcp_dgram_errind = onoff; 10014 break; 10015 case SO_SNDBUF: { 10016 tcp_t *peer_tcp; 10017 10018 if (*i1 > tcp_max_buf) { 10019 *outlenp = 0; 10020 return (ENOBUFS); 10021 } 10022 if (checkonly) 10023 break; 10024 10025 tcp->tcp_xmit_hiwater = *i1; 10026 if (tcp_snd_lowat_fraction != 0) 10027 tcp->tcp_xmit_lowater = 10028 tcp->tcp_xmit_hiwater / 10029 tcp_snd_lowat_fraction; 10030 (void) tcp_maxpsz_set(tcp, B_TRUE); 10031 /* 10032 * If we are flow-controlled, recheck the condition. 10033 * There are apps that increase SO_SNDBUF size when 10034 * flow-controlled (EWOULDBLOCK), and expect the flow 10035 * control condition to be lifted right away. 10036 * 10037 * For the fused tcp loopback case, in order to avoid 10038 * a race with the peer's tcp_fuse_rrw() we need to 10039 * hold its fuse_lock while accessing tcp_flow_stopped. 10040 */ 10041 peer_tcp = tcp->tcp_loopback_peer; 10042 ASSERT(!tcp->tcp_fused || peer_tcp != NULL); 10043 if (tcp->tcp_fused) 10044 mutex_enter(&peer_tcp->tcp_fuse_lock); 10045 10046 if (tcp->tcp_flow_stopped && 10047 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 10048 tcp_clrqfull(tcp); 10049 } 10050 if (tcp->tcp_fused) 10051 mutex_exit(&peer_tcp->tcp_fuse_lock); 10052 break; 10053 } 10054 case SO_RCVBUF: 10055 if (*i1 > tcp_max_buf) { 10056 *outlenp = 0; 10057 return (ENOBUFS); 10058 } 10059 /* Silently ignore zero */ 10060 if (!checkonly && *i1 != 0) { 10061 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 10062 (void) tcp_rwnd_set(tcp, *i1); 10063 } 10064 /* 10065 * XXX should we return the rwnd here 10066 * and tcp_opt_get ? 10067 */ 10068 break; 10069 case SO_SND_COPYAVOID: 10070 if (!checkonly) { 10071 /* we only allow enable at most once for now */ 10072 if (tcp->tcp_loopback || 10073 (!tcp->tcp_snd_zcopy_aware && 10074 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 10075 *outlenp = 0; 10076 return (EOPNOTSUPP); 10077 } 10078 tcp->tcp_snd_zcopy_aware = 1; 10079 } 10080 break; 10081 case SO_ALLZONES: 10082 /* Handled at the IP level */ 10083 return (-EINVAL); 10084 case SO_ANON_MLP: 10085 if (!checkonly) { 10086 mutex_enter(&connp->conn_lock); 10087 connp->conn_anon_mlp = onoff; 10088 mutex_exit(&connp->conn_lock); 10089 } 10090 break; 10091 case SO_MAC_EXEMPT: 10092 if (secpolicy_net_mac_aware(cr) != 0 || 10093 IPCL_IS_BOUND(connp)) 10094 return (EACCES); 10095 if (!checkonly) { 10096 mutex_enter(&connp->conn_lock); 10097 connp->conn_mac_exempt = onoff; 10098 mutex_exit(&connp->conn_lock); 10099 } 10100 break; 10101 default: 10102 *outlenp = 0; 10103 return (EINVAL); 10104 } 10105 break; 10106 case IPPROTO_TCP: 10107 switch (name) { 10108 case TCP_NODELAY: 10109 if (!checkonly) 10110 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 10111 break; 10112 case TCP_NOTIFY_THRESHOLD: 10113 if (!checkonly) 10114 tcp->tcp_first_timer_threshold = *i1; 10115 break; 10116 case TCP_ABORT_THRESHOLD: 10117 if (!checkonly) 10118 tcp->tcp_second_timer_threshold = *i1; 10119 break; 10120 case TCP_CONN_NOTIFY_THRESHOLD: 10121 if (!checkonly) 10122 tcp->tcp_first_ctimer_threshold = *i1; 10123 break; 10124 case TCP_CONN_ABORT_THRESHOLD: 10125 if (!checkonly) 10126 tcp->tcp_second_ctimer_threshold = *i1; 10127 break; 10128 case TCP_RECVDSTADDR: 10129 if (tcp->tcp_state > TCPS_LISTEN) 10130 return (EOPNOTSUPP); 10131 if (!checkonly) 10132 tcp->tcp_recvdstaddr = onoff; 10133 break; 10134 case TCP_ANONPRIVBIND: 10135 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 10136 *outlenp = 0; 10137 return (reterr); 10138 } 10139 if (!checkonly) { 10140 tcp->tcp_anon_priv_bind = onoff; 10141 } 10142 break; 10143 case TCP_EXCLBIND: 10144 if (!checkonly) 10145 tcp->tcp_exclbind = onoff; 10146 break; /* goto sizeof (int) option return */ 10147 case TCP_INIT_CWND: { 10148 uint32_t init_cwnd = *((uint32_t *)invalp); 10149 10150 if (checkonly) 10151 break; 10152 10153 /* 10154 * Only allow socket with network configuration 10155 * privilege to set the initial cwnd to be larger 10156 * than allowed by RFC 3390. 10157 */ 10158 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 10159 tcp->tcp_init_cwnd = init_cwnd; 10160 break; 10161 } 10162 if ((reterr = secpolicy_net_config(cr, B_TRUE)) != 0) { 10163 *outlenp = 0; 10164 return (reterr); 10165 } 10166 if (init_cwnd > TCP_MAX_INIT_CWND) { 10167 *outlenp = 0; 10168 return (EINVAL); 10169 } 10170 tcp->tcp_init_cwnd = init_cwnd; 10171 break; 10172 } 10173 case TCP_KEEPALIVE_THRESHOLD: 10174 if (checkonly) 10175 break; 10176 10177 if (*i1 < tcp_keepalive_interval_low || 10178 *i1 > tcp_keepalive_interval_high) { 10179 *outlenp = 0; 10180 return (EINVAL); 10181 } 10182 if (*i1 != tcp->tcp_ka_interval) { 10183 tcp->tcp_ka_interval = *i1; 10184 /* 10185 * Check if we need to restart the 10186 * keepalive timer. 10187 */ 10188 if (tcp->tcp_ka_tid != 0) { 10189 ASSERT(tcp->tcp_ka_enabled); 10190 (void) TCP_TIMER_CANCEL(tcp, 10191 tcp->tcp_ka_tid); 10192 tcp->tcp_ka_last_intrvl = 0; 10193 tcp->tcp_ka_tid = TCP_TIMER(tcp, 10194 tcp_keepalive_killer, 10195 MSEC_TO_TICK(tcp->tcp_ka_interval)); 10196 } 10197 } 10198 break; 10199 case TCP_KEEPALIVE_ABORT_THRESHOLD: 10200 if (!checkonly) { 10201 if (*i1 < tcp_keepalive_abort_interval_low || 10202 *i1 > tcp_keepalive_abort_interval_high) { 10203 *outlenp = 0; 10204 return (EINVAL); 10205 } 10206 tcp->tcp_ka_abort_thres = *i1; 10207 } 10208 break; 10209 case TCP_CORK: 10210 if (!checkonly) { 10211 /* 10212 * if tcp->tcp_cork was set and is now 10213 * being unset, we have to make sure that 10214 * the remaining data gets sent out. Also 10215 * unset tcp->tcp_cork so that tcp_wput_data() 10216 * can send data even if it is less than mss 10217 */ 10218 if (tcp->tcp_cork && onoff == 0 && 10219 tcp->tcp_unsent > 0) { 10220 tcp->tcp_cork = B_FALSE; 10221 tcp_wput_data(tcp, NULL, B_FALSE); 10222 } 10223 tcp->tcp_cork = onoff; 10224 } 10225 break; 10226 default: 10227 *outlenp = 0; 10228 return (EINVAL); 10229 } 10230 break; 10231 case IPPROTO_IP: 10232 if (tcp->tcp_family != AF_INET) { 10233 *outlenp = 0; 10234 return (ENOPROTOOPT); 10235 } 10236 switch (name) { 10237 case IP_OPTIONS: 10238 case T_IP_OPTIONS: 10239 reterr = tcp_opt_set_header(tcp, checkonly, 10240 invalp, inlen); 10241 if (reterr) { 10242 *outlenp = 0; 10243 return (reterr); 10244 } 10245 /* OK return - copy input buffer into output buffer */ 10246 if (invalp != outvalp) { 10247 /* don't trust bcopy for identical src/dst */ 10248 bcopy(invalp, outvalp, inlen); 10249 } 10250 *outlenp = inlen; 10251 return (0); 10252 case IP_TOS: 10253 case T_IP_TOS: 10254 if (!checkonly) { 10255 tcp->tcp_ipha->ipha_type_of_service = 10256 (uchar_t)*i1; 10257 tcp->tcp_tos = (uchar_t)*i1; 10258 } 10259 break; 10260 case IP_TTL: 10261 if (!checkonly) { 10262 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 10263 tcp->tcp_ttl = (uchar_t)*i1; 10264 } 10265 break; 10266 case IP_BOUND_IF: 10267 case IP_NEXTHOP: 10268 /* Handled at the IP level */ 10269 return (-EINVAL); 10270 case IP_SEC_OPT: 10271 /* 10272 * We should not allow policy setting after 10273 * we start listening for connections. 10274 */ 10275 if (tcp->tcp_state == TCPS_LISTEN) { 10276 return (EINVAL); 10277 } else { 10278 /* Handled at the IP level */ 10279 return (-EINVAL); 10280 } 10281 default: 10282 *outlenp = 0; 10283 return (EINVAL); 10284 } 10285 break; 10286 case IPPROTO_IPV6: { 10287 ip6_pkt_t *ipp; 10288 10289 /* 10290 * IPPROTO_IPV6 options are only supported for sockets 10291 * that are using IPv6 on the wire. 10292 */ 10293 if (tcp->tcp_ipversion != IPV6_VERSION) { 10294 *outlenp = 0; 10295 return (ENOPROTOOPT); 10296 } 10297 /* 10298 * Only sticky options; no ancillary data 10299 */ 10300 ASSERT(thisdg_attrs == NULL); 10301 ipp = &tcp->tcp_sticky_ipp; 10302 10303 switch (name) { 10304 case IPV6_UNICAST_HOPS: 10305 /* -1 means use default */ 10306 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10307 *outlenp = 0; 10308 return (EINVAL); 10309 } 10310 if (!checkonly) { 10311 if (*i1 == -1) { 10312 tcp->tcp_ip6h->ip6_hops = 10313 ipp->ipp_unicast_hops = 10314 (uint8_t)tcp_ipv6_hoplimit; 10315 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10316 /* Pass modified value to IP. */ 10317 *i1 = tcp->tcp_ip6h->ip6_hops; 10318 } else { 10319 tcp->tcp_ip6h->ip6_hops = 10320 ipp->ipp_unicast_hops = 10321 (uint8_t)*i1; 10322 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10323 } 10324 reterr = tcp_build_hdrs(q, tcp); 10325 if (reterr != 0) 10326 return (reterr); 10327 } 10328 break; 10329 case IPV6_BOUND_IF: 10330 if (!checkonly) { 10331 int error = 0; 10332 10333 tcp->tcp_bound_if = *i1; 10334 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10335 B_TRUE, checkonly, level, name, mblk); 10336 if (error != 0) { 10337 *outlenp = 0; 10338 return (error); 10339 } 10340 } 10341 break; 10342 /* 10343 * Set boolean switches for ancillary data delivery 10344 */ 10345 case IPV6_RECVPKTINFO: 10346 if (!checkonly) { 10347 if (onoff) 10348 tcp->tcp_ipv6_recvancillary |= 10349 TCP_IPV6_RECVPKTINFO; 10350 else 10351 tcp->tcp_ipv6_recvancillary &= 10352 ~TCP_IPV6_RECVPKTINFO; 10353 /* Force it to be sent up with the next msg */ 10354 tcp->tcp_recvifindex = 0; 10355 } 10356 break; 10357 case IPV6_RECVTCLASS: 10358 if (!checkonly) { 10359 if (onoff) 10360 tcp->tcp_ipv6_recvancillary |= 10361 TCP_IPV6_RECVTCLASS; 10362 else 10363 tcp->tcp_ipv6_recvancillary &= 10364 ~TCP_IPV6_RECVTCLASS; 10365 } 10366 break; 10367 case IPV6_RECVHOPLIMIT: 10368 if (!checkonly) { 10369 if (onoff) 10370 tcp->tcp_ipv6_recvancillary |= 10371 TCP_IPV6_RECVHOPLIMIT; 10372 else 10373 tcp->tcp_ipv6_recvancillary &= 10374 ~TCP_IPV6_RECVHOPLIMIT; 10375 /* Force it to be sent up with the next msg */ 10376 tcp->tcp_recvhops = 0xffffffffU; 10377 } 10378 break; 10379 case IPV6_RECVHOPOPTS: 10380 if (!checkonly) { 10381 if (onoff) 10382 tcp->tcp_ipv6_recvancillary |= 10383 TCP_IPV6_RECVHOPOPTS; 10384 else 10385 tcp->tcp_ipv6_recvancillary &= 10386 ~TCP_IPV6_RECVHOPOPTS; 10387 } 10388 break; 10389 case IPV6_RECVDSTOPTS: 10390 if (!checkonly) { 10391 if (onoff) 10392 tcp->tcp_ipv6_recvancillary |= 10393 TCP_IPV6_RECVDSTOPTS; 10394 else 10395 tcp->tcp_ipv6_recvancillary &= 10396 ~TCP_IPV6_RECVDSTOPTS; 10397 } 10398 break; 10399 case _OLD_IPV6_RECVDSTOPTS: 10400 if (!checkonly) { 10401 if (onoff) 10402 tcp->tcp_ipv6_recvancillary |= 10403 TCP_OLD_IPV6_RECVDSTOPTS; 10404 else 10405 tcp->tcp_ipv6_recvancillary &= 10406 ~TCP_OLD_IPV6_RECVDSTOPTS; 10407 } 10408 break; 10409 case IPV6_RECVRTHDR: 10410 if (!checkonly) { 10411 if (onoff) 10412 tcp->tcp_ipv6_recvancillary |= 10413 TCP_IPV6_RECVRTHDR; 10414 else 10415 tcp->tcp_ipv6_recvancillary &= 10416 ~TCP_IPV6_RECVRTHDR; 10417 } 10418 break; 10419 case IPV6_RECVRTHDRDSTOPTS: 10420 if (!checkonly) { 10421 if (onoff) 10422 tcp->tcp_ipv6_recvancillary |= 10423 TCP_IPV6_RECVRTDSTOPTS; 10424 else 10425 tcp->tcp_ipv6_recvancillary &= 10426 ~TCP_IPV6_RECVRTDSTOPTS; 10427 } 10428 break; 10429 case IPV6_PKTINFO: 10430 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10431 return (EINVAL); 10432 if (checkonly) 10433 break; 10434 10435 if (inlen == 0) { 10436 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10437 } else { 10438 struct in6_pktinfo *pkti; 10439 10440 pkti = (struct in6_pktinfo *)invalp; 10441 /* 10442 * RFC 3542 states that ipi6_addr must be 10443 * the unspecified address when setting the 10444 * IPV6_PKTINFO sticky socket option on a 10445 * TCP socket. 10446 */ 10447 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10448 return (EINVAL); 10449 /* 10450 * ip6_set_pktinfo() validates the source 10451 * address and interface index. 10452 */ 10453 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10454 pkti, mblk); 10455 if (reterr != 0) 10456 return (reterr); 10457 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10458 ipp->ipp_addr = pkti->ipi6_addr; 10459 if (ipp->ipp_ifindex != 0) 10460 ipp->ipp_fields |= IPPF_IFINDEX; 10461 else 10462 ipp->ipp_fields &= ~IPPF_IFINDEX; 10463 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10464 ipp->ipp_fields |= IPPF_ADDR; 10465 else 10466 ipp->ipp_fields &= ~IPPF_ADDR; 10467 } 10468 reterr = tcp_build_hdrs(q, tcp); 10469 if (reterr != 0) 10470 return (reterr); 10471 break; 10472 case IPV6_TCLASS: 10473 if (inlen != 0 && inlen != sizeof (int)) 10474 return (EINVAL); 10475 if (checkonly) 10476 break; 10477 10478 if (inlen == 0) { 10479 ipp->ipp_fields &= ~IPPF_TCLASS; 10480 } else { 10481 if (*i1 > 255 || *i1 < -1) 10482 return (EINVAL); 10483 if (*i1 == -1) { 10484 ipp->ipp_tclass = 0; 10485 *i1 = 0; 10486 } else { 10487 ipp->ipp_tclass = *i1; 10488 } 10489 ipp->ipp_fields |= IPPF_TCLASS; 10490 } 10491 reterr = tcp_build_hdrs(q, tcp); 10492 if (reterr != 0) 10493 return (reterr); 10494 break; 10495 case IPV6_NEXTHOP: 10496 /* 10497 * IP will verify that the nexthop is reachable 10498 * and fail for sticky options. 10499 */ 10500 if (inlen != 0 && inlen != sizeof (sin6_t)) 10501 return (EINVAL); 10502 if (checkonly) 10503 break; 10504 10505 if (inlen == 0) { 10506 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10507 } else { 10508 sin6_t *sin6 = (sin6_t *)invalp; 10509 10510 if (sin6->sin6_family != AF_INET6) 10511 return (EAFNOSUPPORT); 10512 if (IN6_IS_ADDR_V4MAPPED( 10513 &sin6->sin6_addr)) 10514 return (EADDRNOTAVAIL); 10515 ipp->ipp_nexthop = sin6->sin6_addr; 10516 if (!IN6_IS_ADDR_UNSPECIFIED( 10517 &ipp->ipp_nexthop)) 10518 ipp->ipp_fields |= IPPF_NEXTHOP; 10519 else 10520 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10521 } 10522 reterr = tcp_build_hdrs(q, tcp); 10523 if (reterr != 0) 10524 return (reterr); 10525 break; 10526 case IPV6_HOPOPTS: { 10527 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10528 10529 /* 10530 * Sanity checks - minimum size, size a multiple of 10531 * eight bytes, and matching size passed in. 10532 */ 10533 if (inlen != 0 && 10534 inlen != (8 * (hopts->ip6h_len + 1))) 10535 return (EINVAL); 10536 10537 if (checkonly) 10538 break; 10539 10540 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10541 (uchar_t **)&ipp->ipp_hopopts, 10542 &ipp->ipp_hopoptslen, tcp->tcp_label_len); 10543 if (reterr != 0) 10544 return (reterr); 10545 if (ipp->ipp_hopoptslen == 0) 10546 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10547 else 10548 ipp->ipp_fields |= IPPF_HOPOPTS; 10549 reterr = tcp_build_hdrs(q, tcp); 10550 if (reterr != 0) 10551 return (reterr); 10552 break; 10553 } 10554 case IPV6_RTHDRDSTOPTS: { 10555 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10556 10557 /* 10558 * Sanity checks - minimum size, size a multiple of 10559 * eight bytes, and matching size passed in. 10560 */ 10561 if (inlen != 0 && 10562 inlen != (8 * (dopts->ip6d_len + 1))) 10563 return (EINVAL); 10564 10565 if (checkonly) 10566 break; 10567 10568 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10569 (uchar_t **)&ipp->ipp_rtdstopts, 10570 &ipp->ipp_rtdstoptslen, 0); 10571 if (reterr != 0) 10572 return (reterr); 10573 if (ipp->ipp_rtdstoptslen == 0) 10574 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10575 else 10576 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10577 reterr = tcp_build_hdrs(q, tcp); 10578 if (reterr != 0) 10579 return (reterr); 10580 break; 10581 } 10582 case IPV6_DSTOPTS: { 10583 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10584 10585 /* 10586 * Sanity checks - minimum size, size a multiple of 10587 * eight bytes, and matching size passed in. 10588 */ 10589 if (inlen != 0 && 10590 inlen != (8 * (dopts->ip6d_len + 1))) 10591 return (EINVAL); 10592 10593 if (checkonly) 10594 break; 10595 10596 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10597 (uchar_t **)&ipp->ipp_dstopts, 10598 &ipp->ipp_dstoptslen, 0); 10599 if (reterr != 0) 10600 return (reterr); 10601 if (ipp->ipp_dstoptslen == 0) 10602 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10603 else 10604 ipp->ipp_fields |= IPPF_DSTOPTS; 10605 reterr = tcp_build_hdrs(q, tcp); 10606 if (reterr != 0) 10607 return (reterr); 10608 break; 10609 } 10610 case IPV6_RTHDR: { 10611 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10612 10613 /* 10614 * Sanity checks - minimum size, size a multiple of 10615 * eight bytes, and matching size passed in. 10616 */ 10617 if (inlen != 0 && 10618 inlen != (8 * (rt->ip6r_len + 1))) 10619 return (EINVAL); 10620 10621 if (checkonly) 10622 break; 10623 10624 reterr = optcom_pkt_set(invalp, inlen, B_TRUE, 10625 (uchar_t **)&ipp->ipp_rthdr, 10626 &ipp->ipp_rthdrlen, 0); 10627 if (reterr != 0) 10628 return (reterr); 10629 if (ipp->ipp_rthdrlen == 0) 10630 ipp->ipp_fields &= ~IPPF_RTHDR; 10631 else 10632 ipp->ipp_fields |= IPPF_RTHDR; 10633 reterr = tcp_build_hdrs(q, tcp); 10634 if (reterr != 0) 10635 return (reterr); 10636 break; 10637 } 10638 case IPV6_V6ONLY: 10639 if (!checkonly) 10640 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10641 break; 10642 case IPV6_USE_MIN_MTU: 10643 if (inlen != sizeof (int)) 10644 return (EINVAL); 10645 10646 if (*i1 < -1 || *i1 > 1) 10647 return (EINVAL); 10648 10649 if (checkonly) 10650 break; 10651 10652 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10653 ipp->ipp_use_min_mtu = *i1; 10654 break; 10655 case IPV6_BOUND_PIF: 10656 /* Handled at the IP level */ 10657 return (-EINVAL); 10658 case IPV6_SEC_OPT: 10659 /* 10660 * We should not allow policy setting after 10661 * we start listening for connections. 10662 */ 10663 if (tcp->tcp_state == TCPS_LISTEN) { 10664 return (EINVAL); 10665 } else { 10666 /* Handled at the IP level */ 10667 return (-EINVAL); 10668 } 10669 case IPV6_SRC_PREFERENCES: 10670 if (inlen != sizeof (uint32_t)) 10671 return (EINVAL); 10672 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10673 *(uint32_t *)invalp); 10674 if (reterr != 0) { 10675 *outlenp = 0; 10676 return (reterr); 10677 } 10678 break; 10679 default: 10680 *outlenp = 0; 10681 return (EINVAL); 10682 } 10683 break; 10684 } /* end IPPROTO_IPV6 */ 10685 default: 10686 *outlenp = 0; 10687 return (EINVAL); 10688 } 10689 /* 10690 * Common case of OK return with outval same as inval 10691 */ 10692 if (invalp != outvalp) { 10693 /* don't trust bcopy for identical src/dst */ 10694 (void) bcopy(invalp, outvalp, inlen); 10695 } 10696 *outlenp = inlen; 10697 return (0); 10698 } 10699 10700 /* 10701 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10702 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10703 * headers, and the maximum size tcp header (to avoid reallocation 10704 * on the fly for additional tcp options). 10705 * Returns failure if can't allocate memory. 10706 */ 10707 static int 10708 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 10709 { 10710 char *hdrs; 10711 uint_t hdrs_len; 10712 ip6i_t *ip6i; 10713 char buf[TCP_MAX_HDR_LENGTH]; 10714 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10715 in6_addr_t src, dst; 10716 10717 /* 10718 * save the existing tcp header and source/dest IP addresses 10719 */ 10720 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10721 src = tcp->tcp_ip6h->ip6_src; 10722 dst = tcp->tcp_ip6h->ip6_dst; 10723 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10724 ASSERT(hdrs_len != 0); 10725 if (hdrs_len > tcp->tcp_iphc_len) { 10726 /* Need to reallocate */ 10727 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10728 if (hdrs == NULL) 10729 return (ENOMEM); 10730 if (tcp->tcp_iphc != NULL) { 10731 if (tcp->tcp_hdr_grown) { 10732 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10733 } else { 10734 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10735 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10736 } 10737 tcp->tcp_iphc_len = 0; 10738 } 10739 ASSERT(tcp->tcp_iphc_len == 0); 10740 tcp->tcp_iphc = hdrs; 10741 tcp->tcp_iphc_len = hdrs_len; 10742 tcp->tcp_hdr_grown = B_TRUE; 10743 } 10744 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10745 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10746 10747 /* Set header fields not in ipp */ 10748 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10749 ip6i = (ip6i_t *)tcp->tcp_iphc; 10750 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10751 } else { 10752 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10753 } 10754 /* 10755 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10756 * 10757 * tcp->tcp_tcp_hdr_len doesn't change here. 10758 */ 10759 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10760 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10761 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10762 10763 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10764 10765 tcp->tcp_ip6h->ip6_src = src; 10766 tcp->tcp_ip6h->ip6_dst = dst; 10767 10768 /* 10769 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10770 * the default value for TCP. 10771 */ 10772 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10773 tcp->tcp_ip6h->ip6_hops = tcp_ipv6_hoplimit; 10774 10775 /* 10776 * If we're setting extension headers after a connection 10777 * has been established, and if we have a routing header 10778 * among the extension headers, call ip_massage_options_v6 to 10779 * manipulate the routing header/ip6_dst set the checksum 10780 * difference in the tcp header template. 10781 * (This happens in tcp_connect_ipv6 if the routing header 10782 * is set prior to the connect.) 10783 * Set the tcp_sum to zero first in case we've cleared a 10784 * routing header or don't have one at all. 10785 */ 10786 tcp->tcp_sum = 0; 10787 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10788 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10789 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10790 (uint8_t *)tcp->tcp_tcph); 10791 if (rth != NULL) { 10792 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10793 rth); 10794 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10795 (tcp->tcp_sum >> 16)); 10796 } 10797 } 10798 10799 /* Try to get everything in a single mblk */ 10800 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcp_wroff_xtra); 10801 return (0); 10802 } 10803 10804 /* 10805 * Transfer any source route option from ipha to buf/dst in reversed form. 10806 */ 10807 static int 10808 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10809 { 10810 ipoptp_t opts; 10811 uchar_t *opt; 10812 uint8_t optval; 10813 uint8_t optlen; 10814 uint32_t len = 0; 10815 10816 for (optval = ipoptp_first(&opts, ipha); 10817 optval != IPOPT_EOL; 10818 optval = ipoptp_next(&opts)) { 10819 opt = opts.ipoptp_cur; 10820 optlen = opts.ipoptp_len; 10821 switch (optval) { 10822 int off1, off2; 10823 case IPOPT_SSRR: 10824 case IPOPT_LSRR: 10825 10826 /* Reverse source route */ 10827 /* 10828 * First entry should be the next to last one in the 10829 * current source route (the last entry is our 10830 * address.) 10831 * The last entry should be the final destination. 10832 */ 10833 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10834 buf[IPOPT_OLEN] = (uint8_t)optlen; 10835 off1 = IPOPT_MINOFF_SR - 1; 10836 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10837 if (off2 < 0) { 10838 /* No entries in source route */ 10839 break; 10840 } 10841 bcopy(opt + off2, dst, IP_ADDR_LEN); 10842 /* 10843 * Note: use src since ipha has not had its src 10844 * and dst reversed (it is in the state it was 10845 * received. 10846 */ 10847 bcopy(&ipha->ipha_src, buf + off2, 10848 IP_ADDR_LEN); 10849 off2 -= IP_ADDR_LEN; 10850 10851 while (off2 > 0) { 10852 bcopy(opt + off2, buf + off1, 10853 IP_ADDR_LEN); 10854 off1 += IP_ADDR_LEN; 10855 off2 -= IP_ADDR_LEN; 10856 } 10857 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10858 buf += optlen; 10859 len += optlen; 10860 break; 10861 } 10862 } 10863 done: 10864 /* Pad the resulting options */ 10865 while (len & 0x3) { 10866 *buf++ = IPOPT_EOL; 10867 len++; 10868 } 10869 return (len); 10870 } 10871 10872 10873 /* 10874 * Extract and revert a source route from ipha (if any) 10875 * and then update the relevant fields in both tcp_t and the standard header. 10876 */ 10877 static void 10878 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 10879 { 10880 char buf[TCP_MAX_HDR_LENGTH]; 10881 uint_t tcph_len; 10882 int len; 10883 10884 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 10885 len = IPH_HDR_LENGTH(ipha); 10886 if (len == IP_SIMPLE_HDR_LENGTH) 10887 /* Nothing to do */ 10888 return; 10889 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 10890 (len & 0x3)) 10891 return; 10892 10893 tcph_len = tcp->tcp_tcp_hdr_len; 10894 bcopy(tcp->tcp_tcph, buf, tcph_len); 10895 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 10896 (tcp->tcp_ipha->ipha_dst & 0xffff); 10897 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 10898 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 10899 len += IP_SIMPLE_HDR_LENGTH; 10900 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 10901 (tcp->tcp_ipha->ipha_dst & 0xffff)); 10902 if ((int)tcp->tcp_sum < 0) 10903 tcp->tcp_sum--; 10904 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 10905 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 10906 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 10907 bcopy(buf, tcp->tcp_tcph, tcph_len); 10908 tcp->tcp_ip_hdr_len = len; 10909 tcp->tcp_ipha->ipha_version_and_hdr_length = 10910 (IP_VERSION << 4) | (len >> 2); 10911 len += tcph_len; 10912 tcp->tcp_hdr_len = len; 10913 } 10914 10915 /* 10916 * Copy the standard header into its new location, 10917 * lay in the new options and then update the relevant 10918 * fields in both tcp_t and the standard header. 10919 */ 10920 static int 10921 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 10922 { 10923 uint_t tcph_len; 10924 uint8_t *ip_optp; 10925 tcph_t *new_tcph; 10926 10927 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 10928 return (EINVAL); 10929 10930 if (len > IP_MAX_OPT_LENGTH - tcp->tcp_label_len) 10931 return (EINVAL); 10932 10933 if (checkonly) { 10934 /* 10935 * do not really set, just pretend to - T_CHECK 10936 */ 10937 return (0); 10938 } 10939 10940 ip_optp = (uint8_t *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 10941 if (tcp->tcp_label_len > 0) { 10942 int padlen; 10943 uint8_t opt; 10944 10945 /* convert list termination to no-ops */ 10946 padlen = tcp->tcp_label_len - ip_optp[IPOPT_OLEN]; 10947 ip_optp += ip_optp[IPOPT_OLEN]; 10948 opt = len > 0 ? IPOPT_NOP : IPOPT_EOL; 10949 while (--padlen >= 0) 10950 *ip_optp++ = opt; 10951 } 10952 tcph_len = tcp->tcp_tcp_hdr_len; 10953 new_tcph = (tcph_t *)(ip_optp + len); 10954 ovbcopy(tcp->tcp_tcph, new_tcph, tcph_len); 10955 tcp->tcp_tcph = new_tcph; 10956 bcopy(ptr, ip_optp, len); 10957 10958 len += IP_SIMPLE_HDR_LENGTH + tcp->tcp_label_len; 10959 10960 tcp->tcp_ip_hdr_len = len; 10961 tcp->tcp_ipha->ipha_version_and_hdr_length = 10962 (IP_VERSION << 4) | (len >> 2); 10963 tcp->tcp_hdr_len = len + tcph_len; 10964 if (!TCP_IS_DETACHED(tcp)) { 10965 /* Always allocate room for all options. */ 10966 (void) mi_set_sth_wroff(tcp->tcp_rq, 10967 TCP_MAX_COMBINED_HEADER_LENGTH + tcp_wroff_xtra); 10968 } 10969 return (0); 10970 } 10971 10972 /* Get callback routine passed to nd_load by tcp_param_register */ 10973 /* ARGSUSED */ 10974 static int 10975 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 10976 { 10977 tcpparam_t *tcppa = (tcpparam_t *)cp; 10978 10979 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 10980 return (0); 10981 } 10982 10983 /* 10984 * Walk through the param array specified registering each element with the 10985 * named dispatch handler. 10986 */ 10987 static boolean_t 10988 tcp_param_register(tcpparam_t *tcppa, int cnt) 10989 { 10990 for (; cnt-- > 0; tcppa++) { 10991 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 10992 if (!nd_load(&tcp_g_nd, tcppa->tcp_param_name, 10993 tcp_param_get, tcp_param_set, 10994 (caddr_t)tcppa)) { 10995 nd_free(&tcp_g_nd); 10996 return (B_FALSE); 10997 } 10998 } 10999 } 11000 if (!nd_load(&tcp_g_nd, tcp_wroff_xtra_param.tcp_param_name, 11001 tcp_param_get, tcp_param_set_aligned, 11002 (caddr_t)&tcp_wroff_xtra_param)) { 11003 nd_free(&tcp_g_nd); 11004 return (B_FALSE); 11005 } 11006 if (!nd_load(&tcp_g_nd, tcp_mdt_head_param.tcp_param_name, 11007 tcp_param_get, tcp_param_set_aligned, 11008 (caddr_t)&tcp_mdt_head_param)) { 11009 nd_free(&tcp_g_nd); 11010 return (B_FALSE); 11011 } 11012 if (!nd_load(&tcp_g_nd, tcp_mdt_tail_param.tcp_param_name, 11013 tcp_param_get, tcp_param_set_aligned, 11014 (caddr_t)&tcp_mdt_tail_param)) { 11015 nd_free(&tcp_g_nd); 11016 return (B_FALSE); 11017 } 11018 if (!nd_load(&tcp_g_nd, tcp_mdt_max_pbufs_param.tcp_param_name, 11019 tcp_param_get, tcp_param_set, 11020 (caddr_t)&tcp_mdt_max_pbufs_param)) { 11021 nd_free(&tcp_g_nd); 11022 return (B_FALSE); 11023 } 11024 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports", 11025 tcp_extra_priv_ports_get, NULL, NULL)) { 11026 nd_free(&tcp_g_nd); 11027 return (B_FALSE); 11028 } 11029 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_add", 11030 NULL, tcp_extra_priv_ports_add, NULL)) { 11031 nd_free(&tcp_g_nd); 11032 return (B_FALSE); 11033 } 11034 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_del", 11035 NULL, tcp_extra_priv_ports_del, NULL)) { 11036 nd_free(&tcp_g_nd); 11037 return (B_FALSE); 11038 } 11039 if (!nd_load(&tcp_g_nd, "tcp_status", tcp_status_report, NULL, 11040 NULL)) { 11041 nd_free(&tcp_g_nd); 11042 return (B_FALSE); 11043 } 11044 if (!nd_load(&tcp_g_nd, "tcp_bind_hash", tcp_bind_hash_report, 11045 NULL, NULL)) { 11046 nd_free(&tcp_g_nd); 11047 return (B_FALSE); 11048 } 11049 if (!nd_load(&tcp_g_nd, "tcp_listen_hash", tcp_listen_hash_report, 11050 NULL, NULL)) { 11051 nd_free(&tcp_g_nd); 11052 return (B_FALSE); 11053 } 11054 if (!nd_load(&tcp_g_nd, "tcp_conn_hash", tcp_conn_hash_report, 11055 NULL, NULL)) { 11056 nd_free(&tcp_g_nd); 11057 return (B_FALSE); 11058 } 11059 if (!nd_load(&tcp_g_nd, "tcp_acceptor_hash", tcp_acceptor_hash_report, 11060 NULL, NULL)) { 11061 nd_free(&tcp_g_nd); 11062 return (B_FALSE); 11063 } 11064 if (!nd_load(&tcp_g_nd, "tcp_host_param", tcp_host_param_report, 11065 tcp_host_param_set, NULL)) { 11066 nd_free(&tcp_g_nd); 11067 return (B_FALSE); 11068 } 11069 if (!nd_load(&tcp_g_nd, "tcp_host_param_ipv6", tcp_host_param_report, 11070 tcp_host_param_set_ipv6, NULL)) { 11071 nd_free(&tcp_g_nd); 11072 return (B_FALSE); 11073 } 11074 if (!nd_load(&tcp_g_nd, "tcp_1948_phrase", NULL, tcp_1948_phrase_set, 11075 NULL)) { 11076 nd_free(&tcp_g_nd); 11077 return (B_FALSE); 11078 } 11079 if (!nd_load(&tcp_g_nd, "tcp_reserved_port_list", 11080 tcp_reserved_port_list, NULL, NULL)) { 11081 nd_free(&tcp_g_nd); 11082 return (B_FALSE); 11083 } 11084 /* 11085 * Dummy ndd variables - only to convey obsolescence information 11086 * through printing of their name (no get or set routines) 11087 * XXX Remove in future releases ? 11088 */ 11089 if (!nd_load(&tcp_g_nd, 11090 "tcp_close_wait_interval(obsoleted - " 11091 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 11092 nd_free(&tcp_g_nd); 11093 return (B_FALSE); 11094 } 11095 return (B_TRUE); 11096 } 11097 11098 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 11099 /* ARGSUSED */ 11100 static int 11101 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 11102 cred_t *cr) 11103 { 11104 long new_value; 11105 tcpparam_t *tcppa = (tcpparam_t *)cp; 11106 11107 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11108 new_value < tcppa->tcp_param_min || 11109 new_value > tcppa->tcp_param_max) { 11110 return (EINVAL); 11111 } 11112 /* 11113 * Need to make sure new_value is a multiple of 4. If it is not, 11114 * round it up. For future 64 bit requirement, we actually make it 11115 * a multiple of 8. 11116 */ 11117 if (new_value & 0x7) { 11118 new_value = (new_value & ~0x7) + 0x8; 11119 } 11120 tcppa->tcp_param_val = new_value; 11121 return (0); 11122 } 11123 11124 /* Set callback routine passed to nd_load by tcp_param_register */ 11125 /* ARGSUSED */ 11126 static int 11127 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 11128 { 11129 long new_value; 11130 tcpparam_t *tcppa = (tcpparam_t *)cp; 11131 11132 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 11133 new_value < tcppa->tcp_param_min || 11134 new_value > tcppa->tcp_param_max) { 11135 return (EINVAL); 11136 } 11137 tcppa->tcp_param_val = new_value; 11138 return (0); 11139 } 11140 11141 /* 11142 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 11143 * is filled, return as much as we can. The message passed in may be 11144 * multi-part, chained using b_cont. "start" is the starting sequence 11145 * number for this piece. 11146 */ 11147 static mblk_t * 11148 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 11149 { 11150 uint32_t end; 11151 mblk_t *mp1; 11152 mblk_t *mp2; 11153 mblk_t *next_mp; 11154 uint32_t u1; 11155 11156 /* Walk through all the new pieces. */ 11157 do { 11158 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11159 (uintptr_t)INT_MAX); 11160 end = start + (int)(mp->b_wptr - mp->b_rptr); 11161 next_mp = mp->b_cont; 11162 if (start == end) { 11163 /* Empty. Blast it. */ 11164 freeb(mp); 11165 continue; 11166 } 11167 mp->b_cont = NULL; 11168 TCP_REASS_SET_SEQ(mp, start); 11169 TCP_REASS_SET_END(mp, end); 11170 mp1 = tcp->tcp_reass_tail; 11171 if (!mp1) { 11172 tcp->tcp_reass_tail = mp; 11173 tcp->tcp_reass_head = mp; 11174 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11175 UPDATE_MIB(&tcp_mib, 11176 tcpInDataUnorderBytes, end - start); 11177 continue; 11178 } 11179 /* New stuff completely beyond tail? */ 11180 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11181 /* Link it on end. */ 11182 mp1->b_cont = mp; 11183 tcp->tcp_reass_tail = mp; 11184 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11185 UPDATE_MIB(&tcp_mib, 11186 tcpInDataUnorderBytes, end - start); 11187 continue; 11188 } 11189 mp1 = tcp->tcp_reass_head; 11190 u1 = TCP_REASS_SEQ(mp1); 11191 /* New stuff at the front? */ 11192 if (SEQ_LT(start, u1)) { 11193 /* Yes... Check for overlap. */ 11194 mp->b_cont = mp1; 11195 tcp->tcp_reass_head = mp; 11196 tcp_reass_elim_overlap(tcp, mp); 11197 continue; 11198 } 11199 /* 11200 * The new piece fits somewhere between the head and tail. 11201 * We find our slot, where mp1 precedes us and mp2 trails. 11202 */ 11203 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11204 u1 = TCP_REASS_SEQ(mp2); 11205 if (SEQ_LEQ(start, u1)) 11206 break; 11207 } 11208 /* Link ourselves in */ 11209 mp->b_cont = mp2; 11210 mp1->b_cont = mp; 11211 11212 /* Trim overlap with following mblk(s) first */ 11213 tcp_reass_elim_overlap(tcp, mp); 11214 11215 /* Trim overlap with preceding mblk */ 11216 tcp_reass_elim_overlap(tcp, mp1); 11217 11218 } while (start = end, mp = next_mp); 11219 mp1 = tcp->tcp_reass_head; 11220 /* Anything ready to go? */ 11221 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11222 return (NULL); 11223 /* Eat what we can off the queue */ 11224 for (;;) { 11225 mp = mp1->b_cont; 11226 end = TCP_REASS_END(mp1); 11227 TCP_REASS_SET_SEQ(mp1, 0); 11228 TCP_REASS_SET_END(mp1, 0); 11229 if (!mp) { 11230 tcp->tcp_reass_tail = NULL; 11231 break; 11232 } 11233 if (end != TCP_REASS_SEQ(mp)) { 11234 mp1->b_cont = NULL; 11235 break; 11236 } 11237 mp1 = mp; 11238 } 11239 mp1 = tcp->tcp_reass_head; 11240 tcp->tcp_reass_head = mp; 11241 return (mp1); 11242 } 11243 11244 /* Eliminate any overlap that mp may have over later mblks */ 11245 static void 11246 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11247 { 11248 uint32_t end; 11249 mblk_t *mp1; 11250 uint32_t u1; 11251 11252 end = TCP_REASS_END(mp); 11253 while ((mp1 = mp->b_cont) != NULL) { 11254 u1 = TCP_REASS_SEQ(mp1); 11255 if (!SEQ_GT(end, u1)) 11256 break; 11257 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11258 mp->b_wptr -= end - u1; 11259 TCP_REASS_SET_END(mp, u1); 11260 BUMP_MIB(&tcp_mib, tcpInDataPartDupSegs); 11261 UPDATE_MIB(&tcp_mib, tcpInDataPartDupBytes, end - u1); 11262 break; 11263 } 11264 mp->b_cont = mp1->b_cont; 11265 TCP_REASS_SET_SEQ(mp1, 0); 11266 TCP_REASS_SET_END(mp1, 0); 11267 freeb(mp1); 11268 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 11269 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, end - u1); 11270 } 11271 if (!mp1) 11272 tcp->tcp_reass_tail = mp; 11273 } 11274 11275 /* 11276 * Send up all messages queued on tcp_rcv_list. 11277 */ 11278 static uint_t 11279 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11280 { 11281 mblk_t *mp; 11282 uint_t ret = 0; 11283 uint_t thwin; 11284 #ifdef DEBUG 11285 uint_t cnt = 0; 11286 #endif 11287 /* Can't drain on an eager connection */ 11288 if (tcp->tcp_listener != NULL) 11289 return (ret); 11290 11291 /* 11292 * Handle two cases here: we are currently fused or we were 11293 * previously fused and have some urgent data to be delivered 11294 * upstream. The latter happens because we either ran out of 11295 * memory or were detached and therefore sending the SIGURG was 11296 * deferred until this point. In either case we pass control 11297 * over to tcp_fuse_rcv_drain() since it may need to complete 11298 * some work. 11299 */ 11300 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11301 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11302 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11303 &tcp->tcp_fused_sigurg_mp)) 11304 return (ret); 11305 } 11306 11307 while ((mp = tcp->tcp_rcv_list) != NULL) { 11308 tcp->tcp_rcv_list = mp->b_next; 11309 mp->b_next = NULL; 11310 #ifdef DEBUG 11311 cnt += msgdsize(mp); 11312 #endif 11313 /* Does this need SSL processing first? */ 11314 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11315 tcp_kssl_input(tcp, mp); 11316 continue; 11317 } 11318 putnext(q, mp); 11319 } 11320 ASSERT(cnt == tcp->tcp_rcv_cnt); 11321 tcp->tcp_rcv_last_head = NULL; 11322 tcp->tcp_rcv_last_tail = NULL; 11323 tcp->tcp_rcv_cnt = 0; 11324 11325 /* Learn the latest rwnd information that we sent to the other side. */ 11326 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11327 << tcp->tcp_rcv_ws; 11328 /* This is peer's calculated send window (our receive window). */ 11329 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11330 /* 11331 * Increase the receive window to max. But we need to do receiver 11332 * SWS avoidance. This means that we need to check the increase of 11333 * of receive window is at least 1 MSS. 11334 */ 11335 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11336 /* 11337 * If the window that the other side knows is less than max 11338 * deferred acks segments, send an update immediately. 11339 */ 11340 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11341 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 11342 ret = TH_ACK_NEEDED; 11343 } 11344 tcp->tcp_rwnd = q->q_hiwat; 11345 } 11346 /* No need for the push timer now. */ 11347 if (tcp->tcp_push_tid != 0) { 11348 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11349 tcp->tcp_push_tid = 0; 11350 } 11351 return (ret); 11352 } 11353 11354 /* 11355 * Queue data on tcp_rcv_list which is a b_next chain. 11356 * tcp_rcv_last_head/tail is the last element of this chain. 11357 * Each element of the chain is a b_cont chain. 11358 * 11359 * M_DATA messages are added to the current element. 11360 * Other messages are added as new (b_next) elements. 11361 */ 11362 void 11363 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11364 { 11365 ASSERT(seg_len == msgdsize(mp)); 11366 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11367 11368 if (tcp->tcp_rcv_list == NULL) { 11369 ASSERT(tcp->tcp_rcv_last_head == NULL); 11370 tcp->tcp_rcv_list = mp; 11371 tcp->tcp_rcv_last_head = mp; 11372 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11373 tcp->tcp_rcv_last_tail->b_cont = mp; 11374 } else { 11375 tcp->tcp_rcv_last_head->b_next = mp; 11376 tcp->tcp_rcv_last_head = mp; 11377 } 11378 11379 while (mp->b_cont) 11380 mp = mp->b_cont; 11381 11382 tcp->tcp_rcv_last_tail = mp; 11383 tcp->tcp_rcv_cnt += seg_len; 11384 tcp->tcp_rwnd -= seg_len; 11385 } 11386 11387 /* 11388 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11389 * 11390 * This is the default entry function into TCP on the read side. TCP is 11391 * always entered via squeue i.e. using squeue's for mutual exclusion. 11392 * When classifier does a lookup to find the tcp, it also puts a reference 11393 * on the conn structure associated so the tcp is guaranteed to exist 11394 * when we come here. We still need to check the state because it might 11395 * as well has been closed. The squeue processing function i.e. squeue_enter, 11396 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11397 * CONN_DEC_REF. 11398 * 11399 * Apart from the default entry point, IP also sends packets directly to 11400 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11401 * connections. 11402 */ 11403 void 11404 tcp_input(void *arg, mblk_t *mp, void *arg2) 11405 { 11406 conn_t *connp = (conn_t *)arg; 11407 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11408 11409 /* arg2 is the sqp */ 11410 ASSERT(arg2 != NULL); 11411 ASSERT(mp != NULL); 11412 11413 /* 11414 * Don't accept any input on a closed tcp as this TCP logically does 11415 * not exist on the system. Don't proceed further with this TCP. 11416 * For eg. this packet could trigger another close of this tcp 11417 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11418 * tcp_clean_death / tcp_closei_local must be called at most once 11419 * on a TCP. In this case we need to refeed the packet into the 11420 * classifier and figure out where the packet should go. Need to 11421 * preserve the recv_ill somehow. Until we figure that out, for 11422 * now just drop the packet if we can't classify the packet. 11423 */ 11424 if (tcp->tcp_state == TCPS_CLOSED || 11425 tcp->tcp_state == TCPS_BOUND) { 11426 conn_t *new_connp; 11427 11428 new_connp = ipcl_classify(mp, connp->conn_zoneid); 11429 if (new_connp != NULL) { 11430 tcp_reinput(new_connp, mp, arg2); 11431 return; 11432 } 11433 /* We failed to classify. For now just drop the packet */ 11434 freemsg(mp); 11435 return; 11436 } 11437 11438 if (DB_TYPE(mp) == M_DATA) 11439 tcp_rput_data(connp, mp, arg2); 11440 else 11441 tcp_rput_common(tcp, mp); 11442 } 11443 11444 /* 11445 * The read side put procedure. 11446 * The packets passed up by ip are assume to be aligned according to 11447 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11448 */ 11449 static void 11450 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11451 { 11452 /* 11453 * tcp_rput_data() does not expect M_CTL except for the case 11454 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11455 * type. Need to make sure that any other M_CTLs don't make 11456 * it to tcp_rput_data since it is not expecting any and doesn't 11457 * check for it. 11458 */ 11459 if (DB_TYPE(mp) == M_CTL) { 11460 switch (*(uint32_t *)(mp->b_rptr)) { 11461 case TCP_IOC_ABORT_CONN: 11462 /* 11463 * Handle connection abort request. 11464 */ 11465 tcp_ioctl_abort_handler(tcp, mp); 11466 return; 11467 case IPSEC_IN: 11468 /* 11469 * Only secure icmp arrive in TCP and they 11470 * don't go through data path. 11471 */ 11472 tcp_icmp_error(tcp, mp); 11473 return; 11474 case IN_PKTINFO: 11475 /* 11476 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11477 * sockets that are receiving IPv4 traffic. tcp 11478 */ 11479 ASSERT(tcp->tcp_family == AF_INET6); 11480 ASSERT(tcp->tcp_ipv6_recvancillary & 11481 TCP_IPV6_RECVPKTINFO); 11482 tcp_rput_data(tcp->tcp_connp, mp, 11483 tcp->tcp_connp->conn_sqp); 11484 return; 11485 case MDT_IOC_INFO_UPDATE: 11486 /* 11487 * Handle Multidata information update; the 11488 * following routine will free the message. 11489 */ 11490 if (tcp->tcp_connp->conn_mdt_ok) { 11491 tcp_mdt_update(tcp, 11492 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11493 B_FALSE); 11494 } 11495 freemsg(mp); 11496 return; 11497 default: 11498 break; 11499 } 11500 } 11501 11502 /* No point processing the message if tcp is already closed */ 11503 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11504 freemsg(mp); 11505 return; 11506 } 11507 11508 tcp_rput_other(tcp, mp); 11509 } 11510 11511 11512 /* The minimum of smoothed mean deviation in RTO calculation. */ 11513 #define TCP_SD_MIN 400 11514 11515 /* 11516 * Set RTO for this connection. The formula is from Jacobson and Karels' 11517 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11518 * are the same as those in Appendix A.2 of that paper. 11519 * 11520 * m = new measurement 11521 * sa = smoothed RTT average (8 * average estimates). 11522 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11523 */ 11524 static void 11525 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11526 { 11527 long m = TICK_TO_MSEC(rtt); 11528 clock_t sa = tcp->tcp_rtt_sa; 11529 clock_t sv = tcp->tcp_rtt_sd; 11530 clock_t rto; 11531 11532 BUMP_MIB(&tcp_mib, tcpRttUpdate); 11533 tcp->tcp_rtt_update++; 11534 11535 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11536 if (sa != 0) { 11537 /* 11538 * Update average estimator: 11539 * new rtt = 7/8 old rtt + 1/8 Error 11540 */ 11541 11542 /* m is now Error in estimate. */ 11543 m -= sa >> 3; 11544 if ((sa += m) <= 0) { 11545 /* 11546 * Don't allow the smoothed average to be negative. 11547 * We use 0 to denote reinitialization of the 11548 * variables. 11549 */ 11550 sa = 1; 11551 } 11552 11553 /* 11554 * Update deviation estimator: 11555 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11556 */ 11557 if (m < 0) 11558 m = -m; 11559 m -= sv >> 2; 11560 sv += m; 11561 } else { 11562 /* 11563 * This follows BSD's implementation. So the reinitialized 11564 * RTO is 3 * m. We cannot go less than 2 because if the 11565 * link is bandwidth dominated, doubling the window size 11566 * during slow start means doubling the RTT. We want to be 11567 * more conservative when we reinitialize our estimates. 3 11568 * is just a convenient number. 11569 */ 11570 sa = m << 3; 11571 sv = m << 1; 11572 } 11573 if (sv < TCP_SD_MIN) { 11574 /* 11575 * We do not know that if sa captures the delay ACK 11576 * effect as in a long train of segments, a receiver 11577 * does not delay its ACKs. So set the minimum of sv 11578 * to be TCP_SD_MIN, which is default to 400 ms, twice 11579 * of BSD DATO. That means the minimum of mean 11580 * deviation is 100 ms. 11581 * 11582 */ 11583 sv = TCP_SD_MIN; 11584 } 11585 tcp->tcp_rtt_sa = sa; 11586 tcp->tcp_rtt_sd = sv; 11587 /* 11588 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11589 * 11590 * Add tcp_rexmit_interval extra in case of extreme environment 11591 * where the algorithm fails to work. The default value of 11592 * tcp_rexmit_interval_extra should be 0. 11593 * 11594 * As we use a finer grained clock than BSD and update 11595 * RTO for every ACKs, add in another .25 of RTT to the 11596 * deviation of RTO to accomodate burstiness of 1/4 of 11597 * window size. 11598 */ 11599 rto = (sa >> 3) + sv + tcp_rexmit_interval_extra + (sa >> 5); 11600 11601 if (rto > tcp_rexmit_interval_max) { 11602 tcp->tcp_rto = tcp_rexmit_interval_max; 11603 } else if (rto < tcp_rexmit_interval_min) { 11604 tcp->tcp_rto = tcp_rexmit_interval_min; 11605 } else { 11606 tcp->tcp_rto = rto; 11607 } 11608 11609 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11610 tcp->tcp_timer_backoff = 0; 11611 } 11612 11613 /* 11614 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11615 * send queue which starts at the given seq. no. 11616 * 11617 * Parameters: 11618 * tcp_t *tcp: the tcp instance pointer. 11619 * uint32_t seq: the starting seq. no of the requested segment. 11620 * int32_t *off: after the execution, *off will be the offset to 11621 * the returned mblk which points to the requested seq no. 11622 * It is the caller's responsibility to send in a non-null off. 11623 * 11624 * Return: 11625 * A mblk_t pointer pointing to the requested segment in send queue. 11626 */ 11627 static mblk_t * 11628 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11629 { 11630 int32_t cnt; 11631 mblk_t *mp; 11632 11633 /* Defensive coding. Make sure we don't send incorrect data. */ 11634 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11635 return (NULL); 11636 11637 cnt = seq - tcp->tcp_suna; 11638 mp = tcp->tcp_xmit_head; 11639 while (cnt > 0 && mp != NULL) { 11640 cnt -= mp->b_wptr - mp->b_rptr; 11641 if (cnt < 0) { 11642 cnt += mp->b_wptr - mp->b_rptr; 11643 break; 11644 } 11645 mp = mp->b_cont; 11646 } 11647 ASSERT(mp != NULL); 11648 *off = cnt; 11649 return (mp); 11650 } 11651 11652 /* 11653 * This function handles all retransmissions if SACK is enabled for this 11654 * connection. First it calculates how many segments can be retransmitted 11655 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11656 * segments. A segment is eligible if sack_cnt for that segment is greater 11657 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11658 * all eligible segments, it checks to see if TCP can send some new segments 11659 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11660 * 11661 * Parameters: 11662 * tcp_t *tcp: the tcp structure of the connection. 11663 * uint_t *flags: in return, appropriate value will be set for 11664 * tcp_rput_data(). 11665 */ 11666 static void 11667 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11668 { 11669 notsack_blk_t *notsack_blk; 11670 int32_t usable_swnd; 11671 int32_t mss; 11672 uint32_t seg_len; 11673 mblk_t *xmit_mp; 11674 11675 ASSERT(tcp->tcp_sack_info != NULL); 11676 ASSERT(tcp->tcp_notsack_list != NULL); 11677 ASSERT(tcp->tcp_rexmit == B_FALSE); 11678 11679 /* Defensive coding in case there is a bug... */ 11680 if (tcp->tcp_notsack_list == NULL) { 11681 return; 11682 } 11683 notsack_blk = tcp->tcp_notsack_list; 11684 mss = tcp->tcp_mss; 11685 11686 /* 11687 * Limit the num of outstanding data in the network to be 11688 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11689 */ 11690 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11691 11692 /* At least retransmit 1 MSS of data. */ 11693 if (usable_swnd <= 0) { 11694 usable_swnd = mss; 11695 } 11696 11697 /* Make sure no new RTT samples will be taken. */ 11698 tcp->tcp_csuna = tcp->tcp_snxt; 11699 11700 notsack_blk = tcp->tcp_notsack_list; 11701 while (usable_swnd > 0) { 11702 mblk_t *snxt_mp, *tmp_mp; 11703 tcp_seq begin = tcp->tcp_sack_snxt; 11704 tcp_seq end; 11705 int32_t off; 11706 11707 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11708 if (SEQ_GT(notsack_blk->end, begin) && 11709 (notsack_blk->sack_cnt >= 11710 tcp_dupack_fast_retransmit)) { 11711 end = notsack_blk->end; 11712 if (SEQ_LT(begin, notsack_blk->begin)) { 11713 begin = notsack_blk->begin; 11714 } 11715 break; 11716 } 11717 } 11718 /* 11719 * All holes are filled. Manipulate tcp_cwnd to send more 11720 * if we can. Note that after the SACK recovery, tcp_cwnd is 11721 * set to tcp_cwnd_ssthresh. 11722 */ 11723 if (notsack_blk == NULL) { 11724 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11725 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11726 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11727 ASSERT(tcp->tcp_cwnd > 0); 11728 return; 11729 } else { 11730 usable_swnd = usable_swnd / mss; 11731 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11732 MAX(usable_swnd * mss, mss); 11733 *flags |= TH_XMIT_NEEDED; 11734 return; 11735 } 11736 } 11737 11738 /* 11739 * Note that we may send more than usable_swnd allows here 11740 * because of round off, but no more than 1 MSS of data. 11741 */ 11742 seg_len = end - begin; 11743 if (seg_len > mss) 11744 seg_len = mss; 11745 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11746 ASSERT(snxt_mp != NULL); 11747 /* This should not happen. Defensive coding again... */ 11748 if (snxt_mp == NULL) { 11749 return; 11750 } 11751 11752 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11753 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11754 if (xmit_mp == NULL) 11755 return; 11756 11757 usable_swnd -= seg_len; 11758 tcp->tcp_pipe += seg_len; 11759 tcp->tcp_sack_snxt = begin + seg_len; 11760 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 11761 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11762 11763 /* 11764 * Update the send timestamp to avoid false retransmission. 11765 */ 11766 snxt_mp->b_prev = (mblk_t *)lbolt; 11767 11768 BUMP_MIB(&tcp_mib, tcpRetransSegs); 11769 UPDATE_MIB(&tcp_mib, tcpRetransBytes, seg_len); 11770 BUMP_MIB(&tcp_mib, tcpOutSackRetransSegs); 11771 /* 11772 * Update tcp_rexmit_max to extend this SACK recovery phase. 11773 * This happens when new data sent during fast recovery is 11774 * also lost. If TCP retransmits those new data, it needs 11775 * to extend SACK recover phase to avoid starting another 11776 * fast retransmit/recovery unnecessarily. 11777 */ 11778 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11779 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11780 } 11781 } 11782 } 11783 11784 /* 11785 * This function handles policy checking at TCP level for non-hard_bound/ 11786 * detached connections. 11787 */ 11788 static boolean_t 11789 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 11790 boolean_t secure, boolean_t mctl_present) 11791 { 11792 ipsec_latch_t *ipl = NULL; 11793 ipsec_action_t *act = NULL; 11794 mblk_t *data_mp; 11795 ipsec_in_t *ii; 11796 const char *reason; 11797 kstat_named_t *counter; 11798 11799 ASSERT(mctl_present || !secure); 11800 11801 ASSERT((ipha == NULL && ip6h != NULL) || 11802 (ip6h == NULL && ipha != NULL)); 11803 11804 /* 11805 * We don't necessarily have an ipsec_in_act action to verify 11806 * policy because of assymetrical policy where we have only 11807 * outbound policy and no inbound policy (possible with global 11808 * policy). 11809 */ 11810 if (!secure) { 11811 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 11812 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 11813 return (B_TRUE); 11814 ipsec_log_policy_failure(tcp->tcp_wq, IPSEC_POLICY_MISMATCH, 11815 "tcp_check_policy", ipha, ip6h, secure); 11816 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11817 &ipdrops_tcp_clear, &tcp_dropper); 11818 return (B_FALSE); 11819 } 11820 11821 /* 11822 * We have a secure packet. 11823 */ 11824 if (act == NULL) { 11825 ipsec_log_policy_failure(tcp->tcp_wq, 11826 IPSEC_POLICY_NOT_NEEDED, "tcp_check_policy", ipha, ip6h, 11827 secure); 11828 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11829 &ipdrops_tcp_secure, &tcp_dropper); 11830 return (B_FALSE); 11831 } 11832 11833 /* 11834 * XXX This whole routine is currently incorrect. ipl should 11835 * be set to the latch pointer, but is currently not set, so 11836 * we initialize it to NULL to avoid picking up random garbage. 11837 */ 11838 if (ipl == NULL) 11839 return (B_TRUE); 11840 11841 data_mp = first_mp->b_cont; 11842 11843 ii = (ipsec_in_t *)first_mp->b_rptr; 11844 11845 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 11846 &counter)) { 11847 BUMP_MIB(&ip_mib, ipsecInSucceeded); 11848 return (B_TRUE); 11849 } 11850 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 11851 "tcp inbound policy mismatch: %s, packet dropped\n", 11852 reason); 11853 BUMP_MIB(&ip_mib, ipsecInFailed); 11854 11855 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, &tcp_dropper); 11856 return (B_FALSE); 11857 } 11858 11859 /* 11860 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 11861 * retransmission after a timeout. 11862 * 11863 * To limit the number of duplicate segments, we limit the number of segment 11864 * to be sent in one time to tcp_snd_burst, the burst variable. 11865 */ 11866 static void 11867 tcp_ss_rexmit(tcp_t *tcp) 11868 { 11869 uint32_t snxt; 11870 uint32_t smax; 11871 int32_t win; 11872 int32_t mss; 11873 int32_t off; 11874 int32_t burst = tcp->tcp_snd_burst; 11875 mblk_t *snxt_mp; 11876 11877 /* 11878 * Note that tcp_rexmit can be set even though TCP has retransmitted 11879 * all unack'ed segments. 11880 */ 11881 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 11882 smax = tcp->tcp_rexmit_max; 11883 snxt = tcp->tcp_rexmit_nxt; 11884 if (SEQ_LT(snxt, tcp->tcp_suna)) { 11885 snxt = tcp->tcp_suna; 11886 } 11887 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 11888 win -= snxt - tcp->tcp_suna; 11889 mss = tcp->tcp_mss; 11890 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 11891 11892 while (SEQ_LT(snxt, smax) && (win > 0) && 11893 (burst > 0) && (snxt_mp != NULL)) { 11894 mblk_t *xmit_mp; 11895 mblk_t *old_snxt_mp = snxt_mp; 11896 uint32_t cnt = mss; 11897 11898 if (win < cnt) { 11899 cnt = win; 11900 } 11901 if (SEQ_GT(snxt + cnt, smax)) { 11902 cnt = smax - snxt; 11903 } 11904 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 11905 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 11906 if (xmit_mp == NULL) 11907 return; 11908 11909 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11910 11911 snxt += cnt; 11912 win -= cnt; 11913 /* 11914 * Update the send timestamp to avoid false 11915 * retransmission. 11916 */ 11917 old_snxt_mp->b_prev = (mblk_t *)lbolt; 11918 BUMP_MIB(&tcp_mib, tcpRetransSegs); 11919 UPDATE_MIB(&tcp_mib, tcpRetransBytes, cnt); 11920 11921 tcp->tcp_rexmit_nxt = snxt; 11922 burst--; 11923 } 11924 /* 11925 * If we have transmitted all we have at the time 11926 * we started the retranmission, we can leave 11927 * the rest of the job to tcp_wput_data(). But we 11928 * need to check the send window first. If the 11929 * win is not 0, go on with tcp_wput_data(). 11930 */ 11931 if (SEQ_LT(snxt, smax) || win == 0) { 11932 return; 11933 } 11934 } 11935 /* Only call tcp_wput_data() if there is data to be sent. */ 11936 if (tcp->tcp_unsent) { 11937 tcp_wput_data(tcp, NULL, B_FALSE); 11938 } 11939 } 11940 11941 /* 11942 * Process all TCP option in SYN segment. Note that this function should 11943 * be called after tcp_adapt_ire() is called so that the necessary info 11944 * from IRE is already set in the tcp structure. 11945 * 11946 * This function sets up the correct tcp_mss value according to the 11947 * MSS option value and our header size. It also sets up the window scale 11948 * and timestamp values, and initialize SACK info blocks. But it does not 11949 * change receive window size after setting the tcp_mss value. The caller 11950 * should do the appropriate change. 11951 */ 11952 void 11953 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 11954 { 11955 int options; 11956 tcp_opt_t tcpopt; 11957 uint32_t mss_max; 11958 char *tmp_tcph; 11959 11960 tcpopt.tcp = NULL; 11961 options = tcp_parse_options(tcph, &tcpopt); 11962 11963 /* 11964 * Process MSS option. Note that MSS option value does not account 11965 * for IP or TCP options. This means that it is equal to MTU - minimum 11966 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 11967 * IPv6. 11968 */ 11969 if (!(options & TCP_OPT_MSS_PRESENT)) { 11970 if (tcp->tcp_ipversion == IPV4_VERSION) 11971 tcpopt.tcp_opt_mss = tcp_mss_def_ipv4; 11972 else 11973 tcpopt.tcp_opt_mss = tcp_mss_def_ipv6; 11974 } else { 11975 if (tcp->tcp_ipversion == IPV4_VERSION) 11976 mss_max = tcp_mss_max_ipv4; 11977 else 11978 mss_max = tcp_mss_max_ipv6; 11979 if (tcpopt.tcp_opt_mss < tcp_mss_min) 11980 tcpopt.tcp_opt_mss = tcp_mss_min; 11981 else if (tcpopt.tcp_opt_mss > mss_max) 11982 tcpopt.tcp_opt_mss = mss_max; 11983 } 11984 11985 /* Process Window Scale option. */ 11986 if (options & TCP_OPT_WSCALE_PRESENT) { 11987 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 11988 tcp->tcp_snd_ws_ok = B_TRUE; 11989 } else { 11990 tcp->tcp_snd_ws = B_FALSE; 11991 tcp->tcp_snd_ws_ok = B_FALSE; 11992 tcp->tcp_rcv_ws = B_FALSE; 11993 } 11994 11995 /* Process Timestamp option. */ 11996 if ((options & TCP_OPT_TSTAMP_PRESENT) && 11997 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 11998 tmp_tcph = (char *)tcp->tcp_tcph; 11999 12000 tcp->tcp_snd_ts_ok = B_TRUE; 12001 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 12002 tcp->tcp_last_rcv_lbolt = lbolt64; 12003 ASSERT(OK_32PTR(tmp_tcph)); 12004 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 12005 12006 /* Fill in our template header with basic timestamp option. */ 12007 tmp_tcph += tcp->tcp_tcp_hdr_len; 12008 tmp_tcph[0] = TCPOPT_NOP; 12009 tmp_tcph[1] = TCPOPT_NOP; 12010 tmp_tcph[2] = TCPOPT_TSTAMP; 12011 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 12012 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12013 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 12014 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 12015 } else { 12016 tcp->tcp_snd_ts_ok = B_FALSE; 12017 } 12018 12019 /* 12020 * Process SACK options. If SACK is enabled for this connection, 12021 * then allocate the SACK info structure. Note the following ways 12022 * when tcp_snd_sack_ok is set to true. 12023 * 12024 * For active connection: in tcp_adapt_ire() called in 12025 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 12026 * is checked. 12027 * 12028 * For passive connection: in tcp_adapt_ire() called in 12029 * tcp_accept_comm(). 12030 * 12031 * That's the reason why the extra TCP_IS_DETACHED() check is there. 12032 * That check makes sure that if we did not send a SACK OK option, 12033 * we will not enable SACK for this connection even though the other 12034 * side sends us SACK OK option. For active connection, the SACK 12035 * info structure has already been allocated. So we need to free 12036 * it if SACK is disabled. 12037 */ 12038 if ((options & TCP_OPT_SACK_OK_PRESENT) && 12039 (tcp->tcp_snd_sack_ok || 12040 (tcp_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 12041 /* This should be true only in the passive case. */ 12042 if (tcp->tcp_sack_info == NULL) { 12043 ASSERT(TCP_IS_DETACHED(tcp)); 12044 tcp->tcp_sack_info = 12045 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 12046 } 12047 if (tcp->tcp_sack_info == NULL) { 12048 tcp->tcp_snd_sack_ok = B_FALSE; 12049 } else { 12050 tcp->tcp_snd_sack_ok = B_TRUE; 12051 if (tcp->tcp_snd_ts_ok) { 12052 tcp->tcp_max_sack_blk = 3; 12053 } else { 12054 tcp->tcp_max_sack_blk = 4; 12055 } 12056 } 12057 } else { 12058 /* 12059 * Resetting tcp_snd_sack_ok to B_FALSE so that 12060 * no SACK info will be used for this 12061 * connection. This assumes that SACK usage 12062 * permission is negotiated. This may need 12063 * to be changed once this is clarified. 12064 */ 12065 if (tcp->tcp_sack_info != NULL) { 12066 ASSERT(tcp->tcp_notsack_list == NULL); 12067 kmem_cache_free(tcp_sack_info_cache, 12068 tcp->tcp_sack_info); 12069 tcp->tcp_sack_info = NULL; 12070 } 12071 tcp->tcp_snd_sack_ok = B_FALSE; 12072 } 12073 12074 /* 12075 * Now we know the exact TCP/IP header length, subtract 12076 * that from tcp_mss to get our side's MSS. 12077 */ 12078 tcp->tcp_mss -= tcp->tcp_hdr_len; 12079 /* 12080 * Here we assume that the other side's header size will be equal to 12081 * our header size. We calculate the real MSS accordingly. Need to 12082 * take into additional stuffs IPsec puts in. 12083 * 12084 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 12085 */ 12086 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 12087 ((tcp->tcp_ipversion == IPV4_VERSION ? 12088 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 12089 12090 /* 12091 * Set MSS to the smaller one of both ends of the connection. 12092 * We should not have called tcp_mss_set() before, but our 12093 * side of the MSS should have been set to a proper value 12094 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 12095 * STREAM head parameters properly. 12096 * 12097 * If we have a larger-than-16-bit window but the other side 12098 * didn't want to do window scale, tcp_rwnd_set() will take 12099 * care of that. 12100 */ 12101 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss)); 12102 } 12103 12104 /* 12105 * Sends the T_CONN_IND to the listener. The caller calls this 12106 * functions via squeue to get inside the listener's perimeter 12107 * once the 3 way hand shake is done a T_CONN_IND needs to be 12108 * sent. As an optimization, the caller can call this directly 12109 * if listener's perimeter is same as eager's. 12110 */ 12111 /* ARGSUSED */ 12112 void 12113 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 12114 { 12115 conn_t *lconnp = (conn_t *)arg; 12116 tcp_t *listener = lconnp->conn_tcp; 12117 tcp_t *tcp; 12118 struct T_conn_ind *conn_ind; 12119 ipaddr_t *addr_cache; 12120 boolean_t need_send_conn_ind = B_FALSE; 12121 12122 /* retrieve the eager */ 12123 conn_ind = (struct T_conn_ind *)mp->b_rptr; 12124 ASSERT(conn_ind->OPT_offset != 0 && 12125 conn_ind->OPT_length == sizeof (intptr_t)); 12126 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 12127 conn_ind->OPT_length); 12128 12129 /* 12130 * TLI/XTI applications will get confused by 12131 * sending eager as an option since it violates 12132 * the option semantics. So remove the eager as 12133 * option since TLI/XTI app doesn't need it anyway. 12134 */ 12135 if (!TCP_IS_SOCKET(listener)) { 12136 conn_ind->OPT_length = 0; 12137 conn_ind->OPT_offset = 0; 12138 } 12139 if (listener->tcp_state == TCPS_CLOSED || 12140 TCP_IS_DETACHED(listener)) { 12141 /* 12142 * If listener has closed, it would have caused a 12143 * a cleanup/blowoff to happen for the eager. We 12144 * just need to return. 12145 */ 12146 freemsg(mp); 12147 return; 12148 } 12149 12150 12151 /* 12152 * if the conn_req_q is full defer passing up the 12153 * T_CONN_IND until space is availabe after t_accept() 12154 * processing 12155 */ 12156 mutex_enter(&listener->tcp_eager_lock); 12157 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12158 tcp_t *tail; 12159 12160 /* 12161 * The eager already has an extra ref put in tcp_rput_data 12162 * so that it stays till accept comes back even though it 12163 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12164 */ 12165 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12166 listener->tcp_conn_req_cnt_q0--; 12167 listener->tcp_conn_req_cnt_q++; 12168 12169 /* Move from SYN_RCVD to ESTABLISHED list */ 12170 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12171 tcp->tcp_eager_prev_q0; 12172 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12173 tcp->tcp_eager_next_q0; 12174 tcp->tcp_eager_prev_q0 = NULL; 12175 tcp->tcp_eager_next_q0 = NULL; 12176 12177 /* 12178 * Insert at end of the queue because sockfs 12179 * sends down T_CONN_RES in chronological 12180 * order. Leaving the older conn indications 12181 * at front of the queue helps reducing search 12182 * time. 12183 */ 12184 tail = listener->tcp_eager_last_q; 12185 if (tail != NULL) 12186 tail->tcp_eager_next_q = tcp; 12187 else 12188 listener->tcp_eager_next_q = tcp; 12189 listener->tcp_eager_last_q = tcp; 12190 tcp->tcp_eager_next_q = NULL; 12191 /* 12192 * Delay sending up the T_conn_ind until we are 12193 * done with the eager. Once we have have sent up 12194 * the T_conn_ind, the accept can potentially complete 12195 * any time and release the refhold we have on the eager. 12196 */ 12197 need_send_conn_ind = B_TRUE; 12198 } else { 12199 /* 12200 * Defer connection on q0 and set deferred 12201 * connection bit true 12202 */ 12203 tcp->tcp_conn_def_q0 = B_TRUE; 12204 12205 /* take tcp out of q0 ... */ 12206 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12207 tcp->tcp_eager_next_q0; 12208 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12209 tcp->tcp_eager_prev_q0; 12210 12211 /* ... and place it at the end of q0 */ 12212 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12213 tcp->tcp_eager_next_q0 = listener; 12214 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12215 listener->tcp_eager_prev_q0 = tcp; 12216 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12217 } 12218 12219 /* we have timed out before */ 12220 if (tcp->tcp_syn_rcvd_timeout != 0) { 12221 tcp->tcp_syn_rcvd_timeout = 0; 12222 listener->tcp_syn_rcvd_timeout--; 12223 if (listener->tcp_syn_defense && 12224 listener->tcp_syn_rcvd_timeout <= 12225 (tcp_conn_req_max_q0 >> 5) && 12226 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12227 listener->tcp_last_rcv_lbolt)) { 12228 /* 12229 * Turn off the defense mode if we 12230 * believe the SYN attack is over. 12231 */ 12232 listener->tcp_syn_defense = B_FALSE; 12233 if (listener->tcp_ip_addr_cache) { 12234 kmem_free((void *)listener->tcp_ip_addr_cache, 12235 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12236 listener->tcp_ip_addr_cache = NULL; 12237 } 12238 } 12239 } 12240 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12241 if (addr_cache != NULL) { 12242 /* 12243 * We have finished a 3-way handshake with this 12244 * remote host. This proves the IP addr is good. 12245 * Cache it! 12246 */ 12247 addr_cache[IP_ADDR_CACHE_HASH( 12248 tcp->tcp_remote)] = tcp->tcp_remote; 12249 } 12250 mutex_exit(&listener->tcp_eager_lock); 12251 if (need_send_conn_ind) 12252 putnext(listener->tcp_rq, mp); 12253 } 12254 12255 mblk_t * 12256 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12257 uint_t *ifindexp, ip6_pkt_t *ippp) 12258 { 12259 in_pktinfo_t *pinfo; 12260 ip6_t *ip6h; 12261 uchar_t *rptr; 12262 mblk_t *first_mp = mp; 12263 boolean_t mctl_present = B_FALSE; 12264 uint_t ifindex = 0; 12265 ip6_pkt_t ipp; 12266 uint_t ipvers; 12267 uint_t ip_hdr_len; 12268 12269 rptr = mp->b_rptr; 12270 ASSERT(OK_32PTR(rptr)); 12271 ASSERT(tcp != NULL); 12272 ipp.ipp_fields = 0; 12273 12274 switch DB_TYPE(mp) { 12275 case M_CTL: 12276 mp = mp->b_cont; 12277 if (mp == NULL) { 12278 freemsg(first_mp); 12279 return (NULL); 12280 } 12281 if (DB_TYPE(mp) != M_DATA) { 12282 freemsg(first_mp); 12283 return (NULL); 12284 } 12285 mctl_present = B_TRUE; 12286 break; 12287 case M_DATA: 12288 break; 12289 default: 12290 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12291 freemsg(mp); 12292 return (NULL); 12293 } 12294 ipvers = IPH_HDR_VERSION(rptr); 12295 if (ipvers == IPV4_VERSION) { 12296 if (tcp == NULL) { 12297 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12298 goto done; 12299 } 12300 12301 ipp.ipp_fields |= IPPF_HOPLIMIT; 12302 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12303 12304 /* 12305 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12306 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12307 */ 12308 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12309 mctl_present) { 12310 pinfo = (in_pktinfo_t *)first_mp->b_rptr; 12311 if ((MBLKL(first_mp) == sizeof (in_pktinfo_t)) && 12312 (pinfo->in_pkt_ulp_type == IN_PKTINFO) && 12313 (pinfo->in_pkt_flags & IPF_RECVIF)) { 12314 ipp.ipp_fields |= IPPF_IFINDEX; 12315 ipp.ipp_ifindex = pinfo->in_pkt_ifindex; 12316 ifindex = pinfo->in_pkt_ifindex; 12317 } 12318 freeb(first_mp); 12319 mctl_present = B_FALSE; 12320 } 12321 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12322 } else { 12323 ip6h = (ip6_t *)rptr; 12324 12325 ASSERT(ipvers == IPV6_VERSION); 12326 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12327 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12328 ipp.ipp_hoplimit = ip6h->ip6_hops; 12329 12330 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12331 uint8_t nexthdrp; 12332 12333 /* Look for ifindex information */ 12334 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12335 ip6i_t *ip6i = (ip6i_t *)ip6h; 12336 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12337 BUMP_MIB(&ip_mib, tcpInErrs); 12338 freemsg(first_mp); 12339 return (NULL); 12340 } 12341 12342 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12343 ASSERT(ip6i->ip6i_ifindex != 0); 12344 ipp.ipp_fields |= IPPF_IFINDEX; 12345 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12346 ifindex = ip6i->ip6i_ifindex; 12347 } 12348 rptr = (uchar_t *)&ip6i[1]; 12349 mp->b_rptr = rptr; 12350 if (rptr == mp->b_wptr) { 12351 mblk_t *mp1; 12352 mp1 = mp->b_cont; 12353 freeb(mp); 12354 mp = mp1; 12355 rptr = mp->b_rptr; 12356 } 12357 if (MBLKL(mp) < IPV6_HDR_LEN + 12358 sizeof (tcph_t)) { 12359 BUMP_MIB(&ip_mib, tcpInErrs); 12360 freemsg(first_mp); 12361 return (NULL); 12362 } 12363 ip6h = (ip6_t *)rptr; 12364 } 12365 12366 /* 12367 * Find any potentially interesting extension headers 12368 * as well as the length of the IPv6 + extension 12369 * headers. 12370 */ 12371 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12372 /* Verify if this is a TCP packet */ 12373 if (nexthdrp != IPPROTO_TCP) { 12374 BUMP_MIB(&ip_mib, tcpInErrs); 12375 freemsg(first_mp); 12376 return (NULL); 12377 } 12378 } else { 12379 ip_hdr_len = IPV6_HDR_LEN; 12380 } 12381 } 12382 12383 done: 12384 if (ipversp != NULL) 12385 *ipversp = ipvers; 12386 if (ip_hdr_lenp != NULL) 12387 *ip_hdr_lenp = ip_hdr_len; 12388 if (ippp != NULL) 12389 *ippp = ipp; 12390 if (ifindexp != NULL) 12391 *ifindexp = ifindex; 12392 if (mctl_present) { 12393 freeb(first_mp); 12394 } 12395 return (mp); 12396 } 12397 12398 /* 12399 * Handle M_DATA messages from IP. Its called directly from IP via 12400 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12401 * in this path. 12402 * 12403 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12404 * v4 and v6), we are called through tcp_input() and a M_CTL can 12405 * be present for options but tcp_find_pktinfo() deals with it. We 12406 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12407 * 12408 * The first argument is always the connp/tcp to which the mp belongs. 12409 * There are no exceptions to this rule. The caller has already put 12410 * a reference on this connp/tcp and once tcp_rput_data() returns, 12411 * the squeue will do the refrele. 12412 * 12413 * The TH_SYN for the listener directly go to tcp_conn_request via 12414 * squeue. 12415 * 12416 * sqp: NULL = recursive, sqp != NULL means called from squeue 12417 */ 12418 void 12419 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12420 { 12421 int32_t bytes_acked; 12422 int32_t gap; 12423 mblk_t *mp1; 12424 uint_t flags; 12425 uint32_t new_swnd = 0; 12426 uchar_t *iphdr; 12427 uchar_t *rptr; 12428 int32_t rgap; 12429 uint32_t seg_ack; 12430 int seg_len; 12431 uint_t ip_hdr_len; 12432 uint32_t seg_seq; 12433 tcph_t *tcph; 12434 int urp; 12435 tcp_opt_t tcpopt; 12436 uint_t ipvers; 12437 ip6_pkt_t ipp; 12438 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12439 uint32_t cwnd; 12440 uint32_t add; 12441 int npkt; 12442 int mss; 12443 conn_t *connp = (conn_t *)arg; 12444 squeue_t *sqp = (squeue_t *)arg2; 12445 tcp_t *tcp = connp->conn_tcp; 12446 12447 /* 12448 * RST from fused tcp loopback peer should trigger an unfuse. 12449 */ 12450 if (tcp->tcp_fused) { 12451 TCP_STAT(tcp_fusion_aborted); 12452 tcp_unfuse(tcp); 12453 } 12454 12455 iphdr = mp->b_rptr; 12456 rptr = mp->b_rptr; 12457 ASSERT(OK_32PTR(rptr)); 12458 12459 /* 12460 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12461 * processing here. For rest call tcp_find_pktinfo to fill up the 12462 * necessary information. 12463 */ 12464 if (IPCL_IS_TCP4(connp)) { 12465 ipvers = IPV4_VERSION; 12466 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12467 } else { 12468 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12469 NULL, &ipp); 12470 if (mp == NULL) { 12471 TCP_STAT(tcp_rput_v6_error); 12472 return; 12473 } 12474 iphdr = mp->b_rptr; 12475 rptr = mp->b_rptr; 12476 } 12477 ASSERT(DB_TYPE(mp) == M_DATA); 12478 12479 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12480 seg_seq = ABE32_TO_U32(tcph->th_seq); 12481 seg_ack = ABE32_TO_U32(tcph->th_ack); 12482 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12483 seg_len = (int)(mp->b_wptr - rptr) - 12484 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12485 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12486 do { 12487 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12488 (uintptr_t)INT_MAX); 12489 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12490 } while ((mp1 = mp1->b_cont) != NULL && 12491 mp1->b_datap->db_type == M_DATA); 12492 } 12493 12494 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12495 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12496 seg_len, tcph); 12497 return; 12498 } 12499 12500 if (sqp != NULL) { 12501 /* 12502 * This is the correct place to update tcp_last_recv_time. Note 12503 * that it is also updated for tcp structure that belongs to 12504 * global and listener queues which do not really need updating. 12505 * But that should not cause any harm. And it is updated for 12506 * all kinds of incoming segments, not only for data segments. 12507 */ 12508 tcp->tcp_last_recv_time = lbolt; 12509 } 12510 12511 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12512 12513 BUMP_LOCAL(tcp->tcp_ibsegs); 12514 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12515 12516 if ((flags & TH_URG) && sqp != NULL) { 12517 /* 12518 * TCP can't handle urgent pointers that arrive before 12519 * the connection has been accept()ed since it can't 12520 * buffer OOB data. Discard segment if this happens. 12521 * 12522 * Nor can it reassemble urgent pointers, so discard 12523 * if it's not the next segment expected. 12524 * 12525 * Otherwise, collapse chain into one mblk (discard if 12526 * that fails). This makes sure the headers, retransmitted 12527 * data, and new data all are in the same mblk. 12528 */ 12529 ASSERT(mp != NULL); 12530 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12531 freemsg(mp); 12532 return; 12533 } 12534 /* Update pointers into message */ 12535 iphdr = rptr = mp->b_rptr; 12536 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12537 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12538 /* 12539 * Since we can't handle any data with this urgent 12540 * pointer that is out of sequence, we expunge 12541 * the data. This allows us to still register 12542 * the urgent mark and generate the M_PCSIG, 12543 * which we can do. 12544 */ 12545 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12546 seg_len = 0; 12547 } 12548 } 12549 12550 switch (tcp->tcp_state) { 12551 case TCPS_SYN_SENT: 12552 if (flags & TH_ACK) { 12553 /* 12554 * Note that our stack cannot send data before a 12555 * connection is established, therefore the 12556 * following check is valid. Otherwise, it has 12557 * to be changed. 12558 */ 12559 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12560 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12561 freemsg(mp); 12562 if (flags & TH_RST) 12563 return; 12564 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12565 tcp, seg_ack, 0, TH_RST); 12566 return; 12567 } 12568 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12569 } 12570 if (flags & TH_RST) { 12571 freemsg(mp); 12572 if (flags & TH_ACK) 12573 (void) tcp_clean_death(tcp, 12574 ECONNREFUSED, 13); 12575 return; 12576 } 12577 if (!(flags & TH_SYN)) { 12578 freemsg(mp); 12579 return; 12580 } 12581 12582 /* Process all TCP options. */ 12583 tcp_process_options(tcp, tcph); 12584 /* 12585 * The following changes our rwnd to be a multiple of the 12586 * MIN(peer MSS, our MSS) for performance reason. 12587 */ 12588 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12589 tcp->tcp_mss)); 12590 12591 /* Is the other end ECN capable? */ 12592 if (tcp->tcp_ecn_ok) { 12593 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12594 tcp->tcp_ecn_ok = B_FALSE; 12595 } 12596 } 12597 /* 12598 * Clear ECN flags because it may interfere with later 12599 * processing. 12600 */ 12601 flags &= ~(TH_ECE|TH_CWR); 12602 12603 tcp->tcp_irs = seg_seq; 12604 tcp->tcp_rack = seg_seq; 12605 tcp->tcp_rnxt = seg_seq + 1; 12606 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12607 if (!TCP_IS_DETACHED(tcp)) { 12608 /* Allocate room for SACK options if needed. */ 12609 if (tcp->tcp_snd_sack_ok) { 12610 (void) mi_set_sth_wroff(tcp->tcp_rq, 12611 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 12612 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12613 } else { 12614 (void) mi_set_sth_wroff(tcp->tcp_rq, 12615 tcp->tcp_hdr_len + 12616 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12617 } 12618 } 12619 if (flags & TH_ACK) { 12620 /* 12621 * If we can't get the confirmation upstream, pretend 12622 * we didn't even see this one. 12623 * 12624 * XXX: how can we pretend we didn't see it if we 12625 * have updated rnxt et. al. 12626 * 12627 * For loopback we defer sending up the T_CONN_CON 12628 * until after some checks below. 12629 */ 12630 mp1 = NULL; 12631 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12632 tcp->tcp_loopback ? &mp1 : NULL)) { 12633 freemsg(mp); 12634 return; 12635 } 12636 /* SYN was acked - making progress */ 12637 if (tcp->tcp_ipversion == IPV6_VERSION) 12638 tcp->tcp_ip_forward_progress = B_TRUE; 12639 12640 /* One for the SYN */ 12641 tcp->tcp_suna = tcp->tcp_iss + 1; 12642 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12643 tcp->tcp_state = TCPS_ESTABLISHED; 12644 12645 /* 12646 * If SYN was retransmitted, need to reset all 12647 * retransmission info. This is because this 12648 * segment will be treated as a dup ACK. 12649 */ 12650 if (tcp->tcp_rexmit) { 12651 tcp->tcp_rexmit = B_FALSE; 12652 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12653 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12654 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12655 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12656 tcp->tcp_ms_we_have_waited = 0; 12657 12658 /* 12659 * Set tcp_cwnd back to 1 MSS, per 12660 * recommendation from 12661 * draft-floyd-incr-init-win-01.txt, 12662 * Increasing TCP's Initial Window. 12663 */ 12664 tcp->tcp_cwnd = tcp->tcp_mss; 12665 } 12666 12667 tcp->tcp_swl1 = seg_seq; 12668 tcp->tcp_swl2 = seg_ack; 12669 12670 new_swnd = BE16_TO_U16(tcph->th_win); 12671 tcp->tcp_swnd = new_swnd; 12672 if (new_swnd > tcp->tcp_max_swnd) 12673 tcp->tcp_max_swnd = new_swnd; 12674 12675 /* 12676 * Always send the three-way handshake ack immediately 12677 * in order to make the connection complete as soon as 12678 * possible on the accepting host. 12679 */ 12680 flags |= TH_ACK_NEEDED; 12681 12682 /* 12683 * Special case for loopback. At this point we have 12684 * received SYN-ACK from the remote endpoint. In 12685 * order to ensure that both endpoints reach the 12686 * fused state prior to any data exchange, the final 12687 * ACK needs to be sent before we indicate T_CONN_CON 12688 * to the module upstream. 12689 */ 12690 if (tcp->tcp_loopback) { 12691 mblk_t *ack_mp; 12692 12693 ASSERT(!tcp->tcp_unfusable); 12694 ASSERT(mp1 != NULL); 12695 /* 12696 * For loopback, we always get a pure SYN-ACK 12697 * and only need to send back the final ACK 12698 * with no data (this is because the other 12699 * tcp is ours and we don't do T/TCP). This 12700 * final ACK triggers the passive side to 12701 * perform fusion in ESTABLISHED state. 12702 */ 12703 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12704 if (tcp->tcp_ack_tid != 0) { 12705 (void) TCP_TIMER_CANCEL(tcp, 12706 tcp->tcp_ack_tid); 12707 tcp->tcp_ack_tid = 0; 12708 } 12709 TCP_RECORD_TRACE(tcp, ack_mp, 12710 TCP_TRACE_SEND_PKT); 12711 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12712 BUMP_LOCAL(tcp->tcp_obsegs); 12713 BUMP_MIB(&tcp_mib, tcpOutAck); 12714 12715 /* Send up T_CONN_CON */ 12716 putnext(tcp->tcp_rq, mp1); 12717 12718 freemsg(mp); 12719 return; 12720 } 12721 /* 12722 * Forget fusion; we need to handle more 12723 * complex cases below. Send the deferred 12724 * T_CONN_CON message upstream and proceed 12725 * as usual. Mark this tcp as not capable 12726 * of fusion. 12727 */ 12728 TCP_STAT(tcp_fusion_unfusable); 12729 tcp->tcp_unfusable = B_TRUE; 12730 putnext(tcp->tcp_rq, mp1); 12731 } 12732 12733 /* 12734 * Check to see if there is data to be sent. If 12735 * yes, set the transmit flag. Then check to see 12736 * if received data processing needs to be done. 12737 * If not, go straight to xmit_check. This short 12738 * cut is OK as we don't support T/TCP. 12739 */ 12740 if (tcp->tcp_unsent) 12741 flags |= TH_XMIT_NEEDED; 12742 12743 if (seg_len == 0 && !(flags & TH_URG)) { 12744 freemsg(mp); 12745 goto xmit_check; 12746 } 12747 12748 flags &= ~TH_SYN; 12749 seg_seq++; 12750 break; 12751 } 12752 tcp->tcp_state = TCPS_SYN_RCVD; 12753 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 12754 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 12755 if (mp1) { 12756 DB_CPID(mp1) = tcp->tcp_cpid; 12757 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 12758 tcp_send_data(tcp, tcp->tcp_wq, mp1); 12759 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 12760 } 12761 freemsg(mp); 12762 return; 12763 case TCPS_SYN_RCVD: 12764 if (flags & TH_ACK) { 12765 /* 12766 * In this state, a SYN|ACK packet is either bogus 12767 * because the other side must be ACKing our SYN which 12768 * indicates it has seen the ACK for their SYN and 12769 * shouldn't retransmit it or we're crossing SYNs 12770 * on active open. 12771 */ 12772 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 12773 freemsg(mp); 12774 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 12775 tcp, seg_ack, 0, TH_RST); 12776 return; 12777 } 12778 /* 12779 * NOTE: RFC 793 pg. 72 says this should be 12780 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 12781 * but that would mean we have an ack that ignored 12782 * our SYN. 12783 */ 12784 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 12785 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12786 freemsg(mp); 12787 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 12788 tcp, seg_ack, 0, TH_RST); 12789 return; 12790 } 12791 } 12792 break; 12793 case TCPS_LISTEN: 12794 /* 12795 * Only a TLI listener can come through this path when a 12796 * acceptor is going back to be a listener and a packet 12797 * for the acceptor hits the classifier. For a socket 12798 * listener, this can never happen because a listener 12799 * can never accept connection on itself and hence a 12800 * socket acceptor can not go back to being a listener. 12801 */ 12802 ASSERT(!TCP_IS_SOCKET(tcp)); 12803 /*FALLTHRU*/ 12804 case TCPS_CLOSED: 12805 case TCPS_BOUND: { 12806 conn_t *new_connp; 12807 12808 new_connp = ipcl_classify(mp, connp->conn_zoneid); 12809 if (new_connp != NULL) { 12810 tcp_reinput(new_connp, mp, connp->conn_sqp); 12811 return; 12812 } 12813 /* We failed to classify. For now just drop the packet */ 12814 freemsg(mp); 12815 return; 12816 } 12817 case TCPS_IDLE: 12818 /* 12819 * Handle the case where the tcp_clean_death() has happened 12820 * on a connection (application hasn't closed yet) but a packet 12821 * was already queued on squeue before tcp_clean_death() 12822 * was processed. Calling tcp_clean_death() twice on same 12823 * connection can result in weird behaviour. 12824 */ 12825 freemsg(mp); 12826 return; 12827 default: 12828 break; 12829 } 12830 12831 /* 12832 * Already on the correct queue/perimeter. 12833 * If this is a detached connection and not an eager 12834 * connection hanging off a listener then new data 12835 * (past the FIN) will cause a reset. 12836 * We do a special check here where it 12837 * is out of the main line, rather than check 12838 * if we are detached every time we see new 12839 * data down below. 12840 */ 12841 if (TCP_IS_DETACHED_NONEAGER(tcp) && 12842 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 12843 BUMP_MIB(&tcp_mib, tcpInClosed); 12844 TCP_RECORD_TRACE(tcp, 12845 mp, TCP_TRACE_RECV_PKT); 12846 12847 freemsg(mp); 12848 /* 12849 * This could be an SSL closure alert. We're detached so just 12850 * acknowledge it this last time. 12851 */ 12852 if (tcp->tcp_kssl_ctx != NULL) { 12853 kssl_release_ctx(tcp->tcp_kssl_ctx); 12854 tcp->tcp_kssl_ctx = NULL; 12855 12856 tcp->tcp_rnxt += seg_len; 12857 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12858 flags |= TH_ACK_NEEDED; 12859 goto ack_check; 12860 } 12861 12862 tcp_xmit_ctl("new data when detached", tcp, 12863 tcp->tcp_snxt, 0, TH_RST); 12864 (void) tcp_clean_death(tcp, EPROTO, 12); 12865 return; 12866 } 12867 12868 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12869 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 12870 new_swnd = BE16_TO_U16(tcph->th_win) << 12871 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 12872 mss = tcp->tcp_mss; 12873 12874 if (tcp->tcp_snd_ts_ok) { 12875 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 12876 /* 12877 * This segment is not acceptable. 12878 * Drop it and send back an ACK. 12879 */ 12880 freemsg(mp); 12881 flags |= TH_ACK_NEEDED; 12882 goto ack_check; 12883 } 12884 } else if (tcp->tcp_snd_sack_ok) { 12885 ASSERT(tcp->tcp_sack_info != NULL); 12886 tcpopt.tcp = tcp; 12887 /* 12888 * SACK info in already updated in tcp_parse_options. Ignore 12889 * all other TCP options... 12890 */ 12891 (void) tcp_parse_options(tcph, &tcpopt); 12892 } 12893 try_again:; 12894 gap = seg_seq - tcp->tcp_rnxt; 12895 rgap = tcp->tcp_rwnd - (gap + seg_len); 12896 /* 12897 * gap is the amount of sequence space between what we expect to see 12898 * and what we got for seg_seq. A positive value for gap means 12899 * something got lost. A negative value means we got some old stuff. 12900 */ 12901 if (gap < 0) { 12902 /* Old stuff present. Is the SYN in there? */ 12903 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 12904 (seg_len != 0)) { 12905 flags &= ~TH_SYN; 12906 seg_seq++; 12907 urp--; 12908 /* Recompute the gaps after noting the SYN. */ 12909 goto try_again; 12910 } 12911 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 12912 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 12913 (seg_len > -gap ? -gap : seg_len)); 12914 /* Remove the old stuff from seg_len. */ 12915 seg_len += gap; 12916 /* 12917 * Anything left? 12918 * Make sure to check for unack'd FIN when rest of data 12919 * has been previously ack'd. 12920 */ 12921 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 12922 /* 12923 * Resets are only valid if they lie within our offered 12924 * window. If the RST bit is set, we just ignore this 12925 * segment. 12926 */ 12927 if (flags & TH_RST) { 12928 freemsg(mp); 12929 return; 12930 } 12931 12932 /* 12933 * The arriving of dup data packets indicate that we 12934 * may have postponed an ack for too long, or the other 12935 * side's RTT estimate is out of shape. Start acking 12936 * more often. 12937 */ 12938 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 12939 tcp->tcp_rack_cnt >= 1 && 12940 tcp->tcp_rack_abs_max > 2) { 12941 tcp->tcp_rack_abs_max--; 12942 } 12943 tcp->tcp_rack_cur_max = 1; 12944 12945 /* 12946 * This segment is "unacceptable". None of its 12947 * sequence space lies within our advertized window. 12948 * 12949 * Adjust seg_len to the original value for tracing. 12950 */ 12951 seg_len -= gap; 12952 if (tcp->tcp_debug) { 12953 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 12954 "tcp_rput: unacceptable, gap %d, rgap %d, " 12955 "flags 0x%x, seg_seq %u, seg_ack %u, " 12956 "seg_len %d, rnxt %u, snxt %u, %s", 12957 gap, rgap, flags, seg_seq, seg_ack, 12958 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 12959 tcp_display(tcp, NULL, 12960 DISP_ADDR_AND_PORT)); 12961 } 12962 12963 /* 12964 * Arrange to send an ACK in response to the 12965 * unacceptable segment per RFC 793 page 69. There 12966 * is only one small difference between ours and the 12967 * acceptability test in the RFC - we accept ACK-only 12968 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 12969 * will be generated. 12970 * 12971 * Note that we have to ACK an ACK-only packet at least 12972 * for stacks that send 0-length keep-alives with 12973 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 12974 * section 4.2.3.6. As long as we don't ever generate 12975 * an unacceptable packet in response to an incoming 12976 * packet that is unacceptable, it should not cause 12977 * "ACK wars". 12978 */ 12979 flags |= TH_ACK_NEEDED; 12980 12981 /* 12982 * Continue processing this segment in order to use the 12983 * ACK information it contains, but skip all other 12984 * sequence-number processing. Processing the ACK 12985 * information is necessary in order to 12986 * re-synchronize connections that may have lost 12987 * synchronization. 12988 * 12989 * We clear seg_len and flag fields related to 12990 * sequence number processing as they are not 12991 * to be trusted for an unacceptable segment. 12992 */ 12993 seg_len = 0; 12994 flags &= ~(TH_SYN | TH_FIN | TH_URG); 12995 goto process_ack; 12996 } 12997 12998 /* Fix seg_seq, and chew the gap off the front. */ 12999 seg_seq = tcp->tcp_rnxt; 13000 urp += gap; 13001 do { 13002 mblk_t *mp2; 13003 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13004 (uintptr_t)UINT_MAX); 13005 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 13006 if (gap > 0) { 13007 mp->b_rptr = mp->b_wptr - gap; 13008 break; 13009 } 13010 mp2 = mp; 13011 mp = mp->b_cont; 13012 freeb(mp2); 13013 } while (gap < 0); 13014 /* 13015 * If the urgent data has already been acknowledged, we 13016 * should ignore TH_URG below 13017 */ 13018 if (urp < 0) 13019 flags &= ~TH_URG; 13020 } 13021 /* 13022 * rgap is the amount of stuff received out of window. A negative 13023 * value is the amount out of window. 13024 */ 13025 if (rgap < 0) { 13026 mblk_t *mp2; 13027 13028 if (tcp->tcp_rwnd == 0) { 13029 BUMP_MIB(&tcp_mib, tcpInWinProbe); 13030 } else { 13031 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 13032 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 13033 } 13034 13035 /* 13036 * seg_len does not include the FIN, so if more than 13037 * just the FIN is out of window, we act like we don't 13038 * see it. (If just the FIN is out of window, rgap 13039 * will be zero and we will go ahead and acknowledge 13040 * the FIN.) 13041 */ 13042 flags &= ~TH_FIN; 13043 13044 /* Fix seg_len and make sure there is something left. */ 13045 seg_len += rgap; 13046 if (seg_len <= 0) { 13047 /* 13048 * Resets are only valid if they lie within our offered 13049 * window. If the RST bit is set, we just ignore this 13050 * segment. 13051 */ 13052 if (flags & TH_RST) { 13053 freemsg(mp); 13054 return; 13055 } 13056 13057 /* Per RFC 793, we need to send back an ACK. */ 13058 flags |= TH_ACK_NEEDED; 13059 13060 /* 13061 * Send SIGURG as soon as possible i.e. even 13062 * if the TH_URG was delivered in a window probe 13063 * packet (which will be unacceptable). 13064 * 13065 * We generate a signal if none has been generated 13066 * for this connection or if this is a new urgent 13067 * byte. Also send a zero-length "unmarked" message 13068 * to inform SIOCATMARK that this is not the mark. 13069 * 13070 * tcp_urp_last_valid is cleared when the T_exdata_ind 13071 * is sent up. This plus the check for old data 13072 * (gap >= 0) handles the wraparound of the sequence 13073 * number space without having to always track the 13074 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 13075 * this max in its rcv_up variable). 13076 * 13077 * This prevents duplicate SIGURGS due to a "late" 13078 * zero-window probe when the T_EXDATA_IND has already 13079 * been sent up. 13080 */ 13081 if ((flags & TH_URG) && 13082 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 13083 tcp->tcp_urp_last))) { 13084 mp1 = allocb(0, BPRI_MED); 13085 if (mp1 == NULL) { 13086 freemsg(mp); 13087 return; 13088 } 13089 if (!TCP_IS_DETACHED(tcp) && 13090 !putnextctl1(tcp->tcp_rq, M_PCSIG, 13091 SIGURG)) { 13092 /* Try again on the rexmit. */ 13093 freemsg(mp1); 13094 freemsg(mp); 13095 return; 13096 } 13097 /* 13098 * If the next byte would be the mark 13099 * then mark with MARKNEXT else mark 13100 * with NOTMARKNEXT. 13101 */ 13102 if (gap == 0 && urp == 0) 13103 mp1->b_flag |= MSGMARKNEXT; 13104 else 13105 mp1->b_flag |= MSGNOTMARKNEXT; 13106 freemsg(tcp->tcp_urp_mark_mp); 13107 tcp->tcp_urp_mark_mp = mp1; 13108 flags |= TH_SEND_URP_MARK; 13109 tcp->tcp_urp_last_valid = B_TRUE; 13110 tcp->tcp_urp_last = urp + seg_seq; 13111 } 13112 /* 13113 * If this is a zero window probe, continue to 13114 * process the ACK part. But we need to set seg_len 13115 * to 0 to avoid data processing. Otherwise just 13116 * drop the segment and send back an ACK. 13117 */ 13118 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 13119 flags &= ~(TH_SYN | TH_URG); 13120 seg_len = 0; 13121 goto process_ack; 13122 } else { 13123 freemsg(mp); 13124 goto ack_check; 13125 } 13126 } 13127 /* Pitch out of window stuff off the end. */ 13128 rgap = seg_len; 13129 mp2 = mp; 13130 do { 13131 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 13132 (uintptr_t)INT_MAX); 13133 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 13134 if (rgap < 0) { 13135 mp2->b_wptr += rgap; 13136 if ((mp1 = mp2->b_cont) != NULL) { 13137 mp2->b_cont = NULL; 13138 freemsg(mp1); 13139 } 13140 break; 13141 } 13142 } while ((mp2 = mp2->b_cont) != NULL); 13143 } 13144 ok:; 13145 /* 13146 * TCP should check ECN info for segments inside the window only. 13147 * Therefore the check should be done here. 13148 */ 13149 if (tcp->tcp_ecn_ok) { 13150 if (flags & TH_CWR) { 13151 tcp->tcp_ecn_echo_on = B_FALSE; 13152 } 13153 /* 13154 * Note that both ECN_CE and CWR can be set in the 13155 * same segment. In this case, we once again turn 13156 * on ECN_ECHO. 13157 */ 13158 if (tcp->tcp_ipversion == IPV4_VERSION) { 13159 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13160 13161 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13162 tcp->tcp_ecn_echo_on = B_TRUE; 13163 } 13164 } else { 13165 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13166 13167 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13168 htonl(IPH_ECN_CE << 20)) { 13169 tcp->tcp_ecn_echo_on = B_TRUE; 13170 } 13171 } 13172 } 13173 13174 /* 13175 * Check whether we can update tcp_ts_recent. This test is 13176 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13177 * Extensions for High Performance: An Update", Internet Draft. 13178 */ 13179 if (tcp->tcp_snd_ts_ok && 13180 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13181 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13182 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13183 tcp->tcp_last_rcv_lbolt = lbolt64; 13184 } 13185 13186 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13187 /* 13188 * FIN in an out of order segment. We record this in 13189 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13190 * Clear the FIN so that any check on FIN flag will fail. 13191 * Remember that FIN also counts in the sequence number 13192 * space. So we need to ack out of order FIN only segments. 13193 */ 13194 if (flags & TH_FIN) { 13195 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13196 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13197 flags &= ~TH_FIN; 13198 flags |= TH_ACK_NEEDED; 13199 } 13200 if (seg_len > 0) { 13201 /* Fill in the SACK blk list. */ 13202 if (tcp->tcp_snd_sack_ok) { 13203 ASSERT(tcp->tcp_sack_info != NULL); 13204 tcp_sack_insert(tcp->tcp_sack_list, 13205 seg_seq, seg_seq + seg_len, 13206 &(tcp->tcp_num_sack_blk)); 13207 } 13208 13209 /* 13210 * Attempt reassembly and see if we have something 13211 * ready to go. 13212 */ 13213 mp = tcp_reass(tcp, mp, seg_seq); 13214 /* Always ack out of order packets */ 13215 flags |= TH_ACK_NEEDED | TH_PUSH; 13216 if (mp) { 13217 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13218 (uintptr_t)INT_MAX); 13219 seg_len = mp->b_cont ? msgdsize(mp) : 13220 (int)(mp->b_wptr - mp->b_rptr); 13221 seg_seq = tcp->tcp_rnxt; 13222 /* 13223 * A gap is filled and the seq num and len 13224 * of the gap match that of a previously 13225 * received FIN, put the FIN flag back in. 13226 */ 13227 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13228 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13229 flags |= TH_FIN; 13230 tcp->tcp_valid_bits &= 13231 ~TCP_OFO_FIN_VALID; 13232 } 13233 } else { 13234 /* 13235 * Keep going even with NULL mp. 13236 * There may be a useful ACK or something else 13237 * we don't want to miss. 13238 * 13239 * But TCP should not perform fast retransmit 13240 * because of the ack number. TCP uses 13241 * seg_len == 0 to determine if it is a pure 13242 * ACK. And this is not a pure ACK. 13243 */ 13244 seg_len = 0; 13245 ofo_seg = B_TRUE; 13246 } 13247 } 13248 } else if (seg_len > 0) { 13249 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 13250 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 13251 /* 13252 * If an out of order FIN was received before, and the seq 13253 * num and len of the new segment match that of the FIN, 13254 * put the FIN flag back in. 13255 */ 13256 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13257 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13258 flags |= TH_FIN; 13259 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13260 } 13261 } 13262 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13263 if (flags & TH_RST) { 13264 freemsg(mp); 13265 switch (tcp->tcp_state) { 13266 case TCPS_SYN_RCVD: 13267 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13268 break; 13269 case TCPS_ESTABLISHED: 13270 case TCPS_FIN_WAIT_1: 13271 case TCPS_FIN_WAIT_2: 13272 case TCPS_CLOSE_WAIT: 13273 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13274 break; 13275 case TCPS_CLOSING: 13276 case TCPS_LAST_ACK: 13277 (void) tcp_clean_death(tcp, 0, 16); 13278 break; 13279 default: 13280 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13281 (void) tcp_clean_death(tcp, ENXIO, 17); 13282 break; 13283 } 13284 return; 13285 } 13286 if (flags & TH_SYN) { 13287 /* 13288 * See RFC 793, Page 71 13289 * 13290 * The seq number must be in the window as it should 13291 * be "fixed" above. If it is outside window, it should 13292 * be already rejected. Note that we allow seg_seq to be 13293 * rnxt + rwnd because we want to accept 0 window probe. 13294 */ 13295 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13296 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13297 freemsg(mp); 13298 /* 13299 * If the ACK flag is not set, just use our snxt as the 13300 * seq number of the RST segment. 13301 */ 13302 if (!(flags & TH_ACK)) { 13303 seg_ack = tcp->tcp_snxt; 13304 } 13305 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13306 TH_RST|TH_ACK); 13307 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13308 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13309 return; 13310 } 13311 /* 13312 * urp could be -1 when the urp field in the packet is 0 13313 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13314 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13315 */ 13316 if (flags & TH_URG && urp >= 0) { 13317 if (!tcp->tcp_urp_last_valid || 13318 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13319 /* 13320 * If we haven't generated the signal yet for this 13321 * urgent pointer value, do it now. Also, send up a 13322 * zero-length M_DATA indicating whether or not this is 13323 * the mark. The latter is not needed when a 13324 * T_EXDATA_IND is sent up. However, if there are 13325 * allocation failures this code relies on the sender 13326 * retransmitting and the socket code for determining 13327 * the mark should not block waiting for the peer to 13328 * transmit. Thus, for simplicity we always send up the 13329 * mark indication. 13330 */ 13331 mp1 = allocb(0, BPRI_MED); 13332 if (mp1 == NULL) { 13333 freemsg(mp); 13334 return; 13335 } 13336 if (!TCP_IS_DETACHED(tcp) && 13337 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13338 /* Try again on the rexmit. */ 13339 freemsg(mp1); 13340 freemsg(mp); 13341 return; 13342 } 13343 /* 13344 * Mark with NOTMARKNEXT for now. 13345 * The code below will change this to MARKNEXT 13346 * if we are at the mark. 13347 * 13348 * If there are allocation failures (e.g. in dupmsg 13349 * below) the next time tcp_rput_data sees the urgent 13350 * segment it will send up the MSG*MARKNEXT message. 13351 */ 13352 mp1->b_flag |= MSGNOTMARKNEXT; 13353 freemsg(tcp->tcp_urp_mark_mp); 13354 tcp->tcp_urp_mark_mp = mp1; 13355 flags |= TH_SEND_URP_MARK; 13356 #ifdef DEBUG 13357 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13358 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13359 "last %x, %s", 13360 seg_seq, urp, tcp->tcp_urp_last, 13361 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13362 #endif /* DEBUG */ 13363 tcp->tcp_urp_last_valid = B_TRUE; 13364 tcp->tcp_urp_last = urp + seg_seq; 13365 } else if (tcp->tcp_urp_mark_mp != NULL) { 13366 /* 13367 * An allocation failure prevented the previous 13368 * tcp_rput_data from sending up the allocated 13369 * MSG*MARKNEXT message - send it up this time 13370 * around. 13371 */ 13372 flags |= TH_SEND_URP_MARK; 13373 } 13374 13375 /* 13376 * If the urgent byte is in this segment, make sure that it is 13377 * all by itself. This makes it much easier to deal with the 13378 * possibility of an allocation failure on the T_exdata_ind. 13379 * Note that seg_len is the number of bytes in the segment, and 13380 * urp is the offset into the segment of the urgent byte. 13381 * urp < seg_len means that the urgent byte is in this segment. 13382 */ 13383 if (urp < seg_len) { 13384 if (seg_len != 1) { 13385 uint32_t tmp_rnxt; 13386 /* 13387 * Break it up and feed it back in. 13388 * Re-attach the IP header. 13389 */ 13390 mp->b_rptr = iphdr; 13391 if (urp > 0) { 13392 /* 13393 * There is stuff before the urgent 13394 * byte. 13395 */ 13396 mp1 = dupmsg(mp); 13397 if (!mp1) { 13398 /* 13399 * Trim from urgent byte on. 13400 * The rest will come back. 13401 */ 13402 (void) adjmsg(mp, 13403 urp - seg_len); 13404 tcp_rput_data(connp, 13405 mp, NULL); 13406 return; 13407 } 13408 (void) adjmsg(mp1, urp - seg_len); 13409 /* Feed this piece back in. */ 13410 tmp_rnxt = tcp->tcp_rnxt; 13411 tcp_rput_data(connp, mp1, NULL); 13412 /* 13413 * If the data passed back in was not 13414 * processed (ie: bad ACK) sending 13415 * the remainder back in will cause a 13416 * loop. In this case, drop the 13417 * packet and let the sender try 13418 * sending a good packet. 13419 */ 13420 if (tmp_rnxt == tcp->tcp_rnxt) { 13421 freemsg(mp); 13422 return; 13423 } 13424 } 13425 if (urp != seg_len - 1) { 13426 uint32_t tmp_rnxt; 13427 /* 13428 * There is stuff after the urgent 13429 * byte. 13430 */ 13431 mp1 = dupmsg(mp); 13432 if (!mp1) { 13433 /* 13434 * Trim everything beyond the 13435 * urgent byte. The rest will 13436 * come back. 13437 */ 13438 (void) adjmsg(mp, 13439 urp + 1 - seg_len); 13440 tcp_rput_data(connp, 13441 mp, NULL); 13442 return; 13443 } 13444 (void) adjmsg(mp1, urp + 1 - seg_len); 13445 tmp_rnxt = tcp->tcp_rnxt; 13446 tcp_rput_data(connp, mp1, NULL); 13447 /* 13448 * If the data passed back in was not 13449 * processed (ie: bad ACK) sending 13450 * the remainder back in will cause a 13451 * loop. In this case, drop the 13452 * packet and let the sender try 13453 * sending a good packet. 13454 */ 13455 if (tmp_rnxt == tcp->tcp_rnxt) { 13456 freemsg(mp); 13457 return; 13458 } 13459 } 13460 tcp_rput_data(connp, mp, NULL); 13461 return; 13462 } 13463 /* 13464 * This segment contains only the urgent byte. We 13465 * have to allocate the T_exdata_ind, if we can. 13466 */ 13467 if (!tcp->tcp_urp_mp) { 13468 struct T_exdata_ind *tei; 13469 mp1 = allocb(sizeof (struct T_exdata_ind), 13470 BPRI_MED); 13471 if (!mp1) { 13472 /* 13473 * Sigh... It'll be back. 13474 * Generate any MSG*MARK message now. 13475 */ 13476 freemsg(mp); 13477 seg_len = 0; 13478 if (flags & TH_SEND_URP_MARK) { 13479 13480 13481 ASSERT(tcp->tcp_urp_mark_mp); 13482 tcp->tcp_urp_mark_mp->b_flag &= 13483 ~MSGNOTMARKNEXT; 13484 tcp->tcp_urp_mark_mp->b_flag |= 13485 MSGMARKNEXT; 13486 } 13487 goto ack_check; 13488 } 13489 mp1->b_datap->db_type = M_PROTO; 13490 tei = (struct T_exdata_ind *)mp1->b_rptr; 13491 tei->PRIM_type = T_EXDATA_IND; 13492 tei->MORE_flag = 0; 13493 mp1->b_wptr = (uchar_t *)&tei[1]; 13494 tcp->tcp_urp_mp = mp1; 13495 #ifdef DEBUG 13496 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13497 "tcp_rput: allocated exdata_ind %s", 13498 tcp_display(tcp, NULL, 13499 DISP_PORT_ONLY)); 13500 #endif /* DEBUG */ 13501 /* 13502 * There is no need to send a separate MSG*MARK 13503 * message since the T_EXDATA_IND will be sent 13504 * now. 13505 */ 13506 flags &= ~TH_SEND_URP_MARK; 13507 freemsg(tcp->tcp_urp_mark_mp); 13508 tcp->tcp_urp_mark_mp = NULL; 13509 } 13510 /* 13511 * Now we are all set. On the next putnext upstream, 13512 * tcp_urp_mp will be non-NULL and will get prepended 13513 * to what has to be this piece containing the urgent 13514 * byte. If for any reason we abort this segment below, 13515 * if it comes back, we will have this ready, or it 13516 * will get blown off in close. 13517 */ 13518 } else if (urp == seg_len) { 13519 /* 13520 * The urgent byte is the next byte after this sequence 13521 * number. If there is data it is marked with 13522 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13523 * since it is not needed. Otherwise, if the code 13524 * above just allocated a zero-length tcp_urp_mark_mp 13525 * message, that message is tagged with MSGMARKNEXT. 13526 * Sending up these MSGMARKNEXT messages makes 13527 * SIOCATMARK work correctly even though 13528 * the T_EXDATA_IND will not be sent up until the 13529 * urgent byte arrives. 13530 */ 13531 if (seg_len != 0) { 13532 flags |= TH_MARKNEXT_NEEDED; 13533 freemsg(tcp->tcp_urp_mark_mp); 13534 tcp->tcp_urp_mark_mp = NULL; 13535 flags &= ~TH_SEND_URP_MARK; 13536 } else if (tcp->tcp_urp_mark_mp != NULL) { 13537 flags |= TH_SEND_URP_MARK; 13538 tcp->tcp_urp_mark_mp->b_flag &= 13539 ~MSGNOTMARKNEXT; 13540 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13541 } 13542 #ifdef DEBUG 13543 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13544 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13545 seg_len, flags, 13546 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13547 #endif /* DEBUG */ 13548 } else { 13549 /* Data left until we hit mark */ 13550 #ifdef DEBUG 13551 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13552 "tcp_rput: URP %d bytes left, %s", 13553 urp - seg_len, tcp_display(tcp, NULL, 13554 DISP_PORT_ONLY)); 13555 #endif /* DEBUG */ 13556 } 13557 } 13558 13559 process_ack: 13560 if (!(flags & TH_ACK)) { 13561 freemsg(mp); 13562 goto xmit_check; 13563 } 13564 } 13565 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13566 13567 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13568 tcp->tcp_ip_forward_progress = B_TRUE; 13569 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13570 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13571 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13572 /* 3-way handshake complete - pass up the T_CONN_IND */ 13573 tcp_t *listener = tcp->tcp_listener; 13574 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13575 13576 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13577 /* 13578 * We are here means eager is fine but it can 13579 * get a TH_RST at any point between now and till 13580 * accept completes and disappear. We need to 13581 * ensure that reference to eager is valid after 13582 * we get out of eager's perimeter. So we do 13583 * an extra refhold. 13584 */ 13585 CONN_INC_REF(connp); 13586 13587 /* 13588 * The listener also exists because of the refhold 13589 * done in tcp_conn_request. Its possible that it 13590 * might have closed. We will check that once we 13591 * get inside listeners context. 13592 */ 13593 CONN_INC_REF(listener->tcp_connp); 13594 if (listener->tcp_connp->conn_sqp == 13595 connp->conn_sqp) { 13596 tcp_send_conn_ind(listener->tcp_connp, mp, 13597 listener->tcp_connp->conn_sqp); 13598 CONN_DEC_REF(listener->tcp_connp); 13599 } else if (!tcp->tcp_loopback) { 13600 squeue_fill(listener->tcp_connp->conn_sqp, mp, 13601 tcp_send_conn_ind, 13602 listener->tcp_connp, SQTAG_TCP_CONN_IND); 13603 } else { 13604 squeue_enter(listener->tcp_connp->conn_sqp, mp, 13605 tcp_send_conn_ind, listener->tcp_connp, 13606 SQTAG_TCP_CONN_IND); 13607 } 13608 } 13609 13610 if (tcp->tcp_active_open) { 13611 /* 13612 * We are seeing the final ack in the three way 13613 * hand shake of a active open'ed connection 13614 * so we must send up a T_CONN_CON 13615 */ 13616 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13617 freemsg(mp); 13618 return; 13619 } 13620 /* 13621 * Don't fuse the loopback endpoints for 13622 * simultaneous active opens. 13623 */ 13624 if (tcp->tcp_loopback) { 13625 TCP_STAT(tcp_fusion_unfusable); 13626 tcp->tcp_unfusable = B_TRUE; 13627 } 13628 } 13629 13630 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13631 bytes_acked--; 13632 /* SYN was acked - making progress */ 13633 if (tcp->tcp_ipversion == IPV6_VERSION) 13634 tcp->tcp_ip_forward_progress = B_TRUE; 13635 13636 /* 13637 * If SYN was retransmitted, need to reset all 13638 * retransmission info as this segment will be 13639 * treated as a dup ACK. 13640 */ 13641 if (tcp->tcp_rexmit) { 13642 tcp->tcp_rexmit = B_FALSE; 13643 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13644 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13645 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13646 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13647 tcp->tcp_ms_we_have_waited = 0; 13648 tcp->tcp_cwnd = mss; 13649 } 13650 13651 /* 13652 * We set the send window to zero here. 13653 * This is needed if there is data to be 13654 * processed already on the queue. 13655 * Later (at swnd_update label), the 13656 * "new_swnd > tcp_swnd" condition is satisfied 13657 * the XMIT_NEEDED flag is set in the current 13658 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13659 * called if there is already data on queue in 13660 * this state. 13661 */ 13662 tcp->tcp_swnd = 0; 13663 13664 if (new_swnd > tcp->tcp_max_swnd) 13665 tcp->tcp_max_swnd = new_swnd; 13666 tcp->tcp_swl1 = seg_seq; 13667 tcp->tcp_swl2 = seg_ack; 13668 tcp->tcp_state = TCPS_ESTABLISHED; 13669 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13670 13671 /* Fuse when both sides are in ESTABLISHED state */ 13672 if (tcp->tcp_loopback && do_tcp_fusion) 13673 tcp_fuse(tcp, iphdr, tcph); 13674 13675 } 13676 /* This code follows 4.4BSD-Lite2 mostly. */ 13677 if (bytes_acked < 0) 13678 goto est; 13679 13680 /* 13681 * If TCP is ECN capable and the congestion experience bit is 13682 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13683 * done once per window (or more loosely, per RTT). 13684 */ 13685 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13686 tcp->tcp_cwr = B_FALSE; 13687 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13688 if (!tcp->tcp_cwr) { 13689 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13690 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13691 tcp->tcp_cwnd = npkt * mss; 13692 /* 13693 * If the cwnd is 0, use the timer to clock out 13694 * new segments. This is required by the ECN spec. 13695 */ 13696 if (npkt == 0) { 13697 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13698 /* 13699 * This makes sure that when the ACK comes 13700 * back, we will increase tcp_cwnd by 1 MSS. 13701 */ 13702 tcp->tcp_cwnd_cnt = 0; 13703 } 13704 tcp->tcp_cwr = B_TRUE; 13705 /* 13706 * This marks the end of the current window of in 13707 * flight data. That is why we don't use 13708 * tcp_suna + tcp_swnd. Only data in flight can 13709 * provide ECN info. 13710 */ 13711 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13712 tcp->tcp_ecn_cwr_sent = B_FALSE; 13713 } 13714 } 13715 13716 mp1 = tcp->tcp_xmit_head; 13717 if (bytes_acked == 0) { 13718 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13719 int dupack_cnt; 13720 13721 BUMP_MIB(&tcp_mib, tcpInDupAck); 13722 /* 13723 * Fast retransmit. When we have seen exactly three 13724 * identical ACKs while we have unacked data 13725 * outstanding we take it as a hint that our peer 13726 * dropped something. 13727 * 13728 * If TCP is retransmitting, don't do fast retransmit. 13729 */ 13730 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 13731 ! tcp->tcp_rexmit) { 13732 /* Do Limited Transmit */ 13733 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 13734 tcp_dupack_fast_retransmit) { 13735 /* 13736 * RFC 3042 13737 * 13738 * What we need to do is temporarily 13739 * increase tcp_cwnd so that new 13740 * data can be sent if it is allowed 13741 * by the receive window (tcp_rwnd). 13742 * tcp_wput_data() will take care of 13743 * the rest. 13744 * 13745 * If the connection is SACK capable, 13746 * only do limited xmit when there 13747 * is SACK info. 13748 * 13749 * Note how tcp_cwnd is incremented. 13750 * The first dup ACK will increase 13751 * it by 1 MSS. The second dup ACK 13752 * will increase it by 2 MSS. This 13753 * means that only 1 new segment will 13754 * be sent for each dup ACK. 13755 */ 13756 if (tcp->tcp_unsent > 0 && 13757 (!tcp->tcp_snd_sack_ok || 13758 (tcp->tcp_snd_sack_ok && 13759 tcp->tcp_notsack_list != NULL))) { 13760 tcp->tcp_cwnd += mss << 13761 (tcp->tcp_dupack_cnt - 1); 13762 flags |= TH_LIMIT_XMIT; 13763 } 13764 } else if (dupack_cnt == 13765 tcp_dupack_fast_retransmit) { 13766 13767 /* 13768 * If we have reduced tcp_ssthresh 13769 * because of ECN, do not reduce it again 13770 * unless it is already one window of data 13771 * away. After one window of data, tcp_cwr 13772 * should then be cleared. Note that 13773 * for non ECN capable connection, tcp_cwr 13774 * should always be false. 13775 * 13776 * Adjust cwnd since the duplicate 13777 * ack indicates that a packet was 13778 * dropped (due to congestion.) 13779 */ 13780 if (!tcp->tcp_cwr) { 13781 npkt = ((tcp->tcp_snxt - 13782 tcp->tcp_suna) >> 1) / mss; 13783 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 13784 mss; 13785 tcp->tcp_cwnd = (npkt + 13786 tcp->tcp_dupack_cnt) * mss; 13787 } 13788 if (tcp->tcp_ecn_ok) { 13789 tcp->tcp_cwr = B_TRUE; 13790 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13791 tcp->tcp_ecn_cwr_sent = B_FALSE; 13792 } 13793 13794 /* 13795 * We do Hoe's algorithm. Refer to her 13796 * paper "Improving the Start-up Behavior 13797 * of a Congestion Control Scheme for TCP," 13798 * appeared in SIGCOMM'96. 13799 * 13800 * Save highest seq no we have sent so far. 13801 * Be careful about the invisible FIN byte. 13802 */ 13803 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 13804 (tcp->tcp_unsent == 0)) { 13805 tcp->tcp_rexmit_max = tcp->tcp_fss; 13806 } else { 13807 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13808 } 13809 13810 /* 13811 * Do not allow bursty traffic during. 13812 * fast recovery. Refer to Fall and Floyd's 13813 * paper "Simulation-based Comparisons of 13814 * Tahoe, Reno and SACK TCP" (in CCR?) 13815 * This is a best current practise. 13816 */ 13817 tcp->tcp_snd_burst = TCP_CWND_SS; 13818 13819 /* 13820 * For SACK: 13821 * Calculate tcp_pipe, which is the 13822 * estimated number of bytes in 13823 * network. 13824 * 13825 * tcp_fack is the highest sack'ed seq num 13826 * TCP has received. 13827 * 13828 * tcp_pipe is explained in the above quoted 13829 * Fall and Floyd's paper. tcp_fack is 13830 * explained in Mathis and Mahdavi's 13831 * "Forward Acknowledgment: Refining TCP 13832 * Congestion Control" in SIGCOMM '96. 13833 */ 13834 if (tcp->tcp_snd_sack_ok) { 13835 ASSERT(tcp->tcp_sack_info != NULL); 13836 if (tcp->tcp_notsack_list != NULL) { 13837 tcp->tcp_pipe = tcp->tcp_snxt - 13838 tcp->tcp_fack; 13839 tcp->tcp_sack_snxt = seg_ack; 13840 flags |= TH_NEED_SACK_REXMIT; 13841 } else { 13842 /* 13843 * Always initialize tcp_pipe 13844 * even though we don't have 13845 * any SACK info. If later 13846 * we get SACK info and 13847 * tcp_pipe is not initialized, 13848 * funny things will happen. 13849 */ 13850 tcp->tcp_pipe = 13851 tcp->tcp_cwnd_ssthresh; 13852 } 13853 } else { 13854 flags |= TH_REXMIT_NEEDED; 13855 } /* tcp_snd_sack_ok */ 13856 13857 } else { 13858 /* 13859 * Here we perform congestion 13860 * avoidance, but NOT slow start. 13861 * This is known as the Fast 13862 * Recovery Algorithm. 13863 */ 13864 if (tcp->tcp_snd_sack_ok && 13865 tcp->tcp_notsack_list != NULL) { 13866 flags |= TH_NEED_SACK_REXMIT; 13867 tcp->tcp_pipe -= mss; 13868 if (tcp->tcp_pipe < 0) 13869 tcp->tcp_pipe = 0; 13870 } else { 13871 /* 13872 * We know that one more packet has 13873 * left the pipe thus we can update 13874 * cwnd. 13875 */ 13876 cwnd = tcp->tcp_cwnd + mss; 13877 if (cwnd > tcp->tcp_cwnd_max) 13878 cwnd = tcp->tcp_cwnd_max; 13879 tcp->tcp_cwnd = cwnd; 13880 if (tcp->tcp_unsent > 0) 13881 flags |= TH_XMIT_NEEDED; 13882 } 13883 } 13884 } 13885 } else if (tcp->tcp_zero_win_probe) { 13886 /* 13887 * If the window has opened, need to arrange 13888 * to send additional data. 13889 */ 13890 if (new_swnd != 0) { 13891 /* tcp_suna != tcp_snxt */ 13892 /* Packet contains a window update */ 13893 BUMP_MIB(&tcp_mib, tcpInWinUpdate); 13894 tcp->tcp_zero_win_probe = 0; 13895 tcp->tcp_timer_backoff = 0; 13896 tcp->tcp_ms_we_have_waited = 0; 13897 13898 /* 13899 * Transmit starting with tcp_suna since 13900 * the one byte probe is not ack'ed. 13901 * If TCP has sent more than one identical 13902 * probe, tcp_rexmit will be set. That means 13903 * tcp_ss_rexmit() will send out the one 13904 * byte along with new data. Otherwise, 13905 * fake the retransmission. 13906 */ 13907 flags |= TH_XMIT_NEEDED; 13908 if (!tcp->tcp_rexmit) { 13909 tcp->tcp_rexmit = B_TRUE; 13910 tcp->tcp_dupack_cnt = 0; 13911 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 13912 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 13913 } 13914 } 13915 } 13916 goto swnd_update; 13917 } 13918 13919 /* 13920 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 13921 * If the ACK value acks something that we have not yet sent, it might 13922 * be an old duplicate segment. Send an ACK to re-synchronize the 13923 * other side. 13924 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 13925 * state is handled above, so we can always just drop the segment and 13926 * send an ACK here. 13927 * 13928 * Should we send ACKs in response to ACK only segments? 13929 */ 13930 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13931 BUMP_MIB(&tcp_mib, tcpInAckUnsent); 13932 /* drop the received segment */ 13933 freemsg(mp); 13934 13935 /* 13936 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 13937 * greater than 0, check if the number of such 13938 * bogus ACks is greater than that count. If yes, 13939 * don't send back any ACK. This prevents TCP from 13940 * getting into an ACK storm if somehow an attacker 13941 * successfully spoofs an acceptable segment to our 13942 * peer. 13943 */ 13944 if (tcp_drop_ack_unsent_cnt > 0 && 13945 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 13946 TCP_STAT(tcp_in_ack_unsent_drop); 13947 return; 13948 } 13949 mp = tcp_ack_mp(tcp); 13950 if (mp != NULL) { 13951 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 13952 BUMP_LOCAL(tcp->tcp_obsegs); 13953 BUMP_MIB(&tcp_mib, tcpOutAck); 13954 tcp_send_data(tcp, tcp->tcp_wq, mp); 13955 } 13956 return; 13957 } 13958 13959 /* 13960 * TCP gets a new ACK, update the notsack'ed list to delete those 13961 * blocks that are covered by this ACK. 13962 */ 13963 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 13964 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 13965 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 13966 } 13967 13968 /* 13969 * If we got an ACK after fast retransmit, check to see 13970 * if it is a partial ACK. If it is not and the congestion 13971 * window was inflated to account for the other side's 13972 * cached packets, retract it. If it is, do Hoe's algorithm. 13973 */ 13974 if (tcp->tcp_dupack_cnt >= tcp_dupack_fast_retransmit) { 13975 ASSERT(tcp->tcp_rexmit == B_FALSE); 13976 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 13977 tcp->tcp_dupack_cnt = 0; 13978 /* 13979 * Restore the orig tcp_cwnd_ssthresh after 13980 * fast retransmit phase. 13981 */ 13982 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 13983 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 13984 } 13985 tcp->tcp_rexmit_max = seg_ack; 13986 tcp->tcp_cwnd_cnt = 0; 13987 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13988 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13989 13990 /* 13991 * Remove all notsack info to avoid confusion with 13992 * the next fast retrasnmit/recovery phase. 13993 */ 13994 if (tcp->tcp_snd_sack_ok && 13995 tcp->tcp_notsack_list != NULL) { 13996 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 13997 } 13998 } else { 13999 if (tcp->tcp_snd_sack_ok && 14000 tcp->tcp_notsack_list != NULL) { 14001 flags |= TH_NEED_SACK_REXMIT; 14002 tcp->tcp_pipe -= mss; 14003 if (tcp->tcp_pipe < 0) 14004 tcp->tcp_pipe = 0; 14005 } else { 14006 /* 14007 * Hoe's algorithm: 14008 * 14009 * Retransmit the unack'ed segment and 14010 * restart fast recovery. Note that we 14011 * need to scale back tcp_cwnd to the 14012 * original value when we started fast 14013 * recovery. This is to prevent overly 14014 * aggressive behaviour in sending new 14015 * segments. 14016 */ 14017 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 14018 tcp_dupack_fast_retransmit * mss; 14019 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 14020 flags |= TH_REXMIT_NEEDED; 14021 } 14022 } 14023 } else { 14024 tcp->tcp_dupack_cnt = 0; 14025 if (tcp->tcp_rexmit) { 14026 /* 14027 * TCP is retranmitting. If the ACK ack's all 14028 * outstanding data, update tcp_rexmit_max and 14029 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 14030 * to the correct value. 14031 * 14032 * Note that SEQ_LEQ() is used. This is to avoid 14033 * unnecessary fast retransmit caused by dup ACKs 14034 * received when TCP does slow start retransmission 14035 * after a time out. During this phase, TCP may 14036 * send out segments which are already received. 14037 * This causes dup ACKs to be sent back. 14038 */ 14039 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 14040 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 14041 tcp->tcp_rexmit_nxt = seg_ack; 14042 } 14043 if (seg_ack != tcp->tcp_rexmit_max) { 14044 flags |= TH_XMIT_NEEDED; 14045 } 14046 } else { 14047 tcp->tcp_rexmit = B_FALSE; 14048 tcp->tcp_xmit_zc_clean = B_FALSE; 14049 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 14050 tcp->tcp_snd_burst = tcp->tcp_localnet ? 14051 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 14052 } 14053 tcp->tcp_ms_we_have_waited = 0; 14054 } 14055 } 14056 14057 BUMP_MIB(&tcp_mib, tcpInAckSegs); 14058 UPDATE_MIB(&tcp_mib, tcpInAckBytes, bytes_acked); 14059 tcp->tcp_suna = seg_ack; 14060 if (tcp->tcp_zero_win_probe != 0) { 14061 tcp->tcp_zero_win_probe = 0; 14062 tcp->tcp_timer_backoff = 0; 14063 } 14064 14065 /* 14066 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 14067 * Note that it cannot be the SYN being ack'ed. The code flow 14068 * will not reach here. 14069 */ 14070 if (mp1 == NULL) { 14071 goto fin_acked; 14072 } 14073 14074 /* 14075 * Update the congestion window. 14076 * 14077 * If TCP is not ECN capable or TCP is ECN capable but the 14078 * congestion experience bit is not set, increase the tcp_cwnd as 14079 * usual. 14080 */ 14081 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 14082 cwnd = tcp->tcp_cwnd; 14083 add = mss; 14084 14085 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 14086 /* 14087 * This is to prevent an increase of less than 1 MSS of 14088 * tcp_cwnd. With partial increase, tcp_wput_data() 14089 * may send out tinygrams in order to preserve mblk 14090 * boundaries. 14091 * 14092 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 14093 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 14094 * increased by 1 MSS for every RTTs. 14095 */ 14096 if (tcp->tcp_cwnd_cnt <= 0) { 14097 tcp->tcp_cwnd_cnt = cwnd + add; 14098 } else { 14099 tcp->tcp_cwnd_cnt -= add; 14100 add = 0; 14101 } 14102 } 14103 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 14104 } 14105 14106 /* See if the latest urgent data has been acknowledged */ 14107 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 14108 SEQ_GT(seg_ack, tcp->tcp_urg)) 14109 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 14110 14111 /* Can we update the RTT estimates? */ 14112 if (tcp->tcp_snd_ts_ok) { 14113 /* Ignore zero timestamp echo-reply. */ 14114 if (tcpopt.tcp_opt_ts_ecr != 0) { 14115 tcp_set_rto(tcp, (int32_t)lbolt - 14116 (int32_t)tcpopt.tcp_opt_ts_ecr); 14117 } 14118 14119 /* If needed, restart the timer. */ 14120 if (tcp->tcp_set_timer == 1) { 14121 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14122 tcp->tcp_set_timer = 0; 14123 } 14124 /* 14125 * Update tcp_csuna in case the other side stops sending 14126 * us timestamps. 14127 */ 14128 tcp->tcp_csuna = tcp->tcp_snxt; 14129 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 14130 /* 14131 * An ACK sequence we haven't seen before, so get the RTT 14132 * and update the RTO. But first check if the timestamp is 14133 * valid to use. 14134 */ 14135 if ((mp1->b_next != NULL) && 14136 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 14137 tcp_set_rto(tcp, (int32_t)lbolt - 14138 (int32_t)(intptr_t)mp1->b_prev); 14139 else 14140 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 14141 14142 /* Remeber the last sequence to be ACKed */ 14143 tcp->tcp_csuna = seg_ack; 14144 if (tcp->tcp_set_timer == 1) { 14145 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 14146 tcp->tcp_set_timer = 0; 14147 } 14148 } else { 14149 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 14150 } 14151 14152 /* Eat acknowledged bytes off the xmit queue. */ 14153 for (;;) { 14154 mblk_t *mp2; 14155 uchar_t *wptr; 14156 14157 wptr = mp1->b_wptr; 14158 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14159 bytes_acked -= (int)(wptr - mp1->b_rptr); 14160 if (bytes_acked < 0) { 14161 mp1->b_rptr = wptr + bytes_acked; 14162 /* 14163 * Set a new timestamp if all the bytes timed by the 14164 * old timestamp have been ack'ed. 14165 */ 14166 if (SEQ_GT(seg_ack, 14167 (uint32_t)(uintptr_t)(mp1->b_next))) { 14168 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14169 mp1->b_next = NULL; 14170 } 14171 break; 14172 } 14173 mp1->b_next = NULL; 14174 mp1->b_prev = NULL; 14175 mp2 = mp1; 14176 mp1 = mp1->b_cont; 14177 14178 /* 14179 * This notification is required for some zero-copy 14180 * clients to maintain a copy semantic. After the data 14181 * is ack'ed, client is safe to modify or reuse the buffer. 14182 */ 14183 if (tcp->tcp_snd_zcopy_aware && 14184 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14185 tcp_zcopy_notify(tcp); 14186 freeb(mp2); 14187 if (bytes_acked == 0) { 14188 if (mp1 == NULL) { 14189 /* Everything is ack'ed, clear the tail. */ 14190 tcp->tcp_xmit_tail = NULL; 14191 /* 14192 * Cancel the timer unless we are still 14193 * waiting for an ACK for the FIN packet. 14194 */ 14195 if (tcp->tcp_timer_tid != 0 && 14196 tcp->tcp_snxt == tcp->tcp_suna) { 14197 (void) TCP_TIMER_CANCEL(tcp, 14198 tcp->tcp_timer_tid); 14199 tcp->tcp_timer_tid = 0; 14200 } 14201 goto pre_swnd_update; 14202 } 14203 if (mp2 != tcp->tcp_xmit_tail) 14204 break; 14205 tcp->tcp_xmit_tail = mp1; 14206 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14207 (uintptr_t)INT_MAX); 14208 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14209 mp1->b_rptr); 14210 break; 14211 } 14212 if (mp1 == NULL) { 14213 /* 14214 * More was acked but there is nothing more 14215 * outstanding. This means that the FIN was 14216 * just acked or that we're talking to a clown. 14217 */ 14218 fin_acked: 14219 ASSERT(tcp->tcp_fin_sent); 14220 tcp->tcp_xmit_tail = NULL; 14221 if (tcp->tcp_fin_sent) { 14222 /* FIN was acked - making progress */ 14223 if (tcp->tcp_ipversion == IPV6_VERSION && 14224 !tcp->tcp_fin_acked) 14225 tcp->tcp_ip_forward_progress = B_TRUE; 14226 tcp->tcp_fin_acked = B_TRUE; 14227 if (tcp->tcp_linger_tid != 0 && 14228 TCP_TIMER_CANCEL(tcp, 14229 tcp->tcp_linger_tid) >= 0) { 14230 tcp_stop_lingering(tcp); 14231 } 14232 } else { 14233 /* 14234 * We should never get here because 14235 * we have already checked that the 14236 * number of bytes ack'ed should be 14237 * smaller than or equal to what we 14238 * have sent so far (it is the 14239 * acceptability check of the ACK). 14240 * We can only get here if the send 14241 * queue is corrupted. 14242 * 14243 * Terminate the connection and 14244 * panic the system. It is better 14245 * for us to panic instead of 14246 * continuing to avoid other disaster. 14247 */ 14248 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14249 tcp->tcp_rnxt, TH_RST|TH_ACK); 14250 panic("Memory corruption " 14251 "detected for connection %s.", 14252 tcp_display(tcp, NULL, 14253 DISP_ADDR_AND_PORT)); 14254 /*NOTREACHED*/ 14255 } 14256 goto pre_swnd_update; 14257 } 14258 ASSERT(mp2 != tcp->tcp_xmit_tail); 14259 } 14260 if (tcp->tcp_unsent) { 14261 flags |= TH_XMIT_NEEDED; 14262 } 14263 pre_swnd_update: 14264 tcp->tcp_xmit_head = mp1; 14265 swnd_update: 14266 /* 14267 * The following check is different from most other implementations. 14268 * For bi-directional transfer, when segments are dropped, the 14269 * "normal" check will not accept a window update in those 14270 * retransmitted segemnts. Failing to do that, TCP may send out 14271 * segments which are outside receiver's window. As TCP accepts 14272 * the ack in those retransmitted segments, if the window update in 14273 * the same segment is not accepted, TCP will incorrectly calculates 14274 * that it can send more segments. This can create a deadlock 14275 * with the receiver if its window becomes zero. 14276 */ 14277 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14278 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14279 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14280 /* 14281 * The criteria for update is: 14282 * 14283 * 1. the segment acknowledges some data. Or 14284 * 2. the segment is new, i.e. it has a higher seq num. Or 14285 * 3. the segment is not old and the advertised window is 14286 * larger than the previous advertised window. 14287 */ 14288 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14289 flags |= TH_XMIT_NEEDED; 14290 tcp->tcp_swnd = new_swnd; 14291 if (new_swnd > tcp->tcp_max_swnd) 14292 tcp->tcp_max_swnd = new_swnd; 14293 tcp->tcp_swl1 = seg_seq; 14294 tcp->tcp_swl2 = seg_ack; 14295 } 14296 est: 14297 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14298 14299 switch (tcp->tcp_state) { 14300 case TCPS_FIN_WAIT_1: 14301 if (tcp->tcp_fin_acked) { 14302 tcp->tcp_state = TCPS_FIN_WAIT_2; 14303 /* 14304 * We implement the non-standard BSD/SunOS 14305 * FIN_WAIT_2 flushing algorithm. 14306 * If there is no user attached to this 14307 * TCP endpoint, then this TCP struct 14308 * could hang around forever in FIN_WAIT_2 14309 * state if the peer forgets to send us 14310 * a FIN. To prevent this, we wait only 14311 * 2*MSL (a convenient time value) for 14312 * the FIN to arrive. If it doesn't show up, 14313 * we flush the TCP endpoint. This algorithm, 14314 * though a violation of RFC-793, has worked 14315 * for over 10 years in BSD systems. 14316 * Note: SunOS 4.x waits 675 seconds before 14317 * flushing the FIN_WAIT_2 connection. 14318 */ 14319 TCP_TIMER_RESTART(tcp, 14320 tcp_fin_wait_2_flush_interval); 14321 } 14322 break; 14323 case TCPS_FIN_WAIT_2: 14324 break; /* Shutdown hook? */ 14325 case TCPS_LAST_ACK: 14326 freemsg(mp); 14327 if (tcp->tcp_fin_acked) { 14328 (void) tcp_clean_death(tcp, 0, 19); 14329 return; 14330 } 14331 goto xmit_check; 14332 case TCPS_CLOSING: 14333 if (tcp->tcp_fin_acked) { 14334 tcp->tcp_state = TCPS_TIME_WAIT; 14335 if (!TCP_IS_DETACHED(tcp)) { 14336 TCP_TIMER_RESTART(tcp, 14337 tcp_time_wait_interval); 14338 } else { 14339 tcp_time_wait_append(tcp); 14340 TCP_DBGSTAT(tcp_rput_time_wait); 14341 } 14342 } 14343 /*FALLTHRU*/ 14344 case TCPS_CLOSE_WAIT: 14345 freemsg(mp); 14346 goto xmit_check; 14347 default: 14348 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14349 break; 14350 } 14351 } 14352 if (flags & TH_FIN) { 14353 /* Make sure we ack the fin */ 14354 flags |= TH_ACK_NEEDED; 14355 if (!tcp->tcp_fin_rcvd) { 14356 tcp->tcp_fin_rcvd = B_TRUE; 14357 tcp->tcp_rnxt++; 14358 tcph = tcp->tcp_tcph; 14359 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14360 14361 /* 14362 * Generate the ordrel_ind at the end unless we 14363 * are an eager guy. 14364 * In the eager case tcp_rsrv will do this when run 14365 * after tcp_accept is done. 14366 */ 14367 if (tcp->tcp_listener == NULL && 14368 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14369 flags |= TH_ORDREL_NEEDED; 14370 switch (tcp->tcp_state) { 14371 case TCPS_SYN_RCVD: 14372 case TCPS_ESTABLISHED: 14373 tcp->tcp_state = TCPS_CLOSE_WAIT; 14374 /* Keepalive? */ 14375 break; 14376 case TCPS_FIN_WAIT_1: 14377 if (!tcp->tcp_fin_acked) { 14378 tcp->tcp_state = TCPS_CLOSING; 14379 break; 14380 } 14381 /* FALLTHRU */ 14382 case TCPS_FIN_WAIT_2: 14383 tcp->tcp_state = TCPS_TIME_WAIT; 14384 if (!TCP_IS_DETACHED(tcp)) { 14385 TCP_TIMER_RESTART(tcp, 14386 tcp_time_wait_interval); 14387 } else { 14388 tcp_time_wait_append(tcp); 14389 TCP_DBGSTAT(tcp_rput_time_wait); 14390 } 14391 if (seg_len) { 14392 /* 14393 * implies data piggybacked on FIN. 14394 * break to handle data. 14395 */ 14396 break; 14397 } 14398 freemsg(mp); 14399 goto ack_check; 14400 } 14401 } 14402 } 14403 if (mp == NULL) 14404 goto xmit_check; 14405 if (seg_len == 0) { 14406 freemsg(mp); 14407 goto xmit_check; 14408 } 14409 if (mp->b_rptr == mp->b_wptr) { 14410 /* 14411 * The header has been consumed, so we remove the 14412 * zero-length mblk here. 14413 */ 14414 mp1 = mp; 14415 mp = mp->b_cont; 14416 freeb(mp1); 14417 } 14418 tcph = tcp->tcp_tcph; 14419 tcp->tcp_rack_cnt++; 14420 { 14421 uint32_t cur_max; 14422 14423 cur_max = tcp->tcp_rack_cur_max; 14424 if (tcp->tcp_rack_cnt >= cur_max) { 14425 /* 14426 * We have more unacked data than we should - send 14427 * an ACK now. 14428 */ 14429 flags |= TH_ACK_NEEDED; 14430 cur_max++; 14431 if (cur_max > tcp->tcp_rack_abs_max) 14432 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14433 else 14434 tcp->tcp_rack_cur_max = cur_max; 14435 } else if (TCP_IS_DETACHED(tcp)) { 14436 /* We don't have an ACK timer for detached TCP. */ 14437 flags |= TH_ACK_NEEDED; 14438 } else if (seg_len < mss) { 14439 /* 14440 * If we get a segment that is less than an mss, and we 14441 * already have unacknowledged data, and the amount 14442 * unacknowledged is not a multiple of mss, then we 14443 * better generate an ACK now. Otherwise, this may be 14444 * the tail piece of a transaction, and we would rather 14445 * wait for the response. 14446 */ 14447 uint32_t udif; 14448 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14449 (uintptr_t)INT_MAX); 14450 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14451 if (udif && (udif % mss)) 14452 flags |= TH_ACK_NEEDED; 14453 else 14454 flags |= TH_ACK_TIMER_NEEDED; 14455 } else { 14456 /* Start delayed ack timer */ 14457 flags |= TH_ACK_TIMER_NEEDED; 14458 } 14459 } 14460 tcp->tcp_rnxt += seg_len; 14461 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14462 14463 /* Update SACK list */ 14464 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14465 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14466 &(tcp->tcp_num_sack_blk)); 14467 } 14468 14469 if (tcp->tcp_urp_mp) { 14470 tcp->tcp_urp_mp->b_cont = mp; 14471 mp = tcp->tcp_urp_mp; 14472 tcp->tcp_urp_mp = NULL; 14473 /* Ready for a new signal. */ 14474 tcp->tcp_urp_last_valid = B_FALSE; 14475 #ifdef DEBUG 14476 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14477 "tcp_rput: sending exdata_ind %s", 14478 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14479 #endif /* DEBUG */ 14480 } 14481 14482 /* 14483 * Check for ancillary data changes compared to last segment. 14484 */ 14485 if (tcp->tcp_ipv6_recvancillary != 0) { 14486 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14487 if (mp == NULL) 14488 return; 14489 } 14490 14491 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14492 /* 14493 * Side queue inbound data until the accept happens. 14494 * tcp_accept/tcp_rput drains this when the accept happens. 14495 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14496 * T_EXDATA_IND) it is queued on b_next. 14497 * XXX Make urgent data use this. Requires: 14498 * Removing tcp_listener check for TH_URG 14499 * Making M_PCPROTO and MARK messages skip the eager case 14500 */ 14501 14502 if (tcp->tcp_kssl_pending) { 14503 tcp_kssl_input(tcp, mp); 14504 } else { 14505 tcp_rcv_enqueue(tcp, mp, seg_len); 14506 } 14507 } else { 14508 if (mp->b_datap->db_type != M_DATA || 14509 (flags & TH_MARKNEXT_NEEDED)) { 14510 if (tcp->tcp_rcv_list != NULL) { 14511 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14512 } 14513 ASSERT(tcp->tcp_rcv_list == NULL || 14514 tcp->tcp_fused_sigurg); 14515 if (flags & TH_MARKNEXT_NEEDED) { 14516 #ifdef DEBUG 14517 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14518 "tcp_rput: sending MSGMARKNEXT %s", 14519 tcp_display(tcp, NULL, 14520 DISP_PORT_ONLY)); 14521 #endif /* DEBUG */ 14522 mp->b_flag |= MSGMARKNEXT; 14523 flags &= ~TH_MARKNEXT_NEEDED; 14524 } 14525 14526 /* Does this need SSL processing first? */ 14527 if ((tcp->tcp_kssl_ctx != NULL) && 14528 (DB_TYPE(mp) == M_DATA)) { 14529 tcp_kssl_input(tcp, mp); 14530 } else { 14531 putnext(tcp->tcp_rq, mp); 14532 if (!canputnext(tcp->tcp_rq)) 14533 tcp->tcp_rwnd -= seg_len; 14534 } 14535 } else if ((flags & (TH_PUSH|TH_FIN)) || 14536 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) { 14537 if (tcp->tcp_rcv_list != NULL) { 14538 /* 14539 * Enqueue the new segment first and then 14540 * call tcp_rcv_drain() to send all data 14541 * up. The other way to do this is to 14542 * send all queued data up and then call 14543 * putnext() to send the new segment up. 14544 * This way can remove the else part later 14545 * on. 14546 * 14547 * We don't this to avoid one more call to 14548 * canputnext() as tcp_rcv_drain() needs to 14549 * call canputnext(). 14550 */ 14551 tcp_rcv_enqueue(tcp, mp, seg_len); 14552 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14553 } else { 14554 /* Does this need SSL processing first? */ 14555 if ((tcp->tcp_kssl_ctx != NULL) && 14556 (DB_TYPE(mp) == M_DATA)) { 14557 tcp_kssl_input(tcp, mp); 14558 } else { 14559 putnext(tcp->tcp_rq, mp); 14560 if (!canputnext(tcp->tcp_rq)) 14561 tcp->tcp_rwnd -= seg_len; 14562 } 14563 } 14564 } else { 14565 /* 14566 * Enqueue all packets when processing an mblk 14567 * from the co queue and also enqueue normal packets. 14568 */ 14569 tcp_rcv_enqueue(tcp, mp, seg_len); 14570 } 14571 /* 14572 * Make sure the timer is running if we have data waiting 14573 * for a push bit. This provides resiliency against 14574 * implementations that do not correctly generate push bits. 14575 */ 14576 if (tcp->tcp_rcv_list != NULL && tcp->tcp_push_tid == 0) { 14577 /* 14578 * The connection may be closed at this point, so don't 14579 * do anything for a detached tcp. 14580 */ 14581 if (!TCP_IS_DETACHED(tcp)) 14582 tcp->tcp_push_tid = TCP_TIMER(tcp, 14583 tcp_push_timer, 14584 MSEC_TO_TICK(tcp_push_timer_interval)); 14585 } 14586 } 14587 xmit_check: 14588 /* Is there anything left to do? */ 14589 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14590 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14591 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14592 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14593 goto done; 14594 14595 /* Any transmit work to do and a non-zero window? */ 14596 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14597 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14598 if (flags & TH_REXMIT_NEEDED) { 14599 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14600 14601 BUMP_MIB(&tcp_mib, tcpOutFastRetrans); 14602 if (snd_size > mss) 14603 snd_size = mss; 14604 if (snd_size > tcp->tcp_swnd) 14605 snd_size = tcp->tcp_swnd; 14606 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14607 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14608 B_TRUE); 14609 14610 if (mp1 != NULL) { 14611 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14612 tcp->tcp_csuna = tcp->tcp_snxt; 14613 BUMP_MIB(&tcp_mib, tcpRetransSegs); 14614 UPDATE_MIB(&tcp_mib, tcpRetransBytes, snd_size); 14615 TCP_RECORD_TRACE(tcp, mp1, 14616 TCP_TRACE_SEND_PKT); 14617 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14618 } 14619 } 14620 if (flags & TH_NEED_SACK_REXMIT) { 14621 tcp_sack_rxmit(tcp, &flags); 14622 } 14623 /* 14624 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14625 * out new segment. Note that tcp_rexmit should not be 14626 * set, otherwise TH_LIMIT_XMIT should not be set. 14627 */ 14628 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14629 if (!tcp->tcp_rexmit) { 14630 tcp_wput_data(tcp, NULL, B_FALSE); 14631 } else { 14632 tcp_ss_rexmit(tcp); 14633 } 14634 } 14635 /* 14636 * Adjust tcp_cwnd back to normal value after sending 14637 * new data segments. 14638 */ 14639 if (flags & TH_LIMIT_XMIT) { 14640 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14641 /* 14642 * This will restart the timer. Restarting the 14643 * timer is used to avoid a timeout before the 14644 * limited transmitted segment's ACK gets back. 14645 */ 14646 if (tcp->tcp_xmit_head != NULL) 14647 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14648 } 14649 14650 /* Anything more to do? */ 14651 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14652 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14653 goto done; 14654 } 14655 ack_check: 14656 if (flags & TH_SEND_URP_MARK) { 14657 ASSERT(tcp->tcp_urp_mark_mp); 14658 /* 14659 * Send up any queued data and then send the mark message 14660 */ 14661 if (tcp->tcp_rcv_list != NULL) { 14662 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14663 } 14664 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14665 14666 mp1 = tcp->tcp_urp_mark_mp; 14667 tcp->tcp_urp_mark_mp = NULL; 14668 #ifdef DEBUG 14669 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14670 "tcp_rput: sending zero-length %s %s", 14671 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14672 "MSGNOTMARKNEXT"), 14673 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14674 #endif /* DEBUG */ 14675 putnext(tcp->tcp_rq, mp1); 14676 flags &= ~TH_SEND_URP_MARK; 14677 } 14678 if (flags & TH_ACK_NEEDED) { 14679 /* 14680 * Time to send an ack for some reason. 14681 */ 14682 mp1 = tcp_ack_mp(tcp); 14683 14684 if (mp1 != NULL) { 14685 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 14686 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14687 BUMP_LOCAL(tcp->tcp_obsegs); 14688 BUMP_MIB(&tcp_mib, tcpOutAck); 14689 } 14690 if (tcp->tcp_ack_tid != 0) { 14691 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 14692 tcp->tcp_ack_tid = 0; 14693 } 14694 } 14695 if (flags & TH_ACK_TIMER_NEEDED) { 14696 /* 14697 * Arrange for deferred ACK or push wait timeout. 14698 * Start timer if it is not already running. 14699 */ 14700 if (tcp->tcp_ack_tid == 0) { 14701 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 14702 MSEC_TO_TICK(tcp->tcp_localnet ? 14703 (clock_t)tcp_local_dack_interval : 14704 (clock_t)tcp_deferred_ack_interval)); 14705 } 14706 } 14707 if (flags & TH_ORDREL_NEEDED) { 14708 /* 14709 * Send up the ordrel_ind unless we are an eager guy. 14710 * In the eager case tcp_rsrv will do this when run 14711 * after tcp_accept is done. 14712 */ 14713 ASSERT(tcp->tcp_listener == NULL); 14714 if (tcp->tcp_rcv_list != NULL) { 14715 /* 14716 * Push any mblk(s) enqueued from co processing. 14717 */ 14718 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14719 } 14720 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14721 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 14722 tcp->tcp_ordrel_done = B_TRUE; 14723 putnext(tcp->tcp_rq, mp1); 14724 if (tcp->tcp_deferred_clean_death) { 14725 /* 14726 * tcp_clean_death was deferred 14727 * for T_ORDREL_IND - do it now 14728 */ 14729 (void) tcp_clean_death(tcp, 14730 tcp->tcp_client_errno, 20); 14731 tcp->tcp_deferred_clean_death = B_FALSE; 14732 } 14733 } else { 14734 /* 14735 * Run the orderly release in the 14736 * service routine. 14737 */ 14738 qenable(tcp->tcp_rq); 14739 /* 14740 * Caveat(XXX): The machine may be so 14741 * overloaded that tcp_rsrv() is not scheduled 14742 * until after the endpoint has transitioned 14743 * to TCPS_TIME_WAIT 14744 * and tcp_time_wait_interval expires. Then 14745 * tcp_timer() will blow away state in tcp_t 14746 * and T_ORDREL_IND will never be delivered 14747 * upstream. Unlikely but potentially 14748 * a problem. 14749 */ 14750 } 14751 } 14752 done: 14753 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14754 } 14755 14756 /* 14757 * This function does PAWS protection check. Returns B_TRUE if the 14758 * segment passes the PAWS test, else returns B_FALSE. 14759 */ 14760 boolean_t 14761 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 14762 { 14763 uint8_t flags; 14764 int options; 14765 uint8_t *up; 14766 14767 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 14768 /* 14769 * If timestamp option is aligned nicely, get values inline, 14770 * otherwise call general routine to parse. Only do that 14771 * if timestamp is the only option. 14772 */ 14773 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 14774 TCPOPT_REAL_TS_LEN && 14775 OK_32PTR((up = ((uint8_t *)tcph) + 14776 TCP_MIN_HEADER_LENGTH)) && 14777 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 14778 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 14779 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 14780 14781 options = TCP_OPT_TSTAMP_PRESENT; 14782 } else { 14783 if (tcp->tcp_snd_sack_ok) { 14784 tcpoptp->tcp = tcp; 14785 } else { 14786 tcpoptp->tcp = NULL; 14787 } 14788 options = tcp_parse_options(tcph, tcpoptp); 14789 } 14790 14791 if (options & TCP_OPT_TSTAMP_PRESENT) { 14792 /* 14793 * Do PAWS per RFC 1323 section 4.2. Accept RST 14794 * regardless of the timestamp, page 18 RFC 1323.bis. 14795 */ 14796 if ((flags & TH_RST) == 0 && 14797 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 14798 tcp->tcp_ts_recent)) { 14799 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 14800 PAWS_TIMEOUT)) { 14801 /* This segment is not acceptable. */ 14802 return (B_FALSE); 14803 } else { 14804 /* 14805 * Connection has been idle for 14806 * too long. Reset the timestamp 14807 * and assume the segment is valid. 14808 */ 14809 tcp->tcp_ts_recent = 14810 tcpoptp->tcp_opt_ts_val; 14811 } 14812 } 14813 } else { 14814 /* 14815 * If we don't get a timestamp on every packet, we 14816 * figure we can't really trust 'em, so we stop sending 14817 * and parsing them. 14818 */ 14819 tcp->tcp_snd_ts_ok = B_FALSE; 14820 14821 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 14822 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 14823 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 14824 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN); 14825 if (tcp->tcp_snd_sack_ok) { 14826 ASSERT(tcp->tcp_sack_info != NULL); 14827 tcp->tcp_max_sack_blk = 4; 14828 } 14829 } 14830 return (B_TRUE); 14831 } 14832 14833 /* 14834 * Attach ancillary data to a received TCP segments for the 14835 * ancillary pieces requested by the application that are 14836 * different than they were in the previous data segment. 14837 * 14838 * Save the "current" values once memory allocation is ok so that 14839 * when memory allocation fails we can just wait for the next data segment. 14840 */ 14841 static mblk_t * 14842 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 14843 { 14844 struct T_optdata_ind *todi; 14845 int optlen; 14846 uchar_t *optptr; 14847 struct T_opthdr *toh; 14848 uint_t addflag; /* Which pieces to add */ 14849 mblk_t *mp1; 14850 14851 optlen = 0; 14852 addflag = 0; 14853 /* If app asked for pktinfo and the index has changed ... */ 14854 if ((ipp->ipp_fields & IPPF_IFINDEX) && 14855 ipp->ipp_ifindex != tcp->tcp_recvifindex && 14856 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 14857 optlen += sizeof (struct T_opthdr) + 14858 sizeof (struct in6_pktinfo); 14859 addflag |= TCP_IPV6_RECVPKTINFO; 14860 } 14861 /* If app asked for hoplimit and it has changed ... */ 14862 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 14863 ipp->ipp_hoplimit != tcp->tcp_recvhops && 14864 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 14865 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 14866 addflag |= TCP_IPV6_RECVHOPLIMIT; 14867 } 14868 /* If app asked for tclass and it has changed ... */ 14869 if ((ipp->ipp_fields & IPPF_TCLASS) && 14870 ipp->ipp_tclass != tcp->tcp_recvtclass && 14871 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 14872 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 14873 addflag |= TCP_IPV6_RECVTCLASS; 14874 } 14875 /* 14876 * If app asked for hopbyhop headers and it has changed ... 14877 * For security labels, note that (1) security labels can't change on 14878 * a connected socket at all, (2) we're connected to at most one peer, 14879 * (3) if anything changes, then it must be some other extra option. 14880 */ 14881 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 14882 ip_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 14883 (ipp->ipp_fields & IPPF_HOPOPTS), 14884 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 14885 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen - 14886 tcp->tcp_label_len; 14887 addflag |= TCP_IPV6_RECVHOPOPTS; 14888 if (!ip_allocbuf((void **)&tcp->tcp_hopopts, 14889 &tcp->tcp_hopoptslen, (ipp->ipp_fields & IPPF_HOPOPTS), 14890 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 14891 return (mp); 14892 } 14893 /* If app asked for dst headers before routing headers ... */ 14894 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 14895 ip_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 14896 (ipp->ipp_fields & IPPF_RTDSTOPTS), 14897 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 14898 optlen += sizeof (struct T_opthdr) + 14899 ipp->ipp_rtdstoptslen; 14900 addflag |= TCP_IPV6_RECVRTDSTOPTS; 14901 if (!ip_allocbuf((void **)&tcp->tcp_rtdstopts, 14902 &tcp->tcp_rtdstoptslen, (ipp->ipp_fields & IPPF_RTDSTOPTS), 14903 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 14904 return (mp); 14905 } 14906 /* If app asked for routing headers and it has changed ... */ 14907 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 14908 ip_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 14909 (ipp->ipp_fields & IPPF_RTHDR), 14910 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 14911 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 14912 addflag |= TCP_IPV6_RECVRTHDR; 14913 if (!ip_allocbuf((void **)&tcp->tcp_rthdr, 14914 &tcp->tcp_rthdrlen, (ipp->ipp_fields & IPPF_RTHDR), 14915 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 14916 return (mp); 14917 } 14918 /* If app asked for dest headers and it has changed ... */ 14919 if ((tcp->tcp_ipv6_recvancillary & 14920 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 14921 ip_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 14922 (ipp->ipp_fields & IPPF_DSTOPTS), 14923 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 14924 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 14925 addflag |= TCP_IPV6_RECVDSTOPTS; 14926 if (!ip_allocbuf((void **)&tcp->tcp_dstopts, 14927 &tcp->tcp_dstoptslen, (ipp->ipp_fields & IPPF_DSTOPTS), 14928 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 14929 return (mp); 14930 } 14931 14932 if (optlen == 0) { 14933 /* Nothing to add */ 14934 return (mp); 14935 } 14936 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 14937 if (mp1 == NULL) { 14938 /* 14939 * Defer sending ancillary data until the next TCP segment 14940 * arrives. 14941 */ 14942 return (mp); 14943 } 14944 mp1->b_cont = mp; 14945 mp = mp1; 14946 mp->b_wptr += sizeof (*todi) + optlen; 14947 mp->b_datap->db_type = M_PROTO; 14948 todi = (struct T_optdata_ind *)mp->b_rptr; 14949 todi->PRIM_type = T_OPTDATA_IND; 14950 todi->DATA_flag = 1; /* MORE data */ 14951 todi->OPT_length = optlen; 14952 todi->OPT_offset = sizeof (*todi); 14953 optptr = (uchar_t *)&todi[1]; 14954 /* 14955 * If app asked for pktinfo and the index has changed ... 14956 * Note that the local address never changes for the connection. 14957 */ 14958 if (addflag & TCP_IPV6_RECVPKTINFO) { 14959 struct in6_pktinfo *pkti; 14960 14961 toh = (struct T_opthdr *)optptr; 14962 toh->level = IPPROTO_IPV6; 14963 toh->name = IPV6_PKTINFO; 14964 toh->len = sizeof (*toh) + sizeof (*pkti); 14965 toh->status = 0; 14966 optptr += sizeof (*toh); 14967 pkti = (struct in6_pktinfo *)optptr; 14968 if (tcp->tcp_ipversion == IPV6_VERSION) 14969 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 14970 else 14971 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 14972 &pkti->ipi6_addr); 14973 pkti->ipi6_ifindex = ipp->ipp_ifindex; 14974 optptr += sizeof (*pkti); 14975 ASSERT(OK_32PTR(optptr)); 14976 /* Save as "last" value */ 14977 tcp->tcp_recvifindex = ipp->ipp_ifindex; 14978 } 14979 /* If app asked for hoplimit and it has changed ... */ 14980 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 14981 toh = (struct T_opthdr *)optptr; 14982 toh->level = IPPROTO_IPV6; 14983 toh->name = IPV6_HOPLIMIT; 14984 toh->len = sizeof (*toh) + sizeof (uint_t); 14985 toh->status = 0; 14986 optptr += sizeof (*toh); 14987 *(uint_t *)optptr = ipp->ipp_hoplimit; 14988 optptr += sizeof (uint_t); 14989 ASSERT(OK_32PTR(optptr)); 14990 /* Save as "last" value */ 14991 tcp->tcp_recvhops = ipp->ipp_hoplimit; 14992 } 14993 /* If app asked for tclass and it has changed ... */ 14994 if (addflag & TCP_IPV6_RECVTCLASS) { 14995 toh = (struct T_opthdr *)optptr; 14996 toh->level = IPPROTO_IPV6; 14997 toh->name = IPV6_TCLASS; 14998 toh->len = sizeof (*toh) + sizeof (uint_t); 14999 toh->status = 0; 15000 optptr += sizeof (*toh); 15001 *(uint_t *)optptr = ipp->ipp_tclass; 15002 optptr += sizeof (uint_t); 15003 ASSERT(OK_32PTR(optptr)); 15004 /* Save as "last" value */ 15005 tcp->tcp_recvtclass = ipp->ipp_tclass; 15006 } 15007 if (addflag & TCP_IPV6_RECVHOPOPTS) { 15008 toh = (struct T_opthdr *)optptr; 15009 toh->level = IPPROTO_IPV6; 15010 toh->name = IPV6_HOPOPTS; 15011 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen - 15012 tcp->tcp_label_len; 15013 toh->status = 0; 15014 optptr += sizeof (*toh); 15015 bcopy((uchar_t *)ipp->ipp_hopopts + tcp->tcp_label_len, optptr, 15016 ipp->ipp_hopoptslen - tcp->tcp_label_len); 15017 optptr += ipp->ipp_hopoptslen - tcp->tcp_label_len; 15018 ASSERT(OK_32PTR(optptr)); 15019 /* Save as last value */ 15020 ip_savebuf((void **)&tcp->tcp_hopopts, &tcp->tcp_hopoptslen, 15021 (ipp->ipp_fields & IPPF_HOPOPTS), 15022 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 15023 } 15024 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 15025 toh = (struct T_opthdr *)optptr; 15026 toh->level = IPPROTO_IPV6; 15027 toh->name = IPV6_RTHDRDSTOPTS; 15028 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 15029 toh->status = 0; 15030 optptr += sizeof (*toh); 15031 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 15032 optptr += ipp->ipp_rtdstoptslen; 15033 ASSERT(OK_32PTR(optptr)); 15034 /* Save as last value */ 15035 ip_savebuf((void **)&tcp->tcp_rtdstopts, 15036 &tcp->tcp_rtdstoptslen, 15037 (ipp->ipp_fields & IPPF_RTDSTOPTS), 15038 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 15039 } 15040 if (addflag & TCP_IPV6_RECVRTHDR) { 15041 toh = (struct T_opthdr *)optptr; 15042 toh->level = IPPROTO_IPV6; 15043 toh->name = IPV6_RTHDR; 15044 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 15045 toh->status = 0; 15046 optptr += sizeof (*toh); 15047 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 15048 optptr += ipp->ipp_rthdrlen; 15049 ASSERT(OK_32PTR(optptr)); 15050 /* Save as last value */ 15051 ip_savebuf((void **)&tcp->tcp_rthdr, &tcp->tcp_rthdrlen, 15052 (ipp->ipp_fields & IPPF_RTHDR), 15053 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 15054 } 15055 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 15056 toh = (struct T_opthdr *)optptr; 15057 toh->level = IPPROTO_IPV6; 15058 toh->name = IPV6_DSTOPTS; 15059 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 15060 toh->status = 0; 15061 optptr += sizeof (*toh); 15062 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 15063 optptr += ipp->ipp_dstoptslen; 15064 ASSERT(OK_32PTR(optptr)); 15065 /* Save as last value */ 15066 ip_savebuf((void **)&tcp->tcp_dstopts, &tcp->tcp_dstoptslen, 15067 (ipp->ipp_fields & IPPF_DSTOPTS), 15068 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 15069 } 15070 ASSERT(optptr == mp->b_wptr); 15071 return (mp); 15072 } 15073 15074 15075 /* 15076 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 15077 * or a "bad" IRE detected by tcp_adapt_ire. 15078 * We can't tell if the failure was due to the laddr or the faddr 15079 * thus we clear out all addresses and ports. 15080 */ 15081 static void 15082 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 15083 { 15084 queue_t *q = tcp->tcp_rq; 15085 tcph_t *tcph; 15086 struct T_error_ack *tea; 15087 conn_t *connp = tcp->tcp_connp; 15088 15089 15090 ASSERT(mp->b_datap->db_type == M_PCPROTO); 15091 15092 if (mp->b_cont) { 15093 freemsg(mp->b_cont); 15094 mp->b_cont = NULL; 15095 } 15096 tea = (struct T_error_ack *)mp->b_rptr; 15097 switch (tea->PRIM_type) { 15098 case T_BIND_ACK: 15099 /* 15100 * Need to unbind with classifier since we were just told that 15101 * our bind succeeded. 15102 */ 15103 tcp->tcp_hard_bound = B_FALSE; 15104 tcp->tcp_hard_binding = B_FALSE; 15105 15106 ipcl_hash_remove(connp); 15107 /* Reuse the mblk if possible */ 15108 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 15109 sizeof (*tea)); 15110 mp->b_rptr = mp->b_datap->db_base; 15111 mp->b_wptr = mp->b_rptr + sizeof (*tea); 15112 tea = (struct T_error_ack *)mp->b_rptr; 15113 tea->PRIM_type = T_ERROR_ACK; 15114 tea->TLI_error = TSYSERR; 15115 tea->UNIX_error = error; 15116 if (tcp->tcp_state >= TCPS_SYN_SENT) { 15117 tea->ERROR_prim = T_CONN_REQ; 15118 } else { 15119 tea->ERROR_prim = O_T_BIND_REQ; 15120 } 15121 break; 15122 15123 case T_ERROR_ACK: 15124 if (tcp->tcp_state >= TCPS_SYN_SENT) 15125 tea->ERROR_prim = T_CONN_REQ; 15126 break; 15127 default: 15128 panic("tcp_bind_failed: unexpected TPI type"); 15129 /*NOTREACHED*/ 15130 } 15131 15132 tcp->tcp_state = TCPS_IDLE; 15133 if (tcp->tcp_ipversion == IPV4_VERSION) 15134 tcp->tcp_ipha->ipha_src = 0; 15135 else 15136 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 15137 /* 15138 * Copy of the src addr. in tcp_t is needed since 15139 * the lookup funcs. can only look at tcp_t 15140 */ 15141 V6_SET_ZERO(tcp->tcp_ip_src_v6); 15142 15143 tcph = tcp->tcp_tcph; 15144 tcph->th_lport[0] = 0; 15145 tcph->th_lport[1] = 0; 15146 tcp_bind_hash_remove(tcp); 15147 bzero(&connp->u_port, sizeof (connp->u_port)); 15148 /* blow away saved option results if any */ 15149 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15150 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15151 15152 conn_delete_ire(tcp->tcp_connp, NULL); 15153 putnext(q, mp); 15154 } 15155 15156 /* 15157 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15158 * messages. 15159 */ 15160 void 15161 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15162 { 15163 mblk_t *mp1; 15164 uchar_t *rptr = mp->b_rptr; 15165 queue_t *q = tcp->tcp_rq; 15166 struct T_error_ack *tea; 15167 uint32_t mss; 15168 mblk_t *syn_mp; 15169 mblk_t *mdti; 15170 int retval; 15171 mblk_t *ire_mp; 15172 15173 switch (mp->b_datap->db_type) { 15174 case M_PROTO: 15175 case M_PCPROTO: 15176 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15177 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15178 break; 15179 tea = (struct T_error_ack *)rptr; 15180 switch (tea->PRIM_type) { 15181 case T_BIND_ACK: 15182 /* 15183 * Adapt Multidata information, if any. The 15184 * following tcp_mdt_update routine will free 15185 * the message. 15186 */ 15187 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15188 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15189 b_rptr)->mdt_capab, B_TRUE); 15190 freemsg(mdti); 15191 } 15192 15193 /* Get the IRE, if we had requested for it */ 15194 ire_mp = tcp_ire_mp(mp); 15195 15196 if (tcp->tcp_hard_binding) { 15197 tcp->tcp_hard_binding = B_FALSE; 15198 tcp->tcp_hard_bound = B_TRUE; 15199 CL_INET_CONNECT(tcp); 15200 } else { 15201 if (ire_mp != NULL) 15202 freeb(ire_mp); 15203 goto after_syn_sent; 15204 } 15205 15206 retval = tcp_adapt_ire(tcp, ire_mp); 15207 if (ire_mp != NULL) 15208 freeb(ire_mp); 15209 if (retval == 0) { 15210 tcp_bind_failed(tcp, mp, 15211 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15212 ENETUNREACH : EADDRNOTAVAIL)); 15213 return; 15214 } 15215 /* 15216 * Don't let an endpoint connect to itself. 15217 * Also checked in tcp_connect() but that 15218 * check can't handle the case when the 15219 * local IP address is INADDR_ANY. 15220 */ 15221 if (tcp->tcp_ipversion == IPV4_VERSION) { 15222 if ((tcp->tcp_ipha->ipha_dst == 15223 tcp->tcp_ipha->ipha_src) && 15224 (BE16_EQL(tcp->tcp_tcph->th_lport, 15225 tcp->tcp_tcph->th_fport))) { 15226 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15227 return; 15228 } 15229 } else { 15230 if (IN6_ARE_ADDR_EQUAL( 15231 &tcp->tcp_ip6h->ip6_dst, 15232 &tcp->tcp_ip6h->ip6_src) && 15233 (BE16_EQL(tcp->tcp_tcph->th_lport, 15234 tcp->tcp_tcph->th_fport))) { 15235 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15236 return; 15237 } 15238 } 15239 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15240 /* 15241 * This should not be possible! Just for 15242 * defensive coding... 15243 */ 15244 if (tcp->tcp_state != TCPS_SYN_SENT) 15245 goto after_syn_sent; 15246 15247 if (is_system_labeled() && 15248 !tcp_update_label(tcp, CONN_CRED(tcp->tcp_connp))) { 15249 tcp_bind_failed(tcp, mp, EHOSTUNREACH); 15250 return; 15251 } 15252 15253 ASSERT(q == tcp->tcp_rq); 15254 /* 15255 * tcp_adapt_ire() does not adjust 15256 * for TCP/IP header length. 15257 */ 15258 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15259 15260 /* 15261 * Just make sure our rwnd is at 15262 * least tcp_recv_hiwat_mss * MSS 15263 * large, and round up to the nearest 15264 * MSS. 15265 * 15266 * We do the round up here because 15267 * we need to get the interface 15268 * MTU first before we can do the 15269 * round up. 15270 */ 15271 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15272 tcp_recv_hiwat_minmss * mss); 15273 q->q_hiwat = tcp->tcp_rwnd; 15274 tcp_set_ws_value(tcp); 15275 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15276 tcp->tcp_tcph->th_win); 15277 if (tcp->tcp_rcv_ws > 0 || tcp_wscale_always) 15278 tcp->tcp_snd_ws_ok = B_TRUE; 15279 15280 /* 15281 * Set tcp_snd_ts_ok to true 15282 * so that tcp_xmit_mp will 15283 * include the timestamp 15284 * option in the SYN segment. 15285 */ 15286 if (tcp_tstamp_always || 15287 (tcp->tcp_rcv_ws && tcp_tstamp_if_wscale)) { 15288 tcp->tcp_snd_ts_ok = B_TRUE; 15289 } 15290 15291 /* 15292 * tcp_snd_sack_ok can be set in 15293 * tcp_adapt_ire() if the sack metric 15294 * is set. So check it here also. 15295 */ 15296 if (tcp_sack_permitted == 2 || 15297 tcp->tcp_snd_sack_ok) { 15298 if (tcp->tcp_sack_info == NULL) { 15299 tcp->tcp_sack_info = 15300 kmem_cache_alloc(tcp_sack_info_cache, 15301 KM_SLEEP); 15302 } 15303 tcp->tcp_snd_sack_ok = B_TRUE; 15304 } 15305 15306 /* 15307 * Should we use ECN? Note that the current 15308 * default value (SunOS 5.9) of tcp_ecn_permitted 15309 * is 1. The reason for doing this is that there 15310 * are equipments out there that will drop ECN 15311 * enabled IP packets. Setting it to 1 avoids 15312 * compatibility problems. 15313 */ 15314 if (tcp_ecn_permitted == 2) 15315 tcp->tcp_ecn_ok = B_TRUE; 15316 15317 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15318 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15319 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15320 if (syn_mp) { 15321 cred_t *cr; 15322 pid_t pid; 15323 15324 /* 15325 * Obtain the credential from the 15326 * thread calling connect(); the credential 15327 * lives on in the second mblk which 15328 * originated from T_CONN_REQ and is echoed 15329 * with the T_BIND_ACK from ip. If none 15330 * can be found, default to the creator 15331 * of the socket. 15332 */ 15333 if (mp->b_cont == NULL || 15334 (cr = DB_CRED(mp->b_cont)) == NULL) { 15335 cr = tcp->tcp_cred; 15336 pid = tcp->tcp_cpid; 15337 } else { 15338 pid = DB_CPID(mp->b_cont); 15339 } 15340 15341 TCP_RECORD_TRACE(tcp, syn_mp, 15342 TCP_TRACE_SEND_PKT); 15343 mblk_setcred(syn_mp, cr); 15344 DB_CPID(syn_mp) = pid; 15345 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15346 } 15347 after_syn_sent: 15348 /* 15349 * A trailer mblk indicates a waiting client upstream. 15350 * We complete here the processing begun in 15351 * either tcp_bind() or tcp_connect() by passing 15352 * upstream the reply message they supplied. 15353 */ 15354 mp1 = mp; 15355 mp = mp->b_cont; 15356 freeb(mp1); 15357 if (mp) 15358 break; 15359 return; 15360 case T_ERROR_ACK: 15361 if (tcp->tcp_debug) { 15362 (void) strlog(TCP_MOD_ID, 0, 1, 15363 SL_TRACE|SL_ERROR, 15364 "tcp_rput_other: case T_ERROR_ACK, " 15365 "ERROR_prim == %d", 15366 tea->ERROR_prim); 15367 } 15368 switch (tea->ERROR_prim) { 15369 case O_T_BIND_REQ: 15370 case T_BIND_REQ: 15371 tcp_bind_failed(tcp, mp, 15372 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15373 ENETUNREACH : EADDRNOTAVAIL)); 15374 return; 15375 case T_UNBIND_REQ: 15376 tcp->tcp_hard_binding = B_FALSE; 15377 tcp->tcp_hard_bound = B_FALSE; 15378 if (mp->b_cont) { 15379 freemsg(mp->b_cont); 15380 mp->b_cont = NULL; 15381 } 15382 if (tcp->tcp_unbind_pending) 15383 tcp->tcp_unbind_pending = 0; 15384 else { 15385 /* From tcp_ip_unbind() - free */ 15386 freemsg(mp); 15387 return; 15388 } 15389 break; 15390 case T_SVR4_OPTMGMT_REQ: 15391 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15392 /* T_OPTMGMT_REQ generated by TCP */ 15393 printf("T_SVR4_OPTMGMT_REQ failed " 15394 "%d/%d - dropped (cnt %d)\n", 15395 tea->TLI_error, tea->UNIX_error, 15396 tcp->tcp_drop_opt_ack_cnt); 15397 freemsg(mp); 15398 tcp->tcp_drop_opt_ack_cnt--; 15399 return; 15400 } 15401 break; 15402 } 15403 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15404 tcp->tcp_drop_opt_ack_cnt > 0) { 15405 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15406 "- dropped (cnt %d)\n", 15407 tea->TLI_error, tea->UNIX_error, 15408 tcp->tcp_drop_opt_ack_cnt); 15409 freemsg(mp); 15410 tcp->tcp_drop_opt_ack_cnt--; 15411 return; 15412 } 15413 break; 15414 case T_OPTMGMT_ACK: 15415 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15416 /* T_OPTMGMT_REQ generated by TCP */ 15417 freemsg(mp); 15418 tcp->tcp_drop_opt_ack_cnt--; 15419 return; 15420 } 15421 break; 15422 default: 15423 break; 15424 } 15425 break; 15426 case M_CTL: 15427 /* 15428 * ICMP messages. 15429 */ 15430 tcp_icmp_error(tcp, mp); 15431 return; 15432 case M_FLUSH: 15433 if (*rptr & FLUSHR) 15434 flushq(q, FLUSHDATA); 15435 break; 15436 default: 15437 break; 15438 } 15439 /* 15440 * Make sure we set this bit before sending the ACK for 15441 * bind. Otherwise accept could possibly run and free 15442 * this tcp struct. 15443 */ 15444 putnext(q, mp); 15445 } 15446 15447 /* 15448 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15449 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15450 * tcp_rsrv() try again. 15451 */ 15452 static void 15453 tcp_ordrel_kick(void *arg) 15454 { 15455 conn_t *connp = (conn_t *)arg; 15456 tcp_t *tcp = connp->conn_tcp; 15457 15458 tcp->tcp_ordrelid = 0; 15459 tcp->tcp_timeout = B_FALSE; 15460 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15461 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15462 qenable(tcp->tcp_rq); 15463 } 15464 } 15465 15466 /* ARGSUSED */ 15467 static void 15468 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15469 { 15470 conn_t *connp = (conn_t *)arg; 15471 tcp_t *tcp = connp->conn_tcp; 15472 queue_t *q = tcp->tcp_rq; 15473 uint_t thwin; 15474 15475 freeb(mp); 15476 15477 TCP_STAT(tcp_rsrv_calls); 15478 15479 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15480 return; 15481 } 15482 15483 if (tcp->tcp_fused) { 15484 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15485 15486 ASSERT(tcp->tcp_fused); 15487 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15488 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15489 ASSERT(!TCP_IS_DETACHED(tcp)); 15490 ASSERT(tcp->tcp_connp->conn_sqp == 15491 peer_tcp->tcp_connp->conn_sqp); 15492 15493 /* 15494 * Normally we would not get backenabled in synchronous 15495 * streams mode, but in case this happens, we need to stop 15496 * synchronous streams temporarily to prevent a race with 15497 * tcp_fuse_rrw() or tcp_fuse_rinfop(). It is safe to access 15498 * tcp_rcv_list here because those entry points will return 15499 * right away when synchronous streams is stopped. 15500 */ 15501 TCP_FUSE_SYNCSTR_STOP(tcp); 15502 if (tcp->tcp_rcv_list != NULL) 15503 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15504 15505 tcp_clrqfull(peer_tcp); 15506 TCP_FUSE_SYNCSTR_RESUME(tcp); 15507 TCP_STAT(tcp_fusion_backenabled); 15508 return; 15509 } 15510 15511 if (canputnext(q)) { 15512 tcp->tcp_rwnd = q->q_hiwat; 15513 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15514 << tcp->tcp_rcv_ws; 15515 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15516 /* 15517 * Send back a window update immediately if TCP is above 15518 * ESTABLISHED state and the increase of the rcv window 15519 * that the other side knows is at least 1 MSS after flow 15520 * control is lifted. 15521 */ 15522 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15523 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15524 tcp_xmit_ctl(NULL, tcp, 15525 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15526 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15527 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 15528 } 15529 } 15530 /* Handle a failure to allocate a T_ORDREL_IND here */ 15531 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15532 ASSERT(tcp->tcp_listener == NULL); 15533 if (tcp->tcp_rcv_list != NULL) { 15534 (void) tcp_rcv_drain(q, tcp); 15535 } 15536 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15537 mp = mi_tpi_ordrel_ind(); 15538 if (mp) { 15539 tcp->tcp_ordrel_done = B_TRUE; 15540 putnext(q, mp); 15541 if (tcp->tcp_deferred_clean_death) { 15542 /* 15543 * tcp_clean_death was deferred for 15544 * T_ORDREL_IND - do it now 15545 */ 15546 tcp->tcp_deferred_clean_death = B_FALSE; 15547 (void) tcp_clean_death(tcp, 15548 tcp->tcp_client_errno, 22); 15549 } 15550 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15551 /* 15552 * If there isn't already a timer running 15553 * start one. Use a 4 second 15554 * timer as a fallback since it can't fail. 15555 */ 15556 tcp->tcp_timeout = B_TRUE; 15557 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15558 MSEC_TO_TICK(4000)); 15559 } 15560 } 15561 } 15562 15563 /* 15564 * The read side service routine is called mostly when we get back-enabled as a 15565 * result of flow control relief. Since we don't actually queue anything in 15566 * TCP, we have no data to send out of here. What we do is clear the receive 15567 * window, and send out a window update. 15568 * This routine is also called to drive an orderly release message upstream 15569 * if the attempt in tcp_rput failed. 15570 */ 15571 static void 15572 tcp_rsrv(queue_t *q) 15573 { 15574 conn_t *connp = Q_TO_CONN(q); 15575 tcp_t *tcp = connp->conn_tcp; 15576 mblk_t *mp; 15577 15578 /* No code does a putq on the read side */ 15579 ASSERT(q->q_first == NULL); 15580 15581 /* Nothing to do for the default queue */ 15582 if (q == tcp_g_q) { 15583 return; 15584 } 15585 15586 mp = allocb(0, BPRI_HI); 15587 if (mp == NULL) { 15588 /* 15589 * We are under memory pressure. Return for now and we 15590 * we will be called again later. 15591 */ 15592 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15593 /* 15594 * If there isn't already a timer running 15595 * start one. Use a 4 second 15596 * timer as a fallback since it can't fail. 15597 */ 15598 tcp->tcp_timeout = B_TRUE; 15599 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15600 MSEC_TO_TICK(4000)); 15601 } 15602 return; 15603 } 15604 CONN_INC_REF(connp); 15605 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15606 SQTAG_TCP_RSRV); 15607 } 15608 15609 /* 15610 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15611 * We do not allow the receive window to shrink. After setting rwnd, 15612 * set the flow control hiwat of the stream. 15613 * 15614 * This function is called in 2 cases: 15615 * 15616 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15617 * connection (passive open) and in tcp_rput_data() for active connect. 15618 * This is called after tcp_mss_set() when the desired MSS value is known. 15619 * This makes sure that our window size is a mutiple of the other side's 15620 * MSS. 15621 * 2) Handling SO_RCVBUF option. 15622 * 15623 * It is ASSUMED that the requested size is a multiple of the current MSS. 15624 * 15625 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15626 * user requests so. 15627 */ 15628 static int 15629 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15630 { 15631 uint32_t mss = tcp->tcp_mss; 15632 uint32_t old_max_rwnd; 15633 uint32_t max_transmittable_rwnd; 15634 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15635 15636 if (tcp->tcp_fused) { 15637 size_t sth_hiwat; 15638 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15639 15640 ASSERT(peer_tcp != NULL); 15641 /* 15642 * Record the stream head's high water mark for 15643 * this endpoint; this is used for flow-control 15644 * purposes in tcp_fuse_output(). 15645 */ 15646 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15647 if (!tcp_detached) 15648 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 15649 15650 /* 15651 * In the fusion case, the maxpsz stream head value of 15652 * our peer is set according to its send buffer size 15653 * and our receive buffer size; since the latter may 15654 * have changed we need to update the peer's maxpsz. 15655 */ 15656 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15657 return (rwnd); 15658 } 15659 15660 if (tcp_detached) 15661 old_max_rwnd = tcp->tcp_rwnd; 15662 else 15663 old_max_rwnd = tcp->tcp_rq->q_hiwat; 15664 15665 /* 15666 * Insist on a receive window that is at least 15667 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15668 * funny TCP interactions of Nagle algorithm, SWS avoidance 15669 * and delayed acknowledgement. 15670 */ 15671 rwnd = MAX(rwnd, tcp_recv_hiwat_minmss * mss); 15672 15673 /* 15674 * If window size info has already been exchanged, TCP should not 15675 * shrink the window. Shrinking window is doable if done carefully. 15676 * We may add that support later. But so far there is not a real 15677 * need to do that. 15678 */ 15679 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15680 /* MSS may have changed, do a round up again. */ 15681 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15682 } 15683 15684 /* 15685 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15686 * can be applied even before the window scale option is decided. 15687 */ 15688 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15689 if (rwnd > max_transmittable_rwnd) { 15690 rwnd = max_transmittable_rwnd - 15691 (max_transmittable_rwnd % mss); 15692 if (rwnd < mss) 15693 rwnd = max_transmittable_rwnd; 15694 /* 15695 * If we're over the limit we may have to back down tcp_rwnd. 15696 * The increment below won't work for us. So we set all three 15697 * here and the increment below will have no effect. 15698 */ 15699 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15700 } 15701 if (tcp->tcp_localnet) { 15702 tcp->tcp_rack_abs_max = 15703 MIN(tcp_local_dacks_max, rwnd / mss / 2); 15704 } else { 15705 /* 15706 * For a remote host on a different subnet (through a router), 15707 * we ack every other packet to be conforming to RFC1122. 15708 * tcp_deferred_acks_max is default to 2. 15709 */ 15710 tcp->tcp_rack_abs_max = 15711 MIN(tcp_deferred_acks_max, rwnd / mss / 2); 15712 } 15713 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15714 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15715 else 15716 tcp->tcp_rack_cur_max = 0; 15717 /* 15718 * Increment the current rwnd by the amount the maximum grew (we 15719 * can not overwrite it since we might be in the middle of a 15720 * connection.) 15721 */ 15722 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15723 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15724 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15725 tcp->tcp_cwnd_max = rwnd; 15726 15727 if (tcp_detached) 15728 return (rwnd); 15729 /* 15730 * We set the maximum receive window into rq->q_hiwat. 15731 * This is not actually used for flow control. 15732 */ 15733 tcp->tcp_rq->q_hiwat = rwnd; 15734 /* 15735 * Set the Stream head high water mark. This doesn't have to be 15736 * here, since we are simply using default values, but we would 15737 * prefer to choose these values algorithmically, with a likely 15738 * relationship to rwnd. 15739 */ 15740 (void) mi_set_sth_hiwat(tcp->tcp_rq, MAX(rwnd, tcp_sth_rcv_hiwat)); 15741 return (rwnd); 15742 } 15743 15744 /* 15745 * Return SNMP stuff in buffer in mpdata. 15746 */ 15747 int 15748 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15749 { 15750 mblk_t *mpdata; 15751 mblk_t *mp_conn_ctl = NULL; 15752 mblk_t *mp_conn_tail; 15753 mblk_t *mp_attr_ctl = NULL; 15754 mblk_t *mp_attr_tail; 15755 mblk_t *mp6_conn_ctl = NULL; 15756 mblk_t *mp6_conn_tail; 15757 mblk_t *mp6_attr_ctl = NULL; 15758 mblk_t *mp6_attr_tail; 15759 struct opthdr *optp; 15760 mib2_tcpConnEntry_t tce; 15761 mib2_tcp6ConnEntry_t tce6; 15762 mib2_transportMLPEntry_t mlp; 15763 connf_t *connfp; 15764 conn_t *connp; 15765 int i; 15766 boolean_t ispriv; 15767 zoneid_t zoneid; 15768 int v4_conn_idx; 15769 int v6_conn_idx; 15770 15771 if (mpctl == NULL || 15772 (mpdata = mpctl->b_cont) == NULL || 15773 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15774 (mp_attr_ctl = copymsg(mpctl)) == NULL || 15775 (mp6_conn_ctl = copymsg(mpctl)) == NULL || 15776 (mp6_attr_ctl = copymsg(mpctl)) == NULL) { 15777 freemsg(mp_conn_ctl); 15778 freemsg(mp_attr_ctl); 15779 freemsg(mp6_conn_ctl); 15780 freemsg(mp6_attr_ctl); 15781 return (0); 15782 } 15783 15784 /* build table of connections -- need count in fixed part */ 15785 SET_MIB(tcp_mib.tcpRtoAlgorithm, 4); /* vanj */ 15786 SET_MIB(tcp_mib.tcpRtoMin, tcp_rexmit_interval_min); 15787 SET_MIB(tcp_mib.tcpRtoMax, tcp_rexmit_interval_max); 15788 SET_MIB(tcp_mib.tcpMaxConn, -1); 15789 SET_MIB(tcp_mib.tcpCurrEstab, 0); 15790 15791 ispriv = 15792 secpolicy_net_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15793 zoneid = Q_TO_CONN(q)->conn_zoneid; 15794 15795 v4_conn_idx = v6_conn_idx = 0; 15796 mp_conn_tail = mp_attr_tail = mp6_conn_tail = mp6_attr_tail = NULL; 15797 15798 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15799 15800 connfp = &ipcl_globalhash_fanout[i]; 15801 15802 connp = NULL; 15803 15804 while ((connp = 15805 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15806 tcp_t *tcp; 15807 boolean_t needattr; 15808 15809 if (connp->conn_zoneid != zoneid) 15810 continue; /* not in this zone */ 15811 15812 tcp = connp->conn_tcp; 15813 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 15814 tcp->tcp_ibsegs = 0; 15815 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 15816 tcp->tcp_obsegs = 0; 15817 15818 tce6.tcp6ConnState = tce.tcpConnState = 15819 tcp_snmp_state(tcp); 15820 if (tce.tcpConnState == MIB2_TCP_established || 15821 tce.tcpConnState == MIB2_TCP_closeWait) 15822 BUMP_MIB(&tcp_mib, tcpCurrEstab); 15823 15824 needattr = B_FALSE; 15825 bzero(&mlp, sizeof (mlp)); 15826 if (connp->conn_mlp_type != mlptSingle) { 15827 if (connp->conn_mlp_type == mlptShared || 15828 connp->conn_mlp_type == mlptBoth) 15829 mlp.tme_flags |= MIB2_TMEF_SHARED; 15830 if (connp->conn_mlp_type == mlptPrivate || 15831 connp->conn_mlp_type == mlptBoth) 15832 mlp.tme_flags |= MIB2_TMEF_PRIVATE; 15833 needattr = B_TRUE; 15834 } 15835 if (connp->conn_peercred != NULL) { 15836 ts_label_t *tsl; 15837 15838 tsl = crgetlabel(connp->conn_peercred); 15839 mlp.tme_doi = label2doi(tsl); 15840 mlp.tme_label = *label2bslabel(tsl); 15841 needattr = B_TRUE; 15842 } 15843 15844 /* Create a message to report on IPv6 entries */ 15845 if (tcp->tcp_ipversion == IPV6_VERSION) { 15846 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 15847 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 15848 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 15849 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 15850 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 15851 /* Don't want just anybody seeing these... */ 15852 if (ispriv) { 15853 tce6.tcp6ConnEntryInfo.ce_snxt = 15854 tcp->tcp_snxt; 15855 tce6.tcp6ConnEntryInfo.ce_suna = 15856 tcp->tcp_suna; 15857 tce6.tcp6ConnEntryInfo.ce_rnxt = 15858 tcp->tcp_rnxt; 15859 tce6.tcp6ConnEntryInfo.ce_rack = 15860 tcp->tcp_rack; 15861 } else { 15862 /* 15863 * Netstat, unfortunately, uses this to 15864 * get send/receive queue sizes. How to fix? 15865 * Why not compute the difference only? 15866 */ 15867 tce6.tcp6ConnEntryInfo.ce_snxt = 15868 tcp->tcp_snxt - tcp->tcp_suna; 15869 tce6.tcp6ConnEntryInfo.ce_suna = 0; 15870 tce6.tcp6ConnEntryInfo.ce_rnxt = 15871 tcp->tcp_rnxt - tcp->tcp_rack; 15872 tce6.tcp6ConnEntryInfo.ce_rack = 0; 15873 } 15874 15875 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15876 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15877 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 15878 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 15879 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 15880 15881 (void) snmp_append_data2(mp6_conn_ctl->b_cont, 15882 &mp6_conn_tail, (char *)&tce6, sizeof (tce6)); 15883 15884 mlp.tme_connidx = v6_conn_idx++; 15885 if (needattr) 15886 (void) snmp_append_data2(mp6_attr_ctl->b_cont, 15887 &mp6_attr_tail, (char *)&mlp, sizeof (mlp)); 15888 } 15889 /* 15890 * Create an IPv4 table entry for IPv4 entries and also 15891 * for IPv6 entries which are bound to in6addr_any 15892 * but don't have IPV6_V6ONLY set. 15893 * (i.e. anything an IPv4 peer could connect to) 15894 */ 15895 if (tcp->tcp_ipversion == IPV4_VERSION || 15896 (tcp->tcp_state <= TCPS_LISTEN && 15897 !tcp->tcp_connp->conn_ipv6_v6only && 15898 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 15899 if (tcp->tcp_ipversion == IPV6_VERSION) { 15900 tce.tcpConnRemAddress = INADDR_ANY; 15901 tce.tcpConnLocalAddress = INADDR_ANY; 15902 } else { 15903 tce.tcpConnRemAddress = 15904 tcp->tcp_remote; 15905 tce.tcpConnLocalAddress = 15906 tcp->tcp_ip_src; 15907 } 15908 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 15909 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 15910 /* Don't want just anybody seeing these... */ 15911 if (ispriv) { 15912 tce.tcpConnEntryInfo.ce_snxt = 15913 tcp->tcp_snxt; 15914 tce.tcpConnEntryInfo.ce_suna = 15915 tcp->tcp_suna; 15916 tce.tcpConnEntryInfo.ce_rnxt = 15917 tcp->tcp_rnxt; 15918 tce.tcpConnEntryInfo.ce_rack = 15919 tcp->tcp_rack; 15920 } else { 15921 /* 15922 * Netstat, unfortunately, uses this to 15923 * get send/receive queue sizes. How 15924 * to fix? 15925 * Why not compute the difference only? 15926 */ 15927 tce.tcpConnEntryInfo.ce_snxt = 15928 tcp->tcp_snxt - tcp->tcp_suna; 15929 tce.tcpConnEntryInfo.ce_suna = 0; 15930 tce.tcpConnEntryInfo.ce_rnxt = 15931 tcp->tcp_rnxt - tcp->tcp_rack; 15932 tce.tcpConnEntryInfo.ce_rack = 0; 15933 } 15934 15935 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15936 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15937 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 15938 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 15939 tce.tcpConnEntryInfo.ce_state = 15940 tcp->tcp_state; 15941 15942 (void) snmp_append_data2(mp_conn_ctl->b_cont, 15943 &mp_conn_tail, (char *)&tce, sizeof (tce)); 15944 15945 mlp.tme_connidx = v4_conn_idx++; 15946 if (needattr) 15947 (void) snmp_append_data2( 15948 mp_attr_ctl->b_cont, 15949 &mp_attr_tail, (char *)&mlp, 15950 sizeof (mlp)); 15951 } 15952 } 15953 } 15954 15955 /* fixed length structure for IPv4 and IPv6 counters */ 15956 SET_MIB(tcp_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 15957 SET_MIB(tcp_mib.tcp6ConnTableSize, sizeof (mib2_tcp6ConnEntry_t)); 15958 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 15959 optp->level = MIB2_TCP; 15960 optp->name = 0; 15961 (void) snmp_append_data(mpdata, (char *)&tcp_mib, sizeof (tcp_mib)); 15962 optp->len = msgdsize(mpdata); 15963 qreply(q, mpctl); 15964 15965 /* table of connections... */ 15966 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 15967 sizeof (struct T_optmgmt_ack)]; 15968 optp->level = MIB2_TCP; 15969 optp->name = MIB2_TCP_CONN; 15970 optp->len = msgdsize(mp_conn_ctl->b_cont); 15971 qreply(q, mp_conn_ctl); 15972 15973 /* table of MLP attributes... */ 15974 optp = (struct opthdr *)&mp_attr_ctl->b_rptr[ 15975 sizeof (struct T_optmgmt_ack)]; 15976 optp->level = MIB2_TCP; 15977 optp->name = EXPER_XPORT_MLP; 15978 optp->len = msgdsize(mp_attr_ctl->b_cont); 15979 if (optp->len == 0) 15980 freemsg(mp_attr_ctl); 15981 else 15982 qreply(q, mp_attr_ctl); 15983 15984 /* table of IPv6 connections... */ 15985 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 15986 sizeof (struct T_optmgmt_ack)]; 15987 optp->level = MIB2_TCP6; 15988 optp->name = MIB2_TCP6_CONN; 15989 optp->len = msgdsize(mp6_conn_ctl->b_cont); 15990 qreply(q, mp6_conn_ctl); 15991 15992 /* table of IPv6 MLP attributes... */ 15993 optp = (struct opthdr *)&mp6_attr_ctl->b_rptr[ 15994 sizeof (struct T_optmgmt_ack)]; 15995 optp->level = MIB2_TCP6; 15996 optp->name = EXPER_XPORT_MLP; 15997 optp->len = msgdsize(mp6_attr_ctl->b_cont); 15998 if (optp->len == 0) 15999 freemsg(mp6_attr_ctl); 16000 else 16001 qreply(q, mp6_attr_ctl); 16002 return (1); 16003 } 16004 16005 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 16006 /* ARGSUSED */ 16007 int 16008 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 16009 { 16010 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 16011 16012 switch (level) { 16013 case MIB2_TCP: 16014 switch (name) { 16015 case 13: 16016 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 16017 return (0); 16018 /* TODO: delete entry defined by tce */ 16019 return (1); 16020 default: 16021 return (0); 16022 } 16023 default: 16024 return (1); 16025 } 16026 } 16027 16028 /* Translate TCP state to MIB2 TCP state. */ 16029 static int 16030 tcp_snmp_state(tcp_t *tcp) 16031 { 16032 if (tcp == NULL) 16033 return (0); 16034 16035 switch (tcp->tcp_state) { 16036 case TCPS_CLOSED: 16037 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 16038 case TCPS_BOUND: 16039 return (MIB2_TCP_closed); 16040 case TCPS_LISTEN: 16041 return (MIB2_TCP_listen); 16042 case TCPS_SYN_SENT: 16043 return (MIB2_TCP_synSent); 16044 case TCPS_SYN_RCVD: 16045 return (MIB2_TCP_synReceived); 16046 case TCPS_ESTABLISHED: 16047 return (MIB2_TCP_established); 16048 case TCPS_CLOSE_WAIT: 16049 return (MIB2_TCP_closeWait); 16050 case TCPS_FIN_WAIT_1: 16051 return (MIB2_TCP_finWait1); 16052 case TCPS_CLOSING: 16053 return (MIB2_TCP_closing); 16054 case TCPS_LAST_ACK: 16055 return (MIB2_TCP_lastAck); 16056 case TCPS_FIN_WAIT_2: 16057 return (MIB2_TCP_finWait2); 16058 case TCPS_TIME_WAIT: 16059 return (MIB2_TCP_timeWait); 16060 default: 16061 return (0); 16062 } 16063 } 16064 16065 static char tcp_report_header[] = 16066 "TCP " MI_COL_HDRPAD_STR 16067 "zone dest snxt suna " 16068 "swnd rnxt rack rwnd rto mss w sw rw t " 16069 "recent [lport,fport] state"; 16070 16071 /* 16072 * TCP status report triggered via the Named Dispatch mechanism. 16073 */ 16074 /* ARGSUSED */ 16075 static void 16076 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 16077 cred_t *cr) 16078 { 16079 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 16080 boolean_t ispriv = secpolicy_net_config(cr, B_TRUE) == 0; 16081 char cflag; 16082 in6_addr_t v6dst; 16083 char buf[80]; 16084 uint_t print_len, buf_len; 16085 16086 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16087 if (buf_len <= 0) 16088 return; 16089 16090 if (hashval >= 0) 16091 (void) sprintf(hash, "%03d ", hashval); 16092 else 16093 hash[0] = '\0'; 16094 16095 /* 16096 * Note that we use the remote address in the tcp_b structure. 16097 * This means that it will print out the real destination address, 16098 * not the next hop's address if source routing is used. This 16099 * avoid the confusion on the output because user may not 16100 * know that source routing is used for a connection. 16101 */ 16102 if (tcp->tcp_ipversion == IPV4_VERSION) { 16103 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 16104 } else { 16105 v6dst = tcp->tcp_remote_v6; 16106 } 16107 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16108 /* 16109 * the ispriv checks are so that normal users cannot determine 16110 * sequence number information using NDD. 16111 */ 16112 16113 if (TCP_IS_DETACHED(tcp)) 16114 cflag = '*'; 16115 else 16116 cflag = ' '; 16117 print_len = snprintf((char *)mp->b_wptr, buf_len, 16118 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 16119 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 16120 hash, 16121 (void *)tcp, 16122 tcp->tcp_connp->conn_zoneid, 16123 addrbuf, 16124 (ispriv) ? tcp->tcp_snxt : 0, 16125 (ispriv) ? tcp->tcp_suna : 0, 16126 tcp->tcp_swnd, 16127 (ispriv) ? tcp->tcp_rnxt : 0, 16128 (ispriv) ? tcp->tcp_rack : 0, 16129 tcp->tcp_rwnd, 16130 tcp->tcp_rto, 16131 tcp->tcp_mss, 16132 tcp->tcp_snd_ws_ok, 16133 tcp->tcp_snd_ws, 16134 tcp->tcp_rcv_ws, 16135 tcp->tcp_snd_ts_ok, 16136 tcp->tcp_ts_recent, 16137 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 16138 if (print_len < buf_len) { 16139 ((mblk_t *)mp)->b_wptr += print_len; 16140 } else { 16141 ((mblk_t *)mp)->b_wptr += buf_len; 16142 } 16143 } 16144 16145 /* 16146 * TCP status report (for listeners only) triggered via the Named Dispatch 16147 * mechanism. 16148 */ 16149 /* ARGSUSED */ 16150 static void 16151 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 16152 { 16153 char addrbuf[INET6_ADDRSTRLEN]; 16154 in6_addr_t v6dst; 16155 uint_t print_len, buf_len; 16156 16157 buf_len = mp->b_datap->db_lim - mp->b_wptr; 16158 if (buf_len <= 0) 16159 return; 16160 16161 if (tcp->tcp_ipversion == IPV4_VERSION) { 16162 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 16163 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 16164 } else { 16165 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 16166 addrbuf, sizeof (addrbuf)); 16167 } 16168 print_len = snprintf((char *)mp->b_wptr, buf_len, 16169 "%03d " 16170 MI_COL_PTRFMT_STR 16171 "%d %s %05u %08u %d/%d/%d%c\n", 16172 hashval, (void *)tcp, 16173 tcp->tcp_connp->conn_zoneid, 16174 addrbuf, 16175 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 16176 tcp->tcp_conn_req_seqnum, 16177 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 16178 tcp->tcp_conn_req_max, 16179 tcp->tcp_syn_defense ? '*' : ' '); 16180 if (print_len < buf_len) { 16181 ((mblk_t *)mp)->b_wptr += print_len; 16182 } else { 16183 ((mblk_t *)mp)->b_wptr += buf_len; 16184 } 16185 } 16186 16187 /* TCP status report triggered via the Named Dispatch mechanism. */ 16188 /* ARGSUSED */ 16189 static int 16190 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16191 { 16192 tcp_t *tcp; 16193 int i; 16194 conn_t *connp; 16195 connf_t *connfp; 16196 zoneid_t zoneid; 16197 16198 /* 16199 * Because of the ndd constraint, at most we can have 64K buffer 16200 * to put in all TCP info. So to be more efficient, just 16201 * allocate a 64K buffer here, assuming we need that large buffer. 16202 * This may be a problem as any user can read tcp_status. Therefore 16203 * we limit the rate of doing this using tcp_ndd_get_info_interval. 16204 * This should be OK as normal users should not do this too often. 16205 */ 16206 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16207 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16208 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16209 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16210 return (0); 16211 } 16212 } 16213 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16214 /* The following may work even if we cannot get a large buf. */ 16215 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16216 return (0); 16217 } 16218 16219 (void) mi_mpprintf(mp, "%s", tcp_report_header); 16220 16221 zoneid = Q_TO_CONN(q)->conn_zoneid; 16222 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16223 16224 connfp = &ipcl_globalhash_fanout[i]; 16225 16226 connp = NULL; 16227 16228 while ((connp = 16229 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16230 tcp = connp->conn_tcp; 16231 if (zoneid != GLOBAL_ZONEID && 16232 zoneid != connp->conn_zoneid) 16233 continue; 16234 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16235 cr); 16236 } 16237 16238 } 16239 16240 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16241 return (0); 16242 } 16243 16244 /* TCP status report triggered via the Named Dispatch mechanism. */ 16245 /* ARGSUSED */ 16246 static int 16247 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16248 { 16249 tf_t *tbf; 16250 tcp_t *tcp; 16251 int i; 16252 zoneid_t zoneid; 16253 16254 /* Refer to comments in tcp_status_report(). */ 16255 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16256 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16257 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16258 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16259 return (0); 16260 } 16261 } 16262 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16263 /* The following may work even if we cannot get a large buf. */ 16264 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16265 return (0); 16266 } 16267 16268 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16269 16270 zoneid = Q_TO_CONN(q)->conn_zoneid; 16271 16272 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 16273 tbf = &tcp_bind_fanout[i]; 16274 mutex_enter(&tbf->tf_lock); 16275 for (tcp = tbf->tf_tcp; tcp != NULL; 16276 tcp = tcp->tcp_bind_hash) { 16277 if (zoneid != GLOBAL_ZONEID && 16278 zoneid != tcp->tcp_connp->conn_zoneid) 16279 continue; 16280 CONN_INC_REF(tcp->tcp_connp); 16281 tcp_report_item(mp->b_cont, tcp, i, 16282 Q_TO_TCP(q), cr); 16283 CONN_DEC_REF(tcp->tcp_connp); 16284 } 16285 mutex_exit(&tbf->tf_lock); 16286 } 16287 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16288 return (0); 16289 } 16290 16291 /* TCP status report triggered via the Named Dispatch mechanism. */ 16292 /* ARGSUSED */ 16293 static int 16294 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16295 { 16296 connf_t *connfp; 16297 conn_t *connp; 16298 tcp_t *tcp; 16299 int i; 16300 zoneid_t zoneid; 16301 16302 /* Refer to comments in tcp_status_report(). */ 16303 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16304 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16305 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16306 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16307 return (0); 16308 } 16309 } 16310 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16311 /* The following may work even if we cannot get a large buf. */ 16312 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16313 return (0); 16314 } 16315 16316 (void) mi_mpprintf(mp, 16317 " TCP " MI_COL_HDRPAD_STR 16318 "zone IP addr port seqnum backlog (q0/q/max)"); 16319 16320 zoneid = Q_TO_CONN(q)->conn_zoneid; 16321 16322 for (i = 0; i < ipcl_bind_fanout_size; i++) { 16323 connfp = &ipcl_bind_fanout[i]; 16324 connp = NULL; 16325 while ((connp = 16326 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16327 tcp = connp->conn_tcp; 16328 if (zoneid != GLOBAL_ZONEID && 16329 zoneid != connp->conn_zoneid) 16330 continue; 16331 tcp_report_listener(mp->b_cont, tcp, i); 16332 } 16333 } 16334 16335 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16336 return (0); 16337 } 16338 16339 /* TCP status report triggered via the Named Dispatch mechanism. */ 16340 /* ARGSUSED */ 16341 static int 16342 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16343 { 16344 connf_t *connfp; 16345 conn_t *connp; 16346 tcp_t *tcp; 16347 int i; 16348 zoneid_t zoneid; 16349 16350 /* Refer to comments in tcp_status_report(). */ 16351 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16352 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16353 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16354 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16355 return (0); 16356 } 16357 } 16358 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16359 /* The following may work even if we cannot get a large buf. */ 16360 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16361 return (0); 16362 } 16363 16364 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16365 ipcl_conn_fanout_size); 16366 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16367 16368 zoneid = Q_TO_CONN(q)->conn_zoneid; 16369 16370 for (i = 0; i < ipcl_conn_fanout_size; i++) { 16371 connfp = &ipcl_conn_fanout[i]; 16372 connp = NULL; 16373 while ((connp = 16374 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16375 tcp = connp->conn_tcp; 16376 if (zoneid != GLOBAL_ZONEID && 16377 zoneid != connp->conn_zoneid) 16378 continue; 16379 tcp_report_item(mp->b_cont, tcp, i, 16380 Q_TO_TCP(q), cr); 16381 } 16382 } 16383 16384 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16385 return (0); 16386 } 16387 16388 /* TCP status report triggered via the Named Dispatch mechanism. */ 16389 /* ARGSUSED */ 16390 static int 16391 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16392 { 16393 tf_t *tf; 16394 tcp_t *tcp; 16395 int i; 16396 zoneid_t zoneid; 16397 16398 /* Refer to comments in tcp_status_report(). */ 16399 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16400 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16401 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16402 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16403 return (0); 16404 } 16405 } 16406 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16407 /* The following may work even if we cannot get a large buf. */ 16408 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16409 return (0); 16410 } 16411 16412 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16413 16414 zoneid = Q_TO_CONN(q)->conn_zoneid; 16415 16416 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 16417 tf = &tcp_acceptor_fanout[i]; 16418 mutex_enter(&tf->tf_lock); 16419 for (tcp = tf->tf_tcp; tcp != NULL; 16420 tcp = tcp->tcp_acceptor_hash) { 16421 if (zoneid != GLOBAL_ZONEID && 16422 zoneid != tcp->tcp_connp->conn_zoneid) 16423 continue; 16424 tcp_report_item(mp->b_cont, tcp, i, 16425 Q_TO_TCP(q), cr); 16426 } 16427 mutex_exit(&tf->tf_lock); 16428 } 16429 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16430 return (0); 16431 } 16432 16433 /* 16434 * tcp_timer is the timer service routine. It handles the retransmission, 16435 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16436 * from the state of the tcp instance what kind of action needs to be done 16437 * at the time it is called. 16438 */ 16439 static void 16440 tcp_timer(void *arg) 16441 { 16442 mblk_t *mp; 16443 clock_t first_threshold; 16444 clock_t second_threshold; 16445 clock_t ms; 16446 uint32_t mss; 16447 conn_t *connp = (conn_t *)arg; 16448 tcp_t *tcp = connp->conn_tcp; 16449 16450 tcp->tcp_timer_tid = 0; 16451 16452 if (tcp->tcp_fused) 16453 return; 16454 16455 first_threshold = tcp->tcp_first_timer_threshold; 16456 second_threshold = tcp->tcp_second_timer_threshold; 16457 switch (tcp->tcp_state) { 16458 case TCPS_IDLE: 16459 case TCPS_BOUND: 16460 case TCPS_LISTEN: 16461 return; 16462 case TCPS_SYN_RCVD: { 16463 tcp_t *listener = tcp->tcp_listener; 16464 16465 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16466 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16467 /* it's our first timeout */ 16468 tcp->tcp_syn_rcvd_timeout = 1; 16469 mutex_enter(&listener->tcp_eager_lock); 16470 listener->tcp_syn_rcvd_timeout++; 16471 if (!listener->tcp_syn_defense && 16472 (listener->tcp_syn_rcvd_timeout > 16473 (tcp_conn_req_max_q0 >> 2)) && 16474 (tcp_conn_req_max_q0 > 200)) { 16475 /* We may be under attack. Put on a defense. */ 16476 listener->tcp_syn_defense = B_TRUE; 16477 cmn_err(CE_WARN, "High TCP connect timeout " 16478 "rate! System (port %d) may be under a " 16479 "SYN flood attack!", 16480 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16481 16482 listener->tcp_ip_addr_cache = kmem_zalloc( 16483 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16484 KM_NOSLEEP); 16485 } 16486 mutex_exit(&listener->tcp_eager_lock); 16487 } 16488 } 16489 /* FALLTHRU */ 16490 case TCPS_SYN_SENT: 16491 first_threshold = tcp->tcp_first_ctimer_threshold; 16492 second_threshold = tcp->tcp_second_ctimer_threshold; 16493 break; 16494 case TCPS_ESTABLISHED: 16495 case TCPS_FIN_WAIT_1: 16496 case TCPS_CLOSING: 16497 case TCPS_CLOSE_WAIT: 16498 case TCPS_LAST_ACK: 16499 /* If we have data to rexmit */ 16500 if (tcp->tcp_suna != tcp->tcp_snxt) { 16501 clock_t time_to_wait; 16502 16503 BUMP_MIB(&tcp_mib, tcpTimRetrans); 16504 if (!tcp->tcp_xmit_head) 16505 break; 16506 time_to_wait = lbolt - 16507 (clock_t)tcp->tcp_xmit_head->b_prev; 16508 time_to_wait = tcp->tcp_rto - 16509 TICK_TO_MSEC(time_to_wait); 16510 /* 16511 * If the timer fires too early, 1 clock tick earlier, 16512 * restart the timer. 16513 */ 16514 if (time_to_wait > msec_per_tick) { 16515 TCP_STAT(tcp_timer_fire_early); 16516 TCP_TIMER_RESTART(tcp, time_to_wait); 16517 return; 16518 } 16519 /* 16520 * When we probe zero windows, we force the swnd open. 16521 * If our peer acks with a closed window swnd will be 16522 * set to zero by tcp_rput(). As long as we are 16523 * receiving acks tcp_rput will 16524 * reset 'tcp_ms_we_have_waited' so as not to trip the 16525 * first and second interval actions. NOTE: the timer 16526 * interval is allowed to continue its exponential 16527 * backoff. 16528 */ 16529 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16530 if (tcp->tcp_debug) { 16531 (void) strlog(TCP_MOD_ID, 0, 1, 16532 SL_TRACE, "tcp_timer: zero win"); 16533 } 16534 } else { 16535 /* 16536 * After retransmission, we need to do 16537 * slow start. Set the ssthresh to one 16538 * half of current effective window and 16539 * cwnd to one MSS. Also reset 16540 * tcp_cwnd_cnt. 16541 * 16542 * Note that if tcp_ssthresh is reduced because 16543 * of ECN, do not reduce it again unless it is 16544 * already one window of data away (tcp_cwr 16545 * should then be cleared) or this is a 16546 * timeout for a retransmitted segment. 16547 */ 16548 uint32_t npkt; 16549 16550 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16551 npkt = ((tcp->tcp_timer_backoff ? 16552 tcp->tcp_cwnd_ssthresh : 16553 tcp->tcp_snxt - 16554 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16555 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16556 tcp->tcp_mss; 16557 } 16558 tcp->tcp_cwnd = tcp->tcp_mss; 16559 tcp->tcp_cwnd_cnt = 0; 16560 if (tcp->tcp_ecn_ok) { 16561 tcp->tcp_cwr = B_TRUE; 16562 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16563 tcp->tcp_ecn_cwr_sent = B_FALSE; 16564 } 16565 } 16566 break; 16567 } 16568 /* 16569 * We have something to send yet we cannot send. The 16570 * reason can be: 16571 * 16572 * 1. Zero send window: we need to do zero window probe. 16573 * 2. Zero cwnd: because of ECN, we need to "clock out 16574 * segments. 16575 * 3. SWS avoidance: receiver may have shrunk window, 16576 * reset our knowledge. 16577 * 16578 * Note that condition 2 can happen with either 1 or 16579 * 3. But 1 and 3 are exclusive. 16580 */ 16581 if (tcp->tcp_unsent != 0) { 16582 if (tcp->tcp_cwnd == 0) { 16583 /* 16584 * Set tcp_cwnd to 1 MSS so that a 16585 * new segment can be sent out. We 16586 * are "clocking out" new data when 16587 * the network is really congested. 16588 */ 16589 ASSERT(tcp->tcp_ecn_ok); 16590 tcp->tcp_cwnd = tcp->tcp_mss; 16591 } 16592 if (tcp->tcp_swnd == 0) { 16593 /* Extend window for zero window probe */ 16594 tcp->tcp_swnd++; 16595 tcp->tcp_zero_win_probe = B_TRUE; 16596 BUMP_MIB(&tcp_mib, tcpOutWinProbe); 16597 } else { 16598 /* 16599 * Handle timeout from sender SWS avoidance. 16600 * Reset our knowledge of the max send window 16601 * since the receiver might have reduced its 16602 * receive buffer. Avoid setting tcp_max_swnd 16603 * to one since that will essentially disable 16604 * the SWS checks. 16605 * 16606 * Note that since we don't have a SWS 16607 * state variable, if the timeout is set 16608 * for ECN but not for SWS, this 16609 * code will also be executed. This is 16610 * fine as tcp_max_swnd is updated 16611 * constantly and it will not affect 16612 * anything. 16613 */ 16614 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16615 } 16616 tcp_wput_data(tcp, NULL, B_FALSE); 16617 return; 16618 } 16619 /* Is there a FIN that needs to be to re retransmitted? */ 16620 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16621 !tcp->tcp_fin_acked) 16622 break; 16623 /* Nothing to do, return without restarting timer. */ 16624 TCP_STAT(tcp_timer_fire_miss); 16625 return; 16626 case TCPS_FIN_WAIT_2: 16627 /* 16628 * User closed the TCP endpoint and peer ACK'ed our FIN. 16629 * We waited some time for for peer's FIN, but it hasn't 16630 * arrived. We flush the connection now to avoid 16631 * case where the peer has rebooted. 16632 */ 16633 if (TCP_IS_DETACHED(tcp)) { 16634 (void) tcp_clean_death(tcp, 0, 23); 16635 } else { 16636 TCP_TIMER_RESTART(tcp, tcp_fin_wait_2_flush_interval); 16637 } 16638 return; 16639 case TCPS_TIME_WAIT: 16640 (void) tcp_clean_death(tcp, 0, 24); 16641 return; 16642 default: 16643 if (tcp->tcp_debug) { 16644 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16645 "tcp_timer: strange state (%d) %s", 16646 tcp->tcp_state, tcp_display(tcp, NULL, 16647 DISP_PORT_ONLY)); 16648 } 16649 return; 16650 } 16651 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16652 /* 16653 * For zero window probe, we need to send indefinitely, 16654 * unless we have not heard from the other side for some 16655 * time... 16656 */ 16657 if ((tcp->tcp_zero_win_probe == 0) || 16658 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16659 second_threshold)) { 16660 BUMP_MIB(&tcp_mib, tcpTimRetransDrop); 16661 /* 16662 * If TCP is in SYN_RCVD state, send back a 16663 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16664 * should be zero in TCPS_SYN_RCVD state. 16665 */ 16666 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16667 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16668 "in SYN_RCVD", 16669 tcp, tcp->tcp_snxt, 16670 tcp->tcp_rnxt, TH_RST | TH_ACK); 16671 } 16672 (void) tcp_clean_death(tcp, 16673 tcp->tcp_client_errno ? 16674 tcp->tcp_client_errno : ETIMEDOUT, 25); 16675 return; 16676 } else { 16677 /* 16678 * Set tcp_ms_we_have_waited to second_threshold 16679 * so that in next timeout, we will do the above 16680 * check (lbolt - tcp_last_recv_time). This is 16681 * also to avoid overflow. 16682 * 16683 * We don't need to decrement tcp_timer_backoff 16684 * to avoid overflow because it will be decremented 16685 * later if new timeout value is greater than 16686 * tcp_rexmit_interval_max. In the case when 16687 * tcp_rexmit_interval_max is greater than 16688 * second_threshold, it means that we will wait 16689 * longer than second_threshold to send the next 16690 * window probe. 16691 */ 16692 tcp->tcp_ms_we_have_waited = second_threshold; 16693 } 16694 } else if (ms > first_threshold) { 16695 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16696 tcp->tcp_xmit_head != NULL) { 16697 tcp->tcp_xmit_head = 16698 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16699 } 16700 /* 16701 * We have been retransmitting for too long... The RTT 16702 * we calculated is probably incorrect. Reinitialize it. 16703 * Need to compensate for 0 tcp_rtt_sa. Reset 16704 * tcp_rtt_update so that we won't accidentally cache a 16705 * bad value. But only do this if this is not a zero 16706 * window probe. 16707 */ 16708 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16709 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16710 (tcp->tcp_rtt_sa >> 5); 16711 tcp->tcp_rtt_sa = 0; 16712 tcp_ip_notify(tcp); 16713 tcp->tcp_rtt_update = 0; 16714 } 16715 } 16716 tcp->tcp_timer_backoff++; 16717 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16718 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16719 tcp_rexmit_interval_min) { 16720 /* 16721 * This means the original RTO is tcp_rexmit_interval_min. 16722 * So we will use tcp_rexmit_interval_min as the RTO value 16723 * and do the backoff. 16724 */ 16725 ms = tcp_rexmit_interval_min << tcp->tcp_timer_backoff; 16726 } else { 16727 ms <<= tcp->tcp_timer_backoff; 16728 } 16729 if (ms > tcp_rexmit_interval_max) { 16730 ms = tcp_rexmit_interval_max; 16731 /* 16732 * ms is at max, decrement tcp_timer_backoff to avoid 16733 * overflow. 16734 */ 16735 tcp->tcp_timer_backoff--; 16736 } 16737 tcp->tcp_ms_we_have_waited += ms; 16738 if (tcp->tcp_zero_win_probe == 0) { 16739 tcp->tcp_rto = ms; 16740 } 16741 TCP_TIMER_RESTART(tcp, ms); 16742 /* 16743 * This is after a timeout and tcp_rto is backed off. Set 16744 * tcp_set_timer to 1 so that next time RTO is updated, we will 16745 * restart the timer with a correct value. 16746 */ 16747 tcp->tcp_set_timer = 1; 16748 mss = tcp->tcp_snxt - tcp->tcp_suna; 16749 if (mss > tcp->tcp_mss) 16750 mss = tcp->tcp_mss; 16751 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16752 mss = tcp->tcp_swnd; 16753 16754 if ((mp = tcp->tcp_xmit_head) != NULL) 16755 mp->b_prev = (mblk_t *)lbolt; 16756 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16757 B_TRUE); 16758 16759 /* 16760 * When slow start after retransmission begins, start with 16761 * this seq no. tcp_rexmit_max marks the end of special slow 16762 * start phase. tcp_snd_burst controls how many segments 16763 * can be sent because of an ack. 16764 */ 16765 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16766 tcp->tcp_snd_burst = TCP_CWND_SS; 16767 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16768 (tcp->tcp_unsent == 0)) { 16769 tcp->tcp_rexmit_max = tcp->tcp_fss; 16770 } else { 16771 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16772 } 16773 tcp->tcp_rexmit = B_TRUE; 16774 tcp->tcp_dupack_cnt = 0; 16775 16776 /* 16777 * Remove all rexmit SACK blk to start from fresh. 16778 */ 16779 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 16780 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 16781 tcp->tcp_num_notsack_blk = 0; 16782 tcp->tcp_cnt_notsack_list = 0; 16783 } 16784 if (mp == NULL) { 16785 return; 16786 } 16787 /* Attach credentials to retransmitted initial SYNs. */ 16788 if (tcp->tcp_state == TCPS_SYN_SENT) { 16789 mblk_setcred(mp, tcp->tcp_cred); 16790 DB_CPID(mp) = tcp->tcp_cpid; 16791 } 16792 16793 tcp->tcp_csuna = tcp->tcp_snxt; 16794 BUMP_MIB(&tcp_mib, tcpRetransSegs); 16795 UPDATE_MIB(&tcp_mib, tcpRetransBytes, mss); 16796 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 16797 tcp_send_data(tcp, tcp->tcp_wq, mp); 16798 16799 } 16800 16801 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16802 static void 16803 tcp_unbind(tcp_t *tcp, mblk_t *mp) 16804 { 16805 conn_t *connp; 16806 16807 switch (tcp->tcp_state) { 16808 case TCPS_BOUND: 16809 case TCPS_LISTEN: 16810 break; 16811 default: 16812 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 16813 return; 16814 } 16815 16816 /* 16817 * Need to clean up all the eagers since after the unbind, segments 16818 * will no longer be delivered to this listener stream. 16819 */ 16820 mutex_enter(&tcp->tcp_eager_lock); 16821 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16822 tcp_eager_cleanup(tcp, 0); 16823 } 16824 mutex_exit(&tcp->tcp_eager_lock); 16825 16826 if (tcp->tcp_ipversion == IPV4_VERSION) { 16827 tcp->tcp_ipha->ipha_src = 0; 16828 } else { 16829 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16830 } 16831 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16832 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16833 tcp_bind_hash_remove(tcp); 16834 tcp->tcp_state = TCPS_IDLE; 16835 tcp->tcp_mdt = B_FALSE; 16836 /* Send M_FLUSH according to TPI */ 16837 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16838 connp = tcp->tcp_connp; 16839 connp->conn_mdt_ok = B_FALSE; 16840 ipcl_hash_remove(connp); 16841 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16842 mp = mi_tpi_ok_ack_alloc(mp); 16843 putnext(tcp->tcp_rq, mp); 16844 } 16845 16846 /* 16847 * Don't let port fall into the privileged range. 16848 * Since the extra privileged ports can be arbitrary we also 16849 * ensure that we exclude those from consideration. 16850 * tcp_g_epriv_ports is not sorted thus we loop over it until 16851 * there are no changes. 16852 * 16853 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16854 * but instead the code relies on: 16855 * - the fact that the address of the array and its size never changes 16856 * - the atomic assignment of the elements of the array 16857 * 16858 * Returns 0 if there are no more ports available. 16859 * 16860 * TS note: skip multilevel ports. 16861 */ 16862 static in_port_t 16863 tcp_update_next_port(in_port_t port, const tcp_t *tcp, boolean_t random) 16864 { 16865 int i; 16866 boolean_t restart = B_FALSE; 16867 16868 if (random && tcp_random_anon_port != 0) { 16869 (void) random_get_pseudo_bytes((uint8_t *)&port, 16870 sizeof (in_port_t)); 16871 /* 16872 * Unless changed by a sys admin, the smallest anon port 16873 * is 32768 and the largest anon port is 65535. It is 16874 * very likely (50%) for the random port to be smaller 16875 * than the smallest anon port. When that happens, 16876 * add port % (anon port range) to the smallest anon 16877 * port to get the random port. It should fall into the 16878 * valid anon port range. 16879 */ 16880 if (port < tcp_smallest_anon_port) { 16881 port = tcp_smallest_anon_port + 16882 port % (tcp_largest_anon_port - 16883 tcp_smallest_anon_port); 16884 } 16885 } 16886 16887 retry: 16888 if (port < tcp_smallest_anon_port) 16889 port = (in_port_t)tcp_smallest_anon_port; 16890 16891 if (port > tcp_largest_anon_port) { 16892 if (restart) 16893 return (0); 16894 restart = B_TRUE; 16895 port = (in_port_t)tcp_smallest_anon_port; 16896 } 16897 16898 if (port < tcp_smallest_nonpriv_port) 16899 port = (in_port_t)tcp_smallest_nonpriv_port; 16900 16901 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 16902 if (port == tcp_g_epriv_ports[i]) { 16903 port++; 16904 /* 16905 * Make sure whether the port is in the 16906 * valid range. 16907 */ 16908 goto retry; 16909 } 16910 } 16911 if (is_system_labeled() && 16912 (i = tsol_next_port(crgetzone(tcp->tcp_cred), port, 16913 IPPROTO_TCP, B_TRUE)) != 0) { 16914 port = i; 16915 goto retry; 16916 } 16917 return (port); 16918 } 16919 16920 /* 16921 * Return the next anonymous port in the privileged port range for 16922 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16923 * downwards. This is the same behavior as documented in the userland 16924 * library call rresvport(3N). 16925 * 16926 * TS note: skip multilevel ports. 16927 */ 16928 static in_port_t 16929 tcp_get_next_priv_port(const tcp_t *tcp) 16930 { 16931 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16932 in_port_t nextport; 16933 boolean_t restart = B_FALSE; 16934 16935 retry: 16936 if (next_priv_port < tcp_min_anonpriv_port || 16937 next_priv_port >= IPPORT_RESERVED) { 16938 next_priv_port = IPPORT_RESERVED - 1; 16939 if (restart) 16940 return (0); 16941 restart = B_TRUE; 16942 } 16943 if (is_system_labeled() && 16944 (nextport = tsol_next_port(crgetzone(tcp->tcp_cred), 16945 next_priv_port, IPPROTO_TCP, B_FALSE)) != 0) { 16946 next_priv_port = nextport; 16947 goto retry; 16948 } 16949 return (next_priv_port--); 16950 } 16951 16952 /* The write side r/w procedure. */ 16953 16954 #if CCS_STATS 16955 struct { 16956 struct { 16957 int64_t count, bytes; 16958 } tot, hit; 16959 } wrw_stats; 16960 #endif 16961 16962 /* 16963 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 16964 * messages. 16965 */ 16966 /* ARGSUSED */ 16967 static void 16968 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 16969 { 16970 conn_t *connp = (conn_t *)arg; 16971 tcp_t *tcp = connp->conn_tcp; 16972 queue_t *q = tcp->tcp_wq; 16973 16974 ASSERT(DB_TYPE(mp) != M_IOCTL); 16975 /* 16976 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 16977 * Once the close starts, streamhead and sockfs will not let any data 16978 * packets come down (close ensures that there are no threads using the 16979 * queue and no new threads will come down) but since qprocsoff() 16980 * hasn't happened yet, a M_FLUSH or some non data message might 16981 * get reflected back (in response to our own FLUSHRW) and get 16982 * processed after tcp_close() is done. The conn would still be valid 16983 * because a ref would have added but we need to check the state 16984 * before actually processing the packet. 16985 */ 16986 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 16987 freemsg(mp); 16988 return; 16989 } 16990 16991 switch (DB_TYPE(mp)) { 16992 case M_IOCDATA: 16993 tcp_wput_iocdata(tcp, mp); 16994 break; 16995 case M_FLUSH: 16996 tcp_wput_flush(tcp, mp); 16997 break; 16998 default: 16999 CALL_IP_WPUT(connp, q, mp); 17000 break; 17001 } 17002 } 17003 17004 /* 17005 * The TCP fast path write put procedure. 17006 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 17007 */ 17008 /* ARGSUSED */ 17009 void 17010 tcp_output(void *arg, mblk_t *mp, void *arg2) 17011 { 17012 int len; 17013 int hdrlen; 17014 int plen; 17015 mblk_t *mp1; 17016 uchar_t *rptr; 17017 uint32_t snxt; 17018 tcph_t *tcph; 17019 struct datab *db; 17020 uint32_t suna; 17021 uint32_t mss; 17022 ipaddr_t *dst; 17023 ipaddr_t *src; 17024 uint32_t sum; 17025 int usable; 17026 conn_t *connp = (conn_t *)arg; 17027 tcp_t *tcp = connp->conn_tcp; 17028 uint32_t msize; 17029 17030 /* 17031 * Try and ASSERT the minimum possible references on the 17032 * conn early enough. Since we are executing on write side, 17033 * the connection is obviously not detached and that means 17034 * there is a ref each for TCP and IP. Since we are behind 17035 * the squeue, the minimum references needed are 3. If the 17036 * conn is in classifier hash list, there should be an 17037 * extra ref for that (we check both the possibilities). 17038 */ 17039 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17040 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17041 17042 ASSERT(DB_TYPE(mp) == M_DATA); 17043 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 17044 17045 mutex_enter(&connp->conn_lock); 17046 tcp->tcp_squeue_bytes -= msize; 17047 mutex_exit(&connp->conn_lock); 17048 17049 /* Bypass tcp protocol for fused tcp loopback */ 17050 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 17051 return; 17052 17053 mss = tcp->tcp_mss; 17054 if (tcp->tcp_xmit_zc_clean) 17055 mp = tcp_zcopy_backoff(tcp, mp, 0); 17056 17057 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17058 len = (int)(mp->b_wptr - mp->b_rptr); 17059 17060 /* 17061 * Criteria for fast path: 17062 * 17063 * 1. no unsent data 17064 * 2. single mblk in request 17065 * 3. connection established 17066 * 4. data in mblk 17067 * 5. len <= mss 17068 * 6. no tcp_valid bits 17069 */ 17070 if ((tcp->tcp_unsent != 0) || 17071 (tcp->tcp_cork) || 17072 (mp->b_cont != NULL) || 17073 (tcp->tcp_state != TCPS_ESTABLISHED) || 17074 (len == 0) || 17075 (len > mss) || 17076 (tcp->tcp_valid_bits != 0)) { 17077 tcp_wput_data(tcp, mp, B_FALSE); 17078 return; 17079 } 17080 17081 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 17082 ASSERT(tcp->tcp_fin_sent == 0); 17083 17084 /* queue new packet onto retransmission queue */ 17085 if (tcp->tcp_xmit_head == NULL) { 17086 tcp->tcp_xmit_head = mp; 17087 } else { 17088 tcp->tcp_xmit_last->b_cont = mp; 17089 } 17090 tcp->tcp_xmit_last = mp; 17091 tcp->tcp_xmit_tail = mp; 17092 17093 /* find out how much we can send */ 17094 /* BEGIN CSTYLED */ 17095 /* 17096 * un-acked usable 17097 * |--------------|-----------------| 17098 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 17099 */ 17100 /* END CSTYLED */ 17101 17102 /* start sending from tcp_snxt */ 17103 snxt = tcp->tcp_snxt; 17104 17105 /* 17106 * Check to see if this connection has been idled for some 17107 * time and no ACK is expected. If it is, we need to slow 17108 * start again to get back the connection's "self-clock" as 17109 * described in VJ's paper. 17110 * 17111 * Refer to the comment in tcp_mss_set() for the calculation 17112 * of tcp_cwnd after idle. 17113 */ 17114 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 17115 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 17116 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 17117 } 17118 17119 usable = tcp->tcp_swnd; /* tcp window size */ 17120 if (usable > tcp->tcp_cwnd) 17121 usable = tcp->tcp_cwnd; /* congestion window smaller */ 17122 usable -= snxt; /* subtract stuff already sent */ 17123 suna = tcp->tcp_suna; 17124 usable += suna; 17125 /* usable can be < 0 if the congestion window is smaller */ 17126 if (len > usable) { 17127 /* Can't send complete M_DATA in one shot */ 17128 goto slow; 17129 } 17130 17131 if (tcp->tcp_flow_stopped && 17132 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 17133 tcp_clrqfull(tcp); 17134 } 17135 17136 /* 17137 * determine if anything to send (Nagle). 17138 * 17139 * 1. len < tcp_mss (i.e. small) 17140 * 2. unacknowledged data present 17141 * 3. len < nagle limit 17142 * 4. last packet sent < nagle limit (previous packet sent) 17143 */ 17144 if ((len < mss) && (snxt != suna) && 17145 (len < (int)tcp->tcp_naglim) && 17146 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 17147 /* 17148 * This was the first unsent packet and normally 17149 * mss < xmit_hiwater so there is no need to worry 17150 * about flow control. The next packet will go 17151 * through the flow control check in tcp_wput_data(). 17152 */ 17153 /* leftover work from above */ 17154 tcp->tcp_unsent = len; 17155 tcp->tcp_xmit_tail_unsent = len; 17156 17157 return; 17158 } 17159 17160 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 17161 17162 if (snxt == suna) { 17163 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 17164 } 17165 17166 /* we have always sent something */ 17167 tcp->tcp_rack_cnt = 0; 17168 17169 tcp->tcp_snxt = snxt + len; 17170 tcp->tcp_rack = tcp->tcp_rnxt; 17171 17172 if ((mp1 = dupb(mp)) == 0) 17173 goto no_memory; 17174 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 17175 mp->b_next = (mblk_t *)(uintptr_t)snxt; 17176 17177 /* adjust tcp header information */ 17178 tcph = tcp->tcp_tcph; 17179 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 17180 17181 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 17182 sum = (sum >> 16) + (sum & 0xFFFF); 17183 U16_TO_ABE16(sum, tcph->th_sum); 17184 17185 U32_TO_ABE32(snxt, tcph->th_seq); 17186 17187 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 17188 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 17189 BUMP_LOCAL(tcp->tcp_obsegs); 17190 17191 /* Update the latest receive window size in TCP header. */ 17192 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 17193 tcph->th_win); 17194 17195 tcp->tcp_last_sent_len = (ushort_t)len; 17196 17197 plen = len + tcp->tcp_hdr_len; 17198 17199 if (tcp->tcp_ipversion == IPV4_VERSION) { 17200 tcp->tcp_ipha->ipha_length = htons(plen); 17201 } else { 17202 tcp->tcp_ip6h->ip6_plen = htons(plen - 17203 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 17204 } 17205 17206 /* see if we need to allocate a mblk for the headers */ 17207 hdrlen = tcp->tcp_hdr_len; 17208 rptr = mp1->b_rptr - hdrlen; 17209 db = mp1->b_datap; 17210 if ((db->db_ref != 2) || rptr < db->db_base || 17211 (!OK_32PTR(rptr))) { 17212 /* NOTE: we assume allocb returns an OK_32PTR */ 17213 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 17214 tcp_wroff_xtra, BPRI_MED); 17215 if (!mp) { 17216 freemsg(mp1); 17217 goto no_memory; 17218 } 17219 mp->b_cont = mp1; 17220 mp1 = mp; 17221 /* Leave room for Link Level header */ 17222 /* hdrlen = tcp->tcp_hdr_len; */ 17223 rptr = &mp1->b_rptr[tcp_wroff_xtra]; 17224 mp1->b_wptr = &rptr[hdrlen]; 17225 } 17226 mp1->b_rptr = rptr; 17227 17228 /* Fill in the timestamp option. */ 17229 if (tcp->tcp_snd_ts_ok) { 17230 U32_TO_BE32((uint32_t)lbolt, 17231 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 17232 U32_TO_BE32(tcp->tcp_ts_recent, 17233 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 17234 } else { 17235 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 17236 } 17237 17238 /* copy header into outgoing packet */ 17239 dst = (ipaddr_t *)rptr; 17240 src = (ipaddr_t *)tcp->tcp_iphc; 17241 dst[0] = src[0]; 17242 dst[1] = src[1]; 17243 dst[2] = src[2]; 17244 dst[3] = src[3]; 17245 dst[4] = src[4]; 17246 dst[5] = src[5]; 17247 dst[6] = src[6]; 17248 dst[7] = src[7]; 17249 dst[8] = src[8]; 17250 dst[9] = src[9]; 17251 if (hdrlen -= 40) { 17252 hdrlen >>= 2; 17253 dst += 10; 17254 src += 10; 17255 do { 17256 *dst++ = *src++; 17257 } while (--hdrlen); 17258 } 17259 17260 /* 17261 * Set the ECN info in the TCP header. Note that this 17262 * is not the template header. 17263 */ 17264 if (tcp->tcp_ecn_ok) { 17265 SET_ECT(tcp, rptr); 17266 17267 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17268 if (tcp->tcp_ecn_echo_on) 17269 tcph->th_flags[0] |= TH_ECE; 17270 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17271 tcph->th_flags[0] |= TH_CWR; 17272 tcp->tcp_ecn_cwr_sent = B_TRUE; 17273 } 17274 } 17275 17276 if (tcp->tcp_ip_forward_progress) { 17277 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17278 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17279 tcp->tcp_ip_forward_progress = B_FALSE; 17280 } 17281 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17282 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17283 return; 17284 17285 /* 17286 * If we ran out of memory, we pretend to have sent the packet 17287 * and that it was lost on the wire. 17288 */ 17289 no_memory: 17290 return; 17291 17292 slow: 17293 /* leftover work from above */ 17294 tcp->tcp_unsent = len; 17295 tcp->tcp_xmit_tail_unsent = len; 17296 tcp_wput_data(tcp, NULL, B_FALSE); 17297 } 17298 17299 /* 17300 * The function called through squeue to get behind eager's perimeter to 17301 * finish the accept processing. 17302 */ 17303 /* ARGSUSED */ 17304 void 17305 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17306 { 17307 conn_t *connp = (conn_t *)arg; 17308 tcp_t *tcp = connp->conn_tcp; 17309 queue_t *q = tcp->tcp_rq; 17310 mblk_t *mp1; 17311 mblk_t *stropt_mp = mp; 17312 struct stroptions *stropt; 17313 uint_t thwin; 17314 17315 /* 17316 * Drop the eager's ref on the listener, that was placed when 17317 * this eager began life in tcp_conn_request. 17318 */ 17319 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17320 17321 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17322 /* 17323 * Someone blewoff the eager before we could finish 17324 * the accept. 17325 * 17326 * The only reason eager exists it because we put in 17327 * a ref on it when conn ind went up. We need to send 17328 * a disconnect indication up while the last reference 17329 * on the eager will be dropped by the squeue when we 17330 * return. 17331 */ 17332 ASSERT(tcp->tcp_listener == NULL); 17333 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17334 struct T_discon_ind *tdi; 17335 17336 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17337 /* 17338 * Let us reuse the incoming mblk to avoid memory 17339 * allocation failure problems. We know that the 17340 * size of the incoming mblk i.e. stroptions is greater 17341 * than sizeof T_discon_ind. So the reallocb below 17342 * can't fail. 17343 */ 17344 freemsg(mp->b_cont); 17345 mp->b_cont = NULL; 17346 ASSERT(DB_REF(mp) == 1); 17347 mp = reallocb(mp, sizeof (struct T_discon_ind), 17348 B_FALSE); 17349 ASSERT(mp != NULL); 17350 DB_TYPE(mp) = M_PROTO; 17351 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17352 tdi = (struct T_discon_ind *)mp->b_rptr; 17353 if (tcp->tcp_issocket) { 17354 tdi->DISCON_reason = ECONNREFUSED; 17355 tdi->SEQ_number = 0; 17356 } else { 17357 tdi->DISCON_reason = ENOPROTOOPT; 17358 tdi->SEQ_number = 17359 tcp->tcp_conn_req_seqnum; 17360 } 17361 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17362 putnext(q, mp); 17363 } else { 17364 freemsg(mp); 17365 } 17366 if (tcp->tcp_hard_binding) { 17367 tcp->tcp_hard_binding = B_FALSE; 17368 tcp->tcp_hard_bound = B_TRUE; 17369 } 17370 tcp->tcp_detached = B_FALSE; 17371 return; 17372 } 17373 17374 mp1 = stropt_mp->b_cont; 17375 stropt_mp->b_cont = NULL; 17376 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17377 stropt = (struct stroptions *)stropt_mp->b_rptr; 17378 17379 while (mp1 != NULL) { 17380 mp = mp1; 17381 mp1 = mp1->b_cont; 17382 mp->b_cont = NULL; 17383 tcp->tcp_drop_opt_ack_cnt++; 17384 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17385 } 17386 mp = NULL; 17387 17388 /* 17389 * For a loopback connection with tcp_direct_sockfs on, note that 17390 * we don't have to protect tcp_rcv_list yet because synchronous 17391 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17392 * possibly race with us. 17393 */ 17394 17395 /* 17396 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17397 * properly. This is the first time we know of the acceptor' 17398 * queue. So we do it here. 17399 */ 17400 if (tcp->tcp_rcv_list == NULL) { 17401 /* 17402 * Recv queue is empty, tcp_rwnd should not have changed. 17403 * That means it should be equal to the listener's tcp_rwnd. 17404 */ 17405 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17406 } else { 17407 #ifdef DEBUG 17408 uint_t cnt = 0; 17409 17410 mp1 = tcp->tcp_rcv_list; 17411 while ((mp = mp1) != NULL) { 17412 mp1 = mp->b_next; 17413 cnt += msgdsize(mp); 17414 } 17415 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17416 #endif 17417 /* There is some data, add them back to get the max. */ 17418 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17419 } 17420 17421 stropt->so_flags = SO_HIWAT; 17422 stropt->so_hiwat = MAX(q->q_hiwat, tcp_sth_rcv_hiwat); 17423 17424 stropt->so_flags |= SO_MAXBLK; 17425 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17426 17427 /* 17428 * This is the first time we run on the correct 17429 * queue after tcp_accept. So fix all the q parameters 17430 * here. 17431 */ 17432 /* Allocate room for SACK options if needed. */ 17433 stropt->so_flags |= SO_WROFF; 17434 if (tcp->tcp_fused) { 17435 ASSERT(tcp->tcp_loopback); 17436 ASSERT(tcp->tcp_loopback_peer != NULL); 17437 /* 17438 * For fused tcp loopback, set the stream head's write 17439 * offset value to zero since we won't be needing any room 17440 * for TCP/IP headers. This would also improve performance 17441 * since it would reduce the amount of work done by kmem. 17442 * Non-fused tcp loopback case is handled separately below. 17443 */ 17444 stropt->so_wroff = 0; 17445 /* 17446 * Record the stream head's high water mark for this endpoint; 17447 * this is used for flow-control purposes in tcp_fuse_output(). 17448 */ 17449 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17450 /* 17451 * Update the peer's transmit parameters according to 17452 * our recently calculated high water mark value. 17453 */ 17454 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17455 } else if (tcp->tcp_snd_sack_ok) { 17456 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17457 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra); 17458 } else { 17459 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17460 tcp_wroff_xtra); 17461 } 17462 17463 /* 17464 * If this is endpoint is handling SSL, then reserve extra 17465 * offset and space at the end. 17466 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17467 * overriding the previous setting. The extra cost of signing and 17468 * encrypting multiple MSS-size records (12 of them with Ethernet), 17469 * instead of a single contiguous one by the stream head 17470 * largely outweighs the statistical reduction of ACKs, when 17471 * applicable. The peer will also save on decyption and verification 17472 * costs. 17473 */ 17474 if (tcp->tcp_kssl_ctx != NULL) { 17475 stropt->so_wroff += SSL3_WROFFSET; 17476 17477 stropt->so_flags |= SO_TAIL; 17478 stropt->so_tail = SSL3_MAX_TAIL_LEN; 17479 17480 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 17481 } 17482 17483 /* Send the options up */ 17484 putnext(q, stropt_mp); 17485 17486 /* 17487 * Pass up any data and/or a fin that has been received. 17488 * 17489 * Adjust receive window in case it had decreased 17490 * (because there is data <=> tcp_rcv_list != NULL) 17491 * while the connection was detached. Note that 17492 * in case the eager was flow-controlled, w/o this 17493 * code, the rwnd may never open up again! 17494 */ 17495 if (tcp->tcp_rcv_list != NULL) { 17496 /* We drain directly in case of fused tcp loopback */ 17497 if (!tcp->tcp_fused && canputnext(q)) { 17498 tcp->tcp_rwnd = q->q_hiwat; 17499 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 17500 << tcp->tcp_rcv_ws; 17501 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 17502 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17503 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 17504 tcp_xmit_ctl(NULL, 17505 tcp, (tcp->tcp_swnd == 0) ? 17506 tcp->tcp_suna : tcp->tcp_snxt, 17507 tcp->tcp_rnxt, TH_ACK); 17508 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 17509 } 17510 17511 } 17512 (void) tcp_rcv_drain(q, tcp); 17513 17514 /* 17515 * For fused tcp loopback, back-enable peer endpoint 17516 * if it's currently flow-controlled. 17517 */ 17518 if (tcp->tcp_fused && 17519 tcp->tcp_loopback_peer->tcp_flow_stopped) { 17520 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17521 17522 ASSERT(peer_tcp != NULL); 17523 ASSERT(peer_tcp->tcp_fused); 17524 17525 tcp_clrqfull(peer_tcp); 17526 TCP_STAT(tcp_fusion_backenabled); 17527 } 17528 } 17529 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17530 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17531 mp = mi_tpi_ordrel_ind(); 17532 if (mp) { 17533 tcp->tcp_ordrel_done = B_TRUE; 17534 putnext(q, mp); 17535 if (tcp->tcp_deferred_clean_death) { 17536 /* 17537 * tcp_clean_death was deferred 17538 * for T_ORDREL_IND - do it now 17539 */ 17540 (void) tcp_clean_death(tcp, 17541 tcp->tcp_client_errno, 21); 17542 tcp->tcp_deferred_clean_death = B_FALSE; 17543 } 17544 } else { 17545 /* 17546 * Run the orderly release in the 17547 * service routine. 17548 */ 17549 qenable(q); 17550 } 17551 } 17552 if (tcp->tcp_hard_binding) { 17553 tcp->tcp_hard_binding = B_FALSE; 17554 tcp->tcp_hard_bound = B_TRUE; 17555 } 17556 17557 tcp->tcp_detached = B_FALSE; 17558 17559 /* We can enable synchronous streams now */ 17560 if (tcp->tcp_fused) { 17561 tcp_fuse_syncstr_enable_pair(tcp); 17562 } 17563 17564 if (tcp->tcp_ka_enabled) { 17565 tcp->tcp_ka_last_intrvl = 0; 17566 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17567 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17568 } 17569 17570 /* 17571 * At this point, eager is fully established and will 17572 * have the following references - 17573 * 17574 * 2 references for connection to exist (1 for TCP and 1 for IP). 17575 * 1 reference for the squeue which will be dropped by the squeue as 17576 * soon as this function returns. 17577 * There will be 1 additonal reference for being in classifier 17578 * hash list provided something bad hasn't happened. 17579 */ 17580 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17581 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17582 } 17583 17584 /* 17585 * The function called through squeue to get behind listener's perimeter to 17586 * send a deffered conn_ind. 17587 */ 17588 /* ARGSUSED */ 17589 void 17590 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17591 { 17592 conn_t *connp = (conn_t *)arg; 17593 tcp_t *listener = connp->conn_tcp; 17594 17595 if (listener->tcp_state == TCPS_CLOSED || 17596 TCP_IS_DETACHED(listener)) { 17597 /* 17598 * If listener has closed, it would have caused a 17599 * a cleanup/blowoff to happen for the eager. 17600 */ 17601 tcp_t *tcp; 17602 struct T_conn_ind *conn_ind; 17603 17604 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17605 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17606 conn_ind->OPT_length); 17607 /* 17608 * We need to drop the ref on eager that was put 17609 * tcp_rput_data() before trying to send the conn_ind 17610 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17611 * and tcp_wput_accept() is sending this deferred conn_ind but 17612 * listener is closed so we drop the ref. 17613 */ 17614 CONN_DEC_REF(tcp->tcp_connp); 17615 freemsg(mp); 17616 return; 17617 } 17618 putnext(listener->tcp_rq, mp); 17619 } 17620 17621 17622 /* 17623 * This is the STREAMS entry point for T_CONN_RES coming down on 17624 * Acceptor STREAM when sockfs listener does accept processing. 17625 * Read the block comment on top pf tcp_conn_request(). 17626 */ 17627 void 17628 tcp_wput_accept(queue_t *q, mblk_t *mp) 17629 { 17630 queue_t *rq = RD(q); 17631 struct T_conn_res *conn_res; 17632 tcp_t *eager; 17633 tcp_t *listener; 17634 struct T_ok_ack *ok; 17635 t_scalar_t PRIM_type; 17636 mblk_t *opt_mp; 17637 conn_t *econnp; 17638 17639 ASSERT(DB_TYPE(mp) == M_PROTO); 17640 17641 conn_res = (struct T_conn_res *)mp->b_rptr; 17642 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17643 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17644 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17645 if (mp != NULL) 17646 putnext(rq, mp); 17647 return; 17648 } 17649 switch (conn_res->PRIM_type) { 17650 case O_T_CONN_RES: 17651 case T_CONN_RES: 17652 /* 17653 * We pass up an err ack if allocb fails. This will 17654 * cause sockfs to issue a T_DISCON_REQ which will cause 17655 * tcp_eager_blowoff to be called. sockfs will then call 17656 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17657 * we need to do the allocb up here because we have to 17658 * make sure rq->q_qinfo->qi_qclose still points to the 17659 * correct function (tcpclose_accept) in case allocb 17660 * fails. 17661 */ 17662 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17663 if (opt_mp == NULL) { 17664 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17665 if (mp != NULL) 17666 putnext(rq, mp); 17667 return; 17668 } 17669 17670 bcopy(mp->b_rptr + conn_res->OPT_offset, 17671 &eager, conn_res->OPT_length); 17672 PRIM_type = conn_res->PRIM_type; 17673 mp->b_datap->db_type = M_PCPROTO; 17674 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17675 ok = (struct T_ok_ack *)mp->b_rptr; 17676 ok->PRIM_type = T_OK_ACK; 17677 ok->CORRECT_prim = PRIM_type; 17678 econnp = eager->tcp_connp; 17679 econnp->conn_dev = (dev_t)q->q_ptr; 17680 eager->tcp_rq = rq; 17681 eager->tcp_wq = q; 17682 rq->q_ptr = econnp; 17683 rq->q_qinfo = &tcp_rinit; 17684 q->q_ptr = econnp; 17685 q->q_qinfo = &tcp_winit; 17686 listener = eager->tcp_listener; 17687 eager->tcp_issocket = B_TRUE; 17688 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17689 17690 /* Put the ref for IP */ 17691 CONN_INC_REF(econnp); 17692 17693 /* 17694 * We should have minimum of 3 references on the conn 17695 * at this point. One each for TCP and IP and one for 17696 * the T_conn_ind that was sent up when the 3-way handshake 17697 * completed. In the normal case we would also have another 17698 * reference (making a total of 4) for the conn being in the 17699 * classifier hash list. However the eager could have received 17700 * an RST subsequently and tcp_closei_local could have removed 17701 * the eager from the classifier hash list, hence we can't 17702 * assert that reference. 17703 */ 17704 ASSERT(econnp->conn_ref >= 3); 17705 17706 /* 17707 * Send the new local address also up to sockfs. There 17708 * should already be enough space in the mp that came 17709 * down from soaccept(). 17710 */ 17711 if (eager->tcp_family == AF_INET) { 17712 sin_t *sin; 17713 17714 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17715 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17716 sin = (sin_t *)mp->b_wptr; 17717 mp->b_wptr += sizeof (sin_t); 17718 sin->sin_family = AF_INET; 17719 sin->sin_port = eager->tcp_lport; 17720 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17721 } else { 17722 sin6_t *sin6; 17723 17724 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17725 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17726 sin6 = (sin6_t *)mp->b_wptr; 17727 mp->b_wptr += sizeof (sin6_t); 17728 sin6->sin6_family = AF_INET6; 17729 sin6->sin6_port = eager->tcp_lport; 17730 if (eager->tcp_ipversion == IPV4_VERSION) { 17731 sin6->sin6_flowinfo = 0; 17732 IN6_IPADDR_TO_V4MAPPED( 17733 eager->tcp_ipha->ipha_src, 17734 &sin6->sin6_addr); 17735 } else { 17736 ASSERT(eager->tcp_ip6h != NULL); 17737 sin6->sin6_flowinfo = 17738 eager->tcp_ip6h->ip6_vcf & 17739 ~IPV6_VERS_AND_FLOW_MASK; 17740 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17741 } 17742 sin6->sin6_scope_id = 0; 17743 sin6->__sin6_src_id = 0; 17744 } 17745 17746 putnext(rq, mp); 17747 17748 opt_mp->b_datap->db_type = M_SETOPTS; 17749 opt_mp->b_wptr += sizeof (struct stroptions); 17750 17751 /* 17752 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17753 * from listener to acceptor. The message is chained on the 17754 * bind_mp which tcp_rput_other will send down to IP. 17755 */ 17756 if (listener->tcp_bound_if != 0) { 17757 /* allocate optmgmt req */ 17758 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 17759 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 17760 sizeof (int)); 17761 if (mp != NULL) 17762 linkb(opt_mp, mp); 17763 } 17764 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17765 uint_t on = 1; 17766 17767 /* allocate optmgmt req */ 17768 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 17769 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 17770 if (mp != NULL) 17771 linkb(opt_mp, mp); 17772 } 17773 17774 17775 mutex_enter(&listener->tcp_eager_lock); 17776 17777 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17778 17779 tcp_t *tail; 17780 tcp_t *tcp; 17781 mblk_t *mp1; 17782 17783 tcp = listener->tcp_eager_prev_q0; 17784 /* 17785 * listener->tcp_eager_prev_q0 points to the TAIL of the 17786 * deferred T_conn_ind queue. We need to get to the head 17787 * of the queue in order to send up T_conn_ind the same 17788 * order as how the 3WHS is completed. 17789 */ 17790 while (tcp != listener) { 17791 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17792 !tcp->tcp_kssl_pending) 17793 break; 17794 else 17795 tcp = tcp->tcp_eager_prev_q0; 17796 } 17797 /* None of the pending eagers can be sent up now */ 17798 if (tcp == listener) 17799 goto no_more_eagers; 17800 17801 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17802 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17803 /* Move from q0 to q */ 17804 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17805 listener->tcp_conn_req_cnt_q0--; 17806 listener->tcp_conn_req_cnt_q++; 17807 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17808 tcp->tcp_eager_prev_q0; 17809 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17810 tcp->tcp_eager_next_q0; 17811 tcp->tcp_eager_prev_q0 = NULL; 17812 tcp->tcp_eager_next_q0 = NULL; 17813 tcp->tcp_conn_def_q0 = B_FALSE; 17814 17815 /* 17816 * Insert at end of the queue because sockfs sends 17817 * down T_CONN_RES in chronological order. Leaving 17818 * the older conn indications at front of the queue 17819 * helps reducing search time. 17820 */ 17821 tail = listener->tcp_eager_last_q; 17822 if (tail != NULL) { 17823 tail->tcp_eager_next_q = tcp; 17824 } else { 17825 listener->tcp_eager_next_q = tcp; 17826 } 17827 listener->tcp_eager_last_q = tcp; 17828 tcp->tcp_eager_next_q = NULL; 17829 17830 /* Need to get inside the listener perimeter */ 17831 CONN_INC_REF(listener->tcp_connp); 17832 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 17833 tcp_send_pending, listener->tcp_connp, 17834 SQTAG_TCP_SEND_PENDING); 17835 } 17836 no_more_eagers: 17837 tcp_eager_unlink(eager); 17838 mutex_exit(&listener->tcp_eager_lock); 17839 17840 /* 17841 * At this point, the eager is detached from the listener 17842 * but we still have an extra refs on eager (apart from the 17843 * usual tcp references). The ref was placed in tcp_rput_data 17844 * before sending the conn_ind in tcp_send_conn_ind. 17845 * The ref will be dropped in tcp_accept_finish(). 17846 */ 17847 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 17848 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 17849 return; 17850 default: 17851 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17852 if (mp != NULL) 17853 putnext(rq, mp); 17854 return; 17855 } 17856 } 17857 17858 void 17859 tcp_wput(queue_t *q, mblk_t *mp) 17860 { 17861 conn_t *connp = Q_TO_CONN(q); 17862 tcp_t *tcp; 17863 void (*output_proc)(); 17864 t_scalar_t type; 17865 uchar_t *rptr; 17866 struct iocblk *iocp; 17867 uint32_t msize; 17868 17869 ASSERT(connp->conn_ref >= 2); 17870 17871 switch (DB_TYPE(mp)) { 17872 case M_DATA: 17873 tcp = connp->conn_tcp; 17874 ASSERT(tcp != NULL); 17875 17876 msize = msgdsize(mp); 17877 17878 mutex_enter(&connp->conn_lock); 17879 CONN_INC_REF_LOCKED(connp); 17880 17881 tcp->tcp_squeue_bytes += msize; 17882 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 17883 mutex_exit(&connp->conn_lock); 17884 tcp_setqfull(tcp); 17885 } else 17886 mutex_exit(&connp->conn_lock); 17887 17888 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 17889 tcp_output, connp, SQTAG_TCP_OUTPUT); 17890 return; 17891 case M_PROTO: 17892 case M_PCPROTO: 17893 /* 17894 * if it is a snmp message, don't get behind the squeue 17895 */ 17896 tcp = connp->conn_tcp; 17897 rptr = mp->b_rptr; 17898 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 17899 type = ((union T_primitives *)rptr)->type; 17900 } else { 17901 if (tcp->tcp_debug) { 17902 (void) strlog(TCP_MOD_ID, 0, 1, 17903 SL_ERROR|SL_TRACE, 17904 "tcp_wput_proto, dropping one..."); 17905 } 17906 freemsg(mp); 17907 return; 17908 } 17909 if (type == T_SVR4_OPTMGMT_REQ) { 17910 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 17911 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 17912 cr)) { 17913 /* 17914 * This was a SNMP request 17915 */ 17916 return; 17917 } else { 17918 output_proc = tcp_wput_proto; 17919 } 17920 } else { 17921 output_proc = tcp_wput_proto; 17922 } 17923 break; 17924 case M_IOCTL: 17925 /* 17926 * Most ioctls can be processed right away without going via 17927 * squeues - process them right here. Those that do require 17928 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 17929 * are processed by tcp_wput_ioctl(). 17930 */ 17931 iocp = (struct iocblk *)mp->b_rptr; 17932 tcp = connp->conn_tcp; 17933 17934 switch (iocp->ioc_cmd) { 17935 case TCP_IOC_ABORT_CONN: 17936 tcp_ioctl_abort_conn(q, mp); 17937 return; 17938 case TI_GETPEERNAME: 17939 if (tcp->tcp_state < TCPS_SYN_RCVD) { 17940 iocp->ioc_error = ENOTCONN; 17941 iocp->ioc_count = 0; 17942 mp->b_datap->db_type = M_IOCACK; 17943 qreply(q, mp); 17944 return; 17945 } 17946 /* FALLTHRU */ 17947 case TI_GETMYNAME: 17948 mi_copyin(q, mp, NULL, 17949 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 17950 return; 17951 case ND_SET: 17952 /* nd_getset does the necessary checks */ 17953 case ND_GET: 17954 if (!nd_getset(q, tcp_g_nd, mp)) { 17955 CALL_IP_WPUT(connp, q, mp); 17956 return; 17957 } 17958 qreply(q, mp); 17959 return; 17960 case TCP_IOC_DEFAULT_Q: 17961 /* 17962 * Wants to be the default wq. Check the credentials 17963 * first, the rest is executed via squeue. 17964 */ 17965 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 17966 iocp->ioc_error = EPERM; 17967 iocp->ioc_count = 0; 17968 mp->b_datap->db_type = M_IOCACK; 17969 qreply(q, mp); 17970 return; 17971 } 17972 output_proc = tcp_wput_ioctl; 17973 break; 17974 default: 17975 output_proc = tcp_wput_ioctl; 17976 break; 17977 } 17978 break; 17979 default: 17980 output_proc = tcp_wput_nondata; 17981 break; 17982 } 17983 17984 CONN_INC_REF(connp); 17985 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 17986 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 17987 } 17988 17989 /* 17990 * Initial STREAMS write side put() procedure for sockets. It tries to 17991 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 17992 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 17993 * are handled by tcp_wput() as usual. 17994 * 17995 * All further messages will also be handled by tcp_wput() because we cannot 17996 * be sure that the above short cut is safe later. 17997 */ 17998 static void 17999 tcp_wput_sock(queue_t *wq, mblk_t *mp) 18000 { 18001 conn_t *connp = Q_TO_CONN(wq); 18002 tcp_t *tcp = connp->conn_tcp; 18003 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 18004 18005 ASSERT(wq->q_qinfo == &tcp_sock_winit); 18006 wq->q_qinfo = &tcp_winit; 18007 18008 ASSERT(IPCL_IS_TCP(connp)); 18009 ASSERT(TCP_IS_SOCKET(tcp)); 18010 18011 if (DB_TYPE(mp) == M_PCPROTO && 18012 MBLKL(mp) == sizeof (struct T_capability_req) && 18013 car->PRIM_type == T_CAPABILITY_REQ) { 18014 tcp_capability_req(tcp, mp); 18015 return; 18016 } 18017 18018 tcp_wput(wq, mp); 18019 } 18020 18021 static boolean_t 18022 tcp_zcopy_check(tcp_t *tcp) 18023 { 18024 conn_t *connp = tcp->tcp_connp; 18025 ire_t *ire; 18026 boolean_t zc_enabled = B_FALSE; 18027 18028 if (do_tcpzcopy == 2) 18029 zc_enabled = B_TRUE; 18030 else if (tcp->tcp_ipversion == IPV4_VERSION && 18031 IPCL_IS_CONNECTED(connp) && 18032 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 18033 connp->conn_dontroute == 0 && 18034 !connp->conn_nexthop_set && 18035 connp->conn_xmit_if_ill == NULL && 18036 connp->conn_nofailover_ill == NULL && 18037 do_tcpzcopy == 1) { 18038 /* 18039 * the checks above closely resemble the fast path checks 18040 * in tcp_send_data(). 18041 */ 18042 mutex_enter(&connp->conn_lock); 18043 ire = connp->conn_ire_cache; 18044 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18045 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18046 IRE_REFHOLD(ire); 18047 if (ire->ire_stq != NULL) { 18048 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 18049 18050 zc_enabled = ill && (ill->ill_capabilities & 18051 ILL_CAPAB_ZEROCOPY) && 18052 (ill->ill_zerocopy_capab-> 18053 ill_zerocopy_flags != 0); 18054 } 18055 IRE_REFRELE(ire); 18056 } 18057 mutex_exit(&connp->conn_lock); 18058 } 18059 tcp->tcp_snd_zcopy_on = zc_enabled; 18060 if (!TCP_IS_DETACHED(tcp)) { 18061 if (zc_enabled) { 18062 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 18063 TCP_STAT(tcp_zcopy_on); 18064 } else { 18065 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18066 TCP_STAT(tcp_zcopy_off); 18067 } 18068 } 18069 return (zc_enabled); 18070 } 18071 18072 static mblk_t * 18073 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 18074 { 18075 if (do_tcpzcopy == 2) 18076 return (bp); 18077 else if (tcp->tcp_snd_zcopy_on) { 18078 tcp->tcp_snd_zcopy_on = B_FALSE; 18079 if (!TCP_IS_DETACHED(tcp)) { 18080 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 18081 TCP_STAT(tcp_zcopy_disable); 18082 } 18083 } 18084 return (tcp_zcopy_backoff(tcp, bp, 0)); 18085 } 18086 18087 /* 18088 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 18089 * the original desballoca'ed segmapped mblk. 18090 */ 18091 static mblk_t * 18092 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 18093 { 18094 mblk_t *head, *tail, *nbp; 18095 if (IS_VMLOANED_MBLK(bp)) { 18096 TCP_STAT(tcp_zcopy_backoff); 18097 if ((head = copyb(bp)) == NULL) { 18098 /* fail to backoff; leave it for the next backoff */ 18099 tcp->tcp_xmit_zc_clean = B_FALSE; 18100 return (bp); 18101 } 18102 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18103 if (fix_xmitlist) 18104 tcp_zcopy_notify(tcp); 18105 else 18106 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18107 } 18108 nbp = bp->b_cont; 18109 if (fix_xmitlist) { 18110 head->b_prev = bp->b_prev; 18111 head->b_next = bp->b_next; 18112 if (tcp->tcp_xmit_tail == bp) 18113 tcp->tcp_xmit_tail = head; 18114 } 18115 bp->b_next = NULL; 18116 bp->b_prev = NULL; 18117 freeb(bp); 18118 } else { 18119 head = bp; 18120 nbp = bp->b_cont; 18121 } 18122 tail = head; 18123 while (nbp) { 18124 if (IS_VMLOANED_MBLK(nbp)) { 18125 TCP_STAT(tcp_zcopy_backoff); 18126 if ((tail->b_cont = copyb(nbp)) == NULL) { 18127 tcp->tcp_xmit_zc_clean = B_FALSE; 18128 tail->b_cont = nbp; 18129 return (head); 18130 } 18131 tail = tail->b_cont; 18132 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 18133 if (fix_xmitlist) 18134 tcp_zcopy_notify(tcp); 18135 else 18136 tail->b_datap->db_struioflag |= 18137 STRUIO_ZCNOTIFY; 18138 } 18139 bp = nbp; 18140 nbp = nbp->b_cont; 18141 if (fix_xmitlist) { 18142 tail->b_prev = bp->b_prev; 18143 tail->b_next = bp->b_next; 18144 if (tcp->tcp_xmit_tail == bp) 18145 tcp->tcp_xmit_tail = tail; 18146 } 18147 bp->b_next = NULL; 18148 bp->b_prev = NULL; 18149 freeb(bp); 18150 } else { 18151 tail->b_cont = nbp; 18152 tail = nbp; 18153 nbp = nbp->b_cont; 18154 } 18155 } 18156 if (fix_xmitlist) { 18157 tcp->tcp_xmit_last = tail; 18158 tcp->tcp_xmit_zc_clean = B_TRUE; 18159 } 18160 return (head); 18161 } 18162 18163 static void 18164 tcp_zcopy_notify(tcp_t *tcp) 18165 { 18166 struct stdata *stp; 18167 18168 if (tcp->tcp_detached) 18169 return; 18170 stp = STREAM(tcp->tcp_rq); 18171 mutex_enter(&stp->sd_lock); 18172 stp->sd_flag |= STZCNOTIFY; 18173 cv_broadcast(&stp->sd_zcopy_wait); 18174 mutex_exit(&stp->sd_lock); 18175 } 18176 18177 static void 18178 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 18179 { 18180 ipha_t *ipha; 18181 ipaddr_t src; 18182 ipaddr_t dst; 18183 uint32_t cksum; 18184 ire_t *ire; 18185 uint16_t *up; 18186 ill_t *ill; 18187 conn_t *connp = tcp->tcp_connp; 18188 uint32_t hcksum_txflags = 0; 18189 mblk_t *ire_fp_mp; 18190 uint_t ire_fp_mp_len; 18191 18192 ASSERT(DB_TYPE(mp) == M_DATA); 18193 18194 if (DB_CRED(mp) == NULL) 18195 mblk_setcred(mp, CONN_CRED(connp)); 18196 18197 ipha = (ipha_t *)mp->b_rptr; 18198 src = ipha->ipha_src; 18199 dst = ipha->ipha_dst; 18200 18201 /* 18202 * Drop off fast path for IPv6 and also if options are present or 18203 * we need to resolve a TS label. 18204 */ 18205 if (tcp->tcp_ipversion != IPV4_VERSION || 18206 !IPCL_IS_CONNECTED(connp) || 18207 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 18208 connp->conn_dontroute || 18209 connp->conn_nexthop_set || 18210 connp->conn_xmit_if_ill != NULL || 18211 connp->conn_nofailover_ill != NULL || 18212 !connp->conn_ulp_labeled || 18213 ipha->ipha_ident == IP_HDR_INCLUDED || 18214 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 18215 IPP_ENABLED(IPP_LOCAL_OUT)) { 18216 if (tcp->tcp_snd_zcopy_aware) 18217 mp = tcp_zcopy_disable(tcp, mp); 18218 TCP_STAT(tcp_ip_send); 18219 CALL_IP_WPUT(connp, q, mp); 18220 return; 18221 } 18222 18223 mutex_enter(&connp->conn_lock); 18224 ire = connp->conn_ire_cache; 18225 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 18226 if (ire != NULL && ire->ire_addr == dst && 18227 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18228 IRE_REFHOLD(ire); 18229 mutex_exit(&connp->conn_lock); 18230 } else { 18231 boolean_t cached = B_FALSE; 18232 18233 /* force a recheck later on */ 18234 tcp->tcp_ire_ill_check_done = B_FALSE; 18235 18236 TCP_DBGSTAT(tcp_ire_null1); 18237 connp->conn_ire_cache = NULL; 18238 mutex_exit(&connp->conn_lock); 18239 if (ire != NULL) 18240 IRE_REFRELE_NOTR(ire); 18241 ire = ire_cache_lookup(dst, connp->conn_zoneid, 18242 MBLK_GETLABEL(mp)); 18243 if (ire == NULL) { 18244 if (tcp->tcp_snd_zcopy_aware) 18245 mp = tcp_zcopy_backoff(tcp, mp, 0); 18246 TCP_STAT(tcp_ire_null); 18247 CALL_IP_WPUT(connp, q, mp); 18248 return; 18249 } 18250 IRE_REFHOLD_NOTR(ire); 18251 /* 18252 * Since we are inside the squeue, there cannot be another 18253 * thread in TCP trying to set the conn_ire_cache now. The 18254 * check for IRE_MARK_CONDEMNED ensures that an interface 18255 * unplumb thread has not yet started cleaning up the conns. 18256 * Hence we don't need to grab the conn lock. 18257 */ 18258 if (!(connp->conn_state_flags & CONN_CLOSING)) { 18259 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18260 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18261 connp->conn_ire_cache = ire; 18262 cached = B_TRUE; 18263 } 18264 rw_exit(&ire->ire_bucket->irb_lock); 18265 } 18266 18267 /* 18268 * We can continue to use the ire but since it was 18269 * not cached, we should drop the extra reference. 18270 */ 18271 if (!cached) 18272 IRE_REFRELE_NOTR(ire); 18273 18274 /* 18275 * Rampart note: no need to select a new label here, since 18276 * labels are not allowed to change during the life of a TCP 18277 * connection. 18278 */ 18279 } 18280 18281 if (ire->ire_flags & RTF_MULTIRT || 18282 ire->ire_stq == NULL || 18283 ire->ire_max_frag < ntohs(ipha->ipha_length) || 18284 (ire_fp_mp = ire->ire_fp_mp) == NULL || 18285 (ire_fp_mp_len = MBLKL(ire_fp_mp)) > MBLKHEAD(mp)) { 18286 if (tcp->tcp_snd_zcopy_aware) 18287 mp = tcp_zcopy_disable(tcp, mp); 18288 TCP_STAT(tcp_ip_ire_send); 18289 IRE_REFRELE(ire); 18290 CALL_IP_WPUT(connp, q, mp); 18291 return; 18292 } 18293 18294 ill = ire_to_ill(ire); 18295 if (connp->conn_outgoing_ill != NULL) { 18296 ill_t *conn_outgoing_ill = NULL; 18297 /* 18298 * Choose a good ill in the group to send the packets on. 18299 */ 18300 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18301 ill = ire_to_ill(ire); 18302 } 18303 ASSERT(ill != NULL); 18304 18305 if (!tcp->tcp_ire_ill_check_done) { 18306 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18307 tcp->tcp_ire_ill_check_done = B_TRUE; 18308 } 18309 18310 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18311 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18312 #ifndef _BIG_ENDIAN 18313 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18314 #endif 18315 18316 /* 18317 * Check to see if we need to re-enable MDT for this connection 18318 * because it was previously disabled due to changes in the ill; 18319 * note that by doing it here, this re-enabling only applies when 18320 * the packet is not dispatched through CALL_IP_WPUT(). 18321 * 18322 * That means for IPv4, it is worth re-enabling MDT for the fastpath 18323 * case, since that's how we ended up here. For IPv6, we do the 18324 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18325 */ 18326 if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18327 /* 18328 * Restore MDT for this connection, so that next time around 18329 * it is eligible to go through tcp_multisend() path again. 18330 */ 18331 TCP_STAT(tcp_mdt_conn_resumed1); 18332 tcp->tcp_mdt = B_TRUE; 18333 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18334 "interface %s\n", (void *)connp, ill->ill_name)); 18335 } 18336 18337 if (tcp->tcp_snd_zcopy_aware) { 18338 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18339 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18340 mp = tcp_zcopy_disable(tcp, mp); 18341 /* 18342 * we shouldn't need to reset ipha as the mp containing 18343 * ipha should never be a zero-copy mp. 18344 */ 18345 } 18346 18347 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18348 ASSERT(ill->ill_hcksum_capab != NULL); 18349 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18350 } 18351 18352 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18353 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18354 18355 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18356 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18357 18358 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18359 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18360 18361 /* Software checksum? */ 18362 if (DB_CKSUMFLAGS(mp) == 0) { 18363 TCP_STAT(tcp_out_sw_cksum); 18364 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 18365 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18366 } 18367 18368 ipha->ipha_fragment_offset_and_flags |= 18369 (uint32_t)htons(ire->ire_frag_flag); 18370 18371 /* Calculate IP header checksum if hardware isn't capable */ 18372 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18373 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18374 ((uint16_t *)ipha)[4]); 18375 } 18376 18377 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18378 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18379 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18380 18381 UPDATE_OB_PKT_COUNT(ire); 18382 ire->ire_last_used_time = lbolt; 18383 BUMP_MIB(&ip_mib, ipOutRequests); 18384 18385 if (ILL_DLS_CAPABLE(ill)) { 18386 /* 18387 * Send the packet directly to DLD, where it may be queued 18388 * depending on the availability of transmit resources at 18389 * the media layer. 18390 */ 18391 IP_DLS_ILL_TX(ill, mp); 18392 } else { 18393 putnext(ire->ire_stq, mp); 18394 } 18395 IRE_REFRELE(ire); 18396 } 18397 18398 /* 18399 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18400 * if the receiver shrinks the window, i.e. moves the right window to the 18401 * left, the we should not send new data, but should retransmit normally the 18402 * old unacked data between suna and suna + swnd. We might has sent data 18403 * that is now outside the new window, pretend that we didn't send it. 18404 */ 18405 static void 18406 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18407 { 18408 uint32_t snxt = tcp->tcp_snxt; 18409 mblk_t *xmit_tail; 18410 int32_t offset; 18411 18412 ASSERT(shrunk_count > 0); 18413 18414 /* Pretend we didn't send the data outside the window */ 18415 snxt -= shrunk_count; 18416 18417 /* Get the mblk and the offset in it per the shrunk window */ 18418 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18419 18420 ASSERT(xmit_tail != NULL); 18421 18422 /* Reset all the values per the now shrunk window */ 18423 tcp->tcp_snxt = snxt; 18424 tcp->tcp_xmit_tail = xmit_tail; 18425 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18426 offset; 18427 tcp->tcp_unsent += shrunk_count; 18428 18429 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18430 /* 18431 * Make sure the timer is running so that we will probe a zero 18432 * window. 18433 */ 18434 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18435 } 18436 18437 18438 /* 18439 * The TCP normal data output path. 18440 * NOTE: the logic of the fast path is duplicated from this function. 18441 */ 18442 static void 18443 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18444 { 18445 int len; 18446 mblk_t *local_time; 18447 mblk_t *mp1; 18448 uint32_t snxt; 18449 int tail_unsent; 18450 int tcpstate; 18451 int usable = 0; 18452 mblk_t *xmit_tail; 18453 queue_t *q = tcp->tcp_wq; 18454 int32_t mss; 18455 int32_t num_sack_blk = 0; 18456 int32_t tcp_hdr_len; 18457 int32_t tcp_tcp_hdr_len; 18458 int mdt_thres; 18459 int rc; 18460 18461 tcpstate = tcp->tcp_state; 18462 if (mp == NULL) { 18463 /* 18464 * tcp_wput_data() with NULL mp should only be called when 18465 * there is unsent data. 18466 */ 18467 ASSERT(tcp->tcp_unsent > 0); 18468 /* Really tacky... but we need this for detached closes. */ 18469 len = tcp->tcp_unsent; 18470 goto data_null; 18471 } 18472 18473 #if CCS_STATS 18474 wrw_stats.tot.count++; 18475 wrw_stats.tot.bytes += msgdsize(mp); 18476 #endif 18477 ASSERT(mp->b_datap->db_type == M_DATA); 18478 /* 18479 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18480 * or before a connection attempt has begun. 18481 */ 18482 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18483 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18484 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18485 #ifdef DEBUG 18486 cmn_err(CE_WARN, 18487 "tcp_wput_data: data after ordrel, %s", 18488 tcp_display(tcp, NULL, 18489 DISP_ADDR_AND_PORT)); 18490 #else 18491 if (tcp->tcp_debug) { 18492 (void) strlog(TCP_MOD_ID, 0, 1, 18493 SL_TRACE|SL_ERROR, 18494 "tcp_wput_data: data after ordrel, %s\n", 18495 tcp_display(tcp, NULL, 18496 DISP_ADDR_AND_PORT)); 18497 } 18498 #endif /* DEBUG */ 18499 } 18500 if (tcp->tcp_snd_zcopy_aware && 18501 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18502 tcp_zcopy_notify(tcp); 18503 freemsg(mp); 18504 if (tcp->tcp_flow_stopped && 18505 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18506 tcp_clrqfull(tcp); 18507 } 18508 return; 18509 } 18510 18511 /* Strip empties */ 18512 for (;;) { 18513 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18514 (uintptr_t)INT_MAX); 18515 len = (int)(mp->b_wptr - mp->b_rptr); 18516 if (len > 0) 18517 break; 18518 mp1 = mp; 18519 mp = mp->b_cont; 18520 freeb(mp1); 18521 if (!mp) { 18522 return; 18523 } 18524 } 18525 18526 /* If we are the first on the list ... */ 18527 if (tcp->tcp_xmit_head == NULL) { 18528 tcp->tcp_xmit_head = mp; 18529 tcp->tcp_xmit_tail = mp; 18530 tcp->tcp_xmit_tail_unsent = len; 18531 } else { 18532 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18533 struct datab *dp; 18534 18535 mp1 = tcp->tcp_xmit_last; 18536 if (len < tcp_tx_pull_len && 18537 (dp = mp1->b_datap)->db_ref == 1 && 18538 dp->db_lim - mp1->b_wptr >= len) { 18539 ASSERT(len > 0); 18540 ASSERT(!mp1->b_cont); 18541 if (len == 1) { 18542 *mp1->b_wptr++ = *mp->b_rptr; 18543 } else { 18544 bcopy(mp->b_rptr, mp1->b_wptr, len); 18545 mp1->b_wptr += len; 18546 } 18547 if (mp1 == tcp->tcp_xmit_tail) 18548 tcp->tcp_xmit_tail_unsent += len; 18549 mp1->b_cont = mp->b_cont; 18550 if (tcp->tcp_snd_zcopy_aware && 18551 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18552 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18553 freeb(mp); 18554 mp = mp1; 18555 } else { 18556 tcp->tcp_xmit_last->b_cont = mp; 18557 } 18558 len += tcp->tcp_unsent; 18559 } 18560 18561 /* Tack on however many more positive length mblks we have */ 18562 if ((mp1 = mp->b_cont) != NULL) { 18563 do { 18564 int tlen; 18565 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18566 (uintptr_t)INT_MAX); 18567 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18568 if (tlen <= 0) { 18569 mp->b_cont = mp1->b_cont; 18570 freeb(mp1); 18571 } else { 18572 len += tlen; 18573 mp = mp1; 18574 } 18575 } while ((mp1 = mp->b_cont) != NULL); 18576 } 18577 tcp->tcp_xmit_last = mp; 18578 tcp->tcp_unsent = len; 18579 18580 if (urgent) 18581 usable = 1; 18582 18583 data_null: 18584 snxt = tcp->tcp_snxt; 18585 xmit_tail = tcp->tcp_xmit_tail; 18586 tail_unsent = tcp->tcp_xmit_tail_unsent; 18587 18588 /* 18589 * Note that tcp_mss has been adjusted to take into account the 18590 * timestamp option if applicable. Because SACK options do not 18591 * appear in every TCP segments and they are of variable lengths, 18592 * they cannot be included in tcp_mss. Thus we need to calculate 18593 * the actual segment length when we need to send a segment which 18594 * includes SACK options. 18595 */ 18596 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18597 int32_t opt_len; 18598 18599 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18600 tcp->tcp_num_sack_blk); 18601 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18602 2 + TCPOPT_HEADER_LEN; 18603 mss = tcp->tcp_mss - opt_len; 18604 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18605 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18606 } else { 18607 mss = tcp->tcp_mss; 18608 tcp_hdr_len = tcp->tcp_hdr_len; 18609 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18610 } 18611 18612 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18613 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18614 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 18615 } 18616 if (tcpstate == TCPS_SYN_RCVD) { 18617 /* 18618 * The three-way connection establishment handshake is not 18619 * complete yet. We want to queue the data for transmission 18620 * after entering ESTABLISHED state (RFC793). A jump to 18621 * "done" label effectively leaves data on the queue. 18622 */ 18623 goto done; 18624 } else { 18625 int usable_r; 18626 18627 /* 18628 * In the special case when cwnd is zero, which can only 18629 * happen if the connection is ECN capable, return now. 18630 * New segments is sent using tcp_timer(). The timer 18631 * is set in tcp_rput_data(). 18632 */ 18633 if (tcp->tcp_cwnd == 0) { 18634 /* 18635 * Note that tcp_cwnd is 0 before 3-way handshake is 18636 * finished. 18637 */ 18638 ASSERT(tcp->tcp_ecn_ok || 18639 tcp->tcp_state < TCPS_ESTABLISHED); 18640 return; 18641 } 18642 18643 /* NOTE: trouble if xmitting while SYN not acked? */ 18644 usable_r = snxt - tcp->tcp_suna; 18645 usable_r = tcp->tcp_swnd - usable_r; 18646 18647 /* 18648 * Check if the receiver has shrunk the window. If 18649 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18650 * cannot be set as there is unsent data, so FIN cannot 18651 * be sent out. Otherwise, we need to take into account 18652 * of FIN as it consumes an "invisible" sequence number. 18653 */ 18654 ASSERT(tcp->tcp_fin_sent == 0); 18655 if (usable_r < 0) { 18656 /* 18657 * The receiver has shrunk the window and we have sent 18658 * -usable_r date beyond the window, re-adjust. 18659 * 18660 * If TCP window scaling is enabled, there can be 18661 * round down error as the advertised receive window 18662 * is actually right shifted n bits. This means that 18663 * the lower n bits info is wiped out. It will look 18664 * like the window is shrunk. Do a check here to 18665 * see if the shrunk amount is actually within the 18666 * error in window calculation. If it is, just 18667 * return. Note that this check is inside the 18668 * shrunk window check. This makes sure that even 18669 * though tcp_process_shrunk_swnd() is not called, 18670 * we will stop further processing. 18671 */ 18672 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18673 tcp_process_shrunk_swnd(tcp, -usable_r); 18674 } 18675 return; 18676 } 18677 18678 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18679 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18680 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18681 18682 /* usable = MIN(usable, unsent) */ 18683 if (usable_r > len) 18684 usable_r = len; 18685 18686 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18687 if (usable_r > 0) { 18688 usable = usable_r; 18689 } else { 18690 /* Bypass all other unnecessary processing. */ 18691 goto done; 18692 } 18693 } 18694 18695 local_time = (mblk_t *)lbolt; 18696 18697 /* 18698 * "Our" Nagle Algorithm. This is not the same as in the old 18699 * BSD. This is more in line with the true intent of Nagle. 18700 * 18701 * The conditions are: 18702 * 1. The amount of unsent data (or amount of data which can be 18703 * sent, whichever is smaller) is less than Nagle limit. 18704 * 2. The last sent size is also less than Nagle limit. 18705 * 3. There is unack'ed data. 18706 * 4. Urgent pointer is not set. Send urgent data ignoring the 18707 * Nagle algorithm. This reduces the probability that urgent 18708 * bytes get "merged" together. 18709 * 5. The app has not closed the connection. This eliminates the 18710 * wait time of the receiving side waiting for the last piece of 18711 * (small) data. 18712 * 18713 * If all are satisified, exit without sending anything. Note 18714 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 18715 * the smaller of 1 MSS and global tcp_naglim_def (default to be 18716 * 4095). 18717 */ 18718 if (usable < (int)tcp->tcp_naglim && 18719 tcp->tcp_naglim > tcp->tcp_last_sent_len && 18720 snxt != tcp->tcp_suna && 18721 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 18722 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 18723 goto done; 18724 } 18725 18726 if (tcp->tcp_cork) { 18727 /* 18728 * if the tcp->tcp_cork option is set, then we have to force 18729 * TCP not to send partial segment (smaller than MSS bytes). 18730 * We are calculating the usable now based on full mss and 18731 * will save the rest of remaining data for later. 18732 */ 18733 if (usable < mss) 18734 goto done; 18735 usable = (usable / mss) * mss; 18736 } 18737 18738 /* Update the latest receive window size in TCP header. */ 18739 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18740 tcp->tcp_tcph->th_win); 18741 18742 /* 18743 * Determine if it's worthwhile to attempt MDT, based on: 18744 * 18745 * 1. Simple TCP/IP{v4,v6} (no options). 18746 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 18747 * 3. If the TCP connection is in ESTABLISHED state. 18748 * 4. The TCP is not detached. 18749 * 18750 * If any of the above conditions have changed during the 18751 * connection, stop using MDT and restore the stream head 18752 * parameters accordingly. 18753 */ 18754 if (tcp->tcp_mdt && 18755 ((tcp->tcp_ipversion == IPV4_VERSION && 18756 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 18757 (tcp->tcp_ipversion == IPV6_VERSION && 18758 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 18759 tcp->tcp_state != TCPS_ESTABLISHED || 18760 TCP_IS_DETACHED(tcp) || !CONN_IS_MD_FASTPATH(tcp->tcp_connp) || 18761 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 18762 IPP_ENABLED(IPP_LOCAL_OUT))) { 18763 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 18764 tcp->tcp_mdt = B_FALSE; 18765 18766 /* Anything other than detached is considered pathological */ 18767 if (!TCP_IS_DETACHED(tcp)) { 18768 TCP_STAT(tcp_mdt_conn_halted1); 18769 (void) tcp_maxpsz_set(tcp, B_TRUE); 18770 } 18771 } 18772 18773 /* Use MDT if sendable amount is greater than the threshold */ 18774 if (tcp->tcp_mdt && 18775 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 18776 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 18777 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 18778 (tcp->tcp_valid_bits == 0 || 18779 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 18780 ASSERT(tcp->tcp_connp->conn_mdt_ok); 18781 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18782 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18783 local_time, mdt_thres); 18784 } else { 18785 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18786 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18787 local_time, INT_MAX); 18788 } 18789 18790 /* Pretend that all we were trying to send really got sent */ 18791 if (rc < 0 && tail_unsent < 0) { 18792 do { 18793 xmit_tail = xmit_tail->b_cont; 18794 xmit_tail->b_prev = local_time; 18795 ASSERT((uintptr_t)(xmit_tail->b_wptr - 18796 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 18797 tail_unsent += (int)(xmit_tail->b_wptr - 18798 xmit_tail->b_rptr); 18799 } while (tail_unsent < 0); 18800 } 18801 done:; 18802 tcp->tcp_xmit_tail = xmit_tail; 18803 tcp->tcp_xmit_tail_unsent = tail_unsent; 18804 len = tcp->tcp_snxt - snxt; 18805 if (len) { 18806 /* 18807 * If new data was sent, need to update the notsack 18808 * list, which is, afterall, data blocks that have 18809 * not been sack'ed by the receiver. New data is 18810 * not sack'ed. 18811 */ 18812 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 18813 /* len is a negative value. */ 18814 tcp->tcp_pipe -= len; 18815 tcp_notsack_update(&(tcp->tcp_notsack_list), 18816 tcp->tcp_snxt, snxt, 18817 &(tcp->tcp_num_notsack_blk), 18818 &(tcp->tcp_cnt_notsack_list)); 18819 } 18820 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 18821 tcp->tcp_rack = tcp->tcp_rnxt; 18822 tcp->tcp_rack_cnt = 0; 18823 if ((snxt + len) == tcp->tcp_suna) { 18824 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18825 } 18826 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 18827 /* 18828 * Didn't send anything. Make sure the timer is running 18829 * so that we will probe a zero window. 18830 */ 18831 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18832 } 18833 /* Note that len is the amount we just sent but with a negative sign */ 18834 tcp->tcp_unsent += len; 18835 if (tcp->tcp_flow_stopped) { 18836 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18837 tcp_clrqfull(tcp); 18838 } 18839 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 18840 tcp_setqfull(tcp); 18841 } 18842 } 18843 18844 /* 18845 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 18846 * outgoing TCP header with the template header, as well as other 18847 * options such as time-stamp, ECN and/or SACK. 18848 */ 18849 static void 18850 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 18851 { 18852 tcph_t *tcp_tmpl, *tcp_h; 18853 uint32_t *dst, *src; 18854 int hdrlen; 18855 18856 ASSERT(OK_32PTR(rptr)); 18857 18858 /* Template header */ 18859 tcp_tmpl = tcp->tcp_tcph; 18860 18861 /* Header of outgoing packet */ 18862 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18863 18864 /* dst and src are opaque 32-bit fields, used for copying */ 18865 dst = (uint32_t *)rptr; 18866 src = (uint32_t *)tcp->tcp_iphc; 18867 hdrlen = tcp->tcp_hdr_len; 18868 18869 /* Fill time-stamp option if needed */ 18870 if (tcp->tcp_snd_ts_ok) { 18871 U32_TO_BE32((uint32_t)now, 18872 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 18873 U32_TO_BE32(tcp->tcp_ts_recent, 18874 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 18875 } else { 18876 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18877 } 18878 18879 /* 18880 * Copy the template header; is this really more efficient than 18881 * calling bcopy()? For simple IPv4/TCP, it may be the case, 18882 * but perhaps not for other scenarios. 18883 */ 18884 dst[0] = src[0]; 18885 dst[1] = src[1]; 18886 dst[2] = src[2]; 18887 dst[3] = src[3]; 18888 dst[4] = src[4]; 18889 dst[5] = src[5]; 18890 dst[6] = src[6]; 18891 dst[7] = src[7]; 18892 dst[8] = src[8]; 18893 dst[9] = src[9]; 18894 if (hdrlen -= 40) { 18895 hdrlen >>= 2; 18896 dst += 10; 18897 src += 10; 18898 do { 18899 *dst++ = *src++; 18900 } while (--hdrlen); 18901 } 18902 18903 /* 18904 * Set the ECN info in the TCP header if it is not a zero 18905 * window probe. Zero window probe is only sent in 18906 * tcp_wput_data() and tcp_timer(). 18907 */ 18908 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 18909 SET_ECT(tcp, rptr); 18910 18911 if (tcp->tcp_ecn_echo_on) 18912 tcp_h->th_flags[0] |= TH_ECE; 18913 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 18914 tcp_h->th_flags[0] |= TH_CWR; 18915 tcp->tcp_ecn_cwr_sent = B_TRUE; 18916 } 18917 } 18918 18919 /* Fill in SACK options */ 18920 if (num_sack_blk > 0) { 18921 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 18922 sack_blk_t *tmp; 18923 int32_t i; 18924 18925 wptr[0] = TCPOPT_NOP; 18926 wptr[1] = TCPOPT_NOP; 18927 wptr[2] = TCPOPT_SACK; 18928 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 18929 sizeof (sack_blk_t); 18930 wptr += TCPOPT_REAL_SACK_LEN; 18931 18932 tmp = tcp->tcp_sack_list; 18933 for (i = 0; i < num_sack_blk; i++) { 18934 U32_TO_BE32(tmp[i].begin, wptr); 18935 wptr += sizeof (tcp_seq); 18936 U32_TO_BE32(tmp[i].end, wptr); 18937 wptr += sizeof (tcp_seq); 18938 } 18939 tcp_h->th_offset_and_rsrvd[0] += 18940 ((num_sack_blk * 2 + 1) << 4); 18941 } 18942 } 18943 18944 /* 18945 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 18946 * the destination address and SAP attribute, and if necessary, the 18947 * hardware checksum offload attribute to a Multidata message. 18948 */ 18949 static int 18950 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 18951 const uint32_t start, const uint32_t stuff, const uint32_t end, 18952 const uint32_t flags) 18953 { 18954 /* Add global destination address & SAP attribute */ 18955 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 18956 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 18957 "destination address+SAP\n")); 18958 18959 if (dlmp != NULL) 18960 TCP_STAT(tcp_mdt_allocfail); 18961 return (-1); 18962 } 18963 18964 /* Add global hwcksum attribute */ 18965 if (hwcksum && 18966 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 18967 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 18968 "checksum attribute\n")); 18969 18970 TCP_STAT(tcp_mdt_allocfail); 18971 return (-1); 18972 } 18973 18974 return (0); 18975 } 18976 18977 /* 18978 * Smaller and private version of pdescinfo_t used specifically for TCP, 18979 * which allows for only two payload spans per packet. 18980 */ 18981 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 18982 18983 /* 18984 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 18985 * scheme, and returns one the following: 18986 * 18987 * -1 = failed allocation. 18988 * 0 = success; burst count reached, or usable send window is too small, 18989 * and that we'd rather wait until later before sending again. 18990 */ 18991 static int 18992 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 18993 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 18994 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 18995 const int mdt_thres) 18996 { 18997 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 18998 multidata_t *mmd; 18999 uint_t obsegs, obbytes, hdr_frag_sz; 19000 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 19001 int num_burst_seg, max_pld; 19002 pdesc_t *pkt; 19003 tcp_pdescinfo_t tcp_pkt_info; 19004 pdescinfo_t *pkt_info; 19005 int pbuf_idx, pbuf_idx_nxt; 19006 int seg_len, len, spill, af; 19007 boolean_t add_buffer, zcopy, clusterwide; 19008 boolean_t rconfirm = B_FALSE; 19009 boolean_t done = B_FALSE; 19010 uint32_t cksum; 19011 uint32_t hwcksum_flags; 19012 ire_t *ire; 19013 ill_t *ill; 19014 ipha_t *ipha; 19015 ip6_t *ip6h; 19016 ipaddr_t src, dst; 19017 ill_zerocopy_capab_t *zc_cap = NULL; 19018 uint16_t *up; 19019 int err; 19020 conn_t *connp; 19021 19022 #ifdef _BIG_ENDIAN 19023 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 19024 #else 19025 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 19026 #endif 19027 19028 #define PREP_NEW_MULTIDATA() { \ 19029 mmd = NULL; \ 19030 md_mp = md_hbuf = NULL; \ 19031 cur_hdr_off = 0; \ 19032 max_pld = tcp->tcp_mdt_max_pld; \ 19033 pbuf_idx = pbuf_idx_nxt = -1; \ 19034 add_buffer = B_TRUE; \ 19035 zcopy = B_FALSE; \ 19036 } 19037 19038 #define PREP_NEW_PBUF() { \ 19039 md_pbuf = md_pbuf_nxt = NULL; \ 19040 pbuf_idx = pbuf_idx_nxt = -1; \ 19041 cur_pld_off = 0; \ 19042 first_snxt = *snxt; \ 19043 ASSERT(*tail_unsent > 0); \ 19044 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 19045 } 19046 19047 ASSERT(mdt_thres >= mss); 19048 ASSERT(*usable > 0 && *usable > mdt_thres); 19049 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 19050 ASSERT(!TCP_IS_DETACHED(tcp)); 19051 ASSERT(tcp->tcp_valid_bits == 0 || 19052 tcp->tcp_valid_bits == TCP_FSS_VALID); 19053 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 19054 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 19055 (tcp->tcp_ipversion == IPV6_VERSION && 19056 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 19057 19058 connp = tcp->tcp_connp; 19059 ASSERT(connp != NULL); 19060 ASSERT(CONN_IS_MD_FASTPATH(connp)); 19061 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(connp)); 19062 19063 /* 19064 * Note that tcp will only declare at most 2 payload spans per 19065 * packet, which is much lower than the maximum allowable number 19066 * of packet spans per Multidata. For this reason, we use the 19067 * privately declared and smaller descriptor info structure, in 19068 * order to save some stack space. 19069 */ 19070 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 19071 19072 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 19073 if (af == AF_INET) { 19074 dst = tcp->tcp_ipha->ipha_dst; 19075 src = tcp->tcp_ipha->ipha_src; 19076 ASSERT(!CLASSD(dst)); 19077 } 19078 ASSERT(af == AF_INET || 19079 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 19080 19081 obsegs = obbytes = 0; 19082 num_burst_seg = tcp->tcp_snd_burst; 19083 md_mp_head = NULL; 19084 PREP_NEW_MULTIDATA(); 19085 19086 /* 19087 * Before we go on further, make sure there is an IRE that we can 19088 * use, and that the ILL supports MDT. Otherwise, there's no point 19089 * in proceeding any further, and we should just hand everything 19090 * off to the legacy path. 19091 */ 19092 mutex_enter(&connp->conn_lock); 19093 ire = connp->conn_ire_cache; 19094 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 19095 if (ire != NULL && ((af == AF_INET && ire->ire_addr == dst) || 19096 (af == AF_INET6 && IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, 19097 &tcp->tcp_ip6h->ip6_dst))) && 19098 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19099 IRE_REFHOLD(ire); 19100 mutex_exit(&connp->conn_lock); 19101 } else { 19102 boolean_t cached = B_FALSE; 19103 ts_label_t *tsl; 19104 19105 /* force a recheck later on */ 19106 tcp->tcp_ire_ill_check_done = B_FALSE; 19107 19108 TCP_DBGSTAT(tcp_ire_null1); 19109 connp->conn_ire_cache = NULL; 19110 mutex_exit(&connp->conn_lock); 19111 19112 /* Release the old ire */ 19113 if (ire != NULL) 19114 IRE_REFRELE_NOTR(ire); 19115 19116 tsl = crgetlabel(CONN_CRED(connp)); 19117 ire = (af == AF_INET) ? 19118 ire_cache_lookup(dst, connp->conn_zoneid, tsl) : 19119 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 19120 connp->conn_zoneid, tsl); 19121 19122 if (ire == NULL) { 19123 TCP_STAT(tcp_ire_null); 19124 goto legacy_send_no_md; 19125 } 19126 19127 IRE_REFHOLD_NOTR(ire); 19128 /* 19129 * Since we are inside the squeue, there cannot be another 19130 * thread in TCP trying to set the conn_ire_cache now. The 19131 * check for IRE_MARK_CONDEMNED ensures that an interface 19132 * unplumb thread has not yet started cleaning up the conns. 19133 * Hence we don't need to grab the conn lock. 19134 */ 19135 if (!(connp->conn_state_flags & CONN_CLOSING)) { 19136 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 19137 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 19138 connp->conn_ire_cache = ire; 19139 cached = B_TRUE; 19140 } 19141 rw_exit(&ire->ire_bucket->irb_lock); 19142 } 19143 19144 /* 19145 * We can continue to use the ire but since it was not 19146 * cached, we should drop the extra reference. 19147 */ 19148 if (!cached) 19149 IRE_REFRELE_NOTR(ire); 19150 } 19151 19152 ASSERT(ire != NULL); 19153 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 19154 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 19155 ASSERT(af == AF_INET || ire->ire_nce != NULL); 19156 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 19157 /* 19158 * If we do support loopback for MDT (which requires modifications 19159 * to the receiving paths), the following assertions should go away, 19160 * and we would be sending the Multidata to loopback conn later on. 19161 */ 19162 ASSERT(!IRE_IS_LOCAL(ire)); 19163 ASSERT(ire->ire_stq != NULL); 19164 19165 ill = ire_to_ill(ire); 19166 ASSERT(ill != NULL); 19167 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 19168 19169 if (!tcp->tcp_ire_ill_check_done) { 19170 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 19171 tcp->tcp_ire_ill_check_done = B_TRUE; 19172 } 19173 19174 /* 19175 * If the underlying interface conditions have changed, or if the 19176 * new interface does not support MDT, go back to legacy path. 19177 */ 19178 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 19179 /* don't go through this path anymore for this connection */ 19180 TCP_STAT(tcp_mdt_conn_halted2); 19181 tcp->tcp_mdt = B_FALSE; 19182 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 19183 "interface %s\n", (void *)connp, ill->ill_name)); 19184 /* IRE will be released prior to returning */ 19185 goto legacy_send_no_md; 19186 } 19187 19188 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 19189 zc_cap = ill->ill_zerocopy_capab; 19190 19191 /* go to legacy path if interface doesn't support zerocopy */ 19192 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 19193 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 19194 /* IRE will be released prior to returning */ 19195 goto legacy_send_no_md; 19196 } 19197 19198 /* does the interface support hardware checksum offload? */ 19199 hwcksum_flags = 0; 19200 if (ILL_HCKSUM_CAPABLE(ill) && 19201 (ill->ill_hcksum_capab->ill_hcksum_txflags & 19202 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 19203 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 19204 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19205 HCKSUM_IPHDRCKSUM) 19206 hwcksum_flags = HCK_IPV4_HDRCKSUM; 19207 19208 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19209 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 19210 hwcksum_flags |= HCK_FULLCKSUM; 19211 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 19212 HCKSUM_INET_PARTIAL) 19213 hwcksum_flags |= HCK_PARTIALCKSUM; 19214 } 19215 19216 /* 19217 * Each header fragment consists of the leading extra space, 19218 * followed by the TCP/IP header, and the trailing extra space. 19219 * We make sure that each header fragment begins on a 32-bit 19220 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 19221 * aligned in tcp_mdt_update). 19222 */ 19223 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 19224 tcp->tcp_mdt_hdr_tail), 4); 19225 19226 /* are we starting from the beginning of data block? */ 19227 if (*tail_unsent == 0) { 19228 *xmit_tail = (*xmit_tail)->b_cont; 19229 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 19230 *tail_unsent = (int)MBLKL(*xmit_tail); 19231 } 19232 19233 /* 19234 * Here we create one or more Multidata messages, each made up of 19235 * one header buffer and up to N payload buffers. This entire 19236 * operation is done within two loops: 19237 * 19238 * The outer loop mostly deals with creating the Multidata message, 19239 * as well as the header buffer that gets added to it. It also 19240 * links the Multidata messages together such that all of them can 19241 * be sent down to the lower layer in a single putnext call; this 19242 * linking behavior depends on the tcp_mdt_chain tunable. 19243 * 19244 * The inner loop takes an existing Multidata message, and adds 19245 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 19246 * packetizes those buffers by filling up the corresponding header 19247 * buffer fragments with the proper IP and TCP headers, and by 19248 * describing the layout of each packet in the packet descriptors 19249 * that get added to the Multidata. 19250 */ 19251 do { 19252 /* 19253 * If usable send window is too small, or data blocks in 19254 * transmit list are smaller than our threshold (i.e. app 19255 * performs large writes followed by small ones), we hand 19256 * off the control over to the legacy path. Note that we'll 19257 * get back the control once it encounters a large block. 19258 */ 19259 if (*usable < mss || (*tail_unsent <= mdt_thres && 19260 (*xmit_tail)->b_cont != NULL && 19261 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 19262 /* send down what we've got so far */ 19263 if (md_mp_head != NULL) { 19264 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19265 obsegs, obbytes, &rconfirm); 19266 } 19267 /* 19268 * Pass control over to tcp_send(), but tell it to 19269 * return to us once a large-size transmission is 19270 * possible. 19271 */ 19272 TCP_STAT(tcp_mdt_legacy_small); 19273 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19274 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19275 tail_unsent, xmit_tail, local_time, 19276 mdt_thres)) <= 0) { 19277 /* burst count reached, or alloc failed */ 19278 IRE_REFRELE(ire); 19279 return (err); 19280 } 19281 19282 /* tcp_send() may have sent everything, so check */ 19283 if (*usable <= 0) { 19284 IRE_REFRELE(ire); 19285 return (0); 19286 } 19287 19288 TCP_STAT(tcp_mdt_legacy_ret); 19289 /* 19290 * We may have delivered the Multidata, so make sure 19291 * to re-initialize before the next round. 19292 */ 19293 md_mp_head = NULL; 19294 obsegs = obbytes = 0; 19295 num_burst_seg = tcp->tcp_snd_burst; 19296 PREP_NEW_MULTIDATA(); 19297 19298 /* are we starting from the beginning of data block? */ 19299 if (*tail_unsent == 0) { 19300 *xmit_tail = (*xmit_tail)->b_cont; 19301 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19302 (uintptr_t)INT_MAX); 19303 *tail_unsent = (int)MBLKL(*xmit_tail); 19304 } 19305 } 19306 19307 /* 19308 * max_pld limits the number of mblks in tcp's transmit 19309 * queue that can be added to a Multidata message. Once 19310 * this counter reaches zero, no more additional mblks 19311 * can be added to it. What happens afterwards depends 19312 * on whether or not we are set to chain the Multidata 19313 * messages. If we are to link them together, reset 19314 * max_pld to its original value (tcp_mdt_max_pld) and 19315 * prepare to create a new Multidata message which will 19316 * get linked to md_mp_head. Else, leave it alone and 19317 * let the inner loop break on its own. 19318 */ 19319 if (tcp_mdt_chain && max_pld == 0) 19320 PREP_NEW_MULTIDATA(); 19321 19322 /* adding a payload buffer; re-initialize values */ 19323 if (add_buffer) 19324 PREP_NEW_PBUF(); 19325 19326 /* 19327 * If we don't have a Multidata, either because we just 19328 * (re)entered this outer loop, or after we branched off 19329 * to tcp_send above, setup the Multidata and header 19330 * buffer to be used. 19331 */ 19332 if (md_mp == NULL) { 19333 int md_hbuflen; 19334 uint32_t start, stuff; 19335 19336 /* 19337 * Calculate Multidata header buffer size large enough 19338 * to hold all of the headers that can possibly be 19339 * sent at this moment. We'd rather over-estimate 19340 * the size than running out of space; this is okay 19341 * since this buffer is small anyway. 19342 */ 19343 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19344 19345 /* 19346 * Start and stuff offset for partial hardware 19347 * checksum offload; these are currently for IPv4. 19348 * For full checksum offload, they are set to zero. 19349 */ 19350 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19351 if (af == AF_INET) { 19352 start = IP_SIMPLE_HDR_LENGTH; 19353 stuff = IP_SIMPLE_HDR_LENGTH + 19354 TCP_CHECKSUM_OFFSET; 19355 } else { 19356 start = IPV6_HDR_LEN; 19357 stuff = IPV6_HDR_LEN + 19358 TCP_CHECKSUM_OFFSET; 19359 } 19360 } else { 19361 start = stuff = 0; 19362 } 19363 19364 /* 19365 * Create the header buffer, Multidata, as well as 19366 * any necessary attributes (destination address, 19367 * SAP and hardware checksum offload) that should 19368 * be associated with the Multidata message. 19369 */ 19370 ASSERT(cur_hdr_off == 0); 19371 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19372 ((md_hbuf->b_wptr += md_hbuflen), 19373 (mmd = mmd_alloc(md_hbuf, &md_mp, 19374 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19375 /* fastpath mblk */ 19376 (af == AF_INET) ? ire->ire_dlureq_mp : 19377 ire->ire_nce->nce_res_mp, 19378 /* hardware checksum enabled */ 19379 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19380 /* hardware checksum offsets */ 19381 start, stuff, 0, 19382 /* hardware checksum flag */ 19383 hwcksum_flags) != 0)) { 19384 legacy_send: 19385 if (md_mp != NULL) { 19386 /* Unlink message from the chain */ 19387 if (md_mp_head != NULL) { 19388 err = (intptr_t)rmvb(md_mp_head, 19389 md_mp); 19390 /* 19391 * We can't assert that rmvb 19392 * did not return -1, since we 19393 * may get here before linkb 19394 * happens. We do, however, 19395 * check if we just removed the 19396 * only element in the list. 19397 */ 19398 if (err == 0) 19399 md_mp_head = NULL; 19400 } 19401 /* md_hbuf gets freed automatically */ 19402 TCP_STAT(tcp_mdt_discarded); 19403 freeb(md_mp); 19404 } else { 19405 /* Either allocb or mmd_alloc failed */ 19406 TCP_STAT(tcp_mdt_allocfail); 19407 if (md_hbuf != NULL) 19408 freeb(md_hbuf); 19409 } 19410 19411 /* send down what we've got so far */ 19412 if (md_mp_head != NULL) { 19413 tcp_multisend_data(tcp, ire, ill, 19414 md_mp_head, obsegs, obbytes, 19415 &rconfirm); 19416 } 19417 legacy_send_no_md: 19418 if (ire != NULL) 19419 IRE_REFRELE(ire); 19420 /* 19421 * Too bad; let the legacy path handle this. 19422 * We specify INT_MAX for the threshold, since 19423 * we gave up with the Multidata processings 19424 * and let the old path have it all. 19425 */ 19426 TCP_STAT(tcp_mdt_legacy_all); 19427 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19428 tcp_tcp_hdr_len, num_sack_blk, usable, 19429 snxt, tail_unsent, xmit_tail, local_time, 19430 INT_MAX)); 19431 } 19432 19433 /* link to any existing ones, if applicable */ 19434 TCP_STAT(tcp_mdt_allocd); 19435 if (md_mp_head == NULL) { 19436 md_mp_head = md_mp; 19437 } else if (tcp_mdt_chain) { 19438 TCP_STAT(tcp_mdt_linked); 19439 linkb(md_mp_head, md_mp); 19440 } 19441 } 19442 19443 ASSERT(md_mp_head != NULL); 19444 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19445 ASSERT(md_mp != NULL && mmd != NULL); 19446 ASSERT(md_hbuf != NULL); 19447 19448 /* 19449 * Packetize the transmittable portion of the data block; 19450 * each data block is essentially added to the Multidata 19451 * as a payload buffer. We also deal with adding more 19452 * than one payload buffers, which happens when the remaining 19453 * packetized portion of the current payload buffer is less 19454 * than MSS, while the next data block in transmit queue 19455 * has enough data to make up for one. This "spillover" 19456 * case essentially creates a split-packet, where portions 19457 * of the packet's payload fragments may span across two 19458 * virtually discontiguous address blocks. 19459 */ 19460 seg_len = mss; 19461 do { 19462 len = seg_len; 19463 19464 ASSERT(len > 0); 19465 ASSERT(max_pld >= 0); 19466 ASSERT(!add_buffer || cur_pld_off == 0); 19467 19468 /* 19469 * First time around for this payload buffer; note 19470 * in the case of a spillover, the following has 19471 * been done prior to adding the split-packet 19472 * descriptor to Multidata, and we don't want to 19473 * repeat the process. 19474 */ 19475 if (add_buffer) { 19476 ASSERT(mmd != NULL); 19477 ASSERT(md_pbuf == NULL); 19478 ASSERT(md_pbuf_nxt == NULL); 19479 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19480 19481 /* 19482 * Have we reached the limit? We'd get to 19483 * this case when we're not chaining the 19484 * Multidata messages together, and since 19485 * we're done, terminate this loop. 19486 */ 19487 if (max_pld == 0) 19488 break; /* done */ 19489 19490 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19491 TCP_STAT(tcp_mdt_allocfail); 19492 goto legacy_send; /* out_of_mem */ 19493 } 19494 19495 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19496 zc_cap != NULL) { 19497 if (!ip_md_zcopy_attr(mmd, NULL, 19498 zc_cap->ill_zerocopy_flags)) { 19499 freeb(md_pbuf); 19500 TCP_STAT(tcp_mdt_allocfail); 19501 /* out_of_mem */ 19502 goto legacy_send; 19503 } 19504 zcopy = B_TRUE; 19505 } 19506 19507 md_pbuf->b_rptr += base_pld_off; 19508 19509 /* 19510 * Add a payload buffer to the Multidata; this 19511 * operation must not fail, or otherwise our 19512 * logic in this routine is broken. There 19513 * is no memory allocation done by the 19514 * routine, so any returned failure simply 19515 * tells us that we've done something wrong. 19516 * 19517 * A failure tells us that either we're adding 19518 * the same payload buffer more than once, or 19519 * we're trying to add more buffers than 19520 * allowed (max_pld calculation is wrong). 19521 * None of the above cases should happen, and 19522 * we panic because either there's horrible 19523 * heap corruption, and/or programming mistake. 19524 */ 19525 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19526 if (pbuf_idx < 0) { 19527 cmn_err(CE_PANIC, "tcp_multisend: " 19528 "payload buffer logic error " 19529 "detected for tcp %p mmd %p " 19530 "pbuf %p (%d)\n", 19531 (void *)tcp, (void *)mmd, 19532 (void *)md_pbuf, pbuf_idx); 19533 } 19534 19535 ASSERT(max_pld > 0); 19536 --max_pld; 19537 add_buffer = B_FALSE; 19538 } 19539 19540 ASSERT(md_mp_head != NULL); 19541 ASSERT(md_pbuf != NULL); 19542 ASSERT(md_pbuf_nxt == NULL); 19543 ASSERT(pbuf_idx != -1); 19544 ASSERT(pbuf_idx_nxt == -1); 19545 ASSERT(*usable > 0); 19546 19547 /* 19548 * We spillover to the next payload buffer only 19549 * if all of the following is true: 19550 * 19551 * 1. There is not enough data on the current 19552 * payload buffer to make up `len', 19553 * 2. We are allowed to send `len', 19554 * 3. The next payload buffer length is large 19555 * enough to accomodate `spill'. 19556 */ 19557 if ((spill = len - *tail_unsent) > 0 && 19558 *usable >= len && 19559 MBLKL((*xmit_tail)->b_cont) >= spill && 19560 max_pld > 0) { 19561 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19562 if (md_pbuf_nxt == NULL) { 19563 TCP_STAT(tcp_mdt_allocfail); 19564 goto legacy_send; /* out_of_mem */ 19565 } 19566 19567 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19568 zc_cap != NULL) { 19569 if (!ip_md_zcopy_attr(mmd, NULL, 19570 zc_cap->ill_zerocopy_flags)) { 19571 freeb(md_pbuf_nxt); 19572 TCP_STAT(tcp_mdt_allocfail); 19573 /* out_of_mem */ 19574 goto legacy_send; 19575 } 19576 zcopy = B_TRUE; 19577 } 19578 19579 /* 19580 * See comments above on the first call to 19581 * mmd_addpldbuf for explanation on the panic. 19582 */ 19583 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19584 if (pbuf_idx_nxt < 0) { 19585 panic("tcp_multisend: " 19586 "next payload buffer logic error " 19587 "detected for tcp %p mmd %p " 19588 "pbuf %p (%d)\n", 19589 (void *)tcp, (void *)mmd, 19590 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19591 } 19592 19593 ASSERT(max_pld > 0); 19594 --max_pld; 19595 } else if (spill > 0) { 19596 /* 19597 * If there's a spillover, but the following 19598 * xmit_tail couldn't give us enough octets 19599 * to reach "len", then stop the current 19600 * Multidata creation and let the legacy 19601 * tcp_send() path take over. We don't want 19602 * to send the tiny segment as part of this 19603 * Multidata for performance reasons; instead, 19604 * we let the legacy path deal with grouping 19605 * it with the subsequent small mblks. 19606 */ 19607 if (*usable >= len && 19608 MBLKL((*xmit_tail)->b_cont) < spill) { 19609 max_pld = 0; 19610 break; /* done */ 19611 } 19612 19613 /* 19614 * We can't spillover, and we are near 19615 * the end of the current payload buffer, 19616 * so send what's left. 19617 */ 19618 ASSERT(*tail_unsent > 0); 19619 len = *tail_unsent; 19620 } 19621 19622 /* tail_unsent is negated if there is a spillover */ 19623 *tail_unsent -= len; 19624 *usable -= len; 19625 ASSERT(*usable >= 0); 19626 19627 if (*usable < mss) 19628 seg_len = *usable; 19629 /* 19630 * Sender SWS avoidance; see comments in tcp_send(); 19631 * everything else is the same, except that we only 19632 * do this here if there is no more data to be sent 19633 * following the current xmit_tail. We don't check 19634 * for 1-byte urgent data because we shouldn't get 19635 * here if TCP_URG_VALID is set. 19636 */ 19637 if (*usable > 0 && *usable < mss && 19638 ((md_pbuf_nxt == NULL && 19639 (*xmit_tail)->b_cont == NULL) || 19640 (md_pbuf_nxt != NULL && 19641 (*xmit_tail)->b_cont->b_cont == NULL)) && 19642 seg_len < (tcp->tcp_max_swnd >> 1) && 19643 (tcp->tcp_unsent - 19644 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19645 !tcp->tcp_zero_win_probe) { 19646 if ((*snxt + len) == tcp->tcp_snxt && 19647 (*snxt + len) == tcp->tcp_suna) { 19648 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19649 } 19650 done = B_TRUE; 19651 } 19652 19653 /* 19654 * Prime pump for IP's checksumming on our behalf; 19655 * include the adjustment for a source route if any. 19656 * Do this only for software/partial hardware checksum 19657 * offload, as this field gets zeroed out later for 19658 * the full hardware checksum offload case. 19659 */ 19660 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19661 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19662 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19663 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19664 } 19665 19666 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19667 *snxt += len; 19668 19669 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19670 /* 19671 * We set the PUSH bit only if TCP has no more buffered 19672 * data to be transmitted (or if sender SWS avoidance 19673 * takes place), as opposed to setting it for every 19674 * last packet in the burst. 19675 */ 19676 if (done || 19677 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19678 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19679 19680 /* 19681 * Set FIN bit if this is our last segment; snxt 19682 * already includes its length, and it will not 19683 * be adjusted after this point. 19684 */ 19685 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 19686 *snxt == tcp->tcp_fss) { 19687 if (!tcp->tcp_fin_acked) { 19688 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 19689 BUMP_MIB(&tcp_mib, tcpOutControl); 19690 } 19691 if (!tcp->tcp_fin_sent) { 19692 tcp->tcp_fin_sent = B_TRUE; 19693 /* 19694 * tcp state must be ESTABLISHED 19695 * in order for us to get here in 19696 * the first place. 19697 */ 19698 tcp->tcp_state = TCPS_FIN_WAIT_1; 19699 19700 /* 19701 * Upon returning from this routine, 19702 * tcp_wput_data() will set tcp_snxt 19703 * to be equal to snxt + tcp_fin_sent. 19704 * This is essentially the same as 19705 * setting it to tcp_fss + 1. 19706 */ 19707 } 19708 } 19709 19710 tcp->tcp_last_sent_len = (ushort_t)len; 19711 19712 len += tcp_hdr_len; 19713 if (tcp->tcp_ipversion == IPV4_VERSION) 19714 tcp->tcp_ipha->ipha_length = htons(len); 19715 else 19716 tcp->tcp_ip6h->ip6_plen = htons(len - 19717 ((char *)&tcp->tcp_ip6h[1] - 19718 tcp->tcp_iphc)); 19719 19720 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 19721 19722 /* setup header fragment */ 19723 PDESC_HDR_ADD(pkt_info, 19724 md_hbuf->b_rptr + cur_hdr_off, /* base */ 19725 tcp->tcp_mdt_hdr_head, /* head room */ 19726 tcp_hdr_len, /* len */ 19727 tcp->tcp_mdt_hdr_tail); /* tail room */ 19728 19729 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 19730 hdr_frag_sz); 19731 ASSERT(MBLKIN(md_hbuf, 19732 (pkt_info->hdr_base - md_hbuf->b_rptr), 19733 PDESC_HDRSIZE(pkt_info))); 19734 19735 /* setup first payload fragment */ 19736 PDESC_PLD_INIT(pkt_info); 19737 PDESC_PLD_SPAN_ADD(pkt_info, 19738 pbuf_idx, /* index */ 19739 md_pbuf->b_rptr + cur_pld_off, /* start */ 19740 tcp->tcp_last_sent_len); /* len */ 19741 19742 /* create a split-packet in case of a spillover */ 19743 if (md_pbuf_nxt != NULL) { 19744 ASSERT(spill > 0); 19745 ASSERT(pbuf_idx_nxt > pbuf_idx); 19746 ASSERT(!add_buffer); 19747 19748 md_pbuf = md_pbuf_nxt; 19749 md_pbuf_nxt = NULL; 19750 pbuf_idx = pbuf_idx_nxt; 19751 pbuf_idx_nxt = -1; 19752 cur_pld_off = spill; 19753 19754 /* trim out first payload fragment */ 19755 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 19756 19757 /* setup second payload fragment */ 19758 PDESC_PLD_SPAN_ADD(pkt_info, 19759 pbuf_idx, /* index */ 19760 md_pbuf->b_rptr, /* start */ 19761 spill); /* len */ 19762 19763 if ((*xmit_tail)->b_next == NULL) { 19764 /* 19765 * Store the lbolt used for RTT 19766 * estimation. We can only record one 19767 * timestamp per mblk so we do it when 19768 * we reach the end of the payload 19769 * buffer. Also we only take a new 19770 * timestamp sample when the previous 19771 * timed data from the same mblk has 19772 * been ack'ed. 19773 */ 19774 (*xmit_tail)->b_prev = local_time; 19775 (*xmit_tail)->b_next = 19776 (mblk_t *)(uintptr_t)first_snxt; 19777 } 19778 19779 first_snxt = *snxt - spill; 19780 19781 /* 19782 * Advance xmit_tail; usable could be 0 by 19783 * the time we got here, but we made sure 19784 * above that we would only spillover to 19785 * the next data block if usable includes 19786 * the spilled-over amount prior to the 19787 * subtraction. Therefore, we are sure 19788 * that xmit_tail->b_cont can't be NULL. 19789 */ 19790 ASSERT((*xmit_tail)->b_cont != NULL); 19791 *xmit_tail = (*xmit_tail)->b_cont; 19792 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19793 (uintptr_t)INT_MAX); 19794 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 19795 } else { 19796 cur_pld_off += tcp->tcp_last_sent_len; 19797 } 19798 19799 /* 19800 * Fill in the header using the template header, and 19801 * add options such as time-stamp, ECN and/or SACK, 19802 * as needed. 19803 */ 19804 tcp_fill_header(tcp, pkt_info->hdr_rptr, 19805 (clock_t)local_time, num_sack_blk); 19806 19807 /* take care of some IP header businesses */ 19808 if (af == AF_INET) { 19809 ipha = (ipha_t *)pkt_info->hdr_rptr; 19810 19811 ASSERT(OK_32PTR((uchar_t *)ipha)); 19812 ASSERT(PDESC_HDRL(pkt_info) >= 19813 IP_SIMPLE_HDR_LENGTH); 19814 ASSERT(ipha->ipha_version_and_hdr_length == 19815 IP_SIMPLE_HDR_VERSION); 19816 19817 /* 19818 * Assign ident value for current packet; see 19819 * related comments in ip_wput_ire() about the 19820 * contract private interface with clustering 19821 * group. 19822 */ 19823 clusterwide = B_FALSE; 19824 if (cl_inet_ipident != NULL) { 19825 ASSERT(cl_inet_isclusterwide != NULL); 19826 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 19827 AF_INET, 19828 (uint8_t *)(uintptr_t)src)) { 19829 ipha->ipha_ident = 19830 (*cl_inet_ipident) 19831 (IPPROTO_IP, AF_INET, 19832 (uint8_t *)(uintptr_t)src, 19833 (uint8_t *)(uintptr_t)dst); 19834 clusterwide = B_TRUE; 19835 } 19836 } 19837 19838 if (!clusterwide) { 19839 ipha->ipha_ident = (uint16_t) 19840 atomic_add_32_nv( 19841 &ire->ire_ident, 1); 19842 } 19843 #ifndef _BIG_ENDIAN 19844 ipha->ipha_ident = (ipha->ipha_ident << 8) | 19845 (ipha->ipha_ident >> 8); 19846 #endif 19847 } else { 19848 ip6h = (ip6_t *)pkt_info->hdr_rptr; 19849 19850 ASSERT(OK_32PTR((uchar_t *)ip6h)); 19851 ASSERT(IPVER(ip6h) == IPV6_VERSION); 19852 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 19853 ASSERT(PDESC_HDRL(pkt_info) >= 19854 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 19855 TCP_CHECKSUM_SIZE)); 19856 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 19857 19858 if (tcp->tcp_ip_forward_progress) { 19859 rconfirm = B_TRUE; 19860 tcp->tcp_ip_forward_progress = B_FALSE; 19861 } 19862 } 19863 19864 /* at least one payload span, and at most two */ 19865 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 19866 19867 /* add the packet descriptor to Multidata */ 19868 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 19869 KM_NOSLEEP)) == NULL) { 19870 /* 19871 * Any failure other than ENOMEM indicates 19872 * that we have passed in invalid pkt_info 19873 * or parameters to mmd_addpdesc, which must 19874 * not happen. 19875 * 19876 * EINVAL is a result of failure on boundary 19877 * checks against the pkt_info contents. It 19878 * should not happen, and we panic because 19879 * either there's horrible heap corruption, 19880 * and/or programming mistake. 19881 */ 19882 if (err != ENOMEM) { 19883 cmn_err(CE_PANIC, "tcp_multisend: " 19884 "pdesc logic error detected for " 19885 "tcp %p mmd %p pinfo %p (%d)\n", 19886 (void *)tcp, (void *)mmd, 19887 (void *)pkt_info, err); 19888 } 19889 TCP_STAT(tcp_mdt_addpdescfail); 19890 goto legacy_send; /* out_of_mem */ 19891 } 19892 ASSERT(pkt != NULL); 19893 19894 /* calculate IP header and TCP checksums */ 19895 if (af == AF_INET) { 19896 /* calculate pseudo-header checksum */ 19897 cksum = (dst >> 16) + (dst & 0xFFFF) + 19898 (src >> 16) + (src & 0xFFFF); 19899 19900 /* offset for TCP header checksum */ 19901 up = IPH_TCPH_CHECKSUMP(ipha, 19902 IP_SIMPLE_HDR_LENGTH); 19903 } else { 19904 up = (uint16_t *)&ip6h->ip6_src; 19905 19906 /* calculate pseudo-header checksum */ 19907 cksum = up[0] + up[1] + up[2] + up[3] + 19908 up[4] + up[5] + up[6] + up[7] + 19909 up[8] + up[9] + up[10] + up[11] + 19910 up[12] + up[13] + up[14] + up[15]; 19911 19912 /* Fold the initial sum */ 19913 cksum = (cksum & 0xffff) + (cksum >> 16); 19914 19915 up = (uint16_t *)(((uchar_t *)ip6h) + 19916 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 19917 } 19918 19919 if (hwcksum_flags & HCK_FULLCKSUM) { 19920 /* clear checksum field for hardware */ 19921 *up = 0; 19922 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 19923 uint32_t sum; 19924 19925 /* pseudo-header checksumming */ 19926 sum = *up + cksum + IP_TCP_CSUM_COMP; 19927 sum = (sum & 0xFFFF) + (sum >> 16); 19928 *up = (sum & 0xFFFF) + (sum >> 16); 19929 } else { 19930 /* software checksumming */ 19931 TCP_STAT(tcp_out_sw_cksum); 19932 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 19933 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 19934 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 19935 cksum + IP_TCP_CSUM_COMP); 19936 if (*up == 0) 19937 *up = 0xFFFF; 19938 } 19939 19940 /* IPv4 header checksum */ 19941 if (af == AF_INET) { 19942 ipha->ipha_fragment_offset_and_flags |= 19943 (uint32_t)htons(ire->ire_frag_flag); 19944 19945 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 19946 ipha->ipha_hdr_checksum = 0; 19947 } else { 19948 IP_HDR_CKSUM(ipha, cksum, 19949 ((uint32_t *)ipha)[0], 19950 ((uint16_t *)ipha)[4]); 19951 } 19952 } 19953 19954 /* advance header offset */ 19955 cur_hdr_off += hdr_frag_sz; 19956 19957 obbytes += tcp->tcp_last_sent_len; 19958 ++obsegs; 19959 } while (!done && *usable > 0 && --num_burst_seg > 0 && 19960 *tail_unsent > 0); 19961 19962 if ((*xmit_tail)->b_next == NULL) { 19963 /* 19964 * Store the lbolt used for RTT estimation. We can only 19965 * record one timestamp per mblk so we do it when we 19966 * reach the end of the payload buffer. Also we only 19967 * take a new timestamp sample when the previous timed 19968 * data from the same mblk has been ack'ed. 19969 */ 19970 (*xmit_tail)->b_prev = local_time; 19971 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 19972 } 19973 19974 ASSERT(*tail_unsent >= 0); 19975 if (*tail_unsent > 0) { 19976 /* 19977 * We got here because we broke out of the above 19978 * loop due to of one of the following cases: 19979 * 19980 * 1. len < adjusted MSS (i.e. small), 19981 * 2. Sender SWS avoidance, 19982 * 3. max_pld is zero. 19983 * 19984 * We are done for this Multidata, so trim our 19985 * last payload buffer (if any) accordingly. 19986 */ 19987 if (md_pbuf != NULL) 19988 md_pbuf->b_wptr -= *tail_unsent; 19989 } else if (*usable > 0) { 19990 *xmit_tail = (*xmit_tail)->b_cont; 19991 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19992 (uintptr_t)INT_MAX); 19993 *tail_unsent = (int)MBLKL(*xmit_tail); 19994 add_buffer = B_TRUE; 19995 } 19996 } while (!done && *usable > 0 && num_burst_seg > 0 && 19997 (tcp_mdt_chain || max_pld > 0)); 19998 19999 /* send everything down */ 20000 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 20001 &rconfirm); 20002 20003 #undef PREP_NEW_MULTIDATA 20004 #undef PREP_NEW_PBUF 20005 #undef IPVER 20006 20007 IRE_REFRELE(ire); 20008 return (0); 20009 } 20010 20011 /* 20012 * A wrapper function for sending one or more Multidata messages down to 20013 * the module below ip; this routine does not release the reference of the 20014 * IRE (caller does that). This routine is analogous to tcp_send_data(). 20015 */ 20016 static void 20017 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 20018 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 20019 { 20020 uint64_t delta; 20021 nce_t *nce; 20022 20023 ASSERT(ire != NULL && ill != NULL); 20024 ASSERT(ire->ire_stq != NULL); 20025 ASSERT(md_mp_head != NULL); 20026 ASSERT(rconfirm != NULL); 20027 20028 /* adjust MIBs and IRE timestamp */ 20029 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 20030 tcp->tcp_obsegs += obsegs; 20031 UPDATE_MIB(&tcp_mib, tcpOutDataSegs, obsegs); 20032 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, obbytes); 20033 TCP_STAT_UPDATE(tcp_mdt_pkt_out, obsegs); 20034 20035 if (tcp->tcp_ipversion == IPV4_VERSION) { 20036 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v4, obsegs); 20037 UPDATE_MIB(&ip_mib, ipOutRequests, obsegs); 20038 } else { 20039 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v6, obsegs); 20040 UPDATE_MIB(&ip6_mib, ipv6OutRequests, obsegs); 20041 } 20042 20043 ire->ire_ob_pkt_count += obsegs; 20044 if (ire->ire_ipif != NULL) 20045 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 20046 ire->ire_last_used_time = lbolt; 20047 20048 /* send it down */ 20049 putnext(ire->ire_stq, md_mp_head); 20050 20051 /* we're done for TCP/IPv4 */ 20052 if (tcp->tcp_ipversion == IPV4_VERSION) 20053 return; 20054 20055 nce = ire->ire_nce; 20056 20057 ASSERT(nce != NULL); 20058 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 20059 ASSERT(nce->nce_state != ND_INCOMPLETE); 20060 20061 /* reachability confirmation? */ 20062 if (*rconfirm) { 20063 nce->nce_last = TICK_TO_MSEC(lbolt64); 20064 if (nce->nce_state != ND_REACHABLE) { 20065 mutex_enter(&nce->nce_lock); 20066 nce->nce_state = ND_REACHABLE; 20067 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 20068 mutex_exit(&nce->nce_lock); 20069 (void) untimeout(nce->nce_timeout_id); 20070 if (ip_debug > 2) { 20071 /* ip1dbg */ 20072 pr_addr_dbg("tcp_multisend_data: state " 20073 "for %s changed to REACHABLE\n", 20074 AF_INET6, &ire->ire_addr_v6); 20075 } 20076 } 20077 /* reset transport reachability confirmation */ 20078 *rconfirm = B_FALSE; 20079 } 20080 20081 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 20082 ip1dbg(("tcp_multisend_data: delta = %" PRId64 20083 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 20084 20085 if (delta > (uint64_t)ill->ill_reachable_time) { 20086 mutex_enter(&nce->nce_lock); 20087 switch (nce->nce_state) { 20088 case ND_REACHABLE: 20089 case ND_STALE: 20090 /* 20091 * ND_REACHABLE is identical to ND_STALE in this 20092 * specific case. If reachable time has expired for 20093 * this neighbor (delta is greater than reachable 20094 * time), conceptually, the neighbor cache is no 20095 * longer in REACHABLE state, but already in STALE 20096 * state. So the correct transition here is to 20097 * ND_DELAY. 20098 */ 20099 nce->nce_state = ND_DELAY; 20100 mutex_exit(&nce->nce_lock); 20101 NDP_RESTART_TIMER(nce, delay_first_probe_time); 20102 if (ip_debug > 3) { 20103 /* ip2dbg */ 20104 pr_addr_dbg("tcp_multisend_data: state " 20105 "for %s changed to DELAY\n", 20106 AF_INET6, &ire->ire_addr_v6); 20107 } 20108 break; 20109 case ND_DELAY: 20110 case ND_PROBE: 20111 mutex_exit(&nce->nce_lock); 20112 /* Timers have already started */ 20113 break; 20114 case ND_UNREACHABLE: 20115 /* 20116 * ndp timer has detected that this nce is 20117 * unreachable and initiated deleting this nce 20118 * and all its associated IREs. This is a race 20119 * where we found the ire before it was deleted 20120 * and have just sent out a packet using this 20121 * unreachable nce. 20122 */ 20123 mutex_exit(&nce->nce_lock); 20124 break; 20125 default: 20126 ASSERT(0); 20127 } 20128 } 20129 } 20130 20131 /* 20132 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 20133 * scheme, and returns one of the following: 20134 * 20135 * -1 = failed allocation. 20136 * 0 = success; burst count reached, or usable send window is too small, 20137 * and that we'd rather wait until later before sending again. 20138 * 1 = success; we are called from tcp_multisend(), and both usable send 20139 * window and tail_unsent are greater than the MDT threshold, and thus 20140 * Multidata Transmit should be used instead. 20141 */ 20142 static int 20143 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 20144 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 20145 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 20146 const int mdt_thres) 20147 { 20148 int num_burst_seg = tcp->tcp_snd_burst; 20149 20150 for (;;) { 20151 struct datab *db; 20152 tcph_t *tcph; 20153 uint32_t sum; 20154 mblk_t *mp, *mp1; 20155 uchar_t *rptr; 20156 int len; 20157 20158 /* 20159 * If we're called by tcp_multisend(), and the amount of 20160 * sendable data as well as the size of current xmit_tail 20161 * is beyond the MDT threshold, return to the caller and 20162 * let the large data transmit be done using MDT. 20163 */ 20164 if (*usable > 0 && *usable > mdt_thres && 20165 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 20166 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 20167 ASSERT(tcp->tcp_mdt); 20168 return (1); /* success; do large send */ 20169 } 20170 20171 if (num_burst_seg-- == 0) 20172 break; /* success; burst count reached */ 20173 20174 len = mss; 20175 if (len > *usable) { 20176 len = *usable; 20177 if (len <= 0) { 20178 /* Terminate the loop */ 20179 break; /* success; too small */ 20180 } 20181 /* 20182 * Sender silly-window avoidance. 20183 * Ignore this if we are going to send a 20184 * zero window probe out. 20185 * 20186 * TODO: force data into microscopic window? 20187 * ==> (!pushed || (unsent > usable)) 20188 */ 20189 if (len < (tcp->tcp_max_swnd >> 1) && 20190 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 20191 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 20192 len == 1) && (! tcp->tcp_zero_win_probe)) { 20193 /* 20194 * If the retransmit timer is not running 20195 * we start it so that we will retransmit 20196 * in the case when the the receiver has 20197 * decremented the window. 20198 */ 20199 if (*snxt == tcp->tcp_snxt && 20200 *snxt == tcp->tcp_suna) { 20201 /* 20202 * We are not supposed to send 20203 * anything. So let's wait a little 20204 * bit longer before breaking SWS 20205 * avoidance. 20206 * 20207 * What should the value be? 20208 * Suggestion: MAX(init rexmit time, 20209 * tcp->tcp_rto) 20210 */ 20211 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 20212 } 20213 break; /* success; too small */ 20214 } 20215 } 20216 20217 tcph = tcp->tcp_tcph; 20218 20219 *usable -= len; /* Approximate - can be adjusted later */ 20220 if (*usable > 0) 20221 tcph->th_flags[0] = TH_ACK; 20222 else 20223 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 20224 20225 /* 20226 * Prime pump for IP's checksumming on our behalf 20227 * Include the adjustment for a source route if any. 20228 */ 20229 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 20230 sum = (sum >> 16) + (sum & 0xFFFF); 20231 U16_TO_ABE16(sum, tcph->th_sum); 20232 20233 U32_TO_ABE32(*snxt, tcph->th_seq); 20234 20235 /* 20236 * Branch off to tcp_xmit_mp() if any of the VALID bits is 20237 * set. For the case when TCP_FSS_VALID is the only valid 20238 * bit (normal active close), branch off only when we think 20239 * that the FIN flag needs to be set. Note for this case, 20240 * that (snxt + len) may not reflect the actual seg_len, 20241 * as len may be further reduced in tcp_xmit_mp(). If len 20242 * gets modified, we will end up here again. 20243 */ 20244 if (tcp->tcp_valid_bits != 0 && 20245 (tcp->tcp_valid_bits != TCP_FSS_VALID || 20246 ((*snxt + len) == tcp->tcp_fss))) { 20247 uchar_t *prev_rptr; 20248 uint32_t prev_snxt = tcp->tcp_snxt; 20249 20250 if (*tail_unsent == 0) { 20251 ASSERT((*xmit_tail)->b_cont != NULL); 20252 *xmit_tail = (*xmit_tail)->b_cont; 20253 prev_rptr = (*xmit_tail)->b_rptr; 20254 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20255 (*xmit_tail)->b_rptr); 20256 } else { 20257 prev_rptr = (*xmit_tail)->b_rptr; 20258 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 20259 *tail_unsent; 20260 } 20261 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 20262 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20263 /* Restore tcp_snxt so we get amount sent right. */ 20264 tcp->tcp_snxt = prev_snxt; 20265 if (prev_rptr == (*xmit_tail)->b_rptr) { 20266 /* 20267 * If the previous timestamp is still in use, 20268 * don't stomp on it. 20269 */ 20270 if ((*xmit_tail)->b_next == NULL) { 20271 (*xmit_tail)->b_prev = local_time; 20272 (*xmit_tail)->b_next = 20273 (mblk_t *)(uintptr_t)(*snxt); 20274 } 20275 } else 20276 (*xmit_tail)->b_rptr = prev_rptr; 20277 20278 if (mp == NULL) 20279 return (-1); 20280 mp1 = mp->b_cont; 20281 20282 tcp->tcp_last_sent_len = (ushort_t)len; 20283 while (mp1->b_cont) { 20284 *xmit_tail = (*xmit_tail)->b_cont; 20285 (*xmit_tail)->b_prev = local_time; 20286 (*xmit_tail)->b_next = 20287 (mblk_t *)(uintptr_t)(*snxt); 20288 mp1 = mp1->b_cont; 20289 } 20290 *snxt += len; 20291 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20292 BUMP_LOCAL(tcp->tcp_obsegs); 20293 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20294 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20295 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20296 tcp_send_data(tcp, q, mp); 20297 continue; 20298 } 20299 20300 *snxt += len; /* Adjust later if we don't send all of len */ 20301 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20302 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20303 20304 if (*tail_unsent) { 20305 /* Are the bytes above us in flight? */ 20306 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20307 if (rptr != (*xmit_tail)->b_rptr) { 20308 *tail_unsent -= len; 20309 tcp->tcp_last_sent_len = (ushort_t)len; 20310 len += tcp_hdr_len; 20311 if (tcp->tcp_ipversion == IPV4_VERSION) 20312 tcp->tcp_ipha->ipha_length = htons(len); 20313 else 20314 tcp->tcp_ip6h->ip6_plen = 20315 htons(len - 20316 ((char *)&tcp->tcp_ip6h[1] - 20317 tcp->tcp_iphc)); 20318 mp = dupb(*xmit_tail); 20319 if (!mp) 20320 return (-1); /* out_of_mem */ 20321 mp->b_rptr = rptr; 20322 /* 20323 * If the old timestamp is no longer in use, 20324 * sample a new timestamp now. 20325 */ 20326 if ((*xmit_tail)->b_next == NULL) { 20327 (*xmit_tail)->b_prev = local_time; 20328 (*xmit_tail)->b_next = 20329 (mblk_t *)(uintptr_t)(*snxt-len); 20330 } 20331 goto must_alloc; 20332 } 20333 } else { 20334 *xmit_tail = (*xmit_tail)->b_cont; 20335 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20336 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20337 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20338 (*xmit_tail)->b_rptr); 20339 } 20340 20341 (*xmit_tail)->b_prev = local_time; 20342 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20343 20344 *tail_unsent -= len; 20345 tcp->tcp_last_sent_len = (ushort_t)len; 20346 20347 len += tcp_hdr_len; 20348 if (tcp->tcp_ipversion == IPV4_VERSION) 20349 tcp->tcp_ipha->ipha_length = htons(len); 20350 else 20351 tcp->tcp_ip6h->ip6_plen = htons(len - 20352 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20353 20354 mp = dupb(*xmit_tail); 20355 if (!mp) 20356 return (-1); /* out_of_mem */ 20357 20358 len = tcp_hdr_len; 20359 /* 20360 * There are four reasons to allocate a new hdr mblk: 20361 * 1) The bytes above us are in use by another packet 20362 * 2) We don't have good alignment 20363 * 3) The mblk is being shared 20364 * 4) We don't have enough room for a header 20365 */ 20366 rptr = mp->b_rptr - len; 20367 if (!OK_32PTR(rptr) || 20368 ((db = mp->b_datap), db->db_ref != 2) || 20369 rptr < db->db_base) { 20370 /* NOTE: we assume allocb returns an OK_32PTR */ 20371 20372 must_alloc:; 20373 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20374 tcp_wroff_xtra, BPRI_MED); 20375 if (!mp1) { 20376 freemsg(mp); 20377 return (-1); /* out_of_mem */ 20378 } 20379 mp1->b_cont = mp; 20380 mp = mp1; 20381 /* Leave room for Link Level header */ 20382 len = tcp_hdr_len; 20383 rptr = &mp->b_rptr[tcp_wroff_xtra]; 20384 mp->b_wptr = &rptr[len]; 20385 } 20386 20387 /* 20388 * Fill in the header using the template header, and add 20389 * options such as time-stamp, ECN and/or SACK, as needed. 20390 */ 20391 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 20392 20393 mp->b_rptr = rptr; 20394 20395 if (*tail_unsent) { 20396 int spill = *tail_unsent; 20397 20398 mp1 = mp->b_cont; 20399 if (!mp1) 20400 mp1 = mp; 20401 20402 /* 20403 * If we're a little short, tack on more mblks until 20404 * there is no more spillover. 20405 */ 20406 while (spill < 0) { 20407 mblk_t *nmp; 20408 int nmpsz; 20409 20410 nmp = (*xmit_tail)->b_cont; 20411 nmpsz = MBLKL(nmp); 20412 20413 /* 20414 * Excess data in mblk; can we split it? 20415 * If MDT is enabled for the connection, 20416 * keep on splitting as this is a transient 20417 * send path. 20418 */ 20419 if (!tcp->tcp_mdt && (spill + nmpsz > 0)) { 20420 /* 20421 * Don't split if stream head was 20422 * told to break up larger writes 20423 * into smaller ones. 20424 */ 20425 if (tcp->tcp_maxpsz > 0) 20426 break; 20427 20428 /* 20429 * Next mblk is less than SMSS/2 20430 * rounded up to nearest 64-byte; 20431 * let it get sent as part of the 20432 * next segment. 20433 */ 20434 if (tcp->tcp_localnet && 20435 !tcp->tcp_cork && 20436 (nmpsz < roundup((mss >> 1), 64))) 20437 break; 20438 } 20439 20440 *xmit_tail = nmp; 20441 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 20442 /* Stash for rtt use later */ 20443 (*xmit_tail)->b_prev = local_time; 20444 (*xmit_tail)->b_next = 20445 (mblk_t *)(uintptr_t)(*snxt - len); 20446 mp1->b_cont = dupb(*xmit_tail); 20447 mp1 = mp1->b_cont; 20448 20449 spill += nmpsz; 20450 if (mp1 == NULL) { 20451 *tail_unsent = spill; 20452 freemsg(mp); 20453 return (-1); /* out_of_mem */ 20454 } 20455 } 20456 20457 /* Trim back any surplus on the last mblk */ 20458 if (spill >= 0) { 20459 mp1->b_wptr -= spill; 20460 *tail_unsent = spill; 20461 } else { 20462 /* 20463 * We did not send everything we could in 20464 * order to remain within the b_cont limit. 20465 */ 20466 *usable -= spill; 20467 *snxt += spill; 20468 tcp->tcp_last_sent_len += spill; 20469 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, spill); 20470 /* 20471 * Adjust the checksum 20472 */ 20473 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20474 sum += spill; 20475 sum = (sum >> 16) + (sum & 0xFFFF); 20476 U16_TO_ABE16(sum, tcph->th_sum); 20477 if (tcp->tcp_ipversion == IPV4_VERSION) { 20478 sum = ntohs( 20479 ((ipha_t *)rptr)->ipha_length) + 20480 spill; 20481 ((ipha_t *)rptr)->ipha_length = 20482 htons(sum); 20483 } else { 20484 sum = ntohs( 20485 ((ip6_t *)rptr)->ip6_plen) + 20486 spill; 20487 ((ip6_t *)rptr)->ip6_plen = 20488 htons(sum); 20489 } 20490 *tail_unsent = 0; 20491 } 20492 } 20493 if (tcp->tcp_ip_forward_progress) { 20494 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20495 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 20496 tcp->tcp_ip_forward_progress = B_FALSE; 20497 } 20498 20499 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20500 tcp_send_data(tcp, q, mp); 20501 BUMP_LOCAL(tcp->tcp_obsegs); 20502 } 20503 20504 return (0); 20505 } 20506 20507 /* Unlink and return any mblk that looks like it contains a MDT info */ 20508 static mblk_t * 20509 tcp_mdt_info_mp(mblk_t *mp) 20510 { 20511 mblk_t *prev_mp; 20512 20513 for (;;) { 20514 prev_mp = mp; 20515 /* no more to process? */ 20516 if ((mp = mp->b_cont) == NULL) 20517 break; 20518 20519 switch (DB_TYPE(mp)) { 20520 case M_CTL: 20521 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 20522 continue; 20523 ASSERT(prev_mp != NULL); 20524 prev_mp->b_cont = mp->b_cont; 20525 mp->b_cont = NULL; 20526 return (mp); 20527 default: 20528 break; 20529 } 20530 } 20531 return (mp); 20532 } 20533 20534 /* MDT info update routine, called when IP notifies us about MDT */ 20535 static void 20536 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 20537 { 20538 boolean_t prev_state; 20539 20540 /* 20541 * IP is telling us to abort MDT on this connection? We know 20542 * this because the capability is only turned off when IP 20543 * encounters some pathological cases, e.g. link-layer change 20544 * where the new driver doesn't support MDT, or in situation 20545 * where MDT usage on the link-layer has been switched off. 20546 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 20547 * if the link-layer doesn't support MDT, and if it does, it 20548 * will indicate that the feature is to be turned on. 20549 */ 20550 prev_state = tcp->tcp_mdt; 20551 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 20552 if (!tcp->tcp_mdt && !first) { 20553 TCP_STAT(tcp_mdt_conn_halted3); 20554 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 20555 (void *)tcp->tcp_connp)); 20556 } 20557 20558 /* 20559 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 20560 * so disable MDT otherwise. The checks are done here 20561 * and in tcp_wput_data(). 20562 */ 20563 if (tcp->tcp_mdt && 20564 (tcp->tcp_ipversion == IPV4_VERSION && 20565 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 20566 (tcp->tcp_ipversion == IPV6_VERSION && 20567 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 20568 tcp->tcp_mdt = B_FALSE; 20569 20570 if (tcp->tcp_mdt) { 20571 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 20572 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 20573 "version (%d), expected version is %d", 20574 mdt_capab->ill_mdt_version, MDT_VERSION_2); 20575 tcp->tcp_mdt = B_FALSE; 20576 return; 20577 } 20578 20579 /* 20580 * We need the driver to be able to handle at least three 20581 * spans per packet in order for tcp MDT to be utilized. 20582 * The first is for the header portion, while the rest are 20583 * needed to handle a packet that straddles across two 20584 * virtually non-contiguous buffers; a typical tcp packet 20585 * therefore consists of only two spans. Note that we take 20586 * a zero as "don't care". 20587 */ 20588 if (mdt_capab->ill_mdt_span_limit > 0 && 20589 mdt_capab->ill_mdt_span_limit < 3) { 20590 tcp->tcp_mdt = B_FALSE; 20591 return; 20592 } 20593 20594 /* a zero means driver wants default value */ 20595 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 20596 tcp_mdt_max_pbufs); 20597 if (tcp->tcp_mdt_max_pld == 0) 20598 tcp->tcp_mdt_max_pld = tcp_mdt_max_pbufs; 20599 20600 /* ensure 32-bit alignment */ 20601 tcp->tcp_mdt_hdr_head = roundup(MAX(tcp_mdt_hdr_head_min, 20602 mdt_capab->ill_mdt_hdr_head), 4); 20603 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcp_mdt_hdr_tail_min, 20604 mdt_capab->ill_mdt_hdr_tail), 4); 20605 20606 if (!first && !prev_state) { 20607 TCP_STAT(tcp_mdt_conn_resumed2); 20608 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 20609 (void *)tcp->tcp_connp)); 20610 } 20611 } 20612 } 20613 20614 static void 20615 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_mdt) 20616 { 20617 conn_t *connp = tcp->tcp_connp; 20618 20619 ASSERT(ire != NULL); 20620 20621 /* 20622 * We may be in the fastpath here, and although we essentially do 20623 * similar checks as in ip_bind_connected{_v6}/ip_mdinfo_return, 20624 * we try to keep things as brief as possible. After all, these 20625 * are only best-effort checks, and we do more thorough ones prior 20626 * to calling tcp_multisend(). 20627 */ 20628 if (ip_multidata_outbound && check_mdt && 20629 !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 20630 ill != NULL && ILL_MDT_CAPABLE(ill) && 20631 !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 20632 !(ire->ire_flags & RTF_MULTIRT) && 20633 !IPP_ENABLED(IPP_LOCAL_OUT) && 20634 CONN_IS_MD_FASTPATH(connp)) { 20635 /* Remember the result */ 20636 connp->conn_mdt_ok = B_TRUE; 20637 20638 ASSERT(ill->ill_mdt_capab != NULL); 20639 if (!ill->ill_mdt_capab->ill_mdt_on) { 20640 /* 20641 * If MDT has been previously turned off in the past, 20642 * and we currently can do MDT (due to IPQoS policy 20643 * removal, etc.) then enable it for this interface. 20644 */ 20645 ill->ill_mdt_capab->ill_mdt_on = 1; 20646 ip1dbg(("tcp_ire_ill_check: connp %p enables MDT for " 20647 "interface %s\n", (void *)connp, ill->ill_name)); 20648 } 20649 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 20650 } 20651 20652 /* 20653 * The goal is to reduce the number of generated tcp segments by 20654 * setting the maxpsz multiplier to 0; this will have an affect on 20655 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 20656 * into each packet, up to SMSS bytes. Doing this reduces the number 20657 * of outbound segments and incoming ACKs, thus allowing for better 20658 * network and system performance. In contrast the legacy behavior 20659 * may result in sending less than SMSS size, because the last mblk 20660 * for some packets may have more data than needed to make up SMSS, 20661 * and the legacy code refused to "split" it. 20662 * 20663 * We apply the new behavior on following situations: 20664 * 20665 * 1) Loopback connections, 20666 * 2) Connections in which the remote peer is not on local subnet, 20667 * 3) Local subnet connections over the bge interface (see below). 20668 * 20669 * Ideally, we would like this behavior to apply for interfaces other 20670 * than bge. However, doing so would negatively impact drivers which 20671 * perform dynamic mapping and unmapping of DMA resources, which are 20672 * increased by setting the maxpsz multiplier to 0 (more mblks per 20673 * packet will be generated by tcp). The bge driver does not suffer 20674 * from this, as it copies the mblks into pre-mapped buffers, and 20675 * therefore does not require more I/O resources than before. 20676 * 20677 * Otherwise, this behavior is present on all network interfaces when 20678 * the destination endpoint is non-local, since reducing the number 20679 * of packets in general is good for the network. 20680 * 20681 * TODO We need to remove this hard-coded conditional for bge once 20682 * a better "self-tuning" mechanism, or a way to comprehend 20683 * the driver transmit strategy is devised. Until the solution 20684 * is found and well understood, we live with this hack. 20685 */ 20686 if (!tcp_static_maxpsz && 20687 (tcp->tcp_loopback || !tcp->tcp_localnet || 20688 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 20689 /* override the default value */ 20690 tcp->tcp_maxpsz = 0; 20691 20692 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 20693 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 20694 ill != NULL ? ill->ill_name : ipif_loopback_name)); 20695 } 20696 20697 /* set the stream head parameters accordingly */ 20698 (void) tcp_maxpsz_set(tcp, B_TRUE); 20699 } 20700 20701 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 20702 static void 20703 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 20704 { 20705 uchar_t fval = *mp->b_rptr; 20706 mblk_t *tail; 20707 queue_t *q = tcp->tcp_wq; 20708 20709 /* TODO: How should flush interact with urgent data? */ 20710 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 20711 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 20712 /* 20713 * Flush only data that has not yet been put on the wire. If 20714 * we flush data that we have already transmitted, life, as we 20715 * know it, may come to an end. 20716 */ 20717 tail = tcp->tcp_xmit_tail; 20718 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 20719 tcp->tcp_xmit_tail_unsent = 0; 20720 tcp->tcp_unsent = 0; 20721 if (tail->b_wptr != tail->b_rptr) 20722 tail = tail->b_cont; 20723 if (tail) { 20724 mblk_t **excess = &tcp->tcp_xmit_head; 20725 for (;;) { 20726 mblk_t *mp1 = *excess; 20727 if (mp1 == tail) 20728 break; 20729 tcp->tcp_xmit_tail = mp1; 20730 tcp->tcp_xmit_last = mp1; 20731 excess = &mp1->b_cont; 20732 } 20733 *excess = NULL; 20734 tcp_close_mpp(&tail); 20735 if (tcp->tcp_snd_zcopy_aware) 20736 tcp_zcopy_notify(tcp); 20737 } 20738 /* 20739 * We have no unsent data, so unsent must be less than 20740 * tcp_xmit_lowater, so re-enable flow. 20741 */ 20742 if (tcp->tcp_flow_stopped) { 20743 tcp_clrqfull(tcp); 20744 } 20745 } 20746 /* 20747 * TODO: you can't just flush these, you have to increase rwnd for one 20748 * thing. For another, how should urgent data interact? 20749 */ 20750 if (fval & FLUSHR) { 20751 *mp->b_rptr = fval & ~FLUSHW; 20752 /* XXX */ 20753 qreply(q, mp); 20754 return; 20755 } 20756 freemsg(mp); 20757 } 20758 20759 /* 20760 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 20761 * messages. 20762 */ 20763 static void 20764 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 20765 { 20766 mblk_t *mp1; 20767 STRUCT_HANDLE(strbuf, sb); 20768 uint16_t port; 20769 queue_t *q = tcp->tcp_wq; 20770 in6_addr_t v6addr; 20771 ipaddr_t v4addr; 20772 uint32_t flowinfo = 0; 20773 int addrlen; 20774 20775 /* Make sure it is one of ours. */ 20776 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 20777 case TI_GETMYNAME: 20778 case TI_GETPEERNAME: 20779 break; 20780 default: 20781 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20782 return; 20783 } 20784 switch (mi_copy_state(q, mp, &mp1)) { 20785 case -1: 20786 return; 20787 case MI_COPY_CASE(MI_COPY_IN, 1): 20788 break; 20789 case MI_COPY_CASE(MI_COPY_OUT, 1): 20790 /* Copy out the strbuf. */ 20791 mi_copyout(q, mp); 20792 return; 20793 case MI_COPY_CASE(MI_COPY_OUT, 2): 20794 /* All done. */ 20795 mi_copy_done(q, mp, 0); 20796 return; 20797 default: 20798 mi_copy_done(q, mp, EPROTO); 20799 return; 20800 } 20801 /* Check alignment of the strbuf */ 20802 if (!OK_32PTR(mp1->b_rptr)) { 20803 mi_copy_done(q, mp, EINVAL); 20804 return; 20805 } 20806 20807 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 20808 (void *)mp1->b_rptr); 20809 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 20810 20811 if (STRUCT_FGET(sb, maxlen) < addrlen) { 20812 mi_copy_done(q, mp, EINVAL); 20813 return; 20814 } 20815 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 20816 case TI_GETMYNAME: 20817 if (tcp->tcp_family == AF_INET) { 20818 if (tcp->tcp_ipversion == IPV4_VERSION) { 20819 v4addr = tcp->tcp_ipha->ipha_src; 20820 } else { 20821 /* can't return an address in this case */ 20822 v4addr = 0; 20823 } 20824 } else { 20825 /* tcp->tcp_family == AF_INET6 */ 20826 if (tcp->tcp_ipversion == IPV4_VERSION) { 20827 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 20828 &v6addr); 20829 } else { 20830 v6addr = tcp->tcp_ip6h->ip6_src; 20831 } 20832 } 20833 port = tcp->tcp_lport; 20834 break; 20835 case TI_GETPEERNAME: 20836 if (tcp->tcp_family == AF_INET) { 20837 if (tcp->tcp_ipversion == IPV4_VERSION) { 20838 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 20839 v4addr); 20840 } else { 20841 /* can't return an address in this case */ 20842 v4addr = 0; 20843 } 20844 } else { 20845 /* tcp->tcp_family == AF_INET6) */ 20846 v6addr = tcp->tcp_remote_v6; 20847 if (tcp->tcp_ipversion == IPV6_VERSION) { 20848 /* 20849 * No flowinfo if tcp->tcp_ipversion is v4. 20850 * 20851 * flowinfo was already initialized to zero 20852 * where it was declared above, so only 20853 * set it if ipversion is v6. 20854 */ 20855 flowinfo = tcp->tcp_ip6h->ip6_vcf & 20856 ~IPV6_VERS_AND_FLOW_MASK; 20857 } 20858 } 20859 port = tcp->tcp_fport; 20860 break; 20861 default: 20862 mi_copy_done(q, mp, EPROTO); 20863 return; 20864 } 20865 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 20866 if (!mp1) 20867 return; 20868 20869 if (tcp->tcp_family == AF_INET) { 20870 sin_t *sin; 20871 20872 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 20873 sin = (sin_t *)mp1->b_rptr; 20874 mp1->b_wptr = (uchar_t *)&sin[1]; 20875 *sin = sin_null; 20876 sin->sin_family = AF_INET; 20877 sin->sin_addr.s_addr = v4addr; 20878 sin->sin_port = port; 20879 } else { 20880 /* tcp->tcp_family == AF_INET6 */ 20881 sin6_t *sin6; 20882 20883 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 20884 sin6 = (sin6_t *)mp1->b_rptr; 20885 mp1->b_wptr = (uchar_t *)&sin6[1]; 20886 *sin6 = sin6_null; 20887 sin6->sin6_family = AF_INET6; 20888 sin6->sin6_flowinfo = flowinfo; 20889 sin6->sin6_addr = v6addr; 20890 sin6->sin6_port = port; 20891 } 20892 /* Copy out the address */ 20893 mi_copyout(q, mp); 20894 } 20895 20896 /* 20897 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 20898 * messages. 20899 */ 20900 /* ARGSUSED */ 20901 static void 20902 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 20903 { 20904 conn_t *connp = (conn_t *)arg; 20905 tcp_t *tcp = connp->conn_tcp; 20906 queue_t *q = tcp->tcp_wq; 20907 struct iocblk *iocp; 20908 20909 ASSERT(DB_TYPE(mp) == M_IOCTL); 20910 /* 20911 * Try and ASSERT the minimum possible references on the 20912 * conn early enough. Since we are executing on write side, 20913 * the connection is obviously not detached and that means 20914 * there is a ref each for TCP and IP. Since we are behind 20915 * the squeue, the minimum references needed are 3. If the 20916 * conn is in classifier hash list, there should be an 20917 * extra ref for that (we check both the possibilities). 20918 */ 20919 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 20920 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 20921 20922 iocp = (struct iocblk *)mp->b_rptr; 20923 switch (iocp->ioc_cmd) { 20924 case TCP_IOC_DEFAULT_Q: 20925 /* Wants to be the default wq. */ 20926 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 20927 iocp->ioc_error = EPERM; 20928 iocp->ioc_count = 0; 20929 mp->b_datap->db_type = M_IOCACK; 20930 qreply(q, mp); 20931 return; 20932 } 20933 tcp_def_q_set(tcp, mp); 20934 return; 20935 case _SIOCSOCKFALLBACK: 20936 /* 20937 * Either sockmod is about to be popped and the socket 20938 * would now be treated as a plain stream, or a module 20939 * is about to be pushed so we could no longer use read- 20940 * side synchronous streams for fused loopback tcp. 20941 * Drain any queued data and disable direct sockfs 20942 * interface from now on. 20943 */ 20944 if (!tcp->tcp_issocket) { 20945 DB_TYPE(mp) = M_IOCNAK; 20946 iocp->ioc_error = EINVAL; 20947 } else { 20948 #ifdef _ILP32 20949 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 20950 #else 20951 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 20952 #endif 20953 /* 20954 * Insert this socket into the acceptor hash. 20955 * We might need it for T_CONN_RES message 20956 */ 20957 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 20958 20959 if (tcp->tcp_fused) { 20960 /* 20961 * This is a fused loopback tcp; disable 20962 * read-side synchronous streams interface 20963 * and drain any queued data. It is okay 20964 * to do this for non-synchronous streams 20965 * fused tcp as well. 20966 */ 20967 tcp_fuse_disable_pair(tcp, B_FALSE); 20968 } 20969 tcp->tcp_issocket = B_FALSE; 20970 TCP_STAT(tcp_sock_fallback); 20971 20972 DB_TYPE(mp) = M_IOCACK; 20973 iocp->ioc_error = 0; 20974 } 20975 iocp->ioc_count = 0; 20976 iocp->ioc_rval = 0; 20977 qreply(q, mp); 20978 return; 20979 } 20980 CALL_IP_WPUT(connp, q, mp); 20981 } 20982 20983 /* 20984 * This routine is called by tcp_wput() to handle all TPI requests. 20985 */ 20986 /* ARGSUSED */ 20987 static void 20988 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 20989 { 20990 conn_t *connp = (conn_t *)arg; 20991 tcp_t *tcp = connp->conn_tcp; 20992 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 20993 uchar_t *rptr; 20994 t_scalar_t type; 20995 int len; 20996 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 20997 20998 /* 20999 * Try and ASSERT the minimum possible references on the 21000 * conn early enough. Since we are executing on write side, 21001 * the connection is obviously not detached and that means 21002 * there is a ref each for TCP and IP. Since we are behind 21003 * the squeue, the minimum references needed are 3. If the 21004 * conn is in classifier hash list, there should be an 21005 * extra ref for that (we check both the possibilities). 21006 */ 21007 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 21008 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 21009 21010 rptr = mp->b_rptr; 21011 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21012 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 21013 type = ((union T_primitives *)rptr)->type; 21014 if (type == T_EXDATA_REQ) { 21015 uint32_t msize = msgdsize(mp->b_cont); 21016 21017 len = msize - 1; 21018 if (len < 0) { 21019 freemsg(mp); 21020 return; 21021 } 21022 /* 21023 * Try to force urgent data out on the wire. 21024 * Even if we have unsent data this will 21025 * at least send the urgent flag. 21026 * XXX does not handle more flag correctly. 21027 */ 21028 len += tcp->tcp_unsent; 21029 len += tcp->tcp_snxt; 21030 tcp->tcp_urg = len; 21031 tcp->tcp_valid_bits |= TCP_URG_VALID; 21032 21033 /* Bypass tcp protocol for fused tcp loopback */ 21034 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 21035 return; 21036 } else if (type != T_DATA_REQ) { 21037 goto non_urgent_data; 21038 } 21039 /* TODO: options, flags, ... from user */ 21040 /* Set length to zero for reclamation below */ 21041 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 21042 freeb(mp); 21043 return; 21044 } else { 21045 if (tcp->tcp_debug) { 21046 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21047 "tcp_wput_proto, dropping one..."); 21048 } 21049 freemsg(mp); 21050 return; 21051 } 21052 21053 non_urgent_data: 21054 21055 switch ((int)tprim->type) { 21056 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 21057 /* 21058 * save the kssl_ent_t from the next block, and convert this 21059 * back to a normal bind_req. 21060 */ 21061 if (mp->b_cont != NULL) { 21062 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 21063 21064 if (tcp->tcp_kssl_ent != NULL) { 21065 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 21066 KSSL_NO_PROXY); 21067 tcp->tcp_kssl_ent = NULL; 21068 } 21069 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 21070 sizeof (kssl_ent_t)); 21071 kssl_hold_ent(tcp->tcp_kssl_ent); 21072 freemsg(mp->b_cont); 21073 mp->b_cont = NULL; 21074 } 21075 tprim->type = T_BIND_REQ; 21076 21077 /* FALLTHROUGH */ 21078 case O_T_BIND_REQ: /* bind request */ 21079 case T_BIND_REQ: /* new semantics bind request */ 21080 tcp_bind(tcp, mp); 21081 break; 21082 case T_UNBIND_REQ: /* unbind request */ 21083 tcp_unbind(tcp, mp); 21084 break; 21085 case O_T_CONN_RES: /* old connection response XXX */ 21086 case T_CONN_RES: /* connection response */ 21087 tcp_accept(tcp, mp); 21088 break; 21089 case T_CONN_REQ: /* connection request */ 21090 tcp_connect(tcp, mp); 21091 break; 21092 case T_DISCON_REQ: /* disconnect request */ 21093 tcp_disconnect(tcp, mp); 21094 break; 21095 case T_CAPABILITY_REQ: 21096 tcp_capability_req(tcp, mp); /* capability request */ 21097 break; 21098 case T_INFO_REQ: /* information request */ 21099 tcp_info_req(tcp, mp); 21100 break; 21101 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 21102 /* Only IP is allowed to return meaningful value */ 21103 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 21104 break; 21105 case T_OPTMGMT_REQ: 21106 /* 21107 * Note: no support for snmpcom_req() through new 21108 * T_OPTMGMT_REQ. See comments in ip.c 21109 */ 21110 /* Only IP is allowed to return meaningful value */ 21111 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 21112 break; 21113 21114 case T_UNITDATA_REQ: /* unitdata request */ 21115 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21116 break; 21117 case T_ORDREL_REQ: /* orderly release req */ 21118 freemsg(mp); 21119 21120 if (tcp->tcp_fused) 21121 tcp_unfuse(tcp); 21122 21123 if (tcp_xmit_end(tcp) != 0) { 21124 /* 21125 * We were crossing FINs and got a reset from 21126 * the other side. Just ignore it. 21127 */ 21128 if (tcp->tcp_debug) { 21129 (void) strlog(TCP_MOD_ID, 0, 1, 21130 SL_ERROR|SL_TRACE, 21131 "tcp_wput_proto, T_ORDREL_REQ out of " 21132 "state %s", 21133 tcp_display(tcp, NULL, 21134 DISP_ADDR_AND_PORT)); 21135 } 21136 } 21137 break; 21138 case T_ADDR_REQ: 21139 tcp_addr_req(tcp, mp); 21140 break; 21141 default: 21142 if (tcp->tcp_debug) { 21143 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 21144 "tcp_wput_proto, bogus TPI msg, type %d", 21145 tprim->type); 21146 } 21147 /* 21148 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 21149 * to recover. 21150 */ 21151 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 21152 break; 21153 } 21154 } 21155 21156 /* 21157 * The TCP write service routine should never be called... 21158 */ 21159 /* ARGSUSED */ 21160 static void 21161 tcp_wsrv(queue_t *q) 21162 { 21163 TCP_STAT(tcp_wsrv_called); 21164 } 21165 21166 /* Non overlapping byte exchanger */ 21167 static void 21168 tcp_xchg(uchar_t *a, uchar_t *b, int len) 21169 { 21170 uchar_t uch; 21171 21172 while (len-- > 0) { 21173 uch = a[len]; 21174 a[len] = b[len]; 21175 b[len] = uch; 21176 } 21177 } 21178 21179 /* 21180 * Send out a control packet on the tcp connection specified. This routine 21181 * is typically called where we need a simple ACK or RST generated. 21182 */ 21183 static void 21184 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 21185 { 21186 uchar_t *rptr; 21187 tcph_t *tcph; 21188 ipha_t *ipha = NULL; 21189 ip6_t *ip6h = NULL; 21190 uint32_t sum; 21191 int tcp_hdr_len; 21192 int tcp_ip_hdr_len; 21193 mblk_t *mp; 21194 21195 /* 21196 * Save sum for use in source route later. 21197 */ 21198 ASSERT(tcp != NULL); 21199 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 21200 tcp_hdr_len = tcp->tcp_hdr_len; 21201 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 21202 21203 /* If a text string is passed in with the request, pass it to strlog. */ 21204 if (str != NULL && tcp->tcp_debug) { 21205 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21206 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 21207 str, seq, ack, ctl); 21208 } 21209 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 21210 BPRI_MED); 21211 if (mp == NULL) { 21212 return; 21213 } 21214 rptr = &mp->b_rptr[tcp_wroff_xtra]; 21215 mp->b_rptr = rptr; 21216 mp->b_wptr = &rptr[tcp_hdr_len]; 21217 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 21218 21219 if (tcp->tcp_ipversion == IPV4_VERSION) { 21220 ipha = (ipha_t *)rptr; 21221 ipha->ipha_length = htons(tcp_hdr_len); 21222 } else { 21223 ip6h = (ip6_t *)rptr; 21224 ASSERT(tcp != NULL); 21225 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 21226 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21227 } 21228 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 21229 tcph->th_flags[0] = (uint8_t)ctl; 21230 if (ctl & TH_RST) { 21231 BUMP_MIB(&tcp_mib, tcpOutRsts); 21232 BUMP_MIB(&tcp_mib, tcpOutControl); 21233 /* 21234 * Don't send TSopt w/ TH_RST packets per RFC 1323. 21235 */ 21236 if (tcp->tcp_snd_ts_ok && 21237 tcp->tcp_state > TCPS_SYN_SENT) { 21238 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 21239 *(mp->b_wptr) = TCPOPT_EOL; 21240 if (tcp->tcp_ipversion == IPV4_VERSION) { 21241 ipha->ipha_length = htons(tcp_hdr_len - 21242 TCPOPT_REAL_TS_LEN); 21243 } else { 21244 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 21245 TCPOPT_REAL_TS_LEN); 21246 } 21247 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 21248 sum -= TCPOPT_REAL_TS_LEN; 21249 } 21250 } 21251 if (ctl & TH_ACK) { 21252 if (tcp->tcp_snd_ts_ok) { 21253 U32_TO_BE32(lbolt, 21254 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21255 U32_TO_BE32(tcp->tcp_ts_recent, 21256 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21257 } 21258 21259 /* Update the latest receive window size in TCP header. */ 21260 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21261 tcph->th_win); 21262 tcp->tcp_rack = ack; 21263 tcp->tcp_rack_cnt = 0; 21264 BUMP_MIB(&tcp_mib, tcpOutAck); 21265 } 21266 BUMP_LOCAL(tcp->tcp_obsegs); 21267 U32_TO_BE32(seq, tcph->th_seq); 21268 U32_TO_BE32(ack, tcph->th_ack); 21269 /* 21270 * Include the adjustment for a source route if any. 21271 */ 21272 sum = (sum >> 16) + (sum & 0xFFFF); 21273 U16_TO_BE16(sum, tcph->th_sum); 21274 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21275 tcp_send_data(tcp, tcp->tcp_wq, mp); 21276 } 21277 21278 /* 21279 * If this routine returns B_TRUE, TCP can generate a RST in response 21280 * to a segment. If it returns B_FALSE, TCP should not respond. 21281 */ 21282 static boolean_t 21283 tcp_send_rst_chk(void) 21284 { 21285 clock_t now; 21286 21287 /* 21288 * TCP needs to protect itself from generating too many RSTs. 21289 * This can be a DoS attack by sending us random segments 21290 * soliciting RSTs. 21291 * 21292 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21293 * in each 1 second interval. In this way, TCP still generate 21294 * RSTs in normal cases but when under attack, the impact is 21295 * limited. 21296 */ 21297 if (tcp_rst_sent_rate_enabled != 0) { 21298 now = lbolt; 21299 /* lbolt can wrap around. */ 21300 if ((tcp_last_rst_intrvl > now) || 21301 (TICK_TO_MSEC(now - tcp_last_rst_intrvl) > 1*SECONDS)) { 21302 tcp_last_rst_intrvl = now; 21303 tcp_rst_cnt = 1; 21304 } else if (++tcp_rst_cnt > tcp_rst_sent_rate) { 21305 return (B_FALSE); 21306 } 21307 } 21308 return (B_TRUE); 21309 } 21310 21311 /* 21312 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21313 */ 21314 static void 21315 tcp_ip_ire_mark_advice(tcp_t *tcp) 21316 { 21317 mblk_t *mp; 21318 ipic_t *ipic; 21319 21320 if (tcp->tcp_ipversion == IPV4_VERSION) { 21321 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21322 &ipic); 21323 } else { 21324 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21325 &ipic); 21326 } 21327 if (mp == NULL) 21328 return; 21329 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21330 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21331 } 21332 21333 /* 21334 * Return an IP advice ioctl mblk and set ipic to be the pointer 21335 * to the advice structure. 21336 */ 21337 static mblk_t * 21338 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21339 { 21340 struct iocblk *ioc; 21341 mblk_t *mp, *mp1; 21342 21343 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 21344 if (mp == NULL) 21345 return (NULL); 21346 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 21347 *ipic = (ipic_t *)mp->b_rptr; 21348 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 21349 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 21350 21351 bcopy(addr, *ipic + 1, addr_len); 21352 21353 (*ipic)->ipic_addr_length = addr_len; 21354 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 21355 21356 mp1 = mkiocb(IP_IOCTL); 21357 if (mp1 == NULL) { 21358 freemsg(mp); 21359 return (NULL); 21360 } 21361 mp1->b_cont = mp; 21362 ioc = (struct iocblk *)mp1->b_rptr; 21363 ioc->ioc_count = sizeof (ipic_t) + addr_len; 21364 21365 return (mp1); 21366 } 21367 21368 /* 21369 * Generate a reset based on an inbound packet for which there is no active 21370 * tcp state that we can find. 21371 * 21372 * IPSEC NOTE : Try to send the reply with the same protection as it came 21373 * in. We still have the ipsec_mp that the packet was attached to. Thus 21374 * the packet will go out at the same level of protection as it came in by 21375 * converting the IPSEC_IN to IPSEC_OUT. 21376 */ 21377 static void 21378 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 21379 uint32_t ack, int ctl, uint_t ip_hdr_len) 21380 { 21381 ipha_t *ipha = NULL; 21382 ip6_t *ip6h = NULL; 21383 ushort_t len; 21384 tcph_t *tcph; 21385 int i; 21386 mblk_t *ipsec_mp; 21387 boolean_t mctl_present; 21388 ipic_t *ipic; 21389 ipaddr_t v4addr; 21390 in6_addr_t v6addr; 21391 int addr_len; 21392 void *addr; 21393 queue_t *q = tcp_g_q; 21394 tcp_t *tcp = Q_TO_TCP(q); 21395 cred_t *cr; 21396 21397 if (!tcp_send_rst_chk()) { 21398 tcp_rst_unsent++; 21399 freemsg(mp); 21400 return; 21401 } 21402 21403 if (mp->b_datap->db_type == M_CTL) { 21404 ipsec_mp = mp; 21405 mp = mp->b_cont; 21406 mctl_present = B_TRUE; 21407 } else { 21408 ipsec_mp = mp; 21409 mctl_present = B_FALSE; 21410 } 21411 21412 if (str && q && tcp_dbg) { 21413 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21414 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 21415 "flags 0x%x", 21416 str, seq, ack, ctl); 21417 } 21418 if (mp->b_datap->db_ref != 1) { 21419 mblk_t *mp1 = copyb(mp); 21420 freemsg(mp); 21421 mp = mp1; 21422 if (!mp) { 21423 if (mctl_present) 21424 freeb(ipsec_mp); 21425 return; 21426 } else { 21427 if (mctl_present) { 21428 ipsec_mp->b_cont = mp; 21429 } else { 21430 ipsec_mp = mp; 21431 } 21432 } 21433 } else if (mp->b_cont) { 21434 freemsg(mp->b_cont); 21435 mp->b_cont = NULL; 21436 } 21437 /* 21438 * We skip reversing source route here. 21439 * (for now we replace all IP options with EOL) 21440 */ 21441 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21442 ipha = (ipha_t *)mp->b_rptr; 21443 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 21444 mp->b_rptr[i] = IPOPT_EOL; 21445 /* 21446 * Make sure that src address isn't flagrantly invalid. 21447 * Not all broadcast address checking for the src address 21448 * is possible, since we don't know the netmask of the src 21449 * addr. No check for destination address is done, since 21450 * IP will not pass up a packet with a broadcast dest 21451 * address to TCP. Similar checks are done below for IPv6. 21452 */ 21453 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 21454 CLASSD(ipha->ipha_src)) { 21455 freemsg(ipsec_mp); 21456 BUMP_MIB(&ip_mib, ipInDiscards); 21457 return; 21458 } 21459 } else { 21460 ip6h = (ip6_t *)mp->b_rptr; 21461 21462 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 21463 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 21464 freemsg(ipsec_mp); 21465 BUMP_MIB(&ip6_mib, ipv6InDiscards); 21466 return; 21467 } 21468 21469 /* Remove any extension headers assuming partial overlay */ 21470 if (ip_hdr_len > IPV6_HDR_LEN) { 21471 uint8_t *to; 21472 21473 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 21474 ovbcopy(ip6h, to, IPV6_HDR_LEN); 21475 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 21476 ip_hdr_len = IPV6_HDR_LEN; 21477 ip6h = (ip6_t *)mp->b_rptr; 21478 ip6h->ip6_nxt = IPPROTO_TCP; 21479 } 21480 } 21481 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 21482 if (tcph->th_flags[0] & TH_RST) { 21483 freemsg(ipsec_mp); 21484 return; 21485 } 21486 tcph->th_offset_and_rsrvd[0] = (5 << 4); 21487 len = ip_hdr_len + sizeof (tcph_t); 21488 mp->b_wptr = &mp->b_rptr[len]; 21489 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21490 ipha->ipha_length = htons(len); 21491 /* Swap addresses */ 21492 v4addr = ipha->ipha_src; 21493 ipha->ipha_src = ipha->ipha_dst; 21494 ipha->ipha_dst = v4addr; 21495 ipha->ipha_ident = 0; 21496 ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 21497 addr_len = IP_ADDR_LEN; 21498 addr = &v4addr; 21499 } else { 21500 /* No ip6i_t in this case */ 21501 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 21502 /* Swap addresses */ 21503 v6addr = ip6h->ip6_src; 21504 ip6h->ip6_src = ip6h->ip6_dst; 21505 ip6h->ip6_dst = v6addr; 21506 ip6h->ip6_hops = (uchar_t)tcp_ipv6_hoplimit; 21507 addr_len = IPV6_ADDR_LEN; 21508 addr = &v6addr; 21509 } 21510 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 21511 U32_TO_BE32(ack, tcph->th_ack); 21512 U32_TO_BE32(seq, tcph->th_seq); 21513 U16_TO_BE16(0, tcph->th_win); 21514 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 21515 tcph->th_flags[0] = (uint8_t)ctl; 21516 if (ctl & TH_RST) { 21517 BUMP_MIB(&tcp_mib, tcpOutRsts); 21518 BUMP_MIB(&tcp_mib, tcpOutControl); 21519 } 21520 21521 /* IP trusts us to set up labels when required. */ 21522 if (is_system_labeled() && (cr = DB_CRED(mp)) != NULL && 21523 crgetlabel(cr) != NULL) { 21524 int err, adjust; 21525 21526 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) 21527 err = tsol_check_label(cr, &mp, &adjust, 21528 tcp->tcp_connp->conn_mac_exempt); 21529 else 21530 err = tsol_check_label_v6(cr, &mp, &adjust, 21531 tcp->tcp_connp->conn_mac_exempt); 21532 if (mctl_present) 21533 ipsec_mp->b_cont = mp; 21534 else 21535 ipsec_mp = mp; 21536 if (err != 0) { 21537 freemsg(ipsec_mp); 21538 return; 21539 } 21540 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21541 ipha = (ipha_t *)mp->b_rptr; 21542 adjust += ntohs(ipha->ipha_length); 21543 ipha->ipha_length = htons(adjust); 21544 } else { 21545 ip6h = (ip6_t *)mp->b_rptr; 21546 } 21547 } 21548 21549 if (mctl_present) { 21550 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21551 21552 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21553 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 21554 return; 21555 } 21556 } 21557 /* 21558 * NOTE: one might consider tracing a TCP packet here, but 21559 * this function has no active TCP state and no tcp structure 21560 * that has a trace buffer. If we traced here, we would have 21561 * to keep a local trace buffer in tcp_record_trace(). 21562 * 21563 * TSol note: The mblk that contains the incoming packet was 21564 * reused by tcp_xmit_listener_reset, so it already contains 21565 * the right credentials and we don't need to call mblk_setcred. 21566 * Also the conn's cred is not right since it is associated 21567 * with tcp_g_q. 21568 */ 21569 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 21570 21571 /* 21572 * Tell IP to mark the IRE used for this destination temporary. 21573 * This way, we can limit our exposure to DoS attack because IP 21574 * creates an IRE for each destination. If there are too many, 21575 * the time to do any routing lookup will be extremely long. And 21576 * the lookup can be in interrupt context. 21577 * 21578 * Note that in normal circumstances, this marking should not 21579 * affect anything. It would be nice if only 1 message is 21580 * needed to inform IP that the IRE created for this RST should 21581 * not be added to the cache table. But there is currently 21582 * not such communication mechanism between TCP and IP. So 21583 * the best we can do now is to send the advice ioctl to IP 21584 * to mark the IRE temporary. 21585 */ 21586 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 21587 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21588 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21589 } 21590 } 21591 21592 /* 21593 * Initiate closedown sequence on an active connection. (May be called as 21594 * writer.) Return value zero for OK return, non-zero for error return. 21595 */ 21596 static int 21597 tcp_xmit_end(tcp_t *tcp) 21598 { 21599 ipic_t *ipic; 21600 mblk_t *mp; 21601 21602 if (tcp->tcp_state < TCPS_SYN_RCVD || 21603 tcp->tcp_state > TCPS_CLOSE_WAIT) { 21604 /* 21605 * Invalid state, only states TCPS_SYN_RCVD, 21606 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 21607 */ 21608 return (-1); 21609 } 21610 21611 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 21612 tcp->tcp_valid_bits |= TCP_FSS_VALID; 21613 /* 21614 * If there is nothing more unsent, send the FIN now. 21615 * Otherwise, it will go out with the last segment. 21616 */ 21617 if (tcp->tcp_unsent == 0) { 21618 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 21619 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 21620 21621 if (mp) { 21622 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21623 tcp_send_data(tcp, tcp->tcp_wq, mp); 21624 } else { 21625 /* 21626 * Couldn't allocate msg. Pretend we got it out. 21627 * Wait for rexmit timeout. 21628 */ 21629 tcp->tcp_snxt = tcp->tcp_fss + 1; 21630 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21631 } 21632 21633 /* 21634 * If needed, update tcp_rexmit_snxt as tcp_snxt is 21635 * changed. 21636 */ 21637 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 21638 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 21639 } 21640 } else { 21641 /* 21642 * If tcp->tcp_cork is set, then the data will not get sent, 21643 * so we have to check that and unset it first. 21644 */ 21645 if (tcp->tcp_cork) 21646 tcp->tcp_cork = B_FALSE; 21647 tcp_wput_data(tcp, NULL, B_FALSE); 21648 } 21649 21650 /* 21651 * If TCP does not get enough samples of RTT or tcp_rtt_updates 21652 * is 0, don't update the cache. 21653 */ 21654 if (tcp_rtt_updates == 0 || tcp->tcp_rtt_update < tcp_rtt_updates) 21655 return (0); 21656 21657 /* 21658 * NOTE: should not update if source routes i.e. if tcp_remote if 21659 * different from the destination. 21660 */ 21661 if (tcp->tcp_ipversion == IPV4_VERSION) { 21662 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 21663 return (0); 21664 } 21665 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21666 &ipic); 21667 } else { 21668 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 21669 &tcp->tcp_ip6h->ip6_dst))) { 21670 return (0); 21671 } 21672 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21673 &ipic); 21674 } 21675 21676 /* Record route attributes in the IRE for use by future connections. */ 21677 if (mp == NULL) 21678 return (0); 21679 21680 /* 21681 * We do not have a good algorithm to update ssthresh at this time. 21682 * So don't do any update. 21683 */ 21684 ipic->ipic_rtt = tcp->tcp_rtt_sa; 21685 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 21686 21687 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21688 return (0); 21689 } 21690 21691 /* 21692 * Generate a "no listener here" RST in response to an "unknown" segment. 21693 * Note that we are reusing the incoming mp to construct the outgoing 21694 * RST. 21695 */ 21696 void 21697 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len) 21698 { 21699 uchar_t *rptr; 21700 uint32_t seg_len; 21701 tcph_t *tcph; 21702 uint32_t seg_seq; 21703 uint32_t seg_ack; 21704 uint_t flags; 21705 mblk_t *ipsec_mp; 21706 ipha_t *ipha; 21707 ip6_t *ip6h; 21708 boolean_t mctl_present = B_FALSE; 21709 boolean_t check = B_TRUE; 21710 boolean_t policy_present; 21711 21712 TCP_STAT(tcp_no_listener); 21713 21714 ipsec_mp = mp; 21715 21716 if (mp->b_datap->db_type == M_CTL) { 21717 ipsec_in_t *ii; 21718 21719 mctl_present = B_TRUE; 21720 mp = mp->b_cont; 21721 21722 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21723 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21724 if (ii->ipsec_in_dont_check) { 21725 check = B_FALSE; 21726 if (!ii->ipsec_in_secure) { 21727 freeb(ipsec_mp); 21728 mctl_present = B_FALSE; 21729 ipsec_mp = mp; 21730 } 21731 } 21732 } 21733 21734 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21735 policy_present = ipsec_inbound_v4_policy_present; 21736 ipha = (ipha_t *)mp->b_rptr; 21737 ip6h = NULL; 21738 } else { 21739 policy_present = ipsec_inbound_v6_policy_present; 21740 ipha = NULL; 21741 ip6h = (ip6_t *)mp->b_rptr; 21742 } 21743 21744 if (check && policy_present) { 21745 /* 21746 * The conn_t parameter is NULL because we already know 21747 * nobody's home. 21748 */ 21749 ipsec_mp = ipsec_check_global_policy( 21750 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present); 21751 if (ipsec_mp == NULL) 21752 return; 21753 } 21754 if (is_system_labeled() && !tsol_can_reply_error(mp)) { 21755 DTRACE_PROBE2( 21756 tx__ip__log__error__nolistener__tcp, 21757 char *, "Could not reply with RST to mp(1)", 21758 mblk_t *, mp); 21759 ip2dbg(("tcp_xmit_listeners_reset: not permitted to reply\n")); 21760 freemsg(ipsec_mp); 21761 return; 21762 } 21763 21764 rptr = mp->b_rptr; 21765 21766 tcph = (tcph_t *)&rptr[ip_hdr_len]; 21767 seg_seq = BE32_TO_U32(tcph->th_seq); 21768 seg_ack = BE32_TO_U32(tcph->th_ack); 21769 flags = tcph->th_flags[0]; 21770 21771 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 21772 if (flags & TH_RST) { 21773 freemsg(ipsec_mp); 21774 } else if (flags & TH_ACK) { 21775 tcp_xmit_early_reset("no tcp, reset", 21776 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len); 21777 } else { 21778 if (flags & TH_SYN) { 21779 seg_len++; 21780 } else { 21781 /* 21782 * Here we violate the RFC. Note that a normal 21783 * TCP will never send a segment without the ACK 21784 * flag, except for RST or SYN segment. This 21785 * segment is neither. Just drop it on the 21786 * floor. 21787 */ 21788 freemsg(ipsec_mp); 21789 tcp_rst_unsent++; 21790 return; 21791 } 21792 21793 tcp_xmit_early_reset("no tcp, reset/ack", 21794 ipsec_mp, 0, seg_seq + seg_len, 21795 TH_RST | TH_ACK, ip_hdr_len); 21796 } 21797 } 21798 21799 /* 21800 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 21801 * ip and tcp header ready to pass down to IP. If the mp passed in is 21802 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 21803 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 21804 * otherwise it will dup partial mblks.) 21805 * Otherwise, an appropriate ACK packet will be generated. This 21806 * routine is not usually called to send new data for the first time. It 21807 * is mostly called out of the timer for retransmits, and to generate ACKs. 21808 * 21809 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 21810 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 21811 * of the original mblk chain will be returned in *offset and *end_mp. 21812 */ 21813 static mblk_t * 21814 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 21815 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 21816 boolean_t rexmit) 21817 { 21818 int data_length; 21819 int32_t off = 0; 21820 uint_t flags; 21821 mblk_t *mp1; 21822 mblk_t *mp2; 21823 uchar_t *rptr; 21824 tcph_t *tcph; 21825 int32_t num_sack_blk = 0; 21826 int32_t sack_opt_len = 0; 21827 21828 /* Allocate for our maximum TCP header + link-level */ 21829 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 21830 BPRI_MED); 21831 if (!mp1) 21832 return (NULL); 21833 data_length = 0; 21834 21835 /* 21836 * Note that tcp_mss has been adjusted to take into account the 21837 * timestamp option if applicable. Because SACK options do not 21838 * appear in every TCP segments and they are of variable lengths, 21839 * they cannot be included in tcp_mss. Thus we need to calculate 21840 * the actual segment length when we need to send a segment which 21841 * includes SACK options. 21842 */ 21843 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 21844 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 21845 tcp->tcp_num_sack_blk); 21846 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 21847 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 21848 if (max_to_send + sack_opt_len > tcp->tcp_mss) 21849 max_to_send -= sack_opt_len; 21850 } 21851 21852 if (offset != NULL) { 21853 off = *offset; 21854 /* We use offset as an indicator that end_mp is not NULL. */ 21855 *end_mp = NULL; 21856 } 21857 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 21858 /* This could be faster with cooperation from downstream */ 21859 if (mp2 != mp1 && !sendall && 21860 data_length + (int)(mp->b_wptr - mp->b_rptr) > 21861 max_to_send) 21862 /* 21863 * Don't send the next mblk since the whole mblk 21864 * does not fit. 21865 */ 21866 break; 21867 mp2->b_cont = dupb(mp); 21868 mp2 = mp2->b_cont; 21869 if (!mp2) { 21870 freemsg(mp1); 21871 return (NULL); 21872 } 21873 mp2->b_rptr += off; 21874 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 21875 (uintptr_t)INT_MAX); 21876 21877 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 21878 if (data_length > max_to_send) { 21879 mp2->b_wptr -= data_length - max_to_send; 21880 data_length = max_to_send; 21881 off = mp2->b_wptr - mp->b_rptr; 21882 break; 21883 } else { 21884 off = 0; 21885 } 21886 } 21887 if (offset != NULL) { 21888 *offset = off; 21889 *end_mp = mp; 21890 } 21891 if (seg_len != NULL) { 21892 *seg_len = data_length; 21893 } 21894 21895 /* Update the latest receive window size in TCP header. */ 21896 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21897 tcp->tcp_tcph->th_win); 21898 21899 rptr = mp1->b_rptr + tcp_wroff_xtra; 21900 mp1->b_rptr = rptr; 21901 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 21902 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 21903 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 21904 U32_TO_ABE32(seq, tcph->th_seq); 21905 21906 /* 21907 * Use tcp_unsent to determine if the PUSH bit should be used assumes 21908 * that this function was called from tcp_wput_data. Thus, when called 21909 * to retransmit data the setting of the PUSH bit may appear some 21910 * what random in that it might get set when it should not. This 21911 * should not pose any performance issues. 21912 */ 21913 if (data_length != 0 && (tcp->tcp_unsent == 0 || 21914 tcp->tcp_unsent == data_length)) { 21915 flags = TH_ACK | TH_PUSH; 21916 } else { 21917 flags = TH_ACK; 21918 } 21919 21920 if (tcp->tcp_ecn_ok) { 21921 if (tcp->tcp_ecn_echo_on) 21922 flags |= TH_ECE; 21923 21924 /* 21925 * Only set ECT bit and ECN_CWR if a segment contains new data. 21926 * There is no TCP flow control for non-data segments, and 21927 * only data segment is transmitted reliably. 21928 */ 21929 if (data_length > 0 && !rexmit) { 21930 SET_ECT(tcp, rptr); 21931 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 21932 flags |= TH_CWR; 21933 tcp->tcp_ecn_cwr_sent = B_TRUE; 21934 } 21935 } 21936 } 21937 21938 if (tcp->tcp_valid_bits) { 21939 uint32_t u1; 21940 21941 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 21942 seq == tcp->tcp_iss) { 21943 uchar_t *wptr; 21944 21945 /* 21946 * If TCP_ISS_VALID and the seq number is tcp_iss, 21947 * TCP can only be in SYN-SENT, SYN-RCVD or 21948 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 21949 * our SYN is not ack'ed but the app closes this 21950 * TCP connection. 21951 */ 21952 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 21953 tcp->tcp_state == TCPS_SYN_RCVD || 21954 tcp->tcp_state == TCPS_FIN_WAIT_1); 21955 21956 /* 21957 * Tack on the MSS option. It is always needed 21958 * for both active and passive open. 21959 * 21960 * MSS option value should be interface MTU - MIN 21961 * TCP/IP header according to RFC 793 as it means 21962 * the maximum segment size TCP can receive. But 21963 * to get around some broken middle boxes/end hosts 21964 * out there, we allow the option value to be the 21965 * same as the MSS option size on the peer side. 21966 * In this way, the other side will not send 21967 * anything larger than they can receive. 21968 * 21969 * Note that for SYN_SENT state, the ndd param 21970 * tcp_use_smss_as_mss_opt has no effect as we 21971 * don't know the peer's MSS option value. So 21972 * the only case we need to take care of is in 21973 * SYN_RCVD state, which is done later. 21974 */ 21975 wptr = mp1->b_wptr; 21976 wptr[0] = TCPOPT_MAXSEG; 21977 wptr[1] = TCPOPT_MAXSEG_LEN; 21978 wptr += 2; 21979 u1 = tcp->tcp_if_mtu - 21980 (tcp->tcp_ipversion == IPV4_VERSION ? 21981 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 21982 TCP_MIN_HEADER_LENGTH; 21983 U16_TO_BE16(u1, wptr); 21984 mp1->b_wptr = wptr + 2; 21985 /* Update the offset to cover the additional word */ 21986 tcph->th_offset_and_rsrvd[0] += (1 << 4); 21987 21988 /* 21989 * Note that the following way of filling in 21990 * TCP options are not optimal. Some NOPs can 21991 * be saved. But there is no need at this time 21992 * to optimize it. When it is needed, we will 21993 * do it. 21994 */ 21995 switch (tcp->tcp_state) { 21996 case TCPS_SYN_SENT: 21997 flags = TH_SYN; 21998 21999 if (tcp->tcp_snd_ts_ok) { 22000 uint32_t llbolt = (uint32_t)lbolt; 22001 22002 wptr = mp1->b_wptr; 22003 wptr[0] = TCPOPT_NOP; 22004 wptr[1] = TCPOPT_NOP; 22005 wptr[2] = TCPOPT_TSTAMP; 22006 wptr[3] = TCPOPT_TSTAMP_LEN; 22007 wptr += 4; 22008 U32_TO_BE32(llbolt, wptr); 22009 wptr += 4; 22010 ASSERT(tcp->tcp_ts_recent == 0); 22011 U32_TO_BE32(0L, wptr); 22012 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 22013 tcph->th_offset_and_rsrvd[0] += 22014 (3 << 4); 22015 } 22016 22017 /* 22018 * Set up all the bits to tell other side 22019 * we are ECN capable. 22020 */ 22021 if (tcp->tcp_ecn_ok) { 22022 flags |= (TH_ECE | TH_CWR); 22023 } 22024 break; 22025 case TCPS_SYN_RCVD: 22026 flags |= TH_SYN; 22027 22028 /* 22029 * Reset the MSS option value to be SMSS 22030 * We should probably add back the bytes 22031 * for timestamp option and IPsec. We 22032 * don't do that as this is a workaround 22033 * for broken middle boxes/end hosts, it 22034 * is better for us to be more cautious. 22035 * They may not take these things into 22036 * account in their SMSS calculation. Thus 22037 * the peer's calculated SMSS may be smaller 22038 * than what it can be. This should be OK. 22039 */ 22040 if (tcp_use_smss_as_mss_opt) { 22041 u1 = tcp->tcp_mss; 22042 U16_TO_BE16(u1, wptr); 22043 } 22044 22045 /* 22046 * If the other side is ECN capable, reply 22047 * that we are also ECN capable. 22048 */ 22049 if (tcp->tcp_ecn_ok) 22050 flags |= TH_ECE; 22051 break; 22052 default: 22053 /* 22054 * The above ASSERT() makes sure that this 22055 * must be FIN-WAIT-1 state. Our SYN has 22056 * not been ack'ed so retransmit it. 22057 */ 22058 flags |= TH_SYN; 22059 break; 22060 } 22061 22062 if (tcp->tcp_snd_ws_ok) { 22063 wptr = mp1->b_wptr; 22064 wptr[0] = TCPOPT_NOP; 22065 wptr[1] = TCPOPT_WSCALE; 22066 wptr[2] = TCPOPT_WS_LEN; 22067 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 22068 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 22069 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22070 } 22071 22072 if (tcp->tcp_snd_sack_ok) { 22073 wptr = mp1->b_wptr; 22074 wptr[0] = TCPOPT_NOP; 22075 wptr[1] = TCPOPT_NOP; 22076 wptr[2] = TCPOPT_SACK_PERMITTED; 22077 wptr[3] = TCPOPT_SACK_OK_LEN; 22078 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 22079 tcph->th_offset_and_rsrvd[0] += (1 << 4); 22080 } 22081 22082 /* allocb() of adequate mblk assures space */ 22083 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 22084 (uintptr_t)INT_MAX); 22085 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 22086 /* 22087 * Get IP set to checksum on our behalf 22088 * Include the adjustment for a source route if any. 22089 */ 22090 u1 += tcp->tcp_sum; 22091 u1 = (u1 >> 16) + (u1 & 0xFFFF); 22092 U16_TO_BE16(u1, tcph->th_sum); 22093 BUMP_MIB(&tcp_mib, tcpOutControl); 22094 } 22095 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 22096 (seq + data_length) == tcp->tcp_fss) { 22097 if (!tcp->tcp_fin_acked) { 22098 flags |= TH_FIN; 22099 BUMP_MIB(&tcp_mib, tcpOutControl); 22100 } 22101 if (!tcp->tcp_fin_sent) { 22102 tcp->tcp_fin_sent = B_TRUE; 22103 switch (tcp->tcp_state) { 22104 case TCPS_SYN_RCVD: 22105 case TCPS_ESTABLISHED: 22106 tcp->tcp_state = TCPS_FIN_WAIT_1; 22107 break; 22108 case TCPS_CLOSE_WAIT: 22109 tcp->tcp_state = TCPS_LAST_ACK; 22110 break; 22111 } 22112 if (tcp->tcp_suna == tcp->tcp_snxt) 22113 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 22114 tcp->tcp_snxt = tcp->tcp_fss + 1; 22115 } 22116 } 22117 /* 22118 * Note the trick here. u1 is unsigned. When tcp_urg 22119 * is smaller than seq, u1 will become a very huge value. 22120 * So the comparison will fail. Also note that tcp_urp 22121 * should be positive, see RFC 793 page 17. 22122 */ 22123 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 22124 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 22125 u1 < (uint32_t)(64 * 1024)) { 22126 flags |= TH_URG; 22127 BUMP_MIB(&tcp_mib, tcpOutUrg); 22128 U32_TO_ABE16(u1, tcph->th_urp); 22129 } 22130 } 22131 tcph->th_flags[0] = (uchar_t)flags; 22132 tcp->tcp_rack = tcp->tcp_rnxt; 22133 tcp->tcp_rack_cnt = 0; 22134 22135 if (tcp->tcp_snd_ts_ok) { 22136 if (tcp->tcp_state != TCPS_SYN_SENT) { 22137 uint32_t llbolt = (uint32_t)lbolt; 22138 22139 U32_TO_BE32(llbolt, 22140 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22141 U32_TO_BE32(tcp->tcp_ts_recent, 22142 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22143 } 22144 } 22145 22146 if (num_sack_blk > 0) { 22147 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22148 sack_blk_t *tmp; 22149 int32_t i; 22150 22151 wptr[0] = TCPOPT_NOP; 22152 wptr[1] = TCPOPT_NOP; 22153 wptr[2] = TCPOPT_SACK; 22154 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22155 sizeof (sack_blk_t); 22156 wptr += TCPOPT_REAL_SACK_LEN; 22157 22158 tmp = tcp->tcp_sack_list; 22159 for (i = 0; i < num_sack_blk; i++) { 22160 U32_TO_BE32(tmp[i].begin, wptr); 22161 wptr += sizeof (tcp_seq); 22162 U32_TO_BE32(tmp[i].end, wptr); 22163 wptr += sizeof (tcp_seq); 22164 } 22165 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 22166 } 22167 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 22168 data_length += (int)(mp1->b_wptr - rptr); 22169 if (tcp->tcp_ipversion == IPV4_VERSION) { 22170 ((ipha_t *)rptr)->ipha_length = htons(data_length); 22171 } else { 22172 ip6_t *ip6 = (ip6_t *)(rptr + 22173 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22174 sizeof (ip6i_t) : 0)); 22175 22176 ip6->ip6_plen = htons(data_length - 22177 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22178 } 22179 22180 /* 22181 * Prime pump for IP 22182 * Include the adjustment for a source route if any. 22183 */ 22184 data_length -= tcp->tcp_ip_hdr_len; 22185 data_length += tcp->tcp_sum; 22186 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22187 U16_TO_ABE16(data_length, tcph->th_sum); 22188 if (tcp->tcp_ip_forward_progress) { 22189 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22190 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22191 tcp->tcp_ip_forward_progress = B_FALSE; 22192 } 22193 return (mp1); 22194 } 22195 22196 /* This function handles the push timeout. */ 22197 void 22198 tcp_push_timer(void *arg) 22199 { 22200 conn_t *connp = (conn_t *)arg; 22201 tcp_t *tcp = connp->conn_tcp; 22202 22203 TCP_DBGSTAT(tcp_push_timer_cnt); 22204 22205 ASSERT(tcp->tcp_listener == NULL); 22206 22207 /* 22208 * We need to stop synchronous streams temporarily to prevent a race 22209 * with tcp_fuse_rrw() or tcp_fusion rinfop(). It is safe to access 22210 * tcp_rcv_list here because those entry points will return right 22211 * away when synchronous streams is stopped. 22212 */ 22213 TCP_FUSE_SYNCSTR_STOP(tcp); 22214 tcp->tcp_push_tid = 0; 22215 if ((tcp->tcp_rcv_list != NULL) && 22216 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 22217 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 22218 TCP_FUSE_SYNCSTR_RESUME(tcp); 22219 } 22220 22221 /* 22222 * This function handles delayed ACK timeout. 22223 */ 22224 static void 22225 tcp_ack_timer(void *arg) 22226 { 22227 conn_t *connp = (conn_t *)arg; 22228 tcp_t *tcp = connp->conn_tcp; 22229 mblk_t *mp; 22230 22231 TCP_DBGSTAT(tcp_ack_timer_cnt); 22232 22233 tcp->tcp_ack_tid = 0; 22234 22235 if (tcp->tcp_fused) 22236 return; 22237 22238 /* 22239 * Do not send ACK if there is no outstanding unack'ed data. 22240 */ 22241 if (tcp->tcp_rnxt == tcp->tcp_rack) { 22242 return; 22243 } 22244 22245 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 22246 /* 22247 * Make sure we don't allow deferred ACKs to result in 22248 * timer-based ACKing. If we have held off an ACK 22249 * when there was more than an mss here, and the timer 22250 * goes off, we have to worry about the possibility 22251 * that the sender isn't doing slow-start, or is out 22252 * of step with us for some other reason. We fall 22253 * permanently back in the direction of 22254 * ACK-every-other-packet as suggested in RFC 1122. 22255 */ 22256 if (tcp->tcp_rack_abs_max > 2) 22257 tcp->tcp_rack_abs_max--; 22258 tcp->tcp_rack_cur_max = 2; 22259 } 22260 mp = tcp_ack_mp(tcp); 22261 22262 if (mp != NULL) { 22263 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 22264 BUMP_LOCAL(tcp->tcp_obsegs); 22265 BUMP_MIB(&tcp_mib, tcpOutAck); 22266 BUMP_MIB(&tcp_mib, tcpOutAckDelayed); 22267 tcp_send_data(tcp, tcp->tcp_wq, mp); 22268 } 22269 } 22270 22271 22272 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 22273 static mblk_t * 22274 tcp_ack_mp(tcp_t *tcp) 22275 { 22276 uint32_t seq_no; 22277 22278 /* 22279 * There are a few cases to be considered while setting the sequence no. 22280 * Essentially, we can come here while processing an unacceptable pkt 22281 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 22282 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 22283 * If we are here for a zero window probe, stick with suna. In all 22284 * other cases, we check if suna + swnd encompasses snxt and set 22285 * the sequence number to snxt, if so. If snxt falls outside the 22286 * window (the receiver probably shrunk its window), we will go with 22287 * suna + swnd, otherwise the sequence no will be unacceptable to the 22288 * receiver. 22289 */ 22290 if (tcp->tcp_zero_win_probe) { 22291 seq_no = tcp->tcp_suna; 22292 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 22293 ASSERT(tcp->tcp_swnd == 0); 22294 seq_no = tcp->tcp_snxt; 22295 } else { 22296 seq_no = SEQ_GT(tcp->tcp_snxt, 22297 (tcp->tcp_suna + tcp->tcp_swnd)) ? 22298 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 22299 } 22300 22301 if (tcp->tcp_valid_bits) { 22302 /* 22303 * For the complex case where we have to send some 22304 * controls (FIN or SYN), let tcp_xmit_mp do it. 22305 */ 22306 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 22307 NULL, B_FALSE)); 22308 } else { 22309 /* Generate a simple ACK */ 22310 int data_length; 22311 uchar_t *rptr; 22312 tcph_t *tcph; 22313 mblk_t *mp1; 22314 int32_t tcp_hdr_len; 22315 int32_t tcp_tcp_hdr_len; 22316 int32_t num_sack_blk = 0; 22317 int32_t sack_opt_len; 22318 22319 /* 22320 * Allocate space for TCP + IP headers 22321 * and link-level header 22322 */ 22323 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22324 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22325 tcp->tcp_num_sack_blk); 22326 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22327 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22328 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 22329 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 22330 } else { 22331 tcp_hdr_len = tcp->tcp_hdr_len; 22332 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 22333 } 22334 mp1 = allocb(tcp_hdr_len + tcp_wroff_xtra, BPRI_MED); 22335 if (!mp1) 22336 return (NULL); 22337 22338 /* Update the latest receive window size in TCP header. */ 22339 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22340 tcp->tcp_tcph->th_win); 22341 /* copy in prototype TCP + IP header */ 22342 rptr = mp1->b_rptr + tcp_wroff_xtra; 22343 mp1->b_rptr = rptr; 22344 mp1->b_wptr = rptr + tcp_hdr_len; 22345 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22346 22347 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22348 22349 /* Set the TCP sequence number. */ 22350 U32_TO_ABE32(seq_no, tcph->th_seq); 22351 22352 /* Set up the TCP flag field. */ 22353 tcph->th_flags[0] = (uchar_t)TH_ACK; 22354 if (tcp->tcp_ecn_echo_on) 22355 tcph->th_flags[0] |= TH_ECE; 22356 22357 tcp->tcp_rack = tcp->tcp_rnxt; 22358 tcp->tcp_rack_cnt = 0; 22359 22360 /* fill in timestamp option if in use */ 22361 if (tcp->tcp_snd_ts_ok) { 22362 uint32_t llbolt = (uint32_t)lbolt; 22363 22364 U32_TO_BE32(llbolt, 22365 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22366 U32_TO_BE32(tcp->tcp_ts_recent, 22367 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22368 } 22369 22370 /* Fill in SACK options */ 22371 if (num_sack_blk > 0) { 22372 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22373 sack_blk_t *tmp; 22374 int32_t i; 22375 22376 wptr[0] = TCPOPT_NOP; 22377 wptr[1] = TCPOPT_NOP; 22378 wptr[2] = TCPOPT_SACK; 22379 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22380 sizeof (sack_blk_t); 22381 wptr += TCPOPT_REAL_SACK_LEN; 22382 22383 tmp = tcp->tcp_sack_list; 22384 for (i = 0; i < num_sack_blk; i++) { 22385 U32_TO_BE32(tmp[i].begin, wptr); 22386 wptr += sizeof (tcp_seq); 22387 U32_TO_BE32(tmp[i].end, wptr); 22388 wptr += sizeof (tcp_seq); 22389 } 22390 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 22391 << 4); 22392 } 22393 22394 if (tcp->tcp_ipversion == IPV4_VERSION) { 22395 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 22396 } else { 22397 /* Check for ip6i_t header in sticky hdrs */ 22398 ip6_t *ip6 = (ip6_t *)(rptr + 22399 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22400 sizeof (ip6i_t) : 0)); 22401 22402 ip6->ip6_plen = htons(tcp_hdr_len - 22403 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22404 } 22405 22406 /* 22407 * Prime pump for checksum calculation in IP. Include the 22408 * adjustment for a source route if any. 22409 */ 22410 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 22411 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22412 U16_TO_ABE16(data_length, tcph->th_sum); 22413 22414 if (tcp->tcp_ip_forward_progress) { 22415 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22416 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22417 tcp->tcp_ip_forward_progress = B_FALSE; 22418 } 22419 return (mp1); 22420 } 22421 } 22422 22423 /* 22424 * To create a temporary tcp structure for inserting into bind hash list. 22425 * The parameter is assumed to be in network byte order, ready for use. 22426 */ 22427 /* ARGSUSED */ 22428 static tcp_t * 22429 tcp_alloc_temp_tcp(in_port_t port) 22430 { 22431 conn_t *connp; 22432 tcp_t *tcp; 22433 22434 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP); 22435 if (connp == NULL) 22436 return (NULL); 22437 22438 tcp = connp->conn_tcp; 22439 22440 /* 22441 * Only initialize the necessary info in those structures. Note 22442 * that since INADDR_ANY is all 0, we do not need to set 22443 * tcp_bound_source to INADDR_ANY here. 22444 */ 22445 tcp->tcp_state = TCPS_BOUND; 22446 tcp->tcp_lport = port; 22447 tcp->tcp_exclbind = 1; 22448 tcp->tcp_reserved_port = 1; 22449 22450 /* Just for place holding... */ 22451 tcp->tcp_ipversion = IPV4_VERSION; 22452 22453 return (tcp); 22454 } 22455 22456 /* 22457 * To remove a port range specified by lo_port and hi_port from the 22458 * reserved port ranges. This is one of the three public functions of 22459 * the reserved port interface. Note that a port range has to be removed 22460 * as a whole. Ports in a range cannot be removed individually. 22461 * 22462 * Params: 22463 * in_port_t lo_port: the beginning port of the reserved port range to 22464 * be deleted. 22465 * in_port_t hi_port: the ending port of the reserved port range to 22466 * be deleted. 22467 * 22468 * Return: 22469 * B_TRUE if the deletion is successful, B_FALSE otherwise. 22470 */ 22471 boolean_t 22472 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 22473 { 22474 int i, j; 22475 int size; 22476 tcp_t **temp_tcp_array; 22477 tcp_t *tcp; 22478 22479 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 22480 22481 /* First make sure that the port ranage is indeed reserved. */ 22482 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22483 if (tcp_reserved_port[i].lo_port == lo_port) { 22484 hi_port = tcp_reserved_port[i].hi_port; 22485 temp_tcp_array = tcp_reserved_port[i].temp_tcp_array; 22486 break; 22487 } 22488 } 22489 if (i == tcp_reserved_port_array_size) { 22490 rw_exit(&tcp_reserved_port_lock); 22491 return (B_FALSE); 22492 } 22493 22494 /* 22495 * Remove the range from the array. This simple loop is possible 22496 * because port ranges are inserted in ascending order. 22497 */ 22498 for (j = i; j < tcp_reserved_port_array_size - 1; j++) { 22499 tcp_reserved_port[j].lo_port = tcp_reserved_port[j+1].lo_port; 22500 tcp_reserved_port[j].hi_port = tcp_reserved_port[j+1].hi_port; 22501 tcp_reserved_port[j].temp_tcp_array = 22502 tcp_reserved_port[j+1].temp_tcp_array; 22503 } 22504 22505 /* Remove all the temporary tcp structures. */ 22506 size = hi_port - lo_port + 1; 22507 while (size > 0) { 22508 tcp = temp_tcp_array[size - 1]; 22509 ASSERT(tcp != NULL); 22510 tcp_bind_hash_remove(tcp); 22511 CONN_DEC_REF(tcp->tcp_connp); 22512 size--; 22513 } 22514 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 22515 tcp_reserved_port_array_size--; 22516 rw_exit(&tcp_reserved_port_lock); 22517 return (B_TRUE); 22518 } 22519 22520 /* 22521 * Macro to remove temporary tcp structure from the bind hash list. The 22522 * first parameter is the list of tcp to be removed. The second parameter 22523 * is the number of tcps in the array. 22524 */ 22525 #define TCP_TMP_TCP_REMOVE(tcp_array, num) \ 22526 { \ 22527 while ((num) > 0) { \ 22528 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 22529 tf_t *tbf; \ 22530 tcp_t *tcpnext; \ 22531 tbf = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 22532 mutex_enter(&tbf->tf_lock); \ 22533 tcpnext = tcp->tcp_bind_hash; \ 22534 if (tcpnext) { \ 22535 tcpnext->tcp_ptpbhn = \ 22536 tcp->tcp_ptpbhn; \ 22537 } \ 22538 *tcp->tcp_ptpbhn = tcpnext; \ 22539 mutex_exit(&tbf->tf_lock); \ 22540 kmem_free(tcp, sizeof (tcp_t)); \ 22541 (tcp_array)[(num) - 1] = NULL; \ 22542 (num)--; \ 22543 } \ 22544 } 22545 22546 /* 22547 * The public interface for other modules to call to reserve a port range 22548 * in TCP. The caller passes in how large a port range it wants. TCP 22549 * will try to find a range and return it via lo_port and hi_port. This is 22550 * used by NCA's nca_conn_init. 22551 * NCA can only be used in the global zone so this only affects the global 22552 * zone's ports. 22553 * 22554 * Params: 22555 * int size: the size of the port range to be reserved. 22556 * in_port_t *lo_port (referenced): returns the beginning port of the 22557 * reserved port range added. 22558 * in_port_t *hi_port (referenced): returns the ending port of the 22559 * reserved port range added. 22560 * 22561 * Return: 22562 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 22563 */ 22564 boolean_t 22565 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 22566 { 22567 tcp_t *tcp; 22568 tcp_t *tmp_tcp; 22569 tcp_t **temp_tcp_array; 22570 tf_t *tbf; 22571 in_port_t net_port; 22572 in_port_t port; 22573 int32_t cur_size; 22574 int i, j; 22575 boolean_t used; 22576 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 22577 zoneid_t zoneid = GLOBAL_ZONEID; 22578 22579 /* Sanity check. */ 22580 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 22581 return (B_FALSE); 22582 } 22583 22584 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 22585 if (tcp_reserved_port_array_size == TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 22586 rw_exit(&tcp_reserved_port_lock); 22587 return (B_FALSE); 22588 } 22589 22590 /* 22591 * Find the starting port to try. Since the port ranges are ordered 22592 * in the reserved port array, we can do a simple search here. 22593 */ 22594 *lo_port = TCP_SMALLEST_RESERVED_PORT; 22595 *hi_port = TCP_LARGEST_RESERVED_PORT; 22596 for (i = 0; i < tcp_reserved_port_array_size; 22597 *lo_port = tcp_reserved_port[i].hi_port + 1, i++) { 22598 if (tcp_reserved_port[i].lo_port - *lo_port >= size) { 22599 *hi_port = tcp_reserved_port[i].lo_port - 1; 22600 break; 22601 } 22602 } 22603 /* No available port range. */ 22604 if (i == tcp_reserved_port_array_size && *hi_port - *lo_port < size) { 22605 rw_exit(&tcp_reserved_port_lock); 22606 return (B_FALSE); 22607 } 22608 22609 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 22610 if (temp_tcp_array == NULL) { 22611 rw_exit(&tcp_reserved_port_lock); 22612 return (B_FALSE); 22613 } 22614 22615 /* Go thru the port range to see if some ports are already bound. */ 22616 for (port = *lo_port, cur_size = 0; 22617 cur_size < size && port <= *hi_port; 22618 cur_size++, port++) { 22619 used = B_FALSE; 22620 net_port = htons(port); 22621 tbf = &tcp_bind_fanout[TCP_BIND_HASH(net_port)]; 22622 mutex_enter(&tbf->tf_lock); 22623 for (tcp = tbf->tf_tcp; tcp != NULL; 22624 tcp = tcp->tcp_bind_hash) { 22625 if (IPCL_ZONE_MATCH(tcp->tcp_connp, zoneid) && 22626 net_port == tcp->tcp_lport) { 22627 /* 22628 * A port is already bound. Search again 22629 * starting from port + 1. Release all 22630 * temporary tcps. 22631 */ 22632 mutex_exit(&tbf->tf_lock); 22633 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22634 *lo_port = port + 1; 22635 cur_size = -1; 22636 used = B_TRUE; 22637 break; 22638 } 22639 } 22640 if (!used) { 22641 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port)) == NULL) { 22642 /* 22643 * Allocation failure. Just fail the request. 22644 * Need to remove all those temporary tcp 22645 * structures. 22646 */ 22647 mutex_exit(&tbf->tf_lock); 22648 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22649 rw_exit(&tcp_reserved_port_lock); 22650 kmem_free(temp_tcp_array, 22651 (hi_port - lo_port + 1) * 22652 sizeof (tcp_t *)); 22653 return (B_FALSE); 22654 } 22655 temp_tcp_array[cur_size] = tmp_tcp; 22656 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 22657 mutex_exit(&tbf->tf_lock); 22658 } 22659 } 22660 22661 /* 22662 * The current range is not large enough. We can actually do another 22663 * search if this search is done between 2 reserved port ranges. But 22664 * for first release, we just stop here and return saying that no port 22665 * range is available. 22666 */ 22667 if (cur_size < size) { 22668 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22669 rw_exit(&tcp_reserved_port_lock); 22670 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 22671 return (B_FALSE); 22672 } 22673 *hi_port = port - 1; 22674 22675 /* 22676 * Insert range into array in ascending order. Since this function 22677 * must not be called often, we choose to use the simplest method. 22678 * The above array should not consume excessive stack space as 22679 * the size must be very small. If in future releases, we find 22680 * that we should provide more reserved port ranges, this function 22681 * has to be modified to be more efficient. 22682 */ 22683 if (tcp_reserved_port_array_size == 0) { 22684 tcp_reserved_port[0].lo_port = *lo_port; 22685 tcp_reserved_port[0].hi_port = *hi_port; 22686 tcp_reserved_port[0].temp_tcp_array = temp_tcp_array; 22687 } else { 22688 for (i = 0, j = 0; i < tcp_reserved_port_array_size; i++, j++) { 22689 if (*lo_port < tcp_reserved_port[i].lo_port && i == j) { 22690 tmp_ports[j].lo_port = *lo_port; 22691 tmp_ports[j].hi_port = *hi_port; 22692 tmp_ports[j].temp_tcp_array = temp_tcp_array; 22693 j++; 22694 } 22695 tmp_ports[j].lo_port = tcp_reserved_port[i].lo_port; 22696 tmp_ports[j].hi_port = tcp_reserved_port[i].hi_port; 22697 tmp_ports[j].temp_tcp_array = 22698 tcp_reserved_port[i].temp_tcp_array; 22699 } 22700 if (j == i) { 22701 tmp_ports[j].lo_port = *lo_port; 22702 tmp_ports[j].hi_port = *hi_port; 22703 tmp_ports[j].temp_tcp_array = temp_tcp_array; 22704 } 22705 bcopy(tmp_ports, tcp_reserved_port, sizeof (tmp_ports)); 22706 } 22707 tcp_reserved_port_array_size++; 22708 rw_exit(&tcp_reserved_port_lock); 22709 return (B_TRUE); 22710 } 22711 22712 /* 22713 * Check to see if a port is in any reserved port range. 22714 * 22715 * Params: 22716 * in_port_t port: the port to be verified. 22717 * 22718 * Return: 22719 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 22720 */ 22721 boolean_t 22722 tcp_reserved_port_check(in_port_t port) 22723 { 22724 int i; 22725 22726 rw_enter(&tcp_reserved_port_lock, RW_READER); 22727 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22728 if (port >= tcp_reserved_port[i].lo_port || 22729 port <= tcp_reserved_port[i].hi_port) { 22730 rw_exit(&tcp_reserved_port_lock); 22731 return (B_TRUE); 22732 } 22733 } 22734 rw_exit(&tcp_reserved_port_lock); 22735 return (B_FALSE); 22736 } 22737 22738 /* 22739 * To list all reserved port ranges. This is the function to handle 22740 * ndd tcp_reserved_port_list. 22741 */ 22742 /* ARGSUSED */ 22743 static int 22744 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 22745 { 22746 int i; 22747 22748 rw_enter(&tcp_reserved_port_lock, RW_READER); 22749 if (tcp_reserved_port_array_size > 0) 22750 (void) mi_mpprintf(mp, "The following ports are reserved:"); 22751 else 22752 (void) mi_mpprintf(mp, "No port is reserved."); 22753 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22754 (void) mi_mpprintf(mp, "%d-%d", 22755 tcp_reserved_port[i].lo_port, tcp_reserved_port[i].hi_port); 22756 } 22757 rw_exit(&tcp_reserved_port_lock); 22758 return (0); 22759 } 22760 22761 /* 22762 * Hash list insertion routine for tcp_t structures. 22763 * Inserts entries with the ones bound to a specific IP address first 22764 * followed by those bound to INADDR_ANY. 22765 */ 22766 static void 22767 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 22768 { 22769 tcp_t **tcpp; 22770 tcp_t *tcpnext; 22771 22772 if (tcp->tcp_ptpbhn != NULL) { 22773 ASSERT(!caller_holds_lock); 22774 tcp_bind_hash_remove(tcp); 22775 } 22776 tcpp = &tbf->tf_tcp; 22777 if (!caller_holds_lock) { 22778 mutex_enter(&tbf->tf_lock); 22779 } else { 22780 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 22781 } 22782 tcpnext = tcpp[0]; 22783 if (tcpnext) { 22784 /* 22785 * If the new tcp bound to the INADDR_ANY address 22786 * and the first one in the list is not bound to 22787 * INADDR_ANY we skip all entries until we find the 22788 * first one bound to INADDR_ANY. 22789 * This makes sure that applications binding to a 22790 * specific address get preference over those binding to 22791 * INADDR_ANY. 22792 */ 22793 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 22794 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 22795 while ((tcpnext = tcpp[0]) != NULL && 22796 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 22797 tcpp = &(tcpnext->tcp_bind_hash); 22798 if (tcpnext) 22799 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 22800 } else 22801 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 22802 } 22803 tcp->tcp_bind_hash = tcpnext; 22804 tcp->tcp_ptpbhn = tcpp; 22805 tcpp[0] = tcp; 22806 if (!caller_holds_lock) 22807 mutex_exit(&tbf->tf_lock); 22808 } 22809 22810 /* 22811 * Hash list removal routine for tcp_t structures. 22812 */ 22813 static void 22814 tcp_bind_hash_remove(tcp_t *tcp) 22815 { 22816 tcp_t *tcpnext; 22817 kmutex_t *lockp; 22818 22819 if (tcp->tcp_ptpbhn == NULL) 22820 return; 22821 22822 /* 22823 * Extract the lock pointer in case there are concurrent 22824 * hash_remove's for this instance. 22825 */ 22826 ASSERT(tcp->tcp_lport != 0); 22827 lockp = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 22828 22829 ASSERT(lockp != NULL); 22830 mutex_enter(lockp); 22831 if (tcp->tcp_ptpbhn) { 22832 tcpnext = tcp->tcp_bind_hash; 22833 if (tcpnext) { 22834 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 22835 tcp->tcp_bind_hash = NULL; 22836 } 22837 *tcp->tcp_ptpbhn = tcpnext; 22838 tcp->tcp_ptpbhn = NULL; 22839 } 22840 mutex_exit(lockp); 22841 } 22842 22843 22844 /* 22845 * Hash list lookup routine for tcp_t structures. 22846 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 22847 */ 22848 static tcp_t * 22849 tcp_acceptor_hash_lookup(t_uscalar_t id) 22850 { 22851 tf_t *tf; 22852 tcp_t *tcp; 22853 22854 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 22855 mutex_enter(&tf->tf_lock); 22856 for (tcp = tf->tf_tcp; tcp != NULL; 22857 tcp = tcp->tcp_acceptor_hash) { 22858 if (tcp->tcp_acceptor_id == id) { 22859 CONN_INC_REF(tcp->tcp_connp); 22860 mutex_exit(&tf->tf_lock); 22861 return (tcp); 22862 } 22863 } 22864 mutex_exit(&tf->tf_lock); 22865 return (NULL); 22866 } 22867 22868 22869 /* 22870 * Hash list insertion routine for tcp_t structures. 22871 */ 22872 void 22873 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 22874 { 22875 tf_t *tf; 22876 tcp_t **tcpp; 22877 tcp_t *tcpnext; 22878 22879 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 22880 22881 if (tcp->tcp_ptpahn != NULL) 22882 tcp_acceptor_hash_remove(tcp); 22883 tcpp = &tf->tf_tcp; 22884 mutex_enter(&tf->tf_lock); 22885 tcpnext = tcpp[0]; 22886 if (tcpnext) 22887 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 22888 tcp->tcp_acceptor_hash = tcpnext; 22889 tcp->tcp_ptpahn = tcpp; 22890 tcpp[0] = tcp; 22891 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 22892 mutex_exit(&tf->tf_lock); 22893 } 22894 22895 /* 22896 * Hash list removal routine for tcp_t structures. 22897 */ 22898 static void 22899 tcp_acceptor_hash_remove(tcp_t *tcp) 22900 { 22901 tcp_t *tcpnext; 22902 kmutex_t *lockp; 22903 22904 /* 22905 * Extract the lock pointer in case there are concurrent 22906 * hash_remove's for this instance. 22907 */ 22908 lockp = tcp->tcp_acceptor_lockp; 22909 22910 if (tcp->tcp_ptpahn == NULL) 22911 return; 22912 22913 ASSERT(lockp != NULL); 22914 mutex_enter(lockp); 22915 if (tcp->tcp_ptpahn) { 22916 tcpnext = tcp->tcp_acceptor_hash; 22917 if (tcpnext) { 22918 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 22919 tcp->tcp_acceptor_hash = NULL; 22920 } 22921 *tcp->tcp_ptpahn = tcpnext; 22922 tcp->tcp_ptpahn = NULL; 22923 } 22924 mutex_exit(lockp); 22925 tcp->tcp_acceptor_lockp = NULL; 22926 } 22927 22928 /* ARGSUSED */ 22929 static int 22930 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 22931 { 22932 int error = 0; 22933 int retval; 22934 char *end; 22935 22936 tcp_hsp_t *hsp; 22937 tcp_hsp_t *hspprev; 22938 22939 ipaddr_t addr = 0; /* Address we're looking for */ 22940 in6_addr_t v6addr; /* Address we're looking for */ 22941 uint32_t hash; /* Hash of that address */ 22942 22943 /* 22944 * If the following variables are still zero after parsing the input 22945 * string, the user didn't specify them and we don't change them in 22946 * the HSP. 22947 */ 22948 22949 ipaddr_t mask = 0; /* Subnet mask */ 22950 in6_addr_t v6mask; 22951 long sendspace = 0; /* Send buffer size */ 22952 long recvspace = 0; /* Receive buffer size */ 22953 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 22954 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 22955 22956 rw_enter(&tcp_hsp_lock, RW_WRITER); 22957 22958 /* Parse and validate address */ 22959 if (af == AF_INET) { 22960 retval = inet_pton(af, value, &addr); 22961 if (retval == 1) 22962 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 22963 } else if (af == AF_INET6) { 22964 retval = inet_pton(af, value, &v6addr); 22965 } else { 22966 error = EINVAL; 22967 goto done; 22968 } 22969 if (retval == 0) { 22970 error = EINVAL; 22971 goto done; 22972 } 22973 22974 while ((*value) && *value != ' ') 22975 value++; 22976 22977 /* Parse individual keywords, set variables if found */ 22978 while (*value) { 22979 /* Skip leading blanks */ 22980 22981 while (*value == ' ' || *value == '\t') 22982 value++; 22983 22984 /* If at end of string, we're done */ 22985 22986 if (!*value) 22987 break; 22988 22989 /* We have a word, figure out what it is */ 22990 22991 if (strncmp("mask", value, 4) == 0) { 22992 value += 4; 22993 while (*value == ' ' || *value == '\t') 22994 value++; 22995 /* Parse subnet mask */ 22996 if (af == AF_INET) { 22997 retval = inet_pton(af, value, &mask); 22998 if (retval == 1) { 22999 V4MASK_TO_V6(mask, v6mask); 23000 } 23001 } else if (af == AF_INET6) { 23002 retval = inet_pton(af, value, &v6mask); 23003 } 23004 if (retval != 1) { 23005 error = EINVAL; 23006 goto done; 23007 } 23008 while ((*value) && *value != ' ') 23009 value++; 23010 } else if (strncmp("sendspace", value, 9) == 0) { 23011 value += 9; 23012 23013 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 23014 sendspace < TCP_XMIT_HIWATER || 23015 sendspace >= (1L<<30)) { 23016 error = EINVAL; 23017 goto done; 23018 } 23019 value = end; 23020 } else if (strncmp("recvspace", value, 9) == 0) { 23021 value += 9; 23022 23023 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 23024 recvspace < TCP_RECV_HIWATER || 23025 recvspace >= (1L<<30)) { 23026 error = EINVAL; 23027 goto done; 23028 } 23029 value = end; 23030 } else if (strncmp("timestamp", value, 9) == 0) { 23031 value += 9; 23032 23033 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 23034 timestamp < 0 || timestamp > 1) { 23035 error = EINVAL; 23036 goto done; 23037 } 23038 23039 /* 23040 * We increment timestamp so we know it's been set; 23041 * this is undone when we put it in the HSP 23042 */ 23043 timestamp++; 23044 value = end; 23045 } else if (strncmp("delete", value, 6) == 0) { 23046 value += 6; 23047 delete = B_TRUE; 23048 } else { 23049 error = EINVAL; 23050 goto done; 23051 } 23052 } 23053 23054 /* Hash address for lookup */ 23055 23056 hash = TCP_HSP_HASH(addr); 23057 23058 if (delete) { 23059 /* 23060 * Note that deletes don't return an error if the thing 23061 * we're trying to delete isn't there. 23062 */ 23063 if (tcp_hsp_hash == NULL) 23064 goto done; 23065 hsp = tcp_hsp_hash[hash]; 23066 23067 if (hsp) { 23068 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 23069 &v6addr)) { 23070 tcp_hsp_hash[hash] = hsp->tcp_hsp_next; 23071 mi_free((char *)hsp); 23072 } else { 23073 hspprev = hsp; 23074 while ((hsp = hsp->tcp_hsp_next) != NULL) { 23075 if (IN6_ARE_ADDR_EQUAL( 23076 &hsp->tcp_hsp_addr_v6, &v6addr)) { 23077 hspprev->tcp_hsp_next = 23078 hsp->tcp_hsp_next; 23079 mi_free((char *)hsp); 23080 break; 23081 } 23082 hspprev = hsp; 23083 } 23084 } 23085 } 23086 } else { 23087 /* 23088 * We're adding/modifying an HSP. If we haven't already done 23089 * so, allocate the hash table. 23090 */ 23091 23092 if (!tcp_hsp_hash) { 23093 tcp_hsp_hash = (tcp_hsp_t **) 23094 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 23095 if (!tcp_hsp_hash) { 23096 error = EINVAL; 23097 goto done; 23098 } 23099 } 23100 23101 /* Get head of hash chain */ 23102 23103 hsp = tcp_hsp_hash[hash]; 23104 23105 /* Try to find pre-existing hsp on hash chain */ 23106 /* Doesn't handle CIDR prefixes. */ 23107 while (hsp) { 23108 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 23109 break; 23110 hsp = hsp->tcp_hsp_next; 23111 } 23112 23113 /* 23114 * If we didn't, create one with default values and put it 23115 * at head of hash chain 23116 */ 23117 23118 if (!hsp) { 23119 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 23120 if (!hsp) { 23121 error = EINVAL; 23122 goto done; 23123 } 23124 hsp->tcp_hsp_next = tcp_hsp_hash[hash]; 23125 tcp_hsp_hash[hash] = hsp; 23126 } 23127 23128 /* Set values that the user asked us to change */ 23129 23130 hsp->tcp_hsp_addr_v6 = v6addr; 23131 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 23132 hsp->tcp_hsp_vers = IPV4_VERSION; 23133 else 23134 hsp->tcp_hsp_vers = IPV6_VERSION; 23135 hsp->tcp_hsp_subnet_v6 = v6mask; 23136 if (sendspace > 0) 23137 hsp->tcp_hsp_sendspace = sendspace; 23138 if (recvspace > 0) 23139 hsp->tcp_hsp_recvspace = recvspace; 23140 if (timestamp > 0) 23141 hsp->tcp_hsp_tstamp = timestamp - 1; 23142 } 23143 23144 done: 23145 rw_exit(&tcp_hsp_lock); 23146 return (error); 23147 } 23148 23149 /* Set callback routine passed to nd_load by tcp_param_register. */ 23150 /* ARGSUSED */ 23151 static int 23152 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 23153 { 23154 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 23155 } 23156 /* ARGSUSED */ 23157 static int 23158 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23159 cred_t *cr) 23160 { 23161 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 23162 } 23163 23164 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 23165 /* ARGSUSED */ 23166 static int 23167 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 23168 { 23169 tcp_hsp_t *hsp; 23170 int i; 23171 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 23172 23173 rw_enter(&tcp_hsp_lock, RW_READER); 23174 (void) mi_mpprintf(mp, 23175 "Hash HSP " MI_COL_HDRPAD_STR 23176 "Address Subnet Mask Send Receive TStamp"); 23177 if (tcp_hsp_hash) { 23178 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 23179 hsp = tcp_hsp_hash[i]; 23180 while (hsp) { 23181 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 23182 (void) inet_ntop(AF_INET, 23183 &hsp->tcp_hsp_addr, 23184 addrbuf, sizeof (addrbuf)); 23185 (void) inet_ntop(AF_INET, 23186 &hsp->tcp_hsp_subnet, 23187 subnetbuf, sizeof (subnetbuf)); 23188 } else { 23189 (void) inet_ntop(AF_INET6, 23190 &hsp->tcp_hsp_addr_v6, 23191 addrbuf, sizeof (addrbuf)); 23192 (void) inet_ntop(AF_INET6, 23193 &hsp->tcp_hsp_subnet_v6, 23194 subnetbuf, sizeof (subnetbuf)); 23195 } 23196 (void) mi_mpprintf(mp, 23197 " %03d " MI_COL_PTRFMT_STR 23198 "%s %s %010d %010d %d", 23199 i, 23200 (void *)hsp, 23201 addrbuf, 23202 subnetbuf, 23203 hsp->tcp_hsp_sendspace, 23204 hsp->tcp_hsp_recvspace, 23205 hsp->tcp_hsp_tstamp); 23206 23207 hsp = hsp->tcp_hsp_next; 23208 } 23209 } 23210 } 23211 rw_exit(&tcp_hsp_lock); 23212 return (0); 23213 } 23214 23215 23216 /* Data for fast netmask macro used by tcp_hsp_lookup */ 23217 23218 static ipaddr_t netmasks[] = { 23219 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 23220 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 23221 }; 23222 23223 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 23224 23225 /* 23226 * XXX This routine should go away and instead we should use the metrics 23227 * associated with the routes to determine the default sndspace and rcvspace. 23228 */ 23229 static tcp_hsp_t * 23230 tcp_hsp_lookup(ipaddr_t addr) 23231 { 23232 tcp_hsp_t *hsp = NULL; 23233 23234 /* Quick check without acquiring the lock. */ 23235 if (tcp_hsp_hash == NULL) 23236 return (NULL); 23237 23238 rw_enter(&tcp_hsp_lock, RW_READER); 23239 23240 /* This routine finds the best-matching HSP for address addr. */ 23241 23242 if (tcp_hsp_hash) { 23243 int i; 23244 ipaddr_t srchaddr; 23245 tcp_hsp_t *hsp_net; 23246 23247 /* We do three passes: host, network, and subnet. */ 23248 23249 srchaddr = addr; 23250 23251 for (i = 1; i <= 3; i++) { 23252 /* Look for exact match on srchaddr */ 23253 23254 hsp = tcp_hsp_hash[TCP_HSP_HASH(srchaddr)]; 23255 while (hsp) { 23256 if (hsp->tcp_hsp_vers == IPV4_VERSION && 23257 hsp->tcp_hsp_addr == srchaddr) 23258 break; 23259 hsp = hsp->tcp_hsp_next; 23260 } 23261 ASSERT(hsp == NULL || 23262 hsp->tcp_hsp_vers == IPV4_VERSION); 23263 23264 /* 23265 * If this is the first pass: 23266 * If we found a match, great, return it. 23267 * If not, search for the network on the second pass. 23268 */ 23269 23270 if (i == 1) 23271 if (hsp) 23272 break; 23273 else 23274 { 23275 srchaddr = addr & netmask(addr); 23276 continue; 23277 } 23278 23279 /* 23280 * If this is the second pass: 23281 * If we found a match, but there's a subnet mask, 23282 * save the match but try again using the subnet 23283 * mask on the third pass. 23284 * Otherwise, return whatever we found. 23285 */ 23286 23287 if (i == 2) { 23288 if (hsp && hsp->tcp_hsp_subnet) { 23289 hsp_net = hsp; 23290 srchaddr = addr & hsp->tcp_hsp_subnet; 23291 continue; 23292 } else { 23293 break; 23294 } 23295 } 23296 23297 /* 23298 * This must be the third pass. If we didn't find 23299 * anything, return the saved network HSP instead. 23300 */ 23301 23302 if (!hsp) 23303 hsp = hsp_net; 23304 } 23305 } 23306 23307 rw_exit(&tcp_hsp_lock); 23308 return (hsp); 23309 } 23310 23311 /* 23312 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 23313 * match lookup. 23314 */ 23315 static tcp_hsp_t * 23316 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr) 23317 { 23318 tcp_hsp_t *hsp = NULL; 23319 23320 /* Quick check without acquiring the lock. */ 23321 if (tcp_hsp_hash == NULL) 23322 return (NULL); 23323 23324 rw_enter(&tcp_hsp_lock, RW_READER); 23325 23326 /* This routine finds the best-matching HSP for address addr. */ 23327 23328 if (tcp_hsp_hash) { 23329 int i; 23330 in6_addr_t v6srchaddr; 23331 tcp_hsp_t *hsp_net; 23332 23333 /* We do three passes: host, network, and subnet. */ 23334 23335 v6srchaddr = *v6addr; 23336 23337 for (i = 1; i <= 3; i++) { 23338 /* Look for exact match on srchaddr */ 23339 23340 hsp = tcp_hsp_hash[TCP_HSP_HASH( 23341 V4_PART_OF_V6(v6srchaddr))]; 23342 while (hsp) { 23343 if (hsp->tcp_hsp_vers == IPV6_VERSION && 23344 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 23345 &v6srchaddr)) 23346 break; 23347 hsp = hsp->tcp_hsp_next; 23348 } 23349 23350 /* 23351 * If this is the first pass: 23352 * If we found a match, great, return it. 23353 * If not, search for the network on the second pass. 23354 */ 23355 23356 if (i == 1) 23357 if (hsp) 23358 break; 23359 else { 23360 /* Assume a 64 bit mask */ 23361 v6srchaddr.s6_addr32[0] = 23362 v6addr->s6_addr32[0]; 23363 v6srchaddr.s6_addr32[1] = 23364 v6addr->s6_addr32[1]; 23365 v6srchaddr.s6_addr32[2] = 0; 23366 v6srchaddr.s6_addr32[3] = 0; 23367 continue; 23368 } 23369 23370 /* 23371 * If this is the second pass: 23372 * If we found a match, but there's a subnet mask, 23373 * save the match but try again using the subnet 23374 * mask on the third pass. 23375 * Otherwise, return whatever we found. 23376 */ 23377 23378 if (i == 2) { 23379 ASSERT(hsp == NULL || 23380 hsp->tcp_hsp_vers == IPV6_VERSION); 23381 if (hsp && 23382 !IN6_IS_ADDR_UNSPECIFIED( 23383 &hsp->tcp_hsp_subnet_v6)) { 23384 hsp_net = hsp; 23385 V6_MASK_COPY(*v6addr, 23386 hsp->tcp_hsp_subnet_v6, v6srchaddr); 23387 continue; 23388 } else { 23389 break; 23390 } 23391 } 23392 23393 /* 23394 * This must be the third pass. If we didn't find 23395 * anything, return the saved network HSP instead. 23396 */ 23397 23398 if (!hsp) 23399 hsp = hsp_net; 23400 } 23401 } 23402 23403 rw_exit(&tcp_hsp_lock); 23404 return (hsp); 23405 } 23406 23407 /* 23408 * Type three generator adapted from the random() function in 4.4 BSD: 23409 */ 23410 23411 /* 23412 * Copyright (c) 1983, 1993 23413 * The Regents of the University of California. All rights reserved. 23414 * 23415 * Redistribution and use in source and binary forms, with or without 23416 * modification, are permitted provided that the following conditions 23417 * are met: 23418 * 1. Redistributions of source code must retain the above copyright 23419 * notice, this list of conditions and the following disclaimer. 23420 * 2. Redistributions in binary form must reproduce the above copyright 23421 * notice, this list of conditions and the following disclaimer in the 23422 * documentation and/or other materials provided with the distribution. 23423 * 3. All advertising materials mentioning features or use of this software 23424 * must display the following acknowledgement: 23425 * This product includes software developed by the University of 23426 * California, Berkeley and its contributors. 23427 * 4. Neither the name of the University nor the names of its contributors 23428 * may be used to endorse or promote products derived from this software 23429 * without specific prior written permission. 23430 * 23431 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23432 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23433 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23434 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23435 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23436 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23437 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23438 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23439 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23440 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23441 * SUCH DAMAGE. 23442 */ 23443 23444 /* Type 3 -- x**31 + x**3 + 1 */ 23445 #define DEG_3 31 23446 #define SEP_3 3 23447 23448 23449 /* Protected by tcp_random_lock */ 23450 static int tcp_randtbl[DEG_3 + 1]; 23451 23452 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23453 static int *tcp_random_rptr = &tcp_randtbl[1]; 23454 23455 static int *tcp_random_state = &tcp_randtbl[1]; 23456 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23457 23458 kmutex_t tcp_random_lock; 23459 23460 void 23461 tcp_random_init(void) 23462 { 23463 int i; 23464 hrtime_t hrt; 23465 time_t wallclock; 23466 uint64_t result; 23467 23468 /* 23469 * Use high-res timer and current time for seed. Gethrtime() returns 23470 * a longlong, which may contain resolution down to nanoseconds. 23471 * The current time will either be a 32-bit or a 64-bit quantity. 23472 * XOR the two together in a 64-bit result variable. 23473 * Convert the result to a 32-bit value by multiplying the high-order 23474 * 32-bits by the low-order 32-bits. 23475 */ 23476 23477 hrt = gethrtime(); 23478 (void) drv_getparm(TIME, &wallclock); 23479 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23480 mutex_enter(&tcp_random_lock); 23481 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23482 (result & 0xffffffff); 23483 23484 for (i = 1; i < DEG_3; i++) 23485 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23486 + 12345; 23487 tcp_random_fptr = &tcp_random_state[SEP_3]; 23488 tcp_random_rptr = &tcp_random_state[0]; 23489 mutex_exit(&tcp_random_lock); 23490 for (i = 0; i < 10 * DEG_3; i++) 23491 (void) tcp_random(); 23492 } 23493 23494 /* 23495 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23496 * This range is selected to be approximately centered on TCP_ISS / 2, 23497 * and easy to compute. We get this value by generating a 32-bit random 23498 * number, selecting out the high-order 17 bits, and then adding one so 23499 * that we never return zero. 23500 */ 23501 int 23502 tcp_random(void) 23503 { 23504 int i; 23505 23506 mutex_enter(&tcp_random_lock); 23507 *tcp_random_fptr += *tcp_random_rptr; 23508 23509 /* 23510 * The high-order bits are more random than the low-order bits, 23511 * so we select out the high-order 17 bits and add one so that 23512 * we never return zero. 23513 */ 23514 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23515 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23516 tcp_random_fptr = tcp_random_state; 23517 ++tcp_random_rptr; 23518 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23519 tcp_random_rptr = tcp_random_state; 23520 23521 mutex_exit(&tcp_random_lock); 23522 return (i); 23523 } 23524 23525 /* 23526 * XXX This will go away when TPI is extended to send 23527 * info reqs to sockfs/timod ..... 23528 * Given a queue, set the max packet size for the write 23529 * side of the queue below stream head. This value is 23530 * cached on the stream head. 23531 * Returns 1 on success, 0 otherwise. 23532 */ 23533 static int 23534 setmaxps(queue_t *q, int maxpsz) 23535 { 23536 struct stdata *stp; 23537 queue_t *wq; 23538 stp = STREAM(q); 23539 23540 /* 23541 * At this point change of a queue parameter is not allowed 23542 * when a multiplexor is sitting on top. 23543 */ 23544 if (stp->sd_flag & STPLEX) 23545 return (0); 23546 23547 claimstr(stp->sd_wrq); 23548 wq = stp->sd_wrq->q_next; 23549 ASSERT(wq != NULL); 23550 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 23551 releasestr(stp->sd_wrq); 23552 return (1); 23553 } 23554 23555 static int 23556 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23557 int *t_errorp, int *sys_errorp) 23558 { 23559 int error; 23560 int is_absreq_failure; 23561 t_scalar_t *opt_lenp; 23562 t_scalar_t opt_offset; 23563 int prim_type; 23564 struct T_conn_req *tcreqp; 23565 struct T_conn_res *tcresp; 23566 cred_t *cr; 23567 23568 cr = DB_CREDDEF(mp, tcp->tcp_cred); 23569 23570 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23571 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23572 prim_type == T_CONN_RES); 23573 23574 switch (prim_type) { 23575 case T_CONN_REQ: 23576 tcreqp = (struct T_conn_req *)mp->b_rptr; 23577 opt_offset = tcreqp->OPT_offset; 23578 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23579 break; 23580 case O_T_CONN_RES: 23581 case T_CONN_RES: 23582 tcresp = (struct T_conn_res *)mp->b_rptr; 23583 opt_offset = tcresp->OPT_offset; 23584 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23585 break; 23586 } 23587 23588 *t_errorp = 0; 23589 *sys_errorp = 0; 23590 *do_disconnectp = 0; 23591 23592 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23593 opt_offset, cr, &tcp_opt_obj, 23594 NULL, &is_absreq_failure); 23595 23596 switch (error) { 23597 case 0: /* no error */ 23598 ASSERT(is_absreq_failure == 0); 23599 return (0); 23600 case ENOPROTOOPT: 23601 *t_errorp = TBADOPT; 23602 break; 23603 case EACCES: 23604 *t_errorp = TACCES; 23605 break; 23606 default: 23607 *t_errorp = TSYSERR; *sys_errorp = error; 23608 break; 23609 } 23610 if (is_absreq_failure != 0) { 23611 /* 23612 * The connection request should get the local ack 23613 * T_OK_ACK and then a T_DISCON_IND. 23614 */ 23615 *do_disconnectp = 1; 23616 } 23617 return (-1); 23618 } 23619 23620 /* 23621 * Split this function out so that if the secret changes, I'm okay. 23622 * 23623 * Initialize the tcp_iss_cookie and tcp_iss_key. 23624 */ 23625 23626 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23627 23628 static void 23629 tcp_iss_key_init(uint8_t *phrase, int len) 23630 { 23631 struct { 23632 int32_t current_time; 23633 uint32_t randnum; 23634 uint16_t pad; 23635 uint8_t ether[6]; 23636 uint8_t passwd[PASSWD_SIZE]; 23637 } tcp_iss_cookie; 23638 time_t t; 23639 23640 /* 23641 * Start with the current absolute time. 23642 */ 23643 (void) drv_getparm(TIME, &t); 23644 tcp_iss_cookie.current_time = t; 23645 23646 /* 23647 * XXX - Need a more random number per RFC 1750, not this crap. 23648 * OTOH, if what follows is pretty random, then I'm in better shape. 23649 */ 23650 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23651 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23652 23653 /* 23654 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23655 * as a good template. 23656 */ 23657 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23658 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23659 23660 /* 23661 * The pass-phrase. Normally this is supplied by user-called NDD. 23662 */ 23663 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23664 23665 /* 23666 * See 4010593 if this section becomes a problem again, 23667 * but the local ethernet address is useful here. 23668 */ 23669 (void) localetheraddr(NULL, 23670 (struct ether_addr *)&tcp_iss_cookie.ether); 23671 23672 /* 23673 * Hash 'em all together. The MD5Final is called per-connection. 23674 */ 23675 mutex_enter(&tcp_iss_key_lock); 23676 MD5Init(&tcp_iss_key); 23677 MD5Update(&tcp_iss_key, (uchar_t *)&tcp_iss_cookie, 23678 sizeof (tcp_iss_cookie)); 23679 mutex_exit(&tcp_iss_key_lock); 23680 } 23681 23682 /* 23683 * Set the RFC 1948 pass phrase 23684 */ 23685 /* ARGSUSED */ 23686 static int 23687 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23688 cred_t *cr) 23689 { 23690 /* 23691 * Basically, value contains a new pass phrase. Pass it along! 23692 */ 23693 tcp_iss_key_init((uint8_t *)value, strlen(value)); 23694 return (0); 23695 } 23696 23697 /* ARGSUSED */ 23698 static int 23699 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23700 { 23701 bzero(buf, sizeof (tcp_sack_info_t)); 23702 return (0); 23703 } 23704 23705 /* ARGSUSED */ 23706 static int 23707 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23708 { 23709 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23710 return (0); 23711 } 23712 23713 void 23714 tcp_ddi_init(void) 23715 { 23716 int i; 23717 23718 /* Initialize locks */ 23719 rw_init(&tcp_hsp_lock, NULL, RW_DEFAULT, NULL); 23720 mutex_init(&tcp_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23721 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23722 mutex_init(&tcp_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23723 mutex_init(&tcp_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23724 rw_init(&tcp_reserved_port_lock, NULL, RW_DEFAULT, NULL); 23725 23726 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 23727 mutex_init(&tcp_bind_fanout[i].tf_lock, NULL, 23728 MUTEX_DEFAULT, NULL); 23729 } 23730 23731 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 23732 mutex_init(&tcp_acceptor_fanout[i].tf_lock, NULL, 23733 MUTEX_DEFAULT, NULL); 23734 } 23735 23736 /* TCP's IPsec code calls the packet dropper. */ 23737 ip_drop_register(&tcp_dropper, "TCP IPsec policy enforcement"); 23738 23739 if (!tcp_g_nd) { 23740 if (!tcp_param_register(tcp_param_arr, A_CNT(tcp_param_arr))) { 23741 nd_free(&tcp_g_nd); 23742 } 23743 } 23744 23745 /* 23746 * Note: To really walk the device tree you need the devinfo 23747 * pointer to your device which is only available after probe/attach. 23748 * The following is safe only because it uses ddi_root_node() 23749 */ 23750 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 23751 tcp_opt_obj.odb_opt_arr_cnt); 23752 23753 tcp_timercache = kmem_cache_create("tcp_timercache", 23754 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23755 NULL, NULL, NULL, NULL, NULL, 0); 23756 23757 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23758 sizeof (tcp_sack_info_t), 0, 23759 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23760 23761 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23762 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23763 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23764 23765 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 23766 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 23767 23768 ip_squeue_init(tcp_squeue_add); 23769 23770 /* Initialize the random number generator */ 23771 tcp_random_init(); 23772 23773 /* 23774 * Initialize RFC 1948 secret values. This will probably be reset once 23775 * by the boot scripts. 23776 * 23777 * Use NULL name, as the name is caught by the new lockstats. 23778 * 23779 * Initialize with some random, non-guessable string, like the global 23780 * T_INFO_ACK. 23781 */ 23782 23783 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 23784 sizeof (tcp_g_t_info_ack)); 23785 23786 if ((tcp_kstat = kstat_create(TCP_MOD_NAME, 0, "tcpstat", 23787 "net", KSTAT_TYPE_NAMED, 23788 sizeof (tcp_statistics) / sizeof (kstat_named_t), 23789 KSTAT_FLAG_VIRTUAL)) != NULL) { 23790 tcp_kstat->ks_data = &tcp_statistics; 23791 kstat_install(tcp_kstat); 23792 } 23793 23794 tcp_kstat_init(); 23795 } 23796 23797 void 23798 tcp_ddi_destroy(void) 23799 { 23800 int i; 23801 23802 nd_free(&tcp_g_nd); 23803 23804 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 23805 mutex_destroy(&tcp_bind_fanout[i].tf_lock); 23806 } 23807 23808 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 23809 mutex_destroy(&tcp_acceptor_fanout[i].tf_lock); 23810 } 23811 23812 mutex_destroy(&tcp_iss_key_lock); 23813 rw_destroy(&tcp_hsp_lock); 23814 mutex_destroy(&tcp_g_q_lock); 23815 mutex_destroy(&tcp_random_lock); 23816 mutex_destroy(&tcp_epriv_port_lock); 23817 rw_destroy(&tcp_reserved_port_lock); 23818 23819 ip_drop_unregister(&tcp_dropper); 23820 23821 kmem_cache_destroy(tcp_timercache); 23822 kmem_cache_destroy(tcp_sack_info_cache); 23823 kmem_cache_destroy(tcp_iphc_cache); 23824 23825 tcp_kstat_fini(); 23826 } 23827 23828 /* 23829 * Generate ISS, taking into account NDD changes may happen halfway through. 23830 * (If the iss is not zero, set it.) 23831 */ 23832 23833 static void 23834 tcp_iss_init(tcp_t *tcp) 23835 { 23836 MD5_CTX context; 23837 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 23838 uint32_t answer[4]; 23839 23840 tcp_iss_incr_extra += (ISS_INCR >> 1); 23841 tcp->tcp_iss = tcp_iss_incr_extra; 23842 switch (tcp_strong_iss) { 23843 case 2: 23844 mutex_enter(&tcp_iss_key_lock); 23845 context = tcp_iss_key; 23846 mutex_exit(&tcp_iss_key_lock); 23847 arg.ports = tcp->tcp_ports; 23848 if (tcp->tcp_ipversion == IPV4_VERSION) { 23849 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 23850 &arg.src); 23851 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 23852 &arg.dst); 23853 } else { 23854 arg.src = tcp->tcp_ip6h->ip6_src; 23855 arg.dst = tcp->tcp_ip6h->ip6_dst; 23856 } 23857 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 23858 MD5Final((uchar_t *)answer, &context); 23859 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 23860 /* 23861 * Now that we've hashed into a unique per-connection sequence 23862 * space, add a random increment per strong_iss == 1. So I 23863 * guess we'll have to... 23864 */ 23865 /* FALLTHRU */ 23866 case 1: 23867 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 23868 break; 23869 default: 23870 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 23871 break; 23872 } 23873 tcp->tcp_valid_bits = TCP_ISS_VALID; 23874 tcp->tcp_fss = tcp->tcp_iss - 1; 23875 tcp->tcp_suna = tcp->tcp_iss; 23876 tcp->tcp_snxt = tcp->tcp_iss + 1; 23877 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23878 tcp->tcp_csuna = tcp->tcp_snxt; 23879 } 23880 23881 /* 23882 * Exported routine for extracting active tcp connection status. 23883 * 23884 * This is used by the Solaris Cluster Networking software to 23885 * gather a list of connections that need to be forwarded to 23886 * specific nodes in the cluster when configuration changes occur. 23887 * 23888 * The callback is invoked for each tcp_t structure. Returning 23889 * non-zero from the callback routine terminates the search. 23890 */ 23891 int 23892 cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg) 23893 { 23894 tcp_t *tcp; 23895 cl_tcp_info_t cl_tcpi; 23896 connf_t *connfp; 23897 conn_t *connp; 23898 int i; 23899 23900 ASSERT(callback != NULL); 23901 23902 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 23903 23904 connfp = &ipcl_globalhash_fanout[i]; 23905 connp = NULL; 23906 23907 while ((connp = 23908 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 23909 23910 tcp = connp->conn_tcp; 23911 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 23912 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 23913 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 23914 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 23915 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 23916 /* 23917 * The macros tcp_laddr and tcp_faddr give the IPv4 23918 * addresses. They are copied implicitly below as 23919 * mapped addresses. 23920 */ 23921 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 23922 if (tcp->tcp_ipversion == IPV4_VERSION) { 23923 cl_tcpi.cl_tcpi_faddr = 23924 tcp->tcp_ipha->ipha_dst; 23925 } else { 23926 cl_tcpi.cl_tcpi_faddr_v6 = 23927 tcp->tcp_ip6h->ip6_dst; 23928 } 23929 23930 /* 23931 * If the callback returns non-zero 23932 * we terminate the traversal. 23933 */ 23934 if ((*callback)(&cl_tcpi, arg) != 0) { 23935 CONN_DEC_REF(tcp->tcp_connp); 23936 return (1); 23937 } 23938 } 23939 } 23940 23941 return (0); 23942 } 23943 23944 /* 23945 * Macros used for accessing the different types of sockaddr 23946 * structures inside a tcp_ioc_abort_conn_t. 23947 */ 23948 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 23949 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 23950 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 23951 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 23952 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 23953 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 23954 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 23955 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 23956 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 23957 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 23958 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 23959 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 23960 23961 /* 23962 * Return the correct error code to mimic the behavior 23963 * of a connection reset. 23964 */ 23965 #define TCP_AC_GET_ERRCODE(state, err) { \ 23966 switch ((state)) { \ 23967 case TCPS_SYN_SENT: \ 23968 case TCPS_SYN_RCVD: \ 23969 (err) = ECONNREFUSED; \ 23970 break; \ 23971 case TCPS_ESTABLISHED: \ 23972 case TCPS_FIN_WAIT_1: \ 23973 case TCPS_FIN_WAIT_2: \ 23974 case TCPS_CLOSE_WAIT: \ 23975 (err) = ECONNRESET; \ 23976 break; \ 23977 case TCPS_CLOSING: \ 23978 case TCPS_LAST_ACK: \ 23979 case TCPS_TIME_WAIT: \ 23980 (err) = 0; \ 23981 break; \ 23982 default: \ 23983 (err) = ENXIO; \ 23984 } \ 23985 } 23986 23987 /* 23988 * Check if a tcp structure matches the info in acp. 23989 */ 23990 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 23991 (((acp)->ac_local.ss_family == AF_INET) ? \ 23992 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 23993 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 23994 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 23995 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 23996 (TCP_AC_V4LPORT((acp)) == 0 || \ 23997 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 23998 (TCP_AC_V4RPORT((acp)) == 0 || \ 23999 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 24000 (acp)->ac_start <= (tcp)->tcp_state && \ 24001 (acp)->ac_end >= (tcp)->tcp_state) : \ 24002 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 24003 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 24004 &(tcp)->tcp_ip_src_v6)) && \ 24005 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 24006 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 24007 &(tcp)->tcp_remote_v6)) && \ 24008 (TCP_AC_V6LPORT((acp)) == 0 || \ 24009 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 24010 (TCP_AC_V6RPORT((acp)) == 0 || \ 24011 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 24012 (acp)->ac_start <= (tcp)->tcp_state && \ 24013 (acp)->ac_end >= (tcp)->tcp_state)) 24014 24015 #define TCP_AC_MATCH(acp, tcp) \ 24016 (((acp)->ac_zoneid == ALL_ZONES || \ 24017 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 24018 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 24019 24020 /* 24021 * Build a message containing a tcp_ioc_abort_conn_t structure 24022 * which is filled in with information from acp and tp. 24023 */ 24024 static mblk_t * 24025 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 24026 { 24027 mblk_t *mp; 24028 tcp_ioc_abort_conn_t *tacp; 24029 24030 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 24031 if (mp == NULL) 24032 return (NULL); 24033 24034 mp->b_datap->db_type = M_CTL; 24035 24036 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 24037 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 24038 sizeof (uint32_t)); 24039 24040 tacp->ac_start = acp->ac_start; 24041 tacp->ac_end = acp->ac_end; 24042 tacp->ac_zoneid = acp->ac_zoneid; 24043 24044 if (acp->ac_local.ss_family == AF_INET) { 24045 tacp->ac_local.ss_family = AF_INET; 24046 tacp->ac_remote.ss_family = AF_INET; 24047 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 24048 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 24049 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 24050 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 24051 } else { 24052 tacp->ac_local.ss_family = AF_INET6; 24053 tacp->ac_remote.ss_family = AF_INET6; 24054 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 24055 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 24056 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 24057 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 24058 } 24059 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 24060 return (mp); 24061 } 24062 24063 /* 24064 * Print a tcp_ioc_abort_conn_t structure. 24065 */ 24066 static void 24067 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 24068 { 24069 char lbuf[128]; 24070 char rbuf[128]; 24071 sa_family_t af; 24072 in_port_t lport, rport; 24073 ushort_t logflags; 24074 24075 af = acp->ac_local.ss_family; 24076 24077 if (af == AF_INET) { 24078 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 24079 lbuf, 128); 24080 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 24081 rbuf, 128); 24082 lport = ntohs(TCP_AC_V4LPORT(acp)); 24083 rport = ntohs(TCP_AC_V4RPORT(acp)); 24084 } else { 24085 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 24086 lbuf, 128); 24087 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 24088 rbuf, 128); 24089 lport = ntohs(TCP_AC_V6LPORT(acp)); 24090 rport = ntohs(TCP_AC_V6RPORT(acp)); 24091 } 24092 24093 logflags = SL_TRACE | SL_NOTE; 24094 /* 24095 * Don't print this message to the console if the operation was done 24096 * to a non-global zone. 24097 */ 24098 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24099 logflags |= SL_CONSOLE; 24100 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 24101 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 24102 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 24103 acp->ac_start, acp->ac_end); 24104 } 24105 24106 /* 24107 * Called inside tcp_rput when a message built using 24108 * tcp_ioctl_abort_build_msg is put into a queue. 24109 * Note that when we get here there is no wildcard in acp any more. 24110 */ 24111 static void 24112 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 24113 { 24114 tcp_ioc_abort_conn_t *acp; 24115 24116 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 24117 if (tcp->tcp_state <= acp->ac_end) { 24118 /* 24119 * If we get here, we are already on the correct 24120 * squeue. This ioctl follows the following path 24121 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 24122 * ->tcp_ioctl_abort->squeue_fill (if on a 24123 * different squeue) 24124 */ 24125 int errcode; 24126 24127 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 24128 (void) tcp_clean_death(tcp, errcode, 26); 24129 } 24130 freemsg(mp); 24131 } 24132 24133 /* 24134 * Abort all matching connections on a hash chain. 24135 */ 24136 static int 24137 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 24138 boolean_t exact) 24139 { 24140 int nmatch, err = 0; 24141 tcp_t *tcp; 24142 MBLKP mp, last, listhead = NULL; 24143 conn_t *tconnp; 24144 connf_t *connfp = &ipcl_conn_fanout[index]; 24145 24146 startover: 24147 nmatch = 0; 24148 24149 mutex_enter(&connfp->connf_lock); 24150 for (tconnp = connfp->connf_head; tconnp != NULL; 24151 tconnp = tconnp->conn_next) { 24152 tcp = tconnp->conn_tcp; 24153 if (TCP_AC_MATCH(acp, tcp)) { 24154 CONN_INC_REF(tcp->tcp_connp); 24155 mp = tcp_ioctl_abort_build_msg(acp, tcp); 24156 if (mp == NULL) { 24157 err = ENOMEM; 24158 CONN_DEC_REF(tcp->tcp_connp); 24159 break; 24160 } 24161 mp->b_prev = (mblk_t *)tcp; 24162 24163 if (listhead == NULL) { 24164 listhead = mp; 24165 last = mp; 24166 } else { 24167 last->b_next = mp; 24168 last = mp; 24169 } 24170 nmatch++; 24171 if (exact) 24172 break; 24173 } 24174 24175 /* Avoid holding lock for too long. */ 24176 if (nmatch >= 500) 24177 break; 24178 } 24179 mutex_exit(&connfp->connf_lock); 24180 24181 /* Pass mp into the correct tcp */ 24182 while ((mp = listhead) != NULL) { 24183 listhead = listhead->b_next; 24184 tcp = (tcp_t *)mp->b_prev; 24185 mp->b_next = mp->b_prev = NULL; 24186 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 24187 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 24188 } 24189 24190 *count += nmatch; 24191 if (nmatch >= 500 && err == 0) 24192 goto startover; 24193 return (err); 24194 } 24195 24196 /* 24197 * Abort all connections that matches the attributes specified in acp. 24198 */ 24199 static int 24200 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp) 24201 { 24202 sa_family_t af; 24203 uint32_t ports; 24204 uint16_t *pports; 24205 int err = 0, count = 0; 24206 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 24207 int index = -1; 24208 ushort_t logflags; 24209 24210 af = acp->ac_local.ss_family; 24211 24212 if (af == AF_INET) { 24213 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 24214 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 24215 pports = (uint16_t *)&ports; 24216 pports[1] = TCP_AC_V4LPORT(acp); 24217 pports[0] = TCP_AC_V4RPORT(acp); 24218 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 24219 } 24220 } else { 24221 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 24222 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 24223 pports = (uint16_t *)&ports; 24224 pports[1] = TCP_AC_V6LPORT(acp); 24225 pports[0] = TCP_AC_V6RPORT(acp); 24226 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 24227 } 24228 } 24229 24230 /* 24231 * For cases where remote addr, local port, and remote port are non- 24232 * wildcards, tcp_ioctl_abort_bucket will only be called once. 24233 */ 24234 if (index != -1) { 24235 err = tcp_ioctl_abort_bucket(acp, index, 24236 &count, exact); 24237 } else { 24238 /* 24239 * loop through all entries for wildcard case 24240 */ 24241 for (index = 0; index < ipcl_conn_fanout_size; index++) { 24242 err = tcp_ioctl_abort_bucket(acp, index, 24243 &count, exact); 24244 if (err != 0) 24245 break; 24246 } 24247 } 24248 24249 logflags = SL_TRACE | SL_NOTE; 24250 /* 24251 * Don't print this message to the console if the operation was done 24252 * to a non-global zone. 24253 */ 24254 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 24255 logflags |= SL_CONSOLE; 24256 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 24257 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 24258 if (err == 0 && count == 0) 24259 err = ENOENT; 24260 return (err); 24261 } 24262 24263 /* 24264 * Process the TCP_IOC_ABORT_CONN ioctl request. 24265 */ 24266 static void 24267 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 24268 { 24269 int err; 24270 IOCP iocp; 24271 MBLKP mp1; 24272 sa_family_t laf, raf; 24273 tcp_ioc_abort_conn_t *acp; 24274 zone_t *zptr; 24275 zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid; 24276 24277 iocp = (IOCP)mp->b_rptr; 24278 24279 if ((mp1 = mp->b_cont) == NULL || 24280 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 24281 err = EINVAL; 24282 goto out; 24283 } 24284 24285 /* check permissions */ 24286 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 24287 err = EPERM; 24288 goto out; 24289 } 24290 24291 if (mp1->b_cont != NULL) { 24292 freemsg(mp1->b_cont); 24293 mp1->b_cont = NULL; 24294 } 24295 24296 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 24297 laf = acp->ac_local.ss_family; 24298 raf = acp->ac_remote.ss_family; 24299 24300 /* check that a zone with the supplied zoneid exists */ 24301 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 24302 zptr = zone_find_by_id(zoneid); 24303 if (zptr != NULL) { 24304 zone_rele(zptr); 24305 } else { 24306 err = EINVAL; 24307 goto out; 24308 } 24309 } 24310 24311 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24312 acp->ac_start > acp->ac_end || laf != raf || 24313 (laf != AF_INET && laf != AF_INET6)) { 24314 err = EINVAL; 24315 goto out; 24316 } 24317 24318 tcp_ioctl_abort_dump(acp); 24319 err = tcp_ioctl_abort(acp); 24320 24321 out: 24322 if (mp1 != NULL) { 24323 freemsg(mp1); 24324 mp->b_cont = NULL; 24325 } 24326 24327 if (err != 0) 24328 miocnak(q, mp, 0, err); 24329 else 24330 miocack(q, mp, 0, 0); 24331 } 24332 24333 /* 24334 * tcp_time_wait_processing() handles processing of incoming packets when 24335 * the tcp is in the TIME_WAIT state. 24336 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24337 * on the time wait list. 24338 */ 24339 void 24340 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24341 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24342 { 24343 int32_t bytes_acked; 24344 int32_t gap; 24345 int32_t rgap; 24346 tcp_opt_t tcpopt; 24347 uint_t flags; 24348 uint32_t new_swnd = 0; 24349 conn_t *connp; 24350 24351 BUMP_LOCAL(tcp->tcp_ibsegs); 24352 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 24353 24354 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24355 new_swnd = BE16_TO_U16(tcph->th_win) << 24356 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24357 if (tcp->tcp_snd_ts_ok) { 24358 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24359 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24360 tcp->tcp_rnxt, TH_ACK); 24361 goto done; 24362 } 24363 } 24364 gap = seg_seq - tcp->tcp_rnxt; 24365 rgap = tcp->tcp_rwnd - (gap + seg_len); 24366 if (gap < 0) { 24367 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 24368 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 24369 (seg_len > -gap ? -gap : seg_len)); 24370 seg_len += gap; 24371 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24372 if (flags & TH_RST) { 24373 goto done; 24374 } 24375 if ((flags & TH_FIN) && seg_len == -1) { 24376 /* 24377 * When TCP receives a duplicate FIN in 24378 * TIME_WAIT state, restart the 2 MSL timer. 24379 * See page 73 in RFC 793. Make sure this TCP 24380 * is already on the TIME_WAIT list. If not, 24381 * just restart the timer. 24382 */ 24383 if (TCP_IS_DETACHED(tcp)) { 24384 tcp_time_wait_remove(tcp, NULL); 24385 tcp_time_wait_append(tcp); 24386 TCP_DBGSTAT(tcp_rput_time_wait); 24387 } else { 24388 ASSERT(tcp != NULL); 24389 TCP_TIMER_RESTART(tcp, 24390 tcp_time_wait_interval); 24391 } 24392 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24393 tcp->tcp_rnxt, TH_ACK); 24394 goto done; 24395 } 24396 flags |= TH_ACK_NEEDED; 24397 seg_len = 0; 24398 goto process_ack; 24399 } 24400 24401 /* Fix seg_seq, and chew the gap off the front. */ 24402 seg_seq = tcp->tcp_rnxt; 24403 } 24404 24405 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24406 /* 24407 * Make sure that when we accept the connection, pick 24408 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24409 * old connection. 24410 * 24411 * The next ISS generated is equal to tcp_iss_incr_extra 24412 * + ISS_INCR/2 + other components depending on the 24413 * value of tcp_strong_iss. We pre-calculate the new 24414 * ISS here and compare with tcp_snxt to determine if 24415 * we need to make adjustment to tcp_iss_incr_extra. 24416 * 24417 * The above calculation is ugly and is a 24418 * waste of CPU cycles... 24419 */ 24420 uint32_t new_iss = tcp_iss_incr_extra; 24421 int32_t adj; 24422 24423 switch (tcp_strong_iss) { 24424 case 2: { 24425 /* Add time and MD5 components. */ 24426 uint32_t answer[4]; 24427 struct { 24428 uint32_t ports; 24429 in6_addr_t src; 24430 in6_addr_t dst; 24431 } arg; 24432 MD5_CTX context; 24433 24434 mutex_enter(&tcp_iss_key_lock); 24435 context = tcp_iss_key; 24436 mutex_exit(&tcp_iss_key_lock); 24437 arg.ports = tcp->tcp_ports; 24438 /* We use MAPPED addresses in tcp_iss_init */ 24439 arg.src = tcp->tcp_ip_src_v6; 24440 if (tcp->tcp_ipversion == IPV4_VERSION) { 24441 IN6_IPADDR_TO_V4MAPPED( 24442 tcp->tcp_ipha->ipha_dst, 24443 &arg.dst); 24444 } else { 24445 arg.dst = 24446 tcp->tcp_ip6h->ip6_dst; 24447 } 24448 MD5Update(&context, (uchar_t *)&arg, 24449 sizeof (arg)); 24450 MD5Final((uchar_t *)answer, &context); 24451 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24452 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24453 break; 24454 } 24455 case 1: 24456 /* Add time component and min random (i.e. 1). */ 24457 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24458 break; 24459 default: 24460 /* Add only time component. */ 24461 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24462 break; 24463 } 24464 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24465 /* 24466 * New ISS not guaranteed to be ISS_INCR/2 24467 * ahead of the current tcp_snxt, so add the 24468 * difference to tcp_iss_incr_extra. 24469 */ 24470 tcp_iss_incr_extra += adj; 24471 } 24472 /* 24473 * If tcp_clean_death() can not perform the task now, 24474 * drop the SYN packet and let the other side re-xmit. 24475 * Otherwise pass the SYN packet back in, since the 24476 * old tcp state has been cleaned up or freed. 24477 */ 24478 if (tcp_clean_death(tcp, 0, 27) == -1) 24479 goto done; 24480 /* 24481 * We will come back to tcp_rput_data 24482 * on the global queue. Packets destined 24483 * for the global queue will be checked 24484 * with global policy. But the policy for 24485 * this packet has already been checked as 24486 * this was destined for the detached 24487 * connection. We need to bypass policy 24488 * check this time by attaching a dummy 24489 * ipsec_in with ipsec_in_dont_check set. 24490 */ 24491 if ((connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid)) != 24492 NULL) { 24493 TCP_STAT(tcp_time_wait_syn_success); 24494 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24495 return; 24496 } 24497 goto done; 24498 } 24499 24500 /* 24501 * rgap is the amount of stuff received out of window. A negative 24502 * value is the amount out of window. 24503 */ 24504 if (rgap < 0) { 24505 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 24506 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 24507 /* Fix seg_len and make sure there is something left. */ 24508 seg_len += rgap; 24509 if (seg_len <= 0) { 24510 if (flags & TH_RST) { 24511 goto done; 24512 } 24513 flags |= TH_ACK_NEEDED; 24514 seg_len = 0; 24515 goto process_ack; 24516 } 24517 } 24518 /* 24519 * Check whether we can update tcp_ts_recent. This test is 24520 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24521 * Extensions for High Performance: An Update", Internet Draft. 24522 */ 24523 if (tcp->tcp_snd_ts_ok && 24524 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24525 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24526 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24527 tcp->tcp_last_rcv_lbolt = lbolt64; 24528 } 24529 24530 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24531 /* Always ack out of order packets */ 24532 flags |= TH_ACK_NEEDED; 24533 seg_len = 0; 24534 } else if (seg_len > 0) { 24535 BUMP_MIB(&tcp_mib, tcpInClosed); 24536 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 24537 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 24538 } 24539 if (flags & TH_RST) { 24540 (void) tcp_clean_death(tcp, 0, 28); 24541 goto done; 24542 } 24543 if (flags & TH_SYN) { 24544 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24545 TH_RST|TH_ACK); 24546 /* 24547 * Do not delete the TCP structure if it is in 24548 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24549 */ 24550 goto done; 24551 } 24552 process_ack: 24553 if (flags & TH_ACK) { 24554 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24555 if (bytes_acked <= 0) { 24556 if (bytes_acked == 0 && seg_len == 0 && 24557 new_swnd == tcp->tcp_swnd) 24558 BUMP_MIB(&tcp_mib, tcpInDupAck); 24559 } else { 24560 /* Acks something not sent */ 24561 flags |= TH_ACK_NEEDED; 24562 } 24563 } 24564 if (flags & TH_ACK_NEEDED) { 24565 /* 24566 * Time to send an ack for some reason. 24567 */ 24568 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24569 tcp->tcp_rnxt, TH_ACK); 24570 } 24571 done: 24572 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24573 DB_CKSUMSTART(mp) = 0; 24574 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24575 TCP_STAT(tcp_time_wait_syn_fail); 24576 } 24577 freemsg(mp); 24578 } 24579 24580 /* 24581 * Allocate a T_SVR4_OPTMGMT_REQ. 24582 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 24583 * that tcp_rput_other can drop the acks. 24584 */ 24585 static mblk_t * 24586 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 24587 { 24588 mblk_t *mp; 24589 struct T_optmgmt_req *tor; 24590 struct opthdr *oh; 24591 uint_t size; 24592 char *optptr; 24593 24594 size = sizeof (*tor) + sizeof (*oh) + optlen; 24595 mp = allocb(size, BPRI_MED); 24596 if (mp == NULL) 24597 return (NULL); 24598 24599 mp->b_wptr += size; 24600 mp->b_datap->db_type = M_PROTO; 24601 tor = (struct T_optmgmt_req *)mp->b_rptr; 24602 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 24603 tor->MGMT_flags = T_NEGOTIATE; 24604 tor->OPT_length = sizeof (*oh) + optlen; 24605 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 24606 24607 oh = (struct opthdr *)&tor[1]; 24608 oh->level = level; 24609 oh->name = cmd; 24610 oh->len = optlen; 24611 if (optlen != 0) { 24612 optptr = (char *)&oh[1]; 24613 bcopy(opt, optptr, optlen); 24614 } 24615 return (mp); 24616 } 24617 24618 /* 24619 * TCP Timers Implementation. 24620 */ 24621 timeout_id_t 24622 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24623 { 24624 mblk_t *mp; 24625 tcp_timer_t *tcpt; 24626 tcp_t *tcp = connp->conn_tcp; 24627 24628 ASSERT(connp->conn_sqp != NULL); 24629 24630 TCP_DBGSTAT(tcp_timeout_calls); 24631 24632 if (tcp->tcp_timercache == NULL) { 24633 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24634 } else { 24635 TCP_DBGSTAT(tcp_timeout_cached_alloc); 24636 mp = tcp->tcp_timercache; 24637 tcp->tcp_timercache = mp->b_next; 24638 mp->b_next = NULL; 24639 ASSERT(mp->b_wptr == NULL); 24640 } 24641 24642 CONN_INC_REF(connp); 24643 tcpt = (tcp_timer_t *)mp->b_rptr; 24644 tcpt->connp = connp; 24645 tcpt->tcpt_proc = f; 24646 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 24647 return ((timeout_id_t)mp); 24648 } 24649 24650 static void 24651 tcp_timer_callback(void *arg) 24652 { 24653 mblk_t *mp = (mblk_t *)arg; 24654 tcp_timer_t *tcpt; 24655 conn_t *connp; 24656 24657 tcpt = (tcp_timer_t *)mp->b_rptr; 24658 connp = tcpt->connp; 24659 squeue_fill(connp->conn_sqp, mp, 24660 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 24661 } 24662 24663 static void 24664 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24665 { 24666 tcp_timer_t *tcpt; 24667 conn_t *connp = (conn_t *)arg; 24668 tcp_t *tcp = connp->conn_tcp; 24669 24670 tcpt = (tcp_timer_t *)mp->b_rptr; 24671 ASSERT(connp == tcpt->connp); 24672 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 24673 24674 /* 24675 * If the TCP has reached the closed state, don't proceed any 24676 * further. This TCP logically does not exist on the system. 24677 * tcpt_proc could for example access queues, that have already 24678 * been qprocoff'ed off. Also see comments at the start of tcp_input 24679 */ 24680 if (tcp->tcp_state != TCPS_CLOSED) { 24681 (*tcpt->tcpt_proc)(connp); 24682 } else { 24683 tcp->tcp_timer_tid = 0; 24684 } 24685 tcp_timer_free(connp->conn_tcp, mp); 24686 } 24687 24688 /* 24689 * There is potential race with untimeout and the handler firing at the same 24690 * time. The mblock may be freed by the handler while we are trying to use 24691 * it. But since both should execute on the same squeue, this race should not 24692 * occur. 24693 */ 24694 clock_t 24695 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 24696 { 24697 mblk_t *mp = (mblk_t *)id; 24698 tcp_timer_t *tcpt; 24699 clock_t delta; 24700 24701 TCP_DBGSTAT(tcp_timeout_cancel_reqs); 24702 24703 if (mp == NULL) 24704 return (-1); 24705 24706 tcpt = (tcp_timer_t *)mp->b_rptr; 24707 ASSERT(tcpt->connp == connp); 24708 24709 delta = untimeout(tcpt->tcpt_tid); 24710 24711 if (delta >= 0) { 24712 TCP_DBGSTAT(tcp_timeout_canceled); 24713 tcp_timer_free(connp->conn_tcp, mp); 24714 CONN_DEC_REF(connp); 24715 } 24716 24717 return (delta); 24718 } 24719 24720 /* 24721 * Allocate space for the timer event. The allocation looks like mblk, but it is 24722 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 24723 * 24724 * Dealing with failures: If we can't allocate from the timer cache we try 24725 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 24726 * points to b_rptr. 24727 * If we can't allocate anything using allocb_tryhard(), we perform a last 24728 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 24729 * save the actual allocation size in b_datap. 24730 */ 24731 mblk_t * 24732 tcp_timermp_alloc(int kmflags) 24733 { 24734 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 24735 kmflags & ~KM_PANIC); 24736 24737 if (mp != NULL) { 24738 mp->b_next = mp->b_prev = NULL; 24739 mp->b_rptr = (uchar_t *)(&mp[1]); 24740 mp->b_wptr = NULL; 24741 mp->b_datap = NULL; 24742 mp->b_queue = NULL; 24743 } else if (kmflags & KM_PANIC) { 24744 /* 24745 * Failed to allocate memory for the timer. Try allocating from 24746 * dblock caches. 24747 */ 24748 TCP_STAT(tcp_timermp_allocfail); 24749 mp = allocb_tryhard(sizeof (tcp_timer_t)); 24750 if (mp == NULL) { 24751 size_t size = 0; 24752 /* 24753 * Memory is really low. Try tryhard allocation. 24754 */ 24755 TCP_STAT(tcp_timermp_allocdblfail); 24756 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 24757 sizeof (tcp_timer_t), &size, kmflags); 24758 mp->b_rptr = (uchar_t *)(&mp[1]); 24759 mp->b_next = mp->b_prev = NULL; 24760 mp->b_wptr = (uchar_t *)-1; 24761 mp->b_datap = (dblk_t *)size; 24762 mp->b_queue = NULL; 24763 } 24764 ASSERT(mp->b_wptr != NULL); 24765 } 24766 TCP_DBGSTAT(tcp_timermp_alloced); 24767 24768 return (mp); 24769 } 24770 24771 /* 24772 * Free per-tcp timer cache. 24773 * It can only contain entries from tcp_timercache. 24774 */ 24775 void 24776 tcp_timermp_free(tcp_t *tcp) 24777 { 24778 mblk_t *mp; 24779 24780 while ((mp = tcp->tcp_timercache) != NULL) { 24781 ASSERT(mp->b_wptr == NULL); 24782 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 24783 kmem_cache_free(tcp_timercache, mp); 24784 } 24785 } 24786 24787 /* 24788 * Free timer event. Put it on the per-tcp timer cache if there is not too many 24789 * events there already (currently at most two events are cached). 24790 * If the event is not allocated from the timer cache, free it right away. 24791 */ 24792 static void 24793 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 24794 { 24795 mblk_t *mp1 = tcp->tcp_timercache; 24796 24797 if (mp->b_wptr != NULL) { 24798 /* 24799 * This allocation is not from a timer cache, free it right 24800 * away. 24801 */ 24802 if (mp->b_wptr != (uchar_t *)-1) 24803 freeb(mp); 24804 else 24805 kmem_free(mp, (size_t)mp->b_datap); 24806 } else if (mp1 == NULL || mp1->b_next == NULL) { 24807 /* Cache this timer block for future allocations */ 24808 mp->b_rptr = (uchar_t *)(&mp[1]); 24809 mp->b_next = mp1; 24810 tcp->tcp_timercache = mp; 24811 } else { 24812 kmem_cache_free(tcp_timercache, mp); 24813 TCP_DBGSTAT(tcp_timermp_freed); 24814 } 24815 } 24816 24817 /* 24818 * End of TCP Timers implementation. 24819 */ 24820 24821 /* 24822 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 24823 * on the specified backing STREAMS q. Note, the caller may make the 24824 * decision to call based on the tcp_t.tcp_flow_stopped value which 24825 * when check outside the q's lock is only an advisory check ... 24826 */ 24827 24828 void 24829 tcp_setqfull(tcp_t *tcp) 24830 { 24831 queue_t *q = tcp->tcp_wq; 24832 24833 if (!(q->q_flag & QFULL)) { 24834 mutex_enter(QLOCK(q)); 24835 if (!(q->q_flag & QFULL)) { 24836 /* still need to set QFULL */ 24837 q->q_flag |= QFULL; 24838 tcp->tcp_flow_stopped = B_TRUE; 24839 mutex_exit(QLOCK(q)); 24840 TCP_STAT(tcp_flwctl_on); 24841 } else { 24842 mutex_exit(QLOCK(q)); 24843 } 24844 } 24845 } 24846 24847 void 24848 tcp_clrqfull(tcp_t *tcp) 24849 { 24850 queue_t *q = tcp->tcp_wq; 24851 24852 if (q->q_flag & QFULL) { 24853 mutex_enter(QLOCK(q)); 24854 if (q->q_flag & QFULL) { 24855 q->q_flag &= ~QFULL; 24856 tcp->tcp_flow_stopped = B_FALSE; 24857 mutex_exit(QLOCK(q)); 24858 if (q->q_flag & QWANTW) 24859 qbackenable(q, 0); 24860 } else { 24861 mutex_exit(QLOCK(q)); 24862 } 24863 } 24864 } 24865 24866 /* 24867 * TCP Kstats implementation 24868 */ 24869 static void 24870 tcp_kstat_init(void) 24871 { 24872 tcp_named_kstat_t template = { 24873 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 24874 { "rtoMin", KSTAT_DATA_INT32, 0 }, 24875 { "rtoMax", KSTAT_DATA_INT32, 0 }, 24876 { "maxConn", KSTAT_DATA_INT32, 0 }, 24877 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 24878 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 24879 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 24880 { "estabResets", KSTAT_DATA_UINT32, 0 }, 24881 { "currEstab", KSTAT_DATA_UINT32, 0 }, 24882 { "inSegs", KSTAT_DATA_UINT32, 0 }, 24883 { "outSegs", KSTAT_DATA_UINT32, 0 }, 24884 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 24885 { "connTableSize", KSTAT_DATA_INT32, 0 }, 24886 { "outRsts", KSTAT_DATA_UINT32, 0 }, 24887 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 24888 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 24889 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 24890 { "outAck", KSTAT_DATA_UINT32, 0 }, 24891 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 24892 { "outUrg", KSTAT_DATA_UINT32, 0 }, 24893 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 24894 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 24895 { "outControl", KSTAT_DATA_UINT32, 0 }, 24896 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 24897 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 24898 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 24899 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 24900 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 24901 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 24902 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 24903 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 24904 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 24905 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 24906 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 24907 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 24908 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 24909 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 24910 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 24911 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 24912 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 24913 { "inClosed", KSTAT_DATA_UINT32, 0 }, 24914 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 24915 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 24916 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 24917 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 24918 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 24919 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 24920 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 24921 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 24922 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 24923 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 24924 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 24925 { "connTableSize6", KSTAT_DATA_INT32, 0 } 24926 }; 24927 24928 tcp_mibkp = kstat_create(TCP_MOD_NAME, 0, TCP_MOD_NAME, 24929 "mib2", KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0); 24930 24931 if (tcp_mibkp == NULL) 24932 return; 24933 24934 template.rtoAlgorithm.value.ui32 = 4; 24935 template.rtoMin.value.ui32 = tcp_rexmit_interval_min; 24936 template.rtoMax.value.ui32 = tcp_rexmit_interval_max; 24937 template.maxConn.value.i32 = -1; 24938 24939 bcopy(&template, tcp_mibkp->ks_data, sizeof (template)); 24940 24941 tcp_mibkp->ks_update = tcp_kstat_update; 24942 24943 kstat_install(tcp_mibkp); 24944 } 24945 24946 static void 24947 tcp_kstat_fini(void) 24948 { 24949 24950 if (tcp_mibkp != NULL) { 24951 kstat_delete(tcp_mibkp); 24952 tcp_mibkp = NULL; 24953 } 24954 } 24955 24956 static int 24957 tcp_kstat_update(kstat_t *kp, int rw) 24958 { 24959 tcp_named_kstat_t *tcpkp; 24960 tcp_t *tcp; 24961 connf_t *connfp; 24962 conn_t *connp; 24963 int i; 24964 24965 if (!kp || !kp->ks_data) 24966 return (EIO); 24967 24968 if (rw == KSTAT_WRITE) 24969 return (EACCES); 24970 24971 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 24972 24973 tcpkp->currEstab.value.ui32 = 0; 24974 24975 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24976 connfp = &ipcl_globalhash_fanout[i]; 24977 connp = NULL; 24978 while ((connp = 24979 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24980 tcp = connp->conn_tcp; 24981 switch (tcp_snmp_state(tcp)) { 24982 case MIB2_TCP_established: 24983 case MIB2_TCP_closeWait: 24984 tcpkp->currEstab.value.ui32++; 24985 break; 24986 } 24987 } 24988 } 24989 24990 tcpkp->activeOpens.value.ui32 = tcp_mib.tcpActiveOpens; 24991 tcpkp->passiveOpens.value.ui32 = tcp_mib.tcpPassiveOpens; 24992 tcpkp->attemptFails.value.ui32 = tcp_mib.tcpAttemptFails; 24993 tcpkp->estabResets.value.ui32 = tcp_mib.tcpEstabResets; 24994 tcpkp->inSegs.value.ui32 = tcp_mib.tcpInSegs; 24995 tcpkp->outSegs.value.ui32 = tcp_mib.tcpOutSegs; 24996 tcpkp->retransSegs.value.ui32 = tcp_mib.tcpRetransSegs; 24997 tcpkp->connTableSize.value.i32 = tcp_mib.tcpConnTableSize; 24998 tcpkp->outRsts.value.ui32 = tcp_mib.tcpOutRsts; 24999 tcpkp->outDataSegs.value.ui32 = tcp_mib.tcpOutDataSegs; 25000 tcpkp->outDataBytes.value.ui32 = tcp_mib.tcpOutDataBytes; 25001 tcpkp->retransBytes.value.ui32 = tcp_mib.tcpRetransBytes; 25002 tcpkp->outAck.value.ui32 = tcp_mib.tcpOutAck; 25003 tcpkp->outAckDelayed.value.ui32 = tcp_mib.tcpOutAckDelayed; 25004 tcpkp->outUrg.value.ui32 = tcp_mib.tcpOutUrg; 25005 tcpkp->outWinUpdate.value.ui32 = tcp_mib.tcpOutWinUpdate; 25006 tcpkp->outWinProbe.value.ui32 = tcp_mib.tcpOutWinProbe; 25007 tcpkp->outControl.value.ui32 = tcp_mib.tcpOutControl; 25008 tcpkp->outFastRetrans.value.ui32 = tcp_mib.tcpOutFastRetrans; 25009 tcpkp->inAckSegs.value.ui32 = tcp_mib.tcpInAckSegs; 25010 tcpkp->inAckBytes.value.ui32 = tcp_mib.tcpInAckBytes; 25011 tcpkp->inDupAck.value.ui32 = tcp_mib.tcpInDupAck; 25012 tcpkp->inAckUnsent.value.ui32 = tcp_mib.tcpInAckUnsent; 25013 tcpkp->inDataInorderSegs.value.ui32 = tcp_mib.tcpInDataInorderSegs; 25014 tcpkp->inDataInorderBytes.value.ui32 = tcp_mib.tcpInDataInorderBytes; 25015 tcpkp->inDataUnorderSegs.value.ui32 = tcp_mib.tcpInDataUnorderSegs; 25016 tcpkp->inDataUnorderBytes.value.ui32 = tcp_mib.tcpInDataUnorderBytes; 25017 tcpkp->inDataDupSegs.value.ui32 = tcp_mib.tcpInDataDupSegs; 25018 tcpkp->inDataDupBytes.value.ui32 = tcp_mib.tcpInDataDupBytes; 25019 tcpkp->inDataPartDupSegs.value.ui32 = tcp_mib.tcpInDataPartDupSegs; 25020 tcpkp->inDataPartDupBytes.value.ui32 = tcp_mib.tcpInDataPartDupBytes; 25021 tcpkp->inDataPastWinSegs.value.ui32 = tcp_mib.tcpInDataPastWinSegs; 25022 tcpkp->inDataPastWinBytes.value.ui32 = tcp_mib.tcpInDataPastWinBytes; 25023 tcpkp->inWinProbe.value.ui32 = tcp_mib.tcpInWinProbe; 25024 tcpkp->inWinUpdate.value.ui32 = tcp_mib.tcpInWinUpdate; 25025 tcpkp->inClosed.value.ui32 = tcp_mib.tcpInClosed; 25026 tcpkp->rttNoUpdate.value.ui32 = tcp_mib.tcpRttNoUpdate; 25027 tcpkp->rttUpdate.value.ui32 = tcp_mib.tcpRttUpdate; 25028 tcpkp->timRetrans.value.ui32 = tcp_mib.tcpTimRetrans; 25029 tcpkp->timRetransDrop.value.ui32 = tcp_mib.tcpTimRetransDrop; 25030 tcpkp->timKeepalive.value.ui32 = tcp_mib.tcpTimKeepalive; 25031 tcpkp->timKeepaliveProbe.value.ui32 = tcp_mib.tcpTimKeepaliveProbe; 25032 tcpkp->timKeepaliveDrop.value.ui32 = tcp_mib.tcpTimKeepaliveDrop; 25033 tcpkp->listenDrop.value.ui32 = tcp_mib.tcpListenDrop; 25034 tcpkp->listenDropQ0.value.ui32 = tcp_mib.tcpListenDropQ0; 25035 tcpkp->halfOpenDrop.value.ui32 = tcp_mib.tcpHalfOpenDrop; 25036 tcpkp->outSackRetransSegs.value.ui32 = tcp_mib.tcpOutSackRetransSegs; 25037 tcpkp->connTableSize6.value.i32 = tcp_mib.tcp6ConnTableSize; 25038 25039 return (0); 25040 } 25041 25042 void 25043 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 25044 { 25045 uint16_t hdr_len; 25046 ipha_t *ipha; 25047 uint8_t *nexthdrp; 25048 tcph_t *tcph; 25049 25050 /* Already has an eager */ 25051 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 25052 TCP_STAT(tcp_reinput_syn); 25053 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 25054 connp, SQTAG_TCP_REINPUT_EAGER); 25055 return; 25056 } 25057 25058 switch (IPH_HDR_VERSION(mp->b_rptr)) { 25059 case IPV4_VERSION: 25060 ipha = (ipha_t *)mp->b_rptr; 25061 hdr_len = IPH_HDR_LENGTH(ipha); 25062 break; 25063 case IPV6_VERSION: 25064 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 25065 &hdr_len, &nexthdrp)) { 25066 CONN_DEC_REF(connp); 25067 freemsg(mp); 25068 return; 25069 } 25070 break; 25071 } 25072 25073 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 25074 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 25075 mp->b_datap->db_struioflag |= STRUIO_EAGER; 25076 DB_CKSUMSTART(mp) = (intptr_t)sqp; 25077 } 25078 25079 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 25080 SQTAG_TCP_REINPUT); 25081 } 25082 25083 static squeue_func_t 25084 tcp_squeue_switch(int val) 25085 { 25086 squeue_func_t rval = squeue_fill; 25087 25088 switch (val) { 25089 case 1: 25090 rval = squeue_enter_nodrain; 25091 break; 25092 case 2: 25093 rval = squeue_enter; 25094 break; 25095 default: 25096 break; 25097 } 25098 return (rval); 25099 } 25100 25101 static void 25102 tcp_squeue_add(squeue_t *sqp) 25103 { 25104 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 25105 sizeof (tcp_squeue_priv_t), KM_SLEEP); 25106 25107 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 25108 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 25109 sqp, TCP_TIME_WAIT_DELAY); 25110 if (tcp_free_list_max_cnt == 0) { 25111 int tcp_ncpus = ((boot_max_ncpus == -1) ? 25112 max_ncpus : boot_max_ncpus); 25113 25114 /* 25115 * Limit number of entries to 1% of availble memory / tcp_ncpus 25116 */ 25117 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 25118 (tcp_ncpus * sizeof (tcp_t) * 100); 25119 } 25120 tcp_time_wait->tcp_free_list_cnt = 0; 25121 } 25122