17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52caf0dcdSrshoaib  * Common Development and Distribution License (the "License").
62caf0dcdSrshoaib  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212caf0dcdSrshoaib 
227c478bd9Sstevel@tonic-gate /*
2374e20cfeSnh145002  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/buf.h>
347c478bd9Sstevel@tonic-gate #include <sys/conf.h>
357c478bd9Sstevel@tonic-gate #include <sys/cred.h>
367c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
377c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
387c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
39*aa59c4cbSrsb #include <sys/vfs_opreg.h>
407c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
417c478bd9Sstevel@tonic-gate #include <sys/debug.h>
427c478bd9Sstevel@tonic-gate #include <sys/errno.h>
437c478bd9Sstevel@tonic-gate #include <sys/time.h>
447c478bd9Sstevel@tonic-gate #include <sys/file.h>
457c478bd9Sstevel@tonic-gate #include <sys/open.h>
467c478bd9Sstevel@tonic-gate #include <sys/user.h>
477c478bd9Sstevel@tonic-gate #include <sys/termios.h>
487c478bd9Sstevel@tonic-gate #include <sys/stream.h>
497c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
507c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
517c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
527c478bd9Sstevel@tonic-gate #include <sys/flock.h>
537c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
547c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
557c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
567c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
577c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
587c478bd9Sstevel@tonic-gate #include <sys/stat.h>
597c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
607c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
617c478bd9Sstevel@tonic-gate #include <sys/zone.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate #include <sys/socket.h>
647c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
657c478bd9Sstevel@tonic-gate #include <netinet/in.h>
667c478bd9Sstevel@tonic-gate #include <sys/un.h>
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #include <sys/ucred.h>
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate #include <sys/tiuser.h>
717c478bd9Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
727c478bd9Sstevel@tonic-gate #include <sys/tihdr.h>
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate #include <c2/audit.h>
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate /*
797c478bd9Sstevel@tonic-gate  * Macros that operate on struct cmsghdr.
807c478bd9Sstevel@tonic-gate  * The CMSG_VALID macro does not assume that the last option buffer is padded.
817c478bd9Sstevel@tonic-gate  */
827c478bd9Sstevel@tonic-gate #define	CMSG_CONTENT(cmsg)	(&((cmsg)[1]))
837c478bd9Sstevel@tonic-gate #define	CMSG_CONTENTLEN(cmsg)	((cmsg)->cmsg_len - sizeof (struct cmsghdr))
847c478bd9Sstevel@tonic-gate #define	CMSG_VALID(cmsg, start, end)					\
857c478bd9Sstevel@tonic-gate 	(ISALIGNED_cmsghdr(cmsg) &&					\
867c478bd9Sstevel@tonic-gate 	((uintptr_t)(cmsg) >= (uintptr_t)(start)) &&			\
877c478bd9Sstevel@tonic-gate 	((uintptr_t)(cmsg) < (uintptr_t)(end)) &&			\
887c478bd9Sstevel@tonic-gate 	((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) &&	\
897c478bd9Sstevel@tonic-gate 	((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
907c478bd9Sstevel@tonic-gate #define	SO_LOCK_WAKEUP_TIME	3000	/* Wakeup time in milliseconds */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate static struct kmem_cache *socktpi_cache, *socktpi_unix_cache;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate dev_t sockdev;	/* For fsid in getattr */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate struct sockparams *sphead;
977c478bd9Sstevel@tonic-gate krwlock_t splist_lock;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate struct socklist socklist;
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate static int sockfs_update(kstat_t *, int);
1027c478bd9Sstevel@tonic-gate static int sockfs_snapshot(kstat_t *, void *, int);
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate extern void sendfile_init();
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate extern void nl7c_init(void);
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate #define	ADRSTRLEN (2 * sizeof (void *) + 1)
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * kernel structure for passing the sockinfo data back up to the user.
1117c478bd9Sstevel@tonic-gate  * the strings array allows us to convert AF_UNIX addresses into strings
1127c478bd9Sstevel@tonic-gate  * with a common method regardless of which n-bit kernel we're running.
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate struct k_sockinfo {
1157c478bd9Sstevel@tonic-gate 	struct sockinfo	ks_si;
1167c478bd9Sstevel@tonic-gate 	char		ks_straddr[3][ADRSTRLEN];
1177c478bd9Sstevel@tonic-gate };
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * Translate from a device pathname (e.g. "/dev/tcp") to a vnode.
1217c478bd9Sstevel@tonic-gate  * Returns with the vnode held.
1227c478bd9Sstevel@tonic-gate  */
1237c478bd9Sstevel@tonic-gate static int
1247c478bd9Sstevel@tonic-gate sogetvp(char *devpath, vnode_t **vpp, int uioflag)
1257c478bd9Sstevel@tonic-gate {
1267c478bd9Sstevel@tonic-gate 	struct snode *csp;
1277c478bd9Sstevel@tonic-gate 	vnode_t *vp, *dvp;
1287c478bd9Sstevel@tonic-gate 	major_t maj;
1297c478bd9Sstevel@tonic-gate 	int error;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE);
1327c478bd9Sstevel@tonic-gate 	/*
1337c478bd9Sstevel@tonic-gate 	 * Lookup the underlying filesystem vnode.
1347c478bd9Sstevel@tonic-gate 	 */
1357c478bd9Sstevel@tonic-gate 	error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp);
1367c478bd9Sstevel@tonic-gate 	if (error)
1377c478bd9Sstevel@tonic-gate 		return (error);
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* Check that it is the correct vnode */
1407c478bd9Sstevel@tonic-gate 	if (vp->v_type != VCHR) {
1417c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1427c478bd9Sstevel@tonic-gate 		return (ENOTSOCK);
1437c478bd9Sstevel@tonic-gate 	}
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	/*
1467c478bd9Sstevel@tonic-gate 	 * If devpath went through devfs, the device should already
1477c478bd9Sstevel@tonic-gate 	 * be configured. If devpath is a mknod file, however, we
1487c478bd9Sstevel@tonic-gate 	 * need to make sure the device is properly configured.
1497c478bd9Sstevel@tonic-gate 	 * To do this, we do something similar to spec_open()
1507c478bd9Sstevel@tonic-gate 	 * except that we resolve to the minor/leaf level since
1517c478bd9Sstevel@tonic-gate 	 * we need to return a vnode.
1527c478bd9Sstevel@tonic-gate 	 */
1537c478bd9Sstevel@tonic-gate 	csp = VTOS(VTOS(vp)->s_commonvp);
1547c478bd9Sstevel@tonic-gate 	if (!(csp->s_flag & SDIPSET)) {
1557c478bd9Sstevel@tonic-gate 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1567c478bd9Sstevel@tonic-gate 		error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname);
1577c478bd9Sstevel@tonic-gate 		if (error == 0)
1587c478bd9Sstevel@tonic-gate 			error = devfs_lookupname(pathname, NULLVPP, &dvp);
1597c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1607c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
1617c478bd9Sstevel@tonic-gate 		if (error != 0)
1627c478bd9Sstevel@tonic-gate 			return (ENXIO);
1637c478bd9Sstevel@tonic-gate 		vp = dvp;	/* use the devfs vp */
1647c478bd9Sstevel@tonic-gate 	}
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	/* device is configured at this point */
1677c478bd9Sstevel@tonic-gate 	maj = getmajor(vp->v_rdev);
1687c478bd9Sstevel@tonic-gate 	if (!STREAMSTAB(maj)) {
1697c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1707c478bd9Sstevel@tonic-gate 		return (ENOSTR);
1717c478bd9Sstevel@tonic-gate 	}
1727c478bd9Sstevel@tonic-gate 
1737c478bd9Sstevel@tonic-gate 	*vpp = vp;
1747c478bd9Sstevel@tonic-gate 	return (0);
1757c478bd9Sstevel@tonic-gate }
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate /*
1787c478bd9Sstevel@tonic-gate  * Add or delete (latter if devpath is NULL) an enter to the sockparams
1797c478bd9Sstevel@tonic-gate  * table. If devpathlen is zero the devpath with not be kmem_freed. Otherwise
1807c478bd9Sstevel@tonic-gate  * this routine assumes that the caller has kmem_alloced devpath/devpathlen
1817c478bd9Sstevel@tonic-gate  * for this routine to consume.
1827c478bd9Sstevel@tonic-gate  * The zero devpathlen could be used if the kernel wants to create entries
1837c478bd9Sstevel@tonic-gate  * itself by calling sockconfig(1,2,3, "/dev/tcp", 0);
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate int
1867c478bd9Sstevel@tonic-gate soconfig(int domain, int type, int protocol,
1877c478bd9Sstevel@tonic-gate     char *devpath, int devpathlen)
1887c478bd9Sstevel@tonic-gate {
1897c478bd9Sstevel@tonic-gate 	struct sockparams **spp;
1907c478bd9Sstevel@tonic-gate 	struct sockparams *sp;
1917c478bd9Sstevel@tonic-gate 	int error = 0;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	dprint(0, ("soconfig(%d,%d,%d,%s,%d)\n",
1947c478bd9Sstevel@tonic-gate 		domain, type, protocol, devpath, devpathlen));
1957c478bd9Sstevel@tonic-gate 
1967c478bd9Sstevel@tonic-gate 	/*
1977c478bd9Sstevel@tonic-gate 	 * Look for an existing match.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	rw_enter(&splist_lock, RW_WRITER);
2007c478bd9Sstevel@tonic-gate 	for (spp = &sphead; (sp = *spp) != NULL; spp = &sp->sp_next) {
2017c478bd9Sstevel@tonic-gate 		if (sp->sp_domain == domain &&
2027c478bd9Sstevel@tonic-gate 		    sp->sp_type == type &&
2037c478bd9Sstevel@tonic-gate 		    sp->sp_protocol == protocol) {
2047c478bd9Sstevel@tonic-gate 			break;
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	if (devpath == NULL) {
2087c478bd9Sstevel@tonic-gate 		ASSERT(devpathlen == 0);
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 		/* Delete existing entry */
2117c478bd9Sstevel@tonic-gate 		if (sp == NULL) {
2127c478bd9Sstevel@tonic-gate 			error = ENXIO;
2137c478bd9Sstevel@tonic-gate 			goto done;
2147c478bd9Sstevel@tonic-gate 		}
2157c478bd9Sstevel@tonic-gate 		/* Unlink and free existing entry */
2167c478bd9Sstevel@tonic-gate 		*spp = sp->sp_next;
2177c478bd9Sstevel@tonic-gate 		ASSERT(sp->sp_vnode);
2187c478bd9Sstevel@tonic-gate 		VN_RELE(sp->sp_vnode);
2197c478bd9Sstevel@tonic-gate 		if (sp->sp_devpathlen != 0)
2207c478bd9Sstevel@tonic-gate 			kmem_free(sp->sp_devpath, sp->sp_devpathlen);
2217c478bd9Sstevel@tonic-gate 		kmem_free(sp, sizeof (*sp));
2227c478bd9Sstevel@tonic-gate 	} else {
2237c478bd9Sstevel@tonic-gate 		vnode_t *vp;
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 		/* Add new entry */
2267c478bd9Sstevel@tonic-gate 		if (sp != NULL) {
2277c478bd9Sstevel@tonic-gate 			error = EEXIST;
2287c478bd9Sstevel@tonic-gate 			goto done;
2297c478bd9Sstevel@tonic-gate 		}
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 		error = sogetvp(devpath, &vp, UIO_SYSSPACE);
2327c478bd9Sstevel@tonic-gate 		if (error) {
2337c478bd9Sstevel@tonic-gate 			dprint(0, ("soconfig: vp %s failed with %d\n",
2347c478bd9Sstevel@tonic-gate 				devpath, error));
2357c478bd9Sstevel@tonic-gate 			goto done;
2367c478bd9Sstevel@tonic-gate 		}
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		dprint(0, ("soconfig: %s => vp %p, dev 0x%lx\n",
2397c478bd9Sstevel@tonic-gate 		    devpath, vp, vp->v_rdev));
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 		sp = kmem_alloc(sizeof (*sp), KM_SLEEP);
2427c478bd9Sstevel@tonic-gate 		sp->sp_domain = domain;
2437c478bd9Sstevel@tonic-gate 		sp->sp_type = type;
2447c478bd9Sstevel@tonic-gate 		sp->sp_protocol = protocol;
2457c478bd9Sstevel@tonic-gate 		sp->sp_devpath = devpath;
2467c478bd9Sstevel@tonic-gate 		sp->sp_devpathlen = devpathlen;
2477c478bd9Sstevel@tonic-gate 		sp->sp_vnode = vp;
2487c478bd9Sstevel@tonic-gate 		sp->sp_next = NULL;
2497c478bd9Sstevel@tonic-gate 		*spp = sp;
2507c478bd9Sstevel@tonic-gate 	}
2517c478bd9Sstevel@tonic-gate done:
2527c478bd9Sstevel@tonic-gate 	rw_exit(&splist_lock);
2537c478bd9Sstevel@tonic-gate 	if (error) {
2547c478bd9Sstevel@tonic-gate 		if (devpath != NULL)
2557c478bd9Sstevel@tonic-gate 			kmem_free(devpath, devpathlen);
2567c478bd9Sstevel@tonic-gate #ifdef SOCK_DEBUG
2577c478bd9Sstevel@tonic-gate 		eprintline(error);
2587c478bd9Sstevel@tonic-gate #endif /* SOCK_DEBUG */
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	return (error);
2617c478bd9Sstevel@tonic-gate }
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate /*
2647c478bd9Sstevel@tonic-gate  * Lookup an entry in the sockparams list based on the triple.
2657c478bd9Sstevel@tonic-gate  * If no entry is found and devpath is not NULL translate devpath to a
2667c478bd9Sstevel@tonic-gate  * vnode. Note that devpath is a pointer to a user address!
2677c478bd9Sstevel@tonic-gate  * Returns with the vnode held.
2687c478bd9Sstevel@tonic-gate  *
2697c478bd9Sstevel@tonic-gate  * When this routine uses devpath it does not create an entry in the sockparams
2707c478bd9Sstevel@tonic-gate  * list since this routine can run on behalf of any user and one user
2717c478bd9Sstevel@tonic-gate  * should not be able to effect the transport used by another user.
2727c478bd9Sstevel@tonic-gate  *
2737c478bd9Sstevel@tonic-gate  * In order to return the correct error this routine has to do wildcard scans
2747c478bd9Sstevel@tonic-gate  * of the list. The errors are (in decreasing precedence):
2757c478bd9Sstevel@tonic-gate  *	EAFNOSUPPORT - address family not in list
2767c478bd9Sstevel@tonic-gate  *	EPROTONOSUPPORT - address family supported but not protocol.
2777c478bd9Sstevel@tonic-gate  *	EPROTOTYPE - address family and protocol supported but not socket type.
2787c478bd9Sstevel@tonic-gate  */
2797c478bd9Sstevel@tonic-gate vnode_t *
2807c478bd9Sstevel@tonic-gate solookup(int domain, int type, int protocol, char *devpath, int *errorp)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	struct sockparams *sp;
2837c478bd9Sstevel@tonic-gate 	int error;
2847c478bd9Sstevel@tonic-gate 	vnode_t *vp;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	rw_enter(&splist_lock, RW_READER);
2877c478bd9Sstevel@tonic-gate 	for (sp = sphead; sp != NULL; sp = sp->sp_next) {
2887c478bd9Sstevel@tonic-gate 		if (sp->sp_domain == domain &&
2897c478bd9Sstevel@tonic-gate 		    sp->sp_type == type &&
2907c478bd9Sstevel@tonic-gate 		    sp->sp_protocol == protocol) {
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 		}
2937c478bd9Sstevel@tonic-gate 	}
2947c478bd9Sstevel@tonic-gate 	if (sp == NULL) {
2957c478bd9Sstevel@tonic-gate 		dprint(0, ("solookup(%d,%d,%d) not found\n",
2967c478bd9Sstevel@tonic-gate 			domain, type, protocol));
2977c478bd9Sstevel@tonic-gate 		if (devpath == NULL) {
2987c478bd9Sstevel@tonic-gate 			/* Determine correct error code */
2997c478bd9Sstevel@tonic-gate 			int found = 0;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 			for (sp = sphead; sp != NULL; sp = sp->sp_next) {
3027c478bd9Sstevel@tonic-gate 				if (sp->sp_domain == domain && found < 1)
3037c478bd9Sstevel@tonic-gate 					found = 1;
3047c478bd9Sstevel@tonic-gate 				if (sp->sp_domain == domain &&
3057c478bd9Sstevel@tonic-gate 				    sp->sp_protocol == protocol && found < 2)
3067c478bd9Sstevel@tonic-gate 					found = 2;
3077c478bd9Sstevel@tonic-gate 			}
3087c478bd9Sstevel@tonic-gate 			rw_exit(&splist_lock);
3097c478bd9Sstevel@tonic-gate 			switch (found) {
3107c478bd9Sstevel@tonic-gate 			case 0:
3117c478bd9Sstevel@tonic-gate 				*errorp = EAFNOSUPPORT;
3127c478bd9Sstevel@tonic-gate 				break;
3137c478bd9Sstevel@tonic-gate 			case 1:
3147c478bd9Sstevel@tonic-gate 				*errorp = EPROTONOSUPPORT;
3157c478bd9Sstevel@tonic-gate 				break;
3167c478bd9Sstevel@tonic-gate 			case 2:
3177c478bd9Sstevel@tonic-gate 				*errorp = EPROTOTYPE;
3187c478bd9Sstevel@tonic-gate 				break;
3197c478bd9Sstevel@tonic-gate 			}
3207c478bd9Sstevel@tonic-gate 			return (NULL);
3217c478bd9Sstevel@tonic-gate 		}
3227c478bd9Sstevel@tonic-gate 		rw_exit(&splist_lock);
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 		/*
3257c478bd9Sstevel@tonic-gate 		 * Return vp based on devpath.
3267c478bd9Sstevel@tonic-gate 		 * Do not enter into table to avoid random users
3277c478bd9Sstevel@tonic-gate 		 * modifying the sockparams list.
3287c478bd9Sstevel@tonic-gate 		 */
3297c478bd9Sstevel@tonic-gate 		error = sogetvp(devpath, &vp, UIO_USERSPACE);
3307c478bd9Sstevel@tonic-gate 		if (error) {
3317c478bd9Sstevel@tonic-gate 			dprint(0, ("solookup: vp %p failed with %d\n",
3327c478bd9Sstevel@tonic-gate 				devpath, error));
3337c478bd9Sstevel@tonic-gate 			*errorp = EPROTONOSUPPORT;
3347c478bd9Sstevel@tonic-gate 			return (NULL);
3357c478bd9Sstevel@tonic-gate 		}
3367c478bd9Sstevel@tonic-gate 		dprint(0, ("solookup: %p => vp %p, dev 0x%lx\n",
3377c478bd9Sstevel@tonic-gate 		    devpath, vp, vp->v_rdev));
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 		return (vp);
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate 	dprint(0, ("solookup(%d,%d,%d) vp %p devpath %s\n",
3427c478bd9Sstevel@tonic-gate 		domain, type, protocol, sp->sp_vnode, sp->sp_devpath));
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate 	vp = sp->sp_vnode;
3457c478bd9Sstevel@tonic-gate 	VN_HOLD(vp);
3467c478bd9Sstevel@tonic-gate 	rw_exit(&splist_lock);
3477c478bd9Sstevel@tonic-gate 	return (vp);
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate /*
3517c478bd9Sstevel@tonic-gate  * Return a socket vnode.
3527c478bd9Sstevel@tonic-gate  *
3537c478bd9Sstevel@tonic-gate  * Assumes that the caller is "passing" an VN_HOLD for accessvp i.e.
3547c478bd9Sstevel@tonic-gate  * when the socket is freed a VN_RELE will take place.
3557c478bd9Sstevel@tonic-gate  *
3567c478bd9Sstevel@tonic-gate  * Note that sockets assume that the driver will clone (either itself
3577c478bd9Sstevel@tonic-gate  * or by using the clone driver) i.e. a socket() call will always
3587c478bd9Sstevel@tonic-gate  * result in a new vnode being created.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate struct vnode *
3617c478bd9Sstevel@tonic-gate makesockvp(struct vnode *accessvp, int domain, int type, int protocol)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	kmem_cache_t *cp;
3647c478bd9Sstevel@tonic-gate 	struct sonode *so;
3657c478bd9Sstevel@tonic-gate 	struct vnode *vp;
3667c478bd9Sstevel@tonic-gate 	time_t now;
3677c478bd9Sstevel@tonic-gate 	dev_t dev;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	cp = (domain == AF_UNIX) ? socktpi_unix_cache : socktpi_cache;
3707c478bd9Sstevel@tonic-gate 	so = kmem_cache_alloc(cp, KM_SLEEP);
3717c478bd9Sstevel@tonic-gate 	so->so_cache = cp;
3727c478bd9Sstevel@tonic-gate 	so->so_obj = so;
3737c478bd9Sstevel@tonic-gate 	vp = SOTOV(so);
3747c478bd9Sstevel@tonic-gate 	now = gethrestime_sec();
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	so->so_flag	= 0;
3777c478bd9Sstevel@tonic-gate 	ASSERT(so->so_accessvp == NULL);
3787c478bd9Sstevel@tonic-gate 	so->so_accessvp	= accessvp;
3797c478bd9Sstevel@tonic-gate 	dev = accessvp->v_rdev;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 	/*
3827c478bd9Sstevel@tonic-gate 	 * Record in so_flag that it is a clone.
3837c478bd9Sstevel@tonic-gate 	 */
3847c478bd9Sstevel@tonic-gate 	if (getmajor(dev) == clone_major) {
3857c478bd9Sstevel@tonic-gate 		so->so_flag |= SOCLONE;
3867c478bd9Sstevel@tonic-gate 	}
3877c478bd9Sstevel@tonic-gate 	so->so_dev = dev;
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate 	so->so_state	= 0;
3907c478bd9Sstevel@tonic-gate 	so->so_mode	= 0;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	so->so_fsid	= sockdev;
3937c478bd9Sstevel@tonic-gate 	so->so_atime	= now;
3947c478bd9Sstevel@tonic-gate 	so->so_mtime	= now;
3957c478bd9Sstevel@tonic-gate 	so->so_ctime	= now;		/* Never modified */
3967c478bd9Sstevel@tonic-gate 	so->so_count	= 0;
3977c478bd9Sstevel@tonic-gate 
3987c478bd9Sstevel@tonic-gate 	so->so_family	= (short)domain;
3997c478bd9Sstevel@tonic-gate 	so->so_type	= (short)type;
4007c478bd9Sstevel@tonic-gate 	so->so_protocol	= (short)protocol;
4017c478bd9Sstevel@tonic-gate 	so->so_pushcnt	= 0;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	so->so_options	= 0;
4047c478bd9Sstevel@tonic-gate 	so->so_linger.l_onoff	= 0;
4057c478bd9Sstevel@tonic-gate 	so->so_linger.l_linger = 0;
4067c478bd9Sstevel@tonic-gate 	so->so_sndbuf	= 0;
4077c478bd9Sstevel@tonic-gate 	so->so_rcvbuf	= 0;
4087c478bd9Sstevel@tonic-gate 	so->so_sndlowat	= 0;
4097c478bd9Sstevel@tonic-gate 	so->so_rcvlowat	= 0;
4107c478bd9Sstevel@tonic-gate #ifdef notyet
4117c478bd9Sstevel@tonic-gate 	so->so_sndtimeo	= 0;
4127c478bd9Sstevel@tonic-gate 	so->so_rcvtimeo	= 0;
4137c478bd9Sstevel@tonic-gate #endif /* notyet */
4147c478bd9Sstevel@tonic-gate 	so->so_error	= 0;
4157c478bd9Sstevel@tonic-gate 	so->so_delayed_error = 0;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	ASSERT(so->so_oobmsg == NULL);
4187c478bd9Sstevel@tonic-gate 	so->so_oobcnt	= 0;
4197c478bd9Sstevel@tonic-gate 	so->so_oobsigcnt = 0;
4207c478bd9Sstevel@tonic-gate 	so->so_pgrp	= 0;
4217c478bd9Sstevel@tonic-gate 	so->so_provinfo = NULL;
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	ASSERT(so->so_laddr_sa == NULL && so->so_faddr_sa == NULL);
4247c478bd9Sstevel@tonic-gate 	so->so_laddr_len = so->so_faddr_len = 0;
4257c478bd9Sstevel@tonic-gate 	so->so_laddr_maxlen = so->so_faddr_maxlen = 0;
4267c478bd9Sstevel@tonic-gate 	so->so_eaddr_mp = NULL;
4277c478bd9Sstevel@tonic-gate 	so->so_priv = NULL;
4287c478bd9Sstevel@tonic-gate 
4297c478bd9Sstevel@tonic-gate 	so->so_peercred = NULL;
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	ASSERT(so->so_ack_mp == NULL);
4327c478bd9Sstevel@tonic-gate 	ASSERT(so->so_conn_ind_head == NULL);
4337c478bd9Sstevel@tonic-gate 	ASSERT(so->so_conn_ind_tail == NULL);
4347c478bd9Sstevel@tonic-gate 	ASSERT(so->so_ux_bound_vp == NULL);
4357c478bd9Sstevel@tonic-gate 	ASSERT(so->so_unbind_mp == NULL);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	vn_reinit(vp);
4387c478bd9Sstevel@tonic-gate 	vp->v_vfsp	= rootvfs;
4397c478bd9Sstevel@tonic-gate 	vp->v_type	= VSOCK;
4407c478bd9Sstevel@tonic-gate 	vp->v_rdev	= so->so_dev;
4417c478bd9Sstevel@tonic-gate 	vn_exists(vp);
4427c478bd9Sstevel@tonic-gate 
4437c478bd9Sstevel@tonic-gate 	return (vp);
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate void
4477c478bd9Sstevel@tonic-gate sockfree(struct sonode *so)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	mblk_t *mp;
4507c478bd9Sstevel@tonic-gate 	vnode_t *vp;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	ASSERT(so->so_count == 0);
4537c478bd9Sstevel@tonic-gate 	ASSERT(so->so_accessvp);
4547c478bd9Sstevel@tonic-gate 	ASSERT(so->so_discon_ind_mp == NULL);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	vp = so->so_accessvp;
4577c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/*
4607c478bd9Sstevel@tonic-gate 	 * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely
4617c478bd9Sstevel@tonic-gate 	 * indirect them.  It also uses so_accessvp as a validity test.
4627c478bd9Sstevel@tonic-gate 	 */
4637c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	so->so_accessvp = NULL;
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	if (so->so_laddr_sa) {
4687c478bd9Sstevel@tonic-gate 		ASSERT((caddr_t)so->so_faddr_sa ==
4697c478bd9Sstevel@tonic-gate 		    (caddr_t)so->so_laddr_sa + so->so_laddr_maxlen);
4707c478bd9Sstevel@tonic-gate 		ASSERT(so->so_faddr_maxlen == so->so_laddr_maxlen);
4717c478bd9Sstevel@tonic-gate 		so->so_state &= ~(SS_LADDR_VALID | SS_FADDR_VALID);
4727c478bd9Sstevel@tonic-gate 		kmem_free(so->so_laddr_sa, so->so_laddr_maxlen * 2);
4737c478bd9Sstevel@tonic-gate 		so->so_laddr_sa = NULL;
4747c478bd9Sstevel@tonic-gate 		so->so_laddr_len = so->so_laddr_maxlen = 0;
4757c478bd9Sstevel@tonic-gate 		so->so_faddr_sa = NULL;
4767c478bd9Sstevel@tonic-gate 		so->so_faddr_len = so->so_faddr_maxlen = 0;
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	if ((mp = so->so_eaddr_mp) != NULL) {
4827c478bd9Sstevel@tonic-gate 		freemsg(mp);
4837c478bd9Sstevel@tonic-gate 		so->so_eaddr_mp = NULL;
4847c478bd9Sstevel@tonic-gate 		so->so_delayed_error = 0;
4857c478bd9Sstevel@tonic-gate 	}
4867c478bd9Sstevel@tonic-gate 	if ((mp = so->so_ack_mp) != NULL) {
4877c478bd9Sstevel@tonic-gate 		freemsg(mp);
4887c478bd9Sstevel@tonic-gate 		so->so_ack_mp = NULL;
4897c478bd9Sstevel@tonic-gate 	}
4907c478bd9Sstevel@tonic-gate 	if ((mp = so->so_conn_ind_head) != NULL) {
4917c478bd9Sstevel@tonic-gate 		mblk_t *mp1;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 		while (mp) {
4947c478bd9Sstevel@tonic-gate 			mp1 = mp->b_next;
4957c478bd9Sstevel@tonic-gate 			mp->b_next = NULL;
4967c478bd9Sstevel@tonic-gate 			freemsg(mp);
4977c478bd9Sstevel@tonic-gate 			mp = mp1;
4987c478bd9Sstevel@tonic-gate 		}
4997c478bd9Sstevel@tonic-gate 		so->so_conn_ind_head = so->so_conn_ind_tail = NULL;
5007c478bd9Sstevel@tonic-gate 		so->so_state &= ~SS_HASCONNIND;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate #ifdef DEBUG
5037c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
5047c478bd9Sstevel@tonic-gate 	ASSERT(so_verify_oobstate(so));
5057c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
5067c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5077c478bd9Sstevel@tonic-gate 	if ((mp = so->so_oobmsg) != NULL) {
5087c478bd9Sstevel@tonic-gate 		freemsg(mp);
5097c478bd9Sstevel@tonic-gate 		so->so_oobmsg = NULL;
5107c478bd9Sstevel@tonic-gate 		so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA);
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	if ((mp = so->so_nl7c_rcv_mp) != NULL) {
5147c478bd9Sstevel@tonic-gate 		so->so_nl7c_rcv_mp = NULL;
5157c478bd9Sstevel@tonic-gate 		freemsg(mp);
5167c478bd9Sstevel@tonic-gate 	}
5177c478bd9Sstevel@tonic-gate 	so->so_nl7c_rcv_rval = 0;
5187c478bd9Sstevel@tonic-gate 	if (so->so_nl7c_uri != NULL) {
5197c478bd9Sstevel@tonic-gate 		nl7c_urifree(so);
5202c9e429eSbrutus 		/* urifree() cleared nl7c_uri */
5217c478bd9Sstevel@tonic-gate 	}
5222c9e429eSbrutus 	if (so->so_nl7c_flags) {
5237c478bd9Sstevel@tonic-gate 		so->so_nl7c_flags = 0;
5242c9e429eSbrutus 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	ASSERT(so->so_ux_bound_vp == NULL);
5277c478bd9Sstevel@tonic-gate 	if ((mp = so->so_unbind_mp) != NULL) {
5287c478bd9Sstevel@tonic-gate 		freemsg(mp);
5297c478bd9Sstevel@tonic-gate 		so->so_unbind_mp = NULL;
5307c478bd9Sstevel@tonic-gate 	}
5317c478bd9Sstevel@tonic-gate 	vn_invalid(SOTOV(so));
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 	if (so->so_peercred != NULL)
5347c478bd9Sstevel@tonic-gate 		crfree(so->so_peercred);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	kmem_cache_free(so->so_cache, so->so_obj);
5377c478bd9Sstevel@tonic-gate }
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate /*
5407c478bd9Sstevel@tonic-gate  * Update the accessed, updated, or changed times in an sonode
5417c478bd9Sstevel@tonic-gate  * with the current time.
5427c478bd9Sstevel@tonic-gate  *
5437c478bd9Sstevel@tonic-gate  * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable
5447c478bd9Sstevel@tonic-gate  * attributes in a fstat call. (They return the current time and 0 for
5457c478bd9Sstevel@tonic-gate  * all timestamps, respectively.) We maintain the current timestamps
5467c478bd9Sstevel@tonic-gate  * here primarily so that should sockmod be popped the resulting
5477c478bd9Sstevel@tonic-gate  * file descriptor will behave like a stream w.r.t. the timestamps.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate void
5507c478bd9Sstevel@tonic-gate so_update_attrs(struct sonode *so, int flag)
5517c478bd9Sstevel@tonic-gate {
5527c478bd9Sstevel@tonic-gate 	time_t now = gethrestime_sec();
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
5557c478bd9Sstevel@tonic-gate 	so->so_flag |= flag;
5567c478bd9Sstevel@tonic-gate 	if (flag & SOACC)
5577c478bd9Sstevel@tonic-gate 		so->so_atime = now;
5587c478bd9Sstevel@tonic-gate 	if (flag & SOMOD)
5597c478bd9Sstevel@tonic-gate 		so->so_mtime = now;
5607c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
5617c478bd9Sstevel@tonic-gate }
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate /*ARGSUSED*/
5647c478bd9Sstevel@tonic-gate static int
5657c478bd9Sstevel@tonic-gate socktpi_constructor(void *buf, void *cdrarg, int kmflags)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	struct sonode *so = buf;
5687c478bd9Sstevel@tonic-gate 	struct vnode *vp;
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	so->so_nl7c_flags	= 0;
5717c478bd9Sstevel@tonic-gate 	so->so_nl7c_uri		= NULL;
5727c478bd9Sstevel@tonic-gate 	so->so_nl7c_rcv_mp	= NULL;
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 	so->so_oobmsg		= NULL;
5757c478bd9Sstevel@tonic-gate 	so->so_ack_mp		= NULL;
5767c478bd9Sstevel@tonic-gate 	so->so_conn_ind_head	= NULL;
5777c478bd9Sstevel@tonic-gate 	so->so_conn_ind_tail	= NULL;
5787c478bd9Sstevel@tonic-gate 	so->so_discon_ind_mp	= NULL;
5797c478bd9Sstevel@tonic-gate 	so->so_ux_bound_vp	= NULL;
5807c478bd9Sstevel@tonic-gate 	so->so_unbind_mp	= NULL;
5817c478bd9Sstevel@tonic-gate 	so->so_accessvp		= NULL;
5827c478bd9Sstevel@tonic-gate 	so->so_laddr_sa		= NULL;
5837c478bd9Sstevel@tonic-gate 	so->so_faddr_sa		= NULL;
5847c478bd9Sstevel@tonic-gate 	so->so_ops		= &sotpi_sonodeops;
5857c478bd9Sstevel@tonic-gate 
5867c478bd9Sstevel@tonic-gate 	vp = vn_alloc(KM_SLEEP);
5877c478bd9Sstevel@tonic-gate 	so->so_vnode = vp;
5887c478bd9Sstevel@tonic-gate 
5897c478bd9Sstevel@tonic-gate 	vn_setops(vp, socktpi_vnodeops);
5907c478bd9Sstevel@tonic-gate 	vp->v_data = (caddr_t)so;
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate 	mutex_init(&so->so_lock, NULL, MUTEX_DEFAULT, NULL);
5937c478bd9Sstevel@tonic-gate 	mutex_init(&so->so_plumb_lock, NULL, MUTEX_DEFAULT, NULL);
5947c478bd9Sstevel@tonic-gate 	cv_init(&so->so_state_cv, NULL, CV_DEFAULT, NULL);
5957c478bd9Sstevel@tonic-gate 	cv_init(&so->so_ack_cv, NULL, CV_DEFAULT, NULL);
5967c478bd9Sstevel@tonic-gate 	cv_init(&so->so_connind_cv, NULL, CV_DEFAULT, NULL);
5977c478bd9Sstevel@tonic-gate 	cv_init(&so->so_want_cv, NULL, CV_DEFAULT, NULL);
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate 	return (0);
6007c478bd9Sstevel@tonic-gate }
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate /*ARGSUSED1*/
6037c478bd9Sstevel@tonic-gate static void
6047c478bd9Sstevel@tonic-gate socktpi_destructor(void *buf, void *cdrarg)
6057c478bd9Sstevel@tonic-gate {
6067c478bd9Sstevel@tonic-gate 	struct sonode *so = buf;
6077c478bd9Sstevel@tonic-gate 	struct vnode *vp = SOTOV(so);
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	ASSERT(so->so_nl7c_flags == 0);
6107c478bd9Sstevel@tonic-gate 	ASSERT(so->so_nl7c_uri == NULL);
6117c478bd9Sstevel@tonic-gate 	ASSERT(so->so_nl7c_rcv_mp == NULL);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	ASSERT(so->so_oobmsg == NULL);
6147c478bd9Sstevel@tonic-gate 	ASSERT(so->so_ack_mp == NULL);
6157c478bd9Sstevel@tonic-gate 	ASSERT(so->so_conn_ind_head == NULL);
6167c478bd9Sstevel@tonic-gate 	ASSERT(so->so_conn_ind_tail == NULL);
6177c478bd9Sstevel@tonic-gate 	ASSERT(so->so_discon_ind_mp == NULL);
6187c478bd9Sstevel@tonic-gate 	ASSERT(so->so_ux_bound_vp == NULL);
6197c478bd9Sstevel@tonic-gate 	ASSERT(so->so_unbind_mp == NULL);
6207c478bd9Sstevel@tonic-gate 	ASSERT(so->so_ops == &sotpi_sonodeops);
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	ASSERT(vn_matchops(vp, socktpi_vnodeops));
6237c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_data == (caddr_t)so);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	vn_free(vp);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 	mutex_destroy(&so->so_lock);
6287c478bd9Sstevel@tonic-gate 	mutex_destroy(&so->so_plumb_lock);
6297c478bd9Sstevel@tonic-gate 	cv_destroy(&so->so_state_cv);
6307c478bd9Sstevel@tonic-gate 	cv_destroy(&so->so_ack_cv);
6317c478bd9Sstevel@tonic-gate 	cv_destroy(&so->so_connind_cv);
6327c478bd9Sstevel@tonic-gate 	cv_destroy(&so->so_want_cv);
6337c478bd9Sstevel@tonic-gate }
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate static int
6367c478bd9Sstevel@tonic-gate socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags)
6377c478bd9Sstevel@tonic-gate {
6387c478bd9Sstevel@tonic-gate 	int retval;
6397c478bd9Sstevel@tonic-gate 
6407c478bd9Sstevel@tonic-gate 	if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) {
6417c478bd9Sstevel@tonic-gate 		struct sonode *so = (struct sonode *)buf;
6427c478bd9Sstevel@tonic-gate 
6437c478bd9Sstevel@tonic-gate 		mutex_enter(&socklist.sl_lock);
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		so->so_next = socklist.sl_list;
6467c478bd9Sstevel@tonic-gate 		so->so_prev = NULL;
6477c478bd9Sstevel@tonic-gate 		if (so->so_next != NULL)
6487c478bd9Sstevel@tonic-gate 			so->so_next->so_prev = so;
6497c478bd9Sstevel@tonic-gate 		socklist.sl_list = so;
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 		mutex_exit(&socklist.sl_lock);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate 	}
6547c478bd9Sstevel@tonic-gate 	return (retval);
6557c478bd9Sstevel@tonic-gate }
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate static void
6587c478bd9Sstevel@tonic-gate socktpi_unix_destructor(void *buf, void *cdrarg)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	struct sonode	*so	= (struct sonode *)buf;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	mutex_enter(&socklist.sl_lock);
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	if (so->so_next != NULL)
6657c478bd9Sstevel@tonic-gate 		so->so_next->so_prev = so->so_prev;
6667c478bd9Sstevel@tonic-gate 	if (so->so_prev != NULL)
6677c478bd9Sstevel@tonic-gate 		so->so_prev->so_next = so->so_next;
6687c478bd9Sstevel@tonic-gate 	else
6697c478bd9Sstevel@tonic-gate 		socklist.sl_list = so->so_next;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	mutex_exit(&socklist.sl_lock);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	socktpi_destructor(buf, cdrarg);
6747c478bd9Sstevel@tonic-gate }
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate /*
6777c478bd9Sstevel@tonic-gate  * Init function called when sockfs is loaded.
6787c478bd9Sstevel@tonic-gate  */
6797c478bd9Sstevel@tonic-gate int
6807c478bd9Sstevel@tonic-gate sockinit(int fstype, char *name)
6817c478bd9Sstevel@tonic-gate {
6827c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t sock_vfsops_template[] = {
6837c478bd9Sstevel@tonic-gate 		NULL, NULL
6847c478bd9Sstevel@tonic-gate 	};
6857c478bd9Sstevel@tonic-gate 	int error;
6867c478bd9Sstevel@tonic-gate 	major_t dev;
6877c478bd9Sstevel@tonic-gate 	char *err_str;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, sock_vfsops_template, NULL);
6907c478bd9Sstevel@tonic-gate 	if (error != 0) {
6912caf0dcdSrshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
6922caf0dcdSrshoaib 		    "sockinit: bad vfs ops template");
6937c478bd9Sstevel@tonic-gate 		return (error);
6947c478bd9Sstevel@tonic-gate 	}
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	error = vn_make_ops(name, socktpi_vnodeops_template, &socktpi_vnodeops);
6977c478bd9Sstevel@tonic-gate 	if (error != 0) {
6987c478bd9Sstevel@tonic-gate 		err_str = "sockinit: bad sock vnode ops template";
6997c478bd9Sstevel@tonic-gate 		/* vn_make_ops() does not reset socktpi_vnodeops on failure. */
7007c478bd9Sstevel@tonic-gate 		socktpi_vnodeops = NULL;
7017c478bd9Sstevel@tonic-gate 		goto failure;
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	error = sosctp_init();
7057c478bd9Sstevel@tonic-gate 	if (error != 0) {
7067c478bd9Sstevel@tonic-gate 		err_str = NULL;
7077c478bd9Sstevel@tonic-gate 		goto failure;
7087c478bd9Sstevel@tonic-gate 	}
7097c478bd9Sstevel@tonic-gate 
71074e20cfeSnh145002 	error = sosdp_init();
71174e20cfeSnh145002 	if (error != 0) {
71274e20cfeSnh145002 		err_str = NULL;
71374e20cfeSnh145002 		goto failure;
71474e20cfeSnh145002 	}
71574e20cfeSnh145002 
7167c478bd9Sstevel@tonic-gate 	/*
7177c478bd9Sstevel@tonic-gate 	 * Create sonode caches.  We create a special one for AF_UNIX so
7187c478bd9Sstevel@tonic-gate 	 * that we can track them for netstat(1m).
7197c478bd9Sstevel@tonic-gate 	 */
7207c478bd9Sstevel@tonic-gate 	socktpi_cache = kmem_cache_create("socktpi_cache",
7217c478bd9Sstevel@tonic-gate 	    sizeof (struct sonode), 0, socktpi_constructor,
7227c478bd9Sstevel@tonic-gate 	    socktpi_destructor, NULL, NULL, NULL, 0);
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache",
7257c478bd9Sstevel@tonic-gate 	    sizeof (struct sonode), 0, socktpi_unix_constructor,
7267c478bd9Sstevel@tonic-gate 	    socktpi_unix_destructor, NULL, NULL, NULL, 0);
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	/*
7297c478bd9Sstevel@tonic-gate 	 * Build initial list mapping socket parameters to vnode.
7307c478bd9Sstevel@tonic-gate 	 */
7317c478bd9Sstevel@tonic-gate 	rw_init(&splist_lock, NULL, RW_DEFAULT, NULL);
7327c478bd9Sstevel@tonic-gate 
7337c478bd9Sstevel@tonic-gate 	/*
7347c478bd9Sstevel@tonic-gate 	 * If sockets are needed before init runs /sbin/soconfig
7357c478bd9Sstevel@tonic-gate 	 * it is possible to preload the sockparams list here using
7367c478bd9Sstevel@tonic-gate 	 * calls like:
7377c478bd9Sstevel@tonic-gate 	 *	sockconfig(1,2,3, "/dev/tcp", 0);
7387c478bd9Sstevel@tonic-gate 	 */
7397c478bd9Sstevel@tonic-gate 
7407c478bd9Sstevel@tonic-gate 	/*
7417c478bd9Sstevel@tonic-gate 	 * Create a unique dev_t for use in so_fsid.
7427c478bd9Sstevel@tonic-gate 	 */
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	if ((dev = getudev()) == (major_t)-1)
7457c478bd9Sstevel@tonic-gate 		dev = 0;
7467c478bd9Sstevel@tonic-gate 	sockdev = makedevice(dev, 0);
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 	mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL);
7497c478bd9Sstevel@tonic-gate 	sendfile_init();
7507c478bd9Sstevel@tonic-gate 	nl7c_init();
7517c478bd9Sstevel@tonic-gate 
7527c478bd9Sstevel@tonic-gate 	return (0);
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate failure:
7557c478bd9Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(fstype);
7567c478bd9Sstevel@tonic-gate 	if (socktpi_vnodeops != NULL)
7577c478bd9Sstevel@tonic-gate 		vn_freevnodeops(socktpi_vnodeops);
7587c478bd9Sstevel@tonic-gate 	if (err_str != NULL)
7592caf0dcdSrshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str);
7607c478bd9Sstevel@tonic-gate 	return (error);
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate /*
7647c478bd9Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOLOCKED.
7657c478bd9Sstevel@tonic-gate  */
7667c478bd9Sstevel@tonic-gate void
7677c478bd9Sstevel@tonic-gate so_lock_single(struct sonode *so)
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) {
7727c478bd9Sstevel@tonic-gate 		so->so_flag |= SOWANT;
7737c478bd9Sstevel@tonic-gate 		cv_wait_stop(&so->so_want_cv, &so->so_lock,
7747c478bd9Sstevel@tonic-gate 			SO_LOCK_WAKEUP_TIME);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	so->so_flag |= SOLOCKED;
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate /*
7807c478bd9Sstevel@tonic-gate  * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND.
7817c478bd9Sstevel@tonic-gate  * Used to clear SOLOCKED or SOASYNC_UNBIND.
7827c478bd9Sstevel@tonic-gate  */
7837c478bd9Sstevel@tonic-gate void
7847c478bd9Sstevel@tonic-gate so_unlock_single(struct sonode *so, int flag)
7857c478bd9Sstevel@tonic-gate {
7867c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
7877c478bd9Sstevel@tonic-gate 	ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND));
7887c478bd9Sstevel@tonic-gate 	ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0);
7897c478bd9Sstevel@tonic-gate 	ASSERT(so->so_flag & flag);
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	/*
7927c478bd9Sstevel@tonic-gate 	 * Process the T_DISCON_IND on so_discon_ind_mp.
7937c478bd9Sstevel@tonic-gate 	 *
7947c478bd9Sstevel@tonic-gate 	 * Call to so_drain_discon_ind will result in so_lock
7957c478bd9Sstevel@tonic-gate 	 * being dropped and re-acquired later.
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate 	if (so->so_discon_ind_mp != NULL)
7987c478bd9Sstevel@tonic-gate 		so_drain_discon_ind(so);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (so->so_flag & SOWANT)
8017c478bd9Sstevel@tonic-gate 		cv_broadcast(&so->so_want_cv);
8027c478bd9Sstevel@tonic-gate 	so->so_flag &= ~(SOWANT|flag);
8037c478bd9Sstevel@tonic-gate }
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate /*
8067c478bd9Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOREADLOCKED.
8077c478bd9Sstevel@tonic-gate  * If the caller wants nonblocking behavior it should set fmode.
8087c478bd9Sstevel@tonic-gate  */
8097c478bd9Sstevel@tonic-gate int
8107c478bd9Sstevel@tonic-gate so_lock_read(struct sonode *so, int fmode)
8117c478bd9Sstevel@tonic-gate {
8127c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
8157c478bd9Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
8167c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
8177c478bd9Sstevel@tonic-gate 		so->so_flag |= SOWANT;
8187c478bd9Sstevel@tonic-gate 		cv_wait_stop(&so->so_want_cv, &so->so_lock,
8197c478bd9Sstevel@tonic-gate 			SO_LOCK_WAKEUP_TIME);
8207c478bd9Sstevel@tonic-gate 	}
8217c478bd9Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
8227c478bd9Sstevel@tonic-gate 	return (0);
8237c478bd9Sstevel@tonic-gate }
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate /*
8267c478bd9Sstevel@tonic-gate  * Like so_lock_read above but allows signals.
8277c478bd9Sstevel@tonic-gate  */
8287c478bd9Sstevel@tonic-gate int
8297c478bd9Sstevel@tonic-gate so_lock_read_intr(struct sonode *so, int fmode)
8307c478bd9Sstevel@tonic-gate {
8317c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
8347c478bd9Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
8357c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
8367c478bd9Sstevel@tonic-gate 		so->so_flag |= SOWANT;
8377c478bd9Sstevel@tonic-gate 		if (!cv_wait_sig(&so->so_want_cv, &so->so_lock))
8387c478bd9Sstevel@tonic-gate 			return (EINTR);
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
8417c478bd9Sstevel@tonic-gate 	return (0);
8427c478bd9Sstevel@tonic-gate }
8437c478bd9Sstevel@tonic-gate 
8447c478bd9Sstevel@tonic-gate /*
8457c478bd9Sstevel@tonic-gate  * Caller must hold the mutex. Used to clear SOREADLOCKED,
8467c478bd9Sstevel@tonic-gate  * set in so_lock_read() or so_lock_read_intr().
8477c478bd9Sstevel@tonic-gate  */
8487c478bd9Sstevel@tonic-gate void
8497c478bd9Sstevel@tonic-gate so_unlock_read(struct sonode *so)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
8527c478bd9Sstevel@tonic-gate 	ASSERT(so->so_flag & SOREADLOCKED);
8537c478bd9Sstevel@tonic-gate 
8547c478bd9Sstevel@tonic-gate 	if (so->so_flag & SOWANT)
8557c478bd9Sstevel@tonic-gate 		cv_broadcast(&so->so_want_cv);
8567c478bd9Sstevel@tonic-gate 	so->so_flag &= ~(SOWANT|SOREADLOCKED);
8577c478bd9Sstevel@tonic-gate }
8587c478bd9Sstevel@tonic-gate 
8597c478bd9Sstevel@tonic-gate /*
8607c478bd9Sstevel@tonic-gate  * Verify that the specified offset falls within the mblk and
8617c478bd9Sstevel@tonic-gate  * that the resulting pointer is aligned.
8627c478bd9Sstevel@tonic-gate  * Returns NULL if not.
8637c478bd9Sstevel@tonic-gate  */
8647c478bd9Sstevel@tonic-gate void *
8657c478bd9Sstevel@tonic-gate sogetoff(mblk_t *mp, t_uscalar_t offset,
8667c478bd9Sstevel@tonic-gate     t_uscalar_t length, uint_t align_size)
8677c478bd9Sstevel@tonic-gate {
8687c478bd9Sstevel@tonic-gate 	uintptr_t ptr1, ptr2;
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 	ASSERT(mp && mp->b_wptr >= mp->b_rptr);
8717c478bd9Sstevel@tonic-gate 	ptr1 = (uintptr_t)mp->b_rptr + offset;
8727c478bd9Sstevel@tonic-gate 	ptr2 = (uintptr_t)ptr1 + length;
8737c478bd9Sstevel@tonic-gate 	if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) {
8747c478bd9Sstevel@tonic-gate 		eprintline(0);
8757c478bd9Sstevel@tonic-gate 		return (NULL);
8767c478bd9Sstevel@tonic-gate 	}
8777c478bd9Sstevel@tonic-gate 	if ((ptr1 & (align_size - 1)) != 0) {
8787c478bd9Sstevel@tonic-gate 		eprintline(0);
8797c478bd9Sstevel@tonic-gate 		return (NULL);
8807c478bd9Sstevel@tonic-gate 	}
8817c478bd9Sstevel@tonic-gate 	return ((void *)ptr1);
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /*
8857c478bd9Sstevel@tonic-gate  * Return the AF_UNIX underlying filesystem vnode matching a given name.
8867c478bd9Sstevel@tonic-gate  * Makes sure the sending and the destination sonodes are compatible.
8877c478bd9Sstevel@tonic-gate  * The vnode is returned held.
8887c478bd9Sstevel@tonic-gate  *
8897c478bd9Sstevel@tonic-gate  * The underlying filesystem VSOCK vnode has a v_stream pointer that
8907c478bd9Sstevel@tonic-gate  * references the actual stream head (hence indirectly the actual sonode).
8917c478bd9Sstevel@tonic-gate  */
8927c478bd9Sstevel@tonic-gate static int
8937c478bd9Sstevel@tonic-gate so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess,
8947c478bd9Sstevel@tonic-gate 		vnode_t **vpp)
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	vnode_t		*vp;	/* Underlying filesystem vnode */
8977c478bd9Sstevel@tonic-gate 	vnode_t		*svp;	/* sockfs vnode */
8987c478bd9Sstevel@tonic-gate 	struct sonode	*so2;
8997c478bd9Sstevel@tonic-gate 	int		error;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n",
9027c478bd9Sstevel@tonic-gate 		so, soun->sun_path));
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
9057c478bd9Sstevel@tonic-gate 	if (error) {
9067c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
9077c478bd9Sstevel@tonic-gate 		return (error);
9087c478bd9Sstevel@tonic-gate 	}
9097c478bd9Sstevel@tonic-gate 	if (vp->v_type != VSOCK) {
9107c478bd9Sstevel@tonic-gate 		error = ENOTSOCK;
9117c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
9127c478bd9Sstevel@tonic-gate 		goto done2;
9137c478bd9Sstevel@tonic-gate 	}
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 	if (checkaccess) {
9167c478bd9Sstevel@tonic-gate 		/*
9177c478bd9Sstevel@tonic-gate 		 * Check that we have permissions to access the destination
9187c478bd9Sstevel@tonic-gate 		 * vnode. This check is not done in BSD but it is required
9197c478bd9Sstevel@tonic-gate 		 * by X/Open.
9207c478bd9Sstevel@tonic-gate 		 */
9217c478bd9Sstevel@tonic-gate 		if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED())) {
9227c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
9237c478bd9Sstevel@tonic-gate 			goto done2;
9247c478bd9Sstevel@tonic-gate 		}
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	/*
9287c478bd9Sstevel@tonic-gate 	 * Check if the remote socket has been closed.
9297c478bd9Sstevel@tonic-gate 	 *
9307c478bd9Sstevel@tonic-gate 	 * Synchronize with vn_rele_stream by holding v_lock while traversing
9317c478bd9Sstevel@tonic-gate 	 * v_stream->sd_vnode.
9327c478bd9Sstevel@tonic-gate 	 */
9337c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
9347c478bd9Sstevel@tonic-gate 	if (vp->v_stream == NULL) {
9357c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
9367c478bd9Sstevel@tonic-gate 		if (so->so_type == SOCK_DGRAM)
9377c478bd9Sstevel@tonic-gate 			error = EDESTADDRREQ;
9387c478bd9Sstevel@tonic-gate 		else
9397c478bd9Sstevel@tonic-gate 			error = ECONNREFUSED;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
9427c478bd9Sstevel@tonic-gate 		goto done2;
9437c478bd9Sstevel@tonic-gate 	}
9447c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream->sd_vnode);
9457c478bd9Sstevel@tonic-gate 	svp = vp->v_stream->sd_vnode;
9467c478bd9Sstevel@tonic-gate 	/*
9477c478bd9Sstevel@tonic-gate 	 * holding v_lock on underlying filesystem vnode and acquiring
9487c478bd9Sstevel@tonic-gate 	 * it on sockfs vnode. Assumes that no code ever attempts to
9497c478bd9Sstevel@tonic-gate 	 * acquire these locks in the reverse order.
9507c478bd9Sstevel@tonic-gate 	 */
9517c478bd9Sstevel@tonic-gate 	VN_HOLD(svp);
9527c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 	if (svp->v_type != VSOCK) {
9557c478bd9Sstevel@tonic-gate 		error = ENOTSOCK;
9567c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
9577c478bd9Sstevel@tonic-gate 		goto done;
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	so2 = VTOSO(svp);
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 	if (so->so_type != so2->so_type) {
9637c478bd9Sstevel@tonic-gate 		error = EPROTOTYPE;
9647c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
9657c478bd9Sstevel@tonic-gate 		goto done;
9667c478bd9Sstevel@tonic-gate 	}
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 	VN_RELE(svp);
9697c478bd9Sstevel@tonic-gate 	*vpp = vp;
9707c478bd9Sstevel@tonic-gate 	return (0);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate done:
9737c478bd9Sstevel@tonic-gate 	VN_RELE(svp);
9747c478bd9Sstevel@tonic-gate done2:
9757c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
9767c478bd9Sstevel@tonic-gate 	return (error);
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
9797c478bd9Sstevel@tonic-gate /*
9807c478bd9Sstevel@tonic-gate  * Verify peer address for connect and sendto/sendmsg.
9817c478bd9Sstevel@tonic-gate  * Since sendto/sendmsg would not get synchronous errors from the transport
9827c478bd9Sstevel@tonic-gate  * provider we have to do these ugly checks in the socket layer to
9837c478bd9Sstevel@tonic-gate  * preserve compatibility with SunOS 4.X.
9847c478bd9Sstevel@tonic-gate  */
9857c478bd9Sstevel@tonic-gate int
9867c478bd9Sstevel@tonic-gate so_addr_verify(struct sonode *so, const struct sockaddr *name,
9877c478bd9Sstevel@tonic-gate     socklen_t namelen)
9887c478bd9Sstevel@tonic-gate {
9897c478bd9Sstevel@tonic-gate 	int		family;
9907c478bd9Sstevel@tonic-gate 
9917c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n", so, name, namelen));
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
9947c478bd9Sstevel@tonic-gate 
9957c478bd9Sstevel@tonic-gate 	family = so->so_family;
9967c478bd9Sstevel@tonic-gate 	switch (family) {
9977c478bd9Sstevel@tonic-gate 	case AF_INET:
9987c478bd9Sstevel@tonic-gate 		if (name->sa_family != family) {
9997c478bd9Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
10007c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
10017c478bd9Sstevel@tonic-gate 		}
10027c478bd9Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
10037c478bd9Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
10047c478bd9Sstevel@tonic-gate 			return (EINVAL);
10057c478bd9Sstevel@tonic-gate 		}
10067c478bd9Sstevel@tonic-gate 		break;
10077c478bd9Sstevel@tonic-gate 	case AF_INET6: {
10087c478bd9Sstevel@tonic-gate #ifdef DEBUG
10097c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
10107c478bd9Sstevel@tonic-gate #endif /* DEBUG */
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 		if (name->sa_family != family) {
10137c478bd9Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
10147c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
10157c478bd9Sstevel@tonic-gate 		}
10167c478bd9Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
10177c478bd9Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
10187c478bd9Sstevel@tonic-gate 			return (EINVAL);
10197c478bd9Sstevel@tonic-gate 		}
10207c478bd9Sstevel@tonic-gate #ifdef DEBUG
10217c478bd9Sstevel@tonic-gate 		/* Verify that apps don't forget to clear sin6_scope_id etc */
10227c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)name;
10237c478bd9Sstevel@tonic-gate 		if (sin6->sin6_scope_id != 0 &&
10247c478bd9Sstevel@tonic-gate 		    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
10252caf0dcdSrshoaib 			zcmn_err(getzoneid(), CE_WARN,
10267c478bd9Sstevel@tonic-gate 			    "connect/send* with uninitialized sin6_scope_id "
10277c478bd9Sstevel@tonic-gate 			    "(%d) on socket. Pid = %d\n",
10287c478bd9Sstevel@tonic-gate 			    (int)sin6->sin6_scope_id, (int)curproc->p_pid);
10297c478bd9Sstevel@tonic-gate 		}
10307c478bd9Sstevel@tonic-gate #endif /* DEBUG */
10317c478bd9Sstevel@tonic-gate 		break;
10327c478bd9Sstevel@tonic-gate 	}
10337c478bd9Sstevel@tonic-gate 	case AF_UNIX:
10347c478bd9Sstevel@tonic-gate 		if (so->so_state & SS_FADDR_NOXLATE) {
10357c478bd9Sstevel@tonic-gate 			return (0);
10367c478bd9Sstevel@tonic-gate 		}
10377c478bd9Sstevel@tonic-gate 		if (namelen < (socklen_t)sizeof (short)) {
10387c478bd9Sstevel@tonic-gate 			eprintsoline(so, ENOENT);
10397c478bd9Sstevel@tonic-gate 			return (ENOENT);
10407c478bd9Sstevel@tonic-gate 		}
10417c478bd9Sstevel@tonic-gate 		if (name->sa_family != family) {
10427c478bd9Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
10437c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
10447c478bd9Sstevel@tonic-gate 		}
10457c478bd9Sstevel@tonic-gate 		/* MAXPATHLEN + soun_family + nul termination */
10467c478bd9Sstevel@tonic-gate 		if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
10477c478bd9Sstevel@tonic-gate 			eprintsoline(so, ENAMETOOLONG);
10487c478bd9Sstevel@tonic-gate 			return (ENAMETOOLONG);
10497c478bd9Sstevel@tonic-gate 		}
10507c478bd9Sstevel@tonic-gate 
10517c478bd9Sstevel@tonic-gate 		break;
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	default:
10547c478bd9Sstevel@tonic-gate 		/*
10557c478bd9Sstevel@tonic-gate 		 * Default is don't do any length or sa_family check
10567c478bd9Sstevel@tonic-gate 		 * to allow non-sockaddr style addresses.
10577c478bd9Sstevel@tonic-gate 		 */
10587c478bd9Sstevel@tonic-gate 		break;
10597c478bd9Sstevel@tonic-gate 	}
10607c478bd9Sstevel@tonic-gate 
10617c478bd9Sstevel@tonic-gate 	return (0);
10627c478bd9Sstevel@tonic-gate }
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 
10657c478bd9Sstevel@tonic-gate /*
10667c478bd9Sstevel@tonic-gate  * Translate an AF_UNIX sockaddr_un to the transport internal name.
10677c478bd9Sstevel@tonic-gate  * Assumes caller has called so_addr_verify first.
10687c478bd9Sstevel@tonic-gate  */
10697c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10707c478bd9Sstevel@tonic-gate int
10717c478bd9Sstevel@tonic-gate so_ux_addr_xlate(struct sonode *so, struct sockaddr *name,
10727c478bd9Sstevel@tonic-gate     socklen_t namelen, int checkaccess,
10737c478bd9Sstevel@tonic-gate     void **addrp, socklen_t *addrlenp)
10747c478bd9Sstevel@tonic-gate {
10757c478bd9Sstevel@tonic-gate 	int			error;
10767c478bd9Sstevel@tonic-gate 	struct sockaddr_un	*soun;
10777c478bd9Sstevel@tonic-gate 	vnode_t			*vp;
10787c478bd9Sstevel@tonic-gate 	void			*addr;
10797c478bd9Sstevel@tonic-gate 	socklen_t		addrlen;
10807c478bd9Sstevel@tonic-gate 
10817c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n",
10827c478bd9Sstevel@tonic-gate 			so, name, namelen, checkaccess));
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
10857c478bd9Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
10867c478bd9Sstevel@tonic-gate 	ASSERT(!(so->so_state & SS_FADDR_NOXLATE));
10877c478bd9Sstevel@tonic-gate 	ASSERT(namelen >= (socklen_t)sizeof (short));
10887c478bd9Sstevel@tonic-gate 	ASSERT(name->sa_family == AF_UNIX);
10897c478bd9Sstevel@tonic-gate 	soun = (struct sockaddr_un *)name;
10907c478bd9Sstevel@tonic-gate 	/*
10917c478bd9Sstevel@tonic-gate 	 * Lookup vnode for the specified path name and verify that
10927c478bd9Sstevel@tonic-gate 	 * it is a socket.
10937c478bd9Sstevel@tonic-gate 	 */
10947c478bd9Sstevel@tonic-gate 	error = so_ux_lookup(so, soun, checkaccess, &vp);
10957c478bd9Sstevel@tonic-gate 	if (error) {
10967c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
10977c478bd9Sstevel@tonic-gate 		return (error);
10987c478bd9Sstevel@tonic-gate 	}
10997c478bd9Sstevel@tonic-gate 	/*
11007c478bd9Sstevel@tonic-gate 	 * Use the address of the peer vnode as the address to send
11017c478bd9Sstevel@tonic-gate 	 * to. We release the peer vnode here. In case it has been
11027c478bd9Sstevel@tonic-gate 	 * closed by the time the T_CONN_REQ or T_UNIDATA_REQ reaches the
11037c478bd9Sstevel@tonic-gate 	 * transport the message will get an error or be dropped.
11047c478bd9Sstevel@tonic-gate 	 */
11057c478bd9Sstevel@tonic-gate 	so->so_ux_faddr.soua_vp = vp;
11067c478bd9Sstevel@tonic-gate 	so->so_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT;
11077c478bd9Sstevel@tonic-gate 	addr = &so->so_ux_faddr;
11087c478bd9Sstevel@tonic-gate 	addrlen = (socklen_t)sizeof (so->so_ux_faddr);
11097c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n",
11107c478bd9Sstevel@tonic-gate 				addrlen, vp));
11117c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
11127c478bd9Sstevel@tonic-gate 	*addrp = addr;
11137c478bd9Sstevel@tonic-gate 	*addrlenp = (socklen_t)addrlen;
11147c478bd9Sstevel@tonic-gate 	return (0);
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate /*
11187c478bd9Sstevel@tonic-gate  * Esballoc free function for messages that contain SO_FILEP option.
11197c478bd9Sstevel@tonic-gate  * Decrement the reference count on the file pointers using closef.
11207c478bd9Sstevel@tonic-gate  */
11217c478bd9Sstevel@tonic-gate void
11227c478bd9Sstevel@tonic-gate fdbuf_free(struct fdbuf *fdbuf)
11237c478bd9Sstevel@tonic-gate {
11247c478bd9Sstevel@tonic-gate 	int	i;
11257c478bd9Sstevel@tonic-gate 	struct file *fp;
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd));
11287c478bd9Sstevel@tonic-gate 	for (i = 0; i < fdbuf->fd_numfd; i++) {
11297c478bd9Sstevel@tonic-gate 		/*
11307c478bd9Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
11317c478bd9Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
11327c478bd9Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
11337c478bd9Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
11347c478bd9Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
11357c478bd9Sstevel@tonic-gate 		 */
11367c478bd9Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
11377c478bd9Sstevel@tonic-gate 		dprint(1, ("fdbuf_free: [%d] = %p\n", i, fp));
11387c478bd9Sstevel@tonic-gate 		(void) closef(fp);
11397c478bd9Sstevel@tonic-gate 	}
11407c478bd9Sstevel@tonic-gate 	if (fdbuf->fd_ebuf != NULL)
11417c478bd9Sstevel@tonic-gate 		kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen);
11427c478bd9Sstevel@tonic-gate 	kmem_free(fdbuf, fdbuf->fd_size);
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate /*
1146bd118333Smeem  * Allocate an esballoc'ed message for AF_UNIX file descriptor passing.
1147bd118333Smeem  * Waits if memory is not available.
11487c478bd9Sstevel@tonic-gate  */
11497c478bd9Sstevel@tonic-gate mblk_t *
11507c478bd9Sstevel@tonic-gate fdbuf_allocmsg(int size, struct fdbuf *fdbuf)
11517c478bd9Sstevel@tonic-gate {
1152bd118333Smeem 	uchar_t	*buf;
11537c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd));
11567c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(size, KM_SLEEP);
11577c478bd9Sstevel@tonic-gate 	fdbuf->fd_ebuf = (caddr_t)buf;
11587c478bd9Sstevel@tonic-gate 	fdbuf->fd_ebuflen = size;
11597c478bd9Sstevel@tonic-gate 	fdbuf->fd_frtn.free_func = fdbuf_free;
11607c478bd9Sstevel@tonic-gate 	fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf;
11617c478bd9Sstevel@tonic-gate 
1162bd118333Smeem 	mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn);
11637c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
11647c478bd9Sstevel@tonic-gate 	return (mp);
11657c478bd9Sstevel@tonic-gate }
11667c478bd9Sstevel@tonic-gate 
11677c478bd9Sstevel@tonic-gate /*
11687c478bd9Sstevel@tonic-gate  * Extract file descriptors from a fdbuf.
11697c478bd9Sstevel@tonic-gate  * Return list in rights/rightslen.
11707c478bd9Sstevel@tonic-gate  */
11717c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11727c478bd9Sstevel@tonic-gate static int
11737c478bd9Sstevel@tonic-gate fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen)
11747c478bd9Sstevel@tonic-gate {
11757c478bd9Sstevel@tonic-gate 	int	i, fd;
11767c478bd9Sstevel@tonic-gate 	int	*rp;
11777c478bd9Sstevel@tonic-gate 	struct file *fp;
11787c478bd9Sstevel@tonic-gate 	int	numfd;
11797c478bd9Sstevel@tonic-gate 
11807c478bd9Sstevel@tonic-gate 	dprint(1, ("fdbuf_extract: %d fds, len %d\n",
11817c478bd9Sstevel@tonic-gate 		fdbuf->fd_numfd, rightslen));
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate 	numfd = fdbuf->fd_numfd;
11847c478bd9Sstevel@tonic-gate 	ASSERT(rightslen == numfd * (int)sizeof (int));
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	/*
11877c478bd9Sstevel@tonic-gate 	 * Allocate a file descriptor and increment the f_count.
11887c478bd9Sstevel@tonic-gate 	 * The latter is needed since we always call fdbuf_free
11897c478bd9Sstevel@tonic-gate 	 * which performs a closef.
11907c478bd9Sstevel@tonic-gate 	 */
11917c478bd9Sstevel@tonic-gate 	rp = (int *)rights;
11927c478bd9Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
11937c478bd9Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1)
11947c478bd9Sstevel@tonic-gate 			goto cleanup;
11957c478bd9Sstevel@tonic-gate 		/*
11967c478bd9Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
11977c478bd9Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
11987c478bd9Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
11997c478bd9Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
12007c478bd9Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
12017c478bd9Sstevel@tonic-gate 		 */
12027c478bd9Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
12037c478bd9Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
12047c478bd9Sstevel@tonic-gate 		fp->f_count++;
12057c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
12067c478bd9Sstevel@tonic-gate 		setf(fd, fp);
12077c478bd9Sstevel@tonic-gate 		*rp++ = fd;
12087c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
12097c478bd9Sstevel@tonic-gate 		if (audit_active)
12107c478bd9Sstevel@tonic-gate 			audit_fdrecv(fd, fp);
12117c478bd9Sstevel@tonic-gate #endif
12127c478bd9Sstevel@tonic-gate 		dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
12137c478bd9Sstevel@tonic-gate 			i, fd, fp, fp->f_count));
12147c478bd9Sstevel@tonic-gate 	}
12157c478bd9Sstevel@tonic-gate 	return (0);
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate cleanup:
12187c478bd9Sstevel@tonic-gate 	/*
12197c478bd9Sstevel@tonic-gate 	 * Undo whatever partial work the loop above has done.
12207c478bd9Sstevel@tonic-gate 	 */
12217c478bd9Sstevel@tonic-gate 	{
12227c478bd9Sstevel@tonic-gate 		int j;
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 		rp = (int *)rights;
12257c478bd9Sstevel@tonic-gate 		for (j = 0; j < i; j++) {
12267c478bd9Sstevel@tonic-gate 			dprint(0,
12277c478bd9Sstevel@tonic-gate 			    ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
12287c478bd9Sstevel@tonic-gate 			(void) closeandsetf(*rp++, NULL);
12297c478bd9Sstevel@tonic-gate 		}
12307c478bd9Sstevel@tonic-gate 	}
12317c478bd9Sstevel@tonic-gate 
12327c478bd9Sstevel@tonic-gate 	return (EMFILE);
12337c478bd9Sstevel@tonic-gate }
12347c478bd9Sstevel@tonic-gate 
12357c478bd9Sstevel@tonic-gate /*
12367c478bd9Sstevel@tonic-gate  * Insert file descriptors into an fdbuf.
12377c478bd9Sstevel@tonic-gate  * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
12387c478bd9Sstevel@tonic-gate  * by calling fdbuf_free().
12397c478bd9Sstevel@tonic-gate  */
12407c478bd9Sstevel@tonic-gate int
12417c478bd9Sstevel@tonic-gate fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
12427c478bd9Sstevel@tonic-gate {
12437c478bd9Sstevel@tonic-gate 	int		numfd, i;
12447c478bd9Sstevel@tonic-gate 	int		*fds;
12457c478bd9Sstevel@tonic-gate 	struct file	*fp;
12467c478bd9Sstevel@tonic-gate 	struct fdbuf	*fdbuf;
12477c478bd9Sstevel@tonic-gate 	int		fdbufsize;
12487c478bd9Sstevel@tonic-gate 
12497c478bd9Sstevel@tonic-gate 	dprint(1, ("fdbuf_create: len %d\n", rightslen));
12507c478bd9Sstevel@tonic-gate 
12517c478bd9Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
12527c478bd9Sstevel@tonic-gate 
12537c478bd9Sstevel@tonic-gate 	fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
12547c478bd9Sstevel@tonic-gate 	fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
12557c478bd9Sstevel@tonic-gate 	fdbuf->fd_size = fdbufsize;
12567c478bd9Sstevel@tonic-gate 	fdbuf->fd_numfd = 0;
12577c478bd9Sstevel@tonic-gate 	fdbuf->fd_ebuf = NULL;
12587c478bd9Sstevel@tonic-gate 	fdbuf->fd_ebuflen = 0;
12597c478bd9Sstevel@tonic-gate 	fds = (int *)rights;
12607c478bd9Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
12617c478bd9Sstevel@tonic-gate 		if ((fp = getf(fds[i])) == NULL) {
12627c478bd9Sstevel@tonic-gate 			fdbuf_free(fdbuf);
12637c478bd9Sstevel@tonic-gate 			return (EBADF);
12647c478bd9Sstevel@tonic-gate 		}
12657c478bd9Sstevel@tonic-gate 		dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
12667c478bd9Sstevel@tonic-gate 			i, fds[i], fp, fp->f_count));
12677c478bd9Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
12687c478bd9Sstevel@tonic-gate 		fp->f_count++;
12697c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
12707c478bd9Sstevel@tonic-gate 		/*
12717c478bd9Sstevel@tonic-gate 		 * The maximum alignment for fdbuf (or any option header
12727c478bd9Sstevel@tonic-gate 		 * and its value) it 4 bytes. On a LP64 kernel, the alignment
12737c478bd9Sstevel@tonic-gate 		 * is not sufficient for pointers (fd_fds in this case). Since
12747c478bd9Sstevel@tonic-gate 		 * we just did a kmem_alloc (we get a double word alignment),
12757c478bd9Sstevel@tonic-gate 		 * we don't need to do anything on the send side (we loose
12767c478bd9Sstevel@tonic-gate 		 * the double word alignment because fdbuf goes after an
12777c478bd9Sstevel@tonic-gate 		 * option header (eg T_unitdata_req) which is only 4 byte
12787c478bd9Sstevel@tonic-gate 		 * aligned). We take care of this when we extract the file
12797c478bd9Sstevel@tonic-gate 		 * descriptor in fdbuf_extract or fdbuf_free.
12807c478bd9Sstevel@tonic-gate 		 */
12817c478bd9Sstevel@tonic-gate 		fdbuf->fd_fds[i] = fp;
12827c478bd9Sstevel@tonic-gate 		fdbuf->fd_numfd++;
12837c478bd9Sstevel@tonic-gate 		releasef(fds[i]);
12847c478bd9Sstevel@tonic-gate #ifdef C2_AUDIT
12857c478bd9Sstevel@tonic-gate 		if (audit_active)
12867c478bd9Sstevel@tonic-gate 			audit_fdsend(fds[i], fp, 0);
12877c478bd9Sstevel@tonic-gate #endif
12887c478bd9Sstevel@tonic-gate 	}
12897c478bd9Sstevel@tonic-gate 	*fdbufp = fdbuf;
12907c478bd9Sstevel@tonic-gate 	return (0);
12917c478bd9Sstevel@tonic-gate }
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate static int
12947c478bd9Sstevel@tonic-gate fdbuf_optlen(int rightslen)
12957c478bd9Sstevel@tonic-gate {
12967c478bd9Sstevel@tonic-gate 	int numfd;
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
12997c478bd9Sstevel@tonic-gate 
13007c478bd9Sstevel@tonic-gate 	return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
13017c478bd9Sstevel@tonic-gate }
13027c478bd9Sstevel@tonic-gate 
13037c478bd9Sstevel@tonic-gate static t_uscalar_t
13047c478bd9Sstevel@tonic-gate fdbuf_cmsglen(int fdbuflen)
13057c478bd9Sstevel@tonic-gate {
13067c478bd9Sstevel@tonic-gate 	return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
13077c478bd9Sstevel@tonic-gate 	    (int)sizeof (struct file *) * (int)sizeof (int));
13087c478bd9Sstevel@tonic-gate }
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 
13117c478bd9Sstevel@tonic-gate /*
13127c478bd9Sstevel@tonic-gate  * Return non-zero if the mblk and fdbuf are consistent.
13137c478bd9Sstevel@tonic-gate  */
13147c478bd9Sstevel@tonic-gate static int
13157c478bd9Sstevel@tonic-gate fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
13167c478bd9Sstevel@tonic-gate {
13177c478bd9Sstevel@tonic-gate 	if (fdbuflen >= FDBUF_HDRSIZE &&
13187c478bd9Sstevel@tonic-gate 	    fdbuflen == fdbuf->fd_size) {
13197c478bd9Sstevel@tonic-gate 		frtn_t *frp = mp->b_datap->db_frtnp;
13207c478bd9Sstevel@tonic-gate 		/*
13217c478bd9Sstevel@tonic-gate 		 * Check that the SO_FILEP portion of the
13227c478bd9Sstevel@tonic-gate 		 * message has not been modified by
13237c478bd9Sstevel@tonic-gate 		 * the loopback transport. The sending sockfs generates
13247c478bd9Sstevel@tonic-gate 		 * a message that is esballoc'ed with the free function
13257c478bd9Sstevel@tonic-gate 		 * being fdbuf_free() and where free_arg contains the
13267c478bd9Sstevel@tonic-gate 		 * identical information as the SO_FILEP content.
13277c478bd9Sstevel@tonic-gate 		 *
13287c478bd9Sstevel@tonic-gate 		 * If any of these constraints are not satisfied we
13297c478bd9Sstevel@tonic-gate 		 * silently ignore the option.
13307c478bd9Sstevel@tonic-gate 		 */
13317c478bd9Sstevel@tonic-gate 		ASSERT(mp);
13327c478bd9Sstevel@tonic-gate 		if (frp != NULL &&
13337c478bd9Sstevel@tonic-gate 		    frp->free_func == fdbuf_free &&
13347c478bd9Sstevel@tonic-gate 		    frp->free_arg != NULL &&
13357c478bd9Sstevel@tonic-gate 		    bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
13367c478bd9Sstevel@tonic-gate 			dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
13377c478bd9Sstevel@tonic-gate 				fdbuf, fdbuflen));
13387c478bd9Sstevel@tonic-gate 			return (1);
13397c478bd9Sstevel@tonic-gate 		} else {
13402caf0dcdSrshoaib 			zcmn_err(getzoneid(), CE_WARN,
13417c478bd9Sstevel@tonic-gate 			    "sockfs: mismatched fdbuf content (%p)",
13427c478bd9Sstevel@tonic-gate 			    (void *)mp);
13437c478bd9Sstevel@tonic-gate 			return (0);
13447c478bd9Sstevel@tonic-gate 		}
13457c478bd9Sstevel@tonic-gate 	} else {
13462caf0dcdSrshoaib 		zcmn_err(getzoneid(), CE_WARN,
13477c478bd9Sstevel@tonic-gate 		    "sockfs: mismatched fdbuf len %d, %d\n",
13487c478bd9Sstevel@tonic-gate 		    fdbuflen, fdbuf->fd_size);
13497c478bd9Sstevel@tonic-gate 		return (0);
13507c478bd9Sstevel@tonic-gate 	}
13517c478bd9Sstevel@tonic-gate }
13527c478bd9Sstevel@tonic-gate 
13537c478bd9Sstevel@tonic-gate /*
13547c478bd9Sstevel@tonic-gate  * When the file descriptors returned by sorecvmsg can not be passed
13557c478bd9Sstevel@tonic-gate  * to the application this routine will cleanup the references on
13567c478bd9Sstevel@tonic-gate  * the files. Start at startoff bytes into the buffer.
13577c478bd9Sstevel@tonic-gate  */
13587c478bd9Sstevel@tonic-gate static void
13597c478bd9Sstevel@tonic-gate close_fds(void *fdbuf, int fdbuflen, int startoff)
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate 	int *fds = (int *)fdbuf;
13627c478bd9Sstevel@tonic-gate 	int numfd = fdbuflen / (int)sizeof (int);
13637c478bd9Sstevel@tonic-gate 	int i;
13647c478bd9Sstevel@tonic-gate 
13657c478bd9Sstevel@tonic-gate 	dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
13667c478bd9Sstevel@tonic-gate 
13677c478bd9Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
13687c478bd9Sstevel@tonic-gate 		if (startoff < 0)
13697c478bd9Sstevel@tonic-gate 			startoff = 0;
13707c478bd9Sstevel@tonic-gate 		if (startoff < (int)sizeof (int)) {
13717c478bd9Sstevel@tonic-gate 			/*
13727c478bd9Sstevel@tonic-gate 			 * This file descriptor is partially or fully after
13737c478bd9Sstevel@tonic-gate 			 * the offset
13747c478bd9Sstevel@tonic-gate 			 */
13757c478bd9Sstevel@tonic-gate 			dprint(0,
13767c478bd9Sstevel@tonic-gate 			    ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
13777c478bd9Sstevel@tonic-gate 			(void) closeandsetf(fds[i], NULL);
13787c478bd9Sstevel@tonic-gate 		}
13797c478bd9Sstevel@tonic-gate 		startoff -= (int)sizeof (int);
13807c478bd9Sstevel@tonic-gate 	}
13817c478bd9Sstevel@tonic-gate }
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate /*
13847c478bd9Sstevel@tonic-gate  * Close all file descriptors contained in the control part starting at
13857c478bd9Sstevel@tonic-gate  * the startoffset.
13867c478bd9Sstevel@tonic-gate  */
13877c478bd9Sstevel@tonic-gate void
13887c478bd9Sstevel@tonic-gate so_closefds(void *control, t_uscalar_t controllen, int oldflg,
13897c478bd9Sstevel@tonic-gate     int startoff)
13907c478bd9Sstevel@tonic-gate {
13917c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate 	if (control == NULL)
13947c478bd9Sstevel@tonic-gate 		return;
13957c478bd9Sstevel@tonic-gate 
13967c478bd9Sstevel@tonic-gate 	if (oldflg) {
13977c478bd9Sstevel@tonic-gate 		close_fds(control, controllen, startoff);
13987c478bd9Sstevel@tonic-gate 		return;
13997c478bd9Sstevel@tonic-gate 	}
14007c478bd9Sstevel@tonic-gate 	/* Scan control part for file descriptors. */
14017c478bd9Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
14027c478bd9Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
14037c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
14047c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
14057c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
14067c478bd9Sstevel@tonic-gate 			close_fds(CMSG_CONTENT(cmsg),
14077c478bd9Sstevel@tonic-gate 			    (int)CMSG_CONTENTLEN(cmsg),
14087c478bd9Sstevel@tonic-gate 			    startoff - (int)sizeof (struct cmsghdr));
14097c478bd9Sstevel@tonic-gate 		}
14107c478bd9Sstevel@tonic-gate 		startoff -= cmsg->cmsg_len;
14117c478bd9Sstevel@tonic-gate 	}
14127c478bd9Sstevel@tonic-gate }
14137c478bd9Sstevel@tonic-gate 
14147c478bd9Sstevel@tonic-gate /*
14157c478bd9Sstevel@tonic-gate  * Returns a pointer/length for the file descriptors contained
14167c478bd9Sstevel@tonic-gate  * in the control buffer. Returns with *fdlenp == -1 if there are no
14177c478bd9Sstevel@tonic-gate  * file descriptor options present. This is different than there being
14187c478bd9Sstevel@tonic-gate  * a zero-length file descriptor option.
14197c478bd9Sstevel@tonic-gate  * Fail if there are multiple SCM_RIGHT cmsgs.
14207c478bd9Sstevel@tonic-gate  */
14217c478bd9Sstevel@tonic-gate int
14227c478bd9Sstevel@tonic-gate so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
14237c478bd9Sstevel@tonic-gate     void **fdsp, int *fdlenp)
14247c478bd9Sstevel@tonic-gate {
14257c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
14267c478bd9Sstevel@tonic-gate 	void *fds;
14277c478bd9Sstevel@tonic-gate 	int fdlen;
14287c478bd9Sstevel@tonic-gate 
14297c478bd9Sstevel@tonic-gate 	if (control == NULL) {
14307c478bd9Sstevel@tonic-gate 		*fdsp = NULL;
14317c478bd9Sstevel@tonic-gate 		*fdlenp = -1;
14327c478bd9Sstevel@tonic-gate 		return (0);
14337c478bd9Sstevel@tonic-gate 	}
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	if (oldflg) {
14367c478bd9Sstevel@tonic-gate 		*fdsp = control;
14377c478bd9Sstevel@tonic-gate 		if (controllen == 0)
14387c478bd9Sstevel@tonic-gate 			*fdlenp = -1;
14397c478bd9Sstevel@tonic-gate 		else
14407c478bd9Sstevel@tonic-gate 			*fdlenp = controllen;
14417c478bd9Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
14427c478bd9Sstevel@tonic-gate 		return (0);
14437c478bd9Sstevel@tonic-gate 	}
14447c478bd9Sstevel@tonic-gate 
14457c478bd9Sstevel@tonic-gate 	fds = NULL;
14467c478bd9Sstevel@tonic-gate 	fdlen = 0;
14477c478bd9Sstevel@tonic-gate 
14487c478bd9Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
14497c478bd9Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
14507c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
14517c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
14527c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
14537c478bd9Sstevel@tonic-gate 			if (fds != NULL)
14547c478bd9Sstevel@tonic-gate 				return (EINVAL);
14557c478bd9Sstevel@tonic-gate 			fds = CMSG_CONTENT(cmsg);
14567c478bd9Sstevel@tonic-gate 			fdlen = (int)CMSG_CONTENTLEN(cmsg);
14571e0267ddSkrgopi 			dprint(1, ("so_getfdopt: new %lu\n",
14581e0267ddSkrgopi 				(size_t)CMSG_CONTENTLEN(cmsg)));
14597c478bd9Sstevel@tonic-gate 		}
14607c478bd9Sstevel@tonic-gate 	}
14617c478bd9Sstevel@tonic-gate 	if (fds == NULL) {
14627c478bd9Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: NONE\n"));
14637c478bd9Sstevel@tonic-gate 		*fdlenp = -1;
14647c478bd9Sstevel@tonic-gate 	} else
14657c478bd9Sstevel@tonic-gate 		*fdlenp = fdlen;
14667c478bd9Sstevel@tonic-gate 	*fdsp = fds;
14677c478bd9Sstevel@tonic-gate 	return (0);
14687c478bd9Sstevel@tonic-gate }
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate /*
14717c478bd9Sstevel@tonic-gate  * Return the length of the options including any file descriptor options.
14727c478bd9Sstevel@tonic-gate  */
14737c478bd9Sstevel@tonic-gate t_uscalar_t
14747c478bd9Sstevel@tonic-gate so_optlen(void *control, t_uscalar_t controllen, int oldflg)
14757c478bd9Sstevel@tonic-gate {
14767c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
14777c478bd9Sstevel@tonic-gate 	t_uscalar_t optlen = 0;
14787c478bd9Sstevel@tonic-gate 	t_uscalar_t len;
14797c478bd9Sstevel@tonic-gate 
14807c478bd9Sstevel@tonic-gate 	if (control == NULL)
14817c478bd9Sstevel@tonic-gate 		return (0);
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate 	if (oldflg)
14847c478bd9Sstevel@tonic-gate 		return ((t_uscalar_t)(sizeof (struct T_opthdr) +
14857c478bd9Sstevel@tonic-gate 		    fdbuf_optlen(controllen)));
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
14887c478bd9Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
14897c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
14907c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
14917c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
14927c478bd9Sstevel@tonic-gate 			len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
14937c478bd9Sstevel@tonic-gate 		} else {
14947c478bd9Sstevel@tonic-gate 			len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
14957c478bd9Sstevel@tonic-gate 		}
14967c478bd9Sstevel@tonic-gate 		optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
14977c478bd9Sstevel@tonic-gate 		    sizeof (struct T_opthdr));
14987c478bd9Sstevel@tonic-gate 	}
14997c478bd9Sstevel@tonic-gate 	dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
15007c478bd9Sstevel@tonic-gate 		controllen, oldflg, optlen));
15017c478bd9Sstevel@tonic-gate 	return (optlen);
15027c478bd9Sstevel@tonic-gate }
15037c478bd9Sstevel@tonic-gate 
15047c478bd9Sstevel@tonic-gate /*
15057c478bd9Sstevel@tonic-gate  * Copy options from control to the mblk. Skip any file descriptor options.
15067c478bd9Sstevel@tonic-gate  */
15077c478bd9Sstevel@tonic-gate void
15087c478bd9Sstevel@tonic-gate so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
15097c478bd9Sstevel@tonic-gate {
15107c478bd9Sstevel@tonic-gate 	struct T_opthdr toh;
15117c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
15127c478bd9Sstevel@tonic-gate 
15137c478bd9Sstevel@tonic-gate 	if (control == NULL)
15147c478bd9Sstevel@tonic-gate 		return;
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (oldflg) {
15177c478bd9Sstevel@tonic-gate 		/* No real options - caller has handled file descriptors */
15187c478bd9Sstevel@tonic-gate 		return;
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
15217c478bd9Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
15227c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
15237c478bd9Sstevel@tonic-gate 		/*
15247c478bd9Sstevel@tonic-gate 		 * Note: The caller handles file descriptors prior
15257c478bd9Sstevel@tonic-gate 		 * to calling this function.
15267c478bd9Sstevel@tonic-gate 		 */
15277c478bd9Sstevel@tonic-gate 		t_uscalar_t len;
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
15307c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS)
15317c478bd9Sstevel@tonic-gate 			continue;
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 		len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
15347c478bd9Sstevel@tonic-gate 		toh.level = cmsg->cmsg_level;
15357c478bd9Sstevel@tonic-gate 		toh.name = cmsg->cmsg_type;
15367c478bd9Sstevel@tonic-gate 		toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
15377c478bd9Sstevel@tonic-gate 		toh.status = 0;
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
15407c478bd9Sstevel@tonic-gate 		soappendmsg(mp, CMSG_CONTENT(cmsg), len);
15417c478bd9Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
15427c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
15437c478bd9Sstevel@tonic-gate 	}
15447c478bd9Sstevel@tonic-gate }
15457c478bd9Sstevel@tonic-gate 
15467c478bd9Sstevel@tonic-gate /*
15477c478bd9Sstevel@tonic-gate  * Return the length of the control message derived from the options.
15487c478bd9Sstevel@tonic-gate  * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
15497c478bd9Sstevel@tonic-gate  * When oldflg is set only include SO_FILEP.
15500d204002Sgt145670  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
15510d204002Sgt145670  * allocates the space that so_opt2cmsg fills. If one changes, the other should
15520d204002Sgt145670  * also be checked for any possible impacts.
15537c478bd9Sstevel@tonic-gate  */
15547c478bd9Sstevel@tonic-gate t_uscalar_t
15557c478bd9Sstevel@tonic-gate so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
15567c478bd9Sstevel@tonic-gate {
15577c478bd9Sstevel@tonic-gate 	t_uscalar_t cmsglen = 0;
15587c478bd9Sstevel@tonic-gate 	struct T_opthdr *tohp;
15597c478bd9Sstevel@tonic-gate 	t_uscalar_t len;
15607c478bd9Sstevel@tonic-gate 	t_uscalar_t last_roundup = 0;
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
15637c478bd9Sstevel@tonic-gate 
15647c478bd9Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
15657c478bd9Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
15667c478bd9Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
15677c478bd9Sstevel@tonic-gate 		dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
15687c478bd9Sstevel@tonic-gate 			tohp->level, tohp->name, tohp->len));
15697c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
15707c478bd9Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
15717c478bd9Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
15727c478bd9Sstevel@tonic-gate 			continue;
15737c478bd9Sstevel@tonic-gate 		}
15747c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
15757c478bd9Sstevel@tonic-gate 			struct fdbuf *fdbuf;
15767c478bd9Sstevel@tonic-gate 			int fdbuflen;
15777c478bd9Sstevel@tonic-gate 
15787c478bd9Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
15797c478bd9Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
15827c478bd9Sstevel@tonic-gate 				continue;
15837c478bd9Sstevel@tonic-gate 			if (oldflg) {
15847c478bd9Sstevel@tonic-gate 				cmsglen += fdbuf_cmsglen(fdbuflen);
15857c478bd9Sstevel@tonic-gate 				continue;
15867c478bd9Sstevel@tonic-gate 			}
15877c478bd9Sstevel@tonic-gate 			len = fdbuf_cmsglen(fdbuflen);
15880d204002Sgt145670 		} else if (tohp->level == SOL_SOCKET &&
15890d204002Sgt145670 		    tohp->name == SCM_TIMESTAMP) {
15900d204002Sgt145670 			if (oldflg)
15910d204002Sgt145670 				continue;
15920d204002Sgt145670 
15930d204002Sgt145670 			if (get_udatamodel() == DATAMODEL_NATIVE) {
15940d204002Sgt145670 				len = sizeof (struct timeval);
15950d204002Sgt145670 			} else {
15960d204002Sgt145670 				len = sizeof (struct timeval32);
15970d204002Sgt145670 			}
15987c478bd9Sstevel@tonic-gate 		} else {
15997c478bd9Sstevel@tonic-gate 			if (oldflg)
16007c478bd9Sstevel@tonic-gate 				continue;
16017c478bd9Sstevel@tonic-gate 			len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
16027c478bd9Sstevel@tonic-gate 		}
16037c478bd9Sstevel@tonic-gate 		/*
16040d204002Sgt145670 		 * Exclude roundup for last option to not set
16057c478bd9Sstevel@tonic-gate 		 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
16067c478bd9Sstevel@tonic-gate 		 */
16077c478bd9Sstevel@tonic-gate 		last_roundup = (t_uscalar_t)
16087c478bd9Sstevel@tonic-gate 		    (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
16097c478bd9Sstevel@tonic-gate 		    (len + (int)sizeof (struct cmsghdr)));
16107c478bd9Sstevel@tonic-gate 		cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
16117c478bd9Sstevel@tonic-gate 		    last_roundup;
16127c478bd9Sstevel@tonic-gate 	}
16137c478bd9Sstevel@tonic-gate 	cmsglen -= last_roundup;
16147c478bd9Sstevel@tonic-gate 	dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
16157c478bd9Sstevel@tonic-gate 		optlen, oldflg, cmsglen));
16167c478bd9Sstevel@tonic-gate 	return (cmsglen);
16177c478bd9Sstevel@tonic-gate }
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate /*
16207c478bd9Sstevel@tonic-gate  * Copy options from options to the control. Convert SO_FILEP to
16217c478bd9Sstevel@tonic-gate  * file descriptors.
16227c478bd9Sstevel@tonic-gate  * Returns errno or zero.
16230d204002Sgt145670  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
16240d204002Sgt145670  * allocates the space that so_opt2cmsg fills. If one changes, the other should
16250d204002Sgt145670  * also be checked for any possible impacts.
16267c478bd9Sstevel@tonic-gate  */
16277c478bd9Sstevel@tonic-gate int
16287c478bd9Sstevel@tonic-gate so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
16297c478bd9Sstevel@tonic-gate     void *control, t_uscalar_t controllen)
16307c478bd9Sstevel@tonic-gate {
16317c478bd9Sstevel@tonic-gate 	struct T_opthdr *tohp;
16327c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
16337c478bd9Sstevel@tonic-gate 	struct fdbuf *fdbuf;
16347c478bd9Sstevel@tonic-gate 	int fdbuflen;
16357c478bd9Sstevel@tonic-gate 	int error;
16360d204002Sgt145670 #if defined(DEBUG) || defined(__lint)
16370d204002Sgt145670 	struct cmsghdr *cend = (struct cmsghdr *)
16380d204002Sgt145670 	    (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
16390d204002Sgt145670 #endif
16407c478bd9Sstevel@tonic-gate 	cmsg = (struct cmsghdr *)control;
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
16457c478bd9Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
16467c478bd9Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
16477c478bd9Sstevel@tonic-gate 		dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
16487c478bd9Sstevel@tonic-gate 			tohp->level, tohp->name, tohp->len));
16497c478bd9Sstevel@tonic-gate 
16507c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
16517c478bd9Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
16527c478bd9Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
16537c478bd9Sstevel@tonic-gate 			continue;
16547c478bd9Sstevel@tonic-gate 		}
16557c478bd9Sstevel@tonic-gate 		ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
16567c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
16577c478bd9Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
16587c478bd9Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
16597c478bd9Sstevel@tonic-gate 
16607c478bd9Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
16617c478bd9Sstevel@tonic-gate 				return (EPROTO);
16627c478bd9Sstevel@tonic-gate 			if (oldflg) {
16637c478bd9Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf, control,
16647c478bd9Sstevel@tonic-gate 				    (int)controllen);
16657c478bd9Sstevel@tonic-gate 				if (error != 0)
16667c478bd9Sstevel@tonic-gate 					return (error);
16677c478bd9Sstevel@tonic-gate 				continue;
16687c478bd9Sstevel@tonic-gate 			} else {
16697c478bd9Sstevel@tonic-gate 				int fdlen;
16707c478bd9Sstevel@tonic-gate 
16717c478bd9Sstevel@tonic-gate 				fdlen = (int)fdbuf_cmsglen(
16727c478bd9Sstevel@tonic-gate 				    (int)_TPI_TOPT_DATALEN(tohp));
16737c478bd9Sstevel@tonic-gate 
16747c478bd9Sstevel@tonic-gate 				cmsg->cmsg_level = tohp->level;
16757c478bd9Sstevel@tonic-gate 				cmsg->cmsg_type = SCM_RIGHTS;
16767c478bd9Sstevel@tonic-gate 				cmsg->cmsg_len = (socklen_t)(fdlen +
16777c478bd9Sstevel@tonic-gate 					sizeof (struct cmsghdr));
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf,
16807c478bd9Sstevel@tonic-gate 						CMSG_CONTENT(cmsg), fdlen);
16817c478bd9Sstevel@tonic-gate 				if (error != 0)
16827c478bd9Sstevel@tonic-gate 					return (error);
16837c478bd9Sstevel@tonic-gate 			}
1684e4f35dbaSgt145670 		} else if (tohp->level == SOL_SOCKET &&
1685e4f35dbaSgt145670 		    tohp->name == SCM_TIMESTAMP) {
1686e4f35dbaSgt145670 			timestruc_t *timestamp;
1687e4f35dbaSgt145670 
1688e4f35dbaSgt145670 			if (oldflg)
1689e4f35dbaSgt145670 				continue;
1690e4f35dbaSgt145670 
1691e4f35dbaSgt145670 			cmsg->cmsg_level = tohp->level;
1692e4f35dbaSgt145670 			cmsg->cmsg_type = tohp->name;
1693e4f35dbaSgt145670 
1694e4f35dbaSgt145670 			timestamp =
1695e4f35dbaSgt145670 			    (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
1696e4f35dbaSgt145670 			    sizeof (intptr_t));
1697e4f35dbaSgt145670 
1698e4f35dbaSgt145670 			if (get_udatamodel() == DATAMODEL_NATIVE) {
16990d204002Sgt145670 				struct timeval tv;
1700e4f35dbaSgt145670 
1701e4f35dbaSgt145670 				cmsg->cmsg_len = sizeof (struct timeval) +
1702e4f35dbaSgt145670 				    sizeof (struct cmsghdr);
17030d204002Sgt145670 				tv.tv_sec = timestamp->tv_sec;
17040d204002Sgt145670 				tv.tv_usec = timestamp->tv_nsec /
17050d204002Sgt145670 				    (NANOSEC / MICROSEC);
17060d204002Sgt145670 				/*
17070d204002Sgt145670 				 * on LP64 systems, the struct timeval in
17080d204002Sgt145670 				 * the destination will not be 8-byte aligned,
17090d204002Sgt145670 				 * so use bcopy to avoid alignment trouble
17100d204002Sgt145670 				 */
17110d204002Sgt145670 				bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
1712e4f35dbaSgt145670 			} else {
1713e4f35dbaSgt145670 				struct timeval32 *time32;
1714e4f35dbaSgt145670 
1715e4f35dbaSgt145670 				cmsg->cmsg_len = sizeof (struct timeval32) +
1716e4f35dbaSgt145670 				    sizeof (struct cmsghdr);
1717e4f35dbaSgt145670 				time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
1718e4f35dbaSgt145670 				time32->tv_sec = (time32_t)timestamp->tv_sec;
1719e4f35dbaSgt145670 				time32->tv_usec =
1720e4f35dbaSgt145670 				    (int32_t)(timestamp->tv_nsec /
1721e4f35dbaSgt145670 				    (NANOSEC / MICROSEC));
1722e4f35dbaSgt145670 			}
1723e4f35dbaSgt145670 
17247c478bd9Sstevel@tonic-gate 		} else {
17257c478bd9Sstevel@tonic-gate 			if (oldflg)
17267c478bd9Sstevel@tonic-gate 				continue;
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 			cmsg->cmsg_level = tohp->level;
17297c478bd9Sstevel@tonic-gate 			cmsg->cmsg_type = tohp->name;
17307c478bd9Sstevel@tonic-gate 			cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) +
17317c478bd9Sstevel@tonic-gate 			    sizeof (struct cmsghdr));
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate 			/* copy content to control data part */
17347c478bd9Sstevel@tonic-gate 			bcopy(&tohp[1], CMSG_CONTENT(cmsg),
17357c478bd9Sstevel@tonic-gate 				CMSG_CONTENTLEN(cmsg));
17367c478bd9Sstevel@tonic-gate 		}
17377c478bd9Sstevel@tonic-gate 		/* move to next CMSG structure! */
17387c478bd9Sstevel@tonic-gate 		cmsg = CMSG_NEXT(cmsg);
17397c478bd9Sstevel@tonic-gate 	}
17400d204002Sgt145670 	dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
17410d204002Sgt145670 	    control, controllen, cend, cmsg));
17420d204002Sgt145670 	ASSERT(cmsg <= cend);
17437c478bd9Sstevel@tonic-gate 	return (0);
17447c478bd9Sstevel@tonic-gate }
17457c478bd9Sstevel@tonic-gate 
17467c478bd9Sstevel@tonic-gate /*
17477c478bd9Sstevel@tonic-gate  * Extract the SO_SRCADDR option value if present.
17487c478bd9Sstevel@tonic-gate  */
17497c478bd9Sstevel@tonic-gate void
17507c478bd9Sstevel@tonic-gate so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
17517c478bd9Sstevel@tonic-gate     t_uscalar_t *srclenp)
17527c478bd9Sstevel@tonic-gate {
17537c478bd9Sstevel@tonic-gate 	struct T_opthdr		*tohp;
17547c478bd9Sstevel@tonic-gate 
17557c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
17567c478bd9Sstevel@tonic-gate 
17577c478bd9Sstevel@tonic-gate 	ASSERT(srcp != NULL && srclenp != NULL);
17587c478bd9Sstevel@tonic-gate 	*srcp = NULL;
17597c478bd9Sstevel@tonic-gate 	*srclenp = 0;
17607c478bd9Sstevel@tonic-gate 
17617c478bd9Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
17627c478bd9Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
17637c478bd9Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
17647c478bd9Sstevel@tonic-gate 		dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
17657c478bd9Sstevel@tonic-gate 			tohp->level, tohp->name, tohp->len));
17667c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
17677c478bd9Sstevel@tonic-gate 		    tohp->name == SO_SRCADDR) {
17687c478bd9Sstevel@tonic-gate 			*srcp = _TPI_TOPT_DATA(tohp);
17697c478bd9Sstevel@tonic-gate 			*srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
17707c478bd9Sstevel@tonic-gate 		}
17717c478bd9Sstevel@tonic-gate 	}
17727c478bd9Sstevel@tonic-gate }
17737c478bd9Sstevel@tonic-gate 
17747c478bd9Sstevel@tonic-gate /*
17757c478bd9Sstevel@tonic-gate  * Verify if the SO_UNIX_CLOSE option is present.
17767c478bd9Sstevel@tonic-gate  */
17777c478bd9Sstevel@tonic-gate int
17787c478bd9Sstevel@tonic-gate so_getopt_unix_close(void *opt, t_uscalar_t optlen)
17797c478bd9Sstevel@tonic-gate {
17807c478bd9Sstevel@tonic-gate 	struct T_opthdr		*tohp;
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
17837c478bd9Sstevel@tonic-gate 
17847c478bd9Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
17857c478bd9Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
17867c478bd9Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
17877c478bd9Sstevel@tonic-gate 		dprint(1,
17887c478bd9Sstevel@tonic-gate 			("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
17897c478bd9Sstevel@tonic-gate 			tohp->level, tohp->name, tohp->len));
17907c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
17917c478bd9Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)
17927c478bd9Sstevel@tonic-gate 			return (1);
17937c478bd9Sstevel@tonic-gate 	}
17947c478bd9Sstevel@tonic-gate 	return (0);
17957c478bd9Sstevel@tonic-gate }
17967c478bd9Sstevel@tonic-gate 
17977c478bd9Sstevel@tonic-gate /*
17987c478bd9Sstevel@tonic-gate  * Allocate an M_PROTO message.
17997c478bd9Sstevel@tonic-gate  *
18007c478bd9Sstevel@tonic-gate  * If allocation fails the behavior depends on sleepflg:
18017c478bd9Sstevel@tonic-gate  *	_ALLOC_NOSLEEP	fail immediately
18027c478bd9Sstevel@tonic-gate  *	_ALLOC_INTR	sleep for memory until a signal is caught
18037c478bd9Sstevel@tonic-gate  *	_ALLOC_SLEEP	sleep forever. Don't return NULL.
18047c478bd9Sstevel@tonic-gate  */
18057c478bd9Sstevel@tonic-gate mblk_t *
18067c478bd9Sstevel@tonic-gate soallocproto(size_t size, int sleepflg)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	/* Round up size for reuse */
18117c478bd9Sstevel@tonic-gate 	size = MAX(size, 64);
18127c478bd9Sstevel@tonic-gate 	mp = allocb(size, BPRI_MED);
18137c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
18147c478bd9Sstevel@tonic-gate 		int error;	/* Dummy - error not returned to caller */
18157c478bd9Sstevel@tonic-gate 
18167c478bd9Sstevel@tonic-gate 		switch (sleepflg) {
18177c478bd9Sstevel@tonic-gate 		case _ALLOC_SLEEP:
18187c478bd9Sstevel@tonic-gate 			mp = allocb_wait(size, BPRI_MED, STR_NOSIG, &error);
18197c478bd9Sstevel@tonic-gate 			ASSERT(mp);
18207c478bd9Sstevel@tonic-gate 			break;
18217c478bd9Sstevel@tonic-gate 		case _ALLOC_INTR:
18227c478bd9Sstevel@tonic-gate 			mp = allocb_wait(size, BPRI_MED, 0, &error);
18237c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
18247c478bd9Sstevel@tonic-gate 				/* Caught signal while sleeping for memory */
18257c478bd9Sstevel@tonic-gate 				eprintline(ENOBUFS);
18267c478bd9Sstevel@tonic-gate 				return (NULL);
18277c478bd9Sstevel@tonic-gate 			}
18287c478bd9Sstevel@tonic-gate 			break;
18297c478bd9Sstevel@tonic-gate 		case _ALLOC_NOSLEEP:
18307c478bd9Sstevel@tonic-gate 		default:
18317c478bd9Sstevel@tonic-gate 			eprintline(ENOBUFS);
18327c478bd9Sstevel@tonic-gate 			return (NULL);
18337c478bd9Sstevel@tonic-gate 		}
18347c478bd9Sstevel@tonic-gate 	}
18357c478bd9Sstevel@tonic-gate 	DB_TYPE(mp) = M_PROTO;
18367c478bd9Sstevel@tonic-gate 	return (mp);
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate /*
18407c478bd9Sstevel@tonic-gate  * Allocate an M_PROTO message with a single component.
18417c478bd9Sstevel@tonic-gate  * len is the length of buf. size is the amount to allocate.
18427c478bd9Sstevel@tonic-gate  *
18437c478bd9Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
18447c478bd9Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
18457c478bd9Sstevel@tonic-gate  */
18467c478bd9Sstevel@tonic-gate mblk_t *
18477c478bd9Sstevel@tonic-gate soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg)
18487c478bd9Sstevel@tonic-gate {
18497c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate 	if (size == 0)
18527c478bd9Sstevel@tonic-gate 		size = len;
18537c478bd9Sstevel@tonic-gate 
18547c478bd9Sstevel@tonic-gate 	ASSERT(size >= len);
18557c478bd9Sstevel@tonic-gate 	/* Round up size for reuse */
18567c478bd9Sstevel@tonic-gate 	size = MAX(size, 64);
18577c478bd9Sstevel@tonic-gate 	mp = soallocproto(size, sleepflg);
18587c478bd9Sstevel@tonic-gate 	if (mp == NULL)
18597c478bd9Sstevel@tonic-gate 		return (NULL);
18607c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
18617c478bd9Sstevel@tonic-gate 	if (len != 0) {
18627c478bd9Sstevel@tonic-gate 		if (buf != NULL)
18637c478bd9Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
18647c478bd9Sstevel@tonic-gate 		else
18657c478bd9Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
18667c478bd9Sstevel@tonic-gate 		mp->b_wptr += len;
18677c478bd9Sstevel@tonic-gate 	}
18687c478bd9Sstevel@tonic-gate 	return (mp);
18697c478bd9Sstevel@tonic-gate }
18707c478bd9Sstevel@tonic-gate 
18717c478bd9Sstevel@tonic-gate /*
18727c478bd9Sstevel@tonic-gate  * Append buf/len to mp.
18737c478bd9Sstevel@tonic-gate  * The caller has to ensure that there is enough room in the mblk.
18747c478bd9Sstevel@tonic-gate  *
18757c478bd9Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
18767c478bd9Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
18777c478bd9Sstevel@tonic-gate  */
18787c478bd9Sstevel@tonic-gate void
18797c478bd9Sstevel@tonic-gate soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
18807c478bd9Sstevel@tonic-gate {
18817c478bd9Sstevel@tonic-gate 	ASSERT(mp);
18827c478bd9Sstevel@tonic-gate 
18837c478bd9Sstevel@tonic-gate 	if (len != 0) {
18847c478bd9Sstevel@tonic-gate 		/* Assert for room left */
18857c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
18867c478bd9Sstevel@tonic-gate 		if (buf != NULL)
18877c478bd9Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
18887c478bd9Sstevel@tonic-gate 		else
18897c478bd9Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
18907c478bd9Sstevel@tonic-gate 	}
18917c478bd9Sstevel@tonic-gate 	mp->b_wptr += len;
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate 
18947c478bd9Sstevel@tonic-gate /*
18957c478bd9Sstevel@tonic-gate  * Create a message using two kernel buffers.
18967c478bd9Sstevel@tonic-gate  * If size is set that will determine the allocation size (e.g. for future
18977c478bd9Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
18987c478bd9Sstevel@tonic-gate  * lengths.
18997c478bd9Sstevel@tonic-gate  */
19007c478bd9Sstevel@tonic-gate mblk_t *
19017c478bd9Sstevel@tonic-gate soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
19027c478bd9Sstevel@tonic-gate     ssize_t size, int sleepflg)
19037c478bd9Sstevel@tonic-gate {
19047c478bd9Sstevel@tonic-gate 	mblk_t *mp;
19057c478bd9Sstevel@tonic-gate 
19067c478bd9Sstevel@tonic-gate 	if (size == 0)
19077c478bd9Sstevel@tonic-gate 		size = len1 + len2;
19087c478bd9Sstevel@tonic-gate 	ASSERT(size >= len1 + len2);
19097c478bd9Sstevel@tonic-gate 
19107c478bd9Sstevel@tonic-gate 	mp = soallocproto1(buf1, len1, size, sleepflg);
19117c478bd9Sstevel@tonic-gate 	if (mp)
19127c478bd9Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
19137c478bd9Sstevel@tonic-gate 	return (mp);
19147c478bd9Sstevel@tonic-gate }
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate /*
19177c478bd9Sstevel@tonic-gate  * Create a message using three kernel buffers.
19187c478bd9Sstevel@tonic-gate  * If size is set that will determine the allocation size (for future
19197c478bd9Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
19207c478bd9Sstevel@tonic-gate  * lengths.
19217c478bd9Sstevel@tonic-gate  */
19227c478bd9Sstevel@tonic-gate mblk_t *
19237c478bd9Sstevel@tonic-gate soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
19247c478bd9Sstevel@tonic-gate     const void *buf3, ssize_t len3, ssize_t size, int sleepflg)
19257c478bd9Sstevel@tonic-gate {
19267c478bd9Sstevel@tonic-gate 	mblk_t *mp;
19277c478bd9Sstevel@tonic-gate 
19287c478bd9Sstevel@tonic-gate 	if (size == 0)
19297c478bd9Sstevel@tonic-gate 		size = len1 + len2 +len3;
19307c478bd9Sstevel@tonic-gate 	ASSERT(size >= len1 + len2 + len3);
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate 	mp = soallocproto1(buf1, len1, size, sleepflg);
19337c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
19347c478bd9Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
19357c478bd9Sstevel@tonic-gate 		soappendmsg(mp, buf3, len3);
19367c478bd9Sstevel@tonic-gate 	}
19377c478bd9Sstevel@tonic-gate 	return (mp);
19387c478bd9Sstevel@tonic-gate }
19397c478bd9Sstevel@tonic-gate 
19407c478bd9Sstevel@tonic-gate #ifdef DEBUG
19417c478bd9Sstevel@tonic-gate char *
19427c478bd9Sstevel@tonic-gate pr_state(uint_t state, uint_t mode)
19437c478bd9Sstevel@tonic-gate {
19447c478bd9Sstevel@tonic-gate 	static char buf[1024];
19457c478bd9Sstevel@tonic-gate 
19467c478bd9Sstevel@tonic-gate 	buf[0] = 0;
19477c478bd9Sstevel@tonic-gate 	if (state & SS_ISCONNECTED)
19487c478bd9Sstevel@tonic-gate 		strcat(buf, "ISCONNECTED ");
19497c478bd9Sstevel@tonic-gate 	if (state & SS_ISCONNECTING)
19507c478bd9Sstevel@tonic-gate 		strcat(buf, "ISCONNECTING ");
19517c478bd9Sstevel@tonic-gate 	if (state & SS_ISDISCONNECTING)
19527c478bd9Sstevel@tonic-gate 		strcat(buf, "ISDISCONNECTING ");
19537c478bd9Sstevel@tonic-gate 	if (state & SS_CANTSENDMORE)
19547c478bd9Sstevel@tonic-gate 		strcat(buf, "CANTSENDMORE ");
19557c478bd9Sstevel@tonic-gate 
19567c478bd9Sstevel@tonic-gate 	if (state & SS_CANTRCVMORE)
19577c478bd9Sstevel@tonic-gate 		strcat(buf, "CANTRCVMORE ");
19587c478bd9Sstevel@tonic-gate 	if (state & SS_ISBOUND)
19597c478bd9Sstevel@tonic-gate 		strcat(buf, "ISBOUND ");
19607c478bd9Sstevel@tonic-gate 	if (state & SS_NDELAY)
19617c478bd9Sstevel@tonic-gate 		strcat(buf, "NDELAY ");
19627c478bd9Sstevel@tonic-gate 	if (state & SS_NONBLOCK)
19637c478bd9Sstevel@tonic-gate 		strcat(buf, "NONBLOCK ");
19647c478bd9Sstevel@tonic-gate 
19657c478bd9Sstevel@tonic-gate 	if (state & SS_ASYNC)
19667c478bd9Sstevel@tonic-gate 		strcat(buf, "ASYNC ");
19677c478bd9Sstevel@tonic-gate 	if (state & SS_ACCEPTCONN)
19687c478bd9Sstevel@tonic-gate 		strcat(buf, "ACCEPTCONN ");
19697c478bd9Sstevel@tonic-gate 	if (state & SS_HASCONNIND)
19707c478bd9Sstevel@tonic-gate 		strcat(buf, "HASCONNIND ");
19717c478bd9Sstevel@tonic-gate 	if (state & SS_SAVEDEOR)
19727c478bd9Sstevel@tonic-gate 		strcat(buf, "SAVEDEOR ");
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	if (state & SS_RCVATMARK)
19757c478bd9Sstevel@tonic-gate 		strcat(buf, "RCVATMARK ");
19767c478bd9Sstevel@tonic-gate 	if (state & SS_OOBPEND)
19777c478bd9Sstevel@tonic-gate 		strcat(buf, "OOBPEND ");
19787c478bd9Sstevel@tonic-gate 	if (state & SS_HAVEOOBDATA)
19797c478bd9Sstevel@tonic-gate 		strcat(buf, "HAVEOOBDATA ");
19807c478bd9Sstevel@tonic-gate 	if (state & SS_HADOOBDATA)
19817c478bd9Sstevel@tonic-gate 		strcat(buf, "HADOOBDATA ");
19827c478bd9Sstevel@tonic-gate 
19837c478bd9Sstevel@tonic-gate 	if (state & SS_FADDR_NOXLATE)
19847c478bd9Sstevel@tonic-gate 		strcat(buf, "FADDR_NOXLATE ");
19857c478bd9Sstevel@tonic-gate 
19867c478bd9Sstevel@tonic-gate 	if (mode & SM_PRIV)
19877c478bd9Sstevel@tonic-gate 		strcat(buf, "PRIV ");
19887c478bd9Sstevel@tonic-gate 	if (mode & SM_ATOMIC)
19897c478bd9Sstevel@tonic-gate 		strcat(buf, "ATOMIC ");
19907c478bd9Sstevel@tonic-gate 	if (mode & SM_ADDR)
19917c478bd9Sstevel@tonic-gate 		strcat(buf, "ADDR ");
19927c478bd9Sstevel@tonic-gate 	if (mode & SM_CONNREQUIRED)
19937c478bd9Sstevel@tonic-gate 		strcat(buf, "CONNREQUIRED ");
19947c478bd9Sstevel@tonic-gate 
19957c478bd9Sstevel@tonic-gate 	if (mode & SM_FDPASSING)
19967c478bd9Sstevel@tonic-gate 		strcat(buf, "FDPASSING ");
19977c478bd9Sstevel@tonic-gate 	if (mode & SM_EXDATA)
19987c478bd9Sstevel@tonic-gate 		strcat(buf, "EXDATA ");
19997c478bd9Sstevel@tonic-gate 	if (mode & SM_OPTDATA)
20007c478bd9Sstevel@tonic-gate 		strcat(buf, "OPTDATA ");
20017c478bd9Sstevel@tonic-gate 	if (mode & SM_BYTESTREAM)
20027c478bd9Sstevel@tonic-gate 		strcat(buf, "BYTESTREAM ");
20037c478bd9Sstevel@tonic-gate 	return (buf);
20047c478bd9Sstevel@tonic-gate }
20057c478bd9Sstevel@tonic-gate 
20067c478bd9Sstevel@tonic-gate char *
20077c478bd9Sstevel@tonic-gate pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
20087c478bd9Sstevel@tonic-gate {
20097c478bd9Sstevel@tonic-gate 	static char buf[1024];
20107c478bd9Sstevel@tonic-gate 
20117c478bd9Sstevel@tonic-gate 	if (addr == NULL || addrlen == 0) {
20127c478bd9Sstevel@tonic-gate 		sprintf(buf, "(len %d) %p", addrlen, addr);
20137c478bd9Sstevel@tonic-gate 		return (buf);
20147c478bd9Sstevel@tonic-gate 	}
20157c478bd9Sstevel@tonic-gate 	switch (family) {
20167c478bd9Sstevel@tonic-gate 	case AF_INET: {
20177c478bd9Sstevel@tonic-gate 		struct sockaddr_in sin;
20187c478bd9Sstevel@tonic-gate 
20197c478bd9Sstevel@tonic-gate 		bcopy(addr, &sin, sizeof (sin));
20207c478bd9Sstevel@tonic-gate 
20217c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "(len %d) %x/%d",
20227c478bd9Sstevel@tonic-gate 			addrlen, ntohl(sin.sin_addr.s_addr),
20237c478bd9Sstevel@tonic-gate 			ntohs(sin.sin_port));
20247c478bd9Sstevel@tonic-gate 		break;
20257c478bd9Sstevel@tonic-gate 	}
20267c478bd9Sstevel@tonic-gate 	case AF_INET6: {
20277c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 sin6;
20287c478bd9Sstevel@tonic-gate 		uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
20297c478bd9Sstevel@tonic-gate 
20307c478bd9Sstevel@tonic-gate 		bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
20317c478bd9Sstevel@tonic-gate 		sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
20327c478bd9Sstevel@tonic-gate 		    addrlen,
20337c478bd9Sstevel@tonic-gate 		    ntohs(piece[0]), ntohs(piece[1]),
20347c478bd9Sstevel@tonic-gate 		    ntohs(piece[2]), ntohs(piece[3]),
20357c478bd9Sstevel@tonic-gate 		    ntohs(piece[4]), ntohs(piece[5]),
20367c478bd9Sstevel@tonic-gate 		    ntohs(piece[6]), ntohs(piece[7]),
20377c478bd9Sstevel@tonic-gate 		    ntohs(sin6.sin6_port));
20387c478bd9Sstevel@tonic-gate 		break;
20397c478bd9Sstevel@tonic-gate 	}
20407c478bd9Sstevel@tonic-gate 	case AF_UNIX: {
20417c478bd9Sstevel@tonic-gate 		struct sockaddr_un *soun = (struct sockaddr_un *)addr;
20427c478bd9Sstevel@tonic-gate 
20437c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "(len %d) %s",
20447c478bd9Sstevel@tonic-gate 			addrlen,
20457c478bd9Sstevel@tonic-gate 			(soun == NULL) ? "(none)" : soun->sun_path);
20467c478bd9Sstevel@tonic-gate 		break;
20477c478bd9Sstevel@tonic-gate 	}
20487c478bd9Sstevel@tonic-gate 	default:
20497c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "(unknown af %d)", family);
20507c478bd9Sstevel@tonic-gate 		break;
20517c478bd9Sstevel@tonic-gate 	}
20527c478bd9Sstevel@tonic-gate 	return (buf);
20537c478bd9Sstevel@tonic-gate }
20547c478bd9Sstevel@tonic-gate 
20557c478bd9Sstevel@tonic-gate /* The logical equivalence operator (a if-and-only-if b) */
20567c478bd9Sstevel@tonic-gate #define	EQUIV(a, b)	(((a) && (b)) || (!(a) && (!(b))))
20577c478bd9Sstevel@tonic-gate 
20587c478bd9Sstevel@tonic-gate /*
20597c478bd9Sstevel@tonic-gate  * Verify limitations and invariants on oob state.
20607c478bd9Sstevel@tonic-gate  * Return 1 if OK, otherwise 0 so that it can be used as
20617c478bd9Sstevel@tonic-gate  *	ASSERT(verify_oobstate(so));
20627c478bd9Sstevel@tonic-gate  */
20637c478bd9Sstevel@tonic-gate int
20647c478bd9Sstevel@tonic-gate so_verify_oobstate(struct sonode *so)
20657c478bd9Sstevel@tonic-gate {
20667c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
20677c478bd9Sstevel@tonic-gate 
20687c478bd9Sstevel@tonic-gate 	/*
20697c478bd9Sstevel@tonic-gate 	 * The possible state combinations are:
20707c478bd9Sstevel@tonic-gate 	 *	0
20717c478bd9Sstevel@tonic-gate 	 *	SS_OOBPEND
20727c478bd9Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HAVEOOBDATA
20737c478bd9Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HADOOBDATA
20747c478bd9Sstevel@tonic-gate 	 *	SS_HADOOBDATA
20757c478bd9Sstevel@tonic-gate 	 */
20767c478bd9Sstevel@tonic-gate 	switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
20777c478bd9Sstevel@tonic-gate 	case 0:
20787c478bd9Sstevel@tonic-gate 	case SS_OOBPEND:
20797c478bd9Sstevel@tonic-gate 	case SS_OOBPEND|SS_HAVEOOBDATA:
20807c478bd9Sstevel@tonic-gate 	case SS_OOBPEND|SS_HADOOBDATA:
20817c478bd9Sstevel@tonic-gate 	case SS_HADOOBDATA:
20827c478bd9Sstevel@tonic-gate 		break;
20837c478bd9Sstevel@tonic-gate 	default:
20847c478bd9Sstevel@tonic-gate 		printf("Bad oob state 1 (%p): counts %d/%d state %s\n",
20857c478bd9Sstevel@tonic-gate 			so, so->so_oobsigcnt,
20867c478bd9Sstevel@tonic-gate 			so->so_oobcnt, pr_state(so->so_state, so->so_mode));
20877c478bd9Sstevel@tonic-gate 		return (0);
20887c478bd9Sstevel@tonic-gate 	}
20897c478bd9Sstevel@tonic-gate 
20907c478bd9Sstevel@tonic-gate 	/* SS_RCVATMARK should only be set when SS_OOBPEND is set */
20917c478bd9Sstevel@tonic-gate 	if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
20927c478bd9Sstevel@tonic-gate 		printf("Bad oob state 2 (%p): counts %d/%d state %s\n",
20937c478bd9Sstevel@tonic-gate 			so, so->so_oobsigcnt,
20947c478bd9Sstevel@tonic-gate 			so->so_oobcnt, pr_state(so->so_state, so->so_mode));
20957c478bd9Sstevel@tonic-gate 		return (0);
20967c478bd9Sstevel@tonic-gate 	}
20977c478bd9Sstevel@tonic-gate 
20987c478bd9Sstevel@tonic-gate 	/*
20997c478bd9Sstevel@tonic-gate 	 * (so_oobsigcnt != 0 or SS_RCVATMARK) iff SS_OOBPEND
21007c478bd9Sstevel@tonic-gate 	 */
21017c478bd9Sstevel@tonic-gate 	if (!EQUIV((so->so_oobsigcnt != 0) || (so->so_state & SS_RCVATMARK),
21027c478bd9Sstevel@tonic-gate 		so->so_state & SS_OOBPEND)) {
21037c478bd9Sstevel@tonic-gate 		printf("Bad oob state 3 (%p): counts %d/%d state %s\n",
21047c478bd9Sstevel@tonic-gate 			so, so->so_oobsigcnt,
21057c478bd9Sstevel@tonic-gate 			so->so_oobcnt, pr_state(so->so_state, so->so_mode));
21067c478bd9Sstevel@tonic-gate 		return (0);
21077c478bd9Sstevel@tonic-gate 	}
21087c478bd9Sstevel@tonic-gate 
21097c478bd9Sstevel@tonic-gate 	/*
21107c478bd9Sstevel@tonic-gate 	 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
21117c478bd9Sstevel@tonic-gate 	 */
21127c478bd9Sstevel@tonic-gate 	if (!(so->so_options & SO_OOBINLINE) &&
21137c478bd9Sstevel@tonic-gate 	    !EQUIV(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
21147c478bd9Sstevel@tonic-gate 		printf("Bad oob state 4 (%p): counts %d/%d state %s\n",
21157c478bd9Sstevel@tonic-gate 			so, so->so_oobsigcnt,
21167c478bd9Sstevel@tonic-gate 			so->so_oobcnt, pr_state(so->so_state, so->so_mode));
21177c478bd9Sstevel@tonic-gate 		return (0);
21187c478bd9Sstevel@tonic-gate 	}
21197c478bd9Sstevel@tonic-gate 	if (so->so_oobsigcnt < so->so_oobcnt) {
21207c478bd9Sstevel@tonic-gate 		printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
21217c478bd9Sstevel@tonic-gate 			so, so->so_oobsigcnt,
21227c478bd9Sstevel@tonic-gate 			so->so_oobcnt, pr_state(so->so_state, so->so_mode));
21237c478bd9Sstevel@tonic-gate 		return (0);
21247c478bd9Sstevel@tonic-gate 	}
21257c478bd9Sstevel@tonic-gate 	return (1);
21267c478bd9Sstevel@tonic-gate }
21277c478bd9Sstevel@tonic-gate #undef	EQUIV
21287c478bd9Sstevel@tonic-gate 
21297c478bd9Sstevel@tonic-gate #endif /* DEBUG */
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate /* initialize sockfs zone specific kstat related items			*/
21327c478bd9Sstevel@tonic-gate void *
21337c478bd9Sstevel@tonic-gate sock_kstat_init(zoneid_t zoneid)
21347c478bd9Sstevel@tonic-gate {
21357c478bd9Sstevel@tonic-gate 	kstat_t	*ksp;
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate 	ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
21387c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
21397c478bd9Sstevel@tonic-gate 
21407c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
21417c478bd9Sstevel@tonic-gate 		ksp->ks_update = sockfs_update;
21427c478bd9Sstevel@tonic-gate 		ksp->ks_snapshot = sockfs_snapshot;
21437c478bd9Sstevel@tonic-gate 		ksp->ks_lock = &socklist.sl_lock;
21447c478bd9Sstevel@tonic-gate 		ksp->ks_private = (void *)(uintptr_t)zoneid;
21457c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
21467c478bd9Sstevel@tonic-gate 	}
21477c478bd9Sstevel@tonic-gate 
21487c478bd9Sstevel@tonic-gate 	return (ksp);
21497c478bd9Sstevel@tonic-gate }
21507c478bd9Sstevel@tonic-gate 
21517c478bd9Sstevel@tonic-gate /* tear down sockfs zone specific kstat related items			*/
21527c478bd9Sstevel@tonic-gate /*ARGSUSED*/
21537c478bd9Sstevel@tonic-gate void
21547c478bd9Sstevel@tonic-gate sock_kstat_fini(zoneid_t zoneid, void *arg)
21557c478bd9Sstevel@tonic-gate {
21567c478bd9Sstevel@tonic-gate 	kstat_t *ksp = (kstat_t *)arg;
21577c478bd9Sstevel@tonic-gate 
21587c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
21597c478bd9Sstevel@tonic-gate 		ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
21607c478bd9Sstevel@tonic-gate 		kstat_delete(ksp);
21617c478bd9Sstevel@tonic-gate 	}
21627c478bd9Sstevel@tonic-gate }
21637c478bd9Sstevel@tonic-gate 
21647c478bd9Sstevel@tonic-gate /*
21657c478bd9Sstevel@tonic-gate  * Zones:
21667c478bd9Sstevel@tonic-gate  * Note that nactive is going to be different for each zone.
21677c478bd9Sstevel@tonic-gate  * This means we require kstat to call sockfs_update and then sockfs_snapshot
21687c478bd9Sstevel@tonic-gate  * for the same zone, or sockfs_snapshot will be taken into the wrong size
21697c478bd9Sstevel@tonic-gate  * buffer. This is safe, but if the buffer is too small, user will not be
21707c478bd9Sstevel@tonic-gate  * given details of all sockets. However, as this kstat has a ks_lock, kstat
21717c478bd9Sstevel@tonic-gate  * driver will keep it locked between the update and the snapshot, so no
21727c478bd9Sstevel@tonic-gate  * other process (zone) can currently get inbetween resulting in a wrong size
21737c478bd9Sstevel@tonic-gate  * buffer allocation.
21747c478bd9Sstevel@tonic-gate  */
21757c478bd9Sstevel@tonic-gate static int
21767c478bd9Sstevel@tonic-gate sockfs_update(kstat_t *ksp, int rw)
21777c478bd9Sstevel@tonic-gate {
21787c478bd9Sstevel@tonic-gate 	uint_t	nactive = 0;		/* # of active AF_UNIX sockets	*/
21797c478bd9Sstevel@tonic-gate 	struct sonode	*so;		/* current sonode on socklist	*/
21807c478bd9Sstevel@tonic-gate 	zoneid_t	myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
21817c478bd9Sstevel@tonic-gate 
21827c478bd9Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
21837c478bd9Sstevel@tonic-gate 
21847c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
21857c478bd9Sstevel@tonic-gate 		return (EACCES);
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	for (so = socklist.sl_list; so != NULL; so = so->so_next) {
21897c478bd9Sstevel@tonic-gate 		if (so->so_accessvp != NULL && so->so_zoneid == myzoneid) {
21907c478bd9Sstevel@tonic-gate 			nactive++;
21917c478bd9Sstevel@tonic-gate 		}
21927c478bd9Sstevel@tonic-gate 	}
21937c478bd9Sstevel@tonic-gate 	ksp->ks_ndata = nactive;
21947c478bd9Sstevel@tonic-gate 	ksp->ks_data_size = nactive * sizeof (struct k_sockinfo);
21957c478bd9Sstevel@tonic-gate 
21967c478bd9Sstevel@tonic-gate 	return (0);
21977c478bd9Sstevel@tonic-gate }
21987c478bd9Sstevel@tonic-gate 
21997c478bd9Sstevel@tonic-gate static int
22007c478bd9Sstevel@tonic-gate sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
22017c478bd9Sstevel@tonic-gate {
22027c478bd9Sstevel@tonic-gate 	int			ns;	/* # of sonodes we've copied	*/
22037c478bd9Sstevel@tonic-gate 	struct sonode		*so;	/* current sonode on socklist	*/
22047c478bd9Sstevel@tonic-gate 	struct k_sockinfo	*pksi;	/* where we put sockinfo data	*/
22057c478bd9Sstevel@tonic-gate 	t_uscalar_t		sn_len;	/* soa_len			*/
22067c478bd9Sstevel@tonic-gate 	zoneid_t		myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
22077c478bd9Sstevel@tonic-gate 
22087c478bd9Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
22097c478bd9Sstevel@tonic-gate 
22107c478bd9Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
22117c478bd9Sstevel@tonic-gate 
22127c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
22137c478bd9Sstevel@tonic-gate 		return (EACCES);
22147c478bd9Sstevel@tonic-gate 	}
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate 	/*
22177c478bd9Sstevel@tonic-gate 	 * for each sonode on the socklist, we massage the important
22187c478bd9Sstevel@tonic-gate 	 * info into buf, in k_sockinfo format.
22197c478bd9Sstevel@tonic-gate 	 */
22207c478bd9Sstevel@tonic-gate 	pksi = (struct k_sockinfo *)buf;
22217c478bd9Sstevel@tonic-gate 	for (ns = 0, so = socklist.sl_list; so != NULL; so = so->so_next) {
22227c478bd9Sstevel@tonic-gate 		/* only stuff active sonodes and the same zone:		*/
22237c478bd9Sstevel@tonic-gate 		if (so->so_accessvp == NULL || so->so_zoneid != myzoneid) {
22247c478bd9Sstevel@tonic-gate 			continue;
22257c478bd9Sstevel@tonic-gate 		}
22267c478bd9Sstevel@tonic-gate 
22277c478bd9Sstevel@tonic-gate 		/*
22287c478bd9Sstevel@tonic-gate 		 * If the sonode was activated between the update and the
22297c478bd9Sstevel@tonic-gate 		 * snapshot, we're done - as this is only a snapshot.
22307c478bd9Sstevel@tonic-gate 		 */
22317c478bd9Sstevel@tonic-gate 		if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) {
22327c478bd9Sstevel@tonic-gate 			break;
22337c478bd9Sstevel@tonic-gate 		}
22347c478bd9Sstevel@tonic-gate 
22357c478bd9Sstevel@tonic-gate 		/* copy important info into buf:			*/
22367c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_size = sizeof (struct k_sockinfo);
22377c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_family = so->so_family;
22387c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_type = so->so_type;
22397c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_flag = so->so_flag;
22407c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_state = so->so_state;
22417c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_serv_type = so->so_serv_type;
22427c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_ux_laddr_sou_magic = so->so_ux_laddr.soua_magic;
22437c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_ux_faddr_sou_magic = so->so_ux_faddr.soua_magic;
22447c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_laddr_soa_len = so->so_laddr.soa_len;
22457c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_faddr_soa_len = so->so_faddr.soa_len;
22467c478bd9Sstevel@tonic-gate 		pksi->ks_si.si_szoneid = so->so_zoneid;
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
22497c478bd9Sstevel@tonic-gate 
22507c478bd9Sstevel@tonic-gate 		if (so->so_laddr_sa != NULL) {
22517c478bd9Sstevel@tonic-gate 			ASSERT(so->so_laddr_sa->sa_data != NULL);
22527c478bd9Sstevel@tonic-gate 			sn_len = so->so_laddr_len;
22537c478bd9Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
22547c478bd9Sstevel@tonic-gate 			    sizeof (pksi->ks_si.si_laddr_sun_path));
22557c478bd9Sstevel@tonic-gate 
22567c478bd9Sstevel@tonic-gate 			pksi->ks_si.si_laddr_family =
22577c478bd9Sstevel@tonic-gate 				so->so_laddr_sa->sa_family;
22587c478bd9Sstevel@tonic-gate 			if (sn_len != 0) {
22597c478bd9Sstevel@tonic-gate 				/* AF_UNIX socket names are NULL terminated */
22607c478bd9Sstevel@tonic-gate 				(void) strncpy(pksi->ks_si.si_laddr_sun_path,
22617c478bd9Sstevel@tonic-gate 				    so->so_laddr_sa->sa_data,
22627c478bd9Sstevel@tonic-gate 				    sizeof (pksi->ks_si.si_laddr_sun_path));
22637c478bd9Sstevel@tonic-gate 				sn_len = strlen(pksi->ks_si.si_laddr_sun_path);
22647c478bd9Sstevel@tonic-gate 			}
22657c478bd9Sstevel@tonic-gate 			pksi->ks_si.si_laddr_sun_path[sn_len] = 0;
22667c478bd9Sstevel@tonic-gate 		}
22677c478bd9Sstevel@tonic-gate 
22687c478bd9Sstevel@tonic-gate 		if (so->so_faddr_sa != NULL) {
22697c478bd9Sstevel@tonic-gate 			ASSERT(so->so_faddr_sa->sa_data != NULL);
22707c478bd9Sstevel@tonic-gate 			sn_len = so->so_faddr_len;
22717c478bd9Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
22727c478bd9Sstevel@tonic-gate 			    sizeof (pksi->ks_si.si_faddr_sun_path));
22737c478bd9Sstevel@tonic-gate 
22747c478bd9Sstevel@tonic-gate 			pksi->ks_si.si_faddr_family =
22757c478bd9Sstevel@tonic-gate 			    so->so_faddr_sa->sa_family;
22767c478bd9Sstevel@tonic-gate 			if (sn_len != 0) {
22777c478bd9Sstevel@tonic-gate 				(void) strncpy(pksi->ks_si.si_faddr_sun_path,
22787c478bd9Sstevel@tonic-gate 				    so->so_faddr_sa->sa_data,
22797c478bd9Sstevel@tonic-gate 				    sizeof (pksi->ks_si.si_faddr_sun_path));
22807c478bd9Sstevel@tonic-gate 				sn_len = strlen(pksi->ks_si.si_faddr_sun_path);
22817c478bd9Sstevel@tonic-gate 			}
22827c478bd9Sstevel@tonic-gate 			pksi->ks_si.si_faddr_sun_path[sn_len] = 0;
22837c478bd9Sstevel@tonic-gate 		}
22847c478bd9Sstevel@tonic-gate 
22857c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
22867c478bd9Sstevel@tonic-gate 
22877c478bd9Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[0], "%p", (void *)so);
22887c478bd9Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[1], "%p",
22897c478bd9Sstevel@tonic-gate 		    (void *)so->so_ux_laddr.soua_vp);
22907c478bd9Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[2], "%p",
22917c478bd9Sstevel@tonic-gate 		    (void *)so->so_ux_faddr.soua_vp);
22927c478bd9Sstevel@tonic-gate 
22937c478bd9Sstevel@tonic-gate 		ns++;
22947c478bd9Sstevel@tonic-gate 		pksi++;
22957c478bd9Sstevel@tonic-gate 	}
22967c478bd9Sstevel@tonic-gate 
22977c478bd9Sstevel@tonic-gate 	ksp->ks_ndata = ns;
22987c478bd9Sstevel@tonic-gate 	return (0);
22997c478bd9Sstevel@tonic-gate }
23007c478bd9Sstevel@tonic-gate 
23017c478bd9Sstevel@tonic-gate ssize_t
23027c478bd9Sstevel@tonic-gate soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
23037c478bd9Sstevel@tonic-gate {
23047c478bd9Sstevel@tonic-gate 	struct uio auio;
23057c478bd9Sstevel@tonic-gate 	struct iovec aiov[MSG_MAXIOVLEN];
23067c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
23077c478bd9Sstevel@tonic-gate 	int ioflag, rwflag;
23087c478bd9Sstevel@tonic-gate 	ssize_t cnt;
23097c478bd9Sstevel@tonic-gate 	int error = 0;
23107c478bd9Sstevel@tonic-gate 	int iovcnt = 0;
23117c478bd9Sstevel@tonic-gate 	short fflag;
23127c478bd9Sstevel@tonic-gate 
23137c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
23147c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
23157c478bd9Sstevel@tonic-gate 
23167c478bd9Sstevel@tonic-gate 	rwflag = 0;
23177c478bd9Sstevel@tonic-gate 	aiov[0].iov_base = (caddr_t)buf;
23187c478bd9Sstevel@tonic-gate 	aiov[0].iov_len = size;
23197c478bd9Sstevel@tonic-gate 	iovcnt = 1;
23207c478bd9Sstevel@tonic-gate 	cnt = (ssize_t)size;
23217c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
23227c478bd9Sstevel@tonic-gate 
23237c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
23247c478bd9Sstevel@tonic-gate 	auio.uio_iov = aiov;
23257c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
23267c478bd9Sstevel@tonic-gate 	auio.uio_resid = cnt;
23277c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_SYSSPACE;
23287c478bd9Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
23297c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
23307c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
23317c478bd9Sstevel@tonic-gate 
23327c478bd9Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
23337c478bd9Sstevel@tonic-gate 
23347c478bd9Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
23357c478bd9Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
23367c478bd9Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
23377c478bd9Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
23387c478bd9Sstevel@tonic-gate 	cnt -= auio.uio_resid;
23397c478bd9Sstevel@tonic-gate 
23407c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
23417c478bd9Sstevel@tonic-gate 
23427c478bd9Sstevel@tonic-gate 	if (error == EINTR && cnt != 0)
23437c478bd9Sstevel@tonic-gate 		error = 0;
23447c478bd9Sstevel@tonic-gate out:
23457c478bd9Sstevel@tonic-gate 	if (error != 0) {
23467c478bd9Sstevel@tonic-gate 		*err = error;
23477c478bd9Sstevel@tonic-gate 		return (0);
23487c478bd9Sstevel@tonic-gate 	} else {
23497c478bd9Sstevel@tonic-gate 		*err = 0;
23507c478bd9Sstevel@tonic-gate 		return (cnt);
23517c478bd9Sstevel@tonic-gate 	}
23527c478bd9Sstevel@tonic-gate }
2353