17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52caf0dcdSrshoaib  * Common Development and Distribution License (the "License").
62caf0dcdSrshoaib  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
212caf0dcdSrshoaib 
227c478bd9Sstevel@tonic-gate /*
233e95bd4aSAnders Persson  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24f012ee0cSGordon Ross  * Copyright 2016 Nexenta Systems, Inc.  All rights reserved.
258e935259SBryan Cantrill  * Copyright 2015, Joyent, Inc. All rights reserved.
26d865fc92SAndy Fiddaman  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
2715f90b02SGarrett D'Amore  * Copyright 2022 Garrett D'Amore
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #include <sys/types.h>
317c478bd9Sstevel@tonic-gate #include <sys/t_lock.h>
327c478bd9Sstevel@tonic-gate #include <sys/param.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/buf.h>
357c478bd9Sstevel@tonic-gate #include <sys/conf.h>
367c478bd9Sstevel@tonic-gate #include <sys/cred.h>
377c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
397c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
40aa59c4cbSrsb #include <sys/vfs_opreg.h>
417c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
427c478bd9Sstevel@tonic-gate #include <sys/debug.h>
437c478bd9Sstevel@tonic-gate #include <sys/errno.h>
447c478bd9Sstevel@tonic-gate #include <sys/time.h>
457c478bd9Sstevel@tonic-gate #include <sys/file.h>
467c478bd9Sstevel@tonic-gate #include <sys/open.h>
477c478bd9Sstevel@tonic-gate #include <sys/user.h>
487c478bd9Sstevel@tonic-gate #include <sys/termios.h>
497c478bd9Sstevel@tonic-gate #include <sys/stream.h>
507c478bd9Sstevel@tonic-gate #include <sys/strsubr.h>
517c478bd9Sstevel@tonic-gate #include <sys/strsun.h>
527c478bd9Sstevel@tonic-gate #include <sys/esunddi.h>
537c478bd9Sstevel@tonic-gate #include <sys/flock.h>
547c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
557c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
567c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
577c478bd9Sstevel@tonic-gate #include <sys/pathname.h>
587c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
597c478bd9Sstevel@tonic-gate #include <sys/stat.h>
607c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h>
617c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h>
627c478bd9Sstevel@tonic-gate #include <sys/zone.h>
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #include <sys/socket.h>
657c478bd9Sstevel@tonic-gate #include <sys/socketvar.h>
667c478bd9Sstevel@tonic-gate #include <netinet/in.h>
677c478bd9Sstevel@tonic-gate #include <sys/un.h>
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 
760f1702c5SYu Xiangning #include <fs/sockfs/sockcommon.h>
773e95bd4aSAnders Persson #include <fs/sockfs/sockfilter_impl.h>
780f1702c5SYu Xiangning #include <fs/sockfs/socktpi.h>
790f1702c5SYu Xiangning #include <fs/sockfs/socktpi_impl.h>
80bbc000e5SAnders Persson #include <fs/sockfs/sodirect.h>
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate /*
837c478bd9Sstevel@tonic-gate  * Macros that operate on struct cmsghdr.
847c478bd9Sstevel@tonic-gate  * The CMSG_VALID macro does not assume that the last option buffer is padded.
857c478bd9Sstevel@tonic-gate  */
867c478bd9Sstevel@tonic-gate #define	CMSG_CONTENT(cmsg)	(&((cmsg)[1]))
877c478bd9Sstevel@tonic-gate #define	CMSG_CONTENTLEN(cmsg)	((cmsg)->cmsg_len - sizeof (struct cmsghdr))
887c478bd9Sstevel@tonic-gate #define	CMSG_VALID(cmsg, start, end)					\
897c478bd9Sstevel@tonic-gate 	(ISALIGNED_cmsghdr(cmsg) &&					\
907c478bd9Sstevel@tonic-gate 	((uintptr_t)(cmsg) >= (uintptr_t)(start)) &&			\
917c478bd9Sstevel@tonic-gate 	((uintptr_t)(cmsg) < (uintptr_t)(end)) &&			\
927c478bd9Sstevel@tonic-gate 	((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) &&	\
937c478bd9Sstevel@tonic-gate 	((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
947c478bd9Sstevel@tonic-gate #define	SO_LOCK_WAKEUP_TIME	3000	/* Wakeup time in milliseconds */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate dev_t sockdev;	/* For fsid in getattr */
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate struct socklist socklist;
997c478bd9Sstevel@tonic-gate 
1000f1702c5SYu Xiangning struct kmem_cache *socket_cache;
1010f1702c5SYu Xiangning 
1023e95bd4aSAnders Persson /*
1033e95bd4aSAnders Persson  * sockconf_lock protects the socket configuration (socket types and
1043e95bd4aSAnders Persson  * socket filters) which is changed via the sockconfig system call.
1053e95bd4aSAnders Persson  */
1063e95bd4aSAnders Persson krwlock_t sockconf_lock;
1073e95bd4aSAnders Persson 
1087c478bd9Sstevel@tonic-gate static int sockfs_update(kstat_t *, int);
1097c478bd9Sstevel@tonic-gate static int sockfs_snapshot(kstat_t *, void *, int);
1100f1702c5SYu Xiangning extern smod_info_t *sotpi_smod_create(void);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate extern void sendfile_init();
1137c478bd9Sstevel@tonic-gate 
1146cefaae1SJack Meng extern int modrootloaded;
1156cefaae1SJack Meng 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * Translate from a device pathname (e.g. "/dev/tcp") to a vnode.
1187c478bd9Sstevel@tonic-gate  * Returns with the vnode held.
1197c478bd9Sstevel@tonic-gate  */
1200f1702c5SYu Xiangning int
sogetvp(char * devpath,vnode_t ** vpp,int uioflag)1217c478bd9Sstevel@tonic-gate sogetvp(char *devpath, vnode_t **vpp, int uioflag)
1227c478bd9Sstevel@tonic-gate {
1237c478bd9Sstevel@tonic-gate 	struct snode *csp;
1247c478bd9Sstevel@tonic-gate 	vnode_t *vp, *dvp;
1257c478bd9Sstevel@tonic-gate 	major_t maj;
1267c478bd9Sstevel@tonic-gate 	int error;
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE);
1290f1702c5SYu Xiangning 
1307c478bd9Sstevel@tonic-gate 	/*
1317c478bd9Sstevel@tonic-gate 	 * Lookup the underlying filesystem vnode.
1327c478bd9Sstevel@tonic-gate 	 */
1337c478bd9Sstevel@tonic-gate 	error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp);
1347c478bd9Sstevel@tonic-gate 	if (error)
1357c478bd9Sstevel@tonic-gate 		return (error);
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/* Check that it is the correct vnode */
1387c478bd9Sstevel@tonic-gate 	if (vp->v_type != VCHR) {
1397c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1407c478bd9Sstevel@tonic-gate 		return (ENOTSOCK);
1417c478bd9Sstevel@tonic-gate 	}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * If devpath went through devfs, the device should already
1457c478bd9Sstevel@tonic-gate 	 * be configured. If devpath is a mknod file, however, we
1467c478bd9Sstevel@tonic-gate 	 * need to make sure the device is properly configured.
1477c478bd9Sstevel@tonic-gate 	 * To do this, we do something similar to spec_open()
1487c478bd9Sstevel@tonic-gate 	 * except that we resolve to the minor/leaf level since
1497c478bd9Sstevel@tonic-gate 	 * we need to return a vnode.
1507c478bd9Sstevel@tonic-gate 	 */
1517c478bd9Sstevel@tonic-gate 	csp = VTOS(VTOS(vp)->s_commonvp);
1527c478bd9Sstevel@tonic-gate 	if (!(csp->s_flag & SDIPSET)) {
1537c478bd9Sstevel@tonic-gate 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1547c478bd9Sstevel@tonic-gate 		error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname);
1557c478bd9Sstevel@tonic-gate 		if (error == 0)
1567c478bd9Sstevel@tonic-gate 			error = devfs_lookupname(pathname, NULLVPP, &dvp);
1577c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1587c478bd9Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
1597c478bd9Sstevel@tonic-gate 		if (error != 0)
1607c478bd9Sstevel@tonic-gate 			return (ENXIO);
1617c478bd9Sstevel@tonic-gate 		vp = dvp;	/* use the devfs vp */
1627c478bd9Sstevel@tonic-gate 	}
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 	/* device is configured at this point */
1657c478bd9Sstevel@tonic-gate 	maj = getmajor(vp->v_rdev);
1667c478bd9Sstevel@tonic-gate 	if (!STREAMSTAB(maj)) {
1677c478bd9Sstevel@tonic-gate 		VN_RELE(vp);
1687c478bd9Sstevel@tonic-gate 		return (ENOSTR);
1697c478bd9Sstevel@tonic-gate 	}
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate 	*vpp = vp;
1727c478bd9Sstevel@tonic-gate 	return (0);
1737c478bd9Sstevel@tonic-gate }
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate /*
1767c478bd9Sstevel@tonic-gate  * Update the accessed, updated, or changed times in an sonode
1777c478bd9Sstevel@tonic-gate  * with the current time.
1787c478bd9Sstevel@tonic-gate  *
1797c478bd9Sstevel@tonic-gate  * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable
1807c478bd9Sstevel@tonic-gate  * attributes in a fstat call. (They return the current time and 0 for
1817c478bd9Sstevel@tonic-gate  * all timestamps, respectively.) We maintain the current timestamps
1827c478bd9Sstevel@tonic-gate  * here primarily so that should sockmod be popped the resulting
1837c478bd9Sstevel@tonic-gate  * file descriptor will behave like a stream w.r.t. the timestamps.
1847c478bd9Sstevel@tonic-gate  */
1857c478bd9Sstevel@tonic-gate void
so_update_attrs(struct sonode * so,int flag)1867c478bd9Sstevel@tonic-gate so_update_attrs(struct sonode *so, int flag)
1877c478bd9Sstevel@tonic-gate {
1887c478bd9Sstevel@tonic-gate 	time_t now = gethrestime_sec();
1897c478bd9Sstevel@tonic-gate 
1900f1702c5SYu Xiangning 	if (SOCK_IS_NONSTR(so))
1910f1702c5SYu Xiangning 		return;
1920f1702c5SYu Xiangning 
1937c478bd9Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
1947c478bd9Sstevel@tonic-gate 	so->so_flag |= flag;
1957c478bd9Sstevel@tonic-gate 	if (flag & SOACC)
1960f1702c5SYu Xiangning 		SOTOTPI(so)->sti_atime = now;
1977c478bd9Sstevel@tonic-gate 	if (flag & SOMOD)
1980f1702c5SYu Xiangning 		SOTOTPI(so)->sti_mtime = now;
1997c478bd9Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2020f1702c5SYu Xiangning extern so_create_func_t sock_comm_create_function;
2030f1702c5SYu Xiangning extern so_destroy_func_t sock_comm_destroy_function;
2047c478bd9Sstevel@tonic-gate /*
2057c478bd9Sstevel@tonic-gate  * Init function called when sockfs is loaded.
2067c478bd9Sstevel@tonic-gate  */
2077c478bd9Sstevel@tonic-gate int
sockinit(int fstype,char * name)2087c478bd9Sstevel@tonic-gate sockinit(int fstype, char *name)
2097c478bd9Sstevel@tonic-gate {
2107c478bd9Sstevel@tonic-gate 	static const fs_operation_def_t sock_vfsops_template[] = {
2117c478bd9Sstevel@tonic-gate 		NULL, NULL
2127c478bd9Sstevel@tonic-gate 	};
2137c478bd9Sstevel@tonic-gate 	int error;
2147c478bd9Sstevel@tonic-gate 	major_t dev;
2157c478bd9Sstevel@tonic-gate 	char *err_str;
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate 	error = vfs_setfsops(fstype, sock_vfsops_template, NULL);
2187c478bd9Sstevel@tonic-gate 	if (error != 0) {
2192caf0dcdSrshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
2202caf0dcdSrshoaib 		    "sockinit: bad vfs ops template");
2217c478bd9Sstevel@tonic-gate 		return (error);
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate 
2240f1702c5SYu Xiangning 	error = vn_make_ops(name, socket_vnodeops_template,
2250f1702c5SYu Xiangning 	    &socket_vnodeops);
2267c478bd9Sstevel@tonic-gate 	if (error != 0) {
2270f1702c5SYu Xiangning 		err_str = "sockinit: bad socket vnode ops template";
2287c478bd9Sstevel@tonic-gate 		/* vn_make_ops() does not reset socktpi_vnodeops on failure. */
2290f1702c5SYu Xiangning 		socket_vnodeops = NULL;
2307c478bd9Sstevel@tonic-gate 		goto failure;
2317c478bd9Sstevel@tonic-gate 	}
2327c478bd9Sstevel@tonic-gate 
2330f1702c5SYu Xiangning 	socket_cache = kmem_cache_create("socket_cache",
2340f1702c5SYu Xiangning 	    sizeof (struct sonode), 0, sonode_constructor,
2350f1702c5SYu Xiangning 	    sonode_destructor, NULL, NULL, NULL, 0);
2367c478bd9Sstevel@tonic-gate 
2373e95bd4aSAnders Persson 	rw_init(&sockconf_lock, NULL, RW_DEFAULT, NULL);
2383e95bd4aSAnders Persson 
2390f1702c5SYu Xiangning 	error = socktpi_init();
24074e20cfeSnh 	if (error != 0) {
24174e20cfeSnh 		err_str = NULL;
24274e20cfeSnh 		goto failure;
24374e20cfeSnh 	}
24474e20cfeSnh 
245bbc000e5SAnders Persson 	error = sod_init();
24617169044Sbrutus 	if (error != 0) {
24717169044Sbrutus 		err_str = NULL;
24817169044Sbrutus 		goto failure;
24917169044Sbrutus 	}
25017169044Sbrutus 
2517c478bd9Sstevel@tonic-gate 	/*
2520f1702c5SYu Xiangning 	 * Set up the default create and destroy functions
2537c478bd9Sstevel@tonic-gate 	 */
2540f1702c5SYu Xiangning 	sock_comm_create_function = socket_sonode_create;
2550f1702c5SYu Xiangning 	sock_comm_destroy_function = socket_sonode_destroy;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	/*
2587c478bd9Sstevel@tonic-gate 	 * Build initial list mapping socket parameters to vnode.
2597c478bd9Sstevel@tonic-gate 	 */
2600f1702c5SYu Xiangning 	smod_init();
2610f1702c5SYu Xiangning 	smod_add(sotpi_smod_create());
2620f1702c5SYu Xiangning 
2630f1702c5SYu Xiangning 	sockparams_init();
2647c478bd9Sstevel@tonic-gate 
2657c478bd9Sstevel@tonic-gate 	/*
2667c478bd9Sstevel@tonic-gate 	 * If sockets are needed before init runs /sbin/soconfig
2677c478bd9Sstevel@tonic-gate 	 * it is possible to preload the sockparams list here using
2687c478bd9Sstevel@tonic-gate 	 * calls like:
2697c478bd9Sstevel@tonic-gate 	 *	sockconfig(1,2,3, "/dev/tcp", 0);
2707c478bd9Sstevel@tonic-gate 	 */
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	/*
2737c478bd9Sstevel@tonic-gate 	 * Create a unique dev_t for use in so_fsid.
2747c478bd9Sstevel@tonic-gate 	 */
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	if ((dev = getudev()) == (major_t)-1)
2777c478bd9Sstevel@tonic-gate 		dev = 0;
2787c478bd9Sstevel@tonic-gate 	sockdev = makedevice(dev, 0);
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 	mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL);
2817c478bd9Sstevel@tonic-gate 	sendfile_init();
2827c478bd9Sstevel@tonic-gate 
2833e95bd4aSAnders Persson 	/* Initialize socket filters */
2843e95bd4aSAnders Persson 	sof_init();
2853e95bd4aSAnders Persson 
2867c478bd9Sstevel@tonic-gate 	return (0);
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate failure:
2897c478bd9Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(fstype);
2900f1702c5SYu Xiangning 	if (socket_vnodeops != NULL)
2910f1702c5SYu Xiangning 		vn_freevnodeops(socket_vnodeops);
2927c478bd9Sstevel@tonic-gate 	if (err_str != NULL)
2932caf0dcdSrshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str);
2947c478bd9Sstevel@tonic-gate 	return (error);
2957c478bd9Sstevel@tonic-gate }
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate /*
2987c478bd9Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOLOCKED.
2997c478bd9Sstevel@tonic-gate  */
3007c478bd9Sstevel@tonic-gate void
so_lock_single(struct sonode * so)3017c478bd9Sstevel@tonic-gate so_lock_single(struct sonode *so)
3027c478bd9Sstevel@tonic-gate {
3037c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) {
3066a571a2dSAnders Persson 		cv_wait_stop(&so->so_single_cv, &so->so_lock,
307d3e55dcdSgww 		    SO_LOCK_WAKEUP_TIME);
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 	so->so_flag |= SOLOCKED;
3107c478bd9Sstevel@tonic-gate }
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate /*
3137c478bd9Sstevel@tonic-gate  * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND.
3147c478bd9Sstevel@tonic-gate  * Used to clear SOLOCKED or SOASYNC_UNBIND.
3157c478bd9Sstevel@tonic-gate  */
3167c478bd9Sstevel@tonic-gate void
so_unlock_single(struct sonode * so,int flag)3177c478bd9Sstevel@tonic-gate so_unlock_single(struct sonode *so, int flag)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3207c478bd9Sstevel@tonic-gate 	ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND));
3217c478bd9Sstevel@tonic-gate 	ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0);
3227c478bd9Sstevel@tonic-gate 	ASSERT(so->so_flag & flag);
3237c478bd9Sstevel@tonic-gate 	/*
3240f1702c5SYu Xiangning 	 * Process the T_DISCON_IND on sti_discon_ind_mp.
3257c478bd9Sstevel@tonic-gate 	 *
3267c478bd9Sstevel@tonic-gate 	 * Call to so_drain_discon_ind will result in so_lock
3277c478bd9Sstevel@tonic-gate 	 * being dropped and re-acquired later.
3287c478bd9Sstevel@tonic-gate 	 */
3290f1702c5SYu Xiangning 	if (!SOCK_IS_NONSTR(so)) {
3300f1702c5SYu Xiangning 		sotpi_info_t *sti = SOTOTPI(so);
3310f1702c5SYu Xiangning 
3320f1702c5SYu Xiangning 		if (sti->sti_discon_ind_mp != NULL)
3330f1702c5SYu Xiangning 			so_drain_discon_ind(so);
3340f1702c5SYu Xiangning 	}
3357c478bd9Sstevel@tonic-gate 
3366a571a2dSAnders Persson 	cv_signal(&so->so_single_cv);
3376a571a2dSAnders Persson 	so->so_flag &= ~flag;
3387c478bd9Sstevel@tonic-gate }
3397c478bd9Sstevel@tonic-gate 
3407c478bd9Sstevel@tonic-gate /*
3417c478bd9Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOREADLOCKED.
3427c478bd9Sstevel@tonic-gate  * If the caller wants nonblocking behavior it should set fmode.
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate int
so_lock_read(struct sonode * so,int fmode)3457c478bd9Sstevel@tonic-gate so_lock_read(struct sonode *so, int fmode)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
3507c478bd9Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
3517c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
3526a571a2dSAnders Persson 		cv_wait_stop(&so->so_read_cv, &so->so_lock,
353d3e55dcdSgww 		    SO_LOCK_WAKEUP_TIME);
3547c478bd9Sstevel@tonic-gate 	}
3557c478bd9Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
3567c478bd9Sstevel@tonic-gate 	return (0);
3577c478bd9Sstevel@tonic-gate }
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate /*
3607c478bd9Sstevel@tonic-gate  * Like so_lock_read above but allows signals.
3617c478bd9Sstevel@tonic-gate  */
3627c478bd9Sstevel@tonic-gate int
so_lock_read_intr(struct sonode * so,int fmode)3637c478bd9Sstevel@tonic-gate so_lock_read_intr(struct sonode *so, int fmode)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
3687c478bd9Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
3697c478bd9Sstevel@tonic-gate 			return (EWOULDBLOCK);
3706a571a2dSAnders Persson 		if (!cv_wait_sig(&so->so_read_cv, &so->so_lock))
3717c478bd9Sstevel@tonic-gate 			return (EINTR);
3727c478bd9Sstevel@tonic-gate 	}
3737c478bd9Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
3747c478bd9Sstevel@tonic-gate 	return (0);
3757c478bd9Sstevel@tonic-gate }
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*
3787c478bd9Sstevel@tonic-gate  * Caller must hold the mutex. Used to clear SOREADLOCKED,
3797c478bd9Sstevel@tonic-gate  * set in so_lock_read() or so_lock_read_intr().
3807c478bd9Sstevel@tonic-gate  */
3817c478bd9Sstevel@tonic-gate void
so_unlock_read(struct sonode * so)3827c478bd9Sstevel@tonic-gate so_unlock_read(struct sonode *so)
3837c478bd9Sstevel@tonic-gate {
3847c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3857c478bd9Sstevel@tonic-gate 	ASSERT(so->so_flag & SOREADLOCKED);
3867c478bd9Sstevel@tonic-gate 
3876a571a2dSAnders Persson 	cv_signal(&so->so_read_cv);
3886a571a2dSAnders Persson 	so->so_flag &= ~SOREADLOCKED;
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * Verify that the specified offset falls within the mblk and
3937c478bd9Sstevel@tonic-gate  * that the resulting pointer is aligned.
3947c478bd9Sstevel@tonic-gate  * Returns NULL if not.
3957c478bd9Sstevel@tonic-gate  */
3967c478bd9Sstevel@tonic-gate void *
sogetoff(mblk_t * mp,t_uscalar_t offset,t_uscalar_t length,uint_t align_size)3977c478bd9Sstevel@tonic-gate sogetoff(mblk_t *mp, t_uscalar_t offset,
3987c478bd9Sstevel@tonic-gate     t_uscalar_t length, uint_t align_size)
3997c478bd9Sstevel@tonic-gate {
4007c478bd9Sstevel@tonic-gate 	uintptr_t ptr1, ptr2;
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	ASSERT(mp && mp->b_wptr >= mp->b_rptr);
4037c478bd9Sstevel@tonic-gate 	ptr1 = (uintptr_t)mp->b_rptr + offset;
4047c478bd9Sstevel@tonic-gate 	ptr2 = (uintptr_t)ptr1 + length;
4057c478bd9Sstevel@tonic-gate 	if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) {
4067c478bd9Sstevel@tonic-gate 		eprintline(0);
4077c478bd9Sstevel@tonic-gate 		return (NULL);
4087c478bd9Sstevel@tonic-gate 	}
4097c478bd9Sstevel@tonic-gate 	if ((ptr1 & (align_size - 1)) != 0) {
4107c478bd9Sstevel@tonic-gate 		eprintline(0);
4117c478bd9Sstevel@tonic-gate 		return (NULL);
4127c478bd9Sstevel@tonic-gate 	}
4137c478bd9Sstevel@tonic-gate 	return ((void *)ptr1);
4147c478bd9Sstevel@tonic-gate }
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate /*
4177c478bd9Sstevel@tonic-gate  * Return the AF_UNIX underlying filesystem vnode matching a given name.
4187c478bd9Sstevel@tonic-gate  * Makes sure the sending and the destination sonodes are compatible.
4197c478bd9Sstevel@tonic-gate  * The vnode is returned held.
4207c478bd9Sstevel@tonic-gate  *
4217c478bd9Sstevel@tonic-gate  * The underlying filesystem VSOCK vnode has a v_stream pointer that
4227c478bd9Sstevel@tonic-gate  * references the actual stream head (hence indirectly the actual sonode).
4237c478bd9Sstevel@tonic-gate  */
4247c478bd9Sstevel@tonic-gate static int
so_ux_lookup(struct sonode * so,struct sockaddr_un * soun,int checkaccess,vnode_t ** vpp)4257c478bd9Sstevel@tonic-gate so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess,
426f012ee0cSGordon Ross     vnode_t **vpp)
4277c478bd9Sstevel@tonic-gate {
4287c478bd9Sstevel@tonic-gate 	vnode_t		*vp;	/* Underlying filesystem vnode */
4299bf9355bSRic Aleshire 	vnode_t		*rvp;	/* real vnode */
4307c478bd9Sstevel@tonic-gate 	vnode_t		*svp;	/* sockfs vnode */
4317c478bd9Sstevel@tonic-gate 	struct sonode	*so2;
4327c478bd9Sstevel@tonic-gate 	int		error;
4337c478bd9Sstevel@tonic-gate 
4343eceedbbSrh 	dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", (void *)so,
4353eceedbbSrh 	    soun->sun_path));
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
4387c478bd9Sstevel@tonic-gate 	if (error) {
4397c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
4407c478bd9Sstevel@tonic-gate 		return (error);
4417c478bd9Sstevel@tonic-gate 	}
4429bf9355bSRic Aleshire 
4439bf9355bSRic Aleshire 	/*
4449bf9355bSRic Aleshire 	 * Traverse lofs mounts get the real vnode
4459bf9355bSRic Aleshire 	 */
4469bf9355bSRic Aleshire 	if (VOP_REALVP(vp, &rvp, NULL) == 0) {
4479bf9355bSRic Aleshire 		VN_HOLD(rvp);		/* hold the real vnode */
4489bf9355bSRic Aleshire 		VN_RELE(vp);		/* release hold from lookup */
4499bf9355bSRic Aleshire 		vp = rvp;
4509bf9355bSRic Aleshire 	}
4519bf9355bSRic Aleshire 
4527c478bd9Sstevel@tonic-gate 	if (vp->v_type != VSOCK) {
4537c478bd9Sstevel@tonic-gate 		error = ENOTSOCK;
4547c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
4557c478bd9Sstevel@tonic-gate 		goto done2;
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (checkaccess) {
4597c478bd9Sstevel@tonic-gate 		/*
4607c478bd9Sstevel@tonic-gate 		 * Check that we have permissions to access the destination
4617c478bd9Sstevel@tonic-gate 		 * vnode. This check is not done in BSD but it is required
4627c478bd9Sstevel@tonic-gate 		 * by X/Open.
4637c478bd9Sstevel@tonic-gate 		 */
464*34793d0fSToomas Soome 		error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL);
465*34793d0fSToomas Soome 		if (error != 0) {
4667c478bd9Sstevel@tonic-gate 			eprintsoline(so, error);
4677c478bd9Sstevel@tonic-gate 			goto done2;
4687c478bd9Sstevel@tonic-gate 		}
4697c478bd9Sstevel@tonic-gate 	}
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate 	/*
4727c478bd9Sstevel@tonic-gate 	 * Check if the remote socket has been closed.
4737c478bd9Sstevel@tonic-gate 	 *
4747c478bd9Sstevel@tonic-gate 	 * Synchronize with vn_rele_stream by holding v_lock while traversing
4757c478bd9Sstevel@tonic-gate 	 * v_stream->sd_vnode.
4767c478bd9Sstevel@tonic-gate 	 */
4777c478bd9Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
4787c478bd9Sstevel@tonic-gate 	if (vp->v_stream == NULL) {
4797c478bd9Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4807c478bd9Sstevel@tonic-gate 		if (so->so_type == SOCK_DGRAM)
4817c478bd9Sstevel@tonic-gate 			error = EDESTADDRREQ;
4827c478bd9Sstevel@tonic-gate 		else
4837c478bd9Sstevel@tonic-gate 			error = ECONNREFUSED;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
4867c478bd9Sstevel@tonic-gate 		goto done2;
4877c478bd9Sstevel@tonic-gate 	}
4887c478bd9Sstevel@tonic-gate 	ASSERT(vp->v_stream->sd_vnode);
4897c478bd9Sstevel@tonic-gate 	svp = vp->v_stream->sd_vnode;
4907c478bd9Sstevel@tonic-gate 	/*
4917c478bd9Sstevel@tonic-gate 	 * holding v_lock on underlying filesystem vnode and acquiring
4927c478bd9Sstevel@tonic-gate 	 * it on sockfs vnode. Assumes that no code ever attempts to
4937c478bd9Sstevel@tonic-gate 	 * acquire these locks in the reverse order.
4947c478bd9Sstevel@tonic-gate 	 */
4957c478bd9Sstevel@tonic-gate 	VN_HOLD(svp);
4967c478bd9Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate 	if (svp->v_type != VSOCK) {
4997c478bd9Sstevel@tonic-gate 		error = ENOTSOCK;
5007c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
5017c478bd9Sstevel@tonic-gate 		goto done;
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	so2 = VTOSO(svp);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	if (so->so_type != so2->so_type) {
5077c478bd9Sstevel@tonic-gate 		error = EPROTOTYPE;
5087c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
5097c478bd9Sstevel@tonic-gate 		goto done;
5107c478bd9Sstevel@tonic-gate 	}
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	VN_RELE(svp);
5137c478bd9Sstevel@tonic-gate 	*vpp = vp;
5147c478bd9Sstevel@tonic-gate 	return (0);
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate done:
5177c478bd9Sstevel@tonic-gate 	VN_RELE(svp);
5187c478bd9Sstevel@tonic-gate done2:
5197c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
5207c478bd9Sstevel@tonic-gate 	return (error);
5217c478bd9Sstevel@tonic-gate }
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate /*
5247c478bd9Sstevel@tonic-gate  * Verify peer address for connect and sendto/sendmsg.
5257c478bd9Sstevel@tonic-gate  * Since sendto/sendmsg would not get synchronous errors from the transport
5267c478bd9Sstevel@tonic-gate  * provider we have to do these ugly checks in the socket layer to
5277c478bd9Sstevel@tonic-gate  * preserve compatibility with SunOS 4.X.
5287c478bd9Sstevel@tonic-gate  */
5297c478bd9Sstevel@tonic-gate int
so_addr_verify(struct sonode * so,const struct sockaddr * name,socklen_t namelen)5307c478bd9Sstevel@tonic-gate so_addr_verify(struct sonode *so, const struct sockaddr *name,
5317c478bd9Sstevel@tonic-gate     socklen_t namelen)
5327c478bd9Sstevel@tonic-gate {
5337c478bd9Sstevel@tonic-gate 	int		family;
5347c478bd9Sstevel@tonic-gate 
535903a11ebSrh 	dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n",
536903a11ebSrh 	    (void *)so, (void *)name, namelen));
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	family = so->so_family;
5417c478bd9Sstevel@tonic-gate 	switch (family) {
5427c478bd9Sstevel@tonic-gate 	case AF_INET:
5437c478bd9Sstevel@tonic-gate 		if (name->sa_family != family) {
5447c478bd9Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5457c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
5487c478bd9Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
5497c478bd9Sstevel@tonic-gate 			return (EINVAL);
5507c478bd9Sstevel@tonic-gate 		}
5517c478bd9Sstevel@tonic-gate 		break;
5527c478bd9Sstevel@tonic-gate 	case AF_INET6: {
5537c478bd9Sstevel@tonic-gate #ifdef DEBUG
5547c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
5557c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 		if (name->sa_family != family) {
5587c478bd9Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5597c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5607c478bd9Sstevel@tonic-gate 		}
5617c478bd9Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
5627c478bd9Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
5637c478bd9Sstevel@tonic-gate 			return (EINVAL);
5647c478bd9Sstevel@tonic-gate 		}
5657c478bd9Sstevel@tonic-gate #ifdef DEBUG
5667c478bd9Sstevel@tonic-gate 		/* Verify that apps don't forget to clear sin6_scope_id etc */
5677c478bd9Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)name;
5687c478bd9Sstevel@tonic-gate 		if (sin6->sin6_scope_id != 0 &&
5697c478bd9Sstevel@tonic-gate 		    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
5702caf0dcdSrshoaib 			zcmn_err(getzoneid(), CE_WARN,
5717c478bd9Sstevel@tonic-gate 			    "connect/send* with uninitialized sin6_scope_id "
5727c478bd9Sstevel@tonic-gate 			    "(%d) on socket. Pid = %d\n",
5737c478bd9Sstevel@tonic-gate 			    (int)sin6->sin6_scope_id, (int)curproc->p_pid);
5747c478bd9Sstevel@tonic-gate 		}
5757c478bd9Sstevel@tonic-gate #endif /* DEBUG */
5767c478bd9Sstevel@tonic-gate 		break;
5777c478bd9Sstevel@tonic-gate 	}
5787c478bd9Sstevel@tonic-gate 	case AF_UNIX:
5790f1702c5SYu Xiangning 		if (SOTOTPI(so)->sti_faddr_noxlate) {
5807c478bd9Sstevel@tonic-gate 			return (0);
5817c478bd9Sstevel@tonic-gate 		}
5827c478bd9Sstevel@tonic-gate 		if (namelen < (socklen_t)sizeof (short)) {
5837c478bd9Sstevel@tonic-gate 			eprintsoline(so, ENOENT);
5847c478bd9Sstevel@tonic-gate 			return (ENOENT);
5857c478bd9Sstevel@tonic-gate 		}
5867c478bd9Sstevel@tonic-gate 		if (name->sa_family != family) {
5877c478bd9Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5887c478bd9Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5897c478bd9Sstevel@tonic-gate 		}
5907c478bd9Sstevel@tonic-gate 		/* MAXPATHLEN + soun_family + nul termination */
5917c478bd9Sstevel@tonic-gate 		if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
5927c478bd9Sstevel@tonic-gate 			eprintsoline(so, ENAMETOOLONG);
5937c478bd9Sstevel@tonic-gate 			return (ENAMETOOLONG);
5947c478bd9Sstevel@tonic-gate 		}
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 		break;
5977c478bd9Sstevel@tonic-gate 
5987c478bd9Sstevel@tonic-gate 	default:
5997c478bd9Sstevel@tonic-gate 		/*
6007c478bd9Sstevel@tonic-gate 		 * Default is don't do any length or sa_family check
6017c478bd9Sstevel@tonic-gate 		 * to allow non-sockaddr style addresses.
6027c478bd9Sstevel@tonic-gate 		 */
6037c478bd9Sstevel@tonic-gate 		break;
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 	return (0);
6077c478bd9Sstevel@tonic-gate }
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate /*
6117c478bd9Sstevel@tonic-gate  * Translate an AF_UNIX sockaddr_un to the transport internal name.
612f012ee0cSGordon Ross  * Assumes caller has called so_addr_verify first.  The translated
613f012ee0cSGordon Ross  * (internal form) address is stored in sti->sti_ux_taddr.
6147c478bd9Sstevel@tonic-gate  */
6157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
6167c478bd9Sstevel@tonic-gate int
so_ux_addr_xlate(struct sonode * so,struct sockaddr * name,socklen_t namelen,int checkaccess,void ** addrp,socklen_t * addrlenp)6177c478bd9Sstevel@tonic-gate so_ux_addr_xlate(struct sonode *so, struct sockaddr *name,
6187c478bd9Sstevel@tonic-gate     socklen_t namelen, int checkaccess,
6197c478bd9Sstevel@tonic-gate     void **addrp, socklen_t *addrlenp)
6207c478bd9Sstevel@tonic-gate {
6217c478bd9Sstevel@tonic-gate 	int			error;
6227c478bd9Sstevel@tonic-gate 	struct sockaddr_un	*soun;
6237c478bd9Sstevel@tonic-gate 	vnode_t			*vp;
6247c478bd9Sstevel@tonic-gate 	void			*addr;
6257c478bd9Sstevel@tonic-gate 	socklen_t		addrlen;
6260f1702c5SYu Xiangning 	sotpi_info_t		*sti = SOTOTPI(so);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n",
629903a11ebSrh 	    (void *)so, (void *)name, namelen, checkaccess));
6307c478bd9Sstevel@tonic-gate 
6317c478bd9Sstevel@tonic-gate 	ASSERT(name != NULL);
6327c478bd9Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
6330f1702c5SYu Xiangning 	ASSERT(!sti->sti_faddr_noxlate);
6347c478bd9Sstevel@tonic-gate 	ASSERT(namelen >= (socklen_t)sizeof (short));
6357c478bd9Sstevel@tonic-gate 	ASSERT(name->sa_family == AF_UNIX);
6367c478bd9Sstevel@tonic-gate 	soun = (struct sockaddr_un *)name;
6377c478bd9Sstevel@tonic-gate 	/*
6387c478bd9Sstevel@tonic-gate 	 * Lookup vnode for the specified path name and verify that
6397c478bd9Sstevel@tonic-gate 	 * it is a socket.
6407c478bd9Sstevel@tonic-gate 	 */
6417c478bd9Sstevel@tonic-gate 	error = so_ux_lookup(so, soun, checkaccess, &vp);
6427c478bd9Sstevel@tonic-gate 	if (error) {
6437c478bd9Sstevel@tonic-gate 		eprintsoline(so, error);
6447c478bd9Sstevel@tonic-gate 		return (error);
6457c478bd9Sstevel@tonic-gate 	}
6467c478bd9Sstevel@tonic-gate 	/*
6477c478bd9Sstevel@tonic-gate 	 * Use the address of the peer vnode as the address to send
6487c478bd9Sstevel@tonic-gate 	 * to. We release the peer vnode here. In case it has been
649d28d4716SJerry Jelinek 	 * closed by the time the T_CONN_REQ or T_UNITDATA_REQ reaches the
6507c478bd9Sstevel@tonic-gate 	 * transport the message will get an error or be dropped.
651f012ee0cSGordon Ross 	 * Note that that soua_vp is never dereferenced; it's just a
652f012ee0cSGordon Ross 	 * convenient value by which we can identify the peer.
6537c478bd9Sstevel@tonic-gate 	 */
654f012ee0cSGordon Ross 	sti->sti_ux_taddr.soua_vp = vp;
655f012ee0cSGordon Ross 	sti->sti_ux_taddr.soua_magic = SOU_MAGIC_EXPLICIT;
656f012ee0cSGordon Ross 	addr = &sti->sti_ux_taddr;
657f012ee0cSGordon Ross 	addrlen = (socklen_t)sizeof (sti->sti_ux_taddr);
658903a11ebSrh 	dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n",
659903a11ebSrh 	    addrlen, (void *)vp));
6607c478bd9Sstevel@tonic-gate 	VN_RELE(vp);
6617c478bd9Sstevel@tonic-gate 	*addrp = addr;
6627c478bd9Sstevel@tonic-gate 	*addrlenp = (socklen_t)addrlen;
6637c478bd9Sstevel@tonic-gate 	return (0);
6647c478bd9Sstevel@tonic-gate }
6657c478bd9Sstevel@tonic-gate 
6667c478bd9Sstevel@tonic-gate /*
6677c478bd9Sstevel@tonic-gate  * Esballoc free function for messages that contain SO_FILEP option.
6687c478bd9Sstevel@tonic-gate  * Decrement the reference count on the file pointers using closef.
6697c478bd9Sstevel@tonic-gate  */
6707c478bd9Sstevel@tonic-gate void
fdbuf_free(struct fdbuf * fdbuf)6717c478bd9Sstevel@tonic-gate fdbuf_free(struct fdbuf *fdbuf)
6727c478bd9Sstevel@tonic-gate {
6737c478bd9Sstevel@tonic-gate 	int	i;
6747c478bd9Sstevel@tonic-gate 	struct file *fp;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 	dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd));
6777c478bd9Sstevel@tonic-gate 	for (i = 0; i < fdbuf->fd_numfd; i++) {
6787c478bd9Sstevel@tonic-gate 		/*
6797c478bd9Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
6807c478bd9Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
6817c478bd9Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
6827c478bd9Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
6837c478bd9Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
6847c478bd9Sstevel@tonic-gate 		 */
6857c478bd9Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
686903a11ebSrh 		dprint(1, ("fdbuf_free: [%d] = %p\n", i, (void *)fp));
6877c478bd9Sstevel@tonic-gate 		(void) closef(fp);
6887c478bd9Sstevel@tonic-gate 	}
6897c478bd9Sstevel@tonic-gate 	if (fdbuf->fd_ebuf != NULL)
6907c478bd9Sstevel@tonic-gate 		kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen);
6917c478bd9Sstevel@tonic-gate 	kmem_free(fdbuf, fdbuf->fd_size);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate /*
695bd118333Smeem  * Allocate an esballoc'ed message for AF_UNIX file descriptor passing.
696bd118333Smeem  * Waits if memory is not available.
6977c478bd9Sstevel@tonic-gate  */
6987c478bd9Sstevel@tonic-gate mblk_t *
fdbuf_allocmsg(int size,struct fdbuf * fdbuf)6997c478bd9Sstevel@tonic-gate fdbuf_allocmsg(int size, struct fdbuf *fdbuf)
7007c478bd9Sstevel@tonic-gate {
701bd118333Smeem 	uchar_t	*buf;
7027c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd));
7057c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(size, KM_SLEEP);
7067c478bd9Sstevel@tonic-gate 	fdbuf->fd_ebuf = (caddr_t)buf;
7077c478bd9Sstevel@tonic-gate 	fdbuf->fd_ebuflen = size;
7087c478bd9Sstevel@tonic-gate 	fdbuf->fd_frtn.free_func = fdbuf_free;
7097c478bd9Sstevel@tonic-gate 	fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf;
7107c478bd9Sstevel@tonic-gate 
711bd118333Smeem 	mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn);
7127c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
7137c478bd9Sstevel@tonic-gate 	return (mp);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate /*
7177c478bd9Sstevel@tonic-gate  * Extract file descriptors from a fdbuf.
7187c478bd9Sstevel@tonic-gate  * Return list in rights/rightslen.
7197c478bd9Sstevel@tonic-gate  */
7207c478bd9Sstevel@tonic-gate /*ARGSUSED*/
7217c478bd9Sstevel@tonic-gate static int
fdbuf_extract(struct fdbuf * fdbuf,void * rights,int rightslen)7227c478bd9Sstevel@tonic-gate fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	int	i, fd;
7257c478bd9Sstevel@tonic-gate 	int	*rp;
7267c478bd9Sstevel@tonic-gate 	struct file *fp;
7277c478bd9Sstevel@tonic-gate 	int	numfd;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	dprint(1, ("fdbuf_extract: %d fds, len %d\n",
730d3e55dcdSgww 	    fdbuf->fd_numfd, rightslen));
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	numfd = fdbuf->fd_numfd;
7337c478bd9Sstevel@tonic-gate 	ASSERT(rightslen == numfd * (int)sizeof (int));
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	/*
7367c478bd9Sstevel@tonic-gate 	 * Allocate a file descriptor and increment the f_count.
7377c478bd9Sstevel@tonic-gate 	 * The latter is needed since we always call fdbuf_free
7387c478bd9Sstevel@tonic-gate 	 * which performs a closef.
7397c478bd9Sstevel@tonic-gate 	 */
7407c478bd9Sstevel@tonic-gate 	rp = (int *)rights;
7417c478bd9Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
7427c478bd9Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1)
7437c478bd9Sstevel@tonic-gate 			goto cleanup;
7447c478bd9Sstevel@tonic-gate 		/*
7457c478bd9Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
7467c478bd9Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
7477c478bd9Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
7487c478bd9Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
7497c478bd9Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
7507c478bd9Sstevel@tonic-gate 		 */
7517c478bd9Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
7527c478bd9Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
7537c478bd9Sstevel@tonic-gate 		fp->f_count++;
7547c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
7557c478bd9Sstevel@tonic-gate 		setf(fd, fp);
7567c478bd9Sstevel@tonic-gate 		*rp++ = fd;
757005d3febSMarek Pospisil 		if (AU_AUDITING())
7587c478bd9Sstevel@tonic-gate 			audit_fdrecv(fd, fp);
7597c478bd9Sstevel@tonic-gate 		dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
760903a11ebSrh 		    i, fd, (void *)fp, fp->f_count));
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 	return (0);
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate cleanup:
7657c478bd9Sstevel@tonic-gate 	/*
7667c478bd9Sstevel@tonic-gate 	 * Undo whatever partial work the loop above has done.
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	{
7697c478bd9Sstevel@tonic-gate 		int j;
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 		rp = (int *)rights;
7727c478bd9Sstevel@tonic-gate 		for (j = 0; j < i; j++) {
7737c478bd9Sstevel@tonic-gate 			dprint(0,
7747c478bd9Sstevel@tonic-gate 			    ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
7757c478bd9Sstevel@tonic-gate 			(void) closeandsetf(*rp++, NULL);
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 	}
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	return (EMFILE);
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate /*
7837c478bd9Sstevel@tonic-gate  * Insert file descriptors into an fdbuf.
7847c478bd9Sstevel@tonic-gate  * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
7857c478bd9Sstevel@tonic-gate  * by calling fdbuf_free().
7867c478bd9Sstevel@tonic-gate  */
7877c478bd9Sstevel@tonic-gate int
fdbuf_create(void * rights,int rightslen,struct fdbuf ** fdbufp)7887c478bd9Sstevel@tonic-gate fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 	int		numfd, i;
7917c478bd9Sstevel@tonic-gate 	int		*fds;
7927c478bd9Sstevel@tonic-gate 	struct file	*fp;
7937c478bd9Sstevel@tonic-gate 	struct fdbuf	*fdbuf;
7947c478bd9Sstevel@tonic-gate 	int		fdbufsize;
7957c478bd9Sstevel@tonic-gate 
7967c478bd9Sstevel@tonic-gate 	dprint(1, ("fdbuf_create: len %d\n", rightslen));
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
8017c478bd9Sstevel@tonic-gate 	fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
8027c478bd9Sstevel@tonic-gate 	fdbuf->fd_size = fdbufsize;
8037c478bd9Sstevel@tonic-gate 	fdbuf->fd_numfd = 0;
8047c478bd9Sstevel@tonic-gate 	fdbuf->fd_ebuf = NULL;
8057c478bd9Sstevel@tonic-gate 	fdbuf->fd_ebuflen = 0;
8067c478bd9Sstevel@tonic-gate 	fds = (int *)rights;
8077c478bd9Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
8087c478bd9Sstevel@tonic-gate 		if ((fp = getf(fds[i])) == NULL) {
8097c478bd9Sstevel@tonic-gate 			fdbuf_free(fdbuf);
8107c478bd9Sstevel@tonic-gate 			return (EBADF);
8117c478bd9Sstevel@tonic-gate 		}
8127c478bd9Sstevel@tonic-gate 		dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
813903a11ebSrh 		    i, fds[i], (void *)fp, fp->f_count));
8147c478bd9Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
8157c478bd9Sstevel@tonic-gate 		fp->f_count++;
8167c478bd9Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
8177c478bd9Sstevel@tonic-gate 		/*
8187c478bd9Sstevel@tonic-gate 		 * The maximum alignment for fdbuf (or any option header
8197c478bd9Sstevel@tonic-gate 		 * and its value) it 4 bytes. On a LP64 kernel, the alignment
8207c478bd9Sstevel@tonic-gate 		 * is not sufficient for pointers (fd_fds in this case). Since
8217c478bd9Sstevel@tonic-gate 		 * we just did a kmem_alloc (we get a double word alignment),
8227c478bd9Sstevel@tonic-gate 		 * we don't need to do anything on the send side (we loose
8237c478bd9Sstevel@tonic-gate 		 * the double word alignment because fdbuf goes after an
8247c478bd9Sstevel@tonic-gate 		 * option header (eg T_unitdata_req) which is only 4 byte
8257c478bd9Sstevel@tonic-gate 		 * aligned). We take care of this when we extract the file
8267c478bd9Sstevel@tonic-gate 		 * descriptor in fdbuf_extract or fdbuf_free.
8277c478bd9Sstevel@tonic-gate 		 */
8287c478bd9Sstevel@tonic-gate 		fdbuf->fd_fds[i] = fp;
8297c478bd9Sstevel@tonic-gate 		fdbuf->fd_numfd++;
8307c478bd9Sstevel@tonic-gate 		releasef(fds[i]);
831005d3febSMarek Pospisil 		if (AU_AUDITING())
8327c478bd9Sstevel@tonic-gate 			audit_fdsend(fds[i], fp, 0);
8337c478bd9Sstevel@tonic-gate 	}
8347c478bd9Sstevel@tonic-gate 	*fdbufp = fdbuf;
8357c478bd9Sstevel@tonic-gate 	return (0);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate static int
fdbuf_optlen(int rightslen)8397c478bd9Sstevel@tonic-gate fdbuf_optlen(int rightslen)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	int numfd;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate 	return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate static t_uscalar_t
fdbuf_cmsglen(int fdbuflen)8497c478bd9Sstevel@tonic-gate fdbuf_cmsglen(int fdbuflen)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
8527c478bd9Sstevel@tonic-gate 	    (int)sizeof (struct file *) * (int)sizeof (int));
8537c478bd9Sstevel@tonic-gate }
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate /*
8577c478bd9Sstevel@tonic-gate  * Return non-zero if the mblk and fdbuf are consistent.
8587c478bd9Sstevel@tonic-gate  */
8597c478bd9Sstevel@tonic-gate static int
fdbuf_verify(mblk_t * mp,struct fdbuf * fdbuf,int fdbuflen)8607c478bd9Sstevel@tonic-gate fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
8617c478bd9Sstevel@tonic-gate {
8627c478bd9Sstevel@tonic-gate 	if (fdbuflen >= FDBUF_HDRSIZE &&
8637c478bd9Sstevel@tonic-gate 	    fdbuflen == fdbuf->fd_size) {
8647c478bd9Sstevel@tonic-gate 		frtn_t *frp = mp->b_datap->db_frtnp;
8657c478bd9Sstevel@tonic-gate 		/*
8667c478bd9Sstevel@tonic-gate 		 * Check that the SO_FILEP portion of the
8677c478bd9Sstevel@tonic-gate 		 * message has not been modified by
8687c478bd9Sstevel@tonic-gate 		 * the loopback transport. The sending sockfs generates
8697c478bd9Sstevel@tonic-gate 		 * a message that is esballoc'ed with the free function
8707c478bd9Sstevel@tonic-gate 		 * being fdbuf_free() and where free_arg contains the
8717c478bd9Sstevel@tonic-gate 		 * identical information as the SO_FILEP content.
8727c478bd9Sstevel@tonic-gate 		 *
8737c478bd9Sstevel@tonic-gate 		 * If any of these constraints are not satisfied we
8747c478bd9Sstevel@tonic-gate 		 * silently ignore the option.
8757c478bd9Sstevel@tonic-gate 		 */
8767c478bd9Sstevel@tonic-gate 		ASSERT(mp);
8777c478bd9Sstevel@tonic-gate 		if (frp != NULL &&
8787c478bd9Sstevel@tonic-gate 		    frp->free_func == fdbuf_free &&
8797c478bd9Sstevel@tonic-gate 		    frp->free_arg != NULL &&
8807c478bd9Sstevel@tonic-gate 		    bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
8817c478bd9Sstevel@tonic-gate 			dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
882903a11ebSrh 			    (void *)fdbuf, fdbuflen));
8837c478bd9Sstevel@tonic-gate 			return (1);
8847c478bd9Sstevel@tonic-gate 		} else {
8852caf0dcdSrshoaib 			zcmn_err(getzoneid(), CE_WARN,
8867c478bd9Sstevel@tonic-gate 			    "sockfs: mismatched fdbuf content (%p)",
8877c478bd9Sstevel@tonic-gate 			    (void *)mp);
8887c478bd9Sstevel@tonic-gate 			return (0);
8897c478bd9Sstevel@tonic-gate 		}
8907c478bd9Sstevel@tonic-gate 	} else {
8912caf0dcdSrshoaib 		zcmn_err(getzoneid(), CE_WARN,
8927c478bd9Sstevel@tonic-gate 		    "sockfs: mismatched fdbuf len %d, %d\n",
8937c478bd9Sstevel@tonic-gate 		    fdbuflen, fdbuf->fd_size);
8947c478bd9Sstevel@tonic-gate 		return (0);
8957c478bd9Sstevel@tonic-gate 	}
8967c478bd9Sstevel@tonic-gate }
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate /*
8997c478bd9Sstevel@tonic-gate  * When the file descriptors returned by sorecvmsg can not be passed
9007c478bd9Sstevel@tonic-gate  * to the application this routine will cleanup the references on
9017c478bd9Sstevel@tonic-gate  * the files. Start at startoff bytes into the buffer.
9027c478bd9Sstevel@tonic-gate  */
9037c478bd9Sstevel@tonic-gate static void
close_fds(void * fdbuf,int fdbuflen,int startoff)9047c478bd9Sstevel@tonic-gate close_fds(void *fdbuf, int fdbuflen, int startoff)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	int *fds = (int *)fdbuf;
9077c478bd9Sstevel@tonic-gate 	int numfd = fdbuflen / (int)sizeof (int);
9087c478bd9Sstevel@tonic-gate 	int i;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
9137c478bd9Sstevel@tonic-gate 		if (startoff < 0)
9147c478bd9Sstevel@tonic-gate 			startoff = 0;
9157c478bd9Sstevel@tonic-gate 		if (startoff < (int)sizeof (int)) {
9167c478bd9Sstevel@tonic-gate 			/*
9177c478bd9Sstevel@tonic-gate 			 * This file descriptor is partially or fully after
9187c478bd9Sstevel@tonic-gate 			 * the offset
9197c478bd9Sstevel@tonic-gate 			 */
9207c478bd9Sstevel@tonic-gate 			dprint(0,
9217c478bd9Sstevel@tonic-gate 			    ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
9227c478bd9Sstevel@tonic-gate 			(void) closeandsetf(fds[i], NULL);
9237c478bd9Sstevel@tonic-gate 		}
9247c478bd9Sstevel@tonic-gate 		startoff -= (int)sizeof (int);
9257c478bd9Sstevel@tonic-gate 	}
9267c478bd9Sstevel@tonic-gate }
9277c478bd9Sstevel@tonic-gate 
9287c478bd9Sstevel@tonic-gate /*
9297c478bd9Sstevel@tonic-gate  * Close all file descriptors contained in the control part starting at
9307c478bd9Sstevel@tonic-gate  * the startoffset.
9317c478bd9Sstevel@tonic-gate  */
9327c478bd9Sstevel@tonic-gate void
so_closefds(void * control,t_uscalar_t controllen,int oldflg,int startoff)9337c478bd9Sstevel@tonic-gate so_closefds(void *control, t_uscalar_t controllen, int oldflg,
9347c478bd9Sstevel@tonic-gate     int startoff)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	if (control == NULL)
9397c478bd9Sstevel@tonic-gate 		return;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (oldflg) {
9427c478bd9Sstevel@tonic-gate 		close_fds(control, controllen, startoff);
9437c478bd9Sstevel@tonic-gate 		return;
9447c478bd9Sstevel@tonic-gate 	}
9457c478bd9Sstevel@tonic-gate 	/* Scan control part for file descriptors. */
9467c478bd9Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
9477c478bd9Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
9487c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
9497c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
9507c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
9517c478bd9Sstevel@tonic-gate 			close_fds(CMSG_CONTENT(cmsg),
9527c478bd9Sstevel@tonic-gate 			    (int)CMSG_CONTENTLEN(cmsg),
9537c478bd9Sstevel@tonic-gate 			    startoff - (int)sizeof (struct cmsghdr));
9547c478bd9Sstevel@tonic-gate 		}
955d865fc92SAndy Fiddaman 		startoff -= ROUNDUP_cmsglen(cmsg->cmsg_len);
956d865fc92SAndy Fiddaman 	}
957d865fc92SAndy Fiddaman }
958d865fc92SAndy Fiddaman 
959d865fc92SAndy Fiddaman /*
960d865fc92SAndy Fiddaman  * Handle truncation of a cmsg when the receive buffer is not big enough.
961d865fc92SAndy Fiddaman  * Adjust the cmsg_len header field in the last cmsg that will be included in
962d865fc92SAndy Fiddaman  * the buffer to reflect the number of bytes included.
963d865fc92SAndy Fiddaman  */
964d865fc92SAndy Fiddaman void
so_truncatecmsg(void * control,t_uscalar_t controllen,uint_t maxlen)965d865fc92SAndy Fiddaman so_truncatecmsg(void *control, t_uscalar_t controllen, uint_t maxlen)
966d865fc92SAndy Fiddaman {
967d865fc92SAndy Fiddaman 	struct cmsghdr *cmsg;
968d865fc92SAndy Fiddaman 	uint_t len = 0;
969d865fc92SAndy Fiddaman 
970d865fc92SAndy Fiddaman 	if (control == NULL)
971d865fc92SAndy Fiddaman 		return;
972d865fc92SAndy Fiddaman 
973d865fc92SAndy Fiddaman 	for (cmsg = control;
974d865fc92SAndy Fiddaman 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
975d865fc92SAndy Fiddaman 	    cmsg = CMSG_NEXT(cmsg)) {
976d865fc92SAndy Fiddaman 
977d865fc92SAndy Fiddaman 		len += ROUNDUP_cmsglen(cmsg->cmsg_len);
978d865fc92SAndy Fiddaman 
979d865fc92SAndy Fiddaman 		if (len > maxlen) {
980d865fc92SAndy Fiddaman 			/*
981d865fc92SAndy Fiddaman 			 * This cmsg is the last one that will be included in
982d865fc92SAndy Fiddaman 			 * the truncated buffer.
983d865fc92SAndy Fiddaman 			 */
984d865fc92SAndy Fiddaman 			socklen_t diff = len - maxlen;
985d865fc92SAndy Fiddaman 
986d865fc92SAndy Fiddaman 			if (diff < CMSG_CONTENTLEN(cmsg)) {
987d865fc92SAndy Fiddaman 				dprint(1, ("so_truncatecmsg: %d -> %d\n",
988d865fc92SAndy Fiddaman 				    cmsg->cmsg_len, cmsg->cmsg_len - diff));
989d865fc92SAndy Fiddaman 				cmsg->cmsg_len -= diff;
990d865fc92SAndy Fiddaman 			} else {
991d865fc92SAndy Fiddaman 				cmsg->cmsg_len = sizeof (struct cmsghdr);
992d865fc92SAndy Fiddaman 			}
993d865fc92SAndy Fiddaman 			break;
994d865fc92SAndy Fiddaman 		}
9957c478bd9Sstevel@tonic-gate 	}
9967c478bd9Sstevel@tonic-gate }
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate /*
9997c478bd9Sstevel@tonic-gate  * Returns a pointer/length for the file descriptors contained
10007c478bd9Sstevel@tonic-gate  * in the control buffer. Returns with *fdlenp == -1 if there are no
10017c478bd9Sstevel@tonic-gate  * file descriptor options present. This is different than there being
10027c478bd9Sstevel@tonic-gate  * a zero-length file descriptor option.
10037c478bd9Sstevel@tonic-gate  * Fail if there are multiple SCM_RIGHT cmsgs.
10047c478bd9Sstevel@tonic-gate  */
10057c478bd9Sstevel@tonic-gate int
so_getfdopt(void * control,t_uscalar_t controllen,int oldflg,void ** fdsp,int * fdlenp)10067c478bd9Sstevel@tonic-gate so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
10077c478bd9Sstevel@tonic-gate     void **fdsp, int *fdlenp)
10087c478bd9Sstevel@tonic-gate {
10097c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10107c478bd9Sstevel@tonic-gate 	void *fds;
10117c478bd9Sstevel@tonic-gate 	int fdlen;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	if (control == NULL) {
10147c478bd9Sstevel@tonic-gate 		*fdsp = NULL;
10157c478bd9Sstevel@tonic-gate 		*fdlenp = -1;
10167c478bd9Sstevel@tonic-gate 		return (0);
10177c478bd9Sstevel@tonic-gate 	}
10187c478bd9Sstevel@tonic-gate 
10197c478bd9Sstevel@tonic-gate 	if (oldflg) {
10207c478bd9Sstevel@tonic-gate 		*fdsp = control;
10217c478bd9Sstevel@tonic-gate 		if (controllen == 0)
10227c478bd9Sstevel@tonic-gate 			*fdlenp = -1;
10237c478bd9Sstevel@tonic-gate 		else
10247c478bd9Sstevel@tonic-gate 			*fdlenp = controllen;
10257c478bd9Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
10267c478bd9Sstevel@tonic-gate 		return (0);
10277c478bd9Sstevel@tonic-gate 	}
10287c478bd9Sstevel@tonic-gate 
10297c478bd9Sstevel@tonic-gate 	fds = NULL;
10307c478bd9Sstevel@tonic-gate 	fdlen = 0;
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10337c478bd9Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10347c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10357c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10367c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
10377c478bd9Sstevel@tonic-gate 			if (fds != NULL)
10387c478bd9Sstevel@tonic-gate 				return (EINVAL);
10397c478bd9Sstevel@tonic-gate 			fds = CMSG_CONTENT(cmsg);
10407c478bd9Sstevel@tonic-gate 			fdlen = (int)CMSG_CONTENTLEN(cmsg);
10411e0267ddSkrgopi 			dprint(1, ("so_getfdopt: new %lu\n",
1042d3e55dcdSgww 			    (size_t)CMSG_CONTENTLEN(cmsg)));
10437c478bd9Sstevel@tonic-gate 		}
10447c478bd9Sstevel@tonic-gate 	}
10457c478bd9Sstevel@tonic-gate 	if (fds == NULL) {
10467c478bd9Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: NONE\n"));
10477c478bd9Sstevel@tonic-gate 		*fdlenp = -1;
10487c478bd9Sstevel@tonic-gate 	} else
10497c478bd9Sstevel@tonic-gate 		*fdlenp = fdlen;
10507c478bd9Sstevel@tonic-gate 	*fdsp = fds;
10517c478bd9Sstevel@tonic-gate 	return (0);
10527c478bd9Sstevel@tonic-gate }
10537c478bd9Sstevel@tonic-gate 
10547c478bd9Sstevel@tonic-gate /*
10557c478bd9Sstevel@tonic-gate  * Return the length of the options including any file descriptor options.
10567c478bd9Sstevel@tonic-gate  */
10577c478bd9Sstevel@tonic-gate t_uscalar_t
so_optlen(void * control,t_uscalar_t controllen,int oldflg)10587c478bd9Sstevel@tonic-gate so_optlen(void *control, t_uscalar_t controllen, int oldflg)
10597c478bd9Sstevel@tonic-gate {
10607c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10617c478bd9Sstevel@tonic-gate 	t_uscalar_t optlen = 0;
10627c478bd9Sstevel@tonic-gate 	t_uscalar_t len;
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate 	if (control == NULL)
10657c478bd9Sstevel@tonic-gate 		return (0);
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate 	if (oldflg)
10687c478bd9Sstevel@tonic-gate 		return ((t_uscalar_t)(sizeof (struct T_opthdr) +
10697c478bd9Sstevel@tonic-gate 		    fdbuf_optlen(controllen)));
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10727c478bd9Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10737c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10747c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10757c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
10767c478bd9Sstevel@tonic-gate 			len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
10777c478bd9Sstevel@tonic-gate 		} else {
10787c478bd9Sstevel@tonic-gate 			len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
10797c478bd9Sstevel@tonic-gate 		}
10807c478bd9Sstevel@tonic-gate 		optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
10817c478bd9Sstevel@tonic-gate 		    sizeof (struct T_opthdr));
10827c478bd9Sstevel@tonic-gate 	}
10837c478bd9Sstevel@tonic-gate 	dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
1084d3e55dcdSgww 	    controllen, oldflg, optlen));
10857c478bd9Sstevel@tonic-gate 	return (optlen);
10867c478bd9Sstevel@tonic-gate }
10877c478bd9Sstevel@tonic-gate 
10887c478bd9Sstevel@tonic-gate /*
10897c478bd9Sstevel@tonic-gate  * Copy options from control to the mblk. Skip any file descriptor options.
10907c478bd9Sstevel@tonic-gate  */
10917c478bd9Sstevel@tonic-gate void
so_cmsg2opt(void * control,t_uscalar_t controllen,int oldflg,mblk_t * mp)10927c478bd9Sstevel@tonic-gate so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	struct T_opthdr toh;
10957c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	if (control == NULL)
10987c478bd9Sstevel@tonic-gate 		return;
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate 	if (oldflg) {
11017c478bd9Sstevel@tonic-gate 		/* No real options - caller has handled file descriptors */
11027c478bd9Sstevel@tonic-gate 		return;
11037c478bd9Sstevel@tonic-gate 	}
11047c478bd9Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
11057c478bd9Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
11067c478bd9Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
11077c478bd9Sstevel@tonic-gate 		/*
11087c478bd9Sstevel@tonic-gate 		 * Note: The caller handles file descriptors prior
11097c478bd9Sstevel@tonic-gate 		 * to calling this function.
11107c478bd9Sstevel@tonic-gate 		 */
11117c478bd9Sstevel@tonic-gate 		t_uscalar_t len;
11127c478bd9Sstevel@tonic-gate 
11137c478bd9Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
11147c478bd9Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS)
11157c478bd9Sstevel@tonic-gate 			continue;
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate 		len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
11187c478bd9Sstevel@tonic-gate 		toh.level = cmsg->cmsg_level;
11197c478bd9Sstevel@tonic-gate 		toh.name = cmsg->cmsg_type;
11207c478bd9Sstevel@tonic-gate 		toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
11217c478bd9Sstevel@tonic-gate 		toh.status = 0;
11227c478bd9Sstevel@tonic-gate 
11237c478bd9Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
11247c478bd9Sstevel@tonic-gate 		soappendmsg(mp, CMSG_CONTENT(cmsg), len);
11257c478bd9Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
11267c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
11277c478bd9Sstevel@tonic-gate 	}
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * Return the length of the control message derived from the options.
11327c478bd9Sstevel@tonic-gate  * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
11337c478bd9Sstevel@tonic-gate  * When oldflg is set only include SO_FILEP.
11340d204002Sgt  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
11350d204002Sgt  * allocates the space that so_opt2cmsg fills. If one changes, the other should
11360d204002Sgt  * also be checked for any possible impacts.
11377c478bd9Sstevel@tonic-gate  */
11387c478bd9Sstevel@tonic-gate t_uscalar_t
so_cmsglen(mblk_t * mp,void * opt,t_uscalar_t optlen,int oldflg)11397c478bd9Sstevel@tonic-gate so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
11407c478bd9Sstevel@tonic-gate {
11417c478bd9Sstevel@tonic-gate 	t_uscalar_t cmsglen = 0;
11427c478bd9Sstevel@tonic-gate 	struct T_opthdr *tohp;
11437c478bd9Sstevel@tonic-gate 	t_uscalar_t len;
11447c478bd9Sstevel@tonic-gate 	t_uscalar_t last_roundup = 0;
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
11497c478bd9Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
11507c478bd9Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
11517c478bd9Sstevel@tonic-gate 		dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
1152d3e55dcdSgww 		    tohp->level, tohp->name, tohp->len));
11537c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
11547c478bd9Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
11557c478bd9Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
11567c478bd9Sstevel@tonic-gate 			continue;
11577c478bd9Sstevel@tonic-gate 		}
11587c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
11597c478bd9Sstevel@tonic-gate 			struct fdbuf *fdbuf;
11607c478bd9Sstevel@tonic-gate 			int fdbuflen;
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
11637c478bd9Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
11667c478bd9Sstevel@tonic-gate 				continue;
11677c478bd9Sstevel@tonic-gate 			if (oldflg) {
11687c478bd9Sstevel@tonic-gate 				cmsglen += fdbuf_cmsglen(fdbuflen);
11697c478bd9Sstevel@tonic-gate 				continue;
11707c478bd9Sstevel@tonic-gate 			}
11717c478bd9Sstevel@tonic-gate 			len = fdbuf_cmsglen(fdbuflen);
11720d204002Sgt 		} else if (tohp->level == SOL_SOCKET &&
11730d204002Sgt 		    tohp->name == SCM_TIMESTAMP) {
11740d204002Sgt 			if (oldflg)
11750d204002Sgt 				continue;
11760d204002Sgt 
11770d204002Sgt 			if (get_udatamodel() == DATAMODEL_NATIVE) {
11780d204002Sgt 				len = sizeof (struct timeval);
11790d204002Sgt 			} else {
11800d204002Sgt 				len = sizeof (struct timeval32);
11810d204002Sgt 			}
11827c478bd9Sstevel@tonic-gate 		} else {
11837c478bd9Sstevel@tonic-gate 			if (oldflg)
11847c478bd9Sstevel@tonic-gate 				continue;
11857c478bd9Sstevel@tonic-gate 			len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
11867c478bd9Sstevel@tonic-gate 		}
11877c478bd9Sstevel@tonic-gate 		/*
11880d204002Sgt 		 * Exclude roundup for last option to not set
11897c478bd9Sstevel@tonic-gate 		 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
11907c478bd9Sstevel@tonic-gate 		 */
11917c478bd9Sstevel@tonic-gate 		last_roundup = (t_uscalar_t)
11927c478bd9Sstevel@tonic-gate 		    (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
11937c478bd9Sstevel@tonic-gate 		    (len + (int)sizeof (struct cmsghdr)));
11947c478bd9Sstevel@tonic-gate 		cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
11957c478bd9Sstevel@tonic-gate 		    last_roundup;
11967c478bd9Sstevel@tonic-gate 	}
11977c478bd9Sstevel@tonic-gate 	cmsglen -= last_roundup;
11987c478bd9Sstevel@tonic-gate 	dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
1199d3e55dcdSgww 	    optlen, oldflg, cmsglen));
12007c478bd9Sstevel@tonic-gate 	return (cmsglen);
12017c478bd9Sstevel@tonic-gate }
12027c478bd9Sstevel@tonic-gate 
12037c478bd9Sstevel@tonic-gate /*
12047c478bd9Sstevel@tonic-gate  * Copy options from options to the control. Convert SO_FILEP to
12057c478bd9Sstevel@tonic-gate  * file descriptors.
12067c478bd9Sstevel@tonic-gate  * Returns errno or zero.
12070d204002Sgt  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
12080d204002Sgt  * allocates the space that so_opt2cmsg fills. If one changes, the other should
12090d204002Sgt  * also be checked for any possible impacts.
12107c478bd9Sstevel@tonic-gate  */
12117c478bd9Sstevel@tonic-gate int
so_opt2cmsg(mblk_t * mp,void * opt,t_uscalar_t optlen,int oldflg,void * control,t_uscalar_t controllen)12127c478bd9Sstevel@tonic-gate so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
12137c478bd9Sstevel@tonic-gate     void *control, t_uscalar_t controllen)
12147c478bd9Sstevel@tonic-gate {
12157c478bd9Sstevel@tonic-gate 	struct T_opthdr *tohp;
12167c478bd9Sstevel@tonic-gate 	struct cmsghdr *cmsg;
12177c478bd9Sstevel@tonic-gate 	struct fdbuf *fdbuf;
12187c478bd9Sstevel@tonic-gate 	int fdbuflen;
12197c478bd9Sstevel@tonic-gate 	int error;
12200d204002Sgt #if defined(DEBUG) || defined(__lint)
12210d204002Sgt 	struct cmsghdr *cend = (struct cmsghdr *)
12220d204002Sgt 	    (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
12230d204002Sgt #endif
12247c478bd9Sstevel@tonic-gate 	cmsg = (struct cmsghdr *)control;
12257c478bd9Sstevel@tonic-gate 
12267c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
12297c478bd9Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
12307c478bd9Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
12317c478bd9Sstevel@tonic-gate 		dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
1232d3e55dcdSgww 		    tohp->level, tohp->name, tohp->len));
12337c478bd9Sstevel@tonic-gate 
12347c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
12357c478bd9Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
12367c478bd9Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
12377c478bd9Sstevel@tonic-gate 			continue;
12387c478bd9Sstevel@tonic-gate 		}
12397c478bd9Sstevel@tonic-gate 		ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
12407c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
12417c478bd9Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
12427c478bd9Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
12437c478bd9Sstevel@tonic-gate 
12447c478bd9Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
12457c478bd9Sstevel@tonic-gate 				return (EPROTO);
12467c478bd9Sstevel@tonic-gate 			if (oldflg) {
12477c478bd9Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf, control,
12487c478bd9Sstevel@tonic-gate 				    (int)controllen);
12497c478bd9Sstevel@tonic-gate 				if (error != 0)
12507c478bd9Sstevel@tonic-gate 					return (error);
12517c478bd9Sstevel@tonic-gate 				continue;
12527c478bd9Sstevel@tonic-gate 			} else {
12537c478bd9Sstevel@tonic-gate 				int fdlen;
12547c478bd9Sstevel@tonic-gate 
12557c478bd9Sstevel@tonic-gate 				fdlen = (int)fdbuf_cmsglen(
12567c478bd9Sstevel@tonic-gate 				    (int)_TPI_TOPT_DATALEN(tohp));
12577c478bd9Sstevel@tonic-gate 
12587c478bd9Sstevel@tonic-gate 				cmsg->cmsg_level = tohp->level;
12597c478bd9Sstevel@tonic-gate 				cmsg->cmsg_type = SCM_RIGHTS;
12607c478bd9Sstevel@tonic-gate 				cmsg->cmsg_len = (socklen_t)(fdlen +
1261d3e55dcdSgww 				    sizeof (struct cmsghdr));
12627c478bd9Sstevel@tonic-gate 
12637c478bd9Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf,
1264d3e55dcdSgww 				    CMSG_CONTENT(cmsg), fdlen);
12657c478bd9Sstevel@tonic-gate 				if (error != 0)
12667c478bd9Sstevel@tonic-gate 					return (error);
12677c478bd9Sstevel@tonic-gate 			}
1268e4f35dbaSgt 		} else if (tohp->level == SOL_SOCKET &&
1269e4f35dbaSgt 		    tohp->name == SCM_TIMESTAMP) {
1270e4f35dbaSgt 			timestruc_t *timestamp;
1271e4f35dbaSgt 
1272e4f35dbaSgt 			if (oldflg)
1273e4f35dbaSgt 				continue;
1274e4f35dbaSgt 
1275e4f35dbaSgt 			cmsg->cmsg_level = tohp->level;
1276e4f35dbaSgt 			cmsg->cmsg_type = tohp->name;
1277e4f35dbaSgt 
1278e4f35dbaSgt 			timestamp =
1279e4f35dbaSgt 			    (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
1280e4f35dbaSgt 			    sizeof (intptr_t));
1281e4f35dbaSgt 
1282e4f35dbaSgt 			if (get_udatamodel() == DATAMODEL_NATIVE) {
12830d204002Sgt 				struct timeval tv;
1284e4f35dbaSgt 
1285e4f35dbaSgt 				cmsg->cmsg_len = sizeof (struct timeval) +
1286e4f35dbaSgt 				    sizeof (struct cmsghdr);
12870d204002Sgt 				tv.tv_sec = timestamp->tv_sec;
12880d204002Sgt 				tv.tv_usec = timestamp->tv_nsec /
12890d204002Sgt 				    (NANOSEC / MICROSEC);
12900d204002Sgt 				/*
12910d204002Sgt 				 * on LP64 systems, the struct timeval in
12920d204002Sgt 				 * the destination will not be 8-byte aligned,
12930d204002Sgt 				 * so use bcopy to avoid alignment trouble
12940d204002Sgt 				 */
12950d204002Sgt 				bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
1296e4f35dbaSgt 			} else {
1297e4f35dbaSgt 				struct timeval32 *time32;
1298e4f35dbaSgt 
1299e4f35dbaSgt 				cmsg->cmsg_len = sizeof (struct timeval32) +
1300e4f35dbaSgt 				    sizeof (struct cmsghdr);
1301e4f35dbaSgt 				time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
1302e4f35dbaSgt 				time32->tv_sec = (time32_t)timestamp->tv_sec;
1303e4f35dbaSgt 				time32->tv_usec =
1304e4f35dbaSgt 				    (int32_t)(timestamp->tv_nsec /
1305e4f35dbaSgt 				    (NANOSEC / MICROSEC));
1306e4f35dbaSgt 			}
1307e4f35dbaSgt 
13087c478bd9Sstevel@tonic-gate 		} else {
13097c478bd9Sstevel@tonic-gate 			if (oldflg)
13107c478bd9Sstevel@tonic-gate 				continue;
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 			cmsg->cmsg_level = tohp->level;
13137c478bd9Sstevel@tonic-gate 			cmsg->cmsg_type = tohp->name;
1314221e47fbSAndy Fiddaman 			cmsg->cmsg_len = (socklen_t)sizeof (struct cmsghdr);
1315221e47fbSAndy Fiddaman 			if (tohp->level == IPPROTO_IP &&
1316221e47fbSAndy Fiddaman 			    (tohp->name == IP_RECVTOS ||
1317221e47fbSAndy Fiddaman 			    tohp->name == IP_RECVTTL)) {
1318221e47fbSAndy Fiddaman 				/*
1319221e47fbSAndy Fiddaman 				 * The data for these is a uint8_t but, in
1320221e47fbSAndy Fiddaman 				 * order to maintain alignment for any
1321221e47fbSAndy Fiddaman 				 * following TPI primitives in the message,
1322221e47fbSAndy Fiddaman 				 * there will be some trailing padding bytes
1323221e47fbSAndy Fiddaman 				 * which are included in the TPI_TOPT_DATALEN.
1324221e47fbSAndy Fiddaman 				 * For these types, we set the cmsg_len
1325221e47fbSAndy Fiddaman 				 * explicitly to the correct value.
1326221e47fbSAndy Fiddaman 				 */
1327221e47fbSAndy Fiddaman 				cmsg->cmsg_len += (socklen_t)sizeof (uint8_t);
1328221e47fbSAndy Fiddaman 			} else {
1329221e47fbSAndy Fiddaman 				cmsg->cmsg_len +=
1330221e47fbSAndy Fiddaman 				    (socklen_t)(_TPI_TOPT_DATALEN(tohp));
1331221e47fbSAndy Fiddaman 			}
13327c478bd9Sstevel@tonic-gate 
13337c478bd9Sstevel@tonic-gate 			/* copy content to control data part */
13347c478bd9Sstevel@tonic-gate 			bcopy(&tohp[1], CMSG_CONTENT(cmsg),
1335d3e55dcdSgww 			    CMSG_CONTENTLEN(cmsg));
13367c478bd9Sstevel@tonic-gate 		}
13377c478bd9Sstevel@tonic-gate 		/* move to next CMSG structure! */
13387c478bd9Sstevel@tonic-gate 		cmsg = CMSG_NEXT(cmsg);
13397c478bd9Sstevel@tonic-gate 	}
13400d204002Sgt 	dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
1341903a11ebSrh 	    control, controllen, (void *)cend, (void *)cmsg));
13420d204002Sgt 	ASSERT(cmsg <= cend);
13437c478bd9Sstevel@tonic-gate 	return (0);
13447c478bd9Sstevel@tonic-gate }
13457c478bd9Sstevel@tonic-gate 
13467c478bd9Sstevel@tonic-gate /*
13477c478bd9Sstevel@tonic-gate  * Extract the SO_SRCADDR option value if present.
13487c478bd9Sstevel@tonic-gate  */
13497c478bd9Sstevel@tonic-gate void
so_getopt_srcaddr(void * opt,t_uscalar_t optlen,void ** srcp,t_uscalar_t * srclenp)13507c478bd9Sstevel@tonic-gate so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
13517c478bd9Sstevel@tonic-gate     t_uscalar_t *srclenp)
13527c478bd9Sstevel@tonic-gate {
13537c478bd9Sstevel@tonic-gate 	struct T_opthdr		*tohp;
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
13567c478bd9Sstevel@tonic-gate 
13577c478bd9Sstevel@tonic-gate 	ASSERT(srcp != NULL && srclenp != NULL);
13587c478bd9Sstevel@tonic-gate 	*srcp = NULL;
13597c478bd9Sstevel@tonic-gate 	*srclenp = 0;
13607c478bd9Sstevel@tonic-gate 
13617c478bd9Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
13627c478bd9Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
13637c478bd9Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
13647c478bd9Sstevel@tonic-gate 		dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
1365d3e55dcdSgww 		    tohp->level, tohp->name, tohp->len));
13667c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
13677c478bd9Sstevel@tonic-gate 		    tohp->name == SO_SRCADDR) {
13687c478bd9Sstevel@tonic-gate 			*srcp = _TPI_TOPT_DATA(tohp);
13697c478bd9Sstevel@tonic-gate 			*srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
13707c478bd9Sstevel@tonic-gate 		}
13717c478bd9Sstevel@tonic-gate 	}
13727c478bd9Sstevel@tonic-gate }
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate /*
13757c478bd9Sstevel@tonic-gate  * Verify if the SO_UNIX_CLOSE option is present.
13767c478bd9Sstevel@tonic-gate  */
13777c478bd9Sstevel@tonic-gate int
so_getopt_unix_close(void * opt,t_uscalar_t optlen)13787c478bd9Sstevel@tonic-gate so_getopt_unix_close(void *opt, t_uscalar_t optlen)
13797c478bd9Sstevel@tonic-gate {
13807c478bd9Sstevel@tonic-gate 	struct T_opthdr		*tohp;
13817c478bd9Sstevel@tonic-gate 
13827c478bd9Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
13837c478bd9Sstevel@tonic-gate 
13847c478bd9Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
13857c478bd9Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
13867c478bd9Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
13877c478bd9Sstevel@tonic-gate 		dprint(1,
1388d3e55dcdSgww 		    ("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
1389d3e55dcdSgww 		    tohp->level, tohp->name, tohp->len));
13907c478bd9Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
13917c478bd9Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)
13927c478bd9Sstevel@tonic-gate 			return (1);
13937c478bd9Sstevel@tonic-gate 	}
13947c478bd9Sstevel@tonic-gate 	return (0);
13957c478bd9Sstevel@tonic-gate }
13967c478bd9Sstevel@tonic-gate 
13977c478bd9Sstevel@tonic-gate /*
13987c478bd9Sstevel@tonic-gate  * Allocate an M_PROTO message.
13997c478bd9Sstevel@tonic-gate  *
14007c478bd9Sstevel@tonic-gate  * If allocation fails the behavior depends on sleepflg:
14017c478bd9Sstevel@tonic-gate  *	_ALLOC_NOSLEEP	fail immediately
14027c478bd9Sstevel@tonic-gate  *	_ALLOC_INTR	sleep for memory until a signal is caught
14037c478bd9Sstevel@tonic-gate  *	_ALLOC_SLEEP	sleep forever. Don't return NULL.
14047c478bd9Sstevel@tonic-gate  */
14057c478bd9Sstevel@tonic-gate mblk_t *
soallocproto(size_t size,int sleepflg,cred_t * cr)1406de8c4a14SErik Nordmark soallocproto(size_t size, int sleepflg, cred_t *cr)
14077c478bd9Sstevel@tonic-gate {
14087c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
14097c478bd9Sstevel@tonic-gate 
14107c478bd9Sstevel@tonic-gate 	/* Round up size for reuse */
14117c478bd9Sstevel@tonic-gate 	size = MAX(size, 64);
1412de8c4a14SErik Nordmark 	if (cr != NULL)
1413de8c4a14SErik Nordmark 		mp = allocb_cred(size, cr, curproc->p_pid);
1414de8c4a14SErik Nordmark 	else
1415de8c4a14SErik Nordmark 		mp = allocb(size, BPRI_MED);
1416de8c4a14SErik Nordmark 
14177c478bd9Sstevel@tonic-gate 	if (mp == NULL) {
14187c478bd9Sstevel@tonic-gate 		int error;	/* Dummy - error not returned to caller */
14197c478bd9Sstevel@tonic-gate 
14207c478bd9Sstevel@tonic-gate 		switch (sleepflg) {
14217c478bd9Sstevel@tonic-gate 		case _ALLOC_SLEEP:
1422de8c4a14SErik Nordmark 			if (cr != NULL) {
1423de8c4a14SErik Nordmark 				mp = allocb_cred_wait(size, STR_NOSIG, &error,
1424de8c4a14SErik Nordmark 				    cr, curproc->p_pid);
1425de8c4a14SErik Nordmark 			} else {
1426de8c4a14SErik Nordmark 				mp = allocb_wait(size, BPRI_MED, STR_NOSIG,
1427de8c4a14SErik Nordmark 				    &error);
1428de8c4a14SErik Nordmark 			}
14297c478bd9Sstevel@tonic-gate 			ASSERT(mp);
14307c478bd9Sstevel@tonic-gate 			break;
14317c478bd9Sstevel@tonic-gate 		case _ALLOC_INTR:
1432de8c4a14SErik Nordmark 			if (cr != NULL) {
1433de8c4a14SErik Nordmark 				mp = allocb_cred_wait(size, 0, &error, cr,
1434de8c4a14SErik Nordmark 				    curproc->p_pid);
1435de8c4a14SErik Nordmark 			} else {
1436de8c4a14SErik Nordmark 				mp = allocb_wait(size, BPRI_MED, 0, &error);
1437de8c4a14SErik Nordmark 			}
14387c478bd9Sstevel@tonic-gate 			if (mp == NULL) {
14397c478bd9Sstevel@tonic-gate 				/* Caught signal while sleeping for memory */
14407c478bd9Sstevel@tonic-gate 				eprintline(ENOBUFS);
14417c478bd9Sstevel@tonic-gate 				return (NULL);
14427c478bd9Sstevel@tonic-gate 			}
14437c478bd9Sstevel@tonic-gate 			break;
14447c478bd9Sstevel@tonic-gate 		case _ALLOC_NOSLEEP:
14457c478bd9Sstevel@tonic-gate 		default:
14467c478bd9Sstevel@tonic-gate 			eprintline(ENOBUFS);
14477c478bd9Sstevel@tonic-gate 			return (NULL);
14487c478bd9Sstevel@tonic-gate 		}
14497c478bd9Sstevel@tonic-gate 	}
14507c478bd9Sstevel@tonic-gate 	DB_TYPE(mp) = M_PROTO;
14517c478bd9Sstevel@tonic-gate 	return (mp);
14527c478bd9Sstevel@tonic-gate }
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate /*
14557c478bd9Sstevel@tonic-gate  * Allocate an M_PROTO message with a single component.
14567c478bd9Sstevel@tonic-gate  * len is the length of buf. size is the amount to allocate.
14577c478bd9Sstevel@tonic-gate  *
14587c478bd9Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
14597c478bd9Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
14607c478bd9Sstevel@tonic-gate  */
14617c478bd9Sstevel@tonic-gate mblk_t *
soallocproto1(const void * buf,ssize_t len,ssize_t size,int sleepflg,cred_t * cr)1462de8c4a14SErik Nordmark soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg,
1463de8c4a14SErik Nordmark     cred_t *cr)
14647c478bd9Sstevel@tonic-gate {
14657c478bd9Sstevel@tonic-gate 	mblk_t	*mp;
14667c478bd9Sstevel@tonic-gate 
14677c478bd9Sstevel@tonic-gate 	if (size == 0)
14687c478bd9Sstevel@tonic-gate 		size = len;
14697c478bd9Sstevel@tonic-gate 
14707c478bd9Sstevel@tonic-gate 	ASSERT(size >= len);
14717c478bd9Sstevel@tonic-gate 	/* Round up size for reuse */
14727c478bd9Sstevel@tonic-gate 	size = MAX(size, 64);
1473de8c4a14SErik Nordmark 	mp = soallocproto(size, sleepflg, cr);
14747c478bd9Sstevel@tonic-gate 	if (mp == NULL)
14757c478bd9Sstevel@tonic-gate 		return (NULL);
14767c478bd9Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
14777c478bd9Sstevel@tonic-gate 	if (len != 0) {
14787c478bd9Sstevel@tonic-gate 		if (buf != NULL)
14797c478bd9Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
14807c478bd9Sstevel@tonic-gate 		else
14817c478bd9Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
14827c478bd9Sstevel@tonic-gate 		mp->b_wptr += len;
14837c478bd9Sstevel@tonic-gate 	}
14847c478bd9Sstevel@tonic-gate 	return (mp);
14857c478bd9Sstevel@tonic-gate }
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate /*
14887c478bd9Sstevel@tonic-gate  * Append buf/len to mp.
14897c478bd9Sstevel@tonic-gate  * The caller has to ensure that there is enough room in the mblk.
14907c478bd9Sstevel@tonic-gate  *
14917c478bd9Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
14927c478bd9Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
14937c478bd9Sstevel@tonic-gate  */
14947c478bd9Sstevel@tonic-gate void
soappendmsg(mblk_t * mp,const void * buf,ssize_t len)14957c478bd9Sstevel@tonic-gate soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
14967c478bd9Sstevel@tonic-gate {
14977c478bd9Sstevel@tonic-gate 	ASSERT(mp);
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 	if (len != 0) {
15007c478bd9Sstevel@tonic-gate 		/* Assert for room left */
15017c478bd9Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
15027c478bd9Sstevel@tonic-gate 		if (buf != NULL)
15037c478bd9Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
15047c478bd9Sstevel@tonic-gate 		else
15057c478bd9Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
15067c478bd9Sstevel@tonic-gate 	}
15077c478bd9Sstevel@tonic-gate 	mp->b_wptr += len;
15087c478bd9Sstevel@tonic-gate }
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate /*
15117c478bd9Sstevel@tonic-gate  * Create a message using two kernel buffers.
15127c478bd9Sstevel@tonic-gate  * If size is set that will determine the allocation size (e.g. for future
15137c478bd9Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
15147c478bd9Sstevel@tonic-gate  * lengths.
15157c478bd9Sstevel@tonic-gate  */
15167c478bd9Sstevel@tonic-gate mblk_t *
soallocproto2(const void * buf1,ssize_t len1,const void * buf2,ssize_t len2,ssize_t size,int sleepflg,cred_t * cr)15177c478bd9Sstevel@tonic-gate soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1518de8c4a14SErik Nordmark     ssize_t size, int sleepflg, cred_t *cr)
15197c478bd9Sstevel@tonic-gate {
15207c478bd9Sstevel@tonic-gate 	mblk_t *mp;
15217c478bd9Sstevel@tonic-gate 
15227c478bd9Sstevel@tonic-gate 	if (size == 0)
15237c478bd9Sstevel@tonic-gate 		size = len1 + len2;
15247c478bd9Sstevel@tonic-gate 	ASSERT(size >= len1 + len2);
15257c478bd9Sstevel@tonic-gate 
1526de8c4a14SErik Nordmark 	mp = soallocproto1(buf1, len1, size, sleepflg, cr);
15277c478bd9Sstevel@tonic-gate 	if (mp)
15287c478bd9Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
15297c478bd9Sstevel@tonic-gate 	return (mp);
15307c478bd9Sstevel@tonic-gate }
15317c478bd9Sstevel@tonic-gate 
15327c478bd9Sstevel@tonic-gate /*
15337c478bd9Sstevel@tonic-gate  * Create a message using three kernel buffers.
15347c478bd9Sstevel@tonic-gate  * If size is set that will determine the allocation size (for future
15357c478bd9Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
15367c478bd9Sstevel@tonic-gate  * lengths.
15377c478bd9Sstevel@tonic-gate  */
15387c478bd9Sstevel@tonic-gate mblk_t *
soallocproto3(const void * buf1,ssize_t len1,const void * buf2,ssize_t len2,const void * buf3,ssize_t len3,ssize_t size,int sleepflg,cred_t * cr)15397c478bd9Sstevel@tonic-gate soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1540de8c4a14SErik Nordmark     const void *buf3, ssize_t len3, ssize_t size, int sleepflg, cred_t *cr)
15417c478bd9Sstevel@tonic-gate {
15427c478bd9Sstevel@tonic-gate 	mblk_t *mp;
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate 	if (size == 0)
15457c478bd9Sstevel@tonic-gate 		size = len1 + len2 +len3;
15467c478bd9Sstevel@tonic-gate 	ASSERT(size >= len1 + len2 + len3);
15477c478bd9Sstevel@tonic-gate 
1548de8c4a14SErik Nordmark 	mp = soallocproto1(buf1, len1, size, sleepflg, cr);
15497c478bd9Sstevel@tonic-gate 	if (mp != NULL) {
15507c478bd9Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
15517c478bd9Sstevel@tonic-gate 		soappendmsg(mp, buf3, len3);
15527c478bd9Sstevel@tonic-gate 	}
15537c478bd9Sstevel@tonic-gate 	return (mp);
15547c478bd9Sstevel@tonic-gate }
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate #ifdef DEBUG
15577c478bd9Sstevel@tonic-gate char *
pr_state(uint_t state,uint_t mode)15587c478bd9Sstevel@tonic-gate pr_state(uint_t state, uint_t mode)
15597c478bd9Sstevel@tonic-gate {
15607c478bd9Sstevel@tonic-gate 	static char buf[1024];
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	buf[0] = 0;
15637c478bd9Sstevel@tonic-gate 	if (state & SS_ISCONNECTED)
1564903a11ebSrh 		(void) strcat(buf, "ISCONNECTED ");
15657c478bd9Sstevel@tonic-gate 	if (state & SS_ISCONNECTING)
1566903a11ebSrh 		(void) strcat(buf, "ISCONNECTING ");
15677c478bd9Sstevel@tonic-gate 	if (state & SS_ISDISCONNECTING)
1568903a11ebSrh 		(void) strcat(buf, "ISDISCONNECTING ");
15697c478bd9Sstevel@tonic-gate 	if (state & SS_CANTSENDMORE)
1570903a11ebSrh 		(void) strcat(buf, "CANTSENDMORE ");
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate 	if (state & SS_CANTRCVMORE)
1573903a11ebSrh 		(void) strcat(buf, "CANTRCVMORE ");
15747c478bd9Sstevel@tonic-gate 	if (state & SS_ISBOUND)
1575903a11ebSrh 		(void) strcat(buf, "ISBOUND ");
15767c478bd9Sstevel@tonic-gate 	if (state & SS_NDELAY)
1577903a11ebSrh 		(void) strcat(buf, "NDELAY ");
15787c478bd9Sstevel@tonic-gate 	if (state & SS_NONBLOCK)
1579903a11ebSrh 		(void) strcat(buf, "NONBLOCK ");
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate 	if (state & SS_ASYNC)
1582903a11ebSrh 		(void) strcat(buf, "ASYNC ");
15837c478bd9Sstevel@tonic-gate 	if (state & SS_ACCEPTCONN)
1584903a11ebSrh 		(void) strcat(buf, "ACCEPTCONN ");
15857c478bd9Sstevel@tonic-gate 	if (state & SS_SAVEDEOR)
1586903a11ebSrh 		(void) strcat(buf, "SAVEDEOR ");
15877c478bd9Sstevel@tonic-gate 
15887c478bd9Sstevel@tonic-gate 	if (state & SS_RCVATMARK)
1589903a11ebSrh 		(void) strcat(buf, "RCVATMARK ");
15907c478bd9Sstevel@tonic-gate 	if (state & SS_OOBPEND)
1591903a11ebSrh 		(void) strcat(buf, "OOBPEND ");
15927c478bd9Sstevel@tonic-gate 	if (state & SS_HAVEOOBDATA)
1593903a11ebSrh 		(void) strcat(buf, "HAVEOOBDATA ");
15947c478bd9Sstevel@tonic-gate 	if (state & SS_HADOOBDATA)
1595903a11ebSrh 		(void) strcat(buf, "HADOOBDATA ");
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate 	if (mode & SM_PRIV)
1598903a11ebSrh 		(void) strcat(buf, "PRIV ");
15997c478bd9Sstevel@tonic-gate 	if (mode & SM_ATOMIC)
1600903a11ebSrh 		(void) strcat(buf, "ATOMIC ");
16017c478bd9Sstevel@tonic-gate 	if (mode & SM_ADDR)
1602903a11ebSrh 		(void) strcat(buf, "ADDR ");
16037c478bd9Sstevel@tonic-gate 	if (mode & SM_CONNREQUIRED)
1604903a11ebSrh 		(void) strcat(buf, "CONNREQUIRED ");
16057c478bd9Sstevel@tonic-gate 
16067c478bd9Sstevel@tonic-gate 	if (mode & SM_FDPASSING)
1607903a11ebSrh 		(void) strcat(buf, "FDPASSING ");
16087c478bd9Sstevel@tonic-gate 	if (mode & SM_EXDATA)
1609903a11ebSrh 		(void) strcat(buf, "EXDATA ");
16107c478bd9Sstevel@tonic-gate 	if (mode & SM_OPTDATA)
1611903a11ebSrh 		(void) strcat(buf, "OPTDATA ");
16127c478bd9Sstevel@tonic-gate 	if (mode & SM_BYTESTREAM)
1613903a11ebSrh 		(void) strcat(buf, "BYTESTREAM ");
16147c478bd9Sstevel@tonic-gate 	return (buf);
16157c478bd9Sstevel@tonic-gate }
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate char *
pr_addr(int family,struct sockaddr * addr,t_uscalar_t addrlen)16187c478bd9Sstevel@tonic-gate pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
16197c478bd9Sstevel@tonic-gate {
16207c478bd9Sstevel@tonic-gate 	static char buf[1024];
16217c478bd9Sstevel@tonic-gate 
16227c478bd9Sstevel@tonic-gate 	if (addr == NULL || addrlen == 0) {
1623903a11ebSrh 		(void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr);
16247c478bd9Sstevel@tonic-gate 		return (buf);
16257c478bd9Sstevel@tonic-gate 	}
16267c478bd9Sstevel@tonic-gate 	switch (family) {
16277c478bd9Sstevel@tonic-gate 	case AF_INET: {
16287c478bd9Sstevel@tonic-gate 		struct sockaddr_in sin;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 		bcopy(addr, &sin, sizeof (sin));
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "(len %d) %x/%d",
1633b5fca8f8Stomee 		    addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
16347c478bd9Sstevel@tonic-gate 		break;
16357c478bd9Sstevel@tonic-gate 	}
16367c478bd9Sstevel@tonic-gate 	case AF_INET6: {
16377c478bd9Sstevel@tonic-gate 		struct sockaddr_in6 sin6;
16387c478bd9Sstevel@tonic-gate 		uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 		bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
1641903a11ebSrh 		(void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
16427c478bd9Sstevel@tonic-gate 		    addrlen,
16437c478bd9Sstevel@tonic-gate 		    ntohs(piece[0]), ntohs(piece[1]),
16447c478bd9Sstevel@tonic-gate 		    ntohs(piece[2]), ntohs(piece[3]),
16457c478bd9Sstevel@tonic-gate 		    ntohs(piece[4]), ntohs(piece[5]),
16467c478bd9Sstevel@tonic-gate 		    ntohs(piece[6]), ntohs(piece[7]),
16477c478bd9Sstevel@tonic-gate 		    ntohs(sin6.sin6_port));
16487c478bd9Sstevel@tonic-gate 		break;
16497c478bd9Sstevel@tonic-gate 	}
16507c478bd9Sstevel@tonic-gate 	case AF_UNIX: {
16517c478bd9Sstevel@tonic-gate 		struct sockaddr_un *soun = (struct sockaddr_un *)addr;
16527c478bd9Sstevel@tonic-gate 
1653b5fca8f8Stomee 		(void) sprintf(buf, "(len %d) %s", addrlen,
1654d3e55dcdSgww 		    (soun == NULL) ? "(none)" : soun->sun_path);
16557c478bd9Sstevel@tonic-gate 		break;
16567c478bd9Sstevel@tonic-gate 	}
16577c478bd9Sstevel@tonic-gate 	default:
16587c478bd9Sstevel@tonic-gate 		(void) sprintf(buf, "(unknown af %d)", family);
16597c478bd9Sstevel@tonic-gate 		break;
16607c478bd9Sstevel@tonic-gate 	}
16617c478bd9Sstevel@tonic-gate 	return (buf);
16627c478bd9Sstevel@tonic-gate }
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate /* The logical equivalence operator (a if-and-only-if b) */
166556f33205SJonathan Adams #define	EQUIVALENT(a, b)	(((a) && (b)) || (!(a) && (!(b))))
16667c478bd9Sstevel@tonic-gate 
16677c478bd9Sstevel@tonic-gate /*
16687c478bd9Sstevel@tonic-gate  * Verify limitations and invariants on oob state.
16697c478bd9Sstevel@tonic-gate  * Return 1 if OK, otherwise 0 so that it can be used as
16707c478bd9Sstevel@tonic-gate  *	ASSERT(verify_oobstate(so));
16717c478bd9Sstevel@tonic-gate  */
16727c478bd9Sstevel@tonic-gate int
so_verify_oobstate(struct sonode * so)16737c478bd9Sstevel@tonic-gate so_verify_oobstate(struct sonode *so)
16747c478bd9Sstevel@tonic-gate {
16750f1702c5SYu Xiangning 	boolean_t havemark;
16760f1702c5SYu Xiangning 
16777c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate 	/*
16807c478bd9Sstevel@tonic-gate 	 * The possible state combinations are:
16817c478bd9Sstevel@tonic-gate 	 *	0
16827c478bd9Sstevel@tonic-gate 	 *	SS_OOBPEND
16837c478bd9Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HAVEOOBDATA
16847c478bd9Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HADOOBDATA
16857c478bd9Sstevel@tonic-gate 	 *	SS_HADOOBDATA
16867c478bd9Sstevel@tonic-gate 	 */
16877c478bd9Sstevel@tonic-gate 	switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
16887c478bd9Sstevel@tonic-gate 	case 0:
16897c478bd9Sstevel@tonic-gate 	case SS_OOBPEND:
16907c478bd9Sstevel@tonic-gate 	case SS_OOBPEND|SS_HAVEOOBDATA:
16917c478bd9Sstevel@tonic-gate 	case SS_OOBPEND|SS_HADOOBDATA:
16927c478bd9Sstevel@tonic-gate 	case SS_HADOOBDATA:
16937c478bd9Sstevel@tonic-gate 		break;
16947c478bd9Sstevel@tonic-gate 	default:
16950f1702c5SYu Xiangning 		printf("Bad oob state 1 (%p): state %s\n",
16960f1702c5SYu Xiangning 		    (void *)so, pr_state(so->so_state, so->so_mode));
16977c478bd9Sstevel@tonic-gate 		return (0);
16987c478bd9Sstevel@tonic-gate 	}
16997c478bd9Sstevel@tonic-gate 
17007c478bd9Sstevel@tonic-gate 	/* SS_RCVATMARK should only be set when SS_OOBPEND is set */
17017c478bd9Sstevel@tonic-gate 	if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
17020f1702c5SYu Xiangning 		printf("Bad oob state 2 (%p): state %s\n",
17030f1702c5SYu Xiangning 		    (void *)so, pr_state(so->so_state, so->so_mode));
17047c478bd9Sstevel@tonic-gate 		return (0);
17057c478bd9Sstevel@tonic-gate 	}
17067c478bd9Sstevel@tonic-gate 
17077c478bd9Sstevel@tonic-gate 	/*
17080f1702c5SYu Xiangning 	 * (havemark != 0 or SS_RCVATMARK) iff SS_OOBPEND
17090f1702c5SYu Xiangning 	 * For TPI, the presence of a "mark" is indicated by sti_oobsigcnt.
17107c478bd9Sstevel@tonic-gate 	 */
17110f1702c5SYu Xiangning 	havemark = (SOCK_IS_NONSTR(so)) ? so->so_oobmark > 0 :
17120f1702c5SYu Xiangning 	    SOTOTPI(so)->sti_oobsigcnt > 0;
17130f1702c5SYu Xiangning 
171456f33205SJonathan Adams 	if (!EQUIVALENT(havemark || (so->so_state & SS_RCVATMARK),
1715d3e55dcdSgww 	    so->so_state & SS_OOBPEND)) {
17160f1702c5SYu Xiangning 		printf("Bad oob state 3 (%p): state %s\n",
17170f1702c5SYu Xiangning 		    (void *)so, pr_state(so->so_state, so->so_mode));
17187c478bd9Sstevel@tonic-gate 		return (0);
17197c478bd9Sstevel@tonic-gate 	}
17207c478bd9Sstevel@tonic-gate 
17217c478bd9Sstevel@tonic-gate 	/*
17227c478bd9Sstevel@tonic-gate 	 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
17237c478bd9Sstevel@tonic-gate 	 */
17247c478bd9Sstevel@tonic-gate 	if (!(so->so_options & SO_OOBINLINE) &&
172556f33205SJonathan Adams 	    !EQUIVALENT(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
17260f1702c5SYu Xiangning 		printf("Bad oob state 4 (%p): state %s\n",
17270f1702c5SYu Xiangning 		    (void *)so, pr_state(so->so_state, so->so_mode));
17287c478bd9Sstevel@tonic-gate 		return (0);
17297c478bd9Sstevel@tonic-gate 	}
17300f1702c5SYu Xiangning 
17310f1702c5SYu Xiangning 	if (!SOCK_IS_NONSTR(so) &&
17320f1702c5SYu Xiangning 	    SOTOTPI(so)->sti_oobsigcnt < SOTOTPI(so)->sti_oobcnt) {
17337c478bd9Sstevel@tonic-gate 		printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
17340f1702c5SYu Xiangning 		    (void *)so, SOTOTPI(so)->sti_oobsigcnt,
17350f1702c5SYu Xiangning 		    SOTOTPI(so)->sti_oobcnt,
17360f1702c5SYu Xiangning 		    pr_state(so->so_state, so->so_mode));
17377c478bd9Sstevel@tonic-gate 		return (0);
17387c478bd9Sstevel@tonic-gate 	}
17390f1702c5SYu Xiangning 
17407c478bd9Sstevel@tonic-gate 	return (1);
17417c478bd9Sstevel@tonic-gate }
174256f33205SJonathan Adams #undef	EQUIVALENT
17437c478bd9Sstevel@tonic-gate #endif /* DEBUG */
17447c478bd9Sstevel@tonic-gate 
17457c478bd9Sstevel@tonic-gate /* initialize sockfs zone specific kstat related items			*/
17467c478bd9Sstevel@tonic-gate void *
sock_kstat_init(zoneid_t zoneid)17477c478bd9Sstevel@tonic-gate sock_kstat_init(zoneid_t zoneid)
17487c478bd9Sstevel@tonic-gate {
17497c478bd9Sstevel@tonic-gate 	kstat_t	*ksp;
17507c478bd9Sstevel@tonic-gate 
17517c478bd9Sstevel@tonic-gate 	ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
17527c478bd9Sstevel@tonic-gate 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
17557c478bd9Sstevel@tonic-gate 		ksp->ks_update = sockfs_update;
17567c478bd9Sstevel@tonic-gate 		ksp->ks_snapshot = sockfs_snapshot;
17577c478bd9Sstevel@tonic-gate 		ksp->ks_lock = &socklist.sl_lock;
17587c478bd9Sstevel@tonic-gate 		ksp->ks_private = (void *)(uintptr_t)zoneid;
17597c478bd9Sstevel@tonic-gate 		kstat_install(ksp);
17607c478bd9Sstevel@tonic-gate 	}
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate 	return (ksp);
17637c478bd9Sstevel@tonic-gate }
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate /* tear down sockfs zone specific kstat related items			*/
17667c478bd9Sstevel@tonic-gate /*ARGSUSED*/
17677c478bd9Sstevel@tonic-gate void
sock_kstat_fini(zoneid_t zoneid,void * arg)17687c478bd9Sstevel@tonic-gate sock_kstat_fini(zoneid_t zoneid, void *arg)
17697c478bd9Sstevel@tonic-gate {
17707c478bd9Sstevel@tonic-gate 	kstat_t *ksp = (kstat_t *)arg;
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate 	if (ksp != NULL) {
17737c478bd9Sstevel@tonic-gate 		ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
17747c478bd9Sstevel@tonic-gate 		kstat_delete(ksp);
17757c478bd9Sstevel@tonic-gate 	}
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787c478bd9Sstevel@tonic-gate /*
17797c478bd9Sstevel@tonic-gate  * Zones:
17807c478bd9Sstevel@tonic-gate  * Note that nactive is going to be different for each zone.
17817c478bd9Sstevel@tonic-gate  * This means we require kstat to call sockfs_update and then sockfs_snapshot
17827c478bd9Sstevel@tonic-gate  * for the same zone, or sockfs_snapshot will be taken into the wrong size
17837c478bd9Sstevel@tonic-gate  * buffer. This is safe, but if the buffer is too small, user will not be
17847c478bd9Sstevel@tonic-gate  * given details of all sockets. However, as this kstat has a ks_lock, kstat
17857c478bd9Sstevel@tonic-gate  * driver will keep it locked between the update and the snapshot, so no
17867c478bd9Sstevel@tonic-gate  * other process (zone) can currently get inbetween resulting in a wrong size
17877c478bd9Sstevel@tonic-gate  * buffer allocation.
17887c478bd9Sstevel@tonic-gate  */
17897c478bd9Sstevel@tonic-gate static int
sockfs_update(kstat_t * ksp,int rw)17907c478bd9Sstevel@tonic-gate sockfs_update(kstat_t *ksp, int rw)
17917c478bd9Sstevel@tonic-gate {
17927c478bd9Sstevel@tonic-gate 	uint_t	nactive = 0;		/* # of active AF_UNIX sockets	*/
17937c478bd9Sstevel@tonic-gate 	struct sonode	*so;		/* current sonode on socklist	*/
17947c478bd9Sstevel@tonic-gate 	zoneid_t	myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
17957c478bd9Sstevel@tonic-gate 
17967c478bd9Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
17977c478bd9Sstevel@tonic-gate 
17987c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
17997c478bd9Sstevel@tonic-gate 		return (EACCES);
18007c478bd9Sstevel@tonic-gate 	}
18017c478bd9Sstevel@tonic-gate 
18020f1702c5SYu Xiangning 	for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
18030f1702c5SYu Xiangning 		if (so->so_count != 0 && so->so_zoneid == myzoneid) {
18047c478bd9Sstevel@tonic-gate 			nactive++;
18057c478bd9Sstevel@tonic-gate 		}
18067c478bd9Sstevel@tonic-gate 	}
18077c478bd9Sstevel@tonic-gate 	ksp->ks_ndata = nactive;
180878a2e113SAndy Fiddaman 	ksp->ks_data_size = nactive * sizeof (struct sockinfo);
18097c478bd9Sstevel@tonic-gate 
18107c478bd9Sstevel@tonic-gate 	return (0);
18117c478bd9Sstevel@tonic-gate }
18127c478bd9Sstevel@tonic-gate 
18137c478bd9Sstevel@tonic-gate static int
sockfs_snapshot(kstat_t * ksp,void * buf,int rw)18147c478bd9Sstevel@tonic-gate sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
18157c478bd9Sstevel@tonic-gate {
18167c478bd9Sstevel@tonic-gate 	int			ns;	/* # of sonodes we've copied	*/
18177c478bd9Sstevel@tonic-gate 	struct sonode		*so;	/* current sonode on socklist	*/
181878a2e113SAndy Fiddaman 	struct sockinfo		*psi;	/* where we put sockinfo data	*/
18197c478bd9Sstevel@tonic-gate 	t_uscalar_t		sn_len;	/* soa_len			*/
18207c478bd9Sstevel@tonic-gate 	zoneid_t		myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
182178a2e113SAndy Fiddaman 	sotpi_info_t		*sti;
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
18287c478bd9Sstevel@tonic-gate 		return (EACCES);
18297c478bd9Sstevel@tonic-gate 	}
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 	/*
183278a2e113SAndy Fiddaman 	 * For each sonode on the socklist, we massage the important
183378a2e113SAndy Fiddaman 	 * info into buf, in sockinfo format.
18347c478bd9Sstevel@tonic-gate 	 */
183578a2e113SAndy Fiddaman 	psi = (struct sockinfo *)buf;
18360f1702c5SYu Xiangning 	ns = 0;
18370f1702c5SYu Xiangning 	for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
183878a2e113SAndy Fiddaman 		vattr_t attr;
183978a2e113SAndy Fiddaman 
18407c478bd9Sstevel@tonic-gate 		/* only stuff active sonodes and the same zone:		*/
18410f1702c5SYu Xiangning 		if (so->so_count == 0 || so->so_zoneid != myzoneid) {
18427c478bd9Sstevel@tonic-gate 			continue;
18437c478bd9Sstevel@tonic-gate 		}
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate 		/*
18467c478bd9Sstevel@tonic-gate 		 * If the sonode was activated between the update and the
18477c478bd9Sstevel@tonic-gate 		 * snapshot, we're done - as this is only a snapshot.
18487c478bd9Sstevel@tonic-gate 		 */
184978a2e113SAndy Fiddaman 		if ((caddr_t)(psi) >= (caddr_t)buf + ksp->ks_data_size) {
18507c478bd9Sstevel@tonic-gate 			break;
18517c478bd9Sstevel@tonic-gate 		}
18527c478bd9Sstevel@tonic-gate 
18530f1702c5SYu Xiangning 		sti = SOTOTPI(so);
18547c478bd9Sstevel@tonic-gate 		/* copy important info into buf:			*/
185578a2e113SAndy Fiddaman 		psi->si_size = sizeof (struct sockinfo);
185678a2e113SAndy Fiddaman 		psi->si_family = so->so_family;
185778a2e113SAndy Fiddaman 		psi->si_type = so->so_type;
185878a2e113SAndy Fiddaman 		psi->si_flag = so->so_flag;
185978a2e113SAndy Fiddaman 		psi->si_state = so->so_state;
186078a2e113SAndy Fiddaman 		psi->si_serv_type = sti->sti_serv_type;
186178a2e113SAndy Fiddaman 		psi->si_ux_laddr_sou_magic = sti->sti_ux_laddr.soua_magic;
186278a2e113SAndy Fiddaman 		psi->si_ux_faddr_sou_magic = sti->sti_ux_faddr.soua_magic;
186378a2e113SAndy Fiddaman 		psi->si_laddr_soa_len = sti->sti_laddr.soa_len;
186478a2e113SAndy Fiddaman 		psi->si_faddr_soa_len = sti->sti_faddr.soa_len;
186578a2e113SAndy Fiddaman 		psi->si_szoneid = so->so_zoneid;
186678a2e113SAndy Fiddaman 		psi->si_faddr_noxlate = sti->sti_faddr_noxlate;
186778a2e113SAndy Fiddaman 
186878a2e113SAndy Fiddaman 		/*
186978a2e113SAndy Fiddaman 		 * Grab the inode, if possible.
187078a2e113SAndy Fiddaman 		 * This must be done before entering so_lock as VOP_GETATTR
187178a2e113SAndy Fiddaman 		 * will acquire it.
187278a2e113SAndy Fiddaman 		 */
187378a2e113SAndy Fiddaman 		if (so->so_vnode == NULL ||
187478a2e113SAndy Fiddaman 		    VOP_GETATTR(so->so_vnode, &attr, 0, CRED(), NULL) != 0)
187578a2e113SAndy Fiddaman 			attr.va_nodeid = 0;
187678a2e113SAndy Fiddaman 
187778a2e113SAndy Fiddaman 		psi->si_inode = attr.va_nodeid;
18787c478bd9Sstevel@tonic-gate 
18797c478bd9Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
18807c478bd9Sstevel@tonic-gate 
18810f1702c5SYu Xiangning 		if (sti->sti_laddr_sa != NULL) {
18820f1702c5SYu Xiangning 			ASSERT(sti->sti_laddr_sa->sa_data != NULL);
18830f1702c5SYu Xiangning 			sn_len = sti->sti_laddr_len;
18847c478bd9Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
188578a2e113SAndy Fiddaman 			    sizeof (psi->si_laddr_sun_path));
18867c478bd9Sstevel@tonic-gate 
188778a2e113SAndy Fiddaman 			psi->si_laddr_family =
18880f1702c5SYu Xiangning 			    sti->sti_laddr_sa->sa_family;
18897c478bd9Sstevel@tonic-gate 			if (sn_len != 0) {
18907c478bd9Sstevel@tonic-gate 				/* AF_UNIX socket names are NULL terminated */
189178a2e113SAndy Fiddaman 				(void) strncpy(psi->si_laddr_sun_path,
18920f1702c5SYu Xiangning 				    sti->sti_laddr_sa->sa_data,
189378a2e113SAndy Fiddaman 				    sizeof (psi->si_laddr_sun_path));
189478a2e113SAndy Fiddaman 				sn_len = strlen(psi->si_laddr_sun_path);
18957c478bd9Sstevel@tonic-gate 			}
189678a2e113SAndy Fiddaman 			psi->si_laddr_sun_path[sn_len] = 0;
18977c478bd9Sstevel@tonic-gate 		}
18987c478bd9Sstevel@tonic-gate 
18990f1702c5SYu Xiangning 		if (sti->sti_faddr_sa != NULL) {
19000f1702c5SYu Xiangning 			ASSERT(sti->sti_faddr_sa->sa_data != NULL);
19010f1702c5SYu Xiangning 			sn_len = sti->sti_faddr_len;
19027c478bd9Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
190378a2e113SAndy Fiddaman 			    sizeof (psi->si_faddr_sun_path));
19047c478bd9Sstevel@tonic-gate 
190578a2e113SAndy Fiddaman 			psi->si_faddr_family =
19060f1702c5SYu Xiangning 			    sti->sti_faddr_sa->sa_family;
19077c478bd9Sstevel@tonic-gate 			if (sn_len != 0) {
190878a2e113SAndy Fiddaman 				(void) strncpy(psi->si_faddr_sun_path,
19090f1702c5SYu Xiangning 				    sti->sti_faddr_sa->sa_data,
191078a2e113SAndy Fiddaman 				    sizeof (psi->si_faddr_sun_path));
191178a2e113SAndy Fiddaman 				sn_len = strlen(psi->si_faddr_sun_path);
19127c478bd9Sstevel@tonic-gate 			}
191378a2e113SAndy Fiddaman 			psi->si_faddr_sun_path[sn_len] = 0;
19147c478bd9Sstevel@tonic-gate 		}
19157c478bd9Sstevel@tonic-gate 
19167c478bd9Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
19177c478bd9Sstevel@tonic-gate 
191878a2e113SAndy Fiddaman 		(void) snprintf(psi->si_son_straddr,
191978a2e113SAndy Fiddaman 		    sizeof (psi->si_son_straddr), "%p", (void *)so);
192078a2e113SAndy Fiddaman 		(void) snprintf(psi->si_lvn_straddr,
192178a2e113SAndy Fiddaman 		    sizeof (psi->si_lvn_straddr), "%p",
19220f1702c5SYu Xiangning 		    (void *)sti->sti_ux_laddr.soua_vp);
192378a2e113SAndy Fiddaman 		(void) snprintf(psi->si_fvn_straddr,
192478a2e113SAndy Fiddaman 		    sizeof (psi->si_fvn_straddr), "%p",
19250f1702c5SYu Xiangning 		    (void *)sti->sti_ux_faddr.soua_vp);
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate 		ns++;
192878a2e113SAndy Fiddaman 		psi++;
19297c478bd9Sstevel@tonic-gate 	}
19307c478bd9Sstevel@tonic-gate 
19317c478bd9Sstevel@tonic-gate 	ksp->ks_ndata = ns;
19327c478bd9Sstevel@tonic-gate 	return (0);
19337c478bd9Sstevel@tonic-gate }
19347c478bd9Sstevel@tonic-gate 
19357c478bd9Sstevel@tonic-gate ssize_t
soreadfile(file_t * fp,uchar_t * buf,u_offset_t fileoff,int * err,size_t size)19367c478bd9Sstevel@tonic-gate soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
19377c478bd9Sstevel@tonic-gate {
19387c478bd9Sstevel@tonic-gate 	struct uio auio;
19398e935259SBryan Cantrill 	struct iovec aiov[1];
19407c478bd9Sstevel@tonic-gate 	register vnode_t *vp;
19417c478bd9Sstevel@tonic-gate 	int ioflag, rwflag;
19427c478bd9Sstevel@tonic-gate 	ssize_t cnt;
19437c478bd9Sstevel@tonic-gate 	int error = 0;
19447c478bd9Sstevel@tonic-gate 	int iovcnt = 0;
19457c478bd9Sstevel@tonic-gate 	short fflag;
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate 	vp = fp->f_vnode;
19487c478bd9Sstevel@tonic-gate 	fflag = fp->f_flag;
19497c478bd9Sstevel@tonic-gate 
19507c478bd9Sstevel@tonic-gate 	rwflag = 0;
19517c478bd9Sstevel@tonic-gate 	aiov[0].iov_base = (caddr_t)buf;
19527c478bd9Sstevel@tonic-gate 	aiov[0].iov_len = size;
19537c478bd9Sstevel@tonic-gate 	iovcnt = 1;
19547c478bd9Sstevel@tonic-gate 	cnt = (ssize_t)size;
19557c478bd9Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
19567c478bd9Sstevel@tonic-gate 
19577c478bd9Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
19587c478bd9Sstevel@tonic-gate 	auio.uio_iov = aiov;
19597c478bd9Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
19607c478bd9Sstevel@tonic-gate 	auio.uio_resid = cnt;
19617c478bd9Sstevel@tonic-gate 	auio.uio_segflg = UIO_SYSSPACE;
19627c478bd9Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
19637c478bd9Sstevel@tonic-gate 	auio.uio_fmode = fflag;
19647c478bd9Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
19657c478bd9Sstevel@tonic-gate 
19667c478bd9Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
19677c478bd9Sstevel@tonic-gate 
19687c478bd9Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
19697c478bd9Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
19707c478bd9Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
19717c478bd9Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
19727c478bd9Sstevel@tonic-gate 	cnt -= auio.uio_resid;
19737c478bd9Sstevel@tonic-gate 
19747c478bd9Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
19757c478bd9Sstevel@tonic-gate 
19767c478bd9Sstevel@tonic-gate 	if (error == EINTR && cnt != 0)
19777c478bd9Sstevel@tonic-gate 		error = 0;
19787c478bd9Sstevel@tonic-gate out:
19797c478bd9Sstevel@tonic-gate 	if (error != 0) {
19807c478bd9Sstevel@tonic-gate 		*err = error;
19817c478bd9Sstevel@tonic-gate 		return (0);
19827c478bd9Sstevel@tonic-gate 	} else {
19837c478bd9Sstevel@tonic-gate 		*err = 0;
19847c478bd9Sstevel@tonic-gate 		return (cnt);
19857c478bd9Sstevel@tonic-gate 	}
19867c478bd9Sstevel@tonic-gate }
19870f1702c5SYu Xiangning 
19880f1702c5SYu Xiangning int
so_copyin(const void * from,void * to,size_t size,int fromkernel)19890f1702c5SYu Xiangning so_copyin(const void *from, void *to, size_t size, int fromkernel)
19900f1702c5SYu Xiangning {
19910f1702c5SYu Xiangning 	if (fromkernel) {
19920f1702c5SYu Xiangning 		bcopy(from, to, size);
19930f1702c5SYu Xiangning 		return (0);
19940f1702c5SYu Xiangning 	}
19950f1702c5SYu Xiangning 	return (xcopyin(from, to, size));
19960f1702c5SYu Xiangning }
19970f1702c5SYu Xiangning 
19980f1702c5SYu Xiangning int
so_copyout(const void * from,void * to,size_t size,int tokernel)19990f1702c5SYu Xiangning so_copyout(const void *from, void *to, size_t size, int tokernel)
20000f1702c5SYu Xiangning {
20010f1702c5SYu Xiangning 	if (tokernel) {
20020f1702c5SYu Xiangning 		bcopy(from, to, size);
20030f1702c5SYu Xiangning 		return (0);
20040f1702c5SYu Xiangning 	}
20050f1702c5SYu Xiangning 	return (xcopyout(from, to, size));
20060f1702c5SYu Xiangning }
2007