17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5adccf2f2Skcpoon * Common Development and Distribution License (the "License").
6adccf2f2Skcpoon * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
21adccf2f2Skcpoon
227c478bd9Sstevel@tonic-gate /*
2366cd0f60SKacheong Poon * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
243d0a255cSGarrett D'Amore * Copyright (c) 2011 Nexenta Systems, Inc. All rights reserved.
2545a4b79dSSebastien Roy * Copyright (c) 2013, 2017 by Delphix. All rights reserved.
26a1ca8b43SDan McDonald * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
272ad53042SDan McDonald * Copyright 2020 Joyent, Inc.
28*1edba515SAndy Fiddaman * Copyright 2024 Oxide Computer Company
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate /* Copyright (c) 1990 Mentat Inc. */
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate #include <sys/types.h>
337c478bd9Sstevel@tonic-gate #include <sys/stream.h>
347c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
357c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
367c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
377c478bd9Sstevel@tonic-gate #include <sys/strlog.h>
387c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2
397c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
407c478bd9Sstevel@tonic-gate #include <sys/timod.h>
417c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
427c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
437c478bd9Sstevel@tonic-gate #include <sys/suntpi.h>
447c478bd9Sstevel@tonic-gate #include <sys/xti_inet.h>
457c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
467c478bd9Sstevel@tonic-gate #include <sys/debug.h>
47381a2a9aSdr146992 #include <sys/sdt.h>
487c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/ethernet.h>
517c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
527c478bd9Sstevel@tonic-gate #include <sys/dlpi.h>
537c478bd9Sstevel@tonic-gate #include <sys/pattr.h>
547c478bd9Sstevel@tonic-gate #include <sys/policy.h>
5545916cd2Sjpk #include <sys/priv.h>
567c478bd9Sstevel@tonic-gate #include <sys/zone.h>
57f4b3ec61Sdh155122 #include <sys/sunldi.h>
587c478bd9Sstevel@tonic-gate
597c478bd9Sstevel@tonic-gate #include <sys/errno.h>
607c478bd9Sstevel@tonic-gate #include <sys/signal.h>
617c478bd9Sstevel@tonic-gate #include <sys/socket.h>
620f1702c5SYu Xiangning #include <sys/socketvar.h>
637c478bd9Sstevel@tonic-gate #include <sys/sockio.h>
647c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
657c478bd9Sstevel@tonic-gate #include <sys/md5.h>
667c478bd9Sstevel@tonic-gate #include <sys/random.h>
6717169044Sbrutus #include <sys/uio.h>
6887a18d3fSMadhavan Venkataraman #include <sys/systm.h>
697c478bd9Sstevel@tonic-gate #include <netinet/in.h>
707c478bd9Sstevel@tonic-gate #include <netinet/tcp.h>
717c478bd9Sstevel@tonic-gate #include <netinet/ip6.h>
727c478bd9Sstevel@tonic-gate #include <netinet/icmp6.h>
737c478bd9Sstevel@tonic-gate #include <net/if.h>
747c478bd9Sstevel@tonic-gate #include <net/route.h>
757c478bd9Sstevel@tonic-gate #include <inet/ipsec_impl.h>
76*1edba515SAndy Fiddaman #include <inet/tcp_sig.h>
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate #include <inet/common.h>
7945a4b79dSSebastien Roy #include <inet/cc.h>
807c478bd9Sstevel@tonic-gate #include <inet/ip.h>
81ff550d0eSmasputra #include <inet/ip_impl.h>
827c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
837c478bd9Sstevel@tonic-gate #include <inet/ip_ndp.h>
840f1702c5SYu Xiangning #include <inet/proto_set.h>
857c478bd9Sstevel@tonic-gate #include <inet/mib2.h>
867c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
877c478bd9Sstevel@tonic-gate #include <inet/snmpcom.h>
887c478bd9Sstevel@tonic-gate #include <inet/kstatcom.h>
897c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
90ff550d0eSmasputra #include <inet/tcp_impl.h>
91721fffe3SKacheong Poon #include <inet/tcp_cluster.h>
92ae6aa22aSVenugopal Iyer #include <inet/udp_impl.h>
937c478bd9Sstevel@tonic-gate #include <net/pfkeyv2.h>
947c478bd9Sstevel@tonic-gate #include <inet/ipdrop.h>
957c478bd9Sstevel@tonic-gate
967c478bd9Sstevel@tonic-gate #include <inet/ipclassifier.h>
977c478bd9Sstevel@tonic-gate #include <inet/ip_ire.h>
98c793af95Ssangeeta #include <inet/ip_ftable.h>
997c478bd9Sstevel@tonic-gate #include <inet/ip_if.h>
1007c478bd9Sstevel@tonic-gate #include <inet/ipp_common.h>
101bd670b35SErik Nordmark #include <inet/ip_rts.h>
102381a2a9aSdr146992 #include <inet/ip_netinfo.h>
103da14cebeSEric Cheng #include <sys/squeue_impl.h>
1047c478bd9Sstevel@tonic-gate #include <sys/squeue.h>
10545916cd2Sjpk #include <sys/tsol/label.h>
10645916cd2Sjpk #include <sys/tsol/tnet.h>
10745916cd2Sjpk #include <rpc/pmap_prot.h>
10887a18d3fSMadhavan Venkataraman #include <sys/callo.h>
1097c478bd9Sstevel@tonic-gate
1107c478bd9Sstevel@tonic-gate /*
1117c478bd9Sstevel@tonic-gate * TCP Notes: aka FireEngine Phase I (PSARC 2002/433)
1127c478bd9Sstevel@tonic-gate *
1137c478bd9Sstevel@tonic-gate * (Read the detailed design doc in PSARC case directory)
1147c478bd9Sstevel@tonic-gate *
1157c478bd9Sstevel@tonic-gate * The entire tcp state is contained in tcp_t and conn_t structure
1167c478bd9Sstevel@tonic-gate * which are allocated in tandem using ipcl_conn_create() and passing
117bd670b35SErik Nordmark * IPCL_TCPCONN as a flag. We use 'conn_ref' and 'conn_lock' to protect
1187c478bd9Sstevel@tonic-gate * the references on the tcp_t. The tcp_t structure is never compressed
1197c478bd9Sstevel@tonic-gate * and packets always land on the correct TCP perimeter from the time
1207c478bd9Sstevel@tonic-gate * eager is created till the time tcp_t dies (as such the old mentat
1217c478bd9Sstevel@tonic-gate * TCP global queue is not used for detached state and no IPSEC checking
1227c478bd9Sstevel@tonic-gate * is required). The global queue is still allocated to send out resets
1237c478bd9Sstevel@tonic-gate * for connection which have no listeners and IP directly calls
1247c478bd9Sstevel@tonic-gate * tcp_xmit_listeners_reset() which does any policy check.
1257c478bd9Sstevel@tonic-gate *
1267c478bd9Sstevel@tonic-gate * Protection and Synchronisation mechanism:
1277c478bd9Sstevel@tonic-gate *
1287c478bd9Sstevel@tonic-gate * The tcp data structure does not use any kind of lock for protecting
1297c478bd9Sstevel@tonic-gate * its state but instead uses 'squeues' for mutual exclusion from various
1307c478bd9Sstevel@tonic-gate * read and write side threads. To access a tcp member, the thread should
131da14cebeSEric Cheng * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS,
132da14cebeSEric Cheng * or SQ_NODRAIN). Since the squeues allow a direct function call, caller
1337c478bd9Sstevel@tonic-gate * can pass any tcp function having prototype of edesc_t as argument
1347c478bd9Sstevel@tonic-gate * (different from traditional STREAMs model where packets come in only
1357c478bd9Sstevel@tonic-gate * designated entry points). The list of functions that can be directly
1367c478bd9Sstevel@tonic-gate * called via squeue are listed before the usual function prototype.
1377c478bd9Sstevel@tonic-gate *
1387c478bd9Sstevel@tonic-gate * Referencing:
1397c478bd9Sstevel@tonic-gate *
1407c478bd9Sstevel@tonic-gate * TCP is MT-Hot and we use a reference based scheme to make sure that the
1417c478bd9Sstevel@tonic-gate * tcp structure doesn't disappear when its needed. When the application
1427c478bd9Sstevel@tonic-gate * creates an outgoing connection or accepts an incoming connection, we
1437c478bd9Sstevel@tonic-gate * start out with 2 references on 'conn_ref'. One for TCP and one for IP.
1447c478bd9Sstevel@tonic-gate * The IP reference is just a symbolic reference since ip_tcpclose()
1457c478bd9Sstevel@tonic-gate * looks at tcp structure after tcp_close_output() returns which could
1467c478bd9Sstevel@tonic-gate * have dropped the last TCP reference. So as long as the connection is
1477c478bd9Sstevel@tonic-gate * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the
1487c478bd9Sstevel@tonic-gate * conn_t. The classifier puts its own reference when the connection is
1497c478bd9Sstevel@tonic-gate * inserted in listen or connected hash. Anytime a thread needs to enter
1507c478bd9Sstevel@tonic-gate * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr
1517c478bd9Sstevel@tonic-gate * on write side or by doing a classify on read side and then puts a
1527c478bd9Sstevel@tonic-gate * reference on the conn before doing squeue_enter/tryenter/fill. For
1537c478bd9Sstevel@tonic-gate * read side, the classifier itself puts the reference under fanout lock
1547c478bd9Sstevel@tonic-gate * to make sure that tcp can't disappear before it gets processed. The
1557c478bd9Sstevel@tonic-gate * squeue will drop this reference automatically so the called function
1567c478bd9Sstevel@tonic-gate * doesn't have to do a DEC_REF.
1577c478bd9Sstevel@tonic-gate *
1587c478bd9Sstevel@tonic-gate * Opening a new connection:
1597c478bd9Sstevel@tonic-gate *
160f4b3ec61Sdh155122 * The outgoing connection open is pretty simple. tcp_open() does the
1617c478bd9Sstevel@tonic-gate * work in creating the conn/tcp structure and initializing it. The
1627c478bd9Sstevel@tonic-gate * squeue assignment is done based on the CPU the application
1637c478bd9Sstevel@tonic-gate * is running on. So for outbound connections, processing is always done
1647c478bd9Sstevel@tonic-gate * on application CPU which might be different from the incoming CPU
1657c478bd9Sstevel@tonic-gate * being interrupted by the NIC. An optimal way would be to figure out
1667c478bd9Sstevel@tonic-gate * the NIC <-> CPU binding at listen time, and assign the outgoing
1677c478bd9Sstevel@tonic-gate * connection to the squeue attached to the CPU that will be interrupted
1687c478bd9Sstevel@tonic-gate * for incoming packets (we know the NIC based on the bind IP address).
1697c478bd9Sstevel@tonic-gate * This might seem like a problem if more data is going out but the
1707c478bd9Sstevel@tonic-gate * fact is that in most cases the transmit is ACK driven transmit where
1717c478bd9Sstevel@tonic-gate * the outgoing data normally sits on TCP's xmit queue waiting to be
1727c478bd9Sstevel@tonic-gate * transmitted.
1737c478bd9Sstevel@tonic-gate *
1747c478bd9Sstevel@tonic-gate * Accepting a connection:
1757c478bd9Sstevel@tonic-gate *
1767c478bd9Sstevel@tonic-gate * This is a more interesting case because of various races involved in
1777c478bd9Sstevel@tonic-gate * establishing a eager in its own perimeter. Read the meta comment on
178bd670b35SErik Nordmark * top of tcp_input_listener(). But briefly, the squeue is picked by
179bd670b35SErik Nordmark * ip_fanout based on the ring or the sender (if loopback).
1807c478bd9Sstevel@tonic-gate *
1817c478bd9Sstevel@tonic-gate * Closing a connection:
1827c478bd9Sstevel@tonic-gate *
1837c478bd9Sstevel@tonic-gate * The close is fairly straight forward. tcp_close() calls tcp_close_output()
1847c478bd9Sstevel@tonic-gate * via squeue to do the close and mark the tcp as detached if the connection
1857c478bd9Sstevel@tonic-gate * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its
1867c478bd9Sstevel@tonic-gate * reference but tcp_close() drop IP's reference always. So if tcp was
1877c478bd9Sstevel@tonic-gate * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP
1887c478bd9Sstevel@tonic-gate * and 1 because it is in classifier's connected hash. This is the condition
1897c478bd9Sstevel@tonic-gate * we use to determine that its OK to clean up the tcp outside of squeue
1907c478bd9Sstevel@tonic-gate * when time wait expires (check the ref under fanout and conn_lock and
1917c478bd9Sstevel@tonic-gate * if it is 2, remove it from fanout hash and kill it).
1927c478bd9Sstevel@tonic-gate *
1937c478bd9Sstevel@tonic-gate * Although close just drops the necessary references and marks the
1947c478bd9Sstevel@tonic-gate * tcp_detached state, tcp_close needs to know the tcp_detached has been
1957c478bd9Sstevel@tonic-gate * set (under squeue) before letting the STREAM go away (because a
1967c478bd9Sstevel@tonic-gate * inbound packet might attempt to go up the STREAM while the close
1977c478bd9Sstevel@tonic-gate * has happened and tcp_detached is not set). So a special lock and
1987c478bd9Sstevel@tonic-gate * flag is used along with a condition variable (tcp_closelock, tcp_closed,
1997c478bd9Sstevel@tonic-gate * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked
2007c478bd9Sstevel@tonic-gate * tcp_detached.
2017c478bd9Sstevel@tonic-gate *
2027c478bd9Sstevel@tonic-gate * Special provisions and fast paths:
2037c478bd9Sstevel@tonic-gate *
204bd670b35SErik Nordmark * We make special provisions for sockfs by marking tcp_issocket
2057c478bd9Sstevel@tonic-gate * whenever we have only sockfs on top of TCP. This allows us to skip
2067c478bd9Sstevel@tonic-gate * putting the tcp in acceptor hash since a sockfs listener can never
2077c478bd9Sstevel@tonic-gate * become acceptor and also avoid allocating a tcp_t for acceptor STREAM
2087c478bd9Sstevel@tonic-gate * since eager has already been allocated and the accept now happens
2097c478bd9Sstevel@tonic-gate * on acceptor STREAM. There is a big blob of comment on top of
210bd670b35SErik Nordmark * tcp_input_listener explaining the new accept. When socket is POP'd,
2117c478bd9Sstevel@tonic-gate * sockfs sends us an ioctl to mark the fact and we go back to old
2127c478bd9Sstevel@tonic-gate * behaviour. Once tcp_issocket is unset, its never set for the
2137c478bd9Sstevel@tonic-gate * life of that connection.
2147c478bd9Sstevel@tonic-gate *
2157c478bd9Sstevel@tonic-gate * IPsec notes :
2167c478bd9Sstevel@tonic-gate *
2177c478bd9Sstevel@tonic-gate * Since a packet is always executed on the correct TCP perimeter
2187c478bd9Sstevel@tonic-gate * all IPsec processing is defered to IP including checking new
2197c478bd9Sstevel@tonic-gate * connections and setting IPSEC policies for new connection. The
2207c478bd9Sstevel@tonic-gate * only exception is tcp_xmit_listeners_reset() which is called
2217c478bd9Sstevel@tonic-gate * directly from IP and needs to policy check to see if TH_RST
2227c478bd9Sstevel@tonic-gate * can be sent out.
2237c478bd9Sstevel@tonic-gate */
2247c478bd9Sstevel@tonic-gate
2257c478bd9Sstevel@tonic-gate /*
2267c478bd9Sstevel@tonic-gate * Values for squeue switch:
227da14cebeSEric Cheng * 1: SQ_NODRAIN
228da14cebeSEric Cheng * 2: SQ_PROCESS
229da14cebeSEric Cheng * 3: SQ_FILL
2307c478bd9Sstevel@tonic-gate */
231da14cebeSEric Cheng int tcp_squeue_wput = 2; /* /etc/systems */
232da14cebeSEric Cheng int tcp_squeue_flag;
2337c478bd9Sstevel@tonic-gate
2347c478bd9Sstevel@tonic-gate /*
2351dbf515bSethindra * To prevent memory hog, limit the number of entries in tcp_free_list
2361dbf515bSethindra * to 1% of available memory / number of cpus
2371dbf515bSethindra */
2381dbf515bSethindra uint_t tcp_free_list_max_cnt = 0;
2397c478bd9Sstevel@tonic-gate
2407c478bd9Sstevel@tonic-gate #define TIDUSZ 4096 /* transport interface data unit size */
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate /*
24393fcb0b9SKacheong Poon * Size of acceptor hash list. It has to be a power of 2 for hashing.
2447c478bd9Sstevel@tonic-gate */
245aec2a073SJerry Jelinek #define TCP_ACCEPTOR_FANOUT_SIZE 512
2467c478bd9Sstevel@tonic-gate
2477c478bd9Sstevel@tonic-gate #ifdef _ILP32
2487c478bd9Sstevel@tonic-gate #define TCP_ACCEPTOR_HASH(accid) \
24993fcb0b9SKacheong Poon (((uint_t)(accid) >> 8) & (TCP_ACCEPTOR_FANOUT_SIZE - 1))
2507c478bd9Sstevel@tonic-gate #else
2517c478bd9Sstevel@tonic-gate #define TCP_ACCEPTOR_HASH(accid) \
25293fcb0b9SKacheong Poon ((uint_t)(accid) & (TCP_ACCEPTOR_FANOUT_SIZE - 1))
2537c478bd9Sstevel@tonic-gate #endif /* _ILP32 */
2547c478bd9Sstevel@tonic-gate
2555dd46ab5SKacheong Poon /*
2565dd46ab5SKacheong Poon * Minimum number of connections which can be created per listener. Used
2575dd46ab5SKacheong Poon * when the listener connection count is in effect.
2585dd46ab5SKacheong Poon */
259721fffe3SKacheong Poon static uint32_t tcp_min_conn_listener = 2;
26093fcb0b9SKacheong Poon
26193fcb0b9SKacheong Poon uint32_t tcp_early_abort = 30;
26293fcb0b9SKacheong Poon
2637c478bd9Sstevel@tonic-gate /* TCP Timer control structure */
2647c478bd9Sstevel@tonic-gate typedef struct tcpt_s {
2657c478bd9Sstevel@tonic-gate pfv_t tcpt_pfv; /* The routine we are to call */
2667c478bd9Sstevel@tonic-gate tcp_t *tcpt_tcp; /* The parameter we are to pass in */
2677c478bd9Sstevel@tonic-gate } tcpt_t;
2687c478bd9Sstevel@tonic-gate
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate * Functions called directly via squeue having a prototype of edesc_t.
2717c478bd9Sstevel@tonic-gate */
272bd670b35SErik Nordmark void tcp_input_data(void *arg, mblk_t *mp, void *arg2,
273bd670b35SErik Nordmark ip_recv_attr_t *ira);
274bd670b35SErik Nordmark static void tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2,
275bd670b35SErik Nordmark ip_recv_attr_t *dummy);
2767c478bd9Sstevel@tonic-gate
2777c478bd9Sstevel@tonic-gate
2787c478bd9Sstevel@tonic-gate /* Prototype for TCP functions */
2797c478bd9Sstevel@tonic-gate static void tcp_random_init(void);
2807c478bd9Sstevel@tonic-gate int tcp_random(void);
2810f1702c5SYu Xiangning static int tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp,
282bd670b35SErik Nordmark in_port_t dstport, uint_t srcid);
2830f1702c5SYu Xiangning static int tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp,
284bd670b35SErik Nordmark in_port_t dstport, uint32_t flowinfo,
285bd670b35SErik Nordmark uint_t srcid, uint32_t scope_id);
2867c478bd9Sstevel@tonic-gate static void tcp_iss_init(tcp_t *tcp);
2877c478bd9Sstevel@tonic-gate static void tcp_reinit(tcp_t *tcp);
2887c478bd9Sstevel@tonic-gate static void tcp_reinit_values(tcp_t *tcp);
2897c478bd9Sstevel@tonic-gate
2908a06b3d6SToomas Soome static int tcp_wsrv(queue_t *q);
291bd670b35SErik Nordmark static void tcp_update_lso(tcp_t *tcp, ip_xmit_attr_t *ixa);
292bd670b35SErik Nordmark static void tcp_update_zcopy(tcp_t *tcp);
293bd670b35SErik Nordmark static void tcp_notify(void *, ip_xmit_attr_t *, ixa_notify_type_t,
294bd670b35SErik Nordmark ixa_notify_arg_t);
295f4b3ec61Sdh155122 static void *tcp_stack_init(netstackid_t stackid, netstack_t *ns);
296f4b3ec61Sdh155122 static void tcp_stack_fini(netstackid_t stackid, void *arg);
297721fffe3SKacheong Poon
298da14cebeSEric Cheng static int tcp_squeue_switch(int);
2997c478bd9Sstevel@tonic-gate
300fc80c0dfSnordmark static int tcp_open(queue_t *, dev_t *, int, int, cred_t *, boolean_t);
301fc80c0dfSnordmark static int tcp_openv4(queue_t *, dev_t *, int, int, cred_t *);
302fc80c0dfSnordmark static int tcp_openv6(queue_t *, dev_t *, int, int, cred_t *);
3037c478bd9Sstevel@tonic-gate
3047c478bd9Sstevel@tonic-gate static void tcp_squeue_add(squeue_t *);
3057c478bd9Sstevel@tonic-gate
306721fffe3SKacheong Poon struct module_info tcp_rinfo = {
307ff550d0eSmasputra TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, TCP_RECV_HIWATER, TCP_RECV_LOWATER
3087c478bd9Sstevel@tonic-gate };
3097c478bd9Sstevel@tonic-gate
3107c478bd9Sstevel@tonic-gate static struct module_info tcp_winfo = {
311ff550d0eSmasputra TCP_MOD_ID, TCP_MOD_NAME, 0, INFPSZ, 127, 16
3127c478bd9Sstevel@tonic-gate };
3137c478bd9Sstevel@tonic-gate
3147c478bd9Sstevel@tonic-gate /*
3157c478bd9Sstevel@tonic-gate * Entry points for TCP as a device. The normal case which supports
3167c478bd9Sstevel@tonic-gate * the TCP functionality.
317fc80c0dfSnordmark * We have separate open functions for the /dev/tcp and /dev/tcp6 devices.
3187c478bd9Sstevel@tonic-gate */
319fc80c0dfSnordmark struct qinit tcp_rinitv4 = {
3208a06b3d6SToomas Soome NULL, tcp_rsrv, tcp_openv4, tcp_tpi_close, NULL, &tcp_rinfo
321fc80c0dfSnordmark };
322fc80c0dfSnordmark
323fc80c0dfSnordmark struct qinit tcp_rinitv6 = {
3248a06b3d6SToomas Soome NULL, tcp_rsrv, tcp_openv6, tcp_tpi_close, NULL, &tcp_rinfo
3257c478bd9Sstevel@tonic-gate };
3267c478bd9Sstevel@tonic-gate
3277c478bd9Sstevel@tonic-gate struct qinit tcp_winit = {
3288a06b3d6SToomas Soome tcp_wput, tcp_wsrv, NULL, NULL, NULL, &tcp_winfo
3297c478bd9Sstevel@tonic-gate };
3307c478bd9Sstevel@tonic-gate
3317c478bd9Sstevel@tonic-gate /* Initial entry point for TCP in socket mode. */
3327c478bd9Sstevel@tonic-gate struct qinit tcp_sock_winit = {
3338a06b3d6SToomas Soome tcp_wput_sock, tcp_wsrv, NULL, NULL, NULL, &tcp_winfo
3347c478bd9Sstevel@tonic-gate };
3357c478bd9Sstevel@tonic-gate
3360f1702c5SYu Xiangning /* TCP entry point during fallback */
3370f1702c5SYu Xiangning struct qinit tcp_fallback_sock_winit = {
3388a06b3d6SToomas Soome tcp_wput_fallback, NULL, NULL, NULL, NULL, &tcp_winfo
3390f1702c5SYu Xiangning };
3400f1702c5SYu Xiangning
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate * Entry points for TCP as a acceptor STREAM opened by sockfs when doing
3437c478bd9Sstevel@tonic-gate * an accept. Avoid allocating data structures since eager has already
3447c478bd9Sstevel@tonic-gate * been created.
3457c478bd9Sstevel@tonic-gate */
3467c478bd9Sstevel@tonic-gate struct qinit tcp_acceptor_rinit = {
3478a06b3d6SToomas Soome NULL, tcp_rsrv, NULL, tcp_tpi_close_accept, NULL, &tcp_winfo
3487c478bd9Sstevel@tonic-gate };
3497c478bd9Sstevel@tonic-gate
3507c478bd9Sstevel@tonic-gate struct qinit tcp_acceptor_winit = {
3518a06b3d6SToomas Soome tcp_tpi_accept, NULL, NULL, NULL, NULL, &tcp_winfo
3527c478bd9Sstevel@tonic-gate };
3537c478bd9Sstevel@tonic-gate
354fc80c0dfSnordmark /* For AF_INET aka /dev/tcp */
355fc80c0dfSnordmark struct streamtab tcpinfov4 = {
356fc80c0dfSnordmark &tcp_rinitv4, &tcp_winit
357fc80c0dfSnordmark };
358fc80c0dfSnordmark
359fc80c0dfSnordmark /* For AF_INET6 aka /dev/tcp6 */
360fc80c0dfSnordmark struct streamtab tcpinfov6 = {
361fc80c0dfSnordmark &tcp_rinitv6, &tcp_winit
3627c478bd9Sstevel@tonic-gate };
3637c478bd9Sstevel@tonic-gate
3647c478bd9Sstevel@tonic-gate /*
3657c478bd9Sstevel@tonic-gate * Following assumes TPI alignment requirements stay along 32 bit
3667c478bd9Sstevel@tonic-gate * boundaries
3677c478bd9Sstevel@tonic-gate */
3687c478bd9Sstevel@tonic-gate #define ROUNDUP32(x) \
3697c478bd9Sstevel@tonic-gate (((x) + (sizeof (int32_t) - 1)) & ~(sizeof (int32_t) - 1))
3707c478bd9Sstevel@tonic-gate
3717c478bd9Sstevel@tonic-gate /* Template for response to info request. */
372721fffe3SKacheong Poon struct T_info_ack tcp_g_t_info_ack = {
3737c478bd9Sstevel@tonic-gate T_INFO_ACK, /* PRIM_type */
3747c478bd9Sstevel@tonic-gate 0, /* TSDU_size */
3757c478bd9Sstevel@tonic-gate T_INFINITE, /* ETSDU_size */
3767c478bd9Sstevel@tonic-gate T_INVALID, /* CDATA_size */
3777c478bd9Sstevel@tonic-gate T_INVALID, /* DDATA_size */
3787c478bd9Sstevel@tonic-gate sizeof (sin_t), /* ADDR_size */
3797c478bd9Sstevel@tonic-gate 0, /* OPT_size - not initialized here */
3807c478bd9Sstevel@tonic-gate TIDUSZ, /* TIDU_size */
3817c478bd9Sstevel@tonic-gate T_COTS_ORD, /* SERV_type */
3827c478bd9Sstevel@tonic-gate TCPS_IDLE, /* CURRENT_state */
3837c478bd9Sstevel@tonic-gate (XPG4_1|EXPINLINE) /* PROVIDER_flag */
3847c478bd9Sstevel@tonic-gate };
3857c478bd9Sstevel@tonic-gate
386721fffe3SKacheong Poon struct T_info_ack tcp_g_t_info_ack_v6 = {
3877c478bd9Sstevel@tonic-gate T_INFO_ACK, /* PRIM_type */
3887c478bd9Sstevel@tonic-gate 0, /* TSDU_size */
3897c478bd9Sstevel@tonic-gate T_INFINITE, /* ETSDU_size */
3907c478bd9Sstevel@tonic-gate T_INVALID, /* CDATA_size */
3917c478bd9Sstevel@tonic-gate T_INVALID, /* DDATA_size */
3927c478bd9Sstevel@tonic-gate sizeof (sin6_t), /* ADDR_size */
3937c478bd9Sstevel@tonic-gate 0, /* OPT_size - not initialized here */
3947c478bd9Sstevel@tonic-gate TIDUSZ, /* TIDU_size */
3957c478bd9Sstevel@tonic-gate T_COTS_ORD, /* SERV_type */
3967c478bd9Sstevel@tonic-gate TCPS_IDLE, /* CURRENT_state */
3977c478bd9Sstevel@tonic-gate (XPG4_1|EXPINLINE) /* PROVIDER_flag */
3987c478bd9Sstevel@tonic-gate };
3997c478bd9Sstevel@tonic-gate
4007c478bd9Sstevel@tonic-gate /*
4016e91bba0SGirish Moodalbail * TCP tunables related declarations. Definitions are in tcp_tunables.c
4027c478bd9Sstevel@tonic-gate */
4036e91bba0SGirish Moodalbail extern mod_prop_info_t tcp_propinfo_tbl[];
4046e91bba0SGirish Moodalbail extern int tcp_propinfo_count;
4057c478bd9Sstevel@tonic-gate
4067c478bd9Sstevel@tonic-gate #define IS_VMLOANED_MBLK(mp) \
4077c478bd9Sstevel@tonic-gate (((mp)->b_datap->db_struioflag & STRUIO_ZC) != 0)
4087c478bd9Sstevel@tonic-gate
4097c478bd9Sstevel@tonic-gate uint32_t do_tcpzcopy = 1; /* 0: disable, 1: enable, 2: force */
4107c478bd9Sstevel@tonic-gate
4117c478bd9Sstevel@tonic-gate /*
412f4b3ec61Sdh155122 * Forces all connections to obey the value of the tcps_maxpsz_multiplier
4137c478bd9Sstevel@tonic-gate * tunable settable via NDD. Otherwise, the per-connection behavior is
414bd670b35SErik Nordmark * determined dynamically during tcp_set_destination(), which is the default.
4157c478bd9Sstevel@tonic-gate */
4167c478bd9Sstevel@tonic-gate boolean_t tcp_static_maxpsz = B_FALSE;
4177c478bd9Sstevel@tonic-gate
4187c478bd9Sstevel@tonic-gate /*
419721fffe3SKacheong Poon * If the receive buffer size is changed, this function is called to update
420721fffe3SKacheong Poon * the upper socket layer on the new delayed receive wake up threshold.
421866ba9ddSjprakash */
42279c0745dSRao Shoaib static void
tcp_set_recv_threshold(tcp_t * tcp,uint32_t new_rcvthresh)42379c0745dSRao Shoaib tcp_set_recv_threshold(tcp_t *tcp, uint32_t new_rcvthresh)
42479c0745dSRao Shoaib {
42579c0745dSRao Shoaib uint32_t default_threshold = SOCKET_RECVHIWATER >> 3;
42679c0745dSRao Shoaib
42779c0745dSRao Shoaib if (IPCL_IS_NONSTR(tcp->tcp_connp)) {
42879c0745dSRao Shoaib conn_t *connp = tcp->tcp_connp;
42979c0745dSRao Shoaib struct sock_proto_props sopp;
43079c0745dSRao Shoaib
43179c0745dSRao Shoaib /*
43279c0745dSRao Shoaib * only increase rcvthresh upto default_threshold
43379c0745dSRao Shoaib */
43479c0745dSRao Shoaib if (new_rcvthresh > default_threshold)
43579c0745dSRao Shoaib new_rcvthresh = default_threshold;
43679c0745dSRao Shoaib
43779c0745dSRao Shoaib sopp.sopp_flags = SOCKOPT_RCVTHRESH;
43879c0745dSRao Shoaib sopp.sopp_rcvthresh = new_rcvthresh;
43979c0745dSRao Shoaib
44079c0745dSRao Shoaib (*connp->conn_upcalls->su_set_proto_props)
44179c0745dSRao Shoaib (connp->conn_upper_handle, &sopp);
44279c0745dSRao Shoaib }
44379c0745dSRao Shoaib }
444721fffe3SKacheong Poon
4457c478bd9Sstevel@tonic-gate /*
4467c478bd9Sstevel@tonic-gate * Figure out the value of window scale opton. Note that the rwnd is
4477c478bd9Sstevel@tonic-gate * ASSUMED to be rounded up to the nearest MSS before the calculation.
4487c478bd9Sstevel@tonic-gate * We cannot find the scale value and then do a round up of tcp_rwnd
4497c478bd9Sstevel@tonic-gate * because the scale value may not be correct after that.
4507c478bd9Sstevel@tonic-gate *
4517c478bd9Sstevel@tonic-gate * Set the compiler flag to make this function inline.
4527c478bd9Sstevel@tonic-gate */
453721fffe3SKacheong Poon void
tcp_set_ws_value(tcp_t * tcp)4547c478bd9Sstevel@tonic-gate tcp_set_ws_value(tcp_t *tcp)
4557c478bd9Sstevel@tonic-gate {
4567c478bd9Sstevel@tonic-gate int i;
4577c478bd9Sstevel@tonic-gate uint32_t rwnd = tcp->tcp_rwnd;
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate for (i = 0; rwnd > TCP_MAXWIN && i < TCP_MAX_WINSHIFT;
4607c478bd9Sstevel@tonic-gate i++, rwnd >>= 1)
4617c478bd9Sstevel@tonic-gate ;
4627c478bd9Sstevel@tonic-gate tcp->tcp_rcv_ws = i;
4637c478bd9Sstevel@tonic-gate }
4647c478bd9Sstevel@tonic-gate
4657c478bd9Sstevel@tonic-gate /*
466f4b3ec61Sdh155122 * Remove cached/latched IPsec references.
467f4b3ec61Sdh155122 */
468f4b3ec61Sdh155122 void
tcp_ipsec_cleanup(tcp_t * tcp)469f4b3ec61Sdh155122 tcp_ipsec_cleanup(tcp_t *tcp)
470f4b3ec61Sdh155122 {
471f4b3ec61Sdh155122 conn_t *connp = tcp->tcp_connp;
472f4b3ec61Sdh155122
473fc80c0dfSnordmark ASSERT(connp->conn_flags & IPCL_TCPCONN);
474fc80c0dfSnordmark
475f4b3ec61Sdh155122 if (connp->conn_latch != NULL) {
476bd670b35SErik Nordmark IPLATCH_REFRELE(connp->conn_latch);
477f4b3ec61Sdh155122 connp->conn_latch = NULL;
478f4b3ec61Sdh155122 }
479bd670b35SErik Nordmark if (connp->conn_latch_in_policy != NULL) {
480bd670b35SErik Nordmark IPPOL_REFRELE(connp->conn_latch_in_policy);
481bd670b35SErik Nordmark connp->conn_latch_in_policy = NULL;
482bd670b35SErik Nordmark }
483bd670b35SErik Nordmark if (connp->conn_latch_in_action != NULL) {
484bd670b35SErik Nordmark IPACT_REFRELE(connp->conn_latch_in_action);
485bd670b35SErik Nordmark connp->conn_latch_in_action = NULL;
486bd670b35SErik Nordmark }
487f4b3ec61Sdh155122 if (connp->conn_policy != NULL) {
488f4b3ec61Sdh155122 IPPH_REFRELE(connp->conn_policy, connp->conn_netstack);
489f4b3ec61Sdh155122 connp->conn_policy = NULL;
490f4b3ec61Sdh155122 }
491f4b3ec61Sdh155122 }
492f4b3ec61Sdh155122
493f4b3ec61Sdh155122 /*
494f4b3ec61Sdh155122 * Cleaup before placing on free list.
495f4b3ec61Sdh155122 * Disassociate from the netstack/tcp_stack_t since the freelist
496f4b3ec61Sdh155122 * is per squeue and not per netstack.
497f4b3ec61Sdh155122 */
4987c478bd9Sstevel@tonic-gate void
tcp_cleanup(tcp_t * tcp)4997c478bd9Sstevel@tonic-gate tcp_cleanup(tcp_t *tcp)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate mblk_t *mp;
5027c478bd9Sstevel@tonic-gate conn_t *connp = tcp->tcp_connp;
503f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
504f4b3ec61Sdh155122 netstack_t *ns = tcps->tcps_netstack;
505f7f8e53dSKacheong Poon mblk_t *tcp_rsrv_mp;
5067c478bd9Sstevel@tonic-gate
5077c478bd9Sstevel@tonic-gate tcp_bind_hash_remove(tcp);
508f4b3ec61Sdh155122
509f4b3ec61Sdh155122 /* Cleanup that which needs the netstack first */
510f4b3ec61Sdh155122 tcp_ipsec_cleanup(tcp);
511bd670b35SErik Nordmark ixa_cleanup(connp->conn_ixa);
512bd670b35SErik Nordmark
513bd670b35SErik Nordmark if (connp->conn_ht_iphc != NULL) {
514bd670b35SErik Nordmark kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
515bd670b35SErik Nordmark connp->conn_ht_iphc = NULL;
516bd670b35SErik Nordmark connp->conn_ht_iphc_allocated = 0;
517bd670b35SErik Nordmark connp->conn_ht_iphc_len = 0;
518bd670b35SErik Nordmark connp->conn_ht_ulp = NULL;
519bd670b35SErik Nordmark connp->conn_ht_ulp_len = 0;
520bd670b35SErik Nordmark tcp->tcp_ipha = NULL;
521bd670b35SErik Nordmark tcp->tcp_ip6h = NULL;
522bd670b35SErik Nordmark tcp->tcp_tcpha = NULL;
523bd670b35SErik Nordmark }
524bd670b35SErik Nordmark
525bd670b35SErik Nordmark /* We clear any IP_OPTIONS and extension headers */
526bd670b35SErik Nordmark ip_pkt_free(&connp->conn_xmit_ipp);
527f4b3ec61Sdh155122
5287c478bd9Sstevel@tonic-gate tcp_free(tcp);
5297c478bd9Sstevel@tonic-gate
5307c478bd9Sstevel@tonic-gate /*
5317c478bd9Sstevel@tonic-gate * Since we will bzero the entire structure, we need to
5327c478bd9Sstevel@tonic-gate * remove it and reinsert it in global hash list. We
5337c478bd9Sstevel@tonic-gate * know the walkers can't get to this conn because we
5347c478bd9Sstevel@tonic-gate * had set CONDEMNED flag earlier and checked reference
5357c478bd9Sstevel@tonic-gate * under conn_lock so walker won't pick it and when we
5367c478bd9Sstevel@tonic-gate * go the ipcl_globalhash_remove() below, no walker
5377c478bd9Sstevel@tonic-gate * can get to it.
5387c478bd9Sstevel@tonic-gate */
5397c478bd9Sstevel@tonic-gate ipcl_globalhash_remove(connp);
5407c478bd9Sstevel@tonic-gate
5417c478bd9Sstevel@tonic-gate /* Save some state */
5427c478bd9Sstevel@tonic-gate mp = tcp->tcp_timercache;
5437c478bd9Sstevel@tonic-gate
544f7f8e53dSKacheong Poon tcp_rsrv_mp = tcp->tcp_rsrv_mp;
5457c478bd9Sstevel@tonic-gate
546fc80c0dfSnordmark if (connp->conn_cred != NULL) {
54745916cd2Sjpk crfree(connp->conn_cred);
548fc80c0dfSnordmark connp->conn_cred = NULL;
549fc80c0dfSnordmark }
550fc80c0dfSnordmark ipcl_conn_cleanup(connp);
551fc80c0dfSnordmark connp->conn_flags = IPCL_TCPCONN;
552bd670b35SErik Nordmark
553bd670b35SErik Nordmark /*
554bd670b35SErik Nordmark * Now it is safe to decrement the reference counts.
555bd670b35SErik Nordmark * This might be the last reference on the netstack
556bd670b35SErik Nordmark * in which case it will cause the freeing of the IP Instance.
557bd670b35SErik Nordmark */
558bd670b35SErik Nordmark connp->conn_netstack = NULL;
559bd670b35SErik Nordmark connp->conn_ixa->ixa_ipst = NULL;
560bd670b35SErik Nordmark netstack_rele(ns);
561bd670b35SErik Nordmark ASSERT(tcps != NULL);
562bd670b35SErik Nordmark tcp->tcp_tcps = NULL;
563bd670b35SErik Nordmark
5647c478bd9Sstevel@tonic-gate bzero(tcp, sizeof (tcp_t));
5657c478bd9Sstevel@tonic-gate
5667c478bd9Sstevel@tonic-gate /* restore the state */
5677c478bd9Sstevel@tonic-gate tcp->tcp_timercache = mp;
5687c478bd9Sstevel@tonic-gate
569f7f8e53dSKacheong Poon tcp->tcp_rsrv_mp = tcp_rsrv_mp;
5707c478bd9Sstevel@tonic-gate
5717c478bd9Sstevel@tonic-gate tcp->tcp_connp = connp;
5727c478bd9Sstevel@tonic-gate
573fc80c0dfSnordmark ASSERT(connp->conn_tcp == tcp);
574fc80c0dfSnordmark ASSERT(connp->conn_flags & IPCL_TCPCONN);
5757c478bd9Sstevel@tonic-gate connp->conn_state_flags = CONN_INCIPIENT;
576bd670b35SErik Nordmark ASSERT(connp->conn_proto == IPPROTO_TCP);
577fc80c0dfSnordmark ASSERT(connp->conn_ref == 1);
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate
5807c478bd9Sstevel@tonic-gate /*
5817c478bd9Sstevel@tonic-gate * Adapt to the information, such as rtt and rtt_sd, provided from the
582bd670b35SErik Nordmark * DCE and IRE maintained by IP.
5837c478bd9Sstevel@tonic-gate *
5847c478bd9Sstevel@tonic-gate * Checks for multicast and broadcast destination address.
585bd670b35SErik Nordmark * Returns zero if ok; an errno on failure.
5867c478bd9Sstevel@tonic-gate *
5877c478bd9Sstevel@tonic-gate * Note that the MSS calculation here is based on the info given in
588bd670b35SErik Nordmark * the DCE and IRE. We do not do any calculation based on TCP options. They
589bd670b35SErik Nordmark * will be handled in tcp_input_data() when TCP knows which options to use.
5907c478bd9Sstevel@tonic-gate *
5917c478bd9Sstevel@tonic-gate * Note on how TCP gets its parameters for a connection.
5927c478bd9Sstevel@tonic-gate *
5937c478bd9Sstevel@tonic-gate * When a tcp_t structure is allocated, it gets all the default parameters.
594bd670b35SErik Nordmark * In tcp_set_destination(), it gets those metric parameters, like rtt, rtt_sd,
5957c478bd9Sstevel@tonic-gate * spipe, rpipe, ... from the route metrics. Route metric overrides the
596fab254e2SAruna Ramakrishna * default.
5977c478bd9Sstevel@tonic-gate *
598bd670b35SErik Nordmark * An incoming SYN with a multicast or broadcast destination address is dropped
599bd670b35SErik Nordmark * in ip_fanout_v4/v6.
6007c478bd9Sstevel@tonic-gate *
6017c478bd9Sstevel@tonic-gate * An incoming SYN with a multicast or broadcast source address is always
602bd670b35SErik Nordmark * dropped in tcp_set_destination, since IPDF_ALLOW_MCBC is not set in
603bd670b35SErik Nordmark * conn_connect.
604bd670b35SErik Nordmark * The same logic in tcp_set_destination also serves to
6057c478bd9Sstevel@tonic-gate * reject an attempt to connect to a broadcast or multicast (destination)
6067c478bd9Sstevel@tonic-gate * address.
6077c478bd9Sstevel@tonic-gate */
608721fffe3SKacheong Poon int
tcp_set_destination(tcp_t * tcp)609bd670b35SErik Nordmark tcp_set_destination(tcp_t *tcp)
6107c478bd9Sstevel@tonic-gate {
6117c478bd9Sstevel@tonic-gate uint32_t mss_max;
6127c478bd9Sstevel@tonic-gate uint32_t mss;
6137c478bd9Sstevel@tonic-gate boolean_t tcp_detached = TCP_IS_DETACHED(tcp);
6147c478bd9Sstevel@tonic-gate conn_t *connp = tcp->tcp_connp;
615f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
616bd670b35SErik Nordmark iulp_t uinfo;
617bd670b35SErik Nordmark int error;
618bd670b35SErik Nordmark uint32_t flags;
6197c478bd9Sstevel@tonic-gate
620bd670b35SErik Nordmark flags = IPDF_LSO | IPDF_ZCOPY;
62143d18f1cSpriyanka /*
622bd670b35SErik Nordmark * Make sure we have a dce for the destination to avoid dce_ident
623bd670b35SErik Nordmark * contention for connected sockets.
62443d18f1cSpriyanka */
625bd670b35SErik Nordmark flags |= IPDF_UNIQUE_DCE;
6267c478bd9Sstevel@tonic-gate
627bd670b35SErik Nordmark if (!tcps->tcps_ignore_path_mtu)
628bd670b35SErik Nordmark connp->conn_ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
6297c478bd9Sstevel@tonic-gate
630bd670b35SErik Nordmark /* Use conn_lock to satify ASSERT; tcp is already serialized */
631bd670b35SErik Nordmark mutex_enter(&connp->conn_lock);
632bd670b35SErik Nordmark error = conn_connect(connp, &uinfo, flags);
633bd670b35SErik Nordmark mutex_exit(&connp->conn_lock);
634bd670b35SErik Nordmark if (error != 0)
635bd670b35SErik Nordmark return (error);
6367c478bd9Sstevel@tonic-gate
637bd670b35SErik Nordmark error = tcp_build_hdrs(tcp);
638bd670b35SErik Nordmark if (error != 0)
639bd670b35SErik Nordmark return (error);
6407c478bd9Sstevel@tonic-gate
641bd670b35SErik Nordmark tcp->tcp_localnet = uinfo.iulp_localnet;
6427c478bd9Sstevel@tonic-gate
643bd670b35SErik Nordmark if (uinfo.iulp_rtt != 0) {
644c12492cfSSebastien Roy tcp->tcp_rtt_sa = MSEC2NSEC(uinfo.iulp_rtt);
645c12492cfSSebastien Roy tcp->tcp_rtt_sd = MSEC2NSEC(uinfo.iulp_rtt_sd);
646c12492cfSSebastien Roy tcp->tcp_rto = tcp_calculate_rto(tcp, tcps, 0);
6477c478bd9Sstevel@tonic-gate }
648bd670b35SErik Nordmark if (uinfo.iulp_ssthresh != 0)
649bd670b35SErik Nordmark tcp->tcp_cwnd_ssthresh = uinfo.iulp_ssthresh;
6507c478bd9Sstevel@tonic-gate else
6517c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
652bd670b35SErik Nordmark if (uinfo.iulp_spipe > 0) {
653bd670b35SErik Nordmark connp->conn_sndbuf = MIN(uinfo.iulp_spipe,
654f4b3ec61Sdh155122 tcps->tcps_max_buf);
655bd670b35SErik Nordmark if (tcps->tcps_snd_lowat_fraction != 0) {
656bd670b35SErik Nordmark connp->conn_sndlowat = connp->conn_sndbuf /
657f4b3ec61Sdh155122 tcps->tcps_snd_lowat_fraction;
658bd670b35SErik Nordmark }
6597c478bd9Sstevel@tonic-gate (void) tcp_maxpsz_set(tcp, B_TRUE);
6607c478bd9Sstevel@tonic-gate }
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate * Note that up till now, acceptor always inherits receive
66343d18f1cSpriyanka * window from the listener. But if there is a metrics
66443d18f1cSpriyanka * associated with a host, we should use that instead of
66543d18f1cSpriyanka * inheriting it from listener. Thus we need to pass this
66643d18f1cSpriyanka * info back to the caller.
6677c478bd9Sstevel@tonic-gate */
668bd670b35SErik Nordmark if (uinfo.iulp_rpipe > 0) {
669bd670b35SErik Nordmark tcp->tcp_rwnd = MIN(uinfo.iulp_rpipe,
670f4b3ec61Sdh155122 tcps->tcps_max_buf);
6717c478bd9Sstevel@tonic-gate }
67243d18f1cSpriyanka
673bd670b35SErik Nordmark if (uinfo.iulp_rtomax > 0) {
67443d18f1cSpriyanka tcp->tcp_second_timer_threshold =
675bd670b35SErik Nordmark uinfo.iulp_rtomax;
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate
6787c478bd9Sstevel@tonic-gate /*
67943d18f1cSpriyanka * Use the metric option settings, iulp_tstamp_ok and
68043d18f1cSpriyanka * iulp_wscale_ok, only for active open. What this means
68143d18f1cSpriyanka * is that if the other side uses timestamp or window
68243d18f1cSpriyanka * scale option, TCP will also use those options. That
68343d18f1cSpriyanka * is for passive open. If the application sets a
68443d18f1cSpriyanka * large window, window scale is enabled regardless of
68543d18f1cSpriyanka * the value in iulp_wscale_ok. This is the behavior
68643d18f1cSpriyanka * since 2.6. So we keep it.
68743d18f1cSpriyanka * The only case left in passive open processing is the
68843d18f1cSpriyanka * check for SACK.
68943d18f1cSpriyanka * For ECN, it should probably be like SACK. But the
69043d18f1cSpriyanka * current value is binary, so we treat it like the other
69143d18f1cSpriyanka * cases. The metric only controls active open.For passive
69243d18f1cSpriyanka * open, the ndd param, tcp_ecn_permitted, controls the
69343d18f1cSpriyanka * behavior.
6947c478bd9Sstevel@tonic-gate */
6957c478bd9Sstevel@tonic-gate if (!tcp_detached) {
6967c478bd9Sstevel@tonic-gate /*
69743d18f1cSpriyanka * The if check means that the following can only
69843d18f1cSpriyanka * be turned on by the metrics only IRE, but not off.
6997c478bd9Sstevel@tonic-gate */
700bd670b35SErik Nordmark if (uinfo.iulp_tstamp_ok)
7017c478bd9Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_TRUE;
702bd670b35SErik Nordmark if (uinfo.iulp_wscale_ok)
7037c478bd9Sstevel@tonic-gate tcp->tcp_snd_ws_ok = B_TRUE;
704bd670b35SErik Nordmark if (uinfo.iulp_sack == 2)
7057c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_TRUE;
706bd670b35SErik Nordmark if (uinfo.iulp_ecn_ok)
7077c478bd9Sstevel@tonic-gate tcp->tcp_ecn_ok = B_TRUE;
7087c478bd9Sstevel@tonic-gate } else {
7097c478bd9Sstevel@tonic-gate /*
7107c478bd9Sstevel@tonic-gate * Passive open.
7117c478bd9Sstevel@tonic-gate *
7127c478bd9Sstevel@tonic-gate * As above, the if check means that SACK can only be
7137c478bd9Sstevel@tonic-gate * turned on by the metric only IRE.
7147c478bd9Sstevel@tonic-gate */
715bd670b35SErik Nordmark if (uinfo.iulp_sack > 0) {
7167c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_TRUE;
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate
7207c478bd9Sstevel@tonic-gate /*
721bd670b35SErik Nordmark * XXX Note that currently, iulp_mtu can be as small as 68
7227c478bd9Sstevel@tonic-gate * because of PMTUd. So tcp_mss may go to negative if combined
7237c478bd9Sstevel@tonic-gate * length of all those options exceeds 28 bytes. But because
7247c478bd9Sstevel@tonic-gate * of the tcp_mss_min check below, we may not have a problem if
7257c478bd9Sstevel@tonic-gate * tcp_mss_min is of a reasonable value. The default is 1 so
7267c478bd9Sstevel@tonic-gate * the negative problem still exists. And the check defeats PMTUd.
7277c478bd9Sstevel@tonic-gate * In fact, if PMTUd finds that the MSS should be smaller than
7287c478bd9Sstevel@tonic-gate * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min
7297c478bd9Sstevel@tonic-gate * value.
7307c478bd9Sstevel@tonic-gate *
7317c478bd9Sstevel@tonic-gate * We do not deal with that now. All those problems related to
7327c478bd9Sstevel@tonic-gate * PMTUd will be fixed later.
7337c478bd9Sstevel@tonic-gate */
734bd670b35SErik Nordmark ASSERT(uinfo.iulp_mtu != 0);
735bd670b35SErik Nordmark mss = tcp->tcp_initial_pmtu = uinfo.iulp_mtu;
7367c478bd9Sstevel@tonic-gate
7377c478bd9Sstevel@tonic-gate /* Sanity check for MSS value. */
738bd670b35SErik Nordmark if (connp->conn_ipversion == IPV4_VERSION)
739f4b3ec61Sdh155122 mss_max = tcps->tcps_mss_max_ipv4;
7407c478bd9Sstevel@tonic-gate else
741f4b3ec61Sdh155122 mss_max = tcps->tcps_mss_max_ipv6;
7427c478bd9Sstevel@tonic-gate
7437c478bd9Sstevel@tonic-gate if (tcp->tcp_ipsec_overhead == 0)
7447c478bd9Sstevel@tonic-gate tcp->tcp_ipsec_overhead = conn_ipsec_length(connp);
7457c478bd9Sstevel@tonic-gate
7467c478bd9Sstevel@tonic-gate mss -= tcp->tcp_ipsec_overhead;
7477c478bd9Sstevel@tonic-gate
748f4b3ec61Sdh155122 if (mss < tcps->tcps_mss_min)
749f4b3ec61Sdh155122 mss = tcps->tcps_mss_min;
7507c478bd9Sstevel@tonic-gate if (mss > mss_max)
7517c478bd9Sstevel@tonic-gate mss = mss_max;
7527c478bd9Sstevel@tonic-gate
7537c478bd9Sstevel@tonic-gate /* Note that this is the maximum MSS, excluding all options. */
7547c478bd9Sstevel@tonic-gate tcp->tcp_mss = mss;
7557c478bd9Sstevel@tonic-gate
7567c478bd9Sstevel@tonic-gate /*
757bd670b35SErik Nordmark * Update the tcp connection with LSO capability.
758bd670b35SErik Nordmark */
759bd670b35SErik Nordmark tcp_update_lso(tcp, connp->conn_ixa);
760bd670b35SErik Nordmark
761bd670b35SErik Nordmark /*
7627c478bd9Sstevel@tonic-gate * Initialize the ISS here now that we have the full connection ID.
7637c478bd9Sstevel@tonic-gate * The RFC 1948 method of initial sequence number generation requires
7647c478bd9Sstevel@tonic-gate * knowledge of the full connection ID before setting the ISS.
7657c478bd9Sstevel@tonic-gate */
7667c478bd9Sstevel@tonic-gate tcp_iss_init(tcp);
7677c478bd9Sstevel@tonic-gate
768bd670b35SErik Nordmark tcp->tcp_loopback = (uinfo.iulp_loopback | uinfo.iulp_local);
7697c478bd9Sstevel@tonic-gate
7707c478bd9Sstevel@tonic-gate /*
7717c478bd9Sstevel@tonic-gate * Make sure that conn is not marked incipient
7727c478bd9Sstevel@tonic-gate * for incoming connections. A blind
7737c478bd9Sstevel@tonic-gate * removal of incipient flag is cheaper than
7747c478bd9Sstevel@tonic-gate * check and removal.
7757c478bd9Sstevel@tonic-gate */
776bd670b35SErik Nordmark mutex_enter(&connp->conn_lock);
7777c478bd9Sstevel@tonic-gate connp->conn_state_flags &= ~CONN_INCIPIENT;
7787c478bd9Sstevel@tonic-gate mutex_exit(&connp->conn_lock);
7797c478bd9Sstevel@tonic-gate return (0);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate
7827c478bd9Sstevel@tonic-gate /*
783866ba9ddSjprakash * tcp_clean_death / tcp_close_detached must not be called more than once
784866ba9ddSjprakash * on a tcp. Thus every function that potentially calls tcp_clean_death
785866ba9ddSjprakash * must check for the tcp state before calling tcp_clean_death.
786bd670b35SErik Nordmark * Eg. tcp_input_data, tcp_eager_kill, tcp_clean_death_wrapper,
787866ba9ddSjprakash * tcp_timer_handler, all check for the tcp state.
788866ba9ddSjprakash */
789866ba9ddSjprakash /* ARGSUSED */
790866ba9ddSjprakash void
tcp_clean_death_wrapper(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)791bd670b35SErik Nordmark tcp_clean_death_wrapper(void *arg, mblk_t *mp, void *arg2,
792bd670b35SErik Nordmark ip_recv_attr_t *dummy)
793866ba9ddSjprakash {
794866ba9ddSjprakash tcp_t *tcp = ((conn_t *)arg)->conn_tcp;
795866ba9ddSjprakash
796866ba9ddSjprakash freemsg(mp);
797866ba9ddSjprakash if (tcp->tcp_state > TCPS_BOUND)
798721fffe3SKacheong Poon (void) tcp_clean_death(((conn_t *)arg)->conn_tcp, ETIMEDOUT);
799866ba9ddSjprakash }
800866ba9ddSjprakash
801866ba9ddSjprakash /*
8027c478bd9Sstevel@tonic-gate * We are dying for some reason. Try to do it gracefully. (May be called
8037c478bd9Sstevel@tonic-gate * as writer.)
8047c478bd9Sstevel@tonic-gate *
8057c478bd9Sstevel@tonic-gate * Return -1 if the structure was not cleaned up (if the cleanup had to be
8067c478bd9Sstevel@tonic-gate * done by a service procedure).
8077c478bd9Sstevel@tonic-gate * TBD - Should the return value distinguish between the tcp_t being
8087c478bd9Sstevel@tonic-gate * freed and it being reinitialized?
8097c478bd9Sstevel@tonic-gate */
810721fffe3SKacheong Poon int
tcp_clean_death(tcp_t * tcp,int err)811721fffe3SKacheong Poon tcp_clean_death(tcp_t *tcp, int err)
8127c478bd9Sstevel@tonic-gate {
8137c478bd9Sstevel@tonic-gate mblk_t *mp;
8147c478bd9Sstevel@tonic-gate queue_t *q;
8150f1702c5SYu Xiangning conn_t *connp = tcp->tcp_connp;
816f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
8177c478bd9Sstevel@tonic-gate
818d0ab37afSethindra if (tcp->tcp_fused)
819d0ab37afSethindra tcp_unfuse(tcp);
820d0ab37afSethindra
8217c478bd9Sstevel@tonic-gate if (tcp->tcp_linger_tid != 0 &&
8227c478bd9Sstevel@tonic-gate TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) {
8237c478bd9Sstevel@tonic-gate tcp_stop_lingering(tcp);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate
8267c478bd9Sstevel@tonic-gate ASSERT(tcp != NULL);
827bd670b35SErik Nordmark ASSERT((connp->conn_family == AF_INET &&
828bd670b35SErik Nordmark connp->conn_ipversion == IPV4_VERSION) ||
829bd670b35SErik Nordmark (connp->conn_family == AF_INET6 &&
830bd670b35SErik Nordmark (connp->conn_ipversion == IPV4_VERSION ||
831bd670b35SErik Nordmark connp->conn_ipversion == IPV6_VERSION)));
8327c478bd9Sstevel@tonic-gate
8337c478bd9Sstevel@tonic-gate if (TCP_IS_DETACHED(tcp)) {
8347c478bd9Sstevel@tonic-gate if (tcp->tcp_hard_binding) {
8357c478bd9Sstevel@tonic-gate /*
8367c478bd9Sstevel@tonic-gate * Its an eager that we are dealing with. We close the
8377c478bd9Sstevel@tonic-gate * eager but in case a conn_ind has already gone to the
8387c478bd9Sstevel@tonic-gate * listener, let tcp_accept_finish() send a discon_ind
8397c478bd9Sstevel@tonic-gate * to the listener and drop the last reference. If the
8407c478bd9Sstevel@tonic-gate * listener doesn't even know about the eager i.e. the
8417c478bd9Sstevel@tonic-gate * conn_ind hasn't gone up, blow away the eager and drop
8427c478bd9Sstevel@tonic-gate * the last reference as well. If the conn_ind has gone
8437c478bd9Sstevel@tonic-gate * up, state should be BOUND. tcp_accept_finish
8447c478bd9Sstevel@tonic-gate * will figure out that the connection has received a
8457c478bd9Sstevel@tonic-gate * RST and will send a DISCON_IND to the application.
8467c478bd9Sstevel@tonic-gate */
8477c478bd9Sstevel@tonic-gate tcp_closei_local(tcp);
848866ba9ddSjprakash if (!tcp->tcp_tconnind_started) {
8490f1702c5SYu Xiangning CONN_DEC_REF(connp);
8507c478bd9Sstevel@tonic-gate } else {
8517c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_BOUND;
8529cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL,
8539cd928feSAlan Maguire ip_xmit_attr_t *, connp->conn_ixa,
8549cd928feSAlan Maguire void, NULL, tcp_t *, tcp, void, NULL,
855eb239265SAnders Persson int32_t, TCPS_CLOSED);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate } else {
8587c478bd9Sstevel@tonic-gate tcp_close_detached(tcp);
8597c478bd9Sstevel@tonic-gate }
8607c478bd9Sstevel@tonic-gate return (0);
8617c478bd9Sstevel@tonic-gate }
8627c478bd9Sstevel@tonic-gate
863f4b3ec61Sdh155122 TCP_STAT(tcps, tcp_clean_death_nondetached);
8647c478bd9Sstevel@tonic-gate
86593fcb0b9SKacheong Poon /*
86693fcb0b9SKacheong Poon * The connection is dead. Decrement listener connection counter if
86793fcb0b9SKacheong Poon * necessary.
86893fcb0b9SKacheong Poon */
86993fcb0b9SKacheong Poon if (tcp->tcp_listen_cnt != NULL)
87093fcb0b9SKacheong Poon TCP_DECR_LISTEN_CNT(tcp);
87193fcb0b9SKacheong Poon
872721fffe3SKacheong Poon /*
873721fffe3SKacheong Poon * When a connection is moved to TIME_WAIT state, the connection
874721fffe3SKacheong Poon * counter is already decremented. So no need to decrement here
875721fffe3SKacheong Poon * again. See SET_TIME_WAIT() macro.
876721fffe3SKacheong Poon */
877721fffe3SKacheong Poon if (tcp->tcp_state >= TCPS_ESTABLISHED &&
878721fffe3SKacheong Poon tcp->tcp_state < TCPS_TIME_WAIT) {
879721fffe3SKacheong Poon TCPS_CONN_DEC(tcps);
880721fffe3SKacheong Poon }
881721fffe3SKacheong Poon
882bd670b35SErik Nordmark q = connp->conn_rq;
8837c478bd9Sstevel@tonic-gate
8847c478bd9Sstevel@tonic-gate /* Trash all inbound data */
8850f1702c5SYu Xiangning if (!IPCL_IS_NONSTR(connp)) {
8860f1702c5SYu Xiangning ASSERT(q != NULL);
8877c478bd9Sstevel@tonic-gate flushq(q, FLUSHALL);
8880f1702c5SYu Xiangning }
8897c478bd9Sstevel@tonic-gate
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate * If we are at least part way open and there is error
8927c478bd9Sstevel@tonic-gate * (err==0 implies no error)
8937c478bd9Sstevel@tonic-gate * notify our client by a T_DISCON_IND.
8947c478bd9Sstevel@tonic-gate */
8957c478bd9Sstevel@tonic-gate if ((tcp->tcp_state >= TCPS_SYN_SENT) && err) {
8967c478bd9Sstevel@tonic-gate if (tcp->tcp_state >= TCPS_ESTABLISHED &&
8977c478bd9Sstevel@tonic-gate !TCP_IS_SOCKET(tcp)) {
8987c478bd9Sstevel@tonic-gate /*
8997c478bd9Sstevel@tonic-gate * Send M_FLUSH according to TPI. Because sockets will
9007c478bd9Sstevel@tonic-gate * (and must) ignore FLUSHR we do that only for TPI
9017c478bd9Sstevel@tonic-gate * endpoints and sockets in STREAMS mode.
9027c478bd9Sstevel@tonic-gate */
9037c478bd9Sstevel@tonic-gate (void) putnextctl1(q, M_FLUSH, FLUSHR);
9047c478bd9Sstevel@tonic-gate }
905bd670b35SErik Nordmark if (connp->conn_debug) {
906ff550d0eSmasputra (void) strlog(TCP_MOD_ID, 0, 1, SL_TRACE|SL_ERROR,
9077c478bd9Sstevel@tonic-gate "tcp_clean_death: discon err %d", err);
9087c478bd9Sstevel@tonic-gate }
9090f1702c5SYu Xiangning if (IPCL_IS_NONSTR(connp)) {
9100f1702c5SYu Xiangning /* Direct socket, use upcall */
9110f1702c5SYu Xiangning (*connp->conn_upcalls->su_disconnected)(
9120f1702c5SYu Xiangning connp->conn_upper_handle, tcp->tcp_connid, err);
9130f1702c5SYu Xiangning } else {
9147c478bd9Sstevel@tonic-gate mp = mi_tpi_discon_ind(NULL, err, 0);
9157c478bd9Sstevel@tonic-gate if (mp != NULL) {
9167c478bd9Sstevel@tonic-gate putnext(q, mp);
9177c478bd9Sstevel@tonic-gate } else {
918bd670b35SErik Nordmark if (connp->conn_debug) {
919ff550d0eSmasputra (void) strlog(TCP_MOD_ID, 0, 1,
9207c478bd9Sstevel@tonic-gate SL_ERROR|SL_TRACE,
9217c478bd9Sstevel@tonic-gate "tcp_clean_death, sending M_ERROR");
9227c478bd9Sstevel@tonic-gate }
9237c478bd9Sstevel@tonic-gate (void) putnextctl1(q, M_ERROR, EPROTO);
9247c478bd9Sstevel@tonic-gate }
9250f1702c5SYu Xiangning }
9267c478bd9Sstevel@tonic-gate if (tcp->tcp_state <= TCPS_SYN_RCVD) {
9277c478bd9Sstevel@tonic-gate /* SYN_SENT or SYN_RCVD */
928721fffe3SKacheong Poon TCPS_BUMP_MIB(tcps, tcpAttemptFails);
9297c478bd9Sstevel@tonic-gate } else if (tcp->tcp_state <= TCPS_CLOSE_WAIT) {
9307c478bd9Sstevel@tonic-gate /* ESTABLISHED or CLOSE_WAIT */
931721fffe3SKacheong Poon TCPS_BUMP_MIB(tcps, tcpEstabResets);
9327c478bd9Sstevel@tonic-gate }
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate
9353e95bd4aSAnders Persson /*
9363e95bd4aSAnders Persson * ESTABLISHED non-STREAMS eagers are not 'detached' because
9373e95bd4aSAnders Persson * an upper handle is obtained when the SYN-ACK comes in. So it
9383e95bd4aSAnders Persson * should receive the 'disconnected' upcall, but tcp_reinit should
9393e95bd4aSAnders Persson * not be called since this is an eager.
9403e95bd4aSAnders Persson */
9413e95bd4aSAnders Persson if (tcp->tcp_listener != NULL && IPCL_IS_NONSTR(connp)) {
9423e95bd4aSAnders Persson tcp_closei_local(tcp);
9433e95bd4aSAnders Persson tcp->tcp_state = TCPS_BOUND;
944eb239265SAnders Persson DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
945eb239265SAnders Persson connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
946eb239265SAnders Persson int32_t, TCPS_CLOSED);
9473e95bd4aSAnders Persson return (0);
9483e95bd4aSAnders Persson }
9493e95bd4aSAnders Persson
9507c478bd9Sstevel@tonic-gate tcp_reinit(tcp);
9510f1702c5SYu Xiangning if (IPCL_IS_NONSTR(connp))
9520f1702c5SYu Xiangning (void) tcp_do_unbind(connp);
9530f1702c5SYu Xiangning
9547c478bd9Sstevel@tonic-gate return (-1);
9557c478bd9Sstevel@tonic-gate }
9567c478bd9Sstevel@tonic-gate
9577c478bd9Sstevel@tonic-gate /*
9587c478bd9Sstevel@tonic-gate * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout
9597c478bd9Sstevel@tonic-gate * to expire, stop the wait and finish the close.
9607c478bd9Sstevel@tonic-gate */
961721fffe3SKacheong Poon void
tcp_stop_lingering(tcp_t * tcp)9627c478bd9Sstevel@tonic-gate tcp_stop_lingering(tcp_t *tcp)
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate clock_t delta = 0;
965f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
966bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
9677c478bd9Sstevel@tonic-gate
9687c478bd9Sstevel@tonic-gate tcp->tcp_linger_tid = 0;
9697c478bd9Sstevel@tonic-gate if (tcp->tcp_state > TCPS_LISTEN) {
9707c478bd9Sstevel@tonic-gate tcp_acceptor_hash_remove(tcp);
971e0968231Svi117747 mutex_enter(&tcp->tcp_non_sq_lock);
9727c478bd9Sstevel@tonic-gate if (tcp->tcp_flow_stopped) {
9737c478bd9Sstevel@tonic-gate tcp_clrqfull(tcp);
9747c478bd9Sstevel@tonic-gate }
975e0968231Svi117747 mutex_exit(&tcp->tcp_non_sq_lock);
9767c478bd9Sstevel@tonic-gate
9777c478bd9Sstevel@tonic-gate if (tcp->tcp_timer_tid != 0) {
9787c478bd9Sstevel@tonic-gate delta = TCP_TIMER_CANCEL(tcp, tcp->tcp_timer_tid);
9797c478bd9Sstevel@tonic-gate tcp->tcp_timer_tid = 0;
9807c478bd9Sstevel@tonic-gate }
9817c478bd9Sstevel@tonic-gate /*
9827c478bd9Sstevel@tonic-gate * Need to cancel those timers which will not be used when
983bd670b35SErik Nordmark * TCP is detached. This has to be done before the conn_wq
984bd670b35SErik Nordmark * is cleared.
9857c478bd9Sstevel@tonic-gate */
9867c478bd9Sstevel@tonic-gate tcp_timers_stop(tcp);
9877c478bd9Sstevel@tonic-gate
9887c478bd9Sstevel@tonic-gate tcp->tcp_detached = B_TRUE;
989bd670b35SErik Nordmark connp->conn_rq = NULL;
990bd670b35SErik Nordmark connp->conn_wq = NULL;
9917c478bd9Sstevel@tonic-gate
9927c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_TIME_WAIT) {
9937c478bd9Sstevel@tonic-gate tcp_time_wait_append(tcp);
994f4b3ec61Sdh155122 TCP_DBGSTAT(tcps, tcp_detach_time_wait);
9957c478bd9Sstevel@tonic-gate goto finish;
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate
9987c478bd9Sstevel@tonic-gate /*
9997c478bd9Sstevel@tonic-gate * If delta is zero the timer event wasn't executed and was
10007c478bd9Sstevel@tonic-gate * successfully canceled. In this case we need to restart it
10017c478bd9Sstevel@tonic-gate * with the minimal delta possible.
10027c478bd9Sstevel@tonic-gate */
10037c478bd9Sstevel@tonic-gate if (delta >= 0) {
10047c478bd9Sstevel@tonic-gate tcp->tcp_timer_tid = TCP_TIMER(tcp, tcp_timer,
10057c478bd9Sstevel@tonic-gate delta ? delta : 1);
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate } else {
10087c478bd9Sstevel@tonic-gate tcp_closei_local(tcp);
1009bd670b35SErik Nordmark CONN_DEC_REF(connp);
10107c478bd9Sstevel@tonic-gate }
10117c478bd9Sstevel@tonic-gate finish:
10127c478bd9Sstevel@tonic-gate tcp->tcp_detached = B_TRUE;
1013bd670b35SErik Nordmark connp->conn_rq = NULL;
1014bd670b35SErik Nordmark connp->conn_wq = NULL;
10150f1702c5SYu Xiangning
10163e95bd4aSAnders Persson /* Signal closing thread that it can complete close */
10173e95bd4aSAnders Persson mutex_enter(&tcp->tcp_closelock);
10187c478bd9Sstevel@tonic-gate tcp->tcp_closed = 1;
10197c478bd9Sstevel@tonic-gate cv_signal(&tcp->tcp_closecv);
10207c478bd9Sstevel@tonic-gate mutex_exit(&tcp->tcp_closelock);
10213e95bd4aSAnders Persson
10223e95bd4aSAnders Persson /* If we have an upper handle (socket), release it */
10233e95bd4aSAnders Persson if (IPCL_IS_NONSTR(connp)) {
10242ad53042SDan McDonald sock_upcalls_t *upcalls = connp->conn_upcalls;
10252ad53042SDan McDonald sock_upper_handle_t handle = connp->conn_upper_handle;
10262ad53042SDan McDonald
10272ad53042SDan McDonald ASSERT(upcalls != NULL);
10282ad53042SDan McDonald ASSERT(upcalls->su_closed != NULL);
10292ad53042SDan McDonald ASSERT(handle != NULL);
10302ad53042SDan McDonald /*
10312ad53042SDan McDonald * Set these to NULL first because closed() will free upper
10322ad53042SDan McDonald * structures. Acquire conn_lock because an external caller
10332ad53042SDan McDonald * like conn_get_socket_info() will upcall if these are
10342ad53042SDan McDonald * non-NULL.
10352ad53042SDan McDonald */
10362ad53042SDan McDonald mutex_enter(&connp->conn_lock);
10373e95bd4aSAnders Persson connp->conn_upper_handle = NULL;
10383e95bd4aSAnders Persson connp->conn_upcalls = NULL;
10392ad53042SDan McDonald mutex_exit(&connp->conn_lock);
10402ad53042SDan McDonald upcalls->su_closed(handle);
10413e95bd4aSAnders Persson }
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate
1044721fffe3SKacheong Poon void
tcp_close_common(conn_t * connp,int flags)10450f1702c5SYu Xiangning tcp_close_common(conn_t *connp, int flags)
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate tcp_t *tcp = connp->conn_tcp;
10487c478bd9Sstevel@tonic-gate mblk_t *mp = &tcp->tcp_closemp;
10497c478bd9Sstevel@tonic-gate boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
105059031702Speterte mblk_t *bp;
10517c478bd9Sstevel@tonic-gate
10527c478bd9Sstevel@tonic-gate ASSERT(connp->conn_ref >= 2);
10537c478bd9Sstevel@tonic-gate
10547c478bd9Sstevel@tonic-gate /*
1055bd670b35SErik Nordmark * Mark the conn as closing. ipsq_pending_mp_add will not
10567c478bd9Sstevel@tonic-gate * add any mp to the pending mp list, after this conn has
1057bd670b35SErik Nordmark * started closing.
10587c478bd9Sstevel@tonic-gate */
10597c478bd9Sstevel@tonic-gate mutex_enter(&connp->conn_lock);
10607c478bd9Sstevel@tonic-gate connp->conn_state_flags |= CONN_CLOSING;
10617c478bd9Sstevel@tonic-gate if (connp->conn_oper_pending_ill != NULL)
10627c478bd9Sstevel@tonic-gate conn_ioctl_cleanup_reqd = B_TRUE;
10637c478bd9Sstevel@tonic-gate CONN_INC_REF_LOCKED(connp);
10647c478bd9Sstevel@tonic-gate mutex_exit(&connp->conn_lock);
10657c478bd9Sstevel@tonic-gate tcp->tcp_closeflags = (uint8_t)flags;
10667c478bd9Sstevel@tonic-gate ASSERT(connp->conn_ref >= 3);
10677c478bd9Sstevel@tonic-gate
1068866ba9ddSjprakash /*
1069866ba9ddSjprakash * tcp_closemp_used is used below without any protection of a lock
1070866ba9ddSjprakash * as we don't expect any one else to use it concurrently at this
10710163a147Sjprakash * point otherwise it would be a major defect.
1072866ba9ddSjprakash */
1073866ba9ddSjprakash
10740163a147Sjprakash if (mp->b_prev == NULL)
10750163a147Sjprakash tcp->tcp_closemp_used = B_TRUE;
10760163a147Sjprakash else
10770163a147Sjprakash cmn_err(CE_PANIC, "tcp_close: concurrent use of tcp_closemp: "
10780163a147Sjprakash "connp %p tcp %p\n", (void *)connp, (void *)tcp);
1079866ba9ddSjprakash
1080866ba9ddSjprakash TCP_DEBUG_GETPCSTACK(tcp->tcmp_stk, 15);
1081866ba9ddSjprakash
1082838a4ffaSBrian Ruthven /*
1083838a4ffaSBrian Ruthven * Cleanup any queued ioctls here. This must be done before the wq/rq
1084838a4ffaSBrian Ruthven * are re-written by tcp_close_output().
1085838a4ffaSBrian Ruthven */
1086838a4ffaSBrian Ruthven if (conn_ioctl_cleanup_reqd)
1087838a4ffaSBrian Ruthven conn_ioctl_cleanup(connp);
1088838a4ffaSBrian Ruthven
1089838a4ffaSBrian Ruthven /*
1090838a4ffaSBrian Ruthven * As CONN_CLOSING is set, no further ioctls should be passed down to
1091838a4ffaSBrian Ruthven * IP for this conn (see the guards in tcp_ioctl, tcp_wput_ioctl and
1092838a4ffaSBrian Ruthven * tcp_wput_iocdata). If the ioctl was queued on an ipsq,
1093838a4ffaSBrian Ruthven * conn_ioctl_cleanup should have found it and removed it. If the ioctl
1094838a4ffaSBrian Ruthven * was still in flight at the time, we wait for it here. See comments
1095838a4ffaSBrian Ruthven * for CONN_INC_IOCTLREF in ip.h for details.
1096838a4ffaSBrian Ruthven */
1097838a4ffaSBrian Ruthven mutex_enter(&connp->conn_lock);
1098838a4ffaSBrian Ruthven while (connp->conn_ioctlref > 0)
1099838a4ffaSBrian Ruthven cv_wait(&connp->conn_cv, &connp->conn_lock);
1100838a4ffaSBrian Ruthven ASSERT(connp->conn_ioctlref == 0);
1101838a4ffaSBrian Ruthven ASSERT(connp->conn_oper_pending_ill == NULL);
1102838a4ffaSBrian Ruthven mutex_exit(&connp->conn_lock);
1103838a4ffaSBrian Ruthven
1104da14cebeSEric Cheng SQUEUE_ENTER_ONE(connp->conn_sqp, mp, tcp_close_output, connp,
1105bd670b35SErik Nordmark NULL, tcp_squeue_flag, SQTAG_IP_TCP_CLOSE);
11067c478bd9Sstevel@tonic-gate
11073e95bd4aSAnders Persson /*
11083e95bd4aSAnders Persson * For non-STREAMS sockets, the normal case is that the conn makes
11093e95bd4aSAnders Persson * an upcall when it's finally closed, so there is no need to wait
11103e95bd4aSAnders Persson * in the protocol. But in case of SO_LINGER the thread sleeps here
11113e95bd4aSAnders Persson * so it can properly deal with the thread being interrupted.
11123e95bd4aSAnders Persson */
11133e95bd4aSAnders Persson if (IPCL_IS_NONSTR(connp) && connp->conn_linger == 0)
11143e95bd4aSAnders Persson goto nowait;
11153e95bd4aSAnders Persson
11167c478bd9Sstevel@tonic-gate mutex_enter(&tcp->tcp_closelock);
111759031702Speterte while (!tcp->tcp_closed) {
111859031702Speterte if (!cv_wait_sig(&tcp->tcp_closecv, &tcp->tcp_closelock)) {
111959031702Speterte /*
11206a43307cSja97890 * The cv_wait_sig() was interrupted. We now do the
11216a43307cSja97890 * following:
11226a43307cSja97890 *
11236a43307cSja97890 * 1) If the endpoint was lingering, we allow this
11246a43307cSja97890 * to be interrupted by cancelling the linger timeout
11256a43307cSja97890 * and closing normally.
11266a43307cSja97890 *
11276a43307cSja97890 * 2) Revert to calling cv_wait()
11286a43307cSja97890 *
11296a43307cSja97890 * We revert to using cv_wait() to avoid an
11306a43307cSja97890 * infinite loop which can occur if the calling
11316a43307cSja97890 * thread is higher priority than the squeue worker
11326a43307cSja97890 * thread and is bound to the same cpu.
113359031702Speterte */
1134bd670b35SErik Nordmark if (connp->conn_linger && connp->conn_lingertime > 0) {
11357c478bd9Sstevel@tonic-gate mutex_exit(&tcp->tcp_closelock);
113659031702Speterte /* Entering squeue, bump ref count. */
113759031702Speterte CONN_INC_REF(connp);
113859031702Speterte bp = allocb_wait(0, BPRI_HI, STR_NOSIG, NULL);
1139da14cebeSEric Cheng SQUEUE_ENTER_ONE(connp->conn_sqp, bp,
1140bd670b35SErik Nordmark tcp_linger_interrupted, connp, NULL,
1141da14cebeSEric Cheng tcp_squeue_flag, SQTAG_IP_TCP_CLOSE);
114259031702Speterte mutex_enter(&tcp->tcp_closelock);
114359031702Speterte }
11446a43307cSja97890 break;
114559031702Speterte }
114659031702Speterte }
11476a43307cSja97890 while (!tcp->tcp_closed)
11486a43307cSja97890 cv_wait(&tcp->tcp_closecv, &tcp->tcp_closelock);
114959031702Speterte mutex_exit(&tcp->tcp_closelock);
115059031702Speterte
11517c478bd9Sstevel@tonic-gate /*
11527c478bd9Sstevel@tonic-gate * In the case of listener streams that have eagers in the q or q0
1153bd670b35SErik Nordmark * we wait for the eagers to drop their reference to us. conn_rq and
1154bd670b35SErik Nordmark * conn_wq of the eagers point to our queues. By waiting for the
11557c478bd9Sstevel@tonic-gate * refcnt to drop to 1, we are sure that the eagers have cleaned
11567c478bd9Sstevel@tonic-gate * up their queue pointers and also dropped their references to us.
11573e95bd4aSAnders Persson *
11583e95bd4aSAnders Persson * For non-STREAMS sockets we do not have to wait here; the
11593e95bd4aSAnders Persson * listener will instead make a su_closed upcall when the last
11603e95bd4aSAnders Persson * reference is dropped.
11617c478bd9Sstevel@tonic-gate */
11623e95bd4aSAnders Persson if (tcp->tcp_wait_for_eagers && !IPCL_IS_NONSTR(connp)) {
11637c478bd9Sstevel@tonic-gate mutex_enter(&connp->conn_lock);
11647c478bd9Sstevel@tonic-gate while (connp->conn_ref != 1) {
11657c478bd9Sstevel@tonic-gate cv_wait(&connp->conn_cv, &connp->conn_lock);
11667c478bd9Sstevel@tonic-gate }
11677c478bd9Sstevel@tonic-gate mutex_exit(&connp->conn_lock);
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate
11703e95bd4aSAnders Persson nowait:
1171bd670b35SErik Nordmark connp->conn_cpid = NOPID;
11720f1702c5SYu Xiangning }
11730f1702c5SYu Xiangning
117459031702Speterte /*
117559031702Speterte * Called by tcp_close() routine via squeue when lingering is
117659031702Speterte * interrupted by a signal.
117759031702Speterte */
117859031702Speterte
117959031702Speterte /* ARGSUSED */
118059031702Speterte static void
tcp_linger_interrupted(void * arg,mblk_t * mp,void * arg2,ip_recv_attr_t * dummy)1181bd670b35SErik Nordmark tcp_linger_interrupted(void *arg, mblk_t *mp, void *arg2, ip_recv_attr_t *dummy)
118259031702Speterte {
118359031702Speterte conn_t *connp = (conn_t *)arg;
118459031702Speterte tcp_t *tcp = connp->conn_tcp;
118559031702Speterte
118659031702Speterte freeb(mp);
118759031702Speterte if (tcp->tcp_linger_tid != 0 &&
118859031702Speterte TCP_TIMER_CANCEL(tcp, tcp->tcp_linger_tid) >= 0) {
118959031702Speterte tcp_stop_lingering(tcp);
119059031702Speterte tcp->tcp_client_errno = EINTR;
119159031702Speterte }
119259031702Speterte }
11937c478bd9Sstevel@tonic-gate
11947c478bd9Sstevel@tonic-gate /*
11957c478bd9Sstevel@tonic-gate * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp.
11967c478bd9Sstevel@tonic-gate * Some stream heads get upset if they see these later on as anything but NULL.
11977c478bd9Sstevel@tonic-gate */
1198721fffe3SKacheong Poon void
tcp_close_mpp(mblk_t ** mpp)11997c478bd9Sstevel@tonic-gate tcp_close_mpp(mblk_t **mpp)
12007c478bd9Sstevel@tonic-gate {
12017c478bd9Sstevel@tonic-gate mblk_t *mp;
12027c478bd9Sstevel@tonic-gate
12037c478bd9Sstevel@tonic-gate if ((mp = *mpp) != NULL) {
12047c478bd9Sstevel@tonic-gate do {
12057c478bd9Sstevel@tonic-gate mp->b_next = NULL;
12067c478bd9Sstevel@tonic-gate mp->b_prev = NULL;
12077c478bd9Sstevel@tonic-gate } while ((mp = mp->b_cont) != NULL);
12087c478bd9Sstevel@tonic-gate
12097c478bd9Sstevel@tonic-gate mp = *mpp;
12107c478bd9Sstevel@tonic-gate *mpp = NULL;
12117c478bd9Sstevel@tonic-gate freemsg(mp);
12127c478bd9Sstevel@tonic-gate }
12137c478bd9Sstevel@tonic-gate }
12147c478bd9Sstevel@tonic-gate
12157c478bd9Sstevel@tonic-gate /* Do detached close. */
1216721fffe3SKacheong Poon void
tcp_close_detached(tcp_t * tcp)12177c478bd9Sstevel@tonic-gate tcp_close_detached(tcp_t *tcp)
12187c478bd9Sstevel@tonic-gate {
12197c478bd9Sstevel@tonic-gate if (tcp->tcp_fused)
12207c478bd9Sstevel@tonic-gate tcp_unfuse(tcp);
12217c478bd9Sstevel@tonic-gate
12227c478bd9Sstevel@tonic-gate /*
12237c478bd9Sstevel@tonic-gate * Clustering code serializes TCP disconnect callbacks and
12247c478bd9Sstevel@tonic-gate * cluster tcp list walks by blocking a TCP disconnect callback
12257c478bd9Sstevel@tonic-gate * if a cluster tcp list walk is in progress. This ensures
12267c478bd9Sstevel@tonic-gate * accurate accounting of TCPs in the cluster code even though
12277c478bd9Sstevel@tonic-gate * the TCP list walk itself is not atomic.
12287c478bd9Sstevel@tonic-gate */
12297c478bd9Sstevel@tonic-gate tcp_closei_local(tcp);
12307c478bd9Sstevel@tonic-gate CONN_DEC_REF(tcp->tcp_connp);
12317c478bd9Sstevel@tonic-gate }
12327c478bd9Sstevel@tonic-gate
12337c478bd9Sstevel@tonic-gate /*
12347c478bd9Sstevel@tonic-gate * The tcp_t is going away. Remove it from all lists and set it
12357c478bd9Sstevel@tonic-gate * to TCPS_CLOSED. The freeing up of memory is deferred until
12367c478bd9Sstevel@tonic-gate * tcp_inactive. This is needed since a thread in tcp_rput might have
12377c478bd9Sstevel@tonic-gate * done a CONN_INC_REF on this structure before it was removed from the
12387c478bd9Sstevel@tonic-gate * hashes.
12397c478bd9Sstevel@tonic-gate */
1240721fffe3SKacheong Poon void
tcp_closei_local(tcp_t * tcp)12417c478bd9Sstevel@tonic-gate tcp_closei_local(tcp_t *tcp)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate conn_t *connp = tcp->tcp_connp;
1244f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
12459cd928feSAlan Maguire int32_t oldstate;
12467c478bd9Sstevel@tonic-gate
12477c478bd9Sstevel@tonic-gate if (!TCP_IS_SOCKET(tcp))
12487c478bd9Sstevel@tonic-gate tcp_acceptor_hash_remove(tcp);
12497c478bd9Sstevel@tonic-gate
12507c478bd9Sstevel@tonic-gate /*
1251721fffe3SKacheong Poon * This can be called via tcp_time_wait_processing() if TCP gets a
1252721fffe3SKacheong Poon * SYN with sequence number outside the TIME-WAIT connection's
1253721fffe3SKacheong Poon * window. So we need to check for TIME-WAIT state here as the
1254721fffe3SKacheong Poon * connection counter is already decremented. See SET_TIME_WAIT()
1255721fffe3SKacheong Poon * macro
1256721fffe3SKacheong Poon */
1257721fffe3SKacheong Poon if (tcp->tcp_state >= TCPS_ESTABLISHED &&
1258721fffe3SKacheong Poon tcp->tcp_state < TCPS_TIME_WAIT) {
1259721fffe3SKacheong Poon TCPS_CONN_DEC(tcps);
1260721fffe3SKacheong Poon }
1261721fffe3SKacheong Poon
1262721fffe3SKacheong Poon /*
12637c478bd9Sstevel@tonic-gate * If we are an eager connection hanging off a listener that
126448bbca81SDaniel Hoffman * hasn't formally accepted the connection yet, get off its
12657c478bd9Sstevel@tonic-gate * list and blow off any data that we have accumulated.
12667c478bd9Sstevel@tonic-gate */
12677c478bd9Sstevel@tonic-gate if (tcp->tcp_listener != NULL) {
12687c478bd9Sstevel@tonic-gate tcp_t *listener = tcp->tcp_listener;
12697c478bd9Sstevel@tonic-gate mutex_enter(&listener->tcp_eager_lock);
12707c478bd9Sstevel@tonic-gate /*
1271866ba9ddSjprakash * tcp_tconnind_started == B_TRUE means that the
12727c478bd9Sstevel@tonic-gate * conn_ind has already gone to listener. At
12737c478bd9Sstevel@tonic-gate * this point, eager will be closed but we
12747c478bd9Sstevel@tonic-gate * leave it in listeners eager list so that
12757c478bd9Sstevel@tonic-gate * if listener decides to close without doing
1276bd670b35SErik Nordmark * accept, we can clean this up. In tcp_tli_accept
1277866ba9ddSjprakash * we take care of the case of accept on closed
12787c478bd9Sstevel@tonic-gate * eager.
12797c478bd9Sstevel@tonic-gate */
1280866ba9ddSjprakash if (!tcp->tcp_tconnind_started) {
12817c478bd9Sstevel@tonic-gate tcp_eager_unlink(tcp);
12827c478bd9Sstevel@tonic-gate mutex_exit(&listener->tcp_eager_lock);
12837c478bd9Sstevel@tonic-gate /*
12847c478bd9Sstevel@tonic-gate * We don't want to have any pointers to the
12857c478bd9Sstevel@tonic-gate * listener queue, after we have released our
12867c478bd9Sstevel@tonic-gate * reference on the listener
12877c478bd9Sstevel@tonic-gate */
1288bd670b35SErik Nordmark ASSERT(tcp->tcp_detached);
1289bd670b35SErik Nordmark connp->conn_rq = NULL;
1290bd670b35SErik Nordmark connp->conn_wq = NULL;
12917c478bd9Sstevel@tonic-gate CONN_DEC_REF(listener->tcp_connp);
12927c478bd9Sstevel@tonic-gate } else {
12937c478bd9Sstevel@tonic-gate mutex_exit(&listener->tcp_eager_lock);
12947c478bd9Sstevel@tonic-gate }
12957c478bd9Sstevel@tonic-gate }
12967c478bd9Sstevel@tonic-gate
12977c478bd9Sstevel@tonic-gate /* Stop all the timers */
12987c478bd9Sstevel@tonic-gate tcp_timers_stop(tcp);
12997c478bd9Sstevel@tonic-gate
13007c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_LISTEN) {
13017c478bd9Sstevel@tonic-gate if (tcp->tcp_ip_addr_cache) {
13027c478bd9Sstevel@tonic-gate kmem_free((void *)tcp->tcp_ip_addr_cache,
13037c478bd9Sstevel@tonic-gate IP_ADDR_CACHE_SIZE * sizeof (ipaddr_t));
13047c478bd9Sstevel@tonic-gate tcp->tcp_ip_addr_cache = NULL;
13057c478bd9Sstevel@tonic-gate }
13067c478bd9Sstevel@tonic-gate }
130793fcb0b9SKacheong Poon
130893fcb0b9SKacheong Poon /* Decrement listerner connection counter if necessary. */
130993fcb0b9SKacheong Poon if (tcp->tcp_listen_cnt != NULL)
131093fcb0b9SKacheong Poon TCP_DECR_LISTEN_CNT(tcp);
131193fcb0b9SKacheong Poon
1312e0968231Svi117747 mutex_enter(&tcp->tcp_non_sq_lock);
13137c478bd9Sstevel@tonic-gate if (tcp->tcp_flow_stopped)
13147c478bd9Sstevel@tonic-gate tcp_clrqfull(tcp);
1315e0968231Svi117747 mutex_exit(&tcp->tcp_non_sq_lock);
13167c478bd9Sstevel@tonic-gate
13177c478bd9Sstevel@tonic-gate tcp_bind_hash_remove(tcp);
13187c478bd9Sstevel@tonic-gate /*
13197c478bd9Sstevel@tonic-gate * If the tcp_time_wait_collector (which runs outside the squeue)
13207c478bd9Sstevel@tonic-gate * is trying to remove this tcp from the time wait list, we will
13217c478bd9Sstevel@tonic-gate * block in tcp_time_wait_remove while trying to acquire the
13227c478bd9Sstevel@tonic-gate * tcp_time_wait_lock. The logic in tcp_time_wait_collector also
13237c478bd9Sstevel@tonic-gate * requires the ipcl_hash_remove to be ordered after the
13247c478bd9Sstevel@tonic-gate * tcp_time_wait_remove for the refcnt checks to work correctly.
13257c478bd9Sstevel@tonic-gate */
13267c478bd9Sstevel@tonic-gate if (tcp->tcp_state == TCPS_TIME_WAIT)
1327866ba9ddSjprakash (void) tcp_time_wait_remove(tcp, NULL);
1328bd670b35SErik Nordmark CL_INET_DISCONNECT(connp);
13297c478bd9Sstevel@tonic-gate ipcl_hash_remove(connp);
13309cd928feSAlan Maguire oldstate = tcp->tcp_state;
13319cd928feSAlan Maguire tcp->tcp_state = TCPS_CLOSED;
13329cd928feSAlan Maguire /* Need to probe before ixa_cleanup() is called */
13339cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
13349cd928feSAlan Maguire connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
13359cd928feSAlan Maguire int32_t, oldstate);
1336bd670b35SErik Nordmark ixa_cleanup(connp->conn_ixa);
13377c478bd9Sstevel@tonic-gate
13387c478bd9Sstevel@tonic-gate /*
1339bd670b35SErik Nordmark * Mark the conn as CONDEMNED
13407c478bd9Sstevel@tonic-gate */
13417c478bd9Sstevel@tonic-gate mutex_enter(&connp->conn_lock);
13427c478bd9Sstevel@tonic-gate connp->conn_state_flags |= CONN_CONDEMNED;
13437c478bd9Sstevel@tonic-gate mutex_exit(&connp->conn_lock);
13447c478bd9Sstevel@tonic-gate
13457c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_next == NULL);
13467c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_prev == NULL);
13477c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_expire == 0);
1348c28749e9Skais
1349f4b3ec61Sdh155122 tcp_ipsec_cleanup(tcp);
13507c478bd9Sstevel@tonic-gate }
13517c478bd9Sstevel@tonic-gate
13527c478bd9Sstevel@tonic-gate /*
13537c478bd9Sstevel@tonic-gate * tcp is dying (called from ipcl_conn_destroy and error cases).
13547c478bd9Sstevel@tonic-gate * Free the tcp_t in either case.
13557c478bd9Sstevel@tonic-gate */
13567c478bd9Sstevel@tonic-gate void
tcp_free(tcp_t * tcp)13577c478bd9Sstevel@tonic-gate tcp_free(tcp_t *tcp)
13587c478bd9Sstevel@tonic-gate {
13597c478bd9Sstevel@tonic-gate mblk_t *mp;
1360bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
13617c478bd9Sstevel@tonic-gate
13627c478bd9Sstevel@tonic-gate ASSERT(tcp != NULL);
13637c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_ptpahn == NULL && tcp->tcp_acceptor_hash == NULL);
13647c478bd9Sstevel@tonic-gate
1365bd670b35SErik Nordmark connp->conn_rq = NULL;
1366bd670b35SErik Nordmark connp->conn_wq = NULL;
13677c478bd9Sstevel@tonic-gate
13687c478bd9Sstevel@tonic-gate tcp_close_mpp(&tcp->tcp_xmit_head);
13697c478bd9Sstevel@tonic-gate tcp_close_mpp(&tcp->tcp_reass_head);
13707c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL) {
13717c478bd9Sstevel@tonic-gate /* Free b_next chain */
13727c478bd9Sstevel@tonic-gate tcp_close_mpp(&tcp->tcp_rcv_list);
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate if ((mp = tcp->tcp_urp_mp) != NULL) {
13757c478bd9Sstevel@tonic-gate freemsg(mp);
13767c478bd9Sstevel@tonic-gate }
13777c478bd9Sstevel@tonic-gate if ((mp = tcp->tcp_urp_mark_mp) != NULL) {
13787c478bd9Sstevel@tonic-gate freemsg(mp);
13797c478bd9Sstevel@tonic-gate }
13807c478bd9Sstevel@tonic-gate
13817c478bd9Sstevel@tonic-gate if (tcp->tcp_fused_sigurg_mp != NULL) {
13820f1702c5SYu Xiangning ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
13837c478bd9Sstevel@tonic-gate freeb(tcp->tcp_fused_sigurg_mp);
13847c478bd9Sstevel@tonic-gate tcp->tcp_fused_sigurg_mp = NULL;
13857c478bd9Sstevel@tonic-gate }
13867c478bd9Sstevel@tonic-gate
1387a6fd05c9SKacheong Poon if (tcp->tcp_ordrel_mp != NULL) {
13880f1702c5SYu Xiangning ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1389a6fd05c9SKacheong Poon freeb(tcp->tcp_ordrel_mp);
1390a6fd05c9SKacheong Poon tcp->tcp_ordrel_mp = NULL;
1391a6fd05c9SKacheong Poon }
1392a6fd05c9SKacheong Poon
139366cd0f60SKacheong Poon TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
139466cd0f60SKacheong Poon bzero(&tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
13957c478bd9Sstevel@tonic-gate
13967c478bd9Sstevel@tonic-gate if (tcp->tcp_hopopts != NULL) {
13977c478bd9Sstevel@tonic-gate mi_free(tcp->tcp_hopopts);
13987c478bd9Sstevel@tonic-gate tcp->tcp_hopopts = NULL;
13997c478bd9Sstevel@tonic-gate tcp->tcp_hopoptslen = 0;
14007c478bd9Sstevel@tonic-gate }
14017c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_hopoptslen == 0);
14027c478bd9Sstevel@tonic-gate if (tcp->tcp_dstopts != NULL) {
14037c478bd9Sstevel@tonic-gate mi_free(tcp->tcp_dstopts);
14047c478bd9Sstevel@tonic-gate tcp->tcp_dstopts = NULL;
14057c478bd9Sstevel@tonic-gate tcp->tcp_dstoptslen = 0;
14067c478bd9Sstevel@tonic-gate }
14077c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_dstoptslen == 0);
1408bd670b35SErik Nordmark if (tcp->tcp_rthdrdstopts != NULL) {
1409bd670b35SErik Nordmark mi_free(tcp->tcp_rthdrdstopts);
1410bd670b35SErik Nordmark tcp->tcp_rthdrdstopts = NULL;
1411bd670b35SErik Nordmark tcp->tcp_rthdrdstoptslen = 0;
14127c478bd9Sstevel@tonic-gate }
1413bd670b35SErik Nordmark ASSERT(tcp->tcp_rthdrdstoptslen == 0);
14147c478bd9Sstevel@tonic-gate if (tcp->tcp_rthdr != NULL) {
14157c478bd9Sstevel@tonic-gate mi_free(tcp->tcp_rthdr);
14167c478bd9Sstevel@tonic-gate tcp->tcp_rthdr = NULL;
14177c478bd9Sstevel@tonic-gate tcp->tcp_rthdrlen = 0;
14187c478bd9Sstevel@tonic-gate }
14197c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_rthdrlen == 0);
14207c478bd9Sstevel@tonic-gate
14217c478bd9Sstevel@tonic-gate /*
14227c478bd9Sstevel@tonic-gate * Following is really a blowing away a union.
14237c478bd9Sstevel@tonic-gate * It happens to have exactly two members of identical size
14247c478bd9Sstevel@tonic-gate * the following code is enough.
14257c478bd9Sstevel@tonic-gate */
14267c478bd9Sstevel@tonic-gate tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind);
14273e95bd4aSAnders Persson
1428*1edba515SAndy Fiddaman if (tcp->tcp_sig_sa_in != NULL) {
1429*1edba515SAndy Fiddaman tcpsig_sa_rele(tcp->tcp_sig_sa_in);
1430*1edba515SAndy Fiddaman tcp->tcp_sig_sa_in = NULL;
1431*1edba515SAndy Fiddaman }
1432*1edba515SAndy Fiddaman if (tcp->tcp_sig_sa_out != NULL) {
1433*1edba515SAndy Fiddaman tcpsig_sa_rele(tcp->tcp_sig_sa_out);
1434*1edba515SAndy Fiddaman tcp->tcp_sig_sa_out = NULL;
1435*1edba515SAndy Fiddaman }
1436*1edba515SAndy Fiddaman
143745a4b79dSSebastien Roy /* Allow the CC algorithm to clean up after itself. */
143845a4b79dSSebastien Roy if (tcp->tcp_cc_algo != NULL && tcp->tcp_cc_algo->cb_destroy != NULL)
143945a4b79dSSebastien Roy tcp->tcp_cc_algo->cb_destroy(&tcp->tcp_ccv);
144045a4b79dSSebastien Roy
14413e95bd4aSAnders Persson /*
14423e95bd4aSAnders Persson * If this is a non-STREAM socket still holding on to an upper
14433e95bd4aSAnders Persson * handle, release it. As a result of fallback we might also see
14443e95bd4aSAnders Persson * STREAMS based conns with upper handles, in which case there is
14453e95bd4aSAnders Persson * nothing to do other than clearing the field.
14463e95bd4aSAnders Persson */
14473e95bd4aSAnders Persson if (connp->conn_upper_handle != NULL) {
14482ad53042SDan McDonald sock_upcalls_t *upcalls = connp->conn_upcalls;
14492ad53042SDan McDonald sock_upper_handle_t handle = connp->conn_upper_handle;
14502ad53042SDan McDonald
14512ad53042SDan McDonald /*
14522ad53042SDan McDonald * Set these to NULL first because closed() will free upper
14532ad53042SDan McDonald * structures. Acquire conn_lock because an external caller
14542ad53042SDan McDonald * like conn_get_socket_info() will upcall if these are
14552ad53042SDan McDonald * non-NULL.
14562ad53042SDan McDonald */
14572ad53042SDan McDonald mutex_enter(&connp->conn_lock);
14583e95bd4aSAnders Persson connp->conn_upper_handle = NULL;
14593e95bd4aSAnders Persson connp->conn_upcalls = NULL;
14602ad53042SDan McDonald mutex_exit(&connp->conn_lock);
14612ad53042SDan McDonald if (IPCL_IS_NONSTR(connp)) {
14622ad53042SDan McDonald ASSERT(upcalls != NULL);
14632ad53042SDan McDonald ASSERT(upcalls->su_closed != NULL);
14642ad53042SDan McDonald ASSERT(handle != NULL);
14652ad53042SDan McDonald upcalls->su_closed(handle);
14662ad53042SDan McDonald tcp->tcp_detached = B_TRUE;
14672ad53042SDan McDonald }
14683e95bd4aSAnders Persson }
14697c478bd9Sstevel@tonic-gate }
14707c478bd9Sstevel@tonic-gate
14717c478bd9Sstevel@tonic-gate /*
14727c478bd9Sstevel@tonic-gate * tcp_get_conn/tcp_free_conn
14737c478bd9Sstevel@tonic-gate *
14747c478bd9Sstevel@tonic-gate * tcp_get_conn is used to get a clean tcp connection structure.
14757c478bd9Sstevel@tonic-gate * It tries to reuse the connections put on the freelist by the
14767c478bd9Sstevel@tonic-gate * time_wait_collector failing which it goes to kmem_cache. This
14777c478bd9Sstevel@tonic-gate * way has two benefits compared to just allocating from and
14787c478bd9Sstevel@tonic-gate * freeing to kmem_cache.
14797c478bd9Sstevel@tonic-gate * 1) The time_wait_collector can free (which includes the cleanup)
14807c478bd9Sstevel@tonic-gate * outside the squeue. So when the interrupt comes, we have a clean
14817c478bd9Sstevel@tonic-gate * connection sitting in the freelist. Obviously, this buys us
14827c478bd9Sstevel@tonic-gate * performance.
14837c478bd9Sstevel@tonic-gate *
1484bd670b35SErik Nordmark * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_input_listener
1485bd670b35SErik Nordmark * has multiple disadvantages - tying up the squeue during alloc.
14867c478bd9Sstevel@tonic-gate * But allocating the conn/tcp in IP land is also not the best since
14877c478bd9Sstevel@tonic-gate * we can't check the 'q' and 'q0' which are protected by squeue and
14887c478bd9Sstevel@tonic-gate * blindly allocate memory which might have to be freed here if we are
14897c478bd9Sstevel@tonic-gate * not allowed to accept the connection. By using the freelist and
14907c478bd9Sstevel@tonic-gate * putting the conn/tcp back in freelist, we don't pay a penalty for
14917c478bd9Sstevel@tonic-gate * allocating memory without checking 'q/q0' and freeing it if we can't
14927c478bd9Sstevel@tonic-gate * accept the connection.
14937c478bd9Sstevel@tonic-gate *
14947c478bd9Sstevel@tonic-gate * Care should be taken to put the conn back in the same squeue's freelist
14957c478bd9Sstevel@tonic-gate * from which it was allocated. Best results are obtained if conn is
14967c478bd9Sstevel@tonic-gate * allocated from listener's squeue and freed to the same. Time wait
14977c478bd9Sstevel@tonic-gate * collector will free up the freelist is the connection ends up sitting
14987c478bd9Sstevel@tonic-gate * there for too long.
14997c478bd9Sstevel@tonic-gate */
150045a4b79dSSebastien Roy conn_t *
tcp_get_conn(void * arg,tcp_stack_t * tcps)1501f3124163SAnders Persson tcp_get_conn(void *arg, tcp_stack_t *tcps)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate tcp_t *tcp = NULL;
15047c478bd9Sstevel@tonic-gate conn_t *connp = NULL;
15057c478bd9Sstevel@tonic-gate squeue_t *sqp = (squeue_t *)arg;
15067c478bd9Sstevel@tonic-gate tcp_squeue_priv_t *tcp_time_wait;
1507f4b3ec61Sdh155122 netstack_t *ns;
1508eead73cfSRao Shoaib mblk_t *tcp_rsrv_mp = NULL;
15097c478bd9Sstevel@tonic-gate
15107c478bd9Sstevel@tonic-gate tcp_time_wait =
15117c478bd9Sstevel@tonic-gate *((tcp_squeue_priv_t **)squeue_getprivate(sqp, SQPRIVATE_TCP));
15127c478bd9Sstevel@tonic-gate
15137c478bd9Sstevel@tonic-gate mutex_enter(&tcp_time_wait->tcp_time_wait_lock);
15147c478bd9Sstevel@tonic-gate tcp = tcp_time_wait->tcp_free_list;
15151dbf515bSethindra ASSERT((tcp != NULL) ^ (tcp_time_wait->tcp_free_list_cnt == 0));
15167c478bd9Sstevel@tonic-gate if (tcp != NULL) {
15177c478bd9Sstevel@tonic-gate tcp_time_wait->tcp_free_list = tcp->tcp_time_wait_next;
15181dbf515bSethindra tcp_time_wait->tcp_free_list_cnt--;
15197c478bd9Sstevel@tonic-gate mutex_exit(&tcp_time_wait->tcp_time_wait_lock);
15207c478bd9Sstevel@tonic-gate tcp->tcp_time_wait_next = NULL;
15217c478bd9Sstevel@tonic-gate connp = tcp->tcp_connp;
15227c478bd9Sstevel@tonic-gate connp->conn_flags |= IPCL_REUSED;
1523f4b3ec61Sdh155122
1524f4b3ec61Sdh155122 ASSERT(tcp->tcp_tcps == NULL);
1525f4b3ec61Sdh155122 ASSERT(connp->conn_netstack == NULL);
1526f3124163SAnders Persson ASSERT(tcp->tcp_rsrv_mp != NULL);
1527f4b3ec61Sdh155122 ns = tcps->tcps_netstack;
1528f4b3ec61Sdh155122 netstack_hold(ns);
1529f4b3ec61Sdh155122 connp->conn_netstack = ns;
1530bd670b35SErik Nordmark connp->conn_ixa->ixa_ipst = ns->netstack_ip;
1531f4b3ec61Sdh155122 tcp->tcp_tcps = tcps;
1532f4b3ec61Sdh155122 ipcl_globalhash_insert(connp);
1533bd670b35SErik Nordmark
1534bd670b35SErik Nordmark connp->conn_ixa->ixa_notify_cookie = tcp;
1535bd670b35SErik Nordmark ASSERT(connp->conn_ixa->ixa_notify == tcp_notify);
1536bd670b35SErik Nordmark connp->conn_recv = tcp_input_data;
1537bd670b35SErik Nordmark ASSERT(connp->conn_recvicmp == tcp_icmp_input);
1538bd670b35SErik Nordmark ASSERT(connp->conn_verifyicmp == tcp_verifyicmp);
153945a4b79dSSebastien Roy return (connp);
15407c478bd9Sstevel@tonic-gate }
15417c478bd9Sstevel@tonic-gate mutex_exit(&tcp_time_wait->tcp_time_wait_lock);
1542f7f8e53dSKacheong Poon /*
1543f3124163SAnders Persson * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until
1544f3124163SAnders Persson * this conn_t/tcp_t is freed at ipcl_conn_destroy().
1545f7f8e53dSKacheong Poon */
1546eead73cfSRao Shoaib tcp_rsrv_mp = allocb(0, BPRI_HI);
1547eead73cfSRao Shoaib if (tcp_rsrv_mp == NULL)
1548f7f8e53dSKacheong Poon return (NULL);
1549f3124163SAnders Persson
1550eead73cfSRao Shoaib if ((connp = ipcl_conn_create(IPCL_TCPCONN, KM_NOSLEEP,
1551eead73cfSRao Shoaib tcps->tcps_netstack)) == NULL) {
1552eead73cfSRao Shoaib freeb(tcp_rsrv_mp);
1553eead73cfSRao Shoaib return (NULL);
1554eead73cfSRao Shoaib }
1555aa4b59d3SKacheong Poon
1556eead73cfSRao Shoaib tcp = connp->conn_tcp;
1557eead73cfSRao Shoaib tcp->tcp_rsrv_mp = tcp_rsrv_mp;
1558f7f8e53dSKacheong Poon mutex_init(&tcp->tcp_rsrv_mp_lock, NULL, MUTEX_DEFAULT, NULL);
1559eead73cfSRao Shoaib
1560f4b3ec61Sdh155122 tcp->tcp_tcps = tcps;
1561f7f8e53dSKacheong Poon
1562bd670b35SErik Nordmark connp->conn_recv = tcp_input_data;
1563bd670b35SErik Nordmark connp->conn_recvicmp = tcp_icmp_input;
1564bd670b35SErik Nordmark connp->conn_verifyicmp = tcp_verifyicmp;
15657c478bd9Sstevel@tonic-gate
156645916cd2Sjpk /*
1567bd670b35SErik Nordmark * Register tcp_notify to listen to capability changes detected by IP.
1568bd670b35SErik Nordmark * This upcall is made in the context of the call to conn_ip_output
1569bd670b35SErik Nordmark * thus it is inside the squeue.
157045916cd2Sjpk */
1571bd670b35SErik Nordmark connp->conn_ixa->ixa_notify = tcp_notify;
1572bd670b35SErik Nordmark connp->conn_ixa->ixa_notify_cookie = tcp;
157345916cd2Sjpk
157445a4b79dSSebastien Roy return (connp);
157545916cd2Sjpk }
157645916cd2Sjpk
15777c478bd9Sstevel@tonic-gate /*
15787c478bd9Sstevel@tonic-gate * Handle connect to IPv4 destinations, including connections for AF_INET6
15797c478bd9Sstevel@tonic-gate * sockets connecting to IPv4 mapped IPv6 destinations.
1580bd670b35SErik Nordmark * Returns zero if OK, a positive errno, or a negative TLI error.
15817c478bd9Sstevel@tonic-gate */
15820f1702c5SYu Xiangning static int
tcp_connect_ipv4(tcp_t * tcp,ipaddr_t * dstaddrp,in_port_t dstport,uint_t srcid)15830f1702c5SYu Xiangning tcp_connect_ipv4(tcp_t *tcp, ipaddr_t *dstaddrp, in_port_t dstport,
1584bd670b35SErik Nordmark uint_t srcid)
15857c478bd9Sstevel@tonic-gate {
15867c478bd9Sstevel@tonic-gate ipaddr_t dstaddr = *dstaddrp;
158742274831Sgt145670 uint16_t lport;
1588bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
1589f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
1590bd670b35SErik Nordmark int error;
15917c478bd9Sstevel@tonic-gate
1592bd670b35SErik Nordmark ASSERT(connp->conn_ipversion == IPV4_VERSION);
15937c478bd9Sstevel@tonic-gate
15947c478bd9Sstevel@tonic-gate /* Check for attempt to connect to INADDR_ANY */
15957c478bd9Sstevel@tonic-gate if (dstaddr == INADDR_ANY) {
15967c478bd9Sstevel@tonic-gate /*
15977c478bd9Sstevel@tonic-gate * SunOS 4.x and 4.3 BSD allow an application
15987c478bd9Sstevel@tonic-gate * to connect a TCP socket to INADDR_ANY.
15997c478bd9Sstevel@tonic-gate * When they do this, the kernel picks the
16007c478bd9Sstevel@tonic-gate * address of one interface and uses it
16017c478bd9Sstevel@tonic-gate * instead. The kernel usually ends up
16027c478bd9Sstevel@tonic-gate * picking the address of the loopback
16037c478bd9Sstevel@tonic-gate * interface. This is an undocumented feature.
16047c478bd9Sstevel@tonic-gate * However, we provide the same thing here
16057c478bd9Sstevel@tonic-gate * in order to have source and binary
16067c478bd9Sstevel@tonic-gate * compatibility with SunOS 4.x.
16077c478bd9Sstevel@tonic-gate * Update the T_CONN_REQ (sin/sin6) since it is used to
16087c478bd9Sstevel@tonic-gate * generate the T_CONN_CON.
16097c478bd9Sstevel@tonic-gate */
16107c478bd9Sstevel@tonic-gate dstaddr = htonl(INADDR_LOOPBACK);
16117c478bd9Sstevel@tonic-gate *dstaddrp = dstaddr;
16127c478bd9Sstevel@tonic-gate }
16137c478bd9Sstevel@tonic-gate
16147c478bd9Sstevel@tonic-gate /* Handle __sin6_src_id if socket not bound to an IP address */
1615bd670b35SErik Nordmark if (srcid != 0 && connp->conn_laddr_v4 == INADDR_ANY) {
1616a1ca8b43SDan McDonald if (!ip_srcid_find_id(srcid, &connp->conn_laddr_v6,
1617a1ca8b43SDan McDonald IPCL_ZONEID(connp), B_TRUE, tcps->tcps_netstack)) {
1618a1ca8b43SDan McDonald /* Mismatch - conn_laddr_v6 would be v6 address. */
1619a1ca8b43SDan McDonald return (EADDRNOTAVAIL);
1620a1ca8b43SDan McDonald }
1621bd670b35SErik Nordmark connp->conn_saddr_v6 = connp->conn_laddr_v6;
16227c478bd9Sstevel@tonic-gate }
16237c478bd9Sstevel@tonic-gate
1624bd670b35SErik Nordmark IN6_IPADDR_TO_V4MAPPED(dstaddr, &connp->conn_faddr_v6);
1625bd670b35SErik Nordmark connp->conn_fport = dstport;
16267c478bd9Sstevel@tonic-gate
16275f9878b0Sken Powell - Sun Microsystem /*
162842274831Sgt145670 * At this point the remote destination address and remote port fields
162942274831Sgt145670 * in the tcp-four-tuple have been filled in the tcp structure. Now we
1630bd670b35SErik Nordmark * have to see which state tcp was in so we can take appropriate action.
163142274831Sgt145670 */
1632bd670b35SErik Nordmark if (tcp->tcp_state == TCPS_IDLE) {
163342274831Sgt145670 /*
163442274831Sgt145670 * We support a quick connect capability here, allowing
163542274831Sgt145670 * clients to transition directly from IDLE to SYN_SENT
163642274831Sgt145670 * tcp_bindi will pick an unused port, insert the connection
163742274831Sgt145670 * in the bind hash and transition to BOUND state.
163842274831Sgt145670 */
1639f4b3ec61Sdh155122 lport = tcp_update_next_port(tcps->tcps_next_port_to_try,
1640f4b3ec61Sdh155122 tcp, B_TRUE);
1641bd670b35SErik Nordmark lport = tcp_bindi(tcp, lport, &connp->conn_laddr_v6, 0, B_TRUE,
164242274831Sgt145670 B_FALSE, B_FALSE);
1643bd670b35SErik Nordmark if (lport == 0)
1644bd670b35SErik Nordmark return (-TNOADDR);
164542274831Sgt145670 }
16460f1702c5SYu Xiangning
1647fc80c0dfSnordmark /*
1648bd670b35SErik Nordmark * Lookup the route to determine a source address and the uinfo.
1649bd670b35SErik Nordmark * Setup TCP parameters based on the metrics/DCE.
1650fc80c0dfSnordmark */
1651bd670b35SErik Nordmark error = tcp_set_destination(tcp);
1652bd670b35SErik Nordmark if (error != 0)
16530f1702c5SYu Xiangning return (error);
1654bd670b35SErik Nordmark
1655bd670b35SErik Nordmark /*
1656bd670b35SErik Nordmark * Don't let an endpoint connect to itself.
1657bd670b35SErik Nordmark */
1658bd670b35SErik Nordmark if (connp->conn_faddr_v4 == connp->conn_laddr_v4 &&
1659bd670b35SErik Nordmark connp->conn_fport == connp->conn_lport)
1660bd670b35SErik Nordmark return (-TBADADDR);
1661bd670b35SErik Nordmark
1662bd670b35SErik Nordmark tcp->tcp_state = TCPS_SYN_SENT;
1663bd670b35SErik Nordmark
1664bd670b35SErik Nordmark return (ipcl_conn_insert_v4(connp));
16657c478bd9Sstevel@tonic-gate }
16667c478bd9Sstevel@tonic-gate
16677c478bd9Sstevel@tonic-gate /*
16687c478bd9Sstevel@tonic-gate * Handle connect to IPv6 destinations.
1669bd670b35SErik Nordmark * Returns zero if OK, a positive errno, or a negative TLI error.
16707c478bd9Sstevel@tonic-gate */
16710f1702c5SYu Xiangning static int
tcp_connect_ipv6(tcp_t * tcp,in6_addr_t * dstaddrp,in_port_t dstport,uint32_t flowinfo,uint_t srcid,uint32_t scope_id)16720f1702c5SYu Xiangning tcp_connect_ipv6(tcp_t *tcp, in6_addr_t *dstaddrp, in_port_t dstport,
1673bd670b35SErik Nordmark uint32_t flowinfo, uint_t srcid, uint32_t scope_id)
16747c478bd9Sstevel@tonic-gate {
167542274831Sgt145670 uint16_t lport;
16760f1702c5SYu Xiangning conn_t *connp = tcp->tcp_connp;
1677bd670b35SErik Nordmark tcp_stack_t *tcps = tcp->tcp_tcps;
1678bd670b35SErik Nordmark int error;
16797c478bd9Sstevel@tonic-gate
1680bd670b35SErik Nordmark ASSERT(connp->conn_family == AF_INET6);
16817c478bd9Sstevel@tonic-gate
16827c478bd9Sstevel@tonic-gate /*
16837c478bd9Sstevel@tonic-gate * If we're here, it means that the destination address is a native
1684bd670b35SErik Nordmark * IPv6 address. Return an error if conn_ipversion is not IPv6. A
16857c478bd9Sstevel@tonic-gate * reason why it might not be IPv6 is if the socket was bound to an
16867c478bd9Sstevel@tonic-gate * IPv4-mapped IPv6 address.
16877c478bd9Sstevel@tonic-gate */
1688bd670b35SErik Nordmark if (connp->conn_ipversion != IPV6_VERSION)
16890f1702c5SYu Xiangning return (-TBADADDR);
16907c478bd9Sstevel@tonic-gate
16917c478bd9Sstevel@tonic-gate /*
16927c478bd9Sstevel@tonic-gate * Interpret a zero destination to mean loopback.
16937c478bd9Sstevel@tonic-gate * Update the T_CONN_REQ (sin/sin6) since it is used to
16947c478bd9Sstevel@tonic-gate * generate the T_CONN_CON.
16957c478bd9Sstevel@tonic-gate */
1696bd670b35SErik Nordmark if (IN6_IS_ADDR_UNSPECIFIED(dstaddrp))
16977c478bd9Sstevel@tonic-gate *dstaddrp = ipv6_loopback;
16987c478bd9Sstevel@tonic-gate
16997c478bd9Sstevel@tonic-gate /* Handle __sin6_src_id if socket not bound to an IP address */
1700bd670b35SErik Nordmark if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&connp->conn_laddr_v6)) {
1701a1ca8b43SDan McDonald if (!ip_srcid_find_id(srcid, &connp->conn_laddr_v6,
1702a1ca8b43SDan McDonald IPCL_ZONEID(connp), B_FALSE, tcps->tcps_netstack)) {
1703a1ca8b43SDan McDonald /* Mismatch - conn_laddr_v6 would be v4-mapped. */
1704a1ca8b43SDan McDonald return (EADDRNOTAVAIL);
1705a1ca8b43SDan McDonald }
1706bd670b35SErik Nordmark connp->conn_saddr_v6 = connp->conn_laddr_v6;
17077c478bd9Sstevel@tonic-gate }
17087c478bd9Sstevel@tonic-gate
17097c478bd9Sstevel@tonic-gate /*
1710bd670b35SErik Nordmark * Take care of the scope_id now.
17117c478bd9Sstevel@tonic-gate */
1712bd670b35SErik Nordmark if (scope_id != 0 && IN6_IS_ADDR_LINKSCOPE(dstaddrp)) {
1713bd670b35SErik Nordmark connp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
1714bd670b35SErik Nordmark connp->conn_ixa->ixa_scopeid = scope_id;
17157c478bd9Sstevel@tonic-gate } else {
1716bd670b35SErik Nordmark connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate
1719bd670b35SErik Nordmark connp->conn_flowinfo = flowinfo;
1720bd670b35SErik Nordmark connp->conn_faddr_v6 = *dstaddrp;
1721bd670b35SErik Nordmark connp->conn_fport = dstport;
17227c478bd9Sstevel@tonic-gate
17235f9878b0Sken Powell - Sun Microsystem /*
172442274831Sgt145670 * At this point the remote destination address and remote port fields
172542274831Sgt145670 * in the tcp-four-tuple have been filled in the tcp structure. Now we
1726bd670b35SErik Nordmark * have to see which state tcp was in so we can take appropriate action.
172742274831Sgt145670 */
1728bd670b35SErik Nordmark if (tcp->tcp_state == TCPS_IDLE) {
172942274831Sgt145670 /*
173042274831Sgt145670 * We support a quick connect capability here, allowing
173142274831Sgt145670 * clients to transition directly from IDLE to SYN_SENT
173242274831Sgt145670 * tcp_bindi will pick an unused port, insert the connection
173342274831Sgt145670 * in the bind hash and transition to BOUND state.
173442274831Sgt145670 */
1735f4b3ec61Sdh155122 lport = tcp_update_next_port(tcps->tcps_next_port_to_try,
1736f4b3ec61Sdh155122 tcp, B_TRUE);
1737bd670b35SErik Nordmark lport = tcp_bindi(tcp, lport, &connp->conn_laddr_v6, 0, B_TRUE,
173842274831Sgt145670 B_FALSE, B_FALSE);
1739bd670b35SErik Nordmark if (lport == 0)
1740bd670b35SErik Nordmark return (-TNOADDR);
174142274831Sgt145670 }
1742bd670b35SErik Nordmark
1743bd670b35SErik Nordmark /*
1744bd670b35SErik Nordmark * Lookup the route to determine a source address and the uinfo.
1745bd670b35SErik Nordmark * Setup TCP parameters based on the metrics/DCE.
1746bd670b35SErik Nordmark */
1747bd670b35SErik Nordmark error = tcp_set_destination(tcp);
1748bd670b35SErik Nordmark if (error != 0)
1749bd670b35SErik Nordmark return (error);
1750bd670b35SErik Nordmark
1751bd670b35SErik Nordmark /*
1752bd670b35SErik Nordmark * Don't let an endpoint connect to itself.
1753bd670b35SErik Nordmark */
1754bd670b35SErik Nordmark if (IN6_ARE_ADDR_EQUAL(&connp->conn_faddr_v6, &connp->conn_laddr_v6) &&
1755bd670b35SErik Nordmark connp->conn_fport == connp->conn_lport)
1756bd670b35SErik Nordmark return (-TBADADDR);
1757bd670b35SErik Nordmark
17587c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_SYN_SENT;
17590f1702c5SYu Xiangning
1760bd670b35SErik Nordmark return (ipcl_conn_insert_v6(connp));
17617c478bd9Sstevel@tonic-gate }
17627c478bd9Sstevel@tonic-gate
17637c478bd9Sstevel@tonic-gate /*
1764bd670b35SErik Nordmark * Disconnect
1765bd670b35SErik Nordmark * Note that unlike other functions this returns a positive tli error
1766bd670b35SErik Nordmark * when it fails; it never returns an errno.
17677c478bd9Sstevel@tonic-gate */
17680f1702c5SYu Xiangning static int
tcp_disconnect_common(tcp_t * tcp,t_scalar_t seqnum)17690f1702c5SYu Xiangning tcp_disconnect_common(tcp_t *tcp, t_scalar_t seqnum)
17707c478bd9Sstevel@tonic-gate {
1771bd670b35SErik Nordmark conn_t *lconnp;
1772f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
1773bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
17747c478bd9Sstevel@tonic-gate
17757c478bd9Sstevel@tonic-gate /*
17767c478bd9Sstevel@tonic-gate * Right now, upper modules pass down a T_DISCON_REQ to TCP,
17777c478bd9Sstevel@tonic-gate * when the stream is in BOUND state. Do not send a reset,
17787c478bd9Sstevel@tonic-gate * since the destination IP address is not valid, and it can
17797c478bd9Sstevel@tonic-gate * be the initialized value of all zeros (broadcast address).
17807c478bd9Sstevel@tonic-gate */
17810e0e37a8SErik Nordmark if (tcp->tcp_state <= TCPS_BOUND) {
1782bd670b35SErik Nordmark if (connp->conn_debug) {
1783ff550d0eSmasputra (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
17847c478bd9Sstevel@tonic-gate "tcp_disconnect: bad state, %d", tcp->tcp_state);
17857c478bd9Sstevel@tonic-gate }
17860f1702c5SYu Xiangning return (TOUTSTATE);
1787721fffe3SKacheong Poon } else if (tcp->tcp_state >= TCPS_ESTABLISHED) {
1788721fffe3SKacheong Poon TCPS_CONN_DEC(tcps);
17897c478bd9Sstevel@tonic-gate }
17907c478bd9Sstevel@tonic-gate
17917c478bd9Sstevel@tonic-gate if (seqnum == -1 || tcp->tcp_conn_req_max == 0) {
17927c478bd9Sstevel@tonic-gate
17937c478bd9Sstevel@tonic-gate /*
17947c478bd9Sstevel@tonic-gate * According to TPI, for non-listeners, ignore seqnum
17957c478bd9Sstevel@tonic-gate * and disconnect.
17967c478bd9Sstevel@tonic-gate * Following interpretation of -1 seqnum is historical
17977c478bd9Sstevel@tonic-gate * and implied TPI ? (TPI only states that for T_CONN_IND,
17987c478bd9Sstevel@tonic-gate * a valid seqnum should not be -1).
17997c478bd9Sstevel@tonic-gate *
18007c478bd9Sstevel@tonic-gate * -1 means disconnect everything
18017c478bd9Sstevel@tonic-gate * regardless even on a listener.
18027c478bd9Sstevel@tonic-gate */
18037c478bd9Sstevel@tonic-gate
18047c478bd9Sstevel@tonic-gate int old_state = tcp->tcp_state;
1805f4b3ec61Sdh155122 ip_stack_t *ipst = tcps->tcps_netstack->netstack_ip;
18067c478bd9Sstevel@tonic-gate
18077c478bd9Sstevel@tonic-gate /*
18087c478bd9Sstevel@tonic-gate * The connection can't be on the tcp_time_wait_head list
18097c478bd9Sstevel@tonic-gate * since it is not detached.
18107c478bd9Sstevel@tonic-gate */
18117c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_next == NULL);
18127c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_prev == NULL);
18137c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_expire == 0);
18147c478bd9Sstevel@tonic-gate /*
18157c478bd9Sstevel@tonic-gate * If it used to be a listener, check to make sure no one else
18167c478bd9Sstevel@tonic-gate * has taken the port before switching back to LISTEN state.
18177c478bd9Sstevel@tonic-gate */
1818bd670b35SErik Nordmark if (connp->conn_ipversion == IPV4_VERSION) {
1819bd670b35SErik Nordmark lconnp = ipcl_lookup_listener_v4(connp->conn_lport,
1820bd670b35SErik Nordmark connp->conn_laddr_v4, IPCL_ZONEID(connp), ipst);
18217c478bd9Sstevel@tonic-gate } else {
1822bd670b35SErik Nordmark uint_t ifindex = 0;
1823bd670b35SErik Nordmark
1824bd670b35SErik Nordmark if (connp->conn_ixa->ixa_flags & IXAF_SCOPEID_SET)
1825bd670b35SErik Nordmark ifindex = connp->conn_ixa->ixa_scopeid;
1826bd670b35SErik Nordmark
1827bd670b35SErik Nordmark /* Allow conn_bound_if listeners? */
1828bd670b35SErik Nordmark lconnp = ipcl_lookup_listener_v6(connp->conn_lport,
1829bd670b35SErik Nordmark &connp->conn_laddr_v6, ifindex, IPCL_ZONEID(connp),
1830bd670b35SErik Nordmark ipst);
18317c478bd9Sstevel@tonic-gate }
18320e0e37a8SErik Nordmark if (tcp->tcp_conn_req_max && lconnp == NULL) {
18337c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_LISTEN;
18349cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
18359cd928feSAlan Maguire connp->conn_ixa, void, NULL, tcp_t *, tcp, void,
18369cd928feSAlan Maguire NULL, int32_t, old_state);
18377c478bd9Sstevel@tonic-gate } else if (old_state > TCPS_BOUND) {
18387c478bd9Sstevel@tonic-gate tcp->tcp_conn_req_max = 0;
18397c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_BOUND;
18409cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
18419cd928feSAlan Maguire connp->conn_ixa, void, NULL, tcp_t *, tcp, void,
18429cd928feSAlan Maguire NULL, int32_t, old_state);
184393fcb0b9SKacheong Poon
184493fcb0b9SKacheong Poon /*
184593fcb0b9SKacheong Poon * If this end point is not going to become a listener,
184693fcb0b9SKacheong Poon * decrement the listener connection count if
184793fcb0b9SKacheong Poon * necessary. Note that we do not do this if it is
184893fcb0b9SKacheong Poon * going to be a listner (the above if case) since
184993fcb0b9SKacheong Poon * then it may remove the counter struct.
185093fcb0b9SKacheong Poon */
185193fcb0b9SKacheong Poon if (tcp->tcp_listen_cnt != NULL)
185293fcb0b9SKacheong Poon TCP_DECR_LISTEN_CNT(tcp);
18537c478bd9Sstevel@tonic-gate }
18540e0e37a8SErik Nordmark if (lconnp != NULL)
1855bd670b35SErik Nordmark CONN_DEC_REF(lconnp);
1856721fffe3SKacheong Poon switch (old_state) {
1857721fffe3SKacheong Poon case TCPS_SYN_SENT:
1858721fffe3SKacheong Poon case TCPS_SYN_RCVD:
1859721fffe3SKacheong Poon TCPS_BUMP_MIB(tcps, tcpAttemptFails);
1860721fffe3SKacheong Poon break;
1861721fffe3SKacheong Poon case TCPS_ESTABLISHED:
1862721fffe3SKacheong Poon case TCPS_CLOSE_WAIT:
1863721fffe3SKacheong Poon TCPS_BUMP_MIB(tcps, tcpEstabResets);
1864721fffe3SKacheong Poon break;
18657c478bd9Sstevel@tonic-gate }
18667c478bd9Sstevel@tonic-gate
18677c478bd9Sstevel@tonic-gate if (tcp->tcp_fused)
18687c478bd9Sstevel@tonic-gate tcp_unfuse(tcp);
18697c478bd9Sstevel@tonic-gate
18707c478bd9Sstevel@tonic-gate mutex_enter(&tcp->tcp_eager_lock);
18717c478bd9Sstevel@tonic-gate if ((tcp->tcp_conn_req_cnt_q0 != 0) ||
18727c478bd9Sstevel@tonic-gate (tcp->tcp_conn_req_cnt_q != 0)) {
18737c478bd9Sstevel@tonic-gate tcp_eager_cleanup(tcp, 0);
18747c478bd9Sstevel@tonic-gate }
18757c478bd9Sstevel@tonic-gate mutex_exit(&tcp->tcp_eager_lock);
18767c478bd9Sstevel@tonic-gate
18777c478bd9Sstevel@tonic-gate tcp_xmit_ctl("tcp_disconnect", tcp, tcp->tcp_snxt,
18787c478bd9Sstevel@tonic-gate tcp->tcp_rnxt, TH_RST | TH_ACK);
18797c478bd9Sstevel@tonic-gate
18807c478bd9Sstevel@tonic-gate tcp_reinit(tcp);
18817c478bd9Sstevel@tonic-gate
18820f1702c5SYu Xiangning return (0);
18837c478bd9Sstevel@tonic-gate } else if (!tcp_eager_blowoff(tcp, seqnum)) {
18840f1702c5SYu Xiangning return (TBADSEQ);
18850f1702c5SYu Xiangning }
18860f1702c5SYu Xiangning return (0);
18870f1702c5SYu Xiangning }
18880f1702c5SYu Xiangning
18890f1702c5SYu Xiangning /*
18900f1702c5SYu Xiangning * Our client hereby directs us to reject the connection request
1891bd670b35SErik Nordmark * that tcp_input_listener() marked with 'seqnum'. Rejection consists
18920f1702c5SYu Xiangning * of sending the appropriate RST, not an ICMP error.
18930f1702c5SYu Xiangning */
1894721fffe3SKacheong Poon void
tcp_disconnect(tcp_t * tcp,mblk_t * mp)18950f1702c5SYu Xiangning tcp_disconnect(tcp_t *tcp, mblk_t *mp)
18960f1702c5SYu Xiangning {
18970f1702c5SYu Xiangning t_scalar_t seqnum;
18980f1702c5SYu Xiangning int error;
1899bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
19000f1702c5SYu Xiangning
19010f1702c5SYu Xiangning ASSERT((uintptr_t)(mp->b_wptr - mp->b_rptr) <= (uintptr_t)INT_MAX);
19020f1702c5SYu Xiangning if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_discon_req)) {
19030f1702c5SYu Xiangning tcp_err_ack(tcp, mp, TPROTO, 0);
19047c478bd9Sstevel@tonic-gate return;
19057c478bd9Sstevel@tonic-gate }
19060f1702c5SYu Xiangning seqnum = ((struct T_discon_req *)mp->b_rptr)->SEQ_number;
19070f1702c5SYu Xiangning error = tcp_disconnect_common(tcp, seqnum);
19080f1702c5SYu Xiangning if (error != 0)
19090f1702c5SYu Xiangning tcp_err_ack(tcp, mp, error, 0);
19100f1702c5SYu Xiangning else {
19117c478bd9Sstevel@tonic-gate if (tcp->tcp_state >= TCPS_ESTABLISHED) {
19127c478bd9Sstevel@tonic-gate /* Send M_FLUSH according to TPI */
1913bd670b35SErik Nordmark (void) putnextctl1(connp->conn_rq, M_FLUSH, FLUSHRW);
19147c478bd9Sstevel@tonic-gate }
19157c478bd9Sstevel@tonic-gate mp = mi_tpi_ok_ack_alloc(mp);
1916bd670b35SErik Nordmark if (mp != NULL)
1917bd670b35SErik Nordmark putnext(connp->conn_rq, mp);
19187c478bd9Sstevel@tonic-gate }
19190f1702c5SYu Xiangning }
19207c478bd9Sstevel@tonic-gate
19217c478bd9Sstevel@tonic-gate /*
19227c478bd9Sstevel@tonic-gate * Handle reinitialization of a tcp structure.
19237c478bd9Sstevel@tonic-gate * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE.
19247c478bd9Sstevel@tonic-gate */
19257c478bd9Sstevel@tonic-gate static void
tcp_reinit(tcp_t * tcp)19267c478bd9Sstevel@tonic-gate tcp_reinit(tcp_t *tcp)
19277c478bd9Sstevel@tonic-gate {
19287c478bd9Sstevel@tonic-gate mblk_t *mp;
1929f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
1930bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
19319cd928feSAlan Maguire int32_t oldstate;
19327c478bd9Sstevel@tonic-gate
19337c478bd9Sstevel@tonic-gate /* tcp_reinit should never be called for detached tcp_t's */
19347c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_listener == NULL);
1935bd670b35SErik Nordmark ASSERT((connp->conn_family == AF_INET &&
1936bd670b35SErik Nordmark connp->conn_ipversion == IPV4_VERSION) ||
1937bd670b35SErik Nordmark (connp->conn_family == AF_INET6 &&
1938bd670b35SErik Nordmark (connp->conn_ipversion == IPV4_VERSION ||
1939bd670b35SErik Nordmark connp->conn_ipversion == IPV6_VERSION)));
19407c478bd9Sstevel@tonic-gate
19417c478bd9Sstevel@tonic-gate /* Cancel outstanding timers */
19427c478bd9Sstevel@tonic-gate tcp_timers_stop(tcp);
19437c478bd9Sstevel@tonic-gate
19447c478bd9Sstevel@tonic-gate tcp_close_mpp(&tcp->tcp_xmit_head);
19457c478bd9Sstevel@tonic-gate if (tcp->tcp_snd_zcopy_aware)
19467c478bd9Sstevel@tonic-gate tcp_zcopy_notify(tcp);
19477c478bd9Sstevel@tonic-gate tcp->tcp_xmit_last = tcp->tcp_xmit_tail = NULL;
19487c478bd9Sstevel@tonic-gate tcp->tcp_unsent = tcp->tcp_xmit_tail_unsent = 0;
1949e0968231Svi117747 mutex_enter(&tcp->tcp_non_sq_lock);
1950ff550d0eSmasputra if (tcp->tcp_flow_stopped &&
1951bd670b35SErik Nordmark TCP_UNSENT_BYTES(tcp) <= connp->conn_sndlowat) {
1952ff550d0eSmasputra tcp_clrqfull(tcp);
1953ff550d0eSmasputra }
1954e0968231Svi117747 mutex_exit(&tcp->tcp_non_sq_lock);
19557c478bd9Sstevel@tonic-gate tcp_close_mpp(&tcp->tcp_reass_head);
19567c478bd9Sstevel@tonic-gate tcp->tcp_reass_tail = NULL;
19577c478bd9Sstevel@tonic-gate if (tcp->tcp_rcv_list != NULL) {
19587c478bd9Sstevel@tonic-gate /* Free b_next chain */
19597c478bd9Sstevel@tonic-gate tcp_close_mpp(&tcp->tcp_rcv_list);
19607c478bd9Sstevel@tonic-gate tcp->tcp_rcv_last_head = NULL;
19617c478bd9Sstevel@tonic-gate tcp->tcp_rcv_last_tail = NULL;
19627c478bd9Sstevel@tonic-gate tcp->tcp_rcv_cnt = 0;
19637c478bd9Sstevel@tonic-gate }
19647c478bd9Sstevel@tonic-gate tcp->tcp_rcv_last_tail = NULL;
19657c478bd9Sstevel@tonic-gate
19667c478bd9Sstevel@tonic-gate if ((mp = tcp->tcp_urp_mp) != NULL) {
19677c478bd9Sstevel@tonic-gate freemsg(mp);
19687c478bd9Sstevel@tonic-gate tcp->tcp_urp_mp = NULL;
19697c478bd9Sstevel@tonic-gate }
19707c478bd9Sstevel@tonic-gate if ((mp = tcp->tcp_urp_mark_mp) != NULL) {
19717c478bd9Sstevel@tonic-gate freemsg(mp);
19727c478bd9Sstevel@tonic-gate tcp->tcp_urp_mark_mp = NULL;
19737c478bd9Sstevel@tonic-gate }
19747c478bd9Sstevel@tonic-gate if (tcp->tcp_fused_sigurg_mp != NULL) {
19750f1702c5SYu Xiangning ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
19767c478bd9Sstevel@tonic-gate freeb(tcp->tcp_fused_sigurg_mp);
19777c478bd9Sstevel@tonic-gate tcp->tcp_fused_sigurg_mp = NULL;
19787c478bd9Sstevel@tonic-gate }
1979f7f8e53dSKacheong Poon if (tcp->tcp_ordrel_mp != NULL) {
19800f1702c5SYu Xiangning ASSERT(!IPCL_IS_NONSTR(tcp->tcp_connp));
1981f7f8e53dSKacheong Poon freeb(tcp->tcp_ordrel_mp);
1982f7f8e53dSKacheong Poon tcp->tcp_ordrel_mp = NULL;
1983f7f8e53dSKacheong Poon }
19847c478bd9Sstevel@tonic-gate
19857c478bd9Sstevel@tonic-gate /*
19867c478bd9Sstevel@tonic-gate * Following is a union with two members which are
19877c478bd9Sstevel@tonic-gate * identical types and size so the following cleanup
19887c478bd9Sstevel@tonic-gate * is enough.
19897c478bd9Sstevel@tonic-gate */
19907c478bd9Sstevel@tonic-gate tcp_close_mpp(&tcp->tcp_conn.tcp_eager_conn_ind);
19917c478bd9Sstevel@tonic-gate
1992bd670b35SErik Nordmark CL_INET_DISCONNECT(connp);
19937c478bd9Sstevel@tonic-gate
19947c478bd9Sstevel@tonic-gate /*
19957c478bd9Sstevel@tonic-gate * The connection can't be on the tcp_time_wait_head list
19967c478bd9Sstevel@tonic-gate * since it is not detached.
19977c478bd9Sstevel@tonic-gate */
19987c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_next == NULL);
19997c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_prev == NULL);
20007c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_expire == 0);
20017c478bd9Sstevel@tonic-gate
20027c478bd9Sstevel@tonic-gate /*
20037c478bd9Sstevel@tonic-gate * Reset/preserve other values
20047c478bd9Sstevel@tonic-gate */
20057c478bd9Sstevel@tonic-gate tcp_reinit_values(tcp);
2006bd670b35SErik Nordmark ipcl_hash_remove(connp);
200776a1033eSErik Nordmark /* Note that ixa_cred gets cleared in ixa_cleanup */
2008bd670b35SErik Nordmark ixa_cleanup(connp->conn_ixa);
2009f4b3ec61Sdh155122 tcp_ipsec_cleanup(tcp);
20107c478bd9Sstevel@tonic-gate
2011bd670b35SErik Nordmark connp->conn_laddr_v6 = connp->conn_bound_addr_v6;
2012bd670b35SErik Nordmark connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
20139cd928feSAlan Maguire oldstate = tcp->tcp_state;
20145f9878b0Sken Powell - Sun Microsystem
20157c478bd9Sstevel@tonic-gate if (tcp->tcp_conn_req_max != 0) {
20167c478bd9Sstevel@tonic-gate /*
20177c478bd9Sstevel@tonic-gate * This is the case when a TLI program uses the same
20187c478bd9Sstevel@tonic-gate * transport end point to accept a connection. This
20197c478bd9Sstevel@tonic-gate * makes the TCP both a listener and acceptor. When
20207c478bd9Sstevel@tonic-gate * this connection is closed, we need to set the state
20217c478bd9Sstevel@tonic-gate * back to TCPS_LISTEN. Make sure that the eager list
20227c478bd9Sstevel@tonic-gate * is reinitialized.
20237c478bd9Sstevel@tonic-gate *
20247c478bd9Sstevel@tonic-gate * Note that this stream is still bound to the four
20257c478bd9Sstevel@tonic-gate * tuples of the previous connection in IP. If a new
20267c478bd9Sstevel@tonic-gate * SYN with different foreign address comes in, IP will
20277c478bd9Sstevel@tonic-gate * not find it and will send it to the global queue. In
20287c478bd9Sstevel@tonic-gate * the global queue, TCP will do a tcp_lookup_listener()
20297c478bd9Sstevel@tonic-gate * to find this stream. This works because this stream
20307c478bd9Sstevel@tonic-gate * is only removed from connected hash.
20317c478bd9Sstevel@tonic-gate *
20327c478bd9Sstevel@tonic-gate */
20337c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_LISTEN;
20347c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
2035866ba9ddSjprakash tcp->tcp_eager_next_drop_q0 = tcp;
2036866ba9ddSjprakash tcp->tcp_eager_prev_drop_q0 = tcp;
2037bd670b35SErik Nordmark /*
2038bd670b35SErik Nordmark * Initially set conn_recv to tcp_input_listener_unbound to try
2039bd670b35SErik Nordmark * to pick a good squeue for the listener when the first SYN
2040bd670b35SErik Nordmark * arrives. tcp_input_listener_unbound sets it to
2041bd670b35SErik Nordmark * tcp_input_listener on that first SYN.
2042bd670b35SErik Nordmark */
2043bd670b35SErik Nordmark connp->conn_recv = tcp_input_listener_unbound;
2044bd670b35SErik Nordmark
2045bd670b35SErik Nordmark connp->conn_proto = IPPROTO_TCP;
2046bd670b35SErik Nordmark connp->conn_faddr_v6 = ipv6_all_zeros;
2047bd670b35SErik Nordmark connp->conn_fport = 0;
2048bd670b35SErik Nordmark
2049bd670b35SErik Nordmark (void) ipcl_bind_insert(connp);
20507c478bd9Sstevel@tonic-gate } else {
20517c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_BOUND;
20527c478bd9Sstevel@tonic-gate }
20537c478bd9Sstevel@tonic-gate
20547c478bd9Sstevel@tonic-gate /*
20557c478bd9Sstevel@tonic-gate * Initialize to default values
20567c478bd9Sstevel@tonic-gate */
2057707e74bcSKacheong Poon tcp_init_values(tcp, NULL);
20587c478bd9Sstevel@tonic-gate
20599cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
20609cd928feSAlan Maguire connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
20619cd928feSAlan Maguire int32_t, oldstate);
20629cd928feSAlan Maguire
20637c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_ptpbhn != NULL);
2064bd670b35SErik Nordmark tcp->tcp_rwnd = connp->conn_rcvbuf;
2065bd670b35SErik Nordmark tcp->tcp_mss = connp->conn_ipversion != IPV4_VERSION ?
2066f4b3ec61Sdh155122 tcps->tcps_mss_def_ipv6 : tcps->tcps_mss_def_ipv4;
20677c478bd9Sstevel@tonic-gate }
20687c478bd9Sstevel@tonic-gate
20697c478bd9Sstevel@tonic-gate /*
20707c478bd9Sstevel@tonic-gate * Force values to zero that need be zero.
20717c478bd9Sstevel@tonic-gate * Do not touch values asociated with the BOUND or LISTEN state
20727c478bd9Sstevel@tonic-gate * since the connection will end up in that state after the reinit.
20737c478bd9Sstevel@tonic-gate * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t
20747c478bd9Sstevel@tonic-gate * structure!
20757c478bd9Sstevel@tonic-gate */
20767c478bd9Sstevel@tonic-gate static void
tcp_reinit_values(tcp_t * tcp)20778a06b3d6SToomas Soome tcp_reinit_values(tcp_t *tcp)
20787c478bd9Sstevel@tonic-gate {
2079f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
2080bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
2081f4b3ec61Sdh155122
20827c478bd9Sstevel@tonic-gate #define DONTCARE(x)
20837c478bd9Sstevel@tonic-gate #define PRESERVE(x)
20847c478bd9Sstevel@tonic-gate
20850f1702c5SYu Xiangning PRESERVE(tcp->tcp_bind_hash_port);
20867c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_bind_hash);
20877c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_ptpbhn);
20887c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_acceptor_hash);
20897c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_ptpahn);
20907c478bd9Sstevel@tonic-gate
20917c478bd9Sstevel@tonic-gate /* Should be ASSERT NULL on these with new code! */
20927c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_next == NULL);
20937c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_prev == NULL);
20947c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_time_wait_expire == 0);
20957c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_state);
2096bd670b35SErik Nordmark PRESERVE(connp->conn_rq);
2097bd670b35SErik Nordmark PRESERVE(connp->conn_wq);
20987c478bd9Sstevel@tonic-gate
20997c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_xmit_head == NULL);
21007c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_xmit_last == NULL);
21017c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_unsent == 0);
21027c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_xmit_tail == NULL);
21037c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_xmit_tail_unsent == 0);
21047c478bd9Sstevel@tonic-gate
21057c478bd9Sstevel@tonic-gate tcp->tcp_snxt = 0; /* Displayed in mib */
21067c478bd9Sstevel@tonic-gate tcp->tcp_suna = 0; /* Displayed in mib */
21077c478bd9Sstevel@tonic-gate tcp->tcp_swnd = 0;
2108bd670b35SErik Nordmark DONTCARE(tcp->tcp_cwnd); /* Init in tcp_process_options */
21097c478bd9Sstevel@tonic-gate
2110bd670b35SErik Nordmark if (connp->conn_ht_iphc != NULL) {
2111bd670b35SErik Nordmark kmem_free(connp->conn_ht_iphc, connp->conn_ht_iphc_allocated);
2112bd670b35SErik Nordmark connp->conn_ht_iphc = NULL;
2113bd670b35SErik Nordmark connp->conn_ht_iphc_allocated = 0;
2114bd670b35SErik Nordmark connp->conn_ht_iphc_len = 0;
2115bd670b35SErik Nordmark connp->conn_ht_ulp = NULL;
2116bd670b35SErik Nordmark connp->conn_ht_ulp_len = 0;
2117bd670b35SErik Nordmark tcp->tcp_ipha = NULL;
2118bd670b35SErik Nordmark tcp->tcp_ip6h = NULL;
2119bd670b35SErik Nordmark tcp->tcp_tcpha = NULL;
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate
2122bd670b35SErik Nordmark /* We clear any IP_OPTIONS and extension headers */
2123bd670b35SErik Nordmark ip_pkt_free(&connp->conn_xmit_ipp);
2124bd670b35SErik Nordmark
21257c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_naglim); /* Init in tcp_init_values */
21267c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_ipha);
21277c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_ip6h);
2128bd670b35SErik Nordmark DONTCARE(tcp->tcp_tcpha);
21297c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits = 0;
21307c478bd9Sstevel@tonic-gate
21317c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_timer_backoff); /* Init in tcp_init_values */
21327c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_last_recv_time); /* Init in tcp_init_values */
21337c478bd9Sstevel@tonic-gate tcp->tcp_last_rcv_lbolt = 0;
21347c478bd9Sstevel@tonic-gate
21357c478bd9Sstevel@tonic-gate tcp->tcp_init_cwnd = 0;
21367c478bd9Sstevel@tonic-gate
21377c478bd9Sstevel@tonic-gate tcp->tcp_urp_last_valid = 0;
21387c478bd9Sstevel@tonic-gate tcp->tcp_hard_binding = 0;
21397c478bd9Sstevel@tonic-gate
21407c478bd9Sstevel@tonic-gate tcp->tcp_fin_acked = 0;
21417c478bd9Sstevel@tonic-gate tcp->tcp_fin_rcvd = 0;
21427c478bd9Sstevel@tonic-gate tcp->tcp_fin_sent = 0;
21437c478bd9Sstevel@tonic-gate tcp->tcp_ordrel_done = 0;
21447c478bd9Sstevel@tonic-gate
21457c478bd9Sstevel@tonic-gate tcp->tcp_detached = 0;
21467c478bd9Sstevel@tonic-gate
21477c478bd9Sstevel@tonic-gate tcp->tcp_snd_ws_ok = B_FALSE;
21487c478bd9Sstevel@tonic-gate tcp->tcp_snd_ts_ok = B_FALSE;
21497c478bd9Sstevel@tonic-gate tcp->tcp_zero_win_probe = 0;
21507c478bd9Sstevel@tonic-gate
21517c478bd9Sstevel@tonic-gate tcp->tcp_loopback = 0;
21527c478bd9Sstevel@tonic-gate tcp->tcp_localnet = 0;
21537c478bd9Sstevel@tonic-gate tcp->tcp_syn_defense = 0;
21547c478bd9Sstevel@tonic-gate tcp->tcp_set_timer = 0;
21557c478bd9Sstevel@tonic-gate
21567c478bd9Sstevel@tonic-gate tcp->tcp_active_open = 0;
21577c478bd9Sstevel@tonic-gate tcp->tcp_rexmit = B_FALSE;
21587c478bd9Sstevel@tonic-gate tcp->tcp_xmit_zc_clean = B_FALSE;
21597c478bd9Sstevel@tonic-gate
21607c478bd9Sstevel@tonic-gate tcp->tcp_snd_sack_ok = B_FALSE;
21617c478bd9Sstevel@tonic-gate tcp->tcp_hwcksum = B_FALSE;
21627c478bd9Sstevel@tonic-gate
2163bd670b35SErik Nordmark DONTCARE(tcp->tcp_maxpsz_multiplier); /* Init in tcp_init_values */
21647c478bd9Sstevel@tonic-gate
21657c478bd9Sstevel@tonic-gate tcp->tcp_conn_def_q0 = 0;
21667c478bd9Sstevel@tonic-gate tcp->tcp_ip_forward_progress = B_FALSE;
21677c478bd9Sstevel@tonic-gate tcp->tcp_ecn_ok = B_FALSE;
21687c478bd9Sstevel@tonic-gate
21697c478bd9Sstevel@tonic-gate tcp->tcp_cwr = B_FALSE;
21707c478bd9Sstevel@tonic-gate tcp->tcp_ecn_echo_on = B_FALSE;
2171410734d4SPhilip Kirk tcp->tcp_is_wnd_shrnk = B_FALSE;
21727c478bd9Sstevel@tonic-gate
217366cd0f60SKacheong Poon TCP_NOTSACK_REMOVE_ALL(tcp->tcp_notsack_list, tcp);
217466cd0f60SKacheong Poon bzero(&tcp->tcp_sack_info, sizeof (tcp_sack_info_t));
21757c478bd9Sstevel@tonic-gate
21767c478bd9Sstevel@tonic-gate tcp->tcp_rcv_ws = 0;
21777c478bd9Sstevel@tonic-gate tcp->tcp_snd_ws = 0;
21787c478bd9Sstevel@tonic-gate tcp->tcp_ts_recent = 0;
21797c478bd9Sstevel@tonic-gate tcp->tcp_rnxt = 0; /* Displayed in mib */
21807c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_rwnd); /* Set in tcp_reinit() */
2181bd670b35SErik Nordmark tcp->tcp_initial_pmtu = 0;
21827c478bd9Sstevel@tonic-gate
21837c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_reass_head == NULL);
21847c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_reass_tail == NULL);
21857c478bd9Sstevel@tonic-gate
21867c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_cnt = 0;
21877c478bd9Sstevel@tonic-gate
21887c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_rcv_list == NULL);
21897c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_rcv_last_head == NULL);
21907c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_rcv_last_tail == NULL);
21917c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_rcv_cnt == 0);
21927c478bd9Sstevel@tonic-gate
2193bd670b35SErik Nordmark DONTCARE(tcp->tcp_cwnd_ssthresh); /* Init in tcp_set_destination */
21947c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_cwnd_max); /* Init in tcp_init_values */
21957c478bd9Sstevel@tonic-gate tcp->tcp_csuna = 0;
21967c478bd9Sstevel@tonic-gate
21977c478bd9Sstevel@tonic-gate tcp->tcp_rto = 0; /* Displayed in MIB */
21987c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_rtt_sa); /* Init in tcp_init_values */
21997c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_rtt_sd); /* Init in tcp_init_values */
22007c478bd9Sstevel@tonic-gate tcp->tcp_rtt_update = 0;
2201a2f04351SSebastien Roy tcp->tcp_rtt_sum = 0;
2202a2f04351SSebastien Roy tcp->tcp_rtt_cnt = 0;
22037c478bd9Sstevel@tonic-gate
22047c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_swl1); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */
22057c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_swl2); /* Init in case TCPS_LISTEN/TCPS_SYN_SENT */
22067c478bd9Sstevel@tonic-gate
22077c478bd9Sstevel@tonic-gate tcp->tcp_rack = 0; /* Displayed in mib */
22087c478bd9Sstevel@tonic-gate tcp->tcp_rack_cnt = 0;
22097c478bd9Sstevel@tonic-gate tcp->tcp_rack_cur_max = 0;
22107c478bd9Sstevel@tonic-gate tcp->tcp_rack_abs_max = 0;
22117c478bd9Sstevel@tonic-gate
22127c478bd9Sstevel@tonic-gate tcp->tcp_max_swnd = 0;
22137c478bd9Sstevel@tonic-gate
22147c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_listener == NULL);
22157c478bd9Sstevel@tonic-gate
22167c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_irs); /* tcp_valid_bits cleared */
22177c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_iss); /* tcp_valid_bits cleared */
22187c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_fss); /* tcp_valid_bits cleared */
22197c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_urg); /* tcp_valid_bits cleared */
22207c478bd9Sstevel@tonic-gate
22217c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_conn_req_cnt_q == 0);
22227c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_conn_req_cnt_q0 == 0);
22237c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_conn_req_max);
22247c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_conn_req_seqnum);
22257c478bd9Sstevel@tonic-gate
22267c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_first_timer_threshold); /* Init in tcp_init_values */
22277c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_second_timer_threshold); /* Init in tcp_init_values */
22287c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_first_ctimer_threshold); /* Init in tcp_init_values */
22297c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_second_ctimer_threshold); /* in tcp_init_values */
22307c478bd9Sstevel@tonic-gate
22317c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_urp_last); /* tcp_urp_last_valid is cleared */
22327c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_urp_mp == NULL);
22337c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_urp_mark_mp == NULL);
22347c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_fused_sigurg_mp == NULL);
22357c478bd9Sstevel@tonic-gate
22367c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_eager_next_q == NULL);
22377c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_eager_last_q == NULL);
22387c478bd9Sstevel@tonic-gate ASSERT((tcp->tcp_eager_next_q0 == NULL &&
22397c478bd9Sstevel@tonic-gate tcp->tcp_eager_prev_q0 == NULL) ||
22407c478bd9Sstevel@tonic-gate tcp->tcp_eager_next_q0 == tcp->tcp_eager_prev_q0);
22417c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_conn.tcp_eager_conn_ind == NULL);
22427c478bd9Sstevel@tonic-gate
2243866ba9ddSjprakash ASSERT((tcp->tcp_eager_next_drop_q0 == NULL &&
2244866ba9ddSjprakash tcp->tcp_eager_prev_drop_q0 == NULL) ||
2245866ba9ddSjprakash tcp->tcp_eager_next_drop_q0 == tcp->tcp_eager_prev_drop_q0);
2246866ba9ddSjprakash
224718576aacSDan McDonald DONTCARE(tcp->tcp_ka_rinterval); /* Init in tcp_init_values */
224818576aacSDan McDonald DONTCARE(tcp->tcp_ka_abort_thres); /* Init in tcp_init_values */
224918576aacSDan McDonald DONTCARE(tcp->tcp_ka_cnt); /* Init in tcp_init_values */
225018576aacSDan McDonald
22517c478bd9Sstevel@tonic-gate tcp->tcp_client_errno = 0;
22527c478bd9Sstevel@tonic-gate
2253bd670b35SErik Nordmark DONTCARE(connp->conn_sum); /* Init in tcp_init_values */
22547c478bd9Sstevel@tonic-gate
2255bd670b35SErik Nordmark connp->conn_faddr_v6 = ipv6_all_zeros; /* Displayed in MIB */
22567c478bd9Sstevel@tonic-gate
2257bd670b35SErik Nordmark PRESERVE(connp->conn_bound_addr_v6);
22587c478bd9Sstevel@tonic-gate tcp->tcp_last_sent_len = 0;
22597c478bd9Sstevel@tonic-gate tcp->tcp_dupack_cnt = 0;
22607c478bd9Sstevel@tonic-gate
2261bd670b35SErik Nordmark connp->conn_fport = 0; /* Displayed in MIB */
2262bd670b35SErik Nordmark PRESERVE(connp->conn_lport);
22637c478bd9Sstevel@tonic-gate
22647c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_acceptor_lockp);
22657c478bd9Sstevel@tonic-gate
2266f7f8e53dSKacheong Poon ASSERT(tcp->tcp_ordrel_mp == NULL);
22677c478bd9Sstevel@tonic-gate PRESERVE(tcp->tcp_acceptor_id);
22687c478bd9Sstevel@tonic-gate DONTCARE(tcp->tcp_ipsec_overhead);
22697c478bd9Sstevel@tonic-gate
2270bd670b35SErik Nordmark PRESERVE(connp->conn_family);
2271bd670b35SErik Nordmark /* Remove any remnants of mapped address binding */
2272bd670b35SErik Nordmark if (connp->conn_family == AF_INET6) {
2273bd670b35SErik Nordmark connp->conn_ipversion = IPV6_VERSION;
2274f4b3ec61Sdh155122 tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
22757c478bd9Sstevel@tonic-gate } else {
2276bd670b35SErik Nordmark connp->conn_ipversion = IPV4_VERSION;
2277f4b3ec61Sdh155122 tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
22787c478bd9Sstevel@tonic-gate }
22797c478bd9Sstevel@tonic-gate
2280bd670b35SErik Nordmark connp->conn_bound_if = 0;
2281bd670b35SErik Nordmark connp->conn_recv_ancillary.crb_all = 0;
22827c478bd9Sstevel@tonic-gate tcp->tcp_recvifindex = 0;
22837c478bd9Sstevel@tonic-gate tcp->tcp_recvhops = 0;
22847c478bd9Sstevel@tonic-gate tcp->tcp_closed = 0;
22857c478bd9Sstevel@tonic-gate if (tcp->tcp_hopopts != NULL) {
22867c478bd9Sstevel@tonic-gate mi_free(tcp->tcp_hopopts);
22877c478bd9Sstevel@tonic-gate tcp->tcp_hopopts = NULL;
22887c478bd9Sstevel@tonic-gate tcp->tcp_hopoptslen = 0;
22897c478bd9Sstevel@tonic-gate }
22907c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_hopoptslen == 0);
22917c478bd9Sstevel@tonic-gate if (tcp->tcp_dstopts != NULL) {
22927c478bd9Sstevel@tonic-gate mi_free(tcp->tcp_dstopts);
22937c478bd9Sstevel@tonic-gate tcp->tcp_dstopts = NULL;
22947c478bd9Sstevel@tonic-gate tcp->tcp_dstoptslen = 0;
22957c478bd9Sstevel@tonic-gate }
22967c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_dstoptslen == 0);
2297bd670b35SErik Nordmark if (tcp->tcp_rthdrdstopts != NULL) {
2298bd670b35SErik Nordmark mi_free(tcp->tcp_rthdrdstopts);
2299bd670b35SErik Nordmark tcp->tcp_rthdrdstopts = NULL;
2300bd670b35SErik Nordmark tcp->tcp_rthdrdstoptslen = 0;
23017c478bd9Sstevel@tonic-gate }
2302bd670b35SErik Nordmark ASSERT(tcp->tcp_rthdrdstoptslen == 0);
23037c478bd9Sstevel@tonic-gate if (tcp->tcp_rthdr != NULL) {
23047c478bd9Sstevel@tonic-gate mi_free(tcp->tcp_rthdr);
23057c478bd9Sstevel@tonic-gate tcp->tcp_rthdr = NULL;
23067c478bd9Sstevel@tonic-gate tcp->tcp_rthdrlen = 0;
23077c478bd9Sstevel@tonic-gate }
23087c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_rthdrlen == 0);
23097c478bd9Sstevel@tonic-gate
2310ff550d0eSmasputra /* Reset fusion-related fields */
23117c478bd9Sstevel@tonic-gate tcp->tcp_fused = B_FALSE;
23127c478bd9Sstevel@tonic-gate tcp->tcp_unfusable = B_FALSE;
23137c478bd9Sstevel@tonic-gate tcp->tcp_fused_sigurg = B_FALSE;
23147c478bd9Sstevel@tonic-gate tcp->tcp_loopback_peer = NULL;
23157c478bd9Sstevel@tonic-gate
23168347601bSyl150051 tcp->tcp_lso = B_FALSE;
23178347601bSyl150051
23187c478bd9Sstevel@tonic-gate tcp->tcp_in_ack_unsent = 0;
23197c478bd9Sstevel@tonic-gate tcp->tcp_cork = B_FALSE;
2320866ba9ddSjprakash tcp->tcp_tconnind_started = B_FALSE;
23217c478bd9Sstevel@tonic-gate
2322ff550d0eSmasputra PRESERVE(tcp->tcp_squeue_bytes);
2323ff550d0eSmasputra
23240163a147Sjprakash tcp->tcp_closemp_used = B_FALSE;
2325866ba9ddSjprakash
2326f7f8e53dSKacheong Poon PRESERVE(tcp->tcp_rsrv_mp);
2327f7f8e53dSKacheong Poon PRESERVE(tcp->tcp_rsrv_mp_lock);
2328f7f8e53dSKacheong Poon
2329866ba9ddSjprakash #ifdef DEBUG
2330866ba9ddSjprakash DONTCARE(tcp->tcmp_stk[0]);
2331866ba9ddSjprakash #endif
2332866ba9ddSjprakash
23330f1702c5SYu Xiangning PRESERVE(tcp->tcp_connid);
23340f1702c5SYu Xiangning
2335*1edba515SAndy Fiddaman if (tcp->tcp_sig_sa_in != NULL) {
2336*1edba515SAndy Fiddaman tcpsig_sa_rele(tcp->tcp_sig_sa_in);
2337*1edba515SAndy Fiddaman tcp->tcp_sig_sa_in = NULL;
2338*1edba515SAndy Fiddaman }
2339*1edba515SAndy Fiddaman if (tcp->tcp_sig_sa_out != NULL) {
2340*1edba515SAndy Fiddaman tcpsig_sa_rele(tcp->tcp_sig_sa_out);
2341*1edba515SAndy Fiddaman tcp->tcp_sig_sa_out = NULL;
2342*1edba515SAndy Fiddaman }
2343*1edba515SAndy Fiddaman
234493fcb0b9SKacheong Poon ASSERT(tcp->tcp_listen_cnt == NULL);
234593fcb0b9SKacheong Poon ASSERT(tcp->tcp_reass_tid == 0);
2346866ba9ddSjprakash
234745a4b79dSSebastien Roy /* Allow the CC algorithm to clean up after itself. */
234845a4b79dSSebastien Roy if (tcp->tcp_cc_algo->cb_destroy != NULL)
234945a4b79dSSebastien Roy tcp->tcp_cc_algo->cb_destroy(&tcp->tcp_ccv);
235045a4b79dSSebastien Roy tcp->tcp_cc_algo = NULL;
235145a4b79dSSebastien Roy
23527c478bd9Sstevel@tonic-gate #undef DONTCARE
23537c478bd9Sstevel@tonic-gate #undef PRESERVE
23547c478bd9Sstevel@tonic-gate }
23557c478bd9Sstevel@tonic-gate
2356707e74bcSKacheong Poon /*
2357707e74bcSKacheong Poon * Initialize the various fields in tcp_t. If parent (the listener) is non
2358707e74bcSKacheong Poon * NULL, certain values will be inheritted from it.
2359707e74bcSKacheong Poon */
2360721fffe3SKacheong Poon void
tcp_init_values(tcp_t * tcp,tcp_t * parent)2361707e74bcSKacheong Poon tcp_init_values(tcp_t *tcp, tcp_t *parent)
23627c478bd9Sstevel@tonic-gate {
2363f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
2364bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
23657c478bd9Sstevel@tonic-gate
2366bd670b35SErik Nordmark ASSERT((connp->conn_family == AF_INET &&
2367bd670b35SErik Nordmark connp->conn_ipversion == IPV4_VERSION) ||
2368bd670b35SErik Nordmark (connp->conn_family == AF_INET6 &&
2369bd670b35SErik Nordmark (connp->conn_ipversion == IPV4_VERSION ||
2370bd670b35SErik Nordmark connp->conn_ipversion == IPV6_VERSION)));
23717c478bd9Sstevel@tonic-gate
237245a4b79dSSebastien Roy tcp->tcp_ccv.type = IPPROTO_TCP;
237345a4b79dSSebastien Roy tcp->tcp_ccv.ccvc.tcp = tcp;
237445a4b79dSSebastien Roy
2375707e74bcSKacheong Poon if (parent == NULL) {
237645a4b79dSSebastien Roy tcp->tcp_cc_algo = tcps->tcps_default_cc_algo;
237745a4b79dSSebastien Roy
2378707e74bcSKacheong Poon tcp->tcp_naglim = tcps->tcps_naglim_def;
2379707e74bcSKacheong Poon
2380707e74bcSKacheong Poon tcp->tcp_rto_initial = tcps->tcps_rexmit_interval_initial;
2381707e74bcSKacheong Poon tcp->tcp_rto_min = tcps->tcps_rexmit_interval_min;
2382707e74bcSKacheong Poon tcp->tcp_rto_max = tcps->tcps_rexmit_interval_max;
2383707e74bcSKacheong Poon
2384707e74bcSKacheong Poon tcp->tcp_first_ctimer_threshold =
2385707e74bcSKacheong Poon tcps->tcps_ip_notify_cinterval;
2386707e74bcSKacheong Poon tcp->tcp_second_ctimer_threshold =
2387707e74bcSKacheong Poon tcps->tcps_ip_abort_cinterval;
2388707e74bcSKacheong Poon tcp->tcp_first_timer_threshold = tcps->tcps_ip_notify_interval;
2389707e74bcSKacheong Poon tcp->tcp_second_timer_threshold = tcps->tcps_ip_abort_interval;
2390707e74bcSKacheong Poon
2391707e74bcSKacheong Poon tcp->tcp_fin_wait_2_flush_interval =
2392707e74bcSKacheong Poon tcps->tcps_fin_wait_2_flush_interval;
2393707e74bcSKacheong Poon
2394707e74bcSKacheong Poon tcp->tcp_ka_interval = tcps->tcps_keepalive_interval;
2395707e74bcSKacheong Poon tcp->tcp_ka_abort_thres = tcps->tcps_keepalive_abort_interval;
23963d0a255cSGarrett D'Amore tcp->tcp_ka_cnt = 0;
23973d0a255cSGarrett D'Amore tcp->tcp_ka_rinterval = 0;
2398707e74bcSKacheong Poon
2399707e74bcSKacheong Poon /*
2400707e74bcSKacheong Poon * Default value of tcp_init_cwnd is 0, so no need to set here
2401707e74bcSKacheong Poon * if parent is NULL. But we need to inherit it from parent.
2402707e74bcSKacheong Poon */
2403707e74bcSKacheong Poon } else {
2404707e74bcSKacheong Poon /* Inherit various TCP parameters from the parent. */
240545a4b79dSSebastien Roy tcp->tcp_cc_algo = parent->tcp_cc_algo;
240645a4b79dSSebastien Roy
2407707e74bcSKacheong Poon tcp->tcp_naglim = parent->tcp_naglim;
2408707e74bcSKacheong Poon
2409707e74bcSKacheong Poon tcp->tcp_rto_initial = parent->tcp_rto_initial;
2410707e74bcSKacheong Poon tcp->tcp_rto_min = parent->tcp_rto_min;
2411707e74bcSKacheong Poon tcp->tcp_rto_max = parent->tcp_rto_max;
2412707e74bcSKacheong Poon
2413707e74bcSKacheong Poon tcp->tcp_first_ctimer_threshold =
2414707e74bcSKacheong Poon parent->tcp_first_ctimer_threshold;
2415707e74bcSKacheong Poon tcp->tcp_second_ctimer_threshold =
2416707e74bcSKacheong Poon parent->tcp_second_ctimer_threshold;
2417707e74bcSKacheong Poon tcp->tcp_first_timer_threshold =
2418707e74bcSKacheong Poon parent->tcp_first_timer_threshold;
2419707e74bcSKacheong Poon tcp->tcp_second_timer_threshold =
2420707e74bcSKacheong Poon parent->tcp_second_timer_threshold;
2421707e74bcSKacheong Poon
2422707e74bcSKacheong Poon tcp->tcp_fin_wait_2_flush_interval =
2423707e74bcSKacheong Poon parent->tcp_fin_wait_2_flush_interval;
2424861fa149SNils Nieuwejaar tcp->tcp_quickack = parent->tcp_quickack;
2425*1edba515SAndy Fiddaman tcp->tcp_md5sig = parent->tcp_md5sig;
2426707e74bcSKacheong Poon
2427707e74bcSKacheong Poon tcp->tcp_ka_interval = parent->tcp_ka_interval;
2428707e74bcSKacheong Poon tcp->tcp_ka_abort_thres = parent->tcp_ka_abort_thres;
242918576aacSDan McDonald tcp->tcp_ka_cnt = parent->tcp_ka_cnt;
243018576aacSDan McDonald tcp->tcp_ka_rinterval = parent->tcp_ka_rinterval;
2431707e74bcSKacheong Poon
2432707e74bcSKacheong Poon tcp->tcp_init_cwnd = parent->tcp_init_cwnd;
2433707e74bcSKacheong Poon }
2434707e74bcSKacheong Poon
243545a4b79dSSebastien Roy if (tcp->tcp_cc_algo->cb_init != NULL)
243645a4b79dSSebastien Roy VERIFY(tcp->tcp_cc_algo->cb_init(&tcp->tcp_ccv) == 0);
243745a4b79dSSebastien Roy
24387c478bd9Sstevel@tonic-gate /*
24397c478bd9Sstevel@tonic-gate * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO
24407c478bd9Sstevel@tonic-gate * will be close to tcp_rexmit_interval_initial. By doing this, we
24417c478bd9Sstevel@tonic-gate * allow the algorithm to adjust slowly to large fluctuations of RTT
24427c478bd9Sstevel@tonic-gate * during first few transmissions of a connection as seen in slow
24437c478bd9Sstevel@tonic-gate * links.
24447c478bd9Sstevel@tonic-gate */
2445c12492cfSSebastien Roy tcp->tcp_rtt_sa = MSEC2NSEC(tcp->tcp_rto_initial) << 2;
2446c12492cfSSebastien Roy tcp->tcp_rtt_sd = MSEC2NSEC(tcp->tcp_rto_initial) >> 1;
2447c12492cfSSebastien Roy tcp->tcp_rto = tcp_calculate_rto(tcp, tcps,
2448c12492cfSSebastien Roy tcps->tcps_conn_grace_period);
2449707e74bcSKacheong Poon
24507c478bd9Sstevel@tonic-gate tcp->tcp_timer_backoff = 0;
24517c478bd9Sstevel@tonic-gate tcp->tcp_ms_we_have_waited = 0;
2452d3d50737SRafael Vanoni tcp->tcp_last_recv_time = ddi_get_lbolt();
2453f4b3ec61Sdh155122 tcp->tcp_cwnd_max = tcps->tcps_cwnd_max_;
245443d18f1cSpriyanka tcp->tcp_cwnd_ssthresh = TCP_MAX_LARGEWIN;
24557c478bd9Sstevel@tonic-gate
2456bd670b35SErik Nordmark tcp->tcp_maxpsz_multiplier = tcps->tcps_maxpsz_multiplier;
24577c478bd9Sstevel@tonic-gate
2458bd670b35SErik Nordmark /* NOTE: ISS is now set in tcp_set_destination(). */
24597c478bd9Sstevel@tonic-gate
2460ff550d0eSmasputra /* Reset fusion-related fields */
24617c478bd9Sstevel@tonic-gate tcp->tcp_fused = B_FALSE;
24627c478bd9Sstevel@tonic-gate tcp->tcp_unfusable = B_FALSE;
24637c478bd9Sstevel@tonic-gate tcp->tcp_fused_sigurg = B_FALSE;
24647c478bd9Sstevel@tonic-gate tcp->tcp_loopback_peer = NULL;
24657c478bd9Sstevel@tonic-gate
2466bd670b35SErik Nordmark /* We rebuild the header template on the next connect/conn_request */
2467bd670b35SErik Nordmark
2468bd670b35SErik Nordmark connp->conn_mlp_type = mlptSingle;
24697c478bd9Sstevel@tonic-gate
24707c478bd9Sstevel@tonic-gate /*
24717c478bd9Sstevel@tonic-gate * Init the window scale to the max so tcp_rwnd_set() won't pare
2472bd670b35SErik Nordmark * down tcp_rwnd. tcp_set_destination() will set the right value later.
24737c478bd9Sstevel@tonic-gate */
24747c478bd9Sstevel@tonic-gate tcp->tcp_rcv_ws = TCP_MAX_WINSHIFT;
2475bd670b35SErik Nordmark tcp->tcp_rwnd = connp->conn_rcvbuf;
24767c478bd9Sstevel@tonic-gate
24777c478bd9Sstevel@tonic-gate tcp->tcp_cork = B_FALSE;
24787c478bd9Sstevel@tonic-gate /*
2479bd670b35SErik Nordmark * Init the tcp_debug option if it wasn't already set. This value
2480bd670b35SErik Nordmark * determines whether TCP
24817c478bd9Sstevel@tonic-gate * calls strlog() to print out debug messages. Doing this
24827c478bd9Sstevel@tonic-gate * initialization here means that this value is not inherited thru
24837c478bd9Sstevel@tonic-gate * tcp_reinit().
24847c478bd9Sstevel@tonic-gate */
2485bd670b35SErik Nordmark if (!connp->conn_debug)
2486bd670b35SErik Nordmark connp->conn_debug = tcps->tcps_dbg;
24877c478bd9Sstevel@tonic-gate }
24887c478bd9Sstevel@tonic-gate
2489bd670b35SErik Nordmark /*
2490bd670b35SErik Nordmark * Update the TCP connection according to change of PMTU.
2491bd670b35SErik Nordmark *
2492bd670b35SErik Nordmark * Path MTU might have changed by either increase or decrease, so need to
2493bd670b35SErik Nordmark * adjust the MSS based on the value of ixa_pmtu. No need to handle tiny
2494bd670b35SErik Nordmark * or negative MSS, since tcp_mss_set() will do it.
2495bd670b35SErik Nordmark */
2496721fffe3SKacheong Poon void
tcp_update_pmtu(tcp_t * tcp,boolean_t decrease_only)2497bd670b35SErik Nordmark tcp_update_pmtu(tcp_t *tcp, boolean_t decrease_only)
2498bd670b35SErik Nordmark {
2499bd670b35SErik Nordmark uint32_t pmtu;
2500bd670b35SErik Nordmark int32_t mss;
2501bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
2502bd670b35SErik Nordmark ip_xmit_attr_t *ixa = connp->conn_ixa;
2503bd670b35SErik Nordmark iaflags_t ixaflags;
2504bd670b35SErik Nordmark
2505bd670b35SErik Nordmark if (tcp->tcp_tcps->tcps_ignore_path_mtu)
2506bd670b35SErik Nordmark return;
2507bd670b35SErik Nordmark
2508bd670b35SErik Nordmark if (tcp->tcp_state < TCPS_ESTABLISHED)
2509bd670b35SErik Nordmark return;
2510bd670b35SErik Nordmark
2511bd670b35SErik Nordmark /*
2512bd670b35SErik Nordmark * Always call ip_get_pmtu() to make sure that IP has updated
2513bd670b35SErik Nordmark * ixa_flags properly.
2514bd670b35SErik Nordmark */
2515bd670b35SErik Nordmark pmtu = ip_get_pmtu(ixa);
2516bd670b35SErik Nordmark ixaflags = ixa->ixa_flags;
2517bd670b35SErik Nordmark
2518bd670b35SErik Nordmark /*
2519bd670b35SErik Nordmark * Calculate the MSS by decreasing the PMTU by conn_ht_iphc_len and
2520bd670b35SErik Nordmark * IPsec overhead if applied. Make sure to use the most recent
2521bd670b35SErik Nordmark * IPsec information.
2522bd670b35SErik Nordmark */
2523bd670b35SErik Nordmark mss = pmtu - connp->conn_ht_iphc_len - conn_ipsec_length(connp);
2524bd670b35SErik Nordmark
2525bd670b35SErik Nordmark /*
2526bd670b35SErik Nordmark * Nothing to change, so just return.
2527bd670b35SErik Nordmark */
2528bd670b35SErik Nordmark if (mss == tcp->tcp_mss)
2529bd670b35SErik Nordmark return;
2530bd670b35SErik Nordmark
2531bd670b35SErik Nordmark /*
2532bd670b35SErik Nordmark * Currently, for ICMP errors, only PMTU decrease is handled.
2533bd670b35SErik Nordmark */
2534bd670b35SErik Nordmark if (mss > tcp->tcp_mss && decrease_only)
2535bd670b35SErik Nordmark return;
2536bd670b35SErik Nordmark
2537bd670b35SErik Nordmark DTRACE_PROBE2(tcp_update_pmtu, int32_t, tcp->tcp_mss, uint32_t, mss);
2538bd670b35SErik Nordmark
2539bd670b35SErik Nordmark /*
2540bd670b35SErik Nordmark * Update ixa_fragsize and ixa_pmtu.
2541bd670b35SErik Nordmark */
2542bd670b35SErik Nordmark ixa->ixa_fragsize = ixa->ixa_pmtu = pmtu;
2543bd670b35SErik Nordmark
2544bd670b35SErik Nordmark /*
2545bd670b35SErik Nordmark * Adjust MSS and all relevant variables.
2546bd670b35SErik Nordmark */
2547bd670b35SErik Nordmark tcp_mss_set(tcp, mss);
2548bd670b35SErik Nordmark
2549bd670b35SErik Nordmark /*
2550bd670b35SErik Nordmark * If the PMTU is below the min size maintained by IP, then ip_get_pmtu
2551bd670b35SErik Nordmark * has set IXAF_PMTU_TOO_SMALL and cleared IXAF_PMTU_IPV4_DF. Since TCP
2552bd670b35SErik Nordmark * has a (potentially different) min size we do the same. Make sure to
2553bd670b35SErik Nordmark * clear IXAF_DONTFRAG, which is used by IP to decide whether to
2554bd670b35SErik Nordmark * fragment the packet.
2555bd670b35SErik Nordmark *
2556bd670b35SErik Nordmark * LSO over IPv6 can not be fragmented. So need to disable LSO
2557bd670b35SErik Nordmark * when IPv6 fragmentation is needed.
2558bd670b35SErik Nordmark */
2559bd670b35SErik Nordmark if (mss < tcp->tcp_tcps->tcps_mss_min)
2560bd670b35SErik Nordmark ixaflags |= IXAF_PMTU_TOO_SMALL;
2561bd670b35SErik Nordmark
2562bd670b35SErik Nordmark if (ixaflags & IXAF_PMTU_TOO_SMALL)
2563bd670b35SErik Nordmark ixaflags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
2564bd670b35SErik Nordmark
2565bd670b35SErik Nordmark if ((connp->conn_ipversion == IPV4_VERSION) &&
2566bd670b35SErik Nordmark !(ixaflags & IXAF_PMTU_IPV4_DF)) {
2567bd670b35SErik Nordmark tcp->tcp_ipha->ipha_fragment_offset_and_flags = 0;
2568bd670b35SErik Nordmark }
2569bd670b35SErik Nordmark ixa->ixa_flags = ixaflags;
2570bd670b35SErik Nordmark }
2571bd670b35SErik Nordmark
2572ff550d0eSmasputra int
tcp_maxpsz_set(tcp_t * tcp,boolean_t set_maxblk)25737c478bd9Sstevel@tonic-gate tcp_maxpsz_set(tcp_t *tcp, boolean_t set_maxblk)
25747c478bd9Sstevel@tonic-gate {
2575bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
2576bd670b35SErik Nordmark queue_t *q = connp->conn_rq;
25777c478bd9Sstevel@tonic-gate int32_t mss = tcp->tcp_mss;
25787c478bd9Sstevel@tonic-gate int maxpsz;
25797c478bd9Sstevel@tonic-gate
25807c478bd9Sstevel@tonic-gate if (TCP_IS_DETACHED(tcp))
25817c478bd9Sstevel@tonic-gate return (mss);
2582ff550d0eSmasputra if (tcp->tcp_fused) {
258379c0745dSRao Shoaib maxpsz = tcp_fuse_maxpsz(tcp);
2584ff550d0eSmasputra mss = INFPSZ;
2585bd670b35SErik Nordmark } else if (tcp->tcp_maxpsz_multiplier == 0) {
25867c478bd9Sstevel@tonic-gate /*
25877c478bd9Sstevel@tonic-gate * Set the sd_qn_maxpsz according to the socket send buffer
25887c478bd9Sstevel@tonic-gate * size, and sd_maxblk to INFPSZ (-1). This will essentially
25897c478bd9Sstevel@tonic-gate * instruct the stream head to copyin user data into contiguous
25907c478bd9Sstevel@tonic-gate * kernel-allocated buffers without breaking it up into smaller
25917c478bd9Sstevel@tonic-gate * chunks. We round up the buffer size to the nearest SMSS.
25927c478bd9Sstevel@tonic-gate */
2593bd670b35SErik Nordmark maxpsz = MSS_ROUNDUP(connp->conn_sndbuf, mss);
25947c478bd9Sstevel@tonic-gate mss = INFPSZ;
25957c478bd9Sstevel@tonic-gate } else {
25967c478bd9Sstevel@tonic-gate /*
25977c478bd9Sstevel@tonic-gate * Set sd_qn_maxpsz to approx half the (receivers) buffer
25987c478bd9Sstevel@tonic-gate * (and a multiple of the mss). This instructs the stream
25997c478bd9Sstevel@tonic-gate * head to break down larger than SMSS writes into SMSS-
26007c478bd9Sstevel@tonic-gate * size mblks, up to tcp_maxpsz_multiplier mblks at a time.
26017c478bd9Sstevel@tonic-gate */
2602bd670b35SErik Nordmark maxpsz = tcp->tcp_maxpsz_multiplier * mss;
2603bd670b35SErik Nordmark if (maxpsz > connp->conn_sndbuf / 2) {
2604bd670b35SErik Nordmark maxpsz = connp->conn_sndbuf / 2;
26057c478bd9Sstevel@tonic-gate /* Round up to nearest mss */
26067c478bd9Sstevel@tonic-gate maxpsz = MSS_ROUNDUP(maxpsz, mss);
26077c478bd9Sstevel@tonic-gate }
26087c478bd9Sstevel@tonic-gate }
26090f1702c5SYu Xiangning
26100f1702c5SYu Xiangning (void) proto_set_maxpsz(q, connp, maxpsz);
2611bd670b35SErik Nordmark if (!(IPCL_IS_NONSTR(connp)))
2612bd670b35SErik Nordmark connp->conn_wq->q_maxpsz = maxpsz;
26137c478bd9Sstevel@tonic-gate if (set_maxblk)
26140f1702c5SYu Xiangning (void) proto_set_tx_maxblk(q, connp, mss);
26157c478bd9Sstevel@tonic-gate return (mss);
26167c478bd9Sstevel@tonic-gate }
26177c478bd9Sstevel@tonic-gate
2618fc80c0dfSnordmark /* For /dev/tcp aka AF_INET open */
26197c478bd9Sstevel@tonic-gate static int
tcp_openv4(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)2620fc80c0dfSnordmark tcp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2621fc80c0dfSnordmark {
2622fc80c0dfSnordmark return (tcp_open(q, devp, flag, sflag, credp, B_FALSE));
2623fc80c0dfSnordmark }
2624fc80c0dfSnordmark
2625fc80c0dfSnordmark /* For /dev/tcp6 aka AF_INET6 open */
2626fc80c0dfSnordmark static int
tcp_openv6(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp)2627fc80c0dfSnordmark tcp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
2628fc80c0dfSnordmark {
2629fc80c0dfSnordmark return (tcp_open(q, devp, flag, sflag, credp, B_TRUE));
2630fc80c0dfSnordmark }
2631fc80c0dfSnordmark
2632721fffe3SKacheong Poon conn_t *
tcp_create_common(cred_t * credp,boolean_t isv6,boolean_t issocket,int * errorp)2633bd670b35SErik Nordmark tcp_create_common(cred_t *credp, boolean_t isv6, boolean_t issocket,
2634bd670b35SErik Nordmark int *errorp)
26357c478bd9Sstevel@tonic-gate {
26367c478bd9Sstevel@tonic-gate tcp_t *tcp = NULL;
26377c478bd9Sstevel@tonic-gate conn_t *connp;
2638f4b3ec61Sdh155122 zoneid_t zoneid;
26390f1702c5SYu Xiangning tcp_stack_t *tcps;
26400f1702c5SYu Xiangning squeue_t *sqp;
2641108322fbScarlsonj
26420f1702c5SYu Xiangning ASSERT(errorp != NULL);
26430f1702c5SYu Xiangning /*
26440f1702c5SYu Xiangning * Find the proper zoneid and netstack.
26450f1702c5SYu Xiangning */
2646f4b3ec61Sdh155122 /*
2647f4b3ec61Sdh155122 * Special case for install: miniroot needs to be able to
2648f4b3ec61Sdh155122 * access files via NFS as though it were always in the
2649f4b3ec61Sdh155122 * global zone.
2650f4b3ec61Sdh155122 */
2651f4b3ec61Sdh155122 if (credp == kcred && nfs_global_client_only != 0) {
2652f4b3ec61Sdh155122 zoneid = GLOBAL_ZONEID;
2653f4b3ec61Sdh155122 tcps = netstack_find_by_stackid(GLOBAL_NETSTACKID)->
2654f4b3ec61Sdh155122 netstack_tcp;
2655f4b3ec61Sdh155122 ASSERT(tcps != NULL);
2656f4b3ec61Sdh155122 } else {
2657f4b3ec61Sdh155122 netstack_t *ns;
2658634e26ecSCasper H.S. Dik int err;
2659634e26ecSCasper H.S. Dik
2660634e26ecSCasper H.S. Dik if ((err = secpolicy_basic_net_access(credp)) != 0) {
2661634e26ecSCasper H.S. Dik *errorp = err;
2662634e26ecSCasper H.S. Dik return (NULL);
2663634e26ecSCasper H.S. Dik }
2664f4b3ec61Sdh155122
2665f4b3ec61Sdh155122 ns = netstack_find_by_cred(credp);
2666f4b3ec61Sdh155122 ASSERT(ns != NULL);
2667f4b3ec61Sdh155122 tcps = ns->netstack_tcp;
2668f4b3ec61Sdh155122 ASSERT(tcps != NULL);
2669f4b3ec61Sdh155122
2670f4b3ec61Sdh155122 /*
2671f4b3ec61Sdh155122 * For exclusive stacks we set the zoneid to zero
2672f4b3ec61Sdh155122 * to make TCP operate as if in the global zone.
2673f4b3ec61Sdh155122 */
2674f4b3ec61Sdh155122 if (tcps->tcps_netstack->netstack_stackid !=
2675f4b3ec61Sdh155122 GLOBAL_NETSTACKID)
2676f4b3ec61Sdh155122 zoneid = GLOBAL_ZONEID;
2677f4b3ec61Sdh155122 else
2678f4b3ec61Sdh155122 zoneid = crgetzoneid(credp);
2679f4b3ec61Sdh155122 }
26807c478bd9Sstevel@tonic-gate
26810f1702c5SYu Xiangning sqp = IP_SQUEUE_GET((uint_t)gethrtime());
268245a4b79dSSebastien Roy connp = tcp_get_conn(sqp, tcps);
2683f4b3ec61Sdh155122 /*
2684f4b3ec61Sdh155122 * Both tcp_get_conn and netstack_find_by_cred incremented refcnt,
2685f4b3ec61Sdh155122 * so we drop it by one.
2686f4b3ec61Sdh155122 */
2687f4b3ec61Sdh155122 netstack_rele(tcps->tcps_netstack);
26887c478bd9Sstevel@tonic-gate if (connp == NULL) {
26890f1702c5SYu Xiangning *errorp = ENOSR;
26900f1702c5SYu Xiangning return (NULL);
26917c478bd9Sstevel@tonic-gate }
2692bd670b35SErik Nordmark ASSERT(connp->conn_ixa->ixa_protocol == connp->conn_proto);
2693bd670b35SErik Nordmark
26940f1702c5SYu Xiangning connp->conn_sqp = sqp;
2695da14cebeSEric Cheng connp->conn_initial_sqp = connp->conn_sqp;
2696bd670b35SErik Nordmark connp->conn_ixa->ixa_sqp = connp->conn_sqp;
26977c478bd9Sstevel@tonic-gate tcp = connp->conn_tcp;
26987c478bd9Sstevel@tonic-gate
26997c478bd9Sstevel@tonic-gate /*
2700bd670b35SErik Nordmark * Besides asking IP to set the checksum for us, have conn_ip_output
2701bd670b35SErik Nordmark * to do the following checks when necessary:
2702bd670b35SErik Nordmark *
2703bd670b35SErik Nordmark * IXAF_VERIFY_SOURCE: drop packets when our outer source goes invalid
2704bd670b35SErik Nordmark * IXAF_VERIFY_PMTU: verify PMTU changes
2705bd670b35SErik Nordmark * IXAF_VERIFY_LSO: verify LSO capability changes
27067c478bd9Sstevel@tonic-gate */
2707bd670b35SErik Nordmark connp->conn_ixa->ixa_flags |= IXAF_SET_ULP_CKSUM | IXAF_VERIFY_SOURCE |
2708bd670b35SErik Nordmark IXAF_VERIFY_PMTU | IXAF_VERIFY_LSO;
2709bd670b35SErik Nordmark
2710bd670b35SErik Nordmark if (!tcps->tcps_dev_flow_ctl)
2711bd670b35SErik Nordmark connp->conn_ixa->ixa_flags |= IXAF_NO_DEV_FLOW_CTL;
2712bd670b35SErik Nordmark
2713bd670b35SErik Nordmark if (isv6) {
2714bd670b35SErik Nordmark connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
2715bd670b35SErik Nordmark connp->conn_ipversion = IPV6_VERSION;
2716bd670b35SErik Nordmark connp->conn_family = AF_INET6;
2717bd670b35SErik Nordmark tcp->tcp_mss = tcps->tcps_mss_def_ipv6;
2718bd670b35SErik Nordmark connp->conn_default_ttl = tcps->tcps_ipv6_hoplimit;
2719bd670b35SErik Nordmark } else {
2720bd670b35SErik Nordmark connp->conn_ipversion = IPV4_VERSION;
2721bd670b35SErik Nordmark connp->conn_family = AF_INET;
2722bd670b35SErik Nordmark tcp->tcp_mss = tcps->tcps_mss_def_ipv4;
2723bd670b35SErik Nordmark connp->conn_default_ttl = tcps->tcps_ipv4_ttl;
2724bd670b35SErik Nordmark }
2725bd670b35SErik Nordmark connp->conn_xmit_ipp.ipp_unicast_hops = connp->conn_default_ttl;
2726bd670b35SErik Nordmark
2727bd670b35SErik Nordmark crhold(credp);
2728bd670b35SErik Nordmark connp->conn_cred = credp;
2729bd670b35SErik Nordmark connp->conn_cpid = curproc->p_pid;
2730d3d50737SRafael Vanoni connp->conn_open_time = ddi_get_lbolt64();
2731bd670b35SErik Nordmark
273276a1033eSErik Nordmark /* Cache things in the ixa without any refhold */
2733be4c8f74SErik Nordmark ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
273476a1033eSErik Nordmark connp->conn_ixa->ixa_cred = credp;
273576a1033eSErik Nordmark connp->conn_ixa->ixa_cpid = connp->conn_cpid;
273676a1033eSErik Nordmark
27377c478bd9Sstevel@tonic-gate connp->conn_zoneid = zoneid;
2738bd670b35SErik Nordmark /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
2739bd670b35SErik Nordmark connp->conn_ixa->ixa_zoneid = zoneid;
274045916cd2Sjpk connp->conn_mlp_type = mlptSingle;
2741f4b3ec61Sdh155122 ASSERT(connp->conn_netstack == tcps->tcps_netstack);
2742f4b3ec61Sdh155122 ASSERT(tcp->tcp_tcps == tcps);
274345916cd2Sjpk
274445916cd2Sjpk /*
274545916cd2Sjpk * If the caller has the process-wide flag set, then default to MAC
274645916cd2Sjpk * exempt mode. This allows read-down to unlabeled hosts.
274745916cd2Sjpk */
274845916cd2Sjpk if (getpflags(NET_MAC_AWARE, credp) != 0)
27495d3b8cb7SBill Sommerfeld connp->conn_mac_mode = CONN_MAC_AWARE;
27507c478bd9Sstevel@tonic-gate
2751bd670b35SErik Nordmark connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
2752bd670b35SErik Nordmark
27530f1702c5SYu Xiangning if (issocket) {
27540f1702c5SYu Xiangning tcp->tcp_issocket = 1;
27550f1702c5SYu Xiangning }
27560f1702c5SYu Xiangning
2757bd670b35SErik Nordmark connp->conn_rcvbuf = tcps->tcps_recv_hiwat;
2758bd670b35SErik Nordmark connp->conn_sndbuf = tcps->tcps_xmit_hiwat;
2759299625c6SSebastien Roy if (tcps->tcps_snd_lowat_fraction != 0) {
2760299625c6SSebastien Roy connp->conn_sndlowat = connp->conn_sndbuf /
2761299625c6SSebastien Roy tcps->tcps_snd_lowat_fraction;
2762299625c6SSebastien Roy } else {
2763bd670b35SErik Nordmark connp->conn_sndlowat = tcps->tcps_xmit_lowat;
2764299625c6SSebastien Roy }
2765bd670b35SErik Nordmark connp->conn_so_type = SOCK_STREAM;
2766bd670b35SErik Nordmark connp->conn_wroff = connp->conn_ht_iphc_allocated +
2767bd670b35SErik Nordmark tcps->tcps_wroff_xtra;
27680f1702c5SYu Xiangning
27690f1702c5SYu Xiangning SOCK_CONNID_INIT(tcp->tcp_connid);
27709cd928feSAlan Maguire /* DTrace ignores this - it isn't a tcp:::state-change */
2771bd670b35SErik Nordmark tcp->tcp_state = TCPS_IDLE;
2772707e74bcSKacheong Poon tcp_init_values(tcp, NULL);
27730f1702c5SYu Xiangning return (connp);
27740f1702c5SYu Xiangning }
27750f1702c5SYu Xiangning
27760f1702c5SYu Xiangning static int
tcp_open(queue_t * q,dev_t * devp,int flag,int sflag,cred_t * credp,boolean_t isv6)27770f1702c5SYu Xiangning tcp_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
27780f1702c5SYu Xiangning boolean_t isv6)
27790f1702c5SYu Xiangning {
27800f1702c5SYu Xiangning tcp_t *tcp = NULL;
27810f1702c5SYu Xiangning conn_t *connp = NULL;
27820f1702c5SYu Xiangning int err;
27830f1702c5SYu Xiangning vmem_t *minor_arena = NULL;
27840f1702c5SYu Xiangning dev_t conn_dev;
27850f1702c5SYu Xiangning boolean_t issocket;
27860f1702c5SYu Xiangning
27870f1702c5SYu Xiangning if (q->q_ptr != NULL)
27880f1702c5SYu Xiangning return (0);
27890f1702c5SYu Xiangning
27900f1702c5SYu Xiangning if (sflag == MODOPEN)
27910f1702c5SYu Xiangning return (EINVAL);
27920f1702c5SYu Xiangning
27930f1702c5SYu Xiangning if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
27940f1702c5SYu Xiangning ((conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
27950f1702c5SYu Xiangning minor_arena = ip_minor_arena_la;
27960f1702c5SYu Xiangning } else {
27970f1702c5SYu Xiangning /*
27980f1702c5SYu Xiangning * Either minor numbers in the large arena were exhausted
27990f1702c5SYu Xiangning * or a non socket application is doing the open.
28000f1702c5SYu Xiangning * Try to allocate from the small arena.
28010f1702c5SYu Xiangning */
28020f1702c5SYu Xiangning if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
28030f1702c5SYu Xiangning return (EBUSY);
28040f1702c5SYu Xiangning }
28050f1702c5SYu Xiangning minor_arena = ip_minor_arena_sa;
28060f1702c5SYu Xiangning }
28070f1702c5SYu Xiangning
28080f1702c5SYu Xiangning ASSERT(minor_arena != NULL);
28090f1702c5SYu Xiangning
28100f1702c5SYu Xiangning *devp = makedevice(getmajor(*devp), (minor_t)conn_dev);
28110f1702c5SYu Xiangning
28120f1702c5SYu Xiangning if (flag & SO_FALLBACK) {
28130f1702c5SYu Xiangning /*
28140f1702c5SYu Xiangning * Non streams socket needs a stream to fallback to
28150f1702c5SYu Xiangning */
28160f1702c5SYu Xiangning RD(q)->q_ptr = (void *)conn_dev;
28170f1702c5SYu Xiangning WR(q)->q_qinfo = &tcp_fallback_sock_winit;
28180f1702c5SYu Xiangning WR(q)->q_ptr = (void *)minor_arena;
28190f1702c5SYu Xiangning qprocson(q);
28200f1702c5SYu Xiangning return (0);
28210f1702c5SYu Xiangning } else if (flag & SO_ACCEPTOR) {
28220f1702c5SYu Xiangning q->q_qinfo = &tcp_acceptor_rinit;
28230f1702c5SYu Xiangning /*
28240f1702c5SYu Xiangning * the conn_dev and minor_arena will be subsequently used by
2825bd670b35SErik Nordmark * tcp_tli_accept() and tcp_tpi_close_accept() to figure out
2826eead73cfSRao Shoaib * the minor device number for this connection from the q_ptr.
28270f1702c5SYu Xiangning */
28280f1702c5SYu Xiangning RD(q)->q_ptr = (void *)conn_dev;
28290f1702c5SYu Xiangning WR(q)->q_qinfo = &tcp_acceptor_winit;
28300f1702c5SYu Xiangning WR(q)->q_ptr = (void *)minor_arena;
28310f1702c5SYu Xiangning qprocson(q);
28320f1702c5SYu Xiangning return (0);
28330f1702c5SYu Xiangning }
28340f1702c5SYu Xiangning
28350f1702c5SYu Xiangning issocket = flag & SO_SOCKSTR;
2836bd670b35SErik Nordmark connp = tcp_create_common(credp, isv6, issocket, &err);
28370f1702c5SYu Xiangning
28380f1702c5SYu Xiangning if (connp == NULL) {
28390f1702c5SYu Xiangning inet_minor_free(minor_arena, conn_dev);
28400f1702c5SYu Xiangning q->q_ptr = WR(q)->q_ptr = NULL;
28410f1702c5SYu Xiangning return (err);
28420f1702c5SYu Xiangning }
28430f1702c5SYu Xiangning
2844bd670b35SErik Nordmark connp->conn_rq = q;
2845bd670b35SErik Nordmark connp->conn_wq = WR(q);
28460f1702c5SYu Xiangning q->q_ptr = WR(q)->q_ptr = connp;
28470f1702c5SYu Xiangning
28487c478bd9Sstevel@tonic-gate connp->conn_dev = conn_dev;
2849aa92d85bSgt145670 connp->conn_minor_arena = minor_arena;
28507c478bd9Sstevel@tonic-gate
2851fc80c0dfSnordmark ASSERT(q->q_qinfo == &tcp_rinitv4 || q->q_qinfo == &tcp_rinitv6);
28527c478bd9Sstevel@tonic-gate ASSERT(WR(q)->q_qinfo == &tcp_winit);
28537c478bd9Sstevel@tonic-gate
2854eead73cfSRao Shoaib tcp = connp->conn_tcp;
2855eead73cfSRao Shoaib
28560f1702c5SYu Xiangning if (issocket) {
28577c478bd9Sstevel@tonic-gate WR(q)->q_qinfo = &tcp_sock_winit;
28587c478bd9Sstevel@tonic-gate } else {
28597c478bd9Sstevel@tonic-gate #ifdef _ILP32
28607c478bd9Sstevel@tonic-gate tcp->tcp_acceptor_id = (t_uscalar_t)RD(q);
28617c478bd9Sstevel@tonic-gate #else
28627c478bd9Sstevel@tonic-gate tcp->tcp_acceptor_id = conn_dev;
28637c478bd9Sstevel@tonic-gate #endif /* _ILP32 */
28647c478bd9Sstevel@tonic-gate tcp_acceptor_hash_insert(tcp->tcp_acceptor_id, tcp);
28657c478bd9Sstevel@tonic-gate }
28667c478bd9Sstevel@tonic-gate
28677c478bd9Sstevel@tonic-gate /*
28687c478bd9Sstevel@tonic-gate * Put the ref for TCP. Ref for IP was already put
28697c478bd9Sstevel@tonic-gate * by ipcl_conn_create. Also Make the conn_t globally
28707c478bd9Sstevel@tonic-gate * visible to walkers
28717c478bd9Sstevel@tonic-gate */
28727c478bd9Sstevel@tonic-gate mutex_enter(&connp->conn_lock);
28737c478bd9Sstevel@tonic-gate CONN_INC_REF_LOCKED(connp);
28747c478bd9Sstevel@tonic-gate ASSERT(connp->conn_ref == 2);
28757c478bd9Sstevel@tonic-gate connp->conn_state_flags &= ~CONN_INCIPIENT;
28767c478bd9Sstevel@tonic-gate mutex_exit(&connp->conn_lock);
28777c478bd9Sstevel@tonic-gate
28787c478bd9Sstevel@tonic-gate qprocson(q);
28797c478bd9Sstevel@tonic-gate return (0);
28807c478bd9Sstevel@tonic-gate }
28817c478bd9Sstevel@tonic-gate
28827c478bd9Sstevel@tonic-gate /*
2883bd670b35SErik Nordmark * Build/update the tcp header template (in conn_ht_iphc) based on
2884bd670b35SErik Nordmark * conn_xmit_ipp. The headers include ip6_t, any extension
28857c478bd9Sstevel@tonic-gate * headers, and the maximum size tcp header (to avoid reallocation
28867c478bd9Sstevel@tonic-gate * on the fly for additional tcp options).
2887bd670b35SErik Nordmark *
2888bd670b35SErik Nordmark * Assumes the caller has already set conn_{faddr,laddr,fport,lport,flowinfo}.
28897c478bd9Sstevel@tonic-gate * Returns failure if can't allocate memory.
28907c478bd9Sstevel@tonic-gate */
2891721fffe3SKacheong Poon int
tcp_build_hdrs(tcp_t * tcp)28920f1702c5SYu Xiangning tcp_build_hdrs(tcp_t *tcp)
28937c478bd9Sstevel@tonic-gate {
2894f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
28950f1702c5SYu Xiangning conn_t *connp = tcp->tcp_connp;
2896294f39e4SErik Nordmark char buf[TCP_MAX_HDR_LENGTH];
2897294f39e4SErik Nordmark uint_t buflen;
2898294f39e4SErik Nordmark uint_t ulplen = TCP_MIN_HEADER_LENGTH;
2899294f39e4SErik Nordmark uint_t extralen = TCP_MAX_TCP_OPTIONS_LENGTH;
2900bd670b35SErik Nordmark tcpha_t *tcpha;
2901bd670b35SErik Nordmark uint32_t cksum;
2902bd670b35SErik Nordmark int error;
2903bd670b35SErik Nordmark
2904294f39e4SErik Nordmark /*
2905294f39e4SErik Nordmark * We might be called after the connection is set up, and we might
2906294f39e4SErik Nordmark * have TS options already in the TCP header. Thus we save any
2907294f39e4SErik Nordmark * existing tcp header.
2908294f39e4SErik Nordmark */
2909294f39e4SErik Nordmark buflen = connp->conn_ht_ulp_len;
2910294f39e4SErik Nordmark if (buflen != 0) {
2911294f39e4SErik Nordmark bcopy(connp->conn_ht_ulp, buf, buflen);
2912294f39e4SErik Nordmark extralen -= buflen - ulplen;
2913294f39e4SErik Nordmark ulplen = buflen;
2914294f39e4SErik Nordmark }
2915294f39e4SErik Nordmark
2916bd670b35SErik Nordmark /* Grab lock to satisfy ASSERT; TCP is serialized using squeue */
2917bd670b35SErik Nordmark mutex_enter(&connp->conn_lock);
2918294f39e4SErik Nordmark error = conn_build_hdr_template(connp, ulplen, extralen,
2919294f39e4SErik Nordmark &connp->conn_laddr_v6, &connp->conn_faddr_v6, connp->conn_flowinfo);
2920bd670b35SErik Nordmark mutex_exit(&connp->conn_lock);
2921bd670b35SErik Nordmark if (error != 0)
2922bd670b35SErik Nordmark return (error);
29237c478bd9Sstevel@tonic-gate
29247c478bd9Sstevel@tonic-gate /*
2925bd670b35SErik Nordmark * Any routing header/option has been massaged. The checksum difference
2926bd670b35SErik Nordmark * is stored in conn_sum for later use.
29277c478bd9Sstevel@tonic-gate */
2928bd670b35SErik Nordmark tcpha = (tcpha_t *)connp->conn_ht_ulp;
2929bd670b35SErik Nordmark tcp->tcp_tcpha = tcpha;
29307c478bd9Sstevel@tonic-gate
2931294f39e4SErik Nordmark /* restore any old tcp header */
2932294f39e4SErik Nordmark if (buflen != 0) {
2933294f39e4SErik Nordmark bcopy(buf, connp->conn_ht_ulp, buflen);
2934294f39e4SErik Nordmark } else {
2935bd670b35SErik Nordmark tcpha->tha_sum = 0;
29367426816eSMark Fenwick tcpha->tha_urp = 0;
29377426816eSMark Fenwick tcpha->tha_ack = 0;
2938bd670b35SErik Nordmark tcpha->tha_offset_and_reserved = (5 << 4);
293943e4e284SErik Nordmark tcpha->tha_lport = connp->conn_lport;
294043e4e284SErik Nordmark tcpha->tha_fport = connp->conn_fport;
29416b7506c7SErik Nordmark }
29427c478bd9Sstevel@tonic-gate
29437c478bd9Sstevel@tonic-gate /*
2944bd670b35SErik Nordmark * IP wants our header length in the checksum field to
2945bd670b35SErik Nordmark * allow it to perform a single pseudo-header+checksum
2946bd670b35SErik Nordmark * calculation on behalf of TCP.
2947bd670b35SErik Nordmark * Include the adjustment for a source route once IP_OPTIONS is set.
29487c478bd9Sstevel@tonic-gate */
2949bd670b35SErik Nordmark cksum = sizeof (tcpha_t) + connp->conn_sum;
2950bd670b35SErik Nordmark cksum = (cksum >> 16) + (cksum & 0xFFFF);
2951bd670b35SErik Nordmark ASSERT(cksum < 0x10000);
2952bd670b35SErik Nordmark tcpha->tha_sum = htons(cksum);
29537c478bd9Sstevel@tonic-gate
2954bd670b35SErik Nordmark if (connp->conn_ipversion == IPV4_VERSION)
2955bd670b35SErik Nordmark tcp->tcp_ipha = (ipha_t *)connp->conn_ht_iphc;
2956bd670b35SErik Nordmark else
2957bd670b35SErik Nordmark tcp->tcp_ip6h = (ip6_t *)connp->conn_ht_iphc;
29587c478bd9Sstevel@tonic-gate
2959bd670b35SErik Nordmark if (connp->conn_ht_iphc_allocated + tcps->tcps_wroff_xtra >
2960bd670b35SErik Nordmark connp->conn_wroff) {
2961bd670b35SErik Nordmark connp->conn_wroff = connp->conn_ht_iphc_allocated +
2962bd670b35SErik Nordmark tcps->tcps_wroff_xtra;
2963bd670b35SErik Nordmark (void) proto_set_tx_wroff(connp->conn_rq, connp,
2964bd670b35SErik Nordmark connp->conn_wroff);
29657c478bd9Sstevel@tonic-gate }
29667c478bd9Sstevel@tonic-gate return (0);
29677c478bd9Sstevel@tonic-gate }
29687c478bd9Sstevel@tonic-gate
29697c478bd9Sstevel@tonic-gate /*
29707c478bd9Sstevel@tonic-gate * tcp_rwnd_set() is called to adjust the receive window to a desired value.
29717c478bd9Sstevel@tonic-gate * We do not allow the receive window to shrink. After setting rwnd,
29727c478bd9Sstevel@tonic-gate * set the flow control hiwat of the stream.
29737c478bd9Sstevel@tonic-gate *
29747c478bd9Sstevel@tonic-gate * This function is called in 2 cases:
29757c478bd9Sstevel@tonic-gate *
2976bd670b35SErik Nordmark * 1) Before data transfer begins, in tcp_input_listener() for accepting a
2977bd670b35SErik Nordmark * connection (passive open) and in tcp_input_data() for active connect.
29787c478bd9Sstevel@tonic-gate * This is called after tcp_mss_set() when the desired MSS value is known.
29797c478bd9Sstevel@tonic-gate * This makes sure that our window size is a mutiple of the other side's
29807c478bd9Sstevel@tonic-gate * MSS.
29817c478bd9Sstevel@tonic-gate * 2) Handling SO_RCVBUF option.
29827c478bd9Sstevel@tonic-gate *
29837c478bd9Sstevel@tonic-gate * It is ASSUMED that the requested size is a multiple of the current MSS.
29847c478bd9Sstevel@tonic-gate *
29857c478bd9Sstevel@tonic-gate * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the
29867c478bd9Sstevel@tonic-gate * user requests so.
29877c478bd9Sstevel@tonic-gate */
298879c0745dSRao Shoaib int
tcp_rwnd_set(tcp_t * tcp,uint32_t rwnd)29897c478bd9Sstevel@tonic-gate tcp_rwnd_set(tcp_t *tcp, uint32_t rwnd)
29907c478bd9Sstevel@tonic-gate {
29917c478bd9Sstevel@tonic-gate uint32_t mss = tcp->tcp_mss;
29927c478bd9Sstevel@tonic-gate uint32_t old_max_rwnd;
29937c478bd9Sstevel@tonic-gate uint32_t max_transmittable_rwnd;
29947c478bd9Sstevel@tonic-gate boolean_t tcp_detached = TCP_IS_DETACHED(tcp);
2995f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
2996bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
29977c478bd9Sstevel@tonic-gate
29981b2702b4SRao Shoaib /*
29991b2702b4SRao Shoaib * Insist on a receive window that is at least
30001b2702b4SRao Shoaib * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid
30011b2702b4SRao Shoaib * funny TCP interactions of Nagle algorithm, SWS avoidance
30021b2702b4SRao Shoaib * and delayed acknowledgement.
30031b2702b4SRao Shoaib */
30041b2702b4SRao Shoaib rwnd = MAX(rwnd, tcps->tcps_recv_hiwat_minmss * mss);
30051b2702b4SRao Shoaib
3006ff550d0eSmasputra if (tcp->tcp_fused) {
3007ff550d0eSmasputra size_t sth_hiwat;
3008ff550d0eSmasputra tcp_t *peer_tcp = tcp->tcp_loopback_peer;
3009ff550d0eSmasputra
3010ff550d0eSmasputra ASSERT(peer_tcp != NULL);
3011ff550d0eSmasputra sth_hiwat = tcp_fuse_set_rcv_hiwat(tcp, rwnd);
30120f1702c5SYu Xiangning if (!tcp_detached) {
3013bd670b35SErik Nordmark (void) proto_set_rx_hiwat(connp->conn_rq, connp,
30140f1702c5SYu Xiangning sth_hiwat);
301579c0745dSRao Shoaib tcp_set_recv_threshold(tcp, sth_hiwat >> 3);
30160f1702c5SYu Xiangning }
3017ff550d0eSmasputra
30186b7506c7SErik Nordmark /* Caller could have changed tcp_rwnd; update tha_win */
30196b7506c7SErik Nordmark if (tcp->tcp_tcpha != NULL) {
30206b7506c7SErik Nordmark tcp->tcp_tcpha->tha_win =
30216b7506c7SErik Nordmark htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
30226b7506c7SErik Nordmark }
30236b7506c7SErik Nordmark if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
30246b7506c7SErik Nordmark tcp->tcp_cwnd_max = rwnd;
30256b7506c7SErik Nordmark
3026ff550d0eSmasputra /*
3027ff550d0eSmasputra * In the fusion case, the maxpsz stream head value of
3028ff550d0eSmasputra * our peer is set according to its send buffer size
3029ff550d0eSmasputra * and our receive buffer size; since the latter may
3030ff550d0eSmasputra * have changed we need to update the peer's maxpsz.
3031ff550d0eSmasputra */
3032ff550d0eSmasputra (void) tcp_maxpsz_set(peer_tcp, B_TRUE);
303379c0745dSRao Shoaib return (sth_hiwat);
3034ff550d0eSmasputra }
3035ff550d0eSmasputra
3036bd670b35SErik Nordmark if (tcp_detached)
30377c478bd9Sstevel@tonic-gate old_max_rwnd = tcp->tcp_rwnd;
3038bd670b35SErik Nordmark else
3039bd670b35SErik Nordmark old_max_rwnd = connp->conn_rcvbuf;
30407c478bd9Sstevel@tonic-gate
30417c478bd9Sstevel@tonic-gate
30427c478bd9Sstevel@tonic-gate /*
30437c478bd9Sstevel@tonic-gate * If window size info has already been exchanged, TCP should not
30447c478bd9Sstevel@tonic-gate * shrink the window. Shrinking window is doable if done carefully.
30457c478bd9Sstevel@tonic-gate * We may add that support later. But so far there is not a real
30467c478bd9Sstevel@tonic-gate * need to do that.
30477c478bd9Sstevel@tonic-gate */
30487c478bd9Sstevel@tonic-gate if (rwnd < old_max_rwnd && tcp->tcp_state > TCPS_SYN_SENT) {
30497c478bd9Sstevel@tonic-gate /* MSS may have changed, do a round up again. */
30507c478bd9Sstevel@tonic-gate rwnd = MSS_ROUNDUP(old_max_rwnd, mss);
30517c478bd9Sstevel@tonic-gate }
30527c478bd9Sstevel@tonic-gate
30537c478bd9Sstevel@tonic-gate /*
30547c478bd9Sstevel@tonic-gate * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check
30557c478bd9Sstevel@tonic-gate * can be applied even before the window scale option is decided.
30567c478bd9Sstevel@tonic-gate */
30577c478bd9Sstevel@tonic-gate max_transmittable_rwnd = TCP_MAXWIN << tcp->tcp_rcv_ws;
30587c478bd9Sstevel@tonic-gate if (rwnd > max_transmittable_rwnd) {
30597c478bd9Sstevel@tonic-gate rwnd = max_transmittable_rwnd -
30607c478bd9Sstevel@tonic-gate (max_transmittable_rwnd % mss);
30617c478bd9Sstevel@tonic-gate if (rwnd < mss)
30627c478bd9Sstevel@tonic-gate rwnd = max_transmittable_rwnd;
30637c478bd9Sstevel@tonic-gate /*
30647c478bd9Sstevel@tonic-gate * If we're over the limit we may have to back down tcp_rwnd.
30657c478bd9Sstevel@tonic-gate * The increment below won't work for us. So we set all three
30667c478bd9Sstevel@tonic-gate * here and the increment below will have no effect.
30677c478bd9Sstevel@tonic-gate */
30687c478bd9Sstevel@tonic-gate tcp->tcp_rwnd = old_max_rwnd = rwnd;
30697c478bd9Sstevel@tonic-gate }
30707c478bd9Sstevel@tonic-gate if (tcp->tcp_localnet) {
30717c478bd9Sstevel@tonic-gate tcp->tcp_rack_abs_max =
3072f4b3ec61Sdh155122 MIN(tcps->tcps_local_dacks_max, rwnd / mss / 2);
30737c478bd9Sstevel@tonic-gate } else {
30747c478bd9Sstevel@tonic-gate /*
30757c478bd9Sstevel@tonic-gate * For a remote host on a different subnet (through a router),
30767c478bd9Sstevel@tonic-gate * we ack every other packet to be conforming to RFC1122.
30777c478bd9Sstevel@tonic-gate * tcp_deferred_acks_max is default to 2.
30787c478bd9Sstevel@tonic-gate */
30797c478bd9Sstevel@tonic-gate tcp->tcp_rack_abs_max =
3080f4b3ec61Sdh155122 MIN(tcps->tcps_deferred_acks_max, rwnd / mss / 2);
30817c478bd9Sstevel@tonic-gate }
30827c478bd9Sstevel@tonic-gate if (tcp->tcp_rack_cur_max > tcp->tcp_rack_abs_max)
30837c478bd9Sstevel@tonic-gate tcp->tcp_rack_cur_max = tcp->tcp_rack_abs_max;
30847c478bd9Sstevel@tonic-gate else
30857c478bd9Sstevel@tonic-gate tcp->tcp_rack_cur_max = 0;
30867c478bd9Sstevel@tonic-gate /*
30877c478bd9Sstevel@tonic-gate * Increment the current rwnd by the amount the maximum grew (we
30887c478bd9Sstevel@tonic-gate * can not overwrite it since we might be in the middle of a
30897c478bd9Sstevel@tonic-gate * connection.)
30907c478bd9Sstevel@tonic-gate */
30917c478bd9Sstevel@tonic-gate tcp->tcp_rwnd += rwnd - old_max_rwnd;
3092bd670b35SErik Nordmark connp->conn_rcvbuf = rwnd;
30931b2702b4SRao Shoaib
3094bd670b35SErik Nordmark /* Are we already connected? */
3095bd670b35SErik Nordmark if (tcp->tcp_tcpha != NULL) {
3096bd670b35SErik Nordmark tcp->tcp_tcpha->tha_win =
3097bd670b35SErik Nordmark htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
3098bd670b35SErik Nordmark }
3099bd670b35SErik Nordmark
31007c478bd9Sstevel@tonic-gate if ((tcp->tcp_rcv_ws > 0) && rwnd > tcp->tcp_cwnd_max)
31017c478bd9Sstevel@tonic-gate tcp->tcp_cwnd_max = rwnd;
31027c478bd9Sstevel@tonic-gate
31037c478bd9Sstevel@tonic-gate if (tcp_detached)
31047c478bd9Sstevel@tonic-gate return (rwnd);
310579c0745dSRao Shoaib
310679c0745dSRao Shoaib tcp_set_recv_threshold(tcp, rwnd >> 3);
310779c0745dSRao Shoaib
3108bd670b35SErik Nordmark (void) proto_set_rx_hiwat(connp->conn_rq, connp, rwnd);
31097c478bd9Sstevel@tonic-gate return (rwnd);
31107c478bd9Sstevel@tonic-gate }
31117c478bd9Sstevel@tonic-gate
3112ff550d0eSmasputra int
tcp_do_unbind(conn_t * connp)31130f1702c5SYu Xiangning tcp_do_unbind(conn_t *connp)
31147c478bd9Sstevel@tonic-gate {
31150f1702c5SYu Xiangning tcp_t *tcp = connp->conn_tcp;
31169cd928feSAlan Maguire int32_t oldstate;
31177c478bd9Sstevel@tonic-gate
31187c478bd9Sstevel@tonic-gate switch (tcp->tcp_state) {
31197c478bd9Sstevel@tonic-gate case TCPS_BOUND:
31207c478bd9Sstevel@tonic-gate case TCPS_LISTEN:
31217c478bd9Sstevel@tonic-gate break;
31227c478bd9Sstevel@tonic-gate default:
31230f1702c5SYu Xiangning return (-TOUTSTATE);
31247c478bd9Sstevel@tonic-gate }
31257c478bd9Sstevel@tonic-gate
31267c478bd9Sstevel@tonic-gate /*
31277c478bd9Sstevel@tonic-gate * Need to clean up all the eagers since after the unbind, segments
31287c478bd9Sstevel@tonic-gate * will no longer be delivered to this listener stream.
31297c478bd9Sstevel@tonic-gate */
31307c478bd9Sstevel@tonic-gate mutex_enter(&tcp->tcp_eager_lock);
31317c478bd9Sstevel@tonic-gate if (tcp->tcp_conn_req_cnt_q0 != 0 || tcp->tcp_conn_req_cnt_q != 0) {
31327c478bd9Sstevel@tonic-gate tcp_eager_cleanup(tcp, 0);
31337c478bd9Sstevel@tonic-gate }
31347c478bd9Sstevel@tonic-gate mutex_exit(&tcp->tcp_eager_lock);
31357c478bd9Sstevel@tonic-gate
313693fcb0b9SKacheong Poon /* Clean up the listener connection counter if necessary. */
313793fcb0b9SKacheong Poon if (tcp->tcp_listen_cnt != NULL)
313893fcb0b9SKacheong Poon TCP_DECR_LISTEN_CNT(tcp);
3139bd670b35SErik Nordmark connp->conn_laddr_v6 = ipv6_all_zeros;
3140bd670b35SErik Nordmark connp->conn_saddr_v6 = ipv6_all_zeros;
31417c478bd9Sstevel@tonic-gate tcp_bind_hash_remove(tcp);
31429cd928feSAlan Maguire oldstate = tcp->tcp_state;
31437c478bd9Sstevel@tonic-gate tcp->tcp_state = TCPS_IDLE;
31449cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
31459cd928feSAlan Maguire connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
31469cd928feSAlan Maguire int32_t, oldstate);
31470f1702c5SYu Xiangning
3148bd670b35SErik Nordmark ip_unbind(connp);
31497c478bd9Sstevel@tonic-gate bzero(&connp->conn_ports, sizeof (connp->conn_ports));
31500f1702c5SYu Xiangning
3151bd670b35SErik Nordmark return (0);
31520f1702c5SYu Xiangning }
31530f1702c5SYu Xiangning
3154bd670b35SErik Nordmark /*
31553e95bd4aSAnders Persson * Collect protocol properties to send to the upper handle.
3156bd670b35SErik Nordmark */
31577c478bd9Sstevel@tonic-gate void
tcp_get_proto_props(tcp_t * tcp,struct sock_proto_props * sopp)31583e95bd4aSAnders Persson tcp_get_proto_props(tcp_t *tcp, struct sock_proto_props *sopp)
31597c478bd9Sstevel@tonic-gate {
31603e95bd4aSAnders Persson conn_t *connp = tcp->tcp_connp;
31610f1702c5SYu Xiangning
31623e95bd4aSAnders Persson sopp->sopp_flags = SOCKOPT_RCVHIWAT | SOCKOPT_MAXBLK | SOCKOPT_WROFF;
31633e95bd4aSAnders Persson sopp->sopp_maxblk = tcp_maxpsz_set(tcp, B_FALSE);
31640f1702c5SYu Xiangning
31653e95bd4aSAnders Persson sopp->sopp_rxhiwat = tcp->tcp_fused ?
3166bd670b35SErik Nordmark tcp_fuse_set_rcv_hiwat(tcp, connp->conn_rcvbuf) :
3167bd670b35SErik Nordmark connp->conn_rcvbuf;
31688c0bf406Sja97890 /*
31698c0bf406Sja97890 * Determine what write offset value to use depending on SACK and
31708c0bf406Sja97890 * whether the endpoint is fused or not.
31718c0bf406Sja97890 */
31727c478bd9Sstevel@tonic-gate if (tcp->tcp_fused) {
31737c478bd9Sstevel@tonic-gate ASSERT(tcp->tcp_loopback);
3174ff550d0eSmasputra ASSERT(tcp->tcp_loopback_peer != NULL);
31757c478bd9Sstevel@tonic-gate /*
31767c478bd9Sstevel@tonic-gate * For fused tcp loopback, set the stream head's write
31777c478bd9Sstevel@tonic-gate * offset value to zero since we won't be needing any room
31787c478bd9Sstevel@tonic-gate * for TCP/IP headers. This would also improve performance
31797c478bd9Sstevel@tonic-gate * since it would reduce the amount of work done by kmem.
31807c478bd9Sstevel@tonic-gate * Non-fused tcp loopback case is handled separately below.
31817c478bd9Sstevel@tonic-gate */
31823e95bd4aSAnders Persson sopp->sopp_wroff = 0;
31837c478bd9Sstevel@tonic-gate /*
3184ff550d0eSmasputra * Update the peer's transmit parameters according to
3185ff550d0eSmasputra * our recently calculated high water mark value.
3186ff550d0eSmasputra */
3187ff550d0eSmasputra (void) tcp_maxpsz_set(tcp->tcp_loopback_peer, B_TRUE);
31887c478bd9Sstevel@tonic-gate } else if (tcp->tcp_snd_sack_ok) {
31893e95bd4aSAnders Persson sopp->sopp_wroff = connp->conn_ht_iphc_allocated +
31903e95bd4aSAnders Persson (tcp->tcp_loopback ? 0 : tcp->tcp_tcps->tcps_wroff_xtra);
31917c478bd9Sstevel@tonic-gate } else {
31923e95bd4aSAnders Persson sopp->sopp_wroff = connp->conn_ht_iphc_len +
31933e95bd4aSAnders Persson (tcp->tcp_loopback ? 0 : tcp->tcp_tcps->tcps_wroff_xtra);
31947c478bd9Sstevel@tonic-gate }
31957c478bd9Sstevel@tonic-gate
3196081c0aa8SAnders Persson if (tcp->tcp_loopback) {
31973e95bd4aSAnders Persson sopp->sopp_flags |= SOCKOPT_LOOPBACK;
31983e95bd4aSAnders Persson sopp->sopp_loopback = B_TRUE;
3199081c0aa8SAnders Persson }
32000f1702c5SYu Xiangning }
32010f1702c5SYu Xiangning
3202bd670b35SErik Nordmark /*
3203bd670b35SErik Nordmark * Check the usability of ZEROCOPY. It's instead checking the flag set by IP.
3204bd670b35SErik Nordmark */
3205721fffe3SKacheong Poon boolean_t
tcp_zcopy_check(tcp_t * tcp)32067c478bd9Sstevel@tonic-gate tcp_zcopy_check(tcp_t *tcp)
32077c478bd9Sstevel@tonic-gate {
32087c478bd9Sstevel@tonic-gate conn_t *connp = tcp->tcp_connp;
3209bd670b35SErik Nordmark ip_xmit_attr_t *ixa = connp->conn_ixa;
32107c478bd9Sstevel@tonic-gate boolean_t zc_enabled = B_FALSE;
3211f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
32127c478bd9Sstevel@tonic-gate
32137c478bd9Sstevel@tonic-gate if (do_tcpzcopy == 2)
32147c478bd9Sstevel@tonic-gate zc_enabled = B_TRUE;
3215bd670b35SErik Nordmark else if ((do_tcpzcopy == 1) && (ixa->ixa_flags & IXAF_ZCOPY_CAPAB))
3216bd670b35SErik Nordmark zc_enabled = B_TRUE;
32177c478bd9Sstevel@tonic-gate
32187c478bd9Sstevel@tonic-gate tcp->tcp_snd_zcopy_on = zc_enabled;
32197c478bd9Sstevel@tonic-gate if (!TCP_IS_DETACHED(tcp)) {
32207c478bd9Sstevel@tonic-gate if (zc_enabled) {
3221bd670b35SErik Nordmark ixa->ixa_flags |= IXAF_VERIFY_ZCOPY;
3222bd670b35SErik Nordmark (void) proto_set_tx_copyopt(connp->conn_rq, connp,
32230f1702c5SYu Xiangning ZCVMSAFE);
3224f4b3ec61Sdh155122 TCP_STAT(tcps, tcp_zcopy_on);
32257c478bd9Sstevel@tonic-gate } else {
3226bd670b35SErik Nordmark ixa->ixa_flags &= ~IXAF_VERIFY_ZCOPY;
3227bd670b35SErik Nordmark (void) proto_set_tx_copyopt(connp->conn_rq, connp,
32280f1702c5SYu Xiangning ZCVMUNSAFE);
3229f4b3ec61Sdh155122 TCP_STAT(tcps, tcp_zcopy_off);
32307c478bd9Sstevel@tonic-gate }
32317c478bd9Sstevel@tonic-gate }
32327c478bd9Sstevel@tonic-gate return (zc_enabled);
32337c478bd9Sstevel@tonic-gate }
32347c478bd9Sstevel@tonic-gate
32357c478bd9Sstevel@tonic-gate /*
3236bd670b35SErik Nordmark * Backoff from a zero-copy message by copying data to a new allocated
3237bd670b35SErik Nordmark * message and freeing the original desballoca'ed segmapped message.
3238bd670b35SErik Nordmark *
3239bd670b35SErik Nordmark * This function is called by following two callers:
3240bd670b35SErik Nordmark * 1. tcp_timer: fix_xmitlist is set to B_TRUE, because it's safe to free
3241bd670b35SErik Nordmark * the origial desballoca'ed message and notify sockfs. This is in re-
3242bd670b35SErik Nordmark * transmit state.
3243bd670b35SErik Nordmark * 2. tcp_output: fix_xmitlist is set to B_FALSE. Flag STRUIO_ZCNOTIFY need
3244bd670b35SErik Nordmark * to be copied to new message.
32457c478bd9Sstevel@tonic-gate */
3246721fffe3SKacheong Poon mblk_t *
tcp_zcopy_backoff(tcp_t * tcp,mblk_t * bp,boolean_t fix_xmitlist)3247bd670b35SErik Nordmark tcp_zcopy_backoff(tcp_t *tcp, mblk_t *bp, boolean_t fix_xmitlist)
32487c478bd9Sstevel@tonic-gate {
3249bd670b35SErik Nordmark mblk_t *nbp;
3250bd670b35SErik Nordmark mblk_t *head = NULL;
3251bd670b35SErik Nordmark mblk_t *tail = NULL;
3252f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
3253f4b3ec61Sdh155122
3254bd670b35SErik Nordmark ASSERT(bp != NULL);
3255bd670b35SErik Nordmark while (bp != NULL) {
32567c478bd9Sstevel@tonic-gate if (IS_VMLOANED_MBLK(bp)) {
3257f4b3ec61Sdh155122 TCP_STAT(tcps, tcp_zcopy_backoff);
3258bd670b35SErik Nordmark if ((nbp = copyb(bp)) == NULL) {
32597c478bd9Sstevel@tonic-gate tcp->tcp_xmit_zc_clean = B_FALSE;
3260bd670b35SErik Nordmark if (tail != NULL)
3261bd670b35SErik Nordmark tail->b_cont = bp;
3262bd670b35SErik Nordmark return ((head == NULL) ? bp : head);
32637c478bd9Sstevel@tonic-gate }
3264bd670b35SErik Nordmark
32657c478bd9Sstevel@tonic-gate if (bp->b_datap->db_struioflag & STRUIO_ZCNOTIFY) {
32667c478bd9Sstevel@tonic-gate if (fix_xmitlist)
32677c478bd9Sstevel@tonic-gate tcp_zcopy_notify(tcp);
32687c478bd9Sstevel@tonic-gate else
3269bd670b35SErik Nordmark nbp->b_datap->db_struioflag |=
32707c478bd9Sstevel@tonic-gate STRUIO_ZCNOTIFY;
32717c478bd9Sstevel@tonic-gate }
3272bd670b35SErik Nordmark nbp->b_cont = bp->b_cont;
3273bd670b35SErik Nordmark
3274bd670b35SErik Nordmark /*
3275bd670b35SErik Nordmark * Copy saved information and adjust tcp_xmit_tail
3276bd670b35SErik Nordmark * if needed.
3277bd670b35SErik Nordmark */
32787c478bd9Sstevel@tonic-gate if (fix_xmitlist) {
3279bd670b35SErik Nordmark nbp->b_prev = bp->b_prev;
3280bd670b35SErik Nordmark nbp->b_next = bp->b_next;
3281bd670b35SErik Nordmark
32827c478bd9Sstevel@tonic-gate if (tcp->tcp_xmit_tail == bp)
3283bd670b35SErik Nordmark tcp->tcp_xmit_tail = nbp;
32847c478bd9Sstevel@tonic-gate }
3285bd670b35SErik Nordmark
3286bd670b35SErik Nordmark /* Free the original message. */
32877c478bd9Sstevel@tonic-gate bp->b_prev = NULL;
3288bd670b35SErik Nordmark bp->b_next = NULL;
32897c478bd9Sstevel@tonic-gate freeb(bp);
3290bd670b35SErik Nordmark
3291bd670b35SErik Nordmark bp = nbp;
3292bd670b35SErik Nordmark }
3293bd670b35SErik Nordmark
3294bd670b35SErik Nordmark if (head == NULL) {
3295bd670b35SErik Nordmark head = bp;
3296bd670b35SErik Nordmark }
3297bd670b35SErik Nordmark if (tail == NULL) {
3298bd670b35SErik Nordmark tail = bp;
32997c478bd9Sstevel@tonic-gate } else {
3300bd670b35SErik Nordmark tail->b_cont = bp;
3301bd670b35SErik Nordmark tail = bp;
33027c478bd9Sstevel@tonic-gate }
3303bd670b35SErik Nordmark
3304bd670b35SErik Nordmark /* Move forward. */
3305bd670b35SErik Nordmark bp = bp->b_cont;
33067c478bd9Sstevel@tonic-gate }
3307bd670b35SErik Nordmark
33087c478bd9Sstevel@tonic-gate if (fix_xmitlist) {
33097c478bd9Sstevel@tonic-gate tcp->tcp_xmit_last = tail;
33107c478bd9Sstevel@tonic-gate tcp->tcp_xmit_zc_clean = B_TRUE;
33117c478bd9Sstevel@tonic-gate }
3312bd670b35SErik Nordmark
33137c478bd9Sstevel@tonic-gate return (head);
33147c478bd9Sstevel@tonic-gate }
33157c478bd9Sstevel@tonic-gate
3316721fffe3SKacheong Poon void
tcp_zcopy_notify(tcp_t * tcp)33177c478bd9Sstevel@tonic-gate tcp_zcopy_notify(tcp_t *tcp)
33187c478bd9Sstevel@tonic-gate {
33197c478bd9Sstevel@tonic-gate struct stdata *stp;
33200f1702c5SYu Xiangning conn_t *connp;
33217c478bd9Sstevel@tonic-gate
33227c478bd9Sstevel@tonic-gate if (tcp->tcp_detached)
33237c478bd9Sstevel@tonic-gate return;
33240f1702c5SYu Xiangning connp = tcp->tcp_connp;
33250f1702c5SYu Xiangning if (IPCL_IS_NONSTR(connp)) {
33260f1702c5SYu Xiangning (*connp->conn_upcalls->su_zcopy_notify)
33270f1702c5SYu Xiangning (connp->conn_upper_handle);
33280f1702c5SYu Xiangning return;
33290f1702c5SYu Xiangning }
3330bd670b35SErik Nordmark stp = STREAM(connp->conn_rq);
33317c478bd9Sstevel@tonic-gate mutex_enter(&stp->sd_lock);
33327c478bd9Sstevel@tonic-gate stp->sd_flag |= STZCNOTIFY;
33337c478bd9Sstevel@tonic-gate cv_broadcast(&stp->sd_zcopy_wait);
33347c478bd9Sstevel@tonic-gate mutex_exit(&stp->sd_lock);
33357c478bd9Sstevel@tonic-gate }
33367c478bd9Sstevel@tonic-gate
3337bd670b35SErik Nordmark /*
3338bd670b35SErik Nordmark * Update the TCP connection according to change of LSO capability.
3339bd670b35SErik Nordmark */
3340bd670b35SErik Nordmark static void
tcp_update_lso(tcp_t * tcp,ip_xmit_attr_t * ixa)3341bd670b35SErik Nordmark tcp_update_lso(tcp_t *tcp, ip_xmit_attr_t *ixa)
33427c478bd9Sstevel@tonic-gate {
3343bd670b35SErik Nordmark /*
3344bd670b35SErik Nordmark * We check against IPv4 header length to preserve the old behavior
3345bd670b35SErik Nordmark * of only enabling LSO when there are no IP options.
3346bd670b35SErik Nordmark * But this restriction might not be necessary at all. Before removing
3347bd670b35SErik Nordmark * it, need to verify how LSO is handled for source routing case, with
3348bd670b35SErik Nordmark * which IP does software checksum.
3349bd670b35SErik Nordmark *
3350bd670b35SErik Nordmark * For IPv6, whenever any extension header is needed, LSO is supressed.
3351bd670b35SErik Nordmark */
3352bd670b35SErik Nordmark if (ixa->ixa_ip_hdr_length != ((ixa->ixa_flags & IXAF_IS_IPV4) ?
3353bd670b35SErik Nordmark IP_SIMPLE_HDR_LENGTH : IPV6_HDR_LEN))
3354bd670b35SErik Nordmark return;
3355bd670b35SErik Nordmark
3356bd670b35SErik Nordmark /*
3357bd670b35SErik Nordmark * Either the LSO capability newly became usable, or it has changed.
3358bd670b35SErik Nordmark */
3359bd670b35SErik Nordmark if (ixa->ixa_flags & IXAF_LSO_CAPAB) {
3360bd670b35SErik Nordmark ill_lso_capab_t *lsoc = &ixa->ixa_lso_capab;
336162366fbbSRobert Mustacchi uint_t lso_max = (ixa->ixa_flags & IXAF_IS_IPV4) ?
336262366fbbSRobert Mustacchi lsoc->ill_lso_max_tcpv4 : lsoc->ill_lso_max_tcpv6;
3363bd670b35SErik Nordmark
336462366fbbSRobert Mustacchi ASSERT3U(lso_max, >, 0);
336562366fbbSRobert Mustacchi tcp->tcp_lso_max = MIN(TCP_MAX_LSO_LENGTH, lso_max);
3366bd670b35SErik Nordmark
3367bd670b35SErik Nordmark DTRACE_PROBE3(tcp_update_lso, boolean_t, tcp->tcp_lso,
3368bd670b35SErik Nordmark boolean_t, B_TRUE, uint32_t, tcp->tcp_lso_max);
3369bd670b35SErik Nordmark
3370bd670b35SErik Nordmark /*
3371bd670b35SErik Nordmark * If LSO to be enabled, notify the STREAM header with larger
3372bd670b35SErik Nordmark * data block.
3373bd670b35SErik Nordmark */
3374bd670b35SErik Nordmark if (!tcp->tcp_lso)
3375bd670b35SErik Nordmark tcp->tcp_maxpsz_multiplier = 0;
3376bd670b35SErik Nordmark
3377bd670b35SErik Nordmark tcp->tcp_lso = B_TRUE;
3378bd670b35SErik Nordmark TCP_STAT(tcp->tcp_tcps, tcp_lso_enabled);
3379bd670b35SErik Nordmark } else { /* LSO capability is not usable any more. */
3380bd670b35SErik Nordmark DTRACE_PROBE3(tcp_update_lso, boolean_t, tcp->tcp_lso,
3381bd670b35SErik Nordmark boolean_t, B_FALSE, uint32_t, tcp->tcp_lso_max);
3382bd670b35SErik Nordmark
3383bd670b35SErik Nordmark /*
3384bd670b35SErik Nordmark * If LSO to be disabled, notify the STREAM header with smaller
3385bd670b35SErik Nordmark * data block. And need to restore fragsize to PMTU.
3386bd670b35SErik Nordmark */
3387bd670b35SErik Nordmark if (tcp->tcp_lso) {
3388bd670b35SErik Nordmark tcp->tcp_maxpsz_multiplier =
3389bd670b35SErik Nordmark tcp->tcp_tcps->tcps_maxpsz_multiplier;
3390bd670b35SErik Nordmark ixa->ixa_fragsize = ixa->ixa_pmtu;
3391bd670b35SErik Nordmark tcp->tcp_lso = B_FALSE;
3392bd670b35SErik Nordmark TCP_STAT(tcp->tcp_tcps, tcp_lso_disabled);
3393bd670b35SErik Nordmark }
3394bd670b35SErik Nordmark }
3395bd670b35SErik Nordmark
3396bd670b35SErik Nordmark (void) tcp_maxpsz_set(tcp, B_TRUE);
3397bd670b35SErik Nordmark }
3398bd670b35SErik Nordmark
3399bd670b35SErik Nordmark /*
3400bd670b35SErik Nordmark * Update the TCP connection according to change of ZEROCOPY capability.
3401bd670b35SErik Nordmark */
3402bd670b35SErik Nordmark static void
tcp_update_zcopy(tcp_t * tcp)3403bd670b35SErik Nordmark tcp_update_zcopy(tcp_t *tcp)
3404bd670b35SErik Nordmark {
34057c478bd9Sstevel@tonic-gate conn_t *connp = tcp->tcp_connp;
3406f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
34077c478bd9Sstevel@tonic-gate
3408bd670b35SErik Nordmark if (tcp->tcp_snd_zcopy_on) {
3409bd670b35SErik Nordmark tcp->tcp_snd_zcopy_on = B_FALSE;
3410bd670b35SErik Nordmark if (!TCP_IS_DETACHED(tcp)) {
3411bd670b35SErik Nordmark (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3412bd670b35SErik Nordmark ZCVMUNSAFE);
3413bd670b35SErik Nordmark TCP_STAT(tcps, tcp_zcopy_off);
3414bd670b35SErik Nordmark }
34157c478bd9Sstevel@tonic-gate } else {
3416bd670b35SErik Nordmark tcp->tcp_snd_zcopy_on = B_TRUE;
3417bd670b35SErik Nordmark if (!TCP_IS_DETACHED(tcp)) {
3418bd670b35SErik Nordmark (void) proto_set_tx_copyopt(connp->conn_rq, connp,
3419bd670b35SErik Nordmark ZCVMSAFE);
3420bd670b35SErik Nordmark TCP_STAT(tcps, tcp_zcopy_on);
34217c478bd9Sstevel@tonic-gate }
34227c478bd9Sstevel@tonic-gate }
34238347601bSyl150051 }
34248347601bSyl150051
3425c793af95Ssangeeta /*
3426bd670b35SErik Nordmark * Notify function registered with ip_xmit_attr_t. It's called in the squeue
3427bd670b35SErik Nordmark * so it's safe to update the TCP connection.
3428c793af95Ssangeeta */
3429bd670b35SErik Nordmark /* ARGSUSED1 */
3430bd670b35SErik Nordmark static void
tcp_notify(void * arg,ip_xmit_attr_t * ixa,ixa_notify_type_t ntype,ixa_notify_arg_t narg)3431bd670b35SErik Nordmark tcp_notify(void *arg, ip_xmit_attr_t *ixa, ixa_notify_type_t ntype,
3432bd670b35SErik Nordmark ixa_notify_arg_t narg)
34338347601bSyl150051 {
3434bd670b35SErik Nordmark tcp_t *tcp = (tcp_t *)arg;
3435bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
34368347601bSyl150051
3437bd670b35SErik Nordmark switch (ntype) {
3438bd670b35SErik Nordmark case IXAN_LSO:
3439bd670b35SErik Nordmark tcp_update_lso(tcp, connp->conn_ixa);
3440bd670b35SErik Nordmark break;
3441bd670b35SErik Nordmark case IXAN_PMTU:
3442bd670b35SErik Nordmark tcp_update_pmtu(tcp, B_FALSE);
3443bd670b35SErik Nordmark break;
3444bd670b35SErik Nordmark case IXAN_ZCOPY:
3445bd670b35SErik Nordmark tcp_update_zcopy(tcp);
3446bd670b35SErik Nordmark break;
3447bd670b35SErik Nordmark default:
3448bd670b35SErik Nordmark break;
34497c478bd9Sstevel@tonic-gate }
34508347601bSyl150051 }
34518347601bSyl150051
34527c478bd9Sstevel@tonic-gate /*
34537c478bd9Sstevel@tonic-gate * The TCP write service routine should never be called...
34547c478bd9Sstevel@tonic-gate */
34557c478bd9Sstevel@tonic-gate /* ARGSUSED */
34568a06b3d6SToomas Soome static int
tcp_wsrv(queue_t * q)34577c478bd9Sstevel@tonic-gate tcp_wsrv(queue_t *q)
34587c478bd9Sstevel@tonic-gate {
3459f4b3ec61Sdh155122 tcp_stack_t *tcps = Q_TO_TCP(q)->tcp_tcps;
3460f4b3ec61Sdh155122
3461f4b3ec61Sdh155122 TCP_STAT(tcps, tcp_wsrv_called);
34628a06b3d6SToomas Soome return (0);
34637c478bd9Sstevel@tonic-gate }
34647c478bd9Sstevel@tonic-gate
34657c478bd9Sstevel@tonic-gate /*
34667c478bd9Sstevel@tonic-gate * Hash list lookup routine for tcp_t structures.
34677c478bd9Sstevel@tonic-gate * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF.
34687c478bd9Sstevel@tonic-gate */
3469721fffe3SKacheong Poon tcp_t *
tcp_acceptor_hash_lookup(t_uscalar_t id,tcp_stack_t * tcps)3470f4b3ec61Sdh155122 tcp_acceptor_hash_lookup(t_uscalar_t id, tcp_stack_t *tcps)
34717c478bd9Sstevel@tonic-gate {
34727c478bd9Sstevel@tonic-gate tf_t *tf;
34737c478bd9Sstevel@tonic-gate tcp_t *tcp;
34747c478bd9Sstevel@tonic-gate
3475f4b3ec61Sdh155122 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)];
34767c478bd9Sstevel@tonic-gate mutex_enter(&tf->tf_lock);
34777c478bd9Sstevel@tonic-gate for (tcp = tf->tf_tcp; tcp != NULL;
34787c478bd9Sstevel@tonic-gate tcp = tcp->tcp_acceptor_hash) {
34797c478bd9Sstevel@tonic-gate if (tcp->tcp_acceptor_id == id) {
34807c478bd9Sstevel@tonic-gate CONN_INC_REF(tcp->tcp_connp);
34817c478bd9Sstevel@tonic-gate mutex_exit(&tf->tf_lock);
34827c478bd9Sstevel@tonic-gate return (tcp);
34837c478bd9Sstevel@tonic-gate }
34847c478bd9Sstevel@tonic-gate }
34857c478bd9Sstevel@tonic-gate mutex_exit(&tf->tf_lock);
34867c478bd9Sstevel@tonic-gate return (NULL);
34877c478bd9Sstevel@tonic-gate }
34887c478bd9Sstevel@tonic-gate
34897c478bd9Sstevel@tonic-gate /*
34907c478bd9Sstevel@tonic-gate * Hash list insertion routine for tcp_t structures.
34917c478bd9Sstevel@tonic-gate */
34927c478bd9Sstevel@tonic-gate void
tcp_acceptor_hash_insert(t_uscalar_t id,tcp_t * tcp)34937c478bd9Sstevel@tonic-gate tcp_acceptor_hash_insert(t_uscalar_t id, tcp_t *tcp)
34947c478bd9Sstevel@tonic-gate {
34957c478bd9Sstevel@tonic-gate tf_t *tf;
34967c478bd9Sstevel@tonic-gate tcp_t **tcpp;
34977c478bd9Sstevel@tonic-gate tcp_t *tcpnext;
3498f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
34997c478bd9Sstevel@tonic-gate
3500f4b3ec61Sdh155122 tf = &tcps->tcps_acceptor_fanout[TCP_ACCEPTOR_HASH(id)];
35017c478bd9Sstevel@tonic-gate
35027c478bd9Sstevel@tonic-gate if (tcp->tcp_ptpahn != NULL)
35037c478bd9Sstevel@tonic-gate tcp_acceptor_hash_remove(tcp);
35047c478bd9Sstevel@tonic-gate tcpp = &tf->tf_tcp;
35057c478bd9Sstevel@tonic-gate mutex_enter(&tf->tf_lock);
35067c478bd9Sstevel@tonic-gate tcpnext = tcpp[0];
35077c478bd9Sstevel@tonic-gate if (tcpnext)
35087c478bd9Sstevel@tonic-gate tcpnext->tcp_ptpahn = &tcp->tcp_acceptor_hash;
35097c478bd9Sstevel@tonic-gate tcp->tcp_acceptor_hash = tcpnext;
35107c478bd9Sstevel@tonic-gate tcp->tcp_ptpahn = tcpp;
35117c478bd9Sstevel@tonic-gate tcpp[0] = tcp;
35127c478bd9Sstevel@tonic-gate tcp->tcp_acceptor_lockp = &tf->tf_lock; /* For tcp_*_hash_remove */
35137c478bd9Sstevel@tonic-gate mutex_exit(&tf->tf_lock);
35147c478bd9Sstevel@tonic-gate }
35157c478bd9Sstevel@tonic-gate
35167c478bd9Sstevel@tonic-gate /*
35177c478bd9Sstevel@tonic-gate * Hash list removal routine for tcp_t structures.
35187c478bd9Sstevel@tonic-gate */
3519721fffe3SKacheong Poon void
tcp_acceptor_hash_remove(tcp_t * tcp)35207c478bd9Sstevel@tonic-gate tcp_acceptor_hash_remove(tcp_t *tcp)
35217c478bd9Sstevel@tonic-gate {
35227c478bd9Sstevel@tonic-gate tcp_t *tcpnext;
35237c478bd9Sstevel@tonic-gate kmutex_t *lockp;
35247c478bd9Sstevel@tonic-gate
35257c478bd9Sstevel@tonic-gate /*
35267c478bd9Sstevel@tonic-gate * Extract the lock pointer in case there are concurrent
35277c478bd9Sstevel@tonic-gate * hash_remove's for this instance.
35287c478bd9Sstevel@tonic-gate */
35297c478bd9Sstevel@tonic-gate lockp = tcp->tcp_acceptor_lockp;
35307c478bd9Sstevel@tonic-gate
35317c478bd9Sstevel@tonic-gate if (tcp->tcp_ptpahn == NULL)
35327c478bd9Sstevel@tonic-gate return;
35337c478bd9Sstevel@tonic-gate
35347c478bd9Sstevel@tonic-gate ASSERT(lockp != NULL);
35357c478bd9Sstevel@tonic-gate mutex_enter(lockp);
35367c478bd9Sstevel@tonic-gate if (tcp->tcp_ptpahn) {
35377c478bd9Sstevel@tonic-gate tcpnext = tcp->tcp_acceptor_hash;
35387c478bd9Sstevel@tonic-gate if (tcpnext) {
35397c478bd9Sstevel@tonic-gate tcpnext->tcp_ptpahn = tcp->tcp_ptpahn;
35407c478bd9Sstevel@tonic-gate tcp->tcp_acceptor_hash = NULL;
35417c478bd9Sstevel@tonic-gate }
35427c478bd9Sstevel@tonic-gate *tcp->tcp_ptpahn = tcpnext;
35437c478bd9Sstevel@tonic-gate tcp->tcp_ptpahn = NULL;
35447c478bd9Sstevel@tonic-gate }
35457c478bd9Sstevel@tonic-gate mutex_exit(lockp);
35467c478bd9Sstevel@tonic-gate tcp->tcp_acceptor_lockp = NULL;
35477c478bd9Sstevel@tonic-gate }
35487c478bd9Sstevel@tonic-gate
35497c478bd9Sstevel@tonic-gate /*
35507c478bd9Sstevel@tonic-gate * Type three generator adapted from the random() function in 4.4 BSD:
35517c478bd9Sstevel@tonic-gate */
35527c478bd9Sstevel@tonic-gate
35537c478bd9Sstevel@tonic-gate /*
35547c478bd9Sstevel@tonic-gate * Copyright (c) 1983, 1993
35557c478bd9Sstevel@tonic-gate * The Regents of the University of California. All rights reserved.
35567c478bd9Sstevel@tonic-gate *
35577c478bd9Sstevel@tonic-gate * Redistribution and use in source and binary forms, with or without
35587c478bd9Sstevel@tonic-gate * modification, are permitted provided that the following conditions
35597c478bd9Sstevel@tonic-gate * are met:
35607c478bd9Sstevel@tonic-gate * 1. Redistributions of source code must retain the above copyright
35617c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer.
35627c478bd9Sstevel@tonic-gate * 2. Redistributions in binary form must reproduce the above copyright
35637c478bd9Sstevel@tonic-gate * notice, this list of conditions and the following disclaimer in the
35647c478bd9Sstevel@tonic-gate * documentation and/or other materials provided with the distribution.
35657c478bd9Sstevel@tonic-gate * 3. All advertising materials mentioning features or use of this software
35667c478bd9Sstevel@tonic-gate * must display the following acknowledgement:
35677c478bd9Sstevel@tonic-gate * This product includes software developed by the University of
35687c478bd9Sstevel@tonic-gate * California, Berkeley and its contributors.
35697c478bd9Sstevel@tonic-gate * 4. Neither the name of the University nor the names of its contributors
35707c478bd9Sstevel@tonic-gate * may be used to endorse or promote products derived from this software
35717c478bd9Sstevel@tonic-gate * without specific prior written permission.
35727c478bd9Sstevel@tonic-gate *
35737c478bd9Sstevel@tonic-gate * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
35747c478bd9Sstevel@tonic-gate * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
35757c478bd9Sstevel@tonic-gate * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
35767c478bd9Sstevel@tonic-gate * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35777c478bd9Sstevel@tonic-gate * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
35787c478bd9Sstevel@tonic-gate * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35797c478bd9Sstevel@tonic-gate * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35807c478bd9Sstevel@tonic-gate * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35817c478bd9Sstevel@tonic-gate * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35827c478bd9Sstevel@tonic-gate * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35837c478bd9Sstevel@tonic-gate * SUCH DAMAGE.
35847c478bd9Sstevel@tonic-gate */
35857c478bd9Sstevel@tonic-gate
35867c478bd9Sstevel@tonic-gate /* Type 3 -- x**31 + x**3 + 1 */
35877c478bd9Sstevel@tonic-gate #define DEG_3 31
35887c478bd9Sstevel@tonic-gate #define SEP_3 3
35897c478bd9Sstevel@tonic-gate
35907c478bd9Sstevel@tonic-gate
35917c478bd9Sstevel@tonic-gate /* Protected by tcp_random_lock */
35927c478bd9Sstevel@tonic-gate static int tcp_randtbl[DEG_3 + 1];
35937c478bd9Sstevel@tonic-gate
35947c478bd9Sstevel@tonic-gate static int *tcp_random_fptr = &tcp_randtbl[SEP_3 + 1];
35957c478bd9Sstevel@tonic-gate static int *tcp_random_rptr = &tcp_randtbl[1];
35967c478bd9Sstevel@tonic-gate
35977c478bd9Sstevel@tonic-gate static int *tcp_random_state = &tcp_randtbl[1];
35987c478bd9Sstevel@tonic-gate static int *tcp_random_end_ptr = &tcp_randtbl[DEG_3 + 1];
35997c478bd9Sstevel@tonic-gate
36007c478bd9Sstevel@tonic-gate kmutex_t tcp_random_lock;
36017c478bd9Sstevel@tonic-gate
36027c478bd9Sstevel@tonic-gate void
tcp_random_init(void)36037c478bd9Sstevel@tonic-gate tcp_random_init(void)
36047c478bd9Sstevel@tonic-gate {
36057c478bd9Sstevel@tonic-gate int i;
36067c478bd9Sstevel@tonic-gate hrtime_t hrt;
36077c478bd9Sstevel@tonic-gate time_t wallclock;
36087c478bd9Sstevel@tonic-gate uint64_t result;
36097c478bd9Sstevel@tonic-gate
36107c478bd9Sstevel@tonic-gate /*
36117c478bd9Sstevel@tonic-gate * Use high-res timer and current time for seed. Gethrtime() returns
36127c478bd9Sstevel@tonic-gate * a longlong, which may contain resolution down to nanoseconds.
36137c478bd9Sstevel@tonic-gate * The current time will either be a 32-bit or a 64-bit quantity.
36147c478bd9Sstevel@tonic-gate * XOR the two together in a 64-bit result variable.
36157c478bd9Sstevel@tonic-gate * Convert the result to a 32-bit value by multiplying the high-order
36167c478bd9Sstevel@tonic-gate * 32-bits by the low-order 32-bits.
36177c478bd9Sstevel@tonic-gate */
36187c478bd9Sstevel@tonic-gate
36197c478bd9Sstevel@tonic-gate hrt = gethrtime();
36207c478bd9Sstevel@tonic-gate (void) drv_getparm(TIME, &wallclock);
36217c478bd9Sstevel@tonic-gate result = (uint64_t)wallclock ^ (uint64_t)hrt;
36227c478bd9Sstevel@tonic-gate mutex_enter(&tcp_random_lock);
36237c478bd9Sstevel@tonic-gate tcp_random_state[0] = ((result >> 32) & 0xffffffff) *
36247c478bd9Sstevel@tonic-gate (result & 0xffffffff);
36257c478bd9Sstevel@tonic-gate
36267c478bd9Sstevel@tonic-gate for (i = 1; i < DEG_3; i++)
36277c478bd9Sstevel@tonic-gate tcp_random_state[i] = 1103515245 * tcp_random_state[i - 1]
36287c478bd9Sstevel@tonic-gate + 12345;
36297c478bd9Sstevel@tonic-gate tcp_random_fptr = &tcp_random_state[SEP_3];
36307c478bd9Sstevel@tonic-gate tcp_random_rptr = &tcp_random_state[0];
36317c478bd9Sstevel@tonic-gate mutex_exit(&tcp_random_lock);
36327c478bd9Sstevel@tonic-gate for (i = 0; i < 10 * DEG_3; i++)
36337c478bd9Sstevel@tonic-gate (void) tcp_random();
36347c478bd9Sstevel@tonic-gate }
36357c478bd9Sstevel@tonic-gate
36367c478bd9Sstevel@tonic-gate /*
36377c478bd9Sstevel@tonic-gate * tcp_random: Return a random number in the range [1 - (128K + 1)].
36387c478bd9Sstevel@tonic-gate * This range is selected to be approximately centered on TCP_ISS / 2,
36397c478bd9Sstevel@tonic-gate * and easy to compute. We get this value by generating a 32-bit random
36407c478bd9Sstevel@tonic-gate * number, selecting out the high-order 17 bits, and then adding one so
36417c478bd9Sstevel@tonic-gate * that we never return zero.
36427c478bd9Sstevel@tonic-gate */
36437c478bd9Sstevel@tonic-gate int
tcp_random(void)36447c478bd9Sstevel@tonic-gate tcp_random(void)
36457c478bd9Sstevel@tonic-gate {
36467c478bd9Sstevel@tonic-gate int i;
36477c478bd9Sstevel@tonic-gate
36487c478bd9Sstevel@tonic-gate mutex_enter(&tcp_random_lock);
36497c478bd9Sstevel@tonic-gate *tcp_random_fptr += *tcp_random_rptr;
36507c478bd9Sstevel@tonic-gate
36517c478bd9Sstevel@tonic-gate /*
36527c478bd9Sstevel@tonic-gate * The high-order bits are more random than the low-order bits,
36537c478bd9Sstevel@tonic-gate * so we select out the high-order 17 bits and add one so that
36547c478bd9Sstevel@tonic-gate * we never return zero.
36557c478bd9Sstevel@tonic-gate */
36567c478bd9Sstevel@tonic-gate i = ((*tcp_random_fptr >> 15) & 0x1ffff) + 1;
36577c478bd9Sstevel@tonic-gate if (++tcp_random_fptr >= tcp_random_end_ptr) {
36587c478bd9Sstevel@tonic-gate tcp_random_fptr = tcp_random_state;
36597c478bd9Sstevel@tonic-gate ++tcp_random_rptr;
36607c478bd9Sstevel@tonic-gate } else if (++tcp_random_rptr >= tcp_random_end_ptr)
36617c478bd9Sstevel@tonic-gate tcp_random_rptr = tcp_random_state;
36627c478bd9Sstevel@tonic-gate
36637c478bd9Sstevel@tonic-gate mutex_exit(&tcp_random_lock);
36647c478bd9Sstevel@tonic-gate return (i);
36657c478bd9Sstevel@tonic-gate }
36667c478bd9Sstevel@tonic-gate
36677c478bd9Sstevel@tonic-gate /*
36687c478bd9Sstevel@tonic-gate * Split this function out so that if the secret changes, I'm okay.
36697c478bd9Sstevel@tonic-gate *
36707c478bd9Sstevel@tonic-gate * Initialize the tcp_iss_cookie and tcp_iss_key.
36717c478bd9Sstevel@tonic-gate */
36727c478bd9Sstevel@tonic-gate
36737c478bd9Sstevel@tonic-gate #define PASSWD_SIZE 16 /* MUST be multiple of 4 */
36747c478bd9Sstevel@tonic-gate
36756e91bba0SGirish Moodalbail void
tcp_iss_key_init(uint8_t * phrase,int len,tcp_stack_t * tcps)3676f4b3ec61Sdh155122 tcp_iss_key_init(uint8_t *phrase, int len, tcp_stack_t *tcps)
36777c478bd9Sstevel@tonic-gate {
36787c478bd9Sstevel@tonic-gate struct {
36797c478bd9Sstevel@tonic-gate int32_t current_time;
36807c478bd9Sstevel@tonic-gate uint32_t randnum;
36817c478bd9Sstevel@tonic-gate uint16_t pad;
36827c478bd9Sstevel@tonic-gate uint8_t ether[6];
36837c478bd9Sstevel@tonic-gate uint8_t passwd[PASSWD_SIZE];
36847c478bd9Sstevel@tonic-gate } tcp_iss_cookie;
36857c478bd9Sstevel@tonic-gate time_t t;
36867c478bd9Sstevel@tonic-gate
36877c478bd9Sstevel@tonic-gate /*
36887c478bd9Sstevel@tonic-gate * Start with the current absolute time.
36897c478bd9Sstevel@tonic-gate */
36907c478bd9Sstevel@tonic-gate (void) drv_getparm(TIME, &t);
36917c478bd9Sstevel@tonic-gate tcp_iss_cookie.current_time = t;
36927c478bd9Sstevel@tonic-gate
36937c478bd9Sstevel@tonic-gate /*
36947c478bd9Sstevel@tonic-gate * XXX - Need a more random number per RFC 1750, not this crap.
36957c478bd9Sstevel@tonic-gate * OTOH, if what follows is pretty random, then I'm in better shape.
36967c478bd9Sstevel@tonic-gate */
36977c478bd9Sstevel@tonic-gate tcp_iss_cookie.randnum = (uint32_t)(gethrtime() + tcp_random());
36987c478bd9Sstevel@tonic-gate tcp_iss_cookie.pad = 0x365c; /* Picked from HMAC pad values. */
36997c478bd9Sstevel@tonic-gate
37007c478bd9Sstevel@tonic-gate /*
37017c478bd9Sstevel@tonic-gate * The cpu_type_info is pretty non-random. Ugggh. It does serve
37027c478bd9Sstevel@tonic-gate * as a good template.
37037c478bd9Sstevel@tonic-gate */
37047c478bd9Sstevel@tonic-gate bcopy(&cpu_list->cpu_type_info, &tcp_iss_cookie.passwd,
37057c478bd9Sstevel@tonic-gate min(PASSWD_SIZE, sizeof (cpu_list->cpu_type_info)));
37067c478bd9Sstevel@tonic-gate
37077c478bd9Sstevel@tonic-gate /*
37087c478bd9Sstevel@tonic-gate * The pass-phrase. Normally this is supplied by user-called NDD.
37097c478bd9Sstevel@tonic-gate */
37107c478bd9Sstevel@tonic-gate bcopy(phrase, &tcp_iss_cookie.passwd, min(PASSWD_SIZE, len));
37117c478bd9Sstevel@tonic-gate
37127c478bd9Sstevel@tonic-gate /*
37137c478bd9Sstevel@tonic-gate * See 4010593 if this section becomes a problem again,
37147c478bd9Sstevel@tonic-gate * but the local ethernet address is useful here.
37157c478bd9Sstevel@tonic-gate */
37167c478bd9Sstevel@tonic-gate (void) localetheraddr(NULL,
37177c478bd9Sstevel@tonic-gate (struct ether_addr *)&tcp_iss_cookie.ether);
37187c478bd9Sstevel@tonic-gate
37197c478bd9Sstevel@tonic-gate /*
37207c478bd9Sstevel@tonic-gate * Hash 'em all together. The MD5Final is called per-connection.
37217c478bd9Sstevel@tonic-gate */
3722f4b3ec61Sdh155122 mutex_enter(&tcps->tcps_iss_key_lock);
3723f4b3ec61Sdh155122 MD5Init(&tcps->tcps_iss_key);
3724f4b3ec61Sdh155122 MD5Update(&tcps->tcps_iss_key, (uchar_t *)&tcp_iss_cookie,
37257c478bd9Sstevel@tonic-gate sizeof (tcp_iss_cookie));
3726f4b3ec61Sdh155122 mutex_exit(&tcps->tcps_iss_key_lock);
37277c478bd9Sstevel@tonic-gate }
37287c478bd9Sstevel@tonic-gate
37297c478bd9Sstevel@tonic-gate /*
3730f4b3ec61Sdh155122 * Called by IP when IP is loaded into the kernel
37317c478bd9Sstevel@tonic-gate */
3732f4b3ec61Sdh155122 void
tcp_ddi_g_init(void)3733f4b3ec61Sdh155122 tcp_ddi_g_init(void)
3734f4b3ec61Sdh155122 {
37357c478bd9Sstevel@tonic-gate tcp_timercache = kmem_cache_create("tcp_timercache",
37367c478bd9Sstevel@tonic-gate sizeof (tcp_timer_t) + sizeof (mblk_t), 0,
37377c478bd9Sstevel@tonic-gate NULL, NULL, NULL, NULL, NULL, 0);
37387c478bd9Sstevel@tonic-gate
373966cd0f60SKacheong Poon tcp_notsack_blk_cache = kmem_cache_create("tcp_notsack_blk_cache",
374066cd0f60SKacheong Poon sizeof (notsack_blk_t), 0, NULL, NULL, NULL, NULL, NULL, 0);
37417c478bd9Sstevel@tonic-gate
3742f4b3ec61Sdh155122 mutex_init(&tcp_random_lock, NULL, MUTEX_DEFAULT, NULL);
37437c478bd9Sstevel@tonic-gate
37447c478bd9Sstevel@tonic-gate /* Initialize the random number generator */
37457c478bd9Sstevel@tonic-gate tcp_random_init();
37467c478bd9Sstevel@tonic-gate
3747f4b3ec61Sdh155122 /* A single callback independently of how many netstacks we have */
3748f4b3ec61Sdh155122 ip_squeue_init(tcp_squeue_add);
3749f4b3ec61Sdh155122
3750f4b3ec61Sdh155122 tcp_g_kstat = tcp_g_kstat_init(&tcp_g_statistics);
3751f4b3ec61Sdh155122
3752da14cebeSEric Cheng tcp_squeue_flag = tcp_squeue_switch(tcp_squeue_wput);
3753da14cebeSEric Cheng
3754f4b3ec61Sdh155122 /*
3755f4b3ec61Sdh155122 * We want to be informed each time a stack is created or
3756f4b3ec61Sdh155122 * destroyed in the kernel, so we can maintain the
3757f4b3ec61Sdh155122 * set of tcp_stack_t's.
3758f4b3ec61Sdh155122 */
3759bd670b35SErik Nordmark netstack_register(NS_TCP, tcp_stack_init, NULL, tcp_stack_fini);
3760f4b3ec61Sdh155122 }
3761f4b3ec61Sdh155122
3762f4b3ec61Sdh155122
37630f1702c5SYu Xiangning #define INET_NAME "ip"
37640f1702c5SYu Xiangning
3765f4b3ec61Sdh155122 /*
3766f4b3ec61Sdh155122 * Initialize the TCP stack instance.
3767f4b3ec61Sdh155122 */
3768f4b3ec61Sdh155122 static void *
tcp_stack_init(netstackid_t stackid,netstack_t * ns)3769f4b3ec61Sdh155122 tcp_stack_init(netstackid_t stackid, netstack_t *ns)
3770f4b3ec61Sdh155122 {
3771f4b3ec61Sdh155122 tcp_stack_t *tcps;
3772f4b3ec61Sdh155122 int i;
37730f1702c5SYu Xiangning int error = 0;
37740f1702c5SYu Xiangning major_t major;
37756e91bba0SGirish Moodalbail size_t arrsz;
3776f4b3ec61Sdh155122
3777f4b3ec61Sdh155122 tcps = (tcp_stack_t *)kmem_zalloc(sizeof (*tcps), KM_SLEEP);
3778f4b3ec61Sdh155122 tcps->tcps_netstack = ns;
3779f4b3ec61Sdh155122
3780f4b3ec61Sdh155122 /* Initialize locks */
3781f4b3ec61Sdh155122 mutex_init(&tcps->tcps_iss_key_lock, NULL, MUTEX_DEFAULT, NULL);
3782f4b3ec61Sdh155122 mutex_init(&tcps->tcps_epriv_port_lock, NULL, MUTEX_DEFAULT, NULL);
3783f4b3ec61Sdh155122
3784f4b3ec61Sdh155122 tcps->tcps_g_num_epriv_ports = TCP_NUM_EPRIV_PORTS;
37856e91bba0SGirish Moodalbail tcps->tcps_g_epriv_ports[0] = ULP_DEF_EPRIV_PORT1;
37866e91bba0SGirish Moodalbail tcps->tcps_g_epriv_ports[1] = ULP_DEF_EPRIV_PORT2;
3787f4b3ec61Sdh155122 tcps->tcps_min_anonpriv_port = 512;
3788f4b3ec61Sdh155122
3789f4b3ec61Sdh155122 tcps->tcps_bind_fanout = kmem_zalloc(sizeof (tf_t) *
3790f4b3ec61Sdh155122 TCP_BIND_FANOUT_SIZE, KM_SLEEP);
3791f4b3ec61Sdh155122 tcps->tcps_acceptor_fanout = kmem_zalloc(sizeof (tf_t) *
379293fcb0b9SKacheong Poon TCP_ACCEPTOR_FANOUT_SIZE, KM_SLEEP);
3793f4b3ec61Sdh155122
3794f4b3ec61Sdh155122 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) {
3795f4b3ec61Sdh155122 mutex_init(&tcps->tcps_bind_fanout[i].tf_lock, NULL,
3796f4b3ec61Sdh155122 MUTEX_DEFAULT, NULL);
3797f4b3ec61Sdh155122 }
3798f4b3ec61Sdh155122
379993fcb0b9SKacheong Poon for (i = 0; i < TCP_ACCEPTOR_FANOUT_SIZE; i++) {
3800f4b3ec61Sdh155122 mutex_init(&tcps->tcps_acceptor_fanout[i].tf_lock, NULL,
3801f4b3ec61Sdh155122 MUTEX_DEFAULT, NULL);
3802f4b3ec61Sdh155122 }
3803f4b3ec61Sdh155122
3804f4b3ec61Sdh155122 /* TCP's IPsec code calls the packet dropper. */
3805f4b3ec61Sdh155122 ip_drop_register(&tcps->tcps_dropper, "TCP IPsec policy enforcement");
3806f4b3ec61Sdh155122
38076e91bba0SGirish Moodalbail arrsz = tcp_propinfo_count * sizeof (mod_prop_info_t);
38086e91bba0SGirish Moodalbail tcps->tcps_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz,
38096e91bba0SGirish Moodalbail KM_SLEEP);
38106e91bba0SGirish Moodalbail bcopy(tcp_propinfo_tbl, tcps->tcps_propinfo_tbl, arrsz);
3811f4b3ec61Sdh155122
3812f4b3ec61Sdh155122 /*
3813f4b3ec61Sdh155122 * Note: To really walk the device tree you need the devinfo
3814f4b3ec61Sdh155122 * pointer to your device which is only available after probe/attach.
3815f4b3ec61Sdh155122 * The following is safe only because it uses ddi_root_node()
3816f4b3ec61Sdh155122 */
3817f4b3ec61Sdh155122 tcp_max_optsize = optcom_max_optsize(tcp_opt_obj.odb_opt_des_arr,
3818f4b3ec61Sdh155122 tcp_opt_obj.odb_opt_arr_cnt);
3819f4b3ec61Sdh155122
38207c478bd9Sstevel@tonic-gate /*
38217c478bd9Sstevel@tonic-gate * Initialize RFC 1948 secret values. This will probably be reset once
38227c478bd9Sstevel@tonic-gate * by the boot scripts.
38237c478bd9Sstevel@tonic-gate *
38247c478bd9Sstevel@tonic-gate * Use NULL name, as the name is caught by the new lockstats.
38257c478bd9Sstevel@tonic-gate *
38267c478bd9Sstevel@tonic-gate * Initialize with some random, non-guessable string, like the global
38277c478bd9Sstevel@tonic-gate * T_INFO_ACK.
38287c478bd9Sstevel@tonic-gate */
38297c478bd9Sstevel@tonic-gate
38307c478bd9Sstevel@tonic-gate tcp_iss_key_init((uint8_t *)&tcp_g_t_info_ack,
3831f4b3ec61Sdh155122 sizeof (tcp_g_t_info_ack), tcps);
38327c478bd9Sstevel@tonic-gate
3833721fffe3SKacheong Poon tcps->tcps_kstat = tcp_kstat2_init(stackid);
3834721fffe3SKacheong Poon tcps->tcps_mibkp = tcp_kstat_init(stackid);
3835f4b3ec61Sdh155122
38360f1702c5SYu Xiangning major = mod_name_to_major(INET_NAME);
38370f1702c5SYu Xiangning error = ldi_ident_from_major(major, &tcps->tcps_ldi_ident);
38380f1702c5SYu Xiangning ASSERT(error == 0);
3839bd670b35SErik Nordmark tcps->tcps_ixa_cleanup_mp = allocb_wait(0, BPRI_MED, STR_NOSIG, NULL);
3840bd670b35SErik Nordmark ASSERT(tcps->tcps_ixa_cleanup_mp != NULL);
38417c6d7024SJerry Jelinek cv_init(&tcps->tcps_ixa_cleanup_ready_cv, NULL, CV_DEFAULT, NULL);
38427c6d7024SJerry Jelinek cv_init(&tcps->tcps_ixa_cleanup_done_cv, NULL, CV_DEFAULT, NULL);
3843bd670b35SErik Nordmark mutex_init(&tcps->tcps_ixa_cleanup_lock, NULL, MUTEX_DEFAULT, NULL);
3844bd670b35SErik Nordmark
384593fcb0b9SKacheong Poon mutex_init(&tcps->tcps_reclaim_lock, NULL, MUTEX_DEFAULT, NULL);
384693fcb0b9SKacheong Poon tcps->tcps_reclaim = B_FALSE;
384793fcb0b9SKacheong Poon tcps->tcps_reclaim_tid = 0;
3848721fffe3SKacheong Poon tcps->tcps_reclaim_period = tcps->tcps_rexmit_interval_max;
3849721fffe3SKacheong Poon
3850721fffe3SKacheong Poon /*
3851721fffe3SKacheong Poon * ncpus is the current number of CPUs, which can be bigger than
3852721fffe3SKacheong Poon * boot_ncpus. But we don't want to use ncpus to allocate all the
3853721fffe3SKacheong Poon * tcp_stats_cpu_t at system boot up time since it will be 1. While
3854721fffe3SKacheong Poon * we handle adding CPU in tcp_cpu_update(), it will be slow if
3855721fffe3SKacheong Poon * there are many CPUs as we will be adding them 1 by 1.
3856721fffe3SKacheong Poon *
3857721fffe3SKacheong Poon * Note that tcps_sc_cnt never decreases and the tcps_sc[x] pointers
3858721fffe3SKacheong Poon * are not freed until the stack is going away. So there is no need
3859721fffe3SKacheong Poon * to grab a lock to access the per CPU tcps_sc[x] pointer.
3860721fffe3SKacheong Poon */
38615dd46ab5SKacheong Poon mutex_enter(&cpu_lock);
3862721fffe3SKacheong Poon tcps->tcps_sc_cnt = MAX(ncpus, boot_ncpus);
38635dd46ab5SKacheong Poon mutex_exit(&cpu_lock);
3864721fffe3SKacheong Poon tcps->tcps_sc = kmem_zalloc(max_ncpus * sizeof (tcp_stats_cpu_t *),
3865721fffe3SKacheong Poon KM_SLEEP);
3866721fffe3SKacheong Poon for (i = 0; i < tcps->tcps_sc_cnt; i++) {
3867721fffe3SKacheong Poon tcps->tcps_sc[i] = kmem_zalloc(sizeof (tcp_stats_cpu_t),
3868721fffe3SKacheong Poon KM_SLEEP);
3869721fffe3SKacheong Poon }
387093fcb0b9SKacheong Poon
387193fcb0b9SKacheong Poon mutex_init(&tcps->tcps_listener_conf_lock, NULL, MUTEX_DEFAULT, NULL);
387293fcb0b9SKacheong Poon list_create(&tcps->tcps_listener_conf, sizeof (tcp_listener_t),
387393fcb0b9SKacheong Poon offsetof(tcp_listener_t, tl_link));
387493fcb0b9SKacheong Poon
387545a4b79dSSebastien Roy tcps->tcps_default_cc_algo = cc_load_algo(CC_DEFAULT_ALGO_NAME);
387645a4b79dSSebastien Roy VERIFY3P(tcps->tcps_default_cc_algo, !=, NULL);
387745a4b79dSSebastien Roy
3878*1edba515SAndy Fiddaman tcpsig_init(tcps);
3879*1edba515SAndy Fiddaman
3880f4b3ec61Sdh155122 return (tcps);
38817c478bd9Sstevel@tonic-gate }
3882ff550d0eSmasputra
3883f4b3ec61Sdh155122 /*
3884f4b3ec61Sdh155122 * Called when the IP module is about to be unloaded.
3885f4b3ec61Sdh155122 */
38867c478bd9Sstevel@tonic-gate void
tcp_ddi_g_destroy(void)3887f4b3ec61Sdh155122 tcp_ddi_g_destroy(void)
38887c478bd9Sstevel@tonic-gate {
3889f4b3ec61Sdh155122 tcp_g_kstat_fini(tcp_g_kstat);
3890f4b3ec61Sdh155122 tcp_g_kstat = NULL;
3891f4b3ec61Sdh155122 bzero(&tcp_g_statistics, sizeof (tcp_g_statistics));
38927c478bd9Sstevel@tonic-gate
38937c478bd9Sstevel@tonic-gate mutex_destroy(&tcp_random_lock);
38947c478bd9Sstevel@tonic-gate
38957c478bd9Sstevel@tonic-gate kmem_cache_destroy(tcp_timercache);
389666cd0f60SKacheong Poon kmem_cache_destroy(tcp_notsack_blk_cache);
38977c478bd9Sstevel@tonic-gate
3898f4b3ec61Sdh155122 netstack_unregister(NS_TCP);
3899f4b3ec61Sdh155122 }
3900f4b3ec61Sdh155122
3901f4b3ec61Sdh155122 /*
3902f4b3ec61Sdh155122 * Free the TCP stack instance.
3903f4b3ec61Sdh155122 */
3904f4b3ec61Sdh155122 static void
tcp_stack_fini(netstackid_t stackid,void * arg)3905f4b3ec61Sdh155122 tcp_stack_fini(netstackid_t stackid, void *arg)
3906f4b3ec61Sdh155122 {
3907f4b3ec61Sdh155122 tcp_stack_t *tcps = (tcp_stack_t *)arg;
3908f4b3ec61Sdh155122 int i;
3909f4b3ec61Sdh155122
3910bd670b35SErik Nordmark freeb(tcps->tcps_ixa_cleanup_mp);
3911bd670b35SErik Nordmark tcps->tcps_ixa_cleanup_mp = NULL;
39127c6d7024SJerry Jelinek cv_destroy(&tcps->tcps_ixa_cleanup_ready_cv);
39137c6d7024SJerry Jelinek cv_destroy(&tcps->tcps_ixa_cleanup_done_cv);
3914bd670b35SErik Nordmark mutex_destroy(&tcps->tcps_ixa_cleanup_lock);
3915bd670b35SErik Nordmark
3916721fffe3SKacheong Poon /*
3917721fffe3SKacheong Poon * Set tcps_reclaim to false tells tcp_reclaim_timer() not to restart
3918721fffe3SKacheong Poon * the timer.
3919721fffe3SKacheong Poon */
3920721fffe3SKacheong Poon mutex_enter(&tcps->tcps_reclaim_lock);
3921721fffe3SKacheong Poon tcps->tcps_reclaim = B_FALSE;
3922721fffe3SKacheong Poon mutex_exit(&tcps->tcps_reclaim_lock);
392393fcb0b9SKacheong Poon if (tcps->tcps_reclaim_tid != 0)
392493fcb0b9SKacheong Poon (void) untimeout(tcps->tcps_reclaim_tid);
392593fcb0b9SKacheong Poon mutex_destroy(&tcps->tcps_reclaim_lock);
392693fcb0b9SKacheong Poon
392793fcb0b9SKacheong Poon tcp_listener_conf_cleanup(tcps);
392893fcb0b9SKacheong Poon
3929721fffe3SKacheong Poon for (i = 0; i < tcps->tcps_sc_cnt; i++)
3930721fffe3SKacheong Poon kmem_free(tcps->tcps_sc[i], sizeof (tcp_stats_cpu_t));
3931721fffe3SKacheong Poon kmem_free(tcps->tcps_sc, max_ncpus * sizeof (tcp_stats_cpu_t *));
3932721fffe3SKacheong Poon
39336e91bba0SGirish Moodalbail kmem_free(tcps->tcps_propinfo_tbl,
39346e91bba0SGirish Moodalbail tcp_propinfo_count * sizeof (mod_prop_info_t));
39356e91bba0SGirish Moodalbail tcps->tcps_propinfo_tbl = NULL;
3936f4b3ec61Sdh155122
3937f4b3ec61Sdh155122 for (i = 0; i < TCP_BIND_FANOUT_SIZE; i++) {
3938f4b3ec61Sdh155122 ASSERT(tcps->tcps_bind_fanout[i].tf_tcp == NULL);
3939f4b3ec61Sdh155122 mutex_destroy(&tcps->tcps_bind_fanout[i].tf_lock);
3940f4b3ec61Sdh155122 }
3941f4b3ec61Sdh155122
394293fcb0b9SKacheong Poon for (i = 0; i < TCP_ACCEPTOR_FANOUT_SIZE; i++) {
3943f4b3ec61Sdh155122 ASSERT(tcps->tcps_acceptor_fanout[i].tf_tcp == NULL);
3944f4b3ec61Sdh155122 mutex_destroy(&tcps->tcps_acceptor_fanout[i].tf_lock);
3945f4b3ec61Sdh155122 }
3946f4b3ec61Sdh155122
3947f4b3ec61Sdh155122 kmem_free(tcps->tcps_bind_fanout, sizeof (tf_t) * TCP_BIND_FANOUT_SIZE);
3948f4b3ec61Sdh155122 tcps->tcps_bind_fanout = NULL;
3949f4b3ec61Sdh155122
395093fcb0b9SKacheong Poon kmem_free(tcps->tcps_acceptor_fanout, sizeof (tf_t) *
395193fcb0b9SKacheong Poon TCP_ACCEPTOR_FANOUT_SIZE);
3952f4b3ec61Sdh155122 tcps->tcps_acceptor_fanout = NULL;
3953f4b3ec61Sdh155122
3954f4b3ec61Sdh155122 mutex_destroy(&tcps->tcps_iss_key_lock);
3955f4b3ec61Sdh155122 mutex_destroy(&tcps->tcps_epriv_port_lock);
3956f4b3ec61Sdh155122
3957f4b3ec61Sdh155122 ip_drop_unregister(&tcps->tcps_dropper);
3958f4b3ec61Sdh155122
3959f4b3ec61Sdh155122 tcp_kstat2_fini(stackid, tcps->tcps_kstat);
3960f4b3ec61Sdh155122 tcps->tcps_kstat = NULL;
3961f4b3ec61Sdh155122
3962f4b3ec61Sdh155122 tcp_kstat_fini(stackid, tcps->tcps_mibkp);
3963f4b3ec61Sdh155122 tcps->tcps_mibkp = NULL;
3964f4b3ec61Sdh155122
3965*1edba515SAndy Fiddaman tcpsig_fini(tcps);
3966*1edba515SAndy Fiddaman
39670f1702c5SYu Xiangning ldi_ident_release(tcps->tcps_ldi_ident);
3968f4b3ec61Sdh155122 kmem_free(tcps, sizeof (*tcps));
39697c478bd9Sstevel@tonic-gate }
39707c478bd9Sstevel@tonic-gate
39717c478bd9Sstevel@tonic-gate /*
39727c478bd9Sstevel@tonic-gate * Generate ISS, taking into account NDD changes may happen halfway through.
39737c478bd9Sstevel@tonic-gate * (If the iss is not zero, set it.)
39747c478bd9Sstevel@tonic-gate */
39757c478bd9Sstevel@tonic-gate
39767c478bd9Sstevel@tonic-gate static void
tcp_iss_init(tcp_t * tcp)39777c478bd9Sstevel@tonic-gate tcp_iss_init(tcp_t *tcp)
39787c478bd9Sstevel@tonic-gate {
39797c478bd9Sstevel@tonic-gate MD5_CTX context;
39807c478bd9Sstevel@tonic-gate struct { uint32_t ports; in6_addr_t src; in6_addr_t dst; } arg;
39817c478bd9Sstevel@tonic-gate uint32_t answer[4];
3982f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
3983bd670b35SErik Nordmark conn_t *connp = tcp->tcp_connp;
39847c478bd9Sstevel@tonic-gate
3985c0e6663fSJerry Jelinek tcps->tcps_iss_incr_extra += (tcps->tcps_iss_incr >> 1);
3986f4b3ec61Sdh155122 tcp->tcp_iss = tcps->tcps_iss_incr_extra;
3987f4b3ec61Sdh155122 switch (tcps->tcps_strong_iss) {
39887c478bd9Sstevel@tonic-gate case 2:
3989f4b3ec61Sdh155122 mutex_enter(&tcps->tcps_iss_key_lock);
3990f4b3ec61Sdh155122 context = tcps->tcps_iss_key;
3991f4b3ec61Sdh155122 mutex_exit(&tcps->tcps_iss_key_lock);
3992bd670b35SErik Nordmark arg.ports = connp->conn_ports;
3993bd670b35SErik Nordmark arg.src = connp->conn_laddr_v6;
3994bd670b35SErik Nordmark arg.dst = connp->conn_faddr_v6;
39957c478bd9Sstevel@tonic-gate MD5Update(&context, (uchar_t *)&arg, sizeof (arg));
39967c478bd9Sstevel@tonic-gate MD5Final((uchar_t *)answer, &context);
39977c478bd9Sstevel@tonic-gate tcp->tcp_iss += answer[0] ^ answer[1] ^ answer[2] ^ answer[3];
39987c478bd9Sstevel@tonic-gate /*
39997c478bd9Sstevel@tonic-gate * Now that we've hashed into a unique per-connection sequence
40007c478bd9Sstevel@tonic-gate * space, add a random increment per strong_iss == 1. So I
40017c478bd9Sstevel@tonic-gate * guess we'll have to...
40027c478bd9Sstevel@tonic-gate */
40037c478bd9Sstevel@tonic-gate /* FALLTHRU */
40047c478bd9Sstevel@tonic-gate case 1:
40057c478bd9Sstevel@tonic-gate tcp->tcp_iss += (gethrtime() >> ISS_NSEC_SHT) + tcp_random();
40067c478bd9Sstevel@tonic-gate break;
40077c478bd9Sstevel@tonic-gate default:
4008c0e6663fSJerry Jelinek tcp->tcp_iss += (uint32_t)gethrestime_sec() *
4009c0e6663fSJerry Jelinek tcps->tcps_iss_incr;
40107c478bd9Sstevel@tonic-gate break;
40117c478bd9Sstevel@tonic-gate }
40127c478bd9Sstevel@tonic-gate tcp->tcp_valid_bits = TCP_ISS_VALID;
40137c478bd9Sstevel@tonic-gate tcp->tcp_fss = tcp->tcp_iss - 1;
40147c478bd9Sstevel@tonic-gate tcp->tcp_suna = tcp->tcp_iss;
40157c478bd9Sstevel@tonic-gate tcp->tcp_snxt = tcp->tcp_iss + 1;
40167c478bd9Sstevel@tonic-gate tcp->tcp_rexmit_nxt = tcp->tcp_snxt;
40177c478bd9Sstevel@tonic-gate tcp->tcp_csuna = tcp->tcp_snxt;
40187c478bd9Sstevel@tonic-gate }
40197c478bd9Sstevel@tonic-gate
40207c478bd9Sstevel@tonic-gate /*
4021ff550d0eSmasputra * tcp_{set,clr}qfull() functions are used to either set or clear QFULL
4022ff550d0eSmasputra * on the specified backing STREAMS q. Note, the caller may make the
4023ff550d0eSmasputra * decision to call based on the tcp_t.tcp_flow_stopped value which
4024ff550d0eSmasputra * when check outside the q's lock is only an advisory check ...
4025ff550d0eSmasputra */
4026ff550d0eSmasputra void
tcp_setqfull(tcp_t * tcp)40277c478bd9Sstevel@tonic-gate tcp_setqfull(tcp_t *tcp)
40287c478bd9Sstevel@tonic-gate {
4029f4b3ec61Sdh155122 tcp_stack_t *tcps = tcp->tcp_tcps;
40300f1702c5SYu Xiangning conn_t *connp = tcp->tcp_connp;
40310f1702c5SYu Xiangning
40320f1702c5SYu Xiangning if (tcp->tcp_closed)
40330f1702c5SYu Xiangning return;
40340f1702c5SYu Xiangning
4035bd670b35SErik Nordmark conn_setqfull(connp, &tcp->tcp_flow_stopped);
4036bd670b35SErik Nordmark if (tcp->tcp_flow_stopped)
4037f4b3ec61Sdh155122 TCP_STAT(tcps, tcp_flwctl_on);
40380f1702c5SYu Xiangning }
40397c478bd9Sstevel@tonic-gate
4040ff550d0eSmasputra void
tcp_clrqfull(tcp_t * tcp)40417c478bd9Sstevel@tonic-gate tcp_clrqfull(tcp_t *tcp)
40427c478bd9Sstevel@tonic-gate {
40430f1702c5SYu Xiangning conn_t *connp = tcp->tcp_connp;
40440f1702c5SYu Xiangning
40450f1702c5SYu Xiangning if (tcp->tcp_closed)
40460f1702c5SYu Xiangning return;
4047bd670b35SErik Nordmark conn_clrqfull(connp, &tcp->tcp_flow_stopped);
40480f1702c5SYu Xiangning }
4049f4b3ec61Sdh155122
4050da14cebeSEric Cheng static int
tcp_squeue_switch(int val)40517c478bd9Sstevel@tonic-gate tcp_squeue_switch(int val)
40527c478bd9Sstevel@tonic-gate {
4053da14cebeSEric Cheng int rval = SQ_FILL;
40547c478bd9Sstevel@tonic-gate
40557c478bd9Sstevel@tonic-gate switch (val) {
40567c478bd9Sstevel@tonic-gate case 1:
4057da14cebeSEric Cheng rval = SQ_NODRAIN;
40587c478bd9Sstevel@tonic-gate break;
40597c478bd9Sstevel@tonic-gate case 2:
4060da14cebeSEric Cheng rval = SQ_PROCESS;
40617c478bd9Sstevel@tonic-gate break;
40627c478bd9Sstevel@tonic-gate default:
40637c478bd9Sstevel@tonic-gate break;
40647c478bd9Sstevel@tonic-gate }
40657c478bd9Sstevel@tonic-gate return (rval);
40667c478bd9Sstevel@tonic-gate }
40677c478bd9Sstevel@tonic-gate
4068f4b3ec61Sdh155122 /*
4069f4b3ec61Sdh155122 * This is called once for each squeue - globally for all stack
4070f4b3ec61Sdh155122 * instances.
4071f4b3ec61Sdh155122 */
40727c478bd9Sstevel@tonic-gate static void
tcp_squeue_add(squeue_t * sqp)40737c478bd9Sstevel@tonic-gate tcp_squeue_add(squeue_t *sqp)
40747c478bd9Sstevel@tonic-gate {
40757c478bd9Sstevel@tonic-gate tcp_squeue_priv_t *tcp_time_wait = kmem_zalloc(
40767c478bd9Sstevel@tonic-gate sizeof (tcp_squeue_priv_t), KM_SLEEP);
40777c478bd9Sstevel@tonic-gate
40787c478bd9Sstevel@tonic-gate *squeue_getprivate(sqp, SQPRIVATE_TCP) = (intptr_t)tcp_time_wait;
40791dbf515bSethindra if (tcp_free_list_max_cnt == 0) {
40801dbf515bSethindra int tcp_ncpus = ((boot_max_ncpus == -1) ?
40811dbf515bSethindra max_ncpus : boot_max_ncpus);
40821dbf515bSethindra
40831dbf515bSethindra /*
40841dbf515bSethindra * Limit number of entries to 1% of availble memory / tcp_ncpus
40851dbf515bSethindra */
40861dbf515bSethindra tcp_free_list_max_cnt = (freemem * PAGESIZE) /
40871dbf515bSethindra (tcp_ncpus * sizeof (tcp_t) * 100);
40881dbf515bSethindra }
40891dbf515bSethindra tcp_time_wait->tcp_free_list_cnt = 0;
40907c478bd9Sstevel@tonic-gate }
40910f1702c5SYu Xiangning /*
40920f1702c5SYu Xiangning * Return unix error is tli error is TSYSERR, otherwise return a negative
40930f1702c5SYu Xiangning * tli error.
40940f1702c5SYu Xiangning */
40950f1702c5SYu Xiangning int
tcp_do_bind(conn_t * connp,struct sockaddr * sa,socklen_t len,cred_t * cr,boolean_t bind_to_req_port_only)40960f1702c5SYu Xiangning tcp_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len, cred_t *cr,
40970f1702c5SYu Xiangning boolean_t bind_to_req_port_only)
40980f1702c5SYu Xiangning {
40990f1702c5SYu Xiangning int error;
41000f1702c5SYu Xiangning tcp_t *tcp = connp->conn_tcp;
41010f1702c5SYu Xiangning
41020f1702c5SYu Xiangning if (tcp->tcp_state >= TCPS_BOUND) {
4103bd670b35SErik Nordmark if (connp->conn_debug) {
41040f1702c5SYu Xiangning (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
41050f1702c5SYu Xiangning "tcp_bind: bad state, %d", tcp->tcp_state);
41060f1702c5SYu Xiangning }
41070f1702c5SYu Xiangning return (-TOUTSTATE);
41080f1702c5SYu Xiangning }
41090f1702c5SYu Xiangning
41100f1702c5SYu Xiangning error = tcp_bind_check(connp, sa, len, cr, bind_to_req_port_only);
41110f1702c5SYu Xiangning if (error != 0)
41120f1702c5SYu Xiangning return (error);
41130f1702c5SYu Xiangning
41140f1702c5SYu Xiangning ASSERT(tcp->tcp_state == TCPS_BOUND);
41150f1702c5SYu Xiangning tcp->tcp_conn_req_max = 0;
4116bd670b35SErik Nordmark return (0);
41170f1702c5SYu Xiangning }
41180f1702c5SYu Xiangning
41190f1702c5SYu Xiangning /*
41200f1702c5SYu Xiangning * If the return value from this function is positive, it's a UNIX error.
41210f1702c5SYu Xiangning * Otherwise, if it's negative, then the absolute value is a TLI error.
41220f1702c5SYu Xiangning * the TPI routine tcp_tpi_connect() is a wrapper function for this.
41230f1702c5SYu Xiangning */
41240f1702c5SYu Xiangning int
tcp_do_connect(conn_t * connp,const struct sockaddr * sa,socklen_t len,cred_t * cr,pid_t pid)41250f1702c5SYu Xiangning tcp_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len,
41260f1702c5SYu Xiangning cred_t *cr, pid_t pid)
41270f1702c5SYu Xiangning {
41280f1702c5SYu Xiangning tcp_t *tcp = connp->conn_tcp;
41290f1702c5SYu Xiangning sin_t *sin = (sin_t *)sa;
41300f1702c5SYu Xiangning sin6_t *sin6 = (sin6_t *)sa;
41310f1702c5SYu Xiangning ipaddr_t *dstaddrp;
41320f1702c5SYu Xiangning in_port_t dstport;
41330f1702c5SYu Xiangning uint_t srcid;
4134bd670b35SErik Nordmark int error;
4135bd670b35SErik Nordmark uint32_t mss;
4136bd670b35SErik Nordmark mblk_t *syn_mp;
4137bd670b35SErik Nordmark tcp_stack_t *tcps = tcp->tcp_tcps;
4138bd670b35SErik Nordmark int32_t oldstate;
4139bd670b35SErik Nordmark ip_xmit_attr_t *ixa = connp->conn_ixa;
4140bd670b35SErik Nordmark
4141bd670b35SErik Nordmark oldstate = tcp->tcp_state;
41420f1702c5SYu Xiangning
41430f1702c5SYu Xiangning switch (len) {
41440f1702c5SYu Xiangning default:
41450f1702c5SYu Xiangning /*
41460f1702c5SYu Xiangning * Should never happen
41470f1702c5SYu Xiangning */
41480f1702c5SYu Xiangning return (EINVAL);
41490f1702c5SYu Xiangning
41500f1702c5SYu Xiangning case sizeof (sin_t):
41510f1702c5SYu Xiangning sin = (sin_t *)sa;
41520f1702c5SYu Xiangning if (sin->sin_port == 0) {
41530f1702c5SYu Xiangning return (-TBADADDR);
41540f1702c5SYu Xiangning }
4155bd670b35SErik Nordmark if (connp->conn_ipv6_v6only) {
41560f1702c5SYu Xiangning return (EAFNOSUPPORT);
41570f1702c5SYu Xiangning }
41580f1702c5SYu Xiangning break;
41590f1702c5SYu Xiangning
41600f1702c5SYu Xiangning case sizeof (sin6_t):
41610f1702c5SYu Xiangning sin6 = (sin6_t *)sa;
41620f1702c5SYu Xiangning if (sin6->sin6_port == 0) {
41630f1702c5SYu Xiangning return (-TBADADDR);
41640f1702c5SYu Xiangning }
41650f1702c5SYu Xiangning break;
41660f1702c5SYu Xiangning }
41670f1702c5SYu Xiangning /*
41680f1702c5SYu Xiangning * If we're connecting to an IPv4-mapped IPv6 address, we need to
4169bd670b35SErik Nordmark * make sure that the conn_ipversion is IPV4_VERSION. We
41700f1702c5SYu Xiangning * need to this before we call tcp_bindi() so that the port lookup
41710f1702c5SYu Xiangning * code will look for ports in the correct port space (IPv4 and
41720f1702c5SYu Xiangning * IPv6 have separate port spaces).
41730f1702c5SYu Xiangning */
4174bd670b35SErik Nordmark if (connp->conn_family == AF_INET6 &&
4175bd670b35SErik Nordmark connp->conn_ipversion == IPV6_VERSION &&
41760f1702c5SYu Xiangning IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4177bd670b35SErik Nordmark if (connp->conn_ipv6_v6only)
4178bd670b35SErik Nordmark return (EADDRNOTAVAIL);
41790f1702c5SYu Xiangning
4180bd670b35SErik Nordmark connp->conn_ipversion = IPV4_VERSION;
41810f1702c5SYu Xiangning }
41820f1702c5SYu Xiangning
41830f1702c5SYu Xiangning switch (tcp->tcp_state) {
41840f1702c5SYu Xiangning case TCPS_LISTEN:
41850f1702c5SYu Xiangning /*
41860f1702c5SYu Xiangning * Listening sockets are not allowed to issue connect().
41870f1702c5SYu Xiangning */
41880f1702c5SYu Xiangning if (IPCL_IS_NONSTR(connp))
41890f1702c5SYu Xiangning return (EOPNOTSUPP);
41900f1702c5SYu Xiangning /* FALLTHRU */
41910f1702c5SYu Xiangning case TCPS_IDLE:
41920f1702c5SYu Xiangning /*
41930f1702c5SYu Xiangning * We support quick connect, refer to comments in
41940f1702c5SYu Xiangning * tcp_connect_*()
41950f1702c5SYu Xiangning */
41960f1702c5SYu Xiangning /* FALLTHRU */
41970f1702c5SYu Xiangning case TCPS_BOUND:
4198bd670b35SErik Nordmark break;
4199bd670b35SErik Nordmark default:
4200bd670b35SErik Nordmark return (-TOUTSTATE);
42010f1702c5SYu Xiangning }
4202bd670b35SErik Nordmark
4203bd670b35SErik Nordmark /*
4204bd670b35SErik Nordmark * We update our cred/cpid based on the caller of connect
4205bd670b35SErik Nordmark */
4206bd670b35SErik Nordmark if (connp->conn_cred != cr) {
4207bd670b35SErik Nordmark crhold(cr);
4208bd670b35SErik Nordmark crfree(connp->conn_cred);
4209bd670b35SErik Nordmark connp->conn_cred = cr;
4210bd670b35SErik Nordmark }
4211bd670b35SErik Nordmark connp->conn_cpid = pid;
4212bd670b35SErik Nordmark
4213bd670b35SErik Nordmark /* Cache things in the ixa without any refhold */
4214be4c8f74SErik Nordmark ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
4215bd670b35SErik Nordmark ixa->ixa_cred = cr;
4216bd670b35SErik Nordmark ixa->ixa_cpid = pid;
4217bd670b35SErik Nordmark if (is_system_labeled()) {
4218bd670b35SErik Nordmark /* We need to restart with a label based on the cred */
4219bd670b35SErik Nordmark ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred);
4220bd670b35SErik Nordmark }
4221bd670b35SErik Nordmark
4222bd670b35SErik Nordmark if (connp->conn_family == AF_INET6) {
4223bd670b35SErik Nordmark if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4224bd670b35SErik Nordmark error = tcp_connect_ipv6(tcp, &sin6->sin6_addr,
4225bd670b35SErik Nordmark sin6->sin6_port, sin6->sin6_flowinfo,
4226bd670b35SErik Nordmark sin6->__sin6_src_id, sin6->sin6_scope_id);
4227bd670b35SErik Nordmark } else {
42280f1702c5SYu Xiangning /*
42290f1702c5SYu Xiangning * Destination adress is mapped IPv6 address.
42300f1702c5SYu Xiangning * Source bound address should be unspecified or
42310f1702c5SYu Xiangning * IPv6 mapped address as well.
42320f1702c5SYu Xiangning */
42330f1702c5SYu Xiangning if (!IN6_IS_ADDR_UNSPECIFIED(
4234bd670b35SErik Nordmark &connp->conn_bound_addr_v6) &&
4235bd670b35SErik Nordmark !IN6_IS_ADDR_V4MAPPED(&connp->conn_bound_addr_v6)) {
42360f1702c5SYu Xiangning return (EADDRNOTAVAIL);
42370f1702c5SYu Xiangning }
42380f1702c5SYu Xiangning dstaddrp = &V4_PART_OF_V6((sin6->sin6_addr));
42390f1702c5SYu Xiangning dstport = sin6->sin6_port;
42400f1702c5SYu Xiangning srcid = sin6->__sin6_src_id;
4241bd670b35SErik Nordmark error = tcp_connect_ipv4(tcp, dstaddrp, dstport,
4242bd670b35SErik Nordmark srcid);
4243bd670b35SErik Nordmark }
42440f1702c5SYu Xiangning } else {
42450f1702c5SYu Xiangning dstaddrp = &sin->sin_addr.s_addr;
42460f1702c5SYu Xiangning dstport = sin->sin_port;
42470f1702c5SYu Xiangning srcid = 0;
4248bd670b35SErik Nordmark error = tcp_connect_ipv4(tcp, dstaddrp, dstport, srcid);
42490f1702c5SYu Xiangning }
42500f1702c5SYu Xiangning
4251bd670b35SErik Nordmark if (error != 0)
4252bd670b35SErik Nordmark goto connect_failed;
4253bd670b35SErik Nordmark
4254bd670b35SErik Nordmark CL_INET_CONNECT(connp, B_TRUE, error);
4255bd670b35SErik Nordmark if (error != 0)
4256bd670b35SErik Nordmark goto connect_failed;
4257bd670b35SErik Nordmark
4258bd670b35SErik Nordmark /* connect succeeded */
4259721fffe3SKacheong Poon TCPS_BUMP_MIB(tcps, tcpActiveOpens);
4260bd670b35SErik Nordmark tcp->tcp_active_open = 1;
4261bd670b35SErik Nordmark
42620f1702c5SYu Xiangning /*
4263bd670b35SErik Nordmark * tcp_set_destination() does not adjust for TCP/IP header length.
42640f1702c5SYu Xiangning */
4265bd670b35SErik Nordmark mss = tcp->tcp_mss - connp->conn_ht_iphc_len;
4266bd670b35SErik Nordmark
4267bd670b35SErik Nordmark /*
4268bd670b35SErik Nordmark * Just make sure our rwnd is at least rcvbuf * MSS large, and round up
4269bd670b35SErik Nordmark * to the nearest MSS.
4270bd670b35SErik Nordmark *
4271bd670b35SErik Nordmark * We do the round up here because we need to get the interface MTU
4272bd670b35SErik Nordmark * first before we can do the round up.
4273bd670b35SErik Nordmark */
4274bd670b35SErik Nordmark tcp->tcp_rwnd = connp->conn_rcvbuf;
4275bd670b35SErik Nordmark tcp->tcp_rwnd = MAX(MSS_ROUNDUP(tcp->tcp_rwnd, mss),
4276bd670b35SErik Nordmark tcps->tcps_recv_hiwat_minmss * mss);
4277bd670b35SErik Nordmark connp->conn_rcvbuf = tcp->tcp_rwnd;
4278bd670b35SErik Nordmark tcp_set_ws_value(tcp);
4279bd670b35SErik Nordmark tcp->tcp_tcpha->tha_win = htons(tcp->tcp_rwnd >> tcp->tcp_rcv_ws);
4280bd670b35SErik Nordmark if (tcp->tcp_rcv_ws > 0 || tcps->tcps_wscale_always)
4281bd670b35SErik Nordmark tcp->tcp_snd_ws_ok = B_TRUE;
4282bd670b35SErik Nordmark
4283bd670b35SErik Nordmark /*
4284bd670b35SErik Nordmark * Set tcp_snd_ts_ok to true
4285bd670b35SErik Nordmark * so that tcp_xmit_mp will
4286bd670b35SErik Nordmark * include the timestamp
4287bd670b35SErik Nordmark * option in the SYN segment.
4288bd670b35SErik Nordmark */
4289bd670b35SErik Nordmark if (tcps->tcps_tstamp_always ||
4290bd670b35SErik Nordmark (tcp->tcp_rcv_ws && tcps->tcps_tstamp_if_wscale)) {
4291bd670b35SErik Nordmark tcp->tcp_snd_ts_ok = B_TRUE;
4292bd670b35SErik Nordmark }
4293bd670b35SErik Nordmark
4294bd670b35SErik Nordmark /*
429566cd0f60SKacheong Poon * Note that tcp_snd_sack_ok can be set in tcp_set_destination() if
429666cd0f60SKacheong Poon * the SACK metric is set. So here we just check the per stack SACK
429766cd0f60SKacheong Poon * permitted param.
4298bd670b35SErik Nordmark */
429966cd0f60SKacheong Poon if (tcps->tcps_sack_permitted == 2) {
430066cd0f60SKacheong Poon ASSERT(tcp->tcp_num_sack_blk == 0);
430166cd0f60SKacheong Poon ASSERT(tcp->tcp_notsack_list == NULL);
4302bd670b35SErik Nordmark tcp->tcp_snd_sack_ok = B_TRUE;
4303bd670b35SErik Nordmark }
4304bd670b35SErik Nordmark
4305bd670b35SErik Nordmark /*
4306bd670b35SErik Nordmark * Should we use ECN? Note that the current
4307bd670b35SErik Nordmark * default value (SunOS 5.9) of tcp_ecn_permitted
4308bd670b35SErik Nordmark * is 1. The reason for doing this is that there
4309bd670b35SErik Nordmark * are equipments out there that will drop ECN
4310bd670b35SErik Nordmark * enabled IP packets. Setting it to 1 avoids
4311bd670b35SErik Nordmark * compatibility problems.
4312bd670b35SErik Nordmark */
4313bd670b35SErik Nordmark if (tcps->tcps_ecn_permitted == 2)
4314bd670b35SErik Nordmark tcp->tcp_ecn_ok = B_TRUE;
4315bd670b35SErik Nordmark
43169cd928feSAlan Maguire /* Trace change from BOUND -> SYN_SENT here */
43179cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
43189cd928feSAlan Maguire connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
43199cd928feSAlan Maguire int32_t, TCPS_BOUND);
43209cd928feSAlan Maguire
4321bd670b35SErik Nordmark TCP_TIMER_RESTART(tcp, tcp->tcp_rto);
4322bd670b35SErik Nordmark syn_mp = tcp_xmit_mp(tcp, NULL, 0, NULL, NULL,
4323bd670b35SErik Nordmark tcp->tcp_iss, B_FALSE, NULL, B_FALSE);
4324bd670b35SErik Nordmark if (syn_mp != NULL) {
4325bd670b35SErik Nordmark /*
4326bd670b35SErik Nordmark * We must bump the generation before sending the syn
4327bd670b35SErik Nordmark * to ensure that we use the right generation in case
4328bd670b35SErik Nordmark * this thread issues a "connected" up call.
4329bd670b35SErik Nordmark */
4330bd670b35SErik Nordmark SOCK_CONNID_BUMP(tcp->tcp_connid);
43319cd928feSAlan Maguire /*
43329cd928feSAlan Maguire * DTrace sending the first SYN as a
43339cd928feSAlan Maguire * tcp:::connect-request event.
43349cd928feSAlan Maguire */
43359cd928feSAlan Maguire DTRACE_TCP5(connect__request, mblk_t *, NULL,
43369cd928feSAlan Maguire ip_xmit_attr_t *, connp->conn_ixa,
43379cd928feSAlan Maguire void_ip_t *, syn_mp->b_rptr, tcp_t *, tcp,
43389cd928feSAlan Maguire tcph_t *,
43399cd928feSAlan Maguire &syn_mp->b_rptr[connp->conn_ixa->ixa_ip_hdr_length]);
4340bd670b35SErik Nordmark tcp_send_data(tcp, syn_mp);
4341bd670b35SErik Nordmark }
4342bd670b35SErik Nordmark
4343bd670b35SErik Nordmark if (tcp->tcp_conn.tcp_opts_conn_req != NULL)
4344bd670b35SErik Nordmark tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req);
4345bd670b35SErik Nordmark return (0);
4346bd670b35SErik Nordmark
43470f1702c5SYu Xiangning connect_failed:
4348bd670b35SErik Nordmark connp->conn_faddr_v6 = ipv6_all_zeros;
4349bd670b35SErik Nordmark connp->conn_fport = 0;
4350bd670b35SErik Nordmark tcp->tcp_state = oldstate;
43510f1702c5SYu Xiangning if (tcp->tcp_conn.tcp_opts_conn_req != NULL)
43520f1702c5SYu Xiangning tcp_close_mpp(&tcp->tcp_conn.tcp_opts_conn_req);
43530f1702c5SYu Xiangning return (error);
43540f1702c5SYu Xiangning }
43550f1702c5SYu Xiangning
43560f1702c5SYu Xiangning int
tcp_do_listen(conn_t * connp,struct sockaddr * sa,socklen_t len,int backlog,cred_t * cr,boolean_t bind_to_req_port_only)4357eead73cfSRao Shoaib tcp_do_listen(conn_t *connp, struct sockaddr *sa, socklen_t len,
4358eead73cfSRao Shoaib int backlog, cred_t *cr, boolean_t bind_to_req_port_only)
43590f1702c5SYu Xiangning {
43600f1702c5SYu Xiangning tcp_t *tcp = connp->conn_tcp;
43610f1702c5SYu Xiangning int error = 0;
43620f1702c5SYu Xiangning tcp_stack_t *tcps = tcp->tcp_tcps;
43639cd928feSAlan Maguire int32_t oldstate;
43640f1702c5SYu Xiangning
4365de8c4a14SErik Nordmark /* All Solaris components should pass a cred for this operation. */
4366de8c4a14SErik Nordmark ASSERT(cr != NULL);
4367de8c4a14SErik Nordmark
43680f1702c5SYu Xiangning if (tcp->tcp_state >= TCPS_BOUND) {
43690f1702c5SYu Xiangning if ((tcp->tcp_state == TCPS_BOUND ||
4370a5adac4dSYu Xiangning tcp->tcp_state == TCPS_LISTEN) && backlog > 0) {
43710f1702c5SYu Xiangning /*
43720f1702c5SYu Xiangning * Handle listen() increasing backlog.
43730f1702c5SYu Xiangning * This is more "liberal" then what the TPI spec
43740f1702c5SYu Xiangning * requires but is needed to avoid a t_unbind
43750f1702c5SYu Xiangning * when handling listen() since the port number
43760f1702c5SYu Xiangning * might be "stolen" between the unbind and bind.
43770f1702c5SYu Xiangning */
43780f1702c5SYu Xiangning goto do_listen;
43790f1702c5SYu Xiangning }
4380bd670b35SErik Nordmark if (connp->conn_debug) {
43810f1702c5SYu Xiangning (void) strlog(TCP_MOD_ID, 0, 1, SL_ERROR|SL_TRACE,
4382a5adac4dSYu Xiangning "tcp_listen: bad state, %d", tcp->tcp_state);
43830f1702c5SYu Xiangning }
43840f1702c5SYu Xiangning return (-TOUTSTATE);
43850f1702c5SYu Xiangning } else {
43860f1702c5SYu Xiangning sin6_t addr;
4387eead73cfSRao Shoaib sin_t *sin;
4388eead73cfSRao Shoaib sin6_t *sin6;
4389eead73cfSRao Shoaib
43903ef75692SJohn Levon if (sa == NULL) {
4391eead73cfSRao Shoaib ASSERT(IPCL_IS_NONSTR(connp));
43920f1702c5SYu Xiangning /* Do an implicit bind: Request for a generic port. */
4393bd670b35SErik Nordmark if (connp->conn_family == AF_INET) {
43940f1702c5SYu Xiangning len = sizeof (sin_t);
43950f1702c5SYu Xiangning sin = (sin_t *)&addr;
43960f1702c5SYu Xiangning *sin = sin_null;
43970f1702c5SYu Xiangning sin->sin_family = AF_INET;
43980f1702c5SYu Xiangning } else {
4399bd670b35SErik Nordmark ASSERT(connp->conn_family == AF_INET6);
44000f1702c5SYu Xiangning len = sizeof (sin6_t);
44010f1702c5SYu Xiangning sin6 = (sin6_t *)&addr;
44020f1702c5SYu Xiangning *sin6 = sin6_null;
44030f1702c5SYu Xiangning sin6->sin6_family = AF_INET6;
44040f1702c5SYu Xiangning }
4405eead73cfSRao Shoaib sa = (struct sockaddr *)&addr;
4406eead73cfSRao Shoaib }
44070f1702c5SYu Xiangning
4408eead73cfSRao Shoaib error = tcp_bind_check(connp, sa, len, cr,
4409eead73cfSRao Shoaib bind_to_req_port_only);
44100f1702c5SYu Xiangning if (error)
44110f1702c5SYu Xiangning return (error);
44120f1702c5SYu Xiangning /* Fall through and do the fanout insertion */
44130f1702c5SYu Xiangning }
44140f1702c5SYu Xiangning
44150f1702c5SYu Xiangning do_listen:
44160f1702c5SYu Xiangning ASSERT(tcp->tcp_state == TCPS_BOUND || tcp->tcp_state == TCPS_LISTEN);
44170f1702c5SYu Xiangning tcp->tcp_conn_req_max = backlog;
44180f1702c5SYu Xiangning if (tcp->tcp_conn_req_max) {
44190f1702c5SYu Xiangning if (tcp->tcp_conn_req_max < tcps->tcps_conn_req_min)
44200f1702c5SYu Xiangning tcp->tcp_conn_req_max = tcps->tcps_conn_req_min;
44210f1702c5SYu Xiangning if (tcp->tcp_conn_req_max > tcps->tcps_conn_req_max_q)
44220f1702c5SYu Xiangning tcp->tcp_conn_req_max = tcps->tcps_conn_req_max_q;
44230f1702c5SYu Xiangning /*
44240f1702c5SYu Xiangning * If this is a listener, do not reset the eager list
44250f1702c5SYu Xiangning * and other stuffs. Note that we don't check if the
44260f1702c5SYu Xiangning * existing eager list meets the new tcp_conn_req_max
44270f1702c5SYu Xiangning * requirement.
44280f1702c5SYu Xiangning */
44290f1702c5SYu Xiangning if (tcp->tcp_state != TCPS_LISTEN) {
44300f1702c5SYu Xiangning tcp->tcp_state = TCPS_LISTEN;
44319cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
44329cd928feSAlan Maguire connp->conn_ixa, void, NULL, tcp_t *, tcp,
44339cd928feSAlan Maguire void, NULL, int32_t, TCPS_BOUND);
44340f1702c5SYu Xiangning /* Initialize the chain. Don't need the eager_lock */
44350f1702c5SYu Xiangning tcp->tcp_eager_next_q0 = tcp->tcp_eager_prev_q0 = tcp;
44360f1702c5SYu Xiangning tcp->tcp_eager_next_drop_q0 = tcp;
44370f1702c5SYu Xiangning tcp->tcp_eager_prev_drop_q0 = tcp;
44380f1702c5SYu Xiangning tcp->tcp_second_ctimer_threshold =
44390f1702c5SYu Xiangning tcps->tcps_ip_abort_linterval;
44400f1702c5SYu Xiangning }
44410f1702c5SYu Xiangning }
44420f1702c5SYu Xiangning
44430f1702c5SYu Xiangning /*
44440f1702c5SYu Xiangning * We need to make sure that the conn_recv is set to a non-null
44450f1702c5SYu Xiangning * value before we insert the conn into the classifier table.
44460f1702c5SYu Xiangning * This is to avoid a race with an incoming packet which does an
44470f1702c5SYu Xiangning * ipcl_classify().
4448bd670b35SErik Nordmark * We initially set it to tcp_input_listener_unbound to try to
4449bd670b35SErik Nordmark * pick a good squeue for the listener when the first SYN arrives.
4450bd670b35SErik Nordmark * tcp_input_listener_unbound sets it to tcp_input_listener on that
4451bd670b35SErik Nordmark * first SYN.
44520f1702c5SYu Xiangning */
4453bd670b35SErik Nordmark connp->conn_recv = tcp_input_listener_unbound;
4454bd670b35SErik Nordmark
4455bd670b35SErik Nordmark /* Insert the listener in the classifier table */
4456bd670b35SErik Nordmark error = ip_laddr_fanout_insert(connp);
4457bd670b35SErik Nordmark if (error != 0) {
4458bd670b35SErik Nordmark /* Undo the bind - release the port number */
44599cd928feSAlan Maguire oldstate = tcp->tcp_state;
4460bd670b35SErik Nordmark tcp->tcp_state = TCPS_IDLE;
44619cd928feSAlan Maguire DTRACE_TCP6(state__change, void, NULL, ip_xmit_attr_t *,
44629cd928feSAlan Maguire connp->conn_ixa, void, NULL, tcp_t *, tcp, void, NULL,
44639cd928feSAlan Maguire int32_t, oldstate);
4464bd670b35SErik Nordmark connp->conn_bound_addr_v6 = ipv6_all_zeros;
4465bd670b35SErik Nordmark
4466bd670b35SErik Nordmark connp->conn_laddr_v6 = ipv6_all_zeros;
4467bd670b35SErik Nordmark connp->conn_saddr_v6 = ipv6_all_zeros;
4468bd670b35SErik Nordmark connp->conn_ports = 0;
4469bd670b35SErik Nordmark
4470bd670b35SErik Nordmark if (connp->conn_anon_port) {
4471bd670b35SErik Nordmark zone_t *zone;
4472bd670b35SErik Nordmark
4473bd670b35SErik Nordmark zone = crgetzone(cr);
4474bd670b35SErik Nordmark connp->conn_anon_port = B_FALSE;
4475bd670b35SErik Nordmark (void) tsol_mlp_anon(zone, connp->conn_mlp_type,
4476bd670b35SErik Nordmark connp->conn_proto, connp->conn_lport, B_FALSE);
44770f1702c5SYu Xiangning }
4478bd670b35SErik Nordmark connp->conn_mlp_type = mlptSingle;
4479bd670b35SErik Nordmark
4480bd670b35SErik Nordmark tcp_bind_hash_remove(tcp);
4481bd670b35SErik Nordmark return (error);
448293fcb0b9SKacheong Poon } else {
448393fcb0b9SKacheong Poon /*
448493fcb0b9SKacheong Poon * If there is a connection limit, allocate and initialize
448593fcb0b9SKacheong Poon * the counter struct. Note that since listen can be called
448693fcb0b9SKacheong Poon * multiple times, the struct may have been allready allocated.
448793fcb0b9SKacheong Poon */
448893fcb0b9SKacheong Poon if (!list_is_empty(&tcps->tcps_listener_conf) &&
448993fcb0b9SKacheong Poon tcp->tcp_listen_cnt == NULL) {
449093fcb0b9SKacheong Poon tcp_listen_cnt_t *tlc;
449193fcb0b9SKacheong Poon uint32_t ratio;
449293fcb0b9SKacheong Poon
449393fcb0b9SKacheong Poon ratio = tcp_find_listener_conf(tcps,
449493fcb0b9SKacheong Poon ntohs(connp->conn_lport));
449593fcb0b9SKacheong Poon if (ratio != 0) {
449693fcb0b9SKacheong Poon uint32_t mem_ratio, tot_buf;
449793fcb0b9SKacheong Poon
449893fcb0b9SKacheong Poon tlc = kmem_alloc(sizeof (tcp_listen_cnt_t),
449993fcb0b9SKacheong Poon KM_SLEEP);
450093fcb0b9SKacheong Poon /*
450193fcb0b9SKacheong Poon * Calculate the connection limit based on
450293fcb0b9SKacheong Poon * the configured ratio and maxusers. Maxusers
450393fcb0b9SKacheong Poon * are calculated based on memory size,
450493fcb0b9SKacheong Poon * ~ 1 user per MB. Note that the conn_rcvbuf
450593fcb0b9SKacheong Poon * and conn_sndbuf may change after a
450693fcb0b9SKacheong Poon * connection is accepted. So what we have
450793fcb0b9SKacheong Poon * is only an approximation.
450893fcb0b9SKacheong Poon */
450993fcb0b9SKacheong Poon if ((tot_buf = connp->conn_rcvbuf +
451093fcb0b9SKacheong Poon connp->conn_sndbuf) < MB) {
451193fcb0b9SKacheong Poon mem_ratio = MB / tot_buf;
451293fcb0b9SKacheong Poon tlc->tlc_max = maxusers / ratio *
451393fcb0b9SKacheong Poon mem_ratio;
451493fcb0b9SKacheong Poon } else {
451593fcb0b9SKacheong Poon mem_ratio = tot_buf / MB;
451693fcb0b9SKacheong Poon tlc->tlc_max = maxusers / ratio /
451793fcb0b9SKacheong Poon mem_ratio;
451893fcb0b9SKacheong Poon }
451993fcb0b9SKacheong Poon /* At least we should allow two connections! */
452093fcb0b9SKacheong Poon if (tlc->tlc_max <= tcp_min_conn_listener)
452193fcb0b9SKacheong Poon tlc->tlc_max = tcp_min_conn_listener;
452293fcb0b9SKacheong Poon tlc->tlc_cnt = 1;
452393fcb0b9SKacheong Poon tlc->tlc_drop = 0;
452493fcb0b9SKacheong Poon tcp->tcp_listen_cnt = tlc;
452593fcb0b9SKacheong Poon }
452693fcb0b9SKacheong Poon }
4527bd670b35SErik Nordmark }
4528bd670b35SErik Nordmark return (error);
45290f1702c5SYu Xiangning }
4530