xref: /illumos-gate/usr/src/uts/common/io/tl.c (revision 1c8449e9)
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
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * 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  */
217c478bd9Sstevel@tonic-gate /*
2256f33205SJonathan Adams  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
25b2d8fb89SDan Kruchinin /*
26b2d8fb89SDan Kruchinin  * Copyright 2011 Nexenta Systems, Inc.  All rights reserved.
27bbaa8b60SDan Kruchinin  * Copyright (c) 2012 by Delphix. All rights reserved.
28b8ffbd31SJohn Levon  * Copyright 2020 Joyent, Inc.
29b2d8fb89SDan Kruchinin  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Multithreaded STREAMS Local Transport Provider.
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  * OVERVIEW
357c478bd9Sstevel@tonic-gate  * ========
367c478bd9Sstevel@tonic-gate  *
377c478bd9Sstevel@tonic-gate  * This driver provides TLI as well as socket semantics.  It provides
387c478bd9Sstevel@tonic-gate  * connectionless, connection oriented, and connection oriented with orderly
397c478bd9Sstevel@tonic-gate  * release transports for TLI and sockets. Each transport type has separate name
407c478bd9Sstevel@tonic-gate  * spaces (i.e. it is not possible to connect from a socket to a TLI endpoint) -
417c478bd9Sstevel@tonic-gate  * this removes any name space conflicts when binding to socket style transport
427c478bd9Sstevel@tonic-gate  * addresses.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * NOTE: There is one exception: Socket ticots and ticotsord transports share
457c478bd9Sstevel@tonic-gate  * the same namespace. In fact, sockets always use ticotsord type transport.
467c478bd9Sstevel@tonic-gate  *
477c478bd9Sstevel@tonic-gate  * The driver mode is specified during open() by the minor number used for
487c478bd9Sstevel@tonic-gate  * open.
497c478bd9Sstevel@tonic-gate  *
507c478bd9Sstevel@tonic-gate  *  The sockets in addition have the following semantic differences:
517c478bd9Sstevel@tonic-gate  *  No support for passing up credentials (TL_SET[U]CRED).
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  *	Options are passed through transparently on T_CONN_REQ to T_CONN_IND,
547c478bd9Sstevel@tonic-gate  *	from T_UNITDATA_REQ to T_UNIDATA_IND, and from T_OPTDATA_REQ to
557c478bd9Sstevel@tonic-gate  *	T_OPTDATA_IND.
567c478bd9Sstevel@tonic-gate  *
577c478bd9Sstevel@tonic-gate  *	The T_CONN_CON is generated when processing the T_CONN_REQ i.e. before
587c478bd9Sstevel@tonic-gate  *	a T_CONN_RES is received from the acceptor. This means that a socket
597c478bd9Sstevel@tonic-gate  *	connect will complete before the peer has called accept.
607c478bd9Sstevel@tonic-gate  *
617c478bd9Sstevel@tonic-gate  *
627c478bd9Sstevel@tonic-gate  * MULTITHREADING
637c478bd9Sstevel@tonic-gate  * ==============
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * The driver does not use STREAMS protection mechanisms. Instead it uses a
667c478bd9Sstevel@tonic-gate  * generic "serializer" abstraction. Most of the operations are executed behind
677c478bd9Sstevel@tonic-gate  * the serializer and are, essentially single-threaded. All functions executed
687c478bd9Sstevel@tonic-gate  * behind the same serializer are strictly serialized. So if one thread calls
697c478bd9Sstevel@tonic-gate  * serializer_enter(serializer, foo, mp1, arg1); and another thread calls
707c478bd9Sstevel@tonic-gate  * serializer_enter(serializer, bar, mp2, arg1); then (depending on which one
717c478bd9Sstevel@tonic-gate  * was called) the actual sequence will be foo(mp1, arg1); bar(mp1, arg2) or
727c478bd9Sstevel@tonic-gate  * bar(mp1, arg2); foo(mp1, arg1); But foo() and bar() will never run at the
737c478bd9Sstevel@tonic-gate  * same time.
747c478bd9Sstevel@tonic-gate  *
757c478bd9Sstevel@tonic-gate  * Connectionless transport use a single serializer per transport type (one for
767c478bd9Sstevel@tonic-gate  * TLI and one for sockets. Connection-oriented transports use finer-grained
777c478bd9Sstevel@tonic-gate  * serializers.
787c478bd9Sstevel@tonic-gate  *
797c478bd9Sstevel@tonic-gate  * All COTS-type endpoints start their life with private serializers. During
807c478bd9Sstevel@tonic-gate  * connection request processing the endpoint serializer is switched to the
817c478bd9Sstevel@tonic-gate  * listener's serializer and the rest of T_CONN_REQ processing is done on the
827c478bd9Sstevel@tonic-gate  * listener serializer. During T_CONN_RES processing the eager serializer is
837c478bd9Sstevel@tonic-gate  * switched from listener to acceptor serializer and after that point all
847c478bd9Sstevel@tonic-gate  * processing for eager and acceptor happens on this serializer. To avoid races
857c478bd9Sstevel@tonic-gate  * with endpoint closes while its serializer may be changing closes are blocked
867c478bd9Sstevel@tonic-gate  * while serializers are manipulated.
877c478bd9Sstevel@tonic-gate  *
887c478bd9Sstevel@tonic-gate  * References accounting
897c478bd9Sstevel@tonic-gate  * ---------------------
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * Endpoints are reference counted and freed when the last reference is
927c478bd9Sstevel@tonic-gate  * dropped. Functions within the serializer may access an endpoint state even
937c478bd9Sstevel@tonic-gate  * after an endpoint closed. The te_closing being set on the endpoint indicates
947c478bd9Sstevel@tonic-gate  * that the endpoint entered its close routine.
957c478bd9Sstevel@tonic-gate  *
967c478bd9Sstevel@tonic-gate  * One reference is held for each opened endpoint instance. The reference
977c478bd9Sstevel@tonic-gate  * counter is incremented when the endpoint is linked to another endpoint and
987c478bd9Sstevel@tonic-gate  * decremented when the link disappears. It is also incremented when the
997c478bd9Sstevel@tonic-gate  * endpoint is found by the hash table lookup. This increment is atomic with the
1007c478bd9Sstevel@tonic-gate  * lookup itself and happens while the hash table read lock is held.
1017c478bd9Sstevel@tonic-gate  *
1027c478bd9Sstevel@tonic-gate  * Close synchronization
1037c478bd9Sstevel@tonic-gate  * ---------------------
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * During close the endpoint as marked as closing using te_closing flag. It is
1067c478bd9Sstevel@tonic-gate  * usually enough to check for te_closing flag since all other state changes
1077c478bd9Sstevel@tonic-gate  * happen after this flag is set and the close entered serializer. Immediately
1087c478bd9Sstevel@tonic-gate  * after setting te_closing flag tl_close() enters serializer and waits until
1097c478bd9Sstevel@tonic-gate  * the callback finishes. This allows all functions called within serializer to
1107c478bd9Sstevel@tonic-gate  * simply check te_closing without any locks.
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  * Serializer management.
1137c478bd9Sstevel@tonic-gate  * ---------------------
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  * For COTS transports serializers are created when the endpoint is constructed
1167c478bd9Sstevel@tonic-gate  * and destroyed when the endpoint is destructed. CLTS transports use global
1177c478bd9Sstevel@tonic-gate  * serializers - one for sockets and one for TLI.
1187c478bd9Sstevel@tonic-gate  *
1197c478bd9Sstevel@tonic-gate  * COTS serializers have separate reference counts to deal with several
1207c478bd9Sstevel@tonic-gate  * endpoints sharing the same serializer. There is a subtle problem related to
1217c478bd9Sstevel@tonic-gate  * the serializer destruction. The serializer should never be destroyed by any
1227c478bd9Sstevel@tonic-gate  * function executed inside serializer. This means that close has to wait till
1237c478bd9Sstevel@tonic-gate  * all serializer activity for this endpoint is finished before it can drop the
1247c478bd9Sstevel@tonic-gate  * last reference on the endpoint (which may as well free the serializer).  This
1257c478bd9Sstevel@tonic-gate  * is only relevant for COTS transports which manage serializers
1267c478bd9Sstevel@tonic-gate  * dynamically. For CLTS transports close may complete without waiting for all
1277c478bd9Sstevel@tonic-gate  * serializer activity to finish since serializer is only destroyed at driver
1287c478bd9Sstevel@tonic-gate  * detach time.
1297c478bd9Sstevel@tonic-gate  *
1307c478bd9Sstevel@tonic-gate  * COTS endpoints keep track of the number of outstanding requests on the
1317c478bd9Sstevel@tonic-gate  * serializer for the endpoint. The code handling accept() avoids changing
1327c478bd9Sstevel@tonic-gate  * client serializer if it has any pending messages on the serializer and
1337c478bd9Sstevel@tonic-gate  * instead moves acceptor to listener's serializer.
1347c478bd9Sstevel@tonic-gate  *
1357c478bd9Sstevel@tonic-gate  *
1367c478bd9Sstevel@tonic-gate  * Use of hash tables
1377c478bd9Sstevel@tonic-gate  * ------------------
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * The driver uses modhash hash table implementation. Each transport uses two
1407c478bd9Sstevel@tonic-gate  * hash tables - one for finding endpoints by acceptor ID and another one for
1417c478bd9Sstevel@tonic-gate  * finding endpoints by address. For sockets TICOTS and TICOTSORD share the same
1427c478bd9Sstevel@tonic-gate  * pair of hash tables since sockets only use TICOTSORD.
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * All hash tables lookups increment a reference count for returned endpoints,
1457c478bd9Sstevel@tonic-gate  * so we may safely check the endpoint state even when the endpoint is removed
1467c478bd9Sstevel@tonic-gate  * from the hash by another thread immediately after it is found.
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  *
1497c478bd9Sstevel@tonic-gate  * CLOSE processing
1507c478bd9Sstevel@tonic-gate  * ================
1517c478bd9Sstevel@tonic-gate  *
1527c478bd9Sstevel@tonic-gate  * The driver enters serializer twice on close(). The close sequence is the
1537c478bd9Sstevel@tonic-gate  * following:
1547c478bd9Sstevel@tonic-gate  *
1557c478bd9Sstevel@tonic-gate  * 1) Wait until closing is safe (te_closewait becomes zero)
1567c478bd9Sstevel@tonic-gate  *	This step is needed to prevent close during serializer switches. In most
1577c478bd9Sstevel@tonic-gate  *	cases (close happening after connection establishment) te_closewait is
1587c478bd9Sstevel@tonic-gate  *	zero.
1597c478bd9Sstevel@tonic-gate  * 1) Set te_closing.
1607c478bd9Sstevel@tonic-gate  * 2) Call tl_close_ser() within serializer and wait for it to complete.
1617c478bd9Sstevel@tonic-gate  *
1627c478bd9Sstevel@tonic-gate  *      te_close_ser simply marks endpoint and wakes up waiting tl_close().
1637c478bd9Sstevel@tonic-gate  *	It also needs to clear write-side q_next pointers - this should be done
1647c478bd9Sstevel@tonic-gate  *	before qprocsoff().
1657c478bd9Sstevel@tonic-gate  *
1667c478bd9Sstevel@tonic-gate  *    This synchronous serializer entry during close is needed to ensure that
1677c478bd9Sstevel@tonic-gate  *    the queue is valid everywhere inside the serializer.
1687c478bd9Sstevel@tonic-gate  *
1697c478bd9Sstevel@tonic-gate  *    Note that in many cases close will execute tl_close_ser() synchronously,
1707c478bd9Sstevel@tonic-gate  *    so it will not wait at all.
1717c478bd9Sstevel@tonic-gate  *
1727c478bd9Sstevel@tonic-gate  * 3) Calls qprocsoff().
1737c478bd9Sstevel@tonic-gate  * 4) Calls tl_close_finish_ser() within the serializer and waits for it to
1747c478bd9Sstevel@tonic-gate  *	complete (for COTS transports). For CLTS transport there is no wait.
1757c478bd9Sstevel@tonic-gate  *
1767c478bd9Sstevel@tonic-gate  *	tl_close_finish_ser() Finishes the close process and wakes up waiting
1777c478bd9Sstevel@tonic-gate  *	close if there is any.
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  *    Note that in most cases close will enter te_close_ser_finish()
1807c478bd9Sstevel@tonic-gate  *    synchronously and will not wait at all.
1817c478bd9Sstevel@tonic-gate  *
1827c478bd9Sstevel@tonic-gate  *
1837c478bd9Sstevel@tonic-gate  * Flow Control
1847c478bd9Sstevel@tonic-gate  * ============
1857c478bd9Sstevel@tonic-gate  *
1867c478bd9Sstevel@tonic-gate  * The driver implements both read and write side service routines. No one calls
1877c478bd9Sstevel@tonic-gate  * putq() on the read queue. The read side service routine tl_rsrv() is called
1887c478bd9Sstevel@tonic-gate  * when the read side stream is back-enabled. It enters serializer synchronously
1897c478bd9Sstevel@tonic-gate  * (waits till serializer processing is complete). Within serializer it
1907c478bd9Sstevel@tonic-gate  * back-enables all endpoints blocked by the queue for connection-less
1917c478bd9Sstevel@tonic-gate  * transports and enables write side service processing for the peer for
1927c478bd9Sstevel@tonic-gate  * connection-oriented transports.
1937c478bd9Sstevel@tonic-gate  *
1947c478bd9Sstevel@tonic-gate  * Read and write side service routines use special mblk_sized space in the
1957c478bd9Sstevel@tonic-gate  * endpoint structure to enter perimeter.
1967c478bd9Sstevel@tonic-gate  *
1977c478bd9Sstevel@tonic-gate  * Write-side flow control
1987c478bd9Sstevel@tonic-gate  * -----------------------
1997c478bd9Sstevel@tonic-gate  *
2007c478bd9Sstevel@tonic-gate  * Write side flow control is a bit tricky. The driver needs to deal with two
2017c478bd9Sstevel@tonic-gate  * message queues - the explicit STREAMS message queue maintained by
2027c478bd9Sstevel@tonic-gate  * putq()/getq()/putbq() and the implicit queue within the serializer. These two
2037c478bd9Sstevel@tonic-gate  * queues should be synchronized to preserve message ordering and should
2047c478bd9Sstevel@tonic-gate  * maintain a single order determined by the order in which messages enter
2057c478bd9Sstevel@tonic-gate  * tl_wput(). In order to maintain the ordering between these two queues the
2067c478bd9Sstevel@tonic-gate  * STREAMS queue is only manipulated within the serializer, so the ordering is
2077c478bd9Sstevel@tonic-gate  * provided by the serializer.
2087c478bd9Sstevel@tonic-gate  *
2097c478bd9Sstevel@tonic-gate  * Functions called from the tl_wsrv() sometimes may call putbq(). To
2107c478bd9Sstevel@tonic-gate  * immediately stop any further processing of the STREAMS message queues the
2117c478bd9Sstevel@tonic-gate  * code calling putbq() also sets the te_nowsrv flag in the endpoint. The write
2127c478bd9Sstevel@tonic-gate  * side service processing stops when the flag is set.
2137c478bd9Sstevel@tonic-gate  *
2147c478bd9Sstevel@tonic-gate  * The tl_wsrv() function enters serializer synchronously and waits for it to
2157c478bd9Sstevel@tonic-gate  * complete. The serializer call-back tl_wsrv_ser() either drains all messages
2167c478bd9Sstevel@tonic-gate  * on the STREAMS queue or terminates when it notices the te_nowsrv flag
2177c478bd9Sstevel@tonic-gate  * set. Note that the maximum amount of messages processed by tl_wput_ser() is
2187c478bd9Sstevel@tonic-gate  * always bounded by the amount of messages on the STREAMS queue at the time
2197c478bd9Sstevel@tonic-gate  * tl_wsrv_ser() is entered. Any new messages may only appear on the STREAMS
2207c478bd9Sstevel@tonic-gate  * queue from another serialized entry which can't happen in parallel. This
2217c478bd9Sstevel@tonic-gate  * guarantees that tl_wput_ser() is complete in bounded time (there is no risk
2227c478bd9Sstevel@tonic-gate  * of it draining forever while writer places new messages on the STREAMS
2237c478bd9Sstevel@tonic-gate  * queue).
2247c478bd9Sstevel@tonic-gate  *
2257c478bd9Sstevel@tonic-gate  * Note that a closing endpoint never sets te_nowsrv and never calls putbq().
2267c478bd9Sstevel@tonic-gate  *
2277c478bd9Sstevel@tonic-gate  *
2287c478bd9Sstevel@tonic-gate  * Unix Domain Sockets
2297c478bd9Sstevel@tonic-gate  * ===================
2307c478bd9Sstevel@tonic-gate  *
2317c478bd9Sstevel@tonic-gate  * The driver knows the structure of Unix Domain sockets addresses and treats
2327c478bd9Sstevel@tonic-gate  * them differently from generic TLI addresses. For sockets implicit binds are
2337c478bd9Sstevel@tonic-gate  * requested by setting SOU_MAGIC_IMPLICIT in the soua_magic part of the address
2347c478bd9Sstevel@tonic-gate  * instead of using address length of zero. Explicit binds specify
2357c478bd9Sstevel@tonic-gate  * SOU_MAGIC_EXPLICIT as magic.
2367c478bd9Sstevel@tonic-gate  *
2377c478bd9Sstevel@tonic-gate  * For implicit binds we always use minor number as soua_vp part of the address
2387c478bd9Sstevel@tonic-gate  * and avoid any hash table lookups. This saves two hash tables lookups per
2397c478bd9Sstevel@tonic-gate  * anonymous bind.
2407c478bd9Sstevel@tonic-gate  *
2417c478bd9Sstevel@tonic-gate  * For explicit address we hash the vnode pointer instead of hashing the
2427c478bd9Sstevel@tonic-gate  * full-scale address+zone+length. Hashing by pointer is more efficient then
2437c478bd9Sstevel@tonic-gate  * hashing by the full address.
2447c478bd9Sstevel@tonic-gate  *
2457c478bd9Sstevel@tonic-gate  * For unix domain sockets the te_ap is always pointing to te_uxaddr part of the
2467c478bd9Sstevel@tonic-gate  * tep structure, so it should be never freed.
2477c478bd9Sstevel@tonic-gate  *
2487c478bd9Sstevel@tonic-gate  * Also for sockets the driver always uses minor number as acceptor id.
2497c478bd9Sstevel@tonic-gate  *
2507c478bd9Sstevel@tonic-gate  * TPI VIOLATIONS
2517c478bd9Sstevel@tonic-gate  * --------------
2527c478bd9Sstevel@tonic-gate  *
2537c478bd9Sstevel@tonic-gate  * This driver violates TPI in several respects for Unix Domain Sockets:
2547c478bd9Sstevel@tonic-gate  *
2557c478bd9Sstevel@tonic-gate  * 1) It treats O_T_BIND_REQ as T_BIND_REQ and refuses bind if an explicit bind
2567c478bd9Sstevel@tonic-gate  *	is requested and the endpoint is already in use. There is no point in
2577c478bd9Sstevel@tonic-gate  *	generating an unused address since this address will be rejected by
2587c478bd9Sstevel@tonic-gate  *	sockfs anyway. For implicit binds it always generates a new address
2597c478bd9Sstevel@tonic-gate  *	(sets soua_vp to its minor number).
2607c478bd9Sstevel@tonic-gate  *
2617c478bd9Sstevel@tonic-gate  * 2) It always uses minor number as acceptor ID and never uses queue
2627c478bd9Sstevel@tonic-gate  *	pointer. It is ok since sockets get acceptor ID from T_CAPABILITY_REQ
2637c478bd9Sstevel@tonic-gate  *	message and they do not use the queue pointer.
2647c478bd9Sstevel@tonic-gate  *
2657c478bd9Sstevel@tonic-gate  * 3) For Listener sockets the usual sequence is to issue bind() zero backlog
2667c478bd9Sstevel@tonic-gate  *	followed by listen(). The listen() should be issued with non-zero
2677c478bd9Sstevel@tonic-gate  *	backlog, so sotpi_listen() issues unbind request followed by bind
2687c478bd9Sstevel@tonic-gate  *	request to the same address but with a non-zero qlen value. Both
2697c478bd9Sstevel@tonic-gate  *	tl_bind() and tl_unbind() require write lock on the hash table to
2707c478bd9Sstevel@tonic-gate  *	insert/remove the address. The driver does not remove the address from
2717c478bd9Sstevel@tonic-gate  *	the hash for endpoints that are bound to the explicit address and have
2727c478bd9Sstevel@tonic-gate  *	backlog of zero. During T_BIND_REQ processing if the address requested
2737c478bd9Sstevel@tonic-gate  *	is equal to the address the endpoint already has it updates the backlog
2747c478bd9Sstevel@tonic-gate  *	without reinserting the address in the hash table. This optimization
2757c478bd9Sstevel@tonic-gate  *	avoids two hash table updates for each listener created. It always
2767c478bd9Sstevel@tonic-gate  *	avoids the problem of a "stolen" address when another listener may use
2777c478bd9Sstevel@tonic-gate  *	the same address between the unbind and bind and suddenly listen() fails
2787c478bd9Sstevel@tonic-gate  *	because address is in use even though the bind() succeeded.
2797c478bd9Sstevel@tonic-gate  *
2807c478bd9Sstevel@tonic-gate  *
2817c478bd9Sstevel@tonic-gate  * CONNECTIONLESS TRANSPORTS
2827c478bd9Sstevel@tonic-gate  * =========================
2837c478bd9Sstevel@tonic-gate  *
2847c478bd9Sstevel@tonic-gate  * Connectionless transports all share the same serializer (one for TLI and one
2857c478bd9Sstevel@tonic-gate  * for Sockets). Functions executing behind serializer can check or modify state
2867c478bd9Sstevel@tonic-gate  * of any endpoint.
2877c478bd9Sstevel@tonic-gate  *
2887c478bd9Sstevel@tonic-gate  * When endpoint X talks to another endpoint Y it caches the pointer to Y in the
2897c478bd9Sstevel@tonic-gate  * te_lastep field. The next time X talks to some address A it checks whether A
2907c478bd9Sstevel@tonic-gate  * is the same as Y's address and if it is there is no need to lookup Y. If the
2917c478bd9Sstevel@tonic-gate  * address is different or the state of Y is not appropriate (e.g. closed or not
2927c478bd9Sstevel@tonic-gate  * idle) X does a lookup using tl_find_peer() and caches the new address.
2937c478bd9Sstevel@tonic-gate  * NOTE: tl_find_peer() never returns closing endpoint and it places a refhold
2947c478bd9Sstevel@tonic-gate  * on the endpoint found.
2957c478bd9Sstevel@tonic-gate  *
2967c478bd9Sstevel@tonic-gate  * During close of endpoint Y it doesn't try to remove itself from other
2977c478bd9Sstevel@tonic-gate  * endpoints caches. They will detect that Y is gone and will search the peer
2987c478bd9Sstevel@tonic-gate  * endpoint again.
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * Flow Control Handling.
3017c478bd9Sstevel@tonic-gate  * ----------------------
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  * Each connectionless endpoint keeps a list of endpoints which are
3047c478bd9Sstevel@tonic-gate  * flow-controlled by its queue. It also keeps a pointer to the queue which
3057c478bd9Sstevel@tonic-gate  * flow-controls itself.  Whenever flow control releases for endpoint X it
3067c478bd9Sstevel@tonic-gate  * enables all queues from the list. During close it also back-enables everyone
3077c478bd9Sstevel@tonic-gate  * in the list. If X is flow-controlled when it is closing it removes it from
3087c478bd9Sstevel@tonic-gate  * the peers list.
3097c478bd9Sstevel@tonic-gate  *
3107c478bd9Sstevel@tonic-gate  * DATA STRUCTURES
3117c478bd9Sstevel@tonic-gate  * ===============
3127c478bd9Sstevel@tonic-gate  *
3137c478bd9Sstevel@tonic-gate  * Each endpoint is represented by the tl_endpt_t structure which keeps all the
3147c478bd9Sstevel@tonic-gate  * endpoint state. For connection-oriented transports it has a keeps a list
3157c478bd9Sstevel@tonic-gate  * of pending connections (tl_icon_t). For connectionless transports it keeps a
3167c478bd9Sstevel@tonic-gate  * list of endpoints flow controlled by this one.
3177c478bd9Sstevel@tonic-gate  *
3187c478bd9Sstevel@tonic-gate  * Each transport type is represented by a per-transport data structure
3197c478bd9Sstevel@tonic-gate  * tl_transport_state_t. It contains a pointer to an acceptor ID hash and the
3207c478bd9Sstevel@tonic-gate  * endpoint address hash tables for each transport. It also contains pointer to
3217c478bd9Sstevel@tonic-gate  * transport serializer for connectionless transports.
3227c478bd9Sstevel@tonic-gate  *
3237c478bd9Sstevel@tonic-gate  * Each endpoint keeps a link to its transport structure, so the code can find
3247c478bd9Sstevel@tonic-gate  * all per-transport information quickly.
3257c478bd9Sstevel@tonic-gate  */
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate #include	<sys/types.h>
3287c478bd9Sstevel@tonic-gate #include	<sys/inttypes.h>
3297c478bd9Sstevel@tonic-gate #include	<sys/stream.h>
3307c478bd9Sstevel@tonic-gate #include	<sys/stropts.h>
3317c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION 2
3327c478bd9Sstevel@tonic-gate #include	<sys/tihdr.h>
3337c478bd9Sstevel@tonic-gate #include	<sys/strlog.h>
3347c478bd9Sstevel@tonic-gate #include	<sys/debug.h>
3357c478bd9Sstevel@tonic-gate #include	<sys/cred.h>
3367c478bd9Sstevel@tonic-gate #include	<sys/errno.h>
3377c478bd9Sstevel@tonic-gate #include	<sys/kmem.h>
3387c478bd9Sstevel@tonic-gate #include	<sys/id_space.h>
3397c478bd9Sstevel@tonic-gate #include	<sys/modhash.h>
3407c478bd9Sstevel@tonic-gate #include	<sys/mkdev.h>
3417c478bd9Sstevel@tonic-gate #include	<sys/tl.h>
3427c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
3437c478bd9Sstevel@tonic-gate #include	<sys/conf.h>
3447c478bd9Sstevel@tonic-gate #include	<sys/modctl.h>
3457c478bd9Sstevel@tonic-gate #include	<sys/strsun.h>
3467c478bd9Sstevel@tonic-gate #include	<sys/socket.h>
3477c478bd9Sstevel@tonic-gate #include	<sys/socketvar.h>
3487c478bd9Sstevel@tonic-gate #include	<sys/sysmacros.h>
3497c478bd9Sstevel@tonic-gate #include	<sys/xti_xtiopt.h>
3507c478bd9Sstevel@tonic-gate #include	<sys/ddi.h>
3517c478bd9Sstevel@tonic-gate #include	<sys/sunddi.h>
3527c478bd9Sstevel@tonic-gate #include	<sys/zone.h>
3537c478bd9Sstevel@tonic-gate #include	<inet/common.h>	/* typedef int (*pfi_t)() for inet/optcom.h */
3547c478bd9Sstevel@tonic-gate #include	<inet/optcom.h>
3557c478bd9Sstevel@tonic-gate #include	<sys/strsubr.h>
3567c478bd9Sstevel@tonic-gate #include	<sys/ucred.h>
3577c478bd9Sstevel@tonic-gate #include	<sys/suntpi.h>
3587c478bd9Sstevel@tonic-gate #include	<sys/list.h>
3597c478bd9Sstevel@tonic-gate #include	<sys/serializer.h>
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate /*
3627c478bd9Sstevel@tonic-gate  * TBD List
3637c478bd9Sstevel@tonic-gate  * 14 Eliminate state changes through table
3647c478bd9Sstevel@tonic-gate  * 16. AF_UNIX socket options
3657c478bd9Sstevel@tonic-gate  * 17. connect() for ticlts
3667c478bd9Sstevel@tonic-gate  * 18. support for "netstat" to show AF_UNIX plus TLI local
3677c478bd9Sstevel@tonic-gate  *	transport connections
3687c478bd9Sstevel@tonic-gate  * 21. sanity check to flushing on sending M_ERROR
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate /*
3727c478bd9Sstevel@tonic-gate  * CONSTANT DECLARATIONS
3737c478bd9Sstevel@tonic-gate  * --------------------
3747c478bd9Sstevel@tonic-gate  */
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate /*
3777c478bd9Sstevel@tonic-gate  * Local declarations
3787c478bd9Sstevel@tonic-gate  */
3797c478bd9Sstevel@tonic-gate #define	BADSEQNUM	(-1)	/* initial seq number used by T_DISCON_IND */
3807c478bd9Sstevel@tonic-gate #define	TL_BUFWAIT	(10000)	/* usecs to wait for allocb buffer timeout */
3817c478bd9Sstevel@tonic-gate #define	TL_TIDUSZ (64*1024)	/* tidu size when "strmsgz" is unlimited (0) */
3827c478bd9Sstevel@tonic-gate /*
3837c478bd9Sstevel@tonic-gate  * Hash tables size.
3847c478bd9Sstevel@tonic-gate  */
3857c478bd9Sstevel@tonic-gate #define	TL_HASH_SIZE 311
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate /*
3887c478bd9Sstevel@tonic-gate  * Definitions for module_info
3897c478bd9Sstevel@tonic-gate  */
3907c478bd9Sstevel@tonic-gate #define		TL_ID		(104)		/* module ID number */
3917c478bd9Sstevel@tonic-gate #define		TL_NAME		"tl"		/* module name */
3927c478bd9Sstevel@tonic-gate #define		TL_MINPSZ	(0)		/* min packet size */
3939173eaebSToomas Soome #define		TL_MAXPSZ	INFPSZ		/* max packet size ZZZ */
3947c478bd9Sstevel@tonic-gate #define		TL_HIWAT	(16*1024)	/* hi water mark */
3957c478bd9Sstevel@tonic-gate #define		TL_LOWAT	(256)		/* lo water mark */
3967c478bd9Sstevel@tonic-gate /*
3977c478bd9Sstevel@tonic-gate  * Definition of minor numbers/modes for new transport provider modes.
3987c478bd9Sstevel@tonic-gate  * We view the socket use as a separate mode to get a separate name space.
3997c478bd9Sstevel@tonic-gate  */
4007c478bd9Sstevel@tonic-gate #define		TL_TICOTS	0	/* connection oriented transport */
4019173eaebSToomas Soome #define		TL_TICOTSORD	1	/* COTS w/ orderly release */
4029173eaebSToomas Soome #define		TL_TICLTS	2	/* connectionless transport */
4037c478bd9Sstevel@tonic-gate #define		TL_UNUSED	3
4047c478bd9Sstevel@tonic-gate #define		TL_SOCKET	4	/* Socket */
405cb492d90SToomas Soome #define		TL_SOCK_COTS	(TL_SOCKET | TL_TICOTS)
406cb492d90SToomas Soome #define		TL_SOCK_COTSORD	(TL_SOCKET | TL_TICOTSORD)
407cb492d90SToomas Soome #define		TL_SOCK_CLTS	(TL_SOCKET | TL_TICLTS)
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate #define		TL_MINOR_MASK	0x7
4107c478bd9Sstevel@tonic-gate #define		TL_MINOR_START	(TL_TICLTS + 1)
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * LOCAL MACROS
4147c478bd9Sstevel@tonic-gate  */
4157c478bd9Sstevel@tonic-gate #define	T_ALIGN(p)	P2ROUNDUP((p), sizeof (t_scalar_t))
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate /*
4187c478bd9Sstevel@tonic-gate  * STREAMS DRIVER ENTRY POINTS PROTOTYPES
4197c478bd9Sstevel@tonic-gate  */
4207c478bd9Sstevel@tonic-gate static int tl_open(queue_t *, dev_t *, int, int, cred_t *);
4217c478bd9Sstevel@tonic-gate static int tl_close(queue_t *, int, cred_t *);
422cb492d90SToomas Soome static int tl_wput(queue_t *, mblk_t *);
423cb492d90SToomas Soome static int tl_wsrv(queue_t *);
424cb492d90SToomas Soome static int tl_rsrv(queue_t *);
4257c478bd9Sstevel@tonic-gate 
4267c478bd9Sstevel@tonic-gate static int tl_attach(dev_info_t *, ddi_attach_cmd_t);
4277c478bd9Sstevel@tonic-gate static int tl_detach(dev_info_t *, ddi_detach_cmd_t);
4287c478bd9Sstevel@tonic-gate static int tl_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate  * GLOBAL DATA STRUCTURES AND VARIABLES
4337c478bd9Sstevel@tonic-gate  * -----------------------------------
4347c478bd9Sstevel@tonic-gate  */
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * Table representing database of all options managed by T_SVR4_OPTMGMT_REQ
4387c478bd9Sstevel@tonic-gate  * For now, we only manage the SO_RECVUCRED option but we also have
4397c478bd9Sstevel@tonic-gate  * harmless dummy options to make things work with some common code we access.
4407c478bd9Sstevel@tonic-gate  */
4417c478bd9Sstevel@tonic-gate opdes_t	tl_opt_arr[] = {
4427c478bd9Sstevel@tonic-gate 	/* The SO_TYPE is needed for the hack below */
4437c478bd9Sstevel@tonic-gate 	{
4447c478bd9Sstevel@tonic-gate 		SO_TYPE,
4457c478bd9Sstevel@tonic-gate 		SOL_SOCKET,
4467c478bd9Sstevel@tonic-gate 		OA_R,
4477c478bd9Sstevel@tonic-gate 		OA_R,
4487c478bd9Sstevel@tonic-gate 		OP_NP,
449bd670b35SErik Nordmark 		0,
4507c478bd9Sstevel@tonic-gate 		sizeof (t_scalar_t),
4517c478bd9Sstevel@tonic-gate 		0
4527c478bd9Sstevel@tonic-gate 	},
4537c478bd9Sstevel@tonic-gate 	{
4547c478bd9Sstevel@tonic-gate 		SO_RECVUCRED,
4557c478bd9Sstevel@tonic-gate 		SOL_SOCKET,
4567c478bd9Sstevel@tonic-gate 		OA_RW,
4577c478bd9Sstevel@tonic-gate 		OA_RW,
4587c478bd9Sstevel@tonic-gate 		OP_NP,
459bd670b35SErik Nordmark 		0,
4607c478bd9Sstevel@tonic-gate 		sizeof (int),
4617c478bd9Sstevel@tonic-gate 		0
4627c478bd9Sstevel@tonic-gate 	}
4637c478bd9Sstevel@tonic-gate };
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate /*
4667c478bd9Sstevel@tonic-gate  * Table of all supported levels
4677c478bd9Sstevel@tonic-gate  * Note: Some levels (e.g. XTI_GENERIC) may be valid but may not have
4687c478bd9Sstevel@tonic-gate  * any supported options so we need this info separately.
4697c478bd9Sstevel@tonic-gate  *
4707c478bd9Sstevel@tonic-gate  * This is needed only for topmost tpi providers.
4717c478bd9Sstevel@tonic-gate  */
4727c478bd9Sstevel@tonic-gate optlevel_t	tl_valid_levels_arr[] = {
4737c478bd9Sstevel@tonic-gate 	XTI_GENERIC,
4747c478bd9Sstevel@tonic-gate 	SOL_SOCKET,
4757c478bd9Sstevel@tonic-gate 	TL_PROT_LEVEL
4767c478bd9Sstevel@tonic-gate };
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate #define	TL_VALID_LEVELS_CNT	A_CNT(tl_valid_levels_arr)
4797c478bd9Sstevel@tonic-gate /*
4807c478bd9Sstevel@tonic-gate  * Current upper bound on the amount of space needed to return all options.
4817c478bd9Sstevel@tonic-gate  * Additional options with data size of sizeof(long) are handled automatically.
4827c478bd9Sstevel@tonic-gate  * Others need hand job.
4837c478bd9Sstevel@tonic-gate  */
4847c478bd9Sstevel@tonic-gate #define	TL_MAX_OPT_BUF_LEN						\
4857c478bd9Sstevel@tonic-gate 		((A_CNT(tl_opt_arr) << 2) +				\
4867c478bd9Sstevel@tonic-gate 		(A_CNT(tl_opt_arr) * sizeof (struct opthdr)) +		\
4877c478bd9Sstevel@tonic-gate 		+ 64 + sizeof (struct T_optmgmt_ack))
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate #define	TL_OPT_ARR_CNT	A_CNT(tl_opt_arr)
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate /*
4927c478bd9Sstevel@tonic-gate  *	transport addr structure
4937c478bd9Sstevel@tonic-gate  */
4947c478bd9Sstevel@tonic-gate typedef struct tl_addr {
4957c478bd9Sstevel@tonic-gate 	zoneid_t	ta_zoneid;		/* Zone scope of address */
4967c478bd9Sstevel@tonic-gate 	t_scalar_t	ta_alen;		/* length of abuf */
4977c478bd9Sstevel@tonic-gate 	void		*ta_abuf;		/* the addr itself */
4987c478bd9Sstevel@tonic-gate } tl_addr_t;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * Refcounted version of serializer.
5027c478bd9Sstevel@tonic-gate  */
5037c478bd9Sstevel@tonic-gate typedef struct tl_serializer {
5047c478bd9Sstevel@tonic-gate 	uint_t		ts_refcnt;
5057c478bd9Sstevel@tonic-gate 	serializer_t	*ts_serializer;
5067c478bd9Sstevel@tonic-gate } tl_serializer_t;
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate  * Each transport type has a separate state.
5107c478bd9Sstevel@tonic-gate  * Per-transport state.
5117c478bd9Sstevel@tonic-gate  */
5127c478bd9Sstevel@tonic-gate typedef struct tl_transport_state {
5137c478bd9Sstevel@tonic-gate 	char		*tr_name;
5147c478bd9Sstevel@tonic-gate 	minor_t		tr_minor;
5157c478bd9Sstevel@tonic-gate 	uint32_t	tr_defaddr;
5167c478bd9Sstevel@tonic-gate 	mod_hash_t	*tr_ai_hash;
5177c478bd9Sstevel@tonic-gate 	mod_hash_t	*tr_addr_hash;
5187c478bd9Sstevel@tonic-gate 	tl_serializer_t	*tr_serializer;
5197c478bd9Sstevel@tonic-gate } tl_transport_state_t;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate #define	TL_DFADDR 0x1000
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate static tl_transport_state_t tl_transports[] = {
5247c478bd9Sstevel@tonic-gate 	{ "ticots", TL_TICOTS, TL_DFADDR, NULL, NULL, NULL },
5257c478bd9Sstevel@tonic-gate 	{ "ticotsord", TL_TICOTSORD, TL_DFADDR, NULL, NULL, NULL },
5267c478bd9Sstevel@tonic-gate 	{ "ticlts", TL_TICLTS, TL_DFADDR, NULL, NULL, NULL },
5277c478bd9Sstevel@tonic-gate 	{ "undefined", TL_UNUSED, TL_DFADDR, NULL, NULL, NULL },
5287c478bd9Sstevel@tonic-gate 	{ "sticots", TL_SOCK_COTS, TL_DFADDR, NULL, NULL, NULL },
5297c478bd9Sstevel@tonic-gate 	{ "sticotsord", TL_SOCK_COTSORD, TL_DFADDR, NULL, NULL },
5307c478bd9Sstevel@tonic-gate 	{ "sticlts", TL_SOCK_CLTS, TL_DFADDR, NULL, NULL, NULL }
5317c478bd9Sstevel@tonic-gate };
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate #define	TL_MAXTRANSPORT A_CNT(tl_transports)
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate struct tl_endpt;
5367c478bd9Sstevel@tonic-gate typedef struct tl_endpt tl_endpt_t;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate typedef void (tlproc_t)(mblk_t *, tl_endpt_t *);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate /*
5417c478bd9Sstevel@tonic-gate  * Data structure used to represent pending connects.
5427c478bd9Sstevel@tonic-gate  * Records enough information so that the connecting peer can close
5437c478bd9Sstevel@tonic-gate  * before the connection gets accepted.
5447c478bd9Sstevel@tonic-gate  */
5457c478bd9Sstevel@tonic-gate typedef struct tl_icon {
5467c478bd9Sstevel@tonic-gate 	list_node_t	ti_node;
5477c478bd9Sstevel@tonic-gate 	struct tl_endpt *ti_tep;	/* NULL if peer has already closed */
5487c478bd9Sstevel@tonic-gate 	mblk_t		*ti_mp;		/* b_next list of data + ordrel_ind */
5497c478bd9Sstevel@tonic-gate 	t_scalar_t	ti_seqno;	/* Sequence number */
5507c478bd9Sstevel@tonic-gate } tl_icon_t;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate typedef struct so_ux_addr soux_addr_t;
5537c478bd9Sstevel@tonic-gate #define	TL_SOUX_ADDRLEN sizeof (soux_addr_t)
5547c478bd9Sstevel@tonic-gate 
555d87b1facSakolb /*
556d87b1facSakolb  * Maximum number of unaccepted connection indications allowed per listener.
557d87b1facSakolb  */
558d87b1facSakolb #define	TL_MAXQLEN	4096
559d87b1facSakolb int tl_maxqlen = TL_MAXQLEN;
560d87b1facSakolb 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  *	transport endpoint structure
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate struct tl_endpt {
5657c478bd9Sstevel@tonic-gate 	queue_t		*te_rq;		/* stream read queue */
5667c478bd9Sstevel@tonic-gate 	queue_t		*te_wq;		/* stream write queue */
5677c478bd9Sstevel@tonic-gate 	uint32_t	te_refcnt;
5689173eaebSToomas Soome 	int32_t		te_state;	/* TPI state of endpoint */
5697c478bd9Sstevel@tonic-gate 	minor_t		te_minor;	/* minor number */
5707c478bd9Sstevel@tonic-gate #define	te_seqno	te_minor
5717c478bd9Sstevel@tonic-gate 	uint_t		te_flag;	/* flag field */
5727c478bd9Sstevel@tonic-gate 	boolean_t	te_nowsrv;
5737c478bd9Sstevel@tonic-gate 	tl_serializer_t	*te_ser;	/* Serializer to use */
5747c478bd9Sstevel@tonic-gate #define	te_serializer	te_ser->ts_serializer
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	soux_addr_t	te_uxaddr;	/* Socket address */
5777c478bd9Sstevel@tonic-gate #define	te_magic	te_uxaddr.soua_magic
5787c478bd9Sstevel@tonic-gate #define	te_vp		te_uxaddr.soua_vp
5797c478bd9Sstevel@tonic-gate 	tl_addr_t	te_ap;		/* addr bound to this endpt */
5807c478bd9Sstevel@tonic-gate #define	te_zoneid te_ap.ta_zoneid
5817c478bd9Sstevel@tonic-gate #define	te_alen	te_ap.ta_alen
5827c478bd9Sstevel@tonic-gate #define	te_abuf	te_ap.ta_abuf
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate 	tl_transport_state_t *te_transport;
5857c478bd9Sstevel@tonic-gate #define	te_addrhash	te_transport->tr_addr_hash
5867c478bd9Sstevel@tonic-gate #define	te_aihash	te_transport->tr_ai_hash
5877c478bd9Sstevel@tonic-gate #define	te_defaddr	te_transport->tr_defaddr
5887c478bd9Sstevel@tonic-gate 	cred_t		*te_credp;	/* endpoint user credentials */
5897c478bd9Sstevel@tonic-gate 	mod_hash_hndl_t	te_hash_hndl;	/* Handle for address hash */
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/*
5927c478bd9Sstevel@tonic-gate 	 * State specific for connection-oriented and connectionless transports.
5937c478bd9Sstevel@tonic-gate 	 */
5947c478bd9Sstevel@tonic-gate 	union {
5957c478bd9Sstevel@tonic-gate 		/* Connection-oriented state. */
5967c478bd9Sstevel@tonic-gate 		struct {
5977c478bd9Sstevel@tonic-gate 			t_uscalar_t _te_nicon;	/* count of conn requests */
5987c478bd9Sstevel@tonic-gate 			t_uscalar_t _te_qlen;	/* max conn requests */
5997c478bd9Sstevel@tonic-gate 			tl_endpt_t  *_te_oconp;	/* conn request pending */
6007c478bd9Sstevel@tonic-gate 			tl_endpt_t  *_te_conp;	/* connected endpt */
6017c478bd9Sstevel@tonic-gate #ifndef _ILP32
6027c478bd9Sstevel@tonic-gate 			void	    *_te_pad;
6037c478bd9Sstevel@tonic-gate #endif
6047c478bd9Sstevel@tonic-gate 			list_t	_te_iconp;	/* list of conn ind. pending */
6057c478bd9Sstevel@tonic-gate 		} _te_cots_state;
6067c478bd9Sstevel@tonic-gate 		/* Connection-less state. */
6077c478bd9Sstevel@tonic-gate 		struct {
6087c478bd9Sstevel@tonic-gate 			tl_endpt_t *_te_lastep;	/* last dest. endpoint */
6097c478bd9Sstevel@tonic-gate 			tl_endpt_t *_te_flowq;	/* flow controlled on whom */
6107c478bd9Sstevel@tonic-gate 			list_node_t _te_flows;	/* lists of connections */
6117c478bd9Sstevel@tonic-gate 			list_t  _te_flowlist;	/* Who flowcontrols on me */
6127c478bd9Sstevel@tonic-gate 		} _te_clts_state;
6137c478bd9Sstevel@tonic-gate 	} _te_transport_state;
6147c478bd9Sstevel@tonic-gate #define	te_nicon	_te_transport_state._te_cots_state._te_nicon
6157c478bd9Sstevel@tonic-gate #define	te_qlen		_te_transport_state._te_cots_state._te_qlen
6167c478bd9Sstevel@tonic-gate #define	te_oconp	_te_transport_state._te_cots_state._te_oconp
6177c478bd9Sstevel@tonic-gate #define	te_conp		_te_transport_state._te_cots_state._te_conp
6187c478bd9Sstevel@tonic-gate #define	te_iconp	_te_transport_state._te_cots_state._te_iconp
6197c478bd9Sstevel@tonic-gate #define	te_lastep	_te_transport_state._te_clts_state._te_lastep
6207c478bd9Sstevel@tonic-gate #define	te_flowq	_te_transport_state._te_clts_state._te_flowq
6217c478bd9Sstevel@tonic-gate #define	te_flowlist	_te_transport_state._te_clts_state._te_flowlist
6227c478bd9Sstevel@tonic-gate #define	te_flows	_te_transport_state._te_clts_state._te_flows
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	bufcall_id_t	te_bufcid;	/* outstanding bufcall id */
6257c478bd9Sstevel@tonic-gate 	timeout_id_t	te_timoutid;	/* outstanding timeout id */
6267c478bd9Sstevel@tonic-gate 	pid_t		te_cpid;	/* cached pid of endpoint */
6277c478bd9Sstevel@tonic-gate 	t_uscalar_t	te_acceptor_id;	/* acceptor id for T_CONN_RES */
6287c478bd9Sstevel@tonic-gate 	/*
6297c478bd9Sstevel@tonic-gate 	 * Pieces of the endpoint state needed for closing.
6307c478bd9Sstevel@tonic-gate 	 */
6317c478bd9Sstevel@tonic-gate 	kmutex_t	te_closelock;
6327c478bd9Sstevel@tonic-gate 	kcondvar_t	te_closecv;
6337c478bd9Sstevel@tonic-gate 	uint8_t		te_closing;	/* The endpoint started closing */
6347c478bd9Sstevel@tonic-gate 	uint8_t		te_closewait;	/* Wait in close until zero */
6357c478bd9Sstevel@tonic-gate 	mblk_t		te_closemp;	/* for entering serializer on close */
6367c478bd9Sstevel@tonic-gate 	mblk_t		te_rsrvmp;	/* for entering serializer on rsrv */
6377c478bd9Sstevel@tonic-gate 	mblk_t		te_wsrvmp;	/* for entering serializer on wsrv */
6387c478bd9Sstevel@tonic-gate 	kmutex_t	te_srv_lock;
6397c478bd9Sstevel@tonic-gate 	kcondvar_t	te_srv_cv;
6407c478bd9Sstevel@tonic-gate 	uint8_t		te_rsrv_active;	/* Running in tl_rsrv()	*/
6417c478bd9Sstevel@tonic-gate 	uint8_t		te_wsrv_active;	/* Running in tl_wsrv()	*/
6427c478bd9Sstevel@tonic-gate 	/*
6437c478bd9Sstevel@tonic-gate 	 * Pieces of the endpoint state needed for serializer transitions.
6447c478bd9Sstevel@tonic-gate 	 */
6457c478bd9Sstevel@tonic-gate 	kmutex_t	te_ser_lock;	/* Protects the count below */
6467c478bd9Sstevel@tonic-gate 	uint_t		te_ser_count;	/* Number of messages on serializer */
6477c478bd9Sstevel@tonic-gate };
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate /*
6507c478bd9Sstevel@tonic-gate  * Flag values. Lower 4 bits specify that transport used.
6517c478bd9Sstevel@tonic-gate  * TL_LISTENER, TL_ACCEPTOR, TL_ACCEPTED and TL_EAGER are for debugging only,
6527c478bd9Sstevel@tonic-gate  * they allow to identify the endpoint more easily.
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate #define	TL_LISTENER	0x00010	/* the listener endpoint */
6557c478bd9Sstevel@tonic-gate #define	TL_ACCEPTOR	0x00020	/* the accepting endpoint */
6567c478bd9Sstevel@tonic-gate #define	TL_EAGER	0x00040	/* connecting endpoint */
6577c478bd9Sstevel@tonic-gate #define	TL_ACCEPTED	0x00080	/* accepted connection */
6587c478bd9Sstevel@tonic-gate #define	TL_SETCRED	0x00100	/* flag to indicate sending of credentials */
6597c478bd9Sstevel@tonic-gate #define	TL_SETUCRED	0x00200	/* flag to indicate sending of ucred */
6607c478bd9Sstevel@tonic-gate #define	TL_SOCKUCRED	0x00400	/* flag to indicate sending of SCM_UCRED */
6617c478bd9Sstevel@tonic-gate #define	TL_ADDRHASHED	0x01000	/* Endpoint address is stored in te_addrhash */
6627c478bd9Sstevel@tonic-gate #define	TL_CLOSE_SER	0x10000	/* Endpoint close has entered the serializer */
6637c478bd9Sstevel@tonic-gate /*
6647c478bd9Sstevel@tonic-gate  * Boolean checks for the endpoint type.
6657c478bd9Sstevel@tonic-gate  */
6667c478bd9Sstevel@tonic-gate #define		IS_CLTS(x)	(((x)->te_flag & TL_TICLTS) != 0)
6677c478bd9Sstevel@tonic-gate #define		IS_COTS(x)	(((x)->te_flag & TL_TICLTS) == 0)
6687c478bd9Sstevel@tonic-gate #define		IS_COTSORD(x)	(((x)->te_flag & TL_TICOTSORD) != 0)
6697c478bd9Sstevel@tonic-gate #define		IS_SOCKET(x)	(((x)->te_flag & TL_SOCKET) != 0)
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate  * Certain operations are always used together. These macros reduce the chance
6737c478bd9Sstevel@tonic-gate  * of missing a part of a combination.
6747c478bd9Sstevel@tonic-gate  */
6757c478bd9Sstevel@tonic-gate #define	TL_UNCONNECT(x) { tl_refrele(x); x = NULL; }
6767c478bd9Sstevel@tonic-gate #define	TL_REMOVE_PEER(x) { if ((x) != NULL) TL_UNCONNECT(x) }
6777c478bd9Sstevel@tonic-gate 
6787c478bd9Sstevel@tonic-gate #define	TL_PUTBQ(x, mp) {		\
6797c478bd9Sstevel@tonic-gate 	ASSERT(!((x)->te_flag & TL_CLOSE_SER));	\
6807c478bd9Sstevel@tonic-gate 	(x)->te_nowsrv = B_TRUE;	\
6817c478bd9Sstevel@tonic-gate 	(void) putbq((x)->te_wq, mp);	\
6827c478bd9Sstevel@tonic-gate }
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate #define	TL_QENABLE(x) { (x)->te_nowsrv = B_FALSE; qenable((x)->te_wq); }
6857c478bd9Sstevel@tonic-gate #define	TL_PUTQ(x, mp) { (x)->te_nowsrv = B_FALSE; (void)putq((x)->te_wq, mp); }
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate /*
6887c478bd9Sstevel@tonic-gate  * STREAMS driver glue data structures.
6897c478bd9Sstevel@tonic-gate  */
6907c478bd9Sstevel@tonic-gate static	struct	module_info	tl_minfo = {
6917c478bd9Sstevel@tonic-gate 	TL_ID,			/* mi_idnum */
6927c478bd9Sstevel@tonic-gate 	TL_NAME,		/* mi_idname */
6937c478bd9Sstevel@tonic-gate 	TL_MINPSZ,		/* mi_minpsz */
6947c478bd9Sstevel@tonic-gate 	TL_MAXPSZ,		/* mi_maxpsz */
6957c478bd9Sstevel@tonic-gate 	TL_HIWAT,		/* mi_hiwat */
6967c478bd9Sstevel@tonic-gate 	TL_LOWAT		/* mi_lowat */
6977c478bd9Sstevel@tonic-gate };
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate static	struct	qinit	tl_rinit = {
7007c478bd9Sstevel@tonic-gate 	NULL,			/* qi_putp */
701cb492d90SToomas Soome 	tl_rsrv,		/* qi_srvp */
7027c478bd9Sstevel@tonic-gate 	tl_open,		/* qi_qopen */
7037c478bd9Sstevel@tonic-gate 	tl_close,		/* qi_qclose */
7047c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
7057c478bd9Sstevel@tonic-gate 	&tl_minfo,		/* qi_minfo */
7067c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
7077c478bd9Sstevel@tonic-gate };
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate static	struct	qinit	tl_winit = {
710cb492d90SToomas Soome 	tl_wput,		/* qi_putp */
711cb492d90SToomas Soome 	tl_wsrv,		/* qi_srvp */
7127c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qopen */
7137c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qclose */
7147c478bd9Sstevel@tonic-gate 	NULL,			/* qi_qadmin */
7157c478bd9Sstevel@tonic-gate 	&tl_minfo,		/* qi_minfo */
7167c478bd9Sstevel@tonic-gate 	NULL			/* qi_mstat */
7177c478bd9Sstevel@tonic-gate };
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate static	struct streamtab	tlinfo = {
7207c478bd9Sstevel@tonic-gate 	&tl_rinit,		/* st_rdinit */
7217c478bd9Sstevel@tonic-gate 	&tl_winit,		/* st_wrinit */
7227c478bd9Sstevel@tonic-gate 	NULL,			/* st_muxrinit */
7237c478bd9Sstevel@tonic-gate 	NULL			/* st_muxwrinit */
7247c478bd9Sstevel@tonic-gate };
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate DDI_DEFINE_STREAM_OPS(tl_devops, nulldev, nulldev, tl_attach, tl_detach,
72719397407SSherry Moore     nulldev, tl_info, D_MP, &tlinfo, ddi_quiesce_not_supported);
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate static struct modldrv modldrv = {
7307c478bd9Sstevel@tonic-gate 	&mod_driverops,		/* Type of module -- pseudo driver here */
731903a11ebSrh 	"TPI Local Transport (tl)",
7327c478bd9Sstevel@tonic-gate 	&tl_devops,		/* driver ops */
7337c478bd9Sstevel@tonic-gate };
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate /*
7367c478bd9Sstevel@tonic-gate  * Module linkage information for the kernel.
7377c478bd9Sstevel@tonic-gate  */
7387c478bd9Sstevel@tonic-gate static struct modlinkage modlinkage = {
7397c478bd9Sstevel@tonic-gate 	MODREV_1,
7407c478bd9Sstevel@tonic-gate 	&modldrv,
7417c478bd9Sstevel@tonic-gate 	NULL
7427c478bd9Sstevel@tonic-gate };
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * Templates for response to info request
7467c478bd9Sstevel@tonic-gate  * Check sanity of unlimited connect data etc.
7477c478bd9Sstevel@tonic-gate  */
7487c478bd9Sstevel@tonic-gate 
749cb492d90SToomas Soome #define		TL_CLTS_PROVIDER_FLAG	(XPG4_1 | SENDZERO)
750cb492d90SToomas Soome #define		TL_COTS_PROVIDER_FLAG	(XPG4_1 | SENDZERO)
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate static struct T_info_ack tl_cots_info_ack =
7537c478bd9Sstevel@tonic-gate 	{
7547c478bd9Sstevel@tonic-gate 		T_INFO_ACK,	/* PRIM_type -always T_INFO_ACK */
7557c478bd9Sstevel@tonic-gate 		T_INFINITE,	/* TSDU size */
7567c478bd9Sstevel@tonic-gate 		T_INFINITE,	/* ETSDU size */
7577c478bd9Sstevel@tonic-gate 		T_INFINITE,	/* CDATA_size */
7587c478bd9Sstevel@tonic-gate 		T_INFINITE,	/* DDATA_size */
7597c478bd9Sstevel@tonic-gate 		T_INFINITE,	/* ADDR_size  */
7607c478bd9Sstevel@tonic-gate 		T_INFINITE,	/* OPT_size */
7617c478bd9Sstevel@tonic-gate 		0,		/* TIDU_size - fill at run time */
7627c478bd9Sstevel@tonic-gate 		T_COTS,		/* SERV_type */
7637c478bd9Sstevel@tonic-gate 		-1,		/* CURRENT_state */
7647c478bd9Sstevel@tonic-gate 		TL_COTS_PROVIDER_FLAG	/* PROVIDER_flag */
7657c478bd9Sstevel@tonic-gate 	};
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate static struct T_info_ack tl_clts_info_ack =
7687c478bd9Sstevel@tonic-gate 	{
7697c478bd9Sstevel@tonic-gate 		T_INFO_ACK,	/* PRIM_type - always T_INFO_ACK */
7707c478bd9Sstevel@tonic-gate 		0,		/* TSDU_size - fill at run time */
7717c478bd9Sstevel@tonic-gate 		-2,		/* ETSDU_size -2 => not supported */
7727c478bd9Sstevel@tonic-gate 		-2,		/* CDATA_size -2 => not supported */
7737c478bd9Sstevel@tonic-gate 		-2,		/* DDATA_size  -2 => not supported */
774b2d8fb89SDan Kruchinin 		-1,		/* ADDR_size -1 => infinite */
7757c478bd9Sstevel@tonic-gate 		-1,		/* OPT_size */
7767c478bd9Sstevel@tonic-gate 		0,		/* TIDU_size - fill at run time */
7777c478bd9Sstevel@tonic-gate 		T_CLTS,		/* SERV_type */
7787c478bd9Sstevel@tonic-gate 		-1,		/* CURRENT_state */
7797c478bd9Sstevel@tonic-gate 		TL_CLTS_PROVIDER_FLAG /* PROVIDER_flag */
7807c478bd9Sstevel@tonic-gate 	};
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate  * private copy of devinfo pointer used in tl_info
7847c478bd9Sstevel@tonic-gate  */
7857c478bd9Sstevel@tonic-gate static dev_info_t *tl_dip;
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * Endpoints cache.
7897c478bd9Sstevel@tonic-gate  */
7907c478bd9Sstevel@tonic-gate static kmem_cache_t *tl_cache;
7917c478bd9Sstevel@tonic-gate /*
7927c478bd9Sstevel@tonic-gate  * Minor number space.
7937c478bd9Sstevel@tonic-gate  */
7947c478bd9Sstevel@tonic-gate static id_space_t *tl_minors;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate /*
7977c478bd9Sstevel@tonic-gate  * Default Data Unit size.
7987c478bd9Sstevel@tonic-gate  */
7997c478bd9Sstevel@tonic-gate static t_scalar_t tl_tidusz;
8007c478bd9Sstevel@tonic-gate 
8017c478bd9Sstevel@tonic-gate /*
8027c478bd9Sstevel@tonic-gate  * Size of hash tables.
8037c478bd9Sstevel@tonic-gate  */
8047c478bd9Sstevel@tonic-gate static size_t tl_hash_size = TL_HASH_SIZE;
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate /*
8077c478bd9Sstevel@tonic-gate  * Debug and test variable ONLY. Turn off T_CONN_IND queueing
8087c478bd9Sstevel@tonic-gate  * for sockets.
8097c478bd9Sstevel@tonic-gate  */
8107c478bd9Sstevel@tonic-gate static int tl_disable_early_connect = 0;
8117c478bd9Sstevel@tonic-gate static int tl_client_closing_when_accepting;
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate static int tl_serializer_noswitch;
8147c478bd9Sstevel@tonic-gate 
815b8ffbd31SJohn Levon #define	nr	127		/* not reachable */
816b8ffbd31SJohn Levon 
817b8ffbd31SJohn Levon #define	TE_NOEVENTS	28
818b8ffbd31SJohn Levon 
819b8ffbd31SJohn Levon static char nextstate[TE_NOEVENTS][TS_NOSTATES] = {
820b8ffbd31SJohn Levon 				/* STATES */
821b8ffbd31SJohn Levon 	/* 0  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16 */
822b8ffbd31SJohn Levon 
823b8ffbd31SJohn Levon /* Initialization events */
824b8ffbd31SJohn Levon 
825b8ffbd31SJohn Levon #define	TE_BIND_REQ	0	/* bind request				*/
826b8ffbd31SJohn Levon 	{ 1, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
827b8ffbd31SJohn Levon #define	TE_UNBIND_REQ	1	/* unbind request			*/
828b8ffbd31SJohn Levon 	{nr, nr, nr,  2, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
829b8ffbd31SJohn Levon #define	TE_OPTMGMT_REQ	2	/* manage options req			*/
830b8ffbd31SJohn Levon 	{nr, nr, nr,  4, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
831b8ffbd31SJohn Levon #define	TE_BIND_ACK	3	/* bind acknowledment			*/
832b8ffbd31SJohn Levon 	{nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
833b8ffbd31SJohn Levon #define	TE_OPTMGMT_ACK	4	/* manage options ack			*/
834b8ffbd31SJohn Levon 	{nr, nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
835b8ffbd31SJohn Levon #define	TE_ERROR_ACK	5	/* error acknowledgment			*/
836b8ffbd31SJohn Levon 	{nr,  0,  3, nr,  3,  3, nr, nr,  7, nr, nr, nr,  6,  7,  9, 10, 11},
837b8ffbd31SJohn Levon #define	TE_OK_ACK1	6	/* ok ack  seqcnt == 0			*/
838b8ffbd31SJohn Levon 	{nr, nr,  0, nr, nr,  6, nr, nr, nr, nr, nr, nr,  3, nr,  3,  3,  3},
839b8ffbd31SJohn Levon #define	TE_OK_ACK2	7	/* ok ack  seqcnt == 1, q == resq	*/
840b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, nr, nr, nr,  3, nr, nr, nr},
841b8ffbd31SJohn Levon #define	TE_OK_ACK3	8	/* ok ack  seqcnt == 1, q != resq	*/
842b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr,  3, nr, nr, nr, nr,  3, nr, nr, nr},
843b8ffbd31SJohn Levon #define	TE_OK_ACK4	9	/* ok ack  seqcnt > 1			*/
844b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr,  7, nr, nr, nr, nr,  7, nr, nr, nr},
845b8ffbd31SJohn Levon 
846b8ffbd31SJohn Levon /* Connection oriented events */
847b8ffbd31SJohn Levon #define	TE_CONN_REQ	10	/* connection request			*/
848b8ffbd31SJohn Levon 	{nr, nr, nr,  5, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
849b8ffbd31SJohn Levon #define	TE_CONN_RES	11	/* connection response			*/
850b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr,  8, nr, nr, nr, nr, nr, nr, nr, nr, nr},
851b8ffbd31SJohn Levon #define	TE_DISCON_REQ	12	/* disconnect request			*/
852b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, 12, 13, nr, 14, 15, 16, nr, nr, nr, nr, nr},
853b8ffbd31SJohn Levon #define	TE_DATA_REQ	13	/* data request				*/
854b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, 11, nr, nr, nr, nr, nr},
855b8ffbd31SJohn Levon #define	TE_EXDATA_REQ	14	/* expedited data request		*/
856b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, nr, 11, nr, nr, nr, nr, nr},
857b8ffbd31SJohn Levon #define	TE_ORDREL_REQ	15	/* orderly release req			*/
858b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr, nr, 10, nr,  3, nr, nr, nr, nr, nr},
859b8ffbd31SJohn Levon #define	TE_CONN_IND	16	/* connection indication		*/
860b8ffbd31SJohn Levon 	{nr, nr, nr,  7, nr, nr, nr,  7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
861b8ffbd31SJohn Levon #define	TE_CONN_CON	17	/* connection confirmation		*/
862b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr,  9, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
863b8ffbd31SJohn Levon #define	TE_DATA_IND	18	/* data indication			*/
864b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, 10, nr, nr, nr, nr, nr, nr},
865b8ffbd31SJohn Levon #define	TE_EXDATA_IND	19	/* expedited data indication		*/
866b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr, nr,  9, 10, nr, nr, nr, nr, nr, nr},
867b8ffbd31SJohn Levon #define	TE_ORDREL_IND	20	/* orderly release ind			*/
868b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr, nr, nr, 11,  3, nr, nr, nr, nr, nr, nr},
869b8ffbd31SJohn Levon #define	TE_DISCON_IND1	21	/* disconnect indication seq == 0	*/
870b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr,  3, nr, nr,  3,  3,  3, nr, nr, nr, nr, nr},
871b8ffbd31SJohn Levon #define	TE_DISCON_IND2	22	/* disconnect indication seq == 1	*/
872b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr},
873b8ffbd31SJohn Levon #define	TE_DISCON_IND3	23	/* disconnect indication seq > 1	*/
874b8ffbd31SJohn Levon 	{nr, nr, nr, nr, nr, nr, nr,  7, nr, nr, nr, nr, nr, nr, nr, nr, nr},
875b8ffbd31SJohn Levon #define	TE_PASS_CONN	24	/* pass connection			*/
876b8ffbd31SJohn Levon 	{nr, nr, nr,  9, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
877b8ffbd31SJohn Levon 
878b8ffbd31SJohn Levon 
879b8ffbd31SJohn Levon /* Unit data events */
880b8ffbd31SJohn Levon 
881b8ffbd31SJohn Levon #define	TE_UNITDATA_REQ	25	/* unitdata request			*/
882b8ffbd31SJohn Levon 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
883b8ffbd31SJohn Levon #define	TE_UNITDATA_IND	26	/* unitdata indication			*/
884b8ffbd31SJohn Levon 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
885b8ffbd31SJohn Levon #define	TE_UDERROR_IND	27	/* unitdata error indication		*/
886b8ffbd31SJohn Levon 	{nr, nr, nr,  3, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr, nr},
887b8ffbd31SJohn Levon };
888b8ffbd31SJohn Levon 
889b8ffbd31SJohn Levon 
890b8ffbd31SJohn Levon 
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate  * LOCAL FUNCTION PROTOTYPES
8937c478bd9Sstevel@tonic-gate  * -------------------------
8947c478bd9Sstevel@tonic-gate  */
8957c478bd9Sstevel@tonic-gate static boolean_t tl_eqaddr(tl_addr_t *, tl_addr_t *);
8967c478bd9Sstevel@tonic-gate static void tl_do_proto(mblk_t *, tl_endpt_t *);
8977c478bd9Sstevel@tonic-gate static void tl_do_ioctl(mblk_t *, tl_endpt_t *);
8987c478bd9Sstevel@tonic-gate static void tl_do_ioctl_ser(mblk_t *, tl_endpt_t *);
8997c478bd9Sstevel@tonic-gate static void tl_error_ack(queue_t *, mblk_t *, t_scalar_t, t_scalar_t,
900cb492d90SToomas Soome     t_scalar_t);
9017c478bd9Sstevel@tonic-gate static void tl_bind(mblk_t *, tl_endpt_t *);
9027c478bd9Sstevel@tonic-gate static void tl_bind_ser(mblk_t *, tl_endpt_t *);
9037c478bd9Sstevel@tonic-gate static void tl_ok_ack(queue_t *, mblk_t  *mp, t_scalar_t);
9047c478bd9Sstevel@tonic-gate static void tl_unbind(mblk_t *, tl_endpt_t *);
9057c478bd9Sstevel@tonic-gate static void tl_optmgmt(queue_t *, mblk_t *);
9067c478bd9Sstevel@tonic-gate static void tl_conn_req(queue_t *, mblk_t *);
9077c478bd9Sstevel@tonic-gate static void tl_conn_req_ser(mblk_t *, tl_endpt_t *);
9087c478bd9Sstevel@tonic-gate static void tl_conn_res(mblk_t *, tl_endpt_t *);
9097c478bd9Sstevel@tonic-gate static void tl_discon_req(mblk_t *, tl_endpt_t *);
9107c478bd9Sstevel@tonic-gate static void tl_capability_req(mblk_t *, tl_endpt_t *);
9117c478bd9Sstevel@tonic-gate static void tl_info_req_ser(mblk_t *, tl_endpt_t *);
912b2d8fb89SDan Kruchinin static void tl_addr_req_ser(mblk_t *, tl_endpt_t *);
9137c478bd9Sstevel@tonic-gate static void tl_info_req(mblk_t *, tl_endpt_t *);
9147c478bd9Sstevel@tonic-gate static void tl_addr_req(mblk_t *, tl_endpt_t *);
9157c478bd9Sstevel@tonic-gate static void tl_connected_cots_addr_req(mblk_t *, tl_endpt_t *);
9167c478bd9Sstevel@tonic-gate static void tl_data(mblk_t  *, tl_endpt_t *);
9177c478bd9Sstevel@tonic-gate static void tl_exdata(mblk_t *, tl_endpt_t *);
9187c478bd9Sstevel@tonic-gate static void tl_ordrel(mblk_t *, tl_endpt_t *);
9197c478bd9Sstevel@tonic-gate static void tl_unitdata(mblk_t *, tl_endpt_t *);
9207c478bd9Sstevel@tonic-gate static void tl_unitdata_ser(mblk_t *, tl_endpt_t *);
9217c478bd9Sstevel@tonic-gate static void tl_uderr(queue_t *, mblk_t *, t_scalar_t);
9227c478bd9Sstevel@tonic-gate static tl_endpt_t *tl_find_peer(tl_endpt_t *, tl_addr_t *);
9237c478bd9Sstevel@tonic-gate static tl_endpt_t *tl_sock_find_peer(tl_endpt_t *, struct so_ux_addr *);
9247c478bd9Sstevel@tonic-gate static boolean_t tl_get_any_addr(tl_endpt_t *, tl_addr_t *);
9257c478bd9Sstevel@tonic-gate static void tl_cl_backenable(tl_endpt_t *);
9267c478bd9Sstevel@tonic-gate static void tl_co_unconnect(tl_endpt_t *);
9277c478bd9Sstevel@tonic-gate static mblk_t *tl_resizemp(mblk_t *, ssize_t);
9287c478bd9Sstevel@tonic-gate static void tl_discon_ind(tl_endpt_t *, uint32_t);
9297c478bd9Sstevel@tonic-gate static mblk_t *tl_discon_ind_alloc(uint32_t, t_scalar_t);
9307c478bd9Sstevel@tonic-gate static mblk_t *tl_ordrel_ind_alloc(void);
9317c478bd9Sstevel@tonic-gate static tl_icon_t *tl_icon_find(tl_endpt_t *, t_scalar_t);
9327c478bd9Sstevel@tonic-gate static void tl_icon_queuemsg(tl_endpt_t *, t_scalar_t, mblk_t *);
9337c478bd9Sstevel@tonic-gate static boolean_t tl_icon_hasprim(tl_endpt_t *, t_scalar_t, t_scalar_t);
9347c478bd9Sstevel@tonic-gate static void tl_icon_sendmsgs(tl_endpt_t *, mblk_t **);
9357c478bd9Sstevel@tonic-gate static void tl_icon_freemsgs(mblk_t **);
9367c478bd9Sstevel@tonic-gate static void tl_merror(queue_t *, mblk_t *, int);
93745916cd2Sjpk static void tl_fill_option(uchar_t *, cred_t *, pid_t, int, cred_t *);
9387c478bd9Sstevel@tonic-gate static int tl_default_opt(queue_t *, int, int, uchar_t *);
9397c478bd9Sstevel@tonic-gate static int tl_get_opt(queue_t *, int, int, uchar_t *);
9407c478bd9Sstevel@tonic-gate static int tl_set_opt(queue_t *, uint_t, int, int, uint_t, uchar_t *, uint_t *,
941bd670b35SErik Nordmark     uchar_t *, void *, cred_t *);
9427c478bd9Sstevel@tonic-gate static void tl_memrecover(queue_t *, mblk_t *, size_t);
9437c478bd9Sstevel@tonic-gate static void tl_freetip(tl_endpt_t *, tl_icon_t *);
9447c478bd9Sstevel@tonic-gate static void tl_free(tl_endpt_t *);
9457c478bd9Sstevel@tonic-gate static int  tl_constructor(void *, void *, int);
9467c478bd9Sstevel@tonic-gate static void tl_destructor(void *, void *);
9477c478bd9Sstevel@tonic-gate static void tl_find_callback(mod_hash_key_t, mod_hash_val_t);
9487c478bd9Sstevel@tonic-gate static tl_serializer_t *tl_serializer_alloc(int);
9497c478bd9Sstevel@tonic-gate static void tl_serializer_refhold(tl_serializer_t *);
9507c478bd9Sstevel@tonic-gate static void tl_serializer_refrele(tl_serializer_t *);
9517c478bd9Sstevel@tonic-gate static void tl_serializer_enter(tl_endpt_t *, tlproc_t, mblk_t *);
9527c478bd9Sstevel@tonic-gate static void tl_serializer_exit(tl_endpt_t *);
9537c478bd9Sstevel@tonic-gate static boolean_t tl_noclose(tl_endpt_t *);
9547c478bd9Sstevel@tonic-gate static void tl_closeok(tl_endpt_t *);
9557c478bd9Sstevel@tonic-gate static void tl_refhold(tl_endpt_t *);
9567c478bd9Sstevel@tonic-gate static void tl_refrele(tl_endpt_t *);
9577c478bd9Sstevel@tonic-gate static int tl_hash_cmp_addr(mod_hash_key_t, mod_hash_key_t);
9587c478bd9Sstevel@tonic-gate static uint_t tl_hash_by_addr(void *, mod_hash_key_t);
9597c478bd9Sstevel@tonic-gate static void tl_close_ser(mblk_t *, tl_endpt_t *);
9607c478bd9Sstevel@tonic-gate static void tl_close_finish_ser(mblk_t *, tl_endpt_t *);
9617c478bd9Sstevel@tonic-gate static void tl_wput_data_ser(mblk_t *, tl_endpt_t *);
9627c478bd9Sstevel@tonic-gate static void tl_proto_ser(mblk_t *, tl_endpt_t *);
9637c478bd9Sstevel@tonic-gate static void tl_putq_ser(mblk_t *, tl_endpt_t *);
9647c478bd9Sstevel@tonic-gate static void tl_wput_common_ser(mblk_t *, tl_endpt_t *);
9657c478bd9Sstevel@tonic-gate static void tl_wput_ser(mblk_t *, tl_endpt_t *);
9667c478bd9Sstevel@tonic-gate static void tl_wsrv_ser(mblk_t *, tl_endpt_t *);
9677c478bd9Sstevel@tonic-gate static void tl_rsrv_ser(mblk_t *, tl_endpt_t *);
9687c478bd9Sstevel@tonic-gate static void tl_addr_unbind(tl_endpt_t *);
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate /*
9717c478bd9Sstevel@tonic-gate  * Intialize option database object for TL
9727c478bd9Sstevel@tonic-gate  */
9737c478bd9Sstevel@tonic-gate 
9747c478bd9Sstevel@tonic-gate optdb_obj_t tl_opt_obj = {
9757c478bd9Sstevel@tonic-gate 	tl_default_opt,		/* TL default value function pointer */
9767c478bd9Sstevel@tonic-gate 	tl_get_opt,		/* TL get function pointer */
9777c478bd9Sstevel@tonic-gate 	tl_set_opt,		/* TL set function pointer */
9787c478bd9Sstevel@tonic-gate 	TL_OPT_ARR_CNT,		/* TL option database count of entries */
9797c478bd9Sstevel@tonic-gate 	tl_opt_arr,		/* TL option database */
9807c478bd9Sstevel@tonic-gate 	TL_VALID_LEVELS_CNT,	/* TL valid level count of entries */
9817c478bd9Sstevel@tonic-gate 	tl_valid_levels_arr	/* TL valid level array */
9827c478bd9Sstevel@tonic-gate };
9837c478bd9Sstevel@tonic-gate 
9847c478bd9Sstevel@tonic-gate /*
9857c478bd9Sstevel@tonic-gate  * LOCAL FUNCTIONS AND DRIVER ENTRY POINTS
9867c478bd9Sstevel@tonic-gate  * ---------------------------------------
9877c478bd9Sstevel@tonic-gate  */
9887c478bd9Sstevel@tonic-gate 
9897c478bd9Sstevel@tonic-gate /*
9907c478bd9Sstevel@tonic-gate  * Loadable module routines
9917c478bd9Sstevel@tonic-gate  */
9927c478bd9Sstevel@tonic-gate int
_init(void)9937c478bd9Sstevel@tonic-gate _init(void)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	return (mod_install(&modlinkage));
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate int
_fini(void)9997c478bd9Sstevel@tonic-gate _fini(void)
10007c478bd9Sstevel@tonic-gate {
10017c478bd9Sstevel@tonic-gate 	return (mod_remove(&modlinkage));
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate int
_info(struct modinfo * modinfop)10057c478bd9Sstevel@tonic-gate _info(struct modinfo *modinfop)
10067c478bd9Sstevel@tonic-gate {
10077c478bd9Sstevel@tonic-gate 	return (mod_info(&modlinkage, modinfop));
10087c478bd9Sstevel@tonic-gate }
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate /*
10117c478bd9Sstevel@tonic-gate  * Driver Entry Points and Other routines
10127c478bd9Sstevel@tonic-gate  */
10137c478bd9Sstevel@tonic-gate static int
tl_attach(dev_info_t * devi,ddi_attach_cmd_t cmd)10147c478bd9Sstevel@tonic-gate tl_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
10157c478bd9Sstevel@tonic-gate {
10167c478bd9Sstevel@tonic-gate 	int i;
10177c478bd9Sstevel@tonic-gate 	char name[32];
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	/*
10207c478bd9Sstevel@tonic-gate 	 * Resume from a checkpoint state.
10217c478bd9Sstevel@tonic-gate 	 */
10227c478bd9Sstevel@tonic-gate 	if (cmd == DDI_RESUME)
10237c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
10247c478bd9Sstevel@tonic-gate 
10257c478bd9Sstevel@tonic-gate 	if (cmd != DDI_ATTACH)
10267c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	 * Deduce TIDU size to use.  Note: "strmsgsz" being 0 has semantics that
10307c478bd9Sstevel@tonic-gate 	 * streams message sizes can be unlimited. We use a defined constant
10317c478bd9Sstevel@tonic-gate 	 * instead.
10327c478bd9Sstevel@tonic-gate 	 */
10337c478bd9Sstevel@tonic-gate 	tl_tidusz = strmsgsz != 0 ? (t_scalar_t)strmsgsz : TL_TIDUSZ;
10347c478bd9Sstevel@tonic-gate 
10357c478bd9Sstevel@tonic-gate 	/*
10367c478bd9Sstevel@tonic-gate 	 * Create subdevices for each transport.
10377c478bd9Sstevel@tonic-gate 	 */
10387c478bd9Sstevel@tonic-gate 	for (i = 0; i < TL_UNUSED; i++) {
10397c478bd9Sstevel@tonic-gate 		if (ddi_create_minor_node(devi,
1040fc80c0dfSnordmark 		    tl_transports[i].tr_name,
1041fc80c0dfSnordmark 		    S_IFCHR, tl_transports[i].tr_minor,
10429173eaebSToomas Soome 		    DDI_PSEUDO, 0) == DDI_FAILURE) {
10437c478bd9Sstevel@tonic-gate 			ddi_remove_minor_node(devi, NULL);
10447c478bd9Sstevel@tonic-gate 			return (DDI_FAILURE);
10457c478bd9Sstevel@tonic-gate 		}
10467c478bd9Sstevel@tonic-gate 	}
10477c478bd9Sstevel@tonic-gate 
10487c478bd9Sstevel@tonic-gate 	tl_cache = kmem_cache_create("tl_cache", sizeof (tl_endpt_t),
10497c478bd9Sstevel@tonic-gate 	    0, tl_constructor, tl_destructor, NULL, NULL, NULL, 0);
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 	if (tl_cache == NULL) {
10527c478bd9Sstevel@tonic-gate 		ddi_remove_minor_node(devi, NULL);
10537c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
10547c478bd9Sstevel@tonic-gate 	}
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 	tl_minors = id_space_create("tl_minor_space",
10577c478bd9Sstevel@tonic-gate 	    TL_MINOR_START, MAXMIN32 - TL_MINOR_START + 1);
10587c478bd9Sstevel@tonic-gate 
10597c478bd9Sstevel@tonic-gate 	/*
10607c478bd9Sstevel@tonic-gate 	 * Create ID space for minor numbers
10617c478bd9Sstevel@tonic-gate 	 */
10627c478bd9Sstevel@tonic-gate 	for (i = 0; i < TL_MAXTRANSPORT; i++) {
10637c478bd9Sstevel@tonic-gate 		tl_transport_state_t *t = &tl_transports[i];
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate 		if (i == TL_UNUSED)
10667c478bd9Sstevel@tonic-gate 			continue;
10677c478bd9Sstevel@tonic-gate 
10687c478bd9Sstevel@tonic-gate 		/* Socket COTSORD shares namespace with COTS */
10697c478bd9Sstevel@tonic-gate 		if (i == TL_SOCK_COTSORD) {
10707c478bd9Sstevel@tonic-gate 			t->tr_ai_hash =
10717c478bd9Sstevel@tonic-gate 			    tl_transports[TL_SOCK_COTS].tr_ai_hash;
10727c478bd9Sstevel@tonic-gate 			ASSERT(t->tr_ai_hash != NULL);
10737c478bd9Sstevel@tonic-gate 			t->tr_addr_hash =
10747c478bd9Sstevel@tonic-gate 			    tl_transports[TL_SOCK_COTS].tr_addr_hash;
10757c478bd9Sstevel@tonic-gate 			ASSERT(t->tr_addr_hash != NULL);
10767c478bd9Sstevel@tonic-gate 			continue;
10777c478bd9Sstevel@tonic-gate 		}
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 		/*
10807c478bd9Sstevel@tonic-gate 		 * Create hash tables.
10817c478bd9Sstevel@tonic-gate 		 */
10827c478bd9Sstevel@tonic-gate 		(void) snprintf(name, sizeof (name), "%s_ai_hash",
10837c478bd9Sstevel@tonic-gate 		    t->tr_name);
10847c478bd9Sstevel@tonic-gate #ifdef _ILP32
10857c478bd9Sstevel@tonic-gate 		if (i & TL_SOCKET)
10867c478bd9Sstevel@tonic-gate 			t->tr_ai_hash =
10877c478bd9Sstevel@tonic-gate 			    mod_hash_create_idhash(name, tl_hash_size - 1,
1088fc80c0dfSnordmark 			    mod_hash_null_valdtor);
10897c478bd9Sstevel@tonic-gate 		else
10907c478bd9Sstevel@tonic-gate 			t->tr_ai_hash =
10917c478bd9Sstevel@tonic-gate 			    mod_hash_create_ptrhash(name, tl_hash_size,
1092fc80c0dfSnordmark 			    mod_hash_null_valdtor, sizeof (queue_t));
10937c478bd9Sstevel@tonic-gate #else
10947c478bd9Sstevel@tonic-gate 		t->tr_ai_hash =
10957c478bd9Sstevel@tonic-gate 		    mod_hash_create_idhash(name, tl_hash_size - 1,
1096fc80c0dfSnordmark 		    mod_hash_null_valdtor);
10977c478bd9Sstevel@tonic-gate #endif /* _ILP32 */
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate 		if (i & TL_SOCKET) {
11007c478bd9Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%s_sockaddr_hash",
11017c478bd9Sstevel@tonic-gate 			    t->tr_name);
11027c478bd9Sstevel@tonic-gate 			t->tr_addr_hash = mod_hash_create_ptrhash(name,
11037c478bd9Sstevel@tonic-gate 			    tl_hash_size, mod_hash_null_valdtor,
11047c478bd9Sstevel@tonic-gate 			    sizeof (uintptr_t));
11057c478bd9Sstevel@tonic-gate 		} else {
11067c478bd9Sstevel@tonic-gate 			(void) snprintf(name, sizeof (name), "%s_addr_hash",
11077c478bd9Sstevel@tonic-gate 			    t->tr_name);
11087c478bd9Sstevel@tonic-gate 			t->tr_addr_hash = mod_hash_create_extended(name,
11097c478bd9Sstevel@tonic-gate 			    tl_hash_size, mod_hash_null_keydtor,
11107c478bd9Sstevel@tonic-gate 			    mod_hash_null_valdtor,
11117c478bd9Sstevel@tonic-gate 			    tl_hash_by_addr, NULL, tl_hash_cmp_addr, KM_SLEEP);
11127c478bd9Sstevel@tonic-gate 		}
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 		/* Create serializer for connectionless transports. */
11157c478bd9Sstevel@tonic-gate 		if (i & TL_TICLTS)
11167c478bd9Sstevel@tonic-gate 			t->tr_serializer = tl_serializer_alloc(KM_SLEEP);
11177c478bd9Sstevel@tonic-gate 	}
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate 	tl_dip = devi;
11207c478bd9Sstevel@tonic-gate 
11217c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
11227c478bd9Sstevel@tonic-gate }
11237c478bd9Sstevel@tonic-gate 
11247c478bd9Sstevel@tonic-gate static int
tl_detach(dev_info_t * devi,ddi_detach_cmd_t cmd)11257c478bd9Sstevel@tonic-gate tl_detach(dev_info_t *devi, ddi_detach_cmd_t cmd)
11267c478bd9Sstevel@tonic-gate {
11277c478bd9Sstevel@tonic-gate 	int i;
11287c478bd9Sstevel@tonic-gate 
11297c478bd9Sstevel@tonic-gate 	if (cmd == DDI_SUSPEND)
11307c478bd9Sstevel@tonic-gate 		return (DDI_SUCCESS);
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 	if (cmd != DDI_DETACH)
11337c478bd9Sstevel@tonic-gate 		return (DDI_FAILURE);
11347c478bd9Sstevel@tonic-gate 
11357c478bd9Sstevel@tonic-gate 	/*
11367c478bd9Sstevel@tonic-gate 	 * Destroy arenas and hash tables.
11377c478bd9Sstevel@tonic-gate 	 */
11387c478bd9Sstevel@tonic-gate 	for (i = 0; i < TL_MAXTRANSPORT; i++) {
11397c478bd9Sstevel@tonic-gate 		tl_transport_state_t *t = &tl_transports[i];
11407c478bd9Sstevel@tonic-gate 
11417c478bd9Sstevel@tonic-gate 		if ((i == TL_UNUSED) || (i == TL_SOCK_COTSORD))
11427c478bd9Sstevel@tonic-gate 			continue;
11437c478bd9Sstevel@tonic-gate 
114456f33205SJonathan Adams 		EQUIV(i & TL_TICLTS, t->tr_serializer != NULL);
11457c478bd9Sstevel@tonic-gate 		if (t->tr_serializer != NULL) {
11467c478bd9Sstevel@tonic-gate 			tl_serializer_refrele(t->tr_serializer);
11477c478bd9Sstevel@tonic-gate 			t->tr_serializer = NULL;
11487c478bd9Sstevel@tonic-gate 		}
11497c478bd9Sstevel@tonic-gate 
11507c478bd9Sstevel@tonic-gate #ifdef _ILP32
11517c478bd9Sstevel@tonic-gate 		if (i & TL_SOCKET)
11527c478bd9Sstevel@tonic-gate 			mod_hash_destroy_idhash(t->tr_ai_hash);
11537c478bd9Sstevel@tonic-gate 		else
11547c478bd9Sstevel@tonic-gate 			mod_hash_destroy_ptrhash(t->tr_ai_hash);
11557c478bd9Sstevel@tonic-gate #else
11567c478bd9Sstevel@tonic-gate 		mod_hash_destroy_idhash(t->tr_ai_hash);
11577c478bd9Sstevel@tonic-gate #endif /* _ILP32 */
11587c478bd9Sstevel@tonic-gate 		t->tr_ai_hash = NULL;
11597c478bd9Sstevel@tonic-gate 		if (i & TL_SOCKET)
11607c478bd9Sstevel@tonic-gate 			mod_hash_destroy_ptrhash(t->tr_addr_hash);
11617c478bd9Sstevel@tonic-gate 		else
11627c478bd9Sstevel@tonic-gate 			mod_hash_destroy_hash(t->tr_addr_hash);
11637c478bd9Sstevel@tonic-gate 		t->tr_addr_hash = NULL;
11647c478bd9Sstevel@tonic-gate 	}
11657c478bd9Sstevel@tonic-gate 
11667c478bd9Sstevel@tonic-gate 	kmem_cache_destroy(tl_cache);
11677c478bd9Sstevel@tonic-gate 	tl_cache = NULL;
11687c478bd9Sstevel@tonic-gate 	id_space_destroy(tl_minors);
11697c478bd9Sstevel@tonic-gate 	tl_minors = NULL;
11707c478bd9Sstevel@tonic-gate 	ddi_remove_minor_node(devi, NULL);
11717c478bd9Sstevel@tonic-gate 	return (DDI_SUCCESS);
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
11747c478bd9Sstevel@tonic-gate /* ARGSUSED */
11757c478bd9Sstevel@tonic-gate static int
tl_info(dev_info_t * dip,ddi_info_cmd_t infocmd,void * arg,void ** result)11767c478bd9Sstevel@tonic-gate tl_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
11777c478bd9Sstevel@tonic-gate {
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	int retcode = DDI_FAILURE;
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	switch (infocmd) {
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2DEVINFO:
11847c478bd9Sstevel@tonic-gate 		if (tl_dip != NULL) {
11857c478bd9Sstevel@tonic-gate 			*result = (void *)tl_dip;
11867c478bd9Sstevel@tonic-gate 			retcode = DDI_SUCCESS;
11877c478bd9Sstevel@tonic-gate 		}
11887c478bd9Sstevel@tonic-gate 		break;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	case DDI_INFO_DEVT2INSTANCE:
1191cb492d90SToomas Soome 		*result = NULL;
11927c478bd9Sstevel@tonic-gate 		retcode = DDI_SUCCESS;
11937c478bd9Sstevel@tonic-gate 		break;
11947c478bd9Sstevel@tonic-gate 
11957c478bd9Sstevel@tonic-gate 	default:
11967c478bd9Sstevel@tonic-gate 		break;
11977c478bd9Sstevel@tonic-gate 	}
11987c478bd9Sstevel@tonic-gate 	return (retcode);
11997c478bd9Sstevel@tonic-gate }
12007c478bd9Sstevel@tonic-gate 
12017c478bd9Sstevel@tonic-gate /*
12027c478bd9Sstevel@tonic-gate  * Endpoint reference management.
12037c478bd9Sstevel@tonic-gate  */
12047c478bd9Sstevel@tonic-gate static void
tl_refhold(tl_endpt_t * tep)12057c478bd9Sstevel@tonic-gate tl_refhold(tl_endpt_t *tep)
12067c478bd9Sstevel@tonic-gate {
12071a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&tep->te_refcnt);
12087c478bd9Sstevel@tonic-gate }
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate static void
tl_refrele(tl_endpt_t * tep)12117c478bd9Sstevel@tonic-gate tl_refrele(tl_endpt_t *tep)
12127c478bd9Sstevel@tonic-gate {
12137c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_refcnt != 0);
12147c478bd9Sstevel@tonic-gate 
12151a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&tep->te_refcnt) == 0)
12167c478bd9Sstevel@tonic-gate 		tl_free(tep);
12177c478bd9Sstevel@tonic-gate }
12187c478bd9Sstevel@tonic-gate 
12197c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12207c478bd9Sstevel@tonic-gate static int
tl_constructor(void * buf,void * cdrarg,int kmflags)12217c478bd9Sstevel@tonic-gate tl_constructor(void *buf, void *cdrarg, int kmflags)
12227c478bd9Sstevel@tonic-gate {
12237c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep = buf;
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	bzero(tep, sizeof (tl_endpt_t));
12267c478bd9Sstevel@tonic-gate 	mutex_init(&tep->te_closelock, NULL, MUTEX_DEFAULT, NULL);
12277c478bd9Sstevel@tonic-gate 	cv_init(&tep->te_closecv, NULL, CV_DEFAULT, NULL);
12287c478bd9Sstevel@tonic-gate 	mutex_init(&tep->te_srv_lock, NULL, MUTEX_DEFAULT, NULL);
12297c478bd9Sstevel@tonic-gate 	cv_init(&tep->te_srv_cv, NULL, CV_DEFAULT, NULL);
12307c478bd9Sstevel@tonic-gate 	mutex_init(&tep->te_ser_lock, NULL, MUTEX_DEFAULT, NULL);
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	return (0);
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate /*ARGSUSED*/
12367c478bd9Sstevel@tonic-gate static void
tl_destructor(void * buf,void * cdrarg)12377c478bd9Sstevel@tonic-gate tl_destructor(void *buf, void *cdrarg)
12387c478bd9Sstevel@tonic-gate {
12397c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep = buf;
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate 	mutex_destroy(&tep->te_closelock);
12427c478bd9Sstevel@tonic-gate 	cv_destroy(&tep->te_closecv);
12437c478bd9Sstevel@tonic-gate 	mutex_destroy(&tep->te_srv_lock);
12447c478bd9Sstevel@tonic-gate 	cv_destroy(&tep->te_srv_cv);
12457c478bd9Sstevel@tonic-gate 	mutex_destroy(&tep->te_ser_lock);
12467c478bd9Sstevel@tonic-gate }
12477c478bd9Sstevel@tonic-gate 
12487c478bd9Sstevel@tonic-gate static void
tl_free(tl_endpt_t * tep)12497c478bd9Sstevel@tonic-gate tl_free(tl_endpt_t *tep)
12507c478bd9Sstevel@tonic-gate {
12517c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_refcnt == 0);
12527c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_transport != NULL);
12537c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_rq == NULL);
12547c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_wq == NULL);
12557c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_ser != NULL);
12567c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_ser_count == 0);
1257cb492d90SToomas Soome 	ASSERT(!(tep->te_flag & TL_ADDRHASHED));
12587c478bd9Sstevel@tonic-gate 
12597c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(tep)) {
12607c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_alen == TL_SOUX_ADDRLEN);
12617c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_abuf == &tep->te_uxaddr);
12627c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_vp == (void *)(uintptr_t)tep->te_minor);
12637c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_magic == SOU_MAGIC_IMPLICIT);
12647c478bd9Sstevel@tonic-gate 	} else if (tep->te_abuf != NULL) {
12657c478bd9Sstevel@tonic-gate 		kmem_free(tep->te_abuf, tep->te_alen);
12667c478bd9Sstevel@tonic-gate 		tep->te_alen = -1; /* uninitialized */
12677c478bd9Sstevel@tonic-gate 		tep->te_abuf = NULL;
12687c478bd9Sstevel@tonic-gate 	} else {
12697c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_alen == -1);
12707c478bd9Sstevel@tonic-gate 	}
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate 	id_free(tl_minors, tep->te_minor);
12737c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_credp == NULL);
12747c478bd9Sstevel@tonic-gate 
12757c478bd9Sstevel@tonic-gate 	if (tep->te_hash_hndl != NULL)
12767c478bd9Sstevel@tonic-gate 		mod_hash_cancel(tep->te_addrhash, &tep->te_hash_hndl);
12777c478bd9Sstevel@tonic-gate 
12787c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
12797c478bd9Sstevel@tonic-gate 		TL_REMOVE_PEER(tep->te_conp);
12807c478bd9Sstevel@tonic-gate 		TL_REMOVE_PEER(tep->te_oconp);
12817c478bd9Sstevel@tonic-gate 		tl_serializer_refrele(tep->te_ser);
12827c478bd9Sstevel@tonic-gate 		tep->te_ser = NULL;
12837c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_nicon == 0);
12847c478bd9Sstevel@tonic-gate 		ASSERT(list_head(&tep->te_iconp) == NULL);
12857c478bd9Sstevel@tonic-gate 	} else {
12867c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_lastep == NULL);
12877c478bd9Sstevel@tonic-gate 		ASSERT(list_head(&tep->te_flowlist) == NULL);
12887c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_flowq == NULL);
12897c478bd9Sstevel@tonic-gate 	}
12907c478bd9Sstevel@tonic-gate 
12917c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_bufcid == 0);
12927c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_timoutid == 0);
12937c478bd9Sstevel@tonic-gate 	bzero(&tep->te_ap, sizeof (tep->te_ap));
12947c478bd9Sstevel@tonic-gate 	tep->te_acceptor_id = 0;
12957c478bd9Sstevel@tonic-gate 
12967c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_closewait == 0);
12977c478bd9Sstevel@tonic-gate 	ASSERT(!tep->te_rsrv_active);
12987c478bd9Sstevel@tonic-gate 	ASSERT(!tep->te_wsrv_active);
12997c478bd9Sstevel@tonic-gate 	tep->te_closing = 0;
13007c478bd9Sstevel@tonic-gate 	tep->te_nowsrv = B_FALSE;
13017c478bd9Sstevel@tonic-gate 	tep->te_flag = 0;
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate 	kmem_cache_free(tl_cache, tep);
13047c478bd9Sstevel@tonic-gate }
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate /*
13077c478bd9Sstevel@tonic-gate  * Allocate/free reference-counted wrappers for serializers.
13087c478bd9Sstevel@tonic-gate  */
13097c478bd9Sstevel@tonic-gate static tl_serializer_t *
tl_serializer_alloc(int flags)13107c478bd9Sstevel@tonic-gate tl_serializer_alloc(int flags)
13117c478bd9Sstevel@tonic-gate {
13127c478bd9Sstevel@tonic-gate 	tl_serializer_t *s = kmem_alloc(sizeof (tl_serializer_t), flags);
13137c478bd9Sstevel@tonic-gate 	serializer_t *ser;
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	if (s == NULL)
13167c478bd9Sstevel@tonic-gate 		return (NULL);
13177c478bd9Sstevel@tonic-gate 
13187c478bd9Sstevel@tonic-gate 	ser = serializer_create(flags);
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	if (ser == NULL) {
13217c478bd9Sstevel@tonic-gate 		kmem_free(s, sizeof (tl_serializer_t));
13227c478bd9Sstevel@tonic-gate 		return (NULL);
13237c478bd9Sstevel@tonic-gate 	}
13247c478bd9Sstevel@tonic-gate 
13257c478bd9Sstevel@tonic-gate 	s->ts_refcnt = 1;
13267c478bd9Sstevel@tonic-gate 	s->ts_serializer = ser;
13277c478bd9Sstevel@tonic-gate 	return (s);
13287c478bd9Sstevel@tonic-gate }
13297c478bd9Sstevel@tonic-gate 
13307c478bd9Sstevel@tonic-gate static void
tl_serializer_refhold(tl_serializer_t * s)13317c478bd9Sstevel@tonic-gate tl_serializer_refhold(tl_serializer_t *s)
13327c478bd9Sstevel@tonic-gate {
13331a5e258fSJosef 'Jeff' Sipek 	atomic_inc_32(&s->ts_refcnt);
13347c478bd9Sstevel@tonic-gate }
13357c478bd9Sstevel@tonic-gate 
13367c478bd9Sstevel@tonic-gate static void
tl_serializer_refrele(tl_serializer_t * s)13377c478bd9Sstevel@tonic-gate tl_serializer_refrele(tl_serializer_t *s)
13387c478bd9Sstevel@tonic-gate {
13391a5e258fSJosef 'Jeff' Sipek 	if (atomic_dec_32_nv(&s->ts_refcnt) == 0) {
13407c478bd9Sstevel@tonic-gate 		serializer_destroy(s->ts_serializer);
13417c478bd9Sstevel@tonic-gate 		kmem_free(s, sizeof (tl_serializer_t));
13427c478bd9Sstevel@tonic-gate 	}
13437c478bd9Sstevel@tonic-gate }
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate /*
13467c478bd9Sstevel@tonic-gate  * Post a request on the endpoint serializer. For COTS transports keep track of
13477c478bd9Sstevel@tonic-gate  * the number of pending requests.
13487c478bd9Sstevel@tonic-gate  */
13497c478bd9Sstevel@tonic-gate static void
tl_serializer_enter(tl_endpt_t * tep,tlproc_t tlproc,mblk_t * mp)13507c478bd9Sstevel@tonic-gate tl_serializer_enter(tl_endpt_t *tep, tlproc_t tlproc, mblk_t *mp)
13517c478bd9Sstevel@tonic-gate {
13527c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
13537c478bd9Sstevel@tonic-gate 		mutex_enter(&tep->te_ser_lock);
13547c478bd9Sstevel@tonic-gate 		tep->te_ser_count++;
13557c478bd9Sstevel@tonic-gate 		mutex_exit(&tep->te_ser_lock);
13567c478bd9Sstevel@tonic-gate 	}
13577c478bd9Sstevel@tonic-gate 	serializer_enter(tep->te_serializer, (srproc_t *)tlproc, mp, tep);
13587c478bd9Sstevel@tonic-gate }
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate /*
13617c478bd9Sstevel@tonic-gate  * Complete processing the request on the serializer. Decrement the counter for
13627c478bd9Sstevel@tonic-gate  * pending requests for COTS transports.
13637c478bd9Sstevel@tonic-gate  */
13647c478bd9Sstevel@tonic-gate static void
tl_serializer_exit(tl_endpt_t * tep)13657c478bd9Sstevel@tonic-gate tl_serializer_exit(tl_endpt_t *tep)
13667c478bd9Sstevel@tonic-gate {
13677c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
13687c478bd9Sstevel@tonic-gate 		mutex_enter(&tep->te_ser_lock);
13697c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_ser_count != 0);
13707c478bd9Sstevel@tonic-gate 		tep->te_ser_count--;
13717c478bd9Sstevel@tonic-gate 		mutex_exit(&tep->te_ser_lock);
13727c478bd9Sstevel@tonic-gate 	}
13737c478bd9Sstevel@tonic-gate }
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate /*
13767c478bd9Sstevel@tonic-gate  * Hash management functions.
13777c478bd9Sstevel@tonic-gate  */
13787c478bd9Sstevel@tonic-gate 
13797c478bd9Sstevel@tonic-gate /*
13807c478bd9Sstevel@tonic-gate  * Return TRUE if two addresses are equal, false otherwise.
13817c478bd9Sstevel@tonic-gate  */
13827c478bd9Sstevel@tonic-gate static boolean_t
tl_eqaddr(tl_addr_t * ap1,tl_addr_t * ap2)13837c478bd9Sstevel@tonic-gate tl_eqaddr(tl_addr_t *ap1, tl_addr_t *ap2)
13847c478bd9Sstevel@tonic-gate {
13857c478bd9Sstevel@tonic-gate 	return ((ap1->ta_alen > 0) &&
13867c478bd9Sstevel@tonic-gate 	    (ap1->ta_alen == ap2->ta_alen) &&
13877c478bd9Sstevel@tonic-gate 	    (ap1->ta_zoneid == ap2->ta_zoneid) &&
13887c478bd9Sstevel@tonic-gate 	    (bcmp(ap1->ta_abuf, ap2->ta_abuf, ap1->ta_alen) == 0));
13897c478bd9Sstevel@tonic-gate }
13907c478bd9Sstevel@tonic-gate 
13917c478bd9Sstevel@tonic-gate /*
13927c478bd9Sstevel@tonic-gate  * This function is called whenever an endpoint is found in the hash table.
13937c478bd9Sstevel@tonic-gate  */
13947c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
13957c478bd9Sstevel@tonic-gate static void
tl_find_callback(mod_hash_key_t key,mod_hash_val_t val)13967c478bd9Sstevel@tonic-gate tl_find_callback(mod_hash_key_t key, mod_hash_val_t val)
13977c478bd9Sstevel@tonic-gate {
13987c478bd9Sstevel@tonic-gate 	tl_refhold((tl_endpt_t *)val);
13997c478bd9Sstevel@tonic-gate }
14007c478bd9Sstevel@tonic-gate 
14017c478bd9Sstevel@tonic-gate /*
14027c478bd9Sstevel@tonic-gate  * Address hash function.
14037c478bd9Sstevel@tonic-gate  */
14047c478bd9Sstevel@tonic-gate /* ARGSUSED */
14057c478bd9Sstevel@tonic-gate static uint_t
tl_hash_by_addr(void * hash_data,mod_hash_key_t key)14067c478bd9Sstevel@tonic-gate tl_hash_by_addr(void *hash_data, mod_hash_key_t key)
14077c478bd9Sstevel@tonic-gate {
14087c478bd9Sstevel@tonic-gate 	tl_addr_t *ap = (tl_addr_t *)key;
14097c478bd9Sstevel@tonic-gate 	size_t	len = ap->ta_alen;
14107c478bd9Sstevel@tonic-gate 	uchar_t *p = ap->ta_abuf;
14117c478bd9Sstevel@tonic-gate 	uint_t i, g;
14127c478bd9Sstevel@tonic-gate 
14137c478bd9Sstevel@tonic-gate 	ASSERT((len > 0) && (p != NULL));
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate 	for (i = ap->ta_zoneid; len -- != 0; p++) {
14167c478bd9Sstevel@tonic-gate 		i = (i << 4) + (*p);
14177c478bd9Sstevel@tonic-gate 		if ((g = (i & 0xf0000000U)) != 0) {
14187c478bd9Sstevel@tonic-gate 			i ^= (g >> 24);
14197c478bd9Sstevel@tonic-gate 			i ^= g;
14207c478bd9Sstevel@tonic-gate 		}
14217c478bd9Sstevel@tonic-gate 	}
14227c478bd9Sstevel@tonic-gate 	return (i);
14237c478bd9Sstevel@tonic-gate }
14247c478bd9Sstevel@tonic-gate 
14257c478bd9Sstevel@tonic-gate /*
14267c478bd9Sstevel@tonic-gate  * This function is used by hash lookups. It compares two generic addresses.
14277c478bd9Sstevel@tonic-gate  */
14287c478bd9Sstevel@tonic-gate static int
tl_hash_cmp_addr(mod_hash_key_t key1,mod_hash_key_t key2)14297c478bd9Sstevel@tonic-gate tl_hash_cmp_addr(mod_hash_key_t key1, mod_hash_key_t key2)
14307c478bd9Sstevel@tonic-gate {
14319173eaebSToomas Soome #ifdef	DEBUG
14327c478bd9Sstevel@tonic-gate 	tl_addr_t *ap1 = (tl_addr_t *)key1;
14337c478bd9Sstevel@tonic-gate 	tl_addr_t *ap2 = (tl_addr_t *)key2;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	ASSERT(key1 != NULL);
14367c478bd9Sstevel@tonic-gate 	ASSERT(key2 != NULL);
14377c478bd9Sstevel@tonic-gate 
14387c478bd9Sstevel@tonic-gate 	ASSERT(ap1->ta_abuf != NULL);
14397c478bd9Sstevel@tonic-gate 	ASSERT(ap2->ta_abuf != NULL);
14407c478bd9Sstevel@tonic-gate 	ASSERT(ap1->ta_alen > 0);
14417c478bd9Sstevel@tonic-gate 	ASSERT(ap2->ta_alen > 0);
14427c478bd9Sstevel@tonic-gate #endif
14437c478bd9Sstevel@tonic-gate 
1444cb492d90SToomas Soome 	return (!tl_eqaddr((tl_addr_t *)key1, (tl_addr_t *)key2));
14457c478bd9Sstevel@tonic-gate }
14467c478bd9Sstevel@tonic-gate 
14477c478bd9Sstevel@tonic-gate /*
14487c478bd9Sstevel@tonic-gate  * Prevent endpoint from closing if possible.
14497c478bd9Sstevel@tonic-gate  * Return B_TRUE on success, B_FALSE on failure.
14507c478bd9Sstevel@tonic-gate  */
14517c478bd9Sstevel@tonic-gate static boolean_t
tl_noclose(tl_endpt_t * tep)14527c478bd9Sstevel@tonic-gate tl_noclose(tl_endpt_t *tep)
14537c478bd9Sstevel@tonic-gate {
14547c478bd9Sstevel@tonic-gate 	boolean_t rc = B_FALSE;
14557c478bd9Sstevel@tonic-gate 
14567c478bd9Sstevel@tonic-gate 	mutex_enter(&tep->te_closelock);
1457cb492d90SToomas Soome 	if (!tep->te_closing) {
14587c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_closewait == 0);
14597c478bd9Sstevel@tonic-gate 		tep->te_closewait++;
14607c478bd9Sstevel@tonic-gate 		rc = B_TRUE;
14617c478bd9Sstevel@tonic-gate 	}
14627c478bd9Sstevel@tonic-gate 	mutex_exit(&tep->te_closelock);
14637c478bd9Sstevel@tonic-gate 	return (rc);
14647c478bd9Sstevel@tonic-gate }
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate /*
14677c478bd9Sstevel@tonic-gate  * Allow endpoint to close if needed.
14687c478bd9Sstevel@tonic-gate  */
14697c478bd9Sstevel@tonic-gate static void
tl_closeok(tl_endpt_t * tep)14707c478bd9Sstevel@tonic-gate tl_closeok(tl_endpt_t *tep)
14717c478bd9Sstevel@tonic-gate {
14727c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_closewait > 0);
14737c478bd9Sstevel@tonic-gate 	mutex_enter(&tep->te_closelock);
14747c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_closewait == 1);
14757c478bd9Sstevel@tonic-gate 	tep->te_closewait--;
14767c478bd9Sstevel@tonic-gate 	cv_signal(&tep->te_closecv);
14777c478bd9Sstevel@tonic-gate 	mutex_exit(&tep->te_closelock);
14787c478bd9Sstevel@tonic-gate }
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate /*
14817c478bd9Sstevel@tonic-gate  * STREAMS open entry point.
14827c478bd9Sstevel@tonic-gate  */
14837c478bd9Sstevel@tonic-gate /* ARGSUSED */
14847c478bd9Sstevel@tonic-gate static int
tl_open(queue_t * rq,dev_t * devp,int oflag,int sflag,cred_t * credp)14857c478bd9Sstevel@tonic-gate tl_open(queue_t	*rq, dev_t *devp, int oflag, int sflag,	cred_t	*credp)
14867c478bd9Sstevel@tonic-gate {
1487*1c8449e9SPatrick Mooney 	tl_endpt_t	*tep;
1488*1c8449e9SPatrick Mooney 	minor_t		minor = getminor(*devp);
1489*1c8449e9SPatrick Mooney 	id_t		inst_minor;
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	/*
14927c478bd9Sstevel@tonic-gate 	 * Driver is called directly. Both CLONEOPEN and MODOPEN
14937c478bd9Sstevel@tonic-gate 	 * are illegal
14947c478bd9Sstevel@tonic-gate 	 */
14957c478bd9Sstevel@tonic-gate 	if ((sflag == CLONEOPEN) || (sflag == MODOPEN))
14967c478bd9Sstevel@tonic-gate 		return (ENXIO);
14977c478bd9Sstevel@tonic-gate 
14987c478bd9Sstevel@tonic-gate 	if (rq->q_ptr != NULL)
14997c478bd9Sstevel@tonic-gate 		return (0);
15007c478bd9Sstevel@tonic-gate 
15017c478bd9Sstevel@tonic-gate 	/* Minor number should specify the mode used for the driver. */
15027c478bd9Sstevel@tonic-gate 	if ((minor >= TL_UNUSED))
15037c478bd9Sstevel@tonic-gate 		return (ENXIO);
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate 	if (oflag & SO_SOCKSTR) {
15067c478bd9Sstevel@tonic-gate 		minor |= TL_SOCKET;
15077c478bd9Sstevel@tonic-gate 	}
15087c478bd9Sstevel@tonic-gate 
1509*1c8449e9SPatrick Mooney 	/*
1510*1c8449e9SPatrick Mooney 	 * Attempt to allocate a unique minor number for this instance.
1511*1c8449e9SPatrick Mooney 	 * Avoid an uninterruptable sleep if none are available.
1512*1c8449e9SPatrick Mooney 	 */
1513*1c8449e9SPatrick Mooney 	if ((inst_minor = id_alloc_nosleep(tl_minors)) == -1) {
1514*1c8449e9SPatrick Mooney 		return (ENOMEM);
1515*1c8449e9SPatrick Mooney 	}
1516*1c8449e9SPatrick Mooney 
15177c478bd9Sstevel@tonic-gate 	tep = kmem_cache_alloc(tl_cache, KM_SLEEP);
15187c478bd9Sstevel@tonic-gate 	tep->te_refcnt = 1;
15197c478bd9Sstevel@tonic-gate 	tep->te_cpid = curproc->p_pid;
15207c478bd9Sstevel@tonic-gate 	rq->q_ptr = WR(rq)->q_ptr = tep;
15217c478bd9Sstevel@tonic-gate 	tep->te_state = TS_UNBND;
15227c478bd9Sstevel@tonic-gate 	tep->te_credp = credp;
15237c478bd9Sstevel@tonic-gate 	crhold(credp);
15247c478bd9Sstevel@tonic-gate 	tep->te_zoneid = getzoneid();
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	tep->te_flag = minor & TL_MINOR_MASK;
15277c478bd9Sstevel@tonic-gate 	tep->te_transport = &tl_transports[minor];
1528*1c8449e9SPatrick Mooney 	tep->te_minor = (minor_t)inst_minor;
15297c478bd9Sstevel@tonic-gate 
15307c478bd9Sstevel@tonic-gate 	/* Reserve hash handle for bind(). */
15317c478bd9Sstevel@tonic-gate 	(void) mod_hash_reserve(tep->te_addrhash, &tep->te_hash_hndl);
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	/* Transport-specific initialization */
15347c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
15357c478bd9Sstevel@tonic-gate 		/* Use private serializer */
15367c478bd9Sstevel@tonic-gate 		tep->te_ser = tl_serializer_alloc(KM_SLEEP);
15377c478bd9Sstevel@tonic-gate 
15387c478bd9Sstevel@tonic-gate 		/* Create list for pending connections */
15397c478bd9Sstevel@tonic-gate 		list_create(&tep->te_iconp, sizeof (tl_icon_t),
15407c478bd9Sstevel@tonic-gate 		    offsetof(tl_icon_t, ti_node));
15417c478bd9Sstevel@tonic-gate 		tep->te_qlen = 0;
15427c478bd9Sstevel@tonic-gate 		tep->te_nicon = 0;
15437c478bd9Sstevel@tonic-gate 		tep->te_oconp = NULL;
15447c478bd9Sstevel@tonic-gate 		tep->te_conp = NULL;
15457c478bd9Sstevel@tonic-gate 	} else {
15467c478bd9Sstevel@tonic-gate 		/* Use shared serializer */
15477c478bd9Sstevel@tonic-gate 		tep->te_ser = tep->te_transport->tr_serializer;
15487c478bd9Sstevel@tonic-gate 		bzero(&tep->te_flows, sizeof (list_node_t));
15497c478bd9Sstevel@tonic-gate 		/* Create list for flow control */
15507c478bd9Sstevel@tonic-gate 		list_create(&tep->te_flowlist, sizeof (tl_endpt_t),
15517c478bd9Sstevel@tonic-gate 		    offsetof(tl_endpt_t, te_flows));
15527c478bd9Sstevel@tonic-gate 		tep->te_flowq = NULL;
15537c478bd9Sstevel@tonic-gate 		tep->te_lastep = NULL;
15547c478bd9Sstevel@tonic-gate 
15557c478bd9Sstevel@tonic-gate 	}
15567c478bd9Sstevel@tonic-gate 
15577c478bd9Sstevel@tonic-gate 	/* Initialize endpoint address */
15587c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(tep)) {
15597c478bd9Sstevel@tonic-gate 		/* Socket-specific address handling. */
15607c478bd9Sstevel@tonic-gate 		tep->te_alen = TL_SOUX_ADDRLEN;
15617c478bd9Sstevel@tonic-gate 		tep->te_abuf = &tep->te_uxaddr;
15627c478bd9Sstevel@tonic-gate 		tep->te_vp = (void *)(uintptr_t)tep->te_minor;
15637c478bd9Sstevel@tonic-gate 		tep->te_magic = SOU_MAGIC_IMPLICIT;
15647c478bd9Sstevel@tonic-gate 	} else {
15657c478bd9Sstevel@tonic-gate 		tep->te_alen = -1;
15667c478bd9Sstevel@tonic-gate 		tep->te_abuf = NULL;
15677c478bd9Sstevel@tonic-gate 	}
15687c478bd9Sstevel@tonic-gate 
15697c478bd9Sstevel@tonic-gate 	/* clone the driver */
15707c478bd9Sstevel@tonic-gate 	*devp = makedevice(getmajor(*devp), tep->te_minor);
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	tep->te_rq = rq;
15737c478bd9Sstevel@tonic-gate 	tep->te_wq = WR(rq);
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate #ifdef	_ILP32
15767c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(tep))
15777c478bd9Sstevel@tonic-gate 		tep->te_acceptor_id = tep->te_minor;
15787c478bd9Sstevel@tonic-gate 	else
15797c478bd9Sstevel@tonic-gate 		tep->te_acceptor_id = (t_uscalar_t)rq;
15807c478bd9Sstevel@tonic-gate #else
15817c478bd9Sstevel@tonic-gate 	tep->te_acceptor_id = tep->te_minor;
15827c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	qprocson(rq);
15867c478bd9Sstevel@tonic-gate 
15877c478bd9Sstevel@tonic-gate 	/*
15887c478bd9Sstevel@tonic-gate 	 * Insert acceptor ID in the hash. The AI hash always sleeps on
15897c478bd9Sstevel@tonic-gate 	 * insertion so insertion can't fail.
15907c478bd9Sstevel@tonic-gate 	 */
15917c478bd9Sstevel@tonic-gate 	(void) mod_hash_insert(tep->te_transport->tr_ai_hash,
15927c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)tep->te_acceptor_id,
15937c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t)tep);
15947c478bd9Sstevel@tonic-gate 
15957c478bd9Sstevel@tonic-gate 	return (0);
15967c478bd9Sstevel@tonic-gate }
15977c478bd9Sstevel@tonic-gate 
15987c478bd9Sstevel@tonic-gate /* ARGSUSED1 */
15997c478bd9Sstevel@tonic-gate static int
tl_close(queue_t * rq,int flag,cred_t * credp)16007c478bd9Sstevel@tonic-gate tl_close(queue_t *rq, int flag,	cred_t *credp)
16017c478bd9Sstevel@tonic-gate {
16027c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep = (tl_endpt_t *)rq->q_ptr;
16037c478bd9Sstevel@tonic-gate 	tl_endpt_t *elp = NULL;
16047c478bd9Sstevel@tonic-gate 	queue_t *wq = tep->te_wq;
16057c478bd9Sstevel@tonic-gate 	int rc;
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	ASSERT(wq == WR(rq));
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	/*
16107c478bd9Sstevel@tonic-gate 	 * Remove the endpoint from acceptor hash.
16117c478bd9Sstevel@tonic-gate 	 */
16127c478bd9Sstevel@tonic-gate 	rc = mod_hash_remove(tep->te_transport->tr_ai_hash,
16137c478bd9Sstevel@tonic-gate 	    (mod_hash_key_t)(uintptr_t)tep->te_acceptor_id,
16147c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&elp);
16157c478bd9Sstevel@tonic-gate 	ASSERT(rc == 0 && tep == elp);
16167c478bd9Sstevel@tonic-gate 	if ((rc != 0) || (tep != elp)) {
16177c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
1618cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
1619fc80c0dfSnordmark 		    "tl_close:inconsistency in AI hash"));
16207c478bd9Sstevel@tonic-gate 	}
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	/*
16237c478bd9Sstevel@tonic-gate 	 * Wait till close is safe, then mark endpoint as closing.
16247c478bd9Sstevel@tonic-gate 	 */
16257c478bd9Sstevel@tonic-gate 	mutex_enter(&tep->te_closelock);
16267c478bd9Sstevel@tonic-gate 	while (tep->te_closewait)
16277c478bd9Sstevel@tonic-gate 		cv_wait(&tep->te_closecv, &tep->te_closelock);
16287c478bd9Sstevel@tonic-gate 	tep->te_closing = B_TRUE;
16297c478bd9Sstevel@tonic-gate 	/*
16307c478bd9Sstevel@tonic-gate 	 * Will wait for the serializer part of the close to finish, so set
16317c478bd9Sstevel@tonic-gate 	 * te_closewait now.
16327c478bd9Sstevel@tonic-gate 	 */
16337c478bd9Sstevel@tonic-gate 	tep->te_closewait = 1;
16347c478bd9Sstevel@tonic-gate 	tep->te_nowsrv = B_FALSE;
16357c478bd9Sstevel@tonic-gate 	mutex_exit(&tep->te_closelock);
16367c478bd9Sstevel@tonic-gate 
16377c478bd9Sstevel@tonic-gate 	/*
16387c478bd9Sstevel@tonic-gate 	 * tl_close_ser doesn't drop reference, so no need to tl_refhold.
16397c478bd9Sstevel@tonic-gate 	 * It is safe because close will wait for tl_close_ser to finish.
16407c478bd9Sstevel@tonic-gate 	 */
16417c478bd9Sstevel@tonic-gate 	tl_serializer_enter(tep, tl_close_ser, &tep->te_closemp);
16427c478bd9Sstevel@tonic-gate 
16437c478bd9Sstevel@tonic-gate 	/*
16447c478bd9Sstevel@tonic-gate 	 * Wait for the first phase of close to complete before qprocsoff().
16457c478bd9Sstevel@tonic-gate 	 */
16467c478bd9Sstevel@tonic-gate 	mutex_enter(&tep->te_closelock);
16477c478bd9Sstevel@tonic-gate 	while (tep->te_closewait)
16487c478bd9Sstevel@tonic-gate 		cv_wait(&tep->te_closecv, &tep->te_closelock);
16497c478bd9Sstevel@tonic-gate 	mutex_exit(&tep->te_closelock);
16507c478bd9Sstevel@tonic-gate 
16517c478bd9Sstevel@tonic-gate 	qprocsoff(rq);
16527c478bd9Sstevel@tonic-gate 
16537c478bd9Sstevel@tonic-gate 	if (tep->te_bufcid) {
16547c478bd9Sstevel@tonic-gate 		qunbufcall(rq, tep->te_bufcid);
16557c478bd9Sstevel@tonic-gate 		tep->te_bufcid = 0;
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 	if (tep->te_timoutid) {
16587c478bd9Sstevel@tonic-gate 		(void) quntimeout(rq, tep->te_timoutid);
16597c478bd9Sstevel@tonic-gate 		tep->te_timoutid = 0;
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 
16627c478bd9Sstevel@tonic-gate 	/*
16637c478bd9Sstevel@tonic-gate 	 * Finish close behind serializer.
16647c478bd9Sstevel@tonic-gate 	 *
16657c478bd9Sstevel@tonic-gate 	 * For a CLTS endpoint increase a refcount and continue close processing
16667c478bd9Sstevel@tonic-gate 	 * with serializer protection. This processing may happen asynchronously
16677c478bd9Sstevel@tonic-gate 	 * with the completion of tl_close().
16687c478bd9Sstevel@tonic-gate 	 *
16697c478bd9Sstevel@tonic-gate 	 * Fot a COTS endpoint wait before destroying tep since the serializer
16707c478bd9Sstevel@tonic-gate 	 * may go away together with tep and we need to destroy serializer
16717c478bd9Sstevel@tonic-gate 	 * outside of serializer context.
16727c478bd9Sstevel@tonic-gate 	 */
16737c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_closewait == 0);
16747c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep))
16757c478bd9Sstevel@tonic-gate 		tep->te_closewait = 1;
16767c478bd9Sstevel@tonic-gate 	else
16777c478bd9Sstevel@tonic-gate 		tl_refhold(tep);
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	tl_serializer_enter(tep, tl_close_finish_ser, &tep->te_closemp);
16807c478bd9Sstevel@tonic-gate 
16817c478bd9Sstevel@tonic-gate 	/*
16827c478bd9Sstevel@tonic-gate 	 * For connection-oriented transports wait for all serializer activity
16837c478bd9Sstevel@tonic-gate 	 * to settle down.
16847c478bd9Sstevel@tonic-gate 	 */
16857c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
16867c478bd9Sstevel@tonic-gate 		mutex_enter(&tep->te_closelock);
16877c478bd9Sstevel@tonic-gate 		while (tep->te_closewait)
16887c478bd9Sstevel@tonic-gate 			cv_wait(&tep->te_closecv, &tep->te_closelock);
16897c478bd9Sstevel@tonic-gate 		mutex_exit(&tep->te_closelock);
16907c478bd9Sstevel@tonic-gate 	}
16917c478bd9Sstevel@tonic-gate 
16927c478bd9Sstevel@tonic-gate 	crfree(tep->te_credp);
16937c478bd9Sstevel@tonic-gate 	tep->te_credp = NULL;
16947c478bd9Sstevel@tonic-gate 	tep->te_wq = NULL;
16957c478bd9Sstevel@tonic-gate 	tl_refrele(tep);
16967c478bd9Sstevel@tonic-gate 	/*
16977c478bd9Sstevel@tonic-gate 	 * tep is likely to be destroyed now, so can't reference it any more.
16987c478bd9Sstevel@tonic-gate 	 */
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	rq->q_ptr = wq->q_ptr = NULL;
17017c478bd9Sstevel@tonic-gate 	return (0);
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate /*
17057c478bd9Sstevel@tonic-gate  * First phase of close processing done behind the serializer.
17067c478bd9Sstevel@tonic-gate  *
17077c478bd9Sstevel@tonic-gate  * Do not drop the reference in the end - tl_close() wants this reference to
17087c478bd9Sstevel@tonic-gate  * stay.
17097c478bd9Sstevel@tonic-gate  */
17107c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
17117c478bd9Sstevel@tonic-gate static void
tl_close_ser(mblk_t * mp,tl_endpt_t * tep)17127c478bd9Sstevel@tonic-gate tl_close_ser(mblk_t *mp, tl_endpt_t *tep)
17137c478bd9Sstevel@tonic-gate {
17147c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_closing);
17157c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_closewait == 1);
17167c478bd9Sstevel@tonic-gate 	ASSERT(!(tep->te_flag & TL_CLOSE_SER));
17177c478bd9Sstevel@tonic-gate 
17187c478bd9Sstevel@tonic-gate 	tep->te_flag |= TL_CLOSE_SER;
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate 	/*
17217c478bd9Sstevel@tonic-gate 	 * Drain out all messages on queue except for TL_TICOTS where the
17227c478bd9Sstevel@tonic-gate 	 * abortive release semantics permit discarding of data on close
17237c478bd9Sstevel@tonic-gate 	 */
17247c478bd9Sstevel@tonic-gate 	if (tep->te_wq->q_first && (IS_CLTS(tep) || IS_COTSORD(tep))) {
17257c478bd9Sstevel@tonic-gate 		tl_wsrv_ser(NULL, tep);
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	/* Remove address from hash table. */
17297c478bd9Sstevel@tonic-gate 	tl_addr_unbind(tep);
17307c478bd9Sstevel@tonic-gate 	/*
17317c478bd9Sstevel@tonic-gate 	 * qprocsoff() gets confused when q->q_next is not NULL on the write
17327c478bd9Sstevel@tonic-gate 	 * queue of the driver, so clear these before qprocsoff() is called.
17337c478bd9Sstevel@tonic-gate 	 * Also clear q_next for the peer since this queue is going away.
17347c478bd9Sstevel@tonic-gate 	 */
17357c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep) && !IS_SOCKET(tep)) {
17367c478bd9Sstevel@tonic-gate 		tl_endpt_t *peer_tep = tep->te_conp;
17377c478bd9Sstevel@tonic-gate 
17387c478bd9Sstevel@tonic-gate 		tep->te_wq->q_next = NULL;
17397c478bd9Sstevel@tonic-gate 		if ((peer_tep != NULL) && !peer_tep->te_closing)
17407c478bd9Sstevel@tonic-gate 			peer_tep->te_wq->q_next = NULL;
17417c478bd9Sstevel@tonic-gate 	}
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	tep->te_rq = NULL;
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate 	/* wake up tl_close() */
17467c478bd9Sstevel@tonic-gate 	tl_closeok(tep);
17477c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate /*
17517c478bd9Sstevel@tonic-gate  * Second phase of tl_close(). Should wakeup tl_close() for COTS mode and drop
17527c478bd9Sstevel@tonic-gate  * the reference for CLTS.
17537c478bd9Sstevel@tonic-gate  *
17547c478bd9Sstevel@tonic-gate  * Called from serializer. Should drop reference count for CLTS only.
17557c478bd9Sstevel@tonic-gate  */
17567c478bd9Sstevel@tonic-gate /* ARGSUSED0 */
17577c478bd9Sstevel@tonic-gate static void
tl_close_finish_ser(mblk_t * mp,tl_endpt_t * tep)17587c478bd9Sstevel@tonic-gate tl_close_finish_ser(mblk_t *mp, tl_endpt_t *tep)
17597c478bd9Sstevel@tonic-gate {
17607c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_closing);
176156f33205SJonathan Adams 	IMPLY(IS_CLTS(tep), tep->te_closewait == 0);
176256f33205SJonathan Adams 	IMPLY(IS_COTS(tep), tep->te_closewait == 1);
17637c478bd9Sstevel@tonic-gate 
17647c478bd9Sstevel@tonic-gate 	tep->te_state = -1;	/* Uninitialized */
17657c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
17667c478bd9Sstevel@tonic-gate 		tl_co_unconnect(tep);
17677c478bd9Sstevel@tonic-gate 	} else {
17687c478bd9Sstevel@tonic-gate 		/* Connectionless specific cleanup */
17697c478bd9Sstevel@tonic-gate 		TL_REMOVE_PEER(tep->te_lastep);
17707c478bd9Sstevel@tonic-gate 		/*
17717c478bd9Sstevel@tonic-gate 		 * Backenable anybody that is flow controlled waiting for
17727c478bd9Sstevel@tonic-gate 		 * this endpoint.
17737c478bd9Sstevel@tonic-gate 		 */
17747c478bd9Sstevel@tonic-gate 		tl_cl_backenable(tep);
17757c478bd9Sstevel@tonic-gate 		if (tep->te_flowq != NULL) {
17767c478bd9Sstevel@tonic-gate 			list_remove(&(tep->te_flowq->te_flowlist), tep);
17777c478bd9Sstevel@tonic-gate 			tep->te_flowq = NULL;
17787c478bd9Sstevel@tonic-gate 		}
17797c478bd9Sstevel@tonic-gate 	}
17807c478bd9Sstevel@tonic-gate 
17817c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
17827c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep))
17837c478bd9Sstevel@tonic-gate 		tl_closeok(tep);
17847c478bd9Sstevel@tonic-gate 	else
17857c478bd9Sstevel@tonic-gate 		tl_refrele(tep);
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate /*
17897c478bd9Sstevel@tonic-gate  * STREAMS write-side put procedure.
17907c478bd9Sstevel@tonic-gate  * Enter serializer for most of the processing.
17917c478bd9Sstevel@tonic-gate  *
17927c478bd9Sstevel@tonic-gate  * The T_CONN_REQ is processed outside of serializer.
17937c478bd9Sstevel@tonic-gate  */
1794cb492d90SToomas Soome static int
tl_wput(queue_t * wq,mblk_t * mp)17957c478bd9Sstevel@tonic-gate tl_wput(queue_t *wq, mblk_t *mp)
17967c478bd9Sstevel@tonic-gate {
17977c478bd9Sstevel@tonic-gate 	tl_endpt_t		*tep = (tl_endpt_t *)wq->q_ptr;
17987c478bd9Sstevel@tonic-gate 	ssize_t			msz = MBLKL(mp);
17997c478bd9Sstevel@tonic-gate 	union T_primitives	*prim = (union T_primitives *)mp->b_rptr;
18007c478bd9Sstevel@tonic-gate 	tlproc_t		*tl_proc = NULL;
18017c478bd9Sstevel@tonic-gate 
18027c478bd9Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
18037c478bd9Sstevel@tonic-gate 	case M_DATA:
18047c478bd9Sstevel@tonic-gate 		/* Only valid for connection-oriented transports */
18057c478bd9Sstevel@tonic-gate 		if (IS_CLTS(tep)) {
18067c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
1807cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
1808fc80c0dfSnordmark 			    "tl_wput:M_DATA invalid for ticlts driver"));
18097c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EPROTO);
1810cb492d90SToomas Soome 			return (0);
18117c478bd9Sstevel@tonic-gate 		}
18127c478bd9Sstevel@tonic-gate 		tl_proc = tl_wput_data_ser;
18137c478bd9Sstevel@tonic-gate 		break;
18147c478bd9Sstevel@tonic-gate 
18157c478bd9Sstevel@tonic-gate 	case M_IOCTL:
18167c478bd9Sstevel@tonic-gate 		switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
18177c478bd9Sstevel@tonic-gate 		case TL_IOC_CREDOPT:
18187c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
18197c478bd9Sstevel@tonic-gate 		case TL_IOC_UCREDOPT:
18207c478bd9Sstevel@tonic-gate 			/*
18217c478bd9Sstevel@tonic-gate 			 * Serialize endpoint state change.
18227c478bd9Sstevel@tonic-gate 			 */
18237c478bd9Sstevel@tonic-gate 			tl_proc = tl_do_ioctl_ser;
18247c478bd9Sstevel@tonic-gate 			break;
18257c478bd9Sstevel@tonic-gate 
18267c478bd9Sstevel@tonic-gate 		default:
18277c478bd9Sstevel@tonic-gate 			miocnak(wq, mp, 0, EINVAL);
1828cb492d90SToomas Soome 			return (0);
18297c478bd9Sstevel@tonic-gate 		}
18307c478bd9Sstevel@tonic-gate 		break;
18317c478bd9Sstevel@tonic-gate 
18327c478bd9Sstevel@tonic-gate 	case M_FLUSH:
18337c478bd9Sstevel@tonic-gate 		/*
18347c478bd9Sstevel@tonic-gate 		 * do canonical M_FLUSH processing
18357c478bd9Sstevel@tonic-gate 		 */
18367c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
18377c478bd9Sstevel@tonic-gate 			flushq(wq, FLUSHALL);
18387c478bd9Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHW;
18397c478bd9Sstevel@tonic-gate 		}
18407c478bd9Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHR) {
18417c478bd9Sstevel@tonic-gate 			flushq(RD(wq), FLUSHALL);
18427c478bd9Sstevel@tonic-gate 			qreply(wq, mp);
18437c478bd9Sstevel@tonic-gate 		} else {
18447c478bd9Sstevel@tonic-gate 			freemsg(mp);
18457c478bd9Sstevel@tonic-gate 		}
1846cb492d90SToomas Soome 		return (0);
18477c478bd9Sstevel@tonic-gate 
18487c478bd9Sstevel@tonic-gate 	case M_PROTO:
18497c478bd9Sstevel@tonic-gate 		if (msz < sizeof (prim->type)) {
18507c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
1851cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
1852fc80c0dfSnordmark 			    "tl_wput:M_PROTO data too short"));
18537c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EPROTO);
1854cb492d90SToomas Soome 			return (0);
18557c478bd9Sstevel@tonic-gate 		}
18567c478bd9Sstevel@tonic-gate 		switch (prim->type) {
18577c478bd9Sstevel@tonic-gate 		case T_OPTMGMT_REQ:
18587c478bd9Sstevel@tonic-gate 		case T_SVR4_OPTMGMT_REQ:
18597c478bd9Sstevel@tonic-gate 			/*
18607c478bd9Sstevel@tonic-gate 			 * Process TPI option management requests immediately
18617c478bd9Sstevel@tonic-gate 			 * in put procedure regardless of in-order processing
18627c478bd9Sstevel@tonic-gate 			 * of already queued messages.
18637c478bd9Sstevel@tonic-gate 			 * (Note: This driver supports AF_UNIX socket
18647c478bd9Sstevel@tonic-gate 			 * implementation.  Unless we implement this processing,
18657c478bd9Sstevel@tonic-gate 			 * setsockopt() on socket endpoint will block on flow
18667c478bd9Sstevel@tonic-gate 			 * controlled endpoints which it should not. That is
18677c478bd9Sstevel@tonic-gate 			 * required for successful execution of VSU socket tests
18687c478bd9Sstevel@tonic-gate 			 * and is consistent with BSD socket behavior).
18697c478bd9Sstevel@tonic-gate 			 */
18707c478bd9Sstevel@tonic-gate 			tl_optmgmt(wq, mp);
1871cb492d90SToomas Soome 			return (0);
18727c478bd9Sstevel@tonic-gate 		case O_T_BIND_REQ:
18737c478bd9Sstevel@tonic-gate 		case T_BIND_REQ:
18747c478bd9Sstevel@tonic-gate 			tl_proc = tl_bind_ser;
18757c478bd9Sstevel@tonic-gate 			break;
18767c478bd9Sstevel@tonic-gate 		case T_CONN_REQ:
18777c478bd9Sstevel@tonic-gate 			if (IS_CLTS(tep)) {
18787c478bd9Sstevel@tonic-gate 				tl_merror(wq, mp, EPROTO);
1879cb492d90SToomas Soome 				return (0);
18807c478bd9Sstevel@tonic-gate 			}
18817c478bd9Sstevel@tonic-gate 			tl_conn_req(wq, mp);
1882cb492d90SToomas Soome 			return (0);
18837c478bd9Sstevel@tonic-gate 		case T_DATA_REQ:
18847c478bd9Sstevel@tonic-gate 		case T_OPTDATA_REQ:
18857c478bd9Sstevel@tonic-gate 		case T_EXDATA_REQ:
18867c478bd9Sstevel@tonic-gate 		case T_ORDREL_REQ:
18877c478bd9Sstevel@tonic-gate 			tl_proc = tl_putq_ser;
18887c478bd9Sstevel@tonic-gate 			break;
18897c478bd9Sstevel@tonic-gate 		case T_UNITDATA_REQ:
18907c478bd9Sstevel@tonic-gate 			if (IS_COTS(tep) ||
18917c478bd9Sstevel@tonic-gate 			    (msz < sizeof (struct T_unitdata_req))) {
18927c478bd9Sstevel@tonic-gate 				tl_merror(wq, mp, EPROTO);
1893cb492d90SToomas Soome 				return (0);
18947c478bd9Sstevel@tonic-gate 			}
18957c478bd9Sstevel@tonic-gate 			if ((tep->te_state == TS_IDLE) && !wq->q_first) {
18967c478bd9Sstevel@tonic-gate 				tl_proc = tl_unitdata_ser;
18977c478bd9Sstevel@tonic-gate 			} else {
18987c478bd9Sstevel@tonic-gate 				tl_proc = tl_putq_ser;
18997c478bd9Sstevel@tonic-gate 			}
19007c478bd9Sstevel@tonic-gate 			break;
19017c478bd9Sstevel@tonic-gate 		default:
19027c478bd9Sstevel@tonic-gate 			/*
19037c478bd9Sstevel@tonic-gate 			 * process in service procedure if message already
19047c478bd9Sstevel@tonic-gate 			 * queued (maintain in-order processing)
19057c478bd9Sstevel@tonic-gate 			 */
19067c478bd9Sstevel@tonic-gate 			if (wq->q_first != NULL) {
19077c478bd9Sstevel@tonic-gate 				tl_proc = tl_putq_ser;
19087c478bd9Sstevel@tonic-gate 			} else {
19097c478bd9Sstevel@tonic-gate 				tl_proc = tl_wput_ser;
19107c478bd9Sstevel@tonic-gate 			}
19117c478bd9Sstevel@tonic-gate 			break;
19127c478bd9Sstevel@tonic-gate 		}
19137c478bd9Sstevel@tonic-gate 		break;
19147c478bd9Sstevel@tonic-gate 
19157c478bd9Sstevel@tonic-gate 	case M_PCPROTO:
19167c478bd9Sstevel@tonic-gate 		/*
19177c478bd9Sstevel@tonic-gate 		 * Check that the message has enough data to figure out TPI
19187c478bd9Sstevel@tonic-gate 		 * primitive.
19197c478bd9Sstevel@tonic-gate 		 */
19207c478bd9Sstevel@tonic-gate 		if (msz < sizeof (prim->type)) {
19217c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
1922cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
1923fc80c0dfSnordmark 			    "tl_wput:M_PCROTO data too short"));
19247c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EPROTO);
1925cb492d90SToomas Soome 			return (0);
19267c478bd9Sstevel@tonic-gate 		}
19277c478bd9Sstevel@tonic-gate 		switch (prim->type) {
19287c478bd9Sstevel@tonic-gate 		case T_CAPABILITY_REQ:
19297c478bd9Sstevel@tonic-gate 			tl_capability_req(mp, tep);
1930cb492d90SToomas Soome 			return (0);
19317c478bd9Sstevel@tonic-gate 		case T_INFO_REQ:
19327c478bd9Sstevel@tonic-gate 			tl_proc = tl_info_req_ser;
19337c478bd9Sstevel@tonic-gate 			break;
1934b2d8fb89SDan Kruchinin 		case T_ADDR_REQ:
1935b2d8fb89SDan Kruchinin 			tl_proc = tl_addr_req_ser;
1936b2d8fb89SDan Kruchinin 			break;
1937b2d8fb89SDan Kruchinin 
19387c478bd9Sstevel@tonic-gate 		default:
19397c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
1940cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
1941fc80c0dfSnordmark 			    "tl_wput:unknown TPI msg primitive"));
19427c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EPROTO);
1943cb492d90SToomas Soome 			return (0);
19447c478bd9Sstevel@tonic-gate 		}
19457c478bd9Sstevel@tonic-gate 		break;
19467c478bd9Sstevel@tonic-gate 	default:
1947cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
1948fc80c0dfSnordmark 		    "tl_wput:default:unexpected Streams message"));
19497c478bd9Sstevel@tonic-gate 		freemsg(mp);
1950cb492d90SToomas Soome 		return (0);
19517c478bd9Sstevel@tonic-gate 	}
19527c478bd9Sstevel@tonic-gate 
19537c478bd9Sstevel@tonic-gate 	/*
19547c478bd9Sstevel@tonic-gate 	 * Continue processing via serializer.
19557c478bd9Sstevel@tonic-gate 	 */
19567c478bd9Sstevel@tonic-gate 	ASSERT(tl_proc != NULL);
19577c478bd9Sstevel@tonic-gate 	tl_refhold(tep);
19587c478bd9Sstevel@tonic-gate 	tl_serializer_enter(tep, tl_proc, mp);
1959cb492d90SToomas Soome 	return (0);
19607c478bd9Sstevel@tonic-gate }
19617c478bd9Sstevel@tonic-gate 
19627c478bd9Sstevel@tonic-gate /*
19637c478bd9Sstevel@tonic-gate  * Place message on the queue while preserving order.
19647c478bd9Sstevel@tonic-gate  */
19657c478bd9Sstevel@tonic-gate static void
tl_putq_ser(mblk_t * mp,tl_endpt_t * tep)19667c478bd9Sstevel@tonic-gate tl_putq_ser(mblk_t *mp, tl_endpt_t *tep)
19677c478bd9Sstevel@tonic-gate {
19687c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
19697c478bd9Sstevel@tonic-gate 		tl_wput_ser(mp, tep);
19707c478bd9Sstevel@tonic-gate 	} else {
19717c478bd9Sstevel@tonic-gate 		TL_PUTQ(tep, mp);
19727c478bd9Sstevel@tonic-gate 		tl_serializer_exit(tep);
19737c478bd9Sstevel@tonic-gate 		tl_refrele(tep);
19747c478bd9Sstevel@tonic-gate 	}
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate }
19777c478bd9Sstevel@tonic-gate 
19787c478bd9Sstevel@tonic-gate static void
tl_wput_common_ser(mblk_t * mp,tl_endpt_t * tep)19797c478bd9Sstevel@tonic-gate tl_wput_common_ser(mblk_t *mp, tl_endpt_t *tep)
19807c478bd9Sstevel@tonic-gate {
19817c478bd9Sstevel@tonic-gate 	ASSERT((DB_TYPE(mp) == M_DATA) || (DB_TYPE(mp) == M_PROTO));
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
19847c478bd9Sstevel@tonic-gate 	case M_DATA:
19857c478bd9Sstevel@tonic-gate 		tl_data(mp, tep);
19867c478bd9Sstevel@tonic-gate 		break;
19877c478bd9Sstevel@tonic-gate 	case M_PROTO:
19887c478bd9Sstevel@tonic-gate 		tl_do_proto(mp, tep);
19897c478bd9Sstevel@tonic-gate 		break;
19907c478bd9Sstevel@tonic-gate 	default:
19917c478bd9Sstevel@tonic-gate 		freemsg(mp);
19927c478bd9Sstevel@tonic-gate 		break;
19937c478bd9Sstevel@tonic-gate 	}
19947c478bd9Sstevel@tonic-gate }
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate /*
19977c478bd9Sstevel@tonic-gate  * Write side put procedure called from serializer.
19987c478bd9Sstevel@tonic-gate  */
19997c478bd9Sstevel@tonic-gate static void
tl_wput_ser(mblk_t * mp,tl_endpt_t * tep)20007c478bd9Sstevel@tonic-gate tl_wput_ser(mblk_t *mp, tl_endpt_t *tep)
20017c478bd9Sstevel@tonic-gate {
20027c478bd9Sstevel@tonic-gate 	tl_wput_common_ser(mp, tep);
20037c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
20047c478bd9Sstevel@tonic-gate 	tl_refrele(tep);
20057c478bd9Sstevel@tonic-gate }
20067c478bd9Sstevel@tonic-gate 
20077c478bd9Sstevel@tonic-gate /*
20087c478bd9Sstevel@tonic-gate  * M_DATA processing. Called from serializer.
20097c478bd9Sstevel@tonic-gate  */
20107c478bd9Sstevel@tonic-gate static void
tl_wput_data_ser(mblk_t * mp,tl_endpt_t * tep)20117c478bd9Sstevel@tonic-gate tl_wput_data_ser(mblk_t *mp, tl_endpt_t *tep)
20127c478bd9Sstevel@tonic-gate {
20137c478bd9Sstevel@tonic-gate 	tl_endpt_t	*peer_tep = tep->te_conp;
20147c478bd9Sstevel@tonic-gate 	queue_t		*peer_rq;
20157c478bd9Sstevel@tonic-gate 
20167c478bd9Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_DATA);
20177c478bd9Sstevel@tonic-gate 	ASSERT(IS_COTS(tep));
20187c478bd9Sstevel@tonic-gate 
201956f33205SJonathan Adams 	IMPLY(peer_tep, tep->te_serializer == peer_tep->te_serializer);
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 	/*
20227c478bd9Sstevel@tonic-gate 	 * fastpath for data. Ignore flow control if tep is closing.
20237c478bd9Sstevel@tonic-gate 	 */
20247c478bd9Sstevel@tonic-gate 	if ((peer_tep != NULL) &&
20257c478bd9Sstevel@tonic-gate 	    !peer_tep->te_closing &&
20267c478bd9Sstevel@tonic-gate 	    ((tep->te_state == TS_DATA_XFER) ||
2027fc80c0dfSnordmark 	    (tep->te_state == TS_WREQ_ORDREL)) &&
20287c478bd9Sstevel@tonic-gate 	    (tep->te_wq != NULL) &&
20297c478bd9Sstevel@tonic-gate 	    (tep->te_wq->q_first == NULL) &&
2030b8ffbd31SJohn Levon 	    (peer_tep->te_state == TS_DATA_XFER ||
2031b8ffbd31SJohn Levon 	    peer_tep->te_state == TS_WIND_ORDREL ||
2032b8ffbd31SJohn Levon 	    peer_tep->te_state == TS_WREQ_ORDREL) &&
20337c478bd9Sstevel@tonic-gate 	    ((peer_rq = peer_tep->te_rq) != NULL) &&
20347c478bd9Sstevel@tonic-gate 	    (canputnext(peer_rq) || tep->te_closing)) {
20357c478bd9Sstevel@tonic-gate 		putnext(peer_rq, mp);
20367c478bd9Sstevel@tonic-gate 	} else if (tep->te_closing) {
20377c478bd9Sstevel@tonic-gate 		/*
20387c478bd9Sstevel@tonic-gate 		 * It is possible that by the time we got here tep started to
20397c478bd9Sstevel@tonic-gate 		 * close. If the write queue is not empty, and the state is
20407c478bd9Sstevel@tonic-gate 		 * TS_DATA_XFER the data should be delivered in order, so we
20417c478bd9Sstevel@tonic-gate 		 * call putq() instead of freeing the data.
20427c478bd9Sstevel@tonic-gate 		 */
20437c478bd9Sstevel@tonic-gate 		if ((tep->te_wq != NULL) &&
20447c478bd9Sstevel@tonic-gate 		    ((tep->te_state == TS_DATA_XFER) ||
2045fc80c0dfSnordmark 		    (tep->te_state == TS_WREQ_ORDREL))) {
20467c478bd9Sstevel@tonic-gate 			TL_PUTQ(tep, mp);
20477c478bd9Sstevel@tonic-gate 		} else {
20487c478bd9Sstevel@tonic-gate 			freemsg(mp);
20497c478bd9Sstevel@tonic-gate 		}
20507c478bd9Sstevel@tonic-gate 	} else {
20517c478bd9Sstevel@tonic-gate 		TL_PUTQ(tep, mp);
20527c478bd9Sstevel@tonic-gate 	}
20537c478bd9Sstevel@tonic-gate 
20547c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
20557c478bd9Sstevel@tonic-gate 	tl_refrele(tep);
20567c478bd9Sstevel@tonic-gate }
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate /*
20597c478bd9Sstevel@tonic-gate  * Write side service routine.
20607c478bd9Sstevel@tonic-gate  *
20617c478bd9Sstevel@tonic-gate  * All actual processing happens within serializer which is entered
20627c478bd9Sstevel@tonic-gate  * synchronously. It is possible that by the time tl_wsrv() wakes up, some new
20637c478bd9Sstevel@tonic-gate  * messages that need processing may have arrived, so tl_wsrv repeats until
20647c478bd9Sstevel@tonic-gate  * queue is empty or te_nowsrv is set.
20657c478bd9Sstevel@tonic-gate  */
2066cb492d90SToomas Soome static int
tl_wsrv(queue_t * wq)20677c478bd9Sstevel@tonic-gate tl_wsrv(queue_t *wq)
20687c478bd9Sstevel@tonic-gate {
20697c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep = (tl_endpt_t *)wq->q_ptr;
20707c478bd9Sstevel@tonic-gate 
20717c478bd9Sstevel@tonic-gate 	while ((wq->q_first != NULL) && !tep->te_nowsrv) {
20727c478bd9Sstevel@tonic-gate 		mutex_enter(&tep->te_srv_lock);
20737c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_wsrv_active == B_FALSE);
20747c478bd9Sstevel@tonic-gate 		tep->te_wsrv_active = B_TRUE;
20757c478bd9Sstevel@tonic-gate 		mutex_exit(&tep->te_srv_lock);
20767c478bd9Sstevel@tonic-gate 
20777c478bd9Sstevel@tonic-gate 		tl_serializer_enter(tep, tl_wsrv_ser, &tep->te_wsrvmp);
20787c478bd9Sstevel@tonic-gate 
20797c478bd9Sstevel@tonic-gate 		/*
20807c478bd9Sstevel@tonic-gate 		 * Wait for serializer job to complete.
20817c478bd9Sstevel@tonic-gate 		 */
20827c478bd9Sstevel@tonic-gate 		mutex_enter(&tep->te_srv_lock);
20837c478bd9Sstevel@tonic-gate 		while (tep->te_wsrv_active) {
20847c478bd9Sstevel@tonic-gate 			cv_wait(&tep->te_srv_cv, &tep->te_srv_lock);
20857c478bd9Sstevel@tonic-gate 		}
20867c478bd9Sstevel@tonic-gate 		cv_signal(&tep->te_srv_cv);
20877c478bd9Sstevel@tonic-gate 		mutex_exit(&tep->te_srv_lock);
20887c478bd9Sstevel@tonic-gate 	}
2089cb492d90SToomas Soome 	return (0);
20907c478bd9Sstevel@tonic-gate }
20917c478bd9Sstevel@tonic-gate 
20927c478bd9Sstevel@tonic-gate /*
20937c478bd9Sstevel@tonic-gate  * Serialized write side processing of the STREAMS queue.
20947c478bd9Sstevel@tonic-gate  * May be called either from tl_wsrv() or from tl_close() in which case ser_mp
20957c478bd9Sstevel@tonic-gate  * is NULL.
20967c478bd9Sstevel@tonic-gate  */
20977c478bd9Sstevel@tonic-gate static void
tl_wsrv_ser(mblk_t * ser_mp,tl_endpt_t * tep)20987c478bd9Sstevel@tonic-gate tl_wsrv_ser(mblk_t *ser_mp, tl_endpt_t *tep)
20997c478bd9Sstevel@tonic-gate {
21007c478bd9Sstevel@tonic-gate 	mblk_t *mp;
21017c478bd9Sstevel@tonic-gate 	queue_t *wq = tep->te_wq;
21027c478bd9Sstevel@tonic-gate 
21037c478bd9Sstevel@tonic-gate 	ASSERT(wq != NULL);
21047c478bd9Sstevel@tonic-gate 	while (!tep->te_nowsrv && (mp = getq(wq)) != NULL) {
21057c478bd9Sstevel@tonic-gate 		tl_wput_common_ser(mp, tep);
21067c478bd9Sstevel@tonic-gate 	}
21077c478bd9Sstevel@tonic-gate 
21087c478bd9Sstevel@tonic-gate 	/*
21097c478bd9Sstevel@tonic-gate 	 * Wakeup service routine unless called from close.
21107c478bd9Sstevel@tonic-gate 	 * If ser_mp is specified, the caller is tl_wsrv().
21117c478bd9Sstevel@tonic-gate 	 * Otherwise, the caller is tl_close_ser(). Since tl_close_ser() doesn't
21127c478bd9Sstevel@tonic-gate 	 * call tl_serializer_enter() before calling tl_wsrv_ser(), there should
21137c478bd9Sstevel@tonic-gate 	 * be no matching tl_serializer_exit() in this case.
21147c478bd9Sstevel@tonic-gate 	 * Also, there is no need to wakeup anyone since tl_close_ser() is not
21157c478bd9Sstevel@tonic-gate 	 * waiting on te_srv_cv.
21167c478bd9Sstevel@tonic-gate 	 */
21177c478bd9Sstevel@tonic-gate 	if (ser_mp != NULL) {
21187c478bd9Sstevel@tonic-gate 		/*
21197c478bd9Sstevel@tonic-gate 		 * We are called from tl_wsrv.
21207c478bd9Sstevel@tonic-gate 		 */
21217c478bd9Sstevel@tonic-gate 		mutex_enter(&tep->te_srv_lock);
21227c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_wsrv_active);
21237c478bd9Sstevel@tonic-gate 		tep->te_wsrv_active = B_FALSE;
21247c478bd9Sstevel@tonic-gate 		cv_signal(&tep->te_srv_cv);
21257c478bd9Sstevel@tonic-gate 		mutex_exit(&tep->te_srv_lock);
21267c478bd9Sstevel@tonic-gate 		tl_serializer_exit(tep);
21277c478bd9Sstevel@tonic-gate 	}
21287c478bd9Sstevel@tonic-gate }
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate /*
21317c478bd9Sstevel@tonic-gate  * Called when the stream is backenabled. Enter serializer and qenable everyone
21327c478bd9Sstevel@tonic-gate  * flow controlled by tep.
21337c478bd9Sstevel@tonic-gate  *
21347c478bd9Sstevel@tonic-gate  * NOTE: The service routine should enter serializer synchronously. Otherwise it
21357c478bd9Sstevel@tonic-gate  * is possible that two instances of tl_rsrv will be running reusing the same
21367c478bd9Sstevel@tonic-gate  * rsrv mblk.
21377c478bd9Sstevel@tonic-gate  */
2138cb492d90SToomas Soome static int
tl_rsrv(queue_t * rq)21397c478bd9Sstevel@tonic-gate tl_rsrv(queue_t *rq)
21407c478bd9Sstevel@tonic-gate {
21417c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep = (tl_endpt_t *)rq->q_ptr;
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate 	ASSERT(rq->q_first == NULL);
21447c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_rsrv_active == 0);
21457c478bd9Sstevel@tonic-gate 
21467c478bd9Sstevel@tonic-gate 	tep->te_rsrv_active = B_TRUE;
21477c478bd9Sstevel@tonic-gate 	tl_serializer_enter(tep, tl_rsrv_ser, &tep->te_rsrvmp);
21487c478bd9Sstevel@tonic-gate 	/*
21497c478bd9Sstevel@tonic-gate 	 * Wait for serializer job to complete.
21507c478bd9Sstevel@tonic-gate 	 */
21517c478bd9Sstevel@tonic-gate 	mutex_enter(&tep->te_srv_lock);
21527c478bd9Sstevel@tonic-gate 	while (tep->te_rsrv_active) {
21537c478bd9Sstevel@tonic-gate 		cv_wait(&tep->te_srv_cv, &tep->te_srv_lock);
21547c478bd9Sstevel@tonic-gate 	}
21557c478bd9Sstevel@tonic-gate 	cv_signal(&tep->te_srv_cv);
21567c478bd9Sstevel@tonic-gate 	mutex_exit(&tep->te_srv_lock);
2157cb492d90SToomas Soome 	return (0);
21587c478bd9Sstevel@tonic-gate }
21597c478bd9Sstevel@tonic-gate 
21607c478bd9Sstevel@tonic-gate /* ARGSUSED */
21617c478bd9Sstevel@tonic-gate static void
tl_rsrv_ser(mblk_t * mp,tl_endpt_t * tep)21627c478bd9Sstevel@tonic-gate tl_rsrv_ser(mblk_t *mp, tl_endpt_t *tep)
21637c478bd9Sstevel@tonic-gate {
21647c478bd9Sstevel@tonic-gate 	tl_endpt_t *peer_tep;
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate 	if (IS_CLTS(tep) && tep->te_state == TS_IDLE) {
21677c478bd9Sstevel@tonic-gate 		tl_cl_backenable(tep);
21687c478bd9Sstevel@tonic-gate 	} else if (
2169fc80c0dfSnordmark 	    IS_COTS(tep) &&
2170fc80c0dfSnordmark 	    ((peer_tep = tep->te_conp) != NULL) &&
2171fc80c0dfSnordmark 	    !peer_tep->te_closing &&
2172fc80c0dfSnordmark 	    ((tep->te_state == TS_DATA_XFER) ||
2173fc80c0dfSnordmark 	    (tep->te_state == TS_WIND_ORDREL)||
2174fc80c0dfSnordmark 	    (tep->te_state == TS_WREQ_ORDREL))) {
21757c478bd9Sstevel@tonic-gate 		TL_QENABLE(peer_tep);
21767c478bd9Sstevel@tonic-gate 	}
21777c478bd9Sstevel@tonic-gate 
21787c478bd9Sstevel@tonic-gate 	/*
21797c478bd9Sstevel@tonic-gate 	 * Wakeup read side service routine.
21807c478bd9Sstevel@tonic-gate 	 */
21817c478bd9Sstevel@tonic-gate 	mutex_enter(&tep->te_srv_lock);
21827c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_rsrv_active);
21837c478bd9Sstevel@tonic-gate 	tep->te_rsrv_active = B_FALSE;
21847c478bd9Sstevel@tonic-gate 	cv_signal(&tep->te_srv_cv);
21857c478bd9Sstevel@tonic-gate 	mutex_exit(&tep->te_srv_lock);
21867c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
21877c478bd9Sstevel@tonic-gate }
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate /*
21907c478bd9Sstevel@tonic-gate  * process M_PROTO messages. Always called from serializer.
21917c478bd9Sstevel@tonic-gate  */
21927c478bd9Sstevel@tonic-gate static void
tl_do_proto(mblk_t * mp,tl_endpt_t * tep)21937c478bd9Sstevel@tonic-gate tl_do_proto(mblk_t *mp, tl_endpt_t *tep)
21947c478bd9Sstevel@tonic-gate {
21957c478bd9Sstevel@tonic-gate 	ssize_t			msz = MBLKL(mp);
21967c478bd9Sstevel@tonic-gate 	union T_primitives	*prim = (union T_primitives *)mp->b_rptr;
21977c478bd9Sstevel@tonic-gate 
21987c478bd9Sstevel@tonic-gate 	/* Message size was validated by tl_wput(). */
21997c478bd9Sstevel@tonic-gate 	ASSERT(msz >= sizeof (prim->type));
22007c478bd9Sstevel@tonic-gate 
22017c478bd9Sstevel@tonic-gate 	switch (prim->type) {
22027c478bd9Sstevel@tonic-gate 	case T_UNBIND_REQ:
22037c478bd9Sstevel@tonic-gate 		tl_unbind(mp, tep);
22047c478bd9Sstevel@tonic-gate 		break;
22057c478bd9Sstevel@tonic-gate 
22067c478bd9Sstevel@tonic-gate 	case T_ADDR_REQ:
22077c478bd9Sstevel@tonic-gate 		tl_addr_req(mp, tep);
22087c478bd9Sstevel@tonic-gate 		break;
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	case O_T_CONN_RES:
22117c478bd9Sstevel@tonic-gate 	case T_CONN_RES:
22127c478bd9Sstevel@tonic-gate 		if (IS_CLTS(tep)) {
22137c478bd9Sstevel@tonic-gate 			tl_merror(tep->te_wq, mp, EPROTO);
22147c478bd9Sstevel@tonic-gate 			break;
22157c478bd9Sstevel@tonic-gate 		}
22167c478bd9Sstevel@tonic-gate 		tl_conn_res(mp, tep);
22177c478bd9Sstevel@tonic-gate 		break;
22187c478bd9Sstevel@tonic-gate 
22197c478bd9Sstevel@tonic-gate 	case T_DISCON_REQ:
22207c478bd9Sstevel@tonic-gate 		if (IS_CLTS(tep)) {
22217c478bd9Sstevel@tonic-gate 			tl_merror(tep->te_wq, mp, EPROTO);
22227c478bd9Sstevel@tonic-gate 			break;
22237c478bd9Sstevel@tonic-gate 		}
22247c478bd9Sstevel@tonic-gate 		tl_discon_req(mp, tep);
22257c478bd9Sstevel@tonic-gate 		break;
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 	case T_DATA_REQ:
22287c478bd9Sstevel@tonic-gate 		if (IS_CLTS(tep)) {
22297c478bd9Sstevel@tonic-gate 			tl_merror(tep->te_wq, mp, EPROTO);
22307c478bd9Sstevel@tonic-gate 			break;
22317c478bd9Sstevel@tonic-gate 		}
22327c478bd9Sstevel@tonic-gate 		tl_data(mp, tep);
22337c478bd9Sstevel@tonic-gate 		break;
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 	case T_OPTDATA_REQ:
22367c478bd9Sstevel@tonic-gate 		if (IS_CLTS(tep)) {
22377c478bd9Sstevel@tonic-gate 			tl_merror(tep->te_wq, mp, EPROTO);
22387c478bd9Sstevel@tonic-gate 			break;
22397c478bd9Sstevel@tonic-gate 		}
22407c478bd9Sstevel@tonic-gate 		tl_data(mp, tep);
22417c478bd9Sstevel@tonic-gate 		break;
22427c478bd9Sstevel@tonic-gate 
22437c478bd9Sstevel@tonic-gate 	case T_EXDATA_REQ:
22447c478bd9Sstevel@tonic-gate 		if (IS_CLTS(tep)) {
22457c478bd9Sstevel@tonic-gate 			tl_merror(tep->te_wq, mp, EPROTO);
22467c478bd9Sstevel@tonic-gate 			break;
22477c478bd9Sstevel@tonic-gate 		}
22487c478bd9Sstevel@tonic-gate 		tl_exdata(mp, tep);
22497c478bd9Sstevel@tonic-gate 		break;
22507c478bd9Sstevel@tonic-gate 
22517c478bd9Sstevel@tonic-gate 	case T_ORDREL_REQ:
2252cb492d90SToomas Soome 		if (!IS_COTSORD(tep)) {
22537c478bd9Sstevel@tonic-gate 			tl_merror(tep->te_wq, mp, EPROTO);
22547c478bd9Sstevel@tonic-gate 			break;
22557c478bd9Sstevel@tonic-gate 		}
22567c478bd9Sstevel@tonic-gate 		tl_ordrel(mp, tep);
22577c478bd9Sstevel@tonic-gate 		break;
22587c478bd9Sstevel@tonic-gate 
22597c478bd9Sstevel@tonic-gate 	case T_UNITDATA_REQ:
22607c478bd9Sstevel@tonic-gate 		if (IS_COTS(tep)) {
22617c478bd9Sstevel@tonic-gate 			tl_merror(tep->te_wq, mp, EPROTO);
22627c478bd9Sstevel@tonic-gate 			break;
22637c478bd9Sstevel@tonic-gate 		}
22647c478bd9Sstevel@tonic-gate 		tl_unitdata(mp, tep);
22657c478bd9Sstevel@tonic-gate 		break;
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	default:
22687c478bd9Sstevel@tonic-gate 		tl_merror(tep->te_wq, mp, EPROTO);
22697c478bd9Sstevel@tonic-gate 		break;
22707c478bd9Sstevel@tonic-gate 	}
22717c478bd9Sstevel@tonic-gate }
22727c478bd9Sstevel@tonic-gate 
22737c478bd9Sstevel@tonic-gate /*
22747c478bd9Sstevel@tonic-gate  * Process ioctl from serializer.
22757c478bd9Sstevel@tonic-gate  * This is a wrapper around tl_do_ioctl().
22767c478bd9Sstevel@tonic-gate  */
22777c478bd9Sstevel@tonic-gate static void
tl_do_ioctl_ser(mblk_t * mp,tl_endpt_t * tep)22787c478bd9Sstevel@tonic-gate tl_do_ioctl_ser(mblk_t *mp, tl_endpt_t *tep)
22797c478bd9Sstevel@tonic-gate {
2280cb492d90SToomas Soome 	if (!tep->te_closing)
22817c478bd9Sstevel@tonic-gate 		tl_do_ioctl(mp, tep);
22827c478bd9Sstevel@tonic-gate 	else
22837c478bd9Sstevel@tonic-gate 		freemsg(mp);
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
22867c478bd9Sstevel@tonic-gate 	tl_refrele(tep);
22877c478bd9Sstevel@tonic-gate }
22887c478bd9Sstevel@tonic-gate 
22897c478bd9Sstevel@tonic-gate static void
tl_do_ioctl(mblk_t * mp,tl_endpt_t * tep)22907c478bd9Sstevel@tonic-gate tl_do_ioctl(mblk_t *mp, tl_endpt_t *tep)
22917c478bd9Sstevel@tonic-gate {
22927c478bd9Sstevel@tonic-gate 	struct iocblk *iocbp = (struct iocblk *)mp->b_rptr;
22937c478bd9Sstevel@tonic-gate 	int cmd = iocbp->ioc_cmd;
22947c478bd9Sstevel@tonic-gate 	queue_t *wq = tep->te_wq;
22957c478bd9Sstevel@tonic-gate 	int error;
22967c478bd9Sstevel@tonic-gate 	int thisopt, otheropt;
22977c478bd9Sstevel@tonic-gate 
22987c478bd9Sstevel@tonic-gate 	ASSERT((cmd == TL_IOC_CREDOPT) || (cmd == TL_IOC_UCREDOPT));
22997c478bd9Sstevel@tonic-gate 
23007c478bd9Sstevel@tonic-gate 	switch (cmd) {
23017c478bd9Sstevel@tonic-gate 	case TL_IOC_CREDOPT:
23027c478bd9Sstevel@tonic-gate 		if (cmd == TL_IOC_CREDOPT) {
23037c478bd9Sstevel@tonic-gate 			thisopt = TL_SETCRED;
23047c478bd9Sstevel@tonic-gate 			otheropt = TL_SETUCRED;
23057c478bd9Sstevel@tonic-gate 		} else {
23067c478bd9Sstevel@tonic-gate 			/* FALLTHROUGH */
23077c478bd9Sstevel@tonic-gate 	case TL_IOC_UCREDOPT:
23087c478bd9Sstevel@tonic-gate 			thisopt = TL_SETUCRED;
23097c478bd9Sstevel@tonic-gate 			otheropt = TL_SETCRED;
23107c478bd9Sstevel@tonic-gate 		}
23117c478bd9Sstevel@tonic-gate 		/*
23127c478bd9Sstevel@tonic-gate 		 * The credentials passing does not apply to sockets.
23137c478bd9Sstevel@tonic-gate 		 * Only one of the cred options can be set at a given time.
23147c478bd9Sstevel@tonic-gate 		 */
23157c478bd9Sstevel@tonic-gate 		if (IS_SOCKET(tep) || (tep->te_flag & otheropt)) {
23167c478bd9Sstevel@tonic-gate 			miocnak(wq, mp, 0, EINVAL);
23177c478bd9Sstevel@tonic-gate 			return;
23187c478bd9Sstevel@tonic-gate 		}
23197c478bd9Sstevel@tonic-gate 
23207c478bd9Sstevel@tonic-gate 		/*
23217c478bd9Sstevel@tonic-gate 		 * Turn on generation of credential options for
23227c478bd9Sstevel@tonic-gate 		 * T_conn_req, T_conn_con, T_unidata_ind.
23237c478bd9Sstevel@tonic-gate 		 */
23247c478bd9Sstevel@tonic-gate 		error = miocpullup(mp, sizeof (uint32_t));
23257c478bd9Sstevel@tonic-gate 		if (error != 0) {
23267c478bd9Sstevel@tonic-gate 			miocnak(wq, mp, 0, error);
23277c478bd9Sstevel@tonic-gate 			return;
23287c478bd9Sstevel@tonic-gate 		}
23297c478bd9Sstevel@tonic-gate 		if (!IS_P2ALIGNED(mp->b_cont->b_rptr, sizeof (uint32_t))) {
23307c478bd9Sstevel@tonic-gate 			miocnak(wq, mp, 0, EINVAL);
23317c478bd9Sstevel@tonic-gate 			return;
23327c478bd9Sstevel@tonic-gate 		}
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 		if (*(uint32_t *)mp->b_cont->b_rptr)
23357c478bd9Sstevel@tonic-gate 			tep->te_flag |= thisopt;
23367c478bd9Sstevel@tonic-gate 		else
23377c478bd9Sstevel@tonic-gate 			tep->te_flag &= ~thisopt;
23387c478bd9Sstevel@tonic-gate 
23397c478bd9Sstevel@tonic-gate 		miocack(wq, mp, 0, 0);
23407c478bd9Sstevel@tonic-gate 		break;
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	default:
23437c478bd9Sstevel@tonic-gate 		/* Should not be here */
23447c478bd9Sstevel@tonic-gate 		miocnak(wq, mp, 0, EINVAL);
23457c478bd9Sstevel@tonic-gate 		break;
23467c478bd9Sstevel@tonic-gate 	}
23477c478bd9Sstevel@tonic-gate }
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate 
23507c478bd9Sstevel@tonic-gate /*
23517c478bd9Sstevel@tonic-gate  * send T_ERROR_ACK
23527c478bd9Sstevel@tonic-gate  * Note: assumes enough memory or caller passed big enough mp
23537c478bd9Sstevel@tonic-gate  *	- no recovery from allocb failures
23547c478bd9Sstevel@tonic-gate  */
23557c478bd9Sstevel@tonic-gate 
23567c478bd9Sstevel@tonic-gate static void
tl_error_ack(queue_t * wq,mblk_t * mp,t_scalar_t tli_err,t_scalar_t unix_err,t_scalar_t type)23577c478bd9Sstevel@tonic-gate tl_error_ack(queue_t *wq, mblk_t *mp, t_scalar_t tli_err,
23587c478bd9Sstevel@tonic-gate     t_scalar_t unix_err, t_scalar_t type)
23597c478bd9Sstevel@tonic-gate {
23607c478bd9Sstevel@tonic-gate 	struct T_error_ack *err_ack;
23617c478bd9Sstevel@tonic-gate 	mblk_t *ackmp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
23627c478bd9Sstevel@tonic-gate 	    M_PCPROTO, T_ERROR_ACK);
23637c478bd9Sstevel@tonic-gate 
23647c478bd9Sstevel@tonic-gate 	if (ackmp == NULL) {
2365cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, 0, 1, SL_TRACE | SL_ERROR,
2366fc80c0dfSnordmark 		    "tl_error_ack:out of mblk memory"));
23677c478bd9Sstevel@tonic-gate 		tl_merror(wq, NULL, ENOSR);
23687c478bd9Sstevel@tonic-gate 		return;
23697c478bd9Sstevel@tonic-gate 	}
23707c478bd9Sstevel@tonic-gate 	err_ack = (struct T_error_ack *)ackmp->b_rptr;
23717c478bd9Sstevel@tonic-gate 	err_ack->ERROR_prim = type;
23727c478bd9Sstevel@tonic-gate 	err_ack->TLI_error = tli_err;
23737c478bd9Sstevel@tonic-gate 	err_ack->UNIX_error = unix_err;
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate 	/*
23767c478bd9Sstevel@tonic-gate 	 * send error ack message
23777c478bd9Sstevel@tonic-gate 	 */
23787c478bd9Sstevel@tonic-gate 	qreply(wq, ackmp);
23797c478bd9Sstevel@tonic-gate }
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate 
23827c478bd9Sstevel@tonic-gate 
23837c478bd9Sstevel@tonic-gate /*
23847c478bd9Sstevel@tonic-gate  * send T_OK_ACK
23857c478bd9Sstevel@tonic-gate  * Note: assumes enough memory or caller passed big enough mp
23867c478bd9Sstevel@tonic-gate  *	- no recovery from allocb failures
23877c478bd9Sstevel@tonic-gate  */
23887c478bd9Sstevel@tonic-gate static void
tl_ok_ack(queue_t * wq,mblk_t * mp,t_scalar_t type)23897c478bd9Sstevel@tonic-gate tl_ok_ack(queue_t *wq, mblk_t *mp, t_scalar_t type)
23907c478bd9Sstevel@tonic-gate {
23917c478bd9Sstevel@tonic-gate 	struct T_ok_ack *ok_ack;
23927c478bd9Sstevel@tonic-gate 	mblk_t *ackmp = tpi_ack_alloc(mp, sizeof (struct T_ok_ack),
23937c478bd9Sstevel@tonic-gate 	    M_PCPROTO, T_OK_ACK);
23947c478bd9Sstevel@tonic-gate 
23957c478bd9Sstevel@tonic-gate 	if (ackmp == NULL) {
23967c478bd9Sstevel@tonic-gate 		tl_merror(wq, NULL, ENOMEM);
23977c478bd9Sstevel@tonic-gate 		return;
23987c478bd9Sstevel@tonic-gate 	}
23997c478bd9Sstevel@tonic-gate 
24007c478bd9Sstevel@tonic-gate 	ok_ack = (struct T_ok_ack *)ackmp->b_rptr;
24017c478bd9Sstevel@tonic-gate 	ok_ack->CORRECT_prim = type;
24027c478bd9Sstevel@tonic-gate 
24037c478bd9Sstevel@tonic-gate 	(void) qreply(wq, ackmp);
24047c478bd9Sstevel@tonic-gate }
24057c478bd9Sstevel@tonic-gate 
24067c478bd9Sstevel@tonic-gate /*
24077c478bd9Sstevel@tonic-gate  * Process T_BIND_REQ and O_T_BIND_REQ from serializer.
24087c478bd9Sstevel@tonic-gate  * This is a wrapper around tl_bind().
24097c478bd9Sstevel@tonic-gate  */
24107c478bd9Sstevel@tonic-gate static void
tl_bind_ser(mblk_t * mp,tl_endpt_t * tep)24117c478bd9Sstevel@tonic-gate tl_bind_ser(mblk_t *mp, tl_endpt_t *tep)
24127c478bd9Sstevel@tonic-gate {
2413cb492d90SToomas Soome 	if (!tep->te_closing)
24147c478bd9Sstevel@tonic-gate 		tl_bind(mp, tep);
24157c478bd9Sstevel@tonic-gate 	else
24167c478bd9Sstevel@tonic-gate 		freemsg(mp);
24177c478bd9Sstevel@tonic-gate 
24187c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
24197c478bd9Sstevel@tonic-gate 	tl_refrele(tep);
24207c478bd9Sstevel@tonic-gate }
24217c478bd9Sstevel@tonic-gate 
24227c478bd9Sstevel@tonic-gate /*
24237c478bd9Sstevel@tonic-gate  * Process T_BIND_REQ and O_T_BIND_REQ TPI requests.
24247c478bd9Sstevel@tonic-gate  * Assumes that the endpoint is in the unbound.
24257c478bd9Sstevel@tonic-gate  */
24267c478bd9Sstevel@tonic-gate static void
tl_bind(mblk_t * mp,tl_endpt_t * tep)24277c478bd9Sstevel@tonic-gate tl_bind(mblk_t *mp, tl_endpt_t *tep)
24287c478bd9Sstevel@tonic-gate {
24297c478bd9Sstevel@tonic-gate 	queue_t			*wq = tep->te_wq;
24307c478bd9Sstevel@tonic-gate 	struct T_bind_ack	*b_ack;
24317c478bd9Sstevel@tonic-gate 	struct T_bind_req	*bind = (struct T_bind_req *)mp->b_rptr;
24327c478bd9Sstevel@tonic-gate 	mblk_t			*ackmp, *bamp;
24337c478bd9Sstevel@tonic-gate 	soux_addr_t		ux_addr;
24347c478bd9Sstevel@tonic-gate 	t_uscalar_t		qlen = 0;
24357c478bd9Sstevel@tonic-gate 	t_scalar_t		alen, aoff;
24367c478bd9Sstevel@tonic-gate 	tl_addr_t		addr_req;
24377c478bd9Sstevel@tonic-gate 	void			*addr_startp;
24387c478bd9Sstevel@tonic-gate 	ssize_t			msz = MBLKL(mp), basize;
24397c478bd9Sstevel@tonic-gate 	t_scalar_t		tli_err = 0, unix_err = 0;
24407c478bd9Sstevel@tonic-gate 	t_scalar_t		save_prim_type = bind->PRIM_type;
24417c478bd9Sstevel@tonic-gate 	t_scalar_t		save_state = tep->te_state;
24427c478bd9Sstevel@tonic-gate 
24437c478bd9Sstevel@tonic-gate 	if (tep->te_state != TS_UNBND) {
24447c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
2445cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
2446fc80c0dfSnordmark 		    "tl_wput:bind_request:out of state, state=%d",
2447fc80c0dfSnordmark 		    tep->te_state));
24487c478bd9Sstevel@tonic-gate 		tli_err = TOUTSTATE;
24497c478bd9Sstevel@tonic-gate 		goto error;
24507c478bd9Sstevel@tonic-gate 	}
24517c478bd9Sstevel@tonic-gate 
24527c478bd9Sstevel@tonic-gate 	if (msz < sizeof (struct T_bind_req)) {
2453cb492d90SToomas Soome 		tli_err = TSYSERR;
2454cb492d90SToomas Soome 		unix_err = EINVAL;
24557c478bd9Sstevel@tonic-gate 		goto error;
24567c478bd9Sstevel@tonic-gate 	}
24577c478bd9Sstevel@tonic-gate 
2458b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_BIND_REQ][tep->te_state];
24597c478bd9Sstevel@tonic-gate 
24607c478bd9Sstevel@tonic-gate 	ASSERT((bind->PRIM_type == O_T_BIND_REQ) ||
24617c478bd9Sstevel@tonic-gate 	    (bind->PRIM_type == T_BIND_REQ));
24627c478bd9Sstevel@tonic-gate 
24637c478bd9Sstevel@tonic-gate 	alen = bind->ADDR_length;
24647c478bd9Sstevel@tonic-gate 	aoff = bind->ADDR_offset;
24657c478bd9Sstevel@tonic-gate 
24667c478bd9Sstevel@tonic-gate 	/* negotiate max conn req pending */
24677c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
24687c478bd9Sstevel@tonic-gate 		qlen = bind->CONIND_number;
2469d87b1facSakolb 		if (qlen > tl_maxqlen)
2470d87b1facSakolb 			qlen = tl_maxqlen;
24717c478bd9Sstevel@tonic-gate 	}
24727c478bd9Sstevel@tonic-gate 
24737c478bd9Sstevel@tonic-gate 	/*
24747c478bd9Sstevel@tonic-gate 	 * Reserve hash handle. It can only be NULL if the endpoint is unbound
24757c478bd9Sstevel@tonic-gate 	 * and bound again.
24767c478bd9Sstevel@tonic-gate 	 */
24777c478bd9Sstevel@tonic-gate 	if ((tep->te_hash_hndl == NULL) &&
24787c478bd9Sstevel@tonic-gate 	    ((tep->te_flag & TL_ADDRHASHED) == 0) &&
24797c478bd9Sstevel@tonic-gate 	    mod_hash_reserve_nosleep(tep->te_addrhash,
2480fc80c0dfSnordmark 	    &tep->te_hash_hndl) != 0) {
2481cb492d90SToomas Soome 		tli_err = TSYSERR;
2482cb492d90SToomas Soome 		unix_err = ENOSR;
24837c478bd9Sstevel@tonic-gate 		goto error;
24847c478bd9Sstevel@tonic-gate 	}
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	/*
24877c478bd9Sstevel@tonic-gate 	 * Verify address correctness.
24887c478bd9Sstevel@tonic-gate 	 */
24897c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(tep)) {
24907c478bd9Sstevel@tonic-gate 		ASSERT(bind->PRIM_type == O_T_BIND_REQ);
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 		if ((alen != TL_SOUX_ADDRLEN) ||
24937c478bd9Sstevel@tonic-gate 		    (aoff < 0) ||
24947c478bd9Sstevel@tonic-gate 		    (aoff + alen > msz)) {
24957c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2496cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
2497fc80c0dfSnordmark 			    "tl_bind: invalid socket addr"));
2498b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
2499cb492d90SToomas Soome 			tli_err = TSYSERR;
2500cb492d90SToomas Soome 			unix_err = EINVAL;
25017c478bd9Sstevel@tonic-gate 			goto error;
25027c478bd9Sstevel@tonic-gate 		}
25037c478bd9Sstevel@tonic-gate 		/* Copy address from message to local buffer. */
25047c478bd9Sstevel@tonic-gate 		bcopy(mp->b_rptr + aoff, &ux_addr, sizeof (ux_addr));
25057c478bd9Sstevel@tonic-gate 		/*
25067c478bd9Sstevel@tonic-gate 		 * Check that we got correct address from sockets
25077c478bd9Sstevel@tonic-gate 		 */
25087c478bd9Sstevel@tonic-gate 		if ((ux_addr.soua_magic != SOU_MAGIC_EXPLICIT) &&
25097c478bd9Sstevel@tonic-gate 		    (ux_addr.soua_magic != SOU_MAGIC_IMPLICIT)) {
25107c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2511cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
2512fc80c0dfSnordmark 			    "tl_bind: invalid socket magic"));
2513b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
2514cb492d90SToomas Soome 			tli_err = TSYSERR;
2515cb492d90SToomas Soome 			unix_err = EINVAL;
25167c478bd9Sstevel@tonic-gate 			goto error;
25177c478bd9Sstevel@tonic-gate 		}
25187c478bd9Sstevel@tonic-gate 		if ((ux_addr.soua_magic == SOU_MAGIC_IMPLICIT) &&
25197c478bd9Sstevel@tonic-gate 		    (ux_addr.soua_vp != NULL)) {
25207c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2521cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
2522fc80c0dfSnordmark 			    "tl_bind: implicit addr non-empty"));
2523b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
2524cb492d90SToomas Soome 			tli_err = TSYSERR;
2525cb492d90SToomas Soome 			unix_err = EINVAL;
25267c478bd9Sstevel@tonic-gate 			goto error;
25277c478bd9Sstevel@tonic-gate 		}
25287c478bd9Sstevel@tonic-gate 		if ((ux_addr.soua_magic == SOU_MAGIC_EXPLICIT) &&
25297c478bd9Sstevel@tonic-gate 		    (ux_addr.soua_vp == NULL)) {
25307c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2531cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
2532fc80c0dfSnordmark 			    "tl_bind: explicit addr empty"));
2533b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
2534cb492d90SToomas Soome 			tli_err = TSYSERR;
2535cb492d90SToomas Soome 			unix_err = EINVAL;
25367c478bd9Sstevel@tonic-gate 			goto error;
25377c478bd9Sstevel@tonic-gate 		}
25387c478bd9Sstevel@tonic-gate 	} else {
25397c478bd9Sstevel@tonic-gate 		if ((alen > 0) && ((aoff < 0) ||
2540fc80c0dfSnordmark 		    ((ssize_t)(aoff + alen) > msz) ||
2541fc80c0dfSnordmark 		    ((aoff + alen) < 0))) {
25427c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2543cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
2544fc80c0dfSnordmark 			    "tl_bind: invalid message"));
2545b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
2546cb492d90SToomas Soome 			tli_err = TSYSERR;
2547cb492d90SToomas Soome 			unix_err = EINVAL;
25487c478bd9Sstevel@tonic-gate 			goto error;
25497c478bd9Sstevel@tonic-gate 		}
25507c478bd9Sstevel@tonic-gate 		if ((alen < 0) || (alen > (msz - sizeof (struct T_bind_req)))) {
25517c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2552cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
2553fc80c0dfSnordmark 			    "tl_bind: bad addr in  message"));
2554b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
25557c478bd9Sstevel@tonic-gate 			tli_err = TBADADDR;
25567c478bd9Sstevel@tonic-gate 			goto error;
25577c478bd9Sstevel@tonic-gate 		}
25587c478bd9Sstevel@tonic-gate #ifdef DEBUG
25597c478bd9Sstevel@tonic-gate 		/*
25607c478bd9Sstevel@tonic-gate 		 * Mild form of ASSERT()ion to detect broken TPI apps.
2561cb492d90SToomas Soome 		 * if (!assertion)
25627c478bd9Sstevel@tonic-gate 		 *	log warning;
25637c478bd9Sstevel@tonic-gate 		 */
2564cb492d90SToomas Soome 		if (!((alen == 0 && aoff == 0) ||
25657c478bd9Sstevel@tonic-gate 			(aoff >= (t_scalar_t)(sizeof (struct T_bind_req))))) {
25667c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2567cb492d90SToomas Soome 				    3, SL_TRACE | SL_ERROR,
25687c478bd9Sstevel@tonic-gate 				    "tl_bind: addr overlaps TPI message"));
25697c478bd9Sstevel@tonic-gate 		}
25707c478bd9Sstevel@tonic-gate #endif
25717c478bd9Sstevel@tonic-gate 	}
25727c478bd9Sstevel@tonic-gate 
25737c478bd9Sstevel@tonic-gate 	/*
25747c478bd9Sstevel@tonic-gate 	 * Bind the address provided or allocate one if requested.
25757c478bd9Sstevel@tonic-gate 	 * Allow rebinds with a new qlen value.
25767c478bd9Sstevel@tonic-gate 	 */
25777c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(tep)) {
25787c478bd9Sstevel@tonic-gate 		/*
25797c478bd9Sstevel@tonic-gate 		 * For anonymous requests the te_ap is already set up properly
25807c478bd9Sstevel@tonic-gate 		 * so use minor number as an address.
25817c478bd9Sstevel@tonic-gate 		 * For explicit requests need to check whether the address is
25827c478bd9Sstevel@tonic-gate 		 * already in use.
25837c478bd9Sstevel@tonic-gate 		 */
25847c478bd9Sstevel@tonic-gate 		if (ux_addr.soua_magic == SOU_MAGIC_EXPLICIT) {
25857c478bd9Sstevel@tonic-gate 			int rc;
25867c478bd9Sstevel@tonic-gate 
25877c478bd9Sstevel@tonic-gate 			if (tep->te_flag & TL_ADDRHASHED) {
25887c478bd9Sstevel@tonic-gate 				ASSERT(IS_COTS(tep) && tep->te_qlen == 0);
25897c478bd9Sstevel@tonic-gate 				if (tep->te_vp == ux_addr.soua_vp)
25907c478bd9Sstevel@tonic-gate 					goto skip_addr_bind;
25917c478bd9Sstevel@tonic-gate 				else /* Rebind to a new address. */
25927c478bd9Sstevel@tonic-gate 					tl_addr_unbind(tep);
25937c478bd9Sstevel@tonic-gate 			}
25947c478bd9Sstevel@tonic-gate 			/*
25957c478bd9Sstevel@tonic-gate 			 * Insert address in the hash if it is not already
25967c478bd9Sstevel@tonic-gate 			 * there.  Since we use preallocated handle, the insert
25977c478bd9Sstevel@tonic-gate 			 * can fail only if the key is already present.
25987c478bd9Sstevel@tonic-gate 			 */
25997c478bd9Sstevel@tonic-gate 			rc = mod_hash_insert_reserve(tep->te_addrhash,
26007c478bd9Sstevel@tonic-gate 			    (mod_hash_key_t)ux_addr.soua_vp,
26017c478bd9Sstevel@tonic-gate 			    (mod_hash_val_t)tep, tep->te_hash_hndl);
26027c478bd9Sstevel@tonic-gate 
26037c478bd9Sstevel@tonic-gate 			if (rc != 0) {
26047c478bd9Sstevel@tonic-gate 				ASSERT(rc == MH_ERR_DUPLICATE);
26057c478bd9Sstevel@tonic-gate 				/*
26067c478bd9Sstevel@tonic-gate 				 * Violate O_T_BIND_REQ semantics and fail with
26077c478bd9Sstevel@tonic-gate 				 * TADDRBUSY - sockets will not use any address
26087c478bd9Sstevel@tonic-gate 				 * other than supplied one for explicit binds.
26097c478bd9Sstevel@tonic-gate 				 */
26107c478bd9Sstevel@tonic-gate 				(void) (STRLOG(TL_ID, tep->te_minor, 1,
2611cb492d90SToomas Soome 				    SL_TRACE | SL_ERROR,
2612fc80c0dfSnordmark 				    "tl_bind:requested addr %p is busy",
2613fc80c0dfSnordmark 				    ux_addr.soua_vp));
2614cb492d90SToomas Soome 				tli_err = TADDRBUSY;
2615cb492d90SToomas Soome 				unix_err = 0;
26167c478bd9Sstevel@tonic-gate 				goto error;
26177c478bd9Sstevel@tonic-gate 			}
26187c478bd9Sstevel@tonic-gate 			tep->te_uxaddr = ux_addr;
26197c478bd9Sstevel@tonic-gate 			tep->te_flag |= TL_ADDRHASHED;
26207c478bd9Sstevel@tonic-gate 			tep->te_hash_hndl = NULL;
26217c478bd9Sstevel@tonic-gate 		}
26227c478bd9Sstevel@tonic-gate 	} else if (alen == 0) {
26237c478bd9Sstevel@tonic-gate 		/*
26247c478bd9Sstevel@tonic-gate 		 * assign any free address
26257c478bd9Sstevel@tonic-gate 		 */
2626cb492d90SToomas Soome 		if (!tl_get_any_addr(tep, NULL)) {
26277c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2628cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
2629fc80c0dfSnordmark 			    "tl_bind:failed to get buffer for any "
2630fc80c0dfSnordmark 			    "address"));
2631cb492d90SToomas Soome 			tli_err = TSYSERR;
2632cb492d90SToomas Soome 			unix_err = ENOSR;
26337c478bd9Sstevel@tonic-gate 			goto error;
26347c478bd9Sstevel@tonic-gate 		}
26357c478bd9Sstevel@tonic-gate 	} else {
26367c478bd9Sstevel@tonic-gate 		addr_req.ta_alen = alen;
26377c478bd9Sstevel@tonic-gate 		addr_req.ta_abuf = (mp->b_rptr + aoff);
26387c478bd9Sstevel@tonic-gate 		addr_req.ta_zoneid = tep->te_zoneid;
26397c478bd9Sstevel@tonic-gate 
26407c478bd9Sstevel@tonic-gate 		tep->te_abuf = kmem_zalloc((size_t)alen, KM_NOSLEEP);
26417c478bd9Sstevel@tonic-gate 		if (tep->te_abuf == NULL) {
2642cb492d90SToomas Soome 			tli_err = TSYSERR;
2643cb492d90SToomas Soome 			unix_err = ENOSR;
26447c478bd9Sstevel@tonic-gate 			goto error;
26457c478bd9Sstevel@tonic-gate 		}
26467c478bd9Sstevel@tonic-gate 		bcopy(addr_req.ta_abuf, tep->te_abuf, addr_req.ta_alen);
26477c478bd9Sstevel@tonic-gate 		tep->te_alen = alen;
26487c478bd9Sstevel@tonic-gate 
26497c478bd9Sstevel@tonic-gate 		if (mod_hash_insert_reserve(tep->te_addrhash,
2650fc80c0dfSnordmark 		    (mod_hash_key_t)&tep->te_ap, (mod_hash_val_t)tep,
2651fc80c0dfSnordmark 		    tep->te_hash_hndl) != 0) {
26527c478bd9Sstevel@tonic-gate 			if (save_prim_type == T_BIND_REQ) {
26537c478bd9Sstevel@tonic-gate 				/*
26547c478bd9Sstevel@tonic-gate 				 * The bind semantics for this primitive
26557c478bd9Sstevel@tonic-gate 				 * require a failure if the exact address
26567c478bd9Sstevel@tonic-gate 				 * requested is busy
26577c478bd9Sstevel@tonic-gate 				 */
26587c478bd9Sstevel@tonic-gate 				(void) (STRLOG(TL_ID, tep->te_minor, 1,
2659cb492d90SToomas Soome 				    SL_TRACE | SL_ERROR,
2660fc80c0dfSnordmark 				    "tl_bind:requested addr is busy"));
2661cb492d90SToomas Soome 				tli_err = TADDRBUSY;
2662cb492d90SToomas Soome 				unix_err = 0;
26637c478bd9Sstevel@tonic-gate 				goto error;
26647c478bd9Sstevel@tonic-gate 			}
26657c478bd9Sstevel@tonic-gate 
26667c478bd9Sstevel@tonic-gate 			/*
26677c478bd9Sstevel@tonic-gate 			 * O_T_BIND_REQ semantics say if address if requested
26687c478bd9Sstevel@tonic-gate 			 * address is busy, bind to any available free address
26697c478bd9Sstevel@tonic-gate 			 */
2670cb492d90SToomas Soome 			if (!tl_get_any_addr(tep, &addr_req)) {
26717c478bd9Sstevel@tonic-gate 				(void) (STRLOG(TL_ID, tep->te_minor, 1,
2672cb492d90SToomas Soome 				    SL_TRACE | SL_ERROR,
2673fc80c0dfSnordmark 				    "tl_bind:unable to get any addr buf"));
2674cb492d90SToomas Soome 				tli_err = TSYSERR;
2675cb492d90SToomas Soome 				unix_err = ENOMEM;
26767c478bd9Sstevel@tonic-gate 				goto error;
26777c478bd9Sstevel@tonic-gate 			}
26787c478bd9Sstevel@tonic-gate 		} else {
26797c478bd9Sstevel@tonic-gate 			tep->te_flag |= TL_ADDRHASHED;
26807c478bd9Sstevel@tonic-gate 			tep->te_hash_hndl = NULL;
26817c478bd9Sstevel@tonic-gate 		}
26827c478bd9Sstevel@tonic-gate 	}
26837c478bd9Sstevel@tonic-gate 
26847c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_alen >= 0);
26857c478bd9Sstevel@tonic-gate 
26867c478bd9Sstevel@tonic-gate skip_addr_bind:
26877c478bd9Sstevel@tonic-gate 	/*
26887c478bd9Sstevel@tonic-gate 	 * prepare T_BIND_ACK TPI message
26897c478bd9Sstevel@tonic-gate 	 */
26907c478bd9Sstevel@tonic-gate 	basize = sizeof (struct T_bind_ack) + tep->te_alen;
26917c478bd9Sstevel@tonic-gate 	bamp = reallocb(mp, basize, 0);
26927c478bd9Sstevel@tonic-gate 	if (bamp == NULL) {
2693cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
2694fc80c0dfSnordmark 		    "tl_wput:tl_bind: allocb failed"));
26957c478bd9Sstevel@tonic-gate 		/*
26967c478bd9Sstevel@tonic-gate 		 * roll back state changes
26977c478bd9Sstevel@tonic-gate 		 */
26987c478bd9Sstevel@tonic-gate 		tl_addr_unbind(tep);
26997c478bd9Sstevel@tonic-gate 		tep->te_state = TS_UNBND;
27007c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, basize);
27017c478bd9Sstevel@tonic-gate 		return;
27027c478bd9Sstevel@tonic-gate 	}
27037c478bd9Sstevel@tonic-gate 
27047c478bd9Sstevel@tonic-gate 	DB_TYPE(bamp) = M_PCPROTO;
27057c478bd9Sstevel@tonic-gate 	bamp->b_wptr = bamp->b_rptr + basize;
27067c478bd9Sstevel@tonic-gate 	b_ack = (struct T_bind_ack *)bamp->b_rptr;
27077c478bd9Sstevel@tonic-gate 	b_ack->PRIM_type = T_BIND_ACK;
27087c478bd9Sstevel@tonic-gate 	b_ack->CONIND_number = qlen;
27097c478bd9Sstevel@tonic-gate 	b_ack->ADDR_length = tep->te_alen;
27107c478bd9Sstevel@tonic-gate 	b_ack->ADDR_offset = (t_scalar_t)sizeof (struct T_bind_ack);
27117c478bd9Sstevel@tonic-gate 	addr_startp = bamp->b_rptr + b_ack->ADDR_offset;
27127c478bd9Sstevel@tonic-gate 	bcopy(tep->te_abuf, addr_startp, tep->te_alen);
27137c478bd9Sstevel@tonic-gate 
27147c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
27157c478bd9Sstevel@tonic-gate 		tep->te_qlen = qlen;
27167c478bd9Sstevel@tonic-gate 		if (qlen > 0)
27177c478bd9Sstevel@tonic-gate 			tep->te_flag |= TL_LISTENER;
27187c478bd9Sstevel@tonic-gate 	}
27197c478bd9Sstevel@tonic-gate 
2720b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_BIND_ACK][tep->te_state];
27217c478bd9Sstevel@tonic-gate 	/*
27227c478bd9Sstevel@tonic-gate 	 * send T_BIND_ACK message
27237c478bd9Sstevel@tonic-gate 	 */
27247c478bd9Sstevel@tonic-gate 	(void) qreply(wq, bamp);
27257c478bd9Sstevel@tonic-gate 	return;
27267c478bd9Sstevel@tonic-gate 
27277c478bd9Sstevel@tonic-gate error:
27287c478bd9Sstevel@tonic-gate 	ackmp = reallocb(mp, sizeof (struct T_error_ack), 0);
27297c478bd9Sstevel@tonic-gate 	if (ackmp == NULL) {
27307c478bd9Sstevel@tonic-gate 		/*
27317c478bd9Sstevel@tonic-gate 		 * roll back state changes
27327c478bd9Sstevel@tonic-gate 		 */
27337c478bd9Sstevel@tonic-gate 		tep->te_state = save_state;
27347c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, sizeof (struct T_error_ack));
27357c478bd9Sstevel@tonic-gate 		return;
27367c478bd9Sstevel@tonic-gate 	}
2737b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
27387c478bd9Sstevel@tonic-gate 	tl_error_ack(wq, ackmp, tli_err, unix_err, save_prim_type);
27397c478bd9Sstevel@tonic-gate }
27407c478bd9Sstevel@tonic-gate 
27417c478bd9Sstevel@tonic-gate /*
27427c478bd9Sstevel@tonic-gate  * Process T_UNBIND_REQ.
27437c478bd9Sstevel@tonic-gate  * Called from serializer.
27447c478bd9Sstevel@tonic-gate  */
27457c478bd9Sstevel@tonic-gate static void
tl_unbind(mblk_t * mp,tl_endpt_t * tep)27467c478bd9Sstevel@tonic-gate tl_unbind(mblk_t *mp, tl_endpt_t *tep)
27477c478bd9Sstevel@tonic-gate {
27487c478bd9Sstevel@tonic-gate 	queue_t *wq;
27497c478bd9Sstevel@tonic-gate 	mblk_t *ackmp;
27507c478bd9Sstevel@tonic-gate 
27517c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
27527c478bd9Sstevel@tonic-gate 		freemsg(mp);
27537c478bd9Sstevel@tonic-gate 		return;
27547c478bd9Sstevel@tonic-gate 	}
27557c478bd9Sstevel@tonic-gate 
27567c478bd9Sstevel@tonic-gate 	wq = tep->te_wq;
27577c478bd9Sstevel@tonic-gate 
27587c478bd9Sstevel@tonic-gate 	/*
27597c478bd9Sstevel@tonic-gate 	 * preallocate memory for max of T_OK_ACK and T_ERROR_ACK
27607c478bd9Sstevel@tonic-gate 	 * ==> allocate for T_ERROR_ACK (known max)
27617c478bd9Sstevel@tonic-gate 	 */
27627c478bd9Sstevel@tonic-gate 	if ((ackmp = reallocb(mp, sizeof (struct T_error_ack), 0)) == NULL) {
27637c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, sizeof (struct T_error_ack));
27647c478bd9Sstevel@tonic-gate 		return;
27657c478bd9Sstevel@tonic-gate 	}
27667c478bd9Sstevel@tonic-gate 	/*
27677c478bd9Sstevel@tonic-gate 	 * memory resources committed
27687c478bd9Sstevel@tonic-gate 	 * Note: no message validation. T_UNBIND_REQ message is
27697c478bd9Sstevel@tonic-gate 	 * same size as PRIM_type field so already verified earlier.
27707c478bd9Sstevel@tonic-gate 	 */
27717c478bd9Sstevel@tonic-gate 
27727c478bd9Sstevel@tonic-gate 	/*
27737c478bd9Sstevel@tonic-gate 	 * validate state
27747c478bd9Sstevel@tonic-gate 	 */
27757c478bd9Sstevel@tonic-gate 	if (tep->te_state != TS_IDLE) {
27767c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
2777cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
2778fc80c0dfSnordmark 		    "tl_wput:T_UNBIND_REQ:out of state, state=%d",
2779fc80c0dfSnordmark 		    tep->te_state));
27807c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TOUTSTATE, 0, T_UNBIND_REQ);
27817c478bd9Sstevel@tonic-gate 		return;
27827c478bd9Sstevel@tonic-gate 	}
2783b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_UNBIND_REQ][tep->te_state];
27847c478bd9Sstevel@tonic-gate 
27857c478bd9Sstevel@tonic-gate 	/*
27867c478bd9Sstevel@tonic-gate 	 * TPI says on T_UNBIND_REQ:
27877c478bd9Sstevel@tonic-gate 	 *    send up a M_FLUSH to flush both
27887c478bd9Sstevel@tonic-gate 	 *    read and write queues
27897c478bd9Sstevel@tonic-gate 	 */
27907c478bd9Sstevel@tonic-gate 	(void) putnextctl1(RD(wq), M_FLUSH, FLUSHRW);
27917c478bd9Sstevel@tonic-gate 
2792cb492d90SToomas Soome 	if (!IS_SOCKET(tep) || !IS_CLTS(tep) || tep->te_qlen != 0 ||
27937c478bd9Sstevel@tonic-gate 	    tep->te_magic != SOU_MAGIC_EXPLICIT) {
27947c478bd9Sstevel@tonic-gate 
27957c478bd9Sstevel@tonic-gate 		/*
27967c478bd9Sstevel@tonic-gate 		 * Sockets use bind with qlen==0 followed by bind() to
27977c478bd9Sstevel@tonic-gate 		 * the same address with qlen > 0 for listeners.
27987c478bd9Sstevel@tonic-gate 		 * We allow rebind with a new qlen value.
27997c478bd9Sstevel@tonic-gate 		 */
28007c478bd9Sstevel@tonic-gate 		tl_addr_unbind(tep);
28017c478bd9Sstevel@tonic-gate 	}
28027c478bd9Sstevel@tonic-gate 
2803b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_OK_ACK1][tep->te_state];
28047c478bd9Sstevel@tonic-gate 	/*
28057c478bd9Sstevel@tonic-gate 	 * send  T_OK_ACK
28067c478bd9Sstevel@tonic-gate 	 */
28077c478bd9Sstevel@tonic-gate 	tl_ok_ack(wq, ackmp, T_UNBIND_REQ);
28087c478bd9Sstevel@tonic-gate }
28097c478bd9Sstevel@tonic-gate 
28107c478bd9Sstevel@tonic-gate 
28117c478bd9Sstevel@tonic-gate /*
28127c478bd9Sstevel@tonic-gate  * Option management code from drv/ip is used here
28137c478bd9Sstevel@tonic-gate  * Note: TL_PROT_LEVEL/TL_IOC_CREDOPT option is not part of tl_opt_arr
28147c478bd9Sstevel@tonic-gate  *	database of options. So optcom_req() will fail T_SVR4_OPTMGMT_REQ.
28157c478bd9Sstevel@tonic-gate  *	However, that is what we want as that option is 'unorthodox'
28167c478bd9Sstevel@tonic-gate  *	and only valid in T_CONN_IND, T_CONN_CON  and T_UNITDATA_IND
28177c478bd9Sstevel@tonic-gate  *	and not in T_SVR4_OPTMGMT_REQ/ACK
28187c478bd9Sstevel@tonic-gate  * Note2: use of optcom_req means this routine is an exception to
28197c478bd9Sstevel@tonic-gate  *	 recovery from allocb() failures.
28207c478bd9Sstevel@tonic-gate  */
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate static void
tl_optmgmt(queue_t * wq,mblk_t * mp)28237c478bd9Sstevel@tonic-gate tl_optmgmt(queue_t *wq, mblk_t *mp)
28247c478bd9Sstevel@tonic-gate {
28257c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep;
28267c478bd9Sstevel@tonic-gate 	mblk_t *ackmp;
28277c478bd9Sstevel@tonic-gate 	union T_primitives *prim;
2828de8c4a14SErik Nordmark 	cred_t *cr;
28297c478bd9Sstevel@tonic-gate 
28307c478bd9Sstevel@tonic-gate 	tep = (tl_endpt_t *)wq->q_ptr;
28317c478bd9Sstevel@tonic-gate 	prim = (union T_primitives *)mp->b_rptr;
28327c478bd9Sstevel@tonic-gate 
2833de8c4a14SErik Nordmark 	/*
2834de8c4a14SErik Nordmark 	 * All Solaris components should pass a db_credp
2835de8c4a14SErik Nordmark 	 * for this TPI message, hence we ASSERT.
2836de8c4a14SErik Nordmark 	 * But in case there is some other M_PROTO that looks
2837de8c4a14SErik Nordmark 	 * like a TPI message sent by some other kernel
2838de8c4a14SErik Nordmark 	 * component, we check and return an error.
2839de8c4a14SErik Nordmark 	 */
2840de8c4a14SErik Nordmark 	cr = msg_getcred(mp, NULL);
2841de8c4a14SErik Nordmark 	ASSERT(cr != NULL);
2842de8c4a14SErik Nordmark 	if (cr == NULL) {
2843de8c4a14SErik Nordmark 		tl_error_ack(wq, mp, TSYSERR, EINVAL, prim->type);
2844de8c4a14SErik Nordmark 		return;
2845de8c4a14SErik Nordmark 	}
2846de8c4a14SErik Nordmark 
28477c478bd9Sstevel@tonic-gate 	/*  all states OK for AF_UNIX options ? */
28487c478bd9Sstevel@tonic-gate 	if (!IS_SOCKET(tep) && tep->te_state != TS_IDLE &&
28497c478bd9Sstevel@tonic-gate 	    prim->type == T_SVR4_OPTMGMT_REQ) {
28507c478bd9Sstevel@tonic-gate 		/*
28517c478bd9Sstevel@tonic-gate 		 * Broken TLI semantics that options can only be managed
28527c478bd9Sstevel@tonic-gate 		 * in TS_IDLE state. Needed for Sparc ABI test suite that
28537c478bd9Sstevel@tonic-gate 		 * tests this TLI (mis)feature using this device driver.
28547c478bd9Sstevel@tonic-gate 		 */
28557c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
2856cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
2857fc80c0dfSnordmark 		    "tl_wput:T_SVR4_OPTMGMT_REQ:out of state, state=%d",
2858fc80c0dfSnordmark 		    tep->te_state));
28597c478bd9Sstevel@tonic-gate 		/*
28607c478bd9Sstevel@tonic-gate 		 * preallocate memory for T_ERROR_ACK
28617c478bd9Sstevel@tonic-gate 		 */
28627c478bd9Sstevel@tonic-gate 		ackmp = allocb(sizeof (struct T_error_ack), BPRI_MED);
2863cb492d90SToomas Soome 		if (ackmp == NULL) {
28647c478bd9Sstevel@tonic-gate 			tl_memrecover(wq, mp, sizeof (struct T_error_ack));
28657c478bd9Sstevel@tonic-gate 			return;
28667c478bd9Sstevel@tonic-gate 		}
28677c478bd9Sstevel@tonic-gate 
28687c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TOUTSTATE, 0, T_SVR4_OPTMGMT_REQ);
28697c478bd9Sstevel@tonic-gate 		freemsg(mp);
28707c478bd9Sstevel@tonic-gate 		return;
28717c478bd9Sstevel@tonic-gate 	}
28727c478bd9Sstevel@tonic-gate 
28737c478bd9Sstevel@tonic-gate 	/*
28747c478bd9Sstevel@tonic-gate 	 * call common option management routine from drv/ip
28757c478bd9Sstevel@tonic-gate 	 */
28767c478bd9Sstevel@tonic-gate 	if (prim->type == T_SVR4_OPTMGMT_REQ) {
2877bd670b35SErik Nordmark 		svr4_optcom_req(wq, mp, cr, &tl_opt_obj);
28787c478bd9Sstevel@tonic-gate 	} else {
28797c478bd9Sstevel@tonic-gate 		ASSERT(prim->type == T_OPTMGMT_REQ);
2880bd670b35SErik Nordmark 		tpi_optcom_req(wq, mp, cr, &tl_opt_obj);
28817c478bd9Sstevel@tonic-gate 	}
28827c478bd9Sstevel@tonic-gate }
28837c478bd9Sstevel@tonic-gate 
28847c478bd9Sstevel@tonic-gate /*
28857c478bd9Sstevel@tonic-gate  * Handle T_conn_req - the driver part of accept().
28867c478bd9Sstevel@tonic-gate  * If TL_SET[U]CRED generate the credentials options.
28877c478bd9Sstevel@tonic-gate  * If this is a socket pass through options unmodified.
28887c478bd9Sstevel@tonic-gate  * For sockets generate the T_CONN_CON here instead of
28897c478bd9Sstevel@tonic-gate  * waiting for the T_CONN_RES.
28907c478bd9Sstevel@tonic-gate  */
28917c478bd9Sstevel@tonic-gate static void
tl_conn_req(queue_t * wq,mblk_t * mp)28927c478bd9Sstevel@tonic-gate tl_conn_req(queue_t *wq, mblk_t *mp)
28937c478bd9Sstevel@tonic-gate {
28947c478bd9Sstevel@tonic-gate 	tl_endpt_t		*tep = (tl_endpt_t *)wq->q_ptr;
28957c478bd9Sstevel@tonic-gate 	struct T_conn_req	*creq = (struct T_conn_req *)mp->b_rptr;
28967c478bd9Sstevel@tonic-gate 	ssize_t			msz = MBLKL(mp);
28977c478bd9Sstevel@tonic-gate 	t_scalar_t		alen, aoff, olen, ooff,	err = 0;
28987c478bd9Sstevel@tonic-gate 	tl_endpt_t		*peer_tep = NULL;
28997c478bd9Sstevel@tonic-gate 	mblk_t			*ackmp;
29007c478bd9Sstevel@tonic-gate 	mblk_t			*dimp;
29017c478bd9Sstevel@tonic-gate 	struct T_discon_ind	*di;
29027c478bd9Sstevel@tonic-gate 	soux_addr_t		ux_addr;
29037c478bd9Sstevel@tonic-gate 	tl_addr_t		dst;
29047c478bd9Sstevel@tonic-gate 
29057c478bd9Sstevel@tonic-gate 	ASSERT(IS_COTS(tep));
29067c478bd9Sstevel@tonic-gate 
29077c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
29087c478bd9Sstevel@tonic-gate 		freemsg(mp);
29097c478bd9Sstevel@tonic-gate 		return;
29107c478bd9Sstevel@tonic-gate 	}
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 	/*
29137c478bd9Sstevel@tonic-gate 	 * preallocate memory for:
29147c478bd9Sstevel@tonic-gate 	 * 1. max of T_ERROR_ACK and T_OK_ACK
29157c478bd9Sstevel@tonic-gate 	 *	==> known max T_ERROR_ACK
29167c478bd9Sstevel@tonic-gate 	 * 2. max of T_DISCON_IND and T_CONN_IND
29177c478bd9Sstevel@tonic-gate 	 */
29187c478bd9Sstevel@tonic-gate 	ackmp = allocb(sizeof (struct T_error_ack), BPRI_MED);
2919cb492d90SToomas Soome 	if (ackmp == NULL) {
29207c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, sizeof (struct T_error_ack));
29217c478bd9Sstevel@tonic-gate 		return;
29227c478bd9Sstevel@tonic-gate 	}
29237c478bd9Sstevel@tonic-gate 	/*
29247c478bd9Sstevel@tonic-gate 	 * memory committed for T_OK_ACK/T_ERROR_ACK now
29257c478bd9Sstevel@tonic-gate 	 * will be committed for T_DISCON_IND/T_CONN_IND later
29267c478bd9Sstevel@tonic-gate 	 */
29277c478bd9Sstevel@tonic-gate 
29287c478bd9Sstevel@tonic-gate 	if (tep->te_state != TS_IDLE) {
29297c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
2930cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
2931fc80c0dfSnordmark 		    "tl_wput:T_CONN_REQ:out of state, state=%d",
2932fc80c0dfSnordmark 		    tep->te_state));
29337c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TOUTSTATE, 0, T_CONN_REQ);
29347c478bd9Sstevel@tonic-gate 		freemsg(mp);
29357c478bd9Sstevel@tonic-gate 		return;
29367c478bd9Sstevel@tonic-gate 	}
29377c478bd9Sstevel@tonic-gate 
29387c478bd9Sstevel@tonic-gate 	/*
29397c478bd9Sstevel@tonic-gate 	 * validate the message
29407c478bd9Sstevel@tonic-gate 	 * Note: dereference fields in struct inside message only
29417c478bd9Sstevel@tonic-gate 	 * after validating the message length.
29427c478bd9Sstevel@tonic-gate 	 */
29437c478bd9Sstevel@tonic-gate 	if (msz < sizeof (struct T_conn_req)) {
2944cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
2945fc80c0dfSnordmark 		    "tl_conn_req:invalid message length"));
29467c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TSYSERR, EINVAL, T_CONN_REQ);
29477c478bd9Sstevel@tonic-gate 		freemsg(mp);
29487c478bd9Sstevel@tonic-gate 		return;
29497c478bd9Sstevel@tonic-gate 	}
29507c478bd9Sstevel@tonic-gate 	alen = creq->DEST_length;
29517c478bd9Sstevel@tonic-gate 	aoff = creq->DEST_offset;
29527c478bd9Sstevel@tonic-gate 	olen = creq->OPT_length;
29537c478bd9Sstevel@tonic-gate 	ooff = creq->OPT_offset;
29547c478bd9Sstevel@tonic-gate 	if (olen == 0)
29557c478bd9Sstevel@tonic-gate 		ooff = 0;
29567c478bd9Sstevel@tonic-gate 
29577c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(tep)) {
29587c478bd9Sstevel@tonic-gate 		if ((alen != TL_SOUX_ADDRLEN) ||
29597c478bd9Sstevel@tonic-gate 		    (aoff < 0) ||
29607c478bd9Sstevel@tonic-gate 		    (aoff + alen > msz) ||
29617c478bd9Sstevel@tonic-gate 		    (alen > msz - sizeof (struct T_conn_req))) {
29627c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2963cb492d90SToomas Soome 				    1, SL_TRACE | SL_ERROR,
29647c478bd9Sstevel@tonic-gate 				    "tl_conn_req: invalid socket addr"));
29657c478bd9Sstevel@tonic-gate 			tl_error_ack(wq, ackmp, TSYSERR, EINVAL, T_CONN_REQ);
29667c478bd9Sstevel@tonic-gate 			freemsg(mp);
29677c478bd9Sstevel@tonic-gate 			return;
29687c478bd9Sstevel@tonic-gate 		}
29697c478bd9Sstevel@tonic-gate 		bcopy(mp->b_rptr + aoff, &ux_addr, TL_SOUX_ADDRLEN);
29707c478bd9Sstevel@tonic-gate 		if ((ux_addr.soua_magic != SOU_MAGIC_IMPLICIT) &&
29717c478bd9Sstevel@tonic-gate 		    (ux_addr.soua_magic != SOU_MAGIC_EXPLICIT)) {
29727c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
2973cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
2974fc80c0dfSnordmark 			    "tl_conn_req: invalid socket magic"));
29757c478bd9Sstevel@tonic-gate 			tl_error_ack(wq, ackmp, TSYSERR, EINVAL, T_CONN_REQ);
29767c478bd9Sstevel@tonic-gate 			freemsg(mp);
29777c478bd9Sstevel@tonic-gate 			return;
29787c478bd9Sstevel@tonic-gate 		}
29797c478bd9Sstevel@tonic-gate 	} else {
29807c478bd9Sstevel@tonic-gate 		if ((alen > 0 && ((aoff + alen) > msz || aoff + alen < 0)) ||
29817c478bd9Sstevel@tonic-gate 		    (olen > 0 && ((ssize_t)(ooff + olen) > msz ||
2982fc80c0dfSnordmark 		    ooff + olen < 0)) ||
29837c478bd9Sstevel@tonic-gate 		    olen < 0 || ooff < 0) {
29847c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
2985cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
2986fc80c0dfSnordmark 			    "tl_conn_req:invalid message"));
29877c478bd9Sstevel@tonic-gate 			tl_error_ack(wq, ackmp, TSYSERR, EINVAL, T_CONN_REQ);
29887c478bd9Sstevel@tonic-gate 			freemsg(mp);
29897c478bd9Sstevel@tonic-gate 			return;
29907c478bd9Sstevel@tonic-gate 		}
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 		if (alen <= 0 || aoff < 0 ||
29937c478bd9Sstevel@tonic-gate 		    (ssize_t)alen > msz - sizeof (struct T_conn_req)) {
29947c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
2995cb492d90SToomas Soome 				    SL_TRACE | SL_ERROR,
29967c478bd9Sstevel@tonic-gate 				    "tl_conn_req:bad addr in message, "
29977c478bd9Sstevel@tonic-gate 				    "alen=%d, msz=%ld",
29987c478bd9Sstevel@tonic-gate 				    alen, msz));
29997c478bd9Sstevel@tonic-gate 			tl_error_ack(wq, ackmp, TBADADDR, 0, T_CONN_REQ);
30007c478bd9Sstevel@tonic-gate 			freemsg(mp);
30017c478bd9Sstevel@tonic-gate 			return;
30027c478bd9Sstevel@tonic-gate 		}
30037c478bd9Sstevel@tonic-gate #ifdef DEBUG
30047c478bd9Sstevel@tonic-gate 		/*
30057c478bd9Sstevel@tonic-gate 		 * Mild form of ASSERT()ion to detect broken TPI apps.
3006cb492d90SToomas Soome 		 * if (!assertion)
30077c478bd9Sstevel@tonic-gate 		 *	log warning;
30087c478bd9Sstevel@tonic-gate 		 */
3009cb492d90SToomas Soome 		if (!(aoff >= (t_scalar_t)sizeof (struct T_conn_req))) {
30107c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
3011cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
3012fc80c0dfSnordmark 			    "tl_conn_req: addr overlaps TPI message"));
30137c478bd9Sstevel@tonic-gate 		}
30147c478bd9Sstevel@tonic-gate #endif
30157c478bd9Sstevel@tonic-gate 		if (olen) {
30167c478bd9Sstevel@tonic-gate 			/*
30177c478bd9Sstevel@tonic-gate 			 * no opts in connect req
30187c478bd9Sstevel@tonic-gate 			 * supported in this provider except for sockets.
30197c478bd9Sstevel@tonic-gate 			 */
30207c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
3021cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
3022fc80c0dfSnordmark 			    "tl_conn_req:options not supported "
3023fc80c0dfSnordmark 			    "in message"));
30247c478bd9Sstevel@tonic-gate 			tl_error_ack(wq, ackmp, TBADOPT, 0, T_CONN_REQ);
30257c478bd9Sstevel@tonic-gate 			freemsg(mp);
30267c478bd9Sstevel@tonic-gate 			return;
30277c478bd9Sstevel@tonic-gate 		}
30287c478bd9Sstevel@tonic-gate 	}
30297c478bd9Sstevel@tonic-gate 
30307c478bd9Sstevel@tonic-gate 	/*
30317c478bd9Sstevel@tonic-gate 	 * Prevent tep from closing on us.
30327c478bd9Sstevel@tonic-gate 	 */
3033cb492d90SToomas Soome 	if (!tl_noclose(tep)) {
3034cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
3035fc80c0dfSnordmark 		    "tl_conn_req:endpoint is closing"));
30367c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TOUTSTATE, 0, T_CONN_REQ);
30377c478bd9Sstevel@tonic-gate 		freemsg(mp);
30387c478bd9Sstevel@tonic-gate 		return;
30397c478bd9Sstevel@tonic-gate 	}
30407c478bd9Sstevel@tonic-gate 
3041b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_CONN_REQ][tep->te_state];
30427c478bd9Sstevel@tonic-gate 	/*
30437c478bd9Sstevel@tonic-gate 	 * get endpoint to connect to
30447c478bd9Sstevel@tonic-gate 	 * check that peer with DEST addr is bound to addr
30457c478bd9Sstevel@tonic-gate 	 * and has CONIND_number > 0
30467c478bd9Sstevel@tonic-gate 	 */
30477c478bd9Sstevel@tonic-gate 	dst.ta_alen = alen;
30487c478bd9Sstevel@tonic-gate 	dst.ta_abuf = mp->b_rptr + aoff;
30497c478bd9Sstevel@tonic-gate 	dst.ta_zoneid = tep->te_zoneid;
30507c478bd9Sstevel@tonic-gate 
30517c478bd9Sstevel@tonic-gate 	/*
30527c478bd9Sstevel@tonic-gate 	 * Verify if remote addr is in use
30537c478bd9Sstevel@tonic-gate 	 */
30547c478bd9Sstevel@tonic-gate 	peer_tep = (IS_SOCKET(tep) ?
30557c478bd9Sstevel@tonic-gate 	    tl_sock_find_peer(tep, &ux_addr) :
30567c478bd9Sstevel@tonic-gate 	    tl_find_peer(tep, &dst));
30577c478bd9Sstevel@tonic-gate 
30587c478bd9Sstevel@tonic-gate 	if (peer_tep == NULL) {
3059cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
3060fc80c0dfSnordmark 		    "tl_conn_req:no one at connect address"));
30617c478bd9Sstevel@tonic-gate 		err = ECONNREFUSED;
30627c478bd9Sstevel@tonic-gate 	} else if (peer_tep->te_nicon >= peer_tep->te_qlen)  {
30637c478bd9Sstevel@tonic-gate 		/*
30647c478bd9Sstevel@tonic-gate 		 * validate that number of incoming connection is
30657c478bd9Sstevel@tonic-gate 		 * not to capacity on destination endpoint
30667c478bd9Sstevel@tonic-gate 		 */
30677c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 2, SL_TRACE,
3068fc80c0dfSnordmark 		    "tl_conn_req: qlen overflow connection refused"));
306915c07adcSJohn Levon 		err = ECONNREFUSED;
30707c478bd9Sstevel@tonic-gate 	}
30717c478bd9Sstevel@tonic-gate 
30727c478bd9Sstevel@tonic-gate 	/*
3073b6626b4dSakolb 	 * Send T_DISCON_IND in case of error
30747c478bd9Sstevel@tonic-gate 	 */
30757c478bd9Sstevel@tonic-gate 	if (err != 0) {
30767c478bd9Sstevel@tonic-gate 		if (peer_tep != NULL)
30777c478bd9Sstevel@tonic-gate 			tl_refrele(peer_tep);
30787c478bd9Sstevel@tonic-gate 		/* We are still expected to send T_OK_ACK */
3079b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_OK_ACK1][tep->te_state];
30807c478bd9Sstevel@tonic-gate 		tl_ok_ack(tep->te_wq, ackmp, T_CONN_REQ);
30817c478bd9Sstevel@tonic-gate 		tl_closeok(tep);
30827c478bd9Sstevel@tonic-gate 		dimp = tpi_ack_alloc(mp, sizeof (struct T_discon_ind),
30837c478bd9Sstevel@tonic-gate 		    M_PROTO, T_DISCON_IND);
30847c478bd9Sstevel@tonic-gate 		if (dimp == NULL) {
30857c478bd9Sstevel@tonic-gate 			tl_merror(wq, NULL, ENOSR);
30867c478bd9Sstevel@tonic-gate 			return;
30877c478bd9Sstevel@tonic-gate 		}
30887c478bd9Sstevel@tonic-gate 		di = (struct T_discon_ind *)dimp->b_rptr;
30897c478bd9Sstevel@tonic-gate 		di->DISCON_reason = err;
30907c478bd9Sstevel@tonic-gate 		di->SEQ_number = BADSEQNUM;
30917c478bd9Sstevel@tonic-gate 
30927c478bd9Sstevel@tonic-gate 		tep->te_state = TS_IDLE;
30937c478bd9Sstevel@tonic-gate 		/*
30947c478bd9Sstevel@tonic-gate 		 * send T_DISCON_IND message
30957c478bd9Sstevel@tonic-gate 		 */
30967c478bd9Sstevel@tonic-gate 		putnext(tep->te_rq, dimp);
30977c478bd9Sstevel@tonic-gate 		return;
30987c478bd9Sstevel@tonic-gate 	}
30997c478bd9Sstevel@tonic-gate 
31007c478bd9Sstevel@tonic-gate 	ASSERT(IS_COTS(peer_tep));
31017c478bd9Sstevel@tonic-gate 
31027c478bd9Sstevel@tonic-gate 	/*
31037c478bd9Sstevel@tonic-gate 	 * Found the listener. At this point processing will continue on
31047c478bd9Sstevel@tonic-gate 	 * listener serializer. Close of the endpoint should be blocked while we
31057c478bd9Sstevel@tonic-gate 	 * switch serializers.
31067c478bd9Sstevel@tonic-gate 	 */
31077c478bd9Sstevel@tonic-gate 	tl_serializer_refhold(peer_tep->te_ser);
31087c478bd9Sstevel@tonic-gate 	tl_serializer_refrele(tep->te_ser);
31097c478bd9Sstevel@tonic-gate 	tep->te_ser = peer_tep->te_ser;
31107c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_oconp == NULL);
31117c478bd9Sstevel@tonic-gate 	tep->te_oconp = peer_tep;
31127c478bd9Sstevel@tonic-gate 
31137c478bd9Sstevel@tonic-gate 	/*
31147c478bd9Sstevel@tonic-gate 	 * It is safe to close now. Close may continue on listener serializer.
31157c478bd9Sstevel@tonic-gate 	 */
31167c478bd9Sstevel@tonic-gate 	tl_closeok(tep);
31177c478bd9Sstevel@tonic-gate 
31187c478bd9Sstevel@tonic-gate 	/*
31197c478bd9Sstevel@tonic-gate 	 * Pass ackmp to tl_conn_req_ser. Note that mp->b_cont may contain user
31207c478bd9Sstevel@tonic-gate 	 * data, so we link mp to ackmp.
31217c478bd9Sstevel@tonic-gate 	 */
31227c478bd9Sstevel@tonic-gate 	ackmp->b_cont = mp;
31237c478bd9Sstevel@tonic-gate 	mp = ackmp;
31247c478bd9Sstevel@tonic-gate 
31257c478bd9Sstevel@tonic-gate 	tl_refhold(tep);
31267c478bd9Sstevel@tonic-gate 	tl_serializer_enter(tep, tl_conn_req_ser, mp);
31277c478bd9Sstevel@tonic-gate }
31287c478bd9Sstevel@tonic-gate 
31297c478bd9Sstevel@tonic-gate /*
31307c478bd9Sstevel@tonic-gate  * Finish T_CONN_REQ processing on listener serializer.
31317c478bd9Sstevel@tonic-gate  */
31327c478bd9Sstevel@tonic-gate static void
tl_conn_req_ser(mblk_t * mp,tl_endpt_t * tep)31337c478bd9Sstevel@tonic-gate tl_conn_req_ser(mblk_t *mp, tl_endpt_t *tep)
31347c478bd9Sstevel@tonic-gate {
31357c478bd9Sstevel@tonic-gate 	queue_t		*wq;
31367c478bd9Sstevel@tonic-gate 	tl_endpt_t	*peer_tep = tep->te_oconp;
31377c478bd9Sstevel@tonic-gate 	mblk_t		*confmp, *cimp, *indmp;
31387c478bd9Sstevel@tonic-gate 	void		*opts = NULL;
31397c478bd9Sstevel@tonic-gate 	mblk_t		*ackmp = mp;
31407c478bd9Sstevel@tonic-gate 	struct T_conn_req	*creq = (struct T_conn_req *)mp->b_cont->b_rptr;
31417c478bd9Sstevel@tonic-gate 	struct T_conn_ind	*ci;
31427c478bd9Sstevel@tonic-gate 	tl_icon_t	*tip;
31437c478bd9Sstevel@tonic-gate 	void		*addr_startp;
31447c478bd9Sstevel@tonic-gate 	t_scalar_t	olen = creq->OPT_length;
31457c478bd9Sstevel@tonic-gate 	t_scalar_t	ooff = creq->OPT_offset;
31469173eaebSToomas Soome 	size_t		ci_msz;
31477c478bd9Sstevel@tonic-gate 	size_t		size;
314867dbe2beSCasper H.S. Dik 	cred_t		*cr = NULL;
314967dbe2beSCasper H.S. Dik 	pid_t		cpid;
31507c478bd9Sstevel@tonic-gate 
31517c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
31527c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_oconp);
31537c478bd9Sstevel@tonic-gate 		tl_serializer_exit(tep);
31547c478bd9Sstevel@tonic-gate 		tl_refrele(tep);
31557c478bd9Sstevel@tonic-gate 		freemsg(mp);
31567c478bd9Sstevel@tonic-gate 		return;
31577c478bd9Sstevel@tonic-gate 	}
31587c478bd9Sstevel@tonic-gate 
31597c478bd9Sstevel@tonic-gate 	wq = tep->te_wq;
31607c478bd9Sstevel@tonic-gate 	tep->te_flag |= TL_EAGER;
31617c478bd9Sstevel@tonic-gate 
31627c478bd9Sstevel@tonic-gate 	/*
31637c478bd9Sstevel@tonic-gate 	 * Extract preallocated ackmp from mp.
31647c478bd9Sstevel@tonic-gate 	 */
31657c478bd9Sstevel@tonic-gate 	mp = mp->b_cont;
31667c478bd9Sstevel@tonic-gate 	ackmp->b_cont = NULL;
31677c478bd9Sstevel@tonic-gate 
31687c478bd9Sstevel@tonic-gate 	if (olen == 0)
31697c478bd9Sstevel@tonic-gate 		ooff = 0;
31707c478bd9Sstevel@tonic-gate 
31717c478bd9Sstevel@tonic-gate 	if (peer_tep->te_closing ||
31727c478bd9Sstevel@tonic-gate 	    !((peer_tep->te_state == TS_IDLE) ||
3173fc80c0dfSnordmark 	    (peer_tep->te_state == TS_WRES_CIND))) {
3174b6626b4dSakolb 		(void) (STRLOG(TL_ID, tep->te_minor, 2, SL_TRACE | SL_ERROR,
3175fc80c0dfSnordmark 		    "tl_conn_req:peer in bad state (%d)",
3176fc80c0dfSnordmark 		    peer_tep->te_state));
31777c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_oconp);
31787c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, mp, TSYSERR, ECONNREFUSED, T_CONN_REQ);
31797c478bd9Sstevel@tonic-gate 		freemsg(ackmp);
31807c478bd9Sstevel@tonic-gate 		tl_serializer_exit(tep);
31817c478bd9Sstevel@tonic-gate 		tl_refrele(tep);
31827c478bd9Sstevel@tonic-gate 		return;
31837c478bd9Sstevel@tonic-gate 	}
31847c478bd9Sstevel@tonic-gate 
31857c478bd9Sstevel@tonic-gate 	/*
31867c478bd9Sstevel@tonic-gate 	 * preallocate now for T_DISCON_IND or T_CONN_IND
31877c478bd9Sstevel@tonic-gate 	 */
31887c478bd9Sstevel@tonic-gate 	/*
31897c478bd9Sstevel@tonic-gate 	 * calculate length of T_CONN_IND message
31907c478bd9Sstevel@tonic-gate 	 */
3191cb492d90SToomas Soome 	if (peer_tep->te_flag & (TL_SETCRED | TL_SETUCRED)) {
319267dbe2beSCasper H.S. Dik 		cr = msg_getcred(mp, &cpid);
319367dbe2beSCasper H.S. Dik 		ASSERT(cr != NULL);
319467dbe2beSCasper H.S. Dik 		if (peer_tep->te_flag & TL_SETCRED) {
319567dbe2beSCasper H.S. Dik 			ooff = 0;
319667dbe2beSCasper H.S. Dik 			olen = (t_scalar_t) sizeof (struct opthdr) +
319767dbe2beSCasper H.S. Dik 			    OPTLEN(sizeof (tl_credopt_t));
319867dbe2beSCasper H.S. Dik 			/* 1 option only */
319967dbe2beSCasper H.S. Dik 		} else {
320067dbe2beSCasper H.S. Dik 			ooff = 0;
320167dbe2beSCasper H.S. Dik 			olen = (t_scalar_t)sizeof (struct opthdr) +
320267dbe2beSCasper H.S. Dik 			    OPTLEN(ucredminsize(cr));
320367dbe2beSCasper H.S. Dik 			/* 1 option only */
320467dbe2beSCasper H.S. Dik 		}
32057c478bd9Sstevel@tonic-gate 	}
32067c478bd9Sstevel@tonic-gate 	ci_msz = sizeof (struct T_conn_ind) + tep->te_alen;
32077c478bd9Sstevel@tonic-gate 	ci_msz = T_ALIGN(ci_msz) + olen;
32087c478bd9Sstevel@tonic-gate 	size = max(ci_msz, sizeof (struct T_discon_ind));
32097c478bd9Sstevel@tonic-gate 
32107c478bd9Sstevel@tonic-gate 	/*
32117c478bd9Sstevel@tonic-gate 	 * Save options from mp - we'll need them for T_CONN_IND.
32127c478bd9Sstevel@tonic-gate 	 */
32137c478bd9Sstevel@tonic-gate 	if (ooff != 0) {
32147c478bd9Sstevel@tonic-gate 		opts = kmem_alloc(olen, KM_NOSLEEP);
32157c478bd9Sstevel@tonic-gate 		if (opts == NULL) {
32167c478bd9Sstevel@tonic-gate 			/*
32177c478bd9Sstevel@tonic-gate 			 * roll back state changes
32187c478bd9Sstevel@tonic-gate 			 */
32197c478bd9Sstevel@tonic-gate 			tep->te_state = TS_IDLE;
32207c478bd9Sstevel@tonic-gate 			tl_memrecover(wq, mp, size);
32217c478bd9Sstevel@tonic-gate 			freemsg(ackmp);
32227c478bd9Sstevel@tonic-gate 			TL_UNCONNECT(tep->te_oconp);
32237c478bd9Sstevel@tonic-gate 			tl_serializer_exit(tep);
32247c478bd9Sstevel@tonic-gate 			tl_refrele(tep);
32257c478bd9Sstevel@tonic-gate 			return;
32267c478bd9Sstevel@tonic-gate 		}
32277c478bd9Sstevel@tonic-gate 		/* Copy options to a temp buffer */
32287c478bd9Sstevel@tonic-gate 		bcopy(mp->b_rptr + ooff, opts, olen);
32297c478bd9Sstevel@tonic-gate 	}
32307c478bd9Sstevel@tonic-gate 
32317c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(tep) && !tl_disable_early_connect) {
32327c478bd9Sstevel@tonic-gate 		/*
32337c478bd9Sstevel@tonic-gate 		 * Generate a T_CONN_CON that has the identical address
32347c478bd9Sstevel@tonic-gate 		 * (and options) as the T_CONN_REQ.
32357c478bd9Sstevel@tonic-gate 		 * NOTE: assumes that the T_conn_req and T_conn_con structures
32367c478bd9Sstevel@tonic-gate 		 * are isomorphic.
32377c478bd9Sstevel@tonic-gate 		 */
32387c478bd9Sstevel@tonic-gate 		confmp = copyb(mp);
3239cb492d90SToomas Soome 		if (confmp == NULL) {
32407c478bd9Sstevel@tonic-gate 			/*
32417c478bd9Sstevel@tonic-gate 			 * roll back state changes
32427c478bd9Sstevel@tonic-gate 			 */
32437c478bd9Sstevel@tonic-gate 			tep->te_state = TS_IDLE;
32447c478bd9Sstevel@tonic-gate 			tl_memrecover(wq, mp, mp->b_wptr - mp->b_rptr);
32457c478bd9Sstevel@tonic-gate 			freemsg(ackmp);
32467c478bd9Sstevel@tonic-gate 			if (opts != NULL)
32477c478bd9Sstevel@tonic-gate 				kmem_free(opts, olen);
32487c478bd9Sstevel@tonic-gate 			TL_UNCONNECT(tep->te_oconp);
32497c478bd9Sstevel@tonic-gate 			tl_serializer_exit(tep);
32507c478bd9Sstevel@tonic-gate 			tl_refrele(tep);
32517c478bd9Sstevel@tonic-gate 			return;
32527c478bd9Sstevel@tonic-gate 		}
32537c478bd9Sstevel@tonic-gate 		((struct T_conn_con *)(confmp->b_rptr))->PRIM_type =
3254fc80c0dfSnordmark 		    T_CONN_CON;
32557c478bd9Sstevel@tonic-gate 	} else {
32567c478bd9Sstevel@tonic-gate 		confmp = NULL;
32577c478bd9Sstevel@tonic-gate 	}
32587c478bd9Sstevel@tonic-gate 	if ((indmp = reallocb(mp, size, 0)) == NULL) {
32597c478bd9Sstevel@tonic-gate 		/*
32607c478bd9Sstevel@tonic-gate 		 * roll back state changes
32617c478bd9Sstevel@tonic-gate 		 */
32627c478bd9Sstevel@tonic-gate 		tep->te_state = TS_IDLE;
32637c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, size);
32647c478bd9Sstevel@tonic-gate 		freemsg(ackmp);
32657c478bd9Sstevel@tonic-gate 		if (opts != NULL)
32667c478bd9Sstevel@tonic-gate 			kmem_free(opts, olen);
32677c478bd9Sstevel@tonic-gate 		freemsg(confmp);
32687c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_oconp);
32697c478bd9Sstevel@tonic-gate 		tl_serializer_exit(tep);
32707c478bd9Sstevel@tonic-gate 		tl_refrele(tep);
32717c478bd9Sstevel@tonic-gate 		return;
32727c478bd9Sstevel@tonic-gate 	}
32737c478bd9Sstevel@tonic-gate 
32747c478bd9Sstevel@tonic-gate 	tip = kmem_zalloc(sizeof (*tip), KM_NOSLEEP);
32757c478bd9Sstevel@tonic-gate 	if (tip == NULL) {
32767c478bd9Sstevel@tonic-gate 		/*
32777c478bd9Sstevel@tonic-gate 		 * roll back state changes
32787c478bd9Sstevel@tonic-gate 		 */
32797c478bd9Sstevel@tonic-gate 		tep->te_state = TS_IDLE;
32807c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, indmp, sizeof (*tip));
32817c478bd9Sstevel@tonic-gate 		freemsg(ackmp);
32827c478bd9Sstevel@tonic-gate 		if (opts != NULL)
32837c478bd9Sstevel@tonic-gate 			kmem_free(opts, olen);
32847c478bd9Sstevel@tonic-gate 		freemsg(confmp);
32857c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_oconp);
32867c478bd9Sstevel@tonic-gate 		tl_serializer_exit(tep);
32877c478bd9Sstevel@tonic-gate 		tl_refrele(tep);
32887c478bd9Sstevel@tonic-gate 		return;
32897c478bd9Sstevel@tonic-gate 	}
32907c478bd9Sstevel@tonic-gate 	tip->ti_mp = NULL;
32917c478bd9Sstevel@tonic-gate 
32927c478bd9Sstevel@tonic-gate 	/*
32937c478bd9Sstevel@tonic-gate 	 * memory is now committed for T_DISCON_IND/T_CONN_IND/T_CONN_CON
32947c478bd9Sstevel@tonic-gate 	 * and tl_icon_t cell.
32957c478bd9Sstevel@tonic-gate 	 */
32967c478bd9Sstevel@tonic-gate 
32977c478bd9Sstevel@tonic-gate 	/*
32987c478bd9Sstevel@tonic-gate 	 * ack validity of request and send the peer credential in the ACK.
32997c478bd9Sstevel@tonic-gate 	 */
3300b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_OK_ACK1][tep->te_state];
33017c478bd9Sstevel@tonic-gate 
33027c478bd9Sstevel@tonic-gate 	if (peer_tep != NULL && peer_tep->te_credp != NULL &&
33037c478bd9Sstevel@tonic-gate 	    confmp != NULL) {
3304de8c4a14SErik Nordmark 		mblk_setcred(confmp, peer_tep->te_credp, peer_tep->te_cpid);
33057c478bd9Sstevel@tonic-gate 	}
33067c478bd9Sstevel@tonic-gate 
33077c478bd9Sstevel@tonic-gate 	tl_ok_ack(wq, ackmp, T_CONN_REQ);
33087c478bd9Sstevel@tonic-gate 
33097c478bd9Sstevel@tonic-gate 	/*
33107c478bd9Sstevel@tonic-gate 	 * prepare message to send T_CONN_IND
33117c478bd9Sstevel@tonic-gate 	 */
33127c478bd9Sstevel@tonic-gate 	/*
33137c478bd9Sstevel@tonic-gate 	 * allocate the message - original data blocks retained
33147c478bd9Sstevel@tonic-gate 	 * in the returned mblk
33157c478bd9Sstevel@tonic-gate 	 */
33167c478bd9Sstevel@tonic-gate 	cimp = tl_resizemp(indmp, size);
3317cb492d90SToomas Soome 	if (cimp == NULL) {
3318cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE | SL_ERROR,
3319fc80c0dfSnordmark 		    "tl_conn_req:con_ind:allocb failure"));
33207c478bd9Sstevel@tonic-gate 		tl_merror(wq, indmp, ENOMEM);
33217c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_oconp);
33227c478bd9Sstevel@tonic-gate 		tl_serializer_exit(tep);
33237c478bd9Sstevel@tonic-gate 		tl_refrele(tep);
33247c478bd9Sstevel@tonic-gate 		if (opts != NULL)
33257c478bd9Sstevel@tonic-gate 			kmem_free(opts, olen);
33267c478bd9Sstevel@tonic-gate 		freemsg(confmp);
33277c478bd9Sstevel@tonic-gate 		ASSERT(tip->ti_mp == NULL);
33287c478bd9Sstevel@tonic-gate 		kmem_free(tip, sizeof (*tip));
33297c478bd9Sstevel@tonic-gate 		return;
33307c478bd9Sstevel@tonic-gate 	}
33317c478bd9Sstevel@tonic-gate 
33327c478bd9Sstevel@tonic-gate 	DB_TYPE(cimp) = M_PROTO;
33337c478bd9Sstevel@tonic-gate 	ci = (struct T_conn_ind *)cimp->b_rptr;
33347c478bd9Sstevel@tonic-gate 	ci->PRIM_type  = T_CONN_IND;
33357c478bd9Sstevel@tonic-gate 	ci->SRC_offset = (t_scalar_t)sizeof (struct T_conn_ind);
33367c478bd9Sstevel@tonic-gate 	ci->SRC_length = tep->te_alen;
33377c478bd9Sstevel@tonic-gate 	ci->SEQ_number = tep->te_seqno;
33387c478bd9Sstevel@tonic-gate 
33397c478bd9Sstevel@tonic-gate 	addr_startp = cimp->b_rptr + ci->SRC_offset;
33407c478bd9Sstevel@tonic-gate 	bcopy(tep->te_abuf, addr_startp, tep->te_alen);
33417c478bd9Sstevel@tonic-gate 	if (peer_tep->te_flag & (TL_SETCRED|TL_SETUCRED)) {
3342de8c4a14SErik Nordmark 
33437c478bd9Sstevel@tonic-gate 		ci->OPT_offset = (t_scalar_t)T_ALIGN(ci->SRC_offset +
3344fc80c0dfSnordmark 		    ci->SRC_length);
33457c478bd9Sstevel@tonic-gate 		ci->OPT_length = olen; /* because only 1 option */
33467c478bd9Sstevel@tonic-gate 		tl_fill_option(cimp->b_rptr + ci->OPT_offset,
3347de8c4a14SErik Nordmark 		    cr, cpid,
3348fc80c0dfSnordmark 		    peer_tep->te_flag, peer_tep->te_credp);
33497c478bd9Sstevel@tonic-gate 	} else if (ooff != 0) {
33507c478bd9Sstevel@tonic-gate 		/* Copy option from T_CONN_REQ */
33517c478bd9Sstevel@tonic-gate 		ci->OPT_offset = (t_scalar_t)T_ALIGN(ci->SRC_offset +
3352fc80c0dfSnordmark 		    ci->SRC_length);
33537c478bd9Sstevel@tonic-gate 		ci->OPT_length = olen;
33547c478bd9Sstevel@tonic-gate 		ASSERT(opts != NULL);
33557c478bd9Sstevel@tonic-gate 		bcopy(opts, (void *)((uintptr_t)ci + ci->OPT_offset), olen);
33567c478bd9Sstevel@tonic-gate 	} else {
33577c478bd9Sstevel@tonic-gate 		ci->OPT_offset = 0;
33587c478bd9Sstevel@tonic-gate 		ci->OPT_length = 0;
33597c478bd9Sstevel@tonic-gate 	}
33607c478bd9Sstevel@tonic-gate 	if (opts != NULL)
33617c478bd9Sstevel@tonic-gate 		kmem_free(opts, olen);
33627c478bd9Sstevel@tonic-gate 
33637c478bd9Sstevel@tonic-gate 	/*
33647c478bd9Sstevel@tonic-gate 	 * register connection request with server peer
33657c478bd9Sstevel@tonic-gate 	 * append to list of incoming connections
33667c478bd9Sstevel@tonic-gate 	 * increment references for both peer_tep and tep: peer_tep is placed on
33677c478bd9Sstevel@tonic-gate 	 * te_oconp and tep is placed on listeners queue.
33687c478bd9Sstevel@tonic-gate 	 */
33697c478bd9Sstevel@tonic-gate 	tip->ti_tep = tep;
33707c478bd9Sstevel@tonic-gate 	tip->ti_seqno = tep->te_seqno;
33717c478bd9Sstevel@tonic-gate 	list_insert_tail(&peer_tep->te_iconp, tip);
33727c478bd9Sstevel@tonic-gate 	peer_tep->te_nicon++;
33737c478bd9Sstevel@tonic-gate 
3374b8ffbd31SJohn Levon 	peer_tep->te_state = nextstate[TE_CONN_IND][peer_tep->te_state];
33757c478bd9Sstevel@tonic-gate 	/*
33767c478bd9Sstevel@tonic-gate 	 * send the T_CONN_IND message
33777c478bd9Sstevel@tonic-gate 	 */
33787c478bd9Sstevel@tonic-gate 	putnext(peer_tep->te_rq, cimp);
33797c478bd9Sstevel@tonic-gate 
33807c478bd9Sstevel@tonic-gate 	/*
33817c478bd9Sstevel@tonic-gate 	 * Send a T_CONN_CON message for sockets.
33827c478bd9Sstevel@tonic-gate 	 * Disable the queues until we have reached the correct state!
33837c478bd9Sstevel@tonic-gate 	 */
33847c478bd9Sstevel@tonic-gate 	if (confmp != NULL) {
3385b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_CONN_CON][tep->te_state];
33867c478bd9Sstevel@tonic-gate 		noenable(wq);
33877c478bd9Sstevel@tonic-gate 		putnext(tep->te_rq, confmp);
33887c478bd9Sstevel@tonic-gate 	}
33897c478bd9Sstevel@tonic-gate 	/*
33907c478bd9Sstevel@tonic-gate 	 * Now we need to increment tep reference because tep is referenced by
33917c478bd9Sstevel@tonic-gate 	 * server list of pending connections. We also need to decrement
33927c478bd9Sstevel@tonic-gate 	 * reference before exiting serializer. Two operations void each other
33937c478bd9Sstevel@tonic-gate 	 * so we don't modify reference at all.
33947c478bd9Sstevel@tonic-gate 	 */
33957c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_refcnt >= 2);
33967c478bd9Sstevel@tonic-gate 	ASSERT(peer_tep->te_refcnt >= 2);
33977c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
33987c478bd9Sstevel@tonic-gate }
33997c478bd9Sstevel@tonic-gate 
34007c478bd9Sstevel@tonic-gate 
34017c478bd9Sstevel@tonic-gate 
34027c478bd9Sstevel@tonic-gate /*
34037c478bd9Sstevel@tonic-gate  * Handle T_conn_res on listener stream. Called on listener serializer.
34047c478bd9Sstevel@tonic-gate  * tl_conn_req has already generated the T_CONN_CON.
34057c478bd9Sstevel@tonic-gate  * tl_conn_res is called on listener serializer.
34067c478bd9Sstevel@tonic-gate  * No one accesses acceptor at this point, so it is safe to modify acceptor.
34077c478bd9Sstevel@tonic-gate  * Switch eager serializer to acceptor's.
34087c478bd9Sstevel@tonic-gate  *
34097c478bd9Sstevel@tonic-gate  * If TL_SET[U]CRED generate the credentials options.
34107c478bd9Sstevel@tonic-gate  * For sockets tl_conn_req has already generated the T_CONN_CON.
34117c478bd9Sstevel@tonic-gate  */
34127c478bd9Sstevel@tonic-gate static void
tl_conn_res(mblk_t * mp,tl_endpt_t * tep)34137c478bd9Sstevel@tonic-gate tl_conn_res(mblk_t *mp, tl_endpt_t *tep)
34147c478bd9Sstevel@tonic-gate {
34157c478bd9Sstevel@tonic-gate 	queue_t			*wq;
34167c478bd9Sstevel@tonic-gate 	struct T_conn_res	*cres = (struct T_conn_res *)mp->b_rptr;
34177c478bd9Sstevel@tonic-gate 	ssize_t			msz = MBLKL(mp);
34187c478bd9Sstevel@tonic-gate 	t_scalar_t		olen, ooff, err = 0;
34197c478bd9Sstevel@tonic-gate 	t_scalar_t		prim = cres->PRIM_type;
34207c478bd9Sstevel@tonic-gate 	uchar_t			*addr_startp;
34219173eaebSToomas Soome 	tl_endpt_t		*acc_ep = NULL, *cl_ep = NULL;
34227c478bd9Sstevel@tonic-gate 	tl_icon_t		*tip;
34237c478bd9Sstevel@tonic-gate 	size_t			size;
34247c478bd9Sstevel@tonic-gate 	mblk_t			*ackmp, *respmp;
34257c478bd9Sstevel@tonic-gate 	mblk_t			*dimp, *ccmp = NULL;
34267c478bd9Sstevel@tonic-gate 	struct T_discon_ind	*di;
34277c478bd9Sstevel@tonic-gate 	struct T_conn_con	*cc;
34287c478bd9Sstevel@tonic-gate 	boolean_t		client_noclose_set = B_FALSE;
34297c478bd9Sstevel@tonic-gate 	boolean_t		switch_client_serializer = B_TRUE;
34307c478bd9Sstevel@tonic-gate 
34317c478bd9Sstevel@tonic-gate 	ASSERT(IS_COTS(tep));
34327c478bd9Sstevel@tonic-gate 
34337c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
34347c478bd9Sstevel@tonic-gate 		freemsg(mp);
34357c478bd9Sstevel@tonic-gate 		return;
34367c478bd9Sstevel@tonic-gate 	}
34377c478bd9Sstevel@tonic-gate 
34387c478bd9Sstevel@tonic-gate 	wq = tep->te_wq;
34397c478bd9Sstevel@tonic-gate 
34407c478bd9Sstevel@tonic-gate 	/*
34417c478bd9Sstevel@tonic-gate 	 * preallocate memory for:
34427c478bd9Sstevel@tonic-gate 	 * 1. max of T_ERROR_ACK and T_OK_ACK
34437c478bd9Sstevel@tonic-gate 	 *	==> known max T_ERROR_ACK
34447c478bd9Sstevel@tonic-gate 	 * 2. max of T_DISCON_IND and T_CONN_CON
34457c478bd9Sstevel@tonic-gate 	 */
34467c478bd9Sstevel@tonic-gate 	ackmp = allocb(sizeof (struct T_error_ack), BPRI_MED);
3447cb492d90SToomas Soome 	if (ackmp == NULL) {
34487c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, sizeof (struct T_error_ack));
34497c478bd9Sstevel@tonic-gate 		return;
34507c478bd9Sstevel@tonic-gate 	}
34517c478bd9Sstevel@tonic-gate 	/*
34527c478bd9Sstevel@tonic-gate 	 * memory committed for T_OK_ACK/T_ERROR_ACK now
34537c478bd9Sstevel@tonic-gate 	 * will be committed for T_DISCON_IND/T_CONN_CON later
34547c478bd9Sstevel@tonic-gate 	 */
34557c478bd9Sstevel@tonic-gate 
34567c478bd9Sstevel@tonic-gate 
34577c478bd9Sstevel@tonic-gate 	ASSERT(prim == T_CONN_RES || prim == O_T_CONN_RES);
34587c478bd9Sstevel@tonic-gate 
34597c478bd9Sstevel@tonic-gate 	/*
34607c478bd9Sstevel@tonic-gate 	 * validate state
34617c478bd9Sstevel@tonic-gate 	 */
34627c478bd9Sstevel@tonic-gate 	if (tep->te_state != TS_WRES_CIND) {
34637c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
3464cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
3465fc80c0dfSnordmark 		    "tl_wput:T_CONN_RES:out of state, state=%d",
3466fc80c0dfSnordmark 		    tep->te_state));
34677c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TOUTSTATE, 0, prim);
34687c478bd9Sstevel@tonic-gate 		freemsg(mp);
34697c478bd9Sstevel@tonic-gate 		return;
34707c478bd9Sstevel@tonic-gate 	}
34717c478bd9Sstevel@tonic-gate 
34727c478bd9Sstevel@tonic-gate 	/*
34737c478bd9Sstevel@tonic-gate 	 * validate the message
34747c478bd9Sstevel@tonic-gate 	 * Note: dereference fields in struct inside message only
34757c478bd9Sstevel@tonic-gate 	 * after validating the message length.
34767c478bd9Sstevel@tonic-gate 	 */
34777c478bd9Sstevel@tonic-gate 	if (msz < sizeof (struct T_conn_res)) {
3478cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
3479fc80c0dfSnordmark 		    "tl_conn_res:invalid message length"));
34807c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TSYSERR, EINVAL, prim);
34817c478bd9Sstevel@tonic-gate 		freemsg(mp);
34827c478bd9Sstevel@tonic-gate 		return;
34837c478bd9Sstevel@tonic-gate 	}
34847c478bd9Sstevel@tonic-gate 	olen = cres->OPT_length;
34857c478bd9Sstevel@tonic-gate 	ooff = cres->OPT_offset;
34867c478bd9Sstevel@tonic-gate 	if (((olen > 0) && ((ooff + olen) > msz))) {
3487cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
3488fc80c0dfSnordmark 		    "tl_conn_res:invalid message"));
34897c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TSYSERR, EINVAL, prim);
34907c478bd9Sstevel@tonic-gate 		freemsg(mp);
34917c478bd9Sstevel@tonic-gate 		return;
34927c478bd9Sstevel@tonic-gate 	}
34937c478bd9Sstevel@tonic-gate 	if (olen) {
34947c478bd9Sstevel@tonic-gate 		/*
34957c478bd9Sstevel@tonic-gate 		 * no opts in connect res
34967c478bd9Sstevel@tonic-gate 		 * supported in this provider
34977c478bd9Sstevel@tonic-gate 		 */
3498cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
3499fc80c0dfSnordmark 		    "tl_conn_res:options not supported in message"));
35007c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TBADOPT, 0, prim);
35017c478bd9Sstevel@tonic-gate 		freemsg(mp);
35027c478bd9Sstevel@tonic-gate 		return;
35037c478bd9Sstevel@tonic-gate 	}
35047c478bd9Sstevel@tonic-gate 
3505b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_CONN_RES][tep->te_state];
35067c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_state == TS_WACK_CRES);
35077c478bd9Sstevel@tonic-gate 
35087c478bd9Sstevel@tonic-gate 	if (cres->SEQ_number < TL_MINOR_START &&
3509fc80c0dfSnordmark 	    cres->SEQ_number >= BADSEQNUM) {
3510cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 2, SL_TRACE | SL_ERROR,
3511fc80c0dfSnordmark 		    "tl_conn_res:remote endpoint sequence number bad"));
3512b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
35137c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TBADSEQ, 0, prim);
35147c478bd9Sstevel@tonic-gate 		freemsg(mp);
35157c478bd9Sstevel@tonic-gate 		return;
35167c478bd9Sstevel@tonic-gate 	}
35177c478bd9Sstevel@tonic-gate 
35187c478bd9Sstevel@tonic-gate 	/*
35197c478bd9Sstevel@tonic-gate 	 * find accepting endpoint. Will have extra reference if found.
35207c478bd9Sstevel@tonic-gate 	 */
35217c478bd9Sstevel@tonic-gate 	if (mod_hash_find_cb(tep->te_transport->tr_ai_hash,
3522fc80c0dfSnordmark 	    (mod_hash_key_t)(uintptr_t)cres->ACCEPTOR_id,
3523fc80c0dfSnordmark 	    (mod_hash_val_t *)&acc_ep, tl_find_callback) != 0) {
3524cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 2, SL_TRACE | SL_ERROR,
3525fc80c0dfSnordmark 		    "tl_conn_res:bad accepting endpoint"));
3526b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
35277c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TBADF, 0, prim);
35287c478bd9Sstevel@tonic-gate 		freemsg(mp);
35297c478bd9Sstevel@tonic-gate 		return;
35307c478bd9Sstevel@tonic-gate 	}
35317c478bd9Sstevel@tonic-gate 
35327c478bd9Sstevel@tonic-gate 	/*
35337c478bd9Sstevel@tonic-gate 	 * Prevent acceptor from closing.
35347c478bd9Sstevel@tonic-gate 	 */
3535cb492d90SToomas Soome 	if (!tl_noclose(acc_ep)) {
3536cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 2, SL_TRACE | SL_ERROR,
3537fc80c0dfSnordmark 		    "tl_conn_res:bad accepting endpoint"));
3538b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
35397c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TBADF, 0, prim);
35407c478bd9Sstevel@tonic-gate 		tl_refrele(acc_ep);
35417c478bd9Sstevel@tonic-gate 		freemsg(mp);
35427c478bd9Sstevel@tonic-gate 		return;
35437c478bd9Sstevel@tonic-gate 	}
35447c478bd9Sstevel@tonic-gate 
35457c478bd9Sstevel@tonic-gate 	acc_ep->te_flag |= TL_ACCEPTOR;
35467c478bd9Sstevel@tonic-gate 
35477c478bd9Sstevel@tonic-gate 	/*
35487c478bd9Sstevel@tonic-gate 	 * validate that accepting endpoint, if different from listening
35497c478bd9Sstevel@tonic-gate 	 * has address bound => state is TS_IDLE
35507c478bd9Sstevel@tonic-gate 	 * TROUBLE in XPG4 !!?
35517c478bd9Sstevel@tonic-gate 	 */
35527c478bd9Sstevel@tonic-gate 	if ((tep != acc_ep) && (acc_ep->te_state != TS_IDLE)) {
3553cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 2, SL_TRACE | SL_ERROR,
3554fc80c0dfSnordmark 		    "tl_conn_res:accepting endpoint has no address bound,"
3555fc80c0dfSnordmark 		    "state=%d", acc_ep->te_state));
3556b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
35577c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TOUTSTATE, 0, prim);
35587c478bd9Sstevel@tonic-gate 		freemsg(mp);
35597c478bd9Sstevel@tonic-gate 		tl_closeok(acc_ep);
35607c478bd9Sstevel@tonic-gate 		tl_refrele(acc_ep);
35617c478bd9Sstevel@tonic-gate 		return;
35627c478bd9Sstevel@tonic-gate 	}
35637c478bd9Sstevel@tonic-gate 
35647c478bd9Sstevel@tonic-gate 	/*
35657c478bd9Sstevel@tonic-gate 	 * validate if accepting endpt same as listening, then
35667c478bd9Sstevel@tonic-gate 	 * no other incoming connection should be on the queue
35677c478bd9Sstevel@tonic-gate 	 */
35687c478bd9Sstevel@tonic-gate 
35697c478bd9Sstevel@tonic-gate 	if ((tep == acc_ep) && (tep->te_nicon > 1)) {
3570cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE | SL_ERROR,
3571fc80c0dfSnordmark 		    "tl_conn_res: > 1 conn_ind on listener-acceptor"));
3572b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
35737c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TBADF, 0, prim);
35747c478bd9Sstevel@tonic-gate 		freemsg(mp);
35757c478bd9Sstevel@tonic-gate 		tl_closeok(acc_ep);
35767c478bd9Sstevel@tonic-gate 		tl_refrele(acc_ep);
35777c478bd9Sstevel@tonic-gate 		return;
35787c478bd9Sstevel@tonic-gate 	}
35797c478bd9Sstevel@tonic-gate 
35807c478bd9Sstevel@tonic-gate 	/*
35817c478bd9Sstevel@tonic-gate 	 * Mark for deletion, the entry corresponding to client
35827c478bd9Sstevel@tonic-gate 	 * on list of pending connections made by the listener
35837c478bd9Sstevel@tonic-gate 	 *  search list to see if client is one of the
35847c478bd9Sstevel@tonic-gate 	 * recorded as a listener.
35857c478bd9Sstevel@tonic-gate 	 */
35867c478bd9Sstevel@tonic-gate 	tip = tl_icon_find(tep, cres->SEQ_number);
35877c478bd9Sstevel@tonic-gate 	if (tip == NULL) {
3588cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 2, SL_TRACE | SL_ERROR,
3589fc80c0dfSnordmark 		    "tl_conn_res:no client in listener list"));
3590b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_ERROR_ACK][tep->te_state];
35917c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TBADSEQ, 0, prim);
35927c478bd9Sstevel@tonic-gate 		freemsg(mp);
35937c478bd9Sstevel@tonic-gate 		tl_closeok(acc_ep);
35947c478bd9Sstevel@tonic-gate 		tl_refrele(acc_ep);
35957c478bd9Sstevel@tonic-gate 		return;
35967c478bd9Sstevel@tonic-gate 	}
35977c478bd9Sstevel@tonic-gate 
35987c478bd9Sstevel@tonic-gate 	/*
35997c478bd9Sstevel@tonic-gate 	 * If ti_tep is NULL the client has already closed. In this case
36007c478bd9Sstevel@tonic-gate 	 * the code below will avoid any action on the client side
36017c478bd9Sstevel@tonic-gate 	 * but complete the server and acceptor state transitions.
36027c478bd9Sstevel@tonic-gate 	 */
36037c478bd9Sstevel@tonic-gate 	ASSERT(tip->ti_tep == NULL ||
3604fc80c0dfSnordmark 	    tip->ti_tep->te_seqno == cres->SEQ_number);
36057c478bd9Sstevel@tonic-gate 	cl_ep = tip->ti_tep;
36067c478bd9Sstevel@tonic-gate 
36077c478bd9Sstevel@tonic-gate 	/*
36087c478bd9Sstevel@tonic-gate 	 * If the client is present it is switched from listener's to acceptor's
36097c478bd9Sstevel@tonic-gate 	 * serializer. We should block client closes while serializers are
36107c478bd9Sstevel@tonic-gate 	 * being switched.
36117c478bd9Sstevel@tonic-gate 	 *
36127c478bd9Sstevel@tonic-gate 	 * It is possible that the client is present but is currently being
36137c478bd9Sstevel@tonic-gate 	 * closed. There are two possible cases:
36147c478bd9Sstevel@tonic-gate 	 *
36157c478bd9Sstevel@tonic-gate 	 * 1) The client has already entered tl_close_finish_ser() and sent
36167c478bd9Sstevel@tonic-gate 	 *    T_ORDREL_IND. In this case we can just ignore the client (but we
36177c478bd9Sstevel@tonic-gate 	 *    still need to send all messages from tip->ti_mp to the acceptor).
36187c478bd9Sstevel@tonic-gate 	 *
36197c478bd9Sstevel@tonic-gate 	 * 2) The client started the close but has not entered
36207c478bd9Sstevel@tonic-gate 	 *    tl_close_finish_ser() yet. In this case, the client is already
36217c478bd9Sstevel@tonic-gate 	 *    proceeding asynchronously on the listener's serializer, so we're
36227c478bd9Sstevel@tonic-gate 	 *    forced to change the acceptor to use the listener's serializer to
36237c478bd9Sstevel@tonic-gate 	 *    ensure that any operations on the acceptor are serialized with
36247c478bd9Sstevel@tonic-gate 	 *    respect to the close that's in-progress.
36257c478bd9Sstevel@tonic-gate 	 */
36267c478bd9Sstevel@tonic-gate 	if (cl_ep != NULL) {
36277c478bd9Sstevel@tonic-gate 		if (tl_noclose(cl_ep)) {
36287c478bd9Sstevel@tonic-gate 			client_noclose_set = B_TRUE;
36297c478bd9Sstevel@tonic-gate 		} else {
36307c478bd9Sstevel@tonic-gate 			/*
36317c478bd9Sstevel@tonic-gate 			 * Client is closing. If it it has sent the
36327c478bd9Sstevel@tonic-gate 			 * T_ORDREL_IND, we can simply ignore it - otherwise,
36337c478bd9Sstevel@tonic-gate 			 * we have to let let the client continue until it is
36347c478bd9Sstevel@tonic-gate 			 * sent.
36357c478bd9Sstevel@tonic-gate 			 *
36367c478bd9Sstevel@tonic-gate 			 * If we do continue using the client, acceptor will
36377c478bd9Sstevel@tonic-gate 			 * switch to client's serializer which is used by client
36387c478bd9Sstevel@tonic-gate 			 * for its close.
36397c478bd9Sstevel@tonic-gate 			 */
36407c478bd9Sstevel@tonic-gate 			tl_client_closing_when_accepting++;
36417c478bd9Sstevel@tonic-gate 			switch_client_serializer = B_FALSE;
36427c478bd9Sstevel@tonic-gate 			if (!IS_SOCKET(cl_ep) || tl_disable_early_connect ||
36437c478bd9Sstevel@tonic-gate 			    cl_ep->te_state == -1)
36447c478bd9Sstevel@tonic-gate 				cl_ep = NULL;
36457c478bd9Sstevel@tonic-gate 		}
36467c478bd9Sstevel@tonic-gate 	}
36477c478bd9Sstevel@tonic-gate 
36487c478bd9Sstevel@tonic-gate 	if (cl_ep != NULL) {
36497c478bd9Sstevel@tonic-gate 		/*
36507c478bd9Sstevel@tonic-gate 		 * validate client state to be TS_WCON_CREQ or TS_DATA_XFER
36517c478bd9Sstevel@tonic-gate 		 * (latter for sockets only)
36527c478bd9Sstevel@tonic-gate 		 */
36537c478bd9Sstevel@tonic-gate 		if (cl_ep->te_state != TS_WCON_CREQ &&
36547c478bd9Sstevel@tonic-gate 		    (cl_ep->te_state != TS_DATA_XFER &&
36557c478bd9Sstevel@tonic-gate 		    IS_SOCKET(cl_ep))) {
36567c478bd9Sstevel@tonic-gate 			err = ECONNREFUSED;
36577c478bd9Sstevel@tonic-gate 			/*
36587c478bd9Sstevel@tonic-gate 			 * T_DISCON_IND sent later after committing memory
36597c478bd9Sstevel@tonic-gate 			 * and acking validity of request
36607c478bd9Sstevel@tonic-gate 			 */
36617c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 2, SL_TRACE,
3662fc80c0dfSnordmark 			    "tl_conn_res:peer in bad state"));
36637c478bd9Sstevel@tonic-gate 		}
36647c478bd9Sstevel@tonic-gate 
36657c478bd9Sstevel@tonic-gate 		/*
36667c478bd9Sstevel@tonic-gate 		 * preallocate now for T_DISCON_IND or T_CONN_CONN
36677c478bd9Sstevel@tonic-gate 		 * ack validity of request (T_OK_ACK) after memory committed
36687c478bd9Sstevel@tonic-gate 		 */
36697c478bd9Sstevel@tonic-gate 
3670cb492d90SToomas Soome 		if (err) {
36717c478bd9Sstevel@tonic-gate 			size = sizeof (struct T_discon_ind);
3672cb492d90SToomas Soome 		} else {
36737c478bd9Sstevel@tonic-gate 			/*
36747c478bd9Sstevel@tonic-gate 			 * calculate length of T_CONN_CON message
36757c478bd9Sstevel@tonic-gate 			 */
36767c478bd9Sstevel@tonic-gate 			olen = 0;
36777c478bd9Sstevel@tonic-gate 			if (cl_ep->te_flag & TL_SETCRED) {
36787c478bd9Sstevel@tonic-gate 				olen = (t_scalar_t)sizeof (struct opthdr) +
3679fc80c0dfSnordmark 				    OPTLEN(sizeof (tl_credopt_t));
36807c478bd9Sstevel@tonic-gate 			} else if (cl_ep->te_flag & TL_SETUCRED) {
36817c478bd9Sstevel@tonic-gate 				olen = (t_scalar_t)sizeof (struct opthdr) +
368267dbe2beSCasper H.S. Dik 				    OPTLEN(ucredminsize(acc_ep->te_credp));
36837c478bd9Sstevel@tonic-gate 			}
36847c478bd9Sstevel@tonic-gate 			size = T_ALIGN(sizeof (struct T_conn_con) +
3685fc80c0dfSnordmark 			    acc_ep->te_alen) + olen;
36867c478bd9Sstevel@tonic-gate 		}
36877c478bd9Sstevel@tonic-gate 		if ((respmp = reallocb(mp, size, 0)) == NULL) {
36887c478bd9Sstevel@tonic-gate 			/*
36897c478bd9Sstevel@tonic-gate 			 * roll back state changes
36907c478bd9Sstevel@tonic-gate 			 */
36917c478bd9Sstevel@tonic-gate 			tep->te_state = TS_WRES_CIND;
36927c478bd9Sstevel@tonic-gate 			tl_memrecover(wq, mp, size);
36937c478bd9Sstevel@tonic-gate 			freemsg(ackmp);
36947c478bd9Sstevel@tonic-gate 			if (client_noclose_set)
36957c478bd9Sstevel@tonic-gate 				tl_closeok(cl_ep);
36967c478bd9Sstevel@tonic-gate 			tl_closeok(acc_ep);
36977c478bd9Sstevel@tonic-gate 			tl_refrele(acc_ep);
36987c478bd9Sstevel@tonic-gate 			return;
36997c478bd9Sstevel@tonic-gate 		}
37007c478bd9Sstevel@tonic-gate 		mp = NULL;
37017c478bd9Sstevel@tonic-gate 	}
37027c478bd9Sstevel@tonic-gate 
37037c478bd9Sstevel@tonic-gate 	/*
37047c478bd9Sstevel@tonic-gate 	 * Now ack validity of request
37057c478bd9Sstevel@tonic-gate 	 */
37067c478bd9Sstevel@tonic-gate 	if (tep->te_nicon == 1) {
37077c478bd9Sstevel@tonic-gate 		if (tep == acc_ep)
3708b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_OK_ACK2][tep->te_state];
37097c478bd9Sstevel@tonic-gate 		else
3710b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_OK_ACK3][tep->te_state];
3711cb492d90SToomas Soome 	} else {
3712b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_OK_ACK4][tep->te_state];
3713cb492d90SToomas Soome 	}
37147c478bd9Sstevel@tonic-gate 
37157c478bd9Sstevel@tonic-gate 	/*
37167c478bd9Sstevel@tonic-gate 	 * send T_DISCON_IND now if client state validation failed earlier
37177c478bd9Sstevel@tonic-gate 	 */
37187c478bd9Sstevel@tonic-gate 	if (err) {
37197c478bd9Sstevel@tonic-gate 		tl_ok_ack(wq, ackmp, prim);
37207c478bd9Sstevel@tonic-gate 		/*
37217c478bd9Sstevel@tonic-gate 		 * flush the queues - why always ?
37227c478bd9Sstevel@tonic-gate 		 */
37237c478bd9Sstevel@tonic-gate 		(void) putnextctl1(acc_ep->te_rq, M_FLUSH, FLUSHR);
37247c478bd9Sstevel@tonic-gate 
37257c478bd9Sstevel@tonic-gate 		dimp = tl_resizemp(respmp, size);
3726cb492d90SToomas Soome 		if (dimp == NULL) {
37277c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
3728cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
3729fc80c0dfSnordmark 			    "tl_conn_res:con_ind:allocb failure"));
37307c478bd9Sstevel@tonic-gate 			tl_merror(wq, respmp, ENOMEM);
37317c478bd9Sstevel@tonic-gate 			tl_closeok(acc_ep);
37327c478bd9Sstevel@tonic-gate 			if (client_noclose_set)
37337c478bd9Sstevel@tonic-gate 				tl_closeok(cl_ep);
37347c478bd9Sstevel@tonic-gate 			tl_refrele(acc_ep);
37357c478bd9Sstevel@tonic-gate 			return;
37367c478bd9Sstevel@tonic-gate 		}
37377c478bd9Sstevel@tonic-gate 		if (dimp->b_cont) {
37387c478bd9Sstevel@tonic-gate 			/* no user data in provider generated discon ind */
37397c478bd9Sstevel@tonic-gate 			freemsg(dimp->b_cont);
37407c478bd9Sstevel@tonic-gate 			dimp->b_cont = NULL;
37417c478bd9Sstevel@tonic-gate 		}
37427c478bd9Sstevel@tonic-gate 
37437c478bd9Sstevel@tonic-gate 		DB_TYPE(dimp) = M_PROTO;
37447c478bd9Sstevel@tonic-gate 		di = (struct T_discon_ind *)dimp->b_rptr;
37457c478bd9Sstevel@tonic-gate 		di->PRIM_type  = T_DISCON_IND;
37467c478bd9Sstevel@tonic-gate 		di->DISCON_reason = err;
37477c478bd9Sstevel@tonic-gate 		di->SEQ_number = BADSEQNUM;
37487c478bd9Sstevel@tonic-gate 
37497c478bd9Sstevel@tonic-gate 		tep->te_state = TS_IDLE;
37507c478bd9Sstevel@tonic-gate 		/*
37517c478bd9Sstevel@tonic-gate 		 * send T_DISCON_IND message
37527c478bd9Sstevel@tonic-gate 		 */
37537c478bd9Sstevel@tonic-gate 		putnext(acc_ep->te_rq, dimp);
37547c478bd9Sstevel@tonic-gate 		if (client_noclose_set)
37557c478bd9Sstevel@tonic-gate 			tl_closeok(cl_ep);
37567c478bd9Sstevel@tonic-gate 		tl_closeok(acc_ep);
37577c478bd9Sstevel@tonic-gate 		tl_refrele(acc_ep);
37587c478bd9Sstevel@tonic-gate 		return;
37597c478bd9Sstevel@tonic-gate 	}
37607c478bd9Sstevel@tonic-gate 
37617c478bd9Sstevel@tonic-gate 	/*
37627c478bd9Sstevel@tonic-gate 	 * now start connecting the accepting endpoint
37637c478bd9Sstevel@tonic-gate 	 */
37647c478bd9Sstevel@tonic-gate 	if (tep != acc_ep)
3765b8ffbd31SJohn Levon 		acc_ep->te_state = nextstate[TE_PASS_CONN][acc_ep->te_state];
37667c478bd9Sstevel@tonic-gate 
37677c478bd9Sstevel@tonic-gate 	if (cl_ep == NULL) {
37687c478bd9Sstevel@tonic-gate 		/*
37697c478bd9Sstevel@tonic-gate 		 * The client has already closed. Send up any queued messages
37707c478bd9Sstevel@tonic-gate 		 * and change the state accordingly.
37717c478bd9Sstevel@tonic-gate 		 */
37727c478bd9Sstevel@tonic-gate 		tl_ok_ack(wq, ackmp, prim);
37737c478bd9Sstevel@tonic-gate 		tl_icon_sendmsgs(acc_ep, &tip->ti_mp);
37747c478bd9Sstevel@tonic-gate 
37757c478bd9Sstevel@tonic-gate 		/*
37767c478bd9Sstevel@tonic-gate 		 * remove endpoint from incoming connection
37777c478bd9Sstevel@tonic-gate 		 * delete client from list of incoming connections
37787c478bd9Sstevel@tonic-gate 		 */
37797c478bd9Sstevel@tonic-gate 		tl_freetip(tep, tip);
37807c478bd9Sstevel@tonic-gate 		freemsg(mp);
37817c478bd9Sstevel@tonic-gate 		tl_closeok(acc_ep);
37827c478bd9Sstevel@tonic-gate 		tl_refrele(acc_ep);
37837c478bd9Sstevel@tonic-gate 		return;
37847c478bd9Sstevel@tonic-gate 	} else if (tip->ti_mp != NULL) {
37857c478bd9Sstevel@tonic-gate 		/*
37867c478bd9Sstevel@tonic-gate 		 * The client could have queued a T_DISCON_IND which needs
37877c478bd9Sstevel@tonic-gate 		 * to be sent up.
37887c478bd9Sstevel@tonic-gate 		 * Note that t_discon_req can not operate the same as
37897c478bd9Sstevel@tonic-gate 		 * t_data_req since it is not possible for it to putbq
37907c478bd9Sstevel@tonic-gate 		 * the message and return -1 due to the use of qwriter.
37917c478bd9Sstevel@tonic-gate 		 */
37927c478bd9Sstevel@tonic-gate 		tl_icon_sendmsgs(acc_ep, &tip->ti_mp);
37937c478bd9Sstevel@tonic-gate 	}
37947c478bd9Sstevel@tonic-gate 
37957c478bd9Sstevel@tonic-gate 	/*
37967c478bd9Sstevel@tonic-gate 	 * prepare connect confirm T_CONN_CON message
37977c478bd9Sstevel@tonic-gate 	 */
37987c478bd9Sstevel@tonic-gate 
37997c478bd9Sstevel@tonic-gate 	/*
38007c478bd9Sstevel@tonic-gate 	 * allocate the message - original data blocks
38017c478bd9Sstevel@tonic-gate 	 * retained in the returned mblk
38027c478bd9Sstevel@tonic-gate 	 */
3803cb492d90SToomas Soome 	if (!IS_SOCKET(cl_ep) || tl_disable_early_connect) {
38047c478bd9Sstevel@tonic-gate 		ccmp = tl_resizemp(respmp, size);
38057c478bd9Sstevel@tonic-gate 		if (ccmp == NULL) {
38067c478bd9Sstevel@tonic-gate 			tl_ok_ack(wq, ackmp, prim);
38077c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
3808cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
3809fc80c0dfSnordmark 			    "tl_conn_res:conn_con:allocb failure"));
38107c478bd9Sstevel@tonic-gate 			tl_merror(wq, respmp, ENOMEM);
38117c478bd9Sstevel@tonic-gate 			tl_closeok(acc_ep);
38127c478bd9Sstevel@tonic-gate 			if (client_noclose_set)
38137c478bd9Sstevel@tonic-gate 				tl_closeok(cl_ep);
38147c478bd9Sstevel@tonic-gate 			tl_refrele(acc_ep);
38157c478bd9Sstevel@tonic-gate 			return;
38167c478bd9Sstevel@tonic-gate 		}
38177c478bd9Sstevel@tonic-gate 
38187c478bd9Sstevel@tonic-gate 		DB_TYPE(ccmp) = M_PROTO;
38197c478bd9Sstevel@tonic-gate 		cc = (struct T_conn_con *)ccmp->b_rptr;
38207c478bd9Sstevel@tonic-gate 		cc->PRIM_type  = T_CONN_CON;
38217c478bd9Sstevel@tonic-gate 		cc->RES_offset = (t_scalar_t)sizeof (struct T_conn_con);
38227c478bd9Sstevel@tonic-gate 		cc->RES_length = acc_ep->te_alen;
38237c478bd9Sstevel@tonic-gate 		addr_startp = ccmp->b_rptr + cc->RES_offset;
38247c478bd9Sstevel@tonic-gate 		bcopy(acc_ep->te_abuf, addr_startp, acc_ep->te_alen);
3825cb492d90SToomas Soome 		if (cl_ep->te_flag & (TL_SETCRED | TL_SETUCRED)) {
38267c478bd9Sstevel@tonic-gate 			cc->OPT_offset = (t_scalar_t)T_ALIGN(cc->RES_offset +
38277c478bd9Sstevel@tonic-gate 			    cc->RES_length);
38287c478bd9Sstevel@tonic-gate 			cc->OPT_length = olen;
38297c478bd9Sstevel@tonic-gate 			tl_fill_option(ccmp->b_rptr + cc->OPT_offset,
383045916cd2Sjpk 			    acc_ep->te_credp, acc_ep->te_cpid, cl_ep->te_flag,
383145916cd2Sjpk 			    cl_ep->te_credp);
38327c478bd9Sstevel@tonic-gate 		} else {
38337c478bd9Sstevel@tonic-gate 			cc->OPT_offset = 0;
38347c478bd9Sstevel@tonic-gate 			cc->OPT_length = 0;
38357c478bd9Sstevel@tonic-gate 		}
38367c478bd9Sstevel@tonic-gate 		/*
38377c478bd9Sstevel@tonic-gate 		 * Forward the credential in the packet so it can be picked up
38387c478bd9Sstevel@tonic-gate 		 * at the higher layers for more complete credential processing
38397c478bd9Sstevel@tonic-gate 		 */
3840de8c4a14SErik Nordmark 		mblk_setcred(ccmp, acc_ep->te_credp, acc_ep->te_cpid);
38417c478bd9Sstevel@tonic-gate 	} else {
38427c478bd9Sstevel@tonic-gate 		freemsg(respmp);
38437c478bd9Sstevel@tonic-gate 		respmp = NULL;
38447c478bd9Sstevel@tonic-gate 	}
38457c478bd9Sstevel@tonic-gate 
38467c478bd9Sstevel@tonic-gate 	/*
38477c478bd9Sstevel@tonic-gate 	 * make connection linking
38487c478bd9Sstevel@tonic-gate 	 * accepting and client endpoints
38497c478bd9Sstevel@tonic-gate 	 * No need to increment references:
38507c478bd9Sstevel@tonic-gate 	 *	on client: it should already have one from tip->ti_tep linkage.
38517c478bd9Sstevel@tonic-gate 	 *	on acceptor is should already have one from the table lookup.
38527c478bd9Sstevel@tonic-gate 	 *
38537c478bd9Sstevel@tonic-gate 	 * At this point both client and acceptor can't close. Set client
38547c478bd9Sstevel@tonic-gate 	 * serializer to acceptor's.
38557c478bd9Sstevel@tonic-gate 	 */
38567c478bd9Sstevel@tonic-gate 	ASSERT(cl_ep->te_refcnt >= 2);
38577c478bd9Sstevel@tonic-gate 	ASSERT(acc_ep->te_refcnt >= 2);
38587c478bd9Sstevel@tonic-gate 	ASSERT(cl_ep->te_conp == NULL);
38597c478bd9Sstevel@tonic-gate 	ASSERT(acc_ep->te_conp == NULL);
38607c478bd9Sstevel@tonic-gate 	cl_ep->te_conp = acc_ep;
38617c478bd9Sstevel@tonic-gate 	acc_ep->te_conp = cl_ep;
38627c478bd9Sstevel@tonic-gate 	ASSERT(cl_ep->te_ser == tep->te_ser);
38637c478bd9Sstevel@tonic-gate 	if (switch_client_serializer) {
38647c478bd9Sstevel@tonic-gate 		mutex_enter(&cl_ep->te_ser_lock);
38657c478bd9Sstevel@tonic-gate 		if (cl_ep->te_ser_count > 0) {
38667c478bd9Sstevel@tonic-gate 			switch_client_serializer = B_FALSE;
38677c478bd9Sstevel@tonic-gate 			tl_serializer_noswitch++;
38687c478bd9Sstevel@tonic-gate 		} else {
38697c478bd9Sstevel@tonic-gate 			/*
38707c478bd9Sstevel@tonic-gate 			 * Move client to the acceptor's serializer.
38717c478bd9Sstevel@tonic-gate 			 */
38727c478bd9Sstevel@tonic-gate 			tl_serializer_refhold(acc_ep->te_ser);
38737c478bd9Sstevel@tonic-gate 			tl_serializer_refrele(cl_ep->te_ser);
38747c478bd9Sstevel@tonic-gate 			cl_ep->te_ser = acc_ep->te_ser;
38757c478bd9Sstevel@tonic-gate 		}
38767c478bd9Sstevel@tonic-gate 		mutex_exit(&cl_ep->te_ser_lock);
38777c478bd9Sstevel@tonic-gate 	}
38787c478bd9Sstevel@tonic-gate 	if (!switch_client_serializer) {
38797c478bd9Sstevel@tonic-gate 		/*
38807c478bd9Sstevel@tonic-gate 		 * It is not possible to switch client to use acceptor's.
38817c478bd9Sstevel@tonic-gate 		 * Move acceptor to client's serializer (which is the same as
38827c478bd9Sstevel@tonic-gate 		 * listener's).
38837c478bd9Sstevel@tonic-gate 		 */
38847c478bd9Sstevel@tonic-gate 		tl_serializer_refhold(cl_ep->te_ser);
38857c478bd9Sstevel@tonic-gate 		tl_serializer_refrele(acc_ep->te_ser);
38867c478bd9Sstevel@tonic-gate 		acc_ep->te_ser = cl_ep->te_ser;
38877c478bd9Sstevel@tonic-gate 	}
38887c478bd9Sstevel@tonic-gate 
38897c478bd9Sstevel@tonic-gate 	TL_REMOVE_PEER(cl_ep->te_oconp);
38907c478bd9Sstevel@tonic-gate 	TL_REMOVE_PEER(acc_ep->te_oconp);
38917c478bd9Sstevel@tonic-gate 
38927c478bd9Sstevel@tonic-gate 	/*
38937c478bd9Sstevel@tonic-gate 	 * remove endpoint from incoming connection
38947c478bd9Sstevel@tonic-gate 	 * delete client from list of incoming connections
38957c478bd9Sstevel@tonic-gate 	 */
38967c478bd9Sstevel@tonic-gate 	tip->ti_tep = NULL;
38977c478bd9Sstevel@tonic-gate 	tl_freetip(tep, tip);
38987c478bd9Sstevel@tonic-gate 	tl_ok_ack(wq, ackmp, prim);
38997c478bd9Sstevel@tonic-gate 
39007c478bd9Sstevel@tonic-gate 	/*
39017c478bd9Sstevel@tonic-gate 	 * data blocks already linked in reallocb()
39027c478bd9Sstevel@tonic-gate 	 */
39037c478bd9Sstevel@tonic-gate 
39047c478bd9Sstevel@tonic-gate 	/*
39057c478bd9Sstevel@tonic-gate 	 * link queues so that I_SENDFD will work
39067c478bd9Sstevel@tonic-gate 	 */
3907cb492d90SToomas Soome 	if (!IS_SOCKET(tep)) {
39087c478bd9Sstevel@tonic-gate 		acc_ep->te_wq->q_next = cl_ep->te_rq;
39097c478bd9Sstevel@tonic-gate 		cl_ep->te_wq->q_next = acc_ep->te_rq;
39107c478bd9Sstevel@tonic-gate 	}
39117c478bd9Sstevel@tonic-gate 
39127c478bd9Sstevel@tonic-gate 	/*
39137c478bd9Sstevel@tonic-gate 	 * send T_CONN_CON up on client side unless it was already
39147c478bd9Sstevel@tonic-gate 	 * done (for a socket). In cases any data or ordrel req has been
39157c478bd9Sstevel@tonic-gate 	 * queued make sure that the service procedure runs.
39167c478bd9Sstevel@tonic-gate 	 */
39177c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(cl_ep) && !tl_disable_early_connect) {
39187c478bd9Sstevel@tonic-gate 		enableok(cl_ep->te_wq);
39197c478bd9Sstevel@tonic-gate 		TL_QENABLE(cl_ep);
39207c478bd9Sstevel@tonic-gate 		if (ccmp != NULL)
39217c478bd9Sstevel@tonic-gate 			freemsg(ccmp);
39227c478bd9Sstevel@tonic-gate 	} else {
39237c478bd9Sstevel@tonic-gate 		/*
39247c478bd9Sstevel@tonic-gate 		 * change client state on TE_CONN_CON event
39257c478bd9Sstevel@tonic-gate 		 */
3926b8ffbd31SJohn Levon 		cl_ep->te_state = nextstate[TE_CONN_CON][cl_ep->te_state];
39277c478bd9Sstevel@tonic-gate 		putnext(cl_ep->te_rq, ccmp);
39287c478bd9Sstevel@tonic-gate 	}
39297c478bd9Sstevel@tonic-gate 
39307c478bd9Sstevel@tonic-gate 	/* Mark the both endpoints as accepted */
39317c478bd9Sstevel@tonic-gate 	cl_ep->te_flag |= TL_ACCEPTED;
39327c478bd9Sstevel@tonic-gate 	acc_ep->te_flag |= TL_ACCEPTED;
39337c478bd9Sstevel@tonic-gate 
39347c478bd9Sstevel@tonic-gate 	/*
39357c478bd9Sstevel@tonic-gate 	 * Allow client and acceptor to close.
39367c478bd9Sstevel@tonic-gate 	 */
39377c478bd9Sstevel@tonic-gate 	tl_closeok(acc_ep);
39387c478bd9Sstevel@tonic-gate 	if (client_noclose_set)
39397c478bd9Sstevel@tonic-gate 		tl_closeok(cl_ep);
39407c478bd9Sstevel@tonic-gate }
39417c478bd9Sstevel@tonic-gate 
39427c478bd9Sstevel@tonic-gate 
39437c478bd9Sstevel@tonic-gate 
39447c478bd9Sstevel@tonic-gate 
39457c478bd9Sstevel@tonic-gate static void
tl_discon_req(mblk_t * mp,tl_endpt_t * tep)39467c478bd9Sstevel@tonic-gate tl_discon_req(mblk_t *mp, tl_endpt_t *tep)
39477c478bd9Sstevel@tonic-gate {
39487c478bd9Sstevel@tonic-gate 	queue_t			*wq;
39497c478bd9Sstevel@tonic-gate 	struct T_discon_req	*dr;
39507c478bd9Sstevel@tonic-gate 	ssize_t			msz;
39517c478bd9Sstevel@tonic-gate 	tl_endpt_t		*peer_tep = tep->te_conp;
39527c478bd9Sstevel@tonic-gate 	tl_endpt_t		*srv_tep = tep->te_oconp;
39537c478bd9Sstevel@tonic-gate 	tl_icon_t		*tip;
39547c478bd9Sstevel@tonic-gate 	size_t			size;
39557c478bd9Sstevel@tonic-gate 	mblk_t			*ackmp, *dimp, *respmp;
39567c478bd9Sstevel@tonic-gate 	struct T_discon_ind	*di;
39577c478bd9Sstevel@tonic-gate 	t_scalar_t		save_state, new_state;
39587c478bd9Sstevel@tonic-gate 
39597c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
39607c478bd9Sstevel@tonic-gate 		freemsg(mp);
39617c478bd9Sstevel@tonic-gate 		return;
39627c478bd9Sstevel@tonic-gate 	}
39637c478bd9Sstevel@tonic-gate 
39647c478bd9Sstevel@tonic-gate 	if ((peer_tep != NULL) && peer_tep->te_closing) {
39657c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_conp);
39667c478bd9Sstevel@tonic-gate 		peer_tep = NULL;
39677c478bd9Sstevel@tonic-gate 	}
39687c478bd9Sstevel@tonic-gate 	if ((srv_tep != NULL) && srv_tep->te_closing) {
39697c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_oconp);
39707c478bd9Sstevel@tonic-gate 		srv_tep = NULL;
39717c478bd9Sstevel@tonic-gate 	}
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate 	wq = tep->te_wq;
39747c478bd9Sstevel@tonic-gate 
39757c478bd9Sstevel@tonic-gate 	/*
39767c478bd9Sstevel@tonic-gate 	 * preallocate memory for:
39777c478bd9Sstevel@tonic-gate 	 * 1. max of T_ERROR_ACK and T_OK_ACK
39787c478bd9Sstevel@tonic-gate 	 *	==> known max T_ERROR_ACK
39797c478bd9Sstevel@tonic-gate 	 * 2. for  T_DISCON_IND
39807c478bd9Sstevel@tonic-gate 	 */
39817c478bd9Sstevel@tonic-gate 	ackmp = allocb(sizeof (struct T_error_ack), BPRI_MED);
3982cb492d90SToomas Soome 	if (ackmp == NULL) {
39837c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, sizeof (struct T_error_ack));
39847c478bd9Sstevel@tonic-gate 		return;
39857c478bd9Sstevel@tonic-gate 	}
39867c478bd9Sstevel@tonic-gate 	/*
39877c478bd9Sstevel@tonic-gate 	 * memory committed for T_OK_ACK/T_ERROR_ACK now
39887c478bd9Sstevel@tonic-gate 	 * will be committed for T_DISCON_IND  later
39897c478bd9Sstevel@tonic-gate 	 */
39907c478bd9Sstevel@tonic-gate 
39917c478bd9Sstevel@tonic-gate 	dr = (struct T_discon_req *)mp->b_rptr;
39927c478bd9Sstevel@tonic-gate 	msz = MBLKL(mp);
39937c478bd9Sstevel@tonic-gate 
39947c478bd9Sstevel@tonic-gate 	/*
39957c478bd9Sstevel@tonic-gate 	 * validate the state
39967c478bd9Sstevel@tonic-gate 	 */
39977c478bd9Sstevel@tonic-gate 	save_state = new_state = tep->te_state;
3998cb492d90SToomas Soome 	if (!(save_state >= TS_WCON_CREQ && save_state <= TS_WRES_CIND) &&
3999cb492d90SToomas Soome 	    !(save_state >= TS_DATA_XFER && save_state <= TS_WREQ_ORDREL)) {
40007c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
4001cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
4002fc80c0dfSnordmark 		    "tl_wput:T_DISCON_REQ:out of state, state=%d",
4003fc80c0dfSnordmark 		    tep->te_state));
40047c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TOUTSTATE, 0, T_DISCON_REQ);
40057c478bd9Sstevel@tonic-gate 		freemsg(mp);
40067c478bd9Sstevel@tonic-gate 		return;
40077c478bd9Sstevel@tonic-gate 	}
40087c478bd9Sstevel@tonic-gate 	/*
40097c478bd9Sstevel@tonic-gate 	 * Defer committing the state change until it is determined if
40107c478bd9Sstevel@tonic-gate 	 * the message will be queued with the tl_icon or not.
40117c478bd9Sstevel@tonic-gate 	 */
4012b8ffbd31SJohn Levon 	new_state  = nextstate[TE_DISCON_REQ][tep->te_state];
40137c478bd9Sstevel@tonic-gate 
40147c478bd9Sstevel@tonic-gate 	/* validate the message */
40157c478bd9Sstevel@tonic-gate 	if (msz < sizeof (struct T_discon_req)) {
4016cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4017fc80c0dfSnordmark 		    "tl_discon_req:invalid message"));
4018b8ffbd31SJohn Levon 		tep->te_state = nextstate[TE_ERROR_ACK][new_state];
40197c478bd9Sstevel@tonic-gate 		tl_error_ack(wq, ackmp, TSYSERR, EINVAL, T_DISCON_REQ);
40207c478bd9Sstevel@tonic-gate 		freemsg(mp);
40217c478bd9Sstevel@tonic-gate 		return;
40227c478bd9Sstevel@tonic-gate 	}
40237c478bd9Sstevel@tonic-gate 
40247c478bd9Sstevel@tonic-gate 	/*
40257c478bd9Sstevel@tonic-gate 	 * if server, then validate that client exists
40267c478bd9Sstevel@tonic-gate 	 * by connection sequence number etc.
40277c478bd9Sstevel@tonic-gate 	 */
40287c478bd9Sstevel@tonic-gate 	if (tep->te_nicon > 0) { /* server */
40297c478bd9Sstevel@tonic-gate 
40307c478bd9Sstevel@tonic-gate 		/*
40317c478bd9Sstevel@tonic-gate 		 * search server list for disconnect client
40327c478bd9Sstevel@tonic-gate 		 */
40337c478bd9Sstevel@tonic-gate 		tip = tl_icon_find(tep, dr->SEQ_number);
40347c478bd9Sstevel@tonic-gate 		if (tip == NULL) {
40357c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 2,
4036cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4037fc80c0dfSnordmark 			    "tl_discon_req:no disconnect endpoint"));
4038b8ffbd31SJohn Levon 			tep->te_state = nextstate[TE_ERROR_ACK][new_state];
40397c478bd9Sstevel@tonic-gate 			tl_error_ack(wq, ackmp, TBADSEQ, 0, T_DISCON_REQ);
40407c478bd9Sstevel@tonic-gate 			freemsg(mp);
40417c478bd9Sstevel@tonic-gate 			return;
40427c478bd9Sstevel@tonic-gate 		}
40437c478bd9Sstevel@tonic-gate 		/*
40447c478bd9Sstevel@tonic-gate 		 * If ti_tep is NULL the client has already closed. In this case
40457c478bd9Sstevel@tonic-gate 		 * the code below will avoid any action on the client side.
40467c478bd9Sstevel@tonic-gate 		 */
40477c478bd9Sstevel@tonic-gate 
404856f33205SJonathan Adams 		IMPLY(tip->ti_tep != NULL,
404956f33205SJonathan Adams 		    tip->ti_tep->te_seqno == dr->SEQ_number);
40507c478bd9Sstevel@tonic-gate 		peer_tep = tip->ti_tep;
40517c478bd9Sstevel@tonic-gate 	}
40527c478bd9Sstevel@tonic-gate 
40537c478bd9Sstevel@tonic-gate 	/*
40547c478bd9Sstevel@tonic-gate 	 * preallocate now for T_DISCON_IND
40557c478bd9Sstevel@tonic-gate 	 * ack validity of request (T_OK_ACK) after memory committed
40567c478bd9Sstevel@tonic-gate 	 */
40577c478bd9Sstevel@tonic-gate 	size = sizeof (struct T_discon_ind);
40587c478bd9Sstevel@tonic-gate 	if ((respmp = reallocb(mp, size, 0)) == NULL) {
40597c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, size);
40607c478bd9Sstevel@tonic-gate 		freemsg(ackmp);
40617c478bd9Sstevel@tonic-gate 		return;
40627c478bd9Sstevel@tonic-gate 	}
40637c478bd9Sstevel@tonic-gate 
40647c478bd9Sstevel@tonic-gate 	/*
40657c478bd9Sstevel@tonic-gate 	 * prepare message to ack validity of request
40667c478bd9Sstevel@tonic-gate 	 */
4067cb492d90SToomas Soome 	if (tep->te_nicon == 0) {
4068b8ffbd31SJohn Levon 		new_state = nextstate[TE_OK_ACK1][new_state];
4069cb492d90SToomas Soome 	} else {
40707c478bd9Sstevel@tonic-gate 		if (tep->te_nicon == 1)
4071b8ffbd31SJohn Levon 			new_state = nextstate[TE_OK_ACK2][new_state];
40727c478bd9Sstevel@tonic-gate 		else
4073b8ffbd31SJohn Levon 			new_state = nextstate[TE_OK_ACK4][new_state];
4074cb492d90SToomas Soome 	}
40757c478bd9Sstevel@tonic-gate 
40767c478bd9Sstevel@tonic-gate 	/*
40777c478bd9Sstevel@tonic-gate 	 * Flushing queues according to TPI. Using the old state.
40787c478bd9Sstevel@tonic-gate 	 */
40797c478bd9Sstevel@tonic-gate 	if ((tep->te_nicon <= 1) &&
40807c478bd9Sstevel@tonic-gate 	    ((save_state == TS_DATA_XFER) ||
40817c478bd9Sstevel@tonic-gate 	    (save_state == TS_WIND_ORDREL) ||
40827c478bd9Sstevel@tonic-gate 	    (save_state == TS_WREQ_ORDREL)))
40837c478bd9Sstevel@tonic-gate 		(void) putnextctl1(RD(wq), M_FLUSH, FLUSHRW);
40847c478bd9Sstevel@tonic-gate 
40857c478bd9Sstevel@tonic-gate 	/* send T_OK_ACK up  */
40867c478bd9Sstevel@tonic-gate 	tl_ok_ack(wq, ackmp, T_DISCON_REQ);
40877c478bd9Sstevel@tonic-gate 
40887c478bd9Sstevel@tonic-gate 	/*
40897c478bd9Sstevel@tonic-gate 	 * now do disconnect business
40907c478bd9Sstevel@tonic-gate 	 */
40917c478bd9Sstevel@tonic-gate 	if (tep->te_nicon > 0) { /* listener */
40927c478bd9Sstevel@tonic-gate 		if (peer_tep != NULL && !peer_tep->te_closing) {
40937c478bd9Sstevel@tonic-gate 			/*
40947c478bd9Sstevel@tonic-gate 			 * disconnect incoming connect request pending to tep
40957c478bd9Sstevel@tonic-gate 			 */
40967c478bd9Sstevel@tonic-gate 			if ((dimp = tl_resizemp(respmp, size)) == NULL) {
40977c478bd9Sstevel@tonic-gate 				(void) (STRLOG(TL_ID, tep->te_minor, 2,
4098cb492d90SToomas Soome 				    SL_TRACE | SL_ERROR,
4099fc80c0dfSnordmark 				    "tl_discon_req: reallocb failed"));
41007c478bd9Sstevel@tonic-gate 				tep->te_state = new_state;
41017c478bd9Sstevel@tonic-gate 				tl_merror(wq, respmp, ENOMEM);
41027c478bd9Sstevel@tonic-gate 				return;
41037c478bd9Sstevel@tonic-gate 			}
41047c478bd9Sstevel@tonic-gate 			di = (struct T_discon_ind *)dimp->b_rptr;
41057c478bd9Sstevel@tonic-gate 			di->SEQ_number = BADSEQNUM;
41067c478bd9Sstevel@tonic-gate 			save_state = peer_tep->te_state;
41077c478bd9Sstevel@tonic-gate 			peer_tep->te_state = TS_IDLE;
41087c478bd9Sstevel@tonic-gate 
41097c478bd9Sstevel@tonic-gate 			TL_REMOVE_PEER(peer_tep->te_oconp);
41107c478bd9Sstevel@tonic-gate 			enableok(peer_tep->te_wq);
41117c478bd9Sstevel@tonic-gate 			TL_QENABLE(peer_tep);
41127c478bd9Sstevel@tonic-gate 		} else {
41137c478bd9Sstevel@tonic-gate 			freemsg(respmp);
41147c478bd9Sstevel@tonic-gate 			dimp = NULL;
41157c478bd9Sstevel@tonic-gate 		}
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 		/*
41187c478bd9Sstevel@tonic-gate 		 * remove endpoint from incoming connection list
41197c478bd9Sstevel@tonic-gate 		 * - remove disconnect client from list on server
41207c478bd9Sstevel@tonic-gate 		 */
41217c478bd9Sstevel@tonic-gate 		tl_freetip(tep, tip);
41227c478bd9Sstevel@tonic-gate 	} else if ((peer_tep = tep->te_oconp) != NULL) { /* client */
41237c478bd9Sstevel@tonic-gate 		/*
41247c478bd9Sstevel@tonic-gate 		 * disconnect an outgoing request pending from tep
41257c478bd9Sstevel@tonic-gate 		 */
41267c478bd9Sstevel@tonic-gate 
41277c478bd9Sstevel@tonic-gate 		if ((dimp = tl_resizemp(respmp, size)) == NULL) {
41287c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 2,
4129cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4130fc80c0dfSnordmark 			    "tl_discon_req: reallocb failed"));
41317c478bd9Sstevel@tonic-gate 			tep->te_state = new_state;
41327c478bd9Sstevel@tonic-gate 			tl_merror(wq, respmp, ENOMEM);
41337c478bd9Sstevel@tonic-gate 			return;
41347c478bd9Sstevel@tonic-gate 		}
41357c478bd9Sstevel@tonic-gate 		di = (struct T_discon_ind *)dimp->b_rptr;
41367c478bd9Sstevel@tonic-gate 		DB_TYPE(dimp) = M_PROTO;
41377c478bd9Sstevel@tonic-gate 		di->PRIM_type  = T_DISCON_IND;
41387c478bd9Sstevel@tonic-gate 		di->DISCON_reason = ECONNRESET;
41397c478bd9Sstevel@tonic-gate 		di->SEQ_number = tep->te_seqno;
41407c478bd9Sstevel@tonic-gate 
41417c478bd9Sstevel@tonic-gate 		/*
41427c478bd9Sstevel@tonic-gate 		 * If this is a socket the T_DISCON_IND is queued with
41437c478bd9Sstevel@tonic-gate 		 * the T_CONN_IND. Otherwise the T_CONN_IND is removed
41447c478bd9Sstevel@tonic-gate 		 * from the list of pending connections.
41457c478bd9Sstevel@tonic-gate 		 * Note that when te_oconp is set the peer better have
41467c478bd9Sstevel@tonic-gate 		 * a t_connind_t for the client.
41477c478bd9Sstevel@tonic-gate 		 */
41487c478bd9Sstevel@tonic-gate 		if (IS_SOCKET(tep) && !tl_disable_early_connect) {
41497c478bd9Sstevel@tonic-gate 			/*
41507c478bd9Sstevel@tonic-gate 			 * No need to check that
41517c478bd9Sstevel@tonic-gate 			 * ti_tep == NULL since the T_DISCON_IND
41527c478bd9Sstevel@tonic-gate 			 * takes precedence over other queued
41537c478bd9Sstevel@tonic-gate 			 * messages.
41547c478bd9Sstevel@tonic-gate 			 */
41557c478bd9Sstevel@tonic-gate 			tl_icon_queuemsg(peer_tep, tep->te_seqno, dimp);
41567c478bd9Sstevel@tonic-gate 			peer_tep = NULL;
41577c478bd9Sstevel@tonic-gate 			dimp = NULL;
41587c478bd9Sstevel@tonic-gate 			/*
41597c478bd9Sstevel@tonic-gate 			 * Can't clear te_oconp since tl_co_unconnect needs
41607c478bd9Sstevel@tonic-gate 			 * it as a hint not to free the tep.
41617c478bd9Sstevel@tonic-gate 			 * Keep the state unchanged since tl_conn_res inspects
41627c478bd9Sstevel@tonic-gate 			 * it.
41637c478bd9Sstevel@tonic-gate 			 */
41647c478bd9Sstevel@tonic-gate 			new_state = tep->te_state;
41657c478bd9Sstevel@tonic-gate 		} else {
41667c478bd9Sstevel@tonic-gate 			/* Found - delete it */
41677c478bd9Sstevel@tonic-gate 			tip = tl_icon_find(peer_tep, tep->te_seqno);
41687c478bd9Sstevel@tonic-gate 			if (tip != NULL) {
41697c478bd9Sstevel@tonic-gate 				ASSERT(tep == tip->ti_tep);
41707c478bd9Sstevel@tonic-gate 				save_state = peer_tep->te_state;
41717c478bd9Sstevel@tonic-gate 				if (peer_tep->te_nicon == 1)
41727c478bd9Sstevel@tonic-gate 					peer_tep->te_state =
4173b8ffbd31SJohn Levon 					    nextstate[TE_DISCON_IND2]
4174b8ffbd31SJohn Levon 					    [peer_tep->te_state];
41757c478bd9Sstevel@tonic-gate 				else
41767c478bd9Sstevel@tonic-gate 					peer_tep->te_state =
4177b8ffbd31SJohn Levon 					    nextstate[TE_DISCON_IND3]
4178b8ffbd31SJohn Levon 					    [peer_tep->te_state];
41797c478bd9Sstevel@tonic-gate 				tl_freetip(peer_tep, tip);
41807c478bd9Sstevel@tonic-gate 			}
41817c478bd9Sstevel@tonic-gate 			ASSERT(tep->te_oconp != NULL);
41827c478bd9Sstevel@tonic-gate 			TL_UNCONNECT(tep->te_oconp);
41837c478bd9Sstevel@tonic-gate 		}
41847c478bd9Sstevel@tonic-gate 	} else if ((peer_tep = tep->te_conp) != NULL) { /* connected! */
41857c478bd9Sstevel@tonic-gate 		if ((dimp = tl_resizemp(respmp, size)) == NULL) {
41867c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 2,
4187cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4188fc80c0dfSnordmark 			    "tl_discon_req: reallocb failed"));
41897c478bd9Sstevel@tonic-gate 			tep->te_state = new_state;
41907c478bd9Sstevel@tonic-gate 			tl_merror(wq, respmp, ENOMEM);
41917c478bd9Sstevel@tonic-gate 			return;
41927c478bd9Sstevel@tonic-gate 		}
41937c478bd9Sstevel@tonic-gate 		di = (struct T_discon_ind *)dimp->b_rptr;
41947c478bd9Sstevel@tonic-gate 		di->SEQ_number = BADSEQNUM;
41957c478bd9Sstevel@tonic-gate 
41967c478bd9Sstevel@tonic-gate 		save_state = peer_tep->te_state;
41977c478bd9Sstevel@tonic-gate 		peer_tep->te_state = TS_IDLE;
41987c478bd9Sstevel@tonic-gate 	} else {
41997c478bd9Sstevel@tonic-gate 		/* Not connected */
42007c478bd9Sstevel@tonic-gate 		tep->te_state = new_state;
42017c478bd9Sstevel@tonic-gate 		freemsg(respmp);
42027c478bd9Sstevel@tonic-gate 		return;
42037c478bd9Sstevel@tonic-gate 	}
42047c478bd9Sstevel@tonic-gate 
42057c478bd9Sstevel@tonic-gate 	/* Commit state changes */
42067c478bd9Sstevel@tonic-gate 	tep->te_state = new_state;
42077c478bd9Sstevel@tonic-gate 
42087c478bd9Sstevel@tonic-gate 	if (peer_tep == NULL) {
42097c478bd9Sstevel@tonic-gate 		ASSERT(dimp == NULL);
42107c478bd9Sstevel@tonic-gate 		goto done;
42117c478bd9Sstevel@tonic-gate 	}
42127c478bd9Sstevel@tonic-gate 	/*
42137c478bd9Sstevel@tonic-gate 	 * Flush queues on peer before sending up
42147c478bd9Sstevel@tonic-gate 	 * T_DISCON_IND according to TPI
42157c478bd9Sstevel@tonic-gate 	 */
42167c478bd9Sstevel@tonic-gate 
42177c478bd9Sstevel@tonic-gate 	if ((save_state == TS_DATA_XFER) ||
42187c478bd9Sstevel@tonic-gate 	    (save_state == TS_WIND_ORDREL) ||
42197c478bd9Sstevel@tonic-gate 	    (save_state == TS_WREQ_ORDREL))
42207c478bd9Sstevel@tonic-gate 		(void) putnextctl1(peer_tep->te_rq, M_FLUSH, FLUSHRW);
42217c478bd9Sstevel@tonic-gate 
42227c478bd9Sstevel@tonic-gate 	DB_TYPE(dimp) = M_PROTO;
42237c478bd9Sstevel@tonic-gate 	di->PRIM_type  = T_DISCON_IND;
42247c478bd9Sstevel@tonic-gate 	di->DISCON_reason = ECONNRESET;
42257c478bd9Sstevel@tonic-gate 
42267c478bd9Sstevel@tonic-gate 	/*
42277c478bd9Sstevel@tonic-gate 	 * data blocks already linked into dimp by reallocb()
42287c478bd9Sstevel@tonic-gate 	 */
42297c478bd9Sstevel@tonic-gate 	/*
42307c478bd9Sstevel@tonic-gate 	 * send indication message to peer user module
42317c478bd9Sstevel@tonic-gate 	 */
42327c478bd9Sstevel@tonic-gate 	ASSERT(dimp != NULL);
42337c478bd9Sstevel@tonic-gate 	putnext(peer_tep->te_rq, dimp);
42347c478bd9Sstevel@tonic-gate done:
42357c478bd9Sstevel@tonic-gate 	if (tep->te_conp) {	/* disconnect pointers if connected */
4236cb492d90SToomas Soome 		ASSERT(!peer_tep->te_closing);
42377c478bd9Sstevel@tonic-gate 
42387c478bd9Sstevel@tonic-gate 		/*
42397c478bd9Sstevel@tonic-gate 		 * Messages may be queued on peer's write queue
42407c478bd9Sstevel@tonic-gate 		 * waiting to be processed by its write service
42417c478bd9Sstevel@tonic-gate 		 * procedure. Before the pointer to the peer transport
42427c478bd9Sstevel@tonic-gate 		 * structure is set to NULL, qenable the peer's write
42437c478bd9Sstevel@tonic-gate 		 * queue so that the queued up messages are processed.
42447c478bd9Sstevel@tonic-gate 		 */
42457c478bd9Sstevel@tonic-gate 		if ((save_state == TS_DATA_XFER) ||
42467c478bd9Sstevel@tonic-gate 		    (save_state == TS_WIND_ORDREL) ||
42477c478bd9Sstevel@tonic-gate 		    (save_state == TS_WREQ_ORDREL))
42487c478bd9Sstevel@tonic-gate 			TL_QENABLE(peer_tep);
42497c478bd9Sstevel@tonic-gate 		ASSERT(peer_tep != NULL && peer_tep->te_conp != NULL);
42507c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(peer_tep->te_conp);
4251cb492d90SToomas Soome 		if (!IS_SOCKET(tep)) {
42527c478bd9Sstevel@tonic-gate 			/*
42537c478bd9Sstevel@tonic-gate 			 * unlink the streams
42547c478bd9Sstevel@tonic-gate 			 */
42557c478bd9Sstevel@tonic-gate 			tep->te_wq->q_next = NULL;
42567c478bd9Sstevel@tonic-gate 			peer_tep->te_wq->q_next = NULL;
42577c478bd9Sstevel@tonic-gate 		}
42587c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_conp);
42597c478bd9Sstevel@tonic-gate 	}
42607c478bd9Sstevel@tonic-gate }
42617c478bd9Sstevel@tonic-gate 
4262b2d8fb89SDan Kruchinin static void
tl_addr_req_ser(mblk_t * mp,tl_endpt_t * tep)4263b2d8fb89SDan Kruchinin tl_addr_req_ser(mblk_t *mp, tl_endpt_t *tep)
4264b2d8fb89SDan Kruchinin {
4265b2d8fb89SDan Kruchinin 	if (!tep->te_closing)
4266b2d8fb89SDan Kruchinin 		tl_addr_req(mp, tep);
4267b2d8fb89SDan Kruchinin 	else
4268b2d8fb89SDan Kruchinin 		freemsg(mp);
4269b2d8fb89SDan Kruchinin 
4270b2d8fb89SDan Kruchinin 	tl_serializer_exit(tep);
4271b2d8fb89SDan Kruchinin 	tl_refrele(tep);
4272b2d8fb89SDan Kruchinin }
42737c478bd9Sstevel@tonic-gate 
42747c478bd9Sstevel@tonic-gate static void
tl_addr_req(mblk_t * mp,tl_endpt_t * tep)42757c478bd9Sstevel@tonic-gate tl_addr_req(mblk_t *mp, tl_endpt_t *tep)
42767c478bd9Sstevel@tonic-gate {
42777c478bd9Sstevel@tonic-gate 	queue_t			*wq;
42787c478bd9Sstevel@tonic-gate 	size_t			ack_sz;
42797c478bd9Sstevel@tonic-gate 	mblk_t			*ackmp;
42807c478bd9Sstevel@tonic-gate 	struct T_addr_ack	*taa;
42817c478bd9Sstevel@tonic-gate 
42827c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
42837c478bd9Sstevel@tonic-gate 		freemsg(mp);
42847c478bd9Sstevel@tonic-gate 		return;
42857c478bd9Sstevel@tonic-gate 	}
42867c478bd9Sstevel@tonic-gate 
42877c478bd9Sstevel@tonic-gate 	wq = tep->te_wq;
42887c478bd9Sstevel@tonic-gate 
42897c478bd9Sstevel@tonic-gate 	/*
42907c478bd9Sstevel@tonic-gate 	 * Note: T_ADDR_REQ message has only PRIM_type field
42917c478bd9Sstevel@tonic-gate 	 * so it is already validated earlier.
42927c478bd9Sstevel@tonic-gate 	 */
42937c478bd9Sstevel@tonic-gate 
42947c478bd9Sstevel@tonic-gate 	if (IS_CLTS(tep) ||
42957c478bd9Sstevel@tonic-gate 	    (tep->te_state > TS_WREQ_ORDREL) ||
42967c478bd9Sstevel@tonic-gate 	    (tep->te_state < TS_DATA_XFER)) {
42977c478bd9Sstevel@tonic-gate 		/*
42987c478bd9Sstevel@tonic-gate 		 * Either connectionless or connection oriented but not
42997c478bd9Sstevel@tonic-gate 		 * in connected data transfer state or half-closed states.
43007c478bd9Sstevel@tonic-gate 		 */
43017c478bd9Sstevel@tonic-gate 		ack_sz = sizeof (struct T_addr_ack);
43027c478bd9Sstevel@tonic-gate 		if (tep->te_state >= TS_IDLE)
43037c478bd9Sstevel@tonic-gate 			/* is bound */
43047c478bd9Sstevel@tonic-gate 			ack_sz += tep->te_alen;
43057c478bd9Sstevel@tonic-gate 		ackmp = reallocb(mp, ack_sz, 0);
43067c478bd9Sstevel@tonic-gate 		if (ackmp == NULL) {
43077c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
4308cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4309fc80c0dfSnordmark 			    "tl_addr_req: reallocb failed"));
43107c478bd9Sstevel@tonic-gate 			tl_memrecover(wq, mp, ack_sz);
43117c478bd9Sstevel@tonic-gate 			return;
43127c478bd9Sstevel@tonic-gate 		}
43137c478bd9Sstevel@tonic-gate 
43147c478bd9Sstevel@tonic-gate 		taa = (struct T_addr_ack *)ackmp->b_rptr;
43157c478bd9Sstevel@tonic-gate 
43167c478bd9Sstevel@tonic-gate 		bzero(taa, sizeof (struct T_addr_ack));
43177c478bd9Sstevel@tonic-gate 
43187c478bd9Sstevel@tonic-gate 		taa->PRIM_type = T_ADDR_ACK;
43197c478bd9Sstevel@tonic-gate 		ackmp->b_datap->db_type = M_PCPROTO;
43207c478bd9Sstevel@tonic-gate 		ackmp->b_wptr = (uchar_t *)&taa[1];
43217c478bd9Sstevel@tonic-gate 
43227c478bd9Sstevel@tonic-gate 		if (tep->te_state >= TS_IDLE) {
43237c478bd9Sstevel@tonic-gate 			/* endpoint is bound */
43247c478bd9Sstevel@tonic-gate 			taa->LOCADDR_length = tep->te_alen;
43257c478bd9Sstevel@tonic-gate 			taa->LOCADDR_offset = (t_scalar_t)sizeof (*taa);
43267c478bd9Sstevel@tonic-gate 
43277c478bd9Sstevel@tonic-gate 			bcopy(tep->te_abuf, ackmp->b_wptr,
4328fc80c0dfSnordmark 			    tep->te_alen);
43297c478bd9Sstevel@tonic-gate 			ackmp->b_wptr += tep->te_alen;
43307c478bd9Sstevel@tonic-gate 			ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
43317c478bd9Sstevel@tonic-gate 		}
43327c478bd9Sstevel@tonic-gate 
43337c478bd9Sstevel@tonic-gate 		(void) qreply(wq, ackmp);
43347c478bd9Sstevel@tonic-gate 	} else {
43357c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_state == TS_DATA_XFER ||
4336fc80c0dfSnordmark 		    tep->te_state == TS_WIND_ORDREL ||
4337fc80c0dfSnordmark 		    tep->te_state == TS_WREQ_ORDREL);
43387c478bd9Sstevel@tonic-gate 		/* connection oriented in data transfer */
43397c478bd9Sstevel@tonic-gate 		tl_connected_cots_addr_req(mp, tep);
43407c478bd9Sstevel@tonic-gate 	}
43417c478bd9Sstevel@tonic-gate }
43427c478bd9Sstevel@tonic-gate 
43437c478bd9Sstevel@tonic-gate 
43447c478bd9Sstevel@tonic-gate static void
tl_connected_cots_addr_req(mblk_t * mp,tl_endpt_t * tep)43457c478bd9Sstevel@tonic-gate tl_connected_cots_addr_req(mblk_t *mp, tl_endpt_t *tep)
43467c478bd9Sstevel@tonic-gate {
4347bbaa8b60SDan Kruchinin 	tl_endpt_t		*peer_tep = tep->te_conp;
43487c478bd9Sstevel@tonic-gate 	size_t			ack_sz;
43497c478bd9Sstevel@tonic-gate 	mblk_t			*ackmp;
43507c478bd9Sstevel@tonic-gate 	struct T_addr_ack	*taa;
43517c478bd9Sstevel@tonic-gate 	uchar_t			*addr_startp;
43527c478bd9Sstevel@tonic-gate 
43537c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
43547c478bd9Sstevel@tonic-gate 		freemsg(mp);
43557c478bd9Sstevel@tonic-gate 		return;
43567c478bd9Sstevel@tonic-gate 	}
43577c478bd9Sstevel@tonic-gate 
4358bbaa8b60SDan Kruchinin 	if (peer_tep == NULL || peer_tep->te_closing) {
4359bbaa8b60SDan Kruchinin 		tl_error_ack(tep->te_wq, mp, TSYSERR, ECONNRESET, T_ADDR_REQ);
4360bbaa8b60SDan Kruchinin 		return;
4361bbaa8b60SDan Kruchinin 	}
4362bbaa8b60SDan Kruchinin 
43637c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_state >= TS_IDLE);
43647c478bd9Sstevel@tonic-gate 
43657c478bd9Sstevel@tonic-gate 	ack_sz = sizeof (struct T_addr_ack);
43667c478bd9Sstevel@tonic-gate 	ack_sz += T_ALIGN(tep->te_alen);
43677c478bd9Sstevel@tonic-gate 	ack_sz += peer_tep->te_alen;
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 	ackmp = tpi_ack_alloc(mp, ack_sz, M_PCPROTO, T_ADDR_ACK);
43707c478bd9Sstevel@tonic-gate 	if (ackmp == NULL) {
4371cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4372fc80c0dfSnordmark 		    "tl_connected_cots_addr_req: reallocb failed"));
43737c478bd9Sstevel@tonic-gate 		tl_memrecover(tep->te_wq, mp, ack_sz);
43747c478bd9Sstevel@tonic-gate 		return;
43757c478bd9Sstevel@tonic-gate 	}
43767c478bd9Sstevel@tonic-gate 
43777c478bd9Sstevel@tonic-gate 	taa = (struct T_addr_ack *)ackmp->b_rptr;
43787c478bd9Sstevel@tonic-gate 
43797c478bd9Sstevel@tonic-gate 	/* endpoint is bound */
43807c478bd9Sstevel@tonic-gate 	taa->LOCADDR_length = tep->te_alen;
43817c478bd9Sstevel@tonic-gate 	taa->LOCADDR_offset = (t_scalar_t)sizeof (*taa);
43827c478bd9Sstevel@tonic-gate 
43837c478bd9Sstevel@tonic-gate 	addr_startp = (uchar_t *)&taa[1];
43847c478bd9Sstevel@tonic-gate 
43857c478bd9Sstevel@tonic-gate 	bcopy(tep->te_abuf, addr_startp,
43867c478bd9Sstevel@tonic-gate 	    tep->te_alen);
43877c478bd9Sstevel@tonic-gate 
43887c478bd9Sstevel@tonic-gate 	taa->REMADDR_length = peer_tep->te_alen;
43897c478bd9Sstevel@tonic-gate 	taa->REMADDR_offset = (t_scalar_t)T_ALIGN(taa->LOCADDR_offset +
4390fc80c0dfSnordmark 	    taa->LOCADDR_length);
43917c478bd9Sstevel@tonic-gate 	addr_startp = ackmp->b_rptr + taa->REMADDR_offset;
43927c478bd9Sstevel@tonic-gate 	bcopy(peer_tep->te_abuf, addr_startp,
43937c478bd9Sstevel@tonic-gate 	    peer_tep->te_alen);
43947c478bd9Sstevel@tonic-gate 	ackmp->b_wptr = (uchar_t *)ackmp->b_rptr +
43957c478bd9Sstevel@tonic-gate 	    taa->REMADDR_offset + peer_tep->te_alen;
43967c478bd9Sstevel@tonic-gate 	ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
43977c478bd9Sstevel@tonic-gate 
43987c478bd9Sstevel@tonic-gate 	putnext(tep->te_rq, ackmp);
43997c478bd9Sstevel@tonic-gate }
44007c478bd9Sstevel@tonic-gate 
44017c478bd9Sstevel@tonic-gate static void
tl_copy_info(struct T_info_ack * ia,tl_endpt_t * tep)44027c478bd9Sstevel@tonic-gate tl_copy_info(struct T_info_ack *ia, tl_endpt_t *tep)
44037c478bd9Sstevel@tonic-gate {
44047c478bd9Sstevel@tonic-gate 	if (IS_CLTS(tep)) {
44057c478bd9Sstevel@tonic-gate 		*ia = tl_clts_info_ack;
44067c478bd9Sstevel@tonic-gate 		ia->TSDU_size = tl_tidusz; /* TSDU and TIDU size are same */
44077c478bd9Sstevel@tonic-gate 	} else {
44087c478bd9Sstevel@tonic-gate 		*ia = tl_cots_info_ack;
44097c478bd9Sstevel@tonic-gate 		if (IS_COTSORD(tep))
44107c478bd9Sstevel@tonic-gate 			ia->SERV_type = T_COTS_ORD;
44117c478bd9Sstevel@tonic-gate 	}
44127c478bd9Sstevel@tonic-gate 	ia->TIDU_size = tl_tidusz;
44137c478bd9Sstevel@tonic-gate 	ia->CURRENT_state = tep->te_state;
44147c478bd9Sstevel@tonic-gate }
44157c478bd9Sstevel@tonic-gate 
44167c478bd9Sstevel@tonic-gate /*
44177c478bd9Sstevel@tonic-gate  * This routine responds to T_CAPABILITY_REQ messages.  It is called by
44187c478bd9Sstevel@tonic-gate  * tl_wput.
44197c478bd9Sstevel@tonic-gate  */
44207c478bd9Sstevel@tonic-gate static void
tl_capability_req(mblk_t * mp,tl_endpt_t * tep)44217c478bd9Sstevel@tonic-gate tl_capability_req(mblk_t *mp, tl_endpt_t *tep)
44227c478bd9Sstevel@tonic-gate {
44237c478bd9Sstevel@tonic-gate 	mblk_t			*ackmp;
44247c478bd9Sstevel@tonic-gate 	t_uscalar_t		cap_bits1;
44257c478bd9Sstevel@tonic-gate 	struct T_capability_ack	*tcap;
44267c478bd9Sstevel@tonic-gate 
44277c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
44287c478bd9Sstevel@tonic-gate 		freemsg(mp);
44297c478bd9Sstevel@tonic-gate 		return;
44307c478bd9Sstevel@tonic-gate 	}
44317c478bd9Sstevel@tonic-gate 
44327c478bd9Sstevel@tonic-gate 	cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
44337c478bd9Sstevel@tonic-gate 
44347c478bd9Sstevel@tonic-gate 	ackmp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
44357c478bd9Sstevel@tonic-gate 	    M_PCPROTO, T_CAPABILITY_ACK);
44367c478bd9Sstevel@tonic-gate 	if (ackmp == NULL) {
4437cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4438fc80c0dfSnordmark 		    "tl_capability_req: reallocb failed"));
44397c478bd9Sstevel@tonic-gate 		tl_memrecover(tep->te_wq, mp,
44407c478bd9Sstevel@tonic-gate 		    sizeof (struct T_capability_ack));
44417c478bd9Sstevel@tonic-gate 		return;
44427c478bd9Sstevel@tonic-gate 	}
44437c478bd9Sstevel@tonic-gate 
44447c478bd9Sstevel@tonic-gate 	tcap = (struct T_capability_ack *)ackmp->b_rptr;
44457c478bd9Sstevel@tonic-gate 	tcap->CAP_bits1 = 0;
44467c478bd9Sstevel@tonic-gate 
44477c478bd9Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO) {
44487c478bd9Sstevel@tonic-gate 		tl_copy_info(&tcap->INFO_ack, tep);
44497c478bd9Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_INFO;
44507c478bd9Sstevel@tonic-gate 	}
44517c478bd9Sstevel@tonic-gate 
44527c478bd9Sstevel@tonic-gate 	if (cap_bits1 & TC1_ACCEPTOR_ID) {
44537c478bd9Sstevel@tonic-gate 		tcap->ACCEPTOR_id = tep->te_acceptor_id;
44547c478bd9Sstevel@tonic-gate 		tcap->CAP_bits1 |= TC1_ACCEPTOR_ID;
44557c478bd9Sstevel@tonic-gate 	}
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate 	putnext(tep->te_rq, ackmp);
44587c478bd9Sstevel@tonic-gate }
44597c478bd9Sstevel@tonic-gate 
44607c478bd9Sstevel@tonic-gate static void
tl_info_req_ser(mblk_t * mp,tl_endpt_t * tep)44617c478bd9Sstevel@tonic-gate tl_info_req_ser(mblk_t *mp, tl_endpt_t *tep)
44627c478bd9Sstevel@tonic-gate {
4463cb492d90SToomas Soome 	if (!tep->te_closing)
44647c478bd9Sstevel@tonic-gate 		tl_info_req(mp, tep);
44657c478bd9Sstevel@tonic-gate 	else
44667c478bd9Sstevel@tonic-gate 		freemsg(mp);
44677c478bd9Sstevel@tonic-gate 
44687c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
44697c478bd9Sstevel@tonic-gate 	tl_refrele(tep);
44707c478bd9Sstevel@tonic-gate }
44717c478bd9Sstevel@tonic-gate 
44727c478bd9Sstevel@tonic-gate static void
tl_info_req(mblk_t * mp,tl_endpt_t * tep)44737c478bd9Sstevel@tonic-gate tl_info_req(mblk_t *mp, tl_endpt_t *tep)
44747c478bd9Sstevel@tonic-gate {
44757c478bd9Sstevel@tonic-gate 	mblk_t *ackmp;
44767c478bd9Sstevel@tonic-gate 
44777c478bd9Sstevel@tonic-gate 	ackmp = tpi_ack_alloc(mp, sizeof (struct T_info_ack),
44787c478bd9Sstevel@tonic-gate 	    M_PCPROTO, T_INFO_ACK);
44797c478bd9Sstevel@tonic-gate 	if (ackmp == NULL) {
4480cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4481fc80c0dfSnordmark 		    "tl_info_req: reallocb failed"));
44827c478bd9Sstevel@tonic-gate 		tl_memrecover(tep->te_wq, mp, sizeof (struct T_info_ack));
44837c478bd9Sstevel@tonic-gate 		return;
44847c478bd9Sstevel@tonic-gate 	}
44857c478bd9Sstevel@tonic-gate 
44867c478bd9Sstevel@tonic-gate 	/*
44877c478bd9Sstevel@tonic-gate 	 * fill in T_INFO_ACK contents
44887c478bd9Sstevel@tonic-gate 	 */
44897c478bd9Sstevel@tonic-gate 	tl_copy_info((struct T_info_ack *)ackmp->b_rptr, tep);
44907c478bd9Sstevel@tonic-gate 
44917c478bd9Sstevel@tonic-gate 	/*
44927c478bd9Sstevel@tonic-gate 	 * send ack message
44937c478bd9Sstevel@tonic-gate 	 */
44947c478bd9Sstevel@tonic-gate 	putnext(tep->te_rq, ackmp);
44957c478bd9Sstevel@tonic-gate }
44967c478bd9Sstevel@tonic-gate 
44977c478bd9Sstevel@tonic-gate /*
44987c478bd9Sstevel@tonic-gate  * Handle M_DATA, T_data_req and T_optdata_req.
44997c478bd9Sstevel@tonic-gate  * If this is a socket pass through T_optdata_req options unmodified.
45007c478bd9Sstevel@tonic-gate  */
45017c478bd9Sstevel@tonic-gate static void
tl_data(mblk_t * mp,tl_endpt_t * tep)45027c478bd9Sstevel@tonic-gate tl_data(mblk_t *mp, tl_endpt_t *tep)
45037c478bd9Sstevel@tonic-gate {
45047c478bd9Sstevel@tonic-gate 	queue_t			*wq = tep->te_wq;
45057c478bd9Sstevel@tonic-gate 	union T_primitives	*prim = (union T_primitives *)mp->b_rptr;
45067c478bd9Sstevel@tonic-gate 	ssize_t			msz = MBLKL(mp);
45077c478bd9Sstevel@tonic-gate 	tl_endpt_t		*peer_tep;
45087c478bd9Sstevel@tonic-gate 	queue_t			*peer_rq;
45097c478bd9Sstevel@tonic-gate 	boolean_t		closing = tep->te_closing;
45107c478bd9Sstevel@tonic-gate 
45117c478bd9Sstevel@tonic-gate 	if (IS_CLTS(tep)) {
45127c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 2,
4513cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
4514fc80c0dfSnordmark 		    "tl_wput:clts:unattached M_DATA"));
45157c478bd9Sstevel@tonic-gate 		if (!closing) {
45167c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EPROTO);
45177c478bd9Sstevel@tonic-gate 		} else {
45187c478bd9Sstevel@tonic-gate 			freemsg(mp);
45197c478bd9Sstevel@tonic-gate 		}
45207c478bd9Sstevel@tonic-gate 		return;
45217c478bd9Sstevel@tonic-gate 	}
45227c478bd9Sstevel@tonic-gate 
45237c478bd9Sstevel@tonic-gate 	/*
45247c478bd9Sstevel@tonic-gate 	 * If the endpoint is closing it should still forward any data to the
45257c478bd9Sstevel@tonic-gate 	 * peer (if it has one). If it is not allowed to forward it can just
45267c478bd9Sstevel@tonic-gate 	 * free the message.
45277c478bd9Sstevel@tonic-gate 	 */
45287c478bd9Sstevel@tonic-gate 	if (closing &&
45297c478bd9Sstevel@tonic-gate 	    (tep->te_state != TS_DATA_XFER) &&
45307c478bd9Sstevel@tonic-gate 	    (tep->te_state != TS_WREQ_ORDREL)) {
45317c478bd9Sstevel@tonic-gate 		freemsg(mp);
45327c478bd9Sstevel@tonic-gate 		return;
45337c478bd9Sstevel@tonic-gate 	}
45347c478bd9Sstevel@tonic-gate 
45357c478bd9Sstevel@tonic-gate 	if (DB_TYPE(mp) == M_PROTO) {
45367c478bd9Sstevel@tonic-gate 		if (prim->type == T_DATA_REQ &&
45377c478bd9Sstevel@tonic-gate 		    msz < sizeof (struct T_data_req)) {
45387c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
4539cb492d90SToomas Soome 				SL_TRACE | SL_ERROR,
45407c478bd9Sstevel@tonic-gate 				"tl_data:T_DATA_REQ:invalid message"));
45417c478bd9Sstevel@tonic-gate 			if (!closing) {
45427c478bd9Sstevel@tonic-gate 				tl_merror(wq, mp, EPROTO);
45437c478bd9Sstevel@tonic-gate 			} else {
45447c478bd9Sstevel@tonic-gate 				freemsg(mp);
45457c478bd9Sstevel@tonic-gate 			}
45467c478bd9Sstevel@tonic-gate 			return;
45477c478bd9Sstevel@tonic-gate 		} else if (prim->type == T_OPTDATA_REQ &&
454819397407SSherry Moore 		    (msz < sizeof (struct T_optdata_req) || !IS_SOCKET(tep))) {
45497c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
4550cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4551fc80c0dfSnordmark 			    "tl_data:T_OPTDATA_REQ:invalid message"));
45527c478bd9Sstevel@tonic-gate 			if (!closing) {
45537c478bd9Sstevel@tonic-gate 				tl_merror(wq, mp, EPROTO);
45547c478bd9Sstevel@tonic-gate 			} else {
45557c478bd9Sstevel@tonic-gate 				freemsg(mp);
45567c478bd9Sstevel@tonic-gate 			}
45577c478bd9Sstevel@tonic-gate 			return;
45587c478bd9Sstevel@tonic-gate 		}
45597c478bd9Sstevel@tonic-gate 	}
45607c478bd9Sstevel@tonic-gate 
45617c478bd9Sstevel@tonic-gate 	/*
45627c478bd9Sstevel@tonic-gate 	 * connection oriented provider
45637c478bd9Sstevel@tonic-gate 	 */
45647c478bd9Sstevel@tonic-gate 	switch (tep->te_state) {
45657c478bd9Sstevel@tonic-gate 	case TS_IDLE:
45667c478bd9Sstevel@tonic-gate 		/*
45677c478bd9Sstevel@tonic-gate 		 * Other end not here - do nothing.
45687c478bd9Sstevel@tonic-gate 		 */
45697c478bd9Sstevel@tonic-gate 		freemsg(mp);
4570cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE | SL_ERROR,
4571fc80c0dfSnordmark 		    "tl_data:cots with endpoint idle"));
45727c478bd9Sstevel@tonic-gate 		return;
45737c478bd9Sstevel@tonic-gate 
45747c478bd9Sstevel@tonic-gate 	case TS_DATA_XFER:
45757c478bd9Sstevel@tonic-gate 		/* valid states */
45767c478bd9Sstevel@tonic-gate 		if (tep->te_conp != NULL)
45777c478bd9Sstevel@tonic-gate 			break;
45787c478bd9Sstevel@tonic-gate 
45797c478bd9Sstevel@tonic-gate 		if (tep->te_oconp == NULL) {
45807c478bd9Sstevel@tonic-gate 			if (!closing) {
45817c478bd9Sstevel@tonic-gate 				tl_merror(wq, mp, EPROTO);
45827c478bd9Sstevel@tonic-gate 			} else {
45837c478bd9Sstevel@tonic-gate 				freemsg(mp);
45847c478bd9Sstevel@tonic-gate 			}
45857c478bd9Sstevel@tonic-gate 			return;
45867c478bd9Sstevel@tonic-gate 		}
45877c478bd9Sstevel@tonic-gate 		/*
45887c478bd9Sstevel@tonic-gate 		 * For a socket the T_CONN_CON is sent early thus
45897c478bd9Sstevel@tonic-gate 		 * the peer might not yet have accepted the connection.
45907c478bd9Sstevel@tonic-gate 		 * If we are closing queue the packet with the T_CONN_IND.
45917c478bd9Sstevel@tonic-gate 		 * Otherwise defer processing the packet until the peer
45927c478bd9Sstevel@tonic-gate 		 * accepts the connection.
45937c478bd9Sstevel@tonic-gate 		 * Note that the queue is noenabled when we go into this
45947c478bd9Sstevel@tonic-gate 		 * state.
45957c478bd9Sstevel@tonic-gate 		 */
45967c478bd9Sstevel@tonic-gate 		if (!closing) {
45977c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
4598cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4599fc80c0dfSnordmark 			    "tl_data: ocon"));
46007c478bd9Sstevel@tonic-gate 			TL_PUTBQ(tep, mp);
46017c478bd9Sstevel@tonic-gate 			return;
46027c478bd9Sstevel@tonic-gate 		}
46037c478bd9Sstevel@tonic-gate 		if (DB_TYPE(mp) == M_PROTO) {
46047c478bd9Sstevel@tonic-gate 			if (msz < sizeof (t_scalar_t)) {
46057c478bd9Sstevel@tonic-gate 				freemsg(mp);
46067c478bd9Sstevel@tonic-gate 				return;
46077c478bd9Sstevel@tonic-gate 			}
46087c478bd9Sstevel@tonic-gate 			/* reuse message block - just change REQ to IND */
46097c478bd9Sstevel@tonic-gate 			if (prim->type == T_DATA_REQ)
46107c478bd9Sstevel@tonic-gate 				prim->type = T_DATA_IND;
46117c478bd9Sstevel@tonic-gate 			else
46127c478bd9Sstevel@tonic-gate 				prim->type = T_OPTDATA_IND;
46137c478bd9Sstevel@tonic-gate 		}
46147c478bd9Sstevel@tonic-gate 		tl_icon_queuemsg(tep->te_oconp, tep->te_seqno, mp);
46157c478bd9Sstevel@tonic-gate 		return;
46167c478bd9Sstevel@tonic-gate 
46177c478bd9Sstevel@tonic-gate 	case TS_WREQ_ORDREL:
46187c478bd9Sstevel@tonic-gate 		if (tep->te_conp == NULL) {
46197c478bd9Sstevel@tonic-gate 			/*
46207c478bd9Sstevel@tonic-gate 			 * Other end closed - generate discon_ind
46217c478bd9Sstevel@tonic-gate 			 * with reason 0 to cause an EPIPE but no
46227c478bd9Sstevel@tonic-gate 			 * read side error on AF_UNIX sockets.
46237c478bd9Sstevel@tonic-gate 			 */
46247c478bd9Sstevel@tonic-gate 			freemsg(mp);
46257c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
4626cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4627fc80c0dfSnordmark 			    "tl_data: WREQ_ORDREL and no peer"));
46287c478bd9Sstevel@tonic-gate 			tl_discon_ind(tep, 0);
46297c478bd9Sstevel@tonic-gate 			return;
46307c478bd9Sstevel@tonic-gate 		}
46317c478bd9Sstevel@tonic-gate 		break;
46327c478bd9Sstevel@tonic-gate 
46337c478bd9Sstevel@tonic-gate 	default:
46347c478bd9Sstevel@tonic-gate 		/* invalid state for event TE_DATA_REQ */
4635cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4636fc80c0dfSnordmark 		    "tl_data:cots:out of state"));
46377c478bd9Sstevel@tonic-gate 		tl_merror(wq, mp, EPROTO);
46387c478bd9Sstevel@tonic-gate 		return;
46397c478bd9Sstevel@tonic-gate 	}
46407c478bd9Sstevel@tonic-gate 	/*
4641b8ffbd31SJohn Levon 	 * tep->te_state = nextstate[TE_DATA_REQ][tep->te_state];
46427c478bd9Sstevel@tonic-gate 	 * (State stays same on this event)
46437c478bd9Sstevel@tonic-gate 	 */
46447c478bd9Sstevel@tonic-gate 
46457c478bd9Sstevel@tonic-gate 	/*
46467c478bd9Sstevel@tonic-gate 	 * get connected endpoint
46477c478bd9Sstevel@tonic-gate 	 */
46487c478bd9Sstevel@tonic-gate 	if (((peer_tep = tep->te_conp) == NULL) || peer_tep->te_closing) {
46497c478bd9Sstevel@tonic-gate 		freemsg(mp);
46507c478bd9Sstevel@tonic-gate 		/* Peer closed */
46517c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE,
4652fc80c0dfSnordmark 		    "tl_data: peer gone"));
46537c478bd9Sstevel@tonic-gate 		return;
46547c478bd9Sstevel@tonic-gate 	}
46557c478bd9Sstevel@tonic-gate 
46567c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_serializer == peer_tep->te_serializer);
46577c478bd9Sstevel@tonic-gate 	peer_rq = peer_tep->te_rq;
46587c478bd9Sstevel@tonic-gate 
46597c478bd9Sstevel@tonic-gate 	/*
46607c478bd9Sstevel@tonic-gate 	 * Put it back if flow controlled
46617c478bd9Sstevel@tonic-gate 	 * Note: Messages already on queue when we are closing is bounded
46627c478bd9Sstevel@tonic-gate 	 * so we can ignore flow control.
46637c478bd9Sstevel@tonic-gate 	 */
46647c478bd9Sstevel@tonic-gate 	if (!canputnext(peer_rq) && !closing) {
46657c478bd9Sstevel@tonic-gate 		TL_PUTBQ(tep, mp);
46667c478bd9Sstevel@tonic-gate 		return;
46677c478bd9Sstevel@tonic-gate 	}
46687c478bd9Sstevel@tonic-gate 
46697c478bd9Sstevel@tonic-gate 	/*
46707c478bd9Sstevel@tonic-gate 	 * validate peer state
46717c478bd9Sstevel@tonic-gate 	 */
46727c478bd9Sstevel@tonic-gate 	switch (peer_tep->te_state) {
46737c478bd9Sstevel@tonic-gate 	case TS_DATA_XFER:
46747c478bd9Sstevel@tonic-gate 	case TS_WIND_ORDREL:
46757c478bd9Sstevel@tonic-gate 		/* valid states */
46767c478bd9Sstevel@tonic-gate 		break;
46777c478bd9Sstevel@tonic-gate 	default:
4678cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4679fc80c0dfSnordmark 		    "tl_data:rx side:invalid state"));
46807c478bd9Sstevel@tonic-gate 		tl_merror(peer_tep->te_wq, mp, EPROTO);
46817c478bd9Sstevel@tonic-gate 		return;
46827c478bd9Sstevel@tonic-gate 	}
46837c478bd9Sstevel@tonic-gate 	if (DB_TYPE(mp) == M_PROTO) {
46847c478bd9Sstevel@tonic-gate 		/* reuse message block - just change REQ to IND */
46857c478bd9Sstevel@tonic-gate 		if (prim->type == T_DATA_REQ)
46867c478bd9Sstevel@tonic-gate 			prim->type = T_DATA_IND;
46877c478bd9Sstevel@tonic-gate 		else
46887c478bd9Sstevel@tonic-gate 			prim->type = T_OPTDATA_IND;
46897c478bd9Sstevel@tonic-gate 	}
46907c478bd9Sstevel@tonic-gate 	/*
4691b8ffbd31SJohn Levon 	 * peer_tep->te_state = nextstate[TE_DATA_IND][peer_tep->te_state];
46927c478bd9Sstevel@tonic-gate 	 * (peer state stays same on this event)
46937c478bd9Sstevel@tonic-gate 	 */
46947c478bd9Sstevel@tonic-gate 	/*
46957c478bd9Sstevel@tonic-gate 	 * send data to connected peer
46967c478bd9Sstevel@tonic-gate 	 */
46977c478bd9Sstevel@tonic-gate 	putnext(peer_rq, mp);
46987c478bd9Sstevel@tonic-gate }
46997c478bd9Sstevel@tonic-gate 
47007c478bd9Sstevel@tonic-gate 
47017c478bd9Sstevel@tonic-gate 
47027c478bd9Sstevel@tonic-gate static void
tl_exdata(mblk_t * mp,tl_endpt_t * tep)47037c478bd9Sstevel@tonic-gate tl_exdata(mblk_t *mp, tl_endpt_t *tep)
47047c478bd9Sstevel@tonic-gate {
47057c478bd9Sstevel@tonic-gate 	queue_t			*wq = tep->te_wq;
47067c478bd9Sstevel@tonic-gate 	union T_primitives	*prim = (union T_primitives *)mp->b_rptr;
47077c478bd9Sstevel@tonic-gate 	ssize_t			msz = MBLKL(mp);
47087c478bd9Sstevel@tonic-gate 	tl_endpt_t		*peer_tep;
47097c478bd9Sstevel@tonic-gate 	queue_t			*peer_rq;
47107c478bd9Sstevel@tonic-gate 	boolean_t		closing = tep->te_closing;
47117c478bd9Sstevel@tonic-gate 
47127c478bd9Sstevel@tonic-gate 	if (msz < sizeof (struct T_exdata_req)) {
4713cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4714fc80c0dfSnordmark 		    "tl_exdata:invalid message"));
47157c478bd9Sstevel@tonic-gate 		if (!closing) {
47167c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EPROTO);
47177c478bd9Sstevel@tonic-gate 		} else {
47187c478bd9Sstevel@tonic-gate 			freemsg(mp);
47197c478bd9Sstevel@tonic-gate 		}
47207c478bd9Sstevel@tonic-gate 		return;
47217c478bd9Sstevel@tonic-gate 	}
47227c478bd9Sstevel@tonic-gate 
47237c478bd9Sstevel@tonic-gate 	/*
47247c478bd9Sstevel@tonic-gate 	 * If the endpoint is closing it should still forward any data to the
47257c478bd9Sstevel@tonic-gate 	 * peer (if it has one). If it is not allowed to forward it can just
47267c478bd9Sstevel@tonic-gate 	 * free the message.
47277c478bd9Sstevel@tonic-gate 	 */
47287c478bd9Sstevel@tonic-gate 	if (closing &&
47297c478bd9Sstevel@tonic-gate 	    (tep->te_state != TS_DATA_XFER) &&
47307c478bd9Sstevel@tonic-gate 	    (tep->te_state != TS_WREQ_ORDREL)) {
47317c478bd9Sstevel@tonic-gate 		freemsg(mp);
47327c478bd9Sstevel@tonic-gate 		return;
47337c478bd9Sstevel@tonic-gate 	}
47347c478bd9Sstevel@tonic-gate 
47357c478bd9Sstevel@tonic-gate 	/*
47367c478bd9Sstevel@tonic-gate 	 * validate state
47377c478bd9Sstevel@tonic-gate 	 */
47387c478bd9Sstevel@tonic-gate 	switch (tep->te_state) {
47397c478bd9Sstevel@tonic-gate 	case TS_IDLE:
47407c478bd9Sstevel@tonic-gate 		/*
47417c478bd9Sstevel@tonic-gate 		 * Other end not here - do nothing.
47427c478bd9Sstevel@tonic-gate 		 */
47437c478bd9Sstevel@tonic-gate 		freemsg(mp);
4744cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE | SL_ERROR,
4745fc80c0dfSnordmark 		    "tl_exdata:cots with endpoint idle"));
47467c478bd9Sstevel@tonic-gate 		return;
47477c478bd9Sstevel@tonic-gate 
47487c478bd9Sstevel@tonic-gate 	case TS_DATA_XFER:
47497c478bd9Sstevel@tonic-gate 		/* valid states */
47507c478bd9Sstevel@tonic-gate 		if (tep->te_conp != NULL)
47517c478bd9Sstevel@tonic-gate 			break;
47527c478bd9Sstevel@tonic-gate 
47537c478bd9Sstevel@tonic-gate 		if (tep->te_oconp == NULL) {
47547c478bd9Sstevel@tonic-gate 			if (!closing) {
47557c478bd9Sstevel@tonic-gate 				tl_merror(wq, mp, EPROTO);
47567c478bd9Sstevel@tonic-gate 			} else {
47577c478bd9Sstevel@tonic-gate 				freemsg(mp);
47587c478bd9Sstevel@tonic-gate 			}
47597c478bd9Sstevel@tonic-gate 			return;
47607c478bd9Sstevel@tonic-gate 		}
47617c478bd9Sstevel@tonic-gate 		/*
47627c478bd9Sstevel@tonic-gate 		 * For a socket the T_CONN_CON is sent early thus
47637c478bd9Sstevel@tonic-gate 		 * the peer might not yet have accepted the connection.
47647c478bd9Sstevel@tonic-gate 		 * If we are closing queue the packet with the T_CONN_IND.
47657c478bd9Sstevel@tonic-gate 		 * Otherwise defer processing the packet until the peer
47667c478bd9Sstevel@tonic-gate 		 * accepts the connection.
47677c478bd9Sstevel@tonic-gate 		 * Note that the queue is noenabled when we go into this
47687c478bd9Sstevel@tonic-gate 		 * state.
47697c478bd9Sstevel@tonic-gate 		 */
47707c478bd9Sstevel@tonic-gate 		if (!closing) {
47717c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
4772cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4773fc80c0dfSnordmark 			    "tl_exdata: ocon"));
47747c478bd9Sstevel@tonic-gate 			TL_PUTBQ(tep, mp);
47757c478bd9Sstevel@tonic-gate 			return;
47767c478bd9Sstevel@tonic-gate 		}
4777cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4778fc80c0dfSnordmark 		    "tl_exdata: closing socket ocon"));
47797c478bd9Sstevel@tonic-gate 		prim->type = T_EXDATA_IND;
47807c478bd9Sstevel@tonic-gate 		tl_icon_queuemsg(tep->te_oconp, tep->te_seqno, mp);
47817c478bd9Sstevel@tonic-gate 		return;
47827c478bd9Sstevel@tonic-gate 
47837c478bd9Sstevel@tonic-gate 	case TS_WREQ_ORDREL:
47847c478bd9Sstevel@tonic-gate 		if (tep->te_conp == NULL) {
47857c478bd9Sstevel@tonic-gate 			/*
47867c478bd9Sstevel@tonic-gate 			 * Other end closed - generate discon_ind
47877c478bd9Sstevel@tonic-gate 			 * with reason 0 to cause an EPIPE but no
47887c478bd9Sstevel@tonic-gate 			 * read side error on AF_UNIX sockets.
47897c478bd9Sstevel@tonic-gate 			 */
47907c478bd9Sstevel@tonic-gate 			freemsg(mp);
47917c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
4792cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4793fc80c0dfSnordmark 			    "tl_exdata: WREQ_ORDREL and no peer"));
47947c478bd9Sstevel@tonic-gate 			tl_discon_ind(tep, 0);
47957c478bd9Sstevel@tonic-gate 			return;
47967c478bd9Sstevel@tonic-gate 		}
47977c478bd9Sstevel@tonic-gate 		break;
47987c478bd9Sstevel@tonic-gate 
47997c478bd9Sstevel@tonic-gate 	default:
48007c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
4801cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
4802fc80c0dfSnordmark 		    "tl_wput:T_EXDATA_REQ:out of state, state=%d",
4803fc80c0dfSnordmark 		    tep->te_state));
48047c478bd9Sstevel@tonic-gate 		tl_merror(wq, mp, EPROTO);
48057c478bd9Sstevel@tonic-gate 		return;
48067c478bd9Sstevel@tonic-gate 	}
48077c478bd9Sstevel@tonic-gate 	/*
4808b8ffbd31SJohn Levon 	 * tep->te_state = nextstate[TE_EXDATA_REQ][tep->te_state];
48097c478bd9Sstevel@tonic-gate 	 * (state stays same on this event)
48107c478bd9Sstevel@tonic-gate 	 */
48117c478bd9Sstevel@tonic-gate 
48127c478bd9Sstevel@tonic-gate 	/*
48137c478bd9Sstevel@tonic-gate 	 * get connected endpoint
48147c478bd9Sstevel@tonic-gate 	 */
48157c478bd9Sstevel@tonic-gate 	if (((peer_tep = tep->te_conp) == NULL) || peer_tep->te_closing) {
48167c478bd9Sstevel@tonic-gate 		freemsg(mp);
48177c478bd9Sstevel@tonic-gate 		/* Peer closed */
48187c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE,
4819fc80c0dfSnordmark 		    "tl_exdata: peer gone"));
48207c478bd9Sstevel@tonic-gate 		return;
48217c478bd9Sstevel@tonic-gate 	}
48227c478bd9Sstevel@tonic-gate 
48237c478bd9Sstevel@tonic-gate 	peer_rq = peer_tep->te_rq;
48247c478bd9Sstevel@tonic-gate 
48257c478bd9Sstevel@tonic-gate 	/*
48267c478bd9Sstevel@tonic-gate 	 * Put it back if flow controlled
48277c478bd9Sstevel@tonic-gate 	 * Note: Messages already on queue when we are closing is bounded
48287c478bd9Sstevel@tonic-gate 	 * so we can ignore flow control.
48297c478bd9Sstevel@tonic-gate 	 */
48307c478bd9Sstevel@tonic-gate 	if (!canputnext(peer_rq) && !closing) {
48317c478bd9Sstevel@tonic-gate 		TL_PUTBQ(tep, mp);
48327c478bd9Sstevel@tonic-gate 		return;
48337c478bd9Sstevel@tonic-gate 	}
48347c478bd9Sstevel@tonic-gate 
48357c478bd9Sstevel@tonic-gate 	/*
48367c478bd9Sstevel@tonic-gate 	 * validate state on peer
48377c478bd9Sstevel@tonic-gate 	 */
48387c478bd9Sstevel@tonic-gate 	switch (peer_tep->te_state) {
48397c478bd9Sstevel@tonic-gate 	case TS_DATA_XFER:
48407c478bd9Sstevel@tonic-gate 	case TS_WIND_ORDREL:
48417c478bd9Sstevel@tonic-gate 		/* valid states */
48427c478bd9Sstevel@tonic-gate 		break;
48437c478bd9Sstevel@tonic-gate 	default:
4844cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4845fc80c0dfSnordmark 		    "tl_exdata:rx side:invalid state"));
48467c478bd9Sstevel@tonic-gate 		tl_merror(peer_tep->te_wq, mp, EPROTO);
48477c478bd9Sstevel@tonic-gate 		return;
48487c478bd9Sstevel@tonic-gate 	}
48497c478bd9Sstevel@tonic-gate 	/*
4850b8ffbd31SJohn Levon 	 * peer_tep->te_state = nextstate[TE_DATA_IND][peer_tep->te_state];
48517c478bd9Sstevel@tonic-gate 	 * (peer state stays same on this event)
48527c478bd9Sstevel@tonic-gate 	 */
48537c478bd9Sstevel@tonic-gate 	/*
48547c478bd9Sstevel@tonic-gate 	 * reuse message block
48557c478bd9Sstevel@tonic-gate 	 */
48567c478bd9Sstevel@tonic-gate 	prim->type = T_EXDATA_IND;
48577c478bd9Sstevel@tonic-gate 
48587c478bd9Sstevel@tonic-gate 	/*
48597c478bd9Sstevel@tonic-gate 	 * send data to connected peer
48607c478bd9Sstevel@tonic-gate 	 */
48617c478bd9Sstevel@tonic-gate 	putnext(peer_rq, mp);
48627c478bd9Sstevel@tonic-gate }
48637c478bd9Sstevel@tonic-gate 
48647c478bd9Sstevel@tonic-gate 
48657c478bd9Sstevel@tonic-gate 
48667c478bd9Sstevel@tonic-gate static void
tl_ordrel(mblk_t * mp,tl_endpt_t * tep)48677c478bd9Sstevel@tonic-gate tl_ordrel(mblk_t *mp, tl_endpt_t *tep)
48687c478bd9Sstevel@tonic-gate {
4869cb492d90SToomas Soome 	queue_t			*wq = tep->te_wq;
48707c478bd9Sstevel@tonic-gate 	union T_primitives	*prim = (union T_primitives *)mp->b_rptr;
48717c478bd9Sstevel@tonic-gate 	ssize_t			msz = MBLKL(mp);
48727c478bd9Sstevel@tonic-gate 	tl_endpt_t		*peer_tep;
48737c478bd9Sstevel@tonic-gate 	queue_t			*peer_rq;
48747c478bd9Sstevel@tonic-gate 	boolean_t		closing = tep->te_closing;
48757c478bd9Sstevel@tonic-gate 
48767c478bd9Sstevel@tonic-gate 	if (msz < sizeof (struct T_ordrel_req)) {
4877cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4878fc80c0dfSnordmark 		    "tl_ordrel:invalid message"));
48797c478bd9Sstevel@tonic-gate 		if (!closing) {
48807c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EPROTO);
48817c478bd9Sstevel@tonic-gate 		} else {
48827c478bd9Sstevel@tonic-gate 			freemsg(mp);
48837c478bd9Sstevel@tonic-gate 		}
48847c478bd9Sstevel@tonic-gate 		return;
48857c478bd9Sstevel@tonic-gate 	}
48867c478bd9Sstevel@tonic-gate 
48877c478bd9Sstevel@tonic-gate 	/*
48887c478bd9Sstevel@tonic-gate 	 * validate state
48897c478bd9Sstevel@tonic-gate 	 */
48907c478bd9Sstevel@tonic-gate 	switch (tep->te_state) {
48917c478bd9Sstevel@tonic-gate 	case TS_DATA_XFER:
48927c478bd9Sstevel@tonic-gate 	case TS_WREQ_ORDREL:
48937c478bd9Sstevel@tonic-gate 		/* valid states */
48947c478bd9Sstevel@tonic-gate 		if (tep->te_conp != NULL)
48957c478bd9Sstevel@tonic-gate 			break;
48967c478bd9Sstevel@tonic-gate 
48977c478bd9Sstevel@tonic-gate 		if (tep->te_oconp == NULL)
48987c478bd9Sstevel@tonic-gate 			break;
48997c478bd9Sstevel@tonic-gate 
49007c478bd9Sstevel@tonic-gate 		/*
49017c478bd9Sstevel@tonic-gate 		 * For a socket the T_CONN_CON is sent early thus
49027c478bd9Sstevel@tonic-gate 		 * the peer might not yet have accepted the connection.
49037c478bd9Sstevel@tonic-gate 		 * If we are closing queue the packet with the T_CONN_IND.
49047c478bd9Sstevel@tonic-gate 		 * Otherwise defer processing the packet until the peer
49057c478bd9Sstevel@tonic-gate 		 * accepts the connection.
49067c478bd9Sstevel@tonic-gate 		 * Note that the queue is noenabled when we go into this
49077c478bd9Sstevel@tonic-gate 		 * state.
49087c478bd9Sstevel@tonic-gate 		 */
49097c478bd9Sstevel@tonic-gate 		if (!closing) {
49107c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
4911cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
4912fc80c0dfSnordmark 			    "tl_ordlrel: ocon"));
49137c478bd9Sstevel@tonic-gate 			TL_PUTBQ(tep, mp);
49147c478bd9Sstevel@tonic-gate 			return;
49157c478bd9Sstevel@tonic-gate 		}
4916cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4917fc80c0dfSnordmark 		    "tl_ordlrel: closing socket ocon"));
49187c478bd9Sstevel@tonic-gate 		prim->type = T_ORDREL_IND;
49197c478bd9Sstevel@tonic-gate 		(void) tl_icon_queuemsg(tep->te_oconp, tep->te_seqno, mp);
49207c478bd9Sstevel@tonic-gate 		return;
49217c478bd9Sstevel@tonic-gate 
49227c478bd9Sstevel@tonic-gate 	default:
49237c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
4924cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
4925fc80c0dfSnordmark 		    "tl_wput:T_ORDREL_REQ:out of state, state=%d",
4926fc80c0dfSnordmark 		    tep->te_state));
49277c478bd9Sstevel@tonic-gate 		if (!closing) {
49287c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EPROTO);
49297c478bd9Sstevel@tonic-gate 		} else {
49307c478bd9Sstevel@tonic-gate 			freemsg(mp);
49317c478bd9Sstevel@tonic-gate 		}
49327c478bd9Sstevel@tonic-gate 		return;
49337c478bd9Sstevel@tonic-gate 	}
4934b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_ORDREL_REQ][tep->te_state];
49357c478bd9Sstevel@tonic-gate 
49367c478bd9Sstevel@tonic-gate 	/*
49377c478bd9Sstevel@tonic-gate 	 * get connected endpoint
49387c478bd9Sstevel@tonic-gate 	 */
49397c478bd9Sstevel@tonic-gate 	if (((peer_tep = tep->te_conp) == NULL) || peer_tep->te_closing) {
49407c478bd9Sstevel@tonic-gate 		/* Peer closed */
49417c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE,
4942fc80c0dfSnordmark 		    "tl_ordrel: peer gone"));
49437c478bd9Sstevel@tonic-gate 		freemsg(mp);
49447c478bd9Sstevel@tonic-gate 		return;
49457c478bd9Sstevel@tonic-gate 	}
49467c478bd9Sstevel@tonic-gate 
49477c478bd9Sstevel@tonic-gate 	peer_rq = peer_tep->te_rq;
49487c478bd9Sstevel@tonic-gate 
49497c478bd9Sstevel@tonic-gate 	/*
49507c478bd9Sstevel@tonic-gate 	 * Put it back if flow controlled except when we are closing.
49517c478bd9Sstevel@tonic-gate 	 * Note: Messages already on queue when we are closing is bounded
49527c478bd9Sstevel@tonic-gate 	 * so we can ignore flow control.
49537c478bd9Sstevel@tonic-gate 	 */
4954cb492d90SToomas Soome 	if (!canputnext(peer_rq) && !closing) {
49557c478bd9Sstevel@tonic-gate 		TL_PUTBQ(tep, mp);
49567c478bd9Sstevel@tonic-gate 		return;
49577c478bd9Sstevel@tonic-gate 	}
49587c478bd9Sstevel@tonic-gate 
49597c478bd9Sstevel@tonic-gate 	/*
49607c478bd9Sstevel@tonic-gate 	 * validate state on peer
49617c478bd9Sstevel@tonic-gate 	 */
49627c478bd9Sstevel@tonic-gate 	switch (peer_tep->te_state) {
49637c478bd9Sstevel@tonic-gate 	case TS_DATA_XFER:
49647c478bd9Sstevel@tonic-gate 	case TS_WIND_ORDREL:
49657c478bd9Sstevel@tonic-gate 		/* valid states */
49667c478bd9Sstevel@tonic-gate 		break;
49677c478bd9Sstevel@tonic-gate 	default:
4968cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
4969fc80c0dfSnordmark 		    "tl_ordrel:rx side:invalid state"));
49707c478bd9Sstevel@tonic-gate 		tl_merror(peer_tep->te_wq, mp, EPROTO);
49717c478bd9Sstevel@tonic-gate 		return;
49727c478bd9Sstevel@tonic-gate 	}
4973b8ffbd31SJohn Levon 	peer_tep->te_state = nextstate[TE_ORDREL_IND][peer_tep->te_state];
49747c478bd9Sstevel@tonic-gate 
49757c478bd9Sstevel@tonic-gate 	/*
49767c478bd9Sstevel@tonic-gate 	 * reuse message block
49777c478bd9Sstevel@tonic-gate 	 */
49787c478bd9Sstevel@tonic-gate 	prim->type = T_ORDREL_IND;
49797c478bd9Sstevel@tonic-gate 	(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE,
4980fc80c0dfSnordmark 	    "tl_ordrel: send ordrel_ind"));
49817c478bd9Sstevel@tonic-gate 
49827c478bd9Sstevel@tonic-gate 	/*
49837c478bd9Sstevel@tonic-gate 	 * send data to connected peer
49847c478bd9Sstevel@tonic-gate 	 */
49857c478bd9Sstevel@tonic-gate 	putnext(peer_rq, mp);
49867c478bd9Sstevel@tonic-gate }
49877c478bd9Sstevel@tonic-gate 
49887c478bd9Sstevel@tonic-gate 
49897c478bd9Sstevel@tonic-gate /*
49907c478bd9Sstevel@tonic-gate  * Send T_UDERROR_IND. The error should be from the <sys/errno.h> space.
49917c478bd9Sstevel@tonic-gate  */
49927c478bd9Sstevel@tonic-gate static void
tl_uderr(queue_t * wq,mblk_t * mp,t_scalar_t err)49937c478bd9Sstevel@tonic-gate tl_uderr(queue_t *wq, mblk_t *mp, t_scalar_t err)
49947c478bd9Sstevel@tonic-gate {
49957c478bd9Sstevel@tonic-gate 	size_t			err_sz;
49967c478bd9Sstevel@tonic-gate 	tl_endpt_t		*tep;
49977c478bd9Sstevel@tonic-gate 	struct T_unitdata_req	*udreq;
49987c478bd9Sstevel@tonic-gate 	mblk_t			*err_mp;
49997c478bd9Sstevel@tonic-gate 	t_scalar_t		alen;
50007c478bd9Sstevel@tonic-gate 	t_scalar_t		olen;
50017c478bd9Sstevel@tonic-gate 	struct T_uderror_ind	*uderr;
50027c478bd9Sstevel@tonic-gate 	uchar_t			*addr_startp;
50037c478bd9Sstevel@tonic-gate 
50047c478bd9Sstevel@tonic-gate 	err_sz = sizeof (struct T_uderror_ind);
50057c478bd9Sstevel@tonic-gate 	tep = (tl_endpt_t *)wq->q_ptr;
50067c478bd9Sstevel@tonic-gate 	udreq = (struct T_unitdata_req *)mp->b_rptr;
50077c478bd9Sstevel@tonic-gate 	alen = udreq->DEST_length;
50087c478bd9Sstevel@tonic-gate 	olen = udreq->OPT_length;
50097c478bd9Sstevel@tonic-gate 
50107c478bd9Sstevel@tonic-gate 	if (alen > 0)
50117c478bd9Sstevel@tonic-gate 		err_sz = T_ALIGN(err_sz + alen);
50127c478bd9Sstevel@tonic-gate 	if (olen > 0)
50137c478bd9Sstevel@tonic-gate 		err_sz += olen;
50147c478bd9Sstevel@tonic-gate 
50157c478bd9Sstevel@tonic-gate 	err_mp = allocb(err_sz, BPRI_MED);
5016cb492d90SToomas Soome 	if (err_mp == NULL) {
5017cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE | SL_ERROR,
5018fc80c0dfSnordmark 		    "tl_uderr:allocb failure"));
50197c478bd9Sstevel@tonic-gate 		/*
50207c478bd9Sstevel@tonic-gate 		 * Note: no rollback of state needed as it does
50217c478bd9Sstevel@tonic-gate 		 * not change in connectionless transport
50227c478bd9Sstevel@tonic-gate 		 */
50237c478bd9Sstevel@tonic-gate 		tl_memrecover(wq, mp, err_sz);
50247c478bd9Sstevel@tonic-gate 		return;
50257c478bd9Sstevel@tonic-gate 	}
50267c478bd9Sstevel@tonic-gate 
50277c478bd9Sstevel@tonic-gate 	DB_TYPE(err_mp) = M_PROTO;
50287c478bd9Sstevel@tonic-gate 	err_mp->b_wptr = err_mp->b_rptr + err_sz;
50297c478bd9Sstevel@tonic-gate 	uderr = (struct T_uderror_ind *)err_mp->b_rptr;
50307c478bd9Sstevel@tonic-gate 	uderr->PRIM_type = T_UDERROR_IND;
50317c478bd9Sstevel@tonic-gate 	uderr->ERROR_type = err;
50327c478bd9Sstevel@tonic-gate 	uderr->DEST_length = alen;
50337c478bd9Sstevel@tonic-gate 	uderr->OPT_length = olen;
50347c478bd9Sstevel@tonic-gate 	if (alen <= 0) {
50357c478bd9Sstevel@tonic-gate 		uderr->DEST_offset = 0;
50367c478bd9Sstevel@tonic-gate 	} else {
50377c478bd9Sstevel@tonic-gate 		uderr->DEST_offset =
5038fc80c0dfSnordmark 		    (t_scalar_t)sizeof (struct T_uderror_ind);
5039cb492d90SToomas Soome 		addr_startp = mp->b_rptr + udreq->DEST_offset;
50407c478bd9Sstevel@tonic-gate 		bcopy(addr_startp, err_mp->b_rptr + uderr->DEST_offset,
5041fc80c0dfSnordmark 		    (size_t)alen);
50427c478bd9Sstevel@tonic-gate 	}
50437c478bd9Sstevel@tonic-gate 	if (olen <= 0) {
50447c478bd9Sstevel@tonic-gate 		uderr->OPT_offset = 0;
50457c478bd9Sstevel@tonic-gate 	} else {
50467c478bd9Sstevel@tonic-gate 		uderr->OPT_offset =
5047fc80c0dfSnordmark 		    (t_scalar_t)T_ALIGN(sizeof (struct T_uderror_ind) +
5048fc80c0dfSnordmark 		    uderr->DEST_length);
5049cb492d90SToomas Soome 		addr_startp = mp->b_rptr + udreq->OPT_offset;
50507c478bd9Sstevel@tonic-gate 		bcopy(addr_startp, err_mp->b_rptr+uderr->OPT_offset,
5051fc80c0dfSnordmark 		    (size_t)olen);
50527c478bd9Sstevel@tonic-gate 	}
50537c478bd9Sstevel@tonic-gate 	freemsg(mp);
50547c478bd9Sstevel@tonic-gate 
50557c478bd9Sstevel@tonic-gate 	/*
50567c478bd9Sstevel@tonic-gate 	 * send indication message
50577c478bd9Sstevel@tonic-gate 	 */
5058b8ffbd31SJohn Levon 	tep->te_state = nextstate[TE_UDERROR_IND][tep->te_state];
50597c478bd9Sstevel@tonic-gate 
50607c478bd9Sstevel@tonic-gate 	qreply(wq, err_mp);
50617c478bd9Sstevel@tonic-gate }
50627c478bd9Sstevel@tonic-gate 
50637c478bd9Sstevel@tonic-gate static void
tl_unitdata_ser(mblk_t * mp,tl_endpt_t * tep)50647c478bd9Sstevel@tonic-gate tl_unitdata_ser(mblk_t *mp, tl_endpt_t *tep)
50657c478bd9Sstevel@tonic-gate {
50667c478bd9Sstevel@tonic-gate 	queue_t *wq = tep->te_wq;
50677c478bd9Sstevel@tonic-gate 
50687c478bd9Sstevel@tonic-gate 	if (!tep->te_closing && (wq->q_first != NULL)) {
50697c478bd9Sstevel@tonic-gate 		TL_PUTQ(tep, mp);
5070cb492d90SToomas Soome 	} else {
5071cb492d90SToomas Soome 		if (tep->te_rq != NULL)
5072cb492d90SToomas Soome 			tl_unitdata(mp, tep);
5073cb492d90SToomas Soome 		else
5074cb492d90SToomas Soome 			freemsg(mp);
5075cb492d90SToomas Soome 	}
50767c478bd9Sstevel@tonic-gate 
50777c478bd9Sstevel@tonic-gate 	tl_serializer_exit(tep);
50787c478bd9Sstevel@tonic-gate 	tl_refrele(tep);
50797c478bd9Sstevel@tonic-gate }
50807c478bd9Sstevel@tonic-gate 
50817c478bd9Sstevel@tonic-gate /*
50827c478bd9Sstevel@tonic-gate  * Handle T_unitdata_req.
50837c478bd9Sstevel@tonic-gate  * If TL_SET[U]CRED or TL_SOCKUCRED generate the credentials options.
50847c478bd9Sstevel@tonic-gate  * If this is a socket pass through options unmodified.
50857c478bd9Sstevel@tonic-gate  */
50867c478bd9Sstevel@tonic-gate static void
tl_unitdata(mblk_t * mp,tl_endpt_t * tep)50877c478bd9Sstevel@tonic-gate tl_unitdata(mblk_t *mp, tl_endpt_t *tep)
50887c478bd9Sstevel@tonic-gate {
50897c478bd9Sstevel@tonic-gate 	queue_t			*wq = tep->te_wq;
50907c478bd9Sstevel@tonic-gate 	soux_addr_t		ux_addr;
50917c478bd9Sstevel@tonic-gate 	tl_addr_t		destaddr;
50927c478bd9Sstevel@tonic-gate 	uchar_t			*addr_startp;
50937c478bd9Sstevel@tonic-gate 	tl_endpt_t		*peer_tep;
50947c478bd9Sstevel@tonic-gate 	struct T_unitdata_ind	*udind;
50957c478bd9Sstevel@tonic-gate 	struct T_unitdata_req	*udreq;
5096d28d4716SJerry Jelinek 	ssize_t			msz, ui_sz, reuse_mb_sz;
50977c478bd9Sstevel@tonic-gate 	t_scalar_t		alen, aoff, olen, ooff;
50987c478bd9Sstevel@tonic-gate 	t_scalar_t		oldolen = 0;
509967dbe2beSCasper H.S. Dik 	cred_t			*cr = NULL;
510067dbe2beSCasper H.S. Dik 	pid_t			cpid;
51017c478bd9Sstevel@tonic-gate 
51027c478bd9Sstevel@tonic-gate 	udreq = (struct T_unitdata_req *)mp->b_rptr;
51037c478bd9Sstevel@tonic-gate 	msz = MBLKL(mp);
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate 	/*
51067c478bd9Sstevel@tonic-gate 	 * validate the state
51077c478bd9Sstevel@tonic-gate 	 */
51087c478bd9Sstevel@tonic-gate 	if (tep->te_state != TS_IDLE) {
51097c478bd9Sstevel@tonic-gate 		(void) (STRLOG(TL_ID, tep->te_minor, 1,
5110cb492d90SToomas Soome 		    SL_TRACE | SL_ERROR,
5111fc80c0dfSnordmark 		    "tl_wput:T_CONN_REQ:out of state"));
51127c478bd9Sstevel@tonic-gate 		tl_merror(wq, mp, EPROTO);
51137c478bd9Sstevel@tonic-gate 		return;
51147c478bd9Sstevel@tonic-gate 	}
51157c478bd9Sstevel@tonic-gate 	/*
5116b8ffbd31SJohn Levon 	 * tep->te_state = nextstate[TE_UNITDATA_REQ][tep->te_state];
51177c478bd9Sstevel@tonic-gate 	 * (state does not change on this event)
51187c478bd9Sstevel@tonic-gate 	 */
51197c478bd9Sstevel@tonic-gate 
51207c478bd9Sstevel@tonic-gate 	/*
51217c478bd9Sstevel@tonic-gate 	 * validate the message
51227c478bd9Sstevel@tonic-gate 	 * Note: dereference fields in struct inside message only
51237c478bd9Sstevel@tonic-gate 	 * after validating the message length.
51247c478bd9Sstevel@tonic-gate 	 */
51257c478bd9Sstevel@tonic-gate 	if (msz < sizeof (struct T_unitdata_req)) {
5126cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
5127fc80c0dfSnordmark 		    "tl_unitdata:invalid message length"));
51287c478bd9Sstevel@tonic-gate 		tl_merror(wq, mp, EINVAL);
51297c478bd9Sstevel@tonic-gate 		return;
51307c478bd9Sstevel@tonic-gate 	}
51317c478bd9Sstevel@tonic-gate 	alen = udreq->DEST_length;
51327c478bd9Sstevel@tonic-gate 	aoff = udreq->DEST_offset;
51337c478bd9Sstevel@tonic-gate 	oldolen = olen = udreq->OPT_length;
51347c478bd9Sstevel@tonic-gate 	ooff = udreq->OPT_offset;
51357c478bd9Sstevel@tonic-gate 	if (olen == 0)
51367c478bd9Sstevel@tonic-gate 		ooff = 0;
51377c478bd9Sstevel@tonic-gate 
51387c478bd9Sstevel@tonic-gate 	if (IS_SOCKET(tep)) {
51397c478bd9Sstevel@tonic-gate 		if ((alen != TL_SOUX_ADDRLEN) ||
51407c478bd9Sstevel@tonic-gate 		    (aoff < 0) ||
51417c478bd9Sstevel@tonic-gate 		    (aoff + alen > msz) ||
51427c478bd9Sstevel@tonic-gate 		    (olen < 0) || (ooff < 0) ||
51437c478bd9Sstevel@tonic-gate 		    ((olen > 0) && ((ooff + olen) > msz))) {
51447c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
5145cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
5146fc80c0dfSnordmark 			    "tl_unitdata_req: invalid socket addr "
5147fc80c0dfSnordmark 			    "(msz=%d, al=%d, ao=%d, ol=%d, oo = %d)",
5148fc80c0dfSnordmark 			    (int)msz, alen, aoff, olen, ooff));
51497c478bd9Sstevel@tonic-gate 			tl_error_ack(wq, mp, TSYSERR, EINVAL, T_UNITDATA_REQ);
51507c478bd9Sstevel@tonic-gate 			return;
51517c478bd9Sstevel@tonic-gate 		}
51527c478bd9Sstevel@tonic-gate 		bcopy(mp->b_rptr + aoff, &ux_addr, TL_SOUX_ADDRLEN);
51537c478bd9Sstevel@tonic-gate 
51547c478bd9Sstevel@tonic-gate 		if ((ux_addr.soua_magic != SOU_MAGIC_IMPLICIT) &&
51557c478bd9Sstevel@tonic-gate 		    (ux_addr.soua_magic != SOU_MAGIC_EXPLICIT)) {
51567c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor,
5157cb492d90SToomas Soome 			    1, SL_TRACE | SL_ERROR,
5158fc80c0dfSnordmark 			    "tl_conn_req: invalid socket magic"));
51597c478bd9Sstevel@tonic-gate 			tl_error_ack(wq, mp, TSYSERR, EINVAL, T_UNITDATA_REQ);
51607c478bd9Sstevel@tonic-gate 			return;
51617c478bd9Sstevel@tonic-gate 		}
51627c478bd9Sstevel@tonic-gate 	} else {
51637c478bd9Sstevel@tonic-gate 		if ((alen < 0) ||
51647c478bd9Sstevel@tonic-gate 		    (aoff < 0) ||
51657c478bd9Sstevel@tonic-gate 		    ((alen > 0) && ((aoff + alen) > msz)) ||
51667c478bd9Sstevel@tonic-gate 		    ((ssize_t)alen > (msz - sizeof (struct T_unitdata_req))) ||
51677c478bd9Sstevel@tonic-gate 		    ((aoff + alen) < 0) ||
51687c478bd9Sstevel@tonic-gate 		    ((olen > 0) && ((ooff + olen) > msz)) ||
51697c478bd9Sstevel@tonic-gate 		    (olen < 0) ||
51707c478bd9Sstevel@tonic-gate 		    (ooff < 0) ||
51717c478bd9Sstevel@tonic-gate 		    ((ssize_t)olen > (msz - sizeof (struct T_unitdata_req)))) {
51727c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
5173cb492d90SToomas Soome 				    SL_TRACE | SL_ERROR,
51747c478bd9Sstevel@tonic-gate 				    "tl_unitdata:invalid unit data message"));
51757c478bd9Sstevel@tonic-gate 			tl_merror(wq, mp, EINVAL);
51767c478bd9Sstevel@tonic-gate 			return;
51777c478bd9Sstevel@tonic-gate 		}
51787c478bd9Sstevel@tonic-gate 	}
51797c478bd9Sstevel@tonic-gate 
51807c478bd9Sstevel@tonic-gate 	/* Options not supported unless it's a socket */
51817c478bd9Sstevel@tonic-gate 	if (alen == 0 || (olen != 0 && !IS_SOCKET(tep))) {
5182cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE | SL_ERROR,
51837c478bd9Sstevel@tonic-gate 		    "tl_unitdata:option use(unsupported) or zero len addr"));
51847c478bd9Sstevel@tonic-gate 		tl_uderr(wq, mp, EPROTO);
51857c478bd9Sstevel@tonic-gate 		return;
51867c478bd9Sstevel@tonic-gate 	}
51877c478bd9Sstevel@tonic-gate #ifdef DEBUG
51887c478bd9Sstevel@tonic-gate 	/*
51897c478bd9Sstevel@tonic-gate 	 * Mild form of ASSERT()ion to detect broken TPI apps.
5190cb492d90SToomas Soome 	 * if (!assertion)
51917c478bd9Sstevel@tonic-gate 	 *	log warning;
51927c478bd9Sstevel@tonic-gate 	 */
5193cb492d90SToomas Soome 	if (!(aoff >= (t_scalar_t)sizeof (struct T_unitdata_req))) {
5194cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE | SL_ERROR,
5195fc80c0dfSnordmark 		    "tl_unitdata:addr overlaps TPI message"));
51967c478bd9Sstevel@tonic-gate 	}
51977c478bd9Sstevel@tonic-gate #endif
51987c478bd9Sstevel@tonic-gate 	/*
51997c478bd9Sstevel@tonic-gate 	 * get destination endpoint
52007c478bd9Sstevel@tonic-gate 	 */
52017c478bd9Sstevel@tonic-gate 	destaddr.ta_alen = alen;
52027c478bd9Sstevel@tonic-gate 	destaddr.ta_abuf = mp->b_rptr + aoff;
52037c478bd9Sstevel@tonic-gate 	destaddr.ta_zoneid = tep->te_zoneid;
52047c478bd9Sstevel@tonic-gate 
52057c478bd9Sstevel@tonic-gate 	/*
52067c478bd9Sstevel@tonic-gate 	 * Check whether the destination is the same that was used previously
52077c478bd9Sstevel@tonic-gate 	 * and the destination endpoint is in the right state. If something is
52087c478bd9Sstevel@tonic-gate 	 * wrong, find destination again and cache it.
52097c478bd9Sstevel@tonic-gate 	 */
52107c478bd9Sstevel@tonic-gate 	peer_tep = tep->te_lastep;
52117c478bd9Sstevel@tonic-gate 
52127c478bd9Sstevel@tonic-gate 	if ((peer_tep == NULL) || peer_tep->te_closing ||
52137c478bd9Sstevel@tonic-gate 	    (peer_tep->te_state != TS_IDLE) ||
52147c478bd9Sstevel@tonic-gate 	    !tl_eqaddr(&destaddr, &peer_tep->te_ap)) {
52157c478bd9Sstevel@tonic-gate 		/*
52167c478bd9Sstevel@tonic-gate 		 * Not the same as cached destination , need to find the right
52177c478bd9Sstevel@tonic-gate 		 * destination.
52187c478bd9Sstevel@tonic-gate 		 */
52197c478bd9Sstevel@tonic-gate 		peer_tep = (IS_SOCKET(tep) ?
52207c478bd9Sstevel@tonic-gate 		    tl_sock_find_peer(tep, &ux_addr) :
52217c478bd9Sstevel@tonic-gate 		    tl_find_peer(tep, &destaddr));
52227c478bd9Sstevel@tonic-gate 
52237c478bd9Sstevel@tonic-gate 		if (peer_tep == NULL) {
52247c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
5225cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
5226fc80c0dfSnordmark 			    "tl_unitdata:no one at destination address"));
52277c478bd9Sstevel@tonic-gate 			tl_uderr(wq, mp, ECONNRESET);
52287c478bd9Sstevel@tonic-gate 			return;
52297c478bd9Sstevel@tonic-gate 		}
52307c478bd9Sstevel@tonic-gate 
52317c478bd9Sstevel@tonic-gate 		/*
52327c478bd9Sstevel@tonic-gate 		 * Cache the new peer.
52337c478bd9Sstevel@tonic-gate 		 */
52347c478bd9Sstevel@tonic-gate 		if (tep->te_lastep != NULL)
52357c478bd9Sstevel@tonic-gate 			tl_refrele(tep->te_lastep);
52367c478bd9Sstevel@tonic-gate 
52377c478bd9Sstevel@tonic-gate 		tep->te_lastep = peer_tep;
52387c478bd9Sstevel@tonic-gate 	}
52397c478bd9Sstevel@tonic-gate 
52407c478bd9Sstevel@tonic-gate 	if (peer_tep->te_state != TS_IDLE) {
5241cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
5242fc80c0dfSnordmark 		    "tl_unitdata:provider in invalid state"));
52437c478bd9Sstevel@tonic-gate 		tl_uderr(wq, mp, EPROTO);
52447c478bd9Sstevel@tonic-gate 		return;
52457c478bd9Sstevel@tonic-gate 	}
52467c478bd9Sstevel@tonic-gate 
52477c478bd9Sstevel@tonic-gate 	ASSERT(peer_tep->te_rq != NULL);
52487c478bd9Sstevel@tonic-gate 
52497c478bd9Sstevel@tonic-gate 	/*
52507c478bd9Sstevel@tonic-gate 	 * Put it back if flow controlled except when we are closing.
52517c478bd9Sstevel@tonic-gate 	 * Note: Messages already on queue when we are closing is bounded
52527c478bd9Sstevel@tonic-gate 	 * so we can ignore flow control.
52537c478bd9Sstevel@tonic-gate 	 */
52547c478bd9Sstevel@tonic-gate 	if (!canputnext(peer_tep->te_rq) && !(tep->te_closing)) {
52557c478bd9Sstevel@tonic-gate 		/* record what we are flow controlled on */
52567c478bd9Sstevel@tonic-gate 		if (tep->te_flowq != NULL) {
52577c478bd9Sstevel@tonic-gate 			list_remove(&tep->te_flowq->te_flowlist, tep);
52587c478bd9Sstevel@tonic-gate 		}
52597c478bd9Sstevel@tonic-gate 		list_insert_head(&peer_tep->te_flowlist, tep);
52607c478bd9Sstevel@tonic-gate 		tep->te_flowq = peer_tep;
52617c478bd9Sstevel@tonic-gate 		TL_PUTBQ(tep, mp);
52627c478bd9Sstevel@tonic-gate 		return;
52637c478bd9Sstevel@tonic-gate 	}
52647c478bd9Sstevel@tonic-gate 	/*
52657c478bd9Sstevel@tonic-gate 	 * prepare indication message
52667c478bd9Sstevel@tonic-gate 	 */
52677c478bd9Sstevel@tonic-gate 
52687c478bd9Sstevel@tonic-gate 	/*
52697c478bd9Sstevel@tonic-gate 	 * calculate length of message
52707c478bd9Sstevel@tonic-gate 	 */
5271cb492d90SToomas Soome 	if (peer_tep->te_flag & (TL_SETCRED | TL_SETUCRED | TL_SOCKUCRED)) {
527267dbe2beSCasper H.S. Dik 		cr = msg_getcred(mp, &cpid);
527367dbe2beSCasper H.S. Dik 		ASSERT(cr != NULL);
527467dbe2beSCasper H.S. Dik 
527567dbe2beSCasper H.S. Dik 		if (peer_tep->te_flag & TL_SETCRED) {
527667dbe2beSCasper H.S. Dik 			ASSERT(olen == 0);
527767dbe2beSCasper H.S. Dik 			olen = (t_scalar_t)sizeof (struct opthdr) +
527867dbe2beSCasper H.S. Dik 			    OPTLEN(sizeof (tl_credopt_t));
527967dbe2beSCasper H.S. Dik 						/* 1 option only */
528067dbe2beSCasper H.S. Dik 		} else if (peer_tep->te_flag & TL_SETUCRED) {
528167dbe2beSCasper H.S. Dik 			ASSERT(olen == 0);
528267dbe2beSCasper H.S. Dik 			olen = (t_scalar_t)sizeof (struct opthdr) +
528367dbe2beSCasper H.S. Dik 			    OPTLEN(ucredminsize(cr));
528467dbe2beSCasper H.S. Dik 						/* 1 option only */
528567dbe2beSCasper H.S. Dik 		} else {
528667dbe2beSCasper H.S. Dik 			/* Possibly more than one option */
528767dbe2beSCasper H.S. Dik 			olen += (t_scalar_t)sizeof (struct T_opthdr) +
528867dbe2beSCasper H.S. Dik 			    OPTLEN(ucredminsize(cr));
528967dbe2beSCasper H.S. Dik 		}
52907c478bd9Sstevel@tonic-gate 	}
52917c478bd9Sstevel@tonic-gate 
5292d28d4716SJerry Jelinek 	ui_sz = T_ALIGN(sizeof (struct T_unitdata_ind) + tep->te_alen) + olen;
5293d28d4716SJerry Jelinek 	reuse_mb_sz = T_ALIGN(sizeof (struct T_unitdata_ind) + alen) + olen;
5294d28d4716SJerry Jelinek 
52957c478bd9Sstevel@tonic-gate 	/*
52967c478bd9Sstevel@tonic-gate 	 * If the unitdata_ind fits and we are not adding options
52977c478bd9Sstevel@tonic-gate 	 * reuse the udreq mblk.
5298d28d4716SJerry Jelinek 	 *
5299d28d4716SJerry Jelinek 	 * Otherwise, it is possible we need to append an option if one of the
5300d28d4716SJerry Jelinek 	 * te_flag bits is set. This requires extra space in the data block for
5301d28d4716SJerry Jelinek 	 * the additional option but the traditional technique used below to
5302d28d4716SJerry Jelinek 	 * allocate a new block and copy into it will not work when there is a
5303d28d4716SJerry Jelinek 	 * message block with a free pointer (since we don't know anything
5304d28d4716SJerry Jelinek 	 * about the layout of the data, pointers referencing or within the
5305d28d4716SJerry Jelinek 	 * data, etc.). To handle this possibility the upper layers may have
5306d28d4716SJerry Jelinek 	 * preallocated some space to use for appending an option. We check the
5307d28d4716SJerry Jelinek 	 * overall mblock size against the size we need ('reuse_mb_sz' with the
5308d28d4716SJerry Jelinek 	 * original address length [alen] to ensure we won't overrun the
5309d28d4716SJerry Jelinek 	 * current mblk data size) to see if there is free space and thus
5310d28d4716SJerry Jelinek 	 * avoid allocating a new message block.
53117c478bd9Sstevel@tonic-gate 	 */
53127c478bd9Sstevel@tonic-gate 	if (msz >= ui_sz && alen >= tep->te_alen &&
5313cb492d90SToomas Soome 	    !(peer_tep->te_flag & (TL_SETCRED | TL_SETUCRED | TL_SOCKUCRED))) {
53147c478bd9Sstevel@tonic-gate 		/*
53157c478bd9Sstevel@tonic-gate 		 * Reuse the original mblk. Leave options in place.
53167c478bd9Sstevel@tonic-gate 		 */
5317cb492d90SToomas Soome 		udind = (struct T_unitdata_ind *)mp->b_rptr;
53187c478bd9Sstevel@tonic-gate 		udind->PRIM_type = T_UNITDATA_IND;
53197c478bd9Sstevel@tonic-gate 		udind->SRC_length = tep->te_alen;
53207c478bd9Sstevel@tonic-gate 		addr_startp = mp->b_rptr + udind->SRC_offset;
53217c478bd9Sstevel@tonic-gate 		bcopy(tep->te_abuf, addr_startp, tep->te_alen);
5322d28d4716SJerry Jelinek 
5323d28d4716SJerry Jelinek 	} else if (MBLKSIZE(mp) >= reuse_mb_sz && alen >= tep->te_alen &&
5324d28d4716SJerry Jelinek 	    mp->b_datap->db_frtnp != NULL) {
5325d28d4716SJerry Jelinek 		/*
5326d28d4716SJerry Jelinek 		 * We have a message block with a free pointer, but extra space
5327d28d4716SJerry Jelinek 		 * has been pre-allocated for us in case we need to append an
5328d28d4716SJerry Jelinek 		 * option. Reuse the original mblk, leaving existing options in
5329d28d4716SJerry Jelinek 		 * place.
5330d28d4716SJerry Jelinek 		 */
5331cb492d90SToomas Soome 		udind = (struct T_unitdata_ind *)mp->b_rptr;
5332d28d4716SJerry Jelinek 		udind->PRIM_type = T_UNITDATA_IND;
5333d28d4716SJerry Jelinek 		udind->SRC_length = tep->te_alen;
5334d28d4716SJerry Jelinek 		addr_startp = mp->b_rptr + udind->SRC_offset;
5335d28d4716SJerry Jelinek 		bcopy(tep->te_abuf, addr_startp, tep->te_alen);
5336d28d4716SJerry Jelinek 
5337cb492d90SToomas Soome 		if (peer_tep->te_flag &
5338cb492d90SToomas Soome 		    (TL_SETCRED | TL_SETUCRED | TL_SOCKUCRED)) {
5339d28d4716SJerry Jelinek 			ASSERT(cr != NULL);
5340d28d4716SJerry Jelinek 			/*
5341d28d4716SJerry Jelinek 			 * We're appending one new option here after the
5342d28d4716SJerry Jelinek 			 * original ones.
5343d28d4716SJerry Jelinek 			 */
5344d28d4716SJerry Jelinek 			tl_fill_option(mp->b_rptr + udind->OPT_offset + oldolen,
5345d28d4716SJerry Jelinek 			    cr, cpid, peer_tep->te_flag, peer_tep->te_credp);
5346d28d4716SJerry Jelinek 		}
5347d28d4716SJerry Jelinek 
5348d28d4716SJerry Jelinek 	} else if (mp->b_datap->db_frtnp != NULL) {
5349d28d4716SJerry Jelinek 		/*
5350d28d4716SJerry Jelinek 		 * The next block creates a new mp and tries to copy the data
5351d28d4716SJerry Jelinek 		 * block into it, but that cannot handle a message with a free
5352d28d4716SJerry Jelinek 		 * pointer (for more details see the comment in kstrputmsg()
5353d28d4716SJerry Jelinek 		 * where dupmsg() is called). Since we can never properly
5354d28d4716SJerry Jelinek 		 * duplicate the mp while also extending the data, just error
5355d28d4716SJerry Jelinek 		 * out now.
5356d28d4716SJerry Jelinek 		 */
5357d28d4716SJerry Jelinek 		tl_uderr(wq, mp, EPROTO);
5358d28d4716SJerry Jelinek 		return;
53597c478bd9Sstevel@tonic-gate 	} else {
5360d28d4716SJerry Jelinek 		/* Allocate a new T_unitdata_ind message */
53617c478bd9Sstevel@tonic-gate 		mblk_t *ui_mp;
53627c478bd9Sstevel@tonic-gate 
53637c478bd9Sstevel@tonic-gate 		ui_mp = allocb(ui_sz, BPRI_MED);
5364cb492d90SToomas Soome 		if (ui_mp == NULL) {
53657c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 4, SL_TRACE,
5366fc80c0dfSnordmark 			    "tl_unitdata:allocb failure:message queued"));
53677c478bd9Sstevel@tonic-gate 			tl_memrecover(wq, mp, ui_sz);
53687c478bd9Sstevel@tonic-gate 			return;
53697c478bd9Sstevel@tonic-gate 		}
53707c478bd9Sstevel@tonic-gate 
53717c478bd9Sstevel@tonic-gate 		/*
53727c478bd9Sstevel@tonic-gate 		 * fill in T_UNITDATA_IND contents
53737c478bd9Sstevel@tonic-gate 		 */
53747c478bd9Sstevel@tonic-gate 		DB_TYPE(ui_mp) = M_PROTO;
53757c478bd9Sstevel@tonic-gate 		ui_mp->b_wptr = ui_mp->b_rptr + ui_sz;
5376cb492d90SToomas Soome 		udind = (struct T_unitdata_ind *)ui_mp->b_rptr;
53777c478bd9Sstevel@tonic-gate 		udind->PRIM_type = T_UNITDATA_IND;
53787c478bd9Sstevel@tonic-gate 		udind->SRC_offset = (t_scalar_t)sizeof (struct T_unitdata_ind);
53797c478bd9Sstevel@tonic-gate 		udind->SRC_length = tep->te_alen;
53807c478bd9Sstevel@tonic-gate 		addr_startp = ui_mp->b_rptr + udind->SRC_offset;
53817c478bd9Sstevel@tonic-gate 		bcopy(tep->te_abuf, addr_startp, tep->te_alen);
53827c478bd9Sstevel@tonic-gate 		udind->OPT_offset =
53837c478bd9Sstevel@tonic-gate 		    (t_scalar_t)T_ALIGN(udind->SRC_offset + udind->SRC_length);
53847c478bd9Sstevel@tonic-gate 		udind->OPT_length = olen;
5385cb492d90SToomas Soome 		if (peer_tep->te_flag &
5386cb492d90SToomas Soome 		    (TL_SETCRED | TL_SETUCRED | TL_SOCKUCRED)) {
5387de8c4a14SErik Nordmark 
53887c478bd9Sstevel@tonic-gate 			if (oldolen != 0) {
53897c478bd9Sstevel@tonic-gate 				bcopy((void *)((uintptr_t)udreq + ooff),
53907c478bd9Sstevel@tonic-gate 				    (void *)((uintptr_t)udind +
53917c478bd9Sstevel@tonic-gate 				    udind->OPT_offset),
53927c478bd9Sstevel@tonic-gate 				    oldolen);
53937c478bd9Sstevel@tonic-gate 			}
5394de8c4a14SErik Nordmark 			ASSERT(cr != NULL);
5395de8c4a14SErik Nordmark 
53967c478bd9Sstevel@tonic-gate 			tl_fill_option(ui_mp->b_rptr + udind->OPT_offset +
5397de8c4a14SErik Nordmark 			    oldolen, cr, cpid,
539845916cd2Sjpk 			    peer_tep->te_flag, peer_tep->te_credp);
53997c478bd9Sstevel@tonic-gate 		} else {
54007c478bd9Sstevel@tonic-gate 			bcopy((void *)((uintptr_t)udreq + ooff),
5401fc80c0dfSnordmark 			    (void *)((uintptr_t)udind + udind->OPT_offset),
5402fc80c0dfSnordmark 			    olen);
54037c478bd9Sstevel@tonic-gate 		}
54047c478bd9Sstevel@tonic-gate 
54057c478bd9Sstevel@tonic-gate 		/*
54067c478bd9Sstevel@tonic-gate 		 * relink data blocks from mp to ui_mp
54077c478bd9Sstevel@tonic-gate 		 */
54087c478bd9Sstevel@tonic-gate 		ui_mp->b_cont = mp->b_cont;
54097c478bd9Sstevel@tonic-gate 		freeb(mp);
54107c478bd9Sstevel@tonic-gate 		mp = ui_mp;
54117c478bd9Sstevel@tonic-gate 	}
54127c478bd9Sstevel@tonic-gate 	/*
54137c478bd9Sstevel@tonic-gate 	 * send indication message
54147c478bd9Sstevel@tonic-gate 	 */
5415b8ffbd31SJohn Levon 	peer_tep->te_state = nextstate[TE_UNITDATA_IND][peer_tep->te_state];
54167c478bd9Sstevel@tonic-gate 	putnext(peer_tep->te_rq, mp);
54177c478bd9Sstevel@tonic-gate }
54187c478bd9Sstevel@tonic-gate 
54197c478bd9Sstevel@tonic-gate 
54207c478bd9Sstevel@tonic-gate 
54217c478bd9Sstevel@tonic-gate /*
54227c478bd9Sstevel@tonic-gate  * Check if a given addr is in use.
54237c478bd9Sstevel@tonic-gate  * Endpoint ptr returned or NULL if not found.
54247c478bd9Sstevel@tonic-gate  * The name space is separate for each mode. This implies that
54257c478bd9Sstevel@tonic-gate  * sockets get their own name space.
54267c478bd9Sstevel@tonic-gate  */
54277c478bd9Sstevel@tonic-gate static tl_endpt_t *
tl_find_peer(tl_endpt_t * tep,tl_addr_t * ap)54287c478bd9Sstevel@tonic-gate tl_find_peer(tl_endpt_t *tep, tl_addr_t *ap)
54297c478bd9Sstevel@tonic-gate {
54307c478bd9Sstevel@tonic-gate 	tl_endpt_t *peer_tep = NULL;
54317c478bd9Sstevel@tonic-gate 	int rc = mod_hash_find_cb(tep->te_addrhash, (mod_hash_key_t)ap,
54327c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&peer_tep, tl_find_callback);
54337c478bd9Sstevel@tonic-gate 
5434cb492d90SToomas Soome 	ASSERT(!IS_SOCKET(tep));
54357c478bd9Sstevel@tonic-gate 
54367c478bd9Sstevel@tonic-gate 	ASSERT(ap != NULL && ap->ta_alen > 0);
54377c478bd9Sstevel@tonic-gate 	ASSERT(ap->ta_zoneid == tep->te_zoneid);
54387c478bd9Sstevel@tonic-gate 	ASSERT(ap->ta_abuf != NULL);
543956f33205SJonathan Adams 	EQUIV(rc == 0, peer_tep != NULL);
544056f33205SJonathan Adams 	IMPLY(rc == 0,
5441fc80c0dfSnordmark 	    (tep->te_zoneid == peer_tep->te_zoneid) &&
544256f33205SJonathan Adams 	    (tep->te_transport == peer_tep->te_transport));
54437c478bd9Sstevel@tonic-gate 
54447c478bd9Sstevel@tonic-gate 	if ((rc == 0) && (peer_tep->te_closing)) {
54457c478bd9Sstevel@tonic-gate 		tl_refrele(peer_tep);
54467c478bd9Sstevel@tonic-gate 		peer_tep = NULL;
54477c478bd9Sstevel@tonic-gate 	}
54487c478bd9Sstevel@tonic-gate 
54497c478bd9Sstevel@tonic-gate 	return (peer_tep);
54507c478bd9Sstevel@tonic-gate }
54517c478bd9Sstevel@tonic-gate 
54527c478bd9Sstevel@tonic-gate /*
54537c478bd9Sstevel@tonic-gate  * Find peer for a socket based on unix domain address.
54547c478bd9Sstevel@tonic-gate  * For implicit addresses our peer can be found by minor number in ai hash. For
54559bf9355bSRic Aleshire  * explicit binds we look vnode address at addr_hash.
54567c478bd9Sstevel@tonic-gate  */
54577c478bd9Sstevel@tonic-gate static tl_endpt_t *
tl_sock_find_peer(tl_endpt_t * tep,soux_addr_t * ux_addr)54587c478bd9Sstevel@tonic-gate tl_sock_find_peer(tl_endpt_t *tep, soux_addr_t *ux_addr)
54597c478bd9Sstevel@tonic-gate {
54607c478bd9Sstevel@tonic-gate 	tl_endpt_t *peer_tep = NULL;
54617c478bd9Sstevel@tonic-gate 	mod_hash_t *hash = ux_addr->soua_magic == SOU_MAGIC_IMPLICIT ?
54627c478bd9Sstevel@tonic-gate 	    tep->te_aihash : tep->te_addrhash;
54637c478bd9Sstevel@tonic-gate 	int rc = mod_hash_find_cb(hash, (mod_hash_key_t)ux_addr->soua_vp,
54647c478bd9Sstevel@tonic-gate 	    (mod_hash_val_t *)&peer_tep, tl_find_callback);
54657c478bd9Sstevel@tonic-gate 
54667c478bd9Sstevel@tonic-gate 	ASSERT(IS_SOCKET(tep));
546756f33205SJonathan Adams 	EQUIV(rc == 0, peer_tep != NULL);
546856f33205SJonathan Adams 	IMPLY(rc == 0, (tep->te_transport == peer_tep->te_transport));
54699bf9355bSRic Aleshire 
54709bf9355bSRic Aleshire 	if (peer_tep != NULL) {
54719bf9355bSRic Aleshire 		/* Don't attempt to use closing peer. */
54729bf9355bSRic Aleshire 		if (peer_tep->te_closing)
54739bf9355bSRic Aleshire 			goto errout;
54749bf9355bSRic Aleshire 
54759bf9355bSRic Aleshire 		/*
54769bf9355bSRic Aleshire 		 * Cross-zone unix sockets are permitted, but for Trusted
54779bf9355bSRic Aleshire 		 * Extensions only, the "server" for these must be in the
54789bf9355bSRic Aleshire 		 * global zone.
54799bf9355bSRic Aleshire 		 */
54809bf9355bSRic Aleshire 		if ((peer_tep->te_zoneid != tep->te_zoneid) &&
54819bf9355bSRic Aleshire 		    is_system_labeled() &&
54829bf9355bSRic Aleshire 		    (peer_tep->te_zoneid != GLOBAL_ZONEID))
54839bf9355bSRic Aleshire 			goto errout;
54847c478bd9Sstevel@tonic-gate 	}
54857c478bd9Sstevel@tonic-gate 
54867c478bd9Sstevel@tonic-gate 	return (peer_tep);
54879bf9355bSRic Aleshire 
54889bf9355bSRic Aleshire errout:
54899bf9355bSRic Aleshire 	tl_refrele(peer_tep);
54909bf9355bSRic Aleshire 	return (NULL);
54917c478bd9Sstevel@tonic-gate }
54927c478bd9Sstevel@tonic-gate 
54937c478bd9Sstevel@tonic-gate /*
54947c478bd9Sstevel@tonic-gate  * Generate a free addr and return it in struct pointed by ap
54957c478bd9Sstevel@tonic-gate  * but allocating space for address buffer.
54967c478bd9Sstevel@tonic-gate  * The generated address will be at least 4 bytes long and, if req->ta_alen
54977c478bd9Sstevel@tonic-gate  * exceeds 4 bytes, be req->ta_alen bytes long.
54987c478bd9Sstevel@tonic-gate  *
54997c478bd9Sstevel@tonic-gate  * If address is found it will be inserted in the hash.
55007c478bd9Sstevel@tonic-gate  *
55017c478bd9Sstevel@tonic-gate  * If req->ta_alen is larger than the default alen (4 bytes) the last
55027c478bd9Sstevel@tonic-gate  * alen-4 bytes will always be the same as in req.
55037c478bd9Sstevel@tonic-gate  *
55047c478bd9Sstevel@tonic-gate  * Return 0 for failure.
55057c478bd9Sstevel@tonic-gate  * Return non-zero for success.
55067c478bd9Sstevel@tonic-gate  */
55077c478bd9Sstevel@tonic-gate static boolean_t
tl_get_any_addr(tl_endpt_t * tep,tl_addr_t * req)55087c478bd9Sstevel@tonic-gate tl_get_any_addr(tl_endpt_t *tep, tl_addr_t *req)
55097c478bd9Sstevel@tonic-gate {
55107c478bd9Sstevel@tonic-gate 	t_scalar_t	alen;
55117c478bd9Sstevel@tonic-gate 	uint32_t	loopcnt;	/* Limit loop to 2^32 */
55127c478bd9Sstevel@tonic-gate 
55137c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_hash_hndl != NULL);
5514cb492d90SToomas Soome 	ASSERT(!IS_SOCKET(tep));
55157c478bd9Sstevel@tonic-gate 
55167c478bd9Sstevel@tonic-gate 	if (tep->te_hash_hndl == NULL)
55177c478bd9Sstevel@tonic-gate 		return (B_FALSE);
55187c478bd9Sstevel@tonic-gate 
55197c478bd9Sstevel@tonic-gate 	/*
55207c478bd9Sstevel@tonic-gate 	 * check if default addr is in use
55217c478bd9Sstevel@tonic-gate 	 * if it is - bump it and try again
55227c478bd9Sstevel@tonic-gate 	 */
55237c478bd9Sstevel@tonic-gate 	if (req == NULL) {
55247c478bd9Sstevel@tonic-gate 		alen = sizeof (uint32_t);
55257c478bd9Sstevel@tonic-gate 	} else {
55267c478bd9Sstevel@tonic-gate 		alen = max(req->ta_alen, sizeof (uint32_t));
55277c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_zoneid == req->ta_zoneid);
55287c478bd9Sstevel@tonic-gate 	}
55297c478bd9Sstevel@tonic-gate 
55307c478bd9Sstevel@tonic-gate 	if (tep->te_alen < alen) {
55317c478bd9Sstevel@tonic-gate 		void *abuf = kmem_zalloc((size_t)alen, KM_NOSLEEP);
55327c478bd9Sstevel@tonic-gate 
55337c478bd9Sstevel@tonic-gate 		/*
55347c478bd9Sstevel@tonic-gate 		 * Not enough space in tep->ta_ap to hold the address,
55357c478bd9Sstevel@tonic-gate 		 * allocate a bigger space.
55367c478bd9Sstevel@tonic-gate 		 */
55377c478bd9Sstevel@tonic-gate 		if (abuf == NULL)
55387c478bd9Sstevel@tonic-gate 			return (B_FALSE);
55397c478bd9Sstevel@tonic-gate 
55407c478bd9Sstevel@tonic-gate 		if (tep->te_alen > 0)
55417c478bd9Sstevel@tonic-gate 			kmem_free(tep->te_abuf, tep->te_alen);
55427c478bd9Sstevel@tonic-gate 
55437c478bd9Sstevel@tonic-gate 		tep->te_alen = alen;
55447c478bd9Sstevel@tonic-gate 		tep->te_abuf = abuf;
55457c478bd9Sstevel@tonic-gate 	}
55467c478bd9Sstevel@tonic-gate 
55477c478bd9Sstevel@tonic-gate 	/* Copy in the address in req */
55487c478bd9Sstevel@tonic-gate 	if (req != NULL) {
55497c478bd9Sstevel@tonic-gate 		ASSERT(alen >= req->ta_alen);
55507c478bd9Sstevel@tonic-gate 		bcopy(req->ta_abuf, tep->te_abuf, (size_t)req->ta_alen);
55517c478bd9Sstevel@tonic-gate 	}
55527c478bd9Sstevel@tonic-gate 
55537c478bd9Sstevel@tonic-gate 	/*
55547c478bd9Sstevel@tonic-gate 	 * First try minor number then try default addresses.
55557c478bd9Sstevel@tonic-gate 	 */
55567c478bd9Sstevel@tonic-gate 	bcopy(&tep->te_minor, tep->te_abuf, sizeof (uint32_t));
55577c478bd9Sstevel@tonic-gate 
55587c478bd9Sstevel@tonic-gate 	for (loopcnt = 0; loopcnt < UINT32_MAX; loopcnt++) {
55597c478bd9Sstevel@tonic-gate 		if (mod_hash_insert_reserve(tep->te_addrhash,
5560fc80c0dfSnordmark 		    (mod_hash_key_t)&tep->te_ap, (mod_hash_val_t)tep,
5561fc80c0dfSnordmark 		    tep->te_hash_hndl) == 0) {
55627c478bd9Sstevel@tonic-gate 			/*
55637c478bd9Sstevel@tonic-gate 			 * found free address
55647c478bd9Sstevel@tonic-gate 			 */
55657c478bd9Sstevel@tonic-gate 			tep->te_flag |= TL_ADDRHASHED;
55667c478bd9Sstevel@tonic-gate 			tep->te_hash_hndl = NULL;
55677c478bd9Sstevel@tonic-gate 
55687c478bd9Sstevel@tonic-gate 			return (B_TRUE); /* successful return */
55697c478bd9Sstevel@tonic-gate 		}
55707c478bd9Sstevel@tonic-gate 		/*
55717c478bd9Sstevel@tonic-gate 		 * Use default address.
55727c478bd9Sstevel@tonic-gate 		 */
55737c478bd9Sstevel@tonic-gate 		bcopy(&tep->te_defaddr, tep->te_abuf, sizeof (uint32_t));
55741a5e258fSJosef 'Jeff' Sipek 		atomic_inc_32(&tep->te_defaddr);
55757c478bd9Sstevel@tonic-gate 	}
55767c478bd9Sstevel@tonic-gate 
55777c478bd9Sstevel@tonic-gate 	/*
55787c478bd9Sstevel@tonic-gate 	 * Failed to find anything.
55797c478bd9Sstevel@tonic-gate 	 */
55807c478bd9Sstevel@tonic-gate 	(void) (STRLOG(TL_ID, -1, 1, SL_ERROR,
5581fc80c0dfSnordmark 	    "tl_get_any_addr:looped 2^32 times"));
55827c478bd9Sstevel@tonic-gate 	return (B_FALSE);
55837c478bd9Sstevel@tonic-gate }
55847c478bd9Sstevel@tonic-gate 
55857c478bd9Sstevel@tonic-gate /*
55867c478bd9Sstevel@tonic-gate  * reallocb + set r/w ptrs to reflect size.
55877c478bd9Sstevel@tonic-gate  */
55887c478bd9Sstevel@tonic-gate static mblk_t *
tl_resizemp(mblk_t * mp,ssize_t new_size)55897c478bd9Sstevel@tonic-gate tl_resizemp(mblk_t *mp, ssize_t new_size)
55907c478bd9Sstevel@tonic-gate {
55917c478bd9Sstevel@tonic-gate 	if ((mp = reallocb(mp, new_size, 0)) == NULL)
55927c478bd9Sstevel@tonic-gate 		return (NULL);
55937c478bd9Sstevel@tonic-gate 
55947c478bd9Sstevel@tonic-gate 	mp->b_rptr = DB_BASE(mp);
55957c478bd9Sstevel@tonic-gate 	mp->b_wptr = mp->b_rptr + new_size;
55967c478bd9Sstevel@tonic-gate 	return (mp);
55977c478bd9Sstevel@tonic-gate }
55987c478bd9Sstevel@tonic-gate 
55997c478bd9Sstevel@tonic-gate static void
tl_cl_backenable(tl_endpt_t * tep)56007c478bd9Sstevel@tonic-gate tl_cl_backenable(tl_endpt_t *tep)
56017c478bd9Sstevel@tonic-gate {
56027c478bd9Sstevel@tonic-gate 	list_t *l = &tep->te_flowlist;
56037c478bd9Sstevel@tonic-gate 	tl_endpt_t *elp;
56047c478bd9Sstevel@tonic-gate 
56057c478bd9Sstevel@tonic-gate 	ASSERT(IS_CLTS(tep));
56067c478bd9Sstevel@tonic-gate 
56077c478bd9Sstevel@tonic-gate 	for (elp = list_head(l); elp != NULL; elp = list_head(l)) {
56087c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_ser == elp->te_ser);
56097c478bd9Sstevel@tonic-gate 		ASSERT(elp->te_flowq == tep);
5610cb492d90SToomas Soome 		if (!elp->te_closing)
56117c478bd9Sstevel@tonic-gate 			TL_QENABLE(elp);
56127c478bd9Sstevel@tonic-gate 		elp->te_flowq = NULL;
56137c478bd9Sstevel@tonic-gate 		list_remove(l, elp);
56147c478bd9Sstevel@tonic-gate 	}
56157c478bd9Sstevel@tonic-gate }
56167c478bd9Sstevel@tonic-gate 
56177c478bd9Sstevel@tonic-gate /*
56187c478bd9Sstevel@tonic-gate  * Unconnect endpoints.
56197c478bd9Sstevel@tonic-gate  */
56207c478bd9Sstevel@tonic-gate static void
tl_co_unconnect(tl_endpt_t * tep)56217c478bd9Sstevel@tonic-gate tl_co_unconnect(tl_endpt_t *tep)
56227c478bd9Sstevel@tonic-gate {
56237c478bd9Sstevel@tonic-gate 	tl_endpt_t	*peer_tep = tep->te_conp;
56247c478bd9Sstevel@tonic-gate 	tl_endpt_t	*srv_tep = tep->te_oconp;
56257c478bd9Sstevel@tonic-gate 	list_t		*l;
56269173eaebSToomas Soome 	tl_icon_t	*tip;
56277c478bd9Sstevel@tonic-gate 	tl_endpt_t	*cl_tep;
56287c478bd9Sstevel@tonic-gate 	mblk_t		*d_mp;
56297c478bd9Sstevel@tonic-gate 
56307c478bd9Sstevel@tonic-gate 	ASSERT(IS_COTS(tep));
56317c478bd9Sstevel@tonic-gate 	/*
56327c478bd9Sstevel@tonic-gate 	 * If our peer is closing, don't use it.
56337c478bd9Sstevel@tonic-gate 	 */
56347c478bd9Sstevel@tonic-gate 	if ((peer_tep != NULL) && peer_tep->te_closing) {
56357c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_conp);
56367c478bd9Sstevel@tonic-gate 		peer_tep = NULL;
56377c478bd9Sstevel@tonic-gate 	}
56387c478bd9Sstevel@tonic-gate 	if ((srv_tep != NULL) && srv_tep->te_closing) {
56397c478bd9Sstevel@tonic-gate 		TL_UNCONNECT(tep->te_oconp);
56407c478bd9Sstevel@tonic-gate 		srv_tep = NULL;
56417c478bd9Sstevel@tonic-gate 	}
56427c478bd9Sstevel@tonic-gate 
56437c478bd9Sstevel@tonic-gate 	if (tep->te_nicon > 0) {
56447c478bd9Sstevel@tonic-gate 		l = &tep->te_iconp;
56457c478bd9Sstevel@tonic-gate 		/*
56467c478bd9Sstevel@tonic-gate 		 * If incoming requests pending, change state
56477c478bd9Sstevel@tonic-gate 		 * of clients on disconnect ind event and send
56487c478bd9Sstevel@tonic-gate 		 * discon_ind pdu to modules above them
56497c478bd9Sstevel@tonic-gate 		 * for server: all clients get disconnect
56507c478bd9Sstevel@tonic-gate 		 */
56517c478bd9Sstevel@tonic-gate 
56527c478bd9Sstevel@tonic-gate 		while (tep->te_nicon > 0) {
56537c478bd9Sstevel@tonic-gate 			tip    = list_head(l);
56547c478bd9Sstevel@tonic-gate 			cl_tep = tip->ti_tep;
56557c478bd9Sstevel@tonic-gate 
56567c478bd9Sstevel@tonic-gate 			if (cl_tep == NULL) {
56577c478bd9Sstevel@tonic-gate 				tl_freetip(tep, tip);
56587c478bd9Sstevel@tonic-gate 				continue;
56597c478bd9Sstevel@tonic-gate 			}
56607c478bd9Sstevel@tonic-gate 
56617c478bd9Sstevel@tonic-gate 			if (cl_tep->te_oconp != NULL) {
56627c478bd9Sstevel@tonic-gate 				ASSERT(cl_tep != cl_tep->te_oconp);
56637c478bd9Sstevel@tonic-gate 				TL_UNCONNECT(cl_tep->te_oconp);
56647c478bd9Sstevel@tonic-gate 			}
56657c478bd9Sstevel@tonic-gate 
56667c478bd9Sstevel@tonic-gate 			if (cl_tep->te_closing) {
56677c478bd9Sstevel@tonic-gate 				tl_freetip(tep, tip);
56687c478bd9Sstevel@tonic-gate 				continue;
56697c478bd9Sstevel@tonic-gate 			}
56707c478bd9Sstevel@tonic-gate 
56717c478bd9Sstevel@tonic-gate 			enableok(cl_tep->te_wq);
56727c478bd9Sstevel@tonic-gate 			TL_QENABLE(cl_tep);
56737c478bd9Sstevel@tonic-gate 			d_mp = tl_discon_ind_alloc(ECONNREFUSED, BADSEQNUM);
56747c478bd9Sstevel@tonic-gate 			if (d_mp != NULL) {
56757c478bd9Sstevel@tonic-gate 				cl_tep->te_state = TS_IDLE;
56767c478bd9Sstevel@tonic-gate 				putnext(cl_tep->te_rq, d_mp);
56777c478bd9Sstevel@tonic-gate 			} else {
56787c478bd9Sstevel@tonic-gate 				(void) (STRLOG(TL_ID, tep->te_minor, 3,
5679cb492d90SToomas Soome 				    SL_TRACE | SL_ERROR,
5680fc80c0dfSnordmark 				    "tl_co_unconnect:icmng: "
5681fc80c0dfSnordmark 				    "allocb failure"));
56827c478bd9Sstevel@tonic-gate 			}
56837c478bd9Sstevel@tonic-gate 			tl_freetip(tep, tip);
56847c478bd9Sstevel@tonic-gate 		}
56857c478bd9Sstevel@tonic-gate 	} else if (srv_tep != NULL) {
56867c478bd9Sstevel@tonic-gate 		/*
56877c478bd9Sstevel@tonic-gate 		 * If outgoing request pending, change state
56887c478bd9Sstevel@tonic-gate 		 * of server on discon ind event
56897c478bd9Sstevel@tonic-gate 		 */
56907c478bd9Sstevel@tonic-gate 
56917c478bd9Sstevel@tonic-gate 		if (IS_SOCKET(tep) && !tl_disable_early_connect &&
56927c478bd9Sstevel@tonic-gate 		    IS_COTSORD(srv_tep) &&
56937c478bd9Sstevel@tonic-gate 		    !tl_icon_hasprim(srv_tep, tep->te_seqno, T_ORDREL_IND)) {
56947c478bd9Sstevel@tonic-gate 			/*
56957c478bd9Sstevel@tonic-gate 			 * Queue ordrel_ind for server to be picked up
56967c478bd9Sstevel@tonic-gate 			 * when the connection is accepted.
56977c478bd9Sstevel@tonic-gate 			 */
56987c478bd9Sstevel@tonic-gate 			d_mp = tl_ordrel_ind_alloc();
56997c478bd9Sstevel@tonic-gate 		} else {
57007c478bd9Sstevel@tonic-gate 			/*
57017c478bd9Sstevel@tonic-gate 			 * send discon_ind to server
57027c478bd9Sstevel@tonic-gate 			 */
57037c478bd9Sstevel@tonic-gate 			d_mp = tl_discon_ind_alloc(ECONNRESET, tep->te_seqno);
57047c478bd9Sstevel@tonic-gate 		}
57057c478bd9Sstevel@tonic-gate 		if (d_mp == NULL) {
57067c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
5707cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
5708fc80c0dfSnordmark 			    "tl_co_unconnect:outgoing:allocb failure"));
57097c478bd9Sstevel@tonic-gate 			TL_UNCONNECT(tep->te_oconp);
57107c478bd9Sstevel@tonic-gate 			goto discon_peer;
57117c478bd9Sstevel@tonic-gate 		}
57127c478bd9Sstevel@tonic-gate 
57137c478bd9Sstevel@tonic-gate 		/*
57147c478bd9Sstevel@tonic-gate 		 * If this is a socket the T_DISCON_IND is queued with
57157c478bd9Sstevel@tonic-gate 		 * the T_CONN_IND. Otherwise the T_CONN_IND is removed
57167c478bd9Sstevel@tonic-gate 		 * from the list of pending connections.
57177c478bd9Sstevel@tonic-gate 		 * Note that when te_oconp is set the peer better have
57187c478bd9Sstevel@tonic-gate 		 * a t_connind_t for the client.
57197c478bd9Sstevel@tonic-gate 		 */
57207c478bd9Sstevel@tonic-gate 		if (IS_SOCKET(tep) && !tl_disable_early_connect) {
57217c478bd9Sstevel@tonic-gate 			/*
57227c478bd9Sstevel@tonic-gate 			 * Queue the disconnection message.
57237c478bd9Sstevel@tonic-gate 			 */
57247c478bd9Sstevel@tonic-gate 			tl_icon_queuemsg(srv_tep, tep->te_seqno, d_mp);
57257c478bd9Sstevel@tonic-gate 		} else {
57267c478bd9Sstevel@tonic-gate 			tip = tl_icon_find(srv_tep, tep->te_seqno);
57277c478bd9Sstevel@tonic-gate 			if (tip == NULL) {
57287c478bd9Sstevel@tonic-gate 				freemsg(d_mp);
57297c478bd9Sstevel@tonic-gate 			} else {
57307c478bd9Sstevel@tonic-gate 				ASSERT(tep == tip->ti_tep);
57317c478bd9Sstevel@tonic-gate 				ASSERT(tep->te_ser == srv_tep->te_ser);
57327c478bd9Sstevel@tonic-gate 				/*
57337c478bd9Sstevel@tonic-gate 				 * Delete tip from the server list.
57347c478bd9Sstevel@tonic-gate 				 */
57357c478bd9Sstevel@tonic-gate 				if (srv_tep->te_nicon == 1) {
57367c478bd9Sstevel@tonic-gate 					srv_tep->te_state =
5737b8ffbd31SJohn Levon 					    nextstate[TE_DISCON_IND2]
5738b8ffbd31SJohn Levon 					    [srv_tep->te_state];
57397c478bd9Sstevel@tonic-gate 				} else {
57407c478bd9Sstevel@tonic-gate 					srv_tep->te_state =
5741b8ffbd31SJohn Levon 					    nextstate[TE_DISCON_IND3]
5742b8ffbd31SJohn Levon 					    [srv_tep->te_state];
57437c478bd9Sstevel@tonic-gate 				}
57447c478bd9Sstevel@tonic-gate 				ASSERT(*(uint32_t *)(d_mp->b_rptr) ==
57457c478bd9Sstevel@tonic-gate 				    T_DISCON_IND);
57467c478bd9Sstevel@tonic-gate 				putnext(srv_tep->te_rq, d_mp);
57477c478bd9Sstevel@tonic-gate 				tl_freetip(srv_tep, tip);
57487c478bd9Sstevel@tonic-gate 			}
57497c478bd9Sstevel@tonic-gate 			TL_UNCONNECT(tep->te_oconp);
57507c478bd9Sstevel@tonic-gate 			srv_tep = NULL;
57517c478bd9Sstevel@tonic-gate 		}
57527c478bd9Sstevel@tonic-gate 	} else if (peer_tep != NULL) {
57537c478bd9Sstevel@tonic-gate 		/*
57547c478bd9Sstevel@tonic-gate 		 * unconnect existing connection
57557c478bd9Sstevel@tonic-gate 		 * If connected, change state of peer on
57567c478bd9Sstevel@tonic-gate 		 * discon ind event and send discon ind pdu
57577c478bd9Sstevel@tonic-gate 		 * to module above it
57587c478bd9Sstevel@tonic-gate 		 */
57597c478bd9Sstevel@tonic-gate 
57607c478bd9Sstevel@tonic-gate 		ASSERT(tep->te_ser == peer_tep->te_ser);
57617c478bd9Sstevel@tonic-gate 		if (IS_COTSORD(peer_tep) &&
57627c478bd9Sstevel@tonic-gate 		    (peer_tep->te_state == TS_WIND_ORDREL ||
57637c478bd9Sstevel@tonic-gate 		    peer_tep->te_state == TS_DATA_XFER)) {
57647c478bd9Sstevel@tonic-gate 			/*
57657c478bd9Sstevel@tonic-gate 			 * send ordrel ind
57667c478bd9Sstevel@tonic-gate 			 */
57677c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE,
57687c478bd9Sstevel@tonic-gate 			"tl_co_unconnect:connected: ordrel_ind state %d->%d",
5769fc80c0dfSnordmark 			    peer_tep->te_state,
5770b8ffbd31SJohn Levon 			    nextstate[TE_ORDREL_IND][peer_tep->te_state]));
57717c478bd9Sstevel@tonic-gate 			d_mp = tl_ordrel_ind_alloc();
5772cb492d90SToomas Soome 			if (d_mp == NULL) {
57737c478bd9Sstevel@tonic-gate 				(void) (STRLOG(TL_ID, tep->te_minor, 3,
5774cb492d90SToomas Soome 				    SL_TRACE | SL_ERROR,
57757c478bd9Sstevel@tonic-gate 				    "tl_co_unconnect:connected:"
57767c478bd9Sstevel@tonic-gate 				    "allocb failure"));
57777c478bd9Sstevel@tonic-gate 				/*
57787c478bd9Sstevel@tonic-gate 				 * Continue with cleaning up peer as
57797c478bd9Sstevel@tonic-gate 				 * this side may go away with the close
57807c478bd9Sstevel@tonic-gate 				 */
57817c478bd9Sstevel@tonic-gate 				TL_QENABLE(peer_tep);
57827c478bd9Sstevel@tonic-gate 				goto discon_peer;
57837c478bd9Sstevel@tonic-gate 			}
57847c478bd9Sstevel@tonic-gate 			peer_tep->te_state =
5785b8ffbd31SJohn Levon 			    nextstate[TE_ORDREL_IND][peer_tep->te_state];
57867c478bd9Sstevel@tonic-gate 
57877c478bd9Sstevel@tonic-gate 			putnext(peer_tep->te_rq, d_mp);
57887c478bd9Sstevel@tonic-gate 			/*
57897c478bd9Sstevel@tonic-gate 			 * Handle flow control case.  This will generate
57907c478bd9Sstevel@tonic-gate 			 * a t_discon_ind message with reason 0 if there
57917c478bd9Sstevel@tonic-gate 			 * is data queued on the write side.
57927c478bd9Sstevel@tonic-gate 			 */
57937c478bd9Sstevel@tonic-gate 			TL_QENABLE(peer_tep);
57947c478bd9Sstevel@tonic-gate 		} else if (IS_COTSORD(peer_tep) &&
57957c478bd9Sstevel@tonic-gate 		    peer_tep->te_state == TS_WREQ_ORDREL) {
57967c478bd9Sstevel@tonic-gate 			/*
57977c478bd9Sstevel@tonic-gate 			 * Sent an ordrel_ind. We send a discon with
57987c478bd9Sstevel@tonic-gate 			 * with error 0 to inform that the peer is gone.
57997c478bd9Sstevel@tonic-gate 			 */
58007c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
5801cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
5802fc80c0dfSnordmark 			    "tl_co_unconnect: discon in state %d",
5803fc80c0dfSnordmark 			    tep->te_state));
58047c478bd9Sstevel@tonic-gate 			tl_discon_ind(peer_tep, 0);
58057c478bd9Sstevel@tonic-gate 		} else {
58067c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 3,
5807cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
5808fc80c0dfSnordmark 			    "tl_co_unconnect: state %d", tep->te_state));
58097c478bd9Sstevel@tonic-gate 			tl_discon_ind(peer_tep, ECONNRESET);
58107c478bd9Sstevel@tonic-gate 		}
58117c478bd9Sstevel@tonic-gate 
58127c478bd9Sstevel@tonic-gate discon_peer:
58137c478bd9Sstevel@tonic-gate 		/*
58147c478bd9Sstevel@tonic-gate 		 * Disconnect cross-pointers only for close
58157c478bd9Sstevel@tonic-gate 		 */
58167c478bd9Sstevel@tonic-gate 		if (tep->te_closing) {
58177c478bd9Sstevel@tonic-gate 			peer_tep = tep->te_conp;
58187c478bd9Sstevel@tonic-gate 			TL_REMOVE_PEER(peer_tep->te_conp);
58197c478bd9Sstevel@tonic-gate 			TL_REMOVE_PEER(tep->te_conp);
58207c478bd9Sstevel@tonic-gate 		}
58217c478bd9Sstevel@tonic-gate 	}
58227c478bd9Sstevel@tonic-gate }
58237c478bd9Sstevel@tonic-gate 
58247c478bd9Sstevel@tonic-gate /*
58257c478bd9Sstevel@tonic-gate  * Note: The following routine does not recover from allocb()
58267c478bd9Sstevel@tonic-gate  * failures
58277c478bd9Sstevel@tonic-gate  * The reason should be from the <sys/errno.h> space.
58287c478bd9Sstevel@tonic-gate  */
58297c478bd9Sstevel@tonic-gate static void
tl_discon_ind(tl_endpt_t * tep,uint32_t reason)58307c478bd9Sstevel@tonic-gate tl_discon_ind(tl_endpt_t *tep, uint32_t reason)
58317c478bd9Sstevel@tonic-gate {
58327c478bd9Sstevel@tonic-gate 	mblk_t *d_mp;
58337c478bd9Sstevel@tonic-gate 
58347c478bd9Sstevel@tonic-gate 	if (tep->te_closing)
58357c478bd9Sstevel@tonic-gate 		return;
58367c478bd9Sstevel@tonic-gate 
58377c478bd9Sstevel@tonic-gate 	/*
58387c478bd9Sstevel@tonic-gate 	 * flush the queues.
58397c478bd9Sstevel@tonic-gate 	 */
58407c478bd9Sstevel@tonic-gate 	flushq(tep->te_rq, FLUSHDATA);
58417c478bd9Sstevel@tonic-gate 	(void) putnextctl1(tep->te_rq, M_FLUSH, FLUSHRW);
58427c478bd9Sstevel@tonic-gate 
58437c478bd9Sstevel@tonic-gate 	/*
58447c478bd9Sstevel@tonic-gate 	 * send discon ind
58457c478bd9Sstevel@tonic-gate 	 */
58467c478bd9Sstevel@tonic-gate 	d_mp = tl_discon_ind_alloc(reason, tep->te_seqno);
5847cb492d90SToomas Soome 	if (d_mp == NULL) {
5848cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 3, SL_TRACE | SL_ERROR,
5849fc80c0dfSnordmark 		    "tl_discon_ind:allocb failure"));
58507c478bd9Sstevel@tonic-gate 		return;
58517c478bd9Sstevel@tonic-gate 	}
58527c478bd9Sstevel@tonic-gate 	tep->te_state = TS_IDLE;
58537c478bd9Sstevel@tonic-gate 	putnext(tep->te_rq, d_mp);
58547c478bd9Sstevel@tonic-gate }
58557c478bd9Sstevel@tonic-gate 
58567c478bd9Sstevel@tonic-gate /*
58577c478bd9Sstevel@tonic-gate  * Note: The following routine does not recover from allocb()
58587c478bd9Sstevel@tonic-gate  * failures
58597c478bd9Sstevel@tonic-gate  * The reason should be from the <sys/errno.h> space.
58607c478bd9Sstevel@tonic-gate  */
58617c478bd9Sstevel@tonic-gate static mblk_t *
tl_discon_ind_alloc(uint32_t reason,t_scalar_t seqnum)58627c478bd9Sstevel@tonic-gate tl_discon_ind_alloc(uint32_t reason, t_scalar_t seqnum)
58637c478bd9Sstevel@tonic-gate {
58647c478bd9Sstevel@tonic-gate 	mblk_t *mp;
58657c478bd9Sstevel@tonic-gate 	struct T_discon_ind *tdi;
58667c478bd9Sstevel@tonic-gate 
58677c478bd9Sstevel@tonic-gate 	if (mp = allocb(sizeof (struct T_discon_ind), BPRI_MED)) {
58687c478bd9Sstevel@tonic-gate 		DB_TYPE(mp) = M_PROTO;
58697c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct T_discon_ind);
58707c478bd9Sstevel@tonic-gate 		tdi = (struct T_discon_ind *)mp->b_rptr;
58717c478bd9Sstevel@tonic-gate 		tdi->PRIM_type = T_DISCON_IND;
58727c478bd9Sstevel@tonic-gate 		tdi->DISCON_reason = reason;
58737c478bd9Sstevel@tonic-gate 		tdi->SEQ_number = seqnum;
58747c478bd9Sstevel@tonic-gate 	}
58757c478bd9Sstevel@tonic-gate 	return (mp);
58767c478bd9Sstevel@tonic-gate }
58777c478bd9Sstevel@tonic-gate 
58787c478bd9Sstevel@tonic-gate 
58797c478bd9Sstevel@tonic-gate /*
58807c478bd9Sstevel@tonic-gate  * Note: The following routine does not recover from allocb()
58817c478bd9Sstevel@tonic-gate  * failures
58827c478bd9Sstevel@tonic-gate  */
58837c478bd9Sstevel@tonic-gate static mblk_t *
tl_ordrel_ind_alloc(void)58847c478bd9Sstevel@tonic-gate tl_ordrel_ind_alloc(void)
58857c478bd9Sstevel@tonic-gate {
58867c478bd9Sstevel@tonic-gate 	mblk_t *mp;
58877c478bd9Sstevel@tonic-gate 	struct T_ordrel_ind *toi;
58887c478bd9Sstevel@tonic-gate 
58897c478bd9Sstevel@tonic-gate 	if (mp = allocb(sizeof (struct T_ordrel_ind), BPRI_MED)) {
58907c478bd9Sstevel@tonic-gate 		DB_TYPE(mp) = M_PROTO;
58917c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct T_ordrel_ind);
58927c478bd9Sstevel@tonic-gate 		toi = (struct T_ordrel_ind *)mp->b_rptr;
58937c478bd9Sstevel@tonic-gate 		toi->PRIM_type = T_ORDREL_IND;
58947c478bd9Sstevel@tonic-gate 	}
58957c478bd9Sstevel@tonic-gate 	return (mp);
58967c478bd9Sstevel@tonic-gate }
58977c478bd9Sstevel@tonic-gate 
58987c478bd9Sstevel@tonic-gate 
58997c478bd9Sstevel@tonic-gate /*
59007c478bd9Sstevel@tonic-gate  * Lookup the seqno in the list of queued connections.
59017c478bd9Sstevel@tonic-gate  */
59027c478bd9Sstevel@tonic-gate static tl_icon_t *
tl_icon_find(tl_endpt_t * tep,t_scalar_t seqno)59037c478bd9Sstevel@tonic-gate tl_icon_find(tl_endpt_t *tep, t_scalar_t seqno)
59047c478bd9Sstevel@tonic-gate {
59057c478bd9Sstevel@tonic-gate 	list_t *l = &tep->te_iconp;
59067c478bd9Sstevel@tonic-gate 	tl_icon_t *tip = list_head(l);
59077c478bd9Sstevel@tonic-gate 
59087c478bd9Sstevel@tonic-gate 	ASSERT(seqno != 0);
59097c478bd9Sstevel@tonic-gate 
59107c478bd9Sstevel@tonic-gate 	for (; tip != NULL && (tip->ti_seqno != seqno); tip = list_next(l, tip))
59117c478bd9Sstevel@tonic-gate 		;
59127c478bd9Sstevel@tonic-gate 
59137c478bd9Sstevel@tonic-gate 	return (tip);
59147c478bd9Sstevel@tonic-gate }
59157c478bd9Sstevel@tonic-gate 
59167c478bd9Sstevel@tonic-gate /*
59177c478bd9Sstevel@tonic-gate  * Queue data for a given T_CONN_IND while verifying that redundant
59187c478bd9Sstevel@tonic-gate  * messages, such as a T_ORDREL_IND after a T_DISCON_IND, are not queued.
59197c478bd9Sstevel@tonic-gate  * Used when the originator of the connection closes.
59207c478bd9Sstevel@tonic-gate  */
59217c478bd9Sstevel@tonic-gate static void
tl_icon_queuemsg(tl_endpt_t * tep,t_scalar_t seqno,mblk_t * nmp)59227c478bd9Sstevel@tonic-gate tl_icon_queuemsg(tl_endpt_t *tep, t_scalar_t seqno, mblk_t *nmp)
59237c478bd9Sstevel@tonic-gate {
59247c478bd9Sstevel@tonic-gate 	tl_icon_t		*tip;
59257c478bd9Sstevel@tonic-gate 	mblk_t			**mpp, *mp;
59267c478bd9Sstevel@tonic-gate 	int			prim, nprim;
59277c478bd9Sstevel@tonic-gate 
59287c478bd9Sstevel@tonic-gate 	if (nmp->b_datap->db_type == M_PROTO)
59297c478bd9Sstevel@tonic-gate 		nprim = ((union T_primitives *)nmp->b_rptr)->type;
59307c478bd9Sstevel@tonic-gate 	else
59317c478bd9Sstevel@tonic-gate 		nprim = -1;	/* M_DATA */
59327c478bd9Sstevel@tonic-gate 
59337c478bd9Sstevel@tonic-gate 	tip = tl_icon_find(tep, seqno);
59347c478bd9Sstevel@tonic-gate 	if (tip == NULL) {
59357c478bd9Sstevel@tonic-gate 		freemsg(nmp);
59367c478bd9Sstevel@tonic-gate 		return;
59377c478bd9Sstevel@tonic-gate 	}
59387c478bd9Sstevel@tonic-gate 
59397c478bd9Sstevel@tonic-gate 	ASSERT(tip->ti_seqno != 0);
59407c478bd9Sstevel@tonic-gate 	mpp = &tip->ti_mp;
59417c478bd9Sstevel@tonic-gate 	while (*mpp != NULL) {
59427c478bd9Sstevel@tonic-gate 		mp = *mpp;
59437c478bd9Sstevel@tonic-gate 
59447c478bd9Sstevel@tonic-gate 		if (mp->b_datap->db_type == M_PROTO)
59457c478bd9Sstevel@tonic-gate 			prim = ((union T_primitives *)mp->b_rptr)->type;
59467c478bd9Sstevel@tonic-gate 		else
59477c478bd9Sstevel@tonic-gate 			prim = -1;	/* M_DATA */
59487c478bd9Sstevel@tonic-gate 
59497c478bd9Sstevel@tonic-gate 		/*
59507c478bd9Sstevel@tonic-gate 		 * Allow nothing after a T_DISCON_IND
59517c478bd9Sstevel@tonic-gate 		 */
59527c478bd9Sstevel@tonic-gate 		if (prim == T_DISCON_IND) {
59537c478bd9Sstevel@tonic-gate 			freemsg(nmp);
59547c478bd9Sstevel@tonic-gate 			return;
59557c478bd9Sstevel@tonic-gate 		}
59567c478bd9Sstevel@tonic-gate 		/*
59577c478bd9Sstevel@tonic-gate 		 * Only allow a T_DISCON_IND after an T_ORDREL_IND
59587c478bd9Sstevel@tonic-gate 		 */
59597c478bd9Sstevel@tonic-gate 		if (prim == T_ORDREL_IND && nprim != T_DISCON_IND) {
59607c478bd9Sstevel@tonic-gate 			freemsg(nmp);
59617c478bd9Sstevel@tonic-gate 			return;
59627c478bd9Sstevel@tonic-gate 		}
59637c478bd9Sstevel@tonic-gate 		mpp = &(mp->b_next);
59647c478bd9Sstevel@tonic-gate 	}
59657c478bd9Sstevel@tonic-gate 	*mpp = nmp;
59667c478bd9Sstevel@tonic-gate }
59677c478bd9Sstevel@tonic-gate 
59687c478bd9Sstevel@tonic-gate /*
59697c478bd9Sstevel@tonic-gate  * Verify if a certain TPI primitive exists on the connind queue.
59707c478bd9Sstevel@tonic-gate  * Use prim -1 for M_DATA.
59717c478bd9Sstevel@tonic-gate  * Return non-zero if found.
59727c478bd9Sstevel@tonic-gate  */
59737c478bd9Sstevel@tonic-gate static boolean_t
tl_icon_hasprim(tl_endpt_t * tep,t_scalar_t seqno,t_scalar_t prim)59747c478bd9Sstevel@tonic-gate tl_icon_hasprim(tl_endpt_t *tep, t_scalar_t seqno, t_scalar_t prim)
59757c478bd9Sstevel@tonic-gate {
59767c478bd9Sstevel@tonic-gate 	tl_icon_t *tip = tl_icon_find(tep, seqno);
59777c478bd9Sstevel@tonic-gate 	boolean_t found = B_FALSE;
59787c478bd9Sstevel@tonic-gate 
59797c478bd9Sstevel@tonic-gate 	if (tip != NULL) {
59807c478bd9Sstevel@tonic-gate 		mblk_t *mp;
59817c478bd9Sstevel@tonic-gate 		for (mp = tip->ti_mp; !found && mp != NULL; mp = mp->b_next) {
59827c478bd9Sstevel@tonic-gate 			found = (DB_TYPE(mp) == M_PROTO &&
59837c478bd9Sstevel@tonic-gate 			    ((union T_primitives *)mp->b_rptr)->type == prim);
59847c478bd9Sstevel@tonic-gate 		}
59857c478bd9Sstevel@tonic-gate 	}
59867c478bd9Sstevel@tonic-gate 	return (found);
59877c478bd9Sstevel@tonic-gate }
59887c478bd9Sstevel@tonic-gate 
59897c478bd9Sstevel@tonic-gate /*
59907c478bd9Sstevel@tonic-gate  * Send the b_next mblk chain that has accumulated before the connection
59917c478bd9Sstevel@tonic-gate  * was accepted. Perform the necessary state transitions.
59927c478bd9Sstevel@tonic-gate  */
59937c478bd9Sstevel@tonic-gate static void
tl_icon_sendmsgs(tl_endpt_t * tep,mblk_t ** mpp)59947c478bd9Sstevel@tonic-gate tl_icon_sendmsgs(tl_endpt_t *tep, mblk_t **mpp)
59957c478bd9Sstevel@tonic-gate {
59967c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
59977c478bd9Sstevel@tonic-gate 	union T_primitives	*primp;
59987c478bd9Sstevel@tonic-gate 
59997c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
60007c478bd9Sstevel@tonic-gate 		tl_icon_freemsgs(mpp);
60017c478bd9Sstevel@tonic-gate 		return;
60027c478bd9Sstevel@tonic-gate 	}
60037c478bd9Sstevel@tonic-gate 
60047c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_state == TS_DATA_XFER);
60057c478bd9Sstevel@tonic-gate 	ASSERT(tep->te_rq->q_first == NULL);
60067c478bd9Sstevel@tonic-gate 
60077c478bd9Sstevel@tonic-gate 	while ((mp = *mpp) != NULL) {
60087c478bd9Sstevel@tonic-gate 		*mpp = mp->b_next;
60097c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
60107c478bd9Sstevel@tonic-gate 
60117c478bd9Sstevel@tonic-gate 		ASSERT((DB_TYPE(mp) == M_DATA) || (DB_TYPE(mp) == M_PROTO));
60127c478bd9Sstevel@tonic-gate 		switch (DB_TYPE(mp)) {
60137c478bd9Sstevel@tonic-gate 		default:
60147c478bd9Sstevel@tonic-gate 			freemsg(mp);
60157c478bd9Sstevel@tonic-gate 			break;
60167c478bd9Sstevel@tonic-gate 		case M_DATA:
60177c478bd9Sstevel@tonic-gate 			putnext(tep->te_rq, mp);
60187c478bd9Sstevel@tonic-gate 			break;
60197c478bd9Sstevel@tonic-gate 		case M_PROTO:
60207c478bd9Sstevel@tonic-gate 			primp = (union T_primitives *)mp->b_rptr;
60217c478bd9Sstevel@tonic-gate 			switch (primp->type) {
60227c478bd9Sstevel@tonic-gate 			case T_UNITDATA_IND:
60237c478bd9Sstevel@tonic-gate 			case T_DATA_IND:
60247c478bd9Sstevel@tonic-gate 			case T_OPTDATA_IND:
60257c478bd9Sstevel@tonic-gate 			case T_EXDATA_IND:
60267c478bd9Sstevel@tonic-gate 				putnext(tep->te_rq, mp);
60277c478bd9Sstevel@tonic-gate 				break;
60287c478bd9Sstevel@tonic-gate 			case T_ORDREL_IND:
6029b8ffbd31SJohn Levon 				tep->te_state = nextstate[TE_ORDREL_IND]
6030b8ffbd31SJohn Levon 				    [tep->te_state];
60317c478bd9Sstevel@tonic-gate 				putnext(tep->te_rq, mp);
60327c478bd9Sstevel@tonic-gate 				break;
60337c478bd9Sstevel@tonic-gate 			case T_DISCON_IND:
60347c478bd9Sstevel@tonic-gate 				tep->te_state = TS_IDLE;
60357c478bd9Sstevel@tonic-gate 				putnext(tep->te_rq, mp);
60367c478bd9Sstevel@tonic-gate 				break;
60377c478bd9Sstevel@tonic-gate 			default:
60387c478bd9Sstevel@tonic-gate #ifdef DEBUG
60397c478bd9Sstevel@tonic-gate 				cmn_err(CE_PANIC,
6040fc80c0dfSnordmark 				    "tl_icon_sendmsgs: unknown primitive");
60417c478bd9Sstevel@tonic-gate #endif /* DEBUG */
60427c478bd9Sstevel@tonic-gate 				freemsg(mp);
60437c478bd9Sstevel@tonic-gate 				break;
60447c478bd9Sstevel@tonic-gate 			}
60457c478bd9Sstevel@tonic-gate 			break;
60467c478bd9Sstevel@tonic-gate 		}
60477c478bd9Sstevel@tonic-gate 	}
60487c478bd9Sstevel@tonic-gate }
60497c478bd9Sstevel@tonic-gate 
60507c478bd9Sstevel@tonic-gate /*
60517c478bd9Sstevel@tonic-gate  * Free the b_next mblk chain that has accumulated before the connection
60527c478bd9Sstevel@tonic-gate  * was accepted.
60537c478bd9Sstevel@tonic-gate  */
60547c478bd9Sstevel@tonic-gate static void
tl_icon_freemsgs(mblk_t ** mpp)60557c478bd9Sstevel@tonic-gate tl_icon_freemsgs(mblk_t **mpp)
60567c478bd9Sstevel@tonic-gate {
60577c478bd9Sstevel@tonic-gate 	mblk_t *mp;
60587c478bd9Sstevel@tonic-gate 
60597c478bd9Sstevel@tonic-gate 	while ((mp = *mpp) != NULL) {
60607c478bd9Sstevel@tonic-gate 		*mpp = mp->b_next;
60617c478bd9Sstevel@tonic-gate 		mp->b_next = NULL;
60627c478bd9Sstevel@tonic-gate 		freemsg(mp);
60637c478bd9Sstevel@tonic-gate 	}
60647c478bd9Sstevel@tonic-gate }
60657c478bd9Sstevel@tonic-gate 
60667c478bd9Sstevel@tonic-gate /*
60677c478bd9Sstevel@tonic-gate  * Send M_ERROR
60687c478bd9Sstevel@tonic-gate  * Note: assumes caller ensured enough space in mp or enough
60697c478bd9Sstevel@tonic-gate  *	memory available. Does not attempt recovery from allocb()
60707c478bd9Sstevel@tonic-gate  *	failures
60717c478bd9Sstevel@tonic-gate  */
60727c478bd9Sstevel@tonic-gate 
60737c478bd9Sstevel@tonic-gate static void
tl_merror(queue_t * wq,mblk_t * mp,int error)60747c478bd9Sstevel@tonic-gate tl_merror(queue_t *wq, mblk_t *mp, int error)
60757c478bd9Sstevel@tonic-gate {
60767c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep = (tl_endpt_t *)wq->q_ptr;
60777c478bd9Sstevel@tonic-gate 
60787c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
60797c478bd9Sstevel@tonic-gate 		freemsg(mp);
60807c478bd9Sstevel@tonic-gate 		return;
60817c478bd9Sstevel@tonic-gate 	}
60827c478bd9Sstevel@tonic-gate 
60837c478bd9Sstevel@tonic-gate 	(void) (STRLOG(TL_ID, tep->te_minor, 1,
6084cb492d90SToomas Soome 	    SL_TRACE | SL_ERROR,
6085903a11ebSrh 	    "tl_merror: tep=%p, err=%d", (void *)tep, error));
60867c478bd9Sstevel@tonic-gate 
60877c478bd9Sstevel@tonic-gate 	/*
60887c478bd9Sstevel@tonic-gate 	 * flush all messages on queue. we are shutting
60897c478bd9Sstevel@tonic-gate 	 * the stream down on fatal error
60907c478bd9Sstevel@tonic-gate 	 */
60917c478bd9Sstevel@tonic-gate 	flushq(wq, FLUSHALL);
60927c478bd9Sstevel@tonic-gate 	if (IS_COTS(tep)) {
60937c478bd9Sstevel@tonic-gate 		/* connection oriented - unconnect endpoints */
60947c478bd9Sstevel@tonic-gate 		tl_co_unconnect(tep);
60957c478bd9Sstevel@tonic-gate 	}
60967c478bd9Sstevel@tonic-gate 	if (mp->b_cont) {
60977c478bd9Sstevel@tonic-gate 		freemsg(mp->b_cont);
60987c478bd9Sstevel@tonic-gate 		mp->b_cont = NULL;
60997c478bd9Sstevel@tonic-gate 	}
61007c478bd9Sstevel@tonic-gate 
61017c478bd9Sstevel@tonic-gate 	if ((MBLKSIZE(mp) < 1) || (DB_REF(mp) > 1)) {
61027c478bd9Sstevel@tonic-gate 		freemsg(mp);
61037c478bd9Sstevel@tonic-gate 		mp = allocb(1, BPRI_HI);
6104cb492d90SToomas Soome 		if (mp == NULL) {
61057c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
6106cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
6107fc80c0dfSnordmark 			    "tl_merror:M_PROTO: out of memory"));
61087c478bd9Sstevel@tonic-gate 			return;
61097c478bd9Sstevel@tonic-gate 		}
61107c478bd9Sstevel@tonic-gate 	}
61117c478bd9Sstevel@tonic-gate 	if (mp) {
61127c478bd9Sstevel@tonic-gate 		DB_TYPE(mp) = M_ERROR;
61137c478bd9Sstevel@tonic-gate 		mp->b_rptr = DB_BASE(mp);
61147c478bd9Sstevel@tonic-gate 		*mp->b_rptr = (char)error;
61157c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (char);
61167c478bd9Sstevel@tonic-gate 		qreply(wq, mp);
61177c478bd9Sstevel@tonic-gate 	} else {
61187c478bd9Sstevel@tonic-gate 		(void) putnextctl1(tep->te_rq, M_ERROR, error);
61197c478bd9Sstevel@tonic-gate 	}
61207c478bd9Sstevel@tonic-gate }
61217c478bd9Sstevel@tonic-gate 
61227c478bd9Sstevel@tonic-gate static void
tl_fill_option(uchar_t * buf,cred_t * cr,pid_t cpid,int flag,cred_t * pcr)612345916cd2Sjpk tl_fill_option(uchar_t *buf, cred_t *cr, pid_t cpid, int flag, cred_t *pcr)
61247c478bd9Sstevel@tonic-gate {
6125de8c4a14SErik Nordmark 	ASSERT(cr != NULL);
6126de8c4a14SErik Nordmark 
61277c478bd9Sstevel@tonic-gate 	if (flag & TL_SETCRED) {
61287c478bd9Sstevel@tonic-gate 		struct opthdr *opt = (struct opthdr *)buf;
61297c478bd9Sstevel@tonic-gate 		tl_credopt_t *tlcred;
61307c478bd9Sstevel@tonic-gate 
61317c478bd9Sstevel@tonic-gate 		opt->level = TL_PROT_LEVEL;
61327c478bd9Sstevel@tonic-gate 		opt->name = TL_OPT_PEER_CRED;
61337c478bd9Sstevel@tonic-gate 		opt->len = (t_uscalar_t)OPTLEN(sizeof (tl_credopt_t));
61347c478bd9Sstevel@tonic-gate 
61357c478bd9Sstevel@tonic-gate 		tlcred = (tl_credopt_t *)(opt + 1);
61367c478bd9Sstevel@tonic-gate 		tlcred->tc_uid = crgetuid(cr);
61377c478bd9Sstevel@tonic-gate 		tlcred->tc_gid = crgetgid(cr);
61387c478bd9Sstevel@tonic-gate 		tlcred->tc_ruid = crgetruid(cr);
61397c478bd9Sstevel@tonic-gate 		tlcred->tc_rgid = crgetrgid(cr);
61407c478bd9Sstevel@tonic-gate 		tlcred->tc_suid = crgetsuid(cr);
61417c478bd9Sstevel@tonic-gate 		tlcred->tc_sgid = crgetsgid(cr);
61427c478bd9Sstevel@tonic-gate 		tlcred->tc_ngroups = crgetngroups(cr);
61437c478bd9Sstevel@tonic-gate 	} else if (flag & TL_SETUCRED) {
61447c478bd9Sstevel@tonic-gate 		struct opthdr *opt = (struct opthdr *)buf;
61457c478bd9Sstevel@tonic-gate 
61467c478bd9Sstevel@tonic-gate 		opt->level = TL_PROT_LEVEL;
61477c478bd9Sstevel@tonic-gate 		opt->name = TL_OPT_PEER_UCRED;
614867dbe2beSCasper H.S. Dik 		opt->len = (t_uscalar_t)OPTLEN(ucredminsize(cr));
61497c478bd9Sstevel@tonic-gate 
615045916cd2Sjpk 		(void) cred2ucred(cr, cpid, (void *)(opt + 1), pcr);
61517c478bd9Sstevel@tonic-gate 	} else {
61527c478bd9Sstevel@tonic-gate 		struct T_opthdr *topt = (struct T_opthdr *)buf;
61537c478bd9Sstevel@tonic-gate 		ASSERT(flag & TL_SOCKUCRED);
61547c478bd9Sstevel@tonic-gate 
61557c478bd9Sstevel@tonic-gate 		topt->level = SOL_SOCKET;
61567c478bd9Sstevel@tonic-gate 		topt->name = SCM_UCRED;
615767dbe2beSCasper H.S. Dik 		topt->len = ucredminsize(cr) + sizeof (*topt);
61587c478bd9Sstevel@tonic-gate 		topt->status = 0;
615945916cd2Sjpk 		(void) cred2ucred(cr, cpid, (void *)(topt + 1), pcr);
61607c478bd9Sstevel@tonic-gate 	}
61617c478bd9Sstevel@tonic-gate }
61627c478bd9Sstevel@tonic-gate 
61637c478bd9Sstevel@tonic-gate /* ARGSUSED */
61647c478bd9Sstevel@tonic-gate static int
tl_default_opt(queue_t * wq,int level,int name,uchar_t * ptr)61657c478bd9Sstevel@tonic-gate tl_default_opt(queue_t *wq, int level, int name, uchar_t *ptr)
61667c478bd9Sstevel@tonic-gate {
61677c478bd9Sstevel@tonic-gate 	/* no default value processed in protocol specific code currently */
61687c478bd9Sstevel@tonic-gate 	return (-1);
61697c478bd9Sstevel@tonic-gate }
61707c478bd9Sstevel@tonic-gate 
61717c478bd9Sstevel@tonic-gate /* ARGSUSED */
61727c478bd9Sstevel@tonic-gate static int
tl_get_opt(queue_t * wq,int level,int name,uchar_t * ptr)61737c478bd9Sstevel@tonic-gate tl_get_opt(queue_t *wq, int level, int name, uchar_t *ptr)
61747c478bd9Sstevel@tonic-gate {
61757c478bd9Sstevel@tonic-gate 	int len;
61767c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep;
61777c478bd9Sstevel@tonic-gate 	int *valp;
61787c478bd9Sstevel@tonic-gate 
61797c478bd9Sstevel@tonic-gate 	tep = (tl_endpt_t *)wq->q_ptr;
61807c478bd9Sstevel@tonic-gate 
61817c478bd9Sstevel@tonic-gate 	len = 0;
61827c478bd9Sstevel@tonic-gate 
61837c478bd9Sstevel@tonic-gate 	/*
61847c478bd9Sstevel@tonic-gate 	 * Assumes: option level and name sanity check done elsewhere
61857c478bd9Sstevel@tonic-gate 	 */
61867c478bd9Sstevel@tonic-gate 
61877c478bd9Sstevel@tonic-gate 	switch (level) {
61887c478bd9Sstevel@tonic-gate 	case SOL_SOCKET:
6189cb492d90SToomas Soome 		if (!IS_SOCKET(tep))
61907c478bd9Sstevel@tonic-gate 			break;
61917c478bd9Sstevel@tonic-gate 		switch (name) {
61927c478bd9Sstevel@tonic-gate 		case SO_RECVUCRED:
61937c478bd9Sstevel@tonic-gate 			len = sizeof (int);
61947c478bd9Sstevel@tonic-gate 			valp = (int *)ptr;
61957c478bd9Sstevel@tonic-gate 			*valp = (tep->te_flag & TL_SOCKUCRED) != 0;
61967c478bd9Sstevel@tonic-gate 			break;
61977c478bd9Sstevel@tonic-gate 		default:
61987c478bd9Sstevel@tonic-gate 			break;
61997c478bd9Sstevel@tonic-gate 		}
62007c478bd9Sstevel@tonic-gate 		break;
62017c478bd9Sstevel@tonic-gate 	case TL_PROT_LEVEL:
62027c478bd9Sstevel@tonic-gate 		switch (name) {
62037c478bd9Sstevel@tonic-gate 		case TL_OPT_PEER_CRED:
62047c478bd9Sstevel@tonic-gate 		case TL_OPT_PEER_UCRED:
62057c478bd9Sstevel@tonic-gate 			/*
62067c478bd9Sstevel@tonic-gate 			 * option not supposed to retrieved directly
62077c478bd9Sstevel@tonic-gate 			 * Only sent in T_CON_{IND,CON}, T_UNITDATA_IND
62087c478bd9Sstevel@tonic-gate 			 * when some internal flags set by other options
62097c478bd9Sstevel@tonic-gate 			 * Direct retrieval always designed to fail(ignored)
62107c478bd9Sstevel@tonic-gate 			 * for this option.
62117c478bd9Sstevel@tonic-gate 			 */
62127c478bd9Sstevel@tonic-gate 			break;
62137c478bd9Sstevel@tonic-gate 		}
62147c478bd9Sstevel@tonic-gate 	}
62157c478bd9Sstevel@tonic-gate 	return (len);
62167c478bd9Sstevel@tonic-gate }
62177c478bd9Sstevel@tonic-gate 
62187c478bd9Sstevel@tonic-gate /* ARGSUSED */
62197c478bd9Sstevel@tonic-gate static int
tl_set_opt(queue_t * wq,uint_t mgmt_flags,int level,int name,uint_t inlen,uchar_t * invalp,uint_t * outlenp,uchar_t * outvalp,void * thisdg_attrs,cred_t * cr)6220cb492d90SToomas Soome tl_set_opt(queue_t *wq, uint_t mgmt_flags, int level, int name, uint_t inlen,
6221cb492d90SToomas Soome     uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp, void *thisdg_attrs,
6222cb492d90SToomas Soome     cred_t *cr)
62237c478bd9Sstevel@tonic-gate {
62247c478bd9Sstevel@tonic-gate 	int error;
62257c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep;
62267c478bd9Sstevel@tonic-gate 
62277c478bd9Sstevel@tonic-gate 	tep = (tl_endpt_t *)wq->q_ptr;
62287c478bd9Sstevel@tonic-gate 
62297c478bd9Sstevel@tonic-gate 	error = 0;		/* NOERROR */
62307c478bd9Sstevel@tonic-gate 
62317c478bd9Sstevel@tonic-gate 	/*
62327c478bd9Sstevel@tonic-gate 	 * Assumes: option level and name sanity checks done elsewhere
62337c478bd9Sstevel@tonic-gate 	 */
62347c478bd9Sstevel@tonic-gate 
62357c478bd9Sstevel@tonic-gate 	switch (level) {
62367c478bd9Sstevel@tonic-gate 	case SOL_SOCKET:
6237cb492d90SToomas Soome 		if (!IS_SOCKET(tep)) {
62387c478bd9Sstevel@tonic-gate 			error = EINVAL;
62397c478bd9Sstevel@tonic-gate 			break;
62407c478bd9Sstevel@tonic-gate 		}
62417c478bd9Sstevel@tonic-gate 		/*
62427c478bd9Sstevel@tonic-gate 		 * TBD: fill in other AF_UNIX socket options and then stop
62437c478bd9Sstevel@tonic-gate 		 * returning error.
62447c478bd9Sstevel@tonic-gate 		 */
62457c478bd9Sstevel@tonic-gate 		switch (name) {
62467c478bd9Sstevel@tonic-gate 		case SO_RECVUCRED:
62477c478bd9Sstevel@tonic-gate 			/*
62487c478bd9Sstevel@tonic-gate 			 * We only support this for datagram sockets;
62497c478bd9Sstevel@tonic-gate 			 * getpeerucred handles the connection oriented
62507c478bd9Sstevel@tonic-gate 			 * transports.
62517c478bd9Sstevel@tonic-gate 			 */
6252cb492d90SToomas Soome 			if (!IS_CLTS(tep)) {
62537c478bd9Sstevel@tonic-gate 				error = EINVAL;
62547c478bd9Sstevel@tonic-gate 				break;
62557c478bd9Sstevel@tonic-gate 			}
62567c478bd9Sstevel@tonic-gate 			if (*(int *)invalp == 0)
62577c478bd9Sstevel@tonic-gate 				tep->te_flag &= ~TL_SOCKUCRED;
62587c478bd9Sstevel@tonic-gate 			else
62597c478bd9Sstevel@tonic-gate 				tep->te_flag |= TL_SOCKUCRED;
62607c478bd9Sstevel@tonic-gate 			break;
62617c478bd9Sstevel@tonic-gate 		default:
62627c478bd9Sstevel@tonic-gate 			error = EINVAL;
62637c478bd9Sstevel@tonic-gate 			break;
62647c478bd9Sstevel@tonic-gate 		}
62657c478bd9Sstevel@tonic-gate 		break;
62667c478bd9Sstevel@tonic-gate 	case TL_PROT_LEVEL:
62677c478bd9Sstevel@tonic-gate 		switch (name) {
62687c478bd9Sstevel@tonic-gate 		case TL_OPT_PEER_CRED:
62697c478bd9Sstevel@tonic-gate 		case TL_OPT_PEER_UCRED:
62707c478bd9Sstevel@tonic-gate 			/*
62717c478bd9Sstevel@tonic-gate 			 * option not supposed to be set directly
62727c478bd9Sstevel@tonic-gate 			 * Its value in initialized for each endpoint at
62737c478bd9Sstevel@tonic-gate 			 * driver open time.
62747c478bd9Sstevel@tonic-gate 			 * Direct setting always designed to fail for this
62757c478bd9Sstevel@tonic-gate 			 * option.
62767c478bd9Sstevel@tonic-gate 			 */
62777c478bd9Sstevel@tonic-gate 			(void) (STRLOG(TL_ID, tep->te_minor, 1,
6278cb492d90SToomas Soome 			    SL_TRACE | SL_ERROR,
6279fc80c0dfSnordmark 			    "tl_set_opt: option is not supported"));
62807c478bd9Sstevel@tonic-gate 			error = EPROTO;
62817c478bd9Sstevel@tonic-gate 			break;
62827c478bd9Sstevel@tonic-gate 		}
62837c478bd9Sstevel@tonic-gate 	}
62847c478bd9Sstevel@tonic-gate 	return (error);
62857c478bd9Sstevel@tonic-gate }
62867c478bd9Sstevel@tonic-gate 
62877c478bd9Sstevel@tonic-gate 
62887c478bd9Sstevel@tonic-gate static void
tl_timer(void * arg)62897c478bd9Sstevel@tonic-gate tl_timer(void *arg)
62907c478bd9Sstevel@tonic-gate {
62917c478bd9Sstevel@tonic-gate 	queue_t *wq = arg;
62927c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep = (tl_endpt_t *)wq->q_ptr;
62937c478bd9Sstevel@tonic-gate 
62947c478bd9Sstevel@tonic-gate 	ASSERT(tep);
62957c478bd9Sstevel@tonic-gate 
62967c478bd9Sstevel@tonic-gate 	tep->te_timoutid = 0;
62977c478bd9Sstevel@tonic-gate 
62987c478bd9Sstevel@tonic-gate 	enableok(wq);
62997c478bd9Sstevel@tonic-gate 	/*
63007c478bd9Sstevel@tonic-gate 	 * Note: can call wsrv directly here and save context switch
63017c478bd9Sstevel@tonic-gate 	 * Consider change when qtimeout (not timeout) is active
63027c478bd9Sstevel@tonic-gate 	 */
63037c478bd9Sstevel@tonic-gate 	qenable(wq);
63047c478bd9Sstevel@tonic-gate }
63057c478bd9Sstevel@tonic-gate 
63067c478bd9Sstevel@tonic-gate static void
tl_buffer(void * arg)63077c478bd9Sstevel@tonic-gate tl_buffer(void *arg)
63087c478bd9Sstevel@tonic-gate {
63097c478bd9Sstevel@tonic-gate 	queue_t *wq = arg;
63107c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep = (tl_endpt_t *)wq->q_ptr;
63117c478bd9Sstevel@tonic-gate 
63127c478bd9Sstevel@tonic-gate 	ASSERT(tep);
63137c478bd9Sstevel@tonic-gate 
63147c478bd9Sstevel@tonic-gate 	tep->te_bufcid = 0;
63157c478bd9Sstevel@tonic-gate 	tep->te_nowsrv = B_FALSE;
63167c478bd9Sstevel@tonic-gate 
63177c478bd9Sstevel@tonic-gate 	enableok(wq);
63187c478bd9Sstevel@tonic-gate 	/*
63197c478bd9Sstevel@tonic-gate 	 *  Note: can call wsrv directly here and save context switch
63207c478bd9Sstevel@tonic-gate 	 * Consider change when qbufcall (not bufcall) is active
63217c478bd9Sstevel@tonic-gate 	 */
63227c478bd9Sstevel@tonic-gate 	qenable(wq);
63237c478bd9Sstevel@tonic-gate }
63247c478bd9Sstevel@tonic-gate 
63257c478bd9Sstevel@tonic-gate static void
tl_memrecover(queue_t * wq,mblk_t * mp,size_t size)63267c478bd9Sstevel@tonic-gate tl_memrecover(queue_t *wq, mblk_t *mp, size_t size)
63277c478bd9Sstevel@tonic-gate {
63287c478bd9Sstevel@tonic-gate 	tl_endpt_t *tep;
63297c478bd9Sstevel@tonic-gate 
63307c478bd9Sstevel@tonic-gate 	tep = (tl_endpt_t *)wq->q_ptr;
63317c478bd9Sstevel@tonic-gate 
63327c478bd9Sstevel@tonic-gate 	if (tep->te_closing) {
63337c478bd9Sstevel@tonic-gate 		freemsg(mp);
63347c478bd9Sstevel@tonic-gate 		return;
63357c478bd9Sstevel@tonic-gate 	}
63367c478bd9Sstevel@tonic-gate 	noenable(wq);
63377c478bd9Sstevel@tonic-gate 
63387c478bd9Sstevel@tonic-gate 	(void) insq(wq, wq->q_first, mp);
63397c478bd9Sstevel@tonic-gate 
63407c478bd9Sstevel@tonic-gate 	if (tep->te_bufcid || tep->te_timoutid) {
6341cb492d90SToomas Soome 		(void) (STRLOG(TL_ID, tep->te_minor, 1, SL_TRACE | SL_ERROR,
6342fc80c0dfSnordmark 		    "tl_memrecover:recover %p pending", (void *)wq));
63437c478bd9Sstevel@tonic-gate 		return;
63447c478bd9Sstevel@tonic-gate 	}
63457c478bd9Sstevel@tonic-gate 
6346cb492d90SToomas Soome 	tep->te_bufcid = qbufcall(wq, size, BPRI_MED, tl_buffer, wq);
6347cb492d90SToomas Soome 	if (tep->te_bufcid == NULL) {
63487c478bd9Sstevel@tonic-gate 		tep->te_timoutid = qtimeout(wq, tl_timer, wq,
63497c478bd9Sstevel@tonic-gate 		    drv_usectohz(TL_BUFWAIT));
63507c478bd9Sstevel@tonic-gate 	}
63517c478bd9Sstevel@tonic-gate }
63527c478bd9Sstevel@tonic-gate 
63537c478bd9Sstevel@tonic-gate static void
tl_freetip(tl_endpt_t * tep,tl_icon_t * tip)63547c478bd9Sstevel@tonic-gate tl_freetip(tl_endpt_t *tep, tl_icon_t *tip)
63557c478bd9Sstevel@tonic-gate {
63567c478bd9Sstevel@tonic-gate 	ASSERT(tip->ti_seqno != 0);
63577c478bd9Sstevel@tonic-gate 
63587c478bd9Sstevel@tonic-gate 	if (tip->ti_mp != NULL) {
63597c478bd9Sstevel@tonic-gate 		tl_icon_freemsgs(&tip->ti_mp);
63607c478bd9Sstevel@tonic-gate 		tip->ti_mp = NULL;
63617c478bd9Sstevel@tonic-gate 	}
63627c478bd9Sstevel@tonic-gate 	if (tip->ti_tep != NULL) {
63637c478bd9Sstevel@tonic-gate 		tl_refrele(tip->ti_tep);
63647c478bd9Sstevel@tonic-gate 		tip->ti_tep = NULL;
63657c478bd9Sstevel@tonic-gate 	}
63667c478bd9Sstevel@tonic-gate 	list_remove(&tep->te_iconp, tip);
63677c478bd9Sstevel@tonic-gate 	kmem_free(tip, sizeof (tl_icon_t));
63687c478bd9Sstevel@tonic-gate 	tep->te_nicon--;
63697c478bd9Sstevel@tonic-gate }
63707c478bd9Sstevel@tonic-gate 
63717c478bd9Sstevel@tonic-gate /*
63727c478bd9Sstevel@tonic-gate  * Remove address from address hash.
63737c478bd9Sstevel@tonic-gate  */
63747c478bd9Sstevel@tonic-gate static void
tl_addr_unbind(tl_endpt_t * tep)63757c478bd9Sstevel@tonic-gate tl_addr_unbind(tl_endpt_t *tep)
63767c478bd9Sstevel@tonic-gate {
63777c478bd9Sstevel@tonic-gate 	tl_endpt_t *elp;
63787c478bd9Sstevel@tonic-gate 
63797c478bd9Sstevel@tonic-gate 	if (tep->te_flag & TL_ADDRHASHED) {
63807c478bd9Sstevel@tonic-gate 		if (IS_SOCKET(tep)) {
63817c478bd9Sstevel@tonic-gate 			(void) mod_hash_remove(tep->te_addrhash,
63827c478bd9Sstevel@tonic-gate 			    (mod_hash_key_t)tep->te_vp,
63837c478bd9Sstevel@tonic-gate 			    (mod_hash_val_t *)&elp);
63847c478bd9Sstevel@tonic-gate 			tep->te_vp = (void *)(uintptr_t)tep->te_minor;
63857c478bd9Sstevel@tonic-gate 			tep->te_magic = SOU_MAGIC_IMPLICIT;
63867c478bd9Sstevel@tonic-gate 		} else {
63877c478bd9Sstevel@tonic-gate 			(void) mod_hash_remove(tep->te_addrhash,
63887c478bd9Sstevel@tonic-gate 			    (mod_hash_key_t)&tep->te_ap,
63897c478bd9Sstevel@tonic-gate 			    (mod_hash_val_t *)&elp);
63907c478bd9Sstevel@tonic-gate 			(void) kmem_free(tep->te_abuf, tep->te_alen);
63917c478bd9Sstevel@tonic-gate 			tep->te_alen = -1;
63927c478bd9Sstevel@tonic-gate 			tep->te_abuf = NULL;
63937c478bd9Sstevel@tonic-gate 		}
63947c478bd9Sstevel@tonic-gate 		tep->te_flag &= ~TL_ADDRHASHED;
63957c478bd9Sstevel@tonic-gate 	}
63967c478bd9Sstevel@tonic-gate }
6397