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 30 const char tcp_version[] = "%Z%%M% %I% %E% SMI"; 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/zone.h> 58 59 #include <sys/errno.h> 60 #include <sys/signal.h> 61 #include <sys/socket.h> 62 #include <sys/sockio.h> 63 #include <sys/isa_defs.h> 64 #include <sys/md5.h> 65 #include <sys/random.h> 66 #include <netinet/in.h> 67 #include <netinet/tcp.h> 68 #include <netinet/ip6.h> 69 #include <netinet/icmp6.h> 70 #include <net/if.h> 71 #include <net/route.h> 72 #include <inet/ipsec_impl.h> 73 74 #include <inet/common.h> 75 #include <inet/ip.h> 76 #include <inet/ip_impl.h> 77 #include <inet/ip6.h> 78 #include <inet/ip_ndp.h> 79 #include <inet/mi.h> 80 #include <inet/mib2.h> 81 #include <inet/nd.h> 82 #include <inet/optcom.h> 83 #include <inet/snmpcom.h> 84 #include <inet/kstatcom.h> 85 #include <inet/tcp.h> 86 #include <inet/tcp_impl.h> 87 #include <net/pfkeyv2.h> 88 #include <inet/ipsec_info.h> 89 #include <inet/ipdrop.h> 90 #include <inet/tcp_trace.h> 91 92 #include <inet/ipclassifier.h> 93 #include <inet/ip_ire.h> 94 #include <inet/ip_if.h> 95 #include <inet/ipp_common.h> 96 #include <sys/squeue.h> 97 #include <inet/kssl/ksslapi.h> 98 99 /* 100 * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) 101 * 102 * (Read the detailed design doc in PSARC case directory) 103 * 104 * The entire tcp state is contained in tcp_t and conn_t structure 105 * which are allocated in tandem using ipcl_conn_create() and passing 106 * IPCL_CONNTCP as a flag. We use 'conn_ref' and 'conn_lock' to protect 107 * the references on the tcp_t. The tcp_t structure is never compressed 108 * and packets always land on the correct TCP perimeter from the time 109 * eager is created till the time tcp_t dies (as such the old mentat 110 * TCP global queue is not used for detached state and no IPSEC checking 111 * is required). The global queue is still allocated to send out resets 112 * for connection which have no listeners and IP directly calls 113 * tcp_xmit_listeners_reset() which does any policy check. 114 * 115 * Protection and Synchronisation mechanism: 116 * 117 * The tcp data structure does not use any kind of lock for protecting 118 * its state but instead uses 'squeues' for mutual exclusion from various 119 * read and write side threads. To access a tcp member, the thread should 120 * always be behind squeue (via squeue_enter, squeue_enter_nodrain, or 121 * squeue_fill). Since the squeues allow a direct function call, caller 122 * can pass any tcp function having prototype of edesc_t as argument 123 * (different from traditional STREAMs model where packets come in only 124 * designated entry points). The list of functions that can be directly 125 * called via squeue are listed before the usual function prototype. 126 * 127 * Referencing: 128 * 129 * TCP is MT-Hot and we use a reference based scheme to make sure that the 130 * tcp structure doesn't disappear when its needed. When the application 131 * creates an outgoing connection or accepts an incoming connection, we 132 * start out with 2 references on 'conn_ref'. One for TCP and one for IP. 133 * The IP reference is just a symbolic reference since ip_tcpclose() 134 * looks at tcp structure after tcp_close_output() returns which could 135 * have dropped the last TCP reference. So as long as the connection is 136 * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the 137 * conn_t. The classifier puts its own reference when the connection is 138 * inserted in listen or connected hash. Anytime a thread needs to enter 139 * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr 140 * on write side or by doing a classify on read side and then puts a 141 * reference on the conn before doing squeue_enter/tryenter/fill. For 142 * read side, the classifier itself puts the reference under fanout lock 143 * to make sure that tcp can't disappear before it gets processed. The 144 * squeue will drop this reference automatically so the called function 145 * doesn't have to do a DEC_REF. 146 * 147 * Opening a new connection: 148 * 149 * The outgoing connection open is pretty simple. ip_tcpopen() does the 150 * work in creating the conn/tcp structure and initializing it. The 151 * squeue assignment is done based on the CPU the application 152 * is running on. So for outbound connections, processing is always done 153 * on application CPU which might be different from the incoming CPU 154 * being interrupted by the NIC. An optimal way would be to figure out 155 * the NIC <-> CPU binding at listen time, and assign the outgoing 156 * connection to the squeue attached to the CPU that will be interrupted 157 * for incoming packets (we know the NIC based on the bind IP address). 158 * This might seem like a problem if more data is going out but the 159 * fact is that in most cases the transmit is ACK driven transmit where 160 * the outgoing data normally sits on TCP's xmit queue waiting to be 161 * transmitted. 162 * 163 * Accepting a connection: 164 * 165 * This is a more interesting case because of various races involved in 166 * establishing a eager in its own perimeter. Read the meta comment on 167 * top of tcp_conn_request(). But briefly, the squeue is picked by 168 * ip_tcp_input()/ip_fanout_tcp_v6() based on the interrupted CPU. 169 * 170 * Closing a connection: 171 * 172 * The close is fairly straight forward. tcp_close() calls tcp_close_output() 173 * via squeue to do the close and mark the tcp as detached if the connection 174 * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its 175 * reference but tcp_close() drop IP's reference always. So if tcp was 176 * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP 177 * and 1 because it is in classifier's connected hash. This is the condition 178 * we use to determine that its OK to clean up the tcp outside of squeue 179 * when time wait expires (check the ref under fanout and conn_lock and 180 * if it is 2, remove it from fanout hash and kill it). 181 * 182 * Although close just drops the necessary references and marks the 183 * tcp_detached state, tcp_close needs to know the tcp_detached has been 184 * set (under squeue) before letting the STREAM go away (because a 185 * inbound packet might attempt to go up the STREAM while the close 186 * has happened and tcp_detached is not set). So a special lock and 187 * flag is used along with a condition variable (tcp_closelock, tcp_closed, 188 * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked 189 * tcp_detached. 190 * 191 * Special provisions and fast paths: 192 * 193 * We make special provision for (AF_INET, SOCK_STREAM) sockets which 194 * can't have 'ipv6_recvpktinfo' set and for these type of sockets, IP 195 * will never send a M_CTL to TCP. As such, ip_tcp_input() which handles 196 * all TCP packets from the wire makes a IPCL_IS_TCP4_CONNECTED_NO_POLICY 197 * check to send packets directly to tcp_rput_data via squeue. Everyone 198 * else comes through tcp_input() on the read side. 199 * 200 * We also make special provisions for sockfs by marking tcp_issocket 201 * whenever we have only sockfs on top of TCP. This allows us to skip 202 * putting the tcp in acceptor hash since a sockfs listener can never 203 * become acceptor and also avoid allocating a tcp_t for acceptor STREAM 204 * since eager has already been allocated and the accept now happens 205 * on acceptor STREAM. There is a big blob of comment on top of 206 * tcp_conn_request explaining the new accept. When socket is POP'd, 207 * sockfs sends us an ioctl to mark the fact and we go back to old 208 * behaviour. Once tcp_issocket is unset, its never set for the 209 * life of that connection. 210 * 211 * IPsec notes : 212 * 213 * Since a packet is always executed on the correct TCP perimeter 214 * all IPsec processing is defered to IP including checking new 215 * connections and setting IPSEC policies for new connection. The 216 * only exception is tcp_xmit_listeners_reset() which is called 217 * directly from IP and needs to policy check to see if TH_RST 218 * can be sent out. 219 */ 220 221 222 extern major_t TCP6_MAJ; 223 224 /* 225 * Values for squeue switch: 226 * 1: squeue_enter_nodrain 227 * 2: squeue_enter 228 * 3: squeue_fill 229 */ 230 int tcp_squeue_close = 2; 231 int tcp_squeue_wput = 2; 232 233 squeue_func_t tcp_squeue_close_proc; 234 squeue_func_t tcp_squeue_wput_proc; 235 236 /* 237 * This controls how tiny a write must be before we try to copy it 238 * into the the mblk on the tail of the transmit queue. Not much 239 * speedup is observed for values larger than sixteen. Zero will 240 * disable the optimisation. 241 */ 242 int tcp_tx_pull_len = 16; 243 244 /* 245 * TCP Statistics. 246 * 247 * How TCP statistics work. 248 * 249 * There are two types of statistics invoked by two macros. 250 * 251 * TCP_STAT(name) does non-atomic increment of a named stat counter. It is 252 * supposed to be used in non MT-hot paths of the code. 253 * 254 * TCP_DBGSTAT(name) does atomic increment of a named stat counter. It is 255 * supposed to be used for DEBUG purposes and may be used on a hot path. 256 * 257 * Both TCP_STAT and TCP_DBGSTAT counters are available using kstat 258 * (use "kstat tcp" to get them). 259 * 260 * There is also additional debugging facility that marks tcp_clean_death() 261 * instances and saves them in tcp_t structure. It is triggered by 262 * TCP_TAG_CLEAN_DEATH define. Also, there is a global array of counters for 263 * tcp_clean_death() calls that counts the number of times each tag was hit. It 264 * is triggered by TCP_CLD_COUNTERS define. 265 * 266 * How to add new counters. 267 * 268 * 1) Add a field in the tcp_stat structure describing your counter. 269 * 2) Add a line in tcp_statistics with the name of the counter. 270 * 271 * IMPORTANT!! - make sure that both are in sync !! 272 * 3) Use either TCP_STAT or TCP_DBGSTAT with the name. 273 * 274 * Please avoid using private counters which are not kstat-exported. 275 * 276 * TCP_TAG_CLEAN_DEATH set to 1 enables tagging of tcp_clean_death() instances 277 * in tcp_t structure. 278 * 279 * TCP_MAX_CLEAN_DEATH_TAG is the maximum number of possible clean death tags. 280 */ 281 282 #ifndef TCP_DEBUG_COUNTER 283 #ifdef DEBUG 284 #define TCP_DEBUG_COUNTER 1 285 #else 286 #define TCP_DEBUG_COUNTER 0 287 #endif 288 #endif 289 290 #define TCP_CLD_COUNTERS 0 291 292 #define TCP_TAG_CLEAN_DEATH 1 293 #define TCP_MAX_CLEAN_DEATH_TAG 32 294 295 #ifdef lint 296 static int _lint_dummy_; 297 #endif 298 299 #if TCP_CLD_COUNTERS 300 static uint_t tcp_clean_death_stat[TCP_MAX_CLEAN_DEATH_TAG]; 301 #define TCP_CLD_STAT(x) tcp_clean_death_stat[x]++ 302 #elif defined(lint) 303 #define TCP_CLD_STAT(x) ASSERT(_lint_dummy_ == 0); 304 #else 305 #define TCP_CLD_STAT(x) 306 #endif 307 308 #if TCP_DEBUG_COUNTER 309 #define TCP_DBGSTAT(x) atomic_add_64(&(tcp_statistics.x.value.ui64), 1) 310 #elif defined(lint) 311 #define TCP_DBGSTAT(x) ASSERT(_lint_dummy_ == 0); 312 #else 313 #define TCP_DBGSTAT(x) 314 #endif 315 316 tcp_stat_t tcp_statistics = { 317 { "tcp_time_wait", KSTAT_DATA_UINT64 }, 318 { "tcp_time_wait_syn", KSTAT_DATA_UINT64 }, 319 { "tcp_time_wait_success", KSTAT_DATA_UINT64 }, 320 { "tcp_time_wait_fail", KSTAT_DATA_UINT64 }, 321 { "tcp_reinput_syn", KSTAT_DATA_UINT64 }, 322 { "tcp_ip_output", KSTAT_DATA_UINT64 }, 323 { "tcp_detach_non_time_wait", KSTAT_DATA_UINT64 }, 324 { "tcp_detach_time_wait", KSTAT_DATA_UINT64 }, 325 { "tcp_time_wait_reap", KSTAT_DATA_UINT64 }, 326 { "tcp_clean_death_nondetached", KSTAT_DATA_UINT64 }, 327 { "tcp_reinit_calls", KSTAT_DATA_UINT64 }, 328 { "tcp_eager_err1", KSTAT_DATA_UINT64 }, 329 { "tcp_eager_err2", KSTAT_DATA_UINT64 }, 330 { "tcp_eager_blowoff_calls", KSTAT_DATA_UINT64 }, 331 { "tcp_eager_blowoff_q", KSTAT_DATA_UINT64 }, 332 { "tcp_eager_blowoff_q0", KSTAT_DATA_UINT64 }, 333 { "tcp_not_hard_bound", KSTAT_DATA_UINT64 }, 334 { "tcp_no_listener", KSTAT_DATA_UINT64 }, 335 { "tcp_found_eager", KSTAT_DATA_UINT64 }, 336 { "tcp_wrong_queue", KSTAT_DATA_UINT64 }, 337 { "tcp_found_eager_binding1", KSTAT_DATA_UINT64 }, 338 { "tcp_found_eager_bound1", KSTAT_DATA_UINT64 }, 339 { "tcp_eager_has_listener1", KSTAT_DATA_UINT64 }, 340 { "tcp_open_alloc", KSTAT_DATA_UINT64 }, 341 { "tcp_open_detached_alloc", KSTAT_DATA_UINT64 }, 342 { "tcp_rput_time_wait", KSTAT_DATA_UINT64 }, 343 { "tcp_listendrop", KSTAT_DATA_UINT64 }, 344 { "tcp_listendropq0", KSTAT_DATA_UINT64 }, 345 { "tcp_wrong_rq", KSTAT_DATA_UINT64 }, 346 { "tcp_rsrv_calls", KSTAT_DATA_UINT64 }, 347 { "tcp_eagerfree2", KSTAT_DATA_UINT64 }, 348 { "tcp_eagerfree3", KSTAT_DATA_UINT64 }, 349 { "tcp_eagerfree4", KSTAT_DATA_UINT64 }, 350 { "tcp_eagerfree5", KSTAT_DATA_UINT64 }, 351 { "tcp_timewait_syn_fail", KSTAT_DATA_UINT64 }, 352 { "tcp_listen_badflags", KSTAT_DATA_UINT64 }, 353 { "tcp_timeout_calls", KSTAT_DATA_UINT64 }, 354 { "tcp_timeout_cached_alloc", KSTAT_DATA_UINT64 }, 355 { "tcp_timeout_cancel_reqs", KSTAT_DATA_UINT64 }, 356 { "tcp_timeout_canceled", KSTAT_DATA_UINT64 }, 357 { "tcp_timermp_alloced", KSTAT_DATA_UINT64 }, 358 { "tcp_timermp_freed", KSTAT_DATA_UINT64 }, 359 { "tcp_timermp_allocfail", KSTAT_DATA_UINT64 }, 360 { "tcp_timermp_allocdblfail", KSTAT_DATA_UINT64 }, 361 { "tcp_push_timer_cnt", KSTAT_DATA_UINT64 }, 362 { "tcp_ack_timer_cnt", KSTAT_DATA_UINT64 }, 363 { "tcp_ire_null1", KSTAT_DATA_UINT64 }, 364 { "tcp_ire_null", KSTAT_DATA_UINT64 }, 365 { "tcp_ip_send", KSTAT_DATA_UINT64 }, 366 { "tcp_ip_ire_send", KSTAT_DATA_UINT64 }, 367 { "tcp_wsrv_called", KSTAT_DATA_UINT64 }, 368 { "tcp_flwctl_on", KSTAT_DATA_UINT64 }, 369 { "tcp_timer_fire_early", KSTAT_DATA_UINT64 }, 370 { "tcp_timer_fire_miss", KSTAT_DATA_UINT64 }, 371 { "tcp_freelist_cleanup", KSTAT_DATA_UINT64 }, 372 { "tcp_rput_v6_error", KSTAT_DATA_UINT64 }, 373 { "tcp_out_sw_cksum", KSTAT_DATA_UINT64 }, 374 { "tcp_out_sw_cksum_bytes", KSTAT_DATA_UINT64 }, 375 { "tcp_zcopy_on", KSTAT_DATA_UINT64 }, 376 { "tcp_zcopy_off", KSTAT_DATA_UINT64 }, 377 { "tcp_zcopy_backoff", KSTAT_DATA_UINT64 }, 378 { "tcp_zcopy_disable", KSTAT_DATA_UINT64 }, 379 { "tcp_mdt_pkt_out", KSTAT_DATA_UINT64 }, 380 { "tcp_mdt_pkt_out_v4", KSTAT_DATA_UINT64 }, 381 { "tcp_mdt_pkt_out_v6", KSTAT_DATA_UINT64 }, 382 { "tcp_mdt_discarded", KSTAT_DATA_UINT64 }, 383 { "tcp_mdt_conn_halted1", KSTAT_DATA_UINT64 }, 384 { "tcp_mdt_conn_halted2", KSTAT_DATA_UINT64 }, 385 { "tcp_mdt_conn_halted3", KSTAT_DATA_UINT64 }, 386 { "tcp_mdt_conn_resumed1", KSTAT_DATA_UINT64 }, 387 { "tcp_mdt_conn_resumed2", KSTAT_DATA_UINT64 }, 388 { "tcp_mdt_legacy_small", KSTAT_DATA_UINT64 }, 389 { "tcp_mdt_legacy_all", KSTAT_DATA_UINT64 }, 390 { "tcp_mdt_legacy_ret", KSTAT_DATA_UINT64 }, 391 { "tcp_mdt_allocfail", KSTAT_DATA_UINT64 }, 392 { "tcp_mdt_addpdescfail", KSTAT_DATA_UINT64 }, 393 { "tcp_mdt_allocd", KSTAT_DATA_UINT64 }, 394 { "tcp_mdt_linked", KSTAT_DATA_UINT64 }, 395 { "tcp_fusion_flowctl", KSTAT_DATA_UINT64 }, 396 { "tcp_fusion_backenabled", KSTAT_DATA_UINT64 }, 397 { "tcp_fusion_urg", KSTAT_DATA_UINT64 }, 398 { "tcp_fusion_putnext", KSTAT_DATA_UINT64 }, 399 { "tcp_fusion_unfusable", KSTAT_DATA_UINT64 }, 400 { "tcp_fusion_aborted", KSTAT_DATA_UINT64 }, 401 { "tcp_fusion_unqualified", KSTAT_DATA_UINT64 }, 402 { "tcp_fusion_rrw_busy", KSTAT_DATA_UINT64 }, 403 { "tcp_fusion_rrw_msgcnt", KSTAT_DATA_UINT64 }, 404 { "tcp_in_ack_unsent_drop", KSTAT_DATA_UINT64 }, 405 { "tcp_sock_fallback", KSTAT_DATA_UINT64 }, 406 }; 407 408 static kstat_t *tcp_kstat; 409 410 /* 411 * Call either ip_output or ip_output_v6. This replaces putnext() calls on the 412 * tcp write side. 413 */ 414 #define CALL_IP_WPUT(connp, q, mp) { \ 415 ASSERT(((q)->q_flag & QREADR) == 0); \ 416 TCP_DBGSTAT(tcp_ip_output); \ 417 connp->conn_send(connp, (mp), (q), IP_WPUT); \ 418 } 419 420 /* Macros for timestamp comparisons */ 421 #define TSTMP_GEQ(a, b) ((int32_t)((a)-(b)) >= 0) 422 #define TSTMP_LT(a, b) ((int32_t)((a)-(b)) < 0) 423 424 /* 425 * Parameters for TCP Initial Send Sequence number (ISS) generation. When 426 * tcp_strong_iss is set to 1, which is the default, the ISS is calculated 427 * by adding three components: a time component which grows by 1 every 4096 428 * nanoseconds (versus every 4 microseconds suggested by RFC 793, page 27); 429 * a per-connection component which grows by 125000 for every new connection; 430 * and an "extra" component that grows by a random amount centered 431 * approximately on 64000. This causes the the ISS generator to cycle every 432 * 4.89 hours if no TCP connections are made, and faster if connections are 433 * made. 434 * 435 * When tcp_strong_iss is set to 0, ISS is calculated by adding two 436 * components: a time component which grows by 250000 every second; and 437 * a per-connection component which grows by 125000 for every new connections. 438 * 439 * A third method, when tcp_strong_iss is set to 2, for generating ISS is 440 * prescribed by Steve Bellovin. This involves adding time, the 125000 per 441 * connection, and a one-way hash (MD5) of the connection ID <sport, dport, 442 * src, dst>, a "truly" random (per RFC 1750) number, and a console-entered 443 * password. 444 */ 445 #define ISS_INCR 250000 446 #define ISS_NSEC_SHT 12 447 448 static uint32_t tcp_iss_incr_extra; /* Incremented for each connection */ 449 static kmutex_t tcp_iss_key_lock; 450 static MD5_CTX tcp_iss_key; 451 static sin_t sin_null; /* Zero address for quick clears */ 452 static sin6_t sin6_null; /* Zero address for quick clears */ 453 454 /* Packet dropper for TCP IPsec policy drops. */ 455 static ipdropper_t tcp_dropper; 456 457 /* 458 * This implementation follows the 4.3BSD interpretation of the urgent 459 * pointer and not RFC 1122. Switching to RFC 1122 behavior would cause 460 * incompatible changes in protocols like telnet and rlogin. 461 */ 462 #define TCP_OLD_URP_INTERPRETATION 1 463 464 #define TCP_IS_DETACHED_NONEAGER(tcp) \ 465 (TCP_IS_DETACHED(tcp) && \ 466 (!(tcp)->tcp_hard_binding)) 467 468 /* 469 * TCP reassembly macros. We hide starting and ending sequence numbers in 470 * b_next and b_prev of messages on the reassembly queue. The messages are 471 * chained using b_cont. These macros are used in tcp_reass() so we don't 472 * have to see the ugly casts and assignments. 473 */ 474 #define TCP_REASS_SEQ(mp) ((uint32_t)(uintptr_t)((mp)->b_next)) 475 #define TCP_REASS_SET_SEQ(mp, u) ((mp)->b_next = \ 476 (mblk_t *)(uintptr_t)(u)) 477 #define TCP_REASS_END(mp) ((uint32_t)(uintptr_t)((mp)->b_prev)) 478 #define TCP_REASS_SET_END(mp, u) ((mp)->b_prev = \ 479 (mblk_t *)(uintptr_t)(u)) 480 481 /* 482 * Implementation of TCP Timers. 483 * ============================= 484 * 485 * INTERFACE: 486 * 487 * There are two basic functions dealing with tcp timers: 488 * 489 * timeout_id_t tcp_timeout(connp, func, time) 490 * clock_t tcp_timeout_cancel(connp, timeout_id) 491 * TCP_TIMER_RESTART(tcp, intvl) 492 * 493 * tcp_timeout() starts a timer for the 'tcp' instance arranging to call 'func' 494 * after 'time' ticks passed. The function called by timeout() must adhere to 495 * the same restrictions as a driver soft interrupt handler - it must not sleep 496 * or call other functions that might sleep. The value returned is the opaque 497 * non-zero timeout identifier that can be passed to tcp_timeout_cancel() to 498 * cancel the request. The call to tcp_timeout() may fail in which case it 499 * returns zero. This is different from the timeout(9F) function which never 500 * fails. 501 * 502 * The call-back function 'func' always receives 'connp' as its single 503 * argument. It is always executed in the squeue corresponding to the tcp 504 * structure. The tcp structure is guaranteed to be present at the time the 505 * call-back is called. 506 * 507 * NOTE: The call-back function 'func' is never called if tcp is in 508 * the TCPS_CLOSED state. 509 * 510 * tcp_timeout_cancel() attempts to cancel a pending tcp_timeout() 511 * request. locks acquired by the call-back routine should not be held across 512 * the call to tcp_timeout_cancel() or a deadlock may result. 513 * 514 * tcp_timeout_cancel() returns -1 if it can not cancel the timeout request. 515 * Otherwise, it returns an integer value greater than or equal to 0. In 516 * particular, if the call-back function is already placed on the squeue, it can 517 * not be canceled. 518 * 519 * NOTE: both tcp_timeout() and tcp_timeout_cancel() should always be called 520 * within squeue context corresponding to the tcp instance. Since the 521 * call-back is also called via the same squeue, there are no race 522 * conditions described in untimeout(9F) manual page since all calls are 523 * strictly serialized. 524 * 525 * TCP_TIMER_RESTART() is a macro that attempts to cancel a pending timeout 526 * stored in tcp_timer_tid and starts a new one using 527 * MSEC_TO_TICK(intvl). It always uses tcp_timer() function as a call-back 528 * and stores the return value of tcp_timeout() in the tcp->tcp_timer_tid 529 * field. 530 * 531 * NOTE: since the timeout cancellation is not guaranteed, the cancelled 532 * call-back may still be called, so it is possible tcp_timer() will be 533 * called several times. This should not be a problem since tcp_timer() 534 * should always check the tcp instance state. 535 * 536 * 537 * IMPLEMENTATION: 538 * 539 * TCP timers are implemented using three-stage process. The call to 540 * tcp_timeout() uses timeout(9F) function to call tcp_timer_callback() function 541 * when the timer expires. The tcp_timer_callback() arranges the call of the 542 * tcp_timer_handler() function via squeue corresponding to the tcp 543 * instance. The tcp_timer_handler() calls actual requested timeout call-back 544 * and passes tcp instance as an argument to it. Information is passed between 545 * stages using the tcp_timer_t structure which contains the connp pointer, the 546 * tcp call-back to call and the timeout id returned by the timeout(9F). 547 * 548 * The tcp_timer_t structure is not used directly, it is embedded in an mblk_t - 549 * like structure that is used to enter an squeue. The mp->b_rptr of this pseudo 550 * mblk points to the beginning of tcp_timer_t structure. The tcp_timeout() 551 * returns the pointer to this mblk. 552 * 553 * The pseudo mblk is allocated from a special tcp_timer_cache kmem cache. It 554 * looks like a normal mblk without actual dblk attached to it. 555 * 556 * To optimize performance each tcp instance holds a small cache of timer 557 * mblocks. In the current implementation it caches up to two timer mblocks per 558 * tcp instance. The cache is preserved over tcp frees and is only freed when 559 * the whole tcp structure is destroyed by its kmem destructor. Since all tcp 560 * timer processing happens on a corresponding squeue, the cache manipulation 561 * does not require any locks. Experiments show that majority of timer mblocks 562 * allocations are satisfied from the tcp cache and do not involve kmem calls. 563 * 564 * The tcp_timeout() places a refhold on the connp instance which guarantees 565 * that it will be present at the time the call-back function fires. The 566 * tcp_timer_handler() drops the reference after calling the call-back, so the 567 * call-back function does not need to manipulate the references explicitly. 568 */ 569 570 typedef struct tcp_timer_s { 571 conn_t *connp; 572 void (*tcpt_proc)(void *); 573 timeout_id_t tcpt_tid; 574 } tcp_timer_t; 575 576 static kmem_cache_t *tcp_timercache; 577 kmem_cache_t *tcp_sack_info_cache; 578 kmem_cache_t *tcp_iphc_cache; 579 580 /* 581 * For scalability, we must not run a timer for every TCP connection 582 * in TIME_WAIT state. To see why, consider (for time wait interval of 583 * 4 minutes): 584 * 1000 connections/sec * 240 seconds/time wait = 240,000 active conn's 585 * 586 * This list is ordered by time, so you need only delete from the head 587 * until you get to entries which aren't old enough to delete yet. 588 * The list consists of only the detached TIME_WAIT connections. 589 * 590 * Note that the timer (tcp_time_wait_expire) is started when the tcp_t 591 * becomes detached TIME_WAIT (either by changing the state and already 592 * being detached or the other way around). This means that the TIME_WAIT 593 * state can be extended (up to doubled) if the connection doesn't become 594 * detached for a long time. 595 * 596 * The list manipulations (including tcp_time_wait_next/prev) 597 * are protected by the tcp_time_wait_lock. The content of the 598 * detached TIME_WAIT connections is protected by the normal perimeters. 599 */ 600 601 typedef struct tcp_squeue_priv_s { 602 kmutex_t tcp_time_wait_lock; 603 /* Protects the next 3 globals */ 604 timeout_id_t tcp_time_wait_tid; 605 tcp_t *tcp_time_wait_head; 606 tcp_t *tcp_time_wait_tail; 607 tcp_t *tcp_free_list; 608 uint_t tcp_free_list_cnt; 609 } tcp_squeue_priv_t; 610 611 /* 612 * TCP_TIME_WAIT_DELAY governs how often the time_wait_collector runs. 613 * Running it every 5 seconds seems to give the best results. 614 */ 615 #define TCP_TIME_WAIT_DELAY drv_usectohz(5000000) 616 617 /* 618 * To prevent memory hog, limit the number of entries in tcp_free_list 619 * to 1% of available memory / number of cpus 620 */ 621 uint_t tcp_free_list_max_cnt = 0; 622 623 #define TCP_XMIT_LOWATER 4096 624 #define TCP_XMIT_HIWATER 49152 625 #define TCP_RECV_LOWATER 2048 626 #define TCP_RECV_HIWATER 49152 627 628 /* 629 * PAWS needs a timer for 24 days. This is the number of ticks in 24 days 630 */ 631 #define PAWS_TIMEOUT ((clock_t)(24*24*60*60*hz)) 632 633 #define TIDUSZ 4096 /* transport interface data unit size */ 634 635 /* 636 * Bind hash list size and has function. It has to be a power of 2 for 637 * hashing. 638 */ 639 #define TCP_BIND_FANOUT_SIZE 512 640 #define TCP_BIND_HASH(lport) (ntohs(lport) & (TCP_BIND_FANOUT_SIZE - 1)) 641 /* 642 * Size of listen and acceptor hash list. It has to be a power of 2 for 643 * hashing. 644 */ 645 #define TCP_FANOUT_SIZE 256 646 647 #ifdef _ILP32 648 #define TCP_ACCEPTOR_HASH(accid) \ 649 (((uint_t)(accid) >> 8) & (TCP_FANOUT_SIZE - 1)) 650 #else 651 #define TCP_ACCEPTOR_HASH(accid) \ 652 ((uint_t)(accid) & (TCP_FANOUT_SIZE - 1)) 653 #endif /* _ILP32 */ 654 655 #define IP_ADDR_CACHE_SIZE 2048 656 #define IP_ADDR_CACHE_HASH(faddr) \ 657 (ntohl(faddr) & (IP_ADDR_CACHE_SIZE -1)) 658 659 /* Hash for HSPs uses all 32 bits, since both networks and hosts are in table */ 660 #define TCP_HSP_HASH_SIZE 256 661 662 #define TCP_HSP_HASH(addr) \ 663 (((addr>>24) ^ (addr >>16) ^ \ 664 (addr>>8) ^ (addr)) % TCP_HSP_HASH_SIZE) 665 666 /* 667 * TCP options struct returned from tcp_parse_options. 668 */ 669 typedef struct tcp_opt_s { 670 uint32_t tcp_opt_mss; 671 uint32_t tcp_opt_wscale; 672 uint32_t tcp_opt_ts_val; 673 uint32_t tcp_opt_ts_ecr; 674 tcp_t *tcp; 675 } tcp_opt_t; 676 677 /* 678 * RFC1323-recommended phrasing of TSTAMP option, for easier parsing 679 */ 680 681 #ifdef _BIG_ENDIAN 682 #define TCPOPT_NOP_NOP_TSTAMP ((TCPOPT_NOP << 24) | (TCPOPT_NOP << 16) | \ 683 (TCPOPT_TSTAMP << 8) | 10) 684 #else 685 #define TCPOPT_NOP_NOP_TSTAMP ((10 << 24) | (TCPOPT_TSTAMP << 16) | \ 686 (TCPOPT_NOP << 8) | TCPOPT_NOP) 687 #endif 688 689 /* 690 * Flags returned from tcp_parse_options. 691 */ 692 #define TCP_OPT_MSS_PRESENT 1 693 #define TCP_OPT_WSCALE_PRESENT 2 694 #define TCP_OPT_TSTAMP_PRESENT 4 695 #define TCP_OPT_SACK_OK_PRESENT 8 696 #define TCP_OPT_SACK_PRESENT 16 697 698 /* TCP option length */ 699 #define TCPOPT_NOP_LEN 1 700 #define TCPOPT_MAXSEG_LEN 4 701 #define TCPOPT_WS_LEN 3 702 #define TCPOPT_REAL_WS_LEN (TCPOPT_WS_LEN+1) 703 #define TCPOPT_TSTAMP_LEN 10 704 #define TCPOPT_REAL_TS_LEN (TCPOPT_TSTAMP_LEN+2) 705 #define TCPOPT_SACK_OK_LEN 2 706 #define TCPOPT_REAL_SACK_OK_LEN (TCPOPT_SACK_OK_LEN+2) 707 #define TCPOPT_REAL_SACK_LEN 4 708 #define TCPOPT_MAX_SACK_LEN 36 709 #define TCPOPT_HEADER_LEN 2 710 711 /* TCP cwnd burst factor. */ 712 #define TCP_CWND_INFINITE 65535 713 #define TCP_CWND_SS 3 714 #define TCP_CWND_NORMAL 5 715 716 /* Maximum TCP initial cwin (start/restart). */ 717 #define TCP_MAX_INIT_CWND 8 718 719 /* 720 * Initialize cwnd according to RFC 3390. def_max_init_cwnd is 721 * either tcp_slow_start_initial or tcp_slow_start_after idle 722 * depending on the caller. If the upper layer has not used the 723 * TCP_INIT_CWND option to change the initial cwnd, tcp_init_cwnd 724 * should be 0 and we use the formula in RFC 3390 to set tcp_cwnd. 725 * If the upper layer has changed set the tcp_init_cwnd, just use 726 * it to calculate the tcp_cwnd. 727 */ 728 #define SET_TCP_INIT_CWND(tcp, mss, def_max_init_cwnd) \ 729 { \ 730 if ((tcp)->tcp_init_cwnd == 0) { \ 731 (tcp)->tcp_cwnd = MIN(def_max_init_cwnd * (mss), \ 732 MIN(4 * (mss), MAX(2 * (mss), 4380 / (mss) * (mss)))); \ 733 } else { \ 734 (tcp)->tcp_cwnd = (tcp)->tcp_init_cwnd * (mss); \ 735 } \ 736 tcp->tcp_cwnd_cnt = 0; \ 737 } 738 739 /* TCP Timer control structure */ 740 typedef struct tcpt_s { 741 pfv_t tcpt_pfv; /* The routine we are to call */ 742 tcp_t *tcpt_tcp; /* The parameter we are to pass in */ 743 } tcpt_t; 744 745 /* Host Specific Parameter structure */ 746 typedef struct tcp_hsp { 747 struct tcp_hsp *tcp_hsp_next; 748 in6_addr_t tcp_hsp_addr_v6; 749 in6_addr_t tcp_hsp_subnet_v6; 750 uint_t tcp_hsp_vers; /* IPV4_VERSION | IPV6_VERSION */ 751 int32_t tcp_hsp_sendspace; 752 int32_t tcp_hsp_recvspace; 753 int32_t tcp_hsp_tstamp; 754 } tcp_hsp_t; 755 #define tcp_hsp_addr V4_PART_OF_V6(tcp_hsp_addr_v6) 756 #define tcp_hsp_subnet V4_PART_OF_V6(tcp_hsp_subnet_v6) 757 758 /* 759 * Functions called directly via squeue having a prototype of edesc_t. 760 */ 761 void tcp_conn_request(void *arg, mblk_t *mp, void *arg2); 762 static void tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2); 763 void tcp_accept_finish(void *arg, mblk_t *mp, void *arg2); 764 static void tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2); 765 static void tcp_wput_proto(void *arg, mblk_t *mp, void *arg2); 766 void tcp_input(void *arg, mblk_t *mp, void *arg2); 767 void tcp_rput_data(void *arg, mblk_t *mp, void *arg2); 768 static void tcp_close_output(void *arg, mblk_t *mp, void *arg2); 769 void tcp_output(void *arg, mblk_t *mp, void *arg2); 770 static void tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2); 771 static void tcp_timer_handler(void *arg, mblk_t *mp, void *arg2); 772 773 774 /* Prototype for TCP functions */ 775 static void tcp_random_init(void); 776 int tcp_random(void); 777 static void tcp_accept(tcp_t *tcp, mblk_t *mp); 778 static void tcp_accept_swap(tcp_t *listener, tcp_t *acceptor, 779 tcp_t *eager); 780 static int tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp); 781 static in_port_t tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 782 int reuseaddr, boolean_t quick_connect, boolean_t bind_to_req_port_only, 783 boolean_t user_specified); 784 static void tcp_closei_local(tcp_t *tcp); 785 static void tcp_close_detached(tcp_t *tcp); 786 static boolean_t tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, 787 mblk_t *idmp, mblk_t **defermp); 788 static void tcp_connect(tcp_t *tcp, mblk_t *mp); 789 static void tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, 790 in_port_t dstport, uint_t srcid); 791 static void tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 792 in_port_t dstport, uint32_t flowinfo, uint_t srcid, 793 uint32_t scope_id); 794 static int tcp_clean_death(tcp_t *tcp, int err, uint8_t tag); 795 static void tcp_def_q_set(tcp_t *tcp, mblk_t *mp); 796 static void tcp_disconnect(tcp_t *tcp, mblk_t *mp); 797 static char *tcp_display(tcp_t *tcp, char *, char); 798 static boolean_t tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum); 799 static void tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only); 800 static void tcp_eager_unlink(tcp_t *tcp); 801 static void tcp_err_ack(tcp_t *tcp, mblk_t *mp, int tlierr, 802 int unixerr); 803 static void tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 804 int tlierr, int unixerr); 805 static int tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, 806 cred_t *cr); 807 static int tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, 808 char *value, caddr_t cp, cred_t *cr); 809 static int tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, 810 char *value, caddr_t cp, cred_t *cr); 811 static int tcp_tpistate(tcp_t *tcp); 812 static void tcp_bind_hash_insert(tf_t *tf, tcp_t *tcp, 813 int caller_holds_lock); 814 static void tcp_bind_hash_remove(tcp_t *tcp); 815 static tcp_t *tcp_acceptor_hash_lookup(t_uscalar_t id); 816 void tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp); 817 static void tcp_acceptor_hash_remove(tcp_t *tcp); 818 static void tcp_capability_req(tcp_t *tcp, mblk_t *mp); 819 static void tcp_info_req(tcp_t *tcp, mblk_t *mp); 820 static void tcp_addr_req(tcp_t *tcp, mblk_t *mp); 821 static void tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *mp); 822 static int tcp_header_init_ipv4(tcp_t *tcp); 823 static int tcp_header_init_ipv6(tcp_t *tcp); 824 int tcp_init(tcp_t *tcp, queue_t *q); 825 static int tcp_init_values(tcp_t *tcp); 826 static mblk_t *tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic); 827 static mblk_t *tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, 828 t_scalar_t addr_length); 829 static void tcp_ip_ire_mark_advice(tcp_t *tcp); 830 static void tcp_ip_notify(tcp_t *tcp); 831 static mblk_t *tcp_ire_mp(mblk_t *mp); 832 static void tcp_iss_init(tcp_t *tcp); 833 static void tcp_keepalive_killer(void *arg); 834 static int tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt); 835 static void tcp_mss_set(tcp_t *tcp, uint32_t size); 836 static int tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, 837 int *do_disconnectp, int *t_errorp, int *sys_errorp); 838 static boolean_t tcp_allow_connopt_set(int level, int name); 839 int tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr); 840 int tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr); 841 static int tcp_opt_get_user(ipha_t *ipha, uchar_t *ptr); 842 int tcp_opt_set(queue_t *q, uint_t optset_context, int level, 843 int name, uint_t inlen, uchar_t *invalp, uint_t *outlenp, 844 uchar_t *outvalp, void *thisdg_attrs, cred_t *cr, 845 mblk_t *mblk); 846 static void tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha); 847 static int tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, 848 uchar_t *ptr, uint_t len); 849 static int tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr); 850 static boolean_t tcp_param_register(tcpparam_t *tcppa, int cnt); 851 static int tcp_param_set(queue_t *q, mblk_t *mp, char *value, 852 caddr_t cp, cred_t *cr); 853 static int tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, 854 caddr_t cp, cred_t *cr); 855 static void tcp_iss_key_init(uint8_t *phrase, int len); 856 static int tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, 857 caddr_t cp, cred_t *cr); 858 static void tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_cnt); 859 static mblk_t *tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start); 860 static void tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp); 861 static void tcp_reinit(tcp_t *tcp); 862 static void tcp_reinit_values(tcp_t *tcp); 863 static void tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, 864 tcp_t *thisstream, cred_t *cr); 865 866 static uint_t tcp_rcv_drain(queue_t *q, tcp_t *tcp); 867 static void tcp_sack_rxmit(tcp_t *tcp, uint_t *flags); 868 static boolean_t tcp_send_rst_chk(void); 869 static void tcp_ss_rexmit(tcp_t *tcp); 870 static mblk_t *tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp); 871 static void tcp_process_options(tcp_t *, tcph_t *); 872 static void tcp_rput_common(tcp_t *tcp, mblk_t *mp); 873 static void tcp_rsrv(queue_t *q); 874 static int tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd); 875 static int tcp_snmp_state(tcp_t *tcp); 876 static int tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, 877 cred_t *cr); 878 static int tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 879 cred_t *cr); 880 static int tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 881 cred_t *cr); 882 static int tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 883 cred_t *cr); 884 static int tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, 885 cred_t *cr); 886 static int tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, 887 caddr_t cp, cred_t *cr); 888 static int tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, 889 caddr_t cp, cred_t *cr); 890 static int tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, 891 cred_t *cr); 892 static void tcp_timer(void *arg); 893 static void tcp_timer_callback(void *); 894 static in_port_t tcp_update_next_port(in_port_t port, boolean_t random); 895 static in_port_t tcp_get_next_priv_port(void); 896 static void tcp_wput_sock(queue_t *q, mblk_t *mp); 897 void tcp_wput_accept(queue_t *q, mblk_t *mp); 898 static void tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent); 899 static void tcp_wput_flush(tcp_t *tcp, mblk_t *mp); 900 static void tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp); 901 static int tcp_send(queue_t *q, tcp_t *tcp, const int mss, 902 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 903 const int num_sack_blk, int *usable, uint_t *snxt, 904 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 905 const int mdt_thres); 906 static int tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, 907 const int tcp_hdr_len, const int tcp_tcp_hdr_len, 908 const int num_sack_blk, int *usable, uint_t *snxt, 909 int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 910 const int mdt_thres); 911 static void tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, 912 int num_sack_blk); 913 static void tcp_wsrv(queue_t *q); 914 static int tcp_xmit_end(tcp_t *tcp); 915 void tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len); 916 static mblk_t *tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, 917 int32_t *offset, mblk_t **end_mp, uint32_t seq, 918 boolean_t sendall, uint32_t *seg_len, boolean_t rexmit); 919 static void tcp_ack_timer(void *arg); 920 static mblk_t *tcp_ack_mp(tcp_t *tcp); 921 static void tcp_xmit_early_reset(char *str, mblk_t *mp, 922 uint32_t seq, uint32_t ack, int ctl, uint_t ip_hdr_len); 923 static void tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, 924 uint32_t ack, int ctl); 925 static tcp_hsp_t *tcp_hsp_lookup(ipaddr_t addr); 926 static tcp_hsp_t *tcp_hsp_lookup_ipv6(in6_addr_t *addr); 927 static int setmaxps(queue_t *q, int maxpsz); 928 static void tcp_set_rto(tcp_t *, time_t); 929 static boolean_t tcp_check_policy(tcp_t *, mblk_t *, ipha_t *, ip6_t *, 930 boolean_t, boolean_t); 931 static void tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, 932 boolean_t ipsec_mctl); 933 static boolean_t tcp_cmpbuf(void *a, uint_t alen, 934 boolean_t b_valid, void *b, uint_t blen); 935 static boolean_t tcp_allocbuf(void **dstp, uint_t *dstlenp, 936 boolean_t src_valid, void *src, uint_t srclen); 937 static void tcp_savebuf(void **dstp, uint_t *dstlenp, 938 boolean_t src_valid, void *src, uint_t srclen); 939 static mblk_t *tcp_setsockopt_mp(int level, int cmd, 940 char *opt, int optlen); 941 static int tcp_pkt_set(uchar_t *, uint_t, uchar_t **, uint_t *); 942 static int tcp_build_hdrs(queue_t *, tcp_t *); 943 static void tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, 944 uint32_t seg_seq, uint32_t seg_ack, int seg_len, 945 tcph_t *tcph); 946 boolean_t tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp); 947 boolean_t tcp_reserved_port_add(int, in_port_t *, in_port_t *); 948 boolean_t tcp_reserved_port_del(in_port_t, in_port_t); 949 boolean_t tcp_reserved_port_check(in_port_t); 950 static tcp_t *tcp_alloc_temp_tcp(in_port_t); 951 static int tcp_reserved_port_list(queue_t *, mblk_t *, caddr_t, cred_t *); 952 static mblk_t *tcp_mdt_info_mp(mblk_t *); 953 static void tcp_mdt_update(tcp_t *, ill_mdt_capab_t *, boolean_t); 954 static int tcp_mdt_add_attrs(multidata_t *, const mblk_t *, 955 const boolean_t, const uint32_t, const uint32_t, 956 const uint32_t, const uint32_t); 957 static void tcp_multisend_data(tcp_t *, ire_t *, const ill_t *, mblk_t *, 958 const uint_t, const uint_t, boolean_t *); 959 static void tcp_send_data(tcp_t *, queue_t *, mblk_t *); 960 extern mblk_t *tcp_timermp_alloc(int); 961 extern void tcp_timermp_free(tcp_t *); 962 static void tcp_timer_free(tcp_t *tcp, mblk_t *mp); 963 static void tcp_stop_lingering(tcp_t *tcp); 964 static void tcp_close_linger_timeout(void *arg); 965 void tcp_ddi_init(void); 966 void tcp_ddi_destroy(void); 967 static void tcp_kstat_init(void); 968 static void tcp_kstat_fini(void); 969 static int tcp_kstat_update(kstat_t *kp, int rw); 970 void tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp); 971 static int tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 972 tcph_t *tcph, uint_t ipvers, mblk_t *idmp); 973 static int tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 974 tcph_t *tcph, mblk_t *idmp); 975 static squeue_func_t tcp_squeue_switch(int); 976 977 static int tcp_open(queue_t *, dev_t *, int, int, cred_t *); 978 static int tcp_close(queue_t *, int); 979 static int tcpclose_accept(queue_t *); 980 static int tcp_modclose(queue_t *); 981 static void tcp_wput_mod(queue_t *, mblk_t *); 982 983 static void tcp_squeue_add(squeue_t *); 984 static boolean_t tcp_zcopy_check(tcp_t *); 985 static void tcp_zcopy_notify(tcp_t *); 986 static mblk_t *tcp_zcopy_disable(tcp_t *, mblk_t *); 987 static mblk_t *tcp_zcopy_backoff(tcp_t *, mblk_t *, int); 988 static void tcp_ire_ill_check(tcp_t *, ire_t *, ill_t *, boolean_t); 989 990 extern void tcp_kssl_input(tcp_t *, mblk_t *); 991 992 /* 993 * Routines related to the TCP_IOC_ABORT_CONN ioctl command. 994 * 995 * TCP_IOC_ABORT_CONN is a non-transparent ioctl command used for aborting 996 * TCP connections. To invoke this ioctl, a tcp_ioc_abort_conn_t structure 997 * (defined in tcp.h) needs to be filled in and passed into the kernel 998 * via an I_STR ioctl command (see streamio(7I)). The tcp_ioc_abort_conn_t 999 * structure contains the four-tuple of a TCP connection and a range of TCP 1000 * states (specified by ac_start and ac_end). The use of wildcard addresses 1001 * and ports is allowed. Connections with a matching four tuple and a state 1002 * within the specified range will be aborted. The valid states for the 1003 * ac_start and ac_end fields are in the range TCPS_SYN_SENT to TCPS_TIME_WAIT, 1004 * inclusive. 1005 * 1006 * An application which has its connection aborted by this ioctl will receive 1007 * an error that is dependent on the connection state at the time of the abort. 1008 * If the connection state is < TCPS_TIME_WAIT, an application should behave as 1009 * though a RST packet has been received. If the connection state is equal to 1010 * TCPS_TIME_WAIT, the 2MSL timeout will immediately be canceled by the kernel 1011 * and all resources associated with the connection will be freed. 1012 */ 1013 static mblk_t *tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *, tcp_t *); 1014 static void tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *); 1015 static void tcp_ioctl_abort_handler(tcp_t *, mblk_t *); 1016 static int tcp_ioctl_abort(tcp_ioc_abort_conn_t *); 1017 static void tcp_ioctl_abort_conn(queue_t *, mblk_t *); 1018 static int tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *, int, int *, 1019 boolean_t); 1020 1021 static struct module_info tcp_rinfo = { 1022 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER 1023 }; 1024 1025 static struct module_info tcp_winfo = { 1026 TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16 1027 }; 1028 1029 /* 1030 * Entry points for TCP as a module. It only allows SNMP requests 1031 * to pass through. 1032 */ 1033 struct qinit tcp_mod_rinit = { 1034 (pfi_t)putnext, NULL, tcp_open, ip_snmpmod_close, NULL, &tcp_rinfo, 1035 }; 1036 1037 struct qinit tcp_mod_winit = { 1038 (pfi_t)ip_snmpmod_wput, NULL, tcp_open, ip_snmpmod_close, NULL, 1039 &tcp_rinfo 1040 }; 1041 1042 /* 1043 * Entry points for TCP as a device. The normal case which supports 1044 * the TCP functionality. 1045 */ 1046 struct qinit tcp_rinit = { 1047 NULL, (pfi_t)tcp_rsrv, tcp_open, tcp_close, NULL, &tcp_rinfo 1048 }; 1049 1050 struct qinit tcp_winit = { 1051 (pfi_t)tcp_wput, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1052 }; 1053 1054 /* Initial entry point for TCP in socket mode. */ 1055 struct qinit tcp_sock_winit = { 1056 (pfi_t)tcp_wput_sock, (pfi_t)tcp_wsrv, NULL, NULL, NULL, &tcp_winfo 1057 }; 1058 1059 /* 1060 * Entry points for TCP as a acceptor STREAM opened by sockfs when doing 1061 * an accept. Avoid allocating data structures since eager has already 1062 * been created. 1063 */ 1064 struct qinit tcp_acceptor_rinit = { 1065 NULL, (pfi_t)tcp_rsrv, NULL, tcpclose_accept, NULL, &tcp_winfo 1066 }; 1067 1068 struct qinit tcp_acceptor_winit = { 1069 (pfi_t)tcp_wput_accept, NULL, NULL, NULL, NULL, &tcp_winfo 1070 }; 1071 1072 /* 1073 * Entry points for TCP loopback (read side only) 1074 */ 1075 struct qinit tcp_loopback_rinit = { 1076 (pfi_t)0, (pfi_t)tcp_rsrv, tcp_open, tcp_close, (pfi_t)0, 1077 &tcp_rinfo, NULL, tcp_fuse_rrw, tcp_fuse_rinfop, STRUIOT_STANDARD 1078 }; 1079 1080 struct streamtab tcpinfo = { 1081 &tcp_rinit, &tcp_winit 1082 }; 1083 1084 extern squeue_func_t tcp_squeue_wput_proc; 1085 extern squeue_func_t tcp_squeue_timer_proc; 1086 1087 /* Protected by tcp_g_q_lock */ 1088 static queue_t *tcp_g_q; /* Default queue used during detached closes */ 1089 kmutex_t tcp_g_q_lock; 1090 1091 /* Protected by tcp_hsp_lock */ 1092 /* 1093 * XXX The host param mechanism should go away and instead we should use 1094 * the metrics associated with the routes to determine the default sndspace 1095 * and rcvspace. 1096 */ 1097 static tcp_hsp_t **tcp_hsp_hash; /* Hash table for HSPs */ 1098 krwlock_t tcp_hsp_lock; 1099 1100 /* 1101 * Extra privileged ports. In host byte order. 1102 * Protected by tcp_epriv_port_lock. 1103 */ 1104 #define TCP_NUM_EPRIV_PORTS 64 1105 static int tcp_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS; 1106 static uint16_t tcp_g_epriv_ports[TCP_NUM_EPRIV_PORTS] = { 2049, 4045 }; 1107 kmutex_t tcp_epriv_port_lock; 1108 1109 /* 1110 * The smallest anonymous port in the priviledged port range which TCP 1111 * looks for free port. Use in the option TCP_ANONPRIVBIND. 1112 */ 1113 static in_port_t tcp_min_anonpriv_port = 512; 1114 1115 /* Only modified during _init and _fini thus no locking is needed. */ 1116 static caddr_t tcp_g_nd; /* Head of 'named dispatch' variable list */ 1117 1118 /* Hint not protected by any lock */ 1119 static uint_t tcp_next_port_to_try; 1120 1121 1122 /* TCP bind hash list - all tcp_t with state >= BOUND. */ 1123 tf_t tcp_bind_fanout[TCP_BIND_FANOUT_SIZE]; 1124 1125 /* TCP queue hash list - all tcp_t in case they will be an acceptor. */ 1126 static tf_t tcp_acceptor_fanout[TCP_FANOUT_SIZE]; 1127 1128 /* 1129 * TCP has a private interface for other kernel modules to reserve a 1130 * port range for them to use. Once reserved, TCP will not use any ports 1131 * in the range. This interface relies on the TCP_EXCLBIND feature. If 1132 * the semantics of TCP_EXCLBIND is changed, implementation of this interface 1133 * has to be verified. 1134 * 1135 * There can be TCP_RESERVED_PORTS_ARRAY_MAX_SIZE port ranges. Each port 1136 * range can cover at most TCP_RESERVED_PORTS_RANGE_MAX ports. A port 1137 * range is [port a, port b] inclusive. And each port range is between 1138 * TCP_LOWESET_RESERVED_PORT and TCP_LARGEST_RESERVED_PORT inclusive. 1139 * 1140 * Note that the default anonymous port range starts from 32768. There is 1141 * no port "collision" between that and the reserved port range. If there 1142 * is port collision (because the default smallest anonymous port is lowered 1143 * or some apps specifically bind to ports in the reserved port range), the 1144 * system may not be able to reserve a port range even there are enough 1145 * unbound ports as a reserved port range contains consecutive ports . 1146 */ 1147 #define TCP_RESERVED_PORTS_ARRAY_MAX_SIZE 5 1148 #define TCP_RESERVED_PORTS_RANGE_MAX 1000 1149 #define TCP_SMALLEST_RESERVED_PORT 10240 1150 #define TCP_LARGEST_RESERVED_PORT 20480 1151 1152 /* Structure to represent those reserved port ranges. */ 1153 typedef struct tcp_rport_s { 1154 in_port_t lo_port; 1155 in_port_t hi_port; 1156 tcp_t **temp_tcp_array; 1157 } tcp_rport_t; 1158 1159 /* The reserved port array. */ 1160 static tcp_rport_t tcp_reserved_port[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 1161 1162 /* Locks to protect the tcp_reserved_ports array. */ 1163 static krwlock_t tcp_reserved_port_lock; 1164 1165 /* The number of ranges in the array. */ 1166 uint32_t tcp_reserved_port_array_size = 0; 1167 1168 /* 1169 * MIB-2 stuff for SNMP 1170 * Note: tcpInErrs {tcp 15} is accumulated in ip.c 1171 */ 1172 mib2_tcp_t tcp_mib; /* SNMP fixed size info */ 1173 kstat_t *tcp_mibkp; /* kstat exporting tcp_mib data */ 1174 1175 boolean_t tcp_icmp_source_quench = B_FALSE; 1176 /* 1177 * Following assumes TPI alignment requirements stay along 32 bit 1178 * boundaries 1179 */ 1180 #define ROUNDUP32(x) \ 1181 (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1)) 1182 1183 /* Template for response to info request. */ 1184 static struct T_info_ack tcp_g_t_info_ack = { 1185 T_INFO_ACK, /* PRIM_type */ 1186 0, /* TSDU_size */ 1187 T_INFINITE, /* ETSDU_size */ 1188 T_INVALID, /* CDATA_size */ 1189 T_INVALID, /* DDATA_size */ 1190 sizeof (sin_t), /* ADDR_size */ 1191 0, /* OPT_size - not initialized here */ 1192 TIDUSZ, /* TIDU_size */ 1193 T_COTS_ORD, /* SERV_type */ 1194 TCPS_IDLE, /* CURRENT_state */ 1195 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1196 }; 1197 1198 static struct T_info_ack tcp_g_t_info_ack_v6 = { 1199 T_INFO_ACK, /* PRIM_type */ 1200 0, /* TSDU_size */ 1201 T_INFINITE, /* ETSDU_size */ 1202 T_INVALID, /* CDATA_size */ 1203 T_INVALID, /* DDATA_size */ 1204 sizeof (sin6_t), /* ADDR_size */ 1205 0, /* OPT_size - not initialized here */ 1206 TIDUSZ, /* TIDU_size */ 1207 T_COTS_ORD, /* SERV_type */ 1208 TCPS_IDLE, /* CURRENT_state */ 1209 (XPG4_1|EXPINLINE) /* PROVIDER_flag */ 1210 }; 1211 1212 #define MS 1L 1213 #define SECONDS (1000 * MS) 1214 #define MINUTES (60 * SECONDS) 1215 #define HOURS (60 * MINUTES) 1216 #define DAYS (24 * HOURS) 1217 1218 #define PARAM_MAX (~(uint32_t)0) 1219 1220 /* Max size IP datagram is 64k - 1 */ 1221 #define TCP_MSS_MAX_IPV4 (IP_MAXPACKET - (sizeof (ipha_t) + sizeof (tcph_t))) 1222 #define TCP_MSS_MAX_IPV6 (IP_MAXPACKET - (sizeof (ip6_t) + sizeof (tcph_t))) 1223 /* Max of the above */ 1224 #define TCP_MSS_MAX TCP_MSS_MAX_IPV4 1225 1226 /* Largest TCP port number */ 1227 #define TCP_MAX_PORT (64 * 1024 - 1) 1228 1229 /* 1230 * tcp_wroff_xtra is the extra space in front of TCP/IP header for link 1231 * layer header. It has to be a multiple of 4. 1232 */ 1233 static tcpparam_t tcp_wroff_xtra_param = { 0, 256, 32, "tcp_wroff_xtra" }; 1234 #define tcp_wroff_xtra tcp_wroff_xtra_param.tcp_param_val 1235 1236 /* 1237 * All of these are alterable, within the min/max values given, at run time. 1238 * Note that the default value of "tcp_time_wait_interval" is four minutes, 1239 * per the TCP spec. 1240 */ 1241 /* BEGIN CSTYLED */ 1242 tcpparam_t tcp_param_arr[] = { 1243 /*min max value name */ 1244 { 1*SECONDS, 10*MINUTES, 1*MINUTES, "tcp_time_wait_interval"}, 1245 { 1, PARAM_MAX, 128, "tcp_conn_req_max_q" }, 1246 { 0, PARAM_MAX, 1024, "tcp_conn_req_max_q0" }, 1247 { 1, 1024, 1, "tcp_conn_req_min" }, 1248 { 0*MS, 20*SECONDS, 0*MS, "tcp_conn_grace_period" }, 1249 { 128, (1<<30), 1024*1024, "tcp_cwnd_max" }, 1250 { 0, 10, 0, "tcp_debug" }, 1251 { 1024, (32*1024), 1024, "tcp_smallest_nonpriv_port"}, 1252 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_cinterval"}, 1253 { 1*SECONDS, PARAM_MAX, 3*MINUTES, "tcp_ip_abort_linterval"}, 1254 { 500*MS, PARAM_MAX, 8*MINUTES, "tcp_ip_abort_interval"}, 1255 { 1*SECONDS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_cinterval"}, 1256 { 500*MS, PARAM_MAX, 10*SECONDS, "tcp_ip_notify_interval"}, 1257 { 1, 255, 64, "tcp_ipv4_ttl"}, 1258 { 10*SECONDS, 10*DAYS, 2*HOURS, "tcp_keepalive_interval"}, 1259 { 0, 100, 10, "tcp_maxpsz_multiplier" }, 1260 { 1, TCP_MSS_MAX_IPV4, 536, "tcp_mss_def_ipv4"}, 1261 { 1, TCP_MSS_MAX_IPV4, TCP_MSS_MAX_IPV4, "tcp_mss_max_ipv4"}, 1262 { 1, TCP_MSS_MAX, 108, "tcp_mss_min"}, 1263 { 1, (64*1024)-1, (4*1024)-1, "tcp_naglim_def"}, 1264 { 1*MS, 20*SECONDS, 3*SECONDS, "tcp_rexmit_interval_initial"}, 1265 { 1*MS, 2*HOURS, 60*SECONDS, "tcp_rexmit_interval_max"}, 1266 { 1*MS, 2*HOURS, 400*MS, "tcp_rexmit_interval_min"}, 1267 { 1*MS, 1*MINUTES, 100*MS, "tcp_deferred_ack_interval" }, 1268 { 0, 16, 0, "tcp_snd_lowat_fraction" }, 1269 { 0, 128000, 0, "tcp_sth_rcv_hiwat" }, 1270 { 0, 128000, 0, "tcp_sth_rcv_lowat" }, 1271 { 1, 10000, 3, "tcp_dupack_fast_retransmit" }, 1272 { 0, 1, 0, "tcp_ignore_path_mtu" }, 1273 { 1024, TCP_MAX_PORT, 32*1024, "tcp_smallest_anon_port"}, 1274 { 1024, TCP_MAX_PORT, TCP_MAX_PORT, "tcp_largest_anon_port"}, 1275 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_HIWATER,"tcp_xmit_hiwat"}, 1276 { TCP_XMIT_LOWATER, (1<<30), TCP_XMIT_LOWATER,"tcp_xmit_lowat"}, 1277 { TCP_RECV_LOWATER, (1<<30), TCP_RECV_HIWATER,"tcp_recv_hiwat"}, 1278 { 1, 65536, 4, "tcp_recv_hiwat_minmss"}, 1279 { 1*SECONDS, PARAM_MAX, 675*SECONDS, "tcp_fin_wait_2_flush_interval"}, 1280 { 0, TCP_MSS_MAX, 64, "tcp_co_min"}, 1281 { 8192, (1<<30), 1024*1024, "tcp_max_buf"}, 1282 /* 1283 * Question: What default value should I set for tcp_strong_iss? 1284 */ 1285 { 0, 2, 1, "tcp_strong_iss"}, 1286 { 0, 65536, 20, "tcp_rtt_updates"}, 1287 { 0, 1, 1, "tcp_wscale_always"}, 1288 { 0, 1, 0, "tcp_tstamp_always"}, 1289 { 0, 1, 1, "tcp_tstamp_if_wscale"}, 1290 { 0*MS, 2*HOURS, 0*MS, "tcp_rexmit_interval_extra"}, 1291 { 0, 16, 2, "tcp_deferred_acks_max"}, 1292 { 1, 16384, 4, "tcp_slow_start_after_idle"}, 1293 { 1, 4, 4, "tcp_slow_start_initial"}, 1294 { 10*MS, 50*MS, 20*MS, "tcp_co_timer_interval"}, 1295 { 0, 2, 2, "tcp_sack_permitted"}, 1296 { 0, 1, 0, "tcp_trace"}, 1297 { 0, 1, 1, "tcp_compression_enabled"}, 1298 { 0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS, "tcp_ipv6_hoplimit"}, 1299 { 1, TCP_MSS_MAX_IPV6, 1220, "tcp_mss_def_ipv6"}, 1300 { 1, TCP_MSS_MAX_IPV6, TCP_MSS_MAX_IPV6, "tcp_mss_max_ipv6"}, 1301 { 0, 1, 0, "tcp_rev_src_routes"}, 1302 { 10*MS, 500*MS, 50*MS, "tcp_local_dack_interval"}, 1303 { 100*MS, 60*SECONDS, 1*SECONDS, "tcp_ndd_get_info_interval"}, 1304 { 0, 16, 8, "tcp_local_dacks_max"}, 1305 { 0, 2, 1, "tcp_ecn_permitted"}, 1306 { 0, 1, 1, "tcp_rst_sent_rate_enabled"}, 1307 { 0, PARAM_MAX, 40, "tcp_rst_sent_rate"}, 1308 { 0, 100*MS, 50*MS, "tcp_push_timer_interval"}, 1309 { 0, 1, 0, "tcp_use_smss_as_mss_opt"}, 1310 { 0, PARAM_MAX, 8*MINUTES, "tcp_keepalive_abort_interval"}, 1311 }; 1312 /* END CSTYLED */ 1313 1314 /* 1315 * tcp_mdt_hdr_{head,tail}_min are the leading and trailing spaces of 1316 * each header fragment in the header buffer. Each parameter value has 1317 * to be a multiple of 4 (32-bit aligned). 1318 */ 1319 static tcpparam_t tcp_mdt_head_param = { 32, 256, 32, "tcp_mdt_hdr_head_min" }; 1320 static tcpparam_t tcp_mdt_tail_param = { 0, 256, 32, "tcp_mdt_hdr_tail_min" }; 1321 #define tcp_mdt_hdr_head_min tcp_mdt_head_param.tcp_param_val 1322 #define tcp_mdt_hdr_tail_min tcp_mdt_tail_param.tcp_param_val 1323 1324 /* 1325 * tcp_mdt_max_pbufs is the upper limit value that tcp uses to figure out 1326 * the maximum number of payload buffers associated per Multidata. 1327 */ 1328 static tcpparam_t tcp_mdt_max_pbufs_param = 1329 { 1, MULTIDATA_MAX_PBUFS, MULTIDATA_MAX_PBUFS, "tcp_mdt_max_pbufs" }; 1330 #define tcp_mdt_max_pbufs tcp_mdt_max_pbufs_param.tcp_param_val 1331 1332 /* Round up the value to the nearest mss. */ 1333 #define MSS_ROUNDUP(value, mss) ((((value) - 1) / (mss) + 1) * (mss)) 1334 1335 /* 1336 * Set ECN capable transport (ECT) code point in IP header. 1337 * 1338 * Note that there are 2 ECT code points '01' and '10', which are called 1339 * ECT(1) and ECT(0) respectively. Here we follow the original ECT code 1340 * point ECT(0) for TCP as described in RFC 2481. 1341 */ 1342 #define SET_ECT(tcp, iph) \ 1343 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1344 /* We need to clear the code point first. */ \ 1345 ((ipha_t *)(iph))->ipha_type_of_service &= 0xFC; \ 1346 ((ipha_t *)(iph))->ipha_type_of_service |= IPH_ECN_ECT0; \ 1347 } else { \ 1348 ((ip6_t *)(iph))->ip6_vcf &= htonl(0xFFCFFFFF); \ 1349 ((ip6_t *)(iph))->ip6_vcf |= htonl(IPH_ECN_ECT0 << 20); \ 1350 } 1351 1352 /* 1353 * The format argument to pass to tcp_display(). 1354 * DISP_PORT_ONLY means that the returned string has only port info. 1355 * DISP_ADDR_AND_PORT means that the returned string also contains the 1356 * remote and local IP address. 1357 */ 1358 #define DISP_PORT_ONLY 1 1359 #define DISP_ADDR_AND_PORT 2 1360 1361 /* 1362 * This controls the rate some ndd info report functions can be used 1363 * by non-priviledged users. It stores the last time such info is 1364 * requested. When those report functions are called again, this 1365 * is checked with the current time and compare with the ndd param 1366 * tcp_ndd_get_info_interval. 1367 */ 1368 static clock_t tcp_last_ndd_get_info_time = 0; 1369 #define NDD_TOO_QUICK_MSG \ 1370 "ndd get info rate too high for non-priviledged users, try again " \ 1371 "later.\n" 1372 #define NDD_OUT_OF_BUF_MSG "<< Out of buffer >>\n" 1373 1374 #define IS_VMLOANED_MBLK(mp) \ 1375 (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0) 1376 1377 /* 1378 * These two variables control the rate for TCP to generate RSTs in 1379 * response to segments not belonging to any connections. We limit 1380 * TCP to sent out tcp_rst_sent_rate (ndd param) number of RSTs in 1381 * each 1 second interval. This is to protect TCP against DoS attack. 1382 */ 1383 static clock_t tcp_last_rst_intrvl; 1384 static uint32_t tcp_rst_cnt; 1385 1386 /* The number of RST not sent because of the rate limit. */ 1387 static uint32_t tcp_rst_unsent; 1388 1389 /* Enable or disable b_cont M_MULTIDATA chaining for MDT. */ 1390 boolean_t tcp_mdt_chain = B_TRUE; 1391 1392 /* 1393 * MDT threshold in the form of effective send MSS multiplier; we take 1394 * the MDT path if the amount of unsent data exceeds the threshold value 1395 * (default threshold is 1*SMSS). 1396 */ 1397 uint_t tcp_mdt_smss_threshold = 1; 1398 1399 uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */ 1400 1401 /* 1402 * Forces all connections to obey the value of the tcp_maxpsz_multiplier 1403 * tunable settable via NDD. Otherwise, the per-connection behavior is 1404 * determined dynamically during tcp_adapt_ire(), which is the default. 1405 */ 1406 boolean_t tcp_static_maxpsz = B_FALSE; 1407 1408 /* If set to 0, pick ephemeral port sequentially; otherwise randomly. */ 1409 uint32_t tcp_random_anon_port = 1; 1410 1411 /* 1412 * If tcp_drop_ack_unsent_cnt is greater than 0, when TCP receives more 1413 * than tcp_drop_ack_unsent_cnt number of ACKs which acknowledge unsent 1414 * data, TCP will not respond with an ACK. RFC 793 requires that 1415 * TCP responds with an ACK for such a bogus ACK. By not following 1416 * the RFC, we prevent TCP from getting into an ACK storm if somehow 1417 * an attacker successfully spoofs an acceptable segment to our 1418 * peer; or when our peer is "confused." 1419 */ 1420 uint32_t tcp_drop_ack_unsent_cnt = 10; 1421 1422 /* 1423 * Hook functions to enable cluster networking 1424 * On non-clustered systems these vectors must always be NULL. 1425 */ 1426 1427 void (*cl_inet_listen)(uint8_t protocol, sa_family_t addr_family, 1428 uint8_t *laddrp, in_port_t lport) = NULL; 1429 void (*cl_inet_unlisten)(uint8_t protocol, sa_family_t addr_family, 1430 uint8_t *laddrp, in_port_t lport) = NULL; 1431 void (*cl_inet_connect)(uint8_t protocol, sa_family_t addr_family, 1432 uint8_t *laddrp, in_port_t lport, 1433 uint8_t *faddrp, in_port_t fport) = NULL; 1434 void (*cl_inet_disconnect)(uint8_t protocol, sa_family_t addr_family, 1435 uint8_t *laddrp, in_port_t lport, 1436 uint8_t *faddrp, in_port_t fport) = NULL; 1437 1438 /* 1439 * The following are defined in ip.c 1440 */ 1441 extern int (*cl_inet_isclusterwide)(uint8_t protocol, sa_family_t addr_family, 1442 uint8_t *laddrp); 1443 extern uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family, 1444 uint8_t *laddrp, uint8_t *faddrp); 1445 1446 #define CL_INET_CONNECT(tcp) { \ 1447 if (cl_inet_connect != NULL) { \ 1448 /* \ 1449 * Running in cluster mode - register active connection \ 1450 * information \ 1451 */ \ 1452 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1453 if ((tcp)->tcp_ipha->ipha_src != 0) { \ 1454 (*cl_inet_connect)(IPPROTO_TCP, AF_INET,\ 1455 (uint8_t *)(&((tcp)->tcp_ipha->ipha_src)),\ 1456 (in_port_t)(tcp)->tcp_lport, \ 1457 (uint8_t *)(&((tcp)->tcp_ipha->ipha_dst)),\ 1458 (in_port_t)(tcp)->tcp_fport); \ 1459 } \ 1460 } else { \ 1461 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1462 &(tcp)->tcp_ip6h->ip6_src)) {\ 1463 (*cl_inet_connect)(IPPROTO_TCP, AF_INET6,\ 1464 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_src)),\ 1465 (in_port_t)(tcp)->tcp_lport, \ 1466 (uint8_t *)(&((tcp)->tcp_ip6h->ip6_dst)),\ 1467 (in_port_t)(tcp)->tcp_fport); \ 1468 } \ 1469 } \ 1470 } \ 1471 } 1472 1473 #define CL_INET_DISCONNECT(tcp) { \ 1474 if (cl_inet_disconnect != NULL) { \ 1475 /* \ 1476 * Running in cluster mode - deregister active \ 1477 * connection information \ 1478 */ \ 1479 if ((tcp)->tcp_ipversion == IPV4_VERSION) { \ 1480 if ((tcp)->tcp_ip_src != 0) { \ 1481 (*cl_inet_disconnect)(IPPROTO_TCP, \ 1482 AF_INET, \ 1483 (uint8_t *)(&((tcp)->tcp_ip_src)),\ 1484 (in_port_t)(tcp)->tcp_lport, \ 1485 (uint8_t *) \ 1486 (&((tcp)->tcp_ipha->ipha_dst)),\ 1487 (in_port_t)(tcp)->tcp_fport); \ 1488 } \ 1489 } else { \ 1490 if (!IN6_IS_ADDR_UNSPECIFIED( \ 1491 &(tcp)->tcp_ip_src_v6)) { \ 1492 (*cl_inet_disconnect)(IPPROTO_TCP, AF_INET6,\ 1493 (uint8_t *)(&((tcp)->tcp_ip_src_v6)),\ 1494 (in_port_t)(tcp)->tcp_lport, \ 1495 (uint8_t *) \ 1496 (&((tcp)->tcp_ip6h->ip6_dst)),\ 1497 (in_port_t)(tcp)->tcp_fport); \ 1498 } \ 1499 } \ 1500 } \ 1501 } 1502 1503 /* 1504 * Cluster networking hook for traversing current connection list. 1505 * This routine is used to extract the current list of live connections 1506 * which must continue to to be dispatched to this node. 1507 */ 1508 int cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg); 1509 1510 /* 1511 * Figure out the value of window scale opton. Note that the rwnd is 1512 * ASSUMED to be rounded up to the nearest MSS before the calculation. 1513 * We cannot find the scale value and then do a round up of tcp_rwnd 1514 * because the scale value may not be correct after that. 1515 * 1516 * Set the compiler flag to make this function inline. 1517 */ 1518 static void 1519 tcp_set_ws_value(tcp_t *tcp) 1520 { 1521 int i; 1522 uint32_t rwnd = tcp->tcp_rwnd; 1523 1524 for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT; 1525 i++, rwnd >>= 1) 1526 ; 1527 tcp->tcp_rcv_ws = i; 1528 } 1529 1530 /* 1531 * Remove a connection from the list of detached TIME_WAIT connections. 1532 */ 1533 static void 1534 tcp_time_wait_remove(tcp_t *tcp, tcp_squeue_priv_t *tcp_time_wait) 1535 { 1536 boolean_t locked = B_FALSE; 1537 1538 if (tcp_time_wait == NULL) { 1539 tcp_time_wait = *((tcp_squeue_priv_t **) 1540 squeue_getprivate(tcp->tcp_connp->conn_sqp, SQPRIVATE_TCP)); 1541 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1542 locked = B_TRUE; 1543 } 1544 1545 if (tcp->tcp_time_wait_expire == 0) { 1546 ASSERT(tcp->tcp_time_wait_next == NULL); 1547 ASSERT(tcp->tcp_time_wait_prev == NULL); 1548 if (locked) 1549 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1550 return; 1551 } 1552 ASSERT(TCP_IS_DETACHED(tcp)); 1553 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1554 1555 if (tcp == tcp_time_wait->tcp_time_wait_head) { 1556 ASSERT(tcp->tcp_time_wait_prev == NULL); 1557 tcp_time_wait->tcp_time_wait_head = tcp->tcp_time_wait_next; 1558 if (tcp_time_wait->tcp_time_wait_head != NULL) { 1559 tcp_time_wait->tcp_time_wait_head->tcp_time_wait_prev = 1560 NULL; 1561 } else { 1562 tcp_time_wait->tcp_time_wait_tail = NULL; 1563 } 1564 } else if (tcp == tcp_time_wait->tcp_time_wait_tail) { 1565 ASSERT(tcp != tcp_time_wait->tcp_time_wait_head); 1566 ASSERT(tcp->tcp_time_wait_next == NULL); 1567 tcp_time_wait->tcp_time_wait_tail = tcp->tcp_time_wait_prev; 1568 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1569 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = NULL; 1570 } else { 1571 ASSERT(tcp->tcp_time_wait_prev->tcp_time_wait_next == tcp); 1572 ASSERT(tcp->tcp_time_wait_next->tcp_time_wait_prev == tcp); 1573 tcp->tcp_time_wait_prev->tcp_time_wait_next = 1574 tcp->tcp_time_wait_next; 1575 tcp->tcp_time_wait_next->tcp_time_wait_prev = 1576 tcp->tcp_time_wait_prev; 1577 } 1578 tcp->tcp_time_wait_next = NULL; 1579 tcp->tcp_time_wait_prev = NULL; 1580 tcp->tcp_time_wait_expire = 0; 1581 1582 if (locked) 1583 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1584 } 1585 1586 /* 1587 * Add a connection to the list of detached TIME_WAIT connections 1588 * and set its time to expire. 1589 */ 1590 static void 1591 tcp_time_wait_append(tcp_t *tcp) 1592 { 1593 tcp_squeue_priv_t *tcp_time_wait = 1594 *((tcp_squeue_priv_t **)squeue_getprivate(tcp->tcp_connp->conn_sqp, 1595 SQPRIVATE_TCP)); 1596 1597 tcp_timers_stop(tcp); 1598 1599 /* Freed above */ 1600 ASSERT(tcp->tcp_timer_tid == 0); 1601 ASSERT(tcp->tcp_ack_tid == 0); 1602 1603 /* must have happened at the time of detaching the tcp */ 1604 ASSERT(tcp->tcp_ptpahn == NULL); 1605 ASSERT(tcp->tcp_flow_stopped == 0); 1606 ASSERT(tcp->tcp_time_wait_next == NULL); 1607 ASSERT(tcp->tcp_time_wait_prev == NULL); 1608 ASSERT(tcp->tcp_time_wait_expire == NULL); 1609 ASSERT(tcp->tcp_listener == NULL); 1610 1611 tcp->tcp_time_wait_expire = ddi_get_lbolt(); 1612 /* 1613 * The value computed below in tcp->tcp_time_wait_expire may 1614 * appear negative or wrap around. That is ok since our 1615 * interest is only in the difference between the current lbolt 1616 * value and tcp->tcp_time_wait_expire. But the value should not 1617 * be zero, since it means the tcp is not in the TIME_WAIT list. 1618 * The corresponding comparison in tcp_time_wait_collector() uses 1619 * modular arithmetic. 1620 */ 1621 tcp->tcp_time_wait_expire += 1622 drv_usectohz(tcp_time_wait_interval * 1000); 1623 if (tcp->tcp_time_wait_expire == 0) 1624 tcp->tcp_time_wait_expire = 1; 1625 1626 ASSERT(TCP_IS_DETACHED(tcp)); 1627 ASSERT(tcp->tcp_state == TCPS_TIME_WAIT); 1628 ASSERT(tcp->tcp_time_wait_next == NULL); 1629 ASSERT(tcp->tcp_time_wait_prev == NULL); 1630 TCP_DBGSTAT(tcp_time_wait); 1631 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 1632 if (tcp_time_wait->tcp_time_wait_head == NULL) { 1633 ASSERT(tcp_time_wait->tcp_time_wait_tail == NULL); 1634 tcp_time_wait->tcp_time_wait_head = tcp; 1635 } else { 1636 ASSERT(tcp_time_wait->tcp_time_wait_tail != NULL); 1637 ASSERT(tcp_time_wait->tcp_time_wait_tail->tcp_state == 1638 TCPS_TIME_WAIT); 1639 tcp_time_wait->tcp_time_wait_tail->tcp_time_wait_next = tcp; 1640 tcp->tcp_time_wait_prev = tcp_time_wait->tcp_time_wait_tail; 1641 } 1642 tcp_time_wait->tcp_time_wait_tail = tcp; 1643 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 1644 } 1645 1646 /* ARGSUSED */ 1647 void 1648 tcp_timewait_output(void *arg, mblk_t *mp, void *arg2) 1649 { 1650 conn_t *connp = (conn_t *)arg; 1651 tcp_t *tcp = connp->conn_tcp; 1652 1653 ASSERT(tcp != NULL); 1654 if (tcp->tcp_state == TCPS_CLOSED) { 1655 return; 1656 } 1657 1658 ASSERT((tcp->tcp_family == AF_INET && 1659 tcp->tcp_ipversion == IPV4_VERSION) || 1660 (tcp->tcp_family == AF_INET6 && 1661 (tcp->tcp_ipversion == IPV4_VERSION || 1662 tcp->tcp_ipversion == IPV6_VERSION))); 1663 ASSERT(!tcp->tcp_listener); 1664 1665 TCP_STAT(tcp_time_wait_reap); 1666 ASSERT(TCP_IS_DETACHED(tcp)); 1667 1668 /* 1669 * Because they have no upstream client to rebind or tcp_close() 1670 * them later, we axe the connection here and now. 1671 */ 1672 tcp_close_detached(tcp); 1673 } 1674 1675 void 1676 tcp_cleanup(tcp_t *tcp) 1677 { 1678 mblk_t *mp; 1679 char *tcp_iphc; 1680 int tcp_iphc_len; 1681 int tcp_hdr_grown; 1682 tcp_sack_info_t *tcp_sack_info; 1683 conn_t *connp = tcp->tcp_connp; 1684 1685 tcp_bind_hash_remove(tcp); 1686 tcp_free(tcp); 1687 1688 /* Release any SSL context */ 1689 if (tcp->tcp_kssl_ent != NULL) { 1690 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 1691 tcp->tcp_kssl_ent = NULL; 1692 } 1693 1694 if (tcp->tcp_kssl_ctx != NULL) { 1695 kssl_release_ctx(tcp->tcp_kssl_ctx); 1696 tcp->tcp_kssl_ctx = NULL; 1697 } 1698 tcp->tcp_kssl_pending = B_FALSE; 1699 1700 conn_delete_ire(connp, NULL); 1701 if (connp->conn_flags & IPCL_TCPCONN) { 1702 if (connp->conn_latch != NULL) 1703 IPLATCH_REFRELE(connp->conn_latch); 1704 if (connp->conn_policy != NULL) 1705 IPPH_REFRELE(connp->conn_policy); 1706 } 1707 1708 /* 1709 * Since we will bzero the entire structure, we need to 1710 * remove it and reinsert it in global hash list. We 1711 * know the walkers can't get to this conn because we 1712 * had set CONDEMNED flag earlier and checked reference 1713 * under conn_lock so walker won't pick it and when we 1714 * go the ipcl_globalhash_remove() below, no walker 1715 * can get to it. 1716 */ 1717 ipcl_globalhash_remove(connp); 1718 1719 /* Save some state */ 1720 mp = tcp->tcp_timercache; 1721 1722 tcp_sack_info = tcp->tcp_sack_info; 1723 tcp_iphc = tcp->tcp_iphc; 1724 tcp_iphc_len = tcp->tcp_iphc_len; 1725 tcp_hdr_grown = tcp->tcp_hdr_grown; 1726 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 eager->tcp_cred = econnp->conn_cred = aconnp->conn_cred; 2414 econnp->conn_zoneid = aconnp->conn_zoneid; 2415 aconnp->conn_cred = NULL; 2416 2417 /* Do the IPC initialization */ 2418 CONN_INC_REF(econnp); 2419 2420 econnp->conn_multicast_loop = aconnp->conn_multicast_loop; 2421 econnp->conn_af_isv6 = aconnp->conn_af_isv6; 2422 econnp->conn_pkt_isv6 = aconnp->conn_pkt_isv6; 2423 econnp->conn_ulp = aconnp->conn_ulp; 2424 2425 /* Done with old IPC. Drop its ref on its connp */ 2426 CONN_DEC_REF(aconnp); 2427 } 2428 2429 2430 /* 2431 * Adapt to the information, such as rtt and rtt_sd, provided from the 2432 * ire cached in conn_cache_ire. If no ire cached, do a ire lookup. 2433 * 2434 * Checks for multicast and broadcast destination address. 2435 * Returns zero on failure; non-zero if ok. 2436 * 2437 * Note that the MSS calculation here is based on the info given in 2438 * the IRE. We do not do any calculation based on TCP options. They 2439 * will be handled in tcp_rput_other() and tcp_rput_data() when TCP 2440 * knows which options to use. 2441 * 2442 * Note on how TCP gets its parameters for a connection. 2443 * 2444 * When a tcp_t structure is allocated, it gets all the default parameters. 2445 * In tcp_adapt_ire(), it gets those metric parameters, like rtt, rtt_sd, 2446 * spipe, rpipe, ... from the route metrics. Route metric overrides the 2447 * default. But if there is an associated tcp_host_param, it will override 2448 * the metrics. 2449 * 2450 * An incoming SYN with a multicast or broadcast destination address, is dropped 2451 * in 1 of 2 places. 2452 * 2453 * 1. If the packet was received over the wire it is dropped in 2454 * ip_rput_process_broadcast() 2455 * 2456 * 2. If the packet was received through internal IP loopback, i.e. the packet 2457 * was generated and received on the same machine, it is dropped in 2458 * ip_wput_local() 2459 * 2460 * An incoming SYN with a multicast or broadcast source address is always 2461 * dropped in tcp_adapt_ire. The same logic in tcp_adapt_ire also serves to 2462 * reject an attempt to connect to a broadcast or multicast (destination) 2463 * address. 2464 */ 2465 static int 2466 tcp_adapt_ire(tcp_t *tcp, mblk_t *ire_mp) 2467 { 2468 tcp_hsp_t *hsp; 2469 ire_t *ire; 2470 ire_t *sire = NULL; 2471 iulp_t *ire_uinfo = NULL; 2472 uint32_t mss_max; 2473 uint32_t mss; 2474 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 2475 conn_t *connp = tcp->tcp_connp; 2476 boolean_t ire_cacheable = B_FALSE; 2477 zoneid_t zoneid = connp->conn_zoneid; 2478 ill_t *ill = NULL; 2479 boolean_t incoming = (ire_mp == NULL); 2480 2481 ASSERT(connp->conn_ire_cache == NULL); 2482 2483 if (tcp->tcp_ipversion == IPV4_VERSION) { 2484 2485 if (CLASSD(tcp->tcp_connp->conn_rem)) { 2486 BUMP_MIB(&ip_mib, ipInDiscards); 2487 return (0); 2488 } 2489 /* 2490 * If IP_NEXTHOP is set, then look for an IRE_CACHE 2491 * for the destination with the nexthop as gateway. 2492 * ire_ctable_lookup() is used because this particular 2493 * ire, if it exists, will be marked private. 2494 * If that is not available, use the interface ire 2495 * for the nexthop. 2496 */ 2497 if (tcp->tcp_connp->conn_nexthop_set) { 2498 ire = ire_ctable_lookup(tcp->tcp_connp->conn_rem, 2499 tcp->tcp_connp->conn_nexthop_v4, 0, NULL, zoneid, 2500 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW); 2501 if (ire == NULL) { 2502 ire = ire_ftable_lookup( 2503 tcp->tcp_connp->conn_nexthop_v4, 2504 0, 0, IRE_INTERFACE, NULL, NULL, zoneid, 0, 2505 MATCH_IRE_TYPE); 2506 if (ire == NULL) 2507 return (0); 2508 } else { 2509 ire_uinfo = &ire->ire_uinfo; 2510 } 2511 } else { 2512 ire = ire_cache_lookup(tcp->tcp_connp->conn_rem, 2513 zoneid); 2514 if (ire != NULL) { 2515 ire_cacheable = B_TRUE; 2516 ire_uinfo = (ire_mp != NULL) ? 2517 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2518 &ire->ire_uinfo; 2519 2520 } else { 2521 if (ire_mp == NULL) { 2522 ire = ire_ftable_lookup( 2523 tcp->tcp_connp->conn_rem, 2524 0, 0, 0, NULL, &sire, zoneid, 0, 2525 (MATCH_IRE_RECURSIVE | 2526 MATCH_IRE_DEFAULT)); 2527 if (ire == NULL) 2528 return (0); 2529 ire_uinfo = (sire != NULL) ? 2530 &sire->ire_uinfo : 2531 &ire->ire_uinfo; 2532 } else { 2533 ire = (ire_t *)ire_mp->b_rptr; 2534 ire_uinfo = 2535 &((ire_t *) 2536 ire_mp->b_rptr)->ire_uinfo; 2537 } 2538 } 2539 } 2540 ASSERT(ire != NULL); 2541 2542 if ((ire->ire_src_addr == INADDR_ANY) || 2543 (ire->ire_type & IRE_BROADCAST)) { 2544 /* 2545 * ire->ire_mp is non null when ire_mp passed in is used 2546 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2547 */ 2548 if (ire->ire_mp == NULL) 2549 ire_refrele(ire); 2550 if (sire != NULL) 2551 ire_refrele(sire); 2552 return (0); 2553 } 2554 2555 if (tcp->tcp_ipha->ipha_src == INADDR_ANY) { 2556 ipaddr_t src_addr; 2557 2558 /* 2559 * ip_bind_connected() has stored the correct source 2560 * address in conn_src. 2561 */ 2562 src_addr = tcp->tcp_connp->conn_src; 2563 tcp->tcp_ipha->ipha_src = src_addr; 2564 /* 2565 * Copy of the src addr. in tcp_t is needed 2566 * for the lookup funcs. 2567 */ 2568 IN6_IPADDR_TO_V4MAPPED(src_addr, &tcp->tcp_ip_src_v6); 2569 } 2570 /* 2571 * Set the fragment bit so that IP will tell us if the MTU 2572 * should change. IP tells us the latest setting of 2573 * ip_path_mtu_discovery through ire_frag_flag. 2574 */ 2575 if (ip_path_mtu_discovery) { 2576 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 2577 htons(IPH_DF); 2578 } 2579 /* 2580 * If ire_uinfo is NULL, this is the IRE_INTERFACE case 2581 * for IP_NEXTHOP. No cache ire has been found for the 2582 * destination and we are working with the nexthop's 2583 * interface ire. Since we need to forward all packets 2584 * to the nexthop first, we "blindly" set tcp_localnet 2585 * to false, eventhough the destination may also be 2586 * onlink. 2587 */ 2588 if (ire_uinfo == NULL) 2589 tcp->tcp_localnet = 0; 2590 else 2591 tcp->tcp_localnet = (ire->ire_gateway_addr == 0); 2592 } else { 2593 /* 2594 * For incoming connection ire_mp = NULL 2595 * For outgoing connection ire_mp != NULL 2596 * Technically we should check conn_incoming_ill 2597 * when ire_mp is NULL and conn_outgoing_ill when 2598 * ire_mp is non-NULL. But this is performance 2599 * critical path and for IPV*_BOUND_IF, outgoing 2600 * and incoming ill are always set to the same value. 2601 */ 2602 ill_t *dst_ill = NULL; 2603 ipif_t *dst_ipif = NULL; 2604 int match_flags = MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT; 2605 2606 ASSERT(connp->conn_outgoing_ill == connp->conn_incoming_ill); 2607 2608 if (connp->conn_outgoing_ill != NULL) { 2609 /* Outgoing or incoming path */ 2610 int err; 2611 2612 dst_ill = conn_get_held_ill(connp, 2613 &connp->conn_outgoing_ill, &err); 2614 if (err == ILL_LOOKUP_FAILED || dst_ill == NULL) { 2615 ip1dbg(("tcp_adapt_ire: ill_lookup failed\n")); 2616 return (0); 2617 } 2618 match_flags |= MATCH_IRE_ILL; 2619 dst_ipif = dst_ill->ill_ipif; 2620 } 2621 ire = ire_ctable_lookup_v6(&tcp->tcp_connp->conn_remv6, 2622 0, 0, dst_ipif, zoneid, match_flags); 2623 2624 if (ire != NULL) { 2625 ire_cacheable = B_TRUE; 2626 ire_uinfo = (ire_mp != NULL) ? 2627 &((ire_t *)ire_mp->b_rptr)->ire_uinfo: 2628 &ire->ire_uinfo; 2629 } else { 2630 if (ire_mp == NULL) { 2631 ire = ire_ftable_lookup_v6( 2632 &tcp->tcp_connp->conn_remv6, 2633 0, 0, 0, dst_ipif, &sire, zoneid, 2634 0, match_flags); 2635 if (ire == NULL) { 2636 if (dst_ill != NULL) 2637 ill_refrele(dst_ill); 2638 return (0); 2639 } 2640 ire_uinfo = (sire != NULL) ? &sire->ire_uinfo : 2641 &ire->ire_uinfo; 2642 } else { 2643 ire = (ire_t *)ire_mp->b_rptr; 2644 ire_uinfo = 2645 &((ire_t *)ire_mp->b_rptr)->ire_uinfo; 2646 } 2647 } 2648 if (dst_ill != NULL) 2649 ill_refrele(dst_ill); 2650 2651 ASSERT(ire != NULL); 2652 ASSERT(ire_uinfo != NULL); 2653 2654 if (IN6_IS_ADDR_UNSPECIFIED(&ire->ire_src_addr_v6) || 2655 IN6_IS_ADDR_MULTICAST(&ire->ire_addr_v6)) { 2656 /* 2657 * ire->ire_mp is non null when ire_mp passed in is used 2658 * ire->ire_mp is set in ip_bind_insert_ire[_v6](). 2659 */ 2660 if (ire->ire_mp == NULL) 2661 ire_refrele(ire); 2662 if (sire != NULL) 2663 ire_refrele(sire); 2664 return (0); 2665 } 2666 2667 if (IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 2668 in6_addr_t src_addr; 2669 2670 /* 2671 * ip_bind_connected_v6() has stored the correct source 2672 * address per IPv6 addr. selection policy in 2673 * conn_src_v6. 2674 */ 2675 src_addr = tcp->tcp_connp->conn_srcv6; 2676 2677 tcp->tcp_ip6h->ip6_src = src_addr; 2678 /* 2679 * Copy of the src addr. in tcp_t is needed 2680 * for the lookup funcs. 2681 */ 2682 tcp->tcp_ip_src_v6 = src_addr; 2683 ASSERT(IN6_ARE_ADDR_EQUAL(&tcp->tcp_ip6h->ip6_src, 2684 &connp->conn_srcv6)); 2685 } 2686 tcp->tcp_localnet = 2687 IN6_IS_ADDR_UNSPECIFIED(&ire->ire_gateway_addr_v6); 2688 } 2689 2690 /* 2691 * This allows applications to fail quickly when connections are made 2692 * to dead hosts. Hosts can be labeled dead by adding a reject route 2693 * with both the RTF_REJECT and RTF_PRIVATE flags set. 2694 */ 2695 if ((ire->ire_flags & RTF_REJECT) && 2696 (ire->ire_flags & RTF_PRIVATE)) 2697 goto error; 2698 2699 /* 2700 * Make use of the cached rtt and rtt_sd values to calculate the 2701 * initial RTO. Note that they are already initialized in 2702 * tcp_init_values(). 2703 * If ire_uinfo is NULL, i.e., we do not have a cache ire for 2704 * IP_NEXTHOP, but instead are using the interface ire for the 2705 * nexthop, then we do not use the ire_uinfo from that ire to 2706 * do any initializations. 2707 */ 2708 if (ire_uinfo != NULL) { 2709 if (ire_uinfo->iulp_rtt != 0) { 2710 clock_t rto; 2711 2712 tcp->tcp_rtt_sa = ire_uinfo->iulp_rtt; 2713 tcp->tcp_rtt_sd = ire_uinfo->iulp_rtt_sd; 2714 rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 2715 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5); 2716 2717 if (rto > tcp_rexmit_interval_max) { 2718 tcp->tcp_rto = tcp_rexmit_interval_max; 2719 } else if (rto < tcp_rexmit_interval_min) { 2720 tcp->tcp_rto = tcp_rexmit_interval_min; 2721 } else { 2722 tcp->tcp_rto = rto; 2723 } 2724 } 2725 if (ire_uinfo->iulp_ssthresh != 0) 2726 tcp->tcp_cwnd_ssthresh = ire_uinfo->iulp_ssthresh; 2727 else 2728 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 2729 if (ire_uinfo->iulp_spipe > 0) { 2730 tcp->tcp_xmit_hiwater = MIN(ire_uinfo->iulp_spipe, 2731 tcp_max_buf); 2732 if (tcp_snd_lowat_fraction != 0) 2733 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2734 tcp_snd_lowat_fraction; 2735 (void) tcp_maxpsz_set(tcp, B_TRUE); 2736 } 2737 /* 2738 * Note that up till now, acceptor always inherits receive 2739 * window from the listener. But if there is a metrics 2740 * associated with a host, we should use that instead of 2741 * inheriting it from listener. Thus we need to pass this 2742 * info back to the caller. 2743 */ 2744 if (ire_uinfo->iulp_rpipe > 0) { 2745 tcp->tcp_rwnd = MIN(ire_uinfo->iulp_rpipe, tcp_max_buf); 2746 } 2747 2748 if (ire_uinfo->iulp_rtomax > 0) { 2749 tcp->tcp_second_timer_threshold = 2750 ire_uinfo->iulp_rtomax; 2751 } 2752 2753 /* 2754 * Use the metric option settings, iulp_tstamp_ok and 2755 * iulp_wscale_ok, only for active open. What this means 2756 * is that if the other side uses timestamp or window 2757 * scale option, TCP will also use those options. That 2758 * is for passive open. If the application sets a 2759 * large window, window scale is enabled regardless of 2760 * the value in iulp_wscale_ok. This is the behavior 2761 * since 2.6. So we keep it. 2762 * The only case left in passive open processing is the 2763 * check for SACK. 2764 * For ECN, it should probably be like SACK. But the 2765 * current value is binary, so we treat it like the other 2766 * cases. The metric only controls active open.For passive 2767 * open, the ndd param, tcp_ecn_permitted, controls the 2768 * behavior. 2769 */ 2770 if (!tcp_detached) { 2771 /* 2772 * The if check means that the following can only 2773 * be turned on by the metrics only IRE, but not off. 2774 */ 2775 if (ire_uinfo->iulp_tstamp_ok) 2776 tcp->tcp_snd_ts_ok = B_TRUE; 2777 if (ire_uinfo->iulp_wscale_ok) 2778 tcp->tcp_snd_ws_ok = B_TRUE; 2779 if (ire_uinfo->iulp_sack == 2) 2780 tcp->tcp_snd_sack_ok = B_TRUE; 2781 if (ire_uinfo->iulp_ecn_ok) 2782 tcp->tcp_ecn_ok = B_TRUE; 2783 } else { 2784 /* 2785 * Passive open. 2786 * 2787 * As above, the if check means that SACK can only be 2788 * turned on by the metric only IRE. 2789 */ 2790 if (ire_uinfo->iulp_sack > 0) { 2791 tcp->tcp_snd_sack_ok = B_TRUE; 2792 } 2793 } 2794 } 2795 2796 2797 /* 2798 * XXX: Note that currently, ire_max_frag can be as small as 68 2799 * because of PMTUd. So tcp_mss may go to negative if combined 2800 * length of all those options exceeds 28 bytes. But because 2801 * of the tcp_mss_min check below, we may not have a problem if 2802 * tcp_mss_min is of a reasonable value. The default is 1 so 2803 * the negative problem still exists. And the check defeats PMTUd. 2804 * In fact, if PMTUd finds that the MSS should be smaller than 2805 * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min 2806 * value. 2807 * 2808 * We do not deal with that now. All those problems related to 2809 * PMTUd will be fixed later. 2810 */ 2811 ASSERT(ire->ire_max_frag != 0); 2812 mss = tcp->tcp_if_mtu = ire->ire_max_frag; 2813 if (tcp->tcp_ipp_fields & IPPF_USE_MIN_MTU) { 2814 if (tcp->tcp_ipp_use_min_mtu == IPV6_USE_MIN_MTU_NEVER) { 2815 mss = MIN(mss, IPV6_MIN_MTU); 2816 } 2817 } 2818 2819 /* Sanity check for MSS value. */ 2820 if (tcp->tcp_ipversion == IPV4_VERSION) 2821 mss_max = tcp_mss_max_ipv4; 2822 else 2823 mss_max = tcp_mss_max_ipv6; 2824 2825 if (tcp->tcp_ipversion == IPV6_VERSION && 2826 (ire->ire_frag_flag & IPH_FRAG_HDR)) { 2827 /* 2828 * After receiving an ICMPv6 "packet too big" message with a 2829 * MTU < 1280, and for multirouted IPv6 packets, the IP layer 2830 * will insert a 8-byte fragment header in every packet; we 2831 * reduce the MSS by that amount here. 2832 */ 2833 mss -= sizeof (ip6_frag_t); 2834 } 2835 2836 if (tcp->tcp_ipsec_overhead == 0) 2837 tcp->tcp_ipsec_overhead = conn_ipsec_length(connp); 2838 2839 mss -= tcp->tcp_ipsec_overhead; 2840 2841 if (mss < tcp_mss_min) 2842 mss = tcp_mss_min; 2843 if (mss > mss_max) 2844 mss = mss_max; 2845 2846 /* Note that this is the maximum MSS, excluding all options. */ 2847 tcp->tcp_mss = mss; 2848 2849 /* 2850 * Initialize the ISS here now that we have the full connection ID. 2851 * The RFC 1948 method of initial sequence number generation requires 2852 * knowledge of the full connection ID before setting the ISS. 2853 */ 2854 2855 tcp_iss_init(tcp); 2856 2857 if (ire->ire_type & (IRE_LOOPBACK | IRE_LOCAL)) 2858 tcp->tcp_loopback = B_TRUE; 2859 2860 if (tcp->tcp_ipversion == IPV4_VERSION) { 2861 hsp = tcp_hsp_lookup(tcp->tcp_remote); 2862 } else { 2863 hsp = tcp_hsp_lookup_ipv6(&tcp->tcp_remote_v6); 2864 } 2865 2866 if (hsp != NULL) { 2867 /* Only modify if we're going to make them bigger */ 2868 if (hsp->tcp_hsp_sendspace > tcp->tcp_xmit_hiwater) { 2869 tcp->tcp_xmit_hiwater = hsp->tcp_hsp_sendspace; 2870 if (tcp_snd_lowat_fraction != 0) 2871 tcp->tcp_xmit_lowater = tcp->tcp_xmit_hiwater / 2872 tcp_snd_lowat_fraction; 2873 } 2874 2875 if (hsp->tcp_hsp_recvspace > tcp->tcp_rwnd) { 2876 tcp->tcp_rwnd = hsp->tcp_hsp_recvspace; 2877 } 2878 2879 /* Copy timestamp flag only for active open */ 2880 if (!tcp_detached) 2881 tcp->tcp_snd_ts_ok = hsp->tcp_hsp_tstamp; 2882 } 2883 2884 if (sire != NULL) 2885 IRE_REFRELE(sire); 2886 2887 /* 2888 * If we got an IRE_CACHE and an ILL, go through their properties; 2889 * otherwise, this is deferred until later when we have an IRE_CACHE. 2890 */ 2891 if (tcp->tcp_loopback || 2892 (ire_cacheable && (ill = ire_to_ill(ire)) != NULL)) { 2893 /* 2894 * For incoming, see if this tcp may be MDT-capable. For 2895 * outgoing, this process has been taken care of through 2896 * tcp_rput_other. 2897 */ 2898 tcp_ire_ill_check(tcp, ire, ill, incoming); 2899 tcp->tcp_ire_ill_check_done = B_TRUE; 2900 } 2901 2902 mutex_enter(&connp->conn_lock); 2903 /* 2904 * Make sure that conn is not marked incipient 2905 * for incoming connections. A blind 2906 * removal of incipient flag is cheaper than 2907 * check and removal. 2908 */ 2909 connp->conn_state_flags &= ~CONN_INCIPIENT; 2910 2911 /* Must not cache forwarding table routes. */ 2912 if (ire_cacheable) { 2913 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 2914 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 2915 connp->conn_ire_cache = ire; 2916 IRE_UNTRACE_REF(ire); 2917 rw_exit(&ire->ire_bucket->irb_lock); 2918 mutex_exit(&connp->conn_lock); 2919 return (1); 2920 } 2921 rw_exit(&ire->ire_bucket->irb_lock); 2922 } 2923 mutex_exit(&connp->conn_lock); 2924 2925 if (ire->ire_mp == NULL) 2926 ire_refrele(ire); 2927 return (1); 2928 2929 error: 2930 if (ire->ire_mp == NULL) 2931 ire_refrele(ire); 2932 if (sire != NULL) 2933 ire_refrele(sire); 2934 return (0); 2935 } 2936 2937 /* 2938 * tcp_bind is called (holding the writer lock) by tcp_wput_proto to process a 2939 * O_T_BIND_REQ/T_BIND_REQ message. 2940 */ 2941 static void 2942 tcp_bind(tcp_t *tcp, mblk_t *mp) 2943 { 2944 sin_t *sin; 2945 sin6_t *sin6; 2946 mblk_t *mp1; 2947 in_port_t requested_port; 2948 in_port_t allocated_port; 2949 struct T_bind_req *tbr; 2950 boolean_t bind_to_req_port_only; 2951 boolean_t backlog_update = B_FALSE; 2952 boolean_t user_specified; 2953 in6_addr_t v6addr; 2954 ipaddr_t v4addr; 2955 uint_t origipversion; 2956 int err; 2957 queue_t *q = tcp->tcp_wq; 2958 2959 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 2960 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) { 2961 if (tcp->tcp_debug) { 2962 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 2963 "tcp_bind: bad req, len %u", 2964 (uint_t)(mp->b_wptr - mp->b_rptr)); 2965 } 2966 tcp_err_ack(tcp, mp, TPROTO, 0); 2967 return; 2968 } 2969 /* Make sure the largest address fits */ 2970 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t) + 1, 1); 2971 if (mp1 == NULL) { 2972 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 2973 return; 2974 } 2975 mp = mp1; 2976 tbr = (struct T_bind_req *)mp->b_rptr; 2977 if (tcp->tcp_state >= TCPS_BOUND) { 2978 if ((tcp->tcp_state == TCPS_BOUND || 2979 tcp->tcp_state == TCPS_LISTEN) && 2980 tcp->tcp_conn_req_max != tbr->CONIND_number && 2981 tbr->CONIND_number > 0) { 2982 /* 2983 * Handle listen() increasing CONIND_number. 2984 * This is more "liberal" then what the TPI spec 2985 * requires but is needed to avoid a t_unbind 2986 * when handling listen() since the port number 2987 * might be "stolen" between the unbind and bind. 2988 */ 2989 backlog_update = B_TRUE; 2990 goto do_bind; 2991 } 2992 if (tcp->tcp_debug) { 2993 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 2994 "tcp_bind: bad state, %d", tcp->tcp_state); 2995 } 2996 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 2997 return; 2998 } 2999 origipversion = tcp->tcp_ipversion; 3000 3001 switch (tbr->ADDR_length) { 3002 case 0: /* request for a generic port */ 3003 tbr->ADDR_offset = sizeof (struct T_bind_req); 3004 if (tcp->tcp_family == AF_INET) { 3005 tbr->ADDR_length = sizeof (sin_t); 3006 sin = (sin_t *)&tbr[1]; 3007 *sin = sin_null; 3008 sin->sin_family = AF_INET; 3009 mp->b_wptr = (uchar_t *)&sin[1]; 3010 tcp->tcp_ipversion = IPV4_VERSION; 3011 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &v6addr); 3012 } else { 3013 ASSERT(tcp->tcp_family == AF_INET6); 3014 tbr->ADDR_length = sizeof (sin6_t); 3015 sin6 = (sin6_t *)&tbr[1]; 3016 *sin6 = sin6_null; 3017 sin6->sin6_family = AF_INET6; 3018 mp->b_wptr = (uchar_t *)&sin6[1]; 3019 tcp->tcp_ipversion = IPV6_VERSION; 3020 V6_SET_ZERO(v6addr); 3021 } 3022 requested_port = 0; 3023 break; 3024 3025 case sizeof (sin_t): /* Complete IPv4 address */ 3026 sin = (sin_t *)mi_offset_param(mp, tbr->ADDR_offset, 3027 sizeof (sin_t)); 3028 if (sin == NULL || !OK_32PTR((char *)sin)) { 3029 if (tcp->tcp_debug) { 3030 (void) strlog(TCP_MOD_ID, 0, 1, 3031 SL_ERROR|SL_TRACE, 3032 "tcp_bind: bad address parameter, " 3033 "offset %d, len %d", 3034 tbr->ADDR_offset, tbr->ADDR_length); 3035 } 3036 tcp_err_ack(tcp, mp, TPROTO, 0); 3037 return; 3038 } 3039 /* 3040 * With sockets sockfs will accept bogus sin_family in 3041 * bind() and replace it with the family used in the socket 3042 * call. 3043 */ 3044 if (sin->sin_family != AF_INET || 3045 tcp->tcp_family != AF_INET) { 3046 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3047 return; 3048 } 3049 requested_port = ntohs(sin->sin_port); 3050 tcp->tcp_ipversion = IPV4_VERSION; 3051 v4addr = sin->sin_addr.s_addr; 3052 IN6_IPADDR_TO_V4MAPPED(v4addr, &v6addr); 3053 break; 3054 3055 case sizeof (sin6_t): /* Complete IPv6 address */ 3056 sin6 = (sin6_t *)mi_offset_param(mp, 3057 tbr->ADDR_offset, sizeof (sin6_t)); 3058 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 3059 if (tcp->tcp_debug) { 3060 (void) strlog(TCP_MOD_ID, 0, 1, 3061 SL_ERROR|SL_TRACE, 3062 "tcp_bind: bad IPv6 address parameter, " 3063 "offset %d, len %d", tbr->ADDR_offset, 3064 tbr->ADDR_length); 3065 } 3066 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 3067 return; 3068 } 3069 if (sin6->sin6_family != AF_INET6 || 3070 tcp->tcp_family != AF_INET6) { 3071 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 3072 return; 3073 } 3074 requested_port = ntohs(sin6->sin6_port); 3075 tcp->tcp_ipversion = IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ? 3076 IPV4_VERSION : IPV6_VERSION; 3077 v6addr = sin6->sin6_addr; 3078 break; 3079 3080 default: 3081 if (tcp->tcp_debug) { 3082 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 3083 "tcp_bind: bad address length, %d", 3084 tbr->ADDR_length); 3085 } 3086 tcp_err_ack(tcp, mp, TBADADDR, 0); 3087 return; 3088 } 3089 tcp->tcp_bound_source_v6 = v6addr; 3090 3091 /* Check for change in ipversion */ 3092 if (origipversion != tcp->tcp_ipversion) { 3093 ASSERT(tcp->tcp_family == AF_INET6); 3094 err = tcp->tcp_ipversion == IPV6_VERSION ? 3095 tcp_header_init_ipv6(tcp) : tcp_header_init_ipv4(tcp); 3096 if (err) { 3097 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3098 return; 3099 } 3100 } 3101 3102 /* 3103 * Initialize family specific fields. Copy of the src addr. 3104 * in tcp_t is needed for the lookup funcs. 3105 */ 3106 if (tcp->tcp_ipversion == IPV6_VERSION) { 3107 tcp->tcp_ip6h->ip6_src = v6addr; 3108 } else { 3109 IN6_V4MAPPED_TO_IPADDR(&v6addr, tcp->tcp_ipha->ipha_src); 3110 } 3111 tcp->tcp_ip_src_v6 = v6addr; 3112 3113 /* 3114 * For O_T_BIND_REQ: 3115 * Verify that the target port/addr is available, or choose 3116 * another. 3117 * For T_BIND_REQ: 3118 * Verify that the target port/addr is available or fail. 3119 * In both cases when it succeeds the tcp is inserted in the 3120 * bind hash table. This ensures that the operation is atomic 3121 * under the lock on the hash bucket. 3122 */ 3123 bind_to_req_port_only = requested_port != 0 && 3124 tbr->PRIM_type != O_T_BIND_REQ; 3125 /* 3126 * Get a valid port (within the anonymous range and should not 3127 * be a privileged one) to use if the user has not given a port. 3128 * If multiple threads are here, they may all start with 3129 * with the same initial port. But, it should be fine as long as 3130 * tcp_bindi will ensure that no two threads will be assigned 3131 * the same port. 3132 * 3133 * NOTE: XXX If a privileged process asks for an anonymous port, we 3134 * still check for ports only in the range > tcp_smallest_non_priv_port, 3135 * unless TCP_ANONPRIVBIND option is set. 3136 */ 3137 if (requested_port == 0) { 3138 requested_port = tcp->tcp_anon_priv_bind ? 3139 tcp_get_next_priv_port() : 3140 tcp_update_next_port(tcp_next_port_to_try, B_TRUE); 3141 user_specified = B_FALSE; 3142 } else { 3143 int i; 3144 boolean_t priv = B_FALSE; 3145 /* 3146 * If the requested_port is in the well-known privileged range, 3147 * verify that the stream was opened by a privileged user. 3148 * Note: No locks are held when inspecting tcp_g_*epriv_ports 3149 * but instead the code relies on: 3150 * - the fact that the address of the array and its size never 3151 * changes 3152 * - the atomic assignment of the elements of the array 3153 */ 3154 if (requested_port < tcp_smallest_nonpriv_port) { 3155 priv = B_TRUE; 3156 } else { 3157 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 3158 if (requested_port == 3159 tcp_g_epriv_ports[i]) { 3160 priv = B_TRUE; 3161 break; 3162 } 3163 } 3164 } 3165 if (priv) { 3166 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 3167 3168 if (secpolicy_net_privaddr(cr, requested_port) != 0) { 3169 if (tcp->tcp_debug) { 3170 (void) strlog(TCP_MOD_ID, 0, 1, 3171 SL_ERROR|SL_TRACE, 3172 "tcp_bind: no priv for port %d", 3173 requested_port); 3174 } 3175 tcp_err_ack(tcp, mp, TACCES, 0); 3176 return; 3177 } 3178 } 3179 user_specified = B_TRUE; 3180 } 3181 3182 allocated_port = tcp_bindi(tcp, requested_port, &v6addr, 3183 tcp->tcp_reuseaddr, B_FALSE, bind_to_req_port_only, user_specified); 3184 3185 if (allocated_port == 0) { 3186 if (bind_to_req_port_only) { 3187 if (tcp->tcp_debug) { 3188 (void) strlog(TCP_MOD_ID, 0, 1, 3189 SL_ERROR|SL_TRACE, 3190 "tcp_bind: requested addr busy"); 3191 } 3192 tcp_err_ack(tcp, mp, TADDRBUSY, 0); 3193 } else { 3194 /* If we are out of ports, fail the bind. */ 3195 if (tcp->tcp_debug) { 3196 (void) strlog(TCP_MOD_ID, 0, 1, 3197 SL_ERROR|SL_TRACE, 3198 "tcp_bind: out of ports?"); 3199 } 3200 tcp_err_ack(tcp, mp, TNOADDR, 0); 3201 } 3202 return; 3203 } 3204 ASSERT(tcp->tcp_state == TCPS_BOUND); 3205 do_bind: 3206 if (!backlog_update) { 3207 if (tcp->tcp_family == AF_INET) 3208 sin->sin_port = htons(allocated_port); 3209 else 3210 sin6->sin6_port = htons(allocated_port); 3211 } 3212 if (tcp->tcp_family == AF_INET) { 3213 if (tbr->CONIND_number != 0) { 3214 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3215 sizeof (sin_t)); 3216 } else { 3217 /* Just verify the local IP address */ 3218 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, IP_ADDR_LEN); 3219 } 3220 } else { 3221 if (tbr->CONIND_number != 0) { 3222 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3223 sizeof (sin6_t)); 3224 } else { 3225 /* Just verify the local IP address */ 3226 mp1 = tcp_ip_bind_mp(tcp, tbr->PRIM_type, 3227 IPV6_ADDR_LEN); 3228 } 3229 } 3230 if (!mp1) { 3231 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 3232 return; 3233 } 3234 3235 tbr->PRIM_type = T_BIND_ACK; 3236 mp->b_datap->db_type = M_PCPROTO; 3237 3238 /* Chain in the reply mp for tcp_rput() */ 3239 mp1->b_cont = mp; 3240 mp = mp1; 3241 3242 tcp->tcp_conn_req_max = tbr->CONIND_number; 3243 if (tcp->tcp_conn_req_max) { 3244 if (tcp->tcp_conn_req_max < tcp_conn_req_min) 3245 tcp->tcp_conn_req_max = tcp_conn_req_min; 3246 if (tcp->tcp_conn_req_max > tcp_conn_req_max_q) 3247 tcp->tcp_conn_req_max = tcp_conn_req_max_q; 3248 /* 3249 * If this is a listener, do not reset the eager list 3250 * and other stuffs. Note that we don't check if the 3251 * existing eager list meets the new tcp_conn_req_max 3252 * requirement. 3253 */ 3254 if (tcp->tcp_state != TCPS_LISTEN) { 3255 tcp->tcp_state = TCPS_LISTEN; 3256 /* Initialize the chain. Don't need the eager_lock */ 3257 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 3258 tcp->tcp_second_ctimer_threshold = 3259 tcp_ip_abort_linterval; 3260 } 3261 } 3262 3263 /* 3264 * We can call ip_bind directly which returns a T_BIND_ACK mp. The 3265 * processing continues in tcp_rput_other(). 3266 */ 3267 if (tcp->tcp_family == AF_INET6) { 3268 ASSERT(tcp->tcp_connp->conn_af_isv6); 3269 mp = ip_bind_v6(q, mp, tcp->tcp_connp, &tcp->tcp_sticky_ipp); 3270 } else { 3271 ASSERT(!tcp->tcp_connp->conn_af_isv6); 3272 mp = ip_bind_v4(q, mp, tcp->tcp_connp); 3273 } 3274 /* 3275 * If the bind cannot complete immediately 3276 * IP will arrange to call tcp_rput_other 3277 * when the bind completes. 3278 */ 3279 if (mp != NULL) { 3280 tcp_rput_other(tcp, mp); 3281 } else { 3282 /* 3283 * Bind will be resumed later. Need to ensure 3284 * that conn doesn't disappear when that happens. 3285 * This will be decremented in ip_resume_tcp_bind(). 3286 */ 3287 CONN_INC_REF(tcp->tcp_connp); 3288 } 3289 } 3290 3291 3292 /* 3293 * If the "bind_to_req_port_only" parameter is set, if the requested port 3294 * number is available, return it, If not return 0 3295 * 3296 * If "bind_to_req_port_only" parameter is not set and 3297 * If the requested port number is available, return it. If not, return 3298 * the first anonymous port we happen across. If no anonymous ports are 3299 * available, return 0. addr is the requested local address, if any. 3300 * 3301 * In either case, when succeeding update the tcp_t to record the port number 3302 * and insert it in the bind hash table. 3303 * 3304 * Note that TCP over IPv4 and IPv6 sockets can use the same port number 3305 * without setting SO_REUSEADDR. This is needed so that they 3306 * can be viewed as two independent transport protocols. 3307 */ 3308 static in_port_t 3309 tcp_bindi(tcp_t *tcp, in_port_t port, const in6_addr_t *laddr, 3310 int reuseaddr, boolean_t quick_connect, 3311 boolean_t bind_to_req_port_only, boolean_t user_specified) 3312 { 3313 /* number of times we have run around the loop */ 3314 int count = 0; 3315 /* maximum number of times to run around the loop */ 3316 int loopmax; 3317 zoneid_t zoneid = tcp->tcp_connp->conn_zoneid; 3318 3319 /* 3320 * Lookup for free addresses is done in a loop and "loopmax" 3321 * influences how long we spin in the loop 3322 */ 3323 if (bind_to_req_port_only) { 3324 /* 3325 * If the requested port is busy, don't bother to look 3326 * for a new one. Setting loop maximum count to 1 has 3327 * that effect. 3328 */ 3329 loopmax = 1; 3330 } else { 3331 /* 3332 * If the requested port is busy, look for a free one 3333 * in the anonymous port range. 3334 * Set loopmax appropriately so that one does not look 3335 * forever in the case all of the anonymous ports are in use. 3336 */ 3337 if (tcp->tcp_anon_priv_bind) { 3338 /* 3339 * loopmax = 3340 * (IPPORT_RESERVED-1) - tcp_min_anonpriv_port + 1 3341 */ 3342 loopmax = IPPORT_RESERVED - tcp_min_anonpriv_port; 3343 } else { 3344 loopmax = (tcp_largest_anon_port - 3345 tcp_smallest_anon_port + 1); 3346 } 3347 } 3348 do { 3349 uint16_t lport; 3350 tf_t *tbf; 3351 tcp_t *ltcp; 3352 3353 lport = htons(port); 3354 3355 /* 3356 * Ensure that the tcp_t is not currently in the bind hash. 3357 * Hold the lock on the hash bucket to ensure that 3358 * the duplicate check plus the insertion is an atomic 3359 * operation. 3360 * 3361 * This function does an inline lookup on the bind hash list 3362 * Make sure that we access only members of tcp_t 3363 * and that we don't look at tcp_tcp, since we are not 3364 * doing a CONN_INC_REF. 3365 */ 3366 tcp_bind_hash_remove(tcp); 3367 tbf = &tcp_bind_fanout[TCP_BIND_HASH(lport)]; 3368 mutex_enter(&tbf->tf_lock); 3369 for (ltcp = tbf->tf_tcp; ltcp != NULL; 3370 ltcp = ltcp->tcp_bind_hash) { 3371 if (lport != ltcp->tcp_lport || 3372 ltcp->tcp_connp->conn_zoneid != zoneid) { 3373 continue; 3374 } 3375 3376 /* 3377 * If TCP_EXCLBIND is set for either the bound or 3378 * binding endpoint, the semantics of bind 3379 * is changed according to the following. 3380 * 3381 * spec = specified address (v4 or v6) 3382 * unspec = unspecified address (v4 or v6) 3383 * A = specified addresses are different for endpoints 3384 * 3385 * bound bind to allowed 3386 * ------------------------------------- 3387 * unspec unspec no 3388 * unspec spec no 3389 * spec unspec no 3390 * spec spec yes if A 3391 * 3392 * Note: 3393 * 3394 * 1. Because of TLI semantics, an endpoint can go 3395 * back from, say TCP_ESTABLISHED to TCPS_LISTEN or 3396 * TCPS_BOUND, depending on whether it is originally 3397 * a listener or not. That is why we need to check 3398 * for states greater than or equal to TCPS_BOUND 3399 * here. 3400 * 3401 * 2. Ideally, we should only check for state equals 3402 * to TCPS_LISTEN. And the following check should be 3403 * added. 3404 * 3405 * if (ltcp->tcp_state == TCPS_LISTEN || 3406 * !reuseaddr || !ltcp->tcp_reuseaddr) { 3407 * ... 3408 * } 3409 * 3410 * The semantics will be changed to this. If the 3411 * endpoint on the list is in state not equal to 3412 * TCPS_LISTEN and both endpoints have SO_REUSEADDR 3413 * set, let the bind succeed. 3414 * 3415 * But because of (1), we cannot do that now. If 3416 * in future, we can change this going back semantics, 3417 * we can add the above check. 3418 */ 3419 if (ltcp->tcp_exclbind || tcp->tcp_exclbind) { 3420 if (V6_OR_V4_INADDR_ANY( 3421 ltcp->tcp_bound_source_v6) || 3422 V6_OR_V4_INADDR_ANY(*laddr) || 3423 IN6_ARE_ADDR_EQUAL(laddr, 3424 <cp->tcp_bound_source_v6)) { 3425 break; 3426 } 3427 continue; 3428 } 3429 3430 /* 3431 * Check ipversion to allow IPv4 and IPv6 sockets to 3432 * have disjoint port number spaces, if *_EXCLBIND 3433 * is not set and only if the application binds to a 3434 * specific port. We use the same autoassigned port 3435 * number space for IPv4 and IPv6 sockets. 3436 */ 3437 if (tcp->tcp_ipversion != ltcp->tcp_ipversion && 3438 bind_to_req_port_only) 3439 continue; 3440 3441 /* 3442 * Ideally, we should make sure that the source 3443 * address, remote address, and remote port in the 3444 * four tuple for this tcp-connection is unique. 3445 * However, trying to find out the local source 3446 * address would require too much code duplication 3447 * with IP, since IP needs needs to have that code 3448 * to support userland TCP implementations. 3449 */ 3450 if (quick_connect && 3451 (ltcp->tcp_state > TCPS_LISTEN) && 3452 ((tcp->tcp_fport != ltcp->tcp_fport) || 3453 !IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 3454 <cp->tcp_remote_v6))) 3455 continue; 3456 3457 if (!reuseaddr) { 3458 /* 3459 * No socket option SO_REUSEADDR. 3460 * If existing port is bound to 3461 * a non-wildcard IP address 3462 * and the requesting stream is 3463 * bound to a distinct 3464 * different IP addresses 3465 * (non-wildcard, also), keep 3466 * going. 3467 */ 3468 if (!V6_OR_V4_INADDR_ANY(*laddr) && 3469 !V6_OR_V4_INADDR_ANY( 3470 ltcp->tcp_bound_source_v6) && 3471 !IN6_ARE_ADDR_EQUAL(laddr, 3472 <cp->tcp_bound_source_v6)) 3473 continue; 3474 if (ltcp->tcp_state >= TCPS_BOUND) { 3475 /* 3476 * This port is being used and 3477 * its state is >= TCPS_BOUND, 3478 * so we can't bind to it. 3479 */ 3480 break; 3481 } 3482 } else { 3483 /* 3484 * socket option SO_REUSEADDR is set on the 3485 * binding tcp_t. 3486 * 3487 * If two streams are bound to 3488 * same IP address or both addr 3489 * and bound source are wildcards 3490 * (INADDR_ANY), we want to stop 3491 * searching. 3492 * We have found a match of IP source 3493 * address and source port, which is 3494 * refused regardless of the 3495 * SO_REUSEADDR setting, so we break. 3496 */ 3497 if (IN6_ARE_ADDR_EQUAL(laddr, 3498 <cp->tcp_bound_source_v6) && 3499 (ltcp->tcp_state == TCPS_LISTEN || 3500 ltcp->tcp_state == TCPS_BOUND)) 3501 break; 3502 } 3503 } 3504 if (ltcp != NULL) { 3505 /* The port number is busy */ 3506 mutex_exit(&tbf->tf_lock); 3507 } else { 3508 /* 3509 * This port is ours. Insert in fanout and mark as 3510 * bound to prevent others from getting the port 3511 * number. 3512 */ 3513 tcp->tcp_state = TCPS_BOUND; 3514 tcp->tcp_lport = htons(port); 3515 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 3516 3517 ASSERT(&tcp_bind_fanout[TCP_BIND_HASH( 3518 tcp->tcp_lport)] == tbf); 3519 tcp_bind_hash_insert(tbf, tcp, 1); 3520 3521 mutex_exit(&tbf->tf_lock); 3522 3523 /* 3524 * We don't want tcp_next_port_to_try to "inherit" 3525 * a port number supplied by the user in a bind. 3526 */ 3527 if (user_specified) 3528 return (port); 3529 3530 /* 3531 * This is the only place where tcp_next_port_to_try 3532 * is updated. After the update, it may or may not 3533 * be in the valid range. 3534 */ 3535 if (!tcp->tcp_anon_priv_bind) 3536 tcp_next_port_to_try = port + 1; 3537 return (port); 3538 } 3539 3540 if (tcp->tcp_anon_priv_bind) { 3541 port = tcp_get_next_priv_port(); 3542 } else { 3543 if (count == 0 && user_specified) { 3544 /* 3545 * We may have to return an anonymous port. So 3546 * get one to start with. 3547 */ 3548 port = 3549 tcp_update_next_port(tcp_next_port_to_try, 3550 B_TRUE); 3551 user_specified = B_FALSE; 3552 } else { 3553 port = tcp_update_next_port(port + 1, B_FALSE); 3554 } 3555 } 3556 3557 /* 3558 * Don't let this loop run forever in the case where 3559 * all of the anonymous ports are in use. 3560 */ 3561 } while (++count < loopmax); 3562 return (0); 3563 } 3564 3565 /* 3566 * We are dying for some reason. Try to do it gracefully. (May be called 3567 * as writer.) 3568 * 3569 * Return -1 if the structure was not cleaned up (if the cleanup had to be 3570 * done by a service procedure). 3571 * TBD - Should the return value distinguish between the tcp_t being 3572 * freed and it being reinitialized? 3573 */ 3574 static int 3575 tcp_clean_death(tcp_t *tcp, int err, uint8_t tag) 3576 { 3577 mblk_t *mp; 3578 queue_t *q; 3579 3580 TCP_CLD_STAT(tag); 3581 3582 #if TCP_TAG_CLEAN_DEATH 3583 tcp->tcp_cleandeathtag = tag; 3584 #endif 3585 3586 if (tcp->tcp_linger_tid != 0 && 3587 TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) { 3588 tcp_stop_lingering(tcp); 3589 } 3590 3591 ASSERT(tcp != NULL); 3592 ASSERT((tcp->tcp_family == AF_INET && 3593 tcp->tcp_ipversion == IPV4_VERSION) || 3594 (tcp->tcp_family == AF_INET6 && 3595 (tcp->tcp_ipversion == IPV4_VERSION || 3596 tcp->tcp_ipversion == IPV6_VERSION))); 3597 3598 if (TCP_IS_DETACHED(tcp)) { 3599 if (tcp->tcp_hard_binding) { 3600 /* 3601 * Its an eager that we are dealing with. We close the 3602 * eager but in case a conn_ind has already gone to the 3603 * listener, let tcp_accept_finish() send a discon_ind 3604 * to the listener and drop the last reference. If the 3605 * listener doesn't even know about the eager i.e. the 3606 * conn_ind hasn't gone up, blow away the eager and drop 3607 * the last reference as well. If the conn_ind has gone 3608 * up, state should be BOUND. tcp_accept_finish 3609 * will figure out that the connection has received a 3610 * RST and will send a DISCON_IND to the application. 3611 */ 3612 tcp_closei_local(tcp); 3613 if (tcp->tcp_conn.tcp_eager_conn_ind != NULL) { 3614 CONN_DEC_REF(tcp->tcp_connp); 3615 } else { 3616 tcp->tcp_state = TCPS_BOUND; 3617 } 3618 } else { 3619 tcp_close_detached(tcp); 3620 } 3621 return (0); 3622 } 3623 3624 TCP_STAT(tcp_clean_death_nondetached); 3625 3626 /* 3627 * If T_ORDREL_IND has not been sent yet (done when service routine 3628 * is run) postpone cleaning up the endpoint until service routine 3629 * has sent up the T_ORDREL_IND. Avoid clearing out an existing 3630 * client_errno since tcp_close uses the client_errno field. 3631 */ 3632 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 3633 if (err != 0) 3634 tcp->tcp_client_errno = err; 3635 3636 tcp->tcp_deferred_clean_death = B_TRUE; 3637 return (-1); 3638 } 3639 3640 q = tcp->tcp_rq; 3641 3642 /* Trash all inbound data */ 3643 flushq(q, FLUSHALL); 3644 3645 /* 3646 * If we are at least part way open and there is error 3647 * (err==0 implies no error) 3648 * notify our client by a T_DISCON_IND. 3649 */ 3650 if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) { 3651 if (tcp->tcp_state >= TCPS_ESTABLISHED && 3652 !TCP_IS_SOCKET(tcp)) { 3653 /* 3654 * Send M_FLUSH according to TPI. Because sockets will 3655 * (and must) ignore FLUSHR we do that only for TPI 3656 * endpoints and sockets in STREAMS mode. 3657 */ 3658 (void) putnextctl1(q, M_FLUSH, FLUSHR); 3659 } 3660 if (tcp->tcp_debug) { 3661 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 3662 "tcp_clean_death: discon err %d", err); 3663 } 3664 mp = mi_tpi_discon_ind(NULL, err, 0); 3665 if (mp != NULL) { 3666 putnext(q, mp); 3667 } else { 3668 if (tcp->tcp_debug) { 3669 (void) strlog(TCP_MOD_ID, 0, 1, 3670 SL_ERROR|SL_TRACE, 3671 "tcp_clean_death, sending M_ERROR"); 3672 } 3673 (void) putnextctl1(q, M_ERROR, EPROTO); 3674 } 3675 if (tcp->tcp_state <= TCPS_SYN_RCVD) { 3676 /* SYN_SENT or SYN_RCVD */ 3677 BUMP_MIB(&tcp_mib, tcpAttemptFails); 3678 } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) { 3679 /* ESTABLISHED or CLOSE_WAIT */ 3680 BUMP_MIB(&tcp_mib, tcpEstabResets); 3681 } 3682 } 3683 3684 tcp_reinit(tcp); 3685 return (-1); 3686 } 3687 3688 /* 3689 * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout 3690 * to expire, stop the wait and finish the close. 3691 */ 3692 static void 3693 tcp_stop_lingering(tcp_t *tcp) 3694 { 3695 clock_t delta = 0; 3696 3697 tcp->tcp_linger_tid = 0; 3698 if (tcp->tcp_state > TCPS_LISTEN) { 3699 tcp_acceptor_hash_remove(tcp); 3700 if (tcp->tcp_flow_stopped) { 3701 tcp_clrqfull(tcp); 3702 } 3703 3704 if (tcp->tcp_timer_tid != 0) { 3705 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 3706 tcp->tcp_timer_tid = 0; 3707 } 3708 /* 3709 * Need to cancel those timers which will not be used when 3710 * TCP is detached. This has to be done before the tcp_wq 3711 * is set to the global queue. 3712 */ 3713 tcp_timers_stop(tcp); 3714 3715 3716 tcp->tcp_detached = B_TRUE; 3717 tcp->tcp_rq = tcp_g_q; 3718 tcp->tcp_wq = WR(tcp_g_q); 3719 3720 if (tcp->tcp_state == TCPS_TIME_WAIT) { 3721 tcp_time_wait_append(tcp); 3722 TCP_DBGSTAT(tcp_detach_time_wait); 3723 goto finish; 3724 } 3725 3726 /* 3727 * If delta is zero the timer event wasn't executed and was 3728 * successfully canceled. In this case we need to restart it 3729 * with the minimal delta possible. 3730 */ 3731 if (delta >= 0) { 3732 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 3733 delta ? delta : 1); 3734 } 3735 } else { 3736 tcp_closei_local(tcp); 3737 CONN_DEC_REF(tcp->tcp_connp); 3738 } 3739 finish: 3740 /* Signal closing thread that it can complete close */ 3741 mutex_enter(&tcp->tcp_closelock); 3742 tcp->tcp_detached = B_TRUE; 3743 tcp->tcp_rq = tcp_g_q; 3744 tcp->tcp_wq = WR(tcp_g_q); 3745 tcp->tcp_closed = 1; 3746 cv_signal(&tcp->tcp_closecv); 3747 mutex_exit(&tcp->tcp_closelock); 3748 } 3749 3750 /* 3751 * Handle lingering timeouts. This function is called when the SO_LINGER timeout 3752 * expires. 3753 */ 3754 static void 3755 tcp_close_linger_timeout(void *arg) 3756 { 3757 conn_t *connp = (conn_t *)arg; 3758 tcp_t *tcp = connp->conn_tcp; 3759 3760 tcp->tcp_client_errno = ETIMEDOUT; 3761 tcp_stop_lingering(tcp); 3762 } 3763 3764 static int 3765 tcp_close(queue_t *q, int flags) 3766 { 3767 conn_t *connp = Q_TO_CONN(q); 3768 tcp_t *tcp = connp->conn_tcp; 3769 mblk_t *mp = &tcp->tcp_closemp; 3770 boolean_t conn_ioctl_cleanup_reqd = B_FALSE; 3771 3772 ASSERT(WR(q)->q_next == NULL); 3773 ASSERT(connp->conn_ref >= 2); 3774 ASSERT((connp->conn_flags & IPCL_TCPMOD) == 0); 3775 3776 /* 3777 * We are being closed as /dev/tcp or /dev/tcp6. 3778 * 3779 * Mark the conn as closing. ill_pending_mp_add will not 3780 * add any mp to the pending mp list, after this conn has 3781 * started closing. Same for sq_pending_mp_add 3782 */ 3783 mutex_enter(&connp->conn_lock); 3784 connp->conn_state_flags |= CONN_CLOSING; 3785 if (connp->conn_oper_pending_ill != NULL) 3786 conn_ioctl_cleanup_reqd = B_TRUE; 3787 CONN_INC_REF_LOCKED(connp); 3788 mutex_exit(&connp->conn_lock); 3789 tcp->tcp_closeflags = (uint8_t)flags; 3790 ASSERT(connp->conn_ref >= 3); 3791 3792 (*tcp_squeue_close_proc)(connp->conn_sqp, mp, 3793 tcp_close_output, connp, SQTAG_IP_TCP_CLOSE); 3794 3795 mutex_enter(&tcp->tcp_closelock); 3796 3797 while (!tcp->tcp_closed) 3798 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock); 3799 mutex_exit(&tcp->tcp_closelock); 3800 /* 3801 * In the case of listener streams that have eagers in the q or q0 3802 * we wait for the eagers to drop their reference to us. tcp_rq and 3803 * tcp_wq of the eagers point to our queues. By waiting for the 3804 * refcnt to drop to 1, we are sure that the eagers have cleaned 3805 * up their queue pointers and also dropped their references to us. 3806 */ 3807 if (tcp->tcp_wait_for_eagers) { 3808 mutex_enter(&connp->conn_lock); 3809 while (connp->conn_ref != 1) { 3810 cv_wait(&connp->conn_cv, &connp->conn_lock); 3811 } 3812 mutex_exit(&connp->conn_lock); 3813 } 3814 /* 3815 * ioctl cleanup. The mp is queued in the 3816 * ill_pending_mp or in the sq_pending_mp. 3817 */ 3818 if (conn_ioctl_cleanup_reqd) 3819 conn_ioctl_cleanup(connp); 3820 3821 qprocsoff(q); 3822 inet_minor_free(ip_minor_arena, connp->conn_dev); 3823 3824 ASSERT(connp->conn_cred != NULL); 3825 crfree(connp->conn_cred); 3826 tcp->tcp_cred = connp->conn_cred = NULL; 3827 tcp->tcp_cpid = -1; 3828 3829 /* 3830 * Drop IP's reference on the conn. This is the last reference 3831 * on the connp if the state was less than established. If the 3832 * connection has gone into timewait state, then we will have 3833 * one ref for the TCP and one more ref (total of two) for the 3834 * classifier connected hash list (a timewait connections stays 3835 * in connected hash till closed). 3836 * 3837 * We can't assert the references because there might be other 3838 * transient reference places because of some walkers or queued 3839 * packets in squeue for the timewait state. 3840 */ 3841 CONN_DEC_REF(connp); 3842 q->q_ptr = WR(q)->q_ptr = NULL; 3843 return (0); 3844 } 3845 3846 static int 3847 tcpclose_accept(queue_t *q) 3848 { 3849 ASSERT(WR(q)->q_qinfo == &tcp_acceptor_winit); 3850 3851 /* 3852 * We had opened an acceptor STREAM for sockfs which is 3853 * now being closed due to some error. 3854 */ 3855 qprocsoff(q); 3856 inet_minor_free(ip_minor_arena, (dev_t)q->q_ptr); 3857 q->q_ptr = WR(q)->q_ptr = NULL; 3858 return (0); 3859 } 3860 3861 3862 /* 3863 * Called by streams close routine via squeues when our client blows off her 3864 * descriptor, we take this to mean: "close the stream state NOW, close the tcp 3865 * connection politely" When SO_LINGER is set (with a non-zero linger time and 3866 * it is not a nonblocking socket) then this routine sleeps until the FIN is 3867 * acked. 3868 * 3869 * NOTE: tcp_close potentially returns error when lingering. 3870 * However, the stream head currently does not pass these errors 3871 * to the application. 4.4BSD only returns EINTR and EWOULDBLOCK 3872 * errors to the application (from tsleep()) and not errors 3873 * like ECONNRESET caused by receiving a reset packet. 3874 */ 3875 3876 /* ARGSUSED */ 3877 static void 3878 tcp_close_output(void *arg, mblk_t *mp, void *arg2) 3879 { 3880 char *msg; 3881 conn_t *connp = (conn_t *)arg; 3882 tcp_t *tcp = connp->conn_tcp; 3883 clock_t delta = 0; 3884 3885 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 3886 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 3887 3888 /* Cancel any pending timeout */ 3889 if (tcp->tcp_ordrelid != 0) { 3890 if (tcp->tcp_timeout) { 3891 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ordrelid); 3892 } 3893 tcp->tcp_ordrelid = 0; 3894 tcp->tcp_timeout = B_FALSE; 3895 } 3896 3897 mutex_enter(&tcp->tcp_eager_lock); 3898 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 3899 /* Cleanup for listener */ 3900 tcp_eager_cleanup(tcp, 0); 3901 tcp->tcp_wait_for_eagers = 1; 3902 } 3903 mutex_exit(&tcp->tcp_eager_lock); 3904 3905 connp->conn_mdt_ok = B_FALSE; 3906 tcp->tcp_mdt = B_FALSE; 3907 3908 msg = NULL; 3909 switch (tcp->tcp_state) { 3910 case TCPS_CLOSED: 3911 case TCPS_IDLE: 3912 case TCPS_BOUND: 3913 case TCPS_LISTEN: 3914 break; 3915 case TCPS_SYN_SENT: 3916 msg = "tcp_close, during connect"; 3917 break; 3918 case TCPS_SYN_RCVD: 3919 /* 3920 * Close during the connect 3-way handshake 3921 * but here there may or may not be pending data 3922 * already on queue. Process almost same as in 3923 * the ESTABLISHED state. 3924 */ 3925 /* FALLTHRU */ 3926 default: 3927 if (tcp->tcp_fused) 3928 tcp_unfuse(tcp); 3929 3930 /* 3931 * If SO_LINGER has set a zero linger time, abort the 3932 * connection with a reset. 3933 */ 3934 if (tcp->tcp_linger && tcp->tcp_lingertime == 0) { 3935 msg = "tcp_close, zero lingertime"; 3936 break; 3937 } 3938 3939 ASSERT(tcp->tcp_hard_bound || tcp->tcp_hard_binding); 3940 /* 3941 * Abort connection if there is unread data queued. 3942 */ 3943 if (tcp->tcp_rcv_list || tcp->tcp_reass_head) { 3944 msg = "tcp_close, unread data"; 3945 break; 3946 } 3947 /* 3948 * tcp_hard_bound is now cleared thus all packets go through 3949 * tcp_lookup. This fact is used by tcp_detach below. 3950 * 3951 * We have done a qwait() above which could have possibly 3952 * drained more messages in turn causing transition to a 3953 * different state. Check whether we have to do the rest 3954 * of the processing or not. 3955 */ 3956 if (tcp->tcp_state <= TCPS_LISTEN) 3957 break; 3958 3959 /* 3960 * Transmit the FIN before detaching the tcp_t. 3961 * After tcp_detach returns this queue/perimeter 3962 * no longer owns the tcp_t thus others can modify it. 3963 */ 3964 (void) tcp_xmit_end(tcp); 3965 3966 /* 3967 * If lingering on close then wait until the fin is acked, 3968 * the SO_LINGER time passes, or a reset is sent/received. 3969 */ 3970 if (tcp->tcp_linger && tcp->tcp_lingertime > 0 && 3971 !(tcp->tcp_fin_acked) && 3972 tcp->tcp_state >= TCPS_ESTABLISHED) { 3973 if (tcp->tcp_closeflags & (FNDELAY|FNONBLOCK)) { 3974 tcp->tcp_client_errno = EWOULDBLOCK; 3975 } else if (tcp->tcp_client_errno == 0) { 3976 3977 ASSERT(tcp->tcp_linger_tid == 0); 3978 3979 tcp->tcp_linger_tid = TCP_TIMER(tcp, 3980 tcp_close_linger_timeout, 3981 tcp->tcp_lingertime * hz); 3982 3983 /* tcp_close_linger_timeout will finish close */ 3984 if (tcp->tcp_linger_tid == 0) 3985 tcp->tcp_client_errno = ENOSR; 3986 else 3987 return; 3988 } 3989 3990 /* 3991 * Check if we need to detach or just close 3992 * the instance. 3993 */ 3994 if (tcp->tcp_state <= TCPS_LISTEN) 3995 break; 3996 } 3997 3998 /* 3999 * Make sure that no other thread will access the tcp_rq of 4000 * this instance (through lookups etc.) as tcp_rq will go 4001 * away shortly. 4002 */ 4003 tcp_acceptor_hash_remove(tcp); 4004 4005 if (tcp->tcp_flow_stopped) { 4006 tcp_clrqfull(tcp); 4007 } 4008 4009 if (tcp->tcp_timer_tid != 0) { 4010 delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4011 tcp->tcp_timer_tid = 0; 4012 } 4013 /* 4014 * Need to cancel those timers which will not be used when 4015 * TCP is detached. This has to be done before the tcp_wq 4016 * is set to the global queue. 4017 */ 4018 tcp_timers_stop(tcp); 4019 4020 tcp->tcp_detached = B_TRUE; 4021 if (tcp->tcp_state == TCPS_TIME_WAIT) { 4022 tcp_time_wait_append(tcp); 4023 TCP_DBGSTAT(tcp_detach_time_wait); 4024 ASSERT(connp->conn_ref >= 3); 4025 goto finish; 4026 } 4027 4028 /* 4029 * If delta is zero the timer event wasn't executed and was 4030 * successfully canceled. In this case we need to restart it 4031 * with the minimal delta possible. 4032 */ 4033 if (delta >= 0) 4034 tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer, 4035 delta ? delta : 1); 4036 4037 ASSERT(connp->conn_ref >= 3); 4038 goto finish; 4039 } 4040 4041 /* Detach did not complete. Still need to remove q from stream. */ 4042 if (msg) { 4043 if (tcp->tcp_state == TCPS_ESTABLISHED || 4044 tcp->tcp_state == TCPS_CLOSE_WAIT) 4045 BUMP_MIB(&tcp_mib, tcpEstabResets); 4046 if (tcp->tcp_state == TCPS_SYN_SENT || 4047 tcp->tcp_state == TCPS_SYN_RCVD) 4048 BUMP_MIB(&tcp_mib, tcpAttemptFails); 4049 tcp_xmit_ctl(msg, tcp, tcp->tcp_snxt, 0, TH_RST); 4050 } 4051 4052 tcp_closei_local(tcp); 4053 CONN_DEC_REF(connp); 4054 ASSERT(connp->conn_ref >= 2); 4055 4056 finish: 4057 /* 4058 * Although packets are always processed on the correct 4059 * tcp's perimeter and access is serialized via squeue's, 4060 * IP still needs a queue when sending packets in time_wait 4061 * state so use WR(tcp_g_q) till ip_output() can be 4062 * changed to deal with just connp. For read side, we 4063 * could have set tcp_rq to NULL but there are some cases 4064 * in tcp_rput_data() from early days of this code which 4065 * do a putnext without checking if tcp is closed. Those 4066 * need to be identified before both tcp_rq and tcp_wq 4067 * can be set to NULL and tcp_q_q can disappear forever. 4068 */ 4069 mutex_enter(&tcp->tcp_closelock); 4070 /* 4071 * Don't change the queues in the case of a listener that has 4072 * eagers in its q or q0. It could surprise the eagers. 4073 * Instead wait for the eagers outside the squeue. 4074 */ 4075 if (!tcp->tcp_wait_for_eagers) { 4076 tcp->tcp_detached = B_TRUE; 4077 tcp->tcp_rq = tcp_g_q; 4078 tcp->tcp_wq = WR(tcp_g_q); 4079 } 4080 4081 /* Signal tcp_close() to finish closing. */ 4082 tcp->tcp_closed = 1; 4083 cv_signal(&tcp->tcp_closecv); 4084 mutex_exit(&tcp->tcp_closelock); 4085 } 4086 4087 4088 /* 4089 * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. 4090 * Some stream heads get upset if they see these later on as anything but NULL. 4091 */ 4092 static void 4093 tcp_close_mpp(mblk_t **mpp) 4094 { 4095 mblk_t *mp; 4096 4097 if ((mp = *mpp) != NULL) { 4098 do { 4099 mp->b_next = NULL; 4100 mp->b_prev = NULL; 4101 } while ((mp = mp->b_cont) != NULL); 4102 4103 mp = *mpp; 4104 *mpp = NULL; 4105 freemsg(mp); 4106 } 4107 } 4108 4109 /* Do detached close. */ 4110 static void 4111 tcp_close_detached(tcp_t *tcp) 4112 { 4113 if (tcp->tcp_fused) 4114 tcp_unfuse(tcp); 4115 4116 /* 4117 * Clustering code serializes TCP disconnect callbacks and 4118 * cluster tcp list walks by blocking a TCP disconnect callback 4119 * if a cluster tcp list walk is in progress. This ensures 4120 * accurate accounting of TCPs in the cluster code even though 4121 * the TCP list walk itself is not atomic. 4122 */ 4123 tcp_closei_local(tcp); 4124 CONN_DEC_REF(tcp->tcp_connp); 4125 } 4126 4127 /* 4128 * Stop all TCP timers, and free the timer mblks if requested. 4129 */ 4130 void 4131 tcp_timers_stop(tcp_t *tcp) 4132 { 4133 if (tcp->tcp_timer_tid != 0) { 4134 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid); 4135 tcp->tcp_timer_tid = 0; 4136 } 4137 if (tcp->tcp_ka_tid != 0) { 4138 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ka_tid); 4139 tcp->tcp_ka_tid = 0; 4140 } 4141 if (tcp->tcp_ack_tid != 0) { 4142 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 4143 tcp->tcp_ack_tid = 0; 4144 } 4145 if (tcp->tcp_push_tid != 0) { 4146 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 4147 tcp->tcp_push_tid = 0; 4148 } 4149 } 4150 4151 /* 4152 * The tcp_t is going away. Remove it from all lists and set it 4153 * to TCPS_CLOSED. The freeing up of memory is deferred until 4154 * tcp_inactive. This is needed since a thread in tcp_rput might have 4155 * done a CONN_INC_REF on this structure before it was removed from the 4156 * hashes. 4157 */ 4158 static void 4159 tcp_closei_local(tcp_t *tcp) 4160 { 4161 ire_t *ire; 4162 conn_t *connp = tcp->tcp_connp; 4163 4164 if (!TCP_IS_SOCKET(tcp)) 4165 tcp_acceptor_hash_remove(tcp); 4166 4167 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 4168 tcp->tcp_ibsegs = 0; 4169 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 4170 tcp->tcp_obsegs = 0; 4171 4172 /* 4173 * If we are an eager connection hanging off a listener that 4174 * hasn't formally accepted the connection yet, get off his 4175 * list and blow off any data that we have accumulated. 4176 */ 4177 if (tcp->tcp_listener != NULL) { 4178 tcp_t *listener = tcp->tcp_listener; 4179 mutex_enter(&listener->tcp_eager_lock); 4180 /* 4181 * tcp_eager_conn_ind == NULL means that the 4182 * conn_ind has already gone to listener. At 4183 * this point, eager will be closed but we 4184 * leave it in listeners eager list so that 4185 * if listener decides to close without doing 4186 * accept, we can clean this up. In tcp_wput_accept 4187 * we take case of the case of accept on closed 4188 * eager. 4189 */ 4190 if (tcp->tcp_conn.tcp_eager_conn_ind != NULL) { 4191 tcp_eager_unlink(tcp); 4192 mutex_exit(&listener->tcp_eager_lock); 4193 /* 4194 * We don't want to have any pointers to the 4195 * listener queue, after we have released our 4196 * reference on the listener 4197 */ 4198 tcp->tcp_rq = tcp_g_q; 4199 tcp->tcp_wq = WR(tcp_g_q); 4200 CONN_DEC_REF(listener->tcp_connp); 4201 } else { 4202 mutex_exit(&listener->tcp_eager_lock); 4203 } 4204 } 4205 4206 /* Stop all the timers */ 4207 tcp_timers_stop(tcp); 4208 4209 if (tcp->tcp_state == TCPS_LISTEN) { 4210 if (tcp->tcp_ip_addr_cache) { 4211 kmem_free((void *)tcp->tcp_ip_addr_cache, 4212 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 4213 tcp->tcp_ip_addr_cache = NULL; 4214 } 4215 } 4216 if (tcp->tcp_flow_stopped) 4217 tcp_clrqfull(tcp); 4218 4219 tcp_bind_hash_remove(tcp); 4220 /* 4221 * If the tcp_time_wait_collector (which runs outside the squeue) 4222 * is trying to remove this tcp from the time wait list, we will 4223 * block in tcp_time_wait_remove while trying to acquire the 4224 * tcp_time_wait_lock. The logic in tcp_time_wait_collector also 4225 * requires the ipcl_hash_remove to be ordered after the 4226 * tcp_time_wait_remove for the refcnt checks to work correctly. 4227 */ 4228 if (tcp->tcp_state == TCPS_TIME_WAIT) 4229 tcp_time_wait_remove(tcp, NULL); 4230 CL_INET_DISCONNECT(tcp); 4231 ipcl_hash_remove(connp); 4232 4233 /* 4234 * Delete the cached ire in conn_ire_cache and also mark 4235 * the conn as CONDEMNED 4236 */ 4237 mutex_enter(&connp->conn_lock); 4238 connp->conn_state_flags |= CONN_CONDEMNED; 4239 ire = connp->conn_ire_cache; 4240 connp->conn_ire_cache = NULL; 4241 mutex_exit(&connp->conn_lock); 4242 if (ire != NULL) 4243 IRE_REFRELE_NOTR(ire); 4244 4245 /* Need to cleanup any pending ioctls */ 4246 ASSERT(tcp->tcp_time_wait_next == NULL); 4247 ASSERT(tcp->tcp_time_wait_prev == NULL); 4248 ASSERT(tcp->tcp_time_wait_expire == 0); 4249 tcp->tcp_state = TCPS_CLOSED; 4250 4251 /* Release any SSL context */ 4252 if (tcp->tcp_kssl_ent != NULL) { 4253 kssl_release_ent(tcp->tcp_kssl_ent, NULL, KSSL_NO_PROXY); 4254 tcp->tcp_kssl_ent = NULL; 4255 } 4256 if (tcp->tcp_kssl_ctx != NULL) { 4257 kssl_release_ctx(tcp->tcp_kssl_ctx); 4258 tcp->tcp_kssl_ctx = NULL; 4259 } 4260 tcp->tcp_kssl_pending = B_FALSE; 4261 } 4262 4263 /* 4264 * tcp is dying (called from ipcl_conn_destroy and error cases). 4265 * Free the tcp_t in either case. 4266 */ 4267 void 4268 tcp_free(tcp_t *tcp) 4269 { 4270 mblk_t *mp; 4271 ip6_pkt_t *ipp; 4272 4273 ASSERT(tcp != NULL); 4274 ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL); 4275 4276 tcp->tcp_rq = NULL; 4277 tcp->tcp_wq = NULL; 4278 4279 tcp_close_mpp(&tcp->tcp_xmit_head); 4280 tcp_close_mpp(&tcp->tcp_reass_head); 4281 if (tcp->tcp_rcv_list != NULL) { 4282 /* Free b_next chain */ 4283 tcp_close_mpp(&tcp->tcp_rcv_list); 4284 } 4285 if ((mp = tcp->tcp_urp_mp) != NULL) { 4286 freemsg(mp); 4287 } 4288 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 4289 freemsg(mp); 4290 } 4291 4292 if (tcp->tcp_fused_sigurg_mp != NULL) { 4293 freeb(tcp->tcp_fused_sigurg_mp); 4294 tcp->tcp_fused_sigurg_mp = NULL; 4295 } 4296 4297 if (tcp->tcp_sack_info != NULL) { 4298 if (tcp->tcp_notsack_list != NULL) { 4299 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 4300 } 4301 bzero(tcp->tcp_sack_info, sizeof (tcp_sack_info_t)); 4302 } 4303 4304 if (tcp->tcp_hopopts != NULL) { 4305 mi_free(tcp->tcp_hopopts); 4306 tcp->tcp_hopopts = NULL; 4307 tcp->tcp_hopoptslen = 0; 4308 } 4309 ASSERT(tcp->tcp_hopoptslen == 0); 4310 if (tcp->tcp_dstopts != NULL) { 4311 mi_free(tcp->tcp_dstopts); 4312 tcp->tcp_dstopts = NULL; 4313 tcp->tcp_dstoptslen = 0; 4314 } 4315 ASSERT(tcp->tcp_dstoptslen == 0); 4316 if (tcp->tcp_rtdstopts != NULL) { 4317 mi_free(tcp->tcp_rtdstopts); 4318 tcp->tcp_rtdstopts = NULL; 4319 tcp->tcp_rtdstoptslen = 0; 4320 } 4321 ASSERT(tcp->tcp_rtdstoptslen == 0); 4322 if (tcp->tcp_rthdr != NULL) { 4323 mi_free(tcp->tcp_rthdr); 4324 tcp->tcp_rthdr = NULL; 4325 tcp->tcp_rthdrlen = 0; 4326 } 4327 ASSERT(tcp->tcp_rthdrlen == 0); 4328 4329 ipp = &tcp->tcp_sticky_ipp; 4330 if ((ipp->ipp_fields & (IPPF_HOPOPTS | IPPF_RTDSTOPTS | 4331 IPPF_DSTOPTS | IPPF_RTHDR)) != 0) { 4332 if ((ipp->ipp_fields & IPPF_HOPOPTS) != 0) { 4333 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen); 4334 ipp->ipp_hopopts = NULL; 4335 ipp->ipp_hopoptslen = 0; 4336 } 4337 if ((ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) { 4338 kmem_free(ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 4339 ipp->ipp_rtdstopts = NULL; 4340 ipp->ipp_rtdstoptslen = 0; 4341 } 4342 if ((ipp->ipp_fields & IPPF_DSTOPTS) != 0) { 4343 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen); 4344 ipp->ipp_dstopts = NULL; 4345 ipp->ipp_dstoptslen = 0; 4346 } 4347 if ((ipp->ipp_fields & IPPF_RTHDR) != 0) { 4348 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen); 4349 ipp->ipp_rthdr = NULL; 4350 ipp->ipp_rthdrlen = 0; 4351 } 4352 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTDSTOPTS | 4353 IPPF_DSTOPTS | IPPF_RTHDR); 4354 } 4355 4356 /* 4357 * Free memory associated with the tcp/ip header template. 4358 */ 4359 4360 if (tcp->tcp_iphc != NULL) 4361 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4362 4363 /* 4364 * Following is really a blowing away a union. 4365 * It happens to have exactly two members of identical size 4366 * the following code is enough. 4367 */ 4368 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 4369 4370 if (tcp->tcp_tracebuf != NULL) { 4371 kmem_free(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 4372 tcp->tcp_tracebuf = NULL; 4373 } 4374 } 4375 4376 4377 /* 4378 * Put a connection confirmation message upstream built from the 4379 * address information within 'iph' and 'tcph'. Report our success or failure. 4380 */ 4381 static boolean_t 4382 tcp_conn_con(tcp_t *tcp, uchar_t *iphdr, tcph_t *tcph, mblk_t *idmp, 4383 mblk_t **defermp) 4384 { 4385 sin_t sin; 4386 sin6_t sin6; 4387 mblk_t *mp; 4388 char *optp = NULL; 4389 int optlen = 0; 4390 cred_t *cr; 4391 4392 if (defermp != NULL) 4393 *defermp = NULL; 4394 4395 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) { 4396 /* 4397 * Return in T_CONN_CON results of option negotiation through 4398 * the T_CONN_REQ. Note: If there is an real end-to-end option 4399 * negotiation, then what is received from remote end needs 4400 * to be taken into account but there is no such thing (yet?) 4401 * in our TCP/IP. 4402 * Note: We do not use mi_offset_param() here as 4403 * tcp_opts_conn_req contents do not directly come from 4404 * an application and are either generated in kernel or 4405 * from user input that was already verified. 4406 */ 4407 mp = tcp->tcp_conn.tcp_opts_conn_req; 4408 optp = (char *)(mp->b_rptr + 4409 ((struct T_conn_req *)mp->b_rptr)->OPT_offset); 4410 optlen = (int) 4411 ((struct T_conn_req *)mp->b_rptr)->OPT_length; 4412 } 4413 4414 if (IPH_HDR_VERSION(iphdr) == IPV4_VERSION) { 4415 ipha_t *ipha = (ipha_t *)iphdr; 4416 4417 /* packet is IPv4 */ 4418 if (tcp->tcp_family == AF_INET) { 4419 sin = sin_null; 4420 sin.sin_addr.s_addr = ipha->ipha_src; 4421 sin.sin_port = *(uint16_t *)tcph->th_lport; 4422 sin.sin_family = AF_INET; 4423 mp = mi_tpi_conn_con(NULL, (char *)&sin, 4424 (int)sizeof (sin_t), optp, optlen); 4425 } else { 4426 sin6 = sin6_null; 4427 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4428 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4429 sin6.sin6_family = AF_INET6; 4430 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4431 (int)sizeof (sin6_t), optp, optlen); 4432 4433 } 4434 } else { 4435 ip6_t *ip6h = (ip6_t *)iphdr; 4436 4437 ASSERT(IPH_HDR_VERSION(iphdr) == IPV6_VERSION); 4438 ASSERT(tcp->tcp_family == AF_INET6); 4439 sin6 = sin6_null; 4440 sin6.sin6_addr = ip6h->ip6_src; 4441 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4442 sin6.sin6_family = AF_INET6; 4443 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4444 mp = mi_tpi_conn_con(NULL, (char *)&sin6, 4445 (int)sizeof (sin6_t), optp, optlen); 4446 } 4447 4448 if (!mp) 4449 return (B_FALSE); 4450 4451 if ((cr = DB_CRED(idmp)) != NULL) { 4452 mblk_setcred(mp, cr); 4453 DB_CPID(mp) = DB_CPID(idmp); 4454 } 4455 4456 if (defermp == NULL) 4457 putnext(tcp->tcp_rq, mp); 4458 else 4459 *defermp = mp; 4460 4461 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 4462 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 4463 return (B_TRUE); 4464 } 4465 4466 /* 4467 * Defense for the SYN attack - 4468 * 1. When q0 is full, drop from the tail (tcp_eager_prev_q0) the oldest 4469 * one that doesn't have the dontdrop bit set. 4470 * 2. Don't drop a SYN request before its first timeout. This gives every 4471 * request at least til the first timeout to complete its 3-way handshake. 4472 * 3. Maintain tcp_syn_rcvd_timeout as an accurate count of how many 4473 * requests currently on the queue that has timed out. This will be used 4474 * as an indicator of whether an attack is under way, so that appropriate 4475 * actions can be taken. (It's incremented in tcp_timer() and decremented 4476 * either when eager goes into ESTABLISHED, or gets freed up.) 4477 * 4. The current threshold is - # of timeout > q0len/4 => SYN alert on 4478 * # of timeout drops back to <= q0len/32 => SYN alert off 4479 */ 4480 static boolean_t 4481 tcp_drop_q0(tcp_t *tcp) 4482 { 4483 tcp_t *eager; 4484 4485 ASSERT(MUTEX_HELD(&tcp->tcp_eager_lock)); 4486 ASSERT(tcp->tcp_eager_next_q0 != tcp->tcp_eager_prev_q0); 4487 /* 4488 * New one is added after next_q0 so prev_q0 points to the oldest 4489 * Also do not drop any established connections that are deferred on 4490 * q0 due to q being full 4491 */ 4492 4493 eager = tcp->tcp_eager_prev_q0; 4494 while (eager->tcp_dontdrop || eager->tcp_conn_def_q0) { 4495 eager = eager->tcp_eager_prev_q0; 4496 if (eager == tcp) { 4497 eager = tcp->tcp_eager_prev_q0; 4498 break; 4499 } 4500 } 4501 if (eager->tcp_syn_rcvd_timeout == 0) 4502 return (B_FALSE); 4503 4504 if (tcp->tcp_debug) { 4505 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 4506 "tcp_drop_q0: listen half-open queue (max=%d) overflow" 4507 " (%d pending) on %s, drop one", tcp_conn_req_max_q0, 4508 tcp->tcp_conn_req_cnt_q0, 4509 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 4510 } 4511 4512 BUMP_MIB(&tcp_mib, tcpHalfOpenDrop); 4513 4514 /* 4515 * need to do refhold here because the selected eager could 4516 * be removed by someone else if we release the eager lock. 4517 */ 4518 CONN_INC_REF(eager->tcp_connp); 4519 mutex_exit(&tcp->tcp_eager_lock); 4520 4521 /* Mark the IRE created for this SYN request temporary */ 4522 tcp_ip_ire_mark_advice(eager); 4523 (void) tcp_clean_death(eager, ETIMEDOUT, 5); 4524 CONN_DEC_REF(eager->tcp_connp); 4525 4526 mutex_enter(&tcp->tcp_eager_lock); 4527 return (B_TRUE); 4528 } 4529 4530 int 4531 tcp_conn_create_v6(conn_t *lconnp, conn_t *connp, mblk_t *mp, 4532 tcph_t *tcph, uint_t ipvers, mblk_t *idmp) 4533 { 4534 tcp_t *ltcp = lconnp->conn_tcp; 4535 tcp_t *tcp = connp->conn_tcp; 4536 mblk_t *tpi_mp; 4537 ipha_t *ipha; 4538 ip6_t *ip6h; 4539 sin6_t sin6; 4540 in6_addr_t v6dst; 4541 int err; 4542 int ifindex = 0; 4543 cred_t *cr; 4544 4545 if (ipvers == IPV4_VERSION) { 4546 ipha = (ipha_t *)mp->b_rptr; 4547 4548 connp->conn_send = ip_output; 4549 connp->conn_recv = tcp_input; 4550 4551 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4552 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4553 4554 sin6 = sin6_null; 4555 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &sin6.sin6_addr); 4556 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst); 4557 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4558 sin6.sin6_family = AF_INET6; 4559 sin6.__sin6_src_id = ip_srcid_find_addr(&v6dst, 4560 lconnp->conn_zoneid); 4561 if (tcp->tcp_recvdstaddr) { 4562 sin6_t sin6d; 4563 4564 sin6d = sin6_null; 4565 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, 4566 &sin6d.sin6_addr); 4567 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4568 sin6d.sin6_family = AF_INET; 4569 tpi_mp = mi_tpi_extconn_ind(NULL, 4570 (char *)&sin6d, sizeof (sin6_t), 4571 (char *)&tcp, 4572 (t_scalar_t)sizeof (intptr_t), 4573 (char *)&sin6d, sizeof (sin6_t), 4574 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4575 } else { 4576 tpi_mp = mi_tpi_conn_ind(NULL, 4577 (char *)&sin6, sizeof (sin6_t), 4578 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4579 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4580 } 4581 } else { 4582 ip6h = (ip6_t *)mp->b_rptr; 4583 4584 connp->conn_send = ip_output_v6; 4585 connp->conn_recv = tcp_input; 4586 4587 connp->conn_srcv6 = ip6h->ip6_dst; 4588 connp->conn_remv6 = ip6h->ip6_src; 4589 4590 /* db_cksumstuff is set at ip_fanout_tcp_v6 */ 4591 ifindex = (int)DB_CKSUMSTUFF(mp); 4592 DB_CKSUMSTUFF(mp) = 0; 4593 4594 sin6 = sin6_null; 4595 sin6.sin6_addr = ip6h->ip6_src; 4596 sin6.sin6_port = *(uint16_t *)tcph->th_lport; 4597 sin6.sin6_family = AF_INET6; 4598 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK; 4599 sin6.__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst, 4600 lconnp->conn_zoneid); 4601 4602 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4603 /* Pass up the scope_id of remote addr */ 4604 sin6.sin6_scope_id = ifindex; 4605 } else { 4606 sin6.sin6_scope_id = 0; 4607 } 4608 if (tcp->tcp_recvdstaddr) { 4609 sin6_t sin6d; 4610 4611 sin6d = sin6_null; 4612 sin6.sin6_addr = ip6h->ip6_dst; 4613 sin6d.sin6_port = *(uint16_t *)tcph->th_fport; 4614 sin6d.sin6_family = AF_INET; 4615 tpi_mp = mi_tpi_extconn_ind(NULL, 4616 (char *)&sin6d, sizeof (sin6_t), 4617 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4618 (char *)&sin6d, sizeof (sin6_t), 4619 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4620 } else { 4621 tpi_mp = mi_tpi_conn_ind(NULL, 4622 (char *)&sin6, sizeof (sin6_t), 4623 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4624 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4625 } 4626 } 4627 4628 if (tpi_mp == NULL) 4629 return (ENOMEM); 4630 4631 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4632 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4633 connp->conn_flags |= (IPCL_TCP6|IPCL_EAGER); 4634 connp->conn_fully_bound = B_FALSE; 4635 4636 if (tcp_trace) 4637 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4638 4639 /* Inherit information from the "parent" */ 4640 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4641 tcp->tcp_family = ltcp->tcp_family; 4642 tcp->tcp_wq = ltcp->tcp_wq; 4643 tcp->tcp_rq = ltcp->tcp_rq; 4644 tcp->tcp_mss = tcp_mss_def_ipv6; 4645 tcp->tcp_detached = B_TRUE; 4646 if ((err = tcp_init_values(tcp)) != 0) { 4647 freemsg(tpi_mp); 4648 return (err); 4649 } 4650 4651 if (ipvers == IPV4_VERSION) { 4652 if ((err = tcp_header_init_ipv4(tcp)) != 0) { 4653 freemsg(tpi_mp); 4654 return (err); 4655 } 4656 ASSERT(tcp->tcp_ipha != NULL); 4657 } else { 4658 /* ifindex must be already set */ 4659 ASSERT(ifindex != 0); 4660 4661 if (ltcp->tcp_bound_if != 0) { 4662 /* 4663 * Set newtcp's bound_if equal to 4664 * listener's value. If ifindex is 4665 * not the same as ltcp->tcp_bound_if, 4666 * it must be a packet for the ipmp group 4667 * of interfaces 4668 */ 4669 tcp->tcp_bound_if = ltcp->tcp_bound_if; 4670 } else if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src)) { 4671 tcp->tcp_bound_if = ifindex; 4672 } 4673 4674 tcp->tcp_ipv6_recvancillary = ltcp->tcp_ipv6_recvancillary; 4675 tcp->tcp_recvifindex = 0; 4676 tcp->tcp_recvhops = 0xffffffffU; 4677 ASSERT(tcp->tcp_ip6h != NULL); 4678 } 4679 4680 tcp->tcp_lport = ltcp->tcp_lport; 4681 4682 if (ltcp->tcp_ipversion == tcp->tcp_ipversion) { 4683 if (tcp->tcp_iphc_len != ltcp->tcp_iphc_len) { 4684 /* 4685 * Listener had options of some sort; eager inherits. 4686 * Free up the eager template and allocate one 4687 * of the right size. 4688 */ 4689 if (tcp->tcp_hdr_grown) { 4690 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 4691 } else { 4692 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 4693 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 4694 } 4695 tcp->tcp_iphc = kmem_zalloc(ltcp->tcp_iphc_len, 4696 KM_NOSLEEP); 4697 if (tcp->tcp_iphc == NULL) { 4698 tcp->tcp_iphc_len = 0; 4699 freemsg(tpi_mp); 4700 return (ENOMEM); 4701 } 4702 tcp->tcp_iphc_len = ltcp->tcp_iphc_len; 4703 tcp->tcp_hdr_grown = B_TRUE; 4704 } 4705 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4706 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4707 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4708 tcp->tcp_ip6_hops = ltcp->tcp_ip6_hops; 4709 tcp->tcp_ip6_vcf = ltcp->tcp_ip6_vcf; 4710 4711 /* 4712 * Copy the IP+TCP header template from listener to eager 4713 */ 4714 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4715 if (tcp->tcp_ipversion == IPV6_VERSION) { 4716 if (((ip6i_t *)(tcp->tcp_iphc))->ip6i_nxt == 4717 IPPROTO_RAW) { 4718 tcp->tcp_ip6h = 4719 (ip6_t *)(tcp->tcp_iphc + 4720 sizeof (ip6i_t)); 4721 } else { 4722 tcp->tcp_ip6h = 4723 (ip6_t *)(tcp->tcp_iphc); 4724 } 4725 tcp->tcp_ipha = NULL; 4726 } else { 4727 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4728 tcp->tcp_ip6h = NULL; 4729 } 4730 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4731 tcp->tcp_ip_hdr_len); 4732 } else { 4733 /* 4734 * only valid case when ipversion of listener and 4735 * eager differ is when listener is IPv6 and 4736 * eager is IPv4. 4737 * Eager header template has been initialized to the 4738 * maximum v4 header sizes, which includes space for 4739 * TCP and IP options. 4740 */ 4741 ASSERT((ltcp->tcp_ipversion == IPV6_VERSION) && 4742 (tcp->tcp_ipversion == IPV4_VERSION)); 4743 ASSERT(tcp->tcp_iphc_len >= 4744 TCP_MAX_COMBINED_HEADER_LENGTH); 4745 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4746 /* copy IP header fields individually */ 4747 tcp->tcp_ipha->ipha_ttl = 4748 ltcp->tcp_ip6h->ip6_hops; 4749 bcopy(ltcp->tcp_tcph->th_lport, 4750 tcp->tcp_tcph->th_lport, sizeof (ushort_t)); 4751 } 4752 4753 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4754 bcopy(tcp->tcp_tcph->th_fport, &tcp->tcp_fport, 4755 sizeof (in_port_t)); 4756 4757 if (ltcp->tcp_lport == 0) { 4758 tcp->tcp_lport = *(in_port_t *)tcph->th_fport; 4759 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, 4760 sizeof (in_port_t)); 4761 } 4762 4763 if (tcp->tcp_ipversion == IPV4_VERSION) { 4764 ASSERT(ipha != NULL); 4765 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4766 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4767 4768 /* Source routing option copyover (reverse it) */ 4769 if (tcp_rev_src_routes) 4770 tcp_opt_reverse(tcp, ipha); 4771 } else { 4772 ASSERT(ip6h != NULL); 4773 tcp->tcp_ip6h->ip6_dst = ip6h->ip6_src; 4774 tcp->tcp_ip6h->ip6_src = ip6h->ip6_dst; 4775 } 4776 4777 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4778 /* 4779 * If the SYN contains a credential, it's a loopback packet; attach 4780 * the credential to the TPI message. 4781 */ 4782 if ((cr = DB_CRED(idmp)) != NULL) { 4783 mblk_setcred(tpi_mp, cr); 4784 DB_CPID(tpi_mp) = DB_CPID(idmp); 4785 } 4786 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4787 4788 /* Inherit the listener's SSL protection state */ 4789 4790 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4791 kssl_hold_ent(tcp->tcp_kssl_ent); 4792 tcp->tcp_kssl_pending = B_TRUE; 4793 } 4794 4795 return (0); 4796 } 4797 4798 4799 int 4800 tcp_conn_create_v4(conn_t *lconnp, conn_t *connp, ipha_t *ipha, 4801 tcph_t *tcph, mblk_t *idmp) 4802 { 4803 tcp_t *ltcp = lconnp->conn_tcp; 4804 tcp_t *tcp = connp->conn_tcp; 4805 sin_t sin; 4806 mblk_t *tpi_mp = NULL; 4807 int err; 4808 cred_t *cr; 4809 4810 sin = sin_null; 4811 sin.sin_addr.s_addr = ipha->ipha_src; 4812 sin.sin_port = *(uint16_t *)tcph->th_lport; 4813 sin.sin_family = AF_INET; 4814 if (ltcp->tcp_recvdstaddr) { 4815 sin_t sind; 4816 4817 sind = sin_null; 4818 sind.sin_addr.s_addr = ipha->ipha_dst; 4819 sind.sin_port = *(uint16_t *)tcph->th_fport; 4820 sind.sin_family = AF_INET; 4821 tpi_mp = mi_tpi_extconn_ind(NULL, 4822 (char *)&sind, sizeof (sin_t), (char *)&tcp, 4823 (t_scalar_t)sizeof (intptr_t), (char *)&sind, 4824 sizeof (sin_t), (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4825 } else { 4826 tpi_mp = mi_tpi_conn_ind(NULL, 4827 (char *)&sin, sizeof (sin_t), 4828 (char *)&tcp, (t_scalar_t)sizeof (intptr_t), 4829 (t_scalar_t)ltcp->tcp_conn_req_seqnum); 4830 } 4831 4832 if (tpi_mp == NULL) { 4833 return (ENOMEM); 4834 } 4835 4836 connp->conn_flags |= (IPCL_TCP4|IPCL_EAGER); 4837 connp->conn_send = ip_output; 4838 connp->conn_recv = tcp_input; 4839 connp->conn_fully_bound = B_FALSE; 4840 4841 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &connp->conn_srcv6); 4842 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &connp->conn_remv6); 4843 connp->conn_fport = *(uint16_t *)tcph->th_lport; 4844 connp->conn_lport = *(uint16_t *)tcph->th_fport; 4845 4846 if (tcp_trace) { 4847 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_NOSLEEP); 4848 } 4849 4850 /* Inherit information from the "parent" */ 4851 tcp->tcp_ipversion = ltcp->tcp_ipversion; 4852 tcp->tcp_family = ltcp->tcp_family; 4853 tcp->tcp_wq = ltcp->tcp_wq; 4854 tcp->tcp_rq = ltcp->tcp_rq; 4855 tcp->tcp_mss = tcp_mss_def_ipv4; 4856 tcp->tcp_detached = B_TRUE; 4857 if ((err = tcp_init_values(tcp)) != 0) { 4858 freemsg(tpi_mp); 4859 return (err); 4860 } 4861 4862 /* 4863 * Let's make sure that eager tcp template has enough space to 4864 * copy IPv4 listener's tcp template. Since the conn_t structure is 4865 * preserved and tcp_iphc_len is also preserved, an eager conn_t may 4866 * have a tcp_template of total len TCP_MAX_COMBINED_HEADER_LENGTH or 4867 * more (in case of re-allocation of conn_t with tcp-IPv6 template with 4868 * extension headers or with ip6i_t struct). Note that bcopy() below 4869 * copies listener tcp's hdr_len which cannot be greater than TCP_MAX_ 4870 * COMBINED_HEADER_LENGTH as this listener must be a IPv4 listener. 4871 */ 4872 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 4873 ASSERT(ltcp->tcp_hdr_len <= TCP_MAX_COMBINED_HEADER_LENGTH); 4874 4875 tcp->tcp_hdr_len = ltcp->tcp_hdr_len; 4876 tcp->tcp_ip_hdr_len = ltcp->tcp_ip_hdr_len; 4877 tcp->tcp_tcp_hdr_len = ltcp->tcp_tcp_hdr_len; 4878 tcp->tcp_ttl = ltcp->tcp_ttl; 4879 tcp->tcp_tos = ltcp->tcp_tos; 4880 4881 /* Copy the IP+TCP header template from listener to eager */ 4882 bcopy(ltcp->tcp_iphc, tcp->tcp_iphc, ltcp->tcp_hdr_len); 4883 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 4884 tcp->tcp_ip6h = NULL; 4885 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + 4886 tcp->tcp_ip_hdr_len); 4887 4888 /* Initialize the IP addresses and Ports */ 4889 tcp->tcp_ipha->ipha_dst = ipha->ipha_src; 4890 tcp->tcp_ipha->ipha_src = ipha->ipha_dst; 4891 bcopy(tcph->th_lport, tcp->tcp_tcph->th_fport, sizeof (in_port_t)); 4892 bcopy(tcph->th_fport, tcp->tcp_tcph->th_lport, sizeof (in_port_t)); 4893 4894 /* Source routing option copyover (reverse it) */ 4895 if (tcp_rev_src_routes) 4896 tcp_opt_reverse(tcp, ipha); 4897 4898 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 4899 4900 /* 4901 * If the SYN contains a credential, it's a loopback packet; attach 4902 * the credential to the TPI message. 4903 */ 4904 if ((cr = DB_CRED(idmp)) != NULL) { 4905 mblk_setcred(tpi_mp, cr); 4906 DB_CPID(tpi_mp) = DB_CPID(idmp); 4907 } 4908 tcp->tcp_conn.tcp_eager_conn_ind = tpi_mp; 4909 4910 /* Inherit the listener's SSL protection state */ 4911 if ((tcp->tcp_kssl_ent = ltcp->tcp_kssl_ent) != NULL) { 4912 kssl_hold_ent(tcp->tcp_kssl_ent); 4913 tcp->tcp_kssl_pending = B_TRUE; 4914 } 4915 4916 return (0); 4917 } 4918 4919 /* 4920 * sets up conn for ipsec. 4921 * if the first mblk is M_CTL it is consumed and mpp is updated. 4922 * in case of error mpp is freed. 4923 */ 4924 conn_t * 4925 tcp_get_ipsec_conn(tcp_t *tcp, squeue_t *sqp, mblk_t **mpp) 4926 { 4927 conn_t *connp = tcp->tcp_connp; 4928 conn_t *econnp; 4929 squeue_t *new_sqp; 4930 mblk_t *first_mp = *mpp; 4931 mblk_t *mp = *mpp; 4932 boolean_t mctl_present = B_FALSE; 4933 uint_t ipvers; 4934 4935 econnp = tcp_get_conn(sqp); 4936 if (econnp == NULL) { 4937 freemsg(first_mp); 4938 return (NULL); 4939 } 4940 if (DB_TYPE(mp) == M_CTL) { 4941 if (mp->b_cont == NULL || 4942 mp->b_cont->b_datap->db_type != M_DATA) { 4943 freemsg(first_mp); 4944 return (NULL); 4945 } 4946 mp = mp->b_cont; 4947 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) == 0) { 4948 freemsg(first_mp); 4949 return (NULL); 4950 } 4951 4952 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 4953 first_mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4954 mctl_present = B_TRUE; 4955 } else { 4956 ASSERT(mp->b_datap->db_struioflag & STRUIO_POLICY); 4957 mp->b_datap->db_struioflag &= ~STRUIO_POLICY; 4958 } 4959 4960 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 4961 DB_CKSUMSTART(mp) = 0; 4962 4963 ASSERT(OK_32PTR(mp->b_rptr)); 4964 ipvers = IPH_HDR_VERSION(mp->b_rptr); 4965 if (ipvers == IPV4_VERSION) { 4966 uint16_t *up; 4967 uint32_t ports; 4968 ipha_t *ipha; 4969 4970 ipha = (ipha_t *)mp->b_rptr; 4971 up = (uint16_t *)((uchar_t *)ipha + 4972 IPH_HDR_LENGTH(ipha) + TCP_PORTS_OFFSET); 4973 ports = *(uint32_t *)up; 4974 IPCL_TCP_EAGER_INIT(econnp, IPPROTO_TCP, 4975 ipha->ipha_dst, ipha->ipha_src, ports); 4976 } else { 4977 uint16_t *up; 4978 uint32_t ports; 4979 uint16_t ip_hdr_len; 4980 uint8_t *nexthdrp; 4981 ip6_t *ip6h; 4982 tcph_t *tcph; 4983 4984 ip6h = (ip6_t *)mp->b_rptr; 4985 if (ip6h->ip6_nxt == IPPROTO_TCP) { 4986 ip_hdr_len = IPV6_HDR_LEN; 4987 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ip_hdr_len, 4988 &nexthdrp) || *nexthdrp != IPPROTO_TCP) { 4989 CONN_DEC_REF(econnp); 4990 freemsg(first_mp); 4991 return (NULL); 4992 } 4993 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 4994 up = (uint16_t *)tcph->th_lport; 4995 ports = *(uint32_t *)up; 4996 IPCL_TCP_EAGER_INIT_V6(econnp, IPPROTO_TCP, 4997 ip6h->ip6_dst, ip6h->ip6_src, ports); 4998 } 4999 5000 /* 5001 * The caller already ensured that there is a sqp present. 5002 */ 5003 econnp->conn_sqp = new_sqp; 5004 5005 if (connp->conn_policy != NULL) { 5006 ipsec_in_t *ii; 5007 ii = (ipsec_in_t *)(first_mp->b_rptr); 5008 ASSERT(ii->ipsec_in_policy == NULL); 5009 IPPH_REFHOLD(connp->conn_policy); 5010 ii->ipsec_in_policy = connp->conn_policy; 5011 5012 first_mp->b_datap->db_type = IPSEC_POLICY_SET; 5013 if (!ip_bind_ipsec_policy_set(econnp, first_mp)) { 5014 CONN_DEC_REF(econnp); 5015 freemsg(first_mp); 5016 return (NULL); 5017 } 5018 } 5019 5020 if (ipsec_conn_cache_policy(econnp, ipvers == IPV4_VERSION) != 0) { 5021 CONN_DEC_REF(econnp); 5022 freemsg(first_mp); 5023 return (NULL); 5024 } 5025 5026 /* 5027 * If we know we have some policy, pass the "IPSEC" 5028 * options size TCP uses this adjust the MSS. 5029 */ 5030 econnp->conn_tcp->tcp_ipsec_overhead = conn_ipsec_length(econnp); 5031 if (mctl_present) { 5032 freeb(first_mp); 5033 *mpp = mp; 5034 } 5035 5036 return (econnp); 5037 } 5038 5039 /* 5040 * tcp_get_conn/tcp_free_conn 5041 * 5042 * tcp_get_conn is used to get a clean tcp connection structure. 5043 * It tries to reuse the connections put on the freelist by the 5044 * time_wait_collector failing which it goes to kmem_cache. This 5045 * way has two benefits compared to just allocating from and 5046 * freeing to kmem_cache. 5047 * 1) The time_wait_collector can free (which includes the cleanup) 5048 * outside the squeue. So when the interrupt comes, we have a clean 5049 * connection sitting in the freelist. Obviously, this buys us 5050 * performance. 5051 * 5052 * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_conn_request 5053 * has multiple disadvantages - tying up the squeue during alloc, and the 5054 * fact that IPSec policy initialization has to happen here which 5055 * requires us sending a M_CTL and checking for it i.e. real ugliness. 5056 * But allocating the conn/tcp in IP land is also not the best since 5057 * we can't check the 'q' and 'q0' which are protected by squeue and 5058 * blindly allocate memory which might have to be freed here if we are 5059 * not allowed to accept the connection. By using the freelist and 5060 * putting the conn/tcp back in freelist, we don't pay a penalty for 5061 * allocating memory without checking 'q/q0' and freeing it if we can't 5062 * accept the connection. 5063 * 5064 * Care should be taken to put the conn back in the same squeue's freelist 5065 * from which it was allocated. Best results are obtained if conn is 5066 * allocated from listener's squeue and freed to the same. Time wait 5067 * collector will free up the freelist is the connection ends up sitting 5068 * there for too long. 5069 */ 5070 void * 5071 tcp_get_conn(void *arg) 5072 { 5073 tcp_t *tcp = NULL; 5074 conn_t *connp = NULL; 5075 squeue_t *sqp = (squeue_t *)arg; 5076 tcp_squeue_priv_t *tcp_time_wait; 5077 5078 tcp_time_wait = 5079 *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP)); 5080 5081 mutex_enter(&tcp_time_wait->tcp_time_wait_lock); 5082 tcp = tcp_time_wait->tcp_free_list; 5083 ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0)); 5084 if (tcp != NULL) { 5085 tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next; 5086 tcp_time_wait->tcp_free_list_cnt--; 5087 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5088 tcp->tcp_time_wait_next = NULL; 5089 connp = tcp->tcp_connp; 5090 connp->conn_flags |= IPCL_REUSED; 5091 return ((void *)connp); 5092 } 5093 mutex_exit(&tcp_time_wait->tcp_time_wait_lock); 5094 if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP)) == NULL) 5095 return (NULL); 5096 return ((void *)connp); 5097 } 5098 5099 /* BEGIN CSTYLED */ 5100 /* 5101 * 5102 * The sockfs ACCEPT path: 5103 * ======================= 5104 * 5105 * The eager is now established in its own perimeter as soon as SYN is 5106 * received in tcp_conn_request(). When sockfs receives conn_ind, it 5107 * completes the accept processing on the acceptor STREAM. The sending 5108 * of conn_ind part is common for both sockfs listener and a TLI/XTI 5109 * listener but a TLI/XTI listener completes the accept processing 5110 * on the listener perimeter. 5111 * 5112 * Common control flow for 3 way handshake: 5113 * ---------------------------------------- 5114 * 5115 * incoming SYN (listener perimeter) -> tcp_rput_data() 5116 * -> tcp_conn_request() 5117 * 5118 * incoming SYN-ACK-ACK (eager perim) -> tcp_rput_data() 5119 * send T_CONN_IND (listener perim) -> tcp_send_conn_ind() 5120 * 5121 * Sockfs ACCEPT Path: 5122 * ------------------- 5123 * 5124 * open acceptor stream (ip_tcpopen allocates tcp_wput_accept() 5125 * as STREAM entry point) 5126 * 5127 * soaccept() sends T_CONN_RES on the acceptor STREAM to tcp_wput_accept() 5128 * 5129 * tcp_wput_accept() extracts the eager and makes the q->q_ptr <-> eager 5130 * association (we are not behind eager's squeue but sockfs is protecting us 5131 * and no one knows about this stream yet. The STREAMS entry point q->q_info 5132 * is changed to point at tcp_wput(). 5133 * 5134 * tcp_wput_accept() sends any deferred eagers via tcp_send_pending() to 5135 * listener (done on listener's perimeter). 5136 * 5137 * tcp_wput_accept() calls tcp_accept_finish() on eagers perimeter to finish 5138 * accept. 5139 * 5140 * TLI/XTI client ACCEPT path: 5141 * --------------------------- 5142 * 5143 * soaccept() sends T_CONN_RES on the listener STREAM. 5144 * 5145 * tcp_accept() -> tcp_accept_swap() complete the processing and send 5146 * the bind_mp to eager perimeter to finish accept (tcp_rput_other()). 5147 * 5148 * Locks: 5149 * ====== 5150 * 5151 * listener->tcp_eager_lock protects the listeners->tcp_eager_next_q0 and 5152 * and listeners->tcp_eager_next_q. 5153 * 5154 * Referencing: 5155 * ============ 5156 * 5157 * 1) We start out in tcp_conn_request by eager placing a ref on 5158 * listener and listener adding eager to listeners->tcp_eager_next_q0. 5159 * 5160 * 2) When a SYN-ACK-ACK arrives, we send the conn_ind to listener. Before 5161 * doing so we place a ref on the eager. This ref is finally dropped at the 5162 * end of tcp_accept_finish() while unwinding from the squeue, i.e. the 5163 * reference is dropped by the squeue framework. 5164 * 5165 * 3) The ref on listener placed in 1 above is dropped in tcp_accept_finish 5166 * 5167 * The reference must be released by the same entity that added the reference 5168 * In the above scheme, the eager is the entity that adds and releases the 5169 * references. Note that tcp_accept_finish executes in the squeue of the eager 5170 * (albeit after it is attached to the acceptor stream). Though 1. executes 5171 * in the listener's squeue, the eager is nascent at this point and the 5172 * reference can be considered to have been added on behalf of the eager. 5173 * 5174 * Eager getting a Reset or listener closing: 5175 * ========================================== 5176 * 5177 * Once the listener and eager are linked, the listener never does the unlink. 5178 * If the listener needs to close, tcp_eager_cleanup() is called which queues 5179 * a message on all eager perimeter. The eager then does the unlink, clears 5180 * any pointers to the listener's queue and drops the reference to the 5181 * listener. The listener waits in tcp_close outside the squeue until its 5182 * refcount has dropped to 1. This ensures that the listener has waited for 5183 * all eagers to clear their association with the listener. 5184 * 5185 * Similarly, if eager decides to go away, it can unlink itself and close. 5186 * When the T_CONN_RES comes down, we check if eager has closed. Note that 5187 * the reference to eager is still valid because of the extra ref we put 5188 * in tcp_send_conn_ind. 5189 * 5190 * Listener can always locate the eager under the protection 5191 * of the listener->tcp_eager_lock, and then do a refhold 5192 * on the eager during the accept processing. 5193 * 5194 * The acceptor stream accesses the eager in the accept processing 5195 * based on the ref placed on eager before sending T_conn_ind. 5196 * The only entity that can negate this refhold is a listener close 5197 * which is mutually exclusive with an active acceptor stream. 5198 * 5199 * Eager's reference on the listener 5200 * =================================== 5201 * 5202 * If the accept happens (even on a closed eager) the eager drops its 5203 * reference on the listener at the start of tcp_accept_finish. If the 5204 * eager is killed due to an incoming RST before the T_conn_ind is sent up, 5205 * the reference is dropped in tcp_closei_local. If the listener closes, 5206 * the reference is dropped in tcp_eager_kill. In all cases the reference 5207 * is dropped while executing in the eager's context (squeue). 5208 */ 5209 /* END CSTYLED */ 5210 5211 /* Process the SYN packet, mp, directed at the listener 'tcp' */ 5212 5213 /* 5214 * THIS FUNCTION IS DIRECTLY CALLED BY IP VIA SQUEUE FOR SYN. 5215 * tcp_rput_data will not see any SYN packets. 5216 */ 5217 /* ARGSUSED */ 5218 void 5219 tcp_conn_request(void *arg, mblk_t *mp, void *arg2) 5220 { 5221 tcph_t *tcph; 5222 uint32_t seg_seq; 5223 tcp_t *eager; 5224 uint_t ipvers; 5225 ipha_t *ipha; 5226 ip6_t *ip6h; 5227 int err; 5228 conn_t *econnp = NULL; 5229 squeue_t *new_sqp; 5230 mblk_t *mp1; 5231 uint_t ip_hdr_len; 5232 conn_t *connp = (conn_t *)arg; 5233 tcp_t *tcp = connp->conn_tcp; 5234 ire_t *ire; 5235 5236 if (tcp->tcp_state != TCPS_LISTEN) 5237 goto error2; 5238 5239 ASSERT((tcp->tcp_connp->conn_flags & IPCL_BOUND) != 0); 5240 5241 mutex_enter(&tcp->tcp_eager_lock); 5242 if (tcp->tcp_conn_req_cnt_q >= tcp->tcp_conn_req_max) { 5243 mutex_exit(&tcp->tcp_eager_lock); 5244 TCP_STAT(tcp_listendrop); 5245 BUMP_MIB(&tcp_mib, tcpListenDrop); 5246 if (tcp->tcp_debug) { 5247 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 5248 "tcp_conn_request: listen backlog (max=%d) " 5249 "overflow (%d pending) on %s", 5250 tcp->tcp_conn_req_max, tcp->tcp_conn_req_cnt_q, 5251 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 5252 } 5253 goto error2; 5254 } 5255 5256 if (tcp->tcp_conn_req_cnt_q0 >= 5257 tcp->tcp_conn_req_max + tcp_conn_req_max_q0) { 5258 /* 5259 * Q0 is full. Drop a pending half-open req from the queue 5260 * to make room for the new SYN req. Also mark the time we 5261 * drop a SYN. 5262 * 5263 * A more aggressive defense against SYN attack will 5264 * be to set the "tcp_syn_defense" flag now. 5265 */ 5266 TCP_STAT(tcp_listendropq0); 5267 tcp->tcp_last_rcv_lbolt = lbolt64; 5268 if (!tcp_drop_q0(tcp)) { 5269 mutex_exit(&tcp->tcp_eager_lock); 5270 BUMP_MIB(&tcp_mib, tcpListenDropQ0); 5271 if (tcp->tcp_debug) { 5272 (void) strlog(TCP_MOD_ID, 0, 3, SL_TRACE, 5273 "tcp_conn_request: listen half-open queue " 5274 "(max=%d) full (%d pending) on %s", 5275 tcp_conn_req_max_q0, 5276 tcp->tcp_conn_req_cnt_q0, 5277 tcp_display(tcp, NULL, 5278 DISP_PORT_ONLY)); 5279 } 5280 goto error2; 5281 } 5282 } 5283 mutex_exit(&tcp->tcp_eager_lock); 5284 5285 /* 5286 * IP adds STRUIO_EAGER and ensures that the received packet is 5287 * M_DATA even if conn_ipv6_recvpktinfo is enabled or for ip6 5288 * link local address. If IPSec is enabled, db_struioflag has 5289 * STRUIO_POLICY set (mutually exclusive from STRUIO_EAGER); 5290 * otherwise an error case if neither of them is set. 5291 */ 5292 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5293 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5294 DB_CKSUMSTART(mp) = 0; 5295 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 5296 econnp = (conn_t *)tcp_get_conn(arg2); 5297 if (econnp == NULL) 5298 goto error2; 5299 econnp->conn_sqp = new_sqp; 5300 } else if ((mp->b_datap->db_struioflag & STRUIO_POLICY) != 0) { 5301 /* 5302 * mp is updated in tcp_get_ipsec_conn(). 5303 */ 5304 econnp = tcp_get_ipsec_conn(tcp, arg2, &mp); 5305 if (econnp == NULL) { 5306 /* 5307 * mp freed by tcp_get_ipsec_conn. 5308 */ 5309 return; 5310 } 5311 } else { 5312 goto error2; 5313 } 5314 5315 ASSERT(DB_TYPE(mp) == M_DATA); 5316 5317 ipvers = IPH_HDR_VERSION(mp->b_rptr); 5318 ASSERT(ipvers == IPV6_VERSION || ipvers == IPV4_VERSION); 5319 ASSERT(OK_32PTR(mp->b_rptr)); 5320 if (ipvers == IPV4_VERSION) { 5321 ipha = (ipha_t *)mp->b_rptr; 5322 ip_hdr_len = IPH_HDR_LENGTH(ipha); 5323 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5324 } else { 5325 ip6h = (ip6_t *)mp->b_rptr; 5326 ip_hdr_len = ip_hdr_length_v6(mp, ip6h); 5327 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 5328 } 5329 5330 if (tcp->tcp_family == AF_INET) { 5331 ASSERT(ipvers == IPV4_VERSION); 5332 err = tcp_conn_create_v4(connp, econnp, ipha, tcph, mp); 5333 } else { 5334 err = tcp_conn_create_v6(connp, econnp, mp, tcph, ipvers, mp); 5335 } 5336 5337 if (err) 5338 goto error3; 5339 5340 eager = econnp->conn_tcp; 5341 5342 /* Inherit various TCP parameters from the listener */ 5343 eager->tcp_naglim = tcp->tcp_naglim; 5344 eager->tcp_first_timer_threshold = 5345 tcp->tcp_first_timer_threshold; 5346 eager->tcp_second_timer_threshold = 5347 tcp->tcp_second_timer_threshold; 5348 5349 eager->tcp_first_ctimer_threshold = 5350 tcp->tcp_first_ctimer_threshold; 5351 eager->tcp_second_ctimer_threshold = 5352 tcp->tcp_second_ctimer_threshold; 5353 5354 /* 5355 * tcp_adapt_ire() may change tcp_rwnd according to the ire metrics. 5356 * If it does not, the eager's receive window will be set to the 5357 * listener's receive window later in this function. 5358 */ 5359 eager->tcp_rwnd = 0; 5360 5361 /* 5362 * Inherit listener's tcp_init_cwnd. Need to do this before 5363 * calling tcp_process_options() where tcp_mss_set() is called 5364 * to set the initial cwnd. 5365 */ 5366 eager->tcp_init_cwnd = tcp->tcp_init_cwnd; 5367 5368 /* 5369 * Zones: tcp_adapt_ire() and tcp_send_data() both need the 5370 * zone id before the accept is completed in tcp_wput_accept(). 5371 */ 5372 econnp->conn_zoneid = connp->conn_zoneid; 5373 5374 /* Copy nexthop information from listener to eager */ 5375 if (connp->conn_nexthop_set) { 5376 econnp->conn_nexthop_set = connp->conn_nexthop_set; 5377 econnp->conn_nexthop_v4 = connp->conn_nexthop_v4; 5378 } 5379 5380 eager->tcp_hard_binding = B_TRUE; 5381 5382 tcp_bind_hash_insert(&tcp_bind_fanout[ 5383 TCP_BIND_HASH(eager->tcp_lport)], eager, 0); 5384 5385 CL_INET_CONNECT(eager); 5386 5387 /* 5388 * No need to check for multicast destination since ip will only pass 5389 * up multicasts to those that have expressed interest 5390 * TODO: what about rejecting broadcasts? 5391 * Also check that source is not a multicast or broadcast address. 5392 */ 5393 eager->tcp_state = TCPS_SYN_RCVD; 5394 5395 5396 /* 5397 * There should be no ire in the mp as we are being called after 5398 * receiving the SYN. 5399 */ 5400 ASSERT(tcp_ire_mp(mp) == NULL); 5401 5402 /* 5403 * Adapt our mss, ttl, ... according to information provided in IRE. 5404 */ 5405 5406 if (tcp_adapt_ire(eager, NULL) == 0) { 5407 /* Undo the bind_hash_insert */ 5408 tcp_bind_hash_remove(eager); 5409 goto error3; 5410 } 5411 5412 /* Process all TCP options. */ 5413 tcp_process_options(eager, tcph); 5414 5415 /* Is the other end ECN capable? */ 5416 if (tcp_ecn_permitted >= 1 && 5417 (tcph->th_flags[0] & (TH_ECE|TH_CWR)) == (TH_ECE|TH_CWR)) { 5418 eager->tcp_ecn_ok = B_TRUE; 5419 } 5420 5421 /* 5422 * listener->tcp_rq->q_hiwat should be the default window size or a 5423 * window size changed via SO_RCVBUF option. First round up the 5424 * eager's tcp_rwnd to the nearest MSS. Then find out the window 5425 * scale option value if needed. Call tcp_rwnd_set() to finish the 5426 * setting. 5427 * 5428 * Note if there is a rpipe metric associated with the remote host, 5429 * we should not inherit receive window size from listener. 5430 */ 5431 eager->tcp_rwnd = MSS_ROUNDUP( 5432 (eager->tcp_rwnd == 0 ? tcp->tcp_rq->q_hiwat : 5433 eager->tcp_rwnd), eager->tcp_mss); 5434 if (eager->tcp_snd_ws_ok) 5435 tcp_set_ws_value(eager); 5436 /* 5437 * Note that this is the only place tcp_rwnd_set() is called for 5438 * accepting a connection. We need to call it here instead of 5439 * after the 3-way handshake because we need to tell the other 5440 * side our rwnd in the SYN-ACK segment. 5441 */ 5442 (void) tcp_rwnd_set(eager, eager->tcp_rwnd); 5443 5444 /* 5445 * We eliminate the need for sockfs to send down a T_SVR4_OPTMGMT_REQ 5446 * via soaccept()->soinheritoptions() which essentially applies 5447 * all the listener options to the new STREAM. The options that we 5448 * need to take care of are: 5449 * SO_DEBUG, SO_REUSEADDR, SO_KEEPALIVE, SO_DONTROUTE, SO_BROADCAST, 5450 * SO_USELOOPBACK, SO_OOBINLINE, SO_DGRAM_ERRIND, SO_LINGER, 5451 * SO_SNDBUF, SO_RCVBUF. 5452 * 5453 * SO_RCVBUF: tcp_rwnd_set() above takes care of it. 5454 * SO_SNDBUF: Set the tcp_xmit_hiwater for the eager. When 5455 * tcp_maxpsz_set() gets called later from 5456 * tcp_accept_finish(), the option takes effect. 5457 * 5458 */ 5459 /* Set the TCP options */ 5460 eager->tcp_xmit_hiwater = tcp->tcp_xmit_hiwater; 5461 eager->tcp_dgram_errind = tcp->tcp_dgram_errind; 5462 eager->tcp_oobinline = tcp->tcp_oobinline; 5463 eager->tcp_reuseaddr = tcp->tcp_reuseaddr; 5464 eager->tcp_broadcast = tcp->tcp_broadcast; 5465 eager->tcp_useloopback = tcp->tcp_useloopback; 5466 eager->tcp_dontroute = tcp->tcp_dontroute; 5467 eager->tcp_linger = tcp->tcp_linger; 5468 eager->tcp_lingertime = tcp->tcp_lingertime; 5469 if (tcp->tcp_ka_enabled) 5470 eager->tcp_ka_enabled = 1; 5471 5472 /* Set the IP options */ 5473 econnp->conn_broadcast = connp->conn_broadcast; 5474 econnp->conn_loopback = connp->conn_loopback; 5475 econnp->conn_dontroute = connp->conn_dontroute; 5476 econnp->conn_reuseaddr = connp->conn_reuseaddr; 5477 5478 /* Put a ref on the listener for the eager. */ 5479 CONN_INC_REF(connp); 5480 mutex_enter(&tcp->tcp_eager_lock); 5481 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = eager; 5482 eager->tcp_eager_next_q0 = tcp->tcp_eager_next_q0; 5483 tcp->tcp_eager_next_q0 = eager; 5484 eager->tcp_eager_prev_q0 = tcp; 5485 5486 /* Set tcp_listener before adding it to tcp_conn_fanout */ 5487 eager->tcp_listener = tcp; 5488 eager->tcp_saved_listener = tcp; 5489 5490 /* 5491 * Tag this detached tcp vector for later retrieval 5492 * by our listener client in tcp_accept(). 5493 */ 5494 eager->tcp_conn_req_seqnum = tcp->tcp_conn_req_seqnum; 5495 tcp->tcp_conn_req_cnt_q0++; 5496 if (++tcp->tcp_conn_req_seqnum == -1) { 5497 /* 5498 * -1 is "special" and defined in TPI as something 5499 * that should never be used in T_CONN_IND 5500 */ 5501 ++tcp->tcp_conn_req_seqnum; 5502 } 5503 mutex_exit(&tcp->tcp_eager_lock); 5504 5505 if (tcp->tcp_syn_defense) { 5506 /* Don't drop the SYN that comes from a good IP source */ 5507 ipaddr_t *addr_cache = (ipaddr_t *)(tcp->tcp_ip_addr_cache); 5508 if (addr_cache != NULL && eager->tcp_remote == 5509 addr_cache[IP_ADDR_CACHE_HASH(eager->tcp_remote)]) { 5510 eager->tcp_dontdrop = B_TRUE; 5511 } 5512 } 5513 5514 /* 5515 * We need to insert the eager in its own perimeter but as soon 5516 * as we do that, we expose the eager to the classifier and 5517 * should not touch any field outside the eager's perimeter. 5518 * So do all the work necessary before inserting the eager 5519 * in its own perimeter. Be optimistic that ipcl_conn_insert() 5520 * will succeed but undo everything if it fails. 5521 */ 5522 seg_seq = ABE32_TO_U32(tcph->th_seq); 5523 eager->tcp_irs = seg_seq; 5524 eager->tcp_rack = seg_seq; 5525 eager->tcp_rnxt = seg_seq + 1; 5526 U32_TO_ABE32(eager->tcp_rnxt, eager->tcp_tcph->th_ack); 5527 BUMP_MIB(&tcp_mib, tcpPassiveOpens); 5528 eager->tcp_state = TCPS_SYN_RCVD; 5529 mp1 = tcp_xmit_mp(eager, eager->tcp_xmit_head, eager->tcp_mss, 5530 NULL, NULL, eager->tcp_iss, B_FALSE, NULL, B_FALSE); 5531 if (mp1 == NULL) 5532 goto error1; 5533 mblk_setcred(mp1, tcp->tcp_cred); 5534 DB_CPID(mp1) = tcp->tcp_cpid; 5535 5536 /* 5537 * We need to start the rto timer. In normal case, we start 5538 * the timer after sending the packet on the wire (or at 5539 * least believing that packet was sent by waiting for 5540 * CALL_IP_WPUT() to return). Since this is the first packet 5541 * being sent on the wire for the eager, our initial tcp_rto 5542 * is at least tcp_rexmit_interval_min which is a fairly 5543 * large value to allow the algorithm to adjust slowly to large 5544 * fluctuations of RTT during first few transmissions. 5545 * 5546 * Starting the timer first and then sending the packet in this 5547 * case shouldn't make much difference since tcp_rexmit_interval_min 5548 * is of the order of several 100ms and starting the timer 5549 * first and then sending the packet will result in difference 5550 * of few micro seconds. 5551 * 5552 * Without this optimization, we are forced to hold the fanout 5553 * lock across the ipcl_bind_insert() and sending the packet 5554 * so that we don't race against an incoming packet (maybe RST) 5555 * for this eager. 5556 */ 5557 5558 TCP_RECORD_TRACE(eager, mp1, TCP_TRACE_SEND_PKT); 5559 TCP_TIMER_RESTART(eager, eager->tcp_rto); 5560 5561 5562 /* 5563 * Insert the eager in its own perimeter now. We are ready to deal 5564 * with any packets on eager. 5565 */ 5566 if (eager->tcp_ipversion == IPV4_VERSION) { 5567 if (ipcl_conn_insert(econnp, IPPROTO_TCP, 0, 0, 0) != 0) { 5568 goto error; 5569 } 5570 } else { 5571 if (ipcl_conn_insert_v6(econnp, IPPROTO_TCP, 0, 0, 0, 0) != 0) { 5572 goto error; 5573 } 5574 } 5575 5576 /* mark conn as fully-bound */ 5577 econnp->conn_fully_bound = B_TRUE; 5578 5579 /* Send the SYN-ACK */ 5580 tcp_send_data(eager, eager->tcp_wq, mp1); 5581 freemsg(mp); 5582 5583 return; 5584 error: 5585 (void) TCP_TIMER_CANCEL(eager, eager->tcp_timer_tid); 5586 freemsg(mp1); 5587 error1: 5588 /* Undo what we did above */ 5589 mutex_enter(&tcp->tcp_eager_lock); 5590 tcp_eager_unlink(eager); 5591 mutex_exit(&tcp->tcp_eager_lock); 5592 /* Drop eager's reference on the listener */ 5593 CONN_DEC_REF(connp); 5594 5595 /* 5596 * Delete the cached ire in conn_ire_cache and also mark 5597 * the conn as CONDEMNED 5598 */ 5599 mutex_enter(&econnp->conn_lock); 5600 econnp->conn_state_flags |= CONN_CONDEMNED; 5601 ire = econnp->conn_ire_cache; 5602 econnp->conn_ire_cache = NULL; 5603 mutex_exit(&econnp->conn_lock); 5604 if (ire != NULL) 5605 IRE_REFRELE_NOTR(ire); 5606 5607 /* 5608 * tcp_accept_comm inserts the eager to the bind_hash 5609 * we need to remove it from the hash if ipcl_conn_insert 5610 * fails. 5611 */ 5612 tcp_bind_hash_remove(eager); 5613 /* Drop the eager ref placed in tcp_open_detached */ 5614 CONN_DEC_REF(econnp); 5615 5616 /* 5617 * If a connection already exists, send the mp to that connections so 5618 * that it can be appropriately dealt with. 5619 */ 5620 if ((econnp = ipcl_classify(mp, connp->conn_zoneid)) != NULL) { 5621 if (!IPCL_IS_CONNECTED(econnp)) { 5622 /* 5623 * Something bad happened. ipcl_conn_insert() 5624 * failed because a connection already existed 5625 * in connected hash but we can't find it 5626 * anymore (someone blew it away). Just 5627 * free this message and hopefully remote 5628 * will retransmit at which time the SYN can be 5629 * treated as a new connection or dealth with 5630 * a TH_RST if a connection already exists. 5631 */ 5632 freemsg(mp); 5633 } else { 5634 squeue_fill(econnp->conn_sqp, mp, tcp_input, 5635 econnp, SQTAG_TCP_CONN_REQ); 5636 } 5637 } else { 5638 /* Nobody wants this packet */ 5639 freemsg(mp); 5640 } 5641 return; 5642 error2: 5643 freemsg(mp); 5644 return; 5645 error3: 5646 CONN_DEC_REF(econnp); 5647 freemsg(mp); 5648 } 5649 5650 /* 5651 * In an ideal case of vertical partition in NUMA architecture, its 5652 * beneficial to have the listener and all the incoming connections 5653 * tied to the same squeue. The other constraint is that incoming 5654 * connections should be tied to the squeue attached to interrupted 5655 * CPU for obvious locality reason so this leaves the listener to 5656 * be tied to the same squeue. Our only problem is that when listener 5657 * is binding, the CPU that will get interrupted by the NIC whose 5658 * IP address the listener is binding to is not even known. So 5659 * the code below allows us to change that binding at the time the 5660 * CPU is interrupted by virtue of incoming connection's squeue. 5661 * 5662 * This is usefull only in case of a listener bound to a specific IP 5663 * address. For other kind of listeners, they get bound the 5664 * very first time and there is no attempt to rebind them. 5665 */ 5666 void 5667 tcp_conn_request_unbound(void *arg, mblk_t *mp, void *arg2) 5668 { 5669 conn_t *connp = (conn_t *)arg; 5670 squeue_t *sqp = (squeue_t *)arg2; 5671 squeue_t *new_sqp; 5672 uint32_t conn_flags; 5673 5674 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 5675 new_sqp = (squeue_t *)DB_CKSUMSTART(mp); 5676 } else { 5677 goto done; 5678 } 5679 5680 if (connp->conn_fanout == NULL) 5681 goto done; 5682 5683 if (!(connp->conn_flags & IPCL_FULLY_BOUND)) { 5684 mutex_enter(&connp->conn_fanout->connf_lock); 5685 mutex_enter(&connp->conn_lock); 5686 /* 5687 * No one from read or write side can access us now 5688 * except for already queued packets on this squeue. 5689 * But since we haven't changed the squeue yet, they 5690 * can't execute. If they are processed after we have 5691 * changed the squeue, they are sent back to the 5692 * correct squeue down below. 5693 */ 5694 if (connp->conn_sqp != new_sqp) { 5695 while (connp->conn_sqp != new_sqp) 5696 (void) casptr(&connp->conn_sqp, sqp, new_sqp); 5697 } 5698 5699 do { 5700 conn_flags = connp->conn_flags; 5701 conn_flags |= IPCL_FULLY_BOUND; 5702 (void) cas32(&connp->conn_flags, connp->conn_flags, 5703 conn_flags); 5704 } while (!(connp->conn_flags & IPCL_FULLY_BOUND)); 5705 5706 mutex_exit(&connp->conn_fanout->connf_lock); 5707 mutex_exit(&connp->conn_lock); 5708 } 5709 5710 done: 5711 if (connp->conn_sqp != sqp) { 5712 CONN_INC_REF(connp); 5713 squeue_fill(connp->conn_sqp, mp, 5714 connp->conn_recv, connp, SQTAG_TCP_CONN_REQ_UNBOUND); 5715 } else { 5716 tcp_conn_request(connp, mp, sqp); 5717 } 5718 } 5719 5720 /* 5721 * Successful connect request processing begins when our client passes 5722 * a T_CONN_REQ message into tcp_wput() and ends when tcp_rput() passes 5723 * our T_OK_ACK reply message upstream. The control flow looks like this: 5724 * upstream -> tcp_wput() -> tcp_wput_proto() -> tcp_connect() -> IP 5725 * upstream <- tcp_rput() <- IP 5726 * After various error checks are completed, tcp_connect() lays 5727 * the target address and port into the composite header template, 5728 * preallocates the T_OK_ACK reply message, construct a full 12 byte bind 5729 * request followed by an IRE request, and passes the three mblk message 5730 * down to IP looking like this: 5731 * O_T_BIND_REQ for IP --> IRE req --> T_OK_ACK for our client 5732 * Processing continues in tcp_rput() when we receive the following message: 5733 * T_BIND_ACK from IP --> IRE ack --> T_OK_ACK for our client 5734 * After consuming the first two mblks, tcp_rput() calls tcp_timer(), 5735 * to fire off the connection request, and then passes the T_OK_ACK mblk 5736 * upstream that we filled in below. There are, of course, numerous 5737 * error conditions along the way which truncate the processing described 5738 * above. 5739 */ 5740 static void 5741 tcp_connect(tcp_t *tcp, mblk_t *mp) 5742 { 5743 sin_t *sin; 5744 sin6_t *sin6; 5745 queue_t *q = tcp->tcp_wq; 5746 struct T_conn_req *tcr; 5747 ipaddr_t *dstaddrp; 5748 in_port_t dstport; 5749 uint_t srcid; 5750 5751 tcr = (struct T_conn_req *)mp->b_rptr; 5752 5753 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 5754 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tcr)) { 5755 tcp_err_ack(tcp, mp, TPROTO, 0); 5756 return; 5757 } 5758 5759 /* 5760 * Determine packet type based on type of address passed in 5761 * the request should contain an IPv4 or IPv6 address. 5762 * Make sure that address family matches the type of 5763 * family of the the address passed down 5764 */ 5765 switch (tcr->DEST_length) { 5766 default: 5767 tcp_err_ack(tcp, mp, TBADADDR, 0); 5768 return; 5769 5770 case (sizeof (sin_t) - sizeof (sin->sin_zero)): { 5771 /* 5772 * XXX: The check for valid DEST_length was not there 5773 * in earlier releases and some buggy 5774 * TLI apps (e.g Sybase) got away with not feeding 5775 * in sin_zero part of address. 5776 * We allow that bug to keep those buggy apps humming. 5777 * Test suites require the check on DEST_length. 5778 * We construct a new mblk with valid DEST_length 5779 * free the original so the rest of the code does 5780 * not have to keep track of this special shorter 5781 * length address case. 5782 */ 5783 mblk_t *nmp; 5784 struct T_conn_req *ntcr; 5785 sin_t *nsin; 5786 5787 nmp = allocb(sizeof (struct T_conn_req) + sizeof (sin_t) + 5788 tcr->OPT_length, BPRI_HI); 5789 if (nmp == NULL) { 5790 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 5791 return; 5792 } 5793 ntcr = (struct T_conn_req *)nmp->b_rptr; 5794 bzero(ntcr, sizeof (struct T_conn_req)); /* zero fill */ 5795 ntcr->PRIM_type = T_CONN_REQ; 5796 ntcr->DEST_length = sizeof (sin_t); 5797 ntcr->DEST_offset = sizeof (struct T_conn_req); 5798 5799 nsin = (sin_t *)((uchar_t *)ntcr + ntcr->DEST_offset); 5800 *nsin = sin_null; 5801 /* Get pointer to shorter address to copy from original mp */ 5802 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5803 tcr->DEST_length); /* extract DEST_length worth of sin_t */ 5804 if (sin == NULL || !OK_32PTR((char *)sin)) { 5805 freemsg(nmp); 5806 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5807 return; 5808 } 5809 nsin->sin_family = sin->sin_family; 5810 nsin->sin_port = sin->sin_port; 5811 nsin->sin_addr = sin->sin_addr; 5812 /* Note:nsin->sin_zero zero-fill with sin_null assign above */ 5813 nmp->b_wptr = (uchar_t *)&nsin[1]; 5814 if (tcr->OPT_length != 0) { 5815 ntcr->OPT_length = tcr->OPT_length; 5816 ntcr->OPT_offset = nmp->b_wptr - nmp->b_rptr; 5817 bcopy((uchar_t *)tcr + tcr->OPT_offset, 5818 (uchar_t *)ntcr + ntcr->OPT_offset, 5819 tcr->OPT_length); 5820 nmp->b_wptr += tcr->OPT_length; 5821 } 5822 freemsg(mp); /* original mp freed */ 5823 mp = nmp; /* re-initialize original variables */ 5824 tcr = ntcr; 5825 } 5826 /* FALLTHRU */ 5827 5828 case sizeof (sin_t): 5829 sin = (sin_t *)mi_offset_param(mp, tcr->DEST_offset, 5830 sizeof (sin_t)); 5831 if (sin == NULL || !OK_32PTR((char *)sin)) { 5832 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5833 return; 5834 } 5835 if (tcp->tcp_family != AF_INET || 5836 sin->sin_family != AF_INET) { 5837 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 5838 return; 5839 } 5840 if (sin->sin_port == 0) { 5841 tcp_err_ack(tcp, mp, TBADADDR, 0); 5842 return; 5843 } 5844 if (tcp->tcp_connp && tcp->tcp_connp->conn_ipv6_v6only) { 5845 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 5846 return; 5847 } 5848 5849 break; 5850 5851 case sizeof (sin6_t): 5852 sin6 = (sin6_t *)mi_offset_param(mp, tcr->DEST_offset, 5853 sizeof (sin6_t)); 5854 if (sin6 == NULL || !OK_32PTR((char *)sin6)) { 5855 tcp_err_ack(tcp, mp, TSYSERR, EINVAL); 5856 return; 5857 } 5858 if (tcp->tcp_family != AF_INET6 || 5859 sin6->sin6_family != AF_INET6) { 5860 tcp_err_ack(tcp, mp, TSYSERR, EAFNOSUPPORT); 5861 return; 5862 } 5863 if (sin6->sin6_port == 0) { 5864 tcp_err_ack(tcp, mp, TBADADDR, 0); 5865 return; 5866 } 5867 break; 5868 } 5869 /* 5870 * TODO: If someone in TCPS_TIME_WAIT has this dst/port we 5871 * should key on their sequence number and cut them loose. 5872 */ 5873 5874 /* 5875 * If options passed in, feed it for verification and handling 5876 */ 5877 if (tcr->OPT_length != 0) { 5878 mblk_t *ok_mp; 5879 mblk_t *discon_mp; 5880 mblk_t *conn_opts_mp; 5881 int t_error, sys_error, do_disconnect; 5882 5883 conn_opts_mp = NULL; 5884 5885 if (tcp_conprim_opt_process(tcp, mp, 5886 &do_disconnect, &t_error, &sys_error) < 0) { 5887 if (do_disconnect) { 5888 ASSERT(t_error == 0 && sys_error == 0); 5889 discon_mp = mi_tpi_discon_ind(NULL, 5890 ECONNREFUSED, 0); 5891 if (!discon_mp) { 5892 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 5893 TSYSERR, ENOMEM); 5894 return; 5895 } 5896 ok_mp = mi_tpi_ok_ack_alloc(mp); 5897 if (!ok_mp) { 5898 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 5899 TSYSERR, ENOMEM); 5900 return; 5901 } 5902 qreply(q, ok_mp); 5903 qreply(q, discon_mp); /* no flush! */ 5904 } else { 5905 ASSERT(t_error != 0); 5906 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, t_error, 5907 sys_error); 5908 } 5909 return; 5910 } 5911 /* 5912 * Success in setting options, the mp option buffer represented 5913 * by OPT_length/offset has been potentially modified and 5914 * contains results of option processing. We copy it in 5915 * another mp to save it for potentially influencing returning 5916 * it in T_CONN_CONN. 5917 */ 5918 if (tcr->OPT_length != 0) { /* there are resulting options */ 5919 conn_opts_mp = copyb(mp); 5920 if (!conn_opts_mp) { 5921 tcp_err_ack_prim(tcp, mp, T_CONN_REQ, 5922 TSYSERR, ENOMEM); 5923 return; 5924 } 5925 ASSERT(tcp->tcp_conn.tcp_opts_conn_req == NULL); 5926 tcp->tcp_conn.tcp_opts_conn_req = conn_opts_mp; 5927 /* 5928 * Note: 5929 * These resulting option negotiation can include any 5930 * end-to-end negotiation options but there no such 5931 * thing (yet?) in our TCP/IP. 5932 */ 5933 } 5934 } 5935 5936 /* 5937 * If we're connecting to an IPv4-mapped IPv6 address, we need to 5938 * make sure that the template IP header in the tcp structure is an 5939 * IPv4 header, and that the tcp_ipversion is IPV4_VERSION. We 5940 * need to this before we call tcp_bindi() so that the port lookup 5941 * code will look for ports in the correct port space (IPv4 and 5942 * IPv6 have separate port spaces). 5943 */ 5944 if (tcp->tcp_family == AF_INET6 && tcp->tcp_ipversion == IPV6_VERSION && 5945 IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 5946 int err = 0; 5947 5948 err = tcp_header_init_ipv4(tcp); 5949 if (err != 0) { 5950 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 5951 goto connect_failed; 5952 } 5953 if (tcp->tcp_lport != 0) 5954 *(uint16_t *)tcp->tcp_tcph->th_lport = tcp->tcp_lport; 5955 } 5956 5957 switch (tcp->tcp_state) { 5958 case TCPS_IDLE: 5959 /* 5960 * We support quick connect, refer to comments in 5961 * tcp_connect_*() 5962 */ 5963 /* FALLTHRU */ 5964 case TCPS_BOUND: 5965 case TCPS_LISTEN: 5966 if (tcp->tcp_family == AF_INET6) { 5967 if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) { 5968 tcp_connect_ipv6(tcp, mp, 5969 &sin6->sin6_addr, 5970 sin6->sin6_port, sin6->sin6_flowinfo, 5971 sin6->__sin6_src_id, sin6->sin6_scope_id); 5972 return; 5973 } 5974 /* 5975 * Destination adress is mapped IPv6 address. 5976 * Source bound address should be unspecified or 5977 * IPv6 mapped address as well. 5978 */ 5979 if (!IN6_IS_ADDR_UNSPECIFIED( 5980 &tcp->tcp_bound_source_v6) && 5981 !IN6_IS_ADDR_V4MAPPED(&tcp->tcp_bound_source_v6)) { 5982 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, 5983 EADDRNOTAVAIL); 5984 break; 5985 } 5986 dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr)); 5987 dstport = sin6->sin6_port; 5988 srcid = sin6->__sin6_src_id; 5989 } else { 5990 dstaddrp = &sin->sin_addr.s_addr; 5991 dstport = sin->sin_port; 5992 srcid = 0; 5993 } 5994 5995 tcp_connect_ipv4(tcp, mp, dstaddrp, dstport, srcid); 5996 return; 5997 default: 5998 mp = mi_tpi_err_ack_alloc(mp, TOUTSTATE, 0); 5999 break; 6000 } 6001 /* 6002 * Note: Code below is the "failure" case 6003 */ 6004 /* return error ack and blow away saved option results if any */ 6005 connect_failed: 6006 if (mp != NULL) 6007 putnext(tcp->tcp_rq, mp); 6008 else { 6009 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6010 TSYSERR, ENOMEM); 6011 } 6012 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6013 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6014 } 6015 6016 /* 6017 * Handle connect to IPv4 destinations, including connections for AF_INET6 6018 * sockets connecting to IPv4 mapped IPv6 destinations. 6019 */ 6020 static void 6021 tcp_connect_ipv4(tcp_t *tcp, mblk_t *mp, ipaddr_t *dstaddrp, in_port_t dstport, 6022 uint_t srcid) 6023 { 6024 tcph_t *tcph; 6025 mblk_t *mp1; 6026 ipaddr_t dstaddr = *dstaddrp; 6027 int32_t oldstate; 6028 uint16_t lport; 6029 6030 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 6031 6032 /* Check for attempt to connect to INADDR_ANY */ 6033 if (dstaddr == INADDR_ANY) { 6034 /* 6035 * SunOS 4.x and 4.3 BSD allow an application 6036 * to connect a TCP socket to INADDR_ANY. 6037 * When they do this, the kernel picks the 6038 * address of one interface and uses it 6039 * instead. The kernel usually ends up 6040 * picking the address of the loopback 6041 * interface. This is an undocumented feature. 6042 * However, we provide the same thing here 6043 * in order to have source and binary 6044 * compatibility with SunOS 4.x. 6045 * Update the T_CONN_REQ (sin/sin6) since it is used to 6046 * generate the T_CONN_CON. 6047 */ 6048 dstaddr = htonl(INADDR_LOOPBACK); 6049 *dstaddrp = dstaddr; 6050 } 6051 6052 /* Handle __sin6_src_id if socket not bound to an IP address */ 6053 if (srcid != 0 && tcp->tcp_ipha->ipha_src == INADDR_ANY) { 6054 ip_srcid_find_id(srcid, &tcp->tcp_ip_src_v6, 6055 tcp->tcp_connp->conn_zoneid); 6056 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_ip_src_v6, 6057 tcp->tcp_ipha->ipha_src); 6058 } 6059 6060 /* 6061 * Don't let an endpoint connect to itself. Note that 6062 * the test here does not catch the case where the 6063 * source IP addr was left unspecified by the user. In 6064 * this case, the source addr is set in tcp_adapt_ire() 6065 * using the reply to the T_BIND message that we send 6066 * down to IP here and the check is repeated in tcp_rput_other. 6067 */ 6068 if (dstaddr == tcp->tcp_ipha->ipha_src && 6069 dstport == tcp->tcp_lport) { 6070 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6071 goto failed; 6072 } 6073 6074 tcp->tcp_ipha->ipha_dst = dstaddr; 6075 IN6_IPADDR_TO_V4MAPPED(dstaddr, &tcp->tcp_remote_v6); 6076 6077 /* 6078 * Massage a source route if any putting the first hop 6079 * in iph_dst. Compute a starting value for the checksum which 6080 * takes into account that the original iph_dst should be 6081 * included in the checksum but that ip will include the 6082 * first hop in the source route in the tcp checksum. 6083 */ 6084 tcp->tcp_sum = ip_massage_options(tcp->tcp_ipha); 6085 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6086 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 6087 (tcp->tcp_ipha->ipha_dst & 0xffff)); 6088 if ((int)tcp->tcp_sum < 0) 6089 tcp->tcp_sum--; 6090 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 6091 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6092 (tcp->tcp_sum >> 16)); 6093 tcph = tcp->tcp_tcph; 6094 *(uint16_t *)tcph->th_fport = dstport; 6095 tcp->tcp_fport = dstport; 6096 6097 oldstate = tcp->tcp_state; 6098 /* 6099 * At this point the remote destination address and remote port fields 6100 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6101 * have to see which state tcp was in so we can take apropriate action. 6102 */ 6103 if (oldstate == TCPS_IDLE) { 6104 /* 6105 * We support a quick connect capability here, allowing 6106 * clients to transition directly from IDLE to SYN_SENT 6107 * tcp_bindi will pick an unused port, insert the connection 6108 * in the bind hash and transition to BOUND state. 6109 */ 6110 lport = tcp_update_next_port(tcp_next_port_to_try, B_TRUE); 6111 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6112 B_FALSE, B_FALSE); 6113 if (lport == 0) { 6114 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6115 goto failed; 6116 } 6117 } 6118 tcp->tcp_state = TCPS_SYN_SENT; 6119 6120 /* 6121 * TODO: allow data with connect requests 6122 * by unlinking M_DATA trailers here and 6123 * linking them in behind the T_OK_ACK mblk. 6124 * The tcp_rput() bind ack handler would then 6125 * feed them to tcp_wput_data() rather than call 6126 * tcp_timer(). 6127 */ 6128 mp = mi_tpi_ok_ack_alloc(mp); 6129 if (!mp) { 6130 tcp->tcp_state = oldstate; 6131 goto failed; 6132 } 6133 if (tcp->tcp_family == AF_INET) { 6134 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6135 sizeof (ipa_conn_t)); 6136 } else { 6137 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 6138 sizeof (ipa6_conn_t)); 6139 } 6140 if (mp1) { 6141 /* Hang onto the T_OK_ACK for later. */ 6142 linkb(mp1, mp); 6143 if (tcp->tcp_family == AF_INET) 6144 mp1 = ip_bind_v4(tcp->tcp_wq, mp1, tcp->tcp_connp); 6145 else { 6146 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6147 &tcp->tcp_sticky_ipp); 6148 } 6149 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6150 tcp->tcp_active_open = 1; 6151 /* 6152 * If the bind cannot complete immediately 6153 * IP will arrange to call tcp_rput_other 6154 * when the bind completes. 6155 */ 6156 if (mp1 != NULL) 6157 tcp_rput_other(tcp, mp1); 6158 return; 6159 } 6160 /* Error case */ 6161 tcp->tcp_state = oldstate; 6162 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6163 6164 failed: 6165 /* return error ack and blow away saved option results if any */ 6166 if (mp != NULL) 6167 putnext(tcp->tcp_rq, mp); 6168 else { 6169 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6170 TSYSERR, ENOMEM); 6171 } 6172 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6173 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6174 6175 } 6176 6177 /* 6178 * Handle connect to IPv6 destinations. 6179 */ 6180 static void 6181 tcp_connect_ipv6(tcp_t *tcp, mblk_t *mp, in6_addr_t *dstaddrp, 6182 in_port_t dstport, uint32_t flowinfo, uint_t srcid, uint32_t scope_id) 6183 { 6184 tcph_t *tcph; 6185 mblk_t *mp1; 6186 ip6_rthdr_t *rth; 6187 int32_t oldstate; 6188 uint16_t lport; 6189 6190 ASSERT(tcp->tcp_family == AF_INET6); 6191 6192 /* 6193 * If we're here, it means that the destination address is a native 6194 * IPv6 address. Return an error if tcp_ipversion is not IPv6. A 6195 * reason why it might not be IPv6 is if the socket was bound to an 6196 * IPv4-mapped IPv6 address. 6197 */ 6198 if (tcp->tcp_ipversion != IPV6_VERSION) { 6199 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6200 goto failed; 6201 } 6202 6203 /* 6204 * Interpret a zero destination to mean loopback. 6205 * Update the T_CONN_REQ (sin/sin6) since it is used to 6206 * generate the T_CONN_CON. 6207 */ 6208 if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp)) { 6209 *dstaddrp = ipv6_loopback; 6210 } 6211 6212 /* Handle __sin6_src_id if socket not bound to an IP address */ 6213 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip6h->ip6_src)) { 6214 ip_srcid_find_id(srcid, &tcp->tcp_ip6h->ip6_src, 6215 tcp->tcp_connp->conn_zoneid); 6216 tcp->tcp_ip_src_v6 = tcp->tcp_ip6h->ip6_src; 6217 } 6218 6219 /* 6220 * Take care of the scope_id now and add ip6i_t 6221 * if ip6i_t is not already allocated through TCP 6222 * sticky options. At this point tcp_ip6h does not 6223 * have dst info, thus use dstaddrp. 6224 */ 6225 if (scope_id != 0 && 6226 IN6_IS_ADDR_LINKSCOPE(dstaddrp)) { 6227 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 6228 ip6i_t *ip6i; 6229 6230 ipp->ipp_ifindex = scope_id; 6231 ip6i = (ip6i_t *)tcp->tcp_iphc; 6232 6233 if ((ipp->ipp_fields & IPPF_HAS_IP6I) && 6234 ip6i != NULL && (ip6i->ip6i_nxt == IPPROTO_RAW)) { 6235 /* Already allocated */ 6236 ip6i->ip6i_flags |= IP6I_IFINDEX; 6237 ip6i->ip6i_ifindex = ipp->ipp_ifindex; 6238 ipp->ipp_fields |= IPPF_SCOPE_ID; 6239 } else { 6240 int reterr; 6241 6242 ipp->ipp_fields |= IPPF_SCOPE_ID; 6243 if (ipp->ipp_fields & IPPF_HAS_IP6I) 6244 ip2dbg(("tcp_connect_v6: SCOPE_ID set\n")); 6245 reterr = tcp_build_hdrs(tcp->tcp_rq, tcp); 6246 if (reterr != 0) 6247 goto failed; 6248 ip1dbg(("tcp_connect_ipv6: tcp_bld_hdrs returned\n")); 6249 } 6250 } 6251 6252 /* 6253 * Don't let an endpoint connect to itself. Note that 6254 * the test here does not catch the case where the 6255 * source IP addr was left unspecified by the user. In 6256 * this case, the source addr is set in tcp_adapt_ire() 6257 * using the reply to the T_BIND message that we send 6258 * down to IP here and the check is repeated in tcp_rput_other. 6259 */ 6260 if (IN6_ARE_ADDR_EQUAL(dstaddrp, &tcp->tcp_ip6h->ip6_src) && 6261 (dstport == tcp->tcp_lport)) { 6262 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0); 6263 goto failed; 6264 } 6265 6266 tcp->tcp_ip6h->ip6_dst = *dstaddrp; 6267 tcp->tcp_remote_v6 = *dstaddrp; 6268 tcp->tcp_ip6h->ip6_vcf = 6269 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) | 6270 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK); 6271 6272 6273 /* 6274 * Massage a routing header (if present) putting the first hop 6275 * in ip6_dst. Compute a starting value for the checksum which 6276 * takes into account that the original ip6_dst should be 6277 * included in the checksum but that ip will include the 6278 * first hop in the source route in the tcp checksum. 6279 */ 6280 rth = ip_find_rthdr_v6(tcp->tcp_ip6h, (uint8_t *)tcp->tcp_tcph); 6281 if (rth != NULL) { 6282 6283 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, rth); 6284 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 6285 (tcp->tcp_sum >> 16)); 6286 } else { 6287 tcp->tcp_sum = 0; 6288 } 6289 6290 tcph = tcp->tcp_tcph; 6291 *(uint16_t *)tcph->th_fport = dstport; 6292 tcp->tcp_fport = dstport; 6293 6294 oldstate = tcp->tcp_state; 6295 /* 6296 * At this point the remote destination address and remote port fields 6297 * in the tcp-four-tuple have been filled in the tcp structure. Now we 6298 * have to see which state tcp was in so we can take apropriate action. 6299 */ 6300 if (oldstate == TCPS_IDLE) { 6301 /* 6302 * We support a quick connect capability here, allowing 6303 * clients to transition directly from IDLE to SYN_SENT 6304 * tcp_bindi will pick an unused port, insert the connection 6305 * in the bind hash and transition to BOUND state. 6306 */ 6307 lport = tcp_update_next_port(tcp_next_port_to_try, B_TRUE); 6308 lport = tcp_bindi(tcp, lport, &tcp->tcp_ip_src_v6, 0, B_TRUE, 6309 B_FALSE, B_FALSE); 6310 if (lport == 0) { 6311 mp = mi_tpi_err_ack_alloc(mp, TNOADDR, 0); 6312 goto failed; 6313 } 6314 } 6315 tcp->tcp_state = TCPS_SYN_SENT; 6316 /* 6317 * TODO: allow data with connect requests 6318 * by unlinking M_DATA trailers here and 6319 * linking them in behind the T_OK_ACK mblk. 6320 * The tcp_rput() bind ack handler would then 6321 * feed them to tcp_wput_data() rather than call 6322 * tcp_timer(). 6323 */ 6324 mp = mi_tpi_ok_ack_alloc(mp); 6325 if (!mp) { 6326 tcp->tcp_state = oldstate; 6327 goto failed; 6328 } 6329 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, sizeof (ipa6_conn_t)); 6330 if (mp1) { 6331 /* Hang onto the T_OK_ACK for later. */ 6332 linkb(mp1, mp); 6333 mp1 = ip_bind_v6(tcp->tcp_wq, mp1, tcp->tcp_connp, 6334 &tcp->tcp_sticky_ipp); 6335 BUMP_MIB(&tcp_mib, tcpActiveOpens); 6336 tcp->tcp_active_open = 1; 6337 /* ip_bind_v6() may return ACK or ERROR */ 6338 if (mp1 != NULL) 6339 tcp_rput_other(tcp, mp1); 6340 return; 6341 } 6342 /* Error case */ 6343 tcp->tcp_state = oldstate; 6344 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, ENOMEM); 6345 6346 failed: 6347 /* return error ack and blow away saved option results if any */ 6348 if (mp != NULL) 6349 putnext(tcp->tcp_rq, mp); 6350 else { 6351 tcp_err_ack_prim(tcp, NULL, T_CONN_REQ, 6352 TSYSERR, ENOMEM); 6353 } 6354 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 6355 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 6356 } 6357 6358 /* 6359 * We need a stream q for detached closing tcp connections 6360 * to use. Our client hereby indicates that this q is the 6361 * one to use. 6362 */ 6363 static void 6364 tcp_def_q_set(tcp_t *tcp, mblk_t *mp) 6365 { 6366 struct iocblk *iocp = (struct iocblk *)mp->b_rptr; 6367 queue_t *q = tcp->tcp_wq; 6368 6369 mp->b_datap->db_type = M_IOCACK; 6370 iocp->ioc_count = 0; 6371 mutex_enter(&tcp_g_q_lock); 6372 if (tcp_g_q != NULL) { 6373 mutex_exit(&tcp_g_q_lock); 6374 iocp->ioc_error = EALREADY; 6375 } else { 6376 mblk_t *mp1; 6377 6378 mp1 = tcp_ip_bind_mp(tcp, O_T_BIND_REQ, 0); 6379 if (mp1 == NULL) { 6380 mutex_exit(&tcp_g_q_lock); 6381 iocp->ioc_error = ENOMEM; 6382 } else { 6383 tcp_g_q = tcp->tcp_rq; 6384 mutex_exit(&tcp_g_q_lock); 6385 iocp->ioc_error = 0; 6386 iocp->ioc_rval = 0; 6387 /* 6388 * We are passing tcp_sticky_ipp as NULL 6389 * as it is not useful for tcp_default queue 6390 */ 6391 mp1 = ip_bind_v6(q, mp1, tcp->tcp_connp, NULL); 6392 if (mp1 != NULL) 6393 tcp_rput_other(tcp, mp1); 6394 } 6395 } 6396 qreply(q, mp); 6397 } 6398 6399 /* 6400 * Our client hereby directs us to reject the connection request 6401 * that tcp_conn_request() marked with 'seqnum'. Rejection consists 6402 * of sending the appropriate RST, not an ICMP error. 6403 */ 6404 static void 6405 tcp_disconnect(tcp_t *tcp, mblk_t *mp) 6406 { 6407 tcp_t *ltcp = NULL; 6408 t_scalar_t seqnum; 6409 conn_t *connp; 6410 6411 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 6412 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) { 6413 tcp_err_ack(tcp, mp, TPROTO, 0); 6414 return; 6415 } 6416 6417 /* 6418 * Right now, upper modules pass down a T_DISCON_REQ to TCP, 6419 * when the stream is in BOUND state. Do not send a reset, 6420 * since the destination IP address is not valid, and it can 6421 * be the initialized value of all zeros (broadcast address). 6422 * 6423 * If TCP has sent down a bind request to IP and has not 6424 * received the reply, reject the request. Otherwise, TCP 6425 * will be confused. 6426 */ 6427 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_hard_binding) { 6428 if (tcp->tcp_debug) { 6429 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 6430 "tcp_disconnect: bad state, %d", tcp->tcp_state); 6431 } 6432 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 6433 return; 6434 } 6435 6436 seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number; 6437 6438 if (seqnum == -1 || tcp->tcp_conn_req_max == 0) { 6439 6440 /* 6441 * According to TPI, for non-listeners, ignore seqnum 6442 * and disconnect. 6443 * Following interpretation of -1 seqnum is historical 6444 * and implied TPI ? (TPI only states that for T_CONN_IND, 6445 * a valid seqnum should not be -1). 6446 * 6447 * -1 means disconnect everything 6448 * regardless even on a listener. 6449 */ 6450 6451 int old_state = tcp->tcp_state; 6452 6453 /* 6454 * The connection can't be on the tcp_time_wait_head list 6455 * since it is not detached. 6456 */ 6457 ASSERT(tcp->tcp_time_wait_next == NULL); 6458 ASSERT(tcp->tcp_time_wait_prev == NULL); 6459 ASSERT(tcp->tcp_time_wait_expire == 0); 6460 ltcp = NULL; 6461 /* 6462 * If it used to be a listener, check to make sure no one else 6463 * has taken the port before switching back to LISTEN state. 6464 */ 6465 if (tcp->tcp_ipversion == IPV4_VERSION) { 6466 connp = ipcl_lookup_listener_v4(tcp->tcp_lport, 6467 tcp->tcp_ipha->ipha_src, 6468 tcp->tcp_connp->conn_zoneid); 6469 if (connp != NULL) 6470 ltcp = connp->conn_tcp; 6471 } else { 6472 /* Allow tcp_bound_if listeners? */ 6473 connp = ipcl_lookup_listener_v6(tcp->tcp_lport, 6474 &tcp->tcp_ip6h->ip6_src, 0, 6475 tcp->tcp_connp->conn_zoneid); 6476 if (connp != NULL) 6477 ltcp = connp->conn_tcp; 6478 } 6479 if (tcp->tcp_conn_req_max && ltcp == NULL) { 6480 tcp->tcp_state = TCPS_LISTEN; 6481 } else if (old_state > TCPS_BOUND) { 6482 tcp->tcp_conn_req_max = 0; 6483 tcp->tcp_state = TCPS_BOUND; 6484 } 6485 if (ltcp != NULL) 6486 CONN_DEC_REF(ltcp->tcp_connp); 6487 if (old_state == TCPS_SYN_SENT || old_state == TCPS_SYN_RCVD) { 6488 BUMP_MIB(&tcp_mib, tcpAttemptFails); 6489 } else if (old_state == TCPS_ESTABLISHED || 6490 old_state == TCPS_CLOSE_WAIT) { 6491 BUMP_MIB(&tcp_mib, tcpEstabResets); 6492 } 6493 6494 if (tcp->tcp_fused) 6495 tcp_unfuse(tcp); 6496 6497 mutex_enter(&tcp->tcp_eager_lock); 6498 if ((tcp->tcp_conn_req_cnt_q0 != 0) || 6499 (tcp->tcp_conn_req_cnt_q != 0)) { 6500 tcp_eager_cleanup(tcp, 0); 6501 } 6502 mutex_exit(&tcp->tcp_eager_lock); 6503 6504 tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt, 6505 tcp->tcp_rnxt, TH_RST | TH_ACK); 6506 6507 tcp_reinit(tcp); 6508 6509 if (old_state >= TCPS_ESTABLISHED) { 6510 /* Send M_FLUSH according to TPI */ 6511 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6512 } 6513 mp = mi_tpi_ok_ack_alloc(mp); 6514 if (mp) 6515 putnext(tcp->tcp_rq, mp); 6516 return; 6517 } else if (!tcp_eager_blowoff(tcp, seqnum)) { 6518 tcp_err_ack(tcp, mp, TBADSEQ, 0); 6519 return; 6520 } 6521 if (tcp->tcp_state >= TCPS_ESTABLISHED) { 6522 /* Send M_FLUSH according to TPI */ 6523 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 6524 } 6525 mp = mi_tpi_ok_ack_alloc(mp); 6526 if (mp) 6527 putnext(tcp->tcp_rq, mp); 6528 } 6529 6530 /* 6531 * Diagnostic routine used to return a string associated with the tcp state. 6532 * Note that if the caller does not supply a buffer, it will use an internal 6533 * static string. This means that if multiple threads call this function at 6534 * the same time, output can be corrupted... Note also that this function 6535 * does not check the size of the supplied buffer. The caller has to make 6536 * sure that it is big enough. 6537 */ 6538 static char * 6539 tcp_display(tcp_t *tcp, char *sup_buf, char format) 6540 { 6541 char buf1[30]; 6542 static char priv_buf[INET6_ADDRSTRLEN * 2 + 80]; 6543 char *buf; 6544 char *cp; 6545 in6_addr_t local, remote; 6546 char local_addrbuf[INET6_ADDRSTRLEN]; 6547 char remote_addrbuf[INET6_ADDRSTRLEN]; 6548 6549 if (sup_buf != NULL) 6550 buf = sup_buf; 6551 else 6552 buf = priv_buf; 6553 6554 if (tcp == NULL) 6555 return ("NULL_TCP"); 6556 switch (tcp->tcp_state) { 6557 case TCPS_CLOSED: 6558 cp = "TCP_CLOSED"; 6559 break; 6560 case TCPS_IDLE: 6561 cp = "TCP_IDLE"; 6562 break; 6563 case TCPS_BOUND: 6564 cp = "TCP_BOUND"; 6565 break; 6566 case TCPS_LISTEN: 6567 cp = "TCP_LISTEN"; 6568 break; 6569 case TCPS_SYN_SENT: 6570 cp = "TCP_SYN_SENT"; 6571 break; 6572 case TCPS_SYN_RCVD: 6573 cp = "TCP_SYN_RCVD"; 6574 break; 6575 case TCPS_ESTABLISHED: 6576 cp = "TCP_ESTABLISHED"; 6577 break; 6578 case TCPS_CLOSE_WAIT: 6579 cp = "TCP_CLOSE_WAIT"; 6580 break; 6581 case TCPS_FIN_WAIT_1: 6582 cp = "TCP_FIN_WAIT_1"; 6583 break; 6584 case TCPS_CLOSING: 6585 cp = "TCP_CLOSING"; 6586 break; 6587 case TCPS_LAST_ACK: 6588 cp = "TCP_LAST_ACK"; 6589 break; 6590 case TCPS_FIN_WAIT_2: 6591 cp = "TCP_FIN_WAIT_2"; 6592 break; 6593 case TCPS_TIME_WAIT: 6594 cp = "TCP_TIME_WAIT"; 6595 break; 6596 default: 6597 (void) mi_sprintf(buf1, "TCPUnkState(%d)", tcp->tcp_state); 6598 cp = buf1; 6599 break; 6600 } 6601 switch (format) { 6602 case DISP_ADDR_AND_PORT: 6603 if (tcp->tcp_ipversion == IPV4_VERSION) { 6604 /* 6605 * Note that we use the remote address in the tcp_b 6606 * structure. This means that it will print out 6607 * the real destination address, not the next hop's 6608 * address if source routing is used. 6609 */ 6610 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ip_src, &local); 6611 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &remote); 6612 6613 } else { 6614 local = tcp->tcp_ip_src_v6; 6615 remote = tcp->tcp_remote_v6; 6616 } 6617 (void) inet_ntop(AF_INET6, &local, local_addrbuf, 6618 sizeof (local_addrbuf)); 6619 (void) inet_ntop(AF_INET6, &remote, remote_addrbuf, 6620 sizeof (remote_addrbuf)); 6621 (void) mi_sprintf(buf, "[%s.%u, %s.%u] %s", 6622 local_addrbuf, ntohs(tcp->tcp_lport), remote_addrbuf, 6623 ntohs(tcp->tcp_fport), cp); 6624 break; 6625 case DISP_PORT_ONLY: 6626 default: 6627 (void) mi_sprintf(buf, "[%u, %u] %s", 6628 ntohs(tcp->tcp_lport), ntohs(tcp->tcp_fport), cp); 6629 break; 6630 } 6631 6632 return (buf); 6633 } 6634 6635 /* 6636 * Called via squeue to get on to eager's perimeter to send a 6637 * TH_RST. The listener wants the eager to disappear either 6638 * by means of tcp_eager_blowoff() or tcp_eager_cleanup() 6639 * being called. 6640 */ 6641 /* ARGSUSED */ 6642 void 6643 tcp_eager_kill(void *arg, mblk_t *mp, void *arg2) 6644 { 6645 conn_t *econnp = (conn_t *)arg; 6646 tcp_t *eager = econnp->conn_tcp; 6647 tcp_t *listener = eager->tcp_listener; 6648 6649 /* 6650 * We could be called because listener is closing. Since 6651 * the eager is using listener's queue's, its not safe. 6652 * Better use the default queue just to send the TH_RST 6653 * out. 6654 */ 6655 eager->tcp_rq = tcp_g_q; 6656 eager->tcp_wq = WR(tcp_g_q); 6657 6658 if (eager->tcp_state > TCPS_LISTEN) { 6659 tcp_xmit_ctl("tcp_eager_kill, can't wait", 6660 eager, eager->tcp_snxt, 0, TH_RST); 6661 } 6662 6663 /* We are here because listener wants this eager gone */ 6664 if (listener != NULL) { 6665 mutex_enter(&listener->tcp_eager_lock); 6666 tcp_eager_unlink(eager); 6667 if (eager->tcp_conn.tcp_eager_conn_ind == NULL) { 6668 /* 6669 * The eager has sent a conn_ind up to the 6670 * listener but listener decides to close 6671 * instead. We need to drop the extra ref 6672 * placed on eager in tcp_rput_data() before 6673 * sending the conn_ind to listener. 6674 */ 6675 CONN_DEC_REF(econnp); 6676 } 6677 mutex_exit(&listener->tcp_eager_lock); 6678 CONN_DEC_REF(listener->tcp_connp); 6679 } 6680 6681 if (eager->tcp_state > TCPS_BOUND) 6682 tcp_close_detached(eager); 6683 } 6684 6685 /* 6686 * Reset any eager connection hanging off this listener marked 6687 * with 'seqnum' and then reclaim it's resources. 6688 */ 6689 static boolean_t 6690 tcp_eager_blowoff(tcp_t *listener, t_scalar_t seqnum) 6691 { 6692 tcp_t *eager; 6693 mblk_t *mp; 6694 6695 TCP_STAT(tcp_eager_blowoff_calls); 6696 eager = listener; 6697 mutex_enter(&listener->tcp_eager_lock); 6698 do { 6699 eager = eager->tcp_eager_next_q; 6700 if (eager == NULL) { 6701 mutex_exit(&listener->tcp_eager_lock); 6702 return (B_FALSE); 6703 } 6704 } while (eager->tcp_conn_req_seqnum != seqnum); 6705 CONN_INC_REF(eager->tcp_connp); 6706 mutex_exit(&listener->tcp_eager_lock); 6707 mp = &eager->tcp_closemp; 6708 squeue_fill(eager->tcp_connp->conn_sqp, mp, tcp_eager_kill, 6709 eager->tcp_connp, SQTAG_TCP_EAGER_BLOWOFF); 6710 return (B_TRUE); 6711 } 6712 6713 /* 6714 * Reset any eager connection hanging off this listener 6715 * and then reclaim it's resources. 6716 */ 6717 static void 6718 tcp_eager_cleanup(tcp_t *listener, boolean_t q0_only) 6719 { 6720 tcp_t *eager; 6721 mblk_t *mp; 6722 6723 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6724 6725 if (!q0_only) { 6726 /* First cleanup q */ 6727 TCP_STAT(tcp_eager_blowoff_q); 6728 eager = listener->tcp_eager_next_q; 6729 while (eager != NULL) { 6730 CONN_INC_REF(eager->tcp_connp); 6731 mp = &eager->tcp_closemp; 6732 squeue_fill(eager->tcp_connp->conn_sqp, mp, 6733 tcp_eager_kill, eager->tcp_connp, 6734 SQTAG_TCP_EAGER_CLEANUP); 6735 eager = eager->tcp_eager_next_q; 6736 } 6737 } 6738 /* Then cleanup q0 */ 6739 TCP_STAT(tcp_eager_blowoff_q0); 6740 eager = listener->tcp_eager_next_q0; 6741 while (eager != listener) { 6742 CONN_INC_REF(eager->tcp_connp); 6743 mp = &eager->tcp_closemp; 6744 squeue_fill(eager->tcp_connp->conn_sqp, mp, 6745 tcp_eager_kill, eager->tcp_connp, 6746 SQTAG_TCP_EAGER_CLEANUP_Q0); 6747 eager = eager->tcp_eager_next_q0; 6748 } 6749 } 6750 6751 /* 6752 * If we are an eager connection hanging off a listener that hasn't 6753 * formally accepted the connection yet, get off his list and blow off 6754 * any data that we have accumulated. 6755 */ 6756 static void 6757 tcp_eager_unlink(tcp_t *tcp) 6758 { 6759 tcp_t *listener = tcp->tcp_listener; 6760 6761 ASSERT(MUTEX_HELD(&listener->tcp_eager_lock)); 6762 ASSERT(listener != NULL); 6763 if (tcp->tcp_eager_next_q0 != NULL) { 6764 ASSERT(tcp->tcp_eager_prev_q0 != NULL); 6765 6766 /* Remove the eager tcp from q0 */ 6767 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 6768 tcp->tcp_eager_prev_q0; 6769 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 6770 tcp->tcp_eager_next_q0; 6771 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 6772 listener->tcp_conn_req_cnt_q0--; 6773 6774 tcp->tcp_eager_next_q0 = NULL; 6775 tcp->tcp_eager_prev_q0 = NULL; 6776 6777 if (tcp->tcp_syn_rcvd_timeout != 0) { 6778 /* we have timed out before */ 6779 ASSERT(listener->tcp_syn_rcvd_timeout > 0); 6780 listener->tcp_syn_rcvd_timeout--; 6781 } 6782 } else { 6783 tcp_t **tcpp = &listener->tcp_eager_next_q; 6784 tcp_t *prev = NULL; 6785 6786 for (; tcpp[0]; tcpp = &tcpp[0]->tcp_eager_next_q) { 6787 if (tcpp[0] == tcp) { 6788 if (listener->tcp_eager_last_q == tcp) { 6789 /* 6790 * If we are unlinking the last 6791 * element on the list, adjust 6792 * tail pointer. Set tail pointer 6793 * to nil when list is empty. 6794 */ 6795 ASSERT(tcp->tcp_eager_next_q == NULL); 6796 if (listener->tcp_eager_last_q == 6797 listener->tcp_eager_next_q) { 6798 listener->tcp_eager_last_q = 6799 NULL; 6800 } else { 6801 /* 6802 * We won't get here if there 6803 * is only one eager in the 6804 * list. 6805 */ 6806 ASSERT(prev != NULL); 6807 listener->tcp_eager_last_q = 6808 prev; 6809 } 6810 } 6811 tcpp[0] = tcp->tcp_eager_next_q; 6812 tcp->tcp_eager_next_q = NULL; 6813 tcp->tcp_eager_last_q = NULL; 6814 ASSERT(listener->tcp_conn_req_cnt_q > 0); 6815 listener->tcp_conn_req_cnt_q--; 6816 break; 6817 } 6818 prev = tcpp[0]; 6819 } 6820 } 6821 tcp->tcp_listener = NULL; 6822 } 6823 6824 /* Shorthand to generate and send TPI error acks to our client */ 6825 static void 6826 tcp_err_ack(tcp_t *tcp, mblk_t *mp, int t_error, int sys_error) 6827 { 6828 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL) 6829 putnext(tcp->tcp_rq, mp); 6830 } 6831 6832 /* Shorthand to generate and send TPI error acks to our client */ 6833 static void 6834 tcp_err_ack_prim(tcp_t *tcp, mblk_t *mp, int primitive, 6835 int t_error, int sys_error) 6836 { 6837 struct T_error_ack *teackp; 6838 6839 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack), 6840 M_PCPROTO, T_ERROR_ACK)) != NULL) { 6841 teackp = (struct T_error_ack *)mp->b_rptr; 6842 teackp->ERROR_prim = primitive; 6843 teackp->TLI_error = t_error; 6844 teackp->UNIX_error = sys_error; 6845 putnext(tcp->tcp_rq, mp); 6846 } 6847 } 6848 6849 /* 6850 * Note: No locks are held when inspecting tcp_g_*epriv_ports 6851 * but instead the code relies on: 6852 * - the fact that the address of the array and its size never changes 6853 * - the atomic assignment of the elements of the array 6854 */ 6855 /* ARGSUSED */ 6856 static int 6857 tcp_extra_priv_ports_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 6858 { 6859 int i; 6860 6861 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 6862 if (tcp_g_epriv_ports[i] != 0) 6863 (void) mi_mpprintf(mp, "%d ", tcp_g_epriv_ports[i]); 6864 } 6865 return (0); 6866 } 6867 6868 /* 6869 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 6870 * threads from changing it at the same time. 6871 */ 6872 /* ARGSUSED */ 6873 static int 6874 tcp_extra_priv_ports_add(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 6875 cred_t *cr) 6876 { 6877 long new_value; 6878 int i; 6879 6880 /* 6881 * Fail the request if the new value does not lie within the 6882 * port number limits. 6883 */ 6884 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 6885 new_value <= 0 || new_value >= 65536) { 6886 return (EINVAL); 6887 } 6888 6889 mutex_enter(&tcp_epriv_port_lock); 6890 /* Check if the value is already in the list */ 6891 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 6892 if (new_value == tcp_g_epriv_ports[i]) { 6893 mutex_exit(&tcp_epriv_port_lock); 6894 return (EEXIST); 6895 } 6896 } 6897 /* Find an empty slot */ 6898 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 6899 if (tcp_g_epriv_ports[i] == 0) 6900 break; 6901 } 6902 if (i == tcp_g_num_epriv_ports) { 6903 mutex_exit(&tcp_epriv_port_lock); 6904 return (EOVERFLOW); 6905 } 6906 /* Set the new value */ 6907 tcp_g_epriv_ports[i] = (uint16_t)new_value; 6908 mutex_exit(&tcp_epriv_port_lock); 6909 return (0); 6910 } 6911 6912 /* 6913 * Hold a lock while changing tcp_g_epriv_ports to prevent multiple 6914 * threads from changing it at the same time. 6915 */ 6916 /* ARGSUSED */ 6917 static int 6918 tcp_extra_priv_ports_del(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 6919 cred_t *cr) 6920 { 6921 long new_value; 6922 int i; 6923 6924 /* 6925 * Fail the request if the new value does not lie within the 6926 * port number limits. 6927 */ 6928 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || new_value <= 0 || 6929 new_value >= 65536) { 6930 return (EINVAL); 6931 } 6932 6933 mutex_enter(&tcp_epriv_port_lock); 6934 /* Check that the value is already in the list */ 6935 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 6936 if (tcp_g_epriv_ports[i] == new_value) 6937 break; 6938 } 6939 if (i == tcp_g_num_epriv_ports) { 6940 mutex_exit(&tcp_epriv_port_lock); 6941 return (ESRCH); 6942 } 6943 /* Clear the value */ 6944 tcp_g_epriv_ports[i] = 0; 6945 mutex_exit(&tcp_epriv_port_lock); 6946 return (0); 6947 } 6948 6949 /* Return the TPI/TLI equivalent of our current tcp_state */ 6950 static int 6951 tcp_tpistate(tcp_t *tcp) 6952 { 6953 switch (tcp->tcp_state) { 6954 case TCPS_IDLE: 6955 return (TS_UNBND); 6956 case TCPS_LISTEN: 6957 /* 6958 * Return whether there are outstanding T_CONN_IND waiting 6959 * for the matching T_CONN_RES. Therefore don't count q0. 6960 */ 6961 if (tcp->tcp_conn_req_cnt_q > 0) 6962 return (TS_WRES_CIND); 6963 else 6964 return (TS_IDLE); 6965 case TCPS_BOUND: 6966 return (TS_IDLE); 6967 case TCPS_SYN_SENT: 6968 return (TS_WCON_CREQ); 6969 case TCPS_SYN_RCVD: 6970 /* 6971 * Note: assumption: this has to the active open SYN_RCVD. 6972 * The passive instance is detached in SYN_RCVD stage of 6973 * incoming connection processing so we cannot get request 6974 * for T_info_ack on it. 6975 */ 6976 return (TS_WACK_CRES); 6977 case TCPS_ESTABLISHED: 6978 return (TS_DATA_XFER); 6979 case TCPS_CLOSE_WAIT: 6980 return (TS_WREQ_ORDREL); 6981 case TCPS_FIN_WAIT_1: 6982 return (TS_WIND_ORDREL); 6983 case TCPS_FIN_WAIT_2: 6984 return (TS_WIND_ORDREL); 6985 6986 case TCPS_CLOSING: 6987 case TCPS_LAST_ACK: 6988 case TCPS_TIME_WAIT: 6989 case TCPS_CLOSED: 6990 /* 6991 * Following TS_WACK_DREQ7 is a rendition of "not 6992 * yet TS_IDLE" TPI state. There is no best match to any 6993 * TPI state for TCPS_{CLOSING, LAST_ACK, TIME_WAIT} but we 6994 * choose a value chosen that will map to TLI/XTI level 6995 * state of TSTATECHNG (state is process of changing) which 6996 * captures what this dummy state represents. 6997 */ 6998 return (TS_WACK_DREQ7); 6999 default: 7000 cmn_err(CE_WARN, "tcp_tpistate: strange state (%d) %s", 7001 tcp->tcp_state, tcp_display(tcp, NULL, 7002 DISP_PORT_ONLY)); 7003 return (TS_UNBND); 7004 } 7005 } 7006 7007 static void 7008 tcp_copy_info(struct T_info_ack *tia, tcp_t *tcp) 7009 { 7010 if (tcp->tcp_family == AF_INET6) 7011 *tia = tcp_g_t_info_ack_v6; 7012 else 7013 *tia = tcp_g_t_info_ack; 7014 tia->CURRENT_state = tcp_tpistate(tcp); 7015 tia->OPT_size = tcp_max_optsize; 7016 if (tcp->tcp_mss == 0) { 7017 /* Not yet set - tcp_open does not set mss */ 7018 if (tcp->tcp_ipversion == IPV4_VERSION) 7019 tia->TIDU_size = tcp_mss_def_ipv4; 7020 else 7021 tia->TIDU_size = tcp_mss_def_ipv6; 7022 } else { 7023 tia->TIDU_size = tcp->tcp_mss; 7024 } 7025 /* TODO: Default ETSDU is 1. Is that correct for tcp? */ 7026 } 7027 7028 /* 7029 * This routine responds to T_CAPABILITY_REQ messages. It is called by 7030 * tcp_wput. Much of the T_CAPABILITY_ACK information is copied from 7031 * tcp_g_t_info_ack. The current state of the stream is copied from 7032 * tcp_state. 7033 */ 7034 static void 7035 tcp_capability_req(tcp_t *tcp, mblk_t *mp) 7036 { 7037 t_uscalar_t cap_bits1; 7038 struct T_capability_ack *tcap; 7039 7040 if (MBLKL(mp) < sizeof (struct T_capability_req)) { 7041 freemsg(mp); 7042 return; 7043 } 7044 7045 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1; 7046 7047 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack), 7048 mp->b_datap->db_type, T_CAPABILITY_ACK); 7049 if (mp == NULL) 7050 return; 7051 7052 tcap = (struct T_capability_ack *)mp->b_rptr; 7053 tcap->CAP_bits1 = 0; 7054 7055 if (cap_bits1 & TC1_INFO) { 7056 tcp_copy_info(&tcap->INFO_ack, tcp); 7057 tcap->CAP_bits1 |= TC1_INFO; 7058 } 7059 7060 if (cap_bits1 & TC1_ACCEPTOR_ID) { 7061 tcap->ACCEPTOR_id = tcp->tcp_acceptor_id; 7062 tcap->CAP_bits1 |= TC1_ACCEPTOR_ID; 7063 } 7064 7065 putnext(tcp->tcp_rq, mp); 7066 } 7067 7068 /* 7069 * This routine responds to T_INFO_REQ messages. It is called by tcp_wput. 7070 * Most of the T_INFO_ACK information is copied from tcp_g_t_info_ack. 7071 * The current state of the stream is copied from tcp_state. 7072 */ 7073 static void 7074 tcp_info_req(tcp_t *tcp, mblk_t *mp) 7075 { 7076 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO, 7077 T_INFO_ACK); 7078 if (!mp) { 7079 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7080 return; 7081 } 7082 tcp_copy_info((struct T_info_ack *)mp->b_rptr, tcp); 7083 putnext(tcp->tcp_rq, mp); 7084 } 7085 7086 /* Respond to the TPI addr request */ 7087 static void 7088 tcp_addr_req(tcp_t *tcp, mblk_t *mp) 7089 { 7090 sin_t *sin; 7091 mblk_t *ackmp; 7092 struct T_addr_ack *taa; 7093 7094 /* Make it large enough for worst case */ 7095 ackmp = reallocb(mp, sizeof (struct T_addr_ack) + 7096 2 * sizeof (sin6_t), 1); 7097 if (ackmp == NULL) { 7098 tcp_err_ack(tcp, mp, TSYSERR, ENOMEM); 7099 return; 7100 } 7101 7102 if (tcp->tcp_ipversion == IPV6_VERSION) { 7103 tcp_addr_req_ipv6(tcp, ackmp); 7104 return; 7105 } 7106 taa = (struct T_addr_ack *)ackmp->b_rptr; 7107 7108 bzero(taa, sizeof (struct T_addr_ack)); 7109 ackmp->b_wptr = (uchar_t *)&taa[1]; 7110 7111 taa->PRIM_type = T_ADDR_ACK; 7112 ackmp->b_datap->db_type = M_PCPROTO; 7113 7114 /* 7115 * Note: Following code assumes 32 bit alignment of basic 7116 * data structures like sin_t and struct T_addr_ack. 7117 */ 7118 if (tcp->tcp_state >= TCPS_BOUND) { 7119 /* 7120 * Fill in local address 7121 */ 7122 taa->LOCADDR_length = sizeof (sin_t); 7123 taa->LOCADDR_offset = sizeof (*taa); 7124 7125 sin = (sin_t *)&taa[1]; 7126 7127 /* Fill zeroes and then intialize non-zero fields */ 7128 *sin = sin_null; 7129 7130 sin->sin_family = AF_INET; 7131 7132 sin->sin_addr.s_addr = tcp->tcp_ipha->ipha_src; 7133 sin->sin_port = *(uint16_t *)tcp->tcp_tcph->th_lport; 7134 7135 ackmp->b_wptr = (uchar_t *)&sin[1]; 7136 7137 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7138 /* 7139 * Fill in Remote address 7140 */ 7141 taa->REMADDR_length = sizeof (sin_t); 7142 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7143 taa->LOCADDR_length); 7144 7145 sin = (sin_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7146 *sin = sin_null; 7147 sin->sin_family = AF_INET; 7148 sin->sin_addr.s_addr = tcp->tcp_remote; 7149 sin->sin_port = tcp->tcp_fport; 7150 7151 ackmp->b_wptr = (uchar_t *)&sin[1]; 7152 } 7153 } 7154 putnext(tcp->tcp_rq, ackmp); 7155 } 7156 7157 /* Assumes that tcp_addr_req gets enough space and alignment */ 7158 static void 7159 tcp_addr_req_ipv6(tcp_t *tcp, mblk_t *ackmp) 7160 { 7161 sin6_t *sin6; 7162 struct T_addr_ack *taa; 7163 7164 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 7165 ASSERT(OK_32PTR(ackmp->b_rptr)); 7166 ASSERT(ackmp->b_wptr - ackmp->b_rptr >= sizeof (struct T_addr_ack) + 7167 2 * sizeof (sin6_t)); 7168 7169 taa = (struct T_addr_ack *)ackmp->b_rptr; 7170 7171 bzero(taa, sizeof (struct T_addr_ack)); 7172 ackmp->b_wptr = (uchar_t *)&taa[1]; 7173 7174 taa->PRIM_type = T_ADDR_ACK; 7175 ackmp->b_datap->db_type = M_PCPROTO; 7176 7177 /* 7178 * Note: Following code assumes 32 bit alignment of basic 7179 * data structures like sin6_t and struct T_addr_ack. 7180 */ 7181 if (tcp->tcp_state >= TCPS_BOUND) { 7182 /* 7183 * Fill in local address 7184 */ 7185 taa->LOCADDR_length = sizeof (sin6_t); 7186 taa->LOCADDR_offset = sizeof (*taa); 7187 7188 sin6 = (sin6_t *)&taa[1]; 7189 *sin6 = sin6_null; 7190 7191 sin6->sin6_family = AF_INET6; 7192 sin6->sin6_addr = tcp->tcp_ip6h->ip6_src; 7193 sin6->sin6_port = tcp->tcp_lport; 7194 7195 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7196 7197 if (tcp->tcp_state >= TCPS_SYN_RCVD) { 7198 /* 7199 * Fill in Remote address 7200 */ 7201 taa->REMADDR_length = sizeof (sin6_t); 7202 taa->REMADDR_offset = ROUNDUP32(taa->LOCADDR_offset + 7203 taa->LOCADDR_length); 7204 7205 sin6 = (sin6_t *)(ackmp->b_rptr + taa->REMADDR_offset); 7206 *sin6 = sin6_null; 7207 sin6->sin6_family = AF_INET6; 7208 sin6->sin6_flowinfo = 7209 tcp->tcp_ip6h->ip6_vcf & 7210 ~IPV6_VERS_AND_FLOW_MASK; 7211 sin6->sin6_addr = tcp->tcp_remote_v6; 7212 sin6->sin6_port = tcp->tcp_fport; 7213 7214 ackmp->b_wptr = (uchar_t *)&sin6[1]; 7215 } 7216 } 7217 putnext(tcp->tcp_rq, ackmp); 7218 } 7219 7220 /* 7221 * Handle reinitialization of a tcp structure. 7222 * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. 7223 */ 7224 static void 7225 tcp_reinit(tcp_t *tcp) 7226 { 7227 mblk_t *mp; 7228 int err; 7229 7230 TCP_STAT(tcp_reinit_calls); 7231 7232 /* tcp_reinit should never be called for detached tcp_t's */ 7233 ASSERT(tcp->tcp_listener == NULL); 7234 ASSERT((tcp->tcp_family == AF_INET && 7235 tcp->tcp_ipversion == IPV4_VERSION) || 7236 (tcp->tcp_family == AF_INET6 && 7237 (tcp->tcp_ipversion == IPV4_VERSION || 7238 tcp->tcp_ipversion == IPV6_VERSION))); 7239 7240 /* Cancel outstanding timers */ 7241 tcp_timers_stop(tcp); 7242 7243 /* 7244 * Reset everything in the state vector, after updating global 7245 * MIB data from instance counters. 7246 */ 7247 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 7248 tcp->tcp_ibsegs = 0; 7249 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 7250 tcp->tcp_obsegs = 0; 7251 7252 tcp_close_mpp(&tcp->tcp_xmit_head); 7253 if (tcp->tcp_snd_zcopy_aware) 7254 tcp_zcopy_notify(tcp); 7255 tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL; 7256 tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0; 7257 if (tcp->tcp_flow_stopped && 7258 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 7259 tcp_clrqfull(tcp); 7260 } 7261 tcp_close_mpp(&tcp->tcp_reass_head); 7262 tcp->tcp_reass_tail = NULL; 7263 if (tcp->tcp_rcv_list != NULL) { 7264 /* Free b_next chain */ 7265 tcp_close_mpp(&tcp->tcp_rcv_list); 7266 tcp->tcp_rcv_last_head = NULL; 7267 tcp->tcp_rcv_last_tail = NULL; 7268 tcp->tcp_rcv_cnt = 0; 7269 } 7270 tcp->tcp_rcv_last_tail = NULL; 7271 7272 if ((mp = tcp->tcp_urp_mp) != NULL) { 7273 freemsg(mp); 7274 tcp->tcp_urp_mp = NULL; 7275 } 7276 if ((mp = tcp->tcp_urp_mark_mp) != NULL) { 7277 freemsg(mp); 7278 tcp->tcp_urp_mark_mp = NULL; 7279 } 7280 if (tcp->tcp_fused_sigurg_mp != NULL) { 7281 freeb(tcp->tcp_fused_sigurg_mp); 7282 tcp->tcp_fused_sigurg_mp = NULL; 7283 } 7284 7285 /* 7286 * Following is a union with two members which are 7287 * identical types and size so the following cleanup 7288 * is enough. 7289 */ 7290 tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind); 7291 7292 CL_INET_DISCONNECT(tcp); 7293 7294 /* 7295 * The connection can't be on the tcp_time_wait_head list 7296 * since it is not detached. 7297 */ 7298 ASSERT(tcp->tcp_time_wait_next == NULL); 7299 ASSERT(tcp->tcp_time_wait_prev == NULL); 7300 ASSERT(tcp->tcp_time_wait_expire == 0); 7301 7302 if (tcp->tcp_kssl_pending) { 7303 tcp->tcp_kssl_pending = B_FALSE; 7304 7305 /* Don't reset if the initialized by bind. */ 7306 if (tcp->tcp_kssl_ent != NULL) { 7307 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 7308 KSSL_NO_PROXY); 7309 } 7310 } 7311 if (tcp->tcp_kssl_ctx != NULL) { 7312 kssl_release_ctx(tcp->tcp_kssl_ctx); 7313 tcp->tcp_kssl_ctx = NULL; 7314 } 7315 7316 /* 7317 * Reset/preserve other values 7318 */ 7319 tcp_reinit_values(tcp); 7320 ipcl_hash_remove(tcp->tcp_connp); 7321 conn_delete_ire(tcp->tcp_connp, NULL); 7322 7323 if (tcp->tcp_conn_req_max != 0) { 7324 /* 7325 * This is the case when a TLI program uses the same 7326 * transport end point to accept a connection. This 7327 * makes the TCP both a listener and acceptor. When 7328 * this connection is closed, we need to set the state 7329 * back to TCPS_LISTEN. Make sure that the eager list 7330 * is reinitialized. 7331 * 7332 * Note that this stream is still bound to the four 7333 * tuples of the previous connection in IP. If a new 7334 * SYN with different foreign address comes in, IP will 7335 * not find it and will send it to the global queue. In 7336 * the global queue, TCP will do a tcp_lookup_listener() 7337 * to find this stream. This works because this stream 7338 * is only removed from connected hash. 7339 * 7340 */ 7341 tcp->tcp_state = TCPS_LISTEN; 7342 tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp; 7343 tcp->tcp_connp->conn_recv = tcp_conn_request; 7344 if (tcp->tcp_family == AF_INET6) { 7345 ASSERT(tcp->tcp_connp->conn_af_isv6); 7346 (void) ipcl_bind_insert_v6(tcp->tcp_connp, IPPROTO_TCP, 7347 &tcp->tcp_ip6h->ip6_src, tcp->tcp_lport); 7348 } else { 7349 ASSERT(!tcp->tcp_connp->conn_af_isv6); 7350 (void) ipcl_bind_insert(tcp->tcp_connp, IPPROTO_TCP, 7351 tcp->tcp_ipha->ipha_src, tcp->tcp_lport); 7352 } 7353 } else { 7354 tcp->tcp_state = TCPS_BOUND; 7355 } 7356 7357 /* 7358 * Initialize to default values 7359 * Can't fail since enough header template space already allocated 7360 * at open(). 7361 */ 7362 err = tcp_init_values(tcp); 7363 ASSERT(err == 0); 7364 /* Restore state in tcp_tcph */ 7365 bcopy(&tcp->tcp_lport, tcp->tcp_tcph->th_lport, TCP_PORT_LEN); 7366 if (tcp->tcp_ipversion == IPV4_VERSION) 7367 tcp->tcp_ipha->ipha_src = tcp->tcp_bound_source; 7368 else 7369 tcp->tcp_ip6h->ip6_src = tcp->tcp_bound_source_v6; 7370 /* 7371 * Copy of the src addr. in tcp_t is needed in tcp_t 7372 * since the lookup funcs can only lookup on tcp_t 7373 */ 7374 tcp->tcp_ip_src_v6 = tcp->tcp_bound_source_v6; 7375 7376 ASSERT(tcp->tcp_ptpbhn != NULL); 7377 tcp->tcp_rq->q_hiwat = tcp_recv_hiwat; 7378 tcp->tcp_rwnd = tcp_recv_hiwat; 7379 tcp->tcp_mss = tcp->tcp_ipversion != IPV4_VERSION ? 7380 tcp_mss_def_ipv6 : tcp_mss_def_ipv4; 7381 } 7382 7383 /* 7384 * Force values to zero that need be zero. 7385 * Do not touch values asociated with the BOUND or LISTEN state 7386 * since the connection will end up in that state after the reinit. 7387 * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t 7388 * structure! 7389 */ 7390 static void 7391 tcp_reinit_values(tcp) 7392 tcp_t *tcp; 7393 { 7394 #ifndef lint 7395 #define DONTCARE(x) 7396 #define PRESERVE(x) 7397 #else 7398 #define DONTCARE(x) ((x) = (x)) 7399 #define PRESERVE(x) ((x) = (x)) 7400 #endif /* lint */ 7401 7402 PRESERVE(tcp->tcp_bind_hash); 7403 PRESERVE(tcp->tcp_ptpbhn); 7404 PRESERVE(tcp->tcp_acceptor_hash); 7405 PRESERVE(tcp->tcp_ptpahn); 7406 7407 /* Should be ASSERT NULL on these with new code! */ 7408 ASSERT(tcp->tcp_time_wait_next == NULL); 7409 ASSERT(tcp->tcp_time_wait_prev == NULL); 7410 ASSERT(tcp->tcp_time_wait_expire == 0); 7411 PRESERVE(tcp->tcp_state); 7412 PRESERVE(tcp->tcp_rq); 7413 PRESERVE(tcp->tcp_wq); 7414 7415 ASSERT(tcp->tcp_xmit_head == NULL); 7416 ASSERT(tcp->tcp_xmit_last == NULL); 7417 ASSERT(tcp->tcp_unsent == 0); 7418 ASSERT(tcp->tcp_xmit_tail == NULL); 7419 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 7420 7421 tcp->tcp_snxt = 0; /* Displayed in mib */ 7422 tcp->tcp_suna = 0; /* Displayed in mib */ 7423 tcp->tcp_swnd = 0; 7424 DONTCARE(tcp->tcp_cwnd); /* Init in tcp_mss_set */ 7425 7426 ASSERT(tcp->tcp_ibsegs == 0); 7427 ASSERT(tcp->tcp_obsegs == 0); 7428 7429 if (tcp->tcp_iphc != NULL) { 7430 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7431 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 7432 } 7433 7434 DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */ 7435 DONTCARE(tcp->tcp_hdr_len); /* Init in tcp_init_values */ 7436 DONTCARE(tcp->tcp_ipha); 7437 DONTCARE(tcp->tcp_ip6h); 7438 DONTCARE(tcp->tcp_ip_hdr_len); 7439 DONTCARE(tcp->tcp_tcph); 7440 DONTCARE(tcp->tcp_tcp_hdr_len); /* Init in tcp_init_values */ 7441 tcp->tcp_valid_bits = 0; 7442 7443 DONTCARE(tcp->tcp_xmit_hiwater); /* Init in tcp_init_values */ 7444 DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */ 7445 DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */ 7446 tcp->tcp_last_rcv_lbolt = 0; 7447 7448 tcp->tcp_init_cwnd = 0; 7449 7450 tcp->tcp_urp_last_valid = 0; 7451 tcp->tcp_hard_binding = 0; 7452 tcp->tcp_hard_bound = 0; 7453 PRESERVE(tcp->tcp_cred); 7454 PRESERVE(tcp->tcp_cpid); 7455 PRESERVE(tcp->tcp_exclbind); 7456 7457 tcp->tcp_fin_acked = 0; 7458 tcp->tcp_fin_rcvd = 0; 7459 tcp->tcp_fin_sent = 0; 7460 tcp->tcp_ordrel_done = 0; 7461 7462 tcp->tcp_debug = 0; 7463 tcp->tcp_dontroute = 0; 7464 tcp->tcp_broadcast = 0; 7465 7466 tcp->tcp_useloopback = 0; 7467 tcp->tcp_reuseaddr = 0; 7468 tcp->tcp_oobinline = 0; 7469 tcp->tcp_dgram_errind = 0; 7470 7471 tcp->tcp_detached = 0; 7472 tcp->tcp_bind_pending = 0; 7473 tcp->tcp_unbind_pending = 0; 7474 tcp->tcp_deferred_clean_death = 0; 7475 7476 tcp->tcp_snd_ws_ok = B_FALSE; 7477 tcp->tcp_snd_ts_ok = B_FALSE; 7478 tcp->tcp_linger = 0; 7479 tcp->tcp_ka_enabled = 0; 7480 tcp->tcp_zero_win_probe = 0; 7481 7482 tcp->tcp_loopback = 0; 7483 tcp->tcp_localnet = 0; 7484 tcp->tcp_syn_defense = 0; 7485 tcp->tcp_set_timer = 0; 7486 7487 tcp->tcp_active_open = 0; 7488 ASSERT(tcp->tcp_timeout == B_FALSE); 7489 tcp->tcp_rexmit = B_FALSE; 7490 tcp->tcp_xmit_zc_clean = B_FALSE; 7491 7492 tcp->tcp_snd_sack_ok = B_FALSE; 7493 PRESERVE(tcp->tcp_recvdstaddr); 7494 tcp->tcp_hwcksum = B_FALSE; 7495 7496 tcp->tcp_ire_ill_check_done = B_FALSE; 7497 DONTCARE(tcp->tcp_maxpsz); /* Init in tcp_init_values */ 7498 7499 tcp->tcp_mdt = B_FALSE; 7500 tcp->tcp_mdt_hdr_head = 0; 7501 tcp->tcp_mdt_hdr_tail = 0; 7502 7503 tcp->tcp_conn_def_q0 = 0; 7504 tcp->tcp_ip_forward_progress = B_FALSE; 7505 tcp->tcp_anon_priv_bind = 0; 7506 tcp->tcp_ecn_ok = B_FALSE; 7507 7508 tcp->tcp_cwr = B_FALSE; 7509 tcp->tcp_ecn_echo_on = B_FALSE; 7510 7511 if (tcp->tcp_sack_info != NULL) { 7512 if (tcp->tcp_notsack_list != NULL) { 7513 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 7514 } 7515 kmem_cache_free(tcp_sack_info_cache, tcp->tcp_sack_info); 7516 tcp->tcp_sack_info = NULL; 7517 } 7518 7519 tcp->tcp_rcv_ws = 0; 7520 tcp->tcp_snd_ws = 0; 7521 tcp->tcp_ts_recent = 0; 7522 tcp->tcp_rnxt = 0; /* Displayed in mib */ 7523 DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */ 7524 tcp->tcp_if_mtu = 0; 7525 7526 ASSERT(tcp->tcp_reass_head == NULL); 7527 ASSERT(tcp->tcp_reass_tail == NULL); 7528 7529 tcp->tcp_cwnd_cnt = 0; 7530 7531 ASSERT(tcp->tcp_rcv_list == NULL); 7532 ASSERT(tcp->tcp_rcv_last_head == NULL); 7533 ASSERT(tcp->tcp_rcv_last_tail == NULL); 7534 ASSERT(tcp->tcp_rcv_cnt == 0); 7535 7536 DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_adapt_ire */ 7537 DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */ 7538 tcp->tcp_csuna = 0; 7539 7540 tcp->tcp_rto = 0; /* Displayed in MIB */ 7541 DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */ 7542 DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */ 7543 tcp->tcp_rtt_update = 0; 7544 7545 DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7546 DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */ 7547 7548 tcp->tcp_rack = 0; /* Displayed in mib */ 7549 tcp->tcp_rack_cnt = 0; 7550 tcp->tcp_rack_cur_max = 0; 7551 tcp->tcp_rack_abs_max = 0; 7552 7553 tcp->tcp_max_swnd = 0; 7554 7555 ASSERT(tcp->tcp_listener == NULL); 7556 7557 DONTCARE(tcp->tcp_xmit_lowater); /* Init in tcp_init_values */ 7558 7559 DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */ 7560 DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */ 7561 DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */ 7562 DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */ 7563 7564 ASSERT(tcp->tcp_conn_req_cnt_q == 0); 7565 ASSERT(tcp->tcp_conn_req_cnt_q0 == 0); 7566 PRESERVE(tcp->tcp_conn_req_max); 7567 PRESERVE(tcp->tcp_conn_req_seqnum); 7568 7569 DONTCARE(tcp->tcp_ip_hdr_len); /* Init in tcp_init_values */ 7570 DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */ 7571 DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */ 7572 DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */ 7573 DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */ 7574 7575 tcp->tcp_lingertime = 0; 7576 7577 DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */ 7578 ASSERT(tcp->tcp_urp_mp == NULL); 7579 ASSERT(tcp->tcp_urp_mark_mp == NULL); 7580 ASSERT(tcp->tcp_fused_sigurg_mp == NULL); 7581 7582 ASSERT(tcp->tcp_eager_next_q == NULL); 7583 ASSERT(tcp->tcp_eager_last_q == NULL); 7584 ASSERT((tcp->tcp_eager_next_q0 == NULL && 7585 tcp->tcp_eager_prev_q0 == NULL) || 7586 tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0); 7587 ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL); 7588 7589 tcp->tcp_client_errno = 0; 7590 7591 DONTCARE(tcp->tcp_sum); /* Init in tcp_init_values */ 7592 7593 tcp->tcp_remote_v6 = ipv6_all_zeros; /* Displayed in MIB */ 7594 7595 PRESERVE(tcp->tcp_bound_source_v6); 7596 tcp->tcp_last_sent_len = 0; 7597 tcp->tcp_dupack_cnt = 0; 7598 7599 tcp->tcp_fport = 0; /* Displayed in MIB */ 7600 PRESERVE(tcp->tcp_lport); 7601 7602 PRESERVE(tcp->tcp_acceptor_lockp); 7603 7604 ASSERT(tcp->tcp_ordrelid == 0); 7605 PRESERVE(tcp->tcp_acceptor_id); 7606 DONTCARE(tcp->tcp_ipsec_overhead); 7607 7608 /* 7609 * If tcp_tracing flag is ON (i.e. We have a trace buffer 7610 * in tcp structure and now tracing), Re-initialize all 7611 * members of tcp_traceinfo. 7612 */ 7613 if (tcp->tcp_tracebuf != NULL) { 7614 bzero(tcp->tcp_tracebuf, sizeof (tcptrch_t)); 7615 } 7616 7617 PRESERVE(tcp->tcp_family); 7618 if (tcp->tcp_family == AF_INET6) { 7619 tcp->tcp_ipversion = IPV6_VERSION; 7620 tcp->tcp_mss = tcp_mss_def_ipv6; 7621 } else { 7622 tcp->tcp_ipversion = IPV4_VERSION; 7623 tcp->tcp_mss = tcp_mss_def_ipv4; 7624 } 7625 7626 tcp->tcp_bound_if = 0; 7627 tcp->tcp_ipv6_recvancillary = 0; 7628 tcp->tcp_recvifindex = 0; 7629 tcp->tcp_recvhops = 0; 7630 tcp->tcp_closed = 0; 7631 tcp->tcp_cleandeathtag = 0; 7632 if (tcp->tcp_hopopts != NULL) { 7633 mi_free(tcp->tcp_hopopts); 7634 tcp->tcp_hopopts = NULL; 7635 tcp->tcp_hopoptslen = 0; 7636 } 7637 ASSERT(tcp->tcp_hopoptslen == 0); 7638 if (tcp->tcp_dstopts != NULL) { 7639 mi_free(tcp->tcp_dstopts); 7640 tcp->tcp_dstopts = NULL; 7641 tcp->tcp_dstoptslen = 0; 7642 } 7643 ASSERT(tcp->tcp_dstoptslen == 0); 7644 if (tcp->tcp_rtdstopts != NULL) { 7645 mi_free(tcp->tcp_rtdstopts); 7646 tcp->tcp_rtdstopts = NULL; 7647 tcp->tcp_rtdstoptslen = 0; 7648 } 7649 ASSERT(tcp->tcp_rtdstoptslen == 0); 7650 if (tcp->tcp_rthdr != NULL) { 7651 mi_free(tcp->tcp_rthdr); 7652 tcp->tcp_rthdr = NULL; 7653 tcp->tcp_rthdrlen = 0; 7654 } 7655 ASSERT(tcp->tcp_rthdrlen == 0); 7656 PRESERVE(tcp->tcp_drop_opt_ack_cnt); 7657 7658 /* Reset fusion-related fields */ 7659 tcp->tcp_fused = B_FALSE; 7660 tcp->tcp_unfusable = B_FALSE; 7661 tcp->tcp_fused_sigurg = B_FALSE; 7662 tcp->tcp_direct_sockfs = B_FALSE; 7663 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7664 tcp->tcp_loopback_peer = NULL; 7665 tcp->tcp_fuse_rcv_hiwater = 0; 7666 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7667 tcp->tcp_fuse_rcv_unread_cnt = 0; 7668 7669 tcp->tcp_in_ack_unsent = 0; 7670 tcp->tcp_cork = B_FALSE; 7671 7672 PRESERVE(tcp->tcp_squeue_bytes); 7673 7674 ASSERT(tcp->tcp_kssl_ctx == NULL); 7675 ASSERT(!tcp->tcp_kssl_pending); 7676 PRESERVE(tcp->tcp_kssl_ent); 7677 7678 #undef DONTCARE 7679 #undef PRESERVE 7680 } 7681 7682 /* 7683 * Allocate necessary resources and initialize state vector. 7684 * Guaranteed not to fail so that when an error is returned, 7685 * the caller doesn't need to do any additional cleanup. 7686 */ 7687 int 7688 tcp_init(tcp_t *tcp, queue_t *q) 7689 { 7690 int err; 7691 7692 tcp->tcp_rq = q; 7693 tcp->tcp_wq = WR(q); 7694 tcp->tcp_state = TCPS_IDLE; 7695 if ((err = tcp_init_values(tcp)) != 0) 7696 tcp_timers_stop(tcp); 7697 return (err); 7698 } 7699 7700 static int 7701 tcp_init_values(tcp_t *tcp) 7702 { 7703 int err; 7704 7705 ASSERT((tcp->tcp_family == AF_INET && 7706 tcp->tcp_ipversion == IPV4_VERSION) || 7707 (tcp->tcp_family == AF_INET6 && 7708 (tcp->tcp_ipversion == IPV4_VERSION || 7709 tcp->tcp_ipversion == IPV6_VERSION))); 7710 7711 /* 7712 * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO 7713 * will be close to tcp_rexmit_interval_initial. By doing this, we 7714 * allow the algorithm to adjust slowly to large fluctuations of RTT 7715 * during first few transmissions of a connection as seen in slow 7716 * links. 7717 */ 7718 tcp->tcp_rtt_sa = tcp_rexmit_interval_initial << 2; 7719 tcp->tcp_rtt_sd = tcp_rexmit_interval_initial >> 1; 7720 tcp->tcp_rto = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 7721 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5) + 7722 tcp_conn_grace_period; 7723 if (tcp->tcp_rto < tcp_rexmit_interval_min) 7724 tcp->tcp_rto = tcp_rexmit_interval_min; 7725 tcp->tcp_timer_backoff = 0; 7726 tcp->tcp_ms_we_have_waited = 0; 7727 tcp->tcp_last_recv_time = lbolt; 7728 tcp->tcp_cwnd_max = tcp_cwnd_max_; 7729 tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN; 7730 tcp->tcp_snd_burst = TCP_CWND_INFINITE; 7731 7732 tcp->tcp_maxpsz = tcp_maxpsz_multiplier; 7733 7734 tcp->tcp_first_timer_threshold = tcp_ip_notify_interval; 7735 tcp->tcp_first_ctimer_threshold = tcp_ip_notify_cinterval; 7736 tcp->tcp_second_timer_threshold = tcp_ip_abort_interval; 7737 /* 7738 * Fix it to tcp_ip_abort_linterval later if it turns out to be a 7739 * passive open. 7740 */ 7741 tcp->tcp_second_ctimer_threshold = tcp_ip_abort_cinterval; 7742 7743 tcp->tcp_naglim = tcp_naglim_def; 7744 7745 /* NOTE: ISS is now set in tcp_adapt_ire(). */ 7746 7747 tcp->tcp_mdt_hdr_head = 0; 7748 tcp->tcp_mdt_hdr_tail = 0; 7749 7750 /* Reset fusion-related fields */ 7751 tcp->tcp_fused = B_FALSE; 7752 tcp->tcp_unfusable = B_FALSE; 7753 tcp->tcp_fused_sigurg = B_FALSE; 7754 tcp->tcp_direct_sockfs = B_FALSE; 7755 tcp->tcp_fuse_syncstr_stopped = B_FALSE; 7756 tcp->tcp_loopback_peer = NULL; 7757 tcp->tcp_fuse_rcv_hiwater = 0; 7758 tcp->tcp_fuse_rcv_unread_hiwater = 0; 7759 tcp->tcp_fuse_rcv_unread_cnt = 0; 7760 7761 /* Initialize the header template */ 7762 if (tcp->tcp_ipversion == IPV4_VERSION) { 7763 err = tcp_header_init_ipv4(tcp); 7764 } else { 7765 err = tcp_header_init_ipv6(tcp); 7766 } 7767 if (err) 7768 return (err); 7769 7770 /* 7771 * Init the window scale to the max so tcp_rwnd_set() won't pare 7772 * down tcp_rwnd. tcp_adapt_ire() will set the right value later. 7773 */ 7774 tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT; 7775 tcp->tcp_xmit_lowater = tcp_xmit_lowat; 7776 tcp->tcp_xmit_hiwater = tcp_xmit_hiwat; 7777 7778 tcp->tcp_cork = B_FALSE; 7779 /* 7780 * Init the tcp_debug option. This value determines whether TCP 7781 * calls strlog() to print out debug messages. Doing this 7782 * initialization here means that this value is not inherited thru 7783 * tcp_reinit(). 7784 */ 7785 tcp->tcp_debug = tcp_dbg; 7786 7787 tcp->tcp_ka_interval = tcp_keepalive_interval; 7788 tcp->tcp_ka_abort_thres = tcp_keepalive_abort_interval; 7789 7790 return (0); 7791 } 7792 7793 /* 7794 * Initialize the IPv4 header. Loses any record of any IP options. 7795 */ 7796 static int 7797 tcp_header_init_ipv4(tcp_t *tcp) 7798 { 7799 tcph_t *tcph; 7800 uint32_t sum; 7801 7802 /* 7803 * This is a simple initialization. If there's 7804 * already a template, it should never be too small, 7805 * so reuse it. Otherwise, allocate space for the new one. 7806 */ 7807 if (tcp->tcp_iphc == NULL) { 7808 ASSERT(tcp->tcp_iphc_len == 0); 7809 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 7810 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 7811 if (tcp->tcp_iphc == NULL) { 7812 tcp->tcp_iphc_len = 0; 7813 return (ENOMEM); 7814 } 7815 } 7816 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7817 tcp->tcp_ipha = (ipha_t *)tcp->tcp_iphc; 7818 tcp->tcp_ip6h = NULL; 7819 tcp->tcp_ipversion = IPV4_VERSION; 7820 tcp->tcp_hdr_len = sizeof (ipha_t) + sizeof (tcph_t); 7821 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 7822 tcp->tcp_ip_hdr_len = sizeof (ipha_t); 7823 tcp->tcp_ipha->ipha_length = htons(sizeof (ipha_t) + sizeof (tcph_t)); 7824 tcp->tcp_ipha->ipha_version_and_hdr_length 7825 = (IP_VERSION << 4) | IP_SIMPLE_HDR_LENGTH_IN_WORDS; 7826 tcp->tcp_ipha->ipha_ident = 0; 7827 7828 tcp->tcp_ttl = (uchar_t)tcp_ipv4_ttl; 7829 tcp->tcp_tos = 0; 7830 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0; 7831 tcp->tcp_ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 7832 tcp->tcp_ipha->ipha_protocol = IPPROTO_TCP; 7833 7834 tcph = (tcph_t *)(tcp->tcp_iphc + sizeof (ipha_t)); 7835 tcp->tcp_tcph = tcph; 7836 tcph->th_offset_and_rsrvd[0] = (5 << 4); 7837 /* 7838 * IP wants our header length in the checksum field to 7839 * allow it to perform a single pseudo-header+checksum 7840 * calculation on behalf of TCP. 7841 * Include the adjustment for a source route once IP_OPTIONS is set. 7842 */ 7843 sum = sizeof (tcph_t) + tcp->tcp_sum; 7844 sum = (sum >> 16) + (sum & 0xFFFF); 7845 U16_TO_ABE16(sum, tcph->th_sum); 7846 return (0); 7847 } 7848 7849 /* 7850 * Initialize the IPv6 header. Loses any record of any IPv6 extension headers. 7851 */ 7852 static int 7853 tcp_header_init_ipv6(tcp_t *tcp) 7854 { 7855 tcph_t *tcph; 7856 uint32_t sum; 7857 7858 /* 7859 * This is a simple initialization. If there's 7860 * already a template, it should never be too small, 7861 * so reuse it. Otherwise, allocate space for the new one. 7862 * Ensure that there is enough space to "downgrade" the tcp_t 7863 * to an IPv4 tcp_t. This requires having space for a full load 7864 * of IPv4 options, as well as a full load of TCP options 7865 * (TCP_MAX_COMBINED_HEADER_LENGTH, 120 bytes); this is more space 7866 * than a v6 header and a TCP header with a full load of TCP options 7867 * (IPV6_HDR_LEN is 40 bytes; TCP_MAX_HDR_LENGTH is 60 bytes). 7868 * We want to avoid reallocation in the "downgraded" case when 7869 * processing outbound IPv4 options. 7870 */ 7871 if (tcp->tcp_iphc == NULL) { 7872 ASSERT(tcp->tcp_iphc_len == 0); 7873 tcp->tcp_iphc_len = TCP_MAX_COMBINED_HEADER_LENGTH; 7874 tcp->tcp_iphc = kmem_cache_alloc(tcp_iphc_cache, KM_NOSLEEP); 7875 if (tcp->tcp_iphc == NULL) { 7876 tcp->tcp_iphc_len = 0; 7877 return (ENOMEM); 7878 } 7879 } 7880 ASSERT(tcp->tcp_iphc_len >= TCP_MAX_COMBINED_HEADER_LENGTH); 7881 tcp->tcp_ipversion = IPV6_VERSION; 7882 tcp->tcp_hdr_len = IPV6_HDR_LEN + sizeof (tcph_t); 7883 tcp->tcp_tcp_hdr_len = sizeof (tcph_t); 7884 tcp->tcp_ip_hdr_len = IPV6_HDR_LEN; 7885 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 7886 tcp->tcp_ipha = NULL; 7887 7888 /* Initialize the header template */ 7889 7890 tcp->tcp_ip6h->ip6_vcf = IPV6_DEFAULT_VERS_AND_FLOW; 7891 tcp->tcp_ip6h->ip6_plen = ntohs(sizeof (tcph_t)); 7892 tcp->tcp_ip6h->ip6_nxt = IPPROTO_TCP; 7893 tcp->tcp_ip6h->ip6_hops = (uint8_t)tcp_ipv6_hoplimit; 7894 7895 tcph = (tcph_t *)(tcp->tcp_iphc + IPV6_HDR_LEN); 7896 tcp->tcp_tcph = tcph; 7897 tcph->th_offset_and_rsrvd[0] = (5 << 4); 7898 /* 7899 * IP wants our header length in the checksum field to 7900 * allow it to perform a single psuedo-header+checksum 7901 * calculation on behalf of TCP. 7902 * Include the adjustment for a source route when IPV6_RTHDR is set. 7903 */ 7904 sum = sizeof (tcph_t) + tcp->tcp_sum; 7905 sum = (sum >> 16) + (sum & 0xFFFF); 7906 U16_TO_ABE16(sum, tcph->th_sum); 7907 return (0); 7908 } 7909 7910 /* At minimum we need 4 bytes in the TCP header for the lookup */ 7911 #define ICMP_MIN_TCP_HDR 12 7912 7913 /* 7914 * tcp_icmp_error is called by tcp_rput_other to process ICMP error messages 7915 * passed up by IP. The message is always received on the correct tcp_t. 7916 * Assumes that IP has pulled up everything up to and including the ICMP header. 7917 */ 7918 void 7919 tcp_icmp_error(tcp_t *tcp, mblk_t *mp) 7920 { 7921 icmph_t *icmph; 7922 ipha_t *ipha; 7923 int iph_hdr_length; 7924 tcph_t *tcph; 7925 boolean_t ipsec_mctl = B_FALSE; 7926 boolean_t secure; 7927 mblk_t *first_mp = mp; 7928 uint32_t new_mss; 7929 uint32_t ratio; 7930 size_t mp_size = MBLKL(mp); 7931 uint32_t seg_ack; 7932 uint32_t seg_seq; 7933 7934 /* Assume IP provides aligned packets - otherwise toss */ 7935 if (!OK_32PTR(mp->b_rptr)) { 7936 freemsg(mp); 7937 return; 7938 } 7939 7940 /* 7941 * Since ICMP errors are normal data marked with M_CTL when sent 7942 * to TCP or UDP, we have to look for a IPSEC_IN value to identify 7943 * packets starting with an ipsec_info_t, see ipsec_info.h. 7944 */ 7945 if ((mp_size == sizeof (ipsec_info_t)) && 7946 (((ipsec_info_t *)mp->b_rptr)->ipsec_info_type == IPSEC_IN)) { 7947 ASSERT(mp->b_cont != NULL); 7948 mp = mp->b_cont; 7949 /* IP should have done this */ 7950 ASSERT(OK_32PTR(mp->b_rptr)); 7951 mp_size = MBLKL(mp); 7952 ipsec_mctl = B_TRUE; 7953 } 7954 7955 /* 7956 * Verify that we have a complete outer IP header. If not, drop it. 7957 */ 7958 if (mp_size < sizeof (ipha_t)) { 7959 noticmpv4: 7960 freemsg(first_mp); 7961 return; 7962 } 7963 7964 ipha = (ipha_t *)mp->b_rptr; 7965 /* 7966 * Verify IP version. Anything other than IPv4 or IPv6 packet is sent 7967 * upstream. ICMPv6 is handled in tcp_icmp_error_ipv6. 7968 */ 7969 switch (IPH_HDR_VERSION(ipha)) { 7970 case IPV6_VERSION: 7971 tcp_icmp_error_ipv6(tcp, first_mp, ipsec_mctl); 7972 return; 7973 case IPV4_VERSION: 7974 break; 7975 default: 7976 goto noticmpv4; 7977 } 7978 7979 /* Skip past the outer IP and ICMP headers */ 7980 iph_hdr_length = IPH_HDR_LENGTH(ipha); 7981 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length]; 7982 /* 7983 * If we don't have the correct outer IP header length or if the ULP 7984 * is not IPPROTO_ICMP or if we don't have a complete inner IP header 7985 * send it upstream. 7986 */ 7987 if (iph_hdr_length < sizeof (ipha_t) || 7988 ipha->ipha_protocol != IPPROTO_ICMP || 7989 (ipha_t *)&icmph[1] + 1 > (ipha_t *)mp->b_wptr) { 7990 goto noticmpv4; 7991 } 7992 ipha = (ipha_t *)&icmph[1]; 7993 7994 /* Skip past the inner IP and find the ULP header */ 7995 iph_hdr_length = IPH_HDR_LENGTH(ipha); 7996 tcph = (tcph_t *)((char *)ipha + iph_hdr_length); 7997 /* 7998 * If we don't have the correct inner IP header length or if the ULP 7999 * is not IPPROTO_TCP or if we don't have at least ICMP_MIN_TCP_HDR 8000 * bytes of TCP header, drop it. 8001 */ 8002 if (iph_hdr_length < sizeof (ipha_t) || 8003 ipha->ipha_protocol != IPPROTO_TCP || 8004 (uchar_t *)tcph + ICMP_MIN_TCP_HDR > mp->b_wptr) { 8005 goto noticmpv4; 8006 } 8007 8008 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 8009 if (ipsec_mctl) { 8010 secure = ipsec_in_is_secure(first_mp); 8011 } else { 8012 secure = B_FALSE; 8013 } 8014 if (secure) { 8015 /* 8016 * If we are willing to accept this in clear 8017 * we don't have to verify policy. 8018 */ 8019 if (!ipsec_inbound_accept_clear(mp, ipha, NULL)) { 8020 if (!tcp_check_policy(tcp, first_mp, 8021 ipha, NULL, secure, ipsec_mctl)) { 8022 /* 8023 * tcp_check_policy called 8024 * ip_drop_packet() on failure. 8025 */ 8026 return; 8027 } 8028 } 8029 } 8030 } else if (ipsec_mctl) { 8031 /* 8032 * This is a hard_bound connection. IP has already 8033 * verified policy. We don't have to do it again. 8034 */ 8035 freeb(first_mp); 8036 first_mp = mp; 8037 ipsec_mctl = B_FALSE; 8038 } 8039 8040 seg_ack = ABE32_TO_U32(tcph->th_ack); 8041 seg_seq = ABE32_TO_U32(tcph->th_seq); 8042 /* 8043 * TCP SHOULD check that the TCP sequence number contained in 8044 * payload of the ICMP error message is within the range 8045 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8046 */ 8047 if (SEQ_LT(seg_seq, tcp->tcp_suna) || 8048 SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8049 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8050 /* 8051 * If the ICMP message is bogus, should we kill the 8052 * connection, or should we just drop the bogus ICMP 8053 * message? It would probably make more sense to just 8054 * drop the message so that if this one managed to get 8055 * in, the real connection should not suffer. 8056 */ 8057 goto noticmpv4; 8058 } 8059 8060 switch (icmph->icmph_type) { 8061 case ICMP_DEST_UNREACHABLE: 8062 switch (icmph->icmph_code) { 8063 case ICMP_FRAGMENTATION_NEEDED: 8064 /* 8065 * Reduce the MSS based on the new MTU. This will 8066 * eliminate any fragmentation locally. 8067 * N.B. There may well be some funny side-effects on 8068 * the local send policy and the remote receive policy. 8069 * Pending further research, we provide 8070 * tcp_ignore_path_mtu just in case this proves 8071 * disastrous somewhere. 8072 * 8073 * After updating the MSS, retransmit part of the 8074 * dropped segment using the new mss by calling 8075 * tcp_wput_data(). Need to adjust all those 8076 * params to make sure tcp_wput_data() work properly. 8077 */ 8078 if (tcp_ignore_path_mtu) 8079 break; 8080 8081 /* 8082 * Decrease the MSS by time stamp options 8083 * IP options and IPSEC options. tcp_hdr_len 8084 * includes time stamp option and IP option 8085 * length. 8086 */ 8087 8088 new_mss = ntohs(icmph->icmph_du_mtu) - 8089 tcp->tcp_hdr_len - tcp->tcp_ipsec_overhead; 8090 8091 /* 8092 * Only update the MSS if the new one is 8093 * smaller than the previous one. This is 8094 * to avoid problems when getting multiple 8095 * ICMP errors for the same MTU. 8096 */ 8097 if (new_mss >= tcp->tcp_mss) 8098 break; 8099 8100 /* 8101 * Stop doing PMTU if new_mss is less than 68 8102 * or less than tcp_mss_min. 8103 * The value 68 comes from rfc 1191. 8104 */ 8105 if (new_mss < MAX(68, tcp_mss_min)) 8106 tcp->tcp_ipha->ipha_fragment_offset_and_flags = 8107 0; 8108 8109 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8110 ASSERT(ratio >= 1); 8111 tcp_mss_set(tcp, new_mss); 8112 8113 /* 8114 * Make sure we have something to 8115 * send. 8116 */ 8117 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8118 (tcp->tcp_xmit_head != NULL)) { 8119 /* 8120 * Shrink tcp_cwnd in 8121 * proportion to the old MSS/new MSS. 8122 */ 8123 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8124 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8125 (tcp->tcp_unsent == 0)) { 8126 tcp->tcp_rexmit_max = tcp->tcp_fss; 8127 } else { 8128 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8129 } 8130 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8131 tcp->tcp_rexmit = B_TRUE; 8132 tcp->tcp_dupack_cnt = 0; 8133 tcp->tcp_snd_burst = TCP_CWND_SS; 8134 tcp_ss_rexmit(tcp); 8135 } 8136 break; 8137 case ICMP_PORT_UNREACHABLE: 8138 case ICMP_PROTOCOL_UNREACHABLE: 8139 switch (tcp->tcp_state) { 8140 case TCPS_SYN_SENT: 8141 case TCPS_SYN_RCVD: 8142 /* 8143 * ICMP can snipe away incipient 8144 * TCP connections as long as 8145 * seq number is same as initial 8146 * send seq number. 8147 */ 8148 if (seg_seq == tcp->tcp_iss) { 8149 (void) tcp_clean_death(tcp, 8150 ECONNREFUSED, 6); 8151 } 8152 break; 8153 } 8154 break; 8155 case ICMP_HOST_UNREACHABLE: 8156 case ICMP_NET_UNREACHABLE: 8157 /* Record the error in case we finally time out. */ 8158 if (icmph->icmph_code == ICMP_HOST_UNREACHABLE) 8159 tcp->tcp_client_errno = EHOSTUNREACH; 8160 else 8161 tcp->tcp_client_errno = ENETUNREACH; 8162 if (tcp->tcp_state == TCPS_SYN_RCVD) { 8163 if (tcp->tcp_listener != NULL && 8164 tcp->tcp_listener->tcp_syn_defense) { 8165 /* 8166 * Ditch the half-open connection if we 8167 * suspect a SYN attack is under way. 8168 */ 8169 tcp_ip_ire_mark_advice(tcp); 8170 (void) tcp_clean_death(tcp, 8171 tcp->tcp_client_errno, 7); 8172 } 8173 } 8174 break; 8175 default: 8176 break; 8177 } 8178 break; 8179 case ICMP_SOURCE_QUENCH: { 8180 /* 8181 * use a global boolean to control 8182 * whether TCP should respond to ICMP_SOURCE_QUENCH. 8183 * The default is false. 8184 */ 8185 if (tcp_icmp_source_quench) { 8186 /* 8187 * Reduce the sending rate as if we got a 8188 * retransmit timeout 8189 */ 8190 uint32_t npkt; 8191 8192 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / 8193 tcp->tcp_mss; 8194 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * tcp->tcp_mss; 8195 tcp->tcp_cwnd = tcp->tcp_mss; 8196 tcp->tcp_cwnd_cnt = 0; 8197 } 8198 break; 8199 } 8200 } 8201 freemsg(first_mp); 8202 } 8203 8204 /* 8205 * tcp_icmp_error_ipv6 is called by tcp_rput_other to process ICMPv6 8206 * error messages passed up by IP. 8207 * Assumes that IP has pulled up all the extension headers as well 8208 * as the ICMPv6 header. 8209 */ 8210 static void 8211 tcp_icmp_error_ipv6(tcp_t *tcp, mblk_t *mp, boolean_t ipsec_mctl) 8212 { 8213 icmp6_t *icmp6; 8214 ip6_t *ip6h; 8215 uint16_t iph_hdr_length; 8216 tcpha_t *tcpha; 8217 uint8_t *nexthdrp; 8218 uint32_t new_mss; 8219 uint32_t ratio; 8220 boolean_t secure; 8221 mblk_t *first_mp = mp; 8222 size_t mp_size; 8223 uint32_t seg_ack; 8224 uint32_t seg_seq; 8225 8226 /* 8227 * The caller has determined if this is an IPSEC_IN packet and 8228 * set ipsec_mctl appropriately (see tcp_icmp_error). 8229 */ 8230 if (ipsec_mctl) 8231 mp = mp->b_cont; 8232 8233 mp_size = MBLKL(mp); 8234 8235 /* 8236 * Verify that we have a complete IP header. If not, send it upstream. 8237 */ 8238 if (mp_size < sizeof (ip6_t)) { 8239 noticmpv6: 8240 freemsg(first_mp); 8241 return; 8242 } 8243 8244 /* 8245 * Verify this is an ICMPV6 packet, else send it upstream. 8246 */ 8247 ip6h = (ip6_t *)mp->b_rptr; 8248 if (ip6h->ip6_nxt == IPPROTO_ICMPV6) { 8249 iph_hdr_length = IPV6_HDR_LEN; 8250 } else if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, 8251 &nexthdrp) || 8252 *nexthdrp != IPPROTO_ICMPV6) { 8253 goto noticmpv6; 8254 } 8255 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length]; 8256 ip6h = (ip6_t *)&icmp6[1]; 8257 /* 8258 * Verify if we have a complete ICMP and inner IP header. 8259 */ 8260 if ((uchar_t *)&ip6h[1] > mp->b_wptr) 8261 goto noticmpv6; 8262 8263 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) 8264 goto noticmpv6; 8265 tcpha = (tcpha_t *)((char *)ip6h + iph_hdr_length); 8266 /* 8267 * Validate inner header. If the ULP is not IPPROTO_TCP or if we don't 8268 * have at least ICMP_MIN_TCP_HDR bytes of TCP header drop the 8269 * packet. 8270 */ 8271 if ((*nexthdrp != IPPROTO_TCP) || 8272 ((uchar_t *)tcpha + ICMP_MIN_TCP_HDR) > mp->b_wptr) { 8273 goto noticmpv6; 8274 } 8275 8276 /* 8277 * ICMP errors come on the right queue or come on 8278 * listener/global queue for detached connections and 8279 * get switched to the right queue. If it comes on the 8280 * right queue, policy check has already been done by IP 8281 * and thus free the first_mp without verifying the policy. 8282 * If it has come for a non-hard bound connection, we need 8283 * to verify policy as IP may not have done it. 8284 */ 8285 if (!tcp->tcp_hard_bound) { 8286 if (ipsec_mctl) { 8287 secure = ipsec_in_is_secure(first_mp); 8288 } else { 8289 secure = B_FALSE; 8290 } 8291 if (secure) { 8292 /* 8293 * If we are willing to accept this in clear 8294 * we don't have to verify policy. 8295 */ 8296 if (!ipsec_inbound_accept_clear(mp, NULL, ip6h)) { 8297 if (!tcp_check_policy(tcp, first_mp, 8298 NULL, ip6h, secure, ipsec_mctl)) { 8299 /* 8300 * tcp_check_policy called 8301 * ip_drop_packet() on failure. 8302 */ 8303 return; 8304 } 8305 } 8306 } 8307 } else if (ipsec_mctl) { 8308 /* 8309 * This is a hard_bound connection. IP has already 8310 * verified policy. We don't have to do it again. 8311 */ 8312 freeb(first_mp); 8313 first_mp = mp; 8314 ipsec_mctl = B_FALSE; 8315 } 8316 8317 seg_ack = ntohl(tcpha->tha_ack); 8318 seg_seq = ntohl(tcpha->tha_seq); 8319 /* 8320 * TCP SHOULD check that the TCP sequence number contained in 8321 * payload of the ICMP error message is within the range 8322 * SND.UNA <= SEG.SEQ < SND.NXT. and also SEG.ACK <= RECV.NXT 8323 */ 8324 if (SEQ_LT(seg_seq, tcp->tcp_suna) || SEQ_GEQ(seg_seq, tcp->tcp_snxt) || 8325 SEQ_GT(seg_ack, tcp->tcp_rnxt)) { 8326 /* 8327 * If the ICMP message is bogus, should we kill the 8328 * connection, or should we just drop the bogus ICMP 8329 * message? It would probably make more sense to just 8330 * drop the message so that if this one managed to get 8331 * in, the real connection should not suffer. 8332 */ 8333 goto noticmpv6; 8334 } 8335 8336 switch (icmp6->icmp6_type) { 8337 case ICMP6_PACKET_TOO_BIG: 8338 /* 8339 * Reduce the MSS based on the new MTU. This will 8340 * eliminate any fragmentation locally. 8341 * N.B. There may well be some funny side-effects on 8342 * the local send policy and the remote receive policy. 8343 * Pending further research, we provide 8344 * tcp_ignore_path_mtu just in case this proves 8345 * disastrous somewhere. 8346 * 8347 * After updating the MSS, retransmit part of the 8348 * dropped segment using the new mss by calling 8349 * tcp_wput_data(). Need to adjust all those 8350 * params to make sure tcp_wput_data() work properly. 8351 */ 8352 if (tcp_ignore_path_mtu) 8353 break; 8354 8355 /* 8356 * Decrease the MSS by time stamp options 8357 * IP options and IPSEC options. tcp_hdr_len 8358 * includes time stamp option and IP option 8359 * length. 8360 */ 8361 new_mss = ntohs(icmp6->icmp6_mtu) - tcp->tcp_hdr_len - 8362 tcp->tcp_ipsec_overhead; 8363 8364 /* 8365 * Only update the MSS if the new one is 8366 * smaller than the previous one. This is 8367 * to avoid problems when getting multiple 8368 * ICMP errors for the same MTU. 8369 */ 8370 if (new_mss >= tcp->tcp_mss) 8371 break; 8372 8373 ratio = tcp->tcp_cwnd / tcp->tcp_mss; 8374 ASSERT(ratio >= 1); 8375 tcp_mss_set(tcp, new_mss); 8376 8377 /* 8378 * Make sure we have something to 8379 * send. 8380 */ 8381 if (SEQ_LT(tcp->tcp_suna, tcp->tcp_snxt) && 8382 (tcp->tcp_xmit_head != NULL)) { 8383 /* 8384 * Shrink tcp_cwnd in 8385 * proportion to the old MSS/new MSS. 8386 */ 8387 tcp->tcp_cwnd = ratio * tcp->tcp_mss; 8388 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 8389 (tcp->tcp_unsent == 0)) { 8390 tcp->tcp_rexmit_max = tcp->tcp_fss; 8391 } else { 8392 tcp->tcp_rexmit_max = tcp->tcp_snxt; 8393 } 8394 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 8395 tcp->tcp_rexmit = B_TRUE; 8396 tcp->tcp_dupack_cnt = 0; 8397 tcp->tcp_snd_burst = TCP_CWND_SS; 8398 tcp_ss_rexmit(tcp); 8399 } 8400 break; 8401 8402 case ICMP6_DST_UNREACH: 8403 switch (icmp6->icmp6_code) { 8404 case ICMP6_DST_UNREACH_NOPORT: 8405 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8406 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8407 (seg_seq == tcp->tcp_iss)) { 8408 (void) tcp_clean_death(tcp, 8409 ECONNREFUSED, 8); 8410 } 8411 break; 8412 8413 case ICMP6_DST_UNREACH_ADMIN: 8414 case ICMP6_DST_UNREACH_NOROUTE: 8415 case ICMP6_DST_UNREACH_BEYONDSCOPE: 8416 case ICMP6_DST_UNREACH_ADDR: 8417 /* Record the error in case we finally time out. */ 8418 tcp->tcp_client_errno = EHOSTUNREACH; 8419 if (((tcp->tcp_state == TCPS_SYN_SENT) || 8420 (tcp->tcp_state == TCPS_SYN_RCVD)) && 8421 (seg_seq == tcp->tcp_iss)) { 8422 if (tcp->tcp_listener != NULL && 8423 tcp->tcp_listener->tcp_syn_defense) { 8424 /* 8425 * Ditch the half-open connection if we 8426 * suspect a SYN attack is under way. 8427 */ 8428 tcp_ip_ire_mark_advice(tcp); 8429 (void) tcp_clean_death(tcp, 8430 tcp->tcp_client_errno, 9); 8431 } 8432 } 8433 8434 8435 break; 8436 default: 8437 break; 8438 } 8439 break; 8440 8441 case ICMP6_PARAM_PROB: 8442 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */ 8443 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER && 8444 (uchar_t *)ip6h + icmp6->icmp6_pptr == 8445 (uchar_t *)nexthdrp) { 8446 if (tcp->tcp_state == TCPS_SYN_SENT || 8447 tcp->tcp_state == TCPS_SYN_RCVD) { 8448 (void) tcp_clean_death(tcp, 8449 ECONNREFUSED, 10); 8450 } 8451 break; 8452 } 8453 break; 8454 8455 case ICMP6_TIME_EXCEEDED: 8456 default: 8457 break; 8458 } 8459 freemsg(first_mp); 8460 } 8461 8462 /* 8463 * IP recognizes seven kinds of bind requests: 8464 * 8465 * - A zero-length address binds only to the protocol number. 8466 * 8467 * - A 4-byte address is treated as a request to 8468 * validate that the address is a valid local IPv4 8469 * address, appropriate for an application to bind to. 8470 * IP does the verification, but does not make any note 8471 * of the address at this time. 8472 * 8473 * - A 16-byte address contains is treated as a request 8474 * to validate a local IPv6 address, as the 4-byte 8475 * address case above. 8476 * 8477 * - A 16-byte sockaddr_in to validate the local IPv4 address and also 8478 * use it for the inbound fanout of packets. 8479 * 8480 * - A 24-byte sockaddr_in6 to validate the local IPv6 address and also 8481 * use it for the inbound fanout of packets. 8482 * 8483 * - A 12-byte address (ipa_conn_t) containing complete IPv4 fanout 8484 * information consisting of local and remote addresses 8485 * and ports. In this case, the addresses are both 8486 * validated as appropriate for this operation, and, if 8487 * so, the information is retained for use in the 8488 * inbound fanout. 8489 * 8490 * - A 36-byte address address (ipa6_conn_t) containing complete IPv6 8491 * fanout information, like the 12-byte case above. 8492 * 8493 * IP will also fill in the IRE request mblk with information 8494 * regarding our peer. In all cases, we notify IP of our protocol 8495 * type by appending a single protocol byte to the bind request. 8496 */ 8497 static mblk_t * 8498 tcp_ip_bind_mp(tcp_t *tcp, t_scalar_t bind_prim, t_scalar_t addr_length) 8499 { 8500 char *cp; 8501 mblk_t *mp; 8502 struct T_bind_req *tbr; 8503 ipa_conn_t *ac; 8504 ipa6_conn_t *ac6; 8505 sin_t *sin; 8506 sin6_t *sin6; 8507 8508 ASSERT(bind_prim == O_T_BIND_REQ || bind_prim == T_BIND_REQ); 8509 ASSERT((tcp->tcp_family == AF_INET && 8510 tcp->tcp_ipversion == IPV4_VERSION) || 8511 (tcp->tcp_family == AF_INET6 && 8512 (tcp->tcp_ipversion == IPV4_VERSION || 8513 tcp->tcp_ipversion == IPV6_VERSION))); 8514 8515 mp = allocb(sizeof (*tbr) + addr_length + 1, BPRI_HI); 8516 if (!mp) 8517 return (mp); 8518 mp->b_datap->db_type = M_PROTO; 8519 tbr = (struct T_bind_req *)mp->b_rptr; 8520 tbr->PRIM_type = bind_prim; 8521 tbr->ADDR_offset = sizeof (*tbr); 8522 tbr->CONIND_number = 0; 8523 tbr->ADDR_length = addr_length; 8524 cp = (char *)&tbr[1]; 8525 switch (addr_length) { 8526 case sizeof (ipa_conn_t): 8527 ASSERT(tcp->tcp_family == AF_INET); 8528 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 8529 8530 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 8531 if (mp->b_cont == NULL) { 8532 freemsg(mp); 8533 return (NULL); 8534 } 8535 mp->b_cont->b_wptr += sizeof (ire_t); 8536 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 8537 8538 /* cp known to be 32 bit aligned */ 8539 ac = (ipa_conn_t *)cp; 8540 ac->ac_laddr = tcp->tcp_ipha->ipha_src; 8541 ac->ac_faddr = tcp->tcp_remote; 8542 ac->ac_fport = tcp->tcp_fport; 8543 ac->ac_lport = tcp->tcp_lport; 8544 tcp->tcp_hard_binding = 1; 8545 break; 8546 8547 case sizeof (ipa6_conn_t): 8548 ASSERT(tcp->tcp_family == AF_INET6); 8549 8550 mp->b_cont = allocb(sizeof (ire_t), BPRI_HI); 8551 if (mp->b_cont == NULL) { 8552 freemsg(mp); 8553 return (NULL); 8554 } 8555 mp->b_cont->b_wptr += sizeof (ire_t); 8556 mp->b_cont->b_datap->db_type = IRE_DB_REQ_TYPE; 8557 8558 /* cp known to be 32 bit aligned */ 8559 ac6 = (ipa6_conn_t *)cp; 8560 if (tcp->tcp_ipversion == IPV4_VERSION) { 8561 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 8562 &ac6->ac6_laddr); 8563 } else { 8564 ac6->ac6_laddr = tcp->tcp_ip6h->ip6_src; 8565 } 8566 ac6->ac6_faddr = tcp->tcp_remote_v6; 8567 ac6->ac6_fport = tcp->tcp_fport; 8568 ac6->ac6_lport = tcp->tcp_lport; 8569 tcp->tcp_hard_binding = 1; 8570 break; 8571 8572 case sizeof (sin_t): 8573 /* 8574 * NOTE: IPV6_ADDR_LEN also has same size. 8575 * Use family to discriminate. 8576 */ 8577 if (tcp->tcp_family == AF_INET) { 8578 sin = (sin_t *)cp; 8579 8580 *sin = sin_null; 8581 sin->sin_family = AF_INET; 8582 sin->sin_addr.s_addr = tcp->tcp_bound_source; 8583 sin->sin_port = tcp->tcp_lport; 8584 break; 8585 } else { 8586 *(in6_addr_t *)cp = tcp->tcp_bound_source_v6; 8587 } 8588 break; 8589 8590 case sizeof (sin6_t): 8591 ASSERT(tcp->tcp_family == AF_INET6); 8592 sin6 = (sin6_t *)cp; 8593 8594 *sin6 = sin6_null; 8595 sin6->sin6_family = AF_INET6; 8596 sin6->sin6_addr = tcp->tcp_bound_source_v6; 8597 sin6->sin6_port = tcp->tcp_lport; 8598 break; 8599 8600 case IP_ADDR_LEN: 8601 ASSERT(tcp->tcp_ipversion == IPV4_VERSION); 8602 *(uint32_t *)cp = tcp->tcp_ipha->ipha_src; 8603 break; 8604 8605 } 8606 /* Add protocol number to end */ 8607 cp[addr_length] = (char)IPPROTO_TCP; 8608 mp->b_wptr = (uchar_t *)&cp[addr_length + 1]; 8609 return (mp); 8610 } 8611 8612 /* 8613 * Notify IP that we are having trouble with this connection. IP should 8614 * blow the IRE away and start over. 8615 */ 8616 static void 8617 tcp_ip_notify(tcp_t *tcp) 8618 { 8619 struct iocblk *iocp; 8620 ipid_t *ipid; 8621 mblk_t *mp; 8622 8623 /* IPv6 has NUD thus notification to delete the IRE is not needed */ 8624 if (tcp->tcp_ipversion == IPV6_VERSION) 8625 return; 8626 8627 mp = mkiocb(IP_IOCTL); 8628 if (mp == NULL) 8629 return; 8630 8631 iocp = (struct iocblk *)mp->b_rptr; 8632 iocp->ioc_count = sizeof (ipid_t) + sizeof (tcp->tcp_ipha->ipha_dst); 8633 8634 mp->b_cont = allocb(iocp->ioc_count, BPRI_HI); 8635 if (!mp->b_cont) { 8636 freeb(mp); 8637 return; 8638 } 8639 8640 ipid = (ipid_t *)mp->b_cont->b_rptr; 8641 mp->b_cont->b_wptr += iocp->ioc_count; 8642 bzero(ipid, sizeof (*ipid)); 8643 ipid->ipid_cmd = IP_IOC_IRE_DELETE_NO_REPLY; 8644 ipid->ipid_ire_type = IRE_CACHE; 8645 ipid->ipid_addr_offset = sizeof (ipid_t); 8646 ipid->ipid_addr_length = sizeof (tcp->tcp_ipha->ipha_dst); 8647 /* 8648 * Note: in the case of source routing we want to blow away the 8649 * route to the first source route hop. 8650 */ 8651 bcopy(&tcp->tcp_ipha->ipha_dst, &ipid[1], 8652 sizeof (tcp->tcp_ipha->ipha_dst)); 8653 8654 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 8655 } 8656 8657 /* Unlink and return any mblk that looks like it contains an ire */ 8658 static mblk_t * 8659 tcp_ire_mp(mblk_t *mp) 8660 { 8661 mblk_t *prev_mp; 8662 8663 for (;;) { 8664 prev_mp = mp; 8665 mp = mp->b_cont; 8666 if (mp == NULL) 8667 break; 8668 switch (DB_TYPE(mp)) { 8669 case IRE_DB_TYPE: 8670 case IRE_DB_REQ_TYPE: 8671 if (prev_mp != NULL) 8672 prev_mp->b_cont = mp->b_cont; 8673 mp->b_cont = NULL; 8674 return (mp); 8675 default: 8676 break; 8677 } 8678 } 8679 return (mp); 8680 } 8681 8682 /* 8683 * Timer callback routine for keepalive probe. We do a fake resend of 8684 * last ACKed byte. Then set a timer using RTO. When the timer expires, 8685 * check to see if we have heard anything from the other end for the last 8686 * RTO period. If we have, set the timer to expire for another 8687 * tcp_keepalive_intrvl and check again. If we have not, set a timer using 8688 * RTO << 1 and check again when it expires. Keep exponentially increasing 8689 * the timeout if we have not heard from the other side. If for more than 8690 * (tcp_ka_interval + tcp_ka_abort_thres) we have not heard anything, 8691 * kill the connection unless the keepalive abort threshold is 0. In 8692 * that case, we will probe "forever." 8693 */ 8694 static void 8695 tcp_keepalive_killer(void *arg) 8696 { 8697 mblk_t *mp; 8698 conn_t *connp = (conn_t *)arg; 8699 tcp_t *tcp = connp->conn_tcp; 8700 int32_t firetime; 8701 int32_t idletime; 8702 int32_t ka_intrvl; 8703 8704 tcp->tcp_ka_tid = 0; 8705 8706 if (tcp->tcp_fused) 8707 return; 8708 8709 BUMP_MIB(&tcp_mib, tcpTimKeepalive); 8710 ka_intrvl = tcp->tcp_ka_interval; 8711 8712 /* 8713 * Keepalive probe should only be sent if the application has not 8714 * done a close on the connection. 8715 */ 8716 if (tcp->tcp_state > TCPS_CLOSE_WAIT) { 8717 return; 8718 } 8719 /* Timer fired too early, restart it. */ 8720 if (tcp->tcp_state < TCPS_ESTABLISHED) { 8721 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8722 MSEC_TO_TICK(ka_intrvl)); 8723 return; 8724 } 8725 8726 idletime = TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time); 8727 /* 8728 * If we have not heard from the other side for a long 8729 * time, kill the connection unless the keepalive abort 8730 * threshold is 0. In that case, we will probe "forever." 8731 */ 8732 if (tcp->tcp_ka_abort_thres != 0 && 8733 idletime > (ka_intrvl + tcp->tcp_ka_abort_thres)) { 8734 BUMP_MIB(&tcp_mib, tcpTimKeepaliveDrop); 8735 (void) tcp_clean_death(tcp, tcp->tcp_client_errno ? 8736 tcp->tcp_client_errno : ETIMEDOUT, 11); 8737 return; 8738 } 8739 8740 if (tcp->tcp_snxt == tcp->tcp_suna && 8741 idletime >= ka_intrvl) { 8742 /* Fake resend of last ACKed byte. */ 8743 mblk_t *mp1 = allocb(1, BPRI_LO); 8744 8745 if (mp1 != NULL) { 8746 *mp1->b_wptr++ = '\0'; 8747 mp = tcp_xmit_mp(tcp, mp1, 1, NULL, NULL, 8748 tcp->tcp_suna - 1, B_FALSE, NULL, B_TRUE); 8749 freeb(mp1); 8750 /* 8751 * if allocation failed, fall through to start the 8752 * timer back. 8753 */ 8754 if (mp != NULL) { 8755 TCP_RECORD_TRACE(tcp, mp, 8756 TCP_TRACE_SEND_PKT); 8757 tcp_send_data(tcp, tcp->tcp_wq, mp); 8758 BUMP_MIB(&tcp_mib, tcpTimKeepaliveProbe); 8759 if (tcp->tcp_ka_last_intrvl != 0) { 8760 /* 8761 * We should probe again at least 8762 * in ka_intrvl, but not more than 8763 * tcp_rexmit_interval_max. 8764 */ 8765 firetime = MIN(ka_intrvl - 1, 8766 tcp->tcp_ka_last_intrvl << 1); 8767 if (firetime > tcp_rexmit_interval_max) 8768 firetime = 8769 tcp_rexmit_interval_max; 8770 } else { 8771 firetime = tcp->tcp_rto; 8772 } 8773 tcp->tcp_ka_tid = TCP_TIMER(tcp, 8774 tcp_keepalive_killer, 8775 MSEC_TO_TICK(firetime)); 8776 tcp->tcp_ka_last_intrvl = firetime; 8777 return; 8778 } 8779 } 8780 } else { 8781 tcp->tcp_ka_last_intrvl = 0; 8782 } 8783 8784 /* firetime can be negative if (mp1 == NULL || mp == NULL) */ 8785 if ((firetime = ka_intrvl - idletime) < 0) { 8786 firetime = ka_intrvl; 8787 } 8788 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 8789 MSEC_TO_TICK(firetime)); 8790 } 8791 8792 int 8793 tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk) 8794 { 8795 queue_t *q = tcp->tcp_rq; 8796 int32_t mss = tcp->tcp_mss; 8797 int maxpsz; 8798 8799 if (TCP_IS_DETACHED(tcp)) 8800 return (mss); 8801 8802 if (tcp->tcp_fused) { 8803 maxpsz = tcp_fuse_maxpsz_set(tcp); 8804 mss = INFPSZ; 8805 } else if (tcp->tcp_mdt || tcp->tcp_maxpsz == 0) { 8806 /* 8807 * Set the sd_qn_maxpsz according to the socket send buffer 8808 * size, and sd_maxblk to INFPSZ (-1). This will essentially 8809 * instruct the stream head to copyin user data into contiguous 8810 * kernel-allocated buffers without breaking it up into smaller 8811 * chunks. We round up the buffer size to the nearest SMSS. 8812 */ 8813 maxpsz = MSS_ROUNDUP(tcp->tcp_xmit_hiwater, mss); 8814 if (tcp->tcp_kssl_ctx == NULL) 8815 mss = INFPSZ; 8816 else 8817 mss = SSL3_MAX_RECORD_LEN; 8818 } else { 8819 /* 8820 * Set sd_qn_maxpsz to approx half the (receivers) buffer 8821 * (and a multiple of the mss). This instructs the stream 8822 * head to break down larger than SMSS writes into SMSS- 8823 * size mblks, up to tcp_maxpsz_multiplier mblks at a time. 8824 */ 8825 maxpsz = tcp->tcp_maxpsz * mss; 8826 if (maxpsz > tcp->tcp_xmit_hiwater/2) { 8827 maxpsz = tcp->tcp_xmit_hiwater/2; 8828 /* Round up to nearest mss */ 8829 maxpsz = MSS_ROUNDUP(maxpsz, mss); 8830 } 8831 } 8832 (void) setmaxps(q, maxpsz); 8833 tcp->tcp_wq->q_maxpsz = maxpsz; 8834 8835 if (set_maxblk) 8836 (void) mi_set_sth_maxblk(q, mss); 8837 8838 return (mss); 8839 } 8840 8841 /* 8842 * Extract option values from a tcp header. We put any found values into the 8843 * tcpopt struct and return a bitmask saying which options were found. 8844 */ 8845 static int 8846 tcp_parse_options(tcph_t *tcph, tcp_opt_t *tcpopt) 8847 { 8848 uchar_t *endp; 8849 int len; 8850 uint32_t mss; 8851 uchar_t *up = (uchar_t *)tcph; 8852 int found = 0; 8853 int32_t sack_len; 8854 tcp_seq sack_begin, sack_end; 8855 tcp_t *tcp; 8856 8857 endp = up + TCP_HDR_LENGTH(tcph); 8858 up += TCP_MIN_HEADER_LENGTH; 8859 while (up < endp) { 8860 len = endp - up; 8861 switch (*up) { 8862 case TCPOPT_EOL: 8863 break; 8864 8865 case TCPOPT_NOP: 8866 up++; 8867 continue; 8868 8869 case TCPOPT_MAXSEG: 8870 if (len < TCPOPT_MAXSEG_LEN || 8871 up[1] != TCPOPT_MAXSEG_LEN) 8872 break; 8873 8874 mss = BE16_TO_U16(up+2); 8875 /* Caller must handle tcp_mss_min and tcp_mss_max_* */ 8876 tcpopt->tcp_opt_mss = mss; 8877 found |= TCP_OPT_MSS_PRESENT; 8878 8879 up += TCPOPT_MAXSEG_LEN; 8880 continue; 8881 8882 case TCPOPT_WSCALE: 8883 if (len < TCPOPT_WS_LEN || up[1] != TCPOPT_WS_LEN) 8884 break; 8885 8886 if (up[2] > TCP_MAX_WINSHIFT) 8887 tcpopt->tcp_opt_wscale = TCP_MAX_WINSHIFT; 8888 else 8889 tcpopt->tcp_opt_wscale = up[2]; 8890 found |= TCP_OPT_WSCALE_PRESENT; 8891 8892 up += TCPOPT_WS_LEN; 8893 continue; 8894 8895 case TCPOPT_SACK_PERMITTED: 8896 if (len < TCPOPT_SACK_OK_LEN || 8897 up[1] != TCPOPT_SACK_OK_LEN) 8898 break; 8899 found |= TCP_OPT_SACK_OK_PRESENT; 8900 up += TCPOPT_SACK_OK_LEN; 8901 continue; 8902 8903 case TCPOPT_SACK: 8904 if (len <= 2 || up[1] <= 2 || len < up[1]) 8905 break; 8906 8907 /* If TCP is not interested in SACK blks... */ 8908 if ((tcp = tcpopt->tcp) == NULL) { 8909 up += up[1]; 8910 continue; 8911 } 8912 sack_len = up[1] - TCPOPT_HEADER_LEN; 8913 up += TCPOPT_HEADER_LEN; 8914 8915 /* 8916 * If the list is empty, allocate one and assume 8917 * nothing is sack'ed. 8918 */ 8919 ASSERT(tcp->tcp_sack_info != NULL); 8920 if (tcp->tcp_notsack_list == NULL) { 8921 tcp_notsack_update(&(tcp->tcp_notsack_list), 8922 tcp->tcp_suna, tcp->tcp_snxt, 8923 &(tcp->tcp_num_notsack_blk), 8924 &(tcp->tcp_cnt_notsack_list)); 8925 8926 /* 8927 * Make sure tcp_notsack_list is not NULL. 8928 * This happens when kmem_alloc(KM_NOSLEEP) 8929 * returns NULL. 8930 */ 8931 if (tcp->tcp_notsack_list == NULL) { 8932 up += sack_len; 8933 continue; 8934 } 8935 tcp->tcp_fack = tcp->tcp_suna; 8936 } 8937 8938 while (sack_len > 0) { 8939 if (up + 8 > endp) { 8940 up = endp; 8941 break; 8942 } 8943 sack_begin = BE32_TO_U32(up); 8944 up += 4; 8945 sack_end = BE32_TO_U32(up); 8946 up += 4; 8947 sack_len -= 8; 8948 /* 8949 * Bounds checking. Make sure the SACK 8950 * info is within tcp_suna and tcp_snxt. 8951 * If this SACK blk is out of bound, ignore 8952 * it but continue to parse the following 8953 * blks. 8954 */ 8955 if (SEQ_LEQ(sack_end, sack_begin) || 8956 SEQ_LT(sack_begin, tcp->tcp_suna) || 8957 SEQ_GT(sack_end, tcp->tcp_snxt)) { 8958 continue; 8959 } 8960 tcp_notsack_insert(&(tcp->tcp_notsack_list), 8961 sack_begin, sack_end, 8962 &(tcp->tcp_num_notsack_blk), 8963 &(tcp->tcp_cnt_notsack_list)); 8964 if (SEQ_GT(sack_end, tcp->tcp_fack)) { 8965 tcp->tcp_fack = sack_end; 8966 } 8967 } 8968 found |= TCP_OPT_SACK_PRESENT; 8969 continue; 8970 8971 case TCPOPT_TSTAMP: 8972 if (len < TCPOPT_TSTAMP_LEN || 8973 up[1] != TCPOPT_TSTAMP_LEN) 8974 break; 8975 8976 tcpopt->tcp_opt_ts_val = BE32_TO_U32(up+2); 8977 tcpopt->tcp_opt_ts_ecr = BE32_TO_U32(up+6); 8978 8979 found |= TCP_OPT_TSTAMP_PRESENT; 8980 8981 up += TCPOPT_TSTAMP_LEN; 8982 continue; 8983 8984 default: 8985 if (len <= 1 || len < (int)up[1] || up[1] == 0) 8986 break; 8987 up += up[1]; 8988 continue; 8989 } 8990 break; 8991 } 8992 return (found); 8993 } 8994 8995 /* 8996 * Set the mss associated with a particular tcp based on its current value, 8997 * and a new one passed in. Observe minimums and maximums, and reset 8998 * other state variables that we want to view as multiples of mss. 8999 * 9000 * This function is called in various places mainly because 9001 * 1) Various stuffs, tcp_mss, tcp_cwnd, ... need to be adjusted when the 9002 * other side's SYN/SYN-ACK packet arrives. 9003 * 2) PMTUd may get us a new MSS. 9004 * 3) If the other side stops sending us timestamp option, we need to 9005 * increase the MSS size to use the extra bytes available. 9006 */ 9007 static void 9008 tcp_mss_set(tcp_t *tcp, uint32_t mss) 9009 { 9010 uint32_t mss_max; 9011 9012 if (tcp->tcp_ipversion == IPV4_VERSION) 9013 mss_max = tcp_mss_max_ipv4; 9014 else 9015 mss_max = tcp_mss_max_ipv6; 9016 9017 if (mss < tcp_mss_min) 9018 mss = tcp_mss_min; 9019 if (mss > mss_max) 9020 mss = mss_max; 9021 /* 9022 * Unless naglim has been set by our client to 9023 * a non-mss value, force naglim to track mss. 9024 * This can help to aggregate small writes. 9025 */ 9026 if (mss < tcp->tcp_naglim || tcp->tcp_mss == tcp->tcp_naglim) 9027 tcp->tcp_naglim = mss; 9028 /* 9029 * TCP should be able to buffer at least 4 MSS data for obvious 9030 * performance reason. 9031 */ 9032 if ((mss << 2) > tcp->tcp_xmit_hiwater) 9033 tcp->tcp_xmit_hiwater = mss << 2; 9034 9035 /* 9036 * Check if we need to apply the tcp_init_cwnd here. If 9037 * it is set and the MSS gets bigger (should not happen 9038 * normally), we need to adjust the resulting tcp_cwnd properly. 9039 * The new tcp_cwnd should not get bigger. 9040 */ 9041 if (tcp->tcp_init_cwnd == 0) { 9042 tcp->tcp_cwnd = MIN(tcp_slow_start_initial * mss, 9043 MIN(4 * mss, MAX(2 * mss, 4380 / mss * mss))); 9044 } else { 9045 if (tcp->tcp_mss < mss) { 9046 tcp->tcp_cwnd = MAX(1, 9047 (tcp->tcp_init_cwnd * tcp->tcp_mss / mss)) * mss; 9048 } else { 9049 tcp->tcp_cwnd = tcp->tcp_init_cwnd * mss; 9050 } 9051 } 9052 tcp->tcp_mss = mss; 9053 tcp->tcp_cwnd_cnt = 0; 9054 (void) tcp_maxpsz_set(tcp, B_TRUE); 9055 } 9056 9057 static int 9058 tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp) 9059 { 9060 tcp_t *tcp = NULL; 9061 conn_t *connp; 9062 int err; 9063 dev_t conn_dev; 9064 zoneid_t zoneid = getzoneid(); 9065 9066 /* 9067 * Special case for install: miniroot needs to be able to access files 9068 * via NFS as though it were always in the global zone. 9069 */ 9070 if (credp == kcred && nfs_global_client_only != 0) 9071 zoneid = GLOBAL_ZONEID; 9072 9073 if (q->q_ptr != NULL) 9074 return (0); 9075 9076 if (sflag == MODOPEN) { 9077 /* 9078 * This is a special case. The purpose of a modopen 9079 * is to allow just the T_SVR4_OPTMGMT_REQ to pass 9080 * through for MIB browsers. Everything else is failed. 9081 */ 9082 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9083 9084 if (connp == NULL) 9085 return (ENOMEM); 9086 9087 connp->conn_flags |= IPCL_TCPMOD; 9088 connp->conn_cred = credp; 9089 connp->conn_zoneid = zoneid; 9090 q->q_ptr = WR(q)->q_ptr = connp; 9091 crhold(credp); 9092 q->q_qinfo = &tcp_mod_rinit; 9093 WR(q)->q_qinfo = &tcp_mod_winit; 9094 qprocson(q); 9095 return (0); 9096 } 9097 9098 if ((conn_dev = inet_minor_alloc(ip_minor_arena)) == 0) 9099 return (EBUSY); 9100 9101 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev); 9102 9103 if (flag & SO_ACCEPTOR) { 9104 q->q_qinfo = &tcp_acceptor_rinit; 9105 q->q_ptr = (void *)conn_dev; 9106 WR(q)->q_qinfo = &tcp_acceptor_winit; 9107 WR(q)->q_ptr = (void *)conn_dev; 9108 qprocson(q); 9109 return (0); 9110 } 9111 9112 connp = (conn_t *)tcp_get_conn(IP_SQUEUE_GET(lbolt)); 9113 if (connp == NULL) { 9114 inet_minor_free(ip_minor_arena, conn_dev); 9115 q->q_ptr = NULL; 9116 return (ENOSR); 9117 } 9118 connp->conn_sqp = IP_SQUEUE_GET(lbolt); 9119 tcp = connp->conn_tcp; 9120 9121 q->q_ptr = WR(q)->q_ptr = connp; 9122 if (getmajor(*devp) == TCP6_MAJ) { 9123 connp->conn_flags |= (IPCL_TCP6|IPCL_ISV6); 9124 connp->conn_send = ip_output_v6; 9125 connp->conn_af_isv6 = B_TRUE; 9126 connp->conn_pkt_isv6 = B_TRUE; 9127 connp->conn_src_preferences = IPV6_PREFER_SRC_DEFAULT; 9128 tcp->tcp_ipversion = IPV6_VERSION; 9129 tcp->tcp_family = AF_INET6; 9130 tcp->tcp_mss = tcp_mss_def_ipv6; 9131 } else { 9132 connp->conn_flags |= IPCL_TCP4; 9133 connp->conn_send = ip_output; 9134 connp->conn_af_isv6 = B_FALSE; 9135 connp->conn_pkt_isv6 = B_FALSE; 9136 tcp->tcp_ipversion = IPV4_VERSION; 9137 tcp->tcp_family = AF_INET; 9138 tcp->tcp_mss = tcp_mss_def_ipv4; 9139 } 9140 9141 /* 9142 * TCP keeps a copy of cred for cache locality reasons but 9143 * we put a reference only once. If connp->conn_cred 9144 * becomes invalid, tcp_cred should also be set to NULL. 9145 */ 9146 tcp->tcp_cred = connp->conn_cred = credp; 9147 crhold(connp->conn_cred); 9148 tcp->tcp_cpid = curproc->p_pid; 9149 connp->conn_zoneid = zoneid; 9150 9151 connp->conn_dev = conn_dev; 9152 9153 ASSERT(q->q_qinfo == &tcp_rinit); 9154 ASSERT(WR(q)->q_qinfo == &tcp_winit); 9155 9156 if (flag & SO_SOCKSTR) { 9157 /* 9158 * No need to insert a socket in tcp acceptor hash. 9159 * If it was a socket acceptor stream, we dealt with 9160 * it above. A socket listener can never accept a 9161 * connection and doesn't need acceptor_id. 9162 */ 9163 connp->conn_flags |= IPCL_SOCKET; 9164 tcp->tcp_issocket = 1; 9165 WR(q)->q_qinfo = &tcp_sock_winit; 9166 } else { 9167 #ifdef _ILP32 9168 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 9169 #else 9170 tcp->tcp_acceptor_id = conn_dev; 9171 #endif /* _ILP32 */ 9172 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 9173 } 9174 9175 if (tcp_trace) 9176 tcp->tcp_tracebuf = kmem_zalloc(sizeof (tcptrch_t), KM_SLEEP); 9177 9178 err = tcp_init(tcp, q); 9179 if (err != 0) { 9180 inet_minor_free(ip_minor_arena, connp->conn_dev); 9181 tcp_acceptor_hash_remove(tcp); 9182 CONN_DEC_REF(connp); 9183 q->q_ptr = WR(q)->q_ptr = NULL; 9184 return (err); 9185 } 9186 9187 RD(q)->q_hiwat = tcp_recv_hiwat; 9188 tcp->tcp_rwnd = tcp_recv_hiwat; 9189 9190 /* Non-zero default values */ 9191 connp->conn_multicast_loop = IP_DEFAULT_MULTICAST_LOOP; 9192 /* 9193 * Put the ref for TCP. Ref for IP was already put 9194 * by ipcl_conn_create. Also Make the conn_t globally 9195 * visible to walkers 9196 */ 9197 mutex_enter(&connp->conn_lock); 9198 CONN_INC_REF_LOCKED(connp); 9199 ASSERT(connp->conn_ref == 2); 9200 connp->conn_state_flags &= ~CONN_INCIPIENT; 9201 mutex_exit(&connp->conn_lock); 9202 9203 qprocson(q); 9204 return (0); 9205 } 9206 9207 /* 9208 * Some TCP options can be "set" by requesting them in the option 9209 * buffer. This is needed for XTI feature test though we do not 9210 * allow it in general. We interpret that this mechanism is more 9211 * applicable to OSI protocols and need not be allowed in general. 9212 * This routine filters out options for which it is not allowed (most) 9213 * and lets through those (few) for which it is. [ The XTI interface 9214 * test suite specifics will imply that any XTI_GENERIC level XTI_* if 9215 * ever implemented will have to be allowed here ]. 9216 */ 9217 static boolean_t 9218 tcp_allow_connopt_set(int level, int name) 9219 { 9220 9221 switch (level) { 9222 case IPPROTO_TCP: 9223 switch (name) { 9224 case TCP_NODELAY: 9225 return (B_TRUE); 9226 default: 9227 return (B_FALSE); 9228 } 9229 /*NOTREACHED*/ 9230 default: 9231 return (B_FALSE); 9232 } 9233 /*NOTREACHED*/ 9234 } 9235 9236 /* 9237 * This routine gets default values of certain options whose default 9238 * values are maintained by protocol specific code 9239 */ 9240 /* ARGSUSED */ 9241 int 9242 tcp_opt_default(queue_t *q, int level, int name, uchar_t *ptr) 9243 { 9244 int32_t *i1 = (int32_t *)ptr; 9245 9246 switch (level) { 9247 case IPPROTO_TCP: 9248 switch (name) { 9249 case TCP_NOTIFY_THRESHOLD: 9250 *i1 = tcp_ip_notify_interval; 9251 break; 9252 case TCP_ABORT_THRESHOLD: 9253 *i1 = tcp_ip_abort_interval; 9254 break; 9255 case TCP_CONN_NOTIFY_THRESHOLD: 9256 *i1 = tcp_ip_notify_cinterval; 9257 break; 9258 case TCP_CONN_ABORT_THRESHOLD: 9259 *i1 = tcp_ip_abort_cinterval; 9260 break; 9261 default: 9262 return (-1); 9263 } 9264 break; 9265 case IPPROTO_IP: 9266 switch (name) { 9267 case IP_TTL: 9268 *i1 = tcp_ipv4_ttl; 9269 break; 9270 default: 9271 return (-1); 9272 } 9273 break; 9274 case IPPROTO_IPV6: 9275 switch (name) { 9276 case IPV6_UNICAST_HOPS: 9277 *i1 = tcp_ipv6_hoplimit; 9278 break; 9279 default: 9280 return (-1); 9281 } 9282 break; 9283 default: 9284 return (-1); 9285 } 9286 return (sizeof (int)); 9287 } 9288 9289 9290 /* 9291 * TCP routine to get the values of options. 9292 */ 9293 int 9294 tcp_opt_get(queue_t *q, int level, int name, uchar_t *ptr) 9295 { 9296 int *i1 = (int *)ptr; 9297 conn_t *connp = Q_TO_CONN(q); 9298 tcp_t *tcp = connp->conn_tcp; 9299 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 9300 9301 switch (level) { 9302 case SOL_SOCKET: 9303 switch (name) { 9304 case SO_LINGER: { 9305 struct linger *lgr = (struct linger *)ptr; 9306 9307 lgr->l_onoff = tcp->tcp_linger ? SO_LINGER : 0; 9308 lgr->l_linger = tcp->tcp_lingertime; 9309 } 9310 return (sizeof (struct linger)); 9311 case SO_DEBUG: 9312 *i1 = tcp->tcp_debug ? SO_DEBUG : 0; 9313 break; 9314 case SO_KEEPALIVE: 9315 *i1 = tcp->tcp_ka_enabled ? SO_KEEPALIVE : 0; 9316 break; 9317 case SO_DONTROUTE: 9318 *i1 = tcp->tcp_dontroute ? SO_DONTROUTE : 0; 9319 break; 9320 case SO_USELOOPBACK: 9321 *i1 = tcp->tcp_useloopback ? SO_USELOOPBACK : 0; 9322 break; 9323 case SO_BROADCAST: 9324 *i1 = tcp->tcp_broadcast ? SO_BROADCAST : 0; 9325 break; 9326 case SO_REUSEADDR: 9327 *i1 = tcp->tcp_reuseaddr ? SO_REUSEADDR : 0; 9328 break; 9329 case SO_OOBINLINE: 9330 *i1 = tcp->tcp_oobinline ? SO_OOBINLINE : 0; 9331 break; 9332 case SO_DGRAM_ERRIND: 9333 *i1 = tcp->tcp_dgram_errind ? SO_DGRAM_ERRIND : 0; 9334 break; 9335 case SO_TYPE: 9336 *i1 = SOCK_STREAM; 9337 break; 9338 case SO_SNDBUF: 9339 *i1 = tcp->tcp_xmit_hiwater; 9340 break; 9341 case SO_RCVBUF: 9342 *i1 = RD(q)->q_hiwat; 9343 break; 9344 case SO_SND_COPYAVOID: 9345 *i1 = tcp->tcp_snd_zcopy_on ? 9346 SO_SND_COPYAVOID : 0; 9347 break; 9348 default: 9349 return (-1); 9350 } 9351 break; 9352 case IPPROTO_TCP: 9353 switch (name) { 9354 case TCP_NODELAY: 9355 *i1 = (tcp->tcp_naglim == 1) ? TCP_NODELAY : 0; 9356 break; 9357 case TCP_MAXSEG: 9358 *i1 = tcp->tcp_mss; 9359 break; 9360 case TCP_NOTIFY_THRESHOLD: 9361 *i1 = (int)tcp->tcp_first_timer_threshold; 9362 break; 9363 case TCP_ABORT_THRESHOLD: 9364 *i1 = tcp->tcp_second_timer_threshold; 9365 break; 9366 case TCP_CONN_NOTIFY_THRESHOLD: 9367 *i1 = tcp->tcp_first_ctimer_threshold; 9368 break; 9369 case TCP_CONN_ABORT_THRESHOLD: 9370 *i1 = tcp->tcp_second_ctimer_threshold; 9371 break; 9372 case TCP_RECVDSTADDR: 9373 *i1 = tcp->tcp_recvdstaddr; 9374 break; 9375 case TCP_ANONPRIVBIND: 9376 *i1 = tcp->tcp_anon_priv_bind; 9377 break; 9378 case TCP_EXCLBIND: 9379 *i1 = tcp->tcp_exclbind ? TCP_EXCLBIND : 0; 9380 break; 9381 case TCP_INIT_CWND: 9382 *i1 = tcp->tcp_init_cwnd; 9383 break; 9384 case TCP_KEEPALIVE_THRESHOLD: 9385 *i1 = tcp->tcp_ka_interval; 9386 break; 9387 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9388 *i1 = tcp->tcp_ka_abort_thres; 9389 break; 9390 case TCP_CORK: 9391 *i1 = tcp->tcp_cork; 9392 break; 9393 default: 9394 return (-1); 9395 } 9396 break; 9397 case IPPROTO_IP: 9398 if (tcp->tcp_family != AF_INET) 9399 return (-1); 9400 switch (name) { 9401 case IP_OPTIONS: 9402 case T_IP_OPTIONS: { 9403 /* 9404 * This is compatible with BSD in that in only return 9405 * the reverse source route with the final destination 9406 * as the last entry. The first 4 bytes of the option 9407 * will contain the final destination. 9408 */ 9409 char *opt_ptr; 9410 int opt_len; 9411 opt_ptr = (char *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 9412 opt_len = (char *)tcp->tcp_tcph - opt_ptr; 9413 /* Caller ensures enough space */ 9414 if (opt_len > 0) { 9415 /* 9416 * TODO: Do we have to handle getsockopt on an 9417 * initiator as well? 9418 */ 9419 return (tcp_opt_get_user(tcp->tcp_ipha, ptr)); 9420 } 9421 return (0); 9422 } 9423 case IP_TOS: 9424 case T_IP_TOS: 9425 *i1 = (int)tcp->tcp_ipha->ipha_type_of_service; 9426 break; 9427 case IP_TTL: 9428 *i1 = (int)tcp->tcp_ipha->ipha_ttl; 9429 break; 9430 case IP_NEXTHOP: 9431 /* Handled at IP level */ 9432 return (-EINVAL); 9433 default: 9434 return (-1); 9435 } 9436 break; 9437 case IPPROTO_IPV6: 9438 /* 9439 * IPPROTO_IPV6 options are only supported for sockets 9440 * that are using IPv6 on the wire. 9441 */ 9442 if (tcp->tcp_ipversion != IPV6_VERSION) { 9443 return (-1); 9444 } 9445 switch (name) { 9446 case IPV6_UNICAST_HOPS: 9447 *i1 = (unsigned int) tcp->tcp_ip6h->ip6_hops; 9448 break; /* goto sizeof (int) option return */ 9449 case IPV6_BOUND_IF: 9450 /* Zero if not set */ 9451 *i1 = tcp->tcp_bound_if; 9452 break; /* goto sizeof (int) option return */ 9453 case IPV6_RECVPKTINFO: 9454 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) 9455 *i1 = 1; 9456 else 9457 *i1 = 0; 9458 break; /* goto sizeof (int) option return */ 9459 case IPV6_RECVTCLASS: 9460 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS) 9461 *i1 = 1; 9462 else 9463 *i1 = 0; 9464 break; /* goto sizeof (int) option return */ 9465 case IPV6_RECVHOPLIMIT: 9466 if (tcp->tcp_ipv6_recvancillary & 9467 TCP_IPV6_RECVHOPLIMIT) 9468 *i1 = 1; 9469 else 9470 *i1 = 0; 9471 break; /* goto sizeof (int) option return */ 9472 case IPV6_RECVHOPOPTS: 9473 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) 9474 *i1 = 1; 9475 else 9476 *i1 = 0; 9477 break; /* goto sizeof (int) option return */ 9478 case IPV6_RECVDSTOPTS: 9479 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVDSTOPTS) 9480 *i1 = 1; 9481 else 9482 *i1 = 0; 9483 break; /* goto sizeof (int) option return */ 9484 case _OLD_IPV6_RECVDSTOPTS: 9485 if (tcp->tcp_ipv6_recvancillary & 9486 TCP_OLD_IPV6_RECVDSTOPTS) 9487 *i1 = 1; 9488 else 9489 *i1 = 0; 9490 break; /* goto sizeof (int) option return */ 9491 case IPV6_RECVRTHDR: 9492 if (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) 9493 *i1 = 1; 9494 else 9495 *i1 = 0; 9496 break; /* goto sizeof (int) option return */ 9497 case IPV6_RECVRTHDRDSTOPTS: 9498 if (tcp->tcp_ipv6_recvancillary & 9499 TCP_IPV6_RECVRTDSTOPTS) 9500 *i1 = 1; 9501 else 9502 *i1 = 0; 9503 break; /* goto sizeof (int) option return */ 9504 case IPV6_PKTINFO: { 9505 /* XXX assumes that caller has room for max size! */ 9506 struct in6_pktinfo *pkti; 9507 9508 pkti = (struct in6_pktinfo *)ptr; 9509 if (ipp->ipp_fields & IPPF_IFINDEX) 9510 pkti->ipi6_ifindex = ipp->ipp_ifindex; 9511 else 9512 pkti->ipi6_ifindex = 0; 9513 if (ipp->ipp_fields & IPPF_ADDR) 9514 pkti->ipi6_addr = ipp->ipp_addr; 9515 else 9516 pkti->ipi6_addr = ipv6_all_zeros; 9517 return (sizeof (struct in6_pktinfo)); 9518 } 9519 case IPV6_TCLASS: 9520 if (ipp->ipp_fields & IPPF_TCLASS) 9521 *i1 = ipp->ipp_tclass; 9522 else 9523 *i1 = IPV6_FLOW_TCLASS( 9524 IPV6_DEFAULT_VERS_AND_FLOW); 9525 break; /* goto sizeof (int) option return */ 9526 case IPV6_NEXTHOP: { 9527 sin6_t *sin6 = (sin6_t *)ptr; 9528 9529 if (!(ipp->ipp_fields & IPPF_NEXTHOP)) 9530 return (0); 9531 *sin6 = sin6_null; 9532 sin6->sin6_family = AF_INET6; 9533 sin6->sin6_addr = ipp->ipp_nexthop; 9534 return (sizeof (sin6_t)); 9535 } 9536 case IPV6_HOPOPTS: 9537 if (!(ipp->ipp_fields & IPPF_HOPOPTS)) 9538 return (0); 9539 bcopy(ipp->ipp_hopopts, ptr, ipp->ipp_hopoptslen); 9540 return (ipp->ipp_hopoptslen); 9541 case IPV6_RTHDRDSTOPTS: 9542 if (!(ipp->ipp_fields & IPPF_RTDSTOPTS)) 9543 return (0); 9544 bcopy(ipp->ipp_rtdstopts, ptr, ipp->ipp_rtdstoptslen); 9545 return (ipp->ipp_rtdstoptslen); 9546 case IPV6_RTHDR: 9547 if (!(ipp->ipp_fields & IPPF_RTHDR)) 9548 return (0); 9549 bcopy(ipp->ipp_rthdr, ptr, ipp->ipp_rthdrlen); 9550 return (ipp->ipp_rthdrlen); 9551 case IPV6_DSTOPTS: 9552 if (!(ipp->ipp_fields & IPPF_DSTOPTS)) 9553 return (0); 9554 bcopy(ipp->ipp_dstopts, ptr, ipp->ipp_dstoptslen); 9555 return (ipp->ipp_dstoptslen); 9556 case IPV6_SRC_PREFERENCES: 9557 return (ip6_get_src_preferences(connp, 9558 (uint32_t *)ptr)); 9559 case IPV6_PATHMTU: { 9560 struct ip6_mtuinfo *mtuinfo = (struct ip6_mtuinfo *)ptr; 9561 9562 if (tcp->tcp_state < TCPS_ESTABLISHED) 9563 return (-1); 9564 9565 return (ip_fill_mtuinfo(&connp->conn_remv6, 9566 connp->conn_fport, mtuinfo)); 9567 } 9568 default: 9569 return (-1); 9570 } 9571 break; 9572 default: 9573 return (-1); 9574 } 9575 return (sizeof (int)); 9576 } 9577 9578 /* 9579 * We declare as 'int' rather than 'void' to satisfy pfi_t arg requirements. 9580 * Parameters are assumed to be verified by the caller. 9581 */ 9582 /* ARGSUSED */ 9583 int 9584 tcp_opt_set(queue_t *q, uint_t optset_context, int level, int name, 9585 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, 9586 void *thisdg_attrs, cred_t *cr, mblk_t *mblk) 9587 { 9588 tcp_t *tcp = Q_TO_TCP(q); 9589 int *i1 = (int *)invalp; 9590 boolean_t onoff = (*i1 == 0) ? 0 : 1; 9591 boolean_t checkonly; 9592 int reterr; 9593 9594 switch (optset_context) { 9595 case SETFN_OPTCOM_CHECKONLY: 9596 checkonly = B_TRUE; 9597 /* 9598 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ 9599 * inlen != 0 implies value supplied and 9600 * we have to "pretend" to set it. 9601 * inlen == 0 implies that there is no 9602 * value part in T_CHECK request and just validation 9603 * done elsewhere should be enough, we just return here. 9604 */ 9605 if (inlen == 0) { 9606 *outlenp = 0; 9607 return (0); 9608 } 9609 break; 9610 case SETFN_OPTCOM_NEGOTIATE: 9611 checkonly = B_FALSE; 9612 break; 9613 case SETFN_UD_NEGOTIATE: /* error on conn-oriented transports ? */ 9614 case SETFN_CONN_NEGOTIATE: 9615 checkonly = B_FALSE; 9616 /* 9617 * Negotiating local and "association-related" options 9618 * from other (T_CONN_REQ, T_CONN_RES,T_UNITDATA_REQ) 9619 * primitives is allowed by XTI, but we choose 9620 * to not implement this style negotiation for Internet 9621 * protocols (We interpret it is a must for OSI world but 9622 * optional for Internet protocols) for all options. 9623 * [ Will do only for the few options that enable test 9624 * suites that our XTI implementation of this feature 9625 * works for transports that do allow it ] 9626 */ 9627 if (!tcp_allow_connopt_set(level, name)) { 9628 *outlenp = 0; 9629 return (EINVAL); 9630 } 9631 break; 9632 default: 9633 /* 9634 * We should never get here 9635 */ 9636 *outlenp = 0; 9637 return (EINVAL); 9638 } 9639 9640 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) || 9641 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0)); 9642 9643 /* 9644 * For TCP, we should have no ancillary data sent down 9645 * (sendmsg isn't supported for SOCK_STREAM), so thisdg_attrs 9646 * has to be zero. 9647 */ 9648 ASSERT(thisdg_attrs == NULL); 9649 9650 /* 9651 * For fixed length options, no sanity check 9652 * of passed in length is done. It is assumed *_optcom_req() 9653 * routines do the right thing. 9654 */ 9655 9656 switch (level) { 9657 case SOL_SOCKET: 9658 switch (name) { 9659 case SO_LINGER: { 9660 struct linger *lgr = (struct linger *)invalp; 9661 9662 if (!checkonly) { 9663 if (lgr->l_onoff) { 9664 tcp->tcp_linger = 1; 9665 tcp->tcp_lingertime = lgr->l_linger; 9666 } else { 9667 tcp->tcp_linger = 0; 9668 tcp->tcp_lingertime = 0; 9669 } 9670 /* struct copy */ 9671 *(struct linger *)outvalp = *lgr; 9672 } else { 9673 if (!lgr->l_onoff) { 9674 ((struct linger *)outvalp)->l_onoff = 0; 9675 ((struct linger *)outvalp)->l_linger = 0; 9676 } else { 9677 /* struct copy */ 9678 *(struct linger *)outvalp = *lgr; 9679 } 9680 } 9681 *outlenp = sizeof (struct linger); 9682 return (0); 9683 } 9684 case SO_DEBUG: 9685 if (!checkonly) 9686 tcp->tcp_debug = onoff; 9687 break; 9688 case SO_KEEPALIVE: 9689 if (checkonly) { 9690 /* T_CHECK case */ 9691 break; 9692 } 9693 9694 if (!onoff) { 9695 if (tcp->tcp_ka_enabled) { 9696 if (tcp->tcp_ka_tid != 0) { 9697 (void) TCP_TIMER_CANCEL(tcp, 9698 tcp->tcp_ka_tid); 9699 tcp->tcp_ka_tid = 0; 9700 } 9701 tcp->tcp_ka_enabled = 0; 9702 } 9703 break; 9704 } 9705 if (!tcp->tcp_ka_enabled) { 9706 /* Crank up the keepalive timer */ 9707 tcp->tcp_ka_last_intrvl = 0; 9708 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9709 tcp_keepalive_killer, 9710 MSEC_TO_TICK(tcp->tcp_ka_interval)); 9711 tcp->tcp_ka_enabled = 1; 9712 } 9713 break; 9714 case SO_DONTROUTE: 9715 /* 9716 * SO_DONTROUTE, SO_USELOOPBACK and SO_BROADCAST are 9717 * only of interest to IP. We track them here only so 9718 * that we can report their current value. 9719 */ 9720 if (!checkonly) { 9721 tcp->tcp_dontroute = onoff; 9722 tcp->tcp_connp->conn_dontroute = onoff; 9723 } 9724 break; 9725 case SO_USELOOPBACK: 9726 if (!checkonly) { 9727 tcp->tcp_useloopback = onoff; 9728 tcp->tcp_connp->conn_loopback = onoff; 9729 } 9730 break; 9731 case SO_BROADCAST: 9732 if (!checkonly) { 9733 tcp->tcp_broadcast = onoff; 9734 tcp->tcp_connp->conn_broadcast = onoff; 9735 } 9736 break; 9737 case SO_REUSEADDR: 9738 if (!checkonly) { 9739 tcp->tcp_reuseaddr = onoff; 9740 tcp->tcp_connp->conn_reuseaddr = onoff; 9741 } 9742 break; 9743 case SO_OOBINLINE: 9744 if (!checkonly) 9745 tcp->tcp_oobinline = onoff; 9746 break; 9747 case SO_DGRAM_ERRIND: 9748 if (!checkonly) 9749 tcp->tcp_dgram_errind = onoff; 9750 break; 9751 case SO_SNDBUF: { 9752 tcp_t *peer_tcp; 9753 9754 if (*i1 > tcp_max_buf) { 9755 *outlenp = 0; 9756 return (ENOBUFS); 9757 } 9758 if (checkonly) 9759 break; 9760 9761 tcp->tcp_xmit_hiwater = *i1; 9762 if (tcp_snd_lowat_fraction != 0) 9763 tcp->tcp_xmit_lowater = 9764 tcp->tcp_xmit_hiwater / 9765 tcp_snd_lowat_fraction; 9766 (void) tcp_maxpsz_set(tcp, B_TRUE); 9767 /* 9768 * If we are flow-controlled, recheck the condition. 9769 * There are apps that increase SO_SNDBUF size when 9770 * flow-controlled (EWOULDBLOCK), and expect the flow 9771 * control condition to be lifted right away. 9772 * 9773 * For the fused tcp loopback case, in order to avoid 9774 * a race with the peer's tcp_fuse_rrw() we need to 9775 * hold its fuse_lock while accessing tcp_flow_stopped. 9776 */ 9777 peer_tcp = tcp->tcp_loopback_peer; 9778 ASSERT(!tcp->tcp_fused || peer_tcp != NULL); 9779 if (tcp->tcp_fused) 9780 mutex_enter(&peer_tcp->tcp_fuse_lock); 9781 9782 if (tcp->tcp_flow_stopped && 9783 TCP_UNSENT_BYTES(tcp) < tcp->tcp_xmit_hiwater) { 9784 tcp_clrqfull(tcp); 9785 } 9786 if (tcp->tcp_fused) 9787 mutex_exit(&peer_tcp->tcp_fuse_lock); 9788 break; 9789 } 9790 case SO_RCVBUF: 9791 if (*i1 > tcp_max_buf) { 9792 *outlenp = 0; 9793 return (ENOBUFS); 9794 } 9795 /* Silently ignore zero */ 9796 if (!checkonly && *i1 != 0) { 9797 *i1 = MSS_ROUNDUP(*i1, tcp->tcp_mss); 9798 (void) tcp_rwnd_set(tcp, *i1); 9799 } 9800 /* 9801 * XXX should we return the rwnd here 9802 * and tcp_opt_get ? 9803 */ 9804 break; 9805 case SO_SND_COPYAVOID: 9806 if (!checkonly) { 9807 /* we only allow enable at most once for now */ 9808 if (tcp->tcp_loopback || 9809 (!tcp->tcp_snd_zcopy_aware && 9810 (onoff != 1 || !tcp_zcopy_check(tcp)))) { 9811 *outlenp = 0; 9812 return (EOPNOTSUPP); 9813 } 9814 tcp->tcp_snd_zcopy_aware = 1; 9815 } 9816 break; 9817 default: 9818 *outlenp = 0; 9819 return (EINVAL); 9820 } 9821 break; 9822 case IPPROTO_TCP: 9823 switch (name) { 9824 case TCP_NODELAY: 9825 if (!checkonly) 9826 tcp->tcp_naglim = *i1 ? 1 : tcp->tcp_mss; 9827 break; 9828 case TCP_NOTIFY_THRESHOLD: 9829 if (!checkonly) 9830 tcp->tcp_first_timer_threshold = *i1; 9831 break; 9832 case TCP_ABORT_THRESHOLD: 9833 if (!checkonly) 9834 tcp->tcp_second_timer_threshold = *i1; 9835 break; 9836 case TCP_CONN_NOTIFY_THRESHOLD: 9837 if (!checkonly) 9838 tcp->tcp_first_ctimer_threshold = *i1; 9839 break; 9840 case TCP_CONN_ABORT_THRESHOLD: 9841 if (!checkonly) 9842 tcp->tcp_second_ctimer_threshold = *i1; 9843 break; 9844 case TCP_RECVDSTADDR: 9845 if (tcp->tcp_state > TCPS_LISTEN) 9846 return (EOPNOTSUPP); 9847 if (!checkonly) 9848 tcp->tcp_recvdstaddr = onoff; 9849 break; 9850 case TCP_ANONPRIVBIND: 9851 if ((reterr = secpolicy_net_privaddr(cr, 0)) != 0) { 9852 *outlenp = 0; 9853 return (reterr); 9854 } 9855 if (!checkonly) { 9856 tcp->tcp_anon_priv_bind = onoff; 9857 } 9858 break; 9859 case TCP_EXCLBIND: 9860 if (!checkonly) 9861 tcp->tcp_exclbind = onoff; 9862 break; /* goto sizeof (int) option return */ 9863 case TCP_INIT_CWND: { 9864 uint32_t init_cwnd = *((uint32_t *)invalp); 9865 9866 if (checkonly) 9867 break; 9868 9869 /* 9870 * Only allow socket with network configuration 9871 * privilege to set the initial cwnd to be larger 9872 * than allowed by RFC 3390. 9873 */ 9874 if (init_cwnd <= MIN(4, MAX(2, 4380 / tcp->tcp_mss))) { 9875 tcp->tcp_init_cwnd = init_cwnd; 9876 break; 9877 } 9878 if ((reterr = secpolicy_net_config(cr, B_TRUE)) != 0) { 9879 *outlenp = 0; 9880 return (reterr); 9881 } 9882 if (init_cwnd > TCP_MAX_INIT_CWND) { 9883 *outlenp = 0; 9884 return (EINVAL); 9885 } 9886 tcp->tcp_init_cwnd = init_cwnd; 9887 break; 9888 } 9889 case TCP_KEEPALIVE_THRESHOLD: 9890 if (checkonly) 9891 break; 9892 9893 if (*i1 < tcp_keepalive_interval_low || 9894 *i1 > tcp_keepalive_interval_high) { 9895 *outlenp = 0; 9896 return (EINVAL); 9897 } 9898 if (*i1 != tcp->tcp_ka_interval) { 9899 tcp->tcp_ka_interval = *i1; 9900 /* 9901 * Check if we need to restart the 9902 * keepalive timer. 9903 */ 9904 if (tcp->tcp_ka_tid != 0) { 9905 ASSERT(tcp->tcp_ka_enabled); 9906 (void) TCP_TIMER_CANCEL(tcp, 9907 tcp->tcp_ka_tid); 9908 tcp->tcp_ka_last_intrvl = 0; 9909 tcp->tcp_ka_tid = TCP_TIMER(tcp, 9910 tcp_keepalive_killer, 9911 MSEC_TO_TICK(tcp->tcp_ka_interval)); 9912 } 9913 } 9914 break; 9915 case TCP_KEEPALIVE_ABORT_THRESHOLD: 9916 if (!checkonly) { 9917 if (*i1 < tcp_keepalive_abort_interval_low || 9918 *i1 > tcp_keepalive_abort_interval_high) { 9919 *outlenp = 0; 9920 return (EINVAL); 9921 } 9922 tcp->tcp_ka_abort_thres = *i1; 9923 } 9924 break; 9925 case TCP_CORK: 9926 if (!checkonly) { 9927 /* 9928 * if tcp->tcp_cork was set and is now 9929 * being unset, we have to make sure that 9930 * the remaining data gets sent out. Also 9931 * unset tcp->tcp_cork so that tcp_wput_data() 9932 * can send data even if it is less than mss 9933 */ 9934 if (tcp->tcp_cork && onoff == 0 && 9935 tcp->tcp_unsent > 0) { 9936 tcp->tcp_cork = B_FALSE; 9937 tcp_wput_data(tcp, NULL, B_FALSE); 9938 } 9939 tcp->tcp_cork = onoff; 9940 } 9941 break; 9942 default: 9943 *outlenp = 0; 9944 return (EINVAL); 9945 } 9946 break; 9947 case IPPROTO_IP: 9948 if (tcp->tcp_family != AF_INET) { 9949 *outlenp = 0; 9950 return (ENOPROTOOPT); 9951 } 9952 switch (name) { 9953 case IP_OPTIONS: 9954 case T_IP_OPTIONS: 9955 reterr = tcp_opt_set_header(tcp, checkonly, 9956 invalp, inlen); 9957 if (reterr) { 9958 *outlenp = 0; 9959 return (reterr); 9960 } 9961 /* OK return - copy input buffer into output buffer */ 9962 if (invalp != outvalp) { 9963 /* don't trust bcopy for identical src/dst */ 9964 bcopy(invalp, outvalp, inlen); 9965 } 9966 *outlenp = inlen; 9967 return (0); 9968 case IP_TOS: 9969 case T_IP_TOS: 9970 if (!checkonly) { 9971 tcp->tcp_ipha->ipha_type_of_service = 9972 (uchar_t)*i1; 9973 tcp->tcp_tos = (uchar_t)*i1; 9974 } 9975 break; 9976 case IP_TTL: 9977 if (!checkonly) { 9978 tcp->tcp_ipha->ipha_ttl = (uchar_t)*i1; 9979 tcp->tcp_ttl = (uchar_t)*i1; 9980 } 9981 break; 9982 case IP_BOUND_IF: 9983 case IP_NEXTHOP: 9984 /* Handled at the IP level */ 9985 return (-EINVAL); 9986 case IP_SEC_OPT: 9987 /* 9988 * We should not allow policy setting after 9989 * we start listening for connections. 9990 */ 9991 if (tcp->tcp_state == TCPS_LISTEN) { 9992 return (EINVAL); 9993 } else { 9994 /* Handled at the IP level */ 9995 return (-EINVAL); 9996 } 9997 default: 9998 *outlenp = 0; 9999 return (EINVAL); 10000 } 10001 break; 10002 case IPPROTO_IPV6: { 10003 ip6_pkt_t *ipp; 10004 10005 /* 10006 * IPPROTO_IPV6 options are only supported for sockets 10007 * that are using IPv6 on the wire. 10008 */ 10009 if (tcp->tcp_ipversion != IPV6_VERSION) { 10010 *outlenp = 0; 10011 return (ENOPROTOOPT); 10012 } 10013 /* 10014 * Only sticky options; no ancillary data 10015 */ 10016 ASSERT(thisdg_attrs == NULL); 10017 ipp = &tcp->tcp_sticky_ipp; 10018 10019 switch (name) { 10020 case IPV6_UNICAST_HOPS: 10021 /* -1 means use default */ 10022 if (*i1 < -1 || *i1 > IPV6_MAX_HOPS) { 10023 *outlenp = 0; 10024 return (EINVAL); 10025 } 10026 if (!checkonly) { 10027 if (*i1 == -1) { 10028 tcp->tcp_ip6h->ip6_hops = 10029 ipp->ipp_unicast_hops = 10030 (uint8_t)tcp_ipv6_hoplimit; 10031 ipp->ipp_fields &= ~IPPF_UNICAST_HOPS; 10032 /* Pass modified value to IP. */ 10033 *i1 = tcp->tcp_ip6h->ip6_hops; 10034 } else { 10035 tcp->tcp_ip6h->ip6_hops = 10036 ipp->ipp_unicast_hops = 10037 (uint8_t)*i1; 10038 ipp->ipp_fields |= IPPF_UNICAST_HOPS; 10039 } 10040 reterr = tcp_build_hdrs(q, tcp); 10041 if (reterr != 0) 10042 return (reterr); 10043 } 10044 break; 10045 case IPV6_BOUND_IF: 10046 if (!checkonly) { 10047 int error = 0; 10048 10049 tcp->tcp_bound_if = *i1; 10050 error = ip_opt_set_ill(tcp->tcp_connp, *i1, 10051 B_TRUE, checkonly, level, name, mblk); 10052 if (error != 0) { 10053 *outlenp = 0; 10054 return (error); 10055 } 10056 } 10057 break; 10058 /* 10059 * Set boolean switches for ancillary data delivery 10060 */ 10061 case IPV6_RECVPKTINFO: 10062 if (!checkonly) { 10063 if (onoff) 10064 tcp->tcp_ipv6_recvancillary |= 10065 TCP_IPV6_RECVPKTINFO; 10066 else 10067 tcp->tcp_ipv6_recvancillary &= 10068 ~TCP_IPV6_RECVPKTINFO; 10069 /* Force it to be sent up with the next msg */ 10070 tcp->tcp_recvifindex = 0; 10071 } 10072 break; 10073 case IPV6_RECVTCLASS: 10074 if (!checkonly) { 10075 if (onoff) 10076 tcp->tcp_ipv6_recvancillary |= 10077 TCP_IPV6_RECVTCLASS; 10078 else 10079 tcp->tcp_ipv6_recvancillary &= 10080 ~TCP_IPV6_RECVTCLASS; 10081 } 10082 break; 10083 case IPV6_RECVHOPLIMIT: 10084 if (!checkonly) { 10085 if (onoff) 10086 tcp->tcp_ipv6_recvancillary |= 10087 TCP_IPV6_RECVHOPLIMIT; 10088 else 10089 tcp->tcp_ipv6_recvancillary &= 10090 ~TCP_IPV6_RECVHOPLIMIT; 10091 /* Force it to be sent up with the next msg */ 10092 tcp->tcp_recvhops = 0xffffffffU; 10093 } 10094 break; 10095 case IPV6_RECVHOPOPTS: 10096 if (!checkonly) { 10097 if (onoff) 10098 tcp->tcp_ipv6_recvancillary |= 10099 TCP_IPV6_RECVHOPOPTS; 10100 else 10101 tcp->tcp_ipv6_recvancillary &= 10102 ~TCP_IPV6_RECVHOPOPTS; 10103 } 10104 break; 10105 case IPV6_RECVDSTOPTS: 10106 if (!checkonly) { 10107 if (onoff) 10108 tcp->tcp_ipv6_recvancillary |= 10109 TCP_IPV6_RECVDSTOPTS; 10110 else 10111 tcp->tcp_ipv6_recvancillary &= 10112 ~TCP_IPV6_RECVDSTOPTS; 10113 } 10114 break; 10115 case _OLD_IPV6_RECVDSTOPTS: 10116 if (!checkonly) { 10117 if (onoff) 10118 tcp->tcp_ipv6_recvancillary |= 10119 TCP_OLD_IPV6_RECVDSTOPTS; 10120 else 10121 tcp->tcp_ipv6_recvancillary &= 10122 ~TCP_OLD_IPV6_RECVDSTOPTS; 10123 } 10124 break; 10125 case IPV6_RECVRTHDR: 10126 if (!checkonly) { 10127 if (onoff) 10128 tcp->tcp_ipv6_recvancillary |= 10129 TCP_IPV6_RECVRTHDR; 10130 else 10131 tcp->tcp_ipv6_recvancillary &= 10132 ~TCP_IPV6_RECVRTHDR; 10133 } 10134 break; 10135 case IPV6_RECVRTHDRDSTOPTS: 10136 if (!checkonly) { 10137 if (onoff) 10138 tcp->tcp_ipv6_recvancillary |= 10139 TCP_IPV6_RECVRTDSTOPTS; 10140 else 10141 tcp->tcp_ipv6_recvancillary &= 10142 ~TCP_IPV6_RECVRTDSTOPTS; 10143 } 10144 break; 10145 case IPV6_PKTINFO: 10146 if (inlen != 0 && inlen != sizeof (struct in6_pktinfo)) 10147 return (EINVAL); 10148 if (checkonly) 10149 break; 10150 10151 if (inlen == 0) { 10152 ipp->ipp_fields &= ~(IPPF_IFINDEX|IPPF_ADDR); 10153 } else { 10154 struct in6_pktinfo *pkti; 10155 10156 pkti = (struct in6_pktinfo *)invalp; 10157 /* 10158 * RFC 3542 states that ipi6_addr must be 10159 * the unspecified address when setting the 10160 * IPV6_PKTINFO sticky socket option on a 10161 * TCP socket. 10162 */ 10163 if (!IN6_IS_ADDR_UNSPECIFIED(&pkti->ipi6_addr)) 10164 return (EINVAL); 10165 /* 10166 * ip6_set_pktinfo() validates the source 10167 * address and interface index. 10168 */ 10169 reterr = ip6_set_pktinfo(cr, tcp->tcp_connp, 10170 pkti, mblk); 10171 if (reterr != 0) 10172 return (reterr); 10173 ipp->ipp_ifindex = pkti->ipi6_ifindex; 10174 ipp->ipp_addr = pkti->ipi6_addr; 10175 if (ipp->ipp_ifindex != 0) 10176 ipp->ipp_fields |= IPPF_IFINDEX; 10177 else 10178 ipp->ipp_fields &= ~IPPF_IFINDEX; 10179 if (!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr)) 10180 ipp->ipp_fields |= IPPF_ADDR; 10181 else 10182 ipp->ipp_fields &= ~IPPF_ADDR; 10183 } 10184 reterr = tcp_build_hdrs(q, tcp); 10185 if (reterr != 0) 10186 return (reterr); 10187 break; 10188 case IPV6_TCLASS: 10189 if (inlen != 0 && inlen != sizeof (int)) 10190 return (EINVAL); 10191 if (checkonly) 10192 break; 10193 10194 if (inlen == 0) { 10195 ipp->ipp_fields &= ~IPPF_TCLASS; 10196 } else { 10197 if (*i1 > 255 || *i1 < -1) 10198 return (EINVAL); 10199 if (*i1 == -1) { 10200 ipp->ipp_tclass = 0; 10201 *i1 = 0; 10202 } else { 10203 ipp->ipp_tclass = *i1; 10204 } 10205 ipp->ipp_fields |= IPPF_TCLASS; 10206 } 10207 reterr = tcp_build_hdrs(q, tcp); 10208 if (reterr != 0) 10209 return (reterr); 10210 break; 10211 case IPV6_NEXTHOP: 10212 /* 10213 * IP will verify that the nexthop is reachable 10214 * and fail for sticky options. 10215 */ 10216 if (inlen != 0 && inlen != sizeof (sin6_t)) 10217 return (EINVAL); 10218 if (checkonly) 10219 break; 10220 10221 if (inlen == 0) { 10222 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10223 } else { 10224 sin6_t *sin6 = (sin6_t *)invalp; 10225 10226 if (sin6->sin6_family != AF_INET6) 10227 return (EAFNOSUPPORT); 10228 if (IN6_IS_ADDR_V4MAPPED( 10229 &sin6->sin6_addr)) 10230 return (EADDRNOTAVAIL); 10231 ipp->ipp_nexthop = sin6->sin6_addr; 10232 if (!IN6_IS_ADDR_UNSPECIFIED( 10233 &ipp->ipp_nexthop)) 10234 ipp->ipp_fields |= IPPF_NEXTHOP; 10235 else 10236 ipp->ipp_fields &= ~IPPF_NEXTHOP; 10237 } 10238 reterr = tcp_build_hdrs(q, tcp); 10239 if (reterr != 0) 10240 return (reterr); 10241 break; 10242 case IPV6_HOPOPTS: { 10243 ip6_hbh_t *hopts = (ip6_hbh_t *)invalp; 10244 /* 10245 * Sanity checks - minimum size, size a multiple of 10246 * eight bytes, and matching size passed in. 10247 */ 10248 if (inlen != 0 && 10249 inlen != (8 * (hopts->ip6h_len + 1))) 10250 return (EINVAL); 10251 10252 if (checkonly) 10253 break; 10254 10255 if (inlen == 0) { 10256 if ((ipp->ipp_fields & IPPF_HOPOPTS) != 0) { 10257 kmem_free(ipp->ipp_hopopts, 10258 ipp->ipp_hopoptslen); 10259 ipp->ipp_hopopts = NULL; 10260 ipp->ipp_hopoptslen = 0; 10261 } 10262 ipp->ipp_fields &= ~IPPF_HOPOPTS; 10263 } else { 10264 reterr = tcp_pkt_set(invalp, inlen, 10265 (uchar_t **)&ipp->ipp_hopopts, 10266 &ipp->ipp_hopoptslen); 10267 if (reterr != 0) 10268 return (reterr); 10269 ipp->ipp_fields |= IPPF_HOPOPTS; 10270 } 10271 reterr = tcp_build_hdrs(q, tcp); 10272 if (reterr != 0) 10273 return (reterr); 10274 break; 10275 } 10276 case IPV6_RTHDRDSTOPTS: { 10277 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10278 10279 /* 10280 * Sanity checks - minimum size, size a multiple of 10281 * eight bytes, and matching size passed in. 10282 */ 10283 if (inlen != 0 && 10284 inlen != (8 * (dopts->ip6d_len + 1))) 10285 return (EINVAL); 10286 10287 if (checkonly) 10288 break; 10289 10290 if (inlen == 0) { 10291 if ((ipp->ipp_fields & IPPF_RTDSTOPTS) != 0) { 10292 kmem_free(ipp->ipp_rtdstopts, 10293 ipp->ipp_rtdstoptslen); 10294 ipp->ipp_rtdstopts = NULL; 10295 ipp->ipp_rtdstoptslen = 0; 10296 } 10297 ipp->ipp_fields &= ~IPPF_RTDSTOPTS; 10298 } else { 10299 reterr = tcp_pkt_set(invalp, inlen, 10300 (uchar_t **)&ipp->ipp_rtdstopts, 10301 &ipp->ipp_rtdstoptslen); 10302 if (reterr != 0) 10303 return (reterr); 10304 ipp->ipp_fields |= IPPF_RTDSTOPTS; 10305 } 10306 reterr = tcp_build_hdrs(q, tcp); 10307 if (reterr != 0) 10308 return (reterr); 10309 break; 10310 } 10311 case IPV6_DSTOPTS: { 10312 ip6_dest_t *dopts = (ip6_dest_t *)invalp; 10313 10314 /* 10315 * Sanity checks - minimum size, size a multiple of 10316 * eight bytes, and matching size passed in. 10317 */ 10318 if (inlen != 0 && 10319 inlen != (8 * (dopts->ip6d_len + 1))) 10320 return (EINVAL); 10321 10322 if (checkonly) 10323 break; 10324 10325 if (inlen == 0) { 10326 if ((ipp->ipp_fields & IPPF_DSTOPTS) != 0) { 10327 kmem_free(ipp->ipp_dstopts, 10328 ipp->ipp_dstoptslen); 10329 ipp->ipp_dstopts = NULL; 10330 ipp->ipp_dstoptslen = 0; 10331 } 10332 ipp->ipp_fields &= ~IPPF_DSTOPTS; 10333 } else { 10334 reterr = tcp_pkt_set(invalp, inlen, 10335 (uchar_t **)&ipp->ipp_dstopts, 10336 &ipp->ipp_dstoptslen); 10337 if (reterr != 0) 10338 return (reterr); 10339 ipp->ipp_fields |= IPPF_DSTOPTS; 10340 } 10341 reterr = tcp_build_hdrs(q, tcp); 10342 if (reterr != 0) 10343 return (reterr); 10344 break; 10345 } 10346 case IPV6_RTHDR: { 10347 ip6_rthdr_t *rt = (ip6_rthdr_t *)invalp; 10348 10349 /* 10350 * Sanity checks - minimum size, size a multiple of 10351 * eight bytes, and matching size passed in. 10352 */ 10353 if (inlen != 0 && 10354 inlen != (8 * (rt->ip6r_len + 1))) 10355 return (EINVAL); 10356 10357 if (checkonly) 10358 break; 10359 10360 if (inlen == 0) { 10361 if ((ipp->ipp_fields & IPPF_RTHDR) != 0) { 10362 kmem_free(ipp->ipp_rthdr, 10363 ipp->ipp_rthdrlen); 10364 ipp->ipp_rthdr = NULL; 10365 ipp->ipp_rthdrlen = 0; 10366 } 10367 ipp->ipp_fields &= ~IPPF_RTHDR; 10368 } else { 10369 reterr = tcp_pkt_set(invalp, inlen, 10370 (uchar_t **)&ipp->ipp_rthdr, 10371 &ipp->ipp_rthdrlen); 10372 if (reterr != 0) 10373 return (reterr); 10374 ipp->ipp_fields |= IPPF_RTHDR; 10375 } 10376 reterr = tcp_build_hdrs(q, tcp); 10377 if (reterr != 0) 10378 return (reterr); 10379 break; 10380 } 10381 case IPV6_V6ONLY: 10382 if (!checkonly) 10383 tcp->tcp_connp->conn_ipv6_v6only = onoff; 10384 break; 10385 case IPV6_USE_MIN_MTU: 10386 if (inlen != sizeof (int)) 10387 return (EINVAL); 10388 10389 if (*i1 < -1 || *i1 > 1) 10390 return (EINVAL); 10391 10392 if (checkonly) 10393 break; 10394 10395 ipp->ipp_fields |= IPPF_USE_MIN_MTU; 10396 ipp->ipp_use_min_mtu = *i1; 10397 break; 10398 case IPV6_BOUND_PIF: 10399 /* Handled at the IP level */ 10400 return (-EINVAL); 10401 case IPV6_SEC_OPT: 10402 /* 10403 * We should not allow policy setting after 10404 * we start listening for connections. 10405 */ 10406 if (tcp->tcp_state == TCPS_LISTEN) { 10407 return (EINVAL); 10408 } else { 10409 /* Handled at the IP level */ 10410 return (-EINVAL); 10411 } 10412 case IPV6_SRC_PREFERENCES: 10413 if (inlen != sizeof (uint32_t)) 10414 return (EINVAL); 10415 reterr = ip6_set_src_preferences(tcp->tcp_connp, 10416 *(uint32_t *)invalp); 10417 if (reterr != 0) { 10418 *outlenp = 0; 10419 return (reterr); 10420 } 10421 break; 10422 default: 10423 *outlenp = 0; 10424 return (EINVAL); 10425 } 10426 break; 10427 } /* end IPPROTO_IPV6 */ 10428 default: 10429 *outlenp = 0; 10430 return (EINVAL); 10431 } 10432 /* 10433 * Common case of OK return with outval same as inval 10434 */ 10435 if (invalp != outvalp) { 10436 /* don't trust bcopy for identical src/dst */ 10437 (void) bcopy(invalp, outvalp, inlen); 10438 } 10439 *outlenp = inlen; 10440 return (0); 10441 } 10442 10443 /* 10444 * Update tcp_sticky_hdrs based on tcp_sticky_ipp. 10445 * The headers include ip6i_t (if needed), ip6_t, any sticky extension 10446 * headers, and the maximum size tcp header (to avoid reallocation 10447 * on the fly for additional tcp options). 10448 * Returns failure if can't allocate memory. 10449 */ 10450 static int 10451 tcp_build_hdrs(queue_t *q, tcp_t *tcp) 10452 { 10453 char *hdrs; 10454 uint_t hdrs_len; 10455 ip6i_t *ip6i; 10456 char buf[TCP_MAX_HDR_LENGTH]; 10457 ip6_pkt_t *ipp = &tcp->tcp_sticky_ipp; 10458 in6_addr_t src, dst; 10459 10460 /* 10461 * save the existing tcp header and source/dest IP addresses 10462 */ 10463 bcopy(tcp->tcp_tcph, buf, tcp->tcp_tcp_hdr_len); 10464 src = tcp->tcp_ip6h->ip6_src; 10465 dst = tcp->tcp_ip6h->ip6_dst; 10466 hdrs_len = ip_total_hdrs_len_v6(ipp) + TCP_MAX_HDR_LENGTH; 10467 ASSERT(hdrs_len != 0); 10468 if (hdrs_len > tcp->tcp_iphc_len) { 10469 /* Need to reallocate */ 10470 hdrs = kmem_zalloc(hdrs_len, KM_NOSLEEP); 10471 if (hdrs == NULL) 10472 return (ENOMEM); 10473 if (tcp->tcp_iphc != NULL) { 10474 if (tcp->tcp_hdr_grown) { 10475 kmem_free(tcp->tcp_iphc, tcp->tcp_iphc_len); 10476 } else { 10477 bzero(tcp->tcp_iphc, tcp->tcp_iphc_len); 10478 kmem_cache_free(tcp_iphc_cache, tcp->tcp_iphc); 10479 } 10480 tcp->tcp_iphc_len = 0; 10481 } 10482 ASSERT(tcp->tcp_iphc_len == 0); 10483 tcp->tcp_iphc = hdrs; 10484 tcp->tcp_iphc_len = hdrs_len; 10485 tcp->tcp_hdr_grown = B_TRUE; 10486 } 10487 ip_build_hdrs_v6((uchar_t *)tcp->tcp_iphc, 10488 hdrs_len - TCP_MAX_HDR_LENGTH, ipp, IPPROTO_TCP); 10489 10490 /* Set header fields not in ipp */ 10491 if (ipp->ipp_fields & IPPF_HAS_IP6I) { 10492 ip6i = (ip6i_t *)tcp->tcp_iphc; 10493 tcp->tcp_ip6h = (ip6_t *)&ip6i[1]; 10494 } else { 10495 tcp->tcp_ip6h = (ip6_t *)tcp->tcp_iphc; 10496 } 10497 /* 10498 * tcp->tcp_ip_hdr_len will include ip6i_t if there is one. 10499 * 10500 * tcp->tcp_tcp_hdr_len doesn't change here. 10501 */ 10502 tcp->tcp_ip_hdr_len = hdrs_len - TCP_MAX_HDR_LENGTH; 10503 tcp->tcp_tcph = (tcph_t *)(tcp->tcp_iphc + tcp->tcp_ip_hdr_len); 10504 tcp->tcp_hdr_len = tcp->tcp_ip_hdr_len + tcp->tcp_tcp_hdr_len; 10505 10506 bcopy(buf, tcp->tcp_tcph, tcp->tcp_tcp_hdr_len); 10507 10508 tcp->tcp_ip6h->ip6_src = src; 10509 tcp->tcp_ip6h->ip6_dst = dst; 10510 10511 /* 10512 * If the hop limit was not set by ip_build_hdrs_v6(), set it to 10513 * the default value for TCP. 10514 */ 10515 if (!(ipp->ipp_fields & IPPF_UNICAST_HOPS)) 10516 tcp->tcp_ip6h->ip6_hops = tcp_ipv6_hoplimit; 10517 10518 /* 10519 * If we're setting extension headers after a connection 10520 * has been established, and if we have a routing header 10521 * among the extension headers, call ip_massage_options_v6 to 10522 * manipulate the routing header/ip6_dst set the checksum 10523 * difference in the tcp header template. 10524 * (This happens in tcp_connect_ipv6 if the routing header 10525 * is set prior to the connect.) 10526 * Set the tcp_sum to zero first in case we've cleared a 10527 * routing header or don't have one at all. 10528 */ 10529 tcp->tcp_sum = 0; 10530 if ((tcp->tcp_state >= TCPS_SYN_SENT) && 10531 (tcp->tcp_ipp_fields & IPPF_RTHDR)) { 10532 ip6_rthdr_t *rth = ip_find_rthdr_v6(tcp->tcp_ip6h, 10533 (uint8_t *)tcp->tcp_tcph); 10534 if (rth != NULL) { 10535 tcp->tcp_sum = ip_massage_options_v6(tcp->tcp_ip6h, 10536 rth); 10537 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + 10538 (tcp->tcp_sum >> 16)); 10539 } 10540 } 10541 10542 /* Try to get everything in a single mblk */ 10543 (void) mi_set_sth_wroff(RD(q), hdrs_len + tcp_wroff_xtra); 10544 return (0); 10545 } 10546 10547 /* 10548 * Set optbuf and optlen for the option. 10549 * Allocate memory (if not already present). 10550 * Otherwise just point optbuf and optlen at invalp and inlen. 10551 * Returns failure if memory can not be allocated. 10552 */ 10553 static int 10554 tcp_pkt_set(uchar_t *invalp, uint_t inlen, uchar_t **optbufp, uint_t *optlenp) 10555 { 10556 uchar_t *optbuf; 10557 10558 if (inlen == *optlenp) { 10559 /* Unchanged length - no need to realocate */ 10560 bcopy(invalp, *optbufp, inlen); 10561 return (0); 10562 } 10563 if (inlen != 0) { 10564 /* Allocate new buffer before free */ 10565 optbuf = kmem_alloc(inlen, KM_NOSLEEP); 10566 if (optbuf == NULL) 10567 return (ENOMEM); 10568 } else { 10569 optbuf = NULL; 10570 } 10571 /* Free old buffer */ 10572 if (*optlenp != 0) 10573 kmem_free(*optbufp, *optlenp); 10574 10575 bcopy(invalp, optbuf, inlen); 10576 *optbufp = optbuf; 10577 *optlenp = inlen; 10578 return (0); 10579 } 10580 10581 10582 /* 10583 * Use the outgoing IP header to create an IP_OPTIONS option the way 10584 * it was passed down from the application. 10585 */ 10586 static int 10587 tcp_opt_get_user(ipha_t *ipha, uchar_t *buf) 10588 { 10589 ipoptp_t opts; 10590 uchar_t *opt; 10591 uint8_t optval; 10592 uint8_t optlen; 10593 uint32_t len = 0; 10594 uchar_t *buf1 = buf; 10595 10596 buf += IP_ADDR_LEN; /* Leave room for final destination */ 10597 len += IP_ADDR_LEN; 10598 bzero(buf1, IP_ADDR_LEN); 10599 10600 for (optval = ipoptp_first(&opts, ipha); 10601 optval != IPOPT_EOL; 10602 optval = ipoptp_next(&opts)) { 10603 opt = opts.ipoptp_cur; 10604 optlen = opts.ipoptp_len; 10605 switch (optval) { 10606 int off; 10607 case IPOPT_SSRR: 10608 case IPOPT_LSRR: 10609 10610 /* 10611 * Insert ipha_dst as the first entry in the source 10612 * route and move down the entries on step. 10613 * The last entry gets placed at buf1. 10614 */ 10615 buf[IPOPT_OPTVAL] = optval; 10616 buf[IPOPT_OLEN] = optlen; 10617 buf[IPOPT_OFFSET] = optlen; 10618 10619 off = optlen - IP_ADDR_LEN; 10620 if (off < 0) { 10621 /* No entries in source route */ 10622 break; 10623 } 10624 /* Last entry in source route */ 10625 bcopy(opt + off, buf1, IP_ADDR_LEN); 10626 off -= IP_ADDR_LEN; 10627 10628 while (off > 0) { 10629 bcopy(opt + off, 10630 buf + off + IP_ADDR_LEN, 10631 IP_ADDR_LEN); 10632 off -= IP_ADDR_LEN; 10633 } 10634 /* ipha_dst into first slot */ 10635 bcopy(&ipha->ipha_dst, 10636 buf + off + IP_ADDR_LEN, 10637 IP_ADDR_LEN); 10638 buf += optlen; 10639 len += optlen; 10640 break; 10641 default: 10642 bcopy(opt, buf, optlen); 10643 buf += optlen; 10644 len += optlen; 10645 break; 10646 } 10647 } 10648 done: 10649 /* Pad the resulting options */ 10650 while (len & 0x3) { 10651 *buf++ = IPOPT_EOL; 10652 len++; 10653 } 10654 return (len); 10655 } 10656 10657 /* 10658 * Transfer any source route option from ipha to buf/dst in reversed form. 10659 */ 10660 static int 10661 tcp_opt_rev_src_route(ipha_t *ipha, char *buf, uchar_t *dst) 10662 { 10663 ipoptp_t opts; 10664 uchar_t *opt; 10665 uint8_t optval; 10666 uint8_t optlen; 10667 uint32_t len = 0; 10668 10669 for (optval = ipoptp_first(&opts, ipha); 10670 optval != IPOPT_EOL; 10671 optval = ipoptp_next(&opts)) { 10672 opt = opts.ipoptp_cur; 10673 optlen = opts.ipoptp_len; 10674 switch (optval) { 10675 int off1, off2; 10676 case IPOPT_SSRR: 10677 case IPOPT_LSRR: 10678 10679 /* Reverse source route */ 10680 /* 10681 * First entry should be the next to last one in the 10682 * current source route (the last entry is our 10683 * address.) 10684 * The last entry should be the final destination. 10685 */ 10686 buf[IPOPT_OPTVAL] = (uint8_t)optval; 10687 buf[IPOPT_OLEN] = (uint8_t)optlen; 10688 off1 = IPOPT_MINOFF_SR - 1; 10689 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1; 10690 if (off2 < 0) { 10691 /* No entries in source route */ 10692 break; 10693 } 10694 bcopy(opt + off2, dst, IP_ADDR_LEN); 10695 /* 10696 * Note: use src since ipha has not had its src 10697 * and dst reversed (it is in the state it was 10698 * received. 10699 */ 10700 bcopy(&ipha->ipha_src, buf + off2, 10701 IP_ADDR_LEN); 10702 off2 -= IP_ADDR_LEN; 10703 10704 while (off2 > 0) { 10705 bcopy(opt + off2, buf + off1, 10706 IP_ADDR_LEN); 10707 off1 += IP_ADDR_LEN; 10708 off2 -= IP_ADDR_LEN; 10709 } 10710 buf[IPOPT_OFFSET] = IPOPT_MINOFF_SR; 10711 buf += optlen; 10712 len += optlen; 10713 break; 10714 } 10715 } 10716 done: 10717 /* Pad the resulting options */ 10718 while (len & 0x3) { 10719 *buf++ = IPOPT_EOL; 10720 len++; 10721 } 10722 return (len); 10723 } 10724 10725 10726 /* 10727 * Extract and revert a source route from ipha (if any) 10728 * and then update the relevant fields in both tcp_t and the standard header. 10729 */ 10730 static void 10731 tcp_opt_reverse(tcp_t *tcp, ipha_t *ipha) 10732 { 10733 char buf[TCP_MAX_HDR_LENGTH]; 10734 uint_t tcph_len; 10735 int len; 10736 10737 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION); 10738 len = IPH_HDR_LENGTH(ipha); 10739 if (len == IP_SIMPLE_HDR_LENGTH) 10740 /* Nothing to do */ 10741 return; 10742 if (len > IP_SIMPLE_HDR_LENGTH + TCP_MAX_IP_OPTIONS_LENGTH || 10743 (len & 0x3)) 10744 return; 10745 10746 tcph_len = tcp->tcp_tcp_hdr_len; 10747 bcopy(tcp->tcp_tcph, buf, tcph_len); 10748 tcp->tcp_sum = (tcp->tcp_ipha->ipha_dst >> 16) + 10749 (tcp->tcp_ipha->ipha_dst & 0xffff); 10750 len = tcp_opt_rev_src_route(ipha, (char *)tcp->tcp_ipha + 10751 IP_SIMPLE_HDR_LENGTH, (uchar_t *)&tcp->tcp_ipha->ipha_dst); 10752 len += IP_SIMPLE_HDR_LENGTH; 10753 tcp->tcp_sum -= ((tcp->tcp_ipha->ipha_dst >> 16) + 10754 (tcp->tcp_ipha->ipha_dst & 0xffff)); 10755 if ((int)tcp->tcp_sum < 0) 10756 tcp->tcp_sum--; 10757 tcp->tcp_sum = (tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16); 10758 tcp->tcp_sum = ntohs((tcp->tcp_sum & 0xFFFF) + (tcp->tcp_sum >> 16)); 10759 tcp->tcp_tcph = (tcph_t *)((char *)tcp->tcp_ipha + len); 10760 bcopy(buf, tcp->tcp_tcph, tcph_len); 10761 tcp->tcp_ip_hdr_len = len; 10762 tcp->tcp_ipha->ipha_version_and_hdr_length = 10763 (IP_VERSION << 4) | (len >> 2); 10764 len += tcph_len; 10765 tcp->tcp_hdr_len = len; 10766 } 10767 10768 /* 10769 * Copy the standard header into its new location, 10770 * lay in the new options and then update the relevant 10771 * fields in both tcp_t and the standard header. 10772 */ 10773 static int 10774 tcp_opt_set_header(tcp_t *tcp, boolean_t checkonly, uchar_t *ptr, uint_t len) 10775 { 10776 uint_t tcph_len; 10777 char *ip_optp; 10778 tcph_t *new_tcph; 10779 10780 if (checkonly) { 10781 /* 10782 * do not really set, just pretend to - T_CHECK 10783 */ 10784 if (len != 0) { 10785 /* 10786 * there is value supplied, validate it as if 10787 * for a real set operation. 10788 */ 10789 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 10790 return (EINVAL); 10791 } 10792 return (0); 10793 } 10794 10795 if ((len > TCP_MAX_IP_OPTIONS_LENGTH) || (len & 0x3)) 10796 return (EINVAL); 10797 10798 ip_optp = (char *)tcp->tcp_ipha + IP_SIMPLE_HDR_LENGTH; 10799 tcph_len = tcp->tcp_tcp_hdr_len; 10800 new_tcph = (tcph_t *)(ip_optp + len); 10801 ovbcopy((char *)tcp->tcp_tcph, (char *)new_tcph, tcph_len); 10802 tcp->tcp_tcph = new_tcph; 10803 bcopy(ptr, ip_optp, len); 10804 10805 len += IP_SIMPLE_HDR_LENGTH; 10806 10807 tcp->tcp_ip_hdr_len = len; 10808 tcp->tcp_ipha->ipha_version_and_hdr_length = 10809 (IP_VERSION << 4) | (len >> 2); 10810 len += tcph_len; 10811 tcp->tcp_hdr_len = len; 10812 if (!TCP_IS_DETACHED(tcp)) { 10813 /* Always allocate room for all options. */ 10814 (void) mi_set_sth_wroff(tcp->tcp_rq, 10815 TCP_MAX_COMBINED_HEADER_LENGTH + tcp_wroff_xtra); 10816 } 10817 return (0); 10818 } 10819 10820 /* Get callback routine passed to nd_load by tcp_param_register */ 10821 /* ARGSUSED */ 10822 static int 10823 tcp_param_get(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 10824 { 10825 tcpparam_t *tcppa = (tcpparam_t *)cp; 10826 10827 (void) mi_mpprintf(mp, "%u", tcppa->tcp_param_val); 10828 return (0); 10829 } 10830 10831 /* 10832 * Walk through the param array specified registering each element with the 10833 * named dispatch handler. 10834 */ 10835 static boolean_t 10836 tcp_param_register(tcpparam_t *tcppa, int cnt) 10837 { 10838 for (; cnt-- > 0; tcppa++) { 10839 if (tcppa->tcp_param_name && tcppa->tcp_param_name[0]) { 10840 if (!nd_load(&tcp_g_nd, tcppa->tcp_param_name, 10841 tcp_param_get, tcp_param_set, 10842 (caddr_t)tcppa)) { 10843 nd_free(&tcp_g_nd); 10844 return (B_FALSE); 10845 } 10846 } 10847 } 10848 if (!nd_load(&tcp_g_nd, tcp_wroff_xtra_param.tcp_param_name, 10849 tcp_param_get, tcp_param_set_aligned, 10850 (caddr_t)&tcp_wroff_xtra_param)) { 10851 nd_free(&tcp_g_nd); 10852 return (B_FALSE); 10853 } 10854 if (!nd_load(&tcp_g_nd, tcp_mdt_head_param.tcp_param_name, 10855 tcp_param_get, tcp_param_set_aligned, 10856 (caddr_t)&tcp_mdt_head_param)) { 10857 nd_free(&tcp_g_nd); 10858 return (B_FALSE); 10859 } 10860 if (!nd_load(&tcp_g_nd, tcp_mdt_tail_param.tcp_param_name, 10861 tcp_param_get, tcp_param_set_aligned, 10862 (caddr_t)&tcp_mdt_tail_param)) { 10863 nd_free(&tcp_g_nd); 10864 return (B_FALSE); 10865 } 10866 if (!nd_load(&tcp_g_nd, tcp_mdt_max_pbufs_param.tcp_param_name, 10867 tcp_param_get, tcp_param_set, 10868 (caddr_t)&tcp_mdt_max_pbufs_param)) { 10869 nd_free(&tcp_g_nd); 10870 return (B_FALSE); 10871 } 10872 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports", 10873 tcp_extra_priv_ports_get, NULL, NULL)) { 10874 nd_free(&tcp_g_nd); 10875 return (B_FALSE); 10876 } 10877 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_add", 10878 NULL, tcp_extra_priv_ports_add, NULL)) { 10879 nd_free(&tcp_g_nd); 10880 return (B_FALSE); 10881 } 10882 if (!nd_load(&tcp_g_nd, "tcp_extra_priv_ports_del", 10883 NULL, tcp_extra_priv_ports_del, NULL)) { 10884 nd_free(&tcp_g_nd); 10885 return (B_FALSE); 10886 } 10887 if (!nd_load(&tcp_g_nd, "tcp_status", tcp_status_report, NULL, 10888 NULL)) { 10889 nd_free(&tcp_g_nd); 10890 return (B_FALSE); 10891 } 10892 if (!nd_load(&tcp_g_nd, "tcp_bind_hash", tcp_bind_hash_report, 10893 NULL, NULL)) { 10894 nd_free(&tcp_g_nd); 10895 return (B_FALSE); 10896 } 10897 if (!nd_load(&tcp_g_nd, "tcp_listen_hash", tcp_listen_hash_report, 10898 NULL, NULL)) { 10899 nd_free(&tcp_g_nd); 10900 return (B_FALSE); 10901 } 10902 if (!nd_load(&tcp_g_nd, "tcp_conn_hash", tcp_conn_hash_report, 10903 NULL, NULL)) { 10904 nd_free(&tcp_g_nd); 10905 return (B_FALSE); 10906 } 10907 if (!nd_load(&tcp_g_nd, "tcp_acceptor_hash", tcp_acceptor_hash_report, 10908 NULL, NULL)) { 10909 nd_free(&tcp_g_nd); 10910 return (B_FALSE); 10911 } 10912 if (!nd_load(&tcp_g_nd, "tcp_host_param", tcp_host_param_report, 10913 tcp_host_param_set, NULL)) { 10914 nd_free(&tcp_g_nd); 10915 return (B_FALSE); 10916 } 10917 if (!nd_load(&tcp_g_nd, "tcp_host_param_ipv6", tcp_host_param_report, 10918 tcp_host_param_set_ipv6, NULL)) { 10919 nd_free(&tcp_g_nd); 10920 return (B_FALSE); 10921 } 10922 if (!nd_load(&tcp_g_nd, "tcp_1948_phrase", NULL, tcp_1948_phrase_set, 10923 NULL)) { 10924 nd_free(&tcp_g_nd); 10925 return (B_FALSE); 10926 } 10927 if (!nd_load(&tcp_g_nd, "tcp_reserved_port_list", 10928 tcp_reserved_port_list, NULL, NULL)) { 10929 nd_free(&tcp_g_nd); 10930 return (B_FALSE); 10931 } 10932 /* 10933 * Dummy ndd variables - only to convey obsolescence information 10934 * through printing of their name (no get or set routines) 10935 * XXX Remove in future releases ? 10936 */ 10937 if (!nd_load(&tcp_g_nd, 10938 "tcp_close_wait_interval(obsoleted - " 10939 "use tcp_time_wait_interval)", NULL, NULL, NULL)) { 10940 nd_free(&tcp_g_nd); 10941 return (B_FALSE); 10942 } 10943 return (B_TRUE); 10944 } 10945 10946 /* ndd set routine for tcp_wroff_xtra, tcp_mdt_hdr_{head,tail}_min. */ 10947 /* ARGSUSED */ 10948 static int 10949 tcp_param_set_aligned(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 10950 cred_t *cr) 10951 { 10952 long new_value; 10953 tcpparam_t *tcppa = (tcpparam_t *)cp; 10954 10955 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 10956 new_value < tcppa->tcp_param_min || 10957 new_value > tcppa->tcp_param_max) { 10958 return (EINVAL); 10959 } 10960 /* 10961 * Need to make sure new_value is a multiple of 4. If it is not, 10962 * round it up. For future 64 bit requirement, we actually make it 10963 * a multiple of 8. 10964 */ 10965 if (new_value & 0x7) { 10966 new_value = (new_value & ~0x7) + 0x8; 10967 } 10968 tcppa->tcp_param_val = new_value; 10969 return (0); 10970 } 10971 10972 /* Set callback routine passed to nd_load by tcp_param_register */ 10973 /* ARGSUSED */ 10974 static int 10975 tcp_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 10976 { 10977 long new_value; 10978 tcpparam_t *tcppa = (tcpparam_t *)cp; 10979 10980 if (ddi_strtol(value, NULL, 10, &new_value) != 0 || 10981 new_value < tcppa->tcp_param_min || 10982 new_value > tcppa->tcp_param_max) { 10983 return (EINVAL); 10984 } 10985 tcppa->tcp_param_val = new_value; 10986 return (0); 10987 } 10988 10989 /* 10990 * Add a new piece to the tcp reassembly queue. If the gap at the beginning 10991 * is filled, return as much as we can. The message passed in may be 10992 * multi-part, chained using b_cont. "start" is the starting sequence 10993 * number for this piece. 10994 */ 10995 static mblk_t * 10996 tcp_reass(tcp_t *tcp, mblk_t *mp, uint32_t start) 10997 { 10998 uint32_t end; 10999 mblk_t *mp1; 11000 mblk_t *mp2; 11001 mblk_t *next_mp; 11002 uint32_t u1; 11003 11004 /* Walk through all the new pieces. */ 11005 do { 11006 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 11007 (uintptr_t)INT_MAX); 11008 end = start + (int)(mp->b_wptr - mp->b_rptr); 11009 next_mp = mp->b_cont; 11010 if (start == end) { 11011 /* Empty. Blast it. */ 11012 freeb(mp); 11013 continue; 11014 } 11015 mp->b_cont = NULL; 11016 TCP_REASS_SET_SEQ(mp, start); 11017 TCP_REASS_SET_END(mp, end); 11018 mp1 = tcp->tcp_reass_tail; 11019 if (!mp1) { 11020 tcp->tcp_reass_tail = mp; 11021 tcp->tcp_reass_head = mp; 11022 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11023 UPDATE_MIB(&tcp_mib, 11024 tcpInDataUnorderBytes, end - start); 11025 continue; 11026 } 11027 /* New stuff completely beyond tail? */ 11028 if (SEQ_GEQ(start, TCP_REASS_END(mp1))) { 11029 /* Link it on end. */ 11030 mp1->b_cont = mp; 11031 tcp->tcp_reass_tail = mp; 11032 BUMP_MIB(&tcp_mib, tcpInDataUnorderSegs); 11033 UPDATE_MIB(&tcp_mib, 11034 tcpInDataUnorderBytes, end - start); 11035 continue; 11036 } 11037 mp1 = tcp->tcp_reass_head; 11038 u1 = TCP_REASS_SEQ(mp1); 11039 /* New stuff at the front? */ 11040 if (SEQ_LT(start, u1)) { 11041 /* Yes... Check for overlap. */ 11042 mp->b_cont = mp1; 11043 tcp->tcp_reass_head = mp; 11044 tcp_reass_elim_overlap(tcp, mp); 11045 continue; 11046 } 11047 /* 11048 * The new piece fits somewhere between the head and tail. 11049 * We find our slot, where mp1 precedes us and mp2 trails. 11050 */ 11051 for (; (mp2 = mp1->b_cont) != NULL; mp1 = mp2) { 11052 u1 = TCP_REASS_SEQ(mp2); 11053 if (SEQ_LEQ(start, u1)) 11054 break; 11055 } 11056 /* Link ourselves in */ 11057 mp->b_cont = mp2; 11058 mp1->b_cont = mp; 11059 11060 /* Trim overlap with following mblk(s) first */ 11061 tcp_reass_elim_overlap(tcp, mp); 11062 11063 /* Trim overlap with preceding mblk */ 11064 tcp_reass_elim_overlap(tcp, mp1); 11065 11066 } while (start = end, mp = next_mp); 11067 mp1 = tcp->tcp_reass_head; 11068 /* Anything ready to go? */ 11069 if (TCP_REASS_SEQ(mp1) != tcp->tcp_rnxt) 11070 return (NULL); 11071 /* Eat what we can off the queue */ 11072 for (;;) { 11073 mp = mp1->b_cont; 11074 end = TCP_REASS_END(mp1); 11075 TCP_REASS_SET_SEQ(mp1, 0); 11076 TCP_REASS_SET_END(mp1, 0); 11077 if (!mp) { 11078 tcp->tcp_reass_tail = NULL; 11079 break; 11080 } 11081 if (end != TCP_REASS_SEQ(mp)) { 11082 mp1->b_cont = NULL; 11083 break; 11084 } 11085 mp1 = mp; 11086 } 11087 mp1 = tcp->tcp_reass_head; 11088 tcp->tcp_reass_head = mp; 11089 return (mp1); 11090 } 11091 11092 /* Eliminate any overlap that mp may have over later mblks */ 11093 static void 11094 tcp_reass_elim_overlap(tcp_t *tcp, mblk_t *mp) 11095 { 11096 uint32_t end; 11097 mblk_t *mp1; 11098 uint32_t u1; 11099 11100 end = TCP_REASS_END(mp); 11101 while ((mp1 = mp->b_cont) != NULL) { 11102 u1 = TCP_REASS_SEQ(mp1); 11103 if (!SEQ_GT(end, u1)) 11104 break; 11105 if (!SEQ_GEQ(end, TCP_REASS_END(mp1))) { 11106 mp->b_wptr -= end - u1; 11107 TCP_REASS_SET_END(mp, u1); 11108 BUMP_MIB(&tcp_mib, tcpInDataPartDupSegs); 11109 UPDATE_MIB(&tcp_mib, tcpInDataPartDupBytes, end - u1); 11110 break; 11111 } 11112 mp->b_cont = mp1->b_cont; 11113 TCP_REASS_SET_SEQ(mp1, 0); 11114 TCP_REASS_SET_END(mp1, 0); 11115 freeb(mp1); 11116 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 11117 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, end - u1); 11118 } 11119 if (!mp1) 11120 tcp->tcp_reass_tail = mp; 11121 } 11122 11123 /* 11124 * Send up all messages queued on tcp_rcv_list. 11125 */ 11126 static uint_t 11127 tcp_rcv_drain(queue_t *q, tcp_t *tcp) 11128 { 11129 mblk_t *mp; 11130 uint_t ret = 0; 11131 uint_t thwin; 11132 #ifdef DEBUG 11133 uint_t cnt = 0; 11134 #endif 11135 /* Can't drain on an eager connection */ 11136 if (tcp->tcp_listener != NULL) 11137 return (ret); 11138 11139 /* 11140 * Handle two cases here: we are currently fused or we were 11141 * previously fused and have some urgent data to be delivered 11142 * upstream. The latter happens because we either ran out of 11143 * memory or were detached and therefore sending the SIGURG was 11144 * deferred until this point. In either case we pass control 11145 * over to tcp_fuse_rcv_drain() since it may need to complete 11146 * some work. 11147 */ 11148 if ((tcp->tcp_fused || tcp->tcp_fused_sigurg)) { 11149 ASSERT(tcp->tcp_fused_sigurg_mp != NULL); 11150 if (tcp_fuse_rcv_drain(q, tcp, tcp->tcp_fused ? NULL : 11151 &tcp->tcp_fused_sigurg_mp)) 11152 return (ret); 11153 } 11154 11155 while ((mp = tcp->tcp_rcv_list) != NULL) { 11156 tcp->tcp_rcv_list = mp->b_next; 11157 mp->b_next = NULL; 11158 #ifdef DEBUG 11159 cnt += msgdsize(mp); 11160 #endif 11161 /* Does this need SSL processing first? */ 11162 if ((tcp->tcp_kssl_ctx != NULL) && (DB_TYPE(mp) == M_DATA)) { 11163 tcp_kssl_input(tcp, mp); 11164 continue; 11165 } 11166 putnext(q, mp); 11167 } 11168 ASSERT(cnt == tcp->tcp_rcv_cnt); 11169 tcp->tcp_rcv_last_head = NULL; 11170 tcp->tcp_rcv_last_tail = NULL; 11171 tcp->tcp_rcv_cnt = 0; 11172 11173 /* Learn the latest rwnd information that we sent to the other side. */ 11174 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 11175 << tcp->tcp_rcv_ws; 11176 /* This is peer's calculated send window (our receive window). */ 11177 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 11178 /* 11179 * Increase the receive window to max. But we need to do receiver 11180 * SWS avoidance. This means that we need to check the increase of 11181 * of receive window is at least 1 MSS. 11182 */ 11183 if (canputnext(q) && (q->q_hiwat - thwin >= tcp->tcp_mss)) { 11184 /* 11185 * If the window that the other side knows is less than max 11186 * deferred acks segments, send an update immediately. 11187 */ 11188 if (thwin < tcp->tcp_rack_cur_max * tcp->tcp_mss) { 11189 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 11190 ret = TH_ACK_NEEDED; 11191 } 11192 tcp->tcp_rwnd = q->q_hiwat; 11193 } 11194 /* No need for the push timer now. */ 11195 if (tcp->tcp_push_tid != 0) { 11196 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_push_tid); 11197 tcp->tcp_push_tid = 0; 11198 } 11199 return (ret); 11200 } 11201 11202 /* 11203 * Queue data on tcp_rcv_list which is a b_next chain. 11204 * tcp_rcv_last_head/tail is the last element of this chain. 11205 * Each element of the chain is a b_cont chain. 11206 * 11207 * M_DATA messages are added to the current element. 11208 * Other messages are added as new (b_next) elements. 11209 */ 11210 void 11211 tcp_rcv_enqueue(tcp_t *tcp, mblk_t *mp, uint_t seg_len) 11212 { 11213 ASSERT(seg_len == msgdsize(mp)); 11214 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_rcv_last_head != NULL); 11215 11216 if (tcp->tcp_rcv_list == NULL) { 11217 ASSERT(tcp->tcp_rcv_last_head == NULL); 11218 tcp->tcp_rcv_list = mp; 11219 tcp->tcp_rcv_last_head = mp; 11220 } else if (DB_TYPE(mp) == DB_TYPE(tcp->tcp_rcv_last_head)) { 11221 tcp->tcp_rcv_last_tail->b_cont = mp; 11222 } else { 11223 tcp->tcp_rcv_last_head->b_next = mp; 11224 tcp->tcp_rcv_last_head = mp; 11225 } 11226 11227 while (mp->b_cont) 11228 mp = mp->b_cont; 11229 11230 tcp->tcp_rcv_last_tail = mp; 11231 tcp->tcp_rcv_cnt += seg_len; 11232 tcp->tcp_rwnd -= seg_len; 11233 } 11234 11235 /* 11236 * DEFAULT TCP ENTRY POINT via squeue on READ side. 11237 * 11238 * This is the default entry function into TCP on the read side. TCP is 11239 * always entered via squeue i.e. using squeue's for mutual exclusion. 11240 * When classifier does a lookup to find the tcp, it also puts a reference 11241 * on the conn structure associated so the tcp is guaranteed to exist 11242 * when we come here. We still need to check the state because it might 11243 * as well has been closed. The squeue processing function i.e. squeue_enter, 11244 * squeue_enter_nodrain, or squeue_drain is responsible for doing the 11245 * CONN_DEC_REF. 11246 * 11247 * Apart from the default entry point, IP also sends packets directly to 11248 * tcp_rput_data for AF_INET fast path and tcp_conn_request for incoming 11249 * connections. 11250 */ 11251 void 11252 tcp_input(void *arg, mblk_t *mp, void *arg2) 11253 { 11254 conn_t *connp = (conn_t *)arg; 11255 tcp_t *tcp = (tcp_t *)connp->conn_tcp; 11256 11257 /* arg2 is the sqp */ 11258 ASSERT(arg2 != NULL); 11259 ASSERT(mp != NULL); 11260 11261 /* 11262 * Don't accept any input on a closed tcp as this TCP logically does 11263 * not exist on the system. Don't proceed further with this TCP. 11264 * For eg. this packet could trigger another close of this tcp 11265 * which would be disastrous for tcp_refcnt. tcp_close_detached / 11266 * tcp_clean_death / tcp_closei_local must be called at most once 11267 * on a TCP. In this case we need to refeed the packet into the 11268 * classifier and figure out where the packet should go. Need to 11269 * preserve the recv_ill somehow. Until we figure that out, for 11270 * now just drop the packet if we can't classify the packet. 11271 */ 11272 if (tcp->tcp_state == TCPS_CLOSED || 11273 tcp->tcp_state == TCPS_BOUND) { 11274 conn_t *new_connp; 11275 11276 new_connp = ipcl_classify(mp, connp->conn_zoneid); 11277 if (new_connp != NULL) { 11278 tcp_reinput(new_connp, mp, arg2); 11279 return; 11280 } 11281 /* We failed to classify. For now just drop the packet */ 11282 freemsg(mp); 11283 return; 11284 } 11285 11286 if (DB_TYPE(mp) == M_DATA) 11287 tcp_rput_data(connp, mp, arg2); 11288 else 11289 tcp_rput_common(tcp, mp); 11290 } 11291 11292 /* 11293 * The read side put procedure. 11294 * The packets passed up by ip are assume to be aligned according to 11295 * OK_32PTR and the IP+TCP headers fitting in the first mblk. 11296 */ 11297 static void 11298 tcp_rput_common(tcp_t *tcp, mblk_t *mp) 11299 { 11300 /* 11301 * tcp_rput_data() does not expect M_CTL except for the case 11302 * where tcp_ipv6_recvancillary is set and we get a IN_PKTINFO 11303 * type. Need to make sure that any other M_CTLs don't make 11304 * it to tcp_rput_data since it is not expecting any and doesn't 11305 * check for it. 11306 */ 11307 if (DB_TYPE(mp) == M_CTL) { 11308 switch (*(uint32_t *)(mp->b_rptr)) { 11309 case TCP_IOC_ABORT_CONN: 11310 /* 11311 * Handle connection abort request. 11312 */ 11313 tcp_ioctl_abort_handler(tcp, mp); 11314 return; 11315 case IPSEC_IN: 11316 /* 11317 * Only secure icmp arrive in TCP and they 11318 * don't go through data path. 11319 */ 11320 tcp_icmp_error(tcp, mp); 11321 return; 11322 case IN_PKTINFO: 11323 /* 11324 * Handle IPV6_RECVPKTINFO socket option on AF_INET6 11325 * sockets that are receiving IPv4 traffic. tcp 11326 */ 11327 ASSERT(tcp->tcp_family == AF_INET6); 11328 ASSERT(tcp->tcp_ipv6_recvancillary & 11329 TCP_IPV6_RECVPKTINFO); 11330 tcp_rput_data(tcp->tcp_connp, mp, 11331 tcp->tcp_connp->conn_sqp); 11332 return; 11333 case MDT_IOC_INFO_UPDATE: 11334 /* 11335 * Handle Multidata information update; the 11336 * following routine will free the message. 11337 */ 11338 if (tcp->tcp_connp->conn_mdt_ok) { 11339 tcp_mdt_update(tcp, 11340 &((ip_mdt_info_t *)mp->b_rptr)->mdt_capab, 11341 B_FALSE); 11342 } 11343 freemsg(mp); 11344 return; 11345 default: 11346 break; 11347 } 11348 } 11349 11350 /* No point processing the message if tcp is already closed */ 11351 if (TCP_IS_DETACHED_NONEAGER(tcp)) { 11352 freemsg(mp); 11353 return; 11354 } 11355 11356 tcp_rput_other(tcp, mp); 11357 } 11358 11359 11360 /* The minimum of smoothed mean deviation in RTO calculation. */ 11361 #define TCP_SD_MIN 400 11362 11363 /* 11364 * Set RTO for this connection. The formula is from Jacobson and Karels' 11365 * "Congestion Avoidance and Control" in SIGCOMM '88. The variable names 11366 * are the same as those in Appendix A.2 of that paper. 11367 * 11368 * m = new measurement 11369 * sa = smoothed RTT average (8 * average estimates). 11370 * sv = smoothed mean deviation (mdev) of RTT (4 * deviation estimates). 11371 */ 11372 static void 11373 tcp_set_rto(tcp_t *tcp, clock_t rtt) 11374 { 11375 long m = TICK_TO_MSEC(rtt); 11376 clock_t sa = tcp->tcp_rtt_sa; 11377 clock_t sv = tcp->tcp_rtt_sd; 11378 clock_t rto; 11379 11380 BUMP_MIB(&tcp_mib, tcpRttUpdate); 11381 tcp->tcp_rtt_update++; 11382 11383 /* tcp_rtt_sa is not 0 means this is a new sample. */ 11384 if (sa != 0) { 11385 /* 11386 * Update average estimator: 11387 * new rtt = 7/8 old rtt + 1/8 Error 11388 */ 11389 11390 /* m is now Error in estimate. */ 11391 m -= sa >> 3; 11392 if ((sa += m) <= 0) { 11393 /* 11394 * Don't allow the smoothed average to be negative. 11395 * We use 0 to denote reinitialization of the 11396 * variables. 11397 */ 11398 sa = 1; 11399 } 11400 11401 /* 11402 * Update deviation estimator: 11403 * new mdev = 3/4 old mdev + 1/4 (abs(Error) - old mdev) 11404 */ 11405 if (m < 0) 11406 m = -m; 11407 m -= sv >> 2; 11408 sv += m; 11409 } else { 11410 /* 11411 * This follows BSD's implementation. So the reinitialized 11412 * RTO is 3 * m. We cannot go less than 2 because if the 11413 * link is bandwidth dominated, doubling the window size 11414 * during slow start means doubling the RTT. We want to be 11415 * more conservative when we reinitialize our estimates. 3 11416 * is just a convenient number. 11417 */ 11418 sa = m << 3; 11419 sv = m << 1; 11420 } 11421 if (sv < TCP_SD_MIN) { 11422 /* 11423 * We do not know that if sa captures the delay ACK 11424 * effect as in a long train of segments, a receiver 11425 * does not delay its ACKs. So set the minimum of sv 11426 * to be TCP_SD_MIN, which is default to 400 ms, twice 11427 * of BSD DATO. That means the minimum of mean 11428 * deviation is 100 ms. 11429 * 11430 */ 11431 sv = TCP_SD_MIN; 11432 } 11433 tcp->tcp_rtt_sa = sa; 11434 tcp->tcp_rtt_sd = sv; 11435 /* 11436 * RTO = average estimates (sa / 8) + 4 * deviation estimates (sv) 11437 * 11438 * Add tcp_rexmit_interval extra in case of extreme environment 11439 * where the algorithm fails to work. The default value of 11440 * tcp_rexmit_interval_extra should be 0. 11441 * 11442 * As we use a finer grained clock than BSD and update 11443 * RTO for every ACKs, add in another .25 of RTT to the 11444 * deviation of RTO to accomodate burstiness of 1/4 of 11445 * window size. 11446 */ 11447 rto = (sa >> 3) + sv + tcp_rexmit_interval_extra + (sa >> 5); 11448 11449 if (rto > tcp_rexmit_interval_max) { 11450 tcp->tcp_rto = tcp_rexmit_interval_max; 11451 } else if (rto < tcp_rexmit_interval_min) { 11452 tcp->tcp_rto = tcp_rexmit_interval_min; 11453 } else { 11454 tcp->tcp_rto = rto; 11455 } 11456 11457 /* Now, we can reset tcp_timer_backoff to use the new RTO... */ 11458 tcp->tcp_timer_backoff = 0; 11459 } 11460 11461 /* 11462 * tcp_get_seg_mp() is called to get the pointer to a segment in the 11463 * send queue which starts at the given seq. no. 11464 * 11465 * Parameters: 11466 * tcp_t *tcp: the tcp instance pointer. 11467 * uint32_t seq: the starting seq. no of the requested segment. 11468 * int32_t *off: after the execution, *off will be the offset to 11469 * the returned mblk which points to the requested seq no. 11470 * It is the caller's responsibility to send in a non-null off. 11471 * 11472 * Return: 11473 * A mblk_t pointer pointing to the requested segment in send queue. 11474 */ 11475 static mblk_t * 11476 tcp_get_seg_mp(tcp_t *tcp, uint32_t seq, int32_t *off) 11477 { 11478 int32_t cnt; 11479 mblk_t *mp; 11480 11481 /* Defensive coding. Make sure we don't send incorrect data. */ 11482 if (SEQ_LT(seq, tcp->tcp_suna) || SEQ_GEQ(seq, tcp->tcp_snxt)) 11483 return (NULL); 11484 11485 cnt = seq - tcp->tcp_suna; 11486 mp = tcp->tcp_xmit_head; 11487 while (cnt > 0 && mp != NULL) { 11488 cnt -= mp->b_wptr - mp->b_rptr; 11489 if (cnt < 0) { 11490 cnt += mp->b_wptr - mp->b_rptr; 11491 break; 11492 } 11493 mp = mp->b_cont; 11494 } 11495 ASSERT(mp != NULL); 11496 *off = cnt; 11497 return (mp); 11498 } 11499 11500 /* 11501 * This function handles all retransmissions if SACK is enabled for this 11502 * connection. First it calculates how many segments can be retransmitted 11503 * based on tcp_pipe. Then it goes thru the notsack list to find eligible 11504 * segments. A segment is eligible if sack_cnt for that segment is greater 11505 * than or equal tcp_dupack_fast_retransmit. After it has retransmitted 11506 * all eligible segments, it checks to see if TCP can send some new segments 11507 * (fast recovery). If it can, set the appropriate flag for tcp_rput_data(). 11508 * 11509 * Parameters: 11510 * tcp_t *tcp: the tcp structure of the connection. 11511 * uint_t *flags: in return, appropriate value will be set for 11512 * tcp_rput_data(). 11513 */ 11514 static void 11515 tcp_sack_rxmit(tcp_t *tcp, uint_t *flags) 11516 { 11517 notsack_blk_t *notsack_blk; 11518 int32_t usable_swnd; 11519 int32_t mss; 11520 uint32_t seg_len; 11521 mblk_t *xmit_mp; 11522 11523 ASSERT(tcp->tcp_sack_info != NULL); 11524 ASSERT(tcp->tcp_notsack_list != NULL); 11525 ASSERT(tcp->tcp_rexmit == B_FALSE); 11526 11527 /* Defensive coding in case there is a bug... */ 11528 if (tcp->tcp_notsack_list == NULL) { 11529 return; 11530 } 11531 notsack_blk = tcp->tcp_notsack_list; 11532 mss = tcp->tcp_mss; 11533 11534 /* 11535 * Limit the num of outstanding data in the network to be 11536 * tcp_cwnd_ssthresh, which is half of the original congestion wnd. 11537 */ 11538 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11539 11540 /* At least retransmit 1 MSS of data. */ 11541 if (usable_swnd <= 0) { 11542 usable_swnd = mss; 11543 } 11544 11545 /* Make sure no new RTT samples will be taken. */ 11546 tcp->tcp_csuna = tcp->tcp_snxt; 11547 11548 notsack_blk = tcp->tcp_notsack_list; 11549 while (usable_swnd > 0) { 11550 mblk_t *snxt_mp, *tmp_mp; 11551 tcp_seq begin = tcp->tcp_sack_snxt; 11552 tcp_seq end; 11553 int32_t off; 11554 11555 for (; notsack_blk != NULL; notsack_blk = notsack_blk->next) { 11556 if (SEQ_GT(notsack_blk->end, begin) && 11557 (notsack_blk->sack_cnt >= 11558 tcp_dupack_fast_retransmit)) { 11559 end = notsack_blk->end; 11560 if (SEQ_LT(begin, notsack_blk->begin)) { 11561 begin = notsack_blk->begin; 11562 } 11563 break; 11564 } 11565 } 11566 /* 11567 * All holes are filled. Manipulate tcp_cwnd to send more 11568 * if we can. Note that after the SACK recovery, tcp_cwnd is 11569 * set to tcp_cwnd_ssthresh. 11570 */ 11571 if (notsack_blk == NULL) { 11572 usable_swnd = tcp->tcp_cwnd_ssthresh - tcp->tcp_pipe; 11573 if (usable_swnd <= 0 || tcp->tcp_unsent == 0) { 11574 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna; 11575 ASSERT(tcp->tcp_cwnd > 0); 11576 return; 11577 } else { 11578 usable_swnd = usable_swnd / mss; 11579 tcp->tcp_cwnd = tcp->tcp_snxt - tcp->tcp_suna + 11580 MAX(usable_swnd * mss, mss); 11581 *flags |= TH_XMIT_NEEDED; 11582 return; 11583 } 11584 } 11585 11586 /* 11587 * Note that we may send more than usable_swnd allows here 11588 * because of round off, but no more than 1 MSS of data. 11589 */ 11590 seg_len = end - begin; 11591 if (seg_len > mss) 11592 seg_len = mss; 11593 snxt_mp = tcp_get_seg_mp(tcp, begin, &off); 11594 ASSERT(snxt_mp != NULL); 11595 /* This should not happen. Defensive coding again... */ 11596 if (snxt_mp == NULL) { 11597 return; 11598 } 11599 11600 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, seg_len, &off, 11601 &tmp_mp, begin, B_TRUE, &seg_len, B_TRUE); 11602 if (xmit_mp == NULL) 11603 return; 11604 11605 usable_swnd -= seg_len; 11606 tcp->tcp_pipe += seg_len; 11607 tcp->tcp_sack_snxt = begin + seg_len; 11608 TCP_RECORD_TRACE(tcp, xmit_mp, TCP_TRACE_SEND_PKT); 11609 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11610 11611 /* 11612 * Update the send timestamp to avoid false retransmission. 11613 */ 11614 snxt_mp->b_prev = (mblk_t *)lbolt; 11615 11616 BUMP_MIB(&tcp_mib, tcpRetransSegs); 11617 UPDATE_MIB(&tcp_mib, tcpRetransBytes, seg_len); 11618 BUMP_MIB(&tcp_mib, tcpOutSackRetransSegs); 11619 /* 11620 * Update tcp_rexmit_max to extend this SACK recovery phase. 11621 * This happens when new data sent during fast recovery is 11622 * also lost. If TCP retransmits those new data, it needs 11623 * to extend SACK recover phase to avoid starting another 11624 * fast retransmit/recovery unnecessarily. 11625 */ 11626 if (SEQ_GT(tcp->tcp_sack_snxt, tcp->tcp_rexmit_max)) { 11627 tcp->tcp_rexmit_max = tcp->tcp_sack_snxt; 11628 } 11629 } 11630 } 11631 11632 /* 11633 * This function handles policy checking at TCP level for non-hard_bound/ 11634 * detached connections. 11635 */ 11636 static boolean_t 11637 tcp_check_policy(tcp_t *tcp, mblk_t *first_mp, ipha_t *ipha, ip6_t *ip6h, 11638 boolean_t secure, boolean_t mctl_present) 11639 { 11640 ipsec_latch_t *ipl = NULL; 11641 ipsec_action_t *act = NULL; 11642 mblk_t *data_mp; 11643 ipsec_in_t *ii; 11644 const char *reason; 11645 kstat_named_t *counter; 11646 11647 ASSERT(mctl_present || !secure); 11648 11649 ASSERT((ipha == NULL && ip6h != NULL) || 11650 (ip6h == NULL && ipha != NULL)); 11651 11652 /* 11653 * We don't necessarily have an ipsec_in_act action to verify 11654 * policy because of assymetrical policy where we have only 11655 * outbound policy and no inbound policy (possible with global 11656 * policy). 11657 */ 11658 if (!secure) { 11659 if (act == NULL || act->ipa_act.ipa_type == IPSEC_ACT_BYPASS || 11660 act->ipa_act.ipa_type == IPSEC_ACT_CLEAR) 11661 return (B_TRUE); 11662 ipsec_log_policy_failure(tcp->tcp_wq, IPSEC_POLICY_MISMATCH, 11663 "tcp_check_policy", ipha, ip6h, secure); 11664 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11665 &ipdrops_tcp_clear, &tcp_dropper); 11666 return (B_FALSE); 11667 } 11668 11669 /* 11670 * We have a secure packet. 11671 */ 11672 if (act == NULL) { 11673 ipsec_log_policy_failure(tcp->tcp_wq, 11674 IPSEC_POLICY_NOT_NEEDED, "tcp_check_policy", ipha, ip6h, 11675 secure); 11676 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, 11677 &ipdrops_tcp_secure, &tcp_dropper); 11678 return (B_FALSE); 11679 } 11680 11681 /* 11682 * XXX This whole routine is currently incorrect. ipl should 11683 * be set to the latch pointer, but is currently not set, so 11684 * we initialize it to NULL to avoid picking up random garbage. 11685 */ 11686 if (ipl == NULL) 11687 return (B_TRUE); 11688 11689 data_mp = first_mp->b_cont; 11690 11691 ii = (ipsec_in_t *)first_mp->b_rptr; 11692 11693 if (ipsec_check_ipsecin_latch(ii, data_mp, ipl, ipha, ip6h, &reason, 11694 &counter)) { 11695 BUMP_MIB(&ip_mib, ipsecInSucceeded); 11696 return (B_TRUE); 11697 } 11698 (void) strlog(TCP_MOD_ID, 0, 0, SL_ERROR|SL_WARN|SL_CONSOLE, 11699 "tcp inbound policy mismatch: %s, packet dropped\n", 11700 reason); 11701 BUMP_MIB(&ip_mib, ipsecInFailed); 11702 11703 ip_drop_packet(first_mp, B_TRUE, NULL, NULL, counter, &tcp_dropper); 11704 return (B_FALSE); 11705 } 11706 11707 /* 11708 * tcp_ss_rexmit() is called in tcp_rput_data() to do slow start 11709 * retransmission after a timeout. 11710 * 11711 * To limit the number of duplicate segments, we limit the number of segment 11712 * to be sent in one time to tcp_snd_burst, the burst variable. 11713 */ 11714 static void 11715 tcp_ss_rexmit(tcp_t *tcp) 11716 { 11717 uint32_t snxt; 11718 uint32_t smax; 11719 int32_t win; 11720 int32_t mss; 11721 int32_t off; 11722 int32_t burst = tcp->tcp_snd_burst; 11723 mblk_t *snxt_mp; 11724 11725 /* 11726 * Note that tcp_rexmit can be set even though TCP has retransmitted 11727 * all unack'ed segments. 11728 */ 11729 if (SEQ_LT(tcp->tcp_rexmit_nxt, tcp->tcp_rexmit_max)) { 11730 smax = tcp->tcp_rexmit_max; 11731 snxt = tcp->tcp_rexmit_nxt; 11732 if (SEQ_LT(snxt, tcp->tcp_suna)) { 11733 snxt = tcp->tcp_suna; 11734 } 11735 win = MIN(tcp->tcp_cwnd, tcp->tcp_swnd); 11736 win -= snxt - tcp->tcp_suna; 11737 mss = tcp->tcp_mss; 11738 snxt_mp = tcp_get_seg_mp(tcp, snxt, &off); 11739 11740 while (SEQ_LT(snxt, smax) && (win > 0) && 11741 (burst > 0) && (snxt_mp != NULL)) { 11742 mblk_t *xmit_mp; 11743 mblk_t *old_snxt_mp = snxt_mp; 11744 uint32_t cnt = mss; 11745 11746 if (win < cnt) { 11747 cnt = win; 11748 } 11749 if (SEQ_GT(snxt + cnt, smax)) { 11750 cnt = smax - snxt; 11751 } 11752 xmit_mp = tcp_xmit_mp(tcp, snxt_mp, cnt, &off, 11753 &snxt_mp, snxt, B_TRUE, &cnt, B_TRUE); 11754 if (xmit_mp == NULL) 11755 return; 11756 11757 tcp_send_data(tcp, tcp->tcp_wq, xmit_mp); 11758 11759 snxt += cnt; 11760 win -= cnt; 11761 /* 11762 * Update the send timestamp to avoid false 11763 * retransmission. 11764 */ 11765 old_snxt_mp->b_prev = (mblk_t *)lbolt; 11766 BUMP_MIB(&tcp_mib, tcpRetransSegs); 11767 UPDATE_MIB(&tcp_mib, tcpRetransBytes, cnt); 11768 11769 tcp->tcp_rexmit_nxt = snxt; 11770 burst--; 11771 } 11772 /* 11773 * If we have transmitted all we have at the time 11774 * we started the retranmission, we can leave 11775 * the rest of the job to tcp_wput_data(). But we 11776 * need to check the send window first. If the 11777 * win is not 0, go on with tcp_wput_data(). 11778 */ 11779 if (SEQ_LT(snxt, smax) || win == 0) { 11780 return; 11781 } 11782 } 11783 /* Only call tcp_wput_data() if there is data to be sent. */ 11784 if (tcp->tcp_unsent) { 11785 tcp_wput_data(tcp, NULL, B_FALSE); 11786 } 11787 } 11788 11789 /* 11790 * Process all TCP option in SYN segment. Note that this function should 11791 * be called after tcp_adapt_ire() is called so that the necessary info 11792 * from IRE is already set in the tcp structure. 11793 * 11794 * This function sets up the correct tcp_mss value according to the 11795 * MSS option value and our header size. It also sets up the window scale 11796 * and timestamp values, and initialize SACK info blocks. But it does not 11797 * change receive window size after setting the tcp_mss value. The caller 11798 * should do the appropriate change. 11799 */ 11800 void 11801 tcp_process_options(tcp_t *tcp, tcph_t *tcph) 11802 { 11803 int options; 11804 tcp_opt_t tcpopt; 11805 uint32_t mss_max; 11806 char *tmp_tcph; 11807 11808 tcpopt.tcp = NULL; 11809 options = tcp_parse_options(tcph, &tcpopt); 11810 11811 /* 11812 * Process MSS option. Note that MSS option value does not account 11813 * for IP or TCP options. This means that it is equal to MTU - minimum 11814 * IP+TCP header size, which is 40 bytes for IPv4 and 60 bytes for 11815 * IPv6. 11816 */ 11817 if (!(options & TCP_OPT_MSS_PRESENT)) { 11818 if (tcp->tcp_ipversion == IPV4_VERSION) 11819 tcpopt.tcp_opt_mss = tcp_mss_def_ipv4; 11820 else 11821 tcpopt.tcp_opt_mss = tcp_mss_def_ipv6; 11822 } else { 11823 if (tcp->tcp_ipversion == IPV4_VERSION) 11824 mss_max = tcp_mss_max_ipv4; 11825 else 11826 mss_max = tcp_mss_max_ipv6; 11827 if (tcpopt.tcp_opt_mss < tcp_mss_min) 11828 tcpopt.tcp_opt_mss = tcp_mss_min; 11829 else if (tcpopt.tcp_opt_mss > mss_max) 11830 tcpopt.tcp_opt_mss = mss_max; 11831 } 11832 11833 /* Process Window Scale option. */ 11834 if (options & TCP_OPT_WSCALE_PRESENT) { 11835 tcp->tcp_snd_ws = tcpopt.tcp_opt_wscale; 11836 tcp->tcp_snd_ws_ok = B_TRUE; 11837 } else { 11838 tcp->tcp_snd_ws = B_FALSE; 11839 tcp->tcp_snd_ws_ok = B_FALSE; 11840 tcp->tcp_rcv_ws = B_FALSE; 11841 } 11842 11843 /* Process Timestamp option. */ 11844 if ((options & TCP_OPT_TSTAMP_PRESENT) && 11845 (tcp->tcp_snd_ts_ok || TCP_IS_DETACHED(tcp))) { 11846 tmp_tcph = (char *)tcp->tcp_tcph; 11847 11848 tcp->tcp_snd_ts_ok = B_TRUE; 11849 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 11850 tcp->tcp_last_rcv_lbolt = lbolt64; 11851 ASSERT(OK_32PTR(tmp_tcph)); 11852 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 11853 11854 /* Fill in our template header with basic timestamp option. */ 11855 tmp_tcph += tcp->tcp_tcp_hdr_len; 11856 tmp_tcph[0] = TCPOPT_NOP; 11857 tmp_tcph[1] = TCPOPT_NOP; 11858 tmp_tcph[2] = TCPOPT_TSTAMP; 11859 tmp_tcph[3] = TCPOPT_TSTAMP_LEN; 11860 tcp->tcp_hdr_len += TCPOPT_REAL_TS_LEN; 11861 tcp->tcp_tcp_hdr_len += TCPOPT_REAL_TS_LEN; 11862 tcp->tcp_tcph->th_offset_and_rsrvd[0] += (3 << 4); 11863 } else { 11864 tcp->tcp_snd_ts_ok = B_FALSE; 11865 } 11866 11867 /* 11868 * Process SACK options. If SACK is enabled for this connection, 11869 * then allocate the SACK info structure. Note the following ways 11870 * when tcp_snd_sack_ok is set to true. 11871 * 11872 * For active connection: in tcp_adapt_ire() called in 11873 * tcp_rput_other(), or in tcp_rput_other() when tcp_sack_permitted 11874 * is checked. 11875 * 11876 * For passive connection: in tcp_adapt_ire() called in 11877 * tcp_accept_comm(). 11878 * 11879 * That's the reason why the extra TCP_IS_DETACHED() check is there. 11880 * That check makes sure that if we did not send a SACK OK option, 11881 * we will not enable SACK for this connection even though the other 11882 * side sends us SACK OK option. For active connection, the SACK 11883 * info structure has already been allocated. So we need to free 11884 * it if SACK is disabled. 11885 */ 11886 if ((options & TCP_OPT_SACK_OK_PRESENT) && 11887 (tcp->tcp_snd_sack_ok || 11888 (tcp_sack_permitted != 0 && TCP_IS_DETACHED(tcp)))) { 11889 /* This should be true only in the passive case. */ 11890 if (tcp->tcp_sack_info == NULL) { 11891 ASSERT(TCP_IS_DETACHED(tcp)); 11892 tcp->tcp_sack_info = 11893 kmem_cache_alloc(tcp_sack_info_cache, KM_NOSLEEP); 11894 } 11895 if (tcp->tcp_sack_info == NULL) { 11896 tcp->tcp_snd_sack_ok = B_FALSE; 11897 } else { 11898 tcp->tcp_snd_sack_ok = B_TRUE; 11899 if (tcp->tcp_snd_ts_ok) { 11900 tcp->tcp_max_sack_blk = 3; 11901 } else { 11902 tcp->tcp_max_sack_blk = 4; 11903 } 11904 } 11905 } else { 11906 /* 11907 * Resetting tcp_snd_sack_ok to B_FALSE so that 11908 * no SACK info will be used for this 11909 * connection. This assumes that SACK usage 11910 * permission is negotiated. This may need 11911 * to be changed once this is clarified. 11912 */ 11913 if (tcp->tcp_sack_info != NULL) { 11914 ASSERT(tcp->tcp_notsack_list == NULL); 11915 kmem_cache_free(tcp_sack_info_cache, 11916 tcp->tcp_sack_info); 11917 tcp->tcp_sack_info = NULL; 11918 } 11919 tcp->tcp_snd_sack_ok = B_FALSE; 11920 } 11921 11922 /* 11923 * Now we know the exact TCP/IP header length, subtract 11924 * that from tcp_mss to get our side's MSS. 11925 */ 11926 tcp->tcp_mss -= tcp->tcp_hdr_len; 11927 /* 11928 * Here we assume that the other side's header size will be equal to 11929 * our header size. We calculate the real MSS accordingly. Need to 11930 * take into additional stuffs IPsec puts in. 11931 * 11932 * Real MSS = Opt.MSS - (our TCP/IP header - min TCP/IP header) 11933 */ 11934 tcpopt.tcp_opt_mss -= tcp->tcp_hdr_len + tcp->tcp_ipsec_overhead - 11935 ((tcp->tcp_ipversion == IPV4_VERSION ? 11936 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) + TCP_MIN_HEADER_LENGTH); 11937 11938 /* 11939 * Set MSS to the smaller one of both ends of the connection. 11940 * We should not have called tcp_mss_set() before, but our 11941 * side of the MSS should have been set to a proper value 11942 * by tcp_adapt_ire(). tcp_mss_set() will also set up the 11943 * STREAM head parameters properly. 11944 * 11945 * If we have a larger-than-16-bit window but the other side 11946 * didn't want to do window scale, tcp_rwnd_set() will take 11947 * care of that. 11948 */ 11949 tcp_mss_set(tcp, MIN(tcpopt.tcp_opt_mss, tcp->tcp_mss)); 11950 } 11951 11952 /* 11953 * Sends the T_CONN_IND to the listener. The caller calls this 11954 * functions via squeue to get inside the listener's perimeter 11955 * once the 3 way hand shake is done a T_CONN_IND needs to be 11956 * sent. As an optimization, the caller can call this directly 11957 * if listener's perimeter is same as eager's. 11958 */ 11959 /* ARGSUSED */ 11960 void 11961 tcp_send_conn_ind(void *arg, mblk_t *mp, void *arg2) 11962 { 11963 conn_t *lconnp = (conn_t *)arg; 11964 tcp_t *listener = lconnp->conn_tcp; 11965 tcp_t *tcp; 11966 struct T_conn_ind *conn_ind; 11967 ipaddr_t *addr_cache; 11968 boolean_t need_send_conn_ind = B_FALSE; 11969 11970 /* retrieve the eager */ 11971 conn_ind = (struct T_conn_ind *)mp->b_rptr; 11972 ASSERT(conn_ind->OPT_offset != 0 && 11973 conn_ind->OPT_length == sizeof (intptr_t)); 11974 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 11975 conn_ind->OPT_length); 11976 11977 /* 11978 * TLI/XTI applications will get confused by 11979 * sending eager as an option since it violates 11980 * the option semantics. So remove the eager as 11981 * option since TLI/XTI app doesn't need it anyway. 11982 */ 11983 if (!TCP_IS_SOCKET(listener)) { 11984 conn_ind->OPT_length = 0; 11985 conn_ind->OPT_offset = 0; 11986 } 11987 if (listener->tcp_state == TCPS_CLOSED || 11988 TCP_IS_DETACHED(listener)) { 11989 /* 11990 * If listener has closed, it would have caused a 11991 * a cleanup/blowoff to happen for the eager. We 11992 * just need to return. 11993 */ 11994 freemsg(mp); 11995 return; 11996 } 11997 11998 11999 /* 12000 * if the conn_req_q is full defer passing up the 12001 * T_CONN_IND until space is availabe after t_accept() 12002 * processing 12003 */ 12004 mutex_enter(&listener->tcp_eager_lock); 12005 if (listener->tcp_conn_req_cnt_q < listener->tcp_conn_req_max) { 12006 tcp_t *tail; 12007 12008 /* 12009 * The eager already has an extra ref put in tcp_rput_data 12010 * so that it stays till accept comes back even though it 12011 * might get into TCPS_CLOSED as a result of a TH_RST etc. 12012 */ 12013 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 12014 listener->tcp_conn_req_cnt_q0--; 12015 listener->tcp_conn_req_cnt_q++; 12016 12017 /* Move from SYN_RCVD to ESTABLISHED list */ 12018 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12019 tcp->tcp_eager_prev_q0; 12020 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12021 tcp->tcp_eager_next_q0; 12022 tcp->tcp_eager_prev_q0 = NULL; 12023 tcp->tcp_eager_next_q0 = NULL; 12024 12025 /* 12026 * Insert at end of the queue because sockfs 12027 * sends down T_CONN_RES in chronological 12028 * order. Leaving the older conn indications 12029 * at front of the queue helps reducing search 12030 * time. 12031 */ 12032 tail = listener->tcp_eager_last_q; 12033 if (tail != NULL) 12034 tail->tcp_eager_next_q = tcp; 12035 else 12036 listener->tcp_eager_next_q = tcp; 12037 listener->tcp_eager_last_q = tcp; 12038 tcp->tcp_eager_next_q = NULL; 12039 /* 12040 * Delay sending up the T_conn_ind until we are 12041 * done with the eager. Once we have have sent up 12042 * the T_conn_ind, the accept can potentially complete 12043 * any time and release the refhold we have on the eager. 12044 */ 12045 need_send_conn_ind = B_TRUE; 12046 } else { 12047 /* 12048 * Defer connection on q0 and set deferred 12049 * connection bit true 12050 */ 12051 tcp->tcp_conn_def_q0 = B_TRUE; 12052 12053 /* take tcp out of q0 ... */ 12054 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 12055 tcp->tcp_eager_next_q0; 12056 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 12057 tcp->tcp_eager_prev_q0; 12058 12059 /* ... and place it at the end of q0 */ 12060 tcp->tcp_eager_prev_q0 = listener->tcp_eager_prev_q0; 12061 tcp->tcp_eager_next_q0 = listener; 12062 listener->tcp_eager_prev_q0->tcp_eager_next_q0 = tcp; 12063 listener->tcp_eager_prev_q0 = tcp; 12064 tcp->tcp_conn.tcp_eager_conn_ind = mp; 12065 } 12066 12067 /* we have timed out before */ 12068 if (tcp->tcp_syn_rcvd_timeout != 0) { 12069 tcp->tcp_syn_rcvd_timeout = 0; 12070 listener->tcp_syn_rcvd_timeout--; 12071 if (listener->tcp_syn_defense && 12072 listener->tcp_syn_rcvd_timeout <= 12073 (tcp_conn_req_max_q0 >> 5) && 12074 10*MINUTES < TICK_TO_MSEC(lbolt64 - 12075 listener->tcp_last_rcv_lbolt)) { 12076 /* 12077 * Turn off the defense mode if we 12078 * believe the SYN attack is over. 12079 */ 12080 listener->tcp_syn_defense = B_FALSE; 12081 if (listener->tcp_ip_addr_cache) { 12082 kmem_free((void *)listener->tcp_ip_addr_cache, 12083 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t)); 12084 listener->tcp_ip_addr_cache = NULL; 12085 } 12086 } 12087 } 12088 addr_cache = (ipaddr_t *)(listener->tcp_ip_addr_cache); 12089 if (addr_cache != NULL) { 12090 /* 12091 * We have finished a 3-way handshake with this 12092 * remote host. This proves the IP addr is good. 12093 * Cache it! 12094 */ 12095 addr_cache[IP_ADDR_CACHE_HASH( 12096 tcp->tcp_remote)] = tcp->tcp_remote; 12097 } 12098 mutex_exit(&listener->tcp_eager_lock); 12099 if (need_send_conn_ind) 12100 putnext(listener->tcp_rq, mp); 12101 } 12102 12103 mblk_t * 12104 tcp_find_pktinfo(tcp_t *tcp, mblk_t *mp, uint_t *ipversp, uint_t *ip_hdr_lenp, 12105 uint_t *ifindexp, ip6_pkt_t *ippp) 12106 { 12107 in_pktinfo_t *pinfo; 12108 ip6_t *ip6h; 12109 uchar_t *rptr; 12110 mblk_t *first_mp = mp; 12111 boolean_t mctl_present = B_FALSE; 12112 uint_t ifindex = 0; 12113 ip6_pkt_t ipp; 12114 uint_t ipvers; 12115 uint_t ip_hdr_len; 12116 12117 rptr = mp->b_rptr; 12118 ASSERT(OK_32PTR(rptr)); 12119 ASSERT(tcp != NULL); 12120 ipp.ipp_fields = 0; 12121 12122 switch DB_TYPE(mp) { 12123 case M_CTL: 12124 mp = mp->b_cont; 12125 if (mp == NULL) { 12126 freemsg(first_mp); 12127 return (NULL); 12128 } 12129 if (DB_TYPE(mp) != M_DATA) { 12130 freemsg(first_mp); 12131 return (NULL); 12132 } 12133 mctl_present = B_TRUE; 12134 break; 12135 case M_DATA: 12136 break; 12137 default: 12138 cmn_err(CE_NOTE, "tcp_find_pktinfo: unknown db_type"); 12139 freemsg(mp); 12140 return (NULL); 12141 } 12142 ipvers = IPH_HDR_VERSION(rptr); 12143 if (ipvers == IPV4_VERSION) { 12144 if (tcp == NULL) { 12145 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12146 goto done; 12147 } 12148 12149 ipp.ipp_fields |= IPPF_HOPLIMIT; 12150 ipp.ipp_hoplimit = ((ipha_t *)rptr)->ipha_ttl; 12151 12152 /* 12153 * If we have IN_PKTINFO in an M_CTL and tcp_ipv6_recvancillary 12154 * has TCP_IPV6_RECVPKTINFO set, pass I/F index along in ipp. 12155 */ 12156 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) && 12157 mctl_present) { 12158 pinfo = (in_pktinfo_t *)first_mp->b_rptr; 12159 if ((MBLKL(first_mp) == sizeof (in_pktinfo_t)) && 12160 (pinfo->in_pkt_ulp_type == IN_PKTINFO) && 12161 (pinfo->in_pkt_flags & IPF_RECVIF)) { 12162 ipp.ipp_fields |= IPPF_IFINDEX; 12163 ipp.ipp_ifindex = pinfo->in_pkt_ifindex; 12164 ifindex = pinfo->in_pkt_ifindex; 12165 } 12166 freeb(first_mp); 12167 mctl_present = B_FALSE; 12168 } 12169 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12170 } else { 12171 ip6h = (ip6_t *)rptr; 12172 12173 ASSERT(ipvers == IPV6_VERSION); 12174 ipp.ipp_fields = IPPF_HOPLIMIT | IPPF_TCLASS; 12175 ipp.ipp_tclass = (ip6h->ip6_flow & 0x0FF00000) >> 20; 12176 ipp.ipp_hoplimit = ip6h->ip6_hops; 12177 12178 if (ip6h->ip6_nxt != IPPROTO_TCP) { 12179 uint8_t nexthdrp; 12180 12181 /* Look for ifindex information */ 12182 if (ip6h->ip6_nxt == IPPROTO_RAW) { 12183 ip6i_t *ip6i = (ip6i_t *)ip6h; 12184 if ((uchar_t *)&ip6i[1] > mp->b_wptr) { 12185 BUMP_MIB(&ip_mib, tcpInErrs); 12186 freemsg(first_mp); 12187 return (NULL); 12188 } 12189 12190 if (ip6i->ip6i_flags & IP6I_IFINDEX) { 12191 ASSERT(ip6i->ip6i_ifindex != 0); 12192 ipp.ipp_fields |= IPPF_IFINDEX; 12193 ipp.ipp_ifindex = ip6i->ip6i_ifindex; 12194 ifindex = ip6i->ip6i_ifindex; 12195 } 12196 rptr = (uchar_t *)&ip6i[1]; 12197 mp->b_rptr = rptr; 12198 if (rptr == mp->b_wptr) { 12199 mblk_t *mp1; 12200 mp1 = mp->b_cont; 12201 freeb(mp); 12202 mp = mp1; 12203 rptr = mp->b_rptr; 12204 } 12205 if (MBLKL(mp) < IPV6_HDR_LEN + 12206 sizeof (tcph_t)) { 12207 BUMP_MIB(&ip_mib, tcpInErrs); 12208 freemsg(first_mp); 12209 return (NULL); 12210 } 12211 ip6h = (ip6_t *)rptr; 12212 } 12213 12214 /* 12215 * Find any potentially interesting extension headers 12216 * as well as the length of the IPv6 + extension 12217 * headers. 12218 */ 12219 ip_hdr_len = ip_find_hdr_v6(mp, ip6h, &ipp, &nexthdrp); 12220 /* Verify if this is a TCP packet */ 12221 if (nexthdrp != IPPROTO_TCP) { 12222 BUMP_MIB(&ip_mib, tcpInErrs); 12223 freemsg(first_mp); 12224 return (NULL); 12225 } 12226 } else { 12227 ip_hdr_len = IPV6_HDR_LEN; 12228 } 12229 } 12230 12231 done: 12232 if (ipversp != NULL) 12233 *ipversp = ipvers; 12234 if (ip_hdr_lenp != NULL) 12235 *ip_hdr_lenp = ip_hdr_len; 12236 if (ippp != NULL) 12237 *ippp = ipp; 12238 if (ifindexp != NULL) 12239 *ifindexp = ifindex; 12240 if (mctl_present) { 12241 freeb(first_mp); 12242 } 12243 return (mp); 12244 } 12245 12246 /* 12247 * Handle M_DATA messages from IP. Its called directly from IP via 12248 * squeue for AF_INET type sockets fast path. No M_CTL are expected 12249 * in this path. 12250 * 12251 * For everything else (including AF_INET6 sockets with 'tcp_ipversion' 12252 * v4 and v6), we are called through tcp_input() and a M_CTL can 12253 * be present for options but tcp_find_pktinfo() deals with it. We 12254 * only expect M_DATA packets after tcp_find_pktinfo() is done. 12255 * 12256 * The first argument is always the connp/tcp to which the mp belongs. 12257 * There are no exceptions to this rule. The caller has already put 12258 * a reference on this connp/tcp and once tcp_rput_data() returns, 12259 * the squeue will do the refrele. 12260 * 12261 * The TH_SYN for the listener directly go to tcp_conn_request via 12262 * squeue. 12263 * 12264 * sqp: NULL = recursive, sqp != NULL means called from squeue 12265 */ 12266 void 12267 tcp_rput_data(void *arg, mblk_t *mp, void *arg2) 12268 { 12269 int32_t bytes_acked; 12270 int32_t gap; 12271 mblk_t *mp1; 12272 uint_t flags; 12273 uint32_t new_swnd = 0; 12274 uchar_t *iphdr; 12275 uchar_t *rptr; 12276 int32_t rgap; 12277 uint32_t seg_ack; 12278 int seg_len; 12279 uint_t ip_hdr_len; 12280 uint32_t seg_seq; 12281 tcph_t *tcph; 12282 int urp; 12283 tcp_opt_t tcpopt; 12284 uint_t ipvers; 12285 ip6_pkt_t ipp; 12286 boolean_t ofo_seg = B_FALSE; /* Out of order segment */ 12287 uint32_t cwnd; 12288 uint32_t add; 12289 int npkt; 12290 int mss; 12291 conn_t *connp = (conn_t *)arg; 12292 squeue_t *sqp = (squeue_t *)arg2; 12293 tcp_t *tcp = connp->conn_tcp; 12294 12295 /* 12296 * RST from fused tcp loopback peer should trigger an unfuse. 12297 */ 12298 if (tcp->tcp_fused) { 12299 TCP_STAT(tcp_fusion_aborted); 12300 tcp_unfuse(tcp); 12301 } 12302 12303 iphdr = mp->b_rptr; 12304 rptr = mp->b_rptr; 12305 ASSERT(OK_32PTR(rptr)); 12306 12307 /* 12308 * An AF_INET socket is not capable of receiving any pktinfo. Do inline 12309 * processing here. For rest call tcp_find_pktinfo to fill up the 12310 * necessary information. 12311 */ 12312 if (IPCL_IS_TCP4(connp)) { 12313 ipvers = IPV4_VERSION; 12314 ip_hdr_len = IPH_HDR_LENGTH(rptr); 12315 } else { 12316 mp = tcp_find_pktinfo(tcp, mp, &ipvers, &ip_hdr_len, 12317 NULL, &ipp); 12318 if (mp == NULL) { 12319 TCP_STAT(tcp_rput_v6_error); 12320 return; 12321 } 12322 iphdr = mp->b_rptr; 12323 rptr = mp->b_rptr; 12324 } 12325 ASSERT(DB_TYPE(mp) == M_DATA); 12326 12327 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12328 seg_seq = ABE32_TO_U32(tcph->th_seq); 12329 seg_ack = ABE32_TO_U32(tcph->th_ack); 12330 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 12331 seg_len = (int)(mp->b_wptr - rptr) - 12332 (ip_hdr_len + TCP_HDR_LENGTH(tcph)); 12333 if ((mp1 = mp->b_cont) != NULL && mp1->b_datap->db_type == M_DATA) { 12334 do { 12335 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 12336 (uintptr_t)INT_MAX); 12337 seg_len += (int)(mp1->b_wptr - mp1->b_rptr); 12338 } while ((mp1 = mp1->b_cont) != NULL && 12339 mp1->b_datap->db_type == M_DATA); 12340 } 12341 12342 if (tcp->tcp_state == TCPS_TIME_WAIT) { 12343 tcp_time_wait_processing(tcp, mp, seg_seq, seg_ack, 12344 seg_len, tcph); 12345 return; 12346 } 12347 12348 if (sqp != NULL) { 12349 /* 12350 * This is the correct place to update tcp_last_recv_time. Note 12351 * that it is also updated for tcp structure that belongs to 12352 * global and listener queues which do not really need updating. 12353 * But that should not cause any harm. And it is updated for 12354 * all kinds of incoming segments, not only for data segments. 12355 */ 12356 tcp->tcp_last_recv_time = lbolt; 12357 } 12358 12359 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 12360 12361 BUMP_LOCAL(tcp->tcp_ibsegs); 12362 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 12363 12364 if ((flags & TH_URG) && sqp != NULL) { 12365 /* 12366 * TCP can't handle urgent pointers that arrive before 12367 * the connection has been accept()ed since it can't 12368 * buffer OOB data. Discard segment if this happens. 12369 * 12370 * Nor can it reassemble urgent pointers, so discard 12371 * if it's not the next segment expected. 12372 * 12373 * Otherwise, collapse chain into one mblk (discard if 12374 * that fails). This makes sure the headers, retransmitted 12375 * data, and new data all are in the same mblk. 12376 */ 12377 ASSERT(mp != NULL); 12378 if (tcp->tcp_listener || !pullupmsg(mp, -1)) { 12379 freemsg(mp); 12380 return; 12381 } 12382 /* Update pointers into message */ 12383 iphdr = rptr = mp->b_rptr; 12384 tcph = (tcph_t *)&rptr[ip_hdr_len]; 12385 if (SEQ_GT(seg_seq, tcp->tcp_rnxt)) { 12386 /* 12387 * Since we can't handle any data with this urgent 12388 * pointer that is out of sequence, we expunge 12389 * the data. This allows us to still register 12390 * the urgent mark and generate the M_PCSIG, 12391 * which we can do. 12392 */ 12393 mp->b_wptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12394 seg_len = 0; 12395 } 12396 } 12397 12398 switch (tcp->tcp_state) { 12399 case TCPS_SYN_SENT: 12400 if (flags & TH_ACK) { 12401 /* 12402 * Note that our stack cannot send data before a 12403 * connection is established, therefore the 12404 * following check is valid. Otherwise, it has 12405 * to be changed. 12406 */ 12407 if (SEQ_LEQ(seg_ack, tcp->tcp_iss) || 12408 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12409 freemsg(mp); 12410 if (flags & TH_RST) 12411 return; 12412 tcp_xmit_ctl("TCPS_SYN_SENT-Bad_seq", 12413 tcp, seg_ack, 0, TH_RST); 12414 return; 12415 } 12416 ASSERT(tcp->tcp_suna + 1 == seg_ack); 12417 } 12418 if (flags & TH_RST) { 12419 freemsg(mp); 12420 if (flags & TH_ACK) 12421 (void) tcp_clean_death(tcp, 12422 ECONNREFUSED, 13); 12423 return; 12424 } 12425 if (!(flags & TH_SYN)) { 12426 freemsg(mp); 12427 return; 12428 } 12429 12430 /* Process all TCP options. */ 12431 tcp_process_options(tcp, tcph); 12432 /* 12433 * The following changes our rwnd to be a multiple of the 12434 * MIN(peer MSS, our MSS) for performance reason. 12435 */ 12436 (void) tcp_rwnd_set(tcp, MSS_ROUNDUP(tcp->tcp_rq->q_hiwat, 12437 tcp->tcp_mss)); 12438 12439 /* Is the other end ECN capable? */ 12440 if (tcp->tcp_ecn_ok) { 12441 if ((flags & (TH_ECE|TH_CWR)) != TH_ECE) { 12442 tcp->tcp_ecn_ok = B_FALSE; 12443 } 12444 } 12445 /* 12446 * Clear ECN flags because it may interfere with later 12447 * processing. 12448 */ 12449 flags &= ~(TH_ECE|TH_CWR); 12450 12451 tcp->tcp_irs = seg_seq; 12452 tcp->tcp_rack = seg_seq; 12453 tcp->tcp_rnxt = seg_seq + 1; 12454 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12455 if (!TCP_IS_DETACHED(tcp)) { 12456 /* Allocate room for SACK options if needed. */ 12457 if (tcp->tcp_snd_sack_ok) { 12458 (void) mi_set_sth_wroff(tcp->tcp_rq, 12459 tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 12460 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12461 } else { 12462 (void) mi_set_sth_wroff(tcp->tcp_rq, 12463 tcp->tcp_hdr_len + 12464 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra)); 12465 } 12466 } 12467 if (flags & TH_ACK) { 12468 /* 12469 * If we can't get the confirmation upstream, pretend 12470 * we didn't even see this one. 12471 * 12472 * XXX: how can we pretend we didn't see it if we 12473 * have updated rnxt et. al. 12474 * 12475 * For loopback we defer sending up the T_CONN_CON 12476 * until after some checks below. 12477 */ 12478 mp1 = NULL; 12479 if (!tcp_conn_con(tcp, iphdr, tcph, mp, 12480 tcp->tcp_loopback ? &mp1 : NULL)) { 12481 freemsg(mp); 12482 return; 12483 } 12484 /* SYN was acked - making progress */ 12485 if (tcp->tcp_ipversion == IPV6_VERSION) 12486 tcp->tcp_ip_forward_progress = B_TRUE; 12487 12488 /* One for the SYN */ 12489 tcp->tcp_suna = tcp->tcp_iss + 1; 12490 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 12491 tcp->tcp_state = TCPS_ESTABLISHED; 12492 12493 /* 12494 * If SYN was retransmitted, need to reset all 12495 * retransmission info. This is because this 12496 * segment will be treated as a dup ACK. 12497 */ 12498 if (tcp->tcp_rexmit) { 12499 tcp->tcp_rexmit = B_FALSE; 12500 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 12501 tcp->tcp_rexmit_max = tcp->tcp_snxt; 12502 tcp->tcp_snd_burst = tcp->tcp_localnet ? 12503 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 12504 tcp->tcp_ms_we_have_waited = 0; 12505 12506 /* 12507 * Set tcp_cwnd back to 1 MSS, per 12508 * recommendation from 12509 * draft-floyd-incr-init-win-01.txt, 12510 * Increasing TCP's Initial Window. 12511 */ 12512 tcp->tcp_cwnd = tcp->tcp_mss; 12513 } 12514 12515 tcp->tcp_swl1 = seg_seq; 12516 tcp->tcp_swl2 = seg_ack; 12517 12518 new_swnd = BE16_TO_U16(tcph->th_win); 12519 tcp->tcp_swnd = new_swnd; 12520 if (new_swnd > tcp->tcp_max_swnd) 12521 tcp->tcp_max_swnd = new_swnd; 12522 12523 /* 12524 * Always send the three-way handshake ack immediately 12525 * in order to make the connection complete as soon as 12526 * possible on the accepting host. 12527 */ 12528 flags |= TH_ACK_NEEDED; 12529 12530 /* 12531 * Special case for loopback. At this point we have 12532 * received SYN-ACK from the remote endpoint. In 12533 * order to ensure that both endpoints reach the 12534 * fused state prior to any data exchange, the final 12535 * ACK needs to be sent before we indicate T_CONN_CON 12536 * to the module upstream. 12537 */ 12538 if (tcp->tcp_loopback) { 12539 mblk_t *ack_mp; 12540 12541 ASSERT(!tcp->tcp_unfusable); 12542 ASSERT(mp1 != NULL); 12543 /* 12544 * For loopback, we always get a pure SYN-ACK 12545 * and only need to send back the final ACK 12546 * with no data (this is because the other 12547 * tcp is ours and we don't do T/TCP). This 12548 * final ACK triggers the passive side to 12549 * perform fusion in ESTABLISHED state. 12550 */ 12551 if ((ack_mp = tcp_ack_mp(tcp)) != NULL) { 12552 if (tcp->tcp_ack_tid != 0) { 12553 (void) TCP_TIMER_CANCEL(tcp, 12554 tcp->tcp_ack_tid); 12555 tcp->tcp_ack_tid = 0; 12556 } 12557 TCP_RECORD_TRACE(tcp, ack_mp, 12558 TCP_TRACE_SEND_PKT); 12559 tcp_send_data(tcp, tcp->tcp_wq, ack_mp); 12560 BUMP_LOCAL(tcp->tcp_obsegs); 12561 BUMP_MIB(&tcp_mib, tcpOutAck); 12562 12563 /* Send up T_CONN_CON */ 12564 putnext(tcp->tcp_rq, mp1); 12565 12566 freemsg(mp); 12567 return; 12568 } 12569 /* 12570 * Forget fusion; we need to handle more 12571 * complex cases below. Send the deferred 12572 * T_CONN_CON message upstream and proceed 12573 * as usual. Mark this tcp as not capable 12574 * of fusion. 12575 */ 12576 TCP_STAT(tcp_fusion_unfusable); 12577 tcp->tcp_unfusable = B_TRUE; 12578 putnext(tcp->tcp_rq, mp1); 12579 } 12580 12581 /* 12582 * Check to see if there is data to be sent. If 12583 * yes, set the transmit flag. Then check to see 12584 * if received data processing needs to be done. 12585 * If not, go straight to xmit_check. This short 12586 * cut is OK as we don't support T/TCP. 12587 */ 12588 if (tcp->tcp_unsent) 12589 flags |= TH_XMIT_NEEDED; 12590 12591 if (seg_len == 0 && !(flags & TH_URG)) { 12592 freemsg(mp); 12593 goto xmit_check; 12594 } 12595 12596 flags &= ~TH_SYN; 12597 seg_seq++; 12598 break; 12599 } 12600 tcp->tcp_state = TCPS_SYN_RCVD; 12601 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, tcp->tcp_mss, 12602 NULL, NULL, tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 12603 if (mp1) { 12604 mblk_setcred(mp1, tcp->tcp_cred); 12605 DB_CPID(mp1) = tcp->tcp_cpid; 12606 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 12607 tcp_send_data(tcp, tcp->tcp_wq, mp1); 12608 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 12609 } 12610 freemsg(mp); 12611 return; 12612 case TCPS_SYN_RCVD: 12613 if (flags & TH_ACK) { 12614 /* 12615 * In this state, a SYN|ACK packet is either bogus 12616 * because the other side must be ACKing our SYN which 12617 * indicates it has seen the ACK for their SYN and 12618 * shouldn't retransmit it or we're crossing SYNs 12619 * on active open. 12620 */ 12621 if ((flags & TH_SYN) && !tcp->tcp_active_open) { 12622 freemsg(mp); 12623 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_syn", 12624 tcp, seg_ack, 0, TH_RST); 12625 return; 12626 } 12627 /* 12628 * NOTE: RFC 793 pg. 72 says this should be 12629 * tcp->tcp_suna <= seg_ack <= tcp->tcp_snxt 12630 * but that would mean we have an ack that ignored 12631 * our SYN. 12632 */ 12633 if (SEQ_LEQ(seg_ack, tcp->tcp_suna) || 12634 SEQ_GT(seg_ack, tcp->tcp_snxt)) { 12635 freemsg(mp); 12636 tcp_xmit_ctl("TCPS_SYN_RCVD-bad_ack", 12637 tcp, seg_ack, 0, TH_RST); 12638 return; 12639 } 12640 } 12641 break; 12642 case TCPS_LISTEN: 12643 /* 12644 * Only a TLI listener can come through this path when a 12645 * acceptor is going back to be a listener and a packet 12646 * for the acceptor hits the classifier. For a socket 12647 * listener, this can never happen because a listener 12648 * can never accept connection on itself and hence a 12649 * socket acceptor can not go back to being a listener. 12650 */ 12651 ASSERT(!TCP_IS_SOCKET(tcp)); 12652 /*FALLTHRU*/ 12653 case TCPS_CLOSED: 12654 case TCPS_BOUND: { 12655 conn_t *new_connp; 12656 12657 new_connp = ipcl_classify(mp, connp->conn_zoneid); 12658 if (new_connp != NULL) { 12659 tcp_reinput(new_connp, mp, connp->conn_sqp); 12660 return; 12661 } 12662 /* We failed to classify. For now just drop the packet */ 12663 freemsg(mp); 12664 return; 12665 } 12666 case TCPS_IDLE: 12667 /* 12668 * Handle the case where the tcp_clean_death() has happened 12669 * on a connection (application hasn't closed yet) but a packet 12670 * was already queued on squeue before tcp_clean_death() 12671 * was processed. Calling tcp_clean_death() twice on same 12672 * connection can result in weird behaviour. 12673 */ 12674 freemsg(mp); 12675 return; 12676 default: 12677 break; 12678 } 12679 12680 /* 12681 * Already on the correct queue/perimeter. 12682 * If this is a detached connection and not an eager 12683 * connection hanging off a listener then new data 12684 * (past the FIN) will cause a reset. 12685 * We do a special check here where it 12686 * is out of the main line, rather than check 12687 * if we are detached every time we see new 12688 * data down below. 12689 */ 12690 if (TCP_IS_DETACHED_NONEAGER(tcp) && 12691 (seg_len > 0 && SEQ_GT(seg_seq + seg_len, tcp->tcp_rnxt))) { 12692 BUMP_MIB(&tcp_mib, tcpInClosed); 12693 TCP_RECORD_TRACE(tcp, 12694 mp, TCP_TRACE_RECV_PKT); 12695 12696 freemsg(mp); 12697 /* 12698 * This could be an SSL closure alert. We're detached so just 12699 * acknowledge it this last time. 12700 */ 12701 if (tcp->tcp_kssl_ctx != NULL) { 12702 kssl_release_ctx(tcp->tcp_kssl_ctx); 12703 tcp->tcp_kssl_ctx = NULL; 12704 12705 tcp->tcp_rnxt += seg_len; 12706 U32_TO_ABE32(tcp->tcp_rnxt, tcp->tcp_tcph->th_ack); 12707 flags |= TH_ACK_NEEDED; 12708 goto ack_check; 12709 } 12710 12711 tcp_xmit_ctl("new data when detached", tcp, 12712 tcp->tcp_snxt, 0, TH_RST); 12713 (void) tcp_clean_death(tcp, EPROTO, 12); 12714 return; 12715 } 12716 12717 mp->b_rptr = (uchar_t *)tcph + TCP_HDR_LENGTH(tcph); 12718 urp = BE16_TO_U16(tcph->th_urp) - TCP_OLD_URP_INTERPRETATION; 12719 new_swnd = BE16_TO_U16(tcph->th_win) << 12720 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 12721 mss = tcp->tcp_mss; 12722 12723 if (tcp->tcp_snd_ts_ok) { 12724 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 12725 /* 12726 * This segment is not acceptable. 12727 * Drop it and send back an ACK. 12728 */ 12729 freemsg(mp); 12730 flags |= TH_ACK_NEEDED; 12731 goto ack_check; 12732 } 12733 } else if (tcp->tcp_snd_sack_ok) { 12734 ASSERT(tcp->tcp_sack_info != NULL); 12735 tcpopt.tcp = tcp; 12736 /* 12737 * SACK info in already updated in tcp_parse_options. Ignore 12738 * all other TCP options... 12739 */ 12740 (void) tcp_parse_options(tcph, &tcpopt); 12741 } 12742 try_again:; 12743 gap = seg_seq - tcp->tcp_rnxt; 12744 rgap = tcp->tcp_rwnd - (gap + seg_len); 12745 /* 12746 * gap is the amount of sequence space between what we expect to see 12747 * and what we got for seg_seq. A positive value for gap means 12748 * something got lost. A negative value means we got some old stuff. 12749 */ 12750 if (gap < 0) { 12751 /* Old stuff present. Is the SYN in there? */ 12752 if (seg_seq == tcp->tcp_irs && (flags & TH_SYN) && 12753 (seg_len != 0)) { 12754 flags &= ~TH_SYN; 12755 seg_seq++; 12756 urp--; 12757 /* Recompute the gaps after noting the SYN. */ 12758 goto try_again; 12759 } 12760 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 12761 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 12762 (seg_len > -gap ? -gap : seg_len)); 12763 /* Remove the old stuff from seg_len. */ 12764 seg_len += gap; 12765 /* 12766 * Anything left? 12767 * Make sure to check for unack'd FIN when rest of data 12768 * has been previously ack'd. 12769 */ 12770 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 12771 /* 12772 * Resets are only valid if they lie within our offered 12773 * window. If the RST bit is set, we just ignore this 12774 * segment. 12775 */ 12776 if (flags & TH_RST) { 12777 freemsg(mp); 12778 return; 12779 } 12780 12781 /* 12782 * The arriving of dup data packets indicate that we 12783 * may have postponed an ack for too long, or the other 12784 * side's RTT estimate is out of shape. Start acking 12785 * more often. 12786 */ 12787 if (SEQ_GEQ(seg_seq + seg_len - gap, tcp->tcp_rack) && 12788 tcp->tcp_rack_cnt >= 1 && 12789 tcp->tcp_rack_abs_max > 2) { 12790 tcp->tcp_rack_abs_max--; 12791 } 12792 tcp->tcp_rack_cur_max = 1; 12793 12794 /* 12795 * This segment is "unacceptable". None of its 12796 * sequence space lies within our advertized window. 12797 * 12798 * Adjust seg_len to the original value for tracing. 12799 */ 12800 seg_len -= gap; 12801 if (tcp->tcp_debug) { 12802 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 12803 "tcp_rput: unacceptable, gap %d, rgap %d, " 12804 "flags 0x%x, seg_seq %u, seg_ack %u, " 12805 "seg_len %d, rnxt %u, snxt %u, %s", 12806 gap, rgap, flags, seg_seq, seg_ack, 12807 seg_len, tcp->tcp_rnxt, tcp->tcp_snxt, 12808 tcp_display(tcp, NULL, 12809 DISP_ADDR_AND_PORT)); 12810 } 12811 12812 /* 12813 * Arrange to send an ACK in response to the 12814 * unacceptable segment per RFC 793 page 69. There 12815 * is only one small difference between ours and the 12816 * acceptability test in the RFC - we accept ACK-only 12817 * packet with SEG.SEQ = RCV.NXT+RCV.WND and no ACK 12818 * will be generated. 12819 * 12820 * Note that we have to ACK an ACK-only packet at least 12821 * for stacks that send 0-length keep-alives with 12822 * SEG.SEQ = SND.NXT-1 as recommended by RFC1122, 12823 * section 4.2.3.6. As long as we don't ever generate 12824 * an unacceptable packet in response to an incoming 12825 * packet that is unacceptable, it should not cause 12826 * "ACK wars". 12827 */ 12828 flags |= TH_ACK_NEEDED; 12829 12830 /* 12831 * Continue processing this segment in order to use the 12832 * ACK information it contains, but skip all other 12833 * sequence-number processing. Processing the ACK 12834 * information is necessary in order to 12835 * re-synchronize connections that may have lost 12836 * synchronization. 12837 * 12838 * We clear seg_len and flag fields related to 12839 * sequence number processing as they are not 12840 * to be trusted for an unacceptable segment. 12841 */ 12842 seg_len = 0; 12843 flags &= ~(TH_SYN | TH_FIN | TH_URG); 12844 goto process_ack; 12845 } 12846 12847 /* Fix seg_seq, and chew the gap off the front. */ 12848 seg_seq = tcp->tcp_rnxt; 12849 urp += gap; 12850 do { 12851 mblk_t *mp2; 12852 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 12853 (uintptr_t)UINT_MAX); 12854 gap += (uint_t)(mp->b_wptr - mp->b_rptr); 12855 if (gap > 0) { 12856 mp->b_rptr = mp->b_wptr - gap; 12857 break; 12858 } 12859 mp2 = mp; 12860 mp = mp->b_cont; 12861 freeb(mp2); 12862 } while (gap < 0); 12863 /* 12864 * If the urgent data has already been acknowledged, we 12865 * should ignore TH_URG below 12866 */ 12867 if (urp < 0) 12868 flags &= ~TH_URG; 12869 } 12870 /* 12871 * rgap is the amount of stuff received out of window. A negative 12872 * value is the amount out of window. 12873 */ 12874 if (rgap < 0) { 12875 mblk_t *mp2; 12876 12877 if (tcp->tcp_rwnd == 0) { 12878 BUMP_MIB(&tcp_mib, tcpInWinProbe); 12879 } else { 12880 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 12881 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 12882 } 12883 12884 /* 12885 * seg_len does not include the FIN, so if more than 12886 * just the FIN is out of window, we act like we don't 12887 * see it. (If just the FIN is out of window, rgap 12888 * will be zero and we will go ahead and acknowledge 12889 * the FIN.) 12890 */ 12891 flags &= ~TH_FIN; 12892 12893 /* Fix seg_len and make sure there is something left. */ 12894 seg_len += rgap; 12895 if (seg_len <= 0) { 12896 /* 12897 * Resets are only valid if they lie within our offered 12898 * window. If the RST bit is set, we just ignore this 12899 * segment. 12900 */ 12901 if (flags & TH_RST) { 12902 freemsg(mp); 12903 return; 12904 } 12905 12906 /* Per RFC 793, we need to send back an ACK. */ 12907 flags |= TH_ACK_NEEDED; 12908 12909 /* 12910 * Send SIGURG as soon as possible i.e. even 12911 * if the TH_URG was delivered in a window probe 12912 * packet (which will be unacceptable). 12913 * 12914 * We generate a signal if none has been generated 12915 * for this connection or if this is a new urgent 12916 * byte. Also send a zero-length "unmarked" message 12917 * to inform SIOCATMARK that this is not the mark. 12918 * 12919 * tcp_urp_last_valid is cleared when the T_exdata_ind 12920 * is sent up. This plus the check for old data 12921 * (gap >= 0) handles the wraparound of the sequence 12922 * number space without having to always track the 12923 * correct MAX(tcp_urp_last, tcp_rnxt). (BSD tracks 12924 * this max in its rcv_up variable). 12925 * 12926 * This prevents duplicate SIGURGS due to a "late" 12927 * zero-window probe when the T_EXDATA_IND has already 12928 * been sent up. 12929 */ 12930 if ((flags & TH_URG) && 12931 (!tcp->tcp_urp_last_valid || SEQ_GT(urp + seg_seq, 12932 tcp->tcp_urp_last))) { 12933 mp1 = allocb(0, BPRI_MED); 12934 if (mp1 == NULL) { 12935 freemsg(mp); 12936 return; 12937 } 12938 if (!TCP_IS_DETACHED(tcp) && 12939 !putnextctl1(tcp->tcp_rq, M_PCSIG, 12940 SIGURG)) { 12941 /* Try again on the rexmit. */ 12942 freemsg(mp1); 12943 freemsg(mp); 12944 return; 12945 } 12946 /* 12947 * If the next byte would be the mark 12948 * then mark with MARKNEXT else mark 12949 * with NOTMARKNEXT. 12950 */ 12951 if (gap == 0 && urp == 0) 12952 mp1->b_flag |= MSGMARKNEXT; 12953 else 12954 mp1->b_flag |= MSGNOTMARKNEXT; 12955 freemsg(tcp->tcp_urp_mark_mp); 12956 tcp->tcp_urp_mark_mp = mp1; 12957 flags |= TH_SEND_URP_MARK; 12958 tcp->tcp_urp_last_valid = B_TRUE; 12959 tcp->tcp_urp_last = urp + seg_seq; 12960 } 12961 /* 12962 * If this is a zero window probe, continue to 12963 * process the ACK part. But we need to set seg_len 12964 * to 0 to avoid data processing. Otherwise just 12965 * drop the segment and send back an ACK. 12966 */ 12967 if (tcp->tcp_rwnd == 0 && seg_seq == tcp->tcp_rnxt) { 12968 flags &= ~(TH_SYN | TH_URG); 12969 seg_len = 0; 12970 goto process_ack; 12971 } else { 12972 freemsg(mp); 12973 goto ack_check; 12974 } 12975 } 12976 /* Pitch out of window stuff off the end. */ 12977 rgap = seg_len; 12978 mp2 = mp; 12979 do { 12980 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 12981 (uintptr_t)INT_MAX); 12982 rgap -= (int)(mp2->b_wptr - mp2->b_rptr); 12983 if (rgap < 0) { 12984 mp2->b_wptr += rgap; 12985 if ((mp1 = mp2->b_cont) != NULL) { 12986 mp2->b_cont = NULL; 12987 freemsg(mp1); 12988 } 12989 break; 12990 } 12991 } while ((mp2 = mp2->b_cont) != NULL); 12992 } 12993 ok:; 12994 /* 12995 * TCP should check ECN info for segments inside the window only. 12996 * Therefore the check should be done here. 12997 */ 12998 if (tcp->tcp_ecn_ok) { 12999 if (flags & TH_CWR) { 13000 tcp->tcp_ecn_echo_on = B_FALSE; 13001 } 13002 /* 13003 * Note that both ECN_CE and CWR can be set in the 13004 * same segment. In this case, we once again turn 13005 * on ECN_ECHO. 13006 */ 13007 if (tcp->tcp_ipversion == IPV4_VERSION) { 13008 uchar_t tos = ((ipha_t *)rptr)->ipha_type_of_service; 13009 13010 if ((tos & IPH_ECN_CE) == IPH_ECN_CE) { 13011 tcp->tcp_ecn_echo_on = B_TRUE; 13012 } 13013 } else { 13014 uint32_t vcf = ((ip6_t *)rptr)->ip6_vcf; 13015 13016 if ((vcf & htonl(IPH_ECN_CE << 20)) == 13017 htonl(IPH_ECN_CE << 20)) { 13018 tcp->tcp_ecn_echo_on = B_TRUE; 13019 } 13020 } 13021 } 13022 13023 /* 13024 * Check whether we can update tcp_ts_recent. This test is 13025 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 13026 * Extensions for High Performance: An Update", Internet Draft. 13027 */ 13028 if (tcp->tcp_snd_ts_ok && 13029 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 13030 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 13031 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 13032 tcp->tcp_last_rcv_lbolt = lbolt64; 13033 } 13034 13035 if (seg_seq != tcp->tcp_rnxt || tcp->tcp_reass_head) { 13036 /* 13037 * FIN in an out of order segment. We record this in 13038 * tcp_valid_bits and the seq num of FIN in tcp_ofo_fin_seq. 13039 * Clear the FIN so that any check on FIN flag will fail. 13040 * Remember that FIN also counts in the sequence number 13041 * space. So we need to ack out of order FIN only segments. 13042 */ 13043 if (flags & TH_FIN) { 13044 tcp->tcp_valid_bits |= TCP_OFO_FIN_VALID; 13045 tcp->tcp_ofo_fin_seq = seg_seq + seg_len; 13046 flags &= ~TH_FIN; 13047 flags |= TH_ACK_NEEDED; 13048 } 13049 if (seg_len > 0) { 13050 /* Fill in the SACK blk list. */ 13051 if (tcp->tcp_snd_sack_ok) { 13052 ASSERT(tcp->tcp_sack_info != NULL); 13053 tcp_sack_insert(tcp->tcp_sack_list, 13054 seg_seq, seg_seq + seg_len, 13055 &(tcp->tcp_num_sack_blk)); 13056 } 13057 13058 /* 13059 * Attempt reassembly and see if we have something 13060 * ready to go. 13061 */ 13062 mp = tcp_reass(tcp, mp, seg_seq); 13063 /* Always ack out of order packets */ 13064 flags |= TH_ACK_NEEDED | TH_PUSH; 13065 if (mp) { 13066 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 13067 (uintptr_t)INT_MAX); 13068 seg_len = mp->b_cont ? msgdsize(mp) : 13069 (int)(mp->b_wptr - mp->b_rptr); 13070 seg_seq = tcp->tcp_rnxt; 13071 /* 13072 * A gap is filled and the seq num and len 13073 * of the gap match that of a previously 13074 * received FIN, put the FIN flag back in. 13075 */ 13076 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13077 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13078 flags |= TH_FIN; 13079 tcp->tcp_valid_bits &= 13080 ~TCP_OFO_FIN_VALID; 13081 } 13082 } else { 13083 /* 13084 * Keep going even with NULL mp. 13085 * There may be a useful ACK or something else 13086 * we don't want to miss. 13087 * 13088 * But TCP should not perform fast retransmit 13089 * because of the ack number. TCP uses 13090 * seg_len == 0 to determine if it is a pure 13091 * ACK. And this is not a pure ACK. 13092 */ 13093 seg_len = 0; 13094 ofo_seg = B_TRUE; 13095 } 13096 } 13097 } else if (seg_len > 0) { 13098 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 13099 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 13100 /* 13101 * If an out of order FIN was received before, and the seq 13102 * num and len of the new segment match that of the FIN, 13103 * put the FIN flag back in. 13104 */ 13105 if ((tcp->tcp_valid_bits & TCP_OFO_FIN_VALID) && 13106 seg_seq + seg_len == tcp->tcp_ofo_fin_seq) { 13107 flags |= TH_FIN; 13108 tcp->tcp_valid_bits &= ~TCP_OFO_FIN_VALID; 13109 } 13110 } 13111 if ((flags & (TH_RST | TH_SYN | TH_URG | TH_ACK)) != TH_ACK) { 13112 if (flags & TH_RST) { 13113 freemsg(mp); 13114 switch (tcp->tcp_state) { 13115 case TCPS_SYN_RCVD: 13116 (void) tcp_clean_death(tcp, ECONNREFUSED, 14); 13117 break; 13118 case TCPS_ESTABLISHED: 13119 case TCPS_FIN_WAIT_1: 13120 case TCPS_FIN_WAIT_2: 13121 case TCPS_CLOSE_WAIT: 13122 (void) tcp_clean_death(tcp, ECONNRESET, 15); 13123 break; 13124 case TCPS_CLOSING: 13125 case TCPS_LAST_ACK: 13126 (void) tcp_clean_death(tcp, 0, 16); 13127 break; 13128 default: 13129 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13130 (void) tcp_clean_death(tcp, ENXIO, 17); 13131 break; 13132 } 13133 return; 13134 } 13135 if (flags & TH_SYN) { 13136 /* 13137 * See RFC 793, Page 71 13138 * 13139 * The seq number must be in the window as it should 13140 * be "fixed" above. If it is outside window, it should 13141 * be already rejected. Note that we allow seg_seq to be 13142 * rnxt + rwnd because we want to accept 0 window probe. 13143 */ 13144 ASSERT(SEQ_GEQ(seg_seq, tcp->tcp_rnxt) && 13145 SEQ_LEQ(seg_seq, tcp->tcp_rnxt + tcp->tcp_rwnd)); 13146 freemsg(mp); 13147 /* 13148 * If the ACK flag is not set, just use our snxt as the 13149 * seq number of the RST segment. 13150 */ 13151 if (!(flags & TH_ACK)) { 13152 seg_ack = tcp->tcp_snxt; 13153 } 13154 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 13155 TH_RST|TH_ACK); 13156 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 13157 (void) tcp_clean_death(tcp, ECONNRESET, 18); 13158 return; 13159 } 13160 /* 13161 * urp could be -1 when the urp field in the packet is 0 13162 * and TCP_OLD_URP_INTERPRETATION is set. This implies that the urgent 13163 * byte was at seg_seq - 1, in which case we ignore the urgent flag. 13164 */ 13165 if (flags & TH_URG && urp >= 0) { 13166 if (!tcp->tcp_urp_last_valid || 13167 SEQ_GT(urp + seg_seq, tcp->tcp_urp_last)) { 13168 /* 13169 * If we haven't generated the signal yet for this 13170 * urgent pointer value, do it now. Also, send up a 13171 * zero-length M_DATA indicating whether or not this is 13172 * the mark. The latter is not needed when a 13173 * T_EXDATA_IND is sent up. However, if there are 13174 * allocation failures this code relies on the sender 13175 * retransmitting and the socket code for determining 13176 * the mark should not block waiting for the peer to 13177 * transmit. Thus, for simplicity we always send up the 13178 * mark indication. 13179 */ 13180 mp1 = allocb(0, BPRI_MED); 13181 if (mp1 == NULL) { 13182 freemsg(mp); 13183 return; 13184 } 13185 if (!TCP_IS_DETACHED(tcp) && 13186 !putnextctl1(tcp->tcp_rq, M_PCSIG, SIGURG)) { 13187 /* Try again on the rexmit. */ 13188 freemsg(mp1); 13189 freemsg(mp); 13190 return; 13191 } 13192 /* 13193 * Mark with NOTMARKNEXT for now. 13194 * The code below will change this to MARKNEXT 13195 * if we are at the mark. 13196 * 13197 * If there are allocation failures (e.g. in dupmsg 13198 * below) the next time tcp_rput_data sees the urgent 13199 * segment it will send up the MSG*MARKNEXT message. 13200 */ 13201 mp1->b_flag |= MSGNOTMARKNEXT; 13202 freemsg(tcp->tcp_urp_mark_mp); 13203 tcp->tcp_urp_mark_mp = mp1; 13204 flags |= TH_SEND_URP_MARK; 13205 #ifdef DEBUG 13206 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13207 "tcp_rput: sent M_PCSIG 2 seq %x urp %x " 13208 "last %x, %s", 13209 seg_seq, urp, tcp->tcp_urp_last, 13210 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13211 #endif /* DEBUG */ 13212 tcp->tcp_urp_last_valid = B_TRUE; 13213 tcp->tcp_urp_last = urp + seg_seq; 13214 } else if (tcp->tcp_urp_mark_mp != NULL) { 13215 /* 13216 * An allocation failure prevented the previous 13217 * tcp_rput_data from sending up the allocated 13218 * MSG*MARKNEXT message - send it up this time 13219 * around. 13220 */ 13221 flags |= TH_SEND_URP_MARK; 13222 } 13223 13224 /* 13225 * If the urgent byte is in this segment, make sure that it is 13226 * all by itself. This makes it much easier to deal with the 13227 * possibility of an allocation failure on the T_exdata_ind. 13228 * Note that seg_len is the number of bytes in the segment, and 13229 * urp is the offset into the segment of the urgent byte. 13230 * urp < seg_len means that the urgent byte is in this segment. 13231 */ 13232 if (urp < seg_len) { 13233 if (seg_len != 1) { 13234 uint32_t tmp_rnxt; 13235 /* 13236 * Break it up and feed it back in. 13237 * Re-attach the IP header. 13238 */ 13239 mp->b_rptr = iphdr; 13240 if (urp > 0) { 13241 /* 13242 * There is stuff before the urgent 13243 * byte. 13244 */ 13245 mp1 = dupmsg(mp); 13246 if (!mp1) { 13247 /* 13248 * Trim from urgent byte on. 13249 * The rest will come back. 13250 */ 13251 (void) adjmsg(mp, 13252 urp - seg_len); 13253 tcp_rput_data(connp, 13254 mp, NULL); 13255 return; 13256 } 13257 (void) adjmsg(mp1, urp - seg_len); 13258 /* Feed this piece back in. */ 13259 tmp_rnxt = tcp->tcp_rnxt; 13260 tcp_rput_data(connp, mp1, NULL); 13261 /* 13262 * If the data passed back in was not 13263 * processed (ie: bad ACK) sending 13264 * the remainder back in will cause a 13265 * loop. In this case, drop the 13266 * packet and let the sender try 13267 * sending a good packet. 13268 */ 13269 if (tmp_rnxt == tcp->tcp_rnxt) { 13270 freemsg(mp); 13271 return; 13272 } 13273 } 13274 if (urp != seg_len - 1) { 13275 uint32_t tmp_rnxt; 13276 /* 13277 * There is stuff after the urgent 13278 * byte. 13279 */ 13280 mp1 = dupmsg(mp); 13281 if (!mp1) { 13282 /* 13283 * Trim everything beyond the 13284 * urgent byte. The rest will 13285 * come back. 13286 */ 13287 (void) adjmsg(mp, 13288 urp + 1 - seg_len); 13289 tcp_rput_data(connp, 13290 mp, NULL); 13291 return; 13292 } 13293 (void) adjmsg(mp1, urp + 1 - seg_len); 13294 tmp_rnxt = tcp->tcp_rnxt; 13295 tcp_rput_data(connp, mp1, NULL); 13296 /* 13297 * If the data passed back in was not 13298 * processed (ie: bad ACK) sending 13299 * the remainder back in will cause a 13300 * loop. In this case, drop the 13301 * packet and let the sender try 13302 * sending a good packet. 13303 */ 13304 if (tmp_rnxt == tcp->tcp_rnxt) { 13305 freemsg(mp); 13306 return; 13307 } 13308 } 13309 tcp_rput_data(connp, mp, NULL); 13310 return; 13311 } 13312 /* 13313 * This segment contains only the urgent byte. We 13314 * have to allocate the T_exdata_ind, if we can. 13315 */ 13316 if (!tcp->tcp_urp_mp) { 13317 struct T_exdata_ind *tei; 13318 mp1 = allocb(sizeof (struct T_exdata_ind), 13319 BPRI_MED); 13320 if (!mp1) { 13321 /* 13322 * Sigh... It'll be back. 13323 * Generate any MSG*MARK message now. 13324 */ 13325 freemsg(mp); 13326 seg_len = 0; 13327 if (flags & TH_SEND_URP_MARK) { 13328 13329 13330 ASSERT(tcp->tcp_urp_mark_mp); 13331 tcp->tcp_urp_mark_mp->b_flag &= 13332 ~MSGNOTMARKNEXT; 13333 tcp->tcp_urp_mark_mp->b_flag |= 13334 MSGMARKNEXT; 13335 } 13336 goto ack_check; 13337 } 13338 mp1->b_datap->db_type = M_PROTO; 13339 tei = (struct T_exdata_ind *)mp1->b_rptr; 13340 tei->PRIM_type = T_EXDATA_IND; 13341 tei->MORE_flag = 0; 13342 mp1->b_wptr = (uchar_t *)&tei[1]; 13343 tcp->tcp_urp_mp = mp1; 13344 #ifdef DEBUG 13345 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13346 "tcp_rput: allocated exdata_ind %s", 13347 tcp_display(tcp, NULL, 13348 DISP_PORT_ONLY)); 13349 #endif /* DEBUG */ 13350 /* 13351 * There is no need to send a separate MSG*MARK 13352 * message since the T_EXDATA_IND will be sent 13353 * now. 13354 */ 13355 flags &= ~TH_SEND_URP_MARK; 13356 freemsg(tcp->tcp_urp_mark_mp); 13357 tcp->tcp_urp_mark_mp = NULL; 13358 } 13359 /* 13360 * Now we are all set. On the next putnext upstream, 13361 * tcp_urp_mp will be non-NULL and will get prepended 13362 * to what has to be this piece containing the urgent 13363 * byte. If for any reason we abort this segment below, 13364 * if it comes back, we will have this ready, or it 13365 * will get blown off in close. 13366 */ 13367 } else if (urp == seg_len) { 13368 /* 13369 * The urgent byte is the next byte after this sequence 13370 * number. If there is data it is marked with 13371 * MSGMARKNEXT and any tcp_urp_mark_mp is discarded 13372 * since it is not needed. Otherwise, if the code 13373 * above just allocated a zero-length tcp_urp_mark_mp 13374 * message, that message is tagged with MSGMARKNEXT. 13375 * Sending up these MSGMARKNEXT messages makes 13376 * SIOCATMARK work correctly even though 13377 * the T_EXDATA_IND will not be sent up until the 13378 * urgent byte arrives. 13379 */ 13380 if (seg_len != 0) { 13381 flags |= TH_MARKNEXT_NEEDED; 13382 freemsg(tcp->tcp_urp_mark_mp); 13383 tcp->tcp_urp_mark_mp = NULL; 13384 flags &= ~TH_SEND_URP_MARK; 13385 } else if (tcp->tcp_urp_mark_mp != NULL) { 13386 flags |= TH_SEND_URP_MARK; 13387 tcp->tcp_urp_mark_mp->b_flag &= 13388 ~MSGNOTMARKNEXT; 13389 tcp->tcp_urp_mark_mp->b_flag |= MSGMARKNEXT; 13390 } 13391 #ifdef DEBUG 13392 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13393 "tcp_rput: AT MARK, len %d, flags 0x%x, %s", 13394 seg_len, flags, 13395 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 13396 #endif /* DEBUG */ 13397 } else { 13398 /* Data left until we hit mark */ 13399 #ifdef DEBUG 13400 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 13401 "tcp_rput: URP %d bytes left, %s", 13402 urp - seg_len, tcp_display(tcp, NULL, 13403 DISP_PORT_ONLY)); 13404 #endif /* DEBUG */ 13405 } 13406 } 13407 13408 process_ack: 13409 if (!(flags & TH_ACK)) { 13410 freemsg(mp); 13411 goto xmit_check; 13412 } 13413 } 13414 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 13415 13416 if (tcp->tcp_ipversion == IPV6_VERSION && bytes_acked > 0) 13417 tcp->tcp_ip_forward_progress = B_TRUE; 13418 if (tcp->tcp_state == TCPS_SYN_RCVD) { 13419 if ((tcp->tcp_conn.tcp_eager_conn_ind != NULL) && 13420 ((tcp->tcp_kssl_ent == NULL) || !tcp->tcp_kssl_pending)) { 13421 /* 3-way handshake complete - pass up the T_CONN_IND */ 13422 tcp_t *listener = tcp->tcp_listener; 13423 mblk_t *mp = tcp->tcp_conn.tcp_eager_conn_ind; 13424 13425 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 13426 /* 13427 * We are here means eager is fine but it can 13428 * get a TH_RST at any point between now and till 13429 * accept completes and disappear. We need to 13430 * ensure that reference to eager is valid after 13431 * we get out of eager's perimeter. So we do 13432 * an extra refhold. 13433 */ 13434 CONN_INC_REF(connp); 13435 13436 /* 13437 * The listener also exists because of the refhold 13438 * done in tcp_conn_request. Its possible that it 13439 * might have closed. We will check that once we 13440 * get inside listeners context. 13441 */ 13442 CONN_INC_REF(listener->tcp_connp); 13443 if (listener->tcp_connp->conn_sqp == 13444 connp->conn_sqp) { 13445 tcp_send_conn_ind(listener->tcp_connp, mp, 13446 listener->tcp_connp->conn_sqp); 13447 CONN_DEC_REF(listener->tcp_connp); 13448 } else if (!tcp->tcp_loopback) { 13449 squeue_fill(listener->tcp_connp->conn_sqp, mp, 13450 tcp_send_conn_ind, 13451 listener->tcp_connp, SQTAG_TCP_CONN_IND); 13452 } else { 13453 squeue_enter(listener->tcp_connp->conn_sqp, mp, 13454 tcp_send_conn_ind, listener->tcp_connp, 13455 SQTAG_TCP_CONN_IND); 13456 } 13457 } 13458 13459 if (tcp->tcp_active_open) { 13460 /* 13461 * We are seeing the final ack in the three way 13462 * hand shake of a active open'ed connection 13463 * so we must send up a T_CONN_CON 13464 */ 13465 if (!tcp_conn_con(tcp, iphdr, tcph, mp, NULL)) { 13466 freemsg(mp); 13467 return; 13468 } 13469 /* 13470 * Don't fuse the loopback endpoints for 13471 * simultaneous active opens. 13472 */ 13473 if (tcp->tcp_loopback) { 13474 TCP_STAT(tcp_fusion_unfusable); 13475 tcp->tcp_unfusable = B_TRUE; 13476 } 13477 } 13478 13479 tcp->tcp_suna = tcp->tcp_iss + 1; /* One for the SYN */ 13480 bytes_acked--; 13481 /* SYN was acked - making progress */ 13482 if (tcp->tcp_ipversion == IPV6_VERSION) 13483 tcp->tcp_ip_forward_progress = B_TRUE; 13484 13485 /* 13486 * If SYN was retransmitted, need to reset all 13487 * retransmission info as this segment will be 13488 * treated as a dup ACK. 13489 */ 13490 if (tcp->tcp_rexmit) { 13491 tcp->tcp_rexmit = B_FALSE; 13492 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13493 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13494 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13495 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13496 tcp->tcp_ms_we_have_waited = 0; 13497 tcp->tcp_cwnd = mss; 13498 } 13499 13500 /* 13501 * We set the send window to zero here. 13502 * This is needed if there is data to be 13503 * processed already on the queue. 13504 * Later (at swnd_update label), the 13505 * "new_swnd > tcp_swnd" condition is satisfied 13506 * the XMIT_NEEDED flag is set in the current 13507 * (SYN_RCVD) state. This ensures tcp_wput_data() is 13508 * called if there is already data on queue in 13509 * this state. 13510 */ 13511 tcp->tcp_swnd = 0; 13512 13513 if (new_swnd > tcp->tcp_max_swnd) 13514 tcp->tcp_max_swnd = new_swnd; 13515 tcp->tcp_swl1 = seg_seq; 13516 tcp->tcp_swl2 = seg_ack; 13517 tcp->tcp_state = TCPS_ESTABLISHED; 13518 tcp->tcp_valid_bits &= ~TCP_ISS_VALID; 13519 13520 /* Fuse when both sides are in ESTABLISHED state */ 13521 if (tcp->tcp_loopback && do_tcp_fusion) 13522 tcp_fuse(tcp, iphdr, tcph); 13523 13524 } 13525 /* This code follows 4.4BSD-Lite2 mostly. */ 13526 if (bytes_acked < 0) 13527 goto est; 13528 13529 /* 13530 * If TCP is ECN capable and the congestion experience bit is 13531 * set, reduce tcp_cwnd and tcp_ssthresh. But this should only be 13532 * done once per window (or more loosely, per RTT). 13533 */ 13534 if (tcp->tcp_cwr && SEQ_GT(seg_ack, tcp->tcp_cwr_snd_max)) 13535 tcp->tcp_cwr = B_FALSE; 13536 if (tcp->tcp_ecn_ok && (flags & TH_ECE)) { 13537 if (!tcp->tcp_cwr) { 13538 npkt = ((tcp->tcp_snxt - tcp->tcp_suna) >> 1) / mss; 13539 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * mss; 13540 tcp->tcp_cwnd = npkt * mss; 13541 /* 13542 * If the cwnd is 0, use the timer to clock out 13543 * new segments. This is required by the ECN spec. 13544 */ 13545 if (npkt == 0) { 13546 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13547 /* 13548 * This makes sure that when the ACK comes 13549 * back, we will increase tcp_cwnd by 1 MSS. 13550 */ 13551 tcp->tcp_cwnd_cnt = 0; 13552 } 13553 tcp->tcp_cwr = B_TRUE; 13554 /* 13555 * This marks the end of the current window of in 13556 * flight data. That is why we don't use 13557 * tcp_suna + tcp_swnd. Only data in flight can 13558 * provide ECN info. 13559 */ 13560 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13561 tcp->tcp_ecn_cwr_sent = B_FALSE; 13562 } 13563 } 13564 13565 mp1 = tcp->tcp_xmit_head; 13566 if (bytes_acked == 0) { 13567 if (!ofo_seg && seg_len == 0 && new_swnd == tcp->tcp_swnd) { 13568 int dupack_cnt; 13569 13570 BUMP_MIB(&tcp_mib, tcpInDupAck); 13571 /* 13572 * Fast retransmit. When we have seen exactly three 13573 * identical ACKs while we have unacked data 13574 * outstanding we take it as a hint that our peer 13575 * dropped something. 13576 * 13577 * If TCP is retransmitting, don't do fast retransmit. 13578 */ 13579 if (mp1 && tcp->tcp_suna != tcp->tcp_snxt && 13580 ! tcp->tcp_rexmit) { 13581 /* Do Limited Transmit */ 13582 if ((dupack_cnt = ++tcp->tcp_dupack_cnt) < 13583 tcp_dupack_fast_retransmit) { 13584 /* 13585 * RFC 3042 13586 * 13587 * What we need to do is temporarily 13588 * increase tcp_cwnd so that new 13589 * data can be sent if it is allowed 13590 * by the receive window (tcp_rwnd). 13591 * tcp_wput_data() will take care of 13592 * the rest. 13593 * 13594 * If the connection is SACK capable, 13595 * only do limited xmit when there 13596 * is SACK info. 13597 * 13598 * Note how tcp_cwnd is incremented. 13599 * The first dup ACK will increase 13600 * it by 1 MSS. The second dup ACK 13601 * will increase it by 2 MSS. This 13602 * means that only 1 new segment will 13603 * be sent for each dup ACK. 13604 */ 13605 if (tcp->tcp_unsent > 0 && 13606 (!tcp->tcp_snd_sack_ok || 13607 (tcp->tcp_snd_sack_ok && 13608 tcp->tcp_notsack_list != NULL))) { 13609 tcp->tcp_cwnd += mss << 13610 (tcp->tcp_dupack_cnt - 1); 13611 flags |= TH_LIMIT_XMIT; 13612 } 13613 } else if (dupack_cnt == 13614 tcp_dupack_fast_retransmit) { 13615 13616 /* 13617 * If we have reduced tcp_ssthresh 13618 * because of ECN, do not reduce it again 13619 * unless it is already one window of data 13620 * away. After one window of data, tcp_cwr 13621 * should then be cleared. Note that 13622 * for non ECN capable connection, tcp_cwr 13623 * should always be false. 13624 * 13625 * Adjust cwnd since the duplicate 13626 * ack indicates that a packet was 13627 * dropped (due to congestion.) 13628 */ 13629 if (!tcp->tcp_cwr) { 13630 npkt = ((tcp->tcp_snxt - 13631 tcp->tcp_suna) >> 1) / mss; 13632 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 13633 mss; 13634 tcp->tcp_cwnd = (npkt + 13635 tcp->tcp_dupack_cnt) * mss; 13636 } 13637 if (tcp->tcp_ecn_ok) { 13638 tcp->tcp_cwr = B_TRUE; 13639 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 13640 tcp->tcp_ecn_cwr_sent = B_FALSE; 13641 } 13642 13643 /* 13644 * We do Hoe's algorithm. Refer to her 13645 * paper "Improving the Start-up Behavior 13646 * of a Congestion Control Scheme for TCP," 13647 * appeared in SIGCOMM'96. 13648 * 13649 * Save highest seq no we have sent so far. 13650 * Be careful about the invisible FIN byte. 13651 */ 13652 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 13653 (tcp->tcp_unsent == 0)) { 13654 tcp->tcp_rexmit_max = tcp->tcp_fss; 13655 } else { 13656 tcp->tcp_rexmit_max = tcp->tcp_snxt; 13657 } 13658 13659 /* 13660 * Do not allow bursty traffic during. 13661 * fast recovery. Refer to Fall and Floyd's 13662 * paper "Simulation-based Comparisons of 13663 * Tahoe, Reno and SACK TCP" (in CCR?) 13664 * This is a best current practise. 13665 */ 13666 tcp->tcp_snd_burst = TCP_CWND_SS; 13667 13668 /* 13669 * For SACK: 13670 * Calculate tcp_pipe, which is the 13671 * estimated number of bytes in 13672 * network. 13673 * 13674 * tcp_fack is the highest sack'ed seq num 13675 * TCP has received. 13676 * 13677 * tcp_pipe is explained in the above quoted 13678 * Fall and Floyd's paper. tcp_fack is 13679 * explained in Mathis and Mahdavi's 13680 * "Forward Acknowledgment: Refining TCP 13681 * Congestion Control" in SIGCOMM '96. 13682 */ 13683 if (tcp->tcp_snd_sack_ok) { 13684 ASSERT(tcp->tcp_sack_info != NULL); 13685 if (tcp->tcp_notsack_list != NULL) { 13686 tcp->tcp_pipe = tcp->tcp_snxt - 13687 tcp->tcp_fack; 13688 tcp->tcp_sack_snxt = seg_ack; 13689 flags |= TH_NEED_SACK_REXMIT; 13690 } else { 13691 /* 13692 * Always initialize tcp_pipe 13693 * even though we don't have 13694 * any SACK info. If later 13695 * we get SACK info and 13696 * tcp_pipe is not initialized, 13697 * funny things will happen. 13698 */ 13699 tcp->tcp_pipe = 13700 tcp->tcp_cwnd_ssthresh; 13701 } 13702 } else { 13703 flags |= TH_REXMIT_NEEDED; 13704 } /* tcp_snd_sack_ok */ 13705 13706 } else { 13707 /* 13708 * Here we perform congestion 13709 * avoidance, but NOT slow start. 13710 * This is known as the Fast 13711 * Recovery Algorithm. 13712 */ 13713 if (tcp->tcp_snd_sack_ok && 13714 tcp->tcp_notsack_list != NULL) { 13715 flags |= TH_NEED_SACK_REXMIT; 13716 tcp->tcp_pipe -= mss; 13717 if (tcp->tcp_pipe < 0) 13718 tcp->tcp_pipe = 0; 13719 } else { 13720 /* 13721 * We know that one more packet has 13722 * left the pipe thus we can update 13723 * cwnd. 13724 */ 13725 cwnd = tcp->tcp_cwnd + mss; 13726 if (cwnd > tcp->tcp_cwnd_max) 13727 cwnd = tcp->tcp_cwnd_max; 13728 tcp->tcp_cwnd = cwnd; 13729 if (tcp->tcp_unsent > 0) 13730 flags |= TH_XMIT_NEEDED; 13731 } 13732 } 13733 } 13734 } else if (tcp->tcp_zero_win_probe) { 13735 /* 13736 * If the window has opened, need to arrange 13737 * to send additional data. 13738 */ 13739 if (new_swnd != 0) { 13740 /* tcp_suna != tcp_snxt */ 13741 /* Packet contains a window update */ 13742 BUMP_MIB(&tcp_mib, tcpInWinUpdate); 13743 tcp->tcp_zero_win_probe = 0; 13744 tcp->tcp_timer_backoff = 0; 13745 tcp->tcp_ms_we_have_waited = 0; 13746 13747 /* 13748 * Transmit starting with tcp_suna since 13749 * the one byte probe is not ack'ed. 13750 * If TCP has sent more than one identical 13751 * probe, tcp_rexmit will be set. That means 13752 * tcp_ss_rexmit() will send out the one 13753 * byte along with new data. Otherwise, 13754 * fake the retransmission. 13755 */ 13756 flags |= TH_XMIT_NEEDED; 13757 if (!tcp->tcp_rexmit) { 13758 tcp->tcp_rexmit = B_TRUE; 13759 tcp->tcp_dupack_cnt = 0; 13760 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 13761 tcp->tcp_rexmit_max = tcp->tcp_suna + 1; 13762 } 13763 } 13764 } 13765 goto swnd_update; 13766 } 13767 13768 /* 13769 * Check for "acceptability" of ACK value per RFC 793, pages 72 - 73. 13770 * If the ACK value acks something that we have not yet sent, it might 13771 * be an old duplicate segment. Send an ACK to re-synchronize the 13772 * other side. 13773 * Note: reset in response to unacceptable ACK in SYN_RECEIVE 13774 * state is handled above, so we can always just drop the segment and 13775 * send an ACK here. 13776 * 13777 * Should we send ACKs in response to ACK only segments? 13778 */ 13779 if (SEQ_GT(seg_ack, tcp->tcp_snxt)) { 13780 BUMP_MIB(&tcp_mib, tcpInAckUnsent); 13781 /* drop the received segment */ 13782 freemsg(mp); 13783 13784 /* 13785 * Send back an ACK. If tcp_drop_ack_unsent_cnt is 13786 * greater than 0, check if the number of such 13787 * bogus ACks is greater than that count. If yes, 13788 * don't send back any ACK. This prevents TCP from 13789 * getting into an ACK storm if somehow an attacker 13790 * successfully spoofs an acceptable segment to our 13791 * peer. 13792 */ 13793 if (tcp_drop_ack_unsent_cnt > 0 && 13794 ++tcp->tcp_in_ack_unsent > tcp_drop_ack_unsent_cnt) { 13795 TCP_STAT(tcp_in_ack_unsent_drop); 13796 return; 13797 } 13798 mp = tcp_ack_mp(tcp); 13799 if (mp != NULL) { 13800 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 13801 BUMP_LOCAL(tcp->tcp_obsegs); 13802 BUMP_MIB(&tcp_mib, tcpOutAck); 13803 tcp_send_data(tcp, tcp->tcp_wq, mp); 13804 } 13805 return; 13806 } 13807 13808 /* 13809 * TCP gets a new ACK, update the notsack'ed list to delete those 13810 * blocks that are covered by this ACK. 13811 */ 13812 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 13813 tcp_notsack_remove(&(tcp->tcp_notsack_list), seg_ack, 13814 &(tcp->tcp_num_notsack_blk), &(tcp->tcp_cnt_notsack_list)); 13815 } 13816 13817 /* 13818 * If we got an ACK after fast retransmit, check to see 13819 * if it is a partial ACK. If it is not and the congestion 13820 * window was inflated to account for the other side's 13821 * cached packets, retract it. If it is, do Hoe's algorithm. 13822 */ 13823 if (tcp->tcp_dupack_cnt >= tcp_dupack_fast_retransmit) { 13824 ASSERT(tcp->tcp_rexmit == B_FALSE); 13825 if (SEQ_GEQ(seg_ack, tcp->tcp_rexmit_max)) { 13826 tcp->tcp_dupack_cnt = 0; 13827 /* 13828 * Restore the orig tcp_cwnd_ssthresh after 13829 * fast retransmit phase. 13830 */ 13831 if (tcp->tcp_cwnd > tcp->tcp_cwnd_ssthresh) { 13832 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh; 13833 } 13834 tcp->tcp_rexmit_max = seg_ack; 13835 tcp->tcp_cwnd_cnt = 0; 13836 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13837 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13838 13839 /* 13840 * Remove all notsack info to avoid confusion with 13841 * the next fast retrasnmit/recovery phase. 13842 */ 13843 if (tcp->tcp_snd_sack_ok && 13844 tcp->tcp_notsack_list != NULL) { 13845 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 13846 } 13847 } else { 13848 if (tcp->tcp_snd_sack_ok && 13849 tcp->tcp_notsack_list != NULL) { 13850 flags |= TH_NEED_SACK_REXMIT; 13851 tcp->tcp_pipe -= mss; 13852 if (tcp->tcp_pipe < 0) 13853 tcp->tcp_pipe = 0; 13854 } else { 13855 /* 13856 * Hoe's algorithm: 13857 * 13858 * Retransmit the unack'ed segment and 13859 * restart fast recovery. Note that we 13860 * need to scale back tcp_cwnd to the 13861 * original value when we started fast 13862 * recovery. This is to prevent overly 13863 * aggressive behaviour in sending new 13864 * segments. 13865 */ 13866 tcp->tcp_cwnd = tcp->tcp_cwnd_ssthresh + 13867 tcp_dupack_fast_retransmit * mss; 13868 tcp->tcp_cwnd_cnt = tcp->tcp_cwnd; 13869 flags |= TH_REXMIT_NEEDED; 13870 } 13871 } 13872 } else { 13873 tcp->tcp_dupack_cnt = 0; 13874 if (tcp->tcp_rexmit) { 13875 /* 13876 * TCP is retranmitting. If the ACK ack's all 13877 * outstanding data, update tcp_rexmit_max and 13878 * tcp_rexmit_nxt. Otherwise, update tcp_rexmit_nxt 13879 * to the correct value. 13880 * 13881 * Note that SEQ_LEQ() is used. This is to avoid 13882 * unnecessary fast retransmit caused by dup ACKs 13883 * received when TCP does slow start retransmission 13884 * after a time out. During this phase, TCP may 13885 * send out segments which are already received. 13886 * This causes dup ACKs to be sent back. 13887 */ 13888 if (SEQ_LEQ(seg_ack, tcp->tcp_rexmit_max)) { 13889 if (SEQ_GT(seg_ack, tcp->tcp_rexmit_nxt)) { 13890 tcp->tcp_rexmit_nxt = seg_ack; 13891 } 13892 if (seg_ack != tcp->tcp_rexmit_max) { 13893 flags |= TH_XMIT_NEEDED; 13894 } 13895 } else { 13896 tcp->tcp_rexmit = B_FALSE; 13897 tcp->tcp_xmit_zc_clean = B_FALSE; 13898 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 13899 tcp->tcp_snd_burst = tcp->tcp_localnet ? 13900 TCP_CWND_INFINITE : TCP_CWND_NORMAL; 13901 } 13902 tcp->tcp_ms_we_have_waited = 0; 13903 } 13904 } 13905 13906 BUMP_MIB(&tcp_mib, tcpInAckSegs); 13907 UPDATE_MIB(&tcp_mib, tcpInAckBytes, bytes_acked); 13908 tcp->tcp_suna = seg_ack; 13909 if (tcp->tcp_zero_win_probe != 0) { 13910 tcp->tcp_zero_win_probe = 0; 13911 tcp->tcp_timer_backoff = 0; 13912 } 13913 13914 /* 13915 * If tcp_xmit_head is NULL, then it must be the FIN being ack'ed. 13916 * Note that it cannot be the SYN being ack'ed. The code flow 13917 * will not reach here. 13918 */ 13919 if (mp1 == NULL) { 13920 goto fin_acked; 13921 } 13922 13923 /* 13924 * Update the congestion window. 13925 * 13926 * If TCP is not ECN capable or TCP is ECN capable but the 13927 * congestion experience bit is not set, increase the tcp_cwnd as 13928 * usual. 13929 */ 13930 if (!tcp->tcp_ecn_ok || !(flags & TH_ECE)) { 13931 cwnd = tcp->tcp_cwnd; 13932 add = mss; 13933 13934 if (cwnd >= tcp->tcp_cwnd_ssthresh) { 13935 /* 13936 * This is to prevent an increase of less than 1 MSS of 13937 * tcp_cwnd. With partial increase, tcp_wput_data() 13938 * may send out tinygrams in order to preserve mblk 13939 * boundaries. 13940 * 13941 * By initializing tcp_cwnd_cnt to new tcp_cwnd and 13942 * decrementing it by 1 MSS for every ACKs, tcp_cwnd is 13943 * increased by 1 MSS for every RTTs. 13944 */ 13945 if (tcp->tcp_cwnd_cnt <= 0) { 13946 tcp->tcp_cwnd_cnt = cwnd + add; 13947 } else { 13948 tcp->tcp_cwnd_cnt -= add; 13949 add = 0; 13950 } 13951 } 13952 tcp->tcp_cwnd = MIN(cwnd + add, tcp->tcp_cwnd_max); 13953 } 13954 13955 /* See if the latest urgent data has been acknowledged */ 13956 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && 13957 SEQ_GT(seg_ack, tcp->tcp_urg)) 13958 tcp->tcp_valid_bits &= ~TCP_URG_VALID; 13959 13960 /* Can we update the RTT estimates? */ 13961 if (tcp->tcp_snd_ts_ok) { 13962 /* Ignore zero timestamp echo-reply. */ 13963 if (tcpopt.tcp_opt_ts_ecr != 0) { 13964 tcp_set_rto(tcp, (int32_t)lbolt - 13965 (int32_t)tcpopt.tcp_opt_ts_ecr); 13966 } 13967 13968 /* If needed, restart the timer. */ 13969 if (tcp->tcp_set_timer == 1) { 13970 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13971 tcp->tcp_set_timer = 0; 13972 } 13973 /* 13974 * Update tcp_csuna in case the other side stops sending 13975 * us timestamps. 13976 */ 13977 tcp->tcp_csuna = tcp->tcp_snxt; 13978 } else if (SEQ_GT(seg_ack, tcp->tcp_csuna)) { 13979 /* 13980 * An ACK sequence we haven't seen before, so get the RTT 13981 * and update the RTO. But first check if the timestamp is 13982 * valid to use. 13983 */ 13984 if ((mp1->b_next != NULL) && 13985 SEQ_GT(seg_ack, (uint32_t)(uintptr_t)(mp1->b_next))) 13986 tcp_set_rto(tcp, (int32_t)lbolt - 13987 (int32_t)(intptr_t)mp1->b_prev); 13988 else 13989 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 13990 13991 /* Remeber the last sequence to be ACKed */ 13992 tcp->tcp_csuna = seg_ack; 13993 if (tcp->tcp_set_timer == 1) { 13994 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 13995 tcp->tcp_set_timer = 0; 13996 } 13997 } else { 13998 BUMP_MIB(&tcp_mib, tcpRttNoUpdate); 13999 } 14000 14001 /* Eat acknowledged bytes off the xmit queue. */ 14002 for (;;) { 14003 mblk_t *mp2; 14004 uchar_t *wptr; 14005 14006 wptr = mp1->b_wptr; 14007 ASSERT((uintptr_t)(wptr - mp1->b_rptr) <= (uintptr_t)INT_MAX); 14008 bytes_acked -= (int)(wptr - mp1->b_rptr); 14009 if (bytes_acked < 0) { 14010 mp1->b_rptr = wptr + bytes_acked; 14011 /* 14012 * Set a new timestamp if all the bytes timed by the 14013 * old timestamp have been ack'ed. 14014 */ 14015 if (SEQ_GT(seg_ack, 14016 (uint32_t)(uintptr_t)(mp1->b_next))) { 14017 mp1->b_prev = (mblk_t *)(uintptr_t)lbolt; 14018 mp1->b_next = NULL; 14019 } 14020 break; 14021 } 14022 mp1->b_next = NULL; 14023 mp1->b_prev = NULL; 14024 mp2 = mp1; 14025 mp1 = mp1->b_cont; 14026 14027 /* 14028 * This notification is required for some zero-copy 14029 * clients to maintain a copy semantic. After the data 14030 * is ack'ed, client is safe to modify or reuse the buffer. 14031 */ 14032 if (tcp->tcp_snd_zcopy_aware && 14033 (mp2->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 14034 tcp_zcopy_notify(tcp); 14035 freeb(mp2); 14036 if (bytes_acked == 0) { 14037 if (mp1 == NULL) { 14038 /* Everything is ack'ed, clear the tail. */ 14039 tcp->tcp_xmit_tail = NULL; 14040 /* 14041 * Cancel the timer unless we are still 14042 * waiting for an ACK for the FIN packet. 14043 */ 14044 if (tcp->tcp_timer_tid != 0 && 14045 tcp->tcp_snxt == tcp->tcp_suna) { 14046 (void) TCP_TIMER_CANCEL(tcp, 14047 tcp->tcp_timer_tid); 14048 tcp->tcp_timer_tid = 0; 14049 } 14050 goto pre_swnd_update; 14051 } 14052 if (mp2 != tcp->tcp_xmit_tail) 14053 break; 14054 tcp->tcp_xmit_tail = mp1; 14055 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 14056 (uintptr_t)INT_MAX); 14057 tcp->tcp_xmit_tail_unsent = (int)(mp1->b_wptr - 14058 mp1->b_rptr); 14059 break; 14060 } 14061 if (mp1 == NULL) { 14062 /* 14063 * More was acked but there is nothing more 14064 * outstanding. This means that the FIN was 14065 * just acked or that we're talking to a clown. 14066 */ 14067 fin_acked: 14068 ASSERT(tcp->tcp_fin_sent); 14069 tcp->tcp_xmit_tail = NULL; 14070 if (tcp->tcp_fin_sent) { 14071 /* FIN was acked - making progress */ 14072 if (tcp->tcp_ipversion == IPV6_VERSION && 14073 !tcp->tcp_fin_acked) 14074 tcp->tcp_ip_forward_progress = B_TRUE; 14075 tcp->tcp_fin_acked = B_TRUE; 14076 if (tcp->tcp_linger_tid != 0 && 14077 TCP_TIMER_CANCEL(tcp, 14078 tcp->tcp_linger_tid) >= 0) { 14079 tcp_stop_lingering(tcp); 14080 } 14081 } else { 14082 /* 14083 * We should never get here because 14084 * we have already checked that the 14085 * number of bytes ack'ed should be 14086 * smaller than or equal to what we 14087 * have sent so far (it is the 14088 * acceptability check of the ACK). 14089 * We can only get here if the send 14090 * queue is corrupted. 14091 * 14092 * Terminate the connection and 14093 * panic the system. It is better 14094 * for us to panic instead of 14095 * continuing to avoid other disaster. 14096 */ 14097 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 14098 tcp->tcp_rnxt, TH_RST|TH_ACK); 14099 panic("Memory corruption " 14100 "detected for connection %s.", 14101 tcp_display(tcp, NULL, 14102 DISP_ADDR_AND_PORT)); 14103 /*NOTREACHED*/ 14104 } 14105 goto pre_swnd_update; 14106 } 14107 ASSERT(mp2 != tcp->tcp_xmit_tail); 14108 } 14109 if (tcp->tcp_unsent) { 14110 flags |= TH_XMIT_NEEDED; 14111 } 14112 pre_swnd_update: 14113 tcp->tcp_xmit_head = mp1; 14114 swnd_update: 14115 /* 14116 * The following check is different from most other implementations. 14117 * For bi-directional transfer, when segments are dropped, the 14118 * "normal" check will not accept a window update in those 14119 * retransmitted segemnts. Failing to do that, TCP may send out 14120 * segments which are outside receiver's window. As TCP accepts 14121 * the ack in those retransmitted segments, if the window update in 14122 * the same segment is not accepted, TCP will incorrectly calculates 14123 * that it can send more segments. This can create a deadlock 14124 * with the receiver if its window becomes zero. 14125 */ 14126 if (SEQ_LT(tcp->tcp_swl2, seg_ack) || 14127 SEQ_LT(tcp->tcp_swl1, seg_seq) || 14128 (tcp->tcp_swl1 == seg_seq && new_swnd > tcp->tcp_swnd)) { 14129 /* 14130 * The criteria for update is: 14131 * 14132 * 1. the segment acknowledges some data. Or 14133 * 2. the segment is new, i.e. it has a higher seq num. Or 14134 * 3. the segment is not old and the advertised window is 14135 * larger than the previous advertised window. 14136 */ 14137 if (tcp->tcp_unsent && new_swnd > tcp->tcp_swnd) 14138 flags |= TH_XMIT_NEEDED; 14139 tcp->tcp_swnd = new_swnd; 14140 if (new_swnd > tcp->tcp_max_swnd) 14141 tcp->tcp_max_swnd = new_swnd; 14142 tcp->tcp_swl1 = seg_seq; 14143 tcp->tcp_swl2 = seg_ack; 14144 } 14145 est: 14146 if (tcp->tcp_state > TCPS_ESTABLISHED) { 14147 14148 switch (tcp->tcp_state) { 14149 case TCPS_FIN_WAIT_1: 14150 if (tcp->tcp_fin_acked) { 14151 tcp->tcp_state = TCPS_FIN_WAIT_2; 14152 /* 14153 * We implement the non-standard BSD/SunOS 14154 * FIN_WAIT_2 flushing algorithm. 14155 * If there is no user attached to this 14156 * TCP endpoint, then this TCP struct 14157 * could hang around forever in FIN_WAIT_2 14158 * state if the peer forgets to send us 14159 * a FIN. To prevent this, we wait only 14160 * 2*MSL (a convenient time value) for 14161 * the FIN to arrive. If it doesn't show up, 14162 * we flush the TCP endpoint. This algorithm, 14163 * though a violation of RFC-793, has worked 14164 * for over 10 years in BSD systems. 14165 * Note: SunOS 4.x waits 675 seconds before 14166 * flushing the FIN_WAIT_2 connection. 14167 */ 14168 TCP_TIMER_RESTART(tcp, 14169 tcp_fin_wait_2_flush_interval); 14170 } 14171 break; 14172 case TCPS_FIN_WAIT_2: 14173 break; /* Shutdown hook? */ 14174 case TCPS_LAST_ACK: 14175 freemsg(mp); 14176 if (tcp->tcp_fin_acked) { 14177 (void) tcp_clean_death(tcp, 0, 19); 14178 return; 14179 } 14180 goto xmit_check; 14181 case TCPS_CLOSING: 14182 if (tcp->tcp_fin_acked) { 14183 tcp->tcp_state = TCPS_TIME_WAIT; 14184 if (!TCP_IS_DETACHED(tcp)) { 14185 TCP_TIMER_RESTART(tcp, 14186 tcp_time_wait_interval); 14187 } else { 14188 tcp_time_wait_append(tcp); 14189 TCP_DBGSTAT(tcp_rput_time_wait); 14190 } 14191 } 14192 /*FALLTHRU*/ 14193 case TCPS_CLOSE_WAIT: 14194 freemsg(mp); 14195 goto xmit_check; 14196 default: 14197 ASSERT(tcp->tcp_state != TCPS_TIME_WAIT); 14198 break; 14199 } 14200 } 14201 if (flags & TH_FIN) { 14202 /* Make sure we ack the fin */ 14203 flags |= TH_ACK_NEEDED; 14204 if (!tcp->tcp_fin_rcvd) { 14205 tcp->tcp_fin_rcvd = B_TRUE; 14206 tcp->tcp_rnxt++; 14207 tcph = tcp->tcp_tcph; 14208 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14209 14210 /* 14211 * Generate the ordrel_ind at the end unless we 14212 * are an eager guy. 14213 * In the eager case tcp_rsrv will do this when run 14214 * after tcp_accept is done. 14215 */ 14216 if (tcp->tcp_listener == NULL && 14217 !TCP_IS_DETACHED(tcp) && (!tcp->tcp_hard_binding)) 14218 flags |= TH_ORDREL_NEEDED; 14219 switch (tcp->tcp_state) { 14220 case TCPS_SYN_RCVD: 14221 case TCPS_ESTABLISHED: 14222 tcp->tcp_state = TCPS_CLOSE_WAIT; 14223 /* Keepalive? */ 14224 break; 14225 case TCPS_FIN_WAIT_1: 14226 if (!tcp->tcp_fin_acked) { 14227 tcp->tcp_state = TCPS_CLOSING; 14228 break; 14229 } 14230 /* FALLTHRU */ 14231 case TCPS_FIN_WAIT_2: 14232 tcp->tcp_state = TCPS_TIME_WAIT; 14233 if (!TCP_IS_DETACHED(tcp)) { 14234 TCP_TIMER_RESTART(tcp, 14235 tcp_time_wait_interval); 14236 } else { 14237 tcp_time_wait_append(tcp); 14238 TCP_DBGSTAT(tcp_rput_time_wait); 14239 } 14240 if (seg_len) { 14241 /* 14242 * implies data piggybacked on FIN. 14243 * break to handle data. 14244 */ 14245 break; 14246 } 14247 freemsg(mp); 14248 goto ack_check; 14249 } 14250 } 14251 } 14252 if (mp == NULL) 14253 goto xmit_check; 14254 if (seg_len == 0) { 14255 freemsg(mp); 14256 goto xmit_check; 14257 } 14258 if (mp->b_rptr == mp->b_wptr) { 14259 /* 14260 * The header has been consumed, so we remove the 14261 * zero-length mblk here. 14262 */ 14263 mp1 = mp; 14264 mp = mp->b_cont; 14265 freeb(mp1); 14266 } 14267 tcph = tcp->tcp_tcph; 14268 tcp->tcp_rack_cnt++; 14269 { 14270 uint32_t cur_max; 14271 14272 cur_max = tcp->tcp_rack_cur_max; 14273 if (tcp->tcp_rack_cnt >= cur_max) { 14274 /* 14275 * We have more unacked data than we should - send 14276 * an ACK now. 14277 */ 14278 flags |= TH_ACK_NEEDED; 14279 cur_max++; 14280 if (cur_max > tcp->tcp_rack_abs_max) 14281 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 14282 else 14283 tcp->tcp_rack_cur_max = cur_max; 14284 } else if (TCP_IS_DETACHED(tcp)) { 14285 /* We don't have an ACK timer for detached TCP. */ 14286 flags |= TH_ACK_NEEDED; 14287 } else if (seg_len < mss) { 14288 /* 14289 * If we get a segment that is less than an mss, and we 14290 * already have unacknowledged data, and the amount 14291 * unacknowledged is not a multiple of mss, then we 14292 * better generate an ACK now. Otherwise, this may be 14293 * the tail piece of a transaction, and we would rather 14294 * wait for the response. 14295 */ 14296 uint32_t udif; 14297 ASSERT((uintptr_t)(tcp->tcp_rnxt - tcp->tcp_rack) <= 14298 (uintptr_t)INT_MAX); 14299 udif = (int)(tcp->tcp_rnxt - tcp->tcp_rack); 14300 if (udif && (udif % mss)) 14301 flags |= TH_ACK_NEEDED; 14302 else 14303 flags |= TH_ACK_TIMER_NEEDED; 14304 } else { 14305 /* Start delayed ack timer */ 14306 flags |= TH_ACK_TIMER_NEEDED; 14307 } 14308 } 14309 tcp->tcp_rnxt += seg_len; 14310 U32_TO_ABE32(tcp->tcp_rnxt, tcph->th_ack); 14311 14312 /* Update SACK list */ 14313 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 14314 tcp_sack_remove(tcp->tcp_sack_list, tcp->tcp_rnxt, 14315 &(tcp->tcp_num_sack_blk)); 14316 } 14317 14318 if (tcp->tcp_urp_mp) { 14319 tcp->tcp_urp_mp->b_cont = mp; 14320 mp = tcp->tcp_urp_mp; 14321 tcp->tcp_urp_mp = NULL; 14322 /* Ready for a new signal. */ 14323 tcp->tcp_urp_last_valid = B_FALSE; 14324 #ifdef DEBUG 14325 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14326 "tcp_rput: sending exdata_ind %s", 14327 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14328 #endif /* DEBUG */ 14329 } 14330 14331 /* 14332 * Check for ancillary data changes compared to last segment. 14333 */ 14334 if (tcp->tcp_ipv6_recvancillary != 0) { 14335 mp = tcp_rput_add_ancillary(tcp, mp, &ipp); 14336 if (mp == NULL) 14337 return; 14338 } 14339 14340 if (tcp->tcp_listener || tcp->tcp_hard_binding) { 14341 /* 14342 * Side queue inbound data until the accept happens. 14343 * tcp_accept/tcp_rput drains this when the accept happens. 14344 * M_DATA is queued on b_cont. Otherwise (T_OPTDATA_IND or 14345 * T_EXDATA_IND) it is queued on b_next. 14346 * XXX Make urgent data use this. Requires: 14347 * Removing tcp_listener check for TH_URG 14348 * Making M_PCPROTO and MARK messages skip the eager case 14349 */ 14350 14351 if (tcp->tcp_kssl_pending) { 14352 tcp_kssl_input(tcp, mp); 14353 } else { 14354 tcp_rcv_enqueue(tcp, mp, seg_len); 14355 } 14356 } else { 14357 if (mp->b_datap->db_type != M_DATA || 14358 (flags & TH_MARKNEXT_NEEDED)) { 14359 if (tcp->tcp_rcv_list != NULL) { 14360 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14361 } 14362 ASSERT(tcp->tcp_rcv_list == NULL || 14363 tcp->tcp_fused_sigurg); 14364 if (flags & TH_MARKNEXT_NEEDED) { 14365 #ifdef DEBUG 14366 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14367 "tcp_rput: sending MSGMARKNEXT %s", 14368 tcp_display(tcp, NULL, 14369 DISP_PORT_ONLY)); 14370 #endif /* DEBUG */ 14371 mp->b_flag |= MSGMARKNEXT; 14372 flags &= ~TH_MARKNEXT_NEEDED; 14373 } 14374 14375 /* Does this need SSL processing first? */ 14376 if ((tcp->tcp_kssl_ctx != NULL) && 14377 (DB_TYPE(mp) == M_DATA)) { 14378 tcp_kssl_input(tcp, mp); 14379 } else { 14380 putnext(tcp->tcp_rq, mp); 14381 if (!canputnext(tcp->tcp_rq)) 14382 tcp->tcp_rwnd -= seg_len; 14383 } 14384 } else if (((flags & (TH_PUSH|TH_FIN)) || 14385 tcp->tcp_rcv_cnt + seg_len >= tcp->tcp_rq->q_hiwat >> 3) && 14386 (sqp != NULL)) { 14387 if (tcp->tcp_rcv_list != NULL) { 14388 /* 14389 * Enqueue the new segment first and then 14390 * call tcp_rcv_drain() to send all data 14391 * up. The other way to do this is to 14392 * send all queued data up and then call 14393 * putnext() to send the new segment up. 14394 * This way can remove the else part later 14395 * on. 14396 * 14397 * We don't this to avoid one more call to 14398 * canputnext() as tcp_rcv_drain() needs to 14399 * call canputnext(). 14400 */ 14401 tcp_rcv_enqueue(tcp, mp, seg_len); 14402 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14403 } else { 14404 /* Does this need SSL processing first? */ 14405 if ((tcp->tcp_kssl_ctx != NULL) && 14406 (DB_TYPE(mp) == M_DATA)) { 14407 tcp_kssl_input(tcp, mp); 14408 } else { 14409 putnext(tcp->tcp_rq, mp); 14410 if (!canputnext(tcp->tcp_rq)) 14411 tcp->tcp_rwnd -= seg_len; 14412 } 14413 } 14414 } else { 14415 /* 14416 * Enqueue all packets when processing an mblk 14417 * from the co queue and also enqueue normal packets. 14418 */ 14419 tcp_rcv_enqueue(tcp, mp, seg_len); 14420 } 14421 /* 14422 * Make sure the timer is running if we have data waiting 14423 * for a push bit. This provides resiliency against 14424 * implementations that do not correctly generate push bits. 14425 */ 14426 if ((sqp != NULL) && tcp->tcp_rcv_list != NULL && 14427 tcp->tcp_push_tid == 0) { 14428 /* 14429 * The connection may be closed at this point, so don't 14430 * do anything for a detached tcp. 14431 */ 14432 if (!TCP_IS_DETACHED(tcp)) 14433 tcp->tcp_push_tid = TCP_TIMER(tcp, 14434 tcp_push_timer, 14435 MSEC_TO_TICK(tcp_push_timer_interval)); 14436 } 14437 } 14438 xmit_check: 14439 /* Is there anything left to do? */ 14440 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14441 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_ACK_NEEDED| 14442 TH_NEED_SACK_REXMIT|TH_LIMIT_XMIT|TH_ACK_TIMER_NEEDED| 14443 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14444 goto done; 14445 14446 /* Any transmit work to do and a non-zero window? */ 14447 if ((flags & (TH_REXMIT_NEEDED|TH_XMIT_NEEDED|TH_NEED_SACK_REXMIT| 14448 TH_LIMIT_XMIT)) && tcp->tcp_swnd != 0) { 14449 if (flags & TH_REXMIT_NEEDED) { 14450 uint32_t snd_size = tcp->tcp_snxt - tcp->tcp_suna; 14451 14452 BUMP_MIB(&tcp_mib, tcpOutFastRetrans); 14453 if (snd_size > mss) 14454 snd_size = mss; 14455 if (snd_size > tcp->tcp_swnd) 14456 snd_size = tcp->tcp_swnd; 14457 mp1 = tcp_xmit_mp(tcp, tcp->tcp_xmit_head, snd_size, 14458 NULL, NULL, tcp->tcp_suna, B_TRUE, &snd_size, 14459 B_TRUE); 14460 14461 if (mp1 != NULL) { 14462 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14463 tcp->tcp_csuna = tcp->tcp_snxt; 14464 BUMP_MIB(&tcp_mib, tcpRetransSegs); 14465 UPDATE_MIB(&tcp_mib, tcpRetransBytes, snd_size); 14466 TCP_RECORD_TRACE(tcp, mp1, 14467 TCP_TRACE_SEND_PKT); 14468 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14469 } 14470 } 14471 if (flags & TH_NEED_SACK_REXMIT) { 14472 tcp_sack_rxmit(tcp, &flags); 14473 } 14474 /* 14475 * For TH_LIMIT_XMIT, tcp_wput_data() is called to send 14476 * out new segment. Note that tcp_rexmit should not be 14477 * set, otherwise TH_LIMIT_XMIT should not be set. 14478 */ 14479 if (flags & (TH_XMIT_NEEDED|TH_LIMIT_XMIT)) { 14480 if (!tcp->tcp_rexmit) { 14481 tcp_wput_data(tcp, NULL, B_FALSE); 14482 } else { 14483 tcp_ss_rexmit(tcp); 14484 } 14485 } 14486 /* 14487 * Adjust tcp_cwnd back to normal value after sending 14488 * new data segments. 14489 */ 14490 if (flags & TH_LIMIT_XMIT) { 14491 tcp->tcp_cwnd -= mss << (tcp->tcp_dupack_cnt - 1); 14492 /* 14493 * This will restart the timer. Restarting the 14494 * timer is used to avoid a timeout before the 14495 * limited transmitted segment's ACK gets back. 14496 */ 14497 if (tcp->tcp_xmit_head != NULL) 14498 tcp->tcp_xmit_head->b_prev = (mblk_t *)lbolt; 14499 } 14500 14501 /* Anything more to do? */ 14502 if ((flags & (TH_ACK_NEEDED|TH_ACK_TIMER_NEEDED| 14503 TH_ORDREL_NEEDED|TH_SEND_URP_MARK)) == 0) 14504 goto done; 14505 } 14506 ack_check: 14507 if (flags & TH_SEND_URP_MARK) { 14508 ASSERT(tcp->tcp_urp_mark_mp); 14509 /* 14510 * Send up any queued data and then send the mark message 14511 */ 14512 if (tcp->tcp_rcv_list != NULL) { 14513 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14514 } 14515 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14516 14517 mp1 = tcp->tcp_urp_mark_mp; 14518 tcp->tcp_urp_mark_mp = NULL; 14519 #ifdef DEBUG 14520 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 14521 "tcp_rput: sending zero-length %s %s", 14522 ((mp1->b_flag & MSGMARKNEXT) ? "MSGMARKNEXT" : 14523 "MSGNOTMARKNEXT"), 14524 tcp_display(tcp, NULL, DISP_PORT_ONLY)); 14525 #endif /* DEBUG */ 14526 putnext(tcp->tcp_rq, mp1); 14527 flags &= ~TH_SEND_URP_MARK; 14528 } 14529 if (flags & TH_ACK_NEEDED) { 14530 /* 14531 * Time to send an ack for some reason. 14532 */ 14533 mp1 = tcp_ack_mp(tcp); 14534 14535 if (mp1 != NULL) { 14536 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 14537 tcp_send_data(tcp, tcp->tcp_wq, mp1); 14538 BUMP_LOCAL(tcp->tcp_obsegs); 14539 BUMP_MIB(&tcp_mib, tcpOutAck); 14540 } 14541 if (tcp->tcp_ack_tid != 0) { 14542 (void) TCP_TIMER_CANCEL(tcp, tcp->tcp_ack_tid); 14543 tcp->tcp_ack_tid = 0; 14544 } 14545 } 14546 if (flags & TH_ACK_TIMER_NEEDED) { 14547 /* 14548 * Arrange for deferred ACK or push wait timeout. 14549 * Start timer if it is not already running. 14550 */ 14551 if (tcp->tcp_ack_tid == 0) { 14552 tcp->tcp_ack_tid = TCP_TIMER(tcp, tcp_ack_timer, 14553 MSEC_TO_TICK(tcp->tcp_localnet ? 14554 (clock_t)tcp_local_dack_interval : 14555 (clock_t)tcp_deferred_ack_interval)); 14556 } 14557 } 14558 if (flags & TH_ORDREL_NEEDED) { 14559 /* 14560 * Send up the ordrel_ind unless we are an eager guy. 14561 * In the eager case tcp_rsrv will do this when run 14562 * after tcp_accept is done. 14563 */ 14564 ASSERT(tcp->tcp_listener == NULL); 14565 if (tcp->tcp_rcv_list != NULL) { 14566 /* 14567 * Push any mblk(s) enqueued from co processing. 14568 */ 14569 flags |= tcp_rcv_drain(tcp->tcp_rq, tcp); 14570 } 14571 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 14572 if ((mp1 = mi_tpi_ordrel_ind()) != NULL) { 14573 tcp->tcp_ordrel_done = B_TRUE; 14574 putnext(tcp->tcp_rq, mp1); 14575 if (tcp->tcp_deferred_clean_death) { 14576 /* 14577 * tcp_clean_death was deferred 14578 * for T_ORDREL_IND - do it now 14579 */ 14580 (void) tcp_clean_death(tcp, 14581 tcp->tcp_client_errno, 20); 14582 tcp->tcp_deferred_clean_death = B_FALSE; 14583 } 14584 } else { 14585 /* 14586 * Run the orderly release in the 14587 * service routine. 14588 */ 14589 qenable(tcp->tcp_rq); 14590 /* 14591 * Caveat(XXX): The machine may be so 14592 * overloaded that tcp_rsrv() is not scheduled 14593 * until after the endpoint has transitioned 14594 * to TCPS_TIME_WAIT 14595 * and tcp_time_wait_interval expires. Then 14596 * tcp_timer() will blow away state in tcp_t 14597 * and T_ORDREL_IND will never be delivered 14598 * upstream. Unlikely but potentially 14599 * a problem. 14600 */ 14601 } 14602 } 14603 done: 14604 ASSERT(!(flags & TH_MARKNEXT_NEEDED)); 14605 } 14606 14607 /* 14608 * This function does PAWS protection check. Returns B_TRUE if the 14609 * segment passes the PAWS test, else returns B_FALSE. 14610 */ 14611 boolean_t 14612 tcp_paws_check(tcp_t *tcp, tcph_t *tcph, tcp_opt_t *tcpoptp) 14613 { 14614 uint8_t flags; 14615 int options; 14616 uint8_t *up; 14617 14618 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 14619 /* 14620 * If timestamp option is aligned nicely, get values inline, 14621 * otherwise call general routine to parse. Only do that 14622 * if timestamp is the only option. 14623 */ 14624 if (TCP_HDR_LENGTH(tcph) == (uint32_t)TCP_MIN_HEADER_LENGTH + 14625 TCPOPT_REAL_TS_LEN && 14626 OK_32PTR((up = ((uint8_t *)tcph) + 14627 TCP_MIN_HEADER_LENGTH)) && 14628 *(uint32_t *)up == TCPOPT_NOP_NOP_TSTAMP) { 14629 tcpoptp->tcp_opt_ts_val = ABE32_TO_U32((up+4)); 14630 tcpoptp->tcp_opt_ts_ecr = ABE32_TO_U32((up+8)); 14631 14632 options = TCP_OPT_TSTAMP_PRESENT; 14633 } else { 14634 if (tcp->tcp_snd_sack_ok) { 14635 tcpoptp->tcp = tcp; 14636 } else { 14637 tcpoptp->tcp = NULL; 14638 } 14639 options = tcp_parse_options(tcph, tcpoptp); 14640 } 14641 14642 if (options & TCP_OPT_TSTAMP_PRESENT) { 14643 /* 14644 * Do PAWS per RFC 1323 section 4.2. Accept RST 14645 * regardless of the timestamp, page 18 RFC 1323.bis. 14646 */ 14647 if ((flags & TH_RST) == 0 && 14648 TSTMP_LT(tcpoptp->tcp_opt_ts_val, 14649 tcp->tcp_ts_recent)) { 14650 if (TSTMP_LT(lbolt64, tcp->tcp_last_rcv_lbolt + 14651 PAWS_TIMEOUT)) { 14652 /* This segment is not acceptable. */ 14653 return (B_FALSE); 14654 } else { 14655 /* 14656 * Connection has been idle for 14657 * too long. Reset the timestamp 14658 * and assume the segment is valid. 14659 */ 14660 tcp->tcp_ts_recent = 14661 tcpoptp->tcp_opt_ts_val; 14662 } 14663 } 14664 } else { 14665 /* 14666 * If we don't get a timestamp on every packet, we 14667 * figure we can't really trust 'em, so we stop sending 14668 * and parsing them. 14669 */ 14670 tcp->tcp_snd_ts_ok = B_FALSE; 14671 14672 tcp->tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 14673 tcp->tcp_tcp_hdr_len -= TCPOPT_REAL_TS_LEN; 14674 tcp->tcp_tcph->th_offset_and_rsrvd[0] -= (3 << 4); 14675 tcp_mss_set(tcp, tcp->tcp_mss + TCPOPT_REAL_TS_LEN); 14676 if (tcp->tcp_snd_sack_ok) { 14677 ASSERT(tcp->tcp_sack_info != NULL); 14678 tcp->tcp_max_sack_blk = 4; 14679 } 14680 } 14681 return (B_TRUE); 14682 } 14683 14684 /* 14685 * Attach ancillary data to a received TCP segments for the 14686 * ancillary pieces requested by the application that are 14687 * different than they were in the previous data segment. 14688 * 14689 * Save the "current" values once memory allocation is ok so that 14690 * when memory allocation fails we can just wait for the next data segment. 14691 */ 14692 static mblk_t * 14693 tcp_rput_add_ancillary(tcp_t *tcp, mblk_t *mp, ip6_pkt_t *ipp) 14694 { 14695 struct T_optdata_ind *todi; 14696 int optlen; 14697 uchar_t *optptr; 14698 struct T_opthdr *toh; 14699 uint_t addflag; /* Which pieces to add */ 14700 mblk_t *mp1; 14701 14702 optlen = 0; 14703 addflag = 0; 14704 /* If app asked for pktinfo and the index has changed ... */ 14705 if ((ipp->ipp_fields & IPPF_IFINDEX) && 14706 ipp->ipp_ifindex != tcp->tcp_recvifindex && 14707 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO)) { 14708 optlen += sizeof (struct T_opthdr) + 14709 sizeof (struct in6_pktinfo); 14710 addflag |= TCP_IPV6_RECVPKTINFO; 14711 } 14712 /* If app asked for hoplimit and it has changed ... */ 14713 if ((ipp->ipp_fields & IPPF_HOPLIMIT) && 14714 ipp->ipp_hoplimit != tcp->tcp_recvhops && 14715 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPLIMIT)) { 14716 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 14717 addflag |= TCP_IPV6_RECVHOPLIMIT; 14718 } 14719 /* If app asked for tclass and it has changed ... */ 14720 if ((ipp->ipp_fields & IPPF_TCLASS) && 14721 ipp->ipp_tclass != tcp->tcp_recvtclass && 14722 (tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVTCLASS)) { 14723 optlen += sizeof (struct T_opthdr) + sizeof (uint_t); 14724 addflag |= TCP_IPV6_RECVTCLASS; 14725 } 14726 /* If app asked for hopbyhop headers and it has changed ... */ 14727 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVHOPOPTS) && 14728 tcp_cmpbuf(tcp->tcp_hopopts, tcp->tcp_hopoptslen, 14729 (ipp->ipp_fields & IPPF_HOPOPTS), 14730 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) { 14731 optlen += sizeof (struct T_opthdr) + ipp->ipp_hopoptslen; 14732 addflag |= TCP_IPV6_RECVHOPOPTS; 14733 if (!tcp_allocbuf((void **)&tcp->tcp_hopopts, 14734 &tcp->tcp_hopoptslen, 14735 (ipp->ipp_fields & IPPF_HOPOPTS), 14736 ipp->ipp_hopopts, ipp->ipp_hopoptslen)) 14737 return (mp); 14738 } 14739 /* If app asked for dst headers before routing headers ... */ 14740 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTDSTOPTS) && 14741 tcp_cmpbuf(tcp->tcp_rtdstopts, tcp->tcp_rtdstoptslen, 14742 (ipp->ipp_fields & IPPF_RTDSTOPTS), 14743 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) { 14744 optlen += sizeof (struct T_opthdr) + 14745 ipp->ipp_rtdstoptslen; 14746 addflag |= TCP_IPV6_RECVRTDSTOPTS; 14747 if (!tcp_allocbuf((void **)&tcp->tcp_rtdstopts, 14748 &tcp->tcp_rtdstoptslen, 14749 (ipp->ipp_fields & IPPF_RTDSTOPTS), 14750 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen)) 14751 return (mp); 14752 } 14753 /* If app asked for routing headers and it has changed ... */ 14754 if ((tcp->tcp_ipv6_recvancillary & TCP_IPV6_RECVRTHDR) && 14755 tcp_cmpbuf(tcp->tcp_rthdr, tcp->tcp_rthdrlen, 14756 (ipp->ipp_fields & IPPF_RTHDR), 14757 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) { 14758 optlen += sizeof (struct T_opthdr) + ipp->ipp_rthdrlen; 14759 addflag |= TCP_IPV6_RECVRTHDR; 14760 if (!tcp_allocbuf((void **)&tcp->tcp_rthdr, 14761 &tcp->tcp_rthdrlen, 14762 (ipp->ipp_fields & IPPF_RTHDR), 14763 ipp->ipp_rthdr, ipp->ipp_rthdrlen)) 14764 return (mp); 14765 } 14766 /* If app asked for dest headers and it has changed ... */ 14767 if ((tcp->tcp_ipv6_recvancillary & 14768 (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) && 14769 tcp_cmpbuf(tcp->tcp_dstopts, tcp->tcp_dstoptslen, 14770 (ipp->ipp_fields & IPPF_DSTOPTS), 14771 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) { 14772 optlen += sizeof (struct T_opthdr) + ipp->ipp_dstoptslen; 14773 addflag |= TCP_IPV6_RECVDSTOPTS; 14774 if (!tcp_allocbuf((void **)&tcp->tcp_dstopts, 14775 &tcp->tcp_dstoptslen, 14776 (ipp->ipp_fields & IPPF_DSTOPTS), 14777 ipp->ipp_dstopts, ipp->ipp_dstoptslen)) 14778 return (mp); 14779 } 14780 14781 if (optlen == 0) { 14782 /* Nothing to add */ 14783 return (mp); 14784 } 14785 mp1 = allocb(sizeof (struct T_optdata_ind) + optlen, BPRI_MED); 14786 if (mp1 == NULL) { 14787 /* 14788 * Defer sending ancillary data until the next TCP segment 14789 * arrives. 14790 */ 14791 return (mp); 14792 } 14793 mp1->b_cont = mp; 14794 mp = mp1; 14795 mp->b_wptr += sizeof (*todi) + optlen; 14796 mp->b_datap->db_type = M_PROTO; 14797 todi = (struct T_optdata_ind *)mp->b_rptr; 14798 todi->PRIM_type = T_OPTDATA_IND; 14799 todi->DATA_flag = 1; /* MORE data */ 14800 todi->OPT_length = optlen; 14801 todi->OPT_offset = sizeof (*todi); 14802 optptr = (uchar_t *)&todi[1]; 14803 /* 14804 * If app asked for pktinfo and the index has changed ... 14805 * Note that the local address never changes for the connection. 14806 */ 14807 if (addflag & TCP_IPV6_RECVPKTINFO) { 14808 struct in6_pktinfo *pkti; 14809 14810 toh = (struct T_opthdr *)optptr; 14811 toh->level = IPPROTO_IPV6; 14812 toh->name = IPV6_PKTINFO; 14813 toh->len = sizeof (*toh) + sizeof (*pkti); 14814 toh->status = 0; 14815 optptr += sizeof (*toh); 14816 pkti = (struct in6_pktinfo *)optptr; 14817 if (tcp->tcp_ipversion == IPV6_VERSION) 14818 pkti->ipi6_addr = tcp->tcp_ip6h->ip6_src; 14819 else 14820 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 14821 &pkti->ipi6_addr); 14822 pkti->ipi6_ifindex = ipp->ipp_ifindex; 14823 optptr += sizeof (*pkti); 14824 ASSERT(OK_32PTR(optptr)); 14825 /* Save as "last" value */ 14826 tcp->tcp_recvifindex = ipp->ipp_ifindex; 14827 } 14828 /* If app asked for hoplimit and it has changed ... */ 14829 if (addflag & TCP_IPV6_RECVHOPLIMIT) { 14830 toh = (struct T_opthdr *)optptr; 14831 toh->level = IPPROTO_IPV6; 14832 toh->name = IPV6_HOPLIMIT; 14833 toh->len = sizeof (*toh) + sizeof (uint_t); 14834 toh->status = 0; 14835 optptr += sizeof (*toh); 14836 *(uint_t *)optptr = ipp->ipp_hoplimit; 14837 optptr += sizeof (uint_t); 14838 ASSERT(OK_32PTR(optptr)); 14839 /* Save as "last" value */ 14840 tcp->tcp_recvhops = ipp->ipp_hoplimit; 14841 } 14842 /* If app asked for tclass and it has changed ... */ 14843 if (addflag & TCP_IPV6_RECVTCLASS) { 14844 toh = (struct T_opthdr *)optptr; 14845 toh->level = IPPROTO_IPV6; 14846 toh->name = IPV6_TCLASS; 14847 toh->len = sizeof (*toh) + sizeof (uint_t); 14848 toh->status = 0; 14849 optptr += sizeof (*toh); 14850 *(uint_t *)optptr = ipp->ipp_tclass; 14851 optptr += sizeof (uint_t); 14852 ASSERT(OK_32PTR(optptr)); 14853 /* Save as "last" value */ 14854 tcp->tcp_recvtclass = ipp->ipp_tclass; 14855 } 14856 if (addflag & TCP_IPV6_RECVHOPOPTS) { 14857 toh = (struct T_opthdr *)optptr; 14858 toh->level = IPPROTO_IPV6; 14859 toh->name = IPV6_HOPOPTS; 14860 toh->len = sizeof (*toh) + ipp->ipp_hopoptslen; 14861 toh->status = 0; 14862 optptr += sizeof (*toh); 14863 bcopy(ipp->ipp_hopopts, optptr, ipp->ipp_hopoptslen); 14864 optptr += ipp->ipp_hopoptslen; 14865 ASSERT(OK_32PTR(optptr)); 14866 /* Save as last value */ 14867 tcp_savebuf((void **)&tcp->tcp_hopopts, 14868 &tcp->tcp_hopoptslen, 14869 (ipp->ipp_fields & IPPF_HOPOPTS), 14870 ipp->ipp_hopopts, ipp->ipp_hopoptslen); 14871 } 14872 if (addflag & TCP_IPV6_RECVRTDSTOPTS) { 14873 toh = (struct T_opthdr *)optptr; 14874 toh->level = IPPROTO_IPV6; 14875 toh->name = IPV6_RTHDRDSTOPTS; 14876 toh->len = sizeof (*toh) + ipp->ipp_rtdstoptslen; 14877 toh->status = 0; 14878 optptr += sizeof (*toh); 14879 bcopy(ipp->ipp_rtdstopts, optptr, ipp->ipp_rtdstoptslen); 14880 optptr += ipp->ipp_rtdstoptslen; 14881 ASSERT(OK_32PTR(optptr)); 14882 /* Save as last value */ 14883 tcp_savebuf((void **)&tcp->tcp_rtdstopts, 14884 &tcp->tcp_rtdstoptslen, 14885 (ipp->ipp_fields & IPPF_RTDSTOPTS), 14886 ipp->ipp_rtdstopts, ipp->ipp_rtdstoptslen); 14887 } 14888 if (addflag & TCP_IPV6_RECVRTHDR) { 14889 toh = (struct T_opthdr *)optptr; 14890 toh->level = IPPROTO_IPV6; 14891 toh->name = IPV6_RTHDR; 14892 toh->len = sizeof (*toh) + ipp->ipp_rthdrlen; 14893 toh->status = 0; 14894 optptr += sizeof (*toh); 14895 bcopy(ipp->ipp_rthdr, optptr, ipp->ipp_rthdrlen); 14896 optptr += ipp->ipp_rthdrlen; 14897 ASSERT(OK_32PTR(optptr)); 14898 /* Save as last value */ 14899 tcp_savebuf((void **)&tcp->tcp_rthdr, 14900 &tcp->tcp_rthdrlen, 14901 (ipp->ipp_fields & IPPF_RTHDR), 14902 ipp->ipp_rthdr, ipp->ipp_rthdrlen); 14903 } 14904 if (addflag & (TCP_IPV6_RECVDSTOPTS | TCP_OLD_IPV6_RECVDSTOPTS)) { 14905 toh = (struct T_opthdr *)optptr; 14906 toh->level = IPPROTO_IPV6; 14907 toh->name = IPV6_DSTOPTS; 14908 toh->len = sizeof (*toh) + ipp->ipp_dstoptslen; 14909 toh->status = 0; 14910 optptr += sizeof (*toh); 14911 bcopy(ipp->ipp_dstopts, optptr, ipp->ipp_dstoptslen); 14912 optptr += ipp->ipp_dstoptslen; 14913 ASSERT(OK_32PTR(optptr)); 14914 /* Save as last value */ 14915 tcp_savebuf((void **)&tcp->tcp_dstopts, 14916 &tcp->tcp_dstoptslen, 14917 (ipp->ipp_fields & IPPF_DSTOPTS), 14918 ipp->ipp_dstopts, ipp->ipp_dstoptslen); 14919 } 14920 ASSERT(optptr == mp->b_wptr); 14921 return (mp); 14922 } 14923 14924 14925 /* 14926 * Handle a *T_BIND_REQ that has failed either due to a T_ERROR_ACK 14927 * or a "bad" IRE detected by tcp_adapt_ire. 14928 * We can't tell if the failure was due to the laddr or the faddr 14929 * thus we clear out all addresses and ports. 14930 */ 14931 static void 14932 tcp_bind_failed(tcp_t *tcp, mblk_t *mp, int error) 14933 { 14934 queue_t *q = tcp->tcp_rq; 14935 tcph_t *tcph; 14936 struct T_error_ack *tea; 14937 conn_t *connp = tcp->tcp_connp; 14938 14939 14940 ASSERT(mp->b_datap->db_type == M_PCPROTO); 14941 14942 if (mp->b_cont) { 14943 freemsg(mp->b_cont); 14944 mp->b_cont = NULL; 14945 } 14946 tea = (struct T_error_ack *)mp->b_rptr; 14947 switch (tea->PRIM_type) { 14948 case T_BIND_ACK: 14949 /* 14950 * Need to unbind with classifier since we were just told that 14951 * our bind succeeded. 14952 */ 14953 tcp->tcp_hard_bound = B_FALSE; 14954 tcp->tcp_hard_binding = B_FALSE; 14955 14956 ipcl_hash_remove(connp); 14957 /* Reuse the mblk if possible */ 14958 ASSERT(mp->b_datap->db_lim - mp->b_datap->db_base >= 14959 sizeof (*tea)); 14960 mp->b_rptr = mp->b_datap->db_base; 14961 mp->b_wptr = mp->b_rptr + sizeof (*tea); 14962 tea = (struct T_error_ack *)mp->b_rptr; 14963 tea->PRIM_type = T_ERROR_ACK; 14964 tea->TLI_error = TSYSERR; 14965 tea->UNIX_error = error; 14966 if (tcp->tcp_state >= TCPS_SYN_SENT) { 14967 tea->ERROR_prim = T_CONN_REQ; 14968 } else { 14969 tea->ERROR_prim = O_T_BIND_REQ; 14970 } 14971 break; 14972 14973 case T_ERROR_ACK: 14974 if (tcp->tcp_state >= TCPS_SYN_SENT) 14975 tea->ERROR_prim = T_CONN_REQ; 14976 break; 14977 default: 14978 panic("tcp_bind_failed: unexpected TPI type"); 14979 /*NOTREACHED*/ 14980 } 14981 14982 tcp->tcp_state = TCPS_IDLE; 14983 if (tcp->tcp_ipversion == IPV4_VERSION) 14984 tcp->tcp_ipha->ipha_src = 0; 14985 else 14986 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 14987 /* 14988 * Copy of the src addr. in tcp_t is needed since 14989 * the lookup funcs. can only look at tcp_t 14990 */ 14991 V6_SET_ZERO(tcp->tcp_ip_src_v6); 14992 14993 tcph = tcp->tcp_tcph; 14994 tcph->th_lport[0] = 0; 14995 tcph->th_lport[1] = 0; 14996 tcp_bind_hash_remove(tcp); 14997 bzero(&connp->u_port, sizeof (connp->u_port)); 14998 /* blow away saved option results if any */ 14999 if (tcp->tcp_conn.tcp_opts_conn_req != NULL) 15000 tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req); 15001 15002 conn_delete_ire(tcp->tcp_connp, NULL); 15003 putnext(q, mp); 15004 } 15005 15006 /* 15007 * tcp_rput_other is called by tcp_rput to handle everything other than M_DATA 15008 * messages. 15009 */ 15010 void 15011 tcp_rput_other(tcp_t *tcp, mblk_t *mp) 15012 { 15013 mblk_t *mp1; 15014 uchar_t *rptr = mp->b_rptr; 15015 queue_t *q = tcp->tcp_rq; 15016 struct T_error_ack *tea; 15017 uint32_t mss; 15018 mblk_t *syn_mp; 15019 mblk_t *mdti; 15020 int retval; 15021 mblk_t *ire_mp; 15022 15023 switch (mp->b_datap->db_type) { 15024 case M_PROTO: 15025 case M_PCPROTO: 15026 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 15027 if ((mp->b_wptr - rptr) < sizeof (t_scalar_t)) 15028 break; 15029 tea = (struct T_error_ack *)rptr; 15030 switch (tea->PRIM_type) { 15031 case T_BIND_ACK: 15032 /* 15033 * Adapt Multidata information, if any. The 15034 * following tcp_mdt_update routine will free 15035 * the message. 15036 */ 15037 if ((mdti = tcp_mdt_info_mp(mp)) != NULL) { 15038 tcp_mdt_update(tcp, &((ip_mdt_info_t *)mdti-> 15039 b_rptr)->mdt_capab, B_TRUE); 15040 freemsg(mdti); 15041 } 15042 15043 /* Get the IRE, if we had requested for it */ 15044 ire_mp = tcp_ire_mp(mp); 15045 15046 if (tcp->tcp_hard_binding) { 15047 tcp->tcp_hard_binding = B_FALSE; 15048 tcp->tcp_hard_bound = B_TRUE; 15049 CL_INET_CONNECT(tcp); 15050 } else { 15051 if (ire_mp != NULL) 15052 freeb(ire_mp); 15053 goto after_syn_sent; 15054 } 15055 15056 retval = tcp_adapt_ire(tcp, ire_mp); 15057 if (ire_mp != NULL) 15058 freeb(ire_mp); 15059 if (retval == 0) { 15060 tcp_bind_failed(tcp, mp, 15061 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15062 ENETUNREACH : EADDRNOTAVAIL)); 15063 return; 15064 } 15065 /* 15066 * Don't let an endpoint connect to itself. 15067 * Also checked in tcp_connect() but that 15068 * check can't handle the case when the 15069 * local IP address is INADDR_ANY. 15070 */ 15071 if (tcp->tcp_ipversion == IPV4_VERSION) { 15072 if ((tcp->tcp_ipha->ipha_dst == 15073 tcp->tcp_ipha->ipha_src) && 15074 (BE16_EQL(tcp->tcp_tcph->th_lport, 15075 tcp->tcp_tcph->th_fport))) { 15076 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15077 return; 15078 } 15079 } else { 15080 if (IN6_ARE_ADDR_EQUAL( 15081 &tcp->tcp_ip6h->ip6_dst, 15082 &tcp->tcp_ip6h->ip6_src) && 15083 (BE16_EQL(tcp->tcp_tcph->th_lport, 15084 tcp->tcp_tcph->th_fport))) { 15085 tcp_bind_failed(tcp, mp, EADDRNOTAVAIL); 15086 return; 15087 } 15088 } 15089 ASSERT(tcp->tcp_state == TCPS_SYN_SENT); 15090 /* 15091 * This should not be possible! Just for 15092 * defensive coding... 15093 */ 15094 if (tcp->tcp_state != TCPS_SYN_SENT) 15095 goto after_syn_sent; 15096 15097 ASSERT(q == tcp->tcp_rq); 15098 /* 15099 * tcp_adapt_ire() does not adjust 15100 * for TCP/IP header length. 15101 */ 15102 mss = tcp->tcp_mss - tcp->tcp_hdr_len; 15103 15104 /* 15105 * Just make sure our rwnd is at 15106 * least tcp_recv_hiwat_mss * MSS 15107 * large, and round up to the nearest 15108 * MSS. 15109 * 15110 * We do the round up here because 15111 * we need to get the interface 15112 * MTU first before we can do the 15113 * round up. 15114 */ 15115 tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss), 15116 tcp_recv_hiwat_minmss * mss); 15117 q->q_hiwat = tcp->tcp_rwnd; 15118 tcp_set_ws_value(tcp); 15119 U32_TO_ABE16((tcp->tcp_rwnd >> tcp->tcp_rcv_ws), 15120 tcp->tcp_tcph->th_win); 15121 if (tcp->tcp_rcv_ws > 0 || tcp_wscale_always) 15122 tcp->tcp_snd_ws_ok = B_TRUE; 15123 15124 /* 15125 * Set tcp_snd_ts_ok to true 15126 * so that tcp_xmit_mp will 15127 * include the timestamp 15128 * option in the SYN segment. 15129 */ 15130 if (tcp_tstamp_always || 15131 (tcp->tcp_rcv_ws && tcp_tstamp_if_wscale)) { 15132 tcp->tcp_snd_ts_ok = B_TRUE; 15133 } 15134 15135 /* 15136 * tcp_snd_sack_ok can be set in 15137 * tcp_adapt_ire() if the sack metric 15138 * is set. So check it here also. 15139 */ 15140 if (tcp_sack_permitted == 2 || 15141 tcp->tcp_snd_sack_ok) { 15142 if (tcp->tcp_sack_info == NULL) { 15143 tcp->tcp_sack_info = 15144 kmem_cache_alloc(tcp_sack_info_cache, 15145 KM_SLEEP); 15146 } 15147 tcp->tcp_snd_sack_ok = B_TRUE; 15148 } 15149 15150 /* 15151 * Should we use ECN? Note that the current 15152 * default value (SunOS 5.9) of tcp_ecn_permitted 15153 * is 1. The reason for doing this is that there 15154 * are equipments out there that will drop ECN 15155 * enabled IP packets. Setting it to 1 avoids 15156 * compatibility problems. 15157 */ 15158 if (tcp_ecn_permitted == 2) 15159 tcp->tcp_ecn_ok = B_TRUE; 15160 15161 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 15162 syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 15163 tcp->tcp_iss, B_FALSE, NULL, B_FALSE); 15164 if (syn_mp) { 15165 cred_t *cr; 15166 pid_t pid; 15167 15168 /* 15169 * Obtain the credential from the 15170 * thread calling connect(); the credential 15171 * lives on in the second mblk which 15172 * originated from T_CONN_REQ and is echoed 15173 * with the T_BIND_ACK from ip. If none 15174 * can be found, default to the creator 15175 * of the socket. 15176 */ 15177 if (mp->b_cont == NULL || 15178 (cr = DB_CRED(mp->b_cont)) == NULL) { 15179 cr = tcp->tcp_cred; 15180 pid = tcp->tcp_cpid; 15181 } else { 15182 pid = DB_CPID(mp->b_cont); 15183 } 15184 15185 TCP_RECORD_TRACE(tcp, syn_mp, 15186 TCP_TRACE_SEND_PKT); 15187 mblk_setcred(syn_mp, cr); 15188 DB_CPID(syn_mp) = pid; 15189 tcp_send_data(tcp, tcp->tcp_wq, syn_mp); 15190 } 15191 after_syn_sent: 15192 /* 15193 * A trailer mblk indicates a waiting client upstream. 15194 * We complete here the processing begun in 15195 * either tcp_bind() or tcp_connect() by passing 15196 * upstream the reply message they supplied. 15197 */ 15198 mp1 = mp; 15199 mp = mp->b_cont; 15200 freeb(mp1); 15201 if (mp) 15202 break; 15203 return; 15204 case T_ERROR_ACK: 15205 if (tcp->tcp_debug) { 15206 (void) strlog(TCP_MOD_ID, 0, 1, 15207 SL_TRACE|SL_ERROR, 15208 "tcp_rput_other: case T_ERROR_ACK, " 15209 "ERROR_prim == %d", 15210 tea->ERROR_prim); 15211 } 15212 switch (tea->ERROR_prim) { 15213 case O_T_BIND_REQ: 15214 case T_BIND_REQ: 15215 tcp_bind_failed(tcp, mp, 15216 (int)((tcp->tcp_state >= TCPS_SYN_SENT) ? 15217 ENETUNREACH : EADDRNOTAVAIL)); 15218 return; 15219 case T_UNBIND_REQ: 15220 tcp->tcp_hard_binding = B_FALSE; 15221 tcp->tcp_hard_bound = B_FALSE; 15222 if (mp->b_cont) { 15223 freemsg(mp->b_cont); 15224 mp->b_cont = NULL; 15225 } 15226 if (tcp->tcp_unbind_pending) 15227 tcp->tcp_unbind_pending = 0; 15228 else { 15229 /* From tcp_ip_unbind() - free */ 15230 freemsg(mp); 15231 return; 15232 } 15233 break; 15234 case T_SVR4_OPTMGMT_REQ: 15235 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15236 /* T_OPTMGMT_REQ generated by TCP */ 15237 printf("T_SVR4_OPTMGMT_REQ failed " 15238 "%d/%d - dropped (cnt %d)\n", 15239 tea->TLI_error, tea->UNIX_error, 15240 tcp->tcp_drop_opt_ack_cnt); 15241 freemsg(mp); 15242 tcp->tcp_drop_opt_ack_cnt--; 15243 return; 15244 } 15245 break; 15246 } 15247 if (tea->ERROR_prim == T_SVR4_OPTMGMT_REQ && 15248 tcp->tcp_drop_opt_ack_cnt > 0) { 15249 printf("T_SVR4_OPTMGMT_REQ failed %d/%d " 15250 "- dropped (cnt %d)\n", 15251 tea->TLI_error, tea->UNIX_error, 15252 tcp->tcp_drop_opt_ack_cnt); 15253 freemsg(mp); 15254 tcp->tcp_drop_opt_ack_cnt--; 15255 return; 15256 } 15257 break; 15258 case T_OPTMGMT_ACK: 15259 if (tcp->tcp_drop_opt_ack_cnt > 0) { 15260 /* T_OPTMGMT_REQ generated by TCP */ 15261 freemsg(mp); 15262 tcp->tcp_drop_opt_ack_cnt--; 15263 return; 15264 } 15265 break; 15266 default: 15267 break; 15268 } 15269 break; 15270 case M_CTL: 15271 /* 15272 * ICMP messages. 15273 */ 15274 tcp_icmp_error(tcp, mp); 15275 return; 15276 case M_FLUSH: 15277 if (*rptr & FLUSHR) 15278 flushq(q, FLUSHDATA); 15279 break; 15280 default: 15281 break; 15282 } 15283 /* 15284 * Make sure we set this bit before sending the ACK for 15285 * bind. Otherwise accept could possibly run and free 15286 * this tcp struct. 15287 */ 15288 putnext(q, mp); 15289 } 15290 15291 /* 15292 * Called as the result of a qbufcall or a qtimeout to remedy a failure 15293 * to allocate a T_ordrel_ind in tcp_rsrv(). qenable(q) will make 15294 * tcp_rsrv() try again. 15295 */ 15296 static void 15297 tcp_ordrel_kick(void *arg) 15298 { 15299 conn_t *connp = (conn_t *)arg; 15300 tcp_t *tcp = connp->conn_tcp; 15301 15302 tcp->tcp_ordrelid = 0; 15303 tcp->tcp_timeout = B_FALSE; 15304 if (!TCP_IS_DETACHED(tcp) && tcp->tcp_rq != NULL && 15305 tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15306 qenable(tcp->tcp_rq); 15307 } 15308 } 15309 15310 /* ARGSUSED */ 15311 static void 15312 tcp_rsrv_input(void *arg, mblk_t *mp, void *arg2) 15313 { 15314 conn_t *connp = (conn_t *)arg; 15315 tcp_t *tcp = connp->conn_tcp; 15316 queue_t *q = tcp->tcp_rq; 15317 uint_t thwin; 15318 15319 freeb(mp); 15320 15321 TCP_STAT(tcp_rsrv_calls); 15322 15323 if (TCP_IS_DETACHED(tcp) || q == NULL) { 15324 return; 15325 } 15326 15327 if (tcp->tcp_fused) { 15328 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15329 15330 ASSERT(tcp->tcp_fused); 15331 ASSERT(peer_tcp != NULL && peer_tcp->tcp_fused); 15332 ASSERT(peer_tcp->tcp_loopback_peer == tcp); 15333 ASSERT(!TCP_IS_DETACHED(tcp)); 15334 ASSERT(tcp->tcp_connp->conn_sqp == 15335 peer_tcp->tcp_connp->conn_sqp); 15336 15337 /* 15338 * Normally we would not get backenabled in synchronous 15339 * streams mode, but in case this happens, we need to stop 15340 * synchronous streams temporarily to prevent a race with 15341 * tcp_fuse_rrw() or tcp_fuse_rinfop(). It is safe to access 15342 * tcp_rcv_list here because those entry points will return 15343 * right away when synchronous streams is stopped. 15344 */ 15345 TCP_FUSE_SYNCSTR_STOP(tcp); 15346 if (tcp->tcp_rcv_list != NULL) 15347 (void) tcp_rcv_drain(tcp->tcp_rq, tcp); 15348 15349 tcp_clrqfull(peer_tcp); 15350 TCP_FUSE_SYNCSTR_RESUME(tcp); 15351 TCP_STAT(tcp_fusion_backenabled); 15352 return; 15353 } 15354 15355 if (canputnext(q)) { 15356 tcp->tcp_rwnd = q->q_hiwat; 15357 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 15358 << tcp->tcp_rcv_ws; 15359 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 15360 /* 15361 * Send back a window update immediately if TCP is above 15362 * ESTABLISHED state and the increase of the rcv window 15363 * that the other side knows is at least 1 MSS after flow 15364 * control is lifted. 15365 */ 15366 if (tcp->tcp_state >= TCPS_ESTABLISHED && 15367 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 15368 tcp_xmit_ctl(NULL, tcp, 15369 (tcp->tcp_swnd == 0) ? tcp->tcp_suna : 15370 tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 15371 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 15372 } 15373 } 15374 /* Handle a failure to allocate a T_ORDREL_IND here */ 15375 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 15376 ASSERT(tcp->tcp_listener == NULL); 15377 if (tcp->tcp_rcv_list != NULL) { 15378 (void) tcp_rcv_drain(q, tcp); 15379 } 15380 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 15381 mp = mi_tpi_ordrel_ind(); 15382 if (mp) { 15383 tcp->tcp_ordrel_done = B_TRUE; 15384 putnext(q, mp); 15385 if (tcp->tcp_deferred_clean_death) { 15386 /* 15387 * tcp_clean_death was deferred for 15388 * T_ORDREL_IND - do it now 15389 */ 15390 tcp->tcp_deferred_clean_death = B_FALSE; 15391 (void) tcp_clean_death(tcp, 15392 tcp->tcp_client_errno, 22); 15393 } 15394 } else if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15395 /* 15396 * If there isn't already a timer running 15397 * start one. Use a 4 second 15398 * timer as a fallback since it can't fail. 15399 */ 15400 tcp->tcp_timeout = B_TRUE; 15401 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15402 MSEC_TO_TICK(4000)); 15403 } 15404 } 15405 } 15406 15407 /* 15408 * The read side service routine is called mostly when we get back-enabled as a 15409 * result of flow control relief. Since we don't actually queue anything in 15410 * TCP, we have no data to send out of here. What we do is clear the receive 15411 * window, and send out a window update. 15412 * This routine is also called to drive an orderly release message upstream 15413 * if the attempt in tcp_rput failed. 15414 */ 15415 static void 15416 tcp_rsrv(queue_t *q) 15417 { 15418 conn_t *connp = Q_TO_CONN(q); 15419 tcp_t *tcp = connp->conn_tcp; 15420 mblk_t *mp; 15421 15422 /* No code does a putq on the read side */ 15423 ASSERT(q->q_first == NULL); 15424 15425 /* Nothing to do for the default queue */ 15426 if (q == tcp_g_q) { 15427 return; 15428 } 15429 15430 mp = allocb(0, BPRI_HI); 15431 if (mp == NULL) { 15432 /* 15433 * We are under memory pressure. Return for now and we 15434 * we will be called again later. 15435 */ 15436 if (!tcp->tcp_timeout && tcp->tcp_ordrelid == 0) { 15437 /* 15438 * If there isn't already a timer running 15439 * start one. Use a 4 second 15440 * timer as a fallback since it can't fail. 15441 */ 15442 tcp->tcp_timeout = B_TRUE; 15443 tcp->tcp_ordrelid = TCP_TIMER(tcp, tcp_ordrel_kick, 15444 MSEC_TO_TICK(4000)); 15445 } 15446 return; 15447 } 15448 CONN_INC_REF(connp); 15449 squeue_enter(connp->conn_sqp, mp, tcp_rsrv_input, connp, 15450 SQTAG_TCP_RSRV); 15451 } 15452 15453 /* 15454 * tcp_rwnd_set() is called to adjust the receive window to a desired value. 15455 * We do not allow the receive window to shrink. After setting rwnd, 15456 * set the flow control hiwat of the stream. 15457 * 15458 * This function is called in 2 cases: 15459 * 15460 * 1) Before data transfer begins, in tcp_accept_comm() for accepting a 15461 * connection (passive open) and in tcp_rput_data() for active connect. 15462 * This is called after tcp_mss_set() when the desired MSS value is known. 15463 * This makes sure that our window size is a mutiple of the other side's 15464 * MSS. 15465 * 2) Handling SO_RCVBUF option. 15466 * 15467 * It is ASSUMED that the requested size is a multiple of the current MSS. 15468 * 15469 * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the 15470 * user requests so. 15471 */ 15472 static int 15473 tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd) 15474 { 15475 uint32_t mss = tcp->tcp_mss; 15476 uint32_t old_max_rwnd; 15477 uint32_t max_transmittable_rwnd; 15478 boolean_t tcp_detached = TCP_IS_DETACHED(tcp); 15479 15480 if (tcp->tcp_fused) { 15481 size_t sth_hiwat; 15482 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 15483 15484 ASSERT(peer_tcp != NULL); 15485 /* 15486 * Record the stream head's high water mark for 15487 * this endpoint; this is used for flow-control 15488 * purposes in tcp_fuse_output(). 15489 */ 15490 sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd); 15491 if (!tcp_detached) 15492 (void) mi_set_sth_hiwat(tcp->tcp_rq, sth_hiwat); 15493 15494 /* 15495 * In the fusion case, the maxpsz stream head value of 15496 * our peer is set according to its send buffer size 15497 * and our receive buffer size; since the latter may 15498 * have changed we need to update the peer's maxpsz. 15499 */ 15500 (void) tcp_maxpsz_set(peer_tcp, B_TRUE); 15501 return (rwnd); 15502 } 15503 15504 if (tcp_detached) 15505 old_max_rwnd = tcp->tcp_rwnd; 15506 else 15507 old_max_rwnd = tcp->tcp_rq->q_hiwat; 15508 15509 /* 15510 * Insist on a receive window that is at least 15511 * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid 15512 * funny TCP interactions of Nagle algorithm, SWS avoidance 15513 * and delayed acknowledgement. 15514 */ 15515 rwnd = MAX(rwnd, tcp_recv_hiwat_minmss * mss); 15516 15517 /* 15518 * If window size info has already been exchanged, TCP should not 15519 * shrink the window. Shrinking window is doable if done carefully. 15520 * We may add that support later. But so far there is not a real 15521 * need to do that. 15522 */ 15523 if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) { 15524 /* MSS may have changed, do a round up again. */ 15525 rwnd = MSS_ROUNDUP(old_max_rwnd, mss); 15526 } 15527 15528 /* 15529 * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check 15530 * can be applied even before the window scale option is decided. 15531 */ 15532 max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws; 15533 if (rwnd > max_transmittable_rwnd) { 15534 rwnd = max_transmittable_rwnd - 15535 (max_transmittable_rwnd % mss); 15536 if (rwnd < mss) 15537 rwnd = max_transmittable_rwnd; 15538 /* 15539 * If we're over the limit we may have to back down tcp_rwnd. 15540 * The increment below won't work for us. So we set all three 15541 * here and the increment below will have no effect. 15542 */ 15543 tcp->tcp_rwnd = old_max_rwnd = rwnd; 15544 } 15545 if (tcp->tcp_localnet) { 15546 tcp->tcp_rack_abs_max = 15547 MIN(tcp_local_dacks_max, rwnd / mss / 2); 15548 } else { 15549 /* 15550 * For a remote host on a different subnet (through a router), 15551 * we ack every other packet to be conforming to RFC1122. 15552 * tcp_deferred_acks_max is default to 2. 15553 */ 15554 tcp->tcp_rack_abs_max = 15555 MIN(tcp_deferred_acks_max, rwnd / mss / 2); 15556 } 15557 if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max) 15558 tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max; 15559 else 15560 tcp->tcp_rack_cur_max = 0; 15561 /* 15562 * Increment the current rwnd by the amount the maximum grew (we 15563 * can not overwrite it since we might be in the middle of a 15564 * connection.) 15565 */ 15566 tcp->tcp_rwnd += rwnd - old_max_rwnd; 15567 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, tcp->tcp_tcph->th_win); 15568 if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max) 15569 tcp->tcp_cwnd_max = rwnd; 15570 15571 if (tcp_detached) 15572 return (rwnd); 15573 /* 15574 * We set the maximum receive window into rq->q_hiwat. 15575 * This is not actually used for flow control. 15576 */ 15577 tcp->tcp_rq->q_hiwat = rwnd; 15578 /* 15579 * Set the Stream head high water mark. This doesn't have to be 15580 * here, since we are simply using default values, but we would 15581 * prefer to choose these values algorithmically, with a likely 15582 * relationship to rwnd. 15583 */ 15584 (void) mi_set_sth_hiwat(tcp->tcp_rq, MAX(rwnd, tcp_sth_rcv_hiwat)); 15585 return (rwnd); 15586 } 15587 15588 /* 15589 * Return SNMP stuff in buffer in mpdata. 15590 */ 15591 int 15592 tcp_snmp_get(queue_t *q, mblk_t *mpctl) 15593 { 15594 mblk_t *mpdata; 15595 mblk_t *mp_conn_ctl = NULL; 15596 mblk_t *mp_conn_data; 15597 mblk_t *mp6_conn_ctl = NULL; 15598 mblk_t *mp6_conn_data; 15599 mblk_t *mp_conn_tail = NULL; 15600 mblk_t *mp6_conn_tail = NULL; 15601 struct opthdr *optp; 15602 mib2_tcpConnEntry_t tce; 15603 mib2_tcp6ConnEntry_t tce6; 15604 connf_t *connfp; 15605 conn_t *connp; 15606 int i; 15607 boolean_t ispriv; 15608 zoneid_t zoneid; 15609 15610 if (mpctl == NULL || 15611 (mpdata = mpctl->b_cont) == NULL || 15612 (mp_conn_ctl = copymsg(mpctl)) == NULL || 15613 (mp6_conn_ctl = copymsg(mpctl)) == NULL) { 15614 if (mp_conn_ctl != NULL) 15615 freemsg(mp_conn_ctl); 15616 if (mp6_conn_ctl != NULL) 15617 freemsg(mp6_conn_ctl); 15618 return (0); 15619 } 15620 15621 /* build table of connections -- need count in fixed part */ 15622 mp_conn_data = mp_conn_ctl->b_cont; 15623 mp6_conn_data = mp6_conn_ctl->b_cont; 15624 SET_MIB(tcp_mib.tcpRtoAlgorithm, 4); /* vanj */ 15625 SET_MIB(tcp_mib.tcpRtoMin, tcp_rexmit_interval_min); 15626 SET_MIB(tcp_mib.tcpRtoMax, tcp_rexmit_interval_max); 15627 SET_MIB(tcp_mib.tcpMaxConn, -1); 15628 SET_MIB(tcp_mib.tcpCurrEstab, 0); 15629 15630 ispriv = 15631 secpolicy_net_config((Q_TO_CONN(q))->conn_cred, B_TRUE) == 0; 15632 zoneid = Q_TO_CONN(q)->conn_zoneid; 15633 15634 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 15635 15636 connfp = &ipcl_globalhash_fanout[i]; 15637 15638 connp = NULL; 15639 15640 while ((connp = 15641 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 15642 tcp_t *tcp; 15643 15644 if (connp->conn_zoneid != zoneid) 15645 continue; /* not in this zone */ 15646 15647 tcp = connp->conn_tcp; 15648 UPDATE_MIB(&tcp_mib, tcpInSegs, tcp->tcp_ibsegs); 15649 tcp->tcp_ibsegs = 0; 15650 UPDATE_MIB(&tcp_mib, tcpOutSegs, tcp->tcp_obsegs); 15651 tcp->tcp_obsegs = 0; 15652 15653 tce6.tcp6ConnState = tce.tcpConnState = 15654 tcp_snmp_state(tcp); 15655 if (tce.tcpConnState == MIB2_TCP_established || 15656 tce.tcpConnState == MIB2_TCP_closeWait) 15657 BUMP_MIB(&tcp_mib, tcpCurrEstab); 15658 15659 /* Create a message to report on IPv6 entries */ 15660 if (tcp->tcp_ipversion == IPV6_VERSION) { 15661 tce6.tcp6ConnLocalAddress = tcp->tcp_ip_src_v6; 15662 tce6.tcp6ConnRemAddress = tcp->tcp_remote_v6; 15663 tce6.tcp6ConnLocalPort = ntohs(tcp->tcp_lport); 15664 tce6.tcp6ConnRemPort = ntohs(tcp->tcp_fport); 15665 tce6.tcp6ConnIfIndex = tcp->tcp_bound_if; 15666 /* Don't want just anybody seeing these... */ 15667 if (ispriv) { 15668 tce6.tcp6ConnEntryInfo.ce_snxt = 15669 tcp->tcp_snxt; 15670 tce6.tcp6ConnEntryInfo.ce_suna = 15671 tcp->tcp_suna; 15672 tce6.tcp6ConnEntryInfo.ce_rnxt = 15673 tcp->tcp_rnxt; 15674 tce6.tcp6ConnEntryInfo.ce_rack = 15675 tcp->tcp_rack; 15676 } else { 15677 /* 15678 * Netstat, unfortunately, uses this to 15679 * get send/receive queue sizes. How to fix? 15680 * Why not compute the difference only? 15681 */ 15682 tce6.tcp6ConnEntryInfo.ce_snxt = 15683 tcp->tcp_snxt - tcp->tcp_suna; 15684 tce6.tcp6ConnEntryInfo.ce_suna = 0; 15685 tce6.tcp6ConnEntryInfo.ce_rnxt = 15686 tcp->tcp_rnxt - tcp->tcp_rack; 15687 tce6.tcp6ConnEntryInfo.ce_rack = 0; 15688 } 15689 15690 tce6.tcp6ConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15691 tce6.tcp6ConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15692 tce6.tcp6ConnEntryInfo.ce_rto = tcp->tcp_rto; 15693 tce6.tcp6ConnEntryInfo.ce_mss = tcp->tcp_mss; 15694 tce6.tcp6ConnEntryInfo.ce_state = tcp->tcp_state; 15695 (void) snmp_append_data2(mp6_conn_data, &mp6_conn_tail, 15696 (char *)&tce6, sizeof (tce6)); 15697 } 15698 /* 15699 * Create an IPv4 table entry for IPv4 entries and also 15700 * for IPv6 entries which are bound to in6addr_any 15701 * but don't have IPV6_V6ONLY set. 15702 * (i.e. anything an IPv4 peer could connect to) 15703 */ 15704 if (tcp->tcp_ipversion == IPV4_VERSION || 15705 (tcp->tcp_state <= TCPS_LISTEN && 15706 !tcp->tcp_connp->conn_ipv6_v6only && 15707 IN6_IS_ADDR_UNSPECIFIED(&tcp->tcp_ip_src_v6))) { 15708 if (tcp->tcp_ipversion == IPV6_VERSION) { 15709 tce.tcpConnRemAddress = INADDR_ANY; 15710 tce.tcpConnLocalAddress = INADDR_ANY; 15711 } else { 15712 tce.tcpConnRemAddress = 15713 tcp->tcp_remote; 15714 tce.tcpConnLocalAddress = 15715 tcp->tcp_ip_src; 15716 } 15717 tce.tcpConnLocalPort = ntohs(tcp->tcp_lport); 15718 tce.tcpConnRemPort = ntohs(tcp->tcp_fport); 15719 /* Don't want just anybody seeing these... */ 15720 if (ispriv) { 15721 tce.tcpConnEntryInfo.ce_snxt = 15722 tcp->tcp_snxt; 15723 tce.tcpConnEntryInfo.ce_suna = 15724 tcp->tcp_suna; 15725 tce.tcpConnEntryInfo.ce_rnxt = 15726 tcp->tcp_rnxt; 15727 tce.tcpConnEntryInfo.ce_rack = 15728 tcp->tcp_rack; 15729 } else { 15730 /* 15731 * Netstat, unfortunately, uses this to 15732 * get send/receive queue sizes. How 15733 * to fix? 15734 * Why not compute the difference only? 15735 */ 15736 tce.tcpConnEntryInfo.ce_snxt = 15737 tcp->tcp_snxt - tcp->tcp_suna; 15738 tce.tcpConnEntryInfo.ce_suna = 0; 15739 tce.tcpConnEntryInfo.ce_rnxt = 15740 tcp->tcp_rnxt - tcp->tcp_rack; 15741 tce.tcpConnEntryInfo.ce_rack = 0; 15742 } 15743 15744 tce.tcpConnEntryInfo.ce_swnd = tcp->tcp_swnd; 15745 tce.tcpConnEntryInfo.ce_rwnd = tcp->tcp_rwnd; 15746 tce.tcpConnEntryInfo.ce_rto = tcp->tcp_rto; 15747 tce.tcpConnEntryInfo.ce_mss = tcp->tcp_mss; 15748 tce.tcpConnEntryInfo.ce_state = 15749 tcp->tcp_state; 15750 (void) snmp_append_data2(mp_conn_data, 15751 &mp_conn_tail, (char *)&tce, sizeof (tce)); 15752 } 15753 } 15754 } 15755 15756 /* fixed length structure for IPv4 and IPv6 counters */ 15757 SET_MIB(tcp_mib.tcpConnTableSize, sizeof (mib2_tcpConnEntry_t)); 15758 SET_MIB(tcp_mib.tcp6ConnTableSize, sizeof (mib2_tcp6ConnEntry_t)); 15759 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)]; 15760 optp->level = MIB2_TCP; 15761 optp->name = 0; 15762 (void) snmp_append_data(mpdata, (char *)&tcp_mib, sizeof (tcp_mib)); 15763 optp->len = msgdsize(mpdata); 15764 qreply(q, mpctl); 15765 15766 /* table of connections... */ 15767 optp = (struct opthdr *)&mp_conn_ctl->b_rptr[ 15768 sizeof (struct T_optmgmt_ack)]; 15769 optp->level = MIB2_TCP; 15770 optp->name = MIB2_TCP_CONN; 15771 optp->len = msgdsize(mp_conn_data); 15772 qreply(q, mp_conn_ctl); 15773 15774 /* table of IPv6 connections... */ 15775 optp = (struct opthdr *)&mp6_conn_ctl->b_rptr[ 15776 sizeof (struct T_optmgmt_ack)]; 15777 optp->level = MIB2_TCP6; 15778 optp->name = MIB2_TCP6_CONN; 15779 optp->len = msgdsize(mp6_conn_data); 15780 qreply(q, mp6_conn_ctl); 15781 return (1); 15782 } 15783 15784 /* Return 0 if invalid set request, 1 otherwise, including non-tcp requests */ 15785 /* ARGSUSED */ 15786 int 15787 tcp_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len) 15788 { 15789 mib2_tcpConnEntry_t *tce = (mib2_tcpConnEntry_t *)ptr; 15790 15791 switch (level) { 15792 case MIB2_TCP: 15793 switch (name) { 15794 case 13: 15795 if (tce->tcpConnState != MIB2_TCP_deleteTCB) 15796 return (0); 15797 /* TODO: delete entry defined by tce */ 15798 return (1); 15799 default: 15800 return (0); 15801 } 15802 default: 15803 return (1); 15804 } 15805 } 15806 15807 /* Translate TCP state to MIB2 TCP state. */ 15808 static int 15809 tcp_snmp_state(tcp_t *tcp) 15810 { 15811 if (tcp == NULL) 15812 return (0); 15813 15814 switch (tcp->tcp_state) { 15815 case TCPS_CLOSED: 15816 case TCPS_IDLE: /* RFC1213 doesn't have analogue for IDLE & BOUND */ 15817 case TCPS_BOUND: 15818 return (MIB2_TCP_closed); 15819 case TCPS_LISTEN: 15820 return (MIB2_TCP_listen); 15821 case TCPS_SYN_SENT: 15822 return (MIB2_TCP_synSent); 15823 case TCPS_SYN_RCVD: 15824 return (MIB2_TCP_synReceived); 15825 case TCPS_ESTABLISHED: 15826 return (MIB2_TCP_established); 15827 case TCPS_CLOSE_WAIT: 15828 return (MIB2_TCP_closeWait); 15829 case TCPS_FIN_WAIT_1: 15830 return (MIB2_TCP_finWait1); 15831 case TCPS_CLOSING: 15832 return (MIB2_TCP_closing); 15833 case TCPS_LAST_ACK: 15834 return (MIB2_TCP_lastAck); 15835 case TCPS_FIN_WAIT_2: 15836 return (MIB2_TCP_finWait2); 15837 case TCPS_TIME_WAIT: 15838 return (MIB2_TCP_timeWait); 15839 default: 15840 return (0); 15841 } 15842 } 15843 15844 static char tcp_report_header[] = 15845 "TCP " MI_COL_HDRPAD_STR 15846 "zone dest snxt suna " 15847 "swnd rnxt rack rwnd rto mss w sw rw t " 15848 "recent [lport,fport] state"; 15849 15850 /* 15851 * TCP status report triggered via the Named Dispatch mechanism. 15852 */ 15853 /* ARGSUSED */ 15854 static void 15855 tcp_report_item(mblk_t *mp, tcp_t *tcp, int hashval, tcp_t *thisstream, 15856 cred_t *cr) 15857 { 15858 char hash[10], addrbuf[INET6_ADDRSTRLEN]; 15859 boolean_t ispriv = secpolicy_net_config(cr, B_TRUE) == 0; 15860 char cflag; 15861 in6_addr_t v6dst; 15862 char buf[80]; 15863 uint_t print_len, buf_len; 15864 15865 buf_len = mp->b_datap->db_lim - mp->b_wptr; 15866 if (buf_len <= 0) 15867 return; 15868 15869 if (hashval >= 0) 15870 (void) sprintf(hash, "%03d ", hashval); 15871 else 15872 hash[0] = '\0'; 15873 15874 /* 15875 * Note that we use the remote address in the tcp_b structure. 15876 * This means that it will print out the real destination address, 15877 * not the next hop's address if source routing is used. This 15878 * avoid the confusion on the output because user may not 15879 * know that source routing is used for a connection. 15880 */ 15881 if (tcp->tcp_ipversion == IPV4_VERSION) { 15882 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_remote, &v6dst); 15883 } else { 15884 v6dst = tcp->tcp_remote_v6; 15885 } 15886 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 15887 /* 15888 * the ispriv checks are so that normal users cannot determine 15889 * sequence number information using NDD. 15890 */ 15891 15892 if (TCP_IS_DETACHED(tcp)) 15893 cflag = '*'; 15894 else 15895 cflag = ' '; 15896 print_len = snprintf((char *)mp->b_wptr, buf_len, 15897 "%s " MI_COL_PTRFMT_STR "%d %s %08x %08x %010d %08x %08x " 15898 "%010d %05ld %05d %1d %02d %02d %1d %08x %s%c\n", 15899 hash, 15900 (void *)tcp, 15901 tcp->tcp_connp->conn_zoneid, 15902 addrbuf, 15903 (ispriv) ? tcp->tcp_snxt : 0, 15904 (ispriv) ? tcp->tcp_suna : 0, 15905 tcp->tcp_swnd, 15906 (ispriv) ? tcp->tcp_rnxt : 0, 15907 (ispriv) ? tcp->tcp_rack : 0, 15908 tcp->tcp_rwnd, 15909 tcp->tcp_rto, 15910 tcp->tcp_mss, 15911 tcp->tcp_snd_ws_ok, 15912 tcp->tcp_snd_ws, 15913 tcp->tcp_rcv_ws, 15914 tcp->tcp_snd_ts_ok, 15915 tcp->tcp_ts_recent, 15916 tcp_display(tcp, buf, DISP_PORT_ONLY), cflag); 15917 if (print_len < buf_len) { 15918 ((mblk_t *)mp)->b_wptr += print_len; 15919 } else { 15920 ((mblk_t *)mp)->b_wptr += buf_len; 15921 } 15922 } 15923 15924 /* 15925 * TCP status report (for listeners only) triggered via the Named Dispatch 15926 * mechanism. 15927 */ 15928 /* ARGSUSED */ 15929 static void 15930 tcp_report_listener(mblk_t *mp, tcp_t *tcp, int hashval) 15931 { 15932 char addrbuf[INET6_ADDRSTRLEN]; 15933 in6_addr_t v6dst; 15934 uint_t print_len, buf_len; 15935 15936 buf_len = mp->b_datap->db_lim - mp->b_wptr; 15937 if (buf_len <= 0) 15938 return; 15939 15940 if (tcp->tcp_ipversion == IPV4_VERSION) { 15941 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, &v6dst); 15942 (void) inet_ntop(AF_INET6, &v6dst, addrbuf, sizeof (addrbuf)); 15943 } else { 15944 (void) inet_ntop(AF_INET6, &tcp->tcp_ip6h->ip6_src, 15945 addrbuf, sizeof (addrbuf)); 15946 } 15947 print_len = snprintf((char *)mp->b_wptr, buf_len, 15948 "%03d " 15949 MI_COL_PTRFMT_STR 15950 "%d %s %05u %08u %d/%d/%d%c\n", 15951 hashval, (void *)tcp, 15952 tcp->tcp_connp->conn_zoneid, 15953 addrbuf, 15954 (uint_t)BE16_TO_U16(tcp->tcp_tcph->th_lport), 15955 tcp->tcp_conn_req_seqnum, 15956 tcp->tcp_conn_req_cnt_q0, tcp->tcp_conn_req_cnt_q, 15957 tcp->tcp_conn_req_max, 15958 tcp->tcp_syn_defense ? '*' : ' '); 15959 if (print_len < buf_len) { 15960 ((mblk_t *)mp)->b_wptr += print_len; 15961 } else { 15962 ((mblk_t *)mp)->b_wptr += buf_len; 15963 } 15964 } 15965 15966 /* TCP status report triggered via the Named Dispatch mechanism. */ 15967 /* ARGSUSED */ 15968 static int 15969 tcp_status_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 15970 { 15971 tcp_t *tcp; 15972 int i; 15973 conn_t *connp; 15974 connf_t *connfp; 15975 zoneid_t zoneid; 15976 15977 /* 15978 * Because of the ndd constraint, at most we can have 64K buffer 15979 * to put in all TCP info. So to be more efficient, just 15980 * allocate a 64K buffer here, assuming we need that large buffer. 15981 * This may be a problem as any user can read tcp_status. Therefore 15982 * we limit the rate of doing this using tcp_ndd_get_info_interval. 15983 * This should be OK as normal users should not do this too often. 15984 */ 15985 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 15986 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 15987 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 15988 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 15989 return (0); 15990 } 15991 } 15992 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 15993 /* The following may work even if we cannot get a large buf. */ 15994 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 15995 return (0); 15996 } 15997 15998 (void) mi_mpprintf(mp, "%s", tcp_report_header); 15999 16000 zoneid = Q_TO_CONN(q)->conn_zoneid; 16001 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 16002 16003 connfp = &ipcl_globalhash_fanout[i]; 16004 16005 connp = NULL; 16006 16007 while ((connp = 16008 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16009 tcp = connp->conn_tcp; 16010 if (zoneid != GLOBAL_ZONEID && 16011 zoneid != connp->conn_zoneid) 16012 continue; 16013 tcp_report_item(mp->b_cont, tcp, -1, tcp, 16014 cr); 16015 } 16016 16017 } 16018 16019 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16020 return (0); 16021 } 16022 16023 /* TCP status report triggered via the Named Dispatch mechanism. */ 16024 /* ARGSUSED */ 16025 static int 16026 tcp_bind_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16027 { 16028 tf_t *tbf; 16029 tcp_t *tcp; 16030 int i; 16031 zoneid_t zoneid; 16032 16033 /* Refer to comments in tcp_status_report(). */ 16034 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16035 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16036 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16037 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16038 return (0); 16039 } 16040 } 16041 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16042 /* The following may work even if we cannot get a large buf. */ 16043 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16044 return (0); 16045 } 16046 16047 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16048 16049 zoneid = Q_TO_CONN(q)->conn_zoneid; 16050 16051 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 16052 tbf = &tcp_bind_fanout[i]; 16053 mutex_enter(&tbf->tf_lock); 16054 for (tcp = tbf->tf_tcp; tcp != NULL; 16055 tcp = tcp->tcp_bind_hash) { 16056 if (zoneid != GLOBAL_ZONEID && 16057 zoneid != tcp->tcp_connp->conn_zoneid) 16058 continue; 16059 CONN_INC_REF(tcp->tcp_connp); 16060 tcp_report_item(mp->b_cont, tcp, i, 16061 Q_TO_TCP(q), cr); 16062 CONN_DEC_REF(tcp->tcp_connp); 16063 } 16064 mutex_exit(&tbf->tf_lock); 16065 } 16066 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16067 return (0); 16068 } 16069 16070 /* TCP status report triggered via the Named Dispatch mechanism. */ 16071 /* ARGSUSED */ 16072 static int 16073 tcp_listen_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16074 { 16075 connf_t *connfp; 16076 conn_t *connp; 16077 tcp_t *tcp; 16078 int i; 16079 zoneid_t zoneid; 16080 16081 /* Refer to comments in tcp_status_report(). */ 16082 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16083 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16084 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16085 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16086 return (0); 16087 } 16088 } 16089 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16090 /* The following may work even if we cannot get a large buf. */ 16091 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16092 return (0); 16093 } 16094 16095 (void) mi_mpprintf(mp, 16096 " TCP " MI_COL_HDRPAD_STR 16097 "zone IP addr port seqnum backlog (q0/q/max)"); 16098 16099 zoneid = Q_TO_CONN(q)->conn_zoneid; 16100 16101 for (i = 0; i < ipcl_bind_fanout_size; i++) { 16102 connfp = &ipcl_bind_fanout[i]; 16103 connp = NULL; 16104 while ((connp = 16105 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16106 tcp = connp->conn_tcp; 16107 if (zoneid != GLOBAL_ZONEID && 16108 zoneid != connp->conn_zoneid) 16109 continue; 16110 tcp_report_listener(mp->b_cont, tcp, i); 16111 } 16112 } 16113 16114 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16115 return (0); 16116 } 16117 16118 /* TCP status report triggered via the Named Dispatch mechanism. */ 16119 /* ARGSUSED */ 16120 static int 16121 tcp_conn_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16122 { 16123 connf_t *connfp; 16124 conn_t *connp; 16125 tcp_t *tcp; 16126 int i; 16127 zoneid_t zoneid; 16128 16129 /* Refer to comments in tcp_status_report(). */ 16130 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16131 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16132 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16133 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16134 return (0); 16135 } 16136 } 16137 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16138 /* The following may work even if we cannot get a large buf. */ 16139 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16140 return (0); 16141 } 16142 16143 (void) mi_mpprintf(mp, "tcp_conn_hash_size = %d", 16144 ipcl_conn_fanout_size); 16145 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16146 16147 zoneid = Q_TO_CONN(q)->conn_zoneid; 16148 16149 for (i = 0; i < ipcl_conn_fanout_size; i++) { 16150 connfp = &ipcl_conn_fanout[i]; 16151 connp = NULL; 16152 while ((connp = 16153 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 16154 tcp = connp->conn_tcp; 16155 if (zoneid != GLOBAL_ZONEID && 16156 zoneid != connp->conn_zoneid) 16157 continue; 16158 tcp_report_item(mp->b_cont, tcp, i, 16159 Q_TO_TCP(q), cr); 16160 } 16161 } 16162 16163 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16164 return (0); 16165 } 16166 16167 /* TCP status report triggered via the Named Dispatch mechanism. */ 16168 /* ARGSUSED */ 16169 static int 16170 tcp_acceptor_hash_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 16171 { 16172 tf_t *tf; 16173 tcp_t *tcp; 16174 int i; 16175 zoneid_t zoneid; 16176 16177 /* Refer to comments in tcp_status_report(). */ 16178 if (cr == NULL || secpolicy_net_config(cr, B_TRUE) != 0) { 16179 if (ddi_get_lbolt() - tcp_last_ndd_get_info_time < 16180 drv_usectohz(tcp_ndd_get_info_interval * 1000)) { 16181 (void) mi_mpprintf(mp, NDD_TOO_QUICK_MSG); 16182 return (0); 16183 } 16184 } 16185 if ((mp->b_cont = allocb(ND_MAX_BUF_LEN, BPRI_HI)) == NULL) { 16186 /* The following may work even if we cannot get a large buf. */ 16187 (void) mi_mpprintf(mp, NDD_OUT_OF_BUF_MSG); 16188 return (0); 16189 } 16190 16191 (void) mi_mpprintf(mp, " %s", tcp_report_header); 16192 16193 zoneid = Q_TO_CONN(q)->conn_zoneid; 16194 16195 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 16196 tf = &tcp_acceptor_fanout[i]; 16197 mutex_enter(&tf->tf_lock); 16198 for (tcp = tf->tf_tcp; tcp != NULL; 16199 tcp = tcp->tcp_acceptor_hash) { 16200 if (zoneid != GLOBAL_ZONEID && 16201 zoneid != tcp->tcp_connp->conn_zoneid) 16202 continue; 16203 tcp_report_item(mp->b_cont, tcp, i, 16204 Q_TO_TCP(q), cr); 16205 } 16206 mutex_exit(&tf->tf_lock); 16207 } 16208 tcp_last_ndd_get_info_time = ddi_get_lbolt(); 16209 return (0); 16210 } 16211 16212 /* 16213 * tcp_timer is the timer service routine. It handles the retransmission, 16214 * FIN_WAIT_2 flush, and zero window probe timeout events. It figures out 16215 * from the state of the tcp instance what kind of action needs to be done 16216 * at the time it is called. 16217 */ 16218 static void 16219 tcp_timer(void *arg) 16220 { 16221 mblk_t *mp; 16222 clock_t first_threshold; 16223 clock_t second_threshold; 16224 clock_t ms; 16225 uint32_t mss; 16226 conn_t *connp = (conn_t *)arg; 16227 tcp_t *tcp = connp->conn_tcp; 16228 16229 tcp->tcp_timer_tid = 0; 16230 16231 if (tcp->tcp_fused) 16232 return; 16233 16234 first_threshold = tcp->tcp_first_timer_threshold; 16235 second_threshold = tcp->tcp_second_timer_threshold; 16236 switch (tcp->tcp_state) { 16237 case TCPS_IDLE: 16238 case TCPS_BOUND: 16239 case TCPS_LISTEN: 16240 return; 16241 case TCPS_SYN_RCVD: { 16242 tcp_t *listener = tcp->tcp_listener; 16243 16244 if (tcp->tcp_syn_rcvd_timeout == 0 && (listener != NULL)) { 16245 ASSERT(tcp->tcp_rq == listener->tcp_rq); 16246 /* it's our first timeout */ 16247 tcp->tcp_syn_rcvd_timeout = 1; 16248 mutex_enter(&listener->tcp_eager_lock); 16249 listener->tcp_syn_rcvd_timeout++; 16250 if (!listener->tcp_syn_defense && 16251 (listener->tcp_syn_rcvd_timeout > 16252 (tcp_conn_req_max_q0 >> 2)) && 16253 (tcp_conn_req_max_q0 > 200)) { 16254 /* We may be under attack. Put on a defense. */ 16255 listener->tcp_syn_defense = B_TRUE; 16256 cmn_err(CE_WARN, "High TCP connect timeout " 16257 "rate! System (port %d) may be under a " 16258 "SYN flood attack!", 16259 BE16_TO_U16(listener->tcp_tcph->th_lport)); 16260 16261 listener->tcp_ip_addr_cache = kmem_zalloc( 16262 IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t), 16263 KM_NOSLEEP); 16264 } 16265 mutex_exit(&listener->tcp_eager_lock); 16266 } 16267 } 16268 /* FALLTHRU */ 16269 case TCPS_SYN_SENT: 16270 first_threshold = tcp->tcp_first_ctimer_threshold; 16271 second_threshold = tcp->tcp_second_ctimer_threshold; 16272 break; 16273 case TCPS_ESTABLISHED: 16274 case TCPS_FIN_WAIT_1: 16275 case TCPS_CLOSING: 16276 case TCPS_CLOSE_WAIT: 16277 case TCPS_LAST_ACK: 16278 /* If we have data to rexmit */ 16279 if (tcp->tcp_suna != tcp->tcp_snxt) { 16280 clock_t time_to_wait; 16281 16282 BUMP_MIB(&tcp_mib, tcpTimRetrans); 16283 if (!tcp->tcp_xmit_head) 16284 break; 16285 time_to_wait = lbolt - 16286 (clock_t)tcp->tcp_xmit_head->b_prev; 16287 time_to_wait = tcp->tcp_rto - 16288 TICK_TO_MSEC(time_to_wait); 16289 /* 16290 * If the timer fires too early, 1 clock tick earlier, 16291 * restart the timer. 16292 */ 16293 if (time_to_wait > msec_per_tick) { 16294 TCP_STAT(tcp_timer_fire_early); 16295 TCP_TIMER_RESTART(tcp, time_to_wait); 16296 return; 16297 } 16298 /* 16299 * When we probe zero windows, we force the swnd open. 16300 * If our peer acks with a closed window swnd will be 16301 * set to zero by tcp_rput(). As long as we are 16302 * receiving acks tcp_rput will 16303 * reset 'tcp_ms_we_have_waited' so as not to trip the 16304 * first and second interval actions. NOTE: the timer 16305 * interval is allowed to continue its exponential 16306 * backoff. 16307 */ 16308 if (tcp->tcp_swnd == 0 || tcp->tcp_zero_win_probe) { 16309 if (tcp->tcp_debug) { 16310 (void) strlog(TCP_MOD_ID, 0, 1, 16311 SL_TRACE, "tcp_timer: zero win"); 16312 } 16313 } else { 16314 /* 16315 * After retransmission, we need to do 16316 * slow start. Set the ssthresh to one 16317 * half of current effective window and 16318 * cwnd to one MSS. Also reset 16319 * tcp_cwnd_cnt. 16320 * 16321 * Note that if tcp_ssthresh is reduced because 16322 * of ECN, do not reduce it again unless it is 16323 * already one window of data away (tcp_cwr 16324 * should then be cleared) or this is a 16325 * timeout for a retransmitted segment. 16326 */ 16327 uint32_t npkt; 16328 16329 if (!tcp->tcp_cwr || tcp->tcp_rexmit) { 16330 npkt = ((tcp->tcp_timer_backoff ? 16331 tcp->tcp_cwnd_ssthresh : 16332 tcp->tcp_snxt - 16333 tcp->tcp_suna) >> 1) / tcp->tcp_mss; 16334 tcp->tcp_cwnd_ssthresh = MAX(npkt, 2) * 16335 tcp->tcp_mss; 16336 } 16337 tcp->tcp_cwnd = tcp->tcp_mss; 16338 tcp->tcp_cwnd_cnt = 0; 16339 if (tcp->tcp_ecn_ok) { 16340 tcp->tcp_cwr = B_TRUE; 16341 tcp->tcp_cwr_snd_max = tcp->tcp_snxt; 16342 tcp->tcp_ecn_cwr_sent = B_FALSE; 16343 } 16344 } 16345 break; 16346 } 16347 /* 16348 * We have something to send yet we cannot send. The 16349 * reason can be: 16350 * 16351 * 1. Zero send window: we need to do zero window probe. 16352 * 2. Zero cwnd: because of ECN, we need to "clock out 16353 * segments. 16354 * 3. SWS avoidance: receiver may have shrunk window, 16355 * reset our knowledge. 16356 * 16357 * Note that condition 2 can happen with either 1 or 16358 * 3. But 1 and 3 are exclusive. 16359 */ 16360 if (tcp->tcp_unsent != 0) { 16361 if (tcp->tcp_cwnd == 0) { 16362 /* 16363 * Set tcp_cwnd to 1 MSS so that a 16364 * new segment can be sent out. We 16365 * are "clocking out" new data when 16366 * the network is really congested. 16367 */ 16368 ASSERT(tcp->tcp_ecn_ok); 16369 tcp->tcp_cwnd = tcp->tcp_mss; 16370 } 16371 if (tcp->tcp_swnd == 0) { 16372 /* Extend window for zero window probe */ 16373 tcp->tcp_swnd++; 16374 tcp->tcp_zero_win_probe = B_TRUE; 16375 BUMP_MIB(&tcp_mib, tcpOutWinProbe); 16376 } else { 16377 /* 16378 * Handle timeout from sender SWS avoidance. 16379 * Reset our knowledge of the max send window 16380 * since the receiver might have reduced its 16381 * receive buffer. Avoid setting tcp_max_swnd 16382 * to one since that will essentially disable 16383 * the SWS checks. 16384 * 16385 * Note that since we don't have a SWS 16386 * state variable, if the timeout is set 16387 * for ECN but not for SWS, this 16388 * code will also be executed. This is 16389 * fine as tcp_max_swnd is updated 16390 * constantly and it will not affect 16391 * anything. 16392 */ 16393 tcp->tcp_max_swnd = MAX(tcp->tcp_swnd, 2); 16394 } 16395 tcp_wput_data(tcp, NULL, B_FALSE); 16396 return; 16397 } 16398 /* Is there a FIN that needs to be to re retransmitted? */ 16399 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16400 !tcp->tcp_fin_acked) 16401 break; 16402 /* Nothing to do, return without restarting timer. */ 16403 TCP_STAT(tcp_timer_fire_miss); 16404 return; 16405 case TCPS_FIN_WAIT_2: 16406 /* 16407 * User closed the TCP endpoint and peer ACK'ed our FIN. 16408 * We waited some time for for peer's FIN, but it hasn't 16409 * arrived. We flush the connection now to avoid 16410 * case where the peer has rebooted. 16411 */ 16412 if (TCP_IS_DETACHED(tcp)) { 16413 (void) tcp_clean_death(tcp, 0, 23); 16414 } else { 16415 TCP_TIMER_RESTART(tcp, tcp_fin_wait_2_flush_interval); 16416 } 16417 return; 16418 case TCPS_TIME_WAIT: 16419 (void) tcp_clean_death(tcp, 0, 24); 16420 return; 16421 default: 16422 if (tcp->tcp_debug) { 16423 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR, 16424 "tcp_timer: strange state (%d) %s", 16425 tcp->tcp_state, tcp_display(tcp, NULL, 16426 DISP_PORT_ONLY)); 16427 } 16428 return; 16429 } 16430 if ((ms = tcp->tcp_ms_we_have_waited) > second_threshold) { 16431 /* 16432 * For zero window probe, we need to send indefinitely, 16433 * unless we have not heard from the other side for some 16434 * time... 16435 */ 16436 if ((tcp->tcp_zero_win_probe == 0) || 16437 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) > 16438 second_threshold)) { 16439 BUMP_MIB(&tcp_mib, tcpTimRetransDrop); 16440 /* 16441 * If TCP is in SYN_RCVD state, send back a 16442 * RST|ACK as BSD does. Note that tcp_zero_win_probe 16443 * should be zero in TCPS_SYN_RCVD state. 16444 */ 16445 if (tcp->tcp_state == TCPS_SYN_RCVD) { 16446 tcp_xmit_ctl("tcp_timer: RST sent on timeout " 16447 "in SYN_RCVD", 16448 tcp, tcp->tcp_snxt, 16449 tcp->tcp_rnxt, TH_RST | TH_ACK); 16450 } 16451 (void) tcp_clean_death(tcp, 16452 tcp->tcp_client_errno ? 16453 tcp->tcp_client_errno : ETIMEDOUT, 25); 16454 return; 16455 } else { 16456 /* 16457 * Set tcp_ms_we_have_waited to second_threshold 16458 * so that in next timeout, we will do the above 16459 * check (lbolt - tcp_last_recv_time). This is 16460 * also to avoid overflow. 16461 * 16462 * We don't need to decrement tcp_timer_backoff 16463 * to avoid overflow because it will be decremented 16464 * later if new timeout value is greater than 16465 * tcp_rexmit_interval_max. In the case when 16466 * tcp_rexmit_interval_max is greater than 16467 * second_threshold, it means that we will wait 16468 * longer than second_threshold to send the next 16469 * window probe. 16470 */ 16471 tcp->tcp_ms_we_have_waited = second_threshold; 16472 } 16473 } else if (ms > first_threshold) { 16474 if (tcp->tcp_snd_zcopy_aware && (!tcp->tcp_xmit_zc_clean) && 16475 tcp->tcp_xmit_head != NULL) { 16476 tcp->tcp_xmit_head = 16477 tcp_zcopy_backoff(tcp, tcp->tcp_xmit_head, 1); 16478 } 16479 /* 16480 * We have been retransmitting for too long... The RTT 16481 * we calculated is probably incorrect. Reinitialize it. 16482 * Need to compensate for 0 tcp_rtt_sa. Reset 16483 * tcp_rtt_update so that we won't accidentally cache a 16484 * bad value. But only do this if this is not a zero 16485 * window probe. 16486 */ 16487 if (tcp->tcp_rtt_sa != 0 && tcp->tcp_zero_win_probe == 0) { 16488 tcp->tcp_rtt_sd += (tcp->tcp_rtt_sa >> 3) + 16489 (tcp->tcp_rtt_sa >> 5); 16490 tcp->tcp_rtt_sa = 0; 16491 tcp_ip_notify(tcp); 16492 tcp->tcp_rtt_update = 0; 16493 } 16494 } 16495 tcp->tcp_timer_backoff++; 16496 if ((ms = (tcp->tcp_rtt_sa >> 3) + tcp->tcp_rtt_sd + 16497 tcp_rexmit_interval_extra + (tcp->tcp_rtt_sa >> 5)) < 16498 tcp_rexmit_interval_min) { 16499 /* 16500 * This means the original RTO is tcp_rexmit_interval_min. 16501 * So we will use tcp_rexmit_interval_min as the RTO value 16502 * and do the backoff. 16503 */ 16504 ms = tcp_rexmit_interval_min << tcp->tcp_timer_backoff; 16505 } else { 16506 ms <<= tcp->tcp_timer_backoff; 16507 } 16508 if (ms > tcp_rexmit_interval_max) { 16509 ms = tcp_rexmit_interval_max; 16510 /* 16511 * ms is at max, decrement tcp_timer_backoff to avoid 16512 * overflow. 16513 */ 16514 tcp->tcp_timer_backoff--; 16515 } 16516 tcp->tcp_ms_we_have_waited += ms; 16517 if (tcp->tcp_zero_win_probe == 0) { 16518 tcp->tcp_rto = ms; 16519 } 16520 TCP_TIMER_RESTART(tcp, ms); 16521 /* 16522 * This is after a timeout and tcp_rto is backed off. Set 16523 * tcp_set_timer to 1 so that next time RTO is updated, we will 16524 * restart the timer with a correct value. 16525 */ 16526 tcp->tcp_set_timer = 1; 16527 mss = tcp->tcp_snxt - tcp->tcp_suna; 16528 if (mss > tcp->tcp_mss) 16529 mss = tcp->tcp_mss; 16530 if (mss > tcp->tcp_swnd && tcp->tcp_swnd != 0) 16531 mss = tcp->tcp_swnd; 16532 16533 if ((mp = tcp->tcp_xmit_head) != NULL) 16534 mp->b_prev = (mblk_t *)lbolt; 16535 mp = tcp_xmit_mp(tcp, mp, mss, NULL, NULL, tcp->tcp_suna, B_TRUE, &mss, 16536 B_TRUE); 16537 16538 /* 16539 * When slow start after retransmission begins, start with 16540 * this seq no. tcp_rexmit_max marks the end of special slow 16541 * start phase. tcp_snd_burst controls how many segments 16542 * can be sent because of an ack. 16543 */ 16544 tcp->tcp_rexmit_nxt = tcp->tcp_suna; 16545 tcp->tcp_snd_burst = TCP_CWND_SS; 16546 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 16547 (tcp->tcp_unsent == 0)) { 16548 tcp->tcp_rexmit_max = tcp->tcp_fss; 16549 } else { 16550 tcp->tcp_rexmit_max = tcp->tcp_snxt; 16551 } 16552 tcp->tcp_rexmit = B_TRUE; 16553 tcp->tcp_dupack_cnt = 0; 16554 16555 /* 16556 * Remove all rexmit SACK blk to start from fresh. 16557 */ 16558 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 16559 TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list); 16560 tcp->tcp_num_notsack_blk = 0; 16561 tcp->tcp_cnt_notsack_list = 0; 16562 } 16563 if (mp == NULL) { 16564 return; 16565 } 16566 /* Attach credentials to retransmitted initial SYNs. */ 16567 if (tcp->tcp_state == TCPS_SYN_SENT) { 16568 mblk_setcred(mp, tcp->tcp_cred); 16569 DB_CPID(mp) = tcp->tcp_cpid; 16570 } 16571 16572 tcp->tcp_csuna = tcp->tcp_snxt; 16573 BUMP_MIB(&tcp_mib, tcpRetransSegs); 16574 UPDATE_MIB(&tcp_mib, tcpRetransBytes, mss); 16575 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 16576 tcp_send_data(tcp, tcp->tcp_wq, mp); 16577 16578 } 16579 16580 /* tcp_unbind is called by tcp_wput_proto to handle T_UNBIND_REQ messages. */ 16581 static void 16582 tcp_unbind(tcp_t *tcp, mblk_t *mp) 16583 { 16584 conn_t *connp; 16585 16586 switch (tcp->tcp_state) { 16587 case TCPS_BOUND: 16588 case TCPS_LISTEN: 16589 break; 16590 default: 16591 tcp_err_ack(tcp, mp, TOUTSTATE, 0); 16592 return; 16593 } 16594 16595 /* 16596 * Need to clean up all the eagers since after the unbind, segments 16597 * will no longer be delivered to this listener stream. 16598 */ 16599 mutex_enter(&tcp->tcp_eager_lock); 16600 if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) { 16601 tcp_eager_cleanup(tcp, 0); 16602 } 16603 mutex_exit(&tcp->tcp_eager_lock); 16604 16605 if (tcp->tcp_ipversion == IPV4_VERSION) { 16606 tcp->tcp_ipha->ipha_src = 0; 16607 } else { 16608 V6_SET_ZERO(tcp->tcp_ip6h->ip6_src); 16609 } 16610 V6_SET_ZERO(tcp->tcp_ip_src_v6); 16611 bzero(tcp->tcp_tcph->th_lport, sizeof (tcp->tcp_tcph->th_lport)); 16612 tcp_bind_hash_remove(tcp); 16613 tcp->tcp_state = TCPS_IDLE; 16614 tcp->tcp_mdt = B_FALSE; 16615 /* Send M_FLUSH according to TPI */ 16616 (void) putnextctl1(tcp->tcp_rq, M_FLUSH, FLUSHRW); 16617 connp = tcp->tcp_connp; 16618 connp->conn_mdt_ok = B_FALSE; 16619 ipcl_hash_remove(connp); 16620 bzero(&connp->conn_ports, sizeof (connp->conn_ports)); 16621 mp = mi_tpi_ok_ack_alloc(mp); 16622 putnext(tcp->tcp_rq, mp); 16623 } 16624 16625 /* 16626 * Don't let port fall into the privileged range. 16627 * Since the extra privileged ports can be arbitrary we also 16628 * ensure that we exclude those from consideration. 16629 * tcp_g_epriv_ports is not sorted thus we loop over it until 16630 * there are no changes. 16631 * 16632 * Note: No locks are held when inspecting tcp_g_*epriv_ports 16633 * but instead the code relies on: 16634 * - the fact that the address of the array and its size never changes 16635 * - the atomic assignment of the elements of the array 16636 */ 16637 static in_port_t 16638 tcp_update_next_port(in_port_t port, boolean_t random) 16639 { 16640 int i; 16641 16642 if (random && tcp_random_anon_port != 0) { 16643 (void) random_get_pseudo_bytes((uint8_t *)&port, 16644 sizeof (in_port_t)); 16645 /* 16646 * Unless changed by a sys admin, the smallest anon port 16647 * is 32768 and the largest anon port is 65535. It is 16648 * very likely (50%) for the random port to be smaller 16649 * than the smallest anon port. When that happens, 16650 * add port % (anon port range) to the smallest anon 16651 * port to get the random port. It should fall into the 16652 * valid anon port range. 16653 */ 16654 if (port < tcp_smallest_anon_port) { 16655 port = tcp_smallest_anon_port + 16656 port % (tcp_largest_anon_port - 16657 tcp_smallest_anon_port); 16658 } 16659 } 16660 16661 retry: 16662 if (port < tcp_smallest_anon_port || port > tcp_largest_anon_port) 16663 port = (in_port_t)tcp_smallest_anon_port; 16664 16665 if (port < tcp_smallest_nonpriv_port) 16666 port = (in_port_t)tcp_smallest_nonpriv_port; 16667 16668 for (i = 0; i < tcp_g_num_epriv_ports; i++) { 16669 if (port == tcp_g_epriv_ports[i]) { 16670 port++; 16671 /* 16672 * Make sure whether the port is in the 16673 * valid range. 16674 * 16675 * XXX Note that if tcp_g_epriv_ports contains 16676 * all the anonymous ports this will be an 16677 * infinite loop. 16678 */ 16679 goto retry; 16680 } 16681 } 16682 return (port); 16683 } 16684 16685 /* 16686 * Return the next anonymous port in the priviledged port range for 16687 * bind checking. It starts at IPPORT_RESERVED - 1 and goes 16688 * downwards. This is the same behavior as documented in the userland 16689 * library call rresvport(3N). 16690 */ 16691 static in_port_t 16692 tcp_get_next_priv_port(void) 16693 { 16694 static in_port_t next_priv_port = IPPORT_RESERVED - 1; 16695 16696 if (next_priv_port < tcp_min_anonpriv_port) { 16697 next_priv_port = IPPORT_RESERVED - 1; 16698 } 16699 return (next_priv_port--); 16700 } 16701 16702 /* The write side r/w procedure. */ 16703 16704 #if CCS_STATS 16705 struct { 16706 struct { 16707 int64_t count, bytes; 16708 } tot, hit; 16709 } wrw_stats; 16710 #endif 16711 16712 /* 16713 * Call by tcp_wput() to handle all non data, except M_PROTO and M_PCPROTO, 16714 * messages. 16715 */ 16716 /* ARGSUSED */ 16717 static void 16718 tcp_wput_nondata(void *arg, mblk_t *mp, void *arg2) 16719 { 16720 conn_t *connp = (conn_t *)arg; 16721 tcp_t *tcp = connp->conn_tcp; 16722 queue_t *q = tcp->tcp_wq; 16723 16724 ASSERT(DB_TYPE(mp) != M_IOCTL); 16725 /* 16726 * TCP is D_MP and qprocsoff() is done towards the end of the tcp_close. 16727 * Once the close starts, streamhead and sockfs will not let any data 16728 * packets come down (close ensures that there are no threads using the 16729 * queue and no new threads will come down) but since qprocsoff() 16730 * hasn't happened yet, a M_FLUSH or some non data message might 16731 * get reflected back (in response to our own FLUSHRW) and get 16732 * processed after tcp_close() is done. The conn would still be valid 16733 * because a ref would have added but we need to check the state 16734 * before actually processing the packet. 16735 */ 16736 if (TCP_IS_DETACHED(tcp) || (tcp->tcp_state == TCPS_CLOSED)) { 16737 freemsg(mp); 16738 return; 16739 } 16740 16741 switch (DB_TYPE(mp)) { 16742 case M_IOCDATA: 16743 tcp_wput_iocdata(tcp, mp); 16744 break; 16745 case M_FLUSH: 16746 tcp_wput_flush(tcp, mp); 16747 break; 16748 default: 16749 CALL_IP_WPUT(connp, q, mp); 16750 break; 16751 } 16752 } 16753 16754 /* 16755 * The TCP fast path write put procedure. 16756 * NOTE: the logic of the fast path is duplicated from tcp_wput_data() 16757 */ 16758 /* ARGSUSED */ 16759 void 16760 tcp_output(void *arg, mblk_t *mp, void *arg2) 16761 { 16762 int len; 16763 int hdrlen; 16764 int plen; 16765 mblk_t *mp1; 16766 uchar_t *rptr; 16767 uint32_t snxt; 16768 tcph_t *tcph; 16769 struct datab *db; 16770 uint32_t suna; 16771 uint32_t mss; 16772 ipaddr_t *dst; 16773 ipaddr_t *src; 16774 uint32_t sum; 16775 int usable; 16776 conn_t *connp = (conn_t *)arg; 16777 tcp_t *tcp = connp->conn_tcp; 16778 uint32_t msize; 16779 16780 /* 16781 * Try and ASSERT the minimum possible references on the 16782 * conn early enough. Since we are executing on write side, 16783 * the connection is obviously not detached and that means 16784 * there is a ref each for TCP and IP. Since we are behind 16785 * the squeue, the minimum references needed are 3. If the 16786 * conn is in classifier hash list, there should be an 16787 * extra ref for that (we check both the possibilities). 16788 */ 16789 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 16790 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 16791 16792 ASSERT(DB_TYPE(mp) == M_DATA); 16793 msize = (mp->b_cont == NULL) ? MBLKL(mp) : msgdsize(mp); 16794 16795 mutex_enter(&connp->conn_lock); 16796 tcp->tcp_squeue_bytes -= msize; 16797 mutex_exit(&connp->conn_lock); 16798 16799 /* Bypass tcp protocol for fused tcp loopback */ 16800 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 16801 return; 16802 16803 mss = tcp->tcp_mss; 16804 if (tcp->tcp_xmit_zc_clean) 16805 mp = tcp_zcopy_backoff(tcp, mp, 0); 16806 16807 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 16808 len = (int)(mp->b_wptr - mp->b_rptr); 16809 16810 /* 16811 * Criteria for fast path: 16812 * 16813 * 1. no unsent data 16814 * 2. single mblk in request 16815 * 3. connection established 16816 * 4. data in mblk 16817 * 5. len <= mss 16818 * 6. no tcp_valid bits 16819 */ 16820 if ((tcp->tcp_unsent != 0) || 16821 (tcp->tcp_cork) || 16822 (mp->b_cont != NULL) || 16823 (tcp->tcp_state != TCPS_ESTABLISHED) || 16824 (len == 0) || 16825 (len > mss) || 16826 (tcp->tcp_valid_bits != 0)) { 16827 tcp_wput_data(tcp, mp, B_FALSE); 16828 return; 16829 } 16830 16831 ASSERT(tcp->tcp_xmit_tail_unsent == 0); 16832 ASSERT(tcp->tcp_fin_sent == 0); 16833 16834 /* queue new packet onto retransmission queue */ 16835 if (tcp->tcp_xmit_head == NULL) { 16836 tcp->tcp_xmit_head = mp; 16837 } else { 16838 tcp->tcp_xmit_last->b_cont = mp; 16839 } 16840 tcp->tcp_xmit_last = mp; 16841 tcp->tcp_xmit_tail = mp; 16842 16843 /* find out how much we can send */ 16844 /* BEGIN CSTYLED */ 16845 /* 16846 * un-acked usable 16847 * |--------------|-----------------| 16848 * tcp_suna tcp_snxt tcp_suna+tcp_swnd 16849 */ 16850 /* END CSTYLED */ 16851 16852 /* start sending from tcp_snxt */ 16853 snxt = tcp->tcp_snxt; 16854 16855 /* 16856 * Check to see if this connection has been idled for some 16857 * time and no ACK is expected. If it is, we need to slow 16858 * start again to get back the connection's "self-clock" as 16859 * described in VJ's paper. 16860 * 16861 * Refer to the comment in tcp_mss_set() for the calculation 16862 * of tcp_cwnd after idle. 16863 */ 16864 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 16865 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 16866 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 16867 } 16868 16869 usable = tcp->tcp_swnd; /* tcp window size */ 16870 if (usable > tcp->tcp_cwnd) 16871 usable = tcp->tcp_cwnd; /* congestion window smaller */ 16872 usable -= snxt; /* subtract stuff already sent */ 16873 suna = tcp->tcp_suna; 16874 usable += suna; 16875 /* usable can be < 0 if the congestion window is smaller */ 16876 if (len > usable) { 16877 /* Can't send complete M_DATA in one shot */ 16878 goto slow; 16879 } 16880 16881 if (tcp->tcp_flow_stopped && 16882 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 16883 tcp_clrqfull(tcp); 16884 } 16885 16886 /* 16887 * determine if anything to send (Nagle). 16888 * 16889 * 1. len < tcp_mss (i.e. small) 16890 * 2. unacknowledged data present 16891 * 3. len < nagle limit 16892 * 4. last packet sent < nagle limit (previous packet sent) 16893 */ 16894 if ((len < mss) && (snxt != suna) && 16895 (len < (int)tcp->tcp_naglim) && 16896 (tcp->tcp_last_sent_len < tcp->tcp_naglim)) { 16897 /* 16898 * This was the first unsent packet and normally 16899 * mss < xmit_hiwater so there is no need to worry 16900 * about flow control. The next packet will go 16901 * through the flow control check in tcp_wput_data(). 16902 */ 16903 /* leftover work from above */ 16904 tcp->tcp_unsent = len; 16905 tcp->tcp_xmit_tail_unsent = len; 16906 16907 return; 16908 } 16909 16910 /* len <= tcp->tcp_mss && len == unsent so no silly window */ 16911 16912 if (snxt == suna) { 16913 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 16914 } 16915 16916 /* we have always sent something */ 16917 tcp->tcp_rack_cnt = 0; 16918 16919 tcp->tcp_snxt = snxt + len; 16920 tcp->tcp_rack = tcp->tcp_rnxt; 16921 16922 if ((mp1 = dupb(mp)) == 0) 16923 goto no_memory; 16924 mp->b_prev = (mblk_t *)(uintptr_t)lbolt; 16925 mp->b_next = (mblk_t *)(uintptr_t)snxt; 16926 16927 /* adjust tcp header information */ 16928 tcph = tcp->tcp_tcph; 16929 tcph->th_flags[0] = (TH_ACK|TH_PUSH); 16930 16931 sum = len + tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 16932 sum = (sum >> 16) + (sum & 0xFFFF); 16933 U16_TO_ABE16(sum, tcph->th_sum); 16934 16935 U32_TO_ABE32(snxt, tcph->th_seq); 16936 16937 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 16938 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 16939 BUMP_LOCAL(tcp->tcp_obsegs); 16940 16941 /* Update the latest receive window size in TCP header. */ 16942 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 16943 tcph->th_win); 16944 16945 tcp->tcp_last_sent_len = (ushort_t)len; 16946 16947 plen = len + tcp->tcp_hdr_len; 16948 16949 if (tcp->tcp_ipversion == IPV4_VERSION) { 16950 tcp->tcp_ipha->ipha_length = htons(plen); 16951 } else { 16952 tcp->tcp_ip6h->ip6_plen = htons(plen - 16953 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 16954 } 16955 16956 /* see if we need to allocate a mblk for the headers */ 16957 hdrlen = tcp->tcp_hdr_len; 16958 rptr = mp1->b_rptr - hdrlen; 16959 db = mp1->b_datap; 16960 if ((db->db_ref != 2) || rptr < db->db_base || 16961 (!OK_32PTR(rptr))) { 16962 /* NOTE: we assume allocb returns an OK_32PTR */ 16963 mp = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 16964 tcp_wroff_xtra, BPRI_MED); 16965 if (!mp) { 16966 freemsg(mp1); 16967 goto no_memory; 16968 } 16969 mp->b_cont = mp1; 16970 mp1 = mp; 16971 /* Leave room for Link Level header */ 16972 /* hdrlen = tcp->tcp_hdr_len; */ 16973 rptr = &mp1->b_rptr[tcp_wroff_xtra]; 16974 mp1->b_wptr = &rptr[hdrlen]; 16975 } 16976 mp1->b_rptr = rptr; 16977 16978 /* Fill in the timestamp option. */ 16979 if (tcp->tcp_snd_ts_ok) { 16980 U32_TO_BE32((uint32_t)lbolt, 16981 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 16982 U32_TO_BE32(tcp->tcp_ts_recent, 16983 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 16984 } else { 16985 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 16986 } 16987 16988 /* copy header into outgoing packet */ 16989 dst = (ipaddr_t *)rptr; 16990 src = (ipaddr_t *)tcp->tcp_iphc; 16991 dst[0] = src[0]; 16992 dst[1] = src[1]; 16993 dst[2] = src[2]; 16994 dst[3] = src[3]; 16995 dst[4] = src[4]; 16996 dst[5] = src[5]; 16997 dst[6] = src[6]; 16998 dst[7] = src[7]; 16999 dst[8] = src[8]; 17000 dst[9] = src[9]; 17001 if (hdrlen -= 40) { 17002 hdrlen >>= 2; 17003 dst += 10; 17004 src += 10; 17005 do { 17006 *dst++ = *src++; 17007 } while (--hdrlen); 17008 } 17009 17010 /* 17011 * Set the ECN info in the TCP header. Note that this 17012 * is not the template header. 17013 */ 17014 if (tcp->tcp_ecn_ok) { 17015 SET_ECT(tcp, rptr); 17016 17017 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 17018 if (tcp->tcp_ecn_echo_on) 17019 tcph->th_flags[0] |= TH_ECE; 17020 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 17021 tcph->th_flags[0] |= TH_CWR; 17022 tcp->tcp_ecn_cwr_sent = B_TRUE; 17023 } 17024 } 17025 17026 if (tcp->tcp_ip_forward_progress) { 17027 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 17028 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 17029 tcp->tcp_ip_forward_progress = B_FALSE; 17030 } 17031 TCP_RECORD_TRACE(tcp, mp1, TCP_TRACE_SEND_PKT); 17032 tcp_send_data(tcp, tcp->tcp_wq, mp1); 17033 return; 17034 17035 /* 17036 * If we ran out of memory, we pretend to have sent the packet 17037 * and that it was lost on the wire. 17038 */ 17039 no_memory: 17040 return; 17041 17042 slow: 17043 /* leftover work from above */ 17044 tcp->tcp_unsent = len; 17045 tcp->tcp_xmit_tail_unsent = len; 17046 tcp_wput_data(tcp, NULL, B_FALSE); 17047 } 17048 17049 /* 17050 * The function called through squeue to get behind eager's perimeter to 17051 * finish the accept processing. 17052 */ 17053 /* ARGSUSED */ 17054 void 17055 tcp_accept_finish(void *arg, mblk_t *mp, void *arg2) 17056 { 17057 conn_t *connp = (conn_t *)arg; 17058 tcp_t *tcp = connp->conn_tcp; 17059 queue_t *q = tcp->tcp_rq; 17060 mblk_t *mp1; 17061 mblk_t *stropt_mp = mp; 17062 struct stroptions *stropt; 17063 uint_t thwin; 17064 17065 /* 17066 * Drop the eager's ref on the listener, that was placed when 17067 * this eager began life in tcp_conn_request. 17068 */ 17069 CONN_DEC_REF(tcp->tcp_saved_listener->tcp_connp); 17070 17071 if (tcp->tcp_state <= TCPS_BOUND || tcp->tcp_accept_error) { 17072 /* 17073 * Someone blewoff the eager before we could finish 17074 * the accept. 17075 * 17076 * The only reason eager exists it because we put in 17077 * a ref on it when conn ind went up. We need to send 17078 * a disconnect indication up while the last reference 17079 * on the eager will be dropped by the squeue when we 17080 * return. 17081 */ 17082 ASSERT(tcp->tcp_listener == NULL); 17083 if (tcp->tcp_issocket || tcp->tcp_send_discon_ind) { 17084 struct T_discon_ind *tdi; 17085 17086 (void) putnextctl1(q, M_FLUSH, FLUSHRW); 17087 /* 17088 * Let us reuse the incoming mblk to avoid memory 17089 * allocation failure problems. We know that the 17090 * size of the incoming mblk i.e. stroptions is greater 17091 * than sizeof T_discon_ind. So the reallocb below 17092 * can't fail. 17093 */ 17094 freemsg(mp->b_cont); 17095 mp->b_cont = NULL; 17096 ASSERT(DB_REF(mp) == 1); 17097 mp = reallocb(mp, sizeof (struct T_discon_ind), 17098 B_FALSE); 17099 ASSERT(mp != NULL); 17100 DB_TYPE(mp) = M_PROTO; 17101 ((union T_primitives *)mp->b_rptr)->type = T_DISCON_IND; 17102 tdi = (struct T_discon_ind *)mp->b_rptr; 17103 if (tcp->tcp_issocket) { 17104 tdi->DISCON_reason = ECONNREFUSED; 17105 tdi->SEQ_number = 0; 17106 } else { 17107 tdi->DISCON_reason = ENOPROTOOPT; 17108 tdi->SEQ_number = 17109 tcp->tcp_conn_req_seqnum; 17110 } 17111 mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind); 17112 putnext(q, mp); 17113 } else { 17114 freemsg(mp); 17115 } 17116 if (tcp->tcp_hard_binding) { 17117 tcp->tcp_hard_binding = B_FALSE; 17118 tcp->tcp_hard_bound = B_TRUE; 17119 } 17120 tcp->tcp_detached = B_FALSE; 17121 return; 17122 } 17123 17124 mp1 = stropt_mp->b_cont; 17125 stropt_mp->b_cont = NULL; 17126 ASSERT(DB_TYPE(stropt_mp) == M_SETOPTS); 17127 stropt = (struct stroptions *)stropt_mp->b_rptr; 17128 17129 while (mp1 != NULL) { 17130 mp = mp1; 17131 mp1 = mp1->b_cont; 17132 mp->b_cont = NULL; 17133 tcp->tcp_drop_opt_ack_cnt++; 17134 CALL_IP_WPUT(connp, tcp->tcp_wq, mp); 17135 } 17136 mp = NULL; 17137 17138 /* 17139 * For a loopback connection with tcp_direct_sockfs on, note that 17140 * we don't have to protect tcp_rcv_list yet because synchronous 17141 * streams has not yet been enabled and tcp_fuse_rrw() cannot 17142 * possibly race with us. 17143 */ 17144 17145 /* 17146 * Set the max window size (tcp_rq->q_hiwat) of the acceptor 17147 * properly. This is the first time we know of the acceptor' 17148 * queue. So we do it here. 17149 */ 17150 if (tcp->tcp_rcv_list == NULL) { 17151 /* 17152 * Recv queue is empty, tcp_rwnd should not have changed. 17153 * That means it should be equal to the listener's tcp_rwnd. 17154 */ 17155 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd; 17156 } else { 17157 #ifdef DEBUG 17158 uint_t cnt = 0; 17159 17160 mp1 = tcp->tcp_rcv_list; 17161 while ((mp = mp1) != NULL) { 17162 mp1 = mp->b_next; 17163 cnt += msgdsize(mp); 17164 } 17165 ASSERT(cnt != 0 && tcp->tcp_rcv_cnt == cnt); 17166 #endif 17167 /* There is some data, add them back to get the max. */ 17168 tcp->tcp_rq->q_hiwat = tcp->tcp_rwnd + tcp->tcp_rcv_cnt; 17169 } 17170 17171 stropt->so_flags = SO_HIWAT; 17172 stropt->so_hiwat = MAX(q->q_hiwat, tcp_sth_rcv_hiwat); 17173 17174 stropt->so_flags |= SO_MAXBLK; 17175 stropt->so_maxblk = tcp_maxpsz_set(tcp, B_FALSE); 17176 17177 /* 17178 * This is the first time we run on the correct 17179 * queue after tcp_accept. So fix all the q parameters 17180 * here. 17181 */ 17182 /* Allocate room for SACK options if needed. */ 17183 stropt->so_flags |= SO_WROFF; 17184 if (tcp->tcp_fused) { 17185 ASSERT(tcp->tcp_loopback); 17186 ASSERT(tcp->tcp_loopback_peer != NULL); 17187 /* 17188 * For fused tcp loopback, set the stream head's write 17189 * offset value to zero since we won't be needing any room 17190 * for TCP/IP headers. This would also improve performance 17191 * since it would reduce the amount of work done by kmem. 17192 * Non-fused tcp loopback case is handled separately below. 17193 */ 17194 stropt->so_wroff = 0; 17195 /* 17196 * Record the stream head's high water mark for this endpoint; 17197 * this is used for flow-control purposes in tcp_fuse_output(). 17198 */ 17199 stropt->so_hiwat = tcp_fuse_set_rcv_hiwat(tcp, q->q_hiwat); 17200 /* 17201 * Update the peer's transmit parameters according to 17202 * our recently calculated high water mark value. 17203 */ 17204 (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE); 17205 } else if (tcp->tcp_snd_sack_ok) { 17206 stropt->so_wroff = tcp->tcp_hdr_len + TCPOPT_MAX_SACK_LEN + 17207 (tcp->tcp_loopback ? 0 : tcp_wroff_xtra); 17208 } else { 17209 stropt->so_wroff = tcp->tcp_hdr_len + (tcp->tcp_loopback ? 0 : 17210 tcp_wroff_xtra); 17211 } 17212 17213 /* 17214 * If this is endpoint is handling SSL, then reserve extra 17215 * offset and space at the end. 17216 * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, 17217 * overriding the previous setting. The extra cost of signing and 17218 * encrypting multiple MSS-size records (12 of them with Ethernet), 17219 * instead of a single contiguous one by the stream head 17220 * largely outweighs the statistical reduction of ACKs, when 17221 * applicable. The peer will also save on decyption and verification 17222 * costs. 17223 */ 17224 if (tcp->tcp_kssl_ctx != NULL) { 17225 stropt->so_wroff += SSL3_WROFFSET; 17226 17227 stropt->so_flags |= SO_TAIL; 17228 stropt->so_tail = SSL3_MAX_TAIL_LEN; 17229 17230 stropt->so_maxblk = SSL3_MAX_RECORD_LEN; 17231 } 17232 17233 /* Send the options up */ 17234 putnext(q, stropt_mp); 17235 17236 /* 17237 * Pass up any data and/or a fin that has been received. 17238 * 17239 * Adjust receive window in case it had decreased 17240 * (because there is data <=> tcp_rcv_list != NULL) 17241 * while the connection was detached. Note that 17242 * in case the eager was flow-controlled, w/o this 17243 * code, the rwnd may never open up again! 17244 */ 17245 if (tcp->tcp_rcv_list != NULL) { 17246 /* We drain directly in case of fused tcp loopback */ 17247 if (!tcp->tcp_fused && canputnext(q)) { 17248 tcp->tcp_rwnd = q->q_hiwat; 17249 thwin = ((uint_t)BE16_TO_U16(tcp->tcp_tcph->th_win)) 17250 << tcp->tcp_rcv_ws; 17251 thwin -= tcp->tcp_rnxt - tcp->tcp_rack; 17252 if (tcp->tcp_state >= TCPS_ESTABLISHED && 17253 (q->q_hiwat - thwin >= tcp->tcp_mss)) { 17254 tcp_xmit_ctl(NULL, 17255 tcp, (tcp->tcp_swnd == 0) ? 17256 tcp->tcp_suna : tcp->tcp_snxt, 17257 tcp->tcp_rnxt, TH_ACK); 17258 BUMP_MIB(&tcp_mib, tcpOutWinUpdate); 17259 } 17260 17261 } 17262 (void) tcp_rcv_drain(q, tcp); 17263 17264 /* 17265 * For fused tcp loopback, back-enable peer endpoint 17266 * if it's currently flow-controlled. 17267 */ 17268 if (tcp->tcp_fused && 17269 tcp->tcp_loopback_peer->tcp_flow_stopped) { 17270 tcp_t *peer_tcp = tcp->tcp_loopback_peer; 17271 17272 ASSERT(peer_tcp != NULL); 17273 ASSERT(peer_tcp->tcp_fused); 17274 17275 tcp_clrqfull(peer_tcp); 17276 TCP_STAT(tcp_fusion_backenabled); 17277 } 17278 } 17279 ASSERT(tcp->tcp_rcv_list == NULL || tcp->tcp_fused_sigurg); 17280 if (tcp->tcp_fin_rcvd && !tcp->tcp_ordrel_done) { 17281 mp = mi_tpi_ordrel_ind(); 17282 if (mp) { 17283 tcp->tcp_ordrel_done = B_TRUE; 17284 putnext(q, mp); 17285 if (tcp->tcp_deferred_clean_death) { 17286 /* 17287 * tcp_clean_death was deferred 17288 * for T_ORDREL_IND - do it now 17289 */ 17290 (void) tcp_clean_death(tcp, 17291 tcp->tcp_client_errno, 21); 17292 tcp->tcp_deferred_clean_death = B_FALSE; 17293 } 17294 } else { 17295 /* 17296 * Run the orderly release in the 17297 * service routine. 17298 */ 17299 qenable(q); 17300 } 17301 } 17302 if (tcp->tcp_hard_binding) { 17303 tcp->tcp_hard_binding = B_FALSE; 17304 tcp->tcp_hard_bound = B_TRUE; 17305 } 17306 17307 tcp->tcp_detached = B_FALSE; 17308 17309 /* We can enable synchronous streams now */ 17310 if (tcp->tcp_fused) { 17311 tcp_fuse_syncstr_enable_pair(tcp); 17312 } 17313 17314 if (tcp->tcp_ka_enabled) { 17315 tcp->tcp_ka_last_intrvl = 0; 17316 tcp->tcp_ka_tid = TCP_TIMER(tcp, tcp_keepalive_killer, 17317 MSEC_TO_TICK(tcp->tcp_ka_interval)); 17318 } 17319 17320 /* 17321 * At this point, eager is fully established and will 17322 * have the following references - 17323 * 17324 * 2 references for connection to exist (1 for TCP and 1 for IP). 17325 * 1 reference for the squeue which will be dropped by the squeue as 17326 * soon as this function returns. 17327 * There will be 1 additonal reference for being in classifier 17328 * hash list provided something bad hasn't happened. 17329 */ 17330 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 17331 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 17332 } 17333 17334 /* 17335 * The function called through squeue to get behind listener's perimeter to 17336 * send a deffered conn_ind. 17337 */ 17338 /* ARGSUSED */ 17339 void 17340 tcp_send_pending(void *arg, mblk_t *mp, void *arg2) 17341 { 17342 conn_t *connp = (conn_t *)arg; 17343 tcp_t *listener = connp->conn_tcp; 17344 17345 if (listener->tcp_state == TCPS_CLOSED || 17346 TCP_IS_DETACHED(listener)) { 17347 /* 17348 * If listener has closed, it would have caused a 17349 * a cleanup/blowoff to happen for the eager. 17350 */ 17351 tcp_t *tcp; 17352 struct T_conn_ind *conn_ind; 17353 17354 conn_ind = (struct T_conn_ind *)mp->b_rptr; 17355 bcopy(mp->b_rptr + conn_ind->OPT_offset, &tcp, 17356 conn_ind->OPT_length); 17357 /* 17358 * We need to drop the ref on eager that was put 17359 * tcp_rput_data() before trying to send the conn_ind 17360 * to listener. The conn_ind was deferred in tcp_send_conn_ind 17361 * and tcp_wput_accept() is sending this deferred conn_ind but 17362 * listener is closed so we drop the ref. 17363 */ 17364 CONN_DEC_REF(tcp->tcp_connp); 17365 freemsg(mp); 17366 return; 17367 } 17368 putnext(listener->tcp_rq, mp); 17369 } 17370 17371 17372 /* 17373 * This is the STREAMS entry point for T_CONN_RES coming down on 17374 * Acceptor STREAM when sockfs listener does accept processing. 17375 * Read the block comment on top pf tcp_conn_request(). 17376 */ 17377 void 17378 tcp_wput_accept(queue_t *q, mblk_t *mp) 17379 { 17380 queue_t *rq = RD(q); 17381 struct T_conn_res *conn_res; 17382 tcp_t *eager; 17383 tcp_t *listener; 17384 struct T_ok_ack *ok; 17385 t_scalar_t PRIM_type; 17386 mblk_t *opt_mp; 17387 conn_t *econnp; 17388 17389 ASSERT(DB_TYPE(mp) == M_PROTO); 17390 17391 conn_res = (struct T_conn_res *)mp->b_rptr; 17392 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX); 17393 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_res)) { 17394 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17395 if (mp != NULL) 17396 putnext(rq, mp); 17397 return; 17398 } 17399 switch (conn_res->PRIM_type) { 17400 case O_T_CONN_RES: 17401 case T_CONN_RES: 17402 /* 17403 * We pass up an err ack if allocb fails. This will 17404 * cause sockfs to issue a T_DISCON_REQ which will cause 17405 * tcp_eager_blowoff to be called. sockfs will then call 17406 * rq->q_qinfo->qi_qclose to cleanup the acceptor stream. 17407 * we need to do the allocb up here because we have to 17408 * make sure rq->q_qinfo->qi_qclose still points to the 17409 * correct function (tcpclose_accept) in case allocb 17410 * fails. 17411 */ 17412 opt_mp = allocb(sizeof (struct stroptions), BPRI_HI); 17413 if (opt_mp == NULL) { 17414 mp = mi_tpi_err_ack_alloc(mp, TPROTO, 0); 17415 if (mp != NULL) 17416 putnext(rq, mp); 17417 return; 17418 } 17419 17420 bcopy(mp->b_rptr + conn_res->OPT_offset, 17421 &eager, conn_res->OPT_length); 17422 PRIM_type = conn_res->PRIM_type; 17423 mp->b_datap->db_type = M_PCPROTO; 17424 mp->b_wptr = mp->b_rptr + sizeof (struct T_ok_ack); 17425 ok = (struct T_ok_ack *)mp->b_rptr; 17426 ok->PRIM_type = T_OK_ACK; 17427 ok->CORRECT_prim = PRIM_type; 17428 econnp = eager->tcp_connp; 17429 econnp->conn_dev = (dev_t)q->q_ptr; 17430 eager->tcp_rq = rq; 17431 eager->tcp_wq = q; 17432 rq->q_ptr = econnp; 17433 rq->q_qinfo = &tcp_rinit; 17434 q->q_ptr = econnp; 17435 q->q_qinfo = &tcp_winit; 17436 listener = eager->tcp_listener; 17437 eager->tcp_issocket = B_TRUE; 17438 eager->tcp_cred = econnp->conn_cred = 17439 listener->tcp_connp->conn_cred; 17440 crhold(econnp->conn_cred); 17441 econnp->conn_zoneid = listener->tcp_connp->conn_zoneid; 17442 17443 /* Put the ref for IP */ 17444 CONN_INC_REF(econnp); 17445 17446 /* 17447 * We should have minimum of 3 references on the conn 17448 * at this point. One each for TCP and IP and one for 17449 * the T_conn_ind that was sent up when the 3-way handshake 17450 * completed. In the normal case we would also have another 17451 * reference (making a total of 4) for the conn being in the 17452 * classifier hash list. However the eager could have received 17453 * an RST subsequently and tcp_closei_local could have removed 17454 * the eager from the classifier hash list, hence we can't 17455 * assert that reference. 17456 */ 17457 ASSERT(econnp->conn_ref >= 3); 17458 17459 /* 17460 * Send the new local address also up to sockfs. There 17461 * should already be enough space in the mp that came 17462 * down from soaccept(). 17463 */ 17464 if (eager->tcp_family == AF_INET) { 17465 sin_t *sin; 17466 17467 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17468 (sizeof (struct T_ok_ack) + sizeof (sin_t))); 17469 sin = (sin_t *)mp->b_wptr; 17470 mp->b_wptr += sizeof (sin_t); 17471 sin->sin_family = AF_INET; 17472 sin->sin_port = eager->tcp_lport; 17473 sin->sin_addr.s_addr = eager->tcp_ipha->ipha_src; 17474 } else { 17475 sin6_t *sin6; 17476 17477 ASSERT((mp->b_datap->db_lim - mp->b_datap->db_base) >= 17478 sizeof (struct T_ok_ack) + sizeof (sin6_t)); 17479 sin6 = (sin6_t *)mp->b_wptr; 17480 mp->b_wptr += sizeof (sin6_t); 17481 sin6->sin6_family = AF_INET6; 17482 sin6->sin6_port = eager->tcp_lport; 17483 if (eager->tcp_ipversion == IPV4_VERSION) { 17484 sin6->sin6_flowinfo = 0; 17485 IN6_IPADDR_TO_V4MAPPED( 17486 eager->tcp_ipha->ipha_src, 17487 &sin6->sin6_addr); 17488 } else { 17489 ASSERT(eager->tcp_ip6h != NULL); 17490 sin6->sin6_flowinfo = 17491 eager->tcp_ip6h->ip6_vcf & 17492 ~IPV6_VERS_AND_FLOW_MASK; 17493 sin6->sin6_addr = eager->tcp_ip6h->ip6_src; 17494 } 17495 sin6->sin6_scope_id = 0; 17496 sin6->__sin6_src_id = 0; 17497 } 17498 17499 putnext(rq, mp); 17500 17501 opt_mp->b_datap->db_type = M_SETOPTS; 17502 opt_mp->b_wptr += sizeof (struct stroptions); 17503 17504 /* 17505 * Prepare for inheriting IPV6_BOUND_IF and IPV6_RECVPKTINFO 17506 * from listener to acceptor. The message is chained on the 17507 * bind_mp which tcp_rput_other will send down to IP. 17508 */ 17509 if (listener->tcp_bound_if != 0) { 17510 /* allocate optmgmt req */ 17511 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 17512 IPV6_BOUND_IF, (char *)&listener->tcp_bound_if, 17513 sizeof (int)); 17514 if (mp != NULL) 17515 linkb(opt_mp, mp); 17516 } 17517 if (listener->tcp_ipv6_recvancillary & TCP_IPV6_RECVPKTINFO) { 17518 uint_t on = 1; 17519 17520 /* allocate optmgmt req */ 17521 mp = tcp_setsockopt_mp(IPPROTO_IPV6, 17522 IPV6_RECVPKTINFO, (char *)&on, sizeof (on)); 17523 if (mp != NULL) 17524 linkb(opt_mp, mp); 17525 } 17526 17527 17528 mutex_enter(&listener->tcp_eager_lock); 17529 17530 if (listener->tcp_eager_prev_q0->tcp_conn_def_q0) { 17531 17532 tcp_t *tail; 17533 tcp_t *tcp; 17534 mblk_t *mp1; 17535 17536 tcp = listener->tcp_eager_prev_q0; 17537 /* 17538 * listener->tcp_eager_prev_q0 points to the TAIL of the 17539 * deferred T_conn_ind queue. We need to get to the head 17540 * of the queue in order to send up T_conn_ind the same 17541 * order as how the 3WHS is completed. 17542 */ 17543 while (tcp != listener) { 17544 if (!tcp->tcp_eager_prev_q0->tcp_conn_def_q0 && 17545 !tcp->tcp_kssl_pending) 17546 break; 17547 else 17548 tcp = tcp->tcp_eager_prev_q0; 17549 } 17550 /* None of the pending eagers can be sent up now */ 17551 if (tcp == listener) 17552 goto no_more_eagers; 17553 17554 mp1 = tcp->tcp_conn.tcp_eager_conn_ind; 17555 tcp->tcp_conn.tcp_eager_conn_ind = NULL; 17556 /* Move from q0 to q */ 17557 ASSERT(listener->tcp_conn_req_cnt_q0 > 0); 17558 listener->tcp_conn_req_cnt_q0--; 17559 listener->tcp_conn_req_cnt_q++; 17560 tcp->tcp_eager_next_q0->tcp_eager_prev_q0 = 17561 tcp->tcp_eager_prev_q0; 17562 tcp->tcp_eager_prev_q0->tcp_eager_next_q0 = 17563 tcp->tcp_eager_next_q0; 17564 tcp->tcp_eager_prev_q0 = NULL; 17565 tcp->tcp_eager_next_q0 = NULL; 17566 tcp->tcp_conn_def_q0 = B_FALSE; 17567 17568 /* 17569 * Insert at end of the queue because sockfs sends 17570 * down T_CONN_RES in chronological order. Leaving 17571 * the older conn indications at front of the queue 17572 * helps reducing search time. 17573 */ 17574 tail = listener->tcp_eager_last_q; 17575 if (tail != NULL) { 17576 tail->tcp_eager_next_q = tcp; 17577 } else { 17578 listener->tcp_eager_next_q = tcp; 17579 } 17580 listener->tcp_eager_last_q = tcp; 17581 tcp->tcp_eager_next_q = NULL; 17582 17583 /* Need to get inside the listener perimeter */ 17584 CONN_INC_REF(listener->tcp_connp); 17585 squeue_fill(listener->tcp_connp->conn_sqp, mp1, 17586 tcp_send_pending, listener->tcp_connp, 17587 SQTAG_TCP_SEND_PENDING); 17588 } 17589 no_more_eagers: 17590 tcp_eager_unlink(eager); 17591 mutex_exit(&listener->tcp_eager_lock); 17592 17593 /* 17594 * At this point, the eager is detached from the listener 17595 * but we still have an extra refs on eager (apart from the 17596 * usual tcp references). The ref was placed in tcp_rput_data 17597 * before sending the conn_ind in tcp_send_conn_ind. 17598 * The ref will be dropped in tcp_accept_finish(). 17599 */ 17600 squeue_enter_nodrain(econnp->conn_sqp, opt_mp, 17601 tcp_accept_finish, econnp, SQTAG_TCP_ACCEPT_FINISH_Q0); 17602 return; 17603 default: 17604 mp = mi_tpi_err_ack_alloc(mp, TNOTSUPPORT, 0); 17605 if (mp != NULL) 17606 putnext(rq, mp); 17607 return; 17608 } 17609 } 17610 17611 void 17612 tcp_wput(queue_t *q, mblk_t *mp) 17613 { 17614 conn_t *connp = Q_TO_CONN(q); 17615 tcp_t *tcp; 17616 void (*output_proc)(); 17617 t_scalar_t type; 17618 uchar_t *rptr; 17619 struct iocblk *iocp; 17620 uint32_t msize; 17621 17622 ASSERT(connp->conn_ref >= 2); 17623 17624 switch (DB_TYPE(mp)) { 17625 case M_DATA: 17626 tcp = connp->conn_tcp; 17627 ASSERT(tcp != NULL); 17628 17629 msize = msgdsize(mp); 17630 17631 mutex_enter(&connp->conn_lock); 17632 CONN_INC_REF_LOCKED(connp); 17633 17634 tcp->tcp_squeue_bytes += msize; 17635 if (TCP_UNSENT_BYTES(tcp) > tcp->tcp_xmit_hiwater) { 17636 mutex_exit(&connp->conn_lock); 17637 tcp_setqfull(tcp); 17638 } else 17639 mutex_exit(&connp->conn_lock); 17640 17641 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 17642 tcp_output, connp, SQTAG_TCP_OUTPUT); 17643 return; 17644 case M_PROTO: 17645 case M_PCPROTO: 17646 /* 17647 * if it is a snmp message, don't get behind the squeue 17648 */ 17649 tcp = connp->conn_tcp; 17650 rptr = mp->b_rptr; 17651 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 17652 type = ((union T_primitives *)rptr)->type; 17653 } else { 17654 if (tcp->tcp_debug) { 17655 (void) strlog(TCP_MOD_ID, 0, 1, 17656 SL_ERROR|SL_TRACE, 17657 "tcp_wput_proto, dropping one..."); 17658 } 17659 freemsg(mp); 17660 return; 17661 } 17662 if (type == T_SVR4_OPTMGMT_REQ) { 17663 cred_t *cr = DB_CREDDEF(mp, 17664 tcp->tcp_cred); 17665 if (snmpcom_req(q, mp, tcp_snmp_set, tcp_snmp_get, 17666 cr)) { 17667 /* 17668 * This was a SNMP request 17669 */ 17670 return; 17671 } else { 17672 output_proc = tcp_wput_proto; 17673 } 17674 } else { 17675 output_proc = tcp_wput_proto; 17676 } 17677 break; 17678 case M_IOCTL: 17679 /* 17680 * Most ioctls can be processed right away without going via 17681 * squeues - process them right here. Those that do require 17682 * squeue (currently TCP_IOC_DEFAULT_Q and _SIOCSOCKFALLBACK) 17683 * are processed by tcp_wput_ioctl(). 17684 */ 17685 iocp = (struct iocblk *)mp->b_rptr; 17686 tcp = connp->conn_tcp; 17687 17688 switch (iocp->ioc_cmd) { 17689 case TCP_IOC_ABORT_CONN: 17690 tcp_ioctl_abort_conn(q, mp); 17691 return; 17692 case TI_GETPEERNAME: 17693 if (tcp->tcp_state < TCPS_SYN_RCVD) { 17694 iocp->ioc_error = ENOTCONN; 17695 iocp->ioc_count = 0; 17696 mp->b_datap->db_type = M_IOCACK; 17697 qreply(q, mp); 17698 return; 17699 } 17700 /* FALLTHRU */ 17701 case TI_GETMYNAME: 17702 mi_copyin(q, mp, NULL, 17703 SIZEOF_STRUCT(strbuf, iocp->ioc_flag)); 17704 return; 17705 case ND_SET: 17706 /* nd_getset does the necessary checks */ 17707 case ND_GET: 17708 if (!nd_getset(q, tcp_g_nd, mp)) { 17709 CALL_IP_WPUT(connp, q, mp); 17710 return; 17711 } 17712 qreply(q, mp); 17713 return; 17714 case TCP_IOC_DEFAULT_Q: 17715 /* 17716 * Wants to be the default wq. Check the credentials 17717 * first, the rest is executed via squeue. 17718 */ 17719 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 17720 iocp->ioc_error = EPERM; 17721 iocp->ioc_count = 0; 17722 mp->b_datap->db_type = M_IOCACK; 17723 qreply(q, mp); 17724 return; 17725 } 17726 output_proc = tcp_wput_ioctl; 17727 break; 17728 default: 17729 output_proc = tcp_wput_ioctl; 17730 break; 17731 } 17732 break; 17733 default: 17734 output_proc = tcp_wput_nondata; 17735 break; 17736 } 17737 17738 CONN_INC_REF(connp); 17739 (*tcp_squeue_wput_proc)(connp->conn_sqp, mp, 17740 output_proc, connp, SQTAG_TCP_WPUT_OTHER); 17741 } 17742 17743 /* 17744 * Initial STREAMS write side put() procedure for sockets. It tries to 17745 * handle the T_CAPABILITY_REQ which sockfs sends down while setting 17746 * up the socket without using the squeue. Non T_CAPABILITY_REQ messages 17747 * are handled by tcp_wput() as usual. 17748 * 17749 * All further messages will also be handled by tcp_wput() because we cannot 17750 * be sure that the above short cut is safe later. 17751 */ 17752 static void 17753 tcp_wput_sock(queue_t *wq, mblk_t *mp) 17754 { 17755 conn_t *connp = Q_TO_CONN(wq); 17756 tcp_t *tcp = connp->conn_tcp; 17757 struct T_capability_req *car = (struct T_capability_req *)mp->b_rptr; 17758 17759 ASSERT(wq->q_qinfo == &tcp_sock_winit); 17760 wq->q_qinfo = &tcp_winit; 17761 17762 ASSERT(IPCL_IS_TCP(connp)); 17763 ASSERT(TCP_IS_SOCKET(tcp)); 17764 17765 if (DB_TYPE(mp) == M_PCPROTO && 17766 MBLKL(mp) == sizeof (struct T_capability_req) && 17767 car->PRIM_type == T_CAPABILITY_REQ) { 17768 tcp_capability_req(tcp, mp); 17769 return; 17770 } 17771 17772 tcp_wput(wq, mp); 17773 } 17774 17775 static boolean_t 17776 tcp_zcopy_check(tcp_t *tcp) 17777 { 17778 conn_t *connp = tcp->tcp_connp; 17779 ire_t *ire; 17780 boolean_t zc_enabled = B_FALSE; 17781 17782 if (do_tcpzcopy == 2) 17783 zc_enabled = B_TRUE; 17784 else if (tcp->tcp_ipversion == IPV4_VERSION && 17785 IPCL_IS_CONNECTED(connp) && 17786 (connp->conn_flags & IPCL_CHECK_POLICY) == 0 && 17787 connp->conn_dontroute == 0 && 17788 !connp->conn_nexthop_set && 17789 connp->conn_xmit_if_ill == NULL && 17790 connp->conn_nofailover_ill == NULL && 17791 do_tcpzcopy == 1) { 17792 /* 17793 * the checks above closely resemble the fast path checks 17794 * in tcp_send_data(). 17795 */ 17796 mutex_enter(&connp->conn_lock); 17797 ire = connp->conn_ire_cache; 17798 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 17799 if (ire != NULL && !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 17800 IRE_REFHOLD(ire); 17801 if (ire->ire_stq != NULL) { 17802 ill_t *ill = (ill_t *)ire->ire_stq->q_ptr; 17803 17804 zc_enabled = ill && (ill->ill_capabilities & 17805 ILL_CAPAB_ZEROCOPY) && 17806 (ill->ill_zerocopy_capab-> 17807 ill_zerocopy_flags != 0); 17808 } 17809 IRE_REFRELE(ire); 17810 } 17811 mutex_exit(&connp->conn_lock); 17812 } 17813 tcp->tcp_snd_zcopy_on = zc_enabled; 17814 if (!TCP_IS_DETACHED(tcp)) { 17815 if (zc_enabled) { 17816 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMSAFE); 17817 TCP_STAT(tcp_zcopy_on); 17818 } else { 17819 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 17820 TCP_STAT(tcp_zcopy_off); 17821 } 17822 } 17823 return (zc_enabled); 17824 } 17825 17826 static mblk_t * 17827 tcp_zcopy_disable(tcp_t *tcp, mblk_t *bp) 17828 { 17829 if (do_tcpzcopy == 2) 17830 return (bp); 17831 else if (tcp->tcp_snd_zcopy_on) { 17832 tcp->tcp_snd_zcopy_on = B_FALSE; 17833 if (!TCP_IS_DETACHED(tcp)) { 17834 (void) mi_set_sth_copyopt(tcp->tcp_rq, ZCVMUNSAFE); 17835 TCP_STAT(tcp_zcopy_disable); 17836 } 17837 } 17838 return (tcp_zcopy_backoff(tcp, bp, 0)); 17839 } 17840 17841 /* 17842 * Backoff from a zero-copy mblk by copying data to a new mblk and freeing 17843 * the original desballoca'ed segmapped mblk. 17844 */ 17845 static mblk_t * 17846 tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, int fix_xmitlist) 17847 { 17848 mblk_t *head, *tail, *nbp; 17849 if (IS_VMLOANED_MBLK(bp)) { 17850 TCP_STAT(tcp_zcopy_backoff); 17851 if ((head = copyb(bp)) == NULL) { 17852 /* fail to backoff; leave it for the next backoff */ 17853 tcp->tcp_xmit_zc_clean = B_FALSE; 17854 return (bp); 17855 } 17856 if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 17857 if (fix_xmitlist) 17858 tcp_zcopy_notify(tcp); 17859 else 17860 head->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 17861 } 17862 nbp = bp->b_cont; 17863 if (fix_xmitlist) { 17864 head->b_prev = bp->b_prev; 17865 head->b_next = bp->b_next; 17866 if (tcp->tcp_xmit_tail == bp) 17867 tcp->tcp_xmit_tail = head; 17868 } 17869 bp->b_next = NULL; 17870 bp->b_prev = NULL; 17871 freeb(bp); 17872 } else { 17873 head = bp; 17874 nbp = bp->b_cont; 17875 } 17876 tail = head; 17877 while (nbp) { 17878 if (IS_VMLOANED_MBLK(nbp)) { 17879 TCP_STAT(tcp_zcopy_backoff); 17880 if ((tail->b_cont = copyb(nbp)) == NULL) { 17881 tcp->tcp_xmit_zc_clean = B_FALSE; 17882 tail->b_cont = nbp; 17883 return (head); 17884 } 17885 tail = tail->b_cont; 17886 if (nbp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) { 17887 if (fix_xmitlist) 17888 tcp_zcopy_notify(tcp); 17889 else 17890 tail->b_datap->db_struioflag |= 17891 STRUIO_ZCNOTIFY; 17892 } 17893 bp = nbp; 17894 nbp = nbp->b_cont; 17895 if (fix_xmitlist) { 17896 tail->b_prev = bp->b_prev; 17897 tail->b_next = bp->b_next; 17898 if (tcp->tcp_xmit_tail == bp) 17899 tcp->tcp_xmit_tail = tail; 17900 } 17901 bp->b_next = NULL; 17902 bp->b_prev = NULL; 17903 freeb(bp); 17904 } else { 17905 tail->b_cont = nbp; 17906 tail = nbp; 17907 nbp = nbp->b_cont; 17908 } 17909 } 17910 if (fix_xmitlist) { 17911 tcp->tcp_xmit_last = tail; 17912 tcp->tcp_xmit_zc_clean = B_TRUE; 17913 } 17914 return (head); 17915 } 17916 17917 static void 17918 tcp_zcopy_notify(tcp_t *tcp) 17919 { 17920 struct stdata *stp; 17921 17922 if (tcp->tcp_detached) 17923 return; 17924 stp = STREAM(tcp->tcp_rq); 17925 mutex_enter(&stp->sd_lock); 17926 stp->sd_flag |= STZCNOTIFY; 17927 cv_broadcast(&stp->sd_zcopy_wait); 17928 mutex_exit(&stp->sd_lock); 17929 } 17930 17931 static void 17932 tcp_send_data(tcp_t *tcp, queue_t *q, mblk_t *mp) 17933 { 17934 ipha_t *ipha; 17935 ipaddr_t src; 17936 ipaddr_t dst; 17937 uint32_t cksum; 17938 ire_t *ire; 17939 uint16_t *up; 17940 ill_t *ill; 17941 conn_t *connp = tcp->tcp_connp; 17942 uint32_t hcksum_txflags = 0; 17943 mblk_t *ire_fp_mp; 17944 uint_t ire_fp_mp_len; 17945 17946 ASSERT(DB_TYPE(mp) == M_DATA); 17947 17948 ipha = (ipha_t *)mp->b_rptr; 17949 src = ipha->ipha_src; 17950 dst = ipha->ipha_dst; 17951 17952 /* 17953 * Drop off slow path for IPv6 and also if options are present. 17954 */ 17955 if (tcp->tcp_ipversion != IPV4_VERSION || 17956 !IPCL_IS_CONNECTED(connp) || 17957 (connp->conn_flags & IPCL_CHECK_POLICY) != 0 || 17958 connp->conn_dontroute || 17959 connp->conn_nexthop_set || 17960 connp->conn_xmit_if_ill != NULL || 17961 connp->conn_nofailover_ill != NULL || 17962 ipha->ipha_ident == IP_HDR_INCLUDED || 17963 ipha->ipha_version_and_hdr_length != IP_SIMPLE_HDR_VERSION || 17964 IPP_ENABLED(IPP_LOCAL_OUT)) { 17965 if (tcp->tcp_snd_zcopy_aware) 17966 mp = tcp_zcopy_disable(tcp, mp); 17967 TCP_STAT(tcp_ip_send); 17968 CALL_IP_WPUT(connp, q, mp); 17969 return; 17970 } 17971 17972 mutex_enter(&connp->conn_lock); 17973 ire = connp->conn_ire_cache; 17974 ASSERT(!(connp->conn_state_flags & CONN_INCIPIENT)); 17975 if (ire != NULL && ire->ire_addr == dst && 17976 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 17977 IRE_REFHOLD(ire); 17978 mutex_exit(&connp->conn_lock); 17979 } else { 17980 boolean_t cached = B_FALSE; 17981 17982 /* force a recheck later on */ 17983 tcp->tcp_ire_ill_check_done = B_FALSE; 17984 17985 TCP_DBGSTAT(tcp_ire_null1); 17986 connp->conn_ire_cache = NULL; 17987 mutex_exit(&connp->conn_lock); 17988 if (ire != NULL) 17989 IRE_REFRELE_NOTR(ire); 17990 ire = ire_cache_lookup(dst, connp->conn_zoneid); 17991 if (ire == NULL) { 17992 if (tcp->tcp_snd_zcopy_aware) 17993 mp = tcp_zcopy_backoff(tcp, mp, 0); 17994 TCP_STAT(tcp_ire_null); 17995 CALL_IP_WPUT(connp, q, mp); 17996 return; 17997 } 17998 IRE_REFHOLD_NOTR(ire); 17999 /* 18000 * Since we are inside the squeue, there cannot be another 18001 * thread in TCP trying to set the conn_ire_cache now. The 18002 * check for IRE_MARK_CONDEMNED ensures that an interface 18003 * unplumb thread has not yet started cleaning up the conns. 18004 * Hence we don't need to grab the conn lock. 18005 */ 18006 if (!(connp->conn_state_flags & CONN_CLOSING)) { 18007 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18008 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18009 connp->conn_ire_cache = ire; 18010 cached = B_TRUE; 18011 } 18012 rw_exit(&ire->ire_bucket->irb_lock); 18013 } 18014 18015 /* 18016 * We can continue to use the ire but since it was 18017 * not cached, we should drop the extra reference. 18018 */ 18019 if (!cached) 18020 IRE_REFRELE_NOTR(ire); 18021 } 18022 18023 if (ire->ire_flags & RTF_MULTIRT || 18024 ire->ire_stq == NULL || 18025 ire->ire_max_frag < ntohs(ipha->ipha_length) || 18026 (ire_fp_mp = ire->ire_fp_mp) == NULL || 18027 (ire_fp_mp_len = MBLKL(ire_fp_mp)) > MBLKHEAD(mp)) { 18028 if (tcp->tcp_snd_zcopy_aware) 18029 mp = tcp_zcopy_disable(tcp, mp); 18030 TCP_STAT(tcp_ip_ire_send); 18031 IRE_REFRELE(ire); 18032 CALL_IP_WPUT(connp, q, mp); 18033 return; 18034 } 18035 18036 ill = ire_to_ill(ire); 18037 if (connp->conn_outgoing_ill != NULL) { 18038 ill_t *conn_outgoing_ill = NULL; 18039 /* 18040 * Choose a good ill in the group to send the packets on. 18041 */ 18042 ire = conn_set_outgoing_ill(connp, ire, &conn_outgoing_ill); 18043 ill = ire_to_ill(ire); 18044 } 18045 ASSERT(ill != NULL); 18046 18047 if (!tcp->tcp_ire_ill_check_done) { 18048 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18049 tcp->tcp_ire_ill_check_done = B_TRUE; 18050 } 18051 18052 ASSERT(ipha->ipha_ident == 0 || ipha->ipha_ident == IP_HDR_INCLUDED); 18053 ipha->ipha_ident = (uint16_t)atomic_add_32_nv(&ire->ire_ident, 1); 18054 #ifndef _BIG_ENDIAN 18055 ipha->ipha_ident = (ipha->ipha_ident << 8) | (ipha->ipha_ident >> 8); 18056 #endif 18057 18058 /* 18059 * Check to see if we need to re-enable MDT for this connection 18060 * because it was previously disabled due to changes in the ill; 18061 * note that by doing it here, this re-enabling only applies when 18062 * the packet is not dispatched through CALL_IP_WPUT(). 18063 * 18064 * That means for IPv4, it is worth re-enabling MDT for the fastpath 18065 * case, since that's how we ended up here. For IPv6, we do the 18066 * re-enabling work in ip_xmit_v6(), albeit indirectly via squeue. 18067 */ 18068 if (connp->conn_mdt_ok && !tcp->tcp_mdt && ILL_MDT_USABLE(ill)) { 18069 /* 18070 * Restore MDT for this connection, so that next time around 18071 * it is eligible to go through tcp_multisend() path again. 18072 */ 18073 TCP_STAT(tcp_mdt_conn_resumed1); 18074 tcp->tcp_mdt = B_TRUE; 18075 ip1dbg(("tcp_send_data: reenabling MDT for connp %p on " 18076 "interface %s\n", (void *)connp, ill->ill_name)); 18077 } 18078 18079 if (tcp->tcp_snd_zcopy_aware) { 18080 if ((ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) == 0 || 18081 (ill->ill_zerocopy_capab->ill_zerocopy_flags == 0)) 18082 mp = tcp_zcopy_disable(tcp, mp); 18083 /* 18084 * we shouldn't need to reset ipha as the mp containing 18085 * ipha should never be a zero-copy mp. 18086 */ 18087 } 18088 18089 if (ILL_HCKSUM_CAPABLE(ill) && dohwcksum) { 18090 ASSERT(ill->ill_hcksum_capab != NULL); 18091 hcksum_txflags = ill->ill_hcksum_capab->ill_hcksum_txflags; 18092 } 18093 18094 /* pseudo-header checksum (do it in parts for IP header checksum) */ 18095 cksum = (dst >> 16) + (dst & 0xFFFF) + (src >> 16) + (src & 0xFFFF); 18096 18097 ASSERT(ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION); 18098 up = IPH_TCPH_CHECKSUMP(ipha, IP_SIMPLE_HDR_LENGTH); 18099 18100 IP_CKSUM_XMIT_FAST(ire->ire_ipversion, hcksum_txflags, mp, ipha, up, 18101 IPPROTO_TCP, IP_SIMPLE_HDR_LENGTH, ntohs(ipha->ipha_length), cksum); 18102 18103 /* Software checksum? */ 18104 if (DB_CKSUMFLAGS(mp) == 0) { 18105 TCP_STAT(tcp_out_sw_cksum); 18106 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 18107 ntohs(ipha->ipha_length) - IP_SIMPLE_HDR_LENGTH); 18108 } 18109 18110 ipha->ipha_fragment_offset_and_flags |= 18111 (uint32_t)htons(ire->ire_frag_flag); 18112 18113 /* Calculate IP header checksum if hardware isn't capable */ 18114 if (!(DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM)) { 18115 IP_HDR_CKSUM(ipha, cksum, ((uint32_t *)ipha)[0], 18116 ((uint16_t *)ipha)[4]); 18117 } 18118 18119 ASSERT(DB_TYPE(ire_fp_mp) == M_DATA); 18120 mp->b_rptr = (uchar_t *)ipha - ire_fp_mp_len; 18121 bcopy(ire_fp_mp->b_rptr, mp->b_rptr, ire_fp_mp_len); 18122 18123 UPDATE_OB_PKT_COUNT(ire); 18124 ire->ire_last_used_time = lbolt; 18125 BUMP_MIB(&ip_mib, ipOutRequests); 18126 18127 if (ILL_DLS_CAPABLE(ill)) { 18128 /* 18129 * Send the packet directly to DLD, where it may be queued 18130 * depending on the availability of transmit resources at 18131 * the media layer. 18132 */ 18133 IP_DLS_ILL_TX(ill, mp); 18134 } else { 18135 putnext(ire->ire_stq, mp); 18136 } 18137 IRE_REFRELE(ire); 18138 } 18139 18140 /* 18141 * This handles the case when the receiver has shrunk its win. Per RFC 1122 18142 * if the receiver shrinks the window, i.e. moves the right window to the 18143 * left, the we should not send new data, but should retransmit normally the 18144 * old unacked data between suna and suna + swnd. We might has sent data 18145 * that is now outside the new window, pretend that we didn't send it. 18146 */ 18147 static void 18148 tcp_process_shrunk_swnd(tcp_t *tcp, uint32_t shrunk_count) 18149 { 18150 uint32_t snxt = tcp->tcp_snxt; 18151 mblk_t *xmit_tail; 18152 int32_t offset; 18153 18154 ASSERT(shrunk_count > 0); 18155 18156 /* Pretend we didn't send the data outside the window */ 18157 snxt -= shrunk_count; 18158 18159 /* Get the mblk and the offset in it per the shrunk window */ 18160 xmit_tail = tcp_get_seg_mp(tcp, snxt, &offset); 18161 18162 ASSERT(xmit_tail != NULL); 18163 18164 /* Reset all the values per the now shrunk window */ 18165 tcp->tcp_snxt = snxt; 18166 tcp->tcp_xmit_tail = xmit_tail; 18167 tcp->tcp_xmit_tail_unsent = xmit_tail->b_wptr - xmit_tail->b_rptr - 18168 offset; 18169 tcp->tcp_unsent += shrunk_count; 18170 18171 if (tcp->tcp_suna == tcp->tcp_snxt && tcp->tcp_swnd == 0) 18172 /* 18173 * Make sure the timer is running so that we will probe a zero 18174 * window. 18175 */ 18176 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18177 } 18178 18179 18180 /* 18181 * The TCP normal data output path. 18182 * NOTE: the logic of the fast path is duplicated from this function. 18183 */ 18184 static void 18185 tcp_wput_data(tcp_t *tcp, mblk_t *mp, boolean_t urgent) 18186 { 18187 int len; 18188 mblk_t *local_time; 18189 mblk_t *mp1; 18190 uint32_t snxt; 18191 int tail_unsent; 18192 int tcpstate; 18193 int usable = 0; 18194 mblk_t *xmit_tail; 18195 queue_t *q = tcp->tcp_wq; 18196 int32_t mss; 18197 int32_t num_sack_blk = 0; 18198 int32_t tcp_hdr_len; 18199 int32_t tcp_tcp_hdr_len; 18200 int mdt_thres; 18201 int rc; 18202 18203 tcpstate = tcp->tcp_state; 18204 if (mp == NULL) { 18205 /* 18206 * tcp_wput_data() with NULL mp should only be called when 18207 * there is unsent data. 18208 */ 18209 ASSERT(tcp->tcp_unsent > 0); 18210 /* Really tacky... but we need this for detached closes. */ 18211 len = tcp->tcp_unsent; 18212 goto data_null; 18213 } 18214 18215 #if CCS_STATS 18216 wrw_stats.tot.count++; 18217 wrw_stats.tot.bytes += msgdsize(mp); 18218 #endif 18219 ASSERT(mp->b_datap->db_type == M_DATA); 18220 /* 18221 * Don't allow data after T_ORDREL_REQ or T_DISCON_REQ, 18222 * or before a connection attempt has begun. 18223 */ 18224 if (tcpstate < TCPS_SYN_SENT || tcpstate > TCPS_CLOSE_WAIT || 18225 (tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18226 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) != 0) { 18227 #ifdef DEBUG 18228 cmn_err(CE_WARN, 18229 "tcp_wput_data: data after ordrel, %s", 18230 tcp_display(tcp, NULL, 18231 DISP_ADDR_AND_PORT)); 18232 #else 18233 if (tcp->tcp_debug) { 18234 (void) strlog(TCP_MOD_ID, 0, 1, 18235 SL_TRACE|SL_ERROR, 18236 "tcp_wput_data: data after ordrel, %s\n", 18237 tcp_display(tcp, NULL, 18238 DISP_ADDR_AND_PORT)); 18239 } 18240 #endif /* DEBUG */ 18241 } 18242 if (tcp->tcp_snd_zcopy_aware && 18243 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) != 0) 18244 tcp_zcopy_notify(tcp); 18245 freemsg(mp); 18246 if (tcp->tcp_flow_stopped && 18247 TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18248 tcp_clrqfull(tcp); 18249 } 18250 return; 18251 } 18252 18253 /* Strip empties */ 18254 for (;;) { 18255 ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= 18256 (uintptr_t)INT_MAX); 18257 len = (int)(mp->b_wptr - mp->b_rptr); 18258 if (len > 0) 18259 break; 18260 mp1 = mp; 18261 mp = mp->b_cont; 18262 freeb(mp1); 18263 if (!mp) { 18264 return; 18265 } 18266 } 18267 18268 /* If we are the first on the list ... */ 18269 if (tcp->tcp_xmit_head == NULL) { 18270 tcp->tcp_xmit_head = mp; 18271 tcp->tcp_xmit_tail = mp; 18272 tcp->tcp_xmit_tail_unsent = len; 18273 } else { 18274 /* If tiny tx and room in txq tail, pullup to save mblks. */ 18275 struct datab *dp; 18276 18277 mp1 = tcp->tcp_xmit_last; 18278 if (len < tcp_tx_pull_len && 18279 (dp = mp1->b_datap)->db_ref == 1 && 18280 dp->db_lim - mp1->b_wptr >= len) { 18281 ASSERT(len > 0); 18282 ASSERT(!mp1->b_cont); 18283 if (len == 1) { 18284 *mp1->b_wptr++ = *mp->b_rptr; 18285 } else { 18286 bcopy(mp->b_rptr, mp1->b_wptr, len); 18287 mp1->b_wptr += len; 18288 } 18289 if (mp1 == tcp->tcp_xmit_tail) 18290 tcp->tcp_xmit_tail_unsent += len; 18291 mp1->b_cont = mp->b_cont; 18292 if (tcp->tcp_snd_zcopy_aware && 18293 (mp->b_datap->db_struioflag & STRUIO_ZCNOTIFY)) 18294 mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY; 18295 freeb(mp); 18296 mp = mp1; 18297 } else { 18298 tcp->tcp_xmit_last->b_cont = mp; 18299 } 18300 len += tcp->tcp_unsent; 18301 } 18302 18303 /* Tack on however many more positive length mblks we have */ 18304 if ((mp1 = mp->b_cont) != NULL) { 18305 do { 18306 int tlen; 18307 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 18308 (uintptr_t)INT_MAX); 18309 tlen = (int)(mp1->b_wptr - mp1->b_rptr); 18310 if (tlen <= 0) { 18311 mp->b_cont = mp1->b_cont; 18312 freeb(mp1); 18313 } else { 18314 len += tlen; 18315 mp = mp1; 18316 } 18317 } while ((mp1 = mp->b_cont) != NULL); 18318 } 18319 tcp->tcp_xmit_last = mp; 18320 tcp->tcp_unsent = len; 18321 18322 if (urgent) 18323 usable = 1; 18324 18325 data_null: 18326 snxt = tcp->tcp_snxt; 18327 xmit_tail = tcp->tcp_xmit_tail; 18328 tail_unsent = tcp->tcp_xmit_tail_unsent; 18329 18330 /* 18331 * Note that tcp_mss has been adjusted to take into account the 18332 * timestamp option if applicable. Because SACK options do not 18333 * appear in every TCP segments and they are of variable lengths, 18334 * they cannot be included in tcp_mss. Thus we need to calculate 18335 * the actual segment length when we need to send a segment which 18336 * includes SACK options. 18337 */ 18338 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 18339 int32_t opt_len; 18340 18341 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 18342 tcp->tcp_num_sack_blk); 18343 opt_len = num_sack_blk * sizeof (sack_blk_t) + TCPOPT_NOP_LEN * 18344 2 + TCPOPT_HEADER_LEN; 18345 mss = tcp->tcp_mss - opt_len; 18346 tcp_hdr_len = tcp->tcp_hdr_len + opt_len; 18347 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + opt_len; 18348 } else { 18349 mss = tcp->tcp_mss; 18350 tcp_hdr_len = tcp->tcp_hdr_len; 18351 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 18352 } 18353 18354 if ((tcp->tcp_suna == snxt) && !tcp->tcp_localnet && 18355 (TICK_TO_MSEC(lbolt - tcp->tcp_last_recv_time) >= tcp->tcp_rto)) { 18356 SET_TCP_INIT_CWND(tcp, mss, tcp_slow_start_after_idle); 18357 } 18358 if (tcpstate == TCPS_SYN_RCVD) { 18359 /* 18360 * The three-way connection establishment handshake is not 18361 * complete yet. We want to queue the data for transmission 18362 * after entering ESTABLISHED state (RFC793). A jump to 18363 * "done" label effectively leaves data on the queue. 18364 */ 18365 goto done; 18366 } else { 18367 int usable_r = tcp->tcp_swnd; 18368 18369 /* 18370 * In the special case when cwnd is zero, which can only 18371 * happen if the connection is ECN capable, return now. 18372 * New segments is sent using tcp_timer(). The timer 18373 * is set in tcp_rput_data(). 18374 */ 18375 if (tcp->tcp_cwnd == 0) { 18376 /* 18377 * Note that tcp_cwnd is 0 before 3-way handshake is 18378 * finished. 18379 */ 18380 ASSERT(tcp->tcp_ecn_ok || 18381 tcp->tcp_state < TCPS_ESTABLISHED); 18382 return; 18383 } 18384 18385 /* NOTE: trouble if xmitting while SYN not acked? */ 18386 usable_r -= snxt; 18387 usable_r += tcp->tcp_suna; 18388 18389 /* 18390 * Check if the receiver has shrunk the window. If 18391 * tcp_wput_data() with NULL mp is called, tcp_fin_sent 18392 * cannot be set as there is unsent data, so FIN cannot 18393 * be sent out. Otherwise, we need to take into account 18394 * of FIN as it consumes an "invisible" sequence number. 18395 */ 18396 ASSERT(tcp->tcp_fin_sent == 0); 18397 if (usable_r < 0) { 18398 /* 18399 * The receiver has shrunk the window and we have sent 18400 * -usable_r date beyond the window, re-adjust. 18401 * 18402 * If TCP window scaling is enabled, there can be 18403 * round down error as the advertised receive window 18404 * is actually right shifted n bits. This means that 18405 * the lower n bits info is wiped out. It will look 18406 * like the window is shrunk. Do a check here to 18407 * see if the shrunk amount is actually within the 18408 * error in window calculation. If it is, just 18409 * return. Note that this check is inside the 18410 * shrunk window check. This makes sure that even 18411 * though tcp_process_shrunk_swnd() is not called, 18412 * we will stop further processing. 18413 */ 18414 if ((-usable_r >> tcp->tcp_snd_ws) > 0) { 18415 tcp_process_shrunk_swnd(tcp, -usable_r); 18416 } 18417 return; 18418 } 18419 18420 /* usable = MIN(swnd, cwnd) - unacked_bytes */ 18421 if (tcp->tcp_swnd > tcp->tcp_cwnd) 18422 usable_r -= tcp->tcp_swnd - tcp->tcp_cwnd; 18423 18424 /* usable = MIN(usable, unsent) */ 18425 if (usable_r > len) 18426 usable_r = len; 18427 18428 /* usable = MAX(usable, {1 for urgent, 0 for data}) */ 18429 if (usable_r > 0) { 18430 usable = usable_r; 18431 } else { 18432 /* Bypass all other unnecessary processing. */ 18433 goto done; 18434 } 18435 } 18436 18437 local_time = (mblk_t *)lbolt; 18438 18439 /* 18440 * "Our" Nagle Algorithm. This is not the same as in the old 18441 * BSD. This is more in line with the true intent of Nagle. 18442 * 18443 * The conditions are: 18444 * 1. The amount of unsent data (or amount of data which can be 18445 * sent, whichever is smaller) is less than Nagle limit. 18446 * 2. The last sent size is also less than Nagle limit. 18447 * 3. There is unack'ed data. 18448 * 4. Urgent pointer is not set. Send urgent data ignoring the 18449 * Nagle algorithm. This reduces the probability that urgent 18450 * bytes get "merged" together. 18451 * 5. The app has not closed the connection. This eliminates the 18452 * wait time of the receiving side waiting for the last piece of 18453 * (small) data. 18454 * 18455 * If all are satisified, exit without sending anything. Note 18456 * that Nagle limit can be smaller than 1 MSS. Nagle limit is 18457 * the smaller of 1 MSS and global tcp_naglim_def (default to be 18458 * 4095). 18459 */ 18460 if (usable < (int)tcp->tcp_naglim && 18461 tcp->tcp_naglim > tcp->tcp_last_sent_len && 18462 snxt != tcp->tcp_suna && 18463 !(tcp->tcp_valid_bits & TCP_URG_VALID) && 18464 !(tcp->tcp_valid_bits & TCP_FSS_VALID)) { 18465 goto done; 18466 } 18467 18468 if (tcp->tcp_cork) { 18469 /* 18470 * if the tcp->tcp_cork option is set, then we have to force 18471 * TCP not to send partial segment (smaller than MSS bytes). 18472 * We are calculating the usable now based on full mss and 18473 * will save the rest of remaining data for later. 18474 */ 18475 if (usable < mss) 18476 goto done; 18477 usable = (usable / mss) * mss; 18478 } 18479 18480 /* Update the latest receive window size in TCP header. */ 18481 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 18482 tcp->tcp_tcph->th_win); 18483 18484 /* 18485 * Determine if it's worthwhile to attempt MDT, based on: 18486 * 18487 * 1. Simple TCP/IP{v4,v6} (no options). 18488 * 2. IPSEC/IPQoS processing is not needed for the TCP connection. 18489 * 3. If the TCP connection is in ESTABLISHED state. 18490 * 4. The TCP is not detached. 18491 * 18492 * If any of the above conditions have changed during the 18493 * connection, stop using MDT and restore the stream head 18494 * parameters accordingly. 18495 */ 18496 if (tcp->tcp_mdt && 18497 ((tcp->tcp_ipversion == IPV4_VERSION && 18498 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 18499 (tcp->tcp_ipversion == IPV6_VERSION && 18500 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN) || 18501 tcp->tcp_state != TCPS_ESTABLISHED || 18502 TCP_IS_DETACHED(tcp) || !CONN_IS_MD_FASTPATH(tcp->tcp_connp) || 18503 CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp) || 18504 IPP_ENABLED(IPP_LOCAL_OUT))) { 18505 tcp->tcp_connp->conn_mdt_ok = B_FALSE; 18506 tcp->tcp_mdt = B_FALSE; 18507 18508 /* Anything other than detached is considered pathological */ 18509 if (!TCP_IS_DETACHED(tcp)) { 18510 TCP_STAT(tcp_mdt_conn_halted1); 18511 (void) tcp_maxpsz_set(tcp, B_TRUE); 18512 } 18513 } 18514 18515 /* Use MDT if sendable amount is greater than the threshold */ 18516 if (tcp->tcp_mdt && 18517 (mdt_thres = mss << tcp_mdt_smss_threshold, usable > mdt_thres) && 18518 (tail_unsent > mdt_thres || (xmit_tail->b_cont != NULL && 18519 MBLKL(xmit_tail->b_cont) > mdt_thres)) && 18520 (tcp->tcp_valid_bits == 0 || 18521 tcp->tcp_valid_bits == TCP_FSS_VALID)) { 18522 ASSERT(tcp->tcp_connp->conn_mdt_ok); 18523 rc = tcp_multisend(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18524 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18525 local_time, mdt_thres); 18526 } else { 18527 rc = tcp_send(q, tcp, mss, tcp_hdr_len, tcp_tcp_hdr_len, 18528 num_sack_blk, &usable, &snxt, &tail_unsent, &xmit_tail, 18529 local_time, INT_MAX); 18530 } 18531 18532 /* Pretend that all we were trying to send really got sent */ 18533 if (rc < 0 && tail_unsent < 0) { 18534 do { 18535 xmit_tail = xmit_tail->b_cont; 18536 xmit_tail->b_prev = local_time; 18537 ASSERT((uintptr_t)(xmit_tail->b_wptr - 18538 xmit_tail->b_rptr) <= (uintptr_t)INT_MAX); 18539 tail_unsent += (int)(xmit_tail->b_wptr - 18540 xmit_tail->b_rptr); 18541 } while (tail_unsent < 0); 18542 } 18543 done:; 18544 tcp->tcp_xmit_tail = xmit_tail; 18545 tcp->tcp_xmit_tail_unsent = tail_unsent; 18546 len = tcp->tcp_snxt - snxt; 18547 if (len) { 18548 /* 18549 * If new data was sent, need to update the notsack 18550 * list, which is, afterall, data blocks that have 18551 * not been sack'ed by the receiver. New data is 18552 * not sack'ed. 18553 */ 18554 if (tcp->tcp_snd_sack_ok && tcp->tcp_notsack_list != NULL) { 18555 /* len is a negative value. */ 18556 tcp->tcp_pipe -= len; 18557 tcp_notsack_update(&(tcp->tcp_notsack_list), 18558 tcp->tcp_snxt, snxt, 18559 &(tcp->tcp_num_notsack_blk), 18560 &(tcp->tcp_cnt_notsack_list)); 18561 } 18562 tcp->tcp_snxt = snxt + tcp->tcp_fin_sent; 18563 tcp->tcp_rack = tcp->tcp_rnxt; 18564 tcp->tcp_rack_cnt = 0; 18565 if ((snxt + len) == tcp->tcp_suna) { 18566 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18567 } 18568 } else if (snxt == tcp->tcp_suna && tcp->tcp_swnd == 0) { 18569 /* 18570 * Didn't send anything. Make sure the timer is running 18571 * so that we will probe a zero window. 18572 */ 18573 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 18574 } 18575 /* Note that len is the amount we just sent but with a negative sign */ 18576 tcp->tcp_unsent += len; 18577 if (tcp->tcp_flow_stopped) { 18578 if (TCP_UNSENT_BYTES(tcp) <= tcp->tcp_xmit_lowater) { 18579 tcp_clrqfull(tcp); 18580 } 18581 } else if (TCP_UNSENT_BYTES(tcp) >= tcp->tcp_xmit_hiwater) { 18582 tcp_setqfull(tcp); 18583 } 18584 } 18585 18586 /* 18587 * tcp_fill_header is called by tcp_send() and tcp_multisend() to fill the 18588 * outgoing TCP header with the template header, as well as other 18589 * options such as time-stamp, ECN and/or SACK. 18590 */ 18591 static void 18592 tcp_fill_header(tcp_t *tcp, uchar_t *rptr, clock_t now, int num_sack_blk) 18593 { 18594 tcph_t *tcp_tmpl, *tcp_h; 18595 uint32_t *dst, *src; 18596 int hdrlen; 18597 18598 ASSERT(OK_32PTR(rptr)); 18599 18600 /* Template header */ 18601 tcp_tmpl = tcp->tcp_tcph; 18602 18603 /* Header of outgoing packet */ 18604 tcp_h = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 18605 18606 /* dst and src are opaque 32-bit fields, used for copying */ 18607 dst = (uint32_t *)rptr; 18608 src = (uint32_t *)tcp->tcp_iphc; 18609 hdrlen = tcp->tcp_hdr_len; 18610 18611 /* Fill time-stamp option if needed */ 18612 if (tcp->tcp_snd_ts_ok) { 18613 U32_TO_BE32((uint32_t)now, 18614 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 4); 18615 U32_TO_BE32(tcp->tcp_ts_recent, 18616 (char *)tcp_tmpl + TCP_MIN_HEADER_LENGTH + 8); 18617 } else { 18618 ASSERT(tcp->tcp_tcp_hdr_len == TCP_MIN_HEADER_LENGTH); 18619 } 18620 18621 /* 18622 * Copy the template header; is this really more efficient than 18623 * calling bcopy()? For simple IPv4/TCP, it may be the case, 18624 * but perhaps not for other scenarios. 18625 */ 18626 dst[0] = src[0]; 18627 dst[1] = src[1]; 18628 dst[2] = src[2]; 18629 dst[3] = src[3]; 18630 dst[4] = src[4]; 18631 dst[5] = src[5]; 18632 dst[6] = src[6]; 18633 dst[7] = src[7]; 18634 dst[8] = src[8]; 18635 dst[9] = src[9]; 18636 if (hdrlen -= 40) { 18637 hdrlen >>= 2; 18638 dst += 10; 18639 src += 10; 18640 do { 18641 *dst++ = *src++; 18642 } while (--hdrlen); 18643 } 18644 18645 /* 18646 * Set the ECN info in the TCP header if it is not a zero 18647 * window probe. Zero window probe is only sent in 18648 * tcp_wput_data() and tcp_timer(). 18649 */ 18650 if (tcp->tcp_ecn_ok && !tcp->tcp_zero_win_probe) { 18651 SET_ECT(tcp, rptr); 18652 18653 if (tcp->tcp_ecn_echo_on) 18654 tcp_h->th_flags[0] |= TH_ECE; 18655 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 18656 tcp_h->th_flags[0] |= TH_CWR; 18657 tcp->tcp_ecn_cwr_sent = B_TRUE; 18658 } 18659 } 18660 18661 /* Fill in SACK options */ 18662 if (num_sack_blk > 0) { 18663 uchar_t *wptr = rptr + tcp->tcp_hdr_len; 18664 sack_blk_t *tmp; 18665 int32_t i; 18666 18667 wptr[0] = TCPOPT_NOP; 18668 wptr[1] = TCPOPT_NOP; 18669 wptr[2] = TCPOPT_SACK; 18670 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 18671 sizeof (sack_blk_t); 18672 wptr += TCPOPT_REAL_SACK_LEN; 18673 18674 tmp = tcp->tcp_sack_list; 18675 for (i = 0; i < num_sack_blk; i++) { 18676 U32_TO_BE32(tmp[i].begin, wptr); 18677 wptr += sizeof (tcp_seq); 18678 U32_TO_BE32(tmp[i].end, wptr); 18679 wptr += sizeof (tcp_seq); 18680 } 18681 tcp_h->th_offset_and_rsrvd[0] += 18682 ((num_sack_blk * 2 + 1) << 4); 18683 } 18684 } 18685 18686 /* 18687 * tcp_mdt_add_attrs() is called by tcp_multisend() in order to attach 18688 * the destination address and SAP attribute, and if necessary, the 18689 * hardware checksum offload attribute to a Multidata message. 18690 */ 18691 static int 18692 tcp_mdt_add_attrs(multidata_t *mmd, const mblk_t *dlmp, const boolean_t hwcksum, 18693 const uint32_t start, const uint32_t stuff, const uint32_t end, 18694 const uint32_t flags) 18695 { 18696 /* Add global destination address & SAP attribute */ 18697 if (dlmp == NULL || !ip_md_addr_attr(mmd, NULL, dlmp)) { 18698 ip1dbg(("tcp_mdt_add_attrs: can't add global physical " 18699 "destination address+SAP\n")); 18700 18701 if (dlmp != NULL) 18702 TCP_STAT(tcp_mdt_allocfail); 18703 return (-1); 18704 } 18705 18706 /* Add global hwcksum attribute */ 18707 if (hwcksum && 18708 !ip_md_hcksum_attr(mmd, NULL, start, stuff, end, flags)) { 18709 ip1dbg(("tcp_mdt_add_attrs: can't add global hardware " 18710 "checksum attribute\n")); 18711 18712 TCP_STAT(tcp_mdt_allocfail); 18713 return (-1); 18714 } 18715 18716 return (0); 18717 } 18718 18719 /* 18720 * Smaller and private version of pdescinfo_t used specifically for TCP, 18721 * which allows for only two payload spans per packet. 18722 */ 18723 typedef struct tcp_pdescinfo_s PDESCINFO_STRUCT(2) tcp_pdescinfo_t; 18724 18725 /* 18726 * tcp_multisend() is called by tcp_wput_data() for Multidata Transmit 18727 * scheme, and returns one the following: 18728 * 18729 * -1 = failed allocation. 18730 * 0 = success; burst count reached, or usable send window is too small, 18731 * and that we'd rather wait until later before sending again. 18732 */ 18733 static int 18734 tcp_multisend(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 18735 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 18736 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 18737 const int mdt_thres) 18738 { 18739 mblk_t *md_mp_head, *md_mp, *md_pbuf, *md_pbuf_nxt, *md_hbuf; 18740 multidata_t *mmd; 18741 uint_t obsegs, obbytes, hdr_frag_sz; 18742 uint_t cur_hdr_off, cur_pld_off, base_pld_off, first_snxt; 18743 int num_burst_seg, max_pld; 18744 pdesc_t *pkt; 18745 tcp_pdescinfo_t tcp_pkt_info; 18746 pdescinfo_t *pkt_info; 18747 int pbuf_idx, pbuf_idx_nxt; 18748 int seg_len, len, spill, af; 18749 boolean_t add_buffer, zcopy, clusterwide; 18750 boolean_t rconfirm = B_FALSE; 18751 boolean_t done = B_FALSE; 18752 uint32_t cksum; 18753 uint32_t hwcksum_flags; 18754 ire_t *ire; 18755 ill_t *ill; 18756 ipha_t *ipha; 18757 ip6_t *ip6h; 18758 ipaddr_t src, dst; 18759 ill_zerocopy_capab_t *zc_cap = NULL; 18760 uint16_t *up; 18761 int err; 18762 18763 #ifdef _BIG_ENDIAN 18764 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 28) & 0x7) 18765 #else 18766 #define IPVER(ip6h) ((((uint32_t *)ip6h)[0] >> 4) & 0x7) 18767 #endif 18768 18769 #define PREP_NEW_MULTIDATA() { \ 18770 mmd = NULL; \ 18771 md_mp = md_hbuf = NULL; \ 18772 cur_hdr_off = 0; \ 18773 max_pld = tcp->tcp_mdt_max_pld; \ 18774 pbuf_idx = pbuf_idx_nxt = -1; \ 18775 add_buffer = B_TRUE; \ 18776 zcopy = B_FALSE; \ 18777 } 18778 18779 #define PREP_NEW_PBUF() { \ 18780 md_pbuf = md_pbuf_nxt = NULL; \ 18781 pbuf_idx = pbuf_idx_nxt = -1; \ 18782 cur_pld_off = 0; \ 18783 first_snxt = *snxt; \ 18784 ASSERT(*tail_unsent > 0); \ 18785 base_pld_off = MBLKL(*xmit_tail) - *tail_unsent; \ 18786 } 18787 18788 ASSERT(mdt_thres >= mss); 18789 ASSERT(*usable > 0 && *usable > mdt_thres); 18790 ASSERT(tcp->tcp_state == TCPS_ESTABLISHED); 18791 ASSERT(!TCP_IS_DETACHED(tcp)); 18792 ASSERT(tcp->tcp_valid_bits == 0 || 18793 tcp->tcp_valid_bits == TCP_FSS_VALID); 18794 ASSERT((tcp->tcp_ipversion == IPV4_VERSION && 18795 tcp->tcp_ip_hdr_len == IP_SIMPLE_HDR_LENGTH) || 18796 (tcp->tcp_ipversion == IPV6_VERSION && 18797 tcp->tcp_ip_hdr_len == IPV6_HDR_LEN)); 18798 ASSERT(tcp->tcp_connp != NULL); 18799 ASSERT(CONN_IS_MD_FASTPATH(tcp->tcp_connp)); 18800 ASSERT(!CONN_IPSEC_OUT_ENCAPSULATED(tcp->tcp_connp)); 18801 18802 /* 18803 * Note that tcp will only declare at most 2 payload spans per 18804 * packet, which is much lower than the maximum allowable number 18805 * of packet spans per Multidata. For this reason, we use the 18806 * privately declared and smaller descriptor info structure, in 18807 * order to save some stack space. 18808 */ 18809 pkt_info = (pdescinfo_t *)&tcp_pkt_info; 18810 18811 af = (tcp->tcp_ipversion == IPV4_VERSION) ? AF_INET : AF_INET6; 18812 if (af == AF_INET) { 18813 dst = tcp->tcp_ipha->ipha_dst; 18814 src = tcp->tcp_ipha->ipha_src; 18815 ASSERT(!CLASSD(dst)); 18816 } 18817 ASSERT(af == AF_INET || 18818 !IN6_IS_ADDR_MULTICAST(&tcp->tcp_ip6h->ip6_dst)); 18819 18820 obsegs = obbytes = 0; 18821 num_burst_seg = tcp->tcp_snd_burst; 18822 md_mp_head = NULL; 18823 PREP_NEW_MULTIDATA(); 18824 18825 /* 18826 * Before we go on further, make sure there is an IRE that we can 18827 * use, and that the ILL supports MDT. Otherwise, there's no point 18828 * in proceeding any further, and we should just hand everything 18829 * off to the legacy path. 18830 */ 18831 mutex_enter(&tcp->tcp_connp->conn_lock); 18832 ire = tcp->tcp_connp->conn_ire_cache; 18833 ASSERT(!(tcp->tcp_connp->conn_state_flags & CONN_INCIPIENT)); 18834 if (ire != NULL && ((af == AF_INET && ire->ire_addr == dst) || 18835 (af == AF_INET6 && IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, 18836 &tcp->tcp_ip6h->ip6_dst))) && 18837 !(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18838 IRE_REFHOLD(ire); 18839 mutex_exit(&tcp->tcp_connp->conn_lock); 18840 } else { 18841 boolean_t cached = B_FALSE; 18842 18843 /* force a recheck later on */ 18844 tcp->tcp_ire_ill_check_done = B_FALSE; 18845 18846 TCP_DBGSTAT(tcp_ire_null1); 18847 tcp->tcp_connp->conn_ire_cache = NULL; 18848 mutex_exit(&tcp->tcp_connp->conn_lock); 18849 18850 /* Release the old ire */ 18851 if (ire != NULL) 18852 IRE_REFRELE_NOTR(ire); 18853 18854 ire = (af == AF_INET) ? 18855 ire_cache_lookup(dst, tcp->tcp_connp->conn_zoneid) : 18856 ire_cache_lookup_v6(&tcp->tcp_ip6h->ip6_dst, 18857 tcp->tcp_connp->conn_zoneid); 18858 18859 if (ire == NULL) { 18860 TCP_STAT(tcp_ire_null); 18861 goto legacy_send_no_md; 18862 } 18863 18864 IRE_REFHOLD_NOTR(ire); 18865 /* 18866 * Since we are inside the squeue, there cannot be another 18867 * thread in TCP trying to set the conn_ire_cache now. The 18868 * check for IRE_MARK_CONDEMNED ensures that an interface 18869 * unplumb thread has not yet started cleaning up the conns. 18870 * Hence we don't need to grab the conn lock. 18871 */ 18872 if (!(tcp->tcp_connp->conn_state_flags & CONN_CLOSING)) { 18873 rw_enter(&ire->ire_bucket->irb_lock, RW_READER); 18874 if (!(ire->ire_marks & IRE_MARK_CONDEMNED)) { 18875 tcp->tcp_connp->conn_ire_cache = ire; 18876 cached = B_TRUE; 18877 } 18878 rw_exit(&ire->ire_bucket->irb_lock); 18879 } 18880 18881 /* 18882 * We can continue to use the ire but since it was not 18883 * cached, we should drop the extra reference. 18884 */ 18885 if (!cached) 18886 IRE_REFRELE_NOTR(ire); 18887 } 18888 18889 ASSERT(ire != NULL); 18890 ASSERT(af != AF_INET || ire->ire_ipversion == IPV4_VERSION); 18891 ASSERT(af == AF_INET || !IN6_IS_ADDR_V4MAPPED(&(ire->ire_addr_v6))); 18892 ASSERT(af == AF_INET || ire->ire_nce != NULL); 18893 ASSERT(!(ire->ire_type & IRE_BROADCAST)); 18894 /* 18895 * If we do support loopback for MDT (which requires modifications 18896 * to the receiving paths), the following assertions should go away, 18897 * and we would be sending the Multidata to loopback conn later on. 18898 */ 18899 ASSERT(!IRE_IS_LOCAL(ire)); 18900 ASSERT(ire->ire_stq != NULL); 18901 18902 ill = ire_to_ill(ire); 18903 ASSERT(ill != NULL); 18904 ASSERT(!ILL_MDT_CAPABLE(ill) || ill->ill_mdt_capab != NULL); 18905 18906 if (!tcp->tcp_ire_ill_check_done) { 18907 tcp_ire_ill_check(tcp, ire, ill, B_TRUE); 18908 tcp->tcp_ire_ill_check_done = B_TRUE; 18909 } 18910 18911 /* 18912 * If the underlying interface conditions have changed, or if the 18913 * new interface does not support MDT, go back to legacy path. 18914 */ 18915 if (!ILL_MDT_USABLE(ill) || (ire->ire_flags & RTF_MULTIRT) != 0) { 18916 /* don't go through this path anymore for this connection */ 18917 TCP_STAT(tcp_mdt_conn_halted2); 18918 tcp->tcp_mdt = B_FALSE; 18919 ip1dbg(("tcp_multisend: disabling MDT for connp %p on " 18920 "interface %s\n", (void *)tcp->tcp_connp, ill->ill_name)); 18921 /* IRE will be released prior to returning */ 18922 goto legacy_send_no_md; 18923 } 18924 18925 if (ill->ill_capabilities & ILL_CAPAB_ZEROCOPY) 18926 zc_cap = ill->ill_zerocopy_capab; 18927 18928 /* go to legacy path if interface doesn't support zerocopy */ 18929 if (tcp->tcp_snd_zcopy_aware && do_tcpzcopy != 2 && 18930 (zc_cap == NULL || zc_cap->ill_zerocopy_flags == 0)) { 18931 /* IRE will be released prior to returning */ 18932 goto legacy_send_no_md; 18933 } 18934 18935 /* does the interface support hardware checksum offload? */ 18936 hwcksum_flags = 0; 18937 if (ILL_HCKSUM_CAPABLE(ill) && 18938 (ill->ill_hcksum_capab->ill_hcksum_txflags & 18939 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6 | HCKSUM_INET_PARTIAL | 18940 HCKSUM_IPHDRCKSUM)) && dohwcksum) { 18941 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 18942 HCKSUM_IPHDRCKSUM) 18943 hwcksum_flags = HCK_IPV4_HDRCKSUM; 18944 18945 if (ill->ill_hcksum_capab->ill_hcksum_txflags & 18946 (HCKSUM_INET_FULL_V4 | HCKSUM_INET_FULL_V6)) 18947 hwcksum_flags |= HCK_FULLCKSUM; 18948 else if (ill->ill_hcksum_capab->ill_hcksum_txflags & 18949 HCKSUM_INET_PARTIAL) 18950 hwcksum_flags |= HCK_PARTIALCKSUM; 18951 } 18952 18953 /* 18954 * Each header fragment consists of the leading extra space, 18955 * followed by the TCP/IP header, and the trailing extra space. 18956 * We make sure that each header fragment begins on a 32-bit 18957 * aligned memory address (tcp_mdt_hdr_head is already 32-bit 18958 * aligned in tcp_mdt_update). 18959 */ 18960 hdr_frag_sz = roundup((tcp->tcp_mdt_hdr_head + tcp_hdr_len + 18961 tcp->tcp_mdt_hdr_tail), 4); 18962 18963 /* are we starting from the beginning of data block? */ 18964 if (*tail_unsent == 0) { 18965 *xmit_tail = (*xmit_tail)->b_cont; 18966 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= (uintptr_t)INT_MAX); 18967 *tail_unsent = (int)MBLKL(*xmit_tail); 18968 } 18969 18970 /* 18971 * Here we create one or more Multidata messages, each made up of 18972 * one header buffer and up to N payload buffers. This entire 18973 * operation is done within two loops: 18974 * 18975 * The outer loop mostly deals with creating the Multidata message, 18976 * as well as the header buffer that gets added to it. It also 18977 * links the Multidata messages together such that all of them can 18978 * be sent down to the lower layer in a single putnext call; this 18979 * linking behavior depends on the tcp_mdt_chain tunable. 18980 * 18981 * The inner loop takes an existing Multidata message, and adds 18982 * one or more (up to tcp_mdt_max_pld) payload buffers to it. It 18983 * packetizes those buffers by filling up the corresponding header 18984 * buffer fragments with the proper IP and TCP headers, and by 18985 * describing the layout of each packet in the packet descriptors 18986 * that get added to the Multidata. 18987 */ 18988 do { 18989 /* 18990 * If usable send window is too small, or data blocks in 18991 * transmit list are smaller than our threshold (i.e. app 18992 * performs large writes followed by small ones), we hand 18993 * off the control over to the legacy path. Note that we'll 18994 * get back the control once it encounters a large block. 18995 */ 18996 if (*usable < mss || (*tail_unsent <= mdt_thres && 18997 (*xmit_tail)->b_cont != NULL && 18998 MBLKL((*xmit_tail)->b_cont) <= mdt_thres)) { 18999 /* send down what we've got so far */ 19000 if (md_mp_head != NULL) { 19001 tcp_multisend_data(tcp, ire, ill, md_mp_head, 19002 obsegs, obbytes, &rconfirm); 19003 } 19004 /* 19005 * Pass control over to tcp_send(), but tell it to 19006 * return to us once a large-size transmission is 19007 * possible. 19008 */ 19009 TCP_STAT(tcp_mdt_legacy_small); 19010 if ((err = tcp_send(q, tcp, mss, tcp_hdr_len, 19011 tcp_tcp_hdr_len, num_sack_blk, usable, snxt, 19012 tail_unsent, xmit_tail, local_time, 19013 mdt_thres)) <= 0) { 19014 /* burst count reached, or alloc failed */ 19015 IRE_REFRELE(ire); 19016 return (err); 19017 } 19018 19019 /* tcp_send() may have sent everything, so check */ 19020 if (*usable <= 0) { 19021 IRE_REFRELE(ire); 19022 return (0); 19023 } 19024 19025 TCP_STAT(tcp_mdt_legacy_ret); 19026 /* 19027 * We may have delivered the Multidata, so make sure 19028 * to re-initialize before the next round. 19029 */ 19030 md_mp_head = NULL; 19031 obsegs = obbytes = 0; 19032 num_burst_seg = tcp->tcp_snd_burst; 19033 PREP_NEW_MULTIDATA(); 19034 19035 /* are we starting from the beginning of data block? */ 19036 if (*tail_unsent == 0) { 19037 *xmit_tail = (*xmit_tail)->b_cont; 19038 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19039 (uintptr_t)INT_MAX); 19040 *tail_unsent = (int)MBLKL(*xmit_tail); 19041 } 19042 } 19043 19044 /* 19045 * max_pld limits the number of mblks in tcp's transmit 19046 * queue that can be added to a Multidata message. Once 19047 * this counter reaches zero, no more additional mblks 19048 * can be added to it. What happens afterwards depends 19049 * on whether or not we are set to chain the Multidata 19050 * messages. If we are to link them together, reset 19051 * max_pld to its original value (tcp_mdt_max_pld) and 19052 * prepare to create a new Multidata message which will 19053 * get linked to md_mp_head. Else, leave it alone and 19054 * let the inner loop break on its own. 19055 */ 19056 if (tcp_mdt_chain && max_pld == 0) 19057 PREP_NEW_MULTIDATA(); 19058 19059 /* adding a payload buffer; re-initialize values */ 19060 if (add_buffer) 19061 PREP_NEW_PBUF(); 19062 19063 /* 19064 * If we don't have a Multidata, either because we just 19065 * (re)entered this outer loop, or after we branched off 19066 * to tcp_send above, setup the Multidata and header 19067 * buffer to be used. 19068 */ 19069 if (md_mp == NULL) { 19070 int md_hbuflen; 19071 uint32_t start, stuff; 19072 19073 /* 19074 * Calculate Multidata header buffer size large enough 19075 * to hold all of the headers that can possibly be 19076 * sent at this moment. We'd rather over-estimate 19077 * the size than running out of space; this is okay 19078 * since this buffer is small anyway. 19079 */ 19080 md_hbuflen = (howmany(*usable, mss) + 1) * hdr_frag_sz; 19081 19082 /* 19083 * Start and stuff offset for partial hardware 19084 * checksum offload; these are currently for IPv4. 19085 * For full checksum offload, they are set to zero. 19086 */ 19087 if ((hwcksum_flags & HCK_PARTIALCKSUM)) { 19088 if (af == AF_INET) { 19089 start = IP_SIMPLE_HDR_LENGTH; 19090 stuff = IP_SIMPLE_HDR_LENGTH + 19091 TCP_CHECKSUM_OFFSET; 19092 } else { 19093 start = IPV6_HDR_LEN; 19094 stuff = IPV6_HDR_LEN + 19095 TCP_CHECKSUM_OFFSET; 19096 } 19097 } else { 19098 start = stuff = 0; 19099 } 19100 19101 /* 19102 * Create the header buffer, Multidata, as well as 19103 * any necessary attributes (destination address, 19104 * SAP and hardware checksum offload) that should 19105 * be associated with the Multidata message. 19106 */ 19107 ASSERT(cur_hdr_off == 0); 19108 if ((md_hbuf = allocb(md_hbuflen, BPRI_HI)) == NULL || 19109 ((md_hbuf->b_wptr += md_hbuflen), 19110 (mmd = mmd_alloc(md_hbuf, &md_mp, 19111 KM_NOSLEEP)) == NULL) || (tcp_mdt_add_attrs(mmd, 19112 /* fastpath mblk */ 19113 (af == AF_INET) ? ire->ire_dlureq_mp : 19114 ire->ire_nce->nce_res_mp, 19115 /* hardware checksum enabled */ 19116 (hwcksum_flags & (HCK_FULLCKSUM|HCK_PARTIALCKSUM)), 19117 /* hardware checksum offsets */ 19118 start, stuff, 0, 19119 /* hardware checksum flag */ 19120 hwcksum_flags) != 0)) { 19121 legacy_send: 19122 if (md_mp != NULL) { 19123 /* Unlink message from the chain */ 19124 if (md_mp_head != NULL) { 19125 err = (intptr_t)rmvb(md_mp_head, 19126 md_mp); 19127 /* 19128 * We can't assert that rmvb 19129 * did not return -1, since we 19130 * may get here before linkb 19131 * happens. We do, however, 19132 * check if we just removed the 19133 * only element in the list. 19134 */ 19135 if (err == 0) 19136 md_mp_head = NULL; 19137 } 19138 /* md_hbuf gets freed automatically */ 19139 TCP_STAT(tcp_mdt_discarded); 19140 freeb(md_mp); 19141 } else { 19142 /* Either allocb or mmd_alloc failed */ 19143 TCP_STAT(tcp_mdt_allocfail); 19144 if (md_hbuf != NULL) 19145 freeb(md_hbuf); 19146 } 19147 19148 /* send down what we've got so far */ 19149 if (md_mp_head != NULL) { 19150 tcp_multisend_data(tcp, ire, ill, 19151 md_mp_head, obsegs, obbytes, 19152 &rconfirm); 19153 } 19154 legacy_send_no_md: 19155 if (ire != NULL) 19156 IRE_REFRELE(ire); 19157 /* 19158 * Too bad; let the legacy path handle this. 19159 * We specify INT_MAX for the threshold, since 19160 * we gave up with the Multidata processings 19161 * and let the old path have it all. 19162 */ 19163 TCP_STAT(tcp_mdt_legacy_all); 19164 return (tcp_send(q, tcp, mss, tcp_hdr_len, 19165 tcp_tcp_hdr_len, num_sack_blk, usable, 19166 snxt, tail_unsent, xmit_tail, local_time, 19167 INT_MAX)); 19168 } 19169 19170 /* link to any existing ones, if applicable */ 19171 TCP_STAT(tcp_mdt_allocd); 19172 if (md_mp_head == NULL) { 19173 md_mp_head = md_mp; 19174 } else if (tcp_mdt_chain) { 19175 TCP_STAT(tcp_mdt_linked); 19176 linkb(md_mp_head, md_mp); 19177 } 19178 } 19179 19180 ASSERT(md_mp_head != NULL); 19181 ASSERT(tcp_mdt_chain || md_mp_head->b_cont == NULL); 19182 ASSERT(md_mp != NULL && mmd != NULL); 19183 ASSERT(md_hbuf != NULL); 19184 19185 /* 19186 * Packetize the transmittable portion of the data block; 19187 * each data block is essentially added to the Multidata 19188 * as a payload buffer. We also deal with adding more 19189 * than one payload buffers, which happens when the remaining 19190 * packetized portion of the current payload buffer is less 19191 * than MSS, while the next data block in transmit queue 19192 * has enough data to make up for one. This "spillover" 19193 * case essentially creates a split-packet, where portions 19194 * of the packet's payload fragments may span across two 19195 * virtually discontiguous address blocks. 19196 */ 19197 seg_len = mss; 19198 do { 19199 len = seg_len; 19200 19201 ASSERT(len > 0); 19202 ASSERT(max_pld >= 0); 19203 ASSERT(!add_buffer || cur_pld_off == 0); 19204 19205 /* 19206 * First time around for this payload buffer; note 19207 * in the case of a spillover, the following has 19208 * been done prior to adding the split-packet 19209 * descriptor to Multidata, and we don't want to 19210 * repeat the process. 19211 */ 19212 if (add_buffer) { 19213 ASSERT(mmd != NULL); 19214 ASSERT(md_pbuf == NULL); 19215 ASSERT(md_pbuf_nxt == NULL); 19216 ASSERT(pbuf_idx == -1 && pbuf_idx_nxt == -1); 19217 19218 /* 19219 * Have we reached the limit? We'd get to 19220 * this case when we're not chaining the 19221 * Multidata messages together, and since 19222 * we're done, terminate this loop. 19223 */ 19224 if (max_pld == 0) 19225 break; /* done */ 19226 19227 if ((md_pbuf = dupb(*xmit_tail)) == NULL) { 19228 TCP_STAT(tcp_mdt_allocfail); 19229 goto legacy_send; /* out_of_mem */ 19230 } 19231 19232 if (IS_VMLOANED_MBLK(md_pbuf) && !zcopy && 19233 zc_cap != NULL) { 19234 if (!ip_md_zcopy_attr(mmd, NULL, 19235 zc_cap->ill_zerocopy_flags)) { 19236 freeb(md_pbuf); 19237 TCP_STAT(tcp_mdt_allocfail); 19238 /* out_of_mem */ 19239 goto legacy_send; 19240 } 19241 zcopy = B_TRUE; 19242 } 19243 19244 md_pbuf->b_rptr += base_pld_off; 19245 19246 /* 19247 * Add a payload buffer to the Multidata; this 19248 * operation must not fail, or otherwise our 19249 * logic in this routine is broken. There 19250 * is no memory allocation done by the 19251 * routine, so any returned failure simply 19252 * tells us that we've done something wrong. 19253 * 19254 * A failure tells us that either we're adding 19255 * the same payload buffer more than once, or 19256 * we're trying to add more buffers than 19257 * allowed (max_pld calculation is wrong). 19258 * None of the above cases should happen, and 19259 * we panic because either there's horrible 19260 * heap corruption, and/or programming mistake. 19261 */ 19262 pbuf_idx = mmd_addpldbuf(mmd, md_pbuf); 19263 if (pbuf_idx < 0) { 19264 cmn_err(CE_PANIC, "tcp_multisend: " 19265 "payload buffer logic error " 19266 "detected for tcp %p mmd %p " 19267 "pbuf %p (%d)\n", 19268 (void *)tcp, (void *)mmd, 19269 (void *)md_pbuf, pbuf_idx); 19270 } 19271 19272 ASSERT(max_pld > 0); 19273 --max_pld; 19274 add_buffer = B_FALSE; 19275 } 19276 19277 ASSERT(md_mp_head != NULL); 19278 ASSERT(md_pbuf != NULL); 19279 ASSERT(md_pbuf_nxt == NULL); 19280 ASSERT(pbuf_idx != -1); 19281 ASSERT(pbuf_idx_nxt == -1); 19282 ASSERT(*usable > 0); 19283 19284 /* 19285 * We spillover to the next payload buffer only 19286 * if all of the following is true: 19287 * 19288 * 1. There is not enough data on the current 19289 * payload buffer to make up `len', 19290 * 2. We are allowed to send `len', 19291 * 3. The next payload buffer length is large 19292 * enough to accomodate `spill'. 19293 */ 19294 if ((spill = len - *tail_unsent) > 0 && 19295 *usable >= len && 19296 MBLKL((*xmit_tail)->b_cont) >= spill && 19297 max_pld > 0) { 19298 md_pbuf_nxt = dupb((*xmit_tail)->b_cont); 19299 if (md_pbuf_nxt == NULL) { 19300 TCP_STAT(tcp_mdt_allocfail); 19301 goto legacy_send; /* out_of_mem */ 19302 } 19303 19304 if (IS_VMLOANED_MBLK(md_pbuf_nxt) && !zcopy && 19305 zc_cap != NULL) { 19306 if (!ip_md_zcopy_attr(mmd, NULL, 19307 zc_cap->ill_zerocopy_flags)) { 19308 freeb(md_pbuf_nxt); 19309 TCP_STAT(tcp_mdt_allocfail); 19310 /* out_of_mem */ 19311 goto legacy_send; 19312 } 19313 zcopy = B_TRUE; 19314 } 19315 19316 /* 19317 * See comments above on the first call to 19318 * mmd_addpldbuf for explanation on the panic. 19319 */ 19320 pbuf_idx_nxt = mmd_addpldbuf(mmd, md_pbuf_nxt); 19321 if (pbuf_idx_nxt < 0) { 19322 panic("tcp_multisend: " 19323 "next payload buffer logic error " 19324 "detected for tcp %p mmd %p " 19325 "pbuf %p (%d)\n", 19326 (void *)tcp, (void *)mmd, 19327 (void *)md_pbuf_nxt, pbuf_idx_nxt); 19328 } 19329 19330 ASSERT(max_pld > 0); 19331 --max_pld; 19332 } else if (spill > 0) { 19333 /* 19334 * If there's a spillover, but the following 19335 * xmit_tail couldn't give us enough octets 19336 * to reach "len", then stop the current 19337 * Multidata creation and let the legacy 19338 * tcp_send() path take over. We don't want 19339 * to send the tiny segment as part of this 19340 * Multidata for performance reasons; instead, 19341 * we let the legacy path deal with grouping 19342 * it with the subsequent small mblks. 19343 */ 19344 if (*usable >= len && 19345 MBLKL((*xmit_tail)->b_cont) < spill) { 19346 max_pld = 0; 19347 break; /* done */ 19348 } 19349 19350 /* 19351 * We can't spillover, and we are near 19352 * the end of the current payload buffer, 19353 * so send what's left. 19354 */ 19355 ASSERT(*tail_unsent > 0); 19356 len = *tail_unsent; 19357 } 19358 19359 /* tail_unsent is negated if there is a spillover */ 19360 *tail_unsent -= len; 19361 *usable -= len; 19362 ASSERT(*usable >= 0); 19363 19364 if (*usable < mss) 19365 seg_len = *usable; 19366 /* 19367 * Sender SWS avoidance; see comments in tcp_send(); 19368 * everything else is the same, except that we only 19369 * do this here if there is no more data to be sent 19370 * following the current xmit_tail. We don't check 19371 * for 1-byte urgent data because we shouldn't get 19372 * here if TCP_URG_VALID is set. 19373 */ 19374 if (*usable > 0 && *usable < mss && 19375 ((md_pbuf_nxt == NULL && 19376 (*xmit_tail)->b_cont == NULL) || 19377 (md_pbuf_nxt != NULL && 19378 (*xmit_tail)->b_cont->b_cont == NULL)) && 19379 seg_len < (tcp->tcp_max_swnd >> 1) && 19380 (tcp->tcp_unsent - 19381 ((*snxt + len) - tcp->tcp_snxt)) > seg_len && 19382 !tcp->tcp_zero_win_probe) { 19383 if ((*snxt + len) == tcp->tcp_snxt && 19384 (*snxt + len) == tcp->tcp_suna) { 19385 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19386 } 19387 done = B_TRUE; 19388 } 19389 19390 /* 19391 * Prime pump for IP's checksumming on our behalf; 19392 * include the adjustment for a source route if any. 19393 * Do this only for software/partial hardware checksum 19394 * offload, as this field gets zeroed out later for 19395 * the full hardware checksum offload case. 19396 */ 19397 if (!(hwcksum_flags & HCK_FULLCKSUM)) { 19398 cksum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19399 cksum = (cksum >> 16) + (cksum & 0xFFFF); 19400 U16_TO_ABE16(cksum, tcp->tcp_tcph->th_sum); 19401 } 19402 19403 U32_TO_ABE32(*snxt, tcp->tcp_tcph->th_seq); 19404 *snxt += len; 19405 19406 tcp->tcp_tcph->th_flags[0] = TH_ACK; 19407 /* 19408 * We set the PUSH bit only if TCP has no more buffered 19409 * data to be transmitted (or if sender SWS avoidance 19410 * takes place), as opposed to setting it for every 19411 * last packet in the burst. 19412 */ 19413 if (done || 19414 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) == 0) 19415 tcp->tcp_tcph->th_flags[0] |= TH_PUSH; 19416 19417 /* 19418 * Set FIN bit if this is our last segment; snxt 19419 * already includes its length, and it will not 19420 * be adjusted after this point. 19421 */ 19422 if (tcp->tcp_valid_bits == TCP_FSS_VALID && 19423 *snxt == tcp->tcp_fss) { 19424 if (!tcp->tcp_fin_acked) { 19425 tcp->tcp_tcph->th_flags[0] |= TH_FIN; 19426 BUMP_MIB(&tcp_mib, tcpOutControl); 19427 } 19428 if (!tcp->tcp_fin_sent) { 19429 tcp->tcp_fin_sent = B_TRUE; 19430 /* 19431 * tcp state must be ESTABLISHED 19432 * in order for us to get here in 19433 * the first place. 19434 */ 19435 tcp->tcp_state = TCPS_FIN_WAIT_1; 19436 19437 /* 19438 * Upon returning from this routine, 19439 * tcp_wput_data() will set tcp_snxt 19440 * to be equal to snxt + tcp_fin_sent. 19441 * This is essentially the same as 19442 * setting it to tcp_fss + 1. 19443 */ 19444 } 19445 } 19446 19447 tcp->tcp_last_sent_len = (ushort_t)len; 19448 19449 len += tcp_hdr_len; 19450 if (tcp->tcp_ipversion == IPV4_VERSION) 19451 tcp->tcp_ipha->ipha_length = htons(len); 19452 else 19453 tcp->tcp_ip6h->ip6_plen = htons(len - 19454 ((char *)&tcp->tcp_ip6h[1] - 19455 tcp->tcp_iphc)); 19456 19457 pkt_info->flags = (PDESC_HBUF_REF | PDESC_PBUF_REF); 19458 19459 /* setup header fragment */ 19460 PDESC_HDR_ADD(pkt_info, 19461 md_hbuf->b_rptr + cur_hdr_off, /* base */ 19462 tcp->tcp_mdt_hdr_head, /* head room */ 19463 tcp_hdr_len, /* len */ 19464 tcp->tcp_mdt_hdr_tail); /* tail room */ 19465 19466 ASSERT(pkt_info->hdr_lim - pkt_info->hdr_base == 19467 hdr_frag_sz); 19468 ASSERT(MBLKIN(md_hbuf, 19469 (pkt_info->hdr_base - md_hbuf->b_rptr), 19470 PDESC_HDRSIZE(pkt_info))); 19471 19472 /* setup first payload fragment */ 19473 PDESC_PLD_INIT(pkt_info); 19474 PDESC_PLD_SPAN_ADD(pkt_info, 19475 pbuf_idx, /* index */ 19476 md_pbuf->b_rptr + cur_pld_off, /* start */ 19477 tcp->tcp_last_sent_len); /* len */ 19478 19479 /* create a split-packet in case of a spillover */ 19480 if (md_pbuf_nxt != NULL) { 19481 ASSERT(spill > 0); 19482 ASSERT(pbuf_idx_nxt > pbuf_idx); 19483 ASSERT(!add_buffer); 19484 19485 md_pbuf = md_pbuf_nxt; 19486 md_pbuf_nxt = NULL; 19487 pbuf_idx = pbuf_idx_nxt; 19488 pbuf_idx_nxt = -1; 19489 cur_pld_off = spill; 19490 19491 /* trim out first payload fragment */ 19492 PDESC_PLD_SPAN_TRIM(pkt_info, 0, spill); 19493 19494 /* setup second payload fragment */ 19495 PDESC_PLD_SPAN_ADD(pkt_info, 19496 pbuf_idx, /* index */ 19497 md_pbuf->b_rptr, /* start */ 19498 spill); /* len */ 19499 19500 if ((*xmit_tail)->b_next == NULL) { 19501 /* 19502 * Store the lbolt used for RTT 19503 * estimation. We can only record one 19504 * timestamp per mblk so we do it when 19505 * we reach the end of the payload 19506 * buffer. Also we only take a new 19507 * timestamp sample when the previous 19508 * timed data from the same mblk has 19509 * been ack'ed. 19510 */ 19511 (*xmit_tail)->b_prev = local_time; 19512 (*xmit_tail)->b_next = 19513 (mblk_t *)(uintptr_t)first_snxt; 19514 } 19515 19516 first_snxt = *snxt - spill; 19517 19518 /* 19519 * Advance xmit_tail; usable could be 0 by 19520 * the time we got here, but we made sure 19521 * above that we would only spillover to 19522 * the next data block if usable includes 19523 * the spilled-over amount prior to the 19524 * subtraction. Therefore, we are sure 19525 * that xmit_tail->b_cont can't be NULL. 19526 */ 19527 ASSERT((*xmit_tail)->b_cont != NULL); 19528 *xmit_tail = (*xmit_tail)->b_cont; 19529 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19530 (uintptr_t)INT_MAX); 19531 *tail_unsent = (int)MBLKL(*xmit_tail) - spill; 19532 } else { 19533 cur_pld_off += tcp->tcp_last_sent_len; 19534 } 19535 19536 /* 19537 * Fill in the header using the template header, and 19538 * add options such as time-stamp, ECN and/or SACK, 19539 * as needed. 19540 */ 19541 tcp_fill_header(tcp, pkt_info->hdr_rptr, 19542 (clock_t)local_time, num_sack_blk); 19543 19544 /* take care of some IP header businesses */ 19545 if (af == AF_INET) { 19546 ipha = (ipha_t *)pkt_info->hdr_rptr; 19547 19548 ASSERT(OK_32PTR((uchar_t *)ipha)); 19549 ASSERT(PDESC_HDRL(pkt_info) >= 19550 IP_SIMPLE_HDR_LENGTH); 19551 ASSERT(ipha->ipha_version_and_hdr_length == 19552 IP_SIMPLE_HDR_VERSION); 19553 19554 /* 19555 * Assign ident value for current packet; see 19556 * related comments in ip_wput_ire() about the 19557 * contract private interface with clustering 19558 * group. 19559 */ 19560 clusterwide = B_FALSE; 19561 if (cl_inet_ipident != NULL) { 19562 ASSERT(cl_inet_isclusterwide != NULL); 19563 if ((*cl_inet_isclusterwide)(IPPROTO_IP, 19564 AF_INET, 19565 (uint8_t *)(uintptr_t)src)) { 19566 ipha->ipha_ident = 19567 (*cl_inet_ipident) 19568 (IPPROTO_IP, AF_INET, 19569 (uint8_t *)(uintptr_t)src, 19570 (uint8_t *)(uintptr_t)dst); 19571 clusterwide = B_TRUE; 19572 } 19573 } 19574 19575 if (!clusterwide) { 19576 ipha->ipha_ident = (uint16_t) 19577 atomic_add_32_nv( 19578 &ire->ire_ident, 1); 19579 } 19580 #ifndef _BIG_ENDIAN 19581 ipha->ipha_ident = (ipha->ipha_ident << 8) | 19582 (ipha->ipha_ident >> 8); 19583 #endif 19584 } else { 19585 ip6h = (ip6_t *)pkt_info->hdr_rptr; 19586 19587 ASSERT(OK_32PTR((uchar_t *)ip6h)); 19588 ASSERT(IPVER(ip6h) == IPV6_VERSION); 19589 ASSERT(ip6h->ip6_nxt == IPPROTO_TCP); 19590 ASSERT(PDESC_HDRL(pkt_info) >= 19591 (IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET + 19592 TCP_CHECKSUM_SIZE)); 19593 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 19594 19595 if (tcp->tcp_ip_forward_progress) { 19596 rconfirm = B_TRUE; 19597 tcp->tcp_ip_forward_progress = B_FALSE; 19598 } 19599 } 19600 19601 /* at least one payload span, and at most two */ 19602 ASSERT(pkt_info->pld_cnt > 0 && pkt_info->pld_cnt < 3); 19603 19604 /* add the packet descriptor to Multidata */ 19605 if ((pkt = mmd_addpdesc(mmd, pkt_info, &err, 19606 KM_NOSLEEP)) == NULL) { 19607 /* 19608 * Any failure other than ENOMEM indicates 19609 * that we have passed in invalid pkt_info 19610 * or parameters to mmd_addpdesc, which must 19611 * not happen. 19612 * 19613 * EINVAL is a result of failure on boundary 19614 * checks against the pkt_info contents. It 19615 * should not happen, and we panic because 19616 * either there's horrible heap corruption, 19617 * and/or programming mistake. 19618 */ 19619 if (err != ENOMEM) { 19620 cmn_err(CE_PANIC, "tcp_multisend: " 19621 "pdesc logic error detected for " 19622 "tcp %p mmd %p pinfo %p (%d)\n", 19623 (void *)tcp, (void *)mmd, 19624 (void *)pkt_info, err); 19625 } 19626 TCP_STAT(tcp_mdt_addpdescfail); 19627 goto legacy_send; /* out_of_mem */ 19628 } 19629 ASSERT(pkt != NULL); 19630 19631 /* calculate IP header and TCP checksums */ 19632 if (af == AF_INET) { 19633 /* calculate pseudo-header checksum */ 19634 cksum = (dst >> 16) + (dst & 0xFFFF) + 19635 (src >> 16) + (src & 0xFFFF); 19636 19637 /* offset for TCP header checksum */ 19638 up = IPH_TCPH_CHECKSUMP(ipha, 19639 IP_SIMPLE_HDR_LENGTH); 19640 } else { 19641 up = (uint16_t *)&ip6h->ip6_src; 19642 19643 /* calculate pseudo-header checksum */ 19644 cksum = up[0] + up[1] + up[2] + up[3] + 19645 up[4] + up[5] + up[6] + up[7] + 19646 up[8] + up[9] + up[10] + up[11] + 19647 up[12] + up[13] + up[14] + up[15]; 19648 19649 /* Fold the initial sum */ 19650 cksum = (cksum & 0xffff) + (cksum >> 16); 19651 19652 up = (uint16_t *)(((uchar_t *)ip6h) + 19653 IPV6_HDR_LEN + TCP_CHECKSUM_OFFSET); 19654 } 19655 19656 if (hwcksum_flags & HCK_FULLCKSUM) { 19657 /* clear checksum field for hardware */ 19658 *up = 0; 19659 } else if (hwcksum_flags & HCK_PARTIALCKSUM) { 19660 uint32_t sum; 19661 19662 /* pseudo-header checksumming */ 19663 sum = *up + cksum + IP_TCP_CSUM_COMP; 19664 sum = (sum & 0xFFFF) + (sum >> 16); 19665 *up = (sum & 0xFFFF) + (sum >> 16); 19666 } else { 19667 /* software checksumming */ 19668 TCP_STAT(tcp_out_sw_cksum); 19669 TCP_STAT_UPDATE(tcp_out_sw_cksum_bytes, 19670 tcp->tcp_hdr_len + tcp->tcp_last_sent_len); 19671 *up = IP_MD_CSUM(pkt, tcp->tcp_ip_hdr_len, 19672 cksum + IP_TCP_CSUM_COMP); 19673 if (*up == 0) 19674 *up = 0xFFFF; 19675 } 19676 19677 /* IPv4 header checksum */ 19678 if (af == AF_INET) { 19679 ipha->ipha_fragment_offset_and_flags |= 19680 (uint32_t)htons(ire->ire_frag_flag); 19681 19682 if (hwcksum_flags & HCK_IPV4_HDRCKSUM) { 19683 ipha->ipha_hdr_checksum = 0; 19684 } else { 19685 IP_HDR_CKSUM(ipha, cksum, 19686 ((uint32_t *)ipha)[0], 19687 ((uint16_t *)ipha)[4]); 19688 } 19689 } 19690 19691 /* advance header offset */ 19692 cur_hdr_off += hdr_frag_sz; 19693 19694 obbytes += tcp->tcp_last_sent_len; 19695 ++obsegs; 19696 } while (!done && *usable > 0 && --num_burst_seg > 0 && 19697 *tail_unsent > 0); 19698 19699 if ((*xmit_tail)->b_next == NULL) { 19700 /* 19701 * Store the lbolt used for RTT estimation. We can only 19702 * record one timestamp per mblk so we do it when we 19703 * reach the end of the payload buffer. Also we only 19704 * take a new timestamp sample when the previous timed 19705 * data from the same mblk has been ack'ed. 19706 */ 19707 (*xmit_tail)->b_prev = local_time; 19708 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)first_snxt; 19709 } 19710 19711 ASSERT(*tail_unsent >= 0); 19712 if (*tail_unsent > 0) { 19713 /* 19714 * We got here because we broke out of the above 19715 * loop due to of one of the following cases: 19716 * 19717 * 1. len < adjusted MSS (i.e. small), 19718 * 2. Sender SWS avoidance, 19719 * 3. max_pld is zero. 19720 * 19721 * We are done for this Multidata, so trim our 19722 * last payload buffer (if any) accordingly. 19723 */ 19724 if (md_pbuf != NULL) 19725 md_pbuf->b_wptr -= *tail_unsent; 19726 } else if (*usable > 0) { 19727 *xmit_tail = (*xmit_tail)->b_cont; 19728 ASSERT((uintptr_t)MBLKL(*xmit_tail) <= 19729 (uintptr_t)INT_MAX); 19730 *tail_unsent = (int)MBLKL(*xmit_tail); 19731 add_buffer = B_TRUE; 19732 } 19733 } while (!done && *usable > 0 && num_burst_seg > 0 && 19734 (tcp_mdt_chain || max_pld > 0)); 19735 19736 /* send everything down */ 19737 tcp_multisend_data(tcp, ire, ill, md_mp_head, obsegs, obbytes, 19738 &rconfirm); 19739 19740 #undef PREP_NEW_MULTIDATA 19741 #undef PREP_NEW_PBUF 19742 #undef IPVER 19743 19744 IRE_REFRELE(ire); 19745 return (0); 19746 } 19747 19748 /* 19749 * A wrapper function for sending one or more Multidata messages down to 19750 * the module below ip; this routine does not release the reference of the 19751 * IRE (caller does that). This routine is analogous to tcp_send_data(). 19752 */ 19753 static void 19754 tcp_multisend_data(tcp_t *tcp, ire_t *ire, const ill_t *ill, mblk_t *md_mp_head, 19755 const uint_t obsegs, const uint_t obbytes, boolean_t *rconfirm) 19756 { 19757 uint64_t delta; 19758 nce_t *nce; 19759 19760 ASSERT(ire != NULL && ill != NULL); 19761 ASSERT(ire->ire_stq != NULL); 19762 ASSERT(md_mp_head != NULL); 19763 ASSERT(rconfirm != NULL); 19764 19765 /* adjust MIBs and IRE timestamp */ 19766 TCP_RECORD_TRACE(tcp, md_mp_head, TCP_TRACE_SEND_PKT); 19767 tcp->tcp_obsegs += obsegs; 19768 UPDATE_MIB(&tcp_mib, tcpOutDataSegs, obsegs); 19769 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, obbytes); 19770 TCP_STAT_UPDATE(tcp_mdt_pkt_out, obsegs); 19771 19772 if (tcp->tcp_ipversion == IPV4_VERSION) { 19773 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v4, obsegs); 19774 UPDATE_MIB(&ip_mib, ipOutRequests, obsegs); 19775 } else { 19776 TCP_STAT_UPDATE(tcp_mdt_pkt_out_v6, obsegs); 19777 UPDATE_MIB(&ip6_mib, ipv6OutRequests, obsegs); 19778 } 19779 19780 ire->ire_ob_pkt_count += obsegs; 19781 if (ire->ire_ipif != NULL) 19782 atomic_add_32(&ire->ire_ipif->ipif_ob_pkt_count, obsegs); 19783 ire->ire_last_used_time = lbolt; 19784 19785 /* send it down */ 19786 putnext(ire->ire_stq, md_mp_head); 19787 19788 /* we're done for TCP/IPv4 */ 19789 if (tcp->tcp_ipversion == IPV4_VERSION) 19790 return; 19791 19792 nce = ire->ire_nce; 19793 19794 ASSERT(nce != NULL); 19795 ASSERT(!(nce->nce_flags & (NCE_F_NONUD|NCE_F_PERMANENT))); 19796 ASSERT(nce->nce_state != ND_INCOMPLETE); 19797 19798 /* reachability confirmation? */ 19799 if (*rconfirm) { 19800 nce->nce_last = TICK_TO_MSEC(lbolt64); 19801 if (nce->nce_state != ND_REACHABLE) { 19802 mutex_enter(&nce->nce_lock); 19803 nce->nce_state = ND_REACHABLE; 19804 nce->nce_pcnt = ND_MAX_UNICAST_SOLICIT; 19805 mutex_exit(&nce->nce_lock); 19806 (void) untimeout(nce->nce_timeout_id); 19807 if (ip_debug > 2) { 19808 /* ip1dbg */ 19809 pr_addr_dbg("tcp_multisend_data: state " 19810 "for %s changed to REACHABLE\n", 19811 AF_INET6, &ire->ire_addr_v6); 19812 } 19813 } 19814 /* reset transport reachability confirmation */ 19815 *rconfirm = B_FALSE; 19816 } 19817 19818 delta = TICK_TO_MSEC(lbolt64) - nce->nce_last; 19819 ip1dbg(("tcp_multisend_data: delta = %" PRId64 19820 " ill_reachable_time = %d \n", delta, ill->ill_reachable_time)); 19821 19822 if (delta > (uint64_t)ill->ill_reachable_time) { 19823 mutex_enter(&nce->nce_lock); 19824 switch (nce->nce_state) { 19825 case ND_REACHABLE: 19826 case ND_STALE: 19827 /* 19828 * ND_REACHABLE is identical to ND_STALE in this 19829 * specific case. If reachable time has expired for 19830 * this neighbor (delta is greater than reachable 19831 * time), conceptually, the neighbor cache is no 19832 * longer in REACHABLE state, but already in STALE 19833 * state. So the correct transition here is to 19834 * ND_DELAY. 19835 */ 19836 nce->nce_state = ND_DELAY; 19837 mutex_exit(&nce->nce_lock); 19838 NDP_RESTART_TIMER(nce, delay_first_probe_time); 19839 if (ip_debug > 3) { 19840 /* ip2dbg */ 19841 pr_addr_dbg("tcp_multisend_data: state " 19842 "for %s changed to DELAY\n", 19843 AF_INET6, &ire->ire_addr_v6); 19844 } 19845 break; 19846 case ND_DELAY: 19847 case ND_PROBE: 19848 mutex_exit(&nce->nce_lock); 19849 /* Timers have already started */ 19850 break; 19851 case ND_UNREACHABLE: 19852 /* 19853 * ndp timer has detected that this nce is 19854 * unreachable and initiated deleting this nce 19855 * and all its associated IREs. This is a race 19856 * where we found the ire before it was deleted 19857 * and have just sent out a packet using this 19858 * unreachable nce. 19859 */ 19860 mutex_exit(&nce->nce_lock); 19861 break; 19862 default: 19863 ASSERT(0); 19864 } 19865 } 19866 } 19867 19868 /* 19869 * tcp_send() is called by tcp_wput_data() for non-Multidata transmission 19870 * scheme, and returns one of the following: 19871 * 19872 * -1 = failed allocation. 19873 * 0 = success; burst count reached, or usable send window is too small, 19874 * and that we'd rather wait until later before sending again. 19875 * 1 = success; we are called from tcp_multisend(), and both usable send 19876 * window and tail_unsent are greater than the MDT threshold, and thus 19877 * Multidata Transmit should be used instead. 19878 */ 19879 static int 19880 tcp_send(queue_t *q, tcp_t *tcp, const int mss, const int tcp_hdr_len, 19881 const int tcp_tcp_hdr_len, const int num_sack_blk, int *usable, 19882 uint_t *snxt, int *tail_unsent, mblk_t **xmit_tail, mblk_t *local_time, 19883 const int mdt_thres) 19884 { 19885 int num_burst_seg = tcp->tcp_snd_burst; 19886 19887 for (;;) { 19888 struct datab *db; 19889 tcph_t *tcph; 19890 uint32_t sum; 19891 mblk_t *mp, *mp1; 19892 uchar_t *rptr; 19893 int len; 19894 19895 /* 19896 * If we're called by tcp_multisend(), and the amount of 19897 * sendable data as well as the size of current xmit_tail 19898 * is beyond the MDT threshold, return to the caller and 19899 * let the large data transmit be done using MDT. 19900 */ 19901 if (*usable > 0 && *usable > mdt_thres && 19902 (*tail_unsent > mdt_thres || (*tail_unsent == 0 && 19903 MBLKL((*xmit_tail)->b_cont) > mdt_thres))) { 19904 ASSERT(tcp->tcp_mdt); 19905 return (1); /* success; do large send */ 19906 } 19907 19908 if (num_burst_seg-- == 0) 19909 break; /* success; burst count reached */ 19910 19911 len = mss; 19912 if (len > *usable) { 19913 len = *usable; 19914 if (len <= 0) { 19915 /* Terminate the loop */ 19916 break; /* success; too small */ 19917 } 19918 /* 19919 * Sender silly-window avoidance. 19920 * Ignore this if we are going to send a 19921 * zero window probe out. 19922 * 19923 * TODO: force data into microscopic window? 19924 * ==> (!pushed || (unsent > usable)) 19925 */ 19926 if (len < (tcp->tcp_max_swnd >> 1) && 19927 (tcp->tcp_unsent - (*snxt - tcp->tcp_snxt)) > len && 19928 !((tcp->tcp_valid_bits & TCP_URG_VALID) && 19929 len == 1) && (! tcp->tcp_zero_win_probe)) { 19930 /* 19931 * If the retransmit timer is not running 19932 * we start it so that we will retransmit 19933 * in the case when the the receiver has 19934 * decremented the window. 19935 */ 19936 if (*snxt == tcp->tcp_snxt && 19937 *snxt == tcp->tcp_suna) { 19938 /* 19939 * We are not supposed to send 19940 * anything. So let's wait a little 19941 * bit longer before breaking SWS 19942 * avoidance. 19943 * 19944 * What should the value be? 19945 * Suggestion: MAX(init rexmit time, 19946 * tcp->tcp_rto) 19947 */ 19948 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 19949 } 19950 break; /* success; too small */ 19951 } 19952 } 19953 19954 tcph = tcp->tcp_tcph; 19955 19956 *usable -= len; /* Approximate - can be adjusted later */ 19957 if (*usable > 0) 19958 tcph->th_flags[0] = TH_ACK; 19959 else 19960 tcph->th_flags[0] = (TH_ACK | TH_PUSH); 19961 19962 /* 19963 * Prime pump for IP's checksumming on our behalf 19964 * Include the adjustment for a source route if any. 19965 */ 19966 sum = len + tcp_tcp_hdr_len + tcp->tcp_sum; 19967 sum = (sum >> 16) + (sum & 0xFFFF); 19968 U16_TO_ABE16(sum, tcph->th_sum); 19969 19970 U32_TO_ABE32(*snxt, tcph->th_seq); 19971 19972 /* 19973 * Branch off to tcp_xmit_mp() if any of the VALID bits is 19974 * set. For the case when TCP_FSS_VALID is the only valid 19975 * bit (normal active close), branch off only when we think 19976 * that the FIN flag needs to be set. Note for this case, 19977 * that (snxt + len) may not reflect the actual seg_len, 19978 * as len may be further reduced in tcp_xmit_mp(). If len 19979 * gets modified, we will end up here again. 19980 */ 19981 if (tcp->tcp_valid_bits != 0 && 19982 (tcp->tcp_valid_bits != TCP_FSS_VALID || 19983 ((*snxt + len) == tcp->tcp_fss))) { 19984 uchar_t *prev_rptr; 19985 uint32_t prev_snxt = tcp->tcp_snxt; 19986 19987 if (*tail_unsent == 0) { 19988 ASSERT((*xmit_tail)->b_cont != NULL); 19989 *xmit_tail = (*xmit_tail)->b_cont; 19990 prev_rptr = (*xmit_tail)->b_rptr; 19991 *tail_unsent = (int)((*xmit_tail)->b_wptr - 19992 (*xmit_tail)->b_rptr); 19993 } else { 19994 prev_rptr = (*xmit_tail)->b_rptr; 19995 (*xmit_tail)->b_rptr = (*xmit_tail)->b_wptr - 19996 *tail_unsent; 19997 } 19998 mp = tcp_xmit_mp(tcp, *xmit_tail, len, NULL, NULL, 19999 *snxt, B_FALSE, (uint32_t *)&len, B_FALSE); 20000 /* Restore tcp_snxt so we get amount sent right. */ 20001 tcp->tcp_snxt = prev_snxt; 20002 if (prev_rptr == (*xmit_tail)->b_rptr) { 20003 /* 20004 * If the previous timestamp is still in use, 20005 * don't stomp on it. 20006 */ 20007 if ((*xmit_tail)->b_next == NULL) { 20008 (*xmit_tail)->b_prev = local_time; 20009 (*xmit_tail)->b_next = 20010 (mblk_t *)(uintptr_t)(*snxt); 20011 } 20012 } else 20013 (*xmit_tail)->b_rptr = prev_rptr; 20014 20015 if (mp == NULL) 20016 return (-1); 20017 mp1 = mp->b_cont; 20018 20019 tcp->tcp_last_sent_len = (ushort_t)len; 20020 while (mp1->b_cont) { 20021 *xmit_tail = (*xmit_tail)->b_cont; 20022 (*xmit_tail)->b_prev = local_time; 20023 (*xmit_tail)->b_next = 20024 (mblk_t *)(uintptr_t)(*snxt); 20025 mp1 = mp1->b_cont; 20026 } 20027 *snxt += len; 20028 *tail_unsent = (*xmit_tail)->b_wptr - mp1->b_wptr; 20029 BUMP_LOCAL(tcp->tcp_obsegs); 20030 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20031 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20032 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20033 tcp_send_data(tcp, q, mp); 20034 continue; 20035 } 20036 20037 *snxt += len; /* Adjust later if we don't send all of len */ 20038 BUMP_MIB(&tcp_mib, tcpOutDataSegs); 20039 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, len); 20040 20041 if (*tail_unsent) { 20042 /* Are the bytes above us in flight? */ 20043 rptr = (*xmit_tail)->b_wptr - *tail_unsent; 20044 if (rptr != (*xmit_tail)->b_rptr) { 20045 *tail_unsent -= len; 20046 tcp->tcp_last_sent_len = (ushort_t)len; 20047 len += tcp_hdr_len; 20048 if (tcp->tcp_ipversion == IPV4_VERSION) 20049 tcp->tcp_ipha->ipha_length = htons(len); 20050 else 20051 tcp->tcp_ip6h->ip6_plen = 20052 htons(len - 20053 ((char *)&tcp->tcp_ip6h[1] - 20054 tcp->tcp_iphc)); 20055 mp = dupb(*xmit_tail); 20056 if (!mp) 20057 return (-1); /* out_of_mem */ 20058 mp->b_rptr = rptr; 20059 /* 20060 * If the old timestamp is no longer in use, 20061 * sample a new timestamp now. 20062 */ 20063 if ((*xmit_tail)->b_next == NULL) { 20064 (*xmit_tail)->b_prev = local_time; 20065 (*xmit_tail)->b_next = 20066 (mblk_t *)(uintptr_t)(*snxt-len); 20067 } 20068 goto must_alloc; 20069 } 20070 } else { 20071 *xmit_tail = (*xmit_tail)->b_cont; 20072 ASSERT((uintptr_t)((*xmit_tail)->b_wptr - 20073 (*xmit_tail)->b_rptr) <= (uintptr_t)INT_MAX); 20074 *tail_unsent = (int)((*xmit_tail)->b_wptr - 20075 (*xmit_tail)->b_rptr); 20076 } 20077 20078 (*xmit_tail)->b_prev = local_time; 20079 (*xmit_tail)->b_next = (mblk_t *)(uintptr_t)(*snxt - len); 20080 20081 *tail_unsent -= len; 20082 tcp->tcp_last_sent_len = (ushort_t)len; 20083 20084 len += tcp_hdr_len; 20085 if (tcp->tcp_ipversion == IPV4_VERSION) 20086 tcp->tcp_ipha->ipha_length = htons(len); 20087 else 20088 tcp->tcp_ip6h->ip6_plen = htons(len - 20089 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20090 20091 mp = dupb(*xmit_tail); 20092 if (!mp) 20093 return (-1); /* out_of_mem */ 20094 20095 len = tcp_hdr_len; 20096 /* 20097 * There are four reasons to allocate a new hdr mblk: 20098 * 1) The bytes above us are in use by another packet 20099 * 2) We don't have good alignment 20100 * 3) The mblk is being shared 20101 * 4) We don't have enough room for a header 20102 */ 20103 rptr = mp->b_rptr - len; 20104 if (!OK_32PTR(rptr) || 20105 ((db = mp->b_datap), db->db_ref != 2) || 20106 rptr < db->db_base) { 20107 /* NOTE: we assume allocb returns an OK_32PTR */ 20108 20109 must_alloc:; 20110 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + 20111 tcp_wroff_xtra, BPRI_MED); 20112 if (!mp1) { 20113 freemsg(mp); 20114 return (-1); /* out_of_mem */ 20115 } 20116 mp1->b_cont = mp; 20117 mp = mp1; 20118 /* Leave room for Link Level header */ 20119 len = tcp_hdr_len; 20120 rptr = &mp->b_rptr[tcp_wroff_xtra]; 20121 mp->b_wptr = &rptr[len]; 20122 } 20123 20124 /* 20125 * Fill in the header using the template header, and add 20126 * options such as time-stamp, ECN and/or SACK, as needed. 20127 */ 20128 tcp_fill_header(tcp, rptr, (clock_t)local_time, num_sack_blk); 20129 20130 mp->b_rptr = rptr; 20131 20132 if (*tail_unsent) { 20133 int spill = *tail_unsent; 20134 20135 mp1 = mp->b_cont; 20136 if (!mp1) 20137 mp1 = mp; 20138 20139 /* 20140 * If we're a little short, tack on more mblks until 20141 * there is no more spillover. 20142 */ 20143 while (spill < 0) { 20144 mblk_t *nmp; 20145 int nmpsz; 20146 20147 nmp = (*xmit_tail)->b_cont; 20148 nmpsz = MBLKL(nmp); 20149 20150 /* 20151 * Excess data in mblk; can we split it? 20152 * If MDT is enabled for the connection, 20153 * keep on splitting as this is a transient 20154 * send path. 20155 */ 20156 if (!tcp->tcp_mdt && (spill + nmpsz > 0)) { 20157 /* 20158 * Don't split if stream head was 20159 * told to break up larger writes 20160 * into smaller ones. 20161 */ 20162 if (tcp->tcp_maxpsz > 0) 20163 break; 20164 20165 /* 20166 * Next mblk is less than SMSS/2 20167 * rounded up to nearest 64-byte; 20168 * let it get sent as part of the 20169 * next segment. 20170 */ 20171 if (tcp->tcp_localnet && 20172 !tcp->tcp_cork && 20173 (nmpsz < roundup((mss >> 1), 64))) 20174 break; 20175 } 20176 20177 *xmit_tail = nmp; 20178 ASSERT((uintptr_t)nmpsz <= (uintptr_t)INT_MAX); 20179 /* Stash for rtt use later */ 20180 (*xmit_tail)->b_prev = local_time; 20181 (*xmit_tail)->b_next = 20182 (mblk_t *)(uintptr_t)(*snxt - len); 20183 mp1->b_cont = dupb(*xmit_tail); 20184 mp1 = mp1->b_cont; 20185 20186 spill += nmpsz; 20187 if (mp1 == NULL) { 20188 *tail_unsent = spill; 20189 freemsg(mp); 20190 return (-1); /* out_of_mem */ 20191 } 20192 } 20193 20194 /* Trim back any surplus on the last mblk */ 20195 if (spill >= 0) { 20196 mp1->b_wptr -= spill; 20197 *tail_unsent = spill; 20198 } else { 20199 /* 20200 * We did not send everything we could in 20201 * order to remain within the b_cont limit. 20202 */ 20203 *usable -= spill; 20204 *snxt += spill; 20205 tcp->tcp_last_sent_len += spill; 20206 UPDATE_MIB(&tcp_mib, tcpOutDataBytes, spill); 20207 /* 20208 * Adjust the checksum 20209 */ 20210 tcph = (tcph_t *)(rptr + tcp->tcp_ip_hdr_len); 20211 sum += spill; 20212 sum = (sum >> 16) + (sum & 0xFFFF); 20213 U16_TO_ABE16(sum, tcph->th_sum); 20214 if (tcp->tcp_ipversion == IPV4_VERSION) { 20215 sum = ntohs( 20216 ((ipha_t *)rptr)->ipha_length) + 20217 spill; 20218 ((ipha_t *)rptr)->ipha_length = 20219 htons(sum); 20220 } else { 20221 sum = ntohs( 20222 ((ip6_t *)rptr)->ip6_plen) + 20223 spill; 20224 ((ip6_t *)rptr)->ip6_plen = 20225 htons(sum); 20226 } 20227 *tail_unsent = 0; 20228 } 20229 } 20230 if (tcp->tcp_ip_forward_progress) { 20231 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 20232 *(uint32_t *)mp->b_rptr |= IP_FORWARD_PROG; 20233 tcp->tcp_ip_forward_progress = B_FALSE; 20234 } 20235 20236 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 20237 tcp_send_data(tcp, q, mp); 20238 BUMP_LOCAL(tcp->tcp_obsegs); 20239 } 20240 20241 return (0); 20242 } 20243 20244 /* Unlink and return any mblk that looks like it contains a MDT info */ 20245 static mblk_t * 20246 tcp_mdt_info_mp(mblk_t *mp) 20247 { 20248 mblk_t *prev_mp; 20249 20250 for (;;) { 20251 prev_mp = mp; 20252 /* no more to process? */ 20253 if ((mp = mp->b_cont) == NULL) 20254 break; 20255 20256 switch (DB_TYPE(mp)) { 20257 case M_CTL: 20258 if (*(uint32_t *)mp->b_rptr != MDT_IOC_INFO_UPDATE) 20259 continue; 20260 ASSERT(prev_mp != NULL); 20261 prev_mp->b_cont = mp->b_cont; 20262 mp->b_cont = NULL; 20263 return (mp); 20264 default: 20265 break; 20266 } 20267 } 20268 return (mp); 20269 } 20270 20271 /* MDT info update routine, called when IP notifies us about MDT */ 20272 static void 20273 tcp_mdt_update(tcp_t *tcp, ill_mdt_capab_t *mdt_capab, boolean_t first) 20274 { 20275 boolean_t prev_state; 20276 20277 /* 20278 * IP is telling us to abort MDT on this connection? We know 20279 * this because the capability is only turned off when IP 20280 * encounters some pathological cases, e.g. link-layer change 20281 * where the new driver doesn't support MDT, or in situation 20282 * where MDT usage on the link-layer has been switched off. 20283 * IP would not have sent us the initial MDT_IOC_INFO_UPDATE 20284 * if the link-layer doesn't support MDT, and if it does, it 20285 * will indicate that the feature is to be turned on. 20286 */ 20287 prev_state = tcp->tcp_mdt; 20288 tcp->tcp_mdt = (mdt_capab->ill_mdt_on != 0); 20289 if (!tcp->tcp_mdt && !first) { 20290 TCP_STAT(tcp_mdt_conn_halted3); 20291 ip1dbg(("tcp_mdt_update: disabling MDT for connp %p\n", 20292 (void *)tcp->tcp_connp)); 20293 } 20294 20295 /* 20296 * We currently only support MDT on simple TCP/{IPv4,IPv6}, 20297 * so disable MDT otherwise. The checks are done here 20298 * and in tcp_wput_data(). 20299 */ 20300 if (tcp->tcp_mdt && 20301 (tcp->tcp_ipversion == IPV4_VERSION && 20302 tcp->tcp_ip_hdr_len != IP_SIMPLE_HDR_LENGTH) || 20303 (tcp->tcp_ipversion == IPV6_VERSION && 20304 tcp->tcp_ip_hdr_len != IPV6_HDR_LEN)) 20305 tcp->tcp_mdt = B_FALSE; 20306 20307 if (tcp->tcp_mdt) { 20308 if (mdt_capab->ill_mdt_version != MDT_VERSION_2) { 20309 cmn_err(CE_NOTE, "tcp_mdt_update: unknown MDT " 20310 "version (%d), expected version is %d", 20311 mdt_capab->ill_mdt_version, MDT_VERSION_2); 20312 tcp->tcp_mdt = B_FALSE; 20313 return; 20314 } 20315 20316 /* 20317 * We need the driver to be able to handle at least three 20318 * spans per packet in order for tcp MDT to be utilized. 20319 * The first is for the header portion, while the rest are 20320 * needed to handle a packet that straddles across two 20321 * virtually non-contiguous buffers; a typical tcp packet 20322 * therefore consists of only two spans. Note that we take 20323 * a zero as "don't care". 20324 */ 20325 if (mdt_capab->ill_mdt_span_limit > 0 && 20326 mdt_capab->ill_mdt_span_limit < 3) { 20327 tcp->tcp_mdt = B_FALSE; 20328 return; 20329 } 20330 20331 /* a zero means driver wants default value */ 20332 tcp->tcp_mdt_max_pld = MIN(mdt_capab->ill_mdt_max_pld, 20333 tcp_mdt_max_pbufs); 20334 if (tcp->tcp_mdt_max_pld == 0) 20335 tcp->tcp_mdt_max_pld = tcp_mdt_max_pbufs; 20336 20337 /* ensure 32-bit alignment */ 20338 tcp->tcp_mdt_hdr_head = roundup(MAX(tcp_mdt_hdr_head_min, 20339 mdt_capab->ill_mdt_hdr_head), 4); 20340 tcp->tcp_mdt_hdr_tail = roundup(MAX(tcp_mdt_hdr_tail_min, 20341 mdt_capab->ill_mdt_hdr_tail), 4); 20342 20343 if (!first && !prev_state) { 20344 TCP_STAT(tcp_mdt_conn_resumed2); 20345 ip1dbg(("tcp_mdt_update: reenabling MDT for connp %p\n", 20346 (void *)tcp->tcp_connp)); 20347 } 20348 } 20349 } 20350 20351 static void 20352 tcp_ire_ill_check(tcp_t *tcp, ire_t *ire, ill_t *ill, boolean_t check_mdt) 20353 { 20354 conn_t *connp = tcp->tcp_connp; 20355 20356 ASSERT(ire != NULL); 20357 20358 /* 20359 * We may be in the fastpath here, and although we essentially do 20360 * similar checks as in ip_bind_connected{_v6}/ip_mdinfo_return, 20361 * we try to keep things as brief as possible. After all, these 20362 * are only best-effort checks, and we do more thorough ones prior 20363 * to calling tcp_multisend(). 20364 */ 20365 if (ip_multidata_outbound && check_mdt && 20366 !(ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK)) && 20367 ill != NULL && ILL_MDT_CAPABLE(ill) && 20368 !CONN_IPSEC_OUT_ENCAPSULATED(connp) && 20369 !(ire->ire_flags & RTF_MULTIRT) && 20370 !IPP_ENABLED(IPP_LOCAL_OUT) && 20371 CONN_IS_MD_FASTPATH(connp)) { 20372 /* Remember the result */ 20373 connp->conn_mdt_ok = B_TRUE; 20374 20375 ASSERT(ill->ill_mdt_capab != NULL); 20376 if (!ill->ill_mdt_capab->ill_mdt_on) { 20377 /* 20378 * If MDT has been previously turned off in the past, 20379 * and we currently can do MDT (due to IPQoS policy 20380 * removal, etc.) then enable it for this interface. 20381 */ 20382 ill->ill_mdt_capab->ill_mdt_on = 1; 20383 ip1dbg(("tcp_ire_ill_check: connp %p enables MDT for " 20384 "interface %s\n", (void *)connp, ill->ill_name)); 20385 } 20386 tcp_mdt_update(tcp, ill->ill_mdt_capab, B_TRUE); 20387 } 20388 20389 /* 20390 * The goal is to reduce the number of generated tcp segments by 20391 * setting the maxpsz multiplier to 0; this will have an affect on 20392 * tcp_maxpsz_set(). With this behavior, tcp will pack more data 20393 * into each packet, up to SMSS bytes. Doing this reduces the number 20394 * of outbound segments and incoming ACKs, thus allowing for better 20395 * network and system performance. In contrast the legacy behavior 20396 * may result in sending less than SMSS size, because the last mblk 20397 * for some packets may have more data than needed to make up SMSS, 20398 * and the legacy code refused to "split" it. 20399 * 20400 * We apply the new behavior on following situations: 20401 * 20402 * 1) Loopback connections, 20403 * 2) Connections in which the remote peer is not on local subnet, 20404 * 3) Local subnet connections over the bge interface (see below). 20405 * 20406 * Ideally, we would like this behavior to apply for interfaces other 20407 * than bge. However, doing so would negatively impact drivers which 20408 * perform dynamic mapping and unmapping of DMA resources, which are 20409 * increased by setting the maxpsz multiplier to 0 (more mblks per 20410 * packet will be generated by tcp). The bge driver does not suffer 20411 * from this, as it copies the mblks into pre-mapped buffers, and 20412 * therefore does not require more I/O resources than before. 20413 * 20414 * Otherwise, this behavior is present on all network interfaces when 20415 * the destination endpoint is non-local, since reducing the number 20416 * of packets in general is good for the network. 20417 * 20418 * TODO We need to remove this hard-coded conditional for bge once 20419 * a better "self-tuning" mechanism, or a way to comprehend 20420 * the driver transmit strategy is devised. Until the solution 20421 * is found and well understood, we live with this hack. 20422 */ 20423 if (!tcp_static_maxpsz && 20424 (tcp->tcp_loopback || !tcp->tcp_localnet || 20425 (ill->ill_name_length > 3 && bcmp(ill->ill_name, "bge", 3) == 0))) { 20426 /* override the default value */ 20427 tcp->tcp_maxpsz = 0; 20428 20429 ip3dbg(("tcp_ire_ill_check: connp %p tcp_maxpsz %d on " 20430 "interface %s\n", (void *)connp, tcp->tcp_maxpsz, 20431 ill != NULL ? ill->ill_name : ipif_loopback_name)); 20432 } 20433 20434 /* set the stream head parameters accordingly */ 20435 (void) tcp_maxpsz_set(tcp, B_TRUE); 20436 } 20437 20438 /* tcp_wput_flush is called by tcp_wput_nondata to handle M_FLUSH messages. */ 20439 static void 20440 tcp_wput_flush(tcp_t *tcp, mblk_t *mp) 20441 { 20442 uchar_t fval = *mp->b_rptr; 20443 mblk_t *tail; 20444 queue_t *q = tcp->tcp_wq; 20445 20446 /* TODO: How should flush interact with urgent data? */ 20447 if ((fval & FLUSHW) && tcp->tcp_xmit_head && 20448 !(tcp->tcp_valid_bits & TCP_URG_VALID)) { 20449 /* 20450 * Flush only data that has not yet been put on the wire. If 20451 * we flush data that we have already transmitted, life, as we 20452 * know it, may come to an end. 20453 */ 20454 tail = tcp->tcp_xmit_tail; 20455 tail->b_wptr -= tcp->tcp_xmit_tail_unsent; 20456 tcp->tcp_xmit_tail_unsent = 0; 20457 tcp->tcp_unsent = 0; 20458 if (tail->b_wptr != tail->b_rptr) 20459 tail = tail->b_cont; 20460 if (tail) { 20461 mblk_t **excess = &tcp->tcp_xmit_head; 20462 for (;;) { 20463 mblk_t *mp1 = *excess; 20464 if (mp1 == tail) 20465 break; 20466 tcp->tcp_xmit_tail = mp1; 20467 tcp->tcp_xmit_last = mp1; 20468 excess = &mp1->b_cont; 20469 } 20470 *excess = NULL; 20471 tcp_close_mpp(&tail); 20472 if (tcp->tcp_snd_zcopy_aware) 20473 tcp_zcopy_notify(tcp); 20474 } 20475 /* 20476 * We have no unsent data, so unsent must be less than 20477 * tcp_xmit_lowater, so re-enable flow. 20478 */ 20479 if (tcp->tcp_flow_stopped) { 20480 tcp_clrqfull(tcp); 20481 } 20482 } 20483 /* 20484 * TODO: you can't just flush these, you have to increase rwnd for one 20485 * thing. For another, how should urgent data interact? 20486 */ 20487 if (fval & FLUSHR) { 20488 *mp->b_rptr = fval & ~FLUSHW; 20489 /* XXX */ 20490 qreply(q, mp); 20491 return; 20492 } 20493 freemsg(mp); 20494 } 20495 20496 /* 20497 * tcp_wput_iocdata is called by tcp_wput_nondata to handle all M_IOCDATA 20498 * messages. 20499 */ 20500 static void 20501 tcp_wput_iocdata(tcp_t *tcp, mblk_t *mp) 20502 { 20503 mblk_t *mp1; 20504 STRUCT_HANDLE(strbuf, sb); 20505 uint16_t port; 20506 queue_t *q = tcp->tcp_wq; 20507 in6_addr_t v6addr; 20508 ipaddr_t v4addr; 20509 uint32_t flowinfo = 0; 20510 int addrlen; 20511 20512 /* Make sure it is one of ours. */ 20513 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 20514 case TI_GETMYNAME: 20515 case TI_GETPEERNAME: 20516 break; 20517 default: 20518 CALL_IP_WPUT(tcp->tcp_connp, q, mp); 20519 return; 20520 } 20521 switch (mi_copy_state(q, mp, &mp1)) { 20522 case -1: 20523 return; 20524 case MI_COPY_CASE(MI_COPY_IN, 1): 20525 break; 20526 case MI_COPY_CASE(MI_COPY_OUT, 1): 20527 /* Copy out the strbuf. */ 20528 mi_copyout(q, mp); 20529 return; 20530 case MI_COPY_CASE(MI_COPY_OUT, 2): 20531 /* All done. */ 20532 mi_copy_done(q, mp, 0); 20533 return; 20534 default: 20535 mi_copy_done(q, mp, EPROTO); 20536 return; 20537 } 20538 /* Check alignment of the strbuf */ 20539 if (!OK_32PTR(mp1->b_rptr)) { 20540 mi_copy_done(q, mp, EINVAL); 20541 return; 20542 } 20543 20544 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag, 20545 (void *)mp1->b_rptr); 20546 addrlen = tcp->tcp_family == AF_INET ? sizeof (sin_t) : sizeof (sin6_t); 20547 20548 if (STRUCT_FGET(sb, maxlen) < addrlen) { 20549 mi_copy_done(q, mp, EINVAL); 20550 return; 20551 } 20552 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) { 20553 case TI_GETMYNAME: 20554 if (tcp->tcp_family == AF_INET) { 20555 if (tcp->tcp_ipversion == IPV4_VERSION) { 20556 v4addr = tcp->tcp_ipha->ipha_src; 20557 } else { 20558 /* can't return an address in this case */ 20559 v4addr = 0; 20560 } 20561 } else { 20562 /* tcp->tcp_family == AF_INET6 */ 20563 if (tcp->tcp_ipversion == IPV4_VERSION) { 20564 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 20565 &v6addr); 20566 } else { 20567 v6addr = tcp->tcp_ip6h->ip6_src; 20568 } 20569 } 20570 port = tcp->tcp_lport; 20571 break; 20572 case TI_GETPEERNAME: 20573 if (tcp->tcp_family == AF_INET) { 20574 if (tcp->tcp_ipversion == IPV4_VERSION) { 20575 IN6_V4MAPPED_TO_IPADDR(&tcp->tcp_remote_v6, 20576 v4addr); 20577 } else { 20578 /* can't return an address in this case */ 20579 v4addr = 0; 20580 } 20581 } else { 20582 /* tcp->tcp_family == AF_INET6) */ 20583 v6addr = tcp->tcp_remote_v6; 20584 if (tcp->tcp_ipversion == IPV6_VERSION) { 20585 /* 20586 * No flowinfo if tcp->tcp_ipversion is v4. 20587 * 20588 * flowinfo was already initialized to zero 20589 * where it was declared above, so only 20590 * set it if ipversion is v6. 20591 */ 20592 flowinfo = tcp->tcp_ip6h->ip6_vcf & 20593 ~IPV6_VERS_AND_FLOW_MASK; 20594 } 20595 } 20596 port = tcp->tcp_fport; 20597 break; 20598 default: 20599 mi_copy_done(q, mp, EPROTO); 20600 return; 20601 } 20602 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE); 20603 if (!mp1) 20604 return; 20605 20606 if (tcp->tcp_family == AF_INET) { 20607 sin_t *sin; 20608 20609 STRUCT_FSET(sb, len, (int)sizeof (sin_t)); 20610 sin = (sin_t *)mp1->b_rptr; 20611 mp1->b_wptr = (uchar_t *)&sin[1]; 20612 *sin = sin_null; 20613 sin->sin_family = AF_INET; 20614 sin->sin_addr.s_addr = v4addr; 20615 sin->sin_port = port; 20616 } else { 20617 /* tcp->tcp_family == AF_INET6 */ 20618 sin6_t *sin6; 20619 20620 STRUCT_FSET(sb, len, (int)sizeof (sin6_t)); 20621 sin6 = (sin6_t *)mp1->b_rptr; 20622 mp1->b_wptr = (uchar_t *)&sin6[1]; 20623 *sin6 = sin6_null; 20624 sin6->sin6_family = AF_INET6; 20625 sin6->sin6_flowinfo = flowinfo; 20626 sin6->sin6_addr = v6addr; 20627 sin6->sin6_port = port; 20628 } 20629 /* Copy out the address */ 20630 mi_copyout(q, mp); 20631 } 20632 20633 /* 20634 * tcp_wput_ioctl is called by tcp_wput_nondata() to handle all M_IOCTL 20635 * messages. 20636 */ 20637 /* ARGSUSED */ 20638 static void 20639 tcp_wput_ioctl(void *arg, mblk_t *mp, void *arg2) 20640 { 20641 conn_t *connp = (conn_t *)arg; 20642 tcp_t *tcp = connp->conn_tcp; 20643 queue_t *q = tcp->tcp_wq; 20644 struct iocblk *iocp; 20645 20646 ASSERT(DB_TYPE(mp) == M_IOCTL); 20647 /* 20648 * Try and ASSERT the minimum possible references on the 20649 * conn early enough. Since we are executing on write side, 20650 * the connection is obviously not detached and that means 20651 * there is a ref each for TCP and IP. Since we are behind 20652 * the squeue, the minimum references needed are 3. If the 20653 * conn is in classifier hash list, there should be an 20654 * extra ref for that (we check both the possibilities). 20655 */ 20656 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 20657 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 20658 20659 iocp = (struct iocblk *)mp->b_rptr; 20660 switch (iocp->ioc_cmd) { 20661 case TCP_IOC_DEFAULT_Q: 20662 /* Wants to be the default wq. */ 20663 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 20664 iocp->ioc_error = EPERM; 20665 iocp->ioc_count = 0; 20666 mp->b_datap->db_type = M_IOCACK; 20667 qreply(q, mp); 20668 return; 20669 } 20670 tcp_def_q_set(tcp, mp); 20671 return; 20672 case _SIOCSOCKFALLBACK: 20673 /* 20674 * Either sockmod is about to be popped and the socket 20675 * would now be treated as a plain stream, or a module 20676 * is about to be pushed so we could no longer use read- 20677 * side synchronous streams for fused loopback tcp. 20678 * Drain any queued data and disable direct sockfs 20679 * interface from now on. 20680 */ 20681 if (!tcp->tcp_issocket) { 20682 DB_TYPE(mp) = M_IOCNAK; 20683 iocp->ioc_error = EINVAL; 20684 } else { 20685 #ifdef _ILP32 20686 tcp->tcp_acceptor_id = (t_uscalar_t)RD(q); 20687 #else 20688 tcp->tcp_acceptor_id = tcp->tcp_connp->conn_dev; 20689 #endif 20690 /* 20691 * Insert this socket into the acceptor hash. 20692 * We might need it for T_CONN_RES message 20693 */ 20694 tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp); 20695 20696 if (tcp->tcp_fused) { 20697 /* 20698 * This is a fused loopback tcp; disable 20699 * read-side synchronous streams interface 20700 * and drain any queued data. It is okay 20701 * to do this for non-synchronous streams 20702 * fused tcp as well. 20703 */ 20704 tcp_fuse_disable_pair(tcp, B_FALSE); 20705 } 20706 tcp->tcp_issocket = B_FALSE; 20707 TCP_STAT(tcp_sock_fallback); 20708 20709 DB_TYPE(mp) = M_IOCACK; 20710 iocp->ioc_error = 0; 20711 } 20712 iocp->ioc_count = 0; 20713 iocp->ioc_rval = 0; 20714 qreply(q, mp); 20715 return; 20716 } 20717 CALL_IP_WPUT(connp, q, mp); 20718 } 20719 20720 /* 20721 * This routine is called by tcp_wput() to handle all TPI requests. 20722 */ 20723 /* ARGSUSED */ 20724 static void 20725 tcp_wput_proto(void *arg, mblk_t *mp, void *arg2) 20726 { 20727 conn_t *connp = (conn_t *)arg; 20728 tcp_t *tcp = connp->conn_tcp; 20729 union T_primitives *tprim = (union T_primitives *)mp->b_rptr; 20730 uchar_t *rptr; 20731 t_scalar_t type; 20732 int len; 20733 cred_t *cr = DB_CREDDEF(mp, tcp->tcp_cred); 20734 20735 /* 20736 * Try and ASSERT the minimum possible references on the 20737 * conn early enough. Since we are executing on write side, 20738 * the connection is obviously not detached and that means 20739 * there is a ref each for TCP and IP. Since we are behind 20740 * the squeue, the minimum references needed are 3. If the 20741 * conn is in classifier hash list, there should be an 20742 * extra ref for that (we check both the possibilities). 20743 */ 20744 ASSERT((connp->conn_fanout != NULL && connp->conn_ref >= 4) || 20745 (connp->conn_fanout == NULL && connp->conn_ref >= 3)); 20746 20747 rptr = mp->b_rptr; 20748 ASSERT((uintptr_t)(mp->b_wptr - rptr) <= (uintptr_t)INT_MAX); 20749 if ((mp->b_wptr - rptr) >= sizeof (t_scalar_t)) { 20750 type = ((union T_primitives *)rptr)->type; 20751 if (type == T_EXDATA_REQ) { 20752 uint32_t msize = msgdsize(mp->b_cont); 20753 20754 len = msize - 1; 20755 if (len < 0) { 20756 freemsg(mp); 20757 return; 20758 } 20759 /* 20760 * Try to force urgent data out on the wire. 20761 * Even if we have unsent data this will 20762 * at least send the urgent flag. 20763 * XXX does not handle more flag correctly. 20764 */ 20765 len += tcp->tcp_unsent; 20766 len += tcp->tcp_snxt; 20767 tcp->tcp_urg = len; 20768 tcp->tcp_valid_bits |= TCP_URG_VALID; 20769 20770 /* Bypass tcp protocol for fused tcp loopback */ 20771 if (tcp->tcp_fused && tcp_fuse_output(tcp, mp, msize)) 20772 return; 20773 } else if (type != T_DATA_REQ) { 20774 goto non_urgent_data; 20775 } 20776 /* TODO: options, flags, ... from user */ 20777 /* Set length to zero for reclamation below */ 20778 tcp_wput_data(tcp, mp->b_cont, B_TRUE); 20779 freeb(mp); 20780 return; 20781 } else { 20782 if (tcp->tcp_debug) { 20783 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 20784 "tcp_wput_proto, dropping one..."); 20785 } 20786 freemsg(mp); 20787 return; 20788 } 20789 20790 non_urgent_data: 20791 20792 switch ((int)tprim->type) { 20793 case T_SSL_PROXY_BIND_REQ: /* an SSL proxy endpoint bind request */ 20794 /* 20795 * save the kssl_ent_t from the next block, and convert this 20796 * back to a normal bind_req. 20797 */ 20798 if (mp->b_cont != NULL) { 20799 ASSERT(MBLKL(mp->b_cont) >= sizeof (kssl_ent_t)); 20800 20801 if (tcp->tcp_kssl_ent != NULL) { 20802 kssl_release_ent(tcp->tcp_kssl_ent, NULL, 20803 KSSL_NO_PROXY); 20804 tcp->tcp_kssl_ent = NULL; 20805 } 20806 bcopy(mp->b_cont->b_rptr, &tcp->tcp_kssl_ent, 20807 sizeof (kssl_ent_t)); 20808 kssl_hold_ent(tcp->tcp_kssl_ent); 20809 freemsg(mp->b_cont); 20810 mp->b_cont = NULL; 20811 } 20812 tprim->type = T_BIND_REQ; 20813 20814 /* FALLTHROUGH */ 20815 case O_T_BIND_REQ: /* bind request */ 20816 case T_BIND_REQ: /* new semantics bind request */ 20817 tcp_bind(tcp, mp); 20818 break; 20819 case T_UNBIND_REQ: /* unbind request */ 20820 tcp_unbind(tcp, mp); 20821 break; 20822 case O_T_CONN_RES: /* old connection response XXX */ 20823 case T_CONN_RES: /* connection response */ 20824 tcp_accept(tcp, mp); 20825 break; 20826 case T_CONN_REQ: /* connection request */ 20827 tcp_connect(tcp, mp); 20828 break; 20829 case T_DISCON_REQ: /* disconnect request */ 20830 tcp_disconnect(tcp, mp); 20831 break; 20832 case T_CAPABILITY_REQ: 20833 tcp_capability_req(tcp, mp); /* capability request */ 20834 break; 20835 case T_INFO_REQ: /* information request */ 20836 tcp_info_req(tcp, mp); 20837 break; 20838 case T_SVR4_OPTMGMT_REQ: /* manage options req */ 20839 /* Only IP is allowed to return meaningful value */ 20840 (void) svr4_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 20841 break; 20842 case T_OPTMGMT_REQ: 20843 /* 20844 * Note: no support for snmpcom_req() through new 20845 * T_OPTMGMT_REQ. See comments in ip.c 20846 */ 20847 /* Only IP is allowed to return meaningful value */ 20848 (void) tpi_optcom_req(tcp->tcp_wq, mp, cr, &tcp_opt_obj); 20849 break; 20850 20851 case T_UNITDATA_REQ: /* unitdata request */ 20852 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 20853 break; 20854 case T_ORDREL_REQ: /* orderly release req */ 20855 freemsg(mp); 20856 20857 if (tcp->tcp_fused) 20858 tcp_unfuse(tcp); 20859 20860 if (tcp_xmit_end(tcp) != 0) { 20861 /* 20862 * We were crossing FINs and got a reset from 20863 * the other side. Just ignore it. 20864 */ 20865 if (tcp->tcp_debug) { 20866 (void) strlog(TCP_MOD_ID, 0, 1, 20867 SL_ERROR|SL_TRACE, 20868 "tcp_wput_proto, T_ORDREL_REQ out of " 20869 "state %s", 20870 tcp_display(tcp, NULL, 20871 DISP_ADDR_AND_PORT)); 20872 } 20873 } 20874 break; 20875 case T_ADDR_REQ: 20876 tcp_addr_req(tcp, mp); 20877 break; 20878 default: 20879 if (tcp->tcp_debug) { 20880 (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE, 20881 "tcp_wput_proto, bogus TPI msg, type %d", 20882 tprim->type); 20883 } 20884 /* 20885 * We used to M_ERROR. Sending TNOTSUPPORT gives the user 20886 * to recover. 20887 */ 20888 tcp_err_ack(tcp, mp, TNOTSUPPORT, 0); 20889 break; 20890 } 20891 } 20892 20893 /* 20894 * The TCP write service routine should never be called... 20895 */ 20896 /* ARGSUSED */ 20897 static void 20898 tcp_wsrv(queue_t *q) 20899 { 20900 TCP_STAT(tcp_wsrv_called); 20901 } 20902 20903 /* Non overlapping byte exchanger */ 20904 static void 20905 tcp_xchg(uchar_t *a, uchar_t *b, int len) 20906 { 20907 uchar_t uch; 20908 20909 while (len-- > 0) { 20910 uch = a[len]; 20911 a[len] = b[len]; 20912 b[len] = uch; 20913 } 20914 } 20915 20916 /* 20917 * Send out a control packet on the tcp connection specified. This routine 20918 * is typically called where we need a simple ACK or RST generated. 20919 */ 20920 static void 20921 tcp_xmit_ctl(char *str, tcp_t *tcp, uint32_t seq, uint32_t ack, int ctl) 20922 { 20923 uchar_t *rptr; 20924 tcph_t *tcph; 20925 ipha_t *ipha = NULL; 20926 ip6_t *ip6h = NULL; 20927 uint32_t sum; 20928 int tcp_hdr_len; 20929 int tcp_ip_hdr_len; 20930 mblk_t *mp; 20931 20932 /* 20933 * Save sum for use in source route later. 20934 */ 20935 ASSERT(tcp != NULL); 20936 sum = tcp->tcp_tcp_hdr_len + tcp->tcp_sum; 20937 tcp_hdr_len = tcp->tcp_hdr_len; 20938 tcp_ip_hdr_len = tcp->tcp_ip_hdr_len; 20939 20940 /* If a text string is passed in with the request, pass it to strlog. */ 20941 if (str != NULL && tcp->tcp_debug) { 20942 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 20943 "tcp_xmit_ctl: '%s', seq 0x%x, ack 0x%x, ctl 0x%x", 20944 str, seq, ack, ctl); 20945 } 20946 mp = allocb(tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 20947 BPRI_MED); 20948 if (mp == NULL) { 20949 return; 20950 } 20951 rptr = &mp->b_rptr[tcp_wroff_xtra]; 20952 mp->b_rptr = rptr; 20953 mp->b_wptr = &rptr[tcp_hdr_len]; 20954 bcopy(tcp->tcp_iphc, rptr, tcp_hdr_len); 20955 20956 if (tcp->tcp_ipversion == IPV4_VERSION) { 20957 ipha = (ipha_t *)rptr; 20958 ipha->ipha_length = htons(tcp_hdr_len); 20959 } else { 20960 ip6h = (ip6_t *)rptr; 20961 ASSERT(tcp != NULL); 20962 ip6h->ip6_plen = htons(tcp->tcp_hdr_len - 20963 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 20964 } 20965 tcph = (tcph_t *)&rptr[tcp_ip_hdr_len]; 20966 tcph->th_flags[0] = (uint8_t)ctl; 20967 if (ctl & TH_RST) { 20968 BUMP_MIB(&tcp_mib, tcpOutRsts); 20969 BUMP_MIB(&tcp_mib, tcpOutControl); 20970 /* 20971 * Don't send TSopt w/ TH_RST packets per RFC 1323. 20972 */ 20973 if (tcp->tcp_snd_ts_ok && 20974 tcp->tcp_state > TCPS_SYN_SENT) { 20975 mp->b_wptr = &rptr[tcp_hdr_len - TCPOPT_REAL_TS_LEN]; 20976 *(mp->b_wptr) = TCPOPT_EOL; 20977 if (tcp->tcp_ipversion == IPV4_VERSION) { 20978 ipha->ipha_length = htons(tcp_hdr_len - 20979 TCPOPT_REAL_TS_LEN); 20980 } else { 20981 ip6h->ip6_plen = htons(ntohs(ip6h->ip6_plen) - 20982 TCPOPT_REAL_TS_LEN); 20983 } 20984 tcph->th_offset_and_rsrvd[0] -= (3 << 4); 20985 sum -= TCPOPT_REAL_TS_LEN; 20986 } 20987 } 20988 if (ctl & TH_ACK) { 20989 if (tcp->tcp_snd_ts_ok) { 20990 U32_TO_BE32(lbolt, 20991 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 20992 U32_TO_BE32(tcp->tcp_ts_recent, 20993 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 20994 } 20995 20996 /* Update the latest receive window size in TCP header. */ 20997 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 20998 tcph->th_win); 20999 tcp->tcp_rack = ack; 21000 tcp->tcp_rack_cnt = 0; 21001 BUMP_MIB(&tcp_mib, tcpOutAck); 21002 } 21003 BUMP_LOCAL(tcp->tcp_obsegs); 21004 U32_TO_BE32(seq, tcph->th_seq); 21005 U32_TO_BE32(ack, tcph->th_ack); 21006 /* 21007 * Include the adjustment for a source route if any. 21008 */ 21009 sum = (sum >> 16) + (sum & 0xFFFF); 21010 U16_TO_BE16(sum, tcph->th_sum); 21011 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21012 tcp_send_data(tcp, tcp->tcp_wq, mp); 21013 } 21014 21015 /* 21016 * If this routine returns B_TRUE, TCP can generate a RST in response 21017 * to a segment. If it returns B_FALSE, TCP should not respond. 21018 */ 21019 static boolean_t 21020 tcp_send_rst_chk(void) 21021 { 21022 clock_t now; 21023 21024 /* 21025 * TCP needs to protect itself from generating too many RSTs. 21026 * This can be a DoS attack by sending us random segments 21027 * soliciting RSTs. 21028 * 21029 * What we do here is to have a limit of tcp_rst_sent_rate RSTs 21030 * in each 1 second interval. In this way, TCP still generate 21031 * RSTs in normal cases but when under attack, the impact is 21032 * limited. 21033 */ 21034 if (tcp_rst_sent_rate_enabled != 0) { 21035 now = lbolt; 21036 /* lbolt can wrap around. */ 21037 if ((tcp_last_rst_intrvl > now) || 21038 (TICK_TO_MSEC(now - tcp_last_rst_intrvl) > 1*SECONDS)) { 21039 tcp_last_rst_intrvl = now; 21040 tcp_rst_cnt = 1; 21041 } else if (++tcp_rst_cnt > tcp_rst_sent_rate) { 21042 return (B_FALSE); 21043 } 21044 } 21045 return (B_TRUE); 21046 } 21047 21048 /* 21049 * Send down the advice IP ioctl to tell IP to mark an IRE temporary. 21050 */ 21051 static void 21052 tcp_ip_ire_mark_advice(tcp_t *tcp) 21053 { 21054 mblk_t *mp; 21055 ipic_t *ipic; 21056 21057 if (tcp->tcp_ipversion == IPV4_VERSION) { 21058 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21059 &ipic); 21060 } else { 21061 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21062 &ipic); 21063 } 21064 if (mp == NULL) 21065 return; 21066 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21067 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21068 } 21069 21070 /* 21071 * Return an IP advice ioctl mblk and set ipic to be the pointer 21072 * to the advice structure. 21073 */ 21074 static mblk_t * 21075 tcp_ip_advise_mblk(void *addr, int addr_len, ipic_t **ipic) 21076 { 21077 struct iocblk *ioc; 21078 mblk_t *mp, *mp1; 21079 21080 mp = allocb(sizeof (ipic_t) + addr_len, BPRI_HI); 21081 if (mp == NULL) 21082 return (NULL); 21083 bzero(mp->b_rptr, sizeof (ipic_t) + addr_len); 21084 *ipic = (ipic_t *)mp->b_rptr; 21085 (*ipic)->ipic_cmd = IP_IOC_IRE_ADVISE_NO_REPLY; 21086 (*ipic)->ipic_addr_offset = sizeof (ipic_t); 21087 21088 bcopy(addr, *ipic + 1, addr_len); 21089 21090 (*ipic)->ipic_addr_length = addr_len; 21091 mp->b_wptr = &mp->b_rptr[sizeof (ipic_t) + addr_len]; 21092 21093 mp1 = mkiocb(IP_IOCTL); 21094 if (mp1 == NULL) { 21095 freemsg(mp); 21096 return (NULL); 21097 } 21098 mp1->b_cont = mp; 21099 ioc = (struct iocblk *)mp1->b_rptr; 21100 ioc->ioc_count = sizeof (ipic_t) + addr_len; 21101 21102 return (mp1); 21103 } 21104 21105 /* 21106 * Generate a reset based on an inbound packet for which there is no active 21107 * tcp state that we can find. 21108 * 21109 * IPSEC NOTE : Try to send the reply with the same protection as it came 21110 * in. We still have the ipsec_mp that the packet was attached to. Thus 21111 * the packet will go out at the same level of protection as it came in by 21112 * converting the IPSEC_IN to IPSEC_OUT. 21113 */ 21114 static void 21115 tcp_xmit_early_reset(char *str, mblk_t *mp, uint32_t seq, 21116 uint32_t ack, int ctl, uint_t ip_hdr_len) 21117 { 21118 ipha_t *ipha = NULL; 21119 ip6_t *ip6h = NULL; 21120 ushort_t len; 21121 tcph_t *tcph; 21122 int i; 21123 mblk_t *ipsec_mp; 21124 boolean_t mctl_present; 21125 ipic_t *ipic; 21126 ipaddr_t v4addr; 21127 in6_addr_t v6addr; 21128 int addr_len; 21129 void *addr; 21130 queue_t *q = tcp_g_q; 21131 tcp_t *tcp = Q_TO_TCP(q); 21132 21133 if (!tcp_send_rst_chk()) { 21134 tcp_rst_unsent++; 21135 freemsg(mp); 21136 return; 21137 } 21138 21139 if (mp->b_datap->db_type == M_CTL) { 21140 ipsec_mp = mp; 21141 mp = mp->b_cont; 21142 mctl_present = B_TRUE; 21143 } else { 21144 ipsec_mp = mp; 21145 mctl_present = B_FALSE; 21146 } 21147 21148 if (str && q && tcp_dbg) { 21149 (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE, 21150 "tcp_xmit_early_reset: '%s', seq 0x%x, ack 0x%x, " 21151 "flags 0x%x", 21152 str, seq, ack, ctl); 21153 } 21154 if (mp->b_datap->db_ref != 1) { 21155 mblk_t *mp1 = copyb(mp); 21156 freemsg(mp); 21157 mp = mp1; 21158 if (!mp) { 21159 if (mctl_present) 21160 freeb(ipsec_mp); 21161 return; 21162 } else { 21163 if (mctl_present) { 21164 ipsec_mp->b_cont = mp; 21165 } else { 21166 ipsec_mp = mp; 21167 } 21168 } 21169 } else if (mp->b_cont) { 21170 freemsg(mp->b_cont); 21171 mp->b_cont = NULL; 21172 } 21173 /* 21174 * We skip reversing source route here. 21175 * (for now we replace all IP options with EOL) 21176 */ 21177 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21178 ipha = (ipha_t *)mp->b_rptr; 21179 for (i = IP_SIMPLE_HDR_LENGTH; i < (int)ip_hdr_len; i++) 21180 mp->b_rptr[i] = IPOPT_EOL; 21181 /* 21182 * Make sure that src address isn't flagrantly invalid. 21183 * Not all broadcast address checking for the src address 21184 * is possible, since we don't know the netmask of the src 21185 * addr. No check for destination address is done, since 21186 * IP will not pass up a packet with a broadcast dest 21187 * address to TCP. Similar checks are done below for IPv6. 21188 */ 21189 if (ipha->ipha_src == 0 || ipha->ipha_src == INADDR_BROADCAST || 21190 CLASSD(ipha->ipha_src)) { 21191 freemsg(ipsec_mp); 21192 BUMP_MIB(&ip_mib, ipInDiscards); 21193 return; 21194 } 21195 } else { 21196 ip6h = (ip6_t *)mp->b_rptr; 21197 21198 if (IN6_IS_ADDR_UNSPECIFIED(&ip6h->ip6_src) || 21199 IN6_IS_ADDR_MULTICAST(&ip6h->ip6_src)) { 21200 freemsg(ipsec_mp); 21201 BUMP_MIB(&ip6_mib, ipv6InDiscards); 21202 return; 21203 } 21204 21205 /* Remove any extension headers assuming partial overlay */ 21206 if (ip_hdr_len > IPV6_HDR_LEN) { 21207 uint8_t *to; 21208 21209 to = mp->b_rptr + ip_hdr_len - IPV6_HDR_LEN; 21210 ovbcopy(ip6h, to, IPV6_HDR_LEN); 21211 mp->b_rptr += ip_hdr_len - IPV6_HDR_LEN; 21212 ip_hdr_len = IPV6_HDR_LEN; 21213 ip6h = (ip6_t *)mp->b_rptr; 21214 ip6h->ip6_nxt = IPPROTO_TCP; 21215 } 21216 } 21217 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len]; 21218 if (tcph->th_flags[0] & TH_RST) { 21219 freemsg(ipsec_mp); 21220 return; 21221 } 21222 tcph->th_offset_and_rsrvd[0] = (5 << 4); 21223 len = ip_hdr_len + sizeof (tcph_t); 21224 mp->b_wptr = &mp->b_rptr[len]; 21225 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21226 ipha->ipha_length = htons(len); 21227 /* Swap addresses */ 21228 v4addr = ipha->ipha_src; 21229 ipha->ipha_src = ipha->ipha_dst; 21230 ipha->ipha_dst = v4addr; 21231 ipha->ipha_ident = 0; 21232 ipha->ipha_ttl = (uchar_t)tcp_ipv4_ttl; 21233 addr_len = IP_ADDR_LEN; 21234 addr = &v4addr; 21235 } else { 21236 /* No ip6i_t in this case */ 21237 ip6h->ip6_plen = htons(len - IPV6_HDR_LEN); 21238 /* Swap addresses */ 21239 v6addr = ip6h->ip6_src; 21240 ip6h->ip6_src = ip6h->ip6_dst; 21241 ip6h->ip6_dst = v6addr; 21242 ip6h->ip6_hops = (uchar_t)tcp_ipv6_hoplimit; 21243 addr_len = IPV6_ADDR_LEN; 21244 addr = &v6addr; 21245 } 21246 tcp_xchg(tcph->th_fport, tcph->th_lport, 2); 21247 U32_TO_BE32(ack, tcph->th_ack); 21248 U32_TO_BE32(seq, tcph->th_seq); 21249 U16_TO_BE16(0, tcph->th_win); 21250 U16_TO_BE16(sizeof (tcph_t), tcph->th_sum); 21251 tcph->th_flags[0] = (uint8_t)ctl; 21252 if (ctl & TH_RST) { 21253 BUMP_MIB(&tcp_mib, tcpOutRsts); 21254 BUMP_MIB(&tcp_mib, tcpOutControl); 21255 } 21256 if (mctl_present) { 21257 ipsec_in_t *ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21258 21259 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21260 if (!ipsec_in_to_out(ipsec_mp, ipha, ip6h)) { 21261 return; 21262 } 21263 } 21264 /* 21265 * NOTE: one might consider tracing a TCP packet here, but 21266 * this function has no active TCP state nd no tcp structure 21267 * which has trace buffer. If we traced here, we would have 21268 * to keep a local trace buffer in tcp_record_trace(). 21269 */ 21270 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, ipsec_mp); 21271 21272 /* 21273 * Tell IP to mark the IRE used for this destination temporary. 21274 * This way, we can limit our exposure to DoS attack because IP 21275 * creates an IRE for each destination. If there are too many, 21276 * the time to do any routing lookup will be extremely long. And 21277 * the lookup can be in interrupt context. 21278 * 21279 * Note that in normal circumstances, this marking should not 21280 * affect anything. It would be nice if only 1 message is 21281 * needed to inform IP that the IRE created for this RST should 21282 * not be added to the cache table. But there is currently 21283 * not such communication mechanism between TCP and IP. So 21284 * the best we can do now is to send the advice ioctl to IP 21285 * to mark the IRE temporary. 21286 */ 21287 if ((mp = tcp_ip_advise_mblk(addr, addr_len, &ipic)) != NULL) { 21288 ipic->ipic_ire_marks |= IRE_MARK_TEMPORARY; 21289 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21290 } 21291 } 21292 21293 /* 21294 * Initiate closedown sequence on an active connection. (May be called as 21295 * writer.) Return value zero for OK return, non-zero for error return. 21296 */ 21297 static int 21298 tcp_xmit_end(tcp_t *tcp) 21299 { 21300 ipic_t *ipic; 21301 mblk_t *mp; 21302 21303 if (tcp->tcp_state < TCPS_SYN_RCVD || 21304 tcp->tcp_state > TCPS_CLOSE_WAIT) { 21305 /* 21306 * Invalid state, only states TCPS_SYN_RCVD, 21307 * TCPS_ESTABLISHED and TCPS_CLOSE_WAIT are valid 21308 */ 21309 return (-1); 21310 } 21311 21312 tcp->tcp_fss = tcp->tcp_snxt + tcp->tcp_unsent; 21313 tcp->tcp_valid_bits |= TCP_FSS_VALID; 21314 /* 21315 * If there is nothing more unsent, send the FIN now. 21316 * Otherwise, it will go out with the last segment. 21317 */ 21318 if (tcp->tcp_unsent == 0) { 21319 mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, 21320 tcp->tcp_fss, B_FALSE, NULL, B_FALSE); 21321 21322 if (mp) { 21323 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21324 tcp_send_data(tcp, tcp->tcp_wq, mp); 21325 } else { 21326 /* 21327 * Couldn't allocate msg. Pretend we got it out. 21328 * Wait for rexmit timeout. 21329 */ 21330 tcp->tcp_snxt = tcp->tcp_fss + 1; 21331 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21332 } 21333 21334 /* 21335 * If needed, update tcp_rexmit_snxt as tcp_snxt is 21336 * changed. 21337 */ 21338 if (tcp->tcp_rexmit && tcp->tcp_rexmit_nxt == tcp->tcp_fss) { 21339 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 21340 } 21341 } else { 21342 /* 21343 * If tcp->tcp_cork is set, then the data will not get sent, 21344 * so we have to check that and unset it first. 21345 */ 21346 if (tcp->tcp_cork) 21347 tcp->tcp_cork = B_FALSE; 21348 tcp_wput_data(tcp, NULL, B_FALSE); 21349 } 21350 21351 /* 21352 * If TCP does not get enough samples of RTT or tcp_rtt_updates 21353 * is 0, don't update the cache. 21354 */ 21355 if (tcp_rtt_updates == 0 || tcp->tcp_rtt_update < tcp_rtt_updates) 21356 return (0); 21357 21358 /* 21359 * NOTE: should not update if source routes i.e. if tcp_remote if 21360 * different from the destination. 21361 */ 21362 if (tcp->tcp_ipversion == IPV4_VERSION) { 21363 if (tcp->tcp_remote != tcp->tcp_ipha->ipha_dst) { 21364 return (0); 21365 } 21366 mp = tcp_ip_advise_mblk(&tcp->tcp_ipha->ipha_dst, IP_ADDR_LEN, 21367 &ipic); 21368 } else { 21369 if (!(IN6_ARE_ADDR_EQUAL(&tcp->tcp_remote_v6, 21370 &tcp->tcp_ip6h->ip6_dst))) { 21371 return (0); 21372 } 21373 mp = tcp_ip_advise_mblk(&tcp->tcp_ip6h->ip6_dst, IPV6_ADDR_LEN, 21374 &ipic); 21375 } 21376 21377 /* Record route attributes in the IRE for use by future connections. */ 21378 if (mp == NULL) 21379 return (0); 21380 21381 /* 21382 * We do not have a good algorithm to update ssthresh at this time. 21383 * So don't do any update. 21384 */ 21385 ipic->ipic_rtt = tcp->tcp_rtt_sa; 21386 ipic->ipic_rtt_sd = tcp->tcp_rtt_sd; 21387 21388 CALL_IP_WPUT(tcp->tcp_connp, tcp->tcp_wq, mp); 21389 return (0); 21390 } 21391 21392 /* 21393 * Generate a "no listener here" RST in response to an "unknown" segment. 21394 * Note that we are reusing the incoming mp to construct the outgoing 21395 * RST. 21396 */ 21397 void 21398 tcp_xmit_listeners_reset(mblk_t *mp, uint_t ip_hdr_len) 21399 { 21400 uchar_t *rptr; 21401 uint32_t seg_len; 21402 tcph_t *tcph; 21403 uint32_t seg_seq; 21404 uint32_t seg_ack; 21405 uint_t flags; 21406 mblk_t *ipsec_mp; 21407 ipha_t *ipha; 21408 ip6_t *ip6h; 21409 boolean_t mctl_present = B_FALSE; 21410 boolean_t check = B_TRUE; 21411 boolean_t policy_present; 21412 21413 TCP_STAT(tcp_no_listener); 21414 21415 ipsec_mp = mp; 21416 21417 if (mp->b_datap->db_type == M_CTL) { 21418 ipsec_in_t *ii; 21419 21420 mctl_present = B_TRUE; 21421 mp = mp->b_cont; 21422 21423 ii = (ipsec_in_t *)ipsec_mp->b_rptr; 21424 ASSERT(ii->ipsec_in_type == IPSEC_IN); 21425 if (ii->ipsec_in_dont_check) { 21426 check = B_FALSE; 21427 if (!ii->ipsec_in_secure) { 21428 freeb(ipsec_mp); 21429 mctl_present = B_FALSE; 21430 ipsec_mp = mp; 21431 } 21432 } 21433 } 21434 21435 if (IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION) { 21436 policy_present = ipsec_inbound_v4_policy_present; 21437 ipha = (ipha_t *)mp->b_rptr; 21438 ip6h = NULL; 21439 } else { 21440 policy_present = ipsec_inbound_v6_policy_present; 21441 ipha = NULL; 21442 ip6h = (ip6_t *)mp->b_rptr; 21443 } 21444 21445 if (check && policy_present) { 21446 /* 21447 * The conn_t parameter is NULL because we already know 21448 * nobody's home. 21449 */ 21450 ipsec_mp = ipsec_check_global_policy( 21451 ipsec_mp, (conn_t *)NULL, ipha, ip6h, mctl_present); 21452 if (ipsec_mp == NULL) 21453 return; 21454 } 21455 21456 21457 rptr = mp->b_rptr; 21458 21459 tcph = (tcph_t *)&rptr[ip_hdr_len]; 21460 seg_seq = BE32_TO_U32(tcph->th_seq); 21461 seg_ack = BE32_TO_U32(tcph->th_ack); 21462 flags = tcph->th_flags[0]; 21463 21464 seg_len = msgdsize(mp) - (TCP_HDR_LENGTH(tcph) + ip_hdr_len); 21465 if (flags & TH_RST) { 21466 freemsg(ipsec_mp); 21467 } else if (flags & TH_ACK) { 21468 tcp_xmit_early_reset("no tcp, reset", 21469 ipsec_mp, seg_ack, 0, TH_RST, ip_hdr_len); 21470 } else { 21471 if (flags & TH_SYN) { 21472 seg_len++; 21473 } else { 21474 /* 21475 * Here we violate the RFC. Note that a normal 21476 * TCP will never send a segment without the ACK 21477 * flag, except for RST or SYN segment. This 21478 * segment is neither. Just drop it on the 21479 * floor. 21480 */ 21481 freemsg(ipsec_mp); 21482 tcp_rst_unsent++; 21483 return; 21484 } 21485 21486 tcp_xmit_early_reset("no tcp, reset/ack", 21487 ipsec_mp, 0, seg_seq + seg_len, 21488 TH_RST | TH_ACK, ip_hdr_len); 21489 } 21490 } 21491 21492 /* 21493 * tcp_xmit_mp is called to return a pointer to an mblk chain complete with 21494 * ip and tcp header ready to pass down to IP. If the mp passed in is 21495 * non-NULL, then up to max_to_send bytes of data will be dup'ed off that 21496 * mblk. (If sendall is not set the dup'ing will stop at an mblk boundary 21497 * otherwise it will dup partial mblks.) 21498 * Otherwise, an appropriate ACK packet will be generated. This 21499 * routine is not usually called to send new data for the first time. It 21500 * is mostly called out of the timer for retransmits, and to generate ACKs. 21501 * 21502 * If offset is not NULL, the returned mblk chain's first mblk's b_rptr will 21503 * be adjusted by *offset. And after dupb(), the offset and the ending mblk 21504 * of the original mblk chain will be returned in *offset and *end_mp. 21505 */ 21506 static mblk_t * 21507 tcp_xmit_mp(tcp_t *tcp, mblk_t *mp, int32_t max_to_send, int32_t *offset, 21508 mblk_t **end_mp, uint32_t seq, boolean_t sendall, uint32_t *seg_len, 21509 boolean_t rexmit) 21510 { 21511 int data_length; 21512 int32_t off = 0; 21513 uint_t flags; 21514 mblk_t *mp1; 21515 mblk_t *mp2; 21516 uchar_t *rptr; 21517 tcph_t *tcph; 21518 int32_t num_sack_blk = 0; 21519 int32_t sack_opt_len = 0; 21520 21521 /* Allocate for our maximum TCP header + link-level */ 21522 mp1 = allocb(tcp->tcp_ip_hdr_len + TCP_MAX_HDR_LENGTH + tcp_wroff_xtra, 21523 BPRI_MED); 21524 if (!mp1) 21525 return (NULL); 21526 data_length = 0; 21527 21528 /* 21529 * Note that tcp_mss has been adjusted to take into account the 21530 * timestamp option if applicable. Because SACK options do not 21531 * appear in every TCP segments and they are of variable lengths, 21532 * they cannot be included in tcp_mss. Thus we need to calculate 21533 * the actual segment length when we need to send a segment which 21534 * includes SACK options. 21535 */ 21536 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 21537 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 21538 tcp->tcp_num_sack_blk); 21539 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 21540 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 21541 if (max_to_send + sack_opt_len > tcp->tcp_mss) 21542 max_to_send -= sack_opt_len; 21543 } 21544 21545 if (offset != NULL) { 21546 off = *offset; 21547 /* We use offset as an indicator that end_mp is not NULL. */ 21548 *end_mp = NULL; 21549 } 21550 for (mp2 = mp1; mp && data_length != max_to_send; mp = mp->b_cont) { 21551 /* This could be faster with cooperation from downstream */ 21552 if (mp2 != mp1 && !sendall && 21553 data_length + (int)(mp->b_wptr - mp->b_rptr) > 21554 max_to_send) 21555 /* 21556 * Don't send the next mblk since the whole mblk 21557 * does not fit. 21558 */ 21559 break; 21560 mp2->b_cont = dupb(mp); 21561 mp2 = mp2->b_cont; 21562 if (!mp2) { 21563 freemsg(mp1); 21564 return (NULL); 21565 } 21566 mp2->b_rptr += off; 21567 ASSERT((uintptr_t)(mp2->b_wptr - mp2->b_rptr) <= 21568 (uintptr_t)INT_MAX); 21569 21570 data_length += (int)(mp2->b_wptr - mp2->b_rptr); 21571 if (data_length > max_to_send) { 21572 mp2->b_wptr -= data_length - max_to_send; 21573 data_length = max_to_send; 21574 off = mp2->b_wptr - mp->b_rptr; 21575 break; 21576 } else { 21577 off = 0; 21578 } 21579 } 21580 if (offset != NULL) { 21581 *offset = off; 21582 *end_mp = mp; 21583 } 21584 if (seg_len != NULL) { 21585 *seg_len = data_length; 21586 } 21587 21588 /* Update the latest receive window size in TCP header. */ 21589 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 21590 tcp->tcp_tcph->th_win); 21591 21592 rptr = mp1->b_rptr + tcp_wroff_xtra; 21593 mp1->b_rptr = rptr; 21594 mp1->b_wptr = rptr + tcp->tcp_hdr_len + sack_opt_len; 21595 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 21596 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 21597 U32_TO_ABE32(seq, tcph->th_seq); 21598 21599 /* 21600 * Use tcp_unsent to determine if the PUSH bit should be used assumes 21601 * that this function was called from tcp_wput_data. Thus, when called 21602 * to retransmit data the setting of the PUSH bit may appear some 21603 * what random in that it might get set when it should not. This 21604 * should not pose any performance issues. 21605 */ 21606 if (data_length != 0 && (tcp->tcp_unsent == 0 || 21607 tcp->tcp_unsent == data_length)) { 21608 flags = TH_ACK | TH_PUSH; 21609 } else { 21610 flags = TH_ACK; 21611 } 21612 21613 if (tcp->tcp_ecn_ok) { 21614 if (tcp->tcp_ecn_echo_on) 21615 flags |= TH_ECE; 21616 21617 /* 21618 * Only set ECT bit and ECN_CWR if a segment contains new data. 21619 * There is no TCP flow control for non-data segments, and 21620 * only data segment is transmitted reliably. 21621 */ 21622 if (data_length > 0 && !rexmit) { 21623 SET_ECT(tcp, rptr); 21624 if (tcp->tcp_cwr && !tcp->tcp_ecn_cwr_sent) { 21625 flags |= TH_CWR; 21626 tcp->tcp_ecn_cwr_sent = B_TRUE; 21627 } 21628 } 21629 } 21630 21631 if (tcp->tcp_valid_bits) { 21632 uint32_t u1; 21633 21634 if ((tcp->tcp_valid_bits & TCP_ISS_VALID) && 21635 seq == tcp->tcp_iss) { 21636 uchar_t *wptr; 21637 21638 /* 21639 * If TCP_ISS_VALID and the seq number is tcp_iss, 21640 * TCP can only be in SYN-SENT, SYN-RCVD or 21641 * FIN-WAIT-1 state. It can be FIN-WAIT-1 if 21642 * our SYN is not ack'ed but the app closes this 21643 * TCP connection. 21644 */ 21645 ASSERT(tcp->tcp_state == TCPS_SYN_SENT || 21646 tcp->tcp_state == TCPS_SYN_RCVD || 21647 tcp->tcp_state == TCPS_FIN_WAIT_1); 21648 21649 /* 21650 * Tack on the MSS option. It is always needed 21651 * for both active and passive open. 21652 * 21653 * MSS option value should be interface MTU - MIN 21654 * TCP/IP header according to RFC 793 as it means 21655 * the maximum segment size TCP can receive. But 21656 * to get around some broken middle boxes/end hosts 21657 * out there, we allow the option value to be the 21658 * same as the MSS option size on the peer side. 21659 * In this way, the other side will not send 21660 * anything larger than they can receive. 21661 * 21662 * Note that for SYN_SENT state, the ndd param 21663 * tcp_use_smss_as_mss_opt has no effect as we 21664 * don't know the peer's MSS option value. So 21665 * the only case we need to take care of is in 21666 * SYN_RCVD state, which is done later. 21667 */ 21668 wptr = mp1->b_wptr; 21669 wptr[0] = TCPOPT_MAXSEG; 21670 wptr[1] = TCPOPT_MAXSEG_LEN; 21671 wptr += 2; 21672 u1 = tcp->tcp_if_mtu - 21673 (tcp->tcp_ipversion == IPV4_VERSION ? 21674 IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN) - 21675 TCP_MIN_HEADER_LENGTH; 21676 U16_TO_BE16(u1, wptr); 21677 mp1->b_wptr = wptr + 2; 21678 /* Update the offset to cover the additional word */ 21679 tcph->th_offset_and_rsrvd[0] += (1 << 4); 21680 21681 /* 21682 * Note that the following way of filling in 21683 * TCP options are not optimal. Some NOPs can 21684 * be saved. But there is no need at this time 21685 * to optimize it. When it is needed, we will 21686 * do it. 21687 */ 21688 switch (tcp->tcp_state) { 21689 case TCPS_SYN_SENT: 21690 flags = TH_SYN; 21691 21692 if (tcp->tcp_snd_ts_ok) { 21693 uint32_t llbolt = (uint32_t)lbolt; 21694 21695 wptr = mp1->b_wptr; 21696 wptr[0] = TCPOPT_NOP; 21697 wptr[1] = TCPOPT_NOP; 21698 wptr[2] = TCPOPT_TSTAMP; 21699 wptr[3] = TCPOPT_TSTAMP_LEN; 21700 wptr += 4; 21701 U32_TO_BE32(llbolt, wptr); 21702 wptr += 4; 21703 ASSERT(tcp->tcp_ts_recent == 0); 21704 U32_TO_BE32(0L, wptr); 21705 mp1->b_wptr += TCPOPT_REAL_TS_LEN; 21706 tcph->th_offset_and_rsrvd[0] += 21707 (3 << 4); 21708 } 21709 21710 /* 21711 * Set up all the bits to tell other side 21712 * we are ECN capable. 21713 */ 21714 if (tcp->tcp_ecn_ok) { 21715 flags |= (TH_ECE | TH_CWR); 21716 } 21717 break; 21718 case TCPS_SYN_RCVD: 21719 flags |= TH_SYN; 21720 21721 /* 21722 * Reset the MSS option value to be SMSS 21723 * We should probably add back the bytes 21724 * for timestamp option and IPsec. We 21725 * don't do that as this is a workaround 21726 * for broken middle boxes/end hosts, it 21727 * is better for us to be more cautious. 21728 * They may not take these things into 21729 * account in their SMSS calculation. Thus 21730 * the peer's calculated SMSS may be smaller 21731 * than what it can be. This should be OK. 21732 */ 21733 if (tcp_use_smss_as_mss_opt) { 21734 u1 = tcp->tcp_mss; 21735 U16_TO_BE16(u1, wptr); 21736 } 21737 21738 /* 21739 * If the other side is ECN capable, reply 21740 * that we are also ECN capable. 21741 */ 21742 if (tcp->tcp_ecn_ok) 21743 flags |= TH_ECE; 21744 break; 21745 default: 21746 /* 21747 * The above ASSERT() makes sure that this 21748 * must be FIN-WAIT-1 state. Our SYN has 21749 * not been ack'ed so retransmit it. 21750 */ 21751 flags |= TH_SYN; 21752 break; 21753 } 21754 21755 if (tcp->tcp_snd_ws_ok) { 21756 wptr = mp1->b_wptr; 21757 wptr[0] = TCPOPT_NOP; 21758 wptr[1] = TCPOPT_WSCALE; 21759 wptr[2] = TCPOPT_WS_LEN; 21760 wptr[3] = (uchar_t)tcp->tcp_rcv_ws; 21761 mp1->b_wptr += TCPOPT_REAL_WS_LEN; 21762 tcph->th_offset_and_rsrvd[0] += (1 << 4); 21763 } 21764 21765 if (tcp->tcp_snd_sack_ok) { 21766 wptr = mp1->b_wptr; 21767 wptr[0] = TCPOPT_NOP; 21768 wptr[1] = TCPOPT_NOP; 21769 wptr[2] = TCPOPT_SACK_PERMITTED; 21770 wptr[3] = TCPOPT_SACK_OK_LEN; 21771 mp1->b_wptr += TCPOPT_REAL_SACK_OK_LEN; 21772 tcph->th_offset_and_rsrvd[0] += (1 << 4); 21773 } 21774 21775 /* allocb() of adequate mblk assures space */ 21776 ASSERT((uintptr_t)(mp1->b_wptr - mp1->b_rptr) <= 21777 (uintptr_t)INT_MAX); 21778 u1 = (int)(mp1->b_wptr - mp1->b_rptr); 21779 /* 21780 * Get IP set to checksum on our behalf 21781 * Include the adjustment for a source route if any. 21782 */ 21783 u1 += tcp->tcp_sum; 21784 u1 = (u1 >> 16) + (u1 & 0xFFFF); 21785 U16_TO_BE16(u1, tcph->th_sum); 21786 BUMP_MIB(&tcp_mib, tcpOutControl); 21787 } 21788 if ((tcp->tcp_valid_bits & TCP_FSS_VALID) && 21789 (seq + data_length) == tcp->tcp_fss) { 21790 if (!tcp->tcp_fin_acked) { 21791 flags |= TH_FIN; 21792 BUMP_MIB(&tcp_mib, tcpOutControl); 21793 } 21794 if (!tcp->tcp_fin_sent) { 21795 tcp->tcp_fin_sent = B_TRUE; 21796 switch (tcp->tcp_state) { 21797 case TCPS_SYN_RCVD: 21798 case TCPS_ESTABLISHED: 21799 tcp->tcp_state = TCPS_FIN_WAIT_1; 21800 break; 21801 case TCPS_CLOSE_WAIT: 21802 tcp->tcp_state = TCPS_LAST_ACK; 21803 break; 21804 } 21805 if (tcp->tcp_suna == tcp->tcp_snxt) 21806 TCP_TIMER_RESTART(tcp, tcp->tcp_rto); 21807 tcp->tcp_snxt = tcp->tcp_fss + 1; 21808 } 21809 } 21810 /* 21811 * Note the trick here. u1 is unsigned. When tcp_urg 21812 * is smaller than seq, u1 will become a very huge value. 21813 * So the comparison will fail. Also note that tcp_urp 21814 * should be positive, see RFC 793 page 17. 21815 */ 21816 u1 = tcp->tcp_urg - seq + TCP_OLD_URP_INTERPRETATION; 21817 if ((tcp->tcp_valid_bits & TCP_URG_VALID) && u1 != 0 && 21818 u1 < (uint32_t)(64 * 1024)) { 21819 flags |= TH_URG; 21820 BUMP_MIB(&tcp_mib, tcpOutUrg); 21821 U32_TO_ABE16(u1, tcph->th_urp); 21822 } 21823 } 21824 tcph->th_flags[0] = (uchar_t)flags; 21825 tcp->tcp_rack = tcp->tcp_rnxt; 21826 tcp->tcp_rack_cnt = 0; 21827 21828 if (tcp->tcp_snd_ts_ok) { 21829 if (tcp->tcp_state != TCPS_SYN_SENT) { 21830 uint32_t llbolt = (uint32_t)lbolt; 21831 21832 U32_TO_BE32(llbolt, 21833 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 21834 U32_TO_BE32(tcp->tcp_ts_recent, 21835 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 21836 } 21837 } 21838 21839 if (num_sack_blk > 0) { 21840 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 21841 sack_blk_t *tmp; 21842 int32_t i; 21843 21844 wptr[0] = TCPOPT_NOP; 21845 wptr[1] = TCPOPT_NOP; 21846 wptr[2] = TCPOPT_SACK; 21847 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 21848 sizeof (sack_blk_t); 21849 wptr += TCPOPT_REAL_SACK_LEN; 21850 21851 tmp = tcp->tcp_sack_list; 21852 for (i = 0; i < num_sack_blk; i++) { 21853 U32_TO_BE32(tmp[i].begin, wptr); 21854 wptr += sizeof (tcp_seq); 21855 U32_TO_BE32(tmp[i].end, wptr); 21856 wptr += sizeof (tcp_seq); 21857 } 21858 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) << 4); 21859 } 21860 ASSERT((uintptr_t)(mp1->b_wptr - rptr) <= (uintptr_t)INT_MAX); 21861 data_length += (int)(mp1->b_wptr - rptr); 21862 if (tcp->tcp_ipversion == IPV4_VERSION) { 21863 ((ipha_t *)rptr)->ipha_length = htons(data_length); 21864 } else { 21865 ip6_t *ip6 = (ip6_t *)(rptr + 21866 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 21867 sizeof (ip6i_t) : 0)); 21868 21869 ip6->ip6_plen = htons(data_length - 21870 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 21871 } 21872 21873 /* 21874 * Prime pump for IP 21875 * Include the adjustment for a source route if any. 21876 */ 21877 data_length -= tcp->tcp_ip_hdr_len; 21878 data_length += tcp->tcp_sum; 21879 data_length = (data_length >> 16) + (data_length & 0xFFFF); 21880 U16_TO_ABE16(data_length, tcph->th_sum); 21881 if (tcp->tcp_ip_forward_progress) { 21882 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 21883 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 21884 tcp->tcp_ip_forward_progress = B_FALSE; 21885 } 21886 return (mp1); 21887 } 21888 21889 /* This function handles the push timeout. */ 21890 void 21891 tcp_push_timer(void *arg) 21892 { 21893 conn_t *connp = (conn_t *)arg; 21894 tcp_t *tcp = connp->conn_tcp; 21895 21896 TCP_DBGSTAT(tcp_push_timer_cnt); 21897 21898 ASSERT(tcp->tcp_listener == NULL); 21899 21900 /* 21901 * We need to stop synchronous streams temporarily to prevent a race 21902 * with tcp_fuse_rrw() or tcp_fusion rinfop(). It is safe to access 21903 * tcp_rcv_list here because those entry points will return right 21904 * away when synchronous streams is stopped. 21905 */ 21906 TCP_FUSE_SYNCSTR_STOP(tcp); 21907 tcp->tcp_push_tid = 0; 21908 if ((tcp->tcp_rcv_list != NULL) && 21909 (tcp_rcv_drain(tcp->tcp_rq, tcp) == TH_ACK_NEEDED)) 21910 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, tcp->tcp_rnxt, TH_ACK); 21911 TCP_FUSE_SYNCSTR_RESUME(tcp); 21912 } 21913 21914 /* 21915 * This function handles delayed ACK timeout. 21916 */ 21917 static void 21918 tcp_ack_timer(void *arg) 21919 { 21920 conn_t *connp = (conn_t *)arg; 21921 tcp_t *tcp = connp->conn_tcp; 21922 mblk_t *mp; 21923 21924 TCP_DBGSTAT(tcp_ack_timer_cnt); 21925 21926 tcp->tcp_ack_tid = 0; 21927 21928 if (tcp->tcp_fused) 21929 return; 21930 21931 /* 21932 * Do not send ACK if there is no outstanding unack'ed data. 21933 */ 21934 if (tcp->tcp_rnxt == tcp->tcp_rack) { 21935 return; 21936 } 21937 21938 if ((tcp->tcp_rnxt - tcp->tcp_rack) > tcp->tcp_mss) { 21939 /* 21940 * Make sure we don't allow deferred ACKs to result in 21941 * timer-based ACKing. If we have held off an ACK 21942 * when there was more than an mss here, and the timer 21943 * goes off, we have to worry about the possibility 21944 * that the sender isn't doing slow-start, or is out 21945 * of step with us for some other reason. We fall 21946 * permanently back in the direction of 21947 * ACK-every-other-packet as suggested in RFC 1122. 21948 */ 21949 if (tcp->tcp_rack_abs_max > 2) 21950 tcp->tcp_rack_abs_max--; 21951 tcp->tcp_rack_cur_max = 2; 21952 } 21953 mp = tcp_ack_mp(tcp); 21954 21955 if (mp != NULL) { 21956 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_SEND_PKT); 21957 BUMP_LOCAL(tcp->tcp_obsegs); 21958 BUMP_MIB(&tcp_mib, tcpOutAck); 21959 BUMP_MIB(&tcp_mib, tcpOutAckDelayed); 21960 tcp_send_data(tcp, tcp->tcp_wq, mp); 21961 } 21962 } 21963 21964 21965 /* Generate an ACK-only (no data) segment for a TCP endpoint */ 21966 static mblk_t * 21967 tcp_ack_mp(tcp_t *tcp) 21968 { 21969 uint32_t seq_no; 21970 21971 /* 21972 * There are a few cases to be considered while setting the sequence no. 21973 * Essentially, we can come here while processing an unacceptable pkt 21974 * in the TCPS_SYN_RCVD state, in which case we set the sequence number 21975 * to snxt (per RFC 793), note the swnd wouldn't have been set yet. 21976 * If we are here for a zero window probe, stick with suna. In all 21977 * other cases, we check if suna + swnd encompasses snxt and set 21978 * the sequence number to snxt, if so. If snxt falls outside the 21979 * window (the receiver probably shrunk its window), we will go with 21980 * suna + swnd, otherwise the sequence no will be unacceptable to the 21981 * receiver. 21982 */ 21983 if (tcp->tcp_zero_win_probe) { 21984 seq_no = tcp->tcp_suna; 21985 } else if (tcp->tcp_state == TCPS_SYN_RCVD) { 21986 ASSERT(tcp->tcp_swnd == 0); 21987 seq_no = tcp->tcp_snxt; 21988 } else { 21989 seq_no = SEQ_GT(tcp->tcp_snxt, 21990 (tcp->tcp_suna + tcp->tcp_swnd)) ? 21991 (tcp->tcp_suna + tcp->tcp_swnd) : tcp->tcp_snxt; 21992 } 21993 21994 if (tcp->tcp_valid_bits) { 21995 /* 21996 * For the complex case where we have to send some 21997 * controls (FIN or SYN), let tcp_xmit_mp do it. 21998 */ 21999 return (tcp_xmit_mp(tcp, NULL, 0, NULL, NULL, seq_no, B_FALSE, 22000 NULL, B_FALSE)); 22001 } else { 22002 /* Generate a simple ACK */ 22003 int data_length; 22004 uchar_t *rptr; 22005 tcph_t *tcph; 22006 mblk_t *mp1; 22007 int32_t tcp_hdr_len; 22008 int32_t tcp_tcp_hdr_len; 22009 int32_t num_sack_blk = 0; 22010 int32_t sack_opt_len; 22011 22012 /* 22013 * Allocate space for TCP + IP headers 22014 * and link-level header 22015 */ 22016 if (tcp->tcp_snd_sack_ok && tcp->tcp_num_sack_blk > 0) { 22017 num_sack_blk = MIN(tcp->tcp_max_sack_blk, 22018 tcp->tcp_num_sack_blk); 22019 sack_opt_len = num_sack_blk * sizeof (sack_blk_t) + 22020 TCPOPT_NOP_LEN * 2 + TCPOPT_HEADER_LEN; 22021 tcp_hdr_len = tcp->tcp_hdr_len + sack_opt_len; 22022 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len + sack_opt_len; 22023 } else { 22024 tcp_hdr_len = tcp->tcp_hdr_len; 22025 tcp_tcp_hdr_len = tcp->tcp_tcp_hdr_len; 22026 } 22027 mp1 = allocb(tcp_hdr_len + tcp_wroff_xtra, BPRI_MED); 22028 if (!mp1) 22029 return (NULL); 22030 22031 /* Update the latest receive window size in TCP header. */ 22032 U32_TO_ABE16(tcp->tcp_rwnd >> tcp->tcp_rcv_ws, 22033 tcp->tcp_tcph->th_win); 22034 /* copy in prototype TCP + IP header */ 22035 rptr = mp1->b_rptr + tcp_wroff_xtra; 22036 mp1->b_rptr = rptr; 22037 mp1->b_wptr = rptr + tcp_hdr_len; 22038 bcopy(tcp->tcp_iphc, rptr, tcp->tcp_hdr_len); 22039 22040 tcph = (tcph_t *)&rptr[tcp->tcp_ip_hdr_len]; 22041 22042 /* Set the TCP sequence number. */ 22043 U32_TO_ABE32(seq_no, tcph->th_seq); 22044 22045 /* Set up the TCP flag field. */ 22046 tcph->th_flags[0] = (uchar_t)TH_ACK; 22047 if (tcp->tcp_ecn_echo_on) 22048 tcph->th_flags[0] |= TH_ECE; 22049 22050 tcp->tcp_rack = tcp->tcp_rnxt; 22051 tcp->tcp_rack_cnt = 0; 22052 22053 /* fill in timestamp option if in use */ 22054 if (tcp->tcp_snd_ts_ok) { 22055 uint32_t llbolt = (uint32_t)lbolt; 22056 22057 U32_TO_BE32(llbolt, 22058 (char *)tcph+TCP_MIN_HEADER_LENGTH+4); 22059 U32_TO_BE32(tcp->tcp_ts_recent, 22060 (char *)tcph+TCP_MIN_HEADER_LENGTH+8); 22061 } 22062 22063 /* Fill in SACK options */ 22064 if (num_sack_blk > 0) { 22065 uchar_t *wptr = (uchar_t *)tcph + tcp->tcp_tcp_hdr_len; 22066 sack_blk_t *tmp; 22067 int32_t i; 22068 22069 wptr[0] = TCPOPT_NOP; 22070 wptr[1] = TCPOPT_NOP; 22071 wptr[2] = TCPOPT_SACK; 22072 wptr[3] = TCPOPT_HEADER_LEN + num_sack_blk * 22073 sizeof (sack_blk_t); 22074 wptr += TCPOPT_REAL_SACK_LEN; 22075 22076 tmp = tcp->tcp_sack_list; 22077 for (i = 0; i < num_sack_blk; i++) { 22078 U32_TO_BE32(tmp[i].begin, wptr); 22079 wptr += sizeof (tcp_seq); 22080 U32_TO_BE32(tmp[i].end, wptr); 22081 wptr += sizeof (tcp_seq); 22082 } 22083 tcph->th_offset_and_rsrvd[0] += ((num_sack_blk * 2 + 1) 22084 << 4); 22085 } 22086 22087 if (tcp->tcp_ipversion == IPV4_VERSION) { 22088 ((ipha_t *)rptr)->ipha_length = htons(tcp_hdr_len); 22089 } else { 22090 /* Check for ip6i_t header in sticky hdrs */ 22091 ip6_t *ip6 = (ip6_t *)(rptr + 22092 (((ip6_t *)rptr)->ip6_nxt == IPPROTO_RAW ? 22093 sizeof (ip6i_t) : 0)); 22094 22095 ip6->ip6_plen = htons(tcp_hdr_len - 22096 ((char *)&tcp->tcp_ip6h[1] - tcp->tcp_iphc)); 22097 } 22098 22099 /* 22100 * Prime pump for checksum calculation in IP. Include the 22101 * adjustment for a source route if any. 22102 */ 22103 data_length = tcp_tcp_hdr_len + tcp->tcp_sum; 22104 data_length = (data_length >> 16) + (data_length & 0xFFFF); 22105 U16_TO_ABE16(data_length, tcph->th_sum); 22106 22107 if (tcp->tcp_ip_forward_progress) { 22108 ASSERT(tcp->tcp_ipversion == IPV6_VERSION); 22109 *(uint32_t *)mp1->b_rptr |= IP_FORWARD_PROG; 22110 tcp->tcp_ip_forward_progress = B_FALSE; 22111 } 22112 return (mp1); 22113 } 22114 } 22115 22116 /* 22117 * To create a temporary tcp structure for inserting into bind hash list. 22118 * The parameter is assumed to be in network byte order, ready for use. 22119 */ 22120 /* ARGSUSED */ 22121 static tcp_t * 22122 tcp_alloc_temp_tcp(in_port_t port) 22123 { 22124 conn_t *connp; 22125 tcp_t *tcp; 22126 22127 connp = ipcl_conn_create(IPCL_TCPCONN, KM_SLEEP); 22128 if (connp == NULL) 22129 return (NULL); 22130 22131 tcp = connp->conn_tcp; 22132 22133 /* 22134 * Only initialize the necessary info in those structures. Note 22135 * that since INADDR_ANY is all 0, we do not need to set 22136 * tcp_bound_source to INADDR_ANY here. 22137 */ 22138 tcp->tcp_state = TCPS_BOUND; 22139 tcp->tcp_lport = port; 22140 tcp->tcp_exclbind = 1; 22141 tcp->tcp_reserved_port = 1; 22142 22143 /* Just for place holding... */ 22144 tcp->tcp_ipversion = IPV4_VERSION; 22145 22146 return (tcp); 22147 } 22148 22149 /* 22150 * To remove a port range specified by lo_port and hi_port from the 22151 * reserved port ranges. This is one of the three public functions of 22152 * the reserved port interface. Note that a port range has to be removed 22153 * as a whole. Ports in a range cannot be removed individually. 22154 * 22155 * Params: 22156 * in_port_t lo_port: the beginning port of the reserved port range to 22157 * be deleted. 22158 * in_port_t hi_port: the ending port of the reserved port range to 22159 * be deleted. 22160 * 22161 * Return: 22162 * B_TRUE if the deletion is successful, B_FALSE otherwise. 22163 */ 22164 boolean_t 22165 tcp_reserved_port_del(in_port_t lo_port, in_port_t hi_port) 22166 { 22167 int i, j; 22168 int size; 22169 tcp_t **temp_tcp_array; 22170 tcp_t *tcp; 22171 22172 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 22173 22174 /* First make sure that the port ranage is indeed reserved. */ 22175 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22176 if (tcp_reserved_port[i].lo_port == lo_port) { 22177 hi_port = tcp_reserved_port[i].hi_port; 22178 temp_tcp_array = tcp_reserved_port[i].temp_tcp_array; 22179 break; 22180 } 22181 } 22182 if (i == tcp_reserved_port_array_size) { 22183 rw_exit(&tcp_reserved_port_lock); 22184 return (B_FALSE); 22185 } 22186 22187 /* 22188 * Remove the range from the array. This simple loop is possible 22189 * because port ranges are inserted in ascending order. 22190 */ 22191 for (j = i; j < tcp_reserved_port_array_size - 1; j++) { 22192 tcp_reserved_port[j].lo_port = tcp_reserved_port[j+1].lo_port; 22193 tcp_reserved_port[j].hi_port = tcp_reserved_port[j+1].hi_port; 22194 tcp_reserved_port[j].temp_tcp_array = 22195 tcp_reserved_port[j+1].temp_tcp_array; 22196 } 22197 22198 /* Remove all the temporary tcp structures. */ 22199 size = hi_port - lo_port + 1; 22200 while (size > 0) { 22201 tcp = temp_tcp_array[size - 1]; 22202 ASSERT(tcp != NULL); 22203 tcp_bind_hash_remove(tcp); 22204 CONN_DEC_REF(tcp->tcp_connp); 22205 size--; 22206 } 22207 kmem_free(temp_tcp_array, (hi_port - lo_port + 1) * sizeof (tcp_t *)); 22208 tcp_reserved_port_array_size--; 22209 rw_exit(&tcp_reserved_port_lock); 22210 return (B_TRUE); 22211 } 22212 22213 /* 22214 * Macro to remove temporary tcp structure from the bind hash list. The 22215 * first parameter is the list of tcp to be removed. The second parameter 22216 * is the number of tcps in the array. 22217 */ 22218 #define TCP_TMP_TCP_REMOVE(tcp_array, num) \ 22219 { \ 22220 while ((num) > 0) { \ 22221 tcp_t *tcp = (tcp_array)[(num) - 1]; \ 22222 tf_t *tbf; \ 22223 tcp_t *tcpnext; \ 22224 tbf = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)]; \ 22225 mutex_enter(&tbf->tf_lock); \ 22226 tcpnext = tcp->tcp_bind_hash; \ 22227 if (tcpnext) { \ 22228 tcpnext->tcp_ptpbhn = \ 22229 tcp->tcp_ptpbhn; \ 22230 } \ 22231 *tcp->tcp_ptpbhn = tcpnext; \ 22232 mutex_exit(&tbf->tf_lock); \ 22233 kmem_free(tcp, sizeof (tcp_t)); \ 22234 (tcp_array)[(num) - 1] = NULL; \ 22235 (num)--; \ 22236 } \ 22237 } 22238 22239 /* 22240 * The public interface for other modules to call to reserve a port range 22241 * in TCP. The caller passes in how large a port range it wants. TCP 22242 * will try to find a range and return it via lo_port and hi_port. This is 22243 * used by NCA's nca_conn_init. 22244 * NCA can only be used in the global zone so this only affects the global 22245 * zone's ports. 22246 * 22247 * Params: 22248 * int size: the size of the port range to be reserved. 22249 * in_port_t *lo_port (referenced): returns the beginning port of the 22250 * reserved port range added. 22251 * in_port_t *hi_port (referenced): returns the ending port of the 22252 * reserved port range added. 22253 * 22254 * Return: 22255 * B_TRUE if the port reservation is successful, B_FALSE otherwise. 22256 */ 22257 boolean_t 22258 tcp_reserved_port_add(int size, in_port_t *lo_port, in_port_t *hi_port) 22259 { 22260 tcp_t *tcp; 22261 tcp_t *tmp_tcp; 22262 tcp_t **temp_tcp_array; 22263 tf_t *tbf; 22264 in_port_t net_port; 22265 in_port_t port; 22266 int32_t cur_size; 22267 int i, j; 22268 boolean_t used; 22269 tcp_rport_t tmp_ports[TCP_RESERVED_PORTS_ARRAY_MAX_SIZE]; 22270 zoneid_t zoneid = GLOBAL_ZONEID; 22271 22272 /* Sanity check. */ 22273 if (size <= 0 || size > TCP_RESERVED_PORTS_RANGE_MAX) { 22274 return (B_FALSE); 22275 } 22276 22277 rw_enter(&tcp_reserved_port_lock, RW_WRITER); 22278 if (tcp_reserved_port_array_size == TCP_RESERVED_PORTS_ARRAY_MAX_SIZE) { 22279 rw_exit(&tcp_reserved_port_lock); 22280 return (B_FALSE); 22281 } 22282 22283 /* 22284 * Find the starting port to try. Since the port ranges are ordered 22285 * in the reserved port array, we can do a simple search here. 22286 */ 22287 *lo_port = TCP_SMALLEST_RESERVED_PORT; 22288 *hi_port = TCP_LARGEST_RESERVED_PORT; 22289 for (i = 0; i < tcp_reserved_port_array_size; 22290 *lo_port = tcp_reserved_port[i].hi_port + 1, i++) { 22291 if (tcp_reserved_port[i].lo_port - *lo_port >= size) { 22292 *hi_port = tcp_reserved_port[i].lo_port - 1; 22293 break; 22294 } 22295 } 22296 /* No available port range. */ 22297 if (i == tcp_reserved_port_array_size && *hi_port - *lo_port < size) { 22298 rw_exit(&tcp_reserved_port_lock); 22299 return (B_FALSE); 22300 } 22301 22302 temp_tcp_array = kmem_zalloc(size * sizeof (tcp_t *), KM_NOSLEEP); 22303 if (temp_tcp_array == NULL) { 22304 rw_exit(&tcp_reserved_port_lock); 22305 return (B_FALSE); 22306 } 22307 22308 /* Go thru the port range to see if some ports are already bound. */ 22309 for (port = *lo_port, cur_size = 0; 22310 cur_size < size && port <= *hi_port; 22311 cur_size++, port++) { 22312 used = B_FALSE; 22313 net_port = htons(port); 22314 tbf = &tcp_bind_fanout[TCP_BIND_HASH(net_port)]; 22315 mutex_enter(&tbf->tf_lock); 22316 for (tcp = tbf->tf_tcp; tcp != NULL; 22317 tcp = tcp->tcp_bind_hash) { 22318 if (zoneid == tcp->tcp_connp->conn_zoneid && 22319 net_port == tcp->tcp_lport) { 22320 /* 22321 * A port is already bound. Search again 22322 * starting from port + 1. Release all 22323 * temporary tcps. 22324 */ 22325 mutex_exit(&tbf->tf_lock); 22326 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22327 *lo_port = port + 1; 22328 cur_size = -1; 22329 used = B_TRUE; 22330 break; 22331 } 22332 } 22333 if (!used) { 22334 if ((tmp_tcp = tcp_alloc_temp_tcp(net_port)) == NULL) { 22335 /* 22336 * Allocation failure. Just fail the request. 22337 * Need to remove all those temporary tcp 22338 * structures. 22339 */ 22340 mutex_exit(&tbf->tf_lock); 22341 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22342 rw_exit(&tcp_reserved_port_lock); 22343 kmem_free(temp_tcp_array, 22344 (hi_port - lo_port + 1) * 22345 sizeof (tcp_t *)); 22346 return (B_FALSE); 22347 } 22348 temp_tcp_array[cur_size] = tmp_tcp; 22349 tcp_bind_hash_insert(tbf, tmp_tcp, B_TRUE); 22350 mutex_exit(&tbf->tf_lock); 22351 } 22352 } 22353 22354 /* 22355 * The current range is not large enough. We can actually do another 22356 * search if this search is done between 2 reserved port ranges. But 22357 * for first release, we just stop here and return saying that no port 22358 * range is available. 22359 */ 22360 if (cur_size < size) { 22361 TCP_TMP_TCP_REMOVE(temp_tcp_array, cur_size); 22362 rw_exit(&tcp_reserved_port_lock); 22363 kmem_free(temp_tcp_array, size * sizeof (tcp_t *)); 22364 return (B_FALSE); 22365 } 22366 *hi_port = port - 1; 22367 22368 /* 22369 * Insert range into array in ascending order. Since this function 22370 * must not be called often, we choose to use the simplest method. 22371 * The above array should not consume excessive stack space as 22372 * the size must be very small. If in future releases, we find 22373 * that we should provide more reserved port ranges, this function 22374 * has to be modified to be more efficient. 22375 */ 22376 if (tcp_reserved_port_array_size == 0) { 22377 tcp_reserved_port[0].lo_port = *lo_port; 22378 tcp_reserved_port[0].hi_port = *hi_port; 22379 tcp_reserved_port[0].temp_tcp_array = temp_tcp_array; 22380 } else { 22381 for (i = 0, j = 0; i < tcp_reserved_port_array_size; i++, j++) { 22382 if (*lo_port < tcp_reserved_port[i].lo_port && i == j) { 22383 tmp_ports[j].lo_port = *lo_port; 22384 tmp_ports[j].hi_port = *hi_port; 22385 tmp_ports[j].temp_tcp_array = temp_tcp_array; 22386 j++; 22387 } 22388 tmp_ports[j].lo_port = tcp_reserved_port[i].lo_port; 22389 tmp_ports[j].hi_port = tcp_reserved_port[i].hi_port; 22390 tmp_ports[j].temp_tcp_array = 22391 tcp_reserved_port[i].temp_tcp_array; 22392 } 22393 if (j == i) { 22394 tmp_ports[j].lo_port = *lo_port; 22395 tmp_ports[j].hi_port = *hi_port; 22396 tmp_ports[j].temp_tcp_array = temp_tcp_array; 22397 } 22398 bcopy(tmp_ports, tcp_reserved_port, sizeof (tmp_ports)); 22399 } 22400 tcp_reserved_port_array_size++; 22401 rw_exit(&tcp_reserved_port_lock); 22402 return (B_TRUE); 22403 } 22404 22405 /* 22406 * Check to see if a port is in any reserved port range. 22407 * 22408 * Params: 22409 * in_port_t port: the port to be verified. 22410 * 22411 * Return: 22412 * B_TRUE is the port is inside a reserved port range, B_FALSE otherwise. 22413 */ 22414 boolean_t 22415 tcp_reserved_port_check(in_port_t port) 22416 { 22417 int i; 22418 22419 rw_enter(&tcp_reserved_port_lock, RW_READER); 22420 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22421 if (port >= tcp_reserved_port[i].lo_port || 22422 port <= tcp_reserved_port[i].hi_port) { 22423 rw_exit(&tcp_reserved_port_lock); 22424 return (B_TRUE); 22425 } 22426 } 22427 rw_exit(&tcp_reserved_port_lock); 22428 return (B_FALSE); 22429 } 22430 22431 /* 22432 * To list all reserved port ranges. This is the function to handle 22433 * ndd tcp_reserved_port_list. 22434 */ 22435 /* ARGSUSED */ 22436 static int 22437 tcp_reserved_port_list(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 22438 { 22439 int i; 22440 22441 rw_enter(&tcp_reserved_port_lock, RW_READER); 22442 if (tcp_reserved_port_array_size > 0) 22443 (void) mi_mpprintf(mp, "The following ports are reserved:"); 22444 else 22445 (void) mi_mpprintf(mp, "No port is reserved."); 22446 for (i = 0; i < tcp_reserved_port_array_size; i++) { 22447 (void) mi_mpprintf(mp, "%d-%d", 22448 tcp_reserved_port[i].lo_port, tcp_reserved_port[i].hi_port); 22449 } 22450 rw_exit(&tcp_reserved_port_lock); 22451 return (0); 22452 } 22453 22454 /* 22455 * Hash list insertion routine for tcp_t structures. 22456 * Inserts entries with the ones bound to a specific IP address first 22457 * followed by those bound to INADDR_ANY. 22458 */ 22459 static void 22460 tcp_bind_hash_insert(tf_t *tbf, tcp_t *tcp, int caller_holds_lock) 22461 { 22462 tcp_t **tcpp; 22463 tcp_t *tcpnext; 22464 22465 if (tcp->tcp_ptpbhn != NULL) { 22466 ASSERT(!caller_holds_lock); 22467 tcp_bind_hash_remove(tcp); 22468 } 22469 tcpp = &tbf->tf_tcp; 22470 if (!caller_holds_lock) { 22471 mutex_enter(&tbf->tf_lock); 22472 } else { 22473 ASSERT(MUTEX_HELD(&tbf->tf_lock)); 22474 } 22475 tcpnext = tcpp[0]; 22476 if (tcpnext) { 22477 /* 22478 * If the new tcp bound to the INADDR_ANY address 22479 * and the first one in the list is not bound to 22480 * INADDR_ANY we skip all entries until we find the 22481 * first one bound to INADDR_ANY. 22482 * This makes sure that applications binding to a 22483 * specific address get preference over those binding to 22484 * INADDR_ANY. 22485 */ 22486 if (V6_OR_V4_INADDR_ANY(tcp->tcp_bound_source_v6) && 22487 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) { 22488 while ((tcpnext = tcpp[0]) != NULL && 22489 !V6_OR_V4_INADDR_ANY(tcpnext->tcp_bound_source_v6)) 22490 tcpp = &(tcpnext->tcp_bind_hash); 22491 if (tcpnext) 22492 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 22493 } else 22494 tcpnext->tcp_ptpbhn = &tcp->tcp_bind_hash; 22495 } 22496 tcp->tcp_bind_hash = tcpnext; 22497 tcp->tcp_ptpbhn = tcpp; 22498 tcpp[0] = tcp; 22499 if (!caller_holds_lock) 22500 mutex_exit(&tbf->tf_lock); 22501 } 22502 22503 /* 22504 * Hash list removal routine for tcp_t structures. 22505 */ 22506 static void 22507 tcp_bind_hash_remove(tcp_t *tcp) 22508 { 22509 tcp_t *tcpnext; 22510 kmutex_t *lockp; 22511 22512 if (tcp->tcp_ptpbhn == NULL) 22513 return; 22514 22515 /* 22516 * Extract the lock pointer in case there are concurrent 22517 * hash_remove's for this instance. 22518 */ 22519 ASSERT(tcp->tcp_lport != 0); 22520 lockp = &tcp_bind_fanout[TCP_BIND_HASH(tcp->tcp_lport)].tf_lock; 22521 22522 ASSERT(lockp != NULL); 22523 mutex_enter(lockp); 22524 if (tcp->tcp_ptpbhn) { 22525 tcpnext = tcp->tcp_bind_hash; 22526 if (tcpnext) { 22527 tcpnext->tcp_ptpbhn = tcp->tcp_ptpbhn; 22528 tcp->tcp_bind_hash = NULL; 22529 } 22530 *tcp->tcp_ptpbhn = tcpnext; 22531 tcp->tcp_ptpbhn = NULL; 22532 } 22533 mutex_exit(lockp); 22534 } 22535 22536 22537 /* 22538 * Hash list lookup routine for tcp_t structures. 22539 * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. 22540 */ 22541 static tcp_t * 22542 tcp_acceptor_hash_lookup(t_uscalar_t id) 22543 { 22544 tf_t *tf; 22545 tcp_t *tcp; 22546 22547 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 22548 mutex_enter(&tf->tf_lock); 22549 for (tcp = tf->tf_tcp; tcp != NULL; 22550 tcp = tcp->tcp_acceptor_hash) { 22551 if (tcp->tcp_acceptor_id == id) { 22552 CONN_INC_REF(tcp->tcp_connp); 22553 mutex_exit(&tf->tf_lock); 22554 return (tcp); 22555 } 22556 } 22557 mutex_exit(&tf->tf_lock); 22558 return (NULL); 22559 } 22560 22561 22562 /* 22563 * Hash list insertion routine for tcp_t structures. 22564 */ 22565 void 22566 tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp) 22567 { 22568 tf_t *tf; 22569 tcp_t **tcpp; 22570 tcp_t *tcpnext; 22571 22572 tf = &tcp_acceptor_fanout[TCP_ACCEPTOR_HASH(id)]; 22573 22574 if (tcp->tcp_ptpahn != NULL) 22575 tcp_acceptor_hash_remove(tcp); 22576 tcpp = &tf->tf_tcp; 22577 mutex_enter(&tf->tf_lock); 22578 tcpnext = tcpp[0]; 22579 if (tcpnext) 22580 tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash; 22581 tcp->tcp_acceptor_hash = tcpnext; 22582 tcp->tcp_ptpahn = tcpp; 22583 tcpp[0] = tcp; 22584 tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */ 22585 mutex_exit(&tf->tf_lock); 22586 } 22587 22588 /* 22589 * Hash list removal routine for tcp_t structures. 22590 */ 22591 static void 22592 tcp_acceptor_hash_remove(tcp_t *tcp) 22593 { 22594 tcp_t *tcpnext; 22595 kmutex_t *lockp; 22596 22597 /* 22598 * Extract the lock pointer in case there are concurrent 22599 * hash_remove's for this instance. 22600 */ 22601 lockp = tcp->tcp_acceptor_lockp; 22602 22603 if (tcp->tcp_ptpahn == NULL) 22604 return; 22605 22606 ASSERT(lockp != NULL); 22607 mutex_enter(lockp); 22608 if (tcp->tcp_ptpahn) { 22609 tcpnext = tcp->tcp_acceptor_hash; 22610 if (tcpnext) { 22611 tcpnext->tcp_ptpahn = tcp->tcp_ptpahn; 22612 tcp->tcp_acceptor_hash = NULL; 22613 } 22614 *tcp->tcp_ptpahn = tcpnext; 22615 tcp->tcp_ptpahn = NULL; 22616 } 22617 mutex_exit(lockp); 22618 tcp->tcp_acceptor_lockp = NULL; 22619 } 22620 22621 /* ARGSUSED */ 22622 static int 22623 tcp_host_param_setvalue(queue_t *q, mblk_t *mp, char *value, caddr_t cp, int af) 22624 { 22625 int error = 0; 22626 int retval; 22627 char *end; 22628 22629 tcp_hsp_t *hsp; 22630 tcp_hsp_t *hspprev; 22631 22632 ipaddr_t addr = 0; /* Address we're looking for */ 22633 in6_addr_t v6addr; /* Address we're looking for */ 22634 uint32_t hash; /* Hash of that address */ 22635 22636 /* 22637 * If the following variables are still zero after parsing the input 22638 * string, the user didn't specify them and we don't change them in 22639 * the HSP. 22640 */ 22641 22642 ipaddr_t mask = 0; /* Subnet mask */ 22643 in6_addr_t v6mask; 22644 long sendspace = 0; /* Send buffer size */ 22645 long recvspace = 0; /* Receive buffer size */ 22646 long timestamp = 0; /* Originate TCP TSTAMP option, 1 = yes */ 22647 boolean_t delete = B_FALSE; /* User asked to delete this HSP */ 22648 22649 rw_enter(&tcp_hsp_lock, RW_WRITER); 22650 22651 /* Parse and validate address */ 22652 if (af == AF_INET) { 22653 retval = inet_pton(af, value, &addr); 22654 if (retval == 1) 22655 IN6_IPADDR_TO_V4MAPPED(addr, &v6addr); 22656 } else if (af == AF_INET6) { 22657 retval = inet_pton(af, value, &v6addr); 22658 } else { 22659 error = EINVAL; 22660 goto done; 22661 } 22662 if (retval == 0) { 22663 error = EINVAL; 22664 goto done; 22665 } 22666 22667 while ((*value) && *value != ' ') 22668 value++; 22669 22670 /* Parse individual keywords, set variables if found */ 22671 while (*value) { 22672 /* Skip leading blanks */ 22673 22674 while (*value == ' ' || *value == '\t') 22675 value++; 22676 22677 /* If at end of string, we're done */ 22678 22679 if (!*value) 22680 break; 22681 22682 /* We have a word, figure out what it is */ 22683 22684 if (strncmp("mask", value, 4) == 0) { 22685 value += 4; 22686 while (*value == ' ' || *value == '\t') 22687 value++; 22688 /* Parse subnet mask */ 22689 if (af == AF_INET) { 22690 retval = inet_pton(af, value, &mask); 22691 if (retval == 1) { 22692 V4MASK_TO_V6(mask, v6mask); 22693 } 22694 } else if (af == AF_INET6) { 22695 retval = inet_pton(af, value, &v6mask); 22696 } 22697 if (retval != 1) { 22698 error = EINVAL; 22699 goto done; 22700 } 22701 while ((*value) && *value != ' ') 22702 value++; 22703 } else if (strncmp("sendspace", value, 9) == 0) { 22704 value += 9; 22705 22706 if (ddi_strtol(value, &end, 0, &sendspace) != 0 || 22707 sendspace < TCP_XMIT_HIWATER || 22708 sendspace >= (1L<<30)) { 22709 error = EINVAL; 22710 goto done; 22711 } 22712 value = end; 22713 } else if (strncmp("recvspace", value, 9) == 0) { 22714 value += 9; 22715 22716 if (ddi_strtol(value, &end, 0, &recvspace) != 0 || 22717 recvspace < TCP_RECV_HIWATER || 22718 recvspace >= (1L<<30)) { 22719 error = EINVAL; 22720 goto done; 22721 } 22722 value = end; 22723 } else if (strncmp("timestamp", value, 9) == 0) { 22724 value += 9; 22725 22726 if (ddi_strtol(value, &end, 0, ×tamp) != 0 || 22727 timestamp < 0 || timestamp > 1) { 22728 error = EINVAL; 22729 goto done; 22730 } 22731 22732 /* 22733 * We increment timestamp so we know it's been set; 22734 * this is undone when we put it in the HSP 22735 */ 22736 timestamp++; 22737 value = end; 22738 } else if (strncmp("delete", value, 6) == 0) { 22739 value += 6; 22740 delete = B_TRUE; 22741 } else { 22742 error = EINVAL; 22743 goto done; 22744 } 22745 } 22746 22747 /* Hash address for lookup */ 22748 22749 hash = TCP_HSP_HASH(addr); 22750 22751 if (delete) { 22752 /* 22753 * Note that deletes don't return an error if the thing 22754 * we're trying to delete isn't there. 22755 */ 22756 if (tcp_hsp_hash == NULL) 22757 goto done; 22758 hsp = tcp_hsp_hash[hash]; 22759 22760 if (hsp) { 22761 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 22762 &v6addr)) { 22763 tcp_hsp_hash[hash] = hsp->tcp_hsp_next; 22764 mi_free((char *)hsp); 22765 } else { 22766 hspprev = hsp; 22767 while ((hsp = hsp->tcp_hsp_next) != NULL) { 22768 if (IN6_ARE_ADDR_EQUAL( 22769 &hsp->tcp_hsp_addr_v6, &v6addr)) { 22770 hspprev->tcp_hsp_next = 22771 hsp->tcp_hsp_next; 22772 mi_free((char *)hsp); 22773 break; 22774 } 22775 hspprev = hsp; 22776 } 22777 } 22778 } 22779 } else { 22780 /* 22781 * We're adding/modifying an HSP. If we haven't already done 22782 * so, allocate the hash table. 22783 */ 22784 22785 if (!tcp_hsp_hash) { 22786 tcp_hsp_hash = (tcp_hsp_t **) 22787 mi_zalloc(sizeof (tcp_hsp_t *) * TCP_HSP_HASH_SIZE); 22788 if (!tcp_hsp_hash) { 22789 error = EINVAL; 22790 goto done; 22791 } 22792 } 22793 22794 /* Get head of hash chain */ 22795 22796 hsp = tcp_hsp_hash[hash]; 22797 22798 /* Try to find pre-existing hsp on hash chain */ 22799 /* Doesn't handle CIDR prefixes. */ 22800 while (hsp) { 22801 if (IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, &v6addr)) 22802 break; 22803 hsp = hsp->tcp_hsp_next; 22804 } 22805 22806 /* 22807 * If we didn't, create one with default values and put it 22808 * at head of hash chain 22809 */ 22810 22811 if (!hsp) { 22812 hsp = (tcp_hsp_t *)mi_zalloc(sizeof (tcp_hsp_t)); 22813 if (!hsp) { 22814 error = EINVAL; 22815 goto done; 22816 } 22817 hsp->tcp_hsp_next = tcp_hsp_hash[hash]; 22818 tcp_hsp_hash[hash] = hsp; 22819 } 22820 22821 /* Set values that the user asked us to change */ 22822 22823 hsp->tcp_hsp_addr_v6 = v6addr; 22824 if (IN6_IS_ADDR_V4MAPPED(&v6addr)) 22825 hsp->tcp_hsp_vers = IPV4_VERSION; 22826 else 22827 hsp->tcp_hsp_vers = IPV6_VERSION; 22828 hsp->tcp_hsp_subnet_v6 = v6mask; 22829 if (sendspace > 0) 22830 hsp->tcp_hsp_sendspace = sendspace; 22831 if (recvspace > 0) 22832 hsp->tcp_hsp_recvspace = recvspace; 22833 if (timestamp > 0) 22834 hsp->tcp_hsp_tstamp = timestamp - 1; 22835 } 22836 22837 done: 22838 rw_exit(&tcp_hsp_lock); 22839 return (error); 22840 } 22841 22842 /* Set callback routine passed to nd_load by tcp_param_register. */ 22843 /* ARGSUSED */ 22844 static int 22845 tcp_host_param_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, cred_t *cr) 22846 { 22847 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET)); 22848 } 22849 /* ARGSUSED */ 22850 static int 22851 tcp_host_param_set_ipv6(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 22852 cred_t *cr) 22853 { 22854 return (tcp_host_param_setvalue(q, mp, value, cp, AF_INET6)); 22855 } 22856 22857 /* TCP host parameters report triggered via the Named Dispatch mechanism. */ 22858 /* ARGSUSED */ 22859 static int 22860 tcp_host_param_report(queue_t *q, mblk_t *mp, caddr_t cp, cred_t *cr) 22861 { 22862 tcp_hsp_t *hsp; 22863 int i; 22864 char addrbuf[INET6_ADDRSTRLEN], subnetbuf[INET6_ADDRSTRLEN]; 22865 22866 rw_enter(&tcp_hsp_lock, RW_READER); 22867 (void) mi_mpprintf(mp, 22868 "Hash HSP " MI_COL_HDRPAD_STR 22869 "Address Subnet Mask Send Receive TStamp"); 22870 if (tcp_hsp_hash) { 22871 for (i = 0; i < TCP_HSP_HASH_SIZE; i++) { 22872 hsp = tcp_hsp_hash[i]; 22873 while (hsp) { 22874 if (hsp->tcp_hsp_vers == IPV4_VERSION) { 22875 (void) inet_ntop(AF_INET, 22876 &hsp->tcp_hsp_addr, 22877 addrbuf, sizeof (addrbuf)); 22878 (void) inet_ntop(AF_INET, 22879 &hsp->tcp_hsp_subnet, 22880 subnetbuf, sizeof (subnetbuf)); 22881 } else { 22882 (void) inet_ntop(AF_INET6, 22883 &hsp->tcp_hsp_addr_v6, 22884 addrbuf, sizeof (addrbuf)); 22885 (void) inet_ntop(AF_INET6, 22886 &hsp->tcp_hsp_subnet_v6, 22887 subnetbuf, sizeof (subnetbuf)); 22888 } 22889 (void) mi_mpprintf(mp, 22890 " %03d " MI_COL_PTRFMT_STR 22891 "%s %s %010d %010d %d", 22892 i, 22893 (void *)hsp, 22894 addrbuf, 22895 subnetbuf, 22896 hsp->tcp_hsp_sendspace, 22897 hsp->tcp_hsp_recvspace, 22898 hsp->tcp_hsp_tstamp); 22899 22900 hsp = hsp->tcp_hsp_next; 22901 } 22902 } 22903 } 22904 rw_exit(&tcp_hsp_lock); 22905 return (0); 22906 } 22907 22908 22909 /* Data for fast netmask macro used by tcp_hsp_lookup */ 22910 22911 static ipaddr_t netmasks[] = { 22912 IN_CLASSA_NET, IN_CLASSA_NET, IN_CLASSB_NET, 22913 IN_CLASSC_NET | IN_CLASSD_NET /* Class C,D,E */ 22914 }; 22915 22916 #define netmask(addr) (netmasks[(ipaddr_t)(addr) >> 30]) 22917 22918 /* 22919 * XXX This routine should go away and instead we should use the metrics 22920 * associated with the routes to determine the default sndspace and rcvspace. 22921 */ 22922 static tcp_hsp_t * 22923 tcp_hsp_lookup(ipaddr_t addr) 22924 { 22925 tcp_hsp_t *hsp = NULL; 22926 22927 /* Quick check without acquiring the lock. */ 22928 if (tcp_hsp_hash == NULL) 22929 return (NULL); 22930 22931 rw_enter(&tcp_hsp_lock, RW_READER); 22932 22933 /* This routine finds the best-matching HSP for address addr. */ 22934 22935 if (tcp_hsp_hash) { 22936 int i; 22937 ipaddr_t srchaddr; 22938 tcp_hsp_t *hsp_net; 22939 22940 /* We do three passes: host, network, and subnet. */ 22941 22942 srchaddr = addr; 22943 22944 for (i = 1; i <= 3; i++) { 22945 /* Look for exact match on srchaddr */ 22946 22947 hsp = tcp_hsp_hash[TCP_HSP_HASH(srchaddr)]; 22948 while (hsp) { 22949 if (hsp->tcp_hsp_vers == IPV4_VERSION && 22950 hsp->tcp_hsp_addr == srchaddr) 22951 break; 22952 hsp = hsp->tcp_hsp_next; 22953 } 22954 ASSERT(hsp == NULL || 22955 hsp->tcp_hsp_vers == IPV4_VERSION); 22956 22957 /* 22958 * If this is the first pass: 22959 * If we found a match, great, return it. 22960 * If not, search for the network on the second pass. 22961 */ 22962 22963 if (i == 1) 22964 if (hsp) 22965 break; 22966 else 22967 { 22968 srchaddr = addr & netmask(addr); 22969 continue; 22970 } 22971 22972 /* 22973 * If this is the second pass: 22974 * If we found a match, but there's a subnet mask, 22975 * save the match but try again using the subnet 22976 * mask on the third pass. 22977 * Otherwise, return whatever we found. 22978 */ 22979 22980 if (i == 2) { 22981 if (hsp && hsp->tcp_hsp_subnet) { 22982 hsp_net = hsp; 22983 srchaddr = addr & hsp->tcp_hsp_subnet; 22984 continue; 22985 } else { 22986 break; 22987 } 22988 } 22989 22990 /* 22991 * This must be the third pass. If we didn't find 22992 * anything, return the saved network HSP instead. 22993 */ 22994 22995 if (!hsp) 22996 hsp = hsp_net; 22997 } 22998 } 22999 23000 rw_exit(&tcp_hsp_lock); 23001 return (hsp); 23002 } 23003 23004 /* 23005 * XXX Equally broken as the IPv4 routine. Doesn't handle longest 23006 * match lookup. 23007 */ 23008 static tcp_hsp_t * 23009 tcp_hsp_lookup_ipv6(in6_addr_t *v6addr) 23010 { 23011 tcp_hsp_t *hsp = NULL; 23012 23013 /* Quick check without acquiring the lock. */ 23014 if (tcp_hsp_hash == NULL) 23015 return (NULL); 23016 23017 rw_enter(&tcp_hsp_lock, RW_READER); 23018 23019 /* This routine finds the best-matching HSP for address addr. */ 23020 23021 if (tcp_hsp_hash) { 23022 int i; 23023 in6_addr_t v6srchaddr; 23024 tcp_hsp_t *hsp_net; 23025 23026 /* We do three passes: host, network, and subnet. */ 23027 23028 v6srchaddr = *v6addr; 23029 23030 for (i = 1; i <= 3; i++) { 23031 /* Look for exact match on srchaddr */ 23032 23033 hsp = tcp_hsp_hash[TCP_HSP_HASH( 23034 V4_PART_OF_V6(v6srchaddr))]; 23035 while (hsp) { 23036 if (hsp->tcp_hsp_vers == IPV6_VERSION && 23037 IN6_ARE_ADDR_EQUAL(&hsp->tcp_hsp_addr_v6, 23038 &v6srchaddr)) 23039 break; 23040 hsp = hsp->tcp_hsp_next; 23041 } 23042 23043 /* 23044 * If this is the first pass: 23045 * If we found a match, great, return it. 23046 * If not, search for the network on the second pass. 23047 */ 23048 23049 if (i == 1) 23050 if (hsp) 23051 break; 23052 else { 23053 /* Assume a 64 bit mask */ 23054 v6srchaddr.s6_addr32[0] = 23055 v6addr->s6_addr32[0]; 23056 v6srchaddr.s6_addr32[1] = 23057 v6addr->s6_addr32[1]; 23058 v6srchaddr.s6_addr32[2] = 0; 23059 v6srchaddr.s6_addr32[3] = 0; 23060 continue; 23061 } 23062 23063 /* 23064 * If this is the second pass: 23065 * If we found a match, but there's a subnet mask, 23066 * save the match but try again using the subnet 23067 * mask on the third pass. 23068 * Otherwise, return whatever we found. 23069 */ 23070 23071 if (i == 2) { 23072 ASSERT(hsp == NULL || 23073 hsp->tcp_hsp_vers == IPV6_VERSION); 23074 if (hsp && 23075 !IN6_IS_ADDR_UNSPECIFIED( 23076 &hsp->tcp_hsp_subnet_v6)) { 23077 hsp_net = hsp; 23078 V6_MASK_COPY(*v6addr, 23079 hsp->tcp_hsp_subnet_v6, v6srchaddr); 23080 continue; 23081 } else { 23082 break; 23083 } 23084 } 23085 23086 /* 23087 * This must be the third pass. If we didn't find 23088 * anything, return the saved network HSP instead. 23089 */ 23090 23091 if (!hsp) 23092 hsp = hsp_net; 23093 } 23094 } 23095 23096 rw_exit(&tcp_hsp_lock); 23097 return (hsp); 23098 } 23099 23100 /* 23101 * Type three generator adapted from the random() function in 4.4 BSD: 23102 */ 23103 23104 /* 23105 * Copyright (c) 1983, 1993 23106 * The Regents of the University of California. All rights reserved. 23107 * 23108 * Redistribution and use in source and binary forms, with or without 23109 * modification, are permitted provided that the following conditions 23110 * are met: 23111 * 1. Redistributions of source code must retain the above copyright 23112 * notice, this list of conditions and the following disclaimer. 23113 * 2. Redistributions in binary form must reproduce the above copyright 23114 * notice, this list of conditions and the following disclaimer in the 23115 * documentation and/or other materials provided with the distribution. 23116 * 3. All advertising materials mentioning features or use of this software 23117 * must display the following acknowledgement: 23118 * This product includes software developed by the University of 23119 * California, Berkeley and its contributors. 23120 * 4. Neither the name of the University nor the names of its contributors 23121 * may be used to endorse or promote products derived from this software 23122 * without specific prior written permission. 23123 * 23124 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23125 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23126 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23127 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23128 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23129 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23130 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23131 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23132 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23133 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23134 * SUCH DAMAGE. 23135 */ 23136 23137 /* Type 3 -- x**31 + x**3 + 1 */ 23138 #define DEG_3 31 23139 #define SEP_3 3 23140 23141 23142 /* Protected by tcp_random_lock */ 23143 static int tcp_randtbl[DEG_3 + 1]; 23144 23145 static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1]; 23146 static int *tcp_random_rptr = &tcp_randtbl[1]; 23147 23148 static int *tcp_random_state = &tcp_randtbl[1]; 23149 static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1]; 23150 23151 kmutex_t tcp_random_lock; 23152 23153 void 23154 tcp_random_init(void) 23155 { 23156 int i; 23157 hrtime_t hrt; 23158 time_t wallclock; 23159 uint64_t result; 23160 23161 /* 23162 * Use high-res timer and current time for seed. Gethrtime() returns 23163 * a longlong, which may contain resolution down to nanoseconds. 23164 * The current time will either be a 32-bit or a 64-bit quantity. 23165 * XOR the two together in a 64-bit result variable. 23166 * Convert the result to a 32-bit value by multiplying the high-order 23167 * 32-bits by the low-order 32-bits. 23168 */ 23169 23170 hrt = gethrtime(); 23171 (void) drv_getparm(TIME, &wallclock); 23172 result = (uint64_t)wallclock ^ (uint64_t)hrt; 23173 mutex_enter(&tcp_random_lock); 23174 tcp_random_state[0] = ((result >> 32) & 0xffffffff) * 23175 (result & 0xffffffff); 23176 23177 for (i = 1; i < DEG_3; i++) 23178 tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1] 23179 + 12345; 23180 tcp_random_fptr = &tcp_random_state[SEP_3]; 23181 tcp_random_rptr = &tcp_random_state[0]; 23182 mutex_exit(&tcp_random_lock); 23183 for (i = 0; i < 10 * DEG_3; i++) 23184 (void) tcp_random(); 23185 } 23186 23187 /* 23188 * tcp_random: Return a random number in the range [1 - (128K + 1)]. 23189 * This range is selected to be approximately centered on TCP_ISS / 2, 23190 * and easy to compute. We get this value by generating a 32-bit random 23191 * number, selecting out the high-order 17 bits, and then adding one so 23192 * that we never return zero. 23193 */ 23194 int 23195 tcp_random(void) 23196 { 23197 int i; 23198 23199 mutex_enter(&tcp_random_lock); 23200 *tcp_random_fptr += *tcp_random_rptr; 23201 23202 /* 23203 * The high-order bits are more random than the low-order bits, 23204 * so we select out the high-order 17 bits and add one so that 23205 * we never return zero. 23206 */ 23207 i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1; 23208 if (++tcp_random_fptr >= tcp_random_end_ptr) { 23209 tcp_random_fptr = tcp_random_state; 23210 ++tcp_random_rptr; 23211 } else if (++tcp_random_rptr >= tcp_random_end_ptr) 23212 tcp_random_rptr = tcp_random_state; 23213 23214 mutex_exit(&tcp_random_lock); 23215 return (i); 23216 } 23217 23218 /* 23219 * XXX This will go away when TPI is extended to send 23220 * info reqs to sockfs/timod ..... 23221 * Given a queue, set the max packet size for the write 23222 * side of the queue below stream head. This value is 23223 * cached on the stream head. 23224 * Returns 1 on success, 0 otherwise. 23225 */ 23226 static int 23227 setmaxps(queue_t *q, int maxpsz) 23228 { 23229 struct stdata *stp; 23230 queue_t *wq; 23231 stp = STREAM(q); 23232 23233 /* 23234 * At this point change of a queue parameter is not allowed 23235 * when a multiplexor is sitting on top. 23236 */ 23237 if (stp->sd_flag & STPLEX) 23238 return (0); 23239 23240 claimstr(stp->sd_wrq); 23241 wq = stp->sd_wrq->q_next; 23242 ASSERT(wq != NULL); 23243 (void) strqset(wq, QMAXPSZ, 0, maxpsz); 23244 releasestr(stp->sd_wrq); 23245 return (1); 23246 } 23247 23248 static int 23249 tcp_conprim_opt_process(tcp_t *tcp, mblk_t *mp, int *do_disconnectp, 23250 int *t_errorp, int *sys_errorp) 23251 { 23252 int error; 23253 int is_absreq_failure; 23254 t_scalar_t *opt_lenp; 23255 t_scalar_t opt_offset; 23256 int prim_type; 23257 struct T_conn_req *tcreqp; 23258 struct T_conn_res *tcresp; 23259 cred_t *cr; 23260 23261 cr = DB_CREDDEF(mp, tcp->tcp_cred); 23262 23263 prim_type = ((union T_primitives *)mp->b_rptr)->type; 23264 ASSERT(prim_type == T_CONN_REQ || prim_type == O_T_CONN_RES || 23265 prim_type == T_CONN_RES); 23266 23267 switch (prim_type) { 23268 case T_CONN_REQ: 23269 tcreqp = (struct T_conn_req *)mp->b_rptr; 23270 opt_offset = tcreqp->OPT_offset; 23271 opt_lenp = (t_scalar_t *)&tcreqp->OPT_length; 23272 break; 23273 case O_T_CONN_RES: 23274 case T_CONN_RES: 23275 tcresp = (struct T_conn_res *)mp->b_rptr; 23276 opt_offset = tcresp->OPT_offset; 23277 opt_lenp = (t_scalar_t *)&tcresp->OPT_length; 23278 break; 23279 } 23280 23281 *t_errorp = 0; 23282 *sys_errorp = 0; 23283 *do_disconnectp = 0; 23284 23285 error = tpi_optcom_buf(tcp->tcp_wq, mp, opt_lenp, 23286 opt_offset, cr, &tcp_opt_obj, 23287 NULL, &is_absreq_failure); 23288 23289 switch (error) { 23290 case 0: /* no error */ 23291 ASSERT(is_absreq_failure == 0); 23292 return (0); 23293 case ENOPROTOOPT: 23294 *t_errorp = TBADOPT; 23295 break; 23296 case EACCES: 23297 *t_errorp = TACCES; 23298 break; 23299 default: 23300 *t_errorp = TSYSERR; *sys_errorp = error; 23301 break; 23302 } 23303 if (is_absreq_failure != 0) { 23304 /* 23305 * The connection request should get the local ack 23306 * T_OK_ACK and then a T_DISCON_IND. 23307 */ 23308 *do_disconnectp = 1; 23309 } 23310 return (-1); 23311 } 23312 23313 /* 23314 * Split this function out so that if the secret changes, I'm okay. 23315 * 23316 * Initialize the tcp_iss_cookie and tcp_iss_key. 23317 */ 23318 23319 #define PASSWD_SIZE 16 /* MUST be multiple of 4 */ 23320 23321 static void 23322 tcp_iss_key_init(uint8_t *phrase, int len) 23323 { 23324 struct { 23325 int32_t current_time; 23326 uint32_t randnum; 23327 uint16_t pad; 23328 uint8_t ether[6]; 23329 uint8_t passwd[PASSWD_SIZE]; 23330 } tcp_iss_cookie; 23331 time_t t; 23332 23333 /* 23334 * Start with the current absolute time. 23335 */ 23336 (void) drv_getparm(TIME, &t); 23337 tcp_iss_cookie.current_time = t; 23338 23339 /* 23340 * XXX - Need a more random number per RFC 1750, not this crap. 23341 * OTOH, if what follows is pretty random, then I'm in better shape. 23342 */ 23343 tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random()); 23344 tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */ 23345 23346 /* 23347 * The cpu_type_info is pretty non-random. Ugggh. It does serve 23348 * as a good template. 23349 */ 23350 bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd, 23351 min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info))); 23352 23353 /* 23354 * The pass-phrase. Normally this is supplied by user-called NDD. 23355 */ 23356 bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len)); 23357 23358 /* 23359 * See 4010593 if this section becomes a problem again, 23360 * but the local ethernet address is useful here. 23361 */ 23362 (void) localetheraddr(NULL, 23363 (struct ether_addr *)&tcp_iss_cookie.ether); 23364 23365 /* 23366 * Hash 'em all together. The MD5Final is called per-connection. 23367 */ 23368 mutex_enter(&tcp_iss_key_lock); 23369 MD5Init(&tcp_iss_key); 23370 MD5Update(&tcp_iss_key, (uchar_t *)&tcp_iss_cookie, 23371 sizeof (tcp_iss_cookie)); 23372 mutex_exit(&tcp_iss_key_lock); 23373 } 23374 23375 /* 23376 * Set the RFC 1948 pass phrase 23377 */ 23378 /* ARGSUSED */ 23379 static int 23380 tcp_1948_phrase_set(queue_t *q, mblk_t *mp, char *value, caddr_t cp, 23381 cred_t *cr) 23382 { 23383 /* 23384 * Basically, value contains a new pass phrase. Pass it along! 23385 */ 23386 tcp_iss_key_init((uint8_t *)value, strlen(value)); 23387 return (0); 23388 } 23389 23390 /* ARGSUSED */ 23391 static int 23392 tcp_sack_info_constructor(void *buf, void *cdrarg, int kmflags) 23393 { 23394 bzero(buf, sizeof (tcp_sack_info_t)); 23395 return (0); 23396 } 23397 23398 /* ARGSUSED */ 23399 static int 23400 tcp_iphc_constructor(void *buf, void *cdrarg, int kmflags) 23401 { 23402 bzero(buf, TCP_MAX_COMBINED_HEADER_LENGTH); 23403 return (0); 23404 } 23405 23406 void 23407 tcp_ddi_init(void) 23408 { 23409 int i; 23410 23411 /* Initialize locks */ 23412 rw_init(&tcp_hsp_lock, NULL, RW_DEFAULT, NULL); 23413 mutex_init(&tcp_g_q_lock, NULL, MUTEX_DEFAULT, NULL); 23414 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL); 23415 mutex_init(&tcp_iss_key_lock, NULL, MUTEX_DEFAULT, NULL); 23416 mutex_init(&tcp_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL); 23417 rw_init(&tcp_reserved_port_lock, NULL, RW_DEFAULT, NULL); 23418 23419 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 23420 mutex_init(&tcp_bind_fanout[i].tf_lock, NULL, 23421 MUTEX_DEFAULT, NULL); 23422 } 23423 23424 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 23425 mutex_init(&tcp_acceptor_fanout[i].tf_lock, NULL, 23426 MUTEX_DEFAULT, NULL); 23427 } 23428 23429 /* TCP's IPsec code calls the packet dropper. */ 23430 ip_drop_register(&tcp_dropper, "TCP IPsec policy enforcement"); 23431 23432 if (!tcp_g_nd) { 23433 if (!tcp_param_register(tcp_param_arr, A_CNT(tcp_param_arr))) { 23434 nd_free(&tcp_g_nd); 23435 } 23436 } 23437 23438 /* 23439 * Note: To really walk the device tree you need the devinfo 23440 * pointer to your device which is only available after probe/attach. 23441 * The following is safe only because it uses ddi_root_node() 23442 */ 23443 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr, 23444 tcp_opt_obj.odb_opt_arr_cnt); 23445 23446 tcp_timercache = kmem_cache_create("tcp_timercache", 23447 sizeof (tcp_timer_t) + sizeof (mblk_t), 0, 23448 NULL, NULL, NULL, NULL, NULL, 0); 23449 23450 tcp_sack_info_cache = kmem_cache_create("tcp_sack_info_cache", 23451 sizeof (tcp_sack_info_t), 0, 23452 tcp_sack_info_constructor, NULL, NULL, NULL, NULL, 0); 23453 23454 tcp_iphc_cache = kmem_cache_create("tcp_iphc_cache", 23455 TCP_MAX_COMBINED_HEADER_LENGTH, 0, 23456 tcp_iphc_constructor, NULL, NULL, NULL, NULL, 0); 23457 23458 tcp_squeue_wput_proc = tcp_squeue_switch(tcp_squeue_wput); 23459 tcp_squeue_close_proc = tcp_squeue_switch(tcp_squeue_close); 23460 23461 ip_squeue_init(tcp_squeue_add); 23462 23463 /* Initialize the random number generator */ 23464 tcp_random_init(); 23465 23466 /* 23467 * Initialize RFC 1948 secret values. This will probably be reset once 23468 * by the boot scripts. 23469 * 23470 * Use NULL name, as the name is caught by the new lockstats. 23471 * 23472 * Initialize with some random, non-guessable string, like the global 23473 * T_INFO_ACK. 23474 */ 23475 23476 tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack, 23477 sizeof (tcp_g_t_info_ack)); 23478 23479 if ((tcp_kstat = kstat_create(TCP_MOD_NAME, 0, "tcpstat", 23480 "net", KSTAT_TYPE_NAMED, 23481 sizeof (tcp_statistics) / sizeof (kstat_named_t), 23482 KSTAT_FLAG_VIRTUAL)) != NULL) { 23483 tcp_kstat->ks_data = &tcp_statistics; 23484 kstat_install(tcp_kstat); 23485 } 23486 23487 tcp_kstat_init(); 23488 } 23489 23490 void 23491 tcp_ddi_destroy(void) 23492 { 23493 int i; 23494 23495 nd_free(&tcp_g_nd); 23496 23497 for (i = 0; i < A_CNT(tcp_bind_fanout); i++) { 23498 mutex_destroy(&tcp_bind_fanout[i].tf_lock); 23499 } 23500 23501 for (i = 0; i < A_CNT(tcp_acceptor_fanout); i++) { 23502 mutex_destroy(&tcp_acceptor_fanout[i].tf_lock); 23503 } 23504 23505 mutex_destroy(&tcp_iss_key_lock); 23506 rw_destroy(&tcp_hsp_lock); 23507 mutex_destroy(&tcp_g_q_lock); 23508 mutex_destroy(&tcp_random_lock); 23509 mutex_destroy(&tcp_epriv_port_lock); 23510 rw_destroy(&tcp_reserved_port_lock); 23511 23512 ip_drop_unregister(&tcp_dropper); 23513 23514 kmem_cache_destroy(tcp_timercache); 23515 kmem_cache_destroy(tcp_sack_info_cache); 23516 kmem_cache_destroy(tcp_iphc_cache); 23517 23518 tcp_kstat_fini(); 23519 } 23520 23521 /* 23522 * Generate ISS, taking into account NDD changes may happen halfway through. 23523 * (If the iss is not zero, set it.) 23524 */ 23525 23526 static void 23527 tcp_iss_init(tcp_t *tcp) 23528 { 23529 MD5_CTX context; 23530 struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg; 23531 uint32_t answer[4]; 23532 23533 tcp_iss_incr_extra += (ISS_INCR >> 1); 23534 tcp->tcp_iss = tcp_iss_incr_extra; 23535 switch (tcp_strong_iss) { 23536 case 2: 23537 mutex_enter(&tcp_iss_key_lock); 23538 context = tcp_iss_key; 23539 mutex_exit(&tcp_iss_key_lock); 23540 arg.ports = tcp->tcp_ports; 23541 if (tcp->tcp_ipversion == IPV4_VERSION) { 23542 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_src, 23543 &arg.src); 23544 IN6_IPADDR_TO_V4MAPPED(tcp->tcp_ipha->ipha_dst, 23545 &arg.dst); 23546 } else { 23547 arg.src = tcp->tcp_ip6h->ip6_src; 23548 arg.dst = tcp->tcp_ip6h->ip6_dst; 23549 } 23550 MD5Update(&context, (uchar_t *)&arg, sizeof (arg)); 23551 MD5Final((uchar_t *)answer, &context); 23552 tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3]; 23553 /* 23554 * Now that we've hashed into a unique per-connection sequence 23555 * space, add a random increment per strong_iss == 1. So I 23556 * guess we'll have to... 23557 */ 23558 /* FALLTHRU */ 23559 case 1: 23560 tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random(); 23561 break; 23562 default: 23563 tcp->tcp_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 23564 break; 23565 } 23566 tcp->tcp_valid_bits = TCP_ISS_VALID; 23567 tcp->tcp_fss = tcp->tcp_iss - 1; 23568 tcp->tcp_suna = tcp->tcp_iss; 23569 tcp->tcp_snxt = tcp->tcp_iss + 1; 23570 tcp->tcp_rexmit_nxt = tcp->tcp_snxt; 23571 tcp->tcp_csuna = tcp->tcp_snxt; 23572 } 23573 23574 /* 23575 * Exported routine for extracting active tcp connection status. 23576 * 23577 * This is used by the Solaris Cluster Networking software to 23578 * gather a list of connections that need to be forwarded to 23579 * specific nodes in the cluster when configuration changes occur. 23580 * 23581 * The callback is invoked for each tcp_t structure. Returning 23582 * non-zero from the callback routine terminates the search. 23583 */ 23584 int 23585 cl_tcp_walk_list(int (*callback)(cl_tcp_info_t *, void *), void *arg) 23586 { 23587 tcp_t *tcp; 23588 cl_tcp_info_t cl_tcpi; 23589 connf_t *connfp; 23590 conn_t *connp; 23591 int i; 23592 23593 ASSERT(callback != NULL); 23594 23595 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 23596 23597 connfp = &ipcl_globalhash_fanout[i]; 23598 connp = NULL; 23599 23600 while ((connp = 23601 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 23602 23603 tcp = connp->conn_tcp; 23604 cl_tcpi.cl_tcpi_version = CL_TCPI_V1; 23605 cl_tcpi.cl_tcpi_ipversion = tcp->tcp_ipversion; 23606 cl_tcpi.cl_tcpi_state = tcp->tcp_state; 23607 cl_tcpi.cl_tcpi_lport = tcp->tcp_lport; 23608 cl_tcpi.cl_tcpi_fport = tcp->tcp_fport; 23609 /* 23610 * The macros tcp_laddr and tcp_faddr give the IPv4 23611 * addresses. They are copied implicitly below as 23612 * mapped addresses. 23613 */ 23614 cl_tcpi.cl_tcpi_laddr_v6 = tcp->tcp_ip_src_v6; 23615 if (tcp->tcp_ipversion == IPV4_VERSION) { 23616 cl_tcpi.cl_tcpi_faddr = 23617 tcp->tcp_ipha->ipha_dst; 23618 } else { 23619 cl_tcpi.cl_tcpi_faddr_v6 = 23620 tcp->tcp_ip6h->ip6_dst; 23621 } 23622 23623 /* 23624 * If the callback returns non-zero 23625 * we terminate the traversal. 23626 */ 23627 if ((*callback)(&cl_tcpi, arg) != 0) { 23628 CONN_DEC_REF(tcp->tcp_connp); 23629 return (1); 23630 } 23631 } 23632 } 23633 23634 return (0); 23635 } 23636 23637 /* 23638 * Macros used for accessing the different types of sockaddr 23639 * structures inside a tcp_ioc_abort_conn_t. 23640 */ 23641 #define TCP_AC_V4LADDR(acp) ((sin_t *)&(acp)->ac_local) 23642 #define TCP_AC_V4RADDR(acp) ((sin_t *)&(acp)->ac_remote) 23643 #define TCP_AC_V4LOCAL(acp) (TCP_AC_V4LADDR(acp)->sin_addr.s_addr) 23644 #define TCP_AC_V4REMOTE(acp) (TCP_AC_V4RADDR(acp)->sin_addr.s_addr) 23645 #define TCP_AC_V4LPORT(acp) (TCP_AC_V4LADDR(acp)->sin_port) 23646 #define TCP_AC_V4RPORT(acp) (TCP_AC_V4RADDR(acp)->sin_port) 23647 #define TCP_AC_V6LADDR(acp) ((sin6_t *)&(acp)->ac_local) 23648 #define TCP_AC_V6RADDR(acp) ((sin6_t *)&(acp)->ac_remote) 23649 #define TCP_AC_V6LOCAL(acp) (TCP_AC_V6LADDR(acp)->sin6_addr) 23650 #define TCP_AC_V6REMOTE(acp) (TCP_AC_V6RADDR(acp)->sin6_addr) 23651 #define TCP_AC_V6LPORT(acp) (TCP_AC_V6LADDR(acp)->sin6_port) 23652 #define TCP_AC_V6RPORT(acp) (TCP_AC_V6RADDR(acp)->sin6_port) 23653 23654 /* 23655 * Return the correct error code to mimic the behavior 23656 * of a connection reset. 23657 */ 23658 #define TCP_AC_GET_ERRCODE(state, err) { \ 23659 switch ((state)) { \ 23660 case TCPS_SYN_SENT: \ 23661 case TCPS_SYN_RCVD: \ 23662 (err) = ECONNREFUSED; \ 23663 break; \ 23664 case TCPS_ESTABLISHED: \ 23665 case TCPS_FIN_WAIT_1: \ 23666 case TCPS_FIN_WAIT_2: \ 23667 case TCPS_CLOSE_WAIT: \ 23668 (err) = ECONNRESET; \ 23669 break; \ 23670 case TCPS_CLOSING: \ 23671 case TCPS_LAST_ACK: \ 23672 case TCPS_TIME_WAIT: \ 23673 (err) = 0; \ 23674 break; \ 23675 default: \ 23676 (err) = ENXIO; \ 23677 } \ 23678 } 23679 23680 /* 23681 * Check if a tcp structure matches the info in acp. 23682 */ 23683 #define TCP_AC_ADDR_MATCH(acp, tcp) \ 23684 (((acp)->ac_local.ss_family == AF_INET) ? \ 23685 ((TCP_AC_V4LOCAL((acp)) == INADDR_ANY || \ 23686 TCP_AC_V4LOCAL((acp)) == (tcp)->tcp_ip_src) && \ 23687 (TCP_AC_V4REMOTE((acp)) == INADDR_ANY || \ 23688 TCP_AC_V4REMOTE((acp)) == (tcp)->tcp_remote) && \ 23689 (TCP_AC_V4LPORT((acp)) == 0 || \ 23690 TCP_AC_V4LPORT((acp)) == (tcp)->tcp_lport) && \ 23691 (TCP_AC_V4RPORT((acp)) == 0 || \ 23692 TCP_AC_V4RPORT((acp)) == (tcp)->tcp_fport) && \ 23693 (acp)->ac_start <= (tcp)->tcp_state && \ 23694 (acp)->ac_end >= (tcp)->tcp_state) : \ 23695 ((IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL((acp))) || \ 23696 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6LOCAL((acp)), \ 23697 &(tcp)->tcp_ip_src_v6)) && \ 23698 (IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE((acp))) || \ 23699 IN6_ARE_ADDR_EQUAL(&TCP_AC_V6REMOTE((acp)), \ 23700 &(tcp)->tcp_remote_v6)) && \ 23701 (TCP_AC_V6LPORT((acp)) == 0 || \ 23702 TCP_AC_V6LPORT((acp)) == (tcp)->tcp_lport) && \ 23703 (TCP_AC_V6RPORT((acp)) == 0 || \ 23704 TCP_AC_V6RPORT((acp)) == (tcp)->tcp_fport) && \ 23705 (acp)->ac_start <= (tcp)->tcp_state && \ 23706 (acp)->ac_end >= (tcp)->tcp_state)) 23707 23708 #define TCP_AC_MATCH(acp, tcp) \ 23709 (((acp)->ac_zoneid == ALL_ZONES || \ 23710 (acp)->ac_zoneid == tcp->tcp_connp->conn_zoneid) ? \ 23711 TCP_AC_ADDR_MATCH(acp, tcp) : 0) 23712 23713 /* 23714 * Build a message containing a tcp_ioc_abort_conn_t structure 23715 * which is filled in with information from acp and tp. 23716 */ 23717 static mblk_t * 23718 tcp_ioctl_abort_build_msg(tcp_ioc_abort_conn_t *acp, tcp_t *tp) 23719 { 23720 mblk_t *mp; 23721 tcp_ioc_abort_conn_t *tacp; 23722 23723 mp = allocb(sizeof (uint32_t) + sizeof (*acp), BPRI_LO); 23724 if (mp == NULL) 23725 return (NULL); 23726 23727 mp->b_datap->db_type = M_CTL; 23728 23729 *((uint32_t *)mp->b_rptr) = TCP_IOC_ABORT_CONN; 23730 tacp = (tcp_ioc_abort_conn_t *)((uchar_t *)mp->b_rptr + 23731 sizeof (uint32_t)); 23732 23733 tacp->ac_start = acp->ac_start; 23734 tacp->ac_end = acp->ac_end; 23735 tacp->ac_zoneid = acp->ac_zoneid; 23736 23737 if (acp->ac_local.ss_family == AF_INET) { 23738 tacp->ac_local.ss_family = AF_INET; 23739 tacp->ac_remote.ss_family = AF_INET; 23740 TCP_AC_V4LOCAL(tacp) = tp->tcp_ip_src; 23741 TCP_AC_V4REMOTE(tacp) = tp->tcp_remote; 23742 TCP_AC_V4LPORT(tacp) = tp->tcp_lport; 23743 TCP_AC_V4RPORT(tacp) = tp->tcp_fport; 23744 } else { 23745 tacp->ac_local.ss_family = AF_INET6; 23746 tacp->ac_remote.ss_family = AF_INET6; 23747 TCP_AC_V6LOCAL(tacp) = tp->tcp_ip_src_v6; 23748 TCP_AC_V6REMOTE(tacp) = tp->tcp_remote_v6; 23749 TCP_AC_V6LPORT(tacp) = tp->tcp_lport; 23750 TCP_AC_V6RPORT(tacp) = tp->tcp_fport; 23751 } 23752 mp->b_wptr = (uchar_t *)mp->b_rptr + sizeof (uint32_t) + sizeof (*acp); 23753 return (mp); 23754 } 23755 23756 /* 23757 * Print a tcp_ioc_abort_conn_t structure. 23758 */ 23759 static void 23760 tcp_ioctl_abort_dump(tcp_ioc_abort_conn_t *acp) 23761 { 23762 char lbuf[128]; 23763 char rbuf[128]; 23764 sa_family_t af; 23765 in_port_t lport, rport; 23766 ushort_t logflags; 23767 23768 af = acp->ac_local.ss_family; 23769 23770 if (af == AF_INET) { 23771 (void) inet_ntop(af, (const void *)&TCP_AC_V4LOCAL(acp), 23772 lbuf, 128); 23773 (void) inet_ntop(af, (const void *)&TCP_AC_V4REMOTE(acp), 23774 rbuf, 128); 23775 lport = ntohs(TCP_AC_V4LPORT(acp)); 23776 rport = ntohs(TCP_AC_V4RPORT(acp)); 23777 } else { 23778 (void) inet_ntop(af, (const void *)&TCP_AC_V6LOCAL(acp), 23779 lbuf, 128); 23780 (void) inet_ntop(af, (const void *)&TCP_AC_V6REMOTE(acp), 23781 rbuf, 128); 23782 lport = ntohs(TCP_AC_V6LPORT(acp)); 23783 rport = ntohs(TCP_AC_V6RPORT(acp)); 23784 } 23785 23786 logflags = SL_TRACE | SL_NOTE; 23787 /* 23788 * Don't print this message to the console if the operation was done 23789 * to a non-global zone. 23790 */ 23791 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 23792 logflags |= SL_CONSOLE; 23793 (void) strlog(TCP_MOD_ID, 0, 1, logflags, 23794 "TCP_IOC_ABORT_CONN: local = %s:%d, remote = %s:%d, " 23795 "start = %d, end = %d\n", lbuf, lport, rbuf, rport, 23796 acp->ac_start, acp->ac_end); 23797 } 23798 23799 /* 23800 * Called inside tcp_rput when a message built using 23801 * tcp_ioctl_abort_build_msg is put into a queue. 23802 * Note that when we get here there is no wildcard in acp any more. 23803 */ 23804 static void 23805 tcp_ioctl_abort_handler(tcp_t *tcp, mblk_t *mp) 23806 { 23807 tcp_ioc_abort_conn_t *acp; 23808 23809 acp = (tcp_ioc_abort_conn_t *)(mp->b_rptr + sizeof (uint32_t)); 23810 if (tcp->tcp_state <= acp->ac_end) { 23811 /* 23812 * If we get here, we are already on the correct 23813 * squeue. This ioctl follows the following path 23814 * tcp_wput -> tcp_wput_ioctl -> tcp_ioctl_abort_conn 23815 * ->tcp_ioctl_abort->squeue_fill (if on a 23816 * different squeue) 23817 */ 23818 int errcode; 23819 23820 TCP_AC_GET_ERRCODE(tcp->tcp_state, errcode); 23821 (void) tcp_clean_death(tcp, errcode, 26); 23822 } 23823 freemsg(mp); 23824 } 23825 23826 /* 23827 * Abort all matching connections on a hash chain. 23828 */ 23829 static int 23830 tcp_ioctl_abort_bucket(tcp_ioc_abort_conn_t *acp, int index, int *count, 23831 boolean_t exact) 23832 { 23833 int nmatch, err = 0; 23834 tcp_t *tcp; 23835 MBLKP mp, last, listhead = NULL; 23836 conn_t *tconnp; 23837 connf_t *connfp = &ipcl_conn_fanout[index]; 23838 23839 startover: 23840 nmatch = 0; 23841 23842 mutex_enter(&connfp->connf_lock); 23843 for (tconnp = connfp->connf_head; tconnp != NULL; 23844 tconnp = tconnp->conn_next) { 23845 tcp = tconnp->conn_tcp; 23846 if (TCP_AC_MATCH(acp, tcp)) { 23847 CONN_INC_REF(tcp->tcp_connp); 23848 mp = tcp_ioctl_abort_build_msg(acp, tcp); 23849 if (mp == NULL) { 23850 err = ENOMEM; 23851 CONN_DEC_REF(tcp->tcp_connp); 23852 break; 23853 } 23854 mp->b_prev = (mblk_t *)tcp; 23855 23856 if (listhead == NULL) { 23857 listhead = mp; 23858 last = mp; 23859 } else { 23860 last->b_next = mp; 23861 last = mp; 23862 } 23863 nmatch++; 23864 if (exact) 23865 break; 23866 } 23867 23868 /* Avoid holding lock for too long. */ 23869 if (nmatch >= 500) 23870 break; 23871 } 23872 mutex_exit(&connfp->connf_lock); 23873 23874 /* Pass mp into the correct tcp */ 23875 while ((mp = listhead) != NULL) { 23876 listhead = listhead->b_next; 23877 tcp = (tcp_t *)mp->b_prev; 23878 mp->b_next = mp->b_prev = NULL; 23879 squeue_fill(tcp->tcp_connp->conn_sqp, mp, 23880 tcp_input, tcp->tcp_connp, SQTAG_TCP_ABORT_BUCKET); 23881 } 23882 23883 *count += nmatch; 23884 if (nmatch >= 500 && err == 0) 23885 goto startover; 23886 return (err); 23887 } 23888 23889 /* 23890 * Abort all connections that matches the attributes specified in acp. 23891 */ 23892 static int 23893 tcp_ioctl_abort(tcp_ioc_abort_conn_t *acp) 23894 { 23895 sa_family_t af; 23896 uint32_t ports; 23897 uint16_t *pports; 23898 int err = 0, count = 0; 23899 boolean_t exact = B_FALSE; /* set when there is no wildcard */ 23900 int index = -1; 23901 ushort_t logflags; 23902 23903 af = acp->ac_local.ss_family; 23904 23905 if (af == AF_INET) { 23906 if (TCP_AC_V4REMOTE(acp) != INADDR_ANY && 23907 TCP_AC_V4LPORT(acp) != 0 && TCP_AC_V4RPORT(acp) != 0) { 23908 pports = (uint16_t *)&ports; 23909 pports[1] = TCP_AC_V4LPORT(acp); 23910 pports[0] = TCP_AC_V4RPORT(acp); 23911 exact = (TCP_AC_V4LOCAL(acp) != INADDR_ANY); 23912 } 23913 } else { 23914 if (!IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6REMOTE(acp)) && 23915 TCP_AC_V6LPORT(acp) != 0 && TCP_AC_V6RPORT(acp) != 0) { 23916 pports = (uint16_t *)&ports; 23917 pports[1] = TCP_AC_V6LPORT(acp); 23918 pports[0] = TCP_AC_V6RPORT(acp); 23919 exact = !IN6_IS_ADDR_UNSPECIFIED(&TCP_AC_V6LOCAL(acp)); 23920 } 23921 } 23922 23923 /* 23924 * For cases where remote addr, local port, and remote port are non- 23925 * wildcards, tcp_ioctl_abort_bucket will only be called once. 23926 */ 23927 if (index != -1) { 23928 err = tcp_ioctl_abort_bucket(acp, index, 23929 &count, exact); 23930 } else { 23931 /* 23932 * loop through all entries for wildcard case 23933 */ 23934 for (index = 0; index < ipcl_conn_fanout_size; index++) { 23935 err = tcp_ioctl_abort_bucket(acp, index, 23936 &count, exact); 23937 if (err != 0) 23938 break; 23939 } 23940 } 23941 23942 logflags = SL_TRACE | SL_NOTE; 23943 /* 23944 * Don't print this message to the console if the operation was done 23945 * to a non-global zone. 23946 */ 23947 if (acp->ac_zoneid == GLOBAL_ZONEID || acp->ac_zoneid == ALL_ZONES) 23948 logflags |= SL_CONSOLE; 23949 (void) strlog(TCP_MOD_ID, 0, 1, logflags, "TCP_IOC_ABORT_CONN: " 23950 "aborted %d connection%c\n", count, ((count > 1) ? 's' : ' ')); 23951 if (err == 0 && count == 0) 23952 err = ENOENT; 23953 return (err); 23954 } 23955 23956 /* 23957 * Process the TCP_IOC_ABORT_CONN ioctl request. 23958 */ 23959 static void 23960 tcp_ioctl_abort_conn(queue_t *q, mblk_t *mp) 23961 { 23962 int err; 23963 IOCP iocp; 23964 MBLKP mp1; 23965 sa_family_t laf, raf; 23966 tcp_ioc_abort_conn_t *acp; 23967 zone_t *zptr; 23968 zoneid_t zoneid = Q_TO_CONN(q)->conn_zoneid; 23969 23970 iocp = (IOCP)mp->b_rptr; 23971 23972 if ((mp1 = mp->b_cont) == NULL || 23973 iocp->ioc_count != sizeof (tcp_ioc_abort_conn_t)) { 23974 err = EINVAL; 23975 goto out; 23976 } 23977 23978 /* check permissions */ 23979 if (secpolicy_net_config(iocp->ioc_cr, B_FALSE) != 0) { 23980 err = EPERM; 23981 goto out; 23982 } 23983 23984 if (mp1->b_cont != NULL) { 23985 freemsg(mp1->b_cont); 23986 mp1->b_cont = NULL; 23987 } 23988 23989 acp = (tcp_ioc_abort_conn_t *)mp1->b_rptr; 23990 laf = acp->ac_local.ss_family; 23991 raf = acp->ac_remote.ss_family; 23992 23993 /* check that a zone with the supplied zoneid exists */ 23994 if (acp->ac_zoneid != GLOBAL_ZONEID && acp->ac_zoneid != ALL_ZONES) { 23995 zptr = zone_find_by_id(zoneid); 23996 if (zptr != NULL) { 23997 zone_rele(zptr); 23998 } else { 23999 err = EINVAL; 24000 goto out; 24001 } 24002 } 24003 24004 if (acp->ac_start < TCPS_SYN_SENT || acp->ac_end > TCPS_TIME_WAIT || 24005 acp->ac_start > acp->ac_end || laf != raf || 24006 (laf != AF_INET && laf != AF_INET6)) { 24007 err = EINVAL; 24008 goto out; 24009 } 24010 24011 tcp_ioctl_abort_dump(acp); 24012 err = tcp_ioctl_abort(acp); 24013 24014 out: 24015 if (mp1 != NULL) { 24016 freemsg(mp1); 24017 mp->b_cont = NULL; 24018 } 24019 24020 if (err != 0) 24021 miocnak(q, mp, 0, err); 24022 else 24023 miocack(q, mp, 0, 0); 24024 } 24025 24026 /* 24027 * tcp_time_wait_processing() handles processing of incoming packets when 24028 * the tcp is in the TIME_WAIT state. 24029 * A TIME_WAIT tcp that has an associated open TCP stream is never put 24030 * on the time wait list. 24031 */ 24032 void 24033 tcp_time_wait_processing(tcp_t *tcp, mblk_t *mp, uint32_t seg_seq, 24034 uint32_t seg_ack, int seg_len, tcph_t *tcph) 24035 { 24036 int32_t bytes_acked; 24037 int32_t gap; 24038 int32_t rgap; 24039 tcp_opt_t tcpopt; 24040 uint_t flags; 24041 uint32_t new_swnd = 0; 24042 conn_t *connp; 24043 24044 BUMP_LOCAL(tcp->tcp_ibsegs); 24045 TCP_RECORD_TRACE(tcp, mp, TCP_TRACE_RECV_PKT); 24046 24047 flags = (unsigned int)tcph->th_flags[0] & 0xFF; 24048 new_swnd = BE16_TO_U16(tcph->th_win) << 24049 ((tcph->th_flags[0] & TH_SYN) ? 0 : tcp->tcp_snd_ws); 24050 if (tcp->tcp_snd_ts_ok) { 24051 if (!tcp_paws_check(tcp, tcph, &tcpopt)) { 24052 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24053 tcp->tcp_rnxt, TH_ACK); 24054 goto done; 24055 } 24056 } 24057 gap = seg_seq - tcp->tcp_rnxt; 24058 rgap = tcp->tcp_rwnd - (gap + seg_len); 24059 if (gap < 0) { 24060 BUMP_MIB(&tcp_mib, tcpInDataDupSegs); 24061 UPDATE_MIB(&tcp_mib, tcpInDataDupBytes, 24062 (seg_len > -gap ? -gap : seg_len)); 24063 seg_len += gap; 24064 if (seg_len < 0 || (seg_len == 0 && !(flags & TH_FIN))) { 24065 if (flags & TH_RST) { 24066 goto done; 24067 } 24068 if ((flags & TH_FIN) && seg_len == -1) { 24069 /* 24070 * When TCP receives a duplicate FIN in 24071 * TIME_WAIT state, restart the 2 MSL timer. 24072 * See page 73 in RFC 793. Make sure this TCP 24073 * is already on the TIME_WAIT list. If not, 24074 * just restart the timer. 24075 */ 24076 if (TCP_IS_DETACHED(tcp)) { 24077 tcp_time_wait_remove(tcp, NULL); 24078 tcp_time_wait_append(tcp); 24079 TCP_DBGSTAT(tcp_rput_time_wait); 24080 } else { 24081 ASSERT(tcp != NULL); 24082 TCP_TIMER_RESTART(tcp, 24083 tcp_time_wait_interval); 24084 } 24085 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24086 tcp->tcp_rnxt, TH_ACK); 24087 goto done; 24088 } 24089 flags |= TH_ACK_NEEDED; 24090 seg_len = 0; 24091 goto process_ack; 24092 } 24093 24094 /* Fix seg_seq, and chew the gap off the front. */ 24095 seg_seq = tcp->tcp_rnxt; 24096 } 24097 24098 if ((flags & TH_SYN) && gap > 0 && rgap < 0) { 24099 /* 24100 * Make sure that when we accept the connection, pick 24101 * an ISS greater than (tcp_snxt + ISS_INCR/2) for the 24102 * old connection. 24103 * 24104 * The next ISS generated is equal to tcp_iss_incr_extra 24105 * + ISS_INCR/2 + other components depending on the 24106 * value of tcp_strong_iss. We pre-calculate the new 24107 * ISS here and compare with tcp_snxt to determine if 24108 * we need to make adjustment to tcp_iss_incr_extra. 24109 * 24110 * The above calculation is ugly and is a 24111 * waste of CPU cycles... 24112 */ 24113 uint32_t new_iss = tcp_iss_incr_extra; 24114 int32_t adj; 24115 24116 switch (tcp_strong_iss) { 24117 case 2: { 24118 /* Add time and MD5 components. */ 24119 uint32_t answer[4]; 24120 struct { 24121 uint32_t ports; 24122 in6_addr_t src; 24123 in6_addr_t dst; 24124 } arg; 24125 MD5_CTX context; 24126 24127 mutex_enter(&tcp_iss_key_lock); 24128 context = tcp_iss_key; 24129 mutex_exit(&tcp_iss_key_lock); 24130 arg.ports = tcp->tcp_ports; 24131 /* We use MAPPED addresses in tcp_iss_init */ 24132 arg.src = tcp->tcp_ip_src_v6; 24133 if (tcp->tcp_ipversion == IPV4_VERSION) { 24134 IN6_IPADDR_TO_V4MAPPED( 24135 tcp->tcp_ipha->ipha_dst, 24136 &arg.dst); 24137 } else { 24138 arg.dst = 24139 tcp->tcp_ip6h->ip6_dst; 24140 } 24141 MD5Update(&context, (uchar_t *)&arg, 24142 sizeof (arg)); 24143 MD5Final((uchar_t *)answer, &context); 24144 answer[0] ^= answer[1] ^ answer[2] ^ answer[3]; 24145 new_iss += (gethrtime() >> ISS_NSEC_SHT) + answer[0]; 24146 break; 24147 } 24148 case 1: 24149 /* Add time component and min random (i.e. 1). */ 24150 new_iss += (gethrtime() >> ISS_NSEC_SHT) + 1; 24151 break; 24152 default: 24153 /* Add only time component. */ 24154 new_iss += (uint32_t)gethrestime_sec() * ISS_INCR; 24155 break; 24156 } 24157 if ((adj = (int32_t)(tcp->tcp_snxt - new_iss)) > 0) { 24158 /* 24159 * New ISS not guaranteed to be ISS_INCR/2 24160 * ahead of the current tcp_snxt, so add the 24161 * difference to tcp_iss_incr_extra. 24162 */ 24163 tcp_iss_incr_extra += adj; 24164 } 24165 /* 24166 * If tcp_clean_death() can not perform the task now, 24167 * drop the SYN packet and let the other side re-xmit. 24168 * Otherwise pass the SYN packet back in, since the 24169 * old tcp state has been cleaned up or freed. 24170 */ 24171 if (tcp_clean_death(tcp, 0, 27) == -1) 24172 goto done; 24173 /* 24174 * We will come back to tcp_rput_data 24175 * on the global queue. Packets destined 24176 * for the global queue will be checked 24177 * with global policy. But the policy for 24178 * this packet has already been checked as 24179 * this was destined for the detached 24180 * connection. We need to bypass policy 24181 * check this time by attaching a dummy 24182 * ipsec_in with ipsec_in_dont_check set. 24183 */ 24184 if ((connp = ipcl_classify(mp, tcp->tcp_connp->conn_zoneid)) != 24185 NULL) { 24186 TCP_STAT(tcp_time_wait_syn_success); 24187 tcp_reinput(connp, mp, tcp->tcp_connp->conn_sqp); 24188 return; 24189 } 24190 goto done; 24191 } 24192 24193 /* 24194 * rgap is the amount of stuff received out of window. A negative 24195 * value is the amount out of window. 24196 */ 24197 if (rgap < 0) { 24198 BUMP_MIB(&tcp_mib, tcpInDataPastWinSegs); 24199 UPDATE_MIB(&tcp_mib, tcpInDataPastWinBytes, -rgap); 24200 /* Fix seg_len and make sure there is something left. */ 24201 seg_len += rgap; 24202 if (seg_len <= 0) { 24203 if (flags & TH_RST) { 24204 goto done; 24205 } 24206 flags |= TH_ACK_NEEDED; 24207 seg_len = 0; 24208 goto process_ack; 24209 } 24210 } 24211 /* 24212 * Check whether we can update tcp_ts_recent. This test is 24213 * NOT the one in RFC 1323 3.4. It is from Braden, 1993, "TCP 24214 * Extensions for High Performance: An Update", Internet Draft. 24215 */ 24216 if (tcp->tcp_snd_ts_ok && 24217 TSTMP_GEQ(tcpopt.tcp_opt_ts_val, tcp->tcp_ts_recent) && 24218 SEQ_LEQ(seg_seq, tcp->tcp_rack)) { 24219 tcp->tcp_ts_recent = tcpopt.tcp_opt_ts_val; 24220 tcp->tcp_last_rcv_lbolt = lbolt64; 24221 } 24222 24223 if (seg_seq != tcp->tcp_rnxt && seg_len > 0) { 24224 /* Always ack out of order packets */ 24225 flags |= TH_ACK_NEEDED; 24226 seg_len = 0; 24227 } else if (seg_len > 0) { 24228 BUMP_MIB(&tcp_mib, tcpInClosed); 24229 BUMP_MIB(&tcp_mib, tcpInDataInorderSegs); 24230 UPDATE_MIB(&tcp_mib, tcpInDataInorderBytes, seg_len); 24231 } 24232 if (flags & TH_RST) { 24233 (void) tcp_clean_death(tcp, 0, 28); 24234 goto done; 24235 } 24236 if (flags & TH_SYN) { 24237 tcp_xmit_ctl("TH_SYN", tcp, seg_ack, seg_seq + 1, 24238 TH_RST|TH_ACK); 24239 /* 24240 * Do not delete the TCP structure if it is in 24241 * TIME_WAIT state. Refer to RFC 1122, 4.2.2.13. 24242 */ 24243 goto done; 24244 } 24245 process_ack: 24246 if (flags & TH_ACK) { 24247 bytes_acked = (int)(seg_ack - tcp->tcp_suna); 24248 if (bytes_acked <= 0) { 24249 if (bytes_acked == 0 && seg_len == 0 && 24250 new_swnd == tcp->tcp_swnd) 24251 BUMP_MIB(&tcp_mib, tcpInDupAck); 24252 } else { 24253 /* Acks something not sent */ 24254 flags |= TH_ACK_NEEDED; 24255 } 24256 } 24257 if (flags & TH_ACK_NEEDED) { 24258 /* 24259 * Time to send an ack for some reason. 24260 */ 24261 tcp_xmit_ctl(NULL, tcp, tcp->tcp_snxt, 24262 tcp->tcp_rnxt, TH_ACK); 24263 } 24264 done: 24265 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24266 DB_CKSUMSTART(mp) = 0; 24267 mp->b_datap->db_struioflag &= ~STRUIO_EAGER; 24268 TCP_STAT(tcp_time_wait_syn_fail); 24269 } 24270 freemsg(mp); 24271 } 24272 24273 /* 24274 * Return zero if the buffers are identical in length and content. 24275 * This is used for comparing extension header buffers. 24276 * Note that an extension header would be declared different 24277 * even if all that changed was the next header value in that header i.e. 24278 * what really changed is the next extension header. 24279 */ 24280 static boolean_t 24281 tcp_cmpbuf(void *a, uint_t alen, boolean_t b_valid, void *b, uint_t blen) 24282 { 24283 if (!b_valid) 24284 blen = 0; 24285 24286 if (alen != blen) 24287 return (B_TRUE); 24288 if (alen == 0) 24289 return (B_FALSE); /* Both zero length */ 24290 return (bcmp(a, b, alen)); 24291 } 24292 24293 /* 24294 * Preallocate memory for tcp_savebuf(). Returns B_TRUE if ok. 24295 * Return B_FALSE if memory allocation fails - don't change any state! 24296 */ 24297 static boolean_t 24298 tcp_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 24299 void *src, uint_t srclen) 24300 { 24301 void *dst; 24302 24303 if (!src_valid) 24304 srclen = 0; 24305 24306 ASSERT(*dstlenp == 0); 24307 if (src != NULL && srclen != 0) { 24308 dst = mi_alloc(srclen, BPRI_MED); 24309 if (dst == NULL) 24310 return (B_FALSE); 24311 } else { 24312 dst = NULL; 24313 } 24314 if (*dstp != NULL) { 24315 mi_free(*dstp); 24316 *dstp = NULL; 24317 *dstlenp = 0; 24318 } 24319 *dstp = dst; 24320 if (dst != NULL) 24321 *dstlenp = srclen; 24322 else 24323 *dstlenp = 0; 24324 return (B_TRUE); 24325 } 24326 24327 /* 24328 * Replace what is in *dst, *dstlen with the source. 24329 * Assumes tcp_allocbuf has already been called. 24330 */ 24331 static void 24332 tcp_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid, 24333 void *src, uint_t srclen) 24334 { 24335 if (!src_valid) 24336 srclen = 0; 24337 24338 ASSERT(*dstlenp == srclen); 24339 if (src != NULL && srclen != 0) { 24340 bcopy(src, *dstp, srclen); 24341 } 24342 } 24343 24344 /* 24345 * Allocate a T_SVR4_OPTMGMT_REQ. 24346 * The caller needs to increment tcp_drop_opt_ack_cnt when sending these so 24347 * that tcp_rput_other can drop the acks. 24348 */ 24349 static mblk_t * 24350 tcp_setsockopt_mp(int level, int cmd, char *opt, int optlen) 24351 { 24352 mblk_t *mp; 24353 struct T_optmgmt_req *tor; 24354 struct opthdr *oh; 24355 uint_t size; 24356 char *optptr; 24357 24358 size = sizeof (*tor) + sizeof (*oh) + optlen; 24359 mp = allocb(size, BPRI_MED); 24360 if (mp == NULL) 24361 return (NULL); 24362 24363 mp->b_wptr += size; 24364 mp->b_datap->db_type = M_PROTO; 24365 tor = (struct T_optmgmt_req *)mp->b_rptr; 24366 tor->PRIM_type = T_SVR4_OPTMGMT_REQ; 24367 tor->MGMT_flags = T_NEGOTIATE; 24368 tor->OPT_length = sizeof (*oh) + optlen; 24369 tor->OPT_offset = (t_scalar_t)sizeof (*tor); 24370 24371 oh = (struct opthdr *)&tor[1]; 24372 oh->level = level; 24373 oh->name = cmd; 24374 oh->len = optlen; 24375 if (optlen != 0) { 24376 optptr = (char *)&oh[1]; 24377 bcopy(opt, optptr, optlen); 24378 } 24379 return (mp); 24380 } 24381 24382 /* 24383 * TCP Timers Implementation. 24384 */ 24385 timeout_id_t 24386 tcp_timeout(conn_t *connp, void (*f)(void *), clock_t tim) 24387 { 24388 mblk_t *mp; 24389 tcp_timer_t *tcpt; 24390 tcp_t *tcp = connp->conn_tcp; 24391 24392 ASSERT(connp->conn_sqp != NULL); 24393 24394 TCP_DBGSTAT(tcp_timeout_calls); 24395 24396 if (tcp->tcp_timercache == NULL) { 24397 mp = tcp_timermp_alloc(KM_NOSLEEP | KM_PANIC); 24398 } else { 24399 TCP_DBGSTAT(tcp_timeout_cached_alloc); 24400 mp = tcp->tcp_timercache; 24401 tcp->tcp_timercache = mp->b_next; 24402 mp->b_next = NULL; 24403 ASSERT(mp->b_wptr == NULL); 24404 } 24405 24406 CONN_INC_REF(connp); 24407 tcpt = (tcp_timer_t *)mp->b_rptr; 24408 tcpt->connp = connp; 24409 tcpt->tcpt_proc = f; 24410 tcpt->tcpt_tid = timeout(tcp_timer_callback, mp, tim); 24411 return ((timeout_id_t)mp); 24412 } 24413 24414 static void 24415 tcp_timer_callback(void *arg) 24416 { 24417 mblk_t *mp = (mblk_t *)arg; 24418 tcp_timer_t *tcpt; 24419 conn_t *connp; 24420 24421 tcpt = (tcp_timer_t *)mp->b_rptr; 24422 connp = tcpt->connp; 24423 squeue_fill(connp->conn_sqp, mp, 24424 tcp_timer_handler, connp, SQTAG_TCP_TIMER); 24425 } 24426 24427 static void 24428 tcp_timer_handler(void *arg, mblk_t *mp, void *arg2) 24429 { 24430 tcp_timer_t *tcpt; 24431 conn_t *connp = (conn_t *)arg; 24432 tcp_t *tcp = connp->conn_tcp; 24433 24434 tcpt = (tcp_timer_t *)mp->b_rptr; 24435 ASSERT(connp == tcpt->connp); 24436 ASSERT((squeue_t *)arg2 == connp->conn_sqp); 24437 24438 /* 24439 * If the TCP has reached the closed state, don't proceed any 24440 * further. This TCP logically does not exist on the system. 24441 * tcpt_proc could for example access queues, that have already 24442 * been qprocoff'ed off. Also see comments at the start of tcp_input 24443 */ 24444 if (tcp->tcp_state != TCPS_CLOSED) { 24445 (*tcpt->tcpt_proc)(connp); 24446 } else { 24447 tcp->tcp_timer_tid = 0; 24448 } 24449 tcp_timer_free(connp->conn_tcp, mp); 24450 } 24451 24452 /* 24453 * There is potential race with untimeout and the handler firing at the same 24454 * time. The mblock may be freed by the handler while we are trying to use 24455 * it. But since both should execute on the same squeue, this race should not 24456 * occur. 24457 */ 24458 clock_t 24459 tcp_timeout_cancel(conn_t *connp, timeout_id_t id) 24460 { 24461 mblk_t *mp = (mblk_t *)id; 24462 tcp_timer_t *tcpt; 24463 clock_t delta; 24464 24465 TCP_DBGSTAT(tcp_timeout_cancel_reqs); 24466 24467 if (mp == NULL) 24468 return (-1); 24469 24470 tcpt = (tcp_timer_t *)mp->b_rptr; 24471 ASSERT(tcpt->connp == connp); 24472 24473 delta = untimeout(tcpt->tcpt_tid); 24474 24475 if (delta >= 0) { 24476 TCP_DBGSTAT(tcp_timeout_canceled); 24477 tcp_timer_free(connp->conn_tcp, mp); 24478 CONN_DEC_REF(connp); 24479 } 24480 24481 return (delta); 24482 } 24483 24484 /* 24485 * Allocate space for the timer event. The allocation looks like mblk, but it is 24486 * not a proper mblk. To avoid confusion we set b_wptr to NULL. 24487 * 24488 * Dealing with failures: If we can't allocate from the timer cache we try 24489 * allocating from dblock caches using allocb_tryhard(). In this case b_wptr 24490 * points to b_rptr. 24491 * If we can't allocate anything using allocb_tryhard(), we perform a last 24492 * attempt and use kmem_alloc_tryhard(). In this case we set b_wptr to -1 and 24493 * save the actual allocation size in b_datap. 24494 */ 24495 mblk_t * 24496 tcp_timermp_alloc(int kmflags) 24497 { 24498 mblk_t *mp = (mblk_t *)kmem_cache_alloc(tcp_timercache, 24499 kmflags & ~KM_PANIC); 24500 24501 if (mp != NULL) { 24502 mp->b_next = mp->b_prev = NULL; 24503 mp->b_rptr = (uchar_t *)(&mp[1]); 24504 mp->b_wptr = NULL; 24505 mp->b_datap = NULL; 24506 mp->b_queue = NULL; 24507 } else if (kmflags & KM_PANIC) { 24508 /* 24509 * Failed to allocate memory for the timer. Try allocating from 24510 * dblock caches. 24511 */ 24512 TCP_STAT(tcp_timermp_allocfail); 24513 mp = allocb_tryhard(sizeof (tcp_timer_t)); 24514 if (mp == NULL) { 24515 size_t size = 0; 24516 /* 24517 * Memory is really low. Try tryhard allocation. 24518 */ 24519 TCP_STAT(tcp_timermp_allocdblfail); 24520 mp = kmem_alloc_tryhard(sizeof (mblk_t) + 24521 sizeof (tcp_timer_t), &size, kmflags); 24522 mp->b_rptr = (uchar_t *)(&mp[1]); 24523 mp->b_next = mp->b_prev = NULL; 24524 mp->b_wptr = (uchar_t *)-1; 24525 mp->b_datap = (dblk_t *)size; 24526 mp->b_queue = NULL; 24527 } 24528 ASSERT(mp->b_wptr != NULL); 24529 } 24530 TCP_DBGSTAT(tcp_timermp_alloced); 24531 24532 return (mp); 24533 } 24534 24535 /* 24536 * Free per-tcp timer cache. 24537 * It can only contain entries from tcp_timercache. 24538 */ 24539 void 24540 tcp_timermp_free(tcp_t *tcp) 24541 { 24542 mblk_t *mp; 24543 24544 while ((mp = tcp->tcp_timercache) != NULL) { 24545 ASSERT(mp->b_wptr == NULL); 24546 tcp->tcp_timercache = tcp->tcp_timercache->b_next; 24547 kmem_cache_free(tcp_timercache, mp); 24548 } 24549 } 24550 24551 /* 24552 * Free timer event. Put it on the per-tcp timer cache if there is not too many 24553 * events there already (currently at most two events are cached). 24554 * If the event is not allocated from the timer cache, free it right away. 24555 */ 24556 static void 24557 tcp_timer_free(tcp_t *tcp, mblk_t *mp) 24558 { 24559 mblk_t *mp1 = tcp->tcp_timercache; 24560 24561 if (mp->b_wptr != NULL) { 24562 /* 24563 * This allocation is not from a timer cache, free it right 24564 * away. 24565 */ 24566 if (mp->b_wptr != (uchar_t *)-1) 24567 freeb(mp); 24568 else 24569 kmem_free(mp, (size_t)mp->b_datap); 24570 } else if (mp1 == NULL || mp1->b_next == NULL) { 24571 /* Cache this timer block for future allocations */ 24572 mp->b_rptr = (uchar_t *)(&mp[1]); 24573 mp->b_next = mp1; 24574 tcp->tcp_timercache = mp; 24575 } else { 24576 kmem_cache_free(tcp_timercache, mp); 24577 TCP_DBGSTAT(tcp_timermp_freed); 24578 } 24579 } 24580 24581 /* 24582 * End of TCP Timers implementation. 24583 */ 24584 24585 /* 24586 * tcp_{set,clr}qfull() functions are used to either set or clear QFULL 24587 * on the specified backing STREAMS q. Note, the caller may make the 24588 * decision to call based on the tcp_t.tcp_flow_stopped value which 24589 * when check outside the q's lock is only an advisory check ... 24590 */ 24591 24592 void 24593 tcp_setqfull(tcp_t *tcp) 24594 { 24595 queue_t *q = tcp->tcp_wq; 24596 24597 if (!(q->q_flag & QFULL)) { 24598 mutex_enter(QLOCK(q)); 24599 if (!(q->q_flag & QFULL)) { 24600 /* still need to set QFULL */ 24601 q->q_flag |= QFULL; 24602 tcp->tcp_flow_stopped = B_TRUE; 24603 mutex_exit(QLOCK(q)); 24604 TCP_STAT(tcp_flwctl_on); 24605 } else { 24606 mutex_exit(QLOCK(q)); 24607 } 24608 } 24609 } 24610 24611 void 24612 tcp_clrqfull(tcp_t *tcp) 24613 { 24614 queue_t *q = tcp->tcp_wq; 24615 24616 if (q->q_flag & QFULL) { 24617 mutex_enter(QLOCK(q)); 24618 if (q->q_flag & QFULL) { 24619 q->q_flag &= ~QFULL; 24620 tcp->tcp_flow_stopped = B_FALSE; 24621 mutex_exit(QLOCK(q)); 24622 if (q->q_flag & QWANTW) 24623 qbackenable(q, 0); 24624 } else { 24625 mutex_exit(QLOCK(q)); 24626 } 24627 } 24628 } 24629 24630 /* 24631 * TCP Kstats implementation 24632 */ 24633 static void 24634 tcp_kstat_init(void) 24635 { 24636 tcp_named_kstat_t template = { 24637 { "rtoAlgorithm", KSTAT_DATA_INT32, 0 }, 24638 { "rtoMin", KSTAT_DATA_INT32, 0 }, 24639 { "rtoMax", KSTAT_DATA_INT32, 0 }, 24640 { "maxConn", KSTAT_DATA_INT32, 0 }, 24641 { "activeOpens", KSTAT_DATA_UINT32, 0 }, 24642 { "passiveOpens", KSTAT_DATA_UINT32, 0 }, 24643 { "attemptFails", KSTAT_DATA_UINT32, 0 }, 24644 { "estabResets", KSTAT_DATA_UINT32, 0 }, 24645 { "currEstab", KSTAT_DATA_UINT32, 0 }, 24646 { "inSegs", KSTAT_DATA_UINT32, 0 }, 24647 { "outSegs", KSTAT_DATA_UINT32, 0 }, 24648 { "retransSegs", KSTAT_DATA_UINT32, 0 }, 24649 { "connTableSize", KSTAT_DATA_INT32, 0 }, 24650 { "outRsts", KSTAT_DATA_UINT32, 0 }, 24651 { "outDataSegs", KSTAT_DATA_UINT32, 0 }, 24652 { "outDataBytes", KSTAT_DATA_UINT32, 0 }, 24653 { "retransBytes", KSTAT_DATA_UINT32, 0 }, 24654 { "outAck", KSTAT_DATA_UINT32, 0 }, 24655 { "outAckDelayed", KSTAT_DATA_UINT32, 0 }, 24656 { "outUrg", KSTAT_DATA_UINT32, 0 }, 24657 { "outWinUpdate", KSTAT_DATA_UINT32, 0 }, 24658 { "outWinProbe", KSTAT_DATA_UINT32, 0 }, 24659 { "outControl", KSTAT_DATA_UINT32, 0 }, 24660 { "outFastRetrans", KSTAT_DATA_UINT32, 0 }, 24661 { "inAckSegs", KSTAT_DATA_UINT32, 0 }, 24662 { "inAckBytes", KSTAT_DATA_UINT32, 0 }, 24663 { "inDupAck", KSTAT_DATA_UINT32, 0 }, 24664 { "inAckUnsent", KSTAT_DATA_UINT32, 0 }, 24665 { "inDataInorderSegs", KSTAT_DATA_UINT32, 0 }, 24666 { "inDataInorderBytes", KSTAT_DATA_UINT32, 0 }, 24667 { "inDataUnorderSegs", KSTAT_DATA_UINT32, 0 }, 24668 { "inDataUnorderBytes", KSTAT_DATA_UINT32, 0 }, 24669 { "inDataDupSegs", KSTAT_DATA_UINT32, 0 }, 24670 { "inDataDupBytes", KSTAT_DATA_UINT32, 0 }, 24671 { "inDataPartDupSegs", KSTAT_DATA_UINT32, 0 }, 24672 { "inDataPartDupBytes", KSTAT_DATA_UINT32, 0 }, 24673 { "inDataPastWinSegs", KSTAT_DATA_UINT32, 0 }, 24674 { "inDataPastWinBytes", KSTAT_DATA_UINT32, 0 }, 24675 { "inWinProbe", KSTAT_DATA_UINT32, 0 }, 24676 { "inWinUpdate", KSTAT_DATA_UINT32, 0 }, 24677 { "inClosed", KSTAT_DATA_UINT32, 0 }, 24678 { "rttUpdate", KSTAT_DATA_UINT32, 0 }, 24679 { "rttNoUpdate", KSTAT_DATA_UINT32, 0 }, 24680 { "timRetrans", KSTAT_DATA_UINT32, 0 }, 24681 { "timRetransDrop", KSTAT_DATA_UINT32, 0 }, 24682 { "timKeepalive", KSTAT_DATA_UINT32, 0 }, 24683 { "timKeepaliveProbe", KSTAT_DATA_UINT32, 0 }, 24684 { "timKeepaliveDrop", KSTAT_DATA_UINT32, 0 }, 24685 { "listenDrop", KSTAT_DATA_UINT32, 0 }, 24686 { "listenDropQ0", KSTAT_DATA_UINT32, 0 }, 24687 { "halfOpenDrop", KSTAT_DATA_UINT32, 0 }, 24688 { "outSackRetransSegs", KSTAT_DATA_UINT32, 0 }, 24689 { "connTableSize6", KSTAT_DATA_INT32, 0 } 24690 }; 24691 24692 tcp_mibkp = kstat_create(TCP_MOD_NAME, 0, TCP_MOD_NAME, 24693 "mib2", KSTAT_TYPE_NAMED, NUM_OF_FIELDS(tcp_named_kstat_t), 0); 24694 24695 if (tcp_mibkp == NULL) 24696 return; 24697 24698 template.rtoAlgorithm.value.ui32 = 4; 24699 template.rtoMin.value.ui32 = tcp_rexmit_interval_min; 24700 template.rtoMax.value.ui32 = tcp_rexmit_interval_max; 24701 template.maxConn.value.i32 = -1; 24702 24703 bcopy(&template, tcp_mibkp->ks_data, sizeof (template)); 24704 24705 tcp_mibkp->ks_update = tcp_kstat_update; 24706 24707 kstat_install(tcp_mibkp); 24708 } 24709 24710 static void 24711 tcp_kstat_fini(void) 24712 { 24713 24714 if (tcp_mibkp != NULL) { 24715 kstat_delete(tcp_mibkp); 24716 tcp_mibkp = NULL; 24717 } 24718 } 24719 24720 static int 24721 tcp_kstat_update(kstat_t *kp, int rw) 24722 { 24723 tcp_named_kstat_t *tcpkp; 24724 tcp_t *tcp; 24725 connf_t *connfp; 24726 conn_t *connp; 24727 int i; 24728 24729 if (!kp || !kp->ks_data) 24730 return (EIO); 24731 24732 if (rw == KSTAT_WRITE) 24733 return (EACCES); 24734 24735 tcpkp = (tcp_named_kstat_t *)kp->ks_data; 24736 24737 tcpkp->currEstab.value.ui32 = 0; 24738 24739 for (i = 0; i < CONN_G_HASH_SIZE; i++) { 24740 connfp = &ipcl_globalhash_fanout[i]; 24741 connp = NULL; 24742 while ((connp = 24743 ipcl_get_next_conn(connfp, connp, IPCL_TCP)) != NULL) { 24744 tcp = connp->conn_tcp; 24745 switch (tcp_snmp_state(tcp)) { 24746 case MIB2_TCP_established: 24747 case MIB2_TCP_closeWait: 24748 tcpkp->currEstab.value.ui32++; 24749 break; 24750 } 24751 } 24752 } 24753 24754 tcpkp->activeOpens.value.ui32 = tcp_mib.tcpActiveOpens; 24755 tcpkp->passiveOpens.value.ui32 = tcp_mib.tcpPassiveOpens; 24756 tcpkp->attemptFails.value.ui32 = tcp_mib.tcpAttemptFails; 24757 tcpkp->estabResets.value.ui32 = tcp_mib.tcpEstabResets; 24758 tcpkp->inSegs.value.ui32 = tcp_mib.tcpInSegs; 24759 tcpkp->outSegs.value.ui32 = tcp_mib.tcpOutSegs; 24760 tcpkp->retransSegs.value.ui32 = tcp_mib.tcpRetransSegs; 24761 tcpkp->connTableSize.value.i32 = tcp_mib.tcpConnTableSize; 24762 tcpkp->outRsts.value.ui32 = tcp_mib.tcpOutRsts; 24763 tcpkp->outDataSegs.value.ui32 = tcp_mib.tcpOutDataSegs; 24764 tcpkp->outDataBytes.value.ui32 = tcp_mib.tcpOutDataBytes; 24765 tcpkp->retransBytes.value.ui32 = tcp_mib.tcpRetransBytes; 24766 tcpkp->outAck.value.ui32 = tcp_mib.tcpOutAck; 24767 tcpkp->outAckDelayed.value.ui32 = tcp_mib.tcpOutAckDelayed; 24768 tcpkp->outUrg.value.ui32 = tcp_mib.tcpOutUrg; 24769 tcpkp->outWinUpdate.value.ui32 = tcp_mib.tcpOutWinUpdate; 24770 tcpkp->outWinProbe.value.ui32 = tcp_mib.tcpOutWinProbe; 24771 tcpkp->outControl.value.ui32 = tcp_mib.tcpOutControl; 24772 tcpkp->outFastRetrans.value.ui32 = tcp_mib.tcpOutFastRetrans; 24773 tcpkp->inAckSegs.value.ui32 = tcp_mib.tcpInAckSegs; 24774 tcpkp->inAckBytes.value.ui32 = tcp_mib.tcpInAckBytes; 24775 tcpkp->inDupAck.value.ui32 = tcp_mib.tcpInDupAck; 24776 tcpkp->inAckUnsent.value.ui32 = tcp_mib.tcpInAckUnsent; 24777 tcpkp->inDataInorderSegs.value.ui32 = tcp_mib.tcpInDataInorderSegs; 24778 tcpkp->inDataInorderBytes.value.ui32 = tcp_mib.tcpInDataInorderBytes; 24779 tcpkp->inDataUnorderSegs.value.ui32 = tcp_mib.tcpInDataUnorderSegs; 24780 tcpkp->inDataUnorderBytes.value.ui32 = tcp_mib.tcpInDataUnorderBytes; 24781 tcpkp->inDataDupSegs.value.ui32 = tcp_mib.tcpInDataDupSegs; 24782 tcpkp->inDataDupBytes.value.ui32 = tcp_mib.tcpInDataDupBytes; 24783 tcpkp->inDataPartDupSegs.value.ui32 = tcp_mib.tcpInDataPartDupSegs; 24784 tcpkp->inDataPartDupBytes.value.ui32 = tcp_mib.tcpInDataPartDupBytes; 24785 tcpkp->inDataPastWinSegs.value.ui32 = tcp_mib.tcpInDataPastWinSegs; 24786 tcpkp->inDataPastWinBytes.value.ui32 = tcp_mib.tcpInDataPastWinBytes; 24787 tcpkp->inWinProbe.value.ui32 = tcp_mib.tcpInWinProbe; 24788 tcpkp->inWinUpdate.value.ui32 = tcp_mib.tcpInWinUpdate; 24789 tcpkp->inClosed.value.ui32 = tcp_mib.tcpInClosed; 24790 tcpkp->rttNoUpdate.value.ui32 = tcp_mib.tcpRttNoUpdate; 24791 tcpkp->rttUpdate.value.ui32 = tcp_mib.tcpRttUpdate; 24792 tcpkp->timRetrans.value.ui32 = tcp_mib.tcpTimRetrans; 24793 tcpkp->timRetransDrop.value.ui32 = tcp_mib.tcpTimRetransDrop; 24794 tcpkp->timKeepalive.value.ui32 = tcp_mib.tcpTimKeepalive; 24795 tcpkp->timKeepaliveProbe.value.ui32 = tcp_mib.tcpTimKeepaliveProbe; 24796 tcpkp->timKeepaliveDrop.value.ui32 = tcp_mib.tcpTimKeepaliveDrop; 24797 tcpkp->listenDrop.value.ui32 = tcp_mib.tcpListenDrop; 24798 tcpkp->listenDropQ0.value.ui32 = tcp_mib.tcpListenDropQ0; 24799 tcpkp->halfOpenDrop.value.ui32 = tcp_mib.tcpHalfOpenDrop; 24800 tcpkp->outSackRetransSegs.value.ui32 = tcp_mib.tcpOutSackRetransSegs; 24801 tcpkp->connTableSize6.value.i32 = tcp_mib.tcp6ConnTableSize; 24802 24803 return (0); 24804 } 24805 24806 void 24807 tcp_reinput(conn_t *connp, mblk_t *mp, squeue_t *sqp) 24808 { 24809 uint16_t hdr_len; 24810 ipha_t *ipha; 24811 uint8_t *nexthdrp; 24812 tcph_t *tcph; 24813 24814 /* Already has an eager */ 24815 if ((mp->b_datap->db_struioflag & STRUIO_EAGER) != 0) { 24816 TCP_STAT(tcp_reinput_syn); 24817 squeue_enter(connp->conn_sqp, mp, connp->conn_recv, 24818 connp, SQTAG_TCP_REINPUT_EAGER); 24819 return; 24820 } 24821 24822 switch (IPH_HDR_VERSION(mp->b_rptr)) { 24823 case IPV4_VERSION: 24824 ipha = (ipha_t *)mp->b_rptr; 24825 hdr_len = IPH_HDR_LENGTH(ipha); 24826 break; 24827 case IPV6_VERSION: 24828 if (!ip_hdr_length_nexthdr_v6(mp, (ip6_t *)mp->b_rptr, 24829 &hdr_len, &nexthdrp)) { 24830 CONN_DEC_REF(connp); 24831 freemsg(mp); 24832 return; 24833 } 24834 break; 24835 } 24836 24837 tcph = (tcph_t *)&mp->b_rptr[hdr_len]; 24838 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) { 24839 mp->b_datap->db_struioflag |= STRUIO_EAGER; 24840 DB_CKSUMSTART(mp) = (intptr_t)sqp; 24841 } 24842 24843 squeue_fill(connp->conn_sqp, mp, connp->conn_recv, connp, 24844 SQTAG_TCP_REINPUT); 24845 } 24846 24847 static squeue_func_t 24848 tcp_squeue_switch(int val) 24849 { 24850 squeue_func_t rval = squeue_fill; 24851 24852 switch (val) { 24853 case 1: 24854 rval = squeue_enter_nodrain; 24855 break; 24856 case 2: 24857 rval = squeue_enter; 24858 break; 24859 default: 24860 break; 24861 } 24862 return (rval); 24863 } 24864 24865 static void 24866 tcp_squeue_add(squeue_t *sqp) 24867 { 24868 tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc( 24869 sizeof (tcp_squeue_priv_t), KM_SLEEP); 24870 24871 *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait; 24872 tcp_time_wait->tcp_time_wait_tid = timeout(tcp_time_wait_collector, 24873 sqp, TCP_TIME_WAIT_DELAY); 24874 if (tcp_free_list_max_cnt == 0) { 24875 int tcp_ncpus = ((boot_max_ncpus == -1) ? 24876 max_ncpus : boot_max_ncpus); 24877 24878 /* 24879 * Limit number of entries to 1% of availble memory / tcp_ncpus 24880 */ 24881 tcp_free_list_max_cnt = (freemem * PAGESIZE) / 24882 (tcp_ncpus * sizeof (tcp_t) * 100); 24883 } 24884 tcp_time_wait->tcp_free_list_cnt = 0; 24885 } 24886