xref: /onnv-gate/usr/src/uts/common/fs/sockfs/socktpi.c (revision 4379:97a7ff056a78)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51548Srshoaib  * Common Development and Distribution License (the "License").
61548Srshoaib  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211548Srshoaib 
220Sstevel@tonic-gate /*
233388Skcpoon  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/buf.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/vfs.h>
390Sstevel@tonic-gate #include <sys/vnode.h>
400Sstevel@tonic-gate #include <sys/debug.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/time.h>
430Sstevel@tonic-gate #include <sys/file.h>
440Sstevel@tonic-gate #include <sys/open.h>
450Sstevel@tonic-gate #include <sys/user.h>
460Sstevel@tonic-gate #include <sys/termios.h>
470Sstevel@tonic-gate #include <sys/stream.h>
480Sstevel@tonic-gate #include <sys/strsubr.h>
490Sstevel@tonic-gate #include <sys/strsun.h>
500Sstevel@tonic-gate #include <sys/ddi.h>
510Sstevel@tonic-gate #include <sys/esunddi.h>
520Sstevel@tonic-gate #include <sys/flock.h>
530Sstevel@tonic-gate #include <sys/modctl.h>
540Sstevel@tonic-gate #include <sys/vtrace.h>
550Sstevel@tonic-gate #include <sys/cmn_err.h>
560Sstevel@tonic-gate #include <sys/pathname.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate #include <sys/socket.h>
590Sstevel@tonic-gate #include <sys/socketvar.h>
60741Smasputra #include <sys/sockio.h>
610Sstevel@tonic-gate #include <netinet/in.h>
620Sstevel@tonic-gate #include <sys/un.h>
630Sstevel@tonic-gate #include <sys/strsun.h>
640Sstevel@tonic-gate 
650Sstevel@tonic-gate #include <sys/tiuser.h>
660Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
670Sstevel@tonic-gate #include <sys/tihdr.h>
680Sstevel@tonic-gate #include <sys/timod.h>		/* TI_GETMYNAME, TI_GETPEERNAME */
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <c2/audit.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <inet/common.h>
730Sstevel@tonic-gate #include <inet/ip.h>
740Sstevel@tonic-gate #include <inet/ip6.h>
750Sstevel@tonic-gate #include <inet/tcp.h>
76741Smasputra #include <inet/udp_impl.h>
770Sstevel@tonic-gate 
781974Sbrutus #include <sys/zone.h>
791974Sbrutus 
800Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
811974Sbrutus #include <fs/sockfs/nl7curi.h>
820Sstevel@tonic-gate 
83898Skais #include <inet/kssl/ksslapi.h>
84898Skais 
850Sstevel@tonic-gate /*
860Sstevel@tonic-gate  * Possible failures when memory can't be allocated. The documented behavior:
870Sstevel@tonic-gate  *
880Sstevel@tonic-gate  * 		5.5:			4.X:		XNET:
890Sstevel@tonic-gate  * accept:	ENOMEM/ENOSR/EINTR	- (EINTR)	ENOMEM/ENOBUFS/ENOSR/
900Sstevel@tonic-gate  *							EINTR
910Sstevel@tonic-gate  *	(4.X does not document EINTR but returns it)
920Sstevel@tonic-gate  * bind:	ENOSR			-		ENOBUFS/ENOSR
930Sstevel@tonic-gate  * connect: 	EINTR			EINTR		ENOBUFS/ENOSR/EINTR
940Sstevel@tonic-gate  * getpeername:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
950Sstevel@tonic-gate  * getsockname:	ENOMEM/ENOSR		ENOBUFS (-)	ENOBUFS/ENOSR
960Sstevel@tonic-gate  *	(4.X getpeername and getsockname do not fail in practice)
970Sstevel@tonic-gate  * getsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
980Sstevel@tonic-gate  * listen:	-			-		ENOBUFS
990Sstevel@tonic-gate  * recv:	ENOMEM/ENOSR/EINTR	EINTR		ENOBUFS/ENOMEM/ENOSR/
1000Sstevel@tonic-gate  *							EINTR
1010Sstevel@tonic-gate  * send:	ENOMEM/ENOSR/EINTR	ENOBUFS/EINTR	ENOBUFS/ENOMEM/ENOSR/
1020Sstevel@tonic-gate  *							EINTR
1030Sstevel@tonic-gate  * setsockopt:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
1040Sstevel@tonic-gate  * shutdown:	ENOMEM/ENOSR		-		ENOBUFS/ENOSR
1050Sstevel@tonic-gate  * socket:	ENOMEM/ENOSR		ENOBUFS		ENOBUFS/ENOMEM/ENOSR
1060Sstevel@tonic-gate  * socketpair:	ENOMEM/ENOSR		-		ENOBUFS/ENOMEM/ENOSR
1070Sstevel@tonic-gate  *
1080Sstevel@tonic-gate  * Resolution. When allocation fails:
1090Sstevel@tonic-gate  *	recv: return EINTR
1100Sstevel@tonic-gate  *	send: return EINTR
1110Sstevel@tonic-gate  *	connect, accept: EINTR
1120Sstevel@tonic-gate  *	bind, listen, shutdown (unbind, unix_close, disconnect): sleep
1130Sstevel@tonic-gate  *	socket, socketpair: ENOBUFS
1140Sstevel@tonic-gate  *	getpeername, getsockname: sleep
1150Sstevel@tonic-gate  *	getsockopt, setsockopt: sleep
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate 
1180Sstevel@tonic-gate #ifdef SOCK_TEST
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * Variables that make sockfs do something other than the standard TPI
1210Sstevel@tonic-gate  * for the AF_INET transports.
1220Sstevel@tonic-gate  *
1230Sstevel@tonic-gate  * solisten_tpi_tcp:
1240Sstevel@tonic-gate  *	TCP can handle a O_T_BIND_REQ with an increased backlog even though
1250Sstevel@tonic-gate  *	the transport is already bound. This is needed to avoid loosing the
1260Sstevel@tonic-gate  *	port number should listen() do a T_UNBIND_REQ followed by a
1270Sstevel@tonic-gate  *	O_T_BIND_REQ.
1280Sstevel@tonic-gate  *
1290Sstevel@tonic-gate  * soconnect_tpi_udp:
1300Sstevel@tonic-gate  *	UDP and ICMP can handle a T_CONN_REQ.
1310Sstevel@tonic-gate  *	This is needed to make the sequence of connect(), getsockname()
1320Sstevel@tonic-gate  *	return the local IP address used to send packets to the connected to
1330Sstevel@tonic-gate  *	destination.
1340Sstevel@tonic-gate  *
1350Sstevel@tonic-gate  * soconnect_tpi_tcp:
1360Sstevel@tonic-gate  *	TCP can handle a T_CONN_REQ without seeing a O_T_BIND_REQ.
1370Sstevel@tonic-gate  *	Set this to non-zero to send TPI conformant messages to TCP in this
1380Sstevel@tonic-gate  *	respect. This is a performance optimization.
1390Sstevel@tonic-gate  *
1400Sstevel@tonic-gate  * soaccept_tpi_tcp:
1410Sstevel@tonic-gate  *	TCP can handle a T_CONN_REQ without the acceptor being bound.
1420Sstevel@tonic-gate  *	This is a performance optimization that has been picked up in XTI.
1430Sstevel@tonic-gate  *
1440Sstevel@tonic-gate  * soaccept_tpi_multioptions:
1450Sstevel@tonic-gate  *	When inheriting SOL_SOCKET options from the listener to the accepting
1460Sstevel@tonic-gate  *	socket send them as a single message for AF_INET{,6}.
1470Sstevel@tonic-gate  */
1480Sstevel@tonic-gate int solisten_tpi_tcp = 0;
1490Sstevel@tonic-gate int soconnect_tpi_udp = 0;
1500Sstevel@tonic-gate int soconnect_tpi_tcp = 0;
1510Sstevel@tonic-gate int soaccept_tpi_tcp = 0;
1520Sstevel@tonic-gate int soaccept_tpi_multioptions = 1;
1530Sstevel@tonic-gate #else /* SOCK_TEST */
1540Sstevel@tonic-gate #define	soconnect_tpi_tcp	0
1550Sstevel@tonic-gate #define	soconnect_tpi_udp	0
1560Sstevel@tonic-gate #define	solisten_tpi_tcp	0
1570Sstevel@tonic-gate #define	soaccept_tpi_tcp	0
1580Sstevel@tonic-gate #define	soaccept_tpi_multioptions	1
1590Sstevel@tonic-gate #endif /* SOCK_TEST */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate #ifdef SOCK_TEST
1620Sstevel@tonic-gate extern int do_useracc;
1630Sstevel@tonic-gate extern clock_t sock_test_timelimit;
1640Sstevel@tonic-gate #endif /* SOCK_TEST */
1650Sstevel@tonic-gate 
1660Sstevel@tonic-gate /*
1670Sstevel@tonic-gate  * Some X/Open added checks might have to be backed out to keep SunOS 4.X
1680Sstevel@tonic-gate  * applications working. Turn on this flag to disable these checks.
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate int xnet_skip_checks = 0;
1710Sstevel@tonic-gate int xnet_check_print = 0;
1720Sstevel@tonic-gate int xnet_truncate_print = 0;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate extern	void sigintr(k_sigset_t *, int);
1750Sstevel@tonic-gate extern	void sigunintr(k_sigset_t *);
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate extern	void *nl7c_lookup_addr(void *, t_uscalar_t);
1780Sstevel@tonic-gate extern	void *nl7c_add_addr(void *, t_uscalar_t);
1791974Sbrutus extern	void nl7c_listener_addr(void *, struct sonode *);
1800Sstevel@tonic-gate 
181898Skais /* Sockets acting as an in-kernel SSL proxy */
182898Skais extern mblk_t	*strsock_kssl_input(vnode_t *, mblk_t *, strwakeup_t *,
183898Skais 		    strsigset_t *, strsigset_t *, strpollset_t *);
184898Skais extern mblk_t	*strsock_kssl_output(vnode_t *, mblk_t *, strwakeup_t *,
185898Skais 		    strsigset_t *, strsigset_t *, strpollset_t *);
186898Skais 
1870Sstevel@tonic-gate static int	sotpi_unbind(struct sonode *, int);
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate /* TPI sockfs sonode operations */
1900Sstevel@tonic-gate static int	sotpi_accept(struct sonode *, int, struct sonode **);
1910Sstevel@tonic-gate static int	sotpi_bind(struct sonode *, struct sockaddr *, socklen_t,
1920Sstevel@tonic-gate 		    int);
1930Sstevel@tonic-gate static int	sotpi_connect(struct sonode *, const struct sockaddr *,
1940Sstevel@tonic-gate 		    socklen_t, int, int);
1950Sstevel@tonic-gate static int	sotpi_listen(struct sonode *, int);
1960Sstevel@tonic-gate static int	sotpi_sendmsg(struct sonode *, struct nmsghdr *,
1970Sstevel@tonic-gate 		    struct uio *);
1980Sstevel@tonic-gate static int	sotpi_shutdown(struct sonode *, int);
1990Sstevel@tonic-gate static int	sotpi_getsockname(struct sonode *);
200741Smasputra static int	sosend_dgramcmsg(struct sonode *, struct sockaddr *, socklen_t,
201741Smasputra 		    struct uio *, void *, t_uscalar_t, int);
202741Smasputra static int	sodgram_direct(struct sonode *, struct sockaddr *,
203741Smasputra 		    socklen_t, struct uio *, int);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate sonodeops_t sotpi_sonodeops = {
2060Sstevel@tonic-gate 	sotpi_accept,		/* sop_accept		*/
2070Sstevel@tonic-gate 	sotpi_bind,		/* sop_bind		*/
2080Sstevel@tonic-gate 	sotpi_listen,		/* sop_listen		*/
2090Sstevel@tonic-gate 	sotpi_connect,		/* sop_connect		*/
2100Sstevel@tonic-gate 	sotpi_recvmsg,		/* sop_recvmsg		*/
2110Sstevel@tonic-gate 	sotpi_sendmsg,		/* sop_sendmsg		*/
2120Sstevel@tonic-gate 	sotpi_getpeername,	/* sop_getpeername	*/
2130Sstevel@tonic-gate 	sotpi_getsockname,	/* sop_getsockname	*/
2140Sstevel@tonic-gate 	sotpi_shutdown,		/* sop_shutdown		*/
2150Sstevel@tonic-gate 	sotpi_getsockopt,	/* sop_getsockopt	*/
2160Sstevel@tonic-gate 	sotpi_setsockopt	/* sop_setsockopt	*/
2170Sstevel@tonic-gate };
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate /*
2200Sstevel@tonic-gate  * Common create code for socket and accept. If tso is set the values
2210Sstevel@tonic-gate  * from that node is used instead of issuing a T_INFO_REQ.
2220Sstevel@tonic-gate  *
2230Sstevel@tonic-gate  * Assumes that the caller has a VN_HOLD on accessvp.
2240Sstevel@tonic-gate  * The VN_RELE will occur either when sotpi_create() fails or when
2250Sstevel@tonic-gate  * the returned sonode is freed.
2260Sstevel@tonic-gate  */
2270Sstevel@tonic-gate struct sonode *
2280Sstevel@tonic-gate sotpi_create(vnode_t *accessvp, int domain, int type, int protocol, int version,
2290Sstevel@tonic-gate     struct sonode *tso, int *errorp)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 	struct sonode	*so;
2320Sstevel@tonic-gate 	vnode_t		*vp;
2330Sstevel@tonic-gate 	int		flags, error;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	ASSERT(accessvp != NULL);
2360Sstevel@tonic-gate 	vp = makesockvp(accessvp, domain, type, protocol);
2370Sstevel@tonic-gate 	ASSERT(vp != NULL);
2380Sstevel@tonic-gate 	so = VTOSO(vp);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	flags = FREAD|FWRITE;
241741Smasputra 
242741Smasputra 	if ((type == SOCK_STREAM || type == SOCK_DGRAM) &&
243741Smasputra 	    (domain == AF_INET || domain == AF_INET6) &&
244741Smasputra 	    (protocol == IPPROTO_TCP || protocol == IPPROTO_UDP ||
245741Smasputra 	    protocol == IPPROTO_IP)) {
246741Smasputra 		/* Tell tcp or udp that it's talking to sockets */
247741Smasputra 		flags |= SO_SOCKSTR;
248741Smasputra 
249741Smasputra 		/*
250741Smasputra 		 * Here we indicate to socktpi_open() our attempt to
251741Smasputra 		 * make direct calls between sockfs and transport.
252741Smasputra 		 * The final decision is left to socktpi_open().
253741Smasputra 		 */
254741Smasputra 		so->so_state |= SS_DIRECT;
255741Smasputra 
256741Smasputra 		ASSERT(so->so_type != SOCK_DGRAM || tso == NULL);
257741Smasputra 		if (so->so_type == SOCK_STREAM && tso != NULL) {
258741Smasputra 			if (tso->so_state & SS_DIRECT) {
259741Smasputra 				/*
260741Smasputra 				 * Inherit SS_DIRECT from listener and pass
261741Smasputra 				 * SO_ACCEPTOR open flag to tcp, indicating
262741Smasputra 				 * that this is an accept fast-path instance.
263741Smasputra 				 */
264741Smasputra 				flags |= SO_ACCEPTOR;
265741Smasputra 			} else {
266741Smasputra 				/*
267741Smasputra 				 * SS_DIRECT is not set on listener, meaning
268741Smasputra 				 * that the listener has been converted from
269741Smasputra 				 * a socket to a stream.  Ensure that the
270741Smasputra 				 * acceptor inherits these settings.
271741Smasputra 				 */
272741Smasputra 				so->so_state &= ~SS_DIRECT;
273741Smasputra 				flags &= ~SO_SOCKSTR;
274741Smasputra 			}
2750Sstevel@tonic-gate 		}
2760Sstevel@tonic-gate 	}
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/*
2790Sstevel@tonic-gate 	 * Tell local transport that it is talking to sockets.
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
2820Sstevel@tonic-gate 		flags |= SO_SOCKSTR;
2830Sstevel@tonic-gate 	}
2840Sstevel@tonic-gate 
2851092Skais 	/* Initialize the kernel SSL proxy fields */
2861092Skais 	so->so_kssl_type = KSSL_NO_PROXY;
2871092Skais 	so->so_kssl_ent = NULL;
2881092Skais 	so->so_kssl_ctx = NULL;
2891092Skais 
2900Sstevel@tonic-gate 	if (error = socktpi_open(&vp, flags, CRED())) {
2910Sstevel@tonic-gate 		VN_RELE(vp);
2920Sstevel@tonic-gate 		*errorp = error;
2930Sstevel@tonic-gate 		return (NULL);
2940Sstevel@tonic-gate 	}
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	if (error = so_strinit(so, tso)) {
2970Sstevel@tonic-gate 		(void) VOP_CLOSE(vp, 0, 1, 0, CRED());
2980Sstevel@tonic-gate 		VN_RELE(vp);
2990Sstevel@tonic-gate 		*errorp = error;
3000Sstevel@tonic-gate 		return (NULL);
3010Sstevel@tonic-gate 	}
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate 	if (version == SOV_DEFAULT)
3040Sstevel@tonic-gate 		version = so_default_version;
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	so->so_version = (short)version;
307898Skais 
3080Sstevel@tonic-gate 	return (so);
3090Sstevel@tonic-gate }
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate /*
3120Sstevel@tonic-gate  * Bind the socket to an unspecified address in sockfs only.
3130Sstevel@tonic-gate  * Used for TCP/UDP transports where we know that the O_T_BIND_REQ isn't
3140Sstevel@tonic-gate  * required in all cases.
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate static void
3170Sstevel@tonic-gate so_automatic_bind(struct sonode *so)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3220Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_ISBOUND));
3230Sstevel@tonic-gate 	ASSERT(so->so_unbind_mp);
3240Sstevel@tonic-gate 
3250Sstevel@tonic-gate 	ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
3260Sstevel@tonic-gate 	bzero(so->so_laddr_sa, so->so_laddr_len);
3270Sstevel@tonic-gate 	so->so_laddr_sa->sa_family = so->so_family;
3280Sstevel@tonic-gate 	so->so_state |= SS_ISBOUND;
3290Sstevel@tonic-gate }
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * bind the socket.
3340Sstevel@tonic-gate  *
3350Sstevel@tonic-gate  * If the socket is already bound and none of _SOBIND_SOCKBSD or _SOBIND_XPG4_2
3360Sstevel@tonic-gate  * are passed in we allow rebinding. Note that for backwards compatibility
3370Sstevel@tonic-gate  * even "svr4" sockets pass in _SOBIND_SOCKBSD/SOV_SOCKBSD to sobind/bind.
3380Sstevel@tonic-gate  * Thus the rebinding code is currently not executed.
3390Sstevel@tonic-gate  *
3400Sstevel@tonic-gate  * The constraints for rebinding are:
3410Sstevel@tonic-gate  * - it is a SOCK_DGRAM, or
3420Sstevel@tonic-gate  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
3430Sstevel@tonic-gate  *   and no listen() has been done.
3440Sstevel@tonic-gate  * This rebinding code was added based on some language in the XNET book
3450Sstevel@tonic-gate  * about not returning EINVAL it the protocol allows rebinding. However,
3460Sstevel@tonic-gate  * this language is not present in the Posix socket draft. Thus maybe the
3470Sstevel@tonic-gate  * rebinding logic should be deleted from the source.
3480Sstevel@tonic-gate  *
3490Sstevel@tonic-gate  * A null "name" can be used to unbind the socket if:
3500Sstevel@tonic-gate  * - it is a SOCK_DGRAM, or
3510Sstevel@tonic-gate  * - it is a SOCK_STREAM/SOCK_SEQPACKET that has not been connected
3520Sstevel@tonic-gate  *   and no listen() has been done.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate static int
3550Sstevel@tonic-gate sotpi_bindlisten(struct sonode *so, struct sockaddr *name,
3560Sstevel@tonic-gate     socklen_t namelen, int backlog, int flags)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 	struct T_bind_req	bind_req;
3590Sstevel@tonic-gate 	struct T_bind_ack	*bind_ack;
3600Sstevel@tonic-gate 	int			error = 0;
3610Sstevel@tonic-gate 	mblk_t			*mp;
3620Sstevel@tonic-gate 	void			*addr;
3630Sstevel@tonic-gate 	t_uscalar_t		addrlen;
3640Sstevel@tonic-gate 	int			unbind_on_err = 1;
3650Sstevel@tonic-gate 	boolean_t		clear_acceptconn_on_err = B_FALSE;
3660Sstevel@tonic-gate 	boolean_t		restore_backlog_on_err = B_FALSE;
3670Sstevel@tonic-gate 	int			save_so_backlog;
3680Sstevel@tonic-gate 	t_scalar_t		PRIM_type = O_T_BIND_REQ;
3690Sstevel@tonic-gate 	boolean_t		tcp_udp_xport;
3700Sstevel@tonic-gate 	void			*nl7c = NULL;
3710Sstevel@tonic-gate 
3720Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_bindlisten(%p, %p, %d, %d, 0x%x) %s\n",
3730Sstevel@tonic-gate 		so, name, namelen, backlog, flags,
3740Sstevel@tonic-gate 		pr_state(so->so_state, so->so_mode)));
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	tcp_udp_xport = so->so_type == SOCK_STREAM || so->so_type == SOCK_DGRAM;
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	if (!(flags & _SOBIND_LOCK_HELD)) {
3790Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
3800Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
3810Sstevel@tonic-gate 	} else {
3820Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
3830Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 
3860Sstevel@tonic-gate 	/*
3870Sstevel@tonic-gate 	 * Make sure that there is a preallocated unbind_req message
3880Sstevel@tonic-gate 	 * before binding. This message allocated when the socket is
3890Sstevel@tonic-gate 	 * created  but it might be have been consumed.
3900Sstevel@tonic-gate 	 */
3910Sstevel@tonic-gate 	if (so->so_unbind_mp == NULL) {
3920Sstevel@tonic-gate 		dprintso(so, 1, ("sobind: allocating unbind_req\n"));
3930Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
3940Sstevel@tonic-gate 		so->so_unbind_mp =
3950Sstevel@tonic-gate 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_SLEEP);
3960Sstevel@tonic-gate 	}
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate 	if (flags & _SOBIND_REBIND) {
3990Sstevel@tonic-gate 		/*
4000Sstevel@tonic-gate 		 * Called from solisten after doing an sotpi_unbind() or
4010Sstevel@tonic-gate 		 * potentially without the unbind (latter for AF_INET{,6}).
4020Sstevel@tonic-gate 		 */
4030Sstevel@tonic-gate 		ASSERT(name == NULL && namelen == 0);
4040Sstevel@tonic-gate 
4050Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
4060Sstevel@tonic-gate 			ASSERT(so->so_ux_bound_vp);
4070Sstevel@tonic-gate 			addr = &so->so_ux_laddr;
4080Sstevel@tonic-gate 			addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr);
4090Sstevel@tonic-gate 			dprintso(so, 1,
4100Sstevel@tonic-gate 			("sobind rebind UNIX: addrlen %d, addr 0x%p, vp %p\n",
4110Sstevel@tonic-gate 			    addrlen,
4120Sstevel@tonic-gate 			    ((struct so_ux_addr *)addr)->soua_vp,
4130Sstevel@tonic-gate 			    so->so_ux_bound_vp));
4140Sstevel@tonic-gate 		} else {
4150Sstevel@tonic-gate 			addr = so->so_laddr_sa;
4160Sstevel@tonic-gate 			addrlen = (t_uscalar_t)so->so_laddr_len;
4170Sstevel@tonic-gate 		}
4180Sstevel@tonic-gate 	} else if (flags & _SOBIND_UNSPEC) {
4190Sstevel@tonic-gate 		ASSERT(name == NULL && namelen == 0);
4200Sstevel@tonic-gate 
4210Sstevel@tonic-gate 		/*
4220Sstevel@tonic-gate 		 * The caller checked SS_ISBOUND but not necessarily
4230Sstevel@tonic-gate 		 * under so_lock
4240Sstevel@tonic-gate 		 */
4250Sstevel@tonic-gate 		if (so->so_state & SS_ISBOUND) {
4260Sstevel@tonic-gate 			/* No error */
4270Sstevel@tonic-gate 			goto done;
4280Sstevel@tonic-gate 		}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 		/* Set an initial local address */
4310Sstevel@tonic-gate 		switch (so->so_family) {
4320Sstevel@tonic-gate 		case AF_UNIX:
4330Sstevel@tonic-gate 			/*
4340Sstevel@tonic-gate 			 * Use an address with same size as struct sockaddr
4350Sstevel@tonic-gate 			 * just like BSD.
4360Sstevel@tonic-gate 			 */
4370Sstevel@tonic-gate 			so->so_laddr_len =
4380Sstevel@tonic-gate 				(socklen_t)sizeof (struct sockaddr);
4390Sstevel@tonic-gate 			ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
4400Sstevel@tonic-gate 			bzero(so->so_laddr_sa, so->so_laddr_len);
4410Sstevel@tonic-gate 			so->so_laddr_sa->sa_family = so->so_family;
4420Sstevel@tonic-gate 
4430Sstevel@tonic-gate 			/*
4440Sstevel@tonic-gate 			 * Pass down an address with the implicit bind
4450Sstevel@tonic-gate 			 * magic number and the rest all zeros.
4460Sstevel@tonic-gate 			 * The transport will return a unique address.
4470Sstevel@tonic-gate 			 */
4480Sstevel@tonic-gate 			so->so_ux_laddr.soua_vp = NULL;
4490Sstevel@tonic-gate 			so->so_ux_laddr.soua_magic = SOU_MAGIC_IMPLICIT;
4500Sstevel@tonic-gate 			addr = &so->so_ux_laddr;
4510Sstevel@tonic-gate 			addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr);
4520Sstevel@tonic-gate 			break;
4530Sstevel@tonic-gate 
4540Sstevel@tonic-gate 		case AF_INET:
4550Sstevel@tonic-gate 		case AF_INET6:
4560Sstevel@tonic-gate 			/*
4570Sstevel@tonic-gate 			 * An unspecified bind in TPI has a NULL address.
4580Sstevel@tonic-gate 			 * Set the address in sockfs to have the sa_family.
4590Sstevel@tonic-gate 			 */
4600Sstevel@tonic-gate 			so->so_laddr_len = (so->so_family == AF_INET) ?
4610Sstevel@tonic-gate 			    (socklen_t)sizeof (sin_t) :
4620Sstevel@tonic-gate 			    (socklen_t)sizeof (sin6_t);
4630Sstevel@tonic-gate 			ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
4640Sstevel@tonic-gate 			bzero(so->so_laddr_sa, so->so_laddr_len);
4650Sstevel@tonic-gate 			so->so_laddr_sa->sa_family = so->so_family;
4660Sstevel@tonic-gate 			addr = NULL;
4670Sstevel@tonic-gate 			addrlen = 0;
4680Sstevel@tonic-gate 			break;
4690Sstevel@tonic-gate 
4700Sstevel@tonic-gate 		default:
4710Sstevel@tonic-gate 			/*
4720Sstevel@tonic-gate 			 * An unspecified bind in TPI has a NULL address.
4730Sstevel@tonic-gate 			 * Set the address in sockfs to be zero length.
4740Sstevel@tonic-gate 			 *
4750Sstevel@tonic-gate 			 * Can not assume there is a sa_family for all
4760Sstevel@tonic-gate 			 * protocol families. For example, AF_X25 does not
4770Sstevel@tonic-gate 			 * have a family field.
4780Sstevel@tonic-gate 			 */
4791548Srshoaib 			bzero(so->so_laddr_sa, so->so_laddr_len);
4800Sstevel@tonic-gate 			so->so_laddr_len = 0;	/* XXX correct? */
4810Sstevel@tonic-gate 			addr = NULL;
4820Sstevel@tonic-gate 			addrlen = 0;
4830Sstevel@tonic-gate 			break;
4840Sstevel@tonic-gate 		}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	} else {
4870Sstevel@tonic-gate 		if (so->so_state & SS_ISBOUND) {
4880Sstevel@tonic-gate 			/*
4890Sstevel@tonic-gate 			 * If it is ok to rebind the socket, first unbind
4900Sstevel@tonic-gate 			 * with the transport. A rebind to the NULL address
4910Sstevel@tonic-gate 			 * is interpreted as an unbind.
4920Sstevel@tonic-gate 			 * Note that a bind to NULL in BSD does unbind the
4930Sstevel@tonic-gate 			 * socket but it fails with EINVAL.
4940Sstevel@tonic-gate 			 * Note that regular sockets set SOV_SOCKBSD i.e.
4950Sstevel@tonic-gate 			 * _SOBIND_SOCKBSD gets set here hence no type of
4960Sstevel@tonic-gate 			 * socket does currently allow rebinding.
4970Sstevel@tonic-gate 			 *
4980Sstevel@tonic-gate 			 * If the name is NULL just do an unbind.
4990Sstevel@tonic-gate 			 */
5000Sstevel@tonic-gate 			if (flags & (_SOBIND_SOCKBSD|_SOBIND_XPG4_2) &&
5010Sstevel@tonic-gate 			    name != NULL) {
5020Sstevel@tonic-gate 				error = EINVAL;
5030Sstevel@tonic-gate 				unbind_on_err = 0;
5040Sstevel@tonic-gate 				eprintsoline(so, error);
5050Sstevel@tonic-gate 				goto done;
5060Sstevel@tonic-gate 			}
5070Sstevel@tonic-gate 			if ((so->so_mode & SM_CONNREQUIRED) &&
5080Sstevel@tonic-gate 			    (so->so_state & SS_CANTREBIND)) {
5090Sstevel@tonic-gate 				error = EINVAL;
5100Sstevel@tonic-gate 				unbind_on_err = 0;
5110Sstevel@tonic-gate 				eprintsoline(so, error);
5120Sstevel@tonic-gate 				goto done;
5130Sstevel@tonic-gate 			}
5140Sstevel@tonic-gate 			error = sotpi_unbind(so, 0);
5150Sstevel@tonic-gate 			if (error) {
5160Sstevel@tonic-gate 				eprintsoline(so, error);
5170Sstevel@tonic-gate 				goto done;
5180Sstevel@tonic-gate 			}
5190Sstevel@tonic-gate 			ASSERT(!(so->so_state & SS_ISBOUND));
5200Sstevel@tonic-gate 			if (name == NULL) {
5210Sstevel@tonic-gate 				so->so_state &=
5220Sstevel@tonic-gate 					~(SS_ISCONNECTED|SS_ISCONNECTING);
5230Sstevel@tonic-gate 				goto done;
5240Sstevel@tonic-gate 			}
5250Sstevel@tonic-gate 		}
5260Sstevel@tonic-gate 		/* X/Open requires this check */
5270Sstevel@tonic-gate 		if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
5280Sstevel@tonic-gate 			if (xnet_check_print) {
5290Sstevel@tonic-gate 				printf("sockfs: X/Open bind state check "
5300Sstevel@tonic-gate 				    "caused EINVAL\n");
5310Sstevel@tonic-gate 			}
5320Sstevel@tonic-gate 			error = EINVAL;
5330Sstevel@tonic-gate 			goto done;
5340Sstevel@tonic-gate 		}
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 		switch (so->so_family) {
5370Sstevel@tonic-gate 		case AF_UNIX:
5380Sstevel@tonic-gate 			/*
5390Sstevel@tonic-gate 			 * All AF_UNIX addresses are nul terminated
5400Sstevel@tonic-gate 			 * when copied (copyin_name) in so the minimum
5410Sstevel@tonic-gate 			 * length is 3 bytes.
5420Sstevel@tonic-gate 			 */
5430Sstevel@tonic-gate 			if (name == NULL ||
5440Sstevel@tonic-gate 			    (ssize_t)namelen <= sizeof (short) + 1) {
5450Sstevel@tonic-gate 				error = EISDIR;
5460Sstevel@tonic-gate 				eprintsoline(so, error);
5470Sstevel@tonic-gate 				goto done;
5480Sstevel@tonic-gate 			}
5490Sstevel@tonic-gate 			/*
5500Sstevel@tonic-gate 			 * Verify so_family matches the bound family.
5510Sstevel@tonic-gate 			 * BSD does not check this for AF_UNIX resulting
5520Sstevel@tonic-gate 			 * in funny mknods.
5530Sstevel@tonic-gate 			 */
5540Sstevel@tonic-gate 			if (name->sa_family != so->so_family) {
5550Sstevel@tonic-gate 				error = EAFNOSUPPORT;
5560Sstevel@tonic-gate 				goto done;
5570Sstevel@tonic-gate 			}
5580Sstevel@tonic-gate 			break;
5590Sstevel@tonic-gate 		case AF_INET:
5600Sstevel@tonic-gate 			if (name == NULL) {
5610Sstevel@tonic-gate 				error = EINVAL;
5620Sstevel@tonic-gate 				eprintsoline(so, error);
5630Sstevel@tonic-gate 				goto done;
5640Sstevel@tonic-gate 			}
5650Sstevel@tonic-gate 			if ((size_t)namelen != sizeof (sin_t)) {
5660Sstevel@tonic-gate 				error = name->sa_family != so->so_family ?
5670Sstevel@tonic-gate 				    EAFNOSUPPORT : EINVAL;
5680Sstevel@tonic-gate 				eprintsoline(so, error);
5690Sstevel@tonic-gate 				goto done;
5700Sstevel@tonic-gate 			}
5710Sstevel@tonic-gate 			if ((flags & _SOBIND_XPG4_2) &&
5720Sstevel@tonic-gate 			    (name->sa_family != so->so_family)) {
5730Sstevel@tonic-gate 				/*
5740Sstevel@tonic-gate 				 * This check has to be made for X/Open
5750Sstevel@tonic-gate 				 * sockets however application failures have
5760Sstevel@tonic-gate 				 * been observed when it is applied to
5770Sstevel@tonic-gate 				 * all sockets.
5780Sstevel@tonic-gate 				 */
5790Sstevel@tonic-gate 				error = EAFNOSUPPORT;
5800Sstevel@tonic-gate 				eprintsoline(so, error);
5810Sstevel@tonic-gate 				goto done;
5820Sstevel@tonic-gate 			}
5830Sstevel@tonic-gate 			/*
5840Sstevel@tonic-gate 			 * Force a zero sa_family to match so_family.
5850Sstevel@tonic-gate 			 *
5860Sstevel@tonic-gate 			 * Some programs like inetd(1M) don't set the
5870Sstevel@tonic-gate 			 * family field. Other programs leave
5880Sstevel@tonic-gate 			 * sin_family set to garbage - SunOS 4.X does
5890Sstevel@tonic-gate 			 * not check the family field on a bind.
5900Sstevel@tonic-gate 			 * We use the family field that
5910Sstevel@tonic-gate 			 * was passed in to the socket() call.
5920Sstevel@tonic-gate 			 */
5930Sstevel@tonic-gate 			name->sa_family = so->so_family;
5940Sstevel@tonic-gate 			break;
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		case AF_INET6: {
5970Sstevel@tonic-gate #ifdef DEBUG
5980Sstevel@tonic-gate 			sin6_t *sin6 = (sin6_t *)name;
5990Sstevel@tonic-gate #endif /* DEBUG */
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate 			if (name == NULL) {
6020Sstevel@tonic-gate 				error = EINVAL;
6030Sstevel@tonic-gate 				eprintsoline(so, error);
6040Sstevel@tonic-gate 				goto done;
6050Sstevel@tonic-gate 			}
6060Sstevel@tonic-gate 			if ((size_t)namelen != sizeof (sin6_t)) {
6070Sstevel@tonic-gate 				error = name->sa_family != so->so_family ?
6080Sstevel@tonic-gate 				    EAFNOSUPPORT : EINVAL;
6090Sstevel@tonic-gate 				eprintsoline(so, error);
6100Sstevel@tonic-gate 				goto done;
6110Sstevel@tonic-gate 			}
6120Sstevel@tonic-gate 			if (name->sa_family != so->so_family) {
6130Sstevel@tonic-gate 				/*
6140Sstevel@tonic-gate 				 * With IPv6 we require the family to match
6150Sstevel@tonic-gate 				 * unlike in IPv4.
6160Sstevel@tonic-gate 				 */
6170Sstevel@tonic-gate 				error = EAFNOSUPPORT;
6180Sstevel@tonic-gate 				eprintsoline(so, error);
6190Sstevel@tonic-gate 				goto done;
6200Sstevel@tonic-gate 			}
6210Sstevel@tonic-gate #ifdef DEBUG
6220Sstevel@tonic-gate 			/*
6230Sstevel@tonic-gate 			 * Verify that apps don't forget to clear
6240Sstevel@tonic-gate 			 * sin6_scope_id etc
6250Sstevel@tonic-gate 			 */
6260Sstevel@tonic-gate 			if (sin6->sin6_scope_id != 0 &&
6270Sstevel@tonic-gate 			    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
6281548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
6290Sstevel@tonic-gate 				    "bind with uninitialized sin6_scope_id "
6300Sstevel@tonic-gate 				    "(%d) on socket. Pid = %d\n",
6310Sstevel@tonic-gate 				    (int)sin6->sin6_scope_id,
6320Sstevel@tonic-gate 				    (int)curproc->p_pid);
6330Sstevel@tonic-gate 			}
6340Sstevel@tonic-gate 			if (sin6->__sin6_src_id != 0) {
6351548Srshoaib 				zcmn_err(getzoneid(), CE_WARN,
6360Sstevel@tonic-gate 				    "bind with uninitialized __sin6_src_id "
6370Sstevel@tonic-gate 				    "(%d) on socket. Pid = %d\n",
6380Sstevel@tonic-gate 				    (int)sin6->__sin6_src_id,
6390Sstevel@tonic-gate 				    (int)curproc->p_pid);
6400Sstevel@tonic-gate 			}
6410Sstevel@tonic-gate #endif /* DEBUG */
6420Sstevel@tonic-gate 			break;
6430Sstevel@tonic-gate 		}
6440Sstevel@tonic-gate 		default:
6450Sstevel@tonic-gate 			/*
6460Sstevel@tonic-gate 			 * Don't do any length or sa_family check to allow
6470Sstevel@tonic-gate 			 * non-sockaddr style addresses.
6480Sstevel@tonic-gate 			 */
6490Sstevel@tonic-gate 			if (name == NULL) {
6500Sstevel@tonic-gate 				error = EINVAL;
6510Sstevel@tonic-gate 				eprintsoline(so, error);
6520Sstevel@tonic-gate 				goto done;
6530Sstevel@tonic-gate 			}
6540Sstevel@tonic-gate 			break;
6550Sstevel@tonic-gate 		}
6560Sstevel@tonic-gate 
6570Sstevel@tonic-gate 		if (namelen > (t_uscalar_t)so->so_laddr_maxlen) {
6580Sstevel@tonic-gate 			error = ENAMETOOLONG;
6590Sstevel@tonic-gate 			eprintsoline(so, error);
6600Sstevel@tonic-gate 			goto done;
6610Sstevel@tonic-gate 		}
6620Sstevel@tonic-gate 		/*
6630Sstevel@tonic-gate 		 * Save local address.
6640Sstevel@tonic-gate 		 */
6650Sstevel@tonic-gate 		so->so_laddr_len = (socklen_t)namelen;
6660Sstevel@tonic-gate 		ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
6670Sstevel@tonic-gate 		bcopy(name, so->so_laddr_sa, namelen);
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 		addr = so->so_laddr_sa;
6700Sstevel@tonic-gate 		addrlen = (t_uscalar_t)so->so_laddr_len;
6710Sstevel@tonic-gate 		switch (so->so_family) {
6720Sstevel@tonic-gate 		case AF_INET6:
6730Sstevel@tonic-gate 		case AF_INET:
6740Sstevel@tonic-gate 			break;
6750Sstevel@tonic-gate 		case AF_UNIX: {
6760Sstevel@tonic-gate 			struct sockaddr_un *soun =
6770Sstevel@tonic-gate 				(struct sockaddr_un *)so->so_laddr_sa;
6780Sstevel@tonic-gate 			struct vnode *vp;
6790Sstevel@tonic-gate 			struct vattr vattr;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 			ASSERT(so->so_ux_bound_vp == NULL);
6820Sstevel@tonic-gate 			/*
6830Sstevel@tonic-gate 			 * Create vnode for the specified path name.
6840Sstevel@tonic-gate 			 * Keep vnode held with a reference in so_ux_bound_vp.
6850Sstevel@tonic-gate 			 * Use the vnode pointer as the address used in the
6860Sstevel@tonic-gate 			 * bind with the transport.
6870Sstevel@tonic-gate 			 *
6880Sstevel@tonic-gate 			 * Use the same mode as in BSD. In particular this does
6890Sstevel@tonic-gate 			 * not observe the umask.
6900Sstevel@tonic-gate 			 */
6910Sstevel@tonic-gate 			/* MAXPATHLEN + soun_family + nul termination */
6920Sstevel@tonic-gate 			if (so->so_laddr_len >
6930Sstevel@tonic-gate 			    (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
6940Sstevel@tonic-gate 				error = ENAMETOOLONG;
6950Sstevel@tonic-gate 				eprintsoline(so, error);
6960Sstevel@tonic-gate 				goto done;
6970Sstevel@tonic-gate 			}
6980Sstevel@tonic-gate 			vattr.va_type = VSOCK;
6993446Smrj 			vattr.va_mode = 0777 & ~PTOU(curproc)->u_cmask;
7000Sstevel@tonic-gate 			vattr.va_mask = AT_TYPE|AT_MODE;
7010Sstevel@tonic-gate 			/* NOTE: holding so_lock */
7020Sstevel@tonic-gate 			error = vn_create(soun->sun_path, UIO_SYSSPACE, &vattr,
7030Sstevel@tonic-gate 						EXCL, 0, &vp, CRMKNOD, 0, 0);
7040Sstevel@tonic-gate 			if (error) {
7050Sstevel@tonic-gate 				if (error == EEXIST)
7060Sstevel@tonic-gate 					error = EADDRINUSE;
7070Sstevel@tonic-gate 				eprintsoline(so, error);
7080Sstevel@tonic-gate 				goto done;
7090Sstevel@tonic-gate 			}
7100Sstevel@tonic-gate 			/*
7110Sstevel@tonic-gate 			 * Establish pointer from the underlying filesystem
7120Sstevel@tonic-gate 			 * vnode to the socket node.
7130Sstevel@tonic-gate 			 * so_ux_bound_vp and v_stream->sd_vnode form the
7140Sstevel@tonic-gate 			 * cross-linkage between the underlying filesystem
7150Sstevel@tonic-gate 			 * node and the socket node.
7160Sstevel@tonic-gate 			 */
7170Sstevel@tonic-gate 			ASSERT(SOTOV(so)->v_stream);
7180Sstevel@tonic-gate 			mutex_enter(&vp->v_lock);
7190Sstevel@tonic-gate 			vp->v_stream = SOTOV(so)->v_stream;
7200Sstevel@tonic-gate 			so->so_ux_bound_vp = vp;
7210Sstevel@tonic-gate 			mutex_exit(&vp->v_lock);
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate 			/*
7240Sstevel@tonic-gate 			 * Use the vnode pointer value as a unique address
7250Sstevel@tonic-gate 			 * (together with the magic number to avoid conflicts
7260Sstevel@tonic-gate 			 * with implicit binds) in the transport provider.
7270Sstevel@tonic-gate 			 */
7280Sstevel@tonic-gate 			so->so_ux_laddr.soua_vp = (void *)so->so_ux_bound_vp;
7290Sstevel@tonic-gate 			so->so_ux_laddr.soua_magic = SOU_MAGIC_EXPLICIT;
7300Sstevel@tonic-gate 			addr = &so->so_ux_laddr;
7310Sstevel@tonic-gate 			addrlen = (t_uscalar_t)sizeof (so->so_ux_laddr);
7320Sstevel@tonic-gate 			dprintso(so, 1, ("sobind UNIX: addrlen %d, addr %p\n",
7330Sstevel@tonic-gate 			    addrlen,
7340Sstevel@tonic-gate 			    ((struct so_ux_addr *)addr)->soua_vp));
7350Sstevel@tonic-gate 			break;
7360Sstevel@tonic-gate 		}
7370Sstevel@tonic-gate 		} /* end switch (so->so_family) */
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	/*
7410Sstevel@tonic-gate 	 * set SS_ACCEPTCONN before sending down O_T_BIND_REQ since
7420Sstevel@tonic-gate 	 * the transport can start passing up T_CONN_IND messages
7430Sstevel@tonic-gate 	 * as soon as it receives the bind req and strsock_proto()
7440Sstevel@tonic-gate 	 * insists that SS_ACCEPTCONN is set when processing T_CONN_INDs.
7450Sstevel@tonic-gate 	 */
7460Sstevel@tonic-gate 	if (flags & _SOBIND_LISTEN) {
7470Sstevel@tonic-gate 		if ((so->so_state & SS_ACCEPTCONN) == 0)
7480Sstevel@tonic-gate 			clear_acceptconn_on_err = B_TRUE;
7490Sstevel@tonic-gate 		save_so_backlog = so->so_backlog;
7500Sstevel@tonic-gate 		restore_backlog_on_err = B_TRUE;
7510Sstevel@tonic-gate 		so->so_state |= SS_ACCEPTCONN;
7520Sstevel@tonic-gate 		so->so_backlog = backlog;
7530Sstevel@tonic-gate 	}
7540Sstevel@tonic-gate 
7550Sstevel@tonic-gate 	/*
7560Sstevel@tonic-gate 	 * If NL7C addr(s) have been configured check for addr/port match,
7570Sstevel@tonic-gate 	 * or if an implicit NL7C socket via AF_NCA mark socket as NL7C.
7580Sstevel@tonic-gate 	 *
7590Sstevel@tonic-gate 	 * NL7C supports the TCP transport only so check AF_INET and AF_INET6
7600Sstevel@tonic-gate 	 * family sockets only. If match mark as such.
7610Sstevel@tonic-gate 	 */
7621974Sbrutus 	if (nl7c_enabled && ((addr != NULL &&
7630Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
7640Sstevel@tonic-gate 	    (nl7c = nl7c_lookup_addr(addr, addrlen))) ||
7651974Sbrutus 	    so->so_nl7c_flags == NL7C_AF_NCA)) {
7660Sstevel@tonic-gate 		/*
7670Sstevel@tonic-gate 		 * NL7C is not supported in non-global zones,
7680Sstevel@tonic-gate 		 * we enforce this restriction here.
7690Sstevel@tonic-gate 		 */
7700Sstevel@tonic-gate 		if (so->so_zoneid == GLOBAL_ZONEID) {
7710Sstevel@tonic-gate 			/* An NL7C socket, mark it */
7720Sstevel@tonic-gate 			so->so_nl7c_flags |= NL7C_ENABLED;
7731974Sbrutus 			if (nl7c == NULL) {
7741974Sbrutus 				/*
7751974Sbrutus 				 * Was an AF_NCA bind() so add it to the
7761974Sbrutus 				 * addr list for reporting purposes.
7771974Sbrutus 				 */
7781974Sbrutus 				nl7c = nl7c_add_addr(addr, addrlen);
7791974Sbrutus 			}
7800Sstevel@tonic-gate 		} else
7810Sstevel@tonic-gate 			nl7c = NULL;
7820Sstevel@tonic-gate 	}
7830Sstevel@tonic-gate 	/*
7840Sstevel@tonic-gate 	 * We send a T_BIND_REQ for TCP/UDP since we know it supports it,
7850Sstevel@tonic-gate 	 * for other transports we will send in a O_T_BIND_REQ.
7860Sstevel@tonic-gate 	 */
7870Sstevel@tonic-gate 	if (tcp_udp_xport &&
7880Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6))
7890Sstevel@tonic-gate 		PRIM_type = T_BIND_REQ;
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	bind_req.PRIM_type = PRIM_type;
7920Sstevel@tonic-gate 	bind_req.ADDR_length = addrlen;
7930Sstevel@tonic-gate 	bind_req.ADDR_offset = (t_scalar_t)sizeof (bind_req);
7940Sstevel@tonic-gate 	bind_req.CONIND_number = backlog;
7950Sstevel@tonic-gate 	/* NOTE: holding so_lock while sleeping */
7960Sstevel@tonic-gate 	mp = soallocproto2(&bind_req, sizeof (bind_req),
7970Sstevel@tonic-gate 				addr, addrlen, 0, _ALLOC_SLEEP);
7980Sstevel@tonic-gate 	so->so_state &= ~SS_LADDR_VALID;
799898Skais 
8000Sstevel@tonic-gate 	/* Done using so_laddr_sa - can drop the lock */
8010Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
8020Sstevel@tonic-gate 
803898Skais 	/*
804898Skais 	 * Intercept the bind_req message here to check if this <address/port>
805898Skais 	 * was configured as an SSL proxy server, or if another endpoint was
806898Skais 	 * already configured to act as a proxy for us.
8071974Sbrutus 	 *
8081974Sbrutus 	 * Note, only if NL7C not enabled for this socket.
809898Skais 	 */
8101974Sbrutus 	if (nl7c == NULL &&
8111974Sbrutus 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
812898Skais 	    so->so_type == SOCK_STREAM) {
813898Skais 
814898Skais 		if (so->so_kssl_ent != NULL) {
815898Skais 			kssl_release_ent(so->so_kssl_ent, so, so->so_kssl_type);
816898Skais 			so->so_kssl_ent = NULL;
817898Skais 		}
818898Skais 
819898Skais 		so->so_kssl_type = kssl_check_proxy(mp, so, &so->so_kssl_ent);
820898Skais 		switch (so->so_kssl_type) {
821898Skais 		case KSSL_NO_PROXY:
822898Skais 			break;
823898Skais 
824898Skais 		case KSSL_HAS_PROXY:
825898Skais 			mutex_enter(&so->so_lock);
826898Skais 			goto skip_transport;
827898Skais 
828898Skais 		case KSSL_IS_PROXY:
829898Skais 			break;
830898Skais 		}
831898Skais 	}
832898Skais 
8330Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
8340Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
8350Sstevel@tonic-gate 	if (error) {
8360Sstevel@tonic-gate 		eprintsoline(so, error);
8370Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
8380Sstevel@tonic-gate 		goto done;
8390Sstevel@tonic-gate 	}
8400Sstevel@tonic-gate 
8410Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
8420Sstevel@tonic-gate 	error = sowaitprim(so, PRIM_type, T_BIND_ACK,
8430Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (*bind_ack), &mp, 0);
8440Sstevel@tonic-gate 	if (error) {
8450Sstevel@tonic-gate 		eprintsoline(so, error);
8460Sstevel@tonic-gate 		goto done;
8470Sstevel@tonic-gate 	}
848898Skais skip_transport:
8490Sstevel@tonic-gate 	ASSERT(mp);
8500Sstevel@tonic-gate 	/*
8510Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
8520Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the bind
8530Sstevel@tonic-gate 	 * is allowed to complete.
8540Sstevel@tonic-gate 	 */
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	/* Mark as bound. This will be undone if we detect errors below. */
8570Sstevel@tonic-gate 	if (flags & _SOBIND_NOXLATE) {
8580Sstevel@tonic-gate 		ASSERT(so->so_family == AF_UNIX);
8590Sstevel@tonic-gate 		so->so_state |= SS_FADDR_NOXLATE;
8600Sstevel@tonic-gate 	}
8610Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_ISBOUND) || (flags & _SOBIND_REBIND));
8620Sstevel@tonic-gate 	so->so_state |= SS_ISBOUND;
8630Sstevel@tonic-gate 	ASSERT(so->so_unbind_mp);
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate 	/* note that we've already set SS_ACCEPTCONN above */
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 	/*
8680Sstevel@tonic-gate 	 * Recompute addrlen - an unspecied bind sent down an
8690Sstevel@tonic-gate 	 * address of length zero but we expect the appropriate length
8700Sstevel@tonic-gate 	 * in return.
8710Sstevel@tonic-gate 	 */
8720Sstevel@tonic-gate 	addrlen = (t_uscalar_t)(so->so_family == AF_UNIX ?
8730Sstevel@tonic-gate 	    sizeof (so->so_ux_laddr) : so->so_laddr_len);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	bind_ack = (struct T_bind_ack *)mp->b_rptr;
8760Sstevel@tonic-gate 	/*
8770Sstevel@tonic-gate 	 * The alignment restriction is really too strict but
8780Sstevel@tonic-gate 	 * we want enough alignment to inspect the fields of
8790Sstevel@tonic-gate 	 * a sockaddr_in.
8800Sstevel@tonic-gate 	 */
8810Sstevel@tonic-gate 	addr = sogetoff(mp, bind_ack->ADDR_offset,
8820Sstevel@tonic-gate 			bind_ack->ADDR_length,
8830Sstevel@tonic-gate 			__TPI_ALIGN_SIZE);
8840Sstevel@tonic-gate 	if (addr == NULL) {
8850Sstevel@tonic-gate 		freemsg(mp);
8860Sstevel@tonic-gate 		error = EPROTO;
8870Sstevel@tonic-gate 		eprintsoline(so, error);
8880Sstevel@tonic-gate 		goto done;
8890Sstevel@tonic-gate 	}
8900Sstevel@tonic-gate 	if (!(flags & _SOBIND_UNSPEC)) {
8910Sstevel@tonic-gate 		/*
8920Sstevel@tonic-gate 		 * Verify that the transport didn't return something we
8930Sstevel@tonic-gate 		 * did not want e.g. an address other than what we asked for.
8940Sstevel@tonic-gate 		 *
8950Sstevel@tonic-gate 		 * NOTE: These checks would go away if/when we switch to
8960Sstevel@tonic-gate 		 * using the new TPI (in which the transport would fail
8970Sstevel@tonic-gate 		 * the request instead of assigning a different address).
8980Sstevel@tonic-gate 		 *
8990Sstevel@tonic-gate 		 * NOTE2: For protocols that we don't know (i.e. any
9000Sstevel@tonic-gate 		 * other than AF_INET6, AF_INET and AF_UNIX), we
9010Sstevel@tonic-gate 		 * cannot know if the transport should be expected to
9020Sstevel@tonic-gate 		 * return the same address as that requested.
9030Sstevel@tonic-gate 		 *
9040Sstevel@tonic-gate 		 * NOTE3: For AF_INET and AF_INET6, TCP/UDP, we send
9050Sstevel@tonic-gate 		 * down a T_BIND_REQ. We use O_T_BIND_REQ for others.
9060Sstevel@tonic-gate 		 *
9070Sstevel@tonic-gate 		 * For example, in the case of netatalk it may be
9080Sstevel@tonic-gate 		 * inappropriate for the transport to return the
9090Sstevel@tonic-gate 		 * requested address (as it may have allocated a local
9100Sstevel@tonic-gate 		 * port number in behaviour similar to that of an
9110Sstevel@tonic-gate 		 * AF_INET bind request with a port number of zero).
9120Sstevel@tonic-gate 		 *
9130Sstevel@tonic-gate 		 * Given the definition of O_T_BIND_REQ, where the
9140Sstevel@tonic-gate 		 * transport may bind to an address other than the
9150Sstevel@tonic-gate 		 * requested address, it's not possible to determine
9160Sstevel@tonic-gate 		 * whether a returned address that differs from the
9170Sstevel@tonic-gate 		 * requested address is a reason to fail (because the
9180Sstevel@tonic-gate 		 * requested address was not available) or succeed
9190Sstevel@tonic-gate 		 * (because the transport allocated an appropriate
9200Sstevel@tonic-gate 		 * address and/or port).
9210Sstevel@tonic-gate 		 *
9220Sstevel@tonic-gate 		 * sockfs currently requires that the transport return
9230Sstevel@tonic-gate 		 * the requested address in the T_BIND_ACK, unless
9240Sstevel@tonic-gate 		 * there is code here to allow for any discrepancy.
9250Sstevel@tonic-gate 		 * Such code exists for AF_INET and AF_INET6.
9260Sstevel@tonic-gate 		 *
9270Sstevel@tonic-gate 		 * Netatalk chooses to return the requested address
9280Sstevel@tonic-gate 		 * rather than the (correct) allocated address.  This
9290Sstevel@tonic-gate 		 * means that netatalk violates the TPI specification
9300Sstevel@tonic-gate 		 * (and would not function correctly if used from a
9310Sstevel@tonic-gate 		 * TLI application), but it does mean that it works
9320Sstevel@tonic-gate 		 * with sockfs.
9330Sstevel@tonic-gate 		 *
9340Sstevel@tonic-gate 		 * As noted above, using the newer XTI bind primitive
9350Sstevel@tonic-gate 		 * (T_BIND_REQ) in preference to O_T_BIND_REQ would
9360Sstevel@tonic-gate 		 * allow sockfs to be more sure about whether or not
9370Sstevel@tonic-gate 		 * the bind request had succeeded (as transports are
9380Sstevel@tonic-gate 		 * not permitted to bind to a different address than
9390Sstevel@tonic-gate 		 * that requested - they must return failure).
9400Sstevel@tonic-gate 		 * Unfortunately, support for T_BIND_REQ may not be
9410Sstevel@tonic-gate 		 * present in all transport implementations (netatalk,
9420Sstevel@tonic-gate 		 * for example, doesn't have it), making the
9430Sstevel@tonic-gate 		 * transition difficult.
9440Sstevel@tonic-gate 		 */
9450Sstevel@tonic-gate 		if (bind_ack->ADDR_length != addrlen) {
9460Sstevel@tonic-gate 			/* Assumes that the requested address was in use */
9470Sstevel@tonic-gate 			freemsg(mp);
9480Sstevel@tonic-gate 			error = EADDRINUSE;
9490Sstevel@tonic-gate 			eprintsoline(so, error);
9500Sstevel@tonic-gate 			goto done;
9510Sstevel@tonic-gate 		}
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 		switch (so->so_family) {
9540Sstevel@tonic-gate 		case AF_INET6:
9550Sstevel@tonic-gate 		case AF_INET: {
9560Sstevel@tonic-gate 			sin_t *rname, *aname;
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 			rname = (sin_t *)addr;
9590Sstevel@tonic-gate 			aname = (sin_t *)so->so_laddr_sa;
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 			/*
9620Sstevel@tonic-gate 			 * Take advantage of the alignment
9630Sstevel@tonic-gate 			 * of sin_port and sin6_port which fall
9640Sstevel@tonic-gate 			 * in the same place in their data structures.
9650Sstevel@tonic-gate 			 * Just use sin_port for either address family.
9660Sstevel@tonic-gate 			 *
9670Sstevel@tonic-gate 			 * This may become a problem if (heaven forbid)
9680Sstevel@tonic-gate 			 * there's a separate ipv6port_reserved... :-P
9690Sstevel@tonic-gate 			 *
9700Sstevel@tonic-gate 			 * Binding to port 0 has the semantics of letting
9710Sstevel@tonic-gate 			 * the transport bind to any port.
9720Sstevel@tonic-gate 			 *
9730Sstevel@tonic-gate 			 * If the transport is TCP or UDP since we had sent
9740Sstevel@tonic-gate 			 * a T_BIND_REQ we would not get a port other than
9750Sstevel@tonic-gate 			 * what we asked for.
9760Sstevel@tonic-gate 			 */
9770Sstevel@tonic-gate 			if (tcp_udp_xport) {
9780Sstevel@tonic-gate 				/*
9790Sstevel@tonic-gate 				 * Pick up the new port number if we bound to
9800Sstevel@tonic-gate 				 * port 0.
9810Sstevel@tonic-gate 				 */
9820Sstevel@tonic-gate 				if (aname->sin_port == 0)
9830Sstevel@tonic-gate 					aname->sin_port = rname->sin_port;
9840Sstevel@tonic-gate 				so->so_state |= SS_LADDR_VALID;
9850Sstevel@tonic-gate 				break;
9860Sstevel@tonic-gate 			}
9870Sstevel@tonic-gate 			if (aname->sin_port != 0 &&
9880Sstevel@tonic-gate 			    aname->sin_port != rname->sin_port) {
9890Sstevel@tonic-gate 				freemsg(mp);
9900Sstevel@tonic-gate 				error = EADDRINUSE;
9910Sstevel@tonic-gate 				eprintsoline(so, error);
9920Sstevel@tonic-gate 				goto done;
9930Sstevel@tonic-gate 			}
9940Sstevel@tonic-gate 			/*
9950Sstevel@tonic-gate 			 * Pick up the new port number if we bound to port 0.
9960Sstevel@tonic-gate 			 */
9970Sstevel@tonic-gate 			aname->sin_port = rname->sin_port;
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 			/*
10000Sstevel@tonic-gate 			 * Unfortunately, addresses aren't _quite_ the same.
10010Sstevel@tonic-gate 			 */
10020Sstevel@tonic-gate 			if (so->so_family == AF_INET) {
10030Sstevel@tonic-gate 				if (aname->sin_addr.s_addr !=
10040Sstevel@tonic-gate 				    rname->sin_addr.s_addr) {
10050Sstevel@tonic-gate 					freemsg(mp);
10060Sstevel@tonic-gate 					error = EADDRNOTAVAIL;
10070Sstevel@tonic-gate 					eprintsoline(so, error);
10080Sstevel@tonic-gate 					goto done;
10090Sstevel@tonic-gate 				}
10100Sstevel@tonic-gate 			} else {
10110Sstevel@tonic-gate 				sin6_t *rname6 = (sin6_t *)rname;
10120Sstevel@tonic-gate 				sin6_t *aname6 = (sin6_t *)aname;
10130Sstevel@tonic-gate 
10140Sstevel@tonic-gate 				if (!IN6_ARE_ADDR_EQUAL(&aname6->sin6_addr,
10150Sstevel@tonic-gate 				    &rname6->sin6_addr)) {
10160Sstevel@tonic-gate 					freemsg(mp);
10170Sstevel@tonic-gate 					error = EADDRNOTAVAIL;
10180Sstevel@tonic-gate 					eprintsoline(so, error);
10190Sstevel@tonic-gate 					goto done;
10200Sstevel@tonic-gate 				}
10210Sstevel@tonic-gate 			}
10220Sstevel@tonic-gate 			break;
10230Sstevel@tonic-gate 		}
10240Sstevel@tonic-gate 		case AF_UNIX:
10250Sstevel@tonic-gate 			if (bcmp(addr, &so->so_ux_laddr, addrlen) != 0) {
10260Sstevel@tonic-gate 				freemsg(mp);
10270Sstevel@tonic-gate 				error = EADDRINUSE;
10280Sstevel@tonic-gate 				eprintsoline(so, error);
10290Sstevel@tonic-gate 				eprintso(so,
10300Sstevel@tonic-gate 					("addrlen %d, addr 0x%x, vp %p\n",
10310Sstevel@tonic-gate 					addrlen, *((int *)addr),
10320Sstevel@tonic-gate 					so->so_ux_bound_vp));
10330Sstevel@tonic-gate 				goto done;
10340Sstevel@tonic-gate 			}
10350Sstevel@tonic-gate 			so->so_state |= SS_LADDR_VALID;
10360Sstevel@tonic-gate 			break;
10370Sstevel@tonic-gate 		default:
10380Sstevel@tonic-gate 			/*
10390Sstevel@tonic-gate 			 * NOTE: This assumes that addresses can be
10400Sstevel@tonic-gate 			 * byte-compared for equivalence.
10410Sstevel@tonic-gate 			 */
10420Sstevel@tonic-gate 			if (bcmp(addr, so->so_laddr_sa, addrlen) != 0) {
10430Sstevel@tonic-gate 				freemsg(mp);
10440Sstevel@tonic-gate 				error = EADDRINUSE;
10450Sstevel@tonic-gate 				eprintsoline(so, error);
10460Sstevel@tonic-gate 				goto done;
10470Sstevel@tonic-gate 			}
10480Sstevel@tonic-gate 			/*
10490Sstevel@tonic-gate 			 * Don't mark SS_LADDR_VALID, as we cannot be
10500Sstevel@tonic-gate 			 * sure that the returned address is the real
10510Sstevel@tonic-gate 			 * bound address when talking to an unknown
10520Sstevel@tonic-gate 			 * transport.
10530Sstevel@tonic-gate 			 */
10540Sstevel@tonic-gate 			break;
10550Sstevel@tonic-gate 		}
10560Sstevel@tonic-gate 	} else {
10570Sstevel@tonic-gate 		/*
10580Sstevel@tonic-gate 		 * Save for returned address for getsockname.
10590Sstevel@tonic-gate 		 * Needed for unspecific bind unless transport supports
10600Sstevel@tonic-gate 		 * the TI_GETMYNAME ioctl.
10610Sstevel@tonic-gate 		 * Do this for AF_INET{,6} even though they do, as
10620Sstevel@tonic-gate 		 * caching info here is much better performance than
10630Sstevel@tonic-gate 		 * a TPI/STREAMS trip to the transport for getsockname.
10640Sstevel@tonic-gate 		 * Any which can't for some reason _must_ _not_ set
10650Sstevel@tonic-gate 		 * LADDR_VALID here for the caching version of getsockname
10660Sstevel@tonic-gate 		 * to not break;
10670Sstevel@tonic-gate 		 */
10680Sstevel@tonic-gate 		switch (so->so_family) {
10690Sstevel@tonic-gate 		case AF_UNIX:
10700Sstevel@tonic-gate 			/*
10710Sstevel@tonic-gate 			 * Record the address bound with the transport
10720Sstevel@tonic-gate 			 * for use by socketpair.
10730Sstevel@tonic-gate 			 */
10740Sstevel@tonic-gate 			bcopy(addr, &so->so_ux_laddr, addrlen);
10750Sstevel@tonic-gate 			so->so_state |= SS_LADDR_VALID;
10760Sstevel@tonic-gate 			break;
10770Sstevel@tonic-gate 		case AF_INET:
10780Sstevel@tonic-gate 		case AF_INET6:
10790Sstevel@tonic-gate 			ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
10800Sstevel@tonic-gate 			bcopy(addr, so->so_laddr_sa, so->so_laddr_len);
10810Sstevel@tonic-gate 			so->so_state |= SS_LADDR_VALID;
10820Sstevel@tonic-gate 			break;
10830Sstevel@tonic-gate 		default:
10840Sstevel@tonic-gate 			/*
10850Sstevel@tonic-gate 			 * Don't mark SS_LADDR_VALID, as we cannot be
10860Sstevel@tonic-gate 			 * sure that the returned address is the real
10870Sstevel@tonic-gate 			 * bound address when talking to an unknown
10880Sstevel@tonic-gate 			 * transport.
10890Sstevel@tonic-gate 			 */
10900Sstevel@tonic-gate 			break;
10910Sstevel@tonic-gate 		}
10920Sstevel@tonic-gate 	}
10930Sstevel@tonic-gate 
10940Sstevel@tonic-gate 	if (nl7c != NULL) {
10951974Sbrutus 		/* Register listen()er sonode pointer with NL7C */
10961974Sbrutus 		nl7c_listener_addr(nl7c, so);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate 
10990Sstevel@tonic-gate 	freemsg(mp);
11000Sstevel@tonic-gate 
11010Sstevel@tonic-gate done:
11020Sstevel@tonic-gate 	if (error) {
11030Sstevel@tonic-gate 		/* reset state & backlog to values held on entry */
11040Sstevel@tonic-gate 		if (clear_acceptconn_on_err == B_TRUE)
11050Sstevel@tonic-gate 			so->so_state &= ~SS_ACCEPTCONN;
11060Sstevel@tonic-gate 		if (restore_backlog_on_err == B_TRUE)
11070Sstevel@tonic-gate 			so->so_backlog = save_so_backlog;
11080Sstevel@tonic-gate 
11090Sstevel@tonic-gate 		if (unbind_on_err && so->so_state & SS_ISBOUND) {
11100Sstevel@tonic-gate 			int err;
11110Sstevel@tonic-gate 
11120Sstevel@tonic-gate 			err = sotpi_unbind(so, 0);
11130Sstevel@tonic-gate 			/* LINTED - statement has no consequent: if */
11140Sstevel@tonic-gate 			if (err) {
11150Sstevel@tonic-gate 				eprintsoline(so, error);
11160Sstevel@tonic-gate 			} else {
11170Sstevel@tonic-gate 				ASSERT(!(so->so_state & SS_ISBOUND));
11180Sstevel@tonic-gate 			}
11190Sstevel@tonic-gate 		}
11200Sstevel@tonic-gate 	}
11210Sstevel@tonic-gate 	if (!(flags & _SOBIND_LOCK_HELD)) {
11220Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
11230Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
11240Sstevel@tonic-gate 	} else {
11250Sstevel@tonic-gate 		/* If the caller held the lock don't release it here */
11260Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
11270Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
11280Sstevel@tonic-gate 	}
11290Sstevel@tonic-gate 	return (error);
11300Sstevel@tonic-gate }
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate /* bind the socket */
1133741Smasputra static int
11340Sstevel@tonic-gate sotpi_bind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
11350Sstevel@tonic-gate     int flags)
11360Sstevel@tonic-gate {
11370Sstevel@tonic-gate 	if ((flags & _SOBIND_SOCKETPAIR) == 0)
11380Sstevel@tonic-gate 		return (sotpi_bindlisten(so, name, namelen, 0, flags));
11390Sstevel@tonic-gate 
11400Sstevel@tonic-gate 	flags &= ~_SOBIND_SOCKETPAIR;
11410Sstevel@tonic-gate 	return (sotpi_bindlisten(so, name, namelen, 1, flags));
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate  * Unbind a socket - used when bind() fails, when bind() specifies a NULL
11460Sstevel@tonic-gate  * address, or when listen needs to unbind and bind.
11470Sstevel@tonic-gate  * If the _SOUNBIND_REBIND flag is specified the addresses are retained
11480Sstevel@tonic-gate  * so that a sobind can pick them up.
11490Sstevel@tonic-gate  */
11500Sstevel@tonic-gate static int
11510Sstevel@tonic-gate sotpi_unbind(struct sonode *so, int flags)
11520Sstevel@tonic-gate {
11530Sstevel@tonic-gate 	struct T_unbind_req	unbind_req;
11540Sstevel@tonic-gate 	int			error = 0;
11550Sstevel@tonic-gate 	mblk_t			*mp;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_unbind(%p, 0x%x) %s\n",
11580Sstevel@tonic-gate 			so, flags, pr_state(so->so_state, so->so_mode)));
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
11610Sstevel@tonic-gate 	ASSERT(so->so_flag & SOLOCKED);
11620Sstevel@tonic-gate 
11630Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
11640Sstevel@tonic-gate 		error = EINVAL;
11650Sstevel@tonic-gate 		eprintsoline(so, error);
11660Sstevel@tonic-gate 		goto done;
11670Sstevel@tonic-gate 	}
11680Sstevel@tonic-gate 
11690Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate 	/*
11720Sstevel@tonic-gate 	 * Flush the read and write side (except stream head read queue)
11730Sstevel@tonic-gate 	 * and send down T_UNBIND_REQ.
11740Sstevel@tonic-gate 	 */
11750Sstevel@tonic-gate 	(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHRW);
11760Sstevel@tonic-gate 
11770Sstevel@tonic-gate 	unbind_req.PRIM_type = T_UNBIND_REQ;
11780Sstevel@tonic-gate 	mp = soallocproto1(&unbind_req, sizeof (unbind_req),
11790Sstevel@tonic-gate 	    0, _ALLOC_SLEEP);
11800Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
11810Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
11820Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
11830Sstevel@tonic-gate 	if (error) {
11840Sstevel@tonic-gate 		eprintsoline(so, error);
11850Sstevel@tonic-gate 		goto done;
11860Sstevel@tonic-gate 	}
11870Sstevel@tonic-gate 
11880Sstevel@tonic-gate 	error = sowaitokack(so, T_UNBIND_REQ);
11890Sstevel@tonic-gate 	if (error) {
11900Sstevel@tonic-gate 		eprintsoline(so, error);
11910Sstevel@tonic-gate 		goto done;
11920Sstevel@tonic-gate 	}
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	/*
11950Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
11960Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the unbind
11970Sstevel@tonic-gate 	 * is allowed to complete.
11980Sstevel@tonic-gate 	 */
11990Sstevel@tonic-gate 	if (!(flags & _SOUNBIND_REBIND)) {
12000Sstevel@tonic-gate 		/*
12010Sstevel@tonic-gate 		 * Clear out bound address.
12020Sstevel@tonic-gate 		 */
12030Sstevel@tonic-gate 		vnode_t *vp;
12040Sstevel@tonic-gate 
12050Sstevel@tonic-gate 		if ((vp = so->so_ux_bound_vp) != NULL) {
1206898Skais 
1207898Skais 			/* Undo any SSL proxy setup */
1208898Skais 			if ((so->so_family == AF_INET ||
1209898Skais 			    so->so_family == AF_INET6) &&
1210898Skais 			    (so->so_type == SOCK_STREAM) &&
1211898Skais 			    (so->so_kssl_ent != NULL)) {
1212898Skais 				kssl_release_ent(so->so_kssl_ent, so,
1213898Skais 				    so->so_kssl_type);
1214898Skais 				so->so_kssl_ent = NULL;
1215898Skais 				so->so_kssl_type = KSSL_NO_PROXY;
1216898Skais 			}
1217898Skais 
12180Sstevel@tonic-gate 			so->so_ux_bound_vp = NULL;
12190Sstevel@tonic-gate 			vn_rele_stream(vp);
12200Sstevel@tonic-gate 		}
12210Sstevel@tonic-gate 		/* Clear out address */
12220Sstevel@tonic-gate 		so->so_laddr_len = 0;
12230Sstevel@tonic-gate 	}
12240Sstevel@tonic-gate 	so->so_state &= ~(SS_ISBOUND|SS_ACCEPTCONN|SS_LADDR_VALID);
12251974Sbrutus 
12260Sstevel@tonic-gate done:
1227898Skais 
12280Sstevel@tonic-gate 	/* If the caller held the lock don't release it here */
12290Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
12300Sstevel@tonic-gate 	ASSERT(so->so_flag & SOLOCKED);
12310Sstevel@tonic-gate 
12320Sstevel@tonic-gate 	return (error);
12330Sstevel@tonic-gate }
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate /*
12360Sstevel@tonic-gate  * listen on the socket.
12370Sstevel@tonic-gate  * For TPI conforming transports this has to first unbind with the transport
12380Sstevel@tonic-gate  * and then bind again using the new backlog.
12390Sstevel@tonic-gate  */
12400Sstevel@tonic-gate int
12410Sstevel@tonic-gate sotpi_listen(struct sonode *so, int backlog)
12420Sstevel@tonic-gate {
12430Sstevel@tonic-gate 	int		error = 0;
12440Sstevel@tonic-gate 
12450Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_listen(%p, %d) %s\n",
12460Sstevel@tonic-gate 		so, backlog, pr_state(so->so_state, so->so_mode)));
12470Sstevel@tonic-gate 
12480Sstevel@tonic-gate 	if (so->so_serv_type == T_CLTS)
12490Sstevel@tonic-gate 		return (EOPNOTSUPP);
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	/*
12520Sstevel@tonic-gate 	 * If the socket is ready to accept connections already, then
12530Sstevel@tonic-gate 	 * return without doing anything.  This avoids a problem where
12540Sstevel@tonic-gate 	 * a second listen() call fails if a connection is pending and
12550Sstevel@tonic-gate 	 * leaves the socket unbound. Only when we are not unbinding
12560Sstevel@tonic-gate 	 * with the transport can we safely increase the backlog.
12570Sstevel@tonic-gate 	 */
12580Sstevel@tonic-gate 	if (so->so_state & SS_ACCEPTCONN &&
12590Sstevel@tonic-gate 	    !((so->so_family == AF_INET || so->so_family == AF_INET6) &&
12600Sstevel@tonic-gate 		/*CONSTCOND*/
12610Sstevel@tonic-gate 		!solisten_tpi_tcp))
12620Sstevel@tonic-gate 		return (0);
12630Sstevel@tonic-gate 
12640Sstevel@tonic-gate 	if (so->so_state & SS_ISCONNECTED)
12650Sstevel@tonic-gate 		return (EINVAL);
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
12680Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
12690Sstevel@tonic-gate 
12700Sstevel@tonic-gate 	if (backlog < 0)
12710Sstevel@tonic-gate 		backlog = 0;
12720Sstevel@tonic-gate 	/*
12730Sstevel@tonic-gate 	 * Use the same qlimit as in BSD. BSD checks the qlimit
12740Sstevel@tonic-gate 	 * before queuing the next connection implying that a
12750Sstevel@tonic-gate 	 * listen(sock, 0) allows one connection to be queued.
12760Sstevel@tonic-gate 	 * BSD also uses 1.5 times the requested backlog.
12770Sstevel@tonic-gate 	 *
12780Sstevel@tonic-gate 	 * XNS Issue 4 required a strict interpretation of the backlog.
12790Sstevel@tonic-gate 	 * This has been waived subsequently for Issue 4 and the change
12800Sstevel@tonic-gate 	 * incorporated in XNS Issue 5. So we aren't required to do
12810Sstevel@tonic-gate 	 * anything special for XPG apps.
12820Sstevel@tonic-gate 	 */
12830Sstevel@tonic-gate 	if (backlog >= (INT_MAX - 1) / 3)
12840Sstevel@tonic-gate 		backlog = INT_MAX;
12850Sstevel@tonic-gate 	else
12860Sstevel@tonic-gate 		backlog = backlog * 3 / 2 + 1;
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 	/*
12890Sstevel@tonic-gate 	 * If the listen doesn't change the backlog we do nothing.
12900Sstevel@tonic-gate 	 * This avoids an EPROTO error from the transport.
12910Sstevel@tonic-gate 	 */
12920Sstevel@tonic-gate 	if ((so->so_state & SS_ACCEPTCONN) &&
12930Sstevel@tonic-gate 	    so->so_backlog == backlog)
12940Sstevel@tonic-gate 		goto done;
12950Sstevel@tonic-gate 
12960Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
12970Sstevel@tonic-gate 		/*
12980Sstevel@tonic-gate 		 * Must have been explicitly bound in the UNIX domain.
12990Sstevel@tonic-gate 		 */
13000Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
13010Sstevel@tonic-gate 			error = EINVAL;
13020Sstevel@tonic-gate 			goto done;
13030Sstevel@tonic-gate 		}
13040Sstevel@tonic-gate 		error = sotpi_bindlisten(so, NULL, 0, backlog,
13050Sstevel@tonic-gate 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD|_SOBIND_LISTEN);
13060Sstevel@tonic-gate 	} else if (backlog > 0) {
13070Sstevel@tonic-gate 		/*
13080Sstevel@tonic-gate 		 * AF_INET{,6} hack to avoid losing the port.
13090Sstevel@tonic-gate 		 * Assumes that all AF_INET{,6} transports can handle a
13100Sstevel@tonic-gate 		 * O_T_BIND_REQ with a non-zero CONIND_number when the TPI
13110Sstevel@tonic-gate 		 * has already bound thus it is possible to avoid the unbind.
13120Sstevel@tonic-gate 		 */
13130Sstevel@tonic-gate 		if (!((so->so_family == AF_INET || so->so_family == AF_INET6) &&
13140Sstevel@tonic-gate 		    /*CONSTCOND*/
13150Sstevel@tonic-gate 		    !solisten_tpi_tcp)) {
13160Sstevel@tonic-gate 			error = sotpi_unbind(so, _SOUNBIND_REBIND);
13170Sstevel@tonic-gate 			if (error)
13180Sstevel@tonic-gate 				goto done;
13190Sstevel@tonic-gate 		}
13200Sstevel@tonic-gate 		error = sotpi_bindlisten(so, NULL, 0, backlog,
13210Sstevel@tonic-gate 			    _SOBIND_REBIND|_SOBIND_LOCK_HELD|_SOBIND_LISTEN);
13220Sstevel@tonic-gate 	} else {
13230Sstevel@tonic-gate 		so->so_state |= SS_ACCEPTCONN;
13240Sstevel@tonic-gate 		so->so_backlog = backlog;
13250Sstevel@tonic-gate 	}
13260Sstevel@tonic-gate 	if (error)
13270Sstevel@tonic-gate 		goto done;
13280Sstevel@tonic-gate 	ASSERT(so->so_state & SS_ACCEPTCONN);
13290Sstevel@tonic-gate done:
13300Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
13310Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
13320Sstevel@tonic-gate 	return (error);
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate /*
13360Sstevel@tonic-gate  * Disconnect either a specified seqno or all (-1).
13370Sstevel@tonic-gate  * The former is used on listening sockets only.
13380Sstevel@tonic-gate  *
13390Sstevel@tonic-gate  * When seqno == -1 sodisconnect could call sotpi_unbind. However,
13400Sstevel@tonic-gate  * the current use of sodisconnect(seqno == -1) is only for shutdown
13410Sstevel@tonic-gate  * so there is no point (and potentially incorrect) to unbind.
13420Sstevel@tonic-gate  */
13430Sstevel@tonic-gate int
13440Sstevel@tonic-gate sodisconnect(struct sonode *so, t_scalar_t seqno, int flags)
13450Sstevel@tonic-gate {
13460Sstevel@tonic-gate 	struct T_discon_req	discon_req;
13470Sstevel@tonic-gate 	int			error = 0;
13480Sstevel@tonic-gate 	mblk_t			*mp;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 	dprintso(so, 1, ("sodisconnect(%p, %d, 0x%x) %s\n",
13510Sstevel@tonic-gate 			so, seqno, flags, pr_state(so->so_state, so->so_mode)));
13520Sstevel@tonic-gate 
13530Sstevel@tonic-gate 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
13540Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
13550Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
13560Sstevel@tonic-gate 	} else {
13570Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
13580Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	if (!(so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING|SS_ACCEPTCONN))) {
13620Sstevel@tonic-gate 		error = EINVAL;
13630Sstevel@tonic-gate 		eprintsoline(so, error);
13640Sstevel@tonic-gate 		goto done;
13650Sstevel@tonic-gate 	}
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
13680Sstevel@tonic-gate 	/*
13690Sstevel@tonic-gate 	 * Flush the write side (unless this is a listener)
13700Sstevel@tonic-gate 	 * and then send down a T_DISCON_REQ.
13710Sstevel@tonic-gate 	 * (Don't flush on listener since it could flush {O_}T_CONN_RES
13720Sstevel@tonic-gate 	 * and other messages.)
13730Sstevel@tonic-gate 	 */
13740Sstevel@tonic-gate 	if (!(so->so_state & SS_ACCEPTCONN))
13750Sstevel@tonic-gate 		(void) putnextctl1(strvp2wq(SOTOV(so)), M_FLUSH, FLUSHW);
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 	discon_req.PRIM_type = T_DISCON_REQ;
13780Sstevel@tonic-gate 	discon_req.SEQ_number = seqno;
13790Sstevel@tonic-gate 	mp = soallocproto1(&discon_req, sizeof (discon_req),
13800Sstevel@tonic-gate 	    0, _ALLOC_SLEEP);
13810Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
13820Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
13830Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
13840Sstevel@tonic-gate 	if (error) {
13850Sstevel@tonic-gate 		eprintsoline(so, error);
13860Sstevel@tonic-gate 		goto done;
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	error = sowaitokack(so, T_DISCON_REQ);
13900Sstevel@tonic-gate 	if (error) {
13910Sstevel@tonic-gate 		eprintsoline(so, error);
13920Sstevel@tonic-gate 		goto done;
13930Sstevel@tonic-gate 	}
13940Sstevel@tonic-gate 	/*
13950Sstevel@tonic-gate 	 * Even if some TPI message (e.g. T_DISCON_IND) was received in
13960Sstevel@tonic-gate 	 * strsock_proto while the lock was dropped above, the disconnect
13970Sstevel@tonic-gate 	 * is allowed to complete. However, it is not possible to
13980Sstevel@tonic-gate 	 * assert that SS_ISCONNECTED|SS_ISCONNECTING are set.
13990Sstevel@tonic-gate 	 */
14000Sstevel@tonic-gate 	so->so_state &=
14010Sstevel@tonic-gate 	    ~(SS_ISCONNECTED|SS_ISCONNECTING|SS_LADDR_VALID|SS_FADDR_VALID);
14020Sstevel@tonic-gate done:
14030Sstevel@tonic-gate 	if (!(flags & _SODISCONNECT_LOCK_HELD)) {
14040Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
14050Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
14060Sstevel@tonic-gate 	} else {
14070Sstevel@tonic-gate 		/* If the caller held the lock don't release it here */
14080Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&so->so_lock));
14090Sstevel@tonic-gate 		ASSERT(so->so_flag & SOLOCKED);
14100Sstevel@tonic-gate 	}
14110Sstevel@tonic-gate 	return (error);
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate int
14150Sstevel@tonic-gate sotpi_accept(struct sonode *so, int fflag, struct sonode **nsop)
14160Sstevel@tonic-gate {
14170Sstevel@tonic-gate 	struct T_conn_ind	*conn_ind;
14180Sstevel@tonic-gate 	struct T_conn_res	*conn_res;
14190Sstevel@tonic-gate 	int			error = 0;
1420*4379Sja97890 	mblk_t			*mp, *ctxmp, *ack_mp;
14210Sstevel@tonic-gate 	struct sonode		*nso;
14220Sstevel@tonic-gate 	vnode_t			*nvp;
14230Sstevel@tonic-gate 	void			*src;
14240Sstevel@tonic-gate 	t_uscalar_t		srclen;
14250Sstevel@tonic-gate 	void			*opt;
14260Sstevel@tonic-gate 	t_uscalar_t		optlen;
14270Sstevel@tonic-gate 	t_scalar_t		PRIM_type;
14280Sstevel@tonic-gate 	t_scalar_t		SEQ_number;
1429*4379Sja97890 	size_t			sinlen;
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_accept(%p, 0x%x, %p) %s\n",
14320Sstevel@tonic-gate 		so, fflag, nsop, pr_state(so->so_state, so->so_mode)));
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	/*
14350Sstevel@tonic-gate 	 * Defer single-threading the accepting socket until
14360Sstevel@tonic-gate 	 * the T_CONN_IND has been received and parsed and the
14370Sstevel@tonic-gate 	 * new sonode has been opened.
14380Sstevel@tonic-gate 	 */
14390Sstevel@tonic-gate 
14400Sstevel@tonic-gate 	/* Check that we are not already connected */
14410Sstevel@tonic-gate 	if ((so->so_state & SS_ACCEPTCONN) == 0)
14420Sstevel@tonic-gate 		goto conn_bad;
14430Sstevel@tonic-gate again:
14440Sstevel@tonic-gate 	if ((error = sowaitconnind(so, fflag, &mp)) != 0)
14450Sstevel@tonic-gate 		goto e_bad;
14460Sstevel@tonic-gate 
14470Sstevel@tonic-gate 	ASSERT(mp);
14480Sstevel@tonic-gate 	conn_ind = (struct T_conn_ind *)mp->b_rptr;
1449898Skais 	ctxmp = mp->b_cont;
1450898Skais 
14510Sstevel@tonic-gate 	/*
14520Sstevel@tonic-gate 	 * Save SEQ_number for error paths.
14530Sstevel@tonic-gate 	 */
14540Sstevel@tonic-gate 	SEQ_number = conn_ind->SEQ_number;
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	srclen = conn_ind->SRC_length;
14570Sstevel@tonic-gate 	src = sogetoff(mp, conn_ind->SRC_offset, srclen, 1);
14580Sstevel@tonic-gate 	if (src == NULL) {
14590Sstevel@tonic-gate 		error = EPROTO;
14600Sstevel@tonic-gate 		freemsg(mp);
14610Sstevel@tonic-gate 		eprintsoline(so, error);
14620Sstevel@tonic-gate 		goto disconnect_unlocked;
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 	optlen = conn_ind->OPT_length;
14650Sstevel@tonic-gate 	switch (so->so_family) {
14660Sstevel@tonic-gate 	case AF_INET:
14670Sstevel@tonic-gate 	case AF_INET6:
14680Sstevel@tonic-gate 		if ((optlen == sizeof (intptr_t)) &&
1469741Smasputra 		    ((so->so_state & SS_DIRECT) != 0)) {
14700Sstevel@tonic-gate 			bcopy(mp->b_rptr + conn_ind->OPT_offset,
14710Sstevel@tonic-gate 			    &opt, conn_ind->OPT_length);
14720Sstevel@tonic-gate 		} else {
14730Sstevel@tonic-gate 			/*
14740Sstevel@tonic-gate 			 * The transport (in this case TCP) hasn't sent up
14750Sstevel@tonic-gate 			 * a pointer to an instance for the accept fast-path.
14760Sstevel@tonic-gate 			 * Disable fast-path completely because the call to
14770Sstevel@tonic-gate 			 * sotpi_create() below would otherwise create an
14780Sstevel@tonic-gate 			 * incomplete TCP instance, which would lead to
14790Sstevel@tonic-gate 			 * problems when sockfs sends a normal T_CONN_RES
14800Sstevel@tonic-gate 			 * message down the new stream.
14810Sstevel@tonic-gate 			 */
1482741Smasputra 			if (so->so_state & SS_DIRECT) {
1483741Smasputra 				int rval;
1484741Smasputra 				/*
1485741Smasputra 				 * For consistency we inform tcp to disable
1486741Smasputra 				 * direct interface on the listener, though
1487741Smasputra 				 * we can certainly live without doing this
1488741Smasputra 				 * because no data will ever travel upstream
1489741Smasputra 				 * on the listening socket.
1490741Smasputra 				 */
1491741Smasputra 				so->so_state &= ~SS_DIRECT;
1492741Smasputra 				(void) strioctl(SOTOV(so), _SIOCSOCKFALLBACK,
1493741Smasputra 				    0, 0, K_TO_K, CRED(), &rval);
1494741Smasputra 			}
14950Sstevel@tonic-gate 			opt = NULL;
14960Sstevel@tonic-gate 			optlen = 0;
14970Sstevel@tonic-gate 		}
14980Sstevel@tonic-gate 		break;
14990Sstevel@tonic-gate 	case AF_UNIX:
15000Sstevel@tonic-gate 	default:
15010Sstevel@tonic-gate 		if (optlen != 0) {
15020Sstevel@tonic-gate 			opt = sogetoff(mp, conn_ind->OPT_offset, optlen,
15030Sstevel@tonic-gate 			    __TPI_ALIGN_SIZE);
15040Sstevel@tonic-gate 			if (opt == NULL) {
15050Sstevel@tonic-gate 				error = EPROTO;
15060Sstevel@tonic-gate 				freemsg(mp);
15070Sstevel@tonic-gate 				eprintsoline(so, error);
15080Sstevel@tonic-gate 				goto disconnect_unlocked;
15090Sstevel@tonic-gate 			}
15100Sstevel@tonic-gate 		}
15110Sstevel@tonic-gate 		if (so->so_family == AF_UNIX) {
15120Sstevel@tonic-gate 			if (!(so->so_state & SS_FADDR_NOXLATE)) {
15130Sstevel@tonic-gate 				src = NULL;
15140Sstevel@tonic-gate 				srclen = 0;
15150Sstevel@tonic-gate 			}
15160Sstevel@tonic-gate 			/* Extract src address from options */
15170Sstevel@tonic-gate 			if (optlen != 0)
15180Sstevel@tonic-gate 				so_getopt_srcaddr(opt, optlen, &src, &srclen);
15190Sstevel@tonic-gate 		}
15200Sstevel@tonic-gate 		break;
15210Sstevel@tonic-gate 	}
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	/*
15240Sstevel@tonic-gate 	 * Create the new socket.
15250Sstevel@tonic-gate 	 */
15260Sstevel@tonic-gate 	VN_HOLD(so->so_accessvp);
15270Sstevel@tonic-gate 	nso = sotpi_create(so->so_accessvp, so->so_family, so->so_type,
15280Sstevel@tonic-gate 			so->so_protocol, so->so_version, so, &error);
15290Sstevel@tonic-gate 	if (nso == NULL) {
15300Sstevel@tonic-gate 		ASSERT(error != 0);
15310Sstevel@tonic-gate 		/*
15320Sstevel@tonic-gate 		 * Accept can not fail with ENOBUFS. sotpi_create
15330Sstevel@tonic-gate 		 * sleeps waiting for memory until a signal is caught
15340Sstevel@tonic-gate 		 * so return EINTR.
15350Sstevel@tonic-gate 		 */
15360Sstevel@tonic-gate 		freemsg(mp);
15370Sstevel@tonic-gate 		if (error == ENOBUFS)
15380Sstevel@tonic-gate 			error = EINTR;
15390Sstevel@tonic-gate 		goto e_disc_unl;
15400Sstevel@tonic-gate 	}
15410Sstevel@tonic-gate 	nvp = SOTOV(nso);
15420Sstevel@tonic-gate 
1543898Skais 	/*
1544898Skais 	 * If the transport sent up an SSL connection context, then attach
1545898Skais 	 * it the new socket, and set the (sd_wputdatafunc)() and
1546898Skais 	 * (sd_rputdatafunc)() stream head hooks to intercept and process
1547898Skais 	 * SSL records.
1548898Skais 	 */
1549898Skais 	if (ctxmp != NULL) {
1550898Skais 		/*
1551898Skais 		 * This kssl_ctx_t is already held for us by the transport.
1552898Skais 		 * So, we don't need to do a kssl_hold_ctx() here.
1553898Skais 		 */
1554898Skais 		nso->so_kssl_ctx = *((kssl_ctx_t *)ctxmp->b_rptr);
1555898Skais 		freemsg(ctxmp);
1556898Skais 		mp->b_cont = NULL;
1557898Skais 		strsetrwputdatahooks(nvp, strsock_kssl_input,
1558898Skais 		    strsock_kssl_output);
1559898Skais 	}
15600Sstevel@tonic-gate #ifdef DEBUG
15610Sstevel@tonic-gate 	/*
15620Sstevel@tonic-gate 	 * SO_DEBUG is used to trigger the dprint* and eprint* macros thus
15630Sstevel@tonic-gate 	 * it's inherited early to allow debugging of the accept code itself.
15640Sstevel@tonic-gate 	 */
15650Sstevel@tonic-gate 	nso->so_options |= so->so_options & SO_DEBUG;
15660Sstevel@tonic-gate #endif /* DEBUG */
15670Sstevel@tonic-gate 
15680Sstevel@tonic-gate 	/*
15690Sstevel@tonic-gate 	 * Save the SRC address from the T_CONN_IND
15700Sstevel@tonic-gate 	 * for getpeername to work on AF_UNIX and on transports that do not
15710Sstevel@tonic-gate 	 * support TI_GETPEERNAME.
15720Sstevel@tonic-gate 	 *
15730Sstevel@tonic-gate 	 * NOTE: AF_UNIX NUL termination is ensured by the sender's
15740Sstevel@tonic-gate 	 * copyin_name().
15750Sstevel@tonic-gate 	 */
15760Sstevel@tonic-gate 	if (srclen > (t_uscalar_t)nso->so_faddr_maxlen) {
15770Sstevel@tonic-gate 		error = EINVAL;
15780Sstevel@tonic-gate 		freemsg(mp);
15790Sstevel@tonic-gate 		eprintsoline(so, error);
15800Sstevel@tonic-gate 		goto disconnect_vp_unlocked;
15810Sstevel@tonic-gate 	}
15820Sstevel@tonic-gate 	nso->so_faddr_len = (socklen_t)srclen;
15830Sstevel@tonic-gate 	ASSERT(so->so_faddr_len <= so->so_faddr_maxlen);
15840Sstevel@tonic-gate 	bcopy(src, nso->so_faddr_sa, srclen);
15850Sstevel@tonic-gate 	nso->so_state |= SS_FADDR_VALID;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 	if ((DB_REF(mp) > 1) || MBLKSIZE(mp) <
15880Sstevel@tonic-gate 	    (sizeof (struct T_conn_res) + sizeof (intptr_t))) {
15890Sstevel@tonic-gate 		cred_t *cr;
15900Sstevel@tonic-gate 
15910Sstevel@tonic-gate 		if ((cr = DB_CRED(mp)) != NULL) {
15920Sstevel@tonic-gate 			crhold(cr);
15930Sstevel@tonic-gate 			nso->so_peercred = cr;
15940Sstevel@tonic-gate 			nso->so_cpid = DB_CPID(mp);
15950Sstevel@tonic-gate 		}
15960Sstevel@tonic-gate 		freemsg(mp);
15970Sstevel@tonic-gate 
15980Sstevel@tonic-gate 		mp = soallocproto1(NULL, sizeof (struct T_conn_res) +
15990Sstevel@tonic-gate 		    sizeof (intptr_t), 0, _ALLOC_INTR);
16000Sstevel@tonic-gate 		if (mp == NULL) {
16010Sstevel@tonic-gate 			/*
16020Sstevel@tonic-gate 			 * Accept can not fail with ENOBUFS.
16030Sstevel@tonic-gate 			 * A signal was caught so return EINTR.
16040Sstevel@tonic-gate 			 */
16050Sstevel@tonic-gate 			error = EINTR;
16060Sstevel@tonic-gate 			eprintsoline(so, error);
16070Sstevel@tonic-gate 			goto disconnect_vp_unlocked;
16080Sstevel@tonic-gate 		}
16090Sstevel@tonic-gate 		conn_res = (struct T_conn_res *)mp->b_rptr;
16100Sstevel@tonic-gate 	} else {
16110Sstevel@tonic-gate 		nso->so_peercred = DB_CRED(mp);
16120Sstevel@tonic-gate 		nso->so_cpid = DB_CPID(mp);
16130Sstevel@tonic-gate 		DB_CRED(mp) = NULL;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 		mp->b_rptr = DB_BASE(mp);
16160Sstevel@tonic-gate 		conn_res = (struct T_conn_res *)mp->b_rptr;
16170Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + sizeof (struct T_conn_res);
16180Sstevel@tonic-gate 	}
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 	/*
16210Sstevel@tonic-gate 	 * New socket must be bound at least in sockfs and, except for AF_INET,
16220Sstevel@tonic-gate 	 * (or AF_INET6) it also has to be bound in the transport provider.
1623*4379Sja97890 	 * We set the local address in the sonode from the T_OK_ACK of the
1624*4379Sja97890 	 * T_CONN_RES. For this reason the address we bind to here isn't
1625*4379Sja97890 	 * important.
16260Sstevel@tonic-gate 	 */
16270Sstevel@tonic-gate 	if ((nso->so_family == AF_INET || nso->so_family == AF_INET6) &&
16280Sstevel@tonic-gate 	    /*CONSTCOND*/
16290Sstevel@tonic-gate 	    nso->so_type == SOCK_STREAM && !soaccept_tpi_tcp) {
16300Sstevel@tonic-gate 		/*
16310Sstevel@tonic-gate 		 * Optimization for AF_INET{,6} transports
16320Sstevel@tonic-gate 		 * that can handle a T_CONN_RES without being bound.
16330Sstevel@tonic-gate 		 */
16340Sstevel@tonic-gate 		mutex_enter(&nso->so_lock);
16350Sstevel@tonic-gate 		so_automatic_bind(nso);
16360Sstevel@tonic-gate 		mutex_exit(&nso->so_lock);
16370Sstevel@tonic-gate 	} else {
16380Sstevel@tonic-gate 		/* Perform NULL bind with the transport provider. */
16390Sstevel@tonic-gate 		if ((error = sotpi_bind(nso, NULL, 0, _SOBIND_UNSPEC)) != 0) {
16400Sstevel@tonic-gate 			ASSERT(error != ENOBUFS);
16410Sstevel@tonic-gate 			freemsg(mp);
16420Sstevel@tonic-gate 			eprintsoline(nso, error);
16430Sstevel@tonic-gate 			goto disconnect_vp_unlocked;
16440Sstevel@tonic-gate 		}
16450Sstevel@tonic-gate 	}
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate 	/*
16480Sstevel@tonic-gate 	 * Inherit SIOCSPGRP, SS_ASYNC before we send the {O_}T_CONN_RES
16490Sstevel@tonic-gate 	 * so that any data arriving on the new socket will cause the
16500Sstevel@tonic-gate 	 * appropriate signals to be delivered for the new socket.
16510Sstevel@tonic-gate 	 *
16520Sstevel@tonic-gate 	 * No other thread (except strsock_proto and strsock_misc)
16530Sstevel@tonic-gate 	 * can access the new socket thus we relax the locking.
16540Sstevel@tonic-gate 	 */
16550Sstevel@tonic-gate 	nso->so_pgrp = so->so_pgrp;
16560Sstevel@tonic-gate 	nso->so_state |= so->so_state & (SS_ASYNC|SS_FADDR_NOXLATE);
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	if (nso->so_pgrp != 0) {
16590Sstevel@tonic-gate 		if ((error = so_set_events(nso, nvp, CRED())) != 0) {
16600Sstevel@tonic-gate 			eprintsoline(nso, error);
16610Sstevel@tonic-gate 			error = 0;
16620Sstevel@tonic-gate 			nso->so_pgrp = 0;
16630Sstevel@tonic-gate 		}
16640Sstevel@tonic-gate 	}
16650Sstevel@tonic-gate 
16660Sstevel@tonic-gate 	/*
16670Sstevel@tonic-gate 	 * Make note of the socket level options. TCP and IP level options
16680Sstevel@tonic-gate 	 * are already inherited. We could do all this after accept is
16690Sstevel@tonic-gate 	 * successful but doing it here simplifies code and no harm done
16700Sstevel@tonic-gate 	 * for error case.
16710Sstevel@tonic-gate 	 */
16720Sstevel@tonic-gate 	nso->so_options = so->so_options & (SO_DEBUG|SO_REUSEADDR|SO_KEEPALIVE|
16730Sstevel@tonic-gate 	    SO_DONTROUTE|SO_BROADCAST|SO_USELOOPBACK|
16740Sstevel@tonic-gate 	    SO_OOBINLINE|SO_DGRAM_ERRIND|SO_LINGER);
16750Sstevel@tonic-gate 	nso->so_sndbuf = so->so_sndbuf;
16760Sstevel@tonic-gate 	nso->so_rcvbuf = so->so_rcvbuf;
16770Sstevel@tonic-gate 	if (nso->so_options & SO_LINGER)
16780Sstevel@tonic-gate 		nso->so_linger = so->so_linger;
16790Sstevel@tonic-gate 
1680741Smasputra 	if ((so->so_state & SS_DIRECT) != 0) {
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 		ASSERT(opt != NULL);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 		conn_res->OPT_length = optlen;
16850Sstevel@tonic-gate 		conn_res->OPT_offset = MBLKL(mp);
16860Sstevel@tonic-gate 		bcopy(&opt, mp->b_wptr, optlen);
16870Sstevel@tonic-gate 		mp->b_wptr += optlen;
16880Sstevel@tonic-gate 		conn_res->PRIM_type = T_CONN_RES;
16890Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = 0;
16900Sstevel@tonic-gate 		PRIM_type = T_CONN_RES;
16910Sstevel@tonic-gate 
16920Sstevel@tonic-gate 		/* Send down the T_CONN_RES on acceptor STREAM */
16930Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(nso), mp, NULL,
16940Sstevel@tonic-gate 		    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
16950Sstevel@tonic-gate 		if (error) {
16960Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
16970Sstevel@tonic-gate 			so_lock_single(so);
16980Sstevel@tonic-gate 			eprintsoline(so, error);
16990Sstevel@tonic-gate 			goto disconnect_vp;
17000Sstevel@tonic-gate 		}
17010Sstevel@tonic-gate 		mutex_enter(&nso->so_lock);
17020Sstevel@tonic-gate 		error = sowaitprim(nso, T_CONN_RES, T_OK_ACK,
17030Sstevel@tonic-gate 		    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
17040Sstevel@tonic-gate 		if (error) {
17050Sstevel@tonic-gate 			mutex_exit(&nso->so_lock);
17060Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
17070Sstevel@tonic-gate 			so_lock_single(so);
17080Sstevel@tonic-gate 			eprintsoline(so, error);
17090Sstevel@tonic-gate 			goto disconnect_vp;
17100Sstevel@tonic-gate 		}
17110Sstevel@tonic-gate 		if (nso->so_family == AF_INET) {
17120Sstevel@tonic-gate 			sin_t *sin;
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 			sin = (sin_t *)(ack_mp->b_rptr +
17150Sstevel@tonic-gate 			    sizeof (struct T_ok_ack));
17160Sstevel@tonic-gate 			bcopy(sin, nso->so_laddr_sa, sizeof (sin_t));
17170Sstevel@tonic-gate 			nso->so_laddr_len = sizeof (sin_t);
17180Sstevel@tonic-gate 		} else {
17190Sstevel@tonic-gate 			sin6_t *sin6;
17200Sstevel@tonic-gate 
17210Sstevel@tonic-gate 			sin6 = (sin6_t *)(ack_mp->b_rptr +
17220Sstevel@tonic-gate 			    sizeof (struct T_ok_ack));
17230Sstevel@tonic-gate 			bcopy(sin6, nso->so_laddr_sa, sizeof (sin6_t));
17240Sstevel@tonic-gate 			nso->so_laddr_len = sizeof (sin6_t);
17250Sstevel@tonic-gate 		}
17260Sstevel@tonic-gate 		freemsg(ack_mp);
17270Sstevel@tonic-gate 
17280Sstevel@tonic-gate 		nso->so_state |= SS_ISCONNECTED | SS_LADDR_VALID;
17290Sstevel@tonic-gate 		nso->so_priv = opt;
17300Sstevel@tonic-gate 
17310Sstevel@tonic-gate 		if (so->so_nl7c_flags & NL7C_ENABLED) {
17320Sstevel@tonic-gate 			/*
17331974Sbrutus 			 * A NL7C marked listen()er so the new socket
17341974Sbrutus 			 * inherits the listen()er's NL7C state, except
17351974Sbrutus 			 * for NL7C_POLLIN.
17360Sstevel@tonic-gate 			 *
17371974Sbrutus 			 * Only call NL7C to process the new socket if
17381974Sbrutus 			 * the listen socket allows blocking i/o.
17390Sstevel@tonic-gate 			 */
17401974Sbrutus 			nso->so_nl7c_flags = so->so_nl7c_flags & (~NL7C_POLLIN);
17411974Sbrutus 			if (so->so_state & (SS_NONBLOCK|SS_NDELAY)) {
17421974Sbrutus 				/*
17431974Sbrutus 				 * Nonblocking accept() just make it
17441974Sbrutus 				 * persist to defer processing to the
17451974Sbrutus 				 * read-side syscall (e.g. read).
17461974Sbrutus 				 */
17471974Sbrutus 				nso->so_nl7c_flags |= NL7C_SOPERSIST;
17481974Sbrutus 			} else if (nl7c_process(nso, B_FALSE)) {
17490Sstevel@tonic-gate 				/*
17500Sstevel@tonic-gate 				 * NL7C has completed processing on the
17510Sstevel@tonic-gate 				 * socket, close the socket and back to
17520Sstevel@tonic-gate 				 * the top to await the next T_CONN_IND.
17530Sstevel@tonic-gate 				 */
17540Sstevel@tonic-gate 				mutex_exit(&nso->so_lock);
17550Sstevel@tonic-gate 				(void) VOP_CLOSE(nvp, 0, 1, (offset_t)0,
17560Sstevel@tonic-gate 						CRED());
17570Sstevel@tonic-gate 				VN_RELE(nvp);
17580Sstevel@tonic-gate 				goto again;
17590Sstevel@tonic-gate 			}
17600Sstevel@tonic-gate 			/* Pass the new socket out */
17610Sstevel@tonic-gate 		}
17620Sstevel@tonic-gate 
17630Sstevel@tonic-gate 		mutex_exit(&nso->so_lock);
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 		/*
17662811Sja97890 		 * It's possible, through the use of autopush for example,
17672811Sja97890 		 * that the acceptor stream may not support SS_DIRECT
17682811Sja97890 		 * semantics. If the new socket does not support SS_DIRECT
17692811Sja97890 		 * we issue a _SIOCSOCKFALLBACK to inform the transport
17702811Sja97890 		 * as we would in the I_PUSH case.
17712811Sja97890 		 */
17722811Sja97890 		if (!(nso->so_state & SS_DIRECT)) {
17732811Sja97890 			int	rval;
17742811Sja97890 
17752811Sja97890 			if ((error = strioctl(SOTOV(nso), _SIOCSOCKFALLBACK,
17762811Sja97890 			    0, 0, K_TO_K, CRED(), &rval)) != 0) {
17772811Sja97890 				mutex_enter(&so->so_lock);
17782811Sja97890 				so_lock_single(so);
17792811Sja97890 				eprintsoline(so, error);
17802811Sja97890 				goto disconnect_vp;
17812811Sja97890 			}
17822811Sja97890 		}
17832811Sja97890 
17842811Sja97890 		/*
17850Sstevel@tonic-gate 		 * Pass out new socket.
17860Sstevel@tonic-gate 		 */
17870Sstevel@tonic-gate 		if (nsop != NULL)
17880Sstevel@tonic-gate 			*nsop = nso;
17890Sstevel@tonic-gate 
17900Sstevel@tonic-gate 		return (0);
17910Sstevel@tonic-gate 	}
17920Sstevel@tonic-gate 
17930Sstevel@tonic-gate 	/*
17940Sstevel@tonic-gate 	 * This is the non-performance case for sockets (e.g. AF_UNIX sockets)
17950Sstevel@tonic-gate 	 * which don't support the FireEngine accept fast-path. It is also
17960Sstevel@tonic-gate 	 * used when the virtual "sockmod" has been I_POP'd and I_PUSH'd
17970Sstevel@tonic-gate 	 * again. Neither sockfs nor TCP attempt to find out if some other
17980Sstevel@tonic-gate 	 * random module has been inserted in between (in which case we
17990Sstevel@tonic-gate 	 * should follow TLI accept behaviour). We blindly assume the worst
18000Sstevel@tonic-gate 	 * case and revert back to old behaviour i.e. TCP will not send us
18010Sstevel@tonic-gate 	 * any option (eager) and the accept should happen on the listener
18020Sstevel@tonic-gate 	 * queue. Any queued T_conn_ind have already got their options removed
18030Sstevel@tonic-gate 	 * by so_sock2_stream() when "sockmod" was I_POP'd.
18040Sstevel@tonic-gate 	 */
18050Sstevel@tonic-gate 	/*
18060Sstevel@tonic-gate 	 * Fill in the {O_}T_CONN_RES before getting SOLOCKED.
18070Sstevel@tonic-gate 	 */
18080Sstevel@tonic-gate 	if ((nso->so_mode & SM_ACCEPTOR_ID) == 0) {
18090Sstevel@tonic-gate #ifdef	_ILP32
18100Sstevel@tonic-gate 		queue_t	*q;
18110Sstevel@tonic-gate 
18120Sstevel@tonic-gate 		/*
18130Sstevel@tonic-gate 		 * Find read queue in driver
18140Sstevel@tonic-gate 		 * Can safely do this since we "own" nso/nvp.
18150Sstevel@tonic-gate 		 */
18160Sstevel@tonic-gate 		q = strvp2wq(nvp)->q_next;
18170Sstevel@tonic-gate 		while (SAMESTR(q))
18180Sstevel@tonic-gate 			q = q->q_next;
18190Sstevel@tonic-gate 		q = RD(q);
18200Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = (t_uscalar_t)q;
18210Sstevel@tonic-gate #else
18220Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = (t_uscalar_t)getminor(nvp->v_rdev);
18230Sstevel@tonic-gate #endif	/* _ILP32 */
18240Sstevel@tonic-gate 		conn_res->PRIM_type = O_T_CONN_RES;
18250Sstevel@tonic-gate 		PRIM_type = O_T_CONN_RES;
18260Sstevel@tonic-gate 	} else {
18270Sstevel@tonic-gate 		conn_res->ACCEPTOR_id = nso->so_acceptor_id;
18280Sstevel@tonic-gate 		conn_res->PRIM_type = T_CONN_RES;
18290Sstevel@tonic-gate 		PRIM_type = T_CONN_RES;
18300Sstevel@tonic-gate 	}
18310Sstevel@tonic-gate 	conn_res->SEQ_number = SEQ_number;
18320Sstevel@tonic-gate 	conn_res->OPT_length = 0;
18330Sstevel@tonic-gate 	conn_res->OPT_offset = 0;
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
18360Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
18370Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
18380Sstevel@tonic-gate 
18390Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL,
18400Sstevel@tonic-gate 	    0, 0, MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
18410Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
18420Sstevel@tonic-gate 	if (error) {
18430Sstevel@tonic-gate 		eprintsoline(so, error);
18440Sstevel@tonic-gate 		goto disconnect_vp;
18450Sstevel@tonic-gate 	}
1846*4379Sja97890 	error = sowaitprim(so, PRIM_type, T_OK_ACK,
1847*4379Sja97890 	    (t_uscalar_t)sizeof (struct T_ok_ack), &ack_mp, 0);
18480Sstevel@tonic-gate 	if (error) {
18490Sstevel@tonic-gate 		eprintsoline(so, error);
18500Sstevel@tonic-gate 		goto disconnect_vp;
18510Sstevel@tonic-gate 	}
1852*4379Sja97890 	/*
1853*4379Sja97890 	 * If there is a sin/sin6 appended onto the T_OK_ACK use
1854*4379Sja97890 	 * that to set the local address. If this is not present
1855*4379Sja97890 	 * then we zero out the address and don't set the
1856*4379Sja97890 	 * SS_LADDR_VALID bit.
1857*4379Sja97890 	 */
1858*4379Sja97890 	sinlen = (nso->so_family == AF_INET) ? sizeof (sin_t) : sizeof (sin6_t);
1859*4379Sja97890 	if ((nso->so_family == AF_INET) || (nso->so_family == AF_INET6) &&
1860*4379Sja97890 	    MBLKL(ack_mp) == (sizeof (struct T_ok_ack) + sinlen)) {
1861*4379Sja97890 		ack_mp->b_rptr += sizeof (struct T_ok_ack);
1862*4379Sja97890 		bcopy(ack_mp->b_rptr, nso->so_laddr_sa, sinlen);
1863*4379Sja97890 		nso->so_laddr_len = sinlen;
1864*4379Sja97890 		nso->so_state |= SS_LADDR_VALID;
1865*4379Sja97890 	} else {
1866*4379Sja97890 		nso->so_laddr_len = so->so_laddr_len;
1867*4379Sja97890 		ASSERT(nso->so_laddr_len <= nso->so_laddr_maxlen);
1868*4379Sja97890 		bzero(nso->so_laddr_sa, nso->so_addr_size);
1869*4379Sja97890 	}
1870*4379Sja97890 	freemsg(ack_mp);
1871*4379Sja97890 
18720Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
18730Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate 	nso->so_state |= SS_ISCONNECTED;
18760Sstevel@tonic-gate 
18770Sstevel@tonic-gate 	/*
18780Sstevel@tonic-gate 	 * Pass out new socket.
18790Sstevel@tonic-gate 	 */
18800Sstevel@tonic-gate 	if (nsop != NULL)
18810Sstevel@tonic-gate 		*nsop = nso;
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	return (0);
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 
18860Sstevel@tonic-gate eproto_disc_unl:
18870Sstevel@tonic-gate 	error = EPROTO;
18880Sstevel@tonic-gate e_disc_unl:
18890Sstevel@tonic-gate 	eprintsoline(so, error);
18900Sstevel@tonic-gate 	goto disconnect_unlocked;
18910Sstevel@tonic-gate 
18920Sstevel@tonic-gate pr_disc_vp_unl:
18930Sstevel@tonic-gate 	eprintsoline(so, error);
18940Sstevel@tonic-gate disconnect_vp_unlocked:
18950Sstevel@tonic-gate 	(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
18960Sstevel@tonic-gate 	VN_RELE(nvp);
18970Sstevel@tonic-gate disconnect_unlocked:
18980Sstevel@tonic-gate 	(void) sodisconnect(so, SEQ_number, 0);
18990Sstevel@tonic-gate 	return (error);
19000Sstevel@tonic-gate 
19010Sstevel@tonic-gate pr_disc_vp:
19020Sstevel@tonic-gate 	eprintsoline(so, error);
19030Sstevel@tonic-gate disconnect_vp:
19040Sstevel@tonic-gate 	(void) sodisconnect(so, SEQ_number, _SODISCONNECT_LOCK_HELD);
19050Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
19060Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
19070Sstevel@tonic-gate 	(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
19080Sstevel@tonic-gate 	VN_RELE(nvp);
19090Sstevel@tonic-gate 	return (error);
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate conn_bad:	/* Note: SunOS 4/BSD unconditionally returns EINVAL here */
19120Sstevel@tonic-gate 	error = (so->so_type == SOCK_DGRAM || so->so_type == SOCK_RAW)
19130Sstevel@tonic-gate 	    ? EOPNOTSUPP : EINVAL;
19140Sstevel@tonic-gate e_bad:
19150Sstevel@tonic-gate 	eprintsoline(so, error);
19160Sstevel@tonic-gate 	return (error);
19170Sstevel@tonic-gate }
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate /*
19200Sstevel@tonic-gate  * connect a socket.
19210Sstevel@tonic-gate  *
19220Sstevel@tonic-gate  * Allow SOCK_DGRAM sockets to reconnect (by specifying a new address) and to
19230Sstevel@tonic-gate  * unconnect (by specifying a null address).
19240Sstevel@tonic-gate  */
19250Sstevel@tonic-gate int
19260Sstevel@tonic-gate sotpi_connect(struct sonode *so,
19270Sstevel@tonic-gate 	const struct sockaddr *name,
19280Sstevel@tonic-gate 	socklen_t namelen,
19290Sstevel@tonic-gate 	int fflag,
19300Sstevel@tonic-gate 	int flags)
19310Sstevel@tonic-gate {
19320Sstevel@tonic-gate 	struct T_conn_req	conn_req;
19330Sstevel@tonic-gate 	int			error = 0;
19340Sstevel@tonic-gate 	mblk_t			*mp;
19350Sstevel@tonic-gate 	void			*src;
19360Sstevel@tonic-gate 	socklen_t		srclen;
19370Sstevel@tonic-gate 	void			*addr;
19380Sstevel@tonic-gate 	socklen_t		addrlen;
19390Sstevel@tonic-gate 	boolean_t		need_unlock;
19400Sstevel@tonic-gate 
19410Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_connect(%p, %p, %d, 0x%x, 0x%x) %s\n",
19420Sstevel@tonic-gate 		so, name, namelen, fflag, flags,
19430Sstevel@tonic-gate 		pr_state(so->so_state, so->so_mode)));
19440Sstevel@tonic-gate 
19450Sstevel@tonic-gate 	/*
19460Sstevel@tonic-gate 	 * Preallocate the T_CONN_REQ mblk before grabbing SOLOCKED to
19470Sstevel@tonic-gate 	 * avoid sleeping for memory with SOLOCKED held.
19480Sstevel@tonic-gate 	 * We know that the T_CONN_REQ can't be larger than 2 * so_faddr_maxlen
19490Sstevel@tonic-gate 	 * + sizeof (struct T_opthdr).
19500Sstevel@tonic-gate 	 * (the AF_UNIX so_ux_addr_xlate() does not make the address
19510Sstevel@tonic-gate 	 * exceed so_faddr_maxlen).
19520Sstevel@tonic-gate 	 */
19530Sstevel@tonic-gate 	mp = soallocproto(sizeof (struct T_conn_req) +
19540Sstevel@tonic-gate 	    2 * so->so_faddr_maxlen + sizeof (struct T_opthdr), _ALLOC_INTR);
19550Sstevel@tonic-gate 	if (mp == NULL) {
19560Sstevel@tonic-gate 		/*
19570Sstevel@tonic-gate 		 * Connect can not fail with ENOBUFS. A signal was
19580Sstevel@tonic-gate 		 * caught so return EINTR.
19590Sstevel@tonic-gate 		 */
19600Sstevel@tonic-gate 		error = EINTR;
19610Sstevel@tonic-gate 		eprintsoline(so, error);
19620Sstevel@tonic-gate 		return (error);
19630Sstevel@tonic-gate 	}
19640Sstevel@tonic-gate 
19650Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
19660Sstevel@tonic-gate 	/*
19670Sstevel@tonic-gate 	 * Make sure that there is a preallocated unbind_req
19680Sstevel@tonic-gate 	 * message before any binding. This message allocated when
19690Sstevel@tonic-gate 	 * the socket is created  but it might be have been
19700Sstevel@tonic-gate 	 * consumed.
19710Sstevel@tonic-gate 	 */
19720Sstevel@tonic-gate 	if (so->so_unbind_mp == NULL) {
19730Sstevel@tonic-gate 		dprintso(so, 1, ("sotpi_connect: allocating unbind_req\n"));
19740Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
19750Sstevel@tonic-gate 		so->so_unbind_mp =
19760Sstevel@tonic-gate 		    soallocproto(sizeof (struct T_unbind_req), _ALLOC_INTR);
19770Sstevel@tonic-gate 		if (so->so_unbind_mp == NULL) {
19780Sstevel@tonic-gate 			error = EINTR;
19790Sstevel@tonic-gate 			need_unlock = B_FALSE;
19800Sstevel@tonic-gate 			goto done;
19810Sstevel@tonic-gate 		}
19820Sstevel@tonic-gate 	}
19830Sstevel@tonic-gate 
19840Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
19850Sstevel@tonic-gate 	need_unlock = B_TRUE;
19860Sstevel@tonic-gate 
19870Sstevel@tonic-gate 	/*
19880Sstevel@tonic-gate 	 * Can't have done a listen before connecting.
19890Sstevel@tonic-gate 	 */
19900Sstevel@tonic-gate 	if (so->so_state & SS_ACCEPTCONN) {
19910Sstevel@tonic-gate 		error = EOPNOTSUPP;
19920Sstevel@tonic-gate 		goto done;
19930Sstevel@tonic-gate 	}
19940Sstevel@tonic-gate 
19950Sstevel@tonic-gate 	/*
19960Sstevel@tonic-gate 	 * Must be bound with the transport
19970Sstevel@tonic-gate 	 */
19980Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND)) {
19990Sstevel@tonic-gate 		if ((so->so_family == AF_INET || so->so_family == AF_INET6) &&
20000Sstevel@tonic-gate 		    /*CONSTCOND*/
20010Sstevel@tonic-gate 		    so->so_type == SOCK_STREAM && !soconnect_tpi_tcp) {
20020Sstevel@tonic-gate 			/*
20030Sstevel@tonic-gate 			 * Optimization for AF_INET{,6} transports
20040Sstevel@tonic-gate 			 * that can handle a T_CONN_REQ without being bound.
20050Sstevel@tonic-gate 			 */
20060Sstevel@tonic-gate 			so_automatic_bind(so);
20070Sstevel@tonic-gate 		} else {
20080Sstevel@tonic-gate 			error = sotpi_bind(so, NULL, 0,
20090Sstevel@tonic-gate 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD);
20100Sstevel@tonic-gate 			if (error)
20110Sstevel@tonic-gate 				goto done;
20120Sstevel@tonic-gate 		}
20130Sstevel@tonic-gate 		ASSERT(so->so_state & SS_ISBOUND);
20140Sstevel@tonic-gate 		flags |= _SOCONNECT_DID_BIND;
20150Sstevel@tonic-gate 	}
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 	/*
20180Sstevel@tonic-gate 	 * Handle a connect to a name parameter of type AF_UNSPEC like a
20190Sstevel@tonic-gate 	 * connect to a null address. This is the portable method to
20200Sstevel@tonic-gate 	 * unconnect a socket.
20210Sstevel@tonic-gate 	 */
20220Sstevel@tonic-gate 	if ((namelen >= sizeof (sa_family_t)) &&
20230Sstevel@tonic-gate 	    (name->sa_family == AF_UNSPEC)) {
20240Sstevel@tonic-gate 		name = NULL;
20250Sstevel@tonic-gate 		namelen = 0;
20260Sstevel@tonic-gate 	}
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate 	/*
20290Sstevel@tonic-gate 	 * Check that we are not already connected.
20300Sstevel@tonic-gate 	 * A connection-oriented socket cannot be reconnected.
20310Sstevel@tonic-gate 	 * A connected connection-less socket can be
20320Sstevel@tonic-gate 	 * - connected to a different address by a subsequent connect
20330Sstevel@tonic-gate 	 * - "unconnected" by a connect to the NULL address
20340Sstevel@tonic-gate 	 */
20350Sstevel@tonic-gate 	if (so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) {
20360Sstevel@tonic-gate 		ASSERT(!(flags & _SOCONNECT_DID_BIND));
20370Sstevel@tonic-gate 		if (so->so_mode & SM_CONNREQUIRED) {
20380Sstevel@tonic-gate 			/* Connection-oriented socket */
20390Sstevel@tonic-gate 			error = so->so_state & SS_ISCONNECTED ?
20400Sstevel@tonic-gate 			    EISCONN : EALREADY;
20410Sstevel@tonic-gate 			goto done;
20420Sstevel@tonic-gate 		}
20430Sstevel@tonic-gate 		/* Connection-less socket */
20440Sstevel@tonic-gate 		if (name == NULL) {
20450Sstevel@tonic-gate 			/*
20460Sstevel@tonic-gate 			 * Remove the connected state and clear SO_DGRAM_ERRIND
20470Sstevel@tonic-gate 			 * since it was set when the socket was connected.
20480Sstevel@tonic-gate 			 * If this is UDP also send down a T_DISCON_REQ.
20490Sstevel@tonic-gate 			 */
20500Sstevel@tonic-gate 			int val;
20510Sstevel@tonic-gate 
20520Sstevel@tonic-gate 			if ((so->so_family == AF_INET ||
20530Sstevel@tonic-gate 				so->so_family == AF_INET6) &&
20540Sstevel@tonic-gate 			    (so->so_type == SOCK_DGRAM ||
20550Sstevel@tonic-gate 				so->so_type == SOCK_RAW) &&
20560Sstevel@tonic-gate 			    /*CONSTCOND*/
20570Sstevel@tonic-gate 			    !soconnect_tpi_udp) {
20580Sstevel@tonic-gate 				/* XXX What about implicitly unbinding here? */
20590Sstevel@tonic-gate 				error = sodisconnect(so, -1,
20600Sstevel@tonic-gate 						_SODISCONNECT_LOCK_HELD);
20610Sstevel@tonic-gate 			} else {
20620Sstevel@tonic-gate 				so->so_state &=
20630Sstevel@tonic-gate 				    ~(SS_ISCONNECTED | SS_ISCONNECTING |
20640Sstevel@tonic-gate 				    SS_FADDR_VALID);
20650Sstevel@tonic-gate 				so->so_faddr_len = 0;
20660Sstevel@tonic-gate 			}
20670Sstevel@tonic-gate 
20680Sstevel@tonic-gate 			so_unlock_single(so, SOLOCKED);
20690Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
20700Sstevel@tonic-gate 
20710Sstevel@tonic-gate 			val = 0;
20720Sstevel@tonic-gate 			(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
20730Sstevel@tonic-gate 					&val, (t_uscalar_t)sizeof (val));
20740Sstevel@tonic-gate 
20750Sstevel@tonic-gate 			mutex_enter(&so->so_lock);
20760Sstevel@tonic-gate 			so_lock_single(so);	/* Set SOLOCKED */
20770Sstevel@tonic-gate 			goto done;
20780Sstevel@tonic-gate 		}
20790Sstevel@tonic-gate 	}
20800Sstevel@tonic-gate 	ASSERT(so->so_state & SS_ISBOUND);
20810Sstevel@tonic-gate 
20820Sstevel@tonic-gate 	if (name == NULL || namelen == 0) {
20830Sstevel@tonic-gate 		error = EINVAL;
20840Sstevel@tonic-gate 		goto done;
20850Sstevel@tonic-gate 	}
20860Sstevel@tonic-gate 	/*
20870Sstevel@tonic-gate 	 * Mark the socket if so_faddr_sa represents the transport level
20880Sstevel@tonic-gate 	 * address.
20890Sstevel@tonic-gate 	 */
20900Sstevel@tonic-gate 	if (flags & _SOCONNECT_NOXLATE) {
20910Sstevel@tonic-gate 		struct sockaddr_ux	*soaddr_ux;
20920Sstevel@tonic-gate 
20930Sstevel@tonic-gate 		ASSERT(so->so_family == AF_UNIX);
20940Sstevel@tonic-gate 		if (namelen != sizeof (struct sockaddr_ux)) {
20950Sstevel@tonic-gate 			error = EINVAL;
20960Sstevel@tonic-gate 			goto done;
20970Sstevel@tonic-gate 		}
20980Sstevel@tonic-gate 		soaddr_ux = (struct sockaddr_ux *)name;
20990Sstevel@tonic-gate 		name = (struct sockaddr *)&soaddr_ux->sou_addr;
21000Sstevel@tonic-gate 		namelen = sizeof (soaddr_ux->sou_addr);
21010Sstevel@tonic-gate 		so->so_state |= SS_FADDR_NOXLATE;
21020Sstevel@tonic-gate 	}
21030Sstevel@tonic-gate 
21040Sstevel@tonic-gate 	/*
21050Sstevel@tonic-gate 	 * Length and family checks.
21060Sstevel@tonic-gate 	 */
21070Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
21080Sstevel@tonic-gate 	if (error)
21090Sstevel@tonic-gate 		goto bad;
21100Sstevel@tonic-gate 
21110Sstevel@tonic-gate 	/*
21120Sstevel@tonic-gate 	 * Save foreign address. Needed for AF_UNIX as well as
21130Sstevel@tonic-gate 	 * transport providers that do not support TI_GETPEERNAME.
21140Sstevel@tonic-gate 	 * Also used for cached foreign address for TCP and UDP.
21150Sstevel@tonic-gate 	 */
21160Sstevel@tonic-gate 	if (namelen > (t_uscalar_t)so->so_faddr_maxlen) {
21170Sstevel@tonic-gate 		error = EINVAL;
21180Sstevel@tonic-gate 		goto done;
21190Sstevel@tonic-gate 	}
21200Sstevel@tonic-gate 	so->so_faddr_len = (socklen_t)namelen;
21210Sstevel@tonic-gate 	ASSERT(so->so_faddr_len <= so->so_faddr_maxlen);
21220Sstevel@tonic-gate 	bcopy(name, so->so_faddr_sa, namelen);
21230Sstevel@tonic-gate 	so->so_state |= SS_FADDR_VALID;
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
21260Sstevel@tonic-gate 		if (so->so_state & SS_FADDR_NOXLATE) {
21270Sstevel@tonic-gate 			/*
21280Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
21290Sstevel@tonic-gate 			 * pass any (transport internal) source address.
21300Sstevel@tonic-gate 			 */
21310Sstevel@tonic-gate 			addr = so->so_faddr_sa;
21320Sstevel@tonic-gate 			addrlen = (t_uscalar_t)so->so_faddr_len;
21330Sstevel@tonic-gate 			src = NULL;
21340Sstevel@tonic-gate 			srclen = 0;
21350Sstevel@tonic-gate 		} else {
21360Sstevel@tonic-gate 			/*
21370Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
21380Sstevel@tonic-gate 			 * and translate the remote address.
21390Sstevel@tonic-gate 			 * Holding so_lock thus so_laddr_sa can not change.
21400Sstevel@tonic-gate 			 */
21410Sstevel@tonic-gate 			src = so->so_laddr_sa;
21420Sstevel@tonic-gate 			srclen = (t_uscalar_t)so->so_laddr_len;
21430Sstevel@tonic-gate 			dprintso(so, 1,
21440Sstevel@tonic-gate 				("sotpi_connect UNIX: srclen %d, src %p\n",
21450Sstevel@tonic-gate 				srclen, src));
21460Sstevel@tonic-gate 			error = so_ux_addr_xlate(so,
21470Sstevel@tonic-gate 				so->so_faddr_sa, (socklen_t)so->so_faddr_len,
21480Sstevel@tonic-gate 				(flags & _SOCONNECT_XPG4_2),
21490Sstevel@tonic-gate 				&addr, &addrlen);
21500Sstevel@tonic-gate 			if (error)
21510Sstevel@tonic-gate 				goto bad;
21520Sstevel@tonic-gate 		}
21530Sstevel@tonic-gate 	} else {
21540Sstevel@tonic-gate 		addr = so->so_faddr_sa;
21550Sstevel@tonic-gate 		addrlen = (t_uscalar_t)so->so_faddr_len;
21560Sstevel@tonic-gate 		src = NULL;
21570Sstevel@tonic-gate 		srclen = 0;
21580Sstevel@tonic-gate 	}
21590Sstevel@tonic-gate 	/*
21600Sstevel@tonic-gate 	 * When connecting a datagram socket we issue the SO_DGRAM_ERRIND
21610Sstevel@tonic-gate 	 * option which asks the transport provider to send T_UDERR_IND
21620Sstevel@tonic-gate 	 * messages. These T_UDERR_IND messages are used to return connected
21630Sstevel@tonic-gate 	 * style errors (e.g. ECONNRESET) for connected datagram sockets.
21640Sstevel@tonic-gate 	 *
21650Sstevel@tonic-gate 	 * In addition, for UDP (and SOCK_RAW AF_INET{,6} sockets)
21660Sstevel@tonic-gate 	 * we send down a T_CONN_REQ. This is needed to let the
21670Sstevel@tonic-gate 	 * transport assign a local address that is consistent with
21680Sstevel@tonic-gate 	 * the remote address. Applications depend on a getsockname()
21690Sstevel@tonic-gate 	 * after a connect() to retrieve the "source" IP address for
21700Sstevel@tonic-gate 	 * the connected socket.  Invalidate the cached local address
21710Sstevel@tonic-gate 	 * to force getsockname() to enquire of the transport.
21720Sstevel@tonic-gate 	 */
21730Sstevel@tonic-gate 	if (!(so->so_mode & SM_CONNREQUIRED)) {
21740Sstevel@tonic-gate 		/*
21750Sstevel@tonic-gate 		 * Datagram socket.
21760Sstevel@tonic-gate 		 */
21770Sstevel@tonic-gate 		int32_t val;
21780Sstevel@tonic-gate 
21790Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
21800Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
21810Sstevel@tonic-gate 
21820Sstevel@tonic-gate 		val = 1;
21830Sstevel@tonic-gate 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DGRAM_ERRIND,
21840Sstevel@tonic-gate 					&val, (t_uscalar_t)sizeof (val));
21850Sstevel@tonic-gate 
21860Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
21870Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
21880Sstevel@tonic-gate 		if ((so->so_family != AF_INET && so->so_family != AF_INET6) ||
21890Sstevel@tonic-gate 		    (so->so_type != SOCK_DGRAM && so->so_type != SOCK_RAW) ||
21900Sstevel@tonic-gate 		    soconnect_tpi_udp) {
21910Sstevel@tonic-gate 			soisconnected(so);
21920Sstevel@tonic-gate 			goto done;
21930Sstevel@tonic-gate 		}
21940Sstevel@tonic-gate 		/*
21950Sstevel@tonic-gate 		 * Send down T_CONN_REQ etc.
21960Sstevel@tonic-gate 		 * Clear fflag to avoid returning EWOULDBLOCK.
21970Sstevel@tonic-gate 		 */
21980Sstevel@tonic-gate 		fflag = 0;
21990Sstevel@tonic-gate 		ASSERT(so->so_family != AF_UNIX);
22000Sstevel@tonic-gate 		so->so_state &= ~SS_LADDR_VALID;
22010Sstevel@tonic-gate 	} else if (so->so_laddr_len != 0) {
22020Sstevel@tonic-gate 		/*
22030Sstevel@tonic-gate 		 * If the local address or port was "any" then it may be
22040Sstevel@tonic-gate 		 * changed by the transport as a result of the
22050Sstevel@tonic-gate 		 * connect.  Invalidate the cached version if we have one.
22060Sstevel@tonic-gate 		 */
22070Sstevel@tonic-gate 		switch (so->so_family) {
22080Sstevel@tonic-gate 		case AF_INET:
22090Sstevel@tonic-gate 			ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin_t));
22100Sstevel@tonic-gate 			if (((sin_t *)so->so_laddr_sa)->sin_addr.s_addr ==
22110Sstevel@tonic-gate 			    INADDR_ANY ||
22120Sstevel@tonic-gate 			    ((sin_t *)so->so_laddr_sa)->sin_port == 0)
22130Sstevel@tonic-gate 				so->so_state &= ~SS_LADDR_VALID;
22140Sstevel@tonic-gate 			break;
22150Sstevel@tonic-gate 
22160Sstevel@tonic-gate 		case AF_INET6:
22170Sstevel@tonic-gate 			ASSERT(so->so_laddr_len == (socklen_t)sizeof (sin6_t));
22180Sstevel@tonic-gate 			if (IN6_IS_ADDR_UNSPECIFIED(
22190Sstevel@tonic-gate 			    &((sin6_t *)so->so_laddr_sa) ->sin6_addr) ||
22200Sstevel@tonic-gate 			    IN6_IS_ADDR_V4MAPPED_ANY(
22210Sstevel@tonic-gate 			    &((sin6_t *)so->so_laddr_sa)->sin6_addr) ||
22220Sstevel@tonic-gate 			    ((sin6_t *)so->so_laddr_sa)->sin6_port == 0)
22230Sstevel@tonic-gate 				    so->so_state &= ~SS_LADDR_VALID;
22240Sstevel@tonic-gate 			break;
22250Sstevel@tonic-gate 
22260Sstevel@tonic-gate 		default:
22270Sstevel@tonic-gate 			break;
22280Sstevel@tonic-gate 		}
22290Sstevel@tonic-gate 	}
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate 	/*
22320Sstevel@tonic-gate 	 * Check for failure of an earlier call
22330Sstevel@tonic-gate 	 */
22340Sstevel@tonic-gate 	if (so->so_error != 0)
22350Sstevel@tonic-gate 		goto so_bad;
22360Sstevel@tonic-gate 
22370Sstevel@tonic-gate 	/*
22380Sstevel@tonic-gate 	 * Send down T_CONN_REQ. Message was allocated above.
22390Sstevel@tonic-gate 	 */
22400Sstevel@tonic-gate 	conn_req.PRIM_type = T_CONN_REQ;
22410Sstevel@tonic-gate 	conn_req.DEST_length = addrlen;
22420Sstevel@tonic-gate 	conn_req.DEST_offset = (t_scalar_t)sizeof (conn_req);
22430Sstevel@tonic-gate 	if (srclen == 0) {
22440Sstevel@tonic-gate 		conn_req.OPT_length = 0;
22450Sstevel@tonic-gate 		conn_req.OPT_offset = 0;
22460Sstevel@tonic-gate 		soappendmsg(mp, &conn_req, sizeof (conn_req));
22470Sstevel@tonic-gate 		soappendmsg(mp, addr, addrlen);
22480Sstevel@tonic-gate 	} else {
22490Sstevel@tonic-gate 		/*
22500Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
22510Sstevel@tonic-gate 		 * address option.
22520Sstevel@tonic-gate 		 */
22530Sstevel@tonic-gate 		struct T_opthdr toh;
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
22560Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
22570Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
22580Sstevel@tonic-gate 		toh.status = 0;
22590Sstevel@tonic-gate 		conn_req.OPT_length =
22600Sstevel@tonic-gate 			(t_scalar_t)(sizeof (toh) + _TPI_ALIGN_TOPT(srclen));
22610Sstevel@tonic-gate 		conn_req.OPT_offset = (t_scalar_t)(sizeof (conn_req) +
22620Sstevel@tonic-gate 			_TPI_ALIGN_TOPT(addrlen));
22630Sstevel@tonic-gate 
22640Sstevel@tonic-gate 		soappendmsg(mp, &conn_req, sizeof (conn_req));
22650Sstevel@tonic-gate 		soappendmsg(mp, addr, addrlen);
22660Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
22670Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
22680Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
22690Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
22700Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
22710Sstevel@tonic-gate 	}
22720Sstevel@tonic-gate 	/*
22730Sstevel@tonic-gate 	 * Set SS_ISCONNECTING before sending down the T_CONN_REQ
22740Sstevel@tonic-gate 	 * in order to have the right state when the T_CONN_CON shows up.
22750Sstevel@tonic-gate 	 */
22760Sstevel@tonic-gate 	soisconnecting(so);
22770Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate #ifdef C2_AUDIT
22800Sstevel@tonic-gate 	if (audit_active)
22810Sstevel@tonic-gate 		audit_sock(T_CONN_REQ, strvp2wq(SOTOV(so)), mp, 0);
22820Sstevel@tonic-gate #endif /* C2_AUDIT */
22830Sstevel@tonic-gate 
22840Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
22850Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR, 0);
22860Sstevel@tonic-gate 	mp = NULL;
22870Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
22880Sstevel@tonic-gate 	if (error != 0)
22890Sstevel@tonic-gate 		goto bad;
22900Sstevel@tonic-gate 
22910Sstevel@tonic-gate 	if ((error = sowaitokack(so, T_CONN_REQ)) != 0)
22920Sstevel@tonic-gate 		goto bad;
22930Sstevel@tonic-gate 
22940Sstevel@tonic-gate 	/* Allow other threads to access the socket */
22950Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
22960Sstevel@tonic-gate 	need_unlock = B_FALSE;
22970Sstevel@tonic-gate 
22980Sstevel@tonic-gate 	/*
22990Sstevel@tonic-gate 	 * Wait until we get a T_CONN_CON or an error
23000Sstevel@tonic-gate 	 */
23010Sstevel@tonic-gate 	if ((error = sowaitconnected(so, fflag, 0)) != 0) {
23020Sstevel@tonic-gate 		so_lock_single(so);	/* Set SOLOCKED */
23030Sstevel@tonic-gate 		need_unlock = B_TRUE;
23040Sstevel@tonic-gate 	}
23050Sstevel@tonic-gate 
23060Sstevel@tonic-gate done:
23070Sstevel@tonic-gate 	freemsg(mp);
23080Sstevel@tonic-gate 	switch (error) {
23090Sstevel@tonic-gate 	case EINPROGRESS:
23100Sstevel@tonic-gate 	case EALREADY:
23110Sstevel@tonic-gate 	case EISCONN:
23120Sstevel@tonic-gate 	case EINTR:
23130Sstevel@tonic-gate 		/* Non-fatal errors */
23140Sstevel@tonic-gate 		so->so_state &= ~SS_LADDR_VALID;
23150Sstevel@tonic-gate 		/* FALLTHRU */
23160Sstevel@tonic-gate 	case 0:
23170Sstevel@tonic-gate 		break;
23180Sstevel@tonic-gate 
23190Sstevel@tonic-gate 	case EHOSTUNREACH:
23200Sstevel@tonic-gate 		if (flags & _SOCONNECT_XPG4_2) {
23210Sstevel@tonic-gate 			/*
23220Sstevel@tonic-gate 			 * X/Open specification contains a requirement that
23230Sstevel@tonic-gate 			 * ENETUNREACH be returned but does not require
23240Sstevel@tonic-gate 			 * EHOSTUNREACH. In order to keep the test suite
23250Sstevel@tonic-gate 			 * happy we mess with the errno here.
23260Sstevel@tonic-gate 			 */
23270Sstevel@tonic-gate 			error = ENETUNREACH;
23280Sstevel@tonic-gate 		}
23290Sstevel@tonic-gate 		/* FALLTHRU */
23300Sstevel@tonic-gate 
23310Sstevel@tonic-gate 	default:
23320Sstevel@tonic-gate 		ASSERT(need_unlock);
23330Sstevel@tonic-gate 		/*
23340Sstevel@tonic-gate 		 * Fatal errors: clear SS_ISCONNECTING in case it was set,
23350Sstevel@tonic-gate 		 * and invalidate local-address cache
23360Sstevel@tonic-gate 		 */
23370Sstevel@tonic-gate 		so->so_state &= ~(SS_ISCONNECTING | SS_LADDR_VALID);
23380Sstevel@tonic-gate 		/* A discon_ind might have already unbound us */
23390Sstevel@tonic-gate 		if ((flags & _SOCONNECT_DID_BIND) &&
23400Sstevel@tonic-gate 		    (so->so_state & SS_ISBOUND)) {
23410Sstevel@tonic-gate 			int err;
23420Sstevel@tonic-gate 
23430Sstevel@tonic-gate 			err = sotpi_unbind(so, 0);
23440Sstevel@tonic-gate 			/* LINTED - statement has no conseq */
23450Sstevel@tonic-gate 			if (err) {
23460Sstevel@tonic-gate 				eprintsoline(so, err);
23470Sstevel@tonic-gate 			}
23480Sstevel@tonic-gate 		}
23490Sstevel@tonic-gate 		break;
23500Sstevel@tonic-gate 	}
23510Sstevel@tonic-gate 	if (need_unlock)
23520Sstevel@tonic-gate 		so_unlock_single(so, SOLOCKED);
23530Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
23540Sstevel@tonic-gate 	return (error);
23550Sstevel@tonic-gate 
23560Sstevel@tonic-gate so_bad:	error = sogeterr(so);
23570Sstevel@tonic-gate bad:	eprintsoline(so, error);
23580Sstevel@tonic-gate 	goto done;
23590Sstevel@tonic-gate }
23600Sstevel@tonic-gate 
23610Sstevel@tonic-gate int
23620Sstevel@tonic-gate sotpi_shutdown(struct sonode *so, int how)
23630Sstevel@tonic-gate {
23640Sstevel@tonic-gate 	struct T_ordrel_req	ordrel_req;
23650Sstevel@tonic-gate 	mblk_t			*mp;
23660Sstevel@tonic-gate 	uint_t			old_state, state_change;
23670Sstevel@tonic-gate 	int			error = 0;
23680Sstevel@tonic-gate 
23690Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_shutdown(%p, %d) %s\n",
23700Sstevel@tonic-gate 		so, how, pr_state(so->so_state, so->so_mode)));
23710Sstevel@tonic-gate 
23720Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
23730Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
23740Sstevel@tonic-gate 
23750Sstevel@tonic-gate 	/*
23760Sstevel@tonic-gate 	 * SunOS 4.X has no check for datagram sockets.
23770Sstevel@tonic-gate 	 * 5.X checks that it is connected (ENOTCONN)
23780Sstevel@tonic-gate 	 * X/Open requires that we check the connected state.
23790Sstevel@tonic-gate 	 */
23800Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
23810Sstevel@tonic-gate 		if (!xnet_skip_checks) {
23820Sstevel@tonic-gate 			error = ENOTCONN;
23830Sstevel@tonic-gate 			if (xnet_check_print) {
23840Sstevel@tonic-gate 				printf("sockfs: X/Open shutdown check "
23850Sstevel@tonic-gate 					"caused ENOTCONN\n");
23860Sstevel@tonic-gate 			}
23870Sstevel@tonic-gate 		}
23880Sstevel@tonic-gate 		goto done;
23890Sstevel@tonic-gate 	}
23900Sstevel@tonic-gate 	/*
23910Sstevel@tonic-gate 	 * Record the current state and then perform any state changes.
23920Sstevel@tonic-gate 	 * Then use the difference between the old and new states to
23930Sstevel@tonic-gate 	 * determine which messages need to be sent.
23940Sstevel@tonic-gate 	 * This prevents e.g. duplicate T_ORDREL_REQ when there are
23950Sstevel@tonic-gate 	 * duplicate calls to shutdown().
23960Sstevel@tonic-gate 	 */
23970Sstevel@tonic-gate 	old_state = so->so_state;
23980Sstevel@tonic-gate 
23990Sstevel@tonic-gate 	switch (how) {
24000Sstevel@tonic-gate 	case 0:
24010Sstevel@tonic-gate 		socantrcvmore(so);
24020Sstevel@tonic-gate 		break;
24030Sstevel@tonic-gate 	case 1:
24040Sstevel@tonic-gate 		socantsendmore(so);
24050Sstevel@tonic-gate 		break;
24060Sstevel@tonic-gate 	case 2:
24070Sstevel@tonic-gate 		socantsendmore(so);
24080Sstevel@tonic-gate 		socantrcvmore(so);
24090Sstevel@tonic-gate 		break;
24100Sstevel@tonic-gate 	default:
24110Sstevel@tonic-gate 		error = EINVAL;
24120Sstevel@tonic-gate 		goto done;
24130Sstevel@tonic-gate 	}
24140Sstevel@tonic-gate 
24150Sstevel@tonic-gate 	/*
24160Sstevel@tonic-gate 	 * Assumes that the SS_CANT* flags are never cleared in the above code.
24170Sstevel@tonic-gate 	 */
24180Sstevel@tonic-gate 	state_change = (so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) -
24190Sstevel@tonic-gate 		(old_state & (SS_CANTRCVMORE|SS_CANTSENDMORE));
24200Sstevel@tonic-gate 	ASSERT((state_change & ~(SS_CANTRCVMORE|SS_CANTSENDMORE)) == 0);
24210Sstevel@tonic-gate 
24220Sstevel@tonic-gate 	switch (state_change) {
24230Sstevel@tonic-gate 	case 0:
24240Sstevel@tonic-gate 		dprintso(so, 1,
24250Sstevel@tonic-gate 		    ("sotpi_shutdown: nothing to send in state 0x%x\n",
24260Sstevel@tonic-gate 		    so->so_state));
24270Sstevel@tonic-gate 		goto done;
24280Sstevel@tonic-gate 
24290Sstevel@tonic-gate 	case SS_CANTRCVMORE:
24300Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
24310Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
24320Sstevel@tonic-gate 		/*
24330Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
24340Sstevel@tonic-gate 		 * pollwakeups, and signals.
24350Sstevel@tonic-gate 		 */
24360Sstevel@tonic-gate 		/*
24370Sstevel@tonic-gate 		 * Get the read lock before flushing data to avoid problems
24380Sstevel@tonic-gate 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
24390Sstevel@tonic-gate 		 */
24400Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24410Sstevel@tonic-gate 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
24420Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
24430Sstevel@tonic-gate 
24440Sstevel@tonic-gate 		/* Flush read side queue */
24450Sstevel@tonic-gate 		strflushrq(SOTOV(so), FLUSHALL);
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24480Sstevel@tonic-gate 		so_unlock_read(so);		/* Clear SOREADLOCKED */
24490Sstevel@tonic-gate 		break;
24500Sstevel@tonic-gate 
24510Sstevel@tonic-gate 	case SS_CANTSENDMORE:
24520Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
24530Sstevel@tonic-gate 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
24540Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24550Sstevel@tonic-gate 		break;
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 	case SS_CANTSENDMORE|SS_CANTRCVMORE:
24580Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
24590Sstevel@tonic-gate 		strsetwerror(SOTOV(so), 0, 0, sogetwrerr);
24600Sstevel@tonic-gate 		strseteof(SOTOV(so), 1);
24610Sstevel@tonic-gate 		/*
24620Sstevel@tonic-gate 		 * strseteof takes care of read side wakeups,
24630Sstevel@tonic-gate 		 * pollwakeups, and signals.
24640Sstevel@tonic-gate 		 */
24650Sstevel@tonic-gate 		/*
24660Sstevel@tonic-gate 		 * Get the read lock before flushing data to avoid problems
24670Sstevel@tonic-gate 		 * with the T_EXDATA_IND MSG_PEEK code in sotpi_recvmsg.
24680Sstevel@tonic-gate 		 */
24690Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24700Sstevel@tonic-gate 		(void) so_lock_read(so, 0);	/* Set SOREADLOCKED */
24710Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
24720Sstevel@tonic-gate 
24730Sstevel@tonic-gate 		/* Flush read side queue */
24740Sstevel@tonic-gate 		strflushrq(SOTOV(so), FLUSHALL);
24750Sstevel@tonic-gate 
24760Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
24770Sstevel@tonic-gate 		so_unlock_read(so);		/* Clear SOREADLOCKED */
24780Sstevel@tonic-gate 		break;
24790Sstevel@tonic-gate 	}
24800Sstevel@tonic-gate 
24810Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
24820Sstevel@tonic-gate 
24830Sstevel@tonic-gate 	/*
24840Sstevel@tonic-gate 	 * If either SS_CANTSENDMORE or SS_CANTRCVMORE or both of them
24850Sstevel@tonic-gate 	 * was set due to this call and the new state has both of them set:
24860Sstevel@tonic-gate 	 *	Send the AF_UNIX close indication
24870Sstevel@tonic-gate 	 *	For T_COTS send a discon_ind
24880Sstevel@tonic-gate 	 *
24890Sstevel@tonic-gate 	 * If cantsend was set due to this call:
24900Sstevel@tonic-gate 	 *	For T_COTSORD send an ordrel_ind
24910Sstevel@tonic-gate 	 *
24920Sstevel@tonic-gate 	 * Note that for T_CLTS there is no message sent here.
24930Sstevel@tonic-gate 	 */
24940Sstevel@tonic-gate 	if ((so->so_state & (SS_CANTRCVMORE|SS_CANTSENDMORE)) ==
24950Sstevel@tonic-gate 	    (SS_CANTRCVMORE|SS_CANTSENDMORE)) {
24960Sstevel@tonic-gate 		/*
24970Sstevel@tonic-gate 		 * For SunOS 4.X compatibility we tell the other end
24980Sstevel@tonic-gate 		 * that we are unable to receive at this point.
24990Sstevel@tonic-gate 		 */
25000Sstevel@tonic-gate 		if (so->so_family == AF_UNIX && so->so_serv_type != T_CLTS)
25010Sstevel@tonic-gate 			so_unix_close(so);
25020Sstevel@tonic-gate 
25030Sstevel@tonic-gate 		if (so->so_serv_type == T_COTS)
25040Sstevel@tonic-gate 			error = sodisconnect(so, -1, _SODISCONNECT_LOCK_HELD);
25050Sstevel@tonic-gate 	}
25060Sstevel@tonic-gate 	if ((state_change & SS_CANTSENDMORE) &&
25070Sstevel@tonic-gate 	    (so->so_serv_type == T_COTS_ORD)) {
25080Sstevel@tonic-gate 		/* Send an orderly release */
25090Sstevel@tonic-gate 		ordrel_req.PRIM_type = T_ORDREL_REQ;
25100Sstevel@tonic-gate 
25110Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
25120Sstevel@tonic-gate 		mp = soallocproto1(&ordrel_req, sizeof (ordrel_req),
25130Sstevel@tonic-gate 		    0, _ALLOC_SLEEP);
25140Sstevel@tonic-gate 		/*
25150Sstevel@tonic-gate 		 * Send down the T_ORDREL_REQ even if there is flow control.
25160Sstevel@tonic-gate 		 * This prevents shutdown from blocking.
25170Sstevel@tonic-gate 		 * Note that there is no T_OK_ACK for ordrel_req.
25180Sstevel@tonic-gate 		 */
25190Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
25200Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
25210Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
25220Sstevel@tonic-gate 		if (error) {
25230Sstevel@tonic-gate 			eprintsoline(so, error);
25240Sstevel@tonic-gate 			goto done;
25250Sstevel@tonic-gate 		}
25260Sstevel@tonic-gate 	}
25270Sstevel@tonic-gate 
25280Sstevel@tonic-gate done:
25290Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
25300Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
25310Sstevel@tonic-gate 	return (error);
25320Sstevel@tonic-gate }
25330Sstevel@tonic-gate 
25340Sstevel@tonic-gate /*
25350Sstevel@tonic-gate  * For any connected SOCK_STREAM/SOCK_SEQPACKET AF_UNIX socket we send
25360Sstevel@tonic-gate  * a zero-length T_OPTDATA_REQ with the SO_UNIX_CLOSE option to inform the peer
25370Sstevel@tonic-gate  * that we have closed.
25380Sstevel@tonic-gate  * Also, for connected AF_UNIX SOCK_DGRAM sockets we send a zero-length
25390Sstevel@tonic-gate  * T_UNITDATA_REQ containing the same option.
25400Sstevel@tonic-gate  *
25410Sstevel@tonic-gate  * For SOCK_DGRAM half-connections (somebody connected to this end
25420Sstevel@tonic-gate  * but this end is not connect) we don't know where to send any
25430Sstevel@tonic-gate  * SO_UNIX_CLOSE.
25440Sstevel@tonic-gate  *
25450Sstevel@tonic-gate  * We have to ignore stream head errors just in case there has been
25460Sstevel@tonic-gate  * a shutdown(output).
25470Sstevel@tonic-gate  * Ignore any flow control to try to get the message more quickly to the peer.
25480Sstevel@tonic-gate  * While locally ignoring flow control solves the problem when there
25490Sstevel@tonic-gate  * is only the loopback transport on the stream it would not provide
25500Sstevel@tonic-gate  * the correct AF_UNIX socket semantics when one or more modules have
25510Sstevel@tonic-gate  * been pushed.
25520Sstevel@tonic-gate  */
25530Sstevel@tonic-gate void
25540Sstevel@tonic-gate so_unix_close(struct sonode *so)
25550Sstevel@tonic-gate {
25560Sstevel@tonic-gate 	int		error;
25570Sstevel@tonic-gate 	struct T_opthdr	toh;
25580Sstevel@tonic-gate 	mblk_t		*mp;
25590Sstevel@tonic-gate 
25600Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
25610Sstevel@tonic-gate 
25620Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate 	if ((so->so_state & (SS_ISCONNECTED|SS_ISBOUND)) !=
25650Sstevel@tonic-gate 	    (SS_ISCONNECTED|SS_ISBOUND))
25660Sstevel@tonic-gate 		return;
25670Sstevel@tonic-gate 
25680Sstevel@tonic-gate 	dprintso(so, 1, ("so_unix_close(%p) %s\n",
25690Sstevel@tonic-gate 		so, pr_state(so->so_state, so->so_mode)));
25700Sstevel@tonic-gate 
25710Sstevel@tonic-gate 	toh.level = SOL_SOCKET;
25720Sstevel@tonic-gate 	toh.name = SO_UNIX_CLOSE;
25730Sstevel@tonic-gate 
25740Sstevel@tonic-gate 	/* zero length + header */
25750Sstevel@tonic-gate 	toh.len = (t_uscalar_t)sizeof (struct T_opthdr);
25760Sstevel@tonic-gate 	toh.status = 0;
25770Sstevel@tonic-gate 
25780Sstevel@tonic-gate 	if (so->so_type == SOCK_STREAM || so->so_type == SOCK_SEQPACKET) {
25790Sstevel@tonic-gate 		struct T_optdata_req tdr;
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 		tdr.PRIM_type = T_OPTDATA_REQ;
25820Sstevel@tonic-gate 		tdr.DATA_flag = 0;
25830Sstevel@tonic-gate 
25840Sstevel@tonic-gate 		tdr.OPT_length = (t_scalar_t)sizeof (toh);
25850Sstevel@tonic-gate 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
25860Sstevel@tonic-gate 
25870Sstevel@tonic-gate 		/* NOTE: holding so_lock while sleeping */
25880Sstevel@tonic-gate 		mp = soallocproto2(&tdr, sizeof (tdr),
25890Sstevel@tonic-gate 		    &toh, sizeof (toh), 0, _ALLOC_SLEEP);
25900Sstevel@tonic-gate 	} else {
25910Sstevel@tonic-gate 		struct T_unitdata_req	tudr;
25920Sstevel@tonic-gate 		void			*addr;
25930Sstevel@tonic-gate 		socklen_t		addrlen;
25940Sstevel@tonic-gate 		void			*src;
25950Sstevel@tonic-gate 		socklen_t		srclen;
25960Sstevel@tonic-gate 		struct T_opthdr		toh2;
25970Sstevel@tonic-gate 		t_scalar_t		size;
25980Sstevel@tonic-gate 
25990Sstevel@tonic-gate 		/* Connecteded DGRAM socket */
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate 		/*
26020Sstevel@tonic-gate 		 * For AF_UNIX the destination address is translated to
26030Sstevel@tonic-gate 		 * an internal name and the source address is passed as
26040Sstevel@tonic-gate 		 * an option.
26050Sstevel@tonic-gate 		 */
26060Sstevel@tonic-gate 		/*
26070Sstevel@tonic-gate 		 * Length and family checks.
26080Sstevel@tonic-gate 		 */
26090Sstevel@tonic-gate 		error = so_addr_verify(so, so->so_faddr_sa,
26100Sstevel@tonic-gate 					(t_uscalar_t)so->so_faddr_len);
26110Sstevel@tonic-gate 		if (error) {
26120Sstevel@tonic-gate 			eprintsoline(so, error);
26130Sstevel@tonic-gate 			return;
26140Sstevel@tonic-gate 		}
26150Sstevel@tonic-gate 		if (so->so_state & SS_FADDR_NOXLATE) {
26160Sstevel@tonic-gate 			/*
26170Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
26180Sstevel@tonic-gate 			 * pass any (transport internal) source address.
26190Sstevel@tonic-gate 			 */
26200Sstevel@tonic-gate 			addr = so->so_faddr_sa;
26210Sstevel@tonic-gate 			addrlen = (t_uscalar_t)so->so_faddr_len;
26220Sstevel@tonic-gate 			src = NULL;
26230Sstevel@tonic-gate 			srclen = 0;
26240Sstevel@tonic-gate 		} else {
26250Sstevel@tonic-gate 			/*
26260Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
26270Sstevel@tonic-gate 			 * and translate the remote address.
26280Sstevel@tonic-gate 			 * Holding so_lock thus so_laddr_sa can not change.
26290Sstevel@tonic-gate 			 */
26300Sstevel@tonic-gate 			src = so->so_laddr_sa;
26310Sstevel@tonic-gate 			srclen = (socklen_t)so->so_laddr_len;
26320Sstevel@tonic-gate 			dprintso(so, 1,
26330Sstevel@tonic-gate 				("so_ux_close: srclen %d, src %p\n",
26340Sstevel@tonic-gate 				srclen, src));
26350Sstevel@tonic-gate 			error = so_ux_addr_xlate(so,
26360Sstevel@tonic-gate 				so->so_faddr_sa,
26370Sstevel@tonic-gate 				(socklen_t)so->so_faddr_len, 0,
26380Sstevel@tonic-gate 				&addr, &addrlen);
26390Sstevel@tonic-gate 			if (error) {
26400Sstevel@tonic-gate 				eprintsoline(so, error);
26410Sstevel@tonic-gate 				return;
26420Sstevel@tonic-gate 			}
26430Sstevel@tonic-gate 		}
26440Sstevel@tonic-gate 		tudr.PRIM_type = T_UNITDATA_REQ;
26450Sstevel@tonic-gate 		tudr.DEST_length = addrlen;
26460Sstevel@tonic-gate 		tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
26470Sstevel@tonic-gate 		if (srclen == 0) {
26480Sstevel@tonic-gate 			tudr.OPT_length = (t_scalar_t)sizeof (toh);
26490Sstevel@tonic-gate 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
26500Sstevel@tonic-gate 				_TPI_ALIGN_TOPT(addrlen));
26510Sstevel@tonic-gate 
26520Sstevel@tonic-gate 			size = tudr.OPT_offset + tudr.OPT_length;
26530Sstevel@tonic-gate 			/* NOTE: holding so_lock while sleeping */
26540Sstevel@tonic-gate 			mp = soallocproto2(&tudr, sizeof (tudr),
26550Sstevel@tonic-gate 			    addr, addrlen, size, _ALLOC_SLEEP);
26560Sstevel@tonic-gate 			mp->b_wptr += (_TPI_ALIGN_TOPT(addrlen) - addrlen);
26570Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
26580Sstevel@tonic-gate 		} else {
26590Sstevel@tonic-gate 			/*
26600Sstevel@tonic-gate 			 * There is a AF_UNIX sockaddr_un to include as a
26610Sstevel@tonic-gate 			 * source address option.
26620Sstevel@tonic-gate 			 */
26630Sstevel@tonic-gate 			tudr.OPT_length = (t_scalar_t)(2 * sizeof (toh) +
26640Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(srclen));
26650Sstevel@tonic-gate 			tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
26660Sstevel@tonic-gate 			    _TPI_ALIGN_TOPT(addrlen));
26670Sstevel@tonic-gate 
26680Sstevel@tonic-gate 			toh2.level = SOL_SOCKET;
26690Sstevel@tonic-gate 			toh2.name = SO_SRCADDR;
26700Sstevel@tonic-gate 			toh2.len = (t_uscalar_t)(srclen +
26710Sstevel@tonic-gate 					sizeof (struct T_opthdr));
26720Sstevel@tonic-gate 			toh2.status = 0;
26730Sstevel@tonic-gate 
26740Sstevel@tonic-gate 			size = tudr.OPT_offset + tudr.OPT_length;
26750Sstevel@tonic-gate 
26760Sstevel@tonic-gate 			/* NOTE: holding so_lock while sleeping */
26770Sstevel@tonic-gate 			mp = soallocproto2(&tudr, sizeof (tudr),
26780Sstevel@tonic-gate 			    addr, addrlen, size, _ALLOC_SLEEP);
26790Sstevel@tonic-gate 			mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
26800Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
26810Sstevel@tonic-gate 			soappendmsg(mp, &toh2, sizeof (toh2));
26820Sstevel@tonic-gate 			soappendmsg(mp, src, srclen);
26830Sstevel@tonic-gate 			mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
26840Sstevel@tonic-gate 		}
26850Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
26860Sstevel@tonic-gate 	}
26870Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
26880Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
26890Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
26900Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
26910Sstevel@tonic-gate }
26920Sstevel@tonic-gate 
26930Sstevel@tonic-gate /*
26940Sstevel@tonic-gate  * Handle recv* calls that set MSG_OOB or MSG_OOB together with MSG_PEEK.
26950Sstevel@tonic-gate  */
26960Sstevel@tonic-gate int
26970Sstevel@tonic-gate sorecvoob(struct sonode *so, struct nmsghdr *msg, struct uio *uiop, int flags)
26980Sstevel@tonic-gate {
26990Sstevel@tonic-gate 	mblk_t		*mp, *nmp;
27000Sstevel@tonic-gate 	int		error;
27010Sstevel@tonic-gate 
27020Sstevel@tonic-gate 	dprintso(so, 1, ("sorecvoob(%p, %p, 0x%x)\n", so, msg, flags));
27030Sstevel@tonic-gate 
27040Sstevel@tonic-gate 	/*
27050Sstevel@tonic-gate 	 * There is never any oob data with addresses or control since
27060Sstevel@tonic-gate 	 * the T_EXDATA_IND does not carry any options.
27070Sstevel@tonic-gate 	 */
27080Sstevel@tonic-gate 	msg->msg_controllen = 0;
27090Sstevel@tonic-gate 	msg->msg_namelen = 0;
27100Sstevel@tonic-gate 
27110Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
27120Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
27130Sstevel@tonic-gate 	if ((so->so_options & SO_OOBINLINE) ||
27140Sstevel@tonic-gate 	    (so->so_state & (SS_OOBPEND|SS_HADOOBDATA)) != SS_OOBPEND) {
27150Sstevel@tonic-gate 		dprintso(so, 1, ("sorecvoob: inline or data consumed\n"));
27160Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27170Sstevel@tonic-gate 		return (EINVAL);
27180Sstevel@tonic-gate 	}
27190Sstevel@tonic-gate 	if (!(so->so_state & SS_HAVEOOBDATA)) {
27200Sstevel@tonic-gate 		dprintso(so, 1, ("sorecvoob: no data yet\n"));
27210Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
27220Sstevel@tonic-gate 		return (EWOULDBLOCK);
27230Sstevel@tonic-gate 	}
27240Sstevel@tonic-gate 	ASSERT(so->so_oobmsg != NULL);
27250Sstevel@tonic-gate 	mp = so->so_oobmsg;
27260Sstevel@tonic-gate 	if (flags & MSG_PEEK) {
27270Sstevel@tonic-gate 		/*
27280Sstevel@tonic-gate 		 * Since recv* can not return ENOBUFS we can not use dupmsg.
27290Sstevel@tonic-gate 		 * Instead we revert to the consolidation private
27300Sstevel@tonic-gate 		 * allocb_wait plus bcopy.
27310Sstevel@tonic-gate 		 */
27320Sstevel@tonic-gate 		mblk_t *mp1;
27330Sstevel@tonic-gate 
27340Sstevel@tonic-gate 		mp1 = allocb_wait(msgdsize(mp), BPRI_MED, STR_NOSIG, NULL);
27350Sstevel@tonic-gate 		ASSERT(mp1);
27360Sstevel@tonic-gate 
27370Sstevel@tonic-gate 		while (mp != NULL) {
27380Sstevel@tonic-gate 			ssize_t size;
27390Sstevel@tonic-gate 
27400Sstevel@tonic-gate 			size = MBLKL(mp);
27410Sstevel@tonic-gate 			bcopy(mp->b_rptr, mp1->b_wptr, size);
27420Sstevel@tonic-gate 			mp1->b_wptr += size;
27430Sstevel@tonic-gate 			ASSERT(mp1->b_wptr <= mp1->b_datap->db_lim);
27440Sstevel@tonic-gate 			mp = mp->b_cont;
27450Sstevel@tonic-gate 		}
27460Sstevel@tonic-gate 		mp = mp1;
27470Sstevel@tonic-gate 	} else {
27480Sstevel@tonic-gate 		/*
27490Sstevel@tonic-gate 		 * Update the state indicating that the data has been consumed.
27500Sstevel@tonic-gate 		 * Keep SS_OOBPEND set until data is consumed past the mark.
27510Sstevel@tonic-gate 		 */
27520Sstevel@tonic-gate 		so->so_oobmsg = NULL;
27530Sstevel@tonic-gate 		so->so_state ^= SS_HAVEOOBDATA|SS_HADOOBDATA;
27540Sstevel@tonic-gate 	}
27550Sstevel@tonic-gate 	dprintso(so, 1,
27560Sstevel@tonic-gate 		("after recvoob(%p): counts %d/%d state %s\n",
27570Sstevel@tonic-gate 		so, so->so_oobsigcnt,
27580Sstevel@tonic-gate 		so->so_oobcnt, pr_state(so->so_state, so->so_mode)));
27590Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
27600Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
27610Sstevel@tonic-gate 
27620Sstevel@tonic-gate 	error = 0;
27630Sstevel@tonic-gate 	nmp = mp;
27640Sstevel@tonic-gate 	while (nmp != NULL && uiop->uio_resid > 0) {
27650Sstevel@tonic-gate 		ssize_t n = MBLKL(nmp);
27660Sstevel@tonic-gate 
27670Sstevel@tonic-gate 		n = MIN(n, uiop->uio_resid);
27680Sstevel@tonic-gate 		if (n > 0)
27690Sstevel@tonic-gate 			error = uiomove(nmp->b_rptr, n,
27700Sstevel@tonic-gate 					UIO_READ, uiop);
27710Sstevel@tonic-gate 		if (error)
27720Sstevel@tonic-gate 			break;
27730Sstevel@tonic-gate 		nmp = nmp->b_cont;
27740Sstevel@tonic-gate 	}
27750Sstevel@tonic-gate 	freemsg(mp);
27760Sstevel@tonic-gate 	return (error);
27770Sstevel@tonic-gate }
27780Sstevel@tonic-gate 
27790Sstevel@tonic-gate /*
27800Sstevel@tonic-gate  * Called by sotpi_recvmsg when reading a non-zero amount of data.
27810Sstevel@tonic-gate  * In addition, the caller typically verifies that there is some
27820Sstevel@tonic-gate  * potential state to clear by checking
27830Sstevel@tonic-gate  *	if (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK))
27840Sstevel@tonic-gate  * before calling this routine.
27850Sstevel@tonic-gate  * Note that such a check can be made without holding so_lock since
27860Sstevel@tonic-gate  * sotpi_recvmsg is single-threaded (using SOREADLOCKED) and only sotpi_recvmsg
27870Sstevel@tonic-gate  * decrements so_oobsigcnt.
27880Sstevel@tonic-gate  *
27890Sstevel@tonic-gate  * When data is read *after* the point that all pending
27900Sstevel@tonic-gate  * oob data has been consumed the oob indication is cleared.
27910Sstevel@tonic-gate  *
27920Sstevel@tonic-gate  * This logic keeps select/poll returning POLLRDBAND and
27930Sstevel@tonic-gate  * SIOCATMARK returning true until we have read past
27940Sstevel@tonic-gate  * the mark.
27950Sstevel@tonic-gate  */
27960Sstevel@tonic-gate static void
27970Sstevel@tonic-gate sorecv_update_oobstate(struct sonode *so)
27980Sstevel@tonic-gate {
27990Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
28000Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
28010Sstevel@tonic-gate 	dprintso(so, 1,
28020Sstevel@tonic-gate 		("sorecv_update_oobstate: counts %d/%d state %s\n",
28030Sstevel@tonic-gate 		so->so_oobsigcnt,
28040Sstevel@tonic-gate 		so->so_oobcnt, pr_state(so->so_state, so->so_mode)));
28050Sstevel@tonic-gate 	if (so->so_oobsigcnt == 0) {
28060Sstevel@tonic-gate 		/* No more pending oob indications */
28070Sstevel@tonic-gate 		so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK);
28080Sstevel@tonic-gate 		freemsg(so->so_oobmsg);
28090Sstevel@tonic-gate 		so->so_oobmsg = NULL;
28100Sstevel@tonic-gate 	}
28110Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
28120Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
28130Sstevel@tonic-gate }
28140Sstevel@tonic-gate 
28150Sstevel@tonic-gate /*
28160Sstevel@tonic-gate  * Handle recv* calls for an so which has NL7C saved recv mblk_t(s).
28170Sstevel@tonic-gate  */
28180Sstevel@tonic-gate static int
28190Sstevel@tonic-gate nl7c_sorecv(struct sonode *so, mblk_t **rmp, uio_t *uiop, rval_t *rp)
28200Sstevel@tonic-gate {
28210Sstevel@tonic-gate 	int	error = 0;
28220Sstevel@tonic-gate 	mblk_t *tmp = NULL;
28230Sstevel@tonic-gate 	mblk_t *pmp = NULL;
28240Sstevel@tonic-gate 	mblk_t *nmp = so->so_nl7c_rcv_mp;
28250Sstevel@tonic-gate 
28260Sstevel@tonic-gate 	ASSERT(nmp != NULL);
28270Sstevel@tonic-gate 
28280Sstevel@tonic-gate 	while (nmp != NULL && uiop->uio_resid > 0) {
28290Sstevel@tonic-gate 		ssize_t n;
28300Sstevel@tonic-gate 
28310Sstevel@tonic-gate 		if (DB_TYPE(nmp) == M_DATA) {
28320Sstevel@tonic-gate 			/*
28330Sstevel@tonic-gate 			 * We have some data, uiomove up to resid bytes.
28340Sstevel@tonic-gate 			 */
28350Sstevel@tonic-gate 			n = MIN(MBLKL(nmp), uiop->uio_resid);
28360Sstevel@tonic-gate 			if (n > 0)
28370Sstevel@tonic-gate 				error = uiomove(nmp->b_rptr, n, UIO_READ, uiop);
28380Sstevel@tonic-gate 			nmp->b_rptr += n;
28390Sstevel@tonic-gate 			if (nmp->b_rptr == nmp->b_wptr) {
28400Sstevel@tonic-gate 				pmp = nmp;
28410Sstevel@tonic-gate 				nmp = nmp->b_cont;
28420Sstevel@tonic-gate 			}
28431974Sbrutus 			if (error)
28441974Sbrutus 				break;
28450Sstevel@tonic-gate 		} else {
28460Sstevel@tonic-gate 			/*
28470Sstevel@tonic-gate 			 * We only handle data, save for caller to handle.
28480Sstevel@tonic-gate 			 */
28490Sstevel@tonic-gate 			if (pmp != NULL) {
28500Sstevel@tonic-gate 				pmp->b_cont = nmp->b_cont;
28510Sstevel@tonic-gate 			}
28520Sstevel@tonic-gate 			nmp->b_cont = NULL;
28530Sstevel@tonic-gate 			if (*rmp == NULL) {
28540Sstevel@tonic-gate 				*rmp = nmp;
28550Sstevel@tonic-gate 			} else {
28561974Sbrutus 				tmp->b_cont = nmp;
28570Sstevel@tonic-gate 			}
28580Sstevel@tonic-gate 			nmp = nmp->b_cont;
28590Sstevel@tonic-gate 			tmp = nmp;
28600Sstevel@tonic-gate 		}
28610Sstevel@tonic-gate 	}
28620Sstevel@tonic-gate 	if (pmp != NULL) {
28630Sstevel@tonic-gate 		/* Free any mblk_t(s) which we have consumed */
28640Sstevel@tonic-gate 		pmp->b_cont = NULL;
28650Sstevel@tonic-gate 		freemsg(so->so_nl7c_rcv_mp);
28660Sstevel@tonic-gate 	}
28670Sstevel@tonic-gate 	if ((so->so_nl7c_rcv_mp = nmp) == NULL) {
28681974Sbrutus 		/* Last mblk_t so return the saved kstrgetmsg() rval/error */
28691974Sbrutus 		if (error == 0) {
28701974Sbrutus 			rval_t	*p = (rval_t *)&so->so_nl7c_rcv_rval;
28711974Sbrutus 
28721974Sbrutus 			error = p->r_v.r_v2;
28731974Sbrutus 			p->r_v.r_v2 = 0;
28741974Sbrutus 		}
28750Sstevel@tonic-gate 		rp->r_vals = so->so_nl7c_rcv_rval;
28760Sstevel@tonic-gate 		so->so_nl7c_rcv_rval = 0;
28770Sstevel@tonic-gate 	} else {
28780Sstevel@tonic-gate 		/* More mblk_t(s) to process so no rval to return */
28790Sstevel@tonic-gate 		rp->r_vals = 0;
28800Sstevel@tonic-gate 	}
28810Sstevel@tonic-gate 	return (error);
28820Sstevel@tonic-gate }
28830Sstevel@tonic-gate 
28840Sstevel@tonic-gate /*
28850Sstevel@tonic-gate  * Receive the next message on the queue.
28860Sstevel@tonic-gate  * If msg_controllen is non-zero when called the caller is interested in
28870Sstevel@tonic-gate  * any received control info (options).
28880Sstevel@tonic-gate  * If msg_namelen is non-zero when called the caller is interested in
28890Sstevel@tonic-gate  * any received source address.
28900Sstevel@tonic-gate  * The routine returns with msg_control and msg_name pointing to
28910Sstevel@tonic-gate  * kmem_alloc'ed memory which the caller has to free.
28920Sstevel@tonic-gate  */
28930Sstevel@tonic-gate int
28940Sstevel@tonic-gate sotpi_recvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
28950Sstevel@tonic-gate {
28960Sstevel@tonic-gate 	union T_primitives	*tpr;
28970Sstevel@tonic-gate 	mblk_t			*mp;
28980Sstevel@tonic-gate 	uchar_t			pri;
28990Sstevel@tonic-gate 	int			pflag, opflag;
29000Sstevel@tonic-gate 	void			*control;
29010Sstevel@tonic-gate 	t_uscalar_t		controllen;
29020Sstevel@tonic-gate 	t_uscalar_t		namelen;
29030Sstevel@tonic-gate 	int			so_state = so->so_state; /* Snapshot */
29040Sstevel@tonic-gate 	ssize_t			saved_resid;
29050Sstevel@tonic-gate 	int			error;
29060Sstevel@tonic-gate 	rval_t			rval;
29070Sstevel@tonic-gate 	int			flags;
29080Sstevel@tonic-gate 	clock_t			timout;
29090Sstevel@tonic-gate 	int			first;
29100Sstevel@tonic-gate 
29110Sstevel@tonic-gate 	flags = msg->msg_flags;
29120Sstevel@tonic-gate 	msg->msg_flags = 0;
29130Sstevel@tonic-gate 
29140Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg(%p, %p, 0x%x) state %s err %d\n",
29150Sstevel@tonic-gate 		so, msg, flags,
29160Sstevel@tonic-gate 		pr_state(so->so_state, so->so_mode), so->so_error));
29170Sstevel@tonic-gate 
29180Sstevel@tonic-gate 	/*
29190Sstevel@tonic-gate 	 * If we are not connected because we have never been connected
29200Sstevel@tonic-gate 	 * we return ENOTCONN. If we have been connected (but are no longer
29210Sstevel@tonic-gate 	 * connected) then SS_CANTRCVMORE is set and we let kstrgetmsg return
29220Sstevel@tonic-gate 	 * the EOF.
29230Sstevel@tonic-gate 	 *
29240Sstevel@tonic-gate 	 * An alternative would be to post an ENOTCONN error in stream head
29250Sstevel@tonic-gate 	 * (read+write) and clear it when we're connected. However, that error
29260Sstevel@tonic-gate 	 * would cause incorrect poll/select behavior!
29270Sstevel@tonic-gate 	 */
29280Sstevel@tonic-gate 	if ((so_state & (SS_ISCONNECTED|SS_CANTRCVMORE)) == 0 &&
29290Sstevel@tonic-gate 	    (so->so_mode & SM_CONNREQUIRED)) {
29300Sstevel@tonic-gate 		return (ENOTCONN);
29310Sstevel@tonic-gate 	}
29320Sstevel@tonic-gate 
29330Sstevel@tonic-gate 	/*
29340Sstevel@tonic-gate 	 * Note: SunOS 4.X checks uio_resid == 0 before going to sleep (but
29350Sstevel@tonic-gate 	 * after checking that the read queue is empty) and returns zero.
29360Sstevel@tonic-gate 	 * This implementation will sleep (in kstrgetmsg) even if uio_resid
29370Sstevel@tonic-gate 	 * is zero.
29380Sstevel@tonic-gate 	 */
29390Sstevel@tonic-gate 
29400Sstevel@tonic-gate 	if (flags & MSG_OOB) {
29410Sstevel@tonic-gate 		/* Check that the transport supports OOB */
29420Sstevel@tonic-gate 		if (!(so->so_mode & SM_EXDATA))
29430Sstevel@tonic-gate 			return (EOPNOTSUPP);
29440Sstevel@tonic-gate 		return (sorecvoob(so, msg, uiop, flags));
29450Sstevel@tonic-gate 	}
29460Sstevel@tonic-gate 
29470Sstevel@tonic-gate 	/*
29480Sstevel@tonic-gate 	 * Set msg_controllen and msg_namelen to zero here to make it
29490Sstevel@tonic-gate 	 * simpler in the cases that no control or name is returned.
29500Sstevel@tonic-gate 	 */
29510Sstevel@tonic-gate 	controllen = msg->msg_controllen;
29520Sstevel@tonic-gate 	namelen = msg->msg_namelen;
29530Sstevel@tonic-gate 	msg->msg_controllen = 0;
29540Sstevel@tonic-gate 	msg->msg_namelen = 0;
29550Sstevel@tonic-gate 
29560Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg: namelen %d controllen %d\n",
29570Sstevel@tonic-gate 		namelen, controllen));
29580Sstevel@tonic-gate 
29591974Sbrutus 	mutex_enter(&so->so_lock);
29600Sstevel@tonic-gate 	/*
29610Sstevel@tonic-gate 	 * If an NL7C enabled socket and not waiting for write data.
29620Sstevel@tonic-gate 	 */
29631974Sbrutus 	if ((so->so_nl7c_flags & (NL7C_ENABLED | NL7C_WAITWRITE)) ==
29640Sstevel@tonic-gate 	    NL7C_ENABLED) {
29650Sstevel@tonic-gate 		if (so->so_nl7c_uri) {
29661974Sbrutus 			/* Close uri processing for a previous request */
29670Sstevel@tonic-gate 			nl7c_close(so);
29680Sstevel@tonic-gate 		}
29691974Sbrutus 		if ((so_state & SS_CANTRCVMORE) && so->so_nl7c_rcv_mp == NULL) {
29701974Sbrutus 			/* Nothing to process, EOF */
29711974Sbrutus 			mutex_exit(&so->so_lock);
29721974Sbrutus 			return (0);
29731974Sbrutus 		} else if (so->so_nl7c_flags & NL7C_SOPERSIST) {
29741974Sbrutus 			/* Persistent NL7C socket, try to process request */
29751974Sbrutus 			boolean_t ret;
29761974Sbrutus 
29771974Sbrutus 			ret = nl7c_process(so,
29781974Sbrutus 			    (so->so_state & (SS_NONBLOCK|SS_NDELAY)));
29791974Sbrutus 			rval.r_vals = so->so_nl7c_rcv_rval;
29801974Sbrutus 			error = rval.r_v.r_v2;
29811974Sbrutus 			if (error) {
29821974Sbrutus 				/* Error of some sort, return it */
29831974Sbrutus 				mutex_exit(&so->so_lock);
29841974Sbrutus 				return (error);
29851974Sbrutus 			}
29861974Sbrutus 			if (so->so_nl7c_flags &&
29871974Sbrutus 			    ! (so->so_nl7c_flags & NL7C_WAITWRITE)) {
29881974Sbrutus 				/*
29891974Sbrutus 				 * Still an NL7C socket and no data
29901974Sbrutus 				 * to pass up to the caller.
29911974Sbrutus 				 */
29921974Sbrutus 				mutex_exit(&so->so_lock);
29931974Sbrutus 				if (ret) {
29941974Sbrutus 					/* EOF */
29951974Sbrutus 					return (0);
29961974Sbrutus 				} else {
29971974Sbrutus 					/* Need more data */
29981974Sbrutus 					return (EAGAIN);
29991974Sbrutus 				}
30001974Sbrutus 			}
30011974Sbrutus 		} else {
30020Sstevel@tonic-gate 			/*
30031974Sbrutus 			 * Not persistent so no further NL7C processing.
30040Sstevel@tonic-gate 			 */
30050Sstevel@tonic-gate 			so->so_nl7c_flags = 0;
30060Sstevel@tonic-gate 		}
30070Sstevel@tonic-gate 	}
30080Sstevel@tonic-gate 	/*
30090Sstevel@tonic-gate 	 * Only one reader is allowed at any given time. This is needed
30100Sstevel@tonic-gate 	 * for T_EXDATA handling and, in the future, MSG_WAITALL.
30110Sstevel@tonic-gate 	 *
30120Sstevel@tonic-gate 	 * This is slightly different that BSD behavior in that it fails with
30130Sstevel@tonic-gate 	 * EWOULDBLOCK when using nonblocking io. In BSD the read queue access
30140Sstevel@tonic-gate 	 * is single-threaded using sblock(), which is dropped while waiting
30150Sstevel@tonic-gate 	 * for data to appear. The difference shows up e.g. if one
30160Sstevel@tonic-gate 	 * file descriptor does not have O_NONBLOCK but a dup'ed file descriptor
30170Sstevel@tonic-gate 	 * does use nonblocking io and different threads are reading each
30180Sstevel@tonic-gate 	 * file descriptor. In BSD there would never be an EWOULDBLOCK error
30190Sstevel@tonic-gate 	 * in this case as long as the read queue doesn't get empty.
30200Sstevel@tonic-gate 	 * In this implementation the thread using nonblocking io can
30210Sstevel@tonic-gate 	 * get an EWOULDBLOCK error due to the blocking thread executing
30220Sstevel@tonic-gate 	 * e.g. in the uiomove in kstrgetmsg.
30230Sstevel@tonic-gate 	 * This difference is not believed to be significant.
30240Sstevel@tonic-gate 	 */
30253749Sethindra 	/* Set SOREADLOCKED */
30263749Sethindra 	error = so_lock_read_intr(so,
30273749Sethindra 	    uiop->uio_fmode | ((flags & MSG_DONTWAIT) ? FNONBLOCK : 0));
30280Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
30290Sstevel@tonic-gate 	if (error)
30300Sstevel@tonic-gate 		return (error);
30310Sstevel@tonic-gate 
30320Sstevel@tonic-gate 	/*
30330Sstevel@tonic-gate 	 * Tell kstrgetmsg to not inspect the stream head errors until all
30340Sstevel@tonic-gate 	 * queued data has been consumed.
30350Sstevel@tonic-gate 	 * Use a timeout=-1 to wait forever unless MSG_DONTWAIT is set.
30360Sstevel@tonic-gate 	 * Also, If uio_fmode indicates nonblocking kstrgetmsg will not block.
30370Sstevel@tonic-gate 	 *
30380Sstevel@tonic-gate 	 * MSG_WAITALL only applies to M_DATA and T_DATA_IND messages and
30390Sstevel@tonic-gate 	 * to T_OPTDATA_IND that do not contain any user-visible control msg.
30400Sstevel@tonic-gate 	 * Note that MSG_WAITALL set with MSG_PEEK is a noop.
30410Sstevel@tonic-gate 	 */
30420Sstevel@tonic-gate 	pflag = MSG_ANY | MSG_DELAYERROR;
30430Sstevel@tonic-gate 	if (flags & MSG_PEEK) {
30440Sstevel@tonic-gate 		pflag |= MSG_IPEEK;
30450Sstevel@tonic-gate 		flags &= ~MSG_WAITALL;
30460Sstevel@tonic-gate 	}
30470Sstevel@tonic-gate 	if (so->so_mode & SM_ATOMIC)
30480Sstevel@tonic-gate 		pflag |= MSG_DISCARDTAIL;
30490Sstevel@tonic-gate 
30500Sstevel@tonic-gate 	if (flags & MSG_DONTWAIT)
30510Sstevel@tonic-gate 		timout = 0;
30520Sstevel@tonic-gate 	else
30530Sstevel@tonic-gate 		timout = -1;
30540Sstevel@tonic-gate 	opflag = pflag;
30550Sstevel@tonic-gate 	first = 1;
30560Sstevel@tonic-gate 
30570Sstevel@tonic-gate retry:
30580Sstevel@tonic-gate 	saved_resid = uiop->uio_resid;
30590Sstevel@tonic-gate 	pri = 0;
30600Sstevel@tonic-gate 	mp = NULL;
30610Sstevel@tonic-gate 	if (so->so_nl7c_rcv_mp != NULL) {
30621974Sbrutus 		/* Already kstrgetmsg()ed saved mblk(s) from NL7C */
30630Sstevel@tonic-gate 		error = nl7c_sorecv(so, &mp, uiop, &rval);
30640Sstevel@tonic-gate 	} else {
30650Sstevel@tonic-gate 		error = kstrgetmsg(SOTOV(so), &mp, uiop, &pri, &pflag,
30660Sstevel@tonic-gate 		    timout, &rval);
30670Sstevel@tonic-gate 	}
30680Sstevel@tonic-gate 	if (error) {
30690Sstevel@tonic-gate 		switch (error) {
30700Sstevel@tonic-gate 		case EINTR:
30710Sstevel@tonic-gate 		case EWOULDBLOCK:
30720Sstevel@tonic-gate 			if (!first)
30730Sstevel@tonic-gate 				error = 0;
30740Sstevel@tonic-gate 			break;
30750Sstevel@tonic-gate 		case ETIME:
30760Sstevel@tonic-gate 			/* Returned from kstrgetmsg when timeout expires */
30770Sstevel@tonic-gate 			if (!first)
30780Sstevel@tonic-gate 				error = 0;
30790Sstevel@tonic-gate 			else
30800Sstevel@tonic-gate 				error = EWOULDBLOCK;
30810Sstevel@tonic-gate 			break;
30820Sstevel@tonic-gate 		default:
30830Sstevel@tonic-gate 			eprintsoline(so, error);
30840Sstevel@tonic-gate 			break;
30850Sstevel@tonic-gate 		}
30860Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
30870Sstevel@tonic-gate 		so_unlock_read(so);	/* Clear SOREADLOCKED */
30880Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
30890Sstevel@tonic-gate 		return (error);
30900Sstevel@tonic-gate 	}
30910Sstevel@tonic-gate 	/*
30920Sstevel@tonic-gate 	 * For datagrams the MOREDATA flag is used to set MSG_TRUNC.
30930Sstevel@tonic-gate 	 * For non-datagrams MOREDATA is used to set MSG_EOR.
30940Sstevel@tonic-gate 	 */
30950Sstevel@tonic-gate 	ASSERT(!(rval.r_val1 & MORECTL));
30960Sstevel@tonic-gate 	if ((rval.r_val1 & MOREDATA) && (so->so_mode & SM_ATOMIC))
30970Sstevel@tonic-gate 		msg->msg_flags |= MSG_TRUNC;
30980Sstevel@tonic-gate 
30990Sstevel@tonic-gate 	if (mp == NULL) {
31000Sstevel@tonic-gate 		dprintso(so, 1, ("sotpi_recvmsg: got M_DATA\n"));
31010Sstevel@tonic-gate 		/*
31020Sstevel@tonic-gate 		 * 4.3BSD and 4.4BSD clears the mark when peeking across it.
31030Sstevel@tonic-gate 		 * The draft Posix socket spec states that the mark should
31040Sstevel@tonic-gate 		 * not be cleared when peeking. We follow the latter.
31050Sstevel@tonic-gate 		 */
31060Sstevel@tonic-gate 		if ((so->so_state &
31070Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
31080Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
31090Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
31100Sstevel@tonic-gate 			sorecv_update_oobstate(so);
31110Sstevel@tonic-gate 		}
31120Sstevel@tonic-gate 
31130Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
31140Sstevel@tonic-gate 		/* Set MSG_EOR based on MOREDATA */
31150Sstevel@tonic-gate 		if (!(rval.r_val1 & MOREDATA)) {
31160Sstevel@tonic-gate 			if (so->so_state & SS_SAVEDEOR) {
31170Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
31180Sstevel@tonic-gate 				so->so_state &= ~SS_SAVEDEOR;
31190Sstevel@tonic-gate 			}
31200Sstevel@tonic-gate 		}
31210Sstevel@tonic-gate 		/*
31220Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
31230Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
31240Sstevel@tonic-gate 		 */
31250Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
31260Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
31270Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
31280Sstevel@tonic-gate 			first = 0;
31290Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
31300Sstevel@tonic-gate 			goto retry;
31310Sstevel@tonic-gate 		}
31320Sstevel@tonic-gate 		so_unlock_read(so);	/* Clear SOREADLOCKED */
31330Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
31340Sstevel@tonic-gate 		return (0);
31350Sstevel@tonic-gate 	}
31360Sstevel@tonic-gate 
31370Sstevel@tonic-gate 	/* strsock_proto has already verified length and alignment */
31380Sstevel@tonic-gate 	tpr = (union T_primitives *)mp->b_rptr;
31390Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_recvmsg: type %d\n", tpr->type));
31400Sstevel@tonic-gate 
31410Sstevel@tonic-gate 	switch (tpr->type) {
31420Sstevel@tonic-gate 	case T_DATA_IND: {
31430Sstevel@tonic-gate 		if ((so->so_state &
31440Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
31450Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
31460Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
31470Sstevel@tonic-gate 			sorecv_update_oobstate(so);
31480Sstevel@tonic-gate 		}
31490Sstevel@tonic-gate 
31500Sstevel@tonic-gate 		/*
31510Sstevel@tonic-gate 		 * Set msg_flags to MSG_EOR based on
31520Sstevel@tonic-gate 		 * MORE_flag and MOREDATA.
31530Sstevel@tonic-gate 		 */
31540Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
31550Sstevel@tonic-gate 		so->so_state &= ~SS_SAVEDEOR;
31560Sstevel@tonic-gate 		if (!(tpr->data_ind.MORE_flag & 1)) {
31570Sstevel@tonic-gate 			if (!(rval.r_val1 & MOREDATA))
31580Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
31590Sstevel@tonic-gate 			else
31600Sstevel@tonic-gate 				so->so_state |= SS_SAVEDEOR;
31610Sstevel@tonic-gate 		}
31620Sstevel@tonic-gate 		freemsg(mp);
31630Sstevel@tonic-gate 		/*
31640Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
31650Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
31660Sstevel@tonic-gate 		 */
31670Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
31680Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
31690Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
31700Sstevel@tonic-gate 			first = 0;
31710Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
31720Sstevel@tonic-gate 			goto retry;
31730Sstevel@tonic-gate 		}
31740Sstevel@tonic-gate 		so_unlock_read(so);	/* Clear SOREADLOCKED */
31750Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
31760Sstevel@tonic-gate 		return (0);
31770Sstevel@tonic-gate 	}
31780Sstevel@tonic-gate 	case T_UNITDATA_IND: {
31790Sstevel@tonic-gate 		void *addr;
31800Sstevel@tonic-gate 		t_uscalar_t addrlen;
31810Sstevel@tonic-gate 		void *abuf;
31820Sstevel@tonic-gate 		t_uscalar_t optlen;
31830Sstevel@tonic-gate 		void *opt;
31840Sstevel@tonic-gate 
31850Sstevel@tonic-gate 		if ((so->so_state &
31860Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
31870Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
31880Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
31890Sstevel@tonic-gate 			sorecv_update_oobstate(so);
31900Sstevel@tonic-gate 		}
31910Sstevel@tonic-gate 
31920Sstevel@tonic-gate 		if (namelen != 0) {
31930Sstevel@tonic-gate 			/* Caller wants source address */
31940Sstevel@tonic-gate 			addrlen = tpr->unitdata_ind.SRC_length;
31950Sstevel@tonic-gate 			addr = sogetoff(mp,
31960Sstevel@tonic-gate 				tpr->unitdata_ind.SRC_offset,
31970Sstevel@tonic-gate 				addrlen, 1);
31980Sstevel@tonic-gate 			if (addr == NULL) {
31990Sstevel@tonic-gate 				freemsg(mp);
32000Sstevel@tonic-gate 				error = EPROTO;
32010Sstevel@tonic-gate 				eprintsoline(so, error);
32020Sstevel@tonic-gate 				goto err;
32030Sstevel@tonic-gate 			}
32040Sstevel@tonic-gate 			if (so->so_family == AF_UNIX) {
32050Sstevel@tonic-gate 				/*
32060Sstevel@tonic-gate 				 * Can not use the transport level address.
32070Sstevel@tonic-gate 				 * If there is a SO_SRCADDR option carrying
32080Sstevel@tonic-gate 				 * the socket level address it will be
32090Sstevel@tonic-gate 				 * extracted below.
32100Sstevel@tonic-gate 				 */
32110Sstevel@tonic-gate 				addr = NULL;
32120Sstevel@tonic-gate 				addrlen = 0;
32130Sstevel@tonic-gate 			}
32140Sstevel@tonic-gate 		}
32150Sstevel@tonic-gate 		optlen = tpr->unitdata_ind.OPT_length;
32160Sstevel@tonic-gate 		if (optlen != 0) {
32170Sstevel@tonic-gate 			t_uscalar_t ncontrollen;
32180Sstevel@tonic-gate 
32190Sstevel@tonic-gate 			/*
32200Sstevel@tonic-gate 			 * Extract any source address option.
32210Sstevel@tonic-gate 			 * Determine how large cmsg buffer is needed.
32220Sstevel@tonic-gate 			 */
32230Sstevel@tonic-gate 			opt = sogetoff(mp,
32240Sstevel@tonic-gate 				tpr->unitdata_ind.OPT_offset,
32250Sstevel@tonic-gate 				optlen, __TPI_ALIGN_SIZE);
32260Sstevel@tonic-gate 
32270Sstevel@tonic-gate 			if (opt == NULL) {
32280Sstevel@tonic-gate 				freemsg(mp);
32290Sstevel@tonic-gate 				error = EPROTO;
32300Sstevel@tonic-gate 				eprintsoline(so, error);
32310Sstevel@tonic-gate 				goto err;
32320Sstevel@tonic-gate 			}
32330Sstevel@tonic-gate 			if (so->so_family == AF_UNIX)
32340Sstevel@tonic-gate 				so_getopt_srcaddr(opt, optlen, &addr, &addrlen);
32350Sstevel@tonic-gate 			ncontrollen = so_cmsglen(mp, opt, optlen,
32360Sstevel@tonic-gate 						!(flags & MSG_XPG4_2));
32370Sstevel@tonic-gate 			if (controllen != 0)
32380Sstevel@tonic-gate 				controllen = ncontrollen;
32390Sstevel@tonic-gate 			else if (ncontrollen != 0)
32400Sstevel@tonic-gate 				msg->msg_flags |= MSG_CTRUNC;
32410Sstevel@tonic-gate 		} else {
32420Sstevel@tonic-gate 			controllen = 0;
32430Sstevel@tonic-gate 		}
32440Sstevel@tonic-gate 
32450Sstevel@tonic-gate 		if (namelen != 0) {
32460Sstevel@tonic-gate 			/*
32470Sstevel@tonic-gate 			 * Return address to caller.
32480Sstevel@tonic-gate 			 * Caller handles truncation if length
32490Sstevel@tonic-gate 			 * exceeds msg_namelen.
32500Sstevel@tonic-gate 			 * NOTE: AF_UNIX NUL termination is ensured by
32510Sstevel@tonic-gate 			 * the sender's copyin_name().
32520Sstevel@tonic-gate 			 */
32530Sstevel@tonic-gate 			abuf = kmem_alloc(addrlen, KM_SLEEP);
32540Sstevel@tonic-gate 
32550Sstevel@tonic-gate 			bcopy(addr, abuf, addrlen);
32560Sstevel@tonic-gate 			msg->msg_name = abuf;
32570Sstevel@tonic-gate 			msg->msg_namelen = addrlen;
32580Sstevel@tonic-gate 		}
32590Sstevel@tonic-gate 
32600Sstevel@tonic-gate 		if (controllen != 0) {
32610Sstevel@tonic-gate 			/*
32620Sstevel@tonic-gate 			 * Return control msg to caller.
32630Sstevel@tonic-gate 			 * Caller handles truncation if length
32640Sstevel@tonic-gate 			 * exceeds msg_controllen.
32650Sstevel@tonic-gate 			 */
32660Sstevel@tonic-gate 			control = kmem_alloc(controllen, KM_SLEEP);
32670Sstevel@tonic-gate 
32680Sstevel@tonic-gate 			error = so_opt2cmsg(mp, opt, optlen,
32690Sstevel@tonic-gate 					!(flags & MSG_XPG4_2),
32700Sstevel@tonic-gate 					control, controllen);
32710Sstevel@tonic-gate 			if (error) {
32720Sstevel@tonic-gate 				freemsg(mp);
32730Sstevel@tonic-gate 				if (msg->msg_namelen != 0)
32740Sstevel@tonic-gate 					kmem_free(msg->msg_name,
32750Sstevel@tonic-gate 						msg->msg_namelen);
32760Sstevel@tonic-gate 				kmem_free(control, controllen);
32770Sstevel@tonic-gate 				eprintsoline(so, error);
32780Sstevel@tonic-gate 				goto err;
32790Sstevel@tonic-gate 			}
32800Sstevel@tonic-gate 			msg->msg_control = control;
32810Sstevel@tonic-gate 			msg->msg_controllen = controllen;
32820Sstevel@tonic-gate 		}
32830Sstevel@tonic-gate 
32840Sstevel@tonic-gate 		freemsg(mp);
32850Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
32860Sstevel@tonic-gate 		so_unlock_read(so);	/* Clear SOREADLOCKED */
32870Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
32880Sstevel@tonic-gate 		return (0);
32890Sstevel@tonic-gate 	}
32900Sstevel@tonic-gate 	case T_OPTDATA_IND: {
32910Sstevel@tonic-gate 		struct T_optdata_req *tdr;
32920Sstevel@tonic-gate 		void *opt;
32930Sstevel@tonic-gate 		t_uscalar_t optlen;
32940Sstevel@tonic-gate 
32950Sstevel@tonic-gate 		if ((so->so_state &
32960Sstevel@tonic-gate 		    (SS_OOBPEND|SS_HAVEOOBDATA|SS_RCVATMARK)) &&
32970Sstevel@tonic-gate 		    (uiop->uio_resid != saved_resid) &&
32980Sstevel@tonic-gate 		    !(flags & MSG_PEEK)) {
32990Sstevel@tonic-gate 			sorecv_update_oobstate(so);
33000Sstevel@tonic-gate 		}
33010Sstevel@tonic-gate 
33020Sstevel@tonic-gate 		tdr = (struct T_optdata_req *)mp->b_rptr;
33030Sstevel@tonic-gate 		optlen = tdr->OPT_length;
33040Sstevel@tonic-gate 		if (optlen != 0) {
33050Sstevel@tonic-gate 			t_uscalar_t ncontrollen;
33060Sstevel@tonic-gate 			/*
33070Sstevel@tonic-gate 			 * Determine how large cmsg buffer is needed.
33080Sstevel@tonic-gate 			 */
33090Sstevel@tonic-gate 			opt = sogetoff(mp,
33100Sstevel@tonic-gate 					tpr->optdata_ind.OPT_offset,
33110Sstevel@tonic-gate 					optlen, __TPI_ALIGN_SIZE);
33120Sstevel@tonic-gate 
33130Sstevel@tonic-gate 			if (opt == NULL) {
33140Sstevel@tonic-gate 				freemsg(mp);
33150Sstevel@tonic-gate 				error = EPROTO;
33160Sstevel@tonic-gate 				eprintsoline(so, error);
33170Sstevel@tonic-gate 				goto err;
33180Sstevel@tonic-gate 			}
33190Sstevel@tonic-gate 
33200Sstevel@tonic-gate 			ncontrollen = so_cmsglen(mp, opt, optlen,
33210Sstevel@tonic-gate 						!(flags & MSG_XPG4_2));
33220Sstevel@tonic-gate 			if (controllen != 0)
33230Sstevel@tonic-gate 				controllen = ncontrollen;
33240Sstevel@tonic-gate 			else if (ncontrollen != 0)
33250Sstevel@tonic-gate 				msg->msg_flags |= MSG_CTRUNC;
33260Sstevel@tonic-gate 		} else {
33270Sstevel@tonic-gate 			controllen = 0;
33280Sstevel@tonic-gate 		}
33290Sstevel@tonic-gate 
33300Sstevel@tonic-gate 		if (controllen != 0) {
33310Sstevel@tonic-gate 			/*
33320Sstevel@tonic-gate 			 * Return control msg to caller.
33330Sstevel@tonic-gate 			 * Caller handles truncation if length
33340Sstevel@tonic-gate 			 * exceeds msg_controllen.
33350Sstevel@tonic-gate 			 */
33360Sstevel@tonic-gate 			control = kmem_alloc(controllen, KM_SLEEP);
33370Sstevel@tonic-gate 
33380Sstevel@tonic-gate 			error = so_opt2cmsg(mp, opt, optlen,
33390Sstevel@tonic-gate 					!(flags & MSG_XPG4_2),
33400Sstevel@tonic-gate 					control, controllen);
33410Sstevel@tonic-gate 			if (error) {
33420Sstevel@tonic-gate 				freemsg(mp);
33430Sstevel@tonic-gate 				kmem_free(control, controllen);
33440Sstevel@tonic-gate 				eprintsoline(so, error);
33450Sstevel@tonic-gate 				goto err;
33460Sstevel@tonic-gate 			}
33470Sstevel@tonic-gate 			msg->msg_control = control;
33480Sstevel@tonic-gate 			msg->msg_controllen = controllen;
33490Sstevel@tonic-gate 		}
33500Sstevel@tonic-gate 
33510Sstevel@tonic-gate 		/*
33520Sstevel@tonic-gate 		 * Set msg_flags to MSG_EOR based on
33530Sstevel@tonic-gate 		 * DATA_flag and MOREDATA.
33540Sstevel@tonic-gate 		 */
33550Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
33560Sstevel@tonic-gate 		so->so_state &= ~SS_SAVEDEOR;
33570Sstevel@tonic-gate 		if (!(tpr->data_ind.MORE_flag & 1)) {
33580Sstevel@tonic-gate 			if (!(rval.r_val1 & MOREDATA))
33590Sstevel@tonic-gate 				msg->msg_flags |= MSG_EOR;
33600Sstevel@tonic-gate 			else
33610Sstevel@tonic-gate 				so->so_state |= SS_SAVEDEOR;
33620Sstevel@tonic-gate 		}
33630Sstevel@tonic-gate 		freemsg(mp);
33640Sstevel@tonic-gate 		/*
33650Sstevel@tonic-gate 		 * If some data was received (i.e. not EOF) and the
33660Sstevel@tonic-gate 		 * read/recv* has not been satisfied wait for some more.
33670Sstevel@tonic-gate 		 * Not possible to wait if control info was received.
33680Sstevel@tonic-gate 		 */
33690Sstevel@tonic-gate 		if ((flags & MSG_WAITALL) && !(msg->msg_flags & MSG_EOR) &&
33700Sstevel@tonic-gate 		    controllen == 0 &&
33710Sstevel@tonic-gate 		    uiop->uio_resid != saved_resid && uiop->uio_resid > 0) {
33720Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
33730Sstevel@tonic-gate 			first = 0;
33740Sstevel@tonic-gate 			pflag = opflag | MSG_NOMARK;
33750Sstevel@tonic-gate 			goto retry;
33760Sstevel@tonic-gate 		}
33770Sstevel@tonic-gate 		so_unlock_read(so);	/* Clear SOREADLOCKED */
33780Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
33790Sstevel@tonic-gate 		return (0);
33800Sstevel@tonic-gate 	}
33810Sstevel@tonic-gate 	case T_EXDATA_IND: {
33820Sstevel@tonic-gate 		dprintso(so, 1,
33830Sstevel@tonic-gate 			("sotpi_recvmsg: EXDATA_IND counts %d/%d consumed %ld "
33840Sstevel@tonic-gate 			"state %s\n",
33850Sstevel@tonic-gate 			so->so_oobsigcnt, so->so_oobcnt,
33860Sstevel@tonic-gate 			saved_resid - uiop->uio_resid,
33870Sstevel@tonic-gate 			pr_state(so->so_state, so->so_mode)));
33880Sstevel@tonic-gate 		/*
33890Sstevel@tonic-gate 		 * kstrgetmsg handles MSGMARK so there is nothing to
33900Sstevel@tonic-gate 		 * inspect in the T_EXDATA_IND.
33910Sstevel@tonic-gate 		 * strsock_proto makes the stream head queue the T_EXDATA_IND
33920Sstevel@tonic-gate 		 * as a separate message with no M_DATA component. Furthermore,
33930Sstevel@tonic-gate 		 * the stream head does not consolidate M_DATA messages onto
33940Sstevel@tonic-gate 		 * an MSGMARK'ed message ensuring that the T_EXDATA_IND
33950Sstevel@tonic-gate 		 * remains a message by itself. This is needed since MSGMARK
33960Sstevel@tonic-gate 		 * marks both the whole message as well as the last byte
33970Sstevel@tonic-gate 		 * of the message.
33980Sstevel@tonic-gate 		 */
33990Sstevel@tonic-gate 		freemsg(mp);
34000Sstevel@tonic-gate 		ASSERT(uiop->uio_resid == saved_resid);	/* No data */
34010Sstevel@tonic-gate 		if (flags & MSG_PEEK) {
34020Sstevel@tonic-gate 			/*
34030Sstevel@tonic-gate 			 * Even though we are peeking we consume the
34040Sstevel@tonic-gate 			 * T_EXDATA_IND thereby moving the mark information
34050Sstevel@tonic-gate 			 * to SS_RCVATMARK. Then the oob code below will
34060Sstevel@tonic-gate 			 * retry the peeking kstrgetmsg.
34070Sstevel@tonic-gate 			 * Note that the stream head read queue is
34080Sstevel@tonic-gate 			 * never flushed without holding SOREADLOCKED
34090Sstevel@tonic-gate 			 * thus the T_EXDATA_IND can not disappear
34100Sstevel@tonic-gate 			 * underneath us.
34110Sstevel@tonic-gate 			 */
34120Sstevel@tonic-gate 			dprintso(so, 1,
34130Sstevel@tonic-gate 				("sotpi_recvmsg: consume EXDATA_IND "
34140Sstevel@tonic-gate 				"counts %d/%d state %s\n",
34150Sstevel@tonic-gate 				so->so_oobsigcnt,
34160Sstevel@tonic-gate 				so->so_oobcnt,
34170Sstevel@tonic-gate 				pr_state(so->so_state, so->so_mode)));
34180Sstevel@tonic-gate 
34190Sstevel@tonic-gate 			pflag = MSG_ANY | MSG_DELAYERROR;
34200Sstevel@tonic-gate 			if (so->so_mode & SM_ATOMIC)
34210Sstevel@tonic-gate 				pflag |= MSG_DISCARDTAIL;
34220Sstevel@tonic-gate 
34230Sstevel@tonic-gate 			pri = 0;
34240Sstevel@tonic-gate 			mp = NULL;
34250Sstevel@tonic-gate 
34260Sstevel@tonic-gate 			error = kstrgetmsg(SOTOV(so), &mp, uiop,
34270Sstevel@tonic-gate 				&pri, &pflag, (clock_t)-1, &rval);
34280Sstevel@tonic-gate 			ASSERT(uiop->uio_resid == saved_resid);
34290Sstevel@tonic-gate 
34300Sstevel@tonic-gate 			if (error) {
34310Sstevel@tonic-gate #ifdef SOCK_DEBUG
34320Sstevel@tonic-gate 				if (error != EWOULDBLOCK && error != EINTR) {
34330Sstevel@tonic-gate 					eprintsoline(so, error);
34340Sstevel@tonic-gate 				}
34350Sstevel@tonic-gate #endif /* SOCK_DEBUG */
34360Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
34370Sstevel@tonic-gate 				so_unlock_read(so);	/* Clear SOREADLOCKED */
34380Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
34390Sstevel@tonic-gate 				return (error);
34400Sstevel@tonic-gate 			}
34410Sstevel@tonic-gate 			ASSERT(mp);
34420Sstevel@tonic-gate 			tpr = (union T_primitives *)mp->b_rptr;
34430Sstevel@tonic-gate 			ASSERT(tpr->type == T_EXDATA_IND);
34440Sstevel@tonic-gate 			freemsg(mp);
34450Sstevel@tonic-gate 		} /* end "if (flags & MSG_PEEK)" */
34460Sstevel@tonic-gate 
34470Sstevel@tonic-gate 		/*
34480Sstevel@tonic-gate 		 * Decrement the number of queued and pending oob.
34490Sstevel@tonic-gate 		 *
34500Sstevel@tonic-gate 		 * SS_RCVATMARK is cleared when we read past a mark.
34510Sstevel@tonic-gate 		 * SS_HAVEOOBDATA is cleared when we've read past the
34520Sstevel@tonic-gate 		 * last mark.
34530Sstevel@tonic-gate 		 * SS_OOBPEND is cleared if we've read past the last
34540Sstevel@tonic-gate 		 * mark and no (new) SIGURG has been posted.
34550Sstevel@tonic-gate 		 */
34560Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
34570Sstevel@tonic-gate 		ASSERT(so_verify_oobstate(so));
34580Sstevel@tonic-gate 		ASSERT(so->so_oobsigcnt >= so->so_oobcnt);
34590Sstevel@tonic-gate 		ASSERT(so->so_oobsigcnt > 0);
34600Sstevel@tonic-gate 		so->so_oobsigcnt--;
34610Sstevel@tonic-gate 		ASSERT(so->so_oobcnt > 0);
34620Sstevel@tonic-gate 		so->so_oobcnt--;
34630Sstevel@tonic-gate 		/*
34640Sstevel@tonic-gate 		 * Since the T_EXDATA_IND has been removed from the stream
34650Sstevel@tonic-gate 		 * head, but we have not read data past the mark,
34660Sstevel@tonic-gate 		 * sockfs needs to track that the socket is still at the mark.
34670Sstevel@tonic-gate 		 *
34680Sstevel@tonic-gate 		 * Since no data was received call kstrgetmsg again to wait
34690Sstevel@tonic-gate 		 * for data.
34700Sstevel@tonic-gate 		 */
34710Sstevel@tonic-gate 		so->so_state |= SS_RCVATMARK;
34720Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
34730Sstevel@tonic-gate 		dprintso(so, 1,
34740Sstevel@tonic-gate 		    ("sotpi_recvmsg: retry EXDATA_IND counts %d/%d state %s\n",
34750Sstevel@tonic-gate 		    so->so_oobsigcnt, so->so_oobcnt,
34760Sstevel@tonic-gate 		    pr_state(so->so_state, so->so_mode)));
34770Sstevel@tonic-gate 		pflag = opflag;
34780Sstevel@tonic-gate 		goto retry;
34790Sstevel@tonic-gate 	}
34800Sstevel@tonic-gate 	default:
34810Sstevel@tonic-gate 		ASSERT(0);
34820Sstevel@tonic-gate 		freemsg(mp);
34830Sstevel@tonic-gate 		error = EPROTO;
34840Sstevel@tonic-gate 		eprintsoline(so, error);
34850Sstevel@tonic-gate 		goto err;
34860Sstevel@tonic-gate 	}
34870Sstevel@tonic-gate 	/* NOTREACHED */
34880Sstevel@tonic-gate err:
34890Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
34900Sstevel@tonic-gate 	so_unlock_read(so);	/* Clear SOREADLOCKED */
34910Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
34920Sstevel@tonic-gate 	return (error);
34930Sstevel@tonic-gate }
34940Sstevel@tonic-gate 
34950Sstevel@tonic-gate /*
34960Sstevel@tonic-gate  * Sending data with options on a datagram socket.
34970Sstevel@tonic-gate  * Assumes caller has verified that SS_ISBOUND etc. are set.
34980Sstevel@tonic-gate  */
34990Sstevel@tonic-gate static int
3500741Smasputra sosend_dgramcmsg(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3501741Smasputra     struct uio *uiop, void *control, t_uscalar_t controllen, int flags)
35020Sstevel@tonic-gate {
35030Sstevel@tonic-gate 	struct T_unitdata_req	tudr;
35040Sstevel@tonic-gate 	mblk_t			*mp;
35050Sstevel@tonic-gate 	int			error;
35060Sstevel@tonic-gate 	void			*addr;
35070Sstevel@tonic-gate 	socklen_t		addrlen;
35080Sstevel@tonic-gate 	void			*src;
35090Sstevel@tonic-gate 	socklen_t		srclen;
35100Sstevel@tonic-gate 	ssize_t			len;
35110Sstevel@tonic-gate 	int			size;
35120Sstevel@tonic-gate 	struct T_opthdr		toh;
35130Sstevel@tonic-gate 	struct fdbuf		*fdbuf;
35140Sstevel@tonic-gate 	t_uscalar_t		optlen;
35150Sstevel@tonic-gate 	void			*fds;
35160Sstevel@tonic-gate 	int			fdlen;
35170Sstevel@tonic-gate 
35180Sstevel@tonic-gate 	ASSERT(name && namelen);
35190Sstevel@tonic-gate 	ASSERT(control && controllen);
35200Sstevel@tonic-gate 
35210Sstevel@tonic-gate 	len = uiop->uio_resid;
35220Sstevel@tonic-gate 	if (len > (ssize_t)so->so_tidu_size) {
35230Sstevel@tonic-gate 		return (EMSGSIZE);
35240Sstevel@tonic-gate 	}
35250Sstevel@tonic-gate 
35260Sstevel@tonic-gate 	/*
35270Sstevel@tonic-gate 	 * For AF_UNIX the destination address is translated to an internal
35280Sstevel@tonic-gate 	 * name and the source address is passed as an option.
35290Sstevel@tonic-gate 	 * Also, file descriptors are passed as file pointers in an
35300Sstevel@tonic-gate 	 * option.
35310Sstevel@tonic-gate 	 */
35320Sstevel@tonic-gate 
35330Sstevel@tonic-gate 	/*
35340Sstevel@tonic-gate 	 * Length and family checks.
35350Sstevel@tonic-gate 	 */
35360Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
35370Sstevel@tonic-gate 	if (error) {
35380Sstevel@tonic-gate 		eprintsoline(so, error);
35390Sstevel@tonic-gate 		return (error);
35400Sstevel@tonic-gate 	}
35410Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
35420Sstevel@tonic-gate 		if (so->so_state & SS_FADDR_NOXLATE) {
35430Sstevel@tonic-gate 			/*
35440Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
35450Sstevel@tonic-gate 			 * pass any (transport internal) source address.
35460Sstevel@tonic-gate 			 */
35470Sstevel@tonic-gate 			addr = name;
35480Sstevel@tonic-gate 			addrlen = namelen;
35490Sstevel@tonic-gate 			src = NULL;
35500Sstevel@tonic-gate 			srclen = 0;
35510Sstevel@tonic-gate 		} else {
35520Sstevel@tonic-gate 			/*
35530Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
35540Sstevel@tonic-gate 			 * and translate the remote address.
35550Sstevel@tonic-gate 			 *
35560Sstevel@tonic-gate 			 * Note that this code does not prevent so_laddr_sa
35570Sstevel@tonic-gate 			 * from changing while it is being used. Thus
35580Sstevel@tonic-gate 			 * if an unbind+bind occurs concurrently with this
35590Sstevel@tonic-gate 			 * send the peer might see a partially new and a
35600Sstevel@tonic-gate 			 * partially old "from" address.
35610Sstevel@tonic-gate 			 */
35620Sstevel@tonic-gate 			src = so->so_laddr_sa;
35630Sstevel@tonic-gate 			srclen = (t_uscalar_t)so->so_laddr_len;
35640Sstevel@tonic-gate 			dprintso(so, 1,
35650Sstevel@tonic-gate 			    ("sosend_dgramcmsg UNIX: srclen %d, src %p\n",
35660Sstevel@tonic-gate 			    srclen, src));
35670Sstevel@tonic-gate 			error = so_ux_addr_xlate(so, name, namelen,
35680Sstevel@tonic-gate 				(flags & MSG_XPG4_2),
35690Sstevel@tonic-gate 				&addr, &addrlen);
35700Sstevel@tonic-gate 			if (error) {
35710Sstevel@tonic-gate 				eprintsoline(so, error);
35720Sstevel@tonic-gate 				return (error);
35730Sstevel@tonic-gate 			}
35740Sstevel@tonic-gate 		}
35750Sstevel@tonic-gate 	} else {
35760Sstevel@tonic-gate 		addr = name;
35770Sstevel@tonic-gate 		addrlen = namelen;
35780Sstevel@tonic-gate 		src = NULL;
35790Sstevel@tonic-gate 		srclen = 0;
35800Sstevel@tonic-gate 	}
35810Sstevel@tonic-gate 	optlen = so_optlen(control, controllen,
35820Sstevel@tonic-gate 					!(flags & MSG_XPG4_2));
35830Sstevel@tonic-gate 	tudr.PRIM_type = T_UNITDATA_REQ;
35840Sstevel@tonic-gate 	tudr.DEST_length = addrlen;
35850Sstevel@tonic-gate 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
35860Sstevel@tonic-gate 	if (srclen != 0)
35870Sstevel@tonic-gate 		tudr.OPT_length = (t_scalar_t)(optlen + sizeof (toh) +
35880Sstevel@tonic-gate 		    _TPI_ALIGN_TOPT(srclen));
35890Sstevel@tonic-gate 	else
35900Sstevel@tonic-gate 		tudr.OPT_length = optlen;
35910Sstevel@tonic-gate 	tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
35920Sstevel@tonic-gate 				_TPI_ALIGN_TOPT(addrlen));
35930Sstevel@tonic-gate 
35940Sstevel@tonic-gate 	size = tudr.OPT_offset + tudr.OPT_length;
35950Sstevel@tonic-gate 
35960Sstevel@tonic-gate 	/*
35970Sstevel@tonic-gate 	 * File descriptors only when SM_FDPASSING set.
35980Sstevel@tonic-gate 	 */
35990Sstevel@tonic-gate 	error = so_getfdopt(control, controllen,
36000Sstevel@tonic-gate 			!(flags & MSG_XPG4_2), &fds, &fdlen);
36010Sstevel@tonic-gate 	if (error)
36020Sstevel@tonic-gate 		return (error);
36030Sstevel@tonic-gate 	if (fdlen != -1) {
36040Sstevel@tonic-gate 		if (!(so->so_mode & SM_FDPASSING))
36050Sstevel@tonic-gate 			return (EOPNOTSUPP);
36060Sstevel@tonic-gate 
36070Sstevel@tonic-gate 		error = fdbuf_create(fds, fdlen, &fdbuf);
36080Sstevel@tonic-gate 		if (error)
36090Sstevel@tonic-gate 			return (error);
36100Sstevel@tonic-gate 		mp = fdbuf_allocmsg(size, fdbuf);
36110Sstevel@tonic-gate 	} else {
36120Sstevel@tonic-gate 		mp = soallocproto(size, _ALLOC_INTR);
3613455Smeem 		if (mp == NULL) {
3614455Smeem 			/*
3615455Smeem 			 * Caught a signal waiting for memory.
3616455Smeem 			 * Let send* return EINTR.
3617455Smeem 			 */
3618455Smeem 			return (EINTR);
3619455Smeem 		}
36200Sstevel@tonic-gate 	}
36210Sstevel@tonic-gate 	soappendmsg(mp, &tudr, sizeof (tudr));
36220Sstevel@tonic-gate 	soappendmsg(mp, addr, addrlen);
36230Sstevel@tonic-gate 	mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
36240Sstevel@tonic-gate 
36250Sstevel@tonic-gate 	if (fdlen != -1) {
36260Sstevel@tonic-gate 		ASSERT(fdbuf != NULL);
36270Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
36280Sstevel@tonic-gate 		toh.name = SO_FILEP;
36290Sstevel@tonic-gate 		toh.len = fdbuf->fd_size +
36300Sstevel@tonic-gate 				(t_uscalar_t)sizeof (struct T_opthdr);
36310Sstevel@tonic-gate 		toh.status = 0;
36320Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
36330Sstevel@tonic-gate 		soappendmsg(mp, fdbuf, fdbuf->fd_size);
36340Sstevel@tonic-gate 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
36350Sstevel@tonic-gate 	}
36360Sstevel@tonic-gate 	if (srclen != 0) {
36370Sstevel@tonic-gate 		/*
36380Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
36390Sstevel@tonic-gate 		 * address option.
36400Sstevel@tonic-gate 		 */
36410Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
36420Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
36430Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
36440Sstevel@tonic-gate 		toh.status = 0;
36450Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
36460Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
36470Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
36480Sstevel@tonic-gate 		ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
36490Sstevel@tonic-gate 	}
36500Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
36510Sstevel@tonic-gate 	so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
36520Sstevel@tonic-gate 	/* At most 3 bytes left in the message */
36530Sstevel@tonic-gate 	ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
36540Sstevel@tonic-gate 	ASSERT(MBLKL(mp) <= (ssize_t)size);
36550Sstevel@tonic-gate 
36560Sstevel@tonic-gate 	ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
36570Sstevel@tonic-gate #ifdef C2_AUDIT
36580Sstevel@tonic-gate 	if (audit_active)
36590Sstevel@tonic-gate 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
36600Sstevel@tonic-gate #endif /* C2_AUDIT */
36610Sstevel@tonic-gate 
36620Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
36630Sstevel@tonic-gate #ifdef SOCK_DEBUG
36640Sstevel@tonic-gate 	if (error) {
36650Sstevel@tonic-gate 		eprintsoline(so, error);
36660Sstevel@tonic-gate 	}
36670Sstevel@tonic-gate #endif /* SOCK_DEBUG */
36680Sstevel@tonic-gate 	return (error);
36690Sstevel@tonic-gate }
36700Sstevel@tonic-gate 
36710Sstevel@tonic-gate /*
36720Sstevel@tonic-gate  * Sending data with options on a connected stream socket.
36730Sstevel@tonic-gate  * Assumes caller has verified that SS_ISCONNECTED is set.
36740Sstevel@tonic-gate  */
36750Sstevel@tonic-gate static int
36760Sstevel@tonic-gate sosend_svccmsg(struct sonode *so,
36770Sstevel@tonic-gate 		struct uio *uiop,
36780Sstevel@tonic-gate 		int more,
36790Sstevel@tonic-gate 		void *control,
36800Sstevel@tonic-gate 		t_uscalar_t controllen,
36810Sstevel@tonic-gate 		int flags)
36820Sstevel@tonic-gate {
36830Sstevel@tonic-gate 	struct T_optdata_req	tdr;
36840Sstevel@tonic-gate 	mblk_t			*mp;
36850Sstevel@tonic-gate 	int			error;
36860Sstevel@tonic-gate 	ssize_t			iosize;
36870Sstevel@tonic-gate 	int			first = 1;
36880Sstevel@tonic-gate 	int			size;
36890Sstevel@tonic-gate 	struct fdbuf		*fdbuf;
36900Sstevel@tonic-gate 	t_uscalar_t		optlen;
36910Sstevel@tonic-gate 	void			*fds;
36920Sstevel@tonic-gate 	int			fdlen;
36930Sstevel@tonic-gate 	struct T_opthdr		toh;
36940Sstevel@tonic-gate 
36950Sstevel@tonic-gate 	dprintso(so, 1,
36960Sstevel@tonic-gate 		("sosend_svccmsg: resid %ld bytes\n", uiop->uio_resid));
36970Sstevel@tonic-gate 
36980Sstevel@tonic-gate 	/*
36990Sstevel@tonic-gate 	 * Has to be bound and connected. However, since no locks are
37000Sstevel@tonic-gate 	 * held the state could have changed after sotpi_sendmsg checked it
37010Sstevel@tonic-gate 	 * thus it is not possible to ASSERT on the state.
37020Sstevel@tonic-gate 	 */
37030Sstevel@tonic-gate 
37040Sstevel@tonic-gate 	/* Options on connection-oriented only when SM_OPTDATA set. */
37050Sstevel@tonic-gate 	if (!(so->so_mode & SM_OPTDATA))
37060Sstevel@tonic-gate 		return (EOPNOTSUPP);
37070Sstevel@tonic-gate 
37080Sstevel@tonic-gate 	do {
37090Sstevel@tonic-gate 		/*
37100Sstevel@tonic-gate 		 * Set the MORE flag if uio_resid does not fit in this
37110Sstevel@tonic-gate 		 * message or if the caller passed in "more".
37120Sstevel@tonic-gate 		 * Error for transports with zero tidu_size.
37130Sstevel@tonic-gate 		 */
37140Sstevel@tonic-gate 		tdr.PRIM_type = T_OPTDATA_REQ;
37150Sstevel@tonic-gate 		iosize = so->so_tidu_size;
37160Sstevel@tonic-gate 		if (iosize <= 0)
37170Sstevel@tonic-gate 			return (EMSGSIZE);
37180Sstevel@tonic-gate 		if (uiop->uio_resid > iosize) {
37190Sstevel@tonic-gate 			tdr.DATA_flag = 1;
37200Sstevel@tonic-gate 		} else {
37210Sstevel@tonic-gate 			if (more)
37220Sstevel@tonic-gate 				tdr.DATA_flag = 1;
37230Sstevel@tonic-gate 			else
37240Sstevel@tonic-gate 				tdr.DATA_flag = 0;
37250Sstevel@tonic-gate 			iosize = uiop->uio_resid;
37260Sstevel@tonic-gate 		}
37270Sstevel@tonic-gate 		dprintso(so, 1, ("sosend_svccmsg: sending %d, %ld bytes\n",
37280Sstevel@tonic-gate 			tdr.DATA_flag, iosize));
37290Sstevel@tonic-gate 
37300Sstevel@tonic-gate 		optlen = so_optlen(control, controllen, !(flags & MSG_XPG4_2));
37310Sstevel@tonic-gate 		tdr.OPT_length = optlen;
37320Sstevel@tonic-gate 		tdr.OPT_offset = (t_scalar_t)sizeof (tdr);
37330Sstevel@tonic-gate 
37340Sstevel@tonic-gate 		size = (int)sizeof (tdr) + optlen;
37350Sstevel@tonic-gate 		/*
37360Sstevel@tonic-gate 		 * File descriptors only when SM_FDPASSING set.
37370Sstevel@tonic-gate 		 */
37380Sstevel@tonic-gate 		error = so_getfdopt(control, controllen,
37390Sstevel@tonic-gate 				!(flags & MSG_XPG4_2), &fds, &fdlen);
37400Sstevel@tonic-gate 		if (error)
37410Sstevel@tonic-gate 			return (error);
37420Sstevel@tonic-gate 		if (fdlen != -1) {
37430Sstevel@tonic-gate 			if (!(so->so_mode & SM_FDPASSING))
37440Sstevel@tonic-gate 				return (EOPNOTSUPP);
37450Sstevel@tonic-gate 
37460Sstevel@tonic-gate 			error = fdbuf_create(fds, fdlen, &fdbuf);
37470Sstevel@tonic-gate 			if (error)
37480Sstevel@tonic-gate 				return (error);
37490Sstevel@tonic-gate 			mp = fdbuf_allocmsg(size, fdbuf);
37500Sstevel@tonic-gate 		} else {
37510Sstevel@tonic-gate 			mp = soallocproto(size, _ALLOC_INTR);
3752455Smeem 			if (mp == NULL) {
3753455Smeem 				/*
3754455Smeem 				 * Caught a signal waiting for memory.
3755455Smeem 				 * Let send* return EINTR.
3756455Smeem 				 */
3757455Smeem 				return (first ? EINTR : 0);
3758455Smeem 			}
37590Sstevel@tonic-gate 		}
37600Sstevel@tonic-gate 		soappendmsg(mp, &tdr, sizeof (tdr));
37610Sstevel@tonic-gate 
37620Sstevel@tonic-gate 		if (fdlen != -1) {
37630Sstevel@tonic-gate 			ASSERT(fdbuf != NULL);
37640Sstevel@tonic-gate 			toh.level = SOL_SOCKET;
37650Sstevel@tonic-gate 			toh.name = SO_FILEP;
37660Sstevel@tonic-gate 			toh.len = fdbuf->fd_size +
37670Sstevel@tonic-gate 				(t_uscalar_t)sizeof (struct T_opthdr);
37680Sstevel@tonic-gate 			toh.status = 0;
37690Sstevel@tonic-gate 			soappendmsg(mp, &toh, sizeof (toh));
37700Sstevel@tonic-gate 			soappendmsg(mp, fdbuf, fdbuf->fd_size);
37710Sstevel@tonic-gate 			ASSERT(__TPI_TOPT_ISALIGNED(mp->b_wptr));
37720Sstevel@tonic-gate 		}
37730Sstevel@tonic-gate 		so_cmsg2opt(control, controllen, !(flags & MSG_XPG4_2), mp);
37740Sstevel@tonic-gate 		/* At most 3 bytes left in the message */
37750Sstevel@tonic-gate 		ASSERT(MBLKL(mp) > (ssize_t)(size - __TPI_ALIGN_SIZE));
37760Sstevel@tonic-gate 		ASSERT(MBLKL(mp) <= (ssize_t)size);
37770Sstevel@tonic-gate 
37780Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
37790Sstevel@tonic-gate 
37800Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
37810Sstevel@tonic-gate 					0, MSG_BAND, 0);
37820Sstevel@tonic-gate 		if (error) {
37830Sstevel@tonic-gate 			if (!first && error == EWOULDBLOCK)
37840Sstevel@tonic-gate 				return (0);
37850Sstevel@tonic-gate 			eprintsoline(so, error);
37860Sstevel@tonic-gate 			return (error);
37870Sstevel@tonic-gate 		}
37880Sstevel@tonic-gate 		control = NULL;
37890Sstevel@tonic-gate 		first = 0;
37900Sstevel@tonic-gate 		if (uiop->uio_resid > 0) {
37910Sstevel@tonic-gate 			/*
37920Sstevel@tonic-gate 			 * Recheck for fatal errors. Fail write even though
37930Sstevel@tonic-gate 			 * some data have been written. This is consistent
37940Sstevel@tonic-gate 			 * with strwrite semantics and BSD sockets semantics.
37950Sstevel@tonic-gate 			 */
37960Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
37970Sstevel@tonic-gate 				tsignal(curthread, SIGPIPE);
37980Sstevel@tonic-gate 				eprintsoline(so, error);
37990Sstevel@tonic-gate 				return (EPIPE);
38000Sstevel@tonic-gate 			}
38010Sstevel@tonic-gate 			if (so->so_error != 0) {
38020Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
38030Sstevel@tonic-gate 				error = sogeterr(so);
38040Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
38050Sstevel@tonic-gate 				if (error != 0) {
38060Sstevel@tonic-gate 					eprintsoline(so, error);
38070Sstevel@tonic-gate 					return (error);
38080Sstevel@tonic-gate 				}
38090Sstevel@tonic-gate 			}
38100Sstevel@tonic-gate 		}
38110Sstevel@tonic-gate 	} while (uiop->uio_resid > 0);
38120Sstevel@tonic-gate 	return (0);
38130Sstevel@tonic-gate }
38140Sstevel@tonic-gate 
38150Sstevel@tonic-gate /*
38160Sstevel@tonic-gate  * Sending data on a datagram socket.
38170Sstevel@tonic-gate  * Assumes caller has verified that SS_ISBOUND etc. are set.
38180Sstevel@tonic-gate  *
38190Sstevel@tonic-gate  * For AF_UNIX the destination address is translated to an internal
38200Sstevel@tonic-gate  * name and the source address is passed as an option.
38210Sstevel@tonic-gate  */
38220Sstevel@tonic-gate int
3823741Smasputra sosend_dgram(struct sonode *so, struct sockaddr	*name, socklen_t namelen,
3824741Smasputra     struct uio *uiop, int flags)
38250Sstevel@tonic-gate {
38260Sstevel@tonic-gate 	struct T_unitdata_req	tudr;
38270Sstevel@tonic-gate 	mblk_t			*mp;
38280Sstevel@tonic-gate 	int			error;
38290Sstevel@tonic-gate 	void			*addr;
38300Sstevel@tonic-gate 	socklen_t		addrlen;
38310Sstevel@tonic-gate 	void			*src;
38320Sstevel@tonic-gate 	socklen_t		srclen;
38330Sstevel@tonic-gate 	ssize_t			len;
38340Sstevel@tonic-gate 
3835741Smasputra 	ASSERT(name != NULL && namelen != 0);
38360Sstevel@tonic-gate 
38370Sstevel@tonic-gate 	len = uiop->uio_resid;
38380Sstevel@tonic-gate 	if (len > so->so_tidu_size) {
38390Sstevel@tonic-gate 		error = EMSGSIZE;
38400Sstevel@tonic-gate 		goto done;
38410Sstevel@tonic-gate 	}
38420Sstevel@tonic-gate 
3843741Smasputra 	/* Length and family checks */
38440Sstevel@tonic-gate 	error = so_addr_verify(so, name, namelen);
3845741Smasputra 	if (error != 0)
38460Sstevel@tonic-gate 		goto done;
3847741Smasputra 
3848741Smasputra 	if (so->so_state & SS_DIRECT)
3849741Smasputra 		return (sodgram_direct(so, name, namelen, uiop, flags));
3850741Smasputra 
38510Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
38520Sstevel@tonic-gate 		if (so->so_state & SS_FADDR_NOXLATE) {
38530Sstevel@tonic-gate 			/*
38540Sstevel@tonic-gate 			 * Already have a transport internal address. Do not
38550Sstevel@tonic-gate 			 * pass any (transport internal) source address.
38560Sstevel@tonic-gate 			 */
38570Sstevel@tonic-gate 			addr = name;
38580Sstevel@tonic-gate 			addrlen = namelen;
38590Sstevel@tonic-gate 			src = NULL;
38600Sstevel@tonic-gate 			srclen = 0;
38610Sstevel@tonic-gate 		} else {
38620Sstevel@tonic-gate 			/*
38630Sstevel@tonic-gate 			 * Pass the sockaddr_un source address as an option
38640Sstevel@tonic-gate 			 * and translate the remote address.
38650Sstevel@tonic-gate 			 *
38660Sstevel@tonic-gate 			 * Note that this code does not prevent so_laddr_sa
38670Sstevel@tonic-gate 			 * from changing while it is being used. Thus
38680Sstevel@tonic-gate 			 * if an unbind+bind occurs concurrently with this
38690Sstevel@tonic-gate 			 * send the peer might see a partially new and a
38700Sstevel@tonic-gate 			 * partially old "from" address.
38710Sstevel@tonic-gate 			 */
38720Sstevel@tonic-gate 			src = so->so_laddr_sa;
38730Sstevel@tonic-gate 			srclen = (socklen_t)so->so_laddr_len;
38740Sstevel@tonic-gate 			dprintso(so, 1,
38750Sstevel@tonic-gate 				("sosend_dgram UNIX: srclen %d, src %p\n",
38760Sstevel@tonic-gate 				srclen, src));
38770Sstevel@tonic-gate 			error = so_ux_addr_xlate(so, name, namelen,
38780Sstevel@tonic-gate 				(flags & MSG_XPG4_2),
38790Sstevel@tonic-gate 				&addr, &addrlen);
38800Sstevel@tonic-gate 			if (error) {
38810Sstevel@tonic-gate 				eprintsoline(so, error);
38820Sstevel@tonic-gate 				goto done;
38830Sstevel@tonic-gate 			}
38840Sstevel@tonic-gate 		}
38850Sstevel@tonic-gate 	} else {
38860Sstevel@tonic-gate 		addr = name;
38870Sstevel@tonic-gate 		addrlen = namelen;
38880Sstevel@tonic-gate 		src = NULL;
38890Sstevel@tonic-gate 		srclen = 0;
38900Sstevel@tonic-gate 	}
38910Sstevel@tonic-gate 	tudr.PRIM_type = T_UNITDATA_REQ;
38920Sstevel@tonic-gate 	tudr.DEST_length = addrlen;
38930Sstevel@tonic-gate 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
38940Sstevel@tonic-gate 	if (srclen == 0) {
38950Sstevel@tonic-gate 		tudr.OPT_length = 0;
38960Sstevel@tonic-gate 		tudr.OPT_offset = 0;
38970Sstevel@tonic-gate 
38980Sstevel@tonic-gate 		mp = soallocproto2(&tudr, sizeof (tudr),
38990Sstevel@tonic-gate 		    addr, addrlen, 0, _ALLOC_INTR);
39000Sstevel@tonic-gate 		if (mp == NULL) {
39010Sstevel@tonic-gate 			/*
39020Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
39030Sstevel@tonic-gate 			 * Let send* return EINTR.
39040Sstevel@tonic-gate 			 */
39050Sstevel@tonic-gate 			error = EINTR;
39060Sstevel@tonic-gate 			goto done;
39070Sstevel@tonic-gate 		}
39080Sstevel@tonic-gate 	} else {
39090Sstevel@tonic-gate 		/*
39100Sstevel@tonic-gate 		 * There is a AF_UNIX sockaddr_un to include as a source
39110Sstevel@tonic-gate 		 * address option.
39120Sstevel@tonic-gate 		 */
39130Sstevel@tonic-gate 		struct T_opthdr toh;
39140Sstevel@tonic-gate 		ssize_t size;
39150Sstevel@tonic-gate 
39160Sstevel@tonic-gate 		tudr.OPT_length = (t_scalar_t)(sizeof (toh) +
39170Sstevel@tonic-gate 					_TPI_ALIGN_TOPT(srclen));
39180Sstevel@tonic-gate 		tudr.OPT_offset = (t_scalar_t)(sizeof (tudr) +
39190Sstevel@tonic-gate 					_TPI_ALIGN_TOPT(addrlen));
39200Sstevel@tonic-gate 
39210Sstevel@tonic-gate 		toh.level = SOL_SOCKET;
39220Sstevel@tonic-gate 		toh.name = SO_SRCADDR;
39230Sstevel@tonic-gate 		toh.len = (t_uscalar_t)(srclen + sizeof (struct T_opthdr));
39240Sstevel@tonic-gate 		toh.status = 0;
39250Sstevel@tonic-gate 
39260Sstevel@tonic-gate 		size = tudr.OPT_offset + tudr.OPT_length;
39270Sstevel@tonic-gate 		mp = soallocproto2(&tudr, sizeof (tudr),
39280Sstevel@tonic-gate 		    addr, addrlen, size, _ALLOC_INTR);
39290Sstevel@tonic-gate 		if (mp == NULL) {
39300Sstevel@tonic-gate 			/*
39310Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
39320Sstevel@tonic-gate 			 * Let send* return EINTR.
39330Sstevel@tonic-gate 			 */
39340Sstevel@tonic-gate 			error = EINTR;
39350Sstevel@tonic-gate 			goto done;
39360Sstevel@tonic-gate 		}
39370Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(addrlen) - addrlen;
39380Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
39390Sstevel@tonic-gate 		soappendmsg(mp, src, srclen);
39400Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(srclen) - srclen;
39410Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
39420Sstevel@tonic-gate 	}
39430Sstevel@tonic-gate 
39440Sstevel@tonic-gate #ifdef C2_AUDIT
39450Sstevel@tonic-gate 	if (audit_active)
39460Sstevel@tonic-gate 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
39470Sstevel@tonic-gate #endif /* C2_AUDIT */
39480Sstevel@tonic-gate 
39490Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
39500Sstevel@tonic-gate done:
39510Sstevel@tonic-gate #ifdef SOCK_DEBUG
39520Sstevel@tonic-gate 	if (error) {
39530Sstevel@tonic-gate 		eprintsoline(so, error);
39540Sstevel@tonic-gate 	}
39550Sstevel@tonic-gate #endif /* SOCK_DEBUG */
39560Sstevel@tonic-gate 	return (error);
39570Sstevel@tonic-gate }
39580Sstevel@tonic-gate 
39590Sstevel@tonic-gate /*
39600Sstevel@tonic-gate  * Sending data on a connected stream socket.
39610Sstevel@tonic-gate  * Assumes caller has verified that SS_ISCONNECTED is set.
39620Sstevel@tonic-gate  */
39630Sstevel@tonic-gate int
39640Sstevel@tonic-gate sosend_svc(struct sonode *so,
39650Sstevel@tonic-gate 	struct uio *uiop,
39660Sstevel@tonic-gate 	t_scalar_t prim,
39670Sstevel@tonic-gate 	int more,
39680Sstevel@tonic-gate 	int sflag)
39690Sstevel@tonic-gate {
39700Sstevel@tonic-gate 	struct T_data_req	tdr;
39710Sstevel@tonic-gate 	mblk_t			*mp;
39720Sstevel@tonic-gate 	int			error;
39730Sstevel@tonic-gate 	ssize_t			iosize;
39740Sstevel@tonic-gate 	int			first = 1;
39750Sstevel@tonic-gate 
39760Sstevel@tonic-gate 	dprintso(so, 1,
39770Sstevel@tonic-gate 		("sosend_svc: %p, resid %ld bytes, prim %d, sflag 0x%x\n",
39780Sstevel@tonic-gate 		so, uiop->uio_resid, prim, sflag));
39790Sstevel@tonic-gate 
39800Sstevel@tonic-gate 	/*
39810Sstevel@tonic-gate 	 * Has to be bound and connected. However, since no locks are
39820Sstevel@tonic-gate 	 * held the state could have changed after sotpi_sendmsg checked it
39830Sstevel@tonic-gate 	 * thus it is not possible to ASSERT on the state.
39840Sstevel@tonic-gate 	 */
39850Sstevel@tonic-gate 
39860Sstevel@tonic-gate 	do {
39870Sstevel@tonic-gate 		/*
39880Sstevel@tonic-gate 		 * Set the MORE flag if uio_resid does not fit in this
39890Sstevel@tonic-gate 		 * message or if the caller passed in "more".
39900Sstevel@tonic-gate 		 * Error for transports with zero tidu_size.
39910Sstevel@tonic-gate 		 */
39920Sstevel@tonic-gate 		tdr.PRIM_type = prim;
39930Sstevel@tonic-gate 		iosize = so->so_tidu_size;
39940Sstevel@tonic-gate 		if (iosize <= 0)
39950Sstevel@tonic-gate 			return (EMSGSIZE);
39960Sstevel@tonic-gate 		if (uiop->uio_resid > iosize) {
39970Sstevel@tonic-gate 			tdr.MORE_flag = 1;
39980Sstevel@tonic-gate 		} else {
39990Sstevel@tonic-gate 			if (more)
40000Sstevel@tonic-gate 				tdr.MORE_flag = 1;
40010Sstevel@tonic-gate 			else
40020Sstevel@tonic-gate 				tdr.MORE_flag = 0;
40030Sstevel@tonic-gate 			iosize = uiop->uio_resid;
40040Sstevel@tonic-gate 		}
40050Sstevel@tonic-gate 		dprintso(so, 1, ("sosend_svc: sending 0x%x %d, %ld bytes\n",
40060Sstevel@tonic-gate 			prim, tdr.MORE_flag, iosize));
40070Sstevel@tonic-gate 		mp = soallocproto1(&tdr, sizeof (tdr), 0, _ALLOC_INTR);
40080Sstevel@tonic-gate 		if (mp == NULL) {
40090Sstevel@tonic-gate 			/*
40100Sstevel@tonic-gate 			 * Caught a signal waiting for memory.
40110Sstevel@tonic-gate 			 * Let send* return EINTR.
40120Sstevel@tonic-gate 			 */
40130Sstevel@tonic-gate 			if (first)
40140Sstevel@tonic-gate 				return (EINTR);
40150Sstevel@tonic-gate 			else
40160Sstevel@tonic-gate 				return (0);
40170Sstevel@tonic-gate 		}
40180Sstevel@tonic-gate 
40190Sstevel@tonic-gate 		error = kstrputmsg(SOTOV(so), mp, uiop, iosize,
40200Sstevel@tonic-gate 					0, sflag | MSG_BAND, 0);
40210Sstevel@tonic-gate 		if (error) {
40220Sstevel@tonic-gate 			if (!first && error == EWOULDBLOCK)
40230Sstevel@tonic-gate 				return (0);
40240Sstevel@tonic-gate 			eprintsoline(so, error);
40250Sstevel@tonic-gate 			return (error);
40260Sstevel@tonic-gate 		}
40270Sstevel@tonic-gate 		first = 0;
40280Sstevel@tonic-gate 		if (uiop->uio_resid > 0) {
40290Sstevel@tonic-gate 			/*
40300Sstevel@tonic-gate 			 * Recheck for fatal errors. Fail write even though
40310Sstevel@tonic-gate 			 * some data have been written. This is consistent
40320Sstevel@tonic-gate 			 * with strwrite semantics and BSD sockets semantics.
40330Sstevel@tonic-gate 			 */
40340Sstevel@tonic-gate 			if (so->so_state & SS_CANTSENDMORE) {
40350Sstevel@tonic-gate 				tsignal(curthread, SIGPIPE);
40360Sstevel@tonic-gate 				eprintsoline(so, error);
40370Sstevel@tonic-gate 				return (EPIPE);
40380Sstevel@tonic-gate 			}
40390Sstevel@tonic-gate 			if (so->so_error != 0) {
40400Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
40410Sstevel@tonic-gate 				error = sogeterr(so);
40420Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
40430Sstevel@tonic-gate 				if (error != 0) {
40440Sstevel@tonic-gate 					eprintsoline(so, error);
40450Sstevel@tonic-gate 					return (error);
40460Sstevel@tonic-gate 				}
40470Sstevel@tonic-gate 			}
40480Sstevel@tonic-gate 		}
40490Sstevel@tonic-gate 	} while (uiop->uio_resid > 0);
40500Sstevel@tonic-gate 	return (0);
40510Sstevel@tonic-gate }
40520Sstevel@tonic-gate 
40530Sstevel@tonic-gate /*
40540Sstevel@tonic-gate  * Check the state for errors and call the appropriate send function.
40550Sstevel@tonic-gate  *
40560Sstevel@tonic-gate  * If MSG_DONTROUTE is set (and SO_DONTROUTE isn't already set)
40570Sstevel@tonic-gate  * this function issues a setsockopt to toggle SO_DONTROUTE before and
40580Sstevel@tonic-gate  * after sending the message.
40590Sstevel@tonic-gate  */
40600Sstevel@tonic-gate static int
40610Sstevel@tonic-gate sotpi_sendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
40620Sstevel@tonic-gate {
40630Sstevel@tonic-gate 	int		so_state;
40640Sstevel@tonic-gate 	int		so_mode;
40650Sstevel@tonic-gate 	int		error;
40660Sstevel@tonic-gate 	struct sockaddr *name;
40670Sstevel@tonic-gate 	t_uscalar_t	namelen;
40680Sstevel@tonic-gate 	int		dontroute;
40690Sstevel@tonic-gate 	int		flags;
40700Sstevel@tonic-gate 
40710Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_sendmsg(%p, %p, 0x%x) state %s, error %d\n",
40720Sstevel@tonic-gate 		so, msg, msg->msg_flags,
40730Sstevel@tonic-gate 		pr_state(so->so_state, so->so_mode), so->so_error));
40740Sstevel@tonic-gate 
40750Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
40760Sstevel@tonic-gate 	so_state = so->so_state;
40770Sstevel@tonic-gate 
40780Sstevel@tonic-gate 	if (so_state & SS_CANTSENDMORE) {
40790Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
40800Sstevel@tonic-gate 		tsignal(curthread, SIGPIPE);
40810Sstevel@tonic-gate 		return (EPIPE);
40820Sstevel@tonic-gate 	}
40830Sstevel@tonic-gate 
40840Sstevel@tonic-gate 	if (so->so_error != 0) {
40850Sstevel@tonic-gate 		error = sogeterr(so);
40860Sstevel@tonic-gate 		if (error != 0) {
40870Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
40880Sstevel@tonic-gate 			return (error);
40890Sstevel@tonic-gate 		}
40900Sstevel@tonic-gate 	}
40910Sstevel@tonic-gate 
40920Sstevel@tonic-gate 	name = (struct sockaddr *)msg->msg_name;
40930Sstevel@tonic-gate 	namelen = msg->msg_namelen;
40940Sstevel@tonic-gate 
40950Sstevel@tonic-gate 	so_mode = so->so_mode;
40960Sstevel@tonic-gate 
40970Sstevel@tonic-gate 	if (name == NULL) {
40980Sstevel@tonic-gate 		if (!(so_state & SS_ISCONNECTED)) {
40990Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
41000Sstevel@tonic-gate 			if (so_mode & SM_CONNREQUIRED)
41010Sstevel@tonic-gate 				return (ENOTCONN);
41020Sstevel@tonic-gate 			else
41030Sstevel@tonic-gate 				return (EDESTADDRREQ);
41040Sstevel@tonic-gate 		}
41050Sstevel@tonic-gate 		if (so_mode & SM_CONNREQUIRED) {
41060Sstevel@tonic-gate 			name = NULL;
41070Sstevel@tonic-gate 			namelen = 0;
41080Sstevel@tonic-gate 		} else {
41090Sstevel@tonic-gate 			/*
41100Sstevel@tonic-gate 			 * Note that this code does not prevent so_faddr_sa
41110Sstevel@tonic-gate 			 * from changing while it is being used. Thus
41120Sstevel@tonic-gate 			 * if an "unconnect"+connect occurs concurrently with
41130Sstevel@tonic-gate 			 * this send the datagram might be delivered to a
41140Sstevel@tonic-gate 			 * garbaled address.
41150Sstevel@tonic-gate 			 */
41160Sstevel@tonic-gate 			ASSERT(so->so_faddr_sa);
41170Sstevel@tonic-gate 			name = so->so_faddr_sa;
41180Sstevel@tonic-gate 			namelen = (t_uscalar_t)so->so_faddr_len;
41190Sstevel@tonic-gate 		}
41200Sstevel@tonic-gate 	} else {
41210Sstevel@tonic-gate 		if (!(so_state & SS_ISCONNECTED) &&
41220Sstevel@tonic-gate 		    (so_mode & SM_CONNREQUIRED)) {
41230Sstevel@tonic-gate 			/* Required but not connected */
41240Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
41250Sstevel@tonic-gate 			return (ENOTCONN);
41260Sstevel@tonic-gate 		}
41270Sstevel@tonic-gate 		/*
41280Sstevel@tonic-gate 		 * Ignore the address on connection-oriented sockets.
41290Sstevel@tonic-gate 		 * Just like BSD this code does not generate an error for
41300Sstevel@tonic-gate 		 * TCP (a CONNREQUIRED socket) when sending to an address
41310Sstevel@tonic-gate 		 * passed in with sendto/sendmsg. Instead the data is
41320Sstevel@tonic-gate 		 * delivered on the connection as if no address had been
41330Sstevel@tonic-gate 		 * supplied.
41340Sstevel@tonic-gate 		 */
41350Sstevel@tonic-gate 		if ((so_state & SS_ISCONNECTED) &&
41360Sstevel@tonic-gate 		    !(so_mode & SM_CONNREQUIRED)) {
41370Sstevel@tonic-gate 			mutex_exit(&so->so_lock);
41380Sstevel@tonic-gate 			return (EISCONN);
41390Sstevel@tonic-gate 		}
41400Sstevel@tonic-gate 		if (!(so_state & SS_ISBOUND)) {
41410Sstevel@tonic-gate 			so_lock_single(so);	/* Set SOLOCKED */
41420Sstevel@tonic-gate 			error = sotpi_bind(so, NULL, 0,
41430Sstevel@tonic-gate 			    _SOBIND_UNSPEC|_SOBIND_LOCK_HELD);
41440Sstevel@tonic-gate 			so_unlock_single(so, SOLOCKED);
41450Sstevel@tonic-gate 			if (error) {
41460Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
41470Sstevel@tonic-gate 				eprintsoline(so, error);
41480Sstevel@tonic-gate 				return (error);
41490Sstevel@tonic-gate 			}
41500Sstevel@tonic-gate 		}
41510Sstevel@tonic-gate 		/*
41520Sstevel@tonic-gate 		 * Handle delayed datagram errors. These are only queued
41530Sstevel@tonic-gate 		 * when the application sets SO_DGRAM_ERRIND.
41540Sstevel@tonic-gate 		 * Return the error if we are sending to the address
41550Sstevel@tonic-gate 		 * that was returned in the last T_UDERROR_IND.
41560Sstevel@tonic-gate 		 * If sending to some other address discard the delayed
41570Sstevel@tonic-gate 		 * error indication.
41580Sstevel@tonic-gate 		 */
41590Sstevel@tonic-gate 		if (so->so_delayed_error) {
41600Sstevel@tonic-gate 			struct T_uderror_ind	*tudi;
41610Sstevel@tonic-gate 			void			*addr;
41620Sstevel@tonic-gate 			t_uscalar_t		addrlen;
41630Sstevel@tonic-gate 			boolean_t		match = B_FALSE;
41640Sstevel@tonic-gate 
41650Sstevel@tonic-gate 			ASSERT(so->so_eaddr_mp);
41660Sstevel@tonic-gate 			error = so->so_delayed_error;
41670Sstevel@tonic-gate 			so->so_delayed_error = 0;
41680Sstevel@tonic-gate 			tudi = (struct T_uderror_ind *)so->so_eaddr_mp->b_rptr;
41690Sstevel@tonic-gate 			addrlen = tudi->DEST_length;
41700Sstevel@tonic-gate 			addr = sogetoff(so->so_eaddr_mp,
41710Sstevel@tonic-gate 					tudi->DEST_offset,
41720Sstevel@tonic-gate 					addrlen, 1);
41730Sstevel@tonic-gate 			ASSERT(addr);	/* Checked by strsock_proto */
41740Sstevel@tonic-gate 			switch (so->so_family) {
41750Sstevel@tonic-gate 			case AF_INET: {
41760Sstevel@tonic-gate 				/* Compare just IP address and port */
41770Sstevel@tonic-gate 				sin_t *sin1 = (sin_t *)name;
41780Sstevel@tonic-gate 				sin_t *sin2 = (sin_t *)addr;
41790Sstevel@tonic-gate 
41800Sstevel@tonic-gate 				if (addrlen == sizeof (sin_t) &&
41810Sstevel@tonic-gate 				    namelen == addrlen &&
41820Sstevel@tonic-gate 				    sin1->sin_port == sin2->sin_port &&
41830Sstevel@tonic-gate 				    sin1->sin_addr.s_addr ==
41840Sstevel@tonic-gate 				    sin2->sin_addr.s_addr)
41850Sstevel@tonic-gate 					match = B_TRUE;
41860Sstevel@tonic-gate 				break;
41870Sstevel@tonic-gate 			}
41880Sstevel@tonic-gate 			case AF_INET6: {
41890Sstevel@tonic-gate 				/* Compare just IP address and port. Not flow */
41900Sstevel@tonic-gate 				sin6_t *sin1 = (sin6_t *)name;
41910Sstevel@tonic-gate 				sin6_t *sin2 = (sin6_t *)addr;
41920Sstevel@tonic-gate 
41930Sstevel@tonic-gate 				if (addrlen == sizeof (sin6_t) &&
41940Sstevel@tonic-gate 				    namelen == addrlen &&
41950Sstevel@tonic-gate 				    sin1->sin6_port == sin2->sin6_port &&
41960Sstevel@tonic-gate 				    IN6_ARE_ADDR_EQUAL(&sin1->sin6_addr,
41970Sstevel@tonic-gate 					&sin2->sin6_addr))
41980Sstevel@tonic-gate 					match = B_TRUE;
41990Sstevel@tonic-gate 				break;
42000Sstevel@tonic-gate 			}
42010Sstevel@tonic-gate 			case AF_UNIX:
42020Sstevel@tonic-gate 			default:
42030Sstevel@tonic-gate 				if (namelen == addrlen &&
42040Sstevel@tonic-gate 				    bcmp(name, addr, namelen) == 0)
42050Sstevel@tonic-gate 					match = B_TRUE;
42060Sstevel@tonic-gate 			}
42070Sstevel@tonic-gate 			if (match) {
42080Sstevel@tonic-gate 				freemsg(so->so_eaddr_mp);
42090Sstevel@tonic-gate 				so->so_eaddr_mp = NULL;
42100Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
42110Sstevel@tonic-gate #ifdef DEBUG
42120Sstevel@tonic-gate 				dprintso(so, 0,
42130Sstevel@tonic-gate 					("sockfs delayed error %d for %s\n",
42140Sstevel@tonic-gate 					error,
42150Sstevel@tonic-gate 					pr_addr(so->so_family, name, namelen)));
42160Sstevel@tonic-gate #endif /* DEBUG */
42170Sstevel@tonic-gate 				return (error);
42180Sstevel@tonic-gate 			}
42190Sstevel@tonic-gate 			freemsg(so->so_eaddr_mp);
42200Sstevel@tonic-gate 			so->so_eaddr_mp = NULL;
42210Sstevel@tonic-gate 		}
42220Sstevel@tonic-gate 	}
42230Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
42240Sstevel@tonic-gate 
42250Sstevel@tonic-gate 	flags = msg->msg_flags;
42260Sstevel@tonic-gate 	dontroute = 0;
42270Sstevel@tonic-gate 	if ((flags & MSG_DONTROUTE) && !(so->so_options & SO_DONTROUTE)) {
42280Sstevel@tonic-gate 		uint32_t	val;
42290Sstevel@tonic-gate 
42300Sstevel@tonic-gate 		val = 1;
42310Sstevel@tonic-gate 		error = sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
42320Sstevel@tonic-gate 					&val, (t_uscalar_t)sizeof (val));
42330Sstevel@tonic-gate 		if (error)
42340Sstevel@tonic-gate 			return (error);
42350Sstevel@tonic-gate 		dontroute = 1;
42360Sstevel@tonic-gate 	}
42370Sstevel@tonic-gate 
42380Sstevel@tonic-gate 	if ((flags & MSG_OOB) && !(so_mode & SM_EXDATA)) {
42390Sstevel@tonic-gate 		error = EOPNOTSUPP;
42400Sstevel@tonic-gate 		goto done;
42410Sstevel@tonic-gate 	}
42420Sstevel@tonic-gate 	if (msg->msg_controllen != 0) {
42430Sstevel@tonic-gate 		if (!(so_mode & SM_CONNREQUIRED)) {
42440Sstevel@tonic-gate 			error = sosend_dgramcmsg(so, name, namelen, uiop,
4245741Smasputra 			    msg->msg_control, msg->msg_controllen, flags);
42460Sstevel@tonic-gate 		} else {
42470Sstevel@tonic-gate 			if (flags & MSG_OOB) {
42480Sstevel@tonic-gate 				/* Can't generate T_EXDATA_REQ with options */
42490Sstevel@tonic-gate 				error = EOPNOTSUPP;
42500Sstevel@tonic-gate 				goto done;
42510Sstevel@tonic-gate 			}
42520Sstevel@tonic-gate 			error = sosend_svccmsg(so, uiop,
42530Sstevel@tonic-gate 				!(flags & MSG_EOR),
42540Sstevel@tonic-gate 				msg->msg_control, msg->msg_controllen,
42550Sstevel@tonic-gate 				flags);
42560Sstevel@tonic-gate 		}
42570Sstevel@tonic-gate 		goto done;
42580Sstevel@tonic-gate 	}
42590Sstevel@tonic-gate 
42600Sstevel@tonic-gate 	if (!(so_mode & SM_CONNREQUIRED)) {
42610Sstevel@tonic-gate 		/*
42620Sstevel@tonic-gate 		 * If there is no SO_DONTROUTE to turn off return immediately
4263741Smasputra 		 * from send_dgram. This can allow tail-call optimizations.
42640Sstevel@tonic-gate 		 */
42650Sstevel@tonic-gate 		if (!dontroute) {
42660Sstevel@tonic-gate 			return (sosend_dgram(so, name, namelen, uiop, flags));
42670Sstevel@tonic-gate 		}
42680Sstevel@tonic-gate 		error = sosend_dgram(so, name, namelen, uiop, flags);
42690Sstevel@tonic-gate 	} else {
42700Sstevel@tonic-gate 		t_scalar_t prim;
42710Sstevel@tonic-gate 		int sflag;
42720Sstevel@tonic-gate 
42730Sstevel@tonic-gate 		/* Ignore msg_name in the connected state */
42740Sstevel@tonic-gate 		if (flags & MSG_OOB) {
42750Sstevel@tonic-gate 			prim = T_EXDATA_REQ;
42760Sstevel@tonic-gate 			/*
42770Sstevel@tonic-gate 			 * Send down T_EXDATA_REQ even if there is flow
42780Sstevel@tonic-gate 			 * control for data.
42790Sstevel@tonic-gate 			 */
42800Sstevel@tonic-gate 			sflag = MSG_IGNFLOW;
42810Sstevel@tonic-gate 		} else {
42820Sstevel@tonic-gate 			if (so_mode & SM_BYTESTREAM) {
42830Sstevel@tonic-gate 				/* Byte stream transport - use write */
42840Sstevel@tonic-gate 
42850Sstevel@tonic-gate 				dprintso(so, 1, ("sotpi_sendmsg: write\n"));
42860Sstevel@tonic-gate 				/*
4287741Smasputra 				 * If there is no SO_DONTROUTE to turn off,
4288741Smasputra 				 * SS_DIRECT is on, and there is no flow
4289741Smasputra 				 * control, we can take the fast path.
42900Sstevel@tonic-gate 				 */
4291741Smasputra 				if (!dontroute &&
4292741Smasputra 				    (so_state & SS_DIRECT) &&
4293741Smasputra 				    canputnext(SOTOV(so)->v_stream->sd_wrq)) {
4294741Smasputra 					return (sostream_direct(so, uiop,
4295741Smasputra 					    NULL, CRED()));
4296741Smasputra 				}
42970Sstevel@tonic-gate 				error = strwrite(SOTOV(so), uiop, CRED());
42980Sstevel@tonic-gate 				goto done;
42990Sstevel@tonic-gate 			}
43000Sstevel@tonic-gate 			prim = T_DATA_REQ;
43010Sstevel@tonic-gate 			sflag = 0;
43020Sstevel@tonic-gate 		}
43030Sstevel@tonic-gate 		/*
43040Sstevel@tonic-gate 		 * If there is no SO_DONTROUTE to turn off return immediately
43050Sstevel@tonic-gate 		 * from sosend_svc. This can allow tail-call optimizations.
43060Sstevel@tonic-gate 		 */
43070Sstevel@tonic-gate 		if (!dontroute)
43080Sstevel@tonic-gate 			return (sosend_svc(so, uiop, prim,
43090Sstevel@tonic-gate 				!(flags & MSG_EOR), sflag));
43100Sstevel@tonic-gate 		error = sosend_svc(so, uiop, prim,
43110Sstevel@tonic-gate 				!(flags & MSG_EOR), sflag);
43120Sstevel@tonic-gate 	}
43130Sstevel@tonic-gate 	ASSERT(dontroute);
43140Sstevel@tonic-gate done:
43150Sstevel@tonic-gate 	if (dontroute) {
43160Sstevel@tonic-gate 		uint32_t	val;
43170Sstevel@tonic-gate 
43180Sstevel@tonic-gate 		val = 0;
43190Sstevel@tonic-gate 		(void) sotpi_setsockopt(so, SOL_SOCKET, SO_DONTROUTE,
43200Sstevel@tonic-gate 				&val, (t_uscalar_t)sizeof (val));
43210Sstevel@tonic-gate 	}
43220Sstevel@tonic-gate 	return (error);
43230Sstevel@tonic-gate }
43240Sstevel@tonic-gate 
43250Sstevel@tonic-gate /*
4326741Smasputra  * Sending data on a datagram socket.
4327741Smasputra  * Assumes caller has verified that SS_ISBOUND etc. are set.
4328741Smasputra  */
4329741Smasputra /* ARGSUSED */
4330741Smasputra static int
4331741Smasputra sodgram_direct(struct sonode *so, struct sockaddr *name,
4332741Smasputra     socklen_t namelen, struct uio *uiop, int flags)
4333741Smasputra {
4334741Smasputra 	struct T_unitdata_req	tudr;
4335741Smasputra 	mblk_t			*mp;
4336741Smasputra 	int			error = 0;
4337741Smasputra 	void			*addr;
4338741Smasputra 	socklen_t		addrlen;
4339741Smasputra 	ssize_t			len;
4340741Smasputra 	struct stdata		*stp = SOTOV(so)->v_stream;
4341741Smasputra 	int			so_state;
4342741Smasputra 	queue_t			*udp_wq;
4343741Smasputra 
4344741Smasputra 	ASSERT(name != NULL && namelen != 0);
4345741Smasputra 	ASSERT(!(so->so_mode & SM_CONNREQUIRED));
4346741Smasputra 	ASSERT(!(so->so_mode & SM_EXDATA));
4347741Smasputra 	ASSERT(so->so_family == AF_INET || so->so_family == AF_INET6);
4348741Smasputra 	ASSERT(SOTOV(so)->v_type == VSOCK);
4349741Smasputra 
4350741Smasputra 	/* Caller checked for proper length */
4351741Smasputra 	len = uiop->uio_resid;
4352741Smasputra 	ASSERT(len <= so->so_tidu_size);
4353741Smasputra 
4354741Smasputra 	/* Length and family checks have been done by caller */
4355741Smasputra 	ASSERT(name->sa_family == so->so_family);
4356741Smasputra 	ASSERT(so->so_family == AF_INET ||
4357741Smasputra 	    (namelen == (socklen_t)sizeof (struct sockaddr_in6)));
4358741Smasputra 	ASSERT(so->so_family == AF_INET6 ||
4359741Smasputra 	    (namelen == (socklen_t)sizeof (struct sockaddr_in)));
4360741Smasputra 
4361741Smasputra 	addr = name;
4362741Smasputra 	addrlen = namelen;
4363741Smasputra 
4364741Smasputra 	if (stp->sd_sidp != NULL &&
4365741Smasputra 	    (error = straccess(stp, JCWRITE)) != 0)
4366741Smasputra 		goto done;
4367741Smasputra 
4368741Smasputra 	so_state = so->so_state;
4369741Smasputra 
4370741Smasputra 	/*
4371741Smasputra 	 * For UDP we don't break up the copyin into smaller pieces
4372741Smasputra 	 * as in the TCP case.  That means if ENOMEM is returned by
4373741Smasputra 	 * mcopyinuio() then the uio vector has not been modified at
4374741Smasputra 	 * all and we fallback to either strwrite() or kstrputmsg()
4375741Smasputra 	 * below.  Note also that we never generate priority messages
4376741Smasputra 	 * from here.
4377741Smasputra 	 */
4378741Smasputra 	udp_wq = stp->sd_wrq->q_next;
4379741Smasputra 	if (canput(udp_wq) &&
4380741Smasputra 	    (mp = mcopyinuio(stp, uiop, -1, -1, &error)) != NULL) {
4381741Smasputra 		ASSERT(DB_TYPE(mp) == M_DATA);
4382741Smasputra 		ASSERT(uiop->uio_resid == 0);
4383741Smasputra #ifdef C2_AUDIT
4384741Smasputra 		if (audit_active)
4385741Smasputra 			audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4386741Smasputra #endif /* C2_AUDIT */
4387741Smasputra 		udp_wput_data(udp_wq, mp, addr, addrlen);
4388741Smasputra 		return (0);
4389741Smasputra 	}
4390741Smasputra 	if (error != 0 && error != ENOMEM)
4391741Smasputra 		return (error);
4392741Smasputra 
4393741Smasputra 	/*
4394741Smasputra 	 * For connected, let strwrite() handle the blocking case.
4395741Smasputra 	 * Otherwise we fall thru and use kstrputmsg().
4396741Smasputra 	 */
4397741Smasputra 	if (so_state & SS_ISCONNECTED)
4398741Smasputra 		return (strwrite(SOTOV(so), uiop, CRED()));
4399741Smasputra 
4400741Smasputra 	tudr.PRIM_type = T_UNITDATA_REQ;
4401741Smasputra 	tudr.DEST_length = addrlen;
4402741Smasputra 	tudr.DEST_offset = (t_scalar_t)sizeof (tudr);
4403741Smasputra 	tudr.OPT_length = 0;
4404741Smasputra 	tudr.OPT_offset = 0;
4405741Smasputra 
4406741Smasputra 	mp = soallocproto2(&tudr, sizeof (tudr), addr, addrlen, 0, _ALLOC_INTR);
4407741Smasputra 	if (mp == NULL) {
4408741Smasputra 		/*
4409741Smasputra 		 * Caught a signal waiting for memory.
4410741Smasputra 		 * Let send* return EINTR.
4411741Smasputra 		 */
4412741Smasputra 		error = EINTR;
4413741Smasputra 		goto done;
4414741Smasputra 	}
4415741Smasputra 
4416741Smasputra #ifdef C2_AUDIT
4417741Smasputra 	if (audit_active)
4418741Smasputra 		audit_sock(T_UNITDATA_REQ, strvp2wq(SOTOV(so)), mp, 0);
4419741Smasputra #endif /* C2_AUDIT */
4420741Smasputra 
4421741Smasputra 	error = kstrputmsg(SOTOV(so), mp, uiop, len, 0, MSG_BAND, 0);
4422741Smasputra done:
4423741Smasputra #ifdef SOCK_DEBUG
4424741Smasputra 	if (error != 0) {
4425741Smasputra 		eprintsoline(so, error);
4426741Smasputra 	}
4427741Smasputra #endif /* SOCK_DEBUG */
4428741Smasputra 	return (error);
4429741Smasputra }
4430741Smasputra 
4431741Smasputra int
4432741Smasputra sostream_direct(struct sonode *so, struct uio *uiop, mblk_t *mp, cred_t *cr)
4433741Smasputra {
4434741Smasputra 	struct stdata *stp = SOTOV(so)->v_stream;
4435741Smasputra 	ssize_t iosize, rmax, maxblk;
4436741Smasputra 	queue_t *tcp_wq = stp->sd_wrq->q_next;
4437898Skais 	mblk_t *newmp;
4438741Smasputra 	int error = 0, wflag = 0;
4439741Smasputra 
4440741Smasputra 	ASSERT(so->so_mode & SM_BYTESTREAM);
4441741Smasputra 	ASSERT(SOTOV(so)->v_type == VSOCK);
4442741Smasputra 
4443741Smasputra 	if (stp->sd_sidp != NULL &&
4444741Smasputra 	    (error = straccess(stp, JCWRITE)) != 0)
4445741Smasputra 		return (error);
4446741Smasputra 
4447741Smasputra 	if (uiop == NULL) {
4448741Smasputra 		/*
4449741Smasputra 		 * kstrwritemp() should have checked sd_flag and
4450741Smasputra 		 * flow-control before coming here.  If we end up
4451741Smasputra 		 * here it means that we can simply pass down the
4452741Smasputra 		 * data to tcp.
4453741Smasputra 		 */
4454741Smasputra 		ASSERT(mp != NULL);
4455898Skais 		if (stp->sd_wputdatafunc != NULL) {
4456898Skais 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4457898Skais 			    NULL, NULL, NULL);
4458898Skais 			if (newmp == NULL) {
4459898Skais 				/* The caller will free mp */
4460898Skais 				return (ECOMM);
4461898Skais 			}
4462898Skais 			mp = newmp;
4463898Skais 		}
4464741Smasputra 		tcp_wput(tcp_wq, mp);
4465741Smasputra 		return (0);
4466741Smasputra 	}
4467741Smasputra 
4468741Smasputra 	/* Fallback to strwrite() to do proper error handling */
4469741Smasputra 	if (stp->sd_flag & (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))
4470741Smasputra 		return (strwrite(SOTOV(so), uiop, cr));
4471741Smasputra 
4472741Smasputra 	rmax = stp->sd_qn_maxpsz;
4473741Smasputra 	ASSERT(rmax >= 0 || rmax == INFPSZ);
4474741Smasputra 	if (rmax == 0 || uiop->uio_resid <= 0)
4475741Smasputra 		return (0);
4476741Smasputra 
4477741Smasputra 	if (rmax == INFPSZ)
4478741Smasputra 		rmax = uiop->uio_resid;
4479741Smasputra 
4480741Smasputra 	maxblk = stp->sd_maxblk;
4481741Smasputra 
4482741Smasputra 	for (;;) {
4483741Smasputra 		iosize = MIN(uiop->uio_resid, rmax);
4484741Smasputra 
4485741Smasputra 		mp = mcopyinuio(stp, uiop, iosize, maxblk, &error);
4486741Smasputra 		if (mp == NULL) {
4487741Smasputra 			/*
4488741Smasputra 			 * Fallback to strwrite() for ENOMEM; if this
4489741Smasputra 			 * is our first time in this routine and the uio
4490741Smasputra 			 * vector has not been modified, we will end up
4491741Smasputra 			 * calling strwrite() without any flag set.
4492741Smasputra 			 */
4493741Smasputra 			if (error == ENOMEM)
4494741Smasputra 				goto slow_send;
4495741Smasputra 			else
4496741Smasputra 				return (error);
4497741Smasputra 		}
4498741Smasputra 		ASSERT(uiop->uio_resid >= 0);
4499741Smasputra 		/*
4500741Smasputra 		 * If mp is non-NULL and ENOMEM is set, it means that
4501741Smasputra 		 * mcopyinuio() was able to break down some of the user
4502741Smasputra 		 * data into one or more mblks.  Send the partial data
4503741Smasputra 		 * to tcp and let the rest be handled in strwrite().
4504741Smasputra 		 */
4505741Smasputra 		ASSERT(error == 0 || error == ENOMEM);
4506898Skais 		if (stp->sd_wputdatafunc != NULL) {
4507898Skais 			newmp = (stp->sd_wputdatafunc)(SOTOV(so), mp, NULL,
4508898Skais 			    NULL, NULL, NULL);
4509898Skais 			if (newmp == NULL) {
4510898Skais 				/* The caller will free mp */
4511898Skais 				return (ECOMM);
4512898Skais 			}
4513898Skais 			mp = newmp;
4514898Skais 		}
4515741Smasputra 		tcp_wput(tcp_wq, mp);
4516741Smasputra 
4517741Smasputra 		wflag |= NOINTR;
4518741Smasputra 
4519741Smasputra 		if (uiop->uio_resid == 0) {	/* No more data; we're done */
4520741Smasputra 			ASSERT(error == 0);
4521741Smasputra 			break;
4522741Smasputra 		} else if (error == ENOMEM || !canput(tcp_wq) || (stp->sd_flag &
4523741Smasputra 		    (STWRERR|STRHUP|STPLEX|STRDELIM|OLDNDELAY))) {
4524741Smasputra slow_send:
4525741Smasputra 			/*
4526741Smasputra 			 * We were able to send down partial data using
4527741Smasputra 			 * the direct call interface, but are now relying
4528741Smasputra 			 * on strwrite() to handle the non-fastpath cases.
4529741Smasputra 			 * If the socket is blocking we will sleep in
4530741Smasputra 			 * strwaitq() until write is permitted, otherwise,
4531741Smasputra 			 * we will need to return the amount of bytes
4532741Smasputra 			 * written so far back to the app.  This is the
4533741Smasputra 			 * reason why we pass NOINTR flag to strwrite()
4534741Smasputra 			 * for non-blocking socket, because we don't want
4535741Smasputra 			 * to return EAGAIN when portion of the user data
4536741Smasputra 			 * has actually been sent down.
4537741Smasputra 			 */
4538741Smasputra 			return (strwrite_common(SOTOV(so), uiop, cr, wflag));
4539741Smasputra 		}
4540741Smasputra 	}
4541741Smasputra 	return (0);
4542741Smasputra }
4543741Smasputra 
4544741Smasputra /*
45450Sstevel@tonic-gate  * Update so_faddr by asking the transport (unless AF_UNIX).
45460Sstevel@tonic-gate  */
45470Sstevel@tonic-gate int
45480Sstevel@tonic-gate sotpi_getpeername(struct sonode *so)
45490Sstevel@tonic-gate {
45500Sstevel@tonic-gate 	struct strbuf	strbuf;
45510Sstevel@tonic-gate 	int		error = 0, res;
45520Sstevel@tonic-gate 	void		*addr;
45530Sstevel@tonic-gate 	t_uscalar_t	addrlen;
45540Sstevel@tonic-gate 	k_sigset_t	smask;
45550Sstevel@tonic-gate 
45560Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername(%p) %s\n",
45570Sstevel@tonic-gate 		so, pr_state(so->so_state, so->so_mode)));
45580Sstevel@tonic-gate 
45590Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
45600Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
45610Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
45620Sstevel@tonic-gate 		error = ENOTCONN;
45630Sstevel@tonic-gate 		goto done;
45640Sstevel@tonic-gate 	}
45650Sstevel@tonic-gate 	/* Added this check for X/Open */
45660Sstevel@tonic-gate 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
45670Sstevel@tonic-gate 		error = EINVAL;
45680Sstevel@tonic-gate 		if (xnet_check_print) {
45690Sstevel@tonic-gate 			printf("sockfs: X/Open getpeername check => EINVAL\n");
45700Sstevel@tonic-gate 		}
45710Sstevel@tonic-gate 		goto done;
45720Sstevel@tonic-gate 	}
45730Sstevel@tonic-gate #ifdef DEBUG
45740Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername (local): %s\n",
45750Sstevel@tonic-gate 		pr_addr(so->so_family, so->so_faddr_sa,
45760Sstevel@tonic-gate 			(t_uscalar_t)so->so_faddr_len)));
45770Sstevel@tonic-gate #endif /* DEBUG */
45780Sstevel@tonic-gate 
45791548Srshoaib 	if (so->so_family == AF_UNIX) {
45800Sstevel@tonic-gate 		/* Transport has different name space - return local info */
45810Sstevel@tonic-gate 		error = 0;
45820Sstevel@tonic-gate 		goto done;
45830Sstevel@tonic-gate 	}
45840Sstevel@tonic-gate 
45850Sstevel@tonic-gate 	ASSERT(so->so_faddr_sa);
45860Sstevel@tonic-gate 	/* Allocate local buffer to use with ioctl */
45870Sstevel@tonic-gate 	addrlen = (t_uscalar_t)so->so_faddr_maxlen;
45880Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
45890Sstevel@tonic-gate 	addr = kmem_alloc(addrlen, KM_SLEEP);
45900Sstevel@tonic-gate 
45910Sstevel@tonic-gate 	/*
45920Sstevel@tonic-gate 	 * Issue TI_GETPEERNAME with signals masked.
45930Sstevel@tonic-gate 	 * Put the result in so_faddr_sa so that getpeername works after
45940Sstevel@tonic-gate 	 * a shutdown(output).
45950Sstevel@tonic-gate 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
45960Sstevel@tonic-gate 	 * back to the socket.
45970Sstevel@tonic-gate 	 */
45980Sstevel@tonic-gate 	strbuf.buf = addr;
45990Sstevel@tonic-gate 	strbuf.maxlen = addrlen;
46000Sstevel@tonic-gate 	strbuf.len = 0;
46010Sstevel@tonic-gate 
46020Sstevel@tonic-gate 	sigintr(&smask, 0);
46030Sstevel@tonic-gate 	res = 0;
46040Sstevel@tonic-gate 	ASSERT(CRED());
46050Sstevel@tonic-gate 	error = strioctl(SOTOV(so), TI_GETPEERNAME, (intptr_t)&strbuf,
46060Sstevel@tonic-gate 			0, K_TO_K, CRED(), &res);
46070Sstevel@tonic-gate 	sigunintr(&smask);
46080Sstevel@tonic-gate 
46090Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
46100Sstevel@tonic-gate 	/*
46110Sstevel@tonic-gate 	 * If there is an error record the error in so_error put don't fail
46120Sstevel@tonic-gate 	 * the getpeername. Instead fallback on the recorded
46130Sstevel@tonic-gate 	 * so->so_faddr_sa.
46140Sstevel@tonic-gate 	 */
46150Sstevel@tonic-gate 	if (error) {
46160Sstevel@tonic-gate 		/*
46170Sstevel@tonic-gate 		 * Various stream head errors can be returned to the ioctl.
46180Sstevel@tonic-gate 		 * However, it is impossible to determine which ones of
46190Sstevel@tonic-gate 		 * these are really socket level errors that were incorrectly
46200Sstevel@tonic-gate 		 * consumed by the ioctl. Thus this code silently ignores the
46210Sstevel@tonic-gate 		 * error - to code explicitly does not reinstate the error
46220Sstevel@tonic-gate 		 * using soseterror().
46230Sstevel@tonic-gate 		 * Experiments have shows that at least this set of
46240Sstevel@tonic-gate 		 * errors are reported and should not be reinstated on the
46250Sstevel@tonic-gate 		 * socket:
46260Sstevel@tonic-gate 		 *	EINVAL	E.g. if an I_LINK was in effect when
46270Sstevel@tonic-gate 		 *		getpeername was called.
46280Sstevel@tonic-gate 		 *	EPIPE	The ioctl error semantics prefer the write
46290Sstevel@tonic-gate 		 *		side error over the read side error.
46300Sstevel@tonic-gate 		 *	ENOTCONN The transport just got disconnected but
46310Sstevel@tonic-gate 		 *		sockfs had not yet seen the T_DISCON_IND
46320Sstevel@tonic-gate 		 *		when issuing the ioctl.
46330Sstevel@tonic-gate 		 */
46340Sstevel@tonic-gate 		error = 0;
46350Sstevel@tonic-gate 	} else if (res == 0 && strbuf.len > 0 &&
46360Sstevel@tonic-gate 	    (so->so_state & SS_ISCONNECTED)) {
46370Sstevel@tonic-gate 		ASSERT(strbuf.len <= (int)so->so_faddr_maxlen);
46380Sstevel@tonic-gate 		so->so_faddr_len = (socklen_t)strbuf.len;
46390Sstevel@tonic-gate 		bcopy(addr, so->so_faddr_sa, so->so_faddr_len);
46400Sstevel@tonic-gate 		so->so_state |= SS_FADDR_VALID;
46410Sstevel@tonic-gate 	}
46420Sstevel@tonic-gate 	kmem_free(addr, addrlen);
46430Sstevel@tonic-gate #ifdef DEBUG
46440Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getpeername (tp): %s\n",
46450Sstevel@tonic-gate 			pr_addr(so->so_family, so->so_faddr_sa,
46460Sstevel@tonic-gate 				(t_uscalar_t)so->so_faddr_len)));
46470Sstevel@tonic-gate #endif /* DEBUG */
46480Sstevel@tonic-gate done:
46490Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
46500Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
46510Sstevel@tonic-gate 	return (error);
46520Sstevel@tonic-gate }
46530Sstevel@tonic-gate 
46540Sstevel@tonic-gate /*
46550Sstevel@tonic-gate  * Update so_laddr by asking the transport (unless AF_UNIX).
46560Sstevel@tonic-gate  */
46570Sstevel@tonic-gate int
46580Sstevel@tonic-gate sotpi_getsockname(struct sonode *so)
46590Sstevel@tonic-gate {
46600Sstevel@tonic-gate 	struct strbuf	strbuf;
46610Sstevel@tonic-gate 	int		error = 0, res;
46620Sstevel@tonic-gate 	void		*addr;
46630Sstevel@tonic-gate 	t_uscalar_t	addrlen;
46640Sstevel@tonic-gate 	k_sigset_t	smask;
46650Sstevel@tonic-gate 
46660Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname(%p) %s\n",
46670Sstevel@tonic-gate 		so, pr_state(so->so_state, so->so_mode)));
46680Sstevel@tonic-gate 
46690Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
46700Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
46710Sstevel@tonic-gate 	if (!(so->so_state & SS_ISBOUND) && so->so_family != AF_UNIX) {
46720Sstevel@tonic-gate 		/* Return an all zero address except for the family */
46730Sstevel@tonic-gate 		if (so->so_family == AF_INET)
46740Sstevel@tonic-gate 			so->so_laddr_len = (socklen_t)sizeof (sin_t);
46750Sstevel@tonic-gate 		else if (so->so_family == AF_INET6)
46760Sstevel@tonic-gate 			so->so_laddr_len = (socklen_t)sizeof (sin6_t);
46770Sstevel@tonic-gate 		ASSERT(so->so_laddr_len <= so->so_laddr_maxlen);
46780Sstevel@tonic-gate 		bzero(so->so_laddr_sa, so->so_laddr_len);
46790Sstevel@tonic-gate 		/*
46800Sstevel@tonic-gate 		 * Can not assume there is a sa_family for all
46810Sstevel@tonic-gate 		 * protocol families.
46820Sstevel@tonic-gate 		 */
46830Sstevel@tonic-gate 		if (so->so_family == AF_INET || so->so_family == AF_INET6)
46840Sstevel@tonic-gate 			so->so_laddr_sa->sa_family = so->so_family;
46850Sstevel@tonic-gate 	}
46860Sstevel@tonic-gate #ifdef DEBUG
46870Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname (local): %s\n",
46880Sstevel@tonic-gate 		pr_addr(so->so_family, so->so_laddr_sa,
46890Sstevel@tonic-gate 			(t_uscalar_t)so->so_laddr_len)));
46900Sstevel@tonic-gate #endif /* DEBUG */
46910Sstevel@tonic-gate 	if (so->so_family == AF_UNIX) {
46920Sstevel@tonic-gate 		/* Transport has different name space - return local info */
46930Sstevel@tonic-gate 		error = 0;
46940Sstevel@tonic-gate 		goto done;
46950Sstevel@tonic-gate 	}
46961156Sblu 	if (!(so->so_state & SS_ISBOUND)) {
46971156Sblu 		/* If not bound, then nothing to return. */
46981156Sblu 		error = 0;
46991156Sblu 		goto done;
47001156Sblu 	}
47010Sstevel@tonic-gate 	/* Allocate local buffer to use with ioctl */
47020Sstevel@tonic-gate 	addrlen = (t_uscalar_t)so->so_laddr_maxlen;
47030Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
47040Sstevel@tonic-gate 	addr = kmem_alloc(addrlen, KM_SLEEP);
47050Sstevel@tonic-gate 
47060Sstevel@tonic-gate 	/*
47070Sstevel@tonic-gate 	 * Issue TI_GETMYNAME with signals masked.
47080Sstevel@tonic-gate 	 * Put the result in so_laddr_sa so that getsockname works after
47090Sstevel@tonic-gate 	 * a shutdown(output).
47100Sstevel@tonic-gate 	 * If the ioctl fails (e.g. due to a ECONNRESET) the error is reposted
47110Sstevel@tonic-gate 	 * back to the socket.
47120Sstevel@tonic-gate 	 */
47130Sstevel@tonic-gate 	strbuf.buf = addr;
47140Sstevel@tonic-gate 	strbuf.maxlen = addrlen;
47150Sstevel@tonic-gate 	strbuf.len = 0;
47160Sstevel@tonic-gate 
47170Sstevel@tonic-gate 	sigintr(&smask, 0);
47180Sstevel@tonic-gate 	res = 0;
47190Sstevel@tonic-gate 	ASSERT(CRED());
47200Sstevel@tonic-gate 	error = strioctl(SOTOV(so), TI_GETMYNAME, (intptr_t)&strbuf,
47210Sstevel@tonic-gate 			0, K_TO_K, CRED(), &res);
47220Sstevel@tonic-gate 	sigunintr(&smask);
47230Sstevel@tonic-gate 
47240Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
47250Sstevel@tonic-gate 	/*
47260Sstevel@tonic-gate 	 * If there is an error record the error in so_error put don't fail
47270Sstevel@tonic-gate 	 * the getsockname. Instead fallback on the recorded
47280Sstevel@tonic-gate 	 * so->so_laddr_sa.
47290Sstevel@tonic-gate 	 */
47300Sstevel@tonic-gate 	if (error) {
47310Sstevel@tonic-gate 		/*
47320Sstevel@tonic-gate 		 * Various stream head errors can be returned to the ioctl.
47330Sstevel@tonic-gate 		 * However, it is impossible to determine which ones of
47340Sstevel@tonic-gate 		 * these are really socket level errors that were incorrectly
47350Sstevel@tonic-gate 		 * consumed by the ioctl. Thus this code silently ignores the
47360Sstevel@tonic-gate 		 * error - to code explicitly does not reinstate the error
47370Sstevel@tonic-gate 		 * using soseterror().
47380Sstevel@tonic-gate 		 * Experiments have shows that at least this set of
47390Sstevel@tonic-gate 		 * errors are reported and should not be reinstated on the
47400Sstevel@tonic-gate 		 * socket:
47410Sstevel@tonic-gate 		 *	EINVAL	E.g. if an I_LINK was in effect when
47420Sstevel@tonic-gate 		 *		getsockname was called.
47430Sstevel@tonic-gate 		 *	EPIPE	The ioctl error semantics prefer the write
47440Sstevel@tonic-gate 		 *		side error over the read side error.
47450Sstevel@tonic-gate 		 */
47460Sstevel@tonic-gate 		error = 0;
47470Sstevel@tonic-gate 	} else if (res == 0 && strbuf.len > 0 &&
47480Sstevel@tonic-gate 	    (so->so_state & SS_ISBOUND)) {
47490Sstevel@tonic-gate 		ASSERT(strbuf.len <= (int)so->so_laddr_maxlen);
47500Sstevel@tonic-gate 		so->so_laddr_len = (socklen_t)strbuf.len;
47510Sstevel@tonic-gate 		bcopy(addr, so->so_laddr_sa, so->so_laddr_len);
47520Sstevel@tonic-gate 		so->so_state |= SS_LADDR_VALID;
47530Sstevel@tonic-gate 	}
47540Sstevel@tonic-gate 	kmem_free(addr, addrlen);
47550Sstevel@tonic-gate #ifdef DEBUG
47560Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockname (tp): %s\n",
47570Sstevel@tonic-gate 			pr_addr(so->so_family, so->so_laddr_sa,
47580Sstevel@tonic-gate 				(t_uscalar_t)so->so_laddr_len)));
47590Sstevel@tonic-gate #endif /* DEBUG */
47600Sstevel@tonic-gate done:
47610Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
47620Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
47630Sstevel@tonic-gate 	return (error);
47640Sstevel@tonic-gate }
47650Sstevel@tonic-gate 
47660Sstevel@tonic-gate /*
47670Sstevel@tonic-gate  * Get socket options. For SOL_SOCKET options some options are handled
47680Sstevel@tonic-gate  * by the sockfs while others use the value recorded in the sonode as a
47690Sstevel@tonic-gate  * fallback should the T_SVR4_OPTMGMT_REQ fail.
47700Sstevel@tonic-gate  *
47710Sstevel@tonic-gate  * On the return most *optlenp bytes are copied to optval.
47720Sstevel@tonic-gate  */
47730Sstevel@tonic-gate int
47740Sstevel@tonic-gate sotpi_getsockopt(struct sonode *so, int level, int option_name,
47750Sstevel@tonic-gate 		void *optval, socklen_t *optlenp, int flags)
47760Sstevel@tonic-gate {
47770Sstevel@tonic-gate 	struct T_optmgmt_req	optmgmt_req;
47780Sstevel@tonic-gate 	struct T_optmgmt_ack	*optmgmt_ack;
47790Sstevel@tonic-gate 	struct opthdr		oh;
47800Sstevel@tonic-gate 	struct opthdr		*opt_res;
47810Sstevel@tonic-gate 	mblk_t			*mp = NULL;
47820Sstevel@tonic-gate 	int			error = 0;
47830Sstevel@tonic-gate 	void			*option = NULL;	/* Set if fallback value */
47840Sstevel@tonic-gate 	t_uscalar_t		maxlen = *optlenp;
47850Sstevel@tonic-gate 	t_uscalar_t		len;
47860Sstevel@tonic-gate 	uint32_t		value;
47870Sstevel@tonic-gate 
47880Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_getsockopt(%p, 0x%x, 0x%x, %p, %p) %s\n",
47890Sstevel@tonic-gate 			so, level, option_name, optval, optlenp,
47900Sstevel@tonic-gate 			pr_state(so->so_state, so->so_mode)));
47910Sstevel@tonic-gate 
47920Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
47930Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
47940Sstevel@tonic-gate 
47950Sstevel@tonic-gate 	/*
47960Sstevel@tonic-gate 	 * Check for SOL_SOCKET options.
47970Sstevel@tonic-gate 	 * Certain SOL_SOCKET options are returned directly whereas
47980Sstevel@tonic-gate 	 * others only provide a default (fallback) value should
47990Sstevel@tonic-gate 	 * the T_SVR4_OPTMGMT_REQ fail.
48000Sstevel@tonic-gate 	 */
48010Sstevel@tonic-gate 	if (level == SOL_SOCKET) {
48020Sstevel@tonic-gate 		/* Check parameters */
48030Sstevel@tonic-gate 		switch (option_name) {
48040Sstevel@tonic-gate 		case SO_TYPE:
48050Sstevel@tonic-gate 		case SO_ERROR:
48060Sstevel@tonic-gate 		case SO_DEBUG:
48070Sstevel@tonic-gate 		case SO_ACCEPTCONN:
48080Sstevel@tonic-gate 		case SO_REUSEADDR:
48090Sstevel@tonic-gate 		case SO_KEEPALIVE:
48100Sstevel@tonic-gate 		case SO_DONTROUTE:
48110Sstevel@tonic-gate 		case SO_BROADCAST:
48120Sstevel@tonic-gate 		case SO_USELOOPBACK:
48130Sstevel@tonic-gate 		case SO_OOBINLINE:
48140Sstevel@tonic-gate 		case SO_SNDBUF:
48150Sstevel@tonic-gate 		case SO_RCVBUF:
48160Sstevel@tonic-gate #ifdef notyet
48170Sstevel@tonic-gate 		case SO_SNDLOWAT:
48180Sstevel@tonic-gate 		case SO_RCVLOWAT:
48190Sstevel@tonic-gate 		case SO_SNDTIMEO:
48200Sstevel@tonic-gate 		case SO_RCVTIMEO:
48210Sstevel@tonic-gate #endif /* notyet */
48223388Skcpoon 		case SO_DOMAIN:
48230Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
48240Sstevel@tonic-gate 			if (maxlen < (t_uscalar_t)sizeof (int32_t)) {
48250Sstevel@tonic-gate 				error = EINVAL;
48260Sstevel@tonic-gate 				eprintsoline(so, error);
48270Sstevel@tonic-gate 				goto done2;
48280Sstevel@tonic-gate 			}
48290Sstevel@tonic-gate 			break;
48300Sstevel@tonic-gate 		case SO_LINGER:
48310Sstevel@tonic-gate 			if (maxlen < (t_uscalar_t)sizeof (struct linger)) {
48320Sstevel@tonic-gate 				error = EINVAL;
48330Sstevel@tonic-gate 				eprintsoline(so, error);
48340Sstevel@tonic-gate 				goto done2;
48350Sstevel@tonic-gate 			}
48360Sstevel@tonic-gate 			break;
48370Sstevel@tonic-gate 		}
48380Sstevel@tonic-gate 
48390Sstevel@tonic-gate 		len = (t_uscalar_t)sizeof (uint32_t);	/* Default */
48400Sstevel@tonic-gate 
48410Sstevel@tonic-gate 		switch (option_name) {
48420Sstevel@tonic-gate 		case SO_TYPE:
48430Sstevel@tonic-gate 			value = so->so_type;
48440Sstevel@tonic-gate 			option = &value;
48450Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
48460Sstevel@tonic-gate 
48470Sstevel@tonic-gate 		case SO_ERROR:
48480Sstevel@tonic-gate 			value = sogeterr(so);
48490Sstevel@tonic-gate 			option = &value;
48500Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
48510Sstevel@tonic-gate 
48520Sstevel@tonic-gate 		case SO_ACCEPTCONN:
48530Sstevel@tonic-gate 			if (so->so_state & SS_ACCEPTCONN)
48540Sstevel@tonic-gate 				value = SO_ACCEPTCONN;
48550Sstevel@tonic-gate 			else
48560Sstevel@tonic-gate 				value = 0;
48570Sstevel@tonic-gate #ifdef DEBUG
48580Sstevel@tonic-gate 			if (value) {
48590Sstevel@tonic-gate 				dprintso(so, 1,
48600Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x is set\n",
48610Sstevel@tonic-gate 				    option_name));
48620Sstevel@tonic-gate 			} else {
48630Sstevel@tonic-gate 				dprintso(so, 1,
48640Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x not set\n",
48650Sstevel@tonic-gate 				    option_name));
48660Sstevel@tonic-gate 			}
48670Sstevel@tonic-gate #endif /* DEBUG */
48680Sstevel@tonic-gate 			option = &value;
48690Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
48700Sstevel@tonic-gate 
48710Sstevel@tonic-gate 		case SO_DEBUG:
48720Sstevel@tonic-gate 		case SO_REUSEADDR:
48730Sstevel@tonic-gate 		case SO_KEEPALIVE:
48740Sstevel@tonic-gate 		case SO_DONTROUTE:
48750Sstevel@tonic-gate 		case SO_BROADCAST:
48760Sstevel@tonic-gate 		case SO_USELOOPBACK:
48770Sstevel@tonic-gate 		case SO_OOBINLINE:
48780Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
48790Sstevel@tonic-gate 			value = (so->so_options & option_name);
48800Sstevel@tonic-gate #ifdef DEBUG
48810Sstevel@tonic-gate 			if (value) {
48820Sstevel@tonic-gate 				dprintso(so, 1,
48830Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x is set\n",
48840Sstevel@tonic-gate 				    option_name));
48850Sstevel@tonic-gate 			} else {
48860Sstevel@tonic-gate 				dprintso(so, 1,
48870Sstevel@tonic-gate 				    ("sotpi_getsockopt: 0x%x not set\n",
48880Sstevel@tonic-gate 				    option_name));
48890Sstevel@tonic-gate 			}
48900Sstevel@tonic-gate #endif /* DEBUG */
48910Sstevel@tonic-gate 			option = &value;
48920Sstevel@tonic-gate 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
48930Sstevel@tonic-gate 
48940Sstevel@tonic-gate 		/*
48950Sstevel@tonic-gate 		 * The following options are only returned by sockfs when the
48960Sstevel@tonic-gate 		 * T_SVR4_OPTMGMT_REQ fails.
48970Sstevel@tonic-gate 		 */
48980Sstevel@tonic-gate 		case SO_LINGER:
48990Sstevel@tonic-gate 			option = &so->so_linger;
49000Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (struct linger);
49010Sstevel@tonic-gate 			break;
49020Sstevel@tonic-gate 		case SO_SNDBUF: {
49030Sstevel@tonic-gate 			ssize_t lvalue;
49040Sstevel@tonic-gate 
49050Sstevel@tonic-gate 			/*
49060Sstevel@tonic-gate 			 * If the option has not been set then get a default
49070Sstevel@tonic-gate 			 * value from the read queue. This value is
49080Sstevel@tonic-gate 			 * returned if the transport fails
49090Sstevel@tonic-gate 			 * the T_SVR4_OPTMGMT_REQ.
49100Sstevel@tonic-gate 			 */
49110Sstevel@tonic-gate 			lvalue = so->so_sndbuf;
49120Sstevel@tonic-gate 			if (lvalue == 0) {
49130Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
49140Sstevel@tonic-gate 				(void) strqget(strvp2wq(SOTOV(so))->q_next,
49150Sstevel@tonic-gate 						QHIWAT, 0, &lvalue);
49160Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
49170Sstevel@tonic-gate 				dprintso(so, 1,
49180Sstevel@tonic-gate 				    ("got SO_SNDBUF %ld from q\n", lvalue));
49190Sstevel@tonic-gate 			}
49200Sstevel@tonic-gate 			value = (int)lvalue;
49210Sstevel@tonic-gate 			option = &value;
49220Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (so->so_sndbuf);
49230Sstevel@tonic-gate 			break;
49240Sstevel@tonic-gate 		}
49250Sstevel@tonic-gate 		case SO_RCVBUF: {
49260Sstevel@tonic-gate 			ssize_t lvalue;
49270Sstevel@tonic-gate 
49280Sstevel@tonic-gate 			/*
49290Sstevel@tonic-gate 			 * If the option has not been set then get a default
49300Sstevel@tonic-gate 			 * value from the read queue. This value is
49310Sstevel@tonic-gate 			 * returned if the transport fails
49320Sstevel@tonic-gate 			 * the T_SVR4_OPTMGMT_REQ.
49330Sstevel@tonic-gate 			 *
49340Sstevel@tonic-gate 			 * XXX If SO_RCVBUF has been set and this is an
49350Sstevel@tonic-gate 			 * XPG 4.2 application then do not ask the transport
49360Sstevel@tonic-gate 			 * since the transport might adjust the value and not
49370Sstevel@tonic-gate 			 * return exactly what was set by the application.
49380Sstevel@tonic-gate 			 * For non-XPG 4.2 application we return the value
49390Sstevel@tonic-gate 			 * that the transport is actually using.
49400Sstevel@tonic-gate 			 */
49410Sstevel@tonic-gate 			lvalue = so->so_rcvbuf;
49420Sstevel@tonic-gate 			if (lvalue == 0) {
49430Sstevel@tonic-gate 				mutex_exit(&so->so_lock);
49440Sstevel@tonic-gate 				(void) strqget(RD(strvp2wq(SOTOV(so))),
49450Sstevel@tonic-gate 						QHIWAT, 0, &lvalue);
49460Sstevel@tonic-gate 				mutex_enter(&so->so_lock);
49470Sstevel@tonic-gate 				dprintso(so, 1,
49480Sstevel@tonic-gate 				    ("got SO_RCVBUF %ld from q\n", lvalue));
49490Sstevel@tonic-gate 			} else if (flags & _SOGETSOCKOPT_XPG4_2) {
49500Sstevel@tonic-gate 				value = (int)lvalue;
49510Sstevel@tonic-gate 				option = &value;
49520Sstevel@tonic-gate 				goto copyout;	/* skip asking transport */
49530Sstevel@tonic-gate 			}
49540Sstevel@tonic-gate 			value = (int)lvalue;
49550Sstevel@tonic-gate 			option = &value;
49560Sstevel@tonic-gate 			len = (t_uscalar_t)sizeof (so->so_rcvbuf);
49570Sstevel@tonic-gate 			break;
49580Sstevel@tonic-gate 		}
49593388Skcpoon 		case SO_DOMAIN:
49603388Skcpoon 			value = so->so_family;
49613388Skcpoon 			option = &value;
49623388Skcpoon 			goto copyout; /* No need to issue T_SVR4_OPTMGMT_REQ */
49633388Skcpoon 
49640Sstevel@tonic-gate #ifdef notyet
49650Sstevel@tonic-gate 		/*
49660Sstevel@tonic-gate 		 * We do not implement the semantics of these options
49670Sstevel@tonic-gate 		 * thus we shouldn't implement the options either.
49680Sstevel@tonic-gate 		 */
49690Sstevel@tonic-gate 		case SO_SNDLOWAT:
49700Sstevel@tonic-gate 			value = so->so_sndlowat;
49710Sstevel@tonic-gate 			option = &value;
49720Sstevel@tonic-gate 			break;
49730Sstevel@tonic-gate 		case SO_RCVLOWAT:
49740Sstevel@tonic-gate 			value = so->so_rcvlowat;
49750Sstevel@tonic-gate 			option = &value;
49760Sstevel@tonic-gate 			break;
49770Sstevel@tonic-gate 		case SO_SNDTIMEO:
49780Sstevel@tonic-gate 			value = so->so_sndtimeo;
49790Sstevel@tonic-gate 			option = &value;
49800Sstevel@tonic-gate 			break;
49810Sstevel@tonic-gate 		case SO_RCVTIMEO:
49820Sstevel@tonic-gate 			value = so->so_rcvtimeo;
49830Sstevel@tonic-gate 			option = &value;
49840Sstevel@tonic-gate 			break;
49850Sstevel@tonic-gate #endif /* notyet */
49860Sstevel@tonic-gate 		}
49870Sstevel@tonic-gate 	}
49880Sstevel@tonic-gate 
49890Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
49900Sstevel@tonic-gate 
49910Sstevel@tonic-gate 	/* Send request */
49920Sstevel@tonic-gate 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
49930Sstevel@tonic-gate 	optmgmt_req.MGMT_flags = T_CHECK;
49940Sstevel@tonic-gate 	optmgmt_req.OPT_length = (t_scalar_t)(sizeof (oh) + maxlen);
49950Sstevel@tonic-gate 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
49960Sstevel@tonic-gate 
49970Sstevel@tonic-gate 	oh.level = level;
49980Sstevel@tonic-gate 	oh.name = option_name;
49990Sstevel@tonic-gate 	oh.len = maxlen;
50000Sstevel@tonic-gate 
50010Sstevel@tonic-gate 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
50020Sstevel@tonic-gate 	    &oh, sizeof (oh), NULL, maxlen, 0, _ALLOC_SLEEP);
50030Sstevel@tonic-gate 	/* Let option management work in the presence of data flow control */
50040Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
50050Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
50060Sstevel@tonic-gate 	mp = NULL;
50070Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
50080Sstevel@tonic-gate 	if (error) {
50090Sstevel@tonic-gate 		eprintsoline(so, error);
50100Sstevel@tonic-gate 		goto done2;
50110Sstevel@tonic-gate 	}
50120Sstevel@tonic-gate 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
50130Sstevel@tonic-gate 	    (t_uscalar_t)(sizeof (*optmgmt_ack) + sizeof (*opt_res)), &mp, 0);
50140Sstevel@tonic-gate 	if (error) {
50150Sstevel@tonic-gate 		if (option != NULL) {
50160Sstevel@tonic-gate 			/* We have a fallback value */
50170Sstevel@tonic-gate 			error = 0;
50180Sstevel@tonic-gate 			goto copyout;
50190Sstevel@tonic-gate 		}
50200Sstevel@tonic-gate 		eprintsoline(so, error);
50210Sstevel@tonic-gate 		goto done2;
50220Sstevel@tonic-gate 	}
50230Sstevel@tonic-gate 	ASSERT(mp);
50240Sstevel@tonic-gate 	optmgmt_ack = (struct T_optmgmt_ack *)mp->b_rptr;
50250Sstevel@tonic-gate 	opt_res = (struct opthdr *)sogetoff(mp, optmgmt_ack->OPT_offset,
50260Sstevel@tonic-gate 			optmgmt_ack->OPT_length, __TPI_ALIGN_SIZE);
50270Sstevel@tonic-gate 	if (opt_res == NULL) {
50280Sstevel@tonic-gate 		if (option != NULL) {
50290Sstevel@tonic-gate 			/* We have a fallback value */
50300Sstevel@tonic-gate 			error = 0;
50310Sstevel@tonic-gate 			goto copyout;
50320Sstevel@tonic-gate 		}
50330Sstevel@tonic-gate 		error = EPROTO;
50340Sstevel@tonic-gate 		eprintsoline(so, error);
50350Sstevel@tonic-gate 		goto done;
50360Sstevel@tonic-gate 	}
50370Sstevel@tonic-gate 	option = &opt_res[1];
50380Sstevel@tonic-gate 
50390Sstevel@tonic-gate 	/* check to ensure that the option is within bounds */
50400Sstevel@tonic-gate 	if (((uintptr_t)option + opt_res->len < (uintptr_t)option) ||
50410Sstevel@tonic-gate 		(uintptr_t)option + opt_res->len > (uintptr_t)mp->b_wptr) {
50420Sstevel@tonic-gate 		if (option != NULL) {
50430Sstevel@tonic-gate 			/* We have a fallback value */
50440Sstevel@tonic-gate 			error = 0;
50450Sstevel@tonic-gate 			goto copyout;
50460Sstevel@tonic-gate 		}
50470Sstevel@tonic-gate 		error = EPROTO;
50480Sstevel@tonic-gate 		eprintsoline(so, error);
50490Sstevel@tonic-gate 		goto done;
50500Sstevel@tonic-gate 	}
50510Sstevel@tonic-gate 
50520Sstevel@tonic-gate 	len = opt_res->len;
50530Sstevel@tonic-gate 
50540Sstevel@tonic-gate copyout: {
50550Sstevel@tonic-gate 		t_uscalar_t size = MIN(len, maxlen);
50560Sstevel@tonic-gate 		bcopy(option, optval, size);
50570Sstevel@tonic-gate 		bcopy(&size, optlenp, sizeof (size));
50580Sstevel@tonic-gate 	}
50590Sstevel@tonic-gate done:
50600Sstevel@tonic-gate 	freemsg(mp);
50610Sstevel@tonic-gate done2:
50620Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
50630Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
50640Sstevel@tonic-gate 	return (error);
50650Sstevel@tonic-gate }
50660Sstevel@tonic-gate 
50670Sstevel@tonic-gate /*
50680Sstevel@tonic-gate  * Set socket options. All options are passed down in a T_SVR4_OPTMGMT_REQ.
50690Sstevel@tonic-gate  * SOL_SOCKET options are also recorded in the sonode. A setsockopt for
50700Sstevel@tonic-gate  * SOL_SOCKET options will not fail just because the T_SVR4_OPTMGMT_REQ fails -
50710Sstevel@tonic-gate  * setsockopt has to work even if the transport does not support the option.
50720Sstevel@tonic-gate  */
50730Sstevel@tonic-gate int
50740Sstevel@tonic-gate sotpi_setsockopt(struct sonode *so, int level, int option_name,
50750Sstevel@tonic-gate 	const void *optval, t_uscalar_t optlen)
50760Sstevel@tonic-gate {
50770Sstevel@tonic-gate 	struct T_optmgmt_req	optmgmt_req;
50780Sstevel@tonic-gate 	struct opthdr		oh;
50790Sstevel@tonic-gate 	mblk_t			*mp;
50800Sstevel@tonic-gate 	int			error = 0;
50810Sstevel@tonic-gate 	boolean_t		handled = B_FALSE;
50820Sstevel@tonic-gate 
50830Sstevel@tonic-gate 	dprintso(so, 1, ("sotpi_setsockopt(%p, 0x%x, 0x%x, %p, %d) %s\n",
50840Sstevel@tonic-gate 			so, level, option_name, optval, optlen,
50850Sstevel@tonic-gate 			pr_state(so->so_state, so->so_mode)));
50860Sstevel@tonic-gate 
50870Sstevel@tonic-gate 
50880Sstevel@tonic-gate 	/* X/Open requires this check */
50890Sstevel@tonic-gate 	if ((so->so_state & SS_CANTSENDMORE) && !xnet_skip_checks) {
50900Sstevel@tonic-gate 		if (xnet_check_print)
50910Sstevel@tonic-gate 			printf("sockfs: X/Open setsockopt check => EINVAL\n");
50920Sstevel@tonic-gate 		return (EINVAL);
50930Sstevel@tonic-gate 	}
50940Sstevel@tonic-gate 
50950Sstevel@tonic-gate 	/* Caller allocates aligned optval, or passes null */
50960Sstevel@tonic-gate 	ASSERT(((uintptr_t)optval & (sizeof (t_scalar_t) - 1)) == 0);
50970Sstevel@tonic-gate 	/* If optval is null optlen is 0, and vice-versa */
50980Sstevel@tonic-gate 	ASSERT(optval != NULL || optlen == 0);
50990Sstevel@tonic-gate 	ASSERT(optlen != 0 || optval == NULL);
51000Sstevel@tonic-gate 
51010Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
51020Sstevel@tonic-gate 	so_lock_single(so);	/* Set SOLOCKED */
51030Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
51040Sstevel@tonic-gate 
51050Sstevel@tonic-gate 	/*
51060Sstevel@tonic-gate 	 * For SOCKET or TCP level options, try to set it here itself
51070Sstevel@tonic-gate 	 * provided socket has not been popped and we know the tcp
51080Sstevel@tonic-gate 	 * structure (stored in so_priv).
51090Sstevel@tonic-gate 	 */
51100Sstevel@tonic-gate 	if ((level == SOL_SOCKET || level == IPPROTO_TCP) &&
51110Sstevel@tonic-gate 	    (so->so_family == AF_INET || so->so_family == AF_INET6) &&
51120Sstevel@tonic-gate 	    (so->so_version == SOV_SOCKSTREAM) && (so->so_priv != NULL)) {
51130Sstevel@tonic-gate 		tcp_t		*tcp = so->so_priv;
51140Sstevel@tonic-gate 		boolean_t	onoff;
51150Sstevel@tonic-gate 
51160Sstevel@tonic-gate #define	intvalue	(*(int32_t *)optval)
51170Sstevel@tonic-gate 
51180Sstevel@tonic-gate 		switch (level) {
51190Sstevel@tonic-gate 		case SOL_SOCKET:
51200Sstevel@tonic-gate 			switch (option_name) {		/* Check length param */
51210Sstevel@tonic-gate 			case SO_DEBUG:
51220Sstevel@tonic-gate 			case SO_REUSEADDR:
51230Sstevel@tonic-gate 			case SO_DONTROUTE:
51240Sstevel@tonic-gate 			case SO_BROADCAST:
51250Sstevel@tonic-gate 			case SO_USELOOPBACK:
51260Sstevel@tonic-gate 			case SO_OOBINLINE:
51270Sstevel@tonic-gate 			case SO_DGRAM_ERRIND:
51280Sstevel@tonic-gate 				if (optlen != (t_uscalar_t)sizeof (int32_t)) {
51290Sstevel@tonic-gate 					error = EINVAL;
51300Sstevel@tonic-gate 					eprintsoline(so, error);
51310Sstevel@tonic-gate 					mutex_enter(&so->so_lock);
51320Sstevel@tonic-gate 					goto done2;
51330Sstevel@tonic-gate 				}
51340Sstevel@tonic-gate 				ASSERT(optval);
51350Sstevel@tonic-gate 				onoff = intvalue != 0;
51360Sstevel@tonic-gate 				handled = B_TRUE;
51370Sstevel@tonic-gate 				break;
51380Sstevel@tonic-gate 			case SO_LINGER:
51390Sstevel@tonic-gate 				if (optlen !=
51400Sstevel@tonic-gate 				    (t_uscalar_t)sizeof (struct linger)) {
51410Sstevel@tonic-gate 					error = EINVAL;
51420Sstevel@tonic-gate 					eprintsoline(so, error);
51430Sstevel@tonic-gate 					mutex_enter(&so->so_lock);
51440Sstevel@tonic-gate 					goto done2;
51450Sstevel@tonic-gate 				}
51460Sstevel@tonic-gate 				ASSERT(optval);
51470Sstevel@tonic-gate 				handled = B_TRUE;
51480Sstevel@tonic-gate 				break;
51490Sstevel@tonic-gate 			}
51500Sstevel@tonic-gate 
51510Sstevel@tonic-gate 			switch (option_name) {			/* Do actions */
51520Sstevel@tonic-gate 			case SO_LINGER: {
51530Sstevel@tonic-gate 				struct linger *lgr = (struct linger *)optval;
51540Sstevel@tonic-gate 
51550Sstevel@tonic-gate 				if (lgr->l_onoff) {
51560Sstevel@tonic-gate 					tcp->tcp_linger = 1;
51570Sstevel@tonic-gate 					tcp->tcp_lingertime = lgr->l_linger;
51580Sstevel@tonic-gate 					so->so_linger.l_onoff = SO_LINGER;
51590Sstevel@tonic-gate 					so->so_options |= SO_LINGER;
51600Sstevel@tonic-gate 				} else {
51610Sstevel@tonic-gate 					tcp->tcp_linger = 0;
51620Sstevel@tonic-gate 					tcp->tcp_lingertime = 0;
51630Sstevel@tonic-gate 					so->so_linger.l_onoff = 0;
51640Sstevel@tonic-gate 					so->so_options &= ~SO_LINGER;
51650Sstevel@tonic-gate 				}
51660Sstevel@tonic-gate 				so->so_linger.l_linger = lgr->l_linger;
51670Sstevel@tonic-gate 				handled = B_TRUE;
51680Sstevel@tonic-gate 				break;
51690Sstevel@tonic-gate 			}
51700Sstevel@tonic-gate 			case SO_DEBUG:
51710Sstevel@tonic-gate 				tcp->tcp_debug = onoff;
51720Sstevel@tonic-gate #ifdef SOCK_TEST
51730Sstevel@tonic-gate 				if (intvalue & 2)
51740Sstevel@tonic-gate 					sock_test_timelimit = 10 * hz;
51750Sstevel@tonic-gate 				else
51760Sstevel@tonic-gate 					sock_test_timelimit = 0;
51770Sstevel@tonic-gate 
51780Sstevel@tonic-gate 				if (intvalue & 4)
51790Sstevel@tonic-gate 					do_useracc = 0;
51800Sstevel@tonic-gate 				else
51810Sstevel@tonic-gate 					do_useracc = 1;
51820Sstevel@tonic-gate #endif /* SOCK_TEST */
51830Sstevel@tonic-gate 				break;
51840Sstevel@tonic-gate 			case SO_DONTROUTE:
51850Sstevel@tonic-gate 				/*
51860Sstevel@tonic-gate 				 * SO_DONTROUTE, SO_USELOOPBACK and
51870Sstevel@tonic-gate 				 * SO_BROADCAST are only of interest to IP.
51880Sstevel@tonic-gate 				 * We track them here only so
51890Sstevel@tonic-gate 				 * that we can report their current value.
51900Sstevel@tonic-gate 				 */
51910Sstevel@tonic-gate 				tcp->tcp_dontroute = onoff;
51920Sstevel@tonic-gate 				if (onoff)
51930Sstevel@tonic-gate 					so->so_options |= option_name;
51940Sstevel@tonic-gate 				else
51950Sstevel@tonic-gate 					so->so_options &= ~option_name;
51960Sstevel@tonic-gate 				break;
51970Sstevel@tonic-gate 			case SO_USELOOPBACK:
51980Sstevel@tonic-gate 				tcp->tcp_useloopback = onoff;
51990Sstevel@tonic-gate 				if (onoff)
52000Sstevel@tonic-gate 					so->so_options |= option_name;
52010Sstevel@tonic-gate 				else
52020Sstevel@tonic-gate 					so->so_options &= ~option_name;
52030Sstevel@tonic-gate 				break;
52040Sstevel@tonic-gate 			case SO_BROADCAST:
52050Sstevel@tonic-gate 				tcp->tcp_broadcast = onoff;
52060Sstevel@tonic-gate 				if (onoff)
52070Sstevel@tonic-gate 					so->so_options |= option_name;
52080Sstevel@tonic-gate 				else
52090Sstevel@tonic-gate 					so->so_options &= ~option_name;
52100Sstevel@tonic-gate 				break;
52110Sstevel@tonic-gate 			case SO_REUSEADDR:
52120Sstevel@tonic-gate 				tcp->tcp_reuseaddr = onoff;
52130Sstevel@tonic-gate 				if (onoff)
52140Sstevel@tonic-gate 					so->so_options |= option_name;
52150Sstevel@tonic-gate 				else
52160Sstevel@tonic-gate 					so->so_options &= ~option_name;
52170Sstevel@tonic-gate 				break;
52180Sstevel@tonic-gate 			case SO_OOBINLINE:
52190Sstevel@tonic-gate 				tcp->tcp_oobinline = onoff;
52200Sstevel@tonic-gate 				if (onoff)
52210Sstevel@tonic-gate 					so->so_options |= option_name;
52220Sstevel@tonic-gate 				else
52230Sstevel@tonic-gate 					so->so_options &= ~option_name;
52240Sstevel@tonic-gate 				break;
52250Sstevel@tonic-gate 			case SO_DGRAM_ERRIND:
52260Sstevel@tonic-gate 				tcp->tcp_dgram_errind = onoff;
52270Sstevel@tonic-gate 				if (onoff)
52280Sstevel@tonic-gate 					so->so_options |= option_name;
52290Sstevel@tonic-gate 				else
52300Sstevel@tonic-gate 					so->so_options &= ~option_name;
52310Sstevel@tonic-gate 				break;
52320Sstevel@tonic-gate 			}
52330Sstevel@tonic-gate 			break;
52340Sstevel@tonic-gate 		case IPPROTO_TCP:
52350Sstevel@tonic-gate 			switch (option_name) {
52360Sstevel@tonic-gate 			case TCP_NODELAY:
52370Sstevel@tonic-gate 				if (optlen != (t_uscalar_t)sizeof (int32_t)) {
52380Sstevel@tonic-gate 					error = EINVAL;
52390Sstevel@tonic-gate 					eprintsoline(so, error);
52400Sstevel@tonic-gate 					mutex_enter(&so->so_lock);
52410Sstevel@tonic-gate 					goto done2;
52420Sstevel@tonic-gate 				}
52430Sstevel@tonic-gate 				ASSERT(optval);
52440Sstevel@tonic-gate 				tcp->tcp_naglim = intvalue ? 1 : tcp->tcp_mss;
52450Sstevel@tonic-gate 				handled = B_TRUE;
52460Sstevel@tonic-gate 				break;
52470Sstevel@tonic-gate 			}
52480Sstevel@tonic-gate 			break;
52490Sstevel@tonic-gate 		default:
52500Sstevel@tonic-gate 			handled = B_FALSE;
52510Sstevel@tonic-gate 			break;
52520Sstevel@tonic-gate 		}
52530Sstevel@tonic-gate 	}
52540Sstevel@tonic-gate 
52550Sstevel@tonic-gate 	if (handled) {
52560Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
52570Sstevel@tonic-gate 		goto done2;
52580Sstevel@tonic-gate 	}
52590Sstevel@tonic-gate 
52600Sstevel@tonic-gate 	optmgmt_req.PRIM_type = T_SVR4_OPTMGMT_REQ;
52610Sstevel@tonic-gate 	optmgmt_req.MGMT_flags = T_NEGOTIATE;
52620Sstevel@tonic-gate 	optmgmt_req.OPT_length = (t_scalar_t)sizeof (oh) + optlen;
52630Sstevel@tonic-gate 	optmgmt_req.OPT_offset = (t_scalar_t)sizeof (optmgmt_req);
52640Sstevel@tonic-gate 
52650Sstevel@tonic-gate 	oh.level = level;
52660Sstevel@tonic-gate 	oh.name = option_name;
52670Sstevel@tonic-gate 	oh.len = optlen;
52680Sstevel@tonic-gate 
52690Sstevel@tonic-gate 	mp = soallocproto3(&optmgmt_req, sizeof (optmgmt_req),
52700Sstevel@tonic-gate 	    &oh, sizeof (oh), optval, optlen, 0, _ALLOC_SLEEP);
52710Sstevel@tonic-gate 	/* Let option management work in the presence of data flow control */
52720Sstevel@tonic-gate 	error = kstrputmsg(SOTOV(so), mp, NULL, 0, 0,
52730Sstevel@tonic-gate 			MSG_BAND|MSG_HOLDSIG|MSG_IGNERROR|MSG_IGNFLOW, 0);
52740Sstevel@tonic-gate 	mp = NULL;
52750Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
52760Sstevel@tonic-gate 	if (error) {
52770Sstevel@tonic-gate 		eprintsoline(so, error);
52780Sstevel@tonic-gate 		goto done;
52790Sstevel@tonic-gate 	}
52800Sstevel@tonic-gate 	error = sowaitprim(so, T_SVR4_OPTMGMT_REQ, T_OPTMGMT_ACK,
52810Sstevel@tonic-gate 	    (t_uscalar_t)sizeof (struct T_optmgmt_ack), &mp, 0);
52820Sstevel@tonic-gate 	if (error) {
52830Sstevel@tonic-gate 		eprintsoline(so, error);
52840Sstevel@tonic-gate 		goto done;
52850Sstevel@tonic-gate 	}
52860Sstevel@tonic-gate 	ASSERT(mp);
52870Sstevel@tonic-gate 	/* No need to verify T_optmgmt_ack */
52880Sstevel@tonic-gate 	freemsg(mp);
52890Sstevel@tonic-gate done:
52900Sstevel@tonic-gate 	/*
52910Sstevel@tonic-gate 	 * Check for SOL_SOCKET options and record their values.
52920Sstevel@tonic-gate 	 * If we know about a SOL_SOCKET parameter and the transport
52930Sstevel@tonic-gate 	 * failed it with TBADOPT or TOUTSTATE (i.e. ENOPROTOOPT or
52940Sstevel@tonic-gate 	 * EPROTO) we let the setsockopt succeed.
52950Sstevel@tonic-gate 	 */
52960Sstevel@tonic-gate 	if (level == SOL_SOCKET) {
52970Sstevel@tonic-gate 		/* Check parameters */
52980Sstevel@tonic-gate 		switch (option_name) {
52990Sstevel@tonic-gate 		case SO_DEBUG:
53000Sstevel@tonic-gate 		case SO_REUSEADDR:
53010Sstevel@tonic-gate 		case SO_KEEPALIVE:
53020Sstevel@tonic-gate 		case SO_DONTROUTE:
53030Sstevel@tonic-gate 		case SO_BROADCAST:
53040Sstevel@tonic-gate 		case SO_USELOOPBACK:
53050Sstevel@tonic-gate 		case SO_OOBINLINE:
53060Sstevel@tonic-gate 		case SO_SNDBUF:
53070Sstevel@tonic-gate 		case SO_RCVBUF:
53080Sstevel@tonic-gate #ifdef notyet
53090Sstevel@tonic-gate 		case SO_SNDLOWAT:
53100Sstevel@tonic-gate 		case SO_RCVLOWAT:
53110Sstevel@tonic-gate 		case SO_SNDTIMEO:
53120Sstevel@tonic-gate 		case SO_RCVTIMEO:
53130Sstevel@tonic-gate #endif /* notyet */
53140Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
53150Sstevel@tonic-gate 			if (optlen != (t_uscalar_t)sizeof (int32_t)) {
53160Sstevel@tonic-gate 				error = EINVAL;
53170Sstevel@tonic-gate 				eprintsoline(so, error);
53180Sstevel@tonic-gate 				goto done2;
53190Sstevel@tonic-gate 			}
53200Sstevel@tonic-gate 			ASSERT(optval);
53210Sstevel@tonic-gate 			handled = B_TRUE;
53220Sstevel@tonic-gate 			break;
53230Sstevel@tonic-gate 		case SO_LINGER:
53240Sstevel@tonic-gate 			if (optlen != (t_uscalar_t)sizeof (struct linger)) {
53250Sstevel@tonic-gate 				error = EINVAL;
53260Sstevel@tonic-gate 				eprintsoline(so, error);
53270Sstevel@tonic-gate 				goto done2;
53280Sstevel@tonic-gate 			}
53290Sstevel@tonic-gate 			ASSERT(optval);
53300Sstevel@tonic-gate 			handled = B_TRUE;
53310Sstevel@tonic-gate 			break;
53320Sstevel@tonic-gate 		}
53330Sstevel@tonic-gate 
53340Sstevel@tonic-gate #define	intvalue	(*(int32_t *)optval)
53350Sstevel@tonic-gate 
53360Sstevel@tonic-gate 		switch (option_name) {
53370Sstevel@tonic-gate 		case SO_TYPE:
53380Sstevel@tonic-gate 		case SO_ERROR:
53390Sstevel@tonic-gate 		case SO_ACCEPTCONN:
53400Sstevel@tonic-gate 			/* Can't be set */
53410Sstevel@tonic-gate 			error = ENOPROTOOPT;
53420Sstevel@tonic-gate 			goto done2;
53430Sstevel@tonic-gate 		case SO_LINGER: {
53440Sstevel@tonic-gate 			struct linger *l = (struct linger *)optval;
53450Sstevel@tonic-gate 
53460Sstevel@tonic-gate 			so->so_linger.l_linger = l->l_linger;
53470Sstevel@tonic-gate 			if (l->l_onoff) {
53480Sstevel@tonic-gate 				so->so_linger.l_onoff = SO_LINGER;
53490Sstevel@tonic-gate 				so->so_options |= SO_LINGER;
53500Sstevel@tonic-gate 			} else {
53510Sstevel@tonic-gate 				so->so_linger.l_onoff = 0;
53520Sstevel@tonic-gate 				so->so_options &= ~SO_LINGER;
53530Sstevel@tonic-gate 			}
53540Sstevel@tonic-gate 			break;
53550Sstevel@tonic-gate 		}
53560Sstevel@tonic-gate 
53570Sstevel@tonic-gate 		case SO_DEBUG:
53580Sstevel@tonic-gate #ifdef SOCK_TEST
53590Sstevel@tonic-gate 			if (intvalue & 2)
53600Sstevel@tonic-gate 				sock_test_timelimit = 10 * hz;
53610Sstevel@tonic-gate 			else
53620Sstevel@tonic-gate 				sock_test_timelimit = 0;
53630Sstevel@tonic-gate 
53640Sstevel@tonic-gate 			if (intvalue & 4)
53650Sstevel@tonic-gate 				do_useracc = 0;
53660Sstevel@tonic-gate 			else
53670Sstevel@tonic-gate 				do_useracc = 1;
53680Sstevel@tonic-gate #endif /* SOCK_TEST */
53690Sstevel@tonic-gate 			/* FALLTHRU */
53700Sstevel@tonic-gate 		case SO_REUSEADDR:
53710Sstevel@tonic-gate 		case SO_KEEPALIVE:
53720Sstevel@tonic-gate 		case SO_DONTROUTE:
53730Sstevel@tonic-gate 		case SO_BROADCAST:
53740Sstevel@tonic-gate 		case SO_USELOOPBACK:
53750Sstevel@tonic-gate 		case SO_OOBINLINE:
53760Sstevel@tonic-gate 		case SO_DGRAM_ERRIND:
53770Sstevel@tonic-gate 			if (intvalue != 0) {
53780Sstevel@tonic-gate 				dprintso(so, 1,
53790Sstevel@tonic-gate 					("sotpi_setsockopt: setting 0x%x\n",
53800Sstevel@tonic-gate 					option_name));
53810Sstevel@tonic-gate 				so->so_options |= option_name;
53820Sstevel@tonic-gate 			} else {
53830Sstevel@tonic-gate 				dprintso(so, 1,
53840Sstevel@tonic-gate 					("sotpi_setsockopt: clearing 0x%x\n",
53850Sstevel@tonic-gate 					option_name));
53860Sstevel@tonic-gate 				so->so_options &= ~option_name;
53870Sstevel@tonic-gate 			}
53880Sstevel@tonic-gate 			break;
53890Sstevel@tonic-gate 		/*
53900Sstevel@tonic-gate 		 * The following options are only returned by us when the
53910Sstevel@tonic-gate 		 * T_SVR4_OPTMGMT_REQ fails.
53920Sstevel@tonic-gate 		 * XXX XPG 4.2 applications retrieve SO_RCVBUF from sockfs
53930Sstevel@tonic-gate 		 * since the transport might adjust the value and not
53940Sstevel@tonic-gate 		 * return exactly what was set by the application.
53950Sstevel@tonic-gate 		 */
53960Sstevel@tonic-gate 		case SO_SNDBUF:
53970Sstevel@tonic-gate 			so->so_sndbuf = intvalue;
53980Sstevel@tonic-gate 			break;
53990Sstevel@tonic-gate 		case SO_RCVBUF:
54000Sstevel@tonic-gate 			so->so_rcvbuf = intvalue;
54010Sstevel@tonic-gate 			break;
54020Sstevel@tonic-gate #ifdef notyet
54030Sstevel@tonic-gate 		/*
54040Sstevel@tonic-gate 		 * We do not implement the semantics of these options
54050Sstevel@tonic-gate 		 * thus we shouldn't implement the options either.
54060Sstevel@tonic-gate 		 */
54070Sstevel@tonic-gate 		case SO_SNDLOWAT:
54080Sstevel@tonic-gate 			so->so_sndlowat = intvalue;
54090Sstevel@tonic-gate 			break;
54100Sstevel@tonic-gate 		case SO_RCVLOWAT:
54110Sstevel@tonic-gate 			so->so_rcvlowat = intvalue;
54120Sstevel@tonic-gate 			break;
54130Sstevel@tonic-gate 		case SO_SNDTIMEO:
54140Sstevel@tonic-gate 			so->so_sndtimeo = intvalue;
54150Sstevel@tonic-gate 			break;
54160Sstevel@tonic-gate 		case SO_RCVTIMEO:
54170Sstevel@tonic-gate 			so->so_rcvtimeo = intvalue;
54180Sstevel@tonic-gate 			break;
54190Sstevel@tonic-gate #endif /* notyet */
54200Sstevel@tonic-gate 		}
54210Sstevel@tonic-gate #undef	intvalue
54220Sstevel@tonic-gate 
54230Sstevel@tonic-gate 		if (error) {
54240Sstevel@tonic-gate 			if ((error == ENOPROTOOPT || error == EPROTO ||
54250Sstevel@tonic-gate 			    error == EINVAL) && handled) {
54260Sstevel@tonic-gate 				dprintso(so, 1,
54270Sstevel@tonic-gate 				    ("setsockopt: ignoring error %d for 0x%x\n",
54280Sstevel@tonic-gate 				    error, option_name));
54290Sstevel@tonic-gate 				error = 0;
54300Sstevel@tonic-gate 			}
54310Sstevel@tonic-gate 		}
54320Sstevel@tonic-gate 	}
54330Sstevel@tonic-gate done2:
54340Sstevel@tonic-gate ret:
54350Sstevel@tonic-gate 	so_unlock_single(so, SOLOCKED);
54360Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
54370Sstevel@tonic-gate 	return (error);
54380Sstevel@tonic-gate }
5439