xref: /onnv-gate/usr/src/uts/common/fs/sockfs/sockstr.c (revision 2933:b83c1115488b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51548Srshoaib  * Common Development and Distribution License (the "License").
61548Srshoaib  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211548Srshoaib 
220Sstevel@tonic-gate /*
231548Srshoaib  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/inttypes.h>
310Sstevel@tonic-gate #include <sys/t_lock.h>
320Sstevel@tonic-gate #include <sys/param.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/buf.h>
350Sstevel@tonic-gate #include <sys/conf.h>
360Sstevel@tonic-gate #include <sys/cred.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/sysmacros.h>
390Sstevel@tonic-gate #include <sys/vfs.h>
400Sstevel@tonic-gate #include <sys/vnode.h>
410Sstevel@tonic-gate #include <sys/debug.h>
420Sstevel@tonic-gate #include <sys/errno.h>
430Sstevel@tonic-gate #include <sys/time.h>
440Sstevel@tonic-gate #include <sys/file.h>
450Sstevel@tonic-gate #include <sys/user.h>
460Sstevel@tonic-gate #include <sys/stream.h>
470Sstevel@tonic-gate #include <sys/strsubr.h>
480Sstevel@tonic-gate #include <sys/esunddi.h>
490Sstevel@tonic-gate #include <sys/flock.h>
500Sstevel@tonic-gate #include <sys/modctl.h>
510Sstevel@tonic-gate #include <sys/vtrace.h>
520Sstevel@tonic-gate #include <sys/strsun.h>
530Sstevel@tonic-gate #include <sys/cmn_err.h>
540Sstevel@tonic-gate #include <sys/proc.h>
550Sstevel@tonic-gate #include <sys/ddi.h>
560Sstevel@tonic-gate #include <sys/kmem_impl.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/suntpi.h>
590Sstevel@tonic-gate #include <sys/socket.h>
600Sstevel@tonic-gate #include <sys/sockio.h>
610Sstevel@tonic-gate #include <sys/socketvar.h>
620Sstevel@tonic-gate #include <netinet/in.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <sys/tiuser.h>
650Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
660Sstevel@tonic-gate #include <sys/tihdr.h>
670Sstevel@tonic-gate 
68898Skais #include <inet/kssl/ksslapi.h>
69898Skais 
700Sstevel@tonic-gate #include <c2/audit.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate int so_default_version = SOV_SOCKSTREAM;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #ifdef DEBUG
750Sstevel@tonic-gate /* Set sockdebug to print debug messages when SO_DEBUG is set */
760Sstevel@tonic-gate int sockdebug = 0;
770Sstevel@tonic-gate 
780Sstevel@tonic-gate /* Set sockprinterr to print error messages when SO_DEBUG is set */
790Sstevel@tonic-gate int sockprinterr = 0;
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * Set so_default_options to SO_DEBUG is all sockets should be created
830Sstevel@tonic-gate  * with SO_DEBUG set. This is needed to get debug printouts from the
840Sstevel@tonic-gate  * socket() call itself.
850Sstevel@tonic-gate  */
860Sstevel@tonic-gate int so_default_options = 0;
870Sstevel@tonic-gate #endif /* DEBUG */
880Sstevel@tonic-gate 
890Sstevel@tonic-gate #ifdef SOCK_TEST
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Set to number of ticks to limit cv_waits for code coverage testing.
920Sstevel@tonic-gate  * Set to 1000 when SO_DEBUG is set to 2.
930Sstevel@tonic-gate  */
940Sstevel@tonic-gate clock_t sock_test_timelimit = 0;
950Sstevel@tonic-gate #endif /* SOCK_TEST */
960Sstevel@tonic-gate 
970Sstevel@tonic-gate /*
980Sstevel@tonic-gate  * For concurrency testing of e.g. opening /dev/ip which does not
990Sstevel@tonic-gate  * handle T_INFO_REQ messages.
1000Sstevel@tonic-gate  */
1010Sstevel@tonic-gate int so_no_tinfo = 0;
1020Sstevel@tonic-gate 
1030Sstevel@tonic-gate /*
1040Sstevel@tonic-gate  * Timeout for getting a T_CAPABILITY_ACK - it is possible for a provider
1050Sstevel@tonic-gate  * to simply ignore the T_CAPABILITY_REQ.
1060Sstevel@tonic-gate  */
1070Sstevel@tonic-gate clock_t	sock_capability_timeout	= 2;	/* seconds */
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate static int	do_tcapability(struct sonode *so, t_uscalar_t cap_bits1);
1100Sstevel@tonic-gate static void	so_removehooks(struct sonode *so);
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate static mblk_t *strsock_proto(vnode_t *vp, mblk_t *mp,
1130Sstevel@tonic-gate 		strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1140Sstevel@tonic-gate 		strsigset_t *allmsgsigs, strpollset_t *pollwakeups);
1150Sstevel@tonic-gate static mblk_t *strsock_misc(vnode_t *vp, mblk_t *mp,
1160Sstevel@tonic-gate 		strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1170Sstevel@tonic-gate 		strsigset_t *allmsgsigs, strpollset_t *pollwakeups);
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate static int tlitosyserr(int terr);
1200Sstevel@tonic-gate 
1210Sstevel@tonic-gate /*
1220Sstevel@tonic-gate  * Convert a socket to a stream. Invoked when the illusory sockmod
1230Sstevel@tonic-gate  * is popped from the stream.
1240Sstevel@tonic-gate  * Change the stream head back to default operation without losing
1250Sstevel@tonic-gate  * any messages (T_conn_ind's are moved to the stream head queue).
1260Sstevel@tonic-gate  */
1270Sstevel@tonic-gate int
1280Sstevel@tonic-gate so_sock2stream(struct sonode *so)
1290Sstevel@tonic-gate {
1300Sstevel@tonic-gate 	struct vnode		*vp = SOTOV(so);
1310Sstevel@tonic-gate 	queue_t			*rq;
1320Sstevel@tonic-gate 	mblk_t			*mp;
1330Sstevel@tonic-gate 	int			error = 0;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_plumb_lock));
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
1380Sstevel@tonic-gate 	so_lock_single(so);
1390Sstevel@tonic-gate 
1400Sstevel@tonic-gate 	ASSERT(so->so_version != SOV_STREAM);
1410Sstevel@tonic-gate 
142741Smasputra 	if (so->so_state & SS_DIRECT) {
143741Smasputra 		mblk_t **mpp;
144741Smasputra 		int rval;
1450Sstevel@tonic-gate 
146741Smasputra 		/*
147741Smasputra 		 * Tell the transport below that sockmod is being popped
148741Smasputra 		 */
1490Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
150741Smasputra 		error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K, CRED(),
1510Sstevel@tonic-gate 		    &rval);
1520Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
1530Sstevel@tonic-gate 		if (error != 0) {
154741Smasputra 			dprintso(so, 0, ("so_sock2stream(%p): "
155741Smasputra 			    "_SIOCSOCKFALLBACK failed\n", so));
1560Sstevel@tonic-gate 			goto exit;
1570Sstevel@tonic-gate 		}
158741Smasputra 		so->so_state &= ~SS_DIRECT;
1590Sstevel@tonic-gate 
1600Sstevel@tonic-gate 		for (mpp = &so->so_conn_ind_head; (mp = *mpp) != NULL;
1610Sstevel@tonic-gate 		    mpp = &mp->b_next) {
1620Sstevel@tonic-gate 			struct T_conn_ind	*conn_ind;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 			/*
1650Sstevel@tonic-gate 			 * strsock_proto() has already verified the length of
1660Sstevel@tonic-gate 			 * this message block.
1670Sstevel@tonic-gate 			 */
1680Sstevel@tonic-gate 			ASSERT(MBLKL(mp) >= sizeof (struct T_conn_ind));
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 			conn_ind = (struct T_conn_ind *)mp->b_rptr;
1710Sstevel@tonic-gate 			if (conn_ind->OPT_length == 0 &&
1720Sstevel@tonic-gate 			    conn_ind->OPT_offset == 0)
1730Sstevel@tonic-gate 				continue;
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 			if (DB_REF(mp) > 1) {
1760Sstevel@tonic-gate 				mblk_t	*newmp;
1770Sstevel@tonic-gate 				size_t	length;
1780Sstevel@tonic-gate 				cred_t	*cr;
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 				/*
1810Sstevel@tonic-gate 				 * Copy the message block because it is used
1820Sstevel@tonic-gate 				 * elsewhere, too.
1830Sstevel@tonic-gate 				 */
1840Sstevel@tonic-gate 				length = MBLKL(mp);
1850Sstevel@tonic-gate 				newmp = soallocproto(length, _ALLOC_INTR);
1860Sstevel@tonic-gate 				if (newmp == NULL) {
1870Sstevel@tonic-gate 					error = EINTR;
1880Sstevel@tonic-gate 					goto exit;
1890Sstevel@tonic-gate 				}
1900Sstevel@tonic-gate 				bcopy(mp->b_rptr, newmp->b_wptr, length);
1910Sstevel@tonic-gate 				newmp->b_wptr += length;
1920Sstevel@tonic-gate 				newmp->b_next = mp->b_next;
1930Sstevel@tonic-gate 				cr = DB_CRED(mp);
1940Sstevel@tonic-gate 				if (cr != NULL)
1950Sstevel@tonic-gate 					mblk_setcred(newmp, cr);
1960Sstevel@tonic-gate 				DB_CPID(newmp) = DB_CPID(mp);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate 				/*
1990Sstevel@tonic-gate 				 * Link the new message block into the queue
2000Sstevel@tonic-gate 				 * and free the old one.
2010Sstevel@tonic-gate 				 */
2020Sstevel@tonic-gate 				*mpp = newmp;
2030Sstevel@tonic-gate 				mp->b_next = NULL;
2040Sstevel@tonic-gate 				freemsg(mp);
2050Sstevel@tonic-gate 
2060Sstevel@tonic-gate 				mp = newmp;
2070Sstevel@tonic-gate 				conn_ind = (struct T_conn_ind *)mp->b_rptr;
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 			/*
2110Sstevel@tonic-gate 			 * Remove options added by TCP for accept fast-path.
2120Sstevel@tonic-gate 			 */
2130Sstevel@tonic-gate 			conn_ind->OPT_length = 0;
2140Sstevel@tonic-gate 			conn_ind->OPT_offset = 0;
2150Sstevel@tonic-gate 		}
2160Sstevel@tonic-gate 	}
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	so->so_version = SOV_STREAM;
2190Sstevel@tonic-gate 	so->so_priv = NULL;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	/*
2220Sstevel@tonic-gate 	 * Remove the hooks in the stream head to avoid queuing more
2230Sstevel@tonic-gate 	 * packets in sockfs.
2240Sstevel@tonic-gate 	 */
2250Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2260Sstevel@tonic-gate 	so_removehooks(so);
2270Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	/*
2300Sstevel@tonic-gate 	 * Clear any state related to urgent data. Leave any T_EXDATA_IND
2310Sstevel@tonic-gate 	 * on the queue - the behavior of urgent data after a switch is
2320Sstevel@tonic-gate 	 * left undefined.
2330Sstevel@tonic-gate 	 */
2340Sstevel@tonic-gate 	so->so_error = so->so_delayed_error = 0;
2350Sstevel@tonic-gate 	freemsg(so->so_oobmsg);
2360Sstevel@tonic-gate 	so->so_oobmsg = NULL;
2370Sstevel@tonic-gate 	so->so_oobsigcnt = so->so_oobcnt = 0;
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	so->so_state &= ~(SS_RCVATMARK|SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA|
2400Sstevel@tonic-gate 	    SS_HASCONNIND|SS_SAVEDEOR);
2410Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	freemsg(so->so_ack_mp);
2440Sstevel@tonic-gate 	so->so_ack_mp = NULL;
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	/*
2470Sstevel@tonic-gate 	 * Flush the T_DISCON_IND on so_discon_ind_mp.
2480Sstevel@tonic-gate 	 */
2490Sstevel@tonic-gate 	so_flush_discon_ind(so);
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 	/*
2520Sstevel@tonic-gate 	 * Move any queued T_CONN_IND messages to stream head queue.
2530Sstevel@tonic-gate 	 */
2540Sstevel@tonic-gate 	rq = RD(strvp2wq(vp));
2550Sstevel@tonic-gate 	while ((mp = so->so_conn_ind_head) != NULL) {
2560Sstevel@tonic-gate 		so->so_conn_ind_head = mp->b_next;
2570Sstevel@tonic-gate 		mp->b_next = NULL;
2580Sstevel@tonic-gate 		if (so->so_conn_ind_head == NULL) {
2590Sstevel@tonic-gate 			ASSERT(so->so_conn_ind_tail == mp);
2600Sstevel@tonic-gate 			so->so_conn_ind_tail = NULL;
2610Sstevel@tonic-gate 		}
2620Sstevel@tonic-gate 		dprintso(so, 0,
2630Sstevel@tonic-gate 			("so_sock2stream(%p): moving T_CONN_IND\n",
2640Sstevel@tonic-gate 			so));
2650Sstevel@tonic-gate 
2660Sstevel@tonic-gate 		/* Drop lock across put() */
2670Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
2680Sstevel@tonic-gate 		put(rq, mp);
2690Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
2700Sstevel@tonic-gate 	}
2710Sstevel@tonic-gate 
2720Sstevel@tonic-gate exit:
2730Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
2740Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
2750Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2760Sstevel@tonic-gate 	return (error);
2770Sstevel@tonic-gate }
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate /*
2800Sstevel@tonic-gate  * Covert a stream back to a socket. This is invoked when the illusory
2810Sstevel@tonic-gate  * sockmod is pushed on a stream (where the stream was "created" by
2820Sstevel@tonic-gate  * popping the illusory sockmod).
2830Sstevel@tonic-gate  * This routine can not recreate the socket state (certain aspects of
2840Sstevel@tonic-gate  * it like urgent data state and the bound/connected addresses for AF_UNIX
2850Sstevel@tonic-gate  * sockets can not be recreated by asking the transport for information).
2860Sstevel@tonic-gate  * Thus this routine implicitly assumes that the socket is in an initial
2870Sstevel@tonic-gate  * state (as if it was just created). It flushes any messages queued on the
2880Sstevel@tonic-gate  * read queue to avoid dealing with e.g. TPI acks or T_exdata_ind messages.
2890Sstevel@tonic-gate  */
2900Sstevel@tonic-gate void
2910Sstevel@tonic-gate so_stream2sock(struct sonode *so)
2920Sstevel@tonic-gate {
2930Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_plumb_lock));
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
2980Sstevel@tonic-gate 	so_lock_single(so);
2990Sstevel@tonic-gate 	ASSERT(so->so_version == SOV_STREAM);
3000Sstevel@tonic-gate 	so->so_version = SOV_SOCKSTREAM;
3010Sstevel@tonic-gate 	so->so_pushcnt = 0;
3020Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 	/*
3050Sstevel@tonic-gate 	 * Set a permenent error to force any thread in sorecvmsg to
3060Sstevel@tonic-gate 	 * return (and drop SOREADLOCKED). Clear the error once
3070Sstevel@tonic-gate 	 * we have SOREADLOCKED.
3080Sstevel@tonic-gate 	 * This makes a read sleeping during the I_PUSH of sockmod return
3090Sstevel@tonic-gate 	 * EIO.
3100Sstevel@tonic-gate 	 */
3110Sstevel@tonic-gate 	strsetrerror(SOTOV(so), EIO, 1, NULL);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/*
3140Sstevel@tonic-gate 	 * Get the read lock before flushing data to avoid
3150Sstevel@tonic-gate 	 * problems with the T_EXDATA_IND MSG_PEEK code in sorecvmsg.
3160Sstevel@tonic-gate 	 */
3170Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
3180Sstevel@tonic-gate 	(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
3190Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	strsetrerror(SOTOV(so), 0, 0, NULL);
3220Sstevel@tonic-gate 	so_installhooks(so);
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate 	/*
3250Sstevel@tonic-gate 	 * Flush everything on the read queue.
3260Sstevel@tonic-gate 	 * This ensures that no T_CONN_IND remain and that no T_EXDATA_IND
3270Sstevel@tonic-gate 	 * remain; those types of messages would confuse sockfs.
3280Sstevel@tonic-gate 	 */
3290Sstevel@tonic-gate 	strflushrq(vp, FLUSHALL);
3300Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 	/*
3330Sstevel@tonic-gate 	 * Flush the T_DISCON_IND on so_discon_ind_mp.
3340Sstevel@tonic-gate 	 */
3350Sstevel@tonic-gate 	so_flush_discon_ind(so);
3360Sstevel@tonic-gate 	so_unlock_read(so);	/* Clear SOREADLOCKED */
3370Sstevel@tonic-gate 
3380Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
3390Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
3400Sstevel@tonic-gate }
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate /*
3430Sstevel@tonic-gate  * Install the hooks in the stream head.
3440Sstevel@tonic-gate  */
3450Sstevel@tonic-gate void
3460Sstevel@tonic-gate so_installhooks(struct sonode *so)
3470Sstevel@tonic-gate {
3480Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	strsetrputhooks(vp, SH_SIGALLDATA | SH_IGN_ZEROLEN | SH_CONSOL_DATA,
3510Sstevel@tonic-gate 	    strsock_proto, strsock_misc);
3520Sstevel@tonic-gate 	strsetwputhooks(vp, SH_SIGPIPE | SH_RECHECK_ERR, 0);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate 
3550Sstevel@tonic-gate /*
3560Sstevel@tonic-gate  * Remove the hooks in the stream head.
3570Sstevel@tonic-gate  */
3580Sstevel@tonic-gate static void
3590Sstevel@tonic-gate so_removehooks(struct sonode *so)
3600Sstevel@tonic-gate {
3610Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	strsetrputhooks(vp, 0, NULL, NULL);
3640Sstevel@tonic-gate 	strsetwputhooks(vp, 0, STRTIMOUT);
3650Sstevel@tonic-gate 	/*
3660Sstevel@tonic-gate 	 * Leave read behavior as it would have been for a normal
3670Sstevel@tonic-gate 	 * stream i.e. a read of an M_PROTO will fail.
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate }
3700Sstevel@tonic-gate 
3710Sstevel@tonic-gate /*
3720Sstevel@tonic-gate  * Initialize the streams side of a socket including
3730Sstevel@tonic-gate  * T_info_req/ack processing. If tso is not NULL its values are used thereby
3740Sstevel@tonic-gate  * avoiding the T_INFO_REQ.
3750Sstevel@tonic-gate  */
3760Sstevel@tonic-gate int
3770Sstevel@tonic-gate so_strinit(struct sonode *so, struct sonode *tso)
3780Sstevel@tonic-gate {
3790Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
3800Sstevel@tonic-gate 	struct stdata *stp;
3810Sstevel@tonic-gate 	mblk_t *mp;
3820Sstevel@tonic-gate 	int error;
3830Sstevel@tonic-gate 
3840Sstevel@tonic-gate 	dprintso(so, 1, ("so_strinit(%p)\n", so));
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/* Preallocate an unbind_req message */
3870Sstevel@tonic-gate 	mp = soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP);
3880Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
3890Sstevel@tonic-gate 	so->so_unbind_mp = mp;
3900Sstevel@tonic-gate #ifdef DEBUG
3910Sstevel@tonic-gate 	so->so_options = so_default_options;
3920Sstevel@tonic-gate #endif /* DEBUG */
3930Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	so_installhooks(so);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	/*
3980Sstevel@tonic-gate 	 * The T_CAPABILITY_REQ should be the first message sent down because
3990Sstevel@tonic-gate 	 * at least TCP has a fast-path for this which avoids timeouts while
4000Sstevel@tonic-gate 	 * waiting for the T_CAPABILITY_ACK under high system load.
4010Sstevel@tonic-gate 	 */
4020Sstevel@tonic-gate 	if (tso == NULL) {
4030Sstevel@tonic-gate 		error = do_tcapability(so, TC1_ACCEPTOR_ID | TC1_INFO);
4040Sstevel@tonic-gate 		if (error)
4050Sstevel@tonic-gate 			return (error);
4060Sstevel@tonic-gate 	} else {
4070Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
4080Sstevel@tonic-gate 		so->so_tsdu_size = tso->so_tsdu_size;
4090Sstevel@tonic-gate 		so->so_etsdu_size = tso->so_etsdu_size;
4100Sstevel@tonic-gate 		so->so_addr_size = tso->so_addr_size;
4110Sstevel@tonic-gate 		so->so_opt_size = tso->so_opt_size;
4120Sstevel@tonic-gate 		so->so_tidu_size = tso->so_tidu_size;
4130Sstevel@tonic-gate 		so->so_serv_type = tso->so_serv_type;
4140Sstevel@tonic-gate 		so->so_mode = tso->so_mode & ~SM_ACCEPTOR_ID;
4150Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 		/* the following do_tcapability may update so->so_mode */
4180Sstevel@tonic-gate 		if ((tso->so_serv_type != T_CLTS) &&
4192811Sja97890 		    !(tso->so_state & SS_DIRECT)) {
4200Sstevel@tonic-gate 			error = do_tcapability(so, TC1_ACCEPTOR_ID);
4210Sstevel@tonic-gate 			if (error)
4220Sstevel@tonic-gate 				return (error);
4230Sstevel@tonic-gate 		}
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 	/*
4260Sstevel@tonic-gate 	 * If the addr_size is 0 we treat it as already bound
4270Sstevel@tonic-gate 	 * and connected. This is used by the routing socket.
4280Sstevel@tonic-gate 	 * We set the addr_size to something to allocate a the address
4290Sstevel@tonic-gate 	 * structures.
4300Sstevel@tonic-gate 	 */
4310Sstevel@tonic-gate 	if (so->so_addr_size == 0) {
4320Sstevel@tonic-gate 		so->so_state |= SS_ISBOUND | SS_ISCONNECTED;
4330Sstevel@tonic-gate 		/* Address size can vary with address families. */
4340Sstevel@tonic-gate 		if (so->so_family == AF_INET6)
4350Sstevel@tonic-gate 			so->so_addr_size =
4360Sstevel@tonic-gate 			    (t_scalar_t)sizeof (struct sockaddr_in6);
4370Sstevel@tonic-gate 		else
4380Sstevel@tonic-gate 			so->so_addr_size =
4390Sstevel@tonic-gate 			    (t_scalar_t)sizeof (struct sockaddr_in);
4400Sstevel@tonic-gate 		ASSERT(so->so_unbind_mp);
4410Sstevel@tonic-gate 	}
4420Sstevel@tonic-gate 	/*
4430Sstevel@tonic-gate 	 * Allocate the addresses.
4440Sstevel@tonic-gate 	 */
4450Sstevel@tonic-gate 	ASSERT(so->so_laddr_sa == NULL && so->so_faddr_sa == NULL);
4460Sstevel@tonic-gate 	ASSERT(so->so_laddr_len == 0 && so->so_faddr_len == 0);
4470Sstevel@tonic-gate 	so->so_laddr_maxlen = so->so_faddr_maxlen =
4480Sstevel@tonic-gate 		    P2ROUNDUP(so->so_addr_size, KMEM_ALIGN);
4490Sstevel@tonic-gate 	so->so_laddr_sa = kmem_alloc(so->so_laddr_maxlen * 2, KM_SLEEP);
4500Sstevel@tonic-gate 	so->so_faddr_sa = (struct sockaddr *)((caddr_t)so->so_laddr_sa
4510Sstevel@tonic-gate 		    + so->so_laddr_maxlen);
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
4540Sstevel@tonic-gate 		/*
4550Sstevel@tonic-gate 		 * Initialize AF_UNIX related fields.
4560Sstevel@tonic-gate 		 */
4570Sstevel@tonic-gate 		bzero(&so->so_ux_laddr, sizeof (so->so_ux_laddr));
4580Sstevel@tonic-gate 		bzero(&so->so_ux_faddr, sizeof (so->so_ux_faddr));
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 
4610Sstevel@tonic-gate 	stp = vp->v_stream;
4620Sstevel@tonic-gate 	/*
4630Sstevel@tonic-gate 	 * Have to keep minpsz at zero in order to allow write/send of zero
4640Sstevel@tonic-gate 	 * bytes.
4650Sstevel@tonic-gate 	 */
4660Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
4670Sstevel@tonic-gate 	if (stp->sd_qn_minpsz == 1)
4680Sstevel@tonic-gate 		stp->sd_qn_minpsz = 0;
4690Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	return (0);
4720Sstevel@tonic-gate }
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate static void
4750Sstevel@tonic-gate copy_tinfo(struct sonode *so, struct T_info_ack *tia)
4760Sstevel@tonic-gate {
4770Sstevel@tonic-gate 	so->so_tsdu_size = tia->TSDU_size;
4780Sstevel@tonic-gate 	so->so_etsdu_size = tia->ETSDU_size;
4790Sstevel@tonic-gate 	so->so_addr_size = tia->ADDR_size;
4800Sstevel@tonic-gate 	so->so_opt_size = tia->OPT_size;
4810Sstevel@tonic-gate 	so->so_tidu_size = tia->TIDU_size;
4820Sstevel@tonic-gate 	so->so_serv_type = tia->SERV_type;
4830Sstevel@tonic-gate 	switch (tia->CURRENT_state) {
4840Sstevel@tonic-gate 	case TS_UNBND:
4850Sstevel@tonic-gate 		break;
4860Sstevel@tonic-gate 	case TS_IDLE:
4870Sstevel@tonic-gate 		so->so_state |= SS_ISBOUND;
4880Sstevel@tonic-gate 		so->so_laddr_len = 0;
4890Sstevel@tonic-gate 		so->so_state &= ~SS_LADDR_VALID;
4900Sstevel@tonic-gate 		break;
4910Sstevel@tonic-gate 	case TS_DATA_XFER:
4920Sstevel@tonic-gate 		so->so_state |= SS_ISBOUND|SS_ISCONNECTED;
4930Sstevel@tonic-gate 		so->so_laddr_len = 0;
4940Sstevel@tonic-gate 		so->so_faddr_len = 0;
4950Sstevel@tonic-gate 		so->so_state &= ~(SS_LADDR_VALID | SS_FADDR_VALID);
4960Sstevel@tonic-gate 		break;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 
4990Sstevel@tonic-gate 	/*
5000Sstevel@tonic-gate 	 * Heuristics for determining the socket mode flags
5010Sstevel@tonic-gate 	 * (SM_ATOMIC, SM_CONNREQUIRED, SM_ADDR, SM_FDPASSING,
5020Sstevel@tonic-gate 	 * and SM_EXDATA, SM_OPTDATA, and SM_BYTESTREAM)
5030Sstevel@tonic-gate 	 * from the info ack.
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	if (so->so_serv_type == T_CLTS) {
5060Sstevel@tonic-gate 		so->so_mode |= SM_ATOMIC | SM_ADDR;
5070Sstevel@tonic-gate 	} else {
5080Sstevel@tonic-gate 		so->so_mode |= SM_CONNREQUIRED;
5090Sstevel@tonic-gate 		if (so->so_etsdu_size != 0 && so->so_etsdu_size != -2)
5100Sstevel@tonic-gate 			so->so_mode |= SM_EXDATA;
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 	if (so->so_type == SOCK_SEQPACKET || so->so_type == SOCK_RAW) {
5130Sstevel@tonic-gate 		/* Semantics are to discard tail end of messages */
5140Sstevel@tonic-gate 		so->so_mode |= SM_ATOMIC;
5150Sstevel@tonic-gate 	}
5160Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
5170Sstevel@tonic-gate 		so->so_mode |= SM_FDPASSING | SM_OPTDATA;
5180Sstevel@tonic-gate 		if (so->so_addr_size == -1) {
5190Sstevel@tonic-gate 			/* MAXPATHLEN + soun_family + nul termination */
5200Sstevel@tonic-gate 			so->so_addr_size = (t_scalar_t)(MAXPATHLEN +
5210Sstevel@tonic-gate 				sizeof (short) + 1);
5220Sstevel@tonic-gate 		}
5230Sstevel@tonic-gate 		if (so->so_type == SOCK_STREAM) {
5240Sstevel@tonic-gate 			/*
5250Sstevel@tonic-gate 			 * Make it into a byte-stream transport.
5260Sstevel@tonic-gate 			 * SOCK_SEQPACKET sockets are unchanged.
5270Sstevel@tonic-gate 			 */
5280Sstevel@tonic-gate 			so->so_tsdu_size = 0;
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 	} else if (so->so_addr_size == -1) {
5310Sstevel@tonic-gate 		/*
5320Sstevel@tonic-gate 		 * Logic extracted from sockmod - have to pick some max address
5330Sstevel@tonic-gate 		 * length in order to preallocate the addresses.
5340Sstevel@tonic-gate 		 */
5350Sstevel@tonic-gate 		so->so_addr_size = SOA_DEFSIZE;
5360Sstevel@tonic-gate 	}
5370Sstevel@tonic-gate 	if (so->so_tsdu_size == 0)
5380Sstevel@tonic-gate 		so->so_mode |= SM_BYTESTREAM;
5390Sstevel@tonic-gate }
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate static int
5420Sstevel@tonic-gate check_tinfo(struct sonode *so)
5430Sstevel@tonic-gate {
5440Sstevel@tonic-gate 	/* Consistency checks */
5450Sstevel@tonic-gate 	if (so->so_type == SOCK_DGRAM && so->so_serv_type != T_CLTS) {
5460Sstevel@tonic-gate 		eprintso(so, ("service type and socket type mismatch\n"));
5470Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
5480Sstevel@tonic-gate 		return (EPROTO);
5490Sstevel@tonic-gate 	}
5500Sstevel@tonic-gate 	if (so->so_type == SOCK_STREAM && so->so_serv_type == T_CLTS) {
5510Sstevel@tonic-gate 		eprintso(so, ("service type and socket type mismatch\n"));
5520Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
5530Sstevel@tonic-gate 		return (EPROTO);
5540Sstevel@tonic-gate 	}
5550Sstevel@tonic-gate 	if (so->so_type == SOCK_SEQPACKET && so->so_serv_type == T_CLTS) {
5560Sstevel@tonic-gate 		eprintso(so, ("service type and socket type mismatch\n"));
5570Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
5580Sstevel@tonic-gate 		return (EPROTO);
5590Sstevel@tonic-gate 	}
5600Sstevel@tonic-gate 	if (so->so_family == AF_INET &&
5610Sstevel@tonic-gate 	    so->so_addr_size != (t_scalar_t)sizeof (struct sockaddr_in)) {
5620Sstevel@tonic-gate 		eprintso(so,
5630Sstevel@tonic-gate 		    ("AF_INET must have sockaddr_in address length. Got %d\n",
5640Sstevel@tonic-gate 		    so->so_addr_size));
5650Sstevel@tonic-gate 		eprintsoline(so, EMSGSIZE);
5660Sstevel@tonic-gate 		return (EMSGSIZE);
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 	if (so->so_family == AF_INET6 &&
5690Sstevel@tonic-gate 	    so->so_addr_size != (t_scalar_t)sizeof (struct sockaddr_in6)) {
5700Sstevel@tonic-gate 		eprintso(so,
5710Sstevel@tonic-gate 		    ("AF_INET6 must have sockaddr_in6 address length. Got %d\n",
5720Sstevel@tonic-gate 		    so->so_addr_size));
5730Sstevel@tonic-gate 		eprintsoline(so, EMSGSIZE);
5740Sstevel@tonic-gate 		return (EMSGSIZE);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	dprintso(so, 1, (
5780Sstevel@tonic-gate 	    "tinfo: serv %d tsdu %d, etsdu %d, addr %d, opt %d, tidu %d\n",
5790Sstevel@tonic-gate 	    so->so_serv_type, so->so_tsdu_size, so->so_etsdu_size,
5800Sstevel@tonic-gate 	    so->so_addr_size, so->so_opt_size,
5810Sstevel@tonic-gate 	    so->so_tidu_size));
5820Sstevel@tonic-gate 	dprintso(so, 1, ("tinfo: so_state %s\n",
5830Sstevel@tonic-gate 			pr_state(so->so_state, so->so_mode)));
5840Sstevel@tonic-gate 	return (0);
5850Sstevel@tonic-gate }
5860Sstevel@tonic-gate 
5870Sstevel@tonic-gate /*
5880Sstevel@tonic-gate  * Send down T_info_req and wait for the ack.
5890Sstevel@tonic-gate  * Record interesting T_info_ack values in the sonode.
5900Sstevel@tonic-gate  */
5910Sstevel@tonic-gate static int
5920Sstevel@tonic-gate do_tinfo(struct sonode *so)
5930Sstevel@tonic-gate {
5940Sstevel@tonic-gate 	struct T_info_req tir;
5950Sstevel@tonic-gate 	mblk_t *mp;
5960Sstevel@tonic-gate 	int error;
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
5990Sstevel@tonic-gate 
6000Sstevel@tonic-gate 	if (so_no_tinfo) {
6010Sstevel@tonic-gate 		so->so_addr_size = 0;
6020Sstevel@tonic-gate 		return (0);
6030Sstevel@tonic-gate 	}
6040Sstevel@tonic-gate 
6050Sstevel@tonic-gate 	dprintso(so, 1, ("do_tinfo(%p)\n", so));
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 	/* Send T_INFO_REQ */
6080Sstevel@tonic-gate 	tir.PRIM_type = T_INFO_REQ;
6090Sstevel@tonic-gate 	mp = soallocproto1(&tir, sizeof (tir),
6100Sstevel@tonic-gate 	    sizeof (struct T_info_req) + sizeof (struct T_info_ack),
6110Sstevel@tonic-gate 	    _ALLOC_INTR);
6120Sstevel@tonic-gate 	if (mp == NULL) {
6130Sstevel@tonic-gate 		eprintsoline(so, ENOBUFS);
6140Sstevel@tonic-gate 		return (ENOBUFS);
6150Sstevel@tonic-gate 	}
6160Sstevel@tonic-gate 	/* T_INFO_REQ has to be M_PCPROTO */
6170Sstevel@tonic-gate 	DB_TYPE(mp) = M_PCPROTO;
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
6200Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
6210Sstevel@tonic-gate 	if (error) {
6220Sstevel@tonic-gate 		eprintsoline(so, error);
6230Sstevel@tonic-gate 		return (error);
6240Sstevel@tonic-gate 	}
6250Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
6260Sstevel@tonic-gate 	/* Wait for T_INFO_ACK */
6270Sstevel@tonic-gate 	if ((error = sowaitprim(so, T_INFO_REQ, T_INFO_ACK,
6280Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (struct T_info_ack), &mp, 0))) {
6290Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
6300Sstevel@tonic-gate 		eprintsoline(so, error);
6310Sstevel@tonic-gate 		return (error);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	ASSERT(mp);
6350Sstevel@tonic-gate 	copy_tinfo(so, (struct T_info_ack *)mp->b_rptr);
6360Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
6370Sstevel@tonic-gate 	freemsg(mp);
6380Sstevel@tonic-gate 	return (check_tinfo(so));
6390Sstevel@tonic-gate }
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate /*
6420Sstevel@tonic-gate  * Send down T_capability_req and wait for the ack.
6430Sstevel@tonic-gate  * Record interesting T_capability_ack values in the sonode.
6440Sstevel@tonic-gate  */
6450Sstevel@tonic-gate static int
6460Sstevel@tonic-gate do_tcapability(struct sonode *so, t_uscalar_t cap_bits1)
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate 	struct T_capability_req tcr;
6490Sstevel@tonic-gate 	struct T_capability_ack *tca;
6500Sstevel@tonic-gate 	mblk_t *mp;
6510Sstevel@tonic-gate 	int error;
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate 	ASSERT(cap_bits1 != 0);
6540Sstevel@tonic-gate 	ASSERT((cap_bits1 & ~(TC1_ACCEPTOR_ID | TC1_INFO)) == 0);
6550Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 	if (so->so_provinfo->tpi_capability == PI_NO)
6580Sstevel@tonic-gate 		return (do_tinfo(so));
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	if (so_no_tinfo) {
6610Sstevel@tonic-gate 		so->so_addr_size = 0;
6620Sstevel@tonic-gate 		if ((cap_bits1 &= ~TC1_INFO) == 0)
6630Sstevel@tonic-gate 			return (0);
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	dprintso(so, 1, ("do_tcapability(%p)\n", so));
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	/* Send T_CAPABILITY_REQ */
6690Sstevel@tonic-gate 	tcr.PRIM_type = T_CAPABILITY_REQ;
6700Sstevel@tonic-gate 	tcr.CAP_bits1 = cap_bits1;
6710Sstevel@tonic-gate 	mp = soallocproto1(&tcr, sizeof (tcr),
6720Sstevel@tonic-gate 	    sizeof (struct T_capability_req) + sizeof (struct T_capability_ack),
6730Sstevel@tonic-gate 	    _ALLOC_INTR);
6740Sstevel@tonic-gate 	if (mp == NULL) {
6750Sstevel@tonic-gate 		eprintsoline(so, ENOBUFS);
6760Sstevel@tonic-gate 		return (ENOBUFS);
6770Sstevel@tonic-gate 	}
6780Sstevel@tonic-gate 	/* T_CAPABILITY_REQ should be M_PCPROTO here */
6790Sstevel@tonic-gate 	DB_TYPE(mp) = M_PCPROTO;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
6820Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
6830Sstevel@tonic-gate 	if (error) {
6840Sstevel@tonic-gate 		eprintsoline(so, error);
6850Sstevel@tonic-gate 		return (error);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
6880Sstevel@tonic-gate 	/* Wait for T_CAPABILITY_ACK */
6890Sstevel@tonic-gate 	if ((error = sowaitprim(so, T_CAPABILITY_REQ, T_CAPABILITY_ACK,
6900Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (*tca), &mp, sock_capability_timeout * hz))) {
6910Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
6920Sstevel@tonic-gate 		PI_PROVLOCK(so->so_provinfo);
6930Sstevel@tonic-gate 		if (so->so_provinfo->tpi_capability == PI_DONTKNOW)
6940Sstevel@tonic-gate 			so->so_provinfo->tpi_capability = PI_NO;
6950Sstevel@tonic-gate 		PI_PROVUNLOCK(so->so_provinfo);
6960Sstevel@tonic-gate 		ASSERT((so->so_mode & SM_ACCEPTOR_ID) == 0);
6970Sstevel@tonic-gate 		if (cap_bits1 & TC1_INFO) {
6980Sstevel@tonic-gate 			/*
6990Sstevel@tonic-gate 			 * If the T_CAPABILITY_REQ timed out and then a
7000Sstevel@tonic-gate 			 * T_INFO_REQ gets a protocol error, most likely
7010Sstevel@tonic-gate 			 * the capability was slow (vs. unsupported). Return
7020Sstevel@tonic-gate 			 * ENOSR for this case as a best guess.
7030Sstevel@tonic-gate 			 */
7040Sstevel@tonic-gate 			if (error == ETIME) {
7050Sstevel@tonic-gate 				return ((error = do_tinfo(so)) == EPROTO ?
7060Sstevel@tonic-gate 				    ENOSR : error);
7070Sstevel@tonic-gate 			}
7080Sstevel@tonic-gate 			return (do_tinfo(so));
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 		return (0);
7110Sstevel@tonic-gate 	}
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	if (so->so_provinfo->tpi_capability == PI_DONTKNOW) {
7140Sstevel@tonic-gate 		PI_PROVLOCK(so->so_provinfo);
7150Sstevel@tonic-gate 		so->so_provinfo->tpi_capability = PI_YES;
7160Sstevel@tonic-gate 		PI_PROVUNLOCK(so->so_provinfo);
7170Sstevel@tonic-gate 	}
7180Sstevel@tonic-gate 
7190Sstevel@tonic-gate 	ASSERT(mp);
7200Sstevel@tonic-gate 	tca = (struct T_capability_ack *)mp->b_rptr;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	ASSERT((cap_bits1 & TC1_INFO) == (tca->CAP_bits1 & TC1_INFO));
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 	cap_bits1 = tca->CAP_bits1;
7250Sstevel@tonic-gate 
7260Sstevel@tonic-gate 	if (cap_bits1 & TC1_ACCEPTOR_ID) {
7270Sstevel@tonic-gate 		so->so_acceptor_id = tca->ACCEPTOR_id;
7280Sstevel@tonic-gate 		so->so_mode |= SM_ACCEPTOR_ID;
7290Sstevel@tonic-gate 	}
7300Sstevel@tonic-gate 
7310Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO)
7320Sstevel@tonic-gate 		copy_tinfo(so, &tca->INFO_ack);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
7350Sstevel@tonic-gate 	freemsg(mp);
7360Sstevel@tonic-gate 
7370Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO)
7380Sstevel@tonic-gate 		return (check_tinfo(so));
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	return (0);
7410Sstevel@tonic-gate }
7420Sstevel@tonic-gate 
7430Sstevel@tonic-gate /*
7440Sstevel@tonic-gate  * Retrieve and clear the socket error.
7450Sstevel@tonic-gate  */
7460Sstevel@tonic-gate int
7470Sstevel@tonic-gate sogeterr(struct sonode *so)
7480Sstevel@tonic-gate {
7490Sstevel@tonic-gate 	int error;
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 	error = so->so_error;
7540Sstevel@tonic-gate 	so->so_error = 0;
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	return (error);
7570Sstevel@tonic-gate }
7580Sstevel@tonic-gate 
7590Sstevel@tonic-gate /*
7600Sstevel@tonic-gate  * This routine is registered with the stream head to retrieve read
7610Sstevel@tonic-gate  * side errors.
7620Sstevel@tonic-gate  * It does not clear the socket error for a peeking read side operation.
7630Sstevel@tonic-gate  * It the error is to be cleared it sets *clearerr.
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate int
7660Sstevel@tonic-gate sogetrderr(vnode_t *vp, int ispeek, int *clearerr)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	struct sonode *so = VTOSO(vp);
7690Sstevel@tonic-gate 	int error;
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
7720Sstevel@tonic-gate 	if (ispeek) {
7730Sstevel@tonic-gate 		error = so->so_error;
7740Sstevel@tonic-gate 		*clearerr = 0;
7750Sstevel@tonic-gate 	} else {
7760Sstevel@tonic-gate 		error = so->so_error;
7770Sstevel@tonic-gate 		so->so_error = 0;
7780Sstevel@tonic-gate 		*clearerr = 1;
7790Sstevel@tonic-gate 	}
7800Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
7810Sstevel@tonic-gate 	return (error);
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /*
7850Sstevel@tonic-gate  * This routine is registered with the stream head to retrieve write
7860Sstevel@tonic-gate  * side errors.
7870Sstevel@tonic-gate  * It does not clear the socket error for a peeking read side operation.
7880Sstevel@tonic-gate  * It the error is to be cleared it sets *clearerr.
7890Sstevel@tonic-gate  */
7900Sstevel@tonic-gate int
7910Sstevel@tonic-gate sogetwrerr(vnode_t *vp, int ispeek, int *clearerr)
7920Sstevel@tonic-gate {
7930Sstevel@tonic-gate 	struct sonode *so = VTOSO(vp);
7940Sstevel@tonic-gate 	int error;
7950Sstevel@tonic-gate 
7960Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
7970Sstevel@tonic-gate 	if (so->so_state & SS_CANTSENDMORE) {
7980Sstevel@tonic-gate 		error = EPIPE;
7990Sstevel@tonic-gate 		*clearerr = 0;
8000Sstevel@tonic-gate 	} else {
8010Sstevel@tonic-gate 		error = so->so_error;
8020Sstevel@tonic-gate 		if (ispeek) {
8030Sstevel@tonic-gate 			*clearerr = 0;
8040Sstevel@tonic-gate 		} else {
8050Sstevel@tonic-gate 			so->so_error = 0;
8060Sstevel@tonic-gate 			*clearerr = 1;
8070Sstevel@tonic-gate 		}
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
8100Sstevel@tonic-gate 	return (error);
8110Sstevel@tonic-gate }
8120Sstevel@tonic-gate 
8130Sstevel@tonic-gate /*
8140Sstevel@tonic-gate  * Set a nonpersistent read and write error on the socket.
8150Sstevel@tonic-gate  * Used when there is a T_uderror_ind for a connected socket.
8160Sstevel@tonic-gate  * The caller also needs to call strsetrerror and strsetwerror
8170Sstevel@tonic-gate  * after dropping the lock.
8180Sstevel@tonic-gate  */
8190Sstevel@tonic-gate void
8200Sstevel@tonic-gate soseterror(struct sonode *so, int error)
8210Sstevel@tonic-gate {
8220Sstevel@tonic-gate 	ASSERT(error != 0);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8250Sstevel@tonic-gate 	so->so_error = (ushort_t)error;
8260Sstevel@tonic-gate }
8270Sstevel@tonic-gate 
8280Sstevel@tonic-gate void
8290Sstevel@tonic-gate soisconnecting(struct sonode *so)
8300Sstevel@tonic-gate {
8310Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8320Sstevel@tonic-gate 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
8330Sstevel@tonic-gate 	so->so_state |= SS_ISCONNECTING;
8340Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8350Sstevel@tonic-gate }
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate void
8380Sstevel@tonic-gate soisconnected(struct sonode *so)
8390Sstevel@tonic-gate {
8400Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8410Sstevel@tonic-gate 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
8420Sstevel@tonic-gate 	so->so_state |= SS_ISCONNECTED;
8430Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate  * The caller also needs to call strsetrerror, strsetwerror and strseteof.
8480Sstevel@tonic-gate  */
8490Sstevel@tonic-gate void
8500Sstevel@tonic-gate soisdisconnected(struct sonode *so, int error)
8510Sstevel@tonic-gate {
8520Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8530Sstevel@tonic-gate 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING|
8540Sstevel@tonic-gate 	    SS_LADDR_VALID|SS_FADDR_VALID);
8550Sstevel@tonic-gate 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
8560Sstevel@tonic-gate 	so->so_error = (ushort_t)error;
8570Sstevel@tonic-gate 	if (so->so_peercred != NULL) {
8580Sstevel@tonic-gate 		crfree(so->so_peercred);
8590Sstevel@tonic-gate 		so->so_peercred = NULL;
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate /*
8650Sstevel@tonic-gate  * For connected AF_UNIX SOCK_DGRAM sockets when the peer closes.
8660Sstevel@tonic-gate  * Does not affect write side.
8670Sstevel@tonic-gate  * The caller also has to call strsetrerror.
8680Sstevel@tonic-gate  */
8690Sstevel@tonic-gate static void
8700Sstevel@tonic-gate sobreakconn(struct sonode *so, int error)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8730Sstevel@tonic-gate 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
8740Sstevel@tonic-gate 	so->so_error = (ushort_t)error;
8750Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8760Sstevel@tonic-gate }
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate /*
8790Sstevel@tonic-gate  * Can no longer send.
8800Sstevel@tonic-gate  * Caller must also call strsetwerror.
8810Sstevel@tonic-gate  *
8820Sstevel@tonic-gate  * We mark the peer address as no longer valid for getpeername, but
8830Sstevel@tonic-gate  * leave it around for so_unix_close to notify the peer (that
8840Sstevel@tonic-gate  * transport has no addressing held at that layer).
8850Sstevel@tonic-gate  */
8860Sstevel@tonic-gate void
8870Sstevel@tonic-gate socantsendmore(struct sonode *so)
8880Sstevel@tonic-gate {
8890Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8900Sstevel@tonic-gate 	so->so_state = so->so_state & ~SS_FADDR_VALID | SS_CANTSENDMORE;
8910Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8920Sstevel@tonic-gate }
8930Sstevel@tonic-gate 
8940Sstevel@tonic-gate /*
8950Sstevel@tonic-gate  * The caller must call strseteof(,1) as well as this routine
8960Sstevel@tonic-gate  * to change the socket state.
8970Sstevel@tonic-gate  */
8980Sstevel@tonic-gate void
8990Sstevel@tonic-gate socantrcvmore(struct sonode *so)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
9020Sstevel@tonic-gate 	so->so_state |= SS_CANTRCVMORE;
9030Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
9040Sstevel@tonic-gate }
9050Sstevel@tonic-gate 
9060Sstevel@tonic-gate /*
9070Sstevel@tonic-gate  * The caller has sent down a "request_prim" primitive and wants to wait for
9080Sstevel@tonic-gate  * an ack ("ack_prim") or an T_ERROR_ACK for it.
9090Sstevel@tonic-gate  * The specified "ack_prim" can be a T_OK_ACK.
9100Sstevel@tonic-gate  *
9110Sstevel@tonic-gate  * Assumes that all the TPI acks are M_PCPROTO messages.
9120Sstevel@tonic-gate  *
9130Sstevel@tonic-gate  * Note that the socket is single-threaded (using so_lock_single)
9140Sstevel@tonic-gate  * for all operations that generate TPI ack messages. Since
9150Sstevel@tonic-gate  * only TPI ack messages are M_PCPROTO we should never receive
9160Sstevel@tonic-gate  * anything except either the ack we are expecting or a T_ERROR_ACK
9170Sstevel@tonic-gate  * for the same primitive.
9180Sstevel@tonic-gate  */
9190Sstevel@tonic-gate int
9200Sstevel@tonic-gate sowaitprim(struct sonode *so, t_scalar_t request_prim, t_scalar_t ack_prim,
9210Sstevel@tonic-gate 	    t_uscalar_t min_size, mblk_t **mpp, clock_t wait)
9220Sstevel@tonic-gate {
9230Sstevel@tonic-gate 	mblk_t *mp;
9240Sstevel@tonic-gate 	union T_primitives *tpr;
9250Sstevel@tonic-gate 	int error;
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate 	dprintso(so, 1, ("sowaitprim(%p, %d, %d, %d, %p, %lu)\n",
9280Sstevel@tonic-gate 		so, request_prim, ack_prim, min_size, mpp, wait));
9290Sstevel@tonic-gate 
9300Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
9310Sstevel@tonic-gate 
9320Sstevel@tonic-gate 	error = sowaitack(so, &mp, wait);
9330Sstevel@tonic-gate 	if (error)
9340Sstevel@tonic-gate 		return (error);
9350Sstevel@tonic-gate 
9360Sstevel@tonic-gate 	dprintso(so, 1, ("got msg %p\n", mp));
9370Sstevel@tonic-gate 	if (DB_TYPE(mp) != M_PCPROTO ||
9380Sstevel@tonic-gate 	    MBLKL(mp) < sizeof (tpr->type)) {
9390Sstevel@tonic-gate 		freemsg(mp);
9400Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
9410Sstevel@tonic-gate 		return (EPROTO);
9420Sstevel@tonic-gate 	}
9430Sstevel@tonic-gate 	tpr = (union T_primitives *)mp->b_rptr;
9440Sstevel@tonic-gate 	/*
9450Sstevel@tonic-gate 	 * Did we get the primitive that we were asking for?
9460Sstevel@tonic-gate 	 * For T_OK_ACK we also check that it matches the request primitive.
9470Sstevel@tonic-gate 	 */
9480Sstevel@tonic-gate 	if (tpr->type == ack_prim &&
9490Sstevel@tonic-gate 	    (ack_prim != T_OK_ACK ||
9500Sstevel@tonic-gate 	    tpr->ok_ack.CORRECT_prim == request_prim)) {
9510Sstevel@tonic-gate 		if (MBLKL(mp) >= (ssize_t)min_size) {
9520Sstevel@tonic-gate 			/* Found what we are looking for */
9530Sstevel@tonic-gate 			*mpp = mp;
9540Sstevel@tonic-gate 			return (0);
9550Sstevel@tonic-gate 		}
9560Sstevel@tonic-gate 		/* Too short */
9570Sstevel@tonic-gate 		freemsg(mp);
9580Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
9590Sstevel@tonic-gate 		return (EPROTO);
9600Sstevel@tonic-gate 	}
9610Sstevel@tonic-gate 
9620Sstevel@tonic-gate 	if (tpr->type == T_ERROR_ACK &&
9630Sstevel@tonic-gate 	    tpr->error_ack.ERROR_prim == request_prim) {
9640Sstevel@tonic-gate 		/* Error to the primitive we were looking for */
9650Sstevel@tonic-gate 		if (tpr->error_ack.TLI_error == TSYSERR) {
9660Sstevel@tonic-gate 			error = tpr->error_ack.UNIX_error;
9670Sstevel@tonic-gate 		} else {
9680Sstevel@tonic-gate 			error = tlitosyserr(tpr->error_ack.TLI_error);
9690Sstevel@tonic-gate 		}
9700Sstevel@tonic-gate 		dprintso(so, 0, ("error_ack for %d: %d/%d ->%d\n",
9710Sstevel@tonic-gate 			tpr->error_ack.ERROR_prim,
9720Sstevel@tonic-gate 			tpr->error_ack.TLI_error,
9730Sstevel@tonic-gate 			tpr->error_ack.UNIX_error,
9740Sstevel@tonic-gate 			error));
9750Sstevel@tonic-gate 		freemsg(mp);
9760Sstevel@tonic-gate 		return (error);
9770Sstevel@tonic-gate 	}
9780Sstevel@tonic-gate 	/*
9790Sstevel@tonic-gate 	 * Wrong primitive or T_ERROR_ACK for the wrong primitive
9800Sstevel@tonic-gate 	 */
9810Sstevel@tonic-gate #ifdef DEBUG
9820Sstevel@tonic-gate 	if (tpr->type == T_ERROR_ACK) {
9830Sstevel@tonic-gate 		dprintso(so, 0, ("error_ack for %d: %d/%d\n",
9840Sstevel@tonic-gate 			tpr->error_ack.ERROR_prim,
9850Sstevel@tonic-gate 			tpr->error_ack.TLI_error,
9860Sstevel@tonic-gate 			tpr->error_ack.UNIX_error));
9870Sstevel@tonic-gate 	} else if (tpr->type == T_OK_ACK) {
9880Sstevel@tonic-gate 		dprintso(so, 0, ("ok_ack for %d, expected %d for %d\n",
9890Sstevel@tonic-gate 			tpr->ok_ack.CORRECT_prim,
9900Sstevel@tonic-gate 			ack_prim, request_prim));
9910Sstevel@tonic-gate 	} else {
9920Sstevel@tonic-gate 		dprintso(so, 0,
9930Sstevel@tonic-gate 			("unexpected primitive %d, expected %d for %d\n",
9940Sstevel@tonic-gate 			tpr->type, ack_prim, request_prim));
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate #endif /* DEBUG */
9970Sstevel@tonic-gate 
9980Sstevel@tonic-gate 	freemsg(mp);
9990Sstevel@tonic-gate 	eprintsoline(so, EPROTO);
10000Sstevel@tonic-gate 	return (EPROTO);
10010Sstevel@tonic-gate }
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate /*
10040Sstevel@tonic-gate  * Wait for a T_OK_ACK for the specified primitive.
10050Sstevel@tonic-gate  */
10060Sstevel@tonic-gate int
10070Sstevel@tonic-gate sowaitokack(struct sonode *so, t_scalar_t request_prim)
10080Sstevel@tonic-gate {
10090Sstevel@tonic-gate 	mblk_t *mp;
10100Sstevel@tonic-gate 	int error;
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	error = sowaitprim(so, request_prim, T_OK_ACK,
10130Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (struct T_ok_ack), &mp, 0);
10140Sstevel@tonic-gate 	if (error)
10150Sstevel@tonic-gate 		return (error);
10160Sstevel@tonic-gate 	freemsg(mp);
10170Sstevel@tonic-gate 	return (0);
10180Sstevel@tonic-gate }
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate /*
10210Sstevel@tonic-gate  * Queue a received TPI ack message on so_ack_mp.
10220Sstevel@tonic-gate  */
10230Sstevel@tonic-gate void
10240Sstevel@tonic-gate soqueueack(struct sonode *so, mblk_t *mp)
10250Sstevel@tonic-gate {
10260Sstevel@tonic-gate 	if (DB_TYPE(mp) != M_PCPROTO) {
10271548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
10280Sstevel@tonic-gate 		    "sockfs: received unexpected M_PROTO TPI ack. Prim %d\n",
10290Sstevel@tonic-gate 		    *(t_scalar_t *)mp->b_rptr);
10300Sstevel@tonic-gate 		freemsg(mp);
10310Sstevel@tonic-gate 		return;
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
10350Sstevel@tonic-gate 	if (so->so_ack_mp != NULL) {
10360Sstevel@tonic-gate 		dprintso(so, 1, ("so_ack_mp already set\n"));
10370Sstevel@tonic-gate 		freemsg(so->so_ack_mp);
10380Sstevel@tonic-gate 		so->so_ack_mp = NULL;
10390Sstevel@tonic-gate 	}
10400Sstevel@tonic-gate 	so->so_ack_mp = mp;
10410Sstevel@tonic-gate 	cv_broadcast(&so->so_ack_cv);
10420Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
10430Sstevel@tonic-gate }
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate /*
10460Sstevel@tonic-gate  * Wait for a TPI ack ignoring signals and errors.
10470Sstevel@tonic-gate  */
10480Sstevel@tonic-gate int
10490Sstevel@tonic-gate sowaitack(struct sonode *so, mblk_t **mpp, clock_t wait)
10500Sstevel@tonic-gate {
10510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	while (so->so_ack_mp == NULL) {
10540Sstevel@tonic-gate #ifdef SOCK_TEST
10550Sstevel@tonic-gate 		if (wait == 0 && sock_test_timelimit != 0)
10560Sstevel@tonic-gate 			wait = sock_test_timelimit;
10570Sstevel@tonic-gate #endif
10580Sstevel@tonic-gate 		if (wait != 0) {
10590Sstevel@tonic-gate 			/*
10600Sstevel@tonic-gate 			 * Only wait for the time limit.
10610Sstevel@tonic-gate 			 */
10620Sstevel@tonic-gate 			clock_t now;
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate 			time_to_wait(&now, wait);
10650Sstevel@tonic-gate 			if (cv_timedwait(&so->so_ack_cv, &so->so_lock,
10660Sstevel@tonic-gate 			    now) == -1) {
10670Sstevel@tonic-gate 				eprintsoline(so, ETIME);
10680Sstevel@tonic-gate 				return (ETIME);
10690Sstevel@tonic-gate 			}
10700Sstevel@tonic-gate 		}
10710Sstevel@tonic-gate 		else
10720Sstevel@tonic-gate 			cv_wait(&so->so_ack_cv, &so->so_lock);
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 	*mpp = so->so_ack_mp;
10750Sstevel@tonic-gate #ifdef DEBUG
10760Sstevel@tonic-gate 	{
10770Sstevel@tonic-gate 		union T_primitives *tpr;
10780Sstevel@tonic-gate 		mblk_t *mp = *mpp;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 		tpr = (union T_primitives *)mp->b_rptr;
10810Sstevel@tonic-gate 		ASSERT(DB_TYPE(mp) == M_PCPROTO);
10820Sstevel@tonic-gate 		ASSERT(tpr->type == T_OK_ACK ||
10830Sstevel@tonic-gate 			tpr->type == T_ERROR_ACK ||
10840Sstevel@tonic-gate 			tpr->type == T_BIND_ACK ||
10850Sstevel@tonic-gate 			tpr->type == T_CAPABILITY_ACK ||
10860Sstevel@tonic-gate 			tpr->type == T_INFO_ACK ||
10870Sstevel@tonic-gate 			tpr->type == T_OPTMGMT_ACK);
10880Sstevel@tonic-gate 	}
10890Sstevel@tonic-gate #endif /* DEBUG */
10900Sstevel@tonic-gate 	so->so_ack_mp = NULL;
10910Sstevel@tonic-gate 	return (0);
10920Sstevel@tonic-gate }
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate /*
10950Sstevel@tonic-gate  * Queue a received T_CONN_IND message on so_conn_ind_head/tail.
10960Sstevel@tonic-gate  */
10970Sstevel@tonic-gate void
10980Sstevel@tonic-gate soqueueconnind(struct sonode *so, mblk_t *mp)
10990Sstevel@tonic-gate {
11000Sstevel@tonic-gate 	if (DB_TYPE(mp) != M_PROTO) {
11011548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
11020Sstevel@tonic-gate 		    "sockfs: received unexpected M_PCPROTO T_CONN_IND\n");
11030Sstevel@tonic-gate 		freemsg(mp);
11040Sstevel@tonic-gate 		return;
11050Sstevel@tonic-gate 	}
11060Sstevel@tonic-gate 
11070Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
11080Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL);
11090Sstevel@tonic-gate 	if (so->so_conn_ind_head == NULL) {
11100Sstevel@tonic-gate 		so->so_conn_ind_head = mp;
11110Sstevel@tonic-gate 		so->so_state |= SS_HASCONNIND;
11120Sstevel@tonic-gate 	} else {
11130Sstevel@tonic-gate 		ASSERT(so->so_state & SS_HASCONNIND);
11140Sstevel@tonic-gate 		ASSERT(so->so_conn_ind_tail->b_next == NULL);
11150Sstevel@tonic-gate 		so->so_conn_ind_tail->b_next = mp;
11160Sstevel@tonic-gate 	}
11170Sstevel@tonic-gate 	so->so_conn_ind_tail = mp;
11180Sstevel@tonic-gate 	/* Wakeup a single consumer of the T_CONN_IND */
11190Sstevel@tonic-gate 	cv_signal(&so->so_connind_cv);
11200Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
11210Sstevel@tonic-gate }
11220Sstevel@tonic-gate 
11230Sstevel@tonic-gate /*
11240Sstevel@tonic-gate  * Wait for a T_CONN_IND.
11250Sstevel@tonic-gate  * Don't wait if nonblocking.
11260Sstevel@tonic-gate  * Accept signals and socket errors.
11270Sstevel@tonic-gate  */
11280Sstevel@tonic-gate int
11290Sstevel@tonic-gate sowaitconnind(struct sonode *so, int fmode, mblk_t **mpp)
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	mblk_t *mp;
11320Sstevel@tonic-gate 	int error = 0;
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
11350Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
11360Sstevel@tonic-gate check_error:
11370Sstevel@tonic-gate 	if (so->so_error) {
11380Sstevel@tonic-gate 		error = sogeterr(so);
11390Sstevel@tonic-gate 		if (error) {
11400Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
11410Sstevel@tonic-gate 			return (error);
11420Sstevel@tonic-gate 		}
11430Sstevel@tonic-gate 	}
11440Sstevel@tonic-gate 
11450Sstevel@tonic-gate 	if (so->so_conn_ind_head == NULL) {
11460Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK)) {
11470Sstevel@tonic-gate 			error = EWOULDBLOCK;
11480Sstevel@tonic-gate 			goto done;
11490Sstevel@tonic-gate 		}
11500Sstevel@tonic-gate 		if (!cv_wait_sig_swap(&so->so_connind_cv, &so->so_lock)) {
11510Sstevel@tonic-gate 			error = EINTR;
11520Sstevel@tonic-gate 			goto done;
11530Sstevel@tonic-gate 		}
11540Sstevel@tonic-gate 		goto check_error;
11550Sstevel@tonic-gate 	}
11560Sstevel@tonic-gate 	mp = so->so_conn_ind_head;
11570Sstevel@tonic-gate 	so->so_conn_ind_head = mp->b_next;
11580Sstevel@tonic-gate 	mp->b_next = NULL;
11590Sstevel@tonic-gate 	if (so->so_conn_ind_head == NULL) {
11600Sstevel@tonic-gate 		ASSERT(so->so_conn_ind_tail == mp);
11610Sstevel@tonic-gate 		so->so_conn_ind_tail = NULL;
11620Sstevel@tonic-gate 		so->so_state &= ~SS_HASCONNIND;
11630Sstevel@tonic-gate 	}
11640Sstevel@tonic-gate 	*mpp = mp;
11650Sstevel@tonic-gate done:
11660Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
11670Sstevel@tonic-gate 	return (error);
11680Sstevel@tonic-gate }
11690Sstevel@tonic-gate 
11700Sstevel@tonic-gate /*
11710Sstevel@tonic-gate  * Flush a T_CONN_IND matching the sequence number from the list.
11720Sstevel@tonic-gate  * Return zero if found; non-zero otherwise.
11730Sstevel@tonic-gate  * This is called very infrequently thus it is ok to do a linear search.
11740Sstevel@tonic-gate  */
11750Sstevel@tonic-gate int
11760Sstevel@tonic-gate soflushconnind(struct sonode *so, t_scalar_t seqno)
11770Sstevel@tonic-gate {
11780Sstevel@tonic-gate 	mblk_t *prevmp, *mp;
11790Sstevel@tonic-gate 	struct T_conn_ind *tci;
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
11820Sstevel@tonic-gate 	for (prevmp = NULL, mp = so->so_conn_ind_head; mp != NULL;
11830Sstevel@tonic-gate 	    prevmp = mp, mp = mp->b_next) {
11840Sstevel@tonic-gate 		tci = (struct T_conn_ind *)mp->b_rptr;
11850Sstevel@tonic-gate 		if (tci->SEQ_number == seqno) {
11860Sstevel@tonic-gate 			dprintso(so, 1,
11870Sstevel@tonic-gate 				("t_discon_ind: found T_CONN_IND %d\n", seqno));
11880Sstevel@tonic-gate 			/* Deleting last? */
11890Sstevel@tonic-gate 			if (so->so_conn_ind_tail == mp) {
11900Sstevel@tonic-gate 				so->so_conn_ind_tail = prevmp;
11910Sstevel@tonic-gate 			}
11920Sstevel@tonic-gate 			if (prevmp == NULL) {
11930Sstevel@tonic-gate 				/* Deleting first */
11940Sstevel@tonic-gate 				so->so_conn_ind_head = mp->b_next;
11950Sstevel@tonic-gate 			} else {
11960Sstevel@tonic-gate 				prevmp->b_next = mp->b_next;
11970Sstevel@tonic-gate 			}
11980Sstevel@tonic-gate 			mp->b_next = NULL;
11990Sstevel@tonic-gate 			if (so->so_conn_ind_head == NULL) {
12000Sstevel@tonic-gate 				ASSERT(so->so_conn_ind_tail == NULL);
12010Sstevel@tonic-gate 				so->so_state &= ~SS_HASCONNIND;
12020Sstevel@tonic-gate 			} else {
12030Sstevel@tonic-gate 				ASSERT(so->so_conn_ind_tail != NULL);
12040Sstevel@tonic-gate 			}
12050Sstevel@tonic-gate 			so->so_error = ECONNABORTED;
12060Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
1207898Skais 
1208898Skais 			/*
1209898Skais 			 * T_KSSL_PROXY_CONN_IND may carry a handle for
1210898Skais 			 * an SSL context, and needs to be released.
1211898Skais 			 */
1212898Skais 			if ((tci->PRIM_type == T_SSL_PROXY_CONN_IND) &&
1213898Skais 			    (mp->b_cont != NULL)) {
1214898Skais 				kssl_ctx_t kssl_ctx;
1215898Skais 
1216898Skais 				ASSERT(MBLKL(mp->b_cont) ==
1217898Skais 				    sizeof (kssl_ctx_t));
1218898Skais 				kssl_ctx = *((kssl_ctx_t *)mp->b_cont->b_rptr);
1219898Skais 				kssl_release_ctx(kssl_ctx);
1220898Skais 			}
12210Sstevel@tonic-gate 			freemsg(mp);
12220Sstevel@tonic-gate 			return (0);
12230Sstevel@tonic-gate 		}
12240Sstevel@tonic-gate 	}
12250Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
12260Sstevel@tonic-gate 	dprintso(so, 1,	("t_discon_ind: NOT found T_CONN_IND %d\n", seqno));
12270Sstevel@tonic-gate 	return (-1);
12280Sstevel@tonic-gate }
12290Sstevel@tonic-gate 
12300Sstevel@tonic-gate /*
12310Sstevel@tonic-gate  * Wait until the socket is connected or there is an error.
12320Sstevel@tonic-gate  * fmode should contain any nonblocking flags. nosig should be
12330Sstevel@tonic-gate  * set if the caller does not want the wait to be interrupted by a signal.
12340Sstevel@tonic-gate  */
12350Sstevel@tonic-gate int
12360Sstevel@tonic-gate sowaitconnected(struct sonode *so, int fmode, int nosig)
12370Sstevel@tonic-gate {
12380Sstevel@tonic-gate 	int error;
12390Sstevel@tonic-gate 
12400Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	while ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) ==
12430Sstevel@tonic-gate 		SS_ISCONNECTING && so->so_error == 0) {
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 		dprintso(so, 1, ("waiting for SS_ISCONNECTED on %p\n", so));
12460Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
12470Sstevel@tonic-gate 			return (EINPROGRESS);
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 		if (nosig)
12500Sstevel@tonic-gate 			cv_wait(&so->so_state_cv, &so->so_lock);
12510Sstevel@tonic-gate 		else if (!cv_wait_sig_swap(&so->so_state_cv, &so->so_lock)) {
12520Sstevel@tonic-gate 			/*
12530Sstevel@tonic-gate 			 * Return EINTR and let the application use
12540Sstevel@tonic-gate 			 * nonblocking techniques for detecting when
12550Sstevel@tonic-gate 			 * the connection has been established.
12560Sstevel@tonic-gate 			 */
12570Sstevel@tonic-gate 			return (EINTR);
12580Sstevel@tonic-gate 		}
12590Sstevel@tonic-gate 		dprintso(so, 1, ("awoken on %p\n", so));
12600Sstevel@tonic-gate 	}
12610Sstevel@tonic-gate 
12620Sstevel@tonic-gate 	if (so->so_error != 0) {
12630Sstevel@tonic-gate 		error = sogeterr(so);
12640Sstevel@tonic-gate 		ASSERT(error != 0);
12650Sstevel@tonic-gate 		dprintso(so, 1, ("sowaitconnected: error %d\n", error));
12660Sstevel@tonic-gate 		return (error);
12670Sstevel@tonic-gate 	}
12680Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
12690Sstevel@tonic-gate 		/*
12700Sstevel@tonic-gate 		 * Could have received a T_ORDREL_IND or a T_DISCON_IND with
12710Sstevel@tonic-gate 		 * zero errno. Or another thread could have consumed so_error
12720Sstevel@tonic-gate 		 * e.g. by calling read.
12730Sstevel@tonic-gate 		 */
12740Sstevel@tonic-gate 		error = ECONNREFUSED;
12750Sstevel@tonic-gate 		dprintso(so, 1, ("sowaitconnected: error %d\n", error));
12760Sstevel@tonic-gate 		return (error);
12770Sstevel@tonic-gate 	}
12780Sstevel@tonic-gate 	return (0);
12790Sstevel@tonic-gate }
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate /*
12830Sstevel@tonic-gate  * Handle the signal generation aspect of urgent data.
12840Sstevel@tonic-gate  */
12850Sstevel@tonic-gate static void
12860Sstevel@tonic-gate so_oob_sig(struct sonode *so, int extrasig,
12870Sstevel@tonic-gate     strsigset_t *signals, strpollset_t *pollwakeups)
12880Sstevel@tonic-gate {
12890Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
12920Sstevel@tonic-gate 	ASSERT(so->so_oobsigcnt >= so->so_oobcnt);
12930Sstevel@tonic-gate 	if (so->so_oobsigcnt > so->so_oobcnt) {
12940Sstevel@tonic-gate 		/*
12950Sstevel@tonic-gate 		 * Signal has already been generated once for this
12960Sstevel@tonic-gate 		 * urgent "event". However, since TCP can receive updated
12970Sstevel@tonic-gate 		 * urgent pointers we still generate a signal.
12980Sstevel@tonic-gate 		 */
12990Sstevel@tonic-gate 		ASSERT(so->so_state & SS_OOBPEND);
13000Sstevel@tonic-gate 		if (extrasig) {
13010Sstevel@tonic-gate 			*signals |= S_RDBAND;
13020Sstevel@tonic-gate 			*pollwakeups |= POLLRDBAND;
13030Sstevel@tonic-gate 		}
13040Sstevel@tonic-gate 		return;
13050Sstevel@tonic-gate 	}
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	so->so_oobsigcnt++;
13080Sstevel@tonic-gate 	ASSERT(so->so_oobsigcnt > 0);	/* Wraparound */
13090Sstevel@tonic-gate 	ASSERT(so->so_oobsigcnt > so->so_oobcnt);
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	/*
13120Sstevel@tonic-gate 	 * Record (for select/poll) that urgent data is pending.
13130Sstevel@tonic-gate 	 */
13140Sstevel@tonic-gate 	so->so_state |= SS_OOBPEND;
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * New urgent data on the way so forget about any old
13170Sstevel@tonic-gate 	 * urgent data.
13180Sstevel@tonic-gate 	 */
13190Sstevel@tonic-gate 	so->so_state &= ~(SS_HAVEOOBDATA|SS_HADOOBDATA);
13200Sstevel@tonic-gate 	if (so->so_oobmsg != NULL) {
13210Sstevel@tonic-gate 		dprintso(so, 1, ("sock: discarding old oob\n"));
13220Sstevel@tonic-gate 		freemsg(so->so_oobmsg);
13230Sstevel@tonic-gate 		so->so_oobmsg = NULL;
13240Sstevel@tonic-gate 	}
13250Sstevel@tonic-gate 	*signals |= S_RDBAND;
13260Sstevel@tonic-gate 	*pollwakeups |= POLLRDBAND;
13270Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13280Sstevel@tonic-gate }
13290Sstevel@tonic-gate 
13300Sstevel@tonic-gate /*
13310Sstevel@tonic-gate  * Handle the processing of the T_EXDATA_IND with urgent data.
13320Sstevel@tonic-gate  * Returns the T_EXDATA_IND if it should be queued on the read queue.
13330Sstevel@tonic-gate  */
13340Sstevel@tonic-gate /* ARGSUSED2 */
13350Sstevel@tonic-gate static mblk_t *
13360Sstevel@tonic-gate so_oob_exdata(struct sonode *so, mblk_t *mp,
13370Sstevel@tonic-gate 	strsigset_t *signals, strpollset_t *pollwakeups)
13380Sstevel@tonic-gate {
13390Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	ASSERT(so->so_oobsigcnt > so->so_oobcnt);
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 	so->so_oobcnt++;
13460Sstevel@tonic-gate 	ASSERT(so->so_oobcnt > 0);	/* wraparound? */
13470Sstevel@tonic-gate 	ASSERT(so->so_oobsigcnt >= so->so_oobcnt);
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 	/*
13500Sstevel@tonic-gate 	 * Set MSGMARK for SIOCATMARK.
13510Sstevel@tonic-gate 	 */
13520Sstevel@tonic-gate 	mp->b_flag |= MSGMARK;
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13550Sstevel@tonic-gate 	return (mp);
13560Sstevel@tonic-gate }
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate /*
13590Sstevel@tonic-gate  * Handle the processing of the actual urgent data.
13600Sstevel@tonic-gate  * Returns the data mblk if it should be queued on the read queue.
13610Sstevel@tonic-gate  */
13620Sstevel@tonic-gate static mblk_t *
13630Sstevel@tonic-gate so_oob_data(struct sonode *so, mblk_t *mp,
13640Sstevel@tonic-gate 	strsigset_t *signals, strpollset_t *pollwakeups)
13650Sstevel@tonic-gate {
13660Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
13670Sstevel@tonic-gate 
13680Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13690Sstevel@tonic-gate 
13700Sstevel@tonic-gate 	ASSERT(so->so_oobsigcnt >= so->so_oobcnt);
13710Sstevel@tonic-gate 	ASSERT(mp != NULL);
13720Sstevel@tonic-gate 	/*
13730Sstevel@tonic-gate 	 * For OOBINLINE we keep the data in the T_EXDATA_IND.
13740Sstevel@tonic-gate 	 * Otherwise we store it in so_oobmsg.
13750Sstevel@tonic-gate 	 */
13760Sstevel@tonic-gate 	ASSERT(so->so_oobmsg == NULL);
13770Sstevel@tonic-gate 	if (so->so_options & SO_OOBINLINE) {
13780Sstevel@tonic-gate 		*pollwakeups |= POLLIN | POLLRDNORM | POLLRDBAND;
13790Sstevel@tonic-gate 		*signals |= S_INPUT | S_RDNORM;
13800Sstevel@tonic-gate 	} else {
13810Sstevel@tonic-gate 		*pollwakeups |= POLLRDBAND;
13820Sstevel@tonic-gate 		so->so_state |= SS_HAVEOOBDATA;
13830Sstevel@tonic-gate 		so->so_oobmsg = mp;
13840Sstevel@tonic-gate 		mp = NULL;
13850Sstevel@tonic-gate 	}
13860Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13870Sstevel@tonic-gate 	return (mp);
13880Sstevel@tonic-gate }
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate /*
13910Sstevel@tonic-gate  * Caller must hold the mutex.
13920Sstevel@tonic-gate  * For delayed processing, save the T_DISCON_IND received
13930Sstevel@tonic-gate  * from below on so_discon_ind_mp.
13940Sstevel@tonic-gate  * When the message is processed the framework will call:
13950Sstevel@tonic-gate  *      (*func)(so, mp);
13960Sstevel@tonic-gate  */
13970Sstevel@tonic-gate static void
13980Sstevel@tonic-gate so_save_discon_ind(struct sonode *so,
13990Sstevel@tonic-gate 	mblk_t *mp,
14000Sstevel@tonic-gate 	void (*func)(struct sonode *so, mblk_t *))
14010Sstevel@tonic-gate {
14020Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14030Sstevel@tonic-gate 
14040Sstevel@tonic-gate 	/*
14050Sstevel@tonic-gate 	 * Discard new T_DISCON_IND if we have already received another.
14060Sstevel@tonic-gate 	 * Currently the earlier message can either be on so_discon_ind_mp
14070Sstevel@tonic-gate 	 * or being processed.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	if (so->so_discon_ind_mp != NULL || (so->so_flag & SOASYNC_UNBIND)) {
14101548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
14110Sstevel@tonic-gate 		    "sockfs: received unexpected additional T_DISCON_IND\n");
14120Sstevel@tonic-gate 		freemsg(mp);
14130Sstevel@tonic-gate 		return;
14140Sstevel@tonic-gate 	}
14150Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)func;
14160Sstevel@tonic-gate 	mp->b_next = NULL;
14170Sstevel@tonic-gate 	so->so_discon_ind_mp = mp;
14180Sstevel@tonic-gate }
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate /*
14210Sstevel@tonic-gate  * Caller must hold the mutex and make sure that either SOLOCKED
14220Sstevel@tonic-gate  * or SOASYNC_UNBIND is set. Called from so_unlock_single().
14230Sstevel@tonic-gate  * Perform delayed processing of T_DISCON_IND message on so_discon_ind_mp.
14240Sstevel@tonic-gate  * Need to ensure that strsock_proto() will not end up sleeping for
14250Sstevel@tonic-gate  * SOASYNC_UNBIND, while executing this function.
14260Sstevel@tonic-gate  */
14270Sstevel@tonic-gate void
14280Sstevel@tonic-gate so_drain_discon_ind(struct sonode *so)
14290Sstevel@tonic-gate {
14300Sstevel@tonic-gate 	mblk_t	*bp;
14310Sstevel@tonic-gate 	void (*func)(struct sonode *so, mblk_t *);
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14340Sstevel@tonic-gate 	ASSERT(so->so_flag & (SOLOCKED|SOASYNC_UNBIND));
14350Sstevel@tonic-gate 
14360Sstevel@tonic-gate 	/* Process T_DISCON_IND on so_discon_ind_mp */
14370Sstevel@tonic-gate 	if ((bp = so->so_discon_ind_mp) != NULL) {
14380Sstevel@tonic-gate 		so->so_discon_ind_mp = NULL;
14390Sstevel@tonic-gate 		func = (void (*)())bp->b_prev;
14400Sstevel@tonic-gate 		bp->b_prev = NULL;
14410Sstevel@tonic-gate 
14420Sstevel@tonic-gate 		/*
14430Sstevel@tonic-gate 		 * This (*func) is supposed to generate a message downstream
14440Sstevel@tonic-gate 		 * and we need to have a flag set until the corresponding
14450Sstevel@tonic-gate 		 * upstream message reaches stream head.
14460Sstevel@tonic-gate 		 * When processing T_DISCON_IND in strsock_discon_ind
14470Sstevel@tonic-gate 		 * we hold SOASYN_UNBIND when sending T_UNBIND_REQ down and
14480Sstevel@tonic-gate 		 * drop the flag after we get the ACK in strsock_proto.
14490Sstevel@tonic-gate 		 */
14500Sstevel@tonic-gate 		(void) (*func)(so, bp);
14510Sstevel@tonic-gate 	}
14520Sstevel@tonic-gate }
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate /*
14550Sstevel@tonic-gate  * Caller must hold the mutex.
14560Sstevel@tonic-gate  * Remove the T_DISCON_IND on so_discon_ind_mp.
14570Sstevel@tonic-gate  */
14580Sstevel@tonic-gate void
14590Sstevel@tonic-gate so_flush_discon_ind(struct sonode *so)
14600Sstevel@tonic-gate {
14610Sstevel@tonic-gate 	mblk_t	*bp;
14620Sstevel@tonic-gate 
14630Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14640Sstevel@tonic-gate 
14650Sstevel@tonic-gate 	/*
14660Sstevel@tonic-gate 	 * Remove T_DISCON_IND mblk at so_discon_ind_mp.
14670Sstevel@tonic-gate 	 */
14680Sstevel@tonic-gate 	if ((bp = so->so_discon_ind_mp) != NULL) {
14690Sstevel@tonic-gate 		so->so_discon_ind_mp = NULL;
14700Sstevel@tonic-gate 		bp->b_prev = NULL;
14710Sstevel@tonic-gate 		freemsg(bp);
14720Sstevel@tonic-gate 	}
14730Sstevel@tonic-gate }
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate /*
14760Sstevel@tonic-gate  * Caller must hold the mutex.
14770Sstevel@tonic-gate  *
14780Sstevel@tonic-gate  * This function is used to process the T_DISCON_IND message. It does
14790Sstevel@tonic-gate  * immediate processing when called from strsock_proto and delayed
14800Sstevel@tonic-gate  * processing of discon_ind saved on so_discon_ind_mp when called from
14810Sstevel@tonic-gate  * so_drain_discon_ind. When a T_DISCON_IND message is saved in
14820Sstevel@tonic-gate  * so_discon_ind_mp for delayed processing, this function is registered
14830Sstevel@tonic-gate  * as the callback function to process the message.
14840Sstevel@tonic-gate  *
14850Sstevel@tonic-gate  * SOASYNC_UNBIND should be held in this function, during the non-blocking
14860Sstevel@tonic-gate  * unbind operation, and should be released only after we receive the ACK
14870Sstevel@tonic-gate  * in strsock_proto, for the T_UNBIND_REQ sent here. Since SOLOCKED is not set,
14880Sstevel@tonic-gate  * no TPI messages would be sent down at this time. This is to prevent M_FLUSH
14890Sstevel@tonic-gate  * sent from either this function or tcp_unbind(), flushing away any TPI
14900Sstevel@tonic-gate  * message that is being sent down and stays in a lower module's queue.
14910Sstevel@tonic-gate  *
14920Sstevel@tonic-gate  * This function drops so_lock and grabs it again.
14930Sstevel@tonic-gate  */
14940Sstevel@tonic-gate static void
14950Sstevel@tonic-gate strsock_discon_ind(struct sonode *so, mblk_t *discon_mp)
14960Sstevel@tonic-gate {
14970Sstevel@tonic-gate 	struct vnode *vp;
14980Sstevel@tonic-gate 	struct stdata *stp;
14990Sstevel@tonic-gate 	union T_primitives *tpr;
15000Sstevel@tonic-gate 	struct T_unbind_req *ubr;
15010Sstevel@tonic-gate 	mblk_t *mp;
15020Sstevel@tonic-gate 	int error;
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
15050Sstevel@tonic-gate 	ASSERT(discon_mp);
15060Sstevel@tonic-gate 	ASSERT(discon_mp->b_rptr);
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate 	tpr = (union T_primitives *)discon_mp->b_rptr;
15090Sstevel@tonic-gate 	ASSERT(tpr->type == T_DISCON_IND);
15100Sstevel@tonic-gate 
15110Sstevel@tonic-gate 	vp = SOTOV(so);
15120Sstevel@tonic-gate 	stp = vp->v_stream;
15130Sstevel@tonic-gate 	ASSERT(stp);
15140Sstevel@tonic-gate 
15150Sstevel@tonic-gate 	/*
15160Sstevel@tonic-gate 	 * Not a listener
15170Sstevel@tonic-gate 	 */
15180Sstevel@tonic-gate 	ASSERT((so->so_state & SS_ACCEPTCONN) == 0);
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 	/*
15210Sstevel@tonic-gate 	 * This assumes that the name space for DISCON_reason
15220Sstevel@tonic-gate 	 * is the errno name space.
15230Sstevel@tonic-gate 	 */
15240Sstevel@tonic-gate 	soisdisconnected(so, tpr->discon_ind.DISCON_reason);
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	/*
15270Sstevel@tonic-gate 	 * Unbind with the transport without blocking.
15280Sstevel@tonic-gate 	 * If we've already received a T_DISCON_IND do not unbind.
15290Sstevel@tonic-gate 	 *
15300Sstevel@tonic-gate 	 * If there is no preallocated unbind message, we have already
15310Sstevel@tonic-gate 	 * unbound with the transport
15320Sstevel@tonic-gate 	 *
15330Sstevel@tonic-gate 	 * If the socket is not bound, no need to unbind.
15340Sstevel@tonic-gate 	 */
15350Sstevel@tonic-gate 	mp = so->so_unbind_mp;
15360Sstevel@tonic-gate 	if (mp == NULL) {
15370Sstevel@tonic-gate 		ASSERT(!(so->so_state & SS_ISBOUND));
15380Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
15390Sstevel@tonic-gate 	} else if (!(so->so_state & SS_ISBOUND))  {
15400Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
15410Sstevel@tonic-gate 	} else {
15420Sstevel@tonic-gate 		so->so_unbind_mp = NULL;
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 		/*
15450Sstevel@tonic-gate 		 * Is another T_DISCON_IND being processed.
15460Sstevel@tonic-gate 		 */
15470Sstevel@tonic-gate 		ASSERT((so->so_flag & SOASYNC_UNBIND) == 0);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 		/*
15500Sstevel@tonic-gate 		 * Make strsock_proto ignore T_OK_ACK and T_ERROR_ACK for
15510Sstevel@tonic-gate 		 * this unbind. Set SOASYNC_UNBIND. This should be cleared
15520Sstevel@tonic-gate 		 * only after we receive the ACK in strsock_proto.
15530Sstevel@tonic-gate 		 */
15540Sstevel@tonic-gate 		so->so_flag |= SOASYNC_UNBIND;
15550Sstevel@tonic-gate 		ASSERT(!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)));
15560Sstevel@tonic-gate 		so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN|SS_LADDR_VALID);
15570Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
15580Sstevel@tonic-gate 
15590Sstevel@tonic-gate 		/*
15600Sstevel@tonic-gate 		 * Send down T_UNBIND_REQ ignoring flow control.
15610Sstevel@tonic-gate 		 * XXX Assumes that MSG_IGNFLOW implies that this thread
15620Sstevel@tonic-gate 		 * does not run service procedures.
15630Sstevel@tonic-gate 		 */
15640Sstevel@tonic-gate 		ASSERT(DB_TYPE(mp) == M_PROTO);
15650Sstevel@tonic-gate 		ubr = (struct T_unbind_req *)mp->b_rptr;
15660Sstevel@tonic-gate 		mp->b_wptr += sizeof (*ubr);
15670Sstevel@tonic-gate 		ubr->PRIM_type = T_UNBIND_REQ;
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate 		/*
15700Sstevel@tonic-gate 		 * Flush the read and write side (except stream head read queue)
15710Sstevel@tonic-gate 		 * and send down T_UNBIND_REQ.
15720Sstevel@tonic-gate 		 */
15730Sstevel@tonic-gate 		(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
15740Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
15750Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
15760Sstevel@tonic-gate 		/* LINTED - warning: statement has no consequent: if */
15770Sstevel@tonic-gate 		if (error) {
15780Sstevel@tonic-gate 			eprintsoline(so, error);
15790Sstevel@tonic-gate 		}
15800Sstevel@tonic-gate 	}
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 	if (tpr->discon_ind.DISCON_reason != 0)
15830Sstevel@tonic-gate 		strsetrerror(SOTOV(so), 0, 0, sogetrderr);
15840Sstevel@tonic-gate 	strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
15850Sstevel@tonic-gate 	strseteof(SOTOV(so), 1);
15860Sstevel@tonic-gate 	/*
15870Sstevel@tonic-gate 	 * strseteof takes care of read side wakeups,
15880Sstevel@tonic-gate 	 * pollwakeups, and signals.
15890Sstevel@tonic-gate 	 */
15900Sstevel@tonic-gate 	dprintso(so, 1, ("T_DISCON_IND: error %d\n", so->so_error));
15910Sstevel@tonic-gate 	freemsg(discon_mp);
15920Sstevel@tonic-gate 
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	pollwakeup(&stp->sd_pollist, POLLOUT);
15950Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 	/*
15980Sstevel@tonic-gate 	 * Wake sleeping write
15990Sstevel@tonic-gate 	 */
16000Sstevel@tonic-gate 	if (stp->sd_flag & WSLEEP) {
16010Sstevel@tonic-gate 		stp->sd_flag &= ~WSLEEP;
16020Sstevel@tonic-gate 		cv_broadcast(&stp->sd_wrq->q_wait);
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 
16050Sstevel@tonic-gate 	/*
16060Sstevel@tonic-gate 	 * strsendsig can handle multiple signals with a
16070Sstevel@tonic-gate 	 * single call.  Send SIGPOLL for S_OUTPUT event.
16080Sstevel@tonic-gate 	 */
16090Sstevel@tonic-gate 	if (stp->sd_sigflags & S_OUTPUT)
16100Sstevel@tonic-gate 		strsendsig(stp->sd_siglist, S_OUTPUT, 0, 0);
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
16130Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
16140Sstevel@tonic-gate }
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate /*
16170Sstevel@tonic-gate  * This routine is registered with the stream head to receive M_PROTO
16180Sstevel@tonic-gate  * and M_PCPROTO messages.
16190Sstevel@tonic-gate  *
16200Sstevel@tonic-gate  * Returns NULL if the message was consumed.
16210Sstevel@tonic-gate  * Returns an mblk to make that mblk be processed (and queued) by the stream
16220Sstevel@tonic-gate  * head.
16230Sstevel@tonic-gate  *
16240Sstevel@tonic-gate  * Sets the return parameters (*wakeups, *firstmsgsigs, *allmsgsigs, and
16250Sstevel@tonic-gate  * *pollwakeups) for the stream head to take action on. Note that since
16260Sstevel@tonic-gate  * sockets always deliver SIGIO for every new piece of data this routine
16270Sstevel@tonic-gate  * never sets *firstmsgsigs; any signals are returned in *allmsgsigs.
16280Sstevel@tonic-gate  *
16290Sstevel@tonic-gate  * This routine handles all data related TPI messages independent of
16300Sstevel@tonic-gate  * the type of the socket i.e. it doesn't care if T_UNITDATA_IND message
16310Sstevel@tonic-gate  * arrive on a SOCK_STREAM.
16320Sstevel@tonic-gate  */
16330Sstevel@tonic-gate static mblk_t *
16340Sstevel@tonic-gate strsock_proto(vnode_t *vp, mblk_t *mp,
16350Sstevel@tonic-gate 		strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
16360Sstevel@tonic-gate 		strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
16370Sstevel@tonic-gate {
16380Sstevel@tonic-gate 	union T_primitives *tpr;
16390Sstevel@tonic-gate 	struct sonode *so;
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	so = VTOSO(vp);
16420Sstevel@tonic-gate 
16430Sstevel@tonic-gate 	dprintso(so, 1, ("strsock_proto(%p, %p)\n", vp, mp));
16440Sstevel@tonic-gate 
16450Sstevel@tonic-gate 	/* Set default return values */
16460Sstevel@tonic-gate 	*firstmsgsigs = *wakeups = *allmsgsigs = *pollwakeups = 0;
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_PROTO ||
16490Sstevel@tonic-gate 	    DB_TYPE(mp) == M_PCPROTO);
16500Sstevel@tonic-gate 
16510Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (tpr->type)) {
16520Sstevel@tonic-gate 		/* The message is too short to even contain the primitive */
16531548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
16540Sstevel@tonic-gate 		    "sockfs: Too short TPI message received. Len = %ld\n",
16550Sstevel@tonic-gate 		    (ptrdiff_t)(MBLKL(mp)));
16560Sstevel@tonic-gate 		freemsg(mp);
16570Sstevel@tonic-gate 		return (NULL);
16580Sstevel@tonic-gate 	}
16590Sstevel@tonic-gate 	if (!__TPI_PRIM_ISALIGNED(mp->b_rptr)) {
16600Sstevel@tonic-gate 		/* The read pointer is not aligned correctly for TPI */
16611548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
16620Sstevel@tonic-gate 		    "sockfs: Unaligned TPI message received. rptr = %p\n",
16630Sstevel@tonic-gate 		    (void *)mp->b_rptr);
16640Sstevel@tonic-gate 		freemsg(mp);
16650Sstevel@tonic-gate 		return (NULL);
16660Sstevel@tonic-gate 	}
16670Sstevel@tonic-gate 	tpr = (union T_primitives *)mp->b_rptr;
16680Sstevel@tonic-gate 	dprintso(so, 1, ("strsock_proto: primitive %d\n", tpr->type));
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	switch (tpr->type) {
16710Sstevel@tonic-gate 
16720Sstevel@tonic-gate 	case T_DATA_IND:
16730Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_data_ind)) {
16741548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
16750Sstevel@tonic-gate 			    "sockfs: Too short T_DATA_IND. Len = %ld\n",
16760Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
16770Sstevel@tonic-gate 			freemsg(mp);
16780Sstevel@tonic-gate 			return (NULL);
16790Sstevel@tonic-gate 		}
16800Sstevel@tonic-gate 		/*
16810Sstevel@tonic-gate 		 * Ignore zero-length T_DATA_IND messages. These might be
16820Sstevel@tonic-gate 		 * generated by some transports.
16830Sstevel@tonic-gate 		 * This is needed to prevent read (which skips the M_PROTO
16840Sstevel@tonic-gate 		 * part) to unexpectedly return 0 (or return EWOULDBLOCK
16850Sstevel@tonic-gate 		 * on a non-blocking socket after select/poll has indicated
16860Sstevel@tonic-gate 		 * that data is available).
16870Sstevel@tonic-gate 		 */
16880Sstevel@tonic-gate 		if (msgdsize(mp->b_cont) == 0) {
16890Sstevel@tonic-gate 			dprintso(so, 0,
16900Sstevel@tonic-gate 			    ("strsock_proto: zero length T_DATA_IND\n"));
16910Sstevel@tonic-gate 			freemsg(mp);
16920Sstevel@tonic-gate 			return (NULL);
16930Sstevel@tonic-gate 		}
16940Sstevel@tonic-gate 		*allmsgsigs = S_INPUT | S_RDNORM;
16950Sstevel@tonic-gate 		*pollwakeups = POLLIN | POLLRDNORM;
16960Sstevel@tonic-gate 		*wakeups = RSLEEP;
16970Sstevel@tonic-gate 		return (mp);
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate 	case T_UNITDATA_IND: {
17000Sstevel@tonic-gate 		struct T_unitdata_ind	*tudi = &tpr->unitdata_ind;
17010Sstevel@tonic-gate 		void			*addr;
17020Sstevel@tonic-gate 		t_uscalar_t		addrlen;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_unitdata_ind)) {
17051548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
17060Sstevel@tonic-gate 			    "sockfs: Too short T_UNITDATA_IND. Len = %ld\n",
17070Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
17080Sstevel@tonic-gate 			freemsg(mp);
17090Sstevel@tonic-gate 			return (NULL);
17100Sstevel@tonic-gate 		}
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate 		/* Is this is not a connected datagram socket? */
17130Sstevel@tonic-gate 		if ((so->so_mode & SM_CONNREQUIRED) ||
17140Sstevel@tonic-gate 		    !(so->so_state & SS_ISCONNECTED)) {
17150Sstevel@tonic-gate 			/*
17160Sstevel@tonic-gate 			 * Not a connected datagram socket. Look for
17170Sstevel@tonic-gate 			 * the SO_UNIX_CLOSE option. If such an option is found
17180Sstevel@tonic-gate 			 * discard the message (since it has no meaning
17190Sstevel@tonic-gate 			 * unless connected).
17200Sstevel@tonic-gate 			 */
17210Sstevel@tonic-gate 			if (so->so_family == AF_UNIX && msgdsize(mp) == 0 &&
17220Sstevel@tonic-gate 			    tudi->OPT_length != 0) {
17230Sstevel@tonic-gate 				void *opt;
17240Sstevel@tonic-gate 				t_uscalar_t optlen = tudi->OPT_length;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 				opt = sogetoff(mp, tudi->OPT_offset,
17270Sstevel@tonic-gate 					optlen, __TPI_ALIGN_SIZE);
17280Sstevel@tonic-gate 				if (opt == NULL) {
17290Sstevel@tonic-gate 					/* The len/off falls outside mp */
17300Sstevel@tonic-gate 					freemsg(mp);
17310Sstevel@tonic-gate 					mutex_enter(&so->so_lock);
17320Sstevel@tonic-gate 					soseterror(so, EPROTO);
17330Sstevel@tonic-gate 					mutex_exit(&so->so_lock);
17341548Srshoaib 					zcmn_err(getzoneid(), CE_WARN,
17350Sstevel@tonic-gate 					    "sockfs: T_unidata_ind with "
17360Sstevel@tonic-gate 					    "invalid optlen/offset %u/%d\n",
17370Sstevel@tonic-gate 					    optlen, tudi->OPT_offset);
17380Sstevel@tonic-gate 					return (NULL);
17390Sstevel@tonic-gate 				}
17400Sstevel@tonic-gate 				if (so_getopt_unix_close(opt, optlen)) {
17410Sstevel@tonic-gate 					freemsg(mp);
17420Sstevel@tonic-gate 					return (NULL);
17430Sstevel@tonic-gate 				}
17440Sstevel@tonic-gate 			}
17450Sstevel@tonic-gate 			*allmsgsigs = S_INPUT | S_RDNORM;
17460Sstevel@tonic-gate 			*pollwakeups = POLLIN | POLLRDNORM;
17470Sstevel@tonic-gate 			*wakeups = RSLEEP;
17480Sstevel@tonic-gate #ifdef C2_AUDIT
17490Sstevel@tonic-gate 			if (audit_active)
17500Sstevel@tonic-gate 				audit_sock(T_UNITDATA_IND, strvp2wq(vp),
17510Sstevel@tonic-gate 					mp, 0);
17520Sstevel@tonic-gate #endif /* C2_AUDIT */
17530Sstevel@tonic-gate 			return (mp);
17540Sstevel@tonic-gate 		}
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 		/*
17570Sstevel@tonic-gate 		 * A connect datagram socket. For AF_INET{,6} we verify that
17580Sstevel@tonic-gate 		 * the source address matches the "connected to" address.
17590Sstevel@tonic-gate 		 * The semantics of AF_UNIX sockets is to not verify
17600Sstevel@tonic-gate 		 * the source address.
17610Sstevel@tonic-gate 		 * Note that this source address verification is transport
17620Sstevel@tonic-gate 		 * specific. Thus the real fix would be to extent TPI
17630Sstevel@tonic-gate 		 * to allow T_CONN_REQ messages to be send to connectionless
17640Sstevel@tonic-gate 		 * transport providers and always let the transport provider
17650Sstevel@tonic-gate 		 * do whatever filtering is needed.
17660Sstevel@tonic-gate 		 *
17670Sstevel@tonic-gate 		 * The verification/filtering semantics for transports
17680Sstevel@tonic-gate 		 * other than AF_INET and AF_UNIX are unknown. The choice
17690Sstevel@tonic-gate 		 * would be to either filter using bcmp or let all messages
17700Sstevel@tonic-gate 		 * get through. This code does not filter other address
17710Sstevel@tonic-gate 		 * families since this at least allows the application to
17720Sstevel@tonic-gate 		 * work around any missing filtering.
17730Sstevel@tonic-gate 		 *
17740Sstevel@tonic-gate 		 * XXX Should we move filtering to UDP/ICMP???
17750Sstevel@tonic-gate 		 * That would require passing e.g. a T_DISCON_REQ to UDP
17760Sstevel@tonic-gate 		 * when the socket becomes unconnected.
17770Sstevel@tonic-gate 		 */
17780Sstevel@tonic-gate 		addrlen = tudi->SRC_length;
17790Sstevel@tonic-gate 		/*
17800Sstevel@tonic-gate 		 * The alignment restriction is really to strict but
17810Sstevel@tonic-gate 		 * we want enough alignment to inspect the fields of
17820Sstevel@tonic-gate 		 * a sockaddr_in.
17830Sstevel@tonic-gate 		 */
17840Sstevel@tonic-gate 		addr = sogetoff(mp, tudi->SRC_offset, addrlen,
17850Sstevel@tonic-gate 				__TPI_ALIGN_SIZE);
17860Sstevel@tonic-gate 		if (addr == NULL) {
17870Sstevel@tonic-gate 			freemsg(mp);
17880Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
17890Sstevel@tonic-gate 			soseterror(so, EPROTO);
17900Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
17911548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
17920Sstevel@tonic-gate 			    "sockfs: T_unidata_ind with invalid "
17930Sstevel@tonic-gate 			    "addrlen/offset %u/%d\n",
17940Sstevel@tonic-gate 			    addrlen, tudi->SRC_offset);
17950Sstevel@tonic-gate 			return (NULL);
17960Sstevel@tonic-gate 		}
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		if (so->so_family == AF_INET) {
17990Sstevel@tonic-gate 			/*
18000Sstevel@tonic-gate 			 * For AF_INET we allow wildcarding both sin_addr
18010Sstevel@tonic-gate 			 * and sin_port.
18020Sstevel@tonic-gate 			 */
18030Sstevel@tonic-gate 			struct sockaddr_in *faddr, *sin;
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 			/* Prevent so_faddr_sa from changing while accessed */
18060Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
18070Sstevel@tonic-gate 			ASSERT(so->so_faddr_len ==
18080Sstevel@tonic-gate 				(socklen_t)sizeof (struct sockaddr_in));
18090Sstevel@tonic-gate 			faddr = (struct sockaddr_in *)so->so_faddr_sa;
18100Sstevel@tonic-gate 			sin = (struct sockaddr_in *)addr;
18110Sstevel@tonic-gate 			if (addrlen !=
18120Sstevel@tonic-gate 				(t_uscalar_t)sizeof (struct sockaddr_in) ||
18130Sstevel@tonic-gate 			    (sin->sin_addr.s_addr != faddr->sin_addr.s_addr &&
18140Sstevel@tonic-gate 			    faddr->sin_addr.s_addr != INADDR_ANY) ||
18150Sstevel@tonic-gate 			    (so->so_type != SOCK_RAW &&
18160Sstevel@tonic-gate 			    sin->sin_port != faddr->sin_port &&
18170Sstevel@tonic-gate 			    faddr->sin_port != 0)) {
18180Sstevel@tonic-gate #ifdef DEBUG
18190Sstevel@tonic-gate 				dprintso(so, 0,
18200Sstevel@tonic-gate 					("sockfs: T_UNITDATA_IND mismatch: %s",
18210Sstevel@tonic-gate 					pr_addr(so->so_family,
18220Sstevel@tonic-gate 						(struct sockaddr *)addr,
18230Sstevel@tonic-gate 						addrlen)));
18240Sstevel@tonic-gate 				dprintso(so, 0, (" - %s\n",
18250Sstevel@tonic-gate 					pr_addr(so->so_family, so->so_faddr_sa,
18260Sstevel@tonic-gate 					    (t_uscalar_t)so->so_faddr_len)));
18270Sstevel@tonic-gate #endif /* DEBUG */
18280Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
18290Sstevel@tonic-gate 				freemsg(mp);
18300Sstevel@tonic-gate 				return (NULL);
18310Sstevel@tonic-gate 			}
18320Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
18330Sstevel@tonic-gate 		} else if (so->so_family == AF_INET6) {
18340Sstevel@tonic-gate 			/*
18350Sstevel@tonic-gate 			 * For AF_INET6 we allow wildcarding both sin6_addr
18360Sstevel@tonic-gate 			 * and sin6_port.
18370Sstevel@tonic-gate 			 */
18380Sstevel@tonic-gate 			struct sockaddr_in6 *faddr6, *sin6;
18390Sstevel@tonic-gate 			static struct in6_addr zeroes; /* inits to all zeros */
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate 			/* Prevent so_faddr_sa from changing while accessed */
18420Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
18430Sstevel@tonic-gate 			ASSERT(so->so_faddr_len ==
18440Sstevel@tonic-gate 			    (socklen_t)sizeof (struct sockaddr_in6));
18450Sstevel@tonic-gate 			faddr6 = (struct sockaddr_in6 *)so->so_faddr_sa;
18460Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addr;
18470Sstevel@tonic-gate 			/* XXX could we get a mapped address ::ffff:0.0.0.0 ? */
18480Sstevel@tonic-gate 			if (addrlen !=
18490Sstevel@tonic-gate 			    (t_uscalar_t)sizeof (struct sockaddr_in6) ||
18500Sstevel@tonic-gate 			    (!IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
18510Sstevel@tonic-gate 				&faddr6->sin6_addr) &&
18520Sstevel@tonic-gate 			    !IN6_ARE_ADDR_EQUAL(&faddr6->sin6_addr, &zeroes)) ||
18530Sstevel@tonic-gate 			    (so->so_type != SOCK_RAW &&
18540Sstevel@tonic-gate 			    sin6->sin6_port != faddr6->sin6_port &&
18550Sstevel@tonic-gate 			    faddr6->sin6_port != 0)) {
18560Sstevel@tonic-gate #ifdef DEBUG
18570Sstevel@tonic-gate 				dprintso(so, 0,
18580Sstevel@tonic-gate 				    ("sockfs: T_UNITDATA_IND mismatch: %s",
18590Sstevel@tonic-gate 					pr_addr(so->so_family,
18600Sstevel@tonic-gate 					    (struct sockaddr *)addr,
18610Sstevel@tonic-gate 					    addrlen)));
18620Sstevel@tonic-gate 				dprintso(so, 0, (" - %s\n",
18630Sstevel@tonic-gate 				    pr_addr(so->so_family, so->so_faddr_sa,
18640Sstevel@tonic-gate 					(t_uscalar_t)so->so_faddr_len)));
18650Sstevel@tonic-gate #endif /* DEBUG */
18660Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
18670Sstevel@tonic-gate 				freemsg(mp);
18680Sstevel@tonic-gate 				return (NULL);
18690Sstevel@tonic-gate 			}
18700Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
18710Sstevel@tonic-gate 		} else if (so->so_family == AF_UNIX &&
18720Sstevel@tonic-gate 		    msgdsize(mp->b_cont) == 0 &&
18730Sstevel@tonic-gate 		    tudi->OPT_length != 0) {
18740Sstevel@tonic-gate 			/*
18750Sstevel@tonic-gate 			 * Attempt to extract AF_UNIX
18760Sstevel@tonic-gate 			 * SO_UNIX_CLOSE indication from options.
18770Sstevel@tonic-gate 			 */
18780Sstevel@tonic-gate 			void *opt;
18790Sstevel@tonic-gate 			t_uscalar_t optlen = tudi->OPT_length;
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 			opt = sogetoff(mp, tudi->OPT_offset,
18820Sstevel@tonic-gate 				optlen, __TPI_ALIGN_SIZE);
18830Sstevel@tonic-gate 			if (opt == NULL) {
18840Sstevel@tonic-gate 				/* The len/off falls outside mp */
18850Sstevel@tonic-gate 				freemsg(mp);
18860Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
18870Sstevel@tonic-gate 				soseterror(so, EPROTO);
18880Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
18891548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
18900Sstevel@tonic-gate 				    "sockfs: T_unidata_ind with invalid "
18910Sstevel@tonic-gate 				    "optlen/offset %u/%d\n",
18920Sstevel@tonic-gate 				    optlen, tudi->OPT_offset);
18930Sstevel@tonic-gate 				return (NULL);
18940Sstevel@tonic-gate 			}
18950Sstevel@tonic-gate 			/*
18960Sstevel@tonic-gate 			 * If we received a unix close indication mark the
18970Sstevel@tonic-gate 			 * socket and discard this message.
18980Sstevel@tonic-gate 			 */
18990Sstevel@tonic-gate 			if (so_getopt_unix_close(opt, optlen)) {
19000Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
19010Sstevel@tonic-gate 				sobreakconn(so, ECONNRESET);
19020Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
19030Sstevel@tonic-gate 				strsetrerror(SOTOV(so), 0, 0, sogetrderr);
19040Sstevel@tonic-gate 				freemsg(mp);
19050Sstevel@tonic-gate 				*pollwakeups = POLLIN | POLLRDNORM;
19060Sstevel@tonic-gate 				*allmsgsigs = S_INPUT | S_RDNORM;
19070Sstevel@tonic-gate 				*wakeups = RSLEEP;
19080Sstevel@tonic-gate 				return (NULL);
19090Sstevel@tonic-gate 			}
19100Sstevel@tonic-gate 		}
19110Sstevel@tonic-gate 		*allmsgsigs = S_INPUT | S_RDNORM;
19120Sstevel@tonic-gate 		*pollwakeups = POLLIN | POLLRDNORM;
19130Sstevel@tonic-gate 		*wakeups = RSLEEP;
19140Sstevel@tonic-gate 		return (mp);
19150Sstevel@tonic-gate 	}
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	case T_OPTDATA_IND: {
19180Sstevel@tonic-gate 		struct T_optdata_ind	*tdi = &tpr->optdata_ind;
19190Sstevel@tonic-gate 
19200Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_optdata_ind)) {
19211548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
19220Sstevel@tonic-gate 			    "sockfs: Too short T_OPTDATA_IND. Len = %ld\n",
19230Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
19240Sstevel@tonic-gate 			freemsg(mp);
19250Sstevel@tonic-gate 			return (NULL);
19260Sstevel@tonic-gate 		}
19270Sstevel@tonic-gate 		/*
19280Sstevel@tonic-gate 		 * Allow zero-length messages carrying options.
19290Sstevel@tonic-gate 		 * This is used when carrying the SO_UNIX_CLOSE option.
19300Sstevel@tonic-gate 		 */
19310Sstevel@tonic-gate 		if (so->so_family == AF_UNIX && msgdsize(mp->b_cont) == 0 &&
19320Sstevel@tonic-gate 		    tdi->OPT_length != 0) {
19330Sstevel@tonic-gate 			/*
19340Sstevel@tonic-gate 			 * Attempt to extract AF_UNIX close indication
19350Sstevel@tonic-gate 			 * from the options. Ignore any other options -
19360Sstevel@tonic-gate 			 * those are handled once the message is removed
19370Sstevel@tonic-gate 			 * from the queue.
19380Sstevel@tonic-gate 			 * The close indication message should not carry data.
19390Sstevel@tonic-gate 			 */
19400Sstevel@tonic-gate 			void *opt;
19410Sstevel@tonic-gate 			t_uscalar_t optlen = tdi->OPT_length;
19420Sstevel@tonic-gate 
19430Sstevel@tonic-gate 			opt = sogetoff(mp, tdi->OPT_offset,
19440Sstevel@tonic-gate 				optlen, __TPI_ALIGN_SIZE);
19450Sstevel@tonic-gate 			if (opt == NULL) {
19460Sstevel@tonic-gate 				/* The len/off falls outside mp */
19470Sstevel@tonic-gate 				freemsg(mp);
19480Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
19490Sstevel@tonic-gate 				soseterror(so, EPROTO);
19500Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
19511548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
19520Sstevel@tonic-gate 				    "sockfs: T_optdata_ind with invalid "
19530Sstevel@tonic-gate 				    "optlen/offset %u/%d\n",
19540Sstevel@tonic-gate 				    optlen, tdi->OPT_offset);
19550Sstevel@tonic-gate 				return (NULL);
19560Sstevel@tonic-gate 			}
19570Sstevel@tonic-gate 			/*
19580Sstevel@tonic-gate 			 * If we received a close indication mark the
19590Sstevel@tonic-gate 			 * socket and discard this message.
19600Sstevel@tonic-gate 			 */
19610Sstevel@tonic-gate 			if (so_getopt_unix_close(opt, optlen)) {
19620Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
19630Sstevel@tonic-gate 				socantsendmore(so);
19640Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
19650Sstevel@tonic-gate 				strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
19660Sstevel@tonic-gate 				freemsg(mp);
19670Sstevel@tonic-gate 				return (NULL);
19680Sstevel@tonic-gate 			}
19690Sstevel@tonic-gate 		}
19700Sstevel@tonic-gate 		*allmsgsigs = S_INPUT | S_RDNORM;
19710Sstevel@tonic-gate 		*pollwakeups = POLLIN | POLLRDNORM;
19720Sstevel@tonic-gate 		*wakeups = RSLEEP;
19730Sstevel@tonic-gate 		return (mp);
19740Sstevel@tonic-gate 	}
19750Sstevel@tonic-gate 
19760Sstevel@tonic-gate 	case T_EXDATA_IND: {
19770Sstevel@tonic-gate 		mblk_t		*mctl, *mdata;
1978*2933Sss146032 		mblk_t *lbp;
1979*2933Sss146032 		union T_primitives *tprp;
1980*2933Sss146032 		struct stdata   *stp;
1981*2933Sss146032 		queue_t *qp;
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_exdata_ind)) {
19841548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
19850Sstevel@tonic-gate 			    "sockfs: Too short T_EXDATA_IND. Len = %ld\n",
19860Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
19870Sstevel@tonic-gate 			freemsg(mp);
19880Sstevel@tonic-gate 			return (NULL);
19890Sstevel@tonic-gate 		}
19900Sstevel@tonic-gate 		/*
19910Sstevel@tonic-gate 		 * Ignore zero-length T_EXDATA_IND messages. These might be
19920Sstevel@tonic-gate 		 * generated by some transports.
19930Sstevel@tonic-gate 		 *
19940Sstevel@tonic-gate 		 * This is needed to prevent read (which skips the M_PROTO
19950Sstevel@tonic-gate 		 * part) to unexpectedly return 0 (or return EWOULDBLOCK
19960Sstevel@tonic-gate 		 * on a non-blocking socket after select/poll has indicated
19970Sstevel@tonic-gate 		 * that data is available).
19980Sstevel@tonic-gate 		 */
19990Sstevel@tonic-gate 		dprintso(so, 1,
20000Sstevel@tonic-gate 			("T_EXDATA_IND(%p): counts %d/%d state %s\n",
20010Sstevel@tonic-gate 			vp, so->so_oobsigcnt, so->so_oobcnt,
20020Sstevel@tonic-gate 			pr_state(so->so_state, so->so_mode)));
20030Sstevel@tonic-gate 
20040Sstevel@tonic-gate 		if (msgdsize(mp->b_cont) == 0) {
20050Sstevel@tonic-gate 			dprintso(so, 0,
20060Sstevel@tonic-gate 				("strsock_proto: zero length T_EXDATA_IND\n"));
20070Sstevel@tonic-gate 			freemsg(mp);
20080Sstevel@tonic-gate 			return (NULL);
20090Sstevel@tonic-gate 		}
20100Sstevel@tonic-gate 
20110Sstevel@tonic-gate 		/*
20120Sstevel@tonic-gate 		 * Split into the T_EXDATA_IND and the M_DATA part.
20130Sstevel@tonic-gate 		 * We process these three pieces separately:
20140Sstevel@tonic-gate 		 *	signal generation
20150Sstevel@tonic-gate 		 *	handling T_EXDATA_IND
20160Sstevel@tonic-gate 		 *	handling M_DATA component
20170Sstevel@tonic-gate 		 */
20180Sstevel@tonic-gate 		mctl = mp;
20190Sstevel@tonic-gate 		mdata = mctl->b_cont;
20200Sstevel@tonic-gate 		mctl->b_cont = NULL;
20210Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
20220Sstevel@tonic-gate 		so_oob_sig(so, 0, allmsgsigs, pollwakeups);
20230Sstevel@tonic-gate 		mctl = so_oob_exdata(so, mctl, allmsgsigs, pollwakeups);
20240Sstevel@tonic-gate 		mdata = so_oob_data(so, mdata, allmsgsigs, pollwakeups);
20250Sstevel@tonic-gate 
2026*2933Sss146032 		stp = vp->v_stream;
2027*2933Sss146032 		ASSERT(stp != NULL);
2028*2933Sss146032 		qp = _RD(stp->sd_wrq);
2029*2933Sss146032 
2030*2933Sss146032 		mutex_enter(QLOCK(qp));
2031*2933Sss146032 		lbp = qp->q_last;
2032*2933Sss146032 
2033*2933Sss146032 		/*
2034*2933Sss146032 		 * We want to avoid queueing up a string of T_EXDATA_IND
2035*2933Sss146032 		 * messages with no intervening data messages at the stream
2036*2933Sss146032 		 * head. These messages contribute to the total message
2037*2933Sss146032 		 * count. Eventually this can lead to STREAMS flow contol
2038*2933Sss146032 		 * and also cause TCP to advertise a zero window condition
2039*2933Sss146032 		 * to the peer. This can happen in the degenerate case where
2040*2933Sss146032 		 * the sender and receiver exchange only OOB data. The sender
2041*2933Sss146032 		 * only sends messages with MSG_OOB flag and the receiver
2042*2933Sss146032 		 * receives only MSG_OOB messages and does not use SO_OOBINLINE.
2043*2933Sss146032 		 * An example of this scenario has been reported in applications
2044*2933Sss146032 		 * that use OOB data to exchange heart beats. Flow control
2045*2933Sss146032 		 * relief will never happen if the application only reads OOB
2046*2933Sss146032 		 * data which is done directly by sorecvoob() and the
2047*2933Sss146032 		 * T_EXDATA_IND messages at the streamhead won't be consumed.
2048*2933Sss146032 		 * Note that there is no correctness issue in compressing the
2049*2933Sss146032 		 * string of T_EXDATA_IND messages into a single T_EXDATA_IND
2050*2933Sss146032 		 * message. A single read that does not specify MSG_OOB will
2051*2933Sss146032 		 * read across all the marks in a loop in sotpi_recvmsg().
2052*2933Sss146032 		 * Each mark is individually distinguishable only if the
2053*2933Sss146032 		 * T_EXDATA_IND messages are separated by data messages.
2054*2933Sss146032 		 */
2055*2933Sss146032 		if ((qp->q_first != NULL) && (DB_TYPE(lbp) == M_PROTO)) {
2056*2933Sss146032 			tprp = (union T_primitives *)lbp->b_rptr;
2057*2933Sss146032 			if ((tprp->type == T_EXDATA_IND) &&
2058*2933Sss146032 			    !(so->so_options & SO_OOBINLINE)) {
2059*2933Sss146032 
2060*2933Sss146032 				/*
2061*2933Sss146032 				 * free the new M_PROTO message
2062*2933Sss146032 				 */
2063*2933Sss146032 				freemsg(mctl);
2064*2933Sss146032 
2065*2933Sss146032 				/*
2066*2933Sss146032 				 * adjust the OOB count and OOB	signal count
2067*2933Sss146032 				 * just incremented for the new OOB data.
2068*2933Sss146032 				 */
2069*2933Sss146032 				so->so_oobcnt--;
2070*2933Sss146032 				so->so_oobsigcnt--;
2071*2933Sss146032 				mutex_exit(QLOCK(qp));
2072*2933Sss146032 				mutex_exit(&so->so_lock);
2073*2933Sss146032 				return (NULL);
2074*2933Sss146032 			}
2075*2933Sss146032 		}
2076*2933Sss146032 		mutex_exit(QLOCK(qp));
2077*2933Sss146032 
20780Sstevel@tonic-gate 		/*
20790Sstevel@tonic-gate 		 * Pass the T_EXDATA_IND and the M_DATA back separately
20800Sstevel@tonic-gate 		 * by using b_next linkage. (The stream head will queue any
20810Sstevel@tonic-gate 		 * b_next linked messages separately.) This is needed
20820Sstevel@tonic-gate 		 * since MSGMARK applies to the last by of the message
20830Sstevel@tonic-gate 		 * hence we can not have any M_DATA component attached
20840Sstevel@tonic-gate 		 * to the marked T_EXDATA_IND. Note that the stream head
20850Sstevel@tonic-gate 		 * will not consolidate M_DATA messages onto an MSGMARK'ed
20860Sstevel@tonic-gate 		 * message in order to preserve the constraint that
20870Sstevel@tonic-gate 		 * the T_EXDATA_IND always is a separate message.
20880Sstevel@tonic-gate 		 */
20890Sstevel@tonic-gate 		ASSERT(mctl != NULL);
20900Sstevel@tonic-gate 		mctl->b_next = mdata;
20910Sstevel@tonic-gate 		mp = mctl;
20920Sstevel@tonic-gate #ifdef DEBUG
20930Sstevel@tonic-gate 		if (mdata == NULL) {
20940Sstevel@tonic-gate 			dprintso(so, 1,
20950Sstevel@tonic-gate 				("after outofline T_EXDATA_IND(%p): "
20960Sstevel@tonic-gate 				"counts %d/%d  poll 0x%x sig 0x%x state %s\n",
20970Sstevel@tonic-gate 				vp, so->so_oobsigcnt,
20980Sstevel@tonic-gate 				so->so_oobcnt, *pollwakeups, *allmsgsigs,
20990Sstevel@tonic-gate 				pr_state(so->so_state, so->so_mode)));
21000Sstevel@tonic-gate 		} else {
21010Sstevel@tonic-gate 			dprintso(so, 1,
21020Sstevel@tonic-gate 				("after inline T_EXDATA_IND(%p): "
21030Sstevel@tonic-gate 				"counts %d/%d  poll 0x%x sig 0x%x state %s\n",
21040Sstevel@tonic-gate 				vp, so->so_oobsigcnt,
21050Sstevel@tonic-gate 				so->so_oobcnt, *pollwakeups, *allmsgsigs,
21060Sstevel@tonic-gate 				pr_state(so->so_state, so->so_mode)));
21070Sstevel@tonic-gate 		}
21080Sstevel@tonic-gate #endif /* DEBUG */
21090Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
21100Sstevel@tonic-gate 		*wakeups = RSLEEP;
21110Sstevel@tonic-gate 		return (mp);
21120Sstevel@tonic-gate 	}
21130Sstevel@tonic-gate 
21140Sstevel@tonic-gate 	case T_CONN_CON: {
21150Sstevel@tonic-gate 		struct T_conn_con	*conn_con;
21160Sstevel@tonic-gate 		void			*addr;
21170Sstevel@tonic-gate 		t_uscalar_t		addrlen;
21180Sstevel@tonic-gate 
21190Sstevel@tonic-gate 		/*
21200Sstevel@tonic-gate 		 * Verify the state, update the state to ISCONNECTED,
21210Sstevel@tonic-gate 		 * record the potentially new address in the message,
21220Sstevel@tonic-gate 		 * and drop the message.
21230Sstevel@tonic-gate 		 */
21240Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_conn_con)) {
21251548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
21260Sstevel@tonic-gate 			    "sockfs: Too short T_CONN_CON. Len = %ld\n",
21270Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
21280Sstevel@tonic-gate 			freemsg(mp);
21290Sstevel@tonic-gate 			return (NULL);
21300Sstevel@tonic-gate 		}
21310Sstevel@tonic-gate 
21320Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
21330Sstevel@tonic-gate 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) !=
21340Sstevel@tonic-gate 		    SS_ISCONNECTING) {
21350Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
21360Sstevel@tonic-gate 			dprintso(so, 1,
21370Sstevel@tonic-gate 				("T_CONN_CON: state %x\n", so->so_state));
21380Sstevel@tonic-gate 			freemsg(mp);
21390Sstevel@tonic-gate 			return (NULL);
21400Sstevel@tonic-gate 		}
21410Sstevel@tonic-gate 
21420Sstevel@tonic-gate 		conn_con = &tpr->conn_con;
21430Sstevel@tonic-gate 		addrlen = conn_con->RES_length;
21440Sstevel@tonic-gate 		/*
21450Sstevel@tonic-gate 		 * Allow the address to be of different size than sent down
21460Sstevel@tonic-gate 		 * in the T_CONN_REQ as long as it doesn't exceed the maxlen.
21470Sstevel@tonic-gate 		 * For AF_UNIX require the identical length.
21480Sstevel@tonic-gate 		 */
21490Sstevel@tonic-gate 		if (so->so_family == AF_UNIX ?
21500Sstevel@tonic-gate 		    addrlen != (t_uscalar_t)sizeof (so->so_ux_laddr) :
21510Sstevel@tonic-gate 		    addrlen > (t_uscalar_t)so->so_faddr_maxlen) {
21521548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
21530Sstevel@tonic-gate 			    "sockfs: T_conn_con with different "
21540Sstevel@tonic-gate 			    "length %u/%d\n",
21550Sstevel@tonic-gate 			    addrlen, conn_con->RES_length);
21560Sstevel@tonic-gate 			soisdisconnected(so, EPROTO);
21570Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
21580Sstevel@tonic-gate 			strsetrerror(SOTOV(so), 0, 0, sogetrderr);
21590Sstevel@tonic-gate 			strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
21600Sstevel@tonic-gate 			strseteof(SOTOV(so), 1);
21610Sstevel@tonic-gate 			freemsg(mp);
21620Sstevel@tonic-gate 			/*
21630Sstevel@tonic-gate 			 * strseteof takes care of read side wakeups,
21640Sstevel@tonic-gate 			 * pollwakeups, and signals.
21650Sstevel@tonic-gate 			 */
21660Sstevel@tonic-gate 			*wakeups = WSLEEP;
21670Sstevel@tonic-gate 			*allmsgsigs = S_OUTPUT;
21680Sstevel@tonic-gate 			*pollwakeups = POLLOUT;
21690Sstevel@tonic-gate 			return (NULL);
21700Sstevel@tonic-gate 		}
21710Sstevel@tonic-gate 		addr = sogetoff(mp, conn_con->RES_offset, addrlen, 1);
21720Sstevel@tonic-gate 		if (addr == NULL) {
21731548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
21740Sstevel@tonic-gate 			    "sockfs: T_conn_con with invalid "
21750Sstevel@tonic-gate 			    "addrlen/offset %u/%d\n",
21760Sstevel@tonic-gate 			    addrlen, conn_con->RES_offset);
21770Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
21780Sstevel@tonic-gate 			strsetrerror(SOTOV(so), 0, 0, sogetrderr);
21790Sstevel@tonic-gate 			strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
21800Sstevel@tonic-gate 			strseteof(SOTOV(so), 1);
21810Sstevel@tonic-gate 			freemsg(mp);
21820Sstevel@tonic-gate 			/*
21830Sstevel@tonic-gate 			 * strseteof takes care of read side wakeups,
21840Sstevel@tonic-gate 			 * pollwakeups, and signals.
21850Sstevel@tonic-gate 			 */
21860Sstevel@tonic-gate 			*wakeups = WSLEEP;
21870Sstevel@tonic-gate 			*allmsgsigs = S_OUTPUT;
21880Sstevel@tonic-gate 			*pollwakeups = POLLOUT;
21890Sstevel@tonic-gate 			return (NULL);
21900Sstevel@tonic-gate 		}
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 		/*
21930Sstevel@tonic-gate 		 * Save for getpeername.
21940Sstevel@tonic-gate 		 */
21950Sstevel@tonic-gate 		if (so->so_family != AF_UNIX) {
21960Sstevel@tonic-gate 			so->so_faddr_len = (socklen_t)addrlen;
21970Sstevel@tonic-gate 			ASSERT(so->so_faddr_len <= so->so_faddr_maxlen);
21980Sstevel@tonic-gate 			bcopy(addr, so->so_faddr_sa, addrlen);
21990Sstevel@tonic-gate 			so->so_state |= SS_FADDR_VALID;
22000Sstevel@tonic-gate 		}
22010Sstevel@tonic-gate 
22020Sstevel@tonic-gate 		if (so->so_peercred != NULL)
22030Sstevel@tonic-gate 			crfree(so->so_peercred);
22040Sstevel@tonic-gate 		so->so_peercred = DB_CRED(mp);
22050Sstevel@tonic-gate 		so->so_cpid = DB_CPID(mp);
22060Sstevel@tonic-gate 		if (so->so_peercred != NULL)
22070Sstevel@tonic-gate 			crhold(so->so_peercred);
22080Sstevel@tonic-gate 
22090Sstevel@tonic-gate 		/* Wakeup anybody sleeping in sowaitconnected */
22100Sstevel@tonic-gate 		soisconnected(so);
22110Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 		/*
22140Sstevel@tonic-gate 		 * The socket is now available for sending data.
22150Sstevel@tonic-gate 		 */
22160Sstevel@tonic-gate 		*wakeups = WSLEEP;
22170Sstevel@tonic-gate 		*allmsgsigs = S_OUTPUT;
22180Sstevel@tonic-gate 		*pollwakeups = POLLOUT;
22190Sstevel@tonic-gate 		freemsg(mp);
22200Sstevel@tonic-gate 		return (NULL);
22210Sstevel@tonic-gate 	}
22220Sstevel@tonic-gate 
2223898Skais 	/*
2224898Skais 	 * Extra processing in case of an SSL proxy, before queuing or
2225898Skais 	 * forwarding to the fallback endpoint
2226898Skais 	 */
2227898Skais 	case T_SSL_PROXY_CONN_IND:
22280Sstevel@tonic-gate 	case T_CONN_IND:
22290Sstevel@tonic-gate 		/*
22300Sstevel@tonic-gate 		 * Verify the min size and queue the message on
22310Sstevel@tonic-gate 		 * the so_conn_ind_head/tail list.
22320Sstevel@tonic-gate 		 */
22330Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_conn_ind)) {
22341548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
22350Sstevel@tonic-gate 			    "sockfs: Too short T_CONN_IND. Len = %ld\n",
22360Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
22370Sstevel@tonic-gate 			freemsg(mp);
22380Sstevel@tonic-gate 			return (NULL);
22390Sstevel@tonic-gate 		}
22400Sstevel@tonic-gate 
22410Sstevel@tonic-gate #ifdef C2_AUDIT
22420Sstevel@tonic-gate 		if (audit_active)
22430Sstevel@tonic-gate 			audit_sock(T_CONN_IND, strvp2wq(vp), mp, 0);
22440Sstevel@tonic-gate #endif /* C2_AUDIT */
22450Sstevel@tonic-gate 		if (!(so->so_state & SS_ACCEPTCONN)) {
22461548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
22470Sstevel@tonic-gate 			    "sockfs: T_conn_ind on non-listening socket\n");
22480Sstevel@tonic-gate 			freemsg(mp);
22490Sstevel@tonic-gate 			return (NULL);
22500Sstevel@tonic-gate 		}
2251898Skais 
2252898Skais 		if (tpr->type == T_SSL_PROXY_CONN_IND && mp->b_cont == NULL) {
2253898Skais 			/* No context: need to fall back */
2254898Skais 			struct sonode *fbso;
2255898Skais 			stdata_t *fbstp;
2256898Skais 
2257898Skais 			tpr->type = T_CONN_IND;
2258898Skais 
2259898Skais 			fbso = kssl_find_fallback(so->so_kssl_ent);
2260898Skais 
2261898Skais 			/*
2262898Skais 			 * No fallback: the remote will timeout and
2263898Skais 			 * disconnect.
2264898Skais 			 */
2265898Skais 			if (fbso == NULL) {
2266898Skais 				freemsg(mp);
2267898Skais 				return (NULL);
2268898Skais 			}
2269898Skais 			fbstp = SOTOV(fbso)->v_stream;
2270898Skais 			qreply(fbstp->sd_wrq->q_next, mp);
2271898Skais 			return (NULL);
2272898Skais 		}
22730Sstevel@tonic-gate 		soqueueconnind(so, mp);
22740Sstevel@tonic-gate 		*allmsgsigs = S_INPUT | S_RDNORM;
22750Sstevel@tonic-gate 		*pollwakeups = POLLIN | POLLRDNORM;
22760Sstevel@tonic-gate 		*wakeups = RSLEEP;
22770Sstevel@tonic-gate 		return (NULL);
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate 	case T_ORDREL_IND:
22800Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_ordrel_ind)) {
22811548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
22820Sstevel@tonic-gate 			    "sockfs: Too short T_ORDREL_IND. Len = %ld\n",
22830Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
22840Sstevel@tonic-gate 			freemsg(mp);
22850Sstevel@tonic-gate 			return (NULL);
22860Sstevel@tonic-gate 		}
22870Sstevel@tonic-gate 
22880Sstevel@tonic-gate 		/*
22890Sstevel@tonic-gate 		 * Some providers send this when not fully connected.
22900Sstevel@tonic-gate 		 * SunLink X.25 needs to retrieve disconnect reason after
22910Sstevel@tonic-gate 		 * disconnect for compatibility. It uses T_ORDREL_IND
22920Sstevel@tonic-gate 		 * instead of T_DISCON_IND so that it may use the
22930Sstevel@tonic-gate 		 * endpoint after a connect failure to retrieve the
22940Sstevel@tonic-gate 		 * reason using an ioctl. Thus we explicitly clear
22950Sstevel@tonic-gate 		 * SS_ISCONNECTING here for SunLink X.25.
22960Sstevel@tonic-gate 		 * This is a needed TPI violation.
22970Sstevel@tonic-gate 		 */
22980Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
22990Sstevel@tonic-gate 		so->so_state &= ~SS_ISCONNECTING;
23000Sstevel@tonic-gate 		socantrcvmore(so);
23010Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
23020Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
23030Sstevel@tonic-gate 		/*
23040Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
23050Sstevel@tonic-gate 		 * pollwakeups, and signals.
23060Sstevel@tonic-gate 		 */
23070Sstevel@tonic-gate 		freemsg(mp);
23080Sstevel@tonic-gate 		return (NULL);
23090Sstevel@tonic-gate 
23100Sstevel@tonic-gate 	case T_DISCON_IND:
23110Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_discon_ind)) {
23121548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
23130Sstevel@tonic-gate 			    "sockfs: Too short T_DISCON_IND. Len = %ld\n",
23140Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
23150Sstevel@tonic-gate 			freemsg(mp);
23160Sstevel@tonic-gate 			return (NULL);
23170Sstevel@tonic-gate 		}
23180Sstevel@tonic-gate 		if (so->so_state & SS_ACCEPTCONN) {
23190Sstevel@tonic-gate 			/*
23200Sstevel@tonic-gate 			 * This is a listener. Look for a queued T_CONN_IND
23210Sstevel@tonic-gate 			 * with a matching sequence number and remove it
23220Sstevel@tonic-gate 			 * from the list.
23230Sstevel@tonic-gate 			 * It is normal to not find the sequence number since
23240Sstevel@tonic-gate 			 * the soaccept might have already dequeued it
23250Sstevel@tonic-gate 			 * (in which case the T_CONN_RES will fail with
23260Sstevel@tonic-gate 			 * TBADSEQ).
23270Sstevel@tonic-gate 			 */
23280Sstevel@tonic-gate 			(void) soflushconnind(so, tpr->discon_ind.SEQ_number);
23290Sstevel@tonic-gate 			freemsg(mp);
23300Sstevel@tonic-gate 			return (0);
23310Sstevel@tonic-gate 		}
23320Sstevel@tonic-gate 
23330Sstevel@tonic-gate 		/*
23340Sstevel@tonic-gate 		 * Not a listener
23350Sstevel@tonic-gate 		 *
23360Sstevel@tonic-gate 		 * If SS_CANTRCVMORE for AF_UNIX ignore the discon_reason.
23370Sstevel@tonic-gate 		 * Such a discon_ind appears when the peer has first done
23380Sstevel@tonic-gate 		 * a shutdown() followed by a close() in which case we just
23390Sstevel@tonic-gate 		 * want to record socantsendmore.
23400Sstevel@tonic-gate 		 * In this case sockfs first receives a T_ORDREL_IND followed
23410Sstevel@tonic-gate 		 * by a T_DISCON_IND.
23420Sstevel@tonic-gate 		 * Note that for other transports (e.g. TCP) we need to handle
23430Sstevel@tonic-gate 		 * the discon_ind in this case since it signals an error.
23440Sstevel@tonic-gate 		 */
23450Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
23460Sstevel@tonic-gate 		if ((so->so_state & SS_CANTRCVMORE) &&
23470Sstevel@tonic-gate 		    (so->so_family == AF_UNIX)) {
23480Sstevel@tonic-gate 			socantsendmore(so);
23490Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
23500Sstevel@tonic-gate 			strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
23510Sstevel@tonic-gate 			dprintso(so, 1,
23520Sstevel@tonic-gate 				("T_DISCON_IND: error %d\n", so->so_error));
23530Sstevel@tonic-gate 			freemsg(mp);
23540Sstevel@tonic-gate 			/*
23550Sstevel@tonic-gate 			 * Set these variables for caller to process them.
23560Sstevel@tonic-gate 			 * For the else part where T_DISCON_IND is processed,
23570Sstevel@tonic-gate 			 * this will be done in the function being called
23580Sstevel@tonic-gate 			 * (strsock_discon_ind())
23590Sstevel@tonic-gate 			 */
23600Sstevel@tonic-gate 			*wakeups = WSLEEP;
23610Sstevel@tonic-gate 			*allmsgsigs = S_OUTPUT;
23620Sstevel@tonic-gate 			*pollwakeups = POLLOUT;
23630Sstevel@tonic-gate 		} else if (so->so_flag & (SOASYNC_UNBIND | SOLOCKED)) {
23640Sstevel@tonic-gate 			/*
23650Sstevel@tonic-gate 			 * Deferred processing of T_DISCON_IND
23660Sstevel@tonic-gate 			 */
23670Sstevel@tonic-gate 			so_save_discon_ind(so, mp, strsock_discon_ind);
23680Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
23690Sstevel@tonic-gate 		} else {
23700Sstevel@tonic-gate 			/*
23710Sstevel@tonic-gate 			 * Process T_DISCON_IND now
23720Sstevel@tonic-gate 			 */
23730Sstevel@tonic-gate 			(void) strsock_discon_ind(so, mp);
23740Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
23750Sstevel@tonic-gate 		}
23760Sstevel@tonic-gate 		return (NULL);
23770Sstevel@tonic-gate 
23780Sstevel@tonic-gate 	case T_UDERROR_IND: {
23790Sstevel@tonic-gate 		struct T_uderror_ind	*tudi = &tpr->uderror_ind;
23800Sstevel@tonic-gate 		void			*addr;
23810Sstevel@tonic-gate 		t_uscalar_t		addrlen;
23820Sstevel@tonic-gate 		int			error;
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 		dprintso(so, 0,
23850Sstevel@tonic-gate 			("T_UDERROR_IND: error %d\n", tudi->ERROR_type));
23860Sstevel@tonic-gate 
23870Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_uderror_ind)) {
23881548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
23890Sstevel@tonic-gate 			    "sockfs: Too short T_UDERROR_IND. Len = %ld\n",
23900Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
23910Sstevel@tonic-gate 			freemsg(mp);
23920Sstevel@tonic-gate 			return (NULL);
23930Sstevel@tonic-gate 		}
23940Sstevel@tonic-gate 		/* Ignore on connection-oriented transports */
23950Sstevel@tonic-gate 		if (so->so_mode & SM_CONNREQUIRED) {
23960Sstevel@tonic-gate 			freemsg(mp);
23970Sstevel@tonic-gate 			eprintsoline(so, 0);
23981548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
23990Sstevel@tonic-gate 			    "sockfs: T_uderror_ind on connection-oriented "
24000Sstevel@tonic-gate 			    "transport\n");
24010Sstevel@tonic-gate 			return (NULL);
24020Sstevel@tonic-gate 		}
24030Sstevel@tonic-gate 		addrlen = tudi->DEST_length;
24040Sstevel@tonic-gate 		addr = sogetoff(mp, tudi->DEST_offset, addrlen, 1);
24050Sstevel@tonic-gate 		if (addr == NULL) {
24061548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
24070Sstevel@tonic-gate 			    "sockfs: T_uderror_ind with invalid "
24080Sstevel@tonic-gate 			    "addrlen/offset %u/%d\n",
24090Sstevel@tonic-gate 			    addrlen, tudi->DEST_offset);
24100Sstevel@tonic-gate 			freemsg(mp);
24110Sstevel@tonic-gate 			return (NULL);
24120Sstevel@tonic-gate 		}
24130Sstevel@tonic-gate 
24140Sstevel@tonic-gate 		/* Verify source address for connected socket. */
24150Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24160Sstevel@tonic-gate 		if (so->so_state & SS_ISCONNECTED) {
24170Sstevel@tonic-gate 			void *faddr;
24180Sstevel@tonic-gate 			t_uscalar_t faddr_len;
24190Sstevel@tonic-gate 			boolean_t match = B_FALSE;
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate 			switch (so->so_family) {
24220Sstevel@tonic-gate 			case AF_INET: {
24230Sstevel@tonic-gate 				/* Compare just IP address and port */
24240Sstevel@tonic-gate 				struct sockaddr_in *sin1, *sin2;
24250Sstevel@tonic-gate 
24260Sstevel@tonic-gate 				sin1 = (struct sockaddr_in *)so->so_faddr_sa;
24270Sstevel@tonic-gate 				sin2 = (struct sockaddr_in *)addr;
24280Sstevel@tonic-gate 				if (addrlen == sizeof (struct sockaddr_in) &&
24290Sstevel@tonic-gate 				    sin1->sin_port == sin2->sin_port &&
24300Sstevel@tonic-gate 				    sin1->sin_addr.s_addr ==
24310Sstevel@tonic-gate 				    sin2->sin_addr.s_addr)
24320Sstevel@tonic-gate 					match = B_TRUE;
24330Sstevel@tonic-gate 				break;
24340Sstevel@tonic-gate 			}
24350Sstevel@tonic-gate 			case AF_INET6: {
24360Sstevel@tonic-gate 				/* Compare just IP address and port. Not flow */
24370Sstevel@tonic-gate 				struct sockaddr_in6 *sin1, *sin2;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 				sin1 = (struct sockaddr_in6 *)so->so_faddr_sa;
24400Sstevel@tonic-gate 				sin2 = (struct sockaddr_in6 *)addr;
24410Sstevel@tonic-gate 				if (addrlen == sizeof (struct sockaddr_in6) &&
24420Sstevel@tonic-gate 				    sin1->sin6_port == sin2->sin6_port &&
24430Sstevel@tonic-gate 				    IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
24440Sstevel@tonic-gate 					&sin2->sin6_addr))
24450Sstevel@tonic-gate 					match = B_TRUE;
24460Sstevel@tonic-gate 				break;
24470Sstevel@tonic-gate 			}
24480Sstevel@tonic-gate 			case AF_UNIX:
24490Sstevel@tonic-gate 				faddr = &so->so_ux_faddr;
24500Sstevel@tonic-gate 				faddr_len =
24510Sstevel@tonic-gate 					(t_uscalar_t)sizeof (so->so_ux_faddr);
24520Sstevel@tonic-gate 				if (faddr_len == addrlen &&
24530Sstevel@tonic-gate 				    bcmp(addr, faddr, addrlen) == 0)
24540Sstevel@tonic-gate 					match = B_TRUE;
24550Sstevel@tonic-gate 				break;
24560Sstevel@tonic-gate 			default:
24570Sstevel@tonic-gate 				faddr = so->so_faddr_sa;
24580Sstevel@tonic-gate 				faddr_len = (t_uscalar_t)so->so_faddr_len;
24590Sstevel@tonic-gate 				if (faddr_len == addrlen &&
24600Sstevel@tonic-gate 				    bcmp(addr, faddr, addrlen) == 0)
24610Sstevel@tonic-gate 					match = B_TRUE;
24620Sstevel@tonic-gate 				break;
24630Sstevel@tonic-gate 			}
24640Sstevel@tonic-gate 
24650Sstevel@tonic-gate 			if (!match) {
24660Sstevel@tonic-gate #ifdef DEBUG
24670Sstevel@tonic-gate 				dprintso(so, 0,
24680Sstevel@tonic-gate 					("sockfs: T_UDERR_IND mismatch: %s - ",
24690Sstevel@tonic-gate 					pr_addr(so->so_family,
24700Sstevel@tonic-gate 						(struct sockaddr *)addr,
24710Sstevel@tonic-gate 						addrlen)));
24720Sstevel@tonic-gate 				dprintso(so, 0, ("%s\n",
24730Sstevel@tonic-gate 					pr_addr(so->so_family, so->so_faddr_sa,
24740Sstevel@tonic-gate 						so->so_faddr_len)));
24750Sstevel@tonic-gate #endif /* DEBUG */
24760Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
24770Sstevel@tonic-gate 				freemsg(mp);
24780Sstevel@tonic-gate 				return (NULL);
24790Sstevel@tonic-gate 			}
24800Sstevel@tonic-gate 			/*
24810Sstevel@tonic-gate 			 * Make the write error nonpersistent. If the error
24820Sstevel@tonic-gate 			 * is zero we use ECONNRESET.
24830Sstevel@tonic-gate 			 * This assumes that the name space for ERROR_type
24840Sstevel@tonic-gate 			 * is the errno name space.
24850Sstevel@tonic-gate 			 */
24860Sstevel@tonic-gate 			if (tudi->ERROR_type != 0)
24870Sstevel@tonic-gate 				error = tudi->ERROR_type;
24880Sstevel@tonic-gate 			else
24890Sstevel@tonic-gate 				error = ECONNRESET;
24900Sstevel@tonic-gate 
24910Sstevel@tonic-gate 			soseterror(so, error);
24920Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
24930Sstevel@tonic-gate 			strsetrerror(SOTOV(so), 0, 0, sogetrderr);
24940Sstevel@tonic-gate 			strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
24950Sstevel@tonic-gate 			*wakeups = RSLEEP | WSLEEP;
24960Sstevel@tonic-gate 			*allmsgsigs = S_INPUT | S_RDNORM | S_OUTPUT;
24970Sstevel@tonic-gate 			*pollwakeups = POLLIN | POLLRDNORM | POLLOUT;
24980Sstevel@tonic-gate 			freemsg(mp);
24990Sstevel@tonic-gate 			return (NULL);
25000Sstevel@tonic-gate 		}
25010Sstevel@tonic-gate 		/*
25020Sstevel@tonic-gate 		 * If the application asked for delayed errors
25030Sstevel@tonic-gate 		 * record the T_UDERROR_IND so_eaddr_mp and the reason in
25040Sstevel@tonic-gate 		 * so_delayed_error for delayed error posting. If the reason
25050Sstevel@tonic-gate 		 * is zero use ECONNRESET.
25060Sstevel@tonic-gate 		 * Note that delayed error indications do not make sense for
25070Sstevel@tonic-gate 		 * AF_UNIX sockets since sendto checks that the destination
25080Sstevel@tonic-gate 		 * address is valid at the time of the sendto.
25090Sstevel@tonic-gate 		 */
25100Sstevel@tonic-gate 		if (!(so->so_options & SO_DGRAM_ERRIND)) {
25110Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
25120Sstevel@tonic-gate 			freemsg(mp);
25130Sstevel@tonic-gate 			return (NULL);
25140Sstevel@tonic-gate 		}
25150Sstevel@tonic-gate 		if (so->so_eaddr_mp != NULL)
25160Sstevel@tonic-gate 			freemsg(so->so_eaddr_mp);
25170Sstevel@tonic-gate 
25180Sstevel@tonic-gate 		so->so_eaddr_mp = mp;
25190Sstevel@tonic-gate 		if (tudi->ERROR_type != 0)
25200Sstevel@tonic-gate 			error = tudi->ERROR_type;
25210Sstevel@tonic-gate 		else
25220Sstevel@tonic-gate 			error = ECONNRESET;
25230Sstevel@tonic-gate 		so->so_delayed_error = (ushort_t)error;
25240Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
25250Sstevel@tonic-gate 		return (NULL);
25260Sstevel@tonic-gate 	}
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate 	case T_ERROR_ACK:
25290Sstevel@tonic-gate 		dprintso(so, 0,
25300Sstevel@tonic-gate 			("strsock_proto: T_ERROR_ACK for %d, error %d/%d\n",
25310Sstevel@tonic-gate 			tpr->error_ack.ERROR_prim,
25320Sstevel@tonic-gate 			tpr->error_ack.TLI_error,
25330Sstevel@tonic-gate 			tpr->error_ack.UNIX_error));
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_error_ack)) {
25361548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
25370Sstevel@tonic-gate 			    "sockfs: Too short T_ERROR_ACK. Len = %ld\n",
25380Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
25390Sstevel@tonic-gate 			freemsg(mp);
25400Sstevel@tonic-gate 			return (NULL);
25410Sstevel@tonic-gate 		}
25420Sstevel@tonic-gate 		/*
25430Sstevel@tonic-gate 		 * Check if we were waiting for the async message
25440Sstevel@tonic-gate 		 */
25450Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
25460Sstevel@tonic-gate 		if ((so->so_flag & SOASYNC_UNBIND) &&
25470Sstevel@tonic-gate 		    tpr->error_ack.ERROR_prim == T_UNBIND_REQ) {
25480Sstevel@tonic-gate 			so_unlock_single(so, SOASYNC_UNBIND);
25490Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
25500Sstevel@tonic-gate 			freemsg(mp);
25510Sstevel@tonic-gate 			return (NULL);
25520Sstevel@tonic-gate 		}
25530Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
25540Sstevel@tonic-gate 		soqueueack(so, mp);
25550Sstevel@tonic-gate 		return (NULL);
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate 	case T_OK_ACK:
25580Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_ok_ack)) {
25591548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
25600Sstevel@tonic-gate 			    "sockfs: Too short T_OK_ACK. Len = %ld\n",
25610Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
25620Sstevel@tonic-gate 			freemsg(mp);
25630Sstevel@tonic-gate 			return (NULL);
25640Sstevel@tonic-gate 		}
25650Sstevel@tonic-gate 		/*
25660Sstevel@tonic-gate 		 * Check if we were waiting for the async message
25670Sstevel@tonic-gate 		 */
25680Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
25690Sstevel@tonic-gate 		if ((so->so_flag & SOASYNC_UNBIND) &&
25700Sstevel@tonic-gate 		    tpr->ok_ack.CORRECT_prim == T_UNBIND_REQ) {
25710Sstevel@tonic-gate 			dprintso(so, 1,
25720Sstevel@tonic-gate 				("strsock_proto: T_OK_ACK async unbind\n"));
25730Sstevel@tonic-gate 			so_unlock_single(so, SOASYNC_UNBIND);
25740Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
25750Sstevel@tonic-gate 			freemsg(mp);
25760Sstevel@tonic-gate 			return (NULL);
25770Sstevel@tonic-gate 		}
25780Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
25790Sstevel@tonic-gate 		soqueueack(so, mp);
25800Sstevel@tonic-gate 		return (NULL);
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 	case T_INFO_ACK:
25830Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_info_ack)) {
25841548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
25850Sstevel@tonic-gate 			    "sockfs: Too short T_INFO_ACK. Len = %ld\n",
25860Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
25870Sstevel@tonic-gate 			freemsg(mp);
25880Sstevel@tonic-gate 			return (NULL);
25890Sstevel@tonic-gate 		}
25900Sstevel@tonic-gate 		soqueueack(so, mp);
25910Sstevel@tonic-gate 		return (NULL);
25920Sstevel@tonic-gate 
25930Sstevel@tonic-gate 	case T_CAPABILITY_ACK:
25940Sstevel@tonic-gate 		/*
25950Sstevel@tonic-gate 		 * A T_capability_ack need only be large enough to hold
25960Sstevel@tonic-gate 		 * the PRIM_type and CAP_bits1 fields; checking for anything
25970Sstevel@tonic-gate 		 * larger might reject a correct response from an older
25980Sstevel@tonic-gate 		 * provider.
25990Sstevel@tonic-gate 		 */
26000Sstevel@tonic-gate 		if (MBLKL(mp) < 2 * sizeof (t_uscalar_t)) {
26011548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
26020Sstevel@tonic-gate 			    "sockfs: Too short T_CAPABILITY_ACK. Len = %ld\n",
26030Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
26040Sstevel@tonic-gate 			freemsg(mp);
26050Sstevel@tonic-gate 			return (NULL);
26060Sstevel@tonic-gate 		}
26070Sstevel@tonic-gate 		soqueueack(so, mp);
26080Sstevel@tonic-gate 		return (NULL);
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	case T_BIND_ACK:
26110Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_bind_ack)) {
26121548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
26130Sstevel@tonic-gate 			    "sockfs: Too short T_BIND_ACK. Len = %ld\n",
26140Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
26150Sstevel@tonic-gate 			freemsg(mp);
26160Sstevel@tonic-gate 			return (NULL);
26170Sstevel@tonic-gate 		}
26180Sstevel@tonic-gate 		soqueueack(so, mp);
26190Sstevel@tonic-gate 		return (NULL);
26200Sstevel@tonic-gate 
26210Sstevel@tonic-gate 	case T_OPTMGMT_ACK:
26220Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_optmgmt_ack)) {
26231548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
26240Sstevel@tonic-gate 			    "sockfs: Too short T_OPTMGMT_ACK. Len = %ld\n",
26250Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
26260Sstevel@tonic-gate 			freemsg(mp);
26270Sstevel@tonic-gate 			return (NULL);
26280Sstevel@tonic-gate 		}
26290Sstevel@tonic-gate 		soqueueack(so, mp);
26300Sstevel@tonic-gate 		return (NULL);
26310Sstevel@tonic-gate 	default:
26320Sstevel@tonic-gate #ifdef DEBUG
26331548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
26340Sstevel@tonic-gate 			"sockfs: unknown TPI primitive %d received\n",
26350Sstevel@tonic-gate 			tpr->type);
26360Sstevel@tonic-gate #endif /* DEBUG */
26370Sstevel@tonic-gate 		freemsg(mp);
26380Sstevel@tonic-gate 		return (NULL);
26390Sstevel@tonic-gate 	}
26400Sstevel@tonic-gate }
26410Sstevel@tonic-gate 
26420Sstevel@tonic-gate /*
26430Sstevel@tonic-gate  * This routine is registered with the stream head to receive other
26440Sstevel@tonic-gate  * (non-data, and non-proto) messages.
26450Sstevel@tonic-gate  *
26460Sstevel@tonic-gate  * Returns NULL if the message was consumed.
26470Sstevel@tonic-gate  * Returns an mblk to make that mblk be processed by the stream head.
26480Sstevel@tonic-gate  *
26490Sstevel@tonic-gate  * Sets the return parameters (*wakeups, *firstmsgsigs, *allmsgsigs, and
26500Sstevel@tonic-gate  * *pollwakeups) for the stream head to take action on.
26510Sstevel@tonic-gate  */
26520Sstevel@tonic-gate static mblk_t *
26530Sstevel@tonic-gate strsock_misc(vnode_t *vp, mblk_t *mp,
26540Sstevel@tonic-gate 		strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
26550Sstevel@tonic-gate 		strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
26560Sstevel@tonic-gate {
26570Sstevel@tonic-gate 	struct sonode *so;
26580Sstevel@tonic-gate 
26590Sstevel@tonic-gate 	so = VTOSO(vp);
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 	dprintso(so, 1, ("strsock_misc(%p, %p, 0x%x)\n",
26620Sstevel@tonic-gate 			vp, mp, DB_TYPE(mp)));
26630Sstevel@tonic-gate 
26640Sstevel@tonic-gate 	/* Set default return values */
26650Sstevel@tonic-gate 	*wakeups = *allmsgsigs = *firstmsgsigs = *pollwakeups = 0;
26660Sstevel@tonic-gate 
26670Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
26680Sstevel@tonic-gate 	case M_PCSIG:
26690Sstevel@tonic-gate 		/*
26700Sstevel@tonic-gate 		 * This assumes that an M_PCSIG for the urgent data arrives
26710Sstevel@tonic-gate 		 * before the corresponding T_EXDATA_IND.
26720Sstevel@tonic-gate 		 *
26730Sstevel@tonic-gate 		 * Note: Just like in SunOS 4.X and 4.4BSD a poll will be
26740Sstevel@tonic-gate 		 * awoken before the urgent data shows up.
26750Sstevel@tonic-gate 		 * For OOBINLINE this can result in select returning
26760Sstevel@tonic-gate 		 * only exceptions as opposed to except|read.
26770Sstevel@tonic-gate 		 */
26780Sstevel@tonic-gate 		if (*mp->b_rptr == SIGURG) {
26790Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
26800Sstevel@tonic-gate 			dprintso(so, 1,
26810Sstevel@tonic-gate 				("SIGURG(%p): counts %d/%d state %s\n",
26820Sstevel@tonic-gate 				vp, so->so_oobsigcnt,
26830Sstevel@tonic-gate 				so->so_oobcnt,
26840Sstevel@tonic-gate 				pr_state(so->so_state, so->so_mode)));
26850Sstevel@tonic-gate 			so_oob_sig(so, 1, allmsgsigs, pollwakeups);
26860Sstevel@tonic-gate 			dprintso(so, 1,
26870Sstevel@tonic-gate 				("after SIGURG(%p): counts %d/%d "
26880Sstevel@tonic-gate 				" poll 0x%x sig 0x%x state %s\n",
26890Sstevel@tonic-gate 				vp, so->so_oobsigcnt,
26900Sstevel@tonic-gate 				so->so_oobcnt, *pollwakeups, *allmsgsigs,
26910Sstevel@tonic-gate 				pr_state(so->so_state, so->so_mode)));
26920Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
26930Sstevel@tonic-gate 		}
26940Sstevel@tonic-gate 		freemsg(mp);
26950Sstevel@tonic-gate 		return (NULL);
26960Sstevel@tonic-gate 
26970Sstevel@tonic-gate 	case M_SIG:
26980Sstevel@tonic-gate 	case M_HANGUP:
26990Sstevel@tonic-gate 	case M_UNHANGUP:
27000Sstevel@tonic-gate 	case M_ERROR:
27010Sstevel@tonic-gate 		/* M_ERRORs etc are ignored */
27020Sstevel@tonic-gate 		freemsg(mp);
27030Sstevel@tonic-gate 		return (NULL);
27040Sstevel@tonic-gate 
27050Sstevel@tonic-gate 	case M_FLUSH:
27060Sstevel@tonic-gate 		/*
27070Sstevel@tonic-gate 		 * Do not flush read queue. If the M_FLUSH
27080Sstevel@tonic-gate 		 * arrives because of an impending T_discon_ind
27090Sstevel@tonic-gate 		 * we still have to keep any queued data - this is part of
27100Sstevel@tonic-gate 		 * socket semantics.
27110Sstevel@tonic-gate 		 */
27120Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
27130Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;
27140Sstevel@tonic-gate 			return (mp);
27150Sstevel@tonic-gate 		}
27160Sstevel@tonic-gate 		freemsg(mp);
27170Sstevel@tonic-gate 		return (NULL);
27180Sstevel@tonic-gate 
27190Sstevel@tonic-gate 	default:
27200Sstevel@tonic-gate 		return (mp);
27210Sstevel@tonic-gate 	}
27220Sstevel@tonic-gate }
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate /* Register to receive signals for certain events */
27260Sstevel@tonic-gate int
27270Sstevel@tonic-gate so_set_asyncsigs(vnode_t *vp, pid_t pgrp, int events, int mode, cred_t *cr)
27280Sstevel@tonic-gate {
27290Sstevel@tonic-gate 	struct strsigset ss;
27300Sstevel@tonic-gate 	int32_t rval;
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	/*
27330Sstevel@tonic-gate 	 * Note that SOLOCKED will be set except for the call from soaccept().
27340Sstevel@tonic-gate 	 */
27350Sstevel@tonic-gate 	ASSERT(!mutex_owned(&VTOSO(vp)->so_lock));
27360Sstevel@tonic-gate 	ss.ss_pid = pgrp;
27370Sstevel@tonic-gate 	ss.ss_events = events;
27380Sstevel@tonic-gate 	return (strioctl(vp, I_ESETSIG, (intptr_t)&ss, mode, K_TO_K, cr,
27390Sstevel@tonic-gate 	    &rval));
27400Sstevel@tonic-gate }
27410Sstevel@tonic-gate 
27420Sstevel@tonic-gate 
27430Sstevel@tonic-gate /* Register for events matching the SS_ASYNC flag */
27440Sstevel@tonic-gate int
27450Sstevel@tonic-gate so_set_events(struct sonode *so, vnode_t *vp, cred_t *cr)
27460Sstevel@tonic-gate {
27470Sstevel@tonic-gate 	int events = so->so_state & SS_ASYNC ?
27480Sstevel@tonic-gate 	    S_RDBAND | S_BANDURG | S_RDNORM | S_OUTPUT :
27490Sstevel@tonic-gate 	    S_RDBAND | S_BANDURG;
27500Sstevel@tonic-gate 
27510Sstevel@tonic-gate 	return (so_set_asyncsigs(vp, so->so_pgrp, events, 0, cr));
27520Sstevel@tonic-gate }
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 
27550Sstevel@tonic-gate /* Change the SS_ASYNC flag, and update signal delivery if needed */
27560Sstevel@tonic-gate int
27570Sstevel@tonic-gate so_flip_async(struct sonode *so, vnode_t *vp, int mode, cred_t *cr)
27580Sstevel@tonic-gate {
27590Sstevel@tonic-gate 	ASSERT(mutex_owned(&so->so_lock));
27600Sstevel@tonic-gate 	if (so->so_pgrp != 0) {
27610Sstevel@tonic-gate 		int error;
27620Sstevel@tonic-gate 		int events = so->so_state & SS_ASYNC ?		/* Old flag */
27630Sstevel@tonic-gate 		    S_RDBAND | S_BANDURG :			/* New sigs */
27640Sstevel@tonic-gate 		    S_RDBAND | S_BANDURG | S_RDNORM | S_OUTPUT;
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate 		so_lock_single(so);
27670Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate 		error = so_set_asyncsigs(vp, so->so_pgrp, events, mode, cr);
27700Sstevel@tonic-gate 
27710Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27720Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
27730Sstevel@tonic-gate 		if (error)
27740Sstevel@tonic-gate 			return (error);
27750Sstevel@tonic-gate 	}
27760Sstevel@tonic-gate 	so->so_state ^= SS_ASYNC;
27770Sstevel@tonic-gate 	return (0);
27780Sstevel@tonic-gate }
27790Sstevel@tonic-gate 
27800Sstevel@tonic-gate /*
27810Sstevel@tonic-gate  * Set new pid/pgrp for SIGPOLL (or SIGIO for FIOASYNC mode), replacing
27820Sstevel@tonic-gate  * any existing one.  If passed zero, just clear the existing one.
27830Sstevel@tonic-gate  */
27840Sstevel@tonic-gate int
27850Sstevel@tonic-gate so_set_siggrp(struct sonode *so, vnode_t *vp, pid_t pgrp, int mode, cred_t *cr)
27860Sstevel@tonic-gate {
27870Sstevel@tonic-gate 	int events = so->so_state & SS_ASYNC ?
27880Sstevel@tonic-gate 	    S_RDBAND | S_BANDURG | S_RDNORM | S_OUTPUT :
27890Sstevel@tonic-gate 	    S_RDBAND | S_BANDURG;
27900Sstevel@tonic-gate 	int error;
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	ASSERT(mutex_owned(&so->so_lock));
27930Sstevel@tonic-gate 
27940Sstevel@tonic-gate 	/*
27950Sstevel@tonic-gate 	 * Change socket process (group).
27960Sstevel@tonic-gate 	 *
27970Sstevel@tonic-gate 	 * strioctl (via so_set_asyncsigs) will perform permission check and
27980Sstevel@tonic-gate 	 * also keep a PID_HOLD to prevent the pid from being reused.
27990Sstevel@tonic-gate 	 */
28000Sstevel@tonic-gate 	so_lock_single(so);
28010Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
28020Sstevel@tonic-gate 
28030Sstevel@tonic-gate 	if (pgrp != 0) {
28040Sstevel@tonic-gate 		dprintso(so, 1, ("setown: adding pgrp %d ev 0x%x\n",
28050Sstevel@tonic-gate 		    pgrp, events));
28060Sstevel@tonic-gate 		error = so_set_asyncsigs(vp, pgrp, events, mode, cr);
28070Sstevel@tonic-gate 		if (error != 0) {
28080Sstevel@tonic-gate 			eprintsoline(so, error);
28090Sstevel@tonic-gate 			goto bad;
28100Sstevel@tonic-gate 		}
28110Sstevel@tonic-gate 	}
28120Sstevel@tonic-gate 	/* Remove the previously registered process/group */
28130Sstevel@tonic-gate 	if (so->so_pgrp != 0) {
28140Sstevel@tonic-gate 		dprintso(so, 1, ("setown: removing pgrp %d\n", so->so_pgrp));
28150Sstevel@tonic-gate 		error = so_set_asyncsigs(vp, so->so_pgrp, 0, mode, cr);
28160Sstevel@tonic-gate 		if (error != 0) {
28170Sstevel@tonic-gate 			eprintsoline(so, error);
28180Sstevel@tonic-gate 			error = 0;
28190Sstevel@tonic-gate 		}
28200Sstevel@tonic-gate 	}
28210Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
28220Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
28230Sstevel@tonic-gate 	so->so_pgrp = pgrp;
28240Sstevel@tonic-gate 	return (0);
28250Sstevel@tonic-gate bad:
28260Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
28270Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
28280Sstevel@tonic-gate 	return (error);
28290Sstevel@tonic-gate }
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate /*
28340Sstevel@tonic-gate  * Translate a TLI(/XTI) error into a system error as best we can.
28350Sstevel@tonic-gate  */
28360Sstevel@tonic-gate static const int tli_errs[] = {
28370Sstevel@tonic-gate 		0,		/* no error	*/
28380Sstevel@tonic-gate 		EADDRNOTAVAIL,  /* TBADADDR	*/
28390Sstevel@tonic-gate 		ENOPROTOOPT,	/* TBADOPT	*/
28400Sstevel@tonic-gate 		EACCES,		/* TACCES	*/
28410Sstevel@tonic-gate 		EBADF,		/* TBADF	*/
28420Sstevel@tonic-gate 		EADDRNOTAVAIL,	/* TNOADDR	*/
28430Sstevel@tonic-gate 		EPROTO,		/* TOUTSTATE	*/
28440Sstevel@tonic-gate 		ECONNABORTED,	/* TBADSEQ	*/
28450Sstevel@tonic-gate 		0,		/* TSYSERR - will never get	*/
28460Sstevel@tonic-gate 		EPROTO,		/* TLOOK - should never be sent by transport */
28470Sstevel@tonic-gate 		EMSGSIZE,	/* TBADDATA	*/
28480Sstevel@tonic-gate 		EMSGSIZE,	/* TBUFOVFLW	*/
28490Sstevel@tonic-gate 		EPROTO,		/* TFLOW	*/
28500Sstevel@tonic-gate 		EWOULDBLOCK,	/* TNODATA	*/
28510Sstevel@tonic-gate 		EPROTO,		/* TNODIS	*/
28520Sstevel@tonic-gate 		EPROTO,		/* TNOUDERR	*/
28530Sstevel@tonic-gate 		EINVAL,		/* TBADFLAG	*/
28540Sstevel@tonic-gate 		EPROTO,		/* TNOREL	*/
28550Sstevel@tonic-gate 		EOPNOTSUPP,	/* TNOTSUPPORT	*/
28560Sstevel@tonic-gate 		EPROTO,		/* TSTATECHNG	*/
28570Sstevel@tonic-gate 		/* following represent error namespace expansion with XTI */
28580Sstevel@tonic-gate 		EPROTO,		/* TNOSTRUCTYPE - never sent by transport */
28590Sstevel@tonic-gate 		EPROTO,		/* TBADNAME - never sent by transport */
28600Sstevel@tonic-gate 		EPROTO,		/* TBADQLEN - never sent by transport */
28610Sstevel@tonic-gate 		EADDRINUSE,	/* TADDRBUSY	*/
28620Sstevel@tonic-gate 		EBADF,		/* TINDOUT	*/
28630Sstevel@tonic-gate 		EBADF,		/* TPROVMISMATCH */
28640Sstevel@tonic-gate 		EBADF,		/* TRESQLEN	*/
28650Sstevel@tonic-gate 		EBADF,		/* TRESADDR	*/
28660Sstevel@tonic-gate 		EPROTO,		/* TQFULL - never sent by transport */
28670Sstevel@tonic-gate 		EPROTO,		/* TPROTO	*/
28680Sstevel@tonic-gate };
28690Sstevel@tonic-gate 
28700Sstevel@tonic-gate static int
28710Sstevel@tonic-gate tlitosyserr(int terr)
28720Sstevel@tonic-gate {
28730Sstevel@tonic-gate 	ASSERT(terr != TSYSERR);
28740Sstevel@tonic-gate 	if (terr >= (sizeof (tli_errs) / sizeof (tli_errs[0])))
28750Sstevel@tonic-gate 		return (EPROTO);
28760Sstevel@tonic-gate 	else
28770Sstevel@tonic-gate 		return (tli_errs[terr]);
28780Sstevel@tonic-gate }
2879