xref: /onnv-gate/usr/src/uts/common/fs/sockfs/socktpi.c (revision 13075:71556dfac9dc)
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 /*
2312643SAnders.Persson@Sun.COM  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/t_lock.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/buf.h>
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/cred.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
348348SEric.Yu@Sun.COM #include <sys/kmem_impl.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/vfs.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/errno.h>
400Sstevel@tonic-gate #include <sys/time.h>
410Sstevel@tonic-gate #include <sys/file.h>
420Sstevel@tonic-gate #include <sys/open.h>
430Sstevel@tonic-gate #include <sys/user.h>
440Sstevel@tonic-gate #include <sys/termios.h>
450Sstevel@tonic-gate #include <sys/stream.h>
460Sstevel@tonic-gate #include <sys/strsubr.h>
470Sstevel@tonic-gate #include <sys/strsun.h>
488348SEric.Yu@Sun.COM #include <sys/suntpi.h>
490Sstevel@tonic-gate #include <sys/ddi.h>
500Sstevel@tonic-gate #include <sys/esunddi.h>
510Sstevel@tonic-gate #include <sys/flock.h>
520Sstevel@tonic-gate #include <sys/modctl.h>
530Sstevel@tonic-gate #include <sys/vtrace.h>
540Sstevel@tonic-gate #include <sys/cmn_err.h>
550Sstevel@tonic-gate #include <sys/pathname.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <sys/socket.h>
580Sstevel@tonic-gate #include <sys/socketvar.h>
59741Smasputra #include <sys/sockio.h>
600Sstevel@tonic-gate #include <netinet/in.h>
610Sstevel@tonic-gate #include <sys/un.h>
620Sstevel@tonic-gate #include <sys/strsun.h>
630Sstevel@tonic-gate 
640Sstevel@tonic-gate #include <sys/tiuser.h>
650Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
660Sstevel@tonic-gate #include <sys/tihdr.h>
670Sstevel@tonic-gate #include <sys/timod.h>		/* TI_GETMYNAME, TI_GETPEERNAME */
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <c2/audit.h>
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #include <inet/common.h>
720Sstevel@tonic-gate #include <inet/ip.h>
730Sstevel@tonic-gate #include <inet/ip6.h>
740Sstevel@tonic-gate #include <inet/tcp.h>
75741Smasputra #include <inet/udp_impl.h>
760Sstevel@tonic-gate 
771974Sbrutus #include <sys/zone.h>
781974Sbrutus 
790Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
801974Sbrutus #include <fs/sockfs/nl7curi.h>
810Sstevel@tonic-gate 
828348SEric.Yu@Sun.COM #include <fs/sockfs/sockcommon.h>
838348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h>
848348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi_impl.h>
858348SEric.Yu@Sun.COM 
860Sstevel@tonic-gate /*
870Sstevel@tonic-gate  * Possible failures when memory can't be allocated. The documented behavior:
880Sstevel@tonic-gate  *
890Sstevel@tonic-gate  * 		5.5:			4.X:		XNET:
900Sstevel@tonic-gate  * accept:	ENOMEM/ENOSR/EINTR	- (EINTR)	ENOMEM/ENOBUFS/ENOSR/
910Sstevel@tonic-gate  *							EINTR
920Sstevel@tonic-gate  *	(4.X does not document EINTR but returns it)
930Sstevel@tonic-gate  * bind:	ENOSR			-		ENOBUFS/ENOSR
940Sstevel@tonic-gate  * connect: 	EINTR			EINTR		ENOBUFS/ENOSR/EINTR
950Sstevel@tonic-gate  * getpeername:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
960Sstevel@tonic-gate  * getsockname:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
970Sstevel@tonic-gate  *	(4.X getpeername and getsockname do not fail in practice)
980Sstevel@tonic-gate  * getsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
990Sstevel@tonic-gate  * listen:	-			-		ENOBUFS
1000Sstevel@tonic-gate  * recv:	ENOMEM/ENOSR/EINTR	EINTR		ENOBUFS/ENOMEM/ENOSR/
1010Sstevel@tonic-gate  *							EINTR
1020Sstevel@tonic-gate  * send:	ENOMEM/ENOSR/EINTR	ENOBUFS/EINTR	ENOBUFS/ENOMEM/ENOSR/
1030Sstevel@tonic-gate  *							EINTR
1040Sstevel@tonic-gate  * setsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
1050Sstevel@tonic-gate  * shutdown:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
1060Sstevel@tonic-gate  * socket:	ENOMEM/ENOSR		ENOBUFS		ENOBUFS/ENOMEM/ENOSR
1070Sstevel@tonic-gate  * socketpair:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
1080Sstevel@tonic-gate  *
1090Sstevel@tonic-gate  * Resolution. When allocation fails:
1100Sstevel@tonic-gate  *	recv: return EINTR
1110Sstevel@tonic-gate  *	send: return EINTR
1120Sstevel@tonic-gate  *	connect, accept: EINTR
1130Sstevel@tonic-gate  *	bind, listen, shutdown (unbind, unix_close, disconnect): sleep
1140Sstevel@tonic-gate  *	socket, socketpair: ENOBUFS
1150Sstevel@tonic-gate  *	getpeername, getsockname: sleep
1160Sstevel@tonic-gate  *	getsockopt, setsockopt: sleep
1170Sstevel@tonic-gate  */
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate #ifdef SOCK_TEST
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * Variables that make sockfs do something other than the standard TPI
1220Sstevel@tonic-gate  * for the AF_INET transports.
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  * solisten_tpi_tcp:
1250Sstevel@tonic-gate  *	TCP can handle a O_T_BIND_REQ with an increased backlog even though
1260Sstevel@tonic-gate  *	the transport is already bound. This is needed to avoid loosing the
1270Sstevel@tonic-gate  *	port number should listen() do a T_UNBIND_REQ followed by a
1280Sstevel@tonic-gate  *	O_T_BIND_REQ.
1290Sstevel@tonic-gate  *
1300Sstevel@tonic-gate  * soconnect_tpi_udp:
1310Sstevel@tonic-gate  *	UDP and ICMP can handle a T_CONN_REQ.
1320Sstevel@tonic-gate  *	This is needed to make the sequence of connect(), getsockname()
1330Sstevel@tonic-gate  *	return the local IP address used to send packets to the connected to
1340Sstevel@tonic-gate  *	destination.
1350Sstevel@tonic-gate  *
1360Sstevel@tonic-gate  * soconnect_tpi_tcp:
1370Sstevel@tonic-gate  *	TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ.
1380Sstevel@tonic-gate  *	Set this to non-zero to send TPI conformant messages to TCP in this
1390Sstevel@tonic-gate  *	respect. This is a performance optimization.
1400Sstevel@tonic-gate  *
1410Sstevel@tonic-gate  * soaccept_tpi_tcp:
1420Sstevel@tonic-gate  *	TCP can handle a T_CONN_REQ without the acceptor being bound.
1430Sstevel@tonic-gate  *	This is a performance optimization that has been picked up in XTI.
1440Sstevel@tonic-gate  *
1450Sstevel@tonic-gate  * soaccept_tpi_multioptions:
1460Sstevel@tonic-gate  *	When inheriting SOL_SOCKET options from the listener to the accepting
1470Sstevel@tonic-gate  *	socket send them as a single message for AF_INET{,6}.
1480Sstevel@tonic-gate  */
1490Sstevel@tonic-gate int solisten_tpi_tcp = 0;
1500Sstevel@tonic-gate int soconnect_tpi_udp = 0;
1510Sstevel@tonic-gate int soconnect_tpi_tcp = 0;
1520Sstevel@tonic-gate int soaccept_tpi_tcp = 0;
1530Sstevel@tonic-gate int soaccept_tpi_multioptions = 1;
1540Sstevel@tonic-gate #else /* SOCK_TEST */
1550Sstevel@tonic-gate #define	soconnect_tpi_tcp	0
1560Sstevel@tonic-gate #define	soconnect_tpi_udp	0
1570Sstevel@tonic-gate #define	solisten_tpi_tcp	0
1580Sstevel@tonic-gate #define	soaccept_tpi_tcp	0
1590Sstevel@tonic-gate #define	soaccept_tpi_multioptions	1
1600Sstevel@tonic-gate #endif /* SOCK_TEST */
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate #ifdef SOCK_TEST
1630Sstevel@tonic-gate extern int do_useracc;
1640Sstevel@tonic-gate extern clock_t sock_test_timelimit;
1650Sstevel@tonic-gate #endif /* SOCK_TEST */
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate /*
1680Sstevel@tonic-gate  * Some X/Open added checks might have to be backed out to keep SunOS 4.X
1690Sstevel@tonic-gate  * applications working. Turn on this flag to disable these checks.
1700Sstevel@tonic-gate  */
1710Sstevel@tonic-gate int xnet_skip_checks = 0;
1720Sstevel@tonic-gate int xnet_check_print = 0;
1730Sstevel@tonic-gate int xnet_truncate_print = 0;
1740Sstevel@tonic-gate 
1758348SEric.Yu@Sun.COM static void sotpi_destroy(struct sonode *);
1768348SEric.Yu@Sun.COM static struct sonode *sotpi_create(struct sockparams *, int, int, int, int,
1778348SEric.Yu@Sun.COM     int, int *, cred_t *cr);
1788348SEric.Yu@Sun.COM 
1798348SEric.Yu@Sun.COM static boolean_t	sotpi_info_create(struct sonode *, int);
1808348SEric.Yu@Sun.COM static void		sotpi_info_init(struct sonode *);
1818348SEric.Yu@Sun.COM static void 		sotpi_info_fini(struct sonode *);
1828348SEric.Yu@Sun.COM static void 		sotpi_info_destroy(struct sonode *);
1838348SEric.Yu@Sun.COM 
1848348SEric.Yu@Sun.COM /*
1858348SEric.Yu@Sun.COM  * Do direct function call to the transport layer below; this would
1868348SEric.Yu@Sun.COM  * also allow the transport to utilize read-side synchronous stream
1878348SEric.Yu@Sun.COM  * interface if necessary.  This is a /etc/system tunable that must
1888348SEric.Yu@Sun.COM  * not be modified on a running system.  By default this is enabled
1898348SEric.Yu@Sun.COM  * for performance reasons and may be disabled for debugging purposes.
1908348SEric.Yu@Sun.COM  */
1918348SEric.Yu@Sun.COM boolean_t socktpi_direct = B_TRUE;
1928348SEric.Yu@Sun.COM 
1938348SEric.Yu@Sun.COM static struct kmem_cache *socktpi_cache, *socktpi_unix_cache;
1948348SEric.Yu@Sun.COM 
1950Sstevel@tonic-gate extern	void sigintr(k_sigset_t *, int);
1960Sstevel@tonic-gate extern	void sigunintr(k_sigset_t *);
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate static int	sotpi_unbind(struct sonode *, int);
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate /* TPI sockfs sonode operations */
2018348SEric.Yu@Sun.COM int 		sotpi_init(struct sonode *, struct sonode *, struct cred *,
2028348SEric.Yu@Sun.COM 		    int);
2038348SEric.Yu@Sun.COM static int	sotpi_accept(struct sonode *, int, struct cred *,
2048348SEric.Yu@Sun.COM 		    struct sonode **);
2050Sstevel@tonic-gate static int	sotpi_bind(struct sonode *, struct sockaddr *, socklen_t,
2068348SEric.Yu@Sun.COM 		    int, struct cred *);
2078348SEric.Yu@Sun.COM static int	sotpi_listen(struct sonode *, int, struct cred *);
20812643SAnders.Persson@Sun.COM static int	sotpi_connect(struct sonode *, struct sockaddr *,
2098348SEric.Yu@Sun.COM 		    socklen_t, int, int, struct cred *);
2108348SEric.Yu@Sun.COM extern int	sotpi_recvmsg(struct sonode *, struct nmsghdr *,
2118348SEric.Yu@Sun.COM 		    struct uio *, struct cred *);
2120Sstevel@tonic-gate static int	sotpi_sendmsg(struct sonode *, struct nmsghdr *,
2138348SEric.Yu@Sun.COM 		    struct uio *, struct cred *);
2148348SEric.Yu@Sun.COM static int	sotpi_sendmblk(struct sonode *, struct nmsghdr *, int,
2158348SEric.Yu@Sun.COM 		    struct cred *, mblk_t **);
216741Smasputra static int	sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t,
217741Smasputra 		    struct uio *, void *, t_uscalar_t, int);
218741Smasputra static int	sodgram_direct(struct sonode *, struct sockaddr *,
219741Smasputra 		    socklen_t, struct uio *, int);
2208348SEric.Yu@Sun.COM extern int	sotpi_getpeername(struct sonode *, struct sockaddr *,
2218348SEric.Yu@Sun.COM 		    socklen_t *, boolean_t, struct cred *);
2228348SEric.Yu@Sun.COM static int	sotpi_getsockname(struct sonode *, struct sockaddr *,
2238348SEric.Yu@Sun.COM 		    socklen_t *, struct cred *);
2248348SEric.Yu@Sun.COM static int	sotpi_shutdown(struct sonode *, int, struct cred *);
2258348SEric.Yu@Sun.COM extern int	sotpi_getsockopt(struct sonode *, int, int, void *,
2268348SEric.Yu@Sun.COM 		    socklen_t *, int, struct cred *);
2278348SEric.Yu@Sun.COM extern int	sotpi_setsockopt(struct sonode *, int, int, const void *,
2288348SEric.Yu@Sun.COM 		    socklen_t, struct cred *);
2298348SEric.Yu@Sun.COM static int 	sotpi_ioctl(struct sonode *, int, intptr_t, int, struct cred *,
2308348SEric.Yu@Sun.COM 		    int32_t *);
2318348SEric.Yu@Sun.COM static int 	socktpi_plumbioctl(struct vnode *, int, intptr_t, int,
2328348SEric.Yu@Sun.COM 		    struct cred *, int32_t *);
2338348SEric.Yu@Sun.COM static int 	sotpi_poll(struct sonode *, short, int, short *,
2348348SEric.Yu@Sun.COM 		    struct pollhead **);
2358348SEric.Yu@Sun.COM static int 	sotpi_close(struct sonode *, int, struct cred *);
2368348SEric.Yu@Sun.COM 
2378348SEric.Yu@Sun.COM static int	i_sotpi_info_constructor(sotpi_info_t *);
2388348SEric.Yu@Sun.COM static void 	i_sotpi_info_destructor(sotpi_info_t *);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate sonodeops_t sotpi_sonodeops = {
2418348SEric.Yu@Sun.COM 	sotpi_init,		/* sop_init		*/
2420Sstevel@tonic-gate 	sotpi_accept,		/* sop_accept		*/
2430Sstevel@tonic-gate 	sotpi_bind,		/* sop_bind		*/
2440Sstevel@tonic-gate 	sotpi_listen,		/* sop_listen		*/
2450Sstevel@tonic-gate 	sotpi_connect,		/* sop_connect		*/
2460Sstevel@tonic-gate 	sotpi_recvmsg,		/* sop_recvmsg		*/
2470Sstevel@tonic-gate 	sotpi_sendmsg,		/* sop_sendmsg		*/
2488348SEric.Yu@Sun.COM 	sotpi_sendmblk,		/* sop_sendmblk		*/
2490Sstevel@tonic-gate 	sotpi_getpeername,	/* sop_getpeername	*/
2500Sstevel@tonic-gate 	sotpi_getsockname,	/* sop_getsockname	*/
2510Sstevel@tonic-gate 	sotpi_shutdown,		/* sop_shutdown		*/
2520Sstevel@tonic-gate 	sotpi_getsockopt,	/* sop_getsockopt	*/
2538348SEric.Yu@Sun.COM 	sotpi_setsockopt,	/* sop_setsockopt	*/
2548348SEric.Yu@Sun.COM 	sotpi_ioctl,		/* sop_ioctl		*/
2558348SEric.Yu@Sun.COM 	sotpi_poll,		/* sop_poll		*/
2568348SEric.Yu@Sun.COM 	sotpi_close,		/* sop_close		*/
2570Sstevel@tonic-gate };
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /*
2608348SEric.Yu@Sun.COM  * Return a TPI socket vnode.
2618348SEric.Yu@Sun.COM  *
2628348SEric.Yu@Sun.COM  * Note that sockets assume that the driver will clone (either itself
2638348SEric.Yu@Sun.COM  * or by using the clone driver) i.e. a socket() call will always
2648348SEric.Yu@Sun.COM  * result in a new vnode being created.
2658348SEric.Yu@Sun.COM  */
2668348SEric.Yu@Sun.COM 
2678348SEric.Yu@Sun.COM /*
2680Sstevel@tonic-gate  * Common create code for socket and accept. If tso is set the values
2690Sstevel@tonic-gate  * from that node is used instead of issuing a T_INFO_REQ.
2700Sstevel@tonic-gate  */
2718348SEric.Yu@Sun.COM 
2728348SEric.Yu@Sun.COM /* ARGSUSED */
2738348SEric.Yu@Sun.COM static struct sonode *
sotpi_create(struct sockparams * sp,int family,int type,int protocol,int version,int sflags,int * errorp,cred_t * cr)2748348SEric.Yu@Sun.COM sotpi_create(struct sockparams *sp, int family, int type, int protocol,
2758348SEric.Yu@Sun.COM     int version, int sflags, int *errorp, cred_t *cr)
2760Sstevel@tonic-gate {
2770Sstevel@tonic-gate 	struct sonode	*so;
2788348SEric.Yu@Sun.COM 	kmem_cache_t 	*cp;
2798348SEric.Yu@Sun.COM 	int		sfamily = family;
2808348SEric.Yu@Sun.COM 
2818348SEric.Yu@Sun.COM 	ASSERT(sp->sp_sdev_info.sd_vnode != NULL);
2828348SEric.Yu@Sun.COM 
2838348SEric.Yu@Sun.COM 	if (family == AF_NCA) {
2848348SEric.Yu@Sun.COM 		/*
2858348SEric.Yu@Sun.COM 		 * The request is for an NCA socket so for NL7C use the
2868348SEric.Yu@Sun.COM 		 * INET domain instead and mark NL7C_AF_NCA below.
2878348SEric.Yu@Sun.COM 		 */
2888348SEric.Yu@Sun.COM 		family = AF_INET;
2898348SEric.Yu@Sun.COM 		/*
2908348SEric.Yu@Sun.COM 		 * NL7C is not supported in the non-global zone,
2918348SEric.Yu@Sun.COM 		 * we enforce this restriction here.
2928348SEric.Yu@Sun.COM 		 */
2938348SEric.Yu@Sun.COM 		if (getzoneid() != GLOBAL_ZONEID) {
2948348SEric.Yu@Sun.COM 			*errorp = ENOTSUP;
2958348SEric.Yu@Sun.COM 			return (NULL);
2968348SEric.Yu@Sun.COM 		}
2978348SEric.Yu@Sun.COM 	}
2988348SEric.Yu@Sun.COM 
2998348SEric.Yu@Sun.COM 	/*
3008348SEric.Yu@Sun.COM 	 * to be compatible with old tpi socket implementation ignore
3018348SEric.Yu@Sun.COM 	 * sleep flag (sflags) passed in
3028348SEric.Yu@Sun.COM 	 */
3038348SEric.Yu@Sun.COM 	cp = (family == AF_UNIX) ? socktpi_unix_cache : socktpi_cache;
3048348SEric.Yu@Sun.COM 	so = kmem_cache_alloc(cp, KM_SLEEP);
3058348SEric.Yu@Sun.COM 	if (so == NULL) {
3068348SEric.Yu@Sun.COM 		*errorp = ENOMEM;
3078348SEric.Yu@Sun.COM 		return (NULL);
3088348SEric.Yu@Sun.COM 	}
3098348SEric.Yu@Sun.COM 
3108348SEric.Yu@Sun.COM 	sonode_init(so, sp, family, type, protocol, &sotpi_sonodeops);
3118348SEric.Yu@Sun.COM 	sotpi_info_init(so);
3128348SEric.Yu@Sun.COM 
3138348SEric.Yu@Sun.COM 	if (sfamily == AF_NCA) {
3148348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_nl7c_flags = NL7C_AF_NCA;
3158348SEric.Yu@Sun.COM 	}
3168348SEric.Yu@Sun.COM 
3178348SEric.Yu@Sun.COM 	if (version == SOV_DEFAULT)
3188348SEric.Yu@Sun.COM 		version = so_default_version;
3198348SEric.Yu@Sun.COM 
3208348SEric.Yu@Sun.COM 	so->so_version = (short)version;
3218348SEric.Yu@Sun.COM 	*errorp = 0;
3228348SEric.Yu@Sun.COM 
3238348SEric.Yu@Sun.COM 	return (so);
3248348SEric.Yu@Sun.COM }
3258348SEric.Yu@Sun.COM 
3268348SEric.Yu@Sun.COM static void
sotpi_destroy(struct sonode * so)3278348SEric.Yu@Sun.COM sotpi_destroy(struct sonode *so)
3288348SEric.Yu@Sun.COM {
3298348SEric.Yu@Sun.COM 	kmem_cache_t *cp;
3308348SEric.Yu@Sun.COM 	struct sockparams *origsp;
3318348SEric.Yu@Sun.COM 
3328348SEric.Yu@Sun.COM 	/*
3338348SEric.Yu@Sun.COM 	 * If there is a new dealloc function (ie. smod_destroy_func),
3348348SEric.Yu@Sun.COM 	 * then it should check the correctness of the ops.
3358348SEric.Yu@Sun.COM 	 */
3368348SEric.Yu@Sun.COM 
3378348SEric.Yu@Sun.COM 	ASSERT(so->so_ops == &sotpi_sonodeops);
3388348SEric.Yu@Sun.COM 
3398348SEric.Yu@Sun.COM 	origsp = SOTOTPI(so)->sti_orig_sp;
3408348SEric.Yu@Sun.COM 
3418348SEric.Yu@Sun.COM 	sotpi_info_fini(so);
3428348SEric.Yu@Sun.COM 
3438348SEric.Yu@Sun.COM 	if (so->so_state & SS_FALLBACK_COMP) {
3448348SEric.Yu@Sun.COM 		/*
3458348SEric.Yu@Sun.COM 		 * A fallback happend, which means that a sotpi_info_t struct
3468348SEric.Yu@Sun.COM 		 * was allocated (as opposed to being allocated from the TPI
3478348SEric.Yu@Sun.COM 		 * sonode cache. Therefore we explicitly free the struct
3488348SEric.Yu@Sun.COM 		 * here.
3498348SEric.Yu@Sun.COM 		 */
3508348SEric.Yu@Sun.COM 		sotpi_info_destroy(so);
3518348SEric.Yu@Sun.COM 		ASSERT(origsp != NULL);
3528348SEric.Yu@Sun.COM 
3538348SEric.Yu@Sun.COM 		origsp->sp_smod_info->smod_sock_destroy_func(so);
3548348SEric.Yu@Sun.COM 		SOCKPARAMS_DEC_REF(origsp);
3558348SEric.Yu@Sun.COM 	} else {
3568348SEric.Yu@Sun.COM 		sonode_fini(so);
3578348SEric.Yu@Sun.COM 		cp = (so->so_family == AF_UNIX) ? socktpi_unix_cache :
3588348SEric.Yu@Sun.COM 		    socktpi_cache;
3598348SEric.Yu@Sun.COM 		kmem_cache_free(cp, so);
3608348SEric.Yu@Sun.COM 	}
3618348SEric.Yu@Sun.COM }
3628348SEric.Yu@Sun.COM 
3638348SEric.Yu@Sun.COM /* ARGSUSED1 */
3648348SEric.Yu@Sun.COM int
sotpi_init(struct sonode * so,struct sonode * tso,struct cred * cr,int flags)3658348SEric.Yu@Sun.COM sotpi_init(struct sonode *so, struct sonode *tso, struct cred *cr, int flags)
3668348SEric.Yu@Sun.COM {
3678348SEric.Yu@Sun.COM 	major_t maj;
3688348SEric.Yu@Sun.COM 	dev_t newdev;
3698348SEric.Yu@Sun.COM 	struct vnode *vp;
3708348SEric.Yu@Sun.COM 	int error = 0;
3718348SEric.Yu@Sun.COM 	struct stdata *stp;
3728348SEric.Yu@Sun.COM 
3738348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
3748348SEric.Yu@Sun.COM 
3758348SEric.Yu@Sun.COM 	dprint(1, ("sotpi_init()\n"));
3768348SEric.Yu@Sun.COM 
3778348SEric.Yu@Sun.COM 	/*
3788348SEric.Yu@Sun.COM 	 * over write the sleep flag passed in but that is ok
3798348SEric.Yu@Sun.COM 	 * as tpi socket does not honor sleep flag.
3808348SEric.Yu@Sun.COM 	 */
3818348SEric.Yu@Sun.COM 	flags |= FREAD|FWRITE;
3828348SEric.Yu@Sun.COM 
3838348SEric.Yu@Sun.COM 	/*
3848348SEric.Yu@Sun.COM 	 * Record in so_flag that it is a clone.
3858348SEric.Yu@Sun.COM 	 */
3868348SEric.Yu@Sun.COM 	if (getmajor(sti->sti_dev) == clone_major)
3878348SEric.Yu@Sun.COM 		so->so_flag |= SOCLONE;
3888348SEric.Yu@Sun.COM 
3898348SEric.Yu@Sun.COM 	if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM) &&
3908348SEric.Yu@Sun.COM 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
3918348SEric.Yu@Sun.COM 	    (so->so_protocol == IPPROTO_TCP || so->so_protocol == IPPROTO_UDP ||
3928348SEric.Yu@Sun.COM 	    so->so_protocol == IPPROTO_IP)) {
393741Smasputra 		/* Tell tcp or udp that it's talking to sockets */
394741Smasputra 		flags |= SO_SOCKSTR;
395741Smasputra 
396741Smasputra 		/*
397741Smasputra 		 * Here we indicate to socktpi_open() our attempt to
398741Smasputra 		 * make direct calls between sockfs and transport.
399741Smasputra 		 * The final decision is left to socktpi_open().
400741Smasputra 		 */
4018348SEric.Yu@Sun.COM 		sti->sti_direct = 1;
402741Smasputra 
403741Smasputra 		ASSERT(so->so_type != SOCK_DGRAM || tso == NULL);
404741Smasputra 		if (so->so_type == SOCK_STREAM && tso != NULL) {
4058348SEric.Yu@Sun.COM 			if (SOTOTPI(tso)->sti_direct) {
406741Smasputra 				/*
4078348SEric.Yu@Sun.COM 				 * Inherit sti_direct from listener and pass
408741Smasputra 				 * SO_ACCEPTOR open flag to tcp, indicating
409741Smasputra 				 * that this is an accept fast-path instance.
410741Smasputra 				 */
411741Smasputra 				flags |= SO_ACCEPTOR;
412741Smasputra 			} else {
413741Smasputra 				/*
4148348SEric.Yu@Sun.COM 				 * sti_direct is not set on listener, meaning
415741Smasputra 				 * that the listener has been converted from
416741Smasputra 				 * a socket to a stream.  Ensure that the
417741Smasputra 				 * acceptor inherits these settings.
418741Smasputra 				 */
4198348SEric.Yu@Sun.COM 				sti->sti_direct = 0;
420741Smasputra 				flags &= ~SO_SOCKSTR;
421741Smasputra 			}
4220Sstevel@tonic-gate 		}
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	/*
4260Sstevel@tonic-gate 	 * Tell local transport that it is talking to sockets.
4270Sstevel@tonic-gate 	 */
4280Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
4290Sstevel@tonic-gate 		flags |= SO_SOCKSTR;
4300Sstevel@tonic-gate 	}
4310Sstevel@tonic-gate 
4328348SEric.Yu@Sun.COM 	vp = SOTOV(so);
4338348SEric.Yu@Sun.COM 	newdev = vp->v_rdev;
4348348SEric.Yu@Sun.COM 	maj = getmajor(newdev);
4358348SEric.Yu@Sun.COM 	ASSERT(STREAMSTAB(maj));
4368348SEric.Yu@Sun.COM 
4378348SEric.Yu@Sun.COM 	error = stropen(vp, &newdev, flags, cr);
4388348SEric.Yu@Sun.COM 
4398348SEric.Yu@Sun.COM 	stp = vp->v_stream;
4408348SEric.Yu@Sun.COM 	if (error == 0) {
4418348SEric.Yu@Sun.COM 		if (so->so_flag & SOCLONE)
4428348SEric.Yu@Sun.COM 			ASSERT(newdev != vp->v_rdev);
4438348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
4448348SEric.Yu@Sun.COM 		sti->sti_dev = newdev;
4458348SEric.Yu@Sun.COM 		vp->v_rdev = newdev;
4468348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
4478348SEric.Yu@Sun.COM 
4488348SEric.Yu@Sun.COM 		if (stp->sd_flag & STRISTTY) {
4498348SEric.Yu@Sun.COM 			/*
4508348SEric.Yu@Sun.COM 			 * this is a post SVR4 tty driver - a socket can not
4518348SEric.Yu@Sun.COM 			 * be a controlling terminal. Fail the open.
4528348SEric.Yu@Sun.COM 			 */
4538348SEric.Yu@Sun.COM 			(void) sotpi_close(so, flags, cr);
4548348SEric.Yu@Sun.COM 			return (ENOTTY);	/* XXX */
4558348SEric.Yu@Sun.COM 		}
4568348SEric.Yu@Sun.COM 
4578348SEric.Yu@Sun.COM 		ASSERT(stp->sd_wrq != NULL);
4588348SEric.Yu@Sun.COM 		sti->sti_provinfo = tpi_findprov(stp->sd_wrq);
4598348SEric.Yu@Sun.COM 
4608348SEric.Yu@Sun.COM 		/*
4618348SEric.Yu@Sun.COM 		 * If caller is interested in doing direct function call
4628348SEric.Yu@Sun.COM 		 * interface to/from transport module, probe the module
4638348SEric.Yu@Sun.COM 		 * directly beneath the streamhead to see if it qualifies.
4648348SEric.Yu@Sun.COM 		 *
4658348SEric.Yu@Sun.COM 		 * We turn off the direct interface when qualifications fail.
4668348SEric.Yu@Sun.COM 		 * In the acceptor case, we simply turn off the sti_direct
4678348SEric.Yu@Sun.COM 		 * flag on the socket. We do the fallback after the accept
4688348SEric.Yu@Sun.COM 		 * has completed, before the new socket is returned to the
4698348SEric.Yu@Sun.COM 		 * application.
4708348SEric.Yu@Sun.COM 		 */
4718348SEric.Yu@Sun.COM 		if (sti->sti_direct) {
4728348SEric.Yu@Sun.COM 			queue_t *tq = stp->sd_wrq->q_next;
4738348SEric.Yu@Sun.COM 
4748348SEric.Yu@Sun.COM 			/*
4758348SEric.Yu@Sun.COM 			 * sti_direct is currently supported and tested
4768348SEric.Yu@Sun.COM 			 * only for tcp/udp; this is the main reason to
4778348SEric.Yu@Sun.COM 			 * have the following assertions.
4788348SEric.Yu@Sun.COM 			 */
4798348SEric.Yu@Sun.COM 			ASSERT(so->so_family == AF_INET ||
4808348SEric.Yu@Sun.COM 			    so->so_family == AF_INET6);
4818348SEric.Yu@Sun.COM 			ASSERT(so->so_protocol == IPPROTO_UDP ||
4828348SEric.Yu@Sun.COM 			    so->so_protocol == IPPROTO_TCP ||
4838348SEric.Yu@Sun.COM 			    so->so_protocol == IPPROTO_IP);
4848348SEric.Yu@Sun.COM 			ASSERT(so->so_type == SOCK_DGRAM ||
4858348SEric.Yu@Sun.COM 			    so->so_type == SOCK_STREAM);
4868348SEric.Yu@Sun.COM 
4878348SEric.Yu@Sun.COM 			/*
4888348SEric.Yu@Sun.COM 			 * Abort direct call interface if the module directly
4898348SEric.Yu@Sun.COM 			 * underneath the stream head is not defined with the
4908348SEric.Yu@Sun.COM 			 * _D_DIRECT flag.  This could happen in the tcp or
4918348SEric.Yu@Sun.COM 			 * udp case, when some other module is autopushed
4928348SEric.Yu@Sun.COM 			 * above it, or for some reasons the expected module
4938348SEric.Yu@Sun.COM 			 * isn't purely D_MP (which is the main requirement).
4948348SEric.Yu@Sun.COM 			 */
4958348SEric.Yu@Sun.COM 			if (!socktpi_direct || !(tq->q_flag & _QDIRECT) ||
4968348SEric.Yu@Sun.COM 			    !(_OTHERQ(tq)->q_flag & _QDIRECT)) {
4978348SEric.Yu@Sun.COM 				int rval;
4988348SEric.Yu@Sun.COM 
4998348SEric.Yu@Sun.COM 				/* Continue on without direct calls */
5008348SEric.Yu@Sun.COM 				sti->sti_direct = 0;
5018348SEric.Yu@Sun.COM 
5028348SEric.Yu@Sun.COM 				/*
5038348SEric.Yu@Sun.COM 				 * Cannot issue ioctl on fallback socket since
5048348SEric.Yu@Sun.COM 				 * there is no conn associated with the queue.
5058348SEric.Yu@Sun.COM 				 * The fallback downcall will notify the proto
5068348SEric.Yu@Sun.COM 				 * of the change.
5078348SEric.Yu@Sun.COM 				 */
5088348SEric.Yu@Sun.COM 				if (!(flags & SO_ACCEPTOR) &&
5098348SEric.Yu@Sun.COM 				    !(flags & SO_FALLBACK)) {
5108348SEric.Yu@Sun.COM 					if ((error = strioctl(vp,
5118348SEric.Yu@Sun.COM 					    _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
5128348SEric.Yu@Sun.COM 					    cr, &rval)) != 0) {
5138348SEric.Yu@Sun.COM 						(void) sotpi_close(so, flags,
5148348SEric.Yu@Sun.COM 						    cr);
5158348SEric.Yu@Sun.COM 						return (error);
5168348SEric.Yu@Sun.COM 					}
5178348SEric.Yu@Sun.COM 				}
5188348SEric.Yu@Sun.COM 			}
5198348SEric.Yu@Sun.COM 		}
5208348SEric.Yu@Sun.COM 
5218348SEric.Yu@Sun.COM 		if (flags & SO_FALLBACK) {
5228348SEric.Yu@Sun.COM 			/*
5238348SEric.Yu@Sun.COM 			 * The stream created does not have a conn.
5248348SEric.Yu@Sun.COM 			 * do stream set up after conn has been assigned
5258348SEric.Yu@Sun.COM 			 */
5268348SEric.Yu@Sun.COM 			return (error);
5278348SEric.Yu@Sun.COM 		}
5288348SEric.Yu@Sun.COM 		if (error = so_strinit(so, tso)) {
5298348SEric.Yu@Sun.COM 			(void) sotpi_close(so, flags, cr);
5308348SEric.Yu@Sun.COM 			return (error);
5318348SEric.Yu@Sun.COM 		}
5328348SEric.Yu@Sun.COM 
5338348SEric.Yu@Sun.COM 		/* Wildcard */
5348348SEric.Yu@Sun.COM 		if (so->so_protocol != so->so_sockparams->sp_protocol) {
5358348SEric.Yu@Sun.COM 			int protocol = so->so_protocol;
5368348SEric.Yu@Sun.COM 			/*
5378348SEric.Yu@Sun.COM 			 * Issue SO_PROTOTYPE setsockopt.
5388348SEric.Yu@Sun.COM 			 */
5398348SEric.Yu@Sun.COM 			error = sotpi_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
5408348SEric.Yu@Sun.COM 			    &protocol, (t_uscalar_t)sizeof (protocol), cr);
5418348SEric.Yu@Sun.COM 			if (error != 0) {
5428348SEric.Yu@Sun.COM 				(void) sotpi_close(so, flags, cr);
5438348SEric.Yu@Sun.COM 				/*
5448348SEric.Yu@Sun.COM 				 * Setsockopt often fails with ENOPROTOOPT but
5458348SEric.Yu@Sun.COM 				 * socket() should fail with
5468348SEric.Yu@Sun.COM 				 * EPROTONOSUPPORT/EPROTOTYPE.
5478348SEric.Yu@Sun.COM 				 */
5488348SEric.Yu@Sun.COM 				return (EPROTONOSUPPORT);
5498348SEric.Yu@Sun.COM 			}
5508348SEric.Yu@Sun.COM 		}
5518348SEric.Yu@Sun.COM 
5528348SEric.Yu@Sun.COM 	} else {
5538348SEric.Yu@Sun.COM 		/*
5548348SEric.Yu@Sun.COM 		 * While the same socket can not be reopened (unlike specfs)
5558348SEric.Yu@Sun.COM 		 * the stream head sets STREOPENFAIL when the autopush fails.
5568348SEric.Yu@Sun.COM 		 */
5578348SEric.Yu@Sun.COM 		if ((stp != NULL) &&
5588348SEric.Yu@Sun.COM 		    (stp->sd_flag & STREOPENFAIL)) {
5598348SEric.Yu@Sun.COM 			/*
5608348SEric.Yu@Sun.COM 			 * Open failed part way through.
5618348SEric.Yu@Sun.COM 			 */
5628348SEric.Yu@Sun.COM 			mutex_enter(&stp->sd_lock);
5638348SEric.Yu@Sun.COM 			stp->sd_flag &= ~STREOPENFAIL;
5648348SEric.Yu@Sun.COM 			mutex_exit(&stp->sd_lock);
5658348SEric.Yu@Sun.COM 			(void) sotpi_close(so, flags, cr);
5668348SEric.Yu@Sun.COM 			return (error);
5678348SEric.Yu@Sun.COM 			/*NOTREACHED*/
5688348SEric.Yu@Sun.COM 		}
5698348SEric.Yu@Sun.COM 		ASSERT(stp == NULL);
5708348SEric.Yu@Sun.COM 	}
5718348SEric.Yu@Sun.COM 	TRACE_4(TR_FAC_SOCKFS, TR_SOCKFS_OPEN,
5728348SEric.Yu@Sun.COM 	    "sockfs open:maj %d vp %p so %p error %d",
5738348SEric.Yu@Sun.COM 	    maj, vp, so, error);
5748348SEric.Yu@Sun.COM 	return (error);
5750Sstevel@tonic-gate }
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate /*
5780Sstevel@tonic-gate  * Bind the socket to an unspecified address in sockfs only.
5790Sstevel@tonic-gate  * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't
5800Sstevel@tonic-gate  * required in all cases.
5810Sstevel@tonic-gate  */
5820Sstevel@tonic-gate static void
so_automatic_bind(struct sonode * so)5830Sstevel@tonic-gate so_automatic_bind(struct sonode *so)
5840Sstevel@tonic-gate {
5858348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
5860Sstevel@tonic-gate 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
5890Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_ISBOUND));
5908348SEric.Yu@Sun.COM 	ASSERT(sti->sti_unbind_mp);
5918348SEric.Yu@Sun.COM 
5928348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
5938348SEric.Yu@Sun.COM 	bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
5948348SEric.Yu@Sun.COM 	sti->sti_laddr_sa->sa_family = so->so_family;
5950Sstevel@tonic-gate 	so->so_state |= SS_ISBOUND;
5960Sstevel@tonic-gate }
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate 
5990Sstevel@tonic-gate /*
6000Sstevel@tonic-gate  * bind the socket.
6010Sstevel@tonic-gate  *
6020Sstevel@tonic-gate  * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2
6030Sstevel@tonic-gate  * are passed in we allow rebinding. Note that for backwards compatibility
6040Sstevel@tonic-gate  * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind.
6050Sstevel@tonic-gate  * Thus the rebinding code is currently not executed.
6060Sstevel@tonic-gate  *
6070Sstevel@tonic-gate  * The constraints for rebinding are:
6080Sstevel@tonic-gate  * - it is a SOCK_DGRAM, or
6090Sstevel@tonic-gate  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6100Sstevel@tonic-gate  *   and no listen() has been done.
6110Sstevel@tonic-gate  * This rebinding code was added based on some language in the XNET book
6120Sstevel@tonic-gate  * about not returning EINVAL it the protocol allows rebinding. However,
6130Sstevel@tonic-gate  * this language is not present in the Posix socket draft. Thus maybe the
6140Sstevel@tonic-gate  * rebinding logic should be deleted from the source.
6150Sstevel@tonic-gate  *
6160Sstevel@tonic-gate  * A null "name" can be used to unbind the socket if:
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  */
6218348SEric.Yu@Sun.COM /* ARGSUSED */
6220Sstevel@tonic-gate static int
sotpi_bindlisten(struct sonode * so,struct sockaddr * name,socklen_t namelen,int backlog,int flags,struct cred * cr)6230Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name,
6248348SEric.Yu@Sun.COM     socklen_t namelen, int backlog, int flags, struct cred *cr)
6250Sstevel@tonic-gate {
6260Sstevel@tonic-gate 	struct T_bind_req	bind_req;
6270Sstevel@tonic-gate 	struct T_bind_ack	*bind_ack;
6280Sstevel@tonic-gate 	int			error = 0;
6290Sstevel@tonic-gate 	mblk_t			*mp;
6300Sstevel@tonic-gate 	void			*addr;
6310Sstevel@tonic-gate 	t_uscalar_t		addrlen;
6320Sstevel@tonic-gate 	int			unbind_on_err = 1;
6330Sstevel@tonic-gate 	boolean_t		clear_acceptconn_on_err = B_FALSE;
6340Sstevel@tonic-gate 	boolean_t		restore_backlog_on_err = B_FALSE;
6350Sstevel@tonic-gate 	int			save_so_backlog;
6360Sstevel@tonic-gate 	t_scalar_t		PRIM_type = O_T_BIND_REQ;
6370Sstevel@tonic-gate 	boolean_t		tcp_udp_xport;
6380Sstevel@tonic-gate 	void			*nl7c = NULL;
6398348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
6400Sstevel@tonic-gate 
6410Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n",
6427240Srh87107 	    (void *)so, (void *)name, namelen, backlog, flags,
6435240Snordmark 	    pr_state(so->so_state, so->so_mode)));
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM;
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	if (!(flags & _SOBIND_LOCK_HELD)) {
6480Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
6490Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
6500Sstevel@tonic-gate 	} else {
6510Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
6520Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
6530Sstevel@tonic-gate 	}
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * Make sure that there is a preallocated unbind_req message
6570Sstevel@tonic-gate 	 * before binding. This message allocated when the socket is
6580Sstevel@tonic-gate 	 * created  but it might be have been consumed.
6590Sstevel@tonic-gate 	 */
6608348SEric.Yu@Sun.COM 	if (sti->sti_unbind_mp == NULL) {
6610Sstevel@tonic-gate 		dprintso(so, 1, ("sobind: allocating unbind_req\n"));
6620Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
6638348SEric.Yu@Sun.COM 		sti->sti_unbind_mp =
6648778SErik.Nordmark@Sun.COM 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP,
6658778SErik.Nordmark@Sun.COM 		    cr);
6660Sstevel@tonic-gate 	}
6670Sstevel@tonic-gate 
6680Sstevel@tonic-gate 	if (flags & _SOBIND_REBIND) {
6690Sstevel@tonic-gate 		/*
6700Sstevel@tonic-gate 		 * Called from solisten after doing an sotpi_unbind() or
6710Sstevel@tonic-gate 		 * potentially without the unbind (latter for AF_INET{,6}).
6720Sstevel@tonic-gate 		 */
6730Sstevel@tonic-gate 		ASSERT(name == NULL && namelen == 0);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
6768348SEric.Yu@Sun.COM 			ASSERT(sti->sti_ux_bound_vp);
6778348SEric.Yu@Sun.COM 			addr = &sti->sti_ux_laddr;
6788348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
6795240Snordmark 			dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, "
6805240Snordmark 			    "addr 0x%p, vp %p\n",
6810Sstevel@tonic-gate 			    addrlen,
6827240Srh87107 			    (void *)((struct so_ux_addr *)addr)->soua_vp,
6838348SEric.Yu@Sun.COM 			    (void *)sti->sti_ux_bound_vp));
6840Sstevel@tonic-gate 		} else {
6858348SEric.Yu@Sun.COM 			addr = sti->sti_laddr_sa;
6868348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sti->sti_laddr_len;
6870Sstevel@tonic-gate 		}
6880Sstevel@tonic-gate 	} else if (flags & _SOBIND_UNSPEC) {
6890Sstevel@tonic-gate 		ASSERT(name == NULL && namelen == 0);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 		/*
6920Sstevel@tonic-gate 		 * The caller checked SS_ISBOUND but not necessarily
6930Sstevel@tonic-gate 		 * under so_lock
6940Sstevel@tonic-gate 		 */
6950Sstevel@tonic-gate 		if (so->so_state & SS_ISBOUND) {
6960Sstevel@tonic-gate 			/* No error */
6970Sstevel@tonic-gate 			goto done;
6980Sstevel@tonic-gate 		}
6990Sstevel@tonic-gate 
7000Sstevel@tonic-gate 		/* Set an initial local address */
7010Sstevel@tonic-gate 		switch (so->so_family) {
7020Sstevel@tonic-gate 		case AF_UNIX:
7030Sstevel@tonic-gate 			/*
7040Sstevel@tonic-gate 			 * Use an address with same size as struct sockaddr
7050Sstevel@tonic-gate 			 * just like BSD.
7060Sstevel@tonic-gate 			 */
7078348SEric.Yu@Sun.COM 			sti->sti_laddr_len =
7085240Snordmark 			    (socklen_t)sizeof (struct sockaddr);
7098348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7108348SEric.Yu@Sun.COM 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7118348SEric.Yu@Sun.COM 			sti->sti_laddr_sa->sa_family = so->so_family;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 			/*
7140Sstevel@tonic-gate 			 * Pass down an address with the implicit bind
7150Sstevel@tonic-gate 			 * magic number and the rest all zeros.
7160Sstevel@tonic-gate 			 * The transport will return a unique address.
7170Sstevel@tonic-gate 			 */
7188348SEric.Yu@Sun.COM 			sti->sti_ux_laddr.soua_vp = NULL;
7198348SEric.Yu@Sun.COM 			sti->sti_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT;
7208348SEric.Yu@Sun.COM 			addr = &sti->sti_ux_laddr;
7218348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
7220Sstevel@tonic-gate 			break;
7230Sstevel@tonic-gate 
7240Sstevel@tonic-gate 		case AF_INET:
7250Sstevel@tonic-gate 		case AF_INET6:
7260Sstevel@tonic-gate 			/*
7270Sstevel@tonic-gate 			 * An unspecified bind in TPI has a NULL address.
7280Sstevel@tonic-gate 			 * Set the address in sockfs to have the sa_family.
7290Sstevel@tonic-gate 			 */
7308348SEric.Yu@Sun.COM 			sti->sti_laddr_len = (so->so_family == AF_INET) ?
7310Sstevel@tonic-gate 			    (socklen_t)sizeof (sin_t) :
7320Sstevel@tonic-gate 			    (socklen_t)sizeof (sin6_t);
7338348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7348348SEric.Yu@Sun.COM 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7358348SEric.Yu@Sun.COM 			sti->sti_laddr_sa->sa_family = so->so_family;
7360Sstevel@tonic-gate 			addr = NULL;
7370Sstevel@tonic-gate 			addrlen = 0;
7380Sstevel@tonic-gate 			break;
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 		default:
7410Sstevel@tonic-gate 			/*
7420Sstevel@tonic-gate 			 * An unspecified bind in TPI has a NULL address.
7430Sstevel@tonic-gate 			 * Set the address in sockfs to be zero length.
7440Sstevel@tonic-gate 			 *
7450Sstevel@tonic-gate 			 * Can not assume there is a sa_family for all
7460Sstevel@tonic-gate 			 * protocol families. For example, AF_X25 does not
7470Sstevel@tonic-gate 			 * have a family field.
7480Sstevel@tonic-gate 			 */
7498348SEric.Yu@Sun.COM 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7508348SEric.Yu@Sun.COM 			sti->sti_laddr_len = 0;	/* XXX correct? */
7510Sstevel@tonic-gate 			addr = NULL;
7520Sstevel@tonic-gate 			addrlen = 0;
7530Sstevel@tonic-gate 			break;
7540Sstevel@tonic-gate 		}
7550Sstevel@tonic-gate 
7560Sstevel@tonic-gate 	} else {
7570Sstevel@tonic-gate 		if (so->so_state & SS_ISBOUND) {
7580Sstevel@tonic-gate 			/*
7590Sstevel@tonic-gate 			 * If it is ok to rebind the socket, first unbind
7600Sstevel@tonic-gate 			 * with the transport. A rebind to the NULL address
7610Sstevel@tonic-gate 			 * is interpreted as an unbind.
7620Sstevel@tonic-gate 			 * Note that a bind to NULL in BSD does unbind the
7630Sstevel@tonic-gate 			 * socket but it fails with EINVAL.
7640Sstevel@tonic-gate 			 * Note that regular sockets set SOV_SOCKBSD i.e.
7650Sstevel@tonic-gate 			 * _SOBIND_SOCKBSD gets set here hence no type of
7660Sstevel@tonic-gate 			 * socket does currently allow rebinding.
7670Sstevel@tonic-gate 			 *
7680Sstevel@tonic-gate 			 * If the name is NULL just do an unbind.
7690Sstevel@tonic-gate 			 */
7700Sstevel@tonic-gate 			if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) &&
7710Sstevel@tonic-gate 			    name != NULL) {
7720Sstevel@tonic-gate 				error = EINVAL;
7730Sstevel@tonic-gate 				unbind_on_err = 0;
7740Sstevel@tonic-gate 				eprintsoline(so, error);
7750Sstevel@tonic-gate 				goto done;
7760Sstevel@tonic-gate 			}
7770Sstevel@tonic-gate 			if ((so->so_mode & SM_CONNREQUIRED) &&
7780Sstevel@tonic-gate 			    (so->so_state & SS_CANTREBIND)) {
7790Sstevel@tonic-gate 				error = EINVAL;
7800Sstevel@tonic-gate 				unbind_on_err = 0;
7810Sstevel@tonic-gate 				eprintsoline(so, error);
7820Sstevel@tonic-gate 				goto done;
7830Sstevel@tonic-gate 			}
7840Sstevel@tonic-gate 			error = sotpi_unbind(so, 0);
7850Sstevel@tonic-gate 			if (error) {
7860Sstevel@tonic-gate 				eprintsoline(so, error);
7870Sstevel@tonic-gate 				goto done;
7880Sstevel@tonic-gate 			}
7890Sstevel@tonic-gate 			ASSERT(!(so->so_state & SS_ISBOUND));
7900Sstevel@tonic-gate 			if (name == NULL) {
7910Sstevel@tonic-gate 				so->so_state &=
7925240Snordmark 				    ~(SS_ISCONNECTED|SS_ISCONNECTING);
7930Sstevel@tonic-gate 				goto done;
7940Sstevel@tonic-gate 			}
7950Sstevel@tonic-gate 		}
7968348SEric.Yu@Sun.COM 
7970Sstevel@tonic-gate 		/* X/Open requires this check */
7980Sstevel@tonic-gate 		if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
7990Sstevel@tonic-gate 			if (xnet_check_print) {
8000Sstevel@tonic-gate 				printf("sockfs: X/Open bind state check "
8010Sstevel@tonic-gate 				    "caused EINVAL\n");
8020Sstevel@tonic-gate 			}
8030Sstevel@tonic-gate 			error = EINVAL;
8040Sstevel@tonic-gate 			goto done;
8050Sstevel@tonic-gate 		}
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 		switch (so->so_family) {
8080Sstevel@tonic-gate 		case AF_UNIX:
8090Sstevel@tonic-gate 			/*
8100Sstevel@tonic-gate 			 * All AF_UNIX addresses are nul terminated
8110Sstevel@tonic-gate 			 * when copied (copyin_name) in so the minimum
8120Sstevel@tonic-gate 			 * length is 3 bytes.
8130Sstevel@tonic-gate 			 */
8140Sstevel@tonic-gate 			if (name == NULL ||
8150Sstevel@tonic-gate 			    (ssize_t)namelen <= sizeof (short) + 1) {
8160Sstevel@tonic-gate 				error = EISDIR;
8170Sstevel@tonic-gate 				eprintsoline(so, error);
8180Sstevel@tonic-gate 				goto done;
8190Sstevel@tonic-gate 			}
8200Sstevel@tonic-gate 			/*
8210Sstevel@tonic-gate 			 * Verify so_family matches the bound family.
8220Sstevel@tonic-gate 			 * BSD does not check this for AF_UNIX resulting
8230Sstevel@tonic-gate 			 * in funny mknods.
8240Sstevel@tonic-gate 			 */
8250Sstevel@tonic-gate 			if (name->sa_family != so->so_family) {
8260Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8270Sstevel@tonic-gate 				goto done;
8280Sstevel@tonic-gate 			}
8290Sstevel@tonic-gate 			break;
8300Sstevel@tonic-gate 		case AF_INET:
8310Sstevel@tonic-gate 			if (name == NULL) {
8320Sstevel@tonic-gate 				error = EINVAL;
8330Sstevel@tonic-gate 				eprintsoline(so, error);
8340Sstevel@tonic-gate 				goto done;
8350Sstevel@tonic-gate 			}
8360Sstevel@tonic-gate 			if ((size_t)namelen != sizeof (sin_t)) {
8370Sstevel@tonic-gate 				error = name->sa_family != so->so_family ?
8380Sstevel@tonic-gate 				    EAFNOSUPPORT : EINVAL;
8390Sstevel@tonic-gate 				eprintsoline(so, error);
8400Sstevel@tonic-gate 				goto done;
8410Sstevel@tonic-gate 			}
8420Sstevel@tonic-gate 			if ((flags & _SOBIND_XPG4_2) &&
8430Sstevel@tonic-gate 			    (name->sa_family != so->so_family)) {
8440Sstevel@tonic-gate 				/*
8450Sstevel@tonic-gate 				 * This check has to be made for X/Open
8460Sstevel@tonic-gate 				 * sockets however application failures have
8470Sstevel@tonic-gate 				 * been observed when it is applied to
8480Sstevel@tonic-gate 				 * all sockets.
8490Sstevel@tonic-gate 				 */
8500Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8510Sstevel@tonic-gate 				eprintsoline(so, error);
8520Sstevel@tonic-gate 				goto done;
8530Sstevel@tonic-gate 			}
8540Sstevel@tonic-gate 			/*
8550Sstevel@tonic-gate 			 * Force a zero sa_family to match so_family.
8560Sstevel@tonic-gate 			 *
8570Sstevel@tonic-gate 			 * Some programs like inetd(1M) don't set the
8580Sstevel@tonic-gate 			 * family field. Other programs leave
8590Sstevel@tonic-gate 			 * sin_family set to garbage - SunOS 4.X does
8600Sstevel@tonic-gate 			 * not check the family field on a bind.
8610Sstevel@tonic-gate 			 * We use the family field that
8620Sstevel@tonic-gate 			 * was passed in to the socket() call.
8630Sstevel@tonic-gate 			 */
8640Sstevel@tonic-gate 			name->sa_family = so->so_family;
8650Sstevel@tonic-gate 			break;
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 		case AF_INET6: {
8680Sstevel@tonic-gate #ifdef DEBUG
8690Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)name;
8700Sstevel@tonic-gate #endif /* DEBUG */
8710Sstevel@tonic-gate 
8720Sstevel@tonic-gate 			if (name == NULL) {
8730Sstevel@tonic-gate 				error = EINVAL;
8740Sstevel@tonic-gate 				eprintsoline(so, error);
8750Sstevel@tonic-gate 				goto done;
8760Sstevel@tonic-gate 			}
8770Sstevel@tonic-gate 			if ((size_t)namelen != sizeof (sin6_t)) {
8780Sstevel@tonic-gate 				error = name->sa_family != so->so_family ?
8790Sstevel@tonic-gate 				    EAFNOSUPPORT : EINVAL;
8800Sstevel@tonic-gate 				eprintsoline(so, error);
8810Sstevel@tonic-gate 				goto done;
8820Sstevel@tonic-gate 			}
8830Sstevel@tonic-gate 			if (name->sa_family != so->so_family) {
8840Sstevel@tonic-gate 				/*
8850Sstevel@tonic-gate 				 * With IPv6 we require the family to match
8860Sstevel@tonic-gate 				 * unlike in IPv4.
8870Sstevel@tonic-gate 				 */
8880Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8890Sstevel@tonic-gate 				eprintsoline(so, error);
8900Sstevel@tonic-gate 				goto done;
8910Sstevel@tonic-gate 			}
8920Sstevel@tonic-gate #ifdef DEBUG
8930Sstevel@tonic-gate 			/*
8940Sstevel@tonic-gate 			 * Verify that apps don't forget to clear
8950Sstevel@tonic-gate 			 * sin6_scope_id etc
8960Sstevel@tonic-gate 			 */
8970Sstevel@tonic-gate 			if (sin6->sin6_scope_id != 0 &&
8980Sstevel@tonic-gate 			    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
8991548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
9000Sstevel@tonic-gate 				    "bind with uninitialized sin6_scope_id "
9010Sstevel@tonic-gate 				    "(%d) on socket. Pid = %d\n",
9020Sstevel@tonic-gate 				    (int)sin6->sin6_scope_id,
9030Sstevel@tonic-gate 				    (int)curproc->p_pid);
9040Sstevel@tonic-gate 			}
9050Sstevel@tonic-gate 			if (sin6->__sin6_src_id != 0) {
9061548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
9070Sstevel@tonic-gate 				    "bind with uninitialized __sin6_src_id "
9080Sstevel@tonic-gate 				    "(%d) on socket. Pid = %d\n",
9090Sstevel@tonic-gate 				    (int)sin6->__sin6_src_id,
9100Sstevel@tonic-gate 				    (int)curproc->p_pid);
9110Sstevel@tonic-gate 			}
9120Sstevel@tonic-gate #endif /* DEBUG */
9130Sstevel@tonic-gate 			break;
9140Sstevel@tonic-gate 		}
9150Sstevel@tonic-gate 		default:
9160Sstevel@tonic-gate 			/*
9170Sstevel@tonic-gate 			 * Don't do any length or sa_family check to allow
9180Sstevel@tonic-gate 			 * non-sockaddr style addresses.
9190Sstevel@tonic-gate 			 */
9200Sstevel@tonic-gate 			if (name == NULL) {
9210Sstevel@tonic-gate 				error = EINVAL;
9220Sstevel@tonic-gate 				eprintsoline(so, error);
9230Sstevel@tonic-gate 				goto done;
9240Sstevel@tonic-gate 			}
9250Sstevel@tonic-gate 			break;
9260Sstevel@tonic-gate 		}
9270Sstevel@tonic-gate 
9288348SEric.Yu@Sun.COM 		if (namelen > (t_uscalar_t)sti->sti_laddr_maxlen) {
9290Sstevel@tonic-gate 			error = ENAMETOOLONG;
9300Sstevel@tonic-gate 			eprintsoline(so, error);
9310Sstevel@tonic-gate 			goto done;
9320Sstevel@tonic-gate 		}
9330Sstevel@tonic-gate 		/*
9340Sstevel@tonic-gate 		 * Save local address.
9350Sstevel@tonic-gate 		 */
9368348SEric.Yu@Sun.COM 		sti->sti_laddr_len = (socklen_t)namelen;
9378348SEric.Yu@Sun.COM 		ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
9388348SEric.Yu@Sun.COM 		bcopy(name, sti->sti_laddr_sa, namelen);
9398348SEric.Yu@Sun.COM 
9408348SEric.Yu@Sun.COM 		addr = sti->sti_laddr_sa;
9418348SEric.Yu@Sun.COM 		addrlen = (t_uscalar_t)sti->sti_laddr_len;
9420Sstevel@tonic-gate 		switch (so->so_family) {
9430Sstevel@tonic-gate 		case AF_INET6:
9440Sstevel@tonic-gate 		case AF_INET:
9450Sstevel@tonic-gate 			break;
9460Sstevel@tonic-gate 		case AF_UNIX: {
9470Sstevel@tonic-gate 			struct sockaddr_un *soun =
9488348SEric.Yu@Sun.COM 			    (struct sockaddr_un *)sti->sti_laddr_sa;
9498032SRic.Aleshire@Sun.COM 			struct vnode *vp, *rvp;
9500Sstevel@tonic-gate 			struct vattr vattr;
9510Sstevel@tonic-gate 
9528348SEric.Yu@Sun.COM 			ASSERT(sti->sti_ux_bound_vp == NULL);
9530Sstevel@tonic-gate 			/*
9540Sstevel@tonic-gate 			 * Create vnode for the specified path name.
9558348SEric.Yu@Sun.COM 			 * Keep vnode held with a reference in sti_ux_bound_vp.
9560Sstevel@tonic-gate 			 * Use the vnode pointer as the address used in the
9570Sstevel@tonic-gate 			 * bind with the transport.
9580Sstevel@tonic-gate 			 *
9590Sstevel@tonic-gate 			 * Use the same mode as in BSD. In particular this does
9600Sstevel@tonic-gate 			 * not observe the umask.
9610Sstevel@tonic-gate 			 */
9620Sstevel@tonic-gate 			/* MAXPATHLEN + soun_family + nul termination */
9638348SEric.Yu@Sun.COM 			if (sti->sti_laddr_len >
9640Sstevel@tonic-gate 			    (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
9650Sstevel@tonic-gate 				error = ENAMETOOLONG;
9660Sstevel@tonic-gate 				eprintsoline(so, error);
9670Sstevel@tonic-gate 				goto done;
9680Sstevel@tonic-gate 			}
9690Sstevel@tonic-gate 			vattr.va_type = VSOCK;
9703446Smrj 			vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask;
9710Sstevel@tonic-gate 			vattr.va_mask = AT_TYPE|AT_MODE;
9720Sstevel@tonic-gate 			/* NOTE: holding so_lock */
9730Sstevel@tonic-gate 			error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr,
9745240Snordmark 			    EXCL, 0, &vp, CRMKNOD, 0, 0);
9750Sstevel@tonic-gate 			if (error) {
9760Sstevel@tonic-gate 				if (error == EEXIST)
9770Sstevel@tonic-gate 					error = EADDRINUSE;
9780Sstevel@tonic-gate 				eprintsoline(so, error);
9790Sstevel@tonic-gate 				goto done;
9800Sstevel@tonic-gate 			}
9810Sstevel@tonic-gate 			/*
9820Sstevel@tonic-gate 			 * Establish pointer from the underlying filesystem
9830Sstevel@tonic-gate 			 * vnode to the socket node.
9848348SEric.Yu@Sun.COM 			 * sti_ux_bound_vp and v_stream->sd_vnode form the
9850Sstevel@tonic-gate 			 * cross-linkage between the underlying filesystem
9860Sstevel@tonic-gate 			 * node and the socket node.
9870Sstevel@tonic-gate 			 */
9888032SRic.Aleshire@Sun.COM 
9898032SRic.Aleshire@Sun.COM 			if ((VOP_REALVP(vp, &rvp, NULL) == 0) && (vp != rvp)) {
9908032SRic.Aleshire@Sun.COM 				VN_HOLD(rvp);
9918032SRic.Aleshire@Sun.COM 				VN_RELE(vp);
9928032SRic.Aleshire@Sun.COM 				vp = rvp;
9938032SRic.Aleshire@Sun.COM 			}
9948032SRic.Aleshire@Sun.COM 
9950Sstevel@tonic-gate 			ASSERT(SOTOV(so)->v_stream);
9960Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
9970Sstevel@tonic-gate 			vp->v_stream = SOTOV(so)->v_stream;
9988348SEric.Yu@Sun.COM 			sti->sti_ux_bound_vp = vp;
9990Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 			/*
10020Sstevel@tonic-gate 			 * Use the vnode pointer value as a unique address
10030Sstevel@tonic-gate 			 * (together with the magic number to avoid conflicts
10040Sstevel@tonic-gate 			 * with implicit binds) in the transport provider.
10050Sstevel@tonic-gate 			 */
10068348SEric.Yu@Sun.COM 			sti->sti_ux_laddr.soua_vp =
10078348SEric.Yu@Sun.COM 			    (void *)sti->sti_ux_bound_vp;
10088348SEric.Yu@Sun.COM 			sti->sti_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT;
10098348SEric.Yu@Sun.COM 			addr = &sti->sti_ux_laddr;
10108348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
10110Sstevel@tonic-gate 			dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n",
10120Sstevel@tonic-gate 			    addrlen,
10138348SEric.Yu@Sun.COM 			    (void *)((struct so_ux_addr *)addr)->soua_vp));
10140Sstevel@tonic-gate 			break;
10150Sstevel@tonic-gate 		}
10160Sstevel@tonic-gate 		} /* end switch (so->so_family) */
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate 	/*
10200Sstevel@tonic-gate 	 * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since
10210Sstevel@tonic-gate 	 * the transport can start passing up T_CONN_IND messages
10220Sstevel@tonic-gate 	 * as soon as it receives the bind req and strsock_proto()
10230Sstevel@tonic-gate 	 * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs.
10240Sstevel@tonic-gate 	 */
10250Sstevel@tonic-gate 	if (flags & _SOBIND_LISTEN) {
10260Sstevel@tonic-gate 		if ((so->so_state & SS_ACCEPTCONN) == 0)
10270Sstevel@tonic-gate 			clear_acceptconn_on_err = B_TRUE;
10280Sstevel@tonic-gate 		save_so_backlog = so->so_backlog;
10290Sstevel@tonic-gate 		restore_backlog_on_err = B_TRUE;
10300Sstevel@tonic-gate 		so->so_state |= SS_ACCEPTCONN;
10310Sstevel@tonic-gate 		so->so_backlog = backlog;
10320Sstevel@tonic-gate 	}
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	/*
10350Sstevel@tonic-gate 	 * If NL7C addr(s) have been configured check for addr/port match,
10360Sstevel@tonic-gate 	 * or if an implicit NL7C socket via AF_NCA mark socket as NL7C.
10370Sstevel@tonic-gate 	 *
10380Sstevel@tonic-gate 	 * NL7C supports the TCP transport only so check AF_INET and AF_INET6
10390Sstevel@tonic-gate 	 * family sockets only. If match mark as such.
10400Sstevel@tonic-gate 	 */
10411974Sbrutus 	if (nl7c_enabled && ((addr != NULL &&
10420Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
10430Sstevel@tonic-gate 	    (nl7c = nl7c_lookup_addr(addr, addrlen))) ||
10448348SEric.Yu@Sun.COM 	    sti->sti_nl7c_flags == NL7C_AF_NCA)) {
10450Sstevel@tonic-gate 		/*
10460Sstevel@tonic-gate 		 * NL7C is not supported in non-global zones,
10470Sstevel@tonic-gate 		 * we enforce this restriction here.
10480Sstevel@tonic-gate 		 */
10490Sstevel@tonic-gate 		if (so->so_zoneid == GLOBAL_ZONEID) {
10500Sstevel@tonic-gate 			/* An NL7C socket, mark it */
10518348SEric.Yu@Sun.COM 			sti->sti_nl7c_flags |= NL7C_ENABLED;
10521974Sbrutus 			if (nl7c == NULL) {
10531974Sbrutus 				/*
10541974Sbrutus 				 * Was an AF_NCA bind() so add it to the
10551974Sbrutus 				 * addr list for reporting purposes.
10561974Sbrutus 				 */
10571974Sbrutus 				nl7c = nl7c_add_addr(addr, addrlen);
10581974Sbrutus 			}
10590Sstevel@tonic-gate 		} else
10600Sstevel@tonic-gate 			nl7c = NULL;
10610Sstevel@tonic-gate 	}
10628348SEric.Yu@Sun.COM 
10630Sstevel@tonic-gate 	/*
10640Sstevel@tonic-gate 	 * We send a T_BIND_REQ for TCP/UDP since we know it supports it,
10650Sstevel@tonic-gate 	 * for other transports we will send in a O_T_BIND_REQ.
10660Sstevel@tonic-gate 	 */
10670Sstevel@tonic-gate 	if (tcp_udp_xport &&
10680Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6))
10690Sstevel@tonic-gate 		PRIM_type = T_BIND_REQ;
10700Sstevel@tonic-gate 
10710Sstevel@tonic-gate 	bind_req.PRIM_type = PRIM_type;
10720Sstevel@tonic-gate 	bind_req.ADDR_length = addrlen;
10730Sstevel@tonic-gate 	bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
10740Sstevel@tonic-gate 	bind_req.CONIND_number = backlog;
10750Sstevel@tonic-gate 	/* NOTE: holding so_lock while sleeping */
10760Sstevel@tonic-gate 	mp = soallocproto2(&bind_req, sizeof (bind_req),
10778778SErik.Nordmark@Sun.COM 	    addr, addrlen, 0, _ALLOC_SLEEP, cr);
10788348SEric.Yu@Sun.COM 	sti->sti_laddr_valid = 0;
10798348SEric.Yu@Sun.COM 
10808348SEric.Yu@Sun.COM 	/* Done using sti_laddr_sa - can drop the lock */
10810Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
10845240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
10850Sstevel@tonic-gate 	if (error) {
10860Sstevel@tonic-gate 		eprintsoline(so, error);
10870Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
10880Sstevel@tonic-gate 		goto done;
10890Sstevel@tonic-gate 	}
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
10920Sstevel@tonic-gate 	error = sowaitprim(so, PRIM_type, T_BIND_ACK,
10930Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (*bind_ack), &mp, 0);
10940Sstevel@tonic-gate 	if (error) {
10950Sstevel@tonic-gate 		eprintsoline(so, error);
10960Sstevel@tonic-gate 		goto done;
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 	ASSERT(mp);
10990Sstevel@tonic-gate 	/*
11000Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
11010Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the bind
11020Sstevel@tonic-gate 	 * is allowed to complete.
11030Sstevel@tonic-gate 	 */
11040Sstevel@tonic-gate 
11050Sstevel@tonic-gate 	/* Mark as bound. This will be undone if we detect errors below. */
11060Sstevel@tonic-gate 	if (flags & _SOBIND_NOXLATE) {
11070Sstevel@tonic-gate 		ASSERT(so->so_family == AF_UNIX);
11088348SEric.Yu@Sun.COM 		sti->sti_faddr_noxlate = 1;
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND));
11110Sstevel@tonic-gate 	so->so_state |= SS_ISBOUND;
11128348SEric.Yu@Sun.COM 	ASSERT(sti->sti_unbind_mp);
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	/* note that we've already set SS_ACCEPTCONN above */
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	/*
11170Sstevel@tonic-gate 	 * Recompute addrlen - an unspecied bind sent down an
11180Sstevel@tonic-gate 	 * address of length zero but we expect the appropriate length
11190Sstevel@tonic-gate 	 * in return.
11200Sstevel@tonic-gate 	 */
11210Sstevel@tonic-gate 	addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ?
11228348SEric.Yu@Sun.COM 	    sizeof (sti->sti_ux_laddr) : sti->sti_laddr_len);
11230Sstevel@tonic-gate 
11240Sstevel@tonic-gate 	bind_ack = (struct T_bind_ack *)mp->b_rptr;
11250Sstevel@tonic-gate 	/*
11260Sstevel@tonic-gate 	 * The alignment restriction is really too strict but
11270Sstevel@tonic-gate 	 * we want enough alignment to inspect the fields of
11280Sstevel@tonic-gate 	 * a sockaddr_in.
11290Sstevel@tonic-gate 	 */
11300Sstevel@tonic-gate 	addr = sogetoff(mp, bind_ack->ADDR_offset,
11315240Snordmark 	    bind_ack->ADDR_length,
11325240Snordmark 	    __TPI_ALIGN_SIZE);
11330Sstevel@tonic-gate 	if (addr == NULL) {
11340Sstevel@tonic-gate 		freemsg(mp);
11350Sstevel@tonic-gate 		error = EPROTO;
11360Sstevel@tonic-gate 		eprintsoline(so, error);
11370Sstevel@tonic-gate 		goto done;
11380Sstevel@tonic-gate 	}
11390Sstevel@tonic-gate 	if (!(flags & _SOBIND_UNSPEC)) {
11400Sstevel@tonic-gate 		/*
11410Sstevel@tonic-gate 		 * Verify that the transport didn't return something we
11420Sstevel@tonic-gate 		 * did not want e.g. an address other than what we asked for.
11430Sstevel@tonic-gate 		 *
11440Sstevel@tonic-gate 		 * NOTE: These checks would go away if/when we switch to
11450Sstevel@tonic-gate 		 * using the new TPI (in which the transport would fail
11460Sstevel@tonic-gate 		 * the request instead of assigning a different address).
11470Sstevel@tonic-gate 		 *
11480Sstevel@tonic-gate 		 * NOTE2: For protocols that we don't know (i.e. any
11490Sstevel@tonic-gate 		 * other than AF_INET6, AF_INET and AF_UNIX), we
11500Sstevel@tonic-gate 		 * cannot know if the transport should be expected to
11510Sstevel@tonic-gate 		 * return the same address as that requested.
11520Sstevel@tonic-gate 		 *
11530Sstevel@tonic-gate 		 * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send
11540Sstevel@tonic-gate 		 * down a T_BIND_REQ. We use O_T_BIND_REQ for others.
11550Sstevel@tonic-gate 		 *
11560Sstevel@tonic-gate 		 * For example, in the case of netatalk it may be
11570Sstevel@tonic-gate 		 * inappropriate for the transport to return the
11580Sstevel@tonic-gate 		 * requested address (as it may have allocated a local
11590Sstevel@tonic-gate 		 * port number in behaviour similar to that of an
11600Sstevel@tonic-gate 		 * AF_INET bind request with a port number of zero).
11610Sstevel@tonic-gate 		 *
11620Sstevel@tonic-gate 		 * Given the definition of O_T_BIND_REQ, where the
11630Sstevel@tonic-gate 		 * transport may bind to an address other than the
11640Sstevel@tonic-gate 		 * requested address, it's not possible to determine
11650Sstevel@tonic-gate 		 * whether a returned address that differs from the
11660Sstevel@tonic-gate 		 * requested address is a reason to fail (because the
11670Sstevel@tonic-gate 		 * requested address was not available) or succeed
11680Sstevel@tonic-gate 		 * (because the transport allocated an appropriate
11690Sstevel@tonic-gate 		 * address and/or port).
11700Sstevel@tonic-gate 		 *
11710Sstevel@tonic-gate 		 * sockfs currently requires that the transport return
11720Sstevel@tonic-gate 		 * the requested address in the T_BIND_ACK, unless
11730Sstevel@tonic-gate 		 * there is code here to allow for any discrepancy.
11740Sstevel@tonic-gate 		 * Such code exists for AF_INET and AF_INET6.
11750Sstevel@tonic-gate 		 *
11760Sstevel@tonic-gate 		 * Netatalk chooses to return the requested address
11770Sstevel@tonic-gate 		 * rather than the (correct) allocated address.  This
11780Sstevel@tonic-gate 		 * means that netatalk violates the TPI specification
11790Sstevel@tonic-gate 		 * (and would not function correctly if used from a
11800Sstevel@tonic-gate 		 * TLI application), but it does mean that it works
11810Sstevel@tonic-gate 		 * with sockfs.
11820Sstevel@tonic-gate 		 *
11830Sstevel@tonic-gate 		 * As noted above, using the newer XTI bind primitive
11840Sstevel@tonic-gate 		 * (T_BIND_REQ) in preference to O_T_BIND_REQ would
11850Sstevel@tonic-gate 		 * allow sockfs to be more sure about whether or not
11860Sstevel@tonic-gate 		 * the bind request had succeeded (as transports are
11870Sstevel@tonic-gate 		 * not permitted to bind to a different address than
11880Sstevel@tonic-gate 		 * that requested - they must return failure).
11890Sstevel@tonic-gate 		 * Unfortunately, support for T_BIND_REQ may not be
11900Sstevel@tonic-gate 		 * present in all transport implementations (netatalk,
11910Sstevel@tonic-gate 		 * for example, doesn't have it), making the
11920Sstevel@tonic-gate 		 * transition difficult.
11930Sstevel@tonic-gate 		 */
11940Sstevel@tonic-gate 		if (bind_ack->ADDR_length != addrlen) {
11950Sstevel@tonic-gate 			/* Assumes that the requested address was in use */
11960Sstevel@tonic-gate 			freemsg(mp);
11970Sstevel@tonic-gate 			error = EADDRINUSE;
11980Sstevel@tonic-gate 			eprintsoline(so, error);
11990Sstevel@tonic-gate 			goto done;
12000Sstevel@tonic-gate 		}
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		switch (so->so_family) {
12030Sstevel@tonic-gate 		case AF_INET6:
12040Sstevel@tonic-gate 		case AF_INET: {
12050Sstevel@tonic-gate 			sin_t *rname, *aname;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 			rname = (sin_t *)addr;
12088348SEric.Yu@Sun.COM 			aname = (sin_t *)sti->sti_laddr_sa;
12090Sstevel@tonic-gate 
12100Sstevel@tonic-gate 			/*
12110Sstevel@tonic-gate 			 * Take advantage of the alignment
12120Sstevel@tonic-gate 			 * of sin_port and sin6_port which fall
12130Sstevel@tonic-gate 			 * in the same place in their data structures.
12140Sstevel@tonic-gate 			 * Just use sin_port for either address family.
12150Sstevel@tonic-gate 			 *
12160Sstevel@tonic-gate 			 * This may become a problem if (heaven forbid)
12170Sstevel@tonic-gate 			 * there's a separate ipv6port_reserved... :-P
12180Sstevel@tonic-gate 			 *
12190Sstevel@tonic-gate 			 * Binding to port 0 has the semantics of letting
12200Sstevel@tonic-gate 			 * the transport bind to any port.
12210Sstevel@tonic-gate 			 *
12220Sstevel@tonic-gate 			 * If the transport is TCP or UDP since we had sent
12230Sstevel@tonic-gate 			 * a T_BIND_REQ we would not get a port other than
12240Sstevel@tonic-gate 			 * what we asked for.
12250Sstevel@tonic-gate 			 */
12260Sstevel@tonic-gate 			if (tcp_udp_xport) {
12270Sstevel@tonic-gate 				/*
12280Sstevel@tonic-gate 				 * Pick up the new port number if we bound to
12290Sstevel@tonic-gate 				 * port 0.
12300Sstevel@tonic-gate 				 */
12310Sstevel@tonic-gate 				if (aname->sin_port == 0)
12320Sstevel@tonic-gate 					aname->sin_port = rname->sin_port;
12338348SEric.Yu@Sun.COM 				sti->sti_laddr_valid = 1;
12340Sstevel@tonic-gate 				break;
12350Sstevel@tonic-gate 			}
12360Sstevel@tonic-gate 			if (aname->sin_port != 0 &&
12370Sstevel@tonic-gate 			    aname->sin_port != rname->sin_port) {
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 			 * Pick up the new port number if we bound to port 0.
12450Sstevel@tonic-gate 			 */
12460Sstevel@tonic-gate 			aname->sin_port = rname->sin_port;
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 			/*
12490Sstevel@tonic-gate 			 * Unfortunately, addresses aren't _quite_ the same.
12500Sstevel@tonic-gate 			 */
12510Sstevel@tonic-gate 			if (so->so_family == AF_INET) {
12520Sstevel@tonic-gate 				if (aname->sin_addr.s_addr !=
12530Sstevel@tonic-gate 				    rname->sin_addr.s_addr) {
12540Sstevel@tonic-gate 					freemsg(mp);
12550Sstevel@tonic-gate 					error = EADDRNOTAVAIL;
12560Sstevel@tonic-gate 					eprintsoline(so, error);
12570Sstevel@tonic-gate 					goto done;
12580Sstevel@tonic-gate 				}
12590Sstevel@tonic-gate 			} else {
12600Sstevel@tonic-gate 				sin6_t *rname6 = (sin6_t *)rname;
12610Sstevel@tonic-gate 				sin6_t *aname6 = (sin6_t *)aname;
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate 				if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr,
12640Sstevel@tonic-gate 				    &rname6->sin6_addr)) {
12650Sstevel@tonic-gate 					freemsg(mp);
12660Sstevel@tonic-gate 					error = EADDRNOTAVAIL;
12670Sstevel@tonic-gate 					eprintsoline(so, error);
12680Sstevel@tonic-gate 					goto done;
12690Sstevel@tonic-gate 				}
12700Sstevel@tonic-gate 			}
12710Sstevel@tonic-gate 			break;
12720Sstevel@tonic-gate 		}
12730Sstevel@tonic-gate 		case AF_UNIX:
12748348SEric.Yu@Sun.COM 			if (bcmp(addr, &sti->sti_ux_laddr, addrlen) != 0) {
12750Sstevel@tonic-gate 				freemsg(mp);
12760Sstevel@tonic-gate 				error = EADDRINUSE;
12770Sstevel@tonic-gate 				eprintsoline(so, error);
12780Sstevel@tonic-gate 				eprintso(so,
12795240Snordmark 				    ("addrlen %d, addr 0x%x, vp %p\n",
12805240Snordmark 				    addrlen, *((int *)addr),
12818348SEric.Yu@Sun.COM 				    (void *)sti->sti_ux_bound_vp));
12820Sstevel@tonic-gate 				goto done;
12830Sstevel@tonic-gate 			}
12848348SEric.Yu@Sun.COM 			sti->sti_laddr_valid = 1;
12850Sstevel@tonic-gate 			break;
12860Sstevel@tonic-gate 		default:
12870Sstevel@tonic-gate 			/*
12880Sstevel@tonic-gate 			 * NOTE: This assumes that addresses can be
12890Sstevel@tonic-gate 			 * byte-compared for equivalence.
12900Sstevel@tonic-gate 			 */
12918348SEric.Yu@Sun.COM 			if (bcmp(addr, sti->sti_laddr_sa, addrlen) != 0) {
12920Sstevel@tonic-gate 				freemsg(mp);
12930Sstevel@tonic-gate 				error = EADDRINUSE;
12940Sstevel@tonic-gate 				eprintsoline(so, error);
12950Sstevel@tonic-gate 				goto done;
12960Sstevel@tonic-gate 			}
12970Sstevel@tonic-gate 			/*
12988348SEric.Yu@Sun.COM 			 * Don't mark sti_laddr_valid, as we cannot be
12990Sstevel@tonic-gate 			 * sure that the returned address is the real
13000Sstevel@tonic-gate 			 * bound address when talking to an unknown
13010Sstevel@tonic-gate 			 * transport.
13020Sstevel@tonic-gate 			 */
13030Sstevel@tonic-gate 			break;
13040Sstevel@tonic-gate 		}
13050Sstevel@tonic-gate 	} else {
13060Sstevel@tonic-gate 		/*
13070Sstevel@tonic-gate 		 * Save for returned address for getsockname.
13080Sstevel@tonic-gate 		 * Needed for unspecific bind unless transport supports
13090Sstevel@tonic-gate 		 * the TI_GETMYNAME ioctl.
13100Sstevel@tonic-gate 		 * Do this for AF_INET{,6} even though they do, as
13110Sstevel@tonic-gate 		 * caching info here is much better performance than
13120Sstevel@tonic-gate 		 * a TPI/STREAMS trip to the transport for getsockname.
13130Sstevel@tonic-gate 		 * Any which can't for some reason _must_ _not_ set
13148348SEric.Yu@Sun.COM 		 * sti_laddr_valid here for the caching version of
13158348SEric.Yu@Sun.COM 		 * getsockname to not break;
13160Sstevel@tonic-gate 		 */
13170Sstevel@tonic-gate 		switch (so->so_family) {
13180Sstevel@tonic-gate 		case AF_UNIX:
13190Sstevel@tonic-gate 			/*
13200Sstevel@tonic-gate 			 * Record the address bound with the transport
13210Sstevel@tonic-gate 			 * for use by socketpair.
13220Sstevel@tonic-gate 			 */
13238348SEric.Yu@Sun.COM 			bcopy(addr, &sti->sti_ux_laddr, addrlen);
13248348SEric.Yu@Sun.COM 			sti->sti_laddr_valid = 1;
13250Sstevel@tonic-gate 			break;
13260Sstevel@tonic-gate 		case AF_INET:
13270Sstevel@tonic-gate 		case AF_INET6:
13288348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
13298348SEric.Yu@Sun.COM 			bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
13308348SEric.Yu@Sun.COM 			sti->sti_laddr_valid = 1;
13310Sstevel@tonic-gate 			break;
13320Sstevel@tonic-gate 		default:
13330Sstevel@tonic-gate 			/*
13348348SEric.Yu@Sun.COM 			 * Don't mark sti_laddr_valid, as we cannot be
13350Sstevel@tonic-gate 			 * sure that the returned address is the real
13360Sstevel@tonic-gate 			 * bound address when talking to an unknown
13370Sstevel@tonic-gate 			 * transport.
13380Sstevel@tonic-gate 			 */
13390Sstevel@tonic-gate 			break;
13400Sstevel@tonic-gate 		}
13410Sstevel@tonic-gate 	}
13420Sstevel@tonic-gate 
13430Sstevel@tonic-gate 	if (nl7c != NULL) {
13441974Sbrutus 		/* Register listen()er sonode pointer with NL7C */
13451974Sbrutus 		nl7c_listener_addr(nl7c, so);
13460Sstevel@tonic-gate 	}
13470Sstevel@tonic-gate 
13480Sstevel@tonic-gate 	freemsg(mp);
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate done:
13510Sstevel@tonic-gate 	if (error) {
13520Sstevel@tonic-gate 		/* reset state & backlog to values held on entry */
13530Sstevel@tonic-gate 		if (clear_acceptconn_on_err == B_TRUE)
13540Sstevel@tonic-gate 			so->so_state &= ~SS_ACCEPTCONN;
13550Sstevel@tonic-gate 		if (restore_backlog_on_err == B_TRUE)
13560Sstevel@tonic-gate 			so->so_backlog = save_so_backlog;
13570Sstevel@tonic-gate 
13580Sstevel@tonic-gate 		if (unbind_on_err && so->so_state & SS_ISBOUND) {
13590Sstevel@tonic-gate 			int err;
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 			err = sotpi_unbind(so, 0);
13620Sstevel@tonic-gate 			/* LINTED - statement has no consequent: if */
13630Sstevel@tonic-gate 			if (err) {
13640Sstevel@tonic-gate 				eprintsoline(so, error);
13650Sstevel@tonic-gate 			} else {
13660Sstevel@tonic-gate 				ASSERT(!(so->so_state & SS_ISBOUND));
13670Sstevel@tonic-gate 			}
13680Sstevel@tonic-gate 		}
13690Sstevel@tonic-gate 	}
13700Sstevel@tonic-gate 	if (!(flags & _SOBIND_LOCK_HELD)) {
13710Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
13720Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
13730Sstevel@tonic-gate 	} else {
13740Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
13750Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
13760Sstevel@tonic-gate 	}
13770Sstevel@tonic-gate 	return (error);
13780Sstevel@tonic-gate }
13790Sstevel@tonic-gate 
13800Sstevel@tonic-gate /* bind the socket */
1381741Smasputra static int
sotpi_bind(struct sonode * so,struct sockaddr * name,socklen_t namelen,int flags,struct cred * cr)13820Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
13838348SEric.Yu@Sun.COM     int flags, struct cred *cr)
13840Sstevel@tonic-gate {
13850Sstevel@tonic-gate 	if ((flags & _SOBIND_SOCKETPAIR) == 0)
13868348SEric.Yu@Sun.COM 		return (sotpi_bindlisten(so, name, namelen, 0, flags, cr));
13870Sstevel@tonic-gate 
13880Sstevel@tonic-gate 	flags &= ~_SOBIND_SOCKETPAIR;
13898348SEric.Yu@Sun.COM 	return (sotpi_bindlisten(so, name, namelen, 1, flags, cr));
13900Sstevel@tonic-gate }
13910Sstevel@tonic-gate 
13920Sstevel@tonic-gate /*
13930Sstevel@tonic-gate  * Unbind a socket - used when bind() fails, when bind() specifies a NULL
13940Sstevel@tonic-gate  * address, or when listen needs to unbind and bind.
13950Sstevel@tonic-gate  * If the _SOUNBIND_REBIND flag is specified the addresses are retained
13960Sstevel@tonic-gate  * so that a sobind can pick them up.
13970Sstevel@tonic-gate  */
13980Sstevel@tonic-gate static int
sotpi_unbind(struct sonode * so,int flags)13990Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags)
14000Sstevel@tonic-gate {
14010Sstevel@tonic-gate 	struct T_unbind_req	unbind_req;
14020Sstevel@tonic-gate 	int			error = 0;
14030Sstevel@tonic-gate 	mblk_t			*mp;
14048348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n",
14077240Srh87107 	    (void *)so, flags, pr_state(so->so_state, so->so_mode)));
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14100Sstevel@tonic-gate 	ASSERT(so->so_flag & SOLOCKED);
14110Sstevel@tonic-gate 
14120Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
14130Sstevel@tonic-gate 		error = EINVAL;
14140Sstevel@tonic-gate 		eprintsoline(so, error);
14150Sstevel@tonic-gate 		goto done;
14160Sstevel@tonic-gate 	}
14170Sstevel@tonic-gate 
14180Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
14190Sstevel@tonic-gate 
14200Sstevel@tonic-gate 	/*
14210Sstevel@tonic-gate 	 * Flush the read and write side (except stream head read queue)
14220Sstevel@tonic-gate 	 * and send down T_UNBIND_REQ.
14230Sstevel@tonic-gate 	 */
14240Sstevel@tonic-gate 	(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
14250Sstevel@tonic-gate 
14260Sstevel@tonic-gate 	unbind_req.PRIM_type = T_UNBIND_REQ;
14270Sstevel@tonic-gate 	mp = soallocproto1(&unbind_req, sizeof (unbind_req),
14288778SErik.Nordmark@Sun.COM 	    0, _ALLOC_SLEEP, CRED());
14290Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
14305240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
14310Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
14320Sstevel@tonic-gate 	if (error) {
14330Sstevel@tonic-gate 		eprintsoline(so, error);
14340Sstevel@tonic-gate 		goto done;
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 	error = sowaitokack(so, T_UNBIND_REQ);
14380Sstevel@tonic-gate 	if (error) {
14390Sstevel@tonic-gate 		eprintsoline(so, error);
14400Sstevel@tonic-gate 		goto done;
14410Sstevel@tonic-gate 	}
14420Sstevel@tonic-gate 
14430Sstevel@tonic-gate 	/*
14440Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
14450Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the unbind
14460Sstevel@tonic-gate 	 * is allowed to complete.
14470Sstevel@tonic-gate 	 */
14480Sstevel@tonic-gate 	if (!(flags & _SOUNBIND_REBIND)) {
14490Sstevel@tonic-gate 		/*
14500Sstevel@tonic-gate 		 * Clear out bound address.
14510Sstevel@tonic-gate 		 */
14520Sstevel@tonic-gate 		vnode_t *vp;
14530Sstevel@tonic-gate 
14548348SEric.Yu@Sun.COM 		if ((vp = sti->sti_ux_bound_vp) != NULL) {
14558348SEric.Yu@Sun.COM 			sti->sti_ux_bound_vp = NULL;
14560Sstevel@tonic-gate 			vn_rele_stream(vp);
14570Sstevel@tonic-gate 		}
14580Sstevel@tonic-gate 		/* Clear out address */
14598348SEric.Yu@Sun.COM 		sti->sti_laddr_len = 0;
14608348SEric.Yu@Sun.COM 	}
14618348SEric.Yu@Sun.COM 	so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN);
14628348SEric.Yu@Sun.COM 	sti->sti_laddr_valid = 0;
14631974Sbrutus 
14640Sstevel@tonic-gate done:
1465898Skais 
14660Sstevel@tonic-gate 	/* If the caller held the lock don't release it here */
14670Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14680Sstevel@tonic-gate 	ASSERT(so->so_flag & SOLOCKED);
14690Sstevel@tonic-gate 
14700Sstevel@tonic-gate 	return (error);
14710Sstevel@tonic-gate }
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate /*
14740Sstevel@tonic-gate  * listen on the socket.
14750Sstevel@tonic-gate  * For TPI conforming transports this has to first unbind with the transport
14760Sstevel@tonic-gate  * and then bind again using the new backlog.
14770Sstevel@tonic-gate  */
14788348SEric.Yu@Sun.COM /* ARGSUSED */
14790Sstevel@tonic-gate int
sotpi_listen(struct sonode * so,int backlog,struct cred * cr)14808348SEric.Yu@Sun.COM sotpi_listen(struct sonode *so, int backlog, struct cred *cr)
14810Sstevel@tonic-gate {
14820Sstevel@tonic-gate 	int		error = 0;
14838348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
14840Sstevel@tonic-gate 
14850Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n",
14867240Srh87107 	    (void *)so, backlog, pr_state(so->so_state, so->so_mode)));
14870Sstevel@tonic-gate 
14888348SEric.Yu@Sun.COM 	if (sti->sti_serv_type == T_CLTS)
14890Sstevel@tonic-gate 		return (EOPNOTSUPP);
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	/*
14920Sstevel@tonic-gate 	 * If the socket is ready to accept connections already, then
14930Sstevel@tonic-gate 	 * return without doing anything.  This avoids a problem where
14940Sstevel@tonic-gate 	 * a second listen() call fails if a connection is pending and
14950Sstevel@tonic-gate 	 * leaves the socket unbound. Only when we are not unbinding
14960Sstevel@tonic-gate 	 * with the transport can we safely increase the backlog.
14970Sstevel@tonic-gate 	 */
14980Sstevel@tonic-gate 	if (so->so_state & SS_ACCEPTCONN &&
14990Sstevel@tonic-gate 	    !((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15005240Snordmark 	    /*CONSTCOND*/
15015240Snordmark 	    !solisten_tpi_tcp))
15020Sstevel@tonic-gate 		return (0);
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate 	if (so->so_state & SS_ISCONNECTED)
15050Sstevel@tonic-gate 		return (EINVAL);
15060Sstevel@tonic-gate 
15070Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
15080Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	/*
15110Sstevel@tonic-gate 	 * If the listen doesn't change the backlog we do nothing.
15120Sstevel@tonic-gate 	 * This avoids an EPROTO error from the transport.
15130Sstevel@tonic-gate 	 */
15140Sstevel@tonic-gate 	if ((so->so_state & SS_ACCEPTCONN) &&
15150Sstevel@tonic-gate 	    so->so_backlog == backlog)
15160Sstevel@tonic-gate 		goto done;
15170Sstevel@tonic-gate 
15180Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
15190Sstevel@tonic-gate 		/*
15200Sstevel@tonic-gate 		 * Must have been explicitly bound in the UNIX domain.
15210Sstevel@tonic-gate 		 */
15220Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
15230Sstevel@tonic-gate 			error = EINVAL;
15240Sstevel@tonic-gate 			goto done;
15250Sstevel@tonic-gate 		}
15260Sstevel@tonic-gate 		error = sotpi_bindlisten(so, NULL, 0, backlog,
15278348SEric.Yu@Sun.COM 		    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15280Sstevel@tonic-gate 	} else if (backlog > 0) {
15290Sstevel@tonic-gate 		/*
15300Sstevel@tonic-gate 		 * AF_INET{,6} hack to avoid losing the port.
15310Sstevel@tonic-gate 		 * Assumes that all AF_INET{,6} transports can handle a
15320Sstevel@tonic-gate 		 * O_T_BIND_REQ with a non-zero CONIND_number when the TPI
15330Sstevel@tonic-gate 		 * has already bound thus it is possible to avoid the unbind.
15340Sstevel@tonic-gate 		 */
15350Sstevel@tonic-gate 		if (!((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15360Sstevel@tonic-gate 		    /*CONSTCOND*/
15370Sstevel@tonic-gate 		    !solisten_tpi_tcp)) {
15380Sstevel@tonic-gate 			error = sotpi_unbind(so, _SOUNBIND_REBIND);
15390Sstevel@tonic-gate 			if (error)
15400Sstevel@tonic-gate 				goto done;
15410Sstevel@tonic-gate 		}
15420Sstevel@tonic-gate 		error = sotpi_bindlisten(so, NULL, 0, backlog,
15438348SEric.Yu@Sun.COM 		    _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15440Sstevel@tonic-gate 	} else {
15450Sstevel@tonic-gate 		so->so_state |= SS_ACCEPTCONN;
15460Sstevel@tonic-gate 		so->so_backlog = backlog;
15470Sstevel@tonic-gate 	}
15480Sstevel@tonic-gate 	if (error)
15490Sstevel@tonic-gate 		goto done;
15500Sstevel@tonic-gate 	ASSERT(so->so_state & SS_ACCEPTCONN);
15510Sstevel@tonic-gate done:
15520Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
15530Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
15540Sstevel@tonic-gate 	return (error);
15550Sstevel@tonic-gate }
15560Sstevel@tonic-gate 
15570Sstevel@tonic-gate /*
15580Sstevel@tonic-gate  * Disconnect either a specified seqno or all (-1).
15590Sstevel@tonic-gate  * The former is used on listening sockets only.
15600Sstevel@tonic-gate  *
15610Sstevel@tonic-gate  * When seqno == -1 sodisconnect could call sotpi_unbind. However,
15620Sstevel@tonic-gate  * the current use of sodisconnect(seqno == -1) is only for shutdown
15630Sstevel@tonic-gate  * so there is no point (and potentially incorrect) to unbind.
15640Sstevel@tonic-gate  */
15658348SEric.Yu@Sun.COM static int
sodisconnect(struct sonode * so,t_scalar_t seqno,int flags)15660Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags)
15670Sstevel@tonic-gate {
15680Sstevel@tonic-gate 	struct T_discon_req	discon_req;
15690Sstevel@tonic-gate 	int			error = 0;
15700Sstevel@tonic-gate 	mblk_t			*mp;
15710Sstevel@tonic-gate 
15720Sstevel@tonic-gate 	dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n",
15737240Srh87107 	    (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode)));
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
15760Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
15770Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
15780Sstevel@tonic-gate 	} else {
15790Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
15800Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) {
15840Sstevel@tonic-gate 		error = EINVAL;
15850Sstevel@tonic-gate 		eprintsoline(so, error);
15860Sstevel@tonic-gate 		goto done;
15870Sstevel@tonic-gate 	}
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
15900Sstevel@tonic-gate 	/*
15910Sstevel@tonic-gate 	 * Flush the write side (unless this is a listener)
15920Sstevel@tonic-gate 	 * and then send down a T_DISCON_REQ.
15930Sstevel@tonic-gate 	 * (Don't flush on listener since it could flush {O_}T_CONN_RES
15940Sstevel@tonic-gate 	 * and other messages.)
15950Sstevel@tonic-gate 	 */
15960Sstevel@tonic-gate 	if (!(so->so_state & SS_ACCEPTCONN))
15970Sstevel@tonic-gate 		(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW);
15980Sstevel@tonic-gate 
15990Sstevel@tonic-gate 	discon_req.PRIM_type = T_DISCON_REQ;
16000Sstevel@tonic-gate 	discon_req.SEQ_number = seqno;
16010Sstevel@tonic-gate 	mp = soallocproto1(&discon_req, sizeof (discon_req),
16028778SErik.Nordmark@Sun.COM 	    0, _ALLOC_SLEEP, CRED());
16030Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
16045240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
16050Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
16060Sstevel@tonic-gate 	if (error) {
16070Sstevel@tonic-gate 		eprintsoline(so, error);
16080Sstevel@tonic-gate 		goto done;
16090Sstevel@tonic-gate 	}
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	error = sowaitokack(so, T_DISCON_REQ);
16120Sstevel@tonic-gate 	if (error) {
16130Sstevel@tonic-gate 		eprintsoline(so, error);
16140Sstevel@tonic-gate 		goto done;
16150Sstevel@tonic-gate 	}
16160Sstevel@tonic-gate 	/*
16170Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
16180Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the disconnect
16190Sstevel@tonic-gate 	 * is allowed to complete. However, it is not possible to
16200Sstevel@tonic-gate 	 * assert that SS_ISCONNECTED|SS_ISCONNECTING are set.
16210Sstevel@tonic-gate 	 */
16228348SEric.Yu@Sun.COM 	so->so_state &= ~(SS_ISCONNECTED|SS_ISCONNECTING);
16238348SEric.Yu@Sun.COM 	SOTOTPI(so)->sti_laddr_valid = 0;
16248348SEric.Yu@Sun.COM 	SOTOTPI(so)->sti_faddr_valid = 0;
16250Sstevel@tonic-gate done:
16260Sstevel@tonic-gate 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
16270Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
16280Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
16290Sstevel@tonic-gate 	} else {
16300Sstevel@tonic-gate 		/* If the caller held the lock don't release it here */
16310Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
16320Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
16330Sstevel@tonic-gate 	}
16340Sstevel@tonic-gate 	return (error);
16350Sstevel@tonic-gate }
16360Sstevel@tonic-gate 
16378348SEric.Yu@Sun.COM /* ARGSUSED */
16380Sstevel@tonic-gate int
sotpi_accept(struct sonode * so,int fflag,struct cred * cr,struct sonode ** nsop)16398348SEric.Yu@Sun.COM sotpi_accept(struct sonode *so, int fflag, struct cred *cr,
16408348SEric.Yu@Sun.COM     struct sonode **nsop)
16410Sstevel@tonic-gate {
16420Sstevel@tonic-gate 	struct T_conn_ind	*conn_ind;
16430Sstevel@tonic-gate 	struct T_conn_res	*conn_res;
16440Sstevel@tonic-gate 	int			error = 0;
164512644SAnders.Persson@Sun.COM 	mblk_t			*mp, *ack_mp;
16460Sstevel@tonic-gate 	struct sonode		*nso;
16470Sstevel@tonic-gate 	vnode_t			*nvp;
16480Sstevel@tonic-gate 	void			*src;
16490Sstevel@tonic-gate 	t_uscalar_t		srclen;
16500Sstevel@tonic-gate 	void			*opt;
16510Sstevel@tonic-gate 	t_uscalar_t		optlen;
16520Sstevel@tonic-gate 	t_scalar_t		PRIM_type;
16530Sstevel@tonic-gate 	t_scalar_t		SEQ_number;
16544379Sja97890 	size_t			sinlen;
16558348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
16568348SEric.Yu@Sun.COM 	sotpi_info_t		*nsti;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n",
16597240Srh87107 	    (void *)so, fflag, (void *)nsop,
16607240Srh87107 	    pr_state(so->so_state, so->so_mode)));
16610Sstevel@tonic-gate 
16620Sstevel@tonic-gate 	/*
16630Sstevel@tonic-gate 	 * Defer single-threading the accepting socket until
16640Sstevel@tonic-gate 	 * the T_CONN_IND has been received and parsed and the
16650Sstevel@tonic-gate 	 * new sonode has been opened.
16660Sstevel@tonic-gate 	 */
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	/* Check that we are not already connected */
16690Sstevel@tonic-gate 	if ((so->so_state & SS_ACCEPTCONN) == 0)
16700Sstevel@tonic-gate 		goto conn_bad;
16710Sstevel@tonic-gate again:
16720Sstevel@tonic-gate 	if ((error = sowaitconnind(so, fflag, &mp)) != 0)
16730Sstevel@tonic-gate 		goto e_bad;
16740Sstevel@tonic-gate 
16758348SEric.Yu@Sun.COM 	ASSERT(mp != NULL);
16760Sstevel@tonic-gate 	conn_ind = (struct T_conn_ind *)mp->b_rptr;
1677898Skais 
16780Sstevel@tonic-gate 	/*
16790Sstevel@tonic-gate 	 * Save SEQ_number for error paths.
16800Sstevel@tonic-gate 	 */
16810Sstevel@tonic-gate 	SEQ_number = conn_ind->SEQ_number;
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate 	srclen = conn_ind->SRC_length;
16840Sstevel@tonic-gate 	src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1);
16850Sstevel@tonic-gate 	if (src == NULL) {
16860Sstevel@tonic-gate 		error = EPROTO;
16870Sstevel@tonic-gate 		freemsg(mp);
16880Sstevel@tonic-gate 		eprintsoline(so, error);
16890Sstevel@tonic-gate 		goto disconnect_unlocked;
16900Sstevel@tonic-gate 	}
16910Sstevel@tonic-gate 	optlen = conn_ind->OPT_length;
16920Sstevel@tonic-gate 	switch (so->so_family) {
16930Sstevel@tonic-gate 	case AF_INET:
16940Sstevel@tonic-gate 	case AF_INET6:
16958348SEric.Yu@Sun.COM 		if ((optlen == sizeof (intptr_t)) && (sti->sti_direct != 0)) {
16960Sstevel@tonic-gate 			bcopy(mp->b_rptr + conn_ind->OPT_offset,
16970Sstevel@tonic-gate 			    &opt, conn_ind->OPT_length);
16980Sstevel@tonic-gate 		} else {
16990Sstevel@tonic-gate 			/*
17000Sstevel@tonic-gate 			 * The transport (in this case TCP) hasn't sent up
17010Sstevel@tonic-gate 			 * a pointer to an instance for the accept fast-path.
17020Sstevel@tonic-gate 			 * Disable fast-path completely because the call to
17030Sstevel@tonic-gate 			 * sotpi_create() below would otherwise create an
17040Sstevel@tonic-gate 			 * incomplete TCP instance, which would lead to
17050Sstevel@tonic-gate 			 * problems when sockfs sends a normal T_CONN_RES
17060Sstevel@tonic-gate 			 * message down the new stream.
17070Sstevel@tonic-gate 			 */
17088348SEric.Yu@Sun.COM 			if (sti->sti_direct) {
1709741Smasputra 				int rval;
1710741Smasputra 				/*
1711741Smasputra 				 * For consistency we inform tcp to disable
1712741Smasputra 				 * direct interface on the listener, though
1713741Smasputra 				 * we can certainly live without doing this
1714741Smasputra 				 * because no data will ever travel upstream
1715741Smasputra 				 * on the listening socket.
1716741Smasputra 				 */
17178348SEric.Yu@Sun.COM 				sti->sti_direct = 0;
1718741Smasputra 				(void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK,
17198778SErik.Nordmark@Sun.COM 				    0, 0, K_TO_K, cr, &rval);
1720741Smasputra 			}
17210Sstevel@tonic-gate 			opt = NULL;
17220Sstevel@tonic-gate 			optlen = 0;
17230Sstevel@tonic-gate 		}
17240Sstevel@tonic-gate 		break;
17250Sstevel@tonic-gate 	case AF_UNIX:
17260Sstevel@tonic-gate 	default:
17270Sstevel@tonic-gate 		if (optlen != 0) {
17280Sstevel@tonic-gate 			opt = sogetoff(mp, conn_ind->OPT_offset, optlen,
17290Sstevel@tonic-gate 			    __TPI_ALIGN_SIZE);
17300Sstevel@tonic-gate 			if (opt == NULL) {
17310Sstevel@tonic-gate 				error = EPROTO;
17320Sstevel@tonic-gate 				freemsg(mp);
17330Sstevel@tonic-gate 				eprintsoline(so, error);
17340Sstevel@tonic-gate 				goto disconnect_unlocked;
17350Sstevel@tonic-gate 			}
17360Sstevel@tonic-gate 		}
17370Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
17388348SEric.Yu@Sun.COM 			if (!sti->sti_faddr_noxlate) {
17390Sstevel@tonic-gate 				src = NULL;
17400Sstevel@tonic-gate 				srclen = 0;
17410Sstevel@tonic-gate 			}
17420Sstevel@tonic-gate 			/* Extract src address from options */
17430Sstevel@tonic-gate 			if (optlen != 0)
17440Sstevel@tonic-gate 				so_getopt_srcaddr(opt, optlen, &src, &srclen);
17450Sstevel@tonic-gate 		}
17460Sstevel@tonic-gate 		break;
17470Sstevel@tonic-gate 	}
17480Sstevel@tonic-gate 
17490Sstevel@tonic-gate 	/*
17500Sstevel@tonic-gate 	 * Create the new socket.
17510Sstevel@tonic-gate 	 */
17528348SEric.Yu@Sun.COM 	nso = socket_newconn(so, NULL, NULL, SOCKET_SLEEP, &error);
17530Sstevel@tonic-gate 	if (nso == NULL) {
17540Sstevel@tonic-gate 		ASSERT(error != 0);
17550Sstevel@tonic-gate 		/*
17560Sstevel@tonic-gate 		 * Accept can not fail with ENOBUFS. sotpi_create
17570Sstevel@tonic-gate 		 * sleeps waiting for memory until a signal is caught
17580Sstevel@tonic-gate 		 * so return EINTR.
17590Sstevel@tonic-gate 		 */
17600Sstevel@tonic-gate 		freemsg(mp);
17610Sstevel@tonic-gate 		if (error == ENOBUFS)
17620Sstevel@tonic-gate 			error = EINTR;
17630Sstevel@tonic-gate 		goto e_disc_unl;
17640Sstevel@tonic-gate 	}
17650Sstevel@tonic-gate 	nvp = SOTOV(nso);
17668348SEric.Yu@Sun.COM 	nsti = SOTOTPI(nso);
17670Sstevel@tonic-gate 
17680Sstevel@tonic-gate #ifdef DEBUG
17690Sstevel@tonic-gate 	/*
17700Sstevel@tonic-gate 	 * SO_DEBUG is used to trigger the dprint* and eprint* macros thus
17710Sstevel@tonic-gate 	 * it's inherited early to allow debugging of the accept code itself.
17720Sstevel@tonic-gate 	 */
17730Sstevel@tonic-gate 	nso->so_options |= so->so_options & SO_DEBUG;
17740Sstevel@tonic-gate #endif /* DEBUG */
17750Sstevel@tonic-gate 
17760Sstevel@tonic-gate 	/*
17770Sstevel@tonic-gate 	 * Save the SRC address from the T_CONN_IND
17780Sstevel@tonic-gate 	 * for getpeername to work on AF_UNIX and on transports that do not
17790Sstevel@tonic-gate 	 * support TI_GETPEERNAME.
17800Sstevel@tonic-gate 	 *
17810Sstevel@tonic-gate 	 * NOTE: AF_UNIX NUL termination is ensured by the sender's
17820Sstevel@tonic-gate 	 * copyin_name().
17830Sstevel@tonic-gate 	 */
17848348SEric.Yu@Sun.COM 	if (srclen > (t_uscalar_t)nsti->sti_faddr_maxlen) {
17850Sstevel@tonic-gate 		error = EINVAL;
17860Sstevel@tonic-gate 		freemsg(mp);
17870Sstevel@tonic-gate 		eprintsoline(so, error);
17880Sstevel@tonic-gate 		goto disconnect_vp_unlocked;
17890Sstevel@tonic-gate 	}
17908348SEric.Yu@Sun.COM 	nsti->sti_faddr_len = (socklen_t)srclen;
17918348SEric.Yu@Sun.COM 	ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
17928348SEric.Yu@Sun.COM 	bcopy(src, nsti->sti_faddr_sa, srclen);
17938348SEric.Yu@Sun.COM 	nsti->sti_faddr_valid = 1;
17940Sstevel@tonic-gate 
17958778SErik.Nordmark@Sun.COM 	/*
17968778SErik.Nordmark@Sun.COM 	 * Record so_peercred and so_cpid from a cred in the T_CONN_IND.
17978778SErik.Nordmark@Sun.COM 	 */
17980Sstevel@tonic-gate 	if ((DB_REF(mp) > 1) || MBLKSIZE(mp) <
17990Sstevel@tonic-gate 	    (sizeof (struct T_conn_res) + sizeof (intptr_t))) {
18008778SErik.Nordmark@Sun.COM 		cred_t	*cr;
18018778SErik.Nordmark@Sun.COM 		pid_t	cpid;
18028778SErik.Nordmark@Sun.COM 
18038778SErik.Nordmark@Sun.COM 		cr = msg_getcred(mp, &cpid);
18048778SErik.Nordmark@Sun.COM 		if (cr != NULL) {
18050Sstevel@tonic-gate 			crhold(cr);
18060Sstevel@tonic-gate 			nso->so_peercred = cr;
18078778SErik.Nordmark@Sun.COM 			nso->so_cpid = cpid;
18080Sstevel@tonic-gate 		}
18090Sstevel@tonic-gate 		freemsg(mp);
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 		mp = soallocproto1(NULL, sizeof (struct T_conn_res) +
18128965SAnders.Persson@Sun.COM 		    sizeof (intptr_t), 0, _ALLOC_INTR, cr);
18130Sstevel@tonic-gate 		if (mp == NULL) {
18140Sstevel@tonic-gate 			/*
18150Sstevel@tonic-gate 			 * Accept can not fail with ENOBUFS.
18160Sstevel@tonic-gate 			 * A signal was caught so return EINTR.
18170Sstevel@tonic-gate 			 */
18180Sstevel@tonic-gate 			error = EINTR;
18190Sstevel@tonic-gate 			eprintsoline(so, error);
18200Sstevel@tonic-gate 			goto disconnect_vp_unlocked;
18210Sstevel@tonic-gate 		}
18220Sstevel@tonic-gate 		conn_res = (struct T_conn_res *)mp->b_rptr;
18230Sstevel@tonic-gate 	} else {
18248778SErik.Nordmark@Sun.COM 		/*
18258778SErik.Nordmark@Sun.COM 		 * For efficency reasons we use msg_extractcred; no crhold
18268778SErik.Nordmark@Sun.COM 		 * needed since db_credp is cleared (i.e., we move the cred
18278778SErik.Nordmark@Sun.COM 		 * from the message to so_peercred.
18288778SErik.Nordmark@Sun.COM 		 */
18298778SErik.Nordmark@Sun.COM 		nso->so_peercred = msg_extractcred(mp, &nso->so_cpid);
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 		mp->b_rptr = DB_BASE(mp);
18320Sstevel@tonic-gate 		conn_res = (struct T_conn_res *)mp->b_rptr;
18330Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res);
18348965SAnders.Persson@Sun.COM 
18358965SAnders.Persson@Sun.COM 		mblk_setcred(mp, cr, curproc->p_pid);
18360Sstevel@tonic-gate 	}
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate 	/*
18390Sstevel@tonic-gate 	 * New socket must be bound at least in sockfs and, except for AF_INET,
18400Sstevel@tonic-gate 	 * (or AF_INET6) it also has to be bound in the transport provider.
18414379Sja97890 	 * We set the local address in the sonode from the T_OK_ACK of the
18424379Sja97890 	 * T_CONN_RES. For this reason the address we bind to here isn't
18434379Sja97890 	 * important.
18440Sstevel@tonic-gate 	 */
18450Sstevel@tonic-gate 	if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
18460Sstevel@tonic-gate 	    /*CONSTCOND*/
18470Sstevel@tonic-gate 	    nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) {
18480Sstevel@tonic-gate 		/*
18490Sstevel@tonic-gate 		 * Optimization for AF_INET{,6} transports
18500Sstevel@tonic-gate 		 * that can handle a T_CONN_RES without being bound.
18510Sstevel@tonic-gate 		 */
18520Sstevel@tonic-gate 		mutex_enter(&nso->so_lock);
18530Sstevel@tonic-gate 		so_automatic_bind(nso);
18540Sstevel@tonic-gate 		mutex_exit(&nso->so_lock);
18550Sstevel@tonic-gate 	} else {
18560Sstevel@tonic-gate 		/* Perform NULL bind with the transport provider. */
18578348SEric.Yu@Sun.COM 		if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC,
18588348SEric.Yu@Sun.COM 		    cr)) != 0) {
18590Sstevel@tonic-gate 			ASSERT(error != ENOBUFS);
18600Sstevel@tonic-gate 			freemsg(mp);
18610Sstevel@tonic-gate 			eprintsoline(nso, error);
18620Sstevel@tonic-gate 			goto disconnect_vp_unlocked;
18630Sstevel@tonic-gate 		}
18640Sstevel@tonic-gate 	}
18650Sstevel@tonic-gate 
18660Sstevel@tonic-gate 	/*
18670Sstevel@tonic-gate 	 * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES
18680Sstevel@tonic-gate 	 * so that any data arriving on the new socket will cause the
18690Sstevel@tonic-gate 	 * appropriate signals to be delivered for the new socket.
18700Sstevel@tonic-gate 	 *
18710Sstevel@tonic-gate 	 * No other thread (except strsock_proto and strsock_misc)
18720Sstevel@tonic-gate 	 * can access the new socket thus we relax the locking.
18730Sstevel@tonic-gate 	 */
18740Sstevel@tonic-gate 	nso->so_pgrp = so->so_pgrp;
18758348SEric.Yu@Sun.COM 	nso->so_state |= so->so_state & SS_ASYNC;
18768348SEric.Yu@Sun.COM 	nsti->sti_faddr_noxlate = sti->sti_faddr_noxlate;
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate 	if (nso->so_pgrp != 0) {
18798778SErik.Nordmark@Sun.COM 		if ((error = so_set_events(nso, nvp, cr)) != 0) {
18800Sstevel@tonic-gate 			eprintsoline(nso, error);
18810Sstevel@tonic-gate 			error = 0;
18820Sstevel@tonic-gate 			nso->so_pgrp = 0;
18830Sstevel@tonic-gate 		}
18840Sstevel@tonic-gate 	}
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate 	/*
18870Sstevel@tonic-gate 	 * Make note of the socket level options. TCP and IP level options
18880Sstevel@tonic-gate 	 * are already inherited. We could do all this after accept is
18890Sstevel@tonic-gate 	 * successful but doing it here simplifies code and no harm done
18900Sstevel@tonic-gate 	 * for error case.
18910Sstevel@tonic-gate 	 */
18920Sstevel@tonic-gate 	nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE|
18930Sstevel@tonic-gate 	    SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
18940Sstevel@tonic-gate 	    SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
18950Sstevel@tonic-gate 	nso->so_sndbuf = so->so_sndbuf;
18960Sstevel@tonic-gate 	nso->so_rcvbuf = so->so_rcvbuf;
18970Sstevel@tonic-gate 	if (nso->so_options & SO_LINGER)
18980Sstevel@tonic-gate 		nso->so_linger = so->so_linger;
18990Sstevel@tonic-gate 
19008348SEric.Yu@Sun.COM 	/*
19018348SEric.Yu@Sun.COM 	 * Note that the following sti_direct code path should be
19028348SEric.Yu@Sun.COM 	 * removed once we are confident that the direct sockets
19038348SEric.Yu@Sun.COM 	 * do not result in any degradation.
19048348SEric.Yu@Sun.COM 	 */
19058348SEric.Yu@Sun.COM 	if (sti->sti_direct) {
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate 		ASSERT(opt != NULL);
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 		conn_res->OPT_length = optlen;
19100Sstevel@tonic-gate 		conn_res->OPT_offset = MBLKL(mp);
19110Sstevel@tonic-gate 		bcopy(&opt, mp->b_wptr, optlen);
19120Sstevel@tonic-gate 		mp->b_wptr += optlen;
19130Sstevel@tonic-gate 		conn_res->PRIM_type = T_CONN_RES;
19140Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = 0;
19150Sstevel@tonic-gate 		PRIM_type = T_CONN_RES;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 		/* Send down the T_CONN_RES on acceptor STREAM */
19180Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(nso), mp, NULL,
19190Sstevel@tonic-gate 		    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
19200Sstevel@tonic-gate 		if (error) {
19210Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
19220Sstevel@tonic-gate 			so_lock_single(so);
19230Sstevel@tonic-gate 			eprintsoline(so, error);
19240Sstevel@tonic-gate 			goto disconnect_vp;
19250Sstevel@tonic-gate 		}
19260Sstevel@tonic-gate 		mutex_enter(&nso->so_lock);
19270Sstevel@tonic-gate 		error = sowaitprim(nso, T_CONN_RES, T_OK_ACK,
19280Sstevel@tonic-gate 		    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
19290Sstevel@tonic-gate 		if (error) {
19300Sstevel@tonic-gate 			mutex_exit(&nso->so_lock);
19310Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
19320Sstevel@tonic-gate 			so_lock_single(so);
19330Sstevel@tonic-gate 			eprintsoline(so, error);
19340Sstevel@tonic-gate 			goto disconnect_vp;
19350Sstevel@tonic-gate 		}
19360Sstevel@tonic-gate 		if (nso->so_family == AF_INET) {
19370Sstevel@tonic-gate 			sin_t *sin;
19380Sstevel@tonic-gate 
19390Sstevel@tonic-gate 			sin = (sin_t *)(ack_mp->b_rptr +
19400Sstevel@tonic-gate 			    sizeof (struct T_ok_ack));
19418348SEric.Yu@Sun.COM 			bcopy(sin, nsti->sti_laddr_sa, sizeof (sin_t));
19428348SEric.Yu@Sun.COM 			nsti->sti_laddr_len = sizeof (sin_t);
19430Sstevel@tonic-gate 		} else {
19440Sstevel@tonic-gate 			sin6_t *sin6;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 			sin6 = (sin6_t *)(ack_mp->b_rptr +
19470Sstevel@tonic-gate 			    sizeof (struct T_ok_ack));
19488348SEric.Yu@Sun.COM 			bcopy(sin6, nsti->sti_laddr_sa, sizeof (sin6_t));
19498348SEric.Yu@Sun.COM 			nsti->sti_laddr_len = sizeof (sin6_t);
19500Sstevel@tonic-gate 		}
19510Sstevel@tonic-gate 		freemsg(ack_mp);
19520Sstevel@tonic-gate 
19538348SEric.Yu@Sun.COM 		nso->so_state |= SS_ISCONNECTED;
19548348SEric.Yu@Sun.COM 		nso->so_proto_handle = (sock_lower_handle_t)opt;
19558348SEric.Yu@Sun.COM 		nsti->sti_laddr_valid = 1;
19568348SEric.Yu@Sun.COM 
19578348SEric.Yu@Sun.COM 		if (sti->sti_nl7c_flags & NL7C_ENABLED) {
19580Sstevel@tonic-gate 			/*
19591974Sbrutus 			 * A NL7C marked listen()er so the new socket
19601974Sbrutus 			 * inherits the listen()er's NL7C state, except
19611974Sbrutus 			 * for NL7C_POLLIN.
19620Sstevel@tonic-gate 			 *
19631974Sbrutus 			 * Only call NL7C to process the new socket if
19641974Sbrutus 			 * the listen socket allows blocking i/o.
19650Sstevel@tonic-gate 			 */
19668348SEric.Yu@Sun.COM 			nsti->sti_nl7c_flags =
19678348SEric.Yu@Sun.COM 			    sti->sti_nl7c_flags & (~NL7C_POLLIN);
19681974Sbrutus 			if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) {
19691974Sbrutus 				/*
19701974Sbrutus 				 * Nonblocking accept() just make it
19711974Sbrutus 				 * persist to defer processing to the
19721974Sbrutus 				 * read-side syscall (e.g. read).
19731974Sbrutus 				 */
19748348SEric.Yu@Sun.COM 				nsti->sti_nl7c_flags |= NL7C_SOPERSIST;
19751974Sbrutus 			} else if (nl7c_process(nso, B_FALSE)) {
19760Sstevel@tonic-gate 				/*
19770Sstevel@tonic-gate 				 * NL7C has completed processing on the
19780Sstevel@tonic-gate 				 * socket, close the socket and back to
19790Sstevel@tonic-gate 				 * the top to await the next T_CONN_IND.
19800Sstevel@tonic-gate 				 */
19810Sstevel@tonic-gate 				mutex_exit(&nso->so_lock);
19820Sstevel@tonic-gate 				(void) VOP_CLOSE(nvp, 0, 1, (offset_t)0,
19838778SErik.Nordmark@Sun.COM 				    cr, NULL);
19840Sstevel@tonic-gate 				VN_RELE(nvp);
19850Sstevel@tonic-gate 				goto again;
19860Sstevel@tonic-gate 			}
19870Sstevel@tonic-gate 			/* Pass the new socket out */
19880Sstevel@tonic-gate 		}
19890Sstevel@tonic-gate 
19900Sstevel@tonic-gate 		mutex_exit(&nso->so_lock);
19910Sstevel@tonic-gate 
19920Sstevel@tonic-gate 		/*
19932811Sja97890 		 * It's possible, through the use of autopush for example,
19948348SEric.Yu@Sun.COM 		 * that the acceptor stream may not support sti_direct
19958348SEric.Yu@Sun.COM 		 * semantics. If the new socket does not support sti_direct
19962811Sja97890 		 * we issue a _SIOCSOCKFALLBACK to inform the transport
19972811Sja97890 		 * as we would in the I_PUSH case.
19982811Sja97890 		 */
19998348SEric.Yu@Sun.COM 		if (nsti->sti_direct == 0) {
20002811Sja97890 			int	rval;
20012811Sja97890 
20022811Sja97890 			if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
20038778SErik.Nordmark@Sun.COM 			    0, 0, K_TO_K, cr, &rval)) != 0) {
20042811Sja97890 				mutex_enter(&so->so_lock);
20052811Sja97890 				so_lock_single(so);
20062811Sja97890 				eprintsoline(so, error);
20072811Sja97890 				goto disconnect_vp;
20082811Sja97890 			}
20092811Sja97890 		}
20102811Sja97890 
20112811Sja97890 		/*
20120Sstevel@tonic-gate 		 * Pass out new socket.
20130Sstevel@tonic-gate 		 */
20140Sstevel@tonic-gate 		if (nsop != NULL)
20150Sstevel@tonic-gate 			*nsop = nso;
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 		return (0);
20180Sstevel@tonic-gate 	}
20190Sstevel@tonic-gate 
20200Sstevel@tonic-gate 	/*
20210Sstevel@tonic-gate 	 * This is the non-performance case for sockets (e.g. AF_UNIX sockets)
20220Sstevel@tonic-gate 	 * which don't support the FireEngine accept fast-path. It is also
20230Sstevel@tonic-gate 	 * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd
20240Sstevel@tonic-gate 	 * again. Neither sockfs nor TCP attempt to find out if some other
20250Sstevel@tonic-gate 	 * random module has been inserted in between (in which case we
20260Sstevel@tonic-gate 	 * should follow TLI accept behaviour). We blindly assume the worst
20270Sstevel@tonic-gate 	 * case and revert back to old behaviour i.e. TCP will not send us
20280Sstevel@tonic-gate 	 * any option (eager) and the accept should happen on the listener
20290Sstevel@tonic-gate 	 * queue. Any queued T_conn_ind have already got their options removed
20300Sstevel@tonic-gate 	 * by so_sock2_stream() when "sockmod" was I_POP'd.
20310Sstevel@tonic-gate 	 */
20320Sstevel@tonic-gate 	/*
20330Sstevel@tonic-gate 	 * Fill in the {O_}T_CONN_RES before getting SOLOCKED.
20340Sstevel@tonic-gate 	 */
20350Sstevel@tonic-gate 	if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) {
20360Sstevel@tonic-gate #ifdef	_ILP32
20370Sstevel@tonic-gate 		queue_t	*q;
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate 		/*
20400Sstevel@tonic-gate 		 * Find read queue in driver
20410Sstevel@tonic-gate 		 * Can safely do this since we "own" nso/nvp.
20420Sstevel@tonic-gate 		 */
20430Sstevel@tonic-gate 		q = strvp2wq(nvp)->q_next;
20440Sstevel@tonic-gate 		while (SAMESTR(q))
20450Sstevel@tonic-gate 			q = q->q_next;
20460Sstevel@tonic-gate 		q = RD(q);
20470Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = (t_uscalar_t)q;
20480Sstevel@tonic-gate #else
20490Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev);
20500Sstevel@tonic-gate #endif	/* _ILP32 */
20510Sstevel@tonic-gate 		conn_res->PRIM_type = O_T_CONN_RES;
20520Sstevel@tonic-gate 		PRIM_type = O_T_CONN_RES;
20530Sstevel@tonic-gate 	} else {
20548348SEric.Yu@Sun.COM 		conn_res->ACCEPTOR_id = nsti->sti_acceptor_id;
20550Sstevel@tonic-gate 		conn_res->PRIM_type = T_CONN_RES;
20560Sstevel@tonic-gate 		PRIM_type = T_CONN_RES;
20570Sstevel@tonic-gate 	}
20580Sstevel@tonic-gate 	conn_res->SEQ_number = SEQ_number;
20590Sstevel@tonic-gate 	conn_res->OPT_length = 0;
20600Sstevel@tonic-gate 	conn_res->OPT_offset = 0;
20610Sstevel@tonic-gate 
20620Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
20630Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
20640Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
20650Sstevel@tonic-gate 
20660Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL,
20670Sstevel@tonic-gate 	    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
20680Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
20690Sstevel@tonic-gate 	if (error) {
20700Sstevel@tonic-gate 		eprintsoline(so, error);
20710Sstevel@tonic-gate 		goto disconnect_vp;
20720Sstevel@tonic-gate 	}
20734379Sja97890 	error = sowaitprim(so, PRIM_type, T_OK_ACK,
20744379Sja97890 	    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
20750Sstevel@tonic-gate 	if (error) {
20760Sstevel@tonic-gate 		eprintsoline(so, error);
20770Sstevel@tonic-gate 		goto disconnect_vp;
20780Sstevel@tonic-gate 	}
2079*13075SGeorge.Shepherd@Sun.COM 	mutex_exit(&so->so_lock);
20804379Sja97890 	/*
20814379Sja97890 	 * If there is a sin/sin6 appended onto the T_OK_ACK use
20824379Sja97890 	 * that to set the local address. If this is not present
20834379Sja97890 	 * then we zero out the address and don't set the
20848348SEric.Yu@Sun.COM 	 * sti_laddr_valid bit. For AF_UNIX endpoints we copy over
20854678Sja97890 	 * the pathname from the listening socket.
2086*13075SGeorge.Shepherd@Sun.COM 	 * In the case where this is TCP or an AF_UNIX socket the
2087*13075SGeorge.Shepherd@Sun.COM 	 * client side may have queued data or a T_ORDREL in the
2088*13075SGeorge.Shepherd@Sun.COM 	 * transport. Having now sent the T_CONN_RES we may receive
2089*13075SGeorge.Shepherd@Sun.COM 	 * those queued messages at any time. Hold the acceptor
2090*13075SGeorge.Shepherd@Sun.COM 	 * so_lock until its state and laddr are finalized.
20914379Sja97890 	 */
2092*13075SGeorge.Shepherd@Sun.COM 	mutex_enter(&nso->so_lock);
20934379Sja97890 	sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
20944379Sja97890 	if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) &&
20954379Sja97890 	    MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
20964379Sja97890 		ack_mp->b_rptr += sizeof (struct T_ok_ack);
20978348SEric.Yu@Sun.COM 		bcopy(ack_mp->b_rptr, nsti->sti_laddr_sa, sinlen);
20988348SEric.Yu@Sun.COM 		nsti->sti_laddr_len = sinlen;
20998348SEric.Yu@Sun.COM 		nsti->sti_laddr_valid = 1;
21004678Sja97890 	} else if (nso->so_family == AF_UNIX) {
21014678Sja97890 		ASSERT(so->so_family == AF_UNIX);
21028348SEric.Yu@Sun.COM 		nsti->sti_laddr_len = sti->sti_laddr_len;
21038348SEric.Yu@Sun.COM 		ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
21048348SEric.Yu@Sun.COM 		bcopy(sti->sti_laddr_sa, nsti->sti_laddr_sa,
21058348SEric.Yu@Sun.COM 		    nsti->sti_laddr_len);
21068348SEric.Yu@Sun.COM 		nsti->sti_laddr_valid = 1;
21074379Sja97890 	} else {
21088348SEric.Yu@Sun.COM 		nsti->sti_laddr_len = sti->sti_laddr_len;
21098348SEric.Yu@Sun.COM 		ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
21108348SEric.Yu@Sun.COM 		bzero(nsti->sti_laddr_sa, nsti->sti_addr_size);
21118348SEric.Yu@Sun.COM 		nsti->sti_laddr_sa->sa_family = nso->so_family;
21124379Sja97890 	}
2113*13075SGeorge.Shepherd@Sun.COM 	nso->so_state |= SS_ISCONNECTED;
2114*13075SGeorge.Shepherd@Sun.COM 	mutex_exit(&nso->so_lock);
2115*13075SGeorge.Shepherd@Sun.COM 
21164379Sja97890 	freemsg(ack_mp);
21174379Sja97890 
2118*13075SGeorge.Shepherd@Sun.COM 	mutex_enter(&so->so_lock);
21190Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
21200Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
21210Sstevel@tonic-gate 
21220Sstevel@tonic-gate 	/*
21230Sstevel@tonic-gate 	 * Pass out new socket.
21240Sstevel@tonic-gate 	 */
21250Sstevel@tonic-gate 	if (nsop != NULL)
21260Sstevel@tonic-gate 		*nsop = nso;
21270Sstevel@tonic-gate 
21280Sstevel@tonic-gate 	return (0);
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate eproto_disc_unl:
21320Sstevel@tonic-gate 	error = EPROTO;
21330Sstevel@tonic-gate e_disc_unl:
21340Sstevel@tonic-gate 	eprintsoline(so, error);
21350Sstevel@tonic-gate 	goto disconnect_unlocked;
21360Sstevel@tonic-gate 
21370Sstevel@tonic-gate pr_disc_vp_unl:
21380Sstevel@tonic-gate 	eprintsoline(so, error);
21390Sstevel@tonic-gate disconnect_vp_unlocked:
21408778SErik.Nordmark@Sun.COM 	(void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
21410Sstevel@tonic-gate 	VN_RELE(nvp);
21420Sstevel@tonic-gate disconnect_unlocked:
21430Sstevel@tonic-gate 	(void) sodisconnect(so, SEQ_number, 0);
21440Sstevel@tonic-gate 	return (error);
21450Sstevel@tonic-gate 
21460Sstevel@tonic-gate pr_disc_vp:
21470Sstevel@tonic-gate 	eprintsoline(so, error);
21480Sstevel@tonic-gate disconnect_vp:
21490Sstevel@tonic-gate 	(void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
21500Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
21510Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
21528778SErik.Nordmark@Sun.COM 	(void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
21530Sstevel@tonic-gate 	VN_RELE(nvp);
21540Sstevel@tonic-gate 	return (error);
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate conn_bad:	/* Note: SunOS 4/BSD unconditionally returns EINVAL here */
21570Sstevel@tonic-gate 	error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
21580Sstevel@tonic-gate 	    ? EOPNOTSUPP : EINVAL;
21590Sstevel@tonic-gate e_bad:
21600Sstevel@tonic-gate 	eprintsoline(so, error);
21610Sstevel@tonic-gate 	return (error);
21620Sstevel@tonic-gate }
21630Sstevel@tonic-gate 
21640Sstevel@tonic-gate /*
21650Sstevel@tonic-gate  * connect a socket.
21660Sstevel@tonic-gate  *
21670Sstevel@tonic-gate  * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
21680Sstevel@tonic-gate  * unconnect (by specifying a null address).
21690Sstevel@tonic-gate  */
21700Sstevel@tonic-gate int
sotpi_connect(struct sonode * so,struct sockaddr * name,socklen_t namelen,int fflag,int flags,struct cred * cr)21710Sstevel@tonic-gate sotpi_connect(struct sonode *so,
217212643SAnders.Persson@Sun.COM 	struct sockaddr *name,
21730Sstevel@tonic-gate 	socklen_t namelen,
21740Sstevel@tonic-gate 	int fflag,
21758348SEric.Yu@Sun.COM 	int flags,
21768348SEric.Yu@Sun.COM 	struct cred *cr)
21770Sstevel@tonic-gate {
21780Sstevel@tonic-gate 	struct T_conn_req	conn_req;
21790Sstevel@tonic-gate 	int			error = 0;
21800Sstevel@tonic-gate 	mblk_t			*mp;
21810Sstevel@tonic-gate 	void			*src;
21820Sstevel@tonic-gate 	socklen_t		srclen;
21830Sstevel@tonic-gate 	void			*addr;
21840Sstevel@tonic-gate 	socklen_t		addrlen;
21850Sstevel@tonic-gate 	boolean_t		need_unlock;
21868348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
21870Sstevel@tonic-gate 
21880Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
21897240Srh87107 	    (void *)so, (void *)name, namelen, fflag, flags,
21905240Snordmark 	    pr_state(so->so_state, so->so_mode)));
21910Sstevel@tonic-gate 
21920Sstevel@tonic-gate 	/*
21930Sstevel@tonic-gate 	 * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
21940Sstevel@tonic-gate 	 * avoid sleeping for memory with SOLOCKED held.
21958348SEric.Yu@Sun.COM 	 * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen
21960Sstevel@tonic-gate 	 * + sizeof (struct T_opthdr).
21970Sstevel@tonic-gate 	 * (the AF_UNIX so_ux_addr_xlate() does not make the address
21988348SEric.Yu@Sun.COM 	 * exceed sti_faddr_maxlen).
21990Sstevel@tonic-gate 	 */
22000Sstevel@tonic-gate 	mp = soallocproto(sizeof (struct T_conn_req) +
22018778SErik.Nordmark@Sun.COM 	    2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR,
22028778SErik.Nordmark@Sun.COM 	    cr);
22030Sstevel@tonic-gate 	if (mp == NULL) {
22040Sstevel@tonic-gate 		/*
22050Sstevel@tonic-gate 		 * Connect can not fail with ENOBUFS. A signal was
22060Sstevel@tonic-gate 		 * caught so return EINTR.
22070Sstevel@tonic-gate 		 */
22080Sstevel@tonic-gate 		error = EINTR;
22090Sstevel@tonic-gate 		eprintsoline(so, error);
22100Sstevel@tonic-gate 		return (error);
22110Sstevel@tonic-gate 	}
22120Sstevel@tonic-gate 
22130Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
22140Sstevel@tonic-gate 	/*
22155694Sjprakash 	 * Make sure there is a preallocated T_unbind_req message
22165694Sjprakash 	 * before any binding. This message is allocated when the
22175694Sjprakash 	 * socket is created. Since another thread can consume
22185694Sjprakash 	 * so_unbind_mp by the time we return from so_lock_single(),
22195694Sjprakash 	 * we should check the availability of so_unbind_mp after
22205694Sjprakash 	 * we return from so_lock_single().
22210Sstevel@tonic-gate 	 */
22225694Sjprakash 
22235694Sjprakash 	so_lock_single(so);	/* Set SOLOCKED */
22245694Sjprakash 	need_unlock = B_TRUE;
22255694Sjprakash 
22268348SEric.Yu@Sun.COM 	if (sti->sti_unbind_mp == NULL) {
22270Sstevel@tonic-gate 		dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
22280Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
22298348SEric.Yu@Sun.COM 		sti->sti_unbind_mp =
22308778SErik.Nordmark@Sun.COM 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR, cr);
22318348SEric.Yu@Sun.COM 		if (sti->sti_unbind_mp == NULL) {
22320Sstevel@tonic-gate 			error = EINTR;
22330Sstevel@tonic-gate 			goto done;
22340Sstevel@tonic-gate 		}
22350Sstevel@tonic-gate 	}
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	/*
22380Sstevel@tonic-gate 	 * Can't have done a listen before connecting.
22390Sstevel@tonic-gate 	 */
22400Sstevel@tonic-gate 	if (so->so_state & SS_ACCEPTCONN) {
22410Sstevel@tonic-gate 		error = EOPNOTSUPP;
22420Sstevel@tonic-gate 		goto done;
22430Sstevel@tonic-gate 	}
22440Sstevel@tonic-gate 
22450Sstevel@tonic-gate 	/*
22460Sstevel@tonic-gate 	 * Must be bound with the transport
22470Sstevel@tonic-gate 	 */
22480Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
22490Sstevel@tonic-gate 		if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
22500Sstevel@tonic-gate 		    /*CONSTCOND*/
22510Sstevel@tonic-gate 		    so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
22520Sstevel@tonic-gate 			/*
22530Sstevel@tonic-gate 			 * Optimization for AF_INET{,6} transports
22540Sstevel@tonic-gate 			 * that can handle a T_CONN_REQ without being bound.
22550Sstevel@tonic-gate 			 */
22560Sstevel@tonic-gate 			so_automatic_bind(so);
22570Sstevel@tonic-gate 		} else {
22580Sstevel@tonic-gate 			error = sotpi_bind(so, NULL, 0,
22598348SEric.Yu@Sun.COM 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
22600Sstevel@tonic-gate 			if (error)
22610Sstevel@tonic-gate 				goto done;
22620Sstevel@tonic-gate 		}
22630Sstevel@tonic-gate 		ASSERT(so->so_state & SS_ISBOUND);
22640Sstevel@tonic-gate 		flags |= _SOCONNECT_DID_BIND;
22650Sstevel@tonic-gate 	}
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate 	/*
22680Sstevel@tonic-gate 	 * Handle a connect to a name parameter of type AF_UNSPEC like a
22690Sstevel@tonic-gate 	 * connect to a null address. This is the portable method to
22700Sstevel@tonic-gate 	 * unconnect a socket.
22710Sstevel@tonic-gate 	 */
22720Sstevel@tonic-gate 	if ((namelen >= sizeof (sa_family_t)) &&
22730Sstevel@tonic-gate 	    (name->sa_family == AF_UNSPEC)) {
22740Sstevel@tonic-gate 		name = NULL;
22750Sstevel@tonic-gate 		namelen = 0;
22760Sstevel@tonic-gate 	}
22770Sstevel@tonic-gate 
22780Sstevel@tonic-gate 	/*
22790Sstevel@tonic-gate 	 * Check that we are not already connected.
22800Sstevel@tonic-gate 	 * A connection-oriented socket cannot be reconnected.
22810Sstevel@tonic-gate 	 * A connected connection-less socket can be
22820Sstevel@tonic-gate 	 * - connected to a different address by a subsequent connect
22830Sstevel@tonic-gate 	 * - "unconnected" by a connect to the NULL address
22840Sstevel@tonic-gate 	 */
22850Sstevel@tonic-gate 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
22860Sstevel@tonic-gate 		ASSERT(!(flags & _SOCONNECT_DID_BIND));
22870Sstevel@tonic-gate 		if (so->so_mode & SM_CONNREQUIRED) {
22880Sstevel@tonic-gate 			/* Connection-oriented socket */
22890Sstevel@tonic-gate 			error = so->so_state & SS_ISCONNECTED ?
22900Sstevel@tonic-gate 			    EISCONN : EALREADY;
22910Sstevel@tonic-gate 			goto done;
22920Sstevel@tonic-gate 		}
22930Sstevel@tonic-gate 		/* Connection-less socket */
22940Sstevel@tonic-gate 		if (name == NULL) {
22950Sstevel@tonic-gate 			/*
22960Sstevel@tonic-gate 			 * Remove the connected state and clear SO_DGRAM_ERRIND
22970Sstevel@tonic-gate 			 * since it was set when the socket was connected.
22980Sstevel@tonic-gate 			 * If this is UDP also send down a T_DISCON_REQ.
22990Sstevel@tonic-gate 			 */
23000Sstevel@tonic-gate 			int val;
23010Sstevel@tonic-gate 
23020Sstevel@tonic-gate 			if ((so->so_family == AF_INET ||
23035240Snordmark 			    so->so_family == AF_INET6) &&
23040Sstevel@tonic-gate 			    (so->so_type == SOCK_DGRAM ||
23055240Snordmark 			    so->so_type == SOCK_RAW) &&
23060Sstevel@tonic-gate 			    /*CONSTCOND*/
23070Sstevel@tonic-gate 			    !soconnect_tpi_udp) {
23080Sstevel@tonic-gate 				/* XXX What about implicitly unbinding here? */
23090Sstevel@tonic-gate 				error = sodisconnect(so, -1,
23105240Snordmark 				    _SODISCONNECT_LOCK_HELD);
23110Sstevel@tonic-gate 			} else {
23120Sstevel@tonic-gate 				so->so_state &=
23138348SEric.Yu@Sun.COM 				    ~(SS_ISCONNECTED | SS_ISCONNECTING);
23148348SEric.Yu@Sun.COM 				sti->sti_faddr_valid = 0;
23158348SEric.Yu@Sun.COM 				sti->sti_faddr_len = 0;
23160Sstevel@tonic-gate 			}
23170Sstevel@tonic-gate 
23188348SEric.Yu@Sun.COM 			/* Remove SOLOCKED since setsockopt will grab it */
23190Sstevel@tonic-gate 			so_unlock_single(so, SOLOCKED);
23200Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
23210Sstevel@tonic-gate 
23220Sstevel@tonic-gate 			val = 0;
23238348SEric.Yu@Sun.COM 			(void) sotpi_setsockopt(so, SOL_SOCKET,
23248348SEric.Yu@Sun.COM 			    SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val),
23258348SEric.Yu@Sun.COM 			    cr);
23260Sstevel@tonic-gate 
23270Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
23280Sstevel@tonic-gate 			so_lock_single(so);	/* Set SOLOCKED */
23290Sstevel@tonic-gate 			goto done;
23300Sstevel@tonic-gate 		}
23310Sstevel@tonic-gate 	}
23320Sstevel@tonic-gate 	ASSERT(so->so_state & SS_ISBOUND);
23330Sstevel@tonic-gate 
23340Sstevel@tonic-gate 	if (name == NULL || namelen == 0) {
23350Sstevel@tonic-gate 		error = EINVAL;
23360Sstevel@tonic-gate 		goto done;
23370Sstevel@tonic-gate 	}
23380Sstevel@tonic-gate 	/*
23398348SEric.Yu@Sun.COM 	 * Mark the socket if sti_faddr_sa represents the transport level
23400Sstevel@tonic-gate 	 * address.
23410Sstevel@tonic-gate 	 */
23420Sstevel@tonic-gate 	if (flags & _SOCONNECT_NOXLATE) {
23430Sstevel@tonic-gate 		struct sockaddr_ux	*soaddr_ux;
23440Sstevel@tonic-gate 
23450Sstevel@tonic-gate 		ASSERT(so->so_family == AF_UNIX);
23460Sstevel@tonic-gate 		if (namelen != sizeof (struct sockaddr_ux)) {
23470Sstevel@tonic-gate 			error = EINVAL;
23480Sstevel@tonic-gate 			goto done;
23490Sstevel@tonic-gate 		}
23500Sstevel@tonic-gate 		soaddr_ux = (struct sockaddr_ux *)name;
23510Sstevel@tonic-gate 		name = (struct sockaddr *)&soaddr_ux->sou_addr;
23520Sstevel@tonic-gate 		namelen = sizeof (soaddr_ux->sou_addr);
23538348SEric.Yu@Sun.COM 		sti->sti_faddr_noxlate = 1;
23540Sstevel@tonic-gate 	}
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate 	/*
23570Sstevel@tonic-gate 	 * Length and family checks.
23580Sstevel@tonic-gate 	 */
23590Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
23600Sstevel@tonic-gate 	if (error)
23610Sstevel@tonic-gate 		goto bad;
23620Sstevel@tonic-gate 
23630Sstevel@tonic-gate 	/*
23640Sstevel@tonic-gate 	 * Save foreign address. Needed for AF_UNIX as well as
23650Sstevel@tonic-gate 	 * transport providers that do not support TI_GETPEERNAME.
23660Sstevel@tonic-gate 	 * Also used for cached foreign address for TCP and UDP.
23670Sstevel@tonic-gate 	 */
23688348SEric.Yu@Sun.COM 	if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) {
23690Sstevel@tonic-gate 		error = EINVAL;
23700Sstevel@tonic-gate 		goto done;
23710Sstevel@tonic-gate 	}
23728348SEric.Yu@Sun.COM 	sti->sti_faddr_len = (socklen_t)namelen;
23738348SEric.Yu@Sun.COM 	ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
23748348SEric.Yu@Sun.COM 	bcopy(name, sti->sti_faddr_sa, namelen);
23758348SEric.Yu@Sun.COM 	sti->sti_faddr_valid = 1;
23760Sstevel@tonic-gate 
23770Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
23788348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate) {
23790Sstevel@tonic-gate 			/*
23800Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
23810Sstevel@tonic-gate 			 * pass any (transport internal) source address.
23820Sstevel@tonic-gate 			 */
23838348SEric.Yu@Sun.COM 			addr = sti->sti_faddr_sa;
23848348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sti->sti_faddr_len;
23850Sstevel@tonic-gate 			src = NULL;
23860Sstevel@tonic-gate 			srclen = 0;
23870Sstevel@tonic-gate 		} else {
23880Sstevel@tonic-gate 			/*
23890Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
23900Sstevel@tonic-gate 			 * and translate the remote address.
23918348SEric.Yu@Sun.COM 			 * Holding so_lock thus sti_laddr_sa can not change.
23920Sstevel@tonic-gate 			 */
23938348SEric.Yu@Sun.COM 			src = sti->sti_laddr_sa;
23948348SEric.Yu@Sun.COM 			srclen = (t_uscalar_t)sti->sti_laddr_len;
23950Sstevel@tonic-gate 			dprintso(so, 1,
23965240Snordmark 			    ("sotpi_connect UNIX: srclen %d, src %p\n",
23975240Snordmark 			    srclen, src));
23980Sstevel@tonic-gate 			error = so_ux_addr_xlate(so,
23998348SEric.Yu@Sun.COM 			    sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len,
24005240Snordmark 			    (flags & _SOCONNECT_XPG4_2),
24015240Snordmark 			    &addr, &addrlen);
24020Sstevel@tonic-gate 			if (error)
24030Sstevel@tonic-gate 				goto bad;
24040Sstevel@tonic-gate 		}
24050Sstevel@tonic-gate 	} else {
24068348SEric.Yu@Sun.COM 		addr = sti->sti_faddr_sa;
24078348SEric.Yu@Sun.COM 		addrlen = (t_uscalar_t)sti->sti_faddr_len;
24080Sstevel@tonic-gate 		src = NULL;
24090Sstevel@tonic-gate 		srclen = 0;
24100Sstevel@tonic-gate 	}
24110Sstevel@tonic-gate 	/*
24120Sstevel@tonic-gate 	 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
24130Sstevel@tonic-gate 	 * option which asks the transport provider to send T_UDERR_IND
24140Sstevel@tonic-gate 	 * messages. These T_UDERR_IND messages are used to return connected
24150Sstevel@tonic-gate 	 * style errors (e.g. ECONNRESET) for connected datagram sockets.
24160Sstevel@tonic-gate 	 *
24170Sstevel@tonic-gate 	 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
24180Sstevel@tonic-gate 	 * we send down a T_CONN_REQ. This is needed to let the
24190Sstevel@tonic-gate 	 * transport assign a local address that is consistent with
24200Sstevel@tonic-gate 	 * the remote address. Applications depend on a getsockname()
24210Sstevel@tonic-gate 	 * after a connect() to retrieve the "source" IP address for
24220Sstevel@tonic-gate 	 * the connected socket.  Invalidate the cached local address
24230Sstevel@tonic-gate 	 * to force getsockname() to enquire of the transport.
24240Sstevel@tonic-gate 	 */
24250Sstevel@tonic-gate 	if (!(so->so_mode & SM_CONNREQUIRED)) {
24260Sstevel@tonic-gate 		/*
24270Sstevel@tonic-gate 		 * Datagram socket.
24280Sstevel@tonic-gate 		 */
24290Sstevel@tonic-gate 		int32_t val;
24300Sstevel@tonic-gate 
24310Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
24320Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
24330Sstevel@tonic-gate 
24340Sstevel@tonic-gate 		val = 1;
24350Sstevel@tonic-gate 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
24368348SEric.Yu@Sun.COM 		    &val, (t_uscalar_t)sizeof (val), cr);
24370Sstevel@tonic-gate 
24380Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24390Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
24400Sstevel@tonic-gate 		if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
24410Sstevel@tonic-gate 		    (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
24420Sstevel@tonic-gate 		    soconnect_tpi_udp) {
24430Sstevel@tonic-gate 			soisconnected(so);
24440Sstevel@tonic-gate 			goto done;
24450Sstevel@tonic-gate 		}
24460Sstevel@tonic-gate 		/*
24470Sstevel@tonic-gate 		 * Send down T_CONN_REQ etc.
24480Sstevel@tonic-gate 		 * Clear fflag to avoid returning EWOULDBLOCK.
24490Sstevel@tonic-gate 		 */
24500Sstevel@tonic-gate 		fflag = 0;
24510Sstevel@tonic-gate 		ASSERT(so->so_family != AF_UNIX);
24528348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
24538348SEric.Yu@Sun.COM 	} else if (sti->sti_laddr_len != 0) {
24540Sstevel@tonic-gate 		/*
24550Sstevel@tonic-gate 		 * If the local address or port was "any" then it may be
24560Sstevel@tonic-gate 		 * changed by the transport as a result of the
24570Sstevel@tonic-gate 		 * connect.  Invalidate the cached version if we have one.
24580Sstevel@tonic-gate 		 */
24590Sstevel@tonic-gate 		switch (so->so_family) {
24600Sstevel@tonic-gate 		case AF_INET:
24618348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t));
24628348SEric.Yu@Sun.COM 			if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr ==
24630Sstevel@tonic-gate 			    INADDR_ANY ||
24648348SEric.Yu@Sun.COM 			    ((sin_t *)sti->sti_laddr_sa)->sin_port == 0)
24658348SEric.Yu@Sun.COM 				sti->sti_laddr_valid = 0;
24660Sstevel@tonic-gate 			break;
24670Sstevel@tonic-gate 
24680Sstevel@tonic-gate 		case AF_INET6:
24698348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_len ==
24708348SEric.Yu@Sun.COM 			    (socklen_t)sizeof (sin6_t));
24710Sstevel@tonic-gate 			if (IN6_IS_ADDR_UNSPECIFIED(
24728348SEric.Yu@Sun.COM 			    &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) ||
24730Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED_ANY(
24748348SEric.Yu@Sun.COM 			    &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) ||
24758348SEric.Yu@Sun.COM 			    ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0)
24768348SEric.Yu@Sun.COM 				sti->sti_laddr_valid = 0;
24770Sstevel@tonic-gate 			break;
24780Sstevel@tonic-gate 
24790Sstevel@tonic-gate 		default:
24800Sstevel@tonic-gate 			break;
24810Sstevel@tonic-gate 		}
24820Sstevel@tonic-gate 	}
24830Sstevel@tonic-gate 
24840Sstevel@tonic-gate 	/*
24850Sstevel@tonic-gate 	 * Check for failure of an earlier call
24860Sstevel@tonic-gate 	 */
24870Sstevel@tonic-gate 	if (so->so_error != 0)
24880Sstevel@tonic-gate 		goto so_bad;
24890Sstevel@tonic-gate 
24900Sstevel@tonic-gate 	/*
24910Sstevel@tonic-gate 	 * Send down T_CONN_REQ. Message was allocated above.
24920Sstevel@tonic-gate 	 */
24930Sstevel@tonic-gate 	conn_req.PRIM_type = T_CONN_REQ;
24940Sstevel@tonic-gate 	conn_req.DEST_length = addrlen;
24950Sstevel@tonic-gate 	conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
24960Sstevel@tonic-gate 	if (srclen == 0) {
24970Sstevel@tonic-gate 		conn_req.OPT_length = 0;
24980Sstevel@tonic-gate 		conn_req.OPT_offset = 0;
24990Sstevel@tonic-gate 		soappendmsg(mp, &conn_req, sizeof (conn_req));
25000Sstevel@tonic-gate 		soappendmsg(mp, addr, addrlen);
25010Sstevel@tonic-gate 	} else {
25020Sstevel@tonic-gate 		/*
25030Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
25040Sstevel@tonic-gate 		 * address option.
25050Sstevel@tonic-gate 		 */
25060Sstevel@tonic-gate 		struct T_opthdr toh;
25070Sstevel@tonic-gate 
25080Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
25090Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
25100Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
25110Sstevel@tonic-gate 		toh.status = 0;
25120Sstevel@tonic-gate 		conn_req.OPT_length =
25135240Snordmark 		    (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
25140Sstevel@tonic-gate 		conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
25155240Snordmark 		    _TPI_ALIGN_TOPT(addrlen));
25160Sstevel@tonic-gate 
25170Sstevel@tonic-gate 		soappendmsg(mp, &conn_req, sizeof (conn_req));
25180Sstevel@tonic-gate 		soappendmsg(mp, addr, addrlen);
25190Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
25200Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
25210Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
25220Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
25230Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
25240Sstevel@tonic-gate 	}
25250Sstevel@tonic-gate 	/*
25260Sstevel@tonic-gate 	 * Set SS_ISCONNECTING before sending down the T_CONN_REQ
25270Sstevel@tonic-gate 	 * in order to have the right state when the T_CONN_CON shows up.
25280Sstevel@tonic-gate 	 */
25290Sstevel@tonic-gate 	soisconnecting(so);
25300Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
25310Sstevel@tonic-gate 
253211861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
25330Sstevel@tonic-gate 		audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
25365240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
25370Sstevel@tonic-gate 	mp = NULL;
25380Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
25390Sstevel@tonic-gate 	if (error != 0)
25400Sstevel@tonic-gate 		goto bad;
25410Sstevel@tonic-gate 
25420Sstevel@tonic-gate 	if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
25430Sstevel@tonic-gate 		goto bad;
25440Sstevel@tonic-gate 
25450Sstevel@tonic-gate 	/* Allow other threads to access the socket */
25460Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
25470Sstevel@tonic-gate 	need_unlock = B_FALSE;
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate 	/*
25500Sstevel@tonic-gate 	 * Wait until we get a T_CONN_CON or an error
25510Sstevel@tonic-gate 	 */
25520Sstevel@tonic-gate 	if ((error = sowaitconnected(so, fflag, 0)) != 0) {
25530Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
25540Sstevel@tonic-gate 		need_unlock = B_TRUE;
25550Sstevel@tonic-gate 	}
25560Sstevel@tonic-gate 
25570Sstevel@tonic-gate done:
25580Sstevel@tonic-gate 	freemsg(mp);
25590Sstevel@tonic-gate 	switch (error) {
25600Sstevel@tonic-gate 	case EINPROGRESS:
25610Sstevel@tonic-gate 	case EALREADY:
25620Sstevel@tonic-gate 	case EISCONN:
25630Sstevel@tonic-gate 	case EINTR:
25640Sstevel@tonic-gate 		/* Non-fatal errors */
25658348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
25660Sstevel@tonic-gate 		/* FALLTHRU */
25670Sstevel@tonic-gate 	case 0:
25680Sstevel@tonic-gate 		break;
25690Sstevel@tonic-gate 	default:
25700Sstevel@tonic-gate 		ASSERT(need_unlock);
25710Sstevel@tonic-gate 		/*
25720Sstevel@tonic-gate 		 * Fatal errors: clear SS_ISCONNECTING in case it was set,
25730Sstevel@tonic-gate 		 * and invalidate local-address cache
25740Sstevel@tonic-gate 		 */
25758348SEric.Yu@Sun.COM 		so->so_state &= ~SS_ISCONNECTING;
25768348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
25770Sstevel@tonic-gate 		/* A discon_ind might have already unbound us */
25780Sstevel@tonic-gate 		if ((flags & _SOCONNECT_DID_BIND) &&
25790Sstevel@tonic-gate 		    (so->so_state & SS_ISBOUND)) {
25800Sstevel@tonic-gate 			int err;
25810Sstevel@tonic-gate 
25820Sstevel@tonic-gate 			err = sotpi_unbind(so, 0);
25830Sstevel@tonic-gate 			/* LINTED - statement has no conseq */
25840Sstevel@tonic-gate 			if (err) {
25850Sstevel@tonic-gate 				eprintsoline(so, err);
25860Sstevel@tonic-gate 			}
25870Sstevel@tonic-gate 		}
25880Sstevel@tonic-gate 		break;
25890Sstevel@tonic-gate 	}
25900Sstevel@tonic-gate 	if (need_unlock)
25910Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
25920Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
25930Sstevel@tonic-gate 	return (error);
25940Sstevel@tonic-gate 
25958348SEric.Yu@Sun.COM so_bad:	error = sogeterr(so, B_TRUE);
25960Sstevel@tonic-gate bad:	eprintsoline(so, error);
25970Sstevel@tonic-gate 	goto done;
25980Sstevel@tonic-gate }
25990Sstevel@tonic-gate 
26008348SEric.Yu@Sun.COM /* ARGSUSED */
26010Sstevel@tonic-gate int
sotpi_shutdown(struct sonode * so,int how,struct cred * cr)26028348SEric.Yu@Sun.COM sotpi_shutdown(struct sonode *so, int how, struct cred *cr)
26030Sstevel@tonic-gate {
26040Sstevel@tonic-gate 	struct T_ordrel_req	ordrel_req;
26050Sstevel@tonic-gate 	mblk_t			*mp;
26060Sstevel@tonic-gate 	uint_t			old_state, state_change;
26070Sstevel@tonic-gate 	int			error = 0;
26088348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
26090Sstevel@tonic-gate 
26100Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
26117240Srh87107 	    (void *)so, how, pr_state(so->so_state, so->so_mode)));
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
26140Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
26150Sstevel@tonic-gate 
26160Sstevel@tonic-gate 	/*
26170Sstevel@tonic-gate 	 * SunOS 4.X has no check for datagram sockets.
26180Sstevel@tonic-gate 	 * 5.X checks that it is connected (ENOTCONN)
26190Sstevel@tonic-gate 	 * X/Open requires that we check the connected state.
26200Sstevel@tonic-gate 	 */
26210Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
26220Sstevel@tonic-gate 		if (!xnet_skip_checks) {
26230Sstevel@tonic-gate 			error = ENOTCONN;
26240Sstevel@tonic-gate 			if (xnet_check_print) {
26250Sstevel@tonic-gate 				printf("sockfs: X/Open shutdown check "
26265240Snordmark 				    "caused ENOTCONN\n");
26270Sstevel@tonic-gate 			}
26280Sstevel@tonic-gate 		}
26290Sstevel@tonic-gate 		goto done;
26300Sstevel@tonic-gate 	}
26310Sstevel@tonic-gate 	/*
26320Sstevel@tonic-gate 	 * Record the current state and then perform any state changes.
26330Sstevel@tonic-gate 	 * Then use the difference between the old and new states to
26340Sstevel@tonic-gate 	 * determine which messages need to be sent.
26350Sstevel@tonic-gate 	 * This prevents e.g. duplicate T_ORDREL_REQ when there are
26360Sstevel@tonic-gate 	 * duplicate calls to shutdown().
26370Sstevel@tonic-gate 	 */
26380Sstevel@tonic-gate 	old_state = so->so_state;
26390Sstevel@tonic-gate 
26400Sstevel@tonic-gate 	switch (how) {
26410Sstevel@tonic-gate 	case 0:
26420Sstevel@tonic-gate 		socantrcvmore(so);
26430Sstevel@tonic-gate 		break;
26440Sstevel@tonic-gate 	case 1:
26450Sstevel@tonic-gate 		socantsendmore(so);
26460Sstevel@tonic-gate 		break;
26470Sstevel@tonic-gate 	case 2:
26480Sstevel@tonic-gate 		socantsendmore(so);
26490Sstevel@tonic-gate 		socantrcvmore(so);
26500Sstevel@tonic-gate 		break;
26510Sstevel@tonic-gate 	default:
26520Sstevel@tonic-gate 		error = EINVAL;
26530Sstevel@tonic-gate 		goto done;
26540Sstevel@tonic-gate 	}
26550Sstevel@tonic-gate 
26560Sstevel@tonic-gate 	/*
26570Sstevel@tonic-gate 	 * Assumes that the SS_CANT* flags are never cleared in the above code.
26580Sstevel@tonic-gate 	 */
26590Sstevel@tonic-gate 	state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
26605240Snordmark 	    (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
26610Sstevel@tonic-gate 	ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
26620Sstevel@tonic-gate 
26630Sstevel@tonic-gate 	switch (state_change) {
26640Sstevel@tonic-gate 	case 0:
26650Sstevel@tonic-gate 		dprintso(so, 1,
26660Sstevel@tonic-gate 		    ("sotpi_shutdown: nothing to send in state 0x%x\n",
26670Sstevel@tonic-gate 		    so->so_state));
26680Sstevel@tonic-gate 		goto done;
26690Sstevel@tonic-gate 
26700Sstevel@tonic-gate 	case SS_CANTRCVMORE:
26710Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
26720Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
26730Sstevel@tonic-gate 		/*
26740Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
26750Sstevel@tonic-gate 		 * pollwakeups, and signals.
26760Sstevel@tonic-gate 		 */
26770Sstevel@tonic-gate 		/*
26780Sstevel@tonic-gate 		 * Get the read lock before flushing data to avoid problems
26790Sstevel@tonic-gate 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
26800Sstevel@tonic-gate 		 */
26810Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
26820Sstevel@tonic-gate 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
26830Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
26840Sstevel@tonic-gate 
26850Sstevel@tonic-gate 		/* Flush read side queue */
26860Sstevel@tonic-gate 		strflushrq(SOTOV(so), FLUSHALL);
26870Sstevel@tonic-gate 
26880Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
26890Sstevel@tonic-gate 		so_unlock_read(so);		/* Clear SOREADLOCKED */
26900Sstevel@tonic-gate 		break;
26910Sstevel@tonic-gate 
26920Sstevel@tonic-gate 	case SS_CANTSENDMORE:
26930Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
26940Sstevel@tonic-gate 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
26950Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
26960Sstevel@tonic-gate 		break;
26970Sstevel@tonic-gate 
26980Sstevel@tonic-gate 	case SS_CANTSENDMORE|SS_CANTRCVMORE:
26990Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27000Sstevel@tonic-gate 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
27010Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
27020Sstevel@tonic-gate 		/*
27030Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
27040Sstevel@tonic-gate 		 * pollwakeups, and signals.
27050Sstevel@tonic-gate 		 */
27060Sstevel@tonic-gate 		/*
27070Sstevel@tonic-gate 		 * Get the read lock before flushing data to avoid problems
27080Sstevel@tonic-gate 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
27090Sstevel@tonic-gate 		 */
27100Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27110Sstevel@tonic-gate 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
27120Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27130Sstevel@tonic-gate 
27140Sstevel@tonic-gate 		/* Flush read side queue */
27150Sstevel@tonic-gate 		strflushrq(SOTOV(so), FLUSHALL);
27160Sstevel@tonic-gate 
27170Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27180Sstevel@tonic-gate 		so_unlock_read(so);		/* Clear SOREADLOCKED */
27190Sstevel@tonic-gate 		break;
27200Sstevel@tonic-gate 	}
27210Sstevel@tonic-gate 
27220Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
27230Sstevel@tonic-gate 
27240Sstevel@tonic-gate 	/*
27250Sstevel@tonic-gate 	 * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
27260Sstevel@tonic-gate 	 * was set due to this call and the new state has both of them set:
27270Sstevel@tonic-gate 	 *	Send the AF_UNIX close indication
27280Sstevel@tonic-gate 	 *	For T_COTS send a discon_ind
27290Sstevel@tonic-gate 	 *
27300Sstevel@tonic-gate 	 * If cantsend was set due to this call:
27310Sstevel@tonic-gate 	 *	For T_COTSORD send an ordrel_ind
27320Sstevel@tonic-gate 	 *
27330Sstevel@tonic-gate 	 * Note that for T_CLTS there is no message sent here.
27340Sstevel@tonic-gate 	 */
27350Sstevel@tonic-gate 	if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
27360Sstevel@tonic-gate 	    (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
27370Sstevel@tonic-gate 		/*
27380Sstevel@tonic-gate 		 * For SunOS 4.X compatibility we tell the other end
27390Sstevel@tonic-gate 		 * that we are unable to receive at this point.
27400Sstevel@tonic-gate 		 */
27418348SEric.Yu@Sun.COM 		if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS)
27420Sstevel@tonic-gate 			so_unix_close(so);
27430Sstevel@tonic-gate 
27448348SEric.Yu@Sun.COM 		if (sti->sti_serv_type == T_COTS)
27450Sstevel@tonic-gate 			error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
27460Sstevel@tonic-gate 	}
27470Sstevel@tonic-gate 	if ((state_change & SS_CANTSENDMORE) &&
27488348SEric.Yu@Sun.COM 	    (sti->sti_serv_type == T_COTS_ORD)) {
27490Sstevel@tonic-gate 		/* Send an orderly release */
27500Sstevel@tonic-gate 		ordrel_req.PRIM_type = T_ORDREL_REQ;
27510Sstevel@tonic-gate 
27520Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27530Sstevel@tonic-gate 		mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
27548778SErik.Nordmark@Sun.COM 		    0, _ALLOC_SLEEP, cr);
27550Sstevel@tonic-gate 		/*
27560Sstevel@tonic-gate 		 * Send down the T_ORDREL_REQ even if there is flow control.
27570Sstevel@tonic-gate 		 * This prevents shutdown from blocking.
27580Sstevel@tonic-gate 		 * Note that there is no T_OK_ACK for ordrel_req.
27590Sstevel@tonic-gate 		 */
27600Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
27615240Snordmark 		    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
27620Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27630Sstevel@tonic-gate 		if (error) {
27640Sstevel@tonic-gate 			eprintsoline(so, error);
27650Sstevel@tonic-gate 			goto done;
27660Sstevel@tonic-gate 		}
27670Sstevel@tonic-gate 	}
27680Sstevel@tonic-gate 
27690Sstevel@tonic-gate done:
27700Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
27710Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
27720Sstevel@tonic-gate 	return (error);
27730Sstevel@tonic-gate }
27740Sstevel@tonic-gate 
27750Sstevel@tonic-gate /*
27760Sstevel@tonic-gate  * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
27770Sstevel@tonic-gate  * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
27780Sstevel@tonic-gate  * that we have closed.
27790Sstevel@tonic-gate  * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
27800Sstevel@tonic-gate  * T_UNITDATA_REQ containing the same option.
27810Sstevel@tonic-gate  *
27820Sstevel@tonic-gate  * For SOCK_DGRAM half-connections (somebody connected to this end
27830Sstevel@tonic-gate  * but this end is not connect) we don't know where to send any
27840Sstevel@tonic-gate  * SO_UNIX_CLOSE.
27850Sstevel@tonic-gate  *
27860Sstevel@tonic-gate  * We have to ignore stream head errors just in case there has been
27870Sstevel@tonic-gate  * a shutdown(output).
27880Sstevel@tonic-gate  * Ignore any flow control to try to get the message more quickly to the peer.
27890Sstevel@tonic-gate  * While locally ignoring flow control solves the problem when there
27900Sstevel@tonic-gate  * is only the loopback transport on the stream it would not provide
27910Sstevel@tonic-gate  * the correct AF_UNIX socket semantics when one or more modules have
27920Sstevel@tonic-gate  * been pushed.
27930Sstevel@tonic-gate  */
27940Sstevel@tonic-gate void
so_unix_close(struct sonode * so)27950Sstevel@tonic-gate so_unix_close(struct sonode *so)
27960Sstevel@tonic-gate {
27970Sstevel@tonic-gate 	int		error;
27980Sstevel@tonic-gate 	struct T_opthdr	toh;
27990Sstevel@tonic-gate 	mblk_t		*mp;
28008348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
28010Sstevel@tonic-gate 
28020Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
28030Sstevel@tonic-gate 
28040Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
28050Sstevel@tonic-gate 
28060Sstevel@tonic-gate 	if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
28070Sstevel@tonic-gate 	    (SS_ISCONNECTED|SS_ISBOUND))
28080Sstevel@tonic-gate 		return;
28090Sstevel@tonic-gate 
28100Sstevel@tonic-gate 	dprintso(so, 1, ("so_unix_close(%p) %s\n",
28117240Srh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
28120Sstevel@tonic-gate 
28130Sstevel@tonic-gate 	toh.level = SOL_SOCKET;
28140Sstevel@tonic-gate 	toh.name = SO_UNIX_CLOSE;
28150Sstevel@tonic-gate 
28160Sstevel@tonic-gate 	/* zero length + header */
28170Sstevel@tonic-gate 	toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
28180Sstevel@tonic-gate 	toh.status = 0;
28190Sstevel@tonic-gate 
28200Sstevel@tonic-gate 	if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
28210Sstevel@tonic-gate 		struct T_optdata_req tdr;
28220Sstevel@tonic-gate 
28230Sstevel@tonic-gate 		tdr.PRIM_type = T_OPTDATA_REQ;
28240Sstevel@tonic-gate 		tdr.DATA_flag = 0;
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 		tdr.OPT_length = (t_scalar_t)sizeof (toh);
28270Sstevel@tonic-gate 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
28280Sstevel@tonic-gate 
28290Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
28300Sstevel@tonic-gate 		mp = soallocproto2(&tdr, sizeof (tdr),
28318778SErik.Nordmark@Sun.COM 		    &toh, sizeof (toh), 0, _ALLOC_SLEEP, CRED());
28320Sstevel@tonic-gate 	} else {
28330Sstevel@tonic-gate 		struct T_unitdata_req	tudr;
28340Sstevel@tonic-gate 		void			*addr;
28350Sstevel@tonic-gate 		socklen_t		addrlen;
28360Sstevel@tonic-gate 		void			*src;
28370Sstevel@tonic-gate 		socklen_t		srclen;
28380Sstevel@tonic-gate 		struct T_opthdr		toh2;
28390Sstevel@tonic-gate 		t_scalar_t		size;
28400Sstevel@tonic-gate 
28410Sstevel@tonic-gate 		/* Connecteded DGRAM socket */
28420Sstevel@tonic-gate 
28430Sstevel@tonic-gate 		/*
28440Sstevel@tonic-gate 		 * For AF_UNIX the destination address is translated to
28450Sstevel@tonic-gate 		 * an internal name and the source address is passed as
28460Sstevel@tonic-gate 		 * an option.
28470Sstevel@tonic-gate 		 */
28480Sstevel@tonic-gate 		/*
28490Sstevel@tonic-gate 		 * Length and family checks.
28500Sstevel@tonic-gate 		 */
28518348SEric.Yu@Sun.COM 		error = so_addr_verify(so, sti->sti_faddr_sa,
28528348SEric.Yu@Sun.COM 		    (t_uscalar_t)sti->sti_faddr_len);
28530Sstevel@tonic-gate 		if (error) {
28540Sstevel@tonic-gate 			eprintsoline(so, error);
28550Sstevel@tonic-gate 			return;
28560Sstevel@tonic-gate 		}
28578348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate) {
28580Sstevel@tonic-gate 			/*
28590Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
28600Sstevel@tonic-gate 			 * pass any (transport internal) source address.
28610Sstevel@tonic-gate 			 */
28628348SEric.Yu@Sun.COM 			addr = sti->sti_faddr_sa;
28638348SEric.Yu@Sun.COM 			addrlen = (t_uscalar_t)sti->sti_faddr_len;
28640Sstevel@tonic-gate 			src = NULL;
28650Sstevel@tonic-gate 			srclen = 0;
28660Sstevel@tonic-gate 		} else {
28670Sstevel@tonic-gate 			/*
28680Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
28690Sstevel@tonic-gate 			 * and translate the remote address.
28708348SEric.Yu@Sun.COM 			 * Holding so_lock thus sti_laddr_sa can not change.
28710Sstevel@tonic-gate 			 */
28728348SEric.Yu@Sun.COM 			src = sti->sti_laddr_sa;
28738348SEric.Yu@Sun.COM 			srclen = (socklen_t)sti->sti_laddr_len;
28740Sstevel@tonic-gate 			dprintso(so, 1,
28755240Snordmark 			    ("so_ux_close: srclen %d, src %p\n",
28765240Snordmark 			    srclen, src));
28770Sstevel@tonic-gate 			error = so_ux_addr_xlate(so,
28788348SEric.Yu@Sun.COM 			    sti->sti_faddr_sa,
28798348SEric.Yu@Sun.COM 			    (socklen_t)sti->sti_faddr_len, 0,
28805240Snordmark 			    &addr, &addrlen);
28810Sstevel@tonic-gate 			if (error) {
28820Sstevel@tonic-gate 				eprintsoline(so, error);
28830Sstevel@tonic-gate 				return;
28840Sstevel@tonic-gate 			}
28850Sstevel@tonic-gate 		}
28860Sstevel@tonic-gate 		tudr.PRIM_type = T_UNITDATA_REQ;
28870Sstevel@tonic-gate 		tudr.DEST_length = addrlen;
28880Sstevel@tonic-gate 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
28890Sstevel@tonic-gate 		if (srclen == 0) {
28900Sstevel@tonic-gate 			tudr.OPT_length = (t_scalar_t)sizeof (toh);
28910Sstevel@tonic-gate 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
28925240Snordmark 			    _TPI_ALIGN_TOPT(addrlen));
28930Sstevel@tonic-gate 
28940Sstevel@tonic-gate 			size = tudr.OPT_offset + tudr.OPT_length;
28950Sstevel@tonic-gate 			/* NOTE: holding so_lock while sleeping */
28960Sstevel@tonic-gate 			mp = soallocproto2(&tudr, sizeof (tudr),
28978778SErik.Nordmark@Sun.COM 			    addr, addrlen, size, _ALLOC_SLEEP, CRED());
28980Sstevel@tonic-gate 			mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
28990Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
29000Sstevel@tonic-gate 		} else {
29010Sstevel@tonic-gate 			/*
29020Sstevel@tonic-gate 			 * There is a AF_UNIX sockaddr_un to include as a
29030Sstevel@tonic-gate 			 * source address option.
29040Sstevel@tonic-gate 			 */
29050Sstevel@tonic-gate 			tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
29060Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(srclen));
29070Sstevel@tonic-gate 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
29080Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(addrlen));
29090Sstevel@tonic-gate 
29100Sstevel@tonic-gate 			toh2.level = SOL_SOCKET;
29110Sstevel@tonic-gate 			toh2.name = SO_SRCADDR;
29120Sstevel@tonic-gate 			toh2.len = (t_uscalar_t)(srclen +
29135240Snordmark 			    sizeof (struct T_opthdr));
29140Sstevel@tonic-gate 			toh2.status = 0;
29150Sstevel@tonic-gate 
29160Sstevel@tonic-gate 			size = tudr.OPT_offset + tudr.OPT_length;
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 			/* NOTE: holding so_lock while sleeping */
29190Sstevel@tonic-gate 			mp = soallocproto2(&tudr, sizeof (tudr),
29208778SErik.Nordmark@Sun.COM 			    addr, addrlen, size, _ALLOC_SLEEP, CRED());
29210Sstevel@tonic-gate 			mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
29220Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
29230Sstevel@tonic-gate 			soappendmsg(mp, &toh2, sizeof (toh2));
29240Sstevel@tonic-gate 			soappendmsg(mp, src, srclen);
29250Sstevel@tonic-gate 			mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
29260Sstevel@tonic-gate 		}
29270Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
29280Sstevel@tonic-gate 	}
29290Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
29300Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
29315240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
29320Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
29330Sstevel@tonic-gate }
29340Sstevel@tonic-gate 
29350Sstevel@tonic-gate /*
29360Sstevel@tonic-gate  * Called by sotpi_recvmsg when reading a non-zero amount of data.
29370Sstevel@tonic-gate  * In addition, the caller typically verifies that there is some
29380Sstevel@tonic-gate  * potential state to clear by checking
29390Sstevel@tonic-gate  *	if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
29400Sstevel@tonic-gate  * before calling this routine.
29410Sstevel@tonic-gate  * Note that such a check can be made without holding so_lock since
29420Sstevel@tonic-gate  * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
29438348SEric.Yu@Sun.COM  * decrements sti_oobsigcnt.
29440Sstevel@tonic-gate  *
29450Sstevel@tonic-gate  * When data is read *after* the point that all pending
29460Sstevel@tonic-gate  * oob data has been consumed the oob indication is cleared.
29470Sstevel@tonic-gate  *
29480Sstevel@tonic-gate  * This logic keeps select/poll returning POLLRDBAND and
29490Sstevel@tonic-gate  * SIOCATMARK returning true until we have read past
29500Sstevel@tonic-gate  * the mark.
29510Sstevel@tonic-gate  */
29520Sstevel@tonic-gate static void
sorecv_update_oobstate(struct sonode * so)29530Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so)
29540Sstevel@tonic-gate {
29558348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
29568348SEric.Yu@Sun.COM 
29570Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
29580Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
29590Sstevel@tonic-gate 	dprintso(so, 1,
29605240Snordmark 	    ("sorecv_update_oobstate: counts %d/%d state %s\n",
29618348SEric.Yu@Sun.COM 	    sti->sti_oobsigcnt,
29628348SEric.Yu@Sun.COM 	    sti->sti_oobcnt, pr_state(so->so_state, so->so_mode)));
29638348SEric.Yu@Sun.COM 	if (sti->sti_oobsigcnt == 0) {
29640Sstevel@tonic-gate 		/* No more pending oob indications */
29650Sstevel@tonic-gate 		so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
29660Sstevel@tonic-gate 		freemsg(so->so_oobmsg);
29670Sstevel@tonic-gate 		so->so_oobmsg = NULL;
29680Sstevel@tonic-gate 	}
29690Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
29700Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
29710Sstevel@tonic-gate }
29720Sstevel@tonic-gate 
29730Sstevel@tonic-gate /*
29740Sstevel@tonic-gate  * Handle recv* calls for an so which has NL7C saved recv mblk_t(s).
29750Sstevel@tonic-gate  */
29760Sstevel@tonic-gate static int
nl7c_sorecv(struct sonode * so,mblk_t ** rmp,uio_t * uiop,rval_t * rp)29770Sstevel@tonic-gate nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp)
29780Sstevel@tonic-gate {
29798348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
29800Sstevel@tonic-gate 	int	error = 0;
29810Sstevel@tonic-gate 	mblk_t *tmp = NULL;
29820Sstevel@tonic-gate 	mblk_t *pmp = NULL;
29838348SEric.Yu@Sun.COM 	mblk_t *nmp = sti->sti_nl7c_rcv_mp;
29840Sstevel@tonic-gate 
29850Sstevel@tonic-gate 	ASSERT(nmp != NULL);
29860Sstevel@tonic-gate 
29870Sstevel@tonic-gate 	while (nmp != NULL && uiop->uio_resid > 0) {
29880Sstevel@tonic-gate 		ssize_t n;
29890Sstevel@tonic-gate 
29900Sstevel@tonic-gate 		if (DB_TYPE(nmp) == M_DATA) {
29910Sstevel@tonic-gate 			/*
29920Sstevel@tonic-gate 			 * We have some data, uiomove up to resid bytes.
29930Sstevel@tonic-gate 			 */
29940Sstevel@tonic-gate 			n = MIN(MBLKL(nmp), uiop->uio_resid);
29950Sstevel@tonic-gate 			if (n > 0)
29960Sstevel@tonic-gate 				error = uiomove(nmp->b_rptr, n, UIO_READ, uiop);
29970Sstevel@tonic-gate 			nmp->b_rptr += n;
29980Sstevel@tonic-gate 			if (nmp->b_rptr == nmp->b_wptr) {
29990Sstevel@tonic-gate 				pmp = nmp;
30000Sstevel@tonic-gate 				nmp = nmp->b_cont;
30010Sstevel@tonic-gate 			}
30021974Sbrutus 			if (error)
30031974Sbrutus 				break;
30040Sstevel@tonic-gate 		} else {
30050Sstevel@tonic-gate 			/*
30060Sstevel@tonic-gate 			 * We only handle data, save for caller to handle.
30070Sstevel@tonic-gate 			 */
30080Sstevel@tonic-gate 			if (pmp != NULL) {
30090Sstevel@tonic-gate 				pmp->b_cont = nmp->b_cont;
30100Sstevel@tonic-gate 			}
30110Sstevel@tonic-gate 			nmp->b_cont = NULL;
30120Sstevel@tonic-gate 			if (*rmp == NULL) {
30130Sstevel@tonic-gate 				*rmp = nmp;
30140Sstevel@tonic-gate 			} else {
30151974Sbrutus 				tmp->b_cont = nmp;
30160Sstevel@tonic-gate 			}
30170Sstevel@tonic-gate 			nmp = nmp->b_cont;
30180Sstevel@tonic-gate 			tmp = nmp;
30190Sstevel@tonic-gate 		}
30200Sstevel@tonic-gate 	}
30210Sstevel@tonic-gate 	if (pmp != NULL) {
30220Sstevel@tonic-gate 		/* Free any mblk_t(s) which we have consumed */
30230Sstevel@tonic-gate 		pmp->b_cont = NULL;
30248348SEric.Yu@Sun.COM 		freemsg(sti->sti_nl7c_rcv_mp);
30258348SEric.Yu@Sun.COM 	}
30268348SEric.Yu@Sun.COM 	if ((sti->sti_nl7c_rcv_mp = nmp) == NULL) {
30271974Sbrutus 		/* Last mblk_t so return the saved kstrgetmsg() rval/error */
30281974Sbrutus 		if (error == 0) {
30298348SEric.Yu@Sun.COM 			rval_t	*p = (rval_t *)&sti->sti_nl7c_rcv_rval;
30301974Sbrutus 
30311974Sbrutus 			error = p->r_v.r_v2;
30321974Sbrutus 			p->r_v.r_v2 = 0;
30331974Sbrutus 		}
30348348SEric.Yu@Sun.COM 		rp->r_vals = sti->sti_nl7c_rcv_rval;
30358348SEric.Yu@Sun.COM 		sti->sti_nl7c_rcv_rval = 0;
30360Sstevel@tonic-gate 	} else {
30370Sstevel@tonic-gate 		/* More mblk_t(s) to process so no rval to return */
30380Sstevel@tonic-gate 		rp->r_vals = 0;
30390Sstevel@tonic-gate 	}
30400Sstevel@tonic-gate 	return (error);
30410Sstevel@tonic-gate }
30420Sstevel@tonic-gate /*
30430Sstevel@tonic-gate  * Receive the next message on the queue.
30440Sstevel@tonic-gate  * If msg_controllen is non-zero when called the caller is interested in
30450Sstevel@tonic-gate  * any received control info (options).
30460Sstevel@tonic-gate  * If msg_namelen is non-zero when called the caller is interested in
30470Sstevel@tonic-gate  * any received source address.
30480Sstevel@tonic-gate  * The routine returns with msg_control and msg_name pointing to
30490Sstevel@tonic-gate  * kmem_alloc'ed memory which the caller has to free.
30500Sstevel@tonic-gate  */
30518348SEric.Yu@Sun.COM /* ARGSUSED */
30520Sstevel@tonic-gate int
sotpi_recvmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)30538348SEric.Yu@Sun.COM sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
30548348SEric.Yu@Sun.COM     struct cred *cr)
30550Sstevel@tonic-gate {
30560Sstevel@tonic-gate 	union T_primitives	*tpr;
30570Sstevel@tonic-gate 	mblk_t			*mp;
30580Sstevel@tonic-gate 	uchar_t			pri;
30590Sstevel@tonic-gate 	int			pflag, opflag;
30600Sstevel@tonic-gate 	void			*control;
30610Sstevel@tonic-gate 	t_uscalar_t		controllen;
30620Sstevel@tonic-gate 	t_uscalar_t		namelen;
30630Sstevel@tonic-gate 	int			so_state = so->so_state; /* Snapshot */
30640Sstevel@tonic-gate 	ssize_t			saved_resid;
30650Sstevel@tonic-gate 	rval_t			rval;
30660Sstevel@tonic-gate 	int			flags;
30670Sstevel@tonic-gate 	clock_t			timout;
30686707Sbrutus 	int			error = 0;
30698348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
30700Sstevel@tonic-gate 
30710Sstevel@tonic-gate 	flags = msg->msg_flags;
30720Sstevel@tonic-gate 	msg->msg_flags = 0;
30730Sstevel@tonic-gate 
30740Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
30757240Srh87107 	    (void *)so, (void *)msg, flags,
30765240Snordmark 	    pr_state(so->so_state, so->so_mode), so->so_error));
30770Sstevel@tonic-gate 
30788348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
30798348SEric.Yu@Sun.COM 		so_update_attrs(so, SOACC);
30808348SEric.Yu@Sun.COM 		/* The imaginary "sockmod" has been popped - act as a stream */
30818348SEric.Yu@Sun.COM 		return (strread(SOTOV(so), uiop, cr));
30828348SEric.Yu@Sun.COM 	}
30838348SEric.Yu@Sun.COM 
30840Sstevel@tonic-gate 	/*
30850Sstevel@tonic-gate 	 * If we are not connected because we have never been connected
30860Sstevel@tonic-gate 	 * we return ENOTCONN. If we have been connected (but are no longer
30870Sstevel@tonic-gate 	 * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
30880Sstevel@tonic-gate 	 * the EOF.
30890Sstevel@tonic-gate 	 *
30900Sstevel@tonic-gate 	 * An alternative would be to post an ENOTCONN error in stream head
30910Sstevel@tonic-gate 	 * (read+write) and clear it when we're connected. However, that error
30920Sstevel@tonic-gate 	 * would cause incorrect poll/select behavior!
30930Sstevel@tonic-gate 	 */
30940Sstevel@tonic-gate 	if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
30950Sstevel@tonic-gate 	    (so->so_mode & SM_CONNREQUIRED)) {
30960Sstevel@tonic-gate 		return (ENOTCONN);
30970Sstevel@tonic-gate 	}
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 	/*
31000Sstevel@tonic-gate 	 * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
31010Sstevel@tonic-gate 	 * after checking that the read queue is empty) and returns zero.
31020Sstevel@tonic-gate 	 * This implementation will sleep (in kstrgetmsg) even if uio_resid
31030Sstevel@tonic-gate 	 * is zero.
31040Sstevel@tonic-gate 	 */
31050Sstevel@tonic-gate 
31060Sstevel@tonic-gate 	if (flags & MSG_OOB) {
31070Sstevel@tonic-gate 		/* Check that the transport supports OOB */
31080Sstevel@tonic-gate 		if (!(so->so_mode & SM_EXDATA))
31090Sstevel@tonic-gate 			return (EOPNOTSUPP);
31108348SEric.Yu@Sun.COM 		so_update_attrs(so, SOACC);
31118348SEric.Yu@Sun.COM 		return (sorecvoob(so, msg, uiop, flags,
31128348SEric.Yu@Sun.COM 		    (so->so_options & SO_OOBINLINE)));
31138348SEric.Yu@Sun.COM 	}
31148348SEric.Yu@Sun.COM 
31158348SEric.Yu@Sun.COM 	so_update_attrs(so, SOACC);
31160Sstevel@tonic-gate 
31170Sstevel@tonic-gate 	/*
31180Sstevel@tonic-gate 	 * Set msg_controllen and msg_namelen to zero here to make it
31190Sstevel@tonic-gate 	 * simpler in the cases that no control or name is returned.
31200Sstevel@tonic-gate 	 */
31210Sstevel@tonic-gate 	controllen = msg->msg_controllen;
31220Sstevel@tonic-gate 	namelen = msg->msg_namelen;
31230Sstevel@tonic-gate 	msg->msg_controllen = 0;
31240Sstevel@tonic-gate 	msg->msg_namelen = 0;
31250Sstevel@tonic-gate 
31260Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
31275240Snordmark 	    namelen, controllen));
31280Sstevel@tonic-gate 
31291974Sbrutus 	mutex_enter(&so->so_lock);
31300Sstevel@tonic-gate 	/*
31310Sstevel@tonic-gate 	 * If an NL7C enabled socket and not waiting for write data.
31320Sstevel@tonic-gate 	 */
31338348SEric.Yu@Sun.COM 	if ((sti->sti_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) ==
31340Sstevel@tonic-gate 	    NL7C_ENABLED) {
31358348SEric.Yu@Sun.COM 		if (sti->sti_nl7c_uri) {
31361974Sbrutus 			/* Close uri processing for a previous request */
31370Sstevel@tonic-gate 			nl7c_close(so);
31380Sstevel@tonic-gate 		}
31398348SEric.Yu@Sun.COM 		if ((so_state & SS_CANTRCVMORE) &&
31408348SEric.Yu@Sun.COM 		    sti->sti_nl7c_rcv_mp == NULL) {
31411974Sbrutus 			/* Nothing to process, EOF */
31421974Sbrutus 			mutex_exit(&so->so_lock);
31431974Sbrutus 			return (0);
31448348SEric.Yu@Sun.COM 		} else if (sti->sti_nl7c_flags & NL7C_SOPERSIST) {
31451974Sbrutus 			/* Persistent NL7C socket, try to process request */
31461974Sbrutus 			boolean_t ret;
31471974Sbrutus 
31481974Sbrutus 			ret = nl7c_process(so,
31491974Sbrutus 			    (so->so_state & (SS_NONBLOCK|SS_NDELAY)));
31508348SEric.Yu@Sun.COM 			rval.r_vals = sti->sti_nl7c_rcv_rval;
31511974Sbrutus 			error = rval.r_v.r_v2;
31521974Sbrutus 			if (error) {
31531974Sbrutus 				/* Error of some sort, return it */
31541974Sbrutus 				mutex_exit(&so->so_lock);
31551974Sbrutus 				return (error);
31561974Sbrutus 			}
31578348SEric.Yu@Sun.COM 			if (sti->sti_nl7c_flags &&
31588348SEric.Yu@Sun.COM 			    ! (sti->sti_nl7c_flags & NL7C_WAITWRITE)) {
31591974Sbrutus 				/*
31601974Sbrutus 				 * Still an NL7C socket and no data
31611974Sbrutus 				 * to pass up to the caller.
31621974Sbrutus 				 */
31631974Sbrutus 				mutex_exit(&so->so_lock);
31641974Sbrutus 				if (ret) {
31651974Sbrutus 					/* EOF */
31661974Sbrutus 					return (0);
31671974Sbrutus 				} else {
31681974Sbrutus 					/* Need more data */
31691974Sbrutus 					return (EAGAIN);
31701974Sbrutus 				}
31711974Sbrutus 			}
31721974Sbrutus 		} else {
31730Sstevel@tonic-gate 			/*
31741974Sbrutus 			 * Not persistent so no further NL7C processing.
31750Sstevel@tonic-gate 			 */
31768348SEric.Yu@Sun.COM 			sti->sti_nl7c_flags = 0;
31770Sstevel@tonic-gate 		}
31780Sstevel@tonic-gate 	}
31790Sstevel@tonic-gate 	/*
31800Sstevel@tonic-gate 	 * Only one reader is allowed at any given time. This is needed
31810Sstevel@tonic-gate 	 * for T_EXDATA handling and, in the future, MSG_WAITALL.
31820Sstevel@tonic-gate 	 *
31830Sstevel@tonic-gate 	 * This is slightly different that BSD behavior in that it fails with
31840Sstevel@tonic-gate 	 * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
31850Sstevel@tonic-gate 	 * is single-threaded using sblock(), which is dropped while waiting
31860Sstevel@tonic-gate 	 * for data to appear. The difference shows up e.g. if one
31870Sstevel@tonic-gate 	 * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
31880Sstevel@tonic-gate 	 * does use nonblocking io and different threads are reading each
31890Sstevel@tonic-gate 	 * file descriptor. In BSD there would never be an EWOULDBLOCK error
31900Sstevel@tonic-gate 	 * in this case as long as the read queue doesn't get empty.
31910Sstevel@tonic-gate 	 * In this implementation the thread using nonblocking io can
31920Sstevel@tonic-gate 	 * get an EWOULDBLOCK error due to the blocking thread executing
31930Sstevel@tonic-gate 	 * e.g. in the uiomove in kstrgetmsg.
31940Sstevel@tonic-gate 	 * This difference is not believed to be significant.
31950Sstevel@tonic-gate 	 */
31963749Sethindra 	/* Set SOREADLOCKED */
31973749Sethindra 	error = so_lock_read_intr(so,
31983749Sethindra 	    uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
31990Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
32000Sstevel@tonic-gate 	if (error)
32010Sstevel@tonic-gate 		return (error);
32020Sstevel@tonic-gate 
32030Sstevel@tonic-gate 	/*
32040Sstevel@tonic-gate 	 * Tell kstrgetmsg to not inspect the stream head errors until all
32050Sstevel@tonic-gate 	 * queued data has been consumed.
32060Sstevel@tonic-gate 	 * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
32070Sstevel@tonic-gate 	 * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
32080Sstevel@tonic-gate 	 *
32090Sstevel@tonic-gate 	 * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
32100Sstevel@tonic-gate 	 * to T_OPTDATA_IND that do not contain any user-visible control msg.
32110Sstevel@tonic-gate 	 * Note that MSG_WAITALL set with MSG_PEEK is a noop.
32120Sstevel@tonic-gate 	 */
32130Sstevel@tonic-gate 	pflag = MSG_ANY | MSG_DELAYERROR;
32140Sstevel@tonic-gate 	if (flags & MSG_PEEK) {
32150Sstevel@tonic-gate 		pflag |= MSG_IPEEK;
32160Sstevel@tonic-gate 		flags &= ~MSG_WAITALL;
32170Sstevel@tonic-gate 	}
32180Sstevel@tonic-gate 	if (so->so_mode & SM_ATOMIC)
32190Sstevel@tonic-gate 		pflag |= MSG_DISCARDTAIL;
32200Sstevel@tonic-gate 
32210Sstevel@tonic-gate 	if (flags & MSG_DONTWAIT)
32220Sstevel@tonic-gate 		timout = 0;
32230Sstevel@tonic-gate 	else
32240Sstevel@tonic-gate 		timout = -1;
32250Sstevel@tonic-gate 	opflag = pflag;
32260Sstevel@tonic-gate retry:
32270Sstevel@tonic-gate 	saved_resid = uiop->uio_resid;
32280Sstevel@tonic-gate 	pri = 0;
32290Sstevel@tonic-gate 	mp = NULL;
32308348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_rcv_mp != NULL) {
32311974Sbrutus 		/* Already kstrgetmsg()ed saved mblk(s) from NL7C */
32320Sstevel@tonic-gate 		error = nl7c_sorecv(so, &mp, uiop, &rval);
32330Sstevel@tonic-gate 	} else {
32340Sstevel@tonic-gate 		error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
32350Sstevel@tonic-gate 		    timout, &rval);
32360Sstevel@tonic-gate 	}
32378348SEric.Yu@Sun.COM 	if (error != 0) {
32388348SEric.Yu@Sun.COM 		/* kstrgetmsg returns ETIME when timeout expires */
32398348SEric.Yu@Sun.COM 		if (error == ETIME)
32408348SEric.Yu@Sun.COM 			error = EWOULDBLOCK;
32416707Sbrutus 		goto out;
32420Sstevel@tonic-gate 	}
32430Sstevel@tonic-gate 	/*
32440Sstevel@tonic-gate 	 * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
32450Sstevel@tonic-gate 	 * For non-datagrams MOREDATA is used to set MSG_EOR.
32460Sstevel@tonic-gate 	 */
32470Sstevel@tonic-gate 	ASSERT(!(rval.r_val1 & MORECTL));
32480Sstevel@tonic-gate 	if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
32490Sstevel@tonic-gate 		msg->msg_flags |= MSG_TRUNC;
32500Sstevel@tonic-gate 
32510Sstevel@tonic-gate 	if (mp == NULL) {
32520Sstevel@tonic-gate 		dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
32530Sstevel@tonic-gate 		/*
32540Sstevel@tonic-gate 		 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
32550Sstevel@tonic-gate 		 * The draft Posix socket spec states that the mark should
32560Sstevel@tonic-gate 		 * not be cleared when peeking. We follow the latter.
32570Sstevel@tonic-gate 		 */
32580Sstevel@tonic-gate 		if ((so->so_state &
32590Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32600Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
32610Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
32620Sstevel@tonic-gate 			sorecv_update_oobstate(so);
32630Sstevel@tonic-gate 		}
32640Sstevel@tonic-gate 
32650Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
32660Sstevel@tonic-gate 		/* Set MSG_EOR based on MOREDATA */
32670Sstevel@tonic-gate 		if (!(rval.r_val1 & MOREDATA)) {
32680Sstevel@tonic-gate 			if (so->so_state & SS_SAVEDEOR) {
32690Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
32700Sstevel@tonic-gate 				so->so_state &= ~SS_SAVEDEOR;
32710Sstevel@tonic-gate 			}
32720Sstevel@tonic-gate 		}
32730Sstevel@tonic-gate 		/*
32740Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
32750Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
32760Sstevel@tonic-gate 		 */
32770Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
32780Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
32790Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
32800Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
32810Sstevel@tonic-gate 			goto retry;
32820Sstevel@tonic-gate 		}
32836707Sbrutus 		goto out_locked;
32840Sstevel@tonic-gate 	}
32850Sstevel@tonic-gate 
32860Sstevel@tonic-gate 	/* strsock_proto has already verified length and alignment */
32870Sstevel@tonic-gate 	tpr = (union T_primitives *)mp->b_rptr;
32880Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
32890Sstevel@tonic-gate 
32900Sstevel@tonic-gate 	switch (tpr->type) {
32910Sstevel@tonic-gate 	case T_DATA_IND: {
32920Sstevel@tonic-gate 		if ((so->so_state &
32930Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32940Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
32950Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
32960Sstevel@tonic-gate 			sorecv_update_oobstate(so);
32970Sstevel@tonic-gate 		}
32980Sstevel@tonic-gate 
32990Sstevel@tonic-gate 		/*
33000Sstevel@tonic-gate 		 * Set msg_flags to MSG_EOR based on
33010Sstevel@tonic-gate 		 * MORE_flag and MOREDATA.
33020Sstevel@tonic-gate 		 */
33030Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
33040Sstevel@tonic-gate 		so->so_state &= ~SS_SAVEDEOR;
33050Sstevel@tonic-gate 		if (!(tpr->data_ind.MORE_flag & 1)) {
33060Sstevel@tonic-gate 			if (!(rval.r_val1 & MOREDATA))
33070Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
33080Sstevel@tonic-gate 			else
33090Sstevel@tonic-gate 				so->so_state |= SS_SAVEDEOR;
33100Sstevel@tonic-gate 		}
33110Sstevel@tonic-gate 		freemsg(mp);
33120Sstevel@tonic-gate 		/*
33130Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
33140Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
33150Sstevel@tonic-gate 		 */
33160Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
33170Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
33180Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
33190Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
33200Sstevel@tonic-gate 			goto retry;
33210Sstevel@tonic-gate 		}
33226707Sbrutus 		goto out_locked;
33230Sstevel@tonic-gate 	}
33240Sstevel@tonic-gate 	case T_UNITDATA_IND: {
33250Sstevel@tonic-gate 		void *addr;
33260Sstevel@tonic-gate 		t_uscalar_t addrlen;
33270Sstevel@tonic-gate 		void *abuf;
33280Sstevel@tonic-gate 		t_uscalar_t optlen;
33290Sstevel@tonic-gate 		void *opt;
33300Sstevel@tonic-gate 
33310Sstevel@tonic-gate 		if ((so->so_state &
33320Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
33330Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
33340Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
33350Sstevel@tonic-gate 			sorecv_update_oobstate(so);
33360Sstevel@tonic-gate 		}
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 		if (namelen != 0) {
33390Sstevel@tonic-gate 			/* Caller wants source address */
33400Sstevel@tonic-gate 			addrlen = tpr->unitdata_ind.SRC_length;
33410Sstevel@tonic-gate 			addr = sogetoff(mp,
33425240Snordmark 			    tpr->unitdata_ind.SRC_offset,
33435240Snordmark 			    addrlen, 1);
33440Sstevel@tonic-gate 			if (addr == NULL) {
33450Sstevel@tonic-gate 				freemsg(mp);
33460Sstevel@tonic-gate 				error = EPROTO;
33470Sstevel@tonic-gate 				eprintsoline(so, error);
33486707Sbrutus 				goto out;
33490Sstevel@tonic-gate 			}
33500Sstevel@tonic-gate 			if (so->so_family == AF_UNIX) {
33510Sstevel@tonic-gate 				/*
33520Sstevel@tonic-gate 				 * Can not use the transport level address.
33530Sstevel@tonic-gate 				 * If there is a SO_SRCADDR option carrying
33540Sstevel@tonic-gate 				 * the socket level address it will be
33550Sstevel@tonic-gate 				 * extracted below.
33560Sstevel@tonic-gate 				 */
33570Sstevel@tonic-gate 				addr = NULL;
33580Sstevel@tonic-gate 				addrlen = 0;
33590Sstevel@tonic-gate 			}
33600Sstevel@tonic-gate 		}
33610Sstevel@tonic-gate 		optlen = tpr->unitdata_ind.OPT_length;
33620Sstevel@tonic-gate 		if (optlen != 0) {
33630Sstevel@tonic-gate 			t_uscalar_t ncontrollen;
33640Sstevel@tonic-gate 
33650Sstevel@tonic-gate 			/*
33660Sstevel@tonic-gate 			 * Extract any source address option.
33670Sstevel@tonic-gate 			 * Determine how large cmsg buffer is needed.
33680Sstevel@tonic-gate 			 */
33690Sstevel@tonic-gate 			opt = sogetoff(mp,
33705240Snordmark 			    tpr->unitdata_ind.OPT_offset,
33715240Snordmark 			    optlen, __TPI_ALIGN_SIZE);
33720Sstevel@tonic-gate 
33730Sstevel@tonic-gate 			if (opt == NULL) {
33740Sstevel@tonic-gate 				freemsg(mp);
33750Sstevel@tonic-gate 				error = EPROTO;
33760Sstevel@tonic-gate 				eprintsoline(so, error);
33776707Sbrutus 				goto out;
33780Sstevel@tonic-gate 			}
33790Sstevel@tonic-gate 			if (so->so_family == AF_UNIX)
33800Sstevel@tonic-gate 				so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
33810Sstevel@tonic-gate 			ncontrollen = so_cmsglen(mp, opt, optlen,
33825240Snordmark 			    !(flags & MSG_XPG4_2));
33830Sstevel@tonic-gate 			if (controllen != 0)
33840Sstevel@tonic-gate 				controllen = ncontrollen;
33850Sstevel@tonic-gate 			else if (ncontrollen != 0)
33860Sstevel@tonic-gate 				msg->msg_flags |= MSG_CTRUNC;
33870Sstevel@tonic-gate 		} else {
33880Sstevel@tonic-gate 			controllen = 0;
33890Sstevel@tonic-gate 		}
33900Sstevel@tonic-gate 
33910Sstevel@tonic-gate 		if (namelen != 0) {
33920Sstevel@tonic-gate 			/*
33930Sstevel@tonic-gate 			 * Return address to caller.
33940Sstevel@tonic-gate 			 * Caller handles truncation if length
33950Sstevel@tonic-gate 			 * exceeds msg_namelen.
33960Sstevel@tonic-gate 			 * NOTE: AF_UNIX NUL termination is ensured by
33970Sstevel@tonic-gate 			 * the sender's copyin_name().
33980Sstevel@tonic-gate 			 */
33990Sstevel@tonic-gate 			abuf = kmem_alloc(addrlen, KM_SLEEP);
34000Sstevel@tonic-gate 
34010Sstevel@tonic-gate 			bcopy(addr, abuf, addrlen);
34020Sstevel@tonic-gate 			msg->msg_name = abuf;
34030Sstevel@tonic-gate 			msg->msg_namelen = addrlen;
34040Sstevel@tonic-gate 		}
34050Sstevel@tonic-gate 
34060Sstevel@tonic-gate 		if (controllen != 0) {
34070Sstevel@tonic-gate 			/*
34080Sstevel@tonic-gate 			 * Return control msg to caller.
34090Sstevel@tonic-gate 			 * Caller handles truncation if length
34100Sstevel@tonic-gate 			 * exceeds msg_controllen.
34110Sstevel@tonic-gate 			 */
34124420Samehta 			control = kmem_zalloc(controllen, KM_SLEEP);
34130Sstevel@tonic-gate 
34140Sstevel@tonic-gate 			error = so_opt2cmsg(mp, opt, optlen,
34155240Snordmark 			    !(flags & MSG_XPG4_2),
34165240Snordmark 			    control, controllen);
34170Sstevel@tonic-gate 			if (error) {
34180Sstevel@tonic-gate 				freemsg(mp);
34190Sstevel@tonic-gate 				if (msg->msg_namelen != 0)
34200Sstevel@tonic-gate 					kmem_free(msg->msg_name,
34215240Snordmark 					    msg->msg_namelen);
34220Sstevel@tonic-gate 				kmem_free(control, controllen);
34230Sstevel@tonic-gate 				eprintsoline(so, error);
34246707Sbrutus 				goto out;
34250Sstevel@tonic-gate 			}
34260Sstevel@tonic-gate 			msg->msg_control = control;
34270Sstevel@tonic-gate 			msg->msg_controllen = controllen;
34280Sstevel@tonic-gate 		}
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 		freemsg(mp);
34316707Sbrutus 		goto out;
34320Sstevel@tonic-gate 	}
34330Sstevel@tonic-gate 	case T_OPTDATA_IND: {
34340Sstevel@tonic-gate 		struct T_optdata_req *tdr;
34350Sstevel@tonic-gate 		void *opt;
34360Sstevel@tonic-gate 		t_uscalar_t optlen;
34370Sstevel@tonic-gate 
34380Sstevel@tonic-gate 		if ((so->so_state &
34390Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
34400Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
34410Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
34420Sstevel@tonic-gate 			sorecv_update_oobstate(so);
34430Sstevel@tonic-gate 		}
34440Sstevel@tonic-gate 
34450Sstevel@tonic-gate 		tdr = (struct T_optdata_req *)mp->b_rptr;
34460Sstevel@tonic-gate 		optlen = tdr->OPT_length;
34470Sstevel@tonic-gate 		if (optlen != 0) {
34480Sstevel@tonic-gate 			t_uscalar_t ncontrollen;
34490Sstevel@tonic-gate 			/*
34500Sstevel@tonic-gate 			 * Determine how large cmsg buffer is needed.
34510Sstevel@tonic-gate 			 */
34520Sstevel@tonic-gate 			opt = sogetoff(mp,
34535240Snordmark 			    tpr->optdata_ind.OPT_offset,
34545240Snordmark 			    optlen, __TPI_ALIGN_SIZE);
34550Sstevel@tonic-gate 
34560Sstevel@tonic-gate 			if (opt == NULL) {
34570Sstevel@tonic-gate 				freemsg(mp);
34580Sstevel@tonic-gate 				error = EPROTO;
34590Sstevel@tonic-gate 				eprintsoline(so, error);
34606707Sbrutus 				goto out;
34610Sstevel@tonic-gate 			}
34620Sstevel@tonic-gate 
34630Sstevel@tonic-gate 			ncontrollen = so_cmsglen(mp, opt, optlen,
34645240Snordmark 			    !(flags & MSG_XPG4_2));
34650Sstevel@tonic-gate 			if (controllen != 0)
34660Sstevel@tonic-gate 				controllen = ncontrollen;
34670Sstevel@tonic-gate 			else if (ncontrollen != 0)
34680Sstevel@tonic-gate 				msg->msg_flags |= MSG_CTRUNC;
34690Sstevel@tonic-gate 		} else {
34700Sstevel@tonic-gate 			controllen = 0;
34710Sstevel@tonic-gate 		}
34720Sstevel@tonic-gate 
34730Sstevel@tonic-gate 		if (controllen != 0) {
34740Sstevel@tonic-gate 			/*
34750Sstevel@tonic-gate 			 * Return control msg to caller.
34760Sstevel@tonic-gate 			 * Caller handles truncation if length
34770Sstevel@tonic-gate 			 * exceeds msg_controllen.
34780Sstevel@tonic-gate 			 */
34794420Samehta 			control = kmem_zalloc(controllen, KM_SLEEP);
34800Sstevel@tonic-gate 
34810Sstevel@tonic-gate 			error = so_opt2cmsg(mp, opt, optlen,
34825240Snordmark 			    !(flags & MSG_XPG4_2),
34835240Snordmark 			    control, controllen);
34840Sstevel@tonic-gate 			if (error) {
34850Sstevel@tonic-gate 				freemsg(mp);
34860Sstevel@tonic-gate 				kmem_free(control, controllen);
34870Sstevel@tonic-gate 				eprintsoline(so, error);
34886707Sbrutus 				goto out;
34890Sstevel@tonic-gate 			}
34900Sstevel@tonic-gate 			msg->msg_control = control;
34910Sstevel@tonic-gate 			msg->msg_controllen = controllen;
34920Sstevel@tonic-gate 		}
34930Sstevel@tonic-gate 
34940Sstevel@tonic-gate 		/*
34950Sstevel@tonic-gate 		 * Set msg_flags to MSG_EOR based on
34960Sstevel@tonic-gate 		 * DATA_flag and MOREDATA.
34970Sstevel@tonic-gate 		 */
34980Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
34990Sstevel@tonic-gate 		so->so_state &= ~SS_SAVEDEOR;
35000Sstevel@tonic-gate 		if (!(tpr->data_ind.MORE_flag & 1)) {
35010Sstevel@tonic-gate 			if (!(rval.r_val1 & MOREDATA))
35020Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
35030Sstevel@tonic-gate 			else
35040Sstevel@tonic-gate 				so->so_state |= SS_SAVEDEOR;
35050Sstevel@tonic-gate 		}
35060Sstevel@tonic-gate 		freemsg(mp);
35070Sstevel@tonic-gate 		/*
35080Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
35090Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
35100Sstevel@tonic-gate 		 * Not possible to wait if control info was received.
35110Sstevel@tonic-gate 		 */
35120Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
35130Sstevel@tonic-gate 		    controllen == 0 &&
35140Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
35150Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
35160Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
35170Sstevel@tonic-gate 			goto retry;
35180Sstevel@tonic-gate 		}
35196707Sbrutus 		goto out_locked;
35200Sstevel@tonic-gate 	}
35210Sstevel@tonic-gate 	case T_EXDATA_IND: {
35220Sstevel@tonic-gate 		dprintso(so, 1,
35235240Snordmark 		    ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
35245240Snordmark 		    "state %s\n",
35258348SEric.Yu@Sun.COM 		    sti->sti_oobsigcnt, sti->sti_oobcnt,
35265240Snordmark 		    saved_resid - uiop->uio_resid,
35275240Snordmark 		    pr_state(so->so_state, so->so_mode)));
35280Sstevel@tonic-gate 		/*
35290Sstevel@tonic-gate 		 * kstrgetmsg handles MSGMARK so there is nothing to
35300Sstevel@tonic-gate 		 * inspect in the T_EXDATA_IND.
35310Sstevel@tonic-gate 		 * strsock_proto makes the stream head queue the T_EXDATA_IND
35320Sstevel@tonic-gate 		 * as a separate message with no M_DATA component. Furthermore,
35330Sstevel@tonic-gate 		 * the stream head does not consolidate M_DATA messages onto
35340Sstevel@tonic-gate 		 * an MSGMARK'ed message ensuring that the T_EXDATA_IND
35350Sstevel@tonic-gate 		 * remains a message by itself. This is needed since MSGMARK
35360Sstevel@tonic-gate 		 * marks both the whole message as well as the last byte
35370Sstevel@tonic-gate 		 * of the message.
35380Sstevel@tonic-gate 		 */
35390Sstevel@tonic-gate 		freemsg(mp);
35400Sstevel@tonic-gate 		ASSERT(uiop->uio_resid == saved_resid);	/* No data */
35410Sstevel@tonic-gate 		if (flags & MSG_PEEK) {
35420Sstevel@tonic-gate 			/*
35430Sstevel@tonic-gate 			 * Even though we are peeking we consume the
35440Sstevel@tonic-gate 			 * T_EXDATA_IND thereby moving the mark information
35450Sstevel@tonic-gate 			 * to SS_RCVATMARK. Then the oob code below will
35460Sstevel@tonic-gate 			 * retry the peeking kstrgetmsg.
35470Sstevel@tonic-gate 			 * Note that the stream head read queue is
35480Sstevel@tonic-gate 			 * never flushed without holding SOREADLOCKED
35490Sstevel@tonic-gate 			 * thus the T_EXDATA_IND can not disappear
35500Sstevel@tonic-gate 			 * underneath us.
35510Sstevel@tonic-gate 			 */
35520Sstevel@tonic-gate 			dprintso(so, 1,
35535240Snordmark 			    ("sotpi_recvmsg: consume EXDATA_IND "
35545240Snordmark 			    "counts %d/%d state %s\n",
35558348SEric.Yu@Sun.COM 			    sti->sti_oobsigcnt,
35568348SEric.Yu@Sun.COM 			    sti->sti_oobcnt,
35575240Snordmark 			    pr_state(so->so_state, so->so_mode)));
35580Sstevel@tonic-gate 
35590Sstevel@tonic-gate 			pflag = MSG_ANY | MSG_DELAYERROR;
35600Sstevel@tonic-gate 			if (so->so_mode & SM_ATOMIC)
35610Sstevel@tonic-gate 				pflag |= MSG_DISCARDTAIL;
35620Sstevel@tonic-gate 
35630Sstevel@tonic-gate 			pri = 0;
35640Sstevel@tonic-gate 			mp = NULL;
35650Sstevel@tonic-gate 
35660Sstevel@tonic-gate 			error = kstrgetmsg(SOTOV(so), &mp, uiop,
35675240Snordmark 			    &pri, &pflag, (clock_t)-1, &rval);
35680Sstevel@tonic-gate 			ASSERT(uiop->uio_resid == saved_resid);
35690Sstevel@tonic-gate 
35700Sstevel@tonic-gate 			if (error) {
35710Sstevel@tonic-gate #ifdef SOCK_DEBUG
35720Sstevel@tonic-gate 				if (error != EWOULDBLOCK && error != EINTR) {
35730Sstevel@tonic-gate 					eprintsoline(so, error);
35740Sstevel@tonic-gate 				}
35750Sstevel@tonic-gate #endif /* SOCK_DEBUG */
35766707Sbrutus 				goto out;
35770Sstevel@tonic-gate 			}
35780Sstevel@tonic-gate 			ASSERT(mp);
35790Sstevel@tonic-gate 			tpr = (union T_primitives *)mp->b_rptr;
35800Sstevel@tonic-gate 			ASSERT(tpr->type == T_EXDATA_IND);
35810Sstevel@tonic-gate 			freemsg(mp);
35820Sstevel@tonic-gate 		} /* end "if (flags & MSG_PEEK)" */
35830Sstevel@tonic-gate 
35840Sstevel@tonic-gate 		/*
35850Sstevel@tonic-gate 		 * Decrement the number of queued and pending oob.
35860Sstevel@tonic-gate 		 *
35870Sstevel@tonic-gate 		 * SS_RCVATMARK is cleared when we read past a mark.
35880Sstevel@tonic-gate 		 * SS_HAVEOOBDATA is cleared when we've read past the
35890Sstevel@tonic-gate 		 * last mark.
35900Sstevel@tonic-gate 		 * SS_OOBPEND is cleared if we've read past the last
35910Sstevel@tonic-gate 		 * mark and no (new) SIGURG has been posted.
35920Sstevel@tonic-gate 		 */
35930Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
35940Sstevel@tonic-gate 		ASSERT(so_verify_oobstate(so));
35958348SEric.Yu@Sun.COM 		ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
35968348SEric.Yu@Sun.COM 		ASSERT(sti->sti_oobsigcnt > 0);
35978348SEric.Yu@Sun.COM 		sti->sti_oobsigcnt--;
35988348SEric.Yu@Sun.COM 		ASSERT(sti->sti_oobcnt > 0);
35998348SEric.Yu@Sun.COM 		sti->sti_oobcnt--;
36000Sstevel@tonic-gate 		/*
36010Sstevel@tonic-gate 		 * Since the T_EXDATA_IND has been removed from the stream
36020Sstevel@tonic-gate 		 * head, but we have not read data past the mark,
36030Sstevel@tonic-gate 		 * sockfs needs to track that the socket is still at the mark.
36040Sstevel@tonic-gate 		 *
36050Sstevel@tonic-gate 		 * Since no data was received call kstrgetmsg again to wait
36060Sstevel@tonic-gate 		 * for data.
36070Sstevel@tonic-gate 		 */
36080Sstevel@tonic-gate 		so->so_state |= SS_RCVATMARK;
36090Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
36100Sstevel@tonic-gate 		dprintso(so, 1,
36110Sstevel@tonic-gate 		    ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n",
36128348SEric.Yu@Sun.COM 		    sti->sti_oobsigcnt, sti->sti_oobcnt,
36130Sstevel@tonic-gate 		    pr_state(so->so_state, so->so_mode)));
36140Sstevel@tonic-gate 		pflag = opflag;
36150Sstevel@tonic-gate 		goto retry;
36160Sstevel@tonic-gate 	}
36170Sstevel@tonic-gate 	default:
36188348SEric.Yu@Sun.COM 		cmn_err(CE_CONT, "sotpi_recvmsg: so %p prim %d mp %p\n",
36198348SEric.Yu@Sun.COM 		    (void *)so, tpr->type, (void *)mp);
36200Sstevel@tonic-gate 		ASSERT(0);
36210Sstevel@tonic-gate 		freemsg(mp);
36220Sstevel@tonic-gate 		error = EPROTO;
36230Sstevel@tonic-gate 		eprintsoline(so, error);
36246707Sbrutus 		goto out;
36250Sstevel@tonic-gate 	}
36260Sstevel@tonic-gate 	/* NOTREACHED */
36276707Sbrutus out:
36280Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
36296707Sbrutus out_locked:
36300Sstevel@tonic-gate 	so_unlock_read(so);	/* Clear SOREADLOCKED */
36310Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
36320Sstevel@tonic-gate 	return (error);
36330Sstevel@tonic-gate }
36340Sstevel@tonic-gate 
36350Sstevel@tonic-gate /*
36360Sstevel@tonic-gate  * Sending data with options on a datagram socket.
36370Sstevel@tonic-gate  * Assumes caller has verified that SS_ISBOUND etc. are set.
36380Sstevel@tonic-gate  */
36390Sstevel@tonic-gate static int
sosend_dgramcmsg(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,void * control,t_uscalar_t controllen,int flags)3640741Smasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3641741Smasputra     struct uio *uiop, void *control, t_uscalar_t controllen, int flags)
36420Sstevel@tonic-gate {
36430Sstevel@tonic-gate 	struct T_unitdata_req	tudr;
36440Sstevel@tonic-gate 	mblk_t			*mp;
36450Sstevel@tonic-gate 	int			error;
36460Sstevel@tonic-gate 	void			*addr;
36470Sstevel@tonic-gate 	socklen_t		addrlen;
36480Sstevel@tonic-gate 	void			*src;
36490Sstevel@tonic-gate 	socklen_t		srclen;
36500Sstevel@tonic-gate 	ssize_t			len;
36510Sstevel@tonic-gate 	int			size;
36520Sstevel@tonic-gate 	struct T_opthdr		toh;
36530Sstevel@tonic-gate 	struct fdbuf		*fdbuf;
36540Sstevel@tonic-gate 	t_uscalar_t		optlen;
36550Sstevel@tonic-gate 	void			*fds;
36560Sstevel@tonic-gate 	int			fdlen;
36578348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
36580Sstevel@tonic-gate 
36590Sstevel@tonic-gate 	ASSERT(name && namelen);
36600Sstevel@tonic-gate 	ASSERT(control && controllen);
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate 	len = uiop->uio_resid;
36638348SEric.Yu@Sun.COM 	if (len > (ssize_t)sti->sti_tidu_size) {
36640Sstevel@tonic-gate 		return (EMSGSIZE);
36650Sstevel@tonic-gate 	}
36660Sstevel@tonic-gate 
36670Sstevel@tonic-gate 	/*
36680Sstevel@tonic-gate 	 * For AF_UNIX the destination address is translated to an internal
36690Sstevel@tonic-gate 	 * name and the source address is passed as an option.
36700Sstevel@tonic-gate 	 * Also, file descriptors are passed as file pointers in an
36710Sstevel@tonic-gate 	 * option.
36720Sstevel@tonic-gate 	 */
36730Sstevel@tonic-gate 
36740Sstevel@tonic-gate 	/*
36750Sstevel@tonic-gate 	 * Length and family checks.
36760Sstevel@tonic-gate 	 */
36770Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
36780Sstevel@tonic-gate 	if (error) {
36790Sstevel@tonic-gate 		eprintsoline(so, error);
36800Sstevel@tonic-gate 		return (error);
36810Sstevel@tonic-gate 	}
36820Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
36838348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate) {
36840Sstevel@tonic-gate 			/*
36850Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
36860Sstevel@tonic-gate 			 * pass any (transport internal) source address.
36870Sstevel@tonic-gate 			 */
36880Sstevel@tonic-gate 			addr = name;
36890Sstevel@tonic-gate 			addrlen = namelen;
36900Sstevel@tonic-gate 			src = NULL;
36910Sstevel@tonic-gate 			srclen = 0;
36920Sstevel@tonic-gate 		} else {
36930Sstevel@tonic-gate 			/*
36940Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
36950Sstevel@tonic-gate 			 * and translate the remote address.
36960Sstevel@tonic-gate 			 *
36978348SEric.Yu@Sun.COM 			 * Note that this code does not prevent sti_laddr_sa
36980Sstevel@tonic-gate 			 * from changing while it is being used. Thus
36990Sstevel@tonic-gate 			 * if an unbind+bind occurs concurrently with this
37000Sstevel@tonic-gate 			 * send the peer might see a partially new and a
37010Sstevel@tonic-gate 			 * partially old "from" address.
37020Sstevel@tonic-gate 			 */
37038348SEric.Yu@Sun.COM 			src = sti->sti_laddr_sa;
37048348SEric.Yu@Sun.COM 			srclen = (t_uscalar_t)sti->sti_laddr_len;
37050Sstevel@tonic-gate 			dprintso(so, 1,
37060Sstevel@tonic-gate 			    ("sosend_dgramcmsg UNIX: srclen %d, src %p\n",
37070Sstevel@tonic-gate 			    srclen, src));
37080Sstevel@tonic-gate 			error = so_ux_addr_xlate(so, name, namelen,
37095240Snordmark 			    (flags & MSG_XPG4_2),
37105240Snordmark 			    &addr, &addrlen);
37110Sstevel@tonic-gate 			if (error) {
37120Sstevel@tonic-gate 				eprintsoline(so, error);
37130Sstevel@tonic-gate 				return (error);
37140Sstevel@tonic-gate 			}
37150Sstevel@tonic-gate 		}
37160Sstevel@tonic-gate 	} else {
37170Sstevel@tonic-gate 		addr = name;
37180Sstevel@tonic-gate 		addrlen = namelen;
37190Sstevel@tonic-gate 		src = NULL;
37200Sstevel@tonic-gate 		srclen = 0;
37210Sstevel@tonic-gate 	}
37220Sstevel@tonic-gate 	optlen = so_optlen(control, controllen,
37235240Snordmark 	    !(flags & MSG_XPG4_2));
37240Sstevel@tonic-gate 	tudr.PRIM_type = T_UNITDATA_REQ;
37250Sstevel@tonic-gate 	tudr.DEST_length = addrlen;
37260Sstevel@tonic-gate 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
37270Sstevel@tonic-gate 	if (srclen != 0)
37280Sstevel@tonic-gate 		tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) +
37290Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(srclen));
37300Sstevel@tonic-gate 	else
37310Sstevel@tonic-gate 		tudr.OPT_length = optlen;
37320Sstevel@tonic-gate 	tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
37335240Snordmark 	    _TPI_ALIGN_TOPT(addrlen));
37340Sstevel@tonic-gate 
37350Sstevel@tonic-gate 	size = tudr.OPT_offset + tudr.OPT_length;
37360Sstevel@tonic-gate 
37370Sstevel@tonic-gate 	/*
37380Sstevel@tonic-gate 	 * File descriptors only when SM_FDPASSING set.
37390Sstevel@tonic-gate 	 */
37400Sstevel@tonic-gate 	error = so_getfdopt(control, controllen,
37415240Snordmark 	    !(flags & MSG_XPG4_2), &fds, &fdlen);
37420Sstevel@tonic-gate 	if (error)
37430Sstevel@tonic-gate 		return (error);
37440Sstevel@tonic-gate 	if (fdlen != -1) {
37450Sstevel@tonic-gate 		if (!(so->so_mode & SM_FDPASSING))
37460Sstevel@tonic-gate 			return (EOPNOTSUPP);
37470Sstevel@tonic-gate 
37480Sstevel@tonic-gate 		error = fdbuf_create(fds, fdlen, &fdbuf);
37490Sstevel@tonic-gate 		if (error)
37500Sstevel@tonic-gate 			return (error);
37510Sstevel@tonic-gate 		mp = fdbuf_allocmsg(size, fdbuf);
37520Sstevel@tonic-gate 	} else {
37538778SErik.Nordmark@Sun.COM 		mp = soallocproto(size, _ALLOC_INTR, CRED());
3754455Smeem 		if (mp == NULL) {
3755455Smeem 			/*
3756455Smeem 			 * Caught a signal waiting for memory.
3757455Smeem 			 * Let send* return EINTR.
3758455Smeem 			 */
3759455Smeem 			return (EINTR);
3760455Smeem 		}
37610Sstevel@tonic-gate 	}
37620Sstevel@tonic-gate 	soappendmsg(mp, &tudr, sizeof (tudr));
37630Sstevel@tonic-gate 	soappendmsg(mp, addr, addrlen);
37640Sstevel@tonic-gate 	mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
37650Sstevel@tonic-gate 
37660Sstevel@tonic-gate 	if (fdlen != -1) {
37670Sstevel@tonic-gate 		ASSERT(fdbuf != NULL);
37680Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
37690Sstevel@tonic-gate 		toh.name = SO_FILEP;
37700Sstevel@tonic-gate 		toh.len = fdbuf->fd_size +
37715240Snordmark 		    (t_uscalar_t)sizeof (struct T_opthdr);
37720Sstevel@tonic-gate 		toh.status = 0;
37730Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
37740Sstevel@tonic-gate 		soappendmsg(mp, fdbuf, fdbuf->fd_size);
37750Sstevel@tonic-gate 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
37760Sstevel@tonic-gate 	}
37770Sstevel@tonic-gate 	if (srclen != 0) {
37780Sstevel@tonic-gate 		/*
37790Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
37800Sstevel@tonic-gate 		 * address option.
37810Sstevel@tonic-gate 		 */
37820Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
37830Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
37840Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
37850Sstevel@tonic-gate 		toh.status = 0;
37860Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
37870Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
37880Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
37890Sstevel@tonic-gate 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
37900Sstevel@tonic-gate 	}
37910Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
37920Sstevel@tonic-gate 	so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
37930Sstevel@tonic-gate 	/* At most 3 bytes left in the message */
37940Sstevel@tonic-gate 	ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
37950Sstevel@tonic-gate 	ASSERT(MBLKL(mp) <= (ssize_t)size);
37960Sstevel@tonic-gate 
37970Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
379811861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
37990Sstevel@tonic-gate 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
38000Sstevel@tonic-gate 
38010Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
38020Sstevel@tonic-gate #ifdef SOCK_DEBUG
38030Sstevel@tonic-gate 	if (error) {
38040Sstevel@tonic-gate 		eprintsoline(so, error);
38050Sstevel@tonic-gate 	}
38060Sstevel@tonic-gate #endif /* SOCK_DEBUG */
38070Sstevel@tonic-gate 	return (error);
38080Sstevel@tonic-gate }
38090Sstevel@tonic-gate 
38100Sstevel@tonic-gate /*
38110Sstevel@tonic-gate  * Sending data with options on a connected stream socket.
38120Sstevel@tonic-gate  * Assumes caller has verified that SS_ISCONNECTED is set.
38130Sstevel@tonic-gate  */
38140Sstevel@tonic-gate static int
sosend_svccmsg(struct sonode * so,struct uio * uiop,int more,void * control,t_uscalar_t controllen,int flags)38158348SEric.Yu@Sun.COM sosend_svccmsg(struct sonode *so, struct uio *uiop, int more, void *control,
38168348SEric.Yu@Sun.COM     t_uscalar_t controllen, int flags)
38170Sstevel@tonic-gate {
38180Sstevel@tonic-gate 	struct T_optdata_req	tdr;
38190Sstevel@tonic-gate 	mblk_t			*mp;
38200Sstevel@tonic-gate 	int			error;
38210Sstevel@tonic-gate 	ssize_t			iosize;
38220Sstevel@tonic-gate 	int			size;
38230Sstevel@tonic-gate 	struct fdbuf		*fdbuf;
38240Sstevel@tonic-gate 	t_uscalar_t		optlen;
38250Sstevel@tonic-gate 	void			*fds;
38260Sstevel@tonic-gate 	int			fdlen;
38270Sstevel@tonic-gate 	struct T_opthdr		toh;
38288348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
38290Sstevel@tonic-gate 
38300Sstevel@tonic-gate 	dprintso(so, 1,
38315240Snordmark 	    ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
38320Sstevel@tonic-gate 
38330Sstevel@tonic-gate 	/*
38340Sstevel@tonic-gate 	 * Has to be bound and connected. However, since no locks are
38350Sstevel@tonic-gate 	 * held the state could have changed after sotpi_sendmsg checked it
38360Sstevel@tonic-gate 	 * thus it is not possible to ASSERT on the state.
38370Sstevel@tonic-gate 	 */
38380Sstevel@tonic-gate 
38390Sstevel@tonic-gate 	/* Options on connection-oriented only when SM_OPTDATA set. */
38400Sstevel@tonic-gate 	if (!(so->so_mode & SM_OPTDATA))
38410Sstevel@tonic-gate 		return (EOPNOTSUPP);
38420Sstevel@tonic-gate 
38430Sstevel@tonic-gate 	do {
38440Sstevel@tonic-gate 		/*
38450Sstevel@tonic-gate 		 * Set the MORE flag if uio_resid does not fit in this
38460Sstevel@tonic-gate 		 * message or if the caller passed in "more".
38470Sstevel@tonic-gate 		 * Error for transports with zero tidu_size.
38480Sstevel@tonic-gate 		 */
38490Sstevel@tonic-gate 		tdr.PRIM_type = T_OPTDATA_REQ;
38508348SEric.Yu@Sun.COM 		iosize = sti->sti_tidu_size;
38510Sstevel@tonic-gate 		if (iosize <= 0)
38520Sstevel@tonic-gate 			return (EMSGSIZE);
38530Sstevel@tonic-gate 		if (uiop->uio_resid > iosize) {
38540Sstevel@tonic-gate 			tdr.DATA_flag = 1;
38550Sstevel@tonic-gate 		} else {
38560Sstevel@tonic-gate 			if (more)
38570Sstevel@tonic-gate 				tdr.DATA_flag = 1;
38580Sstevel@tonic-gate 			else
38590Sstevel@tonic-gate 				tdr.DATA_flag = 0;
38600Sstevel@tonic-gate 			iosize = uiop->uio_resid;
38610Sstevel@tonic-gate 		}
38620Sstevel@tonic-gate 		dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
38635240Snordmark 		    tdr.DATA_flag, iosize));
38640Sstevel@tonic-gate 
38650Sstevel@tonic-gate 		optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
38660Sstevel@tonic-gate 		tdr.OPT_length = optlen;
38670Sstevel@tonic-gate 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
38680Sstevel@tonic-gate 
38690Sstevel@tonic-gate 		size = (int)sizeof (tdr) + optlen;
38700Sstevel@tonic-gate 		/*
38710Sstevel@tonic-gate 		 * File descriptors only when SM_FDPASSING set.
38720Sstevel@tonic-gate 		 */
38730Sstevel@tonic-gate 		error = so_getfdopt(control, controllen,
38745240Snordmark 		    !(flags & MSG_XPG4_2), &fds, &fdlen);
38750Sstevel@tonic-gate 		if (error)
38760Sstevel@tonic-gate 			return (error);
38770Sstevel@tonic-gate 		if (fdlen != -1) {
38780Sstevel@tonic-gate 			if (!(so->so_mode & SM_FDPASSING))
38790Sstevel@tonic-gate 				return (EOPNOTSUPP);
38800Sstevel@tonic-gate 
38810Sstevel@tonic-gate 			error = fdbuf_create(fds, fdlen, &fdbuf);
38820Sstevel@tonic-gate 			if (error)
38830Sstevel@tonic-gate 				return (error);
38840Sstevel@tonic-gate 			mp = fdbuf_allocmsg(size, fdbuf);
38850Sstevel@tonic-gate 		} else {
38868778SErik.Nordmark@Sun.COM 			mp = soallocproto(size, _ALLOC_INTR, CRED());
3887455Smeem 			if (mp == NULL) {
3888455Smeem 				/*
3889455Smeem 				 * Caught a signal waiting for memory.
3890455Smeem 				 * Let send* return EINTR.
3891455Smeem 				 */
38928348SEric.Yu@Sun.COM 				return (EINTR);
3893455Smeem 			}
38940Sstevel@tonic-gate 		}
38950Sstevel@tonic-gate 		soappendmsg(mp, &tdr, sizeof (tdr));
38960Sstevel@tonic-gate 
38970Sstevel@tonic-gate 		if (fdlen != -1) {
38980Sstevel@tonic-gate 			ASSERT(fdbuf != NULL);
38990Sstevel@tonic-gate 			toh.level = SOL_SOCKET;
39000Sstevel@tonic-gate 			toh.name = SO_FILEP;
39010Sstevel@tonic-gate 			toh.len = fdbuf->fd_size +
39025240Snordmark 			    (t_uscalar_t)sizeof (struct T_opthdr);
39030Sstevel@tonic-gate 			toh.status = 0;
39040Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
39050Sstevel@tonic-gate 			soappendmsg(mp, fdbuf, fdbuf->fd_size);
39060Sstevel@tonic-gate 			ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
39070Sstevel@tonic-gate 		}
39080Sstevel@tonic-gate 		so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
39090Sstevel@tonic-gate 		/* At most 3 bytes left in the message */
39100Sstevel@tonic-gate 		ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
39110Sstevel@tonic-gate 		ASSERT(MBLKL(mp) <= (ssize_t)size);
39120Sstevel@tonic-gate 
39130Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
39140Sstevel@tonic-gate 
39150Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
39165240Snordmark 		    0, MSG_BAND, 0);
39170Sstevel@tonic-gate 		if (error) {
39180Sstevel@tonic-gate 			eprintsoline(so, error);
39190Sstevel@tonic-gate 			return (error);
39200Sstevel@tonic-gate 		}
39210Sstevel@tonic-gate 		control = NULL;
39220Sstevel@tonic-gate 		if (uiop->uio_resid > 0) {
39230Sstevel@tonic-gate 			/*
39240Sstevel@tonic-gate 			 * Recheck for fatal errors. Fail write even though
39250Sstevel@tonic-gate 			 * some data have been written. This is consistent
39260Sstevel@tonic-gate 			 * with strwrite semantics and BSD sockets semantics.
39270Sstevel@tonic-gate 			 */
39280Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
39290Sstevel@tonic-gate 				eprintsoline(so, error);
39300Sstevel@tonic-gate 				return (EPIPE);
39310Sstevel@tonic-gate 			}
39320Sstevel@tonic-gate 			if (so->so_error != 0) {
39330Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
39348348SEric.Yu@Sun.COM 				error = sogeterr(so, B_TRUE);
39350Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
39360Sstevel@tonic-gate 				if (error != 0) {
39370Sstevel@tonic-gate 					eprintsoline(so, error);
39380Sstevel@tonic-gate 					return (error);
39390Sstevel@tonic-gate 				}
39400Sstevel@tonic-gate 			}
39410Sstevel@tonic-gate 		}
39420Sstevel@tonic-gate 	} while (uiop->uio_resid > 0);
39430Sstevel@tonic-gate 	return (0);
39440Sstevel@tonic-gate }
39450Sstevel@tonic-gate 
39460Sstevel@tonic-gate /*
39470Sstevel@tonic-gate  * Sending data on a datagram socket.
39480Sstevel@tonic-gate  * Assumes caller has verified that SS_ISBOUND etc. are set.
39490Sstevel@tonic-gate  *
39500Sstevel@tonic-gate  * For AF_UNIX the destination address is translated to an internal
39510Sstevel@tonic-gate  * name and the source address is passed as an option.
39520Sstevel@tonic-gate  */
39530Sstevel@tonic-gate int
sosend_dgram(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,int flags)3954741Smasputra sosend_dgram(struct sonode *so, struct sockaddr	*name, socklen_t namelen,
3955741Smasputra     struct uio *uiop, int flags)
39560Sstevel@tonic-gate {
39570Sstevel@tonic-gate 	struct T_unitdata_req	tudr;
39580Sstevel@tonic-gate 	mblk_t			*mp;
39590Sstevel@tonic-gate 	int			error;
39600Sstevel@tonic-gate 	void			*addr;
39610Sstevel@tonic-gate 	socklen_t		addrlen;
39620Sstevel@tonic-gate 	void			*src;
39630Sstevel@tonic-gate 	socklen_t		srclen;
39640Sstevel@tonic-gate 	ssize_t			len;
39658348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
39660Sstevel@tonic-gate 
3967741Smasputra 	ASSERT(name != NULL && namelen != 0);
39680Sstevel@tonic-gate 
39690Sstevel@tonic-gate 	len = uiop->uio_resid;
39708348SEric.Yu@Sun.COM 	if (len > sti->sti_tidu_size) {
39710Sstevel@tonic-gate 		error = EMSGSIZE;
39720Sstevel@tonic-gate 		goto done;
39730Sstevel@tonic-gate 	}
39740Sstevel@tonic-gate 
3975741Smasputra 	/* Length and family checks */
39760Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
3977741Smasputra 	if (error != 0)
39780Sstevel@tonic-gate 		goto done;
3979741Smasputra 
39808348SEric.Yu@Sun.COM 	if (sti->sti_direct)
3981741Smasputra 		return (sodgram_direct(so, name, namelen, uiop, flags));
3982741Smasputra 
39830Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
39848348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate) {
39850Sstevel@tonic-gate 			/*
39860Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
39870Sstevel@tonic-gate 			 * pass any (transport internal) source address.
39880Sstevel@tonic-gate 			 */
39890Sstevel@tonic-gate 			addr = name;
39900Sstevel@tonic-gate 			addrlen = namelen;
39910Sstevel@tonic-gate 			src = NULL;
39920Sstevel@tonic-gate 			srclen = 0;
39930Sstevel@tonic-gate 		} else {
39940Sstevel@tonic-gate 			/*
39950Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
39960Sstevel@tonic-gate 			 * and translate the remote address.
39970Sstevel@tonic-gate 			 *
39988348SEric.Yu@Sun.COM 			 * Note that this code does not prevent sti_laddr_sa
39990Sstevel@tonic-gate 			 * from changing while it is being used. Thus
40000Sstevel@tonic-gate 			 * if an unbind+bind occurs concurrently with this
40010Sstevel@tonic-gate 			 * send the peer might see a partially new and a
40020Sstevel@tonic-gate 			 * partially old "from" address.
40030Sstevel@tonic-gate 			 */
40048348SEric.Yu@Sun.COM 			src = sti->sti_laddr_sa;
40058348SEric.Yu@Sun.COM 			srclen = (socklen_t)sti->sti_laddr_len;
40060Sstevel@tonic-gate 			dprintso(so, 1,
40075240Snordmark 			    ("sosend_dgram UNIX: srclen %d, src %p\n",
40085240Snordmark 			    srclen, src));
40090Sstevel@tonic-gate 			error = so_ux_addr_xlate(so, name, namelen,
40105240Snordmark 			    (flags & MSG_XPG4_2),
40115240Snordmark 			    &addr, &addrlen);
40120Sstevel@tonic-gate 			if (error) {
40130Sstevel@tonic-gate 				eprintsoline(so, error);
40140Sstevel@tonic-gate 				goto done;
40150Sstevel@tonic-gate 			}
40160Sstevel@tonic-gate 		}
40170Sstevel@tonic-gate 	} else {
40180Sstevel@tonic-gate 		addr = name;
40190Sstevel@tonic-gate 		addrlen = namelen;
40200Sstevel@tonic-gate 		src = NULL;
40210Sstevel@tonic-gate 		srclen = 0;
40220Sstevel@tonic-gate 	}
40230Sstevel@tonic-gate 	tudr.PRIM_type = T_UNITDATA_REQ;
40240Sstevel@tonic-gate 	tudr.DEST_length = addrlen;
40250Sstevel@tonic-gate 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
40260Sstevel@tonic-gate 	if (srclen == 0) {
40270Sstevel@tonic-gate 		tudr.OPT_length = 0;
40280Sstevel@tonic-gate 		tudr.OPT_offset = 0;
40290Sstevel@tonic-gate 
40300Sstevel@tonic-gate 		mp = soallocproto2(&tudr, sizeof (tudr),
40318778SErik.Nordmark@Sun.COM 		    addr, addrlen, 0, _ALLOC_INTR, CRED());
40320Sstevel@tonic-gate 		if (mp == NULL) {
40330Sstevel@tonic-gate 			/*
40340Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
40350Sstevel@tonic-gate 			 * Let send* return EINTR.
40360Sstevel@tonic-gate 			 */
40370Sstevel@tonic-gate 			error = EINTR;
40380Sstevel@tonic-gate 			goto done;
40390Sstevel@tonic-gate 		}
40400Sstevel@tonic-gate 	} else {
40410Sstevel@tonic-gate 		/*
40420Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
40430Sstevel@tonic-gate 		 * address option.
40440Sstevel@tonic-gate 		 */
40450Sstevel@tonic-gate 		struct T_opthdr toh;
40460Sstevel@tonic-gate 		ssize_t size;
40470Sstevel@tonic-gate 
40480Sstevel@tonic-gate 		tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
40495240Snordmark 		    _TPI_ALIGN_TOPT(srclen));
40500Sstevel@tonic-gate 		tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
40515240Snordmark 		    _TPI_ALIGN_TOPT(addrlen));
40520Sstevel@tonic-gate 
40530Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
40540Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
40550Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
40560Sstevel@tonic-gate 		toh.status = 0;
40570Sstevel@tonic-gate 
40580Sstevel@tonic-gate 		size = tudr.OPT_offset + tudr.OPT_length;
40590Sstevel@tonic-gate 		mp = soallocproto2(&tudr, sizeof (tudr),
40608778SErik.Nordmark@Sun.COM 		    addr, addrlen, size, _ALLOC_INTR, CRED());
40610Sstevel@tonic-gate 		if (mp == NULL) {
40620Sstevel@tonic-gate 			/*
40630Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
40640Sstevel@tonic-gate 			 * Let send* return EINTR.
40650Sstevel@tonic-gate 			 */
40660Sstevel@tonic-gate 			error = EINTR;
40670Sstevel@tonic-gate 			goto done;
40680Sstevel@tonic-gate 		}
40690Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
40700Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
40710Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
40720Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
40730Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
40740Sstevel@tonic-gate 	}
40750Sstevel@tonic-gate 
407611861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
40770Sstevel@tonic-gate 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
40780Sstevel@tonic-gate 
40790Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
40800Sstevel@tonic-gate done:
40810Sstevel@tonic-gate #ifdef SOCK_DEBUG
40820Sstevel@tonic-gate 	if (error) {
40830Sstevel@tonic-gate 		eprintsoline(so, error);
40840Sstevel@tonic-gate 	}
40850Sstevel@tonic-gate #endif /* SOCK_DEBUG */
40860Sstevel@tonic-gate 	return (error);
40870Sstevel@tonic-gate }
40880Sstevel@tonic-gate 
40890Sstevel@tonic-gate /*
40900Sstevel@tonic-gate  * Sending data on a connected stream socket.
40910Sstevel@tonic-gate  * Assumes caller has verified that SS_ISCONNECTED is set.
40920Sstevel@tonic-gate  */
40930Sstevel@tonic-gate int
sosend_svc(struct sonode * so,struct uio * uiop,t_scalar_t prim,int more,int sflag)40948348SEric.Yu@Sun.COM sosend_svc(struct sonode *so, struct uio *uiop, t_scalar_t prim, int more,
40958348SEric.Yu@Sun.COM     int sflag)
40960Sstevel@tonic-gate {
40970Sstevel@tonic-gate 	struct T_data_req	tdr;
40980Sstevel@tonic-gate 	mblk_t			*mp;
40990Sstevel@tonic-gate 	int			error;
41000Sstevel@tonic-gate 	ssize_t			iosize;
41018348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
41020Sstevel@tonic-gate 
41030Sstevel@tonic-gate 	dprintso(so, 1,
41045240Snordmark 	    ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
41057240Srh87107 	    (void *)so, uiop->uio_resid, prim, sflag));
41060Sstevel@tonic-gate 
41070Sstevel@tonic-gate 	/*
41080Sstevel@tonic-gate 	 * Has to be bound and connected. However, since no locks are
41090Sstevel@tonic-gate 	 * held the state could have changed after sotpi_sendmsg checked it
41100Sstevel@tonic-gate 	 * thus it is not possible to ASSERT on the state.
41110Sstevel@tonic-gate 	 */
41120Sstevel@tonic-gate 
41130Sstevel@tonic-gate 	do {
41140Sstevel@tonic-gate 		/*
41150Sstevel@tonic-gate 		 * Set the MORE flag if uio_resid does not fit in this
41160Sstevel@tonic-gate 		 * message or if the caller passed in "more".
41170Sstevel@tonic-gate 		 * Error for transports with zero tidu_size.
41180Sstevel@tonic-gate 		 */
41190Sstevel@tonic-gate 		tdr.PRIM_type = prim;
41208348SEric.Yu@Sun.COM 		iosize = sti->sti_tidu_size;
41210Sstevel@tonic-gate 		if (iosize <= 0)
41220Sstevel@tonic-gate 			return (EMSGSIZE);
41230Sstevel@tonic-gate 		if (uiop->uio_resid > iosize) {
41240Sstevel@tonic-gate 			tdr.MORE_flag = 1;
41250Sstevel@tonic-gate 		} else {
41260Sstevel@tonic-gate 			if (more)
41270Sstevel@tonic-gate 				tdr.MORE_flag = 1;
41280Sstevel@tonic-gate 			else
41290Sstevel@tonic-gate 				tdr.MORE_flag = 0;
41300Sstevel@tonic-gate 			iosize = uiop->uio_resid;
41310Sstevel@tonic-gate 		}
41320Sstevel@tonic-gate 		dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
41335240Snordmark 		    prim, tdr.MORE_flag, iosize));
41348778SErik.Nordmark@Sun.COM 		mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR, CRED());
41350Sstevel@tonic-gate 		if (mp == NULL) {
41360Sstevel@tonic-gate 			/*
41370Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
41380Sstevel@tonic-gate 			 * Let send* return EINTR.
41390Sstevel@tonic-gate 			 */
41408348SEric.Yu@Sun.COM 			return (EINTR);
41410Sstevel@tonic-gate 		}
41420Sstevel@tonic-gate 
41430Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
41445240Snordmark 		    0, sflag | MSG_BAND, 0);
41450Sstevel@tonic-gate 		if (error) {
41460Sstevel@tonic-gate 			eprintsoline(so, error);
41470Sstevel@tonic-gate 			return (error);
41480Sstevel@tonic-gate 		}
41490Sstevel@tonic-gate 		if (uiop->uio_resid > 0) {
41500Sstevel@tonic-gate 			/*
41510Sstevel@tonic-gate 			 * Recheck for fatal errors. Fail write even though
41520Sstevel@tonic-gate 			 * some data have been written. This is consistent
41530Sstevel@tonic-gate 			 * with strwrite semantics and BSD sockets semantics.
41540Sstevel@tonic-gate 			 */
41550Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
41560Sstevel@tonic-gate 				eprintsoline(so, error);
41570Sstevel@tonic-gate 				return (EPIPE);
41580Sstevel@tonic-gate 			}
41590Sstevel@tonic-gate 			if (so->so_error != 0) {
41600Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
41618348SEric.Yu@Sun.COM 				error = sogeterr(so, B_TRUE);
41620Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
41630Sstevel@tonic-gate 				if (error != 0) {
41640Sstevel@tonic-gate 					eprintsoline(so, error);
41650Sstevel@tonic-gate 					return (error);
41660Sstevel@tonic-gate 				}
41670Sstevel@tonic-gate 			}
41680Sstevel@tonic-gate 		}
41690Sstevel@tonic-gate 	} while (uiop->uio_resid > 0);
41700Sstevel@tonic-gate 	return (0);
41710Sstevel@tonic-gate }
41720Sstevel@tonic-gate 
41730Sstevel@tonic-gate /*
41740Sstevel@tonic-gate  * Check the state for errors and call the appropriate send function.
41750Sstevel@tonic-gate  *
41760Sstevel@tonic-gate  * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
41770Sstevel@tonic-gate  * this function issues a setsockopt to toggle SO_DONTROUTE before and
41780Sstevel@tonic-gate  * after sending the message.
41790Sstevel@tonic-gate  */
41800Sstevel@tonic-gate static int
sotpi_sendmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop,struct cred * cr)41818348SEric.Yu@Sun.COM sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
41828348SEric.Yu@Sun.COM     struct cred *cr)
41830Sstevel@tonic-gate {
41840Sstevel@tonic-gate 	int		so_state;
41850Sstevel@tonic-gate 	int		so_mode;
41860Sstevel@tonic-gate 	int		error;
41870Sstevel@tonic-gate 	struct sockaddr *name;
41880Sstevel@tonic-gate 	t_uscalar_t	namelen;
41890Sstevel@tonic-gate 	int		dontroute;
41900Sstevel@tonic-gate 	int		flags;
41918348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
41920Sstevel@tonic-gate 
41930Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
41947240Srh87107 	    (void *)so, (void *)msg, msg->msg_flags,
41955240Snordmark 	    pr_state(so->so_state, so->so_mode), so->so_error));
41960Sstevel@tonic-gate 
41978348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
41988348SEric.Yu@Sun.COM 		/* The imaginary "sockmod" has been popped - act as a stream */
41998348SEric.Yu@Sun.COM 		so_update_attrs(so, SOMOD);
42008348SEric.Yu@Sun.COM 		return (strwrite(SOTOV(so), uiop, cr));
42018348SEric.Yu@Sun.COM 	}
42028348SEric.Yu@Sun.COM 
42030Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
42040Sstevel@tonic-gate 	so_state = so->so_state;
42050Sstevel@tonic-gate 
42060Sstevel@tonic-gate 	if (so_state & SS_CANTSENDMORE) {
42070Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
42080Sstevel@tonic-gate 		return (EPIPE);
42090Sstevel@tonic-gate 	}
42100Sstevel@tonic-gate 
42110Sstevel@tonic-gate 	if (so->so_error != 0) {
42128348SEric.Yu@Sun.COM 		error = sogeterr(so, B_TRUE);
42130Sstevel@tonic-gate 		if (error != 0) {
42140Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42150Sstevel@tonic-gate 			return (error);
42160Sstevel@tonic-gate 		}
42170Sstevel@tonic-gate 	}
42180Sstevel@tonic-gate 
42190Sstevel@tonic-gate 	name = (struct sockaddr *)msg->msg_name;
42200Sstevel@tonic-gate 	namelen = msg->msg_namelen;
42210Sstevel@tonic-gate 
42220Sstevel@tonic-gate 	so_mode = so->so_mode;
42230Sstevel@tonic-gate 
42240Sstevel@tonic-gate 	if (name == NULL) {
42250Sstevel@tonic-gate 		if (!(so_state & SS_ISCONNECTED)) {
42260Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42270Sstevel@tonic-gate 			if (so_mode & SM_CONNREQUIRED)
42280Sstevel@tonic-gate 				return (ENOTCONN);
42290Sstevel@tonic-gate 			else
42300Sstevel@tonic-gate 				return (EDESTADDRREQ);
42310Sstevel@tonic-gate 		}
42320Sstevel@tonic-gate 		if (so_mode & SM_CONNREQUIRED) {
42330Sstevel@tonic-gate 			name = NULL;
42340Sstevel@tonic-gate 			namelen = 0;
42350Sstevel@tonic-gate 		} else {
42360Sstevel@tonic-gate 			/*
42378348SEric.Yu@Sun.COM 			 * Note that this code does not prevent sti_faddr_sa
42380Sstevel@tonic-gate 			 * from changing while it is being used. Thus
42390Sstevel@tonic-gate 			 * if an "unconnect"+connect occurs concurrently with
42400Sstevel@tonic-gate 			 * this send the datagram might be delivered to a
42410Sstevel@tonic-gate 			 * garbaled address.
42420Sstevel@tonic-gate 			 */
42438348SEric.Yu@Sun.COM 			ASSERT(sti->sti_faddr_sa);
42448348SEric.Yu@Sun.COM 			name = sti->sti_faddr_sa;
42458348SEric.Yu@Sun.COM 			namelen = (t_uscalar_t)sti->sti_faddr_len;
42460Sstevel@tonic-gate 		}
42470Sstevel@tonic-gate 	} else {
42480Sstevel@tonic-gate 		if (!(so_state & SS_ISCONNECTED) &&
42490Sstevel@tonic-gate 		    (so_mode & SM_CONNREQUIRED)) {
42500Sstevel@tonic-gate 			/* Required but not connected */
42510Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42520Sstevel@tonic-gate 			return (ENOTCONN);
42530Sstevel@tonic-gate 		}
42540Sstevel@tonic-gate 		/*
42550Sstevel@tonic-gate 		 * Ignore the address on connection-oriented sockets.
42560Sstevel@tonic-gate 		 * Just like BSD this code does not generate an error for
42570Sstevel@tonic-gate 		 * TCP (a CONNREQUIRED socket) when sending to an address
42580Sstevel@tonic-gate 		 * passed in with sendto/sendmsg. Instead the data is
42590Sstevel@tonic-gate 		 * delivered on the connection as if no address had been
42600Sstevel@tonic-gate 		 * supplied.
42610Sstevel@tonic-gate 		 */
42620Sstevel@tonic-gate 		if ((so_state & SS_ISCONNECTED) &&
42630Sstevel@tonic-gate 		    !(so_mode & SM_CONNREQUIRED)) {
42640Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42650Sstevel@tonic-gate 			return (EISCONN);
42660Sstevel@tonic-gate 		}
42670Sstevel@tonic-gate 		if (!(so_state & SS_ISBOUND)) {
42680Sstevel@tonic-gate 			so_lock_single(so);	/* Set SOLOCKED */
42690Sstevel@tonic-gate 			error = sotpi_bind(so, NULL, 0,
42708348SEric.Yu@Sun.COM 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
42710Sstevel@tonic-gate 			so_unlock_single(so, SOLOCKED);
42720Sstevel@tonic-gate 			if (error) {
42730Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
42740Sstevel@tonic-gate 				eprintsoline(so, error);
42750Sstevel@tonic-gate 				return (error);
42760Sstevel@tonic-gate 			}
42770Sstevel@tonic-gate 		}
42780Sstevel@tonic-gate 		/*
42790Sstevel@tonic-gate 		 * Handle delayed datagram errors. These are only queued
42800Sstevel@tonic-gate 		 * when the application sets SO_DGRAM_ERRIND.
42810Sstevel@tonic-gate 		 * Return the error if we are sending to the address
42820Sstevel@tonic-gate 		 * that was returned in the last T_UDERROR_IND.
42830Sstevel@tonic-gate 		 * If sending to some other address discard the delayed
42840Sstevel@tonic-gate 		 * error indication.
42850Sstevel@tonic-gate 		 */
42868348SEric.Yu@Sun.COM 		if (sti->sti_delayed_error) {
42870Sstevel@tonic-gate 			struct T_uderror_ind	*tudi;
42880Sstevel@tonic-gate 			void			*addr;
42890Sstevel@tonic-gate 			t_uscalar_t		addrlen;
42900Sstevel@tonic-gate 			boolean_t		match = B_FALSE;
42910Sstevel@tonic-gate 
42928348SEric.Yu@Sun.COM 			ASSERT(sti->sti_eaddr_mp);
42938348SEric.Yu@Sun.COM 			error = sti->sti_delayed_error;
42948348SEric.Yu@Sun.COM 			sti->sti_delayed_error = 0;
42958348SEric.Yu@Sun.COM 			tudi =
42968348SEric.Yu@Sun.COM 			    (struct T_uderror_ind *)sti->sti_eaddr_mp->b_rptr;
42970Sstevel@tonic-gate 			addrlen = tudi->DEST_length;
42988348SEric.Yu@Sun.COM 			addr = sogetoff(sti->sti_eaddr_mp,
42998348SEric.Yu@Sun.COM 			    tudi->DEST_offset, addrlen, 1);
43000Sstevel@tonic-gate 			ASSERT(addr);	/* Checked by strsock_proto */
43010Sstevel@tonic-gate 			switch (so->so_family) {
43020Sstevel@tonic-gate 			case AF_INET: {
43030Sstevel@tonic-gate 				/* Compare just IP address and port */
43040Sstevel@tonic-gate 				sin_t *sin1 = (sin_t *)name;
43050Sstevel@tonic-gate 				sin_t *sin2 = (sin_t *)addr;
43060Sstevel@tonic-gate 
43070Sstevel@tonic-gate 				if (addrlen == sizeof (sin_t) &&
43080Sstevel@tonic-gate 				    namelen == addrlen &&
43090Sstevel@tonic-gate 				    sin1->sin_port == sin2->sin_port &&
43100Sstevel@tonic-gate 				    sin1->sin_addr.s_addr ==
43110Sstevel@tonic-gate 				    sin2->sin_addr.s_addr)
43120Sstevel@tonic-gate 					match = B_TRUE;
43130Sstevel@tonic-gate 				break;
43140Sstevel@tonic-gate 			}
43150Sstevel@tonic-gate 			case AF_INET6: {
43160Sstevel@tonic-gate 				/* Compare just IP address and port. Not flow */
43170Sstevel@tonic-gate 				sin6_t *sin1 = (sin6_t *)name;
43180Sstevel@tonic-gate 				sin6_t *sin2 = (sin6_t *)addr;
43190Sstevel@tonic-gate 
43200Sstevel@tonic-gate 				if (addrlen == sizeof (sin6_t) &&
43210Sstevel@tonic-gate 				    namelen == addrlen &&
43220Sstevel@tonic-gate 				    sin1->sin6_port == sin2->sin6_port &&
43230Sstevel@tonic-gate 				    IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
43245240Snordmark 				    &sin2->sin6_addr))
43250Sstevel@tonic-gate 					match = B_TRUE;
43260Sstevel@tonic-gate 				break;
43270Sstevel@tonic-gate 			}
43280Sstevel@tonic-gate 			case AF_UNIX:
43290Sstevel@tonic-gate 			default:
43300Sstevel@tonic-gate 				if (namelen == addrlen &&
43310Sstevel@tonic-gate 				    bcmp(name, addr, namelen) == 0)
43320Sstevel@tonic-gate 					match = B_TRUE;
43330Sstevel@tonic-gate 			}
43340Sstevel@tonic-gate 			if (match) {
43358348SEric.Yu@Sun.COM 				freemsg(sti->sti_eaddr_mp);
43368348SEric.Yu@Sun.COM 				sti->sti_eaddr_mp = NULL;
43370Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
43380Sstevel@tonic-gate #ifdef DEBUG
43390Sstevel@tonic-gate 				dprintso(so, 0,
43405240Snordmark 				    ("sockfs delayed error %d for %s\n",
43415240Snordmark 				    error,
43425240Snordmark 				    pr_addr(so->so_family, name, namelen)));
43430Sstevel@tonic-gate #endif /* DEBUG */
43440Sstevel@tonic-gate 				return (error);
43450Sstevel@tonic-gate 			}
43468348SEric.Yu@Sun.COM 			freemsg(sti->sti_eaddr_mp);
43478348SEric.Yu@Sun.COM 			sti->sti_eaddr_mp = NULL;
43480Sstevel@tonic-gate 		}
43490Sstevel@tonic-gate 	}
43500Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
43510Sstevel@tonic-gate 
43520Sstevel@tonic-gate 	flags = msg->msg_flags;
43530Sstevel@tonic-gate 	dontroute = 0;
43540Sstevel@tonic-gate 	if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
43550Sstevel@tonic-gate 		uint32_t	val;
43560Sstevel@tonic-gate 
43570Sstevel@tonic-gate 		val = 1;
43580Sstevel@tonic-gate 		error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
43598348SEric.Yu@Sun.COM 		    &val, (t_uscalar_t)sizeof (val), cr);
43600Sstevel@tonic-gate 		if (error)
43610Sstevel@tonic-gate 			return (error);
43620Sstevel@tonic-gate 		dontroute = 1;
43630Sstevel@tonic-gate 	}
43640Sstevel@tonic-gate 
43650Sstevel@tonic-gate 	if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
43660Sstevel@tonic-gate 		error = EOPNOTSUPP;
43670Sstevel@tonic-gate 		goto done;
43680Sstevel@tonic-gate 	}
43690Sstevel@tonic-gate 	if (msg->msg_controllen != 0) {
43700Sstevel@tonic-gate 		if (!(so_mode & SM_CONNREQUIRED)) {
43718348SEric.Yu@Sun.COM 			so_update_attrs(so, SOMOD);
43720Sstevel@tonic-gate 			error = sosend_dgramcmsg(so, name, namelen, uiop,
4373741Smasputra 			    msg->msg_control, msg->msg_controllen, flags);
43740Sstevel@tonic-gate 		} else {
43750Sstevel@tonic-gate 			if (flags & MSG_OOB) {
43760Sstevel@tonic-gate 				/* Can't generate T_EXDATA_REQ with options */
43770Sstevel@tonic-gate 				error = EOPNOTSUPP;
43780Sstevel@tonic-gate 				goto done;
43790Sstevel@tonic-gate 			}
43808348SEric.Yu@Sun.COM 			so_update_attrs(so, SOMOD);
43810Sstevel@tonic-gate 			error = sosend_svccmsg(so, uiop,
43825240Snordmark 			    !(flags & MSG_EOR),
43835240Snordmark 			    msg->msg_control, msg->msg_controllen,
43845240Snordmark 			    flags);
43850Sstevel@tonic-gate 		}
43860Sstevel@tonic-gate 		goto done;
43870Sstevel@tonic-gate 	}
43880Sstevel@tonic-gate 
43898348SEric.Yu@Sun.COM 	so_update_attrs(so, SOMOD);
43900Sstevel@tonic-gate 	if (!(so_mode & SM_CONNREQUIRED)) {
43910Sstevel@tonic-gate 		/*
43920Sstevel@tonic-gate 		 * If there is no SO_DONTROUTE to turn off return immediately
4393741Smasputra 		 * from send_dgram. This can allow tail-call optimizations.
43940Sstevel@tonic-gate 		 */
43950Sstevel@tonic-gate 		if (!dontroute) {
43960Sstevel@tonic-gate 			return (sosend_dgram(so, name, namelen, uiop, flags));
43970Sstevel@tonic-gate 		}
43980Sstevel@tonic-gate 		error = sosend_dgram(so, name, namelen, uiop, flags);
43990Sstevel@tonic-gate 	} else {
44000Sstevel@tonic-gate 		t_scalar_t prim;
44010Sstevel@tonic-gate 		int sflag;
44020Sstevel@tonic-gate 
44030Sstevel@tonic-gate 		/* Ignore msg_name in the connected state */
44040Sstevel@tonic-gate 		if (flags & MSG_OOB) {
44050Sstevel@tonic-gate 			prim = T_EXDATA_REQ;
44060Sstevel@tonic-gate 			/*
44070Sstevel@tonic-gate 			 * Send down T_EXDATA_REQ even if there is flow
44080Sstevel@tonic-gate 			 * control for data.
44090Sstevel@tonic-gate 			 */
44100Sstevel@tonic-gate 			sflag = MSG_IGNFLOW;
44110Sstevel@tonic-gate 		} else {
44120Sstevel@tonic-gate 			if (so_mode & SM_BYTESTREAM) {
44130Sstevel@tonic-gate 				/* Byte stream transport - use write */
44140Sstevel@tonic-gate 				dprintso(so, 1, ("sotpi_sendmsg: write\n"));
44158348SEric.Yu@Sun.COM 
44168348SEric.Yu@Sun.COM 				/* Send M_DATA messages */
44178348SEric.Yu@Sun.COM 				if ((sti->sti_nl7c_flags & NL7C_ENABLED) &&
44188348SEric.Yu@Sun.COM 				    (error = nl7c_data(so, uiop)) >= 0) {
44198348SEric.Yu@Sun.COM 					/* NL7C consumed the data */
44208348SEric.Yu@Sun.COM 					return (error);
44218348SEric.Yu@Sun.COM 				}
44220Sstevel@tonic-gate 				/*
4423741Smasputra 				 * If there is no SO_DONTROUTE to turn off,
44248348SEric.Yu@Sun.COM 				 * sti_direct is on, and there is no flow
4425741Smasputra 				 * control, we can take the fast path.
44260Sstevel@tonic-gate 				 */
44278348SEric.Yu@Sun.COM 				if (!dontroute && sti->sti_direct != 0 &&
4428741Smasputra 				    canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4429741Smasputra 					return (sostream_direct(so, uiop,
44308348SEric.Yu@Sun.COM 					    NULL, cr));
4431741Smasputra 				}
44328348SEric.Yu@Sun.COM 				error = strwrite(SOTOV(so), uiop, cr);
44330Sstevel@tonic-gate 				goto done;
44340Sstevel@tonic-gate 			}
44350Sstevel@tonic-gate 			prim = T_DATA_REQ;
44360Sstevel@tonic-gate 			sflag = 0;
44370Sstevel@tonic-gate 		}
44380Sstevel@tonic-gate 		/*
44390Sstevel@tonic-gate 		 * If there is no SO_DONTROUTE to turn off return immediately
44400Sstevel@tonic-gate 		 * from sosend_svc. This can allow tail-call optimizations.
44410Sstevel@tonic-gate 		 */
44420Sstevel@tonic-gate 		if (!dontroute)
44430Sstevel@tonic-gate 			return (sosend_svc(so, uiop, prim,
44445240Snordmark 			    !(flags & MSG_EOR), sflag));
44450Sstevel@tonic-gate 		error = sosend_svc(so, uiop, prim,
44465240Snordmark 		    !(flags & MSG_EOR), sflag);
44470Sstevel@tonic-gate 	}
44480Sstevel@tonic-gate 	ASSERT(dontroute);
44490Sstevel@tonic-gate done:
44500Sstevel@tonic-gate 	if (dontroute) {
44510Sstevel@tonic-gate 		uint32_t	val;
44520Sstevel@tonic-gate 
44530Sstevel@tonic-gate 		val = 0;
44540Sstevel@tonic-gate 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
44558348SEric.Yu@Sun.COM 		    &val, (t_uscalar_t)sizeof (val), cr);
44568348SEric.Yu@Sun.COM 	}
44578348SEric.Yu@Sun.COM 	return (error);
44588348SEric.Yu@Sun.COM }
44598348SEric.Yu@Sun.COM 
44608348SEric.Yu@Sun.COM /*
44618348SEric.Yu@Sun.COM  * kstrwritemp() has very similar semantics as that of strwrite().
44628348SEric.Yu@Sun.COM  * The main difference is it obtains mblks from the caller and also
44638348SEric.Yu@Sun.COM  * does not do any copy as done in strwrite() from user buffers to
44648348SEric.Yu@Sun.COM  * kernel buffers.
44658348SEric.Yu@Sun.COM  *
44668348SEric.Yu@Sun.COM  * Currently, this routine is used by sendfile to send data allocated
44678348SEric.Yu@Sun.COM  * within the kernel without any copying. This interface does not use the
44688348SEric.Yu@Sun.COM  * synchronous stream interface as synch. stream interface implies
44698348SEric.Yu@Sun.COM  * copying.
44708348SEric.Yu@Sun.COM  */
44718348SEric.Yu@Sun.COM int
kstrwritemp(struct vnode * vp,mblk_t * mp,ushort_t fmode)44728348SEric.Yu@Sun.COM kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode)
44738348SEric.Yu@Sun.COM {
44748348SEric.Yu@Sun.COM 	struct stdata *stp;
44758348SEric.Yu@Sun.COM 	struct queue *wqp;
44768348SEric.Yu@Sun.COM 	mblk_t *newmp;
44778348SEric.Yu@Sun.COM 	char waitflag;
44788348SEric.Yu@Sun.COM 	int tempmode;
44798348SEric.Yu@Sun.COM 	int error = 0;
44808348SEric.Yu@Sun.COM 	int done = 0;
44818348SEric.Yu@Sun.COM 	struct sonode *so;
44828348SEric.Yu@Sun.COM 	boolean_t direct;
44838348SEric.Yu@Sun.COM 
44848348SEric.Yu@Sun.COM 	ASSERT(vp->v_stream);
44858348SEric.Yu@Sun.COM 	stp = vp->v_stream;
44868348SEric.Yu@Sun.COM 
44878348SEric.Yu@Sun.COM 	so = VTOSO(vp);
44888348SEric.Yu@Sun.COM 	direct = _SOTOTPI(so)->sti_direct;
44898348SEric.Yu@Sun.COM 
44908348SEric.Yu@Sun.COM 	/*
44918348SEric.Yu@Sun.COM 	 * This is the sockfs direct fast path. canputnext() need
44928348SEric.Yu@Sun.COM 	 * not be accurate so we don't grab the sd_lock here. If
44938348SEric.Yu@Sun.COM 	 * we get flow-controlled, we grab sd_lock just before the
44948348SEric.Yu@Sun.COM 	 * do..while loop below to emulate what strwrite() does.
44958348SEric.Yu@Sun.COM 	 */
44968348SEric.Yu@Sun.COM 	wqp = stp->sd_wrq;
44978348SEric.Yu@Sun.COM 	if (canputnext(wqp) && direct &&
44988348SEric.Yu@Sun.COM 	    !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
44998348SEric.Yu@Sun.COM 		return (sostream_direct(so, NULL, mp, CRED()));
45008348SEric.Yu@Sun.COM 	} else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
45018348SEric.Yu@Sun.COM 		/* Fast check of flags before acquiring the lock */
45028348SEric.Yu@Sun.COM 		mutex_enter(&stp->sd_lock);
45038348SEric.Yu@Sun.COM 		error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
45048348SEric.Yu@Sun.COM 		mutex_exit(&stp->sd_lock);
45058348SEric.Yu@Sun.COM 		if (error != 0) {
45068348SEric.Yu@Sun.COM 			if (!(stp->sd_flag & STPLEX) &&
45078348SEric.Yu@Sun.COM 			    (stp->sd_wput_opt & SW_SIGPIPE)) {
45088348SEric.Yu@Sun.COM 				error = EPIPE;
45098348SEric.Yu@Sun.COM 			}
45108348SEric.Yu@Sun.COM 			return (error);
45118348SEric.Yu@Sun.COM 		}
45128348SEric.Yu@Sun.COM 	}
45138348SEric.Yu@Sun.COM 
45148348SEric.Yu@Sun.COM 	waitflag = WRITEWAIT;
45158348SEric.Yu@Sun.COM 	if (stp->sd_flag & OLDNDELAY)
45168348SEric.Yu@Sun.COM 		tempmode = fmode & ~FNDELAY;
45178348SEric.Yu@Sun.COM 	else
45188348SEric.Yu@Sun.COM 		tempmode = fmode;
45198348SEric.Yu@Sun.COM 
45208348SEric.Yu@Sun.COM 	mutex_enter(&stp->sd_lock);
45218348SEric.Yu@Sun.COM 	do {
45228348SEric.Yu@Sun.COM 		if (canputnext(wqp)) {
45238348SEric.Yu@Sun.COM 			mutex_exit(&stp->sd_lock);
45248348SEric.Yu@Sun.COM 			if (stp->sd_wputdatafunc != NULL) {
45258348SEric.Yu@Sun.COM 				newmp = (stp->sd_wputdatafunc)(vp, mp, NULL,
45268348SEric.Yu@Sun.COM 				    NULL, NULL, NULL);
45278348SEric.Yu@Sun.COM 				if (newmp == NULL) {
45288348SEric.Yu@Sun.COM 					/* The caller will free mp */
45298348SEric.Yu@Sun.COM 					return (ECOMM);
45308348SEric.Yu@Sun.COM 				}
45318348SEric.Yu@Sun.COM 				mp = newmp;
45328348SEric.Yu@Sun.COM 			}
45338348SEric.Yu@Sun.COM 			putnext(wqp, mp);
45348348SEric.Yu@Sun.COM 			return (0);
45358348SEric.Yu@Sun.COM 		}
45368348SEric.Yu@Sun.COM 		error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1,
45378348SEric.Yu@Sun.COM 		    &done);
45388348SEric.Yu@Sun.COM 	} while (error == 0 && !done);
45398348SEric.Yu@Sun.COM 
45408348SEric.Yu@Sun.COM 	mutex_exit(&stp->sd_lock);
45418348SEric.Yu@Sun.COM 	/*
45428348SEric.Yu@Sun.COM 	 * EAGAIN tells the application to try again. ENOMEM
45438348SEric.Yu@Sun.COM 	 * is returned only if the memory allocation size
45448348SEric.Yu@Sun.COM 	 * exceeds the physical limits of the system. ENOMEM
45458348SEric.Yu@Sun.COM 	 * can't be true here.
45468348SEric.Yu@Sun.COM 	 */
45478348SEric.Yu@Sun.COM 	if (error == ENOMEM)
45488348SEric.Yu@Sun.COM 		error = EAGAIN;
45498348SEric.Yu@Sun.COM 	return (error);
45508348SEric.Yu@Sun.COM }
45518348SEric.Yu@Sun.COM 
45528348SEric.Yu@Sun.COM /* ARGSUSED */
45538348SEric.Yu@Sun.COM static int
sotpi_sendmblk(struct sonode * so,struct nmsghdr * msg,int fflag,struct cred * cr,mblk_t ** mpp)45548348SEric.Yu@Sun.COM sotpi_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
45558348SEric.Yu@Sun.COM     struct cred *cr, mblk_t **mpp)
45568348SEric.Yu@Sun.COM {
45578348SEric.Yu@Sun.COM 	int error;
45588348SEric.Yu@Sun.COM 
45598348SEric.Yu@Sun.COM 	if (so->so_family != AF_INET && so->so_family != AF_INET6)
45608348SEric.Yu@Sun.COM 		return (EAFNOSUPPORT);
45618348SEric.Yu@Sun.COM 
45628348SEric.Yu@Sun.COM 	if (so->so_state & SS_CANTSENDMORE)
45638348SEric.Yu@Sun.COM 		return (EPIPE);
45648348SEric.Yu@Sun.COM 
45658348SEric.Yu@Sun.COM 	if (so->so_type != SOCK_STREAM)
45668348SEric.Yu@Sun.COM 		return (EOPNOTSUPP);
45678348SEric.Yu@Sun.COM 
45688348SEric.Yu@Sun.COM 	if ((so->so_state & SS_ISCONNECTED) == 0)
45698348SEric.Yu@Sun.COM 		return (ENOTCONN);
45708348SEric.Yu@Sun.COM 
45718348SEric.Yu@Sun.COM 	error = kstrwritemp(so->so_vnode, *mpp, fflag);
45728348SEric.Yu@Sun.COM 	if (error == 0)
45738348SEric.Yu@Sun.COM 		*mpp = NULL;
45740Sstevel@tonic-gate 	return (error);
45750Sstevel@tonic-gate }
45760Sstevel@tonic-gate 
45770Sstevel@tonic-gate /*
4578741Smasputra  * Sending data on a datagram socket.
4579741Smasputra  * Assumes caller has verified that SS_ISBOUND etc. are set.
4580741Smasputra  */
4581741Smasputra /* ARGSUSED */
4582741Smasputra static int
sodgram_direct(struct sonode * so,struct sockaddr * name,socklen_t namelen,struct uio * uiop,int flags)4583741Smasputra sodgram_direct(struct sonode *so, struct sockaddr *name,
4584741Smasputra     socklen_t namelen, struct uio *uiop, int flags)
4585741Smasputra {
4586741Smasputra 	struct T_unitdata_req	tudr;
45875240Snordmark 	mblk_t			*mp = NULL;
4588741Smasputra 	int			error = 0;
4589741Smasputra 	void			*addr;
4590741Smasputra 	socklen_t		addrlen;
4591741Smasputra 	ssize_t			len;
4592741Smasputra 	struct stdata		*stp = SOTOV(so)->v_stream;
4593741Smasputra 	int			so_state;
4594741Smasputra 	queue_t			*udp_wq;
45955240Snordmark 	boolean_t		connected;
45965240Snordmark 	mblk_t			*mpdata = NULL;
45978348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
459811861SMarek.Pospisil@Sun.COM 	uint32_t		auditing = AU_AUDITING();
4599741Smasputra 
4600741Smasputra 	ASSERT(name != NULL && namelen != 0);
4601741Smasputra 	ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4602741Smasputra 	ASSERT(!(so->so_mode & SM_EXDATA));
4603741Smasputra 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4604741Smasputra 	ASSERT(SOTOV(so)->v_type == VSOCK);
4605741Smasputra 
4606741Smasputra 	/* Caller checked for proper length */
4607741Smasputra 	len = uiop->uio_resid;
46088348SEric.Yu@Sun.COM 	ASSERT(len <= sti->sti_tidu_size);
4609741Smasputra 
4610741Smasputra 	/* Length and family checks have been done by caller */
4611741Smasputra 	ASSERT(name->sa_family == so->so_family);
4612741Smasputra 	ASSERT(so->so_family == AF_INET ||
4613741Smasputra 	    (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4614741Smasputra 	ASSERT(so->so_family == AF_INET6 ||
4615741Smasputra 	    (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4616741Smasputra 
4617741Smasputra 	addr = name;
4618741Smasputra 	addrlen = namelen;
4619741Smasputra 
4620741Smasputra 	if (stp->sd_sidp != NULL &&
4621741Smasputra 	    (error = straccess(stp, JCWRITE)) != 0)
4622741Smasputra 		goto done;
4623741Smasputra 
4624741Smasputra 	so_state = so->so_state;
4625741Smasputra 
46265240Snordmark 	connected = so_state & SS_ISCONNECTED;
46275240Snordmark 	if (!connected) {
46285240Snordmark 		tudr.PRIM_type = T_UNITDATA_REQ;
46295240Snordmark 		tudr.DEST_length = addrlen;
46305240Snordmark 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
46315240Snordmark 		tudr.OPT_length = 0;
46325240Snordmark 		tudr.OPT_offset = 0;
46335240Snordmark 
46345240Snordmark 		mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0,
46358778SErik.Nordmark@Sun.COM 		    _ALLOC_INTR, CRED());
46365240Snordmark 		if (mp == NULL) {
46375240Snordmark 			/*
46385240Snordmark 			 * Caught a signal waiting for memory.
46395240Snordmark 			 * Let send* return EINTR.
46405240Snordmark 			 */
46415240Snordmark 			error = EINTR;
46425240Snordmark 			goto done;
46435240Snordmark 		}
46445240Snordmark 	}
46455240Snordmark 
4646741Smasputra 	/*
4647741Smasputra 	 * For UDP we don't break up the copyin into smaller pieces
4648741Smasputra 	 * as in the TCP case.  That means if ENOMEM is returned by
4649741Smasputra 	 * mcopyinuio() then the uio vector has not been modified at
4650741Smasputra 	 * all and we fallback to either strwrite() or kstrputmsg()
4651741Smasputra 	 * below.  Note also that we never generate priority messages
4652741Smasputra 	 * from here.
4653741Smasputra 	 */
4654741Smasputra 	udp_wq = stp->sd_wrq->q_next;
4655741Smasputra 	if (canput(udp_wq) &&
46565240Snordmark 	    (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
46575240Snordmark 		ASSERT(DB_TYPE(mpdata) == M_DATA);
4658741Smasputra 		ASSERT(uiop->uio_resid == 0);
46595240Snordmark 		if (!connected)
46605240Snordmark 			linkb(mp, mpdata);
46615240Snordmark 		else
46625240Snordmark 			mp = mpdata;
466311861SMarek.Pospisil@Sun.COM 		if (auditing)
4664741Smasputra 			audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
46655240Snordmark 
46665240Snordmark 		udp_wput(udp_wq, mp);
4667741Smasputra 		return (0);
4668741Smasputra 	}
46695240Snordmark 
46705240Snordmark 	ASSERT(mpdata == NULL);
46715240Snordmark 	if (error != 0 && error != ENOMEM) {
46725240Snordmark 		freemsg(mp);
4673741Smasputra 		return (error);
46745240Snordmark 	}
4675741Smasputra 
4676741Smasputra 	/*
4677741Smasputra 	 * For connected, let strwrite() handle the blocking case.
4678741Smasputra 	 * Otherwise we fall thru and use kstrputmsg().
4679741Smasputra 	 */
46805240Snordmark 	if (connected)
4681741Smasputra 		return (strwrite(SOTOV(so), uiop, CRED()));
4682741Smasputra 
468311861SMarek.Pospisil@Sun.COM 	if (auditing)
4684741Smasputra 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4685741Smasputra 
4686741Smasputra 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4687741Smasputra done:
4688741Smasputra #ifdef SOCK_DEBUG
4689741Smasputra 	if (error != 0) {
4690741Smasputra 		eprintsoline(so, error);
4691741Smasputra 	}
4692741Smasputra #endif /* SOCK_DEBUG */
4693741Smasputra 	return (error);
4694741Smasputra }
4695741Smasputra 
4696741Smasputra int
sostream_direct(struct sonode * so,struct uio * uiop,mblk_t * mp,cred_t * cr)4697741Smasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4698741Smasputra {
4699741Smasputra 	struct stdata *stp = SOTOV(so)->v_stream;
4700741Smasputra 	ssize_t iosize, rmax, maxblk;
4701741Smasputra 	queue_t *tcp_wq = stp->sd_wrq->q_next;
4702898Skais 	mblk_t *newmp;
4703741Smasputra 	int error = 0, wflag = 0;
4704741Smasputra 
4705741Smasputra 	ASSERT(so->so_mode & SM_BYTESTREAM);
4706741Smasputra 	ASSERT(SOTOV(so)->v_type == VSOCK);
4707741Smasputra 
4708741Smasputra 	if (stp->sd_sidp != NULL &&
4709741Smasputra 	    (error = straccess(stp, JCWRITE)) != 0)
4710741Smasputra 		return (error);
4711741Smasputra 
4712741Smasputra 	if (uiop == NULL) {
4713741Smasputra 		/*
4714741Smasputra 		 * kstrwritemp() should have checked sd_flag and
4715741Smasputra 		 * flow-control before coming here.  If we end up
4716741Smasputra 		 * here it means that we can simply pass down the
4717741Smasputra 		 * data to tcp.
4718741Smasputra 		 */
4719741Smasputra 		ASSERT(mp != NULL);
4720898Skais 		if (stp->sd_wputdatafunc != NULL) {
4721898Skais 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4722898Skais 			    NULL, NULL, NULL);
4723898Skais 			if (newmp == NULL) {
4724898Skais 				/* The caller will free mp */
4725898Skais 				return (ECOMM);
4726898Skais 			}
4727898Skais 			mp = newmp;
4728898Skais 		}
4729741Smasputra 		tcp_wput(tcp_wq, mp);
4730741Smasputra 		return (0);
4731741Smasputra 	}
4732741Smasputra 
4733741Smasputra 	/* Fallback to strwrite() to do proper error handling */
4734741Smasputra 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4735741Smasputra 		return (strwrite(SOTOV(so), uiop, cr));
4736741Smasputra 
4737741Smasputra 	rmax = stp->sd_qn_maxpsz;
4738741Smasputra 	ASSERT(rmax >= 0 || rmax == INFPSZ);
4739741Smasputra 	if (rmax == 0 || uiop->uio_resid <= 0)
4740741Smasputra 		return (0);
4741741Smasputra 
4742741Smasputra 	if (rmax == INFPSZ)
4743741Smasputra 		rmax = uiop->uio_resid;
4744741Smasputra 
4745741Smasputra 	maxblk = stp->sd_maxblk;
4746741Smasputra 
4747741Smasputra 	for (;;) {
4748741Smasputra 		iosize = MIN(uiop->uio_resid, rmax);
4749741Smasputra 
4750741Smasputra 		mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4751741Smasputra 		if (mp == NULL) {
4752741Smasputra 			/*
4753741Smasputra 			 * Fallback to strwrite() for ENOMEM; if this
4754741Smasputra 			 * is our first time in this routine and the uio
4755741Smasputra 			 * vector has not been modified, we will end up
4756741Smasputra 			 * calling strwrite() without any flag set.
4757741Smasputra 			 */
4758741Smasputra 			if (error == ENOMEM)
4759741Smasputra 				goto slow_send;
4760741Smasputra 			else
4761741Smasputra 				return (error);
4762741Smasputra 		}
4763741Smasputra 		ASSERT(uiop->uio_resid >= 0);
4764741Smasputra 		/*
4765741Smasputra 		 * If mp is non-NULL and ENOMEM is set, it means that
4766741Smasputra 		 * mcopyinuio() was able to break down some of the user
4767741Smasputra 		 * data into one or more mblks.  Send the partial data
4768741Smasputra 		 * to tcp and let the rest be handled in strwrite().
4769741Smasputra 		 */
4770741Smasputra 		ASSERT(error == 0 || error == ENOMEM);
4771898Skais 		if (stp->sd_wputdatafunc != NULL) {
4772898Skais 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4773898Skais 			    NULL, NULL, NULL);
4774898Skais 			if (newmp == NULL) {
4775898Skais 				/* The caller will free mp */
4776898Skais 				return (ECOMM);
4777898Skais 			}
4778898Skais 			mp = newmp;
4779898Skais 		}
4780741Smasputra 		tcp_wput(tcp_wq, mp);
4781741Smasputra 
4782741Smasputra 		wflag |= NOINTR;
4783741Smasputra 
4784741Smasputra 		if (uiop->uio_resid == 0) {	/* No more data; we're done */
4785741Smasputra 			ASSERT(error == 0);
4786741Smasputra 			break;
4787741Smasputra 		} else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4788741Smasputra 		    (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4789741Smasputra slow_send:
4790741Smasputra 			/*
4791741Smasputra 			 * We were able to send down partial data using
4792741Smasputra 			 * the direct call interface, but are now relying
4793741Smasputra 			 * on strwrite() to handle the non-fastpath cases.
4794741Smasputra 			 * If the socket is blocking we will sleep in
4795741Smasputra 			 * strwaitq() until write is permitted, otherwise,
4796741Smasputra 			 * we will need to return the amount of bytes
4797741Smasputra 			 * written so far back to the app.  This is the
4798741Smasputra 			 * reason why we pass NOINTR flag to strwrite()
4799741Smasputra 			 * for non-blocking socket, because we don't want
4800741Smasputra 			 * to return EAGAIN when portion of the user data
4801741Smasputra 			 * has actually been sent down.
4802741Smasputra 			 */
4803741Smasputra 			return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4804741Smasputra 		}
4805741Smasputra 	}
4806741Smasputra 	return (0);
4807741Smasputra }
4808741Smasputra 
4809741Smasputra /*
48108348SEric.Yu@Sun.COM  * Update sti_faddr by asking the transport (unless AF_UNIX).
48110Sstevel@tonic-gate  */
48128348SEric.Yu@Sun.COM /* ARGSUSED */
48130Sstevel@tonic-gate int
sotpi_getpeername(struct sonode * so,struct sockaddr * name,socklen_t * namelen,boolean_t accept,struct cred * cr)48148348SEric.Yu@Sun.COM sotpi_getpeername(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
48158348SEric.Yu@Sun.COM     boolean_t accept, struct cred *cr)
48160Sstevel@tonic-gate {
48170Sstevel@tonic-gate 	struct strbuf	strbuf;
48180Sstevel@tonic-gate 	int		error = 0, res;
48190Sstevel@tonic-gate 	void		*addr;
48200Sstevel@tonic-gate 	t_uscalar_t	addrlen;
48210Sstevel@tonic-gate 	k_sigset_t	smask;
48228348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
48230Sstevel@tonic-gate 
48240Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
48257240Srh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
48260Sstevel@tonic-gate 
48278348SEric.Yu@Sun.COM 	ASSERT(*namelen > 0);
48280Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
48290Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
48308348SEric.Yu@Sun.COM 
48318348SEric.Yu@Sun.COM 	if (accept) {
48328348SEric.Yu@Sun.COM 		bcopy(sti->sti_faddr_sa, name,
48338348SEric.Yu@Sun.COM 		    MIN(*namelen, sti->sti_faddr_len));
48348348SEric.Yu@Sun.COM 		*namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
48358348SEric.Yu@Sun.COM 		goto done;
48368348SEric.Yu@Sun.COM 	}
48378348SEric.Yu@Sun.COM 
48380Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
48390Sstevel@tonic-gate 		error = ENOTCONN;
48400Sstevel@tonic-gate 		goto done;
48410Sstevel@tonic-gate 	}
48420Sstevel@tonic-gate 	/* Added this check for X/Open */
48430Sstevel@tonic-gate 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
48440Sstevel@tonic-gate 		error = EINVAL;
48450Sstevel@tonic-gate 		if (xnet_check_print) {
48460Sstevel@tonic-gate 			printf("sockfs: X/Open getpeername check => EINVAL\n");
48470Sstevel@tonic-gate 		}
48480Sstevel@tonic-gate 		goto done;
48490Sstevel@tonic-gate 	}
48508348SEric.Yu@Sun.COM 
48518348SEric.Yu@Sun.COM 	if (sti->sti_faddr_valid) {
48528348SEric.Yu@Sun.COM 		bcopy(sti->sti_faddr_sa, name,
48538348SEric.Yu@Sun.COM 		    MIN(*namelen, sti->sti_faddr_len));
48548348SEric.Yu@Sun.COM 		*namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
48558348SEric.Yu@Sun.COM 		goto done;
48568348SEric.Yu@Sun.COM 	}
48578348SEric.Yu@Sun.COM 
48580Sstevel@tonic-gate #ifdef DEBUG
48590Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
48608348SEric.Yu@Sun.COM 	    pr_addr(so->so_family, sti->sti_faddr_sa,
48618348SEric.Yu@Sun.COM 	    (t_uscalar_t)sti->sti_faddr_len)));
48620Sstevel@tonic-gate #endif /* DEBUG */
48630Sstevel@tonic-gate 
48641548Srshoaib 	if (so->so_family == AF_UNIX) {
48650Sstevel@tonic-gate 		/* Transport has different name space - return local info */
48668348SEric.Yu@Sun.COM 		if (sti->sti_faddr_noxlate)
48678348SEric.Yu@Sun.COM 			*namelen = 0;
48680Sstevel@tonic-gate 		error = 0;
48690Sstevel@tonic-gate 		goto done;
48700Sstevel@tonic-gate 	}
48710Sstevel@tonic-gate 
48728348SEric.Yu@Sun.COM 	ASSERT(so->so_family != AF_UNIX && sti->sti_faddr_noxlate == 0);
48738348SEric.Yu@Sun.COM 
48748348SEric.Yu@Sun.COM 	ASSERT(sti->sti_faddr_sa);
48750Sstevel@tonic-gate 	/* Allocate local buffer to use with ioctl */
48768348SEric.Yu@Sun.COM 	addrlen = (t_uscalar_t)sti->sti_faddr_maxlen;
48770Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
48780Sstevel@tonic-gate 	addr = kmem_alloc(addrlen, KM_SLEEP);
48790Sstevel@tonic-gate 
48800Sstevel@tonic-gate 	/*
48810Sstevel@tonic-gate 	 * Issue TI_GETPEERNAME with signals masked.
48828348SEric.Yu@Sun.COM 	 * Put the result in sti_faddr_sa so that getpeername works after
48830Sstevel@tonic-gate 	 * a shutdown(output).
48840Sstevel@tonic-gate 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
48850Sstevel@tonic-gate 	 * back to the socket.
48860Sstevel@tonic-gate 	 */
48870Sstevel@tonic-gate 	strbuf.buf = addr;
48880Sstevel@tonic-gate 	strbuf.maxlen = addrlen;
48890Sstevel@tonic-gate 	strbuf.len = 0;
48900Sstevel@tonic-gate 
48910Sstevel@tonic-gate 	sigintr(&smask, 0);
48920Sstevel@tonic-gate 	res = 0;
48938348SEric.Yu@Sun.COM 	ASSERT(cr);
48940Sstevel@tonic-gate 	error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
48958348SEric.Yu@Sun.COM 	    0, K_TO_K, cr, &res);
48960Sstevel@tonic-gate 	sigunintr(&smask);
48970Sstevel@tonic-gate 
48980Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
48990Sstevel@tonic-gate 	/*
49000Sstevel@tonic-gate 	 * If there is an error record the error in so_error put don't fail
49010Sstevel@tonic-gate 	 * the getpeername. Instead fallback on the recorded
49028348SEric.Yu@Sun.COM 	 * sti->sti_faddr_sa.
49030Sstevel@tonic-gate 	 */
49040Sstevel@tonic-gate 	if (error) {
49050Sstevel@tonic-gate 		/*
49060Sstevel@tonic-gate 		 * Various stream head errors can be returned to the ioctl.
49070Sstevel@tonic-gate 		 * However, it is impossible to determine which ones of
49080Sstevel@tonic-gate 		 * these are really socket level errors that were incorrectly
49090Sstevel@tonic-gate 		 * consumed by the ioctl. Thus this code silently ignores the
49100Sstevel@tonic-gate 		 * error - to code explicitly does not reinstate the error
49110Sstevel@tonic-gate 		 * using soseterror().
49120Sstevel@tonic-gate 		 * Experiments have shows that at least this set of
49130Sstevel@tonic-gate 		 * errors are reported and should not be reinstated on the
49140Sstevel@tonic-gate 		 * socket:
49150Sstevel@tonic-gate 		 *	EINVAL	E.g. if an I_LINK was in effect when
49160Sstevel@tonic-gate 		 *		getpeername was called.
49170Sstevel@tonic-gate 		 *	EPIPE	The ioctl error semantics prefer the write
49180Sstevel@tonic-gate 		 *		side error over the read side error.
49190Sstevel@tonic-gate 		 *	ENOTCONN The transport just got disconnected but
49200Sstevel@tonic-gate 		 *		sockfs had not yet seen the T_DISCON_IND
49210Sstevel@tonic-gate 		 *		when issuing the ioctl.
49220Sstevel@tonic-gate 		 */
49230Sstevel@tonic-gate 		error = 0;
49240Sstevel@tonic-gate 	} else if (res == 0 && strbuf.len > 0 &&
49250Sstevel@tonic-gate 	    (so->so_state & SS_ISCONNECTED)) {
49268348SEric.Yu@Sun.COM 		ASSERT(strbuf.len <= (int)sti->sti_faddr_maxlen);
49278348SEric.Yu@Sun.COM 		sti->sti_faddr_len = (socklen_t)strbuf.len;
49288348SEric.Yu@Sun.COM 		bcopy(addr, sti->sti_faddr_sa, sti->sti_faddr_len);
49298348SEric.Yu@Sun.COM 		sti->sti_faddr_valid = 1;
49308348SEric.Yu@Sun.COM 
49318348SEric.Yu@Sun.COM 		bcopy(addr, name, MIN(*namelen, sti->sti_faddr_len));
49328348SEric.Yu@Sun.COM 		*namelen = sti->sti_faddr_len;
49330Sstevel@tonic-gate 	}
49340Sstevel@tonic-gate 	kmem_free(addr, addrlen);
49350Sstevel@tonic-gate #ifdef DEBUG
49360Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
49378348SEric.Yu@Sun.COM 	    pr_addr(so->so_family, sti->sti_faddr_sa,
49388348SEric.Yu@Sun.COM 	    (t_uscalar_t)sti->sti_faddr_len)));
49390Sstevel@tonic-gate #endif /* DEBUG */
49400Sstevel@tonic-gate done:
49410Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
49420Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
49430Sstevel@tonic-gate 	return (error);
49440Sstevel@tonic-gate }
49450Sstevel@tonic-gate 
49460Sstevel@tonic-gate /*
49478348SEric.Yu@Sun.COM  * Update sti_laddr by asking the transport (unless AF_UNIX).
49480Sstevel@tonic-gate  */
49490Sstevel@tonic-gate int
sotpi_getsockname(struct sonode * so,struct sockaddr * name,socklen_t * namelen,struct cred * cr)49508348SEric.Yu@Sun.COM sotpi_getsockname(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
49518348SEric.Yu@Sun.COM     struct cred *cr)
49520Sstevel@tonic-gate {
49530Sstevel@tonic-gate 	struct strbuf	strbuf;
49540Sstevel@tonic-gate 	int		error = 0, res;
49550Sstevel@tonic-gate 	void		*addr;
49560Sstevel@tonic-gate 	t_uscalar_t	addrlen;
49570Sstevel@tonic-gate 	k_sigset_t	smask;
49588348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
49590Sstevel@tonic-gate 
49600Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
49617240Srh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
49620Sstevel@tonic-gate 
49638348SEric.Yu@Sun.COM 	ASSERT(*namelen > 0);
49640Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
49650Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
49668348SEric.Yu@Sun.COM 
49670Sstevel@tonic-gate #ifdef DEBUG
49688348SEric.Yu@Sun.COM 
49690Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
49708348SEric.Yu@Sun.COM 	    pr_addr(so->so_family, sti->sti_laddr_sa,
49718348SEric.Yu@Sun.COM 	    (t_uscalar_t)sti->sti_laddr_len)));
49720Sstevel@tonic-gate #endif /* DEBUG */
49738348SEric.Yu@Sun.COM 	if (sti->sti_laddr_valid) {
49748348SEric.Yu@Sun.COM 		bcopy(sti->sti_laddr_sa, name,
49758348SEric.Yu@Sun.COM 		    MIN(*namelen, sti->sti_laddr_len));
49768348SEric.Yu@Sun.COM 		*namelen = sti->sti_laddr_len;
49778348SEric.Yu@Sun.COM 		goto done;
49788348SEric.Yu@Sun.COM 	}
49798348SEric.Yu@Sun.COM 
49800Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
49810Sstevel@tonic-gate 		/* Transport has different name space - return local info */
49820Sstevel@tonic-gate 		error = 0;
49838465SEric.Yu@Sun.COM 		*namelen = 0;
49840Sstevel@tonic-gate 		goto done;
49850Sstevel@tonic-gate 	}
49861156Sblu 	if (!(so->so_state & SS_ISBOUND)) {
49871156Sblu 		/* If not bound, then nothing to return. */
49881156Sblu 		error = 0;
49891156Sblu 		goto done;
49901156Sblu 	}
49918348SEric.Yu@Sun.COM 
49920Sstevel@tonic-gate 	/* Allocate local buffer to use with ioctl */
49938348SEric.Yu@Sun.COM 	addrlen = (t_uscalar_t)sti->sti_laddr_maxlen;
49940Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
49950Sstevel@tonic-gate 	addr = kmem_alloc(addrlen, KM_SLEEP);
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 	/*
49980Sstevel@tonic-gate 	 * Issue TI_GETMYNAME with signals masked.
49998348SEric.Yu@Sun.COM 	 * Put the result in sti_laddr_sa so that getsockname works after
50000Sstevel@tonic-gate 	 * a shutdown(output).
50010Sstevel@tonic-gate 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
50020Sstevel@tonic-gate 	 * back to the socket.
50030Sstevel@tonic-gate 	 */
50040Sstevel@tonic-gate 	strbuf.buf = addr;
50050Sstevel@tonic-gate 	strbuf.maxlen = addrlen;
50060Sstevel@tonic-gate 	strbuf.len = 0;
50070Sstevel@tonic-gate 
50080Sstevel@tonic-gate 	sigintr(&smask, 0);
50090Sstevel@tonic-gate 	res = 0;
50108348SEric.Yu@Sun.COM 	ASSERT(cr);
50110Sstevel@tonic-gate 	error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
50128348SEric.Yu@Sun.COM 	    0, K_TO_K, cr, &res);
50130Sstevel@tonic-gate 	sigunintr(&smask);
50140Sstevel@tonic-gate 
50150Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
50160Sstevel@tonic-gate 	/*
50170Sstevel@tonic-gate 	 * If there is an error record the error in so_error put don't fail
50180Sstevel@tonic-gate 	 * the getsockname. Instead fallback on the recorded
50198348SEric.Yu@Sun.COM 	 * sti->sti_laddr_sa.
50200Sstevel@tonic-gate 	 */
50210Sstevel@tonic-gate 	if (error) {
50220Sstevel@tonic-gate 		/*
50230Sstevel@tonic-gate 		 * Various stream head errors can be returned to the ioctl.
50240Sstevel@tonic-gate 		 * However, it is impossible to determine which ones of
50250Sstevel@tonic-gate 		 * these are really socket level errors that were incorrectly
50260Sstevel@tonic-gate 		 * consumed by the ioctl. Thus this code silently ignores the
50270Sstevel@tonic-gate 		 * error - to code explicitly does not reinstate the error
50280Sstevel@tonic-gate 		 * using soseterror().
50290Sstevel@tonic-gate 		 * Experiments have shows that at least this set of
50300Sstevel@tonic-gate 		 * errors are reported and should not be reinstated on the
50310Sstevel@tonic-gate 		 * socket:
50320Sstevel@tonic-gate 		 *	EINVAL	E.g. if an I_LINK was in effect when
50330Sstevel@tonic-gate 		 *		getsockname was called.
50340Sstevel@tonic-gate 		 *	EPIPE	The ioctl error semantics prefer the write
50350Sstevel@tonic-gate 		 *		side error over the read side error.
50360Sstevel@tonic-gate 		 */
50370Sstevel@tonic-gate 		error = 0;
50380Sstevel@tonic-gate 	} else if (res == 0 && strbuf.len > 0 &&
50390Sstevel@tonic-gate 	    (so->so_state & SS_ISBOUND)) {
50408348SEric.Yu@Sun.COM 		ASSERT(strbuf.len <= (int)sti->sti_laddr_maxlen);
50418348SEric.Yu@Sun.COM 		sti->sti_laddr_len = (socklen_t)strbuf.len;
50428348SEric.Yu@Sun.COM 		bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
50438348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 1;
50448348SEric.Yu@Sun.COM 
50458348SEric.Yu@Sun.COM 		bcopy(addr, name, MIN(sti->sti_laddr_len, *namelen));
50468348SEric.Yu@Sun.COM 		*namelen = sti->sti_laddr_len;
50470Sstevel@tonic-gate 	}
50480Sstevel@tonic-gate 	kmem_free(addr, addrlen);
50490Sstevel@tonic-gate #ifdef DEBUG
50500Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
50518348SEric.Yu@Sun.COM 	    pr_addr(so->so_family, sti->sti_laddr_sa,
50528348SEric.Yu@Sun.COM 	    (t_uscalar_t)sti->sti_laddr_len)));
50530Sstevel@tonic-gate #endif /* DEBUG */
50540Sstevel@tonic-gate done:
50550Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
50560Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
50570Sstevel@tonic-gate 	return (error);
50580Sstevel@tonic-gate }
50590Sstevel@tonic-gate 
50600Sstevel@tonic-gate /*
50610Sstevel@tonic-gate  * Get socket options. For SOL_SOCKET options some options are handled
50620Sstevel@tonic-gate  * by the sockfs while others use the value recorded in the sonode as a
50630Sstevel@tonic-gate  * fallback should the T_SVR4_OPTMGMT_REQ fail.
50640Sstevel@tonic-gate  *
50650Sstevel@tonic-gate  * On the return most *optlenp bytes are copied to optval.
50660Sstevel@tonic-gate  */
50678348SEric.Yu@Sun.COM /* ARGSUSED */
50680Sstevel@tonic-gate int
sotpi_getsockopt(struct sonode * so,int level,int option_name,void * optval,socklen_t * optlenp,int flags,struct cred * cr)50690Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name,
50708348SEric.Yu@Sun.COM 		void *optval, socklen_t *optlenp, int flags, struct cred *cr)
50710Sstevel@tonic-gate {
50720Sstevel@tonic-gate 	struct T_optmgmt_req	optmgmt_req;
50730Sstevel@tonic-gate 	struct T_optmgmt_ack	*optmgmt_ack;
50740Sstevel@tonic-gate 	struct opthdr		oh;
50750Sstevel@tonic-gate 	struct opthdr		*opt_res;
50760Sstevel@tonic-gate 	mblk_t			*mp = NULL;
50770Sstevel@tonic-gate 	int			error = 0;
50780Sstevel@tonic-gate 	void			*option = NULL;	/* Set if fallback value */
50790Sstevel@tonic-gate 	t_uscalar_t		maxlen = *optlenp;
50800Sstevel@tonic-gate 	t_uscalar_t		len;
50810Sstevel@tonic-gate 	uint32_t		value;
50828348SEric.Yu@Sun.COM 	struct timeval		tmo_val; /* used for SO_RCVTIMEO, SO_SNDTIMEO */
50838489Sshenjian 	struct timeval32	tmo_val32;
50848348SEric.Yu@Sun.COM 	struct so_snd_bufinfo	snd_bufinfo;	/* used for zero copy */
50850Sstevel@tonic-gate 
50860Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
50877240Srh87107 	    (void *)so, level, option_name, optval, (void *)optlenp,
50885240Snordmark 	    pr_state(so->so_state, so->so_mode)));
50890Sstevel@tonic-gate 
50900Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
50910Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
50920Sstevel@tonic-gate 
50930Sstevel@tonic-gate 	/*
50940Sstevel@tonic-gate 	 * Check for SOL_SOCKET options.
50950Sstevel@tonic-gate 	 * Certain SOL_SOCKET options are returned directly whereas
50960Sstevel@tonic-gate 	 * others only provide a default (fallback) value should
50970Sstevel@tonic-gate 	 * the T_SVR4_OPTMGMT_REQ fail.
50980Sstevel@tonic-gate 	 */
50990Sstevel@tonic-gate 	if (level == SOL_SOCKET) {
51000Sstevel@tonic-gate 		/* Check parameters */
51010Sstevel@tonic-gate 		switch (option_name) {
51020Sstevel@tonic-gate 		case SO_TYPE:
51030Sstevel@tonic-gate 		case SO_ERROR:
51040Sstevel@tonic-gate 		case SO_DEBUG:
51050Sstevel@tonic-gate 		case SO_ACCEPTCONN:
51060Sstevel@tonic-gate 		case SO_REUSEADDR:
51070Sstevel@tonic-gate 		case SO_KEEPALIVE:
51080Sstevel@tonic-gate 		case SO_DONTROUTE:
51090Sstevel@tonic-gate 		case SO_BROADCAST:
51100Sstevel@tonic-gate 		case SO_USELOOPBACK:
51110Sstevel@tonic-gate 		case SO_OOBINLINE:
51120Sstevel@tonic-gate 		case SO_SNDBUF:
51130Sstevel@tonic-gate 		case SO_RCVBUF:
51140Sstevel@tonic-gate #ifdef notyet
51150Sstevel@tonic-gate 		case SO_SNDLOWAT:
51160Sstevel@tonic-gate 		case SO_RCVLOWAT:
51170Sstevel@tonic-gate #endif /* notyet */
51183388Skcpoon 		case SO_DOMAIN:
51190Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
51200Sstevel@tonic-gate 			if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
51210Sstevel@tonic-gate 				error = EINVAL;
51220Sstevel@tonic-gate 				eprintsoline(so, error);
51230Sstevel@tonic-gate 				goto done2;
51240Sstevel@tonic-gate 			}
51250Sstevel@tonic-gate 			break;
51268348SEric.Yu@Sun.COM 		case SO_RCVTIMEO:
51278348SEric.Yu@Sun.COM 		case SO_SNDTIMEO:
51288575Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
51298575Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
51308489Sshenjian 				if (maxlen < sizeof (struct timeval)) {
51318489Sshenjian 					error = EINVAL;
51328489Sshenjian 					eprintsoline(so, error);
51338489Sshenjian 					goto done2;
51348489Sshenjian 				}
51358489Sshenjian 			} else {
51368489Sshenjian 				if (maxlen < sizeof (struct timeval32)) {
51378489Sshenjian 					error = EINVAL;
51388489Sshenjian 					eprintsoline(so, error);
51398489Sshenjian 					goto done2;
51408489Sshenjian 				}
51418489Sshenjian 
51428348SEric.Yu@Sun.COM 			}
51438348SEric.Yu@Sun.COM 			break;
51440Sstevel@tonic-gate 		case SO_LINGER:
51450Sstevel@tonic-gate 			if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
51460Sstevel@tonic-gate 				error = EINVAL;
51470Sstevel@tonic-gate 				eprintsoline(so, error);
51480Sstevel@tonic-gate 				goto done2;
51490Sstevel@tonic-gate 			}
51500Sstevel@tonic-gate 			break;
51518348SEric.Yu@Sun.COM 		case SO_SND_BUFINFO:
51528348SEric.Yu@Sun.COM 			if (maxlen < (t_uscalar_t)
51538348SEric.Yu@Sun.COM 			    sizeof (struct so_snd_bufinfo)) {
51548348SEric.Yu@Sun.COM 				error = EINVAL;
51558348SEric.Yu@Sun.COM 				eprintsoline(so, error);
51568348SEric.Yu@Sun.COM 				goto done2;
51578348SEric.Yu@Sun.COM 			}
51588348SEric.Yu@Sun.COM 			break;
51590Sstevel@tonic-gate 		}
51600Sstevel@tonic-gate 
51610Sstevel@tonic-gate 		len = (t_uscalar_t)sizeof (uint32_t);	/* Default */
51620Sstevel@tonic-gate 
51630Sstevel@tonic-gate 		switch (option_name) {
51640Sstevel@tonic-gate 		case SO_TYPE:
51650Sstevel@tonic-gate 			value = so->so_type;
51660Sstevel@tonic-gate 			option = &value;
51670Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51680Sstevel@tonic-gate 
51690Sstevel@tonic-gate 		case SO_ERROR:
51708348SEric.Yu@Sun.COM 			value = sogeterr(so, B_TRUE);
51710Sstevel@tonic-gate 			option = &value;
51720Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51730Sstevel@tonic-gate 
51740Sstevel@tonic-gate 		case SO_ACCEPTCONN:
51750Sstevel@tonic-gate 			if (so->so_state & SS_ACCEPTCONN)
51760Sstevel@tonic-gate 				value = SO_ACCEPTCONN;
51770Sstevel@tonic-gate 			else
51780Sstevel@tonic-gate 				value = 0;
51790Sstevel@tonic-gate #ifdef DEBUG
51800Sstevel@tonic-gate 			if (value) {
51810Sstevel@tonic-gate 				dprintso(so, 1,
51820Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x is set\n",
51830Sstevel@tonic-gate 				    option_name));
51840Sstevel@tonic-gate 			} else {
51850Sstevel@tonic-gate 				dprintso(so, 1,
51860Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x not set\n",
51870Sstevel@tonic-gate 				    option_name));
51880Sstevel@tonic-gate 			}
51890Sstevel@tonic-gate #endif /* DEBUG */
51900Sstevel@tonic-gate 			option = &value;
51910Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51920Sstevel@tonic-gate 
51930Sstevel@tonic-gate 		case SO_DEBUG:
51940Sstevel@tonic-gate 		case SO_REUSEADDR:
51950Sstevel@tonic-gate 		case SO_KEEPALIVE:
51960Sstevel@tonic-gate 		case SO_DONTROUTE:
51970Sstevel@tonic-gate 		case SO_BROADCAST:
51980Sstevel@tonic-gate 		case SO_USELOOPBACK:
51990Sstevel@tonic-gate 		case SO_OOBINLINE:
52000Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
52010Sstevel@tonic-gate 			value = (so->so_options & option_name);
52020Sstevel@tonic-gate #ifdef DEBUG
52030Sstevel@tonic-gate 			if (value) {
52040Sstevel@tonic-gate 				dprintso(so, 1,
52050Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x is set\n",
52060Sstevel@tonic-gate 				    option_name));
52070Sstevel@tonic-gate 			} else {
52080Sstevel@tonic-gate 				dprintso(so, 1,
52090Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x not set\n",
52100Sstevel@tonic-gate 				    option_name));
52110Sstevel@tonic-gate 			}
52120Sstevel@tonic-gate #endif /* DEBUG */
52130Sstevel@tonic-gate 			option = &value;
52140Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52150Sstevel@tonic-gate 
52160Sstevel@tonic-gate 		/*
52170Sstevel@tonic-gate 		 * The following options are only returned by sockfs when the
52180Sstevel@tonic-gate 		 * T_SVR4_OPTMGMT_REQ fails.
52190Sstevel@tonic-gate 		 */
52200Sstevel@tonic-gate 		case SO_LINGER:
52210Sstevel@tonic-gate 			option = &so->so_linger;
52220Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (struct linger);
52230Sstevel@tonic-gate 			break;
52240Sstevel@tonic-gate 		case SO_SNDBUF: {
52250Sstevel@tonic-gate 			ssize_t lvalue;
52260Sstevel@tonic-gate 
52270Sstevel@tonic-gate 			/*
52280Sstevel@tonic-gate 			 * If the option has not been set then get a default
52290Sstevel@tonic-gate 			 * value from the read queue. This value is
52300Sstevel@tonic-gate 			 * returned if the transport fails
52310Sstevel@tonic-gate 			 * the T_SVR4_OPTMGMT_REQ.
52320Sstevel@tonic-gate 			 */
52330Sstevel@tonic-gate 			lvalue = so->so_sndbuf;
52340Sstevel@tonic-gate 			if (lvalue == 0) {
52350Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
52360Sstevel@tonic-gate 				(void) strqget(strvp2wq(SOTOV(so))->q_next,
52375240Snordmark 				    QHIWAT, 0, &lvalue);
52380Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
52390Sstevel@tonic-gate 				dprintso(so, 1,
52400Sstevel@tonic-gate 				    ("got SO_SNDBUF %ld from q\n", lvalue));
52410Sstevel@tonic-gate 			}
52420Sstevel@tonic-gate 			value = (int)lvalue;
52430Sstevel@tonic-gate 			option = &value;
52440Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (so->so_sndbuf);
52450Sstevel@tonic-gate 			break;
52460Sstevel@tonic-gate 		}
52470Sstevel@tonic-gate 		case SO_RCVBUF: {
52480Sstevel@tonic-gate 			ssize_t lvalue;
52490Sstevel@tonic-gate 
52500Sstevel@tonic-gate 			/*
52510Sstevel@tonic-gate 			 * If the option has not been set then get a default
52520Sstevel@tonic-gate 			 * value from the read queue. This value is
52530Sstevel@tonic-gate 			 * returned if the transport fails
52540Sstevel@tonic-gate 			 * the T_SVR4_OPTMGMT_REQ.
52550Sstevel@tonic-gate 			 *
52560Sstevel@tonic-gate 			 * XXX If SO_RCVBUF has been set and this is an
52570Sstevel@tonic-gate 			 * XPG 4.2 application then do not ask the transport
52580Sstevel@tonic-gate 			 * since the transport might adjust the value and not
52590Sstevel@tonic-gate 			 * return exactly what was set by the application.
52600Sstevel@tonic-gate 			 * For non-XPG 4.2 application we return the value
52610Sstevel@tonic-gate 			 * that the transport is actually using.
52620Sstevel@tonic-gate 			 */
52630Sstevel@tonic-gate 			lvalue = so->so_rcvbuf;
52640Sstevel@tonic-gate 			if (lvalue == 0) {
52650Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
52660Sstevel@tonic-gate 				(void) strqget(RD(strvp2wq(SOTOV(so))),
52675240Snordmark 				    QHIWAT, 0, &lvalue);
52680Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
52690Sstevel@tonic-gate 				dprintso(so, 1,
52700Sstevel@tonic-gate 				    ("got SO_RCVBUF %ld from q\n", lvalue));
52710Sstevel@tonic-gate 			} else if (flags & _SOGETSOCKOPT_XPG4_2) {
52720Sstevel@tonic-gate 				value = (int)lvalue;
52730Sstevel@tonic-gate 				option = &value;
52740Sstevel@tonic-gate 				goto copyout;	/* skip asking transport */
52750Sstevel@tonic-gate 			}
52760Sstevel@tonic-gate 			value = (int)lvalue;
52770Sstevel@tonic-gate 			option = &value;
52780Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (so->so_rcvbuf);
52790Sstevel@tonic-gate 			break;
52800Sstevel@tonic-gate 		}
52813388Skcpoon 		case SO_DOMAIN:
52823388Skcpoon 			value = so->so_family;
52833388Skcpoon 			option = &value;
52843388Skcpoon 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52853388Skcpoon 
52860Sstevel@tonic-gate #ifdef notyet
52870Sstevel@tonic-gate 		/*
52880Sstevel@tonic-gate 		 * We do not implement the semantics of these options
52890Sstevel@tonic-gate 		 * thus we shouldn't implement the options either.
52900Sstevel@tonic-gate 		 */
52910Sstevel@tonic-gate 		case SO_SNDLOWAT:
52920Sstevel@tonic-gate 			value = so->so_sndlowat;
52930Sstevel@tonic-gate 			option = &value;
52940Sstevel@tonic-gate 			break;
52950Sstevel@tonic-gate 		case SO_RCVLOWAT:
52960Sstevel@tonic-gate 			value = so->so_rcvlowat;
52970Sstevel@tonic-gate 			option = &value;
52980Sstevel@tonic-gate 			break;
52998348SEric.Yu@Sun.COM #endif /* notyet */
53000Sstevel@tonic-gate 		case SO_SNDTIMEO:
53018348SEric.Yu@Sun.COM 		case SO_RCVTIMEO: {
53028348SEric.Yu@Sun.COM 			clock_t val;
53038489Sshenjian 
53048348SEric.Yu@Sun.COM 			if (option_name == SO_RCVTIMEO)
53058348SEric.Yu@Sun.COM 				val = drv_hztousec(so->so_rcvtimeo);
53068348SEric.Yu@Sun.COM 			else
53078348SEric.Yu@Sun.COM 				val = drv_hztousec(so->so_sndtimeo);
53088348SEric.Yu@Sun.COM 			tmo_val.tv_sec = val / (1000 * 1000);
53098348SEric.Yu@Sun.COM 			tmo_val.tv_usec = val % (1000 * 1000);
53108575Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
53118575Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
53128489Sshenjian 				option = &tmo_val;
53138489Sshenjian 				len = sizeof (struct timeval);
53148489Sshenjian 			} else {
53158489Sshenjian 				TIMEVAL_TO_TIMEVAL32(&tmo_val32, &tmo_val);
53168489Sshenjian 				option = &tmo_val32;
53178489Sshenjian 				len = sizeof (struct timeval32);
53188489Sshenjian 			}
53190Sstevel@tonic-gate 			break;
53208348SEric.Yu@Sun.COM 		}
53218348SEric.Yu@Sun.COM 		case SO_SND_BUFINFO: {
53228348SEric.Yu@Sun.COM 			snd_bufinfo.sbi_wroff =
53238348SEric.Yu@Sun.COM 			    (so->so_proto_props).sopp_wroff;
53248348SEric.Yu@Sun.COM 			snd_bufinfo.sbi_maxblk =
53258348SEric.Yu@Sun.COM 			    (so->so_proto_props).sopp_maxblk;
53268348SEric.Yu@Sun.COM 			snd_bufinfo.sbi_maxpsz =
53278348SEric.Yu@Sun.COM 			    (so->so_proto_props).sopp_maxpsz;
53288348SEric.Yu@Sun.COM 			snd_bufinfo.sbi_tail =
53298348SEric.Yu@Sun.COM 			    (so->so_proto_props).sopp_tail;
53308348SEric.Yu@Sun.COM 			option = &snd_bufinfo;
53318348SEric.Yu@Sun.COM 			len = (t_uscalar_t)sizeof (struct so_snd_bufinfo);
53320Sstevel@tonic-gate 			break;
53338348SEric.Yu@Sun.COM 		}
53340Sstevel@tonic-gate 		}
53350Sstevel@tonic-gate 	}
53360Sstevel@tonic-gate 
53370Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
53380Sstevel@tonic-gate 
53390Sstevel@tonic-gate 	/* Send request */
53400Sstevel@tonic-gate 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
53410Sstevel@tonic-gate 	optmgmt_req.MGMT_flags = T_CHECK;
53420Sstevel@tonic-gate 	optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
53430Sstevel@tonic-gate 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
53440Sstevel@tonic-gate 
53450Sstevel@tonic-gate 	oh.level = level;
53460Sstevel@tonic-gate 	oh.name = option_name;
53470Sstevel@tonic-gate 	oh.len = maxlen;
53480Sstevel@tonic-gate 
53490Sstevel@tonic-gate 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
53508778SErik.Nordmark@Sun.COM 	    &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP, cr);
53510Sstevel@tonic-gate 	/* Let option management work in the presence of data flow control */
53520Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
53535240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
53540Sstevel@tonic-gate 	mp = NULL;
53550Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
53560Sstevel@tonic-gate 	if (error) {
53570Sstevel@tonic-gate 		eprintsoline(so, error);
53580Sstevel@tonic-gate 		goto done2;
53590Sstevel@tonic-gate 	}
53600Sstevel@tonic-gate 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
53610Sstevel@tonic-gate 	    (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
53620Sstevel@tonic-gate 	if (error) {
53630Sstevel@tonic-gate 		if (option != NULL) {
53640Sstevel@tonic-gate 			/* We have a fallback value */
53650Sstevel@tonic-gate 			error = 0;
53660Sstevel@tonic-gate 			goto copyout;
53670Sstevel@tonic-gate 		}
53680Sstevel@tonic-gate 		eprintsoline(so, error);
53690Sstevel@tonic-gate 		goto done2;
53700Sstevel@tonic-gate 	}
53710Sstevel@tonic-gate 	ASSERT(mp);
53720Sstevel@tonic-gate 	optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
53730Sstevel@tonic-gate 	opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
53745240Snordmark 	    optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
53750Sstevel@tonic-gate 	if (opt_res == NULL) {
53760Sstevel@tonic-gate 		if (option != NULL) {
53770Sstevel@tonic-gate 			/* We have a fallback value */
53780Sstevel@tonic-gate 			error = 0;
53790Sstevel@tonic-gate 			goto copyout;
53800Sstevel@tonic-gate 		}
53810Sstevel@tonic-gate 		error = EPROTO;
53820Sstevel@tonic-gate 		eprintsoline(so, error);
53830Sstevel@tonic-gate 		goto done;
53840Sstevel@tonic-gate 	}
53850Sstevel@tonic-gate 	option = &opt_res[1];
53860Sstevel@tonic-gate 
53870Sstevel@tonic-gate 	/* check to ensure that the option is within bounds */
53880Sstevel@tonic-gate 	if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
53895240Snordmark 	    (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
53900Sstevel@tonic-gate 		if (option != NULL) {
53910Sstevel@tonic-gate 			/* We have a fallback value */
53920Sstevel@tonic-gate 			error = 0;
53930Sstevel@tonic-gate 			goto copyout;
53940Sstevel@tonic-gate 		}
53950Sstevel@tonic-gate 		error = EPROTO;
53960Sstevel@tonic-gate 		eprintsoline(so, error);
53970Sstevel@tonic-gate 		goto done;
53980Sstevel@tonic-gate 	}
53990Sstevel@tonic-gate 
54000Sstevel@tonic-gate 	len = opt_res->len;
54010Sstevel@tonic-gate 
54020Sstevel@tonic-gate copyout: {
54030Sstevel@tonic-gate 		t_uscalar_t size = MIN(len, maxlen);
54040Sstevel@tonic-gate 		bcopy(option, optval, size);
54050Sstevel@tonic-gate 		bcopy(&size, optlenp, sizeof (size));
54060Sstevel@tonic-gate 	}
54070Sstevel@tonic-gate done:
54080Sstevel@tonic-gate 	freemsg(mp);
54090Sstevel@tonic-gate done2:
54100Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
54110Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
54128348SEric.Yu@Sun.COM 
54130Sstevel@tonic-gate 	return (error);
54140Sstevel@tonic-gate }
54150Sstevel@tonic-gate 
54160Sstevel@tonic-gate /*
54170Sstevel@tonic-gate  * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
54180Sstevel@tonic-gate  * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
54190Sstevel@tonic-gate  * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
54200Sstevel@tonic-gate  * setsockopt has to work even if the transport does not support the option.
54210Sstevel@tonic-gate  */
54228348SEric.Yu@Sun.COM /* ARGSUSED */
54230Sstevel@tonic-gate int
sotpi_setsockopt(struct sonode * so,int level,int option_name,const void * optval,t_uscalar_t optlen,struct cred * cr)54240Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name,
54258348SEric.Yu@Sun.COM 	const void *optval, t_uscalar_t optlen, struct cred *cr)
54260Sstevel@tonic-gate {
54270Sstevel@tonic-gate 	struct T_optmgmt_req	optmgmt_req;
54280Sstevel@tonic-gate 	struct opthdr		oh;
54290Sstevel@tonic-gate 	mblk_t			*mp;
54300Sstevel@tonic-gate 	int			error = 0;
54310Sstevel@tonic-gate 	boolean_t		handled = B_FALSE;
54320Sstevel@tonic-gate 
54330Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
54347240Srh87107 	    (void *)so, level, option_name, optval, optlen,
54355240Snordmark 	    pr_state(so->so_state, so->so_mode)));
54360Sstevel@tonic-gate 
54370Sstevel@tonic-gate 	/* X/Open requires this check */
54380Sstevel@tonic-gate 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
54390Sstevel@tonic-gate 		if (xnet_check_print)
54400Sstevel@tonic-gate 			printf("sockfs: X/Open setsockopt check => EINVAL\n");
54410Sstevel@tonic-gate 		return (EINVAL);
54420Sstevel@tonic-gate 	}
54430Sstevel@tonic-gate 
54440Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
54450Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
54460Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
54470Sstevel@tonic-gate 
54480Sstevel@tonic-gate 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
54490Sstevel@tonic-gate 	optmgmt_req.MGMT_flags = T_NEGOTIATE;
54500Sstevel@tonic-gate 	optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
54510Sstevel@tonic-gate 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
54520Sstevel@tonic-gate 
54530Sstevel@tonic-gate 	oh.level = level;
54540Sstevel@tonic-gate 	oh.name = option_name;
54550Sstevel@tonic-gate 	oh.len = optlen;
54560Sstevel@tonic-gate 
54570Sstevel@tonic-gate 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
54588778SErik.Nordmark@Sun.COM 	    &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP, cr);
54590Sstevel@tonic-gate 	/* Let option management work in the presence of data flow control */
54600Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
54615240Snordmark 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
54620Sstevel@tonic-gate 	mp = NULL;
54630Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
54640Sstevel@tonic-gate 	if (error) {
54650Sstevel@tonic-gate 		eprintsoline(so, error);
54668348SEric.Yu@Sun.COM 		goto done2;
54670Sstevel@tonic-gate 	}
54680Sstevel@tonic-gate 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
54690Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
54700Sstevel@tonic-gate 	if (error) {
54710Sstevel@tonic-gate 		eprintsoline(so, error);
54720Sstevel@tonic-gate 		goto done;
54730Sstevel@tonic-gate 	}
54740Sstevel@tonic-gate 	ASSERT(mp);
54750Sstevel@tonic-gate 	/* No need to verify T_optmgmt_ack */
54760Sstevel@tonic-gate 	freemsg(mp);
54770Sstevel@tonic-gate done:
54780Sstevel@tonic-gate 	/*
54790Sstevel@tonic-gate 	 * Check for SOL_SOCKET options and record their values.
54800Sstevel@tonic-gate 	 * If we know about a SOL_SOCKET parameter and the transport
54810Sstevel@tonic-gate 	 * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
54820Sstevel@tonic-gate 	 * EPROTO) we let the setsockopt succeed.
54830Sstevel@tonic-gate 	 */
54840Sstevel@tonic-gate 	if (level == SOL_SOCKET) {
54850Sstevel@tonic-gate 		/* Check parameters */
54860Sstevel@tonic-gate 		switch (option_name) {
54870Sstevel@tonic-gate 		case SO_DEBUG:
54880Sstevel@tonic-gate 		case SO_REUSEADDR:
54890Sstevel@tonic-gate 		case SO_KEEPALIVE:
54900Sstevel@tonic-gate 		case SO_DONTROUTE:
54910Sstevel@tonic-gate 		case SO_BROADCAST:
54920Sstevel@tonic-gate 		case SO_USELOOPBACK:
54930Sstevel@tonic-gate 		case SO_OOBINLINE:
54940Sstevel@tonic-gate 		case SO_SNDBUF:
54950Sstevel@tonic-gate 		case SO_RCVBUF:
54960Sstevel@tonic-gate #ifdef notyet
54970Sstevel@tonic-gate 		case SO_SNDLOWAT:
54980Sstevel@tonic-gate 		case SO_RCVLOWAT:
54990Sstevel@tonic-gate #endif /* notyet */
55000Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
55010Sstevel@tonic-gate 			if (optlen != (t_uscalar_t)sizeof (int32_t)) {
55020Sstevel@tonic-gate 				error = EINVAL;
55030Sstevel@tonic-gate 				eprintsoline(so, error);
55040Sstevel@tonic-gate 				goto done2;
55050Sstevel@tonic-gate 			}
55060Sstevel@tonic-gate 			ASSERT(optval);
55070Sstevel@tonic-gate 			handled = B_TRUE;
55080Sstevel@tonic-gate 			break;
55098348SEric.Yu@Sun.COM 		case SO_SNDTIMEO:
55108348SEric.Yu@Sun.COM 		case SO_RCVTIMEO:
55118575Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
55128575Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
55138489Sshenjian 				if (optlen != sizeof (struct timeval)) {
55148489Sshenjian 					error = EINVAL;
55158489Sshenjian 					eprintsoline(so, error);
55168489Sshenjian 					goto done2;
55178489Sshenjian 				}
55188489Sshenjian 			} else {
55198489Sshenjian 				if (optlen != sizeof (struct timeval32)) {
55208489Sshenjian 					error = EINVAL;
55218489Sshenjian 					eprintsoline(so, error);
55228489Sshenjian 					goto done2;
55238489Sshenjian 				}
55248348SEric.Yu@Sun.COM 			}
55258348SEric.Yu@Sun.COM 			ASSERT(optval);
55268348SEric.Yu@Sun.COM 			handled = B_TRUE;
55278348SEric.Yu@Sun.COM 			break;
55280Sstevel@tonic-gate 		case SO_LINGER:
55290Sstevel@tonic-gate 			if (optlen != (t_uscalar_t)sizeof (struct linger)) {
55300Sstevel@tonic-gate 				error = EINVAL;
55310Sstevel@tonic-gate 				eprintsoline(so, error);
55320Sstevel@tonic-gate 				goto done2;
55330Sstevel@tonic-gate 			}
55340Sstevel@tonic-gate 			ASSERT(optval);
55350Sstevel@tonic-gate 			handled = B_TRUE;
55360Sstevel@tonic-gate 			break;
55370Sstevel@tonic-gate 		}
55380Sstevel@tonic-gate 
55390Sstevel@tonic-gate #define	intvalue	(*(int32_t *)optval)
55400Sstevel@tonic-gate 
55410Sstevel@tonic-gate 		switch (option_name) {
55420Sstevel@tonic-gate 		case SO_TYPE:
55430Sstevel@tonic-gate 		case SO_ERROR:
55440Sstevel@tonic-gate 		case SO_ACCEPTCONN:
55450Sstevel@tonic-gate 			/* Can't be set */
55460Sstevel@tonic-gate 			error = ENOPROTOOPT;
55470Sstevel@tonic-gate 			goto done2;
55480Sstevel@tonic-gate 		case SO_LINGER: {
55490Sstevel@tonic-gate 			struct linger *l = (struct linger *)optval;
55500Sstevel@tonic-gate 
55510Sstevel@tonic-gate 			so->so_linger.l_linger = l->l_linger;
55520Sstevel@tonic-gate 			if (l->l_onoff) {
55530Sstevel@tonic-gate 				so->so_linger.l_onoff = SO_LINGER;
55540Sstevel@tonic-gate 				so->so_options |= SO_LINGER;
55550Sstevel@tonic-gate 			} else {
55560Sstevel@tonic-gate 				so->so_linger.l_onoff = 0;
55570Sstevel@tonic-gate 				so->so_options &= ~SO_LINGER;
55580Sstevel@tonic-gate 			}
55590Sstevel@tonic-gate 			break;
55600Sstevel@tonic-gate 		}
55610Sstevel@tonic-gate 
55620Sstevel@tonic-gate 		case SO_DEBUG:
55630Sstevel@tonic-gate #ifdef SOCK_TEST
55640Sstevel@tonic-gate 			if (intvalue & 2)
55650Sstevel@tonic-gate 				sock_test_timelimit = 10 * hz;
55660Sstevel@tonic-gate 			else
55670Sstevel@tonic-gate 				sock_test_timelimit = 0;
55680Sstevel@tonic-gate 
55690Sstevel@tonic-gate 			if (intvalue & 4)
55700Sstevel@tonic-gate 				do_useracc = 0;
55710Sstevel@tonic-gate 			else
55720Sstevel@tonic-gate 				do_useracc = 1;
55730Sstevel@tonic-gate #endif /* SOCK_TEST */
55740Sstevel@tonic-gate 			/* FALLTHRU */
55750Sstevel@tonic-gate 		case SO_REUSEADDR:
55760Sstevel@tonic-gate 		case SO_KEEPALIVE:
55770Sstevel@tonic-gate 		case SO_DONTROUTE:
55780Sstevel@tonic-gate 		case SO_BROADCAST:
55790Sstevel@tonic-gate 		case SO_USELOOPBACK:
55800Sstevel@tonic-gate 		case SO_OOBINLINE:
55810Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
55820Sstevel@tonic-gate 			if (intvalue != 0) {
55830Sstevel@tonic-gate 				dprintso(so, 1,
55848348SEric.Yu@Sun.COM 				    ("socket_setsockopt: setting 0x%x\n",
55855240Snordmark 				    option_name));
55860Sstevel@tonic-gate 				so->so_options |= option_name;
55870Sstevel@tonic-gate 			} else {
55880Sstevel@tonic-gate 				dprintso(so, 1,
55898348SEric.Yu@Sun.COM 				    ("socket_setsockopt: clearing 0x%x\n",
55905240Snordmark 				    option_name));
55910Sstevel@tonic-gate 				so->so_options &= ~option_name;
55920Sstevel@tonic-gate 			}
55930Sstevel@tonic-gate 			break;
55940Sstevel@tonic-gate 		/*
55950Sstevel@tonic-gate 		 * The following options are only returned by us when the
55968348SEric.Yu@Sun.COM 		 * transport layer fails.
55970Sstevel@tonic-gate 		 * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
55980Sstevel@tonic-gate 		 * since the transport might adjust the value and not
55990Sstevel@tonic-gate 		 * return exactly what was set by the application.
56000Sstevel@tonic-gate 		 */
56010Sstevel@tonic-gate 		case SO_SNDBUF:
56020Sstevel@tonic-gate 			so->so_sndbuf = intvalue;
56030Sstevel@tonic-gate 			break;
56040Sstevel@tonic-gate 		case SO_RCVBUF:
56050Sstevel@tonic-gate 			so->so_rcvbuf = intvalue;
56060Sstevel@tonic-gate 			break;
56078348SEric.Yu@Sun.COM 		case SO_RCVPSH:
56088348SEric.Yu@Sun.COM 			so->so_rcv_timer_interval = intvalue;
56098348SEric.Yu@Sun.COM 			break;
56100Sstevel@tonic-gate #ifdef notyet
56110Sstevel@tonic-gate 		/*
56120Sstevel@tonic-gate 		 * We do not implement the semantics of these options
56130Sstevel@tonic-gate 		 * thus we shouldn't implement the options either.
56140Sstevel@tonic-gate 		 */
56150Sstevel@tonic-gate 		case SO_SNDLOWAT:
56160Sstevel@tonic-gate 			so->so_sndlowat = intvalue;
56170Sstevel@tonic-gate 			break;
56180Sstevel@tonic-gate 		case SO_RCVLOWAT:
56190Sstevel@tonic-gate 			so->so_rcvlowat = intvalue;
56200Sstevel@tonic-gate 			break;
56218348SEric.Yu@Sun.COM #endif /* notyet */
56220Sstevel@tonic-gate 		case SO_SNDTIMEO:
56238348SEric.Yu@Sun.COM 		case SO_RCVTIMEO: {
56248489Sshenjian 			struct timeval tl;
56258489Sshenjian 			clock_t val;
56268489Sshenjian 
56278575Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
56288575Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE)
56298489Sshenjian 				bcopy(&tl, (struct timeval *)optval,
56308489Sshenjian 				    sizeof (struct timeval));
56318489Sshenjian 			else
56328489Sshenjian 				TIMEVAL32_TO_TIMEVAL(&tl,
56338489Sshenjian 				    (struct timeval32 *)optval);
56348489Sshenjian 			val = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
56358348SEric.Yu@Sun.COM 			if (option_name == SO_RCVTIMEO)
56368348SEric.Yu@Sun.COM 				so->so_rcvtimeo = drv_usectohz(val);
56378348SEric.Yu@Sun.COM 			else
56388348SEric.Yu@Sun.COM 				so->so_sndtimeo = drv_usectohz(val);
56390Sstevel@tonic-gate 			break;
56408348SEric.Yu@Sun.COM 		}
56410Sstevel@tonic-gate 		}
56420Sstevel@tonic-gate #undef	intvalue
56430Sstevel@tonic-gate 
56440Sstevel@tonic-gate 		if (error) {
56450Sstevel@tonic-gate 			if ((error == ENOPROTOOPT || error == EPROTO ||
56460Sstevel@tonic-gate 			    error == EINVAL) && handled) {
56470Sstevel@tonic-gate 				dprintso(so, 1,
56480Sstevel@tonic-gate 				    ("setsockopt: ignoring error %d for 0x%x\n",
56490Sstevel@tonic-gate 				    error, option_name));
56500Sstevel@tonic-gate 				error = 0;
56510Sstevel@tonic-gate 			}
56520Sstevel@tonic-gate 		}
56530Sstevel@tonic-gate 	}
56540Sstevel@tonic-gate done2:
56550Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
56560Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
56570Sstevel@tonic-gate 	return (error);
56580Sstevel@tonic-gate }
56598348SEric.Yu@Sun.COM 
56608427SAnders.Persson@Sun.COM /*
56618427SAnders.Persson@Sun.COM  * sotpi_close() is called when the last open reference goes away.
56628427SAnders.Persson@Sun.COM  */
56638348SEric.Yu@Sun.COM /* ARGSUSED */
56648348SEric.Yu@Sun.COM int
sotpi_close(struct sonode * so,int flag,struct cred * cr)56658348SEric.Yu@Sun.COM sotpi_close(struct sonode *so, int flag, struct cred *cr)
56668348SEric.Yu@Sun.COM {
56678348SEric.Yu@Sun.COM 	struct vnode *vp = SOTOV(so);
56688348SEric.Yu@Sun.COM 	dev_t dev;
56698348SEric.Yu@Sun.COM 	int error = 0;
56708348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
56718348SEric.Yu@Sun.COM 
56728348SEric.Yu@Sun.COM 	dprintso(so, 1, ("sotpi_close(%p, %x) %s\n",
56738348SEric.Yu@Sun.COM 	    (void *)vp, flag, pr_state(so->so_state, so->so_mode)));
56748348SEric.Yu@Sun.COM 
56758348SEric.Yu@Sun.COM 	dev = sti->sti_dev;
56768348SEric.Yu@Sun.COM 
56778348SEric.Yu@Sun.COM 	ASSERT(STREAMSTAB(getmajor(dev)));
56788348SEric.Yu@Sun.COM 
56798348SEric.Yu@Sun.COM 	mutex_enter(&so->so_lock);
56808348SEric.Yu@Sun.COM 	so_lock_single(so);	/* Set SOLOCKED */
56818348SEric.Yu@Sun.COM 
56828399SRao.Shoaib@Sun.COM 	ASSERT(so_verify_oobstate(so));
56838399SRao.Shoaib@Sun.COM 
56848348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_flags & NL7C_ENABLED) {
56858348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags = 0;
56868348SEric.Yu@Sun.COM 		nl7c_close(so);
56878348SEric.Yu@Sun.COM 	}
56888348SEric.Yu@Sun.COM 
56898348SEric.Yu@Sun.COM 	if (vp->v_stream != NULL) {
56908348SEric.Yu@Sun.COM 		vnode_t *ux_vp;
56918348SEric.Yu@Sun.COM 
56928348SEric.Yu@Sun.COM 		if (so->so_family == AF_UNIX) {
56938348SEric.Yu@Sun.COM 			/* Could avoid this when CANTSENDMORE for !dgram */
56948348SEric.Yu@Sun.COM 			so_unix_close(so);
56958348SEric.Yu@Sun.COM 		}
56968348SEric.Yu@Sun.COM 
56978348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
56988348SEric.Yu@Sun.COM 		/*
56998348SEric.Yu@Sun.COM 		 * Disassemble the linkage from the AF_UNIX underlying file
57008348SEric.Yu@Sun.COM 		 * system vnode to this socket (by atomically clearing
57018348SEric.Yu@Sun.COM 		 * v_stream in vn_rele_stream) before strclose clears sd_vnode
57028348SEric.Yu@Sun.COM 		 * and frees the stream head.
57038348SEric.Yu@Sun.COM 		 */
57048348SEric.Yu@Sun.COM 		if ((ux_vp = sti->sti_ux_bound_vp) != NULL) {
57058348SEric.Yu@Sun.COM 			ASSERT(ux_vp->v_stream);
57068348SEric.Yu@Sun.COM 			sti->sti_ux_bound_vp = NULL;
57078348SEric.Yu@Sun.COM 			vn_rele_stream(ux_vp);
57088348SEric.Yu@Sun.COM 		}
57098348SEric.Yu@Sun.COM 		error = strclose(vp, flag, cr);
57108348SEric.Yu@Sun.COM 		vp->v_stream = NULL;
57118348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
57128348SEric.Yu@Sun.COM 	}
57138348SEric.Yu@Sun.COM 
57148348SEric.Yu@Sun.COM 	/*
57158348SEric.Yu@Sun.COM 	 * Flush the T_DISCON_IND on sti_discon_ind_mp.
57168348SEric.Yu@Sun.COM 	 */
57178348SEric.Yu@Sun.COM 	so_flush_discon_ind(so);
57188348SEric.Yu@Sun.COM 
57198348SEric.Yu@Sun.COM 	so_unlock_single(so, SOLOCKED);
57208348SEric.Yu@Sun.COM 	mutex_exit(&so->so_lock);
57218348SEric.Yu@Sun.COM 
57228348SEric.Yu@Sun.COM 	/*
57238348SEric.Yu@Sun.COM 	 * Needed for STREAMs.
57248348SEric.Yu@Sun.COM 	 * Decrement the device driver's reference count for streams
57258348SEric.Yu@Sun.COM 	 * opened via the clone dip. The driver was held in clone_open().
57268348SEric.Yu@Sun.COM 	 * The absence of clone_close() forces this asymmetry.
57278348SEric.Yu@Sun.COM 	 */
57288348SEric.Yu@Sun.COM 	if (so->so_flag & SOCLONE)
57298348SEric.Yu@Sun.COM 		ddi_rele_driver(getmajor(dev));
57308348SEric.Yu@Sun.COM 
57318348SEric.Yu@Sun.COM 	return (error);
57328348SEric.Yu@Sun.COM }
57338348SEric.Yu@Sun.COM 
57348348SEric.Yu@Sun.COM static int
sotpi_ioctl(struct sonode * so,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)57358348SEric.Yu@Sun.COM sotpi_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
57368348SEric.Yu@Sun.COM     struct cred *cr, int32_t *rvalp)
57378348SEric.Yu@Sun.COM {
57388348SEric.Yu@Sun.COM 	struct vnode *vp = SOTOV(so);
57398348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
57408348SEric.Yu@Sun.COM 	int error = 0;
57418348SEric.Yu@Sun.COM 
57428348SEric.Yu@Sun.COM 	dprintso(so, 0, ("sotpi_ioctl: cmd 0x%x, arg 0x%lx, state %s\n",
57438348SEric.Yu@Sun.COM 	    cmd, arg, pr_state(so->so_state, so->so_mode)));
57448348SEric.Yu@Sun.COM 
57458348SEric.Yu@Sun.COM 	switch (cmd) {
57468477SRao.Shoaib@Sun.COM 	case SIOCSQPTR:
57478477SRao.Shoaib@Sun.COM 		/*
57488477SRao.Shoaib@Sun.COM 		 * SIOCSQPTR is valid only when helper stream is created
57498477SRao.Shoaib@Sun.COM 		 * by the protocol.
57508477SRao.Shoaib@Sun.COM 		 */
57518348SEric.Yu@Sun.COM 	case _I_INSERT:
57528348SEric.Yu@Sun.COM 	case _I_REMOVE:
57538348SEric.Yu@Sun.COM 		/*
57548348SEric.Yu@Sun.COM 		 * Since there's no compelling reason to support these ioctls
57558348SEric.Yu@Sun.COM 		 * on sockets, and doing so would increase the complexity
57568348SEric.Yu@Sun.COM 		 * markedly, prevent it.
57578348SEric.Yu@Sun.COM 		 */
57588348SEric.Yu@Sun.COM 		return (EOPNOTSUPP);
57598348SEric.Yu@Sun.COM 
57608348SEric.Yu@Sun.COM 	case I_FIND:
57618348SEric.Yu@Sun.COM 	case I_LIST:
57628348SEric.Yu@Sun.COM 	case I_LOOK:
57638348SEric.Yu@Sun.COM 	case I_POP:
57648348SEric.Yu@Sun.COM 	case I_PUSH:
57658348SEric.Yu@Sun.COM 		/*
57668348SEric.Yu@Sun.COM 		 * To prevent races and inconsistencies between the actual
57678348SEric.Yu@Sun.COM 		 * state of the stream and the state according to the sonode,
57688348SEric.Yu@Sun.COM 		 * we serialize all operations which modify or operate on the
57698348SEric.Yu@Sun.COM 		 * list of modules on the socket's stream.
57708348SEric.Yu@Sun.COM 		 */
57718348SEric.Yu@Sun.COM 		mutex_enter(&sti->sti_plumb_lock);
57728348SEric.Yu@Sun.COM 		error = socktpi_plumbioctl(vp, cmd, arg, mode, cr, rvalp);
57738348SEric.Yu@Sun.COM 		mutex_exit(&sti->sti_plumb_lock);
57748348SEric.Yu@Sun.COM 		return (error);
57758348SEric.Yu@Sun.COM 
57768348SEric.Yu@Sun.COM 	default:
57778348SEric.Yu@Sun.COM 		if (so->so_version != SOV_STREAM)
57788348SEric.Yu@Sun.COM 			break;
57798348SEric.Yu@Sun.COM 
57808348SEric.Yu@Sun.COM 		/*
57818348SEric.Yu@Sun.COM 		 * The imaginary "sockmod" has been popped; act as a stream.
57828348SEric.Yu@Sun.COM 		 */
57838348SEric.Yu@Sun.COM 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
57848348SEric.Yu@Sun.COM 	}
57858348SEric.Yu@Sun.COM 
57868348SEric.Yu@Sun.COM 	ASSERT(so->so_version != SOV_STREAM);
57878348SEric.Yu@Sun.COM 
57888348SEric.Yu@Sun.COM 	/*
57898348SEric.Yu@Sun.COM 	 * Process socket-specific ioctls.
57908348SEric.Yu@Sun.COM 	 */
57918348SEric.Yu@Sun.COM 	switch (cmd) {
57928348SEric.Yu@Sun.COM 	case FIONBIO: {
57938348SEric.Yu@Sun.COM 		int32_t value;
57948348SEric.Yu@Sun.COM 
57958348SEric.Yu@Sun.COM 		if (so_copyin((void *)arg, &value, sizeof (int32_t),
57968348SEric.Yu@Sun.COM 		    (mode & (int)FKIOCTL)))
57978348SEric.Yu@Sun.COM 			return (EFAULT);
57988348SEric.Yu@Sun.COM 
57998348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
58008348SEric.Yu@Sun.COM 		if (value) {
58018348SEric.Yu@Sun.COM 			so->so_state |= SS_NDELAY;
58028348SEric.Yu@Sun.COM 		} else {
58038348SEric.Yu@Sun.COM 			so->so_state &= ~SS_NDELAY;
58048348SEric.Yu@Sun.COM 		}
58058348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
58068348SEric.Yu@Sun.COM 		return (0);
58078348SEric.Yu@Sun.COM 	}
58088348SEric.Yu@Sun.COM 
58098348SEric.Yu@Sun.COM 	case FIOASYNC: {
58108348SEric.Yu@Sun.COM 		int32_t value;
58118348SEric.Yu@Sun.COM 
58128348SEric.Yu@Sun.COM 		if (so_copyin((void *)arg, &value, sizeof (int32_t),
58138348SEric.Yu@Sun.COM 		    (mode & (int)FKIOCTL)))
58148348SEric.Yu@Sun.COM 			return (EFAULT);
58158348SEric.Yu@Sun.COM 
58168348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
58178348SEric.Yu@Sun.COM 		/*
58188348SEric.Yu@Sun.COM 		 * SS_ASYNC flag not already set correctly?
58198348SEric.Yu@Sun.COM 		 * (!value != !(so->so_state & SS_ASYNC))
58208348SEric.Yu@Sun.COM 		 * but some engineers find that too hard to read.
58218348SEric.Yu@Sun.COM 		 */
58228348SEric.Yu@Sun.COM 		if (value == 0 && (so->so_state & SS_ASYNC) != 0 ||
58238348SEric.Yu@Sun.COM 		    value != 0 && (so->so_state & SS_ASYNC) == 0)
58248348SEric.Yu@Sun.COM 			error = so_flip_async(so, vp, mode, cr);
58258348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
58268348SEric.Yu@Sun.COM 		return (error);
58278348SEric.Yu@Sun.COM 	}
58288348SEric.Yu@Sun.COM 
58298348SEric.Yu@Sun.COM 	case SIOCSPGRP:
58308348SEric.Yu@Sun.COM 	case FIOSETOWN: {
58318348SEric.Yu@Sun.COM 		pid_t pgrp;
58328348SEric.Yu@Sun.COM 
58338348SEric.Yu@Sun.COM 		if (so_copyin((void *)arg, &pgrp, sizeof (pid_t),
58348348SEric.Yu@Sun.COM 		    (mode & (int)FKIOCTL)))
58358348SEric.Yu@Sun.COM 			return (EFAULT);
58368348SEric.Yu@Sun.COM 
58378348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
58388348SEric.Yu@Sun.COM 		dprintso(so, 1, ("setown: new %d old %d\n", pgrp, so->so_pgrp));
58398348SEric.Yu@Sun.COM 		/* Any change? */
58408348SEric.Yu@Sun.COM 		if (pgrp != so->so_pgrp)
58418348SEric.Yu@Sun.COM 			error = so_set_siggrp(so, vp, pgrp, mode, cr);
58428348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
58438348SEric.Yu@Sun.COM 		return (error);
58448348SEric.Yu@Sun.COM 	}
58458348SEric.Yu@Sun.COM 	case SIOCGPGRP:
58468348SEric.Yu@Sun.COM 	case FIOGETOWN:
58478348SEric.Yu@Sun.COM 		if (so_copyout(&so->so_pgrp, (void *)arg,
58488348SEric.Yu@Sun.COM 		    sizeof (pid_t), (mode & (int)FKIOCTL)))
58498348SEric.Yu@Sun.COM 			return (EFAULT);
58508348SEric.Yu@Sun.COM 		return (0);
58518348SEric.Yu@Sun.COM 
58528348SEric.Yu@Sun.COM 	case SIOCATMARK: {
58538348SEric.Yu@Sun.COM 		int retval;
58548348SEric.Yu@Sun.COM 		uint_t so_state;
58558348SEric.Yu@Sun.COM 
58568348SEric.Yu@Sun.COM 		/*
58578348SEric.Yu@Sun.COM 		 * strwaitmark has a finite timeout after which it
58588348SEric.Yu@Sun.COM 		 * returns -1 if the mark state is undetermined.
58598348SEric.Yu@Sun.COM 		 * In order to avoid any race between the mark state
58608348SEric.Yu@Sun.COM 		 * in sockfs and the mark state in the stream head this
58618348SEric.Yu@Sun.COM 		 * routine loops until the mark state can be determined
58628348SEric.Yu@Sun.COM 		 * (or the urgent data indication has been removed by some
58638348SEric.Yu@Sun.COM 		 * other thread).
58648348SEric.Yu@Sun.COM 		 */
58658348SEric.Yu@Sun.COM 		do {
58668348SEric.Yu@Sun.COM 			mutex_enter(&so->so_lock);
58678348SEric.Yu@Sun.COM 			so_state = so->so_state;
58688348SEric.Yu@Sun.COM 			mutex_exit(&so->so_lock);
58698348SEric.Yu@Sun.COM 			if (so_state & SS_RCVATMARK) {
58708348SEric.Yu@Sun.COM 				retval = 1;
58718348SEric.Yu@Sun.COM 			} else if (!(so_state & SS_OOBPEND)) {
58728348SEric.Yu@Sun.COM 				/*
58738348SEric.Yu@Sun.COM 				 * No SIGURG has been generated -- there is no
58748348SEric.Yu@Sun.COM 				 * pending or present urgent data. Thus can't
58758348SEric.Yu@Sun.COM 				 * possibly be at the mark.
58768348SEric.Yu@Sun.COM 				 */
58778348SEric.Yu@Sun.COM 				retval = 0;
58788348SEric.Yu@Sun.COM 			} else {
58798348SEric.Yu@Sun.COM 				/*
58808348SEric.Yu@Sun.COM 				 * Have the stream head wait until there is
58818348SEric.Yu@Sun.COM 				 * either some messages on the read queue, or
58828348SEric.Yu@Sun.COM 				 * STRATMARK or STRNOTATMARK gets set. The
58838348SEric.Yu@Sun.COM 				 * STRNOTATMARK flag is used so that the
58848348SEric.Yu@Sun.COM 				 * transport can send up a MSGNOTMARKNEXT
58858348SEric.Yu@Sun.COM 				 * M_DATA to indicate that it is not
58868348SEric.Yu@Sun.COM 				 * at the mark and additional data is not about
58878348SEric.Yu@Sun.COM 				 * to be send upstream.
58888348SEric.Yu@Sun.COM 				 *
58898348SEric.Yu@Sun.COM 				 * If the mark state is undetermined this will
58908348SEric.Yu@Sun.COM 				 * return -1 and we will loop rechecking the
58918348SEric.Yu@Sun.COM 				 * socket state.
58928348SEric.Yu@Sun.COM 				 */
58938348SEric.Yu@Sun.COM 				retval = strwaitmark(vp);
58948348SEric.Yu@Sun.COM 			}
58958348SEric.Yu@Sun.COM 		} while (retval == -1);
58968348SEric.Yu@Sun.COM 
58978348SEric.Yu@Sun.COM 		if (so_copyout(&retval, (void *)arg, sizeof (int),
58988348SEric.Yu@Sun.COM 		    (mode & (int)FKIOCTL)))
58998348SEric.Yu@Sun.COM 			return (EFAULT);
59008348SEric.Yu@Sun.COM 		return (0);
59018348SEric.Yu@Sun.COM 	}
59028348SEric.Yu@Sun.COM 
59038348SEric.Yu@Sun.COM 	case I_FDINSERT:
59048348SEric.Yu@Sun.COM 	case I_SENDFD:
59058348SEric.Yu@Sun.COM 	case I_RECVFD:
59068348SEric.Yu@Sun.COM 	case I_ATMARK:
59078348SEric.Yu@Sun.COM 	case _SIOCSOCKFALLBACK:
59088348SEric.Yu@Sun.COM 		/*
59098348SEric.Yu@Sun.COM 		 * These ioctls do not apply to sockets. I_FDINSERT can be
59108348SEric.Yu@Sun.COM 		 * used to send M_PROTO messages without modifying the socket
59118348SEric.Yu@Sun.COM 		 * state. I_SENDFD/RECVFD should not be used for socket file
59128348SEric.Yu@Sun.COM 		 * descriptor passing since they assume a twisted stream.
59138348SEric.Yu@Sun.COM 		 * SIOCATMARK must be used instead of I_ATMARK.
59148348SEric.Yu@Sun.COM 		 *
59158348SEric.Yu@Sun.COM 		 * _SIOCSOCKFALLBACK from an application should never be
59168348SEric.Yu@Sun.COM 		 * processed.  It is only generated by socktpi_open() or
59178348SEric.Yu@Sun.COM 		 * in response to I_POP or I_PUSH.
59188348SEric.Yu@Sun.COM 		 */
59198348SEric.Yu@Sun.COM #ifdef DEBUG
59208348SEric.Yu@Sun.COM 		zcmn_err(getzoneid(), CE_WARN,
59218348SEric.Yu@Sun.COM 		    "Unsupported STREAMS ioctl 0x%x on socket. "
59228348SEric.Yu@Sun.COM 		    "Pid = %d\n", cmd, curproc->p_pid);
59238348SEric.Yu@Sun.COM #endif /* DEBUG */
59248348SEric.Yu@Sun.COM 		return (EOPNOTSUPP);
59258348SEric.Yu@Sun.COM 
59268348SEric.Yu@Sun.COM 	case _I_GETPEERCRED:
59278348SEric.Yu@Sun.COM 		if ((mode & FKIOCTL) == 0)
59288348SEric.Yu@Sun.COM 			return (EINVAL);
59298348SEric.Yu@Sun.COM 
59308348SEric.Yu@Sun.COM 		mutex_enter(&so->so_lock);
59318348SEric.Yu@Sun.COM 		if ((so->so_mode & SM_CONNREQUIRED) == 0) {
59328348SEric.Yu@Sun.COM 			error = ENOTSUP;
59338348SEric.Yu@Sun.COM 		} else if ((so->so_state & SS_ISCONNECTED) == 0) {
59348348SEric.Yu@Sun.COM 			error = ENOTCONN;
59358348SEric.Yu@Sun.COM 		} else if (so->so_peercred != NULL) {
59368348SEric.Yu@Sun.COM 			k_peercred_t *kp = (k_peercred_t *)arg;
59378348SEric.Yu@Sun.COM 			kp->pc_cr = so->so_peercred;
59388348SEric.Yu@Sun.COM 			kp->pc_cpid = so->so_cpid;
59398348SEric.Yu@Sun.COM 			crhold(so->so_peercred);
59408348SEric.Yu@Sun.COM 		} else {
59418348SEric.Yu@Sun.COM 			error = EINVAL;
59428348SEric.Yu@Sun.COM 		}
59438348SEric.Yu@Sun.COM 		mutex_exit(&so->so_lock);
59448348SEric.Yu@Sun.COM 		return (error);
59458348SEric.Yu@Sun.COM 
59468348SEric.Yu@Sun.COM 	default:
59478348SEric.Yu@Sun.COM 		/*
59488348SEric.Yu@Sun.COM 		 * Do the higher-order bits of the ioctl cmd indicate
59498348SEric.Yu@Sun.COM 		 * that it is an I_* streams ioctl?
59508348SEric.Yu@Sun.COM 		 */
59518348SEric.Yu@Sun.COM 		if ((cmd & 0xffffff00U) == STR &&
59528348SEric.Yu@Sun.COM 		    so->so_version == SOV_SOCKBSD) {
59538348SEric.Yu@Sun.COM #ifdef DEBUG
59548348SEric.Yu@Sun.COM 			zcmn_err(getzoneid(), CE_WARN,
59558348SEric.Yu@Sun.COM 			    "Unsupported STREAMS ioctl 0x%x on socket. "
59568348SEric.Yu@Sun.COM 			    "Pid = %d\n", cmd, 	curproc->p_pid);
59578348SEric.Yu@Sun.COM #endif /* DEBUG */
59588348SEric.Yu@Sun.COM 			return (EOPNOTSUPP);
59598348SEric.Yu@Sun.COM 		}
59608348SEric.Yu@Sun.COM 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
59618348SEric.Yu@Sun.COM 	}
59628348SEric.Yu@Sun.COM }
59638348SEric.Yu@Sun.COM 
59648348SEric.Yu@Sun.COM /*
59658348SEric.Yu@Sun.COM  * Handle plumbing-related ioctls.
59668348SEric.Yu@Sun.COM  */
59678348SEric.Yu@Sun.COM static int
socktpi_plumbioctl(struct vnode * vp,int cmd,intptr_t arg,int mode,struct cred * cr,int32_t * rvalp)59688348SEric.Yu@Sun.COM socktpi_plumbioctl(struct vnode *vp, int cmd, intptr_t arg, int mode,
59698348SEric.Yu@Sun.COM     struct cred *cr, int32_t *rvalp)
59708348SEric.Yu@Sun.COM {
59718348SEric.Yu@Sun.COM 	static const char sockmod_name[] = "sockmod";
59728348SEric.Yu@Sun.COM 	struct sonode	*so = VTOSO(vp);
59738348SEric.Yu@Sun.COM 	char		mname[FMNAMESZ + 1];
59748348SEric.Yu@Sun.COM 	int		error;
59758348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
59768348SEric.Yu@Sun.COM 
59778348SEric.Yu@Sun.COM 	ASSERT(MUTEX_HELD(&sti->sti_plumb_lock));
59788348SEric.Yu@Sun.COM 
59798348SEric.Yu@Sun.COM 	if (so->so_version == SOV_SOCKBSD)
59808348SEric.Yu@Sun.COM 		return (EOPNOTSUPP);
59818348SEric.Yu@Sun.COM 
59828348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
59838348SEric.Yu@Sun.COM 		/*
59848348SEric.Yu@Sun.COM 		 * The imaginary "sockmod" has been popped - act as a stream.
59858348SEric.Yu@Sun.COM 		 * If this is a push of sockmod then change back to a socket.
59868348SEric.Yu@Sun.COM 		 */
59878348SEric.Yu@Sun.COM 		if (cmd == I_PUSH) {
59888348SEric.Yu@Sun.COM 			error = ((mode & FKIOCTL) ? copystr : copyinstr)(
59898348SEric.Yu@Sun.COM 			    (void *)arg, mname, sizeof (mname), NULL);
59908348SEric.Yu@Sun.COM 
59918348SEric.Yu@Sun.COM 			if (error == 0 && strcmp(mname, sockmod_name) == 0) {
59928348SEric.Yu@Sun.COM 				dprintso(so, 0, ("socktpi_ioctl: going to "
59938348SEric.Yu@Sun.COM 				    "socket version\n"));
59948348SEric.Yu@Sun.COM 				so_stream2sock(so);
59958348SEric.Yu@Sun.COM 				return (0);
59968348SEric.Yu@Sun.COM 			}
59978348SEric.Yu@Sun.COM 		}
59988348SEric.Yu@Sun.COM 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
59998348SEric.Yu@Sun.COM 	}
60008348SEric.Yu@Sun.COM 
60018348SEric.Yu@Sun.COM 	switch (cmd) {
60028348SEric.Yu@Sun.COM 	case I_PUSH:
60038348SEric.Yu@Sun.COM 		if (sti->sti_direct) {
60048348SEric.Yu@Sun.COM 			mutex_enter(&so->so_lock);
60058348SEric.Yu@Sun.COM 			so_lock_single(so);
60068348SEric.Yu@Sun.COM 			mutex_exit(&so->so_lock);
60078348SEric.Yu@Sun.COM 
60088348SEric.Yu@Sun.COM 			error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
60098778SErik.Nordmark@Sun.COM 			    cr, rvalp);
60108348SEric.Yu@Sun.COM 
60118348SEric.Yu@Sun.COM 			mutex_enter(&so->so_lock);
60128348SEric.Yu@Sun.COM 			if (error == 0)
60138348SEric.Yu@Sun.COM 				sti->sti_direct = 0;
60148348SEric.Yu@Sun.COM 			so_unlock_single(so, SOLOCKED);
60158348SEric.Yu@Sun.COM 			mutex_exit(&so->so_lock);
60168348SEric.Yu@Sun.COM 
60178348SEric.Yu@Sun.COM 			if (error != 0)
60188348SEric.Yu@Sun.COM 				return (error);
60198348SEric.Yu@Sun.COM 		}
60208348SEric.Yu@Sun.COM 
60218348SEric.Yu@Sun.COM 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60228348SEric.Yu@Sun.COM 		if (error == 0)
60238348SEric.Yu@Sun.COM 			sti->sti_pushcnt++;
60248348SEric.Yu@Sun.COM 		return (error);
60258348SEric.Yu@Sun.COM 
60268348SEric.Yu@Sun.COM 	case I_POP:
60278348SEric.Yu@Sun.COM 		if (sti->sti_pushcnt == 0) {
60288348SEric.Yu@Sun.COM 			/* Emulate sockmod being popped */
60298348SEric.Yu@Sun.COM 			dprintso(so, 0,
60308348SEric.Yu@Sun.COM 			    ("socktpi_ioctl: going to STREAMS version\n"));
60318348SEric.Yu@Sun.COM 			return (so_sock2stream(so));
60328348SEric.Yu@Sun.COM 		}
60338348SEric.Yu@Sun.COM 
60348348SEric.Yu@Sun.COM 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60358348SEric.Yu@Sun.COM 		if (error == 0)
60368348SEric.Yu@Sun.COM 			sti->sti_pushcnt--;
60378348SEric.Yu@Sun.COM 		return (error);
60388348SEric.Yu@Sun.COM 
60398348SEric.Yu@Sun.COM 	case I_LIST: {
60408348SEric.Yu@Sun.COM 		struct str_mlist *kmlistp, *umlistp;
60418348SEric.Yu@Sun.COM 		struct str_list	kstrlist;
60428348SEric.Yu@Sun.COM 		ssize_t		kstrlistsize;
60438348SEric.Yu@Sun.COM 		int		i, nmods;
60448348SEric.Yu@Sun.COM 
60458348SEric.Yu@Sun.COM 		STRUCT_DECL(str_list, ustrlist);
60468348SEric.Yu@Sun.COM 		STRUCT_INIT(ustrlist, mode);
60478348SEric.Yu@Sun.COM 
60488348SEric.Yu@Sun.COM 		if (arg == NULL) {
60498348SEric.Yu@Sun.COM 			error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60508348SEric.Yu@Sun.COM 			if (error == 0)
60518348SEric.Yu@Sun.COM 				(*rvalp)++;	/* Add one for sockmod */
60528348SEric.Yu@Sun.COM 			return (error);
60538348SEric.Yu@Sun.COM 		}
60548348SEric.Yu@Sun.COM 
60558348SEric.Yu@Sun.COM 		error = so_copyin((void *)arg, STRUCT_BUF(ustrlist),
60568348SEric.Yu@Sun.COM 		    STRUCT_SIZE(ustrlist), mode & FKIOCTL);
60578348SEric.Yu@Sun.COM 		if (error != 0)
60588348SEric.Yu@Sun.COM 			return (error);
60598348SEric.Yu@Sun.COM 
60608348SEric.Yu@Sun.COM 		nmods = STRUCT_FGET(ustrlist, sl_nmods);
60618348SEric.Yu@Sun.COM 		if (nmods <= 0)
60628348SEric.Yu@Sun.COM 			return (EINVAL);
60638348SEric.Yu@Sun.COM 		/*
60648348SEric.Yu@Sun.COM 		 * Ceiling nmods at nstrpush to prevent someone from
60658348SEric.Yu@Sun.COM 		 * maliciously consuming lots of kernel memory.
60668348SEric.Yu@Sun.COM 		 */
60678348SEric.Yu@Sun.COM 		nmods = MIN(nmods, nstrpush);
60688348SEric.Yu@Sun.COM 
60698348SEric.Yu@Sun.COM 		kstrlistsize = (nmods + 1) * sizeof (struct str_mlist);
60708348SEric.Yu@Sun.COM 		kstrlist.sl_nmods = nmods;
60718348SEric.Yu@Sun.COM 		kstrlist.sl_modlist = kmem_zalloc(kstrlistsize, KM_SLEEP);
60728348SEric.Yu@Sun.COM 
60738348SEric.Yu@Sun.COM 		error = strioctl(vp, cmd, (intptr_t)&kstrlist, mode, K_TO_K,
60748348SEric.Yu@Sun.COM 		    cr, rvalp);
60758348SEric.Yu@Sun.COM 		if (error != 0)
60768348SEric.Yu@Sun.COM 			goto done;
60778348SEric.Yu@Sun.COM 
60788348SEric.Yu@Sun.COM 		/*
60798348SEric.Yu@Sun.COM 		 * Considering the module list as a 0-based array of sl_nmods
60808348SEric.Yu@Sun.COM 		 * modules, sockmod should conceptually exist at slot
60818348SEric.Yu@Sun.COM 		 * sti_pushcnt.  Insert sockmod at this location by sliding all
60828348SEric.Yu@Sun.COM 		 * of the module names after so_pushcnt over by one.  We know
60838348SEric.Yu@Sun.COM 		 * that there will be room to do this since we allocated
60848348SEric.Yu@Sun.COM 		 * sl_modlist with an additional slot.
60858348SEric.Yu@Sun.COM 		 */
60868348SEric.Yu@Sun.COM 		for (i = kstrlist.sl_nmods; i > sti->sti_pushcnt; i--)
60878348SEric.Yu@Sun.COM 			kstrlist.sl_modlist[i] = kstrlist.sl_modlist[i - 1];
60888348SEric.Yu@Sun.COM 
60898348SEric.Yu@Sun.COM 		(void) strcpy(kstrlist.sl_modlist[i].l_name, sockmod_name);
60908348SEric.Yu@Sun.COM 		kstrlist.sl_nmods++;
60918348SEric.Yu@Sun.COM 
60928348SEric.Yu@Sun.COM 		/*
60938348SEric.Yu@Sun.COM 		 * Copy all of the entries out to ustrlist.
60948348SEric.Yu@Sun.COM 		 */
60958348SEric.Yu@Sun.COM 		kmlistp = kstrlist.sl_modlist;
60968348SEric.Yu@Sun.COM 		umlistp = STRUCT_FGETP(ustrlist, sl_modlist);
60978348SEric.Yu@Sun.COM 		for (i = 0; i < nmods && i < kstrlist.sl_nmods; i++) {
60988348SEric.Yu@Sun.COM 			error = so_copyout(kmlistp++, umlistp++,
60998348SEric.Yu@Sun.COM 			    sizeof (struct str_mlist), mode & FKIOCTL);
61008348SEric.Yu@Sun.COM 			if (error != 0)
61018348SEric.Yu@Sun.COM 				goto done;
61028348SEric.Yu@Sun.COM 		}
61038348SEric.Yu@Sun.COM 
61048348SEric.Yu@Sun.COM 		error = so_copyout(&i, (void *)arg, sizeof (int32_t),
61058348SEric.Yu@Sun.COM 		    mode & FKIOCTL);
61068348SEric.Yu@Sun.COM 		if (error == 0)
61078348SEric.Yu@Sun.COM 			*rvalp = 0;
61088348SEric.Yu@Sun.COM 	done:
61098348SEric.Yu@Sun.COM 		kmem_free(kstrlist.sl_modlist, kstrlistsize);
61108348SEric.Yu@Sun.COM 		return (error);
61118348SEric.Yu@Sun.COM 	}
61128348SEric.Yu@Sun.COM 	case I_LOOK:
61138348SEric.Yu@Sun.COM 		if (sti->sti_pushcnt == 0) {
61148348SEric.Yu@Sun.COM 			return (so_copyout(sockmod_name, (void *)arg,
61158348SEric.Yu@Sun.COM 			    sizeof (sockmod_name), mode & FKIOCTL));
61168348SEric.Yu@Sun.COM 		}
61178348SEric.Yu@Sun.COM 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
61188348SEric.Yu@Sun.COM 
61198348SEric.Yu@Sun.COM 	case I_FIND:
61208348SEric.Yu@Sun.COM 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
61218348SEric.Yu@Sun.COM 		if (error && error != EINVAL)
61228348SEric.Yu@Sun.COM 			return (error);
61238348SEric.Yu@Sun.COM 
61248348SEric.Yu@Sun.COM 		/* if not found and string was sockmod return 1 */
61258348SEric.Yu@Sun.COM 		if (*rvalp == 0 || error == EINVAL) {
61268348SEric.Yu@Sun.COM 			error = ((mode & FKIOCTL) ? copystr : copyinstr)(
61278348SEric.Yu@Sun.COM 			    (void *)arg, mname, sizeof (mname), NULL);
61288348SEric.Yu@Sun.COM 			if (error == ENAMETOOLONG)
61298348SEric.Yu@Sun.COM 				error = EINVAL;
61308348SEric.Yu@Sun.COM 
61318348SEric.Yu@Sun.COM 			if (error == 0 && strcmp(mname, sockmod_name) == 0)
61328348SEric.Yu@Sun.COM 				*rvalp = 1;
61338348SEric.Yu@Sun.COM 		}
61348348SEric.Yu@Sun.COM 		return (error);
61358348SEric.Yu@Sun.COM 
61368348SEric.Yu@Sun.COM 	default:
61378348SEric.Yu@Sun.COM 		panic("socktpi_plumbioctl: unknown ioctl %d", cmd);
61388348SEric.Yu@Sun.COM 		break;
61398348SEric.Yu@Sun.COM 	}
61408348SEric.Yu@Sun.COM 
61418348SEric.Yu@Sun.COM 	return (0);
61428348SEric.Yu@Sun.COM }
61438348SEric.Yu@Sun.COM 
61448348SEric.Yu@Sun.COM /*
61458348SEric.Yu@Sun.COM  * Wrapper around the streams poll routine that implements socket poll
61468348SEric.Yu@Sun.COM  * semantics.
61478348SEric.Yu@Sun.COM  * The sockfs never calls pollwakeup itself - the stream head take care
61488348SEric.Yu@Sun.COM  * of all pollwakeups. Since sockfs never holds so_lock when calling the
61498348SEric.Yu@Sun.COM  * stream head there can never be a deadlock due to holding so_lock across
61508348SEric.Yu@Sun.COM  * pollwakeup and acquiring so_lock in this routine.
61518348SEric.Yu@Sun.COM  *
61528348SEric.Yu@Sun.COM  * However, since the performance of VOP_POLL is critical we avoid
61538348SEric.Yu@Sun.COM  * acquiring so_lock here. This is based on two assumptions:
61548348SEric.Yu@Sun.COM  *  - The poll implementation holds locks to serialize the VOP_POLL call
61558348SEric.Yu@Sun.COM  *    and a pollwakeup for the same pollhead. This ensures that should
61568348SEric.Yu@Sun.COM  *    e.g. so_state change during a socktpi_poll call the pollwakeup
61578348SEric.Yu@Sun.COM  *    (which strsock_* and strrput conspire to issue) is issued after
61588348SEric.Yu@Sun.COM  *    the state change. Thus the pollwakeup will block until VOP_POLL has
61598348SEric.Yu@Sun.COM  *    returned and then wake up poll and have it call VOP_POLL again.
61608348SEric.Yu@Sun.COM  *  - The reading of so_state without holding so_lock does not result in
61618348SEric.Yu@Sun.COM  *    stale data that is older than the latest state change that has dropped
61628348SEric.Yu@Sun.COM  *    so_lock. This is ensured by the mutex_exit issuing the appropriate
61638348SEric.Yu@Sun.COM  *    memory barrier to force the data into the coherency domain.
61648348SEric.Yu@Sun.COM  */
61658348SEric.Yu@Sun.COM static int
sotpi_poll(struct sonode * so,short events,int anyyet,short * reventsp,struct pollhead ** phpp)61668348SEric.Yu@Sun.COM sotpi_poll(
61678348SEric.Yu@Sun.COM 	struct sonode	*so,
61688348SEric.Yu@Sun.COM 	short		events,
61698348SEric.Yu@Sun.COM 	int		anyyet,
61708348SEric.Yu@Sun.COM 	short		*reventsp,
61718348SEric.Yu@Sun.COM 	struct pollhead **phpp)
61728348SEric.Yu@Sun.COM {
61738348SEric.Yu@Sun.COM 	short origevents = events;
61748348SEric.Yu@Sun.COM 	struct vnode *vp = SOTOV(so);
61758348SEric.Yu@Sun.COM 	int error;
61768348SEric.Yu@Sun.COM 	int so_state = so->so_state;	/* snapshot */
61778348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
61788348SEric.Yu@Sun.COM 
61798348SEric.Yu@Sun.COM 	dprintso(so, 0, ("socktpi_poll(%p): state %s err %d\n",
61808348SEric.Yu@Sun.COM 	    (void *)vp, pr_state(so_state, so->so_mode), so->so_error));
61818348SEric.Yu@Sun.COM 
61828348SEric.Yu@Sun.COM 	ASSERT(vp->v_type == VSOCK);
61838348SEric.Yu@Sun.COM 	ASSERT(vp->v_stream != NULL);
61848348SEric.Yu@Sun.COM 
61858348SEric.Yu@Sun.COM 	if (so->so_version == SOV_STREAM) {
61868348SEric.Yu@Sun.COM 		/* The imaginary "sockmod" has been popped - act as a stream */
61878348SEric.Yu@Sun.COM 		return (strpoll(vp->v_stream, events, anyyet,
61888348SEric.Yu@Sun.COM 		    reventsp, phpp));
61898348SEric.Yu@Sun.COM 	}
61908348SEric.Yu@Sun.COM 
61918348SEric.Yu@Sun.COM 	if (!(so_state & SS_ISCONNECTED) &&
61928348SEric.Yu@Sun.COM 	    (so->so_mode & SM_CONNREQUIRED)) {
61938348SEric.Yu@Sun.COM 		/* Not connected yet - turn off write side events */
61948348SEric.Yu@Sun.COM 		events &= ~(POLLOUT|POLLWRBAND);
61958348SEric.Yu@Sun.COM 	}
61968348SEric.Yu@Sun.COM 	/*
61978348SEric.Yu@Sun.COM 	 * Check for errors without calling strpoll if the caller wants them.
61988348SEric.Yu@Sun.COM 	 * In sockets the errors are represented as input/output events
61998348SEric.Yu@Sun.COM 	 * and there is no need to ask the stream head for this information.
62008348SEric.Yu@Sun.COM 	 */
62018348SEric.Yu@Sun.COM 	if (so->so_error != 0 &&
62028348SEric.Yu@Sun.COM 	    ((POLLIN|POLLRDNORM|POLLOUT) & origevents)  != 0) {
62038348SEric.Yu@Sun.COM 		*reventsp = (POLLIN|POLLRDNORM|POLLOUT) & origevents;
62048348SEric.Yu@Sun.COM 		return (0);
62058348SEric.Yu@Sun.COM 	}
62068348SEric.Yu@Sun.COM 	/*
62078348SEric.Yu@Sun.COM 	 * Ignore M_PROTO only messages such as the T_EXDATA_IND messages.
62088348SEric.Yu@Sun.COM 	 * These message with only an M_PROTO/M_PCPROTO part and no M_DATA
62098348SEric.Yu@Sun.COM 	 * will not trigger a POLLIN event with POLLRDDATA set.
62108348SEric.Yu@Sun.COM 	 * The handling of urgent data (causing POLLRDBAND) is done by
62118348SEric.Yu@Sun.COM 	 * inspecting SS_OOBPEND below.
62128348SEric.Yu@Sun.COM 	 */
62138348SEric.Yu@Sun.COM 	events |= POLLRDDATA;
62148348SEric.Yu@Sun.COM 
62158348SEric.Yu@Sun.COM 	/*
62168348SEric.Yu@Sun.COM 	 * After shutdown(output) a stream head write error is set.
62178348SEric.Yu@Sun.COM 	 * However, we should not return output events.
62188348SEric.Yu@Sun.COM 	 */
62198348SEric.Yu@Sun.COM 	events |= POLLNOERR;
62208348SEric.Yu@Sun.COM 	error = strpoll(vp->v_stream, events, anyyet,
62218348SEric.Yu@Sun.COM 	    reventsp, phpp);
62228348SEric.Yu@Sun.COM 	if (error)
62238348SEric.Yu@Sun.COM 		return (error);
62248348SEric.Yu@Sun.COM 
62258348SEric.Yu@Sun.COM 	ASSERT(!(*reventsp & POLLERR));
62268348SEric.Yu@Sun.COM 
62278348SEric.Yu@Sun.COM 	/*
62288348SEric.Yu@Sun.COM 	 * Notes on T_CONN_IND handling for sockets.
62298348SEric.Yu@Sun.COM 	 *
62308348SEric.Yu@Sun.COM 	 * If strpoll() returned without events, SR_POLLIN is guaranteed
62318348SEric.Yu@Sun.COM 	 * to be set, ensuring any subsequent strrput() runs pollwakeup().
62328348SEric.Yu@Sun.COM 	 *
62338348SEric.Yu@Sun.COM 	 * Since the so_lock is not held, soqueueconnind() may have run
62348348SEric.Yu@Sun.COM 	 * and a T_CONN_IND may be waiting. We now check for any queued
62358348SEric.Yu@Sun.COM 	 * T_CONN_IND msgs on sti_conn_ind_head and set appropriate events
62368348SEric.Yu@Sun.COM 	 * to ensure poll returns.
62378348SEric.Yu@Sun.COM 	 *
62388348SEric.Yu@Sun.COM 	 * However:
62398348SEric.Yu@Sun.COM 	 * If the T_CONN_IND hasn't arrived by the time strpoll() returns,
62408348SEric.Yu@Sun.COM 	 * when strrput() does run for an arriving M_PROTO with T_CONN_IND
62418348SEric.Yu@Sun.COM 	 * the following actions will occur; taken together they ensure the
62428348SEric.Yu@Sun.COM 	 * syscall will return.
62438348SEric.Yu@Sun.COM 	 *
62448348SEric.Yu@Sun.COM 	 * 1. If a socket, soqueueconnind() will queue the T_CONN_IND but if
62458348SEric.Yu@Sun.COM 	 *    the accept() was run on a non-blocking socket sowaitconnind()
62468348SEric.Yu@Sun.COM 	 *    may have already returned EWOULDBLOCK, so not be waiting to
62478348SEric.Yu@Sun.COM 	 *    process the message. Additionally socktpi_poll() has probably
62488348SEric.Yu@Sun.COM 	 *    proceeded past the sti_conn_ind_head check below.
62498348SEric.Yu@Sun.COM 	 * 2. strrput() runs pollwakeup()->pollnotify()->cv_signal() to wake
62508348SEric.Yu@Sun.COM 	 *    this thread,  however that could occur before poll_common()
62518348SEric.Yu@Sun.COM 	 *    has entered cv_wait.
62528348SEric.Yu@Sun.COM 	 * 3. pollnotify() sets T_POLLWAKE, while holding the pc_lock.
62538348SEric.Yu@Sun.COM 	 *
62548348SEric.Yu@Sun.COM 	 * Before proceeding to cv_wait() in poll_common() for an event,
62558348SEric.Yu@Sun.COM 	 * poll_common() atomically checks for T_POLLWAKE under the pc_lock,
62568348SEric.Yu@Sun.COM 	 * and if set, re-calls strpoll() to ensure the late arriving
62578348SEric.Yu@Sun.COM 	 * T_CONN_IND is recognized, and pollsys() returns.
62588348SEric.Yu@Sun.COM 	 */
62598348SEric.Yu@Sun.COM 
62608348SEric.Yu@Sun.COM 	if (sti->sti_conn_ind_head != NULL)
62618348SEric.Yu@Sun.COM 		*reventsp |= (POLLIN|POLLRDNORM) & events;
62628348SEric.Yu@Sun.COM 
62638348SEric.Yu@Sun.COM 	if (so->so_state & SS_OOBPEND)
62648348SEric.Yu@Sun.COM 		*reventsp |= POLLRDBAND & events;
62658348SEric.Yu@Sun.COM 
62668348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_rcv_mp != NULL) {
62678348SEric.Yu@Sun.COM 		*reventsp |= (POLLIN|POLLRDNORM) & events;
62688348SEric.Yu@Sun.COM 	}
62698348SEric.Yu@Sun.COM 	if ((sti->sti_nl7c_flags & NL7C_ENABLED) &&
62708348SEric.Yu@Sun.COM 	    ((POLLIN|POLLRDNORM) & *reventsp)) {
62718348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags |= NL7C_POLLIN;
62728348SEric.Yu@Sun.COM 	}
62738348SEric.Yu@Sun.COM 
62748348SEric.Yu@Sun.COM 	return (0);
62758348SEric.Yu@Sun.COM }
62768348SEric.Yu@Sun.COM 
62778348SEric.Yu@Sun.COM /*ARGSUSED*/
62788348SEric.Yu@Sun.COM static int
socktpi_constructor(void * buf,void * cdrarg,int kmflags)62798348SEric.Yu@Sun.COM socktpi_constructor(void *buf, void *cdrarg, int kmflags)
62808348SEric.Yu@Sun.COM {
62818348SEric.Yu@Sun.COM 	sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
62828348SEric.Yu@Sun.COM 	int error = 0;
62838348SEric.Yu@Sun.COM 
62848348SEric.Yu@Sun.COM 	error = sonode_constructor(buf, cdrarg, kmflags);
62858348SEric.Yu@Sun.COM 	if (error != 0)
62868348SEric.Yu@Sun.COM 		return (error);
62878348SEric.Yu@Sun.COM 
62888348SEric.Yu@Sun.COM 	error = i_sotpi_info_constructor(&st->st_info);
62898348SEric.Yu@Sun.COM 	if (error != 0)
62908348SEric.Yu@Sun.COM 		sonode_destructor(buf, cdrarg);
62918348SEric.Yu@Sun.COM 
62928348SEric.Yu@Sun.COM 	st->st_sonode.so_priv = &st->st_info;
62938348SEric.Yu@Sun.COM 
62948348SEric.Yu@Sun.COM 	return (error);
62958348SEric.Yu@Sun.COM }
62968348SEric.Yu@Sun.COM 
62978348SEric.Yu@Sun.COM /*ARGSUSED1*/
62988348SEric.Yu@Sun.COM static void
socktpi_destructor(void * buf,void * cdrarg)62998348SEric.Yu@Sun.COM socktpi_destructor(void *buf, void *cdrarg)
63008348SEric.Yu@Sun.COM {
63018348SEric.Yu@Sun.COM 	sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
63028348SEric.Yu@Sun.COM 
63038348SEric.Yu@Sun.COM 	ASSERT(st->st_sonode.so_priv == &st->st_info);
63048348SEric.Yu@Sun.COM 	st->st_sonode.so_priv = NULL;
63058348SEric.Yu@Sun.COM 
63068348SEric.Yu@Sun.COM 	i_sotpi_info_destructor(&st->st_info);
63078348SEric.Yu@Sun.COM 	sonode_destructor(buf, cdrarg);
63088348SEric.Yu@Sun.COM }
63098348SEric.Yu@Sun.COM 
63108348SEric.Yu@Sun.COM static int
socktpi_unix_constructor(void * buf,void * cdrarg,int kmflags)63118348SEric.Yu@Sun.COM socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags)
63128348SEric.Yu@Sun.COM {
63138348SEric.Yu@Sun.COM 	int retval;
63148348SEric.Yu@Sun.COM 
63158348SEric.Yu@Sun.COM 	if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) {
63168348SEric.Yu@Sun.COM 		struct sonode *so = (struct sonode *)buf;
63178348SEric.Yu@Sun.COM 		sotpi_info_t *sti = SOTOTPI(so);
63188348SEric.Yu@Sun.COM 
63198348SEric.Yu@Sun.COM 		mutex_enter(&socklist.sl_lock);
63208348SEric.Yu@Sun.COM 
63218348SEric.Yu@Sun.COM 		sti->sti_next_so = socklist.sl_list;
63228348SEric.Yu@Sun.COM 		sti->sti_prev_so = NULL;
63238348SEric.Yu@Sun.COM 		if (sti->sti_next_so != NULL)
63248348SEric.Yu@Sun.COM 			SOTOTPI(sti->sti_next_so)->sti_prev_so = so;
63258348SEric.Yu@Sun.COM 		socklist.sl_list = so;
63268348SEric.Yu@Sun.COM 
63278348SEric.Yu@Sun.COM 		mutex_exit(&socklist.sl_lock);
63288348SEric.Yu@Sun.COM 
63298348SEric.Yu@Sun.COM 	}
63308348SEric.Yu@Sun.COM 	return (retval);
63318348SEric.Yu@Sun.COM }
63328348SEric.Yu@Sun.COM 
63338348SEric.Yu@Sun.COM static void
socktpi_unix_destructor(void * buf,void * cdrarg)63348348SEric.Yu@Sun.COM socktpi_unix_destructor(void *buf, void *cdrarg)
63358348SEric.Yu@Sun.COM {
63368348SEric.Yu@Sun.COM 	struct sonode	*so = (struct sonode *)buf;
63378348SEric.Yu@Sun.COM 	sotpi_info_t	*sti = SOTOTPI(so);
63388348SEric.Yu@Sun.COM 
63398348SEric.Yu@Sun.COM 	mutex_enter(&socklist.sl_lock);
63408348SEric.Yu@Sun.COM 
63418348SEric.Yu@Sun.COM 	if (sti->sti_next_so != NULL)
63428348SEric.Yu@Sun.COM 		SOTOTPI(sti->sti_next_so)->sti_prev_so = sti->sti_prev_so;
63438348SEric.Yu@Sun.COM 	if (sti->sti_prev_so != NULL)
63448348SEric.Yu@Sun.COM 		SOTOTPI(sti->sti_prev_so)->sti_next_so = sti->sti_next_so;
63458348SEric.Yu@Sun.COM 	else
63468348SEric.Yu@Sun.COM 		socklist.sl_list = sti->sti_next_so;
63478348SEric.Yu@Sun.COM 
63488348SEric.Yu@Sun.COM 	mutex_exit(&socklist.sl_lock);
63498348SEric.Yu@Sun.COM 
63508348SEric.Yu@Sun.COM 	socktpi_destructor(buf, cdrarg);
63518348SEric.Yu@Sun.COM }
63528348SEric.Yu@Sun.COM 
63538348SEric.Yu@Sun.COM int
socktpi_init(void)63548348SEric.Yu@Sun.COM socktpi_init(void)
63558348SEric.Yu@Sun.COM {
63568348SEric.Yu@Sun.COM 	/*
63578348SEric.Yu@Sun.COM 	 * Create sonode caches.  We create a special one for AF_UNIX so
63588348SEric.Yu@Sun.COM 	 * that we can track them for netstat(1m).
63598348SEric.Yu@Sun.COM 	 */
63608348SEric.Yu@Sun.COM 	socktpi_cache = kmem_cache_create("socktpi_cache",
63618348SEric.Yu@Sun.COM 	    sizeof (struct sotpi_sonode), 0, socktpi_constructor,
63628348SEric.Yu@Sun.COM 	    socktpi_destructor, NULL, NULL, NULL, 0);
63638348SEric.Yu@Sun.COM 
63648348SEric.Yu@Sun.COM 	socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache",
63658348SEric.Yu@Sun.COM 	    sizeof (struct sotpi_sonode), 0, socktpi_unix_constructor,
63668348SEric.Yu@Sun.COM 	    socktpi_unix_destructor, NULL, NULL, NULL, 0);
63678348SEric.Yu@Sun.COM 
63688348SEric.Yu@Sun.COM 	return (0);
63698348SEric.Yu@Sun.COM }
63708348SEric.Yu@Sun.COM 
63718348SEric.Yu@Sun.COM /*
63728348SEric.Yu@Sun.COM  * Given a non-TPI sonode, allocate and prep it to be ready for TPI.
63738348SEric.Yu@Sun.COM  *
63748348SEric.Yu@Sun.COM  * Caller must still update state and mode using sotpi_update_state().
63758348SEric.Yu@Sun.COM  */
63768963SAnders.Persson@Sun.COM int
sotpi_convert_sonode(struct sonode * so,struct sockparams * newsp,boolean_t * direct,queue_t ** qp,struct cred * cr)63778348SEric.Yu@Sun.COM sotpi_convert_sonode(struct sonode *so, struct sockparams *newsp,
63788963SAnders.Persson@Sun.COM     boolean_t *direct, queue_t **qp, struct cred *cr)
63798348SEric.Yu@Sun.COM {
63808348SEric.Yu@Sun.COM 	sotpi_info_t *sti;
63818348SEric.Yu@Sun.COM 	struct sockparams *origsp = so->so_sockparams;
63828348SEric.Yu@Sun.COM 	sock_lower_handle_t handle = so->so_proto_handle;
63838348SEric.Yu@Sun.COM 	struct stdata *stp;
63848348SEric.Yu@Sun.COM 	struct vnode *vp;
63858348SEric.Yu@Sun.COM 	queue_t *q;
63868963SAnders.Persson@Sun.COM 	int error = 0;
63878963SAnders.Persson@Sun.COM 
63888963SAnders.Persson@Sun.COM 	ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
63898963SAnders.Persson@Sun.COM 	    SS_FALLBACK_PENDING);
63908963SAnders.Persson@Sun.COM 	ASSERT(SOCK_IS_NONSTR(so));
63918963SAnders.Persson@Sun.COM 
63928963SAnders.Persson@Sun.COM 	*qp = NULL;
63938348SEric.Yu@Sun.COM 	*direct = B_FALSE;
63948348SEric.Yu@Sun.COM 	so->so_sockparams = newsp;
63958348SEric.Yu@Sun.COM 	/*
63968348SEric.Yu@Sun.COM 	 * Allocate and initalize fields required by TPI.
63978348SEric.Yu@Sun.COM 	 */
63988348SEric.Yu@Sun.COM 	(void) sotpi_info_create(so, KM_SLEEP);
63998348SEric.Yu@Sun.COM 	sotpi_info_init(so);
64008348SEric.Yu@Sun.COM 
64018963SAnders.Persson@Sun.COM 	if ((error = sotpi_init(so, NULL, cr, SO_FALLBACK)) != 0) {
64028348SEric.Yu@Sun.COM 		sotpi_info_fini(so);
64038348SEric.Yu@Sun.COM 		sotpi_info_destroy(so);
64048963SAnders.Persson@Sun.COM 		return (error);
64058348SEric.Yu@Sun.COM 	}
64068348SEric.Yu@Sun.COM 	ASSERT(handle == so->so_proto_handle);
64078348SEric.Yu@Sun.COM 	sti = SOTOTPI(so);
64088348SEric.Yu@Sun.COM 	if (sti->sti_direct != 0)
64098348SEric.Yu@Sun.COM 		*direct = B_TRUE;
64108348SEric.Yu@Sun.COM 
64118348SEric.Yu@Sun.COM 	/*
64128348SEric.Yu@Sun.COM 	 * Keep the original sp around so we can properly dispose of the
64138348SEric.Yu@Sun.COM 	 * sonode when the socket is being closed.
64148348SEric.Yu@Sun.COM 	 */
64158348SEric.Yu@Sun.COM 	sti->sti_orig_sp = origsp;
64168348SEric.Yu@Sun.COM 
64178348SEric.Yu@Sun.COM 	so_basic_strinit(so);	/* skips the T_CAPABILITY_REQ */
64188348SEric.Yu@Sun.COM 	so_alloc_addr(so, so->so_max_addr_len);
64198348SEric.Yu@Sun.COM 
64208348SEric.Yu@Sun.COM 	/*
64218348SEric.Yu@Sun.COM 	 * If the application has done a SIOCSPGRP, make sure the
64228348SEric.Yu@Sun.COM 	 * STREAM head is aware. This needs to take place before
64238348SEric.Yu@Sun.COM 	 * the protocol start sending up messages. Otherwise we
64248348SEric.Yu@Sun.COM 	 * might miss to generate SIGPOLL.
64258348SEric.Yu@Sun.COM 	 *
64268348SEric.Yu@Sun.COM 	 * It is possible that the application will receive duplicate
64278348SEric.Yu@Sun.COM 	 * signals if some were already generated for either data or
64288348SEric.Yu@Sun.COM 	 * connection indications.
64298348SEric.Yu@Sun.COM 	 */
64308348SEric.Yu@Sun.COM 	if (so->so_pgrp != 0) {
64318348SEric.Yu@Sun.COM 		if (so_set_events(so, so->so_vnode, cr) != 0)
64328348SEric.Yu@Sun.COM 			so->so_pgrp = 0;
64338348SEric.Yu@Sun.COM 	}
64348348SEric.Yu@Sun.COM 
64358348SEric.Yu@Sun.COM 	/*
64368348SEric.Yu@Sun.COM 	 * Determine which queue to use.
64378348SEric.Yu@Sun.COM 	 */
64388348SEric.Yu@Sun.COM 	vp = SOTOV(so);
64398348SEric.Yu@Sun.COM 	stp = vp->v_stream;
64408348SEric.Yu@Sun.COM 	ASSERT(stp != NULL);
64418348SEric.Yu@Sun.COM 	q = stp->sd_wrq->q_next;
64428348SEric.Yu@Sun.COM 
64438348SEric.Yu@Sun.COM 	/*
64448348SEric.Yu@Sun.COM 	 * Skip any modules that may have been auto pushed when the device
64458348SEric.Yu@Sun.COM 	 * was opened
64468348SEric.Yu@Sun.COM 	 */
64478348SEric.Yu@Sun.COM 	while (q->q_next != NULL)
64488348SEric.Yu@Sun.COM 		q = q->q_next;
64498963SAnders.Persson@Sun.COM 	*qp = _RD(q);
64508963SAnders.Persson@Sun.COM 
64518963SAnders.Persson@Sun.COM 	/* This is now a STREAMS sockets */
64528963SAnders.Persson@Sun.COM 	so->so_not_str = B_FALSE;
64538963SAnders.Persson@Sun.COM 
64548963SAnders.Persson@Sun.COM 	return (error);
64558963SAnders.Persson@Sun.COM }
64568963SAnders.Persson@Sun.COM 
64578963SAnders.Persson@Sun.COM /*
64588963SAnders.Persson@Sun.COM  * Revert a TPI sonode. It is only allowed to revert the sonode during
64598963SAnders.Persson@Sun.COM  * the fallback process.
64608963SAnders.Persson@Sun.COM  */
64618963SAnders.Persson@Sun.COM void
sotpi_revert_sonode(struct sonode * so,struct cred * cr)64628963SAnders.Persson@Sun.COM sotpi_revert_sonode(struct sonode *so, struct cred *cr)
64638963SAnders.Persson@Sun.COM {
64648963SAnders.Persson@Sun.COM 	vnode_t *vp = SOTOV(so);
64658963SAnders.Persson@Sun.COM 
64668963SAnders.Persson@Sun.COM 	ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
64678963SAnders.Persson@Sun.COM 	    SS_FALLBACK_PENDING);
64688963SAnders.Persson@Sun.COM 	ASSERT(!SOCK_IS_NONSTR(so));
64698963SAnders.Persson@Sun.COM 	ASSERT(vp->v_stream != NULL);
64708963SAnders.Persson@Sun.COM 
64718963SAnders.Persson@Sun.COM 	strclean(vp);
64728963SAnders.Persson@Sun.COM 	(void) strclose(vp, FREAD|FWRITE|SO_FALLBACK, cr);
64738963SAnders.Persson@Sun.COM 
64748963SAnders.Persson@Sun.COM 	/*
64758963SAnders.Persson@Sun.COM 	 * Restore the original sockparams. The caller is responsible for
64768963SAnders.Persson@Sun.COM 	 * dropping the ref to the new sp.
64778963SAnders.Persson@Sun.COM 	 */
64788963SAnders.Persson@Sun.COM 	so->so_sockparams = SOTOTPI(so)->sti_orig_sp;
64798963SAnders.Persson@Sun.COM 
64808963SAnders.Persson@Sun.COM 	sotpi_info_fini(so);
64818963SAnders.Persson@Sun.COM 	sotpi_info_destroy(so);
64828963SAnders.Persson@Sun.COM 
64838963SAnders.Persson@Sun.COM 	/* This is no longer a STREAMS sockets */
64848963SAnders.Persson@Sun.COM 	so->so_not_str = B_TRUE;
64858348SEric.Yu@Sun.COM }
64868348SEric.Yu@Sun.COM 
64878348SEric.Yu@Sun.COM void
sotpi_update_state(struct sonode * so,struct T_capability_ack * tcap,struct sockaddr * laddr,socklen_t laddrlen,struct sockaddr * faddr,socklen_t faddrlen,short opts)64888348SEric.Yu@Sun.COM sotpi_update_state(struct sonode *so, struct T_capability_ack *tcap,
64898348SEric.Yu@Sun.COM     struct sockaddr *laddr, socklen_t laddrlen, struct sockaddr *faddr,
64908348SEric.Yu@Sun.COM     socklen_t faddrlen, short opts)
64918348SEric.Yu@Sun.COM {
64928348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
64938348SEric.Yu@Sun.COM 
64948348SEric.Yu@Sun.COM 	so_proc_tcapability_ack(so, tcap);
64958348SEric.Yu@Sun.COM 
64968348SEric.Yu@Sun.COM 	so->so_options |= opts;
64978348SEric.Yu@Sun.COM 
64988348SEric.Yu@Sun.COM 	/*
64998348SEric.Yu@Sun.COM 	 * Determine whether the foreign and local address are valid
65008348SEric.Yu@Sun.COM 	 */
65018348SEric.Yu@Sun.COM 	if (laddrlen != 0) {
65028348SEric.Yu@Sun.COM 		ASSERT(laddrlen <= sti->sti_laddr_maxlen);
65038348SEric.Yu@Sun.COM 		sti->sti_laddr_len = laddrlen;
65048348SEric.Yu@Sun.COM 		bcopy(laddr, sti->sti_laddr_sa, laddrlen);
65058348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = (so->so_state & SS_ISBOUND);
65068348SEric.Yu@Sun.COM 	}
65078348SEric.Yu@Sun.COM 
65088348SEric.Yu@Sun.COM 	if (faddrlen != 0) {
65098348SEric.Yu@Sun.COM 		ASSERT(faddrlen <= sti->sti_faddr_maxlen);
65108348SEric.Yu@Sun.COM 		sti->sti_faddr_len = faddrlen;
65118348SEric.Yu@Sun.COM 		bcopy(faddr, sti->sti_faddr_sa, faddrlen);
65128348SEric.Yu@Sun.COM 		sti->sti_faddr_valid = (so->so_state & SS_ISCONNECTED);
65138348SEric.Yu@Sun.COM 	}
65148348SEric.Yu@Sun.COM 
65158348SEric.Yu@Sun.COM }
65168348SEric.Yu@Sun.COM 
65178348SEric.Yu@Sun.COM /*
65188348SEric.Yu@Sun.COM  * Allocate enough space to cache the local and foreign addresses.
65198348SEric.Yu@Sun.COM  */
65208348SEric.Yu@Sun.COM void
so_alloc_addr(struct sonode * so,t_uscalar_t maxlen)65218348SEric.Yu@Sun.COM so_alloc_addr(struct sonode *so, t_uscalar_t maxlen)
65228348SEric.Yu@Sun.COM {
65238348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
65248348SEric.Yu@Sun.COM 
65258348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
65268348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_len == 0 && sti->sti_faddr_len == 0);
65278348SEric.Yu@Sun.COM 	sti->sti_laddr_maxlen = sti->sti_faddr_maxlen =
65288348SEric.Yu@Sun.COM 	    P2ROUNDUP(maxlen, KMEM_ALIGN);
65298348SEric.Yu@Sun.COM 	so->so_max_addr_len = sti->sti_laddr_maxlen;
65308348SEric.Yu@Sun.COM 	sti->sti_laddr_sa = kmem_alloc(sti->sti_laddr_maxlen * 2, KM_SLEEP);
65318348SEric.Yu@Sun.COM 	sti->sti_faddr_sa = (struct sockaddr *)((caddr_t)sti->sti_laddr_sa
65328348SEric.Yu@Sun.COM 	    + sti->sti_laddr_maxlen);
65338348SEric.Yu@Sun.COM 
65348348SEric.Yu@Sun.COM 	if (so->so_family == AF_UNIX) {
65358348SEric.Yu@Sun.COM 		/*
65368348SEric.Yu@Sun.COM 		 * Initialize AF_UNIX related fields.
65378348SEric.Yu@Sun.COM 		 */
65388348SEric.Yu@Sun.COM 		bzero(&sti->sti_ux_laddr, sizeof (sti->sti_ux_laddr));
65398348SEric.Yu@Sun.COM 		bzero(&sti->sti_ux_faddr, sizeof (sti->sti_ux_faddr));
65408348SEric.Yu@Sun.COM 	}
65418348SEric.Yu@Sun.COM }
65428348SEric.Yu@Sun.COM 
65438348SEric.Yu@Sun.COM 
65448348SEric.Yu@Sun.COM sotpi_info_t *
sotpi_sototpi(struct sonode * so)65458348SEric.Yu@Sun.COM sotpi_sototpi(struct sonode *so)
65468348SEric.Yu@Sun.COM {
65478348SEric.Yu@Sun.COM 	sotpi_info_t *sti;
65488348SEric.Yu@Sun.COM 
65498963SAnders.Persson@Sun.COM 	ASSERT(so != NULL);
65508348SEric.Yu@Sun.COM 
65518348SEric.Yu@Sun.COM 	sti = (sotpi_info_t *)so->so_priv;
65528348SEric.Yu@Sun.COM 
65538348SEric.Yu@Sun.COM 	ASSERT(sti != NULL);
65548348SEric.Yu@Sun.COM 	ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
65558348SEric.Yu@Sun.COM 
65568348SEric.Yu@Sun.COM 	return (sti);
65578348SEric.Yu@Sun.COM }
65588348SEric.Yu@Sun.COM 
65598348SEric.Yu@Sun.COM static int
i_sotpi_info_constructor(sotpi_info_t * sti)65608348SEric.Yu@Sun.COM i_sotpi_info_constructor(sotpi_info_t *sti)
65618348SEric.Yu@Sun.COM {
65628348SEric.Yu@Sun.COM 	sti->sti_magic		= SOTPI_INFO_MAGIC;
65638348SEric.Yu@Sun.COM 	sti->sti_ack_mp		= NULL;
65648348SEric.Yu@Sun.COM 	sti->sti_discon_ind_mp	= NULL;
65658348SEric.Yu@Sun.COM 	sti->sti_ux_bound_vp	= NULL;
65668348SEric.Yu@Sun.COM 	sti->sti_unbind_mp	= NULL;
65678348SEric.Yu@Sun.COM 
65688348SEric.Yu@Sun.COM 	sti->sti_conn_ind_head	= NULL;
65698348SEric.Yu@Sun.COM 	sti->sti_conn_ind_tail	= NULL;
65708348SEric.Yu@Sun.COM 
65718348SEric.Yu@Sun.COM 	sti->sti_laddr_sa	= NULL;
65728348SEric.Yu@Sun.COM 	sti->sti_faddr_sa	= NULL;
65738348SEric.Yu@Sun.COM 
65748348SEric.Yu@Sun.COM 	sti->sti_nl7c_flags	= 0;
65758348SEric.Yu@Sun.COM 	sti->sti_nl7c_uri	= NULL;
65768348SEric.Yu@Sun.COM 	sti->sti_nl7c_rcv_mp	= NULL;
65778348SEric.Yu@Sun.COM 
65788348SEric.Yu@Sun.COM 	mutex_init(&sti->sti_plumb_lock, NULL, MUTEX_DEFAULT, NULL);
65798348SEric.Yu@Sun.COM 	cv_init(&sti->sti_ack_cv, NULL, CV_DEFAULT, NULL);
65808348SEric.Yu@Sun.COM 
65818348SEric.Yu@Sun.COM 	return (0);
65828348SEric.Yu@Sun.COM }
65838348SEric.Yu@Sun.COM 
65848348SEric.Yu@Sun.COM static void
i_sotpi_info_destructor(sotpi_info_t * sti)65858348SEric.Yu@Sun.COM i_sotpi_info_destructor(sotpi_info_t *sti)
65868348SEric.Yu@Sun.COM {
65878348SEric.Yu@Sun.COM 	ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
65888348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ack_mp == NULL);
65898348SEric.Yu@Sun.COM 	ASSERT(sti->sti_discon_ind_mp == NULL);
65908348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ux_bound_vp == NULL);
65918348SEric.Yu@Sun.COM 	ASSERT(sti->sti_unbind_mp == NULL);
65928348SEric.Yu@Sun.COM 
65938348SEric.Yu@Sun.COM 	ASSERT(sti->sti_conn_ind_head == NULL);
65948348SEric.Yu@Sun.COM 	ASSERT(sti->sti_conn_ind_tail == NULL);
65958348SEric.Yu@Sun.COM 
65968348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_sa == NULL);
65978348SEric.Yu@Sun.COM 	ASSERT(sti->sti_faddr_sa == NULL);
65988348SEric.Yu@Sun.COM 
65998348SEric.Yu@Sun.COM 	ASSERT(sti->sti_nl7c_flags == 0);
66008348SEric.Yu@Sun.COM 	ASSERT(sti->sti_nl7c_uri == NULL);
66018348SEric.Yu@Sun.COM 	ASSERT(sti->sti_nl7c_rcv_mp == NULL);
66028348SEric.Yu@Sun.COM 
66038348SEric.Yu@Sun.COM 	mutex_destroy(&sti->sti_plumb_lock);
66048348SEric.Yu@Sun.COM 	cv_destroy(&sti->sti_ack_cv);
66058348SEric.Yu@Sun.COM }
66068348SEric.Yu@Sun.COM 
66078348SEric.Yu@Sun.COM /*
66088348SEric.Yu@Sun.COM  * Creates and attaches TPI information to the given sonode
66098348SEric.Yu@Sun.COM  */
66108348SEric.Yu@Sun.COM static boolean_t
sotpi_info_create(struct sonode * so,int kmflags)66118348SEric.Yu@Sun.COM sotpi_info_create(struct sonode *so, int kmflags)
66128348SEric.Yu@Sun.COM {
66138348SEric.Yu@Sun.COM 	sotpi_info_t *sti;
66148348SEric.Yu@Sun.COM 
66158348SEric.Yu@Sun.COM 	ASSERT(so->so_priv == NULL);
66168348SEric.Yu@Sun.COM 
66178348SEric.Yu@Sun.COM 	if ((sti = kmem_zalloc(sizeof (*sti), kmflags)) == NULL)
66188348SEric.Yu@Sun.COM 		return (B_FALSE);
66198348SEric.Yu@Sun.COM 
66208348SEric.Yu@Sun.COM 	if (i_sotpi_info_constructor(sti) != 0) {
66218348SEric.Yu@Sun.COM 		kmem_free(sti, sizeof (*sti));
66228348SEric.Yu@Sun.COM 		return (B_FALSE);
66238348SEric.Yu@Sun.COM 	}
66248348SEric.Yu@Sun.COM 
66258348SEric.Yu@Sun.COM 	so->so_priv = (void *)sti;
66268348SEric.Yu@Sun.COM 	return (B_TRUE);
66278348SEric.Yu@Sun.COM }
66288348SEric.Yu@Sun.COM 
66298348SEric.Yu@Sun.COM /*
66308348SEric.Yu@Sun.COM  * Initializes the TPI information.
66318348SEric.Yu@Sun.COM  */
66328348SEric.Yu@Sun.COM static void
sotpi_info_init(struct sonode * so)66338348SEric.Yu@Sun.COM sotpi_info_init(struct sonode *so)
66348348SEric.Yu@Sun.COM {
66358348SEric.Yu@Sun.COM 	struct vnode *vp = SOTOV(so);
66368348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
66378348SEric.Yu@Sun.COM 	time_t now;
66388348SEric.Yu@Sun.COM 
66398348SEric.Yu@Sun.COM 	sti->sti_dev 	= so->so_sockparams->sp_sdev_info.sd_vnode->v_rdev;
66408348SEric.Yu@Sun.COM 	vp->v_rdev	= sti->sti_dev;
66418348SEric.Yu@Sun.COM 
66428348SEric.Yu@Sun.COM 	sti->sti_orig_sp = NULL;
66438348SEric.Yu@Sun.COM 
66448348SEric.Yu@Sun.COM 	sti->sti_pushcnt = 0;
66458348SEric.Yu@Sun.COM 
66468348SEric.Yu@Sun.COM 	now = gethrestime_sec();
66478348SEric.Yu@Sun.COM 	sti->sti_atime	= now;
66488348SEric.Yu@Sun.COM 	sti->sti_mtime	= now;
66498348SEric.Yu@Sun.COM 	sti->sti_ctime	= now;
66508348SEric.Yu@Sun.COM 
66518348SEric.Yu@Sun.COM 	sti->sti_eaddr_mp = NULL;
66528348SEric.Yu@Sun.COM 	sti->sti_delayed_error = 0;
66538348SEric.Yu@Sun.COM 
66548348SEric.Yu@Sun.COM 	sti->sti_provinfo = NULL;
66558348SEric.Yu@Sun.COM 
66568348SEric.Yu@Sun.COM 	sti->sti_oobcnt = 0;
66578348SEric.Yu@Sun.COM 	sti->sti_oobsigcnt = 0;
66588348SEric.Yu@Sun.COM 
66598348SEric.Yu@Sun.COM 	ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
66608348SEric.Yu@Sun.COM 
66618348SEric.Yu@Sun.COM 	sti->sti_laddr_sa	= 0;
66628348SEric.Yu@Sun.COM 	sti->sti_faddr_sa	= 0;
66638348SEric.Yu@Sun.COM 	sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 0;
66648348SEric.Yu@Sun.COM 	sti->sti_laddr_len = sti->sti_faddr_len = 0;
66658348SEric.Yu@Sun.COM 
66668348SEric.Yu@Sun.COM 	sti->sti_laddr_valid = 0;
66678348SEric.Yu@Sun.COM 	sti->sti_faddr_valid = 0;
66688348SEric.Yu@Sun.COM 	sti->sti_faddr_noxlate = 0;
66698348SEric.Yu@Sun.COM 
66708348SEric.Yu@Sun.COM 	sti->sti_direct = 0;
66718348SEric.Yu@Sun.COM 
66728348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ack_mp == NULL);
66738348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ux_bound_vp == NULL);
66748348SEric.Yu@Sun.COM 	ASSERT(sti->sti_unbind_mp == NULL);
66758348SEric.Yu@Sun.COM 
66768348SEric.Yu@Sun.COM 	ASSERT(sti->sti_conn_ind_head == NULL);
66778348SEric.Yu@Sun.COM 	ASSERT(sti->sti_conn_ind_tail == NULL);
66788348SEric.Yu@Sun.COM }
66798348SEric.Yu@Sun.COM 
66808348SEric.Yu@Sun.COM /*
66818348SEric.Yu@Sun.COM  * Given a sonode, grab the TPI info and free any data.
66828348SEric.Yu@Sun.COM  */
66838348SEric.Yu@Sun.COM static void
sotpi_info_fini(struct sonode * so)66848348SEric.Yu@Sun.COM sotpi_info_fini(struct sonode *so)
66858348SEric.Yu@Sun.COM {
66868348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
66878348SEric.Yu@Sun.COM 	mblk_t *mp;
66888348SEric.Yu@Sun.COM 
66898348SEric.Yu@Sun.COM 	ASSERT(sti->sti_discon_ind_mp == NULL);
66908348SEric.Yu@Sun.COM 
66918348SEric.Yu@Sun.COM 	if ((mp = sti->sti_conn_ind_head) != NULL) {
66928348SEric.Yu@Sun.COM 		mblk_t *mp1;
66938348SEric.Yu@Sun.COM 
66948348SEric.Yu@Sun.COM 		while (mp) {
66958348SEric.Yu@Sun.COM 			mp1 = mp->b_next;
66968348SEric.Yu@Sun.COM 			mp->b_next = NULL;
66978348SEric.Yu@Sun.COM 			freemsg(mp);
66988348SEric.Yu@Sun.COM 			mp = mp1;
66998348SEric.Yu@Sun.COM 		}
67008348SEric.Yu@Sun.COM 		sti->sti_conn_ind_head = sti->sti_conn_ind_tail = NULL;
67018348SEric.Yu@Sun.COM 	}
67028348SEric.Yu@Sun.COM 
67038348SEric.Yu@Sun.COM 	/*
67048348SEric.Yu@Sun.COM 	 * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely
67058348SEric.Yu@Sun.COM 	 * indirect them.  It also uses so_count as a validity test.
67068348SEric.Yu@Sun.COM 	 */
67078348SEric.Yu@Sun.COM 	mutex_enter(&so->so_lock);
67088348SEric.Yu@Sun.COM 
67098348SEric.Yu@Sun.COM 	if (sti->sti_laddr_sa) {
67108348SEric.Yu@Sun.COM 		ASSERT((caddr_t)sti->sti_faddr_sa ==
67118348SEric.Yu@Sun.COM 		    (caddr_t)sti->sti_laddr_sa + sti->sti_laddr_maxlen);
67128348SEric.Yu@Sun.COM 		ASSERT(sti->sti_faddr_maxlen == sti->sti_laddr_maxlen);
67138348SEric.Yu@Sun.COM 		sti->sti_laddr_valid = 0;
67148348SEric.Yu@Sun.COM 		sti->sti_faddr_valid = 0;
67158348SEric.Yu@Sun.COM 		kmem_free(sti->sti_laddr_sa, sti->sti_laddr_maxlen * 2);
67168348SEric.Yu@Sun.COM 		sti->sti_laddr_sa = NULL;
67178348SEric.Yu@Sun.COM 		sti->sti_laddr_len = sti->sti_laddr_maxlen = 0;
67188348SEric.Yu@Sun.COM 		sti->sti_faddr_sa = NULL;
67198348SEric.Yu@Sun.COM 		sti->sti_faddr_len = sti->sti_faddr_maxlen = 0;
67208348SEric.Yu@Sun.COM 	}
67218348SEric.Yu@Sun.COM 
67228348SEric.Yu@Sun.COM 	mutex_exit(&so->so_lock);
67238348SEric.Yu@Sun.COM 
67248348SEric.Yu@Sun.COM 	if ((mp = sti->sti_eaddr_mp) != NULL) {
67258348SEric.Yu@Sun.COM 		freemsg(mp);
67268348SEric.Yu@Sun.COM 		sti->sti_eaddr_mp = NULL;
67278348SEric.Yu@Sun.COM 		sti->sti_delayed_error = 0;
67288348SEric.Yu@Sun.COM 	}
67298348SEric.Yu@Sun.COM 
67308348SEric.Yu@Sun.COM 	if ((mp = sti->sti_ack_mp) != NULL) {
67318348SEric.Yu@Sun.COM 		freemsg(mp);
67328348SEric.Yu@Sun.COM 		sti->sti_ack_mp = NULL;
67338348SEric.Yu@Sun.COM 	}
67348348SEric.Yu@Sun.COM 
67358348SEric.Yu@Sun.COM 	if ((mp = sti->sti_nl7c_rcv_mp) != NULL) {
67368348SEric.Yu@Sun.COM 		sti->sti_nl7c_rcv_mp = NULL;
67378348SEric.Yu@Sun.COM 		freemsg(mp);
67388348SEric.Yu@Sun.COM 	}
67398348SEric.Yu@Sun.COM 	sti->sti_nl7c_rcv_rval = 0;
67408348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_uri != NULL) {
67418348SEric.Yu@Sun.COM 		nl7c_urifree(so);
67428348SEric.Yu@Sun.COM 		/* urifree() cleared nl7c_uri */
67438348SEric.Yu@Sun.COM 	}
67448348SEric.Yu@Sun.COM 	if (sti->sti_nl7c_flags) {
67458348SEric.Yu@Sun.COM 		sti->sti_nl7c_flags = 0;
67468348SEric.Yu@Sun.COM 	}
67478348SEric.Yu@Sun.COM 
67488348SEric.Yu@Sun.COM 	ASSERT(sti->sti_ux_bound_vp == NULL);
67498348SEric.Yu@Sun.COM 	if ((mp = sti->sti_unbind_mp) != NULL) {
67508348SEric.Yu@Sun.COM 		freemsg(mp);
67518348SEric.Yu@Sun.COM 		sti->sti_unbind_mp = NULL;
67528348SEric.Yu@Sun.COM 	}
67538348SEric.Yu@Sun.COM }
67548348SEric.Yu@Sun.COM 
67558348SEric.Yu@Sun.COM /*
67568348SEric.Yu@Sun.COM  * Destroys the TPI information attached to a sonode.
67578348SEric.Yu@Sun.COM  */
67588348SEric.Yu@Sun.COM static void
sotpi_info_destroy(struct sonode * so)67598348SEric.Yu@Sun.COM sotpi_info_destroy(struct sonode *so)
67608348SEric.Yu@Sun.COM {
67618348SEric.Yu@Sun.COM 	sotpi_info_t *sti = SOTOTPI(so);
67628348SEric.Yu@Sun.COM 
67638348SEric.Yu@Sun.COM 	i_sotpi_info_destructor(sti);
67648348SEric.Yu@Sun.COM 	kmem_free(sti, sizeof (*sti));
67658348SEric.Yu@Sun.COM 
67668348SEric.Yu@Sun.COM 	so->so_priv = NULL;
67678348SEric.Yu@Sun.COM }
67688348SEric.Yu@Sun.COM 
67698348SEric.Yu@Sun.COM /*
67708429SEric.Yu@Sun.COM  * Create the global sotpi socket module entry. It will never be freed.
67718348SEric.Yu@Sun.COM  */
67728348SEric.Yu@Sun.COM smod_info_t *
sotpi_smod_create(void)67738348SEric.Yu@Sun.COM sotpi_smod_create(void)
67748348SEric.Yu@Sun.COM {
67758348SEric.Yu@Sun.COM 	smod_info_t *smodp;
67768348SEric.Yu@Sun.COM 
67778348SEric.Yu@Sun.COM 	smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP);
67788429SEric.Yu@Sun.COM 	smodp->smod_name = kmem_alloc(sizeof (SOTPI_SMOD_NAME), KM_SLEEP);
67798429SEric.Yu@Sun.COM 	(void) strcpy(smodp->smod_name, SOTPI_SMOD_NAME);
67808348SEric.Yu@Sun.COM 	/*
67818429SEric.Yu@Sun.COM 	 * Initialize the smod_refcnt to 1 so it will never be freed.
67828348SEric.Yu@Sun.COM 	 */
67838348SEric.Yu@Sun.COM 	smodp->smod_refcnt = 1;
67848348SEric.Yu@Sun.COM 	smodp->smod_uc_version = SOCK_UC_VERSION;
67858348SEric.Yu@Sun.COM 	smodp->smod_dc_version = SOCK_DC_VERSION;
67868348SEric.Yu@Sun.COM 	smodp->smod_sock_create_func = &sotpi_create;
67878348SEric.Yu@Sun.COM 	smodp->smod_sock_destroy_func = &sotpi_destroy;
67888348SEric.Yu@Sun.COM 	return (smodp);
67898348SEric.Yu@Sun.COM }
6790