xref: /onnv-gate/usr/src/uts/common/fs/sockfs/socksubr.c (revision 12643:044ff822d212)
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*12643SAnders.Persson@Sun.COM  * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/t_lock.h>
280Sstevel@tonic-gate #include <sys/param.h>
290Sstevel@tonic-gate #include <sys/systm.h>
300Sstevel@tonic-gate #include <sys/buf.h>
310Sstevel@tonic-gate #include <sys/conf.h>
320Sstevel@tonic-gate #include <sys/cred.h>
330Sstevel@tonic-gate #include <sys/kmem.h>
340Sstevel@tonic-gate #include <sys/sysmacros.h>
350Sstevel@tonic-gate #include <sys/vfs.h>
363898Srsb #include <sys/vfs_opreg.h>
370Sstevel@tonic-gate #include <sys/vnode.h>
380Sstevel@tonic-gate #include <sys/debug.h>
390Sstevel@tonic-gate #include <sys/errno.h>
400Sstevel@tonic-gate #include <sys/time.h>
410Sstevel@tonic-gate #include <sys/file.h>
420Sstevel@tonic-gate #include <sys/open.h>
430Sstevel@tonic-gate #include <sys/user.h>
440Sstevel@tonic-gate #include <sys/termios.h>
450Sstevel@tonic-gate #include <sys/stream.h>
460Sstevel@tonic-gate #include <sys/strsubr.h>
470Sstevel@tonic-gate #include <sys/strsun.h>
480Sstevel@tonic-gate #include <sys/esunddi.h>
490Sstevel@tonic-gate #include <sys/flock.h>
500Sstevel@tonic-gate #include <sys/modctl.h>
510Sstevel@tonic-gate #include <sys/cmn_err.h>
520Sstevel@tonic-gate #include <sys/mkdev.h>
530Sstevel@tonic-gate #include <sys/pathname.h>
540Sstevel@tonic-gate #include <sys/ddi.h>
550Sstevel@tonic-gate #include <sys/stat.h>
560Sstevel@tonic-gate #include <sys/fs/snode.h>
570Sstevel@tonic-gate #include <sys/fs/dv_node.h>
580Sstevel@tonic-gate #include <sys/zone.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate #include <sys/socket.h>
610Sstevel@tonic-gate #include <sys/socketvar.h>
620Sstevel@tonic-gate #include <netinet/in.h>
630Sstevel@tonic-gate #include <sys/un.h>
640Sstevel@tonic-gate #include <sys/ucred.h>
650Sstevel@tonic-gate 
660Sstevel@tonic-gate #include <sys/tiuser.h>
670Sstevel@tonic-gate #define	_SUN_TPI_VERSION	2
680Sstevel@tonic-gate #include <sys/tihdr.h>
690Sstevel@tonic-gate 
700Sstevel@tonic-gate #include <c2/audit.h>
710Sstevel@tonic-gate 
720Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
738348SEric.Yu@Sun.COM #include <fs/sockfs/sockcommon.h>
74*12643SAnders.Persson@Sun.COM #include <fs/sockfs/sockfilter_impl.h>
758348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi.h>
768348SEric.Yu@Sun.COM #include <fs/sockfs/socktpi_impl.h>
779491SAnders.Persson@Sun.COM #include <fs/sockfs/sodirect.h>
780Sstevel@tonic-gate 
790Sstevel@tonic-gate /*
800Sstevel@tonic-gate  * Macros that operate on struct cmsghdr.
810Sstevel@tonic-gate  * The CMSG_VALID macro does not assume that the last option buffer is padded.
820Sstevel@tonic-gate  */
830Sstevel@tonic-gate #define	CMSG_CONTENT(cmsg)	(&((cmsg)[1]))
840Sstevel@tonic-gate #define	CMSG_CONTENTLEN(cmsg)	((cmsg)->cmsg_len - sizeof (struct cmsghdr))
850Sstevel@tonic-gate #define	CMSG_VALID(cmsg, start, end)					\
860Sstevel@tonic-gate 	(ISALIGNED_cmsghdr(cmsg) &&					\
870Sstevel@tonic-gate 	((uintptr_t)(cmsg) >= (uintptr_t)(start)) &&			\
880Sstevel@tonic-gate 	((uintptr_t)(cmsg) < (uintptr_t)(end)) &&			\
890Sstevel@tonic-gate 	((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) &&	\
900Sstevel@tonic-gate 	((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end)))
910Sstevel@tonic-gate #define	SO_LOCK_WAKEUP_TIME	3000	/* Wakeup time in milliseconds */
920Sstevel@tonic-gate 
930Sstevel@tonic-gate dev_t sockdev;	/* For fsid in getattr */
948194SJack.Meng@Sun.COM int sockfs_defer_nl7c_init = 0;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate struct socklist socklist;
970Sstevel@tonic-gate 
988348SEric.Yu@Sun.COM struct kmem_cache *socket_cache;
998348SEric.Yu@Sun.COM 
100*12643SAnders.Persson@Sun.COM /*
101*12643SAnders.Persson@Sun.COM  * sockconf_lock protects the socket configuration (socket types and
102*12643SAnders.Persson@Sun.COM  * socket filters) which is changed via the sockconfig system call.
103*12643SAnders.Persson@Sun.COM  */
104*12643SAnders.Persson@Sun.COM krwlock_t sockconf_lock;
105*12643SAnders.Persson@Sun.COM 
1060Sstevel@tonic-gate static int sockfs_update(kstat_t *, int);
1070Sstevel@tonic-gate static int sockfs_snapshot(kstat_t *, void *, int);
1088348SEric.Yu@Sun.COM extern smod_info_t *sotpi_smod_create(void);
1090Sstevel@tonic-gate 
1100Sstevel@tonic-gate extern void sendfile_init();
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate extern void nl7c_init(void);
1130Sstevel@tonic-gate 
1148194SJack.Meng@Sun.COM extern int modrootloaded;
1158194SJack.Meng@Sun.COM 
1160Sstevel@tonic-gate #define	ADRSTRLEN (2 * sizeof (void *) + 1)
1170Sstevel@tonic-gate /*
1180Sstevel@tonic-gate  * kernel structure for passing the sockinfo data back up to the user.
1190Sstevel@tonic-gate  * the strings array allows us to convert AF_UNIX addresses into strings
1200Sstevel@tonic-gate  * with a common method regardless of which n-bit kernel we're running.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate struct k_sockinfo {
1230Sstevel@tonic-gate 	struct sockinfo	ks_si;
1240Sstevel@tonic-gate 	char		ks_straddr[3][ADRSTRLEN];
1250Sstevel@tonic-gate };
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate /*
1280Sstevel@tonic-gate  * Translate from a device pathname (e.g. "/dev/tcp") to a vnode.
1290Sstevel@tonic-gate  * Returns with the vnode held.
1300Sstevel@tonic-gate  */
1318348SEric.Yu@Sun.COM int
sogetvp(char * devpath,vnode_t ** vpp,int uioflag)1320Sstevel@tonic-gate sogetvp(char *devpath, vnode_t **vpp, int uioflag)
1330Sstevel@tonic-gate {
1340Sstevel@tonic-gate 	struct snode *csp;
1350Sstevel@tonic-gate 	vnode_t *vp, *dvp;
1360Sstevel@tonic-gate 	major_t maj;
1370Sstevel@tonic-gate 	int error;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE);
1408348SEric.Yu@Sun.COM 
1410Sstevel@tonic-gate 	/*
1420Sstevel@tonic-gate 	 * Lookup the underlying filesystem vnode.
1430Sstevel@tonic-gate 	 */
1440Sstevel@tonic-gate 	error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp);
1450Sstevel@tonic-gate 	if (error)
1460Sstevel@tonic-gate 		return (error);
1470Sstevel@tonic-gate 
1480Sstevel@tonic-gate 	/* Check that it is the correct vnode */
1490Sstevel@tonic-gate 	if (vp->v_type != VCHR) {
1500Sstevel@tonic-gate 		VN_RELE(vp);
1510Sstevel@tonic-gate 		return (ENOTSOCK);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 	/*
1550Sstevel@tonic-gate 	 * If devpath went through devfs, the device should already
1560Sstevel@tonic-gate 	 * be configured. If devpath is a mknod file, however, we
1570Sstevel@tonic-gate 	 * need to make sure the device is properly configured.
1580Sstevel@tonic-gate 	 * To do this, we do something similar to spec_open()
1590Sstevel@tonic-gate 	 * except that we resolve to the minor/leaf level since
1600Sstevel@tonic-gate 	 * we need to return a vnode.
1610Sstevel@tonic-gate 	 */
1620Sstevel@tonic-gate 	csp = VTOS(VTOS(vp)->s_commonvp);
1630Sstevel@tonic-gate 	if (!(csp->s_flag & SDIPSET)) {
1640Sstevel@tonic-gate 		char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1650Sstevel@tonic-gate 		error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname);
1660Sstevel@tonic-gate 		if (error == 0)
1670Sstevel@tonic-gate 			error = devfs_lookupname(pathname, NULLVPP, &dvp);
1680Sstevel@tonic-gate 		VN_RELE(vp);
1690Sstevel@tonic-gate 		kmem_free(pathname, MAXPATHLEN);
1700Sstevel@tonic-gate 		if (error != 0)
1710Sstevel@tonic-gate 			return (ENXIO);
1720Sstevel@tonic-gate 		vp = dvp;	/* use the devfs vp */
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/* device is configured at this point */
1760Sstevel@tonic-gate 	maj = getmajor(vp->v_rdev);
1770Sstevel@tonic-gate 	if (!STREAMSTAB(maj)) {
1780Sstevel@tonic-gate 		VN_RELE(vp);
1790Sstevel@tonic-gate 		return (ENOSTR);
1800Sstevel@tonic-gate 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	*vpp = vp;
1830Sstevel@tonic-gate 	return (0);
1840Sstevel@tonic-gate }
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate /*
1870Sstevel@tonic-gate  * Update the accessed, updated, or changed times in an sonode
1880Sstevel@tonic-gate  * with the current time.
1890Sstevel@tonic-gate  *
1900Sstevel@tonic-gate  * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable
1910Sstevel@tonic-gate  * attributes in a fstat call. (They return the current time and 0 for
1920Sstevel@tonic-gate  * all timestamps, respectively.) We maintain the current timestamps
1930Sstevel@tonic-gate  * here primarily so that should sockmod be popped the resulting
1940Sstevel@tonic-gate  * file descriptor will behave like a stream w.r.t. the timestamps.
1950Sstevel@tonic-gate  */
1960Sstevel@tonic-gate void
so_update_attrs(struct sonode * so,int flag)1970Sstevel@tonic-gate so_update_attrs(struct sonode *so, int flag)
1980Sstevel@tonic-gate {
1990Sstevel@tonic-gate 	time_t now = gethrestime_sec();
2000Sstevel@tonic-gate 
2018348SEric.Yu@Sun.COM 	if (SOCK_IS_NONSTR(so))
2028348SEric.Yu@Sun.COM 		return;
2038348SEric.Yu@Sun.COM 
2040Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
2050Sstevel@tonic-gate 	so->so_flag |= flag;
2060Sstevel@tonic-gate 	if (flag & SOACC)
2078348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_atime = now;
2080Sstevel@tonic-gate 	if (flag & SOMOD)
2098348SEric.Yu@Sun.COM 		SOTOTPI(so)->sti_mtime = now;
2100Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
2110Sstevel@tonic-gate }
2120Sstevel@tonic-gate 
2138348SEric.Yu@Sun.COM extern so_create_func_t sock_comm_create_function;
2148348SEric.Yu@Sun.COM extern so_destroy_func_t sock_comm_destroy_function;
2150Sstevel@tonic-gate /*
2160Sstevel@tonic-gate  * Init function called when sockfs is loaded.
2170Sstevel@tonic-gate  */
2180Sstevel@tonic-gate int
sockinit(int fstype,char * name)2190Sstevel@tonic-gate sockinit(int fstype, char *name)
2200Sstevel@tonic-gate {
2210Sstevel@tonic-gate 	static const fs_operation_def_t sock_vfsops_template[] = {
2220Sstevel@tonic-gate 		NULL, NULL
2230Sstevel@tonic-gate 	};
2240Sstevel@tonic-gate 	int error;
2250Sstevel@tonic-gate 	major_t dev;
2260Sstevel@tonic-gate 	char *err_str;
2270Sstevel@tonic-gate 
2280Sstevel@tonic-gate 	error = vfs_setfsops(fstype, sock_vfsops_template, NULL);
2290Sstevel@tonic-gate 	if (error != 0) {
2301548Srshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN,
2311548Srshoaib 		    "sockinit: bad vfs ops template");
2320Sstevel@tonic-gate 		return (error);
2330Sstevel@tonic-gate 	}
2340Sstevel@tonic-gate 
2358348SEric.Yu@Sun.COM 	error = vn_make_ops(name, socket_vnodeops_template,
2368348SEric.Yu@Sun.COM 	    &socket_vnodeops);
2370Sstevel@tonic-gate 	if (error != 0) {
2388348SEric.Yu@Sun.COM 		err_str = "sockinit: bad socket vnode ops template";
2390Sstevel@tonic-gate 		/* vn_make_ops() does not reset socktpi_vnodeops on failure. */
2408348SEric.Yu@Sun.COM 		socket_vnodeops = NULL;
2410Sstevel@tonic-gate 		goto failure;
2420Sstevel@tonic-gate 	}
2430Sstevel@tonic-gate 
2448348SEric.Yu@Sun.COM 	socket_cache = kmem_cache_create("socket_cache",
2458348SEric.Yu@Sun.COM 	    sizeof (struct sonode), 0, sonode_constructor,
2468348SEric.Yu@Sun.COM 	    sonode_destructor, NULL, NULL, NULL, 0);
2470Sstevel@tonic-gate 
248*12643SAnders.Persson@Sun.COM 	rw_init(&sockconf_lock, NULL, RW_DEFAULT, NULL);
249*12643SAnders.Persson@Sun.COM 
2508348SEric.Yu@Sun.COM 	error = socktpi_init();
2513422Snh145002 	if (error != 0) {
2523422Snh145002 		err_str = NULL;
2533422Snh145002 		goto failure;
2543422Snh145002 	}
2553422Snh145002 
2569491SAnders.Persson@Sun.COM 	error = sod_init();
2576707Sbrutus 	if (error != 0) {
2586707Sbrutus 		err_str = NULL;
2596707Sbrutus 		goto failure;
2606707Sbrutus 	}
2616707Sbrutus 
2620Sstevel@tonic-gate 	/*
2638348SEric.Yu@Sun.COM 	 * Set up the default create and destroy functions
2640Sstevel@tonic-gate 	 */
2658348SEric.Yu@Sun.COM 	sock_comm_create_function = socket_sonode_create;
2668348SEric.Yu@Sun.COM 	sock_comm_destroy_function = socket_sonode_destroy;
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	/*
2690Sstevel@tonic-gate 	 * Build initial list mapping socket parameters to vnode.
2700Sstevel@tonic-gate 	 */
2718348SEric.Yu@Sun.COM 	smod_init();
2728348SEric.Yu@Sun.COM 	smod_add(sotpi_smod_create());
2738348SEric.Yu@Sun.COM 
2748348SEric.Yu@Sun.COM 	sockparams_init();
2750Sstevel@tonic-gate 
2760Sstevel@tonic-gate 	/*
2770Sstevel@tonic-gate 	 * If sockets are needed before init runs /sbin/soconfig
2780Sstevel@tonic-gate 	 * it is possible to preload the sockparams list here using
2790Sstevel@tonic-gate 	 * calls like:
2800Sstevel@tonic-gate 	 *	sockconfig(1,2,3, "/dev/tcp", 0);
2810Sstevel@tonic-gate 	 */
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	/*
2840Sstevel@tonic-gate 	 * Create a unique dev_t for use in so_fsid.
2850Sstevel@tonic-gate 	 */
2860Sstevel@tonic-gate 
2870Sstevel@tonic-gate 	if ((dev = getudev()) == (major_t)-1)
2880Sstevel@tonic-gate 		dev = 0;
2890Sstevel@tonic-gate 	sockdev = makedevice(dev, 0);
2900Sstevel@tonic-gate 
2910Sstevel@tonic-gate 	mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL);
2920Sstevel@tonic-gate 	sendfile_init();
2938194SJack.Meng@Sun.COM 	if (!modrootloaded) {
2948194SJack.Meng@Sun.COM 		sockfs_defer_nl7c_init = 1;
2958194SJack.Meng@Sun.COM 	} else {
2968194SJack.Meng@Sun.COM 		nl7c_init();
2978194SJack.Meng@Sun.COM 	}
2980Sstevel@tonic-gate 
299*12643SAnders.Persson@Sun.COM 	/* Initialize socket filters */
300*12643SAnders.Persson@Sun.COM 	sof_init();
301*12643SAnders.Persson@Sun.COM 
3020Sstevel@tonic-gate 	return (0);
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate failure:
3050Sstevel@tonic-gate 	(void) vfs_freevfsops_by_type(fstype);
3068348SEric.Yu@Sun.COM 	if (socket_vnodeops != NULL)
3078348SEric.Yu@Sun.COM 		vn_freevnodeops(socket_vnodeops);
3080Sstevel@tonic-gate 	if (err_str != NULL)
3091548Srshoaib 		zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str);
3100Sstevel@tonic-gate 	return (error);
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOLOCKED.
3150Sstevel@tonic-gate  */
3160Sstevel@tonic-gate void
so_lock_single(struct sonode * so)3170Sstevel@tonic-gate so_lock_single(struct sonode *so)
3180Sstevel@tonic-gate {
3190Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 	while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) {
32211521SAnders.Persson@Sun.COM 		cv_wait_stop(&so->so_single_cv, &so->so_lock,
3235753Sgww 		    SO_LOCK_WAKEUP_TIME);
3240Sstevel@tonic-gate 	}
3250Sstevel@tonic-gate 	so->so_flag |= SOLOCKED;
3260Sstevel@tonic-gate }
3270Sstevel@tonic-gate 
3280Sstevel@tonic-gate /*
3290Sstevel@tonic-gate  * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND.
3300Sstevel@tonic-gate  * Used to clear SOLOCKED or SOASYNC_UNBIND.
3310Sstevel@tonic-gate  */
3320Sstevel@tonic-gate void
so_unlock_single(struct sonode * so,int flag)3330Sstevel@tonic-gate so_unlock_single(struct sonode *so, int flag)
3340Sstevel@tonic-gate {
3350Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3360Sstevel@tonic-gate 	ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND));
3370Sstevel@tonic-gate 	ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0);
3380Sstevel@tonic-gate 	ASSERT(so->so_flag & flag);
3390Sstevel@tonic-gate 	/*
3408348SEric.Yu@Sun.COM 	 * Process the T_DISCON_IND on sti_discon_ind_mp.
3410Sstevel@tonic-gate 	 *
3420Sstevel@tonic-gate 	 * Call to so_drain_discon_ind will result in so_lock
3430Sstevel@tonic-gate 	 * being dropped and re-acquired later.
3440Sstevel@tonic-gate 	 */
3458348SEric.Yu@Sun.COM 	if (!SOCK_IS_NONSTR(so)) {
3468348SEric.Yu@Sun.COM 		sotpi_info_t *sti = SOTOTPI(so);
3478348SEric.Yu@Sun.COM 
3488348SEric.Yu@Sun.COM 		if (sti->sti_discon_ind_mp != NULL)
3498348SEric.Yu@Sun.COM 			so_drain_discon_ind(so);
3508348SEric.Yu@Sun.COM 	}
3510Sstevel@tonic-gate 
35211521SAnders.Persson@Sun.COM 	cv_signal(&so->so_single_cv);
35311521SAnders.Persson@Sun.COM 	so->so_flag &= ~flag;
3540Sstevel@tonic-gate }
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate /*
3570Sstevel@tonic-gate  * Caller must hold the mutex. Used to set SOREADLOCKED.
3580Sstevel@tonic-gate  * If the caller wants nonblocking behavior it should set fmode.
3590Sstevel@tonic-gate  */
3600Sstevel@tonic-gate int
so_lock_read(struct sonode * so,int fmode)3610Sstevel@tonic-gate so_lock_read(struct sonode *so, int fmode)
3620Sstevel@tonic-gate {
3630Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3640Sstevel@tonic-gate 
3650Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
3660Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
3670Sstevel@tonic-gate 			return (EWOULDBLOCK);
36811521SAnders.Persson@Sun.COM 		cv_wait_stop(&so->so_read_cv, &so->so_lock,
3695753Sgww 		    SO_LOCK_WAKEUP_TIME);
3700Sstevel@tonic-gate 	}
3710Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
3720Sstevel@tonic-gate 	return (0);
3730Sstevel@tonic-gate }
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate /*
3760Sstevel@tonic-gate  * Like so_lock_read above but allows signals.
3770Sstevel@tonic-gate  */
3780Sstevel@tonic-gate int
so_lock_read_intr(struct sonode * so,int fmode)3790Sstevel@tonic-gate so_lock_read_intr(struct sonode *so, int fmode)
3800Sstevel@tonic-gate {
3810Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	while (so->so_flag & SOREADLOCKED) {
3840Sstevel@tonic-gate 		if (fmode & (FNDELAY|FNONBLOCK))
3850Sstevel@tonic-gate 			return (EWOULDBLOCK);
38611521SAnders.Persson@Sun.COM 		if (!cv_wait_sig(&so->so_read_cv, &so->so_lock))
3870Sstevel@tonic-gate 			return (EINTR);
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate 	so->so_flag |= SOREADLOCKED;
3900Sstevel@tonic-gate 	return (0);
3910Sstevel@tonic-gate }
3920Sstevel@tonic-gate 
3930Sstevel@tonic-gate /*
3940Sstevel@tonic-gate  * Caller must hold the mutex. Used to clear SOREADLOCKED,
3950Sstevel@tonic-gate  * set in so_lock_read() or so_lock_read_intr().
3960Sstevel@tonic-gate  */
3970Sstevel@tonic-gate void
so_unlock_read(struct sonode * so)3980Sstevel@tonic-gate so_unlock_read(struct sonode *so)
3990Sstevel@tonic-gate {
4000Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
4010Sstevel@tonic-gate 	ASSERT(so->so_flag & SOREADLOCKED);
4020Sstevel@tonic-gate 
40311521SAnders.Persson@Sun.COM 	cv_signal(&so->so_read_cv);
40411521SAnders.Persson@Sun.COM 	so->so_flag &= ~SOREADLOCKED;
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate 
4070Sstevel@tonic-gate /*
4080Sstevel@tonic-gate  * Verify that the specified offset falls within the mblk and
4090Sstevel@tonic-gate  * that the resulting pointer is aligned.
4100Sstevel@tonic-gate  * Returns NULL if not.
4110Sstevel@tonic-gate  */
4120Sstevel@tonic-gate void *
sogetoff(mblk_t * mp,t_uscalar_t offset,t_uscalar_t length,uint_t align_size)4130Sstevel@tonic-gate sogetoff(mblk_t *mp, t_uscalar_t offset,
4140Sstevel@tonic-gate     t_uscalar_t length, uint_t align_size)
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	uintptr_t ptr1, ptr2;
4170Sstevel@tonic-gate 
4180Sstevel@tonic-gate 	ASSERT(mp && mp->b_wptr >= mp->b_rptr);
4190Sstevel@tonic-gate 	ptr1 = (uintptr_t)mp->b_rptr + offset;
4200Sstevel@tonic-gate 	ptr2 = (uintptr_t)ptr1 + length;
4210Sstevel@tonic-gate 	if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) {
4220Sstevel@tonic-gate 		eprintline(0);
4230Sstevel@tonic-gate 		return (NULL);
4240Sstevel@tonic-gate 	}
4250Sstevel@tonic-gate 	if ((ptr1 & (align_size - 1)) != 0) {
4260Sstevel@tonic-gate 		eprintline(0);
4270Sstevel@tonic-gate 		return (NULL);
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 	return ((void *)ptr1);
4300Sstevel@tonic-gate }
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate /*
4330Sstevel@tonic-gate  * Return the AF_UNIX underlying filesystem vnode matching a given name.
4340Sstevel@tonic-gate  * Makes sure the sending and the destination sonodes are compatible.
4350Sstevel@tonic-gate  * The vnode is returned held.
4360Sstevel@tonic-gate  *
4370Sstevel@tonic-gate  * The underlying filesystem VSOCK vnode has a v_stream pointer that
4380Sstevel@tonic-gate  * references the actual stream head (hence indirectly the actual sonode).
4390Sstevel@tonic-gate  */
4400Sstevel@tonic-gate static int
so_ux_lookup(struct sonode * so,struct sockaddr_un * soun,int checkaccess,vnode_t ** vpp)4410Sstevel@tonic-gate so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess,
4420Sstevel@tonic-gate 		vnode_t **vpp)
4430Sstevel@tonic-gate {
4440Sstevel@tonic-gate 	vnode_t		*vp;	/* Underlying filesystem vnode */
4457409SRic.Aleshire@Sun.COM 	vnode_t		*rvp;	/* real vnode */
4460Sstevel@tonic-gate 	vnode_t		*svp;	/* sockfs vnode */
4470Sstevel@tonic-gate 	struct sonode	*so2;
4480Sstevel@tonic-gate 	int		error;
4490Sstevel@tonic-gate 
4507242Srh87107 	dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", (void *)so,
4517242Srh87107 	    soun->sun_path));
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate 	error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp);
4540Sstevel@tonic-gate 	if (error) {
4550Sstevel@tonic-gate 		eprintsoline(so, error);
4560Sstevel@tonic-gate 		return (error);
4570Sstevel@tonic-gate 	}
4587409SRic.Aleshire@Sun.COM 
4597409SRic.Aleshire@Sun.COM 	/*
4607409SRic.Aleshire@Sun.COM 	 * Traverse lofs mounts get the real vnode
4617409SRic.Aleshire@Sun.COM 	 */
4627409SRic.Aleshire@Sun.COM 	if (VOP_REALVP(vp, &rvp, NULL) == 0) {
4637409SRic.Aleshire@Sun.COM 		VN_HOLD(rvp);		/* hold the real vnode */
4647409SRic.Aleshire@Sun.COM 		VN_RELE(vp);		/* release hold from lookup */
4657409SRic.Aleshire@Sun.COM 		vp = rvp;
4667409SRic.Aleshire@Sun.COM 	}
4677409SRic.Aleshire@Sun.COM 
4680Sstevel@tonic-gate 	if (vp->v_type != VSOCK) {
4690Sstevel@tonic-gate 		error = ENOTSOCK;
4700Sstevel@tonic-gate 		eprintsoline(so, error);
4710Sstevel@tonic-gate 		goto done2;
4720Sstevel@tonic-gate 	}
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if (checkaccess) {
4750Sstevel@tonic-gate 		/*
4760Sstevel@tonic-gate 		 * Check that we have permissions to access the destination
4770Sstevel@tonic-gate 		 * vnode. This check is not done in BSD but it is required
4780Sstevel@tonic-gate 		 * by X/Open.
4790Sstevel@tonic-gate 		 */
4805331Samw 		if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL)) {
4810Sstevel@tonic-gate 			eprintsoline(so, error);
4820Sstevel@tonic-gate 			goto done2;
4830Sstevel@tonic-gate 		}
4840Sstevel@tonic-gate 	}
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/*
4870Sstevel@tonic-gate 	 * Check if the remote socket has been closed.
4880Sstevel@tonic-gate 	 *
4890Sstevel@tonic-gate 	 * Synchronize with vn_rele_stream by holding v_lock while traversing
4900Sstevel@tonic-gate 	 * v_stream->sd_vnode.
4910Sstevel@tonic-gate 	 */
4920Sstevel@tonic-gate 	mutex_enter(&vp->v_lock);
4930Sstevel@tonic-gate 	if (vp->v_stream == NULL) {
4940Sstevel@tonic-gate 		mutex_exit(&vp->v_lock);
4950Sstevel@tonic-gate 		if (so->so_type == SOCK_DGRAM)
4960Sstevel@tonic-gate 			error = EDESTADDRREQ;
4970Sstevel@tonic-gate 		else
4980Sstevel@tonic-gate 			error = ECONNREFUSED;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 		eprintsoline(so, error);
5010Sstevel@tonic-gate 		goto done2;
5020Sstevel@tonic-gate 	}
5030Sstevel@tonic-gate 	ASSERT(vp->v_stream->sd_vnode);
5040Sstevel@tonic-gate 	svp = vp->v_stream->sd_vnode;
5050Sstevel@tonic-gate 	/*
5060Sstevel@tonic-gate 	 * holding v_lock on underlying filesystem vnode and acquiring
5070Sstevel@tonic-gate 	 * it on sockfs vnode. Assumes that no code ever attempts to
5080Sstevel@tonic-gate 	 * acquire these locks in the reverse order.
5090Sstevel@tonic-gate 	 */
5100Sstevel@tonic-gate 	VN_HOLD(svp);
5110Sstevel@tonic-gate 	mutex_exit(&vp->v_lock);
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	if (svp->v_type != VSOCK) {
5140Sstevel@tonic-gate 		error = ENOTSOCK;
5150Sstevel@tonic-gate 		eprintsoline(so, error);
5160Sstevel@tonic-gate 		goto done;
5170Sstevel@tonic-gate 	}
5180Sstevel@tonic-gate 
5190Sstevel@tonic-gate 	so2 = VTOSO(svp);
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 	if (so->so_type != so2->so_type) {
5220Sstevel@tonic-gate 		error = EPROTOTYPE;
5230Sstevel@tonic-gate 		eprintsoline(so, error);
5240Sstevel@tonic-gate 		goto done;
5250Sstevel@tonic-gate 	}
5260Sstevel@tonic-gate 
5270Sstevel@tonic-gate 	VN_RELE(svp);
5280Sstevel@tonic-gate 	*vpp = vp;
5290Sstevel@tonic-gate 	return (0);
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate done:
5320Sstevel@tonic-gate 	VN_RELE(svp);
5330Sstevel@tonic-gate done2:
5340Sstevel@tonic-gate 	VN_RELE(vp);
5350Sstevel@tonic-gate 	return (error);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * Verify peer address for connect and sendto/sendmsg.
5400Sstevel@tonic-gate  * Since sendto/sendmsg would not get synchronous errors from the transport
5410Sstevel@tonic-gate  * provider we have to do these ugly checks in the socket layer to
5420Sstevel@tonic-gate  * preserve compatibility with SunOS 4.X.
5430Sstevel@tonic-gate  */
5440Sstevel@tonic-gate int
so_addr_verify(struct sonode * so,const struct sockaddr * name,socklen_t namelen)5450Sstevel@tonic-gate so_addr_verify(struct sonode *so, const struct sockaddr *name,
5460Sstevel@tonic-gate     socklen_t namelen)
5470Sstevel@tonic-gate {
5480Sstevel@tonic-gate 	int		family;
5490Sstevel@tonic-gate 
5507240Srh87107 	dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n",
5517240Srh87107 	    (void *)so, (void *)name, namelen));
5520Sstevel@tonic-gate 
5530Sstevel@tonic-gate 	ASSERT(name != NULL);
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	family = so->so_family;
5560Sstevel@tonic-gate 	switch (family) {
5570Sstevel@tonic-gate 	case AF_INET:
5580Sstevel@tonic-gate 		if (name->sa_family != family) {
5590Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5600Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5610Sstevel@tonic-gate 		}
5620Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
5630Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
5640Sstevel@tonic-gate 			return (EINVAL);
5650Sstevel@tonic-gate 		}
5660Sstevel@tonic-gate 		break;
5670Sstevel@tonic-gate 	case AF_INET6: {
5680Sstevel@tonic-gate #ifdef DEBUG
5690Sstevel@tonic-gate 		struct sockaddr_in6 *sin6;
5700Sstevel@tonic-gate #endif /* DEBUG */
5710Sstevel@tonic-gate 
5720Sstevel@tonic-gate 		if (name->sa_family != family) {
5730Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
5740Sstevel@tonic-gate 			return (EAFNOSUPPORT);
5750Sstevel@tonic-gate 		}
5760Sstevel@tonic-gate 		if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
5770Sstevel@tonic-gate 			eprintsoline(so, EINVAL);
5780Sstevel@tonic-gate 			return (EINVAL);
5790Sstevel@tonic-gate 		}
5800Sstevel@tonic-gate #ifdef DEBUG
5810Sstevel@tonic-gate 		/* Verify that apps don't forget to clear sin6_scope_id etc */
5820Sstevel@tonic-gate 		sin6 = (struct sockaddr_in6 *)name;
5830Sstevel@tonic-gate 		if (sin6->sin6_scope_id != 0 &&
5840Sstevel@tonic-gate 		    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
5851548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
5860Sstevel@tonic-gate 			    "connect/send* with uninitialized sin6_scope_id "
5870Sstevel@tonic-gate 			    "(%d) on socket. Pid = %d\n",
5880Sstevel@tonic-gate 			    (int)sin6->sin6_scope_id, (int)curproc->p_pid);
5890Sstevel@tonic-gate 		}
5900Sstevel@tonic-gate #endif /* DEBUG */
5910Sstevel@tonic-gate 		break;
5920Sstevel@tonic-gate 	}
5930Sstevel@tonic-gate 	case AF_UNIX:
5948348SEric.Yu@Sun.COM 		if (SOTOTPI(so)->sti_faddr_noxlate) {
5950Sstevel@tonic-gate 			return (0);
5960Sstevel@tonic-gate 		}
5970Sstevel@tonic-gate 		if (namelen < (socklen_t)sizeof (short)) {
5980Sstevel@tonic-gate 			eprintsoline(so, ENOENT);
5990Sstevel@tonic-gate 			return (ENOENT);
6000Sstevel@tonic-gate 		}
6010Sstevel@tonic-gate 		if (name->sa_family != family) {
6020Sstevel@tonic-gate 			eprintsoline(so, EAFNOSUPPORT);
6030Sstevel@tonic-gate 			return (EAFNOSUPPORT);
6040Sstevel@tonic-gate 		}
6050Sstevel@tonic-gate 		/* MAXPATHLEN + soun_family + nul termination */
6060Sstevel@tonic-gate 		if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) {
6070Sstevel@tonic-gate 			eprintsoline(so, ENAMETOOLONG);
6080Sstevel@tonic-gate 			return (ENAMETOOLONG);
6090Sstevel@tonic-gate 		}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 		break;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	default:
6140Sstevel@tonic-gate 		/*
6150Sstevel@tonic-gate 		 * Default is don't do any length or sa_family check
6160Sstevel@tonic-gate 		 * to allow non-sockaddr style addresses.
6170Sstevel@tonic-gate 		 */
6180Sstevel@tonic-gate 		break;
6190Sstevel@tonic-gate 	}
6200Sstevel@tonic-gate 
6210Sstevel@tonic-gate 	return (0);
6220Sstevel@tonic-gate }
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate  * Translate an AF_UNIX sockaddr_un to the transport internal name.
6270Sstevel@tonic-gate  * Assumes caller has called so_addr_verify first.
6280Sstevel@tonic-gate  */
6290Sstevel@tonic-gate /*ARGSUSED*/
6300Sstevel@tonic-gate int
so_ux_addr_xlate(struct sonode * so,struct sockaddr * name,socklen_t namelen,int checkaccess,void ** addrp,socklen_t * addrlenp)6310Sstevel@tonic-gate so_ux_addr_xlate(struct sonode *so, struct sockaddr *name,
6320Sstevel@tonic-gate     socklen_t namelen, int checkaccess,
6330Sstevel@tonic-gate     void **addrp, socklen_t *addrlenp)
6340Sstevel@tonic-gate {
6350Sstevel@tonic-gate 	int			error;
6360Sstevel@tonic-gate 	struct sockaddr_un	*soun;
6370Sstevel@tonic-gate 	vnode_t			*vp;
6380Sstevel@tonic-gate 	void			*addr;
6390Sstevel@tonic-gate 	socklen_t		addrlen;
6408348SEric.Yu@Sun.COM 	sotpi_info_t		*sti = SOTOTPI(so);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n",
6437240Srh87107 	    (void *)so, (void *)name, namelen, checkaccess));
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	ASSERT(name != NULL);
6460Sstevel@tonic-gate 	ASSERT(so->so_family == AF_UNIX);
6478348SEric.Yu@Sun.COM 	ASSERT(!sti->sti_faddr_noxlate);
6480Sstevel@tonic-gate 	ASSERT(namelen >= (socklen_t)sizeof (short));
6490Sstevel@tonic-gate 	ASSERT(name->sa_family == AF_UNIX);
6500Sstevel@tonic-gate 	soun = (struct sockaddr_un *)name;
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * Lookup vnode for the specified path name and verify that
6530Sstevel@tonic-gate 	 * it is a socket.
6540Sstevel@tonic-gate 	 */
6550Sstevel@tonic-gate 	error = so_ux_lookup(so, soun, checkaccess, &vp);
6560Sstevel@tonic-gate 	if (error) {
6570Sstevel@tonic-gate 		eprintsoline(so, error);
6580Sstevel@tonic-gate 		return (error);
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 	/*
6610Sstevel@tonic-gate 	 * Use the address of the peer vnode as the address to send
6620Sstevel@tonic-gate 	 * to. We release the peer vnode here. In case it has been
6630Sstevel@tonic-gate 	 * closed by the time the T_CONN_REQ or T_UNIDATA_REQ reaches the
6640Sstevel@tonic-gate 	 * transport the message will get an error or be dropped.
6650Sstevel@tonic-gate 	 */
6668348SEric.Yu@Sun.COM 	sti->sti_ux_faddr.soua_vp = vp;
6678348SEric.Yu@Sun.COM 	sti->sti_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT;
6688348SEric.Yu@Sun.COM 	addr = &sti->sti_ux_faddr;
6698348SEric.Yu@Sun.COM 	addrlen = (socklen_t)sizeof (sti->sti_ux_faddr);
6707240Srh87107 	dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n",
6717240Srh87107 	    addrlen, (void *)vp));
6720Sstevel@tonic-gate 	VN_RELE(vp);
6730Sstevel@tonic-gate 	*addrp = addr;
6740Sstevel@tonic-gate 	*addrlenp = (socklen_t)addrlen;
6750Sstevel@tonic-gate 	return (0);
6760Sstevel@tonic-gate }
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate /*
6790Sstevel@tonic-gate  * Esballoc free function for messages that contain SO_FILEP option.
6800Sstevel@tonic-gate  * Decrement the reference count on the file pointers using closef.
6810Sstevel@tonic-gate  */
6820Sstevel@tonic-gate void
fdbuf_free(struct fdbuf * fdbuf)6830Sstevel@tonic-gate fdbuf_free(struct fdbuf *fdbuf)
6840Sstevel@tonic-gate {
6850Sstevel@tonic-gate 	int	i;
6860Sstevel@tonic-gate 	struct file *fp;
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd));
6890Sstevel@tonic-gate 	for (i = 0; i < fdbuf->fd_numfd; i++) {
6900Sstevel@tonic-gate 		/*
6910Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
6920Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
6930Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
6940Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
6950Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
6960Sstevel@tonic-gate 		 */
6970Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
6987240Srh87107 		dprint(1, ("fdbuf_free: [%d] = %p\n", i, (void *)fp));
6990Sstevel@tonic-gate 		(void) closef(fp);
7000Sstevel@tonic-gate 	}
7010Sstevel@tonic-gate 	if (fdbuf->fd_ebuf != NULL)
7020Sstevel@tonic-gate 		kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen);
7030Sstevel@tonic-gate 	kmem_free(fdbuf, fdbuf->fd_size);
7040Sstevel@tonic-gate }
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate /*
707455Smeem  * Allocate an esballoc'ed message for AF_UNIX file descriptor passing.
708455Smeem  * Waits if memory is not available.
7090Sstevel@tonic-gate  */
7100Sstevel@tonic-gate mblk_t *
fdbuf_allocmsg(int size,struct fdbuf * fdbuf)7110Sstevel@tonic-gate fdbuf_allocmsg(int size, struct fdbuf *fdbuf)
7120Sstevel@tonic-gate {
713455Smeem 	uchar_t	*buf;
7140Sstevel@tonic-gate 	mblk_t	*mp;
7150Sstevel@tonic-gate 
7160Sstevel@tonic-gate 	dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd));
7170Sstevel@tonic-gate 	buf = kmem_alloc(size, KM_SLEEP);
7180Sstevel@tonic-gate 	fdbuf->fd_ebuf = (caddr_t)buf;
7190Sstevel@tonic-gate 	fdbuf->fd_ebuflen = size;
7200Sstevel@tonic-gate 	fdbuf->fd_frtn.free_func = fdbuf_free;
7210Sstevel@tonic-gate 	fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf;
7220Sstevel@tonic-gate 
723455Smeem 	mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn);
7240Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
7250Sstevel@tonic-gate 	return (mp);
7260Sstevel@tonic-gate }
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate /*
7290Sstevel@tonic-gate  * Extract file descriptors from a fdbuf.
7300Sstevel@tonic-gate  * Return list in rights/rightslen.
7310Sstevel@tonic-gate  */
7320Sstevel@tonic-gate /*ARGSUSED*/
7330Sstevel@tonic-gate static int
fdbuf_extract(struct fdbuf * fdbuf,void * rights,int rightslen)7340Sstevel@tonic-gate fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen)
7350Sstevel@tonic-gate {
7360Sstevel@tonic-gate 	int	i, fd;
7370Sstevel@tonic-gate 	int	*rp;
7380Sstevel@tonic-gate 	struct file *fp;
7390Sstevel@tonic-gate 	int	numfd;
7400Sstevel@tonic-gate 
7410Sstevel@tonic-gate 	dprint(1, ("fdbuf_extract: %d fds, len %d\n",
7425753Sgww 	    fdbuf->fd_numfd, rightslen));
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	numfd = fdbuf->fd_numfd;
7450Sstevel@tonic-gate 	ASSERT(rightslen == numfd * (int)sizeof (int));
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate 	/*
7480Sstevel@tonic-gate 	 * Allocate a file descriptor and increment the f_count.
7490Sstevel@tonic-gate 	 * The latter is needed since we always call fdbuf_free
7500Sstevel@tonic-gate 	 * which performs a closef.
7510Sstevel@tonic-gate 	 */
7520Sstevel@tonic-gate 	rp = (int *)rights;
7530Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
7540Sstevel@tonic-gate 		if ((fd = ufalloc(0)) == -1)
7550Sstevel@tonic-gate 			goto cleanup;
7560Sstevel@tonic-gate 		/*
7570Sstevel@tonic-gate 		 * We need pointer size alignment for fd_fds. On a LP64
7580Sstevel@tonic-gate 		 * kernel, the required alignment is 8 bytes while
7590Sstevel@tonic-gate 		 * the option headers and values are only 4 bytes
7600Sstevel@tonic-gate 		 * aligned. So its safer to do a bcopy compared to
7610Sstevel@tonic-gate 		 * assigning fdbuf->fd_fds[i] to fp.
7620Sstevel@tonic-gate 		 */
7630Sstevel@tonic-gate 		bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp));
7640Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
7650Sstevel@tonic-gate 		fp->f_count++;
7660Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
7670Sstevel@tonic-gate 		setf(fd, fp);
7680Sstevel@tonic-gate 		*rp++ = fd;
76911861SMarek.Pospisil@Sun.COM 		if (AU_AUDITING())
7700Sstevel@tonic-gate 			audit_fdrecv(fd, fp);
7710Sstevel@tonic-gate 		dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n",
7727240Srh87107 		    i, fd, (void *)fp, fp->f_count));
7730Sstevel@tonic-gate 	}
7740Sstevel@tonic-gate 	return (0);
7750Sstevel@tonic-gate 
7760Sstevel@tonic-gate cleanup:
7770Sstevel@tonic-gate 	/*
7780Sstevel@tonic-gate 	 * Undo whatever partial work the loop above has done.
7790Sstevel@tonic-gate 	 */
7800Sstevel@tonic-gate 	{
7810Sstevel@tonic-gate 		int j;
7820Sstevel@tonic-gate 
7830Sstevel@tonic-gate 		rp = (int *)rights;
7840Sstevel@tonic-gate 		for (j = 0; j < i; j++) {
7850Sstevel@tonic-gate 			dprint(0,
7860Sstevel@tonic-gate 			    ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp));
7870Sstevel@tonic-gate 			(void) closeandsetf(*rp++, NULL);
7880Sstevel@tonic-gate 		}
7890Sstevel@tonic-gate 	}
7900Sstevel@tonic-gate 
7910Sstevel@tonic-gate 	return (EMFILE);
7920Sstevel@tonic-gate }
7930Sstevel@tonic-gate 
7940Sstevel@tonic-gate /*
7950Sstevel@tonic-gate  * Insert file descriptors into an fdbuf.
7960Sstevel@tonic-gate  * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed
7970Sstevel@tonic-gate  * by calling fdbuf_free().
7980Sstevel@tonic-gate  */
7990Sstevel@tonic-gate int
fdbuf_create(void * rights,int rightslen,struct fdbuf ** fdbufp)8000Sstevel@tonic-gate fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp)
8010Sstevel@tonic-gate {
8020Sstevel@tonic-gate 	int		numfd, i;
8030Sstevel@tonic-gate 	int		*fds;
8040Sstevel@tonic-gate 	struct file	*fp;
8050Sstevel@tonic-gate 	struct fdbuf	*fdbuf;
8060Sstevel@tonic-gate 	int		fdbufsize;
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate 	dprint(1, ("fdbuf_create: len %d\n", rightslen));
8090Sstevel@tonic-gate 
8100Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
8110Sstevel@tonic-gate 
8120Sstevel@tonic-gate 	fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *));
8130Sstevel@tonic-gate 	fdbuf = kmem_alloc(fdbufsize, KM_SLEEP);
8140Sstevel@tonic-gate 	fdbuf->fd_size = fdbufsize;
8150Sstevel@tonic-gate 	fdbuf->fd_numfd = 0;
8160Sstevel@tonic-gate 	fdbuf->fd_ebuf = NULL;
8170Sstevel@tonic-gate 	fdbuf->fd_ebuflen = 0;
8180Sstevel@tonic-gate 	fds = (int *)rights;
8190Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
8200Sstevel@tonic-gate 		if ((fp = getf(fds[i])) == NULL) {
8210Sstevel@tonic-gate 			fdbuf_free(fdbuf);
8220Sstevel@tonic-gate 			return (EBADF);
8230Sstevel@tonic-gate 		}
8240Sstevel@tonic-gate 		dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n",
8257240Srh87107 		    i, fds[i], (void *)fp, fp->f_count));
8260Sstevel@tonic-gate 		mutex_enter(&fp->f_tlock);
8270Sstevel@tonic-gate 		fp->f_count++;
8280Sstevel@tonic-gate 		mutex_exit(&fp->f_tlock);
8290Sstevel@tonic-gate 		/*
8300Sstevel@tonic-gate 		 * The maximum alignment for fdbuf (or any option header
8310Sstevel@tonic-gate 		 * and its value) it 4 bytes. On a LP64 kernel, the alignment
8320Sstevel@tonic-gate 		 * is not sufficient for pointers (fd_fds in this case). Since
8330Sstevel@tonic-gate 		 * we just did a kmem_alloc (we get a double word alignment),
8340Sstevel@tonic-gate 		 * we don't need to do anything on the send side (we loose
8350Sstevel@tonic-gate 		 * the double word alignment because fdbuf goes after an
8360Sstevel@tonic-gate 		 * option header (eg T_unitdata_req) which is only 4 byte
8370Sstevel@tonic-gate 		 * aligned). We take care of this when we extract the file
8380Sstevel@tonic-gate 		 * descriptor in fdbuf_extract or fdbuf_free.
8390Sstevel@tonic-gate 		 */
8400Sstevel@tonic-gate 		fdbuf->fd_fds[i] = fp;
8410Sstevel@tonic-gate 		fdbuf->fd_numfd++;
8420Sstevel@tonic-gate 		releasef(fds[i]);
84311861SMarek.Pospisil@Sun.COM 		if (AU_AUDITING())
8440Sstevel@tonic-gate 			audit_fdsend(fds[i], fp, 0);
8450Sstevel@tonic-gate 	}
8460Sstevel@tonic-gate 	*fdbufp = fdbuf;
8470Sstevel@tonic-gate 	return (0);
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate static int
fdbuf_optlen(int rightslen)8510Sstevel@tonic-gate fdbuf_optlen(int rightslen)
8520Sstevel@tonic-gate {
8530Sstevel@tonic-gate 	int numfd;
8540Sstevel@tonic-gate 
8550Sstevel@tonic-gate 	numfd = rightslen / (int)sizeof (int);
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)));
8580Sstevel@tonic-gate }
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate static t_uscalar_t
fdbuf_cmsglen(int fdbuflen)8610Sstevel@tonic-gate fdbuf_cmsglen(int fdbuflen)
8620Sstevel@tonic-gate {
8630Sstevel@tonic-gate 	return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) /
8640Sstevel@tonic-gate 	    (int)sizeof (struct file *) * (int)sizeof (int));
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate /*
8690Sstevel@tonic-gate  * Return non-zero if the mblk and fdbuf are consistent.
8700Sstevel@tonic-gate  */
8710Sstevel@tonic-gate static int
fdbuf_verify(mblk_t * mp,struct fdbuf * fdbuf,int fdbuflen)8720Sstevel@tonic-gate fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen)
8730Sstevel@tonic-gate {
8740Sstevel@tonic-gate 	if (fdbuflen >= FDBUF_HDRSIZE &&
8750Sstevel@tonic-gate 	    fdbuflen == fdbuf->fd_size) {
8760Sstevel@tonic-gate 		frtn_t *frp = mp->b_datap->db_frtnp;
8770Sstevel@tonic-gate 		/*
8780Sstevel@tonic-gate 		 * Check that the SO_FILEP portion of the
8790Sstevel@tonic-gate 		 * message has not been modified by
8800Sstevel@tonic-gate 		 * the loopback transport. The sending sockfs generates
8810Sstevel@tonic-gate 		 * a message that is esballoc'ed with the free function
8820Sstevel@tonic-gate 		 * being fdbuf_free() and where free_arg contains the
8830Sstevel@tonic-gate 		 * identical information as the SO_FILEP content.
8840Sstevel@tonic-gate 		 *
8850Sstevel@tonic-gate 		 * If any of these constraints are not satisfied we
8860Sstevel@tonic-gate 		 * silently ignore the option.
8870Sstevel@tonic-gate 		 */
8880Sstevel@tonic-gate 		ASSERT(mp);
8890Sstevel@tonic-gate 		if (frp != NULL &&
8900Sstevel@tonic-gate 		    frp->free_func == fdbuf_free &&
8910Sstevel@tonic-gate 		    frp->free_arg != NULL &&
8920Sstevel@tonic-gate 		    bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) {
8930Sstevel@tonic-gate 			dprint(1, ("fdbuf_verify: fdbuf %p len %d\n",
8947240Srh87107 			    (void *)fdbuf, fdbuflen));
8950Sstevel@tonic-gate 			return (1);
8960Sstevel@tonic-gate 		} else {
8971548Srshoaib 			zcmn_err(getzoneid(), CE_WARN,
8980Sstevel@tonic-gate 			    "sockfs: mismatched fdbuf content (%p)",
8990Sstevel@tonic-gate 			    (void *)mp);
9000Sstevel@tonic-gate 			return (0);
9010Sstevel@tonic-gate 		}
9020Sstevel@tonic-gate 	} else {
9031548Srshoaib 		zcmn_err(getzoneid(), CE_WARN,
9040Sstevel@tonic-gate 		    "sockfs: mismatched fdbuf len %d, %d\n",
9050Sstevel@tonic-gate 		    fdbuflen, fdbuf->fd_size);
9060Sstevel@tonic-gate 		return (0);
9070Sstevel@tonic-gate 	}
9080Sstevel@tonic-gate }
9090Sstevel@tonic-gate 
9100Sstevel@tonic-gate /*
9110Sstevel@tonic-gate  * When the file descriptors returned by sorecvmsg can not be passed
9120Sstevel@tonic-gate  * to the application this routine will cleanup the references on
9130Sstevel@tonic-gate  * the files. Start at startoff bytes into the buffer.
9140Sstevel@tonic-gate  */
9150Sstevel@tonic-gate static void
close_fds(void * fdbuf,int fdbuflen,int startoff)9160Sstevel@tonic-gate close_fds(void *fdbuf, int fdbuflen, int startoff)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	int *fds = (int *)fdbuf;
9190Sstevel@tonic-gate 	int numfd = fdbuflen / (int)sizeof (int);
9200Sstevel@tonic-gate 	int i;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 	dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff));
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate 	for (i = 0; i < numfd; i++) {
9250Sstevel@tonic-gate 		if (startoff < 0)
9260Sstevel@tonic-gate 			startoff = 0;
9270Sstevel@tonic-gate 		if (startoff < (int)sizeof (int)) {
9280Sstevel@tonic-gate 			/*
9290Sstevel@tonic-gate 			 * This file descriptor is partially or fully after
9300Sstevel@tonic-gate 			 * the offset
9310Sstevel@tonic-gate 			 */
9320Sstevel@tonic-gate 			dprint(0,
9330Sstevel@tonic-gate 			    ("close_fds: cleanup[%d] = %d\n", i, fds[i]));
9340Sstevel@tonic-gate 			(void) closeandsetf(fds[i], NULL);
9350Sstevel@tonic-gate 		}
9360Sstevel@tonic-gate 		startoff -= (int)sizeof (int);
9370Sstevel@tonic-gate 	}
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate /*
9410Sstevel@tonic-gate  * Close all file descriptors contained in the control part starting at
9420Sstevel@tonic-gate  * the startoffset.
9430Sstevel@tonic-gate  */
9440Sstevel@tonic-gate void
so_closefds(void * control,t_uscalar_t controllen,int oldflg,int startoff)9450Sstevel@tonic-gate so_closefds(void *control, t_uscalar_t controllen, int oldflg,
9460Sstevel@tonic-gate     int startoff)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate 	struct cmsghdr *cmsg;
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate 	if (control == NULL)
9510Sstevel@tonic-gate 		return;
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate 	if (oldflg) {
9540Sstevel@tonic-gate 		close_fds(control, controllen, startoff);
9550Sstevel@tonic-gate 		return;
9560Sstevel@tonic-gate 	}
9570Sstevel@tonic-gate 	/* Scan control part for file descriptors. */
9580Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
9590Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
9600Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
9610Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
9620Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
9630Sstevel@tonic-gate 			close_fds(CMSG_CONTENT(cmsg),
9640Sstevel@tonic-gate 			    (int)CMSG_CONTENTLEN(cmsg),
9650Sstevel@tonic-gate 			    startoff - (int)sizeof (struct cmsghdr));
9660Sstevel@tonic-gate 		}
9670Sstevel@tonic-gate 		startoff -= cmsg->cmsg_len;
9680Sstevel@tonic-gate 	}
9690Sstevel@tonic-gate }
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate /*
9720Sstevel@tonic-gate  * Returns a pointer/length for the file descriptors contained
9730Sstevel@tonic-gate  * in the control buffer. Returns with *fdlenp == -1 if there are no
9740Sstevel@tonic-gate  * file descriptor options present. This is different than there being
9750Sstevel@tonic-gate  * a zero-length file descriptor option.
9760Sstevel@tonic-gate  * Fail if there are multiple SCM_RIGHT cmsgs.
9770Sstevel@tonic-gate  */
9780Sstevel@tonic-gate int
so_getfdopt(void * control,t_uscalar_t controllen,int oldflg,void ** fdsp,int * fdlenp)9790Sstevel@tonic-gate so_getfdopt(void *control, t_uscalar_t controllen, int oldflg,
9800Sstevel@tonic-gate     void **fdsp, int *fdlenp)
9810Sstevel@tonic-gate {
9820Sstevel@tonic-gate 	struct cmsghdr *cmsg;
9830Sstevel@tonic-gate 	void *fds;
9840Sstevel@tonic-gate 	int fdlen;
9850Sstevel@tonic-gate 
9860Sstevel@tonic-gate 	if (control == NULL) {
9870Sstevel@tonic-gate 		*fdsp = NULL;
9880Sstevel@tonic-gate 		*fdlenp = -1;
9890Sstevel@tonic-gate 		return (0);
9900Sstevel@tonic-gate 	}
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate 	if (oldflg) {
9930Sstevel@tonic-gate 		*fdsp = control;
9940Sstevel@tonic-gate 		if (controllen == 0)
9950Sstevel@tonic-gate 			*fdlenp = -1;
9960Sstevel@tonic-gate 		else
9970Sstevel@tonic-gate 			*fdlenp = controllen;
9980Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: old %d\n", *fdlenp));
9990Sstevel@tonic-gate 		return (0);
10000Sstevel@tonic-gate 	}
10010Sstevel@tonic-gate 
10020Sstevel@tonic-gate 	fds = NULL;
10030Sstevel@tonic-gate 	fdlen = 0;
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10060Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10070Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10080Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10090Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
10100Sstevel@tonic-gate 			if (fds != NULL)
10110Sstevel@tonic-gate 				return (EINVAL);
10120Sstevel@tonic-gate 			fds = CMSG_CONTENT(cmsg);
10130Sstevel@tonic-gate 			fdlen = (int)CMSG_CONTENTLEN(cmsg);
1014408Skrgopi 			dprint(1, ("so_getfdopt: new %lu\n",
10155753Sgww 			    (size_t)CMSG_CONTENTLEN(cmsg)));
10160Sstevel@tonic-gate 		}
10170Sstevel@tonic-gate 	}
10180Sstevel@tonic-gate 	if (fds == NULL) {
10190Sstevel@tonic-gate 		dprint(1, ("so_getfdopt: NONE\n"));
10200Sstevel@tonic-gate 		*fdlenp = -1;
10210Sstevel@tonic-gate 	} else
10220Sstevel@tonic-gate 		*fdlenp = fdlen;
10230Sstevel@tonic-gate 	*fdsp = fds;
10240Sstevel@tonic-gate 	return (0);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate /*
10280Sstevel@tonic-gate  * Return the length of the options including any file descriptor options.
10290Sstevel@tonic-gate  */
10300Sstevel@tonic-gate t_uscalar_t
so_optlen(void * control,t_uscalar_t controllen,int oldflg)10310Sstevel@tonic-gate so_optlen(void *control, t_uscalar_t controllen, int oldflg)
10320Sstevel@tonic-gate {
10330Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10340Sstevel@tonic-gate 	t_uscalar_t optlen = 0;
10350Sstevel@tonic-gate 	t_uscalar_t len;
10360Sstevel@tonic-gate 
10370Sstevel@tonic-gate 	if (control == NULL)
10380Sstevel@tonic-gate 		return (0);
10390Sstevel@tonic-gate 
10400Sstevel@tonic-gate 	if (oldflg)
10410Sstevel@tonic-gate 		return ((t_uscalar_t)(sizeof (struct T_opthdr) +
10420Sstevel@tonic-gate 		    fdbuf_optlen(controllen)));
10430Sstevel@tonic-gate 
10440Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10450Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10460Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10470Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10480Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS) {
10490Sstevel@tonic-gate 			len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg));
10500Sstevel@tonic-gate 		} else {
10510Sstevel@tonic-gate 			len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
10520Sstevel@tonic-gate 		}
10530Sstevel@tonic-gate 		optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) +
10540Sstevel@tonic-gate 		    sizeof (struct T_opthdr));
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 	dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n",
10575753Sgww 	    controllen, oldflg, optlen));
10580Sstevel@tonic-gate 	return (optlen);
10590Sstevel@tonic-gate }
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate /*
10620Sstevel@tonic-gate  * Copy options from control to the mblk. Skip any file descriptor options.
10630Sstevel@tonic-gate  */
10640Sstevel@tonic-gate void
so_cmsg2opt(void * control,t_uscalar_t controllen,int oldflg,mblk_t * mp)10650Sstevel@tonic-gate so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp)
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate 	struct T_opthdr toh;
10680Sstevel@tonic-gate 	struct cmsghdr *cmsg;
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate 	if (control == NULL)
10710Sstevel@tonic-gate 		return;
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate 	if (oldflg) {
10740Sstevel@tonic-gate 		/* No real options - caller has handled file descriptors */
10750Sstevel@tonic-gate 		return;
10760Sstevel@tonic-gate 	}
10770Sstevel@tonic-gate 	for (cmsg = (struct cmsghdr *)control;
10780Sstevel@tonic-gate 	    CMSG_VALID(cmsg, control, (uintptr_t)control + controllen);
10790Sstevel@tonic-gate 	    cmsg = CMSG_NEXT(cmsg)) {
10800Sstevel@tonic-gate 		/*
10810Sstevel@tonic-gate 		 * Note: The caller handles file descriptors prior
10820Sstevel@tonic-gate 		 * to calling this function.
10830Sstevel@tonic-gate 		 */
10840Sstevel@tonic-gate 		t_uscalar_t len;
10850Sstevel@tonic-gate 
10860Sstevel@tonic-gate 		if (cmsg->cmsg_level == SOL_SOCKET &&
10870Sstevel@tonic-gate 		    cmsg->cmsg_type == SCM_RIGHTS)
10880Sstevel@tonic-gate 			continue;
10890Sstevel@tonic-gate 
10900Sstevel@tonic-gate 		len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg);
10910Sstevel@tonic-gate 		toh.level = cmsg->cmsg_level;
10920Sstevel@tonic-gate 		toh.name = cmsg->cmsg_type;
10930Sstevel@tonic-gate 		toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr);
10940Sstevel@tonic-gate 		toh.status = 0;
10950Sstevel@tonic-gate 
10960Sstevel@tonic-gate 		soappendmsg(mp, &toh, sizeof (toh));
10970Sstevel@tonic-gate 		soappendmsg(mp, CMSG_CONTENT(cmsg), len);
10980Sstevel@tonic-gate 		mp->b_wptr += _TPI_ALIGN_TOPT(len) - len;
10990Sstevel@tonic-gate 		ASSERT(mp->b_wptr <= mp->b_datap->db_lim);
11000Sstevel@tonic-gate 	}
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate /*
11040Sstevel@tonic-gate  * Return the length of the control message derived from the options.
11050Sstevel@tonic-gate  * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP.
11060Sstevel@tonic-gate  * When oldflg is set only include SO_FILEP.
11072280Sgt145670  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
11082280Sgt145670  * allocates the space that so_opt2cmsg fills. If one changes, the other should
11092280Sgt145670  * also be checked for any possible impacts.
11100Sstevel@tonic-gate  */
11110Sstevel@tonic-gate t_uscalar_t
so_cmsglen(mblk_t * mp,void * opt,t_uscalar_t optlen,int oldflg)11120Sstevel@tonic-gate so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg)
11130Sstevel@tonic-gate {
11140Sstevel@tonic-gate 	t_uscalar_t cmsglen = 0;
11150Sstevel@tonic-gate 	struct T_opthdr *tohp;
11160Sstevel@tonic-gate 	t_uscalar_t len;
11170Sstevel@tonic-gate 	t_uscalar_t last_roundup = 0;
11180Sstevel@tonic-gate 
11190Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
11200Sstevel@tonic-gate 
11210Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
11220Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
11230Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
11240Sstevel@tonic-gate 		dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n",
11255753Sgww 		    tohp->level, tohp->name, tohp->len));
11260Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
11270Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
11280Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
11290Sstevel@tonic-gate 			continue;
11300Sstevel@tonic-gate 		}
11310Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
11320Sstevel@tonic-gate 			struct fdbuf *fdbuf;
11330Sstevel@tonic-gate 			int fdbuflen;
11340Sstevel@tonic-gate 
11350Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
11360Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
11390Sstevel@tonic-gate 				continue;
11400Sstevel@tonic-gate 			if (oldflg) {
11410Sstevel@tonic-gate 				cmsglen += fdbuf_cmsglen(fdbuflen);
11420Sstevel@tonic-gate 				continue;
11430Sstevel@tonic-gate 			}
11440Sstevel@tonic-gate 			len = fdbuf_cmsglen(fdbuflen);
11452280Sgt145670 		} else if (tohp->level == SOL_SOCKET &&
11462280Sgt145670 		    tohp->name == SCM_TIMESTAMP) {
11472280Sgt145670 			if (oldflg)
11482280Sgt145670 				continue;
11492280Sgt145670 
11502280Sgt145670 			if (get_udatamodel() == DATAMODEL_NATIVE) {
11512280Sgt145670 				len = sizeof (struct timeval);
11522280Sgt145670 			} else {
11532280Sgt145670 				len = sizeof (struct timeval32);
11542280Sgt145670 			}
11550Sstevel@tonic-gate 		} else {
11560Sstevel@tonic-gate 			if (oldflg)
11570Sstevel@tonic-gate 				continue;
11580Sstevel@tonic-gate 			len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
11590Sstevel@tonic-gate 		}
11600Sstevel@tonic-gate 		/*
11612280Sgt145670 		 * Exclude roundup for last option to not set
11620Sstevel@tonic-gate 		 * MSG_CTRUNC when the cmsg fits but the padding doesn't fit.
11630Sstevel@tonic-gate 		 */
11640Sstevel@tonic-gate 		last_roundup = (t_uscalar_t)
11650Sstevel@tonic-gate 		    (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) -
11660Sstevel@tonic-gate 		    (len + (int)sizeof (struct cmsghdr)));
11670Sstevel@tonic-gate 		cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) +
11680Sstevel@tonic-gate 		    last_roundup;
11690Sstevel@tonic-gate 	}
11700Sstevel@tonic-gate 	cmsglen -= last_roundup;
11710Sstevel@tonic-gate 	dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n",
11725753Sgww 	    optlen, oldflg, cmsglen));
11730Sstevel@tonic-gate 	return (cmsglen);
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate /*
11770Sstevel@tonic-gate  * Copy options from options to the control. Convert SO_FILEP to
11780Sstevel@tonic-gate  * file descriptors.
11790Sstevel@tonic-gate  * Returns errno or zero.
11802280Sgt145670  * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen
11812280Sgt145670  * allocates the space that so_opt2cmsg fills. If one changes, the other should
11822280Sgt145670  * also be checked for any possible impacts.
11830Sstevel@tonic-gate  */
11840Sstevel@tonic-gate int
so_opt2cmsg(mblk_t * mp,void * opt,t_uscalar_t optlen,int oldflg,void * control,t_uscalar_t controllen)11850Sstevel@tonic-gate so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg,
11860Sstevel@tonic-gate     void *control, t_uscalar_t controllen)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate 	struct T_opthdr *tohp;
11890Sstevel@tonic-gate 	struct cmsghdr *cmsg;
11900Sstevel@tonic-gate 	struct fdbuf *fdbuf;
11910Sstevel@tonic-gate 	int fdbuflen;
11920Sstevel@tonic-gate 	int error;
11932280Sgt145670 #if defined(DEBUG) || defined(__lint)
11942280Sgt145670 	struct cmsghdr *cend = (struct cmsghdr *)
11952280Sgt145670 	    (((uint8_t *)control) + ROUNDUP_cmsglen(controllen));
11962280Sgt145670 #endif
11970Sstevel@tonic-gate 	cmsg = (struct cmsghdr *)control;
11980Sstevel@tonic-gate 
11990Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
12000Sstevel@tonic-gate 
12010Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
12020Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
12030Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
12040Sstevel@tonic-gate 		dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n",
12055753Sgww 		    tohp->level, tohp->name, tohp->len));
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
12080Sstevel@tonic-gate 		    (tohp->name == SO_SRCADDR ||
12090Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)) {
12100Sstevel@tonic-gate 			continue;
12110Sstevel@tonic-gate 		}
12120Sstevel@tonic-gate 		ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen);
12130Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) {
12140Sstevel@tonic-gate 			fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp);
12150Sstevel@tonic-gate 			fdbuflen = (int)_TPI_TOPT_DATALEN(tohp);
12160Sstevel@tonic-gate 
12170Sstevel@tonic-gate 			if (!fdbuf_verify(mp, fdbuf, fdbuflen))
12180Sstevel@tonic-gate 				return (EPROTO);
12190Sstevel@tonic-gate 			if (oldflg) {
12200Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf, control,
12210Sstevel@tonic-gate 				    (int)controllen);
12220Sstevel@tonic-gate 				if (error != 0)
12230Sstevel@tonic-gate 					return (error);
12240Sstevel@tonic-gate 				continue;
12250Sstevel@tonic-gate 			} else {
12260Sstevel@tonic-gate 				int fdlen;
12270Sstevel@tonic-gate 
12280Sstevel@tonic-gate 				fdlen = (int)fdbuf_cmsglen(
12290Sstevel@tonic-gate 				    (int)_TPI_TOPT_DATALEN(tohp));
12300Sstevel@tonic-gate 
12310Sstevel@tonic-gate 				cmsg->cmsg_level = tohp->level;
12320Sstevel@tonic-gate 				cmsg->cmsg_type = SCM_RIGHTS;
12330Sstevel@tonic-gate 				cmsg->cmsg_len = (socklen_t)(fdlen +
12345753Sgww 				    sizeof (struct cmsghdr));
12350Sstevel@tonic-gate 
12360Sstevel@tonic-gate 				error = fdbuf_extract(fdbuf,
12375753Sgww 				    CMSG_CONTENT(cmsg), fdlen);
12380Sstevel@tonic-gate 				if (error != 0)
12390Sstevel@tonic-gate 					return (error);
12400Sstevel@tonic-gate 			}
12411673Sgt145670 		} else if (tohp->level == SOL_SOCKET &&
12421673Sgt145670 		    tohp->name == SCM_TIMESTAMP) {
12431673Sgt145670 			timestruc_t *timestamp;
12441673Sgt145670 
12451673Sgt145670 			if (oldflg)
12461673Sgt145670 				continue;
12471673Sgt145670 
12481673Sgt145670 			cmsg->cmsg_level = tohp->level;
12491673Sgt145670 			cmsg->cmsg_type = tohp->name;
12501673Sgt145670 
12511673Sgt145670 			timestamp =
12521673Sgt145670 			    (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1],
12531673Sgt145670 			    sizeof (intptr_t));
12541673Sgt145670 
12551673Sgt145670 			if (get_udatamodel() == DATAMODEL_NATIVE) {
12562280Sgt145670 				struct timeval tv;
12571673Sgt145670 
12581673Sgt145670 				cmsg->cmsg_len = sizeof (struct timeval) +
12591673Sgt145670 				    sizeof (struct cmsghdr);
12602280Sgt145670 				tv.tv_sec = timestamp->tv_sec;
12612280Sgt145670 				tv.tv_usec = timestamp->tv_nsec /
12622280Sgt145670 				    (NANOSEC / MICROSEC);
12632280Sgt145670 				/*
12642280Sgt145670 				 * on LP64 systems, the struct timeval in
12652280Sgt145670 				 * the destination will not be 8-byte aligned,
12662280Sgt145670 				 * so use bcopy to avoid alignment trouble
12672280Sgt145670 				 */
12682280Sgt145670 				bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv));
12691673Sgt145670 			} else {
12701673Sgt145670 				struct timeval32 *time32;
12711673Sgt145670 
12721673Sgt145670 				cmsg->cmsg_len = sizeof (struct timeval32) +
12731673Sgt145670 				    sizeof (struct cmsghdr);
12741673Sgt145670 				time32 = (struct timeval32 *)CMSG_CONTENT(cmsg);
12751673Sgt145670 				time32->tv_sec = (time32_t)timestamp->tv_sec;
12761673Sgt145670 				time32->tv_usec =
12771673Sgt145670 				    (int32_t)(timestamp->tv_nsec /
12781673Sgt145670 				    (NANOSEC / MICROSEC));
12791673Sgt145670 			}
12801673Sgt145670 
12810Sstevel@tonic-gate 		} else {
12820Sstevel@tonic-gate 			if (oldflg)
12830Sstevel@tonic-gate 				continue;
12840Sstevel@tonic-gate 
12850Sstevel@tonic-gate 			cmsg->cmsg_level = tohp->level;
12860Sstevel@tonic-gate 			cmsg->cmsg_type = tohp->name;
12870Sstevel@tonic-gate 			cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) +
12880Sstevel@tonic-gate 			    sizeof (struct cmsghdr));
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate 			/* copy content to control data part */
12910Sstevel@tonic-gate 			bcopy(&tohp[1], CMSG_CONTENT(cmsg),
12925753Sgww 			    CMSG_CONTENTLEN(cmsg));
12930Sstevel@tonic-gate 		}
12940Sstevel@tonic-gate 		/* move to next CMSG structure! */
12950Sstevel@tonic-gate 		cmsg = CMSG_NEXT(cmsg);
12960Sstevel@tonic-gate 	}
12972280Sgt145670 	dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n",
12987240Srh87107 	    control, controllen, (void *)cend, (void *)cmsg));
12992280Sgt145670 	ASSERT(cmsg <= cend);
13000Sstevel@tonic-gate 	return (0);
13010Sstevel@tonic-gate }
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate /*
13040Sstevel@tonic-gate  * Extract the SO_SRCADDR option value if present.
13050Sstevel@tonic-gate  */
13060Sstevel@tonic-gate void
so_getopt_srcaddr(void * opt,t_uscalar_t optlen,void ** srcp,t_uscalar_t * srclenp)13070Sstevel@tonic-gate so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp,
13080Sstevel@tonic-gate     t_uscalar_t *srclenp)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	struct T_opthdr		*tohp;
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 	ASSERT(srcp != NULL && srclenp != NULL);
13150Sstevel@tonic-gate 	*srcp = NULL;
13160Sstevel@tonic-gate 	*srclenp = 0;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
13190Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
13200Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
13210Sstevel@tonic-gate 		dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n",
13225753Sgww 		    tohp->level, tohp->name, tohp->len));
13230Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
13240Sstevel@tonic-gate 		    tohp->name == SO_SRCADDR) {
13250Sstevel@tonic-gate 			*srcp = _TPI_TOPT_DATA(tohp);
13260Sstevel@tonic-gate 			*srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp);
13270Sstevel@tonic-gate 		}
13280Sstevel@tonic-gate 	}
13290Sstevel@tonic-gate }
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate /*
13320Sstevel@tonic-gate  * Verify if the SO_UNIX_CLOSE option is present.
13330Sstevel@tonic-gate  */
13340Sstevel@tonic-gate int
so_getopt_unix_close(void * opt,t_uscalar_t optlen)13350Sstevel@tonic-gate so_getopt_unix_close(void *opt, t_uscalar_t optlen)
13360Sstevel@tonic-gate {
13370Sstevel@tonic-gate 	struct T_opthdr		*tohp;
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 	ASSERT(__TPI_TOPT_ISALIGNED(opt));
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 	for (tohp = (struct T_opthdr *)opt;
13420Sstevel@tonic-gate 	    tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen);
13430Sstevel@tonic-gate 	    tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) {
13440Sstevel@tonic-gate 		dprint(1,
13455753Sgww 		    ("so_getopt_unix_close: level 0x%x, name %d, len %d\n",
13465753Sgww 		    tohp->level, tohp->name, tohp->len));
13470Sstevel@tonic-gate 		if (tohp->level == SOL_SOCKET &&
13480Sstevel@tonic-gate 		    tohp->name == SO_UNIX_CLOSE)
13490Sstevel@tonic-gate 			return (1);
13500Sstevel@tonic-gate 	}
13510Sstevel@tonic-gate 	return (0);
13520Sstevel@tonic-gate }
13530Sstevel@tonic-gate 
13540Sstevel@tonic-gate /*
13550Sstevel@tonic-gate  * Allocate an M_PROTO message.
13560Sstevel@tonic-gate  *
13570Sstevel@tonic-gate  * If allocation fails the behavior depends on sleepflg:
13580Sstevel@tonic-gate  *	_ALLOC_NOSLEEP	fail immediately
13590Sstevel@tonic-gate  *	_ALLOC_INTR	sleep for memory until a signal is caught
13600Sstevel@tonic-gate  *	_ALLOC_SLEEP	sleep forever. Don't return NULL.
13610Sstevel@tonic-gate  */
13620Sstevel@tonic-gate mblk_t *
soallocproto(size_t size,int sleepflg,cred_t * cr)13638778SErik.Nordmark@Sun.COM soallocproto(size_t size, int sleepflg, cred_t *cr)
13640Sstevel@tonic-gate {
13650Sstevel@tonic-gate 	mblk_t	*mp;
13660Sstevel@tonic-gate 
13670Sstevel@tonic-gate 	/* Round up size for reuse */
13680Sstevel@tonic-gate 	size = MAX(size, 64);
13698778SErik.Nordmark@Sun.COM 	if (cr != NULL)
13708778SErik.Nordmark@Sun.COM 		mp = allocb_cred(size, cr, curproc->p_pid);
13718778SErik.Nordmark@Sun.COM 	else
13728778SErik.Nordmark@Sun.COM 		mp = allocb(size, BPRI_MED);
13738778SErik.Nordmark@Sun.COM 
13740Sstevel@tonic-gate 	if (mp == NULL) {
13750Sstevel@tonic-gate 		int error;	/* Dummy - error not returned to caller */
13760Sstevel@tonic-gate 
13770Sstevel@tonic-gate 		switch (sleepflg) {
13780Sstevel@tonic-gate 		case _ALLOC_SLEEP:
13798778SErik.Nordmark@Sun.COM 			if (cr != NULL) {
13808778SErik.Nordmark@Sun.COM 				mp = allocb_cred_wait(size, STR_NOSIG, &error,
13818778SErik.Nordmark@Sun.COM 				    cr, curproc->p_pid);
13828778SErik.Nordmark@Sun.COM 			} else {
13838778SErik.Nordmark@Sun.COM 				mp = allocb_wait(size, BPRI_MED, STR_NOSIG,
13848778SErik.Nordmark@Sun.COM 				    &error);
13858778SErik.Nordmark@Sun.COM 			}
13860Sstevel@tonic-gate 			ASSERT(mp);
13870Sstevel@tonic-gate 			break;
13880Sstevel@tonic-gate 		case _ALLOC_INTR:
13898778SErik.Nordmark@Sun.COM 			if (cr != NULL) {
13908778SErik.Nordmark@Sun.COM 				mp = allocb_cred_wait(size, 0, &error, cr,
13918778SErik.Nordmark@Sun.COM 				    curproc->p_pid);
13928778SErik.Nordmark@Sun.COM 			} else {
13938778SErik.Nordmark@Sun.COM 				mp = allocb_wait(size, BPRI_MED, 0, &error);
13948778SErik.Nordmark@Sun.COM 			}
13950Sstevel@tonic-gate 			if (mp == NULL) {
13960Sstevel@tonic-gate 				/* Caught signal while sleeping for memory */
13970Sstevel@tonic-gate 				eprintline(ENOBUFS);
13980Sstevel@tonic-gate 				return (NULL);
13990Sstevel@tonic-gate 			}
14000Sstevel@tonic-gate 			break;
14010Sstevel@tonic-gate 		case _ALLOC_NOSLEEP:
14020Sstevel@tonic-gate 		default:
14030Sstevel@tonic-gate 			eprintline(ENOBUFS);
14040Sstevel@tonic-gate 			return (NULL);
14050Sstevel@tonic-gate 		}
14060Sstevel@tonic-gate 	}
14070Sstevel@tonic-gate 	DB_TYPE(mp) = M_PROTO;
14080Sstevel@tonic-gate 	return (mp);
14090Sstevel@tonic-gate }
14100Sstevel@tonic-gate 
14110Sstevel@tonic-gate /*
14120Sstevel@tonic-gate  * Allocate an M_PROTO message with a single component.
14130Sstevel@tonic-gate  * len is the length of buf. size is the amount to allocate.
14140Sstevel@tonic-gate  *
14150Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
14160Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
14170Sstevel@tonic-gate  */
14180Sstevel@tonic-gate mblk_t *
soallocproto1(const void * buf,ssize_t len,ssize_t size,int sleepflg,cred_t * cr)14198778SErik.Nordmark@Sun.COM soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg,
14208778SErik.Nordmark@Sun.COM     cred_t *cr)
14210Sstevel@tonic-gate {
14220Sstevel@tonic-gate 	mblk_t	*mp;
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate 	if (size == 0)
14250Sstevel@tonic-gate 		size = len;
14260Sstevel@tonic-gate 
14270Sstevel@tonic-gate 	ASSERT(size >= len);
14280Sstevel@tonic-gate 	/* Round up size for reuse */
14290Sstevel@tonic-gate 	size = MAX(size, 64);
14308778SErik.Nordmark@Sun.COM 	mp = soallocproto(size, sleepflg, cr);
14310Sstevel@tonic-gate 	if (mp == NULL)
14320Sstevel@tonic-gate 		return (NULL);
14330Sstevel@tonic-gate 	mp->b_datap->db_type = M_PROTO;
14340Sstevel@tonic-gate 	if (len != 0) {
14350Sstevel@tonic-gate 		if (buf != NULL)
14360Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
14370Sstevel@tonic-gate 		else
14380Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
14390Sstevel@tonic-gate 		mp->b_wptr += len;
14400Sstevel@tonic-gate 	}
14410Sstevel@tonic-gate 	return (mp);
14420Sstevel@tonic-gate }
14430Sstevel@tonic-gate 
14440Sstevel@tonic-gate /*
14450Sstevel@tonic-gate  * Append buf/len to mp.
14460Sstevel@tonic-gate  * The caller has to ensure that there is enough room in the mblk.
14470Sstevel@tonic-gate  *
14480Sstevel@tonic-gate  * buf can be NULL with a non-zero len.
14490Sstevel@tonic-gate  * This results in a bzero'ed chunk being placed the message.
14500Sstevel@tonic-gate  */
14510Sstevel@tonic-gate void
soappendmsg(mblk_t * mp,const void * buf,ssize_t len)14520Sstevel@tonic-gate soappendmsg(mblk_t *mp, const void *buf, ssize_t len)
14530Sstevel@tonic-gate {
14540Sstevel@tonic-gate 	ASSERT(mp);
14550Sstevel@tonic-gate 
14560Sstevel@tonic-gate 	if (len != 0) {
14570Sstevel@tonic-gate 		/* Assert for room left */
14580Sstevel@tonic-gate 		ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len);
14590Sstevel@tonic-gate 		if (buf != NULL)
14600Sstevel@tonic-gate 			bcopy(buf, mp->b_wptr, len);
14610Sstevel@tonic-gate 		else
14620Sstevel@tonic-gate 			bzero(mp->b_wptr, len);
14630Sstevel@tonic-gate 	}
14640Sstevel@tonic-gate 	mp->b_wptr += len;
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate /*
14680Sstevel@tonic-gate  * Create a message using two kernel buffers.
14690Sstevel@tonic-gate  * If size is set that will determine the allocation size (e.g. for future
14700Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
14710Sstevel@tonic-gate  * lengths.
14720Sstevel@tonic-gate  */
14730Sstevel@tonic-gate mblk_t *
soallocproto2(const void * buf1,ssize_t len1,const void * buf2,ssize_t len2,ssize_t size,int sleepflg,cred_t * cr)14740Sstevel@tonic-gate soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
14758778SErik.Nordmark@Sun.COM     ssize_t size, int sleepflg, cred_t *cr)
14760Sstevel@tonic-gate {
14770Sstevel@tonic-gate 	mblk_t *mp;
14780Sstevel@tonic-gate 
14790Sstevel@tonic-gate 	if (size == 0)
14800Sstevel@tonic-gate 		size = len1 + len2;
14810Sstevel@tonic-gate 	ASSERT(size >= len1 + len2);
14820Sstevel@tonic-gate 
14838778SErik.Nordmark@Sun.COM 	mp = soallocproto1(buf1, len1, size, sleepflg, cr);
14840Sstevel@tonic-gate 	if (mp)
14850Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
14860Sstevel@tonic-gate 	return (mp);
14870Sstevel@tonic-gate }
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate /*
14900Sstevel@tonic-gate  * Create a message using three kernel buffers.
14910Sstevel@tonic-gate  * If size is set that will determine the allocation size (for future
14920Sstevel@tonic-gate  * soappendmsg calls). If size is zero it is derived from the buffer
14930Sstevel@tonic-gate  * lengths.
14940Sstevel@tonic-gate  */
14950Sstevel@tonic-gate mblk_t *
soallocproto3(const void * buf1,ssize_t len1,const void * buf2,ssize_t len2,const void * buf3,ssize_t len3,ssize_t size,int sleepflg,cred_t * cr)14960Sstevel@tonic-gate soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2,
14978778SErik.Nordmark@Sun.COM     const void *buf3, ssize_t len3, ssize_t size, int sleepflg, cred_t *cr)
14980Sstevel@tonic-gate {
14990Sstevel@tonic-gate 	mblk_t *mp;
15000Sstevel@tonic-gate 
15010Sstevel@tonic-gate 	if (size == 0)
15020Sstevel@tonic-gate 		size = len1 + len2 +len3;
15030Sstevel@tonic-gate 	ASSERT(size >= len1 + len2 + len3);
15040Sstevel@tonic-gate 
15058778SErik.Nordmark@Sun.COM 	mp = soallocproto1(buf1, len1, size, sleepflg, cr);
15060Sstevel@tonic-gate 	if (mp != NULL) {
15070Sstevel@tonic-gate 		soappendmsg(mp, buf2, len2);
15080Sstevel@tonic-gate 		soappendmsg(mp, buf3, len3);
15090Sstevel@tonic-gate 	}
15100Sstevel@tonic-gate 	return (mp);
15110Sstevel@tonic-gate }
15120Sstevel@tonic-gate 
15130Sstevel@tonic-gate #ifdef DEBUG
15140Sstevel@tonic-gate char *
pr_state(uint_t state,uint_t mode)15150Sstevel@tonic-gate pr_state(uint_t state, uint_t mode)
15160Sstevel@tonic-gate {
15170Sstevel@tonic-gate 	static char buf[1024];
15180Sstevel@tonic-gate 
15190Sstevel@tonic-gate 	buf[0] = 0;
15200Sstevel@tonic-gate 	if (state & SS_ISCONNECTED)
15217240Srh87107 		(void) strcat(buf, "ISCONNECTED ");
15220Sstevel@tonic-gate 	if (state & SS_ISCONNECTING)
15237240Srh87107 		(void) strcat(buf, "ISCONNECTING ");
15240Sstevel@tonic-gate 	if (state & SS_ISDISCONNECTING)
15257240Srh87107 		(void) strcat(buf, "ISDISCONNECTING ");
15260Sstevel@tonic-gate 	if (state & SS_CANTSENDMORE)
15277240Srh87107 		(void) strcat(buf, "CANTSENDMORE ");
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate 	if (state & SS_CANTRCVMORE)
15307240Srh87107 		(void) strcat(buf, "CANTRCVMORE ");
15310Sstevel@tonic-gate 	if (state & SS_ISBOUND)
15327240Srh87107 		(void) strcat(buf, "ISBOUND ");
15330Sstevel@tonic-gate 	if (state & SS_NDELAY)
15347240Srh87107 		(void) strcat(buf, "NDELAY ");
15350Sstevel@tonic-gate 	if (state & SS_NONBLOCK)
15367240Srh87107 		(void) strcat(buf, "NONBLOCK ");
15370Sstevel@tonic-gate 
15380Sstevel@tonic-gate 	if (state & SS_ASYNC)
15397240Srh87107 		(void) strcat(buf, "ASYNC ");
15400Sstevel@tonic-gate 	if (state & SS_ACCEPTCONN)
15417240Srh87107 		(void) strcat(buf, "ACCEPTCONN ");
15420Sstevel@tonic-gate 	if (state & SS_SAVEDEOR)
15437240Srh87107 		(void) strcat(buf, "SAVEDEOR ");
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate 	if (state & SS_RCVATMARK)
15467240Srh87107 		(void) strcat(buf, "RCVATMARK ");
15470Sstevel@tonic-gate 	if (state & SS_OOBPEND)
15487240Srh87107 		(void) strcat(buf, "OOBPEND ");
15490Sstevel@tonic-gate 	if (state & SS_HAVEOOBDATA)
15507240Srh87107 		(void) strcat(buf, "HAVEOOBDATA ");
15510Sstevel@tonic-gate 	if (state & SS_HADOOBDATA)
15527240Srh87107 		(void) strcat(buf, "HADOOBDATA ");
15530Sstevel@tonic-gate 
15540Sstevel@tonic-gate 	if (mode & SM_PRIV)
15557240Srh87107 		(void) strcat(buf, "PRIV ");
15560Sstevel@tonic-gate 	if (mode & SM_ATOMIC)
15577240Srh87107 		(void) strcat(buf, "ATOMIC ");
15580Sstevel@tonic-gate 	if (mode & SM_ADDR)
15597240Srh87107 		(void) strcat(buf, "ADDR ");
15600Sstevel@tonic-gate 	if (mode & SM_CONNREQUIRED)
15617240Srh87107 		(void) strcat(buf, "CONNREQUIRED ");
15620Sstevel@tonic-gate 
15630Sstevel@tonic-gate 	if (mode & SM_FDPASSING)
15647240Srh87107 		(void) strcat(buf, "FDPASSING ");
15650Sstevel@tonic-gate 	if (mode & SM_EXDATA)
15667240Srh87107 		(void) strcat(buf, "EXDATA ");
15670Sstevel@tonic-gate 	if (mode & SM_OPTDATA)
15687240Srh87107 		(void) strcat(buf, "OPTDATA ");
15690Sstevel@tonic-gate 	if (mode & SM_BYTESTREAM)
15707240Srh87107 		(void) strcat(buf, "BYTESTREAM ");
15710Sstevel@tonic-gate 	return (buf);
15720Sstevel@tonic-gate }
15730Sstevel@tonic-gate 
15740Sstevel@tonic-gate char *
pr_addr(int family,struct sockaddr * addr,t_uscalar_t addrlen)15750Sstevel@tonic-gate pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen)
15760Sstevel@tonic-gate {
15770Sstevel@tonic-gate 	static char buf[1024];
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate 	if (addr == NULL || addrlen == 0) {
15807240Srh87107 		(void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr);
15810Sstevel@tonic-gate 		return (buf);
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 	switch (family) {
15840Sstevel@tonic-gate 	case AF_INET: {
15850Sstevel@tonic-gate 		struct sockaddr_in sin;
15860Sstevel@tonic-gate 
15870Sstevel@tonic-gate 		bcopy(addr, &sin, sizeof (sin));
15880Sstevel@tonic-gate 
15890Sstevel@tonic-gate 		(void) sprintf(buf, "(len %d) %x/%d",
15906712Stomee 		    addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port));
15910Sstevel@tonic-gate 		break;
15920Sstevel@tonic-gate 	}
15930Sstevel@tonic-gate 	case AF_INET6: {
15940Sstevel@tonic-gate 		struct sockaddr_in6 sin6;
15950Sstevel@tonic-gate 		uint16_t *piece = (uint16_t *)&sin6.sin6_addr;
15960Sstevel@tonic-gate 
15970Sstevel@tonic-gate 		bcopy((char *)addr, (char *)&sin6, sizeof (sin6));
15987240Srh87107 		(void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d",
15990Sstevel@tonic-gate 		    addrlen,
16000Sstevel@tonic-gate 		    ntohs(piece[0]), ntohs(piece[1]),
16010Sstevel@tonic-gate 		    ntohs(piece[2]), ntohs(piece[3]),
16020Sstevel@tonic-gate 		    ntohs(piece[4]), ntohs(piece[5]),
16030Sstevel@tonic-gate 		    ntohs(piece[6]), ntohs(piece[7]),
16040Sstevel@tonic-gate 		    ntohs(sin6.sin6_port));
16050Sstevel@tonic-gate 		break;
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate 	case AF_UNIX: {
16080Sstevel@tonic-gate 		struct sockaddr_un *soun = (struct sockaddr_un *)addr;
16090Sstevel@tonic-gate 
16106712Stomee 		(void) sprintf(buf, "(len %d) %s", addrlen,
16115753Sgww 		    (soun == NULL) ? "(none)" : soun->sun_path);
16120Sstevel@tonic-gate 		break;
16130Sstevel@tonic-gate 	}
16140Sstevel@tonic-gate 	default:
16150Sstevel@tonic-gate 		(void) sprintf(buf, "(unknown af %d)", family);
16160Sstevel@tonic-gate 		break;
16170Sstevel@tonic-gate 	}
16180Sstevel@tonic-gate 	return (buf);
16190Sstevel@tonic-gate }
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate /* The logical equivalence operator (a if-and-only-if b) */
162211474SJonathan.Adams@Sun.COM #define	EQUIVALENT(a, b)	(((a) && (b)) || (!(a) && (!(b))))
16230Sstevel@tonic-gate 
16240Sstevel@tonic-gate /*
16250Sstevel@tonic-gate  * Verify limitations and invariants on oob state.
16260Sstevel@tonic-gate  * Return 1 if OK, otherwise 0 so that it can be used as
16270Sstevel@tonic-gate  *	ASSERT(verify_oobstate(so));
16280Sstevel@tonic-gate  */
16290Sstevel@tonic-gate int
so_verify_oobstate(struct sonode * so)16300Sstevel@tonic-gate so_verify_oobstate(struct sonode *so)
16310Sstevel@tonic-gate {
16328348SEric.Yu@Sun.COM 	boolean_t havemark;
16338348SEric.Yu@Sun.COM 
16340Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&so->so_lock));
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate 	/*
16370Sstevel@tonic-gate 	 * The possible state combinations are:
16380Sstevel@tonic-gate 	 *	0
16390Sstevel@tonic-gate 	 *	SS_OOBPEND
16400Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HAVEOOBDATA
16410Sstevel@tonic-gate 	 *	SS_OOBPEND|SS_HADOOBDATA
16420Sstevel@tonic-gate 	 *	SS_HADOOBDATA
16430Sstevel@tonic-gate 	 */
16440Sstevel@tonic-gate 	switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) {
16450Sstevel@tonic-gate 	case 0:
16460Sstevel@tonic-gate 	case SS_OOBPEND:
16470Sstevel@tonic-gate 	case SS_OOBPEND|SS_HAVEOOBDATA:
16480Sstevel@tonic-gate 	case SS_OOBPEND|SS_HADOOBDATA:
16490Sstevel@tonic-gate 	case SS_HADOOBDATA:
16500Sstevel@tonic-gate 		break;
16510Sstevel@tonic-gate 	default:
16528348SEric.Yu@Sun.COM 		printf("Bad oob state 1 (%p): state %s\n",
16538348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16540Sstevel@tonic-gate 		return (0);
16550Sstevel@tonic-gate 	}
16560Sstevel@tonic-gate 
16570Sstevel@tonic-gate 	/* SS_RCVATMARK should only be set when SS_OOBPEND is set */
16580Sstevel@tonic-gate 	if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) {
16598348SEric.Yu@Sun.COM 		printf("Bad oob state 2 (%p): state %s\n",
16608348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16610Sstevel@tonic-gate 		return (0);
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	/*
16658348SEric.Yu@Sun.COM 	 * (havemark != 0 or SS_RCVATMARK) iff SS_OOBPEND
16668348SEric.Yu@Sun.COM 	 * For TPI, the presence of a "mark" is indicated by sti_oobsigcnt.
16670Sstevel@tonic-gate 	 */
16688348SEric.Yu@Sun.COM 	havemark = (SOCK_IS_NONSTR(so)) ? so->so_oobmark > 0 :
16698348SEric.Yu@Sun.COM 	    SOTOTPI(so)->sti_oobsigcnt > 0;
16708348SEric.Yu@Sun.COM 
167111474SJonathan.Adams@Sun.COM 	if (!EQUIVALENT(havemark || (so->so_state & SS_RCVATMARK),
16725753Sgww 	    so->so_state & SS_OOBPEND)) {
16738348SEric.Yu@Sun.COM 		printf("Bad oob state 3 (%p): state %s\n",
16748348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16750Sstevel@tonic-gate 		return (0);
16760Sstevel@tonic-gate 	}
16770Sstevel@tonic-gate 
16780Sstevel@tonic-gate 	/*
16790Sstevel@tonic-gate 	 * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA
16800Sstevel@tonic-gate 	 */
16810Sstevel@tonic-gate 	if (!(so->so_options & SO_OOBINLINE) &&
168211474SJonathan.Adams@Sun.COM 	    !EQUIVALENT(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) {
16838348SEric.Yu@Sun.COM 		printf("Bad oob state 4 (%p): state %s\n",
16848348SEric.Yu@Sun.COM 		    (void *)so, pr_state(so->so_state, so->so_mode));
16850Sstevel@tonic-gate 		return (0);
16860Sstevel@tonic-gate 	}
16878348SEric.Yu@Sun.COM 
16888348SEric.Yu@Sun.COM 	if (!SOCK_IS_NONSTR(so) &&
16898348SEric.Yu@Sun.COM 	    SOTOTPI(so)->sti_oobsigcnt < SOTOTPI(so)->sti_oobcnt) {
16900Sstevel@tonic-gate 		printf("Bad oob state 5 (%p): counts %d/%d state %s\n",
16918348SEric.Yu@Sun.COM 		    (void *)so, SOTOTPI(so)->sti_oobsigcnt,
16928348SEric.Yu@Sun.COM 		    SOTOTPI(so)->sti_oobcnt,
16938348SEric.Yu@Sun.COM 		    pr_state(so->so_state, so->so_mode));
16940Sstevel@tonic-gate 		return (0);
16950Sstevel@tonic-gate 	}
16968348SEric.Yu@Sun.COM 
16970Sstevel@tonic-gate 	return (1);
16980Sstevel@tonic-gate }
169911474SJonathan.Adams@Sun.COM #undef	EQUIVALENT
17000Sstevel@tonic-gate #endif /* DEBUG */
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate /* initialize sockfs zone specific kstat related items			*/
17030Sstevel@tonic-gate void *
sock_kstat_init(zoneid_t zoneid)17040Sstevel@tonic-gate sock_kstat_init(zoneid_t zoneid)
17050Sstevel@tonic-gate {
17060Sstevel@tonic-gate 	kstat_t	*ksp;
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 	ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc",
17090Sstevel@tonic-gate 	    KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid);
17100Sstevel@tonic-gate 
17110Sstevel@tonic-gate 	if (ksp != NULL) {
17120Sstevel@tonic-gate 		ksp->ks_update = sockfs_update;
17130Sstevel@tonic-gate 		ksp->ks_snapshot = sockfs_snapshot;
17140Sstevel@tonic-gate 		ksp->ks_lock = &socklist.sl_lock;
17150Sstevel@tonic-gate 		ksp->ks_private = (void *)(uintptr_t)zoneid;
17160Sstevel@tonic-gate 		kstat_install(ksp);
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate 
17190Sstevel@tonic-gate 	return (ksp);
17200Sstevel@tonic-gate }
17210Sstevel@tonic-gate 
17220Sstevel@tonic-gate /* tear down sockfs zone specific kstat related items			*/
17230Sstevel@tonic-gate /*ARGSUSED*/
17240Sstevel@tonic-gate void
sock_kstat_fini(zoneid_t zoneid,void * arg)17250Sstevel@tonic-gate sock_kstat_fini(zoneid_t zoneid, void *arg)
17260Sstevel@tonic-gate {
17270Sstevel@tonic-gate 	kstat_t *ksp = (kstat_t *)arg;
17280Sstevel@tonic-gate 
17290Sstevel@tonic-gate 	if (ksp != NULL) {
17300Sstevel@tonic-gate 		ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private);
17310Sstevel@tonic-gate 		kstat_delete(ksp);
17320Sstevel@tonic-gate 	}
17330Sstevel@tonic-gate }
17340Sstevel@tonic-gate 
17350Sstevel@tonic-gate /*
17360Sstevel@tonic-gate  * Zones:
17370Sstevel@tonic-gate  * Note that nactive is going to be different for each zone.
17380Sstevel@tonic-gate  * This means we require kstat to call sockfs_update and then sockfs_snapshot
17390Sstevel@tonic-gate  * for the same zone, or sockfs_snapshot will be taken into the wrong size
17400Sstevel@tonic-gate  * buffer. This is safe, but if the buffer is too small, user will not be
17410Sstevel@tonic-gate  * given details of all sockets. However, as this kstat has a ks_lock, kstat
17420Sstevel@tonic-gate  * driver will keep it locked between the update and the snapshot, so no
17430Sstevel@tonic-gate  * other process (zone) can currently get inbetween resulting in a wrong size
17440Sstevel@tonic-gate  * buffer allocation.
17450Sstevel@tonic-gate  */
17460Sstevel@tonic-gate static int
sockfs_update(kstat_t * ksp,int rw)17470Sstevel@tonic-gate sockfs_update(kstat_t *ksp, int rw)
17480Sstevel@tonic-gate {
17490Sstevel@tonic-gate 	uint_t	nactive = 0;		/* # of active AF_UNIX sockets	*/
17500Sstevel@tonic-gate 	struct sonode	*so;		/* current sonode on socklist	*/
17510Sstevel@tonic-gate 	zoneid_t	myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
17520Sstevel@tonic-gate 
17530Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
17540Sstevel@tonic-gate 
17550Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
17560Sstevel@tonic-gate 		return (EACCES);
17570Sstevel@tonic-gate 	}
17580Sstevel@tonic-gate 
17598348SEric.Yu@Sun.COM 	for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
17608348SEric.Yu@Sun.COM 		if (so->so_count != 0 && so->so_zoneid == myzoneid) {
17610Sstevel@tonic-gate 			nactive++;
17620Sstevel@tonic-gate 		}
17630Sstevel@tonic-gate 	}
17640Sstevel@tonic-gate 	ksp->ks_ndata = nactive;
17650Sstevel@tonic-gate 	ksp->ks_data_size = nactive * sizeof (struct k_sockinfo);
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	return (0);
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate static int
sockfs_snapshot(kstat_t * ksp,void * buf,int rw)17710Sstevel@tonic-gate sockfs_snapshot(kstat_t *ksp, void *buf, int rw)
17720Sstevel@tonic-gate {
17730Sstevel@tonic-gate 	int			ns;	/* # of sonodes we've copied	*/
17740Sstevel@tonic-gate 	struct sonode		*so;	/* current sonode on socklist	*/
17750Sstevel@tonic-gate 	struct k_sockinfo	*pksi;	/* where we put sockinfo data	*/
17760Sstevel@tonic-gate 	t_uscalar_t		sn_len;	/* soa_len			*/
17770Sstevel@tonic-gate 	zoneid_t		myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private;
17788348SEric.Yu@Sun.COM 	sotpi_info_t 		*sti;
17790Sstevel@tonic-gate 
17800Sstevel@tonic-gate 	ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid());
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate 	ksp->ks_snaptime = gethrtime();
17830Sstevel@tonic-gate 
17840Sstevel@tonic-gate 	if (rw == KSTAT_WRITE) {	/* bounce all writes		*/
17850Sstevel@tonic-gate 		return (EACCES);
17860Sstevel@tonic-gate 	}
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate 	/*
17890Sstevel@tonic-gate 	 * for each sonode on the socklist, we massage the important
17900Sstevel@tonic-gate 	 * info into buf, in k_sockinfo format.
17910Sstevel@tonic-gate 	 */
17920Sstevel@tonic-gate 	pksi = (struct k_sockinfo *)buf;
17938348SEric.Yu@Sun.COM 	ns = 0;
17948348SEric.Yu@Sun.COM 	for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) {
17950Sstevel@tonic-gate 		/* only stuff active sonodes and the same zone:		*/
17968348SEric.Yu@Sun.COM 		if (so->so_count == 0 || so->so_zoneid != myzoneid) {
17970Sstevel@tonic-gate 			continue;
17980Sstevel@tonic-gate 		}
17990Sstevel@tonic-gate 
18000Sstevel@tonic-gate 		/*
18010Sstevel@tonic-gate 		 * If the sonode was activated between the update and the
18020Sstevel@tonic-gate 		 * snapshot, we're done - as this is only a snapshot.
18030Sstevel@tonic-gate 		 */
18040Sstevel@tonic-gate 		if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) {
18050Sstevel@tonic-gate 			break;
18060Sstevel@tonic-gate 		}
18070Sstevel@tonic-gate 
18088348SEric.Yu@Sun.COM 		sti = SOTOTPI(so);
18090Sstevel@tonic-gate 		/* copy important info into buf:			*/
18100Sstevel@tonic-gate 		pksi->ks_si.si_size = sizeof (struct k_sockinfo);
18110Sstevel@tonic-gate 		pksi->ks_si.si_family = so->so_family;
18120Sstevel@tonic-gate 		pksi->ks_si.si_type = so->so_type;
18130Sstevel@tonic-gate 		pksi->ks_si.si_flag = so->so_flag;
18140Sstevel@tonic-gate 		pksi->ks_si.si_state = so->so_state;
18158348SEric.Yu@Sun.COM 		pksi->ks_si.si_serv_type = sti->sti_serv_type;
18168348SEric.Yu@Sun.COM 		pksi->ks_si.si_ux_laddr_sou_magic =
18178348SEric.Yu@Sun.COM 		    sti->sti_ux_laddr.soua_magic;
18188348SEric.Yu@Sun.COM 		pksi->ks_si.si_ux_faddr_sou_magic =
18198348SEric.Yu@Sun.COM 		    sti->sti_ux_faddr.soua_magic;
18208348SEric.Yu@Sun.COM 		pksi->ks_si.si_laddr_soa_len = sti->sti_laddr.soa_len;
18218348SEric.Yu@Sun.COM 		pksi->ks_si.si_faddr_soa_len = sti->sti_faddr.soa_len;
18220Sstevel@tonic-gate 		pksi->ks_si.si_szoneid = so->so_zoneid;
18238348SEric.Yu@Sun.COM 		pksi->ks_si.si_faddr_noxlate = sti->sti_faddr_noxlate;
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 		mutex_enter(&so->so_lock);
18260Sstevel@tonic-gate 
18278348SEric.Yu@Sun.COM 		if (sti->sti_laddr_sa != NULL) {
18288348SEric.Yu@Sun.COM 			ASSERT(sti->sti_laddr_sa->sa_data != NULL);
18298348SEric.Yu@Sun.COM 			sn_len = sti->sti_laddr_len;
18300Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
18310Sstevel@tonic-gate 			    sizeof (pksi->ks_si.si_laddr_sun_path));
18320Sstevel@tonic-gate 
18330Sstevel@tonic-gate 			pksi->ks_si.si_laddr_family =
18348348SEric.Yu@Sun.COM 			    sti->sti_laddr_sa->sa_family;
18350Sstevel@tonic-gate 			if (sn_len != 0) {
18360Sstevel@tonic-gate 				/* AF_UNIX socket names are NULL terminated */
18370Sstevel@tonic-gate 				(void) strncpy(pksi->ks_si.si_laddr_sun_path,
18388348SEric.Yu@Sun.COM 				    sti->sti_laddr_sa->sa_data,
18390Sstevel@tonic-gate 				    sizeof (pksi->ks_si.si_laddr_sun_path));
18400Sstevel@tonic-gate 				sn_len = strlen(pksi->ks_si.si_laddr_sun_path);
18410Sstevel@tonic-gate 			}
18420Sstevel@tonic-gate 			pksi->ks_si.si_laddr_sun_path[sn_len] = 0;
18430Sstevel@tonic-gate 		}
18440Sstevel@tonic-gate 
18458348SEric.Yu@Sun.COM 		if (sti->sti_faddr_sa != NULL) {
18468348SEric.Yu@Sun.COM 			ASSERT(sti->sti_faddr_sa->sa_data != NULL);
18478348SEric.Yu@Sun.COM 			sn_len = sti->sti_faddr_len;
18480Sstevel@tonic-gate 			ASSERT(sn_len <= sizeof (short) +
18490Sstevel@tonic-gate 			    sizeof (pksi->ks_si.si_faddr_sun_path));
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate 			pksi->ks_si.si_faddr_family =
18528348SEric.Yu@Sun.COM 			    sti->sti_faddr_sa->sa_family;
18530Sstevel@tonic-gate 			if (sn_len != 0) {
18540Sstevel@tonic-gate 				(void) strncpy(pksi->ks_si.si_faddr_sun_path,
18558348SEric.Yu@Sun.COM 				    sti->sti_faddr_sa->sa_data,
18560Sstevel@tonic-gate 				    sizeof (pksi->ks_si.si_faddr_sun_path));
18570Sstevel@tonic-gate 				sn_len = strlen(pksi->ks_si.si_faddr_sun_path);
18580Sstevel@tonic-gate 			}
18590Sstevel@tonic-gate 			pksi->ks_si.si_faddr_sun_path[sn_len] = 0;
18600Sstevel@tonic-gate 		}
18610Sstevel@tonic-gate 
18620Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
18630Sstevel@tonic-gate 
18640Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[0], "%p", (void *)so);
18650Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[1], "%p",
18668348SEric.Yu@Sun.COM 		    (void *)sti->sti_ux_laddr.soua_vp);
18670Sstevel@tonic-gate 		(void) sprintf(pksi->ks_straddr[2], "%p",
18688348SEric.Yu@Sun.COM 		    (void *)sti->sti_ux_faddr.soua_vp);
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate 		ns++;
18710Sstevel@tonic-gate 		pksi++;
18720Sstevel@tonic-gate 	}
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	ksp->ks_ndata = ns;
18750Sstevel@tonic-gate 	return (0);
18760Sstevel@tonic-gate }
18770Sstevel@tonic-gate 
18780Sstevel@tonic-gate ssize_t
soreadfile(file_t * fp,uchar_t * buf,u_offset_t fileoff,int * err,size_t size)18790Sstevel@tonic-gate soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size)
18800Sstevel@tonic-gate {
18810Sstevel@tonic-gate 	struct uio auio;
18820Sstevel@tonic-gate 	struct iovec aiov[MSG_MAXIOVLEN];
18830Sstevel@tonic-gate 	register vnode_t *vp;
18840Sstevel@tonic-gate 	int ioflag, rwflag;
18850Sstevel@tonic-gate 	ssize_t cnt;
18860Sstevel@tonic-gate 	int error = 0;
18870Sstevel@tonic-gate 	int iovcnt = 0;
18880Sstevel@tonic-gate 	short fflag;
18890Sstevel@tonic-gate 
18900Sstevel@tonic-gate 	vp = fp->f_vnode;
18910Sstevel@tonic-gate 	fflag = fp->f_flag;
18920Sstevel@tonic-gate 
18930Sstevel@tonic-gate 	rwflag = 0;
18940Sstevel@tonic-gate 	aiov[0].iov_base = (caddr_t)buf;
18950Sstevel@tonic-gate 	aiov[0].iov_len = size;
18960Sstevel@tonic-gate 	iovcnt = 1;
18970Sstevel@tonic-gate 	cnt = (ssize_t)size;
18980Sstevel@tonic-gate 	(void) VOP_RWLOCK(vp, rwflag, NULL);
18990Sstevel@tonic-gate 
19000Sstevel@tonic-gate 	auio.uio_loffset = fileoff;
19010Sstevel@tonic-gate 	auio.uio_iov = aiov;
19020Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
19030Sstevel@tonic-gate 	auio.uio_resid = cnt;
19040Sstevel@tonic-gate 	auio.uio_segflg = UIO_SYSSPACE;
19050Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
19060Sstevel@tonic-gate 	auio.uio_fmode = fflag;
19070Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
19080Sstevel@tonic-gate 
19090Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC);
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
19120Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
19130Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
19140Sstevel@tonic-gate 	error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL);
19150Sstevel@tonic-gate 	cnt -= auio.uio_resid;
19160Sstevel@tonic-gate 
19170Sstevel@tonic-gate 	VOP_RWUNLOCK(vp, rwflag, NULL);
19180Sstevel@tonic-gate 
19190Sstevel@tonic-gate 	if (error == EINTR && cnt != 0)
19200Sstevel@tonic-gate 		error = 0;
19210Sstevel@tonic-gate out:
19220Sstevel@tonic-gate 	if (error != 0) {
19230Sstevel@tonic-gate 		*err = error;
19240Sstevel@tonic-gate 		return (0);
19250Sstevel@tonic-gate 	} else {
19260Sstevel@tonic-gate 		*err = 0;
19270Sstevel@tonic-gate 		return (cnt);
19280Sstevel@tonic-gate 	}
19290Sstevel@tonic-gate }
19308348SEric.Yu@Sun.COM 
19318348SEric.Yu@Sun.COM int
so_copyin(const void * from,void * to,size_t size,int fromkernel)19328348SEric.Yu@Sun.COM so_copyin(const void *from, void *to, size_t size, int fromkernel)
19338348SEric.Yu@Sun.COM {
19348348SEric.Yu@Sun.COM 	if (fromkernel) {
19358348SEric.Yu@Sun.COM 		bcopy(from, to, size);
19368348SEric.Yu@Sun.COM 		return (0);
19378348SEric.Yu@Sun.COM 	}
19388348SEric.Yu@Sun.COM 	return (xcopyin(from, to, size));
19398348SEric.Yu@Sun.COM }
19408348SEric.Yu@Sun.COM 
19418348SEric.Yu@Sun.COM int
so_copyout(const void * from,void * to,size_t size,int tokernel)19428348SEric.Yu@Sun.COM so_copyout(const void *from, void *to, size_t size, int tokernel)
19438348SEric.Yu@Sun.COM {
19448348SEric.Yu@Sun.COM 	if (tokernel) {
19458348SEric.Yu@Sun.COM 		bcopy(from, to, size);
19468348SEric.Yu@Sun.COM 		return (0);
19478348SEric.Yu@Sun.COM 	}
19488348SEric.Yu@Sun.COM 	return (xcopyout(from, to, size));
19498348SEric.Yu@Sun.COM }
1950