xref: /onnv-gate/usr/src/uts/common/fs/sockfs/socksubr.c (revision 11474:857f9db4ef05)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51548Srshoaib  * Common Development and Distribution License (the "License").
61548Srshoaib  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
211548Srshoaib 
220Sstevel@tonic-gate /*
23*11474SJonathan.Adams@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <sys/types.h>
280Sstevel@tonic-gate #include <sys/t_lock.h>
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/systm.h>
310Sstevel@tonic-gate #include <sys/buf.h>
320Sstevel@tonic-gate #include <sys/conf.h>
330Sstevel@tonic-gate #include <sys/cred.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/sysmacros.h>
360Sstevel@tonic-gate #include <sys/vfs.h>
373898Srsb #include <sys/vfs_opreg.h>
380Sstevel@tonic-gate #include <sys/vnode.h>
390Sstevel@tonic-gate #include <sys/debug.h>
400Sstevel@tonic-gate #include <sys/errno.h>
410Sstevel@tonic-gate #include <sys/time.h>
420Sstevel@tonic-gate #include <sys/file.h>
430Sstevel@tonic-gate #include <sys/open.h>
440Sstevel@tonic-gate #include <sys/user.h>
450Sstevel@tonic-gate #include <sys/termios.h>
460Sstevel@tonic-gate #include <sys/stream.h>
470Sstevel@tonic-gate #include <sys/strsubr.h>
480Sstevel@tonic-gate #include <sys/strsun.h>
490Sstevel@tonic-gate #include <sys/esunddi.h>
500Sstevel@tonic-gate #include <sys/flock.h>
510Sstevel@tonic-gate #include <sys/modctl.h>
520Sstevel@tonic-gate #include <sys/cmn_err.h>
530Sstevel@tonic-gate #include <sys/mkdev.h>
540Sstevel@tonic-gate #include <sys/pathname.h>
550Sstevel@tonic-gate #include <sys/ddi.h>
560Sstevel@tonic-gate #include <sys/stat.h>
570Sstevel@tonic-gate #include <sys/fs/snode.h>
580Sstevel@tonic-gate #include <sys/fs/dv_node.h>
590Sstevel@tonic-gate #include <sys/zone.h>
600Sstevel@tonic-gate 
610Sstevel@tonic-gate #include <sys/socket.h>
620Sstevel@tonic-gate #include <sys/socketvar.h>
630Sstevel@tonic-gate #include <netinet/in.h>
640Sstevel@tonic-gate #include <sys/un.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/ucred.h>
670Sstevel@tonic-gate 
680Sstevel@tonic-gate #include <sys/tiuser.h>
690Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
700Sstevel@tonic-gate #include <sys/tihdr.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <c2/audit.h>
730Sstevel@tonic-gate 
740Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
758348SEric.Yu@Sun.COM #include <fs/sockfs/sockcommon.h>
768348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h>
778348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi_impl.h>
789491SAnders.Persson@Sun.COM #include <fs/sockfs/sodirect.h>
790Sstevel@tonic-gate 
800Sstevel@tonic-gate /*
810Sstevel@tonic-gate  * Macros that operate on struct cmsghdr.
820Sstevel@tonic-gate  * The CMSG_VALID macro does not assume that the last option buffer is padded.
830Sstevel@tonic-gate  */
840Sstevel@tonic-gate #define	CMSG_CONTENT(cmsg)	(&((cmsg)[1]))
850Sstevel@tonic-gate #define	CMSG_CONTENTLEN(cmsg)	((cmsg)->cmsg_len - sizeof (struct cmsghdr))
860Sstevel@tonic-gate #define	CMSG_VALID(cmsg, start, end)					\
870Sstevel@tonic-gate 	(ISALIGNED_cmsghdr(cmsg) &&					\
880Sstevel@tonic-gate 	((uintptr_t)(cmsg) >= (uintptr_t)(start)) &&			\
890Sstevel@tonic-gate 	((uintptr_t)(cmsg) < (uintptr_t)(end)) &&			\
900Sstevel@tonic-gate 	((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) &&	\
910Sstevel@tonic-gate 	((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
920Sstevel@tonic-gate #define	SO_LOCK_WAKEUP_TIME	3000	/* Wakeup time in milliseconds */
930Sstevel@tonic-gate 
940Sstevel@tonic-gate dev_t sockdev;	/* For fsid in getattr */
958194SJack.Meng@Sun.COM int sockfs_defer_nl7c_init = 0;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate struct socklist socklist;
980Sstevel@tonic-gate 
998348SEric.Yu@Sun.COM struct kmem_cache *socket_cache;
1008348SEric.Yu@Sun.COM 
1010Sstevel@tonic-gate static int sockfs_update(kstat_t *, int);
1020Sstevel@tonic-gate static int sockfs_snapshot(kstat_t *, void *, int);
1038348SEric.Yu@Sun.COM extern smod_info_t *sotpi_smod_create(void);
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate extern void sendfile_init();
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate extern void nl7c_init(void);
1080Sstevel@tonic-gate 
1098194SJack.Meng@Sun.COM extern int modrootloaded;
1108194SJack.Meng@Sun.COM 
1110Sstevel@tonic-gate #define	ADRSTRLEN (2 * sizeof (void *) + 1)
1120Sstevel@tonic-gate /*
1130Sstevel@tonic-gate  * kernel structure for passing the sockinfo data back up to the user.
1140Sstevel@tonic-gate  * the strings array allows us to convert AF_UNIX addresses into strings
1150Sstevel@tonic-gate  * with a common method regardless of which n-bit kernel we're running.
1160Sstevel@tonic-gate  */
1170Sstevel@tonic-gate struct k_sockinfo {
1180Sstevel@tonic-gate 	struct sockinfo	ks_si;
1190Sstevel@tonic-gate 	char		ks_straddr[3][ADRSTRLEN];
1200Sstevel@tonic-gate };
1210Sstevel@tonic-gate 
1220Sstevel@tonic-gate /*
1230Sstevel@tonic-gate  * Translate from a device pathname (e.g. "/dev/tcp") to a vnode.
1240Sstevel@tonic-gate  * Returns with the vnode held.
1250Sstevel@tonic-gate  */
1268348SEric.Yu@Sun.COM int
1270Sstevel@tonic-gate sogetvp(char *devpath, vnode_t **vpp, int uioflag)
1280Sstevel@tonic-gate {
1290Sstevel@tonic-gate 	struct snode *csp;
1300Sstevel@tonic-gate 	vnode_t *vp, *dvp;
1310Sstevel@tonic-gate 	major_t maj;
1320Sstevel@tonic-gate 	int error;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE);
1358348SEric.Yu@Sun.COM 
1360Sstevel@tonic-gate 	/*
1370Sstevel@tonic-gate 	 * Lookup the underlying filesystem vnode.
1380Sstevel@tonic-gate 	 */
1390Sstevel@tonic-gate 	error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp);
1400Sstevel@tonic-gate 	if (error)
1410Sstevel@tonic-gate 		return (error);
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	/* Check that it is the correct vnode */
1440Sstevel@tonic-gate 	if (vp->v_type != VCHR) {
1450Sstevel@tonic-gate 		VN_RELE(vp);
1460Sstevel@tonic-gate 		return (ENOTSOCK);
1470Sstevel@tonic-gate 	}
1480Sstevel@tonic-gate 
1490Sstevel@tonic-gate 	/*
1500Sstevel@tonic-gate 	 * If devpath went through devfs, the device should already
1510Sstevel@tonic-gate 	 * be configured. If devpath is a mknod file, however, we
1520Sstevel@tonic-gate 	 * need to make sure the device is properly configured.
1530Sstevel@tonic-gate 	 * To do this, we do something similar to spec_open()
1540Sstevel@tonic-gate 	 * except that we resolve to the minor/leaf level since
1550Sstevel@tonic-gate 	 * we need to return a vnode.
1560Sstevel@tonic-gate 	 */
1570Sstevel@tonic-gate 	csp = VTOS(VTOS(vp)->s_commonvp);
1580Sstevel@tonic-gate 	if (!(csp->s_flag & SDIPSET)) {
1590Sstevel@tonic-gate 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1600Sstevel@tonic-gate 		error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname);
1610Sstevel@tonic-gate 		if (error == 0)
1620Sstevel@tonic-gate 			error = devfs_lookupname(pathname, NULLVPP, &dvp);
1630Sstevel@tonic-gate 		VN_RELE(vp);
1640Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
1650Sstevel@tonic-gate 		if (error != 0)
1660Sstevel@tonic-gate 			return (ENXIO);
1670Sstevel@tonic-gate 		vp = dvp;	/* use the devfs vp */
1680Sstevel@tonic-gate 	}
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate 	/* device is configured at this point */
1710Sstevel@tonic-gate 	maj = getmajor(vp->v_rdev);
1720Sstevel@tonic-gate 	if (!STREAMSTAB(maj)) {
1730Sstevel@tonic-gate 		VN_RELE(vp);
1740Sstevel@tonic-gate 		return (ENOSTR);
1750Sstevel@tonic-gate 	}
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	*vpp = vp;
1780Sstevel@tonic-gate 	return (0);
1790Sstevel@tonic-gate }
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate /*
1820Sstevel@tonic-gate  * Update the accessed, updated, or changed times in an sonode
1830Sstevel@tonic-gate  * with the current time.
1840Sstevel@tonic-gate  *
1850Sstevel@tonic-gate  * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable
1860Sstevel@tonic-gate  * attributes in a fstat call. (They return the current time and 0 for
1870Sstevel@tonic-gate  * all timestamps, respectively.) We maintain the current timestamps
1880Sstevel@tonic-gate  * here primarily so that should sockmod be popped the resulting
1890Sstevel@tonic-gate  * file descriptor will behave like a stream w.r.t. the timestamps.
1900Sstevel@tonic-gate  */
1910Sstevel@tonic-gate void
1920Sstevel@tonic-gate so_update_attrs(struct sonode *so, int flag)
1930Sstevel@tonic-gate {
1940Sstevel@tonic-gate 	time_t now = gethrestime_sec();
1950Sstevel@tonic-gate 
1968348SEric.Yu@Sun.COM 	if (SOCK_IS_NONSTR(so))
1978348SEric.Yu@Sun.COM 		return;
1988348SEric.Yu@Sun.COM 
1990Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
2000Sstevel@tonic-gate 	so->so_flag |= flag;
2010Sstevel@tonic-gate 	if (flag & SOACC)
2028348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_atime = now;
2030Sstevel@tonic-gate 	if (flag & SOMOD)
2048348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_mtime = now;
2050Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2060Sstevel@tonic-gate }
2070Sstevel@tonic-gate 
2088348SEric.Yu@Sun.COM extern so_create_func_t sock_comm_create_function;
2098348SEric.Yu@Sun.COM extern so_destroy_func_t sock_comm_destroy_function;
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * Init function called when sockfs is loaded.
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate int
2140Sstevel@tonic-gate sockinit(int fstype, char *name)
2150Sstevel@tonic-gate {
2160Sstevel@tonic-gate 	static const fs_operation_def_t sock_vfsops_template[] = {
2170Sstevel@tonic-gate 		NULL, NULL
2180Sstevel@tonic-gate 	};
2190Sstevel@tonic-gate 	int error;
2200Sstevel@tonic-gate 	major_t dev;
2210Sstevel@tonic-gate 	char *err_str;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	error = vfs_setfsops(fstype, sock_vfsops_template, NULL);
2240Sstevel@tonic-gate 	if (error != 0) {
2251548Srshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
2261548Srshoaib 		    "sockinit: bad vfs ops template");
2270Sstevel@tonic-gate 		return (error);
2280Sstevel@tonic-gate 	}
2290Sstevel@tonic-gate 
2308348SEric.Yu@Sun.COM 	error = vn_make_ops(name, socket_vnodeops_template,
2318348SEric.Yu@Sun.COM 	    &socket_vnodeops);
2320Sstevel@tonic-gate 	if (error != 0) {
2338348SEric.Yu@Sun.COM 		err_str = "sockinit: bad socket vnode ops template";
2340Sstevel@tonic-gate 		/* vn_make_ops() does not reset socktpi_vnodeops on failure. */
2358348SEric.Yu@Sun.COM 		socket_vnodeops = NULL;
2360Sstevel@tonic-gate 		goto failure;
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2398348SEric.Yu@Sun.COM 	socket_cache = kmem_cache_create("socket_cache",
2408348SEric.Yu@Sun.COM 	    sizeof (struct sonode), 0, sonode_constructor,
2418348SEric.Yu@Sun.COM 	    sonode_destructor, NULL, NULL, NULL, 0);
2420Sstevel@tonic-gate 
2438348SEric.Yu@Sun.COM 	error = socktpi_init();
2443422Snh145002 	if (error != 0) {
2453422Snh145002 		err_str = NULL;
2463422Snh145002 		goto failure;
2473422Snh145002 	}
2483422Snh145002 
2499491SAnders.Persson@Sun.COM 	error = sod_init();
2506707Sbrutus 	if (error != 0) {
2516707Sbrutus 		err_str = NULL;
2526707Sbrutus 		goto failure;
2536707Sbrutus 	}
2546707Sbrutus 
2550Sstevel@tonic-gate 	/*
2568348SEric.Yu@Sun.COM 	 * Set up the default create and destroy functions
2570Sstevel@tonic-gate 	 */
2588348SEric.Yu@Sun.COM 	sock_comm_create_function = socket_sonode_create;
2598348SEric.Yu@Sun.COM 	sock_comm_destroy_function = socket_sonode_destroy;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * Build initial list mapping socket parameters to vnode.
2630Sstevel@tonic-gate 	 */
2648348SEric.Yu@Sun.COM 	smod_init();
2658348SEric.Yu@Sun.COM 	smod_add(sotpi_smod_create());
2668348SEric.Yu@Sun.COM 
2678348SEric.Yu@Sun.COM 	sockparams_init();
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate 	/*
2700Sstevel@tonic-gate 	 * If sockets are needed before init runs /sbin/soconfig
2710Sstevel@tonic-gate 	 * it is possible to preload the sockparams list here using
2720Sstevel@tonic-gate 	 * calls like:
2730Sstevel@tonic-gate 	 *	sockconfig(1,2,3, "/dev/tcp", 0);
2740Sstevel@tonic-gate 	 */
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * Create a unique dev_t for use in so_fsid.
2780Sstevel@tonic-gate 	 */
2790Sstevel@tonic-gate 
2800Sstevel@tonic-gate 	if ((dev = getudev()) == (major_t)-1)
2810Sstevel@tonic-gate 		dev = 0;
2820Sstevel@tonic-gate 	sockdev = makedevice(dev, 0);
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL);
2850Sstevel@tonic-gate 	sendfile_init();
2868194SJack.Meng@Sun.COM 	if (!modrootloaded) {
2878194SJack.Meng@Sun.COM 		sockfs_defer_nl7c_init = 1;
2888194SJack.Meng@Sun.COM 	} else {
2898194SJack.Meng@Sun.COM 		nl7c_init();
2908194SJack.Meng@Sun.COM 	}
2910Sstevel@tonic-gate 
2920Sstevel@tonic-gate 	return (0);
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate failure:
2950Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(fstype);
2968348SEric.Yu@Sun.COM 	if (socket_vnodeops != NULL)
2978348SEric.Yu@Sun.COM 		vn_freevnodeops(socket_vnodeops);
2980Sstevel@tonic-gate 	if (err_str != NULL)
2991548Srshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str);
3000Sstevel@tonic-gate 	return (error);
3010Sstevel@tonic-gate }
3020Sstevel@tonic-gate 
3030Sstevel@tonic-gate /*
3040Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOLOCKED.
3050Sstevel@tonic-gate  */
3060Sstevel@tonic-gate void
3070Sstevel@tonic-gate so_lock_single(struct sonode *so)
3080Sstevel@tonic-gate {
3090Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3100Sstevel@tonic-gate 
3110Sstevel@tonic-gate 	while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) {
3120Sstevel@tonic-gate 		so->so_flag |= SOWANT;
3130Sstevel@tonic-gate 		cv_wait_stop(&so->so_want_cv, &so->so_lock,
3145753Sgww 		    SO_LOCK_WAKEUP_TIME);
3150Sstevel@tonic-gate 	}
3160Sstevel@tonic-gate 	so->so_flag |= SOLOCKED;
3170Sstevel@tonic-gate }
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate /*
3200Sstevel@tonic-gate  * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND.
3210Sstevel@tonic-gate  * Used to clear SOLOCKED or SOASYNC_UNBIND.
3220Sstevel@tonic-gate  */
3230Sstevel@tonic-gate void
3240Sstevel@tonic-gate so_unlock_single(struct sonode *so, int flag)
3250Sstevel@tonic-gate {
3260Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3270Sstevel@tonic-gate 	ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND));
3280Sstevel@tonic-gate 	ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0);
3290Sstevel@tonic-gate 	ASSERT(so->so_flag & flag);
3300Sstevel@tonic-gate 	/*
3318348SEric.Yu@Sun.COM 	 * Process the T_DISCON_IND on sti_discon_ind_mp.
3320Sstevel@tonic-gate 	 *
3330Sstevel@tonic-gate 	 * Call to so_drain_discon_ind will result in so_lock
3340Sstevel@tonic-gate 	 * being dropped and re-acquired later.
3350Sstevel@tonic-gate 	 */
3368348SEric.Yu@Sun.COM 	if (!SOCK_IS_NONSTR(so)) {
3378348SEric.Yu@Sun.COM 		sotpi_info_t *sti = SOTOTPI(so);
3388348SEric.Yu@Sun.COM 
3398348SEric.Yu@Sun.COM 		if (sti->sti_discon_ind_mp != NULL)
3408348SEric.Yu@Sun.COM 			so_drain_discon_ind(so);
3418348SEric.Yu@Sun.COM 	}
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 	if (so->so_flag & SOWANT)
3440Sstevel@tonic-gate 		cv_broadcast(&so->so_want_cv);
3450Sstevel@tonic-gate 	so->so_flag &= ~(SOWANT|flag);
3460Sstevel@tonic-gate }
3470Sstevel@tonic-gate 
3480Sstevel@tonic-gate /*
3490Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOREADLOCKED.
3500Sstevel@tonic-gate  * If the caller wants nonblocking behavior it should set fmode.
3510Sstevel@tonic-gate  */
3520Sstevel@tonic-gate int
3530Sstevel@tonic-gate so_lock_read(struct sonode *so, int fmode)
3540Sstevel@tonic-gate {
3550Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
3580Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
3590Sstevel@tonic-gate 			return (EWOULDBLOCK);
3600Sstevel@tonic-gate 		so->so_flag |= SOWANT;
3610Sstevel@tonic-gate 		cv_wait_stop(&so->so_want_cv, &so->so_lock,
3625753Sgww 		    SO_LOCK_WAKEUP_TIME);
3630Sstevel@tonic-gate 	}
3640Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
3650Sstevel@tonic-gate 	return (0);
3660Sstevel@tonic-gate }
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate /*
3690Sstevel@tonic-gate  * Like so_lock_read above but allows signals.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate int
3720Sstevel@tonic-gate so_lock_read_intr(struct sonode *so, int fmode)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
3770Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
3780Sstevel@tonic-gate 			return (EWOULDBLOCK);
3790Sstevel@tonic-gate 		so->so_flag |= SOWANT;
3800Sstevel@tonic-gate 		if (!cv_wait_sig(&so->so_want_cv, &so->so_lock))
3810Sstevel@tonic-gate 			return (EINTR);
3820Sstevel@tonic-gate 	}
3830Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
3840Sstevel@tonic-gate 	return (0);
3850Sstevel@tonic-gate }
3860Sstevel@tonic-gate 
3870Sstevel@tonic-gate /*
3880Sstevel@tonic-gate  * Caller must hold the mutex. Used to clear SOREADLOCKED,
3890Sstevel@tonic-gate  * set in so_lock_read() or so_lock_read_intr().
3900Sstevel@tonic-gate  */
3910Sstevel@tonic-gate void
3920Sstevel@tonic-gate so_unlock_read(struct sonode *so)
3930Sstevel@tonic-gate {
3940Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3950Sstevel@tonic-gate 	ASSERT(so->so_flag & SOREADLOCKED);
3960Sstevel@tonic-gate 
3970Sstevel@tonic-gate 	if (so->so_flag & SOWANT)
3980Sstevel@tonic-gate 		cv_broadcast(&so->so_want_cv);
3990Sstevel@tonic-gate 	so->so_flag &= ~(SOWANT|SOREADLOCKED);
4000Sstevel@tonic-gate }
4010Sstevel@tonic-gate 
4020Sstevel@tonic-gate /*
4030Sstevel@tonic-gate  * Verify that the specified offset falls within the mblk and
4040Sstevel@tonic-gate  * that the resulting pointer is aligned.
4050Sstevel@tonic-gate  * Returns NULL if not.
4060Sstevel@tonic-gate  */
4070Sstevel@tonic-gate void *
4080Sstevel@tonic-gate sogetoff(mblk_t *mp, t_uscalar_t offset,
4090Sstevel@tonic-gate     t_uscalar_t length, uint_t align_size)
4100Sstevel@tonic-gate {
4110Sstevel@tonic-gate 	uintptr_t ptr1, ptr2;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	ASSERT(mp && mp->b_wptr >= mp->b_rptr);
4140Sstevel@tonic-gate 	ptr1 = (uintptr_t)mp->b_rptr + offset;
4150Sstevel@tonic-gate 	ptr2 = (uintptr_t)ptr1 + length;
4160Sstevel@tonic-gate 	if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) {
4170Sstevel@tonic-gate 		eprintline(0);
4180Sstevel@tonic-gate 		return (NULL);
4190Sstevel@tonic-gate 	}
4200Sstevel@tonic-gate 	if ((ptr1 & (align_size - 1)) != 0) {
4210Sstevel@tonic-gate 		eprintline(0);
4220Sstevel@tonic-gate 		return (NULL);
4230Sstevel@tonic-gate 	}
4240Sstevel@tonic-gate 	return ((void *)ptr1);
4250Sstevel@tonic-gate }
4260Sstevel@tonic-gate 
4270Sstevel@tonic-gate /*
4280Sstevel@tonic-gate  * Return the AF_UNIX underlying filesystem vnode matching a given name.
4290Sstevel@tonic-gate  * Makes sure the sending and the destination sonodes are compatible.
4300Sstevel@tonic-gate  * The vnode is returned held.
4310Sstevel@tonic-gate  *
4320Sstevel@tonic-gate  * The underlying filesystem VSOCK vnode has a v_stream pointer that
4330Sstevel@tonic-gate  * references the actual stream head (hence indirectly the actual sonode).
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate static int
4360Sstevel@tonic-gate so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess,
4370Sstevel@tonic-gate 		vnode_t **vpp)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	vnode_t		*vp;	/* Underlying filesystem vnode */
4407409SRic.Aleshire@Sun.COM 	vnode_t		*rvp;	/* real vnode */
4410Sstevel@tonic-gate 	vnode_t		*svp;	/* sockfs vnode */
4420Sstevel@tonic-gate 	struct sonode	*so2;
4430Sstevel@tonic-gate 	int		error;
4440Sstevel@tonic-gate 
4457242Srh87107 	dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", (void *)so,
4467242Srh87107 	    soun->sun_path));
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
4490Sstevel@tonic-gate 	if (error) {
4500Sstevel@tonic-gate 		eprintsoline(so, error);
4510Sstevel@tonic-gate 		return (error);
4520Sstevel@tonic-gate 	}
4537409SRic.Aleshire@Sun.COM 
4547409SRic.Aleshire@Sun.COM 	/*
4557409SRic.Aleshire@Sun.COM 	 * Traverse lofs mounts get the real vnode
4567409SRic.Aleshire@Sun.COM 	 */
4577409SRic.Aleshire@Sun.COM 	if (VOP_REALVP(vp, &rvp, NULL) == 0) {
4587409SRic.Aleshire@Sun.COM 		VN_HOLD(rvp);		/* hold the real vnode */
4597409SRic.Aleshire@Sun.COM 		VN_RELE(vp);		/* release hold from lookup */
4607409SRic.Aleshire@Sun.COM 		vp = rvp;
4617409SRic.Aleshire@Sun.COM 	}
4627409SRic.Aleshire@Sun.COM 
4630Sstevel@tonic-gate 	if (vp->v_type != VSOCK) {
4640Sstevel@tonic-gate 		error = ENOTSOCK;
4650Sstevel@tonic-gate 		eprintsoline(so, error);
4660Sstevel@tonic-gate 		goto done2;
4670Sstevel@tonic-gate 	}
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	if (checkaccess) {
4700Sstevel@tonic-gate 		/*
4710Sstevel@tonic-gate 		 * Check that we have permissions to access the destination
4720Sstevel@tonic-gate 		 * vnode. This check is not done in BSD but it is required
4730Sstevel@tonic-gate 		 * by X/Open.
4740Sstevel@tonic-gate 		 */
4755331Samw 		if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL)) {
4760Sstevel@tonic-gate 			eprintsoline(so, error);
4770Sstevel@tonic-gate 			goto done2;
4780Sstevel@tonic-gate 		}
4790Sstevel@tonic-gate 	}
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate 	/*
4820Sstevel@tonic-gate 	 * Check if the remote socket has been closed.
4830Sstevel@tonic-gate 	 *
4840Sstevel@tonic-gate 	 * Synchronize with vn_rele_stream by holding v_lock while traversing
4850Sstevel@tonic-gate 	 * v_stream->sd_vnode.
4860Sstevel@tonic-gate 	 */
4870Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
4880Sstevel@tonic-gate 	if (vp->v_stream == NULL) {
4890Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4900Sstevel@tonic-gate 		if (so->so_type == SOCK_DGRAM)
4910Sstevel@tonic-gate 			error = EDESTADDRREQ;
4920Sstevel@tonic-gate 		else
4930Sstevel@tonic-gate 			error = ECONNREFUSED;
4940Sstevel@tonic-gate 
4950Sstevel@tonic-gate 		eprintsoline(so, error);
4960Sstevel@tonic-gate 		goto done2;
4970Sstevel@tonic-gate 	}
4980Sstevel@tonic-gate 	ASSERT(vp->v_stream->sd_vnode);
4990Sstevel@tonic-gate 	svp = vp->v_stream->sd_vnode;
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * holding v_lock on underlying filesystem vnode and acquiring
5020Sstevel@tonic-gate 	 * it on sockfs vnode. Assumes that no code ever attempts to
5030Sstevel@tonic-gate 	 * acquire these locks in the reverse order.
5040Sstevel@tonic-gate 	 */
5050Sstevel@tonic-gate 	VN_HOLD(svp);
5060Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
5070Sstevel@tonic-gate 
5080Sstevel@tonic-gate 	if (svp->v_type != VSOCK) {
5090Sstevel@tonic-gate 		error = ENOTSOCK;
5100Sstevel@tonic-gate 		eprintsoline(so, error);
5110Sstevel@tonic-gate 		goto done;
5120Sstevel@tonic-gate 	}
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	so2 = VTOSO(svp);
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	if (so->so_type != so2->so_type) {
5170Sstevel@tonic-gate 		error = EPROTOTYPE;
5180Sstevel@tonic-gate 		eprintsoline(so, error);
5190Sstevel@tonic-gate 		goto done;
5200Sstevel@tonic-gate 	}
5210Sstevel@tonic-gate 
5220Sstevel@tonic-gate 	VN_RELE(svp);
5230Sstevel@tonic-gate 	*vpp = vp;
5240Sstevel@tonic-gate 	return (0);
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate done:
5270Sstevel@tonic-gate 	VN_RELE(svp);
5280Sstevel@tonic-gate done2:
5290Sstevel@tonic-gate 	VN_RELE(vp);
5300Sstevel@tonic-gate 	return (error);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * Verify peer address for connect and sendto/sendmsg.
5350Sstevel@tonic-gate  * Since sendto/sendmsg would not get synchronous errors from the transport
5360Sstevel@tonic-gate  * provider we have to do these ugly checks in the socket layer to
5370Sstevel@tonic-gate  * preserve compatibility with SunOS 4.X.
5380Sstevel@tonic-gate  */
5390Sstevel@tonic-gate int
5400Sstevel@tonic-gate so_addr_verify(struct sonode *so, const struct sockaddr *name,
5410Sstevel@tonic-gate     socklen_t namelen)
5420Sstevel@tonic-gate {
5430Sstevel@tonic-gate 	int		family;
5440Sstevel@tonic-gate 
5457240Srh87107 	dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n",
5467240Srh87107 	    (void *)so, (void *)name, namelen));
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	ASSERT(name != NULL);
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	family = so->so_family;
5510Sstevel@tonic-gate 	switch (family) {
5520Sstevel@tonic-gate 	case AF_INET:
5530Sstevel@tonic-gate 		if (name->sa_family != family) {
5540Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5550Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5560Sstevel@tonic-gate 		}
5570Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
5580Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
5590Sstevel@tonic-gate 			return (EINVAL);
5600Sstevel@tonic-gate 		}
5610Sstevel@tonic-gate 		break;
5620Sstevel@tonic-gate 	case AF_INET6: {
5630Sstevel@tonic-gate #ifdef DEBUG
5640Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
5650Sstevel@tonic-gate #endif /* DEBUG */
5660Sstevel@tonic-gate 
5670Sstevel@tonic-gate 		if (name->sa_family != family) {
5680Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5690Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5700Sstevel@tonic-gate 		}
5710Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
5720Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
5730Sstevel@tonic-gate 			return (EINVAL);
5740Sstevel@tonic-gate 		}
5750Sstevel@tonic-gate #ifdef DEBUG
5760Sstevel@tonic-gate 		/* Verify that apps don't forget to clear sin6_scope_id etc */
5770Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)name;
5780Sstevel@tonic-gate 		if (sin6->sin6_scope_id != 0 &&
5790Sstevel@tonic-gate 		    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
5801548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
5810Sstevel@tonic-gate 			    "connect/send* with uninitialized sin6_scope_id "
5820Sstevel@tonic-gate 			    "(%d) on socket. Pid = %d\n",
5830Sstevel@tonic-gate 			    (int)sin6->sin6_scope_id, (int)curproc->p_pid);
5840Sstevel@tonic-gate 		}
5850Sstevel@tonic-gate #endif /* DEBUG */
5860Sstevel@tonic-gate 		break;
5870Sstevel@tonic-gate 	}
5880Sstevel@tonic-gate 	case AF_UNIX:
5898348SEric.Yu@Sun.COM 		if (SOTOTPI(so)->sti_faddr_noxlate) {
5900Sstevel@tonic-gate 			return (0);
5910Sstevel@tonic-gate 		}
5920Sstevel@tonic-gate 		if (namelen < (socklen_t)sizeof (short)) {
5930Sstevel@tonic-gate 			eprintsoline(so, ENOENT);
5940Sstevel@tonic-gate 			return (ENOENT);
5950Sstevel@tonic-gate 		}
5960Sstevel@tonic-gate 		if (name->sa_family != family) {
5970Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5980Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5990Sstevel@tonic-gate 		}
6000Sstevel@tonic-gate 		/* MAXPATHLEN + soun_family + nul termination */
6010Sstevel@tonic-gate 		if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
6020Sstevel@tonic-gate 			eprintsoline(so, ENAMETOOLONG);
6030Sstevel@tonic-gate 			return (ENAMETOOLONG);
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 
6060Sstevel@tonic-gate 		break;
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 	default:
6090Sstevel@tonic-gate 		/*
6100Sstevel@tonic-gate 		 * Default is don't do any length or sa_family check
6110Sstevel@tonic-gate 		 * to allow non-sockaddr style addresses.
6120Sstevel@tonic-gate 		 */
6130Sstevel@tonic-gate 		break;
6140Sstevel@tonic-gate 	}
6150Sstevel@tonic-gate 
6160Sstevel@tonic-gate 	return (0);
6170Sstevel@tonic-gate }
6180Sstevel@tonic-gate 
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate /*
6210Sstevel@tonic-gate  * Translate an AF_UNIX sockaddr_un to the transport internal name.
6220Sstevel@tonic-gate  * Assumes caller has called so_addr_verify first.
6230Sstevel@tonic-gate  */
6240Sstevel@tonic-gate /*ARGSUSED*/
6250Sstevel@tonic-gate int
6260Sstevel@tonic-gate so_ux_addr_xlate(struct sonode *so, struct sockaddr *name,
6270Sstevel@tonic-gate     socklen_t namelen, int checkaccess,
6280Sstevel@tonic-gate     void **addrp, socklen_t *addrlenp)
6290Sstevel@tonic-gate {
6300Sstevel@tonic-gate 	int			error;
6310Sstevel@tonic-gate 	struct sockaddr_un	*soun;
6320Sstevel@tonic-gate 	vnode_t			*vp;
6330Sstevel@tonic-gate 	void			*addr;
6340Sstevel@tonic-gate 	socklen_t		addrlen;
6358348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
6360Sstevel@tonic-gate 
6370Sstevel@tonic-gate 	dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n",
6387240Srh87107 	    (void *)so, (void *)name, namelen, checkaccess));
6390Sstevel@tonic-gate 
6400Sstevel@tonic-gate 	ASSERT(name != NULL);
6410Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
6428348SEric.Yu@Sun.COM 	ASSERT(!sti->sti_faddr_noxlate);
6430Sstevel@tonic-gate 	ASSERT(namelen >= (socklen_t)sizeof (short));
6440Sstevel@tonic-gate 	ASSERT(name->sa_family == AF_UNIX);
6450Sstevel@tonic-gate 	soun = (struct sockaddr_un *)name;
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * Lookup vnode for the specified path name and verify that
6480Sstevel@tonic-gate 	 * it is a socket.
6490Sstevel@tonic-gate 	 */
6500Sstevel@tonic-gate 	error = so_ux_lookup(so, soun, checkaccess, &vp);
6510Sstevel@tonic-gate 	if (error) {
6520Sstevel@tonic-gate 		eprintsoline(so, error);
6530Sstevel@tonic-gate 		return (error);
6540Sstevel@tonic-gate 	}
6550Sstevel@tonic-gate 	/*
6560Sstevel@tonic-gate 	 * Use the address of the peer vnode as the address to send
6570Sstevel@tonic-gate 	 * to. We release the peer vnode here. In case it has been
6580Sstevel@tonic-gate 	 * closed by the time the T_CONN_REQ or T_UNIDATA_REQ reaches the
6590Sstevel@tonic-gate 	 * transport the message will get an error or be dropped.
6600Sstevel@tonic-gate 	 */
6618348SEric.Yu@Sun.COM 	sti->sti_ux_faddr.soua_vp = vp;
6628348SEric.Yu@Sun.COM 	sti->sti_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT;
6638348SEric.Yu@Sun.COM 	addr = &sti->sti_ux_faddr;
6648348SEric.Yu@Sun.COM 	addrlen = (socklen_t)sizeof (sti->sti_ux_faddr);
6657240Srh87107 	dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n",
6667240Srh87107 	    addrlen, (void *)vp));
6670Sstevel@tonic-gate 	VN_RELE(vp);
6680Sstevel@tonic-gate 	*addrp = addr;
6690Sstevel@tonic-gate 	*addrlenp = (socklen_t)addrlen;
6700Sstevel@tonic-gate 	return (0);
6710Sstevel@tonic-gate }
6720Sstevel@tonic-gate 
6730Sstevel@tonic-gate /*
6740Sstevel@tonic-gate  * Esballoc free function for messages that contain SO_FILEP option.
6750Sstevel@tonic-gate  * Decrement the reference count on the file pointers using closef.
6760Sstevel@tonic-gate  */
6770Sstevel@tonic-gate void
6780Sstevel@tonic-gate fdbuf_free(struct fdbuf *fdbuf)
6790Sstevel@tonic-gate {
6800Sstevel@tonic-gate 	int	i;
6810Sstevel@tonic-gate 	struct file *fp;
6820Sstevel@tonic-gate 
6830Sstevel@tonic-gate 	dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd));
6840Sstevel@tonic-gate 	for (i = 0; i < fdbuf->fd_numfd; i++) {
6850Sstevel@tonic-gate 		/*
6860Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
6870Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
6880Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
6890Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
6900Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
6910Sstevel@tonic-gate 		 */
6920Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
6937240Srh87107 		dprint(1, ("fdbuf_free: [%d] = %p\n", i, (void *)fp));
6940Sstevel@tonic-gate 		(void) closef(fp);
6950Sstevel@tonic-gate 	}
6960Sstevel@tonic-gate 	if (fdbuf->fd_ebuf != NULL)
6970Sstevel@tonic-gate 		kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen);
6980Sstevel@tonic-gate 	kmem_free(fdbuf, fdbuf->fd_size);
6990Sstevel@tonic-gate }
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate /*
702455Smeem  * Allocate an esballoc'ed message for AF_UNIX file descriptor passing.
703455Smeem  * Waits if memory is not available.
7040Sstevel@tonic-gate  */
7050Sstevel@tonic-gate mblk_t *
7060Sstevel@tonic-gate fdbuf_allocmsg(int size, struct fdbuf *fdbuf)
7070Sstevel@tonic-gate {
708455Smeem 	uchar_t	*buf;
7090Sstevel@tonic-gate 	mblk_t	*mp;
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd));
7120Sstevel@tonic-gate 	buf = kmem_alloc(size, KM_SLEEP);
7130Sstevel@tonic-gate 	fdbuf->fd_ebuf = (caddr_t)buf;
7140Sstevel@tonic-gate 	fdbuf->fd_ebuflen = size;
7150Sstevel@tonic-gate 	fdbuf->fd_frtn.free_func = fdbuf_free;
7160Sstevel@tonic-gate 	fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf;
7170Sstevel@tonic-gate 
718455Smeem 	mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn);
7190Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
7200Sstevel@tonic-gate 	return (mp);
7210Sstevel@tonic-gate }
7220Sstevel@tonic-gate 
7230Sstevel@tonic-gate /*
7240Sstevel@tonic-gate  * Extract file descriptors from a fdbuf.
7250Sstevel@tonic-gate  * Return list in rights/rightslen.
7260Sstevel@tonic-gate  */
7270Sstevel@tonic-gate /*ARGSUSED*/
7280Sstevel@tonic-gate static int
7290Sstevel@tonic-gate fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen)
7300Sstevel@tonic-gate {
7310Sstevel@tonic-gate 	int	i, fd;
7320Sstevel@tonic-gate 	int	*rp;
7330Sstevel@tonic-gate 	struct file *fp;
7340Sstevel@tonic-gate 	int	numfd;
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	dprint(1, ("fdbuf_extract: %d fds, len %d\n",
7375753Sgww 	    fdbuf->fd_numfd, rightslen));
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	numfd = fdbuf->fd_numfd;
7400Sstevel@tonic-gate 	ASSERT(rightslen == numfd * (int)sizeof (int));
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/*
7430Sstevel@tonic-gate 	 * Allocate a file descriptor and increment the f_count.
7440Sstevel@tonic-gate 	 * The latter is needed since we always call fdbuf_free
7450Sstevel@tonic-gate 	 * which performs a closef.
7460Sstevel@tonic-gate 	 */
7470Sstevel@tonic-gate 	rp = (int *)rights;
7480Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
7490Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1)
7500Sstevel@tonic-gate 			goto cleanup;
7510Sstevel@tonic-gate 		/*
7520Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
7530Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
7540Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
7550Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
7560Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
7570Sstevel@tonic-gate 		 */
7580Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
7590Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
7600Sstevel@tonic-gate 		fp->f_count++;
7610Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
7620Sstevel@tonic-gate 		setf(fd, fp);
7630Sstevel@tonic-gate 		*rp++ = fd;
7640Sstevel@tonic-gate 		if (audit_active)
7650Sstevel@tonic-gate 			audit_fdrecv(fd, fp);
7660Sstevel@tonic-gate 		dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
7677240Srh87107 		    i, fd, (void *)fp, fp->f_count));
7680Sstevel@tonic-gate 	}
7690Sstevel@tonic-gate 	return (0);
7700Sstevel@tonic-gate 
7710Sstevel@tonic-gate cleanup:
7720Sstevel@tonic-gate 	/*
7730Sstevel@tonic-gate 	 * Undo whatever partial work the loop above has done.
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 	{
7760Sstevel@tonic-gate 		int j;
7770Sstevel@tonic-gate 
7780Sstevel@tonic-gate 		rp = (int *)rights;
7790Sstevel@tonic-gate 		for (j = 0; j < i; j++) {
7800Sstevel@tonic-gate 			dprint(0,
7810Sstevel@tonic-gate 			    ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
7820Sstevel@tonic-gate 			(void) closeandsetf(*rp++, NULL);
7830Sstevel@tonic-gate 		}
7840Sstevel@tonic-gate 	}
7850Sstevel@tonic-gate 
7860Sstevel@tonic-gate 	return (EMFILE);
7870Sstevel@tonic-gate }
7880Sstevel@tonic-gate 
7890Sstevel@tonic-gate /*
7900Sstevel@tonic-gate  * Insert file descriptors into an fdbuf.
7910Sstevel@tonic-gate  * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
7920Sstevel@tonic-gate  * by calling fdbuf_free().
7930Sstevel@tonic-gate  */
7940Sstevel@tonic-gate int
7950Sstevel@tonic-gate fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
7960Sstevel@tonic-gate {
7970Sstevel@tonic-gate 	int		numfd, i;
7980Sstevel@tonic-gate 	int		*fds;
7990Sstevel@tonic-gate 	struct file	*fp;
8000Sstevel@tonic-gate 	struct fdbuf	*fdbuf;
8010Sstevel@tonic-gate 	int		fdbufsize;
8020Sstevel@tonic-gate 
8030Sstevel@tonic-gate 	dprint(1, ("fdbuf_create: len %d\n", rightslen));
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
8080Sstevel@tonic-gate 	fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
8090Sstevel@tonic-gate 	fdbuf->fd_size = fdbufsize;
8100Sstevel@tonic-gate 	fdbuf->fd_numfd = 0;
8110Sstevel@tonic-gate 	fdbuf->fd_ebuf = NULL;
8120Sstevel@tonic-gate 	fdbuf->fd_ebuflen = 0;
8130Sstevel@tonic-gate 	fds = (int *)rights;
8140Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
8150Sstevel@tonic-gate 		if ((fp = getf(fds[i])) == NULL) {
8160Sstevel@tonic-gate 			fdbuf_free(fdbuf);
8170Sstevel@tonic-gate 			return (EBADF);
8180Sstevel@tonic-gate 		}
8190Sstevel@tonic-gate 		dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
8207240Srh87107 		    i, fds[i], (void *)fp, fp->f_count));
8210Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
8220Sstevel@tonic-gate 		fp->f_count++;
8230Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
8240Sstevel@tonic-gate 		/*
8250Sstevel@tonic-gate 		 * The maximum alignment for fdbuf (or any option header
8260Sstevel@tonic-gate 		 * and its value) it 4 bytes. On a LP64 kernel, the alignment
8270Sstevel@tonic-gate 		 * is not sufficient for pointers (fd_fds in this case). Since
8280Sstevel@tonic-gate 		 * we just did a kmem_alloc (we get a double word alignment),
8290Sstevel@tonic-gate 		 * we don't need to do anything on the send side (we loose
8300Sstevel@tonic-gate 		 * the double word alignment because fdbuf goes after an
8310Sstevel@tonic-gate 		 * option header (eg T_unitdata_req) which is only 4 byte
8320Sstevel@tonic-gate 		 * aligned). We take care of this when we extract the file
8330Sstevel@tonic-gate 		 * descriptor in fdbuf_extract or fdbuf_free.
8340Sstevel@tonic-gate 		 */
8350Sstevel@tonic-gate 		fdbuf->fd_fds[i] = fp;
8360Sstevel@tonic-gate 		fdbuf->fd_numfd++;
8370Sstevel@tonic-gate 		releasef(fds[i]);
8380Sstevel@tonic-gate 		if (audit_active)
8390Sstevel@tonic-gate 			audit_fdsend(fds[i], fp, 0);
8400Sstevel@tonic-gate 	}
8410Sstevel@tonic-gate 	*fdbufp = fdbuf;
8420Sstevel@tonic-gate 	return (0);
8430Sstevel@tonic-gate }
8440Sstevel@tonic-gate 
8450Sstevel@tonic-gate static int
8460Sstevel@tonic-gate fdbuf_optlen(int rightslen)
8470Sstevel@tonic-gate {
8480Sstevel@tonic-gate 	int numfd;
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
8530Sstevel@tonic-gate }
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate static t_uscalar_t
8560Sstevel@tonic-gate fdbuf_cmsglen(int fdbuflen)
8570Sstevel@tonic-gate {
8580Sstevel@tonic-gate 	return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
8590Sstevel@tonic-gate 	    (int)sizeof (struct file *) * (int)sizeof (int));
8600Sstevel@tonic-gate }
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate /*
8640Sstevel@tonic-gate  * Return non-zero if the mblk and fdbuf are consistent.
8650Sstevel@tonic-gate  */
8660Sstevel@tonic-gate static int
8670Sstevel@tonic-gate fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
8680Sstevel@tonic-gate {
8690Sstevel@tonic-gate 	if (fdbuflen >= FDBUF_HDRSIZE &&
8700Sstevel@tonic-gate 	    fdbuflen == fdbuf->fd_size) {
8710Sstevel@tonic-gate 		frtn_t *frp = mp->b_datap->db_frtnp;
8720Sstevel@tonic-gate 		/*
8730Sstevel@tonic-gate 		 * Check that the SO_FILEP portion of the
8740Sstevel@tonic-gate 		 * message has not been modified by
8750Sstevel@tonic-gate 		 * the loopback transport. The sending sockfs generates
8760Sstevel@tonic-gate 		 * a message that is esballoc'ed with the free function
8770Sstevel@tonic-gate 		 * being fdbuf_free() and where free_arg contains the
8780Sstevel@tonic-gate 		 * identical information as the SO_FILEP content.
8790Sstevel@tonic-gate 		 *
8800Sstevel@tonic-gate 		 * If any of these constraints are not satisfied we
8810Sstevel@tonic-gate 		 * silently ignore the option.
8820Sstevel@tonic-gate 		 */
8830Sstevel@tonic-gate 		ASSERT(mp);
8840Sstevel@tonic-gate 		if (frp != NULL &&
8850Sstevel@tonic-gate 		    frp->free_func == fdbuf_free &&
8860Sstevel@tonic-gate 		    frp->free_arg != NULL &&
8870Sstevel@tonic-gate 		    bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
8880Sstevel@tonic-gate 			dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
8897240Srh87107 			    (void *)fdbuf, fdbuflen));
8900Sstevel@tonic-gate 			return (1);
8910Sstevel@tonic-gate 		} else {
8921548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
8930Sstevel@tonic-gate 			    "sockfs: mismatched fdbuf content (%p)",
8940Sstevel@tonic-gate 			    (void *)mp);
8950Sstevel@tonic-gate 			return (0);
8960Sstevel@tonic-gate 		}
8970Sstevel@tonic-gate 	} else {
8981548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
8990Sstevel@tonic-gate 		    "sockfs: mismatched fdbuf len %d, %d\n",
9000Sstevel@tonic-gate 		    fdbuflen, fdbuf->fd_size);
9010Sstevel@tonic-gate 		return (0);
9020Sstevel@tonic-gate 	}
9030Sstevel@tonic-gate }
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate /*
9060Sstevel@tonic-gate  * When the file descriptors returned by sorecvmsg can not be passed
9070Sstevel@tonic-gate  * to the application this routine will cleanup the references on
9080Sstevel@tonic-gate  * the files. Start at startoff bytes into the buffer.
9090Sstevel@tonic-gate  */
9100Sstevel@tonic-gate static void
9110Sstevel@tonic-gate close_fds(void *fdbuf, int fdbuflen, int startoff)
9120Sstevel@tonic-gate {
9130Sstevel@tonic-gate 	int *fds = (int *)fdbuf;
9140Sstevel@tonic-gate 	int numfd = fdbuflen / (int)sizeof (int);
9150Sstevel@tonic-gate 	int i;
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate 	dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
9200Sstevel@tonic-gate 		if (startoff < 0)
9210Sstevel@tonic-gate 			startoff = 0;
9220Sstevel@tonic-gate 		if (startoff < (int)sizeof (int)) {
9230Sstevel@tonic-gate 			/*
9240Sstevel@tonic-gate 			 * This file descriptor is partially or fully after
9250Sstevel@tonic-gate 			 * the offset
9260Sstevel@tonic-gate 			 */
9270Sstevel@tonic-gate 			dprint(0,
9280Sstevel@tonic-gate 			    ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
9290Sstevel@tonic-gate 			(void) closeandsetf(fds[i], NULL);
9300Sstevel@tonic-gate 		}
9310Sstevel@tonic-gate 		startoff -= (int)sizeof (int);
9320Sstevel@tonic-gate 	}
9330Sstevel@tonic-gate }
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate /*
9360Sstevel@tonic-gate  * Close all file descriptors contained in the control part starting at
9370Sstevel@tonic-gate  * the startoffset.
9380Sstevel@tonic-gate  */
9390Sstevel@tonic-gate void
9400Sstevel@tonic-gate so_closefds(void *control, t_uscalar_t controllen, int oldflg,
9410Sstevel@tonic-gate     int startoff)
9420Sstevel@tonic-gate {
9430Sstevel@tonic-gate 	struct cmsghdr *cmsg;
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 	if (control == NULL)
9460Sstevel@tonic-gate 		return;
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate 	if (oldflg) {
9490Sstevel@tonic-gate 		close_fds(control, controllen, startoff);
9500Sstevel@tonic-gate 		return;
9510Sstevel@tonic-gate 	}
9520Sstevel@tonic-gate 	/* Scan control part for file descriptors. */
9530Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
9540Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
9550Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
9560Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
9570Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
9580Sstevel@tonic-gate 			close_fds(CMSG_CONTENT(cmsg),
9590Sstevel@tonic-gate 			    (int)CMSG_CONTENTLEN(cmsg),
9600Sstevel@tonic-gate 			    startoff - (int)sizeof (struct cmsghdr));
9610Sstevel@tonic-gate 		}
9620Sstevel@tonic-gate 		startoff -= cmsg->cmsg_len;
9630Sstevel@tonic-gate 	}
9640Sstevel@tonic-gate }
9650Sstevel@tonic-gate 
9660Sstevel@tonic-gate /*
9670Sstevel@tonic-gate  * Returns a pointer/length for the file descriptors contained
9680Sstevel@tonic-gate  * in the control buffer. Returns with *fdlenp == -1 if there are no
9690Sstevel@tonic-gate  * file descriptor options present. This is different than there being
9700Sstevel@tonic-gate  * a zero-length file descriptor option.
9710Sstevel@tonic-gate  * Fail if there are multiple SCM_RIGHT cmsgs.
9720Sstevel@tonic-gate  */
9730Sstevel@tonic-gate int
9740Sstevel@tonic-gate so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
9750Sstevel@tonic-gate     void **fdsp, int *fdlenp)
9760Sstevel@tonic-gate {
9770Sstevel@tonic-gate 	struct cmsghdr *cmsg;
9780Sstevel@tonic-gate 	void *fds;
9790Sstevel@tonic-gate 	int fdlen;
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	if (control == NULL) {
9820Sstevel@tonic-gate 		*fdsp = NULL;
9830Sstevel@tonic-gate 		*fdlenp = -1;
9840Sstevel@tonic-gate 		return (0);
9850Sstevel@tonic-gate 	}
9860Sstevel@tonic-gate 
9870Sstevel@tonic-gate 	if (oldflg) {
9880Sstevel@tonic-gate 		*fdsp = control;
9890Sstevel@tonic-gate 		if (controllen == 0)
9900Sstevel@tonic-gate 			*fdlenp = -1;
9910Sstevel@tonic-gate 		else
9920Sstevel@tonic-gate 			*fdlenp = controllen;
9930Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
9940Sstevel@tonic-gate 		return (0);
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	fds = NULL;
9980Sstevel@tonic-gate 	fdlen = 0;
9990Sstevel@tonic-gate 
10000Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10010Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10020Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10030Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10040Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
10050Sstevel@tonic-gate 			if (fds != NULL)
10060Sstevel@tonic-gate 				return (EINVAL);
10070Sstevel@tonic-gate 			fds = CMSG_CONTENT(cmsg);
10080Sstevel@tonic-gate 			fdlen = (int)CMSG_CONTENTLEN(cmsg);
1009408Skrgopi 			dprint(1, ("so_getfdopt: new %lu\n",
10105753Sgww 			    (size_t)CMSG_CONTENTLEN(cmsg)));
10110Sstevel@tonic-gate 		}
10120Sstevel@tonic-gate 	}
10130Sstevel@tonic-gate 	if (fds == NULL) {
10140Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: NONE\n"));
10150Sstevel@tonic-gate 		*fdlenp = -1;
10160Sstevel@tonic-gate 	} else
10170Sstevel@tonic-gate 		*fdlenp = fdlen;
10180Sstevel@tonic-gate 	*fdsp = fds;
10190Sstevel@tonic-gate 	return (0);
10200Sstevel@tonic-gate }
10210Sstevel@tonic-gate 
10220Sstevel@tonic-gate /*
10230Sstevel@tonic-gate  * Return the length of the options including any file descriptor options.
10240Sstevel@tonic-gate  */
10250Sstevel@tonic-gate t_uscalar_t
10260Sstevel@tonic-gate so_optlen(void *control, t_uscalar_t controllen, int oldflg)
10270Sstevel@tonic-gate {
10280Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10290Sstevel@tonic-gate 	t_uscalar_t optlen = 0;
10300Sstevel@tonic-gate 	t_uscalar_t len;
10310Sstevel@tonic-gate 
10320Sstevel@tonic-gate 	if (control == NULL)
10330Sstevel@tonic-gate 		return (0);
10340Sstevel@tonic-gate 
10350Sstevel@tonic-gate 	if (oldflg)
10360Sstevel@tonic-gate 		return ((t_uscalar_t)(sizeof (struct T_opthdr) +
10370Sstevel@tonic-gate 		    fdbuf_optlen(controllen)));
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10400Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10410Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10420Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10430Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
10440Sstevel@tonic-gate 			len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
10450Sstevel@tonic-gate 		} else {
10460Sstevel@tonic-gate 			len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
10470Sstevel@tonic-gate 		}
10480Sstevel@tonic-gate 		optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
10490Sstevel@tonic-gate 		    sizeof (struct T_opthdr));
10500Sstevel@tonic-gate 	}
10510Sstevel@tonic-gate 	dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
10525753Sgww 	    controllen, oldflg, optlen));
10530Sstevel@tonic-gate 	return (optlen);
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate /*
10570Sstevel@tonic-gate  * Copy options from control to the mblk. Skip any file descriptor options.
10580Sstevel@tonic-gate  */
10590Sstevel@tonic-gate void
10600Sstevel@tonic-gate so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
10610Sstevel@tonic-gate {
10620Sstevel@tonic-gate 	struct T_opthdr toh;
10630Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10640Sstevel@tonic-gate 
10650Sstevel@tonic-gate 	if (control == NULL)
10660Sstevel@tonic-gate 		return;
10670Sstevel@tonic-gate 
10680Sstevel@tonic-gate 	if (oldflg) {
10690Sstevel@tonic-gate 		/* No real options - caller has handled file descriptors */
10700Sstevel@tonic-gate 		return;
10710Sstevel@tonic-gate 	}
10720Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10730Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10740Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10750Sstevel@tonic-gate 		/*
10760Sstevel@tonic-gate 		 * Note: The caller handles file descriptors prior
10770Sstevel@tonic-gate 		 * to calling this function.
10780Sstevel@tonic-gate 		 */
10790Sstevel@tonic-gate 		t_uscalar_t len;
10800Sstevel@tonic-gate 
10810Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10820Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS)
10830Sstevel@tonic-gate 			continue;
10840Sstevel@tonic-gate 
10850Sstevel@tonic-gate 		len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
10860Sstevel@tonic-gate 		toh.level = cmsg->cmsg_level;
10870Sstevel@tonic-gate 		toh.name = cmsg->cmsg_type;
10880Sstevel@tonic-gate 		toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
10890Sstevel@tonic-gate 		toh.status = 0;
10900Sstevel@tonic-gate 
10910Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
10920Sstevel@tonic-gate 		soappendmsg(mp, CMSG_CONTENT(cmsg), len);
10930Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
10940Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
10950Sstevel@tonic-gate 	}
10960Sstevel@tonic-gate }
10970Sstevel@tonic-gate 
10980Sstevel@tonic-gate /*
10990Sstevel@tonic-gate  * Return the length of the control message derived from the options.
11000Sstevel@tonic-gate  * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
11010Sstevel@tonic-gate  * When oldflg is set only include SO_FILEP.
11022280Sgt145670  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
11032280Sgt145670  * allocates the space that so_opt2cmsg fills. If one changes, the other should
11042280Sgt145670  * also be checked for any possible impacts.
11050Sstevel@tonic-gate  */
11060Sstevel@tonic-gate t_uscalar_t
11070Sstevel@tonic-gate so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate 	t_uscalar_t cmsglen = 0;
11100Sstevel@tonic-gate 	struct T_opthdr *tohp;
11110Sstevel@tonic-gate 	t_uscalar_t len;
11120Sstevel@tonic-gate 	t_uscalar_t last_roundup = 0;
11130Sstevel@tonic-gate 
11140Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
11170Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
11180Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
11190Sstevel@tonic-gate 		dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
11205753Sgww 		    tohp->level, tohp->name, tohp->len));
11210Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
11220Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
11230Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
11240Sstevel@tonic-gate 			continue;
11250Sstevel@tonic-gate 		}
11260Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
11270Sstevel@tonic-gate 			struct fdbuf *fdbuf;
11280Sstevel@tonic-gate 			int fdbuflen;
11290Sstevel@tonic-gate 
11300Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
11310Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
11340Sstevel@tonic-gate 				continue;
11350Sstevel@tonic-gate 			if (oldflg) {
11360Sstevel@tonic-gate 				cmsglen += fdbuf_cmsglen(fdbuflen);
11370Sstevel@tonic-gate 				continue;
11380Sstevel@tonic-gate 			}
11390Sstevel@tonic-gate 			len = fdbuf_cmsglen(fdbuflen);
11402280Sgt145670 		} else if (tohp->level == SOL_SOCKET &&
11412280Sgt145670 		    tohp->name == SCM_TIMESTAMP) {
11422280Sgt145670 			if (oldflg)
11432280Sgt145670 				continue;
11442280Sgt145670 
11452280Sgt145670 			if (get_udatamodel() == DATAMODEL_NATIVE) {
11462280Sgt145670 				len = sizeof (struct timeval);
11472280Sgt145670 			} else {
11482280Sgt145670 				len = sizeof (struct timeval32);
11492280Sgt145670 			}
11500Sstevel@tonic-gate 		} else {
11510Sstevel@tonic-gate 			if (oldflg)
11520Sstevel@tonic-gate 				continue;
11530Sstevel@tonic-gate 			len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
11540Sstevel@tonic-gate 		}
11550Sstevel@tonic-gate 		/*
11562280Sgt145670 		 * Exclude roundup for last option to not set
11570Sstevel@tonic-gate 		 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
11580Sstevel@tonic-gate 		 */
11590Sstevel@tonic-gate 		last_roundup = (t_uscalar_t)
11600Sstevel@tonic-gate 		    (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
11610Sstevel@tonic-gate 		    (len + (int)sizeof (struct cmsghdr)));
11620Sstevel@tonic-gate 		cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
11630Sstevel@tonic-gate 		    last_roundup;
11640Sstevel@tonic-gate 	}
11650Sstevel@tonic-gate 	cmsglen -= last_roundup;
11660Sstevel@tonic-gate 	dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
11675753Sgww 	    optlen, oldflg, cmsglen));
11680Sstevel@tonic-gate 	return (cmsglen);
11690Sstevel@tonic-gate }
11700Sstevel@tonic-gate 
11710Sstevel@tonic-gate /*
11720Sstevel@tonic-gate  * Copy options from options to the control. Convert SO_FILEP to
11730Sstevel@tonic-gate  * file descriptors.
11740Sstevel@tonic-gate  * Returns errno or zero.
11752280Sgt145670  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
11762280Sgt145670  * allocates the space that so_opt2cmsg fills. If one changes, the other should
11772280Sgt145670  * also be checked for any possible impacts.
11780Sstevel@tonic-gate  */
11790Sstevel@tonic-gate int
11800Sstevel@tonic-gate so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
11810Sstevel@tonic-gate     void *control, t_uscalar_t controllen)
11820Sstevel@tonic-gate {
11830Sstevel@tonic-gate 	struct T_opthdr *tohp;
11840Sstevel@tonic-gate 	struct cmsghdr *cmsg;
11850Sstevel@tonic-gate 	struct fdbuf *fdbuf;
11860Sstevel@tonic-gate 	int fdbuflen;
11870Sstevel@tonic-gate 	int error;
11882280Sgt145670 #if defined(DEBUG) || defined(__lint)
11892280Sgt145670 	struct cmsghdr *cend = (struct cmsghdr *)
11902280Sgt145670 	    (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
11912280Sgt145670 #endif
11920Sstevel@tonic-gate 	cmsg = (struct cmsghdr *)control;
11930Sstevel@tonic-gate 
11940Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
11970Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
11980Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
11990Sstevel@tonic-gate 		dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
12005753Sgww 		    tohp->level, tohp->name, tohp->len));
12010Sstevel@tonic-gate 
12020Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
12030Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
12040Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
12050Sstevel@tonic-gate 			continue;
12060Sstevel@tonic-gate 		}
12070Sstevel@tonic-gate 		ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
12080Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
12090Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
12100Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
12110Sstevel@tonic-gate 
12120Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
12130Sstevel@tonic-gate 				return (EPROTO);
12140Sstevel@tonic-gate 			if (oldflg) {
12150Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf, control,
12160Sstevel@tonic-gate 				    (int)controllen);
12170Sstevel@tonic-gate 				if (error != 0)
12180Sstevel@tonic-gate 					return (error);
12190Sstevel@tonic-gate 				continue;
12200Sstevel@tonic-gate 			} else {
12210Sstevel@tonic-gate 				int fdlen;
12220Sstevel@tonic-gate 
12230Sstevel@tonic-gate 				fdlen = (int)fdbuf_cmsglen(
12240Sstevel@tonic-gate 				    (int)_TPI_TOPT_DATALEN(tohp));
12250Sstevel@tonic-gate 
12260Sstevel@tonic-gate 				cmsg->cmsg_level = tohp->level;
12270Sstevel@tonic-gate 				cmsg->cmsg_type = SCM_RIGHTS;
12280Sstevel@tonic-gate 				cmsg->cmsg_len = (socklen_t)(fdlen +
12295753Sgww 				    sizeof (struct cmsghdr));
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf,
12325753Sgww 				    CMSG_CONTENT(cmsg), fdlen);
12330Sstevel@tonic-gate 				if (error != 0)
12340Sstevel@tonic-gate 					return (error);
12350Sstevel@tonic-gate 			}
12361673Sgt145670 		} else if (tohp->level == SOL_SOCKET &&
12371673Sgt145670 		    tohp->name == SCM_TIMESTAMP) {
12381673Sgt145670 			timestruc_t *timestamp;
12391673Sgt145670 
12401673Sgt145670 			if (oldflg)
12411673Sgt145670 				continue;
12421673Sgt145670 
12431673Sgt145670 			cmsg->cmsg_level = tohp->level;
12441673Sgt145670 			cmsg->cmsg_type = tohp->name;
12451673Sgt145670 
12461673Sgt145670 			timestamp =
12471673Sgt145670 			    (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
12481673Sgt145670 			    sizeof (intptr_t));
12491673Sgt145670 
12501673Sgt145670 			if (get_udatamodel() == DATAMODEL_NATIVE) {
12512280Sgt145670 				struct timeval tv;
12521673Sgt145670 
12531673Sgt145670 				cmsg->cmsg_len = sizeof (struct timeval) +
12541673Sgt145670 				    sizeof (struct cmsghdr);
12552280Sgt145670 				tv.tv_sec = timestamp->tv_sec;
12562280Sgt145670 				tv.tv_usec = timestamp->tv_nsec /
12572280Sgt145670 				    (NANOSEC / MICROSEC);
12582280Sgt145670 				/*
12592280Sgt145670 				 * on LP64 systems, the struct timeval in
12602280Sgt145670 				 * the destination will not be 8-byte aligned,
12612280Sgt145670 				 * so use bcopy to avoid alignment trouble
12622280Sgt145670 				 */
12632280Sgt145670 				bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
12641673Sgt145670 			} else {
12651673Sgt145670 				struct timeval32 *time32;
12661673Sgt145670 
12671673Sgt145670 				cmsg->cmsg_len = sizeof (struct timeval32) +
12681673Sgt145670 				    sizeof (struct cmsghdr);
12691673Sgt145670 				time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
12701673Sgt145670 				time32->tv_sec = (time32_t)timestamp->tv_sec;
12711673Sgt145670 				time32->tv_usec =
12721673Sgt145670 				    (int32_t)(timestamp->tv_nsec /
12731673Sgt145670 				    (NANOSEC / MICROSEC));
12741673Sgt145670 			}
12751673Sgt145670 
12760Sstevel@tonic-gate 		} else {
12770Sstevel@tonic-gate 			if (oldflg)
12780Sstevel@tonic-gate 				continue;
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 			cmsg->cmsg_level = tohp->level;
12810Sstevel@tonic-gate 			cmsg->cmsg_type = tohp->name;
12820Sstevel@tonic-gate 			cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) +
12830Sstevel@tonic-gate 			    sizeof (struct cmsghdr));
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 			/* copy content to control data part */
12860Sstevel@tonic-gate 			bcopy(&tohp[1], CMSG_CONTENT(cmsg),
12875753Sgww 			    CMSG_CONTENTLEN(cmsg));
12880Sstevel@tonic-gate 		}
12890Sstevel@tonic-gate 		/* move to next CMSG structure! */
12900Sstevel@tonic-gate 		cmsg = CMSG_NEXT(cmsg);
12910Sstevel@tonic-gate 	}
12922280Sgt145670 	dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
12937240Srh87107 	    control, controllen, (void *)cend, (void *)cmsg));
12942280Sgt145670 	ASSERT(cmsg <= cend);
12950Sstevel@tonic-gate 	return (0);
12960Sstevel@tonic-gate }
12970Sstevel@tonic-gate 
12980Sstevel@tonic-gate /*
12990Sstevel@tonic-gate  * Extract the SO_SRCADDR option value if present.
13000Sstevel@tonic-gate  */
13010Sstevel@tonic-gate void
13020Sstevel@tonic-gate so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
13030Sstevel@tonic-gate     t_uscalar_t *srclenp)
13040Sstevel@tonic-gate {
13050Sstevel@tonic-gate 	struct T_opthdr		*tohp;
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	ASSERT(srcp != NULL && srclenp != NULL);
13100Sstevel@tonic-gate 	*srcp = NULL;
13110Sstevel@tonic-gate 	*srclenp = 0;
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
13140Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
13150Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
13160Sstevel@tonic-gate 		dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
13175753Sgww 		    tohp->level, tohp->name, tohp->len));
13180Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
13190Sstevel@tonic-gate 		    tohp->name == SO_SRCADDR) {
13200Sstevel@tonic-gate 			*srcp = _TPI_TOPT_DATA(tohp);
13210Sstevel@tonic-gate 			*srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
13220Sstevel@tonic-gate 		}
13230Sstevel@tonic-gate 	}
13240Sstevel@tonic-gate }
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate /*
13270Sstevel@tonic-gate  * Verify if the SO_UNIX_CLOSE option is present.
13280Sstevel@tonic-gate  */
13290Sstevel@tonic-gate int
13300Sstevel@tonic-gate so_getopt_unix_close(void *opt, t_uscalar_t optlen)
13310Sstevel@tonic-gate {
13320Sstevel@tonic-gate 	struct T_opthdr		*tohp;
13330Sstevel@tonic-gate 
13340Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
13370Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
13380Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
13390Sstevel@tonic-gate 		dprint(1,
13405753Sgww 		    ("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
13415753Sgww 		    tohp->level, tohp->name, tohp->len));
13420Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
13430Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)
13440Sstevel@tonic-gate 			return (1);
13450Sstevel@tonic-gate 	}
13460Sstevel@tonic-gate 	return (0);
13470Sstevel@tonic-gate }
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate /*
13500Sstevel@tonic-gate  * Allocate an M_PROTO message.
13510Sstevel@tonic-gate  *
13520Sstevel@tonic-gate  * If allocation fails the behavior depends on sleepflg:
13530Sstevel@tonic-gate  *	_ALLOC_NOSLEEP	fail immediately
13540Sstevel@tonic-gate  *	_ALLOC_INTR	sleep for memory until a signal is caught
13550Sstevel@tonic-gate  *	_ALLOC_SLEEP	sleep forever. Don't return NULL.
13560Sstevel@tonic-gate  */
13570Sstevel@tonic-gate mblk_t *
13588778SErik.Nordmark@Sun.COM soallocproto(size_t size, int sleepflg, cred_t *cr)
13590Sstevel@tonic-gate {
13600Sstevel@tonic-gate 	mblk_t	*mp;
13610Sstevel@tonic-gate 
13620Sstevel@tonic-gate 	/* Round up size for reuse */
13630Sstevel@tonic-gate 	size = MAX(size, 64);
13648778SErik.Nordmark@Sun.COM 	if (cr != NULL)
13658778SErik.Nordmark@Sun.COM 		mp = allocb_cred(size, cr, curproc->p_pid);
13668778SErik.Nordmark@Sun.COM 	else
13678778SErik.Nordmark@Sun.COM 		mp = allocb(size, BPRI_MED);
13688778SErik.Nordmark@Sun.COM 
13690Sstevel@tonic-gate 	if (mp == NULL) {
13700Sstevel@tonic-gate 		int error;	/* Dummy - error not returned to caller */
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 		switch (sleepflg) {
13730Sstevel@tonic-gate 		case _ALLOC_SLEEP:
13748778SErik.Nordmark@Sun.COM 			if (cr != NULL) {
13758778SErik.Nordmark@Sun.COM 				mp = allocb_cred_wait(size, STR_NOSIG, &error,
13768778SErik.Nordmark@Sun.COM 				    cr, curproc->p_pid);
13778778SErik.Nordmark@Sun.COM 			} else {
13788778SErik.Nordmark@Sun.COM 				mp = allocb_wait(size, BPRI_MED, STR_NOSIG,
13798778SErik.Nordmark@Sun.COM 				    &error);
13808778SErik.Nordmark@Sun.COM 			}
13810Sstevel@tonic-gate 			ASSERT(mp);
13820Sstevel@tonic-gate 			break;
13830Sstevel@tonic-gate 		case _ALLOC_INTR:
13848778SErik.Nordmark@Sun.COM 			if (cr != NULL) {
13858778SErik.Nordmark@Sun.COM 				mp = allocb_cred_wait(size, 0, &error, cr,
13868778SErik.Nordmark@Sun.COM 				    curproc->p_pid);
13878778SErik.Nordmark@Sun.COM 			} else {
13888778SErik.Nordmark@Sun.COM 				mp = allocb_wait(size, BPRI_MED, 0, &error);
13898778SErik.Nordmark@Sun.COM 			}
13900Sstevel@tonic-gate 			if (mp == NULL) {
13910Sstevel@tonic-gate 				/* Caught signal while sleeping for memory */
13920Sstevel@tonic-gate 				eprintline(ENOBUFS);
13930Sstevel@tonic-gate 				return (NULL);
13940Sstevel@tonic-gate 			}
13950Sstevel@tonic-gate 			break;
13960Sstevel@tonic-gate 		case _ALLOC_NOSLEEP:
13970Sstevel@tonic-gate 		default:
13980Sstevel@tonic-gate 			eprintline(ENOBUFS);
13990Sstevel@tonic-gate 			return (NULL);
14000Sstevel@tonic-gate 		}
14010Sstevel@tonic-gate 	}
14020Sstevel@tonic-gate 	DB_TYPE(mp) = M_PROTO;
14030Sstevel@tonic-gate 	return (mp);
14040Sstevel@tonic-gate }
14050Sstevel@tonic-gate 
14060Sstevel@tonic-gate /*
14070Sstevel@tonic-gate  * Allocate an M_PROTO message with a single component.
14080Sstevel@tonic-gate  * len is the length of buf. size is the amount to allocate.
14090Sstevel@tonic-gate  *
14100Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
14110Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
14120Sstevel@tonic-gate  */
14130Sstevel@tonic-gate mblk_t *
14148778SErik.Nordmark@Sun.COM soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg,
14158778SErik.Nordmark@Sun.COM     cred_t *cr)
14160Sstevel@tonic-gate {
14170Sstevel@tonic-gate 	mblk_t	*mp;
14180Sstevel@tonic-gate 
14190Sstevel@tonic-gate 	if (size == 0)
14200Sstevel@tonic-gate 		size = len;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	ASSERT(size >= len);
14230Sstevel@tonic-gate 	/* Round up size for reuse */
14240Sstevel@tonic-gate 	size = MAX(size, 64);
14258778SErik.Nordmark@Sun.COM 	mp = soallocproto(size, sleepflg, cr);
14260Sstevel@tonic-gate 	if (mp == NULL)
14270Sstevel@tonic-gate 		return (NULL);
14280Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
14290Sstevel@tonic-gate 	if (len != 0) {
14300Sstevel@tonic-gate 		if (buf != NULL)
14310Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
14320Sstevel@tonic-gate 		else
14330Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
14340Sstevel@tonic-gate 		mp->b_wptr += len;
14350Sstevel@tonic-gate 	}
14360Sstevel@tonic-gate 	return (mp);
14370Sstevel@tonic-gate }
14380Sstevel@tonic-gate 
14390Sstevel@tonic-gate /*
14400Sstevel@tonic-gate  * Append buf/len to mp.
14410Sstevel@tonic-gate  * The caller has to ensure that there is enough room in the mblk.
14420Sstevel@tonic-gate  *
14430Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
14440Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
14450Sstevel@tonic-gate  */
14460Sstevel@tonic-gate void
14470Sstevel@tonic-gate soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
14480Sstevel@tonic-gate {
14490Sstevel@tonic-gate 	ASSERT(mp);
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	if (len != 0) {
14520Sstevel@tonic-gate 		/* Assert for room left */
14530Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
14540Sstevel@tonic-gate 		if (buf != NULL)
14550Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
14560Sstevel@tonic-gate 		else
14570Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
14580Sstevel@tonic-gate 	}
14590Sstevel@tonic-gate 	mp->b_wptr += len;
14600Sstevel@tonic-gate }
14610Sstevel@tonic-gate 
14620Sstevel@tonic-gate /*
14630Sstevel@tonic-gate  * Create a message using two kernel buffers.
14640Sstevel@tonic-gate  * If size is set that will determine the allocation size (e.g. for future
14650Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
14660Sstevel@tonic-gate  * lengths.
14670Sstevel@tonic-gate  */
14680Sstevel@tonic-gate mblk_t *
14690Sstevel@tonic-gate soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
14708778SErik.Nordmark@Sun.COM     ssize_t size, int sleepflg, cred_t *cr)
14710Sstevel@tonic-gate {
14720Sstevel@tonic-gate 	mblk_t *mp;
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate 	if (size == 0)
14750Sstevel@tonic-gate 		size = len1 + len2;
14760Sstevel@tonic-gate 	ASSERT(size >= len1 + len2);
14770Sstevel@tonic-gate 
14788778SErik.Nordmark@Sun.COM 	mp = soallocproto1(buf1, len1, size, sleepflg, cr);
14790Sstevel@tonic-gate 	if (mp)
14800Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
14810Sstevel@tonic-gate 	return (mp);
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate /*
14850Sstevel@tonic-gate  * Create a message using three kernel buffers.
14860Sstevel@tonic-gate  * If size is set that will determine the allocation size (for future
14870Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
14880Sstevel@tonic-gate  * lengths.
14890Sstevel@tonic-gate  */
14900Sstevel@tonic-gate mblk_t *
14910Sstevel@tonic-gate soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
14928778SErik.Nordmark@Sun.COM     const void *buf3, ssize_t len3, ssize_t size, int sleepflg, cred_t *cr)
14930Sstevel@tonic-gate {
14940Sstevel@tonic-gate 	mblk_t *mp;
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	if (size == 0)
14970Sstevel@tonic-gate 		size = len1 + len2 +len3;
14980Sstevel@tonic-gate 	ASSERT(size >= len1 + len2 + len3);
14990Sstevel@tonic-gate 
15008778SErik.Nordmark@Sun.COM 	mp = soallocproto1(buf1, len1, size, sleepflg, cr);
15010Sstevel@tonic-gate 	if (mp != NULL) {
15020Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
15030Sstevel@tonic-gate 		soappendmsg(mp, buf3, len3);
15040Sstevel@tonic-gate 	}
15050Sstevel@tonic-gate 	return (mp);
15060Sstevel@tonic-gate }
15070Sstevel@tonic-gate 
15080Sstevel@tonic-gate #ifdef DEBUG
15090Sstevel@tonic-gate char *
15100Sstevel@tonic-gate pr_state(uint_t state, uint_t mode)
15110Sstevel@tonic-gate {
15120Sstevel@tonic-gate 	static char buf[1024];
15130Sstevel@tonic-gate 
15140Sstevel@tonic-gate 	buf[0] = 0;
15150Sstevel@tonic-gate 	if (state & SS_ISCONNECTED)
15167240Srh87107 		(void) strcat(buf, "ISCONNECTED ");
15170Sstevel@tonic-gate 	if (state & SS_ISCONNECTING)
15187240Srh87107 		(void) strcat(buf, "ISCONNECTING ");
15190Sstevel@tonic-gate 	if (state & SS_ISDISCONNECTING)
15207240Srh87107 		(void) strcat(buf, "ISDISCONNECTING ");
15210Sstevel@tonic-gate 	if (state & SS_CANTSENDMORE)
15227240Srh87107 		(void) strcat(buf, "CANTSENDMORE ");
15230Sstevel@tonic-gate 
15240Sstevel@tonic-gate 	if (state & SS_CANTRCVMORE)
15257240Srh87107 		(void) strcat(buf, "CANTRCVMORE ");
15260Sstevel@tonic-gate 	if (state & SS_ISBOUND)
15277240Srh87107 		(void) strcat(buf, "ISBOUND ");
15280Sstevel@tonic-gate 	if (state & SS_NDELAY)
15297240Srh87107 		(void) strcat(buf, "NDELAY ");
15300Sstevel@tonic-gate 	if (state & SS_NONBLOCK)
15317240Srh87107 		(void) strcat(buf, "NONBLOCK ");
15320Sstevel@tonic-gate 
15330Sstevel@tonic-gate 	if (state & SS_ASYNC)
15347240Srh87107 		(void) strcat(buf, "ASYNC ");
15350Sstevel@tonic-gate 	if (state & SS_ACCEPTCONN)
15367240Srh87107 		(void) strcat(buf, "ACCEPTCONN ");
15370Sstevel@tonic-gate 	if (state & SS_SAVEDEOR)
15387240Srh87107 		(void) strcat(buf, "SAVEDEOR ");
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	if (state & SS_RCVATMARK)
15417240Srh87107 		(void) strcat(buf, "RCVATMARK ");
15420Sstevel@tonic-gate 	if (state & SS_OOBPEND)
15437240Srh87107 		(void) strcat(buf, "OOBPEND ");
15440Sstevel@tonic-gate 	if (state & SS_HAVEOOBDATA)
15457240Srh87107 		(void) strcat(buf, "HAVEOOBDATA ");
15460Sstevel@tonic-gate 	if (state & SS_HADOOBDATA)
15477240Srh87107 		(void) strcat(buf, "HADOOBDATA ");
15480Sstevel@tonic-gate 
15490Sstevel@tonic-gate 	if (mode & SM_PRIV)
15507240Srh87107 		(void) strcat(buf, "PRIV ");
15510Sstevel@tonic-gate 	if (mode & SM_ATOMIC)
15527240Srh87107 		(void) strcat(buf, "ATOMIC ");
15530Sstevel@tonic-gate 	if (mode & SM_ADDR)
15547240Srh87107 		(void) strcat(buf, "ADDR ");
15550Sstevel@tonic-gate 	if (mode & SM_CONNREQUIRED)
15567240Srh87107 		(void) strcat(buf, "CONNREQUIRED ");
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	if (mode & SM_FDPASSING)
15597240Srh87107 		(void) strcat(buf, "FDPASSING ");
15600Sstevel@tonic-gate 	if (mode & SM_EXDATA)
15617240Srh87107 		(void) strcat(buf, "EXDATA ");
15620Sstevel@tonic-gate 	if (mode & SM_OPTDATA)
15637240Srh87107 		(void) strcat(buf, "OPTDATA ");
15640Sstevel@tonic-gate 	if (mode & SM_BYTESTREAM)
15657240Srh87107 		(void) strcat(buf, "BYTESTREAM ");
15660Sstevel@tonic-gate 	return (buf);
15670Sstevel@tonic-gate }
15680Sstevel@tonic-gate 
15690Sstevel@tonic-gate char *
15700Sstevel@tonic-gate pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
15710Sstevel@tonic-gate {
15720Sstevel@tonic-gate 	static char buf[1024];
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate 	if (addr == NULL || addrlen == 0) {
15757240Srh87107 		(void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr);
15760Sstevel@tonic-gate 		return (buf);
15770Sstevel@tonic-gate 	}
15780Sstevel@tonic-gate 	switch (family) {
15790Sstevel@tonic-gate 	case AF_INET: {
15800Sstevel@tonic-gate 		struct sockaddr_in sin;
15810Sstevel@tonic-gate 
15820Sstevel@tonic-gate 		bcopy(addr, &sin, sizeof (sin));
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 		(void) sprintf(buf, "(len %d) %x/%d",
15856712Stomee 		    addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
15860Sstevel@tonic-gate 		break;
15870Sstevel@tonic-gate 	}
15880Sstevel@tonic-gate 	case AF_INET6: {
15890Sstevel@tonic-gate 		struct sockaddr_in6 sin6;
15900Sstevel@tonic-gate 		uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 		bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
15937240Srh87107 		(void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
15940Sstevel@tonic-gate 		    addrlen,
15950Sstevel@tonic-gate 		    ntohs(piece[0]), ntohs(piece[1]),
15960Sstevel@tonic-gate 		    ntohs(piece[2]), ntohs(piece[3]),
15970Sstevel@tonic-gate 		    ntohs(piece[4]), ntohs(piece[5]),
15980Sstevel@tonic-gate 		    ntohs(piece[6]), ntohs(piece[7]),
15990Sstevel@tonic-gate 		    ntohs(sin6.sin6_port));
16000Sstevel@tonic-gate 		break;
16010Sstevel@tonic-gate 	}
16020Sstevel@tonic-gate 	case AF_UNIX: {
16030Sstevel@tonic-gate 		struct sockaddr_un *soun = (struct sockaddr_un *)addr;
16040Sstevel@tonic-gate 
16056712Stomee 		(void) sprintf(buf, "(len %d) %s", addrlen,
16065753Sgww 		    (soun == NULL) ? "(none)" : soun->sun_path);
16070Sstevel@tonic-gate 		break;
16080Sstevel@tonic-gate 	}
16090Sstevel@tonic-gate 	default:
16100Sstevel@tonic-gate 		(void) sprintf(buf, "(unknown af %d)", family);
16110Sstevel@tonic-gate 		break;
16120Sstevel@tonic-gate 	}
16130Sstevel@tonic-gate 	return (buf);
16140Sstevel@tonic-gate }
16150Sstevel@tonic-gate 
16160Sstevel@tonic-gate /* The logical equivalence operator (a if-and-only-if b) */
1617*11474SJonathan.Adams@Sun.COM #define	EQUIVALENT(a, b)	(((a) && (b)) || (!(a) && (!(b))))
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate /*
16200Sstevel@tonic-gate  * Verify limitations and invariants on oob state.
16210Sstevel@tonic-gate  * Return 1 if OK, otherwise 0 so that it can be used as
16220Sstevel@tonic-gate  *	ASSERT(verify_oobstate(so));
16230Sstevel@tonic-gate  */
16240Sstevel@tonic-gate int
16250Sstevel@tonic-gate so_verify_oobstate(struct sonode *so)
16260Sstevel@tonic-gate {
16278348SEric.Yu@Sun.COM 	boolean_t havemark;
16288348SEric.Yu@Sun.COM 
16290Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
16300Sstevel@tonic-gate 
16310Sstevel@tonic-gate 	/*
16320Sstevel@tonic-gate 	 * The possible state combinations are:
16330Sstevel@tonic-gate 	 *	0
16340Sstevel@tonic-gate 	 *	SS_OOBPEND
16350Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HAVEOOBDATA
16360Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HADOOBDATA
16370Sstevel@tonic-gate 	 *	SS_HADOOBDATA
16380Sstevel@tonic-gate 	 */
16390Sstevel@tonic-gate 	switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
16400Sstevel@tonic-gate 	case 0:
16410Sstevel@tonic-gate 	case SS_OOBPEND:
16420Sstevel@tonic-gate 	case SS_OOBPEND|SS_HAVEOOBDATA:
16430Sstevel@tonic-gate 	case SS_OOBPEND|SS_HADOOBDATA:
16440Sstevel@tonic-gate 	case SS_HADOOBDATA:
16450Sstevel@tonic-gate 		break;
16460Sstevel@tonic-gate 	default:
16478348SEric.Yu@Sun.COM 		printf("Bad oob state 1 (%p): state %s\n",
16488348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16490Sstevel@tonic-gate 		return (0);
16500Sstevel@tonic-gate 	}
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate 	/* SS_RCVATMARK should only be set when SS_OOBPEND is set */
16530Sstevel@tonic-gate 	if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
16548348SEric.Yu@Sun.COM 		printf("Bad oob state 2 (%p): state %s\n",
16558348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16560Sstevel@tonic-gate 		return (0);
16570Sstevel@tonic-gate 	}
16580Sstevel@tonic-gate 
16590Sstevel@tonic-gate 	/*
16608348SEric.Yu@Sun.COM 	 * (havemark != 0 or SS_RCVATMARK) iff SS_OOBPEND
16618348SEric.Yu@Sun.COM 	 * For TPI, the presence of a "mark" is indicated by sti_oobsigcnt.
16620Sstevel@tonic-gate 	 */
16638348SEric.Yu@Sun.COM 	havemark = (SOCK_IS_NONSTR(so)) ? so->so_oobmark > 0 :
16648348SEric.Yu@Sun.COM 	    SOTOTPI(so)->sti_oobsigcnt > 0;
16658348SEric.Yu@Sun.COM 
1666*11474SJonathan.Adams@Sun.COM 	if (!EQUIVALENT(havemark || (so->so_state & SS_RCVATMARK),
16675753Sgww 	    so->so_state & SS_OOBPEND)) {
16688348SEric.Yu@Sun.COM 		printf("Bad oob state 3 (%p): state %s\n",
16698348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16700Sstevel@tonic-gate 		return (0);
16710Sstevel@tonic-gate 	}
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate 	/*
16740Sstevel@tonic-gate 	 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
16750Sstevel@tonic-gate 	 */
16760Sstevel@tonic-gate 	if (!(so->so_options & SO_OOBINLINE) &&
1677*11474SJonathan.Adams@Sun.COM 	    !EQUIVALENT(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
16788348SEric.Yu@Sun.COM 		printf("Bad oob state 4 (%p): state %s\n",
16798348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16800Sstevel@tonic-gate 		return (0);
16810Sstevel@tonic-gate 	}
16828348SEric.Yu@Sun.COM 
16838348SEric.Yu@Sun.COM 	if (!SOCK_IS_NONSTR(so) &&
16848348SEric.Yu@Sun.COM 	    SOTOTPI(so)->sti_oobsigcnt < SOTOTPI(so)->sti_oobcnt) {
16850Sstevel@tonic-gate 		printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
16868348SEric.Yu@Sun.COM 		    (void *)so, SOTOTPI(so)->sti_oobsigcnt,
16878348SEric.Yu@Sun.COM 		    SOTOTPI(so)->sti_oobcnt,
16888348SEric.Yu@Sun.COM 		    pr_state(so->so_state, so->so_mode));
16890Sstevel@tonic-gate 		return (0);
16900Sstevel@tonic-gate 	}
16918348SEric.Yu@Sun.COM 
16920Sstevel@tonic-gate 	return (1);
16930Sstevel@tonic-gate }
1694*11474SJonathan.Adams@Sun.COM #undef	EQUIVALENT
16950Sstevel@tonic-gate #endif /* DEBUG */
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate /* initialize sockfs zone specific kstat related items			*/
16980Sstevel@tonic-gate void *
16990Sstevel@tonic-gate sock_kstat_init(zoneid_t zoneid)
17000Sstevel@tonic-gate {
17010Sstevel@tonic-gate 	kstat_t	*ksp;
17020Sstevel@tonic-gate 
17030Sstevel@tonic-gate 	ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
17040Sstevel@tonic-gate 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
17050Sstevel@tonic-gate 
17060Sstevel@tonic-gate 	if (ksp != NULL) {
17070Sstevel@tonic-gate 		ksp->ks_update = sockfs_update;
17080Sstevel@tonic-gate 		ksp->ks_snapshot = sockfs_snapshot;
17090Sstevel@tonic-gate 		ksp->ks_lock = &socklist.sl_lock;
17100Sstevel@tonic-gate 		ksp->ks_private = (void *)(uintptr_t)zoneid;
17110Sstevel@tonic-gate 		kstat_install(ksp);
17120Sstevel@tonic-gate 	}
17130Sstevel@tonic-gate 
17140Sstevel@tonic-gate 	return (ksp);
17150Sstevel@tonic-gate }
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate /* tear down sockfs zone specific kstat related items			*/
17180Sstevel@tonic-gate /*ARGSUSED*/
17190Sstevel@tonic-gate void
17200Sstevel@tonic-gate sock_kstat_fini(zoneid_t zoneid, void *arg)
17210Sstevel@tonic-gate {
17220Sstevel@tonic-gate 	kstat_t *ksp = (kstat_t *)arg;
17230Sstevel@tonic-gate 
17240Sstevel@tonic-gate 	if (ksp != NULL) {
17250Sstevel@tonic-gate 		ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
17260Sstevel@tonic-gate 		kstat_delete(ksp);
17270Sstevel@tonic-gate 	}
17280Sstevel@tonic-gate }
17290Sstevel@tonic-gate 
17300Sstevel@tonic-gate /*
17310Sstevel@tonic-gate  * Zones:
17320Sstevel@tonic-gate  * Note that nactive is going to be different for each zone.
17330Sstevel@tonic-gate  * This means we require kstat to call sockfs_update and then sockfs_snapshot
17340Sstevel@tonic-gate  * for the same zone, or sockfs_snapshot will be taken into the wrong size
17350Sstevel@tonic-gate  * buffer. This is safe, but if the buffer is too small, user will not be
17360Sstevel@tonic-gate  * given details of all sockets. However, as this kstat has a ks_lock, kstat
17370Sstevel@tonic-gate  * driver will keep it locked between the update and the snapshot, so no
17380Sstevel@tonic-gate  * other process (zone) can currently get inbetween resulting in a wrong size
17390Sstevel@tonic-gate  * buffer allocation.
17400Sstevel@tonic-gate  */
17410Sstevel@tonic-gate static int
17420Sstevel@tonic-gate sockfs_update(kstat_t *ksp, int rw)
17430Sstevel@tonic-gate {
17440Sstevel@tonic-gate 	uint_t	nactive = 0;		/* # of active AF_UNIX sockets	*/
17450Sstevel@tonic-gate 	struct sonode	*so;		/* current sonode on socklist	*/
17460Sstevel@tonic-gate 	zoneid_t	myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
17510Sstevel@tonic-gate 		return (EACCES);
17520Sstevel@tonic-gate 	}
17530Sstevel@tonic-gate 
17548348SEric.Yu@Sun.COM 	for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
17558348SEric.Yu@Sun.COM 		if (so->so_count != 0 && so->so_zoneid == myzoneid) {
17560Sstevel@tonic-gate 			nactive++;
17570Sstevel@tonic-gate 		}
17580Sstevel@tonic-gate 	}
17590Sstevel@tonic-gate 	ksp->ks_ndata = nactive;
17600Sstevel@tonic-gate 	ksp->ks_data_size = nactive * sizeof (struct k_sockinfo);
17610Sstevel@tonic-gate 
17620Sstevel@tonic-gate 	return (0);
17630Sstevel@tonic-gate }
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate static int
17660Sstevel@tonic-gate sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
17670Sstevel@tonic-gate {
17680Sstevel@tonic-gate 	int			ns;	/* # of sonodes we've copied	*/
17690Sstevel@tonic-gate 	struct sonode		*so;	/* current sonode on socklist	*/
17700Sstevel@tonic-gate 	struct k_sockinfo	*pksi;	/* where we put sockinfo data	*/
17710Sstevel@tonic-gate 	t_uscalar_t		sn_len;	/* soa_len			*/
17720Sstevel@tonic-gate 	zoneid_t		myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
17738348SEric.Yu@Sun.COM 	sotpi_info_t 		*sti;
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
17800Sstevel@tonic-gate 		return (EACCES);
17810Sstevel@tonic-gate 	}
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate 	/*
17840Sstevel@tonic-gate 	 * for each sonode on the socklist, we massage the important
17850Sstevel@tonic-gate 	 * info into buf, in k_sockinfo format.
17860Sstevel@tonic-gate 	 */
17870Sstevel@tonic-gate 	pksi = (struct k_sockinfo *)buf;
17888348SEric.Yu@Sun.COM 	ns = 0;
17898348SEric.Yu@Sun.COM 	for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
17900Sstevel@tonic-gate 		/* only stuff active sonodes and the same zone:		*/
17918348SEric.Yu@Sun.COM 		if (so->so_count == 0 || so->so_zoneid != myzoneid) {
17920Sstevel@tonic-gate 			continue;
17930Sstevel@tonic-gate 		}
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate 		/*
17960Sstevel@tonic-gate 		 * If the sonode was activated between the update and the
17970Sstevel@tonic-gate 		 * snapshot, we're done - as this is only a snapshot.
17980Sstevel@tonic-gate 		 */
17990Sstevel@tonic-gate 		if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) {
18000Sstevel@tonic-gate 			break;
18010Sstevel@tonic-gate 		}
18020Sstevel@tonic-gate 
18038348SEric.Yu@Sun.COM 		sti = SOTOTPI(so);
18040Sstevel@tonic-gate 		/* copy important info into buf:			*/
18050Sstevel@tonic-gate 		pksi->ks_si.si_size = sizeof (struct k_sockinfo);
18060Sstevel@tonic-gate 		pksi->ks_si.si_family = so->so_family;
18070Sstevel@tonic-gate 		pksi->ks_si.si_type = so->so_type;
18080Sstevel@tonic-gate 		pksi->ks_si.si_flag = so->so_flag;
18090Sstevel@tonic-gate 		pksi->ks_si.si_state = so->so_state;
18108348SEric.Yu@Sun.COM 		pksi->ks_si.si_serv_type = sti->sti_serv_type;
18118348SEric.Yu@Sun.COM 		pksi->ks_si.si_ux_laddr_sou_magic =
18128348SEric.Yu@Sun.COM 		    sti->sti_ux_laddr.soua_magic;
18138348SEric.Yu@Sun.COM 		pksi->ks_si.si_ux_faddr_sou_magic =
18148348SEric.Yu@Sun.COM 		    sti->sti_ux_faddr.soua_magic;
18158348SEric.Yu@Sun.COM 		pksi->ks_si.si_laddr_soa_len = sti->sti_laddr.soa_len;
18168348SEric.Yu@Sun.COM 		pksi->ks_si.si_faddr_soa_len = sti->sti_faddr.soa_len;
18170Sstevel@tonic-gate 		pksi->ks_si.si_szoneid = so->so_zoneid;
18188348SEric.Yu@Sun.COM 		pksi->ks_si.si_faddr_noxlate = sti->sti_faddr_noxlate;
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
18210Sstevel@tonic-gate 
18228348SEric.Yu@Sun.COM 		if (sti->sti_laddr_sa != NULL) {
18238348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_sa->sa_data != NULL);
18248348SEric.Yu@Sun.COM 			sn_len = sti->sti_laddr_len;
18250Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
18260Sstevel@tonic-gate 			    sizeof (pksi->ks_si.si_laddr_sun_path));
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate 			pksi->ks_si.si_laddr_family =
18298348SEric.Yu@Sun.COM 			    sti->sti_laddr_sa->sa_family;
18300Sstevel@tonic-gate 			if (sn_len != 0) {
18310Sstevel@tonic-gate 				/* AF_UNIX socket names are NULL terminated */
18320Sstevel@tonic-gate 				(void) strncpy(pksi->ks_si.si_laddr_sun_path,
18338348SEric.Yu@Sun.COM 				    sti->sti_laddr_sa->sa_data,
18340Sstevel@tonic-gate 				    sizeof (pksi->ks_si.si_laddr_sun_path));
18350Sstevel@tonic-gate 				sn_len = strlen(pksi->ks_si.si_laddr_sun_path);
18360Sstevel@tonic-gate 			}
18370Sstevel@tonic-gate 			pksi->ks_si.si_laddr_sun_path[sn_len] = 0;
18380Sstevel@tonic-gate 		}
18390Sstevel@tonic-gate 
18408348SEric.Yu@Sun.COM 		if (sti->sti_faddr_sa != NULL) {
18418348SEric.Yu@Sun.COM 			ASSERT(sti->sti_faddr_sa->sa_data != NULL);
18428348SEric.Yu@Sun.COM 			sn_len = sti->sti_faddr_len;
18430Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
18440Sstevel@tonic-gate 			    sizeof (pksi->ks_si.si_faddr_sun_path));
18450Sstevel@tonic-gate 
18460Sstevel@tonic-gate 			pksi->ks_si.si_faddr_family =
18478348SEric.Yu@Sun.COM 			    sti->sti_faddr_sa->sa_family;
18480Sstevel@tonic-gate 			if (sn_len != 0) {
18490Sstevel@tonic-gate 				(void) strncpy(pksi->ks_si.si_faddr_sun_path,
18508348SEric.Yu@Sun.COM 				    sti->sti_faddr_sa->sa_data,
18510Sstevel@tonic-gate 				    sizeof (pksi->ks_si.si_faddr_sun_path));
18520Sstevel@tonic-gate 				sn_len = strlen(pksi->ks_si.si_faddr_sun_path);
18530Sstevel@tonic-gate 			}
18540Sstevel@tonic-gate 			pksi->ks_si.si_faddr_sun_path[sn_len] = 0;
18550Sstevel@tonic-gate 		}
18560Sstevel@tonic-gate 
18570Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[0], "%p", (void *)so);
18600Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[1], "%p",
18618348SEric.Yu@Sun.COM 		    (void *)sti->sti_ux_laddr.soua_vp);
18620Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[2], "%p",
18638348SEric.Yu@Sun.COM 		    (void *)sti->sti_ux_faddr.soua_vp);
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 		ns++;
18660Sstevel@tonic-gate 		pksi++;
18670Sstevel@tonic-gate 	}
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 	ksp->ks_ndata = ns;
18700Sstevel@tonic-gate 	return (0);
18710Sstevel@tonic-gate }
18720Sstevel@tonic-gate 
18730Sstevel@tonic-gate ssize_t
18740Sstevel@tonic-gate soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
18750Sstevel@tonic-gate {
18760Sstevel@tonic-gate 	struct uio auio;
18770Sstevel@tonic-gate 	struct iovec aiov[MSG_MAXIOVLEN];
18780Sstevel@tonic-gate 	register vnode_t *vp;
18790Sstevel@tonic-gate 	int ioflag, rwflag;
18800Sstevel@tonic-gate 	ssize_t cnt;
18810Sstevel@tonic-gate 	int error = 0;
18820Sstevel@tonic-gate 	int iovcnt = 0;
18830Sstevel@tonic-gate 	short fflag;
18840Sstevel@tonic-gate 
18850Sstevel@tonic-gate 	vp = fp->f_vnode;
18860Sstevel@tonic-gate 	fflag = fp->f_flag;
18870Sstevel@tonic-gate 
18880Sstevel@tonic-gate 	rwflag = 0;
18890Sstevel@tonic-gate 	aiov[0].iov_base = (caddr_t)buf;
18900Sstevel@tonic-gate 	aiov[0].iov_len = size;
18910Sstevel@tonic-gate 	iovcnt = 1;
18920Sstevel@tonic-gate 	cnt = (ssize_t)size;
18930Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
18940Sstevel@tonic-gate 
18950Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
18960Sstevel@tonic-gate 	auio.uio_iov = aiov;
18970Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
18980Sstevel@tonic-gate 	auio.uio_resid = cnt;
18990Sstevel@tonic-gate 	auio.uio_segflg = UIO_SYSSPACE;
19000Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
19010Sstevel@tonic-gate 	auio.uio_fmode = fflag;
19020Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
19070Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
19080Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
19090Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
19100Sstevel@tonic-gate 	cnt -= auio.uio_resid;
19110Sstevel@tonic-gate 
19120Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	if (error == EINTR && cnt != 0)
19150Sstevel@tonic-gate 		error = 0;
19160Sstevel@tonic-gate out:
19170Sstevel@tonic-gate 	if (error != 0) {
19180Sstevel@tonic-gate 		*err = error;
19190Sstevel@tonic-gate 		return (0);
19200Sstevel@tonic-gate 	} else {
19210Sstevel@tonic-gate 		*err = 0;
19220Sstevel@tonic-gate 		return (cnt);
19230Sstevel@tonic-gate 	}
19240Sstevel@tonic-gate }
19258348SEric.Yu@Sun.COM 
19268348SEric.Yu@Sun.COM int
19278348SEric.Yu@Sun.COM so_copyin(const void *from, void *to, size_t size, int fromkernel)
19288348SEric.Yu@Sun.COM {
19298348SEric.Yu@Sun.COM 	if (fromkernel) {
19308348SEric.Yu@Sun.COM 		bcopy(from, to, size);
19318348SEric.Yu@Sun.COM 		return (0);
19328348SEric.Yu@Sun.COM 	}
19338348SEric.Yu@Sun.COM 	return (xcopyin(from, to, size));
19348348SEric.Yu@Sun.COM }
19358348SEric.Yu@Sun.COM 
19368348SEric.Yu@Sun.COM int
19378348SEric.Yu@Sun.COM so_copyout(const void *from, void *to, size_t size, int tokernel)
19388348SEric.Yu@Sun.COM {
19398348SEric.Yu@Sun.COM 	if (tokernel) {
19408348SEric.Yu@Sun.COM 		bcopy(from, to, size);
19418348SEric.Yu@Sun.COM 		return (0);
19428348SEric.Yu@Sun.COM 	}
19438348SEric.Yu@Sun.COM 	return (xcopyout(from, to, size));
19448348SEric.Yu@Sun.COM }
1945