xref: /titanic_51/usr/src/uts/common/fs/sockfs/socktpi.c (revision 78918900575943abab7e1aa2cfcd0bcaaa1b5c29)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52caf0dcdSrshoaib  * Common Development and Distribution License (the "License").
62caf0dcdSrshoaib  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212caf0dcdSrshoaib 
227c478bd9Sstevel@tonic-gate /*
233e95bd4aSAnders Persson  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24dfc0fed8SRobert Mustacchi  * Copyright (c) 2013, Joyent, Inc.  All rights reserved.
25412cc9e9SGordon Ross  * Copyright 2014 Nexenta Systems, Inc.  All rights reserved.
26dfc0fed8SRobert Mustacchi  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate #include <sys/types.h>
297c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
307c478bd9Sstevel@tonic-gate #include <sys/param.h>
317c478bd9Sstevel@tonic-gate #include <sys/systm.h>
327c478bd9Sstevel@tonic-gate #include <sys/buf.h>
337c478bd9Sstevel@tonic-gate #include <sys/conf.h>
347c478bd9Sstevel@tonic-gate #include <sys/cred.h>
357c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
360f1702c5SYu Xiangning #include <sys/kmem_impl.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
397c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/errno.h>
427c478bd9Sstevel@tonic-gate #include <sys/time.h>
437c478bd9Sstevel@tonic-gate #include <sys/file.h>
447c478bd9Sstevel@tonic-gate #include <sys/open.h>
457c478bd9Sstevel@tonic-gate #include <sys/user.h>
467c478bd9Sstevel@tonic-gate #include <sys/termios.h>
477c478bd9Sstevel@tonic-gate #include <sys/stream.h>
487c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
500f1702c5SYu Xiangning #include <sys/suntpi.h>
517c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
527c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/flock.h>
547c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
557c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
567c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
577c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate #include <sys/socket.h>
607c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
61ff550d0eSmasputra #include <sys/sockio.h>
627c478bd9Sstevel@tonic-gate #include <netinet/in.h>
637c478bd9Sstevel@tonic-gate #include <sys/un.h>
647c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
677c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
687c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
697c478bd9Sstevel@tonic-gate #include <sys/timod.h>		/* TI_GETMYNAME, TI_GETPEERNAME */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #include <c2/audit.h>
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #include <inet/common.h>
747c478bd9Sstevel@tonic-gate #include <inet/ip.h>
757c478bd9Sstevel@tonic-gate #include <inet/ip6.h>
767c478bd9Sstevel@tonic-gate #include <inet/tcp.h>
77ff550d0eSmasputra #include <inet/udp_impl.h>
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #include <sys/zone.h>
807c478bd9Sstevel@tonic-gate 
812c9e429eSbrutus #include <fs/sockfs/nl7c.h>
822c9e429eSbrutus #include <fs/sockfs/nl7curi.h>
832c9e429eSbrutus 
840f1702c5SYu Xiangning #include <fs/sockfs/sockcommon.h>
850f1702c5SYu Xiangning #include <fs/sockfs/socktpi.h>
860f1702c5SYu Xiangning #include <fs/sockfs/socktpi_impl.h>
870f1702c5SYu Xiangning 
887c478bd9Sstevel@tonic-gate /*
897c478bd9Sstevel@tonic-gate  * Possible failures when memory can't be allocated. The documented behavior:
907c478bd9Sstevel@tonic-gate  *
917c478bd9Sstevel@tonic-gate  * 		5.5:			4.X:		XNET:
927c478bd9Sstevel@tonic-gate  * accept:	ENOMEM/ENOSR/EINTR	- (EINTR)	ENOMEM/ENOBUFS/ENOSR/
937c478bd9Sstevel@tonic-gate  *							EINTR
947c478bd9Sstevel@tonic-gate  *	(4.X does not document EINTR but returns it)
957c478bd9Sstevel@tonic-gate  * bind:	ENOSR			-		ENOBUFS/ENOSR
967c478bd9Sstevel@tonic-gate  * connect: 	EINTR			EINTR		ENOBUFS/ENOSR/EINTR
977c478bd9Sstevel@tonic-gate  * getpeername:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
987c478bd9Sstevel@tonic-gate  * getsockname:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
997c478bd9Sstevel@tonic-gate  *	(4.X getpeername and getsockname do not fail in practice)
1007c478bd9Sstevel@tonic-gate  * getsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
1017c478bd9Sstevel@tonic-gate  * listen:	-			-		ENOBUFS
1027c478bd9Sstevel@tonic-gate  * recv:	ENOMEM/ENOSR/EINTR	EINTR		ENOBUFS/ENOMEM/ENOSR/
1037c478bd9Sstevel@tonic-gate  *							EINTR
1047c478bd9Sstevel@tonic-gate  * send:	ENOMEM/ENOSR/EINTR	ENOBUFS/EINTR	ENOBUFS/ENOMEM/ENOSR/
1057c478bd9Sstevel@tonic-gate  *							EINTR
1067c478bd9Sstevel@tonic-gate  * setsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
1077c478bd9Sstevel@tonic-gate  * shutdown:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
1087c478bd9Sstevel@tonic-gate  * socket:	ENOMEM/ENOSR		ENOBUFS		ENOBUFS/ENOMEM/ENOSR
1097c478bd9Sstevel@tonic-gate  * socketpair:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
1107c478bd9Sstevel@tonic-gate  *
1117c478bd9Sstevel@tonic-gate  * Resolution. When allocation fails:
1127c478bd9Sstevel@tonic-gate  *	recv: return EINTR
1137c478bd9Sstevel@tonic-gate  *	send: return EINTR
1147c478bd9Sstevel@tonic-gate  *	connect, accept: EINTR
1157c478bd9Sstevel@tonic-gate  *	bind, listen, shutdown (unbind, unix_close, disconnect): sleep
1167c478bd9Sstevel@tonic-gate  *	socket, socketpair: ENOBUFS
1177c478bd9Sstevel@tonic-gate  *	getpeername, getsockname: sleep
1187c478bd9Sstevel@tonic-gate  *	getsockopt, setsockopt: sleep
1197c478bd9Sstevel@tonic-gate  */
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Variables that make sockfs do something other than the standard TPI
1247c478bd9Sstevel@tonic-gate  * for the AF_INET transports.
1257c478bd9Sstevel@tonic-gate  *
1267c478bd9Sstevel@tonic-gate  * solisten_tpi_tcp:
1277c478bd9Sstevel@tonic-gate  *	TCP can handle a O_T_BIND_REQ with an increased backlog even though
1287c478bd9Sstevel@tonic-gate  *	the transport is already bound. This is needed to avoid loosing the
1297c478bd9Sstevel@tonic-gate  *	port number should listen() do a T_UNBIND_REQ followed by a
1307c478bd9Sstevel@tonic-gate  *	O_T_BIND_REQ.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * soconnect_tpi_udp:
1337c478bd9Sstevel@tonic-gate  *	UDP and ICMP can handle a T_CONN_REQ.
1347c478bd9Sstevel@tonic-gate  *	This is needed to make the sequence of connect(), getsockname()
1357c478bd9Sstevel@tonic-gate  *	return the local IP address used to send packets to the connected to
1367c478bd9Sstevel@tonic-gate  *	destination.
1377c478bd9Sstevel@tonic-gate  *
1387c478bd9Sstevel@tonic-gate  * soconnect_tpi_tcp:
1397c478bd9Sstevel@tonic-gate  *	TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ.
1407c478bd9Sstevel@tonic-gate  *	Set this to non-zero to send TPI conformant messages to TCP in this
1417c478bd9Sstevel@tonic-gate  *	respect. This is a performance optimization.
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * soaccept_tpi_tcp:
1447c478bd9Sstevel@tonic-gate  *	TCP can handle a T_CONN_REQ without the acceptor being bound.
1457c478bd9Sstevel@tonic-gate  *	This is a performance optimization that has been picked up in XTI.
1467c478bd9Sstevel@tonic-gate  *
1477c478bd9Sstevel@tonic-gate  * soaccept_tpi_multioptions:
1487c478bd9Sstevel@tonic-gate  *	When inheriting SOL_SOCKET options from the listener to the accepting
1497c478bd9Sstevel@tonic-gate  *	socket send them as a single message for AF_INET{,6}.
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate int solisten_tpi_tcp = 0;
1527c478bd9Sstevel@tonic-gate int soconnect_tpi_udp = 0;
1537c478bd9Sstevel@tonic-gate int soconnect_tpi_tcp = 0;
1547c478bd9Sstevel@tonic-gate int soaccept_tpi_tcp = 0;
1557c478bd9Sstevel@tonic-gate int soaccept_tpi_multioptions = 1;
1567c478bd9Sstevel@tonic-gate #else /* SOCK_TEST */
1577c478bd9Sstevel@tonic-gate #define	soconnect_tpi_tcp	0
1587c478bd9Sstevel@tonic-gate #define	soconnect_tpi_udp	0
1597c478bd9Sstevel@tonic-gate #define	solisten_tpi_tcp	0
1607c478bd9Sstevel@tonic-gate #define	soaccept_tpi_tcp	0
1617c478bd9Sstevel@tonic-gate #define	soaccept_tpi_multioptions	1
1627c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
1657c478bd9Sstevel@tonic-gate extern int do_useracc;
1667c478bd9Sstevel@tonic-gate extern clock_t sock_test_timelimit;
1677c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate /*
1707c478bd9Sstevel@tonic-gate  * Some X/Open added checks might have to be backed out to keep SunOS 4.X
1717c478bd9Sstevel@tonic-gate  * applications working. Turn on this flag to disable these checks.
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate int xnet_skip_checks = 0;
1747c478bd9Sstevel@tonic-gate int xnet_check_print = 0;
1757c478bd9Sstevel@tonic-gate int xnet_truncate_print = 0;
1767c478bd9Sstevel@tonic-gate 
1770f1702c5SYu Xiangning static void sotpi_destroy(struct sonode *);
1780f1702c5SYu Xiangning static struct sonode *sotpi_create(struct sockparams *, int, int, int, int,
1790f1702c5SYu Xiangning     int, int *, cred_t *cr);
1800f1702c5SYu Xiangning 
1810f1702c5SYu Xiangning static boolean_t	sotpi_info_create(struct sonode *, int);
1820f1702c5SYu Xiangning static void		sotpi_info_init(struct sonode *);
1830f1702c5SYu Xiangning static void 		sotpi_info_fini(struct sonode *);
1840f1702c5SYu Xiangning static void 		sotpi_info_destroy(struct sonode *);
1850f1702c5SYu Xiangning 
1860f1702c5SYu Xiangning /*
1870f1702c5SYu Xiangning  * Do direct function call to the transport layer below; this would
1880f1702c5SYu Xiangning  * also allow the transport to utilize read-side synchronous stream
1890f1702c5SYu Xiangning  * interface if necessary.  This is a /etc/system tunable that must
1900f1702c5SYu Xiangning  * not be modified on a running system.  By default this is enabled
1910f1702c5SYu Xiangning  * for performance reasons and may be disabled for debugging purposes.
1920f1702c5SYu Xiangning  */
1930f1702c5SYu Xiangning boolean_t socktpi_direct = B_TRUE;
1940f1702c5SYu Xiangning 
1950f1702c5SYu Xiangning static struct kmem_cache *socktpi_cache, *socktpi_unix_cache;
1960f1702c5SYu Xiangning 
1977c478bd9Sstevel@tonic-gate extern	void sigintr(k_sigset_t *, int);
1987c478bd9Sstevel@tonic-gate extern	void sigunintr(k_sigset_t *);
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate static int	sotpi_unbind(struct sonode *, int);
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate /* TPI sockfs sonode operations */
2030f1702c5SYu Xiangning int 		sotpi_init(struct sonode *, struct sonode *, struct cred *,
2047c478bd9Sstevel@tonic-gate 		    int);
2050f1702c5SYu Xiangning static int	sotpi_accept(struct sonode *, int, struct cred *,
2060f1702c5SYu Xiangning 		    struct sonode **);
2070f1702c5SYu Xiangning static int	sotpi_bind(struct sonode *, struct sockaddr *, socklen_t,
2080f1702c5SYu Xiangning 		    int, struct cred *);
2090f1702c5SYu Xiangning static int	sotpi_listen(struct sonode *, int, struct cred *);
2103e95bd4aSAnders Persson static int	sotpi_connect(struct sonode *, struct sockaddr *,
2110f1702c5SYu Xiangning 		    socklen_t, int, int, struct cred *);
2120f1702c5SYu Xiangning extern int	sotpi_recvmsg(struct sonode *, struct nmsghdr *,
2130f1702c5SYu Xiangning 		    struct uio *, struct cred *);
2147c478bd9Sstevel@tonic-gate static int	sotpi_sendmsg(struct sonode *, struct nmsghdr *,
2150f1702c5SYu Xiangning 		    struct uio *, struct cred *);
2160f1702c5SYu Xiangning static int	sotpi_sendmblk(struct sonode *, struct nmsghdr *, int,
2170f1702c5SYu Xiangning 		    struct cred *, mblk_t **);
218ff550d0eSmasputra static int	sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t,
219ff550d0eSmasputra 		    struct uio *, void *, t_uscalar_t, int);
220ff550d0eSmasputra static int	sodgram_direct(struct sonode *, struct sockaddr *,
221ff550d0eSmasputra 		    socklen_t, struct uio *, int);
2220f1702c5SYu Xiangning extern int	sotpi_getpeername(struct sonode *, struct sockaddr *,
2230f1702c5SYu Xiangning 		    socklen_t *, boolean_t, struct cred *);
2240f1702c5SYu Xiangning static int	sotpi_getsockname(struct sonode *, struct sockaddr *,
2250f1702c5SYu Xiangning 		    socklen_t *, struct cred *);
2260f1702c5SYu Xiangning static int	sotpi_shutdown(struct sonode *, int, struct cred *);
2270f1702c5SYu Xiangning extern int	sotpi_getsockopt(struct sonode *, int, int, void *,
2280f1702c5SYu Xiangning 		    socklen_t *, int, struct cred *);
2290f1702c5SYu Xiangning extern int	sotpi_setsockopt(struct sonode *, int, int, const void *,
2300f1702c5SYu Xiangning 		    socklen_t, struct cred *);
2310f1702c5SYu Xiangning static int 	sotpi_ioctl(struct sonode *, int, intptr_t, int, struct cred *,
2320f1702c5SYu Xiangning 		    int32_t *);
2330f1702c5SYu Xiangning static int 	socktpi_plumbioctl(struct vnode *, int, intptr_t, int,
2340f1702c5SYu Xiangning 		    struct cred *, int32_t *);
2350f1702c5SYu Xiangning static int 	sotpi_poll(struct sonode *, short, int, short *,
2360f1702c5SYu Xiangning 		    struct pollhead **);
2370f1702c5SYu Xiangning static int 	sotpi_close(struct sonode *, int, struct cred *);
2380f1702c5SYu Xiangning 
2390f1702c5SYu Xiangning static int	i_sotpi_info_constructor(sotpi_info_t *);
2400f1702c5SYu Xiangning static void 	i_sotpi_info_destructor(sotpi_info_t *);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate sonodeops_t sotpi_sonodeops = {
2430f1702c5SYu Xiangning 	sotpi_init,		/* sop_init		*/
2447c478bd9Sstevel@tonic-gate 	sotpi_accept,		/* sop_accept		*/
2457c478bd9Sstevel@tonic-gate 	sotpi_bind,		/* sop_bind		*/
2467c478bd9Sstevel@tonic-gate 	sotpi_listen,		/* sop_listen		*/
2477c478bd9Sstevel@tonic-gate 	sotpi_connect,		/* sop_connect		*/
2487c478bd9Sstevel@tonic-gate 	sotpi_recvmsg,		/* sop_recvmsg		*/
2497c478bd9Sstevel@tonic-gate 	sotpi_sendmsg,		/* sop_sendmsg		*/
2500f1702c5SYu Xiangning 	sotpi_sendmblk,		/* sop_sendmblk		*/
2517c478bd9Sstevel@tonic-gate 	sotpi_getpeername,	/* sop_getpeername	*/
2527c478bd9Sstevel@tonic-gate 	sotpi_getsockname,	/* sop_getsockname	*/
2537c478bd9Sstevel@tonic-gate 	sotpi_shutdown,		/* sop_shutdown		*/
2547c478bd9Sstevel@tonic-gate 	sotpi_getsockopt,	/* sop_getsockopt	*/
2550f1702c5SYu Xiangning 	sotpi_setsockopt,	/* sop_setsockopt	*/
2560f1702c5SYu Xiangning 	sotpi_ioctl,		/* sop_ioctl		*/
2570f1702c5SYu Xiangning 	sotpi_poll,		/* sop_poll		*/
2580f1702c5SYu Xiangning 	sotpi_close,		/* sop_close		*/
2597c478bd9Sstevel@tonic-gate };
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate /*
2620f1702c5SYu Xiangning  * Return a TPI socket vnode.
2630f1702c5SYu Xiangning  *
2640f1702c5SYu Xiangning  * Note that sockets assume that the driver will clone (either itself
2650f1702c5SYu Xiangning  * or by using the clone driver) i.e. a socket() call will always
2660f1702c5SYu Xiangning  * result in a new vnode being created.
2670f1702c5SYu Xiangning  */
2680f1702c5SYu Xiangning 
2690f1702c5SYu Xiangning /*
2707c478bd9Sstevel@tonic-gate  * Common create code for socket and accept. If tso is set the values
2717c478bd9Sstevel@tonic-gate  * from that node is used instead of issuing a T_INFO_REQ.
2727c478bd9Sstevel@tonic-gate  */
2730f1702c5SYu Xiangning 
2740f1702c5SYu Xiangning /* ARGSUSED */
2750f1702c5SYu Xiangning static struct sonode *
2760f1702c5SYu Xiangning sotpi_create(struct sockparams *sp, int family, int type, int protocol,
2770f1702c5SYu Xiangning     int version, int sflags, int *errorp, cred_t *cr)
2787c478bd9Sstevel@tonic-gate {
2797c478bd9Sstevel@tonic-gate 	struct sonode	*so;
2800f1702c5SYu Xiangning 	kmem_cache_t 	*cp;
2810f1702c5SYu Xiangning 	int		sfamily = family;
2827c478bd9Sstevel@tonic-gate 
2830f1702c5SYu Xiangning 	ASSERT(sp->sp_sdev_info.sd_vnode != NULL);
2847c478bd9Sstevel@tonic-gate 
2850f1702c5SYu Xiangning 	if (family == AF_NCA) {
2860f1702c5SYu Xiangning 		/*
2870f1702c5SYu Xiangning 		 * The request is for an NCA socket so for NL7C use the
2880f1702c5SYu Xiangning 		 * INET domain instead and mark NL7C_AF_NCA below.
2890f1702c5SYu Xiangning 		 */
2900f1702c5SYu Xiangning 		family = AF_INET;
2910f1702c5SYu Xiangning 		/*
2920f1702c5SYu Xiangning 		 * NL7C is not supported in the non-global zone,
2930f1702c5SYu Xiangning 		 * we enforce this restriction here.
2940f1702c5SYu Xiangning 		 */
2950f1702c5SYu Xiangning 		if (getzoneid() != GLOBAL_ZONEID) {
2960f1702c5SYu Xiangning 			*errorp = ENOTSUP;
2970f1702c5SYu Xiangning 			return (NULL);
2980f1702c5SYu Xiangning 		}
2990f1702c5SYu Xiangning 	}
300ff550d0eSmasputra 
3010f1702c5SYu Xiangning 	/*
3020f1702c5SYu Xiangning 	 * to be compatible with old tpi socket implementation ignore
3030f1702c5SYu Xiangning 	 * sleep flag (sflags) passed in
3040f1702c5SYu Xiangning 	 */
3050f1702c5SYu Xiangning 	cp = (family == AF_UNIX) ? socktpi_unix_cache : socktpi_cache;
3060f1702c5SYu Xiangning 	so = kmem_cache_alloc(cp, KM_SLEEP);
3070f1702c5SYu Xiangning 	if (so == NULL) {
3080f1702c5SYu Xiangning 		*errorp = ENOMEM;
3090f1702c5SYu Xiangning 		return (NULL);
3100f1702c5SYu Xiangning 	}
3110f1702c5SYu Xiangning 
3120f1702c5SYu Xiangning 	sonode_init(so, sp, family, type, protocol, &sotpi_sonodeops);
3130f1702c5SYu Xiangning 	sotpi_info_init(so);
3140f1702c5SYu Xiangning 
3150f1702c5SYu Xiangning 	if (sfamily == AF_NCA) {
3160f1702c5SYu Xiangning 		SOTOTPI(so)->sti_nl7c_flags = NL7C_AF_NCA;
3170f1702c5SYu Xiangning 	}
3180f1702c5SYu Xiangning 
3190f1702c5SYu Xiangning 	if (version == SOV_DEFAULT)
3200f1702c5SYu Xiangning 		version = so_default_version;
3210f1702c5SYu Xiangning 
3220f1702c5SYu Xiangning 	so->so_version = (short)version;
3230f1702c5SYu Xiangning 	*errorp = 0;
3240f1702c5SYu Xiangning 
3250f1702c5SYu Xiangning 	return (so);
3260f1702c5SYu Xiangning }
3270f1702c5SYu Xiangning 
3280f1702c5SYu Xiangning static void
3290f1702c5SYu Xiangning sotpi_destroy(struct sonode *so)
3300f1702c5SYu Xiangning {
3310f1702c5SYu Xiangning 	kmem_cache_t *cp;
3320f1702c5SYu Xiangning 	struct sockparams *origsp;
3330f1702c5SYu Xiangning 
3340f1702c5SYu Xiangning 	/*
3350f1702c5SYu Xiangning 	 * If there is a new dealloc function (ie. smod_destroy_func),
3360f1702c5SYu Xiangning 	 * then it should check the correctness of the ops.
3370f1702c5SYu Xiangning 	 */
3380f1702c5SYu Xiangning 
3390f1702c5SYu Xiangning 	ASSERT(so->so_ops == &sotpi_sonodeops);
3400f1702c5SYu Xiangning 
3410f1702c5SYu Xiangning 	origsp = SOTOTPI(so)->sti_orig_sp;
3420f1702c5SYu Xiangning 
3430f1702c5SYu Xiangning 	sotpi_info_fini(so);
3440f1702c5SYu Xiangning 
3450f1702c5SYu Xiangning 	if (so->so_state & SS_FALLBACK_COMP) {
3460f1702c5SYu Xiangning 		/*
3470f1702c5SYu Xiangning 		 * A fallback happend, which means that a sotpi_info_t struct
3480f1702c5SYu Xiangning 		 * was allocated (as opposed to being allocated from the TPI
3490f1702c5SYu Xiangning 		 * sonode cache. Therefore we explicitly free the struct
3500f1702c5SYu Xiangning 		 * here.
3510f1702c5SYu Xiangning 		 */
3520f1702c5SYu Xiangning 		sotpi_info_destroy(so);
3530f1702c5SYu Xiangning 		ASSERT(origsp != NULL);
3540f1702c5SYu Xiangning 
3550f1702c5SYu Xiangning 		origsp->sp_smod_info->smod_sock_destroy_func(so);
3560f1702c5SYu Xiangning 		SOCKPARAMS_DEC_REF(origsp);
3570f1702c5SYu Xiangning 	} else {
3580f1702c5SYu Xiangning 		sonode_fini(so);
3590f1702c5SYu Xiangning 		cp = (so->so_family == AF_UNIX) ? socktpi_unix_cache :
3600f1702c5SYu Xiangning 		    socktpi_cache;
3610f1702c5SYu Xiangning 		kmem_cache_free(cp, so);
3620f1702c5SYu Xiangning 	}
3630f1702c5SYu Xiangning }
3640f1702c5SYu Xiangning 
3650f1702c5SYu Xiangning /* ARGSUSED1 */
3660f1702c5SYu Xiangning int
3670f1702c5SYu Xiangning sotpi_init(struct sonode *so, struct sonode *tso, struct cred *cr, int flags)
3680f1702c5SYu Xiangning {
3690f1702c5SYu Xiangning 	major_t maj;
3700f1702c5SYu Xiangning 	dev_t newdev;
3710f1702c5SYu Xiangning 	struct vnode *vp;
3720f1702c5SYu Xiangning 	int error = 0;
3730f1702c5SYu Xiangning 	struct stdata *stp;
3740f1702c5SYu Xiangning 
3750f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
3760f1702c5SYu Xiangning 
3770f1702c5SYu Xiangning 	dprint(1, ("sotpi_init()\n"));
3780f1702c5SYu Xiangning 
3790f1702c5SYu Xiangning 	/*
3800f1702c5SYu Xiangning 	 * over write the sleep flag passed in but that is ok
3810f1702c5SYu Xiangning 	 * as tpi socket does not honor sleep flag.
3820f1702c5SYu Xiangning 	 */
3830f1702c5SYu Xiangning 	flags |= FREAD|FWRITE;
3840f1702c5SYu Xiangning 
3850f1702c5SYu Xiangning 	/*
3860f1702c5SYu Xiangning 	 * Record in so_flag that it is a clone.
3870f1702c5SYu Xiangning 	 */
3880f1702c5SYu Xiangning 	if (getmajor(sti->sti_dev) == clone_major)
3890f1702c5SYu Xiangning 		so->so_flag |= SOCLONE;
3900f1702c5SYu Xiangning 
3910f1702c5SYu Xiangning 	if ((so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM) &&
3920f1702c5SYu Xiangning 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
3930f1702c5SYu Xiangning 	    (so->so_protocol == IPPROTO_TCP || so->so_protocol == IPPROTO_UDP ||
3940f1702c5SYu Xiangning 	    so->so_protocol == IPPROTO_IP)) {
395ff550d0eSmasputra 		/* Tell tcp or udp that it's talking to sockets */
3967c478bd9Sstevel@tonic-gate 		flags |= SO_SOCKSTR;
397ff550d0eSmasputra 
398ff550d0eSmasputra 		/*
399ff550d0eSmasputra 		 * Here we indicate to socktpi_open() our attempt to
400ff550d0eSmasputra 		 * make direct calls between sockfs and transport.
401ff550d0eSmasputra 		 * The final decision is left to socktpi_open().
402ff550d0eSmasputra 		 */
4030f1702c5SYu Xiangning 		sti->sti_direct = 1;
404ff550d0eSmasputra 
405ff550d0eSmasputra 		ASSERT(so->so_type != SOCK_DGRAM || tso == NULL);
406ff550d0eSmasputra 		if (so->so_type == SOCK_STREAM && tso != NULL) {
4070f1702c5SYu Xiangning 			if (SOTOTPI(tso)->sti_direct) {
408ff550d0eSmasputra 				/*
4090f1702c5SYu Xiangning 				 * Inherit sti_direct from listener and pass
410ff550d0eSmasputra 				 * SO_ACCEPTOR open flag to tcp, indicating
411ff550d0eSmasputra 				 * that this is an accept fast-path instance.
412ff550d0eSmasputra 				 */
413ff550d0eSmasputra 				flags |= SO_ACCEPTOR;
414ff550d0eSmasputra 			} else {
415ff550d0eSmasputra 				/*
4160f1702c5SYu Xiangning 				 * sti_direct is not set on listener, meaning
417ff550d0eSmasputra 				 * that the listener has been converted from
418ff550d0eSmasputra 				 * a socket to a stream.  Ensure that the
419ff550d0eSmasputra 				 * acceptor inherits these settings.
420ff550d0eSmasputra 				 */
4210f1702c5SYu Xiangning 				sti->sti_direct = 0;
422ff550d0eSmasputra 				flags &= ~SO_SOCKSTR;
423ff550d0eSmasputra 			}
4247c478bd9Sstevel@tonic-gate 		}
4257c478bd9Sstevel@tonic-gate 	}
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	/*
4287c478bd9Sstevel@tonic-gate 	 * Tell local transport that it is talking to sockets.
4297c478bd9Sstevel@tonic-gate 	 */
4307c478bd9Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
4317c478bd9Sstevel@tonic-gate 		flags |= SO_SOCKSTR;
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 
4340f1702c5SYu Xiangning 	vp = SOTOV(so);
4350f1702c5SYu Xiangning 	newdev = vp->v_rdev;
4360f1702c5SYu Xiangning 	maj = getmajor(newdev);
4370f1702c5SYu Xiangning 	ASSERT(STREAMSTAB(maj));
438655a2e99Skais 
4390f1702c5SYu Xiangning 	error = stropen(vp, &newdev, flags, cr);
4400f1702c5SYu Xiangning 
4410f1702c5SYu Xiangning 	stp = vp->v_stream;
4420f1702c5SYu Xiangning 	if (error == 0) {
4430f1702c5SYu Xiangning 		if (so->so_flag & SOCLONE)
4440f1702c5SYu Xiangning 			ASSERT(newdev != vp->v_rdev);
4450f1702c5SYu Xiangning 		mutex_enter(&so->so_lock);
4460f1702c5SYu Xiangning 		sti->sti_dev = newdev;
4470f1702c5SYu Xiangning 		vp->v_rdev = newdev;
4480f1702c5SYu Xiangning 		mutex_exit(&so->so_lock);
4490f1702c5SYu Xiangning 
4500f1702c5SYu Xiangning 		if (stp->sd_flag & STRISTTY) {
4510f1702c5SYu Xiangning 			/*
4520f1702c5SYu Xiangning 			 * this is a post SVR4 tty driver - a socket can not
4530f1702c5SYu Xiangning 			 * be a controlling terminal. Fail the open.
4540f1702c5SYu Xiangning 			 */
4550f1702c5SYu Xiangning 			(void) sotpi_close(so, flags, cr);
4560f1702c5SYu Xiangning 			return (ENOTTY);	/* XXX */
4577c478bd9Sstevel@tonic-gate 		}
4587c478bd9Sstevel@tonic-gate 
4590f1702c5SYu Xiangning 		ASSERT(stp->sd_wrq != NULL);
4600f1702c5SYu Xiangning 		sti->sti_provinfo = tpi_findprov(stp->sd_wrq);
4610f1702c5SYu Xiangning 
4620f1702c5SYu Xiangning 		/*
4630f1702c5SYu Xiangning 		 * If caller is interested in doing direct function call
4640f1702c5SYu Xiangning 		 * interface to/from transport module, probe the module
4650f1702c5SYu Xiangning 		 * directly beneath the streamhead to see if it qualifies.
4660f1702c5SYu Xiangning 		 *
4670f1702c5SYu Xiangning 		 * We turn off the direct interface when qualifications fail.
4680f1702c5SYu Xiangning 		 * In the acceptor case, we simply turn off the sti_direct
4690f1702c5SYu Xiangning 		 * flag on the socket. We do the fallback after the accept
4700f1702c5SYu Xiangning 		 * has completed, before the new socket is returned to the
4710f1702c5SYu Xiangning 		 * application.
4720f1702c5SYu Xiangning 		 */
4730f1702c5SYu Xiangning 		if (sti->sti_direct) {
4740f1702c5SYu Xiangning 			queue_t *tq = stp->sd_wrq->q_next;
4750f1702c5SYu Xiangning 
4760f1702c5SYu Xiangning 			/*
4770f1702c5SYu Xiangning 			 * sti_direct is currently supported and tested
4780f1702c5SYu Xiangning 			 * only for tcp/udp; this is the main reason to
4790f1702c5SYu Xiangning 			 * have the following assertions.
4800f1702c5SYu Xiangning 			 */
4810f1702c5SYu Xiangning 			ASSERT(so->so_family == AF_INET ||
4820f1702c5SYu Xiangning 			    so->so_family == AF_INET6);
4830f1702c5SYu Xiangning 			ASSERT(so->so_protocol == IPPROTO_UDP ||
4840f1702c5SYu Xiangning 			    so->so_protocol == IPPROTO_TCP ||
4850f1702c5SYu Xiangning 			    so->so_protocol == IPPROTO_IP);
4860f1702c5SYu Xiangning 			ASSERT(so->so_type == SOCK_DGRAM ||
4870f1702c5SYu Xiangning 			    so->so_type == SOCK_STREAM);
4880f1702c5SYu Xiangning 
4890f1702c5SYu Xiangning 			/*
4900f1702c5SYu Xiangning 			 * Abort direct call interface if the module directly
4910f1702c5SYu Xiangning 			 * underneath the stream head is not defined with the
4920f1702c5SYu Xiangning 			 * _D_DIRECT flag.  This could happen in the tcp or
4930f1702c5SYu Xiangning 			 * udp case, when some other module is autopushed
4940f1702c5SYu Xiangning 			 * above it, or for some reasons the expected module
4950f1702c5SYu Xiangning 			 * isn't purely D_MP (which is the main requirement).
4960f1702c5SYu Xiangning 			 */
4970f1702c5SYu Xiangning 			if (!socktpi_direct || !(tq->q_flag & _QDIRECT) ||
4980f1702c5SYu Xiangning 			    !(_OTHERQ(tq)->q_flag & _QDIRECT)) {
4990f1702c5SYu Xiangning 				int rval;
5000f1702c5SYu Xiangning 
5010f1702c5SYu Xiangning 				/* Continue on without direct calls */
5020f1702c5SYu Xiangning 				sti->sti_direct = 0;
5030f1702c5SYu Xiangning 
5040f1702c5SYu Xiangning 				/*
5050f1702c5SYu Xiangning 				 * Cannot issue ioctl on fallback socket since
5060f1702c5SYu Xiangning 				 * there is no conn associated with the queue.
5070f1702c5SYu Xiangning 				 * The fallback downcall will notify the proto
5080f1702c5SYu Xiangning 				 * of the change.
5090f1702c5SYu Xiangning 				 */
5100f1702c5SYu Xiangning 				if (!(flags & SO_ACCEPTOR) &&
5110f1702c5SYu Xiangning 				    !(flags & SO_FALLBACK)) {
5120f1702c5SYu Xiangning 					if ((error = strioctl(vp,
5130f1702c5SYu Xiangning 					    _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
5140f1702c5SYu Xiangning 					    cr, &rval)) != 0) {
5150f1702c5SYu Xiangning 						(void) sotpi_close(so, flags,
5160f1702c5SYu Xiangning 						    cr);
5170f1702c5SYu Xiangning 						return (error);
5180f1702c5SYu Xiangning 					}
5190f1702c5SYu Xiangning 				}
5200f1702c5SYu Xiangning 			}
5210f1702c5SYu Xiangning 		}
5220f1702c5SYu Xiangning 
5230f1702c5SYu Xiangning 		if (flags & SO_FALLBACK) {
5240f1702c5SYu Xiangning 			/*
5250f1702c5SYu Xiangning 			 * The stream created does not have a conn.
5260f1702c5SYu Xiangning 			 * do stream set up after conn has been assigned
5270f1702c5SYu Xiangning 			 */
5280f1702c5SYu Xiangning 			return (error);
5290f1702c5SYu Xiangning 		}
5307c478bd9Sstevel@tonic-gate 		if (error = so_strinit(so, tso)) {
5310f1702c5SYu Xiangning 			(void) sotpi_close(so, flags, cr);
5320f1702c5SYu Xiangning 			return (error);
5337c478bd9Sstevel@tonic-gate 		}
5347c478bd9Sstevel@tonic-gate 
5350f1702c5SYu Xiangning 		/* Wildcard */
5360f1702c5SYu Xiangning 		if (so->so_protocol != so->so_sockparams->sp_protocol) {
5370f1702c5SYu Xiangning 			int protocol = so->so_protocol;
5380f1702c5SYu Xiangning 			/*
5390f1702c5SYu Xiangning 			 * Issue SO_PROTOTYPE setsockopt.
5400f1702c5SYu Xiangning 			 */
5410f1702c5SYu Xiangning 			error = sotpi_setsockopt(so, SOL_SOCKET, SO_PROTOTYPE,
5420f1702c5SYu Xiangning 			    &protocol, (t_uscalar_t)sizeof (protocol), cr);
5430f1702c5SYu Xiangning 			if (error != 0) {
5440f1702c5SYu Xiangning 				(void) sotpi_close(so, flags, cr);
5450f1702c5SYu Xiangning 				/*
5460f1702c5SYu Xiangning 				 * Setsockopt often fails with ENOPROTOOPT but
5470f1702c5SYu Xiangning 				 * socket() should fail with
5480f1702c5SYu Xiangning 				 * EPROTONOSUPPORT/EPROTOTYPE.
5490f1702c5SYu Xiangning 				 */
5500f1702c5SYu Xiangning 				return (EPROTONOSUPPORT);
5510f1702c5SYu Xiangning 			}
5520f1702c5SYu Xiangning 		}
5537c478bd9Sstevel@tonic-gate 
5540f1702c5SYu Xiangning 	} else {
5550f1702c5SYu Xiangning 		/*
5560f1702c5SYu Xiangning 		 * While the same socket can not be reopened (unlike specfs)
5570f1702c5SYu Xiangning 		 * the stream head sets STREOPENFAIL when the autopush fails.
5580f1702c5SYu Xiangning 		 */
5590f1702c5SYu Xiangning 		if ((stp != NULL) &&
5600f1702c5SYu Xiangning 		    (stp->sd_flag & STREOPENFAIL)) {
5610f1702c5SYu Xiangning 			/*
5620f1702c5SYu Xiangning 			 * Open failed part way through.
5630f1702c5SYu Xiangning 			 */
5640f1702c5SYu Xiangning 			mutex_enter(&stp->sd_lock);
5650f1702c5SYu Xiangning 			stp->sd_flag &= ~STREOPENFAIL;
5660f1702c5SYu Xiangning 			mutex_exit(&stp->sd_lock);
5670f1702c5SYu Xiangning 			(void) sotpi_close(so, flags, cr);
5680f1702c5SYu Xiangning 			return (error);
5690f1702c5SYu Xiangning 			/*NOTREACHED*/
5700f1702c5SYu Xiangning 		}
5710f1702c5SYu Xiangning 		ASSERT(stp == NULL);
5720f1702c5SYu Xiangning 	}
5730f1702c5SYu Xiangning 	TRACE_4(TR_FAC_SOCKFS, TR_SOCKFS_OPEN,
5740f1702c5SYu Xiangning 	    "sockfs open:maj %d vp %p so %p error %d",
5750f1702c5SYu Xiangning 	    maj, vp, so, error);
5760f1702c5SYu Xiangning 	return (error);
5777c478bd9Sstevel@tonic-gate }
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate /*
5807c478bd9Sstevel@tonic-gate  * Bind the socket to an unspecified address in sockfs only.
5817c478bd9Sstevel@tonic-gate  * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't
5827c478bd9Sstevel@tonic-gate  * required in all cases.
5837c478bd9Sstevel@tonic-gate  */
5847c478bd9Sstevel@tonic-gate static void
5857c478bd9Sstevel@tonic-gate so_automatic_bind(struct sonode *so)
5867c478bd9Sstevel@tonic-gate {
5870f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
5887c478bd9Sstevel@tonic-gate 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
5917c478bd9Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_ISBOUND));
5920f1702c5SYu Xiangning 	ASSERT(sti->sti_unbind_mp);
5937c478bd9Sstevel@tonic-gate 
5940f1702c5SYu Xiangning 	ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
5950f1702c5SYu Xiangning 	bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
5960f1702c5SYu Xiangning 	sti->sti_laddr_sa->sa_family = so->so_family;
5977c478bd9Sstevel@tonic-gate 	so->so_state |= SS_ISBOUND;
5987c478bd9Sstevel@tonic-gate }
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate /*
6027c478bd9Sstevel@tonic-gate  * bind the socket.
6037c478bd9Sstevel@tonic-gate  *
6047c478bd9Sstevel@tonic-gate  * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2
6057c478bd9Sstevel@tonic-gate  * are passed in we allow rebinding. Note that for backwards compatibility
6067c478bd9Sstevel@tonic-gate  * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind.
6077c478bd9Sstevel@tonic-gate  * Thus the rebinding code is currently not executed.
6087c478bd9Sstevel@tonic-gate  *
6097c478bd9Sstevel@tonic-gate  * The constraints for rebinding are:
6107c478bd9Sstevel@tonic-gate  * - it is a SOCK_DGRAM, or
6117c478bd9Sstevel@tonic-gate  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6127c478bd9Sstevel@tonic-gate  *   and no listen() has been done.
6137c478bd9Sstevel@tonic-gate  * This rebinding code was added based on some language in the XNET book
6147c478bd9Sstevel@tonic-gate  * about not returning EINVAL it the protocol allows rebinding. However,
6157c478bd9Sstevel@tonic-gate  * this language is not present in the Posix socket draft. Thus maybe the
6167c478bd9Sstevel@tonic-gate  * rebinding logic should be deleted from the source.
6177c478bd9Sstevel@tonic-gate  *
6187c478bd9Sstevel@tonic-gate  * A null "name" can be used to unbind the socket if:
6197c478bd9Sstevel@tonic-gate  * - it is a SOCK_DGRAM, or
6207c478bd9Sstevel@tonic-gate  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
6217c478bd9Sstevel@tonic-gate  *   and no listen() has been done.
6227c478bd9Sstevel@tonic-gate  */
6230f1702c5SYu Xiangning /* ARGSUSED */
6247c478bd9Sstevel@tonic-gate static int
6257c478bd9Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name,
6260f1702c5SYu Xiangning     socklen_t namelen, int backlog, int flags, struct cred *cr)
6277c478bd9Sstevel@tonic-gate {
6287c478bd9Sstevel@tonic-gate 	struct T_bind_req	bind_req;
6297c478bd9Sstevel@tonic-gate 	struct T_bind_ack	*bind_ack;
6307c478bd9Sstevel@tonic-gate 	int			error = 0;
6317c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
6327c478bd9Sstevel@tonic-gate 	void			*addr;
6337c478bd9Sstevel@tonic-gate 	t_uscalar_t		addrlen;
6347c478bd9Sstevel@tonic-gate 	int			unbind_on_err = 1;
6357c478bd9Sstevel@tonic-gate 	boolean_t		clear_acceptconn_on_err = B_FALSE;
6367c478bd9Sstevel@tonic-gate 	boolean_t		restore_backlog_on_err = B_FALSE;
6377c478bd9Sstevel@tonic-gate 	int			save_so_backlog;
6387c478bd9Sstevel@tonic-gate 	t_scalar_t		PRIM_type = O_T_BIND_REQ;
6397c478bd9Sstevel@tonic-gate 	boolean_t		tcp_udp_xport;
6407c478bd9Sstevel@tonic-gate 	void			*nl7c = NULL;
6410f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n",
644903a11ebSrh87107 	    (void *)so, (void *)name, namelen, backlog, flags,
6457c478bd9Sstevel@tonic-gate 	    pr_state(so->so_state, so->so_mode)));
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate 	tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 	if (!(flags & _SOBIND_LOCK_HELD)) {
6507c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
6517c478bd9Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
6527c478bd9Sstevel@tonic-gate 	} else {
6537c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
6547c478bd9Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
6557c478bd9Sstevel@tonic-gate 	}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * Make sure that there is a preallocated unbind_req message
6597c478bd9Sstevel@tonic-gate 	 * before binding. This message allocated when the socket is
6607c478bd9Sstevel@tonic-gate 	 * created  but it might be have been consumed.
6617c478bd9Sstevel@tonic-gate 	 */
6620f1702c5SYu Xiangning 	if (sti->sti_unbind_mp == NULL) {
6637c478bd9Sstevel@tonic-gate 		dprintso(so, 1, ("sobind: allocating unbind_req\n"));
6647c478bd9Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
6650f1702c5SYu Xiangning 		sti->sti_unbind_mp =
666de8c4a14SErik Nordmark 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP,
667de8c4a14SErik Nordmark 		    cr);
6687c478bd9Sstevel@tonic-gate 	}
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	if (flags & _SOBIND_REBIND) {
6717c478bd9Sstevel@tonic-gate 		/*
6727c478bd9Sstevel@tonic-gate 		 * Called from solisten after doing an sotpi_unbind() or
6737c478bd9Sstevel@tonic-gate 		 * potentially without the unbind (latter for AF_INET{,6}).
6747c478bd9Sstevel@tonic-gate 		 */
6757c478bd9Sstevel@tonic-gate 		ASSERT(name == NULL && namelen == 0);
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
6780f1702c5SYu Xiangning 			ASSERT(sti->sti_ux_bound_vp);
6790f1702c5SYu Xiangning 			addr = &sti->sti_ux_laddr;
6800f1702c5SYu Xiangning 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
681fc80c0dfSnordmark 			dprintso(so, 1, ("sobind rebind UNIX: addrlen %d, "
682fc80c0dfSnordmark 			    "addr 0x%p, vp %p\n",
6837c478bd9Sstevel@tonic-gate 			    addrlen,
684903a11ebSrh87107 			    (void *)((struct so_ux_addr *)addr)->soua_vp,
6850f1702c5SYu Xiangning 			    (void *)sti->sti_ux_bound_vp));
6867c478bd9Sstevel@tonic-gate 		} else {
6870f1702c5SYu Xiangning 			addr = sti->sti_laddr_sa;
6880f1702c5SYu Xiangning 			addrlen = (t_uscalar_t)sti->sti_laddr_len;
6897c478bd9Sstevel@tonic-gate 		}
6907c478bd9Sstevel@tonic-gate 	} else if (flags & _SOBIND_UNSPEC) {
6917c478bd9Sstevel@tonic-gate 		ASSERT(name == NULL && namelen == 0);
6927c478bd9Sstevel@tonic-gate 
6937c478bd9Sstevel@tonic-gate 		/*
6947c478bd9Sstevel@tonic-gate 		 * The caller checked SS_ISBOUND but not necessarily
6957c478bd9Sstevel@tonic-gate 		 * under so_lock
6967c478bd9Sstevel@tonic-gate 		 */
6977c478bd9Sstevel@tonic-gate 		if (so->so_state & SS_ISBOUND) {
6987c478bd9Sstevel@tonic-gate 			/* No error */
6997c478bd9Sstevel@tonic-gate 			goto done;
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 		/* Set an initial local address */
7037c478bd9Sstevel@tonic-gate 		switch (so->so_family) {
7047c478bd9Sstevel@tonic-gate 		case AF_UNIX:
7057c478bd9Sstevel@tonic-gate 			/*
7067c478bd9Sstevel@tonic-gate 			 * Use an address with same size as struct sockaddr
7077c478bd9Sstevel@tonic-gate 			 * just like BSD.
7087c478bd9Sstevel@tonic-gate 			 */
7090f1702c5SYu Xiangning 			sti->sti_laddr_len =
7107c478bd9Sstevel@tonic-gate 			    (socklen_t)sizeof (struct sockaddr);
7110f1702c5SYu Xiangning 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7120f1702c5SYu Xiangning 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7130f1702c5SYu Xiangning 			sti->sti_laddr_sa->sa_family = so->so_family;
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 			/*
7167c478bd9Sstevel@tonic-gate 			 * Pass down an address with the implicit bind
7177c478bd9Sstevel@tonic-gate 			 * magic number and the rest all zeros.
7187c478bd9Sstevel@tonic-gate 			 * The transport will return a unique address.
7197c478bd9Sstevel@tonic-gate 			 */
7200f1702c5SYu Xiangning 			sti->sti_ux_laddr.soua_vp = NULL;
7210f1702c5SYu Xiangning 			sti->sti_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT;
7220f1702c5SYu Xiangning 			addr = &sti->sti_ux_laddr;
7230f1702c5SYu Xiangning 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
7247c478bd9Sstevel@tonic-gate 			break;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 		case AF_INET:
7277c478bd9Sstevel@tonic-gate 		case AF_INET6:
7287c478bd9Sstevel@tonic-gate 			/*
7297c478bd9Sstevel@tonic-gate 			 * An unspecified bind in TPI has a NULL address.
7307c478bd9Sstevel@tonic-gate 			 * Set the address in sockfs to have the sa_family.
7317c478bd9Sstevel@tonic-gate 			 */
7320f1702c5SYu Xiangning 			sti->sti_laddr_len = (so->so_family == AF_INET) ?
7337c478bd9Sstevel@tonic-gate 			    (socklen_t)sizeof (sin_t) :
7347c478bd9Sstevel@tonic-gate 			    (socklen_t)sizeof (sin6_t);
7350f1702c5SYu Xiangning 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
7360f1702c5SYu Xiangning 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7370f1702c5SYu Xiangning 			sti->sti_laddr_sa->sa_family = so->so_family;
7387c478bd9Sstevel@tonic-gate 			addr = NULL;
7397c478bd9Sstevel@tonic-gate 			addrlen = 0;
7407c478bd9Sstevel@tonic-gate 			break;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		default:
7437c478bd9Sstevel@tonic-gate 			/*
7447c478bd9Sstevel@tonic-gate 			 * An unspecified bind in TPI has a NULL address.
7457c478bd9Sstevel@tonic-gate 			 * Set the address in sockfs to be zero length.
7467c478bd9Sstevel@tonic-gate 			 *
7477c478bd9Sstevel@tonic-gate 			 * Can not assume there is a sa_family for all
7487c478bd9Sstevel@tonic-gate 			 * protocol families. For example, AF_X25 does not
7497c478bd9Sstevel@tonic-gate 			 * have a family field.
7507c478bd9Sstevel@tonic-gate 			 */
7510f1702c5SYu Xiangning 			bzero(sti->sti_laddr_sa, sti->sti_laddr_len);
7520f1702c5SYu Xiangning 			sti->sti_laddr_len = 0;	/* XXX correct? */
7537c478bd9Sstevel@tonic-gate 			addr = NULL;
7547c478bd9Sstevel@tonic-gate 			addrlen = 0;
7557c478bd9Sstevel@tonic-gate 			break;
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	} else {
7597c478bd9Sstevel@tonic-gate 		if (so->so_state & SS_ISBOUND) {
7607c478bd9Sstevel@tonic-gate 			/*
7617c478bd9Sstevel@tonic-gate 			 * If it is ok to rebind the socket, first unbind
7627c478bd9Sstevel@tonic-gate 			 * with the transport. A rebind to the NULL address
7637c478bd9Sstevel@tonic-gate 			 * is interpreted as an unbind.
7647c478bd9Sstevel@tonic-gate 			 * Note that a bind to NULL in BSD does unbind the
7657c478bd9Sstevel@tonic-gate 			 * socket but it fails with EINVAL.
7667c478bd9Sstevel@tonic-gate 			 * Note that regular sockets set SOV_SOCKBSD i.e.
7677c478bd9Sstevel@tonic-gate 			 * _SOBIND_SOCKBSD gets set here hence no type of
7687c478bd9Sstevel@tonic-gate 			 * socket does currently allow rebinding.
7697c478bd9Sstevel@tonic-gate 			 *
7707c478bd9Sstevel@tonic-gate 			 * If the name is NULL just do an unbind.
7717c478bd9Sstevel@tonic-gate 			 */
7727c478bd9Sstevel@tonic-gate 			if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) &&
7737c478bd9Sstevel@tonic-gate 			    name != NULL) {
7747c478bd9Sstevel@tonic-gate 				error = EINVAL;
7757c478bd9Sstevel@tonic-gate 				unbind_on_err = 0;
7767c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
7777c478bd9Sstevel@tonic-gate 				goto done;
7787c478bd9Sstevel@tonic-gate 			}
7797c478bd9Sstevel@tonic-gate 			if ((so->so_mode & SM_CONNREQUIRED) &&
7807c478bd9Sstevel@tonic-gate 			    (so->so_state & SS_CANTREBIND)) {
7817c478bd9Sstevel@tonic-gate 				error = EINVAL;
7827c478bd9Sstevel@tonic-gate 				unbind_on_err = 0;
7837c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
7847c478bd9Sstevel@tonic-gate 				goto done;
7857c478bd9Sstevel@tonic-gate 			}
7867c478bd9Sstevel@tonic-gate 			error = sotpi_unbind(so, 0);
7877c478bd9Sstevel@tonic-gate 			if (error) {
7887c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
7897c478bd9Sstevel@tonic-gate 				goto done;
7907c478bd9Sstevel@tonic-gate 			}
7917c478bd9Sstevel@tonic-gate 			ASSERT(!(so->so_state & SS_ISBOUND));
7927c478bd9Sstevel@tonic-gate 			if (name == NULL) {
7937c478bd9Sstevel@tonic-gate 				so->so_state &=
7947c478bd9Sstevel@tonic-gate 				    ~(SS_ISCONNECTED|SS_ISCONNECTING);
7957c478bd9Sstevel@tonic-gate 				goto done;
7967c478bd9Sstevel@tonic-gate 			}
7977c478bd9Sstevel@tonic-gate 		}
7980f1702c5SYu Xiangning 
7997c478bd9Sstevel@tonic-gate 		/* X/Open requires this check */
8007c478bd9Sstevel@tonic-gate 		if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
8017c478bd9Sstevel@tonic-gate 			if (xnet_check_print) {
8027c478bd9Sstevel@tonic-gate 				printf("sockfs: X/Open bind state check "
8037c478bd9Sstevel@tonic-gate 				    "caused EINVAL\n");
8047c478bd9Sstevel@tonic-gate 			}
8057c478bd9Sstevel@tonic-gate 			error = EINVAL;
8067c478bd9Sstevel@tonic-gate 			goto done;
8077c478bd9Sstevel@tonic-gate 		}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 		switch (so->so_family) {
8107c478bd9Sstevel@tonic-gate 		case AF_UNIX:
8117c478bd9Sstevel@tonic-gate 			/*
8127c478bd9Sstevel@tonic-gate 			 * All AF_UNIX addresses are nul terminated
8137c478bd9Sstevel@tonic-gate 			 * when copied (copyin_name) in so the minimum
8147c478bd9Sstevel@tonic-gate 			 * length is 3 bytes.
8157c478bd9Sstevel@tonic-gate 			 */
8167c478bd9Sstevel@tonic-gate 			if (name == NULL ||
8177c478bd9Sstevel@tonic-gate 			    (ssize_t)namelen <= sizeof (short) + 1) {
8187c478bd9Sstevel@tonic-gate 				error = EISDIR;
8197c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
8207c478bd9Sstevel@tonic-gate 				goto done;
8217c478bd9Sstevel@tonic-gate 			}
8227c478bd9Sstevel@tonic-gate 			/*
8237c478bd9Sstevel@tonic-gate 			 * Verify so_family matches the bound family.
8247c478bd9Sstevel@tonic-gate 			 * BSD does not check this for AF_UNIX resulting
8257c478bd9Sstevel@tonic-gate 			 * in funny mknods.
8267c478bd9Sstevel@tonic-gate 			 */
8277c478bd9Sstevel@tonic-gate 			if (name->sa_family != so->so_family) {
8287c478bd9Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8297c478bd9Sstevel@tonic-gate 				goto done;
8307c478bd9Sstevel@tonic-gate 			}
8317c478bd9Sstevel@tonic-gate 			break;
8327c478bd9Sstevel@tonic-gate 		case AF_INET:
8337c478bd9Sstevel@tonic-gate 			if (name == NULL) {
8347c478bd9Sstevel@tonic-gate 				error = EINVAL;
8357c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
8367c478bd9Sstevel@tonic-gate 				goto done;
8377c478bd9Sstevel@tonic-gate 			}
8387c478bd9Sstevel@tonic-gate 			if ((size_t)namelen != sizeof (sin_t)) {
8397c478bd9Sstevel@tonic-gate 				error = name->sa_family != so->so_family ?
8407c478bd9Sstevel@tonic-gate 				    EAFNOSUPPORT : EINVAL;
8417c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
8427c478bd9Sstevel@tonic-gate 				goto done;
8437c478bd9Sstevel@tonic-gate 			}
8447c478bd9Sstevel@tonic-gate 			if ((flags & _SOBIND_XPG4_2) &&
8457c478bd9Sstevel@tonic-gate 			    (name->sa_family != so->so_family)) {
8467c478bd9Sstevel@tonic-gate 				/*
8477c478bd9Sstevel@tonic-gate 				 * This check has to be made for X/Open
8487c478bd9Sstevel@tonic-gate 				 * sockets however application failures have
8497c478bd9Sstevel@tonic-gate 				 * been observed when it is applied to
8507c478bd9Sstevel@tonic-gate 				 * all sockets.
8517c478bd9Sstevel@tonic-gate 				 */
8527c478bd9Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8537c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
8547c478bd9Sstevel@tonic-gate 				goto done;
8557c478bd9Sstevel@tonic-gate 			}
8567c478bd9Sstevel@tonic-gate 			/*
8577c478bd9Sstevel@tonic-gate 			 * Force a zero sa_family to match so_family.
8587c478bd9Sstevel@tonic-gate 			 *
8597c478bd9Sstevel@tonic-gate 			 * Some programs like inetd(1M) don't set the
8607c478bd9Sstevel@tonic-gate 			 * family field. Other programs leave
8617c478bd9Sstevel@tonic-gate 			 * sin_family set to garbage - SunOS 4.X does
8627c478bd9Sstevel@tonic-gate 			 * not check the family field on a bind.
8637c478bd9Sstevel@tonic-gate 			 * We use the family field that
8647c478bd9Sstevel@tonic-gate 			 * was passed in to the socket() call.
8657c478bd9Sstevel@tonic-gate 			 */
8667c478bd9Sstevel@tonic-gate 			name->sa_family = so->so_family;
8677c478bd9Sstevel@tonic-gate 			break;
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate 		case AF_INET6: {
8707c478bd9Sstevel@tonic-gate #ifdef DEBUG
8717c478bd9Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)name;
8727c478bd9Sstevel@tonic-gate #endif /* DEBUG */
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 			if (name == NULL) {
8757c478bd9Sstevel@tonic-gate 				error = EINVAL;
8767c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
8777c478bd9Sstevel@tonic-gate 				goto done;
8787c478bd9Sstevel@tonic-gate 			}
8797c478bd9Sstevel@tonic-gate 			if ((size_t)namelen != sizeof (sin6_t)) {
8807c478bd9Sstevel@tonic-gate 				error = name->sa_family != so->so_family ?
8817c478bd9Sstevel@tonic-gate 				    EAFNOSUPPORT : EINVAL;
8827c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
8837c478bd9Sstevel@tonic-gate 				goto done;
8847c478bd9Sstevel@tonic-gate 			}
8857c478bd9Sstevel@tonic-gate 			if (name->sa_family != so->so_family) {
8867c478bd9Sstevel@tonic-gate 				/*
8877c478bd9Sstevel@tonic-gate 				 * With IPv6 we require the family to match
8887c478bd9Sstevel@tonic-gate 				 * unlike in IPv4.
8897c478bd9Sstevel@tonic-gate 				 */
8907c478bd9Sstevel@tonic-gate 				error = EAFNOSUPPORT;
8917c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
8927c478bd9Sstevel@tonic-gate 				goto done;
8937c478bd9Sstevel@tonic-gate 			}
8947c478bd9Sstevel@tonic-gate #ifdef DEBUG
8957c478bd9Sstevel@tonic-gate 			/*
8967c478bd9Sstevel@tonic-gate 			 * Verify that apps don't forget to clear
8977c478bd9Sstevel@tonic-gate 			 * sin6_scope_id etc
8987c478bd9Sstevel@tonic-gate 			 */
8997c478bd9Sstevel@tonic-gate 			if (sin6->sin6_scope_id != 0 &&
9007c478bd9Sstevel@tonic-gate 			    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
9012caf0dcdSrshoaib 				zcmn_err(getzoneid(), CE_WARN,
9027c478bd9Sstevel@tonic-gate 				    "bind with uninitialized sin6_scope_id "
9037c478bd9Sstevel@tonic-gate 				    "(%d) on socket. Pid = %d\n",
9047c478bd9Sstevel@tonic-gate 				    (int)sin6->sin6_scope_id,
9057c478bd9Sstevel@tonic-gate 				    (int)curproc->p_pid);
9067c478bd9Sstevel@tonic-gate 			}
9077c478bd9Sstevel@tonic-gate 			if (sin6->__sin6_src_id != 0) {
9082caf0dcdSrshoaib 				zcmn_err(getzoneid(), CE_WARN,
9097c478bd9Sstevel@tonic-gate 				    "bind with uninitialized __sin6_src_id "
9107c478bd9Sstevel@tonic-gate 				    "(%d) on socket. Pid = %d\n",
9117c478bd9Sstevel@tonic-gate 				    (int)sin6->__sin6_src_id,
9127c478bd9Sstevel@tonic-gate 				    (int)curproc->p_pid);
9137c478bd9Sstevel@tonic-gate 			}
9147c478bd9Sstevel@tonic-gate #endif /* DEBUG */
9157c478bd9Sstevel@tonic-gate 			break;
9167c478bd9Sstevel@tonic-gate 		}
9177c478bd9Sstevel@tonic-gate 		default:
9187c478bd9Sstevel@tonic-gate 			/*
9197c478bd9Sstevel@tonic-gate 			 * Don't do any length or sa_family check to allow
9207c478bd9Sstevel@tonic-gate 			 * non-sockaddr style addresses.
9217c478bd9Sstevel@tonic-gate 			 */
9227c478bd9Sstevel@tonic-gate 			if (name == NULL) {
9237c478bd9Sstevel@tonic-gate 				error = EINVAL;
9247c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
9257c478bd9Sstevel@tonic-gate 				goto done;
9267c478bd9Sstevel@tonic-gate 			}
9277c478bd9Sstevel@tonic-gate 			break;
9287c478bd9Sstevel@tonic-gate 		}
9297c478bd9Sstevel@tonic-gate 
9300f1702c5SYu Xiangning 		if (namelen > (t_uscalar_t)sti->sti_laddr_maxlen) {
9317c478bd9Sstevel@tonic-gate 			error = ENAMETOOLONG;
9327c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
9337c478bd9Sstevel@tonic-gate 			goto done;
9347c478bd9Sstevel@tonic-gate 		}
9357c478bd9Sstevel@tonic-gate 		/*
9367c478bd9Sstevel@tonic-gate 		 * Save local address.
9377c478bd9Sstevel@tonic-gate 		 */
9380f1702c5SYu Xiangning 		sti->sti_laddr_len = (socklen_t)namelen;
9390f1702c5SYu Xiangning 		ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
9400f1702c5SYu Xiangning 		bcopy(name, sti->sti_laddr_sa, namelen);
9417c478bd9Sstevel@tonic-gate 
9420f1702c5SYu Xiangning 		addr = sti->sti_laddr_sa;
9430f1702c5SYu Xiangning 		addrlen = (t_uscalar_t)sti->sti_laddr_len;
9447c478bd9Sstevel@tonic-gate 		switch (so->so_family) {
9457c478bd9Sstevel@tonic-gate 		case AF_INET6:
9467c478bd9Sstevel@tonic-gate 		case AF_INET:
9477c478bd9Sstevel@tonic-gate 			break;
9487c478bd9Sstevel@tonic-gate 		case AF_UNIX: {
9497c478bd9Sstevel@tonic-gate 			struct sockaddr_un *soun =
9500f1702c5SYu Xiangning 			    (struct sockaddr_un *)sti->sti_laddr_sa;
95192f45f6dSRic Aleshire 			struct vnode *vp, *rvp;
9527c478bd9Sstevel@tonic-gate 			struct vattr vattr;
9537c478bd9Sstevel@tonic-gate 
9540f1702c5SYu Xiangning 			ASSERT(sti->sti_ux_bound_vp == NULL);
9557c478bd9Sstevel@tonic-gate 			/*
9567c478bd9Sstevel@tonic-gate 			 * Create vnode for the specified path name.
9570f1702c5SYu Xiangning 			 * Keep vnode held with a reference in sti_ux_bound_vp.
9587c478bd9Sstevel@tonic-gate 			 * Use the vnode pointer as the address used in the
9597c478bd9Sstevel@tonic-gate 			 * bind with the transport.
9607c478bd9Sstevel@tonic-gate 			 *
9617c478bd9Sstevel@tonic-gate 			 * Use the same mode as in BSD. In particular this does
9627c478bd9Sstevel@tonic-gate 			 * not observe the umask.
9637c478bd9Sstevel@tonic-gate 			 */
9647c478bd9Sstevel@tonic-gate 			/* MAXPATHLEN + soun_family + nul termination */
9650f1702c5SYu Xiangning 			if (sti->sti_laddr_len >
9667c478bd9Sstevel@tonic-gate 			    (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
9677c478bd9Sstevel@tonic-gate 				error = ENAMETOOLONG;
9687c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
9697c478bd9Sstevel@tonic-gate 				goto done;
9707c478bd9Sstevel@tonic-gate 			}
9717c478bd9Sstevel@tonic-gate 			vattr.va_type = VSOCK;
972ae115bc7Smrj 			vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask;
9737c478bd9Sstevel@tonic-gate 			vattr.va_mask = AT_TYPE|AT_MODE;
9747c478bd9Sstevel@tonic-gate 			/* NOTE: holding so_lock */
9757c478bd9Sstevel@tonic-gate 			error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr,
9767c478bd9Sstevel@tonic-gate 			    EXCL, 0, &vp, CRMKNOD, 0, 0);
9777c478bd9Sstevel@tonic-gate 			if (error) {
9787c478bd9Sstevel@tonic-gate 				if (error == EEXIST)
9797c478bd9Sstevel@tonic-gate 					error = EADDRINUSE;
9807c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
9817c478bd9Sstevel@tonic-gate 				goto done;
9827c478bd9Sstevel@tonic-gate 			}
9837c478bd9Sstevel@tonic-gate 			/*
9847c478bd9Sstevel@tonic-gate 			 * Establish pointer from the underlying filesystem
9857c478bd9Sstevel@tonic-gate 			 * vnode to the socket node.
9860f1702c5SYu Xiangning 			 * sti_ux_bound_vp and v_stream->sd_vnode form the
9877c478bd9Sstevel@tonic-gate 			 * cross-linkage between the underlying filesystem
9887c478bd9Sstevel@tonic-gate 			 * node and the socket node.
9897c478bd9Sstevel@tonic-gate 			 */
99092f45f6dSRic Aleshire 
99192f45f6dSRic Aleshire 			if ((VOP_REALVP(vp, &rvp, NULL) == 0) && (vp != rvp)) {
99292f45f6dSRic Aleshire 				VN_HOLD(rvp);
99392f45f6dSRic Aleshire 				VN_RELE(vp);
99492f45f6dSRic Aleshire 				vp = rvp;
99592f45f6dSRic Aleshire 			}
99692f45f6dSRic Aleshire 
9977c478bd9Sstevel@tonic-gate 			ASSERT(SOTOV(so)->v_stream);
9987c478bd9Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
9997c478bd9Sstevel@tonic-gate 			vp->v_stream = SOTOV(so)->v_stream;
10000f1702c5SYu Xiangning 			sti->sti_ux_bound_vp = vp;
10017c478bd9Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 			/*
10047c478bd9Sstevel@tonic-gate 			 * Use the vnode pointer value as a unique address
10057c478bd9Sstevel@tonic-gate 			 * (together with the magic number to avoid conflicts
10067c478bd9Sstevel@tonic-gate 			 * with implicit binds) in the transport provider.
10077c478bd9Sstevel@tonic-gate 			 */
10080f1702c5SYu Xiangning 			sti->sti_ux_laddr.soua_vp =
10090f1702c5SYu Xiangning 			    (void *)sti->sti_ux_bound_vp;
10100f1702c5SYu Xiangning 			sti->sti_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT;
10110f1702c5SYu Xiangning 			addr = &sti->sti_ux_laddr;
10120f1702c5SYu Xiangning 			addrlen = (t_uscalar_t)sizeof (sti->sti_ux_laddr);
10137c478bd9Sstevel@tonic-gate 			dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n",
10147c478bd9Sstevel@tonic-gate 			    addrlen,
10150f1702c5SYu Xiangning 			    (void *)((struct so_ux_addr *)addr)->soua_vp));
10167c478bd9Sstevel@tonic-gate 			break;
10177c478bd9Sstevel@tonic-gate 		}
10187c478bd9Sstevel@tonic-gate 		} /* end switch (so->so_family) */
10197c478bd9Sstevel@tonic-gate 	}
10207c478bd9Sstevel@tonic-gate 
10217c478bd9Sstevel@tonic-gate 	/*
10227c478bd9Sstevel@tonic-gate 	 * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since
10237c478bd9Sstevel@tonic-gate 	 * the transport can start passing up T_CONN_IND messages
10247c478bd9Sstevel@tonic-gate 	 * as soon as it receives the bind req and strsock_proto()
10257c478bd9Sstevel@tonic-gate 	 * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs.
10267c478bd9Sstevel@tonic-gate 	 */
10277c478bd9Sstevel@tonic-gate 	if (flags & _SOBIND_LISTEN) {
10287c478bd9Sstevel@tonic-gate 		if ((so->so_state & SS_ACCEPTCONN) == 0)
10297c478bd9Sstevel@tonic-gate 			clear_acceptconn_on_err = B_TRUE;
10307c478bd9Sstevel@tonic-gate 		save_so_backlog = so->so_backlog;
10317c478bd9Sstevel@tonic-gate 		restore_backlog_on_err = B_TRUE;
10327c478bd9Sstevel@tonic-gate 		so->so_state |= SS_ACCEPTCONN;
10337c478bd9Sstevel@tonic-gate 		so->so_backlog = backlog;
10347c478bd9Sstevel@tonic-gate 	}
10357c478bd9Sstevel@tonic-gate 
10367c478bd9Sstevel@tonic-gate 	/*
10377c478bd9Sstevel@tonic-gate 	 * If NL7C addr(s) have been configured check for addr/port match,
10387c478bd9Sstevel@tonic-gate 	 * or if an implicit NL7C socket via AF_NCA mark socket as NL7C.
10397c478bd9Sstevel@tonic-gate 	 *
10407c478bd9Sstevel@tonic-gate 	 * NL7C supports the TCP transport only so check AF_INET and AF_INET6
10417c478bd9Sstevel@tonic-gate 	 * family sockets only. If match mark as such.
10427c478bd9Sstevel@tonic-gate 	 */
10432c9e429eSbrutus 	if (nl7c_enabled && ((addr != NULL &&
10447c478bd9Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
10457c478bd9Sstevel@tonic-gate 	    (nl7c = nl7c_lookup_addr(addr, addrlen))) ||
10460f1702c5SYu Xiangning 	    sti->sti_nl7c_flags == NL7C_AF_NCA)) {
10477c478bd9Sstevel@tonic-gate 		/*
10487c478bd9Sstevel@tonic-gate 		 * NL7C is not supported in non-global zones,
10497c478bd9Sstevel@tonic-gate 		 * we enforce this restriction here.
10507c478bd9Sstevel@tonic-gate 		 */
10517c478bd9Sstevel@tonic-gate 		if (so->so_zoneid == GLOBAL_ZONEID) {
10527c478bd9Sstevel@tonic-gate 			/* An NL7C socket, mark it */
10530f1702c5SYu Xiangning 			sti->sti_nl7c_flags |= NL7C_ENABLED;
10542c9e429eSbrutus 			if (nl7c == NULL) {
10552c9e429eSbrutus 				/*
10562c9e429eSbrutus 				 * Was an AF_NCA bind() so add it to the
10572c9e429eSbrutus 				 * addr list for reporting purposes.
10582c9e429eSbrutus 				 */
10592c9e429eSbrutus 				nl7c = nl7c_add_addr(addr, addrlen);
10602c9e429eSbrutus 			}
10617c478bd9Sstevel@tonic-gate 		} else
10627c478bd9Sstevel@tonic-gate 			nl7c = NULL;
10637c478bd9Sstevel@tonic-gate 	}
10640f1702c5SYu Xiangning 
10657c478bd9Sstevel@tonic-gate 	/*
10667c478bd9Sstevel@tonic-gate 	 * We send a T_BIND_REQ for TCP/UDP since we know it supports it,
10677c478bd9Sstevel@tonic-gate 	 * for other transports we will send in a O_T_BIND_REQ.
10687c478bd9Sstevel@tonic-gate 	 */
10697c478bd9Sstevel@tonic-gate 	if (tcp_udp_xport &&
10707c478bd9Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6))
10717c478bd9Sstevel@tonic-gate 		PRIM_type = T_BIND_REQ;
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate 	bind_req.PRIM_type = PRIM_type;
10747c478bd9Sstevel@tonic-gate 	bind_req.ADDR_length = addrlen;
10757c478bd9Sstevel@tonic-gate 	bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
10767c478bd9Sstevel@tonic-gate 	bind_req.CONIND_number = backlog;
10777c478bd9Sstevel@tonic-gate 	/* NOTE: holding so_lock while sleeping */
10787c478bd9Sstevel@tonic-gate 	mp = soallocproto2(&bind_req, sizeof (bind_req),
1079de8c4a14SErik Nordmark 	    addr, addrlen, 0, _ALLOC_SLEEP, cr);
10800f1702c5SYu Xiangning 	sti->sti_laddr_valid = 0;
1081c28749e9Skais 
10820f1702c5SYu Xiangning 	/* Done using sti_laddr_sa - can drop the lock */
10837c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
10867c478bd9Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
10877c478bd9Sstevel@tonic-gate 	if (error) {
10887c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
10897c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
10907c478bd9Sstevel@tonic-gate 		goto done;
10917c478bd9Sstevel@tonic-gate 	}
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
10947c478bd9Sstevel@tonic-gate 	error = sowaitprim(so, PRIM_type, T_BIND_ACK,
10957c478bd9Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (*bind_ack), &mp, 0);
10967c478bd9Sstevel@tonic-gate 	if (error) {
10977c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
10987c478bd9Sstevel@tonic-gate 		goto done;
10997c478bd9Sstevel@tonic-gate 	}
11007c478bd9Sstevel@tonic-gate 	ASSERT(mp);
11017c478bd9Sstevel@tonic-gate 	/*
11027c478bd9Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
11037c478bd9Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the bind
11047c478bd9Sstevel@tonic-gate 	 * is allowed to complete.
11057c478bd9Sstevel@tonic-gate 	 */
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	/* Mark as bound. This will be undone if we detect errors below. */
11087c478bd9Sstevel@tonic-gate 	if (flags & _SOBIND_NOXLATE) {
11097c478bd9Sstevel@tonic-gate 		ASSERT(so->so_family == AF_UNIX);
11100f1702c5SYu Xiangning 		sti->sti_faddr_noxlate = 1;
11117c478bd9Sstevel@tonic-gate 	}
11127c478bd9Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND));
11137c478bd9Sstevel@tonic-gate 	so->so_state |= SS_ISBOUND;
11140f1702c5SYu Xiangning 	ASSERT(sti->sti_unbind_mp);
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	/* note that we've already set SS_ACCEPTCONN above */
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	/*
11197c478bd9Sstevel@tonic-gate 	 * Recompute addrlen - an unspecied bind sent down an
11207c478bd9Sstevel@tonic-gate 	 * address of length zero but we expect the appropriate length
11217c478bd9Sstevel@tonic-gate 	 * in return.
11227c478bd9Sstevel@tonic-gate 	 */
11237c478bd9Sstevel@tonic-gate 	addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ?
11240f1702c5SYu Xiangning 	    sizeof (sti->sti_ux_laddr) : sti->sti_laddr_len);
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	bind_ack = (struct T_bind_ack *)mp->b_rptr;
11277c478bd9Sstevel@tonic-gate 	/*
11287c478bd9Sstevel@tonic-gate 	 * The alignment restriction is really too strict but
11297c478bd9Sstevel@tonic-gate 	 * we want enough alignment to inspect the fields of
11307c478bd9Sstevel@tonic-gate 	 * a sockaddr_in.
11317c478bd9Sstevel@tonic-gate 	 */
11327c478bd9Sstevel@tonic-gate 	addr = sogetoff(mp, bind_ack->ADDR_offset,
11337c478bd9Sstevel@tonic-gate 	    bind_ack->ADDR_length,
11347c478bd9Sstevel@tonic-gate 	    __TPI_ALIGN_SIZE);
11357c478bd9Sstevel@tonic-gate 	if (addr == NULL) {
11367c478bd9Sstevel@tonic-gate 		freemsg(mp);
11377c478bd9Sstevel@tonic-gate 		error = EPROTO;
11387c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
11397c478bd9Sstevel@tonic-gate 		goto done;
11407c478bd9Sstevel@tonic-gate 	}
11417c478bd9Sstevel@tonic-gate 	if (!(flags & _SOBIND_UNSPEC)) {
11427c478bd9Sstevel@tonic-gate 		/*
11437c478bd9Sstevel@tonic-gate 		 * Verify that the transport didn't return something we
11447c478bd9Sstevel@tonic-gate 		 * did not want e.g. an address other than what we asked for.
11457c478bd9Sstevel@tonic-gate 		 *
11467c478bd9Sstevel@tonic-gate 		 * NOTE: These checks would go away if/when we switch to
11477c478bd9Sstevel@tonic-gate 		 * using the new TPI (in which the transport would fail
11487c478bd9Sstevel@tonic-gate 		 * the request instead of assigning a different address).
11497c478bd9Sstevel@tonic-gate 		 *
11507c478bd9Sstevel@tonic-gate 		 * NOTE2: For protocols that we don't know (i.e. any
11517c478bd9Sstevel@tonic-gate 		 * other than AF_INET6, AF_INET and AF_UNIX), we
11527c478bd9Sstevel@tonic-gate 		 * cannot know if the transport should be expected to
11537c478bd9Sstevel@tonic-gate 		 * return the same address as that requested.
11547c478bd9Sstevel@tonic-gate 		 *
11557c478bd9Sstevel@tonic-gate 		 * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send
11567c478bd9Sstevel@tonic-gate 		 * down a T_BIND_REQ. We use O_T_BIND_REQ for others.
11577c478bd9Sstevel@tonic-gate 		 *
11587c478bd9Sstevel@tonic-gate 		 * For example, in the case of netatalk it may be
11597c478bd9Sstevel@tonic-gate 		 * inappropriate for the transport to return the
11607c478bd9Sstevel@tonic-gate 		 * requested address (as it may have allocated a local
11617c478bd9Sstevel@tonic-gate 		 * port number in behaviour similar to that of an
11627c478bd9Sstevel@tonic-gate 		 * AF_INET bind request with a port number of zero).
11637c478bd9Sstevel@tonic-gate 		 *
11647c478bd9Sstevel@tonic-gate 		 * Given the definition of O_T_BIND_REQ, where the
11657c478bd9Sstevel@tonic-gate 		 * transport may bind to an address other than the
11667c478bd9Sstevel@tonic-gate 		 * requested address, it's not possible to determine
11677c478bd9Sstevel@tonic-gate 		 * whether a returned address that differs from the
11687c478bd9Sstevel@tonic-gate 		 * requested address is a reason to fail (because the
11697c478bd9Sstevel@tonic-gate 		 * requested address was not available) or succeed
11707c478bd9Sstevel@tonic-gate 		 * (because the transport allocated an appropriate
11717c478bd9Sstevel@tonic-gate 		 * address and/or port).
11727c478bd9Sstevel@tonic-gate 		 *
11737c478bd9Sstevel@tonic-gate 		 * sockfs currently requires that the transport return
11747c478bd9Sstevel@tonic-gate 		 * the requested address in the T_BIND_ACK, unless
11757c478bd9Sstevel@tonic-gate 		 * there is code here to allow for any discrepancy.
11767c478bd9Sstevel@tonic-gate 		 * Such code exists for AF_INET and AF_INET6.
11777c478bd9Sstevel@tonic-gate 		 *
11787c478bd9Sstevel@tonic-gate 		 * Netatalk chooses to return the requested address
11797c478bd9Sstevel@tonic-gate 		 * rather than the (correct) allocated address.  This
11807c478bd9Sstevel@tonic-gate 		 * means that netatalk violates the TPI specification
11817c478bd9Sstevel@tonic-gate 		 * (and would not function correctly if used from a
11827c478bd9Sstevel@tonic-gate 		 * TLI application), but it does mean that it works
11837c478bd9Sstevel@tonic-gate 		 * with sockfs.
11847c478bd9Sstevel@tonic-gate 		 *
11857c478bd9Sstevel@tonic-gate 		 * As noted above, using the newer XTI bind primitive
11867c478bd9Sstevel@tonic-gate 		 * (T_BIND_REQ) in preference to O_T_BIND_REQ would
11877c478bd9Sstevel@tonic-gate 		 * allow sockfs to be more sure about whether or not
11887c478bd9Sstevel@tonic-gate 		 * the bind request had succeeded (as transports are
11897c478bd9Sstevel@tonic-gate 		 * not permitted to bind to a different address than
11907c478bd9Sstevel@tonic-gate 		 * that requested - they must return failure).
11917c478bd9Sstevel@tonic-gate 		 * Unfortunately, support for T_BIND_REQ may not be
11927c478bd9Sstevel@tonic-gate 		 * present in all transport implementations (netatalk,
11937c478bd9Sstevel@tonic-gate 		 * for example, doesn't have it), making the
11947c478bd9Sstevel@tonic-gate 		 * transition difficult.
11957c478bd9Sstevel@tonic-gate 		 */
11967c478bd9Sstevel@tonic-gate 		if (bind_ack->ADDR_length != addrlen) {
11977c478bd9Sstevel@tonic-gate 			/* Assumes that the requested address was in use */
11987c478bd9Sstevel@tonic-gate 			freemsg(mp);
11997c478bd9Sstevel@tonic-gate 			error = EADDRINUSE;
12007c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
12017c478bd9Sstevel@tonic-gate 			goto done;
12027c478bd9Sstevel@tonic-gate 		}
12037c478bd9Sstevel@tonic-gate 
12047c478bd9Sstevel@tonic-gate 		switch (so->so_family) {
12057c478bd9Sstevel@tonic-gate 		case AF_INET6:
12067c478bd9Sstevel@tonic-gate 		case AF_INET: {
12077c478bd9Sstevel@tonic-gate 			sin_t *rname, *aname;
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate 			rname = (sin_t *)addr;
12100f1702c5SYu Xiangning 			aname = (sin_t *)sti->sti_laddr_sa;
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate 			/*
12137c478bd9Sstevel@tonic-gate 			 * Take advantage of the alignment
12147c478bd9Sstevel@tonic-gate 			 * of sin_port and sin6_port which fall
12157c478bd9Sstevel@tonic-gate 			 * in the same place in their data structures.
12167c478bd9Sstevel@tonic-gate 			 * Just use sin_port for either address family.
12177c478bd9Sstevel@tonic-gate 			 *
12187c478bd9Sstevel@tonic-gate 			 * This may become a problem if (heaven forbid)
12197c478bd9Sstevel@tonic-gate 			 * there's a separate ipv6port_reserved... :-P
12207c478bd9Sstevel@tonic-gate 			 *
12217c478bd9Sstevel@tonic-gate 			 * Binding to port 0 has the semantics of letting
12227c478bd9Sstevel@tonic-gate 			 * the transport bind to any port.
12237c478bd9Sstevel@tonic-gate 			 *
12247c478bd9Sstevel@tonic-gate 			 * If the transport is TCP or UDP since we had sent
12257c478bd9Sstevel@tonic-gate 			 * a T_BIND_REQ we would not get a port other than
12267c478bd9Sstevel@tonic-gate 			 * what we asked for.
12277c478bd9Sstevel@tonic-gate 			 */
12287c478bd9Sstevel@tonic-gate 			if (tcp_udp_xport) {
12297c478bd9Sstevel@tonic-gate 				/*
12307c478bd9Sstevel@tonic-gate 				 * Pick up the new port number if we bound to
12317c478bd9Sstevel@tonic-gate 				 * port 0.
12327c478bd9Sstevel@tonic-gate 				 */
12337c478bd9Sstevel@tonic-gate 				if (aname->sin_port == 0)
12347c478bd9Sstevel@tonic-gate 					aname->sin_port = rname->sin_port;
12350f1702c5SYu Xiangning 				sti->sti_laddr_valid = 1;
12367c478bd9Sstevel@tonic-gate 				break;
12377c478bd9Sstevel@tonic-gate 			}
12387c478bd9Sstevel@tonic-gate 			if (aname->sin_port != 0 &&
12397c478bd9Sstevel@tonic-gate 			    aname->sin_port != rname->sin_port) {
12407c478bd9Sstevel@tonic-gate 				freemsg(mp);
12417c478bd9Sstevel@tonic-gate 				error = EADDRINUSE;
12427c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
12437c478bd9Sstevel@tonic-gate 				goto done;
12447c478bd9Sstevel@tonic-gate 			}
12457c478bd9Sstevel@tonic-gate 			/*
12467c478bd9Sstevel@tonic-gate 			 * Pick up the new port number if we bound to port 0.
12477c478bd9Sstevel@tonic-gate 			 */
12487c478bd9Sstevel@tonic-gate 			aname->sin_port = rname->sin_port;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 			/*
12517c478bd9Sstevel@tonic-gate 			 * Unfortunately, addresses aren't _quite_ the same.
12527c478bd9Sstevel@tonic-gate 			 */
12537c478bd9Sstevel@tonic-gate 			if (so->so_family == AF_INET) {
12547c478bd9Sstevel@tonic-gate 				if (aname->sin_addr.s_addr !=
12557c478bd9Sstevel@tonic-gate 				    rname->sin_addr.s_addr) {
12567c478bd9Sstevel@tonic-gate 					freemsg(mp);
12577c478bd9Sstevel@tonic-gate 					error = EADDRNOTAVAIL;
12587c478bd9Sstevel@tonic-gate 					eprintsoline(so, error);
12597c478bd9Sstevel@tonic-gate 					goto done;
12607c478bd9Sstevel@tonic-gate 				}
12617c478bd9Sstevel@tonic-gate 			} else {
12627c478bd9Sstevel@tonic-gate 				sin6_t *rname6 = (sin6_t *)rname;
12637c478bd9Sstevel@tonic-gate 				sin6_t *aname6 = (sin6_t *)aname;
12647c478bd9Sstevel@tonic-gate 
12657c478bd9Sstevel@tonic-gate 				if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr,
12667c478bd9Sstevel@tonic-gate 				    &rname6->sin6_addr)) {
12677c478bd9Sstevel@tonic-gate 					freemsg(mp);
12687c478bd9Sstevel@tonic-gate 					error = EADDRNOTAVAIL;
12697c478bd9Sstevel@tonic-gate 					eprintsoline(so, error);
12707c478bd9Sstevel@tonic-gate 					goto done;
12717c478bd9Sstevel@tonic-gate 				}
12727c478bd9Sstevel@tonic-gate 			}
12737c478bd9Sstevel@tonic-gate 			break;
12747c478bd9Sstevel@tonic-gate 		}
12757c478bd9Sstevel@tonic-gate 		case AF_UNIX:
12760f1702c5SYu Xiangning 			if (bcmp(addr, &sti->sti_ux_laddr, addrlen) != 0) {
12777c478bd9Sstevel@tonic-gate 				freemsg(mp);
12787c478bd9Sstevel@tonic-gate 				error = EADDRINUSE;
12797c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
12807c478bd9Sstevel@tonic-gate 				eprintso(so,
12817c478bd9Sstevel@tonic-gate 				    ("addrlen %d, addr 0x%x, vp %p\n",
12827c478bd9Sstevel@tonic-gate 				    addrlen, *((int *)addr),
12830f1702c5SYu Xiangning 				    (void *)sti->sti_ux_bound_vp));
12847c478bd9Sstevel@tonic-gate 				goto done;
12857c478bd9Sstevel@tonic-gate 			}
12860f1702c5SYu Xiangning 			sti->sti_laddr_valid = 1;
12877c478bd9Sstevel@tonic-gate 			break;
12887c478bd9Sstevel@tonic-gate 		default:
12897c478bd9Sstevel@tonic-gate 			/*
12907c478bd9Sstevel@tonic-gate 			 * NOTE: This assumes that addresses can be
12917c478bd9Sstevel@tonic-gate 			 * byte-compared for equivalence.
12927c478bd9Sstevel@tonic-gate 			 */
12930f1702c5SYu Xiangning 			if (bcmp(addr, sti->sti_laddr_sa, addrlen) != 0) {
12947c478bd9Sstevel@tonic-gate 				freemsg(mp);
12957c478bd9Sstevel@tonic-gate 				error = EADDRINUSE;
12967c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
12977c478bd9Sstevel@tonic-gate 				goto done;
12987c478bd9Sstevel@tonic-gate 			}
12997c478bd9Sstevel@tonic-gate 			/*
13000f1702c5SYu Xiangning 			 * Don't mark sti_laddr_valid, as we cannot be
13017c478bd9Sstevel@tonic-gate 			 * sure that the returned address is the real
13027c478bd9Sstevel@tonic-gate 			 * bound address when talking to an unknown
13037c478bd9Sstevel@tonic-gate 			 * transport.
13047c478bd9Sstevel@tonic-gate 			 */
13057c478bd9Sstevel@tonic-gate 			break;
13067c478bd9Sstevel@tonic-gate 		}
13077c478bd9Sstevel@tonic-gate 	} else {
13087c478bd9Sstevel@tonic-gate 		/*
13097c478bd9Sstevel@tonic-gate 		 * Save for returned address for getsockname.
13107c478bd9Sstevel@tonic-gate 		 * Needed for unspecific bind unless transport supports
13117c478bd9Sstevel@tonic-gate 		 * the TI_GETMYNAME ioctl.
13127c478bd9Sstevel@tonic-gate 		 * Do this for AF_INET{,6} even though they do, as
13137c478bd9Sstevel@tonic-gate 		 * caching info here is much better performance than
13147c478bd9Sstevel@tonic-gate 		 * a TPI/STREAMS trip to the transport for getsockname.
13157c478bd9Sstevel@tonic-gate 		 * Any which can't for some reason _must_ _not_ set
13160f1702c5SYu Xiangning 		 * sti_laddr_valid here for the caching version of
13170f1702c5SYu Xiangning 		 * getsockname to not break;
13187c478bd9Sstevel@tonic-gate 		 */
13197c478bd9Sstevel@tonic-gate 		switch (so->so_family) {
13207c478bd9Sstevel@tonic-gate 		case AF_UNIX:
13217c478bd9Sstevel@tonic-gate 			/*
13227c478bd9Sstevel@tonic-gate 			 * Record the address bound with the transport
13237c478bd9Sstevel@tonic-gate 			 * for use by socketpair.
13247c478bd9Sstevel@tonic-gate 			 */
13250f1702c5SYu Xiangning 			bcopy(addr, &sti->sti_ux_laddr, addrlen);
13260f1702c5SYu Xiangning 			sti->sti_laddr_valid = 1;
13277c478bd9Sstevel@tonic-gate 			break;
13287c478bd9Sstevel@tonic-gate 		case AF_INET:
13297c478bd9Sstevel@tonic-gate 		case AF_INET6:
13300f1702c5SYu Xiangning 			ASSERT(sti->sti_laddr_len <= sti->sti_laddr_maxlen);
13310f1702c5SYu Xiangning 			bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
13320f1702c5SYu Xiangning 			sti->sti_laddr_valid = 1;
13337c478bd9Sstevel@tonic-gate 			break;
13347c478bd9Sstevel@tonic-gate 		default:
13357c478bd9Sstevel@tonic-gate 			/*
13360f1702c5SYu Xiangning 			 * Don't mark sti_laddr_valid, as we cannot be
13377c478bd9Sstevel@tonic-gate 			 * sure that the returned address is the real
13387c478bd9Sstevel@tonic-gate 			 * bound address when talking to an unknown
13397c478bd9Sstevel@tonic-gate 			 * transport.
13407c478bd9Sstevel@tonic-gate 			 */
13417c478bd9Sstevel@tonic-gate 			break;
13427c478bd9Sstevel@tonic-gate 		}
13437c478bd9Sstevel@tonic-gate 	}
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	if (nl7c != NULL) {
13462c9e429eSbrutus 		/* Register listen()er sonode pointer with NL7C */
13472c9e429eSbrutus 		nl7c_listener_addr(nl7c, so);
13487c478bd9Sstevel@tonic-gate 	}
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	freemsg(mp);
13517c478bd9Sstevel@tonic-gate 
13527c478bd9Sstevel@tonic-gate done:
13537c478bd9Sstevel@tonic-gate 	if (error) {
13547c478bd9Sstevel@tonic-gate 		/* reset state & backlog to values held on entry */
13557c478bd9Sstevel@tonic-gate 		if (clear_acceptconn_on_err == B_TRUE)
13567c478bd9Sstevel@tonic-gate 			so->so_state &= ~SS_ACCEPTCONN;
13577c478bd9Sstevel@tonic-gate 		if (restore_backlog_on_err == B_TRUE)
13587c478bd9Sstevel@tonic-gate 			so->so_backlog = save_so_backlog;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 		if (unbind_on_err && so->so_state & SS_ISBOUND) {
13617c478bd9Sstevel@tonic-gate 			int err;
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 			err = sotpi_unbind(so, 0);
13647c478bd9Sstevel@tonic-gate 			/* LINTED - statement has no consequent: if */
13657c478bd9Sstevel@tonic-gate 			if (err) {
13667c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
13677c478bd9Sstevel@tonic-gate 			} else {
13687c478bd9Sstevel@tonic-gate 				ASSERT(!(so->so_state & SS_ISBOUND));
13697c478bd9Sstevel@tonic-gate 			}
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate 	if (!(flags & _SOBIND_LOCK_HELD)) {
13737c478bd9Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
13747c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
13757c478bd9Sstevel@tonic-gate 	} else {
13767c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
13777c478bd9Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
13787c478bd9Sstevel@tonic-gate 	}
13797c478bd9Sstevel@tonic-gate 	return (error);
13807c478bd9Sstevel@tonic-gate }
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate /* bind the socket */
1383ff550d0eSmasputra static int
13847c478bd9Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
13850f1702c5SYu Xiangning     int flags, struct cred *cr)
13867c478bd9Sstevel@tonic-gate {
13877c478bd9Sstevel@tonic-gate 	if ((flags & _SOBIND_SOCKETPAIR) == 0)
13880f1702c5SYu Xiangning 		return (sotpi_bindlisten(so, name, namelen, 0, flags, cr));
13897c478bd9Sstevel@tonic-gate 
13907c478bd9Sstevel@tonic-gate 	flags &= ~_SOBIND_SOCKETPAIR;
13910f1702c5SYu Xiangning 	return (sotpi_bindlisten(so, name, namelen, 1, flags, cr));
13927c478bd9Sstevel@tonic-gate }
13937c478bd9Sstevel@tonic-gate 
13947c478bd9Sstevel@tonic-gate /*
13957c478bd9Sstevel@tonic-gate  * Unbind a socket - used when bind() fails, when bind() specifies a NULL
13967c478bd9Sstevel@tonic-gate  * address, or when listen needs to unbind and bind.
13977c478bd9Sstevel@tonic-gate  * If the _SOUNBIND_REBIND flag is specified the addresses are retained
13987c478bd9Sstevel@tonic-gate  * so that a sobind can pick them up.
13997c478bd9Sstevel@tonic-gate  */
14007c478bd9Sstevel@tonic-gate static int
14017c478bd9Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags)
14027c478bd9Sstevel@tonic-gate {
14037c478bd9Sstevel@tonic-gate 	struct T_unbind_req	unbind_req;
14047c478bd9Sstevel@tonic-gate 	int			error = 0;
14057c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
14060f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n",
1409903a11ebSrh87107 	    (void *)so, flags, pr_state(so->so_state, so->so_mode)));
14107c478bd9Sstevel@tonic-gate 
14117c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14127c478bd9Sstevel@tonic-gate 	ASSERT(so->so_flag & SOLOCKED);
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
14157c478bd9Sstevel@tonic-gate 		error = EINVAL;
14167c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
14177c478bd9Sstevel@tonic-gate 		goto done;
14187c478bd9Sstevel@tonic-gate 	}
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
14217c478bd9Sstevel@tonic-gate 
14227c478bd9Sstevel@tonic-gate 	/*
14237c478bd9Sstevel@tonic-gate 	 * Flush the read and write side (except stream head read queue)
14247c478bd9Sstevel@tonic-gate 	 * and send down T_UNBIND_REQ.
14257c478bd9Sstevel@tonic-gate 	 */
14267c478bd9Sstevel@tonic-gate 	(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
14277c478bd9Sstevel@tonic-gate 
14287c478bd9Sstevel@tonic-gate 	unbind_req.PRIM_type = T_UNBIND_REQ;
14297c478bd9Sstevel@tonic-gate 	mp = soallocproto1(&unbind_req, sizeof (unbind_req),
1430de8c4a14SErik Nordmark 	    0, _ALLOC_SLEEP, CRED());
14317c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
14327c478bd9Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
14337c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
14347c478bd9Sstevel@tonic-gate 	if (error) {
14357c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
14367c478bd9Sstevel@tonic-gate 		goto done;
14377c478bd9Sstevel@tonic-gate 	}
14387c478bd9Sstevel@tonic-gate 
14397c478bd9Sstevel@tonic-gate 	error = sowaitokack(so, T_UNBIND_REQ);
14407c478bd9Sstevel@tonic-gate 	if (error) {
14417c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
14427c478bd9Sstevel@tonic-gate 		goto done;
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	/*
14467c478bd9Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
14477c478bd9Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the unbind
14487c478bd9Sstevel@tonic-gate 	 * is allowed to complete.
14497c478bd9Sstevel@tonic-gate 	 */
14507c478bd9Sstevel@tonic-gate 	if (!(flags & _SOUNBIND_REBIND)) {
14517c478bd9Sstevel@tonic-gate 		/*
14527c478bd9Sstevel@tonic-gate 		 * Clear out bound address.
14537c478bd9Sstevel@tonic-gate 		 */
14547c478bd9Sstevel@tonic-gate 		vnode_t *vp;
14557c478bd9Sstevel@tonic-gate 
14560f1702c5SYu Xiangning 		if ((vp = sti->sti_ux_bound_vp) != NULL) {
14570f1702c5SYu Xiangning 			sti->sti_ux_bound_vp = NULL;
14587c478bd9Sstevel@tonic-gate 			vn_rele_stream(vp);
14597c478bd9Sstevel@tonic-gate 		}
14607c478bd9Sstevel@tonic-gate 		/* Clear out address */
14610f1702c5SYu Xiangning 		sti->sti_laddr_len = 0;
14627c478bd9Sstevel@tonic-gate 	}
14630f1702c5SYu Xiangning 	so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN);
14640f1702c5SYu Xiangning 	sti->sti_laddr_valid = 0;
14652c9e429eSbrutus 
14667c478bd9Sstevel@tonic-gate done:
1467c28749e9Skais 
14687c478bd9Sstevel@tonic-gate 	/* If the caller held the lock don't release it here */
14697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
14707c478bd9Sstevel@tonic-gate 	ASSERT(so->so_flag & SOLOCKED);
14717c478bd9Sstevel@tonic-gate 
14727c478bd9Sstevel@tonic-gate 	return (error);
14737c478bd9Sstevel@tonic-gate }
14747c478bd9Sstevel@tonic-gate 
14757c478bd9Sstevel@tonic-gate /*
14767c478bd9Sstevel@tonic-gate  * listen on the socket.
14777c478bd9Sstevel@tonic-gate  * For TPI conforming transports this has to first unbind with the transport
14787c478bd9Sstevel@tonic-gate  * and then bind again using the new backlog.
14797c478bd9Sstevel@tonic-gate  */
14800f1702c5SYu Xiangning /* ARGSUSED */
14817c478bd9Sstevel@tonic-gate int
14820f1702c5SYu Xiangning sotpi_listen(struct sonode *so, int backlog, struct cred *cr)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate 	int		error = 0;
14850f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n",
1488903a11ebSrh87107 	    (void *)so, backlog, pr_state(so->so_state, so->so_mode)));
14897c478bd9Sstevel@tonic-gate 
14900f1702c5SYu Xiangning 	if (sti->sti_serv_type == T_CLTS)
14917c478bd9Sstevel@tonic-gate 		return (EOPNOTSUPP);
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 	/*
14947c478bd9Sstevel@tonic-gate 	 * If the socket is ready to accept connections already, then
14957c478bd9Sstevel@tonic-gate 	 * return without doing anything.  This avoids a problem where
14967c478bd9Sstevel@tonic-gate 	 * a second listen() call fails if a connection is pending and
14977c478bd9Sstevel@tonic-gate 	 * leaves the socket unbound. Only when we are not unbinding
14987c478bd9Sstevel@tonic-gate 	 * with the transport can we safely increase the backlog.
14997c478bd9Sstevel@tonic-gate 	 */
15007c478bd9Sstevel@tonic-gate 	if (so->so_state & SS_ACCEPTCONN &&
15017c478bd9Sstevel@tonic-gate 	    !((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15027c478bd9Sstevel@tonic-gate 	    /*CONSTCOND*/
15037c478bd9Sstevel@tonic-gate 	    !solisten_tpi_tcp))
15047c478bd9Sstevel@tonic-gate 		return (0);
15057c478bd9Sstevel@tonic-gate 
15067c478bd9Sstevel@tonic-gate 	if (so->so_state & SS_ISCONNECTED)
15077c478bd9Sstevel@tonic-gate 		return (EINVAL);
15087c478bd9Sstevel@tonic-gate 
15097c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
15107c478bd9Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	/*
15137c478bd9Sstevel@tonic-gate 	 * If the listen doesn't change the backlog we do nothing.
15147c478bd9Sstevel@tonic-gate 	 * This avoids an EPROTO error from the transport.
15157c478bd9Sstevel@tonic-gate 	 */
15167c478bd9Sstevel@tonic-gate 	if ((so->so_state & SS_ACCEPTCONN) &&
15177c478bd9Sstevel@tonic-gate 	    so->so_backlog == backlog)
15187c478bd9Sstevel@tonic-gate 		goto done;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
15217c478bd9Sstevel@tonic-gate 		/*
15227c478bd9Sstevel@tonic-gate 		 * Must have been explicitly bound in the UNIX domain.
15237c478bd9Sstevel@tonic-gate 		 */
15247c478bd9Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
15257c478bd9Sstevel@tonic-gate 			error = EINVAL;
15267c478bd9Sstevel@tonic-gate 			goto done;
15277c478bd9Sstevel@tonic-gate 		}
15287c478bd9Sstevel@tonic-gate 		error = sotpi_bindlisten(so, NULL, 0, backlog,
15290f1702c5SYu Xiangning 		    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15307c478bd9Sstevel@tonic-gate 	} else if (backlog > 0) {
15317c478bd9Sstevel@tonic-gate 		/*
15327c478bd9Sstevel@tonic-gate 		 * AF_INET{,6} hack to avoid losing the port.
15337c478bd9Sstevel@tonic-gate 		 * Assumes that all AF_INET{,6} transports can handle a
15347c478bd9Sstevel@tonic-gate 		 * O_T_BIND_REQ with a non-zero CONIND_number when the TPI
15357c478bd9Sstevel@tonic-gate 		 * has already bound thus it is possible to avoid the unbind.
15367c478bd9Sstevel@tonic-gate 		 */
15377c478bd9Sstevel@tonic-gate 		if (!((so->so_family == AF_INET || so->so_family == AF_INET6) &&
15387c478bd9Sstevel@tonic-gate 		    /*CONSTCOND*/
15397c478bd9Sstevel@tonic-gate 		    !solisten_tpi_tcp)) {
15407c478bd9Sstevel@tonic-gate 			error = sotpi_unbind(so, _SOUNBIND_REBIND);
15417c478bd9Sstevel@tonic-gate 			if (error)
15427c478bd9Sstevel@tonic-gate 				goto done;
15437c478bd9Sstevel@tonic-gate 		}
15447c478bd9Sstevel@tonic-gate 		error = sotpi_bindlisten(so, NULL, 0, backlog,
15450f1702c5SYu Xiangning 		    _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN, cr);
15467c478bd9Sstevel@tonic-gate 	} else {
15477c478bd9Sstevel@tonic-gate 		so->so_state |= SS_ACCEPTCONN;
15487c478bd9Sstevel@tonic-gate 		so->so_backlog = backlog;
15497c478bd9Sstevel@tonic-gate 	}
15507c478bd9Sstevel@tonic-gate 	if (error)
15517c478bd9Sstevel@tonic-gate 		goto done;
15527c478bd9Sstevel@tonic-gate 	ASSERT(so->so_state & SS_ACCEPTCONN);
15537c478bd9Sstevel@tonic-gate done:
15547c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
15557c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
15567c478bd9Sstevel@tonic-gate 	return (error);
15577c478bd9Sstevel@tonic-gate }
15587c478bd9Sstevel@tonic-gate 
15597c478bd9Sstevel@tonic-gate /*
15607c478bd9Sstevel@tonic-gate  * Disconnect either a specified seqno or all (-1).
15617c478bd9Sstevel@tonic-gate  * The former is used on listening sockets only.
15627c478bd9Sstevel@tonic-gate  *
15637c478bd9Sstevel@tonic-gate  * When seqno == -1 sodisconnect could call sotpi_unbind. However,
15647c478bd9Sstevel@tonic-gate  * the current use of sodisconnect(seqno == -1) is only for shutdown
15657c478bd9Sstevel@tonic-gate  * so there is no point (and potentially incorrect) to unbind.
15667c478bd9Sstevel@tonic-gate  */
15670f1702c5SYu Xiangning static int
15687c478bd9Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags)
15697c478bd9Sstevel@tonic-gate {
15707c478bd9Sstevel@tonic-gate 	struct T_discon_req	discon_req;
15717c478bd9Sstevel@tonic-gate 	int			error = 0;
15727c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
15737c478bd9Sstevel@tonic-gate 
15747c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n",
1575903a11ebSrh87107 	    (void *)so, seqno, flags, pr_state(so->so_state, so->so_mode)));
15767c478bd9Sstevel@tonic-gate 
15777c478bd9Sstevel@tonic-gate 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
15787c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
15797c478bd9Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
15807c478bd9Sstevel@tonic-gate 	} else {
15817c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
15827c478bd9Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
15837c478bd9Sstevel@tonic-gate 	}
15847c478bd9Sstevel@tonic-gate 
15857c478bd9Sstevel@tonic-gate 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) {
15867c478bd9Sstevel@tonic-gate 		error = EINVAL;
15877c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
15887c478bd9Sstevel@tonic-gate 		goto done;
15897c478bd9Sstevel@tonic-gate 	}
15907c478bd9Sstevel@tonic-gate 
15917c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
15927c478bd9Sstevel@tonic-gate 	/*
15937c478bd9Sstevel@tonic-gate 	 * Flush the write side (unless this is a listener)
15947c478bd9Sstevel@tonic-gate 	 * and then send down a T_DISCON_REQ.
15957c478bd9Sstevel@tonic-gate 	 * (Don't flush on listener since it could flush {O_}T_CONN_RES
15967c478bd9Sstevel@tonic-gate 	 * and other messages.)
15977c478bd9Sstevel@tonic-gate 	 */
15987c478bd9Sstevel@tonic-gate 	if (!(so->so_state & SS_ACCEPTCONN))
15997c478bd9Sstevel@tonic-gate 		(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW);
16007c478bd9Sstevel@tonic-gate 
16017c478bd9Sstevel@tonic-gate 	discon_req.PRIM_type = T_DISCON_REQ;
16027c478bd9Sstevel@tonic-gate 	discon_req.SEQ_number = seqno;
16037c478bd9Sstevel@tonic-gate 	mp = soallocproto1(&discon_req, sizeof (discon_req),
1604de8c4a14SErik Nordmark 	    0, _ALLOC_SLEEP, CRED());
16057c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
16067c478bd9Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
16077c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
16087c478bd9Sstevel@tonic-gate 	if (error) {
16097c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
16107c478bd9Sstevel@tonic-gate 		goto done;
16117c478bd9Sstevel@tonic-gate 	}
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate 	error = sowaitokack(so, T_DISCON_REQ);
16147c478bd9Sstevel@tonic-gate 	if (error) {
16157c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
16167c478bd9Sstevel@tonic-gate 		goto done;
16177c478bd9Sstevel@tonic-gate 	}
16187c478bd9Sstevel@tonic-gate 	/*
16197c478bd9Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
16207c478bd9Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the disconnect
16217c478bd9Sstevel@tonic-gate 	 * is allowed to complete. However, it is not possible to
16227c478bd9Sstevel@tonic-gate 	 * assert that SS_ISCONNECTED|SS_ISCONNECTING are set.
16237c478bd9Sstevel@tonic-gate 	 */
16240f1702c5SYu Xiangning 	so->so_state &= ~(SS_ISCONNECTED|SS_ISCONNECTING);
16250f1702c5SYu Xiangning 	SOTOTPI(so)->sti_laddr_valid = 0;
16260f1702c5SYu Xiangning 	SOTOTPI(so)->sti_faddr_valid = 0;
16277c478bd9Sstevel@tonic-gate done:
16287c478bd9Sstevel@tonic-gate 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
16297c478bd9Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
16307c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
16317c478bd9Sstevel@tonic-gate 	} else {
16327c478bd9Sstevel@tonic-gate 		/* If the caller held the lock don't release it here */
16337c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
16347c478bd9Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 	return (error);
16377c478bd9Sstevel@tonic-gate }
16387c478bd9Sstevel@tonic-gate 
16390f1702c5SYu Xiangning /* ARGSUSED */
16407c478bd9Sstevel@tonic-gate int
16410f1702c5SYu Xiangning sotpi_accept(struct sonode *so, int fflag, struct cred *cr,
16420f1702c5SYu Xiangning     struct sonode **nsop)
16437c478bd9Sstevel@tonic-gate {
16447c478bd9Sstevel@tonic-gate 	struct T_conn_ind	*conn_ind;
16457c478bd9Sstevel@tonic-gate 	struct T_conn_res	*conn_res;
16467c478bd9Sstevel@tonic-gate 	int			error = 0;
1647dd49f125SAnders Persson 	mblk_t			*mp, *ack_mp;
16487c478bd9Sstevel@tonic-gate 	struct sonode		*nso;
16497c478bd9Sstevel@tonic-gate 	vnode_t			*nvp;
16507c478bd9Sstevel@tonic-gate 	void			*src;
16517c478bd9Sstevel@tonic-gate 	t_uscalar_t		srclen;
16527c478bd9Sstevel@tonic-gate 	void			*opt;
16537c478bd9Sstevel@tonic-gate 	t_uscalar_t		optlen;
16547c478bd9Sstevel@tonic-gate 	t_scalar_t		PRIM_type;
16557c478bd9Sstevel@tonic-gate 	t_scalar_t		SEQ_number;
1656188658baSja97890 	size_t			sinlen;
16570f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
16580f1702c5SYu Xiangning 	sotpi_info_t		*nsti;
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n",
1661903a11ebSrh87107 	    (void *)so, fflag, (void *)nsop,
1662903a11ebSrh87107 	    pr_state(so->so_state, so->so_mode)));
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	/*
16657c478bd9Sstevel@tonic-gate 	 * Defer single-threading the accepting socket until
16667c478bd9Sstevel@tonic-gate 	 * the T_CONN_IND has been received and parsed and the
16677c478bd9Sstevel@tonic-gate 	 * new sonode has been opened.
16687c478bd9Sstevel@tonic-gate 	 */
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	/* Check that we are not already connected */
16717c478bd9Sstevel@tonic-gate 	if ((so->so_state & SS_ACCEPTCONN) == 0)
16727c478bd9Sstevel@tonic-gate 		goto conn_bad;
16737c478bd9Sstevel@tonic-gate again:
16747c478bd9Sstevel@tonic-gate 	if ((error = sowaitconnind(so, fflag, &mp)) != 0)
16757c478bd9Sstevel@tonic-gate 		goto e_bad;
16767c478bd9Sstevel@tonic-gate 
16770f1702c5SYu Xiangning 	ASSERT(mp != NULL);
16787c478bd9Sstevel@tonic-gate 	conn_ind = (struct T_conn_ind *)mp->b_rptr;
1679c28749e9Skais 
16807c478bd9Sstevel@tonic-gate 	/*
16817c478bd9Sstevel@tonic-gate 	 * Save SEQ_number for error paths.
16827c478bd9Sstevel@tonic-gate 	 */
16837c478bd9Sstevel@tonic-gate 	SEQ_number = conn_ind->SEQ_number;
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 	srclen = conn_ind->SRC_length;
16867c478bd9Sstevel@tonic-gate 	src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1);
16877c478bd9Sstevel@tonic-gate 	if (src == NULL) {
16887c478bd9Sstevel@tonic-gate 		error = EPROTO;
16897c478bd9Sstevel@tonic-gate 		freemsg(mp);
16907c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
16917c478bd9Sstevel@tonic-gate 		goto disconnect_unlocked;
16927c478bd9Sstevel@tonic-gate 	}
16937c478bd9Sstevel@tonic-gate 	optlen = conn_ind->OPT_length;
16947c478bd9Sstevel@tonic-gate 	switch (so->so_family) {
16957c478bd9Sstevel@tonic-gate 	case AF_INET:
16967c478bd9Sstevel@tonic-gate 	case AF_INET6:
16970f1702c5SYu Xiangning 		if ((optlen == sizeof (intptr_t)) && (sti->sti_direct != 0)) {
16987c478bd9Sstevel@tonic-gate 			bcopy(mp->b_rptr + conn_ind->OPT_offset,
16997c478bd9Sstevel@tonic-gate 			    &opt, conn_ind->OPT_length);
17007c478bd9Sstevel@tonic-gate 		} else {
17017c478bd9Sstevel@tonic-gate 			/*
17027c478bd9Sstevel@tonic-gate 			 * The transport (in this case TCP) hasn't sent up
17037c478bd9Sstevel@tonic-gate 			 * a pointer to an instance for the accept fast-path.
17047c478bd9Sstevel@tonic-gate 			 * Disable fast-path completely because the call to
17057c478bd9Sstevel@tonic-gate 			 * sotpi_create() below would otherwise create an
17067c478bd9Sstevel@tonic-gate 			 * incomplete TCP instance, which would lead to
17077c478bd9Sstevel@tonic-gate 			 * problems when sockfs sends a normal T_CONN_RES
17087c478bd9Sstevel@tonic-gate 			 * message down the new stream.
17097c478bd9Sstevel@tonic-gate 			 */
17100f1702c5SYu Xiangning 			if (sti->sti_direct) {
1711ff550d0eSmasputra 				int rval;
1712ff550d0eSmasputra 				/*
1713ff550d0eSmasputra 				 * For consistency we inform tcp to disable
1714ff550d0eSmasputra 				 * direct interface on the listener, though
1715ff550d0eSmasputra 				 * we can certainly live without doing this
1716ff550d0eSmasputra 				 * because no data will ever travel upstream
1717ff550d0eSmasputra 				 * on the listening socket.
1718ff550d0eSmasputra 				 */
17190f1702c5SYu Xiangning 				sti->sti_direct = 0;
1720ff550d0eSmasputra 				(void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK,
1721de8c4a14SErik Nordmark 				    0, 0, K_TO_K, cr, &rval);
1722ff550d0eSmasputra 			}
17237c478bd9Sstevel@tonic-gate 			opt = NULL;
17247c478bd9Sstevel@tonic-gate 			optlen = 0;
17257c478bd9Sstevel@tonic-gate 		}
17267c478bd9Sstevel@tonic-gate 		break;
17277c478bd9Sstevel@tonic-gate 	case AF_UNIX:
17287c478bd9Sstevel@tonic-gate 	default:
17297c478bd9Sstevel@tonic-gate 		if (optlen != 0) {
17307c478bd9Sstevel@tonic-gate 			opt = sogetoff(mp, conn_ind->OPT_offset, optlen,
17317c478bd9Sstevel@tonic-gate 			    __TPI_ALIGN_SIZE);
17327c478bd9Sstevel@tonic-gate 			if (opt == NULL) {
17337c478bd9Sstevel@tonic-gate 				error = EPROTO;
17347c478bd9Sstevel@tonic-gate 				freemsg(mp);
17357c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
17367c478bd9Sstevel@tonic-gate 				goto disconnect_unlocked;
17377c478bd9Sstevel@tonic-gate 			}
17387c478bd9Sstevel@tonic-gate 		}
17397c478bd9Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
17400f1702c5SYu Xiangning 			if (!sti->sti_faddr_noxlate) {
17417c478bd9Sstevel@tonic-gate 				src = NULL;
17427c478bd9Sstevel@tonic-gate 				srclen = 0;
17437c478bd9Sstevel@tonic-gate 			}
17447c478bd9Sstevel@tonic-gate 			/* Extract src address from options */
17457c478bd9Sstevel@tonic-gate 			if (optlen != 0)
17467c478bd9Sstevel@tonic-gate 				so_getopt_srcaddr(opt, optlen, &src, &srclen);
17477c478bd9Sstevel@tonic-gate 		}
17487c478bd9Sstevel@tonic-gate 		break;
17497c478bd9Sstevel@tonic-gate 	}
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	/*
17527c478bd9Sstevel@tonic-gate 	 * Create the new socket.
17537c478bd9Sstevel@tonic-gate 	 */
17540f1702c5SYu Xiangning 	nso = socket_newconn(so, NULL, NULL, SOCKET_SLEEP, &error);
17557c478bd9Sstevel@tonic-gate 	if (nso == NULL) {
17567c478bd9Sstevel@tonic-gate 		ASSERT(error != 0);
17577c478bd9Sstevel@tonic-gate 		/*
17587c478bd9Sstevel@tonic-gate 		 * Accept can not fail with ENOBUFS. sotpi_create
17597c478bd9Sstevel@tonic-gate 		 * sleeps waiting for memory until a signal is caught
17607c478bd9Sstevel@tonic-gate 		 * so return EINTR.
17617c478bd9Sstevel@tonic-gate 		 */
17627c478bd9Sstevel@tonic-gate 		freemsg(mp);
17637c478bd9Sstevel@tonic-gate 		if (error == ENOBUFS)
17647c478bd9Sstevel@tonic-gate 			error = EINTR;
17657c478bd9Sstevel@tonic-gate 		goto e_disc_unl;
17667c478bd9Sstevel@tonic-gate 	}
17677c478bd9Sstevel@tonic-gate 	nvp = SOTOV(nso);
17680f1702c5SYu Xiangning 	nsti = SOTOTPI(nso);
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate #ifdef DEBUG
17717c478bd9Sstevel@tonic-gate 	/*
17727c478bd9Sstevel@tonic-gate 	 * SO_DEBUG is used to trigger the dprint* and eprint* macros thus
17737c478bd9Sstevel@tonic-gate 	 * it's inherited early to allow debugging of the accept code itself.
17747c478bd9Sstevel@tonic-gate 	 */
17757c478bd9Sstevel@tonic-gate 	nso->so_options |= so->so_options & SO_DEBUG;
17767c478bd9Sstevel@tonic-gate #endif /* DEBUG */
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate 	/*
17797c478bd9Sstevel@tonic-gate 	 * Save the SRC address from the T_CONN_IND
17807c478bd9Sstevel@tonic-gate 	 * for getpeername to work on AF_UNIX and on transports that do not
17817c478bd9Sstevel@tonic-gate 	 * support TI_GETPEERNAME.
17827c478bd9Sstevel@tonic-gate 	 *
17837c478bd9Sstevel@tonic-gate 	 * NOTE: AF_UNIX NUL termination is ensured by the sender's
17847c478bd9Sstevel@tonic-gate 	 * copyin_name().
17857c478bd9Sstevel@tonic-gate 	 */
17860f1702c5SYu Xiangning 	if (srclen > (t_uscalar_t)nsti->sti_faddr_maxlen) {
17877c478bd9Sstevel@tonic-gate 		error = EINVAL;
17887c478bd9Sstevel@tonic-gate 		freemsg(mp);
17897c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
17907c478bd9Sstevel@tonic-gate 		goto disconnect_vp_unlocked;
17917c478bd9Sstevel@tonic-gate 	}
17920f1702c5SYu Xiangning 	nsti->sti_faddr_len = (socklen_t)srclen;
17930f1702c5SYu Xiangning 	ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
17940f1702c5SYu Xiangning 	bcopy(src, nsti->sti_faddr_sa, srclen);
17950f1702c5SYu Xiangning 	nsti->sti_faddr_valid = 1;
17967c478bd9Sstevel@tonic-gate 
1797de8c4a14SErik Nordmark 	/*
1798de8c4a14SErik Nordmark 	 * Record so_peercred and so_cpid from a cred in the T_CONN_IND.
1799de8c4a14SErik Nordmark 	 */
18007c478bd9Sstevel@tonic-gate 	if ((DB_REF(mp) > 1) || MBLKSIZE(mp) <
18017c478bd9Sstevel@tonic-gate 	    (sizeof (struct T_conn_res) + sizeof (intptr_t))) {
18027c478bd9Sstevel@tonic-gate 		cred_t	*cr;
1803de8c4a14SErik Nordmark 		pid_t	cpid;
18047c478bd9Sstevel@tonic-gate 
1805de8c4a14SErik Nordmark 		cr = msg_getcred(mp, &cpid);
1806de8c4a14SErik Nordmark 		if (cr != NULL) {
18077c478bd9Sstevel@tonic-gate 			crhold(cr);
18087c478bd9Sstevel@tonic-gate 			nso->so_peercred = cr;
1809de8c4a14SErik Nordmark 			nso->so_cpid = cpid;
18107c478bd9Sstevel@tonic-gate 		}
18117c478bd9Sstevel@tonic-gate 		freemsg(mp);
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate 		mp = soallocproto1(NULL, sizeof (struct T_conn_res) +
1814d4f98ef9SAnders Persson 		    sizeof (intptr_t), 0, _ALLOC_INTR, cr);
18157c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
18167c478bd9Sstevel@tonic-gate 			/*
18177c478bd9Sstevel@tonic-gate 			 * Accept can not fail with ENOBUFS.
18187c478bd9Sstevel@tonic-gate 			 * A signal was caught so return EINTR.
18197c478bd9Sstevel@tonic-gate 			 */
18207c478bd9Sstevel@tonic-gate 			error = EINTR;
18217c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
18227c478bd9Sstevel@tonic-gate 			goto disconnect_vp_unlocked;
18237c478bd9Sstevel@tonic-gate 		}
18247c478bd9Sstevel@tonic-gate 		conn_res = (struct T_conn_res *)mp->b_rptr;
18257c478bd9Sstevel@tonic-gate 	} else {
1826de8c4a14SErik Nordmark 		/*
1827de8c4a14SErik Nordmark 		 * For efficency reasons we use msg_extractcred; no crhold
1828de8c4a14SErik Nordmark 		 * needed since db_credp is cleared (i.e., we move the cred
1829de8c4a14SErik Nordmark 		 * from the message to so_peercred.
1830de8c4a14SErik Nordmark 		 */
1831de8c4a14SErik Nordmark 		nso->so_peercred = msg_extractcred(mp, &nso->so_cpid);
18327c478bd9Sstevel@tonic-gate 
18337c478bd9Sstevel@tonic-gate 		mp->b_rptr = DB_BASE(mp);
18347c478bd9Sstevel@tonic-gate 		conn_res = (struct T_conn_res *)mp->b_rptr;
18357c478bd9Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res);
1836d4f98ef9SAnders Persson 
1837d4f98ef9SAnders Persson 		mblk_setcred(mp, cr, curproc->p_pid);
18387c478bd9Sstevel@tonic-gate 	}
18397c478bd9Sstevel@tonic-gate 
18407c478bd9Sstevel@tonic-gate 	/*
18417c478bd9Sstevel@tonic-gate 	 * New socket must be bound at least in sockfs and, except for AF_INET,
18427c478bd9Sstevel@tonic-gate 	 * (or AF_INET6) it also has to be bound in the transport provider.
1843188658baSja97890 	 * We set the local address in the sonode from the T_OK_ACK of the
1844188658baSja97890 	 * T_CONN_RES. For this reason the address we bind to here isn't
1845188658baSja97890 	 * important.
18467c478bd9Sstevel@tonic-gate 	 */
18477c478bd9Sstevel@tonic-gate 	if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
18487c478bd9Sstevel@tonic-gate 	    /*CONSTCOND*/
18497c478bd9Sstevel@tonic-gate 	    nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) {
18507c478bd9Sstevel@tonic-gate 		/*
18517c478bd9Sstevel@tonic-gate 		 * Optimization for AF_INET{,6} transports
18527c478bd9Sstevel@tonic-gate 		 * that can handle a T_CONN_RES without being bound.
18537c478bd9Sstevel@tonic-gate 		 */
18547c478bd9Sstevel@tonic-gate 		mutex_enter(&nso->so_lock);
18557c478bd9Sstevel@tonic-gate 		so_automatic_bind(nso);
18567c478bd9Sstevel@tonic-gate 		mutex_exit(&nso->so_lock);
18577c478bd9Sstevel@tonic-gate 	} else {
18587c478bd9Sstevel@tonic-gate 		/* Perform NULL bind with the transport provider. */
18590f1702c5SYu Xiangning 		if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC,
18600f1702c5SYu Xiangning 		    cr)) != 0) {
18617c478bd9Sstevel@tonic-gate 			ASSERT(error != ENOBUFS);
18627c478bd9Sstevel@tonic-gate 			freemsg(mp);
18637c478bd9Sstevel@tonic-gate 			eprintsoline(nso, error);
18647c478bd9Sstevel@tonic-gate 			goto disconnect_vp_unlocked;
18657c478bd9Sstevel@tonic-gate 		}
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate 
18687c478bd9Sstevel@tonic-gate 	/*
18697c478bd9Sstevel@tonic-gate 	 * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES
18707c478bd9Sstevel@tonic-gate 	 * so that any data arriving on the new socket will cause the
18717c478bd9Sstevel@tonic-gate 	 * appropriate signals to be delivered for the new socket.
18727c478bd9Sstevel@tonic-gate 	 *
18737c478bd9Sstevel@tonic-gate 	 * No other thread (except strsock_proto and strsock_misc)
18747c478bd9Sstevel@tonic-gate 	 * can access the new socket thus we relax the locking.
18757c478bd9Sstevel@tonic-gate 	 */
18767c478bd9Sstevel@tonic-gate 	nso->so_pgrp = so->so_pgrp;
18770f1702c5SYu Xiangning 	nso->so_state |= so->so_state & SS_ASYNC;
18780f1702c5SYu Xiangning 	nsti->sti_faddr_noxlate = sti->sti_faddr_noxlate;
18797c478bd9Sstevel@tonic-gate 
18807c478bd9Sstevel@tonic-gate 	if (nso->so_pgrp != 0) {
1881de8c4a14SErik Nordmark 		if ((error = so_set_events(nso, nvp, cr)) != 0) {
18827c478bd9Sstevel@tonic-gate 			eprintsoline(nso, error);
18837c478bd9Sstevel@tonic-gate 			error = 0;
18847c478bd9Sstevel@tonic-gate 			nso->so_pgrp = 0;
18857c478bd9Sstevel@tonic-gate 		}
18867c478bd9Sstevel@tonic-gate 	}
18877c478bd9Sstevel@tonic-gate 
18887c478bd9Sstevel@tonic-gate 	/*
18897c478bd9Sstevel@tonic-gate 	 * Make note of the socket level options. TCP and IP level options
18907c478bd9Sstevel@tonic-gate 	 * are already inherited. We could do all this after accept is
18917c478bd9Sstevel@tonic-gate 	 * successful but doing it here simplifies code and no harm done
18927c478bd9Sstevel@tonic-gate 	 * for error case.
18937c478bd9Sstevel@tonic-gate 	 */
18947c478bd9Sstevel@tonic-gate 	nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE|
18957c478bd9Sstevel@tonic-gate 	    SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
18967c478bd9Sstevel@tonic-gate 	    SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
18977c478bd9Sstevel@tonic-gate 	nso->so_sndbuf = so->so_sndbuf;
18987c478bd9Sstevel@tonic-gate 	nso->so_rcvbuf = so->so_rcvbuf;
18997c478bd9Sstevel@tonic-gate 	if (nso->so_options & SO_LINGER)
19007c478bd9Sstevel@tonic-gate 		nso->so_linger = so->so_linger;
19017c478bd9Sstevel@tonic-gate 
19020f1702c5SYu Xiangning 	/*
19030f1702c5SYu Xiangning 	 * Note that the following sti_direct code path should be
19040f1702c5SYu Xiangning 	 * removed once we are confident that the direct sockets
19050f1702c5SYu Xiangning 	 * do not result in any degradation.
19060f1702c5SYu Xiangning 	 */
19070f1702c5SYu Xiangning 	if (sti->sti_direct) {
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate 		ASSERT(opt != NULL);
19107c478bd9Sstevel@tonic-gate 
19117c478bd9Sstevel@tonic-gate 		conn_res->OPT_length = optlen;
19127c478bd9Sstevel@tonic-gate 		conn_res->OPT_offset = MBLKL(mp);
19137c478bd9Sstevel@tonic-gate 		bcopy(&opt, mp->b_wptr, optlen);
19147c478bd9Sstevel@tonic-gate 		mp->b_wptr += optlen;
19157c478bd9Sstevel@tonic-gate 		conn_res->PRIM_type = T_CONN_RES;
19167c478bd9Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = 0;
19177c478bd9Sstevel@tonic-gate 		PRIM_type = T_CONN_RES;
19187c478bd9Sstevel@tonic-gate 
19197c478bd9Sstevel@tonic-gate 		/* Send down the T_CONN_RES on acceptor STREAM */
19207c478bd9Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(nso), mp, NULL,
19217c478bd9Sstevel@tonic-gate 		    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
19227c478bd9Sstevel@tonic-gate 		if (error) {
19237c478bd9Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
19247c478bd9Sstevel@tonic-gate 			so_lock_single(so);
19257c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
19267c478bd9Sstevel@tonic-gate 			goto disconnect_vp;
19277c478bd9Sstevel@tonic-gate 		}
19287c478bd9Sstevel@tonic-gate 		mutex_enter(&nso->so_lock);
19297c478bd9Sstevel@tonic-gate 		error = sowaitprim(nso, T_CONN_RES, T_OK_ACK,
19307c478bd9Sstevel@tonic-gate 		    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
19317c478bd9Sstevel@tonic-gate 		if (error) {
19327c478bd9Sstevel@tonic-gate 			mutex_exit(&nso->so_lock);
19337c478bd9Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
19347c478bd9Sstevel@tonic-gate 			so_lock_single(so);
19357c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
19367c478bd9Sstevel@tonic-gate 			goto disconnect_vp;
19377c478bd9Sstevel@tonic-gate 		}
19387c478bd9Sstevel@tonic-gate 		if (nso->so_family == AF_INET) {
19397c478bd9Sstevel@tonic-gate 			sin_t *sin;
19407c478bd9Sstevel@tonic-gate 
19417c478bd9Sstevel@tonic-gate 			sin = (sin_t *)(ack_mp->b_rptr +
19427c478bd9Sstevel@tonic-gate 			    sizeof (struct T_ok_ack));
19430f1702c5SYu Xiangning 			bcopy(sin, nsti->sti_laddr_sa, sizeof (sin_t));
19440f1702c5SYu Xiangning 			nsti->sti_laddr_len = sizeof (sin_t);
19457c478bd9Sstevel@tonic-gate 		} else {
19467c478bd9Sstevel@tonic-gate 			sin6_t *sin6;
19477c478bd9Sstevel@tonic-gate 
19487c478bd9Sstevel@tonic-gate 			sin6 = (sin6_t *)(ack_mp->b_rptr +
19497c478bd9Sstevel@tonic-gate 			    sizeof (struct T_ok_ack));
19500f1702c5SYu Xiangning 			bcopy(sin6, nsti->sti_laddr_sa, sizeof (sin6_t));
19510f1702c5SYu Xiangning 			nsti->sti_laddr_len = sizeof (sin6_t);
19527c478bd9Sstevel@tonic-gate 		}
19537c478bd9Sstevel@tonic-gate 		freemsg(ack_mp);
19547c478bd9Sstevel@tonic-gate 
19550f1702c5SYu Xiangning 		nso->so_state |= SS_ISCONNECTED;
19560f1702c5SYu Xiangning 		nso->so_proto_handle = (sock_lower_handle_t)opt;
19570f1702c5SYu Xiangning 		nsti->sti_laddr_valid = 1;
19587c478bd9Sstevel@tonic-gate 
19590f1702c5SYu Xiangning 		if (sti->sti_nl7c_flags & NL7C_ENABLED) {
19607c478bd9Sstevel@tonic-gate 			/*
19612c9e429eSbrutus 			 * A NL7C marked listen()er so the new socket
19622c9e429eSbrutus 			 * inherits the listen()er's NL7C state, except
19632c9e429eSbrutus 			 * for NL7C_POLLIN.
19647c478bd9Sstevel@tonic-gate 			 *
19652c9e429eSbrutus 			 * Only call NL7C to process the new socket if
19662c9e429eSbrutus 			 * the listen socket allows blocking i/o.
19677c478bd9Sstevel@tonic-gate 			 */
19680f1702c5SYu Xiangning 			nsti->sti_nl7c_flags =
19690f1702c5SYu Xiangning 			    sti->sti_nl7c_flags & (~NL7C_POLLIN);
19702c9e429eSbrutus 			if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) {
19712c9e429eSbrutus 				/*
19722c9e429eSbrutus 				 * Nonblocking accept() just make it
19732c9e429eSbrutus 				 * persist to defer processing to the
19742c9e429eSbrutus 				 * read-side syscall (e.g. read).
19752c9e429eSbrutus 				 */
19760f1702c5SYu Xiangning 				nsti->sti_nl7c_flags |= NL7C_SOPERSIST;
19772c9e429eSbrutus 			} else if (nl7c_process(nso, B_FALSE)) {
19787c478bd9Sstevel@tonic-gate 				/*
19797c478bd9Sstevel@tonic-gate 				 * NL7C has completed processing on the
19807c478bd9Sstevel@tonic-gate 				 * socket, close the socket and back to
19817c478bd9Sstevel@tonic-gate 				 * the top to await the next T_CONN_IND.
19827c478bd9Sstevel@tonic-gate 				 */
19837c478bd9Sstevel@tonic-gate 				mutex_exit(&nso->so_lock);
19847c478bd9Sstevel@tonic-gate 				(void) VOP_CLOSE(nvp, 0, 1, (offset_t)0,
1985de8c4a14SErik Nordmark 				    cr, NULL);
19867c478bd9Sstevel@tonic-gate 				VN_RELE(nvp);
19877c478bd9Sstevel@tonic-gate 				goto again;
19887c478bd9Sstevel@tonic-gate 			}
19897c478bd9Sstevel@tonic-gate 			/* Pass the new socket out */
19907c478bd9Sstevel@tonic-gate 		}
19917c478bd9Sstevel@tonic-gate 
19927c478bd9Sstevel@tonic-gate 		mutex_exit(&nso->so_lock);
19937c478bd9Sstevel@tonic-gate 
19947c478bd9Sstevel@tonic-gate 		/*
19957d6c035bSja97890 		 * It's possible, through the use of autopush for example,
19960f1702c5SYu Xiangning 		 * that the acceptor stream may not support sti_direct
19970f1702c5SYu Xiangning 		 * semantics. If the new socket does not support sti_direct
19987d6c035bSja97890 		 * we issue a _SIOCSOCKFALLBACK to inform the transport
19997d6c035bSja97890 		 * as we would in the I_PUSH case.
20007d6c035bSja97890 		 */
20010f1702c5SYu Xiangning 		if (nsti->sti_direct == 0) {
20027d6c035bSja97890 			int	rval;
20037d6c035bSja97890 
20047d6c035bSja97890 			if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
2005de8c4a14SErik Nordmark 			    0, 0, K_TO_K, cr, &rval)) != 0) {
20067d6c035bSja97890 				mutex_enter(&so->so_lock);
20077d6c035bSja97890 				so_lock_single(so);
20087d6c035bSja97890 				eprintsoline(so, error);
20097d6c035bSja97890 				goto disconnect_vp;
20107d6c035bSja97890 			}
20117d6c035bSja97890 		}
20127d6c035bSja97890 
20137d6c035bSja97890 		/*
20147c478bd9Sstevel@tonic-gate 		 * Pass out new socket.
20157c478bd9Sstevel@tonic-gate 		 */
20167c478bd9Sstevel@tonic-gate 		if (nsop != NULL)
20177c478bd9Sstevel@tonic-gate 			*nsop = nso;
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 		return (0);
20207c478bd9Sstevel@tonic-gate 	}
20217c478bd9Sstevel@tonic-gate 
20227c478bd9Sstevel@tonic-gate 	/*
20237c478bd9Sstevel@tonic-gate 	 * This is the non-performance case for sockets (e.g. AF_UNIX sockets)
20247c478bd9Sstevel@tonic-gate 	 * which don't support the FireEngine accept fast-path. It is also
20257c478bd9Sstevel@tonic-gate 	 * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd
20267c478bd9Sstevel@tonic-gate 	 * again. Neither sockfs nor TCP attempt to find out if some other
20277c478bd9Sstevel@tonic-gate 	 * random module has been inserted in between (in which case we
20287c478bd9Sstevel@tonic-gate 	 * should follow TLI accept behaviour). We blindly assume the worst
20297c478bd9Sstevel@tonic-gate 	 * case and revert back to old behaviour i.e. TCP will not send us
20307c478bd9Sstevel@tonic-gate 	 * any option (eager) and the accept should happen on the listener
20317c478bd9Sstevel@tonic-gate 	 * queue. Any queued T_conn_ind have already got their options removed
20327c478bd9Sstevel@tonic-gate 	 * by so_sock2_stream() when "sockmod" was I_POP'd.
20337c478bd9Sstevel@tonic-gate 	 */
20347c478bd9Sstevel@tonic-gate 	/*
20357c478bd9Sstevel@tonic-gate 	 * Fill in the {O_}T_CONN_RES before getting SOLOCKED.
20367c478bd9Sstevel@tonic-gate 	 */
20377c478bd9Sstevel@tonic-gate 	if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) {
20387c478bd9Sstevel@tonic-gate #ifdef	_ILP32
20397c478bd9Sstevel@tonic-gate 		queue_t	*q;
20407c478bd9Sstevel@tonic-gate 
20417c478bd9Sstevel@tonic-gate 		/*
20427c478bd9Sstevel@tonic-gate 		 * Find read queue in driver
20437c478bd9Sstevel@tonic-gate 		 * Can safely do this since we "own" nso/nvp.
20447c478bd9Sstevel@tonic-gate 		 */
20457c478bd9Sstevel@tonic-gate 		q = strvp2wq(nvp)->q_next;
20467c478bd9Sstevel@tonic-gate 		while (SAMESTR(q))
20477c478bd9Sstevel@tonic-gate 			q = q->q_next;
20487c478bd9Sstevel@tonic-gate 		q = RD(q);
20497c478bd9Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = (t_uscalar_t)q;
20507c478bd9Sstevel@tonic-gate #else
20517c478bd9Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev);
20527c478bd9Sstevel@tonic-gate #endif	/* _ILP32 */
20537c478bd9Sstevel@tonic-gate 		conn_res->PRIM_type = O_T_CONN_RES;
20547c478bd9Sstevel@tonic-gate 		PRIM_type = O_T_CONN_RES;
20557c478bd9Sstevel@tonic-gate 	} else {
20560f1702c5SYu Xiangning 		conn_res->ACCEPTOR_id = nsti->sti_acceptor_id;
20577c478bd9Sstevel@tonic-gate 		conn_res->PRIM_type = T_CONN_RES;
20587c478bd9Sstevel@tonic-gate 		PRIM_type = T_CONN_RES;
20597c478bd9Sstevel@tonic-gate 	}
20607c478bd9Sstevel@tonic-gate 	conn_res->SEQ_number = SEQ_number;
20617c478bd9Sstevel@tonic-gate 	conn_res->OPT_length = 0;
20627c478bd9Sstevel@tonic-gate 	conn_res->OPT_offset = 0;
20637c478bd9Sstevel@tonic-gate 
20647c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
20657c478bd9Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
20667c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL,
20697c478bd9Sstevel@tonic-gate 	    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
20707c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
20717c478bd9Sstevel@tonic-gate 	if (error) {
20727c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
20737c478bd9Sstevel@tonic-gate 		goto disconnect_vp;
20747c478bd9Sstevel@tonic-gate 	}
2075188658baSja97890 	error = sowaitprim(so, PRIM_type, T_OK_ACK,
2076188658baSja97890 	    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
20777c478bd9Sstevel@tonic-gate 	if (error) {
20787c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
20797c478bd9Sstevel@tonic-gate 		goto disconnect_vp;
20807c478bd9Sstevel@tonic-gate 	}
2081881776cfSGeorge Shepherd 	mutex_exit(&so->so_lock);
2082188658baSja97890 	/*
2083188658baSja97890 	 * If there is a sin/sin6 appended onto the T_OK_ACK use
2084188658baSja97890 	 * that to set the local address. If this is not present
2085188658baSja97890 	 * then we zero out the address and don't set the
20860f1702c5SYu Xiangning 	 * sti_laddr_valid bit. For AF_UNIX endpoints we copy over
20876e81d8daSja97890 	 * the pathname from the listening socket.
2088881776cfSGeorge Shepherd 	 * In the case where this is TCP or an AF_UNIX socket the
2089881776cfSGeorge Shepherd 	 * client side may have queued data or a T_ORDREL in the
2090881776cfSGeorge Shepherd 	 * transport. Having now sent the T_CONN_RES we may receive
2091881776cfSGeorge Shepherd 	 * those queued messages at any time. Hold the acceptor
2092881776cfSGeorge Shepherd 	 * so_lock until its state and laddr are finalized.
2093188658baSja97890 	 */
2094881776cfSGeorge Shepherd 	mutex_enter(&nso->so_lock);
2095188658baSja97890 	sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
2096188658baSja97890 	if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) &&
2097188658baSja97890 	    MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
2098188658baSja97890 		ack_mp->b_rptr += sizeof (struct T_ok_ack);
20990f1702c5SYu Xiangning 		bcopy(ack_mp->b_rptr, nsti->sti_laddr_sa, sinlen);
21000f1702c5SYu Xiangning 		nsti->sti_laddr_len = sinlen;
21010f1702c5SYu Xiangning 		nsti->sti_laddr_valid = 1;
21026e81d8daSja97890 	} else if (nso->so_family == AF_UNIX) {
21036e81d8daSja97890 		ASSERT(so->so_family == AF_UNIX);
21040f1702c5SYu Xiangning 		nsti->sti_laddr_len = sti->sti_laddr_len;
21050f1702c5SYu Xiangning 		ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
21060f1702c5SYu Xiangning 		bcopy(sti->sti_laddr_sa, nsti->sti_laddr_sa,
21070f1702c5SYu Xiangning 		    nsti->sti_laddr_len);
21080f1702c5SYu Xiangning 		nsti->sti_laddr_valid = 1;
2109188658baSja97890 	} else {
21100f1702c5SYu Xiangning 		nsti->sti_laddr_len = sti->sti_laddr_len;
21110f1702c5SYu Xiangning 		ASSERT(nsti->sti_laddr_len <= nsti->sti_laddr_maxlen);
21120f1702c5SYu Xiangning 		bzero(nsti->sti_laddr_sa, nsti->sti_addr_size);
21130f1702c5SYu Xiangning 		nsti->sti_laddr_sa->sa_family = nso->so_family;
2114188658baSja97890 	}
2115881776cfSGeorge Shepherd 	nso->so_state |= SS_ISCONNECTED;
2116881776cfSGeorge Shepherd 	mutex_exit(&nso->so_lock);
2117881776cfSGeorge Shepherd 
2118188658baSja97890 	freemsg(ack_mp);
2119188658baSja97890 
2120881776cfSGeorge Shepherd 	mutex_enter(&so->so_lock);
21217c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
21227c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate 	/*
21257c478bd9Sstevel@tonic-gate 	 * Pass out new socket.
21267c478bd9Sstevel@tonic-gate 	 */
21277c478bd9Sstevel@tonic-gate 	if (nsop != NULL)
21287c478bd9Sstevel@tonic-gate 		*nsop = nso;
21297c478bd9Sstevel@tonic-gate 
21307c478bd9Sstevel@tonic-gate 	return (0);
21317c478bd9Sstevel@tonic-gate 
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate eproto_disc_unl:
21347c478bd9Sstevel@tonic-gate 	error = EPROTO;
21357c478bd9Sstevel@tonic-gate e_disc_unl:
21367c478bd9Sstevel@tonic-gate 	eprintsoline(so, error);
21377c478bd9Sstevel@tonic-gate 	goto disconnect_unlocked;
21387c478bd9Sstevel@tonic-gate 
21397c478bd9Sstevel@tonic-gate pr_disc_vp_unl:
21407c478bd9Sstevel@tonic-gate 	eprintsoline(so, error);
21417c478bd9Sstevel@tonic-gate disconnect_vp_unlocked:
2142de8c4a14SErik Nordmark 	(void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
21437c478bd9Sstevel@tonic-gate 	VN_RELE(nvp);
21447c478bd9Sstevel@tonic-gate disconnect_unlocked:
21457c478bd9Sstevel@tonic-gate 	(void) sodisconnect(so, SEQ_number, 0);
21467c478bd9Sstevel@tonic-gate 	return (error);
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate pr_disc_vp:
21497c478bd9Sstevel@tonic-gate 	eprintsoline(so, error);
21507c478bd9Sstevel@tonic-gate disconnect_vp:
21517c478bd9Sstevel@tonic-gate 	(void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
21527c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
21537c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2154de8c4a14SErik Nordmark 	(void) VOP_CLOSE(nvp, 0, 1, 0, cr, NULL);
21557c478bd9Sstevel@tonic-gate 	VN_RELE(nvp);
21567c478bd9Sstevel@tonic-gate 	return (error);
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate conn_bad:	/* Note: SunOS 4/BSD unconditionally returns EINVAL here */
21597c478bd9Sstevel@tonic-gate 	error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
21607c478bd9Sstevel@tonic-gate 	    ? EOPNOTSUPP : EINVAL;
21617c478bd9Sstevel@tonic-gate e_bad:
21627c478bd9Sstevel@tonic-gate 	eprintsoline(so, error);
21637c478bd9Sstevel@tonic-gate 	return (error);
21647c478bd9Sstevel@tonic-gate }
21657c478bd9Sstevel@tonic-gate 
21667c478bd9Sstevel@tonic-gate /*
21677c478bd9Sstevel@tonic-gate  * connect a socket.
21687c478bd9Sstevel@tonic-gate  *
21697c478bd9Sstevel@tonic-gate  * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
21707c478bd9Sstevel@tonic-gate  * unconnect (by specifying a null address).
21717c478bd9Sstevel@tonic-gate  */
21727c478bd9Sstevel@tonic-gate int
21737c478bd9Sstevel@tonic-gate sotpi_connect(struct sonode *so,
21743e95bd4aSAnders Persson 	struct sockaddr *name,
21757c478bd9Sstevel@tonic-gate 	socklen_t namelen,
21767c478bd9Sstevel@tonic-gate 	int fflag,
21770f1702c5SYu Xiangning 	int flags,
21780f1702c5SYu Xiangning 	struct cred *cr)
21797c478bd9Sstevel@tonic-gate {
21807c478bd9Sstevel@tonic-gate 	struct T_conn_req	conn_req;
21817c478bd9Sstevel@tonic-gate 	int			error = 0;
21827c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
21837c478bd9Sstevel@tonic-gate 	void			*src;
21847c478bd9Sstevel@tonic-gate 	socklen_t		srclen;
21857c478bd9Sstevel@tonic-gate 	void			*addr;
21867c478bd9Sstevel@tonic-gate 	socklen_t		addrlen;
21877c478bd9Sstevel@tonic-gate 	boolean_t		need_unlock;
21880f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
2191903a11ebSrh87107 	    (void *)so, (void *)name, namelen, fflag, flags,
21927c478bd9Sstevel@tonic-gate 	    pr_state(so->so_state, so->so_mode)));
21937c478bd9Sstevel@tonic-gate 
21947c478bd9Sstevel@tonic-gate 	/*
21957c478bd9Sstevel@tonic-gate 	 * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
21967c478bd9Sstevel@tonic-gate 	 * avoid sleeping for memory with SOLOCKED held.
21970f1702c5SYu Xiangning 	 * We know that the T_CONN_REQ can't be larger than 2 * sti_faddr_maxlen
21987c478bd9Sstevel@tonic-gate 	 * + sizeof (struct T_opthdr).
21997c478bd9Sstevel@tonic-gate 	 * (the AF_UNIX so_ux_addr_xlate() does not make the address
22000f1702c5SYu Xiangning 	 * exceed sti_faddr_maxlen).
22017c478bd9Sstevel@tonic-gate 	 */
22027c478bd9Sstevel@tonic-gate 	mp = soallocproto(sizeof (struct T_conn_req) +
2203de8c4a14SErik Nordmark 	    2 * sti->sti_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR,
2204de8c4a14SErik Nordmark 	    cr);
22057c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
22067c478bd9Sstevel@tonic-gate 		/*
22077c478bd9Sstevel@tonic-gate 		 * Connect can not fail with ENOBUFS. A signal was
22087c478bd9Sstevel@tonic-gate 		 * caught so return EINTR.
22097c478bd9Sstevel@tonic-gate 		 */
22107c478bd9Sstevel@tonic-gate 		error = EINTR;
22117c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
22127c478bd9Sstevel@tonic-gate 		return (error);
22137c478bd9Sstevel@tonic-gate 	}
22147c478bd9Sstevel@tonic-gate 
22157c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
22167c478bd9Sstevel@tonic-gate 	/*
2217ba3431dfSjprakash 	 * Make sure there is a preallocated T_unbind_req message
2218ba3431dfSjprakash 	 * before any binding. This message is allocated when the
2219ba3431dfSjprakash 	 * socket is created. Since another thread can consume
2220ba3431dfSjprakash 	 * so_unbind_mp by the time we return from so_lock_single(),
2221ba3431dfSjprakash 	 * we should check the availability of so_unbind_mp after
2222ba3431dfSjprakash 	 * we return from so_lock_single().
22237c478bd9Sstevel@tonic-gate 	 */
2224ba3431dfSjprakash 
2225ba3431dfSjprakash 	so_lock_single(so);	/* Set SOLOCKED */
2226ba3431dfSjprakash 	need_unlock = B_TRUE;
2227ba3431dfSjprakash 
22280f1702c5SYu Xiangning 	if (sti->sti_unbind_mp == NULL) {
22297c478bd9Sstevel@tonic-gate 		dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
22307c478bd9Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
22310f1702c5SYu Xiangning 		sti->sti_unbind_mp =
2232de8c4a14SErik Nordmark 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR, cr);
22330f1702c5SYu Xiangning 		if (sti->sti_unbind_mp == NULL) {
22347c478bd9Sstevel@tonic-gate 			error = EINTR;
22357c478bd9Sstevel@tonic-gate 			goto done;
22367c478bd9Sstevel@tonic-gate 		}
22377c478bd9Sstevel@tonic-gate 	}
22387c478bd9Sstevel@tonic-gate 
22397c478bd9Sstevel@tonic-gate 	/*
22407c478bd9Sstevel@tonic-gate 	 * Can't have done a listen before connecting.
22417c478bd9Sstevel@tonic-gate 	 */
22427c478bd9Sstevel@tonic-gate 	if (so->so_state & SS_ACCEPTCONN) {
22437c478bd9Sstevel@tonic-gate 		error = EOPNOTSUPP;
22447c478bd9Sstevel@tonic-gate 		goto done;
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate 
22477c478bd9Sstevel@tonic-gate 	/*
22487c478bd9Sstevel@tonic-gate 	 * Must be bound with the transport
22497c478bd9Sstevel@tonic-gate 	 */
22507c478bd9Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
22517c478bd9Sstevel@tonic-gate 		if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
22527c478bd9Sstevel@tonic-gate 		    /*CONSTCOND*/
22537c478bd9Sstevel@tonic-gate 		    so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
22547c478bd9Sstevel@tonic-gate 			/*
22557c478bd9Sstevel@tonic-gate 			 * Optimization for AF_INET{,6} transports
22567c478bd9Sstevel@tonic-gate 			 * that can handle a T_CONN_REQ without being bound.
22577c478bd9Sstevel@tonic-gate 			 */
22587c478bd9Sstevel@tonic-gate 			so_automatic_bind(so);
22597c478bd9Sstevel@tonic-gate 		} else {
22607c478bd9Sstevel@tonic-gate 			error = sotpi_bind(so, NULL, 0,
22610f1702c5SYu Xiangning 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
22627c478bd9Sstevel@tonic-gate 			if (error)
22637c478bd9Sstevel@tonic-gate 				goto done;
22647c478bd9Sstevel@tonic-gate 		}
22657c478bd9Sstevel@tonic-gate 		ASSERT(so->so_state & SS_ISBOUND);
22667c478bd9Sstevel@tonic-gate 		flags |= _SOCONNECT_DID_BIND;
22677c478bd9Sstevel@tonic-gate 	}
22687c478bd9Sstevel@tonic-gate 
22697c478bd9Sstevel@tonic-gate 	/*
22707c478bd9Sstevel@tonic-gate 	 * Handle a connect to a name parameter of type AF_UNSPEC like a
22717c478bd9Sstevel@tonic-gate 	 * connect to a null address. This is the portable method to
22727c478bd9Sstevel@tonic-gate 	 * unconnect a socket.
22737c478bd9Sstevel@tonic-gate 	 */
22747c478bd9Sstevel@tonic-gate 	if ((namelen >= sizeof (sa_family_t)) &&
22757c478bd9Sstevel@tonic-gate 	    (name->sa_family == AF_UNSPEC)) {
22767c478bd9Sstevel@tonic-gate 		name = NULL;
22777c478bd9Sstevel@tonic-gate 		namelen = 0;
22787c478bd9Sstevel@tonic-gate 	}
22797c478bd9Sstevel@tonic-gate 
22807c478bd9Sstevel@tonic-gate 	/*
22817c478bd9Sstevel@tonic-gate 	 * Check that we are not already connected.
22827c478bd9Sstevel@tonic-gate 	 * A connection-oriented socket cannot be reconnected.
22837c478bd9Sstevel@tonic-gate 	 * A connected connection-less socket can be
22847c478bd9Sstevel@tonic-gate 	 * - connected to a different address by a subsequent connect
22857c478bd9Sstevel@tonic-gate 	 * - "unconnected" by a connect to the NULL address
22867c478bd9Sstevel@tonic-gate 	 */
22877c478bd9Sstevel@tonic-gate 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
22887c478bd9Sstevel@tonic-gate 		ASSERT(!(flags & _SOCONNECT_DID_BIND));
22897c478bd9Sstevel@tonic-gate 		if (so->so_mode & SM_CONNREQUIRED) {
22907c478bd9Sstevel@tonic-gate 			/* Connection-oriented socket */
22917c478bd9Sstevel@tonic-gate 			error = so->so_state & SS_ISCONNECTED ?
22927c478bd9Sstevel@tonic-gate 			    EISCONN : EALREADY;
22937c478bd9Sstevel@tonic-gate 			goto done;
22947c478bd9Sstevel@tonic-gate 		}
22957c478bd9Sstevel@tonic-gate 		/* Connection-less socket */
22967c478bd9Sstevel@tonic-gate 		if (name == NULL) {
22977c478bd9Sstevel@tonic-gate 			/*
22987c478bd9Sstevel@tonic-gate 			 * Remove the connected state and clear SO_DGRAM_ERRIND
22997c478bd9Sstevel@tonic-gate 			 * since it was set when the socket was connected.
23007c478bd9Sstevel@tonic-gate 			 * If this is UDP also send down a T_DISCON_REQ.
23017c478bd9Sstevel@tonic-gate 			 */
23027c478bd9Sstevel@tonic-gate 			int val;
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate 			if ((so->so_family == AF_INET ||
23057c478bd9Sstevel@tonic-gate 			    so->so_family == AF_INET6) &&
23067c478bd9Sstevel@tonic-gate 			    (so->so_type == SOCK_DGRAM ||
23077c478bd9Sstevel@tonic-gate 			    so->so_type == SOCK_RAW) &&
23087c478bd9Sstevel@tonic-gate 			    /*CONSTCOND*/
23097c478bd9Sstevel@tonic-gate 			    !soconnect_tpi_udp) {
23107c478bd9Sstevel@tonic-gate 				/* XXX What about implicitly unbinding here? */
23117c478bd9Sstevel@tonic-gate 				error = sodisconnect(so, -1,
23127c478bd9Sstevel@tonic-gate 				    _SODISCONNECT_LOCK_HELD);
23137c478bd9Sstevel@tonic-gate 			} else {
23147c478bd9Sstevel@tonic-gate 				so->so_state &=
23150f1702c5SYu Xiangning 				    ~(SS_ISCONNECTED | SS_ISCONNECTING);
23160f1702c5SYu Xiangning 				sti->sti_faddr_valid = 0;
23170f1702c5SYu Xiangning 				sti->sti_faddr_len = 0;
23187c478bd9Sstevel@tonic-gate 			}
23197c478bd9Sstevel@tonic-gate 
23200f1702c5SYu Xiangning 			/* Remove SOLOCKED since setsockopt will grab it */
23217c478bd9Sstevel@tonic-gate 			so_unlock_single(so, SOLOCKED);
23227c478bd9Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate 			val = 0;
23250f1702c5SYu Xiangning 			(void) sotpi_setsockopt(so, SOL_SOCKET,
23260f1702c5SYu Xiangning 			    SO_DGRAM_ERRIND, &val, (t_uscalar_t)sizeof (val),
23270f1702c5SYu Xiangning 			    cr);
23287c478bd9Sstevel@tonic-gate 
23297c478bd9Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
23307c478bd9Sstevel@tonic-gate 			so_lock_single(so);	/* Set SOLOCKED */
23317c478bd9Sstevel@tonic-gate 			goto done;
23327c478bd9Sstevel@tonic-gate 		}
23337c478bd9Sstevel@tonic-gate 	}
23347c478bd9Sstevel@tonic-gate 	ASSERT(so->so_state & SS_ISBOUND);
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate 	if (name == NULL || namelen == 0) {
23377c478bd9Sstevel@tonic-gate 		error = EINVAL;
23387c478bd9Sstevel@tonic-gate 		goto done;
23397c478bd9Sstevel@tonic-gate 	}
23407c478bd9Sstevel@tonic-gate 	/*
23410f1702c5SYu Xiangning 	 * Mark the socket if sti_faddr_sa represents the transport level
23427c478bd9Sstevel@tonic-gate 	 * address.
23437c478bd9Sstevel@tonic-gate 	 */
23447c478bd9Sstevel@tonic-gate 	if (flags & _SOCONNECT_NOXLATE) {
23457c478bd9Sstevel@tonic-gate 		struct sockaddr_ux	*soaddr_ux;
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate 		ASSERT(so->so_family == AF_UNIX);
23487c478bd9Sstevel@tonic-gate 		if (namelen != sizeof (struct sockaddr_ux)) {
23497c478bd9Sstevel@tonic-gate 			error = EINVAL;
23507c478bd9Sstevel@tonic-gate 			goto done;
23517c478bd9Sstevel@tonic-gate 		}
23527c478bd9Sstevel@tonic-gate 		soaddr_ux = (struct sockaddr_ux *)name;
23537c478bd9Sstevel@tonic-gate 		name = (struct sockaddr *)&soaddr_ux->sou_addr;
23547c478bd9Sstevel@tonic-gate 		namelen = sizeof (soaddr_ux->sou_addr);
23550f1702c5SYu Xiangning 		sti->sti_faddr_noxlate = 1;
23567c478bd9Sstevel@tonic-gate 	}
23577c478bd9Sstevel@tonic-gate 
23587c478bd9Sstevel@tonic-gate 	/*
23597c478bd9Sstevel@tonic-gate 	 * Length and family checks.
23607c478bd9Sstevel@tonic-gate 	 */
23617c478bd9Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
23627c478bd9Sstevel@tonic-gate 	if (error)
23637c478bd9Sstevel@tonic-gate 		goto bad;
23647c478bd9Sstevel@tonic-gate 
23657c478bd9Sstevel@tonic-gate 	/*
23667c478bd9Sstevel@tonic-gate 	 * Save foreign address. Needed for AF_UNIX as well as
23677c478bd9Sstevel@tonic-gate 	 * transport providers that do not support TI_GETPEERNAME.
23687c478bd9Sstevel@tonic-gate 	 * Also used for cached foreign address for TCP and UDP.
23697c478bd9Sstevel@tonic-gate 	 */
23700f1702c5SYu Xiangning 	if (namelen > (t_uscalar_t)sti->sti_faddr_maxlen) {
23717c478bd9Sstevel@tonic-gate 		error = EINVAL;
23727c478bd9Sstevel@tonic-gate 		goto done;
23737c478bd9Sstevel@tonic-gate 	}
23740f1702c5SYu Xiangning 	sti->sti_faddr_len = (socklen_t)namelen;
23750f1702c5SYu Xiangning 	ASSERT(sti->sti_faddr_len <= sti->sti_faddr_maxlen);
23760f1702c5SYu Xiangning 	bcopy(name, sti->sti_faddr_sa, namelen);
23770f1702c5SYu Xiangning 	sti->sti_faddr_valid = 1;
23787c478bd9Sstevel@tonic-gate 
23797c478bd9Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
23800f1702c5SYu Xiangning 		if (sti->sti_faddr_noxlate) {
23817c478bd9Sstevel@tonic-gate 			/*
23827c478bd9Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
23837c478bd9Sstevel@tonic-gate 			 * pass any (transport internal) source address.
23847c478bd9Sstevel@tonic-gate 			 */
23850f1702c5SYu Xiangning 			addr = sti->sti_faddr_sa;
23860f1702c5SYu Xiangning 			addrlen = (t_uscalar_t)sti->sti_faddr_len;
23877c478bd9Sstevel@tonic-gate 			src = NULL;
23887c478bd9Sstevel@tonic-gate 			srclen = 0;
23897c478bd9Sstevel@tonic-gate 		} else {
23907c478bd9Sstevel@tonic-gate 			/*
23917c478bd9Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
23927c478bd9Sstevel@tonic-gate 			 * and translate the remote address.
23930f1702c5SYu Xiangning 			 * Holding so_lock thus sti_laddr_sa can not change.
23947c478bd9Sstevel@tonic-gate 			 */
23950f1702c5SYu Xiangning 			src = sti->sti_laddr_sa;
23960f1702c5SYu Xiangning 			srclen = (t_uscalar_t)sti->sti_laddr_len;
23977c478bd9Sstevel@tonic-gate 			dprintso(so, 1,
23987c478bd9Sstevel@tonic-gate 			    ("sotpi_connect UNIX: srclen %d, src %p\n",
23997c478bd9Sstevel@tonic-gate 			    srclen, src));
24007c478bd9Sstevel@tonic-gate 			error = so_ux_addr_xlate(so,
24010f1702c5SYu Xiangning 			    sti->sti_faddr_sa, (socklen_t)sti->sti_faddr_len,
24027c478bd9Sstevel@tonic-gate 			    (flags & _SOCONNECT_XPG4_2),
24037c478bd9Sstevel@tonic-gate 			    &addr, &addrlen);
24047c478bd9Sstevel@tonic-gate 			if (error)
24057c478bd9Sstevel@tonic-gate 				goto bad;
24067c478bd9Sstevel@tonic-gate 		}
24077c478bd9Sstevel@tonic-gate 	} else {
24080f1702c5SYu Xiangning 		addr = sti->sti_faddr_sa;
24090f1702c5SYu Xiangning 		addrlen = (t_uscalar_t)sti->sti_faddr_len;
24107c478bd9Sstevel@tonic-gate 		src = NULL;
24117c478bd9Sstevel@tonic-gate 		srclen = 0;
24127c478bd9Sstevel@tonic-gate 	}
24137c478bd9Sstevel@tonic-gate 	/*
24147c478bd9Sstevel@tonic-gate 	 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
24157c478bd9Sstevel@tonic-gate 	 * option which asks the transport provider to send T_UDERR_IND
24167c478bd9Sstevel@tonic-gate 	 * messages. These T_UDERR_IND messages are used to return connected
24177c478bd9Sstevel@tonic-gate 	 * style errors (e.g. ECONNRESET) for connected datagram sockets.
24187c478bd9Sstevel@tonic-gate 	 *
24197c478bd9Sstevel@tonic-gate 	 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
24207c478bd9Sstevel@tonic-gate 	 * we send down a T_CONN_REQ. This is needed to let the
24217c478bd9Sstevel@tonic-gate 	 * transport assign a local address that is consistent with
24227c478bd9Sstevel@tonic-gate 	 * the remote address. Applications depend on a getsockname()
24237c478bd9Sstevel@tonic-gate 	 * after a connect() to retrieve the "source" IP address for
24247c478bd9Sstevel@tonic-gate 	 * the connected socket.  Invalidate the cached local address
24257c478bd9Sstevel@tonic-gate 	 * to force getsockname() to enquire of the transport.
24267c478bd9Sstevel@tonic-gate 	 */
24277c478bd9Sstevel@tonic-gate 	if (!(so->so_mode & SM_CONNREQUIRED)) {
24287c478bd9Sstevel@tonic-gate 		/*
24297c478bd9Sstevel@tonic-gate 		 * Datagram socket.
24307c478bd9Sstevel@tonic-gate 		 */
24317c478bd9Sstevel@tonic-gate 		int32_t val;
24327c478bd9Sstevel@tonic-gate 
24337c478bd9Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
24347c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
24357c478bd9Sstevel@tonic-gate 
24367c478bd9Sstevel@tonic-gate 		val = 1;
24377c478bd9Sstevel@tonic-gate 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
24380f1702c5SYu Xiangning 		    &val, (t_uscalar_t)sizeof (val), cr);
24397c478bd9Sstevel@tonic-gate 
24407c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24417c478bd9Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
24427c478bd9Sstevel@tonic-gate 		if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
24437c478bd9Sstevel@tonic-gate 		    (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
24447c478bd9Sstevel@tonic-gate 		    soconnect_tpi_udp) {
24457c478bd9Sstevel@tonic-gate 			soisconnected(so);
24467c478bd9Sstevel@tonic-gate 			goto done;
24477c478bd9Sstevel@tonic-gate 		}
24487c478bd9Sstevel@tonic-gate 		/*
24497c478bd9Sstevel@tonic-gate 		 * Send down T_CONN_REQ etc.
24507c478bd9Sstevel@tonic-gate 		 * Clear fflag to avoid returning EWOULDBLOCK.
24517c478bd9Sstevel@tonic-gate 		 */
24527c478bd9Sstevel@tonic-gate 		fflag = 0;
24537c478bd9Sstevel@tonic-gate 		ASSERT(so->so_family != AF_UNIX);
24540f1702c5SYu Xiangning 		sti->sti_laddr_valid = 0;
24550f1702c5SYu Xiangning 	} else if (sti->sti_laddr_len != 0) {
24567c478bd9Sstevel@tonic-gate 		/*
24577c478bd9Sstevel@tonic-gate 		 * If the local address or port was "any" then it may be
24587c478bd9Sstevel@tonic-gate 		 * changed by the transport as a result of the
24597c478bd9Sstevel@tonic-gate 		 * connect.  Invalidate the cached version if we have one.
24607c478bd9Sstevel@tonic-gate 		 */
24617c478bd9Sstevel@tonic-gate 		switch (so->so_family) {
24627c478bd9Sstevel@tonic-gate 		case AF_INET:
24630f1702c5SYu Xiangning 			ASSERT(sti->sti_laddr_len == (socklen_t)sizeof (sin_t));
24640f1702c5SYu Xiangning 			if (((sin_t *)sti->sti_laddr_sa)->sin_addr.s_addr ==
24657c478bd9Sstevel@tonic-gate 			    INADDR_ANY ||
24660f1702c5SYu Xiangning 			    ((sin_t *)sti->sti_laddr_sa)->sin_port == 0)
24670f1702c5SYu Xiangning 				sti->sti_laddr_valid = 0;
24687c478bd9Sstevel@tonic-gate 			break;
24697c478bd9Sstevel@tonic-gate 
24707c478bd9Sstevel@tonic-gate 		case AF_INET6:
24710f1702c5SYu Xiangning 			ASSERT(sti->sti_laddr_len ==
24720f1702c5SYu Xiangning 			    (socklen_t)sizeof (sin6_t));
24737c478bd9Sstevel@tonic-gate 			if (IN6_IS_ADDR_UNSPECIFIED(
24740f1702c5SYu Xiangning 			    &((sin6_t *)sti->sti_laddr_sa) ->sin6_addr) ||
24757c478bd9Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED_ANY(
24760f1702c5SYu Xiangning 			    &((sin6_t *)sti->sti_laddr_sa)->sin6_addr) ||
24770f1702c5SYu Xiangning 			    ((sin6_t *)sti->sti_laddr_sa)->sin6_port == 0)
24780f1702c5SYu Xiangning 				sti->sti_laddr_valid = 0;
24797c478bd9Sstevel@tonic-gate 			break;
24807c478bd9Sstevel@tonic-gate 
24817c478bd9Sstevel@tonic-gate 		default:
24827c478bd9Sstevel@tonic-gate 			break;
24837c478bd9Sstevel@tonic-gate 		}
24847c478bd9Sstevel@tonic-gate 	}
24857c478bd9Sstevel@tonic-gate 
24867c478bd9Sstevel@tonic-gate 	/*
24877c478bd9Sstevel@tonic-gate 	 * Check for failure of an earlier call
24887c478bd9Sstevel@tonic-gate 	 */
24897c478bd9Sstevel@tonic-gate 	if (so->so_error != 0)
24907c478bd9Sstevel@tonic-gate 		goto so_bad;
24917c478bd9Sstevel@tonic-gate 
24927c478bd9Sstevel@tonic-gate 	/*
24937c478bd9Sstevel@tonic-gate 	 * Send down T_CONN_REQ. Message was allocated above.
24947c478bd9Sstevel@tonic-gate 	 */
24957c478bd9Sstevel@tonic-gate 	conn_req.PRIM_type = T_CONN_REQ;
24967c478bd9Sstevel@tonic-gate 	conn_req.DEST_length = addrlen;
24977c478bd9Sstevel@tonic-gate 	conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
24987c478bd9Sstevel@tonic-gate 	if (srclen == 0) {
24997c478bd9Sstevel@tonic-gate 		conn_req.OPT_length = 0;
25007c478bd9Sstevel@tonic-gate 		conn_req.OPT_offset = 0;
25017c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &conn_req, sizeof (conn_req));
25027c478bd9Sstevel@tonic-gate 		soappendmsg(mp, addr, addrlen);
25037c478bd9Sstevel@tonic-gate 	} else {
25047c478bd9Sstevel@tonic-gate 		/*
25057c478bd9Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
25067c478bd9Sstevel@tonic-gate 		 * address option.
25077c478bd9Sstevel@tonic-gate 		 */
25087c478bd9Sstevel@tonic-gate 		struct T_opthdr toh;
25097c478bd9Sstevel@tonic-gate 
25107c478bd9Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
25117c478bd9Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
25127c478bd9Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
25137c478bd9Sstevel@tonic-gate 		toh.status = 0;
25147c478bd9Sstevel@tonic-gate 		conn_req.OPT_length =
25157c478bd9Sstevel@tonic-gate 		    (t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
25167c478bd9Sstevel@tonic-gate 		conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
25177c478bd9Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(addrlen));
25187c478bd9Sstevel@tonic-gate 
25197c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &conn_req, sizeof (conn_req));
25207c478bd9Sstevel@tonic-gate 		soappendmsg(mp, addr, addrlen);
25217c478bd9Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
25227c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
25237c478bd9Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
25247c478bd9Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
25257c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
25267c478bd9Sstevel@tonic-gate 	}
25277c478bd9Sstevel@tonic-gate 	/*
25287c478bd9Sstevel@tonic-gate 	 * Set SS_ISCONNECTING before sending down the T_CONN_REQ
25297c478bd9Sstevel@tonic-gate 	 * in order to have the right state when the T_CONN_CON shows up.
25307c478bd9Sstevel@tonic-gate 	 */
25317c478bd9Sstevel@tonic-gate 	soisconnecting(so);
25327c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
25337c478bd9Sstevel@tonic-gate 
2534005d3febSMarek Pospisil 	if (AU_AUDITING())
25357c478bd9Sstevel@tonic-gate 		audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
25367c478bd9Sstevel@tonic-gate 
25377c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
25387c478bd9Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
25397c478bd9Sstevel@tonic-gate 	mp = NULL;
25407c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
25417c478bd9Sstevel@tonic-gate 	if (error != 0)
25427c478bd9Sstevel@tonic-gate 		goto bad;
25437c478bd9Sstevel@tonic-gate 
25447c478bd9Sstevel@tonic-gate 	if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
25457c478bd9Sstevel@tonic-gate 		goto bad;
25467c478bd9Sstevel@tonic-gate 
25477c478bd9Sstevel@tonic-gate 	/* Allow other threads to access the socket */
25487c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
25497c478bd9Sstevel@tonic-gate 	need_unlock = B_FALSE;
25507c478bd9Sstevel@tonic-gate 
25517c478bd9Sstevel@tonic-gate 	/*
25527c478bd9Sstevel@tonic-gate 	 * Wait until we get a T_CONN_CON or an error
25537c478bd9Sstevel@tonic-gate 	 */
25547c478bd9Sstevel@tonic-gate 	if ((error = sowaitconnected(so, fflag, 0)) != 0) {
25557c478bd9Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
25567c478bd9Sstevel@tonic-gate 		need_unlock = B_TRUE;
25577c478bd9Sstevel@tonic-gate 	}
25587c478bd9Sstevel@tonic-gate 
25597c478bd9Sstevel@tonic-gate done:
25607c478bd9Sstevel@tonic-gate 	freemsg(mp);
25617c478bd9Sstevel@tonic-gate 	switch (error) {
25627c478bd9Sstevel@tonic-gate 	case EINPROGRESS:
25637c478bd9Sstevel@tonic-gate 	case EALREADY:
25647c478bd9Sstevel@tonic-gate 	case EISCONN:
25657c478bd9Sstevel@tonic-gate 	case EINTR:
25667c478bd9Sstevel@tonic-gate 		/* Non-fatal errors */
25670f1702c5SYu Xiangning 		sti->sti_laddr_valid = 0;
25687c478bd9Sstevel@tonic-gate 		/* FALLTHRU */
25697c478bd9Sstevel@tonic-gate 	case 0:
25707c478bd9Sstevel@tonic-gate 		break;
25717c478bd9Sstevel@tonic-gate 	default:
25727c478bd9Sstevel@tonic-gate 		ASSERT(need_unlock);
25737c478bd9Sstevel@tonic-gate 		/*
25747c478bd9Sstevel@tonic-gate 		 * Fatal errors: clear SS_ISCONNECTING in case it was set,
25757c478bd9Sstevel@tonic-gate 		 * and invalidate local-address cache
25767c478bd9Sstevel@tonic-gate 		 */
25770f1702c5SYu Xiangning 		so->so_state &= ~SS_ISCONNECTING;
25780f1702c5SYu Xiangning 		sti->sti_laddr_valid = 0;
25797c478bd9Sstevel@tonic-gate 		/* A discon_ind might have already unbound us */
25807c478bd9Sstevel@tonic-gate 		if ((flags & _SOCONNECT_DID_BIND) &&
25817c478bd9Sstevel@tonic-gate 		    (so->so_state & SS_ISBOUND)) {
25827c478bd9Sstevel@tonic-gate 			int err;
25837c478bd9Sstevel@tonic-gate 
25847c478bd9Sstevel@tonic-gate 			err = sotpi_unbind(so, 0);
25857c478bd9Sstevel@tonic-gate 			/* LINTED - statement has no conseq */
25867c478bd9Sstevel@tonic-gate 			if (err) {
25877c478bd9Sstevel@tonic-gate 				eprintsoline(so, err);
25887c478bd9Sstevel@tonic-gate 			}
25897c478bd9Sstevel@tonic-gate 		}
25907c478bd9Sstevel@tonic-gate 		break;
25917c478bd9Sstevel@tonic-gate 	}
25927c478bd9Sstevel@tonic-gate 	if (need_unlock)
25937c478bd9Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
25947c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
25957c478bd9Sstevel@tonic-gate 	return (error);
25967c478bd9Sstevel@tonic-gate 
25970f1702c5SYu Xiangning so_bad:	error = sogeterr(so, B_TRUE);
25987c478bd9Sstevel@tonic-gate bad:	eprintsoline(so, error);
25997c478bd9Sstevel@tonic-gate 	goto done;
26007c478bd9Sstevel@tonic-gate }
26017c478bd9Sstevel@tonic-gate 
26020f1702c5SYu Xiangning /* ARGSUSED */
26037c478bd9Sstevel@tonic-gate int
26040f1702c5SYu Xiangning sotpi_shutdown(struct sonode *so, int how, struct cred *cr)
26057c478bd9Sstevel@tonic-gate {
26067c478bd9Sstevel@tonic-gate 	struct T_ordrel_req	ordrel_req;
26077c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
26087c478bd9Sstevel@tonic-gate 	uint_t			old_state, state_change;
26097c478bd9Sstevel@tonic-gate 	int			error = 0;
26100f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
26117c478bd9Sstevel@tonic-gate 
26127c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
2613903a11ebSrh87107 	    (void *)so, how, pr_state(so->so_state, so->so_mode)));
26147c478bd9Sstevel@tonic-gate 
26157c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
26167c478bd9Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
26177c478bd9Sstevel@tonic-gate 
26187c478bd9Sstevel@tonic-gate 	/*
26197c478bd9Sstevel@tonic-gate 	 * SunOS 4.X has no check for datagram sockets.
26207c478bd9Sstevel@tonic-gate 	 * 5.X checks that it is connected (ENOTCONN)
26217c478bd9Sstevel@tonic-gate 	 * X/Open requires that we check the connected state.
26227c478bd9Sstevel@tonic-gate 	 */
26237c478bd9Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
26247c478bd9Sstevel@tonic-gate 		if (!xnet_skip_checks) {
26257c478bd9Sstevel@tonic-gate 			error = ENOTCONN;
26267c478bd9Sstevel@tonic-gate 			if (xnet_check_print) {
26277c478bd9Sstevel@tonic-gate 				printf("sockfs: X/Open shutdown check "
26287c478bd9Sstevel@tonic-gate 				    "caused ENOTCONN\n");
26297c478bd9Sstevel@tonic-gate 			}
26307c478bd9Sstevel@tonic-gate 		}
26317c478bd9Sstevel@tonic-gate 		goto done;
26327c478bd9Sstevel@tonic-gate 	}
26337c478bd9Sstevel@tonic-gate 	/*
26347c478bd9Sstevel@tonic-gate 	 * Record the current state and then perform any state changes.
26357c478bd9Sstevel@tonic-gate 	 * Then use the difference between the old and new states to
26367c478bd9Sstevel@tonic-gate 	 * determine which messages need to be sent.
26377c478bd9Sstevel@tonic-gate 	 * This prevents e.g. duplicate T_ORDREL_REQ when there are
26387c478bd9Sstevel@tonic-gate 	 * duplicate calls to shutdown().
26397c478bd9Sstevel@tonic-gate 	 */
26407c478bd9Sstevel@tonic-gate 	old_state = so->so_state;
26417c478bd9Sstevel@tonic-gate 
26427c478bd9Sstevel@tonic-gate 	switch (how) {
26437c478bd9Sstevel@tonic-gate 	case 0:
26447c478bd9Sstevel@tonic-gate 		socantrcvmore(so);
26457c478bd9Sstevel@tonic-gate 		break;
26467c478bd9Sstevel@tonic-gate 	case 1:
26477c478bd9Sstevel@tonic-gate 		socantsendmore(so);
26487c478bd9Sstevel@tonic-gate 		break;
26497c478bd9Sstevel@tonic-gate 	case 2:
26507c478bd9Sstevel@tonic-gate 		socantsendmore(so);
26517c478bd9Sstevel@tonic-gate 		socantrcvmore(so);
26527c478bd9Sstevel@tonic-gate 		break;
26537c478bd9Sstevel@tonic-gate 	default:
26547c478bd9Sstevel@tonic-gate 		error = EINVAL;
26557c478bd9Sstevel@tonic-gate 		goto done;
26567c478bd9Sstevel@tonic-gate 	}
26577c478bd9Sstevel@tonic-gate 
26587c478bd9Sstevel@tonic-gate 	/*
26597c478bd9Sstevel@tonic-gate 	 * Assumes that the SS_CANT* flags are never cleared in the above code.
26607c478bd9Sstevel@tonic-gate 	 */
26617c478bd9Sstevel@tonic-gate 	state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
26627c478bd9Sstevel@tonic-gate 	    (old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
26637c478bd9Sstevel@tonic-gate 	ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
26647c478bd9Sstevel@tonic-gate 
26657c478bd9Sstevel@tonic-gate 	switch (state_change) {
26667c478bd9Sstevel@tonic-gate 	case 0:
26677c478bd9Sstevel@tonic-gate 		dprintso(so, 1,
26687c478bd9Sstevel@tonic-gate 		    ("sotpi_shutdown: nothing to send in state 0x%x\n",
26697c478bd9Sstevel@tonic-gate 		    so->so_state));
26707c478bd9Sstevel@tonic-gate 		goto done;
26717c478bd9Sstevel@tonic-gate 
26727c478bd9Sstevel@tonic-gate 	case SS_CANTRCVMORE:
26737c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
26747c478bd9Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
26757c478bd9Sstevel@tonic-gate 		/*
26767c478bd9Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
26777c478bd9Sstevel@tonic-gate 		 * pollwakeups, and signals.
26787c478bd9Sstevel@tonic-gate 		 */
26797c478bd9Sstevel@tonic-gate 		/*
26807c478bd9Sstevel@tonic-gate 		 * Get the read lock before flushing data to avoid problems
26817c478bd9Sstevel@tonic-gate 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
26827c478bd9Sstevel@tonic-gate 		 */
26837c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
26847c478bd9Sstevel@tonic-gate 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
26857c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
26867c478bd9Sstevel@tonic-gate 
26877c478bd9Sstevel@tonic-gate 		/* Flush read side queue */
26887c478bd9Sstevel@tonic-gate 		strflushrq(SOTOV(so), FLUSHALL);
26897c478bd9Sstevel@tonic-gate 
26907c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
26917c478bd9Sstevel@tonic-gate 		so_unlock_read(so);		/* Clear SOREADLOCKED */
26927c478bd9Sstevel@tonic-gate 		break;
26937c478bd9Sstevel@tonic-gate 
26947c478bd9Sstevel@tonic-gate 	case SS_CANTSENDMORE:
26957c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
26967c478bd9Sstevel@tonic-gate 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
26977c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
26987c478bd9Sstevel@tonic-gate 		break;
26997c478bd9Sstevel@tonic-gate 
27007c478bd9Sstevel@tonic-gate 	case SS_CANTSENDMORE|SS_CANTRCVMORE:
27017c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27027c478bd9Sstevel@tonic-gate 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
27037c478bd9Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
27047c478bd9Sstevel@tonic-gate 		/*
27057c478bd9Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
27067c478bd9Sstevel@tonic-gate 		 * pollwakeups, and signals.
27077c478bd9Sstevel@tonic-gate 		 */
27087c478bd9Sstevel@tonic-gate 		/*
27097c478bd9Sstevel@tonic-gate 		 * Get the read lock before flushing data to avoid problems
27107c478bd9Sstevel@tonic-gate 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
27117c478bd9Sstevel@tonic-gate 		 */
27127c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27137c478bd9Sstevel@tonic-gate 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
27147c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27157c478bd9Sstevel@tonic-gate 
27167c478bd9Sstevel@tonic-gate 		/* Flush read side queue */
27177c478bd9Sstevel@tonic-gate 		strflushrq(SOTOV(so), FLUSHALL);
27187c478bd9Sstevel@tonic-gate 
27197c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27207c478bd9Sstevel@tonic-gate 		so_unlock_read(so);		/* Clear SOREADLOCKED */
27217c478bd9Sstevel@tonic-gate 		break;
27227c478bd9Sstevel@tonic-gate 	}
27237c478bd9Sstevel@tonic-gate 
27247c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
27257c478bd9Sstevel@tonic-gate 
27267c478bd9Sstevel@tonic-gate 	/*
27277c478bd9Sstevel@tonic-gate 	 * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
27287c478bd9Sstevel@tonic-gate 	 * was set due to this call and the new state has both of them set:
27297c478bd9Sstevel@tonic-gate 	 *	Send the AF_UNIX close indication
27307c478bd9Sstevel@tonic-gate 	 *	For T_COTS send a discon_ind
27317c478bd9Sstevel@tonic-gate 	 *
27327c478bd9Sstevel@tonic-gate 	 * If cantsend was set due to this call:
27337c478bd9Sstevel@tonic-gate 	 *	For T_COTSORD send an ordrel_ind
27347c478bd9Sstevel@tonic-gate 	 *
27357c478bd9Sstevel@tonic-gate 	 * Note that for T_CLTS there is no message sent here.
27367c478bd9Sstevel@tonic-gate 	 */
27377c478bd9Sstevel@tonic-gate 	if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
27387c478bd9Sstevel@tonic-gate 	    (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
27397c478bd9Sstevel@tonic-gate 		/*
27407c478bd9Sstevel@tonic-gate 		 * For SunOS 4.X compatibility we tell the other end
27417c478bd9Sstevel@tonic-gate 		 * that we are unable to receive at this point.
27427c478bd9Sstevel@tonic-gate 		 */
27430f1702c5SYu Xiangning 		if (so->so_family == AF_UNIX && sti->sti_serv_type != T_CLTS)
27447c478bd9Sstevel@tonic-gate 			so_unix_close(so);
27457c478bd9Sstevel@tonic-gate 
27460f1702c5SYu Xiangning 		if (sti->sti_serv_type == T_COTS)
27477c478bd9Sstevel@tonic-gate 			error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
27487c478bd9Sstevel@tonic-gate 	}
27497c478bd9Sstevel@tonic-gate 	if ((state_change & SS_CANTSENDMORE) &&
27500f1702c5SYu Xiangning 	    (sti->sti_serv_type == T_COTS_ORD)) {
27517c478bd9Sstevel@tonic-gate 		/* Send an orderly release */
27527c478bd9Sstevel@tonic-gate 		ordrel_req.PRIM_type = T_ORDREL_REQ;
27537c478bd9Sstevel@tonic-gate 
27547c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27557c478bd9Sstevel@tonic-gate 		mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
2756de8c4a14SErik Nordmark 		    0, _ALLOC_SLEEP, cr);
27577c478bd9Sstevel@tonic-gate 		/*
27587c478bd9Sstevel@tonic-gate 		 * Send down the T_ORDREL_REQ even if there is flow control.
27597c478bd9Sstevel@tonic-gate 		 * This prevents shutdown from blocking.
27607c478bd9Sstevel@tonic-gate 		 * Note that there is no T_OK_ACK for ordrel_req.
27617c478bd9Sstevel@tonic-gate 		 */
27627c478bd9Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
27637c478bd9Sstevel@tonic-gate 		    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
27647c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
27657c478bd9Sstevel@tonic-gate 		if (error) {
27667c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
27677c478bd9Sstevel@tonic-gate 			goto done;
27687c478bd9Sstevel@tonic-gate 		}
27697c478bd9Sstevel@tonic-gate 	}
27707c478bd9Sstevel@tonic-gate 
27717c478bd9Sstevel@tonic-gate done:
27727c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
27737c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
27747c478bd9Sstevel@tonic-gate 	return (error);
27757c478bd9Sstevel@tonic-gate }
27767c478bd9Sstevel@tonic-gate 
27777c478bd9Sstevel@tonic-gate /*
27787c478bd9Sstevel@tonic-gate  * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
27797c478bd9Sstevel@tonic-gate  * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
27807c478bd9Sstevel@tonic-gate  * that we have closed.
27817c478bd9Sstevel@tonic-gate  * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
27827c478bd9Sstevel@tonic-gate  * T_UNITDATA_REQ containing the same option.
27837c478bd9Sstevel@tonic-gate  *
27847c478bd9Sstevel@tonic-gate  * For SOCK_DGRAM half-connections (somebody connected to this end
27857c478bd9Sstevel@tonic-gate  * but this end is not connect) we don't know where to send any
27867c478bd9Sstevel@tonic-gate  * SO_UNIX_CLOSE.
27877c478bd9Sstevel@tonic-gate  *
27887c478bd9Sstevel@tonic-gate  * We have to ignore stream head errors just in case there has been
27897c478bd9Sstevel@tonic-gate  * a shutdown(output).
27907c478bd9Sstevel@tonic-gate  * Ignore any flow control to try to get the message more quickly to the peer.
27917c478bd9Sstevel@tonic-gate  * While locally ignoring flow control solves the problem when there
27927c478bd9Sstevel@tonic-gate  * is only the loopback transport on the stream it would not provide
27937c478bd9Sstevel@tonic-gate  * the correct AF_UNIX socket semantics when one or more modules have
27947c478bd9Sstevel@tonic-gate  * been pushed.
27957c478bd9Sstevel@tonic-gate  */
27967c478bd9Sstevel@tonic-gate void
27977c478bd9Sstevel@tonic-gate so_unix_close(struct sonode *so)
27987c478bd9Sstevel@tonic-gate {
27997c478bd9Sstevel@tonic-gate 	int		error;
28007c478bd9Sstevel@tonic-gate 	struct T_opthdr	toh;
28017c478bd9Sstevel@tonic-gate 	mblk_t		*mp;
28020f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
28037c478bd9Sstevel@tonic-gate 
28047c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
28057c478bd9Sstevel@tonic-gate 
28067c478bd9Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
28077c478bd9Sstevel@tonic-gate 
28087c478bd9Sstevel@tonic-gate 	if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
28097c478bd9Sstevel@tonic-gate 	    (SS_ISCONNECTED|SS_ISBOUND))
28107c478bd9Sstevel@tonic-gate 		return;
28117c478bd9Sstevel@tonic-gate 
28127c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("so_unix_close(%p) %s\n",
2813903a11ebSrh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
28147c478bd9Sstevel@tonic-gate 
28157c478bd9Sstevel@tonic-gate 	toh.level = SOL_SOCKET;
28167c478bd9Sstevel@tonic-gate 	toh.name = SO_UNIX_CLOSE;
28177c478bd9Sstevel@tonic-gate 
28187c478bd9Sstevel@tonic-gate 	/* zero length + header */
28197c478bd9Sstevel@tonic-gate 	toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
28207c478bd9Sstevel@tonic-gate 	toh.status = 0;
28217c478bd9Sstevel@tonic-gate 
28227c478bd9Sstevel@tonic-gate 	if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
28237c478bd9Sstevel@tonic-gate 		struct T_optdata_req tdr;
28247c478bd9Sstevel@tonic-gate 
28257c478bd9Sstevel@tonic-gate 		tdr.PRIM_type = T_OPTDATA_REQ;
28267c478bd9Sstevel@tonic-gate 		tdr.DATA_flag = 0;
28277c478bd9Sstevel@tonic-gate 
28287c478bd9Sstevel@tonic-gate 		tdr.OPT_length = (t_scalar_t)sizeof (toh);
28297c478bd9Sstevel@tonic-gate 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
28307c478bd9Sstevel@tonic-gate 
28317c478bd9Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
28327c478bd9Sstevel@tonic-gate 		mp = soallocproto2(&tdr, sizeof (tdr),
2833de8c4a14SErik Nordmark 		    &toh, sizeof (toh), 0, _ALLOC_SLEEP, CRED());
28347c478bd9Sstevel@tonic-gate 	} else {
28357c478bd9Sstevel@tonic-gate 		struct T_unitdata_req	tudr;
28367c478bd9Sstevel@tonic-gate 		void			*addr;
28377c478bd9Sstevel@tonic-gate 		socklen_t		addrlen;
28387c478bd9Sstevel@tonic-gate 		void			*src;
28397c478bd9Sstevel@tonic-gate 		socklen_t		srclen;
28407c478bd9Sstevel@tonic-gate 		struct T_opthdr		toh2;
28417c478bd9Sstevel@tonic-gate 		t_scalar_t		size;
28427c478bd9Sstevel@tonic-gate 
28437c478bd9Sstevel@tonic-gate 		/* Connecteded DGRAM socket */
28447c478bd9Sstevel@tonic-gate 
28457c478bd9Sstevel@tonic-gate 		/*
28467c478bd9Sstevel@tonic-gate 		 * For AF_UNIX the destination address is translated to
28477c478bd9Sstevel@tonic-gate 		 * an internal name and the source address is passed as
28487c478bd9Sstevel@tonic-gate 		 * an option.
28497c478bd9Sstevel@tonic-gate 		 */
28507c478bd9Sstevel@tonic-gate 		/*
28517c478bd9Sstevel@tonic-gate 		 * Length and family checks.
28527c478bd9Sstevel@tonic-gate 		 */
28530f1702c5SYu Xiangning 		error = so_addr_verify(so, sti->sti_faddr_sa,
28540f1702c5SYu Xiangning 		    (t_uscalar_t)sti->sti_faddr_len);
28557c478bd9Sstevel@tonic-gate 		if (error) {
28567c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
28577c478bd9Sstevel@tonic-gate 			return;
28587c478bd9Sstevel@tonic-gate 		}
28590f1702c5SYu Xiangning 		if (sti->sti_faddr_noxlate) {
28607c478bd9Sstevel@tonic-gate 			/*
28617c478bd9Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
28627c478bd9Sstevel@tonic-gate 			 * pass any (transport internal) source address.
28637c478bd9Sstevel@tonic-gate 			 */
28640f1702c5SYu Xiangning 			addr = sti->sti_faddr_sa;
28650f1702c5SYu Xiangning 			addrlen = (t_uscalar_t)sti->sti_faddr_len;
28667c478bd9Sstevel@tonic-gate 			src = NULL;
28677c478bd9Sstevel@tonic-gate 			srclen = 0;
28687c478bd9Sstevel@tonic-gate 		} else {
28697c478bd9Sstevel@tonic-gate 			/*
28707c478bd9Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
28717c478bd9Sstevel@tonic-gate 			 * and translate the remote address.
28720f1702c5SYu Xiangning 			 * Holding so_lock thus sti_laddr_sa can not change.
28737c478bd9Sstevel@tonic-gate 			 */
28740f1702c5SYu Xiangning 			src = sti->sti_laddr_sa;
28750f1702c5SYu Xiangning 			srclen = (socklen_t)sti->sti_laddr_len;
28767c478bd9Sstevel@tonic-gate 			dprintso(so, 1,
28777c478bd9Sstevel@tonic-gate 			    ("so_ux_close: srclen %d, src %p\n",
28787c478bd9Sstevel@tonic-gate 			    srclen, src));
28797c478bd9Sstevel@tonic-gate 			error = so_ux_addr_xlate(so,
28800f1702c5SYu Xiangning 			    sti->sti_faddr_sa,
28810f1702c5SYu Xiangning 			    (socklen_t)sti->sti_faddr_len, 0,
28827c478bd9Sstevel@tonic-gate 			    &addr, &addrlen);
28837c478bd9Sstevel@tonic-gate 			if (error) {
28847c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
28857c478bd9Sstevel@tonic-gate 				return;
28867c478bd9Sstevel@tonic-gate 			}
28877c478bd9Sstevel@tonic-gate 		}
28887c478bd9Sstevel@tonic-gate 		tudr.PRIM_type = T_UNITDATA_REQ;
28897c478bd9Sstevel@tonic-gate 		tudr.DEST_length = addrlen;
28907c478bd9Sstevel@tonic-gate 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
28917c478bd9Sstevel@tonic-gate 		if (srclen == 0) {
28927c478bd9Sstevel@tonic-gate 			tudr.OPT_length = (t_scalar_t)sizeof (toh);
28937c478bd9Sstevel@tonic-gate 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
28947c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(addrlen));
28957c478bd9Sstevel@tonic-gate 
28967c478bd9Sstevel@tonic-gate 			size = tudr.OPT_offset + tudr.OPT_length;
28977c478bd9Sstevel@tonic-gate 			/* NOTE: holding so_lock while sleeping */
28987c478bd9Sstevel@tonic-gate 			mp = soallocproto2(&tudr, sizeof (tudr),
2899de8c4a14SErik Nordmark 			    addr, addrlen, size, _ALLOC_SLEEP, CRED());
29007c478bd9Sstevel@tonic-gate 			mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
29017c478bd9Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
29027c478bd9Sstevel@tonic-gate 		} else {
29037c478bd9Sstevel@tonic-gate 			/*
29047c478bd9Sstevel@tonic-gate 			 * There is a AF_UNIX sockaddr_un to include as a
29057c478bd9Sstevel@tonic-gate 			 * source address option.
29067c478bd9Sstevel@tonic-gate 			 */
29077c478bd9Sstevel@tonic-gate 			tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
29087c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(srclen));
29097c478bd9Sstevel@tonic-gate 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
29107c478bd9Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(addrlen));
29117c478bd9Sstevel@tonic-gate 
29127c478bd9Sstevel@tonic-gate 			toh2.level = SOL_SOCKET;
29137c478bd9Sstevel@tonic-gate 			toh2.name = SO_SRCADDR;
29147c478bd9Sstevel@tonic-gate 			toh2.len = (t_uscalar_t)(srclen +
29157c478bd9Sstevel@tonic-gate 			    sizeof (struct T_opthdr));
29167c478bd9Sstevel@tonic-gate 			toh2.status = 0;
29177c478bd9Sstevel@tonic-gate 
29187c478bd9Sstevel@tonic-gate 			size = tudr.OPT_offset + tudr.OPT_length;
29197c478bd9Sstevel@tonic-gate 
29207c478bd9Sstevel@tonic-gate 			/* NOTE: holding so_lock while sleeping */
29217c478bd9Sstevel@tonic-gate 			mp = soallocproto2(&tudr, sizeof (tudr),
2922de8c4a14SErik Nordmark 			    addr, addrlen, size, _ALLOC_SLEEP, CRED());
29237c478bd9Sstevel@tonic-gate 			mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
29247c478bd9Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
29257c478bd9Sstevel@tonic-gate 			soappendmsg(mp, &toh2, sizeof (toh2));
29267c478bd9Sstevel@tonic-gate 			soappendmsg(mp, src, srclen);
29277c478bd9Sstevel@tonic-gate 			mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
29287c478bd9Sstevel@tonic-gate 		}
29297c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
29307c478bd9Sstevel@tonic-gate 	}
29317c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
29327c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
29337c478bd9Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
29347c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
29357c478bd9Sstevel@tonic-gate }
29367c478bd9Sstevel@tonic-gate 
29377c478bd9Sstevel@tonic-gate /*
29387c478bd9Sstevel@tonic-gate  * Called by sotpi_recvmsg when reading a non-zero amount of data.
29397c478bd9Sstevel@tonic-gate  * In addition, the caller typically verifies that there is some
29407c478bd9Sstevel@tonic-gate  * potential state to clear by checking
29417c478bd9Sstevel@tonic-gate  *	if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
29427c478bd9Sstevel@tonic-gate  * before calling this routine.
29437c478bd9Sstevel@tonic-gate  * Note that such a check can be made without holding so_lock since
29447c478bd9Sstevel@tonic-gate  * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
29450f1702c5SYu Xiangning  * decrements sti_oobsigcnt.
29467c478bd9Sstevel@tonic-gate  *
29477c478bd9Sstevel@tonic-gate  * When data is read *after* the point that all pending
29487c478bd9Sstevel@tonic-gate  * oob data has been consumed the oob indication is cleared.
29497c478bd9Sstevel@tonic-gate  *
29507c478bd9Sstevel@tonic-gate  * This logic keeps select/poll returning POLLRDBAND and
29517c478bd9Sstevel@tonic-gate  * SIOCATMARK returning true until we have read past
29527c478bd9Sstevel@tonic-gate  * the mark.
29537c478bd9Sstevel@tonic-gate  */
29547c478bd9Sstevel@tonic-gate static void
29557c478bd9Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so)
29567c478bd9Sstevel@tonic-gate {
29570f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
29580f1702c5SYu Xiangning 
29597c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
29607c478bd9Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
29617c478bd9Sstevel@tonic-gate 	dprintso(so, 1,
29627c478bd9Sstevel@tonic-gate 	    ("sorecv_update_oobstate: counts %d/%d state %s\n",
29630f1702c5SYu Xiangning 	    sti->sti_oobsigcnt,
29640f1702c5SYu Xiangning 	    sti->sti_oobcnt, pr_state(so->so_state, so->so_mode)));
29650f1702c5SYu Xiangning 	if (sti->sti_oobsigcnt == 0) {
29667c478bd9Sstevel@tonic-gate 		/* No more pending oob indications */
29677c478bd9Sstevel@tonic-gate 		so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
29687c478bd9Sstevel@tonic-gate 		freemsg(so->so_oobmsg);
29697c478bd9Sstevel@tonic-gate 		so->so_oobmsg = NULL;
29707c478bd9Sstevel@tonic-gate 	}
29717c478bd9Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
29727c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
29737c478bd9Sstevel@tonic-gate }
29747c478bd9Sstevel@tonic-gate 
29757c478bd9Sstevel@tonic-gate /*
29767c478bd9Sstevel@tonic-gate  * Handle recv* calls for an so which has NL7C saved recv mblk_t(s).
29777c478bd9Sstevel@tonic-gate  */
29787c478bd9Sstevel@tonic-gate static int
29797c478bd9Sstevel@tonic-gate nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp)
29807c478bd9Sstevel@tonic-gate {
29810f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
29827c478bd9Sstevel@tonic-gate 	int	error = 0;
29837c478bd9Sstevel@tonic-gate 	mblk_t *tmp = NULL;
29847c478bd9Sstevel@tonic-gate 	mblk_t *pmp = NULL;
29850f1702c5SYu Xiangning 	mblk_t *nmp = sti->sti_nl7c_rcv_mp;
29867c478bd9Sstevel@tonic-gate 
29877c478bd9Sstevel@tonic-gate 	ASSERT(nmp != NULL);
29887c478bd9Sstevel@tonic-gate 
29897c478bd9Sstevel@tonic-gate 	while (nmp != NULL && uiop->uio_resid > 0) {
29907c478bd9Sstevel@tonic-gate 		ssize_t n;
29917c478bd9Sstevel@tonic-gate 
29927c478bd9Sstevel@tonic-gate 		if (DB_TYPE(nmp) == M_DATA) {
29937c478bd9Sstevel@tonic-gate 			/*
29947c478bd9Sstevel@tonic-gate 			 * We have some data, uiomove up to resid bytes.
29957c478bd9Sstevel@tonic-gate 			 */
29967c478bd9Sstevel@tonic-gate 			n = MIN(MBLKL(nmp), uiop->uio_resid);
29977c478bd9Sstevel@tonic-gate 			if (n > 0)
29987c478bd9Sstevel@tonic-gate 				error = uiomove(nmp->b_rptr, n, UIO_READ, uiop);
29997c478bd9Sstevel@tonic-gate 			nmp->b_rptr += n;
30007c478bd9Sstevel@tonic-gate 			if (nmp->b_rptr == nmp->b_wptr) {
30017c478bd9Sstevel@tonic-gate 				pmp = nmp;
30027c478bd9Sstevel@tonic-gate 				nmp = nmp->b_cont;
30037c478bd9Sstevel@tonic-gate 			}
30042c9e429eSbrutus 			if (error)
30052c9e429eSbrutus 				break;
30067c478bd9Sstevel@tonic-gate 		} else {
30077c478bd9Sstevel@tonic-gate 			/*
30087c478bd9Sstevel@tonic-gate 			 * We only handle data, save for caller to handle.
30097c478bd9Sstevel@tonic-gate 			 */
30107c478bd9Sstevel@tonic-gate 			if (pmp != NULL) {
30117c478bd9Sstevel@tonic-gate 				pmp->b_cont = nmp->b_cont;
30127c478bd9Sstevel@tonic-gate 			}
30137c478bd9Sstevel@tonic-gate 			nmp->b_cont = NULL;
30147c478bd9Sstevel@tonic-gate 			if (*rmp == NULL) {
30157c478bd9Sstevel@tonic-gate 				*rmp = nmp;
30167c478bd9Sstevel@tonic-gate 			} else {
30172c9e429eSbrutus 				tmp->b_cont = nmp;
30187c478bd9Sstevel@tonic-gate 			}
30197c478bd9Sstevel@tonic-gate 			nmp = nmp->b_cont;
30207c478bd9Sstevel@tonic-gate 			tmp = nmp;
30217c478bd9Sstevel@tonic-gate 		}
30227c478bd9Sstevel@tonic-gate 	}
30237c478bd9Sstevel@tonic-gate 	if (pmp != NULL) {
30247c478bd9Sstevel@tonic-gate 		/* Free any mblk_t(s) which we have consumed */
30257c478bd9Sstevel@tonic-gate 		pmp->b_cont = NULL;
30260f1702c5SYu Xiangning 		freemsg(sti->sti_nl7c_rcv_mp);
30277c478bd9Sstevel@tonic-gate 	}
30280f1702c5SYu Xiangning 	if ((sti->sti_nl7c_rcv_mp = nmp) == NULL) {
30292c9e429eSbrutus 		/* Last mblk_t so return the saved kstrgetmsg() rval/error */
30302c9e429eSbrutus 		if (error == 0) {
30310f1702c5SYu Xiangning 			rval_t	*p = (rval_t *)&sti->sti_nl7c_rcv_rval;
30322c9e429eSbrutus 
30332c9e429eSbrutus 			error = p->r_v.r_v2;
30342c9e429eSbrutus 			p->r_v.r_v2 = 0;
30352c9e429eSbrutus 		}
30360f1702c5SYu Xiangning 		rp->r_vals = sti->sti_nl7c_rcv_rval;
30370f1702c5SYu Xiangning 		sti->sti_nl7c_rcv_rval = 0;
30387c478bd9Sstevel@tonic-gate 	} else {
30397c478bd9Sstevel@tonic-gate 		/* More mblk_t(s) to process so no rval to return */
30407c478bd9Sstevel@tonic-gate 		rp->r_vals = 0;
30417c478bd9Sstevel@tonic-gate 	}
30427c478bd9Sstevel@tonic-gate 	return (error);
30437c478bd9Sstevel@tonic-gate }
30447c478bd9Sstevel@tonic-gate /*
30457c478bd9Sstevel@tonic-gate  * Receive the next message on the queue.
30467c478bd9Sstevel@tonic-gate  * If msg_controllen is non-zero when called the caller is interested in
30477c478bd9Sstevel@tonic-gate  * any received control info (options).
30487c478bd9Sstevel@tonic-gate  * If msg_namelen is non-zero when called the caller is interested in
30497c478bd9Sstevel@tonic-gate  * any received source address.
30507c478bd9Sstevel@tonic-gate  * The routine returns with msg_control and msg_name pointing to
30517c478bd9Sstevel@tonic-gate  * kmem_alloc'ed memory which the caller has to free.
30527c478bd9Sstevel@tonic-gate  */
30530f1702c5SYu Xiangning /* ARGSUSED */
30547c478bd9Sstevel@tonic-gate int
30550f1702c5SYu Xiangning sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
30560f1702c5SYu Xiangning     struct cred *cr)
30577c478bd9Sstevel@tonic-gate {
30587c478bd9Sstevel@tonic-gate 	union T_primitives	*tpr;
30597c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
30607c478bd9Sstevel@tonic-gate 	uchar_t			pri;
30617c478bd9Sstevel@tonic-gate 	int			pflag, opflag;
30627c478bd9Sstevel@tonic-gate 	void			*control;
30637c478bd9Sstevel@tonic-gate 	t_uscalar_t		controllen;
30647c478bd9Sstevel@tonic-gate 	t_uscalar_t		namelen;
30657c478bd9Sstevel@tonic-gate 	int			so_state = so->so_state; /* Snapshot */
30667c478bd9Sstevel@tonic-gate 	ssize_t			saved_resid;
30677c478bd9Sstevel@tonic-gate 	rval_t			rval;
30687c478bd9Sstevel@tonic-gate 	int			flags;
30697c478bd9Sstevel@tonic-gate 	clock_t			timout;
307017169044Sbrutus 	int			error = 0;
30710f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
30727c478bd9Sstevel@tonic-gate 
30737c478bd9Sstevel@tonic-gate 	flags = msg->msg_flags;
30747c478bd9Sstevel@tonic-gate 	msg->msg_flags = 0;
30757c478bd9Sstevel@tonic-gate 
30767c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
3077903a11ebSrh87107 	    (void *)so, (void *)msg, flags,
30787c478bd9Sstevel@tonic-gate 	    pr_state(so->so_state, so->so_mode), so->so_error));
30797c478bd9Sstevel@tonic-gate 
30800f1702c5SYu Xiangning 	if (so->so_version == SOV_STREAM) {
30810f1702c5SYu Xiangning 		so_update_attrs(so, SOACC);
30820f1702c5SYu Xiangning 		/* The imaginary "sockmod" has been popped - act as a stream */
30830f1702c5SYu Xiangning 		return (strread(SOTOV(so), uiop, cr));
30840f1702c5SYu Xiangning 	}
30850f1702c5SYu Xiangning 
30867c478bd9Sstevel@tonic-gate 	/*
30877c478bd9Sstevel@tonic-gate 	 * If we are not connected because we have never been connected
30887c478bd9Sstevel@tonic-gate 	 * we return ENOTCONN. If we have been connected (but are no longer
30897c478bd9Sstevel@tonic-gate 	 * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
30907c478bd9Sstevel@tonic-gate 	 * the EOF.
30917c478bd9Sstevel@tonic-gate 	 *
30927c478bd9Sstevel@tonic-gate 	 * An alternative would be to post an ENOTCONN error in stream head
30937c478bd9Sstevel@tonic-gate 	 * (read+write) and clear it when we're connected. However, that error
30947c478bd9Sstevel@tonic-gate 	 * would cause incorrect poll/select behavior!
30957c478bd9Sstevel@tonic-gate 	 */
30967c478bd9Sstevel@tonic-gate 	if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
30977c478bd9Sstevel@tonic-gate 	    (so->so_mode & SM_CONNREQUIRED)) {
30987c478bd9Sstevel@tonic-gate 		return (ENOTCONN);
30997c478bd9Sstevel@tonic-gate 	}
31007c478bd9Sstevel@tonic-gate 
31017c478bd9Sstevel@tonic-gate 	/*
31027c478bd9Sstevel@tonic-gate 	 * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
31037c478bd9Sstevel@tonic-gate 	 * after checking that the read queue is empty) and returns zero.
31047c478bd9Sstevel@tonic-gate 	 * This implementation will sleep (in kstrgetmsg) even if uio_resid
31057c478bd9Sstevel@tonic-gate 	 * is zero.
31067c478bd9Sstevel@tonic-gate 	 */
31077c478bd9Sstevel@tonic-gate 
31087c478bd9Sstevel@tonic-gate 	if (flags & MSG_OOB) {
31097c478bd9Sstevel@tonic-gate 		/* Check that the transport supports OOB */
31107c478bd9Sstevel@tonic-gate 		if (!(so->so_mode & SM_EXDATA))
31117c478bd9Sstevel@tonic-gate 			return (EOPNOTSUPP);
31120f1702c5SYu Xiangning 		so_update_attrs(so, SOACC);
31130f1702c5SYu Xiangning 		return (sorecvoob(so, msg, uiop, flags,
31140f1702c5SYu Xiangning 		    (so->so_options & SO_OOBINLINE)));
31157c478bd9Sstevel@tonic-gate 	}
31167c478bd9Sstevel@tonic-gate 
31170f1702c5SYu Xiangning 	so_update_attrs(so, SOACC);
31180f1702c5SYu Xiangning 
31197c478bd9Sstevel@tonic-gate 	/*
31207c478bd9Sstevel@tonic-gate 	 * Set msg_controllen and msg_namelen to zero here to make it
31217c478bd9Sstevel@tonic-gate 	 * simpler in the cases that no control or name is returned.
31227c478bd9Sstevel@tonic-gate 	 */
31237c478bd9Sstevel@tonic-gate 	controllen = msg->msg_controllen;
31247c478bd9Sstevel@tonic-gate 	namelen = msg->msg_namelen;
31257c478bd9Sstevel@tonic-gate 	msg->msg_controllen = 0;
31267c478bd9Sstevel@tonic-gate 	msg->msg_namelen = 0;
31277c478bd9Sstevel@tonic-gate 
31287c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
31297c478bd9Sstevel@tonic-gate 	    namelen, controllen));
31307c478bd9Sstevel@tonic-gate 
31312c9e429eSbrutus 	mutex_enter(&so->so_lock);
31327c478bd9Sstevel@tonic-gate 	/*
31337c478bd9Sstevel@tonic-gate 	 * If an NL7C enabled socket and not waiting for write data.
31347c478bd9Sstevel@tonic-gate 	 */
31350f1702c5SYu Xiangning 	if ((sti->sti_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) ==
31367c478bd9Sstevel@tonic-gate 	    NL7C_ENABLED) {
31370f1702c5SYu Xiangning 		if (sti->sti_nl7c_uri) {
31382c9e429eSbrutus 			/* Close uri processing for a previous request */
31397c478bd9Sstevel@tonic-gate 			nl7c_close(so);
31407c478bd9Sstevel@tonic-gate 		}
31410f1702c5SYu Xiangning 		if ((so_state & SS_CANTRCVMORE) &&
31420f1702c5SYu Xiangning 		    sti->sti_nl7c_rcv_mp == NULL) {
31432c9e429eSbrutus 			/* Nothing to process, EOF */
31442c9e429eSbrutus 			mutex_exit(&so->so_lock);
31452c9e429eSbrutus 			return (0);
31460f1702c5SYu Xiangning 		} else if (sti->sti_nl7c_flags & NL7C_SOPERSIST) {
31472c9e429eSbrutus 			/* Persistent NL7C socket, try to process request */
31482c9e429eSbrutus 			boolean_t ret;
31492c9e429eSbrutus 
31502c9e429eSbrutus 			ret = nl7c_process(so,
31512c9e429eSbrutus 			    (so->so_state & (SS_NONBLOCK|SS_NDELAY)));
31520f1702c5SYu Xiangning 			rval.r_vals = sti->sti_nl7c_rcv_rval;
31532c9e429eSbrutus 			error = rval.r_v.r_v2;
31542c9e429eSbrutus 			if (error) {
31552c9e429eSbrutus 				/* Error of some sort, return it */
31562c9e429eSbrutus 				mutex_exit(&so->so_lock);
31572c9e429eSbrutus 				return (error);
31582c9e429eSbrutus 			}
31590f1702c5SYu Xiangning 			if (sti->sti_nl7c_flags &&
31600f1702c5SYu Xiangning 			    ! (sti->sti_nl7c_flags & NL7C_WAITWRITE)) {
31617c478bd9Sstevel@tonic-gate 				/*
31622c9e429eSbrutus 				 * Still an NL7C socket and no data
31632c9e429eSbrutus 				 * to pass up to the caller.
31642c9e429eSbrutus 				 */
31652c9e429eSbrutus 				mutex_exit(&so->so_lock);
31662c9e429eSbrutus 				if (ret) {
31672c9e429eSbrutus 					/* EOF */
31682c9e429eSbrutus 					return (0);
31692c9e429eSbrutus 				} else {
31702c9e429eSbrutus 					/* Need more data */
31712c9e429eSbrutus 					return (EAGAIN);
31722c9e429eSbrutus 				}
31732c9e429eSbrutus 			}
31742c9e429eSbrutus 		} else {
31752c9e429eSbrutus 			/*
31762c9e429eSbrutus 			 * Not persistent so no further NL7C processing.
31777c478bd9Sstevel@tonic-gate 			 */
31780f1702c5SYu Xiangning 			sti->sti_nl7c_flags = 0;
31797c478bd9Sstevel@tonic-gate 		}
31807c478bd9Sstevel@tonic-gate 	}
31817c478bd9Sstevel@tonic-gate 	/*
31827c478bd9Sstevel@tonic-gate 	 * Only one reader is allowed at any given time. This is needed
31837c478bd9Sstevel@tonic-gate 	 * for T_EXDATA handling and, in the future, MSG_WAITALL.
31847c478bd9Sstevel@tonic-gate 	 *
31857c478bd9Sstevel@tonic-gate 	 * This is slightly different that BSD behavior in that it fails with
31867c478bd9Sstevel@tonic-gate 	 * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
31877c478bd9Sstevel@tonic-gate 	 * is single-threaded using sblock(), which is dropped while waiting
31887c478bd9Sstevel@tonic-gate 	 * for data to appear. The difference shows up e.g. if one
31897c478bd9Sstevel@tonic-gate 	 * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
31907c478bd9Sstevel@tonic-gate 	 * does use nonblocking io and different threads are reading each
31917c478bd9Sstevel@tonic-gate 	 * file descriptor. In BSD there would never be an EWOULDBLOCK error
31927c478bd9Sstevel@tonic-gate 	 * in this case as long as the read queue doesn't get empty.
31937c478bd9Sstevel@tonic-gate 	 * In this implementation the thread using nonblocking io can
31947c478bd9Sstevel@tonic-gate 	 * get an EWOULDBLOCK error due to the blocking thread executing
31957c478bd9Sstevel@tonic-gate 	 * e.g. in the uiomove in kstrgetmsg.
31967c478bd9Sstevel@tonic-gate 	 * This difference is not believed to be significant.
31977c478bd9Sstevel@tonic-gate 	 */
3198255daac4Sethindra 	/* Set SOREADLOCKED */
3199255daac4Sethindra 	error = so_lock_read_intr(so,
3200255daac4Sethindra 	    uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
32017c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
32027c478bd9Sstevel@tonic-gate 	if (error)
32037c478bd9Sstevel@tonic-gate 		return (error);
32047c478bd9Sstevel@tonic-gate 
32057c478bd9Sstevel@tonic-gate 	/*
32067c478bd9Sstevel@tonic-gate 	 * Tell kstrgetmsg to not inspect the stream head errors until all
32077c478bd9Sstevel@tonic-gate 	 * queued data has been consumed.
32087c478bd9Sstevel@tonic-gate 	 * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
32097c478bd9Sstevel@tonic-gate 	 * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
32107c478bd9Sstevel@tonic-gate 	 *
32117c478bd9Sstevel@tonic-gate 	 * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
32127c478bd9Sstevel@tonic-gate 	 * to T_OPTDATA_IND that do not contain any user-visible control msg.
32137c478bd9Sstevel@tonic-gate 	 * Note that MSG_WAITALL set with MSG_PEEK is a noop.
32147c478bd9Sstevel@tonic-gate 	 */
32157c478bd9Sstevel@tonic-gate 	pflag = MSG_ANY | MSG_DELAYERROR;
32167c478bd9Sstevel@tonic-gate 	if (flags & MSG_PEEK) {
32177c478bd9Sstevel@tonic-gate 		pflag |= MSG_IPEEK;
32187c478bd9Sstevel@tonic-gate 		flags &= ~MSG_WAITALL;
32197c478bd9Sstevel@tonic-gate 	}
32207c478bd9Sstevel@tonic-gate 	if (so->so_mode & SM_ATOMIC)
32217c478bd9Sstevel@tonic-gate 		pflag |= MSG_DISCARDTAIL;
32227c478bd9Sstevel@tonic-gate 
32237c478bd9Sstevel@tonic-gate 	if (flags & MSG_DONTWAIT)
32247c478bd9Sstevel@tonic-gate 		timout = 0;
3225412cc9e9SGordon Ross 	else if (so->so_rcvtimeo != 0)
3226412cc9e9SGordon Ross 		timout = TICK_TO_MSEC(so->so_rcvtimeo);
32277c478bd9Sstevel@tonic-gate 	else
32287c478bd9Sstevel@tonic-gate 		timout = -1;
32297c478bd9Sstevel@tonic-gate 	opflag = pflag;
32307c478bd9Sstevel@tonic-gate retry:
32317c478bd9Sstevel@tonic-gate 	saved_resid = uiop->uio_resid;
32327c478bd9Sstevel@tonic-gate 	pri = 0;
32337c478bd9Sstevel@tonic-gate 	mp = NULL;
32340f1702c5SYu Xiangning 	if (sti->sti_nl7c_rcv_mp != NULL) {
32352c9e429eSbrutus 		/* Already kstrgetmsg()ed saved mblk(s) from NL7C */
32367c478bd9Sstevel@tonic-gate 		error = nl7c_sorecv(so, &mp, uiop, &rval);
32377c478bd9Sstevel@tonic-gate 	} else {
32387c478bd9Sstevel@tonic-gate 		error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
32397c478bd9Sstevel@tonic-gate 		    timout, &rval);
32407c478bd9Sstevel@tonic-gate 	}
32410f1702c5SYu Xiangning 	if (error != 0) {
32420f1702c5SYu Xiangning 		/* kstrgetmsg returns ETIME when timeout expires */
32430f1702c5SYu Xiangning 		if (error == ETIME)
32447c478bd9Sstevel@tonic-gate 			error = EWOULDBLOCK;
324517169044Sbrutus 		goto out;
32467c478bd9Sstevel@tonic-gate 	}
32477c478bd9Sstevel@tonic-gate 	/*
32487c478bd9Sstevel@tonic-gate 	 * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
32497c478bd9Sstevel@tonic-gate 	 * For non-datagrams MOREDATA is used to set MSG_EOR.
32507c478bd9Sstevel@tonic-gate 	 */
32517c478bd9Sstevel@tonic-gate 	ASSERT(!(rval.r_val1 & MORECTL));
32527c478bd9Sstevel@tonic-gate 	if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
32537c478bd9Sstevel@tonic-gate 		msg->msg_flags |= MSG_TRUNC;
32547c478bd9Sstevel@tonic-gate 
32557c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
32567c478bd9Sstevel@tonic-gate 		dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
32577c478bd9Sstevel@tonic-gate 		/*
32587c478bd9Sstevel@tonic-gate 		 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
32597c478bd9Sstevel@tonic-gate 		 * The draft Posix socket spec states that the mark should
32607c478bd9Sstevel@tonic-gate 		 * not be cleared when peeking. We follow the latter.
32617c478bd9Sstevel@tonic-gate 		 */
32627c478bd9Sstevel@tonic-gate 		if ((so->so_state &
32637c478bd9Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32647c478bd9Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
32657c478bd9Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
32667c478bd9Sstevel@tonic-gate 			sorecv_update_oobstate(so);
32677c478bd9Sstevel@tonic-gate 		}
32687c478bd9Sstevel@tonic-gate 
32697c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
32707c478bd9Sstevel@tonic-gate 		/* Set MSG_EOR based on MOREDATA */
32717c478bd9Sstevel@tonic-gate 		if (!(rval.r_val1 & MOREDATA)) {
32727c478bd9Sstevel@tonic-gate 			if (so->so_state & SS_SAVEDEOR) {
32737c478bd9Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
32747c478bd9Sstevel@tonic-gate 				so->so_state &= ~SS_SAVEDEOR;
32757c478bd9Sstevel@tonic-gate 			}
32767c478bd9Sstevel@tonic-gate 		}
32777c478bd9Sstevel@tonic-gate 		/*
32787c478bd9Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
32797c478bd9Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
32807c478bd9Sstevel@tonic-gate 		 */
32817c478bd9Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
32827c478bd9Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
32837c478bd9Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
32847c478bd9Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
32857c478bd9Sstevel@tonic-gate 			goto retry;
32867c478bd9Sstevel@tonic-gate 		}
328717169044Sbrutus 		goto out_locked;
32887c478bd9Sstevel@tonic-gate 	}
32897c478bd9Sstevel@tonic-gate 
32907c478bd9Sstevel@tonic-gate 	/* strsock_proto has already verified length and alignment */
32917c478bd9Sstevel@tonic-gate 	tpr = (union T_primitives *)mp->b_rptr;
32927c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
32937c478bd9Sstevel@tonic-gate 
32947c478bd9Sstevel@tonic-gate 	switch (tpr->type) {
32957c478bd9Sstevel@tonic-gate 	case T_DATA_IND: {
32967c478bd9Sstevel@tonic-gate 		if ((so->so_state &
32977c478bd9Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32987c478bd9Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
32997c478bd9Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
33007c478bd9Sstevel@tonic-gate 			sorecv_update_oobstate(so);
33017c478bd9Sstevel@tonic-gate 		}
33027c478bd9Sstevel@tonic-gate 
33037c478bd9Sstevel@tonic-gate 		/*
33047c478bd9Sstevel@tonic-gate 		 * Set msg_flags to MSG_EOR based on
33057c478bd9Sstevel@tonic-gate 		 * MORE_flag and MOREDATA.
33067c478bd9Sstevel@tonic-gate 		 */
33077c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
33087c478bd9Sstevel@tonic-gate 		so->so_state &= ~SS_SAVEDEOR;
33097c478bd9Sstevel@tonic-gate 		if (!(tpr->data_ind.MORE_flag & 1)) {
33107c478bd9Sstevel@tonic-gate 			if (!(rval.r_val1 & MOREDATA))
33117c478bd9Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
33127c478bd9Sstevel@tonic-gate 			else
33137c478bd9Sstevel@tonic-gate 				so->so_state |= SS_SAVEDEOR;
33147c478bd9Sstevel@tonic-gate 		}
33157c478bd9Sstevel@tonic-gate 		freemsg(mp);
33167c478bd9Sstevel@tonic-gate 		/*
33177c478bd9Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
33187c478bd9Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
33197c478bd9Sstevel@tonic-gate 		 */
33207c478bd9Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
33217c478bd9Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
33227c478bd9Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
33237c478bd9Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
33247c478bd9Sstevel@tonic-gate 			goto retry;
33257c478bd9Sstevel@tonic-gate 		}
332617169044Sbrutus 		goto out_locked;
33277c478bd9Sstevel@tonic-gate 	}
33287c478bd9Sstevel@tonic-gate 	case T_UNITDATA_IND: {
33297c478bd9Sstevel@tonic-gate 		void *addr;
33307c478bd9Sstevel@tonic-gate 		t_uscalar_t addrlen;
33317c478bd9Sstevel@tonic-gate 		void *abuf;
33327c478bd9Sstevel@tonic-gate 		t_uscalar_t optlen;
33337c478bd9Sstevel@tonic-gate 		void *opt;
33347c478bd9Sstevel@tonic-gate 
33357c478bd9Sstevel@tonic-gate 		if ((so->so_state &
33367c478bd9Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
33377c478bd9Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
33387c478bd9Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
33397c478bd9Sstevel@tonic-gate 			sorecv_update_oobstate(so);
33407c478bd9Sstevel@tonic-gate 		}
33417c478bd9Sstevel@tonic-gate 
33427c478bd9Sstevel@tonic-gate 		if (namelen != 0) {
33437c478bd9Sstevel@tonic-gate 			/* Caller wants source address */
33447c478bd9Sstevel@tonic-gate 			addrlen = tpr->unitdata_ind.SRC_length;
33457c478bd9Sstevel@tonic-gate 			addr = sogetoff(mp,
33467c478bd9Sstevel@tonic-gate 			    tpr->unitdata_ind.SRC_offset,
33477c478bd9Sstevel@tonic-gate 			    addrlen, 1);
33487c478bd9Sstevel@tonic-gate 			if (addr == NULL) {
33497c478bd9Sstevel@tonic-gate 				freemsg(mp);
33507c478bd9Sstevel@tonic-gate 				error = EPROTO;
33517c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
335217169044Sbrutus 				goto out;
33537c478bd9Sstevel@tonic-gate 			}
33547c478bd9Sstevel@tonic-gate 			if (so->so_family == AF_UNIX) {
33557c478bd9Sstevel@tonic-gate 				/*
33567c478bd9Sstevel@tonic-gate 				 * Can not use the transport level address.
33577c478bd9Sstevel@tonic-gate 				 * If there is a SO_SRCADDR option carrying
33587c478bd9Sstevel@tonic-gate 				 * the socket level address it will be
33597c478bd9Sstevel@tonic-gate 				 * extracted below.
33607c478bd9Sstevel@tonic-gate 				 */
33617c478bd9Sstevel@tonic-gate 				addr = NULL;
33627c478bd9Sstevel@tonic-gate 				addrlen = 0;
33637c478bd9Sstevel@tonic-gate 			}
33647c478bd9Sstevel@tonic-gate 		}
33657c478bd9Sstevel@tonic-gate 		optlen = tpr->unitdata_ind.OPT_length;
33667c478bd9Sstevel@tonic-gate 		if (optlen != 0) {
33677c478bd9Sstevel@tonic-gate 			t_uscalar_t ncontrollen;
33687c478bd9Sstevel@tonic-gate 
33697c478bd9Sstevel@tonic-gate 			/*
33707c478bd9Sstevel@tonic-gate 			 * Extract any source address option.
33717c478bd9Sstevel@tonic-gate 			 * Determine how large cmsg buffer is needed.
33727c478bd9Sstevel@tonic-gate 			 */
33737c478bd9Sstevel@tonic-gate 			opt = sogetoff(mp,
33747c478bd9Sstevel@tonic-gate 			    tpr->unitdata_ind.OPT_offset,
33757c478bd9Sstevel@tonic-gate 			    optlen, __TPI_ALIGN_SIZE);
33767c478bd9Sstevel@tonic-gate 
33777c478bd9Sstevel@tonic-gate 			if (opt == NULL) {
33787c478bd9Sstevel@tonic-gate 				freemsg(mp);
33797c478bd9Sstevel@tonic-gate 				error = EPROTO;
33807c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
338117169044Sbrutus 				goto out;
33827c478bd9Sstevel@tonic-gate 			}
33837c478bd9Sstevel@tonic-gate 			if (so->so_family == AF_UNIX)
33847c478bd9Sstevel@tonic-gate 				so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
33857c478bd9Sstevel@tonic-gate 			ncontrollen = so_cmsglen(mp, opt, optlen,
33867c478bd9Sstevel@tonic-gate 			    !(flags & MSG_XPG4_2));
33877c478bd9Sstevel@tonic-gate 			if (controllen != 0)
33887c478bd9Sstevel@tonic-gate 				controllen = ncontrollen;
33897c478bd9Sstevel@tonic-gate 			else if (ncontrollen != 0)
33907c478bd9Sstevel@tonic-gate 				msg->msg_flags |= MSG_CTRUNC;
33917c478bd9Sstevel@tonic-gate 		} else {
33927c478bd9Sstevel@tonic-gate 			controllen = 0;
33937c478bd9Sstevel@tonic-gate 		}
33947c478bd9Sstevel@tonic-gate 
33957c478bd9Sstevel@tonic-gate 		if (namelen != 0) {
33967c478bd9Sstevel@tonic-gate 			/*
33977c478bd9Sstevel@tonic-gate 			 * Return address to caller.
33987c478bd9Sstevel@tonic-gate 			 * Caller handles truncation if length
33997c478bd9Sstevel@tonic-gate 			 * exceeds msg_namelen.
34007c478bd9Sstevel@tonic-gate 			 * NOTE: AF_UNIX NUL termination is ensured by
34017c478bd9Sstevel@tonic-gate 			 * the sender's copyin_name().
34027c478bd9Sstevel@tonic-gate 			 */
34037c478bd9Sstevel@tonic-gate 			abuf = kmem_alloc(addrlen, KM_SLEEP);
34047c478bd9Sstevel@tonic-gate 
34057c478bd9Sstevel@tonic-gate 			bcopy(addr, abuf, addrlen);
34067c478bd9Sstevel@tonic-gate 			msg->msg_name = abuf;
34077c478bd9Sstevel@tonic-gate 			msg->msg_namelen = addrlen;
34087c478bd9Sstevel@tonic-gate 		}
34097c478bd9Sstevel@tonic-gate 
34107c478bd9Sstevel@tonic-gate 		if (controllen != 0) {
34117c478bd9Sstevel@tonic-gate 			/*
34127c478bd9Sstevel@tonic-gate 			 * Return control msg to caller.
34137c478bd9Sstevel@tonic-gate 			 * Caller handles truncation if length
34147c478bd9Sstevel@tonic-gate 			 * exceeds msg_controllen.
34157c478bd9Sstevel@tonic-gate 			 */
3416274af231Samehta 			control = kmem_zalloc(controllen, KM_SLEEP);
34177c478bd9Sstevel@tonic-gate 
34187c478bd9Sstevel@tonic-gate 			error = so_opt2cmsg(mp, opt, optlen,
34197c478bd9Sstevel@tonic-gate 			    !(flags & MSG_XPG4_2),
34207c478bd9Sstevel@tonic-gate 			    control, controllen);
34217c478bd9Sstevel@tonic-gate 			if (error) {
34227c478bd9Sstevel@tonic-gate 				freemsg(mp);
34237c478bd9Sstevel@tonic-gate 				if (msg->msg_namelen != 0)
34247c478bd9Sstevel@tonic-gate 					kmem_free(msg->msg_name,
34257c478bd9Sstevel@tonic-gate 					    msg->msg_namelen);
34267c478bd9Sstevel@tonic-gate 				kmem_free(control, controllen);
34277c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
342817169044Sbrutus 				goto out;
34297c478bd9Sstevel@tonic-gate 			}
34307c478bd9Sstevel@tonic-gate 			msg->msg_control = control;
34317c478bd9Sstevel@tonic-gate 			msg->msg_controllen = controllen;
34327c478bd9Sstevel@tonic-gate 		}
34337c478bd9Sstevel@tonic-gate 
34347c478bd9Sstevel@tonic-gate 		freemsg(mp);
343517169044Sbrutus 		goto out;
34367c478bd9Sstevel@tonic-gate 	}
34377c478bd9Sstevel@tonic-gate 	case T_OPTDATA_IND: {
34387c478bd9Sstevel@tonic-gate 		struct T_optdata_req *tdr;
34397c478bd9Sstevel@tonic-gate 		void *opt;
34407c478bd9Sstevel@tonic-gate 		t_uscalar_t optlen;
34417c478bd9Sstevel@tonic-gate 
34427c478bd9Sstevel@tonic-gate 		if ((so->so_state &
34437c478bd9Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
34447c478bd9Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
34457c478bd9Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
34467c478bd9Sstevel@tonic-gate 			sorecv_update_oobstate(so);
34477c478bd9Sstevel@tonic-gate 		}
34487c478bd9Sstevel@tonic-gate 
34497c478bd9Sstevel@tonic-gate 		tdr = (struct T_optdata_req *)mp->b_rptr;
34507c478bd9Sstevel@tonic-gate 		optlen = tdr->OPT_length;
34517c478bd9Sstevel@tonic-gate 		if (optlen != 0) {
34527c478bd9Sstevel@tonic-gate 			t_uscalar_t ncontrollen;
34537c478bd9Sstevel@tonic-gate 			/*
34547c478bd9Sstevel@tonic-gate 			 * Determine how large cmsg buffer is needed.
34557c478bd9Sstevel@tonic-gate 			 */
34567c478bd9Sstevel@tonic-gate 			opt = sogetoff(mp,
34577c478bd9Sstevel@tonic-gate 			    tpr->optdata_ind.OPT_offset,
34587c478bd9Sstevel@tonic-gate 			    optlen, __TPI_ALIGN_SIZE);
34597c478bd9Sstevel@tonic-gate 
34607c478bd9Sstevel@tonic-gate 			if (opt == NULL) {
34617c478bd9Sstevel@tonic-gate 				freemsg(mp);
34627c478bd9Sstevel@tonic-gate 				error = EPROTO;
34637c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
346417169044Sbrutus 				goto out;
34657c478bd9Sstevel@tonic-gate 			}
34667c478bd9Sstevel@tonic-gate 
34677c478bd9Sstevel@tonic-gate 			ncontrollen = so_cmsglen(mp, opt, optlen,
34687c478bd9Sstevel@tonic-gate 			    !(flags & MSG_XPG4_2));
34697c478bd9Sstevel@tonic-gate 			if (controllen != 0)
34707c478bd9Sstevel@tonic-gate 				controllen = ncontrollen;
34717c478bd9Sstevel@tonic-gate 			else if (ncontrollen != 0)
34727c478bd9Sstevel@tonic-gate 				msg->msg_flags |= MSG_CTRUNC;
34737c478bd9Sstevel@tonic-gate 		} else {
34747c478bd9Sstevel@tonic-gate 			controllen = 0;
34757c478bd9Sstevel@tonic-gate 		}
34767c478bd9Sstevel@tonic-gate 
34777c478bd9Sstevel@tonic-gate 		if (controllen != 0) {
34787c478bd9Sstevel@tonic-gate 			/*
34797c478bd9Sstevel@tonic-gate 			 * Return control msg to caller.
34807c478bd9Sstevel@tonic-gate 			 * Caller handles truncation if length
34817c478bd9Sstevel@tonic-gate 			 * exceeds msg_controllen.
34827c478bd9Sstevel@tonic-gate 			 */
3483274af231Samehta 			control = kmem_zalloc(controllen, KM_SLEEP);
34847c478bd9Sstevel@tonic-gate 
34857c478bd9Sstevel@tonic-gate 			error = so_opt2cmsg(mp, opt, optlen,
34867c478bd9Sstevel@tonic-gate 			    !(flags & MSG_XPG4_2),
34877c478bd9Sstevel@tonic-gate 			    control, controllen);
34887c478bd9Sstevel@tonic-gate 			if (error) {
34897c478bd9Sstevel@tonic-gate 				freemsg(mp);
34907c478bd9Sstevel@tonic-gate 				kmem_free(control, controllen);
34917c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
349217169044Sbrutus 				goto out;
34937c478bd9Sstevel@tonic-gate 			}
34947c478bd9Sstevel@tonic-gate 			msg->msg_control = control;
34957c478bd9Sstevel@tonic-gate 			msg->msg_controllen = controllen;
34967c478bd9Sstevel@tonic-gate 		}
34977c478bd9Sstevel@tonic-gate 
34987c478bd9Sstevel@tonic-gate 		/*
34997c478bd9Sstevel@tonic-gate 		 * Set msg_flags to MSG_EOR based on
35007c478bd9Sstevel@tonic-gate 		 * DATA_flag and MOREDATA.
35017c478bd9Sstevel@tonic-gate 		 */
35027c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
35037c478bd9Sstevel@tonic-gate 		so->so_state &= ~SS_SAVEDEOR;
35047c478bd9Sstevel@tonic-gate 		if (!(tpr->data_ind.MORE_flag & 1)) {
35057c478bd9Sstevel@tonic-gate 			if (!(rval.r_val1 & MOREDATA))
35067c478bd9Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
35077c478bd9Sstevel@tonic-gate 			else
35087c478bd9Sstevel@tonic-gate 				so->so_state |= SS_SAVEDEOR;
35097c478bd9Sstevel@tonic-gate 		}
35107c478bd9Sstevel@tonic-gate 		freemsg(mp);
35117c478bd9Sstevel@tonic-gate 		/*
35127c478bd9Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
35137c478bd9Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
35147c478bd9Sstevel@tonic-gate 		 * Not possible to wait if control info was received.
35157c478bd9Sstevel@tonic-gate 		 */
35167c478bd9Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
35177c478bd9Sstevel@tonic-gate 		    controllen == 0 &&
35187c478bd9Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
35197c478bd9Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
35207c478bd9Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
35217c478bd9Sstevel@tonic-gate 			goto retry;
35227c478bd9Sstevel@tonic-gate 		}
352317169044Sbrutus 		goto out_locked;
35247c478bd9Sstevel@tonic-gate 	}
35257c478bd9Sstevel@tonic-gate 	case T_EXDATA_IND: {
35267c478bd9Sstevel@tonic-gate 		dprintso(so, 1,
35277c478bd9Sstevel@tonic-gate 		    ("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
35287c478bd9Sstevel@tonic-gate 		    "state %s\n",
35290f1702c5SYu Xiangning 		    sti->sti_oobsigcnt, sti->sti_oobcnt,
35307c478bd9Sstevel@tonic-gate 		    saved_resid - uiop->uio_resid,
35317c478bd9Sstevel@tonic-gate 		    pr_state(so->so_state, so->so_mode)));
35327c478bd9Sstevel@tonic-gate 		/*
35337c478bd9Sstevel@tonic-gate 		 * kstrgetmsg handles MSGMARK so there is nothing to
35347c478bd9Sstevel@tonic-gate 		 * inspect in the T_EXDATA_IND.
35357c478bd9Sstevel@tonic-gate 		 * strsock_proto makes the stream head queue the T_EXDATA_IND
35367c478bd9Sstevel@tonic-gate 		 * as a separate message with no M_DATA component. Furthermore,
35377c478bd9Sstevel@tonic-gate 		 * the stream head does not consolidate M_DATA messages onto
35387c478bd9Sstevel@tonic-gate 		 * an MSGMARK'ed message ensuring that the T_EXDATA_IND
35397c478bd9Sstevel@tonic-gate 		 * remains a message by itself. This is needed since MSGMARK
35407c478bd9Sstevel@tonic-gate 		 * marks both the whole message as well as the last byte
35417c478bd9Sstevel@tonic-gate 		 * of the message.
35427c478bd9Sstevel@tonic-gate 		 */
35437c478bd9Sstevel@tonic-gate 		freemsg(mp);
35447c478bd9Sstevel@tonic-gate 		ASSERT(uiop->uio_resid == saved_resid);	/* No data */
35457c478bd9Sstevel@tonic-gate 		if (flags & MSG_PEEK) {
35467c478bd9Sstevel@tonic-gate 			/*
35477c478bd9Sstevel@tonic-gate 			 * Even though we are peeking we consume the
35487c478bd9Sstevel@tonic-gate 			 * T_EXDATA_IND thereby moving the mark information
35497c478bd9Sstevel@tonic-gate 			 * to SS_RCVATMARK. Then the oob code below will
35507c478bd9Sstevel@tonic-gate 			 * retry the peeking kstrgetmsg.
35517c478bd9Sstevel@tonic-gate 			 * Note that the stream head read queue is
35527c478bd9Sstevel@tonic-gate 			 * never flushed without holding SOREADLOCKED
35537c478bd9Sstevel@tonic-gate 			 * thus the T_EXDATA_IND can not disappear
35547c478bd9Sstevel@tonic-gate 			 * underneath us.
35557c478bd9Sstevel@tonic-gate 			 */
35567c478bd9Sstevel@tonic-gate 			dprintso(so, 1,
35577c478bd9Sstevel@tonic-gate 			    ("sotpi_recvmsg: consume EXDATA_IND "
35587c478bd9Sstevel@tonic-gate 			    "counts %d/%d state %s\n",
35590f1702c5SYu Xiangning 			    sti->sti_oobsigcnt,
35600f1702c5SYu Xiangning 			    sti->sti_oobcnt,
35617c478bd9Sstevel@tonic-gate 			    pr_state(so->so_state, so->so_mode)));
35627c478bd9Sstevel@tonic-gate 
35637c478bd9Sstevel@tonic-gate 			pflag = MSG_ANY | MSG_DELAYERROR;
35647c478bd9Sstevel@tonic-gate 			if (so->so_mode & SM_ATOMIC)
35657c478bd9Sstevel@tonic-gate 				pflag |= MSG_DISCARDTAIL;
35667c478bd9Sstevel@tonic-gate 
35677c478bd9Sstevel@tonic-gate 			pri = 0;
35687c478bd9Sstevel@tonic-gate 			mp = NULL;
35697c478bd9Sstevel@tonic-gate 
35707c478bd9Sstevel@tonic-gate 			error = kstrgetmsg(SOTOV(so), &mp, uiop,
35717c478bd9Sstevel@tonic-gate 			    &pri, &pflag, (clock_t)-1, &rval);
35727c478bd9Sstevel@tonic-gate 			ASSERT(uiop->uio_resid == saved_resid);
35737c478bd9Sstevel@tonic-gate 
35747c478bd9Sstevel@tonic-gate 			if (error) {
35757c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
35767c478bd9Sstevel@tonic-gate 				if (error != EWOULDBLOCK && error != EINTR) {
35777c478bd9Sstevel@tonic-gate 					eprintsoline(so, error);
35787c478bd9Sstevel@tonic-gate 				}
35797c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
358017169044Sbrutus 				goto out;
35817c478bd9Sstevel@tonic-gate 			}
35827c478bd9Sstevel@tonic-gate 			ASSERT(mp);
35837c478bd9Sstevel@tonic-gate 			tpr = (union T_primitives *)mp->b_rptr;
35847c478bd9Sstevel@tonic-gate 			ASSERT(tpr->type == T_EXDATA_IND);
35857c478bd9Sstevel@tonic-gate 			freemsg(mp);
35867c478bd9Sstevel@tonic-gate 		} /* end "if (flags & MSG_PEEK)" */
35877c478bd9Sstevel@tonic-gate 
35887c478bd9Sstevel@tonic-gate 		/*
35897c478bd9Sstevel@tonic-gate 		 * Decrement the number of queued and pending oob.
35907c478bd9Sstevel@tonic-gate 		 *
35917c478bd9Sstevel@tonic-gate 		 * SS_RCVATMARK is cleared when we read past a mark.
35927c478bd9Sstevel@tonic-gate 		 * SS_HAVEOOBDATA is cleared when we've read past the
35937c478bd9Sstevel@tonic-gate 		 * last mark.
35947c478bd9Sstevel@tonic-gate 		 * SS_OOBPEND is cleared if we've read past the last
35957c478bd9Sstevel@tonic-gate 		 * mark and no (new) SIGURG has been posted.
35967c478bd9Sstevel@tonic-gate 		 */
35977c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
35987c478bd9Sstevel@tonic-gate 		ASSERT(so_verify_oobstate(so));
35990f1702c5SYu Xiangning 		ASSERT(sti->sti_oobsigcnt >= sti->sti_oobcnt);
36000f1702c5SYu Xiangning 		ASSERT(sti->sti_oobsigcnt > 0);
36010f1702c5SYu Xiangning 		sti->sti_oobsigcnt--;
36020f1702c5SYu Xiangning 		ASSERT(sti->sti_oobcnt > 0);
36030f1702c5SYu Xiangning 		sti->sti_oobcnt--;
36047c478bd9Sstevel@tonic-gate 		/*
36057c478bd9Sstevel@tonic-gate 		 * Since the T_EXDATA_IND has been removed from the stream
36067c478bd9Sstevel@tonic-gate 		 * head, but we have not read data past the mark,
36077c478bd9Sstevel@tonic-gate 		 * sockfs needs to track that the socket is still at the mark.
36087c478bd9Sstevel@tonic-gate 		 *
36097c478bd9Sstevel@tonic-gate 		 * Since no data was received call kstrgetmsg again to wait
36107c478bd9Sstevel@tonic-gate 		 * for data.
36117c478bd9Sstevel@tonic-gate 		 */
36127c478bd9Sstevel@tonic-gate 		so->so_state |= SS_RCVATMARK;
36137c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
36147c478bd9Sstevel@tonic-gate 		dprintso(so, 1,
36157c478bd9Sstevel@tonic-gate 		    ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n",
36160f1702c5SYu Xiangning 		    sti->sti_oobsigcnt, sti->sti_oobcnt,
36177c478bd9Sstevel@tonic-gate 		    pr_state(so->so_state, so->so_mode)));
36187c478bd9Sstevel@tonic-gate 		pflag = opflag;
36197c478bd9Sstevel@tonic-gate 		goto retry;
36207c478bd9Sstevel@tonic-gate 	}
36217c478bd9Sstevel@tonic-gate 	default:
36220f1702c5SYu Xiangning 		cmn_err(CE_CONT, "sotpi_recvmsg: so %p prim %d mp %p\n",
36230f1702c5SYu Xiangning 		    (void *)so, tpr->type, (void *)mp);
36247c478bd9Sstevel@tonic-gate 		ASSERT(0);
36257c478bd9Sstevel@tonic-gate 		freemsg(mp);
36267c478bd9Sstevel@tonic-gate 		error = EPROTO;
36277c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
362817169044Sbrutus 		goto out;
36297c478bd9Sstevel@tonic-gate 	}
36307c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
363117169044Sbrutus out:
36327c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
363317169044Sbrutus out_locked:
36347c478bd9Sstevel@tonic-gate 	so_unlock_read(so);	/* Clear SOREADLOCKED */
36357c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
36367c478bd9Sstevel@tonic-gate 	return (error);
36377c478bd9Sstevel@tonic-gate }
36387c478bd9Sstevel@tonic-gate 
36397c478bd9Sstevel@tonic-gate /*
36407c478bd9Sstevel@tonic-gate  * Sending data with options on a datagram socket.
36417c478bd9Sstevel@tonic-gate  * Assumes caller has verified that SS_ISBOUND etc. are set.
36427c478bd9Sstevel@tonic-gate  */
36437c478bd9Sstevel@tonic-gate static int
3644ff550d0eSmasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3645ff550d0eSmasputra     struct uio *uiop, void *control, t_uscalar_t controllen, int flags)
36467c478bd9Sstevel@tonic-gate {
36477c478bd9Sstevel@tonic-gate 	struct T_unitdata_req	tudr;
36487c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
36497c478bd9Sstevel@tonic-gate 	int			error;
36507c478bd9Sstevel@tonic-gate 	void			*addr;
36517c478bd9Sstevel@tonic-gate 	socklen_t		addrlen;
36527c478bd9Sstevel@tonic-gate 	void			*src;
36537c478bd9Sstevel@tonic-gate 	socklen_t		srclen;
36547c478bd9Sstevel@tonic-gate 	ssize_t			len;
36557c478bd9Sstevel@tonic-gate 	int			size;
36567c478bd9Sstevel@tonic-gate 	struct T_opthdr		toh;
36577c478bd9Sstevel@tonic-gate 	struct fdbuf		*fdbuf;
36587c478bd9Sstevel@tonic-gate 	t_uscalar_t		optlen;
36597c478bd9Sstevel@tonic-gate 	void			*fds;
36607c478bd9Sstevel@tonic-gate 	int			fdlen;
36610f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
36627c478bd9Sstevel@tonic-gate 
36637c478bd9Sstevel@tonic-gate 	ASSERT(name && namelen);
36647c478bd9Sstevel@tonic-gate 	ASSERT(control && controllen);
36657c478bd9Sstevel@tonic-gate 
36667c478bd9Sstevel@tonic-gate 	len = uiop->uio_resid;
36670f1702c5SYu Xiangning 	if (len > (ssize_t)sti->sti_tidu_size) {
36687c478bd9Sstevel@tonic-gate 		return (EMSGSIZE);
36697c478bd9Sstevel@tonic-gate 	}
36707c478bd9Sstevel@tonic-gate 
36717c478bd9Sstevel@tonic-gate 	/*
36727c478bd9Sstevel@tonic-gate 	 * For AF_UNIX the destination address is translated to an internal
36737c478bd9Sstevel@tonic-gate 	 * name and the source address is passed as an option.
36747c478bd9Sstevel@tonic-gate 	 * Also, file descriptors are passed as file pointers in an
36757c478bd9Sstevel@tonic-gate 	 * option.
36767c478bd9Sstevel@tonic-gate 	 */
36777c478bd9Sstevel@tonic-gate 
36787c478bd9Sstevel@tonic-gate 	/*
36797c478bd9Sstevel@tonic-gate 	 * Length and family checks.
36807c478bd9Sstevel@tonic-gate 	 */
36817c478bd9Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
36827c478bd9Sstevel@tonic-gate 	if (error) {
36837c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
36847c478bd9Sstevel@tonic-gate 		return (error);
36857c478bd9Sstevel@tonic-gate 	}
36867c478bd9Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
36870f1702c5SYu Xiangning 		if (sti->sti_faddr_noxlate) {
36887c478bd9Sstevel@tonic-gate 			/*
36897c478bd9Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
36907c478bd9Sstevel@tonic-gate 			 * pass any (transport internal) source address.
36917c478bd9Sstevel@tonic-gate 			 */
36927c478bd9Sstevel@tonic-gate 			addr = name;
36937c478bd9Sstevel@tonic-gate 			addrlen = namelen;
36947c478bd9Sstevel@tonic-gate 			src = NULL;
36957c478bd9Sstevel@tonic-gate 			srclen = 0;
36967c478bd9Sstevel@tonic-gate 		} else {
36977c478bd9Sstevel@tonic-gate 			/*
36987c478bd9Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
36997c478bd9Sstevel@tonic-gate 			 * and translate the remote address.
37007c478bd9Sstevel@tonic-gate 			 *
37010f1702c5SYu Xiangning 			 * Note that this code does not prevent sti_laddr_sa
37027c478bd9Sstevel@tonic-gate 			 * from changing while it is being used. Thus
37037c478bd9Sstevel@tonic-gate 			 * if an unbind+bind occurs concurrently with this
37047c478bd9Sstevel@tonic-gate 			 * send the peer might see a partially new and a
37057c478bd9Sstevel@tonic-gate 			 * partially old "from" address.
37067c478bd9Sstevel@tonic-gate 			 */
37070f1702c5SYu Xiangning 			src = sti->sti_laddr_sa;
37080f1702c5SYu Xiangning 			srclen = (t_uscalar_t)sti->sti_laddr_len;
37097c478bd9Sstevel@tonic-gate 			dprintso(so, 1,
37107c478bd9Sstevel@tonic-gate 			    ("sosend_dgramcmsg UNIX: srclen %d, src %p\n",
37117c478bd9Sstevel@tonic-gate 			    srclen, src));
37127c478bd9Sstevel@tonic-gate 			error = so_ux_addr_xlate(so, name, namelen,
37137c478bd9Sstevel@tonic-gate 			    (flags & MSG_XPG4_2),
37147c478bd9Sstevel@tonic-gate 			    &addr, &addrlen);
37157c478bd9Sstevel@tonic-gate 			if (error) {
37167c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
37177c478bd9Sstevel@tonic-gate 				return (error);
37187c478bd9Sstevel@tonic-gate 			}
37197c478bd9Sstevel@tonic-gate 		}
37207c478bd9Sstevel@tonic-gate 	} else {
37217c478bd9Sstevel@tonic-gate 		addr = name;
37227c478bd9Sstevel@tonic-gate 		addrlen = namelen;
37237c478bd9Sstevel@tonic-gate 		src = NULL;
37247c478bd9Sstevel@tonic-gate 		srclen = 0;
37257c478bd9Sstevel@tonic-gate 	}
37267c478bd9Sstevel@tonic-gate 	optlen = so_optlen(control, controllen,
37277c478bd9Sstevel@tonic-gate 	    !(flags & MSG_XPG4_2));
37287c478bd9Sstevel@tonic-gate 	tudr.PRIM_type = T_UNITDATA_REQ;
37297c478bd9Sstevel@tonic-gate 	tudr.DEST_length = addrlen;
37307c478bd9Sstevel@tonic-gate 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
37317c478bd9Sstevel@tonic-gate 	if (srclen != 0)
37327c478bd9Sstevel@tonic-gate 		tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) +
37337c478bd9Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(srclen));
37347c478bd9Sstevel@tonic-gate 	else
37357c478bd9Sstevel@tonic-gate 		tudr.OPT_length = optlen;
37367c478bd9Sstevel@tonic-gate 	tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
37377c478bd9Sstevel@tonic-gate 	    _TPI_ALIGN_TOPT(addrlen));
37387c478bd9Sstevel@tonic-gate 
37397c478bd9Sstevel@tonic-gate 	size = tudr.OPT_offset + tudr.OPT_length;
37407c478bd9Sstevel@tonic-gate 
37417c478bd9Sstevel@tonic-gate 	/*
37427c478bd9Sstevel@tonic-gate 	 * File descriptors only when SM_FDPASSING set.
37437c478bd9Sstevel@tonic-gate 	 */
37447c478bd9Sstevel@tonic-gate 	error = so_getfdopt(control, controllen,
37457c478bd9Sstevel@tonic-gate 	    !(flags & MSG_XPG4_2), &fds, &fdlen);
37467c478bd9Sstevel@tonic-gate 	if (error)
37477c478bd9Sstevel@tonic-gate 		return (error);
37487c478bd9Sstevel@tonic-gate 	if (fdlen != -1) {
37497c478bd9Sstevel@tonic-gate 		if (!(so->so_mode & SM_FDPASSING))
37507c478bd9Sstevel@tonic-gate 			return (EOPNOTSUPP);
37517c478bd9Sstevel@tonic-gate 
37527c478bd9Sstevel@tonic-gate 		error = fdbuf_create(fds, fdlen, &fdbuf);
37537c478bd9Sstevel@tonic-gate 		if (error)
37547c478bd9Sstevel@tonic-gate 			return (error);
37557c478bd9Sstevel@tonic-gate 		mp = fdbuf_allocmsg(size, fdbuf);
37567c478bd9Sstevel@tonic-gate 	} else {
3757de8c4a14SErik Nordmark 		mp = soallocproto(size, _ALLOC_INTR, CRED());
37587c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
37597c478bd9Sstevel@tonic-gate 			/*
37607c478bd9Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
37617c478bd9Sstevel@tonic-gate 			 * Let send* return EINTR.
37627c478bd9Sstevel@tonic-gate 			 */
37637c478bd9Sstevel@tonic-gate 			return (EINTR);
37647c478bd9Sstevel@tonic-gate 		}
3765bd118333Smeem 	}
37667c478bd9Sstevel@tonic-gate 	soappendmsg(mp, &tudr, sizeof (tudr));
37677c478bd9Sstevel@tonic-gate 	soappendmsg(mp, addr, addrlen);
37687c478bd9Sstevel@tonic-gate 	mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
37697c478bd9Sstevel@tonic-gate 
37707c478bd9Sstevel@tonic-gate 	if (fdlen != -1) {
37717c478bd9Sstevel@tonic-gate 		ASSERT(fdbuf != NULL);
37727c478bd9Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
37737c478bd9Sstevel@tonic-gate 		toh.name = SO_FILEP;
37747c478bd9Sstevel@tonic-gate 		toh.len = fdbuf->fd_size +
37757c478bd9Sstevel@tonic-gate 		    (t_uscalar_t)sizeof (struct T_opthdr);
37767c478bd9Sstevel@tonic-gate 		toh.status = 0;
37777c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
37787c478bd9Sstevel@tonic-gate 		soappendmsg(mp, fdbuf, fdbuf->fd_size);
37797c478bd9Sstevel@tonic-gate 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
37807c478bd9Sstevel@tonic-gate 	}
37817c478bd9Sstevel@tonic-gate 	if (srclen != 0) {
37827c478bd9Sstevel@tonic-gate 		/*
37837c478bd9Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
37847c478bd9Sstevel@tonic-gate 		 * address option.
37857c478bd9Sstevel@tonic-gate 		 */
37867c478bd9Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
37877c478bd9Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
37887c478bd9Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
37897c478bd9Sstevel@tonic-gate 		toh.status = 0;
37907c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
37917c478bd9Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
37927c478bd9Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
37937c478bd9Sstevel@tonic-gate 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
37947c478bd9Sstevel@tonic-gate 	}
37957c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
37967c478bd9Sstevel@tonic-gate 	so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
37977c478bd9Sstevel@tonic-gate 	/* At most 3 bytes left in the message */
37987c478bd9Sstevel@tonic-gate 	ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
37997c478bd9Sstevel@tonic-gate 	ASSERT(MBLKL(mp) <= (ssize_t)size);
38007c478bd9Sstevel@tonic-gate 
38017c478bd9Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
3802005d3febSMarek Pospisil 	if (AU_AUDITING())
38037c478bd9Sstevel@tonic-gate 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
38047c478bd9Sstevel@tonic-gate 
38057c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
38067c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
38077c478bd9Sstevel@tonic-gate 	if (error) {
38087c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
38097c478bd9Sstevel@tonic-gate 	}
38107c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
38117c478bd9Sstevel@tonic-gate 	return (error);
38127c478bd9Sstevel@tonic-gate }
38137c478bd9Sstevel@tonic-gate 
38147c478bd9Sstevel@tonic-gate /*
38157c478bd9Sstevel@tonic-gate  * Sending data with options on a connected stream socket.
38167c478bd9Sstevel@tonic-gate  * Assumes caller has verified that SS_ISCONNECTED is set.
38177c478bd9Sstevel@tonic-gate  */
38187c478bd9Sstevel@tonic-gate static int
38190f1702c5SYu Xiangning sosend_svccmsg(struct sonode *so, struct uio *uiop, int more, void *control,
38200f1702c5SYu Xiangning     t_uscalar_t controllen, int flags)
38217c478bd9Sstevel@tonic-gate {
38227c478bd9Sstevel@tonic-gate 	struct T_optdata_req	tdr;
38237c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
38247c478bd9Sstevel@tonic-gate 	int			error;
38257c478bd9Sstevel@tonic-gate 	ssize_t			iosize;
38267c478bd9Sstevel@tonic-gate 	int			size;
38277c478bd9Sstevel@tonic-gate 	struct fdbuf		*fdbuf;
38287c478bd9Sstevel@tonic-gate 	t_uscalar_t		optlen;
38297c478bd9Sstevel@tonic-gate 	void			*fds;
38307c478bd9Sstevel@tonic-gate 	int			fdlen;
38317c478bd9Sstevel@tonic-gate 	struct T_opthdr		toh;
38320f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
38337c478bd9Sstevel@tonic-gate 
38347c478bd9Sstevel@tonic-gate 	dprintso(so, 1,
38357c478bd9Sstevel@tonic-gate 	    ("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
38367c478bd9Sstevel@tonic-gate 
38377c478bd9Sstevel@tonic-gate 	/*
38387c478bd9Sstevel@tonic-gate 	 * Has to be bound and connected. However, since no locks are
38397c478bd9Sstevel@tonic-gate 	 * held the state could have changed after sotpi_sendmsg checked it
38407c478bd9Sstevel@tonic-gate 	 * thus it is not possible to ASSERT on the state.
38417c478bd9Sstevel@tonic-gate 	 */
38427c478bd9Sstevel@tonic-gate 
38437c478bd9Sstevel@tonic-gate 	/* Options on connection-oriented only when SM_OPTDATA set. */
38447c478bd9Sstevel@tonic-gate 	if (!(so->so_mode & SM_OPTDATA))
38457c478bd9Sstevel@tonic-gate 		return (EOPNOTSUPP);
38467c478bd9Sstevel@tonic-gate 
38477c478bd9Sstevel@tonic-gate 	do {
38487c478bd9Sstevel@tonic-gate 		/*
38497c478bd9Sstevel@tonic-gate 		 * Set the MORE flag if uio_resid does not fit in this
38507c478bd9Sstevel@tonic-gate 		 * message or if the caller passed in "more".
38517c478bd9Sstevel@tonic-gate 		 * Error for transports with zero tidu_size.
38527c478bd9Sstevel@tonic-gate 		 */
38537c478bd9Sstevel@tonic-gate 		tdr.PRIM_type = T_OPTDATA_REQ;
38540f1702c5SYu Xiangning 		iosize = sti->sti_tidu_size;
38557c478bd9Sstevel@tonic-gate 		if (iosize <= 0)
38567c478bd9Sstevel@tonic-gate 			return (EMSGSIZE);
38577c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid > iosize) {
38587c478bd9Sstevel@tonic-gate 			tdr.DATA_flag = 1;
38597c478bd9Sstevel@tonic-gate 		} else {
38607c478bd9Sstevel@tonic-gate 			if (more)
38617c478bd9Sstevel@tonic-gate 				tdr.DATA_flag = 1;
38627c478bd9Sstevel@tonic-gate 			else
38637c478bd9Sstevel@tonic-gate 				tdr.DATA_flag = 0;
38647c478bd9Sstevel@tonic-gate 			iosize = uiop->uio_resid;
38657c478bd9Sstevel@tonic-gate 		}
38667c478bd9Sstevel@tonic-gate 		dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
38677c478bd9Sstevel@tonic-gate 		    tdr.DATA_flag, iosize));
38687c478bd9Sstevel@tonic-gate 
38697c478bd9Sstevel@tonic-gate 		optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
38707c478bd9Sstevel@tonic-gate 		tdr.OPT_length = optlen;
38717c478bd9Sstevel@tonic-gate 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
38727c478bd9Sstevel@tonic-gate 
38737c478bd9Sstevel@tonic-gate 		size = (int)sizeof (tdr) + optlen;
38747c478bd9Sstevel@tonic-gate 		/*
38757c478bd9Sstevel@tonic-gate 		 * File descriptors only when SM_FDPASSING set.
38767c478bd9Sstevel@tonic-gate 		 */
38777c478bd9Sstevel@tonic-gate 		error = so_getfdopt(control, controllen,
38787c478bd9Sstevel@tonic-gate 		    !(flags & MSG_XPG4_2), &fds, &fdlen);
38797c478bd9Sstevel@tonic-gate 		if (error)
38807c478bd9Sstevel@tonic-gate 			return (error);
38817c478bd9Sstevel@tonic-gate 		if (fdlen != -1) {
38827c478bd9Sstevel@tonic-gate 			if (!(so->so_mode & SM_FDPASSING))
38837c478bd9Sstevel@tonic-gate 				return (EOPNOTSUPP);
38847c478bd9Sstevel@tonic-gate 
38857c478bd9Sstevel@tonic-gate 			error = fdbuf_create(fds, fdlen, &fdbuf);
38867c478bd9Sstevel@tonic-gate 			if (error)
38877c478bd9Sstevel@tonic-gate 				return (error);
38887c478bd9Sstevel@tonic-gate 			mp = fdbuf_allocmsg(size, fdbuf);
38897c478bd9Sstevel@tonic-gate 		} else {
3890de8c4a14SErik Nordmark 			mp = soallocproto(size, _ALLOC_INTR, CRED());
38917c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
38927c478bd9Sstevel@tonic-gate 				/*
38937c478bd9Sstevel@tonic-gate 				 * Caught a signal waiting for memory.
38947c478bd9Sstevel@tonic-gate 				 * Let send* return EINTR.
38957c478bd9Sstevel@tonic-gate 				 */
38960f1702c5SYu Xiangning 				return (EINTR);
3897bd118333Smeem 			}
38987c478bd9Sstevel@tonic-gate 		}
38997c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &tdr, sizeof (tdr));
39007c478bd9Sstevel@tonic-gate 
39017c478bd9Sstevel@tonic-gate 		if (fdlen != -1) {
39027c478bd9Sstevel@tonic-gate 			ASSERT(fdbuf != NULL);
39037c478bd9Sstevel@tonic-gate 			toh.level = SOL_SOCKET;
39047c478bd9Sstevel@tonic-gate 			toh.name = SO_FILEP;
39057c478bd9Sstevel@tonic-gate 			toh.len = fdbuf->fd_size +
39067c478bd9Sstevel@tonic-gate 			    (t_uscalar_t)sizeof (struct T_opthdr);
39077c478bd9Sstevel@tonic-gate 			toh.status = 0;
39087c478bd9Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
39097c478bd9Sstevel@tonic-gate 			soappendmsg(mp, fdbuf, fdbuf->fd_size);
39107c478bd9Sstevel@tonic-gate 			ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
39117c478bd9Sstevel@tonic-gate 		}
39127c478bd9Sstevel@tonic-gate 		so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
39137c478bd9Sstevel@tonic-gate 		/* At most 3 bytes left in the message */
39147c478bd9Sstevel@tonic-gate 		ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
39157c478bd9Sstevel@tonic-gate 		ASSERT(MBLKL(mp) <= (ssize_t)size);
39167c478bd9Sstevel@tonic-gate 
39177c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
39187c478bd9Sstevel@tonic-gate 
39197c478bd9Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
39207c478bd9Sstevel@tonic-gate 		    0, MSG_BAND, 0);
39217c478bd9Sstevel@tonic-gate 		if (error) {
39227c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
39237c478bd9Sstevel@tonic-gate 			return (error);
39247c478bd9Sstevel@tonic-gate 		}
39257c478bd9Sstevel@tonic-gate 		control = NULL;
39267c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid > 0) {
39277c478bd9Sstevel@tonic-gate 			/*
39287c478bd9Sstevel@tonic-gate 			 * Recheck for fatal errors. Fail write even though
39297c478bd9Sstevel@tonic-gate 			 * some data have been written. This is consistent
39307c478bd9Sstevel@tonic-gate 			 * with strwrite semantics and BSD sockets semantics.
39317c478bd9Sstevel@tonic-gate 			 */
39327c478bd9Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
39337c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
39347c478bd9Sstevel@tonic-gate 				return (EPIPE);
39357c478bd9Sstevel@tonic-gate 			}
39367c478bd9Sstevel@tonic-gate 			if (so->so_error != 0) {
39377c478bd9Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
39380f1702c5SYu Xiangning 				error = sogeterr(so, B_TRUE);
39397c478bd9Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
39407c478bd9Sstevel@tonic-gate 				if (error != 0) {
39417c478bd9Sstevel@tonic-gate 					eprintsoline(so, error);
39427c478bd9Sstevel@tonic-gate 					return (error);
39437c478bd9Sstevel@tonic-gate 				}
39447c478bd9Sstevel@tonic-gate 			}
39457c478bd9Sstevel@tonic-gate 		}
39467c478bd9Sstevel@tonic-gate 	} while (uiop->uio_resid > 0);
39477c478bd9Sstevel@tonic-gate 	return (0);
39487c478bd9Sstevel@tonic-gate }
39497c478bd9Sstevel@tonic-gate 
39507c478bd9Sstevel@tonic-gate /*
39517c478bd9Sstevel@tonic-gate  * Sending data on a datagram socket.
39527c478bd9Sstevel@tonic-gate  * Assumes caller has verified that SS_ISBOUND etc. are set.
39537c478bd9Sstevel@tonic-gate  *
39547c478bd9Sstevel@tonic-gate  * For AF_UNIX the destination address is translated to an internal
39557c478bd9Sstevel@tonic-gate  * name and the source address is passed as an option.
39567c478bd9Sstevel@tonic-gate  */
39577c478bd9Sstevel@tonic-gate int
3958ff550d0eSmasputra sosend_dgram(struct sonode *so, struct sockaddr	*name, socklen_t namelen,
3959ff550d0eSmasputra     struct uio *uiop, int flags)
39607c478bd9Sstevel@tonic-gate {
39617c478bd9Sstevel@tonic-gate 	struct T_unitdata_req	tudr;
39627c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
39637c478bd9Sstevel@tonic-gate 	int			error;
39647c478bd9Sstevel@tonic-gate 	void			*addr;
39657c478bd9Sstevel@tonic-gate 	socklen_t		addrlen;
39667c478bd9Sstevel@tonic-gate 	void			*src;
39677c478bd9Sstevel@tonic-gate 	socklen_t		srclen;
39687c478bd9Sstevel@tonic-gate 	ssize_t			len;
39690f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
39707c478bd9Sstevel@tonic-gate 
3971ff550d0eSmasputra 	ASSERT(name != NULL && namelen != 0);
39727c478bd9Sstevel@tonic-gate 
39737c478bd9Sstevel@tonic-gate 	len = uiop->uio_resid;
39740f1702c5SYu Xiangning 	if (len > sti->sti_tidu_size) {
39757c478bd9Sstevel@tonic-gate 		error = EMSGSIZE;
39767c478bd9Sstevel@tonic-gate 		goto done;
39777c478bd9Sstevel@tonic-gate 	}
39787c478bd9Sstevel@tonic-gate 
3979ff550d0eSmasputra 	/* Length and family checks */
39807c478bd9Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
3981ff550d0eSmasputra 	if (error != 0)
39827c478bd9Sstevel@tonic-gate 		goto done;
3983ff550d0eSmasputra 
39840f1702c5SYu Xiangning 	if (sti->sti_direct)
3985ff550d0eSmasputra 		return (sodgram_direct(so, name, namelen, uiop, flags));
3986ff550d0eSmasputra 
39877c478bd9Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
39880f1702c5SYu Xiangning 		if (sti->sti_faddr_noxlate) {
39897c478bd9Sstevel@tonic-gate 			/*
39907c478bd9Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
39917c478bd9Sstevel@tonic-gate 			 * pass any (transport internal) source address.
39927c478bd9Sstevel@tonic-gate 			 */
39937c478bd9Sstevel@tonic-gate 			addr = name;
39947c478bd9Sstevel@tonic-gate 			addrlen = namelen;
39957c478bd9Sstevel@tonic-gate 			src = NULL;
39967c478bd9Sstevel@tonic-gate 			srclen = 0;
39977c478bd9Sstevel@tonic-gate 		} else {
39987c478bd9Sstevel@tonic-gate 			/*
39997c478bd9Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
40007c478bd9Sstevel@tonic-gate 			 * and translate the remote address.
40017c478bd9Sstevel@tonic-gate 			 *
40020f1702c5SYu Xiangning 			 * Note that this code does not prevent sti_laddr_sa
40037c478bd9Sstevel@tonic-gate 			 * from changing while it is being used. Thus
40047c478bd9Sstevel@tonic-gate 			 * if an unbind+bind occurs concurrently with this
40057c478bd9Sstevel@tonic-gate 			 * send the peer might see a partially new and a
40067c478bd9Sstevel@tonic-gate 			 * partially old "from" address.
40077c478bd9Sstevel@tonic-gate 			 */
40080f1702c5SYu Xiangning 			src = sti->sti_laddr_sa;
40090f1702c5SYu Xiangning 			srclen = (socklen_t)sti->sti_laddr_len;
40107c478bd9Sstevel@tonic-gate 			dprintso(so, 1,
40117c478bd9Sstevel@tonic-gate 			    ("sosend_dgram UNIX: srclen %d, src %p\n",
40127c478bd9Sstevel@tonic-gate 			    srclen, src));
40137c478bd9Sstevel@tonic-gate 			error = so_ux_addr_xlate(so, name, namelen,
40147c478bd9Sstevel@tonic-gate 			    (flags & MSG_XPG4_2),
40157c478bd9Sstevel@tonic-gate 			    &addr, &addrlen);
40167c478bd9Sstevel@tonic-gate 			if (error) {
40177c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
40187c478bd9Sstevel@tonic-gate 				goto done;
40197c478bd9Sstevel@tonic-gate 			}
40207c478bd9Sstevel@tonic-gate 		}
40217c478bd9Sstevel@tonic-gate 	} else {
40227c478bd9Sstevel@tonic-gate 		addr = name;
40237c478bd9Sstevel@tonic-gate 		addrlen = namelen;
40247c478bd9Sstevel@tonic-gate 		src = NULL;
40257c478bd9Sstevel@tonic-gate 		srclen = 0;
40267c478bd9Sstevel@tonic-gate 	}
40277c478bd9Sstevel@tonic-gate 	tudr.PRIM_type = T_UNITDATA_REQ;
40287c478bd9Sstevel@tonic-gate 	tudr.DEST_length = addrlen;
40297c478bd9Sstevel@tonic-gate 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
40307c478bd9Sstevel@tonic-gate 	if (srclen == 0) {
40317c478bd9Sstevel@tonic-gate 		tudr.OPT_length = 0;
40327c478bd9Sstevel@tonic-gate 		tudr.OPT_offset = 0;
40337c478bd9Sstevel@tonic-gate 
40347c478bd9Sstevel@tonic-gate 		mp = soallocproto2(&tudr, sizeof (tudr),
4035de8c4a14SErik Nordmark 		    addr, addrlen, 0, _ALLOC_INTR, CRED());
40367c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
40377c478bd9Sstevel@tonic-gate 			/*
40387c478bd9Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
40397c478bd9Sstevel@tonic-gate 			 * Let send* return EINTR.
40407c478bd9Sstevel@tonic-gate 			 */
40417c478bd9Sstevel@tonic-gate 			error = EINTR;
40427c478bd9Sstevel@tonic-gate 			goto done;
40437c478bd9Sstevel@tonic-gate 		}
40447c478bd9Sstevel@tonic-gate 	} else {
40457c478bd9Sstevel@tonic-gate 		/*
40467c478bd9Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
40477c478bd9Sstevel@tonic-gate 		 * address option.
40487c478bd9Sstevel@tonic-gate 		 */
40497c478bd9Sstevel@tonic-gate 		struct T_opthdr toh;
40507c478bd9Sstevel@tonic-gate 		ssize_t size;
40517c478bd9Sstevel@tonic-gate 
40527c478bd9Sstevel@tonic-gate 		tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
40537c478bd9Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(srclen));
40547c478bd9Sstevel@tonic-gate 		tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
40557c478bd9Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(addrlen));
40567c478bd9Sstevel@tonic-gate 
40577c478bd9Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
40587c478bd9Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
40597c478bd9Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
40607c478bd9Sstevel@tonic-gate 		toh.status = 0;
40617c478bd9Sstevel@tonic-gate 
40627c478bd9Sstevel@tonic-gate 		size = tudr.OPT_offset + tudr.OPT_length;
40637c478bd9Sstevel@tonic-gate 		mp = soallocproto2(&tudr, sizeof (tudr),
4064de8c4a14SErik Nordmark 		    addr, addrlen, size, _ALLOC_INTR, CRED());
40657c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
40667c478bd9Sstevel@tonic-gate 			/*
40677c478bd9Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
40687c478bd9Sstevel@tonic-gate 			 * Let send* return EINTR.
40697c478bd9Sstevel@tonic-gate 			 */
40707c478bd9Sstevel@tonic-gate 			error = EINTR;
40717c478bd9Sstevel@tonic-gate 			goto done;
40727c478bd9Sstevel@tonic-gate 		}
40737c478bd9Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
40747c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
40757c478bd9Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
40767c478bd9Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
40777c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
40787c478bd9Sstevel@tonic-gate 	}
40797c478bd9Sstevel@tonic-gate 
4080005d3febSMarek Pospisil 	if (AU_AUDITING())
40817c478bd9Sstevel@tonic-gate 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
40827c478bd9Sstevel@tonic-gate 
40837c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
40847c478bd9Sstevel@tonic-gate done:
40857c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
40867c478bd9Sstevel@tonic-gate 	if (error) {
40877c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
40887c478bd9Sstevel@tonic-gate 	}
40897c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
40907c478bd9Sstevel@tonic-gate 	return (error);
40917c478bd9Sstevel@tonic-gate }
40927c478bd9Sstevel@tonic-gate 
40937c478bd9Sstevel@tonic-gate /*
40947c478bd9Sstevel@tonic-gate  * Sending data on a connected stream socket.
40957c478bd9Sstevel@tonic-gate  * Assumes caller has verified that SS_ISCONNECTED is set.
40967c478bd9Sstevel@tonic-gate  */
40977c478bd9Sstevel@tonic-gate int
40980f1702c5SYu Xiangning sosend_svc(struct sonode *so, struct uio *uiop, t_scalar_t prim, int more,
40997c478bd9Sstevel@tonic-gate     int sflag)
41007c478bd9Sstevel@tonic-gate {
41017c478bd9Sstevel@tonic-gate 	struct T_data_req	tdr;
41027c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
41037c478bd9Sstevel@tonic-gate 	int			error;
41047c478bd9Sstevel@tonic-gate 	ssize_t			iosize;
41050f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
41067c478bd9Sstevel@tonic-gate 
41077c478bd9Sstevel@tonic-gate 	dprintso(so, 1,
41087c478bd9Sstevel@tonic-gate 	    ("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
4109903a11ebSrh87107 	    (void *)so, uiop->uio_resid, prim, sflag));
41107c478bd9Sstevel@tonic-gate 
41117c478bd9Sstevel@tonic-gate 	/*
41127c478bd9Sstevel@tonic-gate 	 * Has to be bound and connected. However, since no locks are
41137c478bd9Sstevel@tonic-gate 	 * held the state could have changed after sotpi_sendmsg checked it
41147c478bd9Sstevel@tonic-gate 	 * thus it is not possible to ASSERT on the state.
41157c478bd9Sstevel@tonic-gate 	 */
41167c478bd9Sstevel@tonic-gate 
41177c478bd9Sstevel@tonic-gate 	do {
41187c478bd9Sstevel@tonic-gate 		/*
41197c478bd9Sstevel@tonic-gate 		 * Set the MORE flag if uio_resid does not fit in this
41207c478bd9Sstevel@tonic-gate 		 * message or if the caller passed in "more".
41217c478bd9Sstevel@tonic-gate 		 * Error for transports with zero tidu_size.
41227c478bd9Sstevel@tonic-gate 		 */
41237c478bd9Sstevel@tonic-gate 		tdr.PRIM_type = prim;
41240f1702c5SYu Xiangning 		iosize = sti->sti_tidu_size;
41257c478bd9Sstevel@tonic-gate 		if (iosize <= 0)
41267c478bd9Sstevel@tonic-gate 			return (EMSGSIZE);
41277c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid > iosize) {
41287c478bd9Sstevel@tonic-gate 			tdr.MORE_flag = 1;
41297c478bd9Sstevel@tonic-gate 		} else {
41307c478bd9Sstevel@tonic-gate 			if (more)
41317c478bd9Sstevel@tonic-gate 				tdr.MORE_flag = 1;
41327c478bd9Sstevel@tonic-gate 			else
41337c478bd9Sstevel@tonic-gate 				tdr.MORE_flag = 0;
41347c478bd9Sstevel@tonic-gate 			iosize = uiop->uio_resid;
41357c478bd9Sstevel@tonic-gate 		}
41367c478bd9Sstevel@tonic-gate 		dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
41377c478bd9Sstevel@tonic-gate 		    prim, tdr.MORE_flag, iosize));
4138de8c4a14SErik Nordmark 		mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR, CRED());
41397c478bd9Sstevel@tonic-gate 		if (mp == NULL) {
41407c478bd9Sstevel@tonic-gate 			/*
41417c478bd9Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
41427c478bd9Sstevel@tonic-gate 			 * Let send* return EINTR.
41437c478bd9Sstevel@tonic-gate 			 */
41447c478bd9Sstevel@tonic-gate 			return (EINTR);
41457c478bd9Sstevel@tonic-gate 		}
41467c478bd9Sstevel@tonic-gate 
41477c478bd9Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
41487c478bd9Sstevel@tonic-gate 		    0, sflag | MSG_BAND, 0);
41497c478bd9Sstevel@tonic-gate 		if (error) {
41507c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
41517c478bd9Sstevel@tonic-gate 			return (error);
41527c478bd9Sstevel@tonic-gate 		}
41537c478bd9Sstevel@tonic-gate 		if (uiop->uio_resid > 0) {
41547c478bd9Sstevel@tonic-gate 			/*
41557c478bd9Sstevel@tonic-gate 			 * Recheck for fatal errors. Fail write even though
41567c478bd9Sstevel@tonic-gate 			 * some data have been written. This is consistent
41577c478bd9Sstevel@tonic-gate 			 * with strwrite semantics and BSD sockets semantics.
41587c478bd9Sstevel@tonic-gate 			 */
41597c478bd9Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
41607c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
41617c478bd9Sstevel@tonic-gate 				return (EPIPE);
41627c478bd9Sstevel@tonic-gate 			}
41637c478bd9Sstevel@tonic-gate 			if (so->so_error != 0) {
41647c478bd9Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
41650f1702c5SYu Xiangning 				error = sogeterr(so, B_TRUE);
41667c478bd9Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
41677c478bd9Sstevel@tonic-gate 				if (error != 0) {
41687c478bd9Sstevel@tonic-gate 					eprintsoline(so, error);
41697c478bd9Sstevel@tonic-gate 					return (error);
41707c478bd9Sstevel@tonic-gate 				}
41717c478bd9Sstevel@tonic-gate 			}
41727c478bd9Sstevel@tonic-gate 		}
41737c478bd9Sstevel@tonic-gate 	} while (uiop->uio_resid > 0);
41747c478bd9Sstevel@tonic-gate 	return (0);
41757c478bd9Sstevel@tonic-gate }
41767c478bd9Sstevel@tonic-gate 
41777c478bd9Sstevel@tonic-gate /*
41787c478bd9Sstevel@tonic-gate  * Check the state for errors and call the appropriate send function.
41797c478bd9Sstevel@tonic-gate  *
41807c478bd9Sstevel@tonic-gate  * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
41817c478bd9Sstevel@tonic-gate  * this function issues a setsockopt to toggle SO_DONTROUTE before and
41827c478bd9Sstevel@tonic-gate  * after sending the message.
41837c478bd9Sstevel@tonic-gate  */
41847c478bd9Sstevel@tonic-gate static int
41850f1702c5SYu Xiangning sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop,
41860f1702c5SYu Xiangning     struct cred *cr)
41877c478bd9Sstevel@tonic-gate {
41887c478bd9Sstevel@tonic-gate 	int		so_state;
41897c478bd9Sstevel@tonic-gate 	int		so_mode;
41907c478bd9Sstevel@tonic-gate 	int		error;
41917c478bd9Sstevel@tonic-gate 	struct sockaddr *name;
41927c478bd9Sstevel@tonic-gate 	t_uscalar_t	namelen;
41937c478bd9Sstevel@tonic-gate 	int		dontroute;
41947c478bd9Sstevel@tonic-gate 	int		flags;
41950f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
41967c478bd9Sstevel@tonic-gate 
41977c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
4198903a11ebSrh87107 	    (void *)so, (void *)msg, msg->msg_flags,
41997c478bd9Sstevel@tonic-gate 	    pr_state(so->so_state, so->so_mode), so->so_error));
42007c478bd9Sstevel@tonic-gate 
42010f1702c5SYu Xiangning 	if (so->so_version == SOV_STREAM) {
42020f1702c5SYu Xiangning 		/* The imaginary "sockmod" has been popped - act as a stream */
42030f1702c5SYu Xiangning 		so_update_attrs(so, SOMOD);
42040f1702c5SYu Xiangning 		return (strwrite(SOTOV(so), uiop, cr));
42050f1702c5SYu Xiangning 	}
42060f1702c5SYu Xiangning 
42077c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
42087c478bd9Sstevel@tonic-gate 	so_state = so->so_state;
42097c478bd9Sstevel@tonic-gate 
42107c478bd9Sstevel@tonic-gate 	if (so_state & SS_CANTSENDMORE) {
42117c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
42127c478bd9Sstevel@tonic-gate 		return (EPIPE);
42137c478bd9Sstevel@tonic-gate 	}
42147c478bd9Sstevel@tonic-gate 
42157c478bd9Sstevel@tonic-gate 	if (so->so_error != 0) {
42160f1702c5SYu Xiangning 		error = sogeterr(so, B_TRUE);
42177c478bd9Sstevel@tonic-gate 		if (error != 0) {
42187c478bd9Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42197c478bd9Sstevel@tonic-gate 			return (error);
42207c478bd9Sstevel@tonic-gate 		}
42217c478bd9Sstevel@tonic-gate 	}
42227c478bd9Sstevel@tonic-gate 
42237c478bd9Sstevel@tonic-gate 	name = (struct sockaddr *)msg->msg_name;
42247c478bd9Sstevel@tonic-gate 	namelen = msg->msg_namelen;
42257c478bd9Sstevel@tonic-gate 
42267c478bd9Sstevel@tonic-gate 	so_mode = so->so_mode;
42277c478bd9Sstevel@tonic-gate 
42287c478bd9Sstevel@tonic-gate 	if (name == NULL) {
42297c478bd9Sstevel@tonic-gate 		if (!(so_state & SS_ISCONNECTED)) {
42307c478bd9Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42317c478bd9Sstevel@tonic-gate 			if (so_mode & SM_CONNREQUIRED)
42327c478bd9Sstevel@tonic-gate 				return (ENOTCONN);
42337c478bd9Sstevel@tonic-gate 			else
42347c478bd9Sstevel@tonic-gate 				return (EDESTADDRREQ);
42357c478bd9Sstevel@tonic-gate 		}
42367c478bd9Sstevel@tonic-gate 		if (so_mode & SM_CONNREQUIRED) {
42377c478bd9Sstevel@tonic-gate 			name = NULL;
42387c478bd9Sstevel@tonic-gate 			namelen = 0;
42397c478bd9Sstevel@tonic-gate 		} else {
42407c478bd9Sstevel@tonic-gate 			/*
42410f1702c5SYu Xiangning 			 * Note that this code does not prevent sti_faddr_sa
42427c478bd9Sstevel@tonic-gate 			 * from changing while it is being used. Thus
42437c478bd9Sstevel@tonic-gate 			 * if an "unconnect"+connect occurs concurrently with
42447c478bd9Sstevel@tonic-gate 			 * this send the datagram might be delivered to a
42457c478bd9Sstevel@tonic-gate 			 * garbaled address.
42467c478bd9Sstevel@tonic-gate 			 */
42470f1702c5SYu Xiangning 			ASSERT(sti->sti_faddr_sa);
42480f1702c5SYu Xiangning 			name = sti->sti_faddr_sa;
42490f1702c5SYu Xiangning 			namelen = (t_uscalar_t)sti->sti_faddr_len;
42507c478bd9Sstevel@tonic-gate 		}
42517c478bd9Sstevel@tonic-gate 	} else {
42527c478bd9Sstevel@tonic-gate 		if (!(so_state & SS_ISCONNECTED) &&
42537c478bd9Sstevel@tonic-gate 		    (so_mode & SM_CONNREQUIRED)) {
42547c478bd9Sstevel@tonic-gate 			/* Required but not connected */
42557c478bd9Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42567c478bd9Sstevel@tonic-gate 			return (ENOTCONN);
42577c478bd9Sstevel@tonic-gate 		}
42587c478bd9Sstevel@tonic-gate 		/*
42597c478bd9Sstevel@tonic-gate 		 * Ignore the address on connection-oriented sockets.
42607c478bd9Sstevel@tonic-gate 		 * Just like BSD this code does not generate an error for
42617c478bd9Sstevel@tonic-gate 		 * TCP (a CONNREQUIRED socket) when sending to an address
42627c478bd9Sstevel@tonic-gate 		 * passed in with sendto/sendmsg. Instead the data is
42637c478bd9Sstevel@tonic-gate 		 * delivered on the connection as if no address had been
42647c478bd9Sstevel@tonic-gate 		 * supplied.
42657c478bd9Sstevel@tonic-gate 		 */
42667c478bd9Sstevel@tonic-gate 		if ((so_state & SS_ISCONNECTED) &&
42677c478bd9Sstevel@tonic-gate 		    !(so_mode & SM_CONNREQUIRED)) {
42687c478bd9Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
42697c478bd9Sstevel@tonic-gate 			return (EISCONN);
42707c478bd9Sstevel@tonic-gate 		}
42717c478bd9Sstevel@tonic-gate 		if (!(so_state & SS_ISBOUND)) {
42727c478bd9Sstevel@tonic-gate 			so_lock_single(so);	/* Set SOLOCKED */
42737c478bd9Sstevel@tonic-gate 			error = sotpi_bind(so, NULL, 0,
42740f1702c5SYu Xiangning 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD, cr);
42757c478bd9Sstevel@tonic-gate 			so_unlock_single(so, SOLOCKED);
42767c478bd9Sstevel@tonic-gate 			if (error) {
42777c478bd9Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
42787c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
42797c478bd9Sstevel@tonic-gate 				return (error);
42807c478bd9Sstevel@tonic-gate 			}
42817c478bd9Sstevel@tonic-gate 		}
42827c478bd9Sstevel@tonic-gate 		/*
42837c478bd9Sstevel@tonic-gate 		 * Handle delayed datagram errors. These are only queued
42847c478bd9Sstevel@tonic-gate 		 * when the application sets SO_DGRAM_ERRIND.
42857c478bd9Sstevel@tonic-gate 		 * Return the error if we are sending to the address
42867c478bd9Sstevel@tonic-gate 		 * that was returned in the last T_UDERROR_IND.
42877c478bd9Sstevel@tonic-gate 		 * If sending to some other address discard the delayed
42887c478bd9Sstevel@tonic-gate 		 * error indication.
42897c478bd9Sstevel@tonic-gate 		 */
42900f1702c5SYu Xiangning 		if (sti->sti_delayed_error) {
42917c478bd9Sstevel@tonic-gate 			struct T_uderror_ind	*tudi;
42927c478bd9Sstevel@tonic-gate 			void			*addr;
42937c478bd9Sstevel@tonic-gate 			t_uscalar_t		addrlen;
42947c478bd9Sstevel@tonic-gate 			boolean_t		match = B_FALSE;
42957c478bd9Sstevel@tonic-gate 
42960f1702c5SYu Xiangning 			ASSERT(sti->sti_eaddr_mp);
42970f1702c5SYu Xiangning 			error = sti->sti_delayed_error;
42980f1702c5SYu Xiangning 			sti->sti_delayed_error = 0;
42990f1702c5SYu Xiangning 			tudi =
43000f1702c5SYu Xiangning 			    (struct T_uderror_ind *)sti->sti_eaddr_mp->b_rptr;
43017c478bd9Sstevel@tonic-gate 			addrlen = tudi->DEST_length;
43020f1702c5SYu Xiangning 			addr = sogetoff(sti->sti_eaddr_mp,
43030f1702c5SYu Xiangning 			    tudi->DEST_offset, addrlen, 1);
43047c478bd9Sstevel@tonic-gate 			ASSERT(addr);	/* Checked by strsock_proto */
43057c478bd9Sstevel@tonic-gate 			switch (so->so_family) {
43067c478bd9Sstevel@tonic-gate 			case AF_INET: {
43077c478bd9Sstevel@tonic-gate 				/* Compare just IP address and port */
43087c478bd9Sstevel@tonic-gate 				sin_t *sin1 = (sin_t *)name;
43097c478bd9Sstevel@tonic-gate 				sin_t *sin2 = (sin_t *)addr;
43107c478bd9Sstevel@tonic-gate 
43117c478bd9Sstevel@tonic-gate 				if (addrlen == sizeof (sin_t) &&
43127c478bd9Sstevel@tonic-gate 				    namelen == addrlen &&
43137c478bd9Sstevel@tonic-gate 				    sin1->sin_port == sin2->sin_port &&
43147c478bd9Sstevel@tonic-gate 				    sin1->sin_addr.s_addr ==
43157c478bd9Sstevel@tonic-gate 				    sin2->sin_addr.s_addr)
43167c478bd9Sstevel@tonic-gate 					match = B_TRUE;
43177c478bd9Sstevel@tonic-gate 				break;
43187c478bd9Sstevel@tonic-gate 			}
43197c478bd9Sstevel@tonic-gate 			case AF_INET6: {
43207c478bd9Sstevel@tonic-gate 				/* Compare just IP address and port. Not flow */
43217c478bd9Sstevel@tonic-gate 				sin6_t *sin1 = (sin6_t *)name;
43227c478bd9Sstevel@tonic-gate 				sin6_t *sin2 = (sin6_t *)addr;
43237c478bd9Sstevel@tonic-gate 
43247c478bd9Sstevel@tonic-gate 				if (addrlen == sizeof (sin6_t) &&
43257c478bd9Sstevel@tonic-gate 				    namelen == addrlen &&
43267c478bd9Sstevel@tonic-gate 				    sin1->sin6_port == sin2->sin6_port &&
43277c478bd9Sstevel@tonic-gate 				    IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
43287c478bd9Sstevel@tonic-gate 				    &sin2->sin6_addr))
43297c478bd9Sstevel@tonic-gate 					match = B_TRUE;
43307c478bd9Sstevel@tonic-gate 				break;
43317c478bd9Sstevel@tonic-gate 			}
43327c478bd9Sstevel@tonic-gate 			case AF_UNIX:
43337c478bd9Sstevel@tonic-gate 			default:
43347c478bd9Sstevel@tonic-gate 				if (namelen == addrlen &&
43357c478bd9Sstevel@tonic-gate 				    bcmp(name, addr, namelen) == 0)
43367c478bd9Sstevel@tonic-gate 					match = B_TRUE;
43377c478bd9Sstevel@tonic-gate 			}
43387c478bd9Sstevel@tonic-gate 			if (match) {
43390f1702c5SYu Xiangning 				freemsg(sti->sti_eaddr_mp);
43400f1702c5SYu Xiangning 				sti->sti_eaddr_mp = NULL;
43417c478bd9Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
43427c478bd9Sstevel@tonic-gate #ifdef DEBUG
43437c478bd9Sstevel@tonic-gate 				dprintso(so, 0,
43447c478bd9Sstevel@tonic-gate 				    ("sockfs delayed error %d for %s\n",
43457c478bd9Sstevel@tonic-gate 				    error,
43467c478bd9Sstevel@tonic-gate 				    pr_addr(so->so_family, name, namelen)));
43477c478bd9Sstevel@tonic-gate #endif /* DEBUG */
43487c478bd9Sstevel@tonic-gate 				return (error);
43497c478bd9Sstevel@tonic-gate 			}
43500f1702c5SYu Xiangning 			freemsg(sti->sti_eaddr_mp);
43510f1702c5SYu Xiangning 			sti->sti_eaddr_mp = NULL;
43527c478bd9Sstevel@tonic-gate 		}
43537c478bd9Sstevel@tonic-gate 	}
43547c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
43557c478bd9Sstevel@tonic-gate 
43567c478bd9Sstevel@tonic-gate 	flags = msg->msg_flags;
43577c478bd9Sstevel@tonic-gate 	dontroute = 0;
43587c478bd9Sstevel@tonic-gate 	if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
43597c478bd9Sstevel@tonic-gate 		uint32_t	val;
43607c478bd9Sstevel@tonic-gate 
43617c478bd9Sstevel@tonic-gate 		val = 1;
43627c478bd9Sstevel@tonic-gate 		error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
43630f1702c5SYu Xiangning 		    &val, (t_uscalar_t)sizeof (val), cr);
43647c478bd9Sstevel@tonic-gate 		if (error)
43657c478bd9Sstevel@tonic-gate 			return (error);
43667c478bd9Sstevel@tonic-gate 		dontroute = 1;
43677c478bd9Sstevel@tonic-gate 	}
43687c478bd9Sstevel@tonic-gate 
43697c478bd9Sstevel@tonic-gate 	if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
43707c478bd9Sstevel@tonic-gate 		error = EOPNOTSUPP;
43717c478bd9Sstevel@tonic-gate 		goto done;
43727c478bd9Sstevel@tonic-gate 	}
43737c478bd9Sstevel@tonic-gate 	if (msg->msg_controllen != 0) {
43747c478bd9Sstevel@tonic-gate 		if (!(so_mode & SM_CONNREQUIRED)) {
43750f1702c5SYu Xiangning 			so_update_attrs(so, SOMOD);
43767c478bd9Sstevel@tonic-gate 			error = sosend_dgramcmsg(so, name, namelen, uiop,
4377ff550d0eSmasputra 			    msg->msg_control, msg->msg_controllen, flags);
43787c478bd9Sstevel@tonic-gate 		} else {
43797c478bd9Sstevel@tonic-gate 			if (flags & MSG_OOB) {
43807c478bd9Sstevel@tonic-gate 				/* Can't generate T_EXDATA_REQ with options */
43817c478bd9Sstevel@tonic-gate 				error = EOPNOTSUPP;
43827c478bd9Sstevel@tonic-gate 				goto done;
43837c478bd9Sstevel@tonic-gate 			}
43840f1702c5SYu Xiangning 			so_update_attrs(so, SOMOD);
43857c478bd9Sstevel@tonic-gate 			error = sosend_svccmsg(so, uiop,
43867c478bd9Sstevel@tonic-gate 			    !(flags & MSG_EOR),
43877c478bd9Sstevel@tonic-gate 			    msg->msg_control, msg->msg_controllen,
43887c478bd9Sstevel@tonic-gate 			    flags);
43897c478bd9Sstevel@tonic-gate 		}
43907c478bd9Sstevel@tonic-gate 		goto done;
43917c478bd9Sstevel@tonic-gate 	}
43927c478bd9Sstevel@tonic-gate 
43930f1702c5SYu Xiangning 	so_update_attrs(so, SOMOD);
43947c478bd9Sstevel@tonic-gate 	if (!(so_mode & SM_CONNREQUIRED)) {
43957c478bd9Sstevel@tonic-gate 		/*
43967c478bd9Sstevel@tonic-gate 		 * If there is no SO_DONTROUTE to turn off return immediately
4397ff550d0eSmasputra 		 * from send_dgram. This can allow tail-call optimizations.
43987c478bd9Sstevel@tonic-gate 		 */
43997c478bd9Sstevel@tonic-gate 		if (!dontroute) {
44007c478bd9Sstevel@tonic-gate 			return (sosend_dgram(so, name, namelen, uiop, flags));
44017c478bd9Sstevel@tonic-gate 		}
44027c478bd9Sstevel@tonic-gate 		error = sosend_dgram(so, name, namelen, uiop, flags);
44037c478bd9Sstevel@tonic-gate 	} else {
44047c478bd9Sstevel@tonic-gate 		t_scalar_t prim;
44057c478bd9Sstevel@tonic-gate 		int sflag;
44067c478bd9Sstevel@tonic-gate 
44077c478bd9Sstevel@tonic-gate 		/* Ignore msg_name in the connected state */
44087c478bd9Sstevel@tonic-gate 		if (flags & MSG_OOB) {
44097c478bd9Sstevel@tonic-gate 			prim = T_EXDATA_REQ;
44107c478bd9Sstevel@tonic-gate 			/*
44117c478bd9Sstevel@tonic-gate 			 * Send down T_EXDATA_REQ even if there is flow
44127c478bd9Sstevel@tonic-gate 			 * control for data.
44137c478bd9Sstevel@tonic-gate 			 */
44147c478bd9Sstevel@tonic-gate 			sflag = MSG_IGNFLOW;
44157c478bd9Sstevel@tonic-gate 		} else {
44167c478bd9Sstevel@tonic-gate 			if (so_mode & SM_BYTESTREAM) {
44177c478bd9Sstevel@tonic-gate 				/* Byte stream transport - use write */
44187c478bd9Sstevel@tonic-gate 				dprintso(so, 1, ("sotpi_sendmsg: write\n"));
44190f1702c5SYu Xiangning 
44200f1702c5SYu Xiangning 				/* Send M_DATA messages */
44210f1702c5SYu Xiangning 				if ((sti->sti_nl7c_flags & NL7C_ENABLED) &&
44220f1702c5SYu Xiangning 				    (error = nl7c_data(so, uiop)) >= 0) {
44230f1702c5SYu Xiangning 					/* NL7C consumed the data */
44240f1702c5SYu Xiangning 					return (error);
44250f1702c5SYu Xiangning 				}
44267c478bd9Sstevel@tonic-gate 				/*
4427ff550d0eSmasputra 				 * If there is no SO_DONTROUTE to turn off,
44280f1702c5SYu Xiangning 				 * sti_direct is on, and there is no flow
4429ff550d0eSmasputra 				 * control, we can take the fast path.
44307c478bd9Sstevel@tonic-gate 				 */
44310f1702c5SYu Xiangning 				if (!dontroute && sti->sti_direct != 0 &&
4432ff550d0eSmasputra 				    canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4433ff550d0eSmasputra 					return (sostream_direct(so, uiop,
44340f1702c5SYu Xiangning 					    NULL, cr));
4435ff550d0eSmasputra 				}
44360f1702c5SYu Xiangning 				error = strwrite(SOTOV(so), uiop, cr);
44377c478bd9Sstevel@tonic-gate 				goto done;
44387c478bd9Sstevel@tonic-gate 			}
44397c478bd9Sstevel@tonic-gate 			prim = T_DATA_REQ;
44407c478bd9Sstevel@tonic-gate 			sflag = 0;
44417c478bd9Sstevel@tonic-gate 		}
44427c478bd9Sstevel@tonic-gate 		/*
44437c478bd9Sstevel@tonic-gate 		 * If there is no SO_DONTROUTE to turn off return immediately
44447c478bd9Sstevel@tonic-gate 		 * from sosend_svc. This can allow tail-call optimizations.
44457c478bd9Sstevel@tonic-gate 		 */
44467c478bd9Sstevel@tonic-gate 		if (!dontroute)
44477c478bd9Sstevel@tonic-gate 			return (sosend_svc(so, uiop, prim,
44487c478bd9Sstevel@tonic-gate 			    !(flags & MSG_EOR), sflag));
44497c478bd9Sstevel@tonic-gate 		error = sosend_svc(so, uiop, prim,
44507c478bd9Sstevel@tonic-gate 		    !(flags & MSG_EOR), sflag);
44517c478bd9Sstevel@tonic-gate 	}
44527c478bd9Sstevel@tonic-gate 	ASSERT(dontroute);
44537c478bd9Sstevel@tonic-gate done:
44547c478bd9Sstevel@tonic-gate 	if (dontroute) {
44557c478bd9Sstevel@tonic-gate 		uint32_t	val;
44567c478bd9Sstevel@tonic-gate 
44577c478bd9Sstevel@tonic-gate 		val = 0;
44587c478bd9Sstevel@tonic-gate 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
44590f1702c5SYu Xiangning 		    &val, (t_uscalar_t)sizeof (val), cr);
44607c478bd9Sstevel@tonic-gate 	}
44617c478bd9Sstevel@tonic-gate 	return (error);
44627c478bd9Sstevel@tonic-gate }
44637c478bd9Sstevel@tonic-gate 
44647c478bd9Sstevel@tonic-gate /*
44650f1702c5SYu Xiangning  * kstrwritemp() has very similar semantics as that of strwrite().
44660f1702c5SYu Xiangning  * The main difference is it obtains mblks from the caller and also
44670f1702c5SYu Xiangning  * does not do any copy as done in strwrite() from user buffers to
44680f1702c5SYu Xiangning  * kernel buffers.
44690f1702c5SYu Xiangning  *
44700f1702c5SYu Xiangning  * Currently, this routine is used by sendfile to send data allocated
44710f1702c5SYu Xiangning  * within the kernel without any copying. This interface does not use the
44720f1702c5SYu Xiangning  * synchronous stream interface as synch. stream interface implies
44730f1702c5SYu Xiangning  * copying.
44740f1702c5SYu Xiangning  */
44750f1702c5SYu Xiangning int
44760f1702c5SYu Xiangning kstrwritemp(struct vnode *vp, mblk_t *mp, ushort_t fmode)
44770f1702c5SYu Xiangning {
44780f1702c5SYu Xiangning 	struct stdata *stp;
44790f1702c5SYu Xiangning 	struct queue *wqp;
44800f1702c5SYu Xiangning 	mblk_t *newmp;
44810f1702c5SYu Xiangning 	char waitflag;
44820f1702c5SYu Xiangning 	int tempmode;
44830f1702c5SYu Xiangning 	int error = 0;
44840f1702c5SYu Xiangning 	int done = 0;
44850f1702c5SYu Xiangning 	struct sonode *so;
44860f1702c5SYu Xiangning 	boolean_t direct;
44870f1702c5SYu Xiangning 
44880f1702c5SYu Xiangning 	ASSERT(vp->v_stream);
44890f1702c5SYu Xiangning 	stp = vp->v_stream;
44900f1702c5SYu Xiangning 
44910f1702c5SYu Xiangning 	so = VTOSO(vp);
44920f1702c5SYu Xiangning 	direct = _SOTOTPI(so)->sti_direct;
44930f1702c5SYu Xiangning 
44940f1702c5SYu Xiangning 	/*
44950f1702c5SYu Xiangning 	 * This is the sockfs direct fast path. canputnext() need
44960f1702c5SYu Xiangning 	 * not be accurate so we don't grab the sd_lock here. If
44970f1702c5SYu Xiangning 	 * we get flow-controlled, we grab sd_lock just before the
44980f1702c5SYu Xiangning 	 * do..while loop below to emulate what strwrite() does.
44990f1702c5SYu Xiangning 	 */
45000f1702c5SYu Xiangning 	wqp = stp->sd_wrq;
45010f1702c5SYu Xiangning 	if (canputnext(wqp) && direct &&
45020f1702c5SYu Xiangning 	    !(stp->sd_flag & (STWRERR|STRHUP|STPLEX))) {
45030f1702c5SYu Xiangning 		return (sostream_direct(so, NULL, mp, CRED()));
45040f1702c5SYu Xiangning 	} else if (stp->sd_flag & (STWRERR|STRHUP|STPLEX)) {
45050f1702c5SYu Xiangning 		/* Fast check of flags before acquiring the lock */
45060f1702c5SYu Xiangning 		mutex_enter(&stp->sd_lock);
45070f1702c5SYu Xiangning 		error = strgeterr(stp, STWRERR|STRHUP|STPLEX, 0);
45080f1702c5SYu Xiangning 		mutex_exit(&stp->sd_lock);
45090f1702c5SYu Xiangning 		if (error != 0) {
45100f1702c5SYu Xiangning 			if (!(stp->sd_flag & STPLEX) &&
45110f1702c5SYu Xiangning 			    (stp->sd_wput_opt & SW_SIGPIPE)) {
45120f1702c5SYu Xiangning 				error = EPIPE;
45130f1702c5SYu Xiangning 			}
45140f1702c5SYu Xiangning 			return (error);
45150f1702c5SYu Xiangning 		}
45160f1702c5SYu Xiangning 	}
45170f1702c5SYu Xiangning 
45180f1702c5SYu Xiangning 	waitflag = WRITEWAIT;
45190f1702c5SYu Xiangning 	if (stp->sd_flag & OLDNDELAY)
45200f1702c5SYu Xiangning 		tempmode = fmode & ~FNDELAY;
45210f1702c5SYu Xiangning 	else
45220f1702c5SYu Xiangning 		tempmode = fmode;
45230f1702c5SYu Xiangning 
45240f1702c5SYu Xiangning 	mutex_enter(&stp->sd_lock);
45250f1702c5SYu Xiangning 	do {
45260f1702c5SYu Xiangning 		if (canputnext(wqp)) {
45270f1702c5SYu Xiangning 			mutex_exit(&stp->sd_lock);
45280f1702c5SYu Xiangning 			if (stp->sd_wputdatafunc != NULL) {
45290f1702c5SYu Xiangning 				newmp = (stp->sd_wputdatafunc)(vp, mp, NULL,
45300f1702c5SYu Xiangning 				    NULL, NULL, NULL);
45310f1702c5SYu Xiangning 				if (newmp == NULL) {
45320f1702c5SYu Xiangning 					/* The caller will free mp */
45330f1702c5SYu Xiangning 					return (ECOMM);
45340f1702c5SYu Xiangning 				}
45350f1702c5SYu Xiangning 				mp = newmp;
45360f1702c5SYu Xiangning 			}
45370f1702c5SYu Xiangning 			putnext(wqp, mp);
45380f1702c5SYu Xiangning 			return (0);
45390f1702c5SYu Xiangning 		}
45400f1702c5SYu Xiangning 		error = strwaitq(stp, waitflag, (ssize_t)0, tempmode, -1,
45410f1702c5SYu Xiangning 		    &done);
45420f1702c5SYu Xiangning 	} while (error == 0 && !done);
45430f1702c5SYu Xiangning 
45440f1702c5SYu Xiangning 	mutex_exit(&stp->sd_lock);
45450f1702c5SYu Xiangning 	/*
45460f1702c5SYu Xiangning 	 * EAGAIN tells the application to try again. ENOMEM
45470f1702c5SYu Xiangning 	 * is returned only if the memory allocation size
45480f1702c5SYu Xiangning 	 * exceeds the physical limits of the system. ENOMEM
45490f1702c5SYu Xiangning 	 * can't be true here.
45500f1702c5SYu Xiangning 	 */
45510f1702c5SYu Xiangning 	if (error == ENOMEM)
45520f1702c5SYu Xiangning 		error = EAGAIN;
45530f1702c5SYu Xiangning 	return (error);
45540f1702c5SYu Xiangning }
45550f1702c5SYu Xiangning 
45560f1702c5SYu Xiangning /* ARGSUSED */
45570f1702c5SYu Xiangning static int
45580f1702c5SYu Xiangning sotpi_sendmblk(struct sonode *so, struct nmsghdr *msg, int fflag,
45590f1702c5SYu Xiangning     struct cred *cr, mblk_t **mpp)
45600f1702c5SYu Xiangning {
45610f1702c5SYu Xiangning 	int error;
45620f1702c5SYu Xiangning 
45630f1702c5SYu Xiangning 	if (so->so_family != AF_INET && so->so_family != AF_INET6)
45640f1702c5SYu Xiangning 		return (EAFNOSUPPORT);
45650f1702c5SYu Xiangning 
45660f1702c5SYu Xiangning 	if (so->so_state & SS_CANTSENDMORE)
45670f1702c5SYu Xiangning 		return (EPIPE);
45680f1702c5SYu Xiangning 
45690f1702c5SYu Xiangning 	if (so->so_type != SOCK_STREAM)
45700f1702c5SYu Xiangning 		return (EOPNOTSUPP);
45710f1702c5SYu Xiangning 
45720f1702c5SYu Xiangning 	if ((so->so_state & SS_ISCONNECTED) == 0)
45730f1702c5SYu Xiangning 		return (ENOTCONN);
45740f1702c5SYu Xiangning 
45750f1702c5SYu Xiangning 	error = kstrwritemp(so->so_vnode, *mpp, fflag);
45760f1702c5SYu Xiangning 	if (error == 0)
45770f1702c5SYu Xiangning 		*mpp = NULL;
45780f1702c5SYu Xiangning 	return (error);
45790f1702c5SYu Xiangning }
45800f1702c5SYu Xiangning 
45810f1702c5SYu Xiangning /*
4582ff550d0eSmasputra  * Sending data on a datagram socket.
4583ff550d0eSmasputra  * Assumes caller has verified that SS_ISBOUND etc. are set.
4584ff550d0eSmasputra  */
4585ff550d0eSmasputra /* ARGSUSED */
4586ff550d0eSmasputra static int
4587ff550d0eSmasputra sodgram_direct(struct sonode *so, struct sockaddr *name,
4588ff550d0eSmasputra     socklen_t namelen, struct uio *uiop, int flags)
4589ff550d0eSmasputra {
4590ff550d0eSmasputra 	struct T_unitdata_req	tudr;
4591fc80c0dfSnordmark 	mblk_t			*mp = NULL;
4592ff550d0eSmasputra 	int			error = 0;
4593ff550d0eSmasputra 	void			*addr;
4594ff550d0eSmasputra 	socklen_t		addrlen;
4595ff550d0eSmasputra 	ssize_t			len;
4596ff550d0eSmasputra 	struct stdata		*stp = SOTOV(so)->v_stream;
4597ff550d0eSmasputra 	int			so_state;
4598ff550d0eSmasputra 	queue_t			*udp_wq;
4599fc80c0dfSnordmark 	boolean_t		connected;
4600fc80c0dfSnordmark 	mblk_t			*mpdata = NULL;
46010f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
4602005d3febSMarek Pospisil 	uint32_t		auditing = AU_AUDITING();
4603ff550d0eSmasputra 
4604ff550d0eSmasputra 	ASSERT(name != NULL && namelen != 0);
4605ff550d0eSmasputra 	ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4606ff550d0eSmasputra 	ASSERT(!(so->so_mode & SM_EXDATA));
4607ff550d0eSmasputra 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4608ff550d0eSmasputra 	ASSERT(SOTOV(so)->v_type == VSOCK);
4609ff550d0eSmasputra 
4610ff550d0eSmasputra 	/* Caller checked for proper length */
4611ff550d0eSmasputra 	len = uiop->uio_resid;
46120f1702c5SYu Xiangning 	ASSERT(len <= sti->sti_tidu_size);
4613ff550d0eSmasputra 
4614ff550d0eSmasputra 	/* Length and family checks have been done by caller */
4615ff550d0eSmasputra 	ASSERT(name->sa_family == so->so_family);
4616ff550d0eSmasputra 	ASSERT(so->so_family == AF_INET ||
4617ff550d0eSmasputra 	    (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4618ff550d0eSmasputra 	ASSERT(so->so_family == AF_INET6 ||
4619ff550d0eSmasputra 	    (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4620ff550d0eSmasputra 
4621ff550d0eSmasputra 	addr = name;
4622ff550d0eSmasputra 	addrlen = namelen;
4623ff550d0eSmasputra 
4624ff550d0eSmasputra 	if (stp->sd_sidp != NULL &&
4625ff550d0eSmasputra 	    (error = straccess(stp, JCWRITE)) != 0)
4626ff550d0eSmasputra 		goto done;
4627ff550d0eSmasputra 
4628ff550d0eSmasputra 	so_state = so->so_state;
4629ff550d0eSmasputra 
4630fc80c0dfSnordmark 	connected = so_state & SS_ISCONNECTED;
4631fc80c0dfSnordmark 	if (!connected) {
4632fc80c0dfSnordmark 		tudr.PRIM_type = T_UNITDATA_REQ;
4633fc80c0dfSnordmark 		tudr.DEST_length = addrlen;
4634fc80c0dfSnordmark 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
4635fc80c0dfSnordmark 		tudr.OPT_length = 0;
4636fc80c0dfSnordmark 		tudr.OPT_offset = 0;
4637fc80c0dfSnordmark 
4638fc80c0dfSnordmark 		mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0,
4639de8c4a14SErik Nordmark 		    _ALLOC_INTR, CRED());
4640fc80c0dfSnordmark 		if (mp == NULL) {
4641fc80c0dfSnordmark 			/*
4642fc80c0dfSnordmark 			 * Caught a signal waiting for memory.
4643fc80c0dfSnordmark 			 * Let send* return EINTR.
4644fc80c0dfSnordmark 			 */
4645fc80c0dfSnordmark 			error = EINTR;
4646fc80c0dfSnordmark 			goto done;
4647fc80c0dfSnordmark 		}
4648fc80c0dfSnordmark 	}
4649fc80c0dfSnordmark 
4650ff550d0eSmasputra 	/*
4651ff550d0eSmasputra 	 * For UDP we don't break up the copyin into smaller pieces
4652ff550d0eSmasputra 	 * as in the TCP case.  That means if ENOMEM is returned by
4653ff550d0eSmasputra 	 * mcopyinuio() then the uio vector has not been modified at
4654ff550d0eSmasputra 	 * all and we fallback to either strwrite() or kstrputmsg()
4655ff550d0eSmasputra 	 * below.  Note also that we never generate priority messages
4656ff550d0eSmasputra 	 * from here.
4657ff550d0eSmasputra 	 */
4658ff550d0eSmasputra 	udp_wq = stp->sd_wrq->q_next;
4659ff550d0eSmasputra 	if (canput(udp_wq) &&
4660fc80c0dfSnordmark 	    (mpdata = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
4661fc80c0dfSnordmark 		ASSERT(DB_TYPE(mpdata) == M_DATA);
4662ff550d0eSmasputra 		ASSERT(uiop->uio_resid == 0);
4663fc80c0dfSnordmark 		if (!connected)
4664fc80c0dfSnordmark 			linkb(mp, mpdata);
4665fc80c0dfSnordmark 		else
4666fc80c0dfSnordmark 			mp = mpdata;
4667005d3febSMarek Pospisil 		if (auditing)
4668ff550d0eSmasputra 			audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4669fc80c0dfSnordmark 
4670fc80c0dfSnordmark 		udp_wput(udp_wq, mp);
4671ff550d0eSmasputra 		return (0);
4672ff550d0eSmasputra 	}
4673fc80c0dfSnordmark 
4674fc80c0dfSnordmark 	ASSERT(mpdata == NULL);
4675fc80c0dfSnordmark 	if (error != 0 && error != ENOMEM) {
4676fc80c0dfSnordmark 		freemsg(mp);
4677ff550d0eSmasputra 		return (error);
4678fc80c0dfSnordmark 	}
4679ff550d0eSmasputra 
4680ff550d0eSmasputra 	/*
4681ff550d0eSmasputra 	 * For connected, let strwrite() handle the blocking case.
4682ff550d0eSmasputra 	 * Otherwise we fall thru and use kstrputmsg().
4683ff550d0eSmasputra 	 */
4684fc80c0dfSnordmark 	if (connected)
4685ff550d0eSmasputra 		return (strwrite(SOTOV(so), uiop, CRED()));
4686ff550d0eSmasputra 
4687005d3febSMarek Pospisil 	if (auditing)
4688ff550d0eSmasputra 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4689ff550d0eSmasputra 
4690ff550d0eSmasputra 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4691ff550d0eSmasputra done:
4692ff550d0eSmasputra #ifdef SOCK_DEBUG
4693ff550d0eSmasputra 	if (error != 0) {
4694ff550d0eSmasputra 		eprintsoline(so, error);
4695ff550d0eSmasputra 	}
4696ff550d0eSmasputra #endif /* SOCK_DEBUG */
4697ff550d0eSmasputra 	return (error);
4698ff550d0eSmasputra }
4699ff550d0eSmasputra 
4700ff550d0eSmasputra int
4701ff550d0eSmasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4702ff550d0eSmasputra {
4703ff550d0eSmasputra 	struct stdata *stp = SOTOV(so)->v_stream;
4704ff550d0eSmasputra 	ssize_t iosize, rmax, maxblk;
4705ff550d0eSmasputra 	queue_t *tcp_wq = stp->sd_wrq->q_next;
4706c28749e9Skais 	mblk_t *newmp;
4707ff550d0eSmasputra 	int error = 0, wflag = 0;
4708ff550d0eSmasputra 
4709ff550d0eSmasputra 	ASSERT(so->so_mode & SM_BYTESTREAM);
4710ff550d0eSmasputra 	ASSERT(SOTOV(so)->v_type == VSOCK);
4711ff550d0eSmasputra 
4712ff550d0eSmasputra 	if (stp->sd_sidp != NULL &&
4713ff550d0eSmasputra 	    (error = straccess(stp, JCWRITE)) != 0)
4714ff550d0eSmasputra 		return (error);
4715ff550d0eSmasputra 
4716ff550d0eSmasputra 	if (uiop == NULL) {
4717ff550d0eSmasputra 		/*
4718ff550d0eSmasputra 		 * kstrwritemp() should have checked sd_flag and
4719ff550d0eSmasputra 		 * flow-control before coming here.  If we end up
4720ff550d0eSmasputra 		 * here it means that we can simply pass down the
4721ff550d0eSmasputra 		 * data to tcp.
4722ff550d0eSmasputra 		 */
4723ff550d0eSmasputra 		ASSERT(mp != NULL);
4724c28749e9Skais 		if (stp->sd_wputdatafunc != NULL) {
4725c28749e9Skais 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4726c28749e9Skais 			    NULL, NULL, NULL);
4727c28749e9Skais 			if (newmp == NULL) {
4728c28749e9Skais 				/* The caller will free mp */
4729c28749e9Skais 				return (ECOMM);
4730c28749e9Skais 			}
4731c28749e9Skais 			mp = newmp;
4732c28749e9Skais 		}
4733ff550d0eSmasputra 		tcp_wput(tcp_wq, mp);
4734ff550d0eSmasputra 		return (0);
4735ff550d0eSmasputra 	}
4736ff550d0eSmasputra 
4737ff550d0eSmasputra 	/* Fallback to strwrite() to do proper error handling */
4738ff550d0eSmasputra 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4739ff550d0eSmasputra 		return (strwrite(SOTOV(so), uiop, cr));
4740ff550d0eSmasputra 
4741ff550d0eSmasputra 	rmax = stp->sd_qn_maxpsz;
4742ff550d0eSmasputra 	ASSERT(rmax >= 0 || rmax == INFPSZ);
4743ff550d0eSmasputra 	if (rmax == 0 || uiop->uio_resid <= 0)
4744ff550d0eSmasputra 		return (0);
4745ff550d0eSmasputra 
4746ff550d0eSmasputra 	if (rmax == INFPSZ)
4747ff550d0eSmasputra 		rmax = uiop->uio_resid;
4748ff550d0eSmasputra 
4749ff550d0eSmasputra 	maxblk = stp->sd_maxblk;
4750ff550d0eSmasputra 
4751ff550d0eSmasputra 	for (;;) {
4752ff550d0eSmasputra 		iosize = MIN(uiop->uio_resid, rmax);
4753ff550d0eSmasputra 
4754ff550d0eSmasputra 		mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4755ff550d0eSmasputra 		if (mp == NULL) {
4756ff550d0eSmasputra 			/*
4757ff550d0eSmasputra 			 * Fallback to strwrite() for ENOMEM; if this
4758ff550d0eSmasputra 			 * is our first time in this routine and the uio
4759ff550d0eSmasputra 			 * vector has not been modified, we will end up
4760ff550d0eSmasputra 			 * calling strwrite() without any flag set.
4761ff550d0eSmasputra 			 */
4762ff550d0eSmasputra 			if (error == ENOMEM)
4763ff550d0eSmasputra 				goto slow_send;
4764ff550d0eSmasputra 			else
4765ff550d0eSmasputra 				return (error);
4766ff550d0eSmasputra 		}
4767ff550d0eSmasputra 		ASSERT(uiop->uio_resid >= 0);
4768ff550d0eSmasputra 		/*
4769ff550d0eSmasputra 		 * If mp is non-NULL and ENOMEM is set, it means that
4770ff550d0eSmasputra 		 * mcopyinuio() was able to break down some of the user
4771ff550d0eSmasputra 		 * data into one or more mblks.  Send the partial data
4772ff550d0eSmasputra 		 * to tcp and let the rest be handled in strwrite().
4773ff550d0eSmasputra 		 */
4774ff550d0eSmasputra 		ASSERT(error == 0 || error == ENOMEM);
4775c28749e9Skais 		if (stp->sd_wputdatafunc != NULL) {
4776c28749e9Skais 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4777c28749e9Skais 			    NULL, NULL, NULL);
4778c28749e9Skais 			if (newmp == NULL) {
4779c28749e9Skais 				/* The caller will free mp */
4780c28749e9Skais 				return (ECOMM);
4781c28749e9Skais 			}
4782c28749e9Skais 			mp = newmp;
4783c28749e9Skais 		}
4784ff550d0eSmasputra 		tcp_wput(tcp_wq, mp);
4785ff550d0eSmasputra 
4786ff550d0eSmasputra 		wflag |= NOINTR;
4787ff550d0eSmasputra 
4788ff550d0eSmasputra 		if (uiop->uio_resid == 0) {	/* No more data; we're done */
4789ff550d0eSmasputra 			ASSERT(error == 0);
4790ff550d0eSmasputra 			break;
4791ff550d0eSmasputra 		} else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4792ff550d0eSmasputra 		    (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4793ff550d0eSmasputra slow_send:
4794ff550d0eSmasputra 			/*
4795ff550d0eSmasputra 			 * We were able to send down partial data using
4796ff550d0eSmasputra 			 * the direct call interface, but are now relying
4797ff550d0eSmasputra 			 * on strwrite() to handle the non-fastpath cases.
4798ff550d0eSmasputra 			 * If the socket is blocking we will sleep in
4799ff550d0eSmasputra 			 * strwaitq() until write is permitted, otherwise,
4800ff550d0eSmasputra 			 * we will need to return the amount of bytes
4801ff550d0eSmasputra 			 * written so far back to the app.  This is the
4802ff550d0eSmasputra 			 * reason why we pass NOINTR flag to strwrite()
4803ff550d0eSmasputra 			 * for non-blocking socket, because we don't want
4804ff550d0eSmasputra 			 * to return EAGAIN when portion of the user data
4805ff550d0eSmasputra 			 * has actually been sent down.
4806ff550d0eSmasputra 			 */
4807ff550d0eSmasputra 			return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4808ff550d0eSmasputra 		}
4809ff550d0eSmasputra 	}
4810ff550d0eSmasputra 	return (0);
4811ff550d0eSmasputra }
4812ff550d0eSmasputra 
4813ff550d0eSmasputra /*
48140f1702c5SYu Xiangning  * Update sti_faddr by asking the transport (unless AF_UNIX).
48157c478bd9Sstevel@tonic-gate  */
48160f1702c5SYu Xiangning /* ARGSUSED */
48177c478bd9Sstevel@tonic-gate int
48180f1702c5SYu Xiangning sotpi_getpeername(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
48190f1702c5SYu Xiangning     boolean_t accept, struct cred *cr)
48207c478bd9Sstevel@tonic-gate {
48217c478bd9Sstevel@tonic-gate 	struct strbuf	strbuf;
48227c478bd9Sstevel@tonic-gate 	int		error = 0, res;
48237c478bd9Sstevel@tonic-gate 	void		*addr;
48247c478bd9Sstevel@tonic-gate 	t_uscalar_t	addrlen;
48257c478bd9Sstevel@tonic-gate 	k_sigset_t	smask;
48260f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
48277c478bd9Sstevel@tonic-gate 
48287c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
4829903a11ebSrh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
48307c478bd9Sstevel@tonic-gate 
48310f1702c5SYu Xiangning 	ASSERT(*namelen > 0);
48327c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
48337c478bd9Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
48340f1702c5SYu Xiangning 
48350f1702c5SYu Xiangning 	if (accept) {
48360f1702c5SYu Xiangning 		bcopy(sti->sti_faddr_sa, name,
48370f1702c5SYu Xiangning 		    MIN(*namelen, sti->sti_faddr_len));
48380f1702c5SYu Xiangning 		*namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
48390f1702c5SYu Xiangning 		goto done;
48400f1702c5SYu Xiangning 	}
48410f1702c5SYu Xiangning 
48427c478bd9Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
48437c478bd9Sstevel@tonic-gate 		error = ENOTCONN;
48447c478bd9Sstevel@tonic-gate 		goto done;
48457c478bd9Sstevel@tonic-gate 	}
48467c478bd9Sstevel@tonic-gate 	/* Added this check for X/Open */
48477c478bd9Sstevel@tonic-gate 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
48487c478bd9Sstevel@tonic-gate 		error = EINVAL;
48497c478bd9Sstevel@tonic-gate 		if (xnet_check_print) {
48507c478bd9Sstevel@tonic-gate 			printf("sockfs: X/Open getpeername check => EINVAL\n");
48517c478bd9Sstevel@tonic-gate 		}
48527c478bd9Sstevel@tonic-gate 		goto done;
48537c478bd9Sstevel@tonic-gate 	}
48540f1702c5SYu Xiangning 
48550f1702c5SYu Xiangning 	if (sti->sti_faddr_valid) {
48560f1702c5SYu Xiangning 		bcopy(sti->sti_faddr_sa, name,
48570f1702c5SYu Xiangning 		    MIN(*namelen, sti->sti_faddr_len));
48580f1702c5SYu Xiangning 		*namelen = sti->sti_faddr_noxlate ? 0: sti->sti_faddr_len;
48590f1702c5SYu Xiangning 		goto done;
48600f1702c5SYu Xiangning 	}
48610f1702c5SYu Xiangning 
48627c478bd9Sstevel@tonic-gate #ifdef DEBUG
48637c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
48640f1702c5SYu Xiangning 	    pr_addr(so->so_family, sti->sti_faddr_sa,
48650f1702c5SYu Xiangning 	    (t_uscalar_t)sti->sti_faddr_len)));
48667c478bd9Sstevel@tonic-gate #endif /* DEBUG */
48677c478bd9Sstevel@tonic-gate 
48682caf0dcdSrshoaib 	if (so->so_family == AF_UNIX) {
48697c478bd9Sstevel@tonic-gate 		/* Transport has different name space - return local info */
48700f1702c5SYu Xiangning 		if (sti->sti_faddr_noxlate)
48710f1702c5SYu Xiangning 			*namelen = 0;
48727c478bd9Sstevel@tonic-gate 		error = 0;
48737c478bd9Sstevel@tonic-gate 		goto done;
48747c478bd9Sstevel@tonic-gate 	}
48757c478bd9Sstevel@tonic-gate 
48760f1702c5SYu Xiangning 	ASSERT(so->so_family != AF_UNIX && sti->sti_faddr_noxlate == 0);
48770f1702c5SYu Xiangning 
48780f1702c5SYu Xiangning 	ASSERT(sti->sti_faddr_sa);
48797c478bd9Sstevel@tonic-gate 	/* Allocate local buffer to use with ioctl */
48800f1702c5SYu Xiangning 	addrlen = (t_uscalar_t)sti->sti_faddr_maxlen;
48817c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
48827c478bd9Sstevel@tonic-gate 	addr = kmem_alloc(addrlen, KM_SLEEP);
48837c478bd9Sstevel@tonic-gate 
48847c478bd9Sstevel@tonic-gate 	/*
48857c478bd9Sstevel@tonic-gate 	 * Issue TI_GETPEERNAME with signals masked.
48860f1702c5SYu Xiangning 	 * Put the result in sti_faddr_sa so that getpeername works after
48877c478bd9Sstevel@tonic-gate 	 * a shutdown(output).
48887c478bd9Sstevel@tonic-gate 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
48897c478bd9Sstevel@tonic-gate 	 * back to the socket.
48907c478bd9Sstevel@tonic-gate 	 */
48917c478bd9Sstevel@tonic-gate 	strbuf.buf = addr;
48927c478bd9Sstevel@tonic-gate 	strbuf.maxlen = addrlen;
48937c478bd9Sstevel@tonic-gate 	strbuf.len = 0;
48947c478bd9Sstevel@tonic-gate 
48957c478bd9Sstevel@tonic-gate 	sigintr(&smask, 0);
48967c478bd9Sstevel@tonic-gate 	res = 0;
48970f1702c5SYu Xiangning 	ASSERT(cr);
48987c478bd9Sstevel@tonic-gate 	error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
48990f1702c5SYu Xiangning 	    0, K_TO_K, cr, &res);
49007c478bd9Sstevel@tonic-gate 	sigunintr(&smask);
49017c478bd9Sstevel@tonic-gate 
49027c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
49037c478bd9Sstevel@tonic-gate 	/*
49047c478bd9Sstevel@tonic-gate 	 * If there is an error record the error in so_error put don't fail
49057c478bd9Sstevel@tonic-gate 	 * the getpeername. Instead fallback on the recorded
49060f1702c5SYu Xiangning 	 * sti->sti_faddr_sa.
49077c478bd9Sstevel@tonic-gate 	 */
49087c478bd9Sstevel@tonic-gate 	if (error) {
49097c478bd9Sstevel@tonic-gate 		/*
49107c478bd9Sstevel@tonic-gate 		 * Various stream head errors can be returned to the ioctl.
49117c478bd9Sstevel@tonic-gate 		 * However, it is impossible to determine which ones of
49127c478bd9Sstevel@tonic-gate 		 * these are really socket level errors that were incorrectly
49137c478bd9Sstevel@tonic-gate 		 * consumed by the ioctl. Thus this code silently ignores the
49147c478bd9Sstevel@tonic-gate 		 * error - to code explicitly does not reinstate the error
49157c478bd9Sstevel@tonic-gate 		 * using soseterror().
49167c478bd9Sstevel@tonic-gate 		 * Experiments have shows that at least this set of
49177c478bd9Sstevel@tonic-gate 		 * errors are reported and should not be reinstated on the
49187c478bd9Sstevel@tonic-gate 		 * socket:
49197c478bd9Sstevel@tonic-gate 		 *	EINVAL	E.g. if an I_LINK was in effect when
49207c478bd9Sstevel@tonic-gate 		 *		getpeername was called.
49217c478bd9Sstevel@tonic-gate 		 *	EPIPE	The ioctl error semantics prefer the write
49227c478bd9Sstevel@tonic-gate 		 *		side error over the read side error.
49237c478bd9Sstevel@tonic-gate 		 *	ENOTCONN The transport just got disconnected but
49247c478bd9Sstevel@tonic-gate 		 *		sockfs had not yet seen the T_DISCON_IND
49257c478bd9Sstevel@tonic-gate 		 *		when issuing the ioctl.
49267c478bd9Sstevel@tonic-gate 		 */
49277c478bd9Sstevel@tonic-gate 		error = 0;
49287c478bd9Sstevel@tonic-gate 	} else if (res == 0 && strbuf.len > 0 &&
49297c478bd9Sstevel@tonic-gate 	    (so->so_state & SS_ISCONNECTED)) {
49300f1702c5SYu Xiangning 		ASSERT(strbuf.len <= (int)sti->sti_faddr_maxlen);
49310f1702c5SYu Xiangning 		sti->sti_faddr_len = (socklen_t)strbuf.len;
49320f1702c5SYu Xiangning 		bcopy(addr, sti->sti_faddr_sa, sti->sti_faddr_len);
49330f1702c5SYu Xiangning 		sti->sti_faddr_valid = 1;
49340f1702c5SYu Xiangning 
49350f1702c5SYu Xiangning 		bcopy(addr, name, MIN(*namelen, sti->sti_faddr_len));
49360f1702c5SYu Xiangning 		*namelen = sti->sti_faddr_len;
49377c478bd9Sstevel@tonic-gate 	}
49387c478bd9Sstevel@tonic-gate 	kmem_free(addr, addrlen);
49397c478bd9Sstevel@tonic-gate #ifdef DEBUG
49407c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
49410f1702c5SYu Xiangning 	    pr_addr(so->so_family, sti->sti_faddr_sa,
49420f1702c5SYu Xiangning 	    (t_uscalar_t)sti->sti_faddr_len)));
49437c478bd9Sstevel@tonic-gate #endif /* DEBUG */
49447c478bd9Sstevel@tonic-gate done:
49457c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
49467c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
49477c478bd9Sstevel@tonic-gate 	return (error);
49487c478bd9Sstevel@tonic-gate }
49497c478bd9Sstevel@tonic-gate 
49507c478bd9Sstevel@tonic-gate /*
49510f1702c5SYu Xiangning  * Update sti_laddr by asking the transport (unless AF_UNIX).
49527c478bd9Sstevel@tonic-gate  */
49537c478bd9Sstevel@tonic-gate int
49540f1702c5SYu Xiangning sotpi_getsockname(struct sonode *so, struct sockaddr *name, socklen_t *namelen,
49550f1702c5SYu Xiangning     struct cred *cr)
49567c478bd9Sstevel@tonic-gate {
49577c478bd9Sstevel@tonic-gate 	struct strbuf	strbuf;
49587c478bd9Sstevel@tonic-gate 	int		error = 0, res;
49597c478bd9Sstevel@tonic-gate 	void		*addr;
49607c478bd9Sstevel@tonic-gate 	t_uscalar_t	addrlen;
49617c478bd9Sstevel@tonic-gate 	k_sigset_t	smask;
49620f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
49637c478bd9Sstevel@tonic-gate 
49647c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
4965903a11ebSrh87107 	    (void *)so, pr_state(so->so_state, so->so_mode)));
49667c478bd9Sstevel@tonic-gate 
49670f1702c5SYu Xiangning 	ASSERT(*namelen > 0);
49687c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
49697c478bd9Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
49700f1702c5SYu Xiangning 
49717c478bd9Sstevel@tonic-gate #ifdef DEBUG
49720f1702c5SYu Xiangning 
49737c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
49740f1702c5SYu Xiangning 	    pr_addr(so->so_family, sti->sti_laddr_sa,
49750f1702c5SYu Xiangning 	    (t_uscalar_t)sti->sti_laddr_len)));
49767c478bd9Sstevel@tonic-gate #endif /* DEBUG */
49770f1702c5SYu Xiangning 	if (sti->sti_laddr_valid) {
49780f1702c5SYu Xiangning 		bcopy(sti->sti_laddr_sa, name,
49790f1702c5SYu Xiangning 		    MIN(*namelen, sti->sti_laddr_len));
49800f1702c5SYu Xiangning 		*namelen = sti->sti_laddr_len;
49810f1702c5SYu Xiangning 		goto done;
49820f1702c5SYu Xiangning 	}
49830f1702c5SYu Xiangning 
49847c478bd9Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
4985dfc0fed8SRobert Mustacchi 		/*
4986dfc0fed8SRobert Mustacchi 		 * Transport has different name space - return local info. If we
4987dfc0fed8SRobert Mustacchi 		 * have enough space, let consumers know the family.
4988dfc0fed8SRobert Mustacchi 		 */
4989dfc0fed8SRobert Mustacchi 		if (*namelen >= sizeof (sa_family_t)) {
4990dfc0fed8SRobert Mustacchi 			name->sa_family = AF_UNIX;
4991dfc0fed8SRobert Mustacchi 			*namelen = sizeof (sa_family_t);
4992dfc0fed8SRobert Mustacchi 		} else {
4993a5adac4dSYu Xiangning 			*namelen = 0;
4994dfc0fed8SRobert Mustacchi 		}
4995dfc0fed8SRobert Mustacchi 		error = 0;
49967c478bd9Sstevel@tonic-gate 		goto done;
49977c478bd9Sstevel@tonic-gate 	}
499818cbc865Sblu 	if (!(so->so_state & SS_ISBOUND)) {
499918cbc865Sblu 		/* If not bound, then nothing to return. */
500018cbc865Sblu 		error = 0;
500118cbc865Sblu 		goto done;
500218cbc865Sblu 	}
50030f1702c5SYu Xiangning 
50047c478bd9Sstevel@tonic-gate 	/* Allocate local buffer to use with ioctl */
50050f1702c5SYu Xiangning 	addrlen = (t_uscalar_t)sti->sti_laddr_maxlen;
50067c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
50077c478bd9Sstevel@tonic-gate 	addr = kmem_alloc(addrlen, KM_SLEEP);
50087c478bd9Sstevel@tonic-gate 
50097c478bd9Sstevel@tonic-gate 	/*
50107c478bd9Sstevel@tonic-gate 	 * Issue TI_GETMYNAME with signals masked.
50110f1702c5SYu Xiangning 	 * Put the result in sti_laddr_sa so that getsockname works after
50127c478bd9Sstevel@tonic-gate 	 * a shutdown(output).
50137c478bd9Sstevel@tonic-gate 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
50147c478bd9Sstevel@tonic-gate 	 * back to the socket.
50157c478bd9Sstevel@tonic-gate 	 */
50167c478bd9Sstevel@tonic-gate 	strbuf.buf = addr;
50177c478bd9Sstevel@tonic-gate 	strbuf.maxlen = addrlen;
50187c478bd9Sstevel@tonic-gate 	strbuf.len = 0;
50197c478bd9Sstevel@tonic-gate 
50207c478bd9Sstevel@tonic-gate 	sigintr(&smask, 0);
50217c478bd9Sstevel@tonic-gate 	res = 0;
50220f1702c5SYu Xiangning 	ASSERT(cr);
50237c478bd9Sstevel@tonic-gate 	error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
50240f1702c5SYu Xiangning 	    0, K_TO_K, cr, &res);
50257c478bd9Sstevel@tonic-gate 	sigunintr(&smask);
50267c478bd9Sstevel@tonic-gate 
50277c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
50287c478bd9Sstevel@tonic-gate 	/*
50297c478bd9Sstevel@tonic-gate 	 * If there is an error record the error in so_error put don't fail
50307c478bd9Sstevel@tonic-gate 	 * the getsockname. Instead fallback on the recorded
50310f1702c5SYu Xiangning 	 * sti->sti_laddr_sa.
50327c478bd9Sstevel@tonic-gate 	 */
50337c478bd9Sstevel@tonic-gate 	if (error) {
50347c478bd9Sstevel@tonic-gate 		/*
50357c478bd9Sstevel@tonic-gate 		 * Various stream head errors can be returned to the ioctl.
50367c478bd9Sstevel@tonic-gate 		 * However, it is impossible to determine which ones of
50377c478bd9Sstevel@tonic-gate 		 * these are really socket level errors that were incorrectly
50387c478bd9Sstevel@tonic-gate 		 * consumed by the ioctl. Thus this code silently ignores the
50397c478bd9Sstevel@tonic-gate 		 * error - to code explicitly does not reinstate the error
50407c478bd9Sstevel@tonic-gate 		 * using soseterror().
50417c478bd9Sstevel@tonic-gate 		 * Experiments have shows that at least this set of
50427c478bd9Sstevel@tonic-gate 		 * errors are reported and should not be reinstated on the
50437c478bd9Sstevel@tonic-gate 		 * socket:
50447c478bd9Sstevel@tonic-gate 		 *	EINVAL	E.g. if an I_LINK was in effect when
50457c478bd9Sstevel@tonic-gate 		 *		getsockname was called.
50467c478bd9Sstevel@tonic-gate 		 *	EPIPE	The ioctl error semantics prefer the write
50477c478bd9Sstevel@tonic-gate 		 *		side error over the read side error.
50487c478bd9Sstevel@tonic-gate 		 */
50497c478bd9Sstevel@tonic-gate 		error = 0;
50507c478bd9Sstevel@tonic-gate 	} else if (res == 0 && strbuf.len > 0 &&
50517c478bd9Sstevel@tonic-gate 	    (so->so_state & SS_ISBOUND)) {
50520f1702c5SYu Xiangning 		ASSERT(strbuf.len <= (int)sti->sti_laddr_maxlen);
50530f1702c5SYu Xiangning 		sti->sti_laddr_len = (socklen_t)strbuf.len;
50540f1702c5SYu Xiangning 		bcopy(addr, sti->sti_laddr_sa, sti->sti_laddr_len);
50550f1702c5SYu Xiangning 		sti->sti_laddr_valid = 1;
50560f1702c5SYu Xiangning 
50570f1702c5SYu Xiangning 		bcopy(addr, name, MIN(sti->sti_laddr_len, *namelen));
50580f1702c5SYu Xiangning 		*namelen = sti->sti_laddr_len;
50597c478bd9Sstevel@tonic-gate 	}
50607c478bd9Sstevel@tonic-gate 	kmem_free(addr, addrlen);
50617c478bd9Sstevel@tonic-gate #ifdef DEBUG
50627c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
50630f1702c5SYu Xiangning 	    pr_addr(so->so_family, sti->sti_laddr_sa,
50640f1702c5SYu Xiangning 	    (t_uscalar_t)sti->sti_laddr_len)));
50657c478bd9Sstevel@tonic-gate #endif /* DEBUG */
50667c478bd9Sstevel@tonic-gate done:
50677c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
50687c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
50697c478bd9Sstevel@tonic-gate 	return (error);
50707c478bd9Sstevel@tonic-gate }
50717c478bd9Sstevel@tonic-gate 
50727c478bd9Sstevel@tonic-gate /*
50737c478bd9Sstevel@tonic-gate  * Get socket options. For SOL_SOCKET options some options are handled
50747c478bd9Sstevel@tonic-gate  * by the sockfs while others use the value recorded in the sonode as a
50757c478bd9Sstevel@tonic-gate  * fallback should the T_SVR4_OPTMGMT_REQ fail.
50767c478bd9Sstevel@tonic-gate  *
50777c478bd9Sstevel@tonic-gate  * On the return most *optlenp bytes are copied to optval.
50787c478bd9Sstevel@tonic-gate  */
50790f1702c5SYu Xiangning /* ARGSUSED */
50807c478bd9Sstevel@tonic-gate int
50817c478bd9Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name,
50820f1702c5SYu Xiangning 		void *optval, socklen_t *optlenp, int flags, struct cred *cr)
50837c478bd9Sstevel@tonic-gate {
50847c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req	optmgmt_req;
50857c478bd9Sstevel@tonic-gate 	struct T_optmgmt_ack	*optmgmt_ack;
50867c478bd9Sstevel@tonic-gate 	struct opthdr		oh;
50877c478bd9Sstevel@tonic-gate 	struct opthdr		*opt_res;
50887c478bd9Sstevel@tonic-gate 	mblk_t			*mp = NULL;
50897c478bd9Sstevel@tonic-gate 	int			error = 0;
50907c478bd9Sstevel@tonic-gate 	void			*option = NULL;	/* Set if fallback value */
50917c478bd9Sstevel@tonic-gate 	t_uscalar_t		maxlen = *optlenp;
50927c478bd9Sstevel@tonic-gate 	t_uscalar_t		len;
50937c478bd9Sstevel@tonic-gate 	uint32_t		value;
50940f1702c5SYu Xiangning 	struct timeval		tmo_val; /* used for SO_RCVTIMEO, SO_SNDTIMEO */
509522238f73Sshenjian 	struct timeval32	tmo_val32;
50960f1702c5SYu Xiangning 	struct so_snd_bufinfo	snd_bufinfo;	/* used for zero copy */
50977c478bd9Sstevel@tonic-gate 
50987c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
5099903a11ebSrh87107 	    (void *)so, level, option_name, optval, (void *)optlenp,
51007c478bd9Sstevel@tonic-gate 	    pr_state(so->so_state, so->so_mode)));
51017c478bd9Sstevel@tonic-gate 
51027c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
51037c478bd9Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
51047c478bd9Sstevel@tonic-gate 
51057c478bd9Sstevel@tonic-gate 	/*
51067c478bd9Sstevel@tonic-gate 	 * Check for SOL_SOCKET options.
51077c478bd9Sstevel@tonic-gate 	 * Certain SOL_SOCKET options are returned directly whereas
51087c478bd9Sstevel@tonic-gate 	 * others only provide a default (fallback) value should
51097c478bd9Sstevel@tonic-gate 	 * the T_SVR4_OPTMGMT_REQ fail.
51107c478bd9Sstevel@tonic-gate 	 */
51117c478bd9Sstevel@tonic-gate 	if (level == SOL_SOCKET) {
51127c478bd9Sstevel@tonic-gate 		/* Check parameters */
51137c478bd9Sstevel@tonic-gate 		switch (option_name) {
51147c478bd9Sstevel@tonic-gate 		case SO_TYPE:
51157c478bd9Sstevel@tonic-gate 		case SO_ERROR:
51167c478bd9Sstevel@tonic-gate 		case SO_DEBUG:
51177c478bd9Sstevel@tonic-gate 		case SO_ACCEPTCONN:
51187c478bd9Sstevel@tonic-gate 		case SO_REUSEADDR:
5119*78918900SArne Jansen 		case SO_REUSEPORT:
51207c478bd9Sstevel@tonic-gate 		case SO_KEEPALIVE:
51217c478bd9Sstevel@tonic-gate 		case SO_DONTROUTE:
51227c478bd9Sstevel@tonic-gate 		case SO_BROADCAST:
51237c478bd9Sstevel@tonic-gate 		case SO_USELOOPBACK:
51247c478bd9Sstevel@tonic-gate 		case SO_OOBINLINE:
51257c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
51267c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
51277c478bd9Sstevel@tonic-gate #ifdef notyet
51287c478bd9Sstevel@tonic-gate 		case SO_SNDLOWAT:
51297c478bd9Sstevel@tonic-gate 		case SO_RCVLOWAT:
51307c478bd9Sstevel@tonic-gate #endif /* notyet */
513188cda078Skcpoon 		case SO_DOMAIN:
51327c478bd9Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
51337c478bd9Sstevel@tonic-gate 			if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
51347c478bd9Sstevel@tonic-gate 				error = EINVAL;
51357c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
51367c478bd9Sstevel@tonic-gate 				goto done2;
51377c478bd9Sstevel@tonic-gate 			}
51387c478bd9Sstevel@tonic-gate 			break;
51390f1702c5SYu Xiangning 		case SO_RCVTIMEO:
51400f1702c5SYu Xiangning 		case SO_SNDTIMEO:
5141e5083e81Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
5142e5083e81Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
514322238f73Sshenjian 				if (maxlen < sizeof (struct timeval)) {
51440f1702c5SYu Xiangning 					error = EINVAL;
51450f1702c5SYu Xiangning 					eprintsoline(so, error);
51460f1702c5SYu Xiangning 					goto done2;
51470f1702c5SYu Xiangning 				}
514822238f73Sshenjian 			} else {
514922238f73Sshenjian 				if (maxlen < sizeof (struct timeval32)) {
515022238f73Sshenjian 					error = EINVAL;
515122238f73Sshenjian 					eprintsoline(so, error);
515222238f73Sshenjian 					goto done2;
515322238f73Sshenjian 				}
515422238f73Sshenjian 
515522238f73Sshenjian 			}
51560f1702c5SYu Xiangning 			break;
51577c478bd9Sstevel@tonic-gate 		case SO_LINGER:
51587c478bd9Sstevel@tonic-gate 			if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
51597c478bd9Sstevel@tonic-gate 				error = EINVAL;
51607c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
51617c478bd9Sstevel@tonic-gate 				goto done2;
51627c478bd9Sstevel@tonic-gate 			}
51637c478bd9Sstevel@tonic-gate 			break;
51640f1702c5SYu Xiangning 		case SO_SND_BUFINFO:
51650f1702c5SYu Xiangning 			if (maxlen < (t_uscalar_t)
51660f1702c5SYu Xiangning 			    sizeof (struct so_snd_bufinfo)) {
51670f1702c5SYu Xiangning 				error = EINVAL;
51680f1702c5SYu Xiangning 				eprintsoline(so, error);
51690f1702c5SYu Xiangning 				goto done2;
51700f1702c5SYu Xiangning 			}
51710f1702c5SYu Xiangning 			break;
51727c478bd9Sstevel@tonic-gate 		}
51737c478bd9Sstevel@tonic-gate 
51747c478bd9Sstevel@tonic-gate 		len = (t_uscalar_t)sizeof (uint32_t);	/* Default */
51757c478bd9Sstevel@tonic-gate 
51767c478bd9Sstevel@tonic-gate 		switch (option_name) {
51777c478bd9Sstevel@tonic-gate 		case SO_TYPE:
51787c478bd9Sstevel@tonic-gate 			value = so->so_type;
51797c478bd9Sstevel@tonic-gate 			option = &value;
51807c478bd9Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51817c478bd9Sstevel@tonic-gate 
51827c478bd9Sstevel@tonic-gate 		case SO_ERROR:
51830f1702c5SYu Xiangning 			value = sogeterr(so, B_TRUE);
51847c478bd9Sstevel@tonic-gate 			option = &value;
51857c478bd9Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
51867c478bd9Sstevel@tonic-gate 
51877c478bd9Sstevel@tonic-gate 		case SO_ACCEPTCONN:
51887c478bd9Sstevel@tonic-gate 			if (so->so_state & SS_ACCEPTCONN)
51897c478bd9Sstevel@tonic-gate 				value = SO_ACCEPTCONN;
51907c478bd9Sstevel@tonic-gate 			else
51917c478bd9Sstevel@tonic-gate 				value = 0;
51927c478bd9Sstevel@tonic-gate #ifdef DEBUG
51937c478bd9Sstevel@tonic-gate 			if (value) {
51947c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
51957c478bd9Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x is set\n",
51967c478bd9Sstevel@tonic-gate 				    option_name));
51977c478bd9Sstevel@tonic-gate 			} else {
51987c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
51997c478bd9Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x not set\n",
52007c478bd9Sstevel@tonic-gate 				    option_name));
52017c478bd9Sstevel@tonic-gate 			}
52027c478bd9Sstevel@tonic-gate #endif /* DEBUG */
52037c478bd9Sstevel@tonic-gate 			option = &value;
52047c478bd9Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52057c478bd9Sstevel@tonic-gate 
52067c478bd9Sstevel@tonic-gate 		case SO_DEBUG:
52077c478bd9Sstevel@tonic-gate 		case SO_REUSEADDR:
5208*78918900SArne Jansen 		case SO_REUSEPORT:
52097c478bd9Sstevel@tonic-gate 		case SO_KEEPALIVE:
52107c478bd9Sstevel@tonic-gate 		case SO_DONTROUTE:
52117c478bd9Sstevel@tonic-gate 		case SO_BROADCAST:
52127c478bd9Sstevel@tonic-gate 		case SO_USELOOPBACK:
52137c478bd9Sstevel@tonic-gate 		case SO_OOBINLINE:
52147c478bd9Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
52157c478bd9Sstevel@tonic-gate 			value = (so->so_options & option_name);
52167c478bd9Sstevel@tonic-gate #ifdef DEBUG
52177c478bd9Sstevel@tonic-gate 			if (value) {
52187c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
52197c478bd9Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x is set\n",
52207c478bd9Sstevel@tonic-gate 				    option_name));
52217c478bd9Sstevel@tonic-gate 			} else {
52227c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
52237c478bd9Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x not set\n",
52247c478bd9Sstevel@tonic-gate 				    option_name));
52257c478bd9Sstevel@tonic-gate 			}
52267c478bd9Sstevel@tonic-gate #endif /* DEBUG */
52277c478bd9Sstevel@tonic-gate 			option = &value;
52287c478bd9Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
52297c478bd9Sstevel@tonic-gate 
52307c478bd9Sstevel@tonic-gate 		/*
52317c478bd9Sstevel@tonic-gate 		 * The following options are only returned by sockfs when the
52327c478bd9Sstevel@tonic-gate 		 * T_SVR4_OPTMGMT_REQ fails.
52337c478bd9Sstevel@tonic-gate 		 */
52347c478bd9Sstevel@tonic-gate 		case SO_LINGER:
52357c478bd9Sstevel@tonic-gate 			option = &so->so_linger;
52367c478bd9Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (struct linger);
52377c478bd9Sstevel@tonic-gate 			break;
52387c478bd9Sstevel@tonic-gate 		case SO_SNDBUF: {
52397c478bd9Sstevel@tonic-gate 			ssize_t lvalue;
52407c478bd9Sstevel@tonic-gate 
52417c478bd9Sstevel@tonic-gate 			/*
52427c478bd9Sstevel@tonic-gate 			 * If the option has not been set then get a default
52437c478bd9Sstevel@tonic-gate 			 * value from the read queue. This value is
52447c478bd9Sstevel@tonic-gate 			 * returned if the transport fails
52457c478bd9Sstevel@tonic-gate 			 * the T_SVR4_OPTMGMT_REQ.
52467c478bd9Sstevel@tonic-gate 			 */
52477c478bd9Sstevel@tonic-gate 			lvalue = so->so_sndbuf;
52487c478bd9Sstevel@tonic-gate 			if (lvalue == 0) {
52497c478bd9Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
52507c478bd9Sstevel@tonic-gate 				(void) strqget(strvp2wq(SOTOV(so))->q_next,
52517c478bd9Sstevel@tonic-gate 				    QHIWAT, 0, &lvalue);
52527c478bd9Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
52537c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
52547c478bd9Sstevel@tonic-gate 				    ("got SO_SNDBUF %ld from q\n", lvalue));
52557c478bd9Sstevel@tonic-gate 			}
52567c478bd9Sstevel@tonic-gate 			value = (int)lvalue;
52577c478bd9Sstevel@tonic-gate 			option = &value;
52587c478bd9Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (so->so_sndbuf);
52597c478bd9Sstevel@tonic-gate 			break;
52607c478bd9Sstevel@tonic-gate 		}
52617c478bd9Sstevel@tonic-gate 		case SO_RCVBUF: {
52627c478bd9Sstevel@tonic-gate 			ssize_t lvalue;
52637c478bd9Sstevel@tonic-gate 
52647c478bd9Sstevel@tonic-gate 			/*
52657c478bd9Sstevel@tonic-gate 			 * If the option has not been set then get a default
52667c478bd9Sstevel@tonic-gate 			 * value from the read queue. This value is
52677c478bd9Sstevel@tonic-gate 			 * returned if the transport fails
52687c478bd9Sstevel@tonic-gate 			 * the T_SVR4_OPTMGMT_REQ.
52697c478bd9Sstevel@tonic-gate 			 *
52707c478bd9Sstevel@tonic-gate 			 * XXX If SO_RCVBUF has been set and this is an
52717c478bd9Sstevel@tonic-gate 			 * XPG 4.2 application then do not ask the transport
52727c478bd9Sstevel@tonic-gate 			 * since the transport might adjust the value and not
52737c478bd9Sstevel@tonic-gate 			 * return exactly what was set by the application.
52747c478bd9Sstevel@tonic-gate 			 * For non-XPG 4.2 application we return the value
52757c478bd9Sstevel@tonic-gate 			 * that the transport is actually using.
52767c478bd9Sstevel@tonic-gate 			 */
52777c478bd9Sstevel@tonic-gate 			lvalue = so->so_rcvbuf;
52787c478bd9Sstevel@tonic-gate 			if (lvalue == 0) {
52797c478bd9Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
52807c478bd9Sstevel@tonic-gate 				(void) strqget(RD(strvp2wq(SOTOV(so))),
52817c478bd9Sstevel@tonic-gate 				    QHIWAT, 0, &lvalue);
52827c478bd9Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
52837c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
52847c478bd9Sstevel@tonic-gate 				    ("got SO_RCVBUF %ld from q\n", lvalue));
52857c478bd9Sstevel@tonic-gate 			} else if (flags & _SOGETSOCKOPT_XPG4_2) {
52867c478bd9Sstevel@tonic-gate 				value = (int)lvalue;
52877c478bd9Sstevel@tonic-gate 				option = &value;
52887c478bd9Sstevel@tonic-gate 				goto copyout;	/* skip asking transport */
52897c478bd9Sstevel@tonic-gate 			}
52907c478bd9Sstevel@tonic-gate 			value = (int)lvalue;
52917c478bd9Sstevel@tonic-gate 			option = &value;
52927c478bd9Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (so->so_rcvbuf);
52937c478bd9Sstevel@tonic-gate 			break;
52947c478bd9Sstevel@tonic-gate 		}
529588cda078Skcpoon 		case SO_DOMAIN:
529688cda078Skcpoon 			value = so->so_family;
529788cda078Skcpoon 			option = &value;
529888cda078Skcpoon 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
529988cda078Skcpoon 
53007c478bd9Sstevel@tonic-gate #ifdef notyet
53017c478bd9Sstevel@tonic-gate 		/*
53027c478bd9Sstevel@tonic-gate 		 * We do not implement the semantics of these options
53037c478bd9Sstevel@tonic-gate 		 * thus we shouldn't implement the options either.
53047c478bd9Sstevel@tonic-gate 		 */
53057c478bd9Sstevel@tonic-gate 		case SO_SNDLOWAT:
53067c478bd9Sstevel@tonic-gate 			value = so->so_sndlowat;
53077c478bd9Sstevel@tonic-gate 			option = &value;
53087c478bd9Sstevel@tonic-gate 			break;
53097c478bd9Sstevel@tonic-gate 		case SO_RCVLOWAT:
53107c478bd9Sstevel@tonic-gate 			value = so->so_rcvlowat;
53117c478bd9Sstevel@tonic-gate 			option = &value;
53127c478bd9Sstevel@tonic-gate 			break;
53137c478bd9Sstevel@tonic-gate #endif /* notyet */
53140f1702c5SYu Xiangning 		case SO_SNDTIMEO:
53150f1702c5SYu Xiangning 		case SO_RCVTIMEO: {
53160f1702c5SYu Xiangning 			clock_t val;
531722238f73Sshenjian 
53180f1702c5SYu Xiangning 			if (option_name == SO_RCVTIMEO)
53190f1702c5SYu Xiangning 				val = drv_hztousec(so->so_rcvtimeo);
53200f1702c5SYu Xiangning 			else
53210f1702c5SYu Xiangning 				val = drv_hztousec(so->so_sndtimeo);
53220f1702c5SYu Xiangning 			tmo_val.tv_sec = val / (1000 * 1000);
53230f1702c5SYu Xiangning 			tmo_val.tv_usec = val % (1000 * 1000);
5324e5083e81Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
5325e5083e81Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
53260f1702c5SYu Xiangning 				option = &tmo_val;
532722238f73Sshenjian 				len = sizeof (struct timeval);
532822238f73Sshenjian 			} else {
532922238f73Sshenjian 				TIMEVAL_TO_TIMEVAL32(&tmo_val32, &tmo_val);
533022238f73Sshenjian 				option = &tmo_val32;
533122238f73Sshenjian 				len = sizeof (struct timeval32);
533222238f73Sshenjian 			}
53330f1702c5SYu Xiangning 			break;
53340f1702c5SYu Xiangning 		}
53350f1702c5SYu Xiangning 		case SO_SND_BUFINFO: {
53360f1702c5SYu Xiangning 			snd_bufinfo.sbi_wroff =
53370f1702c5SYu Xiangning 			    (so->so_proto_props).sopp_wroff;
53380f1702c5SYu Xiangning 			snd_bufinfo.sbi_maxblk =
53390f1702c5SYu Xiangning 			    (so->so_proto_props).sopp_maxblk;
53400f1702c5SYu Xiangning 			snd_bufinfo.sbi_maxpsz =
53410f1702c5SYu Xiangning 			    (so->so_proto_props).sopp_maxpsz;
53420f1702c5SYu Xiangning 			snd_bufinfo.sbi_tail =
53430f1702c5SYu Xiangning 			    (so->so_proto_props).sopp_tail;
53440f1702c5SYu Xiangning 			option = &snd_bufinfo;
53450f1702c5SYu Xiangning 			len = (t_uscalar_t)sizeof (struct so_snd_bufinfo);
53460f1702c5SYu Xiangning 			break;
53470f1702c5SYu Xiangning 		}
53487c478bd9Sstevel@tonic-gate 		}
53497c478bd9Sstevel@tonic-gate 	}
53507c478bd9Sstevel@tonic-gate 
53517c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
53527c478bd9Sstevel@tonic-gate 
53537c478bd9Sstevel@tonic-gate 	/* Send request */
53547c478bd9Sstevel@tonic-gate 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
53557c478bd9Sstevel@tonic-gate 	optmgmt_req.MGMT_flags = T_CHECK;
53567c478bd9Sstevel@tonic-gate 	optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
53577c478bd9Sstevel@tonic-gate 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
53587c478bd9Sstevel@tonic-gate 
53597c478bd9Sstevel@tonic-gate 	oh.level = level;
53607c478bd9Sstevel@tonic-gate 	oh.name = option_name;
53617c478bd9Sstevel@tonic-gate 	oh.len = maxlen;
53627c478bd9Sstevel@tonic-gate 
53637c478bd9Sstevel@tonic-gate 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5364de8c4a14SErik Nordmark 	    &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP, cr);
53657c478bd9Sstevel@tonic-gate 	/* Let option management work in the presence of data flow control */
53667c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
53677c478bd9Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
53687c478bd9Sstevel@tonic-gate 	mp = NULL;
53697c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
53707c478bd9Sstevel@tonic-gate 	if (error) {
53717c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
53727c478bd9Sstevel@tonic-gate 		goto done2;
53737c478bd9Sstevel@tonic-gate 	}
53747c478bd9Sstevel@tonic-gate 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
53757c478bd9Sstevel@tonic-gate 	    (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
53767c478bd9Sstevel@tonic-gate 	if (error) {
53777c478bd9Sstevel@tonic-gate 		if (option != NULL) {
53787c478bd9Sstevel@tonic-gate 			/* We have a fallback value */
53797c478bd9Sstevel@tonic-gate 			error = 0;
53807c478bd9Sstevel@tonic-gate 			goto copyout;
53817c478bd9Sstevel@tonic-gate 		}
53827c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
53837c478bd9Sstevel@tonic-gate 		goto done2;
53847c478bd9Sstevel@tonic-gate 	}
53857c478bd9Sstevel@tonic-gate 	ASSERT(mp);
53867c478bd9Sstevel@tonic-gate 	optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
53877c478bd9Sstevel@tonic-gate 	opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
53887c478bd9Sstevel@tonic-gate 	    optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
53897c478bd9Sstevel@tonic-gate 	if (opt_res == NULL) {
53907c478bd9Sstevel@tonic-gate 		if (option != NULL) {
53917c478bd9Sstevel@tonic-gate 			/* We have a fallback value */
53927c478bd9Sstevel@tonic-gate 			error = 0;
53937c478bd9Sstevel@tonic-gate 			goto copyout;
53947c478bd9Sstevel@tonic-gate 		}
53957c478bd9Sstevel@tonic-gate 		error = EPROTO;
53967c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
53977c478bd9Sstevel@tonic-gate 		goto done;
53987c478bd9Sstevel@tonic-gate 	}
53997c478bd9Sstevel@tonic-gate 	option = &opt_res[1];
54007c478bd9Sstevel@tonic-gate 
54017c478bd9Sstevel@tonic-gate 	/* check to ensure that the option is within bounds */
54027c478bd9Sstevel@tonic-gate 	if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
54037c478bd9Sstevel@tonic-gate 	    (uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
54047c478bd9Sstevel@tonic-gate 		if (option != NULL) {
54057c478bd9Sstevel@tonic-gate 			/* We have a fallback value */
54067c478bd9Sstevel@tonic-gate 			error = 0;
54077c478bd9Sstevel@tonic-gate 			goto copyout;
54087c478bd9Sstevel@tonic-gate 		}
54097c478bd9Sstevel@tonic-gate 		error = EPROTO;
54107c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
54117c478bd9Sstevel@tonic-gate 		goto done;
54127c478bd9Sstevel@tonic-gate 	}
54137c478bd9Sstevel@tonic-gate 
54147c478bd9Sstevel@tonic-gate 	len = opt_res->len;
54157c478bd9Sstevel@tonic-gate 
54167c478bd9Sstevel@tonic-gate copyout: {
54177c478bd9Sstevel@tonic-gate 		t_uscalar_t size = MIN(len, maxlen);
54187c478bd9Sstevel@tonic-gate 		bcopy(option, optval, size);
54197c478bd9Sstevel@tonic-gate 		bcopy(&size, optlenp, sizeof (size));
54207c478bd9Sstevel@tonic-gate 	}
54217c478bd9Sstevel@tonic-gate done:
54227c478bd9Sstevel@tonic-gate 	freemsg(mp);
54237c478bd9Sstevel@tonic-gate done2:
54247c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
54257c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
54260f1702c5SYu Xiangning 
54277c478bd9Sstevel@tonic-gate 	return (error);
54287c478bd9Sstevel@tonic-gate }
54297c478bd9Sstevel@tonic-gate 
54307c478bd9Sstevel@tonic-gate /*
54317c478bd9Sstevel@tonic-gate  * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
54327c478bd9Sstevel@tonic-gate  * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
54337c478bd9Sstevel@tonic-gate  * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
54347c478bd9Sstevel@tonic-gate  * setsockopt has to work even if the transport does not support the option.
54357c478bd9Sstevel@tonic-gate  */
54360f1702c5SYu Xiangning /* ARGSUSED */
54377c478bd9Sstevel@tonic-gate int
54387c478bd9Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name,
54390f1702c5SYu Xiangning 	const void *optval, t_uscalar_t optlen, struct cred *cr)
54407c478bd9Sstevel@tonic-gate {
54417c478bd9Sstevel@tonic-gate 	struct T_optmgmt_req	optmgmt_req;
54427c478bd9Sstevel@tonic-gate 	struct opthdr		oh;
54437c478bd9Sstevel@tonic-gate 	mblk_t			*mp;
54447c478bd9Sstevel@tonic-gate 	int			error = 0;
54457c478bd9Sstevel@tonic-gate 	boolean_t		handled = B_FALSE;
54467c478bd9Sstevel@tonic-gate 
54477c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
5448903a11ebSrh87107 	    (void *)so, level, option_name, optval, optlen,
54497c478bd9Sstevel@tonic-gate 	    pr_state(so->so_state, so->so_mode)));
54507c478bd9Sstevel@tonic-gate 
54517c478bd9Sstevel@tonic-gate 	/* X/Open requires this check */
54527c478bd9Sstevel@tonic-gate 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
54537c478bd9Sstevel@tonic-gate 		if (xnet_check_print)
54547c478bd9Sstevel@tonic-gate 			printf("sockfs: X/Open setsockopt check => EINVAL\n");
54557c478bd9Sstevel@tonic-gate 		return (EINVAL);
54567c478bd9Sstevel@tonic-gate 	}
54577c478bd9Sstevel@tonic-gate 
54587c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
54597c478bd9Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
54607c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
54617c478bd9Sstevel@tonic-gate 
54627c478bd9Sstevel@tonic-gate 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
54637c478bd9Sstevel@tonic-gate 	optmgmt_req.MGMT_flags = T_NEGOTIATE;
54647c478bd9Sstevel@tonic-gate 	optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
54657c478bd9Sstevel@tonic-gate 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
54667c478bd9Sstevel@tonic-gate 
54677c478bd9Sstevel@tonic-gate 	oh.level = level;
54687c478bd9Sstevel@tonic-gate 	oh.name = option_name;
54697c478bd9Sstevel@tonic-gate 	oh.len = optlen;
54707c478bd9Sstevel@tonic-gate 
54717c478bd9Sstevel@tonic-gate 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
5472de8c4a14SErik Nordmark 	    &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP, cr);
54737c478bd9Sstevel@tonic-gate 	/* Let option management work in the presence of data flow control */
54747c478bd9Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
54757c478bd9Sstevel@tonic-gate 	    MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
54767c478bd9Sstevel@tonic-gate 	mp = NULL;
54777c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
54787c478bd9Sstevel@tonic-gate 	if (error) {
54797c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
54800f1702c5SYu Xiangning 		goto done2;
54817c478bd9Sstevel@tonic-gate 	}
54827c478bd9Sstevel@tonic-gate 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
54837c478bd9Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
54847c478bd9Sstevel@tonic-gate 	if (error) {
54857c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
54867c478bd9Sstevel@tonic-gate 		goto done;
54877c478bd9Sstevel@tonic-gate 	}
54887c478bd9Sstevel@tonic-gate 	ASSERT(mp);
54897c478bd9Sstevel@tonic-gate 	/* No need to verify T_optmgmt_ack */
54907c478bd9Sstevel@tonic-gate 	freemsg(mp);
54917c478bd9Sstevel@tonic-gate done:
54927c478bd9Sstevel@tonic-gate 	/*
54937c478bd9Sstevel@tonic-gate 	 * Check for SOL_SOCKET options and record their values.
54947c478bd9Sstevel@tonic-gate 	 * If we know about a SOL_SOCKET parameter and the transport
54957c478bd9Sstevel@tonic-gate 	 * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
54967c478bd9Sstevel@tonic-gate 	 * EPROTO) we let the setsockopt succeed.
54977c478bd9Sstevel@tonic-gate 	 */
54987c478bd9Sstevel@tonic-gate 	if (level == SOL_SOCKET) {
54997c478bd9Sstevel@tonic-gate 		/* Check parameters */
55007c478bd9Sstevel@tonic-gate 		switch (option_name) {
55017c478bd9Sstevel@tonic-gate 		case SO_DEBUG:
55027c478bd9Sstevel@tonic-gate 		case SO_REUSEADDR:
5503*78918900SArne Jansen 		case SO_REUSEPORT:
55047c478bd9Sstevel@tonic-gate 		case SO_KEEPALIVE:
55057c478bd9Sstevel@tonic-gate 		case SO_DONTROUTE:
55067c478bd9Sstevel@tonic-gate 		case SO_BROADCAST:
55077c478bd9Sstevel@tonic-gate 		case SO_USELOOPBACK:
55087c478bd9Sstevel@tonic-gate 		case SO_OOBINLINE:
55097c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
55107c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
55117c478bd9Sstevel@tonic-gate #ifdef notyet
55127c478bd9Sstevel@tonic-gate 		case SO_SNDLOWAT:
55137c478bd9Sstevel@tonic-gate 		case SO_RCVLOWAT:
55147c478bd9Sstevel@tonic-gate #endif /* notyet */
55157c478bd9Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
55167c478bd9Sstevel@tonic-gate 			if (optlen != (t_uscalar_t)sizeof (int32_t)) {
55177c478bd9Sstevel@tonic-gate 				error = EINVAL;
55187c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
55197c478bd9Sstevel@tonic-gate 				goto done2;
55207c478bd9Sstevel@tonic-gate 			}
55217c478bd9Sstevel@tonic-gate 			ASSERT(optval);
55227c478bd9Sstevel@tonic-gate 			handled = B_TRUE;
55237c478bd9Sstevel@tonic-gate 			break;
55240f1702c5SYu Xiangning 		case SO_SNDTIMEO:
55250f1702c5SYu Xiangning 		case SO_RCVTIMEO:
5526e5083e81Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
5527e5083e81Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE) {
552822238f73Sshenjian 				if (optlen != sizeof (struct timeval)) {
55290f1702c5SYu Xiangning 					error = EINVAL;
55300f1702c5SYu Xiangning 					eprintsoline(so, error);
55310f1702c5SYu Xiangning 					goto done2;
55320f1702c5SYu Xiangning 				}
553322238f73Sshenjian 			} else {
553422238f73Sshenjian 				if (optlen != sizeof (struct timeval32)) {
553522238f73Sshenjian 					error = EINVAL;
553622238f73Sshenjian 					eprintsoline(so, error);
553722238f73Sshenjian 					goto done2;
553822238f73Sshenjian 				}
553922238f73Sshenjian 			}
55400f1702c5SYu Xiangning 			ASSERT(optval);
55410f1702c5SYu Xiangning 			handled = B_TRUE;
55420f1702c5SYu Xiangning 			break;
55437c478bd9Sstevel@tonic-gate 		case SO_LINGER:
55447c478bd9Sstevel@tonic-gate 			if (optlen != (t_uscalar_t)sizeof (struct linger)) {
55457c478bd9Sstevel@tonic-gate 				error = EINVAL;
55467c478bd9Sstevel@tonic-gate 				eprintsoline(so, error);
55477c478bd9Sstevel@tonic-gate 				goto done2;
55487c478bd9Sstevel@tonic-gate 			}
55497c478bd9Sstevel@tonic-gate 			ASSERT(optval);
55507c478bd9Sstevel@tonic-gate 			handled = B_TRUE;
55517c478bd9Sstevel@tonic-gate 			break;
55527c478bd9Sstevel@tonic-gate 		}
55537c478bd9Sstevel@tonic-gate 
55547c478bd9Sstevel@tonic-gate #define	intvalue	(*(int32_t *)optval)
55557c478bd9Sstevel@tonic-gate 
55567c478bd9Sstevel@tonic-gate 		switch (option_name) {
55577c478bd9Sstevel@tonic-gate 		case SO_TYPE:
55587c478bd9Sstevel@tonic-gate 		case SO_ERROR:
55597c478bd9Sstevel@tonic-gate 		case SO_ACCEPTCONN:
55607c478bd9Sstevel@tonic-gate 			/* Can't be set */
55617c478bd9Sstevel@tonic-gate 			error = ENOPROTOOPT;
55627c478bd9Sstevel@tonic-gate 			goto done2;
55637c478bd9Sstevel@tonic-gate 		case SO_LINGER: {
55647c478bd9Sstevel@tonic-gate 			struct linger *l = (struct linger *)optval;
55657c478bd9Sstevel@tonic-gate 
55667c478bd9Sstevel@tonic-gate 			so->so_linger.l_linger = l->l_linger;
55677c478bd9Sstevel@tonic-gate 			if (l->l_onoff) {
55687c478bd9Sstevel@tonic-gate 				so->so_linger.l_onoff = SO_LINGER;
55697c478bd9Sstevel@tonic-gate 				so->so_options |= SO_LINGER;
55707c478bd9Sstevel@tonic-gate 			} else {
55717c478bd9Sstevel@tonic-gate 				so->so_linger.l_onoff = 0;
55727c478bd9Sstevel@tonic-gate 				so->so_options &= ~SO_LINGER;
55737c478bd9Sstevel@tonic-gate 			}
55747c478bd9Sstevel@tonic-gate 			break;
55757c478bd9Sstevel@tonic-gate 		}
55767c478bd9Sstevel@tonic-gate 
55777c478bd9Sstevel@tonic-gate 		case SO_DEBUG:
55787c478bd9Sstevel@tonic-gate #ifdef SOCK_TEST
55797c478bd9Sstevel@tonic-gate 			if (intvalue & 2)
55807c478bd9Sstevel@tonic-gate 				sock_test_timelimit = 10 * hz;
55817c478bd9Sstevel@tonic-gate 			else
55827c478bd9Sstevel@tonic-gate 				sock_test_timelimit = 0;
55837c478bd9Sstevel@tonic-gate 
55847c478bd9Sstevel@tonic-gate 			if (intvalue & 4)
55857c478bd9Sstevel@tonic-gate 				do_useracc = 0;
55867c478bd9Sstevel@tonic-gate 			else
55877c478bd9Sstevel@tonic-gate 				do_useracc = 1;
55887c478bd9Sstevel@tonic-gate #endif /* SOCK_TEST */
55897c478bd9Sstevel@tonic-gate 			/* FALLTHRU */
55907c478bd9Sstevel@tonic-gate 		case SO_REUSEADDR:
5591*78918900SArne Jansen 		case SO_REUSEPORT:
55927c478bd9Sstevel@tonic-gate 		case SO_KEEPALIVE:
55937c478bd9Sstevel@tonic-gate 		case SO_DONTROUTE:
55947c478bd9Sstevel@tonic-gate 		case SO_BROADCAST:
55957c478bd9Sstevel@tonic-gate 		case SO_USELOOPBACK:
55967c478bd9Sstevel@tonic-gate 		case SO_OOBINLINE:
55977c478bd9Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
55987c478bd9Sstevel@tonic-gate 			if (intvalue != 0) {
55997c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
56000f1702c5SYu Xiangning 				    ("socket_setsockopt: setting 0x%x\n",
56017c478bd9Sstevel@tonic-gate 				    option_name));
56027c478bd9Sstevel@tonic-gate 				so->so_options |= option_name;
56037c478bd9Sstevel@tonic-gate 			} else {
56047c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
56050f1702c5SYu Xiangning 				    ("socket_setsockopt: clearing 0x%x\n",
56067c478bd9Sstevel@tonic-gate 				    option_name));
56077c478bd9Sstevel@tonic-gate 				so->so_options &= ~option_name;
56087c478bd9Sstevel@tonic-gate 			}
56097c478bd9Sstevel@tonic-gate 			break;
56107c478bd9Sstevel@tonic-gate 		/*
56117c478bd9Sstevel@tonic-gate 		 * The following options are only returned by us when the
56120f1702c5SYu Xiangning 		 * transport layer fails.
56137c478bd9Sstevel@tonic-gate 		 * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
56147c478bd9Sstevel@tonic-gate 		 * since the transport might adjust the value and not
56157c478bd9Sstevel@tonic-gate 		 * return exactly what was set by the application.
56167c478bd9Sstevel@tonic-gate 		 */
56177c478bd9Sstevel@tonic-gate 		case SO_SNDBUF:
56187c478bd9Sstevel@tonic-gate 			so->so_sndbuf = intvalue;
56197c478bd9Sstevel@tonic-gate 			break;
56207c478bd9Sstevel@tonic-gate 		case SO_RCVBUF:
56217c478bd9Sstevel@tonic-gate 			so->so_rcvbuf = intvalue;
56227c478bd9Sstevel@tonic-gate 			break;
56230f1702c5SYu Xiangning 		case SO_RCVPSH:
56240f1702c5SYu Xiangning 			so->so_rcv_timer_interval = intvalue;
56250f1702c5SYu Xiangning 			break;
56267c478bd9Sstevel@tonic-gate #ifdef notyet
56277c478bd9Sstevel@tonic-gate 		/*
56287c478bd9Sstevel@tonic-gate 		 * We do not implement the semantics of these options
56297c478bd9Sstevel@tonic-gate 		 * thus we shouldn't implement the options either.
56307c478bd9Sstevel@tonic-gate 		 */
56317c478bd9Sstevel@tonic-gate 		case SO_SNDLOWAT:
56327c478bd9Sstevel@tonic-gate 			so->so_sndlowat = intvalue;
56337c478bd9Sstevel@tonic-gate 			break;
56347c478bd9Sstevel@tonic-gate 		case SO_RCVLOWAT:
56357c478bd9Sstevel@tonic-gate 			so->so_rcvlowat = intvalue;
56367c478bd9Sstevel@tonic-gate 			break;
56377c478bd9Sstevel@tonic-gate #endif /* notyet */
56380f1702c5SYu Xiangning 		case SO_SNDTIMEO:
56390f1702c5SYu Xiangning 		case SO_RCVTIMEO: {
564022238f73Sshenjian 			struct timeval tl;
564122238f73Sshenjian 			clock_t val;
564222238f73Sshenjian 
5643e5083e81Sshenjian 			if (get_udatamodel() == DATAMODEL_NONE ||
5644e5083e81Sshenjian 			    get_udatamodel() == DATAMODEL_NATIVE)
564522238f73Sshenjian 				bcopy(&tl, (struct timeval *)optval,
564622238f73Sshenjian 				    sizeof (struct timeval));
564722238f73Sshenjian 			else
564822238f73Sshenjian 				TIMEVAL32_TO_TIMEVAL(&tl,
564922238f73Sshenjian 				    (struct timeval32 *)optval);
565022238f73Sshenjian 			val = tl.tv_sec * 1000 * 1000 + tl.tv_usec;
56510f1702c5SYu Xiangning 			if (option_name == SO_RCVTIMEO)
56520f1702c5SYu Xiangning 				so->so_rcvtimeo = drv_usectohz(val);
56530f1702c5SYu Xiangning 			else
56540f1702c5SYu Xiangning 				so->so_sndtimeo = drv_usectohz(val);
56550f1702c5SYu Xiangning 			break;
56560f1702c5SYu Xiangning 		}
56577c478bd9Sstevel@tonic-gate 		}
56587c478bd9Sstevel@tonic-gate #undef	intvalue
56597c478bd9Sstevel@tonic-gate 
56607c478bd9Sstevel@tonic-gate 		if (error) {
56617c478bd9Sstevel@tonic-gate 			if ((error == ENOPROTOOPT || error == EPROTO ||
56627c478bd9Sstevel@tonic-gate 			    error == EINVAL) && handled) {
56637c478bd9Sstevel@tonic-gate 				dprintso(so, 1,
56647c478bd9Sstevel@tonic-gate 				    ("setsockopt: ignoring error %d for 0x%x\n",
56657c478bd9Sstevel@tonic-gate 				    error, option_name));
56667c478bd9Sstevel@tonic-gate 				error = 0;
56677c478bd9Sstevel@tonic-gate 			}
56687c478bd9Sstevel@tonic-gate 		}
56697c478bd9Sstevel@tonic-gate 	}
56707c478bd9Sstevel@tonic-gate done2:
56717c478bd9Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
56727c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
56737c478bd9Sstevel@tonic-gate 	return (error);
56747c478bd9Sstevel@tonic-gate }
56750f1702c5SYu Xiangning 
5676f0267584Sanders /*
5677f0267584Sanders  * sotpi_close() is called when the last open reference goes away.
5678f0267584Sanders  */
56790f1702c5SYu Xiangning /* ARGSUSED */
56800f1702c5SYu Xiangning int
56810f1702c5SYu Xiangning sotpi_close(struct sonode *so, int flag, struct cred *cr)
56820f1702c5SYu Xiangning {
56830f1702c5SYu Xiangning 	struct vnode *vp = SOTOV(so);
56840f1702c5SYu Xiangning 	dev_t dev;
56850f1702c5SYu Xiangning 	int error = 0;
56860f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
56870f1702c5SYu Xiangning 
56880f1702c5SYu Xiangning 	dprintso(so, 1, ("sotpi_close(%p, %x) %s\n",
56890f1702c5SYu Xiangning 	    (void *)vp, flag, pr_state(so->so_state, so->so_mode)));
56900f1702c5SYu Xiangning 
56910f1702c5SYu Xiangning 	dev = sti->sti_dev;
56920f1702c5SYu Xiangning 
56930f1702c5SYu Xiangning 	ASSERT(STREAMSTAB(getmajor(dev)));
56940f1702c5SYu Xiangning 
56950f1702c5SYu Xiangning 	mutex_enter(&so->so_lock);
56960f1702c5SYu Xiangning 	so_lock_single(so);	/* Set SOLOCKED */
56970f1702c5SYu Xiangning 
5698d36be52eSRao Shoaib 	ASSERT(so_verify_oobstate(so));
5699d36be52eSRao Shoaib 
57000f1702c5SYu Xiangning 	if (sti->sti_nl7c_flags & NL7C_ENABLED) {
57010f1702c5SYu Xiangning 		sti->sti_nl7c_flags = 0;
57020f1702c5SYu Xiangning 		nl7c_close(so);
57030f1702c5SYu Xiangning 	}
57040f1702c5SYu Xiangning 
57050f1702c5SYu Xiangning 	if (vp->v_stream != NULL) {
57060f1702c5SYu Xiangning 		vnode_t *ux_vp;
57070f1702c5SYu Xiangning 
57080f1702c5SYu Xiangning 		if (so->so_family == AF_UNIX) {
57090f1702c5SYu Xiangning 			/* Could avoid this when CANTSENDMORE for !dgram */
57100f1702c5SYu Xiangning 			so_unix_close(so);
57110f1702c5SYu Xiangning 		}
57120f1702c5SYu Xiangning 
57130f1702c5SYu Xiangning 		mutex_exit(&so->so_lock);
57140f1702c5SYu Xiangning 		/*
57150f1702c5SYu Xiangning 		 * Disassemble the linkage from the AF_UNIX underlying file
57160f1702c5SYu Xiangning 		 * system vnode to this socket (by atomically clearing
57170f1702c5SYu Xiangning 		 * v_stream in vn_rele_stream) before strclose clears sd_vnode
57180f1702c5SYu Xiangning 		 * and frees the stream head.
57190f1702c5SYu Xiangning 		 */
57200f1702c5SYu Xiangning 		if ((ux_vp = sti->sti_ux_bound_vp) != NULL) {
57210f1702c5SYu Xiangning 			ASSERT(ux_vp->v_stream);
57220f1702c5SYu Xiangning 			sti->sti_ux_bound_vp = NULL;
57230f1702c5SYu Xiangning 			vn_rele_stream(ux_vp);
57240f1702c5SYu Xiangning 		}
57250f1702c5SYu Xiangning 		error = strclose(vp, flag, cr);
57260f1702c5SYu Xiangning 		vp->v_stream = NULL;
57270f1702c5SYu Xiangning 		mutex_enter(&so->so_lock);
57280f1702c5SYu Xiangning 	}
57290f1702c5SYu Xiangning 
57300f1702c5SYu Xiangning 	/*
57310f1702c5SYu Xiangning 	 * Flush the T_DISCON_IND on sti_discon_ind_mp.
57320f1702c5SYu Xiangning 	 */
57330f1702c5SYu Xiangning 	so_flush_discon_ind(so);
57340f1702c5SYu Xiangning 
57350f1702c5SYu Xiangning 	so_unlock_single(so, SOLOCKED);
57360f1702c5SYu Xiangning 	mutex_exit(&so->so_lock);
57370f1702c5SYu Xiangning 
57380f1702c5SYu Xiangning 	/*
57390f1702c5SYu Xiangning 	 * Needed for STREAMs.
57400f1702c5SYu Xiangning 	 * Decrement the device driver's reference count for streams
57410f1702c5SYu Xiangning 	 * opened via the clone dip. The driver was held in clone_open().
57420f1702c5SYu Xiangning 	 * The absence of clone_close() forces this asymmetry.
57430f1702c5SYu Xiangning 	 */
57440f1702c5SYu Xiangning 	if (so->so_flag & SOCLONE)
57450f1702c5SYu Xiangning 		ddi_rele_driver(getmajor(dev));
57460f1702c5SYu Xiangning 
57470f1702c5SYu Xiangning 	return (error);
57480f1702c5SYu Xiangning }
57490f1702c5SYu Xiangning 
57500f1702c5SYu Xiangning static int
57510f1702c5SYu Xiangning sotpi_ioctl(struct sonode *so, int cmd, intptr_t arg, int mode,
57520f1702c5SYu Xiangning     struct cred *cr, int32_t *rvalp)
57530f1702c5SYu Xiangning {
57540f1702c5SYu Xiangning 	struct vnode *vp = SOTOV(so);
57550f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
57560f1702c5SYu Xiangning 	int error = 0;
57570f1702c5SYu Xiangning 
57580f1702c5SYu Xiangning 	dprintso(so, 0, ("sotpi_ioctl: cmd 0x%x, arg 0x%lx, state %s\n",
57590f1702c5SYu Xiangning 	    cmd, arg, pr_state(so->so_state, so->so_mode)));
57600f1702c5SYu Xiangning 
57610f1702c5SYu Xiangning 	switch (cmd) {
5762bfcb55b8SRao Shoaib 	case SIOCSQPTR:
5763bfcb55b8SRao Shoaib 		/*
5764bfcb55b8SRao Shoaib 		 * SIOCSQPTR is valid only when helper stream is created
5765bfcb55b8SRao Shoaib 		 * by the protocol.
5766bfcb55b8SRao Shoaib 		 */
57670f1702c5SYu Xiangning 	case _I_INSERT:
57680f1702c5SYu Xiangning 	case _I_REMOVE:
57690f1702c5SYu Xiangning 		/*
57700f1702c5SYu Xiangning 		 * Since there's no compelling reason to support these ioctls
57710f1702c5SYu Xiangning 		 * on sockets, and doing so would increase the complexity
57720f1702c5SYu Xiangning 		 * markedly, prevent it.
57730f1702c5SYu Xiangning 		 */
57740f1702c5SYu Xiangning 		return (EOPNOTSUPP);
57750f1702c5SYu Xiangning 
57760f1702c5SYu Xiangning 	case I_FIND:
57770f1702c5SYu Xiangning 	case I_LIST:
57780f1702c5SYu Xiangning 	case I_LOOK:
57790f1702c5SYu Xiangning 	case I_POP:
57800f1702c5SYu Xiangning 	case I_PUSH:
57810f1702c5SYu Xiangning 		/*
57820f1702c5SYu Xiangning 		 * To prevent races and inconsistencies between the actual
57830f1702c5SYu Xiangning 		 * state of the stream and the state according to the sonode,
57840f1702c5SYu Xiangning 		 * we serialize all operations which modify or operate on the
57850f1702c5SYu Xiangning 		 * list of modules on the socket's stream.
57860f1702c5SYu Xiangning 		 */
57870f1702c5SYu Xiangning 		mutex_enter(&sti->sti_plumb_lock);
57880f1702c5SYu Xiangning 		error = socktpi_plumbioctl(vp, cmd, arg, mode, cr, rvalp);
57890f1702c5SYu Xiangning 		mutex_exit(&sti->sti_plumb_lock);
57900f1702c5SYu Xiangning 		return (error);
57910f1702c5SYu Xiangning 
57920f1702c5SYu Xiangning 	default:
57930f1702c5SYu Xiangning 		if (so->so_version != SOV_STREAM)
57940f1702c5SYu Xiangning 			break;
57950f1702c5SYu Xiangning 
57960f1702c5SYu Xiangning 		/*
57970f1702c5SYu Xiangning 		 * The imaginary "sockmod" has been popped; act as a stream.
57980f1702c5SYu Xiangning 		 */
57990f1702c5SYu Xiangning 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
58000f1702c5SYu Xiangning 	}
58010f1702c5SYu Xiangning 
58020f1702c5SYu Xiangning 	ASSERT(so->so_version != SOV_STREAM);
58030f1702c5SYu Xiangning 
58040f1702c5SYu Xiangning 	/*
58050f1702c5SYu Xiangning 	 * Process socket-specific ioctls.
58060f1702c5SYu Xiangning 	 */
58070f1702c5SYu Xiangning 	switch (cmd) {
58080f1702c5SYu Xiangning 	case FIONBIO: {
58090f1702c5SYu Xiangning 		int32_t value;
58100f1702c5SYu Xiangning 
58110f1702c5SYu Xiangning 		if (so_copyin((void *)arg, &value, sizeof (int32_t),
58120f1702c5SYu Xiangning 		    (mode & (int)FKIOCTL)))
58130f1702c5SYu Xiangning 			return (EFAULT);
58140f1702c5SYu Xiangning 
58150f1702c5SYu Xiangning 		mutex_enter(&so->so_lock);
58160f1702c5SYu Xiangning 		if (value) {
58170f1702c5SYu Xiangning 			so->so_state |= SS_NDELAY;
58180f1702c5SYu Xiangning 		} else {
58190f1702c5SYu Xiangning 			so->so_state &= ~SS_NDELAY;
58200f1702c5SYu Xiangning 		}
58210f1702c5SYu Xiangning 		mutex_exit(&so->so_lock);
58220f1702c5SYu Xiangning 		return (0);
58230f1702c5SYu Xiangning 	}
58240f1702c5SYu Xiangning 
58250f1702c5SYu Xiangning 	case FIOASYNC: {
58260f1702c5SYu Xiangning 		int32_t value;
58270f1702c5SYu Xiangning 
58280f1702c5SYu Xiangning 		if (so_copyin((void *)arg, &value, sizeof (int32_t),
58290f1702c5SYu Xiangning 		    (mode & (int)FKIOCTL)))
58300f1702c5SYu Xiangning 			return (EFAULT);
58310f1702c5SYu Xiangning 
58320f1702c5SYu Xiangning 		mutex_enter(&so->so_lock);
58330f1702c5SYu Xiangning 		/*
58340f1702c5SYu Xiangning 		 * SS_ASYNC flag not already set correctly?
58350f1702c5SYu Xiangning 		 * (!value != !(so->so_state & SS_ASYNC))
58360f1702c5SYu Xiangning 		 * but some engineers find that too hard to read.
58370f1702c5SYu Xiangning 		 */
58380f1702c5SYu Xiangning 		if (value == 0 && (so->so_state & SS_ASYNC) != 0 ||
58390f1702c5SYu Xiangning 		    value != 0 && (so->so_state & SS_ASYNC) == 0)
58400f1702c5SYu Xiangning 			error = so_flip_async(so, vp, mode, cr);
58410f1702c5SYu Xiangning 		mutex_exit(&so->so_lock);
58420f1702c5SYu Xiangning 		return (error);
58430f1702c5SYu Xiangning 	}
58440f1702c5SYu Xiangning 
58450f1702c5SYu Xiangning 	case SIOCSPGRP:
58460f1702c5SYu Xiangning 	case FIOSETOWN: {
58470f1702c5SYu Xiangning 		pid_t pgrp;
58480f1702c5SYu Xiangning 
58490f1702c5SYu Xiangning 		if (so_copyin((void *)arg, &pgrp, sizeof (pid_t),
58500f1702c5SYu Xiangning 		    (mode & (int)FKIOCTL)))
58510f1702c5SYu Xiangning 			return (EFAULT);
58520f1702c5SYu Xiangning 
58530f1702c5SYu Xiangning 		mutex_enter(&so->so_lock);
58540f1702c5SYu Xiangning 		dprintso(so, 1, ("setown: new %d old %d\n", pgrp, so->so_pgrp));
58550f1702c5SYu Xiangning 		/* Any change? */
58560f1702c5SYu Xiangning 		if (pgrp != so->so_pgrp)
58570f1702c5SYu Xiangning 			error = so_set_siggrp(so, vp, pgrp, mode, cr);
58580f1702c5SYu Xiangning 		mutex_exit(&so->so_lock);
58590f1702c5SYu Xiangning 		return (error);
58600f1702c5SYu Xiangning 	}
58610f1702c5SYu Xiangning 	case SIOCGPGRP:
58620f1702c5SYu Xiangning 	case FIOGETOWN:
58630f1702c5SYu Xiangning 		if (so_copyout(&so->so_pgrp, (void *)arg,
58640f1702c5SYu Xiangning 		    sizeof (pid_t), (mode & (int)FKIOCTL)))
58650f1702c5SYu Xiangning 			return (EFAULT);
58660f1702c5SYu Xiangning 		return (0);
58670f1702c5SYu Xiangning 
58680f1702c5SYu Xiangning 	case SIOCATMARK: {
58690f1702c5SYu Xiangning 		int retval;
58700f1702c5SYu Xiangning 		uint_t so_state;
58710f1702c5SYu Xiangning 
58720f1702c5SYu Xiangning 		/*
58730f1702c5SYu Xiangning 		 * strwaitmark has a finite timeout after which it
58740f1702c5SYu Xiangning 		 * returns -1 if the mark state is undetermined.
58750f1702c5SYu Xiangning 		 * In order to avoid any race between the mark state
58760f1702c5SYu Xiangning 		 * in sockfs and the mark state in the stream head this
58770f1702c5SYu Xiangning 		 * routine loops until the mark state can be determined
58780f1702c5SYu Xiangning 		 * (or the urgent data indication has been removed by some
58790f1702c5SYu Xiangning 		 * other thread).
58800f1702c5SYu Xiangning 		 */
58810f1702c5SYu Xiangning 		do {
58820f1702c5SYu Xiangning 			mutex_enter(&so->so_lock);
58830f1702c5SYu Xiangning 			so_state = so->so_state;
58840f1702c5SYu Xiangning 			mutex_exit(&so->so_lock);
58850f1702c5SYu Xiangning 			if (so_state & SS_RCVATMARK) {
58860f1702c5SYu Xiangning 				retval = 1;
58870f1702c5SYu Xiangning 			} else if (!(so_state & SS_OOBPEND)) {
58880f1702c5SYu Xiangning 				/*
58890f1702c5SYu Xiangning 				 * No SIGURG has been generated -- there is no
58900f1702c5SYu Xiangning 				 * pending or present urgent data. Thus can't
58910f1702c5SYu Xiangning 				 * possibly be at the mark.
58920f1702c5SYu Xiangning 				 */
58930f1702c5SYu Xiangning 				retval = 0;
58940f1702c5SYu Xiangning 			} else {
58950f1702c5SYu Xiangning 				/*
58960f1702c5SYu Xiangning 				 * Have the stream head wait until there is
58970f1702c5SYu Xiangning 				 * either some messages on the read queue, or
58980f1702c5SYu Xiangning 				 * STRATMARK or STRNOTATMARK gets set. The
58990f1702c5SYu Xiangning 				 * STRNOTATMARK flag is used so that the
59000f1702c5SYu Xiangning 				 * transport can send up a MSGNOTMARKNEXT
59010f1702c5SYu Xiangning 				 * M_DATA to indicate that it is not
59020f1702c5SYu Xiangning 				 * at the mark and additional data is not about
59030f1702c5SYu Xiangning 				 * to be send upstream.
59040f1702c5SYu Xiangning 				 *
59050f1702c5SYu Xiangning 				 * If the mark state is undetermined this will
59060f1702c5SYu Xiangning 				 * return -1 and we will loop rechecking the
59070f1702c5SYu Xiangning 				 * socket state.
59080f1702c5SYu Xiangning 				 */
59090f1702c5SYu Xiangning 				retval = strwaitmark(vp);
59100f1702c5SYu Xiangning 			}
59110f1702c5SYu Xiangning 		} while (retval == -1);
59120f1702c5SYu Xiangning 
59130f1702c5SYu Xiangning 		if (so_copyout(&retval, (void *)arg, sizeof (int),
59140f1702c5SYu Xiangning 		    (mode & (int)FKIOCTL)))
59150f1702c5SYu Xiangning 			return (EFAULT);
59160f1702c5SYu Xiangning 		return (0);
59170f1702c5SYu Xiangning 	}
59180f1702c5SYu Xiangning 
59190f1702c5SYu Xiangning 	case I_FDINSERT:
59200f1702c5SYu Xiangning 	case I_SENDFD:
59210f1702c5SYu Xiangning 	case I_RECVFD:
59220f1702c5SYu Xiangning 	case I_ATMARK:
59230f1702c5SYu Xiangning 	case _SIOCSOCKFALLBACK:
59240f1702c5SYu Xiangning 		/*
59250f1702c5SYu Xiangning 		 * These ioctls do not apply to sockets. I_FDINSERT can be
59260f1702c5SYu Xiangning 		 * used to send M_PROTO messages without modifying the socket
59270f1702c5SYu Xiangning 		 * state. I_SENDFD/RECVFD should not be used for socket file
59280f1702c5SYu Xiangning 		 * descriptor passing since they assume a twisted stream.
59290f1702c5SYu Xiangning 		 * SIOCATMARK must be used instead of I_ATMARK.
59300f1702c5SYu Xiangning 		 *
59310f1702c5SYu Xiangning 		 * _SIOCSOCKFALLBACK from an application should never be
59320f1702c5SYu Xiangning 		 * processed.  It is only generated by socktpi_open() or
59330f1702c5SYu Xiangning 		 * in response to I_POP or I_PUSH.
59340f1702c5SYu Xiangning 		 */
59350f1702c5SYu Xiangning #ifdef DEBUG
59360f1702c5SYu Xiangning 		zcmn_err(getzoneid(), CE_WARN,
59370f1702c5SYu Xiangning 		    "Unsupported STREAMS ioctl 0x%x on socket. "
59380f1702c5SYu Xiangning 		    "Pid = %d\n", cmd, curproc->p_pid);
59390f1702c5SYu Xiangning #endif /* DEBUG */
59400f1702c5SYu Xiangning 		return (EOPNOTSUPP);
59410f1702c5SYu Xiangning 
59420f1702c5SYu Xiangning 	case _I_GETPEERCRED:
59430f1702c5SYu Xiangning 		if ((mode & FKIOCTL) == 0)
59440f1702c5SYu Xiangning 			return (EINVAL);
59450f1702c5SYu Xiangning 
59460f1702c5SYu Xiangning 		mutex_enter(&so->so_lock);
59470f1702c5SYu Xiangning 		if ((so->so_mode & SM_CONNREQUIRED) == 0) {
59480f1702c5SYu Xiangning 			error = ENOTSUP;
59490f1702c5SYu Xiangning 		} else if ((so->so_state & SS_ISCONNECTED) == 0) {
59500f1702c5SYu Xiangning 			error = ENOTCONN;
59510f1702c5SYu Xiangning 		} else if (so->so_peercred != NULL) {
59520f1702c5SYu Xiangning 			k_peercred_t *kp = (k_peercred_t *)arg;
59530f1702c5SYu Xiangning 			kp->pc_cr = so->so_peercred;
59540f1702c5SYu Xiangning 			kp->pc_cpid = so->so_cpid;
59550f1702c5SYu Xiangning 			crhold(so->so_peercred);
59560f1702c5SYu Xiangning 		} else {
59570f1702c5SYu Xiangning 			error = EINVAL;
59580f1702c5SYu Xiangning 		}
59590f1702c5SYu Xiangning 		mutex_exit(&so->so_lock);
59600f1702c5SYu Xiangning 		return (error);
59610f1702c5SYu Xiangning 
59620f1702c5SYu Xiangning 	default:
59630f1702c5SYu Xiangning 		/*
59640f1702c5SYu Xiangning 		 * Do the higher-order bits of the ioctl cmd indicate
59650f1702c5SYu Xiangning 		 * that it is an I_* streams ioctl?
59660f1702c5SYu Xiangning 		 */
59670f1702c5SYu Xiangning 		if ((cmd & 0xffffff00U) == STR &&
59680f1702c5SYu Xiangning 		    so->so_version == SOV_SOCKBSD) {
59690f1702c5SYu Xiangning #ifdef DEBUG
59700f1702c5SYu Xiangning 			zcmn_err(getzoneid(), CE_WARN,
59710f1702c5SYu Xiangning 			    "Unsupported STREAMS ioctl 0x%x on socket. "
59720f1702c5SYu Xiangning 			    "Pid = %d\n", cmd, 	curproc->p_pid);
59730f1702c5SYu Xiangning #endif /* DEBUG */
59740f1702c5SYu Xiangning 			return (EOPNOTSUPP);
59750f1702c5SYu Xiangning 		}
59760f1702c5SYu Xiangning 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
59770f1702c5SYu Xiangning 	}
59780f1702c5SYu Xiangning }
59790f1702c5SYu Xiangning 
59800f1702c5SYu Xiangning /*
59810f1702c5SYu Xiangning  * Handle plumbing-related ioctls.
59820f1702c5SYu Xiangning  */
59830f1702c5SYu Xiangning static int
59840f1702c5SYu Xiangning socktpi_plumbioctl(struct vnode *vp, int cmd, intptr_t arg, int mode,
59850f1702c5SYu Xiangning     struct cred *cr, int32_t *rvalp)
59860f1702c5SYu Xiangning {
59870f1702c5SYu Xiangning 	static const char sockmod_name[] = "sockmod";
59880f1702c5SYu Xiangning 	struct sonode	*so = VTOSO(vp);
59890f1702c5SYu Xiangning 	char		mname[FMNAMESZ + 1];
59900f1702c5SYu Xiangning 	int		error;
59910f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
59920f1702c5SYu Xiangning 
59930f1702c5SYu Xiangning 	ASSERT(MUTEX_HELD(&sti->sti_plumb_lock));
59940f1702c5SYu Xiangning 
59950f1702c5SYu Xiangning 	if (so->so_version == SOV_SOCKBSD)
59960f1702c5SYu Xiangning 		return (EOPNOTSUPP);
59970f1702c5SYu Xiangning 
59980f1702c5SYu Xiangning 	if (so->so_version == SOV_STREAM) {
59990f1702c5SYu Xiangning 		/*
60000f1702c5SYu Xiangning 		 * The imaginary "sockmod" has been popped - act as a stream.
60010f1702c5SYu Xiangning 		 * If this is a push of sockmod then change back to a socket.
60020f1702c5SYu Xiangning 		 */
60030f1702c5SYu Xiangning 		if (cmd == I_PUSH) {
60040f1702c5SYu Xiangning 			error = ((mode & FKIOCTL) ? copystr : copyinstr)(
60050f1702c5SYu Xiangning 			    (void *)arg, mname, sizeof (mname), NULL);
60060f1702c5SYu Xiangning 
60070f1702c5SYu Xiangning 			if (error == 0 && strcmp(mname, sockmod_name) == 0) {
60080f1702c5SYu Xiangning 				dprintso(so, 0, ("socktpi_ioctl: going to "
60090f1702c5SYu Xiangning 				    "socket version\n"));
60100f1702c5SYu Xiangning 				so_stream2sock(so);
60110f1702c5SYu Xiangning 				return (0);
60120f1702c5SYu Xiangning 			}
60130f1702c5SYu Xiangning 		}
60140f1702c5SYu Xiangning 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
60150f1702c5SYu Xiangning 	}
60160f1702c5SYu Xiangning 
60170f1702c5SYu Xiangning 	switch (cmd) {
60180f1702c5SYu Xiangning 	case I_PUSH:
60190f1702c5SYu Xiangning 		if (sti->sti_direct) {
60200f1702c5SYu Xiangning 			mutex_enter(&so->so_lock);
60210f1702c5SYu Xiangning 			so_lock_single(so);
60220f1702c5SYu Xiangning 			mutex_exit(&so->so_lock);
60230f1702c5SYu Xiangning 
60240f1702c5SYu Xiangning 			error = strioctl(vp, _SIOCSOCKFALLBACK, 0, 0, K_TO_K,
6025de8c4a14SErik Nordmark 			    cr, rvalp);
60260f1702c5SYu Xiangning 
60270f1702c5SYu Xiangning 			mutex_enter(&so->so_lock);
60280f1702c5SYu Xiangning 			if (error == 0)
60290f1702c5SYu Xiangning 				sti->sti_direct = 0;
60300f1702c5SYu Xiangning 			so_unlock_single(so, SOLOCKED);
60310f1702c5SYu Xiangning 			mutex_exit(&so->so_lock);
60320f1702c5SYu Xiangning 
60330f1702c5SYu Xiangning 			if (error != 0)
60340f1702c5SYu Xiangning 				return (error);
60350f1702c5SYu Xiangning 		}
60360f1702c5SYu Xiangning 
60370f1702c5SYu Xiangning 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60380f1702c5SYu Xiangning 		if (error == 0)
60390f1702c5SYu Xiangning 			sti->sti_pushcnt++;
60400f1702c5SYu Xiangning 		return (error);
60410f1702c5SYu Xiangning 
60420f1702c5SYu Xiangning 	case I_POP:
60430f1702c5SYu Xiangning 		if (sti->sti_pushcnt == 0) {
60440f1702c5SYu Xiangning 			/* Emulate sockmod being popped */
60450f1702c5SYu Xiangning 			dprintso(so, 0,
60460f1702c5SYu Xiangning 			    ("socktpi_ioctl: going to STREAMS version\n"));
60470f1702c5SYu Xiangning 			return (so_sock2stream(so));
60480f1702c5SYu Xiangning 		}
60490f1702c5SYu Xiangning 
60500f1702c5SYu Xiangning 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60510f1702c5SYu Xiangning 		if (error == 0)
60520f1702c5SYu Xiangning 			sti->sti_pushcnt--;
60530f1702c5SYu Xiangning 		return (error);
60540f1702c5SYu Xiangning 
60550f1702c5SYu Xiangning 	case I_LIST: {
60560f1702c5SYu Xiangning 		struct str_mlist *kmlistp, *umlistp;
60570f1702c5SYu Xiangning 		struct str_list	kstrlist;
60580f1702c5SYu Xiangning 		ssize_t		kstrlistsize;
60590f1702c5SYu Xiangning 		int		i, nmods;
60600f1702c5SYu Xiangning 
60610f1702c5SYu Xiangning 		STRUCT_DECL(str_list, ustrlist);
60620f1702c5SYu Xiangning 		STRUCT_INIT(ustrlist, mode);
60630f1702c5SYu Xiangning 
60640f1702c5SYu Xiangning 		if (arg == NULL) {
60650f1702c5SYu Xiangning 			error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
60660f1702c5SYu Xiangning 			if (error == 0)
60670f1702c5SYu Xiangning 				(*rvalp)++;	/* Add one for sockmod */
60680f1702c5SYu Xiangning 			return (error);
60690f1702c5SYu Xiangning 		}
60700f1702c5SYu Xiangning 
60710f1702c5SYu Xiangning 		error = so_copyin((void *)arg, STRUCT_BUF(ustrlist),
60720f1702c5SYu Xiangning 		    STRUCT_SIZE(ustrlist), mode & FKIOCTL);
60730f1702c5SYu Xiangning 		if (error != 0)
60740f1702c5SYu Xiangning 			return (error);
60750f1702c5SYu Xiangning 
60760f1702c5SYu Xiangning 		nmods = STRUCT_FGET(ustrlist, sl_nmods);
60770f1702c5SYu Xiangning 		if (nmods <= 0)
60780f1702c5SYu Xiangning 			return (EINVAL);
60790f1702c5SYu Xiangning 		/*
60800f1702c5SYu Xiangning 		 * Ceiling nmods at nstrpush to prevent someone from
60810f1702c5SYu Xiangning 		 * maliciously consuming lots of kernel memory.
60820f1702c5SYu Xiangning 		 */
60830f1702c5SYu Xiangning 		nmods = MIN(nmods, nstrpush);
60840f1702c5SYu Xiangning 
60850f1702c5SYu Xiangning 		kstrlistsize = (nmods + 1) * sizeof (struct str_mlist);
60860f1702c5SYu Xiangning 		kstrlist.sl_nmods = nmods;
60870f1702c5SYu Xiangning 		kstrlist.sl_modlist = kmem_zalloc(kstrlistsize, KM_SLEEP);
60880f1702c5SYu Xiangning 
60890f1702c5SYu Xiangning 		error = strioctl(vp, cmd, (intptr_t)&kstrlist, mode, K_TO_K,
60900f1702c5SYu Xiangning 		    cr, rvalp);
60910f1702c5SYu Xiangning 		if (error != 0)
60920f1702c5SYu Xiangning 			goto done;
60930f1702c5SYu Xiangning 
60940f1702c5SYu Xiangning 		/*
60950f1702c5SYu Xiangning 		 * Considering the module list as a 0-based array of sl_nmods
60960f1702c5SYu Xiangning 		 * modules, sockmod should conceptually exist at slot
60970f1702c5SYu Xiangning 		 * sti_pushcnt.  Insert sockmod at this location by sliding all
60980f1702c5SYu Xiangning 		 * of the module names after so_pushcnt over by one.  We know
60990f1702c5SYu Xiangning 		 * that there will be room to do this since we allocated
61000f1702c5SYu Xiangning 		 * sl_modlist with an additional slot.
61010f1702c5SYu Xiangning 		 */
61020f1702c5SYu Xiangning 		for (i = kstrlist.sl_nmods; i > sti->sti_pushcnt; i--)
61030f1702c5SYu Xiangning 			kstrlist.sl_modlist[i] = kstrlist.sl_modlist[i - 1];
61040f1702c5SYu Xiangning 
61050f1702c5SYu Xiangning 		(void) strcpy(kstrlist.sl_modlist[i].l_name, sockmod_name);
61060f1702c5SYu Xiangning 		kstrlist.sl_nmods++;
61070f1702c5SYu Xiangning 
61080f1702c5SYu Xiangning 		/*
61090f1702c5SYu Xiangning 		 * Copy all of the entries out to ustrlist.
61100f1702c5SYu Xiangning 		 */
61110f1702c5SYu Xiangning 		kmlistp = kstrlist.sl_modlist;
61120f1702c5SYu Xiangning 		umlistp = STRUCT_FGETP(ustrlist, sl_modlist);
61130f1702c5SYu Xiangning 		for (i = 0; i < nmods && i < kstrlist.sl_nmods; i++) {
61140f1702c5SYu Xiangning 			error = so_copyout(kmlistp++, umlistp++,
61150f1702c5SYu Xiangning 			    sizeof (struct str_mlist), mode & FKIOCTL);
61160f1702c5SYu Xiangning 			if (error != 0)
61170f1702c5SYu Xiangning 				goto done;
61180f1702c5SYu Xiangning 		}
61190f1702c5SYu Xiangning 
61200f1702c5SYu Xiangning 		error = so_copyout(&i, (void *)arg, sizeof (int32_t),
61210f1702c5SYu Xiangning 		    mode & FKIOCTL);
61220f1702c5SYu Xiangning 		if (error == 0)
61230f1702c5SYu Xiangning 			*rvalp = 0;
61240f1702c5SYu Xiangning 	done:
61250f1702c5SYu Xiangning 		kmem_free(kstrlist.sl_modlist, kstrlistsize);
61260f1702c5SYu Xiangning 		return (error);
61270f1702c5SYu Xiangning 	}
61280f1702c5SYu Xiangning 	case I_LOOK:
61290f1702c5SYu Xiangning 		if (sti->sti_pushcnt == 0) {
61300f1702c5SYu Xiangning 			return (so_copyout(sockmod_name, (void *)arg,
61310f1702c5SYu Xiangning 			    sizeof (sockmod_name), mode & FKIOCTL));
61320f1702c5SYu Xiangning 		}
61330f1702c5SYu Xiangning 		return (strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp));
61340f1702c5SYu Xiangning 
61350f1702c5SYu Xiangning 	case I_FIND:
61360f1702c5SYu Xiangning 		error = strioctl(vp, cmd, arg, mode, U_TO_K, cr, rvalp);
61370f1702c5SYu Xiangning 		if (error && error != EINVAL)
61380f1702c5SYu Xiangning 			return (error);
61390f1702c5SYu Xiangning 
61400f1702c5SYu Xiangning 		/* if not found and string was sockmod return 1 */
61410f1702c5SYu Xiangning 		if (*rvalp == 0 || error == EINVAL) {
61420f1702c5SYu Xiangning 			error = ((mode & FKIOCTL) ? copystr : copyinstr)(
61430f1702c5SYu Xiangning 			    (void *)arg, mname, sizeof (mname), NULL);
61440f1702c5SYu Xiangning 			if (error == ENAMETOOLONG)
61450f1702c5SYu Xiangning 				error = EINVAL;
61460f1702c5SYu Xiangning 
61470f1702c5SYu Xiangning 			if (error == 0 && strcmp(mname, sockmod_name) == 0)
61480f1702c5SYu Xiangning 				*rvalp = 1;
61490f1702c5SYu Xiangning 		}
61500f1702c5SYu Xiangning 		return (error);
61510f1702c5SYu Xiangning 
61520f1702c5SYu Xiangning 	default:
61530f1702c5SYu Xiangning 		panic("socktpi_plumbioctl: unknown ioctl %d", cmd);
61540f1702c5SYu Xiangning 		break;
61550f1702c5SYu Xiangning 	}
61560f1702c5SYu Xiangning 
61570f1702c5SYu Xiangning 	return (0);
61580f1702c5SYu Xiangning }
61590f1702c5SYu Xiangning 
61600f1702c5SYu Xiangning /*
61610f1702c5SYu Xiangning  * Wrapper around the streams poll routine that implements socket poll
61620f1702c5SYu Xiangning  * semantics.
61630f1702c5SYu Xiangning  * The sockfs never calls pollwakeup itself - the stream head take care
61640f1702c5SYu Xiangning  * of all pollwakeups. Since sockfs never holds so_lock when calling the
61650f1702c5SYu Xiangning  * stream head there can never be a deadlock due to holding so_lock across
61660f1702c5SYu Xiangning  * pollwakeup and acquiring so_lock in this routine.
61670f1702c5SYu Xiangning  *
61680f1702c5SYu Xiangning  * However, since the performance of VOP_POLL is critical we avoid
61690f1702c5SYu Xiangning  * acquiring so_lock here. This is based on two assumptions:
61700f1702c5SYu Xiangning  *  - The poll implementation holds locks to serialize the VOP_POLL call
61710f1702c5SYu Xiangning  *    and a pollwakeup for the same pollhead. This ensures that should
61720f1702c5SYu Xiangning  *    e.g. so_state change during a socktpi_poll call the pollwakeup
61730f1702c5SYu Xiangning  *    (which strsock_* and strrput conspire to issue) is issued after
61740f1702c5SYu Xiangning  *    the state change. Thus the pollwakeup will block until VOP_POLL has
61750f1702c5SYu Xiangning  *    returned and then wake up poll and have it call VOP_POLL again.
61760f1702c5SYu Xiangning  *  - The reading of so_state without holding so_lock does not result in
61770f1702c5SYu Xiangning  *    stale data that is older than the latest state change that has dropped
61780f1702c5SYu Xiangning  *    so_lock. This is ensured by the mutex_exit issuing the appropriate
61790f1702c5SYu Xiangning  *    memory barrier to force the data into the coherency domain.
61800f1702c5SYu Xiangning  */
61810f1702c5SYu Xiangning static int
61820f1702c5SYu Xiangning sotpi_poll(
61830f1702c5SYu Xiangning 	struct sonode	*so,
61840f1702c5SYu Xiangning 	short		events,
61850f1702c5SYu Xiangning 	int		anyyet,
61860f1702c5SYu Xiangning 	short		*reventsp,
61870f1702c5SYu Xiangning 	struct pollhead **phpp)
61880f1702c5SYu Xiangning {
61890f1702c5SYu Xiangning 	short origevents = events;
61900f1702c5SYu Xiangning 	struct vnode *vp = SOTOV(so);
61910f1702c5SYu Xiangning 	int error;
61920f1702c5SYu Xiangning 	int so_state = so->so_state;	/* snapshot */
61930f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
61940f1702c5SYu Xiangning 
61950f1702c5SYu Xiangning 	dprintso(so, 0, ("socktpi_poll(%p): state %s err %d\n",
61960f1702c5SYu Xiangning 	    (void *)vp, pr_state(so_state, so->so_mode), so->so_error));
61970f1702c5SYu Xiangning 
61980f1702c5SYu Xiangning 	ASSERT(vp->v_type == VSOCK);
61990f1702c5SYu Xiangning 	ASSERT(vp->v_stream != NULL);
62000f1702c5SYu Xiangning 
62010f1702c5SYu Xiangning 	if (so->so_version == SOV_STREAM) {
62020f1702c5SYu Xiangning 		/* The imaginary "sockmod" has been popped - act as a stream */
62030f1702c5SYu Xiangning 		return (strpoll(vp->v_stream, events, anyyet,
62040f1702c5SYu Xiangning 		    reventsp, phpp));
62050f1702c5SYu Xiangning 	}
62060f1702c5SYu Xiangning 
62070f1702c5SYu Xiangning 	if (!(so_state & SS_ISCONNECTED) &&
62080f1702c5SYu Xiangning 	    (so->so_mode & SM_CONNREQUIRED)) {
62090f1702c5SYu Xiangning 		/* Not connected yet - turn off write side events */
62100f1702c5SYu Xiangning 		events &= ~(POLLOUT|POLLWRBAND);
62110f1702c5SYu Xiangning 	}
62120f1702c5SYu Xiangning 	/*
62130f1702c5SYu Xiangning 	 * Check for errors without calling strpoll if the caller wants them.
62140f1702c5SYu Xiangning 	 * In sockets the errors are represented as input/output events
62150f1702c5SYu Xiangning 	 * and there is no need to ask the stream head for this information.
62160f1702c5SYu Xiangning 	 */
62170f1702c5SYu Xiangning 	if (so->so_error != 0 &&
62180f1702c5SYu Xiangning 	    ((POLLIN|POLLRDNORM|POLLOUT) & origevents)  != 0) {
62190f1702c5SYu Xiangning 		*reventsp = (POLLIN|POLLRDNORM|POLLOUT) & origevents;
62200f1702c5SYu Xiangning 		return (0);
62210f1702c5SYu Xiangning 	}
62220f1702c5SYu Xiangning 	/*
62230f1702c5SYu Xiangning 	 * Ignore M_PROTO only messages such as the T_EXDATA_IND messages.
62240f1702c5SYu Xiangning 	 * These message with only an M_PROTO/M_PCPROTO part and no M_DATA
62250f1702c5SYu Xiangning 	 * will not trigger a POLLIN event with POLLRDDATA set.
62260f1702c5SYu Xiangning 	 * The handling of urgent data (causing POLLRDBAND) is done by
62270f1702c5SYu Xiangning 	 * inspecting SS_OOBPEND below.
62280f1702c5SYu Xiangning 	 */
62290f1702c5SYu Xiangning 	events |= POLLRDDATA;
62300f1702c5SYu Xiangning 
62310f1702c5SYu Xiangning 	/*
62320f1702c5SYu Xiangning 	 * After shutdown(output) a stream head write error is set.
62330f1702c5SYu Xiangning 	 * However, we should not return output events.
62340f1702c5SYu Xiangning 	 */
62350f1702c5SYu Xiangning 	events |= POLLNOERR;
62360f1702c5SYu Xiangning 	error = strpoll(vp->v_stream, events, anyyet,
62370f1702c5SYu Xiangning 	    reventsp, phpp);
62380f1702c5SYu Xiangning 	if (error)
62390f1702c5SYu Xiangning 		return (error);
62400f1702c5SYu Xiangning 
62410f1702c5SYu Xiangning 	ASSERT(!(*reventsp & POLLERR));
62420f1702c5SYu Xiangning 
62430f1702c5SYu Xiangning 	/*
62440f1702c5SYu Xiangning 	 * Notes on T_CONN_IND handling for sockets.
62450f1702c5SYu Xiangning 	 *
62460f1702c5SYu Xiangning 	 * If strpoll() returned without events, SR_POLLIN is guaranteed
62470f1702c5SYu Xiangning 	 * to be set, ensuring any subsequent strrput() runs pollwakeup().
62480f1702c5SYu Xiangning 	 *
62490f1702c5SYu Xiangning 	 * Since the so_lock is not held, soqueueconnind() may have run
62500f1702c5SYu Xiangning 	 * and a T_CONN_IND may be waiting. We now check for any queued
62510f1702c5SYu Xiangning 	 * T_CONN_IND msgs on sti_conn_ind_head and set appropriate events
62520f1702c5SYu Xiangning 	 * to ensure poll returns.
62530f1702c5SYu Xiangning 	 *
62540f1702c5SYu Xiangning 	 * However:
62550f1702c5SYu Xiangning 	 * If the T_CONN_IND hasn't arrived by the time strpoll() returns,
62560f1702c5SYu Xiangning 	 * when strrput() does run for an arriving M_PROTO with T_CONN_IND
62570f1702c5SYu Xiangning 	 * the following actions will occur; taken together they ensure the
62580f1702c5SYu Xiangning 	 * syscall will return.
62590f1702c5SYu Xiangning 	 *
62600f1702c5SYu Xiangning 	 * 1. If a socket, soqueueconnind() will queue the T_CONN_IND but if
62610f1702c5SYu Xiangning 	 *    the accept() was run on a non-blocking socket sowaitconnind()
62620f1702c5SYu Xiangning 	 *    may have already returned EWOULDBLOCK, so not be waiting to
62630f1702c5SYu Xiangning 	 *    process the message. Additionally socktpi_poll() has probably
62640f1702c5SYu Xiangning 	 *    proceeded past the sti_conn_ind_head check below.
62650f1702c5SYu Xiangning 	 * 2. strrput() runs pollwakeup()->pollnotify()->cv_signal() to wake
62660f1702c5SYu Xiangning 	 *    this thread,  however that could occur before poll_common()
62670f1702c5SYu Xiangning 	 *    has entered cv_wait.
62680f1702c5SYu Xiangning 	 * 3. pollnotify() sets T_POLLWAKE, while holding the pc_lock.
62690f1702c5SYu Xiangning 	 *
62700f1702c5SYu Xiangning 	 * Before proceeding to cv_wait() in poll_common() for an event,
62710f1702c5SYu Xiangning 	 * poll_common() atomically checks for T_POLLWAKE under the pc_lock,
62720f1702c5SYu Xiangning 	 * and if set, re-calls strpoll() to ensure the late arriving
62730f1702c5SYu Xiangning 	 * T_CONN_IND is recognized, and pollsys() returns.
62740f1702c5SYu Xiangning 	 */
62750f1702c5SYu Xiangning 
62760f1702c5SYu Xiangning 	if (sti->sti_conn_ind_head != NULL)
62770f1702c5SYu Xiangning 		*reventsp |= (POLLIN|POLLRDNORM) & events;
62780f1702c5SYu Xiangning 
6279075fab9aSBryan Cantrill 	if (so->so_state & SS_CANTRCVMORE) {
6280075fab9aSBryan Cantrill 		*reventsp |= POLLRDHUP & events;
6281075fab9aSBryan Cantrill 
6282075fab9aSBryan Cantrill 		if (so->so_state & SS_CANTSENDMORE)
6283075fab9aSBryan Cantrill 			*reventsp |= POLLHUP;
6284075fab9aSBryan Cantrill 	}
6285075fab9aSBryan Cantrill 
62860f1702c5SYu Xiangning 	if (so->so_state & SS_OOBPEND)
62870f1702c5SYu Xiangning 		*reventsp |= POLLRDBAND & events;
62880f1702c5SYu Xiangning 
62890f1702c5SYu Xiangning 	if (sti->sti_nl7c_rcv_mp != NULL) {
62900f1702c5SYu Xiangning 		*reventsp |= (POLLIN|POLLRDNORM) & events;
62910f1702c5SYu Xiangning 	}
62920f1702c5SYu Xiangning 	if ((sti->sti_nl7c_flags & NL7C_ENABLED) &&
62930f1702c5SYu Xiangning 	    ((POLLIN|POLLRDNORM) & *reventsp)) {
62940f1702c5SYu Xiangning 		sti->sti_nl7c_flags |= NL7C_POLLIN;
62950f1702c5SYu Xiangning 	}
62960f1702c5SYu Xiangning 
62970f1702c5SYu Xiangning 	return (0);
62980f1702c5SYu Xiangning }
62990f1702c5SYu Xiangning 
63000f1702c5SYu Xiangning /*ARGSUSED*/
63010f1702c5SYu Xiangning static int
63020f1702c5SYu Xiangning socktpi_constructor(void *buf, void *cdrarg, int kmflags)
63030f1702c5SYu Xiangning {
63040f1702c5SYu Xiangning 	sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
63050f1702c5SYu Xiangning 	int error = 0;
63060f1702c5SYu Xiangning 
63070f1702c5SYu Xiangning 	error = sonode_constructor(buf, cdrarg, kmflags);
63080f1702c5SYu Xiangning 	if (error != 0)
63090f1702c5SYu Xiangning 		return (error);
63100f1702c5SYu Xiangning 
63110f1702c5SYu Xiangning 	error = i_sotpi_info_constructor(&st->st_info);
63120f1702c5SYu Xiangning 	if (error != 0)
63130f1702c5SYu Xiangning 		sonode_destructor(buf, cdrarg);
63140f1702c5SYu Xiangning 
63150f1702c5SYu Xiangning 	st->st_sonode.so_priv = &st->st_info;
63160f1702c5SYu Xiangning 
63170f1702c5SYu Xiangning 	return (error);
63180f1702c5SYu Xiangning }
63190f1702c5SYu Xiangning 
63200f1702c5SYu Xiangning /*ARGSUSED1*/
63210f1702c5SYu Xiangning static void
63220f1702c5SYu Xiangning socktpi_destructor(void *buf, void *cdrarg)
63230f1702c5SYu Xiangning {
63240f1702c5SYu Xiangning 	sotpi_sonode_t *st = (sotpi_sonode_t *)buf;
63250f1702c5SYu Xiangning 
63260f1702c5SYu Xiangning 	ASSERT(st->st_sonode.so_priv == &st->st_info);
63270f1702c5SYu Xiangning 	st->st_sonode.so_priv = NULL;
63280f1702c5SYu Xiangning 
63290f1702c5SYu Xiangning 	i_sotpi_info_destructor(&st->st_info);
63300f1702c5SYu Xiangning 	sonode_destructor(buf, cdrarg);
63310f1702c5SYu Xiangning }
63320f1702c5SYu Xiangning 
63330f1702c5SYu Xiangning static int
63340f1702c5SYu Xiangning socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags)
63350f1702c5SYu Xiangning {
63360f1702c5SYu Xiangning 	int retval;
63370f1702c5SYu Xiangning 
63380f1702c5SYu Xiangning 	if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) {
63390f1702c5SYu Xiangning 		struct sonode *so = (struct sonode *)buf;
63400f1702c5SYu Xiangning 		sotpi_info_t *sti = SOTOTPI(so);
63410f1702c5SYu Xiangning 
63420f1702c5SYu Xiangning 		mutex_enter(&socklist.sl_lock);
63430f1702c5SYu Xiangning 
63440f1702c5SYu Xiangning 		sti->sti_next_so = socklist.sl_list;
63450f1702c5SYu Xiangning 		sti->sti_prev_so = NULL;
63460f1702c5SYu Xiangning 		if (sti->sti_next_so != NULL)
63470f1702c5SYu Xiangning 			SOTOTPI(sti->sti_next_so)->sti_prev_so = so;
63480f1702c5SYu Xiangning 		socklist.sl_list = so;
63490f1702c5SYu Xiangning 
63500f1702c5SYu Xiangning 		mutex_exit(&socklist.sl_lock);
63510f1702c5SYu Xiangning 
63520f1702c5SYu Xiangning 	}
63530f1702c5SYu Xiangning 	return (retval);
63540f1702c5SYu Xiangning }
63550f1702c5SYu Xiangning 
63560f1702c5SYu Xiangning static void
63570f1702c5SYu Xiangning socktpi_unix_destructor(void *buf, void *cdrarg)
63580f1702c5SYu Xiangning {
63590f1702c5SYu Xiangning 	struct sonode	*so = (struct sonode *)buf;
63600f1702c5SYu Xiangning 	sotpi_info_t	*sti = SOTOTPI(so);
63610f1702c5SYu Xiangning 
63620f1702c5SYu Xiangning 	mutex_enter(&socklist.sl_lock);
63630f1702c5SYu Xiangning 
63640f1702c5SYu Xiangning 	if (sti->sti_next_so != NULL)
63650f1702c5SYu Xiangning 		SOTOTPI(sti->sti_next_so)->sti_prev_so = sti->sti_prev_so;
63660f1702c5SYu Xiangning 	if (sti->sti_prev_so != NULL)
63670f1702c5SYu Xiangning 		SOTOTPI(sti->sti_prev_so)->sti_next_so = sti->sti_next_so;
63680f1702c5SYu Xiangning 	else
63690f1702c5SYu Xiangning 		socklist.sl_list = sti->sti_next_so;
63700f1702c5SYu Xiangning 
63710f1702c5SYu Xiangning 	mutex_exit(&socklist.sl_lock);
63720f1702c5SYu Xiangning 
63730f1702c5SYu Xiangning 	socktpi_destructor(buf, cdrarg);
63740f1702c5SYu Xiangning }
63750f1702c5SYu Xiangning 
63760f1702c5SYu Xiangning int
63770f1702c5SYu Xiangning socktpi_init(void)
63780f1702c5SYu Xiangning {
63790f1702c5SYu Xiangning 	/*
63800f1702c5SYu Xiangning 	 * Create sonode caches.  We create a special one for AF_UNIX so
63810f1702c5SYu Xiangning 	 * that we can track them for netstat(1m).
63820f1702c5SYu Xiangning 	 */
63830f1702c5SYu Xiangning 	socktpi_cache = kmem_cache_create("socktpi_cache",
63840f1702c5SYu Xiangning 	    sizeof (struct sotpi_sonode), 0, socktpi_constructor,
63850f1702c5SYu Xiangning 	    socktpi_destructor, NULL, NULL, NULL, 0);
63860f1702c5SYu Xiangning 
63870f1702c5SYu Xiangning 	socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache",
63880f1702c5SYu Xiangning 	    sizeof (struct sotpi_sonode), 0, socktpi_unix_constructor,
63890f1702c5SYu Xiangning 	    socktpi_unix_destructor, NULL, NULL, NULL, 0);
63900f1702c5SYu Xiangning 
63910f1702c5SYu Xiangning 	return (0);
63920f1702c5SYu Xiangning }
63930f1702c5SYu Xiangning 
63940f1702c5SYu Xiangning /*
63950f1702c5SYu Xiangning  * Given a non-TPI sonode, allocate and prep it to be ready for TPI.
63960f1702c5SYu Xiangning  *
63970f1702c5SYu Xiangning  * Caller must still update state and mode using sotpi_update_state().
63980f1702c5SYu Xiangning  */
639941174437SAnders Persson int
64000f1702c5SYu Xiangning sotpi_convert_sonode(struct sonode *so, struct sockparams *newsp,
640141174437SAnders Persson     boolean_t *direct, queue_t **qp, struct cred *cr)
64020f1702c5SYu Xiangning {
64030f1702c5SYu Xiangning 	sotpi_info_t *sti;
64040f1702c5SYu Xiangning 	struct sockparams *origsp = so->so_sockparams;
64050f1702c5SYu Xiangning 	sock_lower_handle_t handle = so->so_proto_handle;
64060f1702c5SYu Xiangning 	struct stdata *stp;
64070f1702c5SYu Xiangning 	struct vnode *vp;
64080f1702c5SYu Xiangning 	queue_t *q;
640941174437SAnders Persson 	int error = 0;
64100f1702c5SYu Xiangning 
641141174437SAnders Persson 	ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
641241174437SAnders Persson 	    SS_FALLBACK_PENDING);
641341174437SAnders Persson 	ASSERT(SOCK_IS_NONSTR(so));
641441174437SAnders Persson 
641541174437SAnders Persson 	*qp = NULL;
64160f1702c5SYu Xiangning 	*direct = B_FALSE;
64170f1702c5SYu Xiangning 	so->so_sockparams = newsp;
64180f1702c5SYu Xiangning 	/*
64190f1702c5SYu Xiangning 	 * Allocate and initalize fields required by TPI.
64200f1702c5SYu Xiangning 	 */
64210f1702c5SYu Xiangning 	(void) sotpi_info_create(so, KM_SLEEP);
64220f1702c5SYu Xiangning 	sotpi_info_init(so);
64230f1702c5SYu Xiangning 
642441174437SAnders Persson 	if ((error = sotpi_init(so, NULL, cr, SO_FALLBACK)) != 0) {
64250f1702c5SYu Xiangning 		sotpi_info_fini(so);
64260f1702c5SYu Xiangning 		sotpi_info_destroy(so);
642741174437SAnders Persson 		return (error);
64280f1702c5SYu Xiangning 	}
64290f1702c5SYu Xiangning 	ASSERT(handle == so->so_proto_handle);
64300f1702c5SYu Xiangning 	sti = SOTOTPI(so);
64310f1702c5SYu Xiangning 	if (sti->sti_direct != 0)
64320f1702c5SYu Xiangning 		*direct = B_TRUE;
64330f1702c5SYu Xiangning 
64340f1702c5SYu Xiangning 	/*
64350f1702c5SYu Xiangning 	 * Keep the original sp around so we can properly dispose of the
64360f1702c5SYu Xiangning 	 * sonode when the socket is being closed.
64370f1702c5SYu Xiangning 	 */
64380f1702c5SYu Xiangning 	sti->sti_orig_sp = origsp;
64390f1702c5SYu Xiangning 
64400f1702c5SYu Xiangning 	so_basic_strinit(so);	/* skips the T_CAPABILITY_REQ */
64410f1702c5SYu Xiangning 	so_alloc_addr(so, so->so_max_addr_len);
64420f1702c5SYu Xiangning 
64430f1702c5SYu Xiangning 	/*
64440f1702c5SYu Xiangning 	 * If the application has done a SIOCSPGRP, make sure the
64450f1702c5SYu Xiangning 	 * STREAM head is aware. This needs to take place before
64460f1702c5SYu Xiangning 	 * the protocol start sending up messages. Otherwise we
64470f1702c5SYu Xiangning 	 * might miss to generate SIGPOLL.
64480f1702c5SYu Xiangning 	 *
64490f1702c5SYu Xiangning 	 * It is possible that the application will receive duplicate
64500f1702c5SYu Xiangning 	 * signals if some were already generated for either data or
64510f1702c5SYu Xiangning 	 * connection indications.
64520f1702c5SYu Xiangning 	 */
64530f1702c5SYu Xiangning 	if (so->so_pgrp != 0) {
64540f1702c5SYu Xiangning 		if (so_set_events(so, so->so_vnode, cr) != 0)
64550f1702c5SYu Xiangning 			so->so_pgrp = 0;
64560f1702c5SYu Xiangning 	}
64570f1702c5SYu Xiangning 
64580f1702c5SYu Xiangning 	/*
64590f1702c5SYu Xiangning 	 * Determine which queue to use.
64600f1702c5SYu Xiangning 	 */
64610f1702c5SYu Xiangning 	vp = SOTOV(so);
64620f1702c5SYu Xiangning 	stp = vp->v_stream;
64630f1702c5SYu Xiangning 	ASSERT(stp != NULL);
64640f1702c5SYu Xiangning 	q = stp->sd_wrq->q_next;
64650f1702c5SYu Xiangning 
64660f1702c5SYu Xiangning 	/*
64670f1702c5SYu Xiangning 	 * Skip any modules that may have been auto pushed when the device
64680f1702c5SYu Xiangning 	 * was opened
64690f1702c5SYu Xiangning 	 */
64700f1702c5SYu Xiangning 	while (q->q_next != NULL)
64710f1702c5SYu Xiangning 		q = q->q_next;
647241174437SAnders Persson 	*qp = _RD(q);
64730f1702c5SYu Xiangning 
647441174437SAnders Persson 	/* This is now a STREAMS sockets */
647541174437SAnders Persson 	so->so_not_str = B_FALSE;
647641174437SAnders Persson 
647741174437SAnders Persson 	return (error);
647841174437SAnders Persson }
647941174437SAnders Persson 
648041174437SAnders Persson /*
648141174437SAnders Persson  * Revert a TPI sonode. It is only allowed to revert the sonode during
648241174437SAnders Persson  * the fallback process.
648341174437SAnders Persson  */
648441174437SAnders Persson void
648541174437SAnders Persson sotpi_revert_sonode(struct sonode *so, struct cred *cr)
648641174437SAnders Persson {
648741174437SAnders Persson 	vnode_t *vp = SOTOV(so);
648841174437SAnders Persson 
648941174437SAnders Persson 	ASSERT((so->so_state & (SS_FALLBACK_PENDING|SS_FALLBACK_COMP)) ==
649041174437SAnders Persson 	    SS_FALLBACK_PENDING);
649141174437SAnders Persson 	ASSERT(!SOCK_IS_NONSTR(so));
649241174437SAnders Persson 	ASSERT(vp->v_stream != NULL);
649341174437SAnders Persson 
649441174437SAnders Persson 	strclean(vp);
649541174437SAnders Persson 	(void) strclose(vp, FREAD|FWRITE|SO_FALLBACK, cr);
649641174437SAnders Persson 
649741174437SAnders Persson 	/*
649841174437SAnders Persson 	 * Restore the original sockparams. The caller is responsible for
649941174437SAnders Persson 	 * dropping the ref to the new sp.
650041174437SAnders Persson 	 */
650141174437SAnders Persson 	so->so_sockparams = SOTOTPI(so)->sti_orig_sp;
650241174437SAnders Persson 
650341174437SAnders Persson 	sotpi_info_fini(so);
650441174437SAnders Persson 	sotpi_info_destroy(so);
650541174437SAnders Persson 
650641174437SAnders Persson 	/* This is no longer a STREAMS sockets */
650741174437SAnders Persson 	so->so_not_str = B_TRUE;
65080f1702c5SYu Xiangning }
65090f1702c5SYu Xiangning 
65100f1702c5SYu Xiangning void
65110f1702c5SYu Xiangning sotpi_update_state(struct sonode *so, struct T_capability_ack *tcap,
65120f1702c5SYu Xiangning     struct sockaddr *laddr, socklen_t laddrlen, struct sockaddr *faddr,
65130f1702c5SYu Xiangning     socklen_t faddrlen, short opts)
65140f1702c5SYu Xiangning {
65150f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
65160f1702c5SYu Xiangning 
65170f1702c5SYu Xiangning 	so_proc_tcapability_ack(so, tcap);
65180f1702c5SYu Xiangning 
65190f1702c5SYu Xiangning 	so->so_options |= opts;
65200f1702c5SYu Xiangning 
65210f1702c5SYu Xiangning 	/*
65220f1702c5SYu Xiangning 	 * Determine whether the foreign and local address are valid
65230f1702c5SYu Xiangning 	 */
65240f1702c5SYu Xiangning 	if (laddrlen != 0) {
65250f1702c5SYu Xiangning 		ASSERT(laddrlen <= sti->sti_laddr_maxlen);
65260f1702c5SYu Xiangning 		sti->sti_laddr_len = laddrlen;
65270f1702c5SYu Xiangning 		bcopy(laddr, sti->sti_laddr_sa, laddrlen);
65280f1702c5SYu Xiangning 		sti->sti_laddr_valid = (so->so_state & SS_ISBOUND);
65290f1702c5SYu Xiangning 	}
65300f1702c5SYu Xiangning 
65310f1702c5SYu Xiangning 	if (faddrlen != 0) {
65320f1702c5SYu Xiangning 		ASSERT(faddrlen <= sti->sti_faddr_maxlen);
65330f1702c5SYu Xiangning 		sti->sti_faddr_len = faddrlen;
65340f1702c5SYu Xiangning 		bcopy(faddr, sti->sti_faddr_sa, faddrlen);
65350f1702c5SYu Xiangning 		sti->sti_faddr_valid = (so->so_state & SS_ISCONNECTED);
65360f1702c5SYu Xiangning 	}
65370f1702c5SYu Xiangning 
65380f1702c5SYu Xiangning }
65390f1702c5SYu Xiangning 
65400f1702c5SYu Xiangning /*
65410f1702c5SYu Xiangning  * Allocate enough space to cache the local and foreign addresses.
65420f1702c5SYu Xiangning  */
65430f1702c5SYu Xiangning void
65440f1702c5SYu Xiangning so_alloc_addr(struct sonode *so, t_uscalar_t maxlen)
65450f1702c5SYu Xiangning {
65460f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
65470f1702c5SYu Xiangning 
65480f1702c5SYu Xiangning 	ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
65490f1702c5SYu Xiangning 	ASSERT(sti->sti_laddr_len == 0 && sti->sti_faddr_len == 0);
65500f1702c5SYu Xiangning 	sti->sti_laddr_maxlen = sti->sti_faddr_maxlen =
65510f1702c5SYu Xiangning 	    P2ROUNDUP(maxlen, KMEM_ALIGN);
65520f1702c5SYu Xiangning 	so->so_max_addr_len = sti->sti_laddr_maxlen;
65530f1702c5SYu Xiangning 	sti->sti_laddr_sa = kmem_alloc(sti->sti_laddr_maxlen * 2, KM_SLEEP);
65540f1702c5SYu Xiangning 	sti->sti_faddr_sa = (struct sockaddr *)((caddr_t)sti->sti_laddr_sa
65550f1702c5SYu Xiangning 	    + sti->sti_laddr_maxlen);
65560f1702c5SYu Xiangning 
65570f1702c5SYu Xiangning 	if (so->so_family == AF_UNIX) {
65580f1702c5SYu Xiangning 		/*
65590f1702c5SYu Xiangning 		 * Initialize AF_UNIX related fields.
65600f1702c5SYu Xiangning 		 */
65610f1702c5SYu Xiangning 		bzero(&sti->sti_ux_laddr, sizeof (sti->sti_ux_laddr));
65620f1702c5SYu Xiangning 		bzero(&sti->sti_ux_faddr, sizeof (sti->sti_ux_faddr));
65630f1702c5SYu Xiangning 	}
65640f1702c5SYu Xiangning }
65650f1702c5SYu Xiangning 
65660f1702c5SYu Xiangning 
65670f1702c5SYu Xiangning sotpi_info_t *
65680f1702c5SYu Xiangning sotpi_sototpi(struct sonode *so)
65690f1702c5SYu Xiangning {
65700f1702c5SYu Xiangning 	sotpi_info_t *sti;
65710f1702c5SYu Xiangning 
657241174437SAnders Persson 	ASSERT(so != NULL);
65730f1702c5SYu Xiangning 
65740f1702c5SYu Xiangning 	sti = (sotpi_info_t *)so->so_priv;
65750f1702c5SYu Xiangning 
65760f1702c5SYu Xiangning 	ASSERT(sti != NULL);
65770f1702c5SYu Xiangning 	ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
65780f1702c5SYu Xiangning 
65790f1702c5SYu Xiangning 	return (sti);
65800f1702c5SYu Xiangning }
65810f1702c5SYu Xiangning 
65820f1702c5SYu Xiangning static int
65830f1702c5SYu Xiangning i_sotpi_info_constructor(sotpi_info_t *sti)
65840f1702c5SYu Xiangning {
65850f1702c5SYu Xiangning 	sti->sti_magic		= SOTPI_INFO_MAGIC;
65860f1702c5SYu Xiangning 	sti->sti_ack_mp		= NULL;
65870f1702c5SYu Xiangning 	sti->sti_discon_ind_mp	= NULL;
65880f1702c5SYu Xiangning 	sti->sti_ux_bound_vp	= NULL;
65890f1702c5SYu Xiangning 	sti->sti_unbind_mp	= NULL;
65900f1702c5SYu Xiangning 
65910f1702c5SYu Xiangning 	sti->sti_conn_ind_head	= NULL;
65920f1702c5SYu Xiangning 	sti->sti_conn_ind_tail	= NULL;
65930f1702c5SYu Xiangning 
65940f1702c5SYu Xiangning 	sti->sti_laddr_sa	= NULL;
65950f1702c5SYu Xiangning 	sti->sti_faddr_sa	= NULL;
65960f1702c5SYu Xiangning 
65970f1702c5SYu Xiangning 	sti->sti_nl7c_flags	= 0;
65980f1702c5SYu Xiangning 	sti->sti_nl7c_uri	= NULL;
65990f1702c5SYu Xiangning 	sti->sti_nl7c_rcv_mp	= NULL;
66000f1702c5SYu Xiangning 
66010f1702c5SYu Xiangning 	mutex_init(&sti->sti_plumb_lock, NULL, MUTEX_DEFAULT, NULL);
66020f1702c5SYu Xiangning 	cv_init(&sti->sti_ack_cv, NULL, CV_DEFAULT, NULL);
66030f1702c5SYu Xiangning 
66040f1702c5SYu Xiangning 	return (0);
66050f1702c5SYu Xiangning }
66060f1702c5SYu Xiangning 
66070f1702c5SYu Xiangning static void
66080f1702c5SYu Xiangning i_sotpi_info_destructor(sotpi_info_t *sti)
66090f1702c5SYu Xiangning {
66100f1702c5SYu Xiangning 	ASSERT(sti->sti_magic == SOTPI_INFO_MAGIC);
66110f1702c5SYu Xiangning 	ASSERT(sti->sti_ack_mp == NULL);
66120f1702c5SYu Xiangning 	ASSERT(sti->sti_discon_ind_mp == NULL);
66130f1702c5SYu Xiangning 	ASSERT(sti->sti_ux_bound_vp == NULL);
66140f1702c5SYu Xiangning 	ASSERT(sti->sti_unbind_mp == NULL);
66150f1702c5SYu Xiangning 
66160f1702c5SYu Xiangning 	ASSERT(sti->sti_conn_ind_head == NULL);
66170f1702c5SYu Xiangning 	ASSERT(sti->sti_conn_ind_tail == NULL);
66180f1702c5SYu Xiangning 
66190f1702c5SYu Xiangning 	ASSERT(sti->sti_laddr_sa == NULL);
66200f1702c5SYu Xiangning 	ASSERT(sti->sti_faddr_sa == NULL);
66210f1702c5SYu Xiangning 
66220f1702c5SYu Xiangning 	ASSERT(sti->sti_nl7c_flags == 0);
66230f1702c5SYu Xiangning 	ASSERT(sti->sti_nl7c_uri == NULL);
66240f1702c5SYu Xiangning 	ASSERT(sti->sti_nl7c_rcv_mp == NULL);
66250f1702c5SYu Xiangning 
66260f1702c5SYu Xiangning 	mutex_destroy(&sti->sti_plumb_lock);
66270f1702c5SYu Xiangning 	cv_destroy(&sti->sti_ack_cv);
66280f1702c5SYu Xiangning }
66290f1702c5SYu Xiangning 
66300f1702c5SYu Xiangning /*
66310f1702c5SYu Xiangning  * Creates and attaches TPI information to the given sonode
66320f1702c5SYu Xiangning  */
66330f1702c5SYu Xiangning static boolean_t
66340f1702c5SYu Xiangning sotpi_info_create(struct sonode *so, int kmflags)
66350f1702c5SYu Xiangning {
66360f1702c5SYu Xiangning 	sotpi_info_t *sti;
66370f1702c5SYu Xiangning 
66380f1702c5SYu Xiangning 	ASSERT(so->so_priv == NULL);
66390f1702c5SYu Xiangning 
66400f1702c5SYu Xiangning 	if ((sti = kmem_zalloc(sizeof (*sti), kmflags)) == NULL)
66410f1702c5SYu Xiangning 		return (B_FALSE);
66420f1702c5SYu Xiangning 
66430f1702c5SYu Xiangning 	if (i_sotpi_info_constructor(sti) != 0) {
66440f1702c5SYu Xiangning 		kmem_free(sti, sizeof (*sti));
66450f1702c5SYu Xiangning 		return (B_FALSE);
66460f1702c5SYu Xiangning 	}
66470f1702c5SYu Xiangning 
66480f1702c5SYu Xiangning 	so->so_priv = (void *)sti;
66490f1702c5SYu Xiangning 	return (B_TRUE);
66500f1702c5SYu Xiangning }
66510f1702c5SYu Xiangning 
66520f1702c5SYu Xiangning /*
66530f1702c5SYu Xiangning  * Initializes the TPI information.
66540f1702c5SYu Xiangning  */
66550f1702c5SYu Xiangning static void
66560f1702c5SYu Xiangning sotpi_info_init(struct sonode *so)
66570f1702c5SYu Xiangning {
66580f1702c5SYu Xiangning 	struct vnode *vp = SOTOV(so);
66590f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
66600f1702c5SYu Xiangning 	time_t now;
66610f1702c5SYu Xiangning 
66620f1702c5SYu Xiangning 	sti->sti_dev 	= so->so_sockparams->sp_sdev_info.sd_vnode->v_rdev;
66630f1702c5SYu Xiangning 	vp->v_rdev	= sti->sti_dev;
66640f1702c5SYu Xiangning 
66650f1702c5SYu Xiangning 	sti->sti_orig_sp = NULL;
66660f1702c5SYu Xiangning 
66670f1702c5SYu Xiangning 	sti->sti_pushcnt = 0;
66680f1702c5SYu Xiangning 
66690f1702c5SYu Xiangning 	now = gethrestime_sec();
66700f1702c5SYu Xiangning 	sti->sti_atime	= now;
66710f1702c5SYu Xiangning 	sti->sti_mtime	= now;
66720f1702c5SYu Xiangning 	sti->sti_ctime	= now;
66730f1702c5SYu Xiangning 
66740f1702c5SYu Xiangning 	sti->sti_eaddr_mp = NULL;
66750f1702c5SYu Xiangning 	sti->sti_delayed_error = 0;
66760f1702c5SYu Xiangning 
66770f1702c5SYu Xiangning 	sti->sti_provinfo = NULL;
66780f1702c5SYu Xiangning 
66790f1702c5SYu Xiangning 	sti->sti_oobcnt = 0;
66800f1702c5SYu Xiangning 	sti->sti_oobsigcnt = 0;
66810f1702c5SYu Xiangning 
66820f1702c5SYu Xiangning 	ASSERT(sti->sti_laddr_sa == NULL && sti->sti_faddr_sa == NULL);
66830f1702c5SYu Xiangning 
66840f1702c5SYu Xiangning 	sti->sti_laddr_sa	= 0;
66850f1702c5SYu Xiangning 	sti->sti_faddr_sa	= 0;
66860f1702c5SYu Xiangning 	sti->sti_laddr_maxlen = sti->sti_faddr_maxlen = 0;
66870f1702c5SYu Xiangning 	sti->sti_laddr_len = sti->sti_faddr_len = 0;
66880f1702c5SYu Xiangning 
66890f1702c5SYu Xiangning 	sti->sti_laddr_valid = 0;
66900f1702c5SYu Xiangning 	sti->sti_faddr_valid = 0;
66910f1702c5SYu Xiangning 	sti->sti_faddr_noxlate = 0;
66920f1702c5SYu Xiangning 
66930f1702c5SYu Xiangning 	sti->sti_direct = 0;
66940f1702c5SYu Xiangning 
66950f1702c5SYu Xiangning 	ASSERT(sti->sti_ack_mp == NULL);
66960f1702c5SYu Xiangning 	ASSERT(sti->sti_ux_bound_vp == NULL);
66970f1702c5SYu Xiangning 	ASSERT(sti->sti_unbind_mp == NULL);
66980f1702c5SYu Xiangning 
66990f1702c5SYu Xiangning 	ASSERT(sti->sti_conn_ind_head == NULL);
67000f1702c5SYu Xiangning 	ASSERT(sti->sti_conn_ind_tail == NULL);
67010f1702c5SYu Xiangning }
67020f1702c5SYu Xiangning 
67030f1702c5SYu Xiangning /*
67040f1702c5SYu Xiangning  * Given a sonode, grab the TPI info and free any data.
67050f1702c5SYu Xiangning  */
67060f1702c5SYu Xiangning static void
67070f1702c5SYu Xiangning sotpi_info_fini(struct sonode *so)
67080f1702c5SYu Xiangning {
67090f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
67100f1702c5SYu Xiangning 	mblk_t *mp;
67110f1702c5SYu Xiangning 
67120f1702c5SYu Xiangning 	ASSERT(sti->sti_discon_ind_mp == NULL);
67130f1702c5SYu Xiangning 
67140f1702c5SYu Xiangning 	if ((mp = sti->sti_conn_ind_head) != NULL) {
67150f1702c5SYu Xiangning 		mblk_t *mp1;
67160f1702c5SYu Xiangning 
67170f1702c5SYu Xiangning 		while (mp) {
67180f1702c5SYu Xiangning 			mp1 = mp->b_next;
67190f1702c5SYu Xiangning 			mp->b_next = NULL;
67200f1702c5SYu Xiangning 			freemsg(mp);
67210f1702c5SYu Xiangning 			mp = mp1;
67220f1702c5SYu Xiangning 		}
67230f1702c5SYu Xiangning 		sti->sti_conn_ind_head = sti->sti_conn_ind_tail = NULL;
67240f1702c5SYu Xiangning 	}
67250f1702c5SYu Xiangning 
67260f1702c5SYu Xiangning 	/*
67270f1702c5SYu Xiangning 	 * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely
67280f1702c5SYu Xiangning 	 * indirect them.  It also uses so_count as a validity test.
67290f1702c5SYu Xiangning 	 */
67300f1702c5SYu Xiangning 	mutex_enter(&so->so_lock);
67310f1702c5SYu Xiangning 
67320f1702c5SYu Xiangning 	if (sti->sti_laddr_sa) {
67330f1702c5SYu Xiangning 		ASSERT((caddr_t)sti->sti_faddr_sa ==
67340f1702c5SYu Xiangning 		    (caddr_t)sti->sti_laddr_sa + sti->sti_laddr_maxlen);
67350f1702c5SYu Xiangning 		ASSERT(sti->sti_faddr_maxlen == sti->sti_laddr_maxlen);
67360f1702c5SYu Xiangning 		sti->sti_laddr_valid = 0;
67370f1702c5SYu Xiangning 		sti->sti_faddr_valid = 0;
67380f1702c5SYu Xiangning 		kmem_free(sti->sti_laddr_sa, sti->sti_laddr_maxlen * 2);
67390f1702c5SYu Xiangning 		sti->sti_laddr_sa = NULL;
67400f1702c5SYu Xiangning 		sti->sti_laddr_len = sti->sti_laddr_maxlen = 0;
67410f1702c5SYu Xiangning 		sti->sti_faddr_sa = NULL;
67420f1702c5SYu Xiangning 		sti->sti_faddr_len = sti->sti_faddr_maxlen = 0;
67430f1702c5SYu Xiangning 	}
67440f1702c5SYu Xiangning 
67450f1702c5SYu Xiangning 	mutex_exit(&so->so_lock);
67460f1702c5SYu Xiangning 
67470f1702c5SYu Xiangning 	if ((mp = sti->sti_eaddr_mp) != NULL) {
67480f1702c5SYu Xiangning 		freemsg(mp);
67490f1702c5SYu Xiangning 		sti->sti_eaddr_mp = NULL;
67500f1702c5SYu Xiangning 		sti->sti_delayed_error = 0;
67510f1702c5SYu Xiangning 	}
67520f1702c5SYu Xiangning 
67530f1702c5SYu Xiangning 	if ((mp = sti->sti_ack_mp) != NULL) {
67540f1702c5SYu Xiangning 		freemsg(mp);
67550f1702c5SYu Xiangning 		sti->sti_ack_mp = NULL;
67560f1702c5SYu Xiangning 	}
67570f1702c5SYu Xiangning 
67580f1702c5SYu Xiangning 	if ((mp = sti->sti_nl7c_rcv_mp) != NULL) {
67590f1702c5SYu Xiangning 		sti->sti_nl7c_rcv_mp = NULL;
67600f1702c5SYu Xiangning 		freemsg(mp);
67610f1702c5SYu Xiangning 	}
67620f1702c5SYu Xiangning 	sti->sti_nl7c_rcv_rval = 0;
67630f1702c5SYu Xiangning 	if (sti->sti_nl7c_uri != NULL) {
67640f1702c5SYu Xiangning 		nl7c_urifree(so);
67650f1702c5SYu Xiangning 		/* urifree() cleared nl7c_uri */
67660f1702c5SYu Xiangning 	}
67670f1702c5SYu Xiangning 	if (sti->sti_nl7c_flags) {
67680f1702c5SYu Xiangning 		sti->sti_nl7c_flags = 0;
67690f1702c5SYu Xiangning 	}
67700f1702c5SYu Xiangning 
67710f1702c5SYu Xiangning 	ASSERT(sti->sti_ux_bound_vp == NULL);
67720f1702c5SYu Xiangning 	if ((mp = sti->sti_unbind_mp) != NULL) {
67730f1702c5SYu Xiangning 		freemsg(mp);
67740f1702c5SYu Xiangning 		sti->sti_unbind_mp = NULL;
67750f1702c5SYu Xiangning 	}
67760f1702c5SYu Xiangning }
67770f1702c5SYu Xiangning 
67780f1702c5SYu Xiangning /*
67790f1702c5SYu Xiangning  * Destroys the TPI information attached to a sonode.
67800f1702c5SYu Xiangning  */
67810f1702c5SYu Xiangning static void
67820f1702c5SYu Xiangning sotpi_info_destroy(struct sonode *so)
67830f1702c5SYu Xiangning {
67840f1702c5SYu Xiangning 	sotpi_info_t *sti = SOTOTPI(so);
67850f1702c5SYu Xiangning 
67860f1702c5SYu Xiangning 	i_sotpi_info_destructor(sti);
67870f1702c5SYu Xiangning 	kmem_free(sti, sizeof (*sti));
67880f1702c5SYu Xiangning 
67890f1702c5SYu Xiangning 	so->so_priv = NULL;
67900f1702c5SYu Xiangning }
67910f1702c5SYu Xiangning 
67920f1702c5SYu Xiangning /*
67932691240cSYu Xiangning  * Create the global sotpi socket module entry. It will never be freed.
67940f1702c5SYu Xiangning  */
67950f1702c5SYu Xiangning smod_info_t *
67960f1702c5SYu Xiangning sotpi_smod_create(void)
67970f1702c5SYu Xiangning {
67980f1702c5SYu Xiangning 	smod_info_t *smodp;
67990f1702c5SYu Xiangning 
68000f1702c5SYu Xiangning 	smodp = kmem_zalloc(sizeof (*smodp), KM_SLEEP);
68012691240cSYu Xiangning 	smodp->smod_name = kmem_alloc(sizeof (SOTPI_SMOD_NAME), KM_SLEEP);
68022691240cSYu Xiangning 	(void) strcpy(smodp->smod_name, SOTPI_SMOD_NAME);
68030f1702c5SYu Xiangning 	/*
68042691240cSYu Xiangning 	 * Initialize the smod_refcnt to 1 so it will never be freed.
68050f1702c5SYu Xiangning 	 */
68060f1702c5SYu Xiangning 	smodp->smod_refcnt = 1;
68070f1702c5SYu Xiangning 	smodp->smod_uc_version = SOCK_UC_VERSION;
68080f1702c5SYu Xiangning 	smodp->smod_dc_version = SOCK_DC_VERSION;
68090f1702c5SYu Xiangning 	smodp->smod_sock_create_func = &sotpi_create;
68100f1702c5SYu Xiangning 	smodp->smod_sock_destroy_func = &sotpi_destroy;
68110f1702c5SYu Xiangning 	return (smodp);
68120f1702c5SYu Xiangning }
6813