xref: /onnv-gate/usr/src/uts/common/fs/sockfs/sockstr.c (revision 11861:a63258283f8f)
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 /*
23*11861SMarek.Pospisil@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/inttypes.h>
290Sstevel@tonic-gate #include <sys/t_lock.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/buf.h>
330Sstevel@tonic-gate #include <sys/conf.h>
340Sstevel@tonic-gate #include <sys/cred.h>
350Sstevel@tonic-gate #include <sys/kmem.h>
360Sstevel@tonic-gate #include <sys/sysmacros.h>
370Sstevel@tonic-gate #include <sys/vfs.h>
380Sstevel@tonic-gate #include <sys/vnode.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/errno.h>
410Sstevel@tonic-gate #include <sys/time.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/user.h>
440Sstevel@tonic-gate #include <sys/stream.h>
450Sstevel@tonic-gate #include <sys/strsubr.h>
460Sstevel@tonic-gate #include <sys/esunddi.h>
470Sstevel@tonic-gate #include <sys/flock.h>
480Sstevel@tonic-gate #include <sys/modctl.h>
490Sstevel@tonic-gate #include <sys/vtrace.h>
500Sstevel@tonic-gate #include <sys/strsun.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate #include <sys/proc.h>
530Sstevel@tonic-gate #include <sys/ddi.h>
540Sstevel@tonic-gate 
550Sstevel@tonic-gate #include <sys/suntpi.h>
560Sstevel@tonic-gate #include <sys/socket.h>
570Sstevel@tonic-gate #include <sys/sockio.h>
580Sstevel@tonic-gate #include <sys/socketvar.h>
590Sstevel@tonic-gate #include <netinet/in.h>
608348SEric.Yu@Sun.COM #include <inet/common.h>
618348SEric.Yu@Sun.COM #include <inet/proto_set.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <sys/tiuser.h>
640Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
650Sstevel@tonic-gate #include <sys/tihdr.h>
660Sstevel@tonic-gate 
67898Skais #include <inet/kssl/ksslapi.h>
68898Skais 
690Sstevel@tonic-gate #include <c2/audit.h>
700Sstevel@tonic-gate 
718348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h>
728348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi_impl.h>
736707Sbrutus 
740Sstevel@tonic-gate int so_default_version = SOV_SOCKSTREAM;
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #ifdef DEBUG
770Sstevel@tonic-gate /* Set sockdebug to print debug messages when SO_DEBUG is set */
780Sstevel@tonic-gate int sockdebug = 0;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /* Set sockprinterr to print error messages when SO_DEBUG is set */
810Sstevel@tonic-gate int sockprinterr = 0;
820Sstevel@tonic-gate 
830Sstevel@tonic-gate /*
840Sstevel@tonic-gate  * Set so_default_options to SO_DEBUG is all sockets should be created
850Sstevel@tonic-gate  * with SO_DEBUG set. This is needed to get debug printouts from the
860Sstevel@tonic-gate  * socket() call itself.
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate int so_default_options = 0;
890Sstevel@tonic-gate #endif /* DEBUG */
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #ifdef SOCK_TEST
920Sstevel@tonic-gate /*
930Sstevel@tonic-gate  * Set to number of ticks to limit cv_waits for code coverage testing.
940Sstevel@tonic-gate  * Set to 1000 when SO_DEBUG is set to 2.
950Sstevel@tonic-gate  */
960Sstevel@tonic-gate clock_t sock_test_timelimit = 0;
970Sstevel@tonic-gate #endif /* SOCK_TEST */
980Sstevel@tonic-gate 
990Sstevel@tonic-gate /*
1000Sstevel@tonic-gate  * For concurrency testing of e.g. opening /dev/ip which does not
1010Sstevel@tonic-gate  * handle T_INFO_REQ messages.
1020Sstevel@tonic-gate  */
1030Sstevel@tonic-gate int so_no_tinfo = 0;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate /*
1060Sstevel@tonic-gate  * Timeout for getting a T_CAPABILITY_ACK - it is possible for a provider
1070Sstevel@tonic-gate  * to simply ignore the T_CAPABILITY_REQ.
1080Sstevel@tonic-gate  */
1090Sstevel@tonic-gate clock_t	sock_capability_timeout	= 2;	/* seconds */
1100Sstevel@tonic-gate 
1110Sstevel@tonic-gate static int	do_tcapability(struct sonode *so, t_uscalar_t cap_bits1);
1120Sstevel@tonic-gate static void	so_removehooks(struct sonode *so);
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate static mblk_t *strsock_proto(vnode_t *vp, mblk_t *mp,
1150Sstevel@tonic-gate 		strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1160Sstevel@tonic-gate 		strsigset_t *allmsgsigs, strpollset_t *pollwakeups);
1170Sstevel@tonic-gate static mblk_t *strsock_misc(vnode_t *vp, mblk_t *mp,
1180Sstevel@tonic-gate 		strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
1190Sstevel@tonic-gate 		strsigset_t *allmsgsigs, strpollset_t *pollwakeups);
1206707Sbrutus 
1216707Sbrutus /*
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;
1348348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
1350Sstevel@tonic-gate 
1368348SEric.Yu@Sun.COM 	ASSERT(MUTEX_HELD(&sti->sti_plumb_lock));
1370Sstevel@tonic-gate 
1380Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
1390Sstevel@tonic-gate 	so_lock_single(so);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	ASSERT(so->so_version != SOV_STREAM);
1420Sstevel@tonic-gate 
1438348SEric.Yu@Sun.COM 	if (sti->sti_direct) {
144741Smasputra 		mblk_t **mpp;
145741Smasputra 		int rval;
1460Sstevel@tonic-gate 
147741Smasputra 		/*
148741Smasputra 		 * Tell the transport below that sockmod is being popped
149741Smasputra 		 */
1500Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
151741Smasputra 		error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K, CRED(),
1520Sstevel@tonic-gate 		    &rval);
1530Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
1540Sstevel@tonic-gate 		if (error != 0) {
155741Smasputra 			dprintso(so, 0, ("so_sock2stream(%p): "
1567240Srh87107 			    "_SIOCSOCKFALLBACK failed\n", (void *)so));
1570Sstevel@tonic-gate 			goto exit;
1580Sstevel@tonic-gate 		}
1598348SEric.Yu@Sun.COM 		sti->sti_direct = 0;
1600Sstevel@tonic-gate 
1618348SEric.Yu@Sun.COM 		for (mpp = &sti->sti_conn_ind_head; (mp = *mpp) != NULL;
1620Sstevel@tonic-gate 		    mpp = &mp->b_next) {
1630Sstevel@tonic-gate 			struct T_conn_ind	*conn_ind;
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate 			/*
1660Sstevel@tonic-gate 			 * strsock_proto() has already verified the length of
1670Sstevel@tonic-gate 			 * this message block.
1680Sstevel@tonic-gate 			 */
1690Sstevel@tonic-gate 			ASSERT(MBLKL(mp) >= sizeof (struct T_conn_ind));
1700Sstevel@tonic-gate 
1710Sstevel@tonic-gate 			conn_ind = (struct T_conn_ind *)mp->b_rptr;
1720Sstevel@tonic-gate 			if (conn_ind->OPT_length == 0 &&
1730Sstevel@tonic-gate 			    conn_ind->OPT_offset == 0)
1740Sstevel@tonic-gate 				continue;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 			if (DB_REF(mp) > 1) {
1770Sstevel@tonic-gate 				mblk_t	*newmp;
1780Sstevel@tonic-gate 				size_t	length;
1790Sstevel@tonic-gate 				cred_t	*cr;
1808778SErik.Nordmark@Sun.COM 				pid_t	cpid;
1818778SErik.Nordmark@Sun.COM 				int error;	/* Dummy - error not returned */
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 				/*
1840Sstevel@tonic-gate 				 * Copy the message block because it is used
1850Sstevel@tonic-gate 				 * elsewhere, too.
1868778SErik.Nordmark@Sun.COM 				 * Can't use copyb since we want to wait
1878778SErik.Nordmark@Sun.COM 				 * yet allow for EINTR.
1880Sstevel@tonic-gate 				 */
1898778SErik.Nordmark@Sun.COM 				/* Round up size for reuse */
1908778SErik.Nordmark@Sun.COM 				length = MAX(MBLKL(mp), 64);
1918778SErik.Nordmark@Sun.COM 				cr = msg_getcred(mp, &cpid);
1928778SErik.Nordmark@Sun.COM 				if (cr != NULL) {
1938778SErik.Nordmark@Sun.COM 					newmp = allocb_cred_wait(length, 0,
1948778SErik.Nordmark@Sun.COM 					    &error, cr, cpid);
1958778SErik.Nordmark@Sun.COM 				} else {
1968778SErik.Nordmark@Sun.COM 					newmp = allocb_wait(length, 0, 0,
1978778SErik.Nordmark@Sun.COM 					    &error);
1988778SErik.Nordmark@Sun.COM 				}
1990Sstevel@tonic-gate 				if (newmp == NULL) {
2000Sstevel@tonic-gate 					error = EINTR;
2010Sstevel@tonic-gate 					goto exit;
2020Sstevel@tonic-gate 				}
2030Sstevel@tonic-gate 				bcopy(mp->b_rptr, newmp->b_wptr, length);
2040Sstevel@tonic-gate 				newmp->b_wptr += length;
2050Sstevel@tonic-gate 				newmp->b_next = mp->b_next;
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate 				/*
2080Sstevel@tonic-gate 				 * Link the new message block into the queue
2090Sstevel@tonic-gate 				 * and free the old one.
2100Sstevel@tonic-gate 				 */
2110Sstevel@tonic-gate 				*mpp = newmp;
2120Sstevel@tonic-gate 				mp->b_next = NULL;
2130Sstevel@tonic-gate 				freemsg(mp);
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 				mp = newmp;
2160Sstevel@tonic-gate 				conn_ind = (struct T_conn_ind *)mp->b_rptr;
2170Sstevel@tonic-gate 			}
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 			/*
2200Sstevel@tonic-gate 			 * Remove options added by TCP for accept fast-path.
2210Sstevel@tonic-gate 			 */
2220Sstevel@tonic-gate 			conn_ind->OPT_length = 0;
2230Sstevel@tonic-gate 			conn_ind->OPT_offset = 0;
2240Sstevel@tonic-gate 		}
2250Sstevel@tonic-gate 	}
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate 	so->so_version = SOV_STREAM;
2288348SEric.Yu@Sun.COM 	so->so_proto_handle = NULL;
2290Sstevel@tonic-gate 
2300Sstevel@tonic-gate 	/*
2310Sstevel@tonic-gate 	 * Remove the hooks in the stream head to avoid queuing more
2320Sstevel@tonic-gate 	 * packets in sockfs.
2330Sstevel@tonic-gate 	 */
2340Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2350Sstevel@tonic-gate 	so_removehooks(so);
2360Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	/*
2390Sstevel@tonic-gate 	 * Clear any state related to urgent data. Leave any T_EXDATA_IND
2400Sstevel@tonic-gate 	 * on the queue - the behavior of urgent data after a switch is
2410Sstevel@tonic-gate 	 * left undefined.
2420Sstevel@tonic-gate 	 */
2438348SEric.Yu@Sun.COM 	so->so_error = sti->sti_delayed_error = 0;
2440Sstevel@tonic-gate 	freemsg(so->so_oobmsg);
2450Sstevel@tonic-gate 	so->so_oobmsg = NULL;
2468348SEric.Yu@Sun.COM 	sti->sti_oobsigcnt = sti->sti_oobcnt = 0;
2470Sstevel@tonic-gate 
2480Sstevel@tonic-gate 	so->so_state &= ~(SS_RCVATMARK|SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA|
2498348SEric.Yu@Sun.COM 	    SS_SAVEDEOR);
2500Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
2510Sstevel@tonic-gate 
2528348SEric.Yu@Sun.COM 	freemsg(sti->sti_ack_mp);
2538348SEric.Yu@Sun.COM 	sti->sti_ack_mp = NULL;
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 	/*
2568348SEric.Yu@Sun.COM 	 * Flush the T_DISCON_IND on sti_discon_ind_mp.
2570Sstevel@tonic-gate 	 */
2580Sstevel@tonic-gate 	so_flush_discon_ind(so);
2590Sstevel@tonic-gate 
2600Sstevel@tonic-gate 	/*
2610Sstevel@tonic-gate 	 * Move any queued T_CONN_IND messages to stream head queue.
2620Sstevel@tonic-gate 	 */
2630Sstevel@tonic-gate 	rq = RD(strvp2wq(vp));
2648348SEric.Yu@Sun.COM 	while ((mp = sti->sti_conn_ind_head) != NULL) {
2658348SEric.Yu@Sun.COM 		sti->sti_conn_ind_head = mp->b_next;
2660Sstevel@tonic-gate 		mp->b_next = NULL;
2678348SEric.Yu@Sun.COM 		if (sti->sti_conn_ind_head == NULL) {
2688348SEric.Yu@Sun.COM 			ASSERT(sti->sti_conn_ind_tail == mp);
2698348SEric.Yu@Sun.COM 			sti->sti_conn_ind_tail = NULL;
2700Sstevel@tonic-gate 		}
2710Sstevel@tonic-gate 		dprintso(so, 0,
2728348SEric.Yu@Sun.COM 		    ("so_sock2stream(%p): moving T_CONN_IND\n", (void *)so));
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 		/* Drop lock across put() */
2750Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
2760Sstevel@tonic-gate 		put(rq, mp);
2770Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
2780Sstevel@tonic-gate 	}
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate exit:
2810Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
2820Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
2830Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2840Sstevel@tonic-gate 	return (error);
2850Sstevel@tonic-gate }
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate /*
2880Sstevel@tonic-gate  * Covert a stream back to a socket. This is invoked when the illusory
2890Sstevel@tonic-gate  * sockmod is pushed on a stream (where the stream was "created" by
2900Sstevel@tonic-gate  * popping the illusory sockmod).
2910Sstevel@tonic-gate  * This routine can not recreate the socket state (certain aspects of
2920Sstevel@tonic-gate  * it like urgent data state and the bound/connected addresses for AF_UNIX
2930Sstevel@tonic-gate  * sockets can not be recreated by asking the transport for information).
2940Sstevel@tonic-gate  * Thus this routine implicitly assumes that the socket is in an initial
2950Sstevel@tonic-gate  * state (as if it was just created). It flushes any messages queued on the
2960Sstevel@tonic-gate  * read queue to avoid dealing with e.g. TPI acks or T_exdata_ind messages.
2970Sstevel@tonic-gate  */
2980Sstevel@tonic-gate void
2990Sstevel@tonic-gate so_stream2sock(struct sonode *so)
3000Sstevel@tonic-gate {
3010Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
3028348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
3030Sstevel@tonic-gate 
3048348SEric.Yu@Sun.COM 	ASSERT(MUTEX_HELD(&sti->sti_plumb_lock));
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
3070Sstevel@tonic-gate 	so_lock_single(so);
3080Sstevel@tonic-gate 	ASSERT(so->so_version == SOV_STREAM);
3090Sstevel@tonic-gate 	so->so_version = SOV_SOCKSTREAM;
3108348SEric.Yu@Sun.COM 	sti->sti_pushcnt = 0;
3110Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	/*
3140Sstevel@tonic-gate 	 * Set a permenent error to force any thread in sorecvmsg to
3150Sstevel@tonic-gate 	 * return (and drop SOREADLOCKED). Clear the error once
3160Sstevel@tonic-gate 	 * we have SOREADLOCKED.
3170Sstevel@tonic-gate 	 * This makes a read sleeping during the I_PUSH of sockmod return
3180Sstevel@tonic-gate 	 * EIO.
3190Sstevel@tonic-gate 	 */
3200Sstevel@tonic-gate 	strsetrerror(SOTOV(so), EIO, 1, NULL);
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate 	/*
3230Sstevel@tonic-gate 	 * Get the read lock before flushing data to avoid
3240Sstevel@tonic-gate 	 * problems with the T_EXDATA_IND MSG_PEEK code in sorecvmsg.
3250Sstevel@tonic-gate 	 */
3260Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
3270Sstevel@tonic-gate 	(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
3280Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
3290Sstevel@tonic-gate 
3300Sstevel@tonic-gate 	strsetrerror(SOTOV(so), 0, 0, NULL);
3310Sstevel@tonic-gate 	so_installhooks(so);
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * Flush everything on the read queue.
3350Sstevel@tonic-gate 	 * This ensures that no T_CONN_IND remain and that no T_EXDATA_IND
3360Sstevel@tonic-gate 	 * remain; those types of messages would confuse sockfs.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	strflushrq(vp, FLUSHALL);
3390Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
3400Sstevel@tonic-gate 
3410Sstevel@tonic-gate 	/*
3428348SEric.Yu@Sun.COM 	 * Flush the T_DISCON_IND on sti_discon_ind_mp.
3430Sstevel@tonic-gate 	 */
3440Sstevel@tonic-gate 	so_flush_discon_ind(so);
3450Sstevel@tonic-gate 	so_unlock_read(so);	/* Clear SOREADLOCKED */
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
3480Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
3490Sstevel@tonic-gate }
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate /*
3520Sstevel@tonic-gate  * Install the hooks in the stream head.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate void
3550Sstevel@tonic-gate so_installhooks(struct sonode *so)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	strsetrputhooks(vp, SH_SIGALLDATA | SH_IGN_ZEROLEN | SH_CONSOL_DATA,
3600Sstevel@tonic-gate 	    strsock_proto, strsock_misc);
3610Sstevel@tonic-gate 	strsetwputhooks(vp, SH_SIGPIPE | SH_RECHECK_ERR, 0);
3620Sstevel@tonic-gate }
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate /*
3650Sstevel@tonic-gate  * Remove the hooks in the stream head.
3660Sstevel@tonic-gate  */
3670Sstevel@tonic-gate static void
3680Sstevel@tonic-gate so_removehooks(struct sonode *so)
3690Sstevel@tonic-gate {
3700Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	strsetrputhooks(vp, 0, NULL, NULL);
3730Sstevel@tonic-gate 	strsetwputhooks(vp, 0, STRTIMOUT);
3740Sstevel@tonic-gate 	/*
3750Sstevel@tonic-gate 	 * Leave read behavior as it would have been for a normal
3760Sstevel@tonic-gate 	 * stream i.e. a read of an M_PROTO will fail.
3770Sstevel@tonic-gate 	 */
3780Sstevel@tonic-gate }
3790Sstevel@tonic-gate 
3808348SEric.Yu@Sun.COM void
3818348SEric.Yu@Sun.COM so_basic_strinit(struct sonode *so)
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
3840Sstevel@tonic-gate 	struct stdata *stp;
3850Sstevel@tonic-gate 	mblk_t *mp;
3868348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
3870Sstevel@tonic-gate 
3880Sstevel@tonic-gate 	/* Preallocate an unbind_req message */
3898778SErik.Nordmark@Sun.COM 	mp = soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP, CRED());
3900Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
3918348SEric.Yu@Sun.COM 	sti->sti_unbind_mp = mp;
3920Sstevel@tonic-gate #ifdef DEBUG
3930Sstevel@tonic-gate 	so->so_options = so_default_options;
3940Sstevel@tonic-gate #endif /* DEBUG */
3950Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	so_installhooks(so);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	stp = vp->v_stream;
4000Sstevel@tonic-gate 	/*
4010Sstevel@tonic-gate 	 * Have to keep minpsz at zero in order to allow write/send of zero
4020Sstevel@tonic-gate 	 * bytes.
4030Sstevel@tonic-gate 	 */
4040Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
4050Sstevel@tonic-gate 	if (stp->sd_qn_minpsz == 1)
4060Sstevel@tonic-gate 		stp->sd_qn_minpsz = 0;
4070Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
4088348SEric.Yu@Sun.COM }
4096707Sbrutus 
4108348SEric.Yu@Sun.COM /*
4118348SEric.Yu@Sun.COM  * Initialize the streams side of a socket including
4128348SEric.Yu@Sun.COM  * T_info_req/ack processing. If tso is not NULL its values are used thereby
4138348SEric.Yu@Sun.COM  * avoiding the T_INFO_REQ.
4148348SEric.Yu@Sun.COM  */
4158348SEric.Yu@Sun.COM int
4168348SEric.Yu@Sun.COM so_strinit(struct sonode *so, struct sonode *tso)
4178348SEric.Yu@Sun.COM {
4188348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
4198348SEric.Yu@Sun.COM 	sotpi_info_t *tsti;
4208348SEric.Yu@Sun.COM 	int error;
4218348SEric.Yu@Sun.COM 
4228348SEric.Yu@Sun.COM 	so_basic_strinit(so);
4238348SEric.Yu@Sun.COM 
4248348SEric.Yu@Sun.COM 	/*
4258348SEric.Yu@Sun.COM 	 * The T_CAPABILITY_REQ should be the first message sent down because
4268348SEric.Yu@Sun.COM 	 * at least TCP has a fast-path for this which avoids timeouts while
4278348SEric.Yu@Sun.COM 	 * waiting for the T_CAPABILITY_ACK under high system load.
4288348SEric.Yu@Sun.COM 	 */
4298348SEric.Yu@Sun.COM 	if (tso == NULL) {
4308348SEric.Yu@Sun.COM 		error = do_tcapability(so, TC1_ACCEPTOR_ID | TC1_INFO);
4318348SEric.Yu@Sun.COM 		if (error)
4328348SEric.Yu@Sun.COM 			return (error);
4338348SEric.Yu@Sun.COM 	} else {
4348348SEric.Yu@Sun.COM 		tsti = SOTOTPI(tso);
4356707Sbrutus 
4368348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
4378348SEric.Yu@Sun.COM 		sti->sti_tsdu_size = tsti->sti_tsdu_size;
4388348SEric.Yu@Sun.COM 		sti->sti_etsdu_size = tsti->sti_etsdu_size;
4398348SEric.Yu@Sun.COM 		sti->sti_addr_size = tsti->sti_addr_size;
4408348SEric.Yu@Sun.COM 		sti->sti_opt_size = tsti->sti_opt_size;
4418348SEric.Yu@Sun.COM 		sti->sti_tidu_size = tsti->sti_tidu_size;
4428348SEric.Yu@Sun.COM 		sti->sti_serv_type = tsti->sti_serv_type;
4438348SEric.Yu@Sun.COM 		so->so_mode = tso->so_mode & ~SM_ACCEPTOR_ID;
4448348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
4458348SEric.Yu@Sun.COM 
4468348SEric.Yu@Sun.COM 		/* the following do_tcapability may update so->so_mode */
4478348SEric.Yu@Sun.COM 		if ((tsti->sti_serv_type != T_CLTS) &&
4488348SEric.Yu@Sun.COM 		    (sti->sti_direct == 0)) {
4498348SEric.Yu@Sun.COM 			error = do_tcapability(so, TC1_ACCEPTOR_ID);
4508348SEric.Yu@Sun.COM 			if (error)
4518348SEric.Yu@Sun.COM 				return (error);
4528348SEric.Yu@Sun.COM 		}
4536707Sbrutus 	}
4548348SEric.Yu@Sun.COM 	/*
4558348SEric.Yu@Sun.COM 	 * If the addr_size is 0 we treat it as already bound
4568348SEric.Yu@Sun.COM 	 * and connected. This is used by the routing socket.
4578348SEric.Yu@Sun.COM 	 * We set the addr_size to something to allocate a the address
4588348SEric.Yu@Sun.COM 	 * structures.
4598348SEric.Yu@Sun.COM 	 */
4608348SEric.Yu@Sun.COM 	if (sti->sti_addr_size == 0) {
4618348SEric.Yu@Sun.COM 		so->so_state |= SS_ISBOUND | SS_ISCONNECTED;
4628348SEric.Yu@Sun.COM 		/* Address size can vary with address families. */
4638348SEric.Yu@Sun.COM 		if (so->so_family == AF_INET6)
4648348SEric.Yu@Sun.COM 			sti->sti_addr_size =
4658348SEric.Yu@Sun.COM 			    (t_scalar_t)sizeof (struct sockaddr_in6);
4668348SEric.Yu@Sun.COM 		else
4678348SEric.Yu@Sun.COM 			sti->sti_addr_size =
4688348SEric.Yu@Sun.COM 			    (t_scalar_t)sizeof (struct sockaddr_in);
4698348SEric.Yu@Sun.COM 		ASSERT(sti->sti_unbind_mp);
4708348SEric.Yu@Sun.COM 	}
4718348SEric.Yu@Sun.COM 
4728348SEric.Yu@Sun.COM 	so_alloc_addr(so, sti->sti_addr_size);
4736707Sbrutus 
4740Sstevel@tonic-gate 	return (0);
4750Sstevel@tonic-gate }
4760Sstevel@tonic-gate 
4770Sstevel@tonic-gate static void
4780Sstevel@tonic-gate copy_tinfo(struct sonode *so, struct T_info_ack *tia)
4790Sstevel@tonic-gate {
4808348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
4818348SEric.Yu@Sun.COM 
4828348SEric.Yu@Sun.COM 	sti->sti_tsdu_size = tia->TSDU_size;
4838348SEric.Yu@Sun.COM 	sti->sti_etsdu_size = tia->ETSDU_size;
4848348SEric.Yu@Sun.COM 	sti->sti_addr_size = tia->ADDR_size;
4858348SEric.Yu@Sun.COM 	sti->sti_opt_size = tia->OPT_size;
4868348SEric.Yu@Sun.COM 	sti->sti_tidu_size = tia->TIDU_size;
4878348SEric.Yu@Sun.COM 	sti->sti_serv_type = tia->SERV_type;
4880Sstevel@tonic-gate 	switch (tia->CURRENT_state) {
4890Sstevel@tonic-gate 	case TS_UNBND:
4900Sstevel@tonic-gate 		break;
4910Sstevel@tonic-gate 	case TS_IDLE:
4920Sstevel@tonic-gate 		so->so_state |= SS_ISBOUND;
4938348SEric.Yu@Sun.COM 		sti->sti_laddr_len = 0;
4948348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
4950Sstevel@tonic-gate 		break;
4960Sstevel@tonic-gate 	case TS_DATA_XFER:
4970Sstevel@tonic-gate 		so->so_state |= SS_ISBOUND|SS_ISCONNECTED;
4988348SEric.Yu@Sun.COM 		sti->sti_laddr_len = 0;
4998348SEric.Yu@Sun.COM 		sti->sti_faddr_len = 0;
5008348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
5018348SEric.Yu@Sun.COM 		sti->sti_faddr_valid = 0;
5020Sstevel@tonic-gate 		break;
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate 	/*
5060Sstevel@tonic-gate 	 * Heuristics for determining the socket mode flags
5070Sstevel@tonic-gate 	 * (SM_ATOMIC, SM_CONNREQUIRED, SM_ADDR, SM_FDPASSING,
5080Sstevel@tonic-gate 	 * and SM_EXDATA, SM_OPTDATA, and SM_BYTESTREAM)
5090Sstevel@tonic-gate 	 * from the info ack.
5100Sstevel@tonic-gate 	 */
5118348SEric.Yu@Sun.COM 	if (sti->sti_serv_type == T_CLTS) {
5120Sstevel@tonic-gate 		so->so_mode |= SM_ATOMIC | SM_ADDR;
5130Sstevel@tonic-gate 	} else {
5140Sstevel@tonic-gate 		so->so_mode |= SM_CONNREQUIRED;
5158348SEric.Yu@Sun.COM 		if (sti->sti_etsdu_size != 0 && sti->sti_etsdu_size != -2)
5160Sstevel@tonic-gate 			so->so_mode |= SM_EXDATA;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 	if (so->so_type == SOCK_SEQPACKET || so->so_type == SOCK_RAW) {
5190Sstevel@tonic-gate 		/* Semantics are to discard tail end of messages */
5200Sstevel@tonic-gate 		so->so_mode |= SM_ATOMIC;
5210Sstevel@tonic-gate 	}
5220Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
5230Sstevel@tonic-gate 		so->so_mode |= SM_FDPASSING | SM_OPTDATA;
5248348SEric.Yu@Sun.COM 		if (sti->sti_addr_size == -1) {
5250Sstevel@tonic-gate 			/* MAXPATHLEN + soun_family + nul termination */
5268348SEric.Yu@Sun.COM 			sti->sti_addr_size = (t_scalar_t)(MAXPATHLEN +
5275753Sgww 			    sizeof (short) + 1);
5280Sstevel@tonic-gate 		}
5290Sstevel@tonic-gate 		if (so->so_type == SOCK_STREAM) {
5300Sstevel@tonic-gate 			/*
5310Sstevel@tonic-gate 			 * Make it into a byte-stream transport.
5320Sstevel@tonic-gate 			 * SOCK_SEQPACKET sockets are unchanged.
5330Sstevel@tonic-gate 			 */
5348348SEric.Yu@Sun.COM 			sti->sti_tsdu_size = 0;
5350Sstevel@tonic-gate 		}
5368348SEric.Yu@Sun.COM 	} else if (sti->sti_addr_size == -1) {
5370Sstevel@tonic-gate 		/*
5380Sstevel@tonic-gate 		 * Logic extracted from sockmod - have to pick some max address
5390Sstevel@tonic-gate 		 * length in order to preallocate the addresses.
5400Sstevel@tonic-gate 		 */
5418348SEric.Yu@Sun.COM 		sti->sti_addr_size = SOA_DEFSIZE;
5420Sstevel@tonic-gate 	}
5438348SEric.Yu@Sun.COM 	if (sti->sti_tsdu_size == 0)
5440Sstevel@tonic-gate 		so->so_mode |= SM_BYTESTREAM;
5450Sstevel@tonic-gate }
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate static int
5480Sstevel@tonic-gate check_tinfo(struct sonode *so)
5490Sstevel@tonic-gate {
5508348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
5518348SEric.Yu@Sun.COM 
5520Sstevel@tonic-gate 	/* Consistency checks */
5538348SEric.Yu@Sun.COM 	if (so->so_type == SOCK_DGRAM && sti->sti_serv_type != T_CLTS) {
5540Sstevel@tonic-gate 		eprintso(so, ("service type and socket type mismatch\n"));
5550Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
5560Sstevel@tonic-gate 		return (EPROTO);
5570Sstevel@tonic-gate 	}
5588348SEric.Yu@Sun.COM 	if (so->so_type == SOCK_STREAM && sti->sti_serv_type == T_CLTS) {
5590Sstevel@tonic-gate 		eprintso(so, ("service type and socket type mismatch\n"));
5600Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
5610Sstevel@tonic-gate 		return (EPROTO);
5620Sstevel@tonic-gate 	}
5638348SEric.Yu@Sun.COM 	if (so->so_type == SOCK_SEQPACKET && sti->sti_serv_type == T_CLTS) {
5640Sstevel@tonic-gate 		eprintso(so, ("service type and socket type mismatch\n"));
5650Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
5660Sstevel@tonic-gate 		return (EPROTO);
5670Sstevel@tonic-gate 	}
5680Sstevel@tonic-gate 	if (so->so_family == AF_INET &&
5698348SEric.Yu@Sun.COM 	    sti->sti_addr_size != (t_scalar_t)sizeof (struct sockaddr_in)) {
5700Sstevel@tonic-gate 		eprintso(so,
5710Sstevel@tonic-gate 		    ("AF_INET must have sockaddr_in address length. Got %d\n",
5728348SEric.Yu@Sun.COM 		    sti->sti_addr_size));
5730Sstevel@tonic-gate 		eprintsoline(so, EMSGSIZE);
5740Sstevel@tonic-gate 		return (EMSGSIZE);
5750Sstevel@tonic-gate 	}
5760Sstevel@tonic-gate 	if (so->so_family == AF_INET6 &&
5778348SEric.Yu@Sun.COM 	    sti->sti_addr_size != (t_scalar_t)sizeof (struct sockaddr_in6)) {
5780Sstevel@tonic-gate 		eprintso(so,
5790Sstevel@tonic-gate 		    ("AF_INET6 must have sockaddr_in6 address length. Got %d\n",
5808348SEric.Yu@Sun.COM 		    sti->sti_addr_size));
5810Sstevel@tonic-gate 		eprintsoline(so, EMSGSIZE);
5820Sstevel@tonic-gate 		return (EMSGSIZE);
5830Sstevel@tonic-gate 	}
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 	dprintso(so, 1, (
5860Sstevel@tonic-gate 	    "tinfo: serv %d tsdu %d, etsdu %d, addr %d, opt %d, tidu %d\n",
5878348SEric.Yu@Sun.COM 	    sti->sti_serv_type, sti->sti_tsdu_size, sti->sti_etsdu_size,
5888348SEric.Yu@Sun.COM 	    sti->sti_addr_size, sti->sti_opt_size,
5898348SEric.Yu@Sun.COM 	    sti->sti_tidu_size));
5900Sstevel@tonic-gate 	dprintso(so, 1, ("tinfo: so_state %s\n",
5915753Sgww 	    pr_state(so->so_state, so->so_mode)));
5920Sstevel@tonic-gate 	return (0);
5930Sstevel@tonic-gate }
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate /*
5960Sstevel@tonic-gate  * Send down T_info_req and wait for the ack.
5970Sstevel@tonic-gate  * Record interesting T_info_ack values in the sonode.
5980Sstevel@tonic-gate  */
5990Sstevel@tonic-gate static int
6000Sstevel@tonic-gate do_tinfo(struct sonode *so)
6010Sstevel@tonic-gate {
6020Sstevel@tonic-gate 	struct T_info_req tir;
6030Sstevel@tonic-gate 	mblk_t *mp;
6040Sstevel@tonic-gate 	int error;
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	if (so_no_tinfo) {
6098348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_addr_size = 0;
6100Sstevel@tonic-gate 		return (0);
6110Sstevel@tonic-gate 	}
6120Sstevel@tonic-gate 
6137240Srh87107 	dprintso(so, 1, ("do_tinfo(%p)\n", (void *)so));
6140Sstevel@tonic-gate 
6150Sstevel@tonic-gate 	/* Send T_INFO_REQ */
6160Sstevel@tonic-gate 	tir.PRIM_type = T_INFO_REQ;
6170Sstevel@tonic-gate 	mp = soallocproto1(&tir, sizeof (tir),
6180Sstevel@tonic-gate 	    sizeof (struct T_info_req) + sizeof (struct T_info_ack),
6198778SErik.Nordmark@Sun.COM 	    _ALLOC_INTR, CRED());
6200Sstevel@tonic-gate 	if (mp == NULL) {
6210Sstevel@tonic-gate 		eprintsoline(so, ENOBUFS);
6220Sstevel@tonic-gate 		return (ENOBUFS);
6230Sstevel@tonic-gate 	}
6240Sstevel@tonic-gate 	/* T_INFO_REQ has to be M_PCPROTO */
6250Sstevel@tonic-gate 	DB_TYPE(mp) = M_PCPROTO;
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
6285753Sgww 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
6290Sstevel@tonic-gate 	if (error) {
6300Sstevel@tonic-gate 		eprintsoline(so, error);
6310Sstevel@tonic-gate 		return (error);
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
6340Sstevel@tonic-gate 	/* Wait for T_INFO_ACK */
6350Sstevel@tonic-gate 	if ((error = sowaitprim(so, T_INFO_REQ, T_INFO_ACK,
6360Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (struct T_info_ack), &mp, 0))) {
6370Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
6380Sstevel@tonic-gate 		eprintsoline(so, error);
6390Sstevel@tonic-gate 		return (error);
6400Sstevel@tonic-gate 	}
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	ASSERT(mp);
6430Sstevel@tonic-gate 	copy_tinfo(so, (struct T_info_ack *)mp->b_rptr);
6440Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
6450Sstevel@tonic-gate 	freemsg(mp);
6460Sstevel@tonic-gate 	return (check_tinfo(so));
6470Sstevel@tonic-gate }
6480Sstevel@tonic-gate 
6490Sstevel@tonic-gate /*
6500Sstevel@tonic-gate  * Send down T_capability_req and wait for the ack.
6510Sstevel@tonic-gate  * Record interesting T_capability_ack values in the sonode.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate static int
6540Sstevel@tonic-gate do_tcapability(struct sonode *so, t_uscalar_t cap_bits1)
6550Sstevel@tonic-gate {
6560Sstevel@tonic-gate 	struct T_capability_req tcr;
6570Sstevel@tonic-gate 	struct T_capability_ack *tca;
6580Sstevel@tonic-gate 	mblk_t *mp;
6590Sstevel@tonic-gate 	int error;
6608348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
6610Sstevel@tonic-gate 
6620Sstevel@tonic-gate 	ASSERT(cap_bits1 != 0);
6630Sstevel@tonic-gate 	ASSERT((cap_bits1 & ~(TC1_ACCEPTOR_ID | TC1_INFO)) == 0);
6640Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
6650Sstevel@tonic-gate 
6668348SEric.Yu@Sun.COM 	if (sti->sti_provinfo->tpi_capability == PI_NO)
6670Sstevel@tonic-gate 		return (do_tinfo(so));
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if (so_no_tinfo) {
6708348SEric.Yu@Sun.COM 		sti->sti_addr_size = 0;
6710Sstevel@tonic-gate 		if ((cap_bits1 &= ~TC1_INFO) == 0)
6720Sstevel@tonic-gate 			return (0);
6730Sstevel@tonic-gate 	}
6740Sstevel@tonic-gate 
6757240Srh87107 	dprintso(so, 1, ("do_tcapability(%p)\n", (void *)so));
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	/* Send T_CAPABILITY_REQ */
6780Sstevel@tonic-gate 	tcr.PRIM_type = T_CAPABILITY_REQ;
6790Sstevel@tonic-gate 	tcr.CAP_bits1 = cap_bits1;
6800Sstevel@tonic-gate 	mp = soallocproto1(&tcr, sizeof (tcr),
6810Sstevel@tonic-gate 	    sizeof (struct T_capability_req) + sizeof (struct T_capability_ack),
6828778SErik.Nordmark@Sun.COM 	    _ALLOC_INTR, CRED());
6830Sstevel@tonic-gate 	if (mp == NULL) {
6840Sstevel@tonic-gate 		eprintsoline(so, ENOBUFS);
6850Sstevel@tonic-gate 		return (ENOBUFS);
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 	/* T_CAPABILITY_REQ should be M_PCPROTO here */
6880Sstevel@tonic-gate 	DB_TYPE(mp) = M_PCPROTO;
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
6910Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
6920Sstevel@tonic-gate 	if (error) {
6930Sstevel@tonic-gate 		eprintsoline(so, error);
6940Sstevel@tonic-gate 		return (error);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
6970Sstevel@tonic-gate 	/* Wait for T_CAPABILITY_ACK */
6980Sstevel@tonic-gate 	if ((error = sowaitprim(so, T_CAPABILITY_REQ, T_CAPABILITY_ACK,
6990Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (*tca), &mp, sock_capability_timeout * hz))) {
7000Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
7018348SEric.Yu@Sun.COM 		PI_PROVLOCK(sti->sti_provinfo);
7028348SEric.Yu@Sun.COM 		if (sti->sti_provinfo->tpi_capability == PI_DONTKNOW)
7038348SEric.Yu@Sun.COM 			sti->sti_provinfo->tpi_capability = PI_NO;
7048348SEric.Yu@Sun.COM 		PI_PROVUNLOCK(sti->sti_provinfo);
7050Sstevel@tonic-gate 		ASSERT((so->so_mode & SM_ACCEPTOR_ID) == 0);
7060Sstevel@tonic-gate 		if (cap_bits1 & TC1_INFO) {
7070Sstevel@tonic-gate 			/*
7080Sstevel@tonic-gate 			 * If the T_CAPABILITY_REQ timed out and then a
7090Sstevel@tonic-gate 			 * T_INFO_REQ gets a protocol error, most likely
7100Sstevel@tonic-gate 			 * the capability was slow (vs. unsupported). Return
7110Sstevel@tonic-gate 			 * ENOSR for this case as a best guess.
7120Sstevel@tonic-gate 			 */
7130Sstevel@tonic-gate 			if (error == ETIME) {
7140Sstevel@tonic-gate 				return ((error = do_tinfo(so)) == EPROTO ?
7150Sstevel@tonic-gate 				    ENOSR : error);
7160Sstevel@tonic-gate 			}
7170Sstevel@tonic-gate 			return (do_tinfo(so));
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 		return (0);
7200Sstevel@tonic-gate 	}
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	ASSERT(mp);
7230Sstevel@tonic-gate 	tca = (struct T_capability_ack *)mp->b_rptr;
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate 	ASSERT((cap_bits1 & TC1_INFO) == (tca->CAP_bits1 & TC1_INFO));
7268348SEric.Yu@Sun.COM 	so_proc_tcapability_ack(so, tca);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	cap_bits1 = tca->CAP_bits1;
7290Sstevel@tonic-gate 
7300Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
7310Sstevel@tonic-gate 	freemsg(mp);
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 	if (cap_bits1 & TC1_INFO)
7340Sstevel@tonic-gate 		return (check_tinfo(so));
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	return (0);
7370Sstevel@tonic-gate }
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate /*
7408348SEric.Yu@Sun.COM  * Process a T_CAPABILITY_ACK
7418348SEric.Yu@Sun.COM  */
7428348SEric.Yu@Sun.COM void
7438348SEric.Yu@Sun.COM so_proc_tcapability_ack(struct sonode *so, struct T_capability_ack *tca)
7448348SEric.Yu@Sun.COM {
7458348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
7468348SEric.Yu@Sun.COM 
7478348SEric.Yu@Sun.COM 	if (sti->sti_provinfo->tpi_capability == PI_DONTKNOW) {
7488348SEric.Yu@Sun.COM 		PI_PROVLOCK(sti->sti_provinfo);
7498348SEric.Yu@Sun.COM 		sti->sti_provinfo->tpi_capability = PI_YES;
7508348SEric.Yu@Sun.COM 		PI_PROVUNLOCK(sti->sti_provinfo);
7518348SEric.Yu@Sun.COM 	}
7528348SEric.Yu@Sun.COM 
7538348SEric.Yu@Sun.COM 	if (tca->CAP_bits1 & TC1_ACCEPTOR_ID) {
7548348SEric.Yu@Sun.COM 		sti->sti_acceptor_id = tca->ACCEPTOR_id;
7558348SEric.Yu@Sun.COM 		so->so_mode |= SM_ACCEPTOR_ID;
7568348SEric.Yu@Sun.COM 	}
7578348SEric.Yu@Sun.COM 
7588348SEric.Yu@Sun.COM 	if (tca->CAP_bits1 & TC1_INFO)
7598348SEric.Yu@Sun.COM 		copy_tinfo(so, &tca->INFO_ack);
7608348SEric.Yu@Sun.COM }
7618348SEric.Yu@Sun.COM 
7628348SEric.Yu@Sun.COM /*
7638348SEric.Yu@Sun.COM  * Retrieve socket error, clear error if not peek.
7640Sstevel@tonic-gate  */
7650Sstevel@tonic-gate int
7668348SEric.Yu@Sun.COM sogeterr(struct sonode *so, boolean_t clear_err)
7670Sstevel@tonic-gate {
7680Sstevel@tonic-gate 	int error;
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	error = so->so_error;
7738348SEric.Yu@Sun.COM 	if (clear_err)
7748348SEric.Yu@Sun.COM 		so->so_error = 0;
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate 	return (error);
7770Sstevel@tonic-gate }
7780Sstevel@tonic-gate 
7790Sstevel@tonic-gate /*
7800Sstevel@tonic-gate  * This routine is registered with the stream head to retrieve read
7810Sstevel@tonic-gate  * side errors.
7820Sstevel@tonic-gate  * It does not clear the socket error for a peeking read side operation.
7830Sstevel@tonic-gate  * It the error is to be cleared it sets *clearerr.
7840Sstevel@tonic-gate  */
7850Sstevel@tonic-gate int
7860Sstevel@tonic-gate sogetrderr(vnode_t *vp, int ispeek, int *clearerr)
7870Sstevel@tonic-gate {
7880Sstevel@tonic-gate 	struct sonode *so = VTOSO(vp);
7890Sstevel@tonic-gate 	int error;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
7920Sstevel@tonic-gate 	if (ispeek) {
7930Sstevel@tonic-gate 		error = so->so_error;
7940Sstevel@tonic-gate 		*clearerr = 0;
7950Sstevel@tonic-gate 	} else {
7960Sstevel@tonic-gate 		error = so->so_error;
7970Sstevel@tonic-gate 		so->so_error = 0;
7980Sstevel@tonic-gate 		*clearerr = 1;
7990Sstevel@tonic-gate 	}
8000Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
8010Sstevel@tonic-gate 	return (error);
8020Sstevel@tonic-gate }
8030Sstevel@tonic-gate 
8040Sstevel@tonic-gate /*
8050Sstevel@tonic-gate  * This routine is registered with the stream head to retrieve write
8060Sstevel@tonic-gate  * side errors.
8070Sstevel@tonic-gate  * It does not clear the socket error for a peeking read side operation.
8080Sstevel@tonic-gate  * It the error is to be cleared it sets *clearerr.
8090Sstevel@tonic-gate  */
8100Sstevel@tonic-gate int
8110Sstevel@tonic-gate sogetwrerr(vnode_t *vp, int ispeek, int *clearerr)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	struct sonode *so = VTOSO(vp);
8140Sstevel@tonic-gate 	int error;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
8170Sstevel@tonic-gate 	if (so->so_state & SS_CANTSENDMORE) {
8180Sstevel@tonic-gate 		error = EPIPE;
8190Sstevel@tonic-gate 		*clearerr = 0;
8200Sstevel@tonic-gate 	} else {
8210Sstevel@tonic-gate 		error = so->so_error;
8220Sstevel@tonic-gate 		if (ispeek) {
8230Sstevel@tonic-gate 			*clearerr = 0;
8240Sstevel@tonic-gate 		} else {
8250Sstevel@tonic-gate 			so->so_error = 0;
8260Sstevel@tonic-gate 			*clearerr = 1;
8270Sstevel@tonic-gate 		}
8280Sstevel@tonic-gate 	}
8290Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
8300Sstevel@tonic-gate 	return (error);
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /*
8340Sstevel@tonic-gate  * Set a nonpersistent read and write error on the socket.
8350Sstevel@tonic-gate  * Used when there is a T_uderror_ind for a connected socket.
8360Sstevel@tonic-gate  * The caller also needs to call strsetrerror and strsetwerror
8370Sstevel@tonic-gate  * after dropping the lock.
8380Sstevel@tonic-gate  */
8390Sstevel@tonic-gate void
8400Sstevel@tonic-gate soseterror(struct sonode *so, int error)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	ASSERT(error != 0);
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8450Sstevel@tonic-gate 	so->so_error = (ushort_t)error;
8460Sstevel@tonic-gate }
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate void
8490Sstevel@tonic-gate soisconnecting(struct sonode *so)
8500Sstevel@tonic-gate {
8510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8520Sstevel@tonic-gate 	so->so_state &= ~(SS_ISCONNECTED|SS_ISDISCONNECTING);
8530Sstevel@tonic-gate 	so->so_state |= SS_ISCONNECTING;
8540Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate void
8580Sstevel@tonic-gate soisconnected(struct sonode *so)
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8610Sstevel@tonic-gate 	so->so_state &= ~(SS_ISCONNECTING|SS_ISDISCONNECTING);
8620Sstevel@tonic-gate 	so->so_state |= SS_ISCONNECTED;
8630Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8640Sstevel@tonic-gate }
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate /*
8670Sstevel@tonic-gate  * The caller also needs to call strsetrerror, strsetwerror and strseteof.
8680Sstevel@tonic-gate  */
8690Sstevel@tonic-gate void
8700Sstevel@tonic-gate soisdisconnected(struct sonode *so, int error)
8710Sstevel@tonic-gate {
8720Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8738348SEric.Yu@Sun.COM 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
8740Sstevel@tonic-gate 	so->so_state |= (SS_CANTRCVMORE|SS_CANTSENDMORE);
8750Sstevel@tonic-gate 	so->so_error = (ushort_t)error;
8760Sstevel@tonic-gate 	if (so->so_peercred != NULL) {
8770Sstevel@tonic-gate 		crfree(so->so_peercred);
8780Sstevel@tonic-gate 		so->so_peercred = NULL;
8790Sstevel@tonic-gate 	}
8800Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
8830Sstevel@tonic-gate /*
8840Sstevel@tonic-gate  * For connected AF_UNIX SOCK_DGRAM sockets when the peer closes.
8850Sstevel@tonic-gate  * Does not affect write side.
8860Sstevel@tonic-gate  * The caller also has to call strsetrerror.
8870Sstevel@tonic-gate  */
8880Sstevel@tonic-gate static void
8890Sstevel@tonic-gate sobreakconn(struct sonode *so, int error)
8900Sstevel@tonic-gate {
8910Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8920Sstevel@tonic-gate 	so->so_state &= ~(SS_ISCONNECTING|SS_ISCONNECTED|SS_ISDISCONNECTING);
8930Sstevel@tonic-gate 	so->so_error = (ushort_t)error;
8940Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
8950Sstevel@tonic-gate }
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /*
8980Sstevel@tonic-gate  * Can no longer send.
8990Sstevel@tonic-gate  * Caller must also call strsetwerror.
9000Sstevel@tonic-gate  *
9010Sstevel@tonic-gate  * We mark the peer address as no longer valid for getpeername, but
9020Sstevel@tonic-gate  * leave it around for so_unix_close to notify the peer (that
9030Sstevel@tonic-gate  * transport has no addressing held at that layer).
9040Sstevel@tonic-gate  */
9050Sstevel@tonic-gate void
9060Sstevel@tonic-gate socantsendmore(struct sonode *so)
9070Sstevel@tonic-gate {
9080Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
9098348SEric.Yu@Sun.COM 	so->so_state |= SS_CANTSENDMORE;
9100Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate /*
9140Sstevel@tonic-gate  * The caller must call strseteof(,1) as well as this routine
9150Sstevel@tonic-gate  * to change the socket state.
9160Sstevel@tonic-gate  */
9170Sstevel@tonic-gate void
9180Sstevel@tonic-gate socantrcvmore(struct sonode *so)
9190Sstevel@tonic-gate {
9200Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
9210Sstevel@tonic-gate 	so->so_state |= SS_CANTRCVMORE;
9220Sstevel@tonic-gate 	cv_broadcast(&so->so_state_cv);
9230Sstevel@tonic-gate }
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate /*
9260Sstevel@tonic-gate  * The caller has sent down a "request_prim" primitive and wants to wait for
9270Sstevel@tonic-gate  * an ack ("ack_prim") or an T_ERROR_ACK for it.
9280Sstevel@tonic-gate  * The specified "ack_prim" can be a T_OK_ACK.
9290Sstevel@tonic-gate  *
9300Sstevel@tonic-gate  * Assumes that all the TPI acks are M_PCPROTO messages.
9310Sstevel@tonic-gate  *
9320Sstevel@tonic-gate  * Note that the socket is single-threaded (using so_lock_single)
9330Sstevel@tonic-gate  * for all operations that generate TPI ack messages. Since
9340Sstevel@tonic-gate  * only TPI ack messages are M_PCPROTO we should never receive
9350Sstevel@tonic-gate  * anything except either the ack we are expecting or a T_ERROR_ACK
9360Sstevel@tonic-gate  * for the same primitive.
9370Sstevel@tonic-gate  */
9380Sstevel@tonic-gate int
9390Sstevel@tonic-gate sowaitprim(struct sonode *so, t_scalar_t request_prim, t_scalar_t ack_prim,
9400Sstevel@tonic-gate 	    t_uscalar_t min_size, mblk_t **mpp, clock_t wait)
9410Sstevel@tonic-gate {
9420Sstevel@tonic-gate 	mblk_t *mp;
9430Sstevel@tonic-gate 	union T_primitives *tpr;
9440Sstevel@tonic-gate 	int error;
9450Sstevel@tonic-gate 
9460Sstevel@tonic-gate 	dprintso(so, 1, ("sowaitprim(%p, %d, %d, %d, %p, %lu)\n",
9477240Srh87107 	    (void *)so, request_prim, ack_prim, min_size, (void *)mpp, wait));
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
9500Sstevel@tonic-gate 
9510Sstevel@tonic-gate 	error = sowaitack(so, &mp, wait);
9520Sstevel@tonic-gate 	if (error)
9530Sstevel@tonic-gate 		return (error);
9540Sstevel@tonic-gate 
9557240Srh87107 	dprintso(so, 1, ("got msg %p\n", (void *)mp));
9560Sstevel@tonic-gate 	if (DB_TYPE(mp) != M_PCPROTO ||
9570Sstevel@tonic-gate 	    MBLKL(mp) < sizeof (tpr->type)) {
9580Sstevel@tonic-gate 		freemsg(mp);
9590Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
9600Sstevel@tonic-gate 		return (EPROTO);
9610Sstevel@tonic-gate 	}
9620Sstevel@tonic-gate 	tpr = (union T_primitives *)mp->b_rptr;
9630Sstevel@tonic-gate 	/*
9640Sstevel@tonic-gate 	 * Did we get the primitive that we were asking for?
9650Sstevel@tonic-gate 	 * For T_OK_ACK we also check that it matches the request primitive.
9660Sstevel@tonic-gate 	 */
9670Sstevel@tonic-gate 	if (tpr->type == ack_prim &&
9680Sstevel@tonic-gate 	    (ack_prim != T_OK_ACK ||
9690Sstevel@tonic-gate 	    tpr->ok_ack.CORRECT_prim == request_prim)) {
9700Sstevel@tonic-gate 		if (MBLKL(mp) >= (ssize_t)min_size) {
9710Sstevel@tonic-gate 			/* Found what we are looking for */
9720Sstevel@tonic-gate 			*mpp = mp;
9730Sstevel@tonic-gate 			return (0);
9740Sstevel@tonic-gate 		}
9750Sstevel@tonic-gate 		/* Too short */
9760Sstevel@tonic-gate 		freemsg(mp);
9770Sstevel@tonic-gate 		eprintsoline(so, EPROTO);
9780Sstevel@tonic-gate 		return (EPROTO);
9790Sstevel@tonic-gate 	}
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	if (tpr->type == T_ERROR_ACK &&
9820Sstevel@tonic-gate 	    tpr->error_ack.ERROR_prim == request_prim) {
9830Sstevel@tonic-gate 		/* Error to the primitive we were looking for */
9840Sstevel@tonic-gate 		if (tpr->error_ack.TLI_error == TSYSERR) {
9850Sstevel@tonic-gate 			error = tpr->error_ack.UNIX_error;
9860Sstevel@tonic-gate 		} else {
9878348SEric.Yu@Sun.COM 			error = proto_tlitosyserr(tpr->error_ack.TLI_error);
9880Sstevel@tonic-gate 		}
9890Sstevel@tonic-gate 		dprintso(so, 0, ("error_ack for %d: %d/%d ->%d\n",
9908348SEric.Yu@Sun.COM 		    tpr->error_ack.ERROR_prim, tpr->error_ack.TLI_error,
9918348SEric.Yu@Sun.COM 		    tpr->error_ack.UNIX_error, error));
9920Sstevel@tonic-gate 		freemsg(mp);
9930Sstevel@tonic-gate 		return (error);
9940Sstevel@tonic-gate 	}
9950Sstevel@tonic-gate 	/*
9960Sstevel@tonic-gate 	 * Wrong primitive or T_ERROR_ACK for the wrong primitive
9970Sstevel@tonic-gate 	 */
9980Sstevel@tonic-gate #ifdef DEBUG
9990Sstevel@tonic-gate 	if (tpr->type == T_ERROR_ACK) {
10000Sstevel@tonic-gate 		dprintso(so, 0, ("error_ack for %d: %d/%d\n",
10018348SEric.Yu@Sun.COM 		    tpr->error_ack.ERROR_prim, tpr->error_ack.TLI_error,
10025753Sgww 		    tpr->error_ack.UNIX_error));
10030Sstevel@tonic-gate 	} else if (tpr->type == T_OK_ACK) {
10040Sstevel@tonic-gate 		dprintso(so, 0, ("ok_ack for %d, expected %d for %d\n",
10058348SEric.Yu@Sun.COM 		    tpr->ok_ack.CORRECT_prim, ack_prim, request_prim));
10060Sstevel@tonic-gate 	} else {
10070Sstevel@tonic-gate 		dprintso(so, 0,
10085753Sgww 		    ("unexpected primitive %d, expected %d for %d\n",
10095753Sgww 		    tpr->type, ack_prim, request_prim));
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate #endif /* DEBUG */
10120Sstevel@tonic-gate 
10130Sstevel@tonic-gate 	freemsg(mp);
10140Sstevel@tonic-gate 	eprintsoline(so, EPROTO);
10150Sstevel@tonic-gate 	return (EPROTO);
10160Sstevel@tonic-gate }
10170Sstevel@tonic-gate 
10180Sstevel@tonic-gate /*
10190Sstevel@tonic-gate  * Wait for a T_OK_ACK for the specified primitive.
10200Sstevel@tonic-gate  */
10210Sstevel@tonic-gate int
10220Sstevel@tonic-gate sowaitokack(struct sonode *so, t_scalar_t request_prim)
10230Sstevel@tonic-gate {
10240Sstevel@tonic-gate 	mblk_t *mp;
10250Sstevel@tonic-gate 	int error;
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate 	error = sowaitprim(so, request_prim, T_OK_ACK,
10280Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (struct T_ok_ack), &mp, 0);
10290Sstevel@tonic-gate 	if (error)
10300Sstevel@tonic-gate 		return (error);
10310Sstevel@tonic-gate 	freemsg(mp);
10320Sstevel@tonic-gate 	return (0);
10330Sstevel@tonic-gate }
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate /*
10368348SEric.Yu@Sun.COM  * Queue a received TPI ack message on sti_ack_mp.
10370Sstevel@tonic-gate  */
10380Sstevel@tonic-gate void
10390Sstevel@tonic-gate soqueueack(struct sonode *so, mblk_t *mp)
10400Sstevel@tonic-gate {
10418348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
10428348SEric.Yu@Sun.COM 
10430Sstevel@tonic-gate 	if (DB_TYPE(mp) != M_PCPROTO) {
10441548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
10450Sstevel@tonic-gate 		    "sockfs: received unexpected M_PROTO TPI ack. Prim %d\n",
10460Sstevel@tonic-gate 		    *(t_scalar_t *)mp->b_rptr);
10470Sstevel@tonic-gate 		freemsg(mp);
10480Sstevel@tonic-gate 		return;
10490Sstevel@tonic-gate 	}
10500Sstevel@tonic-gate 
10510Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
10528348SEric.Yu@Sun.COM 	if (sti->sti_ack_mp != NULL) {
10538348SEric.Yu@Sun.COM 		dprintso(so, 1, ("sti_ack_mp already set\n"));
10548348SEric.Yu@Sun.COM 		freemsg(sti->sti_ack_mp);
10558348SEric.Yu@Sun.COM 		sti->sti_ack_mp = NULL;
10560Sstevel@tonic-gate 	}
10578348SEric.Yu@Sun.COM 	sti->sti_ack_mp = mp;
10588348SEric.Yu@Sun.COM 	cv_broadcast(&sti->sti_ack_cv);
10590Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
10600Sstevel@tonic-gate }
10610Sstevel@tonic-gate 
10620Sstevel@tonic-gate /*
10630Sstevel@tonic-gate  * Wait for a TPI ack ignoring signals and errors.
10640Sstevel@tonic-gate  */
10650Sstevel@tonic-gate int
10660Sstevel@tonic-gate sowaitack(struct sonode *so, mblk_t **mpp, clock_t wait)
10670Sstevel@tonic-gate {
10688348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
10698348SEric.Yu@Sun.COM 
10700Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
10710Sstevel@tonic-gate 
10728348SEric.Yu@Sun.COM 	while (sti->sti_ack_mp == NULL) {
10730Sstevel@tonic-gate #ifdef SOCK_TEST
10740Sstevel@tonic-gate 		if (wait == 0 && sock_test_timelimit != 0)
10750Sstevel@tonic-gate 			wait = sock_test_timelimit;
10760Sstevel@tonic-gate #endif
10770Sstevel@tonic-gate 		if (wait != 0) {
10780Sstevel@tonic-gate 			/*
10790Sstevel@tonic-gate 			 * Only wait for the time limit.
10800Sstevel@tonic-gate 			 */
108111066Srafael.vanoni@sun.com 			if (cv_reltimedwait(&sti->sti_ack_cv, &so->so_lock,
108211066Srafael.vanoni@sun.com 			    wait, TR_CLOCK_TICK) == -1) {
10830Sstevel@tonic-gate 				eprintsoline(so, ETIME);
10840Sstevel@tonic-gate 				return (ETIME);
10850Sstevel@tonic-gate 			}
10860Sstevel@tonic-gate 		}
10870Sstevel@tonic-gate 		else
10888348SEric.Yu@Sun.COM 			cv_wait(&sti->sti_ack_cv, &so->so_lock);
10890Sstevel@tonic-gate 	}
10908348SEric.Yu@Sun.COM 	*mpp = sti->sti_ack_mp;
10910Sstevel@tonic-gate #ifdef DEBUG
10920Sstevel@tonic-gate 	{
10930Sstevel@tonic-gate 		union T_primitives *tpr;
10940Sstevel@tonic-gate 		mblk_t *mp = *mpp;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		tpr = (union T_primitives *)mp->b_rptr;
10970Sstevel@tonic-gate 		ASSERT(DB_TYPE(mp) == M_PCPROTO);
10980Sstevel@tonic-gate 		ASSERT(tpr->type == T_OK_ACK ||
10995753Sgww 		    tpr->type == T_ERROR_ACK ||
11005753Sgww 		    tpr->type == T_BIND_ACK ||
11015753Sgww 		    tpr->type == T_CAPABILITY_ACK ||
11025753Sgww 		    tpr->type == T_INFO_ACK ||
11035753Sgww 		    tpr->type == T_OPTMGMT_ACK);
11040Sstevel@tonic-gate 	}
11050Sstevel@tonic-gate #endif /* DEBUG */
11068348SEric.Yu@Sun.COM 	sti->sti_ack_mp = NULL;
11070Sstevel@tonic-gate 	return (0);
11080Sstevel@tonic-gate }
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate /*
11118348SEric.Yu@Sun.COM  * Queue a received T_CONN_IND message on sti_conn_ind_head/tail.
11120Sstevel@tonic-gate  */
11130Sstevel@tonic-gate void
11140Sstevel@tonic-gate soqueueconnind(struct sonode *so, mblk_t *mp)
11150Sstevel@tonic-gate {
11168348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
11178348SEric.Yu@Sun.COM 
11180Sstevel@tonic-gate 	if (DB_TYPE(mp) != M_PROTO) {
11191548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
11200Sstevel@tonic-gate 		    "sockfs: received unexpected M_PCPROTO T_CONN_IND\n");
11210Sstevel@tonic-gate 		freemsg(mp);
11220Sstevel@tonic-gate 		return;
11230Sstevel@tonic-gate 	}
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
11260Sstevel@tonic-gate 	ASSERT(mp->b_next == NULL);
11278348SEric.Yu@Sun.COM 	if (sti->sti_conn_ind_head == NULL) {
11288348SEric.Yu@Sun.COM 		sti->sti_conn_ind_head = mp;
11290Sstevel@tonic-gate 	} else {
11308348SEric.Yu@Sun.COM 		ASSERT(sti->sti_conn_ind_tail->b_next == NULL);
11318348SEric.Yu@Sun.COM 		sti->sti_conn_ind_tail->b_next = mp;
11320Sstevel@tonic-gate 	}
11338348SEric.Yu@Sun.COM 	sti->sti_conn_ind_tail = mp;
11340Sstevel@tonic-gate 	/* Wakeup a single consumer of the T_CONN_IND */
11358348SEric.Yu@Sun.COM 	cv_signal(&so->so_acceptq_cv);
11360Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate /*
11400Sstevel@tonic-gate  * Wait for a T_CONN_IND.
11410Sstevel@tonic-gate  * Don't wait if nonblocking.
11420Sstevel@tonic-gate  * Accept signals and socket errors.
11430Sstevel@tonic-gate  */
11440Sstevel@tonic-gate int
11450Sstevel@tonic-gate sowaitconnind(struct sonode *so, int fmode, mblk_t **mpp)
11460Sstevel@tonic-gate {
11470Sstevel@tonic-gate 	mblk_t *mp;
11488348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
11490Sstevel@tonic-gate 	int error = 0;
11500Sstevel@tonic-gate 
11510Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
11520Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
11530Sstevel@tonic-gate check_error:
11540Sstevel@tonic-gate 	if (so->so_error) {
11558348SEric.Yu@Sun.COM 		error = sogeterr(so, B_TRUE);
11560Sstevel@tonic-gate 		if (error) {
11570Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
11580Sstevel@tonic-gate 			return (error);
11590Sstevel@tonic-gate 		}
11600Sstevel@tonic-gate 	}
11610Sstevel@tonic-gate 
11628348SEric.Yu@Sun.COM 	if (sti->sti_conn_ind_head == NULL) {
11630Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK)) {
11640Sstevel@tonic-gate 			error = EWOULDBLOCK;
11650Sstevel@tonic-gate 			goto done;
11660Sstevel@tonic-gate 		}
11678348SEric.Yu@Sun.COM 
11688348SEric.Yu@Sun.COM 		if (so->so_state & SS_CLOSING) {
11698348SEric.Yu@Sun.COM 			error = EINTR;
11708348SEric.Yu@Sun.COM 			goto done;
11718348SEric.Yu@Sun.COM 		}
11728348SEric.Yu@Sun.COM 
11738348SEric.Yu@Sun.COM 		if (!cv_wait_sig_swap(&so->so_acceptq_cv, &so->so_lock)) {
11740Sstevel@tonic-gate 			error = EINTR;
11750Sstevel@tonic-gate 			goto done;
11760Sstevel@tonic-gate 		}
11770Sstevel@tonic-gate 		goto check_error;
11780Sstevel@tonic-gate 	}
11798348SEric.Yu@Sun.COM 	mp = sti->sti_conn_ind_head;
11808348SEric.Yu@Sun.COM 	sti->sti_conn_ind_head = mp->b_next;
11810Sstevel@tonic-gate 	mp->b_next = NULL;
11828348SEric.Yu@Sun.COM 	if (sti->sti_conn_ind_head == NULL) {
11838348SEric.Yu@Sun.COM 		ASSERT(sti->sti_conn_ind_tail == mp);
11848348SEric.Yu@Sun.COM 		sti->sti_conn_ind_tail = NULL;
11850Sstevel@tonic-gate 	}
11860Sstevel@tonic-gate 	*mpp = mp;
11870Sstevel@tonic-gate done:
11880Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
11890Sstevel@tonic-gate 	return (error);
11900Sstevel@tonic-gate }
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate /*
11930Sstevel@tonic-gate  * Flush a T_CONN_IND matching the sequence number from the list.
11940Sstevel@tonic-gate  * Return zero if found; non-zero otherwise.
11950Sstevel@tonic-gate  * This is called very infrequently thus it is ok to do a linear search.
11960Sstevel@tonic-gate  */
11970Sstevel@tonic-gate int
11980Sstevel@tonic-gate soflushconnind(struct sonode *so, t_scalar_t seqno)
11990Sstevel@tonic-gate {
12000Sstevel@tonic-gate 	mblk_t *prevmp, *mp;
12010Sstevel@tonic-gate 	struct T_conn_ind *tci;
12028348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
12058348SEric.Yu@Sun.COM 	for (prevmp = NULL, mp = sti->sti_conn_ind_head; mp != NULL;
12060Sstevel@tonic-gate 	    prevmp = mp, mp = mp->b_next) {
12070Sstevel@tonic-gate 		tci = (struct T_conn_ind *)mp->b_rptr;
12080Sstevel@tonic-gate 		if (tci->SEQ_number == seqno) {
12090Sstevel@tonic-gate 			dprintso(so, 1,
12105753Sgww 			    ("t_discon_ind: found T_CONN_IND %d\n", seqno));
12110Sstevel@tonic-gate 			/* Deleting last? */
12128348SEric.Yu@Sun.COM 			if (sti->sti_conn_ind_tail == mp) {
12138348SEric.Yu@Sun.COM 				sti->sti_conn_ind_tail = prevmp;
12140Sstevel@tonic-gate 			}
12150Sstevel@tonic-gate 			if (prevmp == NULL) {
12160Sstevel@tonic-gate 				/* Deleting first */
12178348SEric.Yu@Sun.COM 				sti->sti_conn_ind_head = mp->b_next;
12180Sstevel@tonic-gate 			} else {
12190Sstevel@tonic-gate 				prevmp->b_next = mp->b_next;
12200Sstevel@tonic-gate 			}
12210Sstevel@tonic-gate 			mp->b_next = NULL;
12228348SEric.Yu@Sun.COM 
12238348SEric.Yu@Sun.COM 			ASSERT((sti->sti_conn_ind_head == NULL &&
12248348SEric.Yu@Sun.COM 			    sti->sti_conn_ind_tail == NULL) ||
12258348SEric.Yu@Sun.COM 			    (sti->sti_conn_ind_head != NULL &&
12268348SEric.Yu@Sun.COM 			    sti->sti_conn_ind_tail != NULL));
12278348SEric.Yu@Sun.COM 
12280Sstevel@tonic-gate 			so->so_error = ECONNABORTED;
12290Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
1230898Skais 
1231898Skais 			/*
1232898Skais 			 * T_KSSL_PROXY_CONN_IND may carry a handle for
1233898Skais 			 * an SSL context, and needs to be released.
1234898Skais 			 */
1235898Skais 			if ((tci->PRIM_type == T_SSL_PROXY_CONN_IND) &&
1236898Skais 			    (mp->b_cont != NULL)) {
1237898Skais 				kssl_ctx_t kssl_ctx;
1238898Skais 
1239898Skais 				ASSERT(MBLKL(mp->b_cont) ==
1240898Skais 				    sizeof (kssl_ctx_t));
1241898Skais 				kssl_ctx = *((kssl_ctx_t *)mp->b_cont->b_rptr);
1242898Skais 				kssl_release_ctx(kssl_ctx);
1243898Skais 			}
12440Sstevel@tonic-gate 			freemsg(mp);
12450Sstevel@tonic-gate 			return (0);
12460Sstevel@tonic-gate 		}
12470Sstevel@tonic-gate 	}
12480Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
12490Sstevel@tonic-gate 	dprintso(so, 1,	("t_discon_ind: NOT found T_CONN_IND %d\n", seqno));
12500Sstevel@tonic-gate 	return (-1);
12510Sstevel@tonic-gate }
12520Sstevel@tonic-gate 
12530Sstevel@tonic-gate /*
12540Sstevel@tonic-gate  * Wait until the socket is connected or there is an error.
12550Sstevel@tonic-gate  * fmode should contain any nonblocking flags. nosig should be
12560Sstevel@tonic-gate  * set if the caller does not want the wait to be interrupted by a signal.
12570Sstevel@tonic-gate  */
12580Sstevel@tonic-gate int
12590Sstevel@tonic-gate sowaitconnected(struct sonode *so, int fmode, int nosig)
12600Sstevel@tonic-gate {
12610Sstevel@tonic-gate 	int error;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
12640Sstevel@tonic-gate 
12650Sstevel@tonic-gate 	while ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) ==
12665753Sgww 	    SS_ISCONNECTING && so->so_error == 0) {
12670Sstevel@tonic-gate 
12687240Srh87107 		dprintso(so, 1, ("waiting for SS_ISCONNECTED on %p\n",
12697240Srh87107 		    (void *)so));
12700Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
12710Sstevel@tonic-gate 			return (EINPROGRESS);
12720Sstevel@tonic-gate 
12738348SEric.Yu@Sun.COM 		if (so->so_state & SS_CLOSING)
12748348SEric.Yu@Sun.COM 			return (EINTR);
12758348SEric.Yu@Sun.COM 
12760Sstevel@tonic-gate 		if (nosig)
12770Sstevel@tonic-gate 			cv_wait(&so->so_state_cv, &so->so_lock);
12780Sstevel@tonic-gate 		else if (!cv_wait_sig_swap(&so->so_state_cv, &so->so_lock)) {
12790Sstevel@tonic-gate 			/*
12800Sstevel@tonic-gate 			 * Return EINTR and let the application use
12810Sstevel@tonic-gate 			 * nonblocking techniques for detecting when
12820Sstevel@tonic-gate 			 * the connection has been established.
12830Sstevel@tonic-gate 			 */
12840Sstevel@tonic-gate 			return (EINTR);
12850Sstevel@tonic-gate 		}
12867240Srh87107 		dprintso(so, 1, ("awoken on %p\n", (void *)so));
12870Sstevel@tonic-gate 	}
12880Sstevel@tonic-gate 
12890Sstevel@tonic-gate 	if (so->so_error != 0) {
12908348SEric.Yu@Sun.COM 		error = sogeterr(so, B_TRUE);
12910Sstevel@tonic-gate 		ASSERT(error != 0);
12920Sstevel@tonic-gate 		dprintso(so, 1, ("sowaitconnected: error %d\n", error));
12930Sstevel@tonic-gate 		return (error);
12940Sstevel@tonic-gate 	}
12950Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
12960Sstevel@tonic-gate 		/*
12970Sstevel@tonic-gate 		 * Could have received a T_ORDREL_IND or a T_DISCON_IND with
12980Sstevel@tonic-gate 		 * zero errno. Or another thread could have consumed so_error
12990Sstevel@tonic-gate 		 * e.g. by calling read.
13000Sstevel@tonic-gate 		 */
13010Sstevel@tonic-gate 		error = ECONNREFUSED;
13020Sstevel@tonic-gate 		dprintso(so, 1, ("sowaitconnected: error %d\n", error));
13030Sstevel@tonic-gate 		return (error);
13040Sstevel@tonic-gate 	}
13050Sstevel@tonic-gate 	return (0);
13060Sstevel@tonic-gate }
13070Sstevel@tonic-gate 
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate /*
13100Sstevel@tonic-gate  * Handle the signal generation aspect of urgent data.
13110Sstevel@tonic-gate  */
13120Sstevel@tonic-gate static void
13130Sstevel@tonic-gate so_oob_sig(struct sonode *so, int extrasig,
13140Sstevel@tonic-gate     strsigset_t *signals, strpollset_t *pollwakeups)
13150Sstevel@tonic-gate {
13168348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
13178348SEric.Yu@Sun.COM 
13180Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13218348SEric.Yu@Sun.COM 	ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
13228348SEric.Yu@Sun.COM 	if (sti->sti_oobsigcnt > sti->sti_oobcnt) {
13230Sstevel@tonic-gate 		/*
13240Sstevel@tonic-gate 		 * Signal has already been generated once for this
13250Sstevel@tonic-gate 		 * urgent "event". However, since TCP can receive updated
13260Sstevel@tonic-gate 		 * urgent pointers we still generate a signal.
13270Sstevel@tonic-gate 		 */
13280Sstevel@tonic-gate 		ASSERT(so->so_state & SS_OOBPEND);
13290Sstevel@tonic-gate 		if (extrasig) {
13300Sstevel@tonic-gate 			*signals |= S_RDBAND;
13310Sstevel@tonic-gate 			*pollwakeups |= POLLRDBAND;
13320Sstevel@tonic-gate 		}
13330Sstevel@tonic-gate 		return;
13340Sstevel@tonic-gate 	}
13350Sstevel@tonic-gate 
13368348SEric.Yu@Sun.COM 	sti->sti_oobsigcnt++;
13378348SEric.Yu@Sun.COM 	ASSERT(sti->sti_oobsigcnt > 0);	/* Wraparound */
13388348SEric.Yu@Sun.COM 	ASSERT(sti->sti_oobsigcnt > sti->sti_oobcnt);
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	/*
13410Sstevel@tonic-gate 	 * Record (for select/poll) that urgent data is pending.
13420Sstevel@tonic-gate 	 */
13430Sstevel@tonic-gate 	so->so_state |= SS_OOBPEND;
13440Sstevel@tonic-gate 	/*
13450Sstevel@tonic-gate 	 * New urgent data on the way so forget about any old
13460Sstevel@tonic-gate 	 * urgent data.
13470Sstevel@tonic-gate 	 */
13480Sstevel@tonic-gate 	so->so_state &= ~(SS_HAVEOOBDATA|SS_HADOOBDATA);
13490Sstevel@tonic-gate 	if (so->so_oobmsg != NULL) {
13500Sstevel@tonic-gate 		dprintso(so, 1, ("sock: discarding old oob\n"));
13510Sstevel@tonic-gate 		freemsg(so->so_oobmsg);
13520Sstevel@tonic-gate 		so->so_oobmsg = NULL;
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 	*signals |= S_RDBAND;
13550Sstevel@tonic-gate 	*pollwakeups |= POLLRDBAND;
13560Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13570Sstevel@tonic-gate }
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate /*
13600Sstevel@tonic-gate  * Handle the processing of the T_EXDATA_IND with urgent data.
13610Sstevel@tonic-gate  * Returns the T_EXDATA_IND if it should be queued on the read queue.
13620Sstevel@tonic-gate  */
13630Sstevel@tonic-gate /* ARGSUSED2 */
13640Sstevel@tonic-gate static mblk_t *
13650Sstevel@tonic-gate so_oob_exdata(struct sonode *so, mblk_t *mp,
13660Sstevel@tonic-gate 	strsigset_t *signals, strpollset_t *pollwakeups)
13670Sstevel@tonic-gate {
13688348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
13698348SEric.Yu@Sun.COM 
13700Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13730Sstevel@tonic-gate 
13748348SEric.Yu@Sun.COM 	ASSERT(sti->sti_oobsigcnt > sti->sti_oobcnt);
13750Sstevel@tonic-gate 
13768348SEric.Yu@Sun.COM 	sti->sti_oobcnt++;
13778348SEric.Yu@Sun.COM 	ASSERT(sti->sti_oobcnt > 0);	/* wraparound? */
13788348SEric.Yu@Sun.COM 	ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate 	/*
13810Sstevel@tonic-gate 	 * Set MSGMARK for SIOCATMARK.
13820Sstevel@tonic-gate 	 */
13830Sstevel@tonic-gate 	mp->b_flag |= MSGMARK;
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
13860Sstevel@tonic-gate 	return (mp);
13870Sstevel@tonic-gate }
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate /*
13900Sstevel@tonic-gate  * Handle the processing of the actual urgent data.
13910Sstevel@tonic-gate  * Returns the data mblk if it should be queued on the read queue.
13920Sstevel@tonic-gate  */
13930Sstevel@tonic-gate static mblk_t *
13940Sstevel@tonic-gate so_oob_data(struct sonode *so, mblk_t *mp,
13950Sstevel@tonic-gate 	strsigset_t *signals, strpollset_t *pollwakeups)
13960Sstevel@tonic-gate {
13978348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
13988348SEric.Yu@Sun.COM 
13990Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14000Sstevel@tonic-gate 
14010Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
14020Sstevel@tonic-gate 
14038348SEric.Yu@Sun.COM 	ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
14040Sstevel@tonic-gate 	ASSERT(mp != NULL);
14050Sstevel@tonic-gate 	/*
14060Sstevel@tonic-gate 	 * For OOBINLINE we keep the data in the T_EXDATA_IND.
14070Sstevel@tonic-gate 	 * Otherwise we store it in so_oobmsg.
14080Sstevel@tonic-gate 	 */
14090Sstevel@tonic-gate 	ASSERT(so->so_oobmsg == NULL);
14100Sstevel@tonic-gate 	if (so->so_options & SO_OOBINLINE) {
14110Sstevel@tonic-gate 		*pollwakeups |= POLLIN | POLLRDNORM | POLLRDBAND;
14120Sstevel@tonic-gate 		*signals |= S_INPUT | S_RDNORM;
14130Sstevel@tonic-gate 	} else {
14140Sstevel@tonic-gate 		*pollwakeups |= POLLRDBAND;
14150Sstevel@tonic-gate 		so->so_state |= SS_HAVEOOBDATA;
14160Sstevel@tonic-gate 		so->so_oobmsg = mp;
14170Sstevel@tonic-gate 		mp = NULL;
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
14200Sstevel@tonic-gate 	return (mp);
14210Sstevel@tonic-gate }
14220Sstevel@tonic-gate 
14230Sstevel@tonic-gate /*
14240Sstevel@tonic-gate  * Caller must hold the mutex.
14250Sstevel@tonic-gate  * For delayed processing, save the T_DISCON_IND received
14268348SEric.Yu@Sun.COM  * from below on sti_discon_ind_mp.
14270Sstevel@tonic-gate  * When the message is processed the framework will call:
14280Sstevel@tonic-gate  *      (*func)(so, mp);
14290Sstevel@tonic-gate  */
14300Sstevel@tonic-gate static void
14310Sstevel@tonic-gate so_save_discon_ind(struct sonode *so,
14320Sstevel@tonic-gate 	mblk_t *mp,
14330Sstevel@tonic-gate 	void (*func)(struct sonode *so, mblk_t *))
14340Sstevel@tonic-gate {
14358348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
14368348SEric.Yu@Sun.COM 
14370Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate 	/*
14400Sstevel@tonic-gate 	 * Discard new T_DISCON_IND if we have already received another.
14418348SEric.Yu@Sun.COM 	 * Currently the earlier message can either be on sti_discon_ind_mp
14420Sstevel@tonic-gate 	 * or being processed.
14430Sstevel@tonic-gate 	 */
14448348SEric.Yu@Sun.COM 	if (sti->sti_discon_ind_mp != NULL || (so->so_flag & SOASYNC_UNBIND)) {
14451548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
14460Sstevel@tonic-gate 		    "sockfs: received unexpected additional T_DISCON_IND\n");
14470Sstevel@tonic-gate 		freemsg(mp);
14480Sstevel@tonic-gate 		return;
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate 	mp->b_prev = (mblk_t *)func;
14510Sstevel@tonic-gate 	mp->b_next = NULL;
14528348SEric.Yu@Sun.COM 	sti->sti_discon_ind_mp = mp;
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate /*
14560Sstevel@tonic-gate  * Caller must hold the mutex and make sure that either SOLOCKED
14570Sstevel@tonic-gate  * or SOASYNC_UNBIND is set. Called from so_unlock_single().
14588348SEric.Yu@Sun.COM  * Perform delayed processing of T_DISCON_IND message on sti_discon_ind_mp.
14590Sstevel@tonic-gate  * Need to ensure that strsock_proto() will not end up sleeping for
14600Sstevel@tonic-gate  * SOASYNC_UNBIND, while executing this function.
14610Sstevel@tonic-gate  */
14620Sstevel@tonic-gate void
14630Sstevel@tonic-gate so_drain_discon_ind(struct sonode *so)
14640Sstevel@tonic-gate {
14650Sstevel@tonic-gate 	mblk_t	*bp;
14660Sstevel@tonic-gate 	void (*func)(struct sonode *so, mblk_t *);
14678348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
14680Sstevel@tonic-gate 
14690Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14700Sstevel@tonic-gate 	ASSERT(so->so_flag & (SOLOCKED|SOASYNC_UNBIND));
14710Sstevel@tonic-gate 
14728348SEric.Yu@Sun.COM 	/* Process T_DISCON_IND on sti_discon_ind_mp */
14738348SEric.Yu@Sun.COM 	if ((bp = sti->sti_discon_ind_mp) != NULL) {
14748348SEric.Yu@Sun.COM 		sti->sti_discon_ind_mp = NULL;
14750Sstevel@tonic-gate 		func = (void (*)())bp->b_prev;
14760Sstevel@tonic-gate 		bp->b_prev = NULL;
14770Sstevel@tonic-gate 
14780Sstevel@tonic-gate 		/*
14790Sstevel@tonic-gate 		 * This (*func) is supposed to generate a message downstream
14800Sstevel@tonic-gate 		 * and we need to have a flag set until the corresponding
14810Sstevel@tonic-gate 		 * upstream message reaches stream head.
14820Sstevel@tonic-gate 		 * When processing T_DISCON_IND in strsock_discon_ind
14830Sstevel@tonic-gate 		 * we hold SOASYN_UNBIND when sending T_UNBIND_REQ down and
14840Sstevel@tonic-gate 		 * drop the flag after we get the ACK in strsock_proto.
14850Sstevel@tonic-gate 		 */
14860Sstevel@tonic-gate 		(void) (*func)(so, bp);
14870Sstevel@tonic-gate 	}
14880Sstevel@tonic-gate }
14890Sstevel@tonic-gate 
14900Sstevel@tonic-gate /*
14910Sstevel@tonic-gate  * Caller must hold the mutex.
14928348SEric.Yu@Sun.COM  * Remove the T_DISCON_IND on sti_discon_ind_mp.
14930Sstevel@tonic-gate  */
14940Sstevel@tonic-gate void
14950Sstevel@tonic-gate so_flush_discon_ind(struct sonode *so)
14960Sstevel@tonic-gate {
14970Sstevel@tonic-gate 	mblk_t	*bp;
14988348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
14990Sstevel@tonic-gate 
15000Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	/*
15038348SEric.Yu@Sun.COM 	 * Remove T_DISCON_IND mblk at sti_discon_ind_mp.
15040Sstevel@tonic-gate 	 */
15058348SEric.Yu@Sun.COM 	if ((bp = sti->sti_discon_ind_mp) != NULL) {
15068348SEric.Yu@Sun.COM 		sti->sti_discon_ind_mp = NULL;
15070Sstevel@tonic-gate 		bp->b_prev = NULL;
15080Sstevel@tonic-gate 		freemsg(bp);
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate }
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate /*
15130Sstevel@tonic-gate  * Caller must hold the mutex.
15140Sstevel@tonic-gate  *
15150Sstevel@tonic-gate  * This function is used to process the T_DISCON_IND message. It does
15160Sstevel@tonic-gate  * immediate processing when called from strsock_proto and delayed
15178348SEric.Yu@Sun.COM  * processing of discon_ind saved on sti_discon_ind_mp when called from
15180Sstevel@tonic-gate  * so_drain_discon_ind. When a T_DISCON_IND message is saved in
15198348SEric.Yu@Sun.COM  * sti_discon_ind_mp for delayed processing, this function is registered
15200Sstevel@tonic-gate  * as the callback function to process the message.
15210Sstevel@tonic-gate  *
15220Sstevel@tonic-gate  * SOASYNC_UNBIND should be held in this function, during the non-blocking
15230Sstevel@tonic-gate  * unbind operation, and should be released only after we receive the ACK
15240Sstevel@tonic-gate  * in strsock_proto, for the T_UNBIND_REQ sent here. Since SOLOCKED is not set,
15250Sstevel@tonic-gate  * no TPI messages would be sent down at this time. This is to prevent M_FLUSH
15260Sstevel@tonic-gate  * sent from either this function or tcp_unbind(), flushing away any TPI
15270Sstevel@tonic-gate  * message that is being sent down and stays in a lower module's queue.
15280Sstevel@tonic-gate  *
15290Sstevel@tonic-gate  * This function drops so_lock and grabs it again.
15300Sstevel@tonic-gate  */
15310Sstevel@tonic-gate static void
15320Sstevel@tonic-gate strsock_discon_ind(struct sonode *so, mblk_t *discon_mp)
15330Sstevel@tonic-gate {
15340Sstevel@tonic-gate 	struct vnode *vp;
15350Sstevel@tonic-gate 	struct stdata *stp;
15360Sstevel@tonic-gate 	union T_primitives *tpr;
15370Sstevel@tonic-gate 	struct T_unbind_req *ubr;
15380Sstevel@tonic-gate 	mblk_t *mp;
15390Sstevel@tonic-gate 	int error;
15408348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
15430Sstevel@tonic-gate 	ASSERT(discon_mp);
15440Sstevel@tonic-gate 	ASSERT(discon_mp->b_rptr);
15450Sstevel@tonic-gate 
15460Sstevel@tonic-gate 	tpr = (union T_primitives *)discon_mp->b_rptr;
15470Sstevel@tonic-gate 	ASSERT(tpr->type == T_DISCON_IND);
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	vp = SOTOV(so);
15500Sstevel@tonic-gate 	stp = vp->v_stream;
15510Sstevel@tonic-gate 	ASSERT(stp);
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	/*
15540Sstevel@tonic-gate 	 * Not a listener
15550Sstevel@tonic-gate 	 */
15560Sstevel@tonic-gate 	ASSERT((so->so_state & SS_ACCEPTCONN) == 0);
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	/*
15590Sstevel@tonic-gate 	 * This assumes that the name space for DISCON_reason
15600Sstevel@tonic-gate 	 * is the errno name space.
15610Sstevel@tonic-gate 	 */
15620Sstevel@tonic-gate 	soisdisconnected(so, tpr->discon_ind.DISCON_reason);
15638348SEric.Yu@Sun.COM 	sti->sti_laddr_valid = 0;
15648348SEric.Yu@Sun.COM 	sti->sti_faddr_valid = 0;
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate 	/*
15670Sstevel@tonic-gate 	 * Unbind with the transport without blocking.
15680Sstevel@tonic-gate 	 * If we've already received a T_DISCON_IND do not unbind.
15690Sstevel@tonic-gate 	 *
15700Sstevel@tonic-gate 	 * If there is no preallocated unbind message, we have already
15710Sstevel@tonic-gate 	 * unbound with the transport
15720Sstevel@tonic-gate 	 *
15730Sstevel@tonic-gate 	 * If the socket is not bound, no need to unbind.
15740Sstevel@tonic-gate 	 */
15758348SEric.Yu@Sun.COM 	mp = sti->sti_unbind_mp;
15760Sstevel@tonic-gate 	if (mp == NULL) {
15770Sstevel@tonic-gate 		ASSERT(!(so->so_state & SS_ISBOUND));
15780Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
15790Sstevel@tonic-gate 	} else if (!(so->so_state & SS_ISBOUND))  {
15800Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
15810Sstevel@tonic-gate 	} else {
15828348SEric.Yu@Sun.COM 		sti->sti_unbind_mp = NULL;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 		/*
15850Sstevel@tonic-gate 		 * Is another T_DISCON_IND being processed.
15860Sstevel@tonic-gate 		 */
15870Sstevel@tonic-gate 		ASSERT((so->so_flag & SOASYNC_UNBIND) == 0);
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 		/*
15900Sstevel@tonic-gate 		 * Make strsock_proto ignore T_OK_ACK and T_ERROR_ACK for
15910Sstevel@tonic-gate 		 * this unbind. Set SOASYNC_UNBIND. This should be cleared
15920Sstevel@tonic-gate 		 * only after we receive the ACK in strsock_proto.
15930Sstevel@tonic-gate 		 */
15940Sstevel@tonic-gate 		so->so_flag |= SOASYNC_UNBIND;
15950Sstevel@tonic-gate 		ASSERT(!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)));
15968348SEric.Yu@Sun.COM 		so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN);
15978348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
15980Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
15990Sstevel@tonic-gate 
16000Sstevel@tonic-gate 		/*
16010Sstevel@tonic-gate 		 * Send down T_UNBIND_REQ ignoring flow control.
16020Sstevel@tonic-gate 		 * XXX Assumes that MSG_IGNFLOW implies that this thread
16030Sstevel@tonic-gate 		 * does not run service procedures.
16040Sstevel@tonic-gate 		 */
16050Sstevel@tonic-gate 		ASSERT(DB_TYPE(mp) == M_PROTO);
16060Sstevel@tonic-gate 		ubr = (struct T_unbind_req *)mp->b_rptr;
16070Sstevel@tonic-gate 		mp->b_wptr += sizeof (*ubr);
16080Sstevel@tonic-gate 		ubr->PRIM_type = T_UNBIND_REQ;
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate 		/*
16110Sstevel@tonic-gate 		 * Flush the read and write side (except stream head read queue)
16120Sstevel@tonic-gate 		 * and send down T_UNBIND_REQ.
16130Sstevel@tonic-gate 		 */
16140Sstevel@tonic-gate 		(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
16150Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
16165753Sgww 		    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
16170Sstevel@tonic-gate 		/* LINTED - warning: statement has no consequent: if */
16180Sstevel@tonic-gate 		if (error) {
16190Sstevel@tonic-gate 			eprintsoline(so, error);
16200Sstevel@tonic-gate 		}
16210Sstevel@tonic-gate 	}
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	if (tpr->discon_ind.DISCON_reason != 0)
16240Sstevel@tonic-gate 		strsetrerror(SOTOV(so), 0, 0, sogetrderr);
16250Sstevel@tonic-gate 	strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
16260Sstevel@tonic-gate 	strseteof(SOTOV(so), 1);
16270Sstevel@tonic-gate 	/*
16280Sstevel@tonic-gate 	 * strseteof takes care of read side wakeups,
16290Sstevel@tonic-gate 	 * pollwakeups, and signals.
16300Sstevel@tonic-gate 	 */
16310Sstevel@tonic-gate 	dprintso(so, 1, ("T_DISCON_IND: error %d\n", so->so_error));
16320Sstevel@tonic-gate 	freemsg(discon_mp);
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate 
16350Sstevel@tonic-gate 	pollwakeup(&stp->sd_pollist, POLLOUT);
16360Sstevel@tonic-gate 	mutex_enter(&stp->sd_lock);
16370Sstevel@tonic-gate 
16380Sstevel@tonic-gate 	/*
16390Sstevel@tonic-gate 	 * Wake sleeping write
16400Sstevel@tonic-gate 	 */
16410Sstevel@tonic-gate 	if (stp->sd_flag & WSLEEP) {
16420Sstevel@tonic-gate 		stp->sd_flag &= ~WSLEEP;
16430Sstevel@tonic-gate 		cv_broadcast(&stp->sd_wrq->q_wait);
16440Sstevel@tonic-gate 	}
16450Sstevel@tonic-gate 
16460Sstevel@tonic-gate 	/*
16470Sstevel@tonic-gate 	 * strsendsig can handle multiple signals with a
16480Sstevel@tonic-gate 	 * single call.  Send SIGPOLL for S_OUTPUT event.
16490Sstevel@tonic-gate 	 */
16500Sstevel@tonic-gate 	if (stp->sd_sigflags & S_OUTPUT)
16510Sstevel@tonic-gate 		strsendsig(stp->sd_siglist, S_OUTPUT, 0, 0);
16520Sstevel@tonic-gate 
16530Sstevel@tonic-gate 	mutex_exit(&stp->sd_lock);
16540Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
16550Sstevel@tonic-gate }
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate /*
16580Sstevel@tonic-gate  * This routine is registered with the stream head to receive M_PROTO
16590Sstevel@tonic-gate  * and M_PCPROTO messages.
16600Sstevel@tonic-gate  *
16610Sstevel@tonic-gate  * Returns NULL if the message was consumed.
16620Sstevel@tonic-gate  * Returns an mblk to make that mblk be processed (and queued) by the stream
16630Sstevel@tonic-gate  * head.
16640Sstevel@tonic-gate  *
16650Sstevel@tonic-gate  * Sets the return parameters (*wakeups, *firstmsgsigs, *allmsgsigs, and
16660Sstevel@tonic-gate  * *pollwakeups) for the stream head to take action on. Note that since
16670Sstevel@tonic-gate  * sockets always deliver SIGIO for every new piece of data this routine
16680Sstevel@tonic-gate  * never sets *firstmsgsigs; any signals are returned in *allmsgsigs.
16690Sstevel@tonic-gate  *
16700Sstevel@tonic-gate  * This routine handles all data related TPI messages independent of
16710Sstevel@tonic-gate  * the type of the socket i.e. it doesn't care if T_UNITDATA_IND message
16720Sstevel@tonic-gate  * arrive on a SOCK_STREAM.
16730Sstevel@tonic-gate  */
16740Sstevel@tonic-gate static mblk_t *
16750Sstevel@tonic-gate strsock_proto(vnode_t *vp, mblk_t *mp,
16760Sstevel@tonic-gate 		strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
16770Sstevel@tonic-gate 		strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
16780Sstevel@tonic-gate {
16790Sstevel@tonic-gate 	union T_primitives *tpr;
16800Sstevel@tonic-gate 	struct sonode *so;
16818348SEric.Yu@Sun.COM 	sotpi_info_t *sti;
1682*11861SMarek.Pospisil@Sun.COM 	uint32_t auditing = AU_AUDITING();
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	so = VTOSO(vp);
16858348SEric.Yu@Sun.COM 	sti = SOTOTPI(so);
16860Sstevel@tonic-gate 
16877240Srh87107 	dprintso(so, 1, ("strsock_proto(%p, %p)\n", (void *)vp, (void *)mp));
16880Sstevel@tonic-gate 
16890Sstevel@tonic-gate 	/* Set default return values */
16900Sstevel@tonic-gate 	*firstmsgsigs = *wakeups = *allmsgsigs = *pollwakeups = 0;
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 	ASSERT(DB_TYPE(mp) == M_PROTO ||
16930Sstevel@tonic-gate 	    DB_TYPE(mp) == M_PCPROTO);
16940Sstevel@tonic-gate 
16950Sstevel@tonic-gate 	if (MBLKL(mp) < sizeof (tpr->type)) {
16960Sstevel@tonic-gate 		/* The message is too short to even contain the primitive */
16971548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
16980Sstevel@tonic-gate 		    "sockfs: Too short TPI message received. Len = %ld\n",
16990Sstevel@tonic-gate 		    (ptrdiff_t)(MBLKL(mp)));
17000Sstevel@tonic-gate 		freemsg(mp);
17010Sstevel@tonic-gate 		return (NULL);
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate 	if (!__TPI_PRIM_ISALIGNED(mp->b_rptr)) {
17040Sstevel@tonic-gate 		/* The read pointer is not aligned correctly for TPI */
17051548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
17060Sstevel@tonic-gate 		    "sockfs: Unaligned TPI message received. rptr = %p\n",
17070Sstevel@tonic-gate 		    (void *)mp->b_rptr);
17080Sstevel@tonic-gate 		freemsg(mp);
17090Sstevel@tonic-gate 		return (NULL);
17100Sstevel@tonic-gate 	}
17110Sstevel@tonic-gate 	tpr = (union T_primitives *)mp->b_rptr;
17120Sstevel@tonic-gate 	dprintso(so, 1, ("strsock_proto: primitive %d\n", tpr->type));
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	switch (tpr->type) {
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	case T_DATA_IND:
17170Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_data_ind)) {
17181548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
17190Sstevel@tonic-gate 			    "sockfs: Too short T_DATA_IND. Len = %ld\n",
17200Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
17210Sstevel@tonic-gate 			freemsg(mp);
17220Sstevel@tonic-gate 			return (NULL);
17230Sstevel@tonic-gate 		}
17240Sstevel@tonic-gate 		/*
17250Sstevel@tonic-gate 		 * Ignore zero-length T_DATA_IND messages. These might be
17260Sstevel@tonic-gate 		 * generated by some transports.
17270Sstevel@tonic-gate 		 * This is needed to prevent read (which skips the M_PROTO
17280Sstevel@tonic-gate 		 * part) to unexpectedly return 0 (or return EWOULDBLOCK
17290Sstevel@tonic-gate 		 * on a non-blocking socket after select/poll has indicated
17300Sstevel@tonic-gate 		 * that data is available).
17310Sstevel@tonic-gate 		 */
17320Sstevel@tonic-gate 		if (msgdsize(mp->b_cont) == 0) {
17330Sstevel@tonic-gate 			dprintso(so, 0,
17340Sstevel@tonic-gate 			    ("strsock_proto: zero length T_DATA_IND\n"));
17350Sstevel@tonic-gate 			freemsg(mp);
17360Sstevel@tonic-gate 			return (NULL);
17370Sstevel@tonic-gate 		}
17380Sstevel@tonic-gate 		*allmsgsigs = S_INPUT | S_RDNORM;
17390Sstevel@tonic-gate 		*pollwakeups = POLLIN | POLLRDNORM;
17400Sstevel@tonic-gate 		*wakeups = RSLEEP;
17410Sstevel@tonic-gate 		return (mp);
17420Sstevel@tonic-gate 
17430Sstevel@tonic-gate 	case T_UNITDATA_IND: {
17440Sstevel@tonic-gate 		struct T_unitdata_ind	*tudi = &tpr->unitdata_ind;
17450Sstevel@tonic-gate 		void			*addr;
17460Sstevel@tonic-gate 		t_uscalar_t		addrlen;
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_unitdata_ind)) {
17491548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
17500Sstevel@tonic-gate 			    "sockfs: Too short T_UNITDATA_IND. Len = %ld\n",
17510Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
17520Sstevel@tonic-gate 			freemsg(mp);
17530Sstevel@tonic-gate 			return (NULL);
17540Sstevel@tonic-gate 		}
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 		/* Is this is not a connected datagram socket? */
17570Sstevel@tonic-gate 		if ((so->so_mode & SM_CONNREQUIRED) ||
17580Sstevel@tonic-gate 		    !(so->so_state & SS_ISCONNECTED)) {
17590Sstevel@tonic-gate 			/*
17600Sstevel@tonic-gate 			 * Not a connected datagram socket. Look for
17610Sstevel@tonic-gate 			 * the SO_UNIX_CLOSE option. If such an option is found
17620Sstevel@tonic-gate 			 * discard the message (since it has no meaning
17630Sstevel@tonic-gate 			 * unless connected).
17640Sstevel@tonic-gate 			 */
17650Sstevel@tonic-gate 			if (so->so_family == AF_UNIX && msgdsize(mp) == 0 &&
17660Sstevel@tonic-gate 			    tudi->OPT_length != 0) {
17670Sstevel@tonic-gate 				void *opt;
17680Sstevel@tonic-gate 				t_uscalar_t optlen = tudi->OPT_length;
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate 				opt = sogetoff(mp, tudi->OPT_offset,
17715753Sgww 				    optlen, __TPI_ALIGN_SIZE);
17720Sstevel@tonic-gate 				if (opt == NULL) {
17730Sstevel@tonic-gate 					/* The len/off falls outside mp */
17740Sstevel@tonic-gate 					freemsg(mp);
17750Sstevel@tonic-gate 					mutex_enter(&so->so_lock);
17760Sstevel@tonic-gate 					soseterror(so, EPROTO);
17770Sstevel@tonic-gate 					mutex_exit(&so->so_lock);
17781548Srshoaib 					zcmn_err(getzoneid(), CE_WARN,
17790Sstevel@tonic-gate 					    "sockfs: T_unidata_ind with "
17800Sstevel@tonic-gate 					    "invalid optlen/offset %u/%d\n",
17810Sstevel@tonic-gate 					    optlen, tudi->OPT_offset);
17820Sstevel@tonic-gate 					return (NULL);
17830Sstevel@tonic-gate 				}
17840Sstevel@tonic-gate 				if (so_getopt_unix_close(opt, optlen)) {
17850Sstevel@tonic-gate 					freemsg(mp);
17860Sstevel@tonic-gate 					return (NULL);
17870Sstevel@tonic-gate 				}
17880Sstevel@tonic-gate 			}
17890Sstevel@tonic-gate 			*allmsgsigs = S_INPUT | S_RDNORM;
17900Sstevel@tonic-gate 			*pollwakeups = POLLIN | POLLRDNORM;
17910Sstevel@tonic-gate 			*wakeups = RSLEEP;
1792*11861SMarek.Pospisil@Sun.COM 			if (auditing)
17930Sstevel@tonic-gate 				audit_sock(T_UNITDATA_IND, strvp2wq(vp),
17945753Sgww 				    mp, 0);
17950Sstevel@tonic-gate 			return (mp);
17960Sstevel@tonic-gate 		}
17970Sstevel@tonic-gate 
17980Sstevel@tonic-gate 		/*
17990Sstevel@tonic-gate 		 * A connect datagram socket. For AF_INET{,6} we verify that
18000Sstevel@tonic-gate 		 * the source address matches the "connected to" address.
18010Sstevel@tonic-gate 		 * The semantics of AF_UNIX sockets is to not verify
18020Sstevel@tonic-gate 		 * the source address.
18030Sstevel@tonic-gate 		 * Note that this source address verification is transport
18040Sstevel@tonic-gate 		 * specific. Thus the real fix would be to extent TPI
18050Sstevel@tonic-gate 		 * to allow T_CONN_REQ messages to be send to connectionless
18060Sstevel@tonic-gate 		 * transport providers and always let the transport provider
18070Sstevel@tonic-gate 		 * do whatever filtering is needed.
18080Sstevel@tonic-gate 		 *
18090Sstevel@tonic-gate 		 * The verification/filtering semantics for transports
18100Sstevel@tonic-gate 		 * other than AF_INET and AF_UNIX are unknown. The choice
18110Sstevel@tonic-gate 		 * would be to either filter using bcmp or let all messages
18120Sstevel@tonic-gate 		 * get through. This code does not filter other address
18130Sstevel@tonic-gate 		 * families since this at least allows the application to
18140Sstevel@tonic-gate 		 * work around any missing filtering.
18150Sstevel@tonic-gate 		 *
18160Sstevel@tonic-gate 		 * XXX Should we move filtering to UDP/ICMP???
18170Sstevel@tonic-gate 		 * That would require passing e.g. a T_DISCON_REQ to UDP
18180Sstevel@tonic-gate 		 * when the socket becomes unconnected.
18190Sstevel@tonic-gate 		 */
18200Sstevel@tonic-gate 		addrlen = tudi->SRC_length;
18210Sstevel@tonic-gate 		/*
18220Sstevel@tonic-gate 		 * The alignment restriction is really to strict but
18230Sstevel@tonic-gate 		 * we want enough alignment to inspect the fields of
18240Sstevel@tonic-gate 		 * a sockaddr_in.
18250Sstevel@tonic-gate 		 */
18260Sstevel@tonic-gate 		addr = sogetoff(mp, tudi->SRC_offset, addrlen,
18275753Sgww 		    __TPI_ALIGN_SIZE);
18280Sstevel@tonic-gate 		if (addr == NULL) {
18290Sstevel@tonic-gate 			freemsg(mp);
18300Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
18310Sstevel@tonic-gate 			soseterror(so, EPROTO);
18320Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
18331548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
18340Sstevel@tonic-gate 			    "sockfs: T_unidata_ind with invalid "
18350Sstevel@tonic-gate 			    "addrlen/offset %u/%d\n",
18360Sstevel@tonic-gate 			    addrlen, tudi->SRC_offset);
18370Sstevel@tonic-gate 			return (NULL);
18380Sstevel@tonic-gate 		}
18390Sstevel@tonic-gate 
18400Sstevel@tonic-gate 		if (so->so_family == AF_INET) {
18410Sstevel@tonic-gate 			/*
18420Sstevel@tonic-gate 			 * For AF_INET we allow wildcarding both sin_addr
18430Sstevel@tonic-gate 			 * and sin_port.
18440Sstevel@tonic-gate 			 */
18450Sstevel@tonic-gate 			struct sockaddr_in *faddr, *sin;
18460Sstevel@tonic-gate 
18478348SEric.Yu@Sun.COM 			/* Prevent sti_faddr_sa from changing while accessed */
18480Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
18498348SEric.Yu@Sun.COM 			ASSERT(sti->sti_faddr_len ==
18505753Sgww 			    (socklen_t)sizeof (struct sockaddr_in));
18518348SEric.Yu@Sun.COM 			faddr = (struct sockaddr_in *)sti->sti_faddr_sa;
18520Sstevel@tonic-gate 			sin = (struct sockaddr_in *)addr;
18530Sstevel@tonic-gate 			if (addrlen !=
18545753Sgww 			    (t_uscalar_t)sizeof (struct sockaddr_in) ||
18550Sstevel@tonic-gate 			    (sin->sin_addr.s_addr != faddr->sin_addr.s_addr &&
18560Sstevel@tonic-gate 			    faddr->sin_addr.s_addr != INADDR_ANY) ||
18570Sstevel@tonic-gate 			    (so->so_type != SOCK_RAW &&
18580Sstevel@tonic-gate 			    sin->sin_port != faddr->sin_port &&
18590Sstevel@tonic-gate 			    faddr->sin_port != 0)) {
18600Sstevel@tonic-gate #ifdef DEBUG
18610Sstevel@tonic-gate 				dprintso(so, 0,
18625753Sgww 				    ("sockfs: T_UNITDATA_IND mismatch: %s",
18635753Sgww 				    pr_addr(so->so_family,
18648348SEric.Yu@Sun.COM 				    (struct sockaddr *)addr, addrlen)));
18650Sstevel@tonic-gate 				dprintso(so, 0, (" - %s\n",
18668348SEric.Yu@Sun.COM 				    pr_addr(so->so_family, sti->sti_faddr_sa,
18678348SEric.Yu@Sun.COM 				    (t_uscalar_t)sti->sti_faddr_len)));
18680Sstevel@tonic-gate #endif /* DEBUG */
18690Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
18700Sstevel@tonic-gate 				freemsg(mp);
18710Sstevel@tonic-gate 				return (NULL);
18720Sstevel@tonic-gate 			}
18730Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
18740Sstevel@tonic-gate 		} else if (so->so_family == AF_INET6) {
18750Sstevel@tonic-gate 			/*
18760Sstevel@tonic-gate 			 * For AF_INET6 we allow wildcarding both sin6_addr
18770Sstevel@tonic-gate 			 * and sin6_port.
18780Sstevel@tonic-gate 			 */
18790Sstevel@tonic-gate 			struct sockaddr_in6 *faddr6, *sin6;
18800Sstevel@tonic-gate 			static struct in6_addr zeroes; /* inits to all zeros */
18810Sstevel@tonic-gate 
18828348SEric.Yu@Sun.COM 			/* Prevent sti_faddr_sa from changing while accessed */
18830Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
18848348SEric.Yu@Sun.COM 			ASSERT(sti->sti_faddr_len ==
18850Sstevel@tonic-gate 			    (socklen_t)sizeof (struct sockaddr_in6));
18868348SEric.Yu@Sun.COM 			faddr6 = (struct sockaddr_in6 *)sti->sti_faddr_sa;
18870Sstevel@tonic-gate 			sin6 = (struct sockaddr_in6 *)addr;
18880Sstevel@tonic-gate 			/* XXX could we get a mapped address ::ffff:0.0.0.0 ? */
18890Sstevel@tonic-gate 			if (addrlen !=
18900Sstevel@tonic-gate 			    (t_uscalar_t)sizeof (struct sockaddr_in6) ||
18910Sstevel@tonic-gate 			    (!IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
18925753Sgww 			    &faddr6->sin6_addr) &&
18930Sstevel@tonic-gate 			    !IN6_ARE_ADDR_EQUAL(&faddr6->sin6_addr, &zeroes)) ||
18940Sstevel@tonic-gate 			    (so->so_type != SOCK_RAW &&
18950Sstevel@tonic-gate 			    sin6->sin6_port != faddr6->sin6_port &&
18960Sstevel@tonic-gate 			    faddr6->sin6_port != 0)) {
18970Sstevel@tonic-gate #ifdef DEBUG
18980Sstevel@tonic-gate 				dprintso(so, 0,
18990Sstevel@tonic-gate 				    ("sockfs: T_UNITDATA_IND mismatch: %s",
19005753Sgww 				    pr_addr(so->so_family,
19018348SEric.Yu@Sun.COM 				    (struct sockaddr *)addr, addrlen)));
19020Sstevel@tonic-gate 				dprintso(so, 0, (" - %s\n",
19038348SEric.Yu@Sun.COM 				    pr_addr(so->so_family, sti->sti_faddr_sa,
19048348SEric.Yu@Sun.COM 				    (t_uscalar_t)sti->sti_faddr_len)));
19050Sstevel@tonic-gate #endif /* DEBUG */
19060Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
19070Sstevel@tonic-gate 				freemsg(mp);
19080Sstevel@tonic-gate 				return (NULL);
19090Sstevel@tonic-gate 			}
19100Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
19110Sstevel@tonic-gate 		} else if (so->so_family == AF_UNIX &&
19120Sstevel@tonic-gate 		    msgdsize(mp->b_cont) == 0 &&
19130Sstevel@tonic-gate 		    tudi->OPT_length != 0) {
19140Sstevel@tonic-gate 			/*
19150Sstevel@tonic-gate 			 * Attempt to extract AF_UNIX
19160Sstevel@tonic-gate 			 * SO_UNIX_CLOSE indication from options.
19170Sstevel@tonic-gate 			 */
19180Sstevel@tonic-gate 			void *opt;
19190Sstevel@tonic-gate 			t_uscalar_t optlen = tudi->OPT_length;
19200Sstevel@tonic-gate 
19210Sstevel@tonic-gate 			opt = sogetoff(mp, tudi->OPT_offset,
19225753Sgww 			    optlen, __TPI_ALIGN_SIZE);
19230Sstevel@tonic-gate 			if (opt == NULL) {
19240Sstevel@tonic-gate 				/* The len/off falls outside mp */
19250Sstevel@tonic-gate 				freemsg(mp);
19260Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
19270Sstevel@tonic-gate 				soseterror(so, EPROTO);
19280Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
19291548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
19300Sstevel@tonic-gate 				    "sockfs: T_unidata_ind with invalid "
19310Sstevel@tonic-gate 				    "optlen/offset %u/%d\n",
19320Sstevel@tonic-gate 				    optlen, tudi->OPT_offset);
19330Sstevel@tonic-gate 				return (NULL);
19340Sstevel@tonic-gate 			}
19350Sstevel@tonic-gate 			/*
19360Sstevel@tonic-gate 			 * If we received a unix close indication mark the
19370Sstevel@tonic-gate 			 * socket and discard this message.
19380Sstevel@tonic-gate 			 */
19390Sstevel@tonic-gate 			if (so_getopt_unix_close(opt, optlen)) {
19400Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
19410Sstevel@tonic-gate 				sobreakconn(so, ECONNRESET);
19420Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
19430Sstevel@tonic-gate 				strsetrerror(SOTOV(so), 0, 0, sogetrderr);
19440Sstevel@tonic-gate 				freemsg(mp);
19450Sstevel@tonic-gate 				*pollwakeups = POLLIN | POLLRDNORM;
19460Sstevel@tonic-gate 				*allmsgsigs = S_INPUT | S_RDNORM;
19470Sstevel@tonic-gate 				*wakeups = RSLEEP;
19480Sstevel@tonic-gate 				return (NULL);
19490Sstevel@tonic-gate 			}
19500Sstevel@tonic-gate 		}
19510Sstevel@tonic-gate 		*allmsgsigs = S_INPUT | S_RDNORM;
19520Sstevel@tonic-gate 		*pollwakeups = POLLIN | POLLRDNORM;
19530Sstevel@tonic-gate 		*wakeups = RSLEEP;
19540Sstevel@tonic-gate 		return (mp);
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	case T_OPTDATA_IND: {
19580Sstevel@tonic-gate 		struct T_optdata_ind	*tdi = &tpr->optdata_ind;
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_optdata_ind)) {
19611548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
19620Sstevel@tonic-gate 			    "sockfs: Too short T_OPTDATA_IND. Len = %ld\n",
19630Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
19640Sstevel@tonic-gate 			freemsg(mp);
19650Sstevel@tonic-gate 			return (NULL);
19660Sstevel@tonic-gate 		}
19670Sstevel@tonic-gate 		/*
19680Sstevel@tonic-gate 		 * Allow zero-length messages carrying options.
19690Sstevel@tonic-gate 		 * This is used when carrying the SO_UNIX_CLOSE option.
19700Sstevel@tonic-gate 		 */
19710Sstevel@tonic-gate 		if (so->so_family == AF_UNIX && msgdsize(mp->b_cont) == 0 &&
19720Sstevel@tonic-gate 		    tdi->OPT_length != 0) {
19730Sstevel@tonic-gate 			/*
19740Sstevel@tonic-gate 			 * Attempt to extract AF_UNIX close indication
19750Sstevel@tonic-gate 			 * from the options. Ignore any other options -
19760Sstevel@tonic-gate 			 * those are handled once the message is removed
19770Sstevel@tonic-gate 			 * from the queue.
19780Sstevel@tonic-gate 			 * The close indication message should not carry data.
19790Sstevel@tonic-gate 			 */
19800Sstevel@tonic-gate 			void *opt;
19810Sstevel@tonic-gate 			t_uscalar_t optlen = tdi->OPT_length;
19820Sstevel@tonic-gate 
19830Sstevel@tonic-gate 			opt = sogetoff(mp, tdi->OPT_offset,
19845753Sgww 			    optlen, __TPI_ALIGN_SIZE);
19850Sstevel@tonic-gate 			if (opt == NULL) {
19860Sstevel@tonic-gate 				/* The len/off falls outside mp */
19870Sstevel@tonic-gate 				freemsg(mp);
19880Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
19890Sstevel@tonic-gate 				soseterror(so, EPROTO);
19900Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
19911548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
19920Sstevel@tonic-gate 				    "sockfs: T_optdata_ind with invalid "
19930Sstevel@tonic-gate 				    "optlen/offset %u/%d\n",
19940Sstevel@tonic-gate 				    optlen, tdi->OPT_offset);
19950Sstevel@tonic-gate 				return (NULL);
19960Sstevel@tonic-gate 			}
19970Sstevel@tonic-gate 			/*
19980Sstevel@tonic-gate 			 * If we received a close indication mark the
19990Sstevel@tonic-gate 			 * socket and discard this message.
20000Sstevel@tonic-gate 			 */
20010Sstevel@tonic-gate 			if (so_getopt_unix_close(opt, optlen)) {
20020Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
20030Sstevel@tonic-gate 				socantsendmore(so);
20048348SEric.Yu@Sun.COM 				sti->sti_faddr_valid = 0;
20050Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
20060Sstevel@tonic-gate 				strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
20070Sstevel@tonic-gate 				freemsg(mp);
20080Sstevel@tonic-gate 				return (NULL);
20090Sstevel@tonic-gate 			}
20100Sstevel@tonic-gate 		}
20110Sstevel@tonic-gate 		*allmsgsigs = S_INPUT | S_RDNORM;
20120Sstevel@tonic-gate 		*pollwakeups = POLLIN | POLLRDNORM;
20130Sstevel@tonic-gate 		*wakeups = RSLEEP;
20140Sstevel@tonic-gate 		return (mp);
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	case T_EXDATA_IND: {
20180Sstevel@tonic-gate 		mblk_t		*mctl, *mdata;
20192933Sss146032 		mblk_t *lbp;
20202933Sss146032 		union T_primitives *tprp;
20212933Sss146032 		struct stdata   *stp;
20222933Sss146032 		queue_t *qp;
20230Sstevel@tonic-gate 
20240Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_exdata_ind)) {
20251548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
20260Sstevel@tonic-gate 			    "sockfs: Too short T_EXDATA_IND. Len = %ld\n",
20270Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
20280Sstevel@tonic-gate 			freemsg(mp);
20290Sstevel@tonic-gate 			return (NULL);
20300Sstevel@tonic-gate 		}
20310Sstevel@tonic-gate 		/*
20320Sstevel@tonic-gate 		 * Ignore zero-length T_EXDATA_IND messages. These might be
20330Sstevel@tonic-gate 		 * generated by some transports.
20340Sstevel@tonic-gate 		 *
20350Sstevel@tonic-gate 		 * This is needed to prevent read (which skips the M_PROTO
20360Sstevel@tonic-gate 		 * part) to unexpectedly return 0 (or return EWOULDBLOCK
20370Sstevel@tonic-gate 		 * on a non-blocking socket after select/poll has indicated
20380Sstevel@tonic-gate 		 * that data is available).
20390Sstevel@tonic-gate 		 */
20400Sstevel@tonic-gate 		dprintso(so, 1,
20415753Sgww 		    ("T_EXDATA_IND(%p): counts %d/%d state %s\n",
20428348SEric.Yu@Sun.COM 		    (void *)vp, sti->sti_oobsigcnt, sti->sti_oobcnt,
20435753Sgww 		    pr_state(so->so_state, so->so_mode)));
20440Sstevel@tonic-gate 
20450Sstevel@tonic-gate 		if (msgdsize(mp->b_cont) == 0) {
20460Sstevel@tonic-gate 			dprintso(so, 0,
20475753Sgww 			    ("strsock_proto: zero length T_EXDATA_IND\n"));
20480Sstevel@tonic-gate 			freemsg(mp);
20490Sstevel@tonic-gate 			return (NULL);
20500Sstevel@tonic-gate 		}
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 		/*
20530Sstevel@tonic-gate 		 * Split into the T_EXDATA_IND and the M_DATA part.
20540Sstevel@tonic-gate 		 * We process these three pieces separately:
20550Sstevel@tonic-gate 		 *	signal generation
20560Sstevel@tonic-gate 		 *	handling T_EXDATA_IND
20570Sstevel@tonic-gate 		 *	handling M_DATA component
20580Sstevel@tonic-gate 		 */
20590Sstevel@tonic-gate 		mctl = mp;
20600Sstevel@tonic-gate 		mdata = mctl->b_cont;
20610Sstevel@tonic-gate 		mctl->b_cont = NULL;
20620Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
20630Sstevel@tonic-gate 		so_oob_sig(so, 0, allmsgsigs, pollwakeups);
20640Sstevel@tonic-gate 		mctl = so_oob_exdata(so, mctl, allmsgsigs, pollwakeups);
20650Sstevel@tonic-gate 		mdata = so_oob_data(so, mdata, allmsgsigs, pollwakeups);
20660Sstevel@tonic-gate 
20672933Sss146032 		stp = vp->v_stream;
20682933Sss146032 		ASSERT(stp != NULL);
20692933Sss146032 		qp = _RD(stp->sd_wrq);
20702933Sss146032 
20712933Sss146032 		mutex_enter(QLOCK(qp));
20722933Sss146032 		lbp = qp->q_last;
20732933Sss146032 
20742933Sss146032 		/*
20752933Sss146032 		 * We want to avoid queueing up a string of T_EXDATA_IND
20762933Sss146032 		 * messages with no intervening data messages at the stream
20772933Sss146032 		 * head. These messages contribute to the total message
20782933Sss146032 		 * count. Eventually this can lead to STREAMS flow contol
20792933Sss146032 		 * and also cause TCP to advertise a zero window condition
20802933Sss146032 		 * to the peer. This can happen in the degenerate case where
20812933Sss146032 		 * the sender and receiver exchange only OOB data. The sender
20822933Sss146032 		 * only sends messages with MSG_OOB flag and the receiver
20832933Sss146032 		 * receives only MSG_OOB messages and does not use SO_OOBINLINE.
20842933Sss146032 		 * An example of this scenario has been reported in applications
20852933Sss146032 		 * that use OOB data to exchange heart beats. Flow control
20862933Sss146032 		 * relief will never happen if the application only reads OOB
20872933Sss146032 		 * data which is done directly by sorecvoob() and the
20882933Sss146032 		 * T_EXDATA_IND messages at the streamhead won't be consumed.
20892933Sss146032 		 * Note that there is no correctness issue in compressing the
20902933Sss146032 		 * string of T_EXDATA_IND messages into a single T_EXDATA_IND
20912933Sss146032 		 * message. A single read that does not specify MSG_OOB will
20922933Sss146032 		 * read across all the marks in a loop in sotpi_recvmsg().
20932933Sss146032 		 * Each mark is individually distinguishable only if the
20942933Sss146032 		 * T_EXDATA_IND messages are separated by data messages.
20952933Sss146032 		 */
20962933Sss146032 		if ((qp->q_first != NULL) && (DB_TYPE(lbp) == M_PROTO)) {
20972933Sss146032 			tprp = (union T_primitives *)lbp->b_rptr;
20982933Sss146032 			if ((tprp->type == T_EXDATA_IND) &&
20992933Sss146032 			    !(so->so_options & SO_OOBINLINE)) {
21002933Sss146032 
21012933Sss146032 				/*
21022933Sss146032 				 * free the new M_PROTO message
21032933Sss146032 				 */
21042933Sss146032 				freemsg(mctl);
21052933Sss146032 
21062933Sss146032 				/*
21072933Sss146032 				 * adjust the OOB count and OOB	signal count
21082933Sss146032 				 * just incremented for the new OOB data.
21092933Sss146032 				 */
21108348SEric.Yu@Sun.COM 				sti->sti_oobcnt--;
21118348SEric.Yu@Sun.COM 				sti->sti_oobsigcnt--;
21122933Sss146032 				mutex_exit(QLOCK(qp));
21132933Sss146032 				mutex_exit(&so->so_lock);
21142933Sss146032 				return (NULL);
21152933Sss146032 			}
21162933Sss146032 		}
21172933Sss146032 		mutex_exit(QLOCK(qp));
21182933Sss146032 
21190Sstevel@tonic-gate 		/*
21200Sstevel@tonic-gate 		 * Pass the T_EXDATA_IND and the M_DATA back separately
21210Sstevel@tonic-gate 		 * by using b_next linkage. (The stream head will queue any
21220Sstevel@tonic-gate 		 * b_next linked messages separately.) This is needed
21230Sstevel@tonic-gate 		 * since MSGMARK applies to the last by of the message
21240Sstevel@tonic-gate 		 * hence we can not have any M_DATA component attached
21250Sstevel@tonic-gate 		 * to the marked T_EXDATA_IND. Note that the stream head
21260Sstevel@tonic-gate 		 * will not consolidate M_DATA messages onto an MSGMARK'ed
21270Sstevel@tonic-gate 		 * message in order to preserve the constraint that
21280Sstevel@tonic-gate 		 * the T_EXDATA_IND always is a separate message.
21290Sstevel@tonic-gate 		 */
21300Sstevel@tonic-gate 		ASSERT(mctl != NULL);
21310Sstevel@tonic-gate 		mctl->b_next = mdata;
21320Sstevel@tonic-gate 		mp = mctl;
21330Sstevel@tonic-gate #ifdef DEBUG
21340Sstevel@tonic-gate 		if (mdata == NULL) {
21350Sstevel@tonic-gate 			dprintso(so, 1,
21365753Sgww 			    ("after outofline T_EXDATA_IND(%p): "
21375753Sgww 			    "counts %d/%d  poll 0x%x sig 0x%x state %s\n",
21388348SEric.Yu@Sun.COM 			    (void *)vp, sti->sti_oobsigcnt,
21398348SEric.Yu@Sun.COM 			    sti->sti_oobcnt, *pollwakeups, *allmsgsigs,
21405753Sgww 			    pr_state(so->so_state, so->so_mode)));
21410Sstevel@tonic-gate 		} else {
21420Sstevel@tonic-gate 			dprintso(so, 1,
21435753Sgww 			    ("after inline T_EXDATA_IND(%p): "
21445753Sgww 			    "counts %d/%d  poll 0x%x sig 0x%x state %s\n",
21458348SEric.Yu@Sun.COM 			    (void *)vp, sti->sti_oobsigcnt,
21468348SEric.Yu@Sun.COM 			    sti->sti_oobcnt, *pollwakeups, *allmsgsigs,
21475753Sgww 			    pr_state(so->so_state, so->so_mode)));
21480Sstevel@tonic-gate 		}
21490Sstevel@tonic-gate #endif /* DEBUG */
21500Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
21510Sstevel@tonic-gate 		*wakeups = RSLEEP;
21520Sstevel@tonic-gate 		return (mp);
21530Sstevel@tonic-gate 	}
21540Sstevel@tonic-gate 
21550Sstevel@tonic-gate 	case T_CONN_CON: {
21560Sstevel@tonic-gate 		struct T_conn_con	*conn_con;
21570Sstevel@tonic-gate 		void			*addr;
21580Sstevel@tonic-gate 		t_uscalar_t		addrlen;
21590Sstevel@tonic-gate 
21600Sstevel@tonic-gate 		/*
21610Sstevel@tonic-gate 		 * Verify the state, update the state to ISCONNECTED,
21620Sstevel@tonic-gate 		 * record the potentially new address in the message,
21630Sstevel@tonic-gate 		 * and drop the message.
21640Sstevel@tonic-gate 		 */
21650Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_conn_con)) {
21661548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
21670Sstevel@tonic-gate 			    "sockfs: Too short T_CONN_CON. Len = %ld\n",
21680Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
21690Sstevel@tonic-gate 			freemsg(mp);
21700Sstevel@tonic-gate 			return (NULL);
21710Sstevel@tonic-gate 		}
21720Sstevel@tonic-gate 
21730Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
21740Sstevel@tonic-gate 		if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) !=
21750Sstevel@tonic-gate 		    SS_ISCONNECTING) {
21760Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
21770Sstevel@tonic-gate 			dprintso(so, 1,
21785753Sgww 			    ("T_CONN_CON: state %x\n", so->so_state));
21790Sstevel@tonic-gate 			freemsg(mp);
21800Sstevel@tonic-gate 			return (NULL);
21810Sstevel@tonic-gate 		}
21820Sstevel@tonic-gate 
21830Sstevel@tonic-gate 		conn_con = &tpr->conn_con;
21840Sstevel@tonic-gate 		addrlen = conn_con->RES_length;
21850Sstevel@tonic-gate 		/*
21860Sstevel@tonic-gate 		 * Allow the address to be of different size than sent down
21870Sstevel@tonic-gate 		 * in the T_CONN_REQ as long as it doesn't exceed the maxlen.
21880Sstevel@tonic-gate 		 * For AF_UNIX require the identical length.
21890Sstevel@tonic-gate 		 */
21900Sstevel@tonic-gate 		if (so->so_family == AF_UNIX ?
21918348SEric.Yu@Sun.COM 		    addrlen != (t_uscalar_t)sizeof (sti->sti_ux_laddr) :
21928348SEric.Yu@Sun.COM 		    addrlen > (t_uscalar_t)sti->sti_faddr_maxlen) {
21931548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
21940Sstevel@tonic-gate 			    "sockfs: T_conn_con with different "
21950Sstevel@tonic-gate 			    "length %u/%d\n",
21960Sstevel@tonic-gate 			    addrlen, conn_con->RES_length);
21970Sstevel@tonic-gate 			soisdisconnected(so, EPROTO);
21988348SEric.Yu@Sun.COM 			sti->sti_laddr_valid = 0;
21998348SEric.Yu@Sun.COM 			sti->sti_faddr_valid = 0;
22000Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
22010Sstevel@tonic-gate 			strsetrerror(SOTOV(so), 0, 0, sogetrderr);
22020Sstevel@tonic-gate 			strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
22030Sstevel@tonic-gate 			strseteof(SOTOV(so), 1);
22040Sstevel@tonic-gate 			freemsg(mp);
22050Sstevel@tonic-gate 			/*
22060Sstevel@tonic-gate 			 * strseteof takes care of read side wakeups,
22070Sstevel@tonic-gate 			 * pollwakeups, and signals.
22080Sstevel@tonic-gate 			 */
22090Sstevel@tonic-gate 			*wakeups = WSLEEP;
22100Sstevel@tonic-gate 			*allmsgsigs = S_OUTPUT;
22110Sstevel@tonic-gate 			*pollwakeups = POLLOUT;
22120Sstevel@tonic-gate 			return (NULL);
22130Sstevel@tonic-gate 		}
22140Sstevel@tonic-gate 		addr = sogetoff(mp, conn_con->RES_offset, addrlen, 1);
22150Sstevel@tonic-gate 		if (addr == NULL) {
22161548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
22170Sstevel@tonic-gate 			    "sockfs: T_conn_con with invalid "
22180Sstevel@tonic-gate 			    "addrlen/offset %u/%d\n",
22190Sstevel@tonic-gate 			    addrlen, conn_con->RES_offset);
22200Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
22210Sstevel@tonic-gate 			strsetrerror(SOTOV(so), 0, 0, sogetrderr);
22220Sstevel@tonic-gate 			strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
22230Sstevel@tonic-gate 			strseteof(SOTOV(so), 1);
22240Sstevel@tonic-gate 			freemsg(mp);
22250Sstevel@tonic-gate 			/*
22260Sstevel@tonic-gate 			 * strseteof takes care of read side wakeups,
22270Sstevel@tonic-gate 			 * pollwakeups, and signals.
22280Sstevel@tonic-gate 			 */
22290Sstevel@tonic-gate 			*wakeups = WSLEEP;
22300Sstevel@tonic-gate 			*allmsgsigs = S_OUTPUT;
22310Sstevel@tonic-gate 			*pollwakeups = POLLOUT;
22320Sstevel@tonic-gate 			return (NULL);
22330Sstevel@tonic-gate 		}
22340Sstevel@tonic-gate 
22350Sstevel@tonic-gate 		/*
22360Sstevel@tonic-gate 		 * Save for getpeername.
22370Sstevel@tonic-gate 		 */
22380Sstevel@tonic-gate 		if (so->so_family != AF_UNIX) {
22398348SEric.Yu@Sun.COM 			sti->sti_faddr_len = (socklen_t)addrlen;
22408348SEric.Yu@Sun.COM 			ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
22418348SEric.Yu@Sun.COM 			bcopy(addr, sti->sti_faddr_sa, addrlen);
22428348SEric.Yu@Sun.COM 			sti->sti_faddr_valid = 1;
22430Sstevel@tonic-gate 		}
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 		if (so->so_peercred != NULL)
22460Sstevel@tonic-gate 			crfree(so->so_peercred);
22478778SErik.Nordmark@Sun.COM 		so->so_peercred = msg_getcred(mp, &so->so_cpid);
22480Sstevel@tonic-gate 		if (so->so_peercred != NULL)
22490Sstevel@tonic-gate 			crhold(so->so_peercred);
22500Sstevel@tonic-gate 
22510Sstevel@tonic-gate 		/* Wakeup anybody sleeping in sowaitconnected */
22520Sstevel@tonic-gate 		soisconnected(so);
22530Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 		/*
22560Sstevel@tonic-gate 		 * The socket is now available for sending data.
22570Sstevel@tonic-gate 		 */
22580Sstevel@tonic-gate 		*wakeups = WSLEEP;
22590Sstevel@tonic-gate 		*allmsgsigs = S_OUTPUT;
22600Sstevel@tonic-gate 		*pollwakeups = POLLOUT;
22610Sstevel@tonic-gate 		freemsg(mp);
22620Sstevel@tonic-gate 		return (NULL);
22630Sstevel@tonic-gate 	}
22640Sstevel@tonic-gate 
2265898Skais 	/*
2266898Skais 	 * Extra processing in case of an SSL proxy, before queuing or
2267898Skais 	 * forwarding to the fallback endpoint
2268898Skais 	 */
2269898Skais 	case T_SSL_PROXY_CONN_IND:
22700Sstevel@tonic-gate 	case T_CONN_IND:
22710Sstevel@tonic-gate 		/*
22720Sstevel@tonic-gate 		 * Verify the min size and queue the message on
22738348SEric.Yu@Sun.COM 		 * the sti_conn_ind_head/tail list.
22740Sstevel@tonic-gate 		 */
22750Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_conn_ind)) {
22761548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
22770Sstevel@tonic-gate 			    "sockfs: Too short T_CONN_IND. Len = %ld\n",
22780Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
22790Sstevel@tonic-gate 			freemsg(mp);
22800Sstevel@tonic-gate 			return (NULL);
22810Sstevel@tonic-gate 		}
22820Sstevel@tonic-gate 
2283*11861SMarek.Pospisil@Sun.COM 		if (auditing)
22840Sstevel@tonic-gate 			audit_sock(T_CONN_IND, strvp2wq(vp), mp, 0);
22850Sstevel@tonic-gate 		if (!(so->so_state & SS_ACCEPTCONN)) {
22861548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
22870Sstevel@tonic-gate 			    "sockfs: T_conn_ind on non-listening socket\n");
22880Sstevel@tonic-gate 			freemsg(mp);
22890Sstevel@tonic-gate 			return (NULL);
22900Sstevel@tonic-gate 		}
2291898Skais 
2292898Skais 		if (tpr->type == T_SSL_PROXY_CONN_IND && mp->b_cont == NULL) {
2293898Skais 			/* No context: need to fall back */
2294898Skais 			struct sonode *fbso;
2295898Skais 			stdata_t *fbstp;
2296898Skais 
2297898Skais 			tpr->type = T_CONN_IND;
2298898Skais 
22998348SEric.Yu@Sun.COM 			fbso = kssl_find_fallback(sti->sti_kssl_ent);
2300898Skais 
2301898Skais 			/*
2302898Skais 			 * No fallback: the remote will timeout and
2303898Skais 			 * disconnect.
2304898Skais 			 */
2305898Skais 			if (fbso == NULL) {
2306898Skais 				freemsg(mp);
2307898Skais 				return (NULL);
2308898Skais 			}
2309898Skais 			fbstp = SOTOV(fbso)->v_stream;
2310898Skais 			qreply(fbstp->sd_wrq->q_next, mp);
2311898Skais 			return (NULL);
2312898Skais 		}
23130Sstevel@tonic-gate 		soqueueconnind(so, mp);
23140Sstevel@tonic-gate 		*allmsgsigs = S_INPUT | S_RDNORM;
23150Sstevel@tonic-gate 		*pollwakeups = POLLIN | POLLRDNORM;
23160Sstevel@tonic-gate 		*wakeups = RSLEEP;
23170Sstevel@tonic-gate 		return (NULL);
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	case T_ORDREL_IND:
23200Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_ordrel_ind)) {
23211548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
23220Sstevel@tonic-gate 			    "sockfs: Too short T_ORDREL_IND. Len = %ld\n",
23230Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
23240Sstevel@tonic-gate 			freemsg(mp);
23250Sstevel@tonic-gate 			return (NULL);
23260Sstevel@tonic-gate 		}
23270Sstevel@tonic-gate 
23280Sstevel@tonic-gate 		/*
23290Sstevel@tonic-gate 		 * Some providers send this when not fully connected.
23300Sstevel@tonic-gate 		 * SunLink X.25 needs to retrieve disconnect reason after
23310Sstevel@tonic-gate 		 * disconnect for compatibility. It uses T_ORDREL_IND
23320Sstevel@tonic-gate 		 * instead of T_DISCON_IND so that it may use the
23330Sstevel@tonic-gate 		 * endpoint after a connect failure to retrieve the
23340Sstevel@tonic-gate 		 * reason using an ioctl. Thus we explicitly clear
23350Sstevel@tonic-gate 		 * SS_ISCONNECTING here for SunLink X.25.
23360Sstevel@tonic-gate 		 * This is a needed TPI violation.
23370Sstevel@tonic-gate 		 */
23380Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
23390Sstevel@tonic-gate 		so->so_state &= ~SS_ISCONNECTING;
23400Sstevel@tonic-gate 		socantrcvmore(so);
23410Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
23420Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
23430Sstevel@tonic-gate 		/*
23440Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
23450Sstevel@tonic-gate 		 * pollwakeups, and signals.
23460Sstevel@tonic-gate 		 */
23470Sstevel@tonic-gate 		freemsg(mp);
23480Sstevel@tonic-gate 		return (NULL);
23490Sstevel@tonic-gate 
23500Sstevel@tonic-gate 	case T_DISCON_IND:
23510Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_discon_ind)) {
23521548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
23530Sstevel@tonic-gate 			    "sockfs: Too short T_DISCON_IND. Len = %ld\n",
23540Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
23550Sstevel@tonic-gate 			freemsg(mp);
23560Sstevel@tonic-gate 			return (NULL);
23570Sstevel@tonic-gate 		}
23580Sstevel@tonic-gate 		if (so->so_state & SS_ACCEPTCONN) {
23590Sstevel@tonic-gate 			/*
23600Sstevel@tonic-gate 			 * This is a listener. Look for a queued T_CONN_IND
23610Sstevel@tonic-gate 			 * with a matching sequence number and remove it
23620Sstevel@tonic-gate 			 * from the list.
23630Sstevel@tonic-gate 			 * It is normal to not find the sequence number since
23640Sstevel@tonic-gate 			 * the soaccept might have already dequeued it
23650Sstevel@tonic-gate 			 * (in which case the T_CONN_RES will fail with
23660Sstevel@tonic-gate 			 * TBADSEQ).
23670Sstevel@tonic-gate 			 */
23680Sstevel@tonic-gate 			(void) soflushconnind(so, tpr->discon_ind.SEQ_number);
23690Sstevel@tonic-gate 			freemsg(mp);
23700Sstevel@tonic-gate 			return (0);
23710Sstevel@tonic-gate 		}
23720Sstevel@tonic-gate 
23730Sstevel@tonic-gate 		/*
23740Sstevel@tonic-gate 		 * Not a listener
23750Sstevel@tonic-gate 		 *
23760Sstevel@tonic-gate 		 * If SS_CANTRCVMORE for AF_UNIX ignore the discon_reason.
23770Sstevel@tonic-gate 		 * Such a discon_ind appears when the peer has first done
23780Sstevel@tonic-gate 		 * a shutdown() followed by a close() in which case we just
23790Sstevel@tonic-gate 		 * want to record socantsendmore.
23800Sstevel@tonic-gate 		 * In this case sockfs first receives a T_ORDREL_IND followed
23810Sstevel@tonic-gate 		 * by a T_DISCON_IND.
23820Sstevel@tonic-gate 		 * Note that for other transports (e.g. TCP) we need to handle
23830Sstevel@tonic-gate 		 * the discon_ind in this case since it signals an error.
23840Sstevel@tonic-gate 		 */
23850Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
23860Sstevel@tonic-gate 		if ((so->so_state & SS_CANTRCVMORE) &&
23870Sstevel@tonic-gate 		    (so->so_family == AF_UNIX)) {
23880Sstevel@tonic-gate 			socantsendmore(so);
23898348SEric.Yu@Sun.COM 			sti->sti_faddr_valid = 0;
23900Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
23910Sstevel@tonic-gate 			strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
23920Sstevel@tonic-gate 			dprintso(so, 1,
23935753Sgww 			    ("T_DISCON_IND: error %d\n", so->so_error));
23940Sstevel@tonic-gate 			freemsg(mp);
23950Sstevel@tonic-gate 			/*
23960Sstevel@tonic-gate 			 * Set these variables for caller to process them.
23970Sstevel@tonic-gate 			 * For the else part where T_DISCON_IND is processed,
23980Sstevel@tonic-gate 			 * this will be done in the function being called
23990Sstevel@tonic-gate 			 * (strsock_discon_ind())
24000Sstevel@tonic-gate 			 */
24010Sstevel@tonic-gate 			*wakeups = WSLEEP;
24020Sstevel@tonic-gate 			*allmsgsigs = S_OUTPUT;
24030Sstevel@tonic-gate 			*pollwakeups = POLLOUT;
24040Sstevel@tonic-gate 		} else if (so->so_flag & (SOASYNC_UNBIND | SOLOCKED)) {
24050Sstevel@tonic-gate 			/*
24060Sstevel@tonic-gate 			 * Deferred processing of T_DISCON_IND
24070Sstevel@tonic-gate 			 */
24080Sstevel@tonic-gate 			so_save_discon_ind(so, mp, strsock_discon_ind);
24090Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
24100Sstevel@tonic-gate 		} else {
24110Sstevel@tonic-gate 			/*
24120Sstevel@tonic-gate 			 * Process T_DISCON_IND now
24130Sstevel@tonic-gate 			 */
24140Sstevel@tonic-gate 			(void) strsock_discon_ind(so, mp);
24150Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
24160Sstevel@tonic-gate 		}
24170Sstevel@tonic-gate 		return (NULL);
24180Sstevel@tonic-gate 
24190Sstevel@tonic-gate 	case T_UDERROR_IND: {
24200Sstevel@tonic-gate 		struct T_uderror_ind	*tudi = &tpr->uderror_ind;
24210Sstevel@tonic-gate 		void			*addr;
24220Sstevel@tonic-gate 		t_uscalar_t		addrlen;
24230Sstevel@tonic-gate 		int			error;
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 		dprintso(so, 0,
24265753Sgww 		    ("T_UDERROR_IND: error %d\n", tudi->ERROR_type));
24270Sstevel@tonic-gate 
24280Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_uderror_ind)) {
24291548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
24300Sstevel@tonic-gate 			    "sockfs: Too short T_UDERROR_IND. Len = %ld\n",
24310Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
24320Sstevel@tonic-gate 			freemsg(mp);
24330Sstevel@tonic-gate 			return (NULL);
24340Sstevel@tonic-gate 		}
24350Sstevel@tonic-gate 		/* Ignore on connection-oriented transports */
24360Sstevel@tonic-gate 		if (so->so_mode & SM_CONNREQUIRED) {
24370Sstevel@tonic-gate 			freemsg(mp);
24380Sstevel@tonic-gate 			eprintsoline(so, 0);
24391548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
24400Sstevel@tonic-gate 			    "sockfs: T_uderror_ind on connection-oriented "
24410Sstevel@tonic-gate 			    "transport\n");
24420Sstevel@tonic-gate 			return (NULL);
24430Sstevel@tonic-gate 		}
24440Sstevel@tonic-gate 		addrlen = tudi->DEST_length;
24450Sstevel@tonic-gate 		addr = sogetoff(mp, tudi->DEST_offset, addrlen, 1);
24460Sstevel@tonic-gate 		if (addr == NULL) {
24471548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
24480Sstevel@tonic-gate 			    "sockfs: T_uderror_ind with invalid "
24490Sstevel@tonic-gate 			    "addrlen/offset %u/%d\n",
24500Sstevel@tonic-gate 			    addrlen, tudi->DEST_offset);
24510Sstevel@tonic-gate 			freemsg(mp);
24520Sstevel@tonic-gate 			return (NULL);
24530Sstevel@tonic-gate 		}
24540Sstevel@tonic-gate 
24550Sstevel@tonic-gate 		/* Verify source address for connected socket. */
24560Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24570Sstevel@tonic-gate 		if (so->so_state & SS_ISCONNECTED) {
24580Sstevel@tonic-gate 			void *faddr;
24590Sstevel@tonic-gate 			t_uscalar_t faddr_len;
24600Sstevel@tonic-gate 			boolean_t match = B_FALSE;
24610Sstevel@tonic-gate 
24620Sstevel@tonic-gate 			switch (so->so_family) {
24630Sstevel@tonic-gate 			case AF_INET: {
24640Sstevel@tonic-gate 				/* Compare just IP address and port */
24650Sstevel@tonic-gate 				struct sockaddr_in *sin1, *sin2;
24660Sstevel@tonic-gate 
24678348SEric.Yu@Sun.COM 				sin1 = (struct sockaddr_in *)sti->sti_faddr_sa;
24680Sstevel@tonic-gate 				sin2 = (struct sockaddr_in *)addr;
24690Sstevel@tonic-gate 				if (addrlen == sizeof (struct sockaddr_in) &&
24700Sstevel@tonic-gate 				    sin1->sin_port == sin2->sin_port &&
24710Sstevel@tonic-gate 				    sin1->sin_addr.s_addr ==
24720Sstevel@tonic-gate 				    sin2->sin_addr.s_addr)
24730Sstevel@tonic-gate 					match = B_TRUE;
24740Sstevel@tonic-gate 				break;
24750Sstevel@tonic-gate 			}
24760Sstevel@tonic-gate 			case AF_INET6: {
24770Sstevel@tonic-gate 				/* Compare just IP address and port. Not flow */
24780Sstevel@tonic-gate 				struct sockaddr_in6 *sin1, *sin2;
24790Sstevel@tonic-gate 
24808348SEric.Yu@Sun.COM 				sin1 = (struct sockaddr_in6 *)sti->sti_faddr_sa;
24810Sstevel@tonic-gate 				sin2 = (struct sockaddr_in6 *)addr;
24820Sstevel@tonic-gate 				if (addrlen == sizeof (struct sockaddr_in6) &&
24830Sstevel@tonic-gate 				    sin1->sin6_port == sin2->sin6_port &&
24840Sstevel@tonic-gate 				    IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
24855753Sgww 				    &sin2->sin6_addr))
24860Sstevel@tonic-gate 					match = B_TRUE;
24870Sstevel@tonic-gate 				break;
24880Sstevel@tonic-gate 			}
24890Sstevel@tonic-gate 			case AF_UNIX:
24908348SEric.Yu@Sun.COM 				faddr = &sti->sti_ux_faddr;
24910Sstevel@tonic-gate 				faddr_len =
24928348SEric.Yu@Sun.COM 				    (t_uscalar_t)sizeof (sti->sti_ux_faddr);
24930Sstevel@tonic-gate 				if (faddr_len == addrlen &&
24940Sstevel@tonic-gate 				    bcmp(addr, faddr, addrlen) == 0)
24950Sstevel@tonic-gate 					match = B_TRUE;
24960Sstevel@tonic-gate 				break;
24970Sstevel@tonic-gate 			default:
24988348SEric.Yu@Sun.COM 				faddr = sti->sti_faddr_sa;
24998348SEric.Yu@Sun.COM 				faddr_len = (t_uscalar_t)sti->sti_faddr_len;
25000Sstevel@tonic-gate 				if (faddr_len == addrlen &&
25010Sstevel@tonic-gate 				    bcmp(addr, faddr, addrlen) == 0)
25020Sstevel@tonic-gate 					match = B_TRUE;
25030Sstevel@tonic-gate 				break;
25040Sstevel@tonic-gate 			}
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 			if (!match) {
25070Sstevel@tonic-gate #ifdef DEBUG
25080Sstevel@tonic-gate 				dprintso(so, 0,
25095753Sgww 				    ("sockfs: T_UDERR_IND mismatch: %s - ",
25105753Sgww 				    pr_addr(so->so_family,
25118348SEric.Yu@Sun.COM 				    (struct sockaddr *)addr, addrlen)));
25120Sstevel@tonic-gate 				dprintso(so, 0, ("%s\n",
25138348SEric.Yu@Sun.COM 				    pr_addr(so->so_family, sti->sti_faddr_sa,
25148348SEric.Yu@Sun.COM 				    sti->sti_faddr_len)));
25150Sstevel@tonic-gate #endif /* DEBUG */
25160Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
25170Sstevel@tonic-gate 				freemsg(mp);
25180Sstevel@tonic-gate 				return (NULL);
25190Sstevel@tonic-gate 			}
25200Sstevel@tonic-gate 			/*
25210Sstevel@tonic-gate 			 * Make the write error nonpersistent. If the error
25220Sstevel@tonic-gate 			 * is zero we use ECONNRESET.
25230Sstevel@tonic-gate 			 * This assumes that the name space for ERROR_type
25240Sstevel@tonic-gate 			 * is the errno name space.
25250Sstevel@tonic-gate 			 */
25260Sstevel@tonic-gate 			if (tudi->ERROR_type != 0)
25270Sstevel@tonic-gate 				error = tudi->ERROR_type;
25280Sstevel@tonic-gate 			else
25290Sstevel@tonic-gate 				error = ECONNRESET;
25300Sstevel@tonic-gate 
25310Sstevel@tonic-gate 			soseterror(so, error);
25320Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
25330Sstevel@tonic-gate 			strsetrerror(SOTOV(so), 0, 0, sogetrderr);
25340Sstevel@tonic-gate 			strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
25350Sstevel@tonic-gate 			*wakeups = RSLEEP | WSLEEP;
25360Sstevel@tonic-gate 			*allmsgsigs = S_INPUT | S_RDNORM | S_OUTPUT;
25370Sstevel@tonic-gate 			*pollwakeups = POLLIN | POLLRDNORM | POLLOUT;
25380Sstevel@tonic-gate 			freemsg(mp);
25390Sstevel@tonic-gate 			return (NULL);
25400Sstevel@tonic-gate 		}
25410Sstevel@tonic-gate 		/*
25420Sstevel@tonic-gate 		 * If the application asked for delayed errors
25438348SEric.Yu@Sun.COM 		 * record the T_UDERROR_IND sti_eaddr_mp and the reason in
25448348SEric.Yu@Sun.COM 		 * sti_delayed_error for delayed error posting. If the reason
25450Sstevel@tonic-gate 		 * is zero use ECONNRESET.
25460Sstevel@tonic-gate 		 * Note that delayed error indications do not make sense for
25470Sstevel@tonic-gate 		 * AF_UNIX sockets since sendto checks that the destination
25480Sstevel@tonic-gate 		 * address is valid at the time of the sendto.
25490Sstevel@tonic-gate 		 */
25500Sstevel@tonic-gate 		if (!(so->so_options & SO_DGRAM_ERRIND)) {
25510Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
25520Sstevel@tonic-gate 			freemsg(mp);
25530Sstevel@tonic-gate 			return (NULL);
25540Sstevel@tonic-gate 		}
25558348SEric.Yu@Sun.COM 		if (sti->sti_eaddr_mp != NULL)
25568348SEric.Yu@Sun.COM 			freemsg(sti->sti_eaddr_mp);
25570Sstevel@tonic-gate 
25588348SEric.Yu@Sun.COM 		sti->sti_eaddr_mp = mp;
25590Sstevel@tonic-gate 		if (tudi->ERROR_type != 0)
25600Sstevel@tonic-gate 			error = tudi->ERROR_type;
25610Sstevel@tonic-gate 		else
25620Sstevel@tonic-gate 			error = ECONNRESET;
25638348SEric.Yu@Sun.COM 		sti->sti_delayed_error = (ushort_t)error;
25640Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
25650Sstevel@tonic-gate 		return (NULL);
25660Sstevel@tonic-gate 	}
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 	case T_ERROR_ACK:
25690Sstevel@tonic-gate 		dprintso(so, 0,
25705753Sgww 		    ("strsock_proto: T_ERROR_ACK for %d, error %d/%d\n",
25715753Sgww 		    tpr->error_ack.ERROR_prim,
25725753Sgww 		    tpr->error_ack.TLI_error,
25735753Sgww 		    tpr->error_ack.UNIX_error));
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_error_ack)) {
25761548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
25770Sstevel@tonic-gate 			    "sockfs: Too short T_ERROR_ACK. Len = %ld\n",
25780Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
25790Sstevel@tonic-gate 			freemsg(mp);
25800Sstevel@tonic-gate 			return (NULL);
25810Sstevel@tonic-gate 		}
25820Sstevel@tonic-gate 		/*
25830Sstevel@tonic-gate 		 * Check if we were waiting for the async message
25840Sstevel@tonic-gate 		 */
25850Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
25860Sstevel@tonic-gate 		if ((so->so_flag & SOASYNC_UNBIND) &&
25870Sstevel@tonic-gate 		    tpr->error_ack.ERROR_prim == T_UNBIND_REQ) {
25880Sstevel@tonic-gate 			so_unlock_single(so, SOASYNC_UNBIND);
25890Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
25900Sstevel@tonic-gate 			freemsg(mp);
25910Sstevel@tonic-gate 			return (NULL);
25920Sstevel@tonic-gate 		}
25930Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
25940Sstevel@tonic-gate 		soqueueack(so, mp);
25950Sstevel@tonic-gate 		return (NULL);
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	case T_OK_ACK:
25980Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_ok_ack)) {
25991548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
26000Sstevel@tonic-gate 			    "sockfs: Too short T_OK_ACK. Len = %ld\n",
26010Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
26020Sstevel@tonic-gate 			freemsg(mp);
26030Sstevel@tonic-gate 			return (NULL);
26040Sstevel@tonic-gate 		}
26050Sstevel@tonic-gate 		/*
26060Sstevel@tonic-gate 		 * Check if we were waiting for the async message
26070Sstevel@tonic-gate 		 */
26080Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
26090Sstevel@tonic-gate 		if ((so->so_flag & SOASYNC_UNBIND) &&
26100Sstevel@tonic-gate 		    tpr->ok_ack.CORRECT_prim == T_UNBIND_REQ) {
26110Sstevel@tonic-gate 			dprintso(so, 1,
26125753Sgww 			    ("strsock_proto: T_OK_ACK async unbind\n"));
26130Sstevel@tonic-gate 			so_unlock_single(so, SOASYNC_UNBIND);
26140Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
26150Sstevel@tonic-gate 			freemsg(mp);
26160Sstevel@tonic-gate 			return (NULL);
26170Sstevel@tonic-gate 		}
26180Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
26190Sstevel@tonic-gate 		soqueueack(so, mp);
26200Sstevel@tonic-gate 		return (NULL);
26210Sstevel@tonic-gate 
26220Sstevel@tonic-gate 	case T_INFO_ACK:
26230Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_info_ack)) {
26241548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
26250Sstevel@tonic-gate 			    "sockfs: Too short T_INFO_ACK. Len = %ld\n",
26260Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
26270Sstevel@tonic-gate 			freemsg(mp);
26280Sstevel@tonic-gate 			return (NULL);
26290Sstevel@tonic-gate 		}
26300Sstevel@tonic-gate 		soqueueack(so, mp);
26310Sstevel@tonic-gate 		return (NULL);
26320Sstevel@tonic-gate 
26330Sstevel@tonic-gate 	case T_CAPABILITY_ACK:
26340Sstevel@tonic-gate 		/*
26350Sstevel@tonic-gate 		 * A T_capability_ack need only be large enough to hold
26360Sstevel@tonic-gate 		 * the PRIM_type and CAP_bits1 fields; checking for anything
26370Sstevel@tonic-gate 		 * larger might reject a correct response from an older
26380Sstevel@tonic-gate 		 * provider.
26390Sstevel@tonic-gate 		 */
26400Sstevel@tonic-gate 		if (MBLKL(mp) < 2 * sizeof (t_uscalar_t)) {
26411548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
26420Sstevel@tonic-gate 			    "sockfs: Too short T_CAPABILITY_ACK. Len = %ld\n",
26430Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
26440Sstevel@tonic-gate 			freemsg(mp);
26450Sstevel@tonic-gate 			return (NULL);
26460Sstevel@tonic-gate 		}
26470Sstevel@tonic-gate 		soqueueack(so, mp);
26480Sstevel@tonic-gate 		return (NULL);
26490Sstevel@tonic-gate 
26500Sstevel@tonic-gate 	case T_BIND_ACK:
26510Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_bind_ack)) {
26521548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
26530Sstevel@tonic-gate 			    "sockfs: Too short T_BIND_ACK. Len = %ld\n",
26540Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
26550Sstevel@tonic-gate 			freemsg(mp);
26560Sstevel@tonic-gate 			return (NULL);
26570Sstevel@tonic-gate 		}
26580Sstevel@tonic-gate 		soqueueack(so, mp);
26590Sstevel@tonic-gate 		return (NULL);
26600Sstevel@tonic-gate 
26610Sstevel@tonic-gate 	case T_OPTMGMT_ACK:
26620Sstevel@tonic-gate 		if (MBLKL(mp) < sizeof (struct T_optmgmt_ack)) {
26631548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
26640Sstevel@tonic-gate 			    "sockfs: Too short T_OPTMGMT_ACK. Len = %ld\n",
26650Sstevel@tonic-gate 			    (ptrdiff_t)(MBLKL(mp)));
26660Sstevel@tonic-gate 			freemsg(mp);
26670Sstevel@tonic-gate 			return (NULL);
26680Sstevel@tonic-gate 		}
26690Sstevel@tonic-gate 		soqueueack(so, mp);
26700Sstevel@tonic-gate 		return (NULL);
26710Sstevel@tonic-gate 	default:
26720Sstevel@tonic-gate #ifdef DEBUG
26731548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
26745753Sgww 		    "sockfs: unknown TPI primitive %d received\n",
26755753Sgww 		    tpr->type);
26760Sstevel@tonic-gate #endif /* DEBUG */
26770Sstevel@tonic-gate 		freemsg(mp);
26780Sstevel@tonic-gate 		return (NULL);
26790Sstevel@tonic-gate 	}
26800Sstevel@tonic-gate }
26810Sstevel@tonic-gate 
26820Sstevel@tonic-gate /*
26830Sstevel@tonic-gate  * This routine is registered with the stream head to receive other
26840Sstevel@tonic-gate  * (non-data, and non-proto) messages.
26850Sstevel@tonic-gate  *
26860Sstevel@tonic-gate  * Returns NULL if the message was consumed.
26870Sstevel@tonic-gate  * Returns an mblk to make that mblk be processed by the stream head.
26880Sstevel@tonic-gate  *
26890Sstevel@tonic-gate  * Sets the return parameters (*wakeups, *firstmsgsigs, *allmsgsigs, and
26900Sstevel@tonic-gate  * *pollwakeups) for the stream head to take action on.
26910Sstevel@tonic-gate  */
26920Sstevel@tonic-gate static mblk_t *
26930Sstevel@tonic-gate strsock_misc(vnode_t *vp, mblk_t *mp,
26940Sstevel@tonic-gate 		strwakeup_t *wakeups, strsigset_t *firstmsgsigs,
26950Sstevel@tonic-gate 		strsigset_t *allmsgsigs, strpollset_t *pollwakeups)
26960Sstevel@tonic-gate {
26970Sstevel@tonic-gate 	struct sonode *so;
26988348SEric.Yu@Sun.COM 	sotpi_info_t *sti;
26990Sstevel@tonic-gate 
27000Sstevel@tonic-gate 	so = VTOSO(vp);
27018348SEric.Yu@Sun.COM 	sti = SOTOTPI(so);
27020Sstevel@tonic-gate 
27030Sstevel@tonic-gate 	dprintso(so, 1, ("strsock_misc(%p, %p, 0x%x)\n",
27047240Srh87107 	    (void *)vp, (void *)mp, DB_TYPE(mp)));
27050Sstevel@tonic-gate 
27060Sstevel@tonic-gate 	/* Set default return values */
27070Sstevel@tonic-gate 	*wakeups = *allmsgsigs = *firstmsgsigs = *pollwakeups = 0;
27080Sstevel@tonic-gate 
27090Sstevel@tonic-gate 	switch (DB_TYPE(mp)) {
27100Sstevel@tonic-gate 	case M_PCSIG:
27110Sstevel@tonic-gate 		/*
27120Sstevel@tonic-gate 		 * This assumes that an M_PCSIG for the urgent data arrives
27130Sstevel@tonic-gate 		 * before the corresponding T_EXDATA_IND.
27140Sstevel@tonic-gate 		 *
27150Sstevel@tonic-gate 		 * Note: Just like in SunOS 4.X and 4.4BSD a poll will be
27160Sstevel@tonic-gate 		 * awoken before the urgent data shows up.
27170Sstevel@tonic-gate 		 * For OOBINLINE this can result in select returning
27180Sstevel@tonic-gate 		 * only exceptions as opposed to except|read.
27190Sstevel@tonic-gate 		 */
27200Sstevel@tonic-gate 		if (*mp->b_rptr == SIGURG) {
27210Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
27220Sstevel@tonic-gate 			dprintso(so, 1,
27235753Sgww 			    ("SIGURG(%p): counts %d/%d state %s\n",
27248348SEric.Yu@Sun.COM 			    (void *)vp, sti->sti_oobsigcnt, sti->sti_oobcnt,
27255753Sgww 			    pr_state(so->so_state, so->so_mode)));
27260Sstevel@tonic-gate 			so_oob_sig(so, 1, allmsgsigs, pollwakeups);
27270Sstevel@tonic-gate 			dprintso(so, 1,
27285753Sgww 			    ("after SIGURG(%p): counts %d/%d "
27295753Sgww 			    " poll 0x%x sig 0x%x state %s\n",
27308348SEric.Yu@Sun.COM 			    (void *)vp, sti->sti_oobsigcnt, sti->sti_oobcnt,
27318348SEric.Yu@Sun.COM 			    *pollwakeups, *allmsgsigs,
27325753Sgww 			    pr_state(so->so_state, so->so_mode)));
27330Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
27340Sstevel@tonic-gate 		}
27350Sstevel@tonic-gate 		freemsg(mp);
27360Sstevel@tonic-gate 		return (NULL);
27370Sstevel@tonic-gate 
27380Sstevel@tonic-gate 	case M_SIG:
27390Sstevel@tonic-gate 	case M_HANGUP:
27400Sstevel@tonic-gate 	case M_UNHANGUP:
27410Sstevel@tonic-gate 	case M_ERROR:
27420Sstevel@tonic-gate 		/* M_ERRORs etc are ignored */
27430Sstevel@tonic-gate 		freemsg(mp);
27440Sstevel@tonic-gate 		return (NULL);
27450Sstevel@tonic-gate 
27460Sstevel@tonic-gate 	case M_FLUSH:
27470Sstevel@tonic-gate 		/*
27480Sstevel@tonic-gate 		 * Do not flush read queue. If the M_FLUSH
27490Sstevel@tonic-gate 		 * arrives because of an impending T_discon_ind
27500Sstevel@tonic-gate 		 * we still have to keep any queued data - this is part of
27510Sstevel@tonic-gate 		 * socket semantics.
27520Sstevel@tonic-gate 		 */
27530Sstevel@tonic-gate 		if (*mp->b_rptr & FLUSHW) {
27540Sstevel@tonic-gate 			*mp->b_rptr &= ~FLUSHR;
27550Sstevel@tonic-gate 			return (mp);
27560Sstevel@tonic-gate 		}
27570Sstevel@tonic-gate 		freemsg(mp);
27580Sstevel@tonic-gate 		return (NULL);
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	default:
27610Sstevel@tonic-gate 		return (mp);
27620Sstevel@tonic-gate 	}
27630Sstevel@tonic-gate }
27640Sstevel@tonic-gate 
27650Sstevel@tonic-gate 
27660Sstevel@tonic-gate /* Register to receive signals for certain events */
27670Sstevel@tonic-gate int
27680Sstevel@tonic-gate so_set_asyncsigs(vnode_t *vp, pid_t pgrp, int events, int mode, cred_t *cr)
27690Sstevel@tonic-gate {
27700Sstevel@tonic-gate 	struct strsigset ss;
27710Sstevel@tonic-gate 	int32_t rval;
27720Sstevel@tonic-gate 
27730Sstevel@tonic-gate 	/*
27740Sstevel@tonic-gate 	 * Note that SOLOCKED will be set except for the call from soaccept().
27750Sstevel@tonic-gate 	 */
27760Sstevel@tonic-gate 	ASSERT(!mutex_owned(&VTOSO(vp)->so_lock));
27770Sstevel@tonic-gate 	ss.ss_pid = pgrp;
27780Sstevel@tonic-gate 	ss.ss_events = events;
27790Sstevel@tonic-gate 	return (strioctl(vp, I_ESETSIG, (intptr_t)&ss, mode, K_TO_K, cr,
27800Sstevel@tonic-gate 	    &rval));
27810Sstevel@tonic-gate }
27820Sstevel@tonic-gate 
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate /* Register for events matching the SS_ASYNC flag */
27850Sstevel@tonic-gate int
27860Sstevel@tonic-gate so_set_events(struct sonode *so, vnode_t *vp, cred_t *cr)
27870Sstevel@tonic-gate {
27880Sstevel@tonic-gate 	int events = so->so_state & SS_ASYNC ?
27890Sstevel@tonic-gate 	    S_RDBAND | S_BANDURG | S_RDNORM | S_OUTPUT :
27900Sstevel@tonic-gate 	    S_RDBAND | S_BANDURG;
27910Sstevel@tonic-gate 
27920Sstevel@tonic-gate 	return (so_set_asyncsigs(vp, so->so_pgrp, events, 0, cr));
27930Sstevel@tonic-gate }
27940Sstevel@tonic-gate 
27950Sstevel@tonic-gate 
27960Sstevel@tonic-gate /* Change the SS_ASYNC flag, and update signal delivery if needed */
27970Sstevel@tonic-gate int
27980Sstevel@tonic-gate so_flip_async(struct sonode *so, vnode_t *vp, int mode, cred_t *cr)
27990Sstevel@tonic-gate {
28000Sstevel@tonic-gate 	ASSERT(mutex_owned(&so->so_lock));
28010Sstevel@tonic-gate 	if (so->so_pgrp != 0) {
28020Sstevel@tonic-gate 		int error;
28030Sstevel@tonic-gate 		int events = so->so_state & SS_ASYNC ?		/* Old flag */
28040Sstevel@tonic-gate 		    S_RDBAND | S_BANDURG :			/* New sigs */
28050Sstevel@tonic-gate 		    S_RDBAND | S_BANDURG | S_RDNORM | S_OUTPUT;
28060Sstevel@tonic-gate 
28070Sstevel@tonic-gate 		so_lock_single(so);
28080Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 		error = so_set_asyncsigs(vp, so->so_pgrp, events, mode, cr);
28110Sstevel@tonic-gate 
28120Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
28130Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
28140Sstevel@tonic-gate 		if (error)
28150Sstevel@tonic-gate 			return (error);
28160Sstevel@tonic-gate 	}
28170Sstevel@tonic-gate 	so->so_state ^= SS_ASYNC;
28180Sstevel@tonic-gate 	return (0);
28190Sstevel@tonic-gate }
28200Sstevel@tonic-gate 
28210Sstevel@tonic-gate /*
28220Sstevel@tonic-gate  * Set new pid/pgrp for SIGPOLL (or SIGIO for FIOASYNC mode), replacing
28230Sstevel@tonic-gate  * any existing one.  If passed zero, just clear the existing one.
28240Sstevel@tonic-gate  */
28250Sstevel@tonic-gate int
28260Sstevel@tonic-gate so_set_siggrp(struct sonode *so, vnode_t *vp, pid_t pgrp, int mode, cred_t *cr)
28270Sstevel@tonic-gate {
28280Sstevel@tonic-gate 	int events = so->so_state & SS_ASYNC ?
28290Sstevel@tonic-gate 	    S_RDBAND | S_BANDURG | S_RDNORM | S_OUTPUT :
28300Sstevel@tonic-gate 	    S_RDBAND | S_BANDURG;
28310Sstevel@tonic-gate 	int error;
28320Sstevel@tonic-gate 
28330Sstevel@tonic-gate 	ASSERT(mutex_owned(&so->so_lock));
28340Sstevel@tonic-gate 
28350Sstevel@tonic-gate 	/*
28360Sstevel@tonic-gate 	 * Change socket process (group).
28370Sstevel@tonic-gate 	 *
28380Sstevel@tonic-gate 	 * strioctl (via so_set_asyncsigs) will perform permission check and
28390Sstevel@tonic-gate 	 * also keep a PID_HOLD to prevent the pid from being reused.
28400Sstevel@tonic-gate 	 */
28410Sstevel@tonic-gate 	so_lock_single(so);
28420Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
28430Sstevel@tonic-gate 
28440Sstevel@tonic-gate 	if (pgrp != 0) {
28450Sstevel@tonic-gate 		dprintso(so, 1, ("setown: adding pgrp %d ev 0x%x\n",
28460Sstevel@tonic-gate 		    pgrp, events));
28470Sstevel@tonic-gate 		error = so_set_asyncsigs(vp, pgrp, events, mode, cr);
28480Sstevel@tonic-gate 		if (error != 0) {
28490Sstevel@tonic-gate 			eprintsoline(so, error);
28500Sstevel@tonic-gate 			goto bad;
28510Sstevel@tonic-gate 		}
28520Sstevel@tonic-gate 	}
28530Sstevel@tonic-gate 	/* Remove the previously registered process/group */
28540Sstevel@tonic-gate 	if (so->so_pgrp != 0) {
28550Sstevel@tonic-gate 		dprintso(so, 1, ("setown: removing pgrp %d\n", so->so_pgrp));
28560Sstevel@tonic-gate 		error = so_set_asyncsigs(vp, so->so_pgrp, 0, mode, cr);
28570Sstevel@tonic-gate 		if (error != 0) {
28580Sstevel@tonic-gate 			eprintsoline(so, error);
28590Sstevel@tonic-gate 			error = 0;
28600Sstevel@tonic-gate 		}
28610Sstevel@tonic-gate 	}
28620Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
28630Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
28640Sstevel@tonic-gate 	so->so_pgrp = pgrp;
28650Sstevel@tonic-gate 	return (0);
28660Sstevel@tonic-gate bad:
28670Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
28680Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
28690Sstevel@tonic-gate 	return (error);
28700Sstevel@tonic-gate }
28710Sstevel@tonic-gate 
28728348SEric.Yu@Sun.COM /*
28738348SEric.Yu@Sun.COM  * Wrapper for getmsg. If the socket has been converted to a stream
28748348SEric.Yu@Sun.COM  * pass the request to the stream head.
28758348SEric.Yu@Sun.COM  */
28768348SEric.Yu@Sun.COM int
28778348SEric.Yu@Sun.COM sock_getmsg(
28788348SEric.Yu@Sun.COM 	struct vnode *vp,
28798348SEric.Yu@Sun.COM 	struct strbuf *mctl,
28808348SEric.Yu@Sun.COM 	struct strbuf *mdata,
28818348SEric.Yu@Sun.COM 	uchar_t *prip,
28828348SEric.Yu@Sun.COM 	int *flagsp,
28838348SEric.Yu@Sun.COM 	int fmode,
28848348SEric.Yu@Sun.COM 	rval_t *rvp
28858348SEric.Yu@Sun.COM )
28868348SEric.Yu@Sun.COM {
28878348SEric.Yu@Sun.COM 	struct sonode *so;
28880Sstevel@tonic-gate 
28898348SEric.Yu@Sun.COM 	ASSERT(vp->v_type == VSOCK);
28908348SEric.Yu@Sun.COM 	/*
28918348SEric.Yu@Sun.COM 	 * Use the stream head to find the real socket vnode.
28928348SEric.Yu@Sun.COM 	 * This is needed when namefs sits above sockfs.  Some
28938348SEric.Yu@Sun.COM 	 * sockets (like SCTP) are not streams.
28948348SEric.Yu@Sun.COM 	 */
28958348SEric.Yu@Sun.COM 	if (!vp->v_stream) {
28968348SEric.Yu@Sun.COM 		return (ENOSTR);
28978348SEric.Yu@Sun.COM 	}
28988348SEric.Yu@Sun.COM 	ASSERT(vp->v_stream->sd_vnode);
28998348SEric.Yu@Sun.COM 	vp = vp->v_stream->sd_vnode;
29008348SEric.Yu@Sun.COM 	ASSERT(vn_matchops(vp, socket_vnodeops));
29018348SEric.Yu@Sun.COM 	so = VTOSO(vp);
29028348SEric.Yu@Sun.COM 
29038348SEric.Yu@Sun.COM 	dprintso(so, 1, ("sock_getmsg(%p) %s\n",
29048348SEric.Yu@Sun.COM 	    (void *)so, pr_state(so->so_state, so->so_mode)));
29058348SEric.Yu@Sun.COM 
29068348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
29078348SEric.Yu@Sun.COM 		/* The imaginary "sockmod" has been popped - act as a stream */
29088348SEric.Yu@Sun.COM 		return (strgetmsg(vp, mctl, mdata, prip, flagsp, fmode, rvp));
29098348SEric.Yu@Sun.COM 	}
29108348SEric.Yu@Sun.COM 	eprintsoline(so, ENOSTR);
29118348SEric.Yu@Sun.COM 	return (ENOSTR);
29128348SEric.Yu@Sun.COM }
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate /*
29158348SEric.Yu@Sun.COM  * Wrapper for putmsg. If the socket has been converted to a stream
29168348SEric.Yu@Sun.COM  * pass the request to the stream head.
29178348SEric.Yu@Sun.COM  *
29188348SEric.Yu@Sun.COM  * Note that a while a regular socket (SOV_SOCKSTREAM) does support the
29198348SEric.Yu@Sun.COM  * streams ioctl set it does not support putmsg and getmsg.
29208348SEric.Yu@Sun.COM  * Allowing putmsg would prevent sockfs from tracking the state of
29218348SEric.Yu@Sun.COM  * the socket/transport and would also invalidate the locking in sockfs.
29220Sstevel@tonic-gate  */
29238348SEric.Yu@Sun.COM int
29248348SEric.Yu@Sun.COM sock_putmsg(
29258348SEric.Yu@Sun.COM 	struct vnode *vp,
29268348SEric.Yu@Sun.COM 	struct strbuf *mctl,
29278348SEric.Yu@Sun.COM 	struct strbuf *mdata,
29288348SEric.Yu@Sun.COM 	uchar_t pri,
29298348SEric.Yu@Sun.COM 	int flag,
29308348SEric.Yu@Sun.COM 	int fmode
29318348SEric.Yu@Sun.COM )
29328348SEric.Yu@Sun.COM {
29338348SEric.Yu@Sun.COM 	struct sonode *so;
29348348SEric.Yu@Sun.COM 
29358348SEric.Yu@Sun.COM 	ASSERT(vp->v_type == VSOCK);
29368348SEric.Yu@Sun.COM 	/*
29378348SEric.Yu@Sun.COM 	 * Use the stream head to find the real socket vnode.
29388348SEric.Yu@Sun.COM 	 * This is needed when namefs sits above sockfs.
29398348SEric.Yu@Sun.COM 	 */
29408348SEric.Yu@Sun.COM 	if (!vp->v_stream) {
29418348SEric.Yu@Sun.COM 		return (ENOSTR);
29428348SEric.Yu@Sun.COM 	}
29438348SEric.Yu@Sun.COM 	ASSERT(vp->v_stream->sd_vnode);
29448348SEric.Yu@Sun.COM 	vp = vp->v_stream->sd_vnode;
29458348SEric.Yu@Sun.COM 	ASSERT(vn_matchops(vp, socket_vnodeops));
29468348SEric.Yu@Sun.COM 	so = VTOSO(vp);
29478348SEric.Yu@Sun.COM 
29488348SEric.Yu@Sun.COM 	dprintso(so, 1, ("sock_putmsg(%p) %s\n",
29498348SEric.Yu@Sun.COM 	    (void *)so, pr_state(so->so_state, so->so_mode)));
29500Sstevel@tonic-gate 
29518348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
29528348SEric.Yu@Sun.COM 		/* The imaginary "sockmod" has been popped - act as a stream */
29538348SEric.Yu@Sun.COM 		return (strputmsg(vp, mctl, mdata, pri, flag, fmode));
29548348SEric.Yu@Sun.COM 	}
29558348SEric.Yu@Sun.COM 	eprintsoline(so, ENOSTR);
29568348SEric.Yu@Sun.COM 	return (ENOSTR);
29578348SEric.Yu@Sun.COM }
29588348SEric.Yu@Sun.COM 
29598348SEric.Yu@Sun.COM /*
29608348SEric.Yu@Sun.COM  * Special function called only from f_getfl().
29618348SEric.Yu@Sun.COM  * Returns FASYNC if the SS_ASYNC flag is set on a socket, else 0.
29628348SEric.Yu@Sun.COM  * No locks are acquired here, so it is safe to use while uf_lock is held.
29638348SEric.Yu@Sun.COM  * This exists solely for BSD fcntl() FASYNC compatibility.
29648348SEric.Yu@Sun.COM  */
29658348SEric.Yu@Sun.COM int
29668348SEric.Yu@Sun.COM sock_getfasync(vnode_t *vp)
29670Sstevel@tonic-gate {
29688348SEric.Yu@Sun.COM 	struct sonode *so;
29698348SEric.Yu@Sun.COM 
29708348SEric.Yu@Sun.COM 	ASSERT(vp->v_type == VSOCK);
29718348SEric.Yu@Sun.COM 	/*
29728348SEric.Yu@Sun.COM 	 * For stream model, v_stream is used; For non-stream, v_stream always
29738348SEric.Yu@Sun.COM 	 * equals NULL
29748348SEric.Yu@Sun.COM 	 */
29758348SEric.Yu@Sun.COM 	if (vp->v_stream != NULL)
29768348SEric.Yu@Sun.COM 		so = VTOSO(vp->v_stream->sd_vnode);
29770Sstevel@tonic-gate 	else
29788348SEric.Yu@Sun.COM 		so = VTOSO(vp);
29798348SEric.Yu@Sun.COM 
29808348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM || !(so->so_state & SS_ASYNC))
29818348SEric.Yu@Sun.COM 		return (0);
29828348SEric.Yu@Sun.COM 
29838348SEric.Yu@Sun.COM 	return (FASYNC);
29840Sstevel@tonic-gate }
2985