xref: /onnv-gate/usr/src/uts/common/fs/sockfs/socksubr.c (revision 8778:b4169d2ab299)
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*8778SErik.Nordmark@Sun.COM  * Copyright 2009 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>
456707Sbrutus #include <sys/uio.h>
460Sstevel@tonic-gate #include <sys/termios.h>
470Sstevel@tonic-gate #include <sys/stream.h>
480Sstevel@tonic-gate #include <sys/strsubr.h>
490Sstevel@tonic-gate #include <sys/strsun.h>
500Sstevel@tonic-gate #include <sys/esunddi.h>
510Sstevel@tonic-gate #include <sys/flock.h>
520Sstevel@tonic-gate #include <sys/modctl.h>
530Sstevel@tonic-gate #include <sys/cmn_err.h>
540Sstevel@tonic-gate #include <sys/mkdev.h>
550Sstevel@tonic-gate #include <sys/pathname.h>
560Sstevel@tonic-gate #include <sys/ddi.h>
570Sstevel@tonic-gate #include <sys/stat.h>
580Sstevel@tonic-gate #include <sys/fs/snode.h>
590Sstevel@tonic-gate #include <sys/fs/dv_node.h>
600Sstevel@tonic-gate #include <sys/zone.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate #include <sys/socket.h>
630Sstevel@tonic-gate #include <sys/socketvar.h>
640Sstevel@tonic-gate #include <netinet/in.h>
650Sstevel@tonic-gate #include <sys/un.h>
660Sstevel@tonic-gate 
670Sstevel@tonic-gate #include <sys/ucred.h>
680Sstevel@tonic-gate 
690Sstevel@tonic-gate #include <sys/tiuser.h>
700Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
710Sstevel@tonic-gate #include <sys/tihdr.h>
720Sstevel@tonic-gate 
730Sstevel@tonic-gate #include <c2/audit.h>
740Sstevel@tonic-gate 
750Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
768348SEric.Yu@Sun.COM #include <fs/sockfs/sockcommon.h>
778348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h>
788348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi_impl.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 
1096707Sbrutus extern int sostr_init();
1106707Sbrutus 
1118194SJack.Meng@Sun.COM extern int modrootloaded;
1128194SJack.Meng@Sun.COM 
1130Sstevel@tonic-gate #define	ADRSTRLEN (2 * sizeof (void *) + 1)
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * kernel structure for passing the sockinfo data back up to the user.
1160Sstevel@tonic-gate  * the strings array allows us to convert AF_UNIX addresses into strings
1170Sstevel@tonic-gate  * with a common method regardless of which n-bit kernel we're running.
1180Sstevel@tonic-gate  */
1190Sstevel@tonic-gate struct k_sockinfo {
1200Sstevel@tonic-gate 	struct sockinfo	ks_si;
1210Sstevel@tonic-gate 	char		ks_straddr[3][ADRSTRLEN];
1220Sstevel@tonic-gate };
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate /*
1250Sstevel@tonic-gate  * Translate from a device pathname (e.g. "/dev/tcp") to a vnode.
1260Sstevel@tonic-gate  * Returns with the vnode held.
1270Sstevel@tonic-gate  */
1288348SEric.Yu@Sun.COM int
1290Sstevel@tonic-gate sogetvp(char *devpath, vnode_t **vpp, int uioflag)
1300Sstevel@tonic-gate {
1310Sstevel@tonic-gate 	struct snode *csp;
1320Sstevel@tonic-gate 	vnode_t *vp, *dvp;
1330Sstevel@tonic-gate 	major_t maj;
1340Sstevel@tonic-gate 	int error;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE);
1378348SEric.Yu@Sun.COM 
1380Sstevel@tonic-gate 	/*
1390Sstevel@tonic-gate 	 * Lookup the underlying filesystem vnode.
1400Sstevel@tonic-gate 	 */
1410Sstevel@tonic-gate 	error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp);
1420Sstevel@tonic-gate 	if (error)
1430Sstevel@tonic-gate 		return (error);
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 	/* Check that it is the correct vnode */
1460Sstevel@tonic-gate 	if (vp->v_type != VCHR) {
1470Sstevel@tonic-gate 		VN_RELE(vp);
1480Sstevel@tonic-gate 		return (ENOTSOCK);
1490Sstevel@tonic-gate 	}
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 	/*
1520Sstevel@tonic-gate 	 * If devpath went through devfs, the device should already
1530Sstevel@tonic-gate 	 * be configured. If devpath is a mknod file, however, we
1540Sstevel@tonic-gate 	 * need to make sure the device is properly configured.
1550Sstevel@tonic-gate 	 * To do this, we do something similar to spec_open()
1560Sstevel@tonic-gate 	 * except that we resolve to the minor/leaf level since
1570Sstevel@tonic-gate 	 * we need to return a vnode.
1580Sstevel@tonic-gate 	 */
1590Sstevel@tonic-gate 	csp = VTOS(VTOS(vp)->s_commonvp);
1600Sstevel@tonic-gate 	if (!(csp->s_flag & SDIPSET)) {
1610Sstevel@tonic-gate 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1620Sstevel@tonic-gate 		error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname);
1630Sstevel@tonic-gate 		if (error == 0)
1640Sstevel@tonic-gate 			error = devfs_lookupname(pathname, NULLVPP, &dvp);
1650Sstevel@tonic-gate 		VN_RELE(vp);
1660Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
1670Sstevel@tonic-gate 		if (error != 0)
1680Sstevel@tonic-gate 			return (ENXIO);
1690Sstevel@tonic-gate 		vp = dvp;	/* use the devfs vp */
1700Sstevel@tonic-gate 	}
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 	/* device is configured at this point */
1730Sstevel@tonic-gate 	maj = getmajor(vp->v_rdev);
1740Sstevel@tonic-gate 	if (!STREAMSTAB(maj)) {
1750Sstevel@tonic-gate 		VN_RELE(vp);
1760Sstevel@tonic-gate 		return (ENOSTR);
1770Sstevel@tonic-gate 	}
1780Sstevel@tonic-gate 
1790Sstevel@tonic-gate 	*vpp = vp;
1800Sstevel@tonic-gate 	return (0);
1810Sstevel@tonic-gate }
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate /*
1840Sstevel@tonic-gate  * Update the accessed, updated, or changed times in an sonode
1850Sstevel@tonic-gate  * with the current time.
1860Sstevel@tonic-gate  *
1870Sstevel@tonic-gate  * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable
1880Sstevel@tonic-gate  * attributes in a fstat call. (They return the current time and 0 for
1890Sstevel@tonic-gate  * all timestamps, respectively.) We maintain the current timestamps
1900Sstevel@tonic-gate  * here primarily so that should sockmod be popped the resulting
1910Sstevel@tonic-gate  * file descriptor will behave like a stream w.r.t. the timestamps.
1920Sstevel@tonic-gate  */
1930Sstevel@tonic-gate void
1940Sstevel@tonic-gate so_update_attrs(struct sonode *so, int flag)
1950Sstevel@tonic-gate {
1960Sstevel@tonic-gate 	time_t now = gethrestime_sec();
1970Sstevel@tonic-gate 
1988348SEric.Yu@Sun.COM 	if (SOCK_IS_NONSTR(so))
1998348SEric.Yu@Sun.COM 		return;
2008348SEric.Yu@Sun.COM 
2010Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
2020Sstevel@tonic-gate 	so->so_flag |= flag;
2030Sstevel@tonic-gate 	if (flag & SOACC)
2048348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_atime = now;
2050Sstevel@tonic-gate 	if (flag & SOMOD)
2068348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_mtime = now;
2070Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2080Sstevel@tonic-gate }
2090Sstevel@tonic-gate 
2108348SEric.Yu@Sun.COM extern so_create_func_t sock_comm_create_function;
2118348SEric.Yu@Sun.COM extern so_destroy_func_t sock_comm_destroy_function;
2120Sstevel@tonic-gate /*
2130Sstevel@tonic-gate  * Init function called when sockfs is loaded.
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate int
2160Sstevel@tonic-gate sockinit(int fstype, char *name)
2170Sstevel@tonic-gate {
2180Sstevel@tonic-gate 	static const fs_operation_def_t sock_vfsops_template[] = {
2190Sstevel@tonic-gate 		NULL, NULL
2200Sstevel@tonic-gate 	};
2210Sstevel@tonic-gate 	int error;
2220Sstevel@tonic-gate 	major_t dev;
2230Sstevel@tonic-gate 	char *err_str;
2240Sstevel@tonic-gate 
2250Sstevel@tonic-gate 	error = vfs_setfsops(fstype, sock_vfsops_template, NULL);
2260Sstevel@tonic-gate 	if (error != 0) {
2271548Srshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
2281548Srshoaib 		    "sockinit: bad vfs ops template");
2290Sstevel@tonic-gate 		return (error);
2300Sstevel@tonic-gate 	}
2310Sstevel@tonic-gate 
2328348SEric.Yu@Sun.COM 	error = vn_make_ops(name, socket_vnodeops_template,
2338348SEric.Yu@Sun.COM 	    &socket_vnodeops);
2340Sstevel@tonic-gate 	if (error != 0) {
2358348SEric.Yu@Sun.COM 		err_str = "sockinit: bad socket vnode ops template";
2360Sstevel@tonic-gate 		/* vn_make_ops() does not reset socktpi_vnodeops on failure. */
2378348SEric.Yu@Sun.COM 		socket_vnodeops = NULL;
2380Sstevel@tonic-gate 		goto failure;
2390Sstevel@tonic-gate 	}
2400Sstevel@tonic-gate 
2418348SEric.Yu@Sun.COM 	socket_cache = kmem_cache_create("socket_cache",
2428348SEric.Yu@Sun.COM 	    sizeof (struct sonode), 0, sonode_constructor,
2438348SEric.Yu@Sun.COM 	    sonode_destructor, NULL, NULL, NULL, 0);
2440Sstevel@tonic-gate 
2458348SEric.Yu@Sun.COM 	error = socktpi_init();
2463422Snh145002 	if (error != 0) {
2473422Snh145002 		err_str = NULL;
2483422Snh145002 		goto failure;
2493422Snh145002 	}
2503422Snh145002 
2516707Sbrutus 	error = sostr_init();
2526707Sbrutus 	if (error != 0) {
2536707Sbrutus 		err_str = NULL;
2546707Sbrutus 		goto failure;
2556707Sbrutus 	}
2566707Sbrutus 
2570Sstevel@tonic-gate 	/*
2588348SEric.Yu@Sun.COM 	 * Set up the default create and destroy functions
2590Sstevel@tonic-gate 	 */
2608348SEric.Yu@Sun.COM 	sock_comm_create_function = socket_sonode_create;
2618348SEric.Yu@Sun.COM 	sock_comm_destroy_function = socket_sonode_destroy;
2620Sstevel@tonic-gate 
2630Sstevel@tonic-gate 	/*
2640Sstevel@tonic-gate 	 * Build initial list mapping socket parameters to vnode.
2650Sstevel@tonic-gate 	 */
2668348SEric.Yu@Sun.COM 	smod_init();
2678348SEric.Yu@Sun.COM 	smod_add(sotpi_smod_create());
2688348SEric.Yu@Sun.COM 
2698348SEric.Yu@Sun.COM 	sockparams_init();
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	/*
2720Sstevel@tonic-gate 	 * If sockets are needed before init runs /sbin/soconfig
2730Sstevel@tonic-gate 	 * it is possible to preload the sockparams list here using
2740Sstevel@tonic-gate 	 * calls like:
2750Sstevel@tonic-gate 	 *	sockconfig(1,2,3, "/dev/tcp", 0);
2760Sstevel@tonic-gate 	 */
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate 	/*
2790Sstevel@tonic-gate 	 * Create a unique dev_t for use in so_fsid.
2800Sstevel@tonic-gate 	 */
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	if ((dev = getudev()) == (major_t)-1)
2830Sstevel@tonic-gate 		dev = 0;
2840Sstevel@tonic-gate 	sockdev = makedevice(dev, 0);
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL);
2870Sstevel@tonic-gate 	sendfile_init();
2888194SJack.Meng@Sun.COM 	if (!modrootloaded) {
2898194SJack.Meng@Sun.COM 		sockfs_defer_nl7c_init = 1;
2908194SJack.Meng@Sun.COM 	} else {
2918194SJack.Meng@Sun.COM 		nl7c_init();
2928194SJack.Meng@Sun.COM 	}
2930Sstevel@tonic-gate 
2940Sstevel@tonic-gate 	return (0);
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate failure:
2970Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(fstype);
2988348SEric.Yu@Sun.COM 	if (socket_vnodeops != NULL)
2998348SEric.Yu@Sun.COM 		vn_freevnodeops(socket_vnodeops);
3000Sstevel@tonic-gate 	if (err_str != NULL)
3011548Srshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str);
3020Sstevel@tonic-gate 	return (error);
3030Sstevel@tonic-gate }
3040Sstevel@tonic-gate 
3050Sstevel@tonic-gate /*
3060Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOLOCKED.
3070Sstevel@tonic-gate  */
3080Sstevel@tonic-gate void
3090Sstevel@tonic-gate so_lock_single(struct sonode *so)
3100Sstevel@tonic-gate {
3110Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate 	while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) {
3140Sstevel@tonic-gate 		so->so_flag |= SOWANT;
3150Sstevel@tonic-gate 		cv_wait_stop(&so->so_want_cv, &so->so_lock,
3165753Sgww 		    SO_LOCK_WAKEUP_TIME);
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate 	so->so_flag |= SOLOCKED;
3190Sstevel@tonic-gate }
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate /*
3220Sstevel@tonic-gate  * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND.
3230Sstevel@tonic-gate  * Used to clear SOLOCKED or SOASYNC_UNBIND.
3240Sstevel@tonic-gate  */
3250Sstevel@tonic-gate void
3260Sstevel@tonic-gate so_unlock_single(struct sonode *so, int flag)
3270Sstevel@tonic-gate {
3280Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3290Sstevel@tonic-gate 	ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND));
3300Sstevel@tonic-gate 	ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0);
3310Sstevel@tonic-gate 	ASSERT(so->so_flag & flag);
3320Sstevel@tonic-gate 	/*
3338348SEric.Yu@Sun.COM 	 * Process the T_DISCON_IND on sti_discon_ind_mp.
3340Sstevel@tonic-gate 	 *
3350Sstevel@tonic-gate 	 * Call to so_drain_discon_ind will result in so_lock
3360Sstevel@tonic-gate 	 * being dropped and re-acquired later.
3370Sstevel@tonic-gate 	 */
3388348SEric.Yu@Sun.COM 	if (!SOCK_IS_NONSTR(so)) {
3398348SEric.Yu@Sun.COM 		sotpi_info_t *sti = SOTOTPI(so);
3408348SEric.Yu@Sun.COM 
3418348SEric.Yu@Sun.COM 		if (sti->sti_discon_ind_mp != NULL)
3428348SEric.Yu@Sun.COM 			so_drain_discon_ind(so);
3438348SEric.Yu@Sun.COM 	}
3440Sstevel@tonic-gate 
3450Sstevel@tonic-gate 	if (so->so_flag & SOWANT)
3460Sstevel@tonic-gate 		cv_broadcast(&so->so_want_cv);
3470Sstevel@tonic-gate 	so->so_flag &= ~(SOWANT|flag);
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate /*
3510Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOREADLOCKED.
3520Sstevel@tonic-gate  * If the caller wants nonblocking behavior it should set fmode.
3530Sstevel@tonic-gate  */
3540Sstevel@tonic-gate int
3550Sstevel@tonic-gate so_lock_read(struct sonode *so, int fmode)
3560Sstevel@tonic-gate {
3570Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
3600Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
3610Sstevel@tonic-gate 			return (EWOULDBLOCK);
3620Sstevel@tonic-gate 		so->so_flag |= SOWANT;
3630Sstevel@tonic-gate 		cv_wait_stop(&so->so_want_cv, &so->so_lock,
3645753Sgww 		    SO_LOCK_WAKEUP_TIME);
3650Sstevel@tonic-gate 	}
3660Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
3670Sstevel@tonic-gate 	return (0);
3680Sstevel@tonic-gate }
3690Sstevel@tonic-gate 
3700Sstevel@tonic-gate /*
3710Sstevel@tonic-gate  * Like so_lock_read above but allows signals.
3720Sstevel@tonic-gate  */
3730Sstevel@tonic-gate int
3740Sstevel@tonic-gate so_lock_read_intr(struct sonode *so, int fmode)
3750Sstevel@tonic-gate {
3760Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
3790Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
3800Sstevel@tonic-gate 			return (EWOULDBLOCK);
3810Sstevel@tonic-gate 		so->so_flag |= SOWANT;
3820Sstevel@tonic-gate 		if (!cv_wait_sig(&so->so_want_cv, &so->so_lock))
3830Sstevel@tonic-gate 			return (EINTR);
3840Sstevel@tonic-gate 	}
3850Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
3860Sstevel@tonic-gate 	return (0);
3870Sstevel@tonic-gate }
3880Sstevel@tonic-gate 
3890Sstevel@tonic-gate /*
3900Sstevel@tonic-gate  * Caller must hold the mutex. Used to clear SOREADLOCKED,
3910Sstevel@tonic-gate  * set in so_lock_read() or so_lock_read_intr().
3920Sstevel@tonic-gate  */
3930Sstevel@tonic-gate void
3940Sstevel@tonic-gate so_unlock_read(struct sonode *so)
3950Sstevel@tonic-gate {
3960Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3970Sstevel@tonic-gate 	ASSERT(so->so_flag & SOREADLOCKED);
3980Sstevel@tonic-gate 
3990Sstevel@tonic-gate 	if (so->so_flag & SOWANT)
4000Sstevel@tonic-gate 		cv_broadcast(&so->so_want_cv);
4010Sstevel@tonic-gate 	so->so_flag &= ~(SOWANT|SOREADLOCKED);
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate  * Verify that the specified offset falls within the mblk and
4060Sstevel@tonic-gate  * that the resulting pointer is aligned.
4070Sstevel@tonic-gate  * Returns NULL if not.
4080Sstevel@tonic-gate  */
4090Sstevel@tonic-gate void *
4100Sstevel@tonic-gate sogetoff(mblk_t *mp, t_uscalar_t offset,
4110Sstevel@tonic-gate     t_uscalar_t length, uint_t align_size)
4120Sstevel@tonic-gate {
4130Sstevel@tonic-gate 	uintptr_t ptr1, ptr2;
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	ASSERT(mp && mp->b_wptr >= mp->b_rptr);
4160Sstevel@tonic-gate 	ptr1 = (uintptr_t)mp->b_rptr + offset;
4170Sstevel@tonic-gate 	ptr2 = (uintptr_t)ptr1 + length;
4180Sstevel@tonic-gate 	if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) {
4190Sstevel@tonic-gate 		eprintline(0);
4200Sstevel@tonic-gate 		return (NULL);
4210Sstevel@tonic-gate 	}
4220Sstevel@tonic-gate 	if ((ptr1 & (align_size - 1)) != 0) {
4230Sstevel@tonic-gate 		eprintline(0);
4240Sstevel@tonic-gate 		return (NULL);
4250Sstevel@tonic-gate 	}
4260Sstevel@tonic-gate 	return ((void *)ptr1);
4270Sstevel@tonic-gate }
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  * Return the AF_UNIX underlying filesystem vnode matching a given name.
4310Sstevel@tonic-gate  * Makes sure the sending and the destination sonodes are compatible.
4320Sstevel@tonic-gate  * The vnode is returned held.
4330Sstevel@tonic-gate  *
4340Sstevel@tonic-gate  * The underlying filesystem VSOCK vnode has a v_stream pointer that
4350Sstevel@tonic-gate  * references the actual stream head (hence indirectly the actual sonode).
4360Sstevel@tonic-gate  */
4370Sstevel@tonic-gate static int
4380Sstevel@tonic-gate so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess,
4390Sstevel@tonic-gate 		vnode_t **vpp)
4400Sstevel@tonic-gate {
4410Sstevel@tonic-gate 	vnode_t		*vp;	/* Underlying filesystem vnode */
4427409SRic.Aleshire@Sun.COM 	vnode_t		*rvp;	/* real vnode */
4430Sstevel@tonic-gate 	vnode_t		*svp;	/* sockfs vnode */
4440Sstevel@tonic-gate 	struct sonode	*so2;
4450Sstevel@tonic-gate 	int		error;
4460Sstevel@tonic-gate 
4477242Srh87107 	dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", (void *)so,
4487242Srh87107 	    soun->sun_path));
4490Sstevel@tonic-gate 
4500Sstevel@tonic-gate 	error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
4510Sstevel@tonic-gate 	if (error) {
4520Sstevel@tonic-gate 		eprintsoline(so, error);
4530Sstevel@tonic-gate 		return (error);
4540Sstevel@tonic-gate 	}
4557409SRic.Aleshire@Sun.COM 
4567409SRic.Aleshire@Sun.COM 	/*
4577409SRic.Aleshire@Sun.COM 	 * Traverse lofs mounts get the real vnode
4587409SRic.Aleshire@Sun.COM 	 */
4597409SRic.Aleshire@Sun.COM 	if (VOP_REALVP(vp, &rvp, NULL) == 0) {
4607409SRic.Aleshire@Sun.COM 		VN_HOLD(rvp);		/* hold the real vnode */
4617409SRic.Aleshire@Sun.COM 		VN_RELE(vp);		/* release hold from lookup */
4627409SRic.Aleshire@Sun.COM 		vp = rvp;
4637409SRic.Aleshire@Sun.COM 	}
4647409SRic.Aleshire@Sun.COM 
4650Sstevel@tonic-gate 	if (vp->v_type != VSOCK) {
4660Sstevel@tonic-gate 		error = ENOTSOCK;
4670Sstevel@tonic-gate 		eprintsoline(so, error);
4680Sstevel@tonic-gate 		goto done2;
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 
4710Sstevel@tonic-gate 	if (checkaccess) {
4720Sstevel@tonic-gate 		/*
4730Sstevel@tonic-gate 		 * Check that we have permissions to access the destination
4740Sstevel@tonic-gate 		 * vnode. This check is not done in BSD but it is required
4750Sstevel@tonic-gate 		 * by X/Open.
4760Sstevel@tonic-gate 		 */
4775331Samw 		if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL)) {
4780Sstevel@tonic-gate 			eprintsoline(so, error);
4790Sstevel@tonic-gate 			goto done2;
4800Sstevel@tonic-gate 		}
4810Sstevel@tonic-gate 	}
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	/*
4840Sstevel@tonic-gate 	 * Check if the remote socket has been closed.
4850Sstevel@tonic-gate 	 *
4860Sstevel@tonic-gate 	 * Synchronize with vn_rele_stream by holding v_lock while traversing
4870Sstevel@tonic-gate 	 * v_stream->sd_vnode.
4880Sstevel@tonic-gate 	 */
4890Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
4900Sstevel@tonic-gate 	if (vp->v_stream == NULL) {
4910Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4920Sstevel@tonic-gate 		if (so->so_type == SOCK_DGRAM)
4930Sstevel@tonic-gate 			error = EDESTADDRREQ;
4940Sstevel@tonic-gate 		else
4950Sstevel@tonic-gate 			error = ECONNREFUSED;
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate 		eprintsoline(so, error);
4980Sstevel@tonic-gate 		goto done2;
4990Sstevel@tonic-gate 	}
5000Sstevel@tonic-gate 	ASSERT(vp->v_stream->sd_vnode);
5010Sstevel@tonic-gate 	svp = vp->v_stream->sd_vnode;
5020Sstevel@tonic-gate 	/*
5030Sstevel@tonic-gate 	 * holding v_lock on underlying filesystem vnode and acquiring
5040Sstevel@tonic-gate 	 * it on sockfs vnode. Assumes that no code ever attempts to
5050Sstevel@tonic-gate 	 * acquire these locks in the reverse order.
5060Sstevel@tonic-gate 	 */
5070Sstevel@tonic-gate 	VN_HOLD(svp);
5080Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	if (svp->v_type != VSOCK) {
5110Sstevel@tonic-gate 		error = ENOTSOCK;
5120Sstevel@tonic-gate 		eprintsoline(so, error);
5130Sstevel@tonic-gate 		goto done;
5140Sstevel@tonic-gate 	}
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate 	so2 = VTOSO(svp);
5170Sstevel@tonic-gate 
5180Sstevel@tonic-gate 	if (so->so_type != so2->so_type) {
5190Sstevel@tonic-gate 		error = EPROTOTYPE;
5200Sstevel@tonic-gate 		eprintsoline(so, error);
5210Sstevel@tonic-gate 		goto done;
5220Sstevel@tonic-gate 	}
5230Sstevel@tonic-gate 
5240Sstevel@tonic-gate 	VN_RELE(svp);
5250Sstevel@tonic-gate 	*vpp = vp;
5260Sstevel@tonic-gate 	return (0);
5270Sstevel@tonic-gate 
5280Sstevel@tonic-gate done:
5290Sstevel@tonic-gate 	VN_RELE(svp);
5300Sstevel@tonic-gate done2:
5310Sstevel@tonic-gate 	VN_RELE(vp);
5320Sstevel@tonic-gate 	return (error);
5330Sstevel@tonic-gate }
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate /*
5360Sstevel@tonic-gate  * Verify peer address for connect and sendto/sendmsg.
5370Sstevel@tonic-gate  * Since sendto/sendmsg would not get synchronous errors from the transport
5380Sstevel@tonic-gate  * provider we have to do these ugly checks in the socket layer to
5390Sstevel@tonic-gate  * preserve compatibility with SunOS 4.X.
5400Sstevel@tonic-gate  */
5410Sstevel@tonic-gate int
5420Sstevel@tonic-gate so_addr_verify(struct sonode *so, const struct sockaddr *name,
5430Sstevel@tonic-gate     socklen_t namelen)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	int		family;
5460Sstevel@tonic-gate 
5477240Srh87107 	dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n",
5487240Srh87107 	    (void *)so, (void *)name, namelen));
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	ASSERT(name != NULL);
5510Sstevel@tonic-gate 
5520Sstevel@tonic-gate 	family = so->so_family;
5530Sstevel@tonic-gate 	switch (family) {
5540Sstevel@tonic-gate 	case AF_INET:
5550Sstevel@tonic-gate 		if (name->sa_family != family) {
5560Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5570Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5580Sstevel@tonic-gate 		}
5590Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
5600Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
5610Sstevel@tonic-gate 			return (EINVAL);
5620Sstevel@tonic-gate 		}
5630Sstevel@tonic-gate 		break;
5640Sstevel@tonic-gate 	case AF_INET6: {
5650Sstevel@tonic-gate #ifdef DEBUG
5660Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
5670Sstevel@tonic-gate #endif /* DEBUG */
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 		if (name->sa_family != family) {
5700Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5710Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
5740Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
5750Sstevel@tonic-gate 			return (EINVAL);
5760Sstevel@tonic-gate 		}
5770Sstevel@tonic-gate #ifdef DEBUG
5780Sstevel@tonic-gate 		/* Verify that apps don't forget to clear sin6_scope_id etc */
5790Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)name;
5800Sstevel@tonic-gate 		if (sin6->sin6_scope_id != 0 &&
5810Sstevel@tonic-gate 		    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
5821548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
5830Sstevel@tonic-gate 			    "connect/send* with uninitialized sin6_scope_id "
5840Sstevel@tonic-gate 			    "(%d) on socket. Pid = %d\n",
5850Sstevel@tonic-gate 			    (int)sin6->sin6_scope_id, (int)curproc->p_pid);
5860Sstevel@tonic-gate 		}
5870Sstevel@tonic-gate #endif /* DEBUG */
5880Sstevel@tonic-gate 		break;
5890Sstevel@tonic-gate 	}
5900Sstevel@tonic-gate 	case AF_UNIX:
5918348SEric.Yu@Sun.COM 		if (SOTOTPI(so)->sti_faddr_noxlate) {
5920Sstevel@tonic-gate 			return (0);
5930Sstevel@tonic-gate 		}
5940Sstevel@tonic-gate 		if (namelen < (socklen_t)sizeof (short)) {
5950Sstevel@tonic-gate 			eprintsoline(so, ENOENT);
5960Sstevel@tonic-gate 			return (ENOENT);
5970Sstevel@tonic-gate 		}
5980Sstevel@tonic-gate 		if (name->sa_family != family) {
5990Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
6000Sstevel@tonic-gate 			return (EAFNOSUPPORT);
6010Sstevel@tonic-gate 		}
6020Sstevel@tonic-gate 		/* MAXPATHLEN + soun_family + nul termination */
6030Sstevel@tonic-gate 		if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
6040Sstevel@tonic-gate 			eprintsoline(so, ENAMETOOLONG);
6050Sstevel@tonic-gate 			return (ENAMETOOLONG);
6060Sstevel@tonic-gate 		}
6070Sstevel@tonic-gate 
6080Sstevel@tonic-gate 		break;
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	default:
6110Sstevel@tonic-gate 		/*
6120Sstevel@tonic-gate 		 * Default is don't do any length or sa_family check
6130Sstevel@tonic-gate 		 * to allow non-sockaddr style addresses.
6140Sstevel@tonic-gate 		 */
6150Sstevel@tonic-gate 		break;
6160Sstevel@tonic-gate 	}
6170Sstevel@tonic-gate 
6180Sstevel@tonic-gate 	return (0);
6190Sstevel@tonic-gate }
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 
6220Sstevel@tonic-gate /*
6230Sstevel@tonic-gate  * Translate an AF_UNIX sockaddr_un to the transport internal name.
6240Sstevel@tonic-gate  * Assumes caller has called so_addr_verify first.
6250Sstevel@tonic-gate  */
6260Sstevel@tonic-gate /*ARGSUSED*/
6270Sstevel@tonic-gate int
6280Sstevel@tonic-gate so_ux_addr_xlate(struct sonode *so, struct sockaddr *name,
6290Sstevel@tonic-gate     socklen_t namelen, int checkaccess,
6300Sstevel@tonic-gate     void **addrp, socklen_t *addrlenp)
6310Sstevel@tonic-gate {
6320Sstevel@tonic-gate 	int			error;
6330Sstevel@tonic-gate 	struct sockaddr_un	*soun;
6340Sstevel@tonic-gate 	vnode_t			*vp;
6350Sstevel@tonic-gate 	void			*addr;
6360Sstevel@tonic-gate 	socklen_t		addrlen;
6378348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n",
6407240Srh87107 	    (void *)so, (void *)name, namelen, checkaccess));
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	ASSERT(name != NULL);
6430Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
6448348SEric.Yu@Sun.COM 	ASSERT(!sti->sti_faddr_noxlate);
6450Sstevel@tonic-gate 	ASSERT(namelen >= (socklen_t)sizeof (short));
6460Sstevel@tonic-gate 	ASSERT(name->sa_family == AF_UNIX);
6470Sstevel@tonic-gate 	soun = (struct sockaddr_un *)name;
6480Sstevel@tonic-gate 	/*
6490Sstevel@tonic-gate 	 * Lookup vnode for the specified path name and verify that
6500Sstevel@tonic-gate 	 * it is a socket.
6510Sstevel@tonic-gate 	 */
6520Sstevel@tonic-gate 	error = so_ux_lookup(so, soun, checkaccess, &vp);
6530Sstevel@tonic-gate 	if (error) {
6540Sstevel@tonic-gate 		eprintsoline(so, error);
6550Sstevel@tonic-gate 		return (error);
6560Sstevel@tonic-gate 	}
6570Sstevel@tonic-gate 	/*
6580Sstevel@tonic-gate 	 * Use the address of the peer vnode as the address to send
6590Sstevel@tonic-gate 	 * to. We release the peer vnode here. In case it has been
6600Sstevel@tonic-gate 	 * closed by the time the T_CONN_REQ or T_UNIDATA_REQ reaches the
6610Sstevel@tonic-gate 	 * transport the message will get an error or be dropped.
6620Sstevel@tonic-gate 	 */
6638348SEric.Yu@Sun.COM 	sti->sti_ux_faddr.soua_vp = vp;
6648348SEric.Yu@Sun.COM 	sti->sti_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT;
6658348SEric.Yu@Sun.COM 	addr = &sti->sti_ux_faddr;
6668348SEric.Yu@Sun.COM 	addrlen = (socklen_t)sizeof (sti->sti_ux_faddr);
6677240Srh87107 	dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n",
6687240Srh87107 	    addrlen, (void *)vp));
6690Sstevel@tonic-gate 	VN_RELE(vp);
6700Sstevel@tonic-gate 	*addrp = addr;
6710Sstevel@tonic-gate 	*addrlenp = (socklen_t)addrlen;
6720Sstevel@tonic-gate 	return (0);
6730Sstevel@tonic-gate }
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate /*
6760Sstevel@tonic-gate  * Esballoc free function for messages that contain SO_FILEP option.
6770Sstevel@tonic-gate  * Decrement the reference count on the file pointers using closef.
6780Sstevel@tonic-gate  */
6790Sstevel@tonic-gate void
6800Sstevel@tonic-gate fdbuf_free(struct fdbuf *fdbuf)
6810Sstevel@tonic-gate {
6820Sstevel@tonic-gate 	int	i;
6830Sstevel@tonic-gate 	struct file *fp;
6840Sstevel@tonic-gate 
6850Sstevel@tonic-gate 	dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd));
6860Sstevel@tonic-gate 	for (i = 0; i < fdbuf->fd_numfd; i++) {
6870Sstevel@tonic-gate 		/*
6880Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
6890Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
6900Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
6910Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
6920Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
6930Sstevel@tonic-gate 		 */
6940Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
6957240Srh87107 		dprint(1, ("fdbuf_free: [%d] = %p\n", i, (void *)fp));
6960Sstevel@tonic-gate 		(void) closef(fp);
6970Sstevel@tonic-gate 	}
6980Sstevel@tonic-gate 	if (fdbuf->fd_ebuf != NULL)
6990Sstevel@tonic-gate 		kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen);
7000Sstevel@tonic-gate 	kmem_free(fdbuf, fdbuf->fd_size);
7010Sstevel@tonic-gate }
7020Sstevel@tonic-gate 
7030Sstevel@tonic-gate /*
704455Smeem  * Allocate an esballoc'ed message for AF_UNIX file descriptor passing.
705455Smeem  * Waits if memory is not available.
7060Sstevel@tonic-gate  */
7070Sstevel@tonic-gate mblk_t *
7080Sstevel@tonic-gate fdbuf_allocmsg(int size, struct fdbuf *fdbuf)
7090Sstevel@tonic-gate {
710455Smeem 	uchar_t	*buf;
7110Sstevel@tonic-gate 	mblk_t	*mp;
7120Sstevel@tonic-gate 
7130Sstevel@tonic-gate 	dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd));
7140Sstevel@tonic-gate 	buf = kmem_alloc(size, KM_SLEEP);
7150Sstevel@tonic-gate 	fdbuf->fd_ebuf = (caddr_t)buf;
7160Sstevel@tonic-gate 	fdbuf->fd_ebuflen = size;
7170Sstevel@tonic-gate 	fdbuf->fd_frtn.free_func = fdbuf_free;
7180Sstevel@tonic-gate 	fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf;
7190Sstevel@tonic-gate 
720455Smeem 	mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn);
7210Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
7220Sstevel@tonic-gate 	return (mp);
7230Sstevel@tonic-gate }
7240Sstevel@tonic-gate 
7250Sstevel@tonic-gate /*
7260Sstevel@tonic-gate  * Extract file descriptors from a fdbuf.
7270Sstevel@tonic-gate  * Return list in rights/rightslen.
7280Sstevel@tonic-gate  */
7290Sstevel@tonic-gate /*ARGSUSED*/
7300Sstevel@tonic-gate static int
7310Sstevel@tonic-gate fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen)
7320Sstevel@tonic-gate {
7330Sstevel@tonic-gate 	int	i, fd;
7340Sstevel@tonic-gate 	int	*rp;
7350Sstevel@tonic-gate 	struct file *fp;
7360Sstevel@tonic-gate 	int	numfd;
7370Sstevel@tonic-gate 
7380Sstevel@tonic-gate 	dprint(1, ("fdbuf_extract: %d fds, len %d\n",
7395753Sgww 	    fdbuf->fd_numfd, rightslen));
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	numfd = fdbuf->fd_numfd;
7420Sstevel@tonic-gate 	ASSERT(rightslen == numfd * (int)sizeof (int));
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	/*
7450Sstevel@tonic-gate 	 * Allocate a file descriptor and increment the f_count.
7460Sstevel@tonic-gate 	 * The latter is needed since we always call fdbuf_free
7470Sstevel@tonic-gate 	 * which performs a closef.
7480Sstevel@tonic-gate 	 */
7490Sstevel@tonic-gate 	rp = (int *)rights;
7500Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
7510Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1)
7520Sstevel@tonic-gate 			goto cleanup;
7530Sstevel@tonic-gate 		/*
7540Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
7550Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
7560Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
7570Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
7580Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
7590Sstevel@tonic-gate 		 */
7600Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
7610Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
7620Sstevel@tonic-gate 		fp->f_count++;
7630Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
7640Sstevel@tonic-gate 		setf(fd, fp);
7650Sstevel@tonic-gate 		*rp++ = fd;
7660Sstevel@tonic-gate 		if (audit_active)
7670Sstevel@tonic-gate 			audit_fdrecv(fd, fp);
7680Sstevel@tonic-gate 		dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
7697240Srh87107 		    i, fd, (void *)fp, fp->f_count));
7700Sstevel@tonic-gate 	}
7710Sstevel@tonic-gate 	return (0);
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate cleanup:
7740Sstevel@tonic-gate 	/*
7750Sstevel@tonic-gate 	 * Undo whatever partial work the loop above has done.
7760Sstevel@tonic-gate 	 */
7770Sstevel@tonic-gate 	{
7780Sstevel@tonic-gate 		int j;
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 		rp = (int *)rights;
7810Sstevel@tonic-gate 		for (j = 0; j < i; j++) {
7820Sstevel@tonic-gate 			dprint(0,
7830Sstevel@tonic-gate 			    ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
7840Sstevel@tonic-gate 			(void) closeandsetf(*rp++, NULL);
7850Sstevel@tonic-gate 		}
7860Sstevel@tonic-gate 	}
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	return (EMFILE);
7890Sstevel@tonic-gate }
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate /*
7920Sstevel@tonic-gate  * Insert file descriptors into an fdbuf.
7930Sstevel@tonic-gate  * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
7940Sstevel@tonic-gate  * by calling fdbuf_free().
7950Sstevel@tonic-gate  */
7960Sstevel@tonic-gate int
7970Sstevel@tonic-gate fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
7980Sstevel@tonic-gate {
7990Sstevel@tonic-gate 	int		numfd, i;
8000Sstevel@tonic-gate 	int		*fds;
8010Sstevel@tonic-gate 	struct file	*fp;
8020Sstevel@tonic-gate 	struct fdbuf	*fdbuf;
8030Sstevel@tonic-gate 	int		fdbufsize;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	dprint(1, ("fdbuf_create: len %d\n", rightslen));
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate 	fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
8100Sstevel@tonic-gate 	fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
8110Sstevel@tonic-gate 	fdbuf->fd_size = fdbufsize;
8120Sstevel@tonic-gate 	fdbuf->fd_numfd = 0;
8130Sstevel@tonic-gate 	fdbuf->fd_ebuf = NULL;
8140Sstevel@tonic-gate 	fdbuf->fd_ebuflen = 0;
8150Sstevel@tonic-gate 	fds = (int *)rights;
8160Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
8170Sstevel@tonic-gate 		if ((fp = getf(fds[i])) == NULL) {
8180Sstevel@tonic-gate 			fdbuf_free(fdbuf);
8190Sstevel@tonic-gate 			return (EBADF);
8200Sstevel@tonic-gate 		}
8210Sstevel@tonic-gate 		dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
8227240Srh87107 		    i, fds[i], (void *)fp, fp->f_count));
8230Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
8240Sstevel@tonic-gate 		fp->f_count++;
8250Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
8260Sstevel@tonic-gate 		/*
8270Sstevel@tonic-gate 		 * The maximum alignment for fdbuf (or any option header
8280Sstevel@tonic-gate 		 * and its value) it 4 bytes. On a LP64 kernel, the alignment
8290Sstevel@tonic-gate 		 * is not sufficient for pointers (fd_fds in this case). Since
8300Sstevel@tonic-gate 		 * we just did a kmem_alloc (we get a double word alignment),
8310Sstevel@tonic-gate 		 * we don't need to do anything on the send side (we loose
8320Sstevel@tonic-gate 		 * the double word alignment because fdbuf goes after an
8330Sstevel@tonic-gate 		 * option header (eg T_unitdata_req) which is only 4 byte
8340Sstevel@tonic-gate 		 * aligned). We take care of this when we extract the file
8350Sstevel@tonic-gate 		 * descriptor in fdbuf_extract or fdbuf_free.
8360Sstevel@tonic-gate 		 */
8370Sstevel@tonic-gate 		fdbuf->fd_fds[i] = fp;
8380Sstevel@tonic-gate 		fdbuf->fd_numfd++;
8390Sstevel@tonic-gate 		releasef(fds[i]);
8400Sstevel@tonic-gate 		if (audit_active)
8410Sstevel@tonic-gate 			audit_fdsend(fds[i], fp, 0);
8420Sstevel@tonic-gate 	}
8430Sstevel@tonic-gate 	*fdbufp = fdbuf;
8440Sstevel@tonic-gate 	return (0);
8450Sstevel@tonic-gate }
8460Sstevel@tonic-gate 
8470Sstevel@tonic-gate static int
8480Sstevel@tonic-gate fdbuf_optlen(int rightslen)
8490Sstevel@tonic-gate {
8500Sstevel@tonic-gate 	int numfd;
8510Sstevel@tonic-gate 
8520Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
8550Sstevel@tonic-gate }
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate static t_uscalar_t
8580Sstevel@tonic-gate fdbuf_cmsglen(int fdbuflen)
8590Sstevel@tonic-gate {
8600Sstevel@tonic-gate 	return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
8610Sstevel@tonic-gate 	    (int)sizeof (struct file *) * (int)sizeof (int));
8620Sstevel@tonic-gate }
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 
8650Sstevel@tonic-gate /*
8660Sstevel@tonic-gate  * Return non-zero if the mblk and fdbuf are consistent.
8670Sstevel@tonic-gate  */
8680Sstevel@tonic-gate static int
8690Sstevel@tonic-gate fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
8700Sstevel@tonic-gate {
8710Sstevel@tonic-gate 	if (fdbuflen >= FDBUF_HDRSIZE &&
8720Sstevel@tonic-gate 	    fdbuflen == fdbuf->fd_size) {
8730Sstevel@tonic-gate 		frtn_t *frp = mp->b_datap->db_frtnp;
8740Sstevel@tonic-gate 		/*
8750Sstevel@tonic-gate 		 * Check that the SO_FILEP portion of the
8760Sstevel@tonic-gate 		 * message has not been modified by
8770Sstevel@tonic-gate 		 * the loopback transport. The sending sockfs generates
8780Sstevel@tonic-gate 		 * a message that is esballoc'ed with the free function
8790Sstevel@tonic-gate 		 * being fdbuf_free() and where free_arg contains the
8800Sstevel@tonic-gate 		 * identical information as the SO_FILEP content.
8810Sstevel@tonic-gate 		 *
8820Sstevel@tonic-gate 		 * If any of these constraints are not satisfied we
8830Sstevel@tonic-gate 		 * silently ignore the option.
8840Sstevel@tonic-gate 		 */
8850Sstevel@tonic-gate 		ASSERT(mp);
8860Sstevel@tonic-gate 		if (frp != NULL &&
8870Sstevel@tonic-gate 		    frp->free_func == fdbuf_free &&
8880Sstevel@tonic-gate 		    frp->free_arg != NULL &&
8890Sstevel@tonic-gate 		    bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
8900Sstevel@tonic-gate 			dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
8917240Srh87107 			    (void *)fdbuf, fdbuflen));
8920Sstevel@tonic-gate 			return (1);
8930Sstevel@tonic-gate 		} else {
8941548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
8950Sstevel@tonic-gate 			    "sockfs: mismatched fdbuf content (%p)",
8960Sstevel@tonic-gate 			    (void *)mp);
8970Sstevel@tonic-gate 			return (0);
8980Sstevel@tonic-gate 		}
8990Sstevel@tonic-gate 	} else {
9001548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
9010Sstevel@tonic-gate 		    "sockfs: mismatched fdbuf len %d, %d\n",
9020Sstevel@tonic-gate 		    fdbuflen, fdbuf->fd_size);
9030Sstevel@tonic-gate 		return (0);
9040Sstevel@tonic-gate 	}
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate  * When the file descriptors returned by sorecvmsg can not be passed
9090Sstevel@tonic-gate  * to the application this routine will cleanup the references on
9100Sstevel@tonic-gate  * the files. Start at startoff bytes into the buffer.
9110Sstevel@tonic-gate  */
9120Sstevel@tonic-gate static void
9130Sstevel@tonic-gate close_fds(void *fdbuf, int fdbuflen, int startoff)
9140Sstevel@tonic-gate {
9150Sstevel@tonic-gate 	int *fds = (int *)fdbuf;
9160Sstevel@tonic-gate 	int numfd = fdbuflen / (int)sizeof (int);
9170Sstevel@tonic-gate 	int i;
9180Sstevel@tonic-gate 
9190Sstevel@tonic-gate 	dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
9200Sstevel@tonic-gate 
9210Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
9220Sstevel@tonic-gate 		if (startoff < 0)
9230Sstevel@tonic-gate 			startoff = 0;
9240Sstevel@tonic-gate 		if (startoff < (int)sizeof (int)) {
9250Sstevel@tonic-gate 			/*
9260Sstevel@tonic-gate 			 * This file descriptor is partially or fully after
9270Sstevel@tonic-gate 			 * the offset
9280Sstevel@tonic-gate 			 */
9290Sstevel@tonic-gate 			dprint(0,
9300Sstevel@tonic-gate 			    ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
9310Sstevel@tonic-gate 			(void) closeandsetf(fds[i], NULL);
9320Sstevel@tonic-gate 		}
9330Sstevel@tonic-gate 		startoff -= (int)sizeof (int);
9340Sstevel@tonic-gate 	}
9350Sstevel@tonic-gate }
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate /*
9380Sstevel@tonic-gate  * Close all file descriptors contained in the control part starting at
9390Sstevel@tonic-gate  * the startoffset.
9400Sstevel@tonic-gate  */
9410Sstevel@tonic-gate void
9420Sstevel@tonic-gate so_closefds(void *control, t_uscalar_t controllen, int oldflg,
9430Sstevel@tonic-gate     int startoff)
9440Sstevel@tonic-gate {
9450Sstevel@tonic-gate 	struct cmsghdr *cmsg;
9460Sstevel@tonic-gate 
9470Sstevel@tonic-gate 	if (control == NULL)
9480Sstevel@tonic-gate 		return;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	if (oldflg) {
9510Sstevel@tonic-gate 		close_fds(control, controllen, startoff);
9520Sstevel@tonic-gate 		return;
9530Sstevel@tonic-gate 	}
9540Sstevel@tonic-gate 	/* Scan control part for file descriptors. */
9550Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
9560Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
9570Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
9580Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
9590Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
9600Sstevel@tonic-gate 			close_fds(CMSG_CONTENT(cmsg),
9610Sstevel@tonic-gate 			    (int)CMSG_CONTENTLEN(cmsg),
9620Sstevel@tonic-gate 			    startoff - (int)sizeof (struct cmsghdr));
9630Sstevel@tonic-gate 		}
9640Sstevel@tonic-gate 		startoff -= cmsg->cmsg_len;
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate }
9670Sstevel@tonic-gate 
9680Sstevel@tonic-gate /*
9690Sstevel@tonic-gate  * Returns a pointer/length for the file descriptors contained
9700Sstevel@tonic-gate  * in the control buffer. Returns with *fdlenp == -1 if there are no
9710Sstevel@tonic-gate  * file descriptor options present. This is different than there being
9720Sstevel@tonic-gate  * a zero-length file descriptor option.
9730Sstevel@tonic-gate  * Fail if there are multiple SCM_RIGHT cmsgs.
9740Sstevel@tonic-gate  */
9750Sstevel@tonic-gate int
9760Sstevel@tonic-gate so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
9770Sstevel@tonic-gate     void **fdsp, int *fdlenp)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate 	struct cmsghdr *cmsg;
9800Sstevel@tonic-gate 	void *fds;
9810Sstevel@tonic-gate 	int fdlen;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate 	if (control == NULL) {
9840Sstevel@tonic-gate 		*fdsp = NULL;
9850Sstevel@tonic-gate 		*fdlenp = -1;
9860Sstevel@tonic-gate 		return (0);
9870Sstevel@tonic-gate 	}
9880Sstevel@tonic-gate 
9890Sstevel@tonic-gate 	if (oldflg) {
9900Sstevel@tonic-gate 		*fdsp = control;
9910Sstevel@tonic-gate 		if (controllen == 0)
9920Sstevel@tonic-gate 			*fdlenp = -1;
9930Sstevel@tonic-gate 		else
9940Sstevel@tonic-gate 			*fdlenp = controllen;
9950Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
9960Sstevel@tonic-gate 		return (0);
9970Sstevel@tonic-gate 	}
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate 	fds = NULL;
10000Sstevel@tonic-gate 	fdlen = 0;
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10030Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10040Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10050Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10060Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
10070Sstevel@tonic-gate 			if (fds != NULL)
10080Sstevel@tonic-gate 				return (EINVAL);
10090Sstevel@tonic-gate 			fds = CMSG_CONTENT(cmsg);
10100Sstevel@tonic-gate 			fdlen = (int)CMSG_CONTENTLEN(cmsg);
1011408Skrgopi 			dprint(1, ("so_getfdopt: new %lu\n",
10125753Sgww 			    (size_t)CMSG_CONTENTLEN(cmsg)));
10130Sstevel@tonic-gate 		}
10140Sstevel@tonic-gate 	}
10150Sstevel@tonic-gate 	if (fds == NULL) {
10160Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: NONE\n"));
10170Sstevel@tonic-gate 		*fdlenp = -1;
10180Sstevel@tonic-gate 	} else
10190Sstevel@tonic-gate 		*fdlenp = fdlen;
10200Sstevel@tonic-gate 	*fdsp = fds;
10210Sstevel@tonic-gate 	return (0);
10220Sstevel@tonic-gate }
10230Sstevel@tonic-gate 
10240Sstevel@tonic-gate /*
10250Sstevel@tonic-gate  * Return the length of the options including any file descriptor options.
10260Sstevel@tonic-gate  */
10270Sstevel@tonic-gate t_uscalar_t
10280Sstevel@tonic-gate so_optlen(void *control, t_uscalar_t controllen, int oldflg)
10290Sstevel@tonic-gate {
10300Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10310Sstevel@tonic-gate 	t_uscalar_t optlen = 0;
10320Sstevel@tonic-gate 	t_uscalar_t len;
10330Sstevel@tonic-gate 
10340Sstevel@tonic-gate 	if (control == NULL)
10350Sstevel@tonic-gate 		return (0);
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	if (oldflg)
10380Sstevel@tonic-gate 		return ((t_uscalar_t)(sizeof (struct T_opthdr) +
10390Sstevel@tonic-gate 		    fdbuf_optlen(controllen)));
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10420Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10430Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10440Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10450Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
10460Sstevel@tonic-gate 			len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
10470Sstevel@tonic-gate 		} else {
10480Sstevel@tonic-gate 			len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
10490Sstevel@tonic-gate 		}
10500Sstevel@tonic-gate 		optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
10510Sstevel@tonic-gate 		    sizeof (struct T_opthdr));
10520Sstevel@tonic-gate 	}
10530Sstevel@tonic-gate 	dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
10545753Sgww 	    controllen, oldflg, optlen));
10550Sstevel@tonic-gate 	return (optlen);
10560Sstevel@tonic-gate }
10570Sstevel@tonic-gate 
10580Sstevel@tonic-gate /*
10590Sstevel@tonic-gate  * Copy options from control to the mblk. Skip any file descriptor options.
10600Sstevel@tonic-gate  */
10610Sstevel@tonic-gate void
10620Sstevel@tonic-gate so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
10630Sstevel@tonic-gate {
10640Sstevel@tonic-gate 	struct T_opthdr toh;
10650Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10660Sstevel@tonic-gate 
10670Sstevel@tonic-gate 	if (control == NULL)
10680Sstevel@tonic-gate 		return;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (oldflg) {
10710Sstevel@tonic-gate 		/* No real options - caller has handled file descriptors */
10720Sstevel@tonic-gate 		return;
10730Sstevel@tonic-gate 	}
10740Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10750Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10760Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10770Sstevel@tonic-gate 		/*
10780Sstevel@tonic-gate 		 * Note: The caller handles file descriptors prior
10790Sstevel@tonic-gate 		 * to calling this function.
10800Sstevel@tonic-gate 		 */
10810Sstevel@tonic-gate 		t_uscalar_t len;
10820Sstevel@tonic-gate 
10830Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10840Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS)
10850Sstevel@tonic-gate 			continue;
10860Sstevel@tonic-gate 
10870Sstevel@tonic-gate 		len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
10880Sstevel@tonic-gate 		toh.level = cmsg->cmsg_level;
10890Sstevel@tonic-gate 		toh.name = cmsg->cmsg_type;
10900Sstevel@tonic-gate 		toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
10910Sstevel@tonic-gate 		toh.status = 0;
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
10940Sstevel@tonic-gate 		soappendmsg(mp, CMSG_CONTENT(cmsg), len);
10950Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
10960Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
10970Sstevel@tonic-gate 	}
10980Sstevel@tonic-gate }
10990Sstevel@tonic-gate 
11000Sstevel@tonic-gate /*
11010Sstevel@tonic-gate  * Return the length of the control message derived from the options.
11020Sstevel@tonic-gate  * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
11030Sstevel@tonic-gate  * When oldflg is set only include SO_FILEP.
11042280Sgt145670  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
11052280Sgt145670  * allocates the space that so_opt2cmsg fills. If one changes, the other should
11062280Sgt145670  * also be checked for any possible impacts.
11070Sstevel@tonic-gate  */
11080Sstevel@tonic-gate t_uscalar_t
11090Sstevel@tonic-gate so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
11100Sstevel@tonic-gate {
11110Sstevel@tonic-gate 	t_uscalar_t cmsglen = 0;
11120Sstevel@tonic-gate 	struct T_opthdr *tohp;
11130Sstevel@tonic-gate 	t_uscalar_t len;
11140Sstevel@tonic-gate 	t_uscalar_t last_roundup = 0;
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
11170Sstevel@tonic-gate 
11180Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
11190Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
11200Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
11210Sstevel@tonic-gate 		dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
11225753Sgww 		    tohp->level, tohp->name, tohp->len));
11230Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
11240Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
11250Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
11260Sstevel@tonic-gate 			continue;
11270Sstevel@tonic-gate 		}
11280Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
11290Sstevel@tonic-gate 			struct fdbuf *fdbuf;
11300Sstevel@tonic-gate 			int fdbuflen;
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
11330Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
11360Sstevel@tonic-gate 				continue;
11370Sstevel@tonic-gate 			if (oldflg) {
11380Sstevel@tonic-gate 				cmsglen += fdbuf_cmsglen(fdbuflen);
11390Sstevel@tonic-gate 				continue;
11400Sstevel@tonic-gate 			}
11410Sstevel@tonic-gate 			len = fdbuf_cmsglen(fdbuflen);
11422280Sgt145670 		} else if (tohp->level == SOL_SOCKET &&
11432280Sgt145670 		    tohp->name == SCM_TIMESTAMP) {
11442280Sgt145670 			if (oldflg)
11452280Sgt145670 				continue;
11462280Sgt145670 
11472280Sgt145670 			if (get_udatamodel() == DATAMODEL_NATIVE) {
11482280Sgt145670 				len = sizeof (struct timeval);
11492280Sgt145670 			} else {
11502280Sgt145670 				len = sizeof (struct timeval32);
11512280Sgt145670 			}
11520Sstevel@tonic-gate 		} else {
11530Sstevel@tonic-gate 			if (oldflg)
11540Sstevel@tonic-gate 				continue;
11550Sstevel@tonic-gate 			len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
11560Sstevel@tonic-gate 		}
11570Sstevel@tonic-gate 		/*
11582280Sgt145670 		 * Exclude roundup for last option to not set
11590Sstevel@tonic-gate 		 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
11600Sstevel@tonic-gate 		 */
11610Sstevel@tonic-gate 		last_roundup = (t_uscalar_t)
11620Sstevel@tonic-gate 		    (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
11630Sstevel@tonic-gate 		    (len + (int)sizeof (struct cmsghdr)));
11640Sstevel@tonic-gate 		cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
11650Sstevel@tonic-gate 		    last_roundup;
11660Sstevel@tonic-gate 	}
11670Sstevel@tonic-gate 	cmsglen -= last_roundup;
11680Sstevel@tonic-gate 	dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
11695753Sgww 	    optlen, oldflg, cmsglen));
11700Sstevel@tonic-gate 	return (cmsglen);
11710Sstevel@tonic-gate }
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate /*
11740Sstevel@tonic-gate  * Copy options from options to the control. Convert SO_FILEP to
11750Sstevel@tonic-gate  * file descriptors.
11760Sstevel@tonic-gate  * Returns errno or zero.
11772280Sgt145670  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
11782280Sgt145670  * allocates the space that so_opt2cmsg fills. If one changes, the other should
11792280Sgt145670  * also be checked for any possible impacts.
11800Sstevel@tonic-gate  */
11810Sstevel@tonic-gate int
11820Sstevel@tonic-gate so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
11830Sstevel@tonic-gate     void *control, t_uscalar_t controllen)
11840Sstevel@tonic-gate {
11850Sstevel@tonic-gate 	struct T_opthdr *tohp;
11860Sstevel@tonic-gate 	struct cmsghdr *cmsg;
11870Sstevel@tonic-gate 	struct fdbuf *fdbuf;
11880Sstevel@tonic-gate 	int fdbuflen;
11890Sstevel@tonic-gate 	int error;
11902280Sgt145670 #if defined(DEBUG) || defined(__lint)
11912280Sgt145670 	struct cmsghdr *cend = (struct cmsghdr *)
11922280Sgt145670 	    (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
11932280Sgt145670 #endif
11940Sstevel@tonic-gate 	cmsg = (struct cmsghdr *)control;
11950Sstevel@tonic-gate 
11960Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
11990Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
12000Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
12010Sstevel@tonic-gate 		dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
12025753Sgww 		    tohp->level, tohp->name, tohp->len));
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
12050Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
12060Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
12070Sstevel@tonic-gate 			continue;
12080Sstevel@tonic-gate 		}
12090Sstevel@tonic-gate 		ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
12100Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
12110Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
12120Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
12130Sstevel@tonic-gate 
12140Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
12150Sstevel@tonic-gate 				return (EPROTO);
12160Sstevel@tonic-gate 			if (oldflg) {
12170Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf, control,
12180Sstevel@tonic-gate 				    (int)controllen);
12190Sstevel@tonic-gate 				if (error != 0)
12200Sstevel@tonic-gate 					return (error);
12210Sstevel@tonic-gate 				continue;
12220Sstevel@tonic-gate 			} else {
12230Sstevel@tonic-gate 				int fdlen;
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate 				fdlen = (int)fdbuf_cmsglen(
12260Sstevel@tonic-gate 				    (int)_TPI_TOPT_DATALEN(tohp));
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 				cmsg->cmsg_level = tohp->level;
12290Sstevel@tonic-gate 				cmsg->cmsg_type = SCM_RIGHTS;
12300Sstevel@tonic-gate 				cmsg->cmsg_len = (socklen_t)(fdlen +
12315753Sgww 				    sizeof (struct cmsghdr));
12320Sstevel@tonic-gate 
12330Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf,
12345753Sgww 				    CMSG_CONTENT(cmsg), fdlen);
12350Sstevel@tonic-gate 				if (error != 0)
12360Sstevel@tonic-gate 					return (error);
12370Sstevel@tonic-gate 			}
12381673Sgt145670 		} else if (tohp->level == SOL_SOCKET &&
12391673Sgt145670 		    tohp->name == SCM_TIMESTAMP) {
12401673Sgt145670 			timestruc_t *timestamp;
12411673Sgt145670 
12421673Sgt145670 			if (oldflg)
12431673Sgt145670 				continue;
12441673Sgt145670 
12451673Sgt145670 			cmsg->cmsg_level = tohp->level;
12461673Sgt145670 			cmsg->cmsg_type = tohp->name;
12471673Sgt145670 
12481673Sgt145670 			timestamp =
12491673Sgt145670 			    (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
12501673Sgt145670 			    sizeof (intptr_t));
12511673Sgt145670 
12521673Sgt145670 			if (get_udatamodel() == DATAMODEL_NATIVE) {
12532280Sgt145670 				struct timeval tv;
12541673Sgt145670 
12551673Sgt145670 				cmsg->cmsg_len = sizeof (struct timeval) +
12561673Sgt145670 				    sizeof (struct cmsghdr);
12572280Sgt145670 				tv.tv_sec = timestamp->tv_sec;
12582280Sgt145670 				tv.tv_usec = timestamp->tv_nsec /
12592280Sgt145670 				    (NANOSEC / MICROSEC);
12602280Sgt145670 				/*
12612280Sgt145670 				 * on LP64 systems, the struct timeval in
12622280Sgt145670 				 * the destination will not be 8-byte aligned,
12632280Sgt145670 				 * so use bcopy to avoid alignment trouble
12642280Sgt145670 				 */
12652280Sgt145670 				bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
12661673Sgt145670 			} else {
12671673Sgt145670 				struct timeval32 *time32;
12681673Sgt145670 
12691673Sgt145670 				cmsg->cmsg_len = sizeof (struct timeval32) +
12701673Sgt145670 				    sizeof (struct cmsghdr);
12711673Sgt145670 				time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
12721673Sgt145670 				time32->tv_sec = (time32_t)timestamp->tv_sec;
12731673Sgt145670 				time32->tv_usec =
12741673Sgt145670 				    (int32_t)(timestamp->tv_nsec /
12751673Sgt145670 				    (NANOSEC / MICROSEC));
12761673Sgt145670 			}
12771673Sgt145670 
12780Sstevel@tonic-gate 		} else {
12790Sstevel@tonic-gate 			if (oldflg)
12800Sstevel@tonic-gate 				continue;
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate 			cmsg->cmsg_level = tohp->level;
12830Sstevel@tonic-gate 			cmsg->cmsg_type = tohp->name;
12840Sstevel@tonic-gate 			cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) +
12850Sstevel@tonic-gate 			    sizeof (struct cmsghdr));
12860Sstevel@tonic-gate 
12870Sstevel@tonic-gate 			/* copy content to control data part */
12880Sstevel@tonic-gate 			bcopy(&tohp[1], CMSG_CONTENT(cmsg),
12895753Sgww 			    CMSG_CONTENTLEN(cmsg));
12900Sstevel@tonic-gate 		}
12910Sstevel@tonic-gate 		/* move to next CMSG structure! */
12920Sstevel@tonic-gate 		cmsg = CMSG_NEXT(cmsg);
12930Sstevel@tonic-gate 	}
12942280Sgt145670 	dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
12957240Srh87107 	    control, controllen, (void *)cend, (void *)cmsg));
12962280Sgt145670 	ASSERT(cmsg <= cend);
12970Sstevel@tonic-gate 	return (0);
12980Sstevel@tonic-gate }
12990Sstevel@tonic-gate 
13000Sstevel@tonic-gate /*
13010Sstevel@tonic-gate  * Extract the SO_SRCADDR option value if present.
13020Sstevel@tonic-gate  */
13030Sstevel@tonic-gate void
13040Sstevel@tonic-gate so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
13050Sstevel@tonic-gate     t_uscalar_t *srclenp)
13060Sstevel@tonic-gate {
13070Sstevel@tonic-gate 	struct T_opthdr		*tohp;
13080Sstevel@tonic-gate 
13090Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
13100Sstevel@tonic-gate 
13110Sstevel@tonic-gate 	ASSERT(srcp != NULL && srclenp != NULL);
13120Sstevel@tonic-gate 	*srcp = NULL;
13130Sstevel@tonic-gate 	*srclenp = 0;
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
13160Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
13170Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
13180Sstevel@tonic-gate 		dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
13195753Sgww 		    tohp->level, tohp->name, tohp->len));
13200Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
13210Sstevel@tonic-gate 		    tohp->name == SO_SRCADDR) {
13220Sstevel@tonic-gate 			*srcp = _TPI_TOPT_DATA(tohp);
13230Sstevel@tonic-gate 			*srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
13240Sstevel@tonic-gate 		}
13250Sstevel@tonic-gate 	}
13260Sstevel@tonic-gate }
13270Sstevel@tonic-gate 
13280Sstevel@tonic-gate /*
13290Sstevel@tonic-gate  * Verify if the SO_UNIX_CLOSE option is present.
13300Sstevel@tonic-gate  */
13310Sstevel@tonic-gate int
13320Sstevel@tonic-gate so_getopt_unix_close(void *opt, t_uscalar_t optlen)
13330Sstevel@tonic-gate {
13340Sstevel@tonic-gate 	struct T_opthdr		*tohp;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
13370Sstevel@tonic-gate 
13380Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
13390Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
13400Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
13410Sstevel@tonic-gate 		dprint(1,
13425753Sgww 		    ("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
13435753Sgww 		    tohp->level, tohp->name, tohp->len));
13440Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
13450Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)
13460Sstevel@tonic-gate 			return (1);
13470Sstevel@tonic-gate 	}
13480Sstevel@tonic-gate 	return (0);
13490Sstevel@tonic-gate }
13500Sstevel@tonic-gate 
13510Sstevel@tonic-gate /*
13520Sstevel@tonic-gate  * Allocate an M_PROTO message.
13530Sstevel@tonic-gate  *
13540Sstevel@tonic-gate  * If allocation fails the behavior depends on sleepflg:
13550Sstevel@tonic-gate  *	_ALLOC_NOSLEEP	fail immediately
13560Sstevel@tonic-gate  *	_ALLOC_INTR	sleep for memory until a signal is caught
13570Sstevel@tonic-gate  *	_ALLOC_SLEEP	sleep forever. Don't return NULL.
13580Sstevel@tonic-gate  */
13590Sstevel@tonic-gate mblk_t *
1360*8778SErik.Nordmark@Sun.COM soallocproto(size_t size, int sleepflg, cred_t *cr)
13610Sstevel@tonic-gate {
13620Sstevel@tonic-gate 	mblk_t	*mp;
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate 	/* Round up size for reuse */
13650Sstevel@tonic-gate 	size = MAX(size, 64);
1366*8778SErik.Nordmark@Sun.COM 	if (cr != NULL)
1367*8778SErik.Nordmark@Sun.COM 		mp = allocb_cred(size, cr, curproc->p_pid);
1368*8778SErik.Nordmark@Sun.COM 	else
1369*8778SErik.Nordmark@Sun.COM 		mp = allocb(size, BPRI_MED);
1370*8778SErik.Nordmark@Sun.COM 
13710Sstevel@tonic-gate 	if (mp == NULL) {
13720Sstevel@tonic-gate 		int error;	/* Dummy - error not returned to caller */
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 		switch (sleepflg) {
13750Sstevel@tonic-gate 		case _ALLOC_SLEEP:
1376*8778SErik.Nordmark@Sun.COM 			if (cr != NULL) {
1377*8778SErik.Nordmark@Sun.COM 				mp = allocb_cred_wait(size, STR_NOSIG, &error,
1378*8778SErik.Nordmark@Sun.COM 				    cr, curproc->p_pid);
1379*8778SErik.Nordmark@Sun.COM 			} else {
1380*8778SErik.Nordmark@Sun.COM 				mp = allocb_wait(size, BPRI_MED, STR_NOSIG,
1381*8778SErik.Nordmark@Sun.COM 				    &error);
1382*8778SErik.Nordmark@Sun.COM 			}
13830Sstevel@tonic-gate 			ASSERT(mp);
13840Sstevel@tonic-gate 			break;
13850Sstevel@tonic-gate 		case _ALLOC_INTR:
1386*8778SErik.Nordmark@Sun.COM 			if (cr != NULL) {
1387*8778SErik.Nordmark@Sun.COM 				mp = allocb_cred_wait(size, 0, &error, cr,
1388*8778SErik.Nordmark@Sun.COM 				    curproc->p_pid);
1389*8778SErik.Nordmark@Sun.COM 			} else {
1390*8778SErik.Nordmark@Sun.COM 				mp = allocb_wait(size, BPRI_MED, 0, &error);
1391*8778SErik.Nordmark@Sun.COM 			}
13920Sstevel@tonic-gate 			if (mp == NULL) {
13930Sstevel@tonic-gate 				/* Caught signal while sleeping for memory */
13940Sstevel@tonic-gate 				eprintline(ENOBUFS);
13950Sstevel@tonic-gate 				return (NULL);
13960Sstevel@tonic-gate 			}
13970Sstevel@tonic-gate 			break;
13980Sstevel@tonic-gate 		case _ALLOC_NOSLEEP:
13990Sstevel@tonic-gate 		default:
14000Sstevel@tonic-gate 			eprintline(ENOBUFS);
14010Sstevel@tonic-gate 			return (NULL);
14020Sstevel@tonic-gate 		}
14030Sstevel@tonic-gate 	}
14040Sstevel@tonic-gate 	DB_TYPE(mp) = M_PROTO;
14050Sstevel@tonic-gate 	return (mp);
14060Sstevel@tonic-gate }
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate /*
14090Sstevel@tonic-gate  * Allocate an M_PROTO message with a single component.
14100Sstevel@tonic-gate  * len is the length of buf. size is the amount to allocate.
14110Sstevel@tonic-gate  *
14120Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
14130Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
14140Sstevel@tonic-gate  */
14150Sstevel@tonic-gate mblk_t *
1416*8778SErik.Nordmark@Sun.COM soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg,
1417*8778SErik.Nordmark@Sun.COM     cred_t *cr)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate 	mblk_t	*mp;
14200Sstevel@tonic-gate 
14210Sstevel@tonic-gate 	if (size == 0)
14220Sstevel@tonic-gate 		size = len;
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	ASSERT(size >= len);
14250Sstevel@tonic-gate 	/* Round up size for reuse */
14260Sstevel@tonic-gate 	size = MAX(size, 64);
1427*8778SErik.Nordmark@Sun.COM 	mp = soallocproto(size, sleepflg, cr);
14280Sstevel@tonic-gate 	if (mp == NULL)
14290Sstevel@tonic-gate 		return (NULL);
14300Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
14310Sstevel@tonic-gate 	if (len != 0) {
14320Sstevel@tonic-gate 		if (buf != NULL)
14330Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
14340Sstevel@tonic-gate 		else
14350Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
14360Sstevel@tonic-gate 		mp->b_wptr += len;
14370Sstevel@tonic-gate 	}
14380Sstevel@tonic-gate 	return (mp);
14390Sstevel@tonic-gate }
14400Sstevel@tonic-gate 
14410Sstevel@tonic-gate /*
14420Sstevel@tonic-gate  * Append buf/len to mp.
14430Sstevel@tonic-gate  * The caller has to ensure that there is enough room in the mblk.
14440Sstevel@tonic-gate  *
14450Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
14460Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
14470Sstevel@tonic-gate  */
14480Sstevel@tonic-gate void
14490Sstevel@tonic-gate soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
14500Sstevel@tonic-gate {
14510Sstevel@tonic-gate 	ASSERT(mp);
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	if (len != 0) {
14540Sstevel@tonic-gate 		/* Assert for room left */
14550Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
14560Sstevel@tonic-gate 		if (buf != NULL)
14570Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
14580Sstevel@tonic-gate 		else
14590Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
14600Sstevel@tonic-gate 	}
14610Sstevel@tonic-gate 	mp->b_wptr += len;
14620Sstevel@tonic-gate }
14630Sstevel@tonic-gate 
14640Sstevel@tonic-gate /*
14650Sstevel@tonic-gate  * Create a message using two kernel buffers.
14660Sstevel@tonic-gate  * If size is set that will determine the allocation size (e.g. for future
14670Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
14680Sstevel@tonic-gate  * lengths.
14690Sstevel@tonic-gate  */
14700Sstevel@tonic-gate mblk_t *
14710Sstevel@tonic-gate soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1472*8778SErik.Nordmark@Sun.COM     ssize_t size, int sleepflg, cred_t *cr)
14730Sstevel@tonic-gate {
14740Sstevel@tonic-gate 	mblk_t *mp;
14750Sstevel@tonic-gate 
14760Sstevel@tonic-gate 	if (size == 0)
14770Sstevel@tonic-gate 		size = len1 + len2;
14780Sstevel@tonic-gate 	ASSERT(size >= len1 + len2);
14790Sstevel@tonic-gate 
1480*8778SErik.Nordmark@Sun.COM 	mp = soallocproto1(buf1, len1, size, sleepflg, cr);
14810Sstevel@tonic-gate 	if (mp)
14820Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
14830Sstevel@tonic-gate 	return (mp);
14840Sstevel@tonic-gate }
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate /*
14870Sstevel@tonic-gate  * Create a message using three kernel buffers.
14880Sstevel@tonic-gate  * If size is set that will determine the allocation size (for future
14890Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
14900Sstevel@tonic-gate  * lengths.
14910Sstevel@tonic-gate  */
14920Sstevel@tonic-gate mblk_t *
14930Sstevel@tonic-gate soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
1494*8778SErik.Nordmark@Sun.COM     const void *buf3, ssize_t len3, ssize_t size, int sleepflg, cred_t *cr)
14950Sstevel@tonic-gate {
14960Sstevel@tonic-gate 	mblk_t *mp;
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate 	if (size == 0)
14990Sstevel@tonic-gate 		size = len1 + len2 +len3;
15000Sstevel@tonic-gate 	ASSERT(size >= len1 + len2 + len3);
15010Sstevel@tonic-gate 
1502*8778SErik.Nordmark@Sun.COM 	mp = soallocproto1(buf1, len1, size, sleepflg, cr);
15030Sstevel@tonic-gate 	if (mp != NULL) {
15040Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
15050Sstevel@tonic-gate 		soappendmsg(mp, buf3, len3);
15060Sstevel@tonic-gate 	}
15070Sstevel@tonic-gate 	return (mp);
15080Sstevel@tonic-gate }
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate #ifdef DEBUG
15110Sstevel@tonic-gate char *
15120Sstevel@tonic-gate pr_state(uint_t state, uint_t mode)
15130Sstevel@tonic-gate {
15140Sstevel@tonic-gate 	static char buf[1024];
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	buf[0] = 0;
15170Sstevel@tonic-gate 	if (state & SS_ISCONNECTED)
15187240Srh87107 		(void) strcat(buf, "ISCONNECTED ");
15190Sstevel@tonic-gate 	if (state & SS_ISCONNECTING)
15207240Srh87107 		(void) strcat(buf, "ISCONNECTING ");
15210Sstevel@tonic-gate 	if (state & SS_ISDISCONNECTING)
15227240Srh87107 		(void) strcat(buf, "ISDISCONNECTING ");
15230Sstevel@tonic-gate 	if (state & SS_CANTSENDMORE)
15247240Srh87107 		(void) strcat(buf, "CANTSENDMORE ");
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	if (state & SS_CANTRCVMORE)
15277240Srh87107 		(void) strcat(buf, "CANTRCVMORE ");
15280Sstevel@tonic-gate 	if (state & SS_ISBOUND)
15297240Srh87107 		(void) strcat(buf, "ISBOUND ");
15300Sstevel@tonic-gate 	if (state & SS_NDELAY)
15317240Srh87107 		(void) strcat(buf, "NDELAY ");
15320Sstevel@tonic-gate 	if (state & SS_NONBLOCK)
15337240Srh87107 		(void) strcat(buf, "NONBLOCK ");
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate 	if (state & SS_ASYNC)
15367240Srh87107 		(void) strcat(buf, "ASYNC ");
15370Sstevel@tonic-gate 	if (state & SS_ACCEPTCONN)
15387240Srh87107 		(void) strcat(buf, "ACCEPTCONN ");
15390Sstevel@tonic-gate 	if (state & SS_SAVEDEOR)
15407240Srh87107 		(void) strcat(buf, "SAVEDEOR ");
15410Sstevel@tonic-gate 
15420Sstevel@tonic-gate 	if (state & SS_RCVATMARK)
15437240Srh87107 		(void) strcat(buf, "RCVATMARK ");
15440Sstevel@tonic-gate 	if (state & SS_OOBPEND)
15457240Srh87107 		(void) strcat(buf, "OOBPEND ");
15460Sstevel@tonic-gate 	if (state & SS_HAVEOOBDATA)
15477240Srh87107 		(void) strcat(buf, "HAVEOOBDATA ");
15480Sstevel@tonic-gate 	if (state & SS_HADOOBDATA)
15497240Srh87107 		(void) strcat(buf, "HADOOBDATA ");
15500Sstevel@tonic-gate 
15510Sstevel@tonic-gate 	if (mode & SM_PRIV)
15527240Srh87107 		(void) strcat(buf, "PRIV ");
15530Sstevel@tonic-gate 	if (mode & SM_ATOMIC)
15547240Srh87107 		(void) strcat(buf, "ATOMIC ");
15550Sstevel@tonic-gate 	if (mode & SM_ADDR)
15567240Srh87107 		(void) strcat(buf, "ADDR ");
15570Sstevel@tonic-gate 	if (mode & SM_CONNREQUIRED)
15587240Srh87107 		(void) strcat(buf, "CONNREQUIRED ");
15590Sstevel@tonic-gate 
15600Sstevel@tonic-gate 	if (mode & SM_FDPASSING)
15617240Srh87107 		(void) strcat(buf, "FDPASSING ");
15620Sstevel@tonic-gate 	if (mode & SM_EXDATA)
15637240Srh87107 		(void) strcat(buf, "EXDATA ");
15640Sstevel@tonic-gate 	if (mode & SM_OPTDATA)
15657240Srh87107 		(void) strcat(buf, "OPTDATA ");
15660Sstevel@tonic-gate 	if (mode & SM_BYTESTREAM)
15677240Srh87107 		(void) strcat(buf, "BYTESTREAM ");
15680Sstevel@tonic-gate 	return (buf);
15690Sstevel@tonic-gate }
15700Sstevel@tonic-gate 
15710Sstevel@tonic-gate char *
15720Sstevel@tonic-gate pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
15730Sstevel@tonic-gate {
15740Sstevel@tonic-gate 	static char buf[1024];
15750Sstevel@tonic-gate 
15760Sstevel@tonic-gate 	if (addr == NULL || addrlen == 0) {
15777240Srh87107 		(void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr);
15780Sstevel@tonic-gate 		return (buf);
15790Sstevel@tonic-gate 	}
15800Sstevel@tonic-gate 	switch (family) {
15810Sstevel@tonic-gate 	case AF_INET: {
15820Sstevel@tonic-gate 		struct sockaddr_in sin;
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 		bcopy(addr, &sin, sizeof (sin));
15850Sstevel@tonic-gate 
15860Sstevel@tonic-gate 		(void) sprintf(buf, "(len %d) %x/%d",
15876712Stomee 		    addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
15880Sstevel@tonic-gate 		break;
15890Sstevel@tonic-gate 	}
15900Sstevel@tonic-gate 	case AF_INET6: {
15910Sstevel@tonic-gate 		struct sockaddr_in6 sin6;
15920Sstevel@tonic-gate 		uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 		bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
15957240Srh87107 		(void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
15960Sstevel@tonic-gate 		    addrlen,
15970Sstevel@tonic-gate 		    ntohs(piece[0]), ntohs(piece[1]),
15980Sstevel@tonic-gate 		    ntohs(piece[2]), ntohs(piece[3]),
15990Sstevel@tonic-gate 		    ntohs(piece[4]), ntohs(piece[5]),
16000Sstevel@tonic-gate 		    ntohs(piece[6]), ntohs(piece[7]),
16010Sstevel@tonic-gate 		    ntohs(sin6.sin6_port));
16020Sstevel@tonic-gate 		break;
16030Sstevel@tonic-gate 	}
16040Sstevel@tonic-gate 	case AF_UNIX: {
16050Sstevel@tonic-gate 		struct sockaddr_un *soun = (struct sockaddr_un *)addr;
16060Sstevel@tonic-gate 
16076712Stomee 		(void) sprintf(buf, "(len %d) %s", addrlen,
16085753Sgww 		    (soun == NULL) ? "(none)" : soun->sun_path);
16090Sstevel@tonic-gate 		break;
16100Sstevel@tonic-gate 	}
16110Sstevel@tonic-gate 	default:
16120Sstevel@tonic-gate 		(void) sprintf(buf, "(unknown af %d)", family);
16130Sstevel@tonic-gate 		break;
16140Sstevel@tonic-gate 	}
16150Sstevel@tonic-gate 	return (buf);
16160Sstevel@tonic-gate }
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate /* The logical equivalence operator (a if-and-only-if b) */
16190Sstevel@tonic-gate #define	EQUIV(a, b)	(((a) && (b)) || (!(a) && (!(b))))
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate /*
16220Sstevel@tonic-gate  * Verify limitations and invariants on oob state.
16230Sstevel@tonic-gate  * Return 1 if OK, otherwise 0 so that it can be used as
16240Sstevel@tonic-gate  *	ASSERT(verify_oobstate(so));
16250Sstevel@tonic-gate  */
16260Sstevel@tonic-gate int
16270Sstevel@tonic-gate so_verify_oobstate(struct sonode *so)
16280Sstevel@tonic-gate {
16298348SEric.Yu@Sun.COM 	boolean_t havemark;
16308348SEric.Yu@Sun.COM 
16310Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	/*
16340Sstevel@tonic-gate 	 * The possible state combinations are:
16350Sstevel@tonic-gate 	 *	0
16360Sstevel@tonic-gate 	 *	SS_OOBPEND
16370Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HAVEOOBDATA
16380Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HADOOBDATA
16390Sstevel@tonic-gate 	 *	SS_HADOOBDATA
16400Sstevel@tonic-gate 	 */
16410Sstevel@tonic-gate 	switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
16420Sstevel@tonic-gate 	case 0:
16430Sstevel@tonic-gate 	case SS_OOBPEND:
16440Sstevel@tonic-gate 	case SS_OOBPEND|SS_HAVEOOBDATA:
16450Sstevel@tonic-gate 	case SS_OOBPEND|SS_HADOOBDATA:
16460Sstevel@tonic-gate 	case SS_HADOOBDATA:
16470Sstevel@tonic-gate 		break;
16480Sstevel@tonic-gate 	default:
16498348SEric.Yu@Sun.COM 		printf("Bad oob state 1 (%p): state %s\n",
16508348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16510Sstevel@tonic-gate 		return (0);
16520Sstevel@tonic-gate 	}
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate 	/* SS_RCVATMARK should only be set when SS_OOBPEND is set */
16550Sstevel@tonic-gate 	if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
16568348SEric.Yu@Sun.COM 		printf("Bad oob state 2 (%p): state %s\n",
16578348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16580Sstevel@tonic-gate 		return (0);
16590Sstevel@tonic-gate 	}
16600Sstevel@tonic-gate 
16610Sstevel@tonic-gate 	/*
16628348SEric.Yu@Sun.COM 	 * (havemark != 0 or SS_RCVATMARK) iff SS_OOBPEND
16638348SEric.Yu@Sun.COM 	 * For TPI, the presence of a "mark" is indicated by sti_oobsigcnt.
16640Sstevel@tonic-gate 	 */
16658348SEric.Yu@Sun.COM 	havemark = (SOCK_IS_NONSTR(so)) ? so->so_oobmark > 0 :
16668348SEric.Yu@Sun.COM 	    SOTOTPI(so)->sti_oobsigcnt > 0;
16678348SEric.Yu@Sun.COM 
16688348SEric.Yu@Sun.COM 	if (!EQUIV(havemark || (so->so_state & SS_RCVATMARK),
16695753Sgww 	    so->so_state & SS_OOBPEND)) {
16708348SEric.Yu@Sun.COM 		printf("Bad oob state 3 (%p): state %s\n",
16718348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16720Sstevel@tonic-gate 		return (0);
16730Sstevel@tonic-gate 	}
16740Sstevel@tonic-gate 
16750Sstevel@tonic-gate 	/*
16760Sstevel@tonic-gate 	 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
16770Sstevel@tonic-gate 	 */
16780Sstevel@tonic-gate 	if (!(so->so_options & SO_OOBINLINE) &&
16790Sstevel@tonic-gate 	    !EQUIV(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
16808348SEric.Yu@Sun.COM 		printf("Bad oob state 4 (%p): state %s\n",
16818348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16820Sstevel@tonic-gate 		return (0);
16830Sstevel@tonic-gate 	}
16848348SEric.Yu@Sun.COM 
16858348SEric.Yu@Sun.COM 	if (!SOCK_IS_NONSTR(so) &&
16868348SEric.Yu@Sun.COM 	    SOTOTPI(so)->sti_oobsigcnt < SOTOTPI(so)->sti_oobcnt) {
16870Sstevel@tonic-gate 		printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
16888348SEric.Yu@Sun.COM 		    (void *)so, SOTOTPI(so)->sti_oobsigcnt,
16898348SEric.Yu@Sun.COM 		    SOTOTPI(so)->sti_oobcnt,
16908348SEric.Yu@Sun.COM 		    pr_state(so->so_state, so->so_mode));
16910Sstevel@tonic-gate 		return (0);
16920Sstevel@tonic-gate 	}
16938348SEric.Yu@Sun.COM 
16940Sstevel@tonic-gate 	return (1);
16950Sstevel@tonic-gate }
16960Sstevel@tonic-gate #undef	EQUIV
16970Sstevel@tonic-gate #endif /* DEBUG */
16980Sstevel@tonic-gate 
16990Sstevel@tonic-gate /* initialize sockfs zone specific kstat related items			*/
17000Sstevel@tonic-gate void *
17010Sstevel@tonic-gate sock_kstat_init(zoneid_t zoneid)
17020Sstevel@tonic-gate {
17030Sstevel@tonic-gate 	kstat_t	*ksp;
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
17060Sstevel@tonic-gate 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	if (ksp != NULL) {
17090Sstevel@tonic-gate 		ksp->ks_update = sockfs_update;
17100Sstevel@tonic-gate 		ksp->ks_snapshot = sockfs_snapshot;
17110Sstevel@tonic-gate 		ksp->ks_lock = &socklist.sl_lock;
17120Sstevel@tonic-gate 		ksp->ks_private = (void *)(uintptr_t)zoneid;
17130Sstevel@tonic-gate 		kstat_install(ksp);
17140Sstevel@tonic-gate 	}
17150Sstevel@tonic-gate 
17160Sstevel@tonic-gate 	return (ksp);
17170Sstevel@tonic-gate }
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate /* tear down sockfs zone specific kstat related items			*/
17200Sstevel@tonic-gate /*ARGSUSED*/
17210Sstevel@tonic-gate void
17220Sstevel@tonic-gate sock_kstat_fini(zoneid_t zoneid, void *arg)
17230Sstevel@tonic-gate {
17240Sstevel@tonic-gate 	kstat_t *ksp = (kstat_t *)arg;
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate 	if (ksp != NULL) {
17270Sstevel@tonic-gate 		ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
17280Sstevel@tonic-gate 		kstat_delete(ksp);
17290Sstevel@tonic-gate 	}
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate /*
17330Sstevel@tonic-gate  * Zones:
17340Sstevel@tonic-gate  * Note that nactive is going to be different for each zone.
17350Sstevel@tonic-gate  * This means we require kstat to call sockfs_update and then sockfs_snapshot
17360Sstevel@tonic-gate  * for the same zone, or sockfs_snapshot will be taken into the wrong size
17370Sstevel@tonic-gate  * buffer. This is safe, but if the buffer is too small, user will not be
17380Sstevel@tonic-gate  * given details of all sockets. However, as this kstat has a ks_lock, kstat
17390Sstevel@tonic-gate  * driver will keep it locked between the update and the snapshot, so no
17400Sstevel@tonic-gate  * other process (zone) can currently get inbetween resulting in a wrong size
17410Sstevel@tonic-gate  * buffer allocation.
17420Sstevel@tonic-gate  */
17430Sstevel@tonic-gate static int
17440Sstevel@tonic-gate sockfs_update(kstat_t *ksp, int rw)
17450Sstevel@tonic-gate {
17460Sstevel@tonic-gate 	uint_t	nactive = 0;		/* # of active AF_UNIX sockets	*/
17470Sstevel@tonic-gate 	struct sonode	*so;		/* current sonode on socklist	*/
17480Sstevel@tonic-gate 	zoneid_t	myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
17490Sstevel@tonic-gate 
17500Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
17530Sstevel@tonic-gate 		return (EACCES);
17540Sstevel@tonic-gate 	}
17550Sstevel@tonic-gate 
17568348SEric.Yu@Sun.COM 	for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
17578348SEric.Yu@Sun.COM 		if (so->so_count != 0 && so->so_zoneid == myzoneid) {
17580Sstevel@tonic-gate 			nactive++;
17590Sstevel@tonic-gate 		}
17600Sstevel@tonic-gate 	}
17610Sstevel@tonic-gate 	ksp->ks_ndata = nactive;
17620Sstevel@tonic-gate 	ksp->ks_data_size = nactive * sizeof (struct k_sockinfo);
17630Sstevel@tonic-gate 
17640Sstevel@tonic-gate 	return (0);
17650Sstevel@tonic-gate }
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate static int
17680Sstevel@tonic-gate sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
17690Sstevel@tonic-gate {
17700Sstevel@tonic-gate 	int			ns;	/* # of sonodes we've copied	*/
17710Sstevel@tonic-gate 	struct sonode		*so;	/* current sonode on socklist	*/
17720Sstevel@tonic-gate 	struct k_sockinfo	*pksi;	/* where we put sockinfo data	*/
17730Sstevel@tonic-gate 	t_uscalar_t		sn_len;	/* soa_len			*/
17740Sstevel@tonic-gate 	zoneid_t		myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
17758348SEric.Yu@Sun.COM 	sotpi_info_t 		*sti;
17760Sstevel@tonic-gate 
17770Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
17780Sstevel@tonic-gate 
17790Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
17800Sstevel@tonic-gate 
17810Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
17820Sstevel@tonic-gate 		return (EACCES);
17830Sstevel@tonic-gate 	}
17840Sstevel@tonic-gate 
17850Sstevel@tonic-gate 	/*
17860Sstevel@tonic-gate 	 * for each sonode on the socklist, we massage the important
17870Sstevel@tonic-gate 	 * info into buf, in k_sockinfo format.
17880Sstevel@tonic-gate 	 */
17890Sstevel@tonic-gate 	pksi = (struct k_sockinfo *)buf;
17908348SEric.Yu@Sun.COM 	ns = 0;
17918348SEric.Yu@Sun.COM 	for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
17920Sstevel@tonic-gate 		/* only stuff active sonodes and the same zone:		*/
17938348SEric.Yu@Sun.COM 		if (so->so_count == 0 || so->so_zoneid != myzoneid) {
17940Sstevel@tonic-gate 			continue;
17950Sstevel@tonic-gate 		}
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 		/*
17980Sstevel@tonic-gate 		 * If the sonode was activated between the update and the
17990Sstevel@tonic-gate 		 * snapshot, we're done - as this is only a snapshot.
18000Sstevel@tonic-gate 		 */
18010Sstevel@tonic-gate 		if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) {
18020Sstevel@tonic-gate 			break;
18030Sstevel@tonic-gate 		}
18040Sstevel@tonic-gate 
18058348SEric.Yu@Sun.COM 		sti = SOTOTPI(so);
18060Sstevel@tonic-gate 		/* copy important info into buf:			*/
18070Sstevel@tonic-gate 		pksi->ks_si.si_size = sizeof (struct k_sockinfo);
18080Sstevel@tonic-gate 		pksi->ks_si.si_family = so->so_family;
18090Sstevel@tonic-gate 		pksi->ks_si.si_type = so->so_type;
18100Sstevel@tonic-gate 		pksi->ks_si.si_flag = so->so_flag;
18110Sstevel@tonic-gate 		pksi->ks_si.si_state = so->so_state;
18128348SEric.Yu@Sun.COM 		pksi->ks_si.si_serv_type = sti->sti_serv_type;
18138348SEric.Yu@Sun.COM 		pksi->ks_si.si_ux_laddr_sou_magic =
18148348SEric.Yu@Sun.COM 		    sti->sti_ux_laddr.soua_magic;
18158348SEric.Yu@Sun.COM 		pksi->ks_si.si_ux_faddr_sou_magic =
18168348SEric.Yu@Sun.COM 		    sti->sti_ux_faddr.soua_magic;
18178348SEric.Yu@Sun.COM 		pksi->ks_si.si_laddr_soa_len = sti->sti_laddr.soa_len;
18188348SEric.Yu@Sun.COM 		pksi->ks_si.si_faddr_soa_len = sti->sti_faddr.soa_len;
18190Sstevel@tonic-gate 		pksi->ks_si.si_szoneid = so->so_zoneid;
18208348SEric.Yu@Sun.COM 		pksi->ks_si.si_faddr_noxlate = sti->sti_faddr_noxlate;
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
18230Sstevel@tonic-gate 
18248348SEric.Yu@Sun.COM 		if (sti->sti_laddr_sa != NULL) {
18258348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_sa->sa_data != NULL);
18268348SEric.Yu@Sun.COM 			sn_len = sti->sti_laddr_len;
18270Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
18280Sstevel@tonic-gate 			    sizeof (pksi->ks_si.si_laddr_sun_path));
18290Sstevel@tonic-gate 
18300Sstevel@tonic-gate 			pksi->ks_si.si_laddr_family =
18318348SEric.Yu@Sun.COM 			    sti->sti_laddr_sa->sa_family;
18320Sstevel@tonic-gate 			if (sn_len != 0) {
18330Sstevel@tonic-gate 				/* AF_UNIX socket names are NULL terminated */
18340Sstevel@tonic-gate 				(void) strncpy(pksi->ks_si.si_laddr_sun_path,
18358348SEric.Yu@Sun.COM 				    sti->sti_laddr_sa->sa_data,
18360Sstevel@tonic-gate 				    sizeof (pksi->ks_si.si_laddr_sun_path));
18370Sstevel@tonic-gate 				sn_len = strlen(pksi->ks_si.si_laddr_sun_path);
18380Sstevel@tonic-gate 			}
18390Sstevel@tonic-gate 			pksi->ks_si.si_laddr_sun_path[sn_len] = 0;
18400Sstevel@tonic-gate 		}
18410Sstevel@tonic-gate 
18428348SEric.Yu@Sun.COM 		if (sti->sti_faddr_sa != NULL) {
18438348SEric.Yu@Sun.COM 			ASSERT(sti->sti_faddr_sa->sa_data != NULL);
18448348SEric.Yu@Sun.COM 			sn_len = sti->sti_faddr_len;
18450Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
18460Sstevel@tonic-gate 			    sizeof (pksi->ks_si.si_faddr_sun_path));
18470Sstevel@tonic-gate 
18480Sstevel@tonic-gate 			pksi->ks_si.si_faddr_family =
18498348SEric.Yu@Sun.COM 			    sti->sti_faddr_sa->sa_family;
18500Sstevel@tonic-gate 			if (sn_len != 0) {
18510Sstevel@tonic-gate 				(void) strncpy(pksi->ks_si.si_faddr_sun_path,
18528348SEric.Yu@Sun.COM 				    sti->sti_faddr_sa->sa_data,
18530Sstevel@tonic-gate 				    sizeof (pksi->ks_si.si_faddr_sun_path));
18540Sstevel@tonic-gate 				sn_len = strlen(pksi->ks_si.si_faddr_sun_path);
18550Sstevel@tonic-gate 			}
18560Sstevel@tonic-gate 			pksi->ks_si.si_faddr_sun_path[sn_len] = 0;
18570Sstevel@tonic-gate 		}
18580Sstevel@tonic-gate 
18590Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
18600Sstevel@tonic-gate 
18610Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[0], "%p", (void *)so);
18620Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[1], "%p",
18638348SEric.Yu@Sun.COM 		    (void *)sti->sti_ux_laddr.soua_vp);
18640Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[2], "%p",
18658348SEric.Yu@Sun.COM 		    (void *)sti->sti_ux_faddr.soua_vp);
18660Sstevel@tonic-gate 
18670Sstevel@tonic-gate 		ns++;
18680Sstevel@tonic-gate 		pksi++;
18690Sstevel@tonic-gate 	}
18700Sstevel@tonic-gate 
18710Sstevel@tonic-gate 	ksp->ks_ndata = ns;
18720Sstevel@tonic-gate 	return (0);
18730Sstevel@tonic-gate }
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate ssize_t
18760Sstevel@tonic-gate soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
18770Sstevel@tonic-gate {
18780Sstevel@tonic-gate 	struct uio auio;
18790Sstevel@tonic-gate 	struct iovec aiov[MSG_MAXIOVLEN];
18800Sstevel@tonic-gate 	register vnode_t *vp;
18810Sstevel@tonic-gate 	int ioflag, rwflag;
18820Sstevel@tonic-gate 	ssize_t cnt;
18830Sstevel@tonic-gate 	int error = 0;
18840Sstevel@tonic-gate 	int iovcnt = 0;
18850Sstevel@tonic-gate 	short fflag;
18860Sstevel@tonic-gate 
18870Sstevel@tonic-gate 	vp = fp->f_vnode;
18880Sstevel@tonic-gate 	fflag = fp->f_flag;
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	rwflag = 0;
18910Sstevel@tonic-gate 	aiov[0].iov_base = (caddr_t)buf;
18920Sstevel@tonic-gate 	aiov[0].iov_len = size;
18930Sstevel@tonic-gate 	iovcnt = 1;
18940Sstevel@tonic-gate 	cnt = (ssize_t)size;
18950Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
18960Sstevel@tonic-gate 
18970Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
18980Sstevel@tonic-gate 	auio.uio_iov = aiov;
18990Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
19000Sstevel@tonic-gate 	auio.uio_resid = cnt;
19010Sstevel@tonic-gate 	auio.uio_segflg = UIO_SYSSPACE;
19020Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
19030Sstevel@tonic-gate 	auio.uio_fmode = fflag;
19040Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
19050Sstevel@tonic-gate 
19060Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
19070Sstevel@tonic-gate 
19080Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
19090Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
19100Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
19110Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
19120Sstevel@tonic-gate 	cnt -= auio.uio_resid;
19130Sstevel@tonic-gate 
19140Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
19150Sstevel@tonic-gate 
19160Sstevel@tonic-gate 	if (error == EINTR && cnt != 0)
19170Sstevel@tonic-gate 		error = 0;
19180Sstevel@tonic-gate out:
19190Sstevel@tonic-gate 	if (error != 0) {
19200Sstevel@tonic-gate 		*err = error;
19210Sstevel@tonic-gate 		return (0);
19220Sstevel@tonic-gate 	} else {
19230Sstevel@tonic-gate 		*err = 0;
19240Sstevel@tonic-gate 		return (cnt);
19250Sstevel@tonic-gate 	}
19260Sstevel@tonic-gate }
19278348SEric.Yu@Sun.COM 
19288348SEric.Yu@Sun.COM int
19298348SEric.Yu@Sun.COM so_copyin(const void *from, void *to, size_t size, int fromkernel)
19308348SEric.Yu@Sun.COM {
19318348SEric.Yu@Sun.COM 	if (fromkernel) {
19328348SEric.Yu@Sun.COM 		bcopy(from, to, size);
19338348SEric.Yu@Sun.COM 		return (0);
19348348SEric.Yu@Sun.COM 	}
19358348SEric.Yu@Sun.COM 	return (xcopyin(from, to, size));
19368348SEric.Yu@Sun.COM }
19378348SEric.Yu@Sun.COM 
19388348SEric.Yu@Sun.COM int
19398348SEric.Yu@Sun.COM so_copyout(const void *from, void *to, size_t size, int tokernel)
19408348SEric.Yu@Sun.COM {
19418348SEric.Yu@Sun.COM 	if (tokernel) {
19428348SEric.Yu@Sun.COM 		bcopy(from, to, size);
19438348SEric.Yu@Sun.COM 		return (0);
19448348SEric.Yu@Sun.COM 	}
19458348SEric.Yu@Sun.COM 	return (xcopyout(from, to, size));
19468348SEric.Yu@Sun.COM }
1947