xref: /onnv-gate/usr/src/uts/common/fs/sockfs/socktpi.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/t_lock.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/buf.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
358348SEric.Yu@Sun.COM #include <sys/kmem_impl.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/open.h>
440Sstevel@tonic-gate #include <sys/user.h>
450Sstevel@tonic-gate #include <sys/termios.h>
460Sstevel@tonic-gate #include <sys/stream.h>
470Sstevel@tonic-gate #include <sys/strsubr.h>
480Sstevel@tonic-gate #include <sys/strsun.h>
498348SEric.Yu@Sun.COM #include <sys/suntpi.h>
500Sstevel@tonic-gate #include <sys/ddi.h>
510Sstevel@tonic-gate #include <sys/esunddi.h>
520Sstevel@tonic-gate #include <sys/flock.h>
530Sstevel@tonic-gate #include <sys/modctl.h>
540Sstevel@tonic-gate #include <sys/vtrace.h>
550Sstevel@tonic-gate #include <sys/cmn_err.h>
560Sstevel@tonic-gate #include <sys/pathname.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/socket.h>
590Sstevel@tonic-gate #include <sys/socketvar.h>
60741Smasputra #include <sys/sockio.h>
610Sstevel@tonic-gate #include <netinet/in.h>
620Sstevel@tonic-gate #include <sys/un.h>
630Sstevel@tonic-gate #include <sys/strsun.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include <sys/tiuser.h>
660Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
670Sstevel@tonic-gate #include <sys/tihdr.h>
680Sstevel@tonic-gate #include <sys/timod.h>		/* TI_GETMYNAME, TI_GETPEERNAME */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <c2/audit.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <inet/common.h>
730Sstevel@tonic-gate #include <inet/ip.h>
740Sstevel@tonic-gate #include <inet/ip6.h>
750Sstevel@tonic-gate #include <inet/tcp.h>
76741Smasputra #include <inet/udp_impl.h>
770Sstevel@tonic-gate 
781974Sbrutus #include <sys/zone.h>
791974Sbrutus 
800Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
811974Sbrutus #include <fs/sockfs/nl7curi.h>
820Sstevel@tonic-gate 
83898Skais #include <inet/kssl/ksslapi.h>
84898Skais 
858348SEric.Yu@Sun.COM #include <fs/sockfs/sockcommon.h>
868348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h>
878348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi_impl.h>
888348SEric.Yu@Sun.COM 
890Sstevel@tonic-gate /*
900Sstevel@tonic-gate  * Possible failures when memory can't be allocated. The documented behavior:
910Sstevel@tonic-gate  *
920Sstevel@tonic-gate  * 		5.5:			4.X:		XNET:
930Sstevel@tonic-gate  * accept:	ENOMEM/ENOSR/EINTR	- (EINTR)	ENOMEM/ENOBUFS/ENOSR/
940Sstevel@tonic-gate  *							EINTR
950Sstevel@tonic-gate  *	(4.X does not document EINTR but returns it)
960Sstevel@tonic-gate  * bind:	ENOSR			-		ENOBUFS/ENOSR
970Sstevel@tonic-gate  * connect: 	EINTR			EINTR		ENOBUFS/ENOSR/EINTR
980Sstevel@tonic-gate  * getpeername:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
990Sstevel@tonic-gate  * getsockname:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
1000Sstevel@tonic-gate  *	(4.X getpeername and getsockname do not fail in practice)
1010Sstevel@tonic-gate  * getsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
1020Sstevel@tonic-gate  * listen:	-			-		ENOBUFS
1030Sstevel@tonic-gate  * recv:	ENOMEM/ENOSR/EINTR	EINTR		ENOBUFS/ENOMEM/ENOSR/
1040Sstevel@tonic-gate  *							EINTR
1050Sstevel@tonic-gate  * send:	ENOMEM/ENOSR/EINTR	ENOBUFS/EINTR	ENOBUFS/ENOMEM/ENOSR/
1060Sstevel@tonic-gate  *							EINTR
1070Sstevel@tonic-gate  * setsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
1080Sstevel@tonic-gate  * shutdown:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
1090Sstevel@tonic-gate  * socket:	ENOMEM/ENOSR		ENOBUFS		ENOBUFS/ENOMEM/ENOSR
1100Sstevel@tonic-gate  * socketpair:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
1110Sstevel@tonic-gate  *
1120Sstevel@tonic-gate  * Resolution. When allocation fails:
1130Sstevel@tonic-gate  *	recv: return EINTR
1140Sstevel@tonic-gate  *	send: return EINTR
1150Sstevel@tonic-gate  *	connect, accept: EINTR
1160Sstevel@tonic-gate  *	bind, listen, shutdown (unbind, unix_close, disconnect): sleep
1170Sstevel@tonic-gate  *	socket, socketpair: ENOBUFS
1180Sstevel@tonic-gate  *	getpeername, getsockname: sleep
1190Sstevel@tonic-gate  *	getsockopt, setsockopt: sleep
1200Sstevel@tonic-gate  */
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate #ifdef SOCK_TEST
1230Sstevel@tonic-gate /*
1240Sstevel@tonic-gate  * Variables that make sockfs do something other than the standard TPI
1250Sstevel@tonic-gate  * for the AF_INET transports.
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  * solisten_tpi_tcp:
1280Sstevel@tonic-gate  *	TCP can handle a O_T_BIND_REQ with an increased backlog even though
1290Sstevel@tonic-gate  *	the transport is already bound. This is needed to avoid loosing the
1300Sstevel@tonic-gate  *	port number should listen() do a T_UNBIND_REQ followed by a
1310Sstevel@tonic-gate  *	O_T_BIND_REQ.
1320Sstevel@tonic-gate  *
1330Sstevel@tonic-gate  * soconnect_tpi_udp:
1340Sstevel@tonic-gate  *	UDP and ICMP can handle a T_CONN_REQ.
1350Sstevel@tonic-gate  *	This is needed to make the sequence of connect(), getsockname()
1360Sstevel@tonic-gate  *	return the local IP address used to send packets to the connected to
1370Sstevel@tonic-gate  *	destination.
1380Sstevel@tonic-gate  *
1390Sstevel@tonic-gate  * soconnect_tpi_tcp:
1400Sstevel@tonic-gate  *	TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ.
1410Sstevel@tonic-gate  *	Set this to non-zero to send TPI conformant messages to TCP in this
1420Sstevel@tonic-gate  *	respect. This is a performance optimization.
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  * soaccept_tpi_tcp:
1450Sstevel@tonic-gate  *	TCP can handle a T_CONN_REQ without the acceptor being bound.
1460Sstevel@tonic-gate  *	This is a performance optimization that has been picked up in XTI.
1470Sstevel@tonic-gate  *
1480Sstevel@tonic-gate  * soaccept_tpi_multioptions:
1490Sstevel@tonic-gate  *	When inheriting SOL_SOCKET options from the listener to the accepting
1500Sstevel@tonic-gate  *	socket send them as a single message for AF_INET{,6}.
1510Sstevel@tonic-gate  */
1520Sstevel@tonic-gate int solisten_tpi_tcp = 0;
1530Sstevel@tonic-gate int soconnect_tpi_udp = 0;
1540Sstevel@tonic-gate int soconnect_tpi_tcp = 0;
1550Sstevel@tonic-gate int soaccept_tpi_tcp = 0;
1560Sstevel@tonic-gate int soaccept_tpi_multioptions = 1;
1570Sstevel@tonic-gate #else /* SOCK_TEST */
1580Sstevel@tonic-gate #define	soconnect_tpi_tcp	0
1590Sstevel@tonic-gate #define	soconnect_tpi_udp	0
1600Sstevel@tonic-gate #define	solisten_tpi_tcp	0
1610Sstevel@tonic-gate #define	soaccept_tpi_tcp	0
1620Sstevel@tonic-gate #define	soaccept_tpi_multioptions	1
1630Sstevel@tonic-gate #endif /* SOCK_TEST */
1640Sstevel@tonic-gate 
1650Sstevel@tonic-gate #ifdef SOCK_TEST
1660Sstevel@tonic-gate extern int do_useracc;
1670Sstevel@tonic-gate extern clock_t sock_test_timelimit;
1680Sstevel@tonic-gate #endif /* SOCK_TEST */
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Some X/Open added checks might have to be backed out to keep SunOS 4.X
1720Sstevel@tonic-gate  * applications working. Turn on this flag to disable these checks.
1730Sstevel@tonic-gate  */
1740Sstevel@tonic-gate int xnet_skip_checks = 0;
1750Sstevel@tonic-gate int xnet_check_print = 0;
1760Sstevel@tonic-gate int xnet_truncate_print = 0;
1770Sstevel@tonic-gate 
1788348SEric.Yu@Sun.COM static void sotpi_destroy(struct sonode *);
1798348SEric.Yu@Sun.COM static struct sonode *sotpi_create(struct sockparams *, int, int, int, int,
1808348SEric.Yu@Sun.COM     int, int *, cred_t *cr);
1818348SEric.Yu@Sun.COM 
1828348SEric.Yu@Sun.COM static boolean_t	sotpi_info_create(struct sonode *, int);
1838348SEric.Yu@Sun.COM static void		sotpi_info_init(struct sonode *);
1848348SEric.Yu@Sun.COM static void 		sotpi_info_fini(struct sonode *);
1858348SEric.Yu@Sun.COM static void 		sotpi_info_destroy(struct sonode *);
1868348SEric.Yu@Sun.COM 
1878348SEric.Yu@Sun.COM /*
1888348SEric.Yu@Sun.COM  * Do direct function call to the transport layer below; this would
1898348SEric.Yu@Sun.COM  * also allow the transport to utilize read-side synchronous stream
1908348SEric.Yu@Sun.COM  * interface if necessary.  This is a /etc/system tunable that must
1918348SEric.Yu@Sun.COM  * not be modified on a running system.  By default this is enabled
1928348SEric.Yu@Sun.COM  * for performance reasons and may be disabled for debugging purposes.
1938348SEric.Yu@Sun.COM  */
1948348SEric.Yu@Sun.COM boolean_t socktpi_direct = B_TRUE;
1958348SEric.Yu@Sun.COM 
1968348SEric.Yu@Sun.COM static struct kmem_cache *socktpi_cache, *socktpi_unix_cache;
1978348SEric.Yu@Sun.COM 
1980Sstevel@tonic-gate extern	void sigintr(k_sigset_t *, int);
1990Sstevel@tonic-gate extern	void sigunintr(k_sigset_t *);
2000Sstevel@tonic-gate 
201898Skais /* Sockets acting as an in-kernel SSL proxy */
202898Skais extern mblk_t	*strsock_kssl_input(vnode_t *, mblk_t *, strwakeup_t *,
203898Skais 		    strsigset_t *, strsigset_t *, strpollset_t *);
204898Skais extern mblk_t	*strsock_kssl_output(vnode_t *, mblk_t *, strwakeup_t *,
205898Skais 		    strsigset_t *, strsigset_t *, strpollset_t *);
206898Skais 
2070Sstevel@tonic-gate static int	sotpi_unbind(struct sonode *, int);
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /* TPI sockfs sonode operations */
2108348SEric.Yu@Sun.COM int 		sotpi_init(struct sonode *, struct sonode *, struct cred *,
2118348SEric.Yu@Sun.COM 		    int);
2128348SEric.Yu@Sun.COM static int	sotpi_accept(struct sonode *, int, struct cred *,
2138348SEric.Yu@Sun.COM 		    struct sonode **);
2140Sstevel@tonic-gate static int	sotpi_bind(struct sonode *, struct sockaddr *, socklen_t,
2158348SEric.Yu@Sun.COM 		    int, struct cred *);
2168348SEric.Yu@Sun.COM static int	sotpi_listen(struct sonode *, int, struct cred *);
2170Sstevel@tonic-gate static int	sotpi_connect(struct sonode *, const struct sockaddr *,
2188348SEric.Yu@Sun.COM 		    socklen_t, int, int, struct cred *);
2198348SEric.Yu@Sun.COM extern int	sotpi_recvmsg(struct sonode *, struct nmsghdr *,
2208348SEric.Yu@Sun.COM 		    struct uio *, struct cred *);
2210Sstevel@tonic-gate static int	sotpi_sendmsg(struct sonode *, struct nmsghdr *,
2228348SEric.Yu@Sun.COM 		    struct uio *, struct cred *);
2238348SEric.Yu@Sun.COM static int	sotpi_sendmblk(struct sonode *, struct nmsghdr *, int,
2248348SEric.Yu@Sun.COM 		    struct cred *, mblk_t **);
225741Smasputra static int	sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t,
226741Smasputra 		    struct uio *, void *, t_uscalar_t, int);
227741Smasputra static int	sodgram_direct(struct sonode *, struct sockaddr *,
228741Smasputra 		    socklen_t, struct uio *, int);
2298348SEric.Yu@Sun.COM extern int	sotpi_getpeername(struct sonode *, struct sockaddr *,
2308348SEric.Yu@Sun.COM 		    socklen_t *, boolean_t, struct cred *);
2318348SEric.Yu@Sun.COM static int	sotpi_getsockname(struct sonode *, struct sockaddr *,
2328348SEric.Yu@Sun.COM 		    socklen_t *, struct cred *);
2338348SEric.Yu@Sun.COM static int	sotpi_shutdown(struct sonode *, int, struct cred *);
2348348SEric.Yu@Sun.COM extern int	sotpi_getsockopt(struct sonode *, int, int, void *,
2358348SEric.Yu@Sun.COM 		    socklen_t *, int, struct cred *);
2368348SEric.Yu@Sun.COM extern int	sotpi_setsockopt(struct sonode *, int, int, const void *,
2378348SEric.Yu@Sun.COM 		    socklen_t, struct cred *);
2388348SEric.Yu@Sun.COM static int 	sotpi_ioctl(struct sonode *, int, intptr_t, int, struct cred *,
2398348SEric.Yu@Sun.COM 		    int32_t *);
2408348SEric.Yu@Sun.COM static int 	socktpi_plumbioctl(struct vnode *, int, intptr_t, int,
2418348SEric.Yu@Sun.COM 		    struct cred *, int32_t *);
2428348SEric.Yu@Sun.COM static int 	sotpi_poll(struct sonode *, short, int, short *,
2438348SEric.Yu@Sun.COM 		    struct pollhead **);
2448348SEric.Yu@Sun.COM static int 	sotpi_close(struct sonode *, int, struct cred *);
2458348SEric.Yu@Sun.COM 
2468348SEric.Yu@Sun.COM static int	i_sotpi_info_constructor(sotpi_info_t *);
2478348SEric.Yu@Sun.COM static void 	i_sotpi_info_destructor(sotpi_info_t *);
2480Sstevel@tonic-gate 
2490Sstevel@tonic-gate sonodeops_t sotpi_sonodeops = {
2508348SEric.Yu@Sun.COM 	sotpi_init,		/* sop_init		*/
2510Sstevel@tonic-gate 	sotpi_accept,		/* sop_accept		*/
2520Sstevel@tonic-gate 	sotpi_bind,		/* sop_bind		*/
2530Sstevel@tonic-gate 	sotpi_listen,		/* sop_listen		*/
2540Sstevel@tonic-gate 	sotpi_connect,		/* sop_connect		*/
2550Sstevel@tonic-gate 	sotpi_recvmsg,		/* sop_recvmsg		*/
2560Sstevel@tonic-gate 	sotpi_sendmsg,		/* sop_sendmsg		*/
2578348SEric.Yu@Sun.COM 	sotpi_sendmblk,		/* sop_sendmblk		*/
2580Sstevel@tonic-gate 	sotpi_getpeername,	/* sop_getpeername	*/
2590Sstevel@tonic-gate 	sotpi_getsockname,	/* sop_getsockname	*/
2600Sstevel@tonic-gate 	sotpi_shutdown,		/* sop_shutdown		*/
2610Sstevel@tonic-gate 	sotpi_getsockopt,	/* sop_getsockopt	*/
2628348SEric.Yu@Sun.COM 	sotpi_setsockopt,	/* sop_setsockopt	*/
2638348SEric.Yu@Sun.COM 	sotpi_ioctl,		/* sop_ioctl		*/
2648348SEric.Yu@Sun.COM 	sotpi_poll,		/* sop_poll		*/
2658348SEric.Yu@Sun.COM 	sotpi_close,		/* sop_close		*/
2660Sstevel@tonic-gate };
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate /*
2698348SEric.Yu@Sun.COM  * Return a TPI socket vnode.
2708348SEric.Yu@Sun.COM  *
2718348SEric.Yu@Sun.COM  * Note that sockets assume that the driver will clone (either itself
2728348SEric.Yu@Sun.COM  * or by using the clone driver) i.e. a socket() call will always
2738348SEric.Yu@Sun.COM  * result in a new vnode being created.
2748348SEric.Yu@Sun.COM  */
2758348SEric.Yu@Sun.COM 
2768348SEric.Yu@Sun.COM /*
2770Sstevel@tonic-gate  * Common create code for socket and accept. If tso is set the values
2780Sstevel@tonic-gate  * from that node is used instead of issuing a T_INFO_REQ.
2790Sstevel@tonic-gate  */
2808348SEric.Yu@Sun.COM 
2818348SEric.Yu@Sun.COM /* ARGSUSED */
2828348SEric.Yu@Sun.COM static struct sonode *
2838348SEric.Yu@Sun.COM sotpi_create(struct sockparams *sp, int family, int type, int protocol,
2848348SEric.Yu@Sun.COM     int version, int sflags, int *errorp, cred_t *cr)
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate 	struct sonode	*so;
2878348SEric.Yu@Sun.COM 	kmem_cache_t 	*cp;
2888348SEric.Yu@Sun.COM 	int		sfamily = family;
2898348SEric.Yu@Sun.COM 
2908348SEric.Yu@Sun.COM 	ASSERT(sp->sp_sdev_info.sd_vnode != NULL);
2918348SEric.Yu@Sun.COM 
2928348SEric.Yu@Sun.COM 	if (family == AF_NCA) {
2938348SEric.Yu@Sun.COM 		/*
2948348SEric.Yu@Sun.COM 		 * The request is for an NCA socket so for NL7C use the
2958348SEric.Yu@Sun.COM 		 * INET domain instead and mark NL7C_AF_NCA below.
2968348SEric.Yu@Sun.COM 		 */
2978348SEric.Yu@Sun.COM 		family = AF_INET;
2988348SEric.Yu@Sun.COM 		/*
2998348SEric.Yu@Sun.COM 		 * NL7C is not supported in the non-global zone,
3008348SEric.Yu@Sun.COM 		 * we enforce this restriction here.
3018348SEric.Yu@Sun.COM 		 */
3028348SEric.Yu@Sun.COM 		if (getzoneid() != GLOBAL_ZONEID) {
3038348SEric.Yu@Sun.COM 			*errorp = ENOTSUP;
3048348SEric.Yu@Sun.COM 			return (NULL);
3058348SEric.Yu@Sun.COM 		}
3068348SEric.Yu@Sun.COM 	}
3078348SEric.Yu@Sun.COM 
3088348SEric.Yu@Sun.COM 	/*
3098348SEric.Yu@Sun.COM 	 * to be compatible with old tpi socket implementation ignore
3108348SEric.Yu@Sun.COM 	 * sleep flag (sflags) passed in
3118348SEric.Yu@Sun.COM 	 */
3128348SEric.Yu@Sun.COM 	cp = (family == AF_UNIX) ? socktpi_unix_cache : socktpi_cache;
3138348SEric.Yu@Sun.COM 	so = kmem_cache_alloc(cp, KM_SLEEP);
3148348SEric.Yu@Sun.COM 	if (so == NULL) {
3158348SEric.Yu@Sun.COM 		*errorp = ENOMEM;
3168348SEric.Yu@Sun.COM 		return (NULL);
3178348SEric.Yu@Sun.COM 	}
3188348SEric.Yu@Sun.COM 
3198348SEric.Yu@Sun.COM 	sonode_init(so, sp, family, type, protocol, &sotpi_sonodeops);
3208348SEric.Yu@Sun.COM 	sotpi_info_init(so);
3218348SEric.Yu@Sun.COM 
3228348SEric.Yu@Sun.COM 	if (sfamily == AF_NCA) {
3238348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_nl7c_flags = NL7C_AF_NCA;
3248348SEric.Yu@Sun.COM 	}
3258348SEric.Yu@Sun.COM 
3268348SEric.Yu@Sun.COM 	if (version == SOV_DEFAULT)
3278348SEric.Yu@Sun.COM 		version = so_default_version;
3288348SEric.Yu@Sun.COM 
3298348SEric.Yu@Sun.COM 	so->so_version = (short)version;
3308348SEric.Yu@Sun.COM 	*errorp = 0;
3318348SEric.Yu@Sun.COM 
3328348SEric.Yu@Sun.COM 	return (so);
3338348SEric.Yu@Sun.COM }
3348348SEric.Yu@Sun.COM 
3358348SEric.Yu@Sun.COM static void
3368348SEric.Yu@Sun.COM sotpi_destroy(struct sonode *so)
3378348SEric.Yu@Sun.COM {
3388348SEric.Yu@Sun.COM 	kmem_cache_t *cp;
3398348SEric.Yu@Sun.COM 	struct sockparams *origsp;
3408348SEric.Yu@Sun.COM 
3418348SEric.Yu@Sun.COM 	/*
3428348SEric.Yu@Sun.COM 	 * If there is a new dealloc function (ie. smod_destroy_func),
3438348SEric.Yu@Sun.COM 	 * then it should check the correctness of the ops.
3448348SEric.Yu@Sun.COM 	 */
3458348SEric.Yu@Sun.COM 
3468348SEric.Yu@Sun.COM 	ASSERT(so->so_ops == &sotpi_sonodeops);
3478348SEric.Yu@Sun.COM 
3488348SEric.Yu@Sun.COM 	origsp = SOTOTPI(so)->sti_orig_sp;
3498348SEric.Yu@Sun.COM 
3508348SEric.Yu@Sun.COM 	sotpi_info_fini(so);
3518348SEric.Yu@Sun.COM 
3528348SEric.Yu@Sun.COM 	if (so->so_state & SS_FALLBACK_COMP) {
3538348SEric.Yu@Sun.COM 		/*
3548348SEric.Yu@Sun.COM 		 * A fallback happend, which means that a sotpi_info_t struct
3558348SEric.Yu@Sun.COM 		 * was allocated (as opposed to being allocated from the TPI
3568348SEric.Yu@Sun.COM 		 * sonode cache. Therefore we explicitly free the struct
3578348SEric.Yu@Sun.COM 		 * here.
3588348SEric.Yu@Sun.COM 		 */
3598348SEric.Yu@Sun.COM 		sotpi_info_destroy(so);
3608348SEric.Yu@Sun.COM 		ASSERT(origsp != NULL);
3618348SEric.Yu@Sun.COM 
3628348SEric.Yu@Sun.COM 		origsp->sp_smod_info->smod_sock_destroy_func(so);
3638348SEric.Yu@Sun.COM 		SOCKPARAMS_DEC_REF(origsp);
3648348SEric.Yu@Sun.COM 	} else {
3658348SEric.Yu@Sun.COM 		sonode_fini(so);
3668348SEric.Yu@Sun.COM 		cp = (so->so_family == AF_UNIX) ? socktpi_unix_cache :
3678348SEric.Yu@Sun.COM 		    socktpi_cache;
3688348SEric.Yu@Sun.COM 		kmem_cache_free(cp, so);
3698348SEric.Yu@Sun.COM 	}
3708348SEric.Yu@Sun.COM }
3718348SEric.Yu@Sun.COM 
3728348SEric.Yu@Sun.COM /* ARGSUSED1 */
3738348SEric.Yu@Sun.COM int
3748348SEric.Yu@Sun.COM sotpi_init(struct sonode *so, struct sonode *tso, struct cred *cr, int flags)
3758348SEric.Yu@Sun.COM {
3768348SEric.Yu@Sun.COM 	major_t maj;
3778348SEric.Yu@Sun.COM 	dev_t newdev;
3788348SEric.Yu@Sun.COM 	struct vnode *vp;
3798348SEric.Yu@Sun.COM 	int error = 0;
3808348SEric.Yu@Sun.COM 	struct stdata *stp;
3818348SEric.Yu@Sun.COM 
3828348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
3838348SEric.Yu@Sun.COM 
3848348SEric.Yu@Sun.COM 	dprint(1, ("sotpi_init()\n"));
3858348SEric.Yu@Sun.COM 
3868348SEric.Yu@Sun.COM 	/*
3878348SEric.Yu@Sun.COM 	 * over write the sleep flag passed in but that is ok
3888348SEric.Yu@Sun.COM 	 * as tpi socket does not honor sleep flag.
3898348SEric.Yu@Sun.COM 	 */
3908348SEric.Yu@Sun.COM 	flags |= FREAD|FWRITE;
3918348SEric.Yu@Sun.COM 
3928348SEric.Yu@Sun.COM 	/*
3938348SEric.Yu@Sun.COM 	 * Record in so_flag that it is a clone.
3948348SEric.Yu@Sun.COM 	 */
3958348SEric.Yu@Sun.COM 	if (getmajor(sti->sti_dev) == clone_major)
3968348SEric.Yu@Sun.COM 		so->so_flag |= SOCLONE;
3978348SEric.Yu@Sun.COM 
3988348SEric.Yu@Sun.COM 	if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM) &&
3998348SEric.Yu@Sun.COM 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
4008348SEric.Yu@Sun.COM 	    (so->so_protocol == IPPROTO_TCP || so->so_protocol == IPPROTO_UDP ||
4018348SEric.Yu@Sun.COM 	    so->so_protocol == IPPROTO_IP)) {
402741Smasputra 		/* Tell tcp or udp that it's talking to sockets */
403741Smasputra 		flags |= SO_SOCKSTR;
404741Smasputra 
405741Smasputra 		/*
406741Smasputra 		 * Here we indicate to socktpi_open() our attempt to
407741Smasputra 		 * make direct calls between sockfs and transport.
408741Smasputra 		 * The final decision is left to socktpi_open().
409741Smasputra 		 */
4108348SEric.Yu@Sun.COM 		sti->sti_direct = 1;
411741Smasputra 
412741Smasputra 		ASSERT(so->so_type != SOCK_DGRAM || tso == NULL);
413741Smasputra 		if (so->so_type == SOCK_STREAM && tso != NULL) {
4148348SEric.Yu@Sun.COM 			if (SOTOTPI(tso)->sti_direct) {
415741Smasputra 				/*
4168348SEric.Yu@Sun.COM 				 * Inherit sti_direct from listener and pass
417741Smasputra 				 * SO_ACCEPTOR open flag to tcp, indicating
418741Smasputra 				 * that this is an accept fast-path instance.
419741Smasputra 				 */
420741Smasputra 				flags |= SO_ACCEPTOR;
421741Smasputra 			} else {
422741Smasputra 				/*
4238348SEric.Yu@Sun.COM 				 * sti_direct is not set on listener, meaning
424741Smasputra 				 * that the listener has been converted from
425741Smasputra 				 * a socket to a stream.  Ensure that the
426741Smasputra 				 * acceptor inherits these settings.
427741Smasputra 				 */
4288348SEric.Yu@Sun.COM 				sti->sti_direct = 0;
429741Smasputra 				flags &= ~SO_SOCKSTR;
430741Smasputra 			}
4310Sstevel@tonic-gate 		}
4320Sstevel@tonic-gate 	}
4330Sstevel@tonic-gate 
4340Sstevel@tonic-gate 	/*
4350Sstevel@tonic-gate 	 * Tell local transport that it is talking to sockets.
4360Sstevel@tonic-gate 	 */
4370Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
4380Sstevel@tonic-gate 		flags |= SO_SOCKSTR;
4390Sstevel@tonic-gate 	}
4400Sstevel@tonic-gate 
4418348SEric.Yu@Sun.COM 	vp = SOTOV(so);
4428348SEric.Yu@Sun.COM 	newdev = vp->v_rdev;
4438348SEric.Yu@Sun.COM 	maj = getmajor(newdev);
4448348SEric.Yu@Sun.COM 	ASSERT(STREAMSTAB(maj));
4458348SEric.Yu@Sun.COM 
4468348SEric.Yu@Sun.COM 	error = stropen(vp, &newdev, flags, cr);
4478348SEric.Yu@Sun.COM 
4488348SEric.Yu@Sun.COM 	stp = vp->v_stream;
4498348SEric.Yu@Sun.COM 	if (error == 0) {
4508348SEric.Yu@Sun.COM 		if (so->so_flag & SOCLONE)
4518348SEric.Yu@Sun.COM 			ASSERT(newdev != vp->v_rdev);
4528348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
4538348SEric.Yu@Sun.COM 		sti->sti_dev = newdev;
4548348SEric.Yu@Sun.COM 		vp->v_rdev = newdev;
4558348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
4568348SEric.Yu@Sun.COM 
4578348SEric.Yu@Sun.COM 		if (stp->sd_flag & STRISTTY) {
4588348SEric.Yu@Sun.COM 			/*
4598348SEric.Yu@Sun.COM 			 * this is a post SVR4 tty driver - a socket can not
4608348SEric.Yu@Sun.COM 			 * be a controlling terminal. Fail the open.
4618348SEric.Yu@Sun.COM 			 */
4628348SEric.Yu@Sun.COM 			(void) sotpi_close(so, flags, cr);
4638348SEric.Yu@Sun.COM 			return (ENOTTY);	/* XXX */
4648348SEric.Yu@Sun.COM 		}
4658348SEric.Yu@Sun.COM 
4668348SEric.Yu@Sun.COM 		ASSERT(stp->sd_wrq != NULL);
4678348SEric.Yu@Sun.COM 		sti->sti_provinfo = tpi_findprov(stp->sd_wrq);
4688348SEric.Yu@Sun.COM 
4698348SEric.Yu@Sun.COM 		/*
4708348SEric.Yu@Sun.COM 		 * If caller is interested in doing direct function call
4718348SEric.Yu@Sun.COM 		 * interface to/from transport module, probe the module
4728348SEric.Yu@Sun.COM 		 * directly beneath the streamhead to see if it qualifies.
4738348SEric.Yu@Sun.COM 		 *
4748348SEric.Yu@Sun.COM 		 * We turn off the direct interface when qualifications fail.
4758348SEric.Yu@Sun.COM 		 * In the acceptor case, we simply turn off the sti_direct
4768348SEric.Yu@Sun.COM 		 * flag on the socket. We do the fallback after the accept
4778348SEric.Yu@Sun.COM 		 * has completed, before the new socket is returned to the
4788348SEric.Yu@Sun.COM 		 * application.
4798348SEric.Yu@Sun.COM 		 */
4808348SEric.Yu@Sun.COM 		if (sti->sti_direct) {
4818348SEric.Yu@Sun.COM 			queue_t *tq = stp->sd_wrq->q_next;
4828348SEric.Yu@Sun.COM 
4838348SEric.Yu@Sun.COM 			/*
4848348SEric.Yu@Sun.COM 			 * sti_direct is currently supported and tested
4858348SEric.Yu@Sun.COM 			 * only for tcp/udp; this is the main reason to
4868348SEric.Yu@Sun.COM 			 * have the following assertions.
4878348SEric.Yu@Sun.COM 			 */
4888348SEric.Yu@Sun.COM 			ASSERT(so->so_family == AF_INET ||
4898348SEric.Yu@Sun.COM 			    so->so_family == AF_INET6);
4908348SEric.Yu@Sun.COM 			ASSERT(so->so_protocol == IPPROTO_UDP ||
4918348SEric.Yu@Sun.COM 			    so->so_protocol == IPPROTO_TCP ||
4928348SEric.Yu@Sun.COM 			    so->so_protocol == IPPROTO_IP);
4938348SEric.Yu@Sun.COM 			ASSERT(so->so_type == SOCK_DGRAM ||
4948348SEric.Yu@Sun.COM 			    so->so_type == SOCK_STREAM);
4958348SEric.Yu@Sun.COM 
4968348SEric.Yu@Sun.COM 			/*
4978348SEric.Yu@Sun.COM 			 * Abort direct call interface if the module directly
4988348SEric.Yu@Sun.COM 			 * underneath the stream head is not defined with the
4998348SEric.Yu@Sun.COM 			 * _D_DIRECT flag.  This could happen in the tcp or
5008348SEric.Yu@Sun.COM 			 * udp case, when some other module is autopushed
5018348SEric.Yu@Sun.COM 			 * above it, or for some reasons the expected module
5028348SEric.Yu@Sun.COM 			 * isn't purely D_MP (which is the main requirement).
5038348SEric.Yu@Sun.COM 			 */
5048348SEric.Yu@Sun.COM 			if (!socktpi_direct || !(tq->q_flag & _QDIRECT) ||
5058348SEric.Yu@Sun.COM 			    !(_OTHERQ(tq)->q_flag & _QDIRECT)) {
5068348SEric.Yu@Sun.COM 				int rval;
5078348SEric.Yu@Sun.COM 
5088348SEric.Yu@Sun.COM 				/* Continue on without direct calls */
5098348SEric.Yu@Sun.COM 				sti->sti_direct = 0;
5108348SEric.Yu@Sun.COM 
5118348SEric.Yu@Sun.COM 				/*
5128348SEric.Yu@Sun.COM 				 * Cannot issue ioctl on fallback socket since
5138348SEric.Yu@Sun.COM 				 * there is no conn associated with the queue.
5148348SEric.Yu@Sun.COM 				 * The fallback downcall will notify the proto
5158348SEric.Yu@Sun.COM 				 * of the change.
5168348SEric.Yu@Sun.COM 				 */
5178348SEric.Yu@Sun.COM 				if (!(flags & SO_ACCEPTOR) &&
5188348SEric.Yu@Sun.COM 				    !(flags & SO_FALLBACK)) {
5198348SEric.Yu@Sun.COM 					if ((error = strioctl(vp,
5208348SEric.Yu@Sun.COM 					    _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
5218348SEric.Yu@Sun.COM 					    cr, &rval)) != 0) {
5228348SEric.Yu@Sun.COM 						(void) sotpi_close(so, flags,
5238348SEric.Yu@Sun.COM 						    cr);
5248348SEric.Yu@Sun.COM 						return (error);
5258348SEric.Yu@Sun.COM 					}
5268348SEric.Yu@Sun.COM 				}
5278348SEric.Yu@Sun.COM 			}
5288348SEric.Yu@Sun.COM 		}
5298348SEric.Yu@Sun.COM 
5308348SEric.Yu@Sun.COM 		if (flags & SO_FALLBACK) {
5318348SEric.Yu@Sun.COM 			/*
5328348SEric.Yu@Sun.COM 			 * The stream created does not have a conn.
5338348SEric.Yu@Sun.COM 			 * do stream set up after conn has been assigned
5348348SEric.Yu@Sun.COM 			 */
5358348SEric.Yu@Sun.COM 			return (error);
5368348SEric.Yu@Sun.COM 		}
5378348SEric.Yu@Sun.COM 		if (error = so_strinit(so, tso)) {
5388348SEric.Yu@Sun.COM 			(void) sotpi_close(so, flags, cr);
5398348SEric.Yu@Sun.COM 			return (error);
5408348SEric.Yu@Sun.COM 		}
5418348SEric.Yu@Sun.COM 
5428348SEric.Yu@Sun.COM 		/* Wildcard */
5438348SEric.Yu@Sun.COM 		if (so->so_protocol != so->so_sockparams->sp_protocol) {
5448348SEric.Yu@Sun.COM 			int protocol = so->so_protocol;
5458348SEric.Yu@Sun.COM 			/*
5468348SEric.Yu@Sun.COM 			 * Issue SO_PROTOTYPE setsockopt.
5478348SEric.Yu@Sun.COM 			 */
5488348SEric.Yu@Sun.COM 			error = sotpi_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
5498348SEric.Yu@Sun.COM 			    &protocol, (t_uscalar_t)sizeof (protocol), cr);
5508348SEric.Yu@Sun.COM 			if (error != 0) {
5518348SEric.Yu@Sun.COM 				(void) sotpi_close(so, flags, cr);
5528348SEric.Yu@Sun.COM 				/*
5538348SEric.Yu@Sun.COM 				 * Setsockopt often fails with ENOPROTOOPT but
5548348SEric.Yu@Sun.COM 				 * socket() should fail with
5558348SEric.Yu@Sun.COM 				 * EPROTONOSUPPORT/EPROTOTYPE.
5568348SEric.Yu@Sun.COM 				 */
5578348SEric.Yu@Sun.COM 				return (EPROTONOSUPPORT);
5588348SEric.Yu@Sun.COM 			}
5598348SEric.Yu@Sun.COM 		}
5608348SEric.Yu@Sun.COM 
5618348SEric.Yu@Sun.COM 	} else {
5628348SEric.Yu@Sun.COM 		/*
5638348SEric.Yu@Sun.COM 		 * While the same socket can not be reopened (unlike specfs)
5648348SEric.Yu@Sun.COM 		 * the stream head sets STREOPENFAIL when the autopush fails.
5658348SEric.Yu@Sun.COM 		 */
5668348SEric.Yu@Sun.COM 		if ((stp != NULL) &&
5678348SEric.Yu@Sun.COM 		    (stp->sd_flag & STREOPENFAIL)) {
5688348SEric.Yu@Sun.COM 			/*
5698348SEric.Yu@Sun.COM 			 * Open failed part way through.
5708348SEric.Yu@Sun.COM 			 */
5718348SEric.Yu@Sun.COM 			mutex_enter(&stp->sd_lock);
5728348SEric.Yu@Sun.COM 			stp->sd_flag &= ~STREOPENFAIL;
5738348SEric.Yu@Sun.COM 			mutex_exit(&stp->sd_lock);
5748348SEric.Yu@Sun.COM 			(void) sotpi_close(so, flags, cr);
5758348SEric.Yu@Sun.COM 			return (error);
5768348SEric.Yu@Sun.COM 			/*NOTREACHED*/
5778348SEric.Yu@Sun.COM 		}
5788348SEric.Yu@Sun.COM 		ASSERT(stp == NULL);
5798348SEric.Yu@Sun.COM 	}
5808348SEric.Yu@Sun.COM 	TRACE_4(TR_FAC_SOCKFS, TR_SOCKFS_OPEN,
5818348SEric.Yu@Sun.COM 	    "sockfs open:maj %d vp %p so %p error %d",
5828348SEric.Yu@Sun.COM 	    maj, vp, so, error);
5838348SEric.Yu@Sun.COM 	return (error);
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate /*
5870Sstevel@tonic-gate  * Bind the socket to an unspecified address in sockfs only.
5880Sstevel@tonic-gate  * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't
5890Sstevel@tonic-gate  * required in all cases.
5900Sstevel@tonic-gate  */
5910Sstevel@tonic-gate static void
5920Sstevel@tonic-gate so_automatic_bind(struct sonode *so)
5930Sstevel@tonic-gate {
5948348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
5950Sstevel@tonic-gate 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
5980Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_ISBOUND));
5998348SEric.Yu@Sun.COM 	ASSERT(sti->sti_unbind_mp);
6008348SEric.Yu@Sun.COM 
6018348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
6028348SEric.Yu@Sun.COM 	bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
6038348SEric.Yu@Sun.COM 	sti->sti_laddr_sa->sa_family = so->so_family;
6040Sstevel@tonic-gate 	so->so_state |= SS_ISBOUND;
6050Sstevel@tonic-gate }
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate /*
6090Sstevel@tonic-gate  * bind the socket.
6100Sstevel@tonic-gate  *
6110Sstevel@tonic-gate  * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2
6120Sstevel@tonic-gate  * are passed in we allow rebinding. Note that for backwards compatibility
6130Sstevel@tonic-gate  * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind.
6140Sstevel@tonic-gate  * Thus the rebinding code is currently not executed.
6150Sstevel@tonic-gate  *
6160Sstevel@tonic-gate  * The constraints for rebinding are:
6170Sstevel@tonic-gate  * - it is a SOCK_DGRAM, or
6180Sstevel@tonic-gate  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6190Sstevel@tonic-gate  *   and no listen() has been done.
6200Sstevel@tonic-gate  * This rebinding code was added based on some language in the XNET book
6210Sstevel@tonic-gate  * about not returning EINVAL it the protocol allows rebinding. However,
6220Sstevel@tonic-gate  * this language is not present in the Posix socket draft. Thus maybe the
6230Sstevel@tonic-gate  * rebinding logic should be deleted from the source.
6240Sstevel@tonic-gate  *
6250Sstevel@tonic-gate  * A null "name" can be used to unbind the socket if:
6260Sstevel@tonic-gate  * - it is a SOCK_DGRAM, or
6270Sstevel@tonic-gate  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6280Sstevel@tonic-gate  *   and no listen() has been done.
6290Sstevel@tonic-gate  */
6308348SEric.Yu@Sun.COM /* ARGSUSED */
6310Sstevel@tonic-gate static int
6320Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name,
6338348SEric.Yu@Sun.COM     socklen_t namelen, int backlog, int flags, struct cred *cr)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	struct T_bind_req	bind_req;
6360Sstevel@tonic-gate 	struct T_bind_ack	*bind_ack;
6370Sstevel@tonic-gate 	int			error = 0;
6380Sstevel@tonic-gate 	mblk_t			*mp;
6390Sstevel@tonic-gate 	void			*addr;
6400Sstevel@tonic-gate 	t_uscalar_t		addrlen;
6410Sstevel@tonic-gate 	int			unbind_on_err = 1;
6420Sstevel@tonic-gate 	boolean_t		clear_acceptconn_on_err = B_FALSE;
6430Sstevel@tonic-gate 	boolean_t		restore_backlog_on_err = B_FALSE;
6440Sstevel@tonic-gate 	int			save_so_backlog;
6450Sstevel@tonic-gate 	t_scalar_t		PRIM_type = O_T_BIND_REQ;
6460Sstevel@tonic-gate 	boolean_t		tcp_udp_xport;
6470Sstevel@tonic-gate 	void			*nl7c = NULL;
6488348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n",
6517240Srh87107 	    (void *)so, (void *)name, namelen, backlog, flags,
6525240Snordmark 	    pr_state(so->so_state, so->so_mode)));
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM;
6550Sstevel@tonic-gate 
6560Sstevel@tonic-gate 	if (!(flags & _SOBIND_LOCK_HELD)) {
6570Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
6580Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
6590Sstevel@tonic-gate 	} else {
6600Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
6610Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
6620Sstevel@tonic-gate 	}
6630Sstevel@tonic-gate 
6640Sstevel@tonic-gate 	/*
6650Sstevel@tonic-gate 	 * Make sure that there is a preallocated unbind_req message
6660Sstevel@tonic-gate 	 * before binding. This message allocated when the socket is
6670Sstevel@tonic-gate 	 * created  but it might be have been consumed.
6680Sstevel@tonic-gate 	 */
6698348SEric.Yu@Sun.COM 	if (sti->sti_unbind_mp == NULL) {
6700Sstevel@tonic-gate 		dprintso(so, 1, ("sobind: allocating unbind_req\n"));
6710Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
6728348SEric.Yu@Sun.COM 		sti->sti_unbind_mp =
6738778SErik.Nordmark@Sun.COM 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP,
6748778SErik.Nordmark@Sun.COM 		    cr);
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate 
6770Sstevel@tonic-gate 	if (flags & _SOBIND_REBIND) {
6780Sstevel@tonic-gate 		/*
6790Sstevel@tonic-gate 		 * Called from solisten after doing an sotpi_unbind() or
6800Sstevel@tonic-gate 		 * potentially without the unbind (latter for AF_INET{,6}).
6810Sstevel@tonic-gate 		 */
6820Sstevel@tonic-gate 		ASSERT(name == NULL && namelen == 0);
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
6858348SEric.Yu@Sun.COM 			ASSERT(sti->sti_ux_bound_vp);
6868348SEric.Yu@Sun.COM 			addr = &sti->sti_ux_laddr;
6878348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
6885240Snordmark 			dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, "
6895240Snordmark 			    "addr 0x%p, vp %p\n",
6900Sstevel@tonic-gate 			    addrlen,
6917240Srh87107 			    (void *)((struct so_ux_addr *)addr)->soua_vp,
6928348SEric.Yu@Sun.COM 			    (void *)sti->sti_ux_bound_vp));
6930Sstevel@tonic-gate 		} else {
6948348SEric.Yu@Sun.COM 			addr = sti->sti_laddr_sa;
6958348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sti->sti_laddr_len;
6960Sstevel@tonic-gate 		}
6970Sstevel@tonic-gate 	} else if (flags & _SOBIND_UNSPEC) {
6980Sstevel@tonic-gate 		ASSERT(name == NULL && namelen == 0);
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		/*
7010Sstevel@tonic-gate 		 * The caller checked SS_ISBOUND but not necessarily
7020Sstevel@tonic-gate 		 * under so_lock
7030Sstevel@tonic-gate 		 */
7040Sstevel@tonic-gate 		if (so->so_state & SS_ISBOUND) {
7050Sstevel@tonic-gate 			/* No error */
7060Sstevel@tonic-gate 			goto done;
7070Sstevel@tonic-gate 		}
7080Sstevel@tonic-gate 
7090Sstevel@tonic-gate 		/* Set an initial local address */
7100Sstevel@tonic-gate 		switch (so->so_family) {
7110Sstevel@tonic-gate 		case AF_UNIX:
7120Sstevel@tonic-gate 			/*
7130Sstevel@tonic-gate 			 * Use an address with same size as struct sockaddr
7140Sstevel@tonic-gate 			 * just like BSD.
7150Sstevel@tonic-gate 			 */
7168348SEric.Yu@Sun.COM 			sti->sti_laddr_len =
7175240Snordmark 			    (socklen_t)sizeof (struct sockaddr);
7188348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7198348SEric.Yu@Sun.COM 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7208348SEric.Yu@Sun.COM 			sti->sti_laddr_sa->sa_family = so->so_family;
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 			/*
7230Sstevel@tonic-gate 			 * Pass down an address with the implicit bind
7240Sstevel@tonic-gate 			 * magic number and the rest all zeros.
7250Sstevel@tonic-gate 			 * The transport will return a unique address.
7260Sstevel@tonic-gate 			 */
7278348SEric.Yu@Sun.COM 			sti->sti_ux_laddr.soua_vp = NULL;
7288348SEric.Yu@Sun.COM 			sti->sti_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT;
7298348SEric.Yu@Sun.COM 			addr = &sti->sti_ux_laddr;
7308348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
7310Sstevel@tonic-gate 			break;
7320Sstevel@tonic-gate 
7330Sstevel@tonic-gate 		case AF_INET:
7340Sstevel@tonic-gate 		case AF_INET6:
7350Sstevel@tonic-gate 			/*
7360Sstevel@tonic-gate 			 * An unspecified bind in TPI has a NULL address.
7370Sstevel@tonic-gate 			 * Set the address in sockfs to have the sa_family.
7380Sstevel@tonic-gate 			 */
7398348SEric.Yu@Sun.COM 			sti->sti_laddr_len = (so->so_family == AF_INET) ?
7400Sstevel@tonic-gate 			    (socklen_t)sizeof (sin_t) :
7410Sstevel@tonic-gate 			    (socklen_t)sizeof (sin6_t);
7428348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7438348SEric.Yu@Sun.COM 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7448348SEric.Yu@Sun.COM 			sti->sti_laddr_sa->sa_family = so->so_family;
7450Sstevel@tonic-gate 			addr = NULL;
7460Sstevel@tonic-gate 			addrlen = 0;
7470Sstevel@tonic-gate 			break;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 		default:
7500Sstevel@tonic-gate 			/*
7510Sstevel@tonic-gate 			 * An unspecified bind in TPI has a NULL address.
7520Sstevel@tonic-gate 			 * Set the address in sockfs to be zero length.
7530Sstevel@tonic-gate 			 *
7540Sstevel@tonic-gate 			 * Can not assume there is a sa_family for all
7550Sstevel@tonic-gate 			 * protocol families. For example, AF_X25 does not
7560Sstevel@tonic-gate 			 * have a family field.
7570Sstevel@tonic-gate 			 */
7588348SEric.Yu@Sun.COM 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7598348SEric.Yu@Sun.COM 			sti->sti_laddr_len = 0;	/* XXX correct? */
7600Sstevel@tonic-gate 			addr = NULL;
7610Sstevel@tonic-gate 			addrlen = 0;
7620Sstevel@tonic-gate 			break;
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	} else {
7660Sstevel@tonic-gate 		if (so->so_state & SS_ISBOUND) {
7670Sstevel@tonic-gate 			/*
7680Sstevel@tonic-gate 			 * If it is ok to rebind the socket, first unbind
7690Sstevel@tonic-gate 			 * with the transport. A rebind to the NULL address
7700Sstevel@tonic-gate 			 * is interpreted as an unbind.
7710Sstevel@tonic-gate 			 * Note that a bind to NULL in BSD does unbind the
7720Sstevel@tonic-gate 			 * socket but it fails with EINVAL.
7730Sstevel@tonic-gate 			 * Note that regular sockets set SOV_SOCKBSD i.e.
7740Sstevel@tonic-gate 			 * _SOBIND_SOCKBSD gets set here hence no type of
7750Sstevel@tonic-gate 			 * socket does currently allow rebinding.
7760Sstevel@tonic-gate 			 *
7770Sstevel@tonic-gate 			 * If the name is NULL just do an unbind.
7780Sstevel@tonic-gate 			 */
7790Sstevel@tonic-gate 			if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) &&
7800Sstevel@tonic-gate 			    name != NULL) {
7810Sstevel@tonic-gate 				error = EINVAL;
7820Sstevel@tonic-gate 				unbind_on_err = 0;
7830Sstevel@tonic-gate 				eprintsoline(so, error);
7840Sstevel@tonic-gate 				goto done;
7850Sstevel@tonic-gate 			}
7860Sstevel@tonic-gate 			if ((so->so_mode & SM_CONNREQUIRED) &&
7870Sstevel@tonic-gate 			    (so->so_state & SS_CANTREBIND)) {
7880Sstevel@tonic-gate 				error = EINVAL;
7890Sstevel@tonic-gate 				unbind_on_err = 0;
7900Sstevel@tonic-gate 				eprintsoline(so, error);
7910Sstevel@tonic-gate 				goto done;
7920Sstevel@tonic-gate 			}
7930Sstevel@tonic-gate 			error = sotpi_unbind(so, 0);
7940Sstevel@tonic-gate 			if (error) {
7950Sstevel@tonic-gate 				eprintsoline(so, error);
7960Sstevel@tonic-gate 				goto done;
7970Sstevel@tonic-gate 			}
7980Sstevel@tonic-gate 			ASSERT(!(so->so_state & SS_ISBOUND));
7990Sstevel@tonic-gate 			if (name == NULL) {
8000Sstevel@tonic-gate 				so->so_state &=
8015240Snordmark 				    ~(SS_ISCONNECTED|SS_ISCONNECTING);
8020Sstevel@tonic-gate 				goto done;
8030Sstevel@tonic-gate 			}
8040Sstevel@tonic-gate 		}
8058348SEric.Yu@Sun.COM 
8060Sstevel@tonic-gate 		/* X/Open requires this check */
8070Sstevel@tonic-gate 		if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
8080Sstevel@tonic-gate 			if (xnet_check_print) {
8090Sstevel@tonic-gate 				printf("sockfs: X/Open bind state check "
8100Sstevel@tonic-gate 				    "caused EINVAL\n");
8110Sstevel@tonic-gate 			}
8120Sstevel@tonic-gate 			error = EINVAL;
8130Sstevel@tonic-gate 			goto done;
8140Sstevel@tonic-gate 		}
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 		switch (so->so_family) {
8170Sstevel@tonic-gate 		case AF_UNIX:
8180Sstevel@tonic-gate 			/*
8190Sstevel@tonic-gate 			 * All AF_UNIX addresses are nul terminated
8200Sstevel@tonic-gate 			 * when copied (copyin_name) in so the minimum
8210Sstevel@tonic-gate 			 * length is 3 bytes.
8220Sstevel@tonic-gate 			 */
8230Sstevel@tonic-gate 			if (name == NULL ||
8240Sstevel@tonic-gate 			    (ssize_t)namelen <= sizeof (short) + 1) {
8250Sstevel@tonic-gate 				error = EISDIR;
8260Sstevel@tonic-gate 				eprintsoline(so, error);
8270Sstevel@tonic-gate 				goto done;
8280Sstevel@tonic-gate 			}
8290Sstevel@tonic-gate 			/*
8300Sstevel@tonic-gate 			 * Verify so_family matches the bound family.
8310Sstevel@tonic-gate 			 * BSD does not check this for AF_UNIX resulting
8320Sstevel@tonic-gate 			 * in funny mknods.
8330Sstevel@tonic-gate 			 */
8340Sstevel@tonic-gate 			if (name->sa_family != so->so_family) {
8350Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8360Sstevel@tonic-gate 				goto done;
8370Sstevel@tonic-gate 			}
8380Sstevel@tonic-gate 			break;
8390Sstevel@tonic-gate 		case AF_INET:
8400Sstevel@tonic-gate 			if (name == NULL) {
8410Sstevel@tonic-gate 				error = EINVAL;
8420Sstevel@tonic-gate 				eprintsoline(so, error);
8430Sstevel@tonic-gate 				goto done;
8440Sstevel@tonic-gate 			}
8450Sstevel@tonic-gate 			if ((size_t)namelen != sizeof (sin_t)) {
8460Sstevel@tonic-gate 				error = name->sa_family != so->so_family ?
8470Sstevel@tonic-gate 				    EAFNOSUPPORT : EINVAL;
8480Sstevel@tonic-gate 				eprintsoline(so, error);
8490Sstevel@tonic-gate 				goto done;
8500Sstevel@tonic-gate 			}
8510Sstevel@tonic-gate 			if ((flags & _SOBIND_XPG4_2) &&
8520Sstevel@tonic-gate 			    (name->sa_family != so->so_family)) {
8530Sstevel@tonic-gate 				/*
8540Sstevel@tonic-gate 				 * This check has to be made for X/Open
8550Sstevel@tonic-gate 				 * sockets however application failures have
8560Sstevel@tonic-gate 				 * been observed when it is applied to
8570Sstevel@tonic-gate 				 * all sockets.
8580Sstevel@tonic-gate 				 */
8590Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8600Sstevel@tonic-gate 				eprintsoline(so, error);
8610Sstevel@tonic-gate 				goto done;
8620Sstevel@tonic-gate 			}
8630Sstevel@tonic-gate 			/*
8640Sstevel@tonic-gate 			 * Force a zero sa_family to match so_family.
8650Sstevel@tonic-gate 			 *
8660Sstevel@tonic-gate 			 * Some programs like inetd(1M) don't set the
8670Sstevel@tonic-gate 			 * family field. Other programs leave
8680Sstevel@tonic-gate 			 * sin_family set to garbage - SunOS 4.X does
8690Sstevel@tonic-gate 			 * not check the family field on a bind.
8700Sstevel@tonic-gate 			 * We use the family field that
8710Sstevel@tonic-gate 			 * was passed in to the socket() call.
8720Sstevel@tonic-gate 			 */
8730Sstevel@tonic-gate 			name->sa_family = so->so_family;
8740Sstevel@tonic-gate 			break;
8750Sstevel@tonic-gate 
8760Sstevel@tonic-gate 		case AF_INET6: {
8770Sstevel@tonic-gate #ifdef DEBUG
8780Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)name;
8790Sstevel@tonic-gate #endif /* DEBUG */
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 			if (name == NULL) {
8820Sstevel@tonic-gate 				error = EINVAL;
8830Sstevel@tonic-gate 				eprintsoline(so, error);
8840Sstevel@tonic-gate 				goto done;
8850Sstevel@tonic-gate 			}
8860Sstevel@tonic-gate 			if ((size_t)namelen != sizeof (sin6_t)) {
8870Sstevel@tonic-gate 				error = name->sa_family != so->so_family ?
8880Sstevel@tonic-gate 				    EAFNOSUPPORT : EINVAL;
8890Sstevel@tonic-gate 				eprintsoline(so, error);
8900Sstevel@tonic-gate 				goto done;
8910Sstevel@tonic-gate 			}
8920Sstevel@tonic-gate 			if (name->sa_family != so->so_family) {
8930Sstevel@tonic-gate 				/*
8940Sstevel@tonic-gate 				 * With IPv6 we require the family to match
8950Sstevel@tonic-gate 				 * unlike in IPv4.
8960Sstevel@tonic-gate 				 */
8970Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8980Sstevel@tonic-gate 				eprintsoline(so, error);
8990Sstevel@tonic-gate 				goto done;
9000Sstevel@tonic-gate 			}
9010Sstevel@tonic-gate #ifdef DEBUG
9020Sstevel@tonic-gate 			/*
9030Sstevel@tonic-gate 			 * Verify that apps don't forget to clear
9040Sstevel@tonic-gate 			 * sin6_scope_id etc
9050Sstevel@tonic-gate 			 */
9060Sstevel@tonic-gate 			if (sin6->sin6_scope_id != 0 &&
9070Sstevel@tonic-gate 			    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
9081548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
9090Sstevel@tonic-gate 				    "bind with uninitialized sin6_scope_id "
9100Sstevel@tonic-gate 				    "(%d) on socket. Pid = %d\n",
9110Sstevel@tonic-gate 				    (int)sin6->sin6_scope_id,
9120Sstevel@tonic-gate 				    (int)curproc->p_pid);
9130Sstevel@tonic-gate 			}
9140Sstevel@tonic-gate 			if (sin6->__sin6_src_id != 0) {
9151548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
9160Sstevel@tonic-gate 				    "bind with uninitialized __sin6_src_id "
9170Sstevel@tonic-gate 				    "(%d) on socket. Pid = %d\n",
9180Sstevel@tonic-gate 				    (int)sin6->__sin6_src_id,
9190Sstevel@tonic-gate 				    (int)curproc->p_pid);
9200Sstevel@tonic-gate 			}
9210Sstevel@tonic-gate #endif /* DEBUG */
9220Sstevel@tonic-gate 			break;
9230Sstevel@tonic-gate 		}
9240Sstevel@tonic-gate 		default:
9250Sstevel@tonic-gate 			/*
9260Sstevel@tonic-gate 			 * Don't do any length or sa_family check to allow
9270Sstevel@tonic-gate 			 * non-sockaddr style addresses.
9280Sstevel@tonic-gate 			 */
9290Sstevel@tonic-gate 			if (name == NULL) {
9300Sstevel@tonic-gate 				error = EINVAL;
9310Sstevel@tonic-gate 				eprintsoline(so, error);
9320Sstevel@tonic-gate 				goto done;
9330Sstevel@tonic-gate 			}
9340Sstevel@tonic-gate 			break;
9350Sstevel@tonic-gate 		}
9360Sstevel@tonic-gate 
9378348SEric.Yu@Sun.COM 		if (namelen > (t_uscalar_t)sti->sti_laddr_maxlen) {
9380Sstevel@tonic-gate 			error = ENAMETOOLONG;
9390Sstevel@tonic-gate 			eprintsoline(so, error);
9400Sstevel@tonic-gate 			goto done;
9410Sstevel@tonic-gate 		}
9420Sstevel@tonic-gate 		/*
9430Sstevel@tonic-gate 		 * Save local address.
9440Sstevel@tonic-gate 		 */
9458348SEric.Yu@Sun.COM 		sti->sti_laddr_len = (socklen_t)namelen;
9468348SEric.Yu@Sun.COM 		ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
9478348SEric.Yu@Sun.COM 		bcopy(name, sti->sti_laddr_sa, namelen);
9488348SEric.Yu@Sun.COM 
9498348SEric.Yu@Sun.COM 		addr = sti->sti_laddr_sa;
9508348SEric.Yu@Sun.COM 		addrlen = (t_uscalar_t)sti->sti_laddr_len;
9510Sstevel@tonic-gate 		switch (so->so_family) {
9520Sstevel@tonic-gate 		case AF_INET6:
9530Sstevel@tonic-gate 		case AF_INET:
9540Sstevel@tonic-gate 			break;
9550Sstevel@tonic-gate 		case AF_UNIX: {
9560Sstevel@tonic-gate 			struct sockaddr_un *soun =
9578348SEric.Yu@Sun.COM 			    (struct sockaddr_un *)sti->sti_laddr_sa;
9588032SRic.Aleshire@Sun.COM 			struct vnode *vp, *rvp;
9590Sstevel@tonic-gate 			struct vattr vattr;
9600Sstevel@tonic-gate 
9618348SEric.Yu@Sun.COM 			ASSERT(sti->sti_ux_bound_vp == NULL);
9620Sstevel@tonic-gate 			/*
9630Sstevel@tonic-gate 			 * Create vnode for the specified path name.
9648348SEric.Yu@Sun.COM 			 * Keep vnode held with a reference in sti_ux_bound_vp.
9650Sstevel@tonic-gate 			 * Use the vnode pointer as the address used in the
9660Sstevel@tonic-gate 			 * bind with the transport.
9670Sstevel@tonic-gate 			 *
9680Sstevel@tonic-gate 			 * Use the same mode as in BSD. In particular this does
9690Sstevel@tonic-gate 			 * not observe the umask.
9700Sstevel@tonic-gate 			 */
9710Sstevel@tonic-gate 			/* MAXPATHLEN + soun_family + nul termination */
9728348SEric.Yu@Sun.COM 			if (sti->sti_laddr_len >
9730Sstevel@tonic-gate 			    (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
9740Sstevel@tonic-gate 				error = ENAMETOOLONG;
9750Sstevel@tonic-gate 				eprintsoline(so, error);
9760Sstevel@tonic-gate 				goto done;
9770Sstevel@tonic-gate 			}
9780Sstevel@tonic-gate 			vattr.va_type = VSOCK;
9793446Smrj 			vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask;
9800Sstevel@tonic-gate 			vattr.va_mask = AT_TYPE|AT_MODE;
9810Sstevel@tonic-gate 			/* NOTE: holding so_lock */
9820Sstevel@tonic-gate 			error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr,
9835240Snordmark 			    EXCL, 0, &vp, CRMKNOD, 0, 0);
9840Sstevel@tonic-gate 			if (error) {
9850Sstevel@tonic-gate 				if (error == EEXIST)
9860Sstevel@tonic-gate 					error = EADDRINUSE;
9870Sstevel@tonic-gate 				eprintsoline(so, error);
9880Sstevel@tonic-gate 				goto done;
9890Sstevel@tonic-gate 			}
9900Sstevel@tonic-gate 			/*
9910Sstevel@tonic-gate 			 * Establish pointer from the underlying filesystem
9920Sstevel@tonic-gate 			 * vnode to the socket node.
9938348SEric.Yu@Sun.COM 			 * sti_ux_bound_vp and v_stream->sd_vnode form the
9940Sstevel@tonic-gate 			 * cross-linkage between the underlying filesystem
9950Sstevel@tonic-gate 			 * node and the socket node.
9960Sstevel@tonic-gate 			 */
9978032SRic.Aleshire@Sun.COM 
9988032SRic.Aleshire@Sun.COM 			if ((VOP_REALVP(vp, &rvp, NULL) == 0) && (vp != rvp)) {
9998032SRic.Aleshire@Sun.COM 				VN_HOLD(rvp);
10008032SRic.Aleshire@Sun.COM 				VN_RELE(vp);
10018032SRic.Aleshire@Sun.COM 				vp = rvp;
10028032SRic.Aleshire@Sun.COM 			}
10038032SRic.Aleshire@Sun.COM 
10040Sstevel@tonic-gate 			ASSERT(SOTOV(so)->v_stream);
10050Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
10060Sstevel@tonic-gate 			vp->v_stream = SOTOV(so)->v_stream;
10078348SEric.Yu@Sun.COM 			sti->sti_ux_bound_vp = vp;
10080Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
10090Sstevel@tonic-gate 
10100Sstevel@tonic-gate 			/*
10110Sstevel@tonic-gate 			 * Use the vnode pointer value as a unique address
10120Sstevel@tonic-gate 			 * (together with the magic number to avoid conflicts
10130Sstevel@tonic-gate 			 * with implicit binds) in the transport provider.
10140Sstevel@tonic-gate 			 */
10158348SEric.Yu@Sun.COM 			sti->sti_ux_laddr.soua_vp =
10168348SEric.Yu@Sun.COM 			    (void *)sti->sti_ux_bound_vp;
10178348SEric.Yu@Sun.COM 			sti->sti_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT;
10188348SEric.Yu@Sun.COM 			addr = &sti->sti_ux_laddr;
10198348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
10200Sstevel@tonic-gate 			dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n",
10210Sstevel@tonic-gate 			    addrlen,
10228348SEric.Yu@Sun.COM 			    (void *)((struct so_ux_addr *)addr)->soua_vp));
10230Sstevel@tonic-gate 			break;
10240Sstevel@tonic-gate 		}
10250Sstevel@tonic-gate 		} /* end switch (so->so_family) */
10260Sstevel@tonic-gate 	}
10270Sstevel@tonic-gate 
10280Sstevel@tonic-gate 	/*
10290Sstevel@tonic-gate 	 * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since
10300Sstevel@tonic-gate 	 * the transport can start passing up T_CONN_IND messages
10310Sstevel@tonic-gate 	 * as soon as it receives the bind req and strsock_proto()
10320Sstevel@tonic-gate 	 * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs.
10330Sstevel@tonic-gate 	 */
10340Sstevel@tonic-gate 	if (flags & _SOBIND_LISTEN) {
10350Sstevel@tonic-gate 		if ((so->so_state & SS_ACCEPTCONN) == 0)
10360Sstevel@tonic-gate 			clear_acceptconn_on_err = B_TRUE;
10370Sstevel@tonic-gate 		save_so_backlog = so->so_backlog;
10380Sstevel@tonic-gate 		restore_backlog_on_err = B_TRUE;
10390Sstevel@tonic-gate 		so->so_state |= SS_ACCEPTCONN;
10400Sstevel@tonic-gate 		so->so_backlog = backlog;
10410Sstevel@tonic-gate 	}
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	/*
10440Sstevel@tonic-gate 	 * If NL7C addr(s) have been configured check for addr/port match,
10450Sstevel@tonic-gate 	 * or if an implicit NL7C socket via AF_NCA mark socket as NL7C.
10460Sstevel@tonic-gate 	 *
10470Sstevel@tonic-gate 	 * NL7C supports the TCP transport only so check AF_INET and AF_INET6
10480Sstevel@tonic-gate 	 * family sockets only. If match mark as such.
10490Sstevel@tonic-gate 	 */
10501974Sbrutus 	if (nl7c_enabled && ((addr != NULL &&
10510Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
10520Sstevel@tonic-gate 	    (nl7c = nl7c_lookup_addr(addr, addrlen))) ||
10538348SEric.Yu@Sun.COM 	    sti->sti_nl7c_flags == NL7C_AF_NCA)) {
10540Sstevel@tonic-gate 		/*
10550Sstevel@tonic-gate 		 * NL7C is not supported in non-global zones,
10560Sstevel@tonic-gate 		 * we enforce this restriction here.
10570Sstevel@tonic-gate 		 */
10580Sstevel@tonic-gate 		if (so->so_zoneid == GLOBAL_ZONEID) {
10590Sstevel@tonic-gate 			/* An NL7C socket, mark it */
10608348SEric.Yu@Sun.COM 			sti->sti_nl7c_flags |= NL7C_ENABLED;
10611974Sbrutus 			if (nl7c == NULL) {
10621974Sbrutus 				/*
10631974Sbrutus 				 * Was an AF_NCA bind() so add it to the
10641974Sbrutus 				 * addr list for reporting purposes.
10651974Sbrutus 				 */
10661974Sbrutus 				nl7c = nl7c_add_addr(addr, addrlen);
10671974Sbrutus 			}
10680Sstevel@tonic-gate 		} else
10690Sstevel@tonic-gate 			nl7c = NULL;
10700Sstevel@tonic-gate 	}
10718348SEric.Yu@Sun.COM 
10720Sstevel@tonic-gate 	/*
10730Sstevel@tonic-gate 	 * We send a T_BIND_REQ for TCP/UDP since we know it supports it,
10740Sstevel@tonic-gate 	 * for other transports we will send in a O_T_BIND_REQ.
10750Sstevel@tonic-gate 	 */
10760Sstevel@tonic-gate 	if (tcp_udp_xport &&
10770Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6))
10780Sstevel@tonic-gate 		PRIM_type = T_BIND_REQ;
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate 	bind_req.PRIM_type = PRIM_type;
10810Sstevel@tonic-gate 	bind_req.ADDR_length = addrlen;
10820Sstevel@tonic-gate 	bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
10830Sstevel@tonic-gate 	bind_req.CONIND_number = backlog;
10840Sstevel@tonic-gate 	/* NOTE: holding so_lock while sleeping */
10850Sstevel@tonic-gate 	mp = soallocproto2(&bind_req, sizeof (bind_req),
10868778SErik.Nordmark@Sun.COM 	    addr, addrlen, 0, _ALLOC_SLEEP, cr);
10878348SEric.Yu@Sun.COM 	sti->sti_laddr_valid = 0;
10888348SEric.Yu@Sun.COM 
10898348SEric.Yu@Sun.COM 	/* Done using sti_laddr_sa - can drop the lock */
10900Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
10910Sstevel@tonic-gate 
1092898Skais 	/*
1093898Skais 	 * Intercept the bind_req message here to check if this <address/port>
1094898Skais 	 * was configured as an SSL proxy server, or if another endpoint was
1095898Skais 	 * already configured to act as a proxy for us.
10961974Sbrutus 	 *
10971974Sbrutus 	 * Note, only if NL7C not enabled for this socket.
1098898Skais 	 */
10991974Sbrutus 	if (nl7c == NULL &&
11001974Sbrutus 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
1101898Skais 	    so->so_type == SOCK_STREAM) {
1102898Skais 
11038348SEric.Yu@Sun.COM 		if (sti->sti_kssl_ent != NULL) {
11048348SEric.Yu@Sun.COM 			kssl_release_ent(sti->sti_kssl_ent, so,
11058348SEric.Yu@Sun.COM 			    sti->sti_kssl_type);
11068348SEric.Yu@Sun.COM 			sti->sti_kssl_ent = NULL;
1107898Skais 		}
1108898Skais 
11098348SEric.Yu@Sun.COM 		sti->sti_kssl_type = kssl_check_proxy(mp, so,
11108348SEric.Yu@Sun.COM 		    &sti->sti_kssl_ent);
11118348SEric.Yu@Sun.COM 		switch (sti->sti_kssl_type) {
1112898Skais 		case KSSL_NO_PROXY:
1113898Skais 			break;
1114898Skais 
1115898Skais 		case KSSL_HAS_PROXY:
1116898Skais 			mutex_enter(&so->so_lock);
1117898Skais 			goto skip_transport;
1118898Skais 
1119898Skais 		case KSSL_IS_PROXY:
1120898Skais 			break;
1121898Skais 		}
1122898Skais 	}
1123898Skais 
11240Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
11255240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
11260Sstevel@tonic-gate 	if (error) {
11270Sstevel@tonic-gate 		eprintsoline(so, error);
11280Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
11290Sstevel@tonic-gate 		goto done;
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
11330Sstevel@tonic-gate 	error = sowaitprim(so, PRIM_type, T_BIND_ACK,
11340Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (*bind_ack), &mp, 0);
11350Sstevel@tonic-gate 	if (error) {
11360Sstevel@tonic-gate 		eprintsoline(so, error);
11370Sstevel@tonic-gate 		goto done;
11380Sstevel@tonic-gate 	}
1139898Skais skip_transport:
11400Sstevel@tonic-gate 	ASSERT(mp);
11410Sstevel@tonic-gate 	/*
11420Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
11430Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the bind
11440Sstevel@tonic-gate 	 * is allowed to complete.
11450Sstevel@tonic-gate 	 */
11460Sstevel@tonic-gate 
11470Sstevel@tonic-gate 	/* Mark as bound. This will be undone if we detect errors below. */
11480Sstevel@tonic-gate 	if (flags & _SOBIND_NOXLATE) {
11490Sstevel@tonic-gate 		ASSERT(so->so_family == AF_UNIX);
11508348SEric.Yu@Sun.COM 		sti->sti_faddr_noxlate = 1;
11510Sstevel@tonic-gate 	}
11520Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND));
11530Sstevel@tonic-gate 	so->so_state |= SS_ISBOUND;
11548348SEric.Yu@Sun.COM 	ASSERT(sti->sti_unbind_mp);
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	/* note that we've already set SS_ACCEPTCONN above */
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate 	/*
11590Sstevel@tonic-gate 	 * Recompute addrlen - an unspecied bind sent down an
11600Sstevel@tonic-gate 	 * address of length zero but we expect the appropriate length
11610Sstevel@tonic-gate 	 * in return.
11620Sstevel@tonic-gate 	 */
11630Sstevel@tonic-gate 	addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ?
11648348SEric.Yu@Sun.COM 	    sizeof (sti->sti_ux_laddr) : sti->sti_laddr_len);
11650Sstevel@tonic-gate 
11660Sstevel@tonic-gate 	bind_ack = (struct T_bind_ack *)mp->b_rptr;
11670Sstevel@tonic-gate 	/*
11680Sstevel@tonic-gate 	 * The alignment restriction is really too strict but
11690Sstevel@tonic-gate 	 * we want enough alignment to inspect the fields of
11700Sstevel@tonic-gate 	 * a sockaddr_in.
11710Sstevel@tonic-gate 	 */
11720Sstevel@tonic-gate 	addr = sogetoff(mp, bind_ack->ADDR_offset,
11735240Snordmark 	    bind_ack->ADDR_length,
11745240Snordmark 	    __TPI_ALIGN_SIZE);
11750Sstevel@tonic-gate 	if (addr == NULL) {
11760Sstevel@tonic-gate 		freemsg(mp);
11770Sstevel@tonic-gate 		error = EPROTO;
11780Sstevel@tonic-gate 		eprintsoline(so, error);
11790Sstevel@tonic-gate 		goto done;
11800Sstevel@tonic-gate 	}
11810Sstevel@tonic-gate 	if (!(flags & _SOBIND_UNSPEC)) {
11820Sstevel@tonic-gate 		/*
11830Sstevel@tonic-gate 		 * Verify that the transport didn't return something we
11840Sstevel@tonic-gate 		 * did not want e.g. an address other than what we asked for.
11850Sstevel@tonic-gate 		 *
11860Sstevel@tonic-gate 		 * NOTE: These checks would go away if/when we switch to
11870Sstevel@tonic-gate 		 * using the new TPI (in which the transport would fail
11880Sstevel@tonic-gate 		 * the request instead of assigning a different address).
11890Sstevel@tonic-gate 		 *
11900Sstevel@tonic-gate 		 * NOTE2: For protocols that we don't know (i.e. any
11910Sstevel@tonic-gate 		 * other than AF_INET6, AF_INET and AF_UNIX), we
11920Sstevel@tonic-gate 		 * cannot know if the transport should be expected to
11930Sstevel@tonic-gate 		 * return the same address as that requested.
11940Sstevel@tonic-gate 		 *
11950Sstevel@tonic-gate 		 * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send
11960Sstevel@tonic-gate 		 * down a T_BIND_REQ. We use O_T_BIND_REQ for others.
11970Sstevel@tonic-gate 		 *
11980Sstevel@tonic-gate 		 * For example, in the case of netatalk it may be
11990Sstevel@tonic-gate 		 * inappropriate for the transport to return the
12000Sstevel@tonic-gate 		 * requested address (as it may have allocated a local
12010Sstevel@tonic-gate 		 * port number in behaviour similar to that of an
12020Sstevel@tonic-gate 		 * AF_INET bind request with a port number of zero).
12030Sstevel@tonic-gate 		 *
12040Sstevel@tonic-gate 		 * Given the definition of O_T_BIND_REQ, where the
12050Sstevel@tonic-gate 		 * transport may bind to an address other than the
12060Sstevel@tonic-gate 		 * requested address, it's not possible to determine
12070Sstevel@tonic-gate 		 * whether a returned address that differs from the
12080Sstevel@tonic-gate 		 * requested address is a reason to fail (because the
12090Sstevel@tonic-gate 		 * requested address was not available) or succeed
12100Sstevel@tonic-gate 		 * (because the transport allocated an appropriate
12110Sstevel@tonic-gate 		 * address and/or port).
12120Sstevel@tonic-gate 		 *
12130Sstevel@tonic-gate 		 * sockfs currently requires that the transport return
12140Sstevel@tonic-gate 		 * the requested address in the T_BIND_ACK, unless
12150Sstevel@tonic-gate 		 * there is code here to allow for any discrepancy.
12160Sstevel@tonic-gate 		 * Such code exists for AF_INET and AF_INET6.
12170Sstevel@tonic-gate 		 *
12180Sstevel@tonic-gate 		 * Netatalk chooses to return the requested address
12190Sstevel@tonic-gate 		 * rather than the (correct) allocated address.  This
12200Sstevel@tonic-gate 		 * means that netatalk violates the TPI specification
12210Sstevel@tonic-gate 		 * (and would not function correctly if used from a
12220Sstevel@tonic-gate 		 * TLI application), but it does mean that it works
12230Sstevel@tonic-gate 		 * with sockfs.
12240Sstevel@tonic-gate 		 *
12250Sstevel@tonic-gate 		 * As noted above, using the newer XTI bind primitive
12260Sstevel@tonic-gate 		 * (T_BIND_REQ) in preference to O_T_BIND_REQ would
12270Sstevel@tonic-gate 		 * allow sockfs to be more sure about whether or not
12280Sstevel@tonic-gate 		 * the bind request had succeeded (as transports are
12290Sstevel@tonic-gate 		 * not permitted to bind to a different address than
12300Sstevel@tonic-gate 		 * that requested - they must return failure).
12310Sstevel@tonic-gate 		 * Unfortunately, support for T_BIND_REQ may not be
12320Sstevel@tonic-gate 		 * present in all transport implementations (netatalk,
12330Sstevel@tonic-gate 		 * for example, doesn't have it), making the
12340Sstevel@tonic-gate 		 * transition difficult.
12350Sstevel@tonic-gate 		 */
12360Sstevel@tonic-gate 		if (bind_ack->ADDR_length != addrlen) {
12370Sstevel@tonic-gate 			/* Assumes that the requested address was in use */
12380Sstevel@tonic-gate 			freemsg(mp);
12390Sstevel@tonic-gate 			error = EADDRINUSE;
12400Sstevel@tonic-gate 			eprintsoline(so, error);
12410Sstevel@tonic-gate 			goto done;
12420Sstevel@tonic-gate 		}
12430Sstevel@tonic-gate 
12440Sstevel@tonic-gate 		switch (so->so_family) {
12450Sstevel@tonic-gate 		case AF_INET6:
12460Sstevel@tonic-gate 		case AF_INET: {
12470Sstevel@tonic-gate 			sin_t *rname, *aname;
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 			rname = (sin_t *)addr;
12508348SEric.Yu@Sun.COM 			aname = (sin_t *)sti->sti_laddr_sa;
12510Sstevel@tonic-gate 
12520Sstevel@tonic-gate 			/*
12530Sstevel@tonic-gate 			 * Take advantage of the alignment
12540Sstevel@tonic-gate 			 * of sin_port and sin6_port which fall
12550Sstevel@tonic-gate 			 * in the same place in their data structures.
12560Sstevel@tonic-gate 			 * Just use sin_port for either address family.
12570Sstevel@tonic-gate 			 *
12580Sstevel@tonic-gate 			 * This may become a problem if (heaven forbid)
12590Sstevel@tonic-gate 			 * there's a separate ipv6port_reserved... :-P
12600Sstevel@tonic-gate 			 *
12610Sstevel@tonic-gate 			 * Binding to port 0 has the semantics of letting
12620Sstevel@tonic-gate 			 * the transport bind to any port.
12630Sstevel@tonic-gate 			 *
12640Sstevel@tonic-gate 			 * If the transport is TCP or UDP since we had sent
12650Sstevel@tonic-gate 			 * a T_BIND_REQ we would not get a port other than
12660Sstevel@tonic-gate 			 * what we asked for.
12670Sstevel@tonic-gate 			 */
12680Sstevel@tonic-gate 			if (tcp_udp_xport) {
12690Sstevel@tonic-gate 				/*
12700Sstevel@tonic-gate 				 * Pick up the new port number if we bound to
12710Sstevel@tonic-gate 				 * port 0.
12720Sstevel@tonic-gate 				 */
12730Sstevel@tonic-gate 				if (aname->sin_port == 0)
12740Sstevel@tonic-gate 					aname->sin_port = rname->sin_port;
12758348SEric.Yu@Sun.COM 				sti->sti_laddr_valid = 1;
12760Sstevel@tonic-gate 				break;
12770Sstevel@tonic-gate 			}
12780Sstevel@tonic-gate 			if (aname->sin_port != 0 &&
12790Sstevel@tonic-gate 			    aname->sin_port != rname->sin_port) {
12800Sstevel@tonic-gate 				freemsg(mp);
12810Sstevel@tonic-gate 				error = EADDRINUSE;
12820Sstevel@tonic-gate 				eprintsoline(so, error);
12830Sstevel@tonic-gate 				goto done;
12840Sstevel@tonic-gate 			}
12850Sstevel@tonic-gate 			/*
12860Sstevel@tonic-gate 			 * Pick up the new port number if we bound to port 0.
12870Sstevel@tonic-gate 			 */
12880Sstevel@tonic-gate 			aname->sin_port = rname->sin_port;
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 			/*
12910Sstevel@tonic-gate 			 * Unfortunately, addresses aren't _quite_ the same.
12920Sstevel@tonic-gate 			 */
12930Sstevel@tonic-gate 			if (so->so_family == AF_INET) {
12940Sstevel@tonic-gate 				if (aname->sin_addr.s_addr !=
12950Sstevel@tonic-gate 				    rname->sin_addr.s_addr) {
12960Sstevel@tonic-gate 					freemsg(mp);
12970Sstevel@tonic-gate 					error = EADDRNOTAVAIL;
12980Sstevel@tonic-gate 					eprintsoline(so, error);
12990Sstevel@tonic-gate 					goto done;
13000Sstevel@tonic-gate 				}
13010Sstevel@tonic-gate 			} else {
13020Sstevel@tonic-gate 				sin6_t *rname6 = (sin6_t *)rname;
13030Sstevel@tonic-gate 				sin6_t *aname6 = (sin6_t *)aname;
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 				if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr,
13060Sstevel@tonic-gate 				    &rname6->sin6_addr)) {
13070Sstevel@tonic-gate 					freemsg(mp);
13080Sstevel@tonic-gate 					error = EADDRNOTAVAIL;
13090Sstevel@tonic-gate 					eprintsoline(so, error);
13100Sstevel@tonic-gate 					goto done;
13110Sstevel@tonic-gate 				}
13120Sstevel@tonic-gate 			}
13130Sstevel@tonic-gate 			break;
13140Sstevel@tonic-gate 		}
13150Sstevel@tonic-gate 		case AF_UNIX:
13168348SEric.Yu@Sun.COM 			if (bcmp(addr, &sti->sti_ux_laddr, addrlen) != 0) {
13170Sstevel@tonic-gate 				freemsg(mp);
13180Sstevel@tonic-gate 				error = EADDRINUSE;
13190Sstevel@tonic-gate 				eprintsoline(so, error);
13200Sstevel@tonic-gate 				eprintso(so,
13215240Snordmark 				    ("addrlen %d, addr 0x%x, vp %p\n",
13225240Snordmark 				    addrlen, *((int *)addr),
13238348SEric.Yu@Sun.COM 				    (void *)sti->sti_ux_bound_vp));
13240Sstevel@tonic-gate 				goto done;
13250Sstevel@tonic-gate 			}
13268348SEric.Yu@Sun.COM 			sti->sti_laddr_valid = 1;
13270Sstevel@tonic-gate 			break;
13280Sstevel@tonic-gate 		default:
13290Sstevel@tonic-gate 			/*
13300Sstevel@tonic-gate 			 * NOTE: This assumes that addresses can be
13310Sstevel@tonic-gate 			 * byte-compared for equivalence.
13320Sstevel@tonic-gate 			 */
13338348SEric.Yu@Sun.COM 			if (bcmp(addr, sti->sti_laddr_sa, addrlen) != 0) {
13340Sstevel@tonic-gate 				freemsg(mp);
13350Sstevel@tonic-gate 				error = EADDRINUSE;
13360Sstevel@tonic-gate 				eprintsoline(so, error);
13370Sstevel@tonic-gate 				goto done;
13380Sstevel@tonic-gate 			}
13390Sstevel@tonic-gate 			/*
13408348SEric.Yu@Sun.COM 			 * Don't mark sti_laddr_valid, as we cannot be
13410Sstevel@tonic-gate 			 * sure that the returned address is the real
13420Sstevel@tonic-gate 			 * bound address when talking to an unknown
13430Sstevel@tonic-gate 			 * transport.
13440Sstevel@tonic-gate 			 */
13450Sstevel@tonic-gate 			break;
13460Sstevel@tonic-gate 		}
13470Sstevel@tonic-gate 	} else {
13480Sstevel@tonic-gate 		/*
13490Sstevel@tonic-gate 		 * Save for returned address for getsockname.
13500Sstevel@tonic-gate 		 * Needed for unspecific bind unless transport supports
13510Sstevel@tonic-gate 		 * the TI_GETMYNAME ioctl.
13520Sstevel@tonic-gate 		 * Do this for AF_INET{,6} even though they do, as
13530Sstevel@tonic-gate 		 * caching info here is much better performance than
13540Sstevel@tonic-gate 		 * a TPI/STREAMS trip to the transport for getsockname.
13550Sstevel@tonic-gate 		 * Any which can't for some reason _must_ _not_ set
13568348SEric.Yu@Sun.COM 		 * sti_laddr_valid here for the caching version of
13578348SEric.Yu@Sun.COM 		 * getsockname to not break;
13580Sstevel@tonic-gate 		 */
13590Sstevel@tonic-gate 		switch (so->so_family) {
13600Sstevel@tonic-gate 		case AF_UNIX:
13610Sstevel@tonic-gate 			/*
13620Sstevel@tonic-gate 			 * Record the address bound with the transport
13630Sstevel@tonic-gate 			 * for use by socketpair.
13640Sstevel@tonic-gate 			 */
13658348SEric.Yu@Sun.COM 			bcopy(addr, &sti->sti_ux_laddr, addrlen);
13668348SEric.Yu@Sun.COM 			sti->sti_laddr_valid = 1;
13670Sstevel@tonic-gate 			break;
13680Sstevel@tonic-gate 		case AF_INET:
13690Sstevel@tonic-gate 		case AF_INET6:
13708348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
13718348SEric.Yu@Sun.COM 			bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
13728348SEric.Yu@Sun.COM 			sti->sti_laddr_valid = 1;
13730Sstevel@tonic-gate 			break;
13740Sstevel@tonic-gate 		default:
13750Sstevel@tonic-gate 			/*
13768348SEric.Yu@Sun.COM 			 * Don't mark sti_laddr_valid, as we cannot be
13770Sstevel@tonic-gate 			 * sure that the returned address is the real
13780Sstevel@tonic-gate 			 * bound address when talking to an unknown
13790Sstevel@tonic-gate 			 * transport.
13800Sstevel@tonic-gate 			 */
13810Sstevel@tonic-gate 			break;
13820Sstevel@tonic-gate 		}
13830Sstevel@tonic-gate 	}
13840Sstevel@tonic-gate 
13850Sstevel@tonic-gate 	if (nl7c != NULL) {
13861974Sbrutus 		/* Register listen()er sonode pointer with NL7C */
13871974Sbrutus 		nl7c_listener_addr(nl7c, so);
13880Sstevel@tonic-gate 	}
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 	freemsg(mp);
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate done:
13930Sstevel@tonic-gate 	if (error) {
13940Sstevel@tonic-gate 		/* reset state & backlog to values held on entry */
13950Sstevel@tonic-gate 		if (clear_acceptconn_on_err == B_TRUE)
13960Sstevel@tonic-gate 			so->so_state &= ~SS_ACCEPTCONN;
13970Sstevel@tonic-gate 		if (restore_backlog_on_err == B_TRUE)
13980Sstevel@tonic-gate 			so->so_backlog = save_so_backlog;
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate 		if (unbind_on_err && so->so_state & SS_ISBOUND) {
14010Sstevel@tonic-gate 			int err;
14020Sstevel@tonic-gate 
14030Sstevel@tonic-gate 			err = sotpi_unbind(so, 0);
14040Sstevel@tonic-gate 			/* LINTED - statement has no consequent: if */
14050Sstevel@tonic-gate 			if (err) {
14060Sstevel@tonic-gate 				eprintsoline(so, error);
14070Sstevel@tonic-gate 			} else {
14080Sstevel@tonic-gate 				ASSERT(!(so->so_state & SS_ISBOUND));
14090Sstevel@tonic-gate 			}
14100Sstevel@tonic-gate 		}
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 	if (!(flags & _SOBIND_LOCK_HELD)) {
14130Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
14140Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
14150Sstevel@tonic-gate 	} else {
14160Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
14170Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
14180Sstevel@tonic-gate 	}
14190Sstevel@tonic-gate 	return (error);
14200Sstevel@tonic-gate }
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate /* bind the socket */
1423741Smasputra static int
14240Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
14258348SEric.Yu@Sun.COM     int flags, struct cred *cr)
14260Sstevel@tonic-gate {
14270Sstevel@tonic-gate 	if ((flags & _SOBIND_SOCKETPAIR) == 0)
14288348SEric.Yu@Sun.COM 		return (sotpi_bindlisten(so, name, namelen, 0, flags, cr));
14290Sstevel@tonic-gate 
14300Sstevel@tonic-gate 	flags &= ~_SOBIND_SOCKETPAIR;
14318348SEric.Yu@Sun.COM 	return (sotpi_bindlisten(so, name, namelen, 1, flags, cr));
14320Sstevel@tonic-gate }
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate /*
14350Sstevel@tonic-gate  * Unbind a socket - used when bind() fails, when bind() specifies a NULL
14360Sstevel@tonic-gate  * address, or when listen needs to unbind and bind.
14370Sstevel@tonic-gate  * If the _SOUNBIND_REBIND flag is specified the addresses are retained
14380Sstevel@tonic-gate  * so that a sobind can pick them up.
14390Sstevel@tonic-gate  */
14400Sstevel@tonic-gate static int
14410Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags)
14420Sstevel@tonic-gate {
14430Sstevel@tonic-gate 	struct T_unbind_req	unbind_req;
14440Sstevel@tonic-gate 	int			error = 0;
14450Sstevel@tonic-gate 	mblk_t			*mp;
14468348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n",
14497240Srh87107 	    (void *)so, flags, pr_state(so->so_state, so->so_mode)));
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14520Sstevel@tonic-gate 	ASSERT(so->so_flag & SOLOCKED);
14530Sstevel@tonic-gate 
14540Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
14550Sstevel@tonic-gate 		error = EINVAL;
14560Sstevel@tonic-gate 		eprintsoline(so, error);
14570Sstevel@tonic-gate 		goto done;
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate 
14600Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate 	/*
14630Sstevel@tonic-gate 	 * Flush the read and write side (except stream head read queue)
14640Sstevel@tonic-gate 	 * and send down T_UNBIND_REQ.
14650Sstevel@tonic-gate 	 */
14660Sstevel@tonic-gate 	(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	unbind_req.PRIM_type = T_UNBIND_REQ;
14690Sstevel@tonic-gate 	mp = soallocproto1(&unbind_req, sizeof (unbind_req),
14708778SErik.Nordmark@Sun.COM 	    0, _ALLOC_SLEEP, CRED());
14710Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
14725240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
14730Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
14740Sstevel@tonic-gate 	if (error) {
14750Sstevel@tonic-gate 		eprintsoline(so, error);
14760Sstevel@tonic-gate 		goto done;
14770Sstevel@tonic-gate 	}
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	error = sowaitokack(so, T_UNBIND_REQ);
14800Sstevel@tonic-gate 	if (error) {
14810Sstevel@tonic-gate 		eprintsoline(so, error);
14820Sstevel@tonic-gate 		goto done;
14830Sstevel@tonic-gate 	}
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	/*
14860Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
14870Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the unbind
14880Sstevel@tonic-gate 	 * is allowed to complete.
14890Sstevel@tonic-gate 	 */
14900Sstevel@tonic-gate 	if (!(flags & _SOUNBIND_REBIND)) {
14910Sstevel@tonic-gate 		/*
14920Sstevel@tonic-gate 		 * Clear out bound address.
14930Sstevel@tonic-gate 		 */
14940Sstevel@tonic-gate 		vnode_t *vp;
14950Sstevel@tonic-gate 
14968348SEric.Yu@Sun.COM 		if ((vp = sti->sti_ux_bound_vp) != NULL) {
1497898Skais 
1498898Skais 			/* Undo any SSL proxy setup */
1499898Skais 			if ((so->so_family == AF_INET ||
1500898Skais 			    so->so_family == AF_INET6) &&
1501898Skais 			    (so->so_type == SOCK_STREAM) &&
15028348SEric.Yu@Sun.COM 			    (sti->sti_kssl_ent != NULL)) {
15038348SEric.Yu@Sun.COM 				kssl_release_ent(sti->sti_kssl_ent, so,
15048348SEric.Yu@Sun.COM 				    sti->sti_kssl_type);
15058348SEric.Yu@Sun.COM 				sti->sti_kssl_ent = NULL;
15068348SEric.Yu@Sun.COM 				sti->sti_kssl_type = KSSL_NO_PROXY;
1507898Skais 			}
15088348SEric.Yu@Sun.COM 			sti->sti_ux_bound_vp = NULL;
15090Sstevel@tonic-gate 			vn_rele_stream(vp);
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 		/* Clear out address */
15128348SEric.Yu@Sun.COM 		sti->sti_laddr_len = 0;
15138348SEric.Yu@Sun.COM 	}
15148348SEric.Yu@Sun.COM 	so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN);
15158348SEric.Yu@Sun.COM 	sti->sti_laddr_valid = 0;
15161974Sbrutus 
15170Sstevel@tonic-gate done:
1518898Skais 
15190Sstevel@tonic-gate 	/* If the caller held the lock don't release it here */
15200Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
15210Sstevel@tonic-gate 	ASSERT(so->so_flag & SOLOCKED);
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	return (error);
15240Sstevel@tonic-gate }
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate /*
15270Sstevel@tonic-gate  * listen on the socket.
15280Sstevel@tonic-gate  * For TPI conforming transports this has to first unbind with the transport
15290Sstevel@tonic-gate  * and then bind again using the new backlog.
15300Sstevel@tonic-gate  */
15318348SEric.Yu@Sun.COM /* ARGSUSED */
15320Sstevel@tonic-gate int
15338348SEric.Yu@Sun.COM sotpi_listen(struct sonode *so, int backlog, struct cred *cr)
15340Sstevel@tonic-gate {
15350Sstevel@tonic-gate 	int		error = 0;
15368348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n",
15397240Srh87107 	    (void *)so, backlog, pr_state(so->so_state, so->so_mode)));
15400Sstevel@tonic-gate 
15418348SEric.Yu@Sun.COM 	if (sti->sti_serv_type == T_CLTS)
15420Sstevel@tonic-gate 		return (EOPNOTSUPP);
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate 	/*
15450Sstevel@tonic-gate 	 * If the socket is ready to accept connections already, then
15460Sstevel@tonic-gate 	 * return without doing anything.  This avoids a problem where
15470Sstevel@tonic-gate 	 * a second listen() call fails if a connection is pending and
15480Sstevel@tonic-gate 	 * leaves the socket unbound. Only when we are not unbinding
15490Sstevel@tonic-gate 	 * with the transport can we safely increase the backlog.
15500Sstevel@tonic-gate 	 */
15510Sstevel@tonic-gate 	if (so->so_state & SS_ACCEPTCONN &&
15520Sstevel@tonic-gate 	    !((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15535240Snordmark 	    /*CONSTCOND*/
15545240Snordmark 	    !solisten_tpi_tcp))
15550Sstevel@tonic-gate 		return (0);
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate 	if (so->so_state & SS_ISCONNECTED)
15580Sstevel@tonic-gate 		return (EINVAL);
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
15610Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	/*
15640Sstevel@tonic-gate 	 * If the listen doesn't change the backlog we do nothing.
15650Sstevel@tonic-gate 	 * This avoids an EPROTO error from the transport.
15660Sstevel@tonic-gate 	 */
15670Sstevel@tonic-gate 	if ((so->so_state & SS_ACCEPTCONN) &&
15680Sstevel@tonic-gate 	    so->so_backlog == backlog)
15690Sstevel@tonic-gate 		goto done;
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
15720Sstevel@tonic-gate 		/*
15730Sstevel@tonic-gate 		 * Must have been explicitly bound in the UNIX domain.
15740Sstevel@tonic-gate 		 */
15750Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
15760Sstevel@tonic-gate 			error = EINVAL;
15770Sstevel@tonic-gate 			goto done;
15780Sstevel@tonic-gate 		}
15790Sstevel@tonic-gate 		error = sotpi_bindlisten(so, NULL, 0, backlog,
15808348SEric.Yu@Sun.COM 		    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15810Sstevel@tonic-gate 	} else if (backlog > 0) {
15820Sstevel@tonic-gate 		/*
15830Sstevel@tonic-gate 		 * AF_INET{,6} hack to avoid losing the port.
15840Sstevel@tonic-gate 		 * Assumes that all AF_INET{,6} transports can handle a
15850Sstevel@tonic-gate 		 * O_T_BIND_REQ with a non-zero CONIND_number when the TPI
15860Sstevel@tonic-gate 		 * has already bound thus it is possible to avoid the unbind.
15870Sstevel@tonic-gate 		 */
15880Sstevel@tonic-gate 		if (!((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15890Sstevel@tonic-gate 		    /*CONSTCOND*/
15900Sstevel@tonic-gate 		    !solisten_tpi_tcp)) {
15910Sstevel@tonic-gate 			error = sotpi_unbind(so, _SOUNBIND_REBIND);
15920Sstevel@tonic-gate 			if (error)
15930Sstevel@tonic-gate 				goto done;
15940Sstevel@tonic-gate 		}
15950Sstevel@tonic-gate 		error = sotpi_bindlisten(so, NULL, 0, backlog,
15968348SEric.Yu@Sun.COM 		    _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15970Sstevel@tonic-gate 	} else {
15980Sstevel@tonic-gate 		so->so_state |= SS_ACCEPTCONN;
15990Sstevel@tonic-gate 		so->so_backlog = backlog;
16000Sstevel@tonic-gate 	}
16010Sstevel@tonic-gate 	if (error)
16020Sstevel@tonic-gate 		goto done;
16030Sstevel@tonic-gate 	ASSERT(so->so_state & SS_ACCEPTCONN);
16040Sstevel@tonic-gate done:
16050Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
16060Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
16070Sstevel@tonic-gate 	return (error);
16080Sstevel@tonic-gate }
16090Sstevel@tonic-gate 
16100Sstevel@tonic-gate /*
16110Sstevel@tonic-gate  * Disconnect either a specified seqno or all (-1).
16120Sstevel@tonic-gate  * The former is used on listening sockets only.
16130Sstevel@tonic-gate  *
16140Sstevel@tonic-gate  * When seqno == -1 sodisconnect could call sotpi_unbind. However,
16150Sstevel@tonic-gate  * the current use of sodisconnect(seqno == -1) is only for shutdown
16160Sstevel@tonic-gate  * so there is no point (and potentially incorrect) to unbind.
16170Sstevel@tonic-gate  */
16188348SEric.Yu@Sun.COM static int
16190Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags)
16200Sstevel@tonic-gate {
16210Sstevel@tonic-gate 	struct T_discon_req	discon_req;
16220Sstevel@tonic-gate 	int			error = 0;
16230Sstevel@tonic-gate 	mblk_t			*mp;
16240Sstevel@tonic-gate 
16250Sstevel@tonic-gate 	dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n",
16267240Srh87107 	    (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode)));
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
16290Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
16300Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
16310Sstevel@tonic-gate 	} else {
16320Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
16330Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
16340Sstevel@tonic-gate 	}
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) {
16370Sstevel@tonic-gate 		error = EINVAL;
16380Sstevel@tonic-gate 		eprintsoline(so, error);
16390Sstevel@tonic-gate 		goto done;
16400Sstevel@tonic-gate 	}
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
16430Sstevel@tonic-gate 	/*
16440Sstevel@tonic-gate 	 * Flush the write side (unless this is a listener)
16450Sstevel@tonic-gate 	 * and then send down a T_DISCON_REQ.
16460Sstevel@tonic-gate 	 * (Don't flush on listener since it could flush {O_}T_CONN_RES
16470Sstevel@tonic-gate 	 * and other messages.)
16480Sstevel@tonic-gate 	 */
16490Sstevel@tonic-gate 	if (!(so->so_state & SS_ACCEPTCONN))
16500Sstevel@tonic-gate 		(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW);
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	discon_req.PRIM_type = T_DISCON_REQ;
16530Sstevel@tonic-gate 	discon_req.SEQ_number = seqno;
16540Sstevel@tonic-gate 	mp = soallocproto1(&discon_req, sizeof (discon_req),
16558778SErik.Nordmark@Sun.COM 	    0, _ALLOC_SLEEP, CRED());
16560Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
16575240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
16580Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
16590Sstevel@tonic-gate 	if (error) {
16600Sstevel@tonic-gate 		eprintsoline(so, error);
16610Sstevel@tonic-gate 		goto done;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	error = sowaitokack(so, T_DISCON_REQ);
16650Sstevel@tonic-gate 	if (error) {
16660Sstevel@tonic-gate 		eprintsoline(so, error);
16670Sstevel@tonic-gate 		goto done;
16680Sstevel@tonic-gate 	}
16690Sstevel@tonic-gate 	/*
16700Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
16710Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the disconnect
16720Sstevel@tonic-gate 	 * is allowed to complete. However, it is not possible to
16730Sstevel@tonic-gate 	 * assert that SS_ISCONNECTED|SS_ISCONNECTING are set.
16740Sstevel@tonic-gate 	 */
16758348SEric.Yu@Sun.COM 	so->so_state &= ~(SS_ISCONNECTED|SS_ISCONNECTING);
16768348SEric.Yu@Sun.COM 	SOTOTPI(so)->sti_laddr_valid = 0;
16778348SEric.Yu@Sun.COM 	SOTOTPI(so)->sti_faddr_valid = 0;
16780Sstevel@tonic-gate done:
16790Sstevel@tonic-gate 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
16800Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
16810Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
16820Sstevel@tonic-gate 	} else {
16830Sstevel@tonic-gate 		/* If the caller held the lock don't release it here */
16840Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
16850Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
16860Sstevel@tonic-gate 	}
16870Sstevel@tonic-gate 	return (error);
16880Sstevel@tonic-gate }
16890Sstevel@tonic-gate 
16908348SEric.Yu@Sun.COM /* ARGSUSED */
16910Sstevel@tonic-gate int
16928348SEric.Yu@Sun.COM sotpi_accept(struct sonode *so, int fflag, struct cred *cr,
16938348SEric.Yu@Sun.COM     struct sonode **nsop)
16940Sstevel@tonic-gate {
16950Sstevel@tonic-gate 	struct T_conn_ind	*conn_ind;
16960Sstevel@tonic-gate 	struct T_conn_res	*conn_res;
16970Sstevel@tonic-gate 	int			error = 0;
16984379Sja97890 	mblk_t			*mp, *ctxmp, *ack_mp;
16990Sstevel@tonic-gate 	struct sonode		*nso;
17000Sstevel@tonic-gate 	vnode_t			*nvp;
17010Sstevel@tonic-gate 	void			*src;
17020Sstevel@tonic-gate 	t_uscalar_t		srclen;
17030Sstevel@tonic-gate 	void			*opt;
17040Sstevel@tonic-gate 	t_uscalar_t		optlen;
17050Sstevel@tonic-gate 	t_scalar_t		PRIM_type;
17060Sstevel@tonic-gate 	t_scalar_t		SEQ_number;
17074379Sja97890 	size_t			sinlen;
17088348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
17098348SEric.Yu@Sun.COM 	sotpi_info_t		*nsti;
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n",
17127240Srh87107 	    (void *)so, fflag, (void *)nsop,
17137240Srh87107 	    pr_state(so->so_state, so->so_mode)));
17140Sstevel@tonic-gate 
17150Sstevel@tonic-gate 	/*
17160Sstevel@tonic-gate 	 * Defer single-threading the accepting socket until
17170Sstevel@tonic-gate 	 * the T_CONN_IND has been received and parsed and the
17180Sstevel@tonic-gate 	 * new sonode has been opened.
17190Sstevel@tonic-gate 	 */
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 	/* Check that we are not already connected */
17220Sstevel@tonic-gate 	if ((so->so_state & SS_ACCEPTCONN) == 0)
17230Sstevel@tonic-gate 		goto conn_bad;
17240Sstevel@tonic-gate again:
17250Sstevel@tonic-gate 	if ((error = sowaitconnind(so, fflag, &mp)) != 0)
17260Sstevel@tonic-gate 		goto e_bad;
17270Sstevel@tonic-gate 
17288348SEric.Yu@Sun.COM 	ASSERT(mp != NULL);
17290Sstevel@tonic-gate 	conn_ind = (struct T_conn_ind *)mp->b_rptr;
1730898Skais 	ctxmp = mp->b_cont;
1731898Skais 
17320Sstevel@tonic-gate 	/*
17330Sstevel@tonic-gate 	 * Save SEQ_number for error paths.
17340Sstevel@tonic-gate 	 */
17350Sstevel@tonic-gate 	SEQ_number = conn_ind->SEQ_number;
17360Sstevel@tonic-gate 
17370Sstevel@tonic-gate 	srclen = conn_ind->SRC_length;
17380Sstevel@tonic-gate 	src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1);
17390Sstevel@tonic-gate 	if (src == NULL) {
17400Sstevel@tonic-gate 		error = EPROTO;
17410Sstevel@tonic-gate 		freemsg(mp);
17420Sstevel@tonic-gate 		eprintsoline(so, error);
17430Sstevel@tonic-gate 		goto disconnect_unlocked;
17440Sstevel@tonic-gate 	}
17450Sstevel@tonic-gate 	optlen = conn_ind->OPT_length;
17460Sstevel@tonic-gate 	switch (so->so_family) {
17470Sstevel@tonic-gate 	case AF_INET:
17480Sstevel@tonic-gate 	case AF_INET6:
17498348SEric.Yu@Sun.COM 		if ((optlen == sizeof (intptr_t)) && (sti->sti_direct != 0)) {
17500Sstevel@tonic-gate 			bcopy(mp->b_rptr + conn_ind->OPT_offset,
17510Sstevel@tonic-gate 			    &opt, conn_ind->OPT_length);
17520Sstevel@tonic-gate 		} else {
17530Sstevel@tonic-gate 			/*
17540Sstevel@tonic-gate 			 * The transport (in this case TCP) hasn't sent up
17550Sstevel@tonic-gate 			 * a pointer to an instance for the accept fast-path.
17560Sstevel@tonic-gate 			 * Disable fast-path completely because the call to
17570Sstevel@tonic-gate 			 * sotpi_create() below would otherwise create an
17580Sstevel@tonic-gate 			 * incomplete TCP instance, which would lead to
17590Sstevel@tonic-gate 			 * problems when sockfs sends a normal T_CONN_RES
17600Sstevel@tonic-gate 			 * message down the new stream.
17610Sstevel@tonic-gate 			 */
17628348SEric.Yu@Sun.COM 			if (sti->sti_direct) {
1763741Smasputra 				int rval;
1764741Smasputra 				/*
1765741Smasputra 				 * For consistency we inform tcp to disable
1766741Smasputra 				 * direct interface on the listener, though
1767741Smasputra 				 * we can certainly live without doing this
1768741Smasputra 				 * because no data will ever travel upstream
1769741Smasputra 				 * on the listening socket.
1770741Smasputra 				 */
17718348SEric.Yu@Sun.COM 				sti->sti_direct = 0;
1772741Smasputra 				(void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK,
17738778SErik.Nordmark@Sun.COM 				    0, 0, K_TO_K, cr, &rval);
1774741Smasputra 			}
17750Sstevel@tonic-gate 			opt = NULL;
17760Sstevel@tonic-gate 			optlen = 0;
17770Sstevel@tonic-gate 		}
17780Sstevel@tonic-gate 		break;
17790Sstevel@tonic-gate 	case AF_UNIX:
17800Sstevel@tonic-gate 	default:
17810Sstevel@tonic-gate 		if (optlen != 0) {
17820Sstevel@tonic-gate 			opt = sogetoff(mp, conn_ind->OPT_offset, optlen,
17830Sstevel@tonic-gate 			    __TPI_ALIGN_SIZE);
17840Sstevel@tonic-gate 			if (opt == NULL) {
17850Sstevel@tonic-gate 				error = EPROTO;
17860Sstevel@tonic-gate 				freemsg(mp);
17870Sstevel@tonic-gate 				eprintsoline(so, error);
17880Sstevel@tonic-gate 				goto disconnect_unlocked;
17890Sstevel@tonic-gate 			}
17900Sstevel@tonic-gate 		}
17910Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
17928348SEric.Yu@Sun.COM 			if (!sti->sti_faddr_noxlate) {
17930Sstevel@tonic-gate 				src = NULL;
17940Sstevel@tonic-gate 				srclen = 0;
17950Sstevel@tonic-gate 			}
17960Sstevel@tonic-gate 			/* Extract src address from options */
17970Sstevel@tonic-gate 			if (optlen != 0)
17980Sstevel@tonic-gate 				so_getopt_srcaddr(opt, optlen, &src, &srclen);
17990Sstevel@tonic-gate 		}
18000Sstevel@tonic-gate 		break;
18010Sstevel@tonic-gate 	}
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate 	/*
18040Sstevel@tonic-gate 	 * Create the new socket.
18050Sstevel@tonic-gate 	 */
18068348SEric.Yu@Sun.COM 	nso = socket_newconn(so, NULL, NULL, SOCKET_SLEEP, &error);
18070Sstevel@tonic-gate 	if (nso == NULL) {
18080Sstevel@tonic-gate 		ASSERT(error != 0);
18090Sstevel@tonic-gate 		/*
18100Sstevel@tonic-gate 		 * Accept can not fail with ENOBUFS. sotpi_create
18110Sstevel@tonic-gate 		 * sleeps waiting for memory until a signal is caught
18120Sstevel@tonic-gate 		 * so return EINTR.
18130Sstevel@tonic-gate 		 */
18140Sstevel@tonic-gate 		freemsg(mp);
18150Sstevel@tonic-gate 		if (error == ENOBUFS)
18160Sstevel@tonic-gate 			error = EINTR;
18170Sstevel@tonic-gate 		goto e_disc_unl;
18180Sstevel@tonic-gate 	}
18190Sstevel@tonic-gate 	nvp = SOTOV(nso);
18208348SEric.Yu@Sun.COM 	nsti = SOTOTPI(nso);
18210Sstevel@tonic-gate 
1822898Skais 	/*
1823898Skais 	 * If the transport sent up an SSL connection context, then attach
1824898Skais 	 * it the new socket, and set the (sd_wputdatafunc)() and
1825898Skais 	 * (sd_rputdatafunc)() stream head hooks to intercept and process
1826898Skais 	 * SSL records.
1827898Skais 	 */
1828898Skais 	if (ctxmp != NULL) {
1829898Skais 		/*
1830898Skais 		 * This kssl_ctx_t is already held for us by the transport.
1831898Skais 		 * So, we don't need to do a kssl_hold_ctx() here.
1832898Skais 		 */
18338348SEric.Yu@Sun.COM 		nsti->sti_kssl_ctx = *((kssl_ctx_t *)ctxmp->b_rptr);
1834898Skais 		freemsg(ctxmp);
1835898Skais 		mp->b_cont = NULL;
1836898Skais 		strsetrwputdatahooks(nvp, strsock_kssl_input,
1837898Skais 		    strsock_kssl_output);
1838898Skais 	}
18390Sstevel@tonic-gate #ifdef DEBUG
18400Sstevel@tonic-gate 	/*
18410Sstevel@tonic-gate 	 * SO_DEBUG is used to trigger the dprint* and eprint* macros thus
18420Sstevel@tonic-gate 	 * it's inherited early to allow debugging of the accept code itself.
18430Sstevel@tonic-gate 	 */
18440Sstevel@tonic-gate 	nso->so_options |= so->so_options & SO_DEBUG;
18450Sstevel@tonic-gate #endif /* DEBUG */
18460Sstevel@tonic-gate 
18470Sstevel@tonic-gate 	/*
18480Sstevel@tonic-gate 	 * Save the SRC address from the T_CONN_IND
18490Sstevel@tonic-gate 	 * for getpeername to work on AF_UNIX and on transports that do not
18500Sstevel@tonic-gate 	 * support TI_GETPEERNAME.
18510Sstevel@tonic-gate 	 *
18520Sstevel@tonic-gate 	 * NOTE: AF_UNIX NUL termination is ensured by the sender's
18530Sstevel@tonic-gate 	 * copyin_name().
18540Sstevel@tonic-gate 	 */
18558348SEric.Yu@Sun.COM 	if (srclen > (t_uscalar_t)nsti->sti_faddr_maxlen) {
18560Sstevel@tonic-gate 		error = EINVAL;
18570Sstevel@tonic-gate 		freemsg(mp);
18580Sstevel@tonic-gate 		eprintsoline(so, error);
18590Sstevel@tonic-gate 		goto disconnect_vp_unlocked;
18600Sstevel@tonic-gate 	}
18618348SEric.Yu@Sun.COM 	nsti->sti_faddr_len = (socklen_t)srclen;
18628348SEric.Yu@Sun.COM 	ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
18638348SEric.Yu@Sun.COM 	bcopy(src, nsti->sti_faddr_sa, srclen);
18648348SEric.Yu@Sun.COM 	nsti->sti_faddr_valid = 1;
18650Sstevel@tonic-gate 
18668778SErik.Nordmark@Sun.COM 	/*
18678778SErik.Nordmark@Sun.COM 	 * Record so_peercred and so_cpid from a cred in the T_CONN_IND.
18688778SErik.Nordmark@Sun.COM 	 */
18690Sstevel@tonic-gate 	if ((DB_REF(mp) > 1) || MBLKSIZE(mp) <
18700Sstevel@tonic-gate 	    (sizeof (struct T_conn_res) + sizeof (intptr_t))) {
18718778SErik.Nordmark@Sun.COM 		cred_t	*cr;
18728778SErik.Nordmark@Sun.COM 		pid_t	cpid;
18738778SErik.Nordmark@Sun.COM 
18748778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
18758778SErik.Nordmark@Sun.COM 		if (cr != NULL) {
18760Sstevel@tonic-gate 			crhold(cr);
18770Sstevel@tonic-gate 			nso->so_peercred = cr;
18788778SErik.Nordmark@Sun.COM 			nso->so_cpid = cpid;
18790Sstevel@tonic-gate 		}
18800Sstevel@tonic-gate 		freemsg(mp);
18810Sstevel@tonic-gate 
18820Sstevel@tonic-gate 		mp = soallocproto1(NULL, sizeof (struct T_conn_res) +
18838965SAnders.Persson@Sun.COM 		    sizeof (intptr_t), 0, _ALLOC_INTR, cr);
18840Sstevel@tonic-gate 		if (mp == NULL) {
18850Sstevel@tonic-gate 			/*
18860Sstevel@tonic-gate 			 * Accept can not fail with ENOBUFS.
18870Sstevel@tonic-gate 			 * A signal was caught so return EINTR.
18880Sstevel@tonic-gate 			 */
18890Sstevel@tonic-gate 			error = EINTR;
18900Sstevel@tonic-gate 			eprintsoline(so, error);
18910Sstevel@tonic-gate 			goto disconnect_vp_unlocked;
18920Sstevel@tonic-gate 		}
18930Sstevel@tonic-gate 		conn_res = (struct T_conn_res *)mp->b_rptr;
18940Sstevel@tonic-gate 	} else {
18958778SErik.Nordmark@Sun.COM 		/*
18968778SErik.Nordmark@Sun.COM 		 * For efficency reasons we use msg_extractcred; no crhold
18978778SErik.Nordmark@Sun.COM 		 * needed since db_credp is cleared (i.e., we move the cred
18988778SErik.Nordmark@Sun.COM 		 * from the message to so_peercred.
18998778SErik.Nordmark@Sun.COM 		 */
19008778SErik.Nordmark@Sun.COM 		nso->so_peercred = msg_extractcred(mp, &nso->so_cpid);
19010Sstevel@tonic-gate 
19020Sstevel@tonic-gate 		mp->b_rptr = DB_BASE(mp);
19030Sstevel@tonic-gate 		conn_res = (struct T_conn_res *)mp->b_rptr;
19040Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res);
19058965SAnders.Persson@Sun.COM 
19068965SAnders.Persson@Sun.COM 		mblk_setcred(mp, cr, curproc->p_pid);
19070Sstevel@tonic-gate 	}
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	/*
19100Sstevel@tonic-gate 	 * New socket must be bound at least in sockfs and, except for AF_INET,
19110Sstevel@tonic-gate 	 * (or AF_INET6) it also has to be bound in the transport provider.
19124379Sja97890 	 * We set the local address in the sonode from the T_OK_ACK of the
19134379Sja97890 	 * T_CONN_RES. For this reason the address we bind to here isn't
19144379Sja97890 	 * important.
19150Sstevel@tonic-gate 	 */
19160Sstevel@tonic-gate 	if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
19170Sstevel@tonic-gate 	    /*CONSTCOND*/
19180Sstevel@tonic-gate 	    nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) {
19190Sstevel@tonic-gate 		/*
19200Sstevel@tonic-gate 		 * Optimization for AF_INET{,6} transports
19210Sstevel@tonic-gate 		 * that can handle a T_CONN_RES without being bound.
19220Sstevel@tonic-gate 		 */
19230Sstevel@tonic-gate 		mutex_enter(&nso->so_lock);
19240Sstevel@tonic-gate 		so_automatic_bind(nso);
19250Sstevel@tonic-gate 		mutex_exit(&nso->so_lock);
19260Sstevel@tonic-gate 	} else {
19270Sstevel@tonic-gate 		/* Perform NULL bind with the transport provider. */
19288348SEric.Yu@Sun.COM 		if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC,
19298348SEric.Yu@Sun.COM 		    cr)) != 0) {
19300Sstevel@tonic-gate 			ASSERT(error != ENOBUFS);
19310Sstevel@tonic-gate 			freemsg(mp);
19320Sstevel@tonic-gate 			eprintsoline(nso, error);
19330Sstevel@tonic-gate 			goto disconnect_vp_unlocked;
19340Sstevel@tonic-gate 		}
19350Sstevel@tonic-gate 	}
19360Sstevel@tonic-gate 
19370Sstevel@tonic-gate 	/*
19380Sstevel@tonic-gate 	 * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES
19390Sstevel@tonic-gate 	 * so that any data arriving on the new socket will cause the
19400Sstevel@tonic-gate 	 * appropriate signals to be delivered for the new socket.
19410Sstevel@tonic-gate 	 *
19420Sstevel@tonic-gate 	 * No other thread (except strsock_proto and strsock_misc)
19430Sstevel@tonic-gate 	 * can access the new socket thus we relax the locking.
19440Sstevel@tonic-gate 	 */
19450Sstevel@tonic-gate 	nso->so_pgrp = so->so_pgrp;
19468348SEric.Yu@Sun.COM 	nso->so_state |= so->so_state & SS_ASYNC;
19478348SEric.Yu@Sun.COM 	nsti->sti_faddr_noxlate = sti->sti_faddr_noxlate;
19480Sstevel@tonic-gate 
19490Sstevel@tonic-gate 	if (nso->so_pgrp != 0) {
19508778SErik.Nordmark@Sun.COM 		if ((error = so_set_events(nso, nvp, cr)) != 0) {
19510Sstevel@tonic-gate 			eprintsoline(nso, error);
19520Sstevel@tonic-gate 			error = 0;
19530Sstevel@tonic-gate 			nso->so_pgrp = 0;
19540Sstevel@tonic-gate 		}
19550Sstevel@tonic-gate 	}
19560Sstevel@tonic-gate 
19570Sstevel@tonic-gate 	/*
19580Sstevel@tonic-gate 	 * Make note of the socket level options. TCP and IP level options
19590Sstevel@tonic-gate 	 * are already inherited. We could do all this after accept is
19600Sstevel@tonic-gate 	 * successful but doing it here simplifies code and no harm done
19610Sstevel@tonic-gate 	 * for error case.
19620Sstevel@tonic-gate 	 */
19630Sstevel@tonic-gate 	nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE|
19640Sstevel@tonic-gate 	    SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
19650Sstevel@tonic-gate 	    SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
19660Sstevel@tonic-gate 	nso->so_sndbuf = so->so_sndbuf;
19670Sstevel@tonic-gate 	nso->so_rcvbuf = so->so_rcvbuf;
19680Sstevel@tonic-gate 	if (nso->so_options & SO_LINGER)
19690Sstevel@tonic-gate 		nso->so_linger = so->so_linger;
19700Sstevel@tonic-gate 
19718348SEric.Yu@Sun.COM 	/*
19728348SEric.Yu@Sun.COM 	 * Note that the following sti_direct code path should be
19738348SEric.Yu@Sun.COM 	 * removed once we are confident that the direct sockets
19748348SEric.Yu@Sun.COM 	 * do not result in any degradation.
19758348SEric.Yu@Sun.COM 	 */
19768348SEric.Yu@Sun.COM 	if (sti->sti_direct) {
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate 		ASSERT(opt != NULL);
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate 		conn_res->OPT_length = optlen;
19810Sstevel@tonic-gate 		conn_res->OPT_offset = MBLKL(mp);
19820Sstevel@tonic-gate 		bcopy(&opt, mp->b_wptr, optlen);
19830Sstevel@tonic-gate 		mp->b_wptr += optlen;
19840Sstevel@tonic-gate 		conn_res->PRIM_type = T_CONN_RES;
19850Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = 0;
19860Sstevel@tonic-gate 		PRIM_type = T_CONN_RES;
19870Sstevel@tonic-gate 
19880Sstevel@tonic-gate 		/* Send down the T_CONN_RES on acceptor STREAM */
19890Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(nso), mp, NULL,
19900Sstevel@tonic-gate 		    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
19910Sstevel@tonic-gate 		if (error) {
19920Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
19930Sstevel@tonic-gate 			so_lock_single(so);
19940Sstevel@tonic-gate 			eprintsoline(so, error);
19950Sstevel@tonic-gate 			goto disconnect_vp;
19960Sstevel@tonic-gate 		}
19970Sstevel@tonic-gate 		mutex_enter(&nso->so_lock);
19980Sstevel@tonic-gate 		error = sowaitprim(nso, T_CONN_RES, T_OK_ACK,
19990Sstevel@tonic-gate 		    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
20000Sstevel@tonic-gate 		if (error) {
20010Sstevel@tonic-gate 			mutex_exit(&nso->so_lock);
20020Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
20030Sstevel@tonic-gate 			so_lock_single(so);
20040Sstevel@tonic-gate 			eprintsoline(so, error);
20050Sstevel@tonic-gate 			goto disconnect_vp;
20060Sstevel@tonic-gate 		}
20070Sstevel@tonic-gate 		if (nso->so_family == AF_INET) {
20080Sstevel@tonic-gate 			sin_t *sin;
20090Sstevel@tonic-gate 
20100Sstevel@tonic-gate 			sin = (sin_t *)(ack_mp->b_rptr +
20110Sstevel@tonic-gate 			    sizeof (struct T_ok_ack));
20128348SEric.Yu@Sun.COM 			bcopy(sin, nsti->sti_laddr_sa, sizeof (sin_t));
20138348SEric.Yu@Sun.COM 			nsti->sti_laddr_len = sizeof (sin_t);
20140Sstevel@tonic-gate 		} else {
20150Sstevel@tonic-gate 			sin6_t *sin6;
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 			sin6 = (sin6_t *)(ack_mp->b_rptr +
20180Sstevel@tonic-gate 			    sizeof (struct T_ok_ack));
20198348SEric.Yu@Sun.COM 			bcopy(sin6, nsti->sti_laddr_sa, sizeof (sin6_t));
20208348SEric.Yu@Sun.COM 			nsti->sti_laddr_len = sizeof (sin6_t);
20210Sstevel@tonic-gate 		}
20220Sstevel@tonic-gate 		freemsg(ack_mp);
20230Sstevel@tonic-gate 
20248348SEric.Yu@Sun.COM 		nso->so_state |= SS_ISCONNECTED;
20258348SEric.Yu@Sun.COM 		nso->so_proto_handle = (sock_lower_handle_t)opt;
20268348SEric.Yu@Sun.COM 		nsti->sti_laddr_valid = 1;
20278348SEric.Yu@Sun.COM 
20288348SEric.Yu@Sun.COM 		if (sti->sti_nl7c_flags & NL7C_ENABLED) {
20290Sstevel@tonic-gate 			/*
20301974Sbrutus 			 * A NL7C marked listen()er so the new socket
20311974Sbrutus 			 * inherits the listen()er's NL7C state, except
20321974Sbrutus 			 * for NL7C_POLLIN.
20330Sstevel@tonic-gate 			 *
20341974Sbrutus 			 * Only call NL7C to process the new socket if
20351974Sbrutus 			 * the listen socket allows blocking i/o.
20360Sstevel@tonic-gate 			 */
20378348SEric.Yu@Sun.COM 			nsti->sti_nl7c_flags =
20388348SEric.Yu@Sun.COM 			    sti->sti_nl7c_flags & (~NL7C_POLLIN);
20391974Sbrutus 			if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) {
20401974Sbrutus 				/*
20411974Sbrutus 				 * Nonblocking accept() just make it
20421974Sbrutus 				 * persist to defer processing to the
20431974Sbrutus 				 * read-side syscall (e.g. read).
20441974Sbrutus 				 */
20458348SEric.Yu@Sun.COM 				nsti->sti_nl7c_flags |= NL7C_SOPERSIST;
20461974Sbrutus 			} else if (nl7c_process(nso, B_FALSE)) {
20470Sstevel@tonic-gate 				/*
20480Sstevel@tonic-gate 				 * NL7C has completed processing on the
20490Sstevel@tonic-gate 				 * socket, close the socket and back to
20500Sstevel@tonic-gate 				 * the top to await the next T_CONN_IND.
20510Sstevel@tonic-gate 				 */
20520Sstevel@tonic-gate 				mutex_exit(&nso->so_lock);
20530Sstevel@tonic-gate 				(void) VOP_CLOSE(nvp, 0, 1, (offset_t)0,
20548778SErik.Nordmark@Sun.COM 				    cr, NULL);
20550Sstevel@tonic-gate 				VN_RELE(nvp);
20560Sstevel@tonic-gate 				goto again;
20570Sstevel@tonic-gate 			}
20580Sstevel@tonic-gate 			/* Pass the new socket out */
20590Sstevel@tonic-gate 		}
20600Sstevel@tonic-gate 
20610Sstevel@tonic-gate 		mutex_exit(&nso->so_lock);
20620Sstevel@tonic-gate 
20630Sstevel@tonic-gate 		/*
20642811Sja97890 		 * It's possible, through the use of autopush for example,
20658348SEric.Yu@Sun.COM 		 * that the acceptor stream may not support sti_direct
20668348SEric.Yu@Sun.COM 		 * semantics. If the new socket does not support sti_direct
20672811Sja97890 		 * we issue a _SIOCSOCKFALLBACK to inform the transport
20682811Sja97890 		 * as we would in the I_PUSH case.
20692811Sja97890 		 */
20708348SEric.Yu@Sun.COM 		if (nsti->sti_direct == 0) {
20712811Sja97890 			int	rval;
20722811Sja97890 
20732811Sja97890 			if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
20748778SErik.Nordmark@Sun.COM 			    0, 0, K_TO_K, cr, &rval)) != 0) {
20752811Sja97890 				mutex_enter(&so->so_lock);
20762811Sja97890 				so_lock_single(so);
20772811Sja97890 				eprintsoline(so, error);
20782811Sja97890 				goto disconnect_vp;
20792811Sja97890 			}
20802811Sja97890 		}
20812811Sja97890 
20822811Sja97890 		/*
20830Sstevel@tonic-gate 		 * Pass out new socket.
20840Sstevel@tonic-gate 		 */
20850Sstevel@tonic-gate 		if (nsop != NULL)
20860Sstevel@tonic-gate 			*nsop = nso;
20870Sstevel@tonic-gate 
20880Sstevel@tonic-gate 		return (0);
20890Sstevel@tonic-gate 	}
20900Sstevel@tonic-gate 
20910Sstevel@tonic-gate 	/*
20920Sstevel@tonic-gate 	 * This is the non-performance case for sockets (e.g. AF_UNIX sockets)
20930Sstevel@tonic-gate 	 * which don't support the FireEngine accept fast-path. It is also
20940Sstevel@tonic-gate 	 * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd
20950Sstevel@tonic-gate 	 * again. Neither sockfs nor TCP attempt to find out if some other
20960Sstevel@tonic-gate 	 * random module has been inserted in between (in which case we
20970Sstevel@tonic-gate 	 * should follow TLI accept behaviour). We blindly assume the worst
20980Sstevel@tonic-gate 	 * case and revert back to old behaviour i.e. TCP will not send us
20990Sstevel@tonic-gate 	 * any option (eager) and the accept should happen on the listener
21000Sstevel@tonic-gate 	 * queue. Any queued T_conn_ind have already got their options removed
21010Sstevel@tonic-gate 	 * by so_sock2_stream() when "sockmod" was I_POP'd.
21020Sstevel@tonic-gate 	 */
21030Sstevel@tonic-gate 	/*
21040Sstevel@tonic-gate 	 * Fill in the {O_}T_CONN_RES before getting SOLOCKED.
21050Sstevel@tonic-gate 	 */
21060Sstevel@tonic-gate 	if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) {
21070Sstevel@tonic-gate #ifdef	_ILP32
21080Sstevel@tonic-gate 		queue_t	*q;
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 		/*
21110Sstevel@tonic-gate 		 * Find read queue in driver
21120Sstevel@tonic-gate 		 * Can safely do this since we "own" nso/nvp.
21130Sstevel@tonic-gate 		 */
21140Sstevel@tonic-gate 		q = strvp2wq(nvp)->q_next;
21150Sstevel@tonic-gate 		while (SAMESTR(q))
21160Sstevel@tonic-gate 			q = q->q_next;
21170Sstevel@tonic-gate 		q = RD(q);
21180Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = (t_uscalar_t)q;
21190Sstevel@tonic-gate #else
21200Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev);
21210Sstevel@tonic-gate #endif	/* _ILP32 */
21220Sstevel@tonic-gate 		conn_res->PRIM_type = O_T_CONN_RES;
21230Sstevel@tonic-gate 		PRIM_type = O_T_CONN_RES;
21240Sstevel@tonic-gate 	} else {
21258348SEric.Yu@Sun.COM 		conn_res->ACCEPTOR_id = nsti->sti_acceptor_id;
21260Sstevel@tonic-gate 		conn_res->PRIM_type = T_CONN_RES;
21270Sstevel@tonic-gate 		PRIM_type = T_CONN_RES;
21280Sstevel@tonic-gate 	}
21290Sstevel@tonic-gate 	conn_res->SEQ_number = SEQ_number;
21300Sstevel@tonic-gate 	conn_res->OPT_length = 0;
21310Sstevel@tonic-gate 	conn_res->OPT_offset = 0;
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
21340Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
21350Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL,
21380Sstevel@tonic-gate 	    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
21390Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
21400Sstevel@tonic-gate 	if (error) {
21410Sstevel@tonic-gate 		eprintsoline(so, error);
21420Sstevel@tonic-gate 		goto disconnect_vp;
21430Sstevel@tonic-gate 	}
21444379Sja97890 	error = sowaitprim(so, PRIM_type, T_OK_ACK,
21454379Sja97890 	    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
21460Sstevel@tonic-gate 	if (error) {
21470Sstevel@tonic-gate 		eprintsoline(so, error);
21480Sstevel@tonic-gate 		goto disconnect_vp;
21490Sstevel@tonic-gate 	}
21504379Sja97890 	/*
21514379Sja97890 	 * If there is a sin/sin6 appended onto the T_OK_ACK use
21524379Sja97890 	 * that to set the local address. If this is not present
21534379Sja97890 	 * then we zero out the address and don't set the
21548348SEric.Yu@Sun.COM 	 * sti_laddr_valid bit. For AF_UNIX endpoints we copy over
21554678Sja97890 	 * the pathname from the listening socket.
21564379Sja97890 	 */
21574379Sja97890 	sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
21584379Sja97890 	if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) &&
21594379Sja97890 	    MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
21604379Sja97890 		ack_mp->b_rptr += sizeof (struct T_ok_ack);
21618348SEric.Yu@Sun.COM 		bcopy(ack_mp->b_rptr, nsti->sti_laddr_sa, sinlen);
21628348SEric.Yu@Sun.COM 		nsti->sti_laddr_len = sinlen;
21638348SEric.Yu@Sun.COM 		nsti->sti_laddr_valid = 1;
21644678Sja97890 	} else if (nso->so_family == AF_UNIX) {
21654678Sja97890 		ASSERT(so->so_family == AF_UNIX);
21668348SEric.Yu@Sun.COM 		nsti->sti_laddr_len = sti->sti_laddr_len;
21678348SEric.Yu@Sun.COM 		ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
21688348SEric.Yu@Sun.COM 		bcopy(sti->sti_laddr_sa, nsti->sti_laddr_sa,
21698348SEric.Yu@Sun.COM 		    nsti->sti_laddr_len);
21708348SEric.Yu@Sun.COM 		nsti->sti_laddr_valid = 1;
21714379Sja97890 	} else {
21728348SEric.Yu@Sun.COM 		nsti->sti_laddr_len = sti->sti_laddr_len;
21738348SEric.Yu@Sun.COM 		ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
21748348SEric.Yu@Sun.COM 		bzero(nsti->sti_laddr_sa, nsti->sti_addr_size);
21758348SEric.Yu@Sun.COM 		nsti->sti_laddr_sa->sa_family = nso->so_family;
21764379Sja97890 	}
21774379Sja97890 	freemsg(ack_mp);
21784379Sja97890 
21790Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
21800Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 	nso->so_state |= SS_ISCONNECTED;
21830Sstevel@tonic-gate 
21840Sstevel@tonic-gate 	/*
21850Sstevel@tonic-gate 	 * Pass out new socket.
21860Sstevel@tonic-gate 	 */
21870Sstevel@tonic-gate 	if (nsop != NULL)
21880Sstevel@tonic-gate 		*nsop = nso;
21890Sstevel@tonic-gate 
21900Sstevel@tonic-gate 	return (0);
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 
21930Sstevel@tonic-gate eproto_disc_unl:
21940Sstevel@tonic-gate 	error = EPROTO;
21950Sstevel@tonic-gate e_disc_unl:
21960Sstevel@tonic-gate 	eprintsoline(so, error);
21970Sstevel@tonic-gate 	goto disconnect_unlocked;
21980Sstevel@tonic-gate 
21990Sstevel@tonic-gate pr_disc_vp_unl:
22000Sstevel@tonic-gate 	eprintsoline(so, error);
22010Sstevel@tonic-gate disconnect_vp_unlocked:
22028778SErik.Nordmark@Sun.COM 	(void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
22030Sstevel@tonic-gate 	VN_RELE(nvp);
22040Sstevel@tonic-gate disconnect_unlocked:
22050Sstevel@tonic-gate 	(void) sodisconnect(so, SEQ_number, 0);
22060Sstevel@tonic-gate 	return (error);
22070Sstevel@tonic-gate 
22080Sstevel@tonic-gate pr_disc_vp:
22090Sstevel@tonic-gate 	eprintsoline(so, error);
22100Sstevel@tonic-gate disconnect_vp:
22110Sstevel@tonic-gate 	(void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
22120Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
22130Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
22148778SErik.Nordmark@Sun.COM 	(void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
22150Sstevel@tonic-gate 	VN_RELE(nvp);
22160Sstevel@tonic-gate 	return (error);
22170Sstevel@tonic-gate 
22180Sstevel@tonic-gate conn_bad:	/* Note: SunOS 4/BSD unconditionally returns EINVAL here */
22190Sstevel@tonic-gate 	error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
22200Sstevel@tonic-gate 	    ? EOPNOTSUPP : EINVAL;
22210Sstevel@tonic-gate e_bad:
22220Sstevel@tonic-gate 	eprintsoline(so, error);
22230Sstevel@tonic-gate 	return (error);
22240Sstevel@tonic-gate }
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate /*
22270Sstevel@tonic-gate  * connect a socket.
22280Sstevel@tonic-gate  *
22290Sstevel@tonic-gate  * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
22300Sstevel@tonic-gate  * unconnect (by specifying a null address).
22310Sstevel@tonic-gate  */
22320Sstevel@tonic-gate int
22330Sstevel@tonic-gate sotpi_connect(struct sonode *so,
22340Sstevel@tonic-gate 	const struct sockaddr *name,
22350Sstevel@tonic-gate 	socklen_t namelen,
22360Sstevel@tonic-gate 	int fflag,
22378348SEric.Yu@Sun.COM 	int flags,
22388348SEric.Yu@Sun.COM 	struct cred *cr)
22390Sstevel@tonic-gate {
22400Sstevel@tonic-gate 	struct T_conn_req	conn_req;
22410Sstevel@tonic-gate 	int			error = 0;
22420Sstevel@tonic-gate 	mblk_t			*mp;
22430Sstevel@tonic-gate 	void			*src;
22440Sstevel@tonic-gate 	socklen_t		srclen;
22450Sstevel@tonic-gate 	void			*addr;
22460Sstevel@tonic-gate 	socklen_t		addrlen;
22470Sstevel@tonic-gate 	boolean_t		need_unlock;
22488348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
22490Sstevel@tonic-gate 
22500Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
22517240Srh87107 	    (void *)so, (void *)name, namelen, fflag, flags,
22525240Snordmark 	    pr_state(so->so_state, so->so_mode)));
22530Sstevel@tonic-gate 
22540Sstevel@tonic-gate 	/*
22550Sstevel@tonic-gate 	 * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
22560Sstevel@tonic-gate 	 * avoid sleeping for memory with SOLOCKED held.
22578348SEric.Yu@Sun.COM 	 * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen
22580Sstevel@tonic-gate 	 * + sizeof (struct T_opthdr).
22590Sstevel@tonic-gate 	 * (the AF_UNIX so_ux_addr_xlate() does not make the address
22608348SEric.Yu@Sun.COM 	 * exceed sti_faddr_maxlen).
22610Sstevel@tonic-gate 	 */
22620Sstevel@tonic-gate 	mp = soallocproto(sizeof (struct T_conn_req) +
22638778SErik.Nordmark@Sun.COM 	    2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR,
22648778SErik.Nordmark@Sun.COM 	    cr);
22650Sstevel@tonic-gate 	if (mp == NULL) {
22660Sstevel@tonic-gate 		/*
22670Sstevel@tonic-gate 		 * Connect can not fail with ENOBUFS. A signal was
22680Sstevel@tonic-gate 		 * caught so return EINTR.
22690Sstevel@tonic-gate 		 */
22700Sstevel@tonic-gate 		error = EINTR;
22710Sstevel@tonic-gate 		eprintsoline(so, error);
22720Sstevel@tonic-gate 		return (error);
22730Sstevel@tonic-gate 	}
22740Sstevel@tonic-gate 
22750Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
22760Sstevel@tonic-gate 	/*
22775694Sjprakash 	 * Make sure there is a preallocated T_unbind_req message
22785694Sjprakash 	 * before any binding. This message is allocated when the
22795694Sjprakash 	 * socket is created. Since another thread can consume
22805694Sjprakash 	 * so_unbind_mp by the time we return from so_lock_single(),
22815694Sjprakash 	 * we should check the availability of so_unbind_mp after
22825694Sjprakash 	 * we return from so_lock_single().
22830Sstevel@tonic-gate 	 */
22845694Sjprakash 
22855694Sjprakash 	so_lock_single(so);	/* Set SOLOCKED */
22865694Sjprakash 	need_unlock = B_TRUE;
22875694Sjprakash 
22888348SEric.Yu@Sun.COM 	if (sti->sti_unbind_mp == NULL) {
22890Sstevel@tonic-gate 		dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
22900Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
22918348SEric.Yu@Sun.COM 		sti->sti_unbind_mp =
22928778SErik.Nordmark@Sun.COM 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR, cr);
22938348SEric.Yu@Sun.COM 		if (sti->sti_unbind_mp == NULL) {
22940Sstevel@tonic-gate 			error = EINTR;
22950Sstevel@tonic-gate 			goto done;
22960Sstevel@tonic-gate 		}
22970Sstevel@tonic-gate 	}
22980Sstevel@tonic-gate 
22990Sstevel@tonic-gate 	/*
23000Sstevel@tonic-gate 	 * Can't have done a listen before connecting.
23010Sstevel@tonic-gate 	 */
23020Sstevel@tonic-gate 	if (so->so_state & SS_ACCEPTCONN) {
23030Sstevel@tonic-gate 		error = EOPNOTSUPP;
23040Sstevel@tonic-gate 		goto done;
23050Sstevel@tonic-gate 	}
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 	/*
23080Sstevel@tonic-gate 	 * Must be bound with the transport
23090Sstevel@tonic-gate 	 */
23100Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
23110Sstevel@tonic-gate 		if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
23120Sstevel@tonic-gate 		    /*CONSTCOND*/
23130Sstevel@tonic-gate 		    so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
23140Sstevel@tonic-gate 			/*
23150Sstevel@tonic-gate 			 * Optimization for AF_INET{,6} transports
23160Sstevel@tonic-gate 			 * that can handle a T_CONN_REQ without being bound.
23170Sstevel@tonic-gate 			 */
23180Sstevel@tonic-gate 			so_automatic_bind(so);
23190Sstevel@tonic-gate 		} else {
23200Sstevel@tonic-gate 			error = sotpi_bind(so, NULL, 0,
23218348SEric.Yu@Sun.COM 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
23220Sstevel@tonic-gate 			if (error)
23230Sstevel@tonic-gate 				goto done;
23240Sstevel@tonic-gate 		}
23250Sstevel@tonic-gate 		ASSERT(so->so_state & SS_ISBOUND);
23260Sstevel@tonic-gate 		flags |= _SOCONNECT_DID_BIND;
23270Sstevel@tonic-gate 	}
23280Sstevel@tonic-gate 
23290Sstevel@tonic-gate 	/*
23300Sstevel@tonic-gate 	 * Handle a connect to a name parameter of type AF_UNSPEC like a
23310Sstevel@tonic-gate 	 * connect to a null address. This is the portable method to
23320Sstevel@tonic-gate 	 * unconnect a socket.
23330Sstevel@tonic-gate 	 */
23340Sstevel@tonic-gate 	if ((namelen >= sizeof (sa_family_t)) &&
23350Sstevel@tonic-gate 	    (name->sa_family == AF_UNSPEC)) {
23360Sstevel@tonic-gate 		name = NULL;
23370Sstevel@tonic-gate 		namelen = 0;
23380Sstevel@tonic-gate 	}
23390Sstevel@tonic-gate 
23400Sstevel@tonic-gate 	/*
23410Sstevel@tonic-gate 	 * Check that we are not already connected.
23420Sstevel@tonic-gate 	 * A connection-oriented socket cannot be reconnected.
23430Sstevel@tonic-gate 	 * A connected connection-less socket can be
23440Sstevel@tonic-gate 	 * - connected to a different address by a subsequent connect
23450Sstevel@tonic-gate 	 * - "unconnected" by a connect to the NULL address
23460Sstevel@tonic-gate 	 */
23470Sstevel@tonic-gate 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
23480Sstevel@tonic-gate 		ASSERT(!(flags & _SOCONNECT_DID_BIND));
23490Sstevel@tonic-gate 		if (so->so_mode & SM_CONNREQUIRED) {
23500Sstevel@tonic-gate 			/* Connection-oriented socket */
23510Sstevel@tonic-gate 			error = so->so_state & SS_ISCONNECTED ?
23520Sstevel@tonic-gate 			    EISCONN : EALREADY;
23530Sstevel@tonic-gate 			goto done;
23540Sstevel@tonic-gate 		}
23550Sstevel@tonic-gate 		/* Connection-less socket */
23560Sstevel@tonic-gate 		if (name == NULL) {
23570Sstevel@tonic-gate 			/*
23580Sstevel@tonic-gate 			 * Remove the connected state and clear SO_DGRAM_ERRIND
23590Sstevel@tonic-gate 			 * since it was set when the socket was connected.
23600Sstevel@tonic-gate 			 * If this is UDP also send down a T_DISCON_REQ.
23610Sstevel@tonic-gate 			 */
23620Sstevel@tonic-gate 			int val;
23630Sstevel@tonic-gate 
23640Sstevel@tonic-gate 			if ((so->so_family == AF_INET ||
23655240Snordmark 			    so->so_family == AF_INET6) &&
23660Sstevel@tonic-gate 			    (so->so_type == SOCK_DGRAM ||
23675240Snordmark 			    so->so_type == SOCK_RAW) &&
23680Sstevel@tonic-gate 			    /*CONSTCOND*/
23690Sstevel@tonic-gate 			    !soconnect_tpi_udp) {
23700Sstevel@tonic-gate 				/* XXX What about implicitly unbinding here? */
23710Sstevel@tonic-gate 				error = sodisconnect(so, -1,
23725240Snordmark 				    _SODISCONNECT_LOCK_HELD);
23730Sstevel@tonic-gate 			} else {
23740Sstevel@tonic-gate 				so->so_state &=
23758348SEric.Yu@Sun.COM 				    ~(SS_ISCONNECTED | SS_ISCONNECTING);
23768348SEric.Yu@Sun.COM 				sti->sti_faddr_valid = 0;
23778348SEric.Yu@Sun.COM 				sti->sti_faddr_len = 0;
23780Sstevel@tonic-gate 			}
23790Sstevel@tonic-gate 
23808348SEric.Yu@Sun.COM 			/* Remove SOLOCKED since setsockopt will grab it */
23810Sstevel@tonic-gate 			so_unlock_single(so, SOLOCKED);
23820Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
23830Sstevel@tonic-gate 
23840Sstevel@tonic-gate 			val = 0;
23858348SEric.Yu@Sun.COM 			(void) sotpi_setsockopt(so, SOL_SOCKET,
23868348SEric.Yu@Sun.COM 			    SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val),
23878348SEric.Yu@Sun.COM 			    cr);
23880Sstevel@tonic-gate 
23890Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
23900Sstevel@tonic-gate 			so_lock_single(so);	/* Set SOLOCKED */
23910Sstevel@tonic-gate 			goto done;
23920Sstevel@tonic-gate 		}
23930Sstevel@tonic-gate 	}
23940Sstevel@tonic-gate 	ASSERT(so->so_state & SS_ISBOUND);
23950Sstevel@tonic-gate 
23960Sstevel@tonic-gate 	if (name == NULL || namelen == 0) {
23970Sstevel@tonic-gate 		error = EINVAL;
23980Sstevel@tonic-gate 		goto done;
23990Sstevel@tonic-gate 	}
24000Sstevel@tonic-gate 	/*
24018348SEric.Yu@Sun.COM 	 * Mark the socket if sti_faddr_sa represents the transport level
24020Sstevel@tonic-gate 	 * address.
24030Sstevel@tonic-gate 	 */
24040Sstevel@tonic-gate 	if (flags & _SOCONNECT_NOXLATE) {
24050Sstevel@tonic-gate 		struct sockaddr_ux	*soaddr_ux;
24060Sstevel@tonic-gate 
24070Sstevel@tonic-gate 		ASSERT(so->so_family == AF_UNIX);
24080Sstevel@tonic-gate 		if (namelen != sizeof (struct sockaddr_ux)) {
24090Sstevel@tonic-gate 			error = EINVAL;
24100Sstevel@tonic-gate 			goto done;
24110Sstevel@tonic-gate 		}
24120Sstevel@tonic-gate 		soaddr_ux = (struct sockaddr_ux *)name;
24130Sstevel@tonic-gate 		name = (struct sockaddr *)&soaddr_ux->sou_addr;
24140Sstevel@tonic-gate 		namelen = sizeof (soaddr_ux->sou_addr);
24158348SEric.Yu@Sun.COM 		sti->sti_faddr_noxlate = 1;
24160Sstevel@tonic-gate 	}
24170Sstevel@tonic-gate 
24180Sstevel@tonic-gate 	/*
24190Sstevel@tonic-gate 	 * Length and family checks.
24200Sstevel@tonic-gate 	 */
24210Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
24220Sstevel@tonic-gate 	if (error)
24230Sstevel@tonic-gate 		goto bad;
24240Sstevel@tonic-gate 
24250Sstevel@tonic-gate 	/*
24260Sstevel@tonic-gate 	 * Save foreign address. Needed for AF_UNIX as well as
24270Sstevel@tonic-gate 	 * transport providers that do not support TI_GETPEERNAME.
24280Sstevel@tonic-gate 	 * Also used for cached foreign address for TCP and UDP.
24290Sstevel@tonic-gate 	 */
24308348SEric.Yu@Sun.COM 	if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) {
24310Sstevel@tonic-gate 		error = EINVAL;
24320Sstevel@tonic-gate 		goto done;
24330Sstevel@tonic-gate 	}
24348348SEric.Yu@Sun.COM 	sti->sti_faddr_len = (socklen_t)namelen;
24358348SEric.Yu@Sun.COM 	ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
24368348SEric.Yu@Sun.COM 	bcopy(name, sti->sti_faddr_sa, namelen);
24378348SEric.Yu@Sun.COM 	sti->sti_faddr_valid = 1;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
24408348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate) {
24410Sstevel@tonic-gate 			/*
24420Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
24430Sstevel@tonic-gate 			 * pass any (transport internal) source address.
24440Sstevel@tonic-gate 			 */
24458348SEric.Yu@Sun.COM 			addr = sti->sti_faddr_sa;
24468348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sti->sti_faddr_len;
24470Sstevel@tonic-gate 			src = NULL;
24480Sstevel@tonic-gate 			srclen = 0;
24490Sstevel@tonic-gate 		} else {
24500Sstevel@tonic-gate 			/*
24510Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
24520Sstevel@tonic-gate 			 * and translate the remote address.
24538348SEric.Yu@Sun.COM 			 * Holding so_lock thus sti_laddr_sa can not change.
24540Sstevel@tonic-gate 			 */
24558348SEric.Yu@Sun.COM 			src = sti->sti_laddr_sa;
24568348SEric.Yu@Sun.COM 			srclen = (t_uscalar_t)sti->sti_laddr_len;
24570Sstevel@tonic-gate 			dprintso(so, 1,
24585240Snordmark 			    ("sotpi_connect UNIX: srclen %d, src %p\n",
24595240Snordmark 			    srclen, src));
24600Sstevel@tonic-gate 			error = so_ux_addr_xlate(so,
24618348SEric.Yu@Sun.COM 			    sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len,
24625240Snordmark 			    (flags & _SOCONNECT_XPG4_2),
24635240Snordmark 			    &addr, &addrlen);
24640Sstevel@tonic-gate 			if (error)
24650Sstevel@tonic-gate 				goto bad;
24660Sstevel@tonic-gate 		}
24670Sstevel@tonic-gate 	} else {
24688348SEric.Yu@Sun.COM 		addr = sti->sti_faddr_sa;
24698348SEric.Yu@Sun.COM 		addrlen = (t_uscalar_t)sti->sti_faddr_len;
24700Sstevel@tonic-gate 		src = NULL;
24710Sstevel@tonic-gate 		srclen = 0;
24720Sstevel@tonic-gate 	}
24730Sstevel@tonic-gate 	/*
24740Sstevel@tonic-gate 	 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
24750Sstevel@tonic-gate 	 * option which asks the transport provider to send T_UDERR_IND
24760Sstevel@tonic-gate 	 * messages. These T_UDERR_IND messages are used to return connected
24770Sstevel@tonic-gate 	 * style errors (e.g. ECONNRESET) for connected datagram sockets.
24780Sstevel@tonic-gate 	 *
24790Sstevel@tonic-gate 	 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
24800Sstevel@tonic-gate 	 * we send down a T_CONN_REQ. This is needed to let the
24810Sstevel@tonic-gate 	 * transport assign a local address that is consistent with
24820Sstevel@tonic-gate 	 * the remote address. Applications depend on a getsockname()
24830Sstevel@tonic-gate 	 * after a connect() to retrieve the "source" IP address for
24840Sstevel@tonic-gate 	 * the connected socket.  Invalidate the cached local address
24850Sstevel@tonic-gate 	 * to force getsockname() to enquire of the transport.
24860Sstevel@tonic-gate 	 */
24870Sstevel@tonic-gate 	if (!(so->so_mode & SM_CONNREQUIRED)) {
24880Sstevel@tonic-gate 		/*
24890Sstevel@tonic-gate 		 * Datagram socket.
24900Sstevel@tonic-gate 		 */
24910Sstevel@tonic-gate 		int32_t val;
24920Sstevel@tonic-gate 
24930Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
24940Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
24950Sstevel@tonic-gate 
24960Sstevel@tonic-gate 		val = 1;
24970Sstevel@tonic-gate 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
24988348SEric.Yu@Sun.COM 		    &val, (t_uscalar_t)sizeof (val), cr);
24990Sstevel@tonic-gate 
25000Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
25010Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
25020Sstevel@tonic-gate 		if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
25030Sstevel@tonic-gate 		    (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
25040Sstevel@tonic-gate 		    soconnect_tpi_udp) {
25050Sstevel@tonic-gate 			soisconnected(so);
25060Sstevel@tonic-gate 			goto done;
25070Sstevel@tonic-gate 		}
25080Sstevel@tonic-gate 		/*
25090Sstevel@tonic-gate 		 * Send down T_CONN_REQ etc.
25100Sstevel@tonic-gate 		 * Clear fflag to avoid returning EWOULDBLOCK.
25110Sstevel@tonic-gate 		 */
25120Sstevel@tonic-gate 		fflag = 0;
25130Sstevel@tonic-gate 		ASSERT(so->so_family != AF_UNIX);
25148348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
25158348SEric.Yu@Sun.COM 	} else if (sti->sti_laddr_len != 0) {
25160Sstevel@tonic-gate 		/*
25170Sstevel@tonic-gate 		 * If the local address or port was "any" then it may be
25180Sstevel@tonic-gate 		 * changed by the transport as a result of the
25190Sstevel@tonic-gate 		 * connect.  Invalidate the cached version if we have one.
25200Sstevel@tonic-gate 		 */
25210Sstevel@tonic-gate 		switch (so->so_family) {
25220Sstevel@tonic-gate 		case AF_INET:
25238348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t));
25248348SEric.Yu@Sun.COM 			if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr ==
25250Sstevel@tonic-gate 			    INADDR_ANY ||
25268348SEric.Yu@Sun.COM 			    ((sin_t *)sti->sti_laddr_sa)->sin_port == 0)
25278348SEric.Yu@Sun.COM 				sti->sti_laddr_valid = 0;
25280Sstevel@tonic-gate 			break;
25290Sstevel@tonic-gate 
25300Sstevel@tonic-gate 		case AF_INET6:
25318348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len ==
25328348SEric.Yu@Sun.COM 			    (socklen_t)sizeof (sin6_t));
25330Sstevel@tonic-gate 			if (IN6_IS_ADDR_UNSPECIFIED(
25348348SEric.Yu@Sun.COM 			    &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) ||
25350Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED_ANY(
25368348SEric.Yu@Sun.COM 			    &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) ||
25378348SEric.Yu@Sun.COM 			    ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0)
25388348SEric.Yu@Sun.COM 				sti->sti_laddr_valid = 0;
25390Sstevel@tonic-gate 			break;
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate 		default:
25420Sstevel@tonic-gate 			break;
25430Sstevel@tonic-gate 		}
25440Sstevel@tonic-gate 	}
25450Sstevel@tonic-gate 
25460Sstevel@tonic-gate 	/*
25470Sstevel@tonic-gate 	 * Check for failure of an earlier call
25480Sstevel@tonic-gate 	 */
25490Sstevel@tonic-gate 	if (so->so_error != 0)
25500Sstevel@tonic-gate 		goto so_bad;
25510Sstevel@tonic-gate 
25520Sstevel@tonic-gate 	/*
25530Sstevel@tonic-gate 	 * Send down T_CONN_REQ. Message was allocated above.
25540Sstevel@tonic-gate 	 */
25550Sstevel@tonic-gate 	conn_req.PRIM_type = T_CONN_REQ;
25560Sstevel@tonic-gate 	conn_req.DEST_length = addrlen;
25570Sstevel@tonic-gate 	conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
25580Sstevel@tonic-gate 	if (srclen == 0) {
25590Sstevel@tonic-gate 		conn_req.OPT_length = 0;
25600Sstevel@tonic-gate 		conn_req.OPT_offset = 0;
25610Sstevel@tonic-gate 		soappendmsg(mp, &conn_req, sizeof (conn_req));
25620Sstevel@tonic-gate 		soappendmsg(mp, addr, addrlen);
25630Sstevel@tonic-gate 	} else {
25640Sstevel@tonic-gate 		/*
25650Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
25660Sstevel@tonic-gate 		 * address option.
25670Sstevel@tonic-gate 		 */
25680Sstevel@tonic-gate 		struct T_opthdr toh;
25690Sstevel@tonic-gate 
25700Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
25710Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
25720Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
25730Sstevel@tonic-gate 		toh.status = 0;
25740Sstevel@tonic-gate 		conn_req.OPT_length =
25755240Snordmark 		    (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
25760Sstevel@tonic-gate 		conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
25775240Snordmark 		    _TPI_ALIGN_TOPT(addrlen));
25780Sstevel@tonic-gate 
25790Sstevel@tonic-gate 		soappendmsg(mp, &conn_req, sizeof (conn_req));
25800Sstevel@tonic-gate 		soappendmsg(mp, addr, addrlen);
25810Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
25820Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
25830Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
25840Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
25850Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
25860Sstevel@tonic-gate 	}
25870Sstevel@tonic-gate 	/*
25880Sstevel@tonic-gate 	 * Set SS_ISCONNECTING before sending down the T_CONN_REQ
25890Sstevel@tonic-gate 	 * in order to have the right state when the T_CONN_CON shows up.
25900Sstevel@tonic-gate 	 */
25910Sstevel@tonic-gate 	soisconnecting(so);
25920Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
25930Sstevel@tonic-gate 
2594*11861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
25950Sstevel@tonic-gate 		audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
25960Sstevel@tonic-gate 
25970Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
25985240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
25990Sstevel@tonic-gate 	mp = NULL;
26000Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
26010Sstevel@tonic-gate 	if (error != 0)
26020Sstevel@tonic-gate 		goto bad;
26030Sstevel@tonic-gate 
26040Sstevel@tonic-gate 	if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
26050Sstevel@tonic-gate 		goto bad;
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate 	/* Allow other threads to access the socket */
26080Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
26090Sstevel@tonic-gate 	need_unlock = B_FALSE;
26100Sstevel@tonic-gate 
26110Sstevel@tonic-gate 	/*
26120Sstevel@tonic-gate 	 * Wait until we get a T_CONN_CON or an error
26130Sstevel@tonic-gate 	 */
26140Sstevel@tonic-gate 	if ((error = sowaitconnected(so, fflag, 0)) != 0) {
26150Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
26160Sstevel@tonic-gate 		need_unlock = B_TRUE;
26170Sstevel@tonic-gate 	}
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate done:
26200Sstevel@tonic-gate 	freemsg(mp);
26210Sstevel@tonic-gate 	switch (error) {
26220Sstevel@tonic-gate 	case EINPROGRESS:
26230Sstevel@tonic-gate 	case EALREADY:
26240Sstevel@tonic-gate 	case EISCONN:
26250Sstevel@tonic-gate 	case EINTR:
26260Sstevel@tonic-gate 		/* Non-fatal errors */
26278348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
26280Sstevel@tonic-gate 		/* FALLTHRU */
26290Sstevel@tonic-gate 	case 0:
26300Sstevel@tonic-gate 		break;
26310Sstevel@tonic-gate 	default:
26320Sstevel@tonic-gate 		ASSERT(need_unlock);
26330Sstevel@tonic-gate 		/*
26340Sstevel@tonic-gate 		 * Fatal errors: clear SS_ISCONNECTING in case it was set,
26350Sstevel@tonic-gate 		 * and invalidate local-address cache
26360Sstevel@tonic-gate 		 */
26378348SEric.Yu@Sun.COM 		so->so_state &= ~SS_ISCONNECTING;
26388348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
26390Sstevel@tonic-gate 		/* A discon_ind might have already unbound us */
26400Sstevel@tonic-gate 		if ((flags & _SOCONNECT_DID_BIND) &&
26410Sstevel@tonic-gate 		    (so->so_state & SS_ISBOUND)) {
26420Sstevel@tonic-gate 			int err;
26430Sstevel@tonic-gate 
26440Sstevel@tonic-gate 			err = sotpi_unbind(so, 0);
26450Sstevel@tonic-gate 			/* LINTED - statement has no conseq */
26460Sstevel@tonic-gate 			if (err) {
26470Sstevel@tonic-gate 				eprintsoline(so, err);
26480Sstevel@tonic-gate 			}
26490Sstevel@tonic-gate 		}
26500Sstevel@tonic-gate 		break;
26510Sstevel@tonic-gate 	}
26520Sstevel@tonic-gate 	if (need_unlock)
26530Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
26540Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
26550Sstevel@tonic-gate 	return (error);
26560Sstevel@tonic-gate 
26578348SEric.Yu@Sun.COM so_bad:	error = sogeterr(so, B_TRUE);
26580Sstevel@tonic-gate bad:	eprintsoline(so, error);
26590Sstevel@tonic-gate 	goto done;
26600Sstevel@tonic-gate }
26610Sstevel@tonic-gate 
26628348SEric.Yu@Sun.COM /* ARGSUSED */
26630Sstevel@tonic-gate int
26648348SEric.Yu@Sun.COM sotpi_shutdown(struct sonode *so, int how, struct cred *cr)
26650Sstevel@tonic-gate {
26660Sstevel@tonic-gate 	struct T_ordrel_req	ordrel_req;
26670Sstevel@tonic-gate 	mblk_t			*mp;
26680Sstevel@tonic-gate 	uint_t			old_state, state_change;
26690Sstevel@tonic-gate 	int			error = 0;
26708348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
26710Sstevel@tonic-gate 
26720Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
26737240Srh87107 	    (void *)so, how, pr_state(so->so_state, so->so_mode)));
26740Sstevel@tonic-gate 
26750Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
26760Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
26770Sstevel@tonic-gate 
26780Sstevel@tonic-gate 	/*
26790Sstevel@tonic-gate 	 * SunOS 4.X has no check for datagram sockets.
26800Sstevel@tonic-gate 	 * 5.X checks that it is connected (ENOTCONN)
26810Sstevel@tonic-gate 	 * X/Open requires that we check the connected state.
26820Sstevel@tonic-gate 	 */
26830Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
26840Sstevel@tonic-gate 		if (!xnet_skip_checks) {
26850Sstevel@tonic-gate 			error = ENOTCONN;
26860Sstevel@tonic-gate 			if (xnet_check_print) {
26870Sstevel@tonic-gate 				printf("sockfs: X/Open shutdown check "
26885240Snordmark 				    "caused ENOTCONN\n");
26890Sstevel@tonic-gate 			}
26900Sstevel@tonic-gate 		}
26910Sstevel@tonic-gate 		goto done;
26920Sstevel@tonic-gate 	}
26930Sstevel@tonic-gate 	/*
26940Sstevel@tonic-gate 	 * Record the current state and then perform any state changes.
26950Sstevel@tonic-gate 	 * Then use the difference between the old and new states to
26960Sstevel@tonic-gate 	 * determine which messages need to be sent.
26970Sstevel@tonic-gate 	 * This prevents e.g. duplicate T_ORDREL_REQ when there are
26980Sstevel@tonic-gate 	 * duplicate calls to shutdown().
26990Sstevel@tonic-gate 	 */
27000Sstevel@tonic-gate 	old_state = so->so_state;
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	switch (how) {
27030Sstevel@tonic-gate 	case 0:
27040Sstevel@tonic-gate 		socantrcvmore(so);
27050Sstevel@tonic-gate 		break;
27060Sstevel@tonic-gate 	case 1:
27070Sstevel@tonic-gate 		socantsendmore(so);
27080Sstevel@tonic-gate 		break;
27090Sstevel@tonic-gate 	case 2:
27100Sstevel@tonic-gate 		socantsendmore(so);
27110Sstevel@tonic-gate 		socantrcvmore(so);
27120Sstevel@tonic-gate 		break;
27130Sstevel@tonic-gate 	default:
27140Sstevel@tonic-gate 		error = EINVAL;
27150Sstevel@tonic-gate 		goto done;
27160Sstevel@tonic-gate 	}
27170Sstevel@tonic-gate 
27180Sstevel@tonic-gate 	/*
27190Sstevel@tonic-gate 	 * Assumes that the SS_CANT* flags are never cleared in the above code.
27200Sstevel@tonic-gate 	 */
27210Sstevel@tonic-gate 	state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
27225240Snordmark 	    (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
27230Sstevel@tonic-gate 	ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
27240Sstevel@tonic-gate 
27250Sstevel@tonic-gate 	switch (state_change) {
27260Sstevel@tonic-gate 	case 0:
27270Sstevel@tonic-gate 		dprintso(so, 1,
27280Sstevel@tonic-gate 		    ("sotpi_shutdown: nothing to send in state 0x%x\n",
27290Sstevel@tonic-gate 		    so->so_state));
27300Sstevel@tonic-gate 		goto done;
27310Sstevel@tonic-gate 
27320Sstevel@tonic-gate 	case SS_CANTRCVMORE:
27330Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27340Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
27350Sstevel@tonic-gate 		/*
27360Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
27370Sstevel@tonic-gate 		 * pollwakeups, and signals.
27380Sstevel@tonic-gate 		 */
27390Sstevel@tonic-gate 		/*
27400Sstevel@tonic-gate 		 * Get the read lock before flushing data to avoid problems
27410Sstevel@tonic-gate 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
27420Sstevel@tonic-gate 		 */
27430Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27440Sstevel@tonic-gate 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
27450Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27460Sstevel@tonic-gate 
27470Sstevel@tonic-gate 		/* Flush read side queue */
27480Sstevel@tonic-gate 		strflushrq(SOTOV(so), FLUSHALL);
27490Sstevel@tonic-gate 
27500Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27510Sstevel@tonic-gate 		so_unlock_read(so);		/* Clear SOREADLOCKED */
27520Sstevel@tonic-gate 		break;
27530Sstevel@tonic-gate 
27540Sstevel@tonic-gate 	case SS_CANTSENDMORE:
27550Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27560Sstevel@tonic-gate 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
27570Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27580Sstevel@tonic-gate 		break;
27590Sstevel@tonic-gate 
27600Sstevel@tonic-gate 	case SS_CANTSENDMORE|SS_CANTRCVMORE:
27610Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27620Sstevel@tonic-gate 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
27630Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
27640Sstevel@tonic-gate 		/*
27650Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
27660Sstevel@tonic-gate 		 * pollwakeups, and signals.
27670Sstevel@tonic-gate 		 */
27680Sstevel@tonic-gate 		/*
27690Sstevel@tonic-gate 		 * Get the read lock before flushing data to avoid problems
27700Sstevel@tonic-gate 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
27710Sstevel@tonic-gate 		 */
27720Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27730Sstevel@tonic-gate 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
27740Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27750Sstevel@tonic-gate 
27760Sstevel@tonic-gate 		/* Flush read side queue */
27770Sstevel@tonic-gate 		strflushrq(SOTOV(so), FLUSHALL);
27780Sstevel@tonic-gate 
27790Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27800Sstevel@tonic-gate 		so_unlock_read(so);		/* Clear SOREADLOCKED */
27810Sstevel@tonic-gate 		break;
27820Sstevel@tonic-gate 	}
27830Sstevel@tonic-gate 
27840Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
27850Sstevel@tonic-gate 
27860Sstevel@tonic-gate 	/*
27870Sstevel@tonic-gate 	 * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
27880Sstevel@tonic-gate 	 * was set due to this call and the new state has both of them set:
27890Sstevel@tonic-gate 	 *	Send the AF_UNIX close indication
27900Sstevel@tonic-gate 	 *	For T_COTS send a discon_ind
27910Sstevel@tonic-gate 	 *
27920Sstevel@tonic-gate 	 * If cantsend was set due to this call:
27930Sstevel@tonic-gate 	 *	For T_COTSORD send an ordrel_ind
27940Sstevel@tonic-gate 	 *
27950Sstevel@tonic-gate 	 * Note that for T_CLTS there is no message sent here.
27960Sstevel@tonic-gate 	 */
27970Sstevel@tonic-gate 	if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
27980Sstevel@tonic-gate 	    (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
27990Sstevel@tonic-gate 		/*
28000Sstevel@tonic-gate 		 * For SunOS 4.X compatibility we tell the other end
28010Sstevel@tonic-gate 		 * that we are unable to receive at this point.
28020Sstevel@tonic-gate 		 */
28038348SEric.Yu@Sun.COM 		if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS)
28040Sstevel@tonic-gate 			so_unix_close(so);
28050Sstevel@tonic-gate 
28068348SEric.Yu@Sun.COM 		if (sti->sti_serv_type == T_COTS)
28070Sstevel@tonic-gate 			error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
28080Sstevel@tonic-gate 	}
28090Sstevel@tonic-gate 	if ((state_change & SS_CANTSENDMORE) &&
28108348SEric.Yu@Sun.COM 	    (sti->sti_serv_type == T_COTS_ORD)) {
28110Sstevel@tonic-gate 		/* Send an orderly release */
28120Sstevel@tonic-gate 		ordrel_req.PRIM_type = T_ORDREL_REQ;
28130Sstevel@tonic-gate 
28140Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
28150Sstevel@tonic-gate 		mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
28168778SErik.Nordmark@Sun.COM 		    0, _ALLOC_SLEEP, cr);
28170Sstevel@tonic-gate 		/*
28180Sstevel@tonic-gate 		 * Send down the T_ORDREL_REQ even if there is flow control.
28190Sstevel@tonic-gate 		 * This prevents shutdown from blocking.
28200Sstevel@tonic-gate 		 * Note that there is no T_OK_ACK for ordrel_req.
28210Sstevel@tonic-gate 		 */
28220Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
28235240Snordmark 		    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
28240Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
28250Sstevel@tonic-gate 		if (error) {
28260Sstevel@tonic-gate 			eprintsoline(so, error);
28270Sstevel@tonic-gate 			goto done;
28280Sstevel@tonic-gate 		}
28290Sstevel@tonic-gate 	}
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate done:
28320Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
28330Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
28340Sstevel@tonic-gate 	return (error);
28350Sstevel@tonic-gate }
28360Sstevel@tonic-gate 
28370Sstevel@tonic-gate /*
28380Sstevel@tonic-gate  * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
28390Sstevel@tonic-gate  * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
28400Sstevel@tonic-gate  * that we have closed.
28410Sstevel@tonic-gate  * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
28420Sstevel@tonic-gate  * T_UNITDATA_REQ containing the same option.
28430Sstevel@tonic-gate  *
28440Sstevel@tonic-gate  * For SOCK_DGRAM half-connections (somebody connected to this end
28450Sstevel@tonic-gate  * but this end is not connect) we don't know where to send any
28460Sstevel@tonic-gate  * SO_UNIX_CLOSE.
28470Sstevel@tonic-gate  *
28480Sstevel@tonic-gate  * We have to ignore stream head errors just in case there has been
28490Sstevel@tonic-gate  * a shutdown(output).
28500Sstevel@tonic-gate  * Ignore any flow control to try to get the message more quickly to the peer.
28510Sstevel@tonic-gate  * While locally ignoring flow control solves the problem when there
28520Sstevel@tonic-gate  * is only the loopback transport on the stream it would not provide
28530Sstevel@tonic-gate  * the correct AF_UNIX socket semantics when one or more modules have
28540Sstevel@tonic-gate  * been pushed.
28550Sstevel@tonic-gate  */
28560Sstevel@tonic-gate void
28570Sstevel@tonic-gate so_unix_close(struct sonode *so)
28580Sstevel@tonic-gate {
28590Sstevel@tonic-gate 	int		error;
28600Sstevel@tonic-gate 	struct T_opthdr	toh;
28610Sstevel@tonic-gate 	mblk_t		*mp;
28628348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
28630Sstevel@tonic-gate 
28640Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
28650Sstevel@tonic-gate 
28660Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
28670Sstevel@tonic-gate 
28680Sstevel@tonic-gate 	if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
28690Sstevel@tonic-gate 	    (SS_ISCONNECTED|SS_ISBOUND))
28700Sstevel@tonic-gate 		return;
28710Sstevel@tonic-gate 
28720Sstevel@tonic-gate 	dprintso(so, 1, ("so_unix_close(%p) %s\n",
28737240Srh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
28740Sstevel@tonic-gate 
28750Sstevel@tonic-gate 	toh.level = SOL_SOCKET;
28760Sstevel@tonic-gate 	toh.name = SO_UNIX_CLOSE;
28770Sstevel@tonic-gate 
28780Sstevel@tonic-gate 	/* zero length + header */
28790Sstevel@tonic-gate 	toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
28800Sstevel@tonic-gate 	toh.status = 0;
28810Sstevel@tonic-gate 
28820Sstevel@tonic-gate 	if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
28830Sstevel@tonic-gate 		struct T_optdata_req tdr;
28840Sstevel@tonic-gate 
28850Sstevel@tonic-gate 		tdr.PRIM_type = T_OPTDATA_REQ;
28860Sstevel@tonic-gate 		tdr.DATA_flag = 0;
28870Sstevel@tonic-gate 
28880Sstevel@tonic-gate 		tdr.OPT_length = (t_scalar_t)sizeof (toh);
28890Sstevel@tonic-gate 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
28900Sstevel@tonic-gate 
28910Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
28920Sstevel@tonic-gate 		mp = soallocproto2(&tdr, sizeof (tdr),
28938778SErik.Nordmark@Sun.COM 		    &toh, sizeof (toh), 0, _ALLOC_SLEEP, CRED());
28940Sstevel@tonic-gate 	} else {
28950Sstevel@tonic-gate 		struct T_unitdata_req	tudr;
28960Sstevel@tonic-gate 		void			*addr;
28970Sstevel@tonic-gate 		socklen_t		addrlen;
28980Sstevel@tonic-gate 		void			*src;
28990Sstevel@tonic-gate 		socklen_t		srclen;
29000Sstevel@tonic-gate 		struct T_opthdr		toh2;
29010Sstevel@tonic-gate 		t_scalar_t		size;
29020Sstevel@tonic-gate 
29030Sstevel@tonic-gate 		/* Connecteded DGRAM socket */
29040Sstevel@tonic-gate 
29050Sstevel@tonic-gate 		/*
29060Sstevel@tonic-gate 		 * For AF_UNIX the destination address is translated to
29070Sstevel@tonic-gate 		 * an internal name and the source address is passed as
29080Sstevel@tonic-gate 		 * an option.
29090Sstevel@tonic-gate 		 */
29100Sstevel@tonic-gate 		/*
29110Sstevel@tonic-gate 		 * Length and family checks.
29120Sstevel@tonic-gate 		 */
29138348SEric.Yu@Sun.COM 		error = so_addr_verify(so, sti->sti_faddr_sa,
29148348SEric.Yu@Sun.COM 		    (t_uscalar_t)sti->sti_faddr_len);
29150Sstevel@tonic-gate 		if (error) {
29160Sstevel@tonic-gate 			eprintsoline(so, error);
29170Sstevel@tonic-gate 			return;
29180Sstevel@tonic-gate 		}
29198348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate) {
29200Sstevel@tonic-gate 			/*
29210Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
29220Sstevel@tonic-gate 			 * pass any (transport internal) source address.
29230Sstevel@tonic-gate 			 */
29248348SEric.Yu@Sun.COM 			addr = sti->sti_faddr_sa;
29258348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sti->sti_faddr_len;
29260Sstevel@tonic-gate 			src = NULL;
29270Sstevel@tonic-gate 			srclen = 0;
29280Sstevel@tonic-gate 		} else {
29290Sstevel@tonic-gate 			/*
29300Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
29310Sstevel@tonic-gate 			 * and translate the remote address.
29328348SEric.Yu@Sun.COM 			 * Holding so_lock thus sti_laddr_sa can not change.
29330Sstevel@tonic-gate 			 */
29348348SEric.Yu@Sun.COM 			src = sti->sti_laddr_sa;
29358348SEric.Yu@Sun.COM 			srclen = (socklen_t)sti->sti_laddr_len;
29360Sstevel@tonic-gate 			dprintso(so, 1,
29375240Snordmark 			    ("so_ux_close: srclen %d, src %p\n",
29385240Snordmark 			    srclen, src));
29390Sstevel@tonic-gate 			error = so_ux_addr_xlate(so,
29408348SEric.Yu@Sun.COM 			    sti->sti_faddr_sa,
29418348SEric.Yu@Sun.COM 			    (socklen_t)sti->sti_faddr_len, 0,
29425240Snordmark 			    &addr, &addrlen);
29430Sstevel@tonic-gate 			if (error) {
29440Sstevel@tonic-gate 				eprintsoline(so, error);
29450Sstevel@tonic-gate 				return;
29460Sstevel@tonic-gate 			}
29470Sstevel@tonic-gate 		}
29480Sstevel@tonic-gate 		tudr.PRIM_type = T_UNITDATA_REQ;
29490Sstevel@tonic-gate 		tudr.DEST_length = addrlen;
29500Sstevel@tonic-gate 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
29510Sstevel@tonic-gate 		if (srclen == 0) {
29520Sstevel@tonic-gate 			tudr.OPT_length = (t_scalar_t)sizeof (toh);
29530Sstevel@tonic-gate 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
29545240Snordmark 			    _TPI_ALIGN_TOPT(addrlen));
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate 			size = tudr.OPT_offset + tudr.OPT_length;
29570Sstevel@tonic-gate 			/* NOTE: holding so_lock while sleeping */
29580Sstevel@tonic-gate 			mp = soallocproto2(&tudr, sizeof (tudr),
29598778SErik.Nordmark@Sun.COM 			    addr, addrlen, size, _ALLOC_SLEEP, CRED());
29600Sstevel@tonic-gate 			mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
29610Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
29620Sstevel@tonic-gate 		} else {
29630Sstevel@tonic-gate 			/*
29640Sstevel@tonic-gate 			 * There is a AF_UNIX sockaddr_un to include as a
29650Sstevel@tonic-gate 			 * source address option.
29660Sstevel@tonic-gate 			 */
29670Sstevel@tonic-gate 			tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
29680Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(srclen));
29690Sstevel@tonic-gate 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
29700Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(addrlen));
29710Sstevel@tonic-gate 
29720Sstevel@tonic-gate 			toh2.level = SOL_SOCKET;
29730Sstevel@tonic-gate 			toh2.name = SO_SRCADDR;
29740Sstevel@tonic-gate 			toh2.len = (t_uscalar_t)(srclen +
29755240Snordmark 			    sizeof (struct T_opthdr));
29760Sstevel@tonic-gate 			toh2.status = 0;
29770Sstevel@tonic-gate 
29780Sstevel@tonic-gate 			size = tudr.OPT_offset + tudr.OPT_length;
29790Sstevel@tonic-gate 
29800Sstevel@tonic-gate 			/* NOTE: holding so_lock while sleeping */
29810Sstevel@tonic-gate 			mp = soallocproto2(&tudr, sizeof (tudr),
29828778SErik.Nordmark@Sun.COM 			    addr, addrlen, size, _ALLOC_SLEEP, CRED());
29830Sstevel@tonic-gate 			mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
29840Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
29850Sstevel@tonic-gate 			soappendmsg(mp, &toh2, sizeof (toh2));
29860Sstevel@tonic-gate 			soappendmsg(mp, src, srclen);
29870Sstevel@tonic-gate 			mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
29880Sstevel@tonic-gate 		}
29890Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
29900Sstevel@tonic-gate 	}
29910Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
29920Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
29935240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
29940Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
29950Sstevel@tonic-gate }
29960Sstevel@tonic-gate 
29970Sstevel@tonic-gate /*
29980Sstevel@tonic-gate  * Called by sotpi_recvmsg when reading a non-zero amount of data.
29990Sstevel@tonic-gate  * In addition, the caller typically verifies that there is some
30000Sstevel@tonic-gate  * potential state to clear by checking
30010Sstevel@tonic-gate  *	if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
30020Sstevel@tonic-gate  * before calling this routine.
30030Sstevel@tonic-gate  * Note that such a check can be made without holding so_lock since
30040Sstevel@tonic-gate  * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
30058348SEric.Yu@Sun.COM  * decrements sti_oobsigcnt.
30060Sstevel@tonic-gate  *
30070Sstevel@tonic-gate  * When data is read *after* the point that all pending
30080Sstevel@tonic-gate  * oob data has been consumed the oob indication is cleared.
30090Sstevel@tonic-gate  *
30100Sstevel@tonic-gate  * This logic keeps select/poll returning POLLRDBAND and
30110Sstevel@tonic-gate  * SIOCATMARK returning true until we have read past
30120Sstevel@tonic-gate  * the mark.
30130Sstevel@tonic-gate  */
30140Sstevel@tonic-gate static void
30150Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so)
30160Sstevel@tonic-gate {
30178348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
30188348SEric.Yu@Sun.COM 
30190Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
30200Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
30210Sstevel@tonic-gate 	dprintso(so, 1,
30225240Snordmark 	    ("sorecv_update_oobstate: counts %d/%d state %s\n",
30238348SEric.Yu@Sun.COM 	    sti->sti_oobsigcnt,
30248348SEric.Yu@Sun.COM 	    sti->sti_oobcnt, pr_state(so->so_state, so->so_mode)));
30258348SEric.Yu@Sun.COM 	if (sti->sti_oobsigcnt == 0) {
30260Sstevel@tonic-gate 		/* No more pending oob indications */
30270Sstevel@tonic-gate 		so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
30280Sstevel@tonic-gate 		freemsg(so->so_oobmsg);
30290Sstevel@tonic-gate 		so->so_oobmsg = NULL;
30300Sstevel@tonic-gate 	}
30310Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
30320Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
30330Sstevel@tonic-gate }
30340Sstevel@tonic-gate 
30350Sstevel@tonic-gate /*
30360Sstevel@tonic-gate  * Handle recv* calls for an so which has NL7C saved recv mblk_t(s).
30370Sstevel@tonic-gate  */
30380Sstevel@tonic-gate static int
30390Sstevel@tonic-gate nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp)
30400Sstevel@tonic-gate {
30418348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
30420Sstevel@tonic-gate 	int	error = 0;
30430Sstevel@tonic-gate 	mblk_t *tmp = NULL;
30440Sstevel@tonic-gate 	mblk_t *pmp = NULL;
30458348SEric.Yu@Sun.COM 	mblk_t *nmp = sti->sti_nl7c_rcv_mp;
30460Sstevel@tonic-gate 
30470Sstevel@tonic-gate 	ASSERT(nmp != NULL);
30480Sstevel@tonic-gate 
30490Sstevel@tonic-gate 	while (nmp != NULL && uiop->uio_resid > 0) {
30500Sstevel@tonic-gate 		ssize_t n;
30510Sstevel@tonic-gate 
30520Sstevel@tonic-gate 		if (DB_TYPE(nmp) == M_DATA) {
30530Sstevel@tonic-gate 			/*
30540Sstevel@tonic-gate 			 * We have some data, uiomove up to resid bytes.
30550Sstevel@tonic-gate 			 */
30560Sstevel@tonic-gate 			n = MIN(MBLKL(nmp), uiop->uio_resid);
30570Sstevel@tonic-gate 			if (n > 0)
30580Sstevel@tonic-gate 				error = uiomove(nmp->b_rptr, n, UIO_READ, uiop);
30590Sstevel@tonic-gate 			nmp->b_rptr += n;
30600Sstevel@tonic-gate 			if (nmp->b_rptr == nmp->b_wptr) {
30610Sstevel@tonic-gate 				pmp = nmp;
30620Sstevel@tonic-gate 				nmp = nmp->b_cont;
30630Sstevel@tonic-gate 			}
30641974Sbrutus 			if (error)
30651974Sbrutus 				break;
30660Sstevel@tonic-gate 		} else {
30670Sstevel@tonic-gate 			/*
30680Sstevel@tonic-gate 			 * We only handle data, save for caller to handle.
30690Sstevel@tonic-gate 			 */
30700Sstevel@tonic-gate 			if (pmp != NULL) {
30710Sstevel@tonic-gate 				pmp->b_cont = nmp->b_cont;
30720Sstevel@tonic-gate 			}
30730Sstevel@tonic-gate 			nmp->b_cont = NULL;
30740Sstevel@tonic-gate 			if (*rmp == NULL) {
30750Sstevel@tonic-gate 				*rmp = nmp;
30760Sstevel@tonic-gate 			} else {
30771974Sbrutus 				tmp->b_cont = nmp;
30780Sstevel@tonic-gate 			}
30790Sstevel@tonic-gate 			nmp = nmp->b_cont;
30800Sstevel@tonic-gate 			tmp = nmp;
30810Sstevel@tonic-gate 		}
30820Sstevel@tonic-gate 	}
30830Sstevel@tonic-gate 	if (pmp != NULL) {
30840Sstevel@tonic-gate 		/* Free any mblk_t(s) which we have consumed */
30850Sstevel@tonic-gate 		pmp->b_cont = NULL;
30868348SEric.Yu@Sun.COM 		freemsg(sti->sti_nl7c_rcv_mp);
30878348SEric.Yu@Sun.COM 	}
30888348SEric.Yu@Sun.COM 	if ((sti->sti_nl7c_rcv_mp = nmp) == NULL) {
30891974Sbrutus 		/* Last mblk_t so return the saved kstrgetmsg() rval/error */
30901974Sbrutus 		if (error == 0) {
30918348SEric.Yu@Sun.COM 			rval_t	*p = (rval_t *)&sti->sti_nl7c_rcv_rval;
30921974Sbrutus 
30931974Sbrutus 			error = p->r_v.r_v2;
30941974Sbrutus 			p->r_v.r_v2 = 0;
30951974Sbrutus 		}
30968348SEric.Yu@Sun.COM 		rp->r_vals = sti->sti_nl7c_rcv_rval;
30978348SEric.Yu@Sun.COM 		sti->sti_nl7c_rcv_rval = 0;
30980Sstevel@tonic-gate 	} else {
30990Sstevel@tonic-gate 		/* More mblk_t(s) to process so no rval to return */
31000Sstevel@tonic-gate 		rp->r_vals = 0;
31010Sstevel@tonic-gate 	}
31020Sstevel@tonic-gate 	return (error);
31030Sstevel@tonic-gate }
31040Sstevel@tonic-gate /*
31050Sstevel@tonic-gate  * Receive the next message on the queue.
31060Sstevel@tonic-gate  * If msg_controllen is non-zero when called the caller is interested in
31070Sstevel@tonic-gate  * any received control info (options).
31080Sstevel@tonic-gate  * If msg_namelen is non-zero when called the caller is interested in
31090Sstevel@tonic-gate  * any received source address.
31100Sstevel@tonic-gate  * The routine returns with msg_control and msg_name pointing to
31110Sstevel@tonic-gate  * kmem_alloc'ed memory which the caller has to free.
31120Sstevel@tonic-gate  */
31138348SEric.Yu@Sun.COM /* ARGSUSED */
31140Sstevel@tonic-gate int
31158348SEric.Yu@Sun.COM sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
31168348SEric.Yu@Sun.COM     struct cred *cr)
31170Sstevel@tonic-gate {
31180Sstevel@tonic-gate 	union T_primitives	*tpr;
31190Sstevel@tonic-gate 	mblk_t			*mp;
31200Sstevel@tonic-gate 	uchar_t			pri;
31210Sstevel@tonic-gate 	int			pflag, opflag;
31220Sstevel@tonic-gate 	void			*control;
31230Sstevel@tonic-gate 	t_uscalar_t		controllen;
31240Sstevel@tonic-gate 	t_uscalar_t		namelen;
31250Sstevel@tonic-gate 	int			so_state = so->so_state; /* Snapshot */
31260Sstevel@tonic-gate 	ssize_t			saved_resid;
31270Sstevel@tonic-gate 	rval_t			rval;
31280Sstevel@tonic-gate 	int			flags;
31290Sstevel@tonic-gate 	clock_t			timout;
31306707Sbrutus 	int			error = 0;
31318348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
31320Sstevel@tonic-gate 
31330Sstevel@tonic-gate 	flags = msg->msg_flags;
31340Sstevel@tonic-gate 	msg->msg_flags = 0;
31350Sstevel@tonic-gate 
31360Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
31377240Srh87107 	    (void *)so, (void *)msg, flags,
31385240Snordmark 	    pr_state(so->so_state, so->so_mode), so->so_error));
31390Sstevel@tonic-gate 
31408348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
31418348SEric.Yu@Sun.COM 		so_update_attrs(so, SOACC);
31428348SEric.Yu@Sun.COM 		/* The imaginary "sockmod" has been popped - act as a stream */
31438348SEric.Yu@Sun.COM 		return (strread(SOTOV(so), uiop, cr));
31448348SEric.Yu@Sun.COM 	}
31458348SEric.Yu@Sun.COM 
31460Sstevel@tonic-gate 	/*
31470Sstevel@tonic-gate 	 * If we are not connected because we have never been connected
31480Sstevel@tonic-gate 	 * we return ENOTCONN. If we have been connected (but are no longer
31490Sstevel@tonic-gate 	 * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
31500Sstevel@tonic-gate 	 * the EOF.
31510Sstevel@tonic-gate 	 *
31520Sstevel@tonic-gate 	 * An alternative would be to post an ENOTCONN error in stream head
31530Sstevel@tonic-gate 	 * (read+write) and clear it when we're connected. However, that error
31540Sstevel@tonic-gate 	 * would cause incorrect poll/select behavior!
31550Sstevel@tonic-gate 	 */
31560Sstevel@tonic-gate 	if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
31570Sstevel@tonic-gate 	    (so->so_mode & SM_CONNREQUIRED)) {
31580Sstevel@tonic-gate 		return (ENOTCONN);
31590Sstevel@tonic-gate 	}
31600Sstevel@tonic-gate 
31610Sstevel@tonic-gate 	/*
31620Sstevel@tonic-gate 	 * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
31630Sstevel@tonic-gate 	 * after checking that the read queue is empty) and returns zero.
31640Sstevel@tonic-gate 	 * This implementation will sleep (in kstrgetmsg) even if uio_resid
31650Sstevel@tonic-gate 	 * is zero.
31660Sstevel@tonic-gate 	 */
31670Sstevel@tonic-gate 
31680Sstevel@tonic-gate 	if (flags & MSG_OOB) {
31690Sstevel@tonic-gate 		/* Check that the transport supports OOB */
31700Sstevel@tonic-gate 		if (!(so->so_mode & SM_EXDATA))
31710Sstevel@tonic-gate 			return (EOPNOTSUPP);
31728348SEric.Yu@Sun.COM 		so_update_attrs(so, SOACC);
31738348SEric.Yu@Sun.COM 		return (sorecvoob(so, msg, uiop, flags,
31748348SEric.Yu@Sun.COM 		    (so->so_options & SO_OOBINLINE)));
31758348SEric.Yu@Sun.COM 	}
31768348SEric.Yu@Sun.COM 
31778348SEric.Yu@Sun.COM 	so_update_attrs(so, SOACC);
31780Sstevel@tonic-gate 
31790Sstevel@tonic-gate 	/*
31800Sstevel@tonic-gate 	 * Set msg_controllen and msg_namelen to zero here to make it
31810Sstevel@tonic-gate 	 * simpler in the cases that no control or name is returned.
31820Sstevel@tonic-gate 	 */
31830Sstevel@tonic-gate 	controllen = msg->msg_controllen;
31840Sstevel@tonic-gate 	namelen = msg->msg_namelen;
31850Sstevel@tonic-gate 	msg->msg_controllen = 0;
31860Sstevel@tonic-gate 	msg->msg_namelen = 0;
31870Sstevel@tonic-gate 
31880Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
31895240Snordmark 	    namelen, controllen));
31900Sstevel@tonic-gate 
31911974Sbrutus 	mutex_enter(&so->so_lock);
31920Sstevel@tonic-gate 	/*
31930Sstevel@tonic-gate 	 * If an NL7C enabled socket and not waiting for write data.
31940Sstevel@tonic-gate 	 */
31958348SEric.Yu@Sun.COM 	if ((sti->sti_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) ==
31960Sstevel@tonic-gate 	    NL7C_ENABLED) {
31978348SEric.Yu@Sun.COM 		if (sti->sti_nl7c_uri) {
31981974Sbrutus 			/* Close uri processing for a previous request */
31990Sstevel@tonic-gate 			nl7c_close(so);
32000Sstevel@tonic-gate 		}
32018348SEric.Yu@Sun.COM 		if ((so_state & SS_CANTRCVMORE) &&
32028348SEric.Yu@Sun.COM 		    sti->sti_nl7c_rcv_mp == NULL) {
32031974Sbrutus 			/* Nothing to process, EOF */
32041974Sbrutus 			mutex_exit(&so->so_lock);
32051974Sbrutus 			return (0);
32068348SEric.Yu@Sun.COM 		} else if (sti->sti_nl7c_flags & NL7C_SOPERSIST) {
32071974Sbrutus 			/* Persistent NL7C socket, try to process request */
32081974Sbrutus 			boolean_t ret;
32091974Sbrutus 
32101974Sbrutus 			ret = nl7c_process(so,
32111974Sbrutus 			    (so->so_state & (SS_NONBLOCK|SS_NDELAY)));
32128348SEric.Yu@Sun.COM 			rval.r_vals = sti->sti_nl7c_rcv_rval;
32131974Sbrutus 			error = rval.r_v.r_v2;
32141974Sbrutus 			if (error) {
32151974Sbrutus 				/* Error of some sort, return it */
32161974Sbrutus 				mutex_exit(&so->so_lock);
32171974Sbrutus 				return (error);
32181974Sbrutus 			}
32198348SEric.Yu@Sun.COM 			if (sti->sti_nl7c_flags &&
32208348SEric.Yu@Sun.COM 			    ! (sti->sti_nl7c_flags & NL7C_WAITWRITE)) {
32211974Sbrutus 				/*
32221974Sbrutus 				 * Still an NL7C socket and no data
32231974Sbrutus 				 * to pass up to the caller.
32241974Sbrutus 				 */
32251974Sbrutus 				mutex_exit(&so->so_lock);
32261974Sbrutus 				if (ret) {
32271974Sbrutus 					/* EOF */
32281974Sbrutus 					return (0);
32291974Sbrutus 				} else {
32301974Sbrutus 					/* Need more data */
32311974Sbrutus 					return (EAGAIN);
32321974Sbrutus 				}
32331974Sbrutus 			}
32341974Sbrutus 		} else {
32350Sstevel@tonic-gate 			/*
32361974Sbrutus 			 * Not persistent so no further NL7C processing.
32370Sstevel@tonic-gate 			 */
32388348SEric.Yu@Sun.COM 			sti->sti_nl7c_flags = 0;
32390Sstevel@tonic-gate 		}
32400Sstevel@tonic-gate 	}
32410Sstevel@tonic-gate 	/*
32420Sstevel@tonic-gate 	 * Only one reader is allowed at any given time. This is needed
32430Sstevel@tonic-gate 	 * for T_EXDATA handling and, in the future, MSG_WAITALL.
32440Sstevel@tonic-gate 	 *
32450Sstevel@tonic-gate 	 * This is slightly different that BSD behavior in that it fails with
32460Sstevel@tonic-gate 	 * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
32470Sstevel@tonic-gate 	 * is single-threaded using sblock(), which is dropped while waiting
32480Sstevel@tonic-gate 	 * for data to appear. The difference shows up e.g. if one
32490Sstevel@tonic-gate 	 * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
32500Sstevel@tonic-gate 	 * does use nonblocking io and different threads are reading each
32510Sstevel@tonic-gate 	 * file descriptor. In BSD there would never be an EWOULDBLOCK error
32520Sstevel@tonic-gate 	 * in this case as long as the read queue doesn't get empty.
32530Sstevel@tonic-gate 	 * In this implementation the thread using nonblocking io can
32540Sstevel@tonic-gate 	 * get an EWOULDBLOCK error due to the blocking thread executing
32550Sstevel@tonic-gate 	 * e.g. in the uiomove in kstrgetmsg.
32560Sstevel@tonic-gate 	 * This difference is not believed to be significant.
32570Sstevel@tonic-gate 	 */
32583749Sethindra 	/* Set SOREADLOCKED */
32593749Sethindra 	error = so_lock_read_intr(so,
32603749Sethindra 	    uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
32610Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
32620Sstevel@tonic-gate 	if (error)
32630Sstevel@tonic-gate 		return (error);
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 	/*
32660Sstevel@tonic-gate 	 * Tell kstrgetmsg to not inspect the stream head errors until all
32670Sstevel@tonic-gate 	 * queued data has been consumed.
32680Sstevel@tonic-gate 	 * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
32690Sstevel@tonic-gate 	 * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
32700Sstevel@tonic-gate 	 *
32710Sstevel@tonic-gate 	 * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
32720Sstevel@tonic-gate 	 * to T_OPTDATA_IND that do not contain any user-visible control msg.
32730Sstevel@tonic-gate 	 * Note that MSG_WAITALL set with MSG_PEEK is a noop.
32740Sstevel@tonic-gate 	 */
32750Sstevel@tonic-gate 	pflag = MSG_ANY | MSG_DELAYERROR;
32760Sstevel@tonic-gate 	if (flags & MSG_PEEK) {
32770Sstevel@tonic-gate 		pflag |= MSG_IPEEK;
32780Sstevel@tonic-gate 		flags &= ~MSG_WAITALL;
32790Sstevel@tonic-gate 	}
32800Sstevel@tonic-gate 	if (so->so_mode & SM_ATOMIC)
32810Sstevel@tonic-gate 		pflag |= MSG_DISCARDTAIL;
32820Sstevel@tonic-gate 
32830Sstevel@tonic-gate 	if (flags & MSG_DONTWAIT)
32840Sstevel@tonic-gate 		timout = 0;
32850Sstevel@tonic-gate 	else
32860Sstevel@tonic-gate 		timout = -1;
32870Sstevel@tonic-gate 	opflag = pflag;
32880Sstevel@tonic-gate retry:
32890Sstevel@tonic-gate 	saved_resid = uiop->uio_resid;
32900Sstevel@tonic-gate 	pri = 0;
32910Sstevel@tonic-gate 	mp = NULL;
32928348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_rcv_mp != NULL) {
32931974Sbrutus 		/* Already kstrgetmsg()ed saved mblk(s) from NL7C */
32940Sstevel@tonic-gate 		error = nl7c_sorecv(so, &mp, uiop, &rval);
32950Sstevel@tonic-gate 	} else {
32960Sstevel@tonic-gate 		error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
32970Sstevel@tonic-gate 		    timout, &rval);
32980Sstevel@tonic-gate 	}
32998348SEric.Yu@Sun.COM 	if (error != 0) {
33008348SEric.Yu@Sun.COM 		/* kstrgetmsg returns ETIME when timeout expires */
33018348SEric.Yu@Sun.COM 		if (error == ETIME)
33028348SEric.Yu@Sun.COM 			error = EWOULDBLOCK;
33036707Sbrutus 		goto out;
33040Sstevel@tonic-gate 	}
33050Sstevel@tonic-gate 	/*
33060Sstevel@tonic-gate 	 * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
33070Sstevel@tonic-gate 	 * For non-datagrams MOREDATA is used to set MSG_EOR.
33080Sstevel@tonic-gate 	 */
33090Sstevel@tonic-gate 	ASSERT(!(rval.r_val1 & MORECTL));
33100Sstevel@tonic-gate 	if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
33110Sstevel@tonic-gate 		msg->msg_flags |= MSG_TRUNC;
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 	if (mp == NULL) {
33140Sstevel@tonic-gate 		dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
33150Sstevel@tonic-gate 		/*
33160Sstevel@tonic-gate 		 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
33170Sstevel@tonic-gate 		 * The draft Posix socket spec states that the mark should
33180Sstevel@tonic-gate 		 * not be cleared when peeking. We follow the latter.
33190Sstevel@tonic-gate 		 */
33200Sstevel@tonic-gate 		if ((so->so_state &
33210Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
33220Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
33230Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
33240Sstevel@tonic-gate 			sorecv_update_oobstate(so);
33250Sstevel@tonic-gate 		}
33260Sstevel@tonic-gate 
33270Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
33280Sstevel@tonic-gate 		/* Set MSG_EOR based on MOREDATA */
33290Sstevel@tonic-gate 		if (!(rval.r_val1 & MOREDATA)) {
33300Sstevel@tonic-gate 			if (so->so_state & SS_SAVEDEOR) {
33310Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
33320Sstevel@tonic-gate 				so->so_state &= ~SS_SAVEDEOR;
33330Sstevel@tonic-gate 			}
33340Sstevel@tonic-gate 		}
33350Sstevel@tonic-gate 		/*
33360Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
33370Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
33380Sstevel@tonic-gate 		 */
33390Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
33400Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
33410Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
33420Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
33430Sstevel@tonic-gate 			goto retry;
33440Sstevel@tonic-gate 		}
33456707Sbrutus 		goto out_locked;
33460Sstevel@tonic-gate 	}
33470Sstevel@tonic-gate 
33480Sstevel@tonic-gate 	/* strsock_proto has already verified length and alignment */
33490Sstevel@tonic-gate 	tpr = (union T_primitives *)mp->b_rptr;
33500Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
33510Sstevel@tonic-gate 
33520Sstevel@tonic-gate 	switch (tpr->type) {
33530Sstevel@tonic-gate 	case T_DATA_IND: {
33540Sstevel@tonic-gate 		if ((so->so_state &
33550Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
33560Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
33570Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
33580Sstevel@tonic-gate 			sorecv_update_oobstate(so);
33590Sstevel@tonic-gate 		}
33600Sstevel@tonic-gate 
33610Sstevel@tonic-gate 		/*
33620Sstevel@tonic-gate 		 * Set msg_flags to MSG_EOR based on
33630Sstevel@tonic-gate 		 * MORE_flag and MOREDATA.
33640Sstevel@tonic-gate 		 */
33650Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
33660Sstevel@tonic-gate 		so->so_state &= ~SS_SAVEDEOR;
33670Sstevel@tonic-gate 		if (!(tpr->data_ind.MORE_flag & 1)) {
33680Sstevel@tonic-gate 			if (!(rval.r_val1 & MOREDATA))
33690Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
33700Sstevel@tonic-gate 			else
33710Sstevel@tonic-gate 				so->so_state |= SS_SAVEDEOR;
33720Sstevel@tonic-gate 		}
33730Sstevel@tonic-gate 		freemsg(mp);
33740Sstevel@tonic-gate 		/*
33750Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
33760Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
33770Sstevel@tonic-gate 		 */
33780Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
33790Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
33800Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
33810Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
33820Sstevel@tonic-gate 			goto retry;
33830Sstevel@tonic-gate 		}
33846707Sbrutus 		goto out_locked;
33850Sstevel@tonic-gate 	}
33860Sstevel@tonic-gate 	case T_UNITDATA_IND: {
33870Sstevel@tonic-gate 		void *addr;
33880Sstevel@tonic-gate 		t_uscalar_t addrlen;
33890Sstevel@tonic-gate 		void *abuf;
33900Sstevel@tonic-gate 		t_uscalar_t optlen;
33910Sstevel@tonic-gate 		void *opt;
33920Sstevel@tonic-gate 
33930Sstevel@tonic-gate 		if ((so->so_state &
33940Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
33950Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
33960Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
33970Sstevel@tonic-gate 			sorecv_update_oobstate(so);
33980Sstevel@tonic-gate 		}
33990Sstevel@tonic-gate 
34000Sstevel@tonic-gate 		if (namelen != 0) {
34010Sstevel@tonic-gate 			/* Caller wants source address */
34020Sstevel@tonic-gate 			addrlen = tpr->unitdata_ind.SRC_length;
34030Sstevel@tonic-gate 			addr = sogetoff(mp,
34045240Snordmark 			    tpr->unitdata_ind.SRC_offset,
34055240Snordmark 			    addrlen, 1);
34060Sstevel@tonic-gate 			if (addr == NULL) {
34070Sstevel@tonic-gate 				freemsg(mp);
34080Sstevel@tonic-gate 				error = EPROTO;
34090Sstevel@tonic-gate 				eprintsoline(so, error);
34106707Sbrutus 				goto out;
34110Sstevel@tonic-gate 			}
34120Sstevel@tonic-gate 			if (so->so_family == AF_UNIX) {
34130Sstevel@tonic-gate 				/*
34140Sstevel@tonic-gate 				 * Can not use the transport level address.
34150Sstevel@tonic-gate 				 * If there is a SO_SRCADDR option carrying
34160Sstevel@tonic-gate 				 * the socket level address it will be
34170Sstevel@tonic-gate 				 * extracted below.
34180Sstevel@tonic-gate 				 */
34190Sstevel@tonic-gate 				addr = NULL;
34200Sstevel@tonic-gate 				addrlen = 0;
34210Sstevel@tonic-gate 			}
34220Sstevel@tonic-gate 		}
34230Sstevel@tonic-gate 		optlen = tpr->unitdata_ind.OPT_length;
34240Sstevel@tonic-gate 		if (optlen != 0) {
34250Sstevel@tonic-gate 			t_uscalar_t ncontrollen;
34260Sstevel@tonic-gate 
34270Sstevel@tonic-gate 			/*
34280Sstevel@tonic-gate 			 * Extract any source address option.
34290Sstevel@tonic-gate 			 * Determine how large cmsg buffer is needed.
34300Sstevel@tonic-gate 			 */
34310Sstevel@tonic-gate 			opt = sogetoff(mp,
34325240Snordmark 			    tpr->unitdata_ind.OPT_offset,
34335240Snordmark 			    optlen, __TPI_ALIGN_SIZE);
34340Sstevel@tonic-gate 
34350Sstevel@tonic-gate 			if (opt == NULL) {
34360Sstevel@tonic-gate 				freemsg(mp);
34370Sstevel@tonic-gate 				error = EPROTO;
34380Sstevel@tonic-gate 				eprintsoline(so, error);
34396707Sbrutus 				goto out;
34400Sstevel@tonic-gate 			}
34410Sstevel@tonic-gate 			if (so->so_family == AF_UNIX)
34420Sstevel@tonic-gate 				so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
34430Sstevel@tonic-gate 			ncontrollen = so_cmsglen(mp, opt, optlen,
34445240Snordmark 			    !(flags & MSG_XPG4_2));
34450Sstevel@tonic-gate 			if (controllen != 0)
34460Sstevel@tonic-gate 				controllen = ncontrollen;
34470Sstevel@tonic-gate 			else if (ncontrollen != 0)
34480Sstevel@tonic-gate 				msg->msg_flags |= MSG_CTRUNC;
34490Sstevel@tonic-gate 		} else {
34500Sstevel@tonic-gate 			controllen = 0;
34510Sstevel@tonic-gate 		}
34520Sstevel@tonic-gate 
34530Sstevel@tonic-gate 		if (namelen != 0) {
34540Sstevel@tonic-gate 			/*
34550Sstevel@tonic-gate 			 * Return address to caller.
34560Sstevel@tonic-gate 			 * Caller handles truncation if length
34570Sstevel@tonic-gate 			 * exceeds msg_namelen.
34580Sstevel@tonic-gate 			 * NOTE: AF_UNIX NUL termination is ensured by
34590Sstevel@tonic-gate 			 * the sender's copyin_name().
34600Sstevel@tonic-gate 			 */
34610Sstevel@tonic-gate 			abuf = kmem_alloc(addrlen, KM_SLEEP);
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 			bcopy(addr, abuf, addrlen);
34640Sstevel@tonic-gate 			msg->msg_name = abuf;
34650Sstevel@tonic-gate 			msg->msg_namelen = addrlen;
34660Sstevel@tonic-gate 		}
34670Sstevel@tonic-gate 
34680Sstevel@tonic-gate 		if (controllen != 0) {
34690Sstevel@tonic-gate 			/*
34700Sstevel@tonic-gate 			 * Return control msg to caller.
34710Sstevel@tonic-gate 			 * Caller handles truncation if length
34720Sstevel@tonic-gate 			 * exceeds msg_controllen.
34730Sstevel@tonic-gate 			 */
34744420Samehta 			control = kmem_zalloc(controllen, KM_SLEEP);
34750Sstevel@tonic-gate 
34760Sstevel@tonic-gate 			error = so_opt2cmsg(mp, opt, optlen,
34775240Snordmark 			    !(flags & MSG_XPG4_2),
34785240Snordmark 			    control, controllen);
34790Sstevel@tonic-gate 			if (error) {
34800Sstevel@tonic-gate 				freemsg(mp);
34810Sstevel@tonic-gate 				if (msg->msg_namelen != 0)
34820Sstevel@tonic-gate 					kmem_free(msg->msg_name,
34835240Snordmark 					    msg->msg_namelen);
34840Sstevel@tonic-gate 				kmem_free(control, controllen);
34850Sstevel@tonic-gate 				eprintsoline(so, error);
34866707Sbrutus 				goto out;
34870Sstevel@tonic-gate 			}
34880Sstevel@tonic-gate 			msg->msg_control = control;
34890Sstevel@tonic-gate 			msg->msg_controllen = controllen;
34900Sstevel@tonic-gate 		}
34910Sstevel@tonic-gate 
34920Sstevel@tonic-gate 		freemsg(mp);
34936707Sbrutus 		goto out;
34940Sstevel@tonic-gate 	}
34950Sstevel@tonic-gate 	case T_OPTDATA_IND: {
34960Sstevel@tonic-gate 		struct T_optdata_req *tdr;
34970Sstevel@tonic-gate 		void *opt;
34980Sstevel@tonic-gate 		t_uscalar_t optlen;
34990Sstevel@tonic-gate 
35000Sstevel@tonic-gate 		if ((so->so_state &
35010Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
35020Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
35030Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
35040Sstevel@tonic-gate 			sorecv_update_oobstate(so);
35050Sstevel@tonic-gate 		}
35060Sstevel@tonic-gate 
35070Sstevel@tonic-gate 		tdr = (struct T_optdata_req *)mp->b_rptr;
35080Sstevel@tonic-gate 		optlen = tdr->OPT_length;
35090Sstevel@tonic-gate 		if (optlen != 0) {
35100Sstevel@tonic-gate 			t_uscalar_t ncontrollen;
35110Sstevel@tonic-gate 			/*
35120Sstevel@tonic-gate 			 * Determine how large cmsg buffer is needed.
35130Sstevel@tonic-gate 			 */
35140Sstevel@tonic-gate 			opt = sogetoff(mp,
35155240Snordmark 			    tpr->optdata_ind.OPT_offset,
35165240Snordmark 			    optlen, __TPI_ALIGN_SIZE);
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 			if (opt == NULL) {
35190Sstevel@tonic-gate 				freemsg(mp);
35200Sstevel@tonic-gate 				error = EPROTO;
35210Sstevel@tonic-gate 				eprintsoline(so, error);
35226707Sbrutus 				goto out;
35230Sstevel@tonic-gate 			}
35240Sstevel@tonic-gate 
35250Sstevel@tonic-gate 			ncontrollen = so_cmsglen(mp, opt, optlen,
35265240Snordmark 			    !(flags & MSG_XPG4_2));
35270Sstevel@tonic-gate 			if (controllen != 0)
35280Sstevel@tonic-gate 				controllen = ncontrollen;
35290Sstevel@tonic-gate 			else if (ncontrollen != 0)
35300Sstevel@tonic-gate 				msg->msg_flags |= MSG_CTRUNC;
35310Sstevel@tonic-gate 		} else {
35320Sstevel@tonic-gate 			controllen = 0;
35330Sstevel@tonic-gate 		}
35340Sstevel@tonic-gate 
35350Sstevel@tonic-gate 		if (controllen != 0) {
35360Sstevel@tonic-gate 			/*
35370Sstevel@tonic-gate 			 * Return control msg to caller.
35380Sstevel@tonic-gate 			 * Caller handles truncation if length
35390Sstevel@tonic-gate 			 * exceeds msg_controllen.
35400Sstevel@tonic-gate 			 */
35414420Samehta 			control = kmem_zalloc(controllen, KM_SLEEP);
35420Sstevel@tonic-gate 
35430Sstevel@tonic-gate 			error = so_opt2cmsg(mp, opt, optlen,
35445240Snordmark 			    !(flags & MSG_XPG4_2),
35455240Snordmark 			    control, controllen);
35460Sstevel@tonic-gate 			if (error) {
35470Sstevel@tonic-gate 				freemsg(mp);
35480Sstevel@tonic-gate 				kmem_free(control, controllen);
35490Sstevel@tonic-gate 				eprintsoline(so, error);
35506707Sbrutus 				goto out;
35510Sstevel@tonic-gate 			}
35520Sstevel@tonic-gate 			msg->msg_control = control;
35530Sstevel@tonic-gate 			msg->msg_controllen = controllen;
35540Sstevel@tonic-gate 		}
35550Sstevel@tonic-gate 
35560Sstevel@tonic-gate 		/*
35570Sstevel@tonic-gate 		 * Set msg_flags to MSG_EOR based on
35580Sstevel@tonic-gate 		 * DATA_flag and MOREDATA.
35590Sstevel@tonic-gate 		 */
35600Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
35610Sstevel@tonic-gate 		so->so_state &= ~SS_SAVEDEOR;
35620Sstevel@tonic-gate 		if (!(tpr->data_ind.MORE_flag & 1)) {
35630Sstevel@tonic-gate 			if (!(rval.r_val1 & MOREDATA))
35640Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
35650Sstevel@tonic-gate 			else
35660Sstevel@tonic-gate 				so->so_state |= SS_SAVEDEOR;
35670Sstevel@tonic-gate 		}
35680Sstevel@tonic-gate 		freemsg(mp);
35690Sstevel@tonic-gate 		/*
35700Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
35710Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
35720Sstevel@tonic-gate 		 * Not possible to wait if control info was received.
35730Sstevel@tonic-gate 		 */
35740Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
35750Sstevel@tonic-gate 		    controllen == 0 &&
35760Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
35770Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
35780Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
35790Sstevel@tonic-gate 			goto retry;
35800Sstevel@tonic-gate 		}
35816707Sbrutus 		goto out_locked;
35820Sstevel@tonic-gate 	}
35830Sstevel@tonic-gate 	case T_EXDATA_IND: {
35840Sstevel@tonic-gate 		dprintso(so, 1,
35855240Snordmark 		    ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
35865240Snordmark 		    "state %s\n",
35878348SEric.Yu@Sun.COM 		    sti->sti_oobsigcnt, sti->sti_oobcnt,
35885240Snordmark 		    saved_resid - uiop->uio_resid,
35895240Snordmark 		    pr_state(so->so_state, so->so_mode)));
35900Sstevel@tonic-gate 		/*
35910Sstevel@tonic-gate 		 * kstrgetmsg handles MSGMARK so there is nothing to
35920Sstevel@tonic-gate 		 * inspect in the T_EXDATA_IND.
35930Sstevel@tonic-gate 		 * strsock_proto makes the stream head queue the T_EXDATA_IND
35940Sstevel@tonic-gate 		 * as a separate message with no M_DATA component. Furthermore,
35950Sstevel@tonic-gate 		 * the stream head does not consolidate M_DATA messages onto
35960Sstevel@tonic-gate 		 * an MSGMARK'ed message ensuring that the T_EXDATA_IND
35970Sstevel@tonic-gate 		 * remains a message by itself. This is needed since MSGMARK
35980Sstevel@tonic-gate 		 * marks both the whole message as well as the last byte
35990Sstevel@tonic-gate 		 * of the message.
36000Sstevel@tonic-gate 		 */
36010Sstevel@tonic-gate 		freemsg(mp);
36020Sstevel@tonic-gate 		ASSERT(uiop->uio_resid == saved_resid);	/* No data */
36030Sstevel@tonic-gate 		if (flags & MSG_PEEK) {
36040Sstevel@tonic-gate 			/*
36050Sstevel@tonic-gate 			 * Even though we are peeking we consume the
36060Sstevel@tonic-gate 			 * T_EXDATA_IND thereby moving the mark information
36070Sstevel@tonic-gate 			 * to SS_RCVATMARK. Then the oob code below will
36080Sstevel@tonic-gate 			 * retry the peeking kstrgetmsg.
36090Sstevel@tonic-gate 			 * Note that the stream head read queue is
36100Sstevel@tonic-gate 			 * never flushed without holding SOREADLOCKED
36110Sstevel@tonic-gate 			 * thus the T_EXDATA_IND can not disappear
36120Sstevel@tonic-gate 			 * underneath us.
36130Sstevel@tonic-gate 			 */
36140Sstevel@tonic-gate 			dprintso(so, 1,
36155240Snordmark 			    ("sotpi_recvmsg: consume EXDATA_IND "
36165240Snordmark 			    "counts %d/%d state %s\n",
36178348SEric.Yu@Sun.COM 			    sti->sti_oobsigcnt,
36188348SEric.Yu@Sun.COM 			    sti->sti_oobcnt,
36195240Snordmark 			    pr_state(so->so_state, so->so_mode)));
36200Sstevel@tonic-gate 
36210Sstevel@tonic-gate 			pflag = MSG_ANY | MSG_DELAYERROR;
36220Sstevel@tonic-gate 			if (so->so_mode & SM_ATOMIC)
36230Sstevel@tonic-gate 				pflag |= MSG_DISCARDTAIL;
36240Sstevel@tonic-gate 
36250Sstevel@tonic-gate 			pri = 0;
36260Sstevel@tonic-gate 			mp = NULL;
36270Sstevel@tonic-gate 
36280Sstevel@tonic-gate 			error = kstrgetmsg(SOTOV(so), &mp, uiop,
36295240Snordmark 			    &pri, &pflag, (clock_t)-1, &rval);
36300Sstevel@tonic-gate 			ASSERT(uiop->uio_resid == saved_resid);
36310Sstevel@tonic-gate 
36320Sstevel@tonic-gate 			if (error) {
36330Sstevel@tonic-gate #ifdef SOCK_DEBUG
36340Sstevel@tonic-gate 				if (error != EWOULDBLOCK && error != EINTR) {
36350Sstevel@tonic-gate 					eprintsoline(so, error);
36360Sstevel@tonic-gate 				}
36370Sstevel@tonic-gate #endif /* SOCK_DEBUG */
36386707Sbrutus 				goto out;
36390Sstevel@tonic-gate 			}
36400Sstevel@tonic-gate 			ASSERT(mp);
36410Sstevel@tonic-gate 			tpr = (union T_primitives *)mp->b_rptr;
36420Sstevel@tonic-gate 			ASSERT(tpr->type == T_EXDATA_IND);
36430Sstevel@tonic-gate 			freemsg(mp);
36440Sstevel@tonic-gate 		} /* end "if (flags & MSG_PEEK)" */
36450Sstevel@tonic-gate 
36460Sstevel@tonic-gate 		/*
36470Sstevel@tonic-gate 		 * Decrement the number of queued and pending oob.
36480Sstevel@tonic-gate 		 *
36490Sstevel@tonic-gate 		 * SS_RCVATMARK is cleared when we read past a mark.
36500Sstevel@tonic-gate 		 * SS_HAVEOOBDATA is cleared when we've read past the
36510Sstevel@tonic-gate 		 * last mark.
36520Sstevel@tonic-gate 		 * SS_OOBPEND is cleared if we've read past the last
36530Sstevel@tonic-gate 		 * mark and no (new) SIGURG has been posted.
36540Sstevel@tonic-gate 		 */
36550Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
36560Sstevel@tonic-gate 		ASSERT(so_verify_oobstate(so));
36578348SEric.Yu@Sun.COM 		ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
36588348SEric.Yu@Sun.COM 		ASSERT(sti->sti_oobsigcnt > 0);
36598348SEric.Yu@Sun.COM 		sti->sti_oobsigcnt--;
36608348SEric.Yu@Sun.COM 		ASSERT(sti->sti_oobcnt > 0);
36618348SEric.Yu@Sun.COM 		sti->sti_oobcnt--;
36620Sstevel@tonic-gate 		/*
36630Sstevel@tonic-gate 		 * Since the T_EXDATA_IND has been removed from the stream
36640Sstevel@tonic-gate 		 * head, but we have not read data past the mark,
36650Sstevel@tonic-gate 		 * sockfs needs to track that the socket is still at the mark.
36660Sstevel@tonic-gate 		 *
36670Sstevel@tonic-gate 		 * Since no data was received call kstrgetmsg again to wait
36680Sstevel@tonic-gate 		 * for data.
36690Sstevel@tonic-gate 		 */
36700Sstevel@tonic-gate 		so->so_state |= SS_RCVATMARK;
36710Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
36720Sstevel@tonic-gate 		dprintso(so, 1,
36730Sstevel@tonic-gate 		    ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n",
36748348SEric.Yu@Sun.COM 		    sti->sti_oobsigcnt, sti->sti_oobcnt,
36750Sstevel@tonic-gate 		    pr_state(so->so_state, so->so_mode)));
36760Sstevel@tonic-gate 		pflag = opflag;
36770Sstevel@tonic-gate 		goto retry;
36780Sstevel@tonic-gate 	}
36790Sstevel@tonic-gate 	default:
36808348SEric.Yu@Sun.COM 		cmn_err(CE_CONT, "sotpi_recvmsg: so %p prim %d mp %p\n",
36818348SEric.Yu@Sun.COM 		    (void *)so, tpr->type, (void *)mp);
36820Sstevel@tonic-gate 		ASSERT(0);
36830Sstevel@tonic-gate 		freemsg(mp);
36840Sstevel@tonic-gate 		error = EPROTO;
36850Sstevel@tonic-gate 		eprintsoline(so, error);
36866707Sbrutus 		goto out;
36870Sstevel@tonic-gate 	}
36880Sstevel@tonic-gate 	/* NOTREACHED */
36896707Sbrutus out:
36900Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
36916707Sbrutus out_locked:
36920Sstevel@tonic-gate 	so_unlock_read(so);	/* Clear SOREADLOCKED */
36930Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
36940Sstevel@tonic-gate 	return (error);
36950Sstevel@tonic-gate }
36960Sstevel@tonic-gate 
36970Sstevel@tonic-gate /*
36980Sstevel@tonic-gate  * Sending data with options on a datagram socket.
36990Sstevel@tonic-gate  * Assumes caller has verified that SS_ISBOUND etc. are set.
37000Sstevel@tonic-gate  */
37010Sstevel@tonic-gate static int
3702741Smasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3703741Smasputra     struct uio *uiop, void *control, t_uscalar_t controllen, int flags)
37040Sstevel@tonic-gate {
37050Sstevel@tonic-gate 	struct T_unitdata_req	tudr;
37060Sstevel@tonic-gate 	mblk_t			*mp;
37070Sstevel@tonic-gate 	int			error;
37080Sstevel@tonic-gate 	void			*addr;
37090Sstevel@tonic-gate 	socklen_t		addrlen;
37100Sstevel@tonic-gate 	void			*src;
37110Sstevel@tonic-gate 	socklen_t		srclen;
37120Sstevel@tonic-gate 	ssize_t			len;
37130Sstevel@tonic-gate 	int			size;
37140Sstevel@tonic-gate 	struct T_opthdr		toh;
37150Sstevel@tonic-gate 	struct fdbuf		*fdbuf;
37160Sstevel@tonic-gate 	t_uscalar_t		optlen;
37170Sstevel@tonic-gate 	void			*fds;
37180Sstevel@tonic-gate 	int			fdlen;
37198348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
37200Sstevel@tonic-gate 
37210Sstevel@tonic-gate 	ASSERT(name && namelen);
37220Sstevel@tonic-gate 	ASSERT(control && controllen);
37230Sstevel@tonic-gate 
37240Sstevel@tonic-gate 	len = uiop->uio_resid;
37258348SEric.Yu@Sun.COM 	if (len > (ssize_t)sti->sti_tidu_size) {
37260Sstevel@tonic-gate 		return (EMSGSIZE);
37270Sstevel@tonic-gate 	}
37280Sstevel@tonic-gate 
37290Sstevel@tonic-gate 	/*
37300Sstevel@tonic-gate 	 * For AF_UNIX the destination address is translated to an internal
37310Sstevel@tonic-gate 	 * name and the source address is passed as an option.
37320Sstevel@tonic-gate 	 * Also, file descriptors are passed as file pointers in an
37330Sstevel@tonic-gate 	 * option.
37340Sstevel@tonic-gate 	 */
37350Sstevel@tonic-gate 
37360Sstevel@tonic-gate 	/*
37370Sstevel@tonic-gate 	 * Length and family checks.
37380Sstevel@tonic-gate 	 */
37390Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
37400Sstevel@tonic-gate 	if (error) {
37410Sstevel@tonic-gate 		eprintsoline(so, error);
37420Sstevel@tonic-gate 		return (error);
37430Sstevel@tonic-gate 	}
37440Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
37458348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate) {
37460Sstevel@tonic-gate 			/*
37470Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
37480Sstevel@tonic-gate 			 * pass any (transport internal) source address.
37490Sstevel@tonic-gate 			 */
37500Sstevel@tonic-gate 			addr = name;
37510Sstevel@tonic-gate 			addrlen = namelen;
37520Sstevel@tonic-gate 			src = NULL;
37530Sstevel@tonic-gate 			srclen = 0;
37540Sstevel@tonic-gate 		} else {
37550Sstevel@tonic-gate 			/*
37560Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
37570Sstevel@tonic-gate 			 * and translate the remote address.
37580Sstevel@tonic-gate 			 *
37598348SEric.Yu@Sun.COM 			 * Note that this code does not prevent sti_laddr_sa
37600Sstevel@tonic-gate 			 * from changing while it is being used. Thus
37610Sstevel@tonic-gate 			 * if an unbind+bind occurs concurrently with this
37620Sstevel@tonic-gate 			 * send the peer might see a partially new and a
37630Sstevel@tonic-gate 			 * partially old "from" address.
37640Sstevel@tonic-gate 			 */
37658348SEric.Yu@Sun.COM 			src = sti->sti_laddr_sa;
37668348SEric.Yu@Sun.COM 			srclen = (t_uscalar_t)sti->sti_laddr_len;
37670Sstevel@tonic-gate 			dprintso(so, 1,
37680Sstevel@tonic-gate 			    ("sosend_dgramcmsg UNIX: srclen %d, src %p\n",
37690Sstevel@tonic-gate 			    srclen, src));
37700Sstevel@tonic-gate 			error = so_ux_addr_xlate(so, name, namelen,
37715240Snordmark 			    (flags & MSG_XPG4_2),
37725240Snordmark 			    &addr, &addrlen);
37730Sstevel@tonic-gate 			if (error) {
37740Sstevel@tonic-gate 				eprintsoline(so, error);
37750Sstevel@tonic-gate 				return (error);
37760Sstevel@tonic-gate 			}
37770Sstevel@tonic-gate 		}
37780Sstevel@tonic-gate 	} else {
37790Sstevel@tonic-gate 		addr = name;
37800Sstevel@tonic-gate 		addrlen = namelen;
37810Sstevel@tonic-gate 		src = NULL;
37820Sstevel@tonic-gate 		srclen = 0;
37830Sstevel@tonic-gate 	}
37840Sstevel@tonic-gate 	optlen = so_optlen(control, controllen,
37855240Snordmark 	    !(flags & MSG_XPG4_2));
37860Sstevel@tonic-gate 	tudr.PRIM_type = T_UNITDATA_REQ;
37870Sstevel@tonic-gate 	tudr.DEST_length = addrlen;
37880Sstevel@tonic-gate 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
37890Sstevel@tonic-gate 	if (srclen != 0)
37900Sstevel@tonic-gate 		tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) +
37910Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(srclen));
37920Sstevel@tonic-gate 	else
37930Sstevel@tonic-gate 		tudr.OPT_length = optlen;
37940Sstevel@tonic-gate 	tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
37955240Snordmark 	    _TPI_ALIGN_TOPT(addrlen));
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate 	size = tudr.OPT_offset + tudr.OPT_length;
37980Sstevel@tonic-gate 
37990Sstevel@tonic-gate 	/*
38000Sstevel@tonic-gate 	 * File descriptors only when SM_FDPASSING set.
38010Sstevel@tonic-gate 	 */
38020Sstevel@tonic-gate 	error = so_getfdopt(control, controllen,
38035240Snordmark 	    !(flags & MSG_XPG4_2), &fds, &fdlen);
38040Sstevel@tonic-gate 	if (error)
38050Sstevel@tonic-gate 		return (error);
38060Sstevel@tonic-gate 	if (fdlen != -1) {
38070Sstevel@tonic-gate 		if (!(so->so_mode & SM_FDPASSING))
38080Sstevel@tonic-gate 			return (EOPNOTSUPP);
38090Sstevel@tonic-gate 
38100Sstevel@tonic-gate 		error = fdbuf_create(fds, fdlen, &fdbuf);
38110Sstevel@tonic-gate 		if (error)
38120Sstevel@tonic-gate 			return (error);
38130Sstevel@tonic-gate 		mp = fdbuf_allocmsg(size, fdbuf);
38140Sstevel@tonic-gate 	} else {
38158778SErik.Nordmark@Sun.COM 		mp = soallocproto(size, _ALLOC_INTR, CRED());
3816455Smeem 		if (mp == NULL) {
3817455Smeem 			/*
3818455Smeem 			 * Caught a signal waiting for memory.
3819455Smeem 			 * Let send* return EINTR.
3820455Smeem 			 */
3821455Smeem 			return (EINTR);
3822455Smeem 		}
38230Sstevel@tonic-gate 	}
38240Sstevel@tonic-gate 	soappendmsg(mp, &tudr, sizeof (tudr));
38250Sstevel@tonic-gate 	soappendmsg(mp, addr, addrlen);
38260Sstevel@tonic-gate 	mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
38270Sstevel@tonic-gate 
38280Sstevel@tonic-gate 	if (fdlen != -1) {
38290Sstevel@tonic-gate 		ASSERT(fdbuf != NULL);
38300Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
38310Sstevel@tonic-gate 		toh.name = SO_FILEP;
38320Sstevel@tonic-gate 		toh.len = fdbuf->fd_size +
38335240Snordmark 		    (t_uscalar_t)sizeof (struct T_opthdr);
38340Sstevel@tonic-gate 		toh.status = 0;
38350Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
38360Sstevel@tonic-gate 		soappendmsg(mp, fdbuf, fdbuf->fd_size);
38370Sstevel@tonic-gate 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
38380Sstevel@tonic-gate 	}
38390Sstevel@tonic-gate 	if (srclen != 0) {
38400Sstevel@tonic-gate 		/*
38410Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
38420Sstevel@tonic-gate 		 * address option.
38430Sstevel@tonic-gate 		 */
38440Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
38450Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
38460Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
38470Sstevel@tonic-gate 		toh.status = 0;
38480Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
38490Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
38500Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
38510Sstevel@tonic-gate 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
38520Sstevel@tonic-gate 	}
38530Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
38540Sstevel@tonic-gate 	so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
38550Sstevel@tonic-gate 	/* At most 3 bytes left in the message */
38560Sstevel@tonic-gate 	ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
38570Sstevel@tonic-gate 	ASSERT(MBLKL(mp) <= (ssize_t)size);
38580Sstevel@tonic-gate 
38590Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3860*11861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
38610Sstevel@tonic-gate 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
38620Sstevel@tonic-gate 
38630Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
38640Sstevel@tonic-gate #ifdef SOCK_DEBUG
38650Sstevel@tonic-gate 	if (error) {
38660Sstevel@tonic-gate 		eprintsoline(so, error);
38670Sstevel@tonic-gate 	}
38680Sstevel@tonic-gate #endif /* SOCK_DEBUG */
38690Sstevel@tonic-gate 	return (error);
38700Sstevel@tonic-gate }
38710Sstevel@tonic-gate 
38720Sstevel@tonic-gate /*
38730Sstevel@tonic-gate  * Sending data with options on a connected stream socket.
38740Sstevel@tonic-gate  * Assumes caller has verified that SS_ISCONNECTED is set.
38750Sstevel@tonic-gate  */
38760Sstevel@tonic-gate static int
38778348SEric.Yu@Sun.COM sosend_svccmsg(struct sonode *so, struct uio *uiop, int more, void *control,
38788348SEric.Yu@Sun.COM     t_uscalar_t controllen, int flags)
38790Sstevel@tonic-gate {
38800Sstevel@tonic-gate 	struct T_optdata_req	tdr;
38810Sstevel@tonic-gate 	mblk_t			*mp;
38820Sstevel@tonic-gate 	int			error;
38830Sstevel@tonic-gate 	ssize_t			iosize;
38840Sstevel@tonic-gate 	int			size;
38850Sstevel@tonic-gate 	struct fdbuf		*fdbuf;
38860Sstevel@tonic-gate 	t_uscalar_t		optlen;
38870Sstevel@tonic-gate 	void			*fds;
38880Sstevel@tonic-gate 	int			fdlen;
38890Sstevel@tonic-gate 	struct T_opthdr		toh;
38908348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
38910Sstevel@tonic-gate 
38920Sstevel@tonic-gate 	dprintso(so, 1,
38935240Snordmark 	    ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
38940Sstevel@tonic-gate 
38950Sstevel@tonic-gate 	/*
38960Sstevel@tonic-gate 	 * Has to be bound and connected. However, since no locks are
38970Sstevel@tonic-gate 	 * held the state could have changed after sotpi_sendmsg checked it
38980Sstevel@tonic-gate 	 * thus it is not possible to ASSERT on the state.
38990Sstevel@tonic-gate 	 */
39000Sstevel@tonic-gate 
39010Sstevel@tonic-gate 	/* Options on connection-oriented only when SM_OPTDATA set. */
39020Sstevel@tonic-gate 	if (!(so->so_mode & SM_OPTDATA))
39030Sstevel@tonic-gate 		return (EOPNOTSUPP);
39040Sstevel@tonic-gate 
39050Sstevel@tonic-gate 	do {
39060Sstevel@tonic-gate 		/*
39070Sstevel@tonic-gate 		 * Set the MORE flag if uio_resid does not fit in this
39080Sstevel@tonic-gate 		 * message or if the caller passed in "more".
39090Sstevel@tonic-gate 		 * Error for transports with zero tidu_size.
39100Sstevel@tonic-gate 		 */
39110Sstevel@tonic-gate 		tdr.PRIM_type = T_OPTDATA_REQ;
39128348SEric.Yu@Sun.COM 		iosize = sti->sti_tidu_size;
39130Sstevel@tonic-gate 		if (iosize <= 0)
39140Sstevel@tonic-gate 			return (EMSGSIZE);
39150Sstevel@tonic-gate 		if (uiop->uio_resid > iosize) {
39160Sstevel@tonic-gate 			tdr.DATA_flag = 1;
39170Sstevel@tonic-gate 		} else {
39180Sstevel@tonic-gate 			if (more)
39190Sstevel@tonic-gate 				tdr.DATA_flag = 1;
39200Sstevel@tonic-gate 			else
39210Sstevel@tonic-gate 				tdr.DATA_flag = 0;
39220Sstevel@tonic-gate 			iosize = uiop->uio_resid;
39230Sstevel@tonic-gate 		}
39240Sstevel@tonic-gate 		dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
39255240Snordmark 		    tdr.DATA_flag, iosize));
39260Sstevel@tonic-gate 
39270Sstevel@tonic-gate 		optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
39280Sstevel@tonic-gate 		tdr.OPT_length = optlen;
39290Sstevel@tonic-gate 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
39300Sstevel@tonic-gate 
39310Sstevel@tonic-gate 		size = (int)sizeof (tdr) + optlen;
39320Sstevel@tonic-gate 		/*
39330Sstevel@tonic-gate 		 * File descriptors only when SM_FDPASSING set.
39340Sstevel@tonic-gate 		 */
39350Sstevel@tonic-gate 		error = so_getfdopt(control, controllen,
39365240Snordmark 		    !(flags & MSG_XPG4_2), &fds, &fdlen);
39370Sstevel@tonic-gate 		if (error)
39380Sstevel@tonic-gate 			return (error);
39390Sstevel@tonic-gate 		if (fdlen != -1) {
39400Sstevel@tonic-gate 			if (!(so->so_mode & SM_FDPASSING))
39410Sstevel@tonic-gate 				return (EOPNOTSUPP);
39420Sstevel@tonic-gate 
39430Sstevel@tonic-gate 			error = fdbuf_create(fds, fdlen, &fdbuf);
39440Sstevel@tonic-gate 			if (error)
39450Sstevel@tonic-gate 				return (error);
39460Sstevel@tonic-gate 			mp = fdbuf_allocmsg(size, fdbuf);
39470Sstevel@tonic-gate 		} else {
39488778SErik.Nordmark@Sun.COM 			mp = soallocproto(size, _ALLOC_INTR, CRED());
3949455Smeem 			if (mp == NULL) {
3950455Smeem 				/*
3951455Smeem 				 * Caught a signal waiting for memory.
3952455Smeem 				 * Let send* return EINTR.
3953455Smeem 				 */
39548348SEric.Yu@Sun.COM 				return (EINTR);
3955455Smeem 			}
39560Sstevel@tonic-gate 		}
39570Sstevel@tonic-gate 		soappendmsg(mp, &tdr, sizeof (tdr));
39580Sstevel@tonic-gate 
39590Sstevel@tonic-gate 		if (fdlen != -1) {
39600Sstevel@tonic-gate 			ASSERT(fdbuf != NULL);
39610Sstevel@tonic-gate 			toh.level = SOL_SOCKET;
39620Sstevel@tonic-gate 			toh.name = SO_FILEP;
39630Sstevel@tonic-gate 			toh.len = fdbuf->fd_size +
39645240Snordmark 			    (t_uscalar_t)sizeof (struct T_opthdr);
39650Sstevel@tonic-gate 			toh.status = 0;
39660Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
39670Sstevel@tonic-gate 			soappendmsg(mp, fdbuf, fdbuf->fd_size);
39680Sstevel@tonic-gate 			ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
39690Sstevel@tonic-gate 		}
39700Sstevel@tonic-gate 		so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
39710Sstevel@tonic-gate 		/* At most 3 bytes left in the message */
39720Sstevel@tonic-gate 		ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
39730Sstevel@tonic-gate 		ASSERT(MBLKL(mp) <= (ssize_t)size);
39740Sstevel@tonic-gate 
39750Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
39760Sstevel@tonic-gate 
39770Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
39785240Snordmark 		    0, MSG_BAND, 0);
39790Sstevel@tonic-gate 		if (error) {
39800Sstevel@tonic-gate 			eprintsoline(so, error);
39810Sstevel@tonic-gate 			return (error);
39820Sstevel@tonic-gate 		}
39830Sstevel@tonic-gate 		control = NULL;
39840Sstevel@tonic-gate 		if (uiop->uio_resid > 0) {
39850Sstevel@tonic-gate 			/*
39860Sstevel@tonic-gate 			 * Recheck for fatal errors. Fail write even though
39870Sstevel@tonic-gate 			 * some data have been written. This is consistent
39880Sstevel@tonic-gate 			 * with strwrite semantics and BSD sockets semantics.
39890Sstevel@tonic-gate 			 */
39900Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
39910Sstevel@tonic-gate 				eprintsoline(so, error);
39920Sstevel@tonic-gate 				return (EPIPE);
39930Sstevel@tonic-gate 			}
39940Sstevel@tonic-gate 			if (so->so_error != 0) {
39950Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
39968348SEric.Yu@Sun.COM 				error = sogeterr(so, B_TRUE);
39970Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
39980Sstevel@tonic-gate 				if (error != 0) {
39990Sstevel@tonic-gate 					eprintsoline(so, error);
40000Sstevel@tonic-gate 					return (error);
40010Sstevel@tonic-gate 				}
40020Sstevel@tonic-gate 			}
40030Sstevel@tonic-gate 		}
40040Sstevel@tonic-gate 	} while (uiop->uio_resid > 0);
40050Sstevel@tonic-gate 	return (0);
40060Sstevel@tonic-gate }
40070Sstevel@tonic-gate 
40080Sstevel@tonic-gate /*
40090Sstevel@tonic-gate  * Sending data on a datagram socket.
40100Sstevel@tonic-gate  * Assumes caller has verified that SS_ISBOUND etc. are set.
40110Sstevel@tonic-gate  *
40120Sstevel@tonic-gate  * For AF_UNIX the destination address is translated to an internal
40130Sstevel@tonic-gate  * name and the source address is passed as an option.
40140Sstevel@tonic-gate  */
40150Sstevel@tonic-gate int
4016741Smasputra sosend_dgram(struct sonode *so, struct sockaddr	*name, socklen_t namelen,
4017741Smasputra     struct uio *uiop, int flags)
40180Sstevel@tonic-gate {
40190Sstevel@tonic-gate 	struct T_unitdata_req	tudr;
40200Sstevel@tonic-gate 	mblk_t			*mp;
40210Sstevel@tonic-gate 	int			error;
40220Sstevel@tonic-gate 	void			*addr;
40230Sstevel@tonic-gate 	socklen_t		addrlen;
40240Sstevel@tonic-gate 	void			*src;
40250Sstevel@tonic-gate 	socklen_t		srclen;
40260Sstevel@tonic-gate 	ssize_t			len;
40278348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
40280Sstevel@tonic-gate 
4029741Smasputra 	ASSERT(name != NULL && namelen != 0);
40300Sstevel@tonic-gate 
40310Sstevel@tonic-gate 	len = uiop->uio_resid;
40328348SEric.Yu@Sun.COM 	if (len > sti->sti_tidu_size) {
40330Sstevel@tonic-gate 		error = EMSGSIZE;
40340Sstevel@tonic-gate 		goto done;
40350Sstevel@tonic-gate 	}
40360Sstevel@tonic-gate 
4037741Smasputra 	/* Length and family checks */
40380Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
4039741Smasputra 	if (error != 0)
40400Sstevel@tonic-gate 		goto done;
4041741Smasputra 
40428348SEric.Yu@Sun.COM 	if (sti->sti_direct)
4043741Smasputra 		return (sodgram_direct(so, name, namelen, uiop, flags));
4044741Smasputra 
40450Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
40468348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate) {
40470Sstevel@tonic-gate 			/*
40480Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
40490Sstevel@tonic-gate 			 * pass any (transport internal) source address.
40500Sstevel@tonic-gate 			 */
40510Sstevel@tonic-gate 			addr = name;
40520Sstevel@tonic-gate 			addrlen = namelen;
40530Sstevel@tonic-gate 			src = NULL;
40540Sstevel@tonic-gate 			srclen = 0;
40550Sstevel@tonic-gate 		} else {
40560Sstevel@tonic-gate 			/*
40570Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
40580Sstevel@tonic-gate 			 * and translate the remote address.
40590Sstevel@tonic-gate 			 *
40608348SEric.Yu@Sun.COM 			 * Note that this code does not prevent sti_laddr_sa
40610Sstevel@tonic-gate 			 * from changing while it is being used. Thus
40620Sstevel@tonic-gate 			 * if an unbind+bind occurs concurrently with this
40630Sstevel@tonic-gate 			 * send the peer might see a partially new and a
40640Sstevel@tonic-gate 			 * partially old "from" address.
40650Sstevel@tonic-gate 			 */
40668348SEric.Yu@Sun.COM 			src = sti->sti_laddr_sa;
40678348SEric.Yu@Sun.COM 			srclen = (socklen_t)sti->sti_laddr_len;
40680Sstevel@tonic-gate 			dprintso(so, 1,
40695240Snordmark 			    ("sosend_dgram UNIX: srclen %d, src %p\n",
40705240Snordmark 			    srclen, src));
40710Sstevel@tonic-gate 			error = so_ux_addr_xlate(so, name, namelen,
40725240Snordmark 			    (flags & MSG_XPG4_2),
40735240Snordmark 			    &addr, &addrlen);
40740Sstevel@tonic-gate 			if (error) {
40750Sstevel@tonic-gate 				eprintsoline(so, error);
40760Sstevel@tonic-gate 				goto done;
40770Sstevel@tonic-gate 			}
40780Sstevel@tonic-gate 		}
40790Sstevel@tonic-gate 	} else {
40800Sstevel@tonic-gate 		addr = name;
40810Sstevel@tonic-gate 		addrlen = namelen;
40820Sstevel@tonic-gate 		src = NULL;
40830Sstevel@tonic-gate 		srclen = 0;
40840Sstevel@tonic-gate 	}
40850Sstevel@tonic-gate 	tudr.PRIM_type = T_UNITDATA_REQ;
40860Sstevel@tonic-gate 	tudr.DEST_length = addrlen;
40870Sstevel@tonic-gate 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
40880Sstevel@tonic-gate 	if (srclen == 0) {
40890Sstevel@tonic-gate 		tudr.OPT_length = 0;
40900Sstevel@tonic-gate 		tudr.OPT_offset = 0;
40910Sstevel@tonic-gate 
40920Sstevel@tonic-gate 		mp = soallocproto2(&tudr, sizeof (tudr),
40938778SErik.Nordmark@Sun.COM 		    addr, addrlen, 0, _ALLOC_INTR, CRED());
40940Sstevel@tonic-gate 		if (mp == NULL) {
40950Sstevel@tonic-gate 			/*
40960Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
40970Sstevel@tonic-gate 			 * Let send* return EINTR.
40980Sstevel@tonic-gate 			 */
40990Sstevel@tonic-gate 			error = EINTR;
41000Sstevel@tonic-gate 			goto done;
41010Sstevel@tonic-gate 		}
41020Sstevel@tonic-gate 	} else {
41030Sstevel@tonic-gate 		/*
41040Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
41050Sstevel@tonic-gate 		 * address option.
41060Sstevel@tonic-gate 		 */
41070Sstevel@tonic-gate 		struct T_opthdr toh;
41080Sstevel@tonic-gate 		ssize_t size;
41090Sstevel@tonic-gate 
41100Sstevel@tonic-gate 		tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
41115240Snordmark 		    _TPI_ALIGN_TOPT(srclen));
41120Sstevel@tonic-gate 		tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
41135240Snordmark 		    _TPI_ALIGN_TOPT(addrlen));
41140Sstevel@tonic-gate 
41150Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
41160Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
41170Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
41180Sstevel@tonic-gate 		toh.status = 0;
41190Sstevel@tonic-gate 
41200Sstevel@tonic-gate 		size = tudr.OPT_offset + tudr.OPT_length;
41210Sstevel@tonic-gate 		mp = soallocproto2(&tudr, sizeof (tudr),
41228778SErik.Nordmark@Sun.COM 		    addr, addrlen, size, _ALLOC_INTR, CRED());
41230Sstevel@tonic-gate 		if (mp == NULL) {
41240Sstevel@tonic-gate 			/*
41250Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
41260Sstevel@tonic-gate 			 * Let send* return EINTR.
41270Sstevel@tonic-gate 			 */
41280Sstevel@tonic-gate 			error = EINTR;
41290Sstevel@tonic-gate 			goto done;
41300Sstevel@tonic-gate 		}
41310Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
41320Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
41330Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
41340Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
41350Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
41360Sstevel@tonic-gate 	}
41370Sstevel@tonic-gate 
4138*11861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
41390Sstevel@tonic-gate 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
41400Sstevel@tonic-gate 
41410Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
41420Sstevel@tonic-gate done:
41430Sstevel@tonic-gate #ifdef SOCK_DEBUG
41440Sstevel@tonic-gate 	if (error) {
41450Sstevel@tonic-gate 		eprintsoline(so, error);
41460Sstevel@tonic-gate 	}
41470Sstevel@tonic-gate #endif /* SOCK_DEBUG */
41480Sstevel@tonic-gate 	return (error);
41490Sstevel@tonic-gate }
41500Sstevel@tonic-gate 
41510Sstevel@tonic-gate /*
41520Sstevel@tonic-gate  * Sending data on a connected stream socket.
41530Sstevel@tonic-gate  * Assumes caller has verified that SS_ISCONNECTED is set.
41540Sstevel@tonic-gate  */
41550Sstevel@tonic-gate int
41568348SEric.Yu@Sun.COM sosend_svc(struct sonode *so, struct uio *uiop, t_scalar_t prim, int more,
41578348SEric.Yu@Sun.COM     int sflag)
41580Sstevel@tonic-gate {
41590Sstevel@tonic-gate 	struct T_data_req	tdr;
41600Sstevel@tonic-gate 	mblk_t			*mp;
41610Sstevel@tonic-gate 	int			error;
41620Sstevel@tonic-gate 	ssize_t			iosize;
41638348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
41640Sstevel@tonic-gate 
41650Sstevel@tonic-gate 	dprintso(so, 1,
41665240Snordmark 	    ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
41677240Srh87107 	    (void *)so, uiop->uio_resid, prim, sflag));
41680Sstevel@tonic-gate 
41690Sstevel@tonic-gate 	/*
41700Sstevel@tonic-gate 	 * Has to be bound and connected. However, since no locks are
41710Sstevel@tonic-gate 	 * held the state could have changed after sotpi_sendmsg checked it
41720Sstevel@tonic-gate 	 * thus it is not possible to ASSERT on the state.
41730Sstevel@tonic-gate 	 */
41740Sstevel@tonic-gate 
41750Sstevel@tonic-gate 	do {
41760Sstevel@tonic-gate 		/*
41770Sstevel@tonic-gate 		 * Set the MORE flag if uio_resid does not fit in this
41780Sstevel@tonic-gate 		 * message or if the caller passed in "more".
41790Sstevel@tonic-gate 		 * Error for transports with zero tidu_size.
41800Sstevel@tonic-gate 		 */
41810Sstevel@tonic-gate 		tdr.PRIM_type = prim;
41828348SEric.Yu@Sun.COM 		iosize = sti->sti_tidu_size;
41830Sstevel@tonic-gate 		if (iosize <= 0)
41840Sstevel@tonic-gate 			return (EMSGSIZE);
41850Sstevel@tonic-gate 		if (uiop->uio_resid > iosize) {
41860Sstevel@tonic-gate 			tdr.MORE_flag = 1;
41870Sstevel@tonic-gate 		} else {
41880Sstevel@tonic-gate 			if (more)
41890Sstevel@tonic-gate 				tdr.MORE_flag = 1;
41900Sstevel@tonic-gate 			else
41910Sstevel@tonic-gate 				tdr.MORE_flag = 0;
41920Sstevel@tonic-gate 			iosize = uiop->uio_resid;
41930Sstevel@tonic-gate 		}
41940Sstevel@tonic-gate 		dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
41955240Snordmark 		    prim, tdr.MORE_flag, iosize));
41968778SErik.Nordmark@Sun.COM 		mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR, CRED());
41970Sstevel@tonic-gate 		if (mp == NULL) {
41980Sstevel@tonic-gate 			/*
41990Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
42000Sstevel@tonic-gate 			 * Let send* return EINTR.
42010Sstevel@tonic-gate 			 */
42028348SEric.Yu@Sun.COM 			return (EINTR);
42030Sstevel@tonic-gate 		}
42040Sstevel@tonic-gate 
42050Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
42065240Snordmark 		    0, sflag | MSG_BAND, 0);
42070Sstevel@tonic-gate 		if (error) {
42080Sstevel@tonic-gate 			eprintsoline(so, error);
42090Sstevel@tonic-gate 			return (error);
42100Sstevel@tonic-gate 		}
42110Sstevel@tonic-gate 		if (uiop->uio_resid > 0) {
42120Sstevel@tonic-gate 			/*
42130Sstevel@tonic-gate 			 * Recheck for fatal errors. Fail write even though
42140Sstevel@tonic-gate 			 * some data have been written. This is consistent
42150Sstevel@tonic-gate 			 * with strwrite semantics and BSD sockets semantics.
42160Sstevel@tonic-gate 			 */
42170Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
42180Sstevel@tonic-gate 				eprintsoline(so, error);
42190Sstevel@tonic-gate 				return (EPIPE);
42200Sstevel@tonic-gate 			}
42210Sstevel@tonic-gate 			if (so->so_error != 0) {
42220Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
42238348SEric.Yu@Sun.COM 				error = sogeterr(so, B_TRUE);
42240Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
42250Sstevel@tonic-gate 				if (error != 0) {
42260Sstevel@tonic-gate 					eprintsoline(so, error);
42270Sstevel@tonic-gate 					return (error);
42280Sstevel@tonic-gate 				}
42290Sstevel@tonic-gate 			}
42300Sstevel@tonic-gate 		}
42310Sstevel@tonic-gate 	} while (uiop->uio_resid > 0);
42320Sstevel@tonic-gate 	return (0);
42330Sstevel@tonic-gate }
42340Sstevel@tonic-gate 
42350Sstevel@tonic-gate /*
42360Sstevel@tonic-gate  * Check the state for errors and call the appropriate send function.
42370Sstevel@tonic-gate  *
42380Sstevel@tonic-gate  * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
42390Sstevel@tonic-gate  * this function issues a setsockopt to toggle SO_DONTROUTE before and
42400Sstevel@tonic-gate  * after sending the message.
42410Sstevel@tonic-gate  */
42420Sstevel@tonic-gate static int
42438348SEric.Yu@Sun.COM sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
42448348SEric.Yu@Sun.COM     struct cred *cr)
42450Sstevel@tonic-gate {
42460Sstevel@tonic-gate 	int		so_state;
42470Sstevel@tonic-gate 	int		so_mode;
42480Sstevel@tonic-gate 	int		error;
42490Sstevel@tonic-gate 	struct sockaddr *name;
42500Sstevel@tonic-gate 	t_uscalar_t	namelen;
42510Sstevel@tonic-gate 	int		dontroute;
42520Sstevel@tonic-gate 	int		flags;
42538348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
42540Sstevel@tonic-gate 
42550Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
42567240Srh87107 	    (void *)so, (void *)msg, msg->msg_flags,
42575240Snordmark 	    pr_state(so->so_state, so->so_mode), so->so_error));
42580Sstevel@tonic-gate 
42598348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
42608348SEric.Yu@Sun.COM 		/* The imaginary "sockmod" has been popped - act as a stream */
42618348SEric.Yu@Sun.COM 		so_update_attrs(so, SOMOD);
42628348SEric.Yu@Sun.COM 		return (strwrite(SOTOV(so), uiop, cr));
42638348SEric.Yu@Sun.COM 	}
42648348SEric.Yu@Sun.COM 
42650Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
42660Sstevel@tonic-gate 	so_state = so->so_state;
42670Sstevel@tonic-gate 
42680Sstevel@tonic-gate 	if (so_state & SS_CANTSENDMORE) {
42690Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
42700Sstevel@tonic-gate 		return (EPIPE);
42710Sstevel@tonic-gate 	}
42720Sstevel@tonic-gate 
42730Sstevel@tonic-gate 	if (so->so_error != 0) {
42748348SEric.Yu@Sun.COM 		error = sogeterr(so, B_TRUE);
42750Sstevel@tonic-gate 		if (error != 0) {
42760Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42770Sstevel@tonic-gate 			return (error);
42780Sstevel@tonic-gate 		}
42790Sstevel@tonic-gate 	}
42800Sstevel@tonic-gate 
42810Sstevel@tonic-gate 	name = (struct sockaddr *)msg->msg_name;
42820Sstevel@tonic-gate 	namelen = msg->msg_namelen;
42830Sstevel@tonic-gate 
42840Sstevel@tonic-gate 	so_mode = so->so_mode;
42850Sstevel@tonic-gate 
42860Sstevel@tonic-gate 	if (name == NULL) {
42870Sstevel@tonic-gate 		if (!(so_state & SS_ISCONNECTED)) {
42880Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42890Sstevel@tonic-gate 			if (so_mode & SM_CONNREQUIRED)
42900Sstevel@tonic-gate 				return (ENOTCONN);
42910Sstevel@tonic-gate 			else
42920Sstevel@tonic-gate 				return (EDESTADDRREQ);
42930Sstevel@tonic-gate 		}
42940Sstevel@tonic-gate 		if (so_mode & SM_CONNREQUIRED) {
42950Sstevel@tonic-gate 			name = NULL;
42960Sstevel@tonic-gate 			namelen = 0;
42970Sstevel@tonic-gate 		} else {
42980Sstevel@tonic-gate 			/*
42998348SEric.Yu@Sun.COM 			 * Note that this code does not prevent sti_faddr_sa
43000Sstevel@tonic-gate 			 * from changing while it is being used. Thus
43010Sstevel@tonic-gate 			 * if an "unconnect"+connect occurs concurrently with
43020Sstevel@tonic-gate 			 * this send the datagram might be delivered to a
43030Sstevel@tonic-gate 			 * garbaled address.
43040Sstevel@tonic-gate 			 */
43058348SEric.Yu@Sun.COM 			ASSERT(sti->sti_faddr_sa);
43068348SEric.Yu@Sun.COM 			name = sti->sti_faddr_sa;
43078348SEric.Yu@Sun.COM 			namelen = (t_uscalar_t)sti->sti_faddr_len;
43080Sstevel@tonic-gate 		}
43090Sstevel@tonic-gate 	} else {
43100Sstevel@tonic-gate 		if (!(so_state & SS_ISCONNECTED) &&
43110Sstevel@tonic-gate 		    (so_mode & SM_CONNREQUIRED)) {
43120Sstevel@tonic-gate 			/* Required but not connected */
43130Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
43140Sstevel@tonic-gate 			return (ENOTCONN);
43150Sstevel@tonic-gate 		}
43160Sstevel@tonic-gate 		/*
43170Sstevel@tonic-gate 		 * Ignore the address on connection-oriented sockets.
43180Sstevel@tonic-gate 		 * Just like BSD this code does not generate an error for
43190Sstevel@tonic-gate 		 * TCP (a CONNREQUIRED socket) when sending to an address
43200Sstevel@tonic-gate 		 * passed in with sendto/sendmsg. Instead the data is
43210Sstevel@tonic-gate 		 * delivered on the connection as if no address had been
43220Sstevel@tonic-gate 		 * supplied.
43230Sstevel@tonic-gate 		 */
43240Sstevel@tonic-gate 		if ((so_state & SS_ISCONNECTED) &&
43250Sstevel@tonic-gate 		    !(so_mode & SM_CONNREQUIRED)) {
43260Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
43270Sstevel@tonic-gate 			return (EISCONN);
43280Sstevel@tonic-gate 		}
43290Sstevel@tonic-gate 		if (!(so_state & SS_ISBOUND)) {
43300Sstevel@tonic-gate 			so_lock_single(so);	/* Set SOLOCKED */
43310Sstevel@tonic-gate 			error = sotpi_bind(so, NULL, 0,
43328348SEric.Yu@Sun.COM 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
43330Sstevel@tonic-gate 			so_unlock_single(so, SOLOCKED);
43340Sstevel@tonic-gate 			if (error) {
43350Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
43360Sstevel@tonic-gate 				eprintsoline(so, error);
43370Sstevel@tonic-gate 				return (error);
43380Sstevel@tonic-gate 			}
43390Sstevel@tonic-gate 		}
43400Sstevel@tonic-gate 		/*
43410Sstevel@tonic-gate 		 * Handle delayed datagram errors. These are only queued
43420Sstevel@tonic-gate 		 * when the application sets SO_DGRAM_ERRIND.
43430Sstevel@tonic-gate 		 * Return the error if we are sending to the address
43440Sstevel@tonic-gate 		 * that was returned in the last T_UDERROR_IND.
43450Sstevel@tonic-gate 		 * If sending to some other address discard the delayed
43460Sstevel@tonic-gate 		 * error indication.
43470Sstevel@tonic-gate 		 */
43488348SEric.Yu@Sun.COM 		if (sti->sti_delayed_error) {
43490Sstevel@tonic-gate 			struct T_uderror_ind	*tudi;
43500Sstevel@tonic-gate 			void			*addr;
43510Sstevel@tonic-gate 			t_uscalar_t		addrlen;
43520Sstevel@tonic-gate 			boolean_t		match = B_FALSE;
43530Sstevel@tonic-gate 
43548348SEric.Yu@Sun.COM 			ASSERT(sti->sti_eaddr_mp);
43558348SEric.Yu@Sun.COM 			error = sti->sti_delayed_error;
43568348SEric.Yu@Sun.COM 			sti->sti_delayed_error = 0;
43578348SEric.Yu@Sun.COM 			tudi =
43588348SEric.Yu@Sun.COM 			    (struct T_uderror_ind *)sti->sti_eaddr_mp->b_rptr;
43590Sstevel@tonic-gate 			addrlen = tudi->DEST_length;
43608348SEric.Yu@Sun.COM 			addr = sogetoff(sti->sti_eaddr_mp,
43618348SEric.Yu@Sun.COM 			    tudi->DEST_offset, addrlen, 1);
43620Sstevel@tonic-gate 			ASSERT(addr);	/* Checked by strsock_proto */
43630Sstevel@tonic-gate 			switch (so->so_family) {
43640Sstevel@tonic-gate 			case AF_INET: {
43650Sstevel@tonic-gate 				/* Compare just IP address and port */
43660Sstevel@tonic-gate 				sin_t *sin1 = (sin_t *)name;
43670Sstevel@tonic-gate 				sin_t *sin2 = (sin_t *)addr;
43680Sstevel@tonic-gate 
43690Sstevel@tonic-gate 				if (addrlen == sizeof (sin_t) &&
43700Sstevel@tonic-gate 				    namelen == addrlen &&
43710Sstevel@tonic-gate 				    sin1->sin_port == sin2->sin_port &&
43720Sstevel@tonic-gate 				    sin1->sin_addr.s_addr ==
43730Sstevel@tonic-gate 				    sin2->sin_addr.s_addr)
43740Sstevel@tonic-gate 					match = B_TRUE;
43750Sstevel@tonic-gate 				break;
43760Sstevel@tonic-gate 			}
43770Sstevel@tonic-gate 			case AF_INET6: {
43780Sstevel@tonic-gate 				/* Compare just IP address and port. Not flow */
43790Sstevel@tonic-gate 				sin6_t *sin1 = (sin6_t *)name;
43800Sstevel@tonic-gate 				sin6_t *sin2 = (sin6_t *)addr;
43810Sstevel@tonic-gate 
43820Sstevel@tonic-gate 				if (addrlen == sizeof (sin6_t) &&
43830Sstevel@tonic-gate 				    namelen == addrlen &&
43840Sstevel@tonic-gate 				    sin1->sin6_port == sin2->sin6_port &&
43850Sstevel@tonic-gate 				    IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
43865240Snordmark 				    &sin2->sin6_addr))
43870Sstevel@tonic-gate 					match = B_TRUE;
43880Sstevel@tonic-gate 				break;
43890Sstevel@tonic-gate 			}
43900Sstevel@tonic-gate 			case AF_UNIX:
43910Sstevel@tonic-gate 			default:
43920Sstevel@tonic-gate 				if (namelen == addrlen &&
43930Sstevel@tonic-gate 				    bcmp(name, addr, namelen) == 0)
43940Sstevel@tonic-gate 					match = B_TRUE;
43950Sstevel@tonic-gate 			}
43960Sstevel@tonic-gate 			if (match) {
43978348SEric.Yu@Sun.COM 				freemsg(sti->sti_eaddr_mp);
43988348SEric.Yu@Sun.COM 				sti->sti_eaddr_mp = NULL;
43990Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
44000Sstevel@tonic-gate #ifdef DEBUG
44010Sstevel@tonic-gate 				dprintso(so, 0,
44025240Snordmark 				    ("sockfs delayed error %d for %s\n",
44035240Snordmark 				    error,
44045240Snordmark 				    pr_addr(so->so_family, name, namelen)));
44050Sstevel@tonic-gate #endif /* DEBUG */
44060Sstevel@tonic-gate 				return (error);
44070Sstevel@tonic-gate 			}
44088348SEric.Yu@Sun.COM 			freemsg(sti->sti_eaddr_mp);
44098348SEric.Yu@Sun.COM 			sti->sti_eaddr_mp = NULL;
44100Sstevel@tonic-gate 		}
44110Sstevel@tonic-gate 	}
44120Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
44130Sstevel@tonic-gate 
44140Sstevel@tonic-gate 	flags = msg->msg_flags;
44150Sstevel@tonic-gate 	dontroute = 0;
44160Sstevel@tonic-gate 	if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
44170Sstevel@tonic-gate 		uint32_t	val;
44180Sstevel@tonic-gate 
44190Sstevel@tonic-gate 		val = 1;
44200Sstevel@tonic-gate 		error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
44218348SEric.Yu@Sun.COM 		    &val, (t_uscalar_t)sizeof (val), cr);
44220Sstevel@tonic-gate 		if (error)
44230Sstevel@tonic-gate 			return (error);
44240Sstevel@tonic-gate 		dontroute = 1;
44250Sstevel@tonic-gate 	}
44260Sstevel@tonic-gate 
44270Sstevel@tonic-gate 	if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
44280Sstevel@tonic-gate 		error = EOPNOTSUPP;
44290Sstevel@tonic-gate 		goto done;
44300Sstevel@tonic-gate 	}
44310Sstevel@tonic-gate 	if (msg->msg_controllen != 0) {
44320Sstevel@tonic-gate 		if (!(so_mode & SM_CONNREQUIRED)) {
44338348SEric.Yu@Sun.COM 			so_update_attrs(so, SOMOD);
44340Sstevel@tonic-gate 			error = sosend_dgramcmsg(so, name, namelen, uiop,
4435741Smasputra 			    msg->msg_control, msg->msg_controllen, flags);
44360Sstevel@tonic-gate 		} else {
44370Sstevel@tonic-gate 			if (flags & MSG_OOB) {
44380Sstevel@tonic-gate 				/* Can't generate T_EXDATA_REQ with options */
44390Sstevel@tonic-gate 				error = EOPNOTSUPP;
44400Sstevel@tonic-gate 				goto done;
44410Sstevel@tonic-gate 			}
44428348SEric.Yu@Sun.COM 			so_update_attrs(so, SOMOD);
44430Sstevel@tonic-gate 			error = sosend_svccmsg(so, uiop,
44445240Snordmark 			    !(flags & MSG_EOR),
44455240Snordmark 			    msg->msg_control, msg->msg_controllen,
44465240Snordmark 			    flags);
44470Sstevel@tonic-gate 		}
44480Sstevel@tonic-gate 		goto done;
44490Sstevel@tonic-gate 	}
44500Sstevel@tonic-gate 
44518348SEric.Yu@Sun.COM 	so_update_attrs(so, SOMOD);
44520Sstevel@tonic-gate 	if (!(so_mode & SM_CONNREQUIRED)) {
44530Sstevel@tonic-gate 		/*
44540Sstevel@tonic-gate 		 * If there is no SO_DONTROUTE to turn off return immediately
4455741Smasputra 		 * from send_dgram. This can allow tail-call optimizations.
44560Sstevel@tonic-gate 		 */
44570Sstevel@tonic-gate 		if (!dontroute) {
44580Sstevel@tonic-gate 			return (sosend_dgram(so, name, namelen, uiop, flags));
44590Sstevel@tonic-gate 		}
44600Sstevel@tonic-gate 		error = sosend_dgram(so, name, namelen, uiop, flags);
44610Sstevel@tonic-gate 	} else {
44620Sstevel@tonic-gate 		t_scalar_t prim;
44630Sstevel@tonic-gate 		int sflag;
44640Sstevel@tonic-gate 
44650Sstevel@tonic-gate 		/* Ignore msg_name in the connected state */
44660Sstevel@tonic-gate 		if (flags & MSG_OOB) {
44670Sstevel@tonic-gate 			prim = T_EXDATA_REQ;
44680Sstevel@tonic-gate 			/*
44690Sstevel@tonic-gate 			 * Send down T_EXDATA_REQ even if there is flow
44700Sstevel@tonic-gate 			 * control for data.
44710Sstevel@tonic-gate 			 */
44720Sstevel@tonic-gate 			sflag = MSG_IGNFLOW;
44730Sstevel@tonic-gate 		} else {
44740Sstevel@tonic-gate 			if (so_mode & SM_BYTESTREAM) {
44750Sstevel@tonic-gate 				/* Byte stream transport - use write */
44760Sstevel@tonic-gate 				dprintso(so, 1, ("sotpi_sendmsg: write\n"));
44778348SEric.Yu@Sun.COM 
44788348SEric.Yu@Sun.COM 				/* Send M_DATA messages */
44798348SEric.Yu@Sun.COM 				if ((sti->sti_nl7c_flags & NL7C_ENABLED) &&
44808348SEric.Yu@Sun.COM 				    (error = nl7c_data(so, uiop)) >= 0) {
44818348SEric.Yu@Sun.COM 					/* NL7C consumed the data */
44828348SEric.Yu@Sun.COM 					return (error);
44838348SEric.Yu@Sun.COM 				}
44840Sstevel@tonic-gate 				/*
4485741Smasputra 				 * If there is no SO_DONTROUTE to turn off,
44868348SEric.Yu@Sun.COM 				 * sti_direct is on, and there is no flow
4487741Smasputra 				 * control, we can take the fast path.
44880Sstevel@tonic-gate 				 */
44898348SEric.Yu@Sun.COM 				if (!dontroute && sti->sti_direct != 0 &&
4490741Smasputra 				    canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4491741Smasputra 					return (sostream_direct(so, uiop,
44928348SEric.Yu@Sun.COM 					    NULL, cr));
4493741Smasputra 				}
44948348SEric.Yu@Sun.COM 				error = strwrite(SOTOV(so), uiop, cr);
44950Sstevel@tonic-gate 				goto done;
44960Sstevel@tonic-gate 			}
44970Sstevel@tonic-gate 			prim = T_DATA_REQ;
44980Sstevel@tonic-gate 			sflag = 0;
44990Sstevel@tonic-gate 		}
45000Sstevel@tonic-gate 		/*
45010Sstevel@tonic-gate 		 * If there is no SO_DONTROUTE to turn off return immediately
45020Sstevel@tonic-gate 		 * from sosend_svc. This can allow tail-call optimizations.
45030Sstevel@tonic-gate 		 */
45040Sstevel@tonic-gate 		if (!dontroute)
45050Sstevel@tonic-gate 			return (sosend_svc(so, uiop, prim,
45065240Snordmark 			    !(flags & MSG_EOR), sflag));
45070Sstevel@tonic-gate 		error = sosend_svc(so, uiop, prim,
45085240Snordmark 		    !(flags & MSG_EOR), sflag);
45090Sstevel@tonic-gate 	}
45100Sstevel@tonic-gate 	ASSERT(dontroute);
45110Sstevel@tonic-gate done:
45120Sstevel@tonic-gate 	if (dontroute) {
45130Sstevel@tonic-gate 		uint32_t	val;
45140Sstevel@tonic-gate 
45150Sstevel@tonic-gate 		val = 0;
45160Sstevel@tonic-gate 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
45178348SEric.Yu@Sun.COM 		    &val, (t_uscalar_t)sizeof (val), cr);
45188348SEric.Yu@Sun.COM 	}
45198348SEric.Yu@Sun.COM 	return (error);
45208348SEric.Yu@Sun.COM }
45218348SEric.Yu@Sun.COM 
45228348SEric.Yu@Sun.COM /*
45238348SEric.Yu@Sun.COM  * kstrwritemp() has very similar semantics as that of strwrite().
45248348SEric.Yu@Sun.COM  * The main difference is it obtains mblks from the caller and also
45258348SEric.Yu@Sun.COM  * does not do any copy as done in strwrite() from user buffers to
45268348SEric.Yu@Sun.COM  * kernel buffers.
45278348SEric.Yu@Sun.COM  *
45288348SEric.Yu@Sun.COM  * Currently, this routine is used by sendfile to send data allocated
45298348SEric.Yu@Sun.COM  * within the kernel without any copying. This interface does not use the
45308348SEric.Yu@Sun.COM  * synchronous stream interface as synch. stream interface implies
45318348SEric.Yu@Sun.COM  * copying.
45328348SEric.Yu@Sun.COM  */
45338348SEric.Yu@Sun.COM int
45348348SEric.Yu@Sun.COM kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode)
45358348SEric.Yu@Sun.COM {
45368348SEric.Yu@Sun.COM 	struct stdata *stp;
45378348SEric.Yu@Sun.COM 	struct queue *wqp;
45388348SEric.Yu@Sun.COM 	mblk_t *newmp;
45398348SEric.Yu@Sun.COM 	char waitflag;
45408348SEric.Yu@Sun.COM 	int tempmode;
45418348SEric.Yu@Sun.COM 	int error = 0;
45428348SEric.Yu@Sun.COM 	int done = 0;
45438348SEric.Yu@Sun.COM 	struct sonode *so;
45448348SEric.Yu@Sun.COM 	boolean_t direct;
45458348SEric.Yu@Sun.COM 
45468348SEric.Yu@Sun.COM 	ASSERT(vp->v_stream);
45478348SEric.Yu@Sun.COM 	stp = vp->v_stream;
45488348SEric.Yu@Sun.COM 
45498348SEric.Yu@Sun.COM 	so = VTOSO(vp);
45508348SEric.Yu@Sun.COM 	direct = _SOTOTPI(so)->sti_direct;
45518348SEric.Yu@Sun.COM 
45528348SEric.Yu@Sun.COM 	/*
45538348SEric.Yu@Sun.COM 	 * This is the sockfs direct fast path. canputnext() need
45548348SEric.Yu@Sun.COM 	 * not be accurate so we don't grab the sd_lock here. If
45558348SEric.Yu@Sun.COM 	 * we get flow-controlled, we grab sd_lock just before the
45568348SEric.Yu@Sun.COM 	 * do..while loop below to emulate what strwrite() does.
45578348SEric.Yu@Sun.COM 	 */
45588348SEric.Yu@Sun.COM 	wqp = stp->sd_wrq;
45598348SEric.Yu@Sun.COM 	if (canputnext(wqp) && direct &&
45608348SEric.Yu@Sun.COM 	    !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
45618348SEric.Yu@Sun.COM 		return (sostream_direct(so, NULL, mp, CRED()));
45628348SEric.Yu@Sun.COM 	} else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
45638348SEric.Yu@Sun.COM 		/* Fast check of flags before acquiring the lock */
45648348SEric.Yu@Sun.COM 		mutex_enter(&stp->sd_lock);
45658348SEric.Yu@Sun.COM 		error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
45668348SEric.Yu@Sun.COM 		mutex_exit(&stp->sd_lock);
45678348SEric.Yu@Sun.COM 		if (error != 0) {
45688348SEric.Yu@Sun.COM 			if (!(stp->sd_flag & STPLEX) &&
45698348SEric.Yu@Sun.COM 			    (stp->sd_wput_opt & SW_SIGPIPE)) {
45708348SEric.Yu@Sun.COM 				error = EPIPE;
45718348SEric.Yu@Sun.COM 			}
45728348SEric.Yu@Sun.COM 			return (error);
45738348SEric.Yu@Sun.COM 		}
45748348SEric.Yu@Sun.COM 	}
45758348SEric.Yu@Sun.COM 
45768348SEric.Yu@Sun.COM 	waitflag = WRITEWAIT;
45778348SEric.Yu@Sun.COM 	if (stp->sd_flag & OLDNDELAY)
45788348SEric.Yu@Sun.COM 		tempmode = fmode & ~FNDELAY;
45798348SEric.Yu@Sun.COM 	else
45808348SEric.Yu@Sun.COM 		tempmode = fmode;
45818348SEric.Yu@Sun.COM 
45828348SEric.Yu@Sun.COM 	mutex_enter(&stp->sd_lock);
45838348SEric.Yu@Sun.COM 	do {
45848348SEric.Yu@Sun.COM 		if (canputnext(wqp)) {
45858348SEric.Yu@Sun.COM 			mutex_exit(&stp->sd_lock);
45868348SEric.Yu@Sun.COM 			if (stp->sd_wputdatafunc != NULL) {
45878348SEric.Yu@Sun.COM 				newmp = (stp->sd_wputdatafunc)(vp, mp, NULL,
45888348SEric.Yu@Sun.COM 				    NULL, NULL, NULL);
45898348SEric.Yu@Sun.COM 				if (newmp == NULL) {
45908348SEric.Yu@Sun.COM 					/* The caller will free mp */
45918348SEric.Yu@Sun.COM 					return (ECOMM);
45928348SEric.Yu@Sun.COM 				}
45938348SEric.Yu@Sun.COM 				mp = newmp;
45948348SEric.Yu@Sun.COM 			}
45958348SEric.Yu@Sun.COM 			putnext(wqp, mp);
45968348SEric.Yu@Sun.COM 			return (0);
45978348SEric.Yu@Sun.COM 		}
45988348SEric.Yu@Sun.COM 		error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1,
45998348SEric.Yu@Sun.COM 		    &done);
46008348SEric.Yu@Sun.COM 	} while (error == 0 && !done);
46018348SEric.Yu@Sun.COM 
46028348SEric.Yu@Sun.COM 	mutex_exit(&stp->sd_lock);
46038348SEric.Yu@Sun.COM 	/*
46048348SEric.Yu@Sun.COM 	 * EAGAIN tells the application to try again. ENOMEM
46058348SEric.Yu@Sun.COM 	 * is returned only if the memory allocation size
46068348SEric.Yu@Sun.COM 	 * exceeds the physical limits of the system. ENOMEM
46078348SEric.Yu@Sun.COM 	 * can't be true here.
46088348SEric.Yu@Sun.COM 	 */
46098348SEric.Yu@Sun.COM 	if (error == ENOMEM)
46108348SEric.Yu@Sun.COM 		error = EAGAIN;
46118348SEric.Yu@Sun.COM 	return (error);
46128348SEric.Yu@Sun.COM }
46138348SEric.Yu@Sun.COM 
46148348SEric.Yu@Sun.COM /* ARGSUSED */
46158348SEric.Yu@Sun.COM static int
46168348SEric.Yu@Sun.COM sotpi_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
46178348SEric.Yu@Sun.COM     struct cred *cr, mblk_t **mpp)
46188348SEric.Yu@Sun.COM {
46198348SEric.Yu@Sun.COM 	int error;
46208348SEric.Yu@Sun.COM 
46218348SEric.Yu@Sun.COM 	if (so->so_family != AF_INET && so->so_family != AF_INET6)
46228348SEric.Yu@Sun.COM 		return (EAFNOSUPPORT);
46238348SEric.Yu@Sun.COM 
46248348SEric.Yu@Sun.COM 	if (so->so_state & SS_CANTSENDMORE)
46258348SEric.Yu@Sun.COM 		return (EPIPE);
46268348SEric.Yu@Sun.COM 
46278348SEric.Yu@Sun.COM 	if (so->so_type != SOCK_STREAM)
46288348SEric.Yu@Sun.COM 		return (EOPNOTSUPP);
46298348SEric.Yu@Sun.COM 
46308348SEric.Yu@Sun.COM 	if ((so->so_state & SS_ISCONNECTED) == 0)
46318348SEric.Yu@Sun.COM 		return (ENOTCONN);
46328348SEric.Yu@Sun.COM 
46338348SEric.Yu@Sun.COM 	error = kstrwritemp(so->so_vnode, *mpp, fflag);
46348348SEric.Yu@Sun.COM 	if (error == 0)
46358348SEric.Yu@Sun.COM 		*mpp = NULL;
46360Sstevel@tonic-gate 	return (error);
46370Sstevel@tonic-gate }
46380Sstevel@tonic-gate 
46390Sstevel@tonic-gate /*
4640741Smasputra  * Sending data on a datagram socket.
4641741Smasputra  * Assumes caller has verified that SS_ISBOUND etc. are set.
4642741Smasputra  */
4643741Smasputra /* ARGSUSED */
4644741Smasputra static int
4645741Smasputra sodgram_direct(struct sonode *so, struct sockaddr *name,
4646741Smasputra     socklen_t namelen, struct uio *uiop, int flags)
4647741Smasputra {
4648741Smasputra 	struct T_unitdata_req	tudr;
46495240Snordmark 	mblk_t			*mp = NULL;
4650741Smasputra 	int			error = 0;
4651741Smasputra 	void			*addr;
4652741Smasputra 	socklen_t		addrlen;
4653741Smasputra 	ssize_t			len;
4654741Smasputra 	struct stdata		*stp = SOTOV(so)->v_stream;
4655741Smasputra 	int			so_state;
4656741Smasputra 	queue_t			*udp_wq;
46575240Snordmark 	boolean_t		connected;
46585240Snordmark 	mblk_t			*mpdata = NULL;
46598348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
4660*11861SMarek.Pospisil@Sun.COM 	uint32_t		auditing = AU_AUDITING();
4661741Smasputra 
4662741Smasputra 	ASSERT(name != NULL && namelen != 0);
4663741Smasputra 	ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4664741Smasputra 	ASSERT(!(so->so_mode & SM_EXDATA));
4665741Smasputra 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4666741Smasputra 	ASSERT(SOTOV(so)->v_type == VSOCK);
4667741Smasputra 
4668741Smasputra 	/* Caller checked for proper length */
4669741Smasputra 	len = uiop->uio_resid;
46708348SEric.Yu@Sun.COM 	ASSERT(len <= sti->sti_tidu_size);
4671741Smasputra 
4672741Smasputra 	/* Length and family checks have been done by caller */
4673741Smasputra 	ASSERT(name->sa_family == so->so_family);
4674741Smasputra 	ASSERT(so->so_family == AF_INET ||
4675741Smasputra 	    (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4676741Smasputra 	ASSERT(so->so_family == AF_INET6 ||
4677741Smasputra 	    (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4678741Smasputra 
4679741Smasputra 	addr = name;
4680741Smasputra 	addrlen = namelen;
4681741Smasputra 
4682741Smasputra 	if (stp->sd_sidp != NULL &&
4683741Smasputra 	    (error = straccess(stp, JCWRITE)) != 0)
4684741Smasputra 		goto done;
4685741Smasputra 
4686741Smasputra 	so_state = so->so_state;
4687741Smasputra 
46885240Snordmark 	connected = so_state & SS_ISCONNECTED;
46895240Snordmark 	if (!connected) {
46905240Snordmark 		tudr.PRIM_type = T_UNITDATA_REQ;
46915240Snordmark 		tudr.DEST_length = addrlen;
46925240Snordmark 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
46935240Snordmark 		tudr.OPT_length = 0;
46945240Snordmark 		tudr.OPT_offset = 0;
46955240Snordmark 
46965240Snordmark 		mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0,
46978778SErik.Nordmark@Sun.COM 		    _ALLOC_INTR, CRED());
46985240Snordmark 		if (mp == NULL) {
46995240Snordmark 			/*
47005240Snordmark 			 * Caught a signal waiting for memory.
47015240Snordmark 			 * Let send* return EINTR.
47025240Snordmark 			 */
47035240Snordmark 			error = EINTR;
47045240Snordmark 			goto done;
47055240Snordmark 		}
47065240Snordmark 	}
47075240Snordmark 
4708741Smasputra 	/*
4709741Smasputra 	 * For UDP we don't break up the copyin into smaller pieces
4710741Smasputra 	 * as in the TCP case.  That means if ENOMEM is returned by
4711741Smasputra 	 * mcopyinuio() then the uio vector has not been modified at
4712741Smasputra 	 * all and we fallback to either strwrite() or kstrputmsg()
4713741Smasputra 	 * below.  Note also that we never generate priority messages
4714741Smasputra 	 * from here.
4715741Smasputra 	 */
4716741Smasputra 	udp_wq = stp->sd_wrq->q_next;
4717741Smasputra 	if (canput(udp_wq) &&
47185240Snordmark 	    (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
47195240Snordmark 		ASSERT(DB_TYPE(mpdata) == M_DATA);
4720741Smasputra 		ASSERT(uiop->uio_resid == 0);
47215240Snordmark 		if (!connected)
47225240Snordmark 			linkb(mp, mpdata);
47235240Snordmark 		else
47245240Snordmark 			mp = mpdata;
4725*11861SMarek.Pospisil@Sun.COM 		if (auditing)
4726741Smasputra 			audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
47275240Snordmark 
47285240Snordmark 		udp_wput(udp_wq, mp);
4729741Smasputra 		return (0);
4730741Smasputra 	}
47315240Snordmark 
47325240Snordmark 	ASSERT(mpdata == NULL);
47335240Snordmark 	if (error != 0 && error != ENOMEM) {
47345240Snordmark 		freemsg(mp);
4735741Smasputra 		return (error);
47365240Snordmark 	}
4737741Smasputra 
4738741Smasputra 	/*
4739741Smasputra 	 * For connected, let strwrite() handle the blocking case.
4740741Smasputra 	 * Otherwise we fall thru and use kstrputmsg().
4741741Smasputra 	 */
47425240Snordmark 	if (connected)
4743741Smasputra 		return (strwrite(SOTOV(so), uiop, CRED()));
4744741Smasputra 
4745*11861SMarek.Pospisil@Sun.COM 	if (auditing)
4746741Smasputra 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4747741Smasputra 
4748741Smasputra 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4749741Smasputra done:
4750741Smasputra #ifdef SOCK_DEBUG
4751741Smasputra 	if (error != 0) {
4752741Smasputra 		eprintsoline(so, error);
4753741Smasputra 	}
4754741Smasputra #endif /* SOCK_DEBUG */
4755741Smasputra 	return (error);
4756741Smasputra }
4757741Smasputra 
4758741Smasputra int
4759741Smasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4760741Smasputra {
4761741Smasputra 	struct stdata *stp = SOTOV(so)->v_stream;
4762741Smasputra 	ssize_t iosize, rmax, maxblk;
4763741Smasputra 	queue_t *tcp_wq = stp->sd_wrq->q_next;
4764898Skais 	mblk_t *newmp;
4765741Smasputra 	int error = 0, wflag = 0;
4766741Smasputra 
4767741Smasputra 	ASSERT(so->so_mode & SM_BYTESTREAM);
4768741Smasputra 	ASSERT(SOTOV(so)->v_type == VSOCK);
4769741Smasputra 
4770741Smasputra 	if (stp->sd_sidp != NULL &&
4771741Smasputra 	    (error = straccess(stp, JCWRITE)) != 0)
4772741Smasputra 		return (error);
4773741Smasputra 
4774741Smasputra 	if (uiop == NULL) {
4775741Smasputra 		/*
4776741Smasputra 		 * kstrwritemp() should have checked sd_flag and
4777741Smasputra 		 * flow-control before coming here.  If we end up
4778741Smasputra 		 * here it means that we can simply pass down the
4779741Smasputra 		 * data to tcp.
4780741Smasputra 		 */
4781741Smasputra 		ASSERT(mp != NULL);
4782898Skais 		if (stp->sd_wputdatafunc != NULL) {
4783898Skais 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4784898Skais 			    NULL, NULL, NULL);
4785898Skais 			if (newmp == NULL) {
4786898Skais 				/* The caller will free mp */
4787898Skais 				return (ECOMM);
4788898Skais 			}
4789898Skais 			mp = newmp;
4790898Skais 		}
4791741Smasputra 		tcp_wput(tcp_wq, mp);
4792741Smasputra 		return (0);
4793741Smasputra 	}
4794741Smasputra 
4795741Smasputra 	/* Fallback to strwrite() to do proper error handling */
4796741Smasputra 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4797741Smasputra 		return (strwrite(SOTOV(so), uiop, cr));
4798741Smasputra 
4799741Smasputra 	rmax = stp->sd_qn_maxpsz;
4800741Smasputra 	ASSERT(rmax >= 0 || rmax == INFPSZ);
4801741Smasputra 	if (rmax == 0 || uiop->uio_resid <= 0)
4802741Smasputra 		return (0);
4803741Smasputra 
4804741Smasputra 	if (rmax == INFPSZ)
4805741Smasputra 		rmax = uiop->uio_resid;
4806741Smasputra 
4807741Smasputra 	maxblk = stp->sd_maxblk;
4808741Smasputra 
4809741Smasputra 	for (;;) {
4810741Smasputra 		iosize = MIN(uiop->uio_resid, rmax);
4811741Smasputra 
4812741Smasputra 		mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4813741Smasputra 		if (mp == NULL) {
4814741Smasputra 			/*
4815741Smasputra 			 * Fallback to strwrite() for ENOMEM; if this
4816741Smasputra 			 * is our first time in this routine and the uio
4817741Smasputra 			 * vector has not been modified, we will end up
4818741Smasputra 			 * calling strwrite() without any flag set.
4819741Smasputra 			 */
4820741Smasputra 			if (error == ENOMEM)
4821741Smasputra 				goto slow_send;
4822741Smasputra 			else
4823741Smasputra 				return (error);
4824741Smasputra 		}
4825741Smasputra 		ASSERT(uiop->uio_resid >= 0);
4826741Smasputra 		/*
4827741Smasputra 		 * If mp is non-NULL and ENOMEM is set, it means that
4828741Smasputra 		 * mcopyinuio() was able to break down some of the user
4829741Smasputra 		 * data into one or more mblks.  Send the partial data
4830741Smasputra 		 * to tcp and let the rest be handled in strwrite().
4831741Smasputra 		 */
4832741Smasputra 		ASSERT(error == 0 || error == ENOMEM);
4833898Skais 		if (stp->sd_wputdatafunc != NULL) {
4834898Skais 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4835898Skais 			    NULL, NULL, NULL);
4836898Skais 			if (newmp == NULL) {
4837898Skais 				/* The caller will free mp */
4838898Skais 				return (ECOMM);
4839898Skais 			}
4840898Skais 			mp = newmp;
4841898Skais 		}
4842741Smasputra 		tcp_wput(tcp_wq, mp);
4843741Smasputra 
4844741Smasputra 		wflag |= NOINTR;
4845741Smasputra 
4846741Smasputra 		if (uiop->uio_resid == 0) {	/* No more data; we're done */
4847741Smasputra 			ASSERT(error == 0);
4848741Smasputra 			break;
4849741Smasputra 		} else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4850741Smasputra 		    (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4851741Smasputra slow_send:
4852741Smasputra 			/*
4853741Smasputra 			 * We were able to send down partial data using
4854741Smasputra 			 * the direct call interface, but are now relying
4855741Smasputra 			 * on strwrite() to handle the non-fastpath cases.
4856741Smasputra 			 * If the socket is blocking we will sleep in
4857741Smasputra 			 * strwaitq() until write is permitted, otherwise,
4858741Smasputra 			 * we will need to return the amount of bytes
4859741Smasputra 			 * written so far back to the app.  This is the
4860741Smasputra 			 * reason why we pass NOINTR flag to strwrite()
4861741Smasputra 			 * for non-blocking socket, because we don't want
4862741Smasputra 			 * to return EAGAIN when portion of the user data
4863741Smasputra 			 * has actually been sent down.
4864741Smasputra 			 */
4865741Smasputra 			return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4866741Smasputra 		}
4867741Smasputra 	}
4868741Smasputra 	return (0);
4869741Smasputra }
4870741Smasputra 
4871741Smasputra /*
48728348SEric.Yu@Sun.COM  * Update sti_faddr by asking the transport (unless AF_UNIX).
48730Sstevel@tonic-gate  */
48748348SEric.Yu@Sun.COM /* ARGSUSED */
48750Sstevel@tonic-gate int
48768348SEric.Yu@Sun.COM sotpi_getpeername(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
48778348SEric.Yu@Sun.COM     boolean_t accept, struct cred *cr)
48780Sstevel@tonic-gate {
48790Sstevel@tonic-gate 	struct strbuf	strbuf;
48800Sstevel@tonic-gate 	int		error = 0, res;
48810Sstevel@tonic-gate 	void		*addr;
48820Sstevel@tonic-gate 	t_uscalar_t	addrlen;
48830Sstevel@tonic-gate 	k_sigset_t	smask;
48848348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
48850Sstevel@tonic-gate 
48860Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
48877240Srh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
48880Sstevel@tonic-gate 
48898348SEric.Yu@Sun.COM 	ASSERT(*namelen > 0);
48900Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
48910Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
48928348SEric.Yu@Sun.COM 
48938348SEric.Yu@Sun.COM 	if (accept) {
48948348SEric.Yu@Sun.COM 		bcopy(sti->sti_faddr_sa, name,
48958348SEric.Yu@Sun.COM 		    MIN(*namelen, sti->sti_faddr_len));
48968348SEric.Yu@Sun.COM 		*namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
48978348SEric.Yu@Sun.COM 		goto done;
48988348SEric.Yu@Sun.COM 	}
48998348SEric.Yu@Sun.COM 
49000Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
49010Sstevel@tonic-gate 		error = ENOTCONN;
49020Sstevel@tonic-gate 		goto done;
49030Sstevel@tonic-gate 	}
49040Sstevel@tonic-gate 	/* Added this check for X/Open */
49050Sstevel@tonic-gate 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
49060Sstevel@tonic-gate 		error = EINVAL;
49070Sstevel@tonic-gate 		if (xnet_check_print) {
49080Sstevel@tonic-gate 			printf("sockfs: X/Open getpeername check => EINVAL\n");
49090Sstevel@tonic-gate 		}
49100Sstevel@tonic-gate 		goto done;
49110Sstevel@tonic-gate 	}
49128348SEric.Yu@Sun.COM 
49138348SEric.Yu@Sun.COM 	if (sti->sti_faddr_valid) {
49148348SEric.Yu@Sun.COM 		bcopy(sti->sti_faddr_sa, name,
49158348SEric.Yu@Sun.COM 		    MIN(*namelen, sti->sti_faddr_len));
49168348SEric.Yu@Sun.COM 		*namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
49178348SEric.Yu@Sun.COM 		goto done;
49188348SEric.Yu@Sun.COM 	}
49198348SEric.Yu@Sun.COM 
49200Sstevel@tonic-gate #ifdef DEBUG
49210Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
49228348SEric.Yu@Sun.COM 	    pr_addr(so->so_family, sti->sti_faddr_sa,
49238348SEric.Yu@Sun.COM 	    (t_uscalar_t)sti->sti_faddr_len)));
49240Sstevel@tonic-gate #endif /* DEBUG */
49250Sstevel@tonic-gate 
49261548Srshoaib 	if (so->so_family == AF_UNIX) {
49270Sstevel@tonic-gate 		/* Transport has different name space - return local info */
49288348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate)
49298348SEric.Yu@Sun.COM 			*namelen = 0;
49300Sstevel@tonic-gate 		error = 0;
49310Sstevel@tonic-gate 		goto done;
49320Sstevel@tonic-gate 	}
49330Sstevel@tonic-gate 
49348348SEric.Yu@Sun.COM 	ASSERT(so->so_family != AF_UNIX && sti->sti_faddr_noxlate == 0);
49358348SEric.Yu@Sun.COM 
49368348SEric.Yu@Sun.COM 	ASSERT(sti->sti_faddr_sa);
49370Sstevel@tonic-gate 	/* Allocate local buffer to use with ioctl */
49388348SEric.Yu@Sun.COM 	addrlen = (t_uscalar_t)sti->sti_faddr_maxlen;
49390Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
49400Sstevel@tonic-gate 	addr = kmem_alloc(addrlen, KM_SLEEP);
49410Sstevel@tonic-gate 
49420Sstevel@tonic-gate 	/*
49430Sstevel@tonic-gate 	 * Issue TI_GETPEERNAME with signals masked.
49448348SEric.Yu@Sun.COM 	 * Put the result in sti_faddr_sa so that getpeername works after
49450Sstevel@tonic-gate 	 * a shutdown(output).
49460Sstevel@tonic-gate 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
49470Sstevel@tonic-gate 	 * back to the socket.
49480Sstevel@tonic-gate 	 */
49490Sstevel@tonic-gate 	strbuf.buf = addr;
49500Sstevel@tonic-gate 	strbuf.maxlen = addrlen;
49510Sstevel@tonic-gate 	strbuf.len = 0;
49520Sstevel@tonic-gate 
49530Sstevel@tonic-gate 	sigintr(&smask, 0);
49540Sstevel@tonic-gate 	res = 0;
49558348SEric.Yu@Sun.COM 	ASSERT(cr);
49560Sstevel@tonic-gate 	error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
49578348SEric.Yu@Sun.COM 	    0, K_TO_K, cr, &res);
49580Sstevel@tonic-gate 	sigunintr(&smask);
49590Sstevel@tonic-gate 
49600Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
49610Sstevel@tonic-gate 	/*
49620Sstevel@tonic-gate 	 * If there is an error record the error in so_error put don't fail
49630Sstevel@tonic-gate 	 * the getpeername. Instead fallback on the recorded
49648348SEric.Yu@Sun.COM 	 * sti->sti_faddr_sa.
49650Sstevel@tonic-gate 	 */
49660Sstevel@tonic-gate 	if (error) {
49670Sstevel@tonic-gate 		/*
49680Sstevel@tonic-gate 		 * Various stream head errors can be returned to the ioctl.
49690Sstevel@tonic-gate 		 * However, it is impossible to determine which ones of
49700Sstevel@tonic-gate 		 * these are really socket level errors that were incorrectly
49710Sstevel@tonic-gate 		 * consumed by the ioctl. Thus this code silently ignores the
49720Sstevel@tonic-gate 		 * error - to code explicitly does not reinstate the error
49730Sstevel@tonic-gate 		 * using soseterror().
49740Sstevel@tonic-gate 		 * Experiments have shows that at least this set of
49750Sstevel@tonic-gate 		 * errors are reported and should not be reinstated on the
49760Sstevel@tonic-gate 		 * socket:
49770Sstevel@tonic-gate 		 *	EINVAL	E.g. if an I_LINK was in effect when
49780Sstevel@tonic-gate 		 *		getpeername was called.
49790Sstevel@tonic-gate 		 *	EPIPE	The ioctl error semantics prefer the write
49800Sstevel@tonic-gate 		 *		side error over the read side error.
49810Sstevel@tonic-gate 		 *	ENOTCONN The transport just got disconnected but
49820Sstevel@tonic-gate 		 *		sockfs had not yet seen the T_DISCON_IND
49830Sstevel@tonic-gate 		 *		when issuing the ioctl.
49840Sstevel@tonic-gate 		 */
49850Sstevel@tonic-gate 		error = 0;
49860Sstevel@tonic-gate 	} else if (res == 0 && strbuf.len > 0 &&
49870Sstevel@tonic-gate 	    (so->so_state & SS_ISCONNECTED)) {
49888348SEric.Yu@Sun.COM 		ASSERT(strbuf.len <= (int)sti->sti_faddr_maxlen);
49898348SEric.Yu@Sun.COM 		sti->sti_faddr_len = (socklen_t)strbuf.len;
49908348SEric.Yu@Sun.COM 		bcopy(addr, sti->sti_faddr_sa, sti->sti_faddr_len);
49918348SEric.Yu@Sun.COM 		sti->sti_faddr_valid = 1;
49928348SEric.Yu@Sun.COM 
49938348SEric.Yu@Sun.COM 		bcopy(addr, name, MIN(*namelen, sti->sti_faddr_len));
49948348SEric.Yu@Sun.COM 		*namelen = sti->sti_faddr_len;
49950Sstevel@tonic-gate 	}
49960Sstevel@tonic-gate 	kmem_free(addr, addrlen);
49970Sstevel@tonic-gate #ifdef DEBUG
49980Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
49998348SEric.Yu@Sun.COM 	    pr_addr(so->so_family, sti->sti_faddr_sa,
50008348SEric.Yu@Sun.COM 	    (t_uscalar_t)sti->sti_faddr_len)));
50010Sstevel@tonic-gate #endif /* DEBUG */
50020Sstevel@tonic-gate done:
50030Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
50040Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
50050Sstevel@tonic-gate 	return (error);
50060Sstevel@tonic-gate }
50070Sstevel@tonic-gate 
50080Sstevel@tonic-gate /*
50098348SEric.Yu@Sun.COM  * Update sti_laddr by asking the transport (unless AF_UNIX).
50100Sstevel@tonic-gate  */
50110Sstevel@tonic-gate int
50128348SEric.Yu@Sun.COM sotpi_getsockname(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
50138348SEric.Yu@Sun.COM     struct cred *cr)
50140Sstevel@tonic-gate {
50150Sstevel@tonic-gate 	struct strbuf	strbuf;
50160Sstevel@tonic-gate 	int		error = 0, res;
50170Sstevel@tonic-gate 	void		*addr;
50180Sstevel@tonic-gate 	t_uscalar_t	addrlen;
50190Sstevel@tonic-gate 	k_sigset_t	smask;
50208348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
50210Sstevel@tonic-gate 
50220Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
50237240Srh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
50240Sstevel@tonic-gate 
50258348SEric.Yu@Sun.COM 	ASSERT(*namelen > 0);
50260Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
50270Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
50288348SEric.Yu@Sun.COM 
50290Sstevel@tonic-gate #ifdef DEBUG
50308348SEric.Yu@Sun.COM 
50310Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
50328348SEric.Yu@Sun.COM 	    pr_addr(so->so_family, sti->sti_laddr_sa,
50338348SEric.Yu@Sun.COM 	    (t_uscalar_t)sti->sti_laddr_len)));
50340Sstevel@tonic-gate #endif /* DEBUG */
50358348SEric.Yu@Sun.COM 	if (sti->sti_laddr_valid) {
50368348SEric.Yu@Sun.COM 		bcopy(sti->sti_laddr_sa, name,
50378348SEric.Yu@Sun.COM 		    MIN(*namelen, sti->sti_laddr_len));
50388348SEric.Yu@Sun.COM 		*namelen = sti->sti_laddr_len;
50398348SEric.Yu@Sun.COM 		goto done;
50408348SEric.Yu@Sun.COM 	}
50418348SEric.Yu@Sun.COM 
50420Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
50430Sstevel@tonic-gate 		/* Transport has different name space - return local info */
50440Sstevel@tonic-gate 		error = 0;
50458465SEric.Yu@Sun.COM 		*namelen = 0;
50460Sstevel@tonic-gate 		goto done;
50470Sstevel@tonic-gate 	}
50481156Sblu 	if (!(so->so_state & SS_ISBOUND)) {
50491156Sblu 		/* If not bound, then nothing to return. */
50501156Sblu 		error = 0;
50511156Sblu 		goto done;
50521156Sblu 	}
50538348SEric.Yu@Sun.COM 
50540Sstevel@tonic-gate 	/* Allocate local buffer to use with ioctl */
50558348SEric.Yu@Sun.COM 	addrlen = (t_uscalar_t)sti->sti_laddr_maxlen;
50560Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
50570Sstevel@tonic-gate 	addr = kmem_alloc(addrlen, KM_SLEEP);
50580Sstevel@tonic-gate 
50590Sstevel@tonic-gate 	/*
50600Sstevel@tonic-gate 	 * Issue TI_GETMYNAME with signals masked.
50618348SEric.Yu@Sun.COM 	 * Put the result in sti_laddr_sa so that getsockname works after
50620Sstevel@tonic-gate 	 * a shutdown(output).
50630Sstevel@tonic-gate 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
50640Sstevel@tonic-gate 	 * back to the socket.
50650Sstevel@tonic-gate 	 */
50660Sstevel@tonic-gate 	strbuf.buf = addr;
50670Sstevel@tonic-gate 	strbuf.maxlen = addrlen;
50680Sstevel@tonic-gate 	strbuf.len = 0;
50690Sstevel@tonic-gate 
50700Sstevel@tonic-gate 	sigintr(&smask, 0);
50710Sstevel@tonic-gate 	res = 0;
50728348SEric.Yu@Sun.COM 	ASSERT(cr);
50730Sstevel@tonic-gate 	error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
50748348SEric.Yu@Sun.COM 	    0, K_TO_K, cr, &res);
50750Sstevel@tonic-gate 	sigunintr(&smask);
50760Sstevel@tonic-gate 
50770Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
50780Sstevel@tonic-gate 	/*
50790Sstevel@tonic-gate 	 * If there is an error record the error in so_error put don't fail
50800Sstevel@tonic-gate 	 * the getsockname. Instead fallback on the recorded
50818348SEric.Yu@Sun.COM 	 * sti->sti_laddr_sa.
50820Sstevel@tonic-gate 	 */
50830Sstevel@tonic-gate 	if (error) {
50840Sstevel@tonic-gate 		/*
50850Sstevel@tonic-gate 		 * Various stream head errors can be returned to the ioctl.
50860Sstevel@tonic-gate 		 * However, it is impossible to determine which ones of
50870Sstevel@tonic-gate 		 * these are really socket level errors that were incorrectly
50880Sstevel@tonic-gate 		 * consumed by the ioctl. Thus this code silently ignores the
50890Sstevel@tonic-gate 		 * error - to code explicitly does not reinstate the error
50900Sstevel@tonic-gate 		 * using soseterror().
50910Sstevel@tonic-gate 		 * Experiments have shows that at least this set of
50920Sstevel@tonic-gate 		 * errors are reported and should not be reinstated on the
50930Sstevel@tonic-gate 		 * socket:
50940Sstevel@tonic-gate 		 *	EINVAL	E.g. if an I_LINK was in effect when
50950Sstevel@tonic-gate 		 *		getsockname was called.
50960Sstevel@tonic-gate 		 *	EPIPE	The ioctl error semantics prefer the write
50970Sstevel@tonic-gate 		 *		side error over the read side error.
50980Sstevel@tonic-gate 		 */
50990Sstevel@tonic-gate 		error = 0;
51000Sstevel@tonic-gate 	} else if (res == 0 && strbuf.len > 0 &&
51010Sstevel@tonic-gate 	    (so->so_state & SS_ISBOUND)) {
51028348SEric.Yu@Sun.COM 		ASSERT(strbuf.len <= (int)sti->sti_laddr_maxlen);
51038348SEric.Yu@Sun.COM 		sti->sti_laddr_len = (socklen_t)strbuf.len;
51048348SEric.Yu@Sun.COM 		bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
51058348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 1;
51068348SEric.Yu@Sun.COM 
51078348SEric.Yu@Sun.COM 		bcopy(addr, name, MIN(sti->sti_laddr_len, *namelen));
51088348SEric.Yu@Sun.COM 		*namelen = sti->sti_laddr_len;
51090Sstevel@tonic-gate 	}
51100Sstevel@tonic-gate 	kmem_free(addr, addrlen);
51110Sstevel@tonic-gate #ifdef DEBUG
51120Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
51138348SEric.Yu@Sun.COM 	    pr_addr(so->so_family, sti->sti_laddr_sa,
51148348SEric.Yu@Sun.COM 	    (t_uscalar_t)sti->sti_laddr_len)));
51150Sstevel@tonic-gate #endif /* DEBUG */
51160Sstevel@tonic-gate done:
51170Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
51180Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
51190Sstevel@tonic-gate 	return (error);
51200Sstevel@tonic-gate }
51210Sstevel@tonic-gate 
51220Sstevel@tonic-gate /*
51230Sstevel@tonic-gate  * Get socket options. For SOL_SOCKET options some options are handled
51240Sstevel@tonic-gate  * by the sockfs while others use the value recorded in the sonode as a
51250Sstevel@tonic-gate  * fallback should the T_SVR4_OPTMGMT_REQ fail.
51260Sstevel@tonic-gate  *
51270Sstevel@tonic-gate  * On the return most *optlenp bytes are copied to optval.
51280Sstevel@tonic-gate  */
51298348SEric.Yu@Sun.COM /* ARGSUSED */
51300Sstevel@tonic-gate int
51310Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name,
51328348SEric.Yu@Sun.COM 		void *optval, socklen_t *optlenp, int flags, struct cred *cr)
51330Sstevel@tonic-gate {
51340Sstevel@tonic-gate 	struct T_optmgmt_req	optmgmt_req;
51350Sstevel@tonic-gate 	struct T_optmgmt_ack	*optmgmt_ack;
51360Sstevel@tonic-gate 	struct opthdr		oh;
51370Sstevel@tonic-gate 	struct opthdr		*opt_res;
51380Sstevel@tonic-gate 	mblk_t			*mp = NULL;
51390Sstevel@tonic-gate 	int			error = 0;
51400Sstevel@tonic-gate 	void			*option = NULL;	/* Set if fallback value */
51410Sstevel@tonic-gate 	t_uscalar_t		maxlen = *optlenp;
51420Sstevel@tonic-gate 	t_uscalar_t		len;
51430Sstevel@tonic-gate 	uint32_t		value;
51448348SEric.Yu@Sun.COM 	struct timeval		tmo_val; /* used for SO_RCVTIMEO, SO_SNDTIMEO */
51458489Sshenjian 	struct timeval32	tmo_val32;
51468348SEric.Yu@Sun.COM 	struct so_snd_bufinfo	snd_bufinfo;	/* used for zero copy */
51470Sstevel@tonic-gate 
51480Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
51497240Srh87107 	    (void *)so, level, option_name, optval, (void *)optlenp,
51505240Snordmark 	    pr_state(so->so_state, so->so_mode)));
51510Sstevel@tonic-gate 
51520Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
51530Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
51540Sstevel@tonic-gate 
51550Sstevel@tonic-gate 	/*
51560Sstevel@tonic-gate 	 * Check for SOL_SOCKET options.
51570Sstevel@tonic-gate 	 * Certain SOL_SOCKET options are returned directly whereas
51580Sstevel@tonic-gate 	 * others only provide a default (fallback) value should
51590Sstevel@tonic-gate 	 * the T_SVR4_OPTMGMT_REQ fail.
51600Sstevel@tonic-gate 	 */
51610Sstevel@tonic-gate 	if (level == SOL_SOCKET) {
51620Sstevel@tonic-gate 		/* Check parameters */
51630Sstevel@tonic-gate 		switch (option_name) {
51640Sstevel@tonic-gate 		case SO_TYPE:
51650Sstevel@tonic-gate 		case SO_ERROR:
51660Sstevel@tonic-gate 		case SO_DEBUG:
51670Sstevel@tonic-gate 		case SO_ACCEPTCONN:
51680Sstevel@tonic-gate 		case SO_REUSEADDR:
51690Sstevel@tonic-gate 		case SO_KEEPALIVE:
51700Sstevel@tonic-gate 		case SO_DONTROUTE:
51710Sstevel@tonic-gate 		case SO_BROADCAST:
51720Sstevel@tonic-gate 		case SO_USELOOPBACK:
51730Sstevel@tonic-gate 		case SO_OOBINLINE:
51740Sstevel@tonic-gate 		case SO_SNDBUF:
51750Sstevel@tonic-gate 		case SO_RCVBUF:
51760Sstevel@tonic-gate #ifdef notyet
51770Sstevel@tonic-gate 		case SO_SNDLOWAT:
51780Sstevel@tonic-gate 		case SO_RCVLOWAT:
51790Sstevel@tonic-gate #endif /* notyet */
51803388Skcpoon 		case SO_DOMAIN:
51810Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
51820Sstevel@tonic-gate 			if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
51830Sstevel@tonic-gate 				error = EINVAL;
51840Sstevel@tonic-gate 				eprintsoline(so, error);
51850Sstevel@tonic-gate 				goto done2;
51860Sstevel@tonic-gate 			}
51870Sstevel@tonic-gate 			break;
51888348SEric.Yu@Sun.COM 		case SO_RCVTIMEO:
51898348SEric.Yu@Sun.COM 		case SO_SNDTIMEO:
51908575Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
51918575Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
51928489Sshenjian 				if (maxlen < sizeof (struct timeval)) {
51938489Sshenjian 					error = EINVAL;
51948489Sshenjian 					eprintsoline(so, error);
51958489Sshenjian 					goto done2;
51968489Sshenjian 				}
51978489Sshenjian 			} else {
51988489Sshenjian 				if (maxlen < sizeof (struct timeval32)) {
51998489Sshenjian 					error = EINVAL;
52008489Sshenjian 					eprintsoline(so, error);
52018489Sshenjian 					goto done2;
52028489Sshenjian 				}
52038489Sshenjian 
52048348SEric.Yu@Sun.COM 			}
52058348SEric.Yu@Sun.COM 			break;
52060Sstevel@tonic-gate 		case SO_LINGER:
52070Sstevel@tonic-gate 			if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
52080Sstevel@tonic-gate 				error = EINVAL;
52090Sstevel@tonic-gate 				eprintsoline(so, error);
52100Sstevel@tonic-gate 				goto done2;
52110Sstevel@tonic-gate 			}
52120Sstevel@tonic-gate 			break;
52138348SEric.Yu@Sun.COM 		case SO_SND_BUFINFO:
52148348SEric.Yu@Sun.COM 			if (maxlen < (t_uscalar_t)
52158348SEric.Yu@Sun.COM 			    sizeof (struct so_snd_bufinfo)) {
52168348SEric.Yu@Sun.COM 				error = EINVAL;
52178348SEric.Yu@Sun.COM 				eprintsoline(so, error);
52188348SEric.Yu@Sun.COM 				goto done2;
52198348SEric.Yu@Sun.COM 			}
52208348SEric.Yu@Sun.COM 			break;
52210Sstevel@tonic-gate 		}
52220Sstevel@tonic-gate 
52230Sstevel@tonic-gate 		len = (t_uscalar_t)sizeof (uint32_t);	/* Default */
52240Sstevel@tonic-gate 
52250Sstevel@tonic-gate 		switch (option_name) {
52260Sstevel@tonic-gate 		case SO_TYPE:
52270Sstevel@tonic-gate 			value = so->so_type;
52280Sstevel@tonic-gate 			option = &value;
52290Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52300Sstevel@tonic-gate 
52310Sstevel@tonic-gate 		case SO_ERROR:
52328348SEric.Yu@Sun.COM 			value = sogeterr(so, B_TRUE);
52330Sstevel@tonic-gate 			option = &value;
52340Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52350Sstevel@tonic-gate 
52360Sstevel@tonic-gate 		case SO_ACCEPTCONN:
52370Sstevel@tonic-gate 			if (so->so_state & SS_ACCEPTCONN)
52380Sstevel@tonic-gate 				value = SO_ACCEPTCONN;
52390Sstevel@tonic-gate 			else
52400Sstevel@tonic-gate 				value = 0;
52410Sstevel@tonic-gate #ifdef DEBUG
52420Sstevel@tonic-gate 			if (value) {
52430Sstevel@tonic-gate 				dprintso(so, 1,
52440Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x is set\n",
52450Sstevel@tonic-gate 				    option_name));
52460Sstevel@tonic-gate 			} else {
52470Sstevel@tonic-gate 				dprintso(so, 1,
52480Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x not set\n",
52490Sstevel@tonic-gate 				    option_name));
52500Sstevel@tonic-gate 			}
52510Sstevel@tonic-gate #endif /* DEBUG */
52520Sstevel@tonic-gate 			option = &value;
52530Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52540Sstevel@tonic-gate 
52550Sstevel@tonic-gate 		case SO_DEBUG:
52560Sstevel@tonic-gate 		case SO_REUSEADDR:
52570Sstevel@tonic-gate 		case SO_KEEPALIVE:
52580Sstevel@tonic-gate 		case SO_DONTROUTE:
52590Sstevel@tonic-gate 		case SO_BROADCAST:
52600Sstevel@tonic-gate 		case SO_USELOOPBACK:
52610Sstevel@tonic-gate 		case SO_OOBINLINE:
52620Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
52630Sstevel@tonic-gate 			value = (so->so_options & option_name);
52640Sstevel@tonic-gate #ifdef DEBUG
52650Sstevel@tonic-gate 			if (value) {
52660Sstevel@tonic-gate 				dprintso(so, 1,
52670Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x is set\n",
52680Sstevel@tonic-gate 				    option_name));
52690Sstevel@tonic-gate 			} else {
52700Sstevel@tonic-gate 				dprintso(so, 1,
52710Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x not set\n",
52720Sstevel@tonic-gate 				    option_name));
52730Sstevel@tonic-gate 			}
52740Sstevel@tonic-gate #endif /* DEBUG */
52750Sstevel@tonic-gate 			option = &value;
52760Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52770Sstevel@tonic-gate 
52780Sstevel@tonic-gate 		/*
52790Sstevel@tonic-gate 		 * The following options are only returned by sockfs when the
52800Sstevel@tonic-gate 		 * T_SVR4_OPTMGMT_REQ fails.
52810Sstevel@tonic-gate 		 */
52820Sstevel@tonic-gate 		case SO_LINGER:
52830Sstevel@tonic-gate 			option = &so->so_linger;
52840Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (struct linger);
52850Sstevel@tonic-gate 			break;
52860Sstevel@tonic-gate 		case SO_SNDBUF: {
52870Sstevel@tonic-gate 			ssize_t lvalue;
52880Sstevel@tonic-gate 
52890Sstevel@tonic-gate 			/*
52900Sstevel@tonic-gate 			 * If the option has not been set then get a default
52910Sstevel@tonic-gate 			 * value from the read queue. This value is
52920Sstevel@tonic-gate 			 * returned if the transport fails
52930Sstevel@tonic-gate 			 * the T_SVR4_OPTMGMT_REQ.
52940Sstevel@tonic-gate 			 */
52950Sstevel@tonic-gate 			lvalue = so->so_sndbuf;
52960Sstevel@tonic-gate 			if (lvalue == 0) {
52970Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
52980Sstevel@tonic-gate 				(void) strqget(strvp2wq(SOTOV(so))->q_next,
52995240Snordmark 				    QHIWAT, 0, &lvalue);
53000Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
53010Sstevel@tonic-gate 				dprintso(so, 1,
53020Sstevel@tonic-gate 				    ("got SO_SNDBUF %ld from q\n", lvalue));
53030Sstevel@tonic-gate 			}
53040Sstevel@tonic-gate 			value = (int)lvalue;
53050Sstevel@tonic-gate 			option = &value;
53060Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (so->so_sndbuf);
53070Sstevel@tonic-gate 			break;
53080Sstevel@tonic-gate 		}
53090Sstevel@tonic-gate 		case SO_RCVBUF: {
53100Sstevel@tonic-gate 			ssize_t lvalue;
53110Sstevel@tonic-gate 
53120Sstevel@tonic-gate 			/*
53130Sstevel@tonic-gate 			 * If the option has not been set then get a default
53140Sstevel@tonic-gate 			 * value from the read queue. This value is
53150Sstevel@tonic-gate 			 * returned if the transport fails
53160Sstevel@tonic-gate 			 * the T_SVR4_OPTMGMT_REQ.
53170Sstevel@tonic-gate 			 *
53180Sstevel@tonic-gate 			 * XXX If SO_RCVBUF has been set and this is an
53190Sstevel@tonic-gate 			 * XPG 4.2 application then do not ask the transport
53200Sstevel@tonic-gate 			 * since the transport might adjust the value and not
53210Sstevel@tonic-gate 			 * return exactly what was set by the application.
53220Sstevel@tonic-gate 			 * For non-XPG 4.2 application we return the value
53230Sstevel@tonic-gate 			 * that the transport is actually using.
53240Sstevel@tonic-gate 			 */
53250Sstevel@tonic-gate 			lvalue = so->so_rcvbuf;
53260Sstevel@tonic-gate 			if (lvalue == 0) {
53270Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
53280Sstevel@tonic-gate 				(void) strqget(RD(strvp2wq(SOTOV(so))),
53295240Snordmark 				    QHIWAT, 0, &lvalue);
53300Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
53310Sstevel@tonic-gate 				dprintso(so, 1,
53320Sstevel@tonic-gate 				    ("got SO_RCVBUF %ld from q\n", lvalue));
53330Sstevel@tonic-gate 			} else if (flags & _SOGETSOCKOPT_XPG4_2) {
53340Sstevel@tonic-gate 				value = (int)lvalue;
53350Sstevel@tonic-gate 				option = &value;
53360Sstevel@tonic-gate 				goto copyout;	/* skip asking transport */
53370Sstevel@tonic-gate 			}
53380Sstevel@tonic-gate 			value = (int)lvalue;
53390Sstevel@tonic-gate 			option = &value;
53400Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (so->so_rcvbuf);
53410Sstevel@tonic-gate 			break;
53420Sstevel@tonic-gate 		}
53433388Skcpoon 		case SO_DOMAIN:
53443388Skcpoon 			value = so->so_family;
53453388Skcpoon 			option = &value;
53463388Skcpoon 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
53473388Skcpoon 
53480Sstevel@tonic-gate #ifdef notyet
53490Sstevel@tonic-gate 		/*
53500Sstevel@tonic-gate 		 * We do not implement the semantics of these options
53510Sstevel@tonic-gate 		 * thus we shouldn't implement the options either.
53520Sstevel@tonic-gate 		 */
53530Sstevel@tonic-gate 		case SO_SNDLOWAT:
53540Sstevel@tonic-gate 			value = so->so_sndlowat;
53550Sstevel@tonic-gate 			option = &value;
53560Sstevel@tonic-gate 			break;
53570Sstevel@tonic-gate 		case SO_RCVLOWAT:
53580Sstevel@tonic-gate 			value = so->so_rcvlowat;
53590Sstevel@tonic-gate 			option = &value;
53600Sstevel@tonic-gate 			break;
53618348SEric.Yu@Sun.COM #endif /* notyet */
53620Sstevel@tonic-gate 		case SO_SNDTIMEO:
53638348SEric.Yu@Sun.COM 		case SO_RCVTIMEO: {
53648348SEric.Yu@Sun.COM 			clock_t val;
53658489Sshenjian 
53668348SEric.Yu@Sun.COM 			if (option_name == SO_RCVTIMEO)
53678348SEric.Yu@Sun.COM 				val = drv_hztousec(so->so_rcvtimeo);
53688348SEric.Yu@Sun.COM 			else
53698348SEric.Yu@Sun.COM 				val = drv_hztousec(so->so_sndtimeo);
53708348SEric.Yu@Sun.COM 			tmo_val.tv_sec = val / (1000 * 1000);
53718348SEric.Yu@Sun.COM 			tmo_val.tv_usec = val % (1000 * 1000);
53728575Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
53738575Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
53748489Sshenjian 				option = &tmo_val;
53758489Sshenjian 				len = sizeof (struct timeval);
53768489Sshenjian 			} else {
53778489Sshenjian 				TIMEVAL_TO_TIMEVAL32(&tmo_val32, &tmo_val);
53788489Sshenjian 				option = &tmo_val32;
53798489Sshenjian 				len = sizeof (struct timeval32);
53808489Sshenjian 			}
53810Sstevel@tonic-gate 			break;
53828348SEric.Yu@Sun.COM 		}
53838348SEric.Yu@Sun.COM 		case SO_SND_BUFINFO: {
53848348SEric.Yu@Sun.COM 			snd_bufinfo.sbi_wroff =
53858348SEric.Yu@Sun.COM 			    (so->so_proto_props).sopp_wroff;
53868348SEric.Yu@Sun.COM 			snd_bufinfo.sbi_maxblk =
53878348SEric.Yu@Sun.COM 			    (so->so_proto_props).sopp_maxblk;
53888348SEric.Yu@Sun.COM 			snd_bufinfo.sbi_maxpsz =
53898348SEric.Yu@Sun.COM 			    (so->so_proto_props).sopp_maxpsz;
53908348SEric.Yu@Sun.COM 			snd_bufinfo.sbi_tail =
53918348SEric.Yu@Sun.COM 			    (so->so_proto_props).sopp_tail;
53928348SEric.Yu@Sun.COM 			option = &snd_bufinfo;
53938348SEric.Yu@Sun.COM 			len = (t_uscalar_t)sizeof (struct so_snd_bufinfo);
53940Sstevel@tonic-gate 			break;
53958348SEric.Yu@Sun.COM 		}
53960Sstevel@tonic-gate 		}
53970Sstevel@tonic-gate 	}
53980Sstevel@tonic-gate 
53990Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
54000Sstevel@tonic-gate 
54010Sstevel@tonic-gate 	/* Send request */
54020Sstevel@tonic-gate 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
54030Sstevel@tonic-gate 	optmgmt_req.MGMT_flags = T_CHECK;
54040Sstevel@tonic-gate 	optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
54050Sstevel@tonic-gate 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
54060Sstevel@tonic-gate 
54070Sstevel@tonic-gate 	oh.level = level;
54080Sstevel@tonic-gate 	oh.name = option_name;
54090Sstevel@tonic-gate 	oh.len = maxlen;
54100Sstevel@tonic-gate 
54110Sstevel@tonic-gate 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
54128778SErik.Nordmark@Sun.COM 	    &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP, cr);
54130Sstevel@tonic-gate 	/* Let option management work in the presence of data flow control */
54140Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
54155240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
54160Sstevel@tonic-gate 	mp = NULL;
54170Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
54180Sstevel@tonic-gate 	if (error) {
54190Sstevel@tonic-gate 		eprintsoline(so, error);
54200Sstevel@tonic-gate 		goto done2;
54210Sstevel@tonic-gate 	}
54220Sstevel@tonic-gate 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
54230Sstevel@tonic-gate 	    (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
54240Sstevel@tonic-gate 	if (error) {
54250Sstevel@tonic-gate 		if (option != NULL) {
54260Sstevel@tonic-gate 			/* We have a fallback value */
54270Sstevel@tonic-gate 			error = 0;
54280Sstevel@tonic-gate 			goto copyout;
54290Sstevel@tonic-gate 		}
54300Sstevel@tonic-gate 		eprintsoline(so, error);
54310Sstevel@tonic-gate 		goto done2;
54320Sstevel@tonic-gate 	}
54330Sstevel@tonic-gate 	ASSERT(mp);
54340Sstevel@tonic-gate 	optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
54350Sstevel@tonic-gate 	opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
54365240Snordmark 	    optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
54370Sstevel@tonic-gate 	if (opt_res == NULL) {
54380Sstevel@tonic-gate 		if (option != NULL) {
54390Sstevel@tonic-gate 			/* We have a fallback value */
54400Sstevel@tonic-gate 			error = 0;
54410Sstevel@tonic-gate 			goto copyout;
54420Sstevel@tonic-gate 		}
54430Sstevel@tonic-gate 		error = EPROTO;
54440Sstevel@tonic-gate 		eprintsoline(so, error);
54450Sstevel@tonic-gate 		goto done;
54460Sstevel@tonic-gate 	}
54470Sstevel@tonic-gate 	option = &opt_res[1];
54480Sstevel@tonic-gate 
54490Sstevel@tonic-gate 	/* check to ensure that the option is within bounds */
54500Sstevel@tonic-gate 	if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
54515240Snordmark 	    (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
54520Sstevel@tonic-gate 		if (option != NULL) {
54530Sstevel@tonic-gate 			/* We have a fallback value */
54540Sstevel@tonic-gate 			error = 0;
54550Sstevel@tonic-gate 			goto copyout;
54560Sstevel@tonic-gate 		}
54570Sstevel@tonic-gate 		error = EPROTO;
54580Sstevel@tonic-gate 		eprintsoline(so, error);
54590Sstevel@tonic-gate 		goto done;
54600Sstevel@tonic-gate 	}
54610Sstevel@tonic-gate 
54620Sstevel@tonic-gate 	len = opt_res->len;
54630Sstevel@tonic-gate 
54640Sstevel@tonic-gate copyout: {
54650Sstevel@tonic-gate 		t_uscalar_t size = MIN(len, maxlen);
54660Sstevel@tonic-gate 		bcopy(option, optval, size);
54670Sstevel@tonic-gate 		bcopy(&size, optlenp, sizeof (size));
54680Sstevel@tonic-gate 	}
54690Sstevel@tonic-gate done:
54700Sstevel@tonic-gate 	freemsg(mp);
54710Sstevel@tonic-gate done2:
54720Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
54730Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
54748348SEric.Yu@Sun.COM 
54750Sstevel@tonic-gate 	return (error);
54760Sstevel@tonic-gate }
54770Sstevel@tonic-gate 
54780Sstevel@tonic-gate /*
54790Sstevel@tonic-gate  * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
54800Sstevel@tonic-gate  * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
54810Sstevel@tonic-gate  * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
54820Sstevel@tonic-gate  * setsockopt has to work even if the transport does not support the option.
54830Sstevel@tonic-gate  */
54848348SEric.Yu@Sun.COM /* ARGSUSED */
54850Sstevel@tonic-gate int
54860Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name,
54878348SEric.Yu@Sun.COM 	const void *optval, t_uscalar_t optlen, struct cred *cr)
54880Sstevel@tonic-gate {
54890Sstevel@tonic-gate 	struct T_optmgmt_req	optmgmt_req;
54900Sstevel@tonic-gate 	struct opthdr		oh;
54910Sstevel@tonic-gate 	mblk_t			*mp;
54920Sstevel@tonic-gate 	int			error = 0;
54930Sstevel@tonic-gate 	boolean_t		handled = B_FALSE;
54940Sstevel@tonic-gate 
54950Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
54967240Srh87107 	    (void *)so, level, option_name, optval, optlen,
54975240Snordmark 	    pr_state(so->so_state, so->so_mode)));
54980Sstevel@tonic-gate 
54990Sstevel@tonic-gate 	/* X/Open requires this check */
55000Sstevel@tonic-gate 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
55010Sstevel@tonic-gate 		if (xnet_check_print)
55020Sstevel@tonic-gate 			printf("sockfs: X/Open setsockopt check => EINVAL\n");
55030Sstevel@tonic-gate 		return (EINVAL);
55040Sstevel@tonic-gate 	}
55050Sstevel@tonic-gate 
55060Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
55070Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
55080Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
55090Sstevel@tonic-gate 
55100Sstevel@tonic-gate 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
55110Sstevel@tonic-gate 	optmgmt_req.MGMT_flags = T_NEGOTIATE;
55120Sstevel@tonic-gate 	optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
55130Sstevel@tonic-gate 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
55140Sstevel@tonic-gate 
55150Sstevel@tonic-gate 	oh.level = level;
55160Sstevel@tonic-gate 	oh.name = option_name;
55170Sstevel@tonic-gate 	oh.len = optlen;
55180Sstevel@tonic-gate 
55190Sstevel@tonic-gate 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
55208778SErik.Nordmark@Sun.COM 	    &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP, cr);
55210Sstevel@tonic-gate 	/* Let option management work in the presence of data flow control */
55220Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
55235240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
55240Sstevel@tonic-gate 	mp = NULL;
55250Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
55260Sstevel@tonic-gate 	if (error) {
55270Sstevel@tonic-gate 		eprintsoline(so, error);
55288348SEric.Yu@Sun.COM 		goto done2;
55290Sstevel@tonic-gate 	}
55300Sstevel@tonic-gate 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
55310Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
55320Sstevel@tonic-gate 	if (error) {
55330Sstevel@tonic-gate 		eprintsoline(so, error);
55340Sstevel@tonic-gate 		goto done;
55350Sstevel@tonic-gate 	}
55360Sstevel@tonic-gate 	ASSERT(mp);
55370Sstevel@tonic-gate 	/* No need to verify T_optmgmt_ack */
55380Sstevel@tonic-gate 	freemsg(mp);
55390Sstevel@tonic-gate done:
55400Sstevel@tonic-gate 	/*
55410Sstevel@tonic-gate 	 * Check for SOL_SOCKET options and record their values.
55420Sstevel@tonic-gate 	 * If we know about a SOL_SOCKET parameter and the transport
55430Sstevel@tonic-gate 	 * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
55440Sstevel@tonic-gate 	 * EPROTO) we let the setsockopt succeed.
55450Sstevel@tonic-gate 	 */
55460Sstevel@tonic-gate 	if (level == SOL_SOCKET) {
55470Sstevel@tonic-gate 		/* Check parameters */
55480Sstevel@tonic-gate 		switch (option_name) {
55490Sstevel@tonic-gate 		case SO_DEBUG:
55500Sstevel@tonic-gate 		case SO_REUSEADDR:
55510Sstevel@tonic-gate 		case SO_KEEPALIVE:
55520Sstevel@tonic-gate 		case SO_DONTROUTE:
55530Sstevel@tonic-gate 		case SO_BROADCAST:
55540Sstevel@tonic-gate 		case SO_USELOOPBACK:
55550Sstevel@tonic-gate 		case SO_OOBINLINE:
55560Sstevel@tonic-gate 		case SO_SNDBUF:
55570Sstevel@tonic-gate 		case SO_RCVBUF:
55580Sstevel@tonic-gate #ifdef notyet
55590Sstevel@tonic-gate 		case SO_SNDLOWAT:
55600Sstevel@tonic-gate 		case SO_RCVLOWAT:
55610Sstevel@tonic-gate #endif /* notyet */
55620Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
55630Sstevel@tonic-gate 			if (optlen != (t_uscalar_t)sizeof (int32_t)) {
55640Sstevel@tonic-gate 				error = EINVAL;
55650Sstevel@tonic-gate 				eprintsoline(so, error);
55660Sstevel@tonic-gate 				goto done2;
55670Sstevel@tonic-gate 			}
55680Sstevel@tonic-gate 			ASSERT(optval);
55690Sstevel@tonic-gate 			handled = B_TRUE;
55700Sstevel@tonic-gate 			break;
55718348SEric.Yu@Sun.COM 		case SO_SNDTIMEO:
55728348SEric.Yu@Sun.COM 		case SO_RCVTIMEO:
55738575Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
55748575Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
55758489Sshenjian 				if (optlen != sizeof (struct timeval)) {
55768489Sshenjian 					error = EINVAL;
55778489Sshenjian 					eprintsoline(so, error);
55788489Sshenjian 					goto done2;
55798489Sshenjian 				}
55808489Sshenjian 			} else {
55818489Sshenjian 				if (optlen != sizeof (struct timeval32)) {
55828489Sshenjian 					error = EINVAL;
55838489Sshenjian 					eprintsoline(so, error);
55848489Sshenjian 					goto done2;
55858489Sshenjian 				}
55868348SEric.Yu@Sun.COM 			}
55878348SEric.Yu@Sun.COM 			ASSERT(optval);
55888348SEric.Yu@Sun.COM 			handled = B_TRUE;
55898348SEric.Yu@Sun.COM 			break;
55900Sstevel@tonic-gate 		case SO_LINGER:
55910Sstevel@tonic-gate 			if (optlen != (t_uscalar_t)sizeof (struct linger)) {
55920Sstevel@tonic-gate 				error = EINVAL;
55930Sstevel@tonic-gate 				eprintsoline(so, error);
55940Sstevel@tonic-gate 				goto done2;
55950Sstevel@tonic-gate 			}
55960Sstevel@tonic-gate 			ASSERT(optval);
55970Sstevel@tonic-gate 			handled = B_TRUE;
55980Sstevel@tonic-gate 			break;
55990Sstevel@tonic-gate 		}
56000Sstevel@tonic-gate 
56010Sstevel@tonic-gate #define	intvalue	(*(int32_t *)optval)
56020Sstevel@tonic-gate 
56030Sstevel@tonic-gate 		switch (option_name) {
56040Sstevel@tonic-gate 		case SO_TYPE:
56050Sstevel@tonic-gate 		case SO_ERROR:
56060Sstevel@tonic-gate 		case SO_ACCEPTCONN:
56070Sstevel@tonic-gate 			/* Can't be set */
56080Sstevel@tonic-gate 			error = ENOPROTOOPT;
56090Sstevel@tonic-gate 			goto done2;
56100Sstevel@tonic-gate 		case SO_LINGER: {
56110Sstevel@tonic-gate 			struct linger *l = (struct linger *)optval;
56120Sstevel@tonic-gate 
56130Sstevel@tonic-gate 			so->so_linger.l_linger = l->l_linger;
56140Sstevel@tonic-gate 			if (l->l_onoff) {
56150Sstevel@tonic-gate 				so->so_linger.l_onoff = SO_LINGER;
56160Sstevel@tonic-gate 				so->so_options |= SO_LINGER;
56170Sstevel@tonic-gate 			} else {
56180Sstevel@tonic-gate 				so->so_linger.l_onoff = 0;
56190Sstevel@tonic-gate 				so->so_options &= ~SO_LINGER;
56200Sstevel@tonic-gate 			}
56210Sstevel@tonic-gate 			break;
56220Sstevel@tonic-gate 		}
56230Sstevel@tonic-gate 
56240Sstevel@tonic-gate 		case SO_DEBUG:
56250Sstevel@tonic-gate #ifdef SOCK_TEST
56260Sstevel@tonic-gate 			if (intvalue & 2)
56270Sstevel@tonic-gate 				sock_test_timelimit = 10 * hz;
56280Sstevel@tonic-gate 			else
56290Sstevel@tonic-gate 				sock_test_timelimit = 0;
56300Sstevel@tonic-gate 
56310Sstevel@tonic-gate 			if (intvalue & 4)
56320Sstevel@tonic-gate 				do_useracc = 0;
56330Sstevel@tonic-gate 			else
56340Sstevel@tonic-gate 				do_useracc = 1;
56350Sstevel@tonic-gate #endif /* SOCK_TEST */
56360Sstevel@tonic-gate 			/* FALLTHRU */
56370Sstevel@tonic-gate 		case SO_REUSEADDR:
56380Sstevel@tonic-gate 		case SO_KEEPALIVE:
56390Sstevel@tonic-gate 		case SO_DONTROUTE:
56400Sstevel@tonic-gate 		case SO_BROADCAST:
56410Sstevel@tonic-gate 		case SO_USELOOPBACK:
56420Sstevel@tonic-gate 		case SO_OOBINLINE:
56430Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
56440Sstevel@tonic-gate 			if (intvalue != 0) {
56450Sstevel@tonic-gate 				dprintso(so, 1,
56468348SEric.Yu@Sun.COM 				    ("socket_setsockopt: setting 0x%x\n",
56475240Snordmark 				    option_name));
56480Sstevel@tonic-gate 				so->so_options |= option_name;
56490Sstevel@tonic-gate 			} else {
56500Sstevel@tonic-gate 				dprintso(so, 1,
56518348SEric.Yu@Sun.COM 				    ("socket_setsockopt: clearing 0x%x\n",
56525240Snordmark 				    option_name));
56530Sstevel@tonic-gate 				so->so_options &= ~option_name;
56540Sstevel@tonic-gate 			}
56550Sstevel@tonic-gate 			break;
56560Sstevel@tonic-gate 		/*
56570Sstevel@tonic-gate 		 * The following options are only returned by us when the
56588348SEric.Yu@Sun.COM 		 * transport layer fails.
56590Sstevel@tonic-gate 		 * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
56600Sstevel@tonic-gate 		 * since the transport might adjust the value and not
56610Sstevel@tonic-gate 		 * return exactly what was set by the application.
56620Sstevel@tonic-gate 		 */
56630Sstevel@tonic-gate 		case SO_SNDBUF:
56640Sstevel@tonic-gate 			so->so_sndbuf = intvalue;
56650Sstevel@tonic-gate 			break;
56660Sstevel@tonic-gate 		case SO_RCVBUF:
56670Sstevel@tonic-gate 			so->so_rcvbuf = intvalue;
56680Sstevel@tonic-gate 			break;
56698348SEric.Yu@Sun.COM 		case SO_RCVPSH:
56708348SEric.Yu@Sun.COM 			so->so_rcv_timer_interval = intvalue;
56718348SEric.Yu@Sun.COM 			break;
56720Sstevel@tonic-gate #ifdef notyet
56730Sstevel@tonic-gate 		/*
56740Sstevel@tonic-gate 		 * We do not implement the semantics of these options
56750Sstevel@tonic-gate 		 * thus we shouldn't implement the options either.
56760Sstevel@tonic-gate 		 */
56770Sstevel@tonic-gate 		case SO_SNDLOWAT:
56780Sstevel@tonic-gate 			so->so_sndlowat = intvalue;
56790Sstevel@tonic-gate 			break;
56800Sstevel@tonic-gate 		case SO_RCVLOWAT:
56810Sstevel@tonic-gate 			so->so_rcvlowat = intvalue;
56820Sstevel@tonic-gate 			break;
56838348SEric.Yu@Sun.COM #endif /* notyet */
56840Sstevel@tonic-gate 		case SO_SNDTIMEO:
56858348SEric.Yu@Sun.COM 		case SO_RCVTIMEO: {
56868489Sshenjian 			struct timeval tl;
56878489Sshenjian 			clock_t val;
56888489Sshenjian 
56898575Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
56908575Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE)
56918489Sshenjian 				bcopy(&tl, (struct timeval *)optval,
56928489Sshenjian 				    sizeof (struct timeval));
56938489Sshenjian 			else
56948489Sshenjian 				TIMEVAL32_TO_TIMEVAL(&tl,
56958489Sshenjian 				    (struct timeval32 *)optval);
56968489Sshenjian 			val = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
56978348SEric.Yu@Sun.COM 			if (option_name == SO_RCVTIMEO)
56988348SEric.Yu@Sun.COM 				so->so_rcvtimeo = drv_usectohz(val);
56998348SEric.Yu@Sun.COM 			else
57008348SEric.Yu@Sun.COM 				so->so_sndtimeo = drv_usectohz(val);
57010Sstevel@tonic-gate 			break;
57028348SEric.Yu@Sun.COM 		}
57030Sstevel@tonic-gate 		}
57040Sstevel@tonic-gate #undef	intvalue
57050Sstevel@tonic-gate 
57060Sstevel@tonic-gate 		if (error) {
57070Sstevel@tonic-gate 			if ((error == ENOPROTOOPT || error == EPROTO ||
57080Sstevel@tonic-gate 			    error == EINVAL) && handled) {
57090Sstevel@tonic-gate 				dprintso(so, 1,
57100Sstevel@tonic-gate 				    ("setsockopt: ignoring error %d for 0x%x\n",
57110Sstevel@tonic-gate 				    error, option_name));
57120Sstevel@tonic-gate 				error = 0;
57130Sstevel@tonic-gate 			}
57140Sstevel@tonic-gate 		}
57150Sstevel@tonic-gate 	}
57160Sstevel@tonic-gate done2:
57170Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
57180Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
57190Sstevel@tonic-gate 	return (error);
57200Sstevel@tonic-gate }
57218348SEric.Yu@Sun.COM 
57228427SAnders.Persson@Sun.COM /*
57238427SAnders.Persson@Sun.COM  * sotpi_close() is called when the last open reference goes away.
57248427SAnders.Persson@Sun.COM  */
57258348SEric.Yu@Sun.COM /* ARGSUSED */
57268348SEric.Yu@Sun.COM int
57278348SEric.Yu@Sun.COM sotpi_close(struct sonode *so, int flag, struct cred *cr)
57288348SEric.Yu@Sun.COM {
57298348SEric.Yu@Sun.COM 	struct vnode *vp = SOTOV(so);
57308348SEric.Yu@Sun.COM 	dev_t dev;
57318348SEric.Yu@Sun.COM 	int error = 0;
57328348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
57338348SEric.Yu@Sun.COM 
57348348SEric.Yu@Sun.COM 	dprintso(so, 1, ("sotpi_close(%p, %x) %s\n",
57358348SEric.Yu@Sun.COM 	    (void *)vp, flag, pr_state(so->so_state, so->so_mode)));
57368348SEric.Yu@Sun.COM 
57378348SEric.Yu@Sun.COM 	dev = sti->sti_dev;
57388348SEric.Yu@Sun.COM 
57398348SEric.Yu@Sun.COM 	ASSERT(STREAMSTAB(getmajor(dev)));
57408348SEric.Yu@Sun.COM 
57418348SEric.Yu@Sun.COM 	mutex_enter(&so->so_lock);
57428348SEric.Yu@Sun.COM 	so_lock_single(so);	/* Set SOLOCKED */
57438348SEric.Yu@Sun.COM 
57448399SRao.Shoaib@Sun.COM 	ASSERT(so_verify_oobstate(so));
57458399SRao.Shoaib@Sun.COM 
57468348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_flags & NL7C_ENABLED) {
57478348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags = 0;
57488348SEric.Yu@Sun.COM 		nl7c_close(so);
57498348SEric.Yu@Sun.COM 	}
57508348SEric.Yu@Sun.COM 
57518348SEric.Yu@Sun.COM 	if (vp->v_stream != NULL) {
57528348SEric.Yu@Sun.COM 		vnode_t *ux_vp;
57538348SEric.Yu@Sun.COM 
57548348SEric.Yu@Sun.COM 		if (so->so_family == AF_UNIX) {
57558348SEric.Yu@Sun.COM 			/* Could avoid this when CANTSENDMORE for !dgram */
57568348SEric.Yu@Sun.COM 			so_unix_close(so);
57578348SEric.Yu@Sun.COM 		}
57588348SEric.Yu@Sun.COM 
57598348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
57608348SEric.Yu@Sun.COM 		/*
57618348SEric.Yu@Sun.COM 		 * Disassemble the linkage from the AF_UNIX underlying file
57628348SEric.Yu@Sun.COM 		 * system vnode to this socket (by atomically clearing
57638348SEric.Yu@Sun.COM 		 * v_stream in vn_rele_stream) before strclose clears sd_vnode
57648348SEric.Yu@Sun.COM 		 * and frees the stream head.
57658348SEric.Yu@Sun.COM 		 */
57668348SEric.Yu@Sun.COM 		if ((ux_vp = sti->sti_ux_bound_vp) != NULL) {
57678348SEric.Yu@Sun.COM 			ASSERT(ux_vp->v_stream);
57688348SEric.Yu@Sun.COM 			sti->sti_ux_bound_vp = NULL;
57698348SEric.Yu@Sun.COM 			vn_rele_stream(ux_vp);
57708348SEric.Yu@Sun.COM 		}
57718348SEric.Yu@Sun.COM 		if (so->so_family == AF_INET || so->so_family == AF_INET6) {
57728348SEric.Yu@Sun.COM 			strsetrwputdatahooks(SOTOV(so), NULL, NULL);
57738348SEric.Yu@Sun.COM 			if (sti->sti_kssl_ent != NULL) {
57748348SEric.Yu@Sun.COM 				kssl_release_ent(sti->sti_kssl_ent, so,
57758348SEric.Yu@Sun.COM 				    sti->sti_kssl_type);
57768348SEric.Yu@Sun.COM 				sti->sti_kssl_ent = NULL;
57778348SEric.Yu@Sun.COM 			}
57788348SEric.Yu@Sun.COM 			if (sti->sti_kssl_ctx != NULL) {
57798348SEric.Yu@Sun.COM 				kssl_release_ctx(sti->sti_kssl_ctx);
57808348SEric.Yu@Sun.COM 				sti->sti_kssl_ctx = NULL;
57818348SEric.Yu@Sun.COM 			}
57828348SEric.Yu@Sun.COM 			sti->sti_kssl_type = KSSL_NO_PROXY;
57838348SEric.Yu@Sun.COM 		}
57848348SEric.Yu@Sun.COM 		error = strclose(vp, flag, cr);
57858348SEric.Yu@Sun.COM 		vp->v_stream = NULL;
57868348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
57878348SEric.Yu@Sun.COM 	}
57888348SEric.Yu@Sun.COM 
57898348SEric.Yu@Sun.COM 	/*
57908348SEric.Yu@Sun.COM 	 * Flush the T_DISCON_IND on sti_discon_ind_mp.
57918348SEric.Yu@Sun.COM 	 */
57928348SEric.Yu@Sun.COM 	so_flush_discon_ind(so);
57938348SEric.Yu@Sun.COM 
57948348SEric.Yu@Sun.COM 	so_unlock_single(so, SOLOCKED);
57958348SEric.Yu@Sun.COM 	mutex_exit(&so->so_lock);
57968348SEric.Yu@Sun.COM 
57978348SEric.Yu@Sun.COM 	/*
57988348SEric.Yu@Sun.COM 	 * Needed for STREAMs.
57998348SEric.Yu@Sun.COM 	 * Decrement the device driver's reference count for streams
58008348SEric.Yu@Sun.COM 	 * opened via the clone dip. The driver was held in clone_open().
58018348SEric.Yu@Sun.COM 	 * The absence of clone_close() forces this asymmetry.
58028348SEric.Yu@Sun.COM 	 */
58038348SEric.Yu@Sun.COM 	if (so->so_flag & SOCLONE)
58048348SEric.Yu@Sun.COM 		ddi_rele_driver(getmajor(dev));
58058348SEric.Yu@Sun.COM 
58068348SEric.Yu@Sun.COM 	return (error);
58078348SEric.Yu@Sun.COM }
58088348SEric.Yu@Sun.COM 
58098348SEric.Yu@Sun.COM static int
58108348SEric.Yu@Sun.COM sotpi_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
58118348SEric.Yu@Sun.COM     struct cred *cr, int32_t *rvalp)
58128348SEric.Yu@Sun.COM {
58138348SEric.Yu@Sun.COM 	struct vnode *vp = SOTOV(so);
58148348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
58158348SEric.Yu@Sun.COM 	int error = 0;
58168348SEric.Yu@Sun.COM 
58178348SEric.Yu@Sun.COM 	dprintso(so, 0, ("sotpi_ioctl: cmd 0x%x, arg 0x%lx, state %s\n",
58188348SEric.Yu@Sun.COM 	    cmd, arg, pr_state(so->so_state, so->so_mode)));
58198348SEric.Yu@Sun.COM 
58208348SEric.Yu@Sun.COM 	switch (cmd) {
58218477SRao.Shoaib@Sun.COM 	case SIOCSQPTR:
58228477SRao.Shoaib@Sun.COM 		/*
58238477SRao.Shoaib@Sun.COM 		 * SIOCSQPTR is valid only when helper stream is created
58248477SRao.Shoaib@Sun.COM 		 * by the protocol.
58258477SRao.Shoaib@Sun.COM 		 */
58268348SEric.Yu@Sun.COM 	case _I_INSERT:
58278348SEric.Yu@Sun.COM 	case _I_REMOVE:
58288348SEric.Yu@Sun.COM 		/*
58298348SEric.Yu@Sun.COM 		 * Since there's no compelling reason to support these ioctls
58308348SEric.Yu@Sun.COM 		 * on sockets, and doing so would increase the complexity
58318348SEric.Yu@Sun.COM 		 * markedly, prevent it.
58328348SEric.Yu@Sun.COM 		 */
58338348SEric.Yu@Sun.COM 		return (EOPNOTSUPP);
58348348SEric.Yu@Sun.COM 
58358348SEric.Yu@Sun.COM 	case I_FIND:
58368348SEric.Yu@Sun.COM 	case I_LIST:
58378348SEric.Yu@Sun.COM 	case I_LOOK:
58388348SEric.Yu@Sun.COM 	case I_POP:
58398348SEric.Yu@Sun.COM 	case I_PUSH:
58408348SEric.Yu@Sun.COM 		/*
58418348SEric.Yu@Sun.COM 		 * To prevent races and inconsistencies between the actual
58428348SEric.Yu@Sun.COM 		 * state of the stream and the state according to the sonode,
58438348SEric.Yu@Sun.COM 		 * we serialize all operations which modify or operate on the
58448348SEric.Yu@Sun.COM 		 * list of modules on the socket's stream.
58458348SEric.Yu@Sun.COM 		 */
58468348SEric.Yu@Sun.COM 		mutex_enter(&sti->sti_plumb_lock);
58478348SEric.Yu@Sun.COM 		error = socktpi_plumbioctl(vp, cmd, arg, mode, cr, rvalp);
58488348SEric.Yu@Sun.COM 		mutex_exit(&sti->sti_plumb_lock);
58498348SEric.Yu@Sun.COM 		return (error);
58508348SEric.Yu@Sun.COM 
58518348SEric.Yu@Sun.COM 	default:
58528348SEric.Yu@Sun.COM 		if (so->so_version != SOV_STREAM)
58538348SEric.Yu@Sun.COM 			break;
58548348SEric.Yu@Sun.COM 
58558348SEric.Yu@Sun.COM 		/*
58568348SEric.Yu@Sun.COM 		 * The imaginary "sockmod" has been popped; act as a stream.
58578348SEric.Yu@Sun.COM 		 */
58588348SEric.Yu@Sun.COM 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
58598348SEric.Yu@Sun.COM 	}
58608348SEric.Yu@Sun.COM 
58618348SEric.Yu@Sun.COM 	ASSERT(so->so_version != SOV_STREAM);
58628348SEric.Yu@Sun.COM 
58638348SEric.Yu@Sun.COM 	/*
58648348SEric.Yu@Sun.COM 	 * Process socket-specific ioctls.
58658348SEric.Yu@Sun.COM 	 */
58668348SEric.Yu@Sun.COM 	switch (cmd) {
58678348SEric.Yu@Sun.COM 	case FIONBIO: {
58688348SEric.Yu@Sun.COM 		int32_t value;
58698348SEric.Yu@Sun.COM 
58708348SEric.Yu@Sun.COM 		if (so_copyin((void *)arg, &value, sizeof (int32_t),
58718348SEric.Yu@Sun.COM 		    (mode & (int)FKIOCTL)))
58728348SEric.Yu@Sun.COM 			return (EFAULT);
58738348SEric.Yu@Sun.COM 
58748348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
58758348SEric.Yu@Sun.COM 		if (value) {
58768348SEric.Yu@Sun.COM 			so->so_state |= SS_NDELAY;
58778348SEric.Yu@Sun.COM 		} else {
58788348SEric.Yu@Sun.COM 			so->so_state &= ~SS_NDELAY;
58798348SEric.Yu@Sun.COM 		}
58808348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
58818348SEric.Yu@Sun.COM 		return (0);
58828348SEric.Yu@Sun.COM 	}
58838348SEric.Yu@Sun.COM 
58848348SEric.Yu@Sun.COM 	case FIOASYNC: {
58858348SEric.Yu@Sun.COM 		int32_t value;
58868348SEric.Yu@Sun.COM 
58878348SEric.Yu@Sun.COM 		if (so_copyin((void *)arg, &value, sizeof (int32_t),
58888348SEric.Yu@Sun.COM 		    (mode & (int)FKIOCTL)))
58898348SEric.Yu@Sun.COM 			return (EFAULT);
58908348SEric.Yu@Sun.COM 
58918348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
58928348SEric.Yu@Sun.COM 		/*
58938348SEric.Yu@Sun.COM 		 * SS_ASYNC flag not already set correctly?
58948348SEric.Yu@Sun.COM 		 * (!value != !(so->so_state & SS_ASYNC))
58958348SEric.Yu@Sun.COM 		 * but some engineers find that too hard to read.
58968348SEric.Yu@Sun.COM 		 */
58978348SEric.Yu@Sun.COM 		if (value == 0 && (so->so_state & SS_ASYNC) != 0 ||
58988348SEric.Yu@Sun.COM 		    value != 0 && (so->so_state & SS_ASYNC) == 0)
58998348SEric.Yu@Sun.COM 			error = so_flip_async(so, vp, mode, cr);
59008348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
59018348SEric.Yu@Sun.COM 		return (error);
59028348SEric.Yu@Sun.COM 	}
59038348SEric.Yu@Sun.COM 
59048348SEric.Yu@Sun.COM 	case SIOCSPGRP:
59058348SEric.Yu@Sun.COM 	case FIOSETOWN: {
59068348SEric.Yu@Sun.COM 		pid_t pgrp;
59078348SEric.Yu@Sun.COM 
59088348SEric.Yu@Sun.COM 		if (so_copyin((void *)arg, &pgrp, sizeof (pid_t),
59098348SEric.Yu@Sun.COM 		    (mode & (int)FKIOCTL)))
59108348SEric.Yu@Sun.COM 			return (EFAULT);
59118348SEric.Yu@Sun.COM 
59128348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
59138348SEric.Yu@Sun.COM 		dprintso(so, 1, ("setown: new %d old %d\n", pgrp, so->so_pgrp));
59148348SEric.Yu@Sun.COM 		/* Any change? */
59158348SEric.Yu@Sun.COM 		if (pgrp != so->so_pgrp)
59168348SEric.Yu@Sun.COM 			error = so_set_siggrp(so, vp, pgrp, mode, cr);
59178348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
59188348SEric.Yu@Sun.COM 		return (error);
59198348SEric.Yu@Sun.COM 	}
59208348SEric.Yu@Sun.COM 	case SIOCGPGRP:
59218348SEric.Yu@Sun.COM 	case FIOGETOWN:
59228348SEric.Yu@Sun.COM 		if (so_copyout(&so->so_pgrp, (void *)arg,
59238348SEric.Yu@Sun.COM 		    sizeof (pid_t), (mode & (int)FKIOCTL)))
59248348SEric.Yu@Sun.COM 			return (EFAULT);
59258348SEric.Yu@Sun.COM 		return (0);
59268348SEric.Yu@Sun.COM 
59278348SEric.Yu@Sun.COM 	case SIOCATMARK: {
59288348SEric.Yu@Sun.COM 		int retval;
59298348SEric.Yu@Sun.COM 		uint_t so_state;
59308348SEric.Yu@Sun.COM 
59318348SEric.Yu@Sun.COM 		/*
59328348SEric.Yu@Sun.COM 		 * strwaitmark has a finite timeout after which it
59338348SEric.Yu@Sun.COM 		 * returns -1 if the mark state is undetermined.
59348348SEric.Yu@Sun.COM 		 * In order to avoid any race between the mark state
59358348SEric.Yu@Sun.COM 		 * in sockfs and the mark state in the stream head this
59368348SEric.Yu@Sun.COM 		 * routine loops until the mark state can be determined
59378348SEric.Yu@Sun.COM 		 * (or the urgent data indication has been removed by some
59388348SEric.Yu@Sun.COM 		 * other thread).
59398348SEric.Yu@Sun.COM 		 */
59408348SEric.Yu@Sun.COM 		do {
59418348SEric.Yu@Sun.COM 			mutex_enter(&so->so_lock);
59428348SEric.Yu@Sun.COM 			so_state = so->so_state;
59438348SEric.Yu@Sun.COM 			mutex_exit(&so->so_lock);
59448348SEric.Yu@Sun.COM 			if (so_state & SS_RCVATMARK) {
59458348SEric.Yu@Sun.COM 				retval = 1;
59468348SEric.Yu@Sun.COM 			} else if (!(so_state & SS_OOBPEND)) {
59478348SEric.Yu@Sun.COM 				/*
59488348SEric.Yu@Sun.COM 				 * No SIGURG has been generated -- there is no
59498348SEric.Yu@Sun.COM 				 * pending or present urgent data. Thus can't
59508348SEric.Yu@Sun.COM 				 * possibly be at the mark.
59518348SEric.Yu@Sun.COM 				 */
59528348SEric.Yu@Sun.COM 				retval = 0;
59538348SEric.Yu@Sun.COM 			} else {
59548348SEric.Yu@Sun.COM 				/*
59558348SEric.Yu@Sun.COM 				 * Have the stream head wait until there is
59568348SEric.Yu@Sun.COM 				 * either some messages on the read queue, or
59578348SEric.Yu@Sun.COM 				 * STRATMARK or STRNOTATMARK gets set. The
59588348SEric.Yu@Sun.COM 				 * STRNOTATMARK flag is used so that the
59598348SEric.Yu@Sun.COM 				 * transport can send up a MSGNOTMARKNEXT
59608348SEric.Yu@Sun.COM 				 * M_DATA to indicate that it is not
59618348SEric.Yu@Sun.COM 				 * at the mark and additional data is not about
59628348SEric.Yu@Sun.COM 				 * to be send upstream.
59638348SEric.Yu@Sun.COM 				 *
59648348SEric.Yu@Sun.COM 				 * If the mark state is undetermined this will
59658348SEric.Yu@Sun.COM 				 * return -1 and we will loop rechecking the
59668348SEric.Yu@Sun.COM 				 * socket state.
59678348SEric.Yu@Sun.COM 				 */
59688348SEric.Yu@Sun.COM 				retval = strwaitmark(vp);
59698348SEric.Yu@Sun.COM 			}
59708348SEric.Yu@Sun.COM 		} while (retval == -1);
59718348SEric.Yu@Sun.COM 
59728348SEric.Yu@Sun.COM 		if (so_copyout(&retval, (void *)arg, sizeof (int),
59738348SEric.Yu@Sun.COM 		    (mode & (int)FKIOCTL)))
59748348SEric.Yu@Sun.COM 			return (EFAULT);
59758348SEric.Yu@Sun.COM 		return (0);
59768348SEric.Yu@Sun.COM 	}
59778348SEric.Yu@Sun.COM 
59788348SEric.Yu@Sun.COM 	case I_FDINSERT:
59798348SEric.Yu@Sun.COM 	case I_SENDFD:
59808348SEric.Yu@Sun.COM 	case I_RECVFD:
59818348SEric.Yu@Sun.COM 	case I_ATMARK:
59828348SEric.Yu@Sun.COM 	case _SIOCSOCKFALLBACK:
59838348SEric.Yu@Sun.COM 		/*
59848348SEric.Yu@Sun.COM 		 * These ioctls do not apply to sockets. I_FDINSERT can be
59858348SEric.Yu@Sun.COM 		 * used to send M_PROTO messages without modifying the socket
59868348SEric.Yu@Sun.COM 		 * state. I_SENDFD/RECVFD should not be used for socket file
59878348SEric.Yu@Sun.COM 		 * descriptor passing since they assume a twisted stream.
59888348SEric.Yu@Sun.COM 		 * SIOCATMARK must be used instead of I_ATMARK.
59898348SEric.Yu@Sun.COM 		 *
59908348SEric.Yu@Sun.COM 		 * _SIOCSOCKFALLBACK from an application should never be
59918348SEric.Yu@Sun.COM 		 * processed.  It is only generated by socktpi_open() or
59928348SEric.Yu@Sun.COM 		 * in response to I_POP or I_PUSH.
59938348SEric.Yu@Sun.COM 		 */
59948348SEric.Yu@Sun.COM #ifdef DEBUG
59958348SEric.Yu@Sun.COM 		zcmn_err(getzoneid(), CE_WARN,
59968348SEric.Yu@Sun.COM 		    "Unsupported STREAMS ioctl 0x%x on socket. "
59978348SEric.Yu@Sun.COM 		    "Pid = %d\n", cmd, curproc->p_pid);
59988348SEric.Yu@Sun.COM #endif /* DEBUG */
59998348SEric.Yu@Sun.COM 		return (EOPNOTSUPP);
60008348SEric.Yu@Sun.COM 
60018348SEric.Yu@Sun.COM 	case _I_GETPEERCRED:
60028348SEric.Yu@Sun.COM 		if ((mode & FKIOCTL) == 0)
60038348SEric.Yu@Sun.COM 			return (EINVAL);
60048348SEric.Yu@Sun.COM 
60058348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
60068348SEric.Yu@Sun.COM 		if ((so->so_mode & SM_CONNREQUIRED) == 0) {
60078348SEric.Yu@Sun.COM 			error = ENOTSUP;
60088348SEric.Yu@Sun.COM 		} else if ((so->so_state & SS_ISCONNECTED) == 0) {
60098348SEric.Yu@Sun.COM 			error = ENOTCONN;
60108348SEric.Yu@Sun.COM 		} else if (so->so_peercred != NULL) {
60118348SEric.Yu@Sun.COM 			k_peercred_t *kp = (k_peercred_t *)arg;
60128348SEric.Yu@Sun.COM 			kp->pc_cr = so->so_peercred;
60138348SEric.Yu@Sun.COM 			kp->pc_cpid = so->so_cpid;
60148348SEric.Yu@Sun.COM 			crhold(so->so_peercred);
60158348SEric.Yu@Sun.COM 		} else {
60168348SEric.Yu@Sun.COM 			error = EINVAL;
60178348SEric.Yu@Sun.COM 		}
60188348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
60198348SEric.Yu@Sun.COM 		return (error);
60208348SEric.Yu@Sun.COM 
60218348SEric.Yu@Sun.COM 	default:
60228348SEric.Yu@Sun.COM 		/*
60238348SEric.Yu@Sun.COM 		 * Do the higher-order bits of the ioctl cmd indicate
60248348SEric.Yu@Sun.COM 		 * that it is an I_* streams ioctl?
60258348SEric.Yu@Sun.COM 		 */
60268348SEric.Yu@Sun.COM 		if ((cmd & 0xffffff00U) == STR &&
60278348SEric.Yu@Sun.COM 		    so->so_version == SOV_SOCKBSD) {
60288348SEric.Yu@Sun.COM #ifdef DEBUG
60298348SEric.Yu@Sun.COM 			zcmn_err(getzoneid(), CE_WARN,
60308348SEric.Yu@Sun.COM 			    "Unsupported STREAMS ioctl 0x%x on socket. "
60318348SEric.Yu@Sun.COM 			    "Pid = %d\n", cmd, 	curproc->p_pid);
60328348SEric.Yu@Sun.COM #endif /* DEBUG */
60338348SEric.Yu@Sun.COM 			return (EOPNOTSUPP);
60348348SEric.Yu@Sun.COM 		}
60358348SEric.Yu@Sun.COM 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
60368348SEric.Yu@Sun.COM 	}
60378348SEric.Yu@Sun.COM }
60388348SEric.Yu@Sun.COM 
60398348SEric.Yu@Sun.COM /*
60408348SEric.Yu@Sun.COM  * Handle plumbing-related ioctls.
60418348SEric.Yu@Sun.COM  */
60428348SEric.Yu@Sun.COM static int
60438348SEric.Yu@Sun.COM socktpi_plumbioctl(struct vnode *vp, int cmd, intptr_t arg, int mode,
60448348SEric.Yu@Sun.COM     struct cred *cr, int32_t *rvalp)
60458348SEric.Yu@Sun.COM {
60468348SEric.Yu@Sun.COM 	static const char sockmod_name[] = "sockmod";
60478348SEric.Yu@Sun.COM 	struct sonode	*so = VTOSO(vp);
60488348SEric.Yu@Sun.COM 	char		mname[FMNAMESZ + 1];
60498348SEric.Yu@Sun.COM 	int		error;
60508348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
60518348SEric.Yu@Sun.COM 
60528348SEric.Yu@Sun.COM 	ASSERT(MUTEX_HELD(&sti->sti_plumb_lock));
60538348SEric.Yu@Sun.COM 
60548348SEric.Yu@Sun.COM 	if (so->so_version == SOV_SOCKBSD)
60558348SEric.Yu@Sun.COM 		return (EOPNOTSUPP);
60568348SEric.Yu@Sun.COM 
60578348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
60588348SEric.Yu@Sun.COM 		/*
60598348SEric.Yu@Sun.COM 		 * The imaginary "sockmod" has been popped - act as a stream.
60608348SEric.Yu@Sun.COM 		 * If this is a push of sockmod then change back to a socket.
60618348SEric.Yu@Sun.COM 		 */
60628348SEric.Yu@Sun.COM 		if (cmd == I_PUSH) {
60638348SEric.Yu@Sun.COM 			error = ((mode & FKIOCTL) ? copystr : copyinstr)(
60648348SEric.Yu@Sun.COM 			    (void *)arg, mname, sizeof (mname), NULL);
60658348SEric.Yu@Sun.COM 
60668348SEric.Yu@Sun.COM 			if (error == 0 && strcmp(mname, sockmod_name) == 0) {
60678348SEric.Yu@Sun.COM 				dprintso(so, 0, ("socktpi_ioctl: going to "
60688348SEric.Yu@Sun.COM 				    "socket version\n"));
60698348SEric.Yu@Sun.COM 				so_stream2sock(so);
60708348SEric.Yu@Sun.COM 				return (0);
60718348SEric.Yu@Sun.COM 			}
60728348SEric.Yu@Sun.COM 		}
60738348SEric.Yu@Sun.COM 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
60748348SEric.Yu@Sun.COM 	}
60758348SEric.Yu@Sun.COM 
60768348SEric.Yu@Sun.COM 	switch (cmd) {
60778348SEric.Yu@Sun.COM 	case I_PUSH:
60788348SEric.Yu@Sun.COM 		if (sti->sti_direct) {
60798348SEric.Yu@Sun.COM 			mutex_enter(&so->so_lock);
60808348SEric.Yu@Sun.COM 			so_lock_single(so);
60818348SEric.Yu@Sun.COM 			mutex_exit(&so->so_lock);
60828348SEric.Yu@Sun.COM 
60838348SEric.Yu@Sun.COM 			error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
60848778SErik.Nordmark@Sun.COM 			    cr, rvalp);
60858348SEric.Yu@Sun.COM 
60868348SEric.Yu@Sun.COM 			mutex_enter(&so->so_lock);
60878348SEric.Yu@Sun.COM 			if (error == 0)
60888348SEric.Yu@Sun.COM 				sti->sti_direct = 0;
60898348SEric.Yu@Sun.COM 			so_unlock_single(so, SOLOCKED);
60908348SEric.Yu@Sun.COM 			mutex_exit(&so->so_lock);
60918348SEric.Yu@Sun.COM 
60928348SEric.Yu@Sun.COM 			if (error != 0)
60938348SEric.Yu@Sun.COM 				return (error);
60948348SEric.Yu@Sun.COM 		}
60958348SEric.Yu@Sun.COM 
60968348SEric.Yu@Sun.COM 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60978348SEric.Yu@Sun.COM 		if (error == 0)
60988348SEric.Yu@Sun.COM 			sti->sti_pushcnt++;
60998348SEric.Yu@Sun.COM 		return (error);
61008348SEric.Yu@Sun.COM 
61018348SEric.Yu@Sun.COM 	case I_POP:
61028348SEric.Yu@Sun.COM 		if (sti->sti_pushcnt == 0) {
61038348SEric.Yu@Sun.COM 			/* Emulate sockmod being popped */
61048348SEric.Yu@Sun.COM 			dprintso(so, 0,
61058348SEric.Yu@Sun.COM 			    ("socktpi_ioctl: going to STREAMS version\n"));
61068348SEric.Yu@Sun.COM 			return (so_sock2stream(so));
61078348SEric.Yu@Sun.COM 		}
61088348SEric.Yu@Sun.COM 
61098348SEric.Yu@Sun.COM 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
61108348SEric.Yu@Sun.COM 		if (error == 0)
61118348SEric.Yu@Sun.COM 			sti->sti_pushcnt--;
61128348SEric.Yu@Sun.COM 		return (error);
61138348SEric.Yu@Sun.COM 
61148348SEric.Yu@Sun.COM 	case I_LIST: {
61158348SEric.Yu@Sun.COM 		struct str_mlist *kmlistp, *umlistp;
61168348SEric.Yu@Sun.COM 		struct str_list	kstrlist;
61178348SEric.Yu@Sun.COM 		ssize_t		kstrlistsize;
61188348SEric.Yu@Sun.COM 		int		i, nmods;
61198348SEric.Yu@Sun.COM 
61208348SEric.Yu@Sun.COM 		STRUCT_DECL(str_list, ustrlist);
61218348SEric.Yu@Sun.COM 		STRUCT_INIT(ustrlist, mode);
61228348SEric.Yu@Sun.COM 
61238348SEric.Yu@Sun.COM 		if (arg == NULL) {
61248348SEric.Yu@Sun.COM 			error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
61258348SEric.Yu@Sun.COM 			if (error == 0)
61268348SEric.Yu@Sun.COM 				(*rvalp)++;	/* Add one for sockmod */
61278348SEric.Yu@Sun.COM 			return (error);
61288348SEric.Yu@Sun.COM 		}
61298348SEric.Yu@Sun.COM 
61308348SEric.Yu@Sun.COM 		error = so_copyin((void *)arg, STRUCT_BUF(ustrlist),
61318348SEric.Yu@Sun.COM 		    STRUCT_SIZE(ustrlist), mode & FKIOCTL);
61328348SEric.Yu@Sun.COM 		if (error != 0)
61338348SEric.Yu@Sun.COM 			return (error);
61348348SEric.Yu@Sun.COM 
61358348SEric.Yu@Sun.COM 		nmods = STRUCT_FGET(ustrlist, sl_nmods);
61368348SEric.Yu@Sun.COM 		if (nmods <= 0)
61378348SEric.Yu@Sun.COM 			return (EINVAL);
61388348SEric.Yu@Sun.COM 		/*
61398348SEric.Yu@Sun.COM 		 * Ceiling nmods at nstrpush to prevent someone from
61408348SEric.Yu@Sun.COM 		 * maliciously consuming lots of kernel memory.
61418348SEric.Yu@Sun.COM 		 */
61428348SEric.Yu@Sun.COM 		nmods = MIN(nmods, nstrpush);
61438348SEric.Yu@Sun.COM 
61448348SEric.Yu@Sun.COM 		kstrlistsize = (nmods + 1) * sizeof (struct str_mlist);
61458348SEric.Yu@Sun.COM 		kstrlist.sl_nmods = nmods;
61468348SEric.Yu@Sun.COM 		kstrlist.sl_modlist = kmem_zalloc(kstrlistsize, KM_SLEEP);
61478348SEric.Yu@Sun.COM 
61488348SEric.Yu@Sun.COM 		error = strioctl(vp, cmd, (intptr_t)&kstrlist, mode, K_TO_K,
61498348SEric.Yu@Sun.COM 		    cr, rvalp);
61508348SEric.Yu@Sun.COM 		if (error != 0)
61518348SEric.Yu@Sun.COM 			goto done;
61528348SEric.Yu@Sun.COM 
61538348SEric.Yu@Sun.COM 		/*
61548348SEric.Yu@Sun.COM 		 * Considering the module list as a 0-based array of sl_nmods
61558348SEric.Yu@Sun.COM 		 * modules, sockmod should conceptually exist at slot
61568348SEric.Yu@Sun.COM 		 * sti_pushcnt.  Insert sockmod at this location by sliding all
61578348SEric.Yu@Sun.COM 		 * of the module names after so_pushcnt over by one.  We know
61588348SEric.Yu@Sun.COM 		 * that there will be room to do this since we allocated
61598348SEric.Yu@Sun.COM 		 * sl_modlist with an additional slot.
61608348SEric.Yu@Sun.COM 		 */
61618348SEric.Yu@Sun.COM 		for (i = kstrlist.sl_nmods; i > sti->sti_pushcnt; i--)
61628348SEric.Yu@Sun.COM 			kstrlist.sl_modlist[i] = kstrlist.sl_modlist[i - 1];
61638348SEric.Yu@Sun.COM 
61648348SEric.Yu@Sun.COM 		(void) strcpy(kstrlist.sl_modlist[i].l_name, sockmod_name);
61658348SEric.Yu@Sun.COM 		kstrlist.sl_nmods++;
61668348SEric.Yu@Sun.COM 
61678348SEric.Yu@Sun.COM 		/*
61688348SEric.Yu@Sun.COM 		 * Copy all of the entries out to ustrlist.
61698348SEric.Yu@Sun.COM 		 */
61708348SEric.Yu@Sun.COM 		kmlistp = kstrlist.sl_modlist;
61718348SEric.Yu@Sun.COM 		umlistp = STRUCT_FGETP(ustrlist, sl_modlist);
61728348SEric.Yu@Sun.COM 		for (i = 0; i < nmods && i < kstrlist.sl_nmods; i++) {
61738348SEric.Yu@Sun.COM 			error = so_copyout(kmlistp++, umlistp++,
61748348SEric.Yu@Sun.COM 			    sizeof (struct str_mlist), mode & FKIOCTL);
61758348SEric.Yu@Sun.COM 			if (error != 0)
61768348SEric.Yu@Sun.COM 				goto done;
61778348SEric.Yu@Sun.COM 		}
61788348SEric.Yu@Sun.COM 
61798348SEric.Yu@Sun.COM 		error = so_copyout(&i, (void *)arg, sizeof (int32_t),
61808348SEric.Yu@Sun.COM 		    mode & FKIOCTL);
61818348SEric.Yu@Sun.COM 		if (error == 0)
61828348SEric.Yu@Sun.COM 			*rvalp = 0;
61838348SEric.Yu@Sun.COM 	done:
61848348SEric.Yu@Sun.COM 		kmem_free(kstrlist.sl_modlist, kstrlistsize);
61858348SEric.Yu@Sun.COM 		return (error);
61868348SEric.Yu@Sun.COM 	}
61878348SEric.Yu@Sun.COM 	case I_LOOK:
61888348SEric.Yu@Sun.COM 		if (sti->sti_pushcnt == 0) {
61898348SEric.Yu@Sun.COM 			return (so_copyout(sockmod_name, (void *)arg,
61908348SEric.Yu@Sun.COM 			    sizeof (sockmod_name), mode & FKIOCTL));
61918348SEric.Yu@Sun.COM 		}
61928348SEric.Yu@Sun.COM 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
61938348SEric.Yu@Sun.COM 
61948348SEric.Yu@Sun.COM 	case I_FIND:
61958348SEric.Yu@Sun.COM 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
61968348SEric.Yu@Sun.COM 		if (error && error != EINVAL)
61978348SEric.Yu@Sun.COM 			return (error);
61988348SEric.Yu@Sun.COM 
61998348SEric.Yu@Sun.COM 		/* if not found and string was sockmod return 1 */
62008348SEric.Yu@Sun.COM 		if (*rvalp == 0 || error == EINVAL) {
62018348SEric.Yu@Sun.COM 			error = ((mode & FKIOCTL) ? copystr : copyinstr)(
62028348SEric.Yu@Sun.COM 			    (void *)arg, mname, sizeof (mname), NULL);
62038348SEric.Yu@Sun.COM 			if (error == ENAMETOOLONG)
62048348SEric.Yu@Sun.COM 				error = EINVAL;
62058348SEric.Yu@Sun.COM 
62068348SEric.Yu@Sun.COM 			if (error == 0 && strcmp(mname, sockmod_name) == 0)
62078348SEric.Yu@Sun.COM 				*rvalp = 1;
62088348SEric.Yu@Sun.COM 		}
62098348SEric.Yu@Sun.COM 		return (error);
62108348SEric.Yu@Sun.COM 
62118348SEric.Yu@Sun.COM 	default:
62128348SEric.Yu@Sun.COM 		panic("socktpi_plumbioctl: unknown ioctl %d", cmd);
62138348SEric.Yu@Sun.COM 		break;
62148348SEric.Yu@Sun.COM 	}
62158348SEric.Yu@Sun.COM 
62168348SEric.Yu@Sun.COM 	return (0);
62178348SEric.Yu@Sun.COM }
62188348SEric.Yu@Sun.COM 
62198348SEric.Yu@Sun.COM /*
62208348SEric.Yu@Sun.COM  * Wrapper around the streams poll routine that implements socket poll
62218348SEric.Yu@Sun.COM  * semantics.
62228348SEric.Yu@Sun.COM  * The sockfs never calls pollwakeup itself - the stream head take care
62238348SEric.Yu@Sun.COM  * of all pollwakeups. Since sockfs never holds so_lock when calling the
62248348SEric.Yu@Sun.COM  * stream head there can never be a deadlock due to holding so_lock across
62258348SEric.Yu@Sun.COM  * pollwakeup and acquiring so_lock in this routine.
62268348SEric.Yu@Sun.COM  *
62278348SEric.Yu@Sun.COM  * However, since the performance of VOP_POLL is critical we avoid
62288348SEric.Yu@Sun.COM  * acquiring so_lock here. This is based on two assumptions:
62298348SEric.Yu@Sun.COM  *  - The poll implementation holds locks to serialize the VOP_POLL call
62308348SEric.Yu@Sun.COM  *    and a pollwakeup for the same pollhead. This ensures that should
62318348SEric.Yu@Sun.COM  *    e.g. so_state change during a socktpi_poll call the pollwakeup
62328348SEric.Yu@Sun.COM  *    (which strsock_* and strrput conspire to issue) is issued after
62338348SEric.Yu@Sun.COM  *    the state change. Thus the pollwakeup will block until VOP_POLL has
62348348SEric.Yu@Sun.COM  *    returned and then wake up poll and have it call VOP_POLL again.
62358348SEric.Yu@Sun.COM  *  - The reading of so_state without holding so_lock does not result in
62368348SEric.Yu@Sun.COM  *    stale data that is older than the latest state change that has dropped
62378348SEric.Yu@Sun.COM  *    so_lock. This is ensured by the mutex_exit issuing the appropriate
62388348SEric.Yu@Sun.COM  *    memory barrier to force the data into the coherency domain.
62398348SEric.Yu@Sun.COM  */
62408348SEric.Yu@Sun.COM static int
62418348SEric.Yu@Sun.COM sotpi_poll(
62428348SEric.Yu@Sun.COM 	struct sonode	*so,
62438348SEric.Yu@Sun.COM 	short		events,
62448348SEric.Yu@Sun.COM 	int		anyyet,
62458348SEric.Yu@Sun.COM 	short		*reventsp,
62468348SEric.Yu@Sun.COM 	struct pollhead **phpp)
62478348SEric.Yu@Sun.COM {
62488348SEric.Yu@Sun.COM 	short origevents = events;
62498348SEric.Yu@Sun.COM 	struct vnode *vp = SOTOV(so);
62508348SEric.Yu@Sun.COM 	int error;
62518348SEric.Yu@Sun.COM 	int so_state = so->so_state;	/* snapshot */
62528348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
62538348SEric.Yu@Sun.COM 
62548348SEric.Yu@Sun.COM 	dprintso(so, 0, ("socktpi_poll(%p): state %s err %d\n",
62558348SEric.Yu@Sun.COM 	    (void *)vp, pr_state(so_state, so->so_mode), so->so_error));
62568348SEric.Yu@Sun.COM 
62578348SEric.Yu@Sun.COM 	ASSERT(vp->v_type == VSOCK);
62588348SEric.Yu@Sun.COM 	ASSERT(vp->v_stream != NULL);
62598348SEric.Yu@Sun.COM 
62608348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
62618348SEric.Yu@Sun.COM 		/* The imaginary "sockmod" has been popped - act as a stream */
62628348SEric.Yu@Sun.COM 		return (strpoll(vp->v_stream, events, anyyet,
62638348SEric.Yu@Sun.COM 		    reventsp, phpp));
62648348SEric.Yu@Sun.COM 	}
62658348SEric.Yu@Sun.COM 
62668348SEric.Yu@Sun.COM 	if (!(so_state & SS_ISCONNECTED) &&
62678348SEric.Yu@Sun.COM 	    (so->so_mode & SM_CONNREQUIRED)) {
62688348SEric.Yu@Sun.COM 		/* Not connected yet - turn off write side events */
62698348SEric.Yu@Sun.COM 		events &= ~(POLLOUT|POLLWRBAND);
62708348SEric.Yu@Sun.COM 	}
62718348SEric.Yu@Sun.COM 	/*
62728348SEric.Yu@Sun.COM 	 * Check for errors without calling strpoll if the caller wants them.
62738348SEric.Yu@Sun.COM 	 * In sockets the errors are represented as input/output events
62748348SEric.Yu@Sun.COM 	 * and there is no need to ask the stream head for this information.
62758348SEric.Yu@Sun.COM 	 */
62768348SEric.Yu@Sun.COM 	if (so->so_error != 0 &&
62778348SEric.Yu@Sun.COM 	    ((POLLIN|POLLRDNORM|POLLOUT) & origevents)  != 0) {
62788348SEric.Yu@Sun.COM 		*reventsp = (POLLIN|POLLRDNORM|POLLOUT) & origevents;
62798348SEric.Yu@Sun.COM 		return (0);
62808348SEric.Yu@Sun.COM 	}
62818348SEric.Yu@Sun.COM 	/*
62828348SEric.Yu@Sun.COM 	 * Ignore M_PROTO only messages such as the T_EXDATA_IND messages.
62838348SEric.Yu@Sun.COM 	 * These message with only an M_PROTO/M_PCPROTO part and no M_DATA
62848348SEric.Yu@Sun.COM 	 * will not trigger a POLLIN event with POLLRDDATA set.
62858348SEric.Yu@Sun.COM 	 * The handling of urgent data (causing POLLRDBAND) is done by
62868348SEric.Yu@Sun.COM 	 * inspecting SS_OOBPEND below.
62878348SEric.Yu@Sun.COM 	 */
62888348SEric.Yu@Sun.COM 	events |= POLLRDDATA;
62898348SEric.Yu@Sun.COM 
62908348SEric.Yu@Sun.COM 	/*
62918348SEric.Yu@Sun.COM 	 * After shutdown(output) a stream head write error is set.
62928348SEric.Yu@Sun.COM 	 * However, we should not return output events.
62938348SEric.Yu@Sun.COM 	 */
62948348SEric.Yu@Sun.COM 	events |= POLLNOERR;
62958348SEric.Yu@Sun.COM 	error = strpoll(vp->v_stream, events, anyyet,
62968348SEric.Yu@Sun.COM 	    reventsp, phpp);
62978348SEric.Yu@Sun.COM 	if (error)
62988348SEric.Yu@Sun.COM 		return (error);
62998348SEric.Yu@Sun.COM 
63008348SEric.Yu@Sun.COM 	ASSERT(!(*reventsp & POLLERR));
63018348SEric.Yu@Sun.COM 
63028348SEric.Yu@Sun.COM 	/*
63038348SEric.Yu@Sun.COM 	 * Notes on T_CONN_IND handling for sockets.
63048348SEric.Yu@Sun.COM 	 *
63058348SEric.Yu@Sun.COM 	 * If strpoll() returned without events, SR_POLLIN is guaranteed
63068348SEric.Yu@Sun.COM 	 * to be set, ensuring any subsequent strrput() runs pollwakeup().
63078348SEric.Yu@Sun.COM 	 *
63088348SEric.Yu@Sun.COM 	 * Since the so_lock is not held, soqueueconnind() may have run
63098348SEric.Yu@Sun.COM 	 * and a T_CONN_IND may be waiting. We now check for any queued
63108348SEric.Yu@Sun.COM 	 * T_CONN_IND msgs on sti_conn_ind_head and set appropriate events
63118348SEric.Yu@Sun.COM 	 * to ensure poll returns.
63128348SEric.Yu@Sun.COM 	 *
63138348SEric.Yu@Sun.COM 	 * However:
63148348SEric.Yu@Sun.COM 	 * If the T_CONN_IND hasn't arrived by the time strpoll() returns,
63158348SEric.Yu@Sun.COM 	 * when strrput() does run for an arriving M_PROTO with T_CONN_IND
63168348SEric.Yu@Sun.COM 	 * the following actions will occur; taken together they ensure the
63178348SEric.Yu@Sun.COM 	 * syscall will return.
63188348SEric.Yu@Sun.COM 	 *
63198348SEric.Yu@Sun.COM 	 * 1. If a socket, soqueueconnind() will queue the T_CONN_IND but if
63208348SEric.Yu@Sun.COM 	 *    the accept() was run on a non-blocking socket sowaitconnind()
63218348SEric.Yu@Sun.COM 	 *    may have already returned EWOULDBLOCK, so not be waiting to
63228348SEric.Yu@Sun.COM 	 *    process the message. Additionally socktpi_poll() has probably
63238348SEric.Yu@Sun.COM 	 *    proceeded past the sti_conn_ind_head check below.
63248348SEric.Yu@Sun.COM 	 * 2. strrput() runs pollwakeup()->pollnotify()->cv_signal() to wake
63258348SEric.Yu@Sun.COM 	 *    this thread,  however that could occur before poll_common()
63268348SEric.Yu@Sun.COM 	 *    has entered cv_wait.
63278348SEric.Yu@Sun.COM 	 * 3. pollnotify() sets T_POLLWAKE, while holding the pc_lock.
63288348SEric.Yu@Sun.COM 	 *
63298348SEric.Yu@Sun.COM 	 * Before proceeding to cv_wait() in poll_common() for an event,
63308348SEric.Yu@Sun.COM 	 * poll_common() atomically checks for T_POLLWAKE under the pc_lock,
63318348SEric.Yu@Sun.COM 	 * and if set, re-calls strpoll() to ensure the late arriving
63328348SEric.Yu@Sun.COM 	 * T_CONN_IND is recognized, and pollsys() returns.
63338348SEric.Yu@Sun.COM 	 */
63348348SEric.Yu@Sun.COM 
63358348SEric.Yu@Sun.COM 	if (sti->sti_conn_ind_head != NULL)
63368348SEric.Yu@Sun.COM 		*reventsp |= (POLLIN|POLLRDNORM) & events;
63378348SEric.Yu@Sun.COM 
63388348SEric.Yu@Sun.COM 	if (so->so_state & SS_OOBPEND)
63398348SEric.Yu@Sun.COM 		*reventsp |= POLLRDBAND & events;
63408348SEric.Yu@Sun.COM 
63418348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_rcv_mp != NULL) {
63428348SEric.Yu@Sun.COM 		*reventsp |= (POLLIN|POLLRDNORM) & events;
63438348SEric.Yu@Sun.COM 	}
63448348SEric.Yu@Sun.COM 	if ((sti->sti_nl7c_flags & NL7C_ENABLED) &&
63458348SEric.Yu@Sun.COM 	    ((POLLIN|POLLRDNORM) & *reventsp)) {
63468348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags |= NL7C_POLLIN;
63478348SEric.Yu@Sun.COM 	}
63488348SEric.Yu@Sun.COM 
63498348SEric.Yu@Sun.COM 	return (0);
63508348SEric.Yu@Sun.COM }
63518348SEric.Yu@Sun.COM 
63528348SEric.Yu@Sun.COM /*ARGSUSED*/
63538348SEric.Yu@Sun.COM static int
63548348SEric.Yu@Sun.COM socktpi_constructor(void *buf, void *cdrarg, int kmflags)
63558348SEric.Yu@Sun.COM {
63568348SEric.Yu@Sun.COM 	sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
63578348SEric.Yu@Sun.COM 	int error = 0;
63588348SEric.Yu@Sun.COM 
63598348SEric.Yu@Sun.COM 	error = sonode_constructor(buf, cdrarg, kmflags);
63608348SEric.Yu@Sun.COM 	if (error != 0)
63618348SEric.Yu@Sun.COM 		return (error);
63628348SEric.Yu@Sun.COM 
63638348SEric.Yu@Sun.COM 	error = i_sotpi_info_constructor(&st->st_info);
63648348SEric.Yu@Sun.COM 	if (error != 0)
63658348SEric.Yu@Sun.COM 		sonode_destructor(buf, cdrarg);
63668348SEric.Yu@Sun.COM 
63678348SEric.Yu@Sun.COM 	st->st_sonode.so_priv = &st->st_info;
63688348SEric.Yu@Sun.COM 
63698348SEric.Yu@Sun.COM 	return (error);
63708348SEric.Yu@Sun.COM }
63718348SEric.Yu@Sun.COM 
63728348SEric.Yu@Sun.COM /*ARGSUSED1*/
63738348SEric.Yu@Sun.COM static void
63748348SEric.Yu@Sun.COM socktpi_destructor(void *buf, void *cdrarg)
63758348SEric.Yu@Sun.COM {
63768348SEric.Yu@Sun.COM 	sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
63778348SEric.Yu@Sun.COM 
63788348SEric.Yu@Sun.COM 	ASSERT(st->st_sonode.so_priv == &st->st_info);
63798348SEric.Yu@Sun.COM 	st->st_sonode.so_priv = NULL;
63808348SEric.Yu@Sun.COM 
63818348SEric.Yu@Sun.COM 	i_sotpi_info_destructor(&st->st_info);
63828348SEric.Yu@Sun.COM 	sonode_destructor(buf, cdrarg);
63838348SEric.Yu@Sun.COM }
63848348SEric.Yu@Sun.COM 
63858348SEric.Yu@Sun.COM static int
63868348SEric.Yu@Sun.COM socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags)
63878348SEric.Yu@Sun.COM {
63888348SEric.Yu@Sun.COM 	int retval;
63898348SEric.Yu@Sun.COM 
63908348SEric.Yu@Sun.COM 	if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) {
63918348SEric.Yu@Sun.COM 		struct sonode *so = (struct sonode *)buf;
63928348SEric.Yu@Sun.COM 		sotpi_info_t *sti = SOTOTPI(so);
63938348SEric.Yu@Sun.COM 
63948348SEric.Yu@Sun.COM 		mutex_enter(&socklist.sl_lock);
63958348SEric.Yu@Sun.COM 
63968348SEric.Yu@Sun.COM 		sti->sti_next_so = socklist.sl_list;
63978348SEric.Yu@Sun.COM 		sti->sti_prev_so = NULL;
63988348SEric.Yu@Sun.COM 		if (sti->sti_next_so != NULL)
63998348SEric.Yu@Sun.COM 			SOTOTPI(sti->sti_next_so)->sti_prev_so = so;
64008348SEric.Yu@Sun.COM 		socklist.sl_list = so;
64018348SEric.Yu@Sun.COM 
64028348SEric.Yu@Sun.COM 		mutex_exit(&socklist.sl_lock);
64038348SEric.Yu@Sun.COM 
64048348SEric.Yu@Sun.COM 	}
64058348SEric.Yu@Sun.COM 	return (retval);
64068348SEric.Yu@Sun.COM }
64078348SEric.Yu@Sun.COM 
64088348SEric.Yu@Sun.COM static void
64098348SEric.Yu@Sun.COM socktpi_unix_destructor(void *buf, void *cdrarg)
64108348SEric.Yu@Sun.COM {
64118348SEric.Yu@Sun.COM 	struct sonode	*so = (struct sonode *)buf;
64128348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
64138348SEric.Yu@Sun.COM 
64148348SEric.Yu@Sun.COM 	mutex_enter(&socklist.sl_lock);
64158348SEric.Yu@Sun.COM 
64168348SEric.Yu@Sun.COM 	if (sti->sti_next_so != NULL)
64178348SEric.Yu@Sun.COM 		SOTOTPI(sti->sti_next_so)->sti_prev_so = sti->sti_prev_so;
64188348SEric.Yu@Sun.COM 	if (sti->sti_prev_so != NULL)
64198348SEric.Yu@Sun.COM 		SOTOTPI(sti->sti_prev_so)->sti_next_so = sti->sti_next_so;
64208348SEric.Yu@Sun.COM 	else
64218348SEric.Yu@Sun.COM 		socklist.sl_list = sti->sti_next_so;
64228348SEric.Yu@Sun.COM 
64238348SEric.Yu@Sun.COM 	mutex_exit(&socklist.sl_lock);
64248348SEric.Yu@Sun.COM 
64258348SEric.Yu@Sun.COM 	socktpi_destructor(buf, cdrarg);
64268348SEric.Yu@Sun.COM }
64278348SEric.Yu@Sun.COM 
64288348SEric.Yu@Sun.COM int
64298348SEric.Yu@Sun.COM socktpi_init(void)
64308348SEric.Yu@Sun.COM {
64318348SEric.Yu@Sun.COM 	/*
64328348SEric.Yu@Sun.COM 	 * Create sonode caches.  We create a special one for AF_UNIX so
64338348SEric.Yu@Sun.COM 	 * that we can track them for netstat(1m).
64348348SEric.Yu@Sun.COM 	 */
64358348SEric.Yu@Sun.COM 	socktpi_cache = kmem_cache_create("socktpi_cache",
64368348SEric.Yu@Sun.COM 	    sizeof (struct sotpi_sonode), 0, socktpi_constructor,
64378348SEric.Yu@Sun.COM 	    socktpi_destructor, NULL, NULL, NULL, 0);
64388348SEric.Yu@Sun.COM 
64398348SEric.Yu@Sun.COM 	socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache",
64408348SEric.Yu@Sun.COM 	    sizeof (struct sotpi_sonode), 0, socktpi_unix_constructor,
64418348SEric.Yu@Sun.COM 	    socktpi_unix_destructor, NULL, NULL, NULL, 0);
64428348SEric.Yu@Sun.COM 
64438348SEric.Yu@Sun.COM 	return (0);
64448348SEric.Yu@Sun.COM }
64458348SEric.Yu@Sun.COM 
64468348SEric.Yu@Sun.COM /*
64478348SEric.Yu@Sun.COM  * Given a non-TPI sonode, allocate and prep it to be ready for TPI.
64488348SEric.Yu@Sun.COM  *
64498348SEric.Yu@Sun.COM  * Caller must still update state and mode using sotpi_update_state().
64508348SEric.Yu@Sun.COM  */
64518963SAnders.Persson@Sun.COM int
64528348SEric.Yu@Sun.COM sotpi_convert_sonode(struct sonode *so, struct sockparams *newsp,
64538963SAnders.Persson@Sun.COM     boolean_t *direct, queue_t **qp, struct cred *cr)
64548348SEric.Yu@Sun.COM {
64558348SEric.Yu@Sun.COM 	sotpi_info_t *sti;
64568348SEric.Yu@Sun.COM 	struct sockparams *origsp = so->so_sockparams;
64578348SEric.Yu@Sun.COM 	sock_lower_handle_t handle = so->so_proto_handle;
64588348SEric.Yu@Sun.COM 	struct stdata *stp;
64598348SEric.Yu@Sun.COM 	struct vnode *vp;
64608348SEric.Yu@Sun.COM 	queue_t *q;
64618963SAnders.Persson@Sun.COM 	int error = 0;
64628963SAnders.Persson@Sun.COM 
64638963SAnders.Persson@Sun.COM 	ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
64648963SAnders.Persson@Sun.COM 	    SS_FALLBACK_PENDING);
64658963SAnders.Persson@Sun.COM 	ASSERT(SOCK_IS_NONSTR(so));
64668963SAnders.Persson@Sun.COM 
64678963SAnders.Persson@Sun.COM 	*qp = NULL;
64688348SEric.Yu@Sun.COM 	*direct = B_FALSE;
64698348SEric.Yu@Sun.COM 	so->so_sockparams = newsp;
64708348SEric.Yu@Sun.COM 	/*
64718348SEric.Yu@Sun.COM 	 * Allocate and initalize fields required by TPI.
64728348SEric.Yu@Sun.COM 	 */
64738348SEric.Yu@Sun.COM 	(void) sotpi_info_create(so, KM_SLEEP);
64748348SEric.Yu@Sun.COM 	sotpi_info_init(so);
64758348SEric.Yu@Sun.COM 
64768963SAnders.Persson@Sun.COM 	if ((error = sotpi_init(so, NULL, cr, SO_FALLBACK)) != 0) {
64778348SEric.Yu@Sun.COM 		sotpi_info_fini(so);
64788348SEric.Yu@Sun.COM 		sotpi_info_destroy(so);
64798963SAnders.Persson@Sun.COM 		return (error);
64808348SEric.Yu@Sun.COM 	}
64818348SEric.Yu@Sun.COM 	ASSERT(handle == so->so_proto_handle);
64828348SEric.Yu@Sun.COM 	sti = SOTOTPI(so);
64838348SEric.Yu@Sun.COM 	if (sti->sti_direct != 0)
64848348SEric.Yu@Sun.COM 		*direct = B_TRUE;
64858348SEric.Yu@Sun.COM 
64868348SEric.Yu@Sun.COM 	/*
64878963SAnders.Persson@Sun.COM 	 * When it comes to urgent data we have two cases to deal with;
64888963SAnders.Persson@Sun.COM 	 * (1) The oob byte has already arrived, or (2) the protocol has
64898963SAnders.Persson@Sun.COM 	 * notified that oob data is pending, but it has not yet arrived.
64908963SAnders.Persson@Sun.COM 	 *
64918963SAnders.Persson@Sun.COM 	 * For (1) all we need to do is send a T_EXDATA_IND to indicate were
64928963SAnders.Persson@Sun.COM 	 * in the byte stream the oob byte is. For (2) we have to send a
64938963SAnders.Persson@Sun.COM 	 * SIGURG (M_PCSIG), followed by a zero-length mblk indicating whether
64948963SAnders.Persson@Sun.COM 	 * the oob byte will be the next byte from the protocol.
64958963SAnders.Persson@Sun.COM 	 *
64968963SAnders.Persson@Sun.COM 	 * So in the worst case we need two mblks, one for the signal, another
64978963SAnders.Persson@Sun.COM 	 * for mark indication. In that case we use the exdata_mp for the sig.
64988963SAnders.Persson@Sun.COM 	 */
64998963SAnders.Persson@Sun.COM 	sti->sti_exdata_mp = allocb_wait(sizeof (struct T_exdata_ind), BPRI_MED,
65008963SAnders.Persson@Sun.COM 	    STR_NOSIG, NULL);
65018963SAnders.Persson@Sun.COM 	sti->sti_urgmark_mp = allocb_wait(0, BPRI_MED, STR_NOSIG, NULL);
65028963SAnders.Persson@Sun.COM 
65038963SAnders.Persson@Sun.COM 	/*
65048348SEric.Yu@Sun.COM 	 * Keep the original sp around so we can properly dispose of the
65058348SEric.Yu@Sun.COM 	 * sonode when the socket is being closed.
65068348SEric.Yu@Sun.COM 	 */
65078348SEric.Yu@Sun.COM 	sti->sti_orig_sp = origsp;
65088348SEric.Yu@Sun.COM 
65098348SEric.Yu@Sun.COM 	so_basic_strinit(so);	/* skips the T_CAPABILITY_REQ */
65108348SEric.Yu@Sun.COM 	so_alloc_addr(so, so->so_max_addr_len);
65118348SEric.Yu@Sun.COM 
65128348SEric.Yu@Sun.COM 	/*
65138348SEric.Yu@Sun.COM 	 * If the application has done a SIOCSPGRP, make sure the
65148348SEric.Yu@Sun.COM 	 * STREAM head is aware. This needs to take place before
65158348SEric.Yu@Sun.COM 	 * the protocol start sending up messages. Otherwise we
65168348SEric.Yu@Sun.COM 	 * might miss to generate SIGPOLL.
65178348SEric.Yu@Sun.COM 	 *
65188348SEric.Yu@Sun.COM 	 * It is possible that the application will receive duplicate
65198348SEric.Yu@Sun.COM 	 * signals if some were already generated for either data or
65208348SEric.Yu@Sun.COM 	 * connection indications.
65218348SEric.Yu@Sun.COM 	 */
65228348SEric.Yu@Sun.COM 	if (so->so_pgrp != 0) {
65238348SEric.Yu@Sun.COM 		if (so_set_events(so, so->so_vnode, cr) != 0)
65248348SEric.Yu@Sun.COM 			so->so_pgrp = 0;
65258348SEric.Yu@Sun.COM 	}
65268348SEric.Yu@Sun.COM 
65278348SEric.Yu@Sun.COM 	/*
65288348SEric.Yu@Sun.COM 	 * Determine which queue to use.
65298348SEric.Yu@Sun.COM 	 */
65308348SEric.Yu@Sun.COM 	vp = SOTOV(so);
65318348SEric.Yu@Sun.COM 	stp = vp->v_stream;
65328348SEric.Yu@Sun.COM 	ASSERT(stp != NULL);
65338348SEric.Yu@Sun.COM 	q = stp->sd_wrq->q_next;
65348348SEric.Yu@Sun.COM 
65358348SEric.Yu@Sun.COM 	/*
65368348SEric.Yu@Sun.COM 	 * Skip any modules that may have been auto pushed when the device
65378348SEric.Yu@Sun.COM 	 * was opened
65388348SEric.Yu@Sun.COM 	 */
65398348SEric.Yu@Sun.COM 	while (q->q_next != NULL)
65408348SEric.Yu@Sun.COM 		q = q->q_next;
65418963SAnders.Persson@Sun.COM 	*qp = _RD(q);
65428963SAnders.Persson@Sun.COM 
65438963SAnders.Persson@Sun.COM 	/* This is now a STREAMS sockets */
65448963SAnders.Persson@Sun.COM 	so->so_not_str = B_FALSE;
65458963SAnders.Persson@Sun.COM 
65468963SAnders.Persson@Sun.COM 	return (error);
65478963SAnders.Persson@Sun.COM }
65488963SAnders.Persson@Sun.COM 
65498963SAnders.Persson@Sun.COM /*
65508963SAnders.Persson@Sun.COM  * Revert a TPI sonode. It is only allowed to revert the sonode during
65518963SAnders.Persson@Sun.COM  * the fallback process.
65528963SAnders.Persson@Sun.COM  */
65538963SAnders.Persson@Sun.COM void
65548963SAnders.Persson@Sun.COM sotpi_revert_sonode(struct sonode *so, struct cred *cr)
65558963SAnders.Persson@Sun.COM {
65568963SAnders.Persson@Sun.COM 	vnode_t *vp = SOTOV(so);
65578963SAnders.Persson@Sun.COM 
65588963SAnders.Persson@Sun.COM 	ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
65598963SAnders.Persson@Sun.COM 	    SS_FALLBACK_PENDING);
65608963SAnders.Persson@Sun.COM 	ASSERT(!SOCK_IS_NONSTR(so));
65618963SAnders.Persson@Sun.COM 	ASSERT(vp->v_stream != NULL);
65628963SAnders.Persson@Sun.COM 
65638963SAnders.Persson@Sun.COM 	if (SOTOTPI(so)->sti_exdata_mp != NULL) {
65648963SAnders.Persson@Sun.COM 		freeb(SOTOTPI(so)->sti_exdata_mp);
65658963SAnders.Persson@Sun.COM 		SOTOTPI(so)->sti_exdata_mp = NULL;
65668963SAnders.Persson@Sun.COM 	}
65678963SAnders.Persson@Sun.COM 
65688963SAnders.Persson@Sun.COM 	if (SOTOTPI(so)->sti_urgmark_mp != NULL) {
65698963SAnders.Persson@Sun.COM 		freeb(SOTOTPI(so)->sti_urgmark_mp);
65708963SAnders.Persson@Sun.COM 		SOTOTPI(so)->sti_urgmark_mp = NULL;
65718963SAnders.Persson@Sun.COM 	}
65728963SAnders.Persson@Sun.COM 
65738963SAnders.Persson@Sun.COM 	strclean(vp);
65748963SAnders.Persson@Sun.COM 	(void) strclose(vp, FREAD|FWRITE|SO_FALLBACK, cr);
65758963SAnders.Persson@Sun.COM 
65768963SAnders.Persson@Sun.COM 	/*
65778963SAnders.Persson@Sun.COM 	 * Restore the original sockparams. The caller is responsible for
65788963SAnders.Persson@Sun.COM 	 * dropping the ref to the new sp.
65798963SAnders.Persson@Sun.COM 	 */
65808963SAnders.Persson@Sun.COM 	so->so_sockparams = SOTOTPI(so)->sti_orig_sp;
65818963SAnders.Persson@Sun.COM 
65828963SAnders.Persson@Sun.COM 	sotpi_info_fini(so);
65838963SAnders.Persson@Sun.COM 	sotpi_info_destroy(so);
65848963SAnders.Persson@Sun.COM 
65858963SAnders.Persson@Sun.COM 	/* This is no longer a STREAMS sockets */
65868963SAnders.Persson@Sun.COM 	so->so_not_str = B_TRUE;
65878348SEric.Yu@Sun.COM }
65888348SEric.Yu@Sun.COM 
65898348SEric.Yu@Sun.COM void
65908348SEric.Yu@Sun.COM sotpi_update_state(struct sonode *so, struct T_capability_ack *tcap,
65918348SEric.Yu@Sun.COM     struct sockaddr *laddr, socklen_t laddrlen, struct sockaddr *faddr,
65928348SEric.Yu@Sun.COM     socklen_t faddrlen, short opts)
65938348SEric.Yu@Sun.COM {
65948348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
65958348SEric.Yu@Sun.COM 
65968348SEric.Yu@Sun.COM 	so_proc_tcapability_ack(so, tcap);
65978348SEric.Yu@Sun.COM 
65988348SEric.Yu@Sun.COM 	so->so_options |= opts;
65998348SEric.Yu@Sun.COM 
66008348SEric.Yu@Sun.COM 	/*
66018348SEric.Yu@Sun.COM 	 * Determine whether the foreign and local address are valid
66028348SEric.Yu@Sun.COM 	 */
66038348SEric.Yu@Sun.COM 	if (laddrlen != 0) {
66048348SEric.Yu@Sun.COM 		ASSERT(laddrlen <= sti->sti_laddr_maxlen);
66058348SEric.Yu@Sun.COM 		sti->sti_laddr_len = laddrlen;
66068348SEric.Yu@Sun.COM 		bcopy(laddr, sti->sti_laddr_sa, laddrlen);
66078348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = (so->so_state & SS_ISBOUND);
66088348SEric.Yu@Sun.COM 	}
66098348SEric.Yu@Sun.COM 
66108348SEric.Yu@Sun.COM 	if (faddrlen != 0) {
66118348SEric.Yu@Sun.COM 		ASSERT(faddrlen <= sti->sti_faddr_maxlen);
66128348SEric.Yu@Sun.COM 		sti->sti_faddr_len = faddrlen;
66138348SEric.Yu@Sun.COM 		bcopy(faddr, sti->sti_faddr_sa, faddrlen);
66148348SEric.Yu@Sun.COM 		sti->sti_faddr_valid = (so->so_state & SS_ISCONNECTED);
66158348SEric.Yu@Sun.COM 	}
66168348SEric.Yu@Sun.COM 
66178348SEric.Yu@Sun.COM }
66188348SEric.Yu@Sun.COM 
66198348SEric.Yu@Sun.COM /*
66208348SEric.Yu@Sun.COM  * Allocate enough space to cache the local and foreign addresses.
66218348SEric.Yu@Sun.COM  */
66228348SEric.Yu@Sun.COM void
66238348SEric.Yu@Sun.COM so_alloc_addr(struct sonode *so, t_uscalar_t maxlen)
66248348SEric.Yu@Sun.COM {
66258348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
66268348SEric.Yu@Sun.COM 
66278348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
66288348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_len == 0 && sti->sti_faddr_len == 0);
66298348SEric.Yu@Sun.COM 	sti->sti_laddr_maxlen = sti->sti_faddr_maxlen =
66308348SEric.Yu@Sun.COM 	    P2ROUNDUP(maxlen, KMEM_ALIGN);
66318348SEric.Yu@Sun.COM 	so->so_max_addr_len = sti->sti_laddr_maxlen;
66328348SEric.Yu@Sun.COM 	sti->sti_laddr_sa = kmem_alloc(sti->sti_laddr_maxlen * 2, KM_SLEEP);
66338348SEric.Yu@Sun.COM 	sti->sti_faddr_sa = (struct sockaddr *)((caddr_t)sti->sti_laddr_sa
66348348SEric.Yu@Sun.COM 	    + sti->sti_laddr_maxlen);
66358348SEric.Yu@Sun.COM 
66368348SEric.Yu@Sun.COM 	if (so->so_family == AF_UNIX) {
66378348SEric.Yu@Sun.COM 		/*
66388348SEric.Yu@Sun.COM 		 * Initialize AF_UNIX related fields.
66398348SEric.Yu@Sun.COM 		 */
66408348SEric.Yu@Sun.COM 		bzero(&sti->sti_ux_laddr, sizeof (sti->sti_ux_laddr));
66418348SEric.Yu@Sun.COM 		bzero(&sti->sti_ux_faddr, sizeof (sti->sti_ux_faddr));
66428348SEric.Yu@Sun.COM 	}
66438348SEric.Yu@Sun.COM }
66448348SEric.Yu@Sun.COM 
66458348SEric.Yu@Sun.COM 
66468348SEric.Yu@Sun.COM sotpi_info_t *
66478348SEric.Yu@Sun.COM sotpi_sototpi(struct sonode *so)
66488348SEric.Yu@Sun.COM {
66498348SEric.Yu@Sun.COM 	sotpi_info_t *sti;
66508348SEric.Yu@Sun.COM 
66518963SAnders.Persson@Sun.COM 	ASSERT(so != NULL);
66528348SEric.Yu@Sun.COM 
66538348SEric.Yu@Sun.COM 	sti = (sotpi_info_t *)so->so_priv;
66548348SEric.Yu@Sun.COM 
66558348SEric.Yu@Sun.COM 	ASSERT(sti != NULL);
66568348SEric.Yu@Sun.COM 	ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
66578348SEric.Yu@Sun.COM 
66588348SEric.Yu@Sun.COM 	return (sti);
66598348SEric.Yu@Sun.COM }
66608348SEric.Yu@Sun.COM 
66618348SEric.Yu@Sun.COM static int
66628348SEric.Yu@Sun.COM i_sotpi_info_constructor(sotpi_info_t *sti)
66638348SEric.Yu@Sun.COM {
66648348SEric.Yu@Sun.COM 	sti->sti_magic		= SOTPI_INFO_MAGIC;
66658348SEric.Yu@Sun.COM 	sti->sti_ack_mp		= NULL;
66668348SEric.Yu@Sun.COM 	sti->sti_discon_ind_mp	= NULL;
66678348SEric.Yu@Sun.COM 	sti->sti_ux_bound_vp	= NULL;
66688348SEric.Yu@Sun.COM 	sti->sti_unbind_mp	= NULL;
66698348SEric.Yu@Sun.COM 
66708348SEric.Yu@Sun.COM 	sti->sti_conn_ind_head	= NULL;
66718348SEric.Yu@Sun.COM 	sti->sti_conn_ind_tail	= NULL;
66728348SEric.Yu@Sun.COM 
66738348SEric.Yu@Sun.COM 	sti->sti_laddr_sa	= NULL;
66748348SEric.Yu@Sun.COM 	sti->sti_faddr_sa	= NULL;
66758348SEric.Yu@Sun.COM 
66768348SEric.Yu@Sun.COM 	sti->sti_nl7c_flags	= 0;
66778348SEric.Yu@Sun.COM 	sti->sti_nl7c_uri	= NULL;
66788348SEric.Yu@Sun.COM 	sti->sti_nl7c_rcv_mp	= NULL;
66798348SEric.Yu@Sun.COM 
66808963SAnders.Persson@Sun.COM 	sti->sti_exdata_mp	= NULL;
66818963SAnders.Persson@Sun.COM 	sti->sti_urgmark_mp	= NULL;
66828963SAnders.Persson@Sun.COM 
66838348SEric.Yu@Sun.COM 	mutex_init(&sti->sti_plumb_lock, NULL, MUTEX_DEFAULT, NULL);
66848348SEric.Yu@Sun.COM 	cv_init(&sti->sti_ack_cv, NULL, CV_DEFAULT, NULL);
66858348SEric.Yu@Sun.COM 
66868348SEric.Yu@Sun.COM 	return (0);
66878348SEric.Yu@Sun.COM }
66888348SEric.Yu@Sun.COM 
66898348SEric.Yu@Sun.COM static void
66908348SEric.Yu@Sun.COM i_sotpi_info_destructor(sotpi_info_t *sti)
66918348SEric.Yu@Sun.COM {
66928348SEric.Yu@Sun.COM 	ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
66938348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ack_mp == NULL);
66948348SEric.Yu@Sun.COM 	ASSERT(sti->sti_discon_ind_mp == NULL);
66958348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ux_bound_vp == NULL);
66968348SEric.Yu@Sun.COM 	ASSERT(sti->sti_unbind_mp == NULL);
66978348SEric.Yu@Sun.COM 
66988348SEric.Yu@Sun.COM 	ASSERT(sti->sti_conn_ind_head == NULL);
66998348SEric.Yu@Sun.COM 	ASSERT(sti->sti_conn_ind_tail == NULL);
67008348SEric.Yu@Sun.COM 
67018348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_sa == NULL);
67028348SEric.Yu@Sun.COM 	ASSERT(sti->sti_faddr_sa == NULL);
67038348SEric.Yu@Sun.COM 
67048348SEric.Yu@Sun.COM 	ASSERT(sti->sti_nl7c_flags == 0);
67058348SEric.Yu@Sun.COM 	ASSERT(sti->sti_nl7c_uri == NULL);
67068348SEric.Yu@Sun.COM 	ASSERT(sti->sti_nl7c_rcv_mp == NULL);
67078348SEric.Yu@Sun.COM 
67088963SAnders.Persson@Sun.COM 	ASSERT(sti->sti_exdata_mp == NULL);
67098963SAnders.Persson@Sun.COM 	ASSERT(sti->sti_urgmark_mp == NULL);
67108963SAnders.Persson@Sun.COM 
67118348SEric.Yu@Sun.COM 	mutex_destroy(&sti->sti_plumb_lock);
67128348SEric.Yu@Sun.COM 	cv_destroy(&sti->sti_ack_cv);
67138348SEric.Yu@Sun.COM }
67148348SEric.Yu@Sun.COM 
67158348SEric.Yu@Sun.COM /*
67168348SEric.Yu@Sun.COM  * Creates and attaches TPI information to the given sonode
67178348SEric.Yu@Sun.COM  */
67188348SEric.Yu@Sun.COM static boolean_t
67198348SEric.Yu@Sun.COM sotpi_info_create(struct sonode *so, int kmflags)
67208348SEric.Yu@Sun.COM {
67218348SEric.Yu@Sun.COM 	sotpi_info_t *sti;
67228348SEric.Yu@Sun.COM 
67238348SEric.Yu@Sun.COM 	ASSERT(so->so_priv == NULL);
67248348SEric.Yu@Sun.COM 
67258348SEric.Yu@Sun.COM 	if ((sti = kmem_zalloc(sizeof (*sti), kmflags)) == NULL)
67268348SEric.Yu@Sun.COM 		return (B_FALSE);
67278348SEric.Yu@Sun.COM 
67288348SEric.Yu@Sun.COM 	if (i_sotpi_info_constructor(sti) != 0) {
67298348SEric.Yu@Sun.COM 		kmem_free(sti, sizeof (*sti));
67308348SEric.Yu@Sun.COM 		return (B_FALSE);
67318348SEric.Yu@Sun.COM 	}
67328348SEric.Yu@Sun.COM 
67338348SEric.Yu@Sun.COM 	so->so_priv = (void *)sti;
67348348SEric.Yu@Sun.COM 	return (B_TRUE);
67358348SEric.Yu@Sun.COM }
67368348SEric.Yu@Sun.COM 
67378348SEric.Yu@Sun.COM /*
67388348SEric.Yu@Sun.COM  * Initializes the TPI information.
67398348SEric.Yu@Sun.COM  */
67408348SEric.Yu@Sun.COM static void
67418348SEric.Yu@Sun.COM sotpi_info_init(struct sonode *so)
67428348SEric.Yu@Sun.COM {
67438348SEric.Yu@Sun.COM 	struct vnode *vp = SOTOV(so);
67448348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
67458348SEric.Yu@Sun.COM 	time_t now;
67468348SEric.Yu@Sun.COM 
67478348SEric.Yu@Sun.COM 	sti->sti_dev 	= so->so_sockparams->sp_sdev_info.sd_vnode->v_rdev;
67488348SEric.Yu@Sun.COM 	vp->v_rdev	= sti->sti_dev;
67498348SEric.Yu@Sun.COM 
67508348SEric.Yu@Sun.COM 	sti->sti_orig_sp = NULL;
67518348SEric.Yu@Sun.COM 
67528348SEric.Yu@Sun.COM 	sti->sti_pushcnt = 0;
67538348SEric.Yu@Sun.COM 
67548348SEric.Yu@Sun.COM 	now = gethrestime_sec();
67558348SEric.Yu@Sun.COM 	sti->sti_atime	= now;
67568348SEric.Yu@Sun.COM 	sti->sti_mtime	= now;
67578348SEric.Yu@Sun.COM 	sti->sti_ctime	= now;
67588348SEric.Yu@Sun.COM 
67598348SEric.Yu@Sun.COM 	sti->sti_eaddr_mp = NULL;
67608348SEric.Yu@Sun.COM 	sti->sti_delayed_error = 0;
67618348SEric.Yu@Sun.COM 
67628348SEric.Yu@Sun.COM 	sti->sti_provinfo = NULL;
67638348SEric.Yu@Sun.COM 
67648348SEric.Yu@Sun.COM 	sti->sti_oobcnt = 0;
67658348SEric.Yu@Sun.COM 	sti->sti_oobsigcnt = 0;
67668348SEric.Yu@Sun.COM 
67678348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
67688348SEric.Yu@Sun.COM 
67698348SEric.Yu@Sun.COM 	sti->sti_laddr_sa	= 0;
67708348SEric.Yu@Sun.COM 	sti->sti_faddr_sa	= 0;
67718348SEric.Yu@Sun.COM 	sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 0;
67728348SEric.Yu@Sun.COM 	sti->sti_laddr_len = sti->sti_faddr_len = 0;
67738348SEric.Yu@Sun.COM 
67748348SEric.Yu@Sun.COM 	sti->sti_laddr_valid = 0;
67758348SEric.Yu@Sun.COM 	sti->sti_faddr_valid = 0;
67768348SEric.Yu@Sun.COM 	sti->sti_faddr_noxlate = 0;
67778348SEric.Yu@Sun.COM 
67788348SEric.Yu@Sun.COM 	sti->sti_direct = 0;
67798348SEric.Yu@Sun.COM 
67808348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ack_mp == NULL);
67818348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ux_bound_vp == NULL);
67828348SEric.Yu@Sun.COM 	ASSERT(sti->sti_unbind_mp == NULL);
67838348SEric.Yu@Sun.COM 
67848348SEric.Yu@Sun.COM 	ASSERT(sti->sti_conn_ind_head == NULL);
67858348SEric.Yu@Sun.COM 	ASSERT(sti->sti_conn_ind_tail == NULL);
67868348SEric.Yu@Sun.COM 
67878348SEric.Yu@Sun.COM 	/* Initialize the kernel SSL proxy fields */
67888348SEric.Yu@Sun.COM 	sti->sti_kssl_type = KSSL_NO_PROXY;
67898348SEric.Yu@Sun.COM 	sti->sti_kssl_ent = NULL;
67908348SEric.Yu@Sun.COM 	sti->sti_kssl_ctx = NULL;
67918348SEric.Yu@Sun.COM }
67928348SEric.Yu@Sun.COM 
67938348SEric.Yu@Sun.COM /*
67948348SEric.Yu@Sun.COM  * Given a sonode, grab the TPI info and free any data.
67958348SEric.Yu@Sun.COM  */
67968348SEric.Yu@Sun.COM static void
67978348SEric.Yu@Sun.COM sotpi_info_fini(struct sonode *so)
67988348SEric.Yu@Sun.COM {
67998348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
68008348SEric.Yu@Sun.COM 	mblk_t *mp;
68018348SEric.Yu@Sun.COM 
68028348SEric.Yu@Sun.COM 	ASSERT(sti->sti_discon_ind_mp == NULL);
68038348SEric.Yu@Sun.COM 
68048348SEric.Yu@Sun.COM 	if ((mp = sti->sti_conn_ind_head) != NULL) {
68058348SEric.Yu@Sun.COM 		mblk_t *mp1;
68068348SEric.Yu@Sun.COM 
68078348SEric.Yu@Sun.COM 		while (mp) {
68088348SEric.Yu@Sun.COM 			mp1 = mp->b_next;
68098348SEric.Yu@Sun.COM 			mp->b_next = NULL;
68108348SEric.Yu@Sun.COM 			freemsg(mp);
68118348SEric.Yu@Sun.COM 			mp = mp1;
68128348SEric.Yu@Sun.COM 		}
68138348SEric.Yu@Sun.COM 		sti->sti_conn_ind_head = sti->sti_conn_ind_tail = NULL;
68148348SEric.Yu@Sun.COM 	}
68158348SEric.Yu@Sun.COM 
68168348SEric.Yu@Sun.COM 	/*
68178348SEric.Yu@Sun.COM 	 * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely
68188348SEric.Yu@Sun.COM 	 * indirect them.  It also uses so_count as a validity test.
68198348SEric.Yu@Sun.COM 	 */
68208348SEric.Yu@Sun.COM 	mutex_enter(&so->so_lock);
68218348SEric.Yu@Sun.COM 
68228348SEric.Yu@Sun.COM 	if (sti->sti_laddr_sa) {
68238348SEric.Yu@Sun.COM 		ASSERT((caddr_t)sti->sti_faddr_sa ==
68248348SEric.Yu@Sun.COM 		    (caddr_t)sti->sti_laddr_sa + sti->sti_laddr_maxlen);
68258348SEric.Yu@Sun.COM 		ASSERT(sti->sti_faddr_maxlen == sti->sti_laddr_maxlen);
68268348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
68278348SEric.Yu@Sun.COM 		sti->sti_faddr_valid = 0;
68288348SEric.Yu@Sun.COM 		kmem_free(sti->sti_laddr_sa, sti->sti_laddr_maxlen * 2);
68298348SEric.Yu@Sun.COM 		sti->sti_laddr_sa = NULL;
68308348SEric.Yu@Sun.COM 		sti->sti_laddr_len = sti->sti_laddr_maxlen = 0;
68318348SEric.Yu@Sun.COM 		sti->sti_faddr_sa = NULL;
68328348SEric.Yu@Sun.COM 		sti->sti_faddr_len = sti->sti_faddr_maxlen = 0;
68338348SEric.Yu@Sun.COM 	}
68348348SEric.Yu@Sun.COM 
68358348SEric.Yu@Sun.COM 	mutex_exit(&so->so_lock);
68368348SEric.Yu@Sun.COM 
68378348SEric.Yu@Sun.COM 	if ((mp = sti->sti_eaddr_mp) != NULL) {
68388348SEric.Yu@Sun.COM 		freemsg(mp);
68398348SEric.Yu@Sun.COM 		sti->sti_eaddr_mp = NULL;
68408348SEric.Yu@Sun.COM 		sti->sti_delayed_error = 0;
68418348SEric.Yu@Sun.COM 	}
68428348SEric.Yu@Sun.COM 
68438348SEric.Yu@Sun.COM 	if ((mp = sti->sti_ack_mp) != NULL) {
68448348SEric.Yu@Sun.COM 		freemsg(mp);
68458348SEric.Yu@Sun.COM 		sti->sti_ack_mp = NULL;
68468348SEric.Yu@Sun.COM 	}
68478348SEric.Yu@Sun.COM 
68488348SEric.Yu@Sun.COM 	if ((mp = sti->sti_nl7c_rcv_mp) != NULL) {
68498348SEric.Yu@Sun.COM 		sti->sti_nl7c_rcv_mp = NULL;
68508348SEric.Yu@Sun.COM 		freemsg(mp);
68518348SEric.Yu@Sun.COM 	}
68528348SEric.Yu@Sun.COM 	sti->sti_nl7c_rcv_rval = 0;
68538348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_uri != NULL) {
68548348SEric.Yu@Sun.COM 		nl7c_urifree(so);
68558348SEric.Yu@Sun.COM 		/* urifree() cleared nl7c_uri */
68568348SEric.Yu@Sun.COM 	}
68578348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_flags) {
68588348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags = 0;
68598348SEric.Yu@Sun.COM 	}
68608348SEric.Yu@Sun.COM 
68618348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ux_bound_vp == NULL);
68628348SEric.Yu@Sun.COM 	if ((mp = sti->sti_unbind_mp) != NULL) {
68638348SEric.Yu@Sun.COM 		freemsg(mp);
68648348SEric.Yu@Sun.COM 		sti->sti_unbind_mp = NULL;
68658348SEric.Yu@Sun.COM 	}
68668348SEric.Yu@Sun.COM }
68678348SEric.Yu@Sun.COM 
68688348SEric.Yu@Sun.COM /*
68698348SEric.Yu@Sun.COM  * Destroys the TPI information attached to a sonode.
68708348SEric.Yu@Sun.COM  */
68718348SEric.Yu@Sun.COM static void
68728348SEric.Yu@Sun.COM sotpi_info_destroy(struct sonode *so)
68738348SEric.Yu@Sun.COM {
68748348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
68758348SEric.Yu@Sun.COM 
68768348SEric.Yu@Sun.COM 	i_sotpi_info_destructor(sti);
68778348SEric.Yu@Sun.COM 	kmem_free(sti, sizeof (*sti));
68788348SEric.Yu@Sun.COM 
68798348SEric.Yu@Sun.COM 	so->so_priv = NULL;
68808348SEric.Yu@Sun.COM }
68818348SEric.Yu@Sun.COM 
68828348SEric.Yu@Sun.COM /*
68838429SEric.Yu@Sun.COM  * Create the global sotpi socket module entry. It will never be freed.
68848348SEric.Yu@Sun.COM  */
68858348SEric.Yu@Sun.COM smod_info_t *
68868348SEric.Yu@Sun.COM sotpi_smod_create(void)
68878348SEric.Yu@Sun.COM {
68888348SEric.Yu@Sun.COM 	smod_info_t *smodp;
68898348SEric.Yu@Sun.COM 
68908348SEric.Yu@Sun.COM 	smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP);
68918429SEric.Yu@Sun.COM 	smodp->smod_name = kmem_alloc(sizeof (SOTPI_SMOD_NAME), KM_SLEEP);
68928429SEric.Yu@Sun.COM 	(void) strcpy(smodp->smod_name, SOTPI_SMOD_NAME);
68938348SEric.Yu@Sun.COM 	/*
68948429SEric.Yu@Sun.COM 	 * Initialize the smod_refcnt to 1 so it will never be freed.
68958348SEric.Yu@Sun.COM 	 */
68968348SEric.Yu@Sun.COM 	smodp->smod_refcnt = 1;
68978348SEric.Yu@Sun.COM 	smodp->smod_uc_version = SOCK_UC_VERSION;
68988348SEric.Yu@Sun.COM 	smodp->smod_dc_version = SOCK_DC_VERSION;
68998348SEric.Yu@Sun.COM 	smodp->smod_sock_create_func = &sotpi_create;
69008348SEric.Yu@Sun.COM 	smodp->smod_sock_destroy_func = &sotpi_destroy;
69018348SEric.Yu@Sun.COM 	return (smodp);
69028348SEric.Yu@Sun.COM }
6903