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 /* 233422Snh145002 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/types.h> 300Sstevel@tonic-gate #include <sys/t_lock.h> 310Sstevel@tonic-gate #include <sys/param.h> 320Sstevel@tonic-gate #include <sys/systm.h> 330Sstevel@tonic-gate #include <sys/buf.h> 340Sstevel@tonic-gate #include <sys/conf.h> 350Sstevel@tonic-gate #include <sys/cred.h> 360Sstevel@tonic-gate #include <sys/kmem.h> 370Sstevel@tonic-gate #include <sys/sysmacros.h> 380Sstevel@tonic-gate #include <sys/vfs.h> 39*3898Srsb #include <sys/vfs_opreg.h> 400Sstevel@tonic-gate #include <sys/vnode.h> 410Sstevel@tonic-gate #include <sys/debug.h> 420Sstevel@tonic-gate #include <sys/errno.h> 430Sstevel@tonic-gate #include <sys/time.h> 440Sstevel@tonic-gate #include <sys/file.h> 450Sstevel@tonic-gate #include <sys/open.h> 460Sstevel@tonic-gate #include <sys/user.h> 470Sstevel@tonic-gate #include <sys/termios.h> 480Sstevel@tonic-gate #include <sys/stream.h> 490Sstevel@tonic-gate #include <sys/strsubr.h> 500Sstevel@tonic-gate #include <sys/strsun.h> 510Sstevel@tonic-gate #include <sys/esunddi.h> 520Sstevel@tonic-gate #include <sys/flock.h> 530Sstevel@tonic-gate #include <sys/modctl.h> 540Sstevel@tonic-gate #include <sys/cmn_err.h> 550Sstevel@tonic-gate #include <sys/mkdev.h> 560Sstevel@tonic-gate #include <sys/pathname.h> 570Sstevel@tonic-gate #include <sys/ddi.h> 580Sstevel@tonic-gate #include <sys/stat.h> 590Sstevel@tonic-gate #include <sys/fs/snode.h> 600Sstevel@tonic-gate #include <sys/fs/dv_node.h> 610Sstevel@tonic-gate #include <sys/zone.h> 620Sstevel@tonic-gate 630Sstevel@tonic-gate #include <sys/socket.h> 640Sstevel@tonic-gate #include <sys/socketvar.h> 650Sstevel@tonic-gate #include <netinet/in.h> 660Sstevel@tonic-gate #include <sys/un.h> 670Sstevel@tonic-gate 680Sstevel@tonic-gate #include <sys/ucred.h> 690Sstevel@tonic-gate 700Sstevel@tonic-gate #include <sys/tiuser.h> 710Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 720Sstevel@tonic-gate #include <sys/tihdr.h> 730Sstevel@tonic-gate 740Sstevel@tonic-gate #include <c2/audit.h> 750Sstevel@tonic-gate 760Sstevel@tonic-gate #include <fs/sockfs/nl7c.h> 770Sstevel@tonic-gate 780Sstevel@tonic-gate /* 790Sstevel@tonic-gate * Macros that operate on struct cmsghdr. 800Sstevel@tonic-gate * The CMSG_VALID macro does not assume that the last option buffer is padded. 810Sstevel@tonic-gate */ 820Sstevel@tonic-gate #define CMSG_CONTENT(cmsg) (&((cmsg)[1])) 830Sstevel@tonic-gate #define CMSG_CONTENTLEN(cmsg) ((cmsg)->cmsg_len - sizeof (struct cmsghdr)) 840Sstevel@tonic-gate #define CMSG_VALID(cmsg, start, end) \ 850Sstevel@tonic-gate (ISALIGNED_cmsghdr(cmsg) && \ 860Sstevel@tonic-gate ((uintptr_t)(cmsg) >= (uintptr_t)(start)) && \ 870Sstevel@tonic-gate ((uintptr_t)(cmsg) < (uintptr_t)(end)) && \ 880Sstevel@tonic-gate ((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) && \ 890Sstevel@tonic-gate ((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end))) 900Sstevel@tonic-gate #define SO_LOCK_WAKEUP_TIME 3000 /* Wakeup time in milliseconds */ 910Sstevel@tonic-gate 920Sstevel@tonic-gate static struct kmem_cache *socktpi_cache, *socktpi_unix_cache; 930Sstevel@tonic-gate 940Sstevel@tonic-gate dev_t sockdev; /* For fsid in getattr */ 950Sstevel@tonic-gate 960Sstevel@tonic-gate struct sockparams *sphead; 970Sstevel@tonic-gate krwlock_t splist_lock; 980Sstevel@tonic-gate 990Sstevel@tonic-gate struct socklist socklist; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate static int sockfs_update(kstat_t *, int); 1020Sstevel@tonic-gate static int sockfs_snapshot(kstat_t *, void *, int); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate extern void sendfile_init(); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate extern void nl7c_init(void); 1070Sstevel@tonic-gate 1080Sstevel@tonic-gate #define ADRSTRLEN (2 * sizeof (void *) + 1) 1090Sstevel@tonic-gate /* 1100Sstevel@tonic-gate * kernel structure for passing the sockinfo data back up to the user. 1110Sstevel@tonic-gate * the strings array allows us to convert AF_UNIX addresses into strings 1120Sstevel@tonic-gate * with a common method regardless of which n-bit kernel we're running. 1130Sstevel@tonic-gate */ 1140Sstevel@tonic-gate struct k_sockinfo { 1150Sstevel@tonic-gate struct sockinfo ks_si; 1160Sstevel@tonic-gate char ks_straddr[3][ADRSTRLEN]; 1170Sstevel@tonic-gate }; 1180Sstevel@tonic-gate 1190Sstevel@tonic-gate /* 1200Sstevel@tonic-gate * Translate from a device pathname (e.g. "/dev/tcp") to a vnode. 1210Sstevel@tonic-gate * Returns with the vnode held. 1220Sstevel@tonic-gate */ 1230Sstevel@tonic-gate static int 1240Sstevel@tonic-gate sogetvp(char *devpath, vnode_t **vpp, int uioflag) 1250Sstevel@tonic-gate { 1260Sstevel@tonic-gate struct snode *csp; 1270Sstevel@tonic-gate vnode_t *vp, *dvp; 1280Sstevel@tonic-gate major_t maj; 1290Sstevel@tonic-gate int error; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE); 1320Sstevel@tonic-gate /* 1330Sstevel@tonic-gate * Lookup the underlying filesystem vnode. 1340Sstevel@tonic-gate */ 1350Sstevel@tonic-gate error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp); 1360Sstevel@tonic-gate if (error) 1370Sstevel@tonic-gate return (error); 1380Sstevel@tonic-gate 1390Sstevel@tonic-gate /* Check that it is the correct vnode */ 1400Sstevel@tonic-gate if (vp->v_type != VCHR) { 1410Sstevel@tonic-gate VN_RELE(vp); 1420Sstevel@tonic-gate return (ENOTSOCK); 1430Sstevel@tonic-gate } 1440Sstevel@tonic-gate 1450Sstevel@tonic-gate /* 1460Sstevel@tonic-gate * If devpath went through devfs, the device should already 1470Sstevel@tonic-gate * be configured. If devpath is a mknod file, however, we 1480Sstevel@tonic-gate * need to make sure the device is properly configured. 1490Sstevel@tonic-gate * To do this, we do something similar to spec_open() 1500Sstevel@tonic-gate * except that we resolve to the minor/leaf level since 1510Sstevel@tonic-gate * we need to return a vnode. 1520Sstevel@tonic-gate */ 1530Sstevel@tonic-gate csp = VTOS(VTOS(vp)->s_commonvp); 1540Sstevel@tonic-gate if (!(csp->s_flag & SDIPSET)) { 1550Sstevel@tonic-gate char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1560Sstevel@tonic-gate error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname); 1570Sstevel@tonic-gate if (error == 0) 1580Sstevel@tonic-gate error = devfs_lookupname(pathname, NULLVPP, &dvp); 1590Sstevel@tonic-gate VN_RELE(vp); 1600Sstevel@tonic-gate kmem_free(pathname, MAXPATHLEN); 1610Sstevel@tonic-gate if (error != 0) 1620Sstevel@tonic-gate return (ENXIO); 1630Sstevel@tonic-gate vp = dvp; /* use the devfs vp */ 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate 1660Sstevel@tonic-gate /* device is configured at this point */ 1670Sstevel@tonic-gate maj = getmajor(vp->v_rdev); 1680Sstevel@tonic-gate if (!STREAMSTAB(maj)) { 1690Sstevel@tonic-gate VN_RELE(vp); 1700Sstevel@tonic-gate return (ENOSTR); 1710Sstevel@tonic-gate } 1720Sstevel@tonic-gate 1730Sstevel@tonic-gate *vpp = vp; 1740Sstevel@tonic-gate return (0); 1750Sstevel@tonic-gate } 1760Sstevel@tonic-gate 1770Sstevel@tonic-gate /* 1780Sstevel@tonic-gate * Add or delete (latter if devpath is NULL) an enter to the sockparams 1790Sstevel@tonic-gate * table. If devpathlen is zero the devpath with not be kmem_freed. Otherwise 1800Sstevel@tonic-gate * this routine assumes that the caller has kmem_alloced devpath/devpathlen 1810Sstevel@tonic-gate * for this routine to consume. 1820Sstevel@tonic-gate * The zero devpathlen could be used if the kernel wants to create entries 1830Sstevel@tonic-gate * itself by calling sockconfig(1,2,3, "/dev/tcp", 0); 1840Sstevel@tonic-gate */ 1850Sstevel@tonic-gate int 1860Sstevel@tonic-gate soconfig(int domain, int type, int protocol, 1870Sstevel@tonic-gate char *devpath, int devpathlen) 1880Sstevel@tonic-gate { 1890Sstevel@tonic-gate struct sockparams **spp; 1900Sstevel@tonic-gate struct sockparams *sp; 1910Sstevel@tonic-gate int error = 0; 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate dprint(0, ("soconfig(%d,%d,%d,%s,%d)\n", 1940Sstevel@tonic-gate domain, type, protocol, devpath, devpathlen)); 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate /* 1970Sstevel@tonic-gate * Look for an existing match. 1980Sstevel@tonic-gate */ 1990Sstevel@tonic-gate rw_enter(&splist_lock, RW_WRITER); 2000Sstevel@tonic-gate for (spp = &sphead; (sp = *spp) != NULL; spp = &sp->sp_next) { 2010Sstevel@tonic-gate if (sp->sp_domain == domain && 2020Sstevel@tonic-gate sp->sp_type == type && 2030Sstevel@tonic-gate sp->sp_protocol == protocol) { 2040Sstevel@tonic-gate break; 2050Sstevel@tonic-gate } 2060Sstevel@tonic-gate } 2070Sstevel@tonic-gate if (devpath == NULL) { 2080Sstevel@tonic-gate ASSERT(devpathlen == 0); 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate /* Delete existing entry */ 2110Sstevel@tonic-gate if (sp == NULL) { 2120Sstevel@tonic-gate error = ENXIO; 2130Sstevel@tonic-gate goto done; 2140Sstevel@tonic-gate } 2150Sstevel@tonic-gate /* Unlink and free existing entry */ 2160Sstevel@tonic-gate *spp = sp->sp_next; 2170Sstevel@tonic-gate ASSERT(sp->sp_vnode); 2180Sstevel@tonic-gate VN_RELE(sp->sp_vnode); 2190Sstevel@tonic-gate if (sp->sp_devpathlen != 0) 2200Sstevel@tonic-gate kmem_free(sp->sp_devpath, sp->sp_devpathlen); 2210Sstevel@tonic-gate kmem_free(sp, sizeof (*sp)); 2220Sstevel@tonic-gate } else { 2230Sstevel@tonic-gate vnode_t *vp; 2240Sstevel@tonic-gate 2250Sstevel@tonic-gate /* Add new entry */ 2260Sstevel@tonic-gate if (sp != NULL) { 2270Sstevel@tonic-gate error = EEXIST; 2280Sstevel@tonic-gate goto done; 2290Sstevel@tonic-gate } 2300Sstevel@tonic-gate 2310Sstevel@tonic-gate error = sogetvp(devpath, &vp, UIO_SYSSPACE); 2320Sstevel@tonic-gate if (error) { 2330Sstevel@tonic-gate dprint(0, ("soconfig: vp %s failed with %d\n", 2340Sstevel@tonic-gate devpath, error)); 2350Sstevel@tonic-gate goto done; 2360Sstevel@tonic-gate } 2370Sstevel@tonic-gate 2380Sstevel@tonic-gate dprint(0, ("soconfig: %s => vp %p, dev 0x%lx\n", 2390Sstevel@tonic-gate devpath, vp, vp->v_rdev)); 2400Sstevel@tonic-gate 2410Sstevel@tonic-gate sp = kmem_alloc(sizeof (*sp), KM_SLEEP); 2420Sstevel@tonic-gate sp->sp_domain = domain; 2430Sstevel@tonic-gate sp->sp_type = type; 2440Sstevel@tonic-gate sp->sp_protocol = protocol; 2450Sstevel@tonic-gate sp->sp_devpath = devpath; 2460Sstevel@tonic-gate sp->sp_devpathlen = devpathlen; 2470Sstevel@tonic-gate sp->sp_vnode = vp; 2480Sstevel@tonic-gate sp->sp_next = NULL; 2490Sstevel@tonic-gate *spp = sp; 2500Sstevel@tonic-gate } 2510Sstevel@tonic-gate done: 2520Sstevel@tonic-gate rw_exit(&splist_lock); 2530Sstevel@tonic-gate if (error) { 2540Sstevel@tonic-gate if (devpath != NULL) 2550Sstevel@tonic-gate kmem_free(devpath, devpathlen); 2560Sstevel@tonic-gate #ifdef SOCK_DEBUG 2570Sstevel@tonic-gate eprintline(error); 2580Sstevel@tonic-gate #endif /* SOCK_DEBUG */ 2590Sstevel@tonic-gate } 2600Sstevel@tonic-gate return (error); 2610Sstevel@tonic-gate } 2620Sstevel@tonic-gate 2630Sstevel@tonic-gate /* 2640Sstevel@tonic-gate * Lookup an entry in the sockparams list based on the triple. 2650Sstevel@tonic-gate * If no entry is found and devpath is not NULL translate devpath to a 2660Sstevel@tonic-gate * vnode. Note that devpath is a pointer to a user address! 2670Sstevel@tonic-gate * Returns with the vnode held. 2680Sstevel@tonic-gate * 2690Sstevel@tonic-gate * When this routine uses devpath it does not create an entry in the sockparams 2700Sstevel@tonic-gate * list since this routine can run on behalf of any user and one user 2710Sstevel@tonic-gate * should not be able to effect the transport used by another user. 2720Sstevel@tonic-gate * 2730Sstevel@tonic-gate * In order to return the correct error this routine has to do wildcard scans 2740Sstevel@tonic-gate * of the list. The errors are (in decreasing precedence): 2750Sstevel@tonic-gate * EAFNOSUPPORT - address family not in list 2760Sstevel@tonic-gate * EPROTONOSUPPORT - address family supported but not protocol. 2770Sstevel@tonic-gate * EPROTOTYPE - address family and protocol supported but not socket type. 2780Sstevel@tonic-gate */ 2790Sstevel@tonic-gate vnode_t * 2800Sstevel@tonic-gate solookup(int domain, int type, int protocol, char *devpath, int *errorp) 2810Sstevel@tonic-gate { 2820Sstevel@tonic-gate struct sockparams *sp; 2830Sstevel@tonic-gate int error; 2840Sstevel@tonic-gate vnode_t *vp; 2850Sstevel@tonic-gate 2860Sstevel@tonic-gate rw_enter(&splist_lock, RW_READER); 2870Sstevel@tonic-gate for (sp = sphead; sp != NULL; sp = sp->sp_next) { 2880Sstevel@tonic-gate if (sp->sp_domain == domain && 2890Sstevel@tonic-gate sp->sp_type == type && 2900Sstevel@tonic-gate sp->sp_protocol == protocol) { 2910Sstevel@tonic-gate break; 2920Sstevel@tonic-gate } 2930Sstevel@tonic-gate } 2940Sstevel@tonic-gate if (sp == NULL) { 2950Sstevel@tonic-gate dprint(0, ("solookup(%d,%d,%d) not found\n", 2960Sstevel@tonic-gate domain, type, protocol)); 2970Sstevel@tonic-gate if (devpath == NULL) { 2980Sstevel@tonic-gate /* Determine correct error code */ 2990Sstevel@tonic-gate int found = 0; 3000Sstevel@tonic-gate 3010Sstevel@tonic-gate for (sp = sphead; sp != NULL; sp = sp->sp_next) { 3020Sstevel@tonic-gate if (sp->sp_domain == domain && found < 1) 3030Sstevel@tonic-gate found = 1; 3040Sstevel@tonic-gate if (sp->sp_domain == domain && 3050Sstevel@tonic-gate sp->sp_protocol == protocol && found < 2) 3060Sstevel@tonic-gate found = 2; 3070Sstevel@tonic-gate } 3080Sstevel@tonic-gate rw_exit(&splist_lock); 3090Sstevel@tonic-gate switch (found) { 3100Sstevel@tonic-gate case 0: 3110Sstevel@tonic-gate *errorp = EAFNOSUPPORT; 3120Sstevel@tonic-gate break; 3130Sstevel@tonic-gate case 1: 3140Sstevel@tonic-gate *errorp = EPROTONOSUPPORT; 3150Sstevel@tonic-gate break; 3160Sstevel@tonic-gate case 2: 3170Sstevel@tonic-gate *errorp = EPROTOTYPE; 3180Sstevel@tonic-gate break; 3190Sstevel@tonic-gate } 3200Sstevel@tonic-gate return (NULL); 3210Sstevel@tonic-gate } 3220Sstevel@tonic-gate rw_exit(&splist_lock); 3230Sstevel@tonic-gate 3240Sstevel@tonic-gate /* 3250Sstevel@tonic-gate * Return vp based on devpath. 3260Sstevel@tonic-gate * Do not enter into table to avoid random users 3270Sstevel@tonic-gate * modifying the sockparams list. 3280Sstevel@tonic-gate */ 3290Sstevel@tonic-gate error = sogetvp(devpath, &vp, UIO_USERSPACE); 3300Sstevel@tonic-gate if (error) { 3310Sstevel@tonic-gate dprint(0, ("solookup: vp %p failed with %d\n", 3320Sstevel@tonic-gate devpath, error)); 3330Sstevel@tonic-gate *errorp = EPROTONOSUPPORT; 3340Sstevel@tonic-gate return (NULL); 3350Sstevel@tonic-gate } 3360Sstevel@tonic-gate dprint(0, ("solookup: %p => vp %p, dev 0x%lx\n", 3370Sstevel@tonic-gate devpath, vp, vp->v_rdev)); 3380Sstevel@tonic-gate 3390Sstevel@tonic-gate return (vp); 3400Sstevel@tonic-gate } 3410Sstevel@tonic-gate dprint(0, ("solookup(%d,%d,%d) vp %p devpath %s\n", 3420Sstevel@tonic-gate domain, type, protocol, sp->sp_vnode, sp->sp_devpath)); 3430Sstevel@tonic-gate 3440Sstevel@tonic-gate vp = sp->sp_vnode; 3450Sstevel@tonic-gate VN_HOLD(vp); 3460Sstevel@tonic-gate rw_exit(&splist_lock); 3470Sstevel@tonic-gate return (vp); 3480Sstevel@tonic-gate } 3490Sstevel@tonic-gate 3500Sstevel@tonic-gate /* 3510Sstevel@tonic-gate * Return a socket vnode. 3520Sstevel@tonic-gate * 3530Sstevel@tonic-gate * Assumes that the caller is "passing" an VN_HOLD for accessvp i.e. 3540Sstevel@tonic-gate * when the socket is freed a VN_RELE will take place. 3550Sstevel@tonic-gate * 3560Sstevel@tonic-gate * Note that sockets assume that the driver will clone (either itself 3570Sstevel@tonic-gate * or by using the clone driver) i.e. a socket() call will always 3580Sstevel@tonic-gate * result in a new vnode being created. 3590Sstevel@tonic-gate */ 3600Sstevel@tonic-gate struct vnode * 3610Sstevel@tonic-gate makesockvp(struct vnode *accessvp, int domain, int type, int protocol) 3620Sstevel@tonic-gate { 3630Sstevel@tonic-gate kmem_cache_t *cp; 3640Sstevel@tonic-gate struct sonode *so; 3650Sstevel@tonic-gate struct vnode *vp; 3660Sstevel@tonic-gate time_t now; 3670Sstevel@tonic-gate dev_t dev; 3680Sstevel@tonic-gate 3690Sstevel@tonic-gate cp = (domain == AF_UNIX) ? socktpi_unix_cache : socktpi_cache; 3700Sstevel@tonic-gate so = kmem_cache_alloc(cp, KM_SLEEP); 3710Sstevel@tonic-gate so->so_cache = cp; 3720Sstevel@tonic-gate so->so_obj = so; 3730Sstevel@tonic-gate vp = SOTOV(so); 3740Sstevel@tonic-gate now = gethrestime_sec(); 3750Sstevel@tonic-gate 3760Sstevel@tonic-gate so->so_flag = 0; 3770Sstevel@tonic-gate ASSERT(so->so_accessvp == NULL); 3780Sstevel@tonic-gate so->so_accessvp = accessvp; 3790Sstevel@tonic-gate dev = accessvp->v_rdev; 3800Sstevel@tonic-gate 3810Sstevel@tonic-gate /* 3820Sstevel@tonic-gate * Record in so_flag that it is a clone. 3830Sstevel@tonic-gate */ 3840Sstevel@tonic-gate if (getmajor(dev) == clone_major) { 3850Sstevel@tonic-gate so->so_flag |= SOCLONE; 3860Sstevel@tonic-gate } 3870Sstevel@tonic-gate so->so_dev = dev; 3880Sstevel@tonic-gate 3890Sstevel@tonic-gate so->so_state = 0; 3900Sstevel@tonic-gate so->so_mode = 0; 3910Sstevel@tonic-gate 3920Sstevel@tonic-gate so->so_fsid = sockdev; 3930Sstevel@tonic-gate so->so_atime = now; 3940Sstevel@tonic-gate so->so_mtime = now; 3950Sstevel@tonic-gate so->so_ctime = now; /* Never modified */ 3960Sstevel@tonic-gate so->so_count = 0; 3970Sstevel@tonic-gate 3980Sstevel@tonic-gate so->so_family = (short)domain; 3990Sstevel@tonic-gate so->so_type = (short)type; 4000Sstevel@tonic-gate so->so_protocol = (short)protocol; 4010Sstevel@tonic-gate so->so_pushcnt = 0; 4020Sstevel@tonic-gate 4030Sstevel@tonic-gate so->so_options = 0; 4040Sstevel@tonic-gate so->so_linger.l_onoff = 0; 4050Sstevel@tonic-gate so->so_linger.l_linger = 0; 4060Sstevel@tonic-gate so->so_sndbuf = 0; 4070Sstevel@tonic-gate so->so_rcvbuf = 0; 4080Sstevel@tonic-gate so->so_sndlowat = 0; 4090Sstevel@tonic-gate so->so_rcvlowat = 0; 4100Sstevel@tonic-gate #ifdef notyet 4110Sstevel@tonic-gate so->so_sndtimeo = 0; 4120Sstevel@tonic-gate so->so_rcvtimeo = 0; 4130Sstevel@tonic-gate #endif /* notyet */ 4140Sstevel@tonic-gate so->so_error = 0; 4150Sstevel@tonic-gate so->so_delayed_error = 0; 4160Sstevel@tonic-gate 4170Sstevel@tonic-gate ASSERT(so->so_oobmsg == NULL); 4180Sstevel@tonic-gate so->so_oobcnt = 0; 4190Sstevel@tonic-gate so->so_oobsigcnt = 0; 4200Sstevel@tonic-gate so->so_pgrp = 0; 4210Sstevel@tonic-gate so->so_provinfo = NULL; 4220Sstevel@tonic-gate 4230Sstevel@tonic-gate ASSERT(so->so_laddr_sa == NULL && so->so_faddr_sa == NULL); 4240Sstevel@tonic-gate so->so_laddr_len = so->so_faddr_len = 0; 4250Sstevel@tonic-gate so->so_laddr_maxlen = so->so_faddr_maxlen = 0; 4260Sstevel@tonic-gate so->so_eaddr_mp = NULL; 4270Sstevel@tonic-gate so->so_priv = NULL; 4280Sstevel@tonic-gate 4290Sstevel@tonic-gate so->so_peercred = NULL; 4300Sstevel@tonic-gate 4310Sstevel@tonic-gate ASSERT(so->so_ack_mp == NULL); 4320Sstevel@tonic-gate ASSERT(so->so_conn_ind_head == NULL); 4330Sstevel@tonic-gate ASSERT(so->so_conn_ind_tail == NULL); 4340Sstevel@tonic-gate ASSERT(so->so_ux_bound_vp == NULL); 4350Sstevel@tonic-gate ASSERT(so->so_unbind_mp == NULL); 4360Sstevel@tonic-gate 4370Sstevel@tonic-gate vn_reinit(vp); 4380Sstevel@tonic-gate vp->v_vfsp = rootvfs; 4390Sstevel@tonic-gate vp->v_type = VSOCK; 4400Sstevel@tonic-gate vp->v_rdev = so->so_dev; 4410Sstevel@tonic-gate vn_exists(vp); 4420Sstevel@tonic-gate 4430Sstevel@tonic-gate return (vp); 4440Sstevel@tonic-gate } 4450Sstevel@tonic-gate 4460Sstevel@tonic-gate void 4470Sstevel@tonic-gate sockfree(struct sonode *so) 4480Sstevel@tonic-gate { 4490Sstevel@tonic-gate mblk_t *mp; 4500Sstevel@tonic-gate vnode_t *vp; 4510Sstevel@tonic-gate 4520Sstevel@tonic-gate ASSERT(so->so_count == 0); 4530Sstevel@tonic-gate ASSERT(so->so_accessvp); 4540Sstevel@tonic-gate ASSERT(so->so_discon_ind_mp == NULL); 4550Sstevel@tonic-gate 4560Sstevel@tonic-gate vp = so->so_accessvp; 4570Sstevel@tonic-gate VN_RELE(vp); 4580Sstevel@tonic-gate 4590Sstevel@tonic-gate /* 4600Sstevel@tonic-gate * Protect so->so_[lf]addr_sa so that sockfs_snapshot() can safely 4610Sstevel@tonic-gate * indirect them. It also uses so_accessvp as a validity test. 4620Sstevel@tonic-gate */ 4630Sstevel@tonic-gate mutex_enter(&so->so_lock); 4640Sstevel@tonic-gate 4650Sstevel@tonic-gate so->so_accessvp = NULL; 4660Sstevel@tonic-gate 4670Sstevel@tonic-gate if (so->so_laddr_sa) { 4680Sstevel@tonic-gate ASSERT((caddr_t)so->so_faddr_sa == 4690Sstevel@tonic-gate (caddr_t)so->so_laddr_sa + so->so_laddr_maxlen); 4700Sstevel@tonic-gate ASSERT(so->so_faddr_maxlen == so->so_laddr_maxlen); 4710Sstevel@tonic-gate so->so_state &= ~(SS_LADDR_VALID | SS_FADDR_VALID); 4720Sstevel@tonic-gate kmem_free(so->so_laddr_sa, so->so_laddr_maxlen * 2); 4730Sstevel@tonic-gate so->so_laddr_sa = NULL; 4740Sstevel@tonic-gate so->so_laddr_len = so->so_laddr_maxlen = 0; 4750Sstevel@tonic-gate so->so_faddr_sa = NULL; 4760Sstevel@tonic-gate so->so_faddr_len = so->so_faddr_maxlen = 0; 4770Sstevel@tonic-gate } 4780Sstevel@tonic-gate 4790Sstevel@tonic-gate mutex_exit(&so->so_lock); 4800Sstevel@tonic-gate 4810Sstevel@tonic-gate if ((mp = so->so_eaddr_mp) != NULL) { 4820Sstevel@tonic-gate freemsg(mp); 4830Sstevel@tonic-gate so->so_eaddr_mp = NULL; 4840Sstevel@tonic-gate so->so_delayed_error = 0; 4850Sstevel@tonic-gate } 4860Sstevel@tonic-gate if ((mp = so->so_ack_mp) != NULL) { 4870Sstevel@tonic-gate freemsg(mp); 4880Sstevel@tonic-gate so->so_ack_mp = NULL; 4890Sstevel@tonic-gate } 4900Sstevel@tonic-gate if ((mp = so->so_conn_ind_head) != NULL) { 4910Sstevel@tonic-gate mblk_t *mp1; 4920Sstevel@tonic-gate 4930Sstevel@tonic-gate while (mp) { 4940Sstevel@tonic-gate mp1 = mp->b_next; 4950Sstevel@tonic-gate mp->b_next = NULL; 4960Sstevel@tonic-gate freemsg(mp); 4970Sstevel@tonic-gate mp = mp1; 4980Sstevel@tonic-gate } 4990Sstevel@tonic-gate so->so_conn_ind_head = so->so_conn_ind_tail = NULL; 5000Sstevel@tonic-gate so->so_state &= ~SS_HASCONNIND; 5010Sstevel@tonic-gate } 5020Sstevel@tonic-gate #ifdef DEBUG 5030Sstevel@tonic-gate mutex_enter(&so->so_lock); 5040Sstevel@tonic-gate ASSERT(so_verify_oobstate(so)); 5050Sstevel@tonic-gate mutex_exit(&so->so_lock); 5060Sstevel@tonic-gate #endif /* DEBUG */ 5070Sstevel@tonic-gate if ((mp = so->so_oobmsg) != NULL) { 5080Sstevel@tonic-gate freemsg(mp); 5090Sstevel@tonic-gate so->so_oobmsg = NULL; 5100Sstevel@tonic-gate so->so_state &= ~(SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA); 5110Sstevel@tonic-gate } 5120Sstevel@tonic-gate 5130Sstevel@tonic-gate if ((mp = so->so_nl7c_rcv_mp) != NULL) { 5140Sstevel@tonic-gate so->so_nl7c_rcv_mp = NULL; 5150Sstevel@tonic-gate freemsg(mp); 5160Sstevel@tonic-gate } 5170Sstevel@tonic-gate so->so_nl7c_rcv_rval = 0; 5180Sstevel@tonic-gate if (so->so_nl7c_uri != NULL) { 5190Sstevel@tonic-gate nl7c_urifree(so); 5201974Sbrutus /* urifree() cleared nl7c_uri */ 5210Sstevel@tonic-gate } 5221974Sbrutus if (so->so_nl7c_flags) { 5231974Sbrutus so->so_nl7c_flags = 0; 5241974Sbrutus } 5250Sstevel@tonic-gate 5260Sstevel@tonic-gate ASSERT(so->so_ux_bound_vp == NULL); 5270Sstevel@tonic-gate if ((mp = so->so_unbind_mp) != NULL) { 5280Sstevel@tonic-gate freemsg(mp); 5290Sstevel@tonic-gate so->so_unbind_mp = NULL; 5300Sstevel@tonic-gate } 5310Sstevel@tonic-gate vn_invalid(SOTOV(so)); 5320Sstevel@tonic-gate 5330Sstevel@tonic-gate if (so->so_peercred != NULL) 5340Sstevel@tonic-gate crfree(so->so_peercred); 5350Sstevel@tonic-gate 5360Sstevel@tonic-gate kmem_cache_free(so->so_cache, so->so_obj); 5370Sstevel@tonic-gate } 5380Sstevel@tonic-gate 5390Sstevel@tonic-gate /* 5400Sstevel@tonic-gate * Update the accessed, updated, or changed times in an sonode 5410Sstevel@tonic-gate * with the current time. 5420Sstevel@tonic-gate * 5430Sstevel@tonic-gate * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable 5440Sstevel@tonic-gate * attributes in a fstat call. (They return the current time and 0 for 5450Sstevel@tonic-gate * all timestamps, respectively.) We maintain the current timestamps 5460Sstevel@tonic-gate * here primarily so that should sockmod be popped the resulting 5470Sstevel@tonic-gate * file descriptor will behave like a stream w.r.t. the timestamps. 5480Sstevel@tonic-gate */ 5490Sstevel@tonic-gate void 5500Sstevel@tonic-gate so_update_attrs(struct sonode *so, int flag) 5510Sstevel@tonic-gate { 5520Sstevel@tonic-gate time_t now = gethrestime_sec(); 5530Sstevel@tonic-gate 5540Sstevel@tonic-gate mutex_enter(&so->so_lock); 5550Sstevel@tonic-gate so->so_flag |= flag; 5560Sstevel@tonic-gate if (flag & SOACC) 5570Sstevel@tonic-gate so->so_atime = now; 5580Sstevel@tonic-gate if (flag & SOMOD) 5590Sstevel@tonic-gate so->so_mtime = now; 5600Sstevel@tonic-gate mutex_exit(&so->so_lock); 5610Sstevel@tonic-gate } 5620Sstevel@tonic-gate 5630Sstevel@tonic-gate /*ARGSUSED*/ 5640Sstevel@tonic-gate static int 5650Sstevel@tonic-gate socktpi_constructor(void *buf, void *cdrarg, int kmflags) 5660Sstevel@tonic-gate { 5670Sstevel@tonic-gate struct sonode *so = buf; 5680Sstevel@tonic-gate struct vnode *vp; 5690Sstevel@tonic-gate 5700Sstevel@tonic-gate so->so_nl7c_flags = 0; 5710Sstevel@tonic-gate so->so_nl7c_uri = NULL; 5720Sstevel@tonic-gate so->so_nl7c_rcv_mp = NULL; 5730Sstevel@tonic-gate 5740Sstevel@tonic-gate so->so_oobmsg = NULL; 5750Sstevel@tonic-gate so->so_ack_mp = NULL; 5760Sstevel@tonic-gate so->so_conn_ind_head = NULL; 5770Sstevel@tonic-gate so->so_conn_ind_tail = NULL; 5780Sstevel@tonic-gate so->so_discon_ind_mp = NULL; 5790Sstevel@tonic-gate so->so_ux_bound_vp = NULL; 5800Sstevel@tonic-gate so->so_unbind_mp = NULL; 5810Sstevel@tonic-gate so->so_accessvp = NULL; 5820Sstevel@tonic-gate so->so_laddr_sa = NULL; 5830Sstevel@tonic-gate so->so_faddr_sa = NULL; 5840Sstevel@tonic-gate so->so_ops = &sotpi_sonodeops; 5850Sstevel@tonic-gate 5860Sstevel@tonic-gate vp = vn_alloc(KM_SLEEP); 5870Sstevel@tonic-gate so->so_vnode = vp; 5880Sstevel@tonic-gate 5890Sstevel@tonic-gate vn_setops(vp, socktpi_vnodeops); 5900Sstevel@tonic-gate vp->v_data = (caddr_t)so; 5910Sstevel@tonic-gate 5920Sstevel@tonic-gate mutex_init(&so->so_lock, NULL, MUTEX_DEFAULT, NULL); 5930Sstevel@tonic-gate mutex_init(&so->so_plumb_lock, NULL, MUTEX_DEFAULT, NULL); 5940Sstevel@tonic-gate cv_init(&so->so_state_cv, NULL, CV_DEFAULT, NULL); 5950Sstevel@tonic-gate cv_init(&so->so_ack_cv, NULL, CV_DEFAULT, NULL); 5960Sstevel@tonic-gate cv_init(&so->so_connind_cv, NULL, CV_DEFAULT, NULL); 5970Sstevel@tonic-gate cv_init(&so->so_want_cv, NULL, CV_DEFAULT, NULL); 5980Sstevel@tonic-gate 5990Sstevel@tonic-gate return (0); 6000Sstevel@tonic-gate } 6010Sstevel@tonic-gate 6020Sstevel@tonic-gate /*ARGSUSED1*/ 6030Sstevel@tonic-gate static void 6040Sstevel@tonic-gate socktpi_destructor(void *buf, void *cdrarg) 6050Sstevel@tonic-gate { 6060Sstevel@tonic-gate struct sonode *so = buf; 6070Sstevel@tonic-gate struct vnode *vp = SOTOV(so); 6080Sstevel@tonic-gate 6090Sstevel@tonic-gate ASSERT(so->so_nl7c_flags == 0); 6100Sstevel@tonic-gate ASSERT(so->so_nl7c_uri == NULL); 6110Sstevel@tonic-gate ASSERT(so->so_nl7c_rcv_mp == NULL); 6120Sstevel@tonic-gate 6130Sstevel@tonic-gate ASSERT(so->so_oobmsg == NULL); 6140Sstevel@tonic-gate ASSERT(so->so_ack_mp == NULL); 6150Sstevel@tonic-gate ASSERT(so->so_conn_ind_head == NULL); 6160Sstevel@tonic-gate ASSERT(so->so_conn_ind_tail == NULL); 6170Sstevel@tonic-gate ASSERT(so->so_discon_ind_mp == NULL); 6180Sstevel@tonic-gate ASSERT(so->so_ux_bound_vp == NULL); 6190Sstevel@tonic-gate ASSERT(so->so_unbind_mp == NULL); 6200Sstevel@tonic-gate ASSERT(so->so_ops == &sotpi_sonodeops); 6210Sstevel@tonic-gate 6220Sstevel@tonic-gate ASSERT(vn_matchops(vp, socktpi_vnodeops)); 6230Sstevel@tonic-gate ASSERT(vp->v_data == (caddr_t)so); 6240Sstevel@tonic-gate 6250Sstevel@tonic-gate vn_free(vp); 6260Sstevel@tonic-gate 6270Sstevel@tonic-gate mutex_destroy(&so->so_lock); 6280Sstevel@tonic-gate mutex_destroy(&so->so_plumb_lock); 6290Sstevel@tonic-gate cv_destroy(&so->so_state_cv); 6300Sstevel@tonic-gate cv_destroy(&so->so_ack_cv); 6310Sstevel@tonic-gate cv_destroy(&so->so_connind_cv); 6320Sstevel@tonic-gate cv_destroy(&so->so_want_cv); 6330Sstevel@tonic-gate } 6340Sstevel@tonic-gate 6350Sstevel@tonic-gate static int 6360Sstevel@tonic-gate socktpi_unix_constructor(void *buf, void *cdrarg, int kmflags) 6370Sstevel@tonic-gate { 6380Sstevel@tonic-gate int retval; 6390Sstevel@tonic-gate 6400Sstevel@tonic-gate if ((retval = socktpi_constructor(buf, cdrarg, kmflags)) == 0) { 6410Sstevel@tonic-gate struct sonode *so = (struct sonode *)buf; 6420Sstevel@tonic-gate 6430Sstevel@tonic-gate mutex_enter(&socklist.sl_lock); 6440Sstevel@tonic-gate 6450Sstevel@tonic-gate so->so_next = socklist.sl_list; 6460Sstevel@tonic-gate so->so_prev = NULL; 6470Sstevel@tonic-gate if (so->so_next != NULL) 6480Sstevel@tonic-gate so->so_next->so_prev = so; 6490Sstevel@tonic-gate socklist.sl_list = so; 6500Sstevel@tonic-gate 6510Sstevel@tonic-gate mutex_exit(&socklist.sl_lock); 6520Sstevel@tonic-gate 6530Sstevel@tonic-gate } 6540Sstevel@tonic-gate return (retval); 6550Sstevel@tonic-gate } 6560Sstevel@tonic-gate 6570Sstevel@tonic-gate static void 6580Sstevel@tonic-gate socktpi_unix_destructor(void *buf, void *cdrarg) 6590Sstevel@tonic-gate { 6600Sstevel@tonic-gate struct sonode *so = (struct sonode *)buf; 6610Sstevel@tonic-gate 6620Sstevel@tonic-gate mutex_enter(&socklist.sl_lock); 6630Sstevel@tonic-gate 6640Sstevel@tonic-gate if (so->so_next != NULL) 6650Sstevel@tonic-gate so->so_next->so_prev = so->so_prev; 6660Sstevel@tonic-gate if (so->so_prev != NULL) 6670Sstevel@tonic-gate so->so_prev->so_next = so->so_next; 6680Sstevel@tonic-gate else 6690Sstevel@tonic-gate socklist.sl_list = so->so_next; 6700Sstevel@tonic-gate 6710Sstevel@tonic-gate mutex_exit(&socklist.sl_lock); 6720Sstevel@tonic-gate 6730Sstevel@tonic-gate socktpi_destructor(buf, cdrarg); 6740Sstevel@tonic-gate } 6750Sstevel@tonic-gate 6760Sstevel@tonic-gate /* 6770Sstevel@tonic-gate * Init function called when sockfs is loaded. 6780Sstevel@tonic-gate */ 6790Sstevel@tonic-gate int 6800Sstevel@tonic-gate sockinit(int fstype, char *name) 6810Sstevel@tonic-gate { 6820Sstevel@tonic-gate static const fs_operation_def_t sock_vfsops_template[] = { 6830Sstevel@tonic-gate NULL, NULL 6840Sstevel@tonic-gate }; 6850Sstevel@tonic-gate int error; 6860Sstevel@tonic-gate major_t dev; 6870Sstevel@tonic-gate char *err_str; 6880Sstevel@tonic-gate 6890Sstevel@tonic-gate error = vfs_setfsops(fstype, sock_vfsops_template, NULL); 6900Sstevel@tonic-gate if (error != 0) { 6911548Srshoaib zcmn_err(GLOBAL_ZONEID, CE_WARN, 6921548Srshoaib "sockinit: bad vfs ops template"); 6930Sstevel@tonic-gate return (error); 6940Sstevel@tonic-gate } 6950Sstevel@tonic-gate 6960Sstevel@tonic-gate error = vn_make_ops(name, socktpi_vnodeops_template, &socktpi_vnodeops); 6970Sstevel@tonic-gate if (error != 0) { 6980Sstevel@tonic-gate err_str = "sockinit: bad sock vnode ops template"; 6990Sstevel@tonic-gate /* vn_make_ops() does not reset socktpi_vnodeops on failure. */ 7000Sstevel@tonic-gate socktpi_vnodeops = NULL; 7010Sstevel@tonic-gate goto failure; 7020Sstevel@tonic-gate } 7030Sstevel@tonic-gate 7040Sstevel@tonic-gate error = sosctp_init(); 7050Sstevel@tonic-gate if (error != 0) { 7060Sstevel@tonic-gate err_str = NULL; 7070Sstevel@tonic-gate goto failure; 7080Sstevel@tonic-gate } 7090Sstevel@tonic-gate 7103422Snh145002 error = sosdp_init(); 7113422Snh145002 if (error != 0) { 7123422Snh145002 err_str = NULL; 7133422Snh145002 goto failure; 7143422Snh145002 } 7153422Snh145002 7160Sstevel@tonic-gate /* 7170Sstevel@tonic-gate * Create sonode caches. We create a special one for AF_UNIX so 7180Sstevel@tonic-gate * that we can track them for netstat(1m). 7190Sstevel@tonic-gate */ 7200Sstevel@tonic-gate socktpi_cache = kmem_cache_create("socktpi_cache", 7210Sstevel@tonic-gate sizeof (struct sonode), 0, socktpi_constructor, 7220Sstevel@tonic-gate socktpi_destructor, NULL, NULL, NULL, 0); 7230Sstevel@tonic-gate 7240Sstevel@tonic-gate socktpi_unix_cache = kmem_cache_create("socktpi_unix_cache", 7250Sstevel@tonic-gate sizeof (struct sonode), 0, socktpi_unix_constructor, 7260Sstevel@tonic-gate socktpi_unix_destructor, NULL, NULL, NULL, 0); 7270Sstevel@tonic-gate 7280Sstevel@tonic-gate /* 7290Sstevel@tonic-gate * Build initial list mapping socket parameters to vnode. 7300Sstevel@tonic-gate */ 7310Sstevel@tonic-gate rw_init(&splist_lock, NULL, RW_DEFAULT, NULL); 7320Sstevel@tonic-gate 7330Sstevel@tonic-gate /* 7340Sstevel@tonic-gate * If sockets are needed before init runs /sbin/soconfig 7350Sstevel@tonic-gate * it is possible to preload the sockparams list here using 7360Sstevel@tonic-gate * calls like: 7370Sstevel@tonic-gate * sockconfig(1,2,3, "/dev/tcp", 0); 7380Sstevel@tonic-gate */ 7390Sstevel@tonic-gate 7400Sstevel@tonic-gate /* 7410Sstevel@tonic-gate * Create a unique dev_t for use in so_fsid. 7420Sstevel@tonic-gate */ 7430Sstevel@tonic-gate 7440Sstevel@tonic-gate if ((dev = getudev()) == (major_t)-1) 7450Sstevel@tonic-gate dev = 0; 7460Sstevel@tonic-gate sockdev = makedevice(dev, 0); 7470Sstevel@tonic-gate 7480Sstevel@tonic-gate mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL); 7490Sstevel@tonic-gate sendfile_init(); 7500Sstevel@tonic-gate nl7c_init(); 7510Sstevel@tonic-gate 7520Sstevel@tonic-gate return (0); 7530Sstevel@tonic-gate 7540Sstevel@tonic-gate failure: 7550Sstevel@tonic-gate (void) vfs_freevfsops_by_type(fstype); 7560Sstevel@tonic-gate if (socktpi_vnodeops != NULL) 7570Sstevel@tonic-gate vn_freevnodeops(socktpi_vnodeops); 7580Sstevel@tonic-gate if (err_str != NULL) 7591548Srshoaib zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str); 7600Sstevel@tonic-gate return (error); 7610Sstevel@tonic-gate } 7620Sstevel@tonic-gate 7630Sstevel@tonic-gate /* 7640Sstevel@tonic-gate * Caller must hold the mutex. Used to set SOLOCKED. 7650Sstevel@tonic-gate */ 7660Sstevel@tonic-gate void 7670Sstevel@tonic-gate so_lock_single(struct sonode *so) 7680Sstevel@tonic-gate { 7690Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 7700Sstevel@tonic-gate 7710Sstevel@tonic-gate while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) { 7720Sstevel@tonic-gate so->so_flag |= SOWANT; 7730Sstevel@tonic-gate cv_wait_stop(&so->so_want_cv, &so->so_lock, 7740Sstevel@tonic-gate SO_LOCK_WAKEUP_TIME); 7750Sstevel@tonic-gate } 7760Sstevel@tonic-gate so->so_flag |= SOLOCKED; 7770Sstevel@tonic-gate } 7780Sstevel@tonic-gate 7790Sstevel@tonic-gate /* 7800Sstevel@tonic-gate * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND. 7810Sstevel@tonic-gate * Used to clear SOLOCKED or SOASYNC_UNBIND. 7820Sstevel@tonic-gate */ 7830Sstevel@tonic-gate void 7840Sstevel@tonic-gate so_unlock_single(struct sonode *so, int flag) 7850Sstevel@tonic-gate { 7860Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 7870Sstevel@tonic-gate ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND)); 7880Sstevel@tonic-gate ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0); 7890Sstevel@tonic-gate ASSERT(so->so_flag & flag); 7900Sstevel@tonic-gate 7910Sstevel@tonic-gate /* 7920Sstevel@tonic-gate * Process the T_DISCON_IND on so_discon_ind_mp. 7930Sstevel@tonic-gate * 7940Sstevel@tonic-gate * Call to so_drain_discon_ind will result in so_lock 7950Sstevel@tonic-gate * being dropped and re-acquired later. 7960Sstevel@tonic-gate */ 7970Sstevel@tonic-gate if (so->so_discon_ind_mp != NULL) 7980Sstevel@tonic-gate so_drain_discon_ind(so); 7990Sstevel@tonic-gate 8000Sstevel@tonic-gate if (so->so_flag & SOWANT) 8010Sstevel@tonic-gate cv_broadcast(&so->so_want_cv); 8020Sstevel@tonic-gate so->so_flag &= ~(SOWANT|flag); 8030Sstevel@tonic-gate } 8040Sstevel@tonic-gate 8050Sstevel@tonic-gate /* 8060Sstevel@tonic-gate * Caller must hold the mutex. Used to set SOREADLOCKED. 8070Sstevel@tonic-gate * If the caller wants nonblocking behavior it should set fmode. 8080Sstevel@tonic-gate */ 8090Sstevel@tonic-gate int 8100Sstevel@tonic-gate so_lock_read(struct sonode *so, int fmode) 8110Sstevel@tonic-gate { 8120Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 8130Sstevel@tonic-gate 8140Sstevel@tonic-gate while (so->so_flag & SOREADLOCKED) { 8150Sstevel@tonic-gate if (fmode & (FNDELAY|FNONBLOCK)) 8160Sstevel@tonic-gate return (EWOULDBLOCK); 8170Sstevel@tonic-gate so->so_flag |= SOWANT; 8180Sstevel@tonic-gate cv_wait_stop(&so->so_want_cv, &so->so_lock, 8190Sstevel@tonic-gate SO_LOCK_WAKEUP_TIME); 8200Sstevel@tonic-gate } 8210Sstevel@tonic-gate so->so_flag |= SOREADLOCKED; 8220Sstevel@tonic-gate return (0); 8230Sstevel@tonic-gate } 8240Sstevel@tonic-gate 8250Sstevel@tonic-gate /* 8260Sstevel@tonic-gate * Like so_lock_read above but allows signals. 8270Sstevel@tonic-gate */ 8280Sstevel@tonic-gate int 8290Sstevel@tonic-gate so_lock_read_intr(struct sonode *so, int fmode) 8300Sstevel@tonic-gate { 8310Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 8320Sstevel@tonic-gate 8330Sstevel@tonic-gate while (so->so_flag & SOREADLOCKED) { 8340Sstevel@tonic-gate if (fmode & (FNDELAY|FNONBLOCK)) 8350Sstevel@tonic-gate return (EWOULDBLOCK); 8360Sstevel@tonic-gate so->so_flag |= SOWANT; 8370Sstevel@tonic-gate if (!cv_wait_sig(&so->so_want_cv, &so->so_lock)) 8380Sstevel@tonic-gate return (EINTR); 8390Sstevel@tonic-gate } 8400Sstevel@tonic-gate so->so_flag |= SOREADLOCKED; 8410Sstevel@tonic-gate return (0); 8420Sstevel@tonic-gate } 8430Sstevel@tonic-gate 8440Sstevel@tonic-gate /* 8450Sstevel@tonic-gate * Caller must hold the mutex. Used to clear SOREADLOCKED, 8460Sstevel@tonic-gate * set in so_lock_read() or so_lock_read_intr(). 8470Sstevel@tonic-gate */ 8480Sstevel@tonic-gate void 8490Sstevel@tonic-gate so_unlock_read(struct sonode *so) 8500Sstevel@tonic-gate { 8510Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 8520Sstevel@tonic-gate ASSERT(so->so_flag & SOREADLOCKED); 8530Sstevel@tonic-gate 8540Sstevel@tonic-gate if (so->so_flag & SOWANT) 8550Sstevel@tonic-gate cv_broadcast(&so->so_want_cv); 8560Sstevel@tonic-gate so->so_flag &= ~(SOWANT|SOREADLOCKED); 8570Sstevel@tonic-gate } 8580Sstevel@tonic-gate 8590Sstevel@tonic-gate /* 8600Sstevel@tonic-gate * Verify that the specified offset falls within the mblk and 8610Sstevel@tonic-gate * that the resulting pointer is aligned. 8620Sstevel@tonic-gate * Returns NULL if not. 8630Sstevel@tonic-gate */ 8640Sstevel@tonic-gate void * 8650Sstevel@tonic-gate sogetoff(mblk_t *mp, t_uscalar_t offset, 8660Sstevel@tonic-gate t_uscalar_t length, uint_t align_size) 8670Sstevel@tonic-gate { 8680Sstevel@tonic-gate uintptr_t ptr1, ptr2; 8690Sstevel@tonic-gate 8700Sstevel@tonic-gate ASSERT(mp && mp->b_wptr >= mp->b_rptr); 8710Sstevel@tonic-gate ptr1 = (uintptr_t)mp->b_rptr + offset; 8720Sstevel@tonic-gate ptr2 = (uintptr_t)ptr1 + length; 8730Sstevel@tonic-gate if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) { 8740Sstevel@tonic-gate eprintline(0); 8750Sstevel@tonic-gate return (NULL); 8760Sstevel@tonic-gate } 8770Sstevel@tonic-gate if ((ptr1 & (align_size - 1)) != 0) { 8780Sstevel@tonic-gate eprintline(0); 8790Sstevel@tonic-gate return (NULL); 8800Sstevel@tonic-gate } 8810Sstevel@tonic-gate return ((void *)ptr1); 8820Sstevel@tonic-gate } 8830Sstevel@tonic-gate 8840Sstevel@tonic-gate /* 8850Sstevel@tonic-gate * Return the AF_UNIX underlying filesystem vnode matching a given name. 8860Sstevel@tonic-gate * Makes sure the sending and the destination sonodes are compatible. 8870Sstevel@tonic-gate * The vnode is returned held. 8880Sstevel@tonic-gate * 8890Sstevel@tonic-gate * The underlying filesystem VSOCK vnode has a v_stream pointer that 8900Sstevel@tonic-gate * references the actual stream head (hence indirectly the actual sonode). 8910Sstevel@tonic-gate */ 8920Sstevel@tonic-gate static int 8930Sstevel@tonic-gate so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess, 8940Sstevel@tonic-gate vnode_t **vpp) 8950Sstevel@tonic-gate { 8960Sstevel@tonic-gate vnode_t *vp; /* Underlying filesystem vnode */ 8970Sstevel@tonic-gate vnode_t *svp; /* sockfs vnode */ 8980Sstevel@tonic-gate struct sonode *so2; 8990Sstevel@tonic-gate int error; 9000Sstevel@tonic-gate 9010Sstevel@tonic-gate dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", 9020Sstevel@tonic-gate so, soun->sun_path)); 9030Sstevel@tonic-gate 9040Sstevel@tonic-gate error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 9050Sstevel@tonic-gate if (error) { 9060Sstevel@tonic-gate eprintsoline(so, error); 9070Sstevel@tonic-gate return (error); 9080Sstevel@tonic-gate } 9090Sstevel@tonic-gate if (vp->v_type != VSOCK) { 9100Sstevel@tonic-gate error = ENOTSOCK; 9110Sstevel@tonic-gate eprintsoline(so, error); 9120Sstevel@tonic-gate goto done2; 9130Sstevel@tonic-gate } 9140Sstevel@tonic-gate 9150Sstevel@tonic-gate if (checkaccess) { 9160Sstevel@tonic-gate /* 9170Sstevel@tonic-gate * Check that we have permissions to access the destination 9180Sstevel@tonic-gate * vnode. This check is not done in BSD but it is required 9190Sstevel@tonic-gate * by X/Open. 9200Sstevel@tonic-gate */ 9210Sstevel@tonic-gate if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED())) { 9220Sstevel@tonic-gate eprintsoline(so, error); 9230Sstevel@tonic-gate goto done2; 9240Sstevel@tonic-gate } 9250Sstevel@tonic-gate } 9260Sstevel@tonic-gate 9270Sstevel@tonic-gate /* 9280Sstevel@tonic-gate * Check if the remote socket has been closed. 9290Sstevel@tonic-gate * 9300Sstevel@tonic-gate * Synchronize with vn_rele_stream by holding v_lock while traversing 9310Sstevel@tonic-gate * v_stream->sd_vnode. 9320Sstevel@tonic-gate */ 9330Sstevel@tonic-gate mutex_enter(&vp->v_lock); 9340Sstevel@tonic-gate if (vp->v_stream == NULL) { 9350Sstevel@tonic-gate mutex_exit(&vp->v_lock); 9360Sstevel@tonic-gate if (so->so_type == SOCK_DGRAM) 9370Sstevel@tonic-gate error = EDESTADDRREQ; 9380Sstevel@tonic-gate else 9390Sstevel@tonic-gate error = ECONNREFUSED; 9400Sstevel@tonic-gate 9410Sstevel@tonic-gate eprintsoline(so, error); 9420Sstevel@tonic-gate goto done2; 9430Sstevel@tonic-gate } 9440Sstevel@tonic-gate ASSERT(vp->v_stream->sd_vnode); 9450Sstevel@tonic-gate svp = vp->v_stream->sd_vnode; 9460Sstevel@tonic-gate /* 9470Sstevel@tonic-gate * holding v_lock on underlying filesystem vnode and acquiring 9480Sstevel@tonic-gate * it on sockfs vnode. Assumes that no code ever attempts to 9490Sstevel@tonic-gate * acquire these locks in the reverse order. 9500Sstevel@tonic-gate */ 9510Sstevel@tonic-gate VN_HOLD(svp); 9520Sstevel@tonic-gate mutex_exit(&vp->v_lock); 9530Sstevel@tonic-gate 9540Sstevel@tonic-gate if (svp->v_type != VSOCK) { 9550Sstevel@tonic-gate error = ENOTSOCK; 9560Sstevel@tonic-gate eprintsoline(so, error); 9570Sstevel@tonic-gate goto done; 9580Sstevel@tonic-gate } 9590Sstevel@tonic-gate 9600Sstevel@tonic-gate so2 = VTOSO(svp); 9610Sstevel@tonic-gate 9620Sstevel@tonic-gate if (so->so_type != so2->so_type) { 9630Sstevel@tonic-gate error = EPROTOTYPE; 9640Sstevel@tonic-gate eprintsoline(so, error); 9650Sstevel@tonic-gate goto done; 9660Sstevel@tonic-gate } 9670Sstevel@tonic-gate 9680Sstevel@tonic-gate VN_RELE(svp); 9690Sstevel@tonic-gate *vpp = vp; 9700Sstevel@tonic-gate return (0); 9710Sstevel@tonic-gate 9720Sstevel@tonic-gate done: 9730Sstevel@tonic-gate VN_RELE(svp); 9740Sstevel@tonic-gate done2: 9750Sstevel@tonic-gate VN_RELE(vp); 9760Sstevel@tonic-gate return (error); 9770Sstevel@tonic-gate } 9780Sstevel@tonic-gate 9790Sstevel@tonic-gate /* 9800Sstevel@tonic-gate * Verify peer address for connect and sendto/sendmsg. 9810Sstevel@tonic-gate * Since sendto/sendmsg would not get synchronous errors from the transport 9820Sstevel@tonic-gate * provider we have to do these ugly checks in the socket layer to 9830Sstevel@tonic-gate * preserve compatibility with SunOS 4.X. 9840Sstevel@tonic-gate */ 9850Sstevel@tonic-gate int 9860Sstevel@tonic-gate so_addr_verify(struct sonode *so, const struct sockaddr *name, 9870Sstevel@tonic-gate socklen_t namelen) 9880Sstevel@tonic-gate { 9890Sstevel@tonic-gate int family; 9900Sstevel@tonic-gate 9910Sstevel@tonic-gate dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n", so, name, namelen)); 9920Sstevel@tonic-gate 9930Sstevel@tonic-gate ASSERT(name != NULL); 9940Sstevel@tonic-gate 9950Sstevel@tonic-gate family = so->so_family; 9960Sstevel@tonic-gate switch (family) { 9970Sstevel@tonic-gate case AF_INET: 9980Sstevel@tonic-gate if (name->sa_family != family) { 9990Sstevel@tonic-gate eprintsoline(so, EAFNOSUPPORT); 10000Sstevel@tonic-gate return (EAFNOSUPPORT); 10010Sstevel@tonic-gate } 10020Sstevel@tonic-gate if (namelen != (socklen_t)sizeof (struct sockaddr_in)) { 10030Sstevel@tonic-gate eprintsoline(so, EINVAL); 10040Sstevel@tonic-gate return (EINVAL); 10050Sstevel@tonic-gate } 10060Sstevel@tonic-gate break; 10070Sstevel@tonic-gate case AF_INET6: { 10080Sstevel@tonic-gate #ifdef DEBUG 10090Sstevel@tonic-gate struct sockaddr_in6 *sin6; 10100Sstevel@tonic-gate #endif /* DEBUG */ 10110Sstevel@tonic-gate 10120Sstevel@tonic-gate if (name->sa_family != family) { 10130Sstevel@tonic-gate eprintsoline(so, EAFNOSUPPORT); 10140Sstevel@tonic-gate return (EAFNOSUPPORT); 10150Sstevel@tonic-gate } 10160Sstevel@tonic-gate if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) { 10170Sstevel@tonic-gate eprintsoline(so, EINVAL); 10180Sstevel@tonic-gate return (EINVAL); 10190Sstevel@tonic-gate } 10200Sstevel@tonic-gate #ifdef DEBUG 10210Sstevel@tonic-gate /* Verify that apps don't forget to clear sin6_scope_id etc */ 10220Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)name; 10230Sstevel@tonic-gate if (sin6->sin6_scope_id != 0 && 10240Sstevel@tonic-gate !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 10251548Srshoaib zcmn_err(getzoneid(), CE_WARN, 10260Sstevel@tonic-gate "connect/send* with uninitialized sin6_scope_id " 10270Sstevel@tonic-gate "(%d) on socket. Pid = %d\n", 10280Sstevel@tonic-gate (int)sin6->sin6_scope_id, (int)curproc->p_pid); 10290Sstevel@tonic-gate } 10300Sstevel@tonic-gate #endif /* DEBUG */ 10310Sstevel@tonic-gate break; 10320Sstevel@tonic-gate } 10330Sstevel@tonic-gate case AF_UNIX: 10340Sstevel@tonic-gate if (so->so_state & SS_FADDR_NOXLATE) { 10350Sstevel@tonic-gate return (0); 10360Sstevel@tonic-gate } 10370Sstevel@tonic-gate if (namelen < (socklen_t)sizeof (short)) { 10380Sstevel@tonic-gate eprintsoline(so, ENOENT); 10390Sstevel@tonic-gate return (ENOENT); 10400Sstevel@tonic-gate } 10410Sstevel@tonic-gate if (name->sa_family != family) { 10420Sstevel@tonic-gate eprintsoline(so, EAFNOSUPPORT); 10430Sstevel@tonic-gate return (EAFNOSUPPORT); 10440Sstevel@tonic-gate } 10450Sstevel@tonic-gate /* MAXPATHLEN + soun_family + nul termination */ 10460Sstevel@tonic-gate if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) { 10470Sstevel@tonic-gate eprintsoline(so, ENAMETOOLONG); 10480Sstevel@tonic-gate return (ENAMETOOLONG); 10490Sstevel@tonic-gate } 10500Sstevel@tonic-gate 10510Sstevel@tonic-gate break; 10520Sstevel@tonic-gate 10530Sstevel@tonic-gate default: 10540Sstevel@tonic-gate /* 10550Sstevel@tonic-gate * Default is don't do any length or sa_family check 10560Sstevel@tonic-gate * to allow non-sockaddr style addresses. 10570Sstevel@tonic-gate */ 10580Sstevel@tonic-gate break; 10590Sstevel@tonic-gate } 10600Sstevel@tonic-gate 10610Sstevel@tonic-gate return (0); 10620Sstevel@tonic-gate } 10630Sstevel@tonic-gate 10640Sstevel@tonic-gate 10650Sstevel@tonic-gate /* 10660Sstevel@tonic-gate * Translate an AF_UNIX sockaddr_un to the transport internal name. 10670Sstevel@tonic-gate * Assumes caller has called so_addr_verify first. 10680Sstevel@tonic-gate */ 10690Sstevel@tonic-gate /*ARGSUSED*/ 10700Sstevel@tonic-gate int 10710Sstevel@tonic-gate so_ux_addr_xlate(struct sonode *so, struct sockaddr *name, 10720Sstevel@tonic-gate socklen_t namelen, int checkaccess, 10730Sstevel@tonic-gate void **addrp, socklen_t *addrlenp) 10740Sstevel@tonic-gate { 10750Sstevel@tonic-gate int error; 10760Sstevel@tonic-gate struct sockaddr_un *soun; 10770Sstevel@tonic-gate vnode_t *vp; 10780Sstevel@tonic-gate void *addr; 10790Sstevel@tonic-gate socklen_t addrlen; 10800Sstevel@tonic-gate 10810Sstevel@tonic-gate dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n", 10820Sstevel@tonic-gate so, name, namelen, checkaccess)); 10830Sstevel@tonic-gate 10840Sstevel@tonic-gate ASSERT(name != NULL); 10850Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 10860Sstevel@tonic-gate ASSERT(!(so->so_state & SS_FADDR_NOXLATE)); 10870Sstevel@tonic-gate ASSERT(namelen >= (socklen_t)sizeof (short)); 10880Sstevel@tonic-gate ASSERT(name->sa_family == AF_UNIX); 10890Sstevel@tonic-gate soun = (struct sockaddr_un *)name; 10900Sstevel@tonic-gate /* 10910Sstevel@tonic-gate * Lookup vnode for the specified path name and verify that 10920Sstevel@tonic-gate * it is a socket. 10930Sstevel@tonic-gate */ 10940Sstevel@tonic-gate error = so_ux_lookup(so, soun, checkaccess, &vp); 10950Sstevel@tonic-gate if (error) { 10960Sstevel@tonic-gate eprintsoline(so, error); 10970Sstevel@tonic-gate return (error); 10980Sstevel@tonic-gate } 10990Sstevel@tonic-gate /* 11000Sstevel@tonic-gate * Use the address of the peer vnode as the address to send 11010Sstevel@tonic-gate * to. We release the peer vnode here. In case it has been 11020Sstevel@tonic-gate * closed by the time the T_CONN_REQ or T_UNIDATA_REQ reaches the 11030Sstevel@tonic-gate * transport the message will get an error or be dropped. 11040Sstevel@tonic-gate */ 11050Sstevel@tonic-gate so->so_ux_faddr.soua_vp = vp; 11060Sstevel@tonic-gate so->so_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT; 11070Sstevel@tonic-gate addr = &so->so_ux_faddr; 11080Sstevel@tonic-gate addrlen = (socklen_t)sizeof (so->so_ux_faddr); 11090Sstevel@tonic-gate dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n", 11100Sstevel@tonic-gate addrlen, vp)); 11110Sstevel@tonic-gate VN_RELE(vp); 11120Sstevel@tonic-gate *addrp = addr; 11130Sstevel@tonic-gate *addrlenp = (socklen_t)addrlen; 11140Sstevel@tonic-gate return (0); 11150Sstevel@tonic-gate } 11160Sstevel@tonic-gate 11170Sstevel@tonic-gate /* 11180Sstevel@tonic-gate * Esballoc free function for messages that contain SO_FILEP option. 11190Sstevel@tonic-gate * Decrement the reference count on the file pointers using closef. 11200Sstevel@tonic-gate */ 11210Sstevel@tonic-gate void 11220Sstevel@tonic-gate fdbuf_free(struct fdbuf *fdbuf) 11230Sstevel@tonic-gate { 11240Sstevel@tonic-gate int i; 11250Sstevel@tonic-gate struct file *fp; 11260Sstevel@tonic-gate 11270Sstevel@tonic-gate dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd)); 11280Sstevel@tonic-gate for (i = 0; i < fdbuf->fd_numfd; i++) { 11290Sstevel@tonic-gate /* 11300Sstevel@tonic-gate * We need pointer size alignment for fd_fds. On a LP64 11310Sstevel@tonic-gate * kernel, the required alignment is 8 bytes while 11320Sstevel@tonic-gate * the option headers and values are only 4 bytes 11330Sstevel@tonic-gate * aligned. So its safer to do a bcopy compared to 11340Sstevel@tonic-gate * assigning fdbuf->fd_fds[i] to fp. 11350Sstevel@tonic-gate */ 11360Sstevel@tonic-gate bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp)); 11370Sstevel@tonic-gate dprint(1, ("fdbuf_free: [%d] = %p\n", i, fp)); 11380Sstevel@tonic-gate (void) closef(fp); 11390Sstevel@tonic-gate } 11400Sstevel@tonic-gate if (fdbuf->fd_ebuf != NULL) 11410Sstevel@tonic-gate kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen); 11420Sstevel@tonic-gate kmem_free(fdbuf, fdbuf->fd_size); 11430Sstevel@tonic-gate } 11440Sstevel@tonic-gate 11450Sstevel@tonic-gate /* 1146455Smeem * Allocate an esballoc'ed message for AF_UNIX file descriptor passing. 1147455Smeem * Waits if memory is not available. 11480Sstevel@tonic-gate */ 11490Sstevel@tonic-gate mblk_t * 11500Sstevel@tonic-gate fdbuf_allocmsg(int size, struct fdbuf *fdbuf) 11510Sstevel@tonic-gate { 1152455Smeem uchar_t *buf; 11530Sstevel@tonic-gate mblk_t *mp; 11540Sstevel@tonic-gate 11550Sstevel@tonic-gate dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd)); 11560Sstevel@tonic-gate buf = kmem_alloc(size, KM_SLEEP); 11570Sstevel@tonic-gate fdbuf->fd_ebuf = (caddr_t)buf; 11580Sstevel@tonic-gate fdbuf->fd_ebuflen = size; 11590Sstevel@tonic-gate fdbuf->fd_frtn.free_func = fdbuf_free; 11600Sstevel@tonic-gate fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf; 11610Sstevel@tonic-gate 1162455Smeem mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn); 11630Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO; 11640Sstevel@tonic-gate return (mp); 11650Sstevel@tonic-gate } 11660Sstevel@tonic-gate 11670Sstevel@tonic-gate /* 11680Sstevel@tonic-gate * Extract file descriptors from a fdbuf. 11690Sstevel@tonic-gate * Return list in rights/rightslen. 11700Sstevel@tonic-gate */ 11710Sstevel@tonic-gate /*ARGSUSED*/ 11720Sstevel@tonic-gate static int 11730Sstevel@tonic-gate fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen) 11740Sstevel@tonic-gate { 11750Sstevel@tonic-gate int i, fd; 11760Sstevel@tonic-gate int *rp; 11770Sstevel@tonic-gate struct file *fp; 11780Sstevel@tonic-gate int numfd; 11790Sstevel@tonic-gate 11800Sstevel@tonic-gate dprint(1, ("fdbuf_extract: %d fds, len %d\n", 11810Sstevel@tonic-gate fdbuf->fd_numfd, rightslen)); 11820Sstevel@tonic-gate 11830Sstevel@tonic-gate numfd = fdbuf->fd_numfd; 11840Sstevel@tonic-gate ASSERT(rightslen == numfd * (int)sizeof (int)); 11850Sstevel@tonic-gate 11860Sstevel@tonic-gate /* 11870Sstevel@tonic-gate * Allocate a file descriptor and increment the f_count. 11880Sstevel@tonic-gate * The latter is needed since we always call fdbuf_free 11890Sstevel@tonic-gate * which performs a closef. 11900Sstevel@tonic-gate */ 11910Sstevel@tonic-gate rp = (int *)rights; 11920Sstevel@tonic-gate for (i = 0; i < numfd; i++) { 11930Sstevel@tonic-gate if ((fd = ufalloc(0)) == -1) 11940Sstevel@tonic-gate goto cleanup; 11950Sstevel@tonic-gate /* 11960Sstevel@tonic-gate * We need pointer size alignment for fd_fds. On a LP64 11970Sstevel@tonic-gate * kernel, the required alignment is 8 bytes while 11980Sstevel@tonic-gate * the option headers and values are only 4 bytes 11990Sstevel@tonic-gate * aligned. So its safer to do a bcopy compared to 12000Sstevel@tonic-gate * assigning fdbuf->fd_fds[i] to fp. 12010Sstevel@tonic-gate */ 12020Sstevel@tonic-gate bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp)); 12030Sstevel@tonic-gate mutex_enter(&fp->f_tlock); 12040Sstevel@tonic-gate fp->f_count++; 12050Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 12060Sstevel@tonic-gate setf(fd, fp); 12070Sstevel@tonic-gate *rp++ = fd; 12080Sstevel@tonic-gate #ifdef C2_AUDIT 12090Sstevel@tonic-gate if (audit_active) 12100Sstevel@tonic-gate audit_fdrecv(fd, fp); 12110Sstevel@tonic-gate #endif 12120Sstevel@tonic-gate dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n", 12130Sstevel@tonic-gate i, fd, fp, fp->f_count)); 12140Sstevel@tonic-gate } 12150Sstevel@tonic-gate return (0); 12160Sstevel@tonic-gate 12170Sstevel@tonic-gate cleanup: 12180Sstevel@tonic-gate /* 12190Sstevel@tonic-gate * Undo whatever partial work the loop above has done. 12200Sstevel@tonic-gate */ 12210Sstevel@tonic-gate { 12220Sstevel@tonic-gate int j; 12230Sstevel@tonic-gate 12240Sstevel@tonic-gate rp = (int *)rights; 12250Sstevel@tonic-gate for (j = 0; j < i; j++) { 12260Sstevel@tonic-gate dprint(0, 12270Sstevel@tonic-gate ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp)); 12280Sstevel@tonic-gate (void) closeandsetf(*rp++, NULL); 12290Sstevel@tonic-gate } 12300Sstevel@tonic-gate } 12310Sstevel@tonic-gate 12320Sstevel@tonic-gate return (EMFILE); 12330Sstevel@tonic-gate } 12340Sstevel@tonic-gate 12350Sstevel@tonic-gate /* 12360Sstevel@tonic-gate * Insert file descriptors into an fdbuf. 12370Sstevel@tonic-gate * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed 12380Sstevel@tonic-gate * by calling fdbuf_free(). 12390Sstevel@tonic-gate */ 12400Sstevel@tonic-gate int 12410Sstevel@tonic-gate fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp) 12420Sstevel@tonic-gate { 12430Sstevel@tonic-gate int numfd, i; 12440Sstevel@tonic-gate int *fds; 12450Sstevel@tonic-gate struct file *fp; 12460Sstevel@tonic-gate struct fdbuf *fdbuf; 12470Sstevel@tonic-gate int fdbufsize; 12480Sstevel@tonic-gate 12490Sstevel@tonic-gate dprint(1, ("fdbuf_create: len %d\n", rightslen)); 12500Sstevel@tonic-gate 12510Sstevel@tonic-gate numfd = rightslen / (int)sizeof (int); 12520Sstevel@tonic-gate 12530Sstevel@tonic-gate fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)); 12540Sstevel@tonic-gate fdbuf = kmem_alloc(fdbufsize, KM_SLEEP); 12550Sstevel@tonic-gate fdbuf->fd_size = fdbufsize; 12560Sstevel@tonic-gate fdbuf->fd_numfd = 0; 12570Sstevel@tonic-gate fdbuf->fd_ebuf = NULL; 12580Sstevel@tonic-gate fdbuf->fd_ebuflen = 0; 12590Sstevel@tonic-gate fds = (int *)rights; 12600Sstevel@tonic-gate for (i = 0; i < numfd; i++) { 12610Sstevel@tonic-gate if ((fp = getf(fds[i])) == NULL) { 12620Sstevel@tonic-gate fdbuf_free(fdbuf); 12630Sstevel@tonic-gate return (EBADF); 12640Sstevel@tonic-gate } 12650Sstevel@tonic-gate dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n", 12660Sstevel@tonic-gate i, fds[i], fp, fp->f_count)); 12670Sstevel@tonic-gate mutex_enter(&fp->f_tlock); 12680Sstevel@tonic-gate fp->f_count++; 12690Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 12700Sstevel@tonic-gate /* 12710Sstevel@tonic-gate * The maximum alignment for fdbuf (or any option header 12720Sstevel@tonic-gate * and its value) it 4 bytes. On a LP64 kernel, the alignment 12730Sstevel@tonic-gate * is not sufficient for pointers (fd_fds in this case). Since 12740Sstevel@tonic-gate * we just did a kmem_alloc (we get a double word alignment), 12750Sstevel@tonic-gate * we don't need to do anything on the send side (we loose 12760Sstevel@tonic-gate * the double word alignment because fdbuf goes after an 12770Sstevel@tonic-gate * option header (eg T_unitdata_req) which is only 4 byte 12780Sstevel@tonic-gate * aligned). We take care of this when we extract the file 12790Sstevel@tonic-gate * descriptor in fdbuf_extract or fdbuf_free. 12800Sstevel@tonic-gate */ 12810Sstevel@tonic-gate fdbuf->fd_fds[i] = fp; 12820Sstevel@tonic-gate fdbuf->fd_numfd++; 12830Sstevel@tonic-gate releasef(fds[i]); 12840Sstevel@tonic-gate #ifdef C2_AUDIT 12850Sstevel@tonic-gate if (audit_active) 12860Sstevel@tonic-gate audit_fdsend(fds[i], fp, 0); 12870Sstevel@tonic-gate #endif 12880Sstevel@tonic-gate } 12890Sstevel@tonic-gate *fdbufp = fdbuf; 12900Sstevel@tonic-gate return (0); 12910Sstevel@tonic-gate } 12920Sstevel@tonic-gate 12930Sstevel@tonic-gate static int 12940Sstevel@tonic-gate fdbuf_optlen(int rightslen) 12950Sstevel@tonic-gate { 12960Sstevel@tonic-gate int numfd; 12970Sstevel@tonic-gate 12980Sstevel@tonic-gate numfd = rightslen / (int)sizeof (int); 12990Sstevel@tonic-gate 13000Sstevel@tonic-gate return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *))); 13010Sstevel@tonic-gate } 13020Sstevel@tonic-gate 13030Sstevel@tonic-gate static t_uscalar_t 13040Sstevel@tonic-gate fdbuf_cmsglen(int fdbuflen) 13050Sstevel@tonic-gate { 13060Sstevel@tonic-gate return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) / 13070Sstevel@tonic-gate (int)sizeof (struct file *) * (int)sizeof (int)); 13080Sstevel@tonic-gate } 13090Sstevel@tonic-gate 13100Sstevel@tonic-gate 13110Sstevel@tonic-gate /* 13120Sstevel@tonic-gate * Return non-zero if the mblk and fdbuf are consistent. 13130Sstevel@tonic-gate */ 13140Sstevel@tonic-gate static int 13150Sstevel@tonic-gate fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen) 13160Sstevel@tonic-gate { 13170Sstevel@tonic-gate if (fdbuflen >= FDBUF_HDRSIZE && 13180Sstevel@tonic-gate fdbuflen == fdbuf->fd_size) { 13190Sstevel@tonic-gate frtn_t *frp = mp->b_datap->db_frtnp; 13200Sstevel@tonic-gate /* 13210Sstevel@tonic-gate * Check that the SO_FILEP portion of the 13220Sstevel@tonic-gate * message has not been modified by 13230Sstevel@tonic-gate * the loopback transport. The sending sockfs generates 13240Sstevel@tonic-gate * a message that is esballoc'ed with the free function 13250Sstevel@tonic-gate * being fdbuf_free() and where free_arg contains the 13260Sstevel@tonic-gate * identical information as the SO_FILEP content. 13270Sstevel@tonic-gate * 13280Sstevel@tonic-gate * If any of these constraints are not satisfied we 13290Sstevel@tonic-gate * silently ignore the option. 13300Sstevel@tonic-gate */ 13310Sstevel@tonic-gate ASSERT(mp); 13320Sstevel@tonic-gate if (frp != NULL && 13330Sstevel@tonic-gate frp->free_func == fdbuf_free && 13340Sstevel@tonic-gate frp->free_arg != NULL && 13350Sstevel@tonic-gate bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) { 13360Sstevel@tonic-gate dprint(1, ("fdbuf_verify: fdbuf %p len %d\n", 13370Sstevel@tonic-gate fdbuf, fdbuflen)); 13380Sstevel@tonic-gate return (1); 13390Sstevel@tonic-gate } else { 13401548Srshoaib zcmn_err(getzoneid(), CE_WARN, 13410Sstevel@tonic-gate "sockfs: mismatched fdbuf content (%p)", 13420Sstevel@tonic-gate (void *)mp); 13430Sstevel@tonic-gate return (0); 13440Sstevel@tonic-gate } 13450Sstevel@tonic-gate } else { 13461548Srshoaib zcmn_err(getzoneid(), CE_WARN, 13470Sstevel@tonic-gate "sockfs: mismatched fdbuf len %d, %d\n", 13480Sstevel@tonic-gate fdbuflen, fdbuf->fd_size); 13490Sstevel@tonic-gate return (0); 13500Sstevel@tonic-gate } 13510Sstevel@tonic-gate } 13520Sstevel@tonic-gate 13530Sstevel@tonic-gate /* 13540Sstevel@tonic-gate * When the file descriptors returned by sorecvmsg can not be passed 13550Sstevel@tonic-gate * to the application this routine will cleanup the references on 13560Sstevel@tonic-gate * the files. Start at startoff bytes into the buffer. 13570Sstevel@tonic-gate */ 13580Sstevel@tonic-gate static void 13590Sstevel@tonic-gate close_fds(void *fdbuf, int fdbuflen, int startoff) 13600Sstevel@tonic-gate { 13610Sstevel@tonic-gate int *fds = (int *)fdbuf; 13620Sstevel@tonic-gate int numfd = fdbuflen / (int)sizeof (int); 13630Sstevel@tonic-gate int i; 13640Sstevel@tonic-gate 13650Sstevel@tonic-gate dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff)); 13660Sstevel@tonic-gate 13670Sstevel@tonic-gate for (i = 0; i < numfd; i++) { 13680Sstevel@tonic-gate if (startoff < 0) 13690Sstevel@tonic-gate startoff = 0; 13700Sstevel@tonic-gate if (startoff < (int)sizeof (int)) { 13710Sstevel@tonic-gate /* 13720Sstevel@tonic-gate * This file descriptor is partially or fully after 13730Sstevel@tonic-gate * the offset 13740Sstevel@tonic-gate */ 13750Sstevel@tonic-gate dprint(0, 13760Sstevel@tonic-gate ("close_fds: cleanup[%d] = %d\n", i, fds[i])); 13770Sstevel@tonic-gate (void) closeandsetf(fds[i], NULL); 13780Sstevel@tonic-gate } 13790Sstevel@tonic-gate startoff -= (int)sizeof (int); 13800Sstevel@tonic-gate } 13810Sstevel@tonic-gate } 13820Sstevel@tonic-gate 13830Sstevel@tonic-gate /* 13840Sstevel@tonic-gate * Close all file descriptors contained in the control part starting at 13850Sstevel@tonic-gate * the startoffset. 13860Sstevel@tonic-gate */ 13870Sstevel@tonic-gate void 13880Sstevel@tonic-gate so_closefds(void *control, t_uscalar_t controllen, int oldflg, 13890Sstevel@tonic-gate int startoff) 13900Sstevel@tonic-gate { 13910Sstevel@tonic-gate struct cmsghdr *cmsg; 13920Sstevel@tonic-gate 13930Sstevel@tonic-gate if (control == NULL) 13940Sstevel@tonic-gate return; 13950Sstevel@tonic-gate 13960Sstevel@tonic-gate if (oldflg) { 13970Sstevel@tonic-gate close_fds(control, controllen, startoff); 13980Sstevel@tonic-gate return; 13990Sstevel@tonic-gate } 14000Sstevel@tonic-gate /* Scan control part for file descriptors. */ 14010Sstevel@tonic-gate for (cmsg = (struct cmsghdr *)control; 14020Sstevel@tonic-gate CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 14030Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg)) { 14040Sstevel@tonic-gate if (cmsg->cmsg_level == SOL_SOCKET && 14050Sstevel@tonic-gate cmsg->cmsg_type == SCM_RIGHTS) { 14060Sstevel@tonic-gate close_fds(CMSG_CONTENT(cmsg), 14070Sstevel@tonic-gate (int)CMSG_CONTENTLEN(cmsg), 14080Sstevel@tonic-gate startoff - (int)sizeof (struct cmsghdr)); 14090Sstevel@tonic-gate } 14100Sstevel@tonic-gate startoff -= cmsg->cmsg_len; 14110Sstevel@tonic-gate } 14120Sstevel@tonic-gate } 14130Sstevel@tonic-gate 14140Sstevel@tonic-gate /* 14150Sstevel@tonic-gate * Returns a pointer/length for the file descriptors contained 14160Sstevel@tonic-gate * in the control buffer. Returns with *fdlenp == -1 if there are no 14170Sstevel@tonic-gate * file descriptor options present. This is different than there being 14180Sstevel@tonic-gate * a zero-length file descriptor option. 14190Sstevel@tonic-gate * Fail if there are multiple SCM_RIGHT cmsgs. 14200Sstevel@tonic-gate */ 14210Sstevel@tonic-gate int 14220Sstevel@tonic-gate so_getfdopt(void *control, t_uscalar_t controllen, int oldflg, 14230Sstevel@tonic-gate void **fdsp, int *fdlenp) 14240Sstevel@tonic-gate { 14250Sstevel@tonic-gate struct cmsghdr *cmsg; 14260Sstevel@tonic-gate void *fds; 14270Sstevel@tonic-gate int fdlen; 14280Sstevel@tonic-gate 14290Sstevel@tonic-gate if (control == NULL) { 14300Sstevel@tonic-gate *fdsp = NULL; 14310Sstevel@tonic-gate *fdlenp = -1; 14320Sstevel@tonic-gate return (0); 14330Sstevel@tonic-gate } 14340Sstevel@tonic-gate 14350Sstevel@tonic-gate if (oldflg) { 14360Sstevel@tonic-gate *fdsp = control; 14370Sstevel@tonic-gate if (controllen == 0) 14380Sstevel@tonic-gate *fdlenp = -1; 14390Sstevel@tonic-gate else 14400Sstevel@tonic-gate *fdlenp = controllen; 14410Sstevel@tonic-gate dprint(1, ("so_getfdopt: old %d\n", *fdlenp)); 14420Sstevel@tonic-gate return (0); 14430Sstevel@tonic-gate } 14440Sstevel@tonic-gate 14450Sstevel@tonic-gate fds = NULL; 14460Sstevel@tonic-gate fdlen = 0; 14470Sstevel@tonic-gate 14480Sstevel@tonic-gate for (cmsg = (struct cmsghdr *)control; 14490Sstevel@tonic-gate CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 14500Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg)) { 14510Sstevel@tonic-gate if (cmsg->cmsg_level == SOL_SOCKET && 14520Sstevel@tonic-gate cmsg->cmsg_type == SCM_RIGHTS) { 14530Sstevel@tonic-gate if (fds != NULL) 14540Sstevel@tonic-gate return (EINVAL); 14550Sstevel@tonic-gate fds = CMSG_CONTENT(cmsg); 14560Sstevel@tonic-gate fdlen = (int)CMSG_CONTENTLEN(cmsg); 1457408Skrgopi dprint(1, ("so_getfdopt: new %lu\n", 1458408Skrgopi (size_t)CMSG_CONTENTLEN(cmsg))); 14590Sstevel@tonic-gate } 14600Sstevel@tonic-gate } 14610Sstevel@tonic-gate if (fds == NULL) { 14620Sstevel@tonic-gate dprint(1, ("so_getfdopt: NONE\n")); 14630Sstevel@tonic-gate *fdlenp = -1; 14640Sstevel@tonic-gate } else 14650Sstevel@tonic-gate *fdlenp = fdlen; 14660Sstevel@tonic-gate *fdsp = fds; 14670Sstevel@tonic-gate return (0); 14680Sstevel@tonic-gate } 14690Sstevel@tonic-gate 14700Sstevel@tonic-gate /* 14710Sstevel@tonic-gate * Return the length of the options including any file descriptor options. 14720Sstevel@tonic-gate */ 14730Sstevel@tonic-gate t_uscalar_t 14740Sstevel@tonic-gate so_optlen(void *control, t_uscalar_t controllen, int oldflg) 14750Sstevel@tonic-gate { 14760Sstevel@tonic-gate struct cmsghdr *cmsg; 14770Sstevel@tonic-gate t_uscalar_t optlen = 0; 14780Sstevel@tonic-gate t_uscalar_t len; 14790Sstevel@tonic-gate 14800Sstevel@tonic-gate if (control == NULL) 14810Sstevel@tonic-gate return (0); 14820Sstevel@tonic-gate 14830Sstevel@tonic-gate if (oldflg) 14840Sstevel@tonic-gate return ((t_uscalar_t)(sizeof (struct T_opthdr) + 14850Sstevel@tonic-gate fdbuf_optlen(controllen))); 14860Sstevel@tonic-gate 14870Sstevel@tonic-gate for (cmsg = (struct cmsghdr *)control; 14880Sstevel@tonic-gate CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 14890Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg)) { 14900Sstevel@tonic-gate if (cmsg->cmsg_level == SOL_SOCKET && 14910Sstevel@tonic-gate cmsg->cmsg_type == SCM_RIGHTS) { 14920Sstevel@tonic-gate len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg)); 14930Sstevel@tonic-gate } else { 14940Sstevel@tonic-gate len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg); 14950Sstevel@tonic-gate } 14960Sstevel@tonic-gate optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) + 14970Sstevel@tonic-gate sizeof (struct T_opthdr)); 14980Sstevel@tonic-gate } 14990Sstevel@tonic-gate dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n", 15000Sstevel@tonic-gate controllen, oldflg, optlen)); 15010Sstevel@tonic-gate return (optlen); 15020Sstevel@tonic-gate } 15030Sstevel@tonic-gate 15040Sstevel@tonic-gate /* 15050Sstevel@tonic-gate * Copy options from control to the mblk. Skip any file descriptor options. 15060Sstevel@tonic-gate */ 15070Sstevel@tonic-gate void 15080Sstevel@tonic-gate so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp) 15090Sstevel@tonic-gate { 15100Sstevel@tonic-gate struct T_opthdr toh; 15110Sstevel@tonic-gate struct cmsghdr *cmsg; 15120Sstevel@tonic-gate 15130Sstevel@tonic-gate if (control == NULL) 15140Sstevel@tonic-gate return; 15150Sstevel@tonic-gate 15160Sstevel@tonic-gate if (oldflg) { 15170Sstevel@tonic-gate /* No real options - caller has handled file descriptors */ 15180Sstevel@tonic-gate return; 15190Sstevel@tonic-gate } 15200Sstevel@tonic-gate for (cmsg = (struct cmsghdr *)control; 15210Sstevel@tonic-gate CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 15220Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg)) { 15230Sstevel@tonic-gate /* 15240Sstevel@tonic-gate * Note: The caller handles file descriptors prior 15250Sstevel@tonic-gate * to calling this function. 15260Sstevel@tonic-gate */ 15270Sstevel@tonic-gate t_uscalar_t len; 15280Sstevel@tonic-gate 15290Sstevel@tonic-gate if (cmsg->cmsg_level == SOL_SOCKET && 15300Sstevel@tonic-gate cmsg->cmsg_type == SCM_RIGHTS) 15310Sstevel@tonic-gate continue; 15320Sstevel@tonic-gate 15330Sstevel@tonic-gate len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg); 15340Sstevel@tonic-gate toh.level = cmsg->cmsg_level; 15350Sstevel@tonic-gate toh.name = cmsg->cmsg_type; 15360Sstevel@tonic-gate toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr); 15370Sstevel@tonic-gate toh.status = 0; 15380Sstevel@tonic-gate 15390Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 15400Sstevel@tonic-gate soappendmsg(mp, CMSG_CONTENT(cmsg), len); 15410Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(len) - len; 15420Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 15430Sstevel@tonic-gate } 15440Sstevel@tonic-gate } 15450Sstevel@tonic-gate 15460Sstevel@tonic-gate /* 15470Sstevel@tonic-gate * Return the length of the control message derived from the options. 15480Sstevel@tonic-gate * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP. 15490Sstevel@tonic-gate * When oldflg is set only include SO_FILEP. 15502280Sgt145670 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen 15512280Sgt145670 * allocates the space that so_opt2cmsg fills. If one changes, the other should 15522280Sgt145670 * also be checked for any possible impacts. 15530Sstevel@tonic-gate */ 15540Sstevel@tonic-gate t_uscalar_t 15550Sstevel@tonic-gate so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg) 15560Sstevel@tonic-gate { 15570Sstevel@tonic-gate t_uscalar_t cmsglen = 0; 15580Sstevel@tonic-gate struct T_opthdr *tohp; 15590Sstevel@tonic-gate t_uscalar_t len; 15600Sstevel@tonic-gate t_uscalar_t last_roundup = 0; 15610Sstevel@tonic-gate 15620Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(opt)); 15630Sstevel@tonic-gate 15640Sstevel@tonic-gate for (tohp = (struct T_opthdr *)opt; 15650Sstevel@tonic-gate tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 15660Sstevel@tonic-gate tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 15670Sstevel@tonic-gate dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n", 15680Sstevel@tonic-gate tohp->level, tohp->name, tohp->len)); 15690Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && 15700Sstevel@tonic-gate (tohp->name == SO_SRCADDR || 15710Sstevel@tonic-gate tohp->name == SO_UNIX_CLOSE)) { 15720Sstevel@tonic-gate continue; 15730Sstevel@tonic-gate } 15740Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) { 15750Sstevel@tonic-gate struct fdbuf *fdbuf; 15760Sstevel@tonic-gate int fdbuflen; 15770Sstevel@tonic-gate 15780Sstevel@tonic-gate fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp); 15790Sstevel@tonic-gate fdbuflen = (int)_TPI_TOPT_DATALEN(tohp); 15800Sstevel@tonic-gate 15810Sstevel@tonic-gate if (!fdbuf_verify(mp, fdbuf, fdbuflen)) 15820Sstevel@tonic-gate continue; 15830Sstevel@tonic-gate if (oldflg) { 15840Sstevel@tonic-gate cmsglen += fdbuf_cmsglen(fdbuflen); 15850Sstevel@tonic-gate continue; 15860Sstevel@tonic-gate } 15870Sstevel@tonic-gate len = fdbuf_cmsglen(fdbuflen); 15882280Sgt145670 } else if (tohp->level == SOL_SOCKET && 15892280Sgt145670 tohp->name == SCM_TIMESTAMP) { 15902280Sgt145670 if (oldflg) 15912280Sgt145670 continue; 15922280Sgt145670 15932280Sgt145670 if (get_udatamodel() == DATAMODEL_NATIVE) { 15942280Sgt145670 len = sizeof (struct timeval); 15952280Sgt145670 } else { 15962280Sgt145670 len = sizeof (struct timeval32); 15972280Sgt145670 } 15980Sstevel@tonic-gate } else { 15990Sstevel@tonic-gate if (oldflg) 16000Sstevel@tonic-gate continue; 16010Sstevel@tonic-gate len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp); 16020Sstevel@tonic-gate } 16030Sstevel@tonic-gate /* 16042280Sgt145670 * Exclude roundup for last option to not set 16050Sstevel@tonic-gate * MSG_CTRUNC when the cmsg fits but the padding doesn't fit. 16060Sstevel@tonic-gate */ 16070Sstevel@tonic-gate last_roundup = (t_uscalar_t) 16080Sstevel@tonic-gate (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) - 16090Sstevel@tonic-gate (len + (int)sizeof (struct cmsghdr))); 16100Sstevel@tonic-gate cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) + 16110Sstevel@tonic-gate last_roundup; 16120Sstevel@tonic-gate } 16130Sstevel@tonic-gate cmsglen -= last_roundup; 16140Sstevel@tonic-gate dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n", 16150Sstevel@tonic-gate optlen, oldflg, cmsglen)); 16160Sstevel@tonic-gate return (cmsglen); 16170Sstevel@tonic-gate } 16180Sstevel@tonic-gate 16190Sstevel@tonic-gate /* 16200Sstevel@tonic-gate * Copy options from options to the control. Convert SO_FILEP to 16210Sstevel@tonic-gate * file descriptors. 16220Sstevel@tonic-gate * Returns errno or zero. 16232280Sgt145670 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen 16242280Sgt145670 * allocates the space that so_opt2cmsg fills. If one changes, the other should 16252280Sgt145670 * also be checked for any possible impacts. 16260Sstevel@tonic-gate */ 16270Sstevel@tonic-gate int 16280Sstevel@tonic-gate so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg, 16290Sstevel@tonic-gate void *control, t_uscalar_t controllen) 16300Sstevel@tonic-gate { 16310Sstevel@tonic-gate struct T_opthdr *tohp; 16320Sstevel@tonic-gate struct cmsghdr *cmsg; 16330Sstevel@tonic-gate struct fdbuf *fdbuf; 16340Sstevel@tonic-gate int fdbuflen; 16350Sstevel@tonic-gate int error; 16362280Sgt145670 #if defined(DEBUG) || defined(__lint) 16372280Sgt145670 struct cmsghdr *cend = (struct cmsghdr *) 16382280Sgt145670 (((uint8_t *)control) + ROUNDUP_cmsglen(controllen)); 16392280Sgt145670 #endif 16400Sstevel@tonic-gate cmsg = (struct cmsghdr *)control; 16410Sstevel@tonic-gate 16420Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(opt)); 16430Sstevel@tonic-gate 16440Sstevel@tonic-gate for (tohp = (struct T_opthdr *)opt; 16450Sstevel@tonic-gate tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 16460Sstevel@tonic-gate tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 16470Sstevel@tonic-gate dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n", 16480Sstevel@tonic-gate tohp->level, tohp->name, tohp->len)); 16490Sstevel@tonic-gate 16500Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && 16510Sstevel@tonic-gate (tohp->name == SO_SRCADDR || 16520Sstevel@tonic-gate tohp->name == SO_UNIX_CLOSE)) { 16530Sstevel@tonic-gate continue; 16540Sstevel@tonic-gate } 16550Sstevel@tonic-gate ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen); 16560Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) { 16570Sstevel@tonic-gate fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp); 16580Sstevel@tonic-gate fdbuflen = (int)_TPI_TOPT_DATALEN(tohp); 16590Sstevel@tonic-gate 16600Sstevel@tonic-gate if (!fdbuf_verify(mp, fdbuf, fdbuflen)) 16610Sstevel@tonic-gate return (EPROTO); 16620Sstevel@tonic-gate if (oldflg) { 16630Sstevel@tonic-gate error = fdbuf_extract(fdbuf, control, 16640Sstevel@tonic-gate (int)controllen); 16650Sstevel@tonic-gate if (error != 0) 16660Sstevel@tonic-gate return (error); 16670Sstevel@tonic-gate continue; 16680Sstevel@tonic-gate } else { 16690Sstevel@tonic-gate int fdlen; 16700Sstevel@tonic-gate 16710Sstevel@tonic-gate fdlen = (int)fdbuf_cmsglen( 16720Sstevel@tonic-gate (int)_TPI_TOPT_DATALEN(tohp)); 16730Sstevel@tonic-gate 16740Sstevel@tonic-gate cmsg->cmsg_level = tohp->level; 16750Sstevel@tonic-gate cmsg->cmsg_type = SCM_RIGHTS; 16760Sstevel@tonic-gate cmsg->cmsg_len = (socklen_t)(fdlen + 16770Sstevel@tonic-gate sizeof (struct cmsghdr)); 16780Sstevel@tonic-gate 16790Sstevel@tonic-gate error = fdbuf_extract(fdbuf, 16800Sstevel@tonic-gate CMSG_CONTENT(cmsg), fdlen); 16810Sstevel@tonic-gate if (error != 0) 16820Sstevel@tonic-gate return (error); 16830Sstevel@tonic-gate } 16841673Sgt145670 } else if (tohp->level == SOL_SOCKET && 16851673Sgt145670 tohp->name == SCM_TIMESTAMP) { 16861673Sgt145670 timestruc_t *timestamp; 16871673Sgt145670 16881673Sgt145670 if (oldflg) 16891673Sgt145670 continue; 16901673Sgt145670 16911673Sgt145670 cmsg->cmsg_level = tohp->level; 16921673Sgt145670 cmsg->cmsg_type = tohp->name; 16931673Sgt145670 16941673Sgt145670 timestamp = 16951673Sgt145670 (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1], 16961673Sgt145670 sizeof (intptr_t)); 16971673Sgt145670 16981673Sgt145670 if (get_udatamodel() == DATAMODEL_NATIVE) { 16992280Sgt145670 struct timeval tv; 17001673Sgt145670 17011673Sgt145670 cmsg->cmsg_len = sizeof (struct timeval) + 17021673Sgt145670 sizeof (struct cmsghdr); 17032280Sgt145670 tv.tv_sec = timestamp->tv_sec; 17042280Sgt145670 tv.tv_usec = timestamp->tv_nsec / 17052280Sgt145670 (NANOSEC / MICROSEC); 17062280Sgt145670 /* 17072280Sgt145670 * on LP64 systems, the struct timeval in 17082280Sgt145670 * the destination will not be 8-byte aligned, 17092280Sgt145670 * so use bcopy to avoid alignment trouble 17102280Sgt145670 */ 17112280Sgt145670 bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv)); 17121673Sgt145670 } else { 17131673Sgt145670 struct timeval32 *time32; 17141673Sgt145670 17151673Sgt145670 cmsg->cmsg_len = sizeof (struct timeval32) + 17161673Sgt145670 sizeof (struct cmsghdr); 17171673Sgt145670 time32 = (struct timeval32 *)CMSG_CONTENT(cmsg); 17181673Sgt145670 time32->tv_sec = (time32_t)timestamp->tv_sec; 17191673Sgt145670 time32->tv_usec = 17201673Sgt145670 (int32_t)(timestamp->tv_nsec / 17211673Sgt145670 (NANOSEC / MICROSEC)); 17221673Sgt145670 } 17231673Sgt145670 17240Sstevel@tonic-gate } else { 17250Sstevel@tonic-gate if (oldflg) 17260Sstevel@tonic-gate continue; 17270Sstevel@tonic-gate 17280Sstevel@tonic-gate cmsg->cmsg_level = tohp->level; 17290Sstevel@tonic-gate cmsg->cmsg_type = tohp->name; 17300Sstevel@tonic-gate cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) + 17310Sstevel@tonic-gate sizeof (struct cmsghdr)); 17320Sstevel@tonic-gate 17330Sstevel@tonic-gate /* copy content to control data part */ 17340Sstevel@tonic-gate bcopy(&tohp[1], CMSG_CONTENT(cmsg), 17350Sstevel@tonic-gate CMSG_CONTENTLEN(cmsg)); 17360Sstevel@tonic-gate } 17370Sstevel@tonic-gate /* move to next CMSG structure! */ 17380Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg); 17390Sstevel@tonic-gate } 17402280Sgt145670 dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n", 17412280Sgt145670 control, controllen, cend, cmsg)); 17422280Sgt145670 ASSERT(cmsg <= cend); 17430Sstevel@tonic-gate return (0); 17440Sstevel@tonic-gate } 17450Sstevel@tonic-gate 17460Sstevel@tonic-gate /* 17470Sstevel@tonic-gate * Extract the SO_SRCADDR option value if present. 17480Sstevel@tonic-gate */ 17490Sstevel@tonic-gate void 17500Sstevel@tonic-gate so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp, 17510Sstevel@tonic-gate t_uscalar_t *srclenp) 17520Sstevel@tonic-gate { 17530Sstevel@tonic-gate struct T_opthdr *tohp; 17540Sstevel@tonic-gate 17550Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(opt)); 17560Sstevel@tonic-gate 17570Sstevel@tonic-gate ASSERT(srcp != NULL && srclenp != NULL); 17580Sstevel@tonic-gate *srcp = NULL; 17590Sstevel@tonic-gate *srclenp = 0; 17600Sstevel@tonic-gate 17610Sstevel@tonic-gate for (tohp = (struct T_opthdr *)opt; 17620Sstevel@tonic-gate tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 17630Sstevel@tonic-gate tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 17640Sstevel@tonic-gate dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n", 17650Sstevel@tonic-gate tohp->level, tohp->name, tohp->len)); 17660Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && 17670Sstevel@tonic-gate tohp->name == SO_SRCADDR) { 17680Sstevel@tonic-gate *srcp = _TPI_TOPT_DATA(tohp); 17690Sstevel@tonic-gate *srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp); 17700Sstevel@tonic-gate } 17710Sstevel@tonic-gate } 17720Sstevel@tonic-gate } 17730Sstevel@tonic-gate 17740Sstevel@tonic-gate /* 17750Sstevel@tonic-gate * Verify if the SO_UNIX_CLOSE option is present. 17760Sstevel@tonic-gate */ 17770Sstevel@tonic-gate int 17780Sstevel@tonic-gate so_getopt_unix_close(void *opt, t_uscalar_t optlen) 17790Sstevel@tonic-gate { 17800Sstevel@tonic-gate struct T_opthdr *tohp; 17810Sstevel@tonic-gate 17820Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(opt)); 17830Sstevel@tonic-gate 17840Sstevel@tonic-gate for (tohp = (struct T_opthdr *)opt; 17850Sstevel@tonic-gate tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 17860Sstevel@tonic-gate tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 17870Sstevel@tonic-gate dprint(1, 17880Sstevel@tonic-gate ("so_getopt_unix_close: level 0x%x, name %d, len %d\n", 17890Sstevel@tonic-gate tohp->level, tohp->name, tohp->len)); 17900Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && 17910Sstevel@tonic-gate tohp->name == SO_UNIX_CLOSE) 17920Sstevel@tonic-gate return (1); 17930Sstevel@tonic-gate } 17940Sstevel@tonic-gate return (0); 17950Sstevel@tonic-gate } 17960Sstevel@tonic-gate 17970Sstevel@tonic-gate /* 17980Sstevel@tonic-gate * Allocate an M_PROTO message. 17990Sstevel@tonic-gate * 18000Sstevel@tonic-gate * If allocation fails the behavior depends on sleepflg: 18010Sstevel@tonic-gate * _ALLOC_NOSLEEP fail immediately 18020Sstevel@tonic-gate * _ALLOC_INTR sleep for memory until a signal is caught 18030Sstevel@tonic-gate * _ALLOC_SLEEP sleep forever. Don't return NULL. 18040Sstevel@tonic-gate */ 18050Sstevel@tonic-gate mblk_t * 18060Sstevel@tonic-gate soallocproto(size_t size, int sleepflg) 18070Sstevel@tonic-gate { 18080Sstevel@tonic-gate mblk_t *mp; 18090Sstevel@tonic-gate 18100Sstevel@tonic-gate /* Round up size for reuse */ 18110Sstevel@tonic-gate size = MAX(size, 64); 18120Sstevel@tonic-gate mp = allocb(size, BPRI_MED); 18130Sstevel@tonic-gate if (mp == NULL) { 18140Sstevel@tonic-gate int error; /* Dummy - error not returned to caller */ 18150Sstevel@tonic-gate 18160Sstevel@tonic-gate switch (sleepflg) { 18170Sstevel@tonic-gate case _ALLOC_SLEEP: 18180Sstevel@tonic-gate mp = allocb_wait(size, BPRI_MED, STR_NOSIG, &error); 18190Sstevel@tonic-gate ASSERT(mp); 18200Sstevel@tonic-gate break; 18210Sstevel@tonic-gate case _ALLOC_INTR: 18220Sstevel@tonic-gate mp = allocb_wait(size, BPRI_MED, 0, &error); 18230Sstevel@tonic-gate if (mp == NULL) { 18240Sstevel@tonic-gate /* Caught signal while sleeping for memory */ 18250Sstevel@tonic-gate eprintline(ENOBUFS); 18260Sstevel@tonic-gate return (NULL); 18270Sstevel@tonic-gate } 18280Sstevel@tonic-gate break; 18290Sstevel@tonic-gate case _ALLOC_NOSLEEP: 18300Sstevel@tonic-gate default: 18310Sstevel@tonic-gate eprintline(ENOBUFS); 18320Sstevel@tonic-gate return (NULL); 18330Sstevel@tonic-gate } 18340Sstevel@tonic-gate } 18350Sstevel@tonic-gate DB_TYPE(mp) = M_PROTO; 18360Sstevel@tonic-gate return (mp); 18370Sstevel@tonic-gate } 18380Sstevel@tonic-gate 18390Sstevel@tonic-gate /* 18400Sstevel@tonic-gate * Allocate an M_PROTO message with a single component. 18410Sstevel@tonic-gate * len is the length of buf. size is the amount to allocate. 18420Sstevel@tonic-gate * 18430Sstevel@tonic-gate * buf can be NULL with a non-zero len. 18440Sstevel@tonic-gate * This results in a bzero'ed chunk being placed the message. 18450Sstevel@tonic-gate */ 18460Sstevel@tonic-gate mblk_t * 18470Sstevel@tonic-gate soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg) 18480Sstevel@tonic-gate { 18490Sstevel@tonic-gate mblk_t *mp; 18500Sstevel@tonic-gate 18510Sstevel@tonic-gate if (size == 0) 18520Sstevel@tonic-gate size = len; 18530Sstevel@tonic-gate 18540Sstevel@tonic-gate ASSERT(size >= len); 18550Sstevel@tonic-gate /* Round up size for reuse */ 18560Sstevel@tonic-gate size = MAX(size, 64); 18570Sstevel@tonic-gate mp = soallocproto(size, sleepflg); 18580Sstevel@tonic-gate if (mp == NULL) 18590Sstevel@tonic-gate return (NULL); 18600Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO; 18610Sstevel@tonic-gate if (len != 0) { 18620Sstevel@tonic-gate if (buf != NULL) 18630Sstevel@tonic-gate bcopy(buf, mp->b_wptr, len); 18640Sstevel@tonic-gate else 18650Sstevel@tonic-gate bzero(mp->b_wptr, len); 18660Sstevel@tonic-gate mp->b_wptr += len; 18670Sstevel@tonic-gate } 18680Sstevel@tonic-gate return (mp); 18690Sstevel@tonic-gate } 18700Sstevel@tonic-gate 18710Sstevel@tonic-gate /* 18720Sstevel@tonic-gate * Append buf/len to mp. 18730Sstevel@tonic-gate * The caller has to ensure that there is enough room in the mblk. 18740Sstevel@tonic-gate * 18750Sstevel@tonic-gate * buf can be NULL with a non-zero len. 18760Sstevel@tonic-gate * This results in a bzero'ed chunk being placed the message. 18770Sstevel@tonic-gate */ 18780Sstevel@tonic-gate void 18790Sstevel@tonic-gate soappendmsg(mblk_t *mp, const void *buf, ssize_t len) 18800Sstevel@tonic-gate { 18810Sstevel@tonic-gate ASSERT(mp); 18820Sstevel@tonic-gate 18830Sstevel@tonic-gate if (len != 0) { 18840Sstevel@tonic-gate /* Assert for room left */ 18850Sstevel@tonic-gate ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len); 18860Sstevel@tonic-gate if (buf != NULL) 18870Sstevel@tonic-gate bcopy(buf, mp->b_wptr, len); 18880Sstevel@tonic-gate else 18890Sstevel@tonic-gate bzero(mp->b_wptr, len); 18900Sstevel@tonic-gate } 18910Sstevel@tonic-gate mp->b_wptr += len; 18920Sstevel@tonic-gate } 18930Sstevel@tonic-gate 18940Sstevel@tonic-gate /* 18950Sstevel@tonic-gate * Create a message using two kernel buffers. 18960Sstevel@tonic-gate * If size is set that will determine the allocation size (e.g. for future 18970Sstevel@tonic-gate * soappendmsg calls). If size is zero it is derived from the buffer 18980Sstevel@tonic-gate * lengths. 18990Sstevel@tonic-gate */ 19000Sstevel@tonic-gate mblk_t * 19010Sstevel@tonic-gate soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2, 19020Sstevel@tonic-gate ssize_t size, int sleepflg) 19030Sstevel@tonic-gate { 19040Sstevel@tonic-gate mblk_t *mp; 19050Sstevel@tonic-gate 19060Sstevel@tonic-gate if (size == 0) 19070Sstevel@tonic-gate size = len1 + len2; 19080Sstevel@tonic-gate ASSERT(size >= len1 + len2); 19090Sstevel@tonic-gate 19100Sstevel@tonic-gate mp = soallocproto1(buf1, len1, size, sleepflg); 19110Sstevel@tonic-gate if (mp) 19120Sstevel@tonic-gate soappendmsg(mp, buf2, len2); 19130Sstevel@tonic-gate return (mp); 19140Sstevel@tonic-gate } 19150Sstevel@tonic-gate 19160Sstevel@tonic-gate /* 19170Sstevel@tonic-gate * Create a message using three kernel buffers. 19180Sstevel@tonic-gate * If size is set that will determine the allocation size (for future 19190Sstevel@tonic-gate * soappendmsg calls). If size is zero it is derived from the buffer 19200Sstevel@tonic-gate * lengths. 19210Sstevel@tonic-gate */ 19220Sstevel@tonic-gate mblk_t * 19230Sstevel@tonic-gate soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2, 19240Sstevel@tonic-gate const void *buf3, ssize_t len3, ssize_t size, int sleepflg) 19250Sstevel@tonic-gate { 19260Sstevel@tonic-gate mblk_t *mp; 19270Sstevel@tonic-gate 19280Sstevel@tonic-gate if (size == 0) 19290Sstevel@tonic-gate size = len1 + len2 +len3; 19300Sstevel@tonic-gate ASSERT(size >= len1 + len2 + len3); 19310Sstevel@tonic-gate 19320Sstevel@tonic-gate mp = soallocproto1(buf1, len1, size, sleepflg); 19330Sstevel@tonic-gate if (mp != NULL) { 19340Sstevel@tonic-gate soappendmsg(mp, buf2, len2); 19350Sstevel@tonic-gate soappendmsg(mp, buf3, len3); 19360Sstevel@tonic-gate } 19370Sstevel@tonic-gate return (mp); 19380Sstevel@tonic-gate } 19390Sstevel@tonic-gate 19400Sstevel@tonic-gate #ifdef DEBUG 19410Sstevel@tonic-gate char * 19420Sstevel@tonic-gate pr_state(uint_t state, uint_t mode) 19430Sstevel@tonic-gate { 19440Sstevel@tonic-gate static char buf[1024]; 19450Sstevel@tonic-gate 19460Sstevel@tonic-gate buf[0] = 0; 19470Sstevel@tonic-gate if (state & SS_ISCONNECTED) 19480Sstevel@tonic-gate strcat(buf, "ISCONNECTED "); 19490Sstevel@tonic-gate if (state & SS_ISCONNECTING) 19500Sstevel@tonic-gate strcat(buf, "ISCONNECTING "); 19510Sstevel@tonic-gate if (state & SS_ISDISCONNECTING) 19520Sstevel@tonic-gate strcat(buf, "ISDISCONNECTING "); 19530Sstevel@tonic-gate if (state & SS_CANTSENDMORE) 19540Sstevel@tonic-gate strcat(buf, "CANTSENDMORE "); 19550Sstevel@tonic-gate 19560Sstevel@tonic-gate if (state & SS_CANTRCVMORE) 19570Sstevel@tonic-gate strcat(buf, "CANTRCVMORE "); 19580Sstevel@tonic-gate if (state & SS_ISBOUND) 19590Sstevel@tonic-gate strcat(buf, "ISBOUND "); 19600Sstevel@tonic-gate if (state & SS_NDELAY) 19610Sstevel@tonic-gate strcat(buf, "NDELAY "); 19620Sstevel@tonic-gate if (state & SS_NONBLOCK) 19630Sstevel@tonic-gate strcat(buf, "NONBLOCK "); 19640Sstevel@tonic-gate 19650Sstevel@tonic-gate if (state & SS_ASYNC) 19660Sstevel@tonic-gate strcat(buf, "ASYNC "); 19670Sstevel@tonic-gate if (state & SS_ACCEPTCONN) 19680Sstevel@tonic-gate strcat(buf, "ACCEPTCONN "); 19690Sstevel@tonic-gate if (state & SS_HASCONNIND) 19700Sstevel@tonic-gate strcat(buf, "HASCONNIND "); 19710Sstevel@tonic-gate if (state & SS_SAVEDEOR) 19720Sstevel@tonic-gate strcat(buf, "SAVEDEOR "); 19730Sstevel@tonic-gate 19740Sstevel@tonic-gate if (state & SS_RCVATMARK) 19750Sstevel@tonic-gate strcat(buf, "RCVATMARK "); 19760Sstevel@tonic-gate if (state & SS_OOBPEND) 19770Sstevel@tonic-gate strcat(buf, "OOBPEND "); 19780Sstevel@tonic-gate if (state & SS_HAVEOOBDATA) 19790Sstevel@tonic-gate strcat(buf, "HAVEOOBDATA "); 19800Sstevel@tonic-gate if (state & SS_HADOOBDATA) 19810Sstevel@tonic-gate strcat(buf, "HADOOBDATA "); 19820Sstevel@tonic-gate 19830Sstevel@tonic-gate if (state & SS_FADDR_NOXLATE) 19840Sstevel@tonic-gate strcat(buf, "FADDR_NOXLATE "); 19850Sstevel@tonic-gate 19860Sstevel@tonic-gate if (mode & SM_PRIV) 19870Sstevel@tonic-gate strcat(buf, "PRIV "); 19880Sstevel@tonic-gate if (mode & SM_ATOMIC) 19890Sstevel@tonic-gate strcat(buf, "ATOMIC "); 19900Sstevel@tonic-gate if (mode & SM_ADDR) 19910Sstevel@tonic-gate strcat(buf, "ADDR "); 19920Sstevel@tonic-gate if (mode & SM_CONNREQUIRED) 19930Sstevel@tonic-gate strcat(buf, "CONNREQUIRED "); 19940Sstevel@tonic-gate 19950Sstevel@tonic-gate if (mode & SM_FDPASSING) 19960Sstevel@tonic-gate strcat(buf, "FDPASSING "); 19970Sstevel@tonic-gate if (mode & SM_EXDATA) 19980Sstevel@tonic-gate strcat(buf, "EXDATA "); 19990Sstevel@tonic-gate if (mode & SM_OPTDATA) 20000Sstevel@tonic-gate strcat(buf, "OPTDATA "); 20010Sstevel@tonic-gate if (mode & SM_BYTESTREAM) 20020Sstevel@tonic-gate strcat(buf, "BYTESTREAM "); 20030Sstevel@tonic-gate return (buf); 20040Sstevel@tonic-gate } 20050Sstevel@tonic-gate 20060Sstevel@tonic-gate char * 20070Sstevel@tonic-gate pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen) 20080Sstevel@tonic-gate { 20090Sstevel@tonic-gate static char buf[1024]; 20100Sstevel@tonic-gate 20110Sstevel@tonic-gate if (addr == NULL || addrlen == 0) { 20120Sstevel@tonic-gate sprintf(buf, "(len %d) %p", addrlen, addr); 20130Sstevel@tonic-gate return (buf); 20140Sstevel@tonic-gate } 20150Sstevel@tonic-gate switch (family) { 20160Sstevel@tonic-gate case AF_INET: { 20170Sstevel@tonic-gate struct sockaddr_in sin; 20180Sstevel@tonic-gate 20190Sstevel@tonic-gate bcopy(addr, &sin, sizeof (sin)); 20200Sstevel@tonic-gate 20210Sstevel@tonic-gate (void) sprintf(buf, "(len %d) %x/%d", 20220Sstevel@tonic-gate addrlen, ntohl(sin.sin_addr.s_addr), 20230Sstevel@tonic-gate ntohs(sin.sin_port)); 20240Sstevel@tonic-gate break; 20250Sstevel@tonic-gate } 20260Sstevel@tonic-gate case AF_INET6: { 20270Sstevel@tonic-gate struct sockaddr_in6 sin6; 20280Sstevel@tonic-gate uint16_t *piece = (uint16_t *)&sin6.sin6_addr; 20290Sstevel@tonic-gate 20300Sstevel@tonic-gate bcopy((char *)addr, (char *)&sin6, sizeof (sin6)); 20310Sstevel@tonic-gate sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d", 20320Sstevel@tonic-gate addrlen, 20330Sstevel@tonic-gate ntohs(piece[0]), ntohs(piece[1]), 20340Sstevel@tonic-gate ntohs(piece[2]), ntohs(piece[3]), 20350Sstevel@tonic-gate ntohs(piece[4]), ntohs(piece[5]), 20360Sstevel@tonic-gate ntohs(piece[6]), ntohs(piece[7]), 20370Sstevel@tonic-gate ntohs(sin6.sin6_port)); 20380Sstevel@tonic-gate break; 20390Sstevel@tonic-gate } 20400Sstevel@tonic-gate case AF_UNIX: { 20410Sstevel@tonic-gate struct sockaddr_un *soun = (struct sockaddr_un *)addr; 20420Sstevel@tonic-gate 20430Sstevel@tonic-gate (void) sprintf(buf, "(len %d) %s", 20440Sstevel@tonic-gate addrlen, 20450Sstevel@tonic-gate (soun == NULL) ? "(none)" : soun->sun_path); 20460Sstevel@tonic-gate break; 20470Sstevel@tonic-gate } 20480Sstevel@tonic-gate default: 20490Sstevel@tonic-gate (void) sprintf(buf, "(unknown af %d)", family); 20500Sstevel@tonic-gate break; 20510Sstevel@tonic-gate } 20520Sstevel@tonic-gate return (buf); 20530Sstevel@tonic-gate } 20540Sstevel@tonic-gate 20550Sstevel@tonic-gate /* The logical equivalence operator (a if-and-only-if b) */ 20560Sstevel@tonic-gate #define EQUIV(a, b) (((a) && (b)) || (!(a) && (!(b)))) 20570Sstevel@tonic-gate 20580Sstevel@tonic-gate /* 20590Sstevel@tonic-gate * Verify limitations and invariants on oob state. 20600Sstevel@tonic-gate * Return 1 if OK, otherwise 0 so that it can be used as 20610Sstevel@tonic-gate * ASSERT(verify_oobstate(so)); 20620Sstevel@tonic-gate */ 20630Sstevel@tonic-gate int 20640Sstevel@tonic-gate so_verify_oobstate(struct sonode *so) 20650Sstevel@tonic-gate { 20660Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 20670Sstevel@tonic-gate 20680Sstevel@tonic-gate /* 20690Sstevel@tonic-gate * The possible state combinations are: 20700Sstevel@tonic-gate * 0 20710Sstevel@tonic-gate * SS_OOBPEND 20720Sstevel@tonic-gate * SS_OOBPEND|SS_HAVEOOBDATA 20730Sstevel@tonic-gate * SS_OOBPEND|SS_HADOOBDATA 20740Sstevel@tonic-gate * SS_HADOOBDATA 20750Sstevel@tonic-gate */ 20760Sstevel@tonic-gate switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) { 20770Sstevel@tonic-gate case 0: 20780Sstevel@tonic-gate case SS_OOBPEND: 20790Sstevel@tonic-gate case SS_OOBPEND|SS_HAVEOOBDATA: 20800Sstevel@tonic-gate case SS_OOBPEND|SS_HADOOBDATA: 20810Sstevel@tonic-gate case SS_HADOOBDATA: 20820Sstevel@tonic-gate break; 20830Sstevel@tonic-gate default: 20840Sstevel@tonic-gate printf("Bad oob state 1 (%p): counts %d/%d state %s\n", 20850Sstevel@tonic-gate so, so->so_oobsigcnt, 20860Sstevel@tonic-gate so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 20870Sstevel@tonic-gate return (0); 20880Sstevel@tonic-gate } 20890Sstevel@tonic-gate 20900Sstevel@tonic-gate /* SS_RCVATMARK should only be set when SS_OOBPEND is set */ 20910Sstevel@tonic-gate if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) { 20920Sstevel@tonic-gate printf("Bad oob state 2 (%p): counts %d/%d state %s\n", 20930Sstevel@tonic-gate so, so->so_oobsigcnt, 20940Sstevel@tonic-gate so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 20950Sstevel@tonic-gate return (0); 20960Sstevel@tonic-gate } 20970Sstevel@tonic-gate 20980Sstevel@tonic-gate /* 20990Sstevel@tonic-gate * (so_oobsigcnt != 0 or SS_RCVATMARK) iff SS_OOBPEND 21000Sstevel@tonic-gate */ 21010Sstevel@tonic-gate if (!EQUIV((so->so_oobsigcnt != 0) || (so->so_state & SS_RCVATMARK), 21020Sstevel@tonic-gate so->so_state & SS_OOBPEND)) { 21030Sstevel@tonic-gate printf("Bad oob state 3 (%p): counts %d/%d state %s\n", 21040Sstevel@tonic-gate so, so->so_oobsigcnt, 21050Sstevel@tonic-gate so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 21060Sstevel@tonic-gate return (0); 21070Sstevel@tonic-gate } 21080Sstevel@tonic-gate 21090Sstevel@tonic-gate /* 21100Sstevel@tonic-gate * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA 21110Sstevel@tonic-gate */ 21120Sstevel@tonic-gate if (!(so->so_options & SO_OOBINLINE) && 21130Sstevel@tonic-gate !EQUIV(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) { 21140Sstevel@tonic-gate printf("Bad oob state 4 (%p): counts %d/%d state %s\n", 21150Sstevel@tonic-gate so, so->so_oobsigcnt, 21160Sstevel@tonic-gate so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 21170Sstevel@tonic-gate return (0); 21180Sstevel@tonic-gate } 21190Sstevel@tonic-gate if (so->so_oobsigcnt < so->so_oobcnt) { 21200Sstevel@tonic-gate printf("Bad oob state 5 (%p): counts %d/%d state %s\n", 21210Sstevel@tonic-gate so, so->so_oobsigcnt, 21220Sstevel@tonic-gate so->so_oobcnt, pr_state(so->so_state, so->so_mode)); 21230Sstevel@tonic-gate return (0); 21240Sstevel@tonic-gate } 21250Sstevel@tonic-gate return (1); 21260Sstevel@tonic-gate } 21270Sstevel@tonic-gate #undef EQUIV 21280Sstevel@tonic-gate 21290Sstevel@tonic-gate #endif /* DEBUG */ 21300Sstevel@tonic-gate 21310Sstevel@tonic-gate /* initialize sockfs zone specific kstat related items */ 21320Sstevel@tonic-gate void * 21330Sstevel@tonic-gate sock_kstat_init(zoneid_t zoneid) 21340Sstevel@tonic-gate { 21350Sstevel@tonic-gate kstat_t *ksp; 21360Sstevel@tonic-gate 21370Sstevel@tonic-gate ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc", 21380Sstevel@tonic-gate KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid); 21390Sstevel@tonic-gate 21400Sstevel@tonic-gate if (ksp != NULL) { 21410Sstevel@tonic-gate ksp->ks_update = sockfs_update; 21420Sstevel@tonic-gate ksp->ks_snapshot = sockfs_snapshot; 21430Sstevel@tonic-gate ksp->ks_lock = &socklist.sl_lock; 21440Sstevel@tonic-gate ksp->ks_private = (void *)(uintptr_t)zoneid; 21450Sstevel@tonic-gate kstat_install(ksp); 21460Sstevel@tonic-gate } 21470Sstevel@tonic-gate 21480Sstevel@tonic-gate return (ksp); 21490Sstevel@tonic-gate } 21500Sstevel@tonic-gate 21510Sstevel@tonic-gate /* tear down sockfs zone specific kstat related items */ 21520Sstevel@tonic-gate /*ARGSUSED*/ 21530Sstevel@tonic-gate void 21540Sstevel@tonic-gate sock_kstat_fini(zoneid_t zoneid, void *arg) 21550Sstevel@tonic-gate { 21560Sstevel@tonic-gate kstat_t *ksp = (kstat_t *)arg; 21570Sstevel@tonic-gate 21580Sstevel@tonic-gate if (ksp != NULL) { 21590Sstevel@tonic-gate ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private); 21600Sstevel@tonic-gate kstat_delete(ksp); 21610Sstevel@tonic-gate } 21620Sstevel@tonic-gate } 21630Sstevel@tonic-gate 21640Sstevel@tonic-gate /* 21650Sstevel@tonic-gate * Zones: 21660Sstevel@tonic-gate * Note that nactive is going to be different for each zone. 21670Sstevel@tonic-gate * This means we require kstat to call sockfs_update and then sockfs_snapshot 21680Sstevel@tonic-gate * for the same zone, or sockfs_snapshot will be taken into the wrong size 21690Sstevel@tonic-gate * buffer. This is safe, but if the buffer is too small, user will not be 21700Sstevel@tonic-gate * given details of all sockets. However, as this kstat has a ks_lock, kstat 21710Sstevel@tonic-gate * driver will keep it locked between the update and the snapshot, so no 21720Sstevel@tonic-gate * other process (zone) can currently get inbetween resulting in a wrong size 21730Sstevel@tonic-gate * buffer allocation. 21740Sstevel@tonic-gate */ 21750Sstevel@tonic-gate static int 21760Sstevel@tonic-gate sockfs_update(kstat_t *ksp, int rw) 21770Sstevel@tonic-gate { 21780Sstevel@tonic-gate uint_t nactive = 0; /* # of active AF_UNIX sockets */ 21790Sstevel@tonic-gate struct sonode *so; /* current sonode on socklist */ 21800Sstevel@tonic-gate zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private; 21810Sstevel@tonic-gate 21820Sstevel@tonic-gate ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid()); 21830Sstevel@tonic-gate 21840Sstevel@tonic-gate if (rw == KSTAT_WRITE) { /* bounce all writes */ 21850Sstevel@tonic-gate return (EACCES); 21860Sstevel@tonic-gate } 21870Sstevel@tonic-gate 21880Sstevel@tonic-gate for (so = socklist.sl_list; so != NULL; so = so->so_next) { 21890Sstevel@tonic-gate if (so->so_accessvp != NULL && so->so_zoneid == myzoneid) { 21900Sstevel@tonic-gate nactive++; 21910Sstevel@tonic-gate } 21920Sstevel@tonic-gate } 21930Sstevel@tonic-gate ksp->ks_ndata = nactive; 21940Sstevel@tonic-gate ksp->ks_data_size = nactive * sizeof (struct k_sockinfo); 21950Sstevel@tonic-gate 21960Sstevel@tonic-gate return (0); 21970Sstevel@tonic-gate } 21980Sstevel@tonic-gate 21990Sstevel@tonic-gate static int 22000Sstevel@tonic-gate sockfs_snapshot(kstat_t *ksp, void *buf, int rw) 22010Sstevel@tonic-gate { 22020Sstevel@tonic-gate int ns; /* # of sonodes we've copied */ 22030Sstevel@tonic-gate struct sonode *so; /* current sonode on socklist */ 22040Sstevel@tonic-gate struct k_sockinfo *pksi; /* where we put sockinfo data */ 22050Sstevel@tonic-gate t_uscalar_t sn_len; /* soa_len */ 22060Sstevel@tonic-gate zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private; 22070Sstevel@tonic-gate 22080Sstevel@tonic-gate ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid()); 22090Sstevel@tonic-gate 22100Sstevel@tonic-gate ksp->ks_snaptime = gethrtime(); 22110Sstevel@tonic-gate 22120Sstevel@tonic-gate if (rw == KSTAT_WRITE) { /* bounce all writes */ 22130Sstevel@tonic-gate return (EACCES); 22140Sstevel@tonic-gate } 22150Sstevel@tonic-gate 22160Sstevel@tonic-gate /* 22170Sstevel@tonic-gate * for each sonode on the socklist, we massage the important 22180Sstevel@tonic-gate * info into buf, in k_sockinfo format. 22190Sstevel@tonic-gate */ 22200Sstevel@tonic-gate pksi = (struct k_sockinfo *)buf; 22210Sstevel@tonic-gate for (ns = 0, so = socklist.sl_list; so != NULL; so = so->so_next) { 22220Sstevel@tonic-gate /* only stuff active sonodes and the same zone: */ 22230Sstevel@tonic-gate if (so->so_accessvp == NULL || so->so_zoneid != myzoneid) { 22240Sstevel@tonic-gate continue; 22250Sstevel@tonic-gate } 22260Sstevel@tonic-gate 22270Sstevel@tonic-gate /* 22280Sstevel@tonic-gate * If the sonode was activated between the update and the 22290Sstevel@tonic-gate * snapshot, we're done - as this is only a snapshot. 22300Sstevel@tonic-gate */ 22310Sstevel@tonic-gate if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) { 22320Sstevel@tonic-gate break; 22330Sstevel@tonic-gate } 22340Sstevel@tonic-gate 22350Sstevel@tonic-gate /* copy important info into buf: */ 22360Sstevel@tonic-gate pksi->ks_si.si_size = sizeof (struct k_sockinfo); 22370Sstevel@tonic-gate pksi->ks_si.si_family = so->so_family; 22380Sstevel@tonic-gate pksi->ks_si.si_type = so->so_type; 22390Sstevel@tonic-gate pksi->ks_si.si_flag = so->so_flag; 22400Sstevel@tonic-gate pksi->ks_si.si_state = so->so_state; 22410Sstevel@tonic-gate pksi->ks_si.si_serv_type = so->so_serv_type; 22420Sstevel@tonic-gate pksi->ks_si.si_ux_laddr_sou_magic = so->so_ux_laddr.soua_magic; 22430Sstevel@tonic-gate pksi->ks_si.si_ux_faddr_sou_magic = so->so_ux_faddr.soua_magic; 22440Sstevel@tonic-gate pksi->ks_si.si_laddr_soa_len = so->so_laddr.soa_len; 22450Sstevel@tonic-gate pksi->ks_si.si_faddr_soa_len = so->so_faddr.soa_len; 22460Sstevel@tonic-gate pksi->ks_si.si_szoneid = so->so_zoneid; 22470Sstevel@tonic-gate 22480Sstevel@tonic-gate mutex_enter(&so->so_lock); 22490Sstevel@tonic-gate 22500Sstevel@tonic-gate if (so->so_laddr_sa != NULL) { 22510Sstevel@tonic-gate ASSERT(so->so_laddr_sa->sa_data != NULL); 22520Sstevel@tonic-gate sn_len = so->so_laddr_len; 22530Sstevel@tonic-gate ASSERT(sn_len <= sizeof (short) + 22540Sstevel@tonic-gate sizeof (pksi->ks_si.si_laddr_sun_path)); 22550Sstevel@tonic-gate 22560Sstevel@tonic-gate pksi->ks_si.si_laddr_family = 22570Sstevel@tonic-gate so->so_laddr_sa->sa_family; 22580Sstevel@tonic-gate if (sn_len != 0) { 22590Sstevel@tonic-gate /* AF_UNIX socket names are NULL terminated */ 22600Sstevel@tonic-gate (void) strncpy(pksi->ks_si.si_laddr_sun_path, 22610Sstevel@tonic-gate so->so_laddr_sa->sa_data, 22620Sstevel@tonic-gate sizeof (pksi->ks_si.si_laddr_sun_path)); 22630Sstevel@tonic-gate sn_len = strlen(pksi->ks_si.si_laddr_sun_path); 22640Sstevel@tonic-gate } 22650Sstevel@tonic-gate pksi->ks_si.si_laddr_sun_path[sn_len] = 0; 22660Sstevel@tonic-gate } 22670Sstevel@tonic-gate 22680Sstevel@tonic-gate if (so->so_faddr_sa != NULL) { 22690Sstevel@tonic-gate ASSERT(so->so_faddr_sa->sa_data != NULL); 22700Sstevel@tonic-gate sn_len = so->so_faddr_len; 22710Sstevel@tonic-gate ASSERT(sn_len <= sizeof (short) + 22720Sstevel@tonic-gate sizeof (pksi->ks_si.si_faddr_sun_path)); 22730Sstevel@tonic-gate 22740Sstevel@tonic-gate pksi->ks_si.si_faddr_family = 22750Sstevel@tonic-gate so->so_faddr_sa->sa_family; 22760Sstevel@tonic-gate if (sn_len != 0) { 22770Sstevel@tonic-gate (void) strncpy(pksi->ks_si.si_faddr_sun_path, 22780Sstevel@tonic-gate so->so_faddr_sa->sa_data, 22790Sstevel@tonic-gate sizeof (pksi->ks_si.si_faddr_sun_path)); 22800Sstevel@tonic-gate sn_len = strlen(pksi->ks_si.si_faddr_sun_path); 22810Sstevel@tonic-gate } 22820Sstevel@tonic-gate pksi->ks_si.si_faddr_sun_path[sn_len] = 0; 22830Sstevel@tonic-gate } 22840Sstevel@tonic-gate 22850Sstevel@tonic-gate mutex_exit(&so->so_lock); 22860Sstevel@tonic-gate 22870Sstevel@tonic-gate (void) sprintf(pksi->ks_straddr[0], "%p", (void *)so); 22880Sstevel@tonic-gate (void) sprintf(pksi->ks_straddr[1], "%p", 22890Sstevel@tonic-gate (void *)so->so_ux_laddr.soua_vp); 22900Sstevel@tonic-gate (void) sprintf(pksi->ks_straddr[2], "%p", 22910Sstevel@tonic-gate (void *)so->so_ux_faddr.soua_vp); 22920Sstevel@tonic-gate 22930Sstevel@tonic-gate ns++; 22940Sstevel@tonic-gate pksi++; 22950Sstevel@tonic-gate } 22960Sstevel@tonic-gate 22970Sstevel@tonic-gate ksp->ks_ndata = ns; 22980Sstevel@tonic-gate return (0); 22990Sstevel@tonic-gate } 23000Sstevel@tonic-gate 23010Sstevel@tonic-gate ssize_t 23020Sstevel@tonic-gate soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size) 23030Sstevel@tonic-gate { 23040Sstevel@tonic-gate struct uio auio; 23050Sstevel@tonic-gate struct iovec aiov[MSG_MAXIOVLEN]; 23060Sstevel@tonic-gate register vnode_t *vp; 23070Sstevel@tonic-gate int ioflag, rwflag; 23080Sstevel@tonic-gate ssize_t cnt; 23090Sstevel@tonic-gate int error = 0; 23100Sstevel@tonic-gate int iovcnt = 0; 23110Sstevel@tonic-gate short fflag; 23120Sstevel@tonic-gate 23130Sstevel@tonic-gate vp = fp->f_vnode; 23140Sstevel@tonic-gate fflag = fp->f_flag; 23150Sstevel@tonic-gate 23160Sstevel@tonic-gate rwflag = 0; 23170Sstevel@tonic-gate aiov[0].iov_base = (caddr_t)buf; 23180Sstevel@tonic-gate aiov[0].iov_len = size; 23190Sstevel@tonic-gate iovcnt = 1; 23200Sstevel@tonic-gate cnt = (ssize_t)size; 23210Sstevel@tonic-gate (void) VOP_RWLOCK(vp, rwflag, NULL); 23220Sstevel@tonic-gate 23230Sstevel@tonic-gate auio.uio_loffset = fileoff; 23240Sstevel@tonic-gate auio.uio_iov = aiov; 23250Sstevel@tonic-gate auio.uio_iovcnt = iovcnt; 23260Sstevel@tonic-gate auio.uio_resid = cnt; 23270Sstevel@tonic-gate auio.uio_segflg = UIO_SYSSPACE; 23280Sstevel@tonic-gate auio.uio_llimit = MAXOFFSET_T; 23290Sstevel@tonic-gate auio.uio_fmode = fflag; 23300Sstevel@tonic-gate auio.uio_extflg = UIO_COPY_CACHED; 23310Sstevel@tonic-gate 23320Sstevel@tonic-gate ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC); 23330Sstevel@tonic-gate 23340Sstevel@tonic-gate /* If read sync is not asked for, filter sync flags */ 23350Sstevel@tonic-gate if ((ioflag & FRSYNC) == 0) 23360Sstevel@tonic-gate ioflag &= ~(FSYNC|FDSYNC); 23370Sstevel@tonic-gate error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL); 23380Sstevel@tonic-gate cnt -= auio.uio_resid; 23390Sstevel@tonic-gate 23400Sstevel@tonic-gate VOP_RWUNLOCK(vp, rwflag, NULL); 23410Sstevel@tonic-gate 23420Sstevel@tonic-gate if (error == EINTR && cnt != 0) 23430Sstevel@tonic-gate error = 0; 23440Sstevel@tonic-gate out: 23450Sstevel@tonic-gate if (error != 0) { 23460Sstevel@tonic-gate *err = error; 23470Sstevel@tonic-gate return (0); 23480Sstevel@tonic-gate } else { 23490Sstevel@tonic-gate *err = 0; 23500Sstevel@tonic-gate return (cnt); 23510Sstevel@tonic-gate } 23520Sstevel@tonic-gate } 2353