17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 52caf0dcdSrshoaib * Common Development and Distribution License (the "License"). 62caf0dcdSrshoaib * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 212caf0dcdSrshoaib 227c478bd9Sstevel@tonic-gate /* 23*3e95bd4aSAnders Persson * Copyright (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 277c478bd9Sstevel@tonic-gate #include <sys/t_lock.h> 287c478bd9Sstevel@tonic-gate #include <sys/param.h> 297c478bd9Sstevel@tonic-gate #include <sys/systm.h> 307c478bd9Sstevel@tonic-gate #include <sys/buf.h> 317c478bd9Sstevel@tonic-gate #include <sys/conf.h> 327c478bd9Sstevel@tonic-gate #include <sys/cred.h> 337c478bd9Sstevel@tonic-gate #include <sys/kmem.h> 347c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 357c478bd9Sstevel@tonic-gate #include <sys/vfs.h> 36aa59c4cbSrsb #include <sys/vfs_opreg.h> 377c478bd9Sstevel@tonic-gate #include <sys/vnode.h> 387c478bd9Sstevel@tonic-gate #include <sys/debug.h> 397c478bd9Sstevel@tonic-gate #include <sys/errno.h> 407c478bd9Sstevel@tonic-gate #include <sys/time.h> 417c478bd9Sstevel@tonic-gate #include <sys/file.h> 427c478bd9Sstevel@tonic-gate #include <sys/open.h> 437c478bd9Sstevel@tonic-gate #include <sys/user.h> 447c478bd9Sstevel@tonic-gate #include <sys/termios.h> 457c478bd9Sstevel@tonic-gate #include <sys/stream.h> 467c478bd9Sstevel@tonic-gate #include <sys/strsubr.h> 477c478bd9Sstevel@tonic-gate #include <sys/strsun.h> 487c478bd9Sstevel@tonic-gate #include <sys/esunddi.h> 497c478bd9Sstevel@tonic-gate #include <sys/flock.h> 507c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 517c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h> 527c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 537c478bd9Sstevel@tonic-gate #include <sys/pathname.h> 547c478bd9Sstevel@tonic-gate #include <sys/ddi.h> 557c478bd9Sstevel@tonic-gate #include <sys/stat.h> 567c478bd9Sstevel@tonic-gate #include <sys/fs/snode.h> 577c478bd9Sstevel@tonic-gate #include <sys/fs/dv_node.h> 587c478bd9Sstevel@tonic-gate #include <sys/zone.h> 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate #include <sys/socket.h> 617c478bd9Sstevel@tonic-gate #include <sys/socketvar.h> 627c478bd9Sstevel@tonic-gate #include <netinet/in.h> 637c478bd9Sstevel@tonic-gate #include <sys/un.h> 647c478bd9Sstevel@tonic-gate #include <sys/ucred.h> 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #include <sys/tiuser.h> 677c478bd9Sstevel@tonic-gate #define _SUN_TPI_VERSION 2 687c478bd9Sstevel@tonic-gate #include <sys/tihdr.h> 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate #include <c2/audit.h> 717c478bd9Sstevel@tonic-gate 727c478bd9Sstevel@tonic-gate #include <fs/sockfs/nl7c.h> 730f1702c5SYu Xiangning #include <fs/sockfs/sockcommon.h> 74*3e95bd4aSAnders Persson #include <fs/sockfs/sockfilter_impl.h> 750f1702c5SYu Xiangning #include <fs/sockfs/socktpi.h> 760f1702c5SYu Xiangning #include <fs/sockfs/socktpi_impl.h> 77bbc000e5SAnders Persson #include <fs/sockfs/sodirect.h> 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate /* 807c478bd9Sstevel@tonic-gate * Macros that operate on struct cmsghdr. 817c478bd9Sstevel@tonic-gate * The CMSG_VALID macro does not assume that the last option buffer is padded. 827c478bd9Sstevel@tonic-gate */ 837c478bd9Sstevel@tonic-gate #define CMSG_CONTENT(cmsg) (&((cmsg)[1])) 847c478bd9Sstevel@tonic-gate #define CMSG_CONTENTLEN(cmsg) ((cmsg)->cmsg_len - sizeof (struct cmsghdr)) 857c478bd9Sstevel@tonic-gate #define CMSG_VALID(cmsg, start, end) \ 867c478bd9Sstevel@tonic-gate (ISALIGNED_cmsghdr(cmsg) && \ 877c478bd9Sstevel@tonic-gate ((uintptr_t)(cmsg) >= (uintptr_t)(start)) && \ 887c478bd9Sstevel@tonic-gate ((uintptr_t)(cmsg) < (uintptr_t)(end)) && \ 897c478bd9Sstevel@tonic-gate ((ssize_t)(cmsg)->cmsg_len >= sizeof (struct cmsghdr)) && \ 907c478bd9Sstevel@tonic-gate ((uintptr_t)(cmsg) + (cmsg)->cmsg_len <= (uintptr_t)(end))) 917c478bd9Sstevel@tonic-gate #define SO_LOCK_WAKEUP_TIME 3000 /* Wakeup time in milliseconds */ 927c478bd9Sstevel@tonic-gate 937c478bd9Sstevel@tonic-gate dev_t sockdev; /* For fsid in getattr */ 946cefaae1SJack Meng int sockfs_defer_nl7c_init = 0; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate struct socklist socklist; 977c478bd9Sstevel@tonic-gate 980f1702c5SYu Xiangning struct kmem_cache *socket_cache; 990f1702c5SYu Xiangning 100*3e95bd4aSAnders Persson /* 101*3e95bd4aSAnders Persson * sockconf_lock protects the socket configuration (socket types and 102*3e95bd4aSAnders Persson * socket filters) which is changed via the sockconfig system call. 103*3e95bd4aSAnders Persson */ 104*3e95bd4aSAnders Persson krwlock_t sockconf_lock; 105*3e95bd4aSAnders Persson 1067c478bd9Sstevel@tonic-gate static int sockfs_update(kstat_t *, int); 1077c478bd9Sstevel@tonic-gate static int sockfs_snapshot(kstat_t *, void *, int); 1080f1702c5SYu Xiangning extern smod_info_t *sotpi_smod_create(void); 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate extern void sendfile_init(); 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate extern void nl7c_init(void); 1137c478bd9Sstevel@tonic-gate 1146cefaae1SJack Meng extern int modrootloaded; 1156cefaae1SJack Meng 1167c478bd9Sstevel@tonic-gate #define ADRSTRLEN (2 * sizeof (void *) + 1) 1177c478bd9Sstevel@tonic-gate /* 1187c478bd9Sstevel@tonic-gate * kernel structure for passing the sockinfo data back up to the user. 1197c478bd9Sstevel@tonic-gate * the strings array allows us to convert AF_UNIX addresses into strings 1207c478bd9Sstevel@tonic-gate * with a common method regardless of which n-bit kernel we're running. 1217c478bd9Sstevel@tonic-gate */ 1227c478bd9Sstevel@tonic-gate struct k_sockinfo { 1237c478bd9Sstevel@tonic-gate struct sockinfo ks_si; 1247c478bd9Sstevel@tonic-gate char ks_straddr[3][ADRSTRLEN]; 1257c478bd9Sstevel@tonic-gate }; 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * Translate from a device pathname (e.g. "/dev/tcp") to a vnode. 1297c478bd9Sstevel@tonic-gate * Returns with the vnode held. 1307c478bd9Sstevel@tonic-gate */ 1310f1702c5SYu Xiangning int 1327c478bd9Sstevel@tonic-gate sogetvp(char *devpath, vnode_t **vpp, int uioflag) 1337c478bd9Sstevel@tonic-gate { 1347c478bd9Sstevel@tonic-gate struct snode *csp; 1357c478bd9Sstevel@tonic-gate vnode_t *vp, *dvp; 1367c478bd9Sstevel@tonic-gate major_t maj; 1377c478bd9Sstevel@tonic-gate int error; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate ASSERT(uioflag == UIO_SYSSPACE || uioflag == UIO_USERSPACE); 1400f1702c5SYu Xiangning 1417c478bd9Sstevel@tonic-gate /* 1427c478bd9Sstevel@tonic-gate * Lookup the underlying filesystem vnode. 1437c478bd9Sstevel@tonic-gate */ 1447c478bd9Sstevel@tonic-gate error = lookupname(devpath, uioflag, FOLLOW, NULLVPP, &vp); 1457c478bd9Sstevel@tonic-gate if (error) 1467c478bd9Sstevel@tonic-gate return (error); 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate /* Check that it is the correct vnode */ 1497c478bd9Sstevel@tonic-gate if (vp->v_type != VCHR) { 1507c478bd9Sstevel@tonic-gate VN_RELE(vp); 1517c478bd9Sstevel@tonic-gate return (ENOTSOCK); 1527c478bd9Sstevel@tonic-gate } 1537c478bd9Sstevel@tonic-gate 1547c478bd9Sstevel@tonic-gate /* 1557c478bd9Sstevel@tonic-gate * If devpath went through devfs, the device should already 1567c478bd9Sstevel@tonic-gate * be configured. If devpath is a mknod file, however, we 1577c478bd9Sstevel@tonic-gate * need to make sure the device is properly configured. 1587c478bd9Sstevel@tonic-gate * To do this, we do something similar to spec_open() 1597c478bd9Sstevel@tonic-gate * except that we resolve to the minor/leaf level since 1607c478bd9Sstevel@tonic-gate * we need to return a vnode. 1617c478bd9Sstevel@tonic-gate */ 1627c478bd9Sstevel@tonic-gate csp = VTOS(VTOS(vp)->s_commonvp); 1637c478bd9Sstevel@tonic-gate if (!(csp->s_flag & SDIPSET)) { 1647c478bd9Sstevel@tonic-gate char *pathname = kmem_alloc(MAXPATHLEN, KM_SLEEP); 1657c478bd9Sstevel@tonic-gate error = ddi_dev_pathname(vp->v_rdev, S_IFCHR, pathname); 1667c478bd9Sstevel@tonic-gate if (error == 0) 1677c478bd9Sstevel@tonic-gate error = devfs_lookupname(pathname, NULLVPP, &dvp); 1687c478bd9Sstevel@tonic-gate VN_RELE(vp); 1697c478bd9Sstevel@tonic-gate kmem_free(pathname, MAXPATHLEN); 1707c478bd9Sstevel@tonic-gate if (error != 0) 1717c478bd9Sstevel@tonic-gate return (ENXIO); 1727c478bd9Sstevel@tonic-gate vp = dvp; /* use the devfs vp */ 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate /* device is configured at this point */ 1767c478bd9Sstevel@tonic-gate maj = getmajor(vp->v_rdev); 1777c478bd9Sstevel@tonic-gate if (!STREAMSTAB(maj)) { 1787c478bd9Sstevel@tonic-gate VN_RELE(vp); 1797c478bd9Sstevel@tonic-gate return (ENOSTR); 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate *vpp = vp; 1837c478bd9Sstevel@tonic-gate return (0); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate 1867c478bd9Sstevel@tonic-gate /* 1877c478bd9Sstevel@tonic-gate * Update the accessed, updated, or changed times in an sonode 1887c478bd9Sstevel@tonic-gate * with the current time. 1897c478bd9Sstevel@tonic-gate * 1907c478bd9Sstevel@tonic-gate * Note that both SunOS 4.X and 4.4BSD sockets do not present reasonable 1917c478bd9Sstevel@tonic-gate * attributes in a fstat call. (They return the current time and 0 for 1927c478bd9Sstevel@tonic-gate * all timestamps, respectively.) We maintain the current timestamps 1937c478bd9Sstevel@tonic-gate * here primarily so that should sockmod be popped the resulting 1947c478bd9Sstevel@tonic-gate * file descriptor will behave like a stream w.r.t. the timestamps. 1957c478bd9Sstevel@tonic-gate */ 1967c478bd9Sstevel@tonic-gate void 1977c478bd9Sstevel@tonic-gate so_update_attrs(struct sonode *so, int flag) 1987c478bd9Sstevel@tonic-gate { 1997c478bd9Sstevel@tonic-gate time_t now = gethrestime_sec(); 2007c478bd9Sstevel@tonic-gate 2010f1702c5SYu Xiangning if (SOCK_IS_NONSTR(so)) 2020f1702c5SYu Xiangning return; 2030f1702c5SYu Xiangning 2047c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock); 2057c478bd9Sstevel@tonic-gate so->so_flag |= flag; 2067c478bd9Sstevel@tonic-gate if (flag & SOACC) 2070f1702c5SYu Xiangning SOTOTPI(so)->sti_atime = now; 2087c478bd9Sstevel@tonic-gate if (flag & SOMOD) 2090f1702c5SYu Xiangning SOTOTPI(so)->sti_mtime = now; 2107c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock); 2117c478bd9Sstevel@tonic-gate } 2127c478bd9Sstevel@tonic-gate 2130f1702c5SYu Xiangning extern so_create_func_t sock_comm_create_function; 2140f1702c5SYu Xiangning extern so_destroy_func_t sock_comm_destroy_function; 2157c478bd9Sstevel@tonic-gate /* 2167c478bd9Sstevel@tonic-gate * Init function called when sockfs is loaded. 2177c478bd9Sstevel@tonic-gate */ 2187c478bd9Sstevel@tonic-gate int 2197c478bd9Sstevel@tonic-gate sockinit(int fstype, char *name) 2207c478bd9Sstevel@tonic-gate { 2217c478bd9Sstevel@tonic-gate static const fs_operation_def_t sock_vfsops_template[] = { 2227c478bd9Sstevel@tonic-gate NULL, NULL 2237c478bd9Sstevel@tonic-gate }; 2247c478bd9Sstevel@tonic-gate int error; 2257c478bd9Sstevel@tonic-gate major_t dev; 2267c478bd9Sstevel@tonic-gate char *err_str; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate error = vfs_setfsops(fstype, sock_vfsops_template, NULL); 2297c478bd9Sstevel@tonic-gate if (error != 0) { 2302caf0dcdSrshoaib zcmn_err(GLOBAL_ZONEID, CE_WARN, 2312caf0dcdSrshoaib "sockinit: bad vfs ops template"); 2327c478bd9Sstevel@tonic-gate return (error); 2337c478bd9Sstevel@tonic-gate } 2347c478bd9Sstevel@tonic-gate 2350f1702c5SYu Xiangning error = vn_make_ops(name, socket_vnodeops_template, 2360f1702c5SYu Xiangning &socket_vnodeops); 2377c478bd9Sstevel@tonic-gate if (error != 0) { 2380f1702c5SYu Xiangning err_str = "sockinit: bad socket vnode ops template"; 2397c478bd9Sstevel@tonic-gate /* vn_make_ops() does not reset socktpi_vnodeops on failure. */ 2400f1702c5SYu Xiangning socket_vnodeops = NULL; 2417c478bd9Sstevel@tonic-gate goto failure; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2440f1702c5SYu Xiangning socket_cache = kmem_cache_create("socket_cache", 2450f1702c5SYu Xiangning sizeof (struct sonode), 0, sonode_constructor, 2460f1702c5SYu Xiangning sonode_destructor, NULL, NULL, NULL, 0); 2477c478bd9Sstevel@tonic-gate 248*3e95bd4aSAnders Persson rw_init(&sockconf_lock, NULL, RW_DEFAULT, NULL); 249*3e95bd4aSAnders Persson 2500f1702c5SYu Xiangning error = socktpi_init(); 25174e20cfeSnh145002 if (error != 0) { 25274e20cfeSnh145002 err_str = NULL; 25374e20cfeSnh145002 goto failure; 25474e20cfeSnh145002 } 25574e20cfeSnh145002 256bbc000e5SAnders Persson error = sod_init(); 25717169044Sbrutus if (error != 0) { 25817169044Sbrutus err_str = NULL; 25917169044Sbrutus goto failure; 26017169044Sbrutus } 26117169044Sbrutus 2627c478bd9Sstevel@tonic-gate /* 2630f1702c5SYu Xiangning * Set up the default create and destroy functions 2647c478bd9Sstevel@tonic-gate */ 2650f1702c5SYu Xiangning sock_comm_create_function = socket_sonode_create; 2660f1702c5SYu Xiangning sock_comm_destroy_function = socket_sonode_destroy; 2677c478bd9Sstevel@tonic-gate 2687c478bd9Sstevel@tonic-gate /* 2697c478bd9Sstevel@tonic-gate * Build initial list mapping socket parameters to vnode. 2707c478bd9Sstevel@tonic-gate */ 2710f1702c5SYu Xiangning smod_init(); 2720f1702c5SYu Xiangning smod_add(sotpi_smod_create()); 2730f1702c5SYu Xiangning 2740f1702c5SYu Xiangning sockparams_init(); 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate /* 2777c478bd9Sstevel@tonic-gate * If sockets are needed before init runs /sbin/soconfig 2787c478bd9Sstevel@tonic-gate * it is possible to preload the sockparams list here using 2797c478bd9Sstevel@tonic-gate * calls like: 2807c478bd9Sstevel@tonic-gate * sockconfig(1,2,3, "/dev/tcp", 0); 2817c478bd9Sstevel@tonic-gate */ 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate /* 2847c478bd9Sstevel@tonic-gate * Create a unique dev_t for use in so_fsid. 2857c478bd9Sstevel@tonic-gate */ 2867c478bd9Sstevel@tonic-gate 2877c478bd9Sstevel@tonic-gate if ((dev = getudev()) == (major_t)-1) 2887c478bd9Sstevel@tonic-gate dev = 0; 2897c478bd9Sstevel@tonic-gate sockdev = makedevice(dev, 0); 2907c478bd9Sstevel@tonic-gate 2917c478bd9Sstevel@tonic-gate mutex_init(&socklist.sl_lock, NULL, MUTEX_DEFAULT, NULL); 2927c478bd9Sstevel@tonic-gate sendfile_init(); 2936cefaae1SJack Meng if (!modrootloaded) { 2946cefaae1SJack Meng sockfs_defer_nl7c_init = 1; 2956cefaae1SJack Meng } else { 2967c478bd9Sstevel@tonic-gate nl7c_init(); 2976cefaae1SJack Meng } 2987c478bd9Sstevel@tonic-gate 299*3e95bd4aSAnders Persson /* Initialize socket filters */ 300*3e95bd4aSAnders Persson sof_init(); 301*3e95bd4aSAnders Persson 3027c478bd9Sstevel@tonic-gate return (0); 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate failure: 3057c478bd9Sstevel@tonic-gate (void) vfs_freevfsops_by_type(fstype); 3060f1702c5SYu Xiangning if (socket_vnodeops != NULL) 3070f1702c5SYu Xiangning vn_freevnodeops(socket_vnodeops); 3087c478bd9Sstevel@tonic-gate if (err_str != NULL) 3092caf0dcdSrshoaib zcmn_err(GLOBAL_ZONEID, CE_WARN, err_str); 3107c478bd9Sstevel@tonic-gate return (error); 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate /* 3147c478bd9Sstevel@tonic-gate * Caller must hold the mutex. Used to set SOLOCKED. 3157c478bd9Sstevel@tonic-gate */ 3167c478bd9Sstevel@tonic-gate void 3177c478bd9Sstevel@tonic-gate so_lock_single(struct sonode *so) 3187c478bd9Sstevel@tonic-gate { 3197c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 3207c478bd9Sstevel@tonic-gate 3217c478bd9Sstevel@tonic-gate while (so->so_flag & (SOLOCKED | SOASYNC_UNBIND)) { 3226a571a2dSAnders Persson cv_wait_stop(&so->so_single_cv, &so->so_lock, 3237c478bd9Sstevel@tonic-gate SO_LOCK_WAKEUP_TIME); 3247c478bd9Sstevel@tonic-gate } 3257c478bd9Sstevel@tonic-gate so->so_flag |= SOLOCKED; 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate /* 3297c478bd9Sstevel@tonic-gate * Caller must hold the mutex and pass in SOLOCKED or SOASYNC_UNBIND. 3307c478bd9Sstevel@tonic-gate * Used to clear SOLOCKED or SOASYNC_UNBIND. 3317c478bd9Sstevel@tonic-gate */ 3327c478bd9Sstevel@tonic-gate void 3337c478bd9Sstevel@tonic-gate so_unlock_single(struct sonode *so, int flag) 3347c478bd9Sstevel@tonic-gate { 3357c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 3367c478bd9Sstevel@tonic-gate ASSERT(flag & (SOLOCKED|SOASYNC_UNBIND)); 3377c478bd9Sstevel@tonic-gate ASSERT((flag & ~(SOLOCKED|SOASYNC_UNBIND)) == 0); 3387c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & flag); 3397c478bd9Sstevel@tonic-gate /* 3400f1702c5SYu Xiangning * Process the T_DISCON_IND on sti_discon_ind_mp. 3417c478bd9Sstevel@tonic-gate * 3427c478bd9Sstevel@tonic-gate * Call to so_drain_discon_ind will result in so_lock 3437c478bd9Sstevel@tonic-gate * being dropped and re-acquired later. 3447c478bd9Sstevel@tonic-gate */ 3450f1702c5SYu Xiangning if (!SOCK_IS_NONSTR(so)) { 3460f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so); 3470f1702c5SYu Xiangning 3480f1702c5SYu Xiangning if (sti->sti_discon_ind_mp != NULL) 3497c478bd9Sstevel@tonic-gate so_drain_discon_ind(so); 3500f1702c5SYu Xiangning } 3517c478bd9Sstevel@tonic-gate 3526a571a2dSAnders Persson cv_signal(&so->so_single_cv); 3536a571a2dSAnders Persson so->so_flag &= ~flag; 3547c478bd9Sstevel@tonic-gate } 3557c478bd9Sstevel@tonic-gate 3567c478bd9Sstevel@tonic-gate /* 3577c478bd9Sstevel@tonic-gate * Caller must hold the mutex. Used to set SOREADLOCKED. 3587c478bd9Sstevel@tonic-gate * If the caller wants nonblocking behavior it should set fmode. 3597c478bd9Sstevel@tonic-gate */ 3607c478bd9Sstevel@tonic-gate int 3617c478bd9Sstevel@tonic-gate so_lock_read(struct sonode *so, int fmode) 3627c478bd9Sstevel@tonic-gate { 3637c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate while (so->so_flag & SOREADLOCKED) { 3667c478bd9Sstevel@tonic-gate if (fmode & (FNDELAY|FNONBLOCK)) 3677c478bd9Sstevel@tonic-gate return (EWOULDBLOCK); 3686a571a2dSAnders Persson cv_wait_stop(&so->so_read_cv, &so->so_lock, 3697c478bd9Sstevel@tonic-gate SO_LOCK_WAKEUP_TIME); 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate so->so_flag |= SOREADLOCKED; 3727c478bd9Sstevel@tonic-gate return (0); 3737c478bd9Sstevel@tonic-gate } 3747c478bd9Sstevel@tonic-gate 3757c478bd9Sstevel@tonic-gate /* 3767c478bd9Sstevel@tonic-gate * Like so_lock_read above but allows signals. 3777c478bd9Sstevel@tonic-gate */ 3787c478bd9Sstevel@tonic-gate int 3797c478bd9Sstevel@tonic-gate so_lock_read_intr(struct sonode *so, int fmode) 3807c478bd9Sstevel@tonic-gate { 3817c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 3827c478bd9Sstevel@tonic-gate 3837c478bd9Sstevel@tonic-gate while (so->so_flag & SOREADLOCKED) { 3847c478bd9Sstevel@tonic-gate if (fmode & (FNDELAY|FNONBLOCK)) 3857c478bd9Sstevel@tonic-gate return (EWOULDBLOCK); 3866a571a2dSAnders Persson if (!cv_wait_sig(&so->so_read_cv, &so->so_lock)) 3877c478bd9Sstevel@tonic-gate return (EINTR); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate so->so_flag |= SOREADLOCKED; 3907c478bd9Sstevel@tonic-gate return (0); 3917c478bd9Sstevel@tonic-gate } 3927c478bd9Sstevel@tonic-gate 3937c478bd9Sstevel@tonic-gate /* 3947c478bd9Sstevel@tonic-gate * Caller must hold the mutex. Used to clear SOREADLOCKED, 3957c478bd9Sstevel@tonic-gate * set in so_lock_read() or so_lock_read_intr(). 3967c478bd9Sstevel@tonic-gate */ 3977c478bd9Sstevel@tonic-gate void 3987c478bd9Sstevel@tonic-gate so_unlock_read(struct sonode *so) 3997c478bd9Sstevel@tonic-gate { 4007c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 4017c478bd9Sstevel@tonic-gate ASSERT(so->so_flag & SOREADLOCKED); 4027c478bd9Sstevel@tonic-gate 4036a571a2dSAnders Persson cv_signal(&so->so_read_cv); 4046a571a2dSAnders Persson so->so_flag &= ~SOREADLOCKED; 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate 4077c478bd9Sstevel@tonic-gate /* 4087c478bd9Sstevel@tonic-gate * Verify that the specified offset falls within the mblk and 4097c478bd9Sstevel@tonic-gate * that the resulting pointer is aligned. 4107c478bd9Sstevel@tonic-gate * Returns NULL if not. 4117c478bd9Sstevel@tonic-gate */ 4127c478bd9Sstevel@tonic-gate void * 4137c478bd9Sstevel@tonic-gate sogetoff(mblk_t *mp, t_uscalar_t offset, 4147c478bd9Sstevel@tonic-gate t_uscalar_t length, uint_t align_size) 4157c478bd9Sstevel@tonic-gate { 4167c478bd9Sstevel@tonic-gate uintptr_t ptr1, ptr2; 4177c478bd9Sstevel@tonic-gate 4187c478bd9Sstevel@tonic-gate ASSERT(mp && mp->b_wptr >= mp->b_rptr); 4197c478bd9Sstevel@tonic-gate ptr1 = (uintptr_t)mp->b_rptr + offset; 4207c478bd9Sstevel@tonic-gate ptr2 = (uintptr_t)ptr1 + length; 4217c478bd9Sstevel@tonic-gate if (ptr1 < (uintptr_t)mp->b_rptr || ptr2 > (uintptr_t)mp->b_wptr) { 4227c478bd9Sstevel@tonic-gate eprintline(0); 4237c478bd9Sstevel@tonic-gate return (NULL); 4247c478bd9Sstevel@tonic-gate } 4257c478bd9Sstevel@tonic-gate if ((ptr1 & (align_size - 1)) != 0) { 4267c478bd9Sstevel@tonic-gate eprintline(0); 4277c478bd9Sstevel@tonic-gate return (NULL); 4287c478bd9Sstevel@tonic-gate } 4297c478bd9Sstevel@tonic-gate return ((void *)ptr1); 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate 4327c478bd9Sstevel@tonic-gate /* 4337c478bd9Sstevel@tonic-gate * Return the AF_UNIX underlying filesystem vnode matching a given name. 4347c478bd9Sstevel@tonic-gate * Makes sure the sending and the destination sonodes are compatible. 4357c478bd9Sstevel@tonic-gate * The vnode is returned held. 4367c478bd9Sstevel@tonic-gate * 4377c478bd9Sstevel@tonic-gate * The underlying filesystem VSOCK vnode has a v_stream pointer that 4387c478bd9Sstevel@tonic-gate * references the actual stream head (hence indirectly the actual sonode). 4397c478bd9Sstevel@tonic-gate */ 4407c478bd9Sstevel@tonic-gate static int 4417c478bd9Sstevel@tonic-gate so_ux_lookup(struct sonode *so, struct sockaddr_un *soun, int checkaccess, 4427c478bd9Sstevel@tonic-gate vnode_t **vpp) 4437c478bd9Sstevel@tonic-gate { 4447c478bd9Sstevel@tonic-gate vnode_t *vp; /* Underlying filesystem vnode */ 4459bf9355bSRic Aleshire vnode_t *rvp; /* real vnode */ 4467c478bd9Sstevel@tonic-gate vnode_t *svp; /* sockfs vnode */ 4477c478bd9Sstevel@tonic-gate struct sonode *so2; 4487c478bd9Sstevel@tonic-gate int error; 4497c478bd9Sstevel@tonic-gate 4503eceedbbSrh87107 dprintso(so, 1, ("so_ux_lookup(%p) name <%s>\n", (void *)so, 4513eceedbbSrh87107 soun->sun_path)); 4527c478bd9Sstevel@tonic-gate 4537c478bd9Sstevel@tonic-gate error = lookupname(soun->sun_path, UIO_SYSSPACE, FOLLOW, NULLVPP, &vp); 4547c478bd9Sstevel@tonic-gate if (error) { 4557c478bd9Sstevel@tonic-gate eprintsoline(so, error); 4567c478bd9Sstevel@tonic-gate return (error); 4577c478bd9Sstevel@tonic-gate } 4589bf9355bSRic Aleshire 4599bf9355bSRic Aleshire /* 4609bf9355bSRic Aleshire * Traverse lofs mounts get the real vnode 4619bf9355bSRic Aleshire */ 4629bf9355bSRic Aleshire if (VOP_REALVP(vp, &rvp, NULL) == 0) { 4639bf9355bSRic Aleshire VN_HOLD(rvp); /* hold the real vnode */ 4649bf9355bSRic Aleshire VN_RELE(vp); /* release hold from lookup */ 4659bf9355bSRic Aleshire vp = rvp; 4669bf9355bSRic Aleshire } 4679bf9355bSRic Aleshire 4687c478bd9Sstevel@tonic-gate if (vp->v_type != VSOCK) { 4697c478bd9Sstevel@tonic-gate error = ENOTSOCK; 4707c478bd9Sstevel@tonic-gate eprintsoline(so, error); 4717c478bd9Sstevel@tonic-gate goto done2; 4727c478bd9Sstevel@tonic-gate } 4737c478bd9Sstevel@tonic-gate 4747c478bd9Sstevel@tonic-gate if (checkaccess) { 4757c478bd9Sstevel@tonic-gate /* 4767c478bd9Sstevel@tonic-gate * Check that we have permissions to access the destination 4777c478bd9Sstevel@tonic-gate * vnode. This check is not done in BSD but it is required 4787c478bd9Sstevel@tonic-gate * by X/Open. 4797c478bd9Sstevel@tonic-gate */ 480da6c28aaSamw if (error = VOP_ACCESS(vp, VREAD|VWRITE, 0, CRED(), NULL)) { 4817c478bd9Sstevel@tonic-gate eprintsoline(so, error); 4827c478bd9Sstevel@tonic-gate goto done2; 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate /* 4877c478bd9Sstevel@tonic-gate * Check if the remote socket has been closed. 4887c478bd9Sstevel@tonic-gate * 4897c478bd9Sstevel@tonic-gate * Synchronize with vn_rele_stream by holding v_lock while traversing 4907c478bd9Sstevel@tonic-gate * v_stream->sd_vnode. 4917c478bd9Sstevel@tonic-gate */ 4927c478bd9Sstevel@tonic-gate mutex_enter(&vp->v_lock); 4937c478bd9Sstevel@tonic-gate if (vp->v_stream == NULL) { 4947c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 4957c478bd9Sstevel@tonic-gate if (so->so_type == SOCK_DGRAM) 4967c478bd9Sstevel@tonic-gate error = EDESTADDRREQ; 4977c478bd9Sstevel@tonic-gate else 4987c478bd9Sstevel@tonic-gate error = ECONNREFUSED; 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate eprintsoline(so, error); 5017c478bd9Sstevel@tonic-gate goto done2; 5027c478bd9Sstevel@tonic-gate } 5037c478bd9Sstevel@tonic-gate ASSERT(vp->v_stream->sd_vnode); 5047c478bd9Sstevel@tonic-gate svp = vp->v_stream->sd_vnode; 5057c478bd9Sstevel@tonic-gate /* 5067c478bd9Sstevel@tonic-gate * holding v_lock on underlying filesystem vnode and acquiring 5077c478bd9Sstevel@tonic-gate * it on sockfs vnode. Assumes that no code ever attempts to 5087c478bd9Sstevel@tonic-gate * acquire these locks in the reverse order. 5097c478bd9Sstevel@tonic-gate */ 5107c478bd9Sstevel@tonic-gate VN_HOLD(svp); 5117c478bd9Sstevel@tonic-gate mutex_exit(&vp->v_lock); 5127c478bd9Sstevel@tonic-gate 5137c478bd9Sstevel@tonic-gate if (svp->v_type != VSOCK) { 5147c478bd9Sstevel@tonic-gate error = ENOTSOCK; 5157c478bd9Sstevel@tonic-gate eprintsoline(so, error); 5167c478bd9Sstevel@tonic-gate goto done; 5177c478bd9Sstevel@tonic-gate } 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate so2 = VTOSO(svp); 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate if (so->so_type != so2->so_type) { 5227c478bd9Sstevel@tonic-gate error = EPROTOTYPE; 5237c478bd9Sstevel@tonic-gate eprintsoline(so, error); 5247c478bd9Sstevel@tonic-gate goto done; 5257c478bd9Sstevel@tonic-gate } 5267c478bd9Sstevel@tonic-gate 5277c478bd9Sstevel@tonic-gate VN_RELE(svp); 5287c478bd9Sstevel@tonic-gate *vpp = vp; 5297c478bd9Sstevel@tonic-gate return (0); 5307c478bd9Sstevel@tonic-gate 5317c478bd9Sstevel@tonic-gate done: 5327c478bd9Sstevel@tonic-gate VN_RELE(svp); 5337c478bd9Sstevel@tonic-gate done2: 5347c478bd9Sstevel@tonic-gate VN_RELE(vp); 5357c478bd9Sstevel@tonic-gate return (error); 5367c478bd9Sstevel@tonic-gate } 5377c478bd9Sstevel@tonic-gate 5387c478bd9Sstevel@tonic-gate /* 5397c478bd9Sstevel@tonic-gate * Verify peer address for connect and sendto/sendmsg. 5407c478bd9Sstevel@tonic-gate * Since sendto/sendmsg would not get synchronous errors from the transport 5417c478bd9Sstevel@tonic-gate * provider we have to do these ugly checks in the socket layer to 5427c478bd9Sstevel@tonic-gate * preserve compatibility with SunOS 4.X. 5437c478bd9Sstevel@tonic-gate */ 5447c478bd9Sstevel@tonic-gate int 5457c478bd9Sstevel@tonic-gate so_addr_verify(struct sonode *so, const struct sockaddr *name, 5467c478bd9Sstevel@tonic-gate socklen_t namelen) 5477c478bd9Sstevel@tonic-gate { 5487c478bd9Sstevel@tonic-gate int family; 5497c478bd9Sstevel@tonic-gate 550903a11ebSrh87107 dprintso(so, 1, ("so_addr_verify(%p, %p, %d)\n", 551903a11ebSrh87107 (void *)so, (void *)name, namelen)); 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate ASSERT(name != NULL); 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate family = so->so_family; 5567c478bd9Sstevel@tonic-gate switch (family) { 5577c478bd9Sstevel@tonic-gate case AF_INET: 5587c478bd9Sstevel@tonic-gate if (name->sa_family != family) { 5597c478bd9Sstevel@tonic-gate eprintsoline(so, EAFNOSUPPORT); 5607c478bd9Sstevel@tonic-gate return (EAFNOSUPPORT); 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate if (namelen != (socklen_t)sizeof (struct sockaddr_in)) { 5637c478bd9Sstevel@tonic-gate eprintsoline(so, EINVAL); 5647c478bd9Sstevel@tonic-gate return (EINVAL); 5657c478bd9Sstevel@tonic-gate } 5667c478bd9Sstevel@tonic-gate break; 5677c478bd9Sstevel@tonic-gate case AF_INET6: { 5687c478bd9Sstevel@tonic-gate #ifdef DEBUG 5697c478bd9Sstevel@tonic-gate struct sockaddr_in6 *sin6; 5707c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 5717c478bd9Sstevel@tonic-gate 5727c478bd9Sstevel@tonic-gate if (name->sa_family != family) { 5737c478bd9Sstevel@tonic-gate eprintsoline(so, EAFNOSUPPORT); 5747c478bd9Sstevel@tonic-gate return (EAFNOSUPPORT); 5757c478bd9Sstevel@tonic-gate } 5767c478bd9Sstevel@tonic-gate if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) { 5777c478bd9Sstevel@tonic-gate eprintsoline(so, EINVAL); 5787c478bd9Sstevel@tonic-gate return (EINVAL); 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate #ifdef DEBUG 5817c478bd9Sstevel@tonic-gate /* Verify that apps don't forget to clear sin6_scope_id etc */ 5827c478bd9Sstevel@tonic-gate sin6 = (struct sockaddr_in6 *)name; 5837c478bd9Sstevel@tonic-gate if (sin6->sin6_scope_id != 0 && 5847c478bd9Sstevel@tonic-gate !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) { 5852caf0dcdSrshoaib zcmn_err(getzoneid(), CE_WARN, 5867c478bd9Sstevel@tonic-gate "connect/send* with uninitialized sin6_scope_id " 5877c478bd9Sstevel@tonic-gate "(%d) on socket. Pid = %d\n", 5887c478bd9Sstevel@tonic-gate (int)sin6->sin6_scope_id, (int)curproc->p_pid); 5897c478bd9Sstevel@tonic-gate } 5907c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 5917c478bd9Sstevel@tonic-gate break; 5927c478bd9Sstevel@tonic-gate } 5937c478bd9Sstevel@tonic-gate case AF_UNIX: 5940f1702c5SYu Xiangning if (SOTOTPI(so)->sti_faddr_noxlate) { 5957c478bd9Sstevel@tonic-gate return (0); 5967c478bd9Sstevel@tonic-gate } 5977c478bd9Sstevel@tonic-gate if (namelen < (socklen_t)sizeof (short)) { 5987c478bd9Sstevel@tonic-gate eprintsoline(so, ENOENT); 5997c478bd9Sstevel@tonic-gate return (ENOENT); 6007c478bd9Sstevel@tonic-gate } 6017c478bd9Sstevel@tonic-gate if (name->sa_family != family) { 6027c478bd9Sstevel@tonic-gate eprintsoline(so, EAFNOSUPPORT); 6037c478bd9Sstevel@tonic-gate return (EAFNOSUPPORT); 6047c478bd9Sstevel@tonic-gate } 6057c478bd9Sstevel@tonic-gate /* MAXPATHLEN + soun_family + nul termination */ 6067c478bd9Sstevel@tonic-gate if (namelen > (socklen_t)(MAXPATHLEN + sizeof (short) + 1)) { 6077c478bd9Sstevel@tonic-gate eprintsoline(so, ENAMETOOLONG); 6087c478bd9Sstevel@tonic-gate return (ENAMETOOLONG); 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate 6117c478bd9Sstevel@tonic-gate break; 6127c478bd9Sstevel@tonic-gate 6137c478bd9Sstevel@tonic-gate default: 6147c478bd9Sstevel@tonic-gate /* 6157c478bd9Sstevel@tonic-gate * Default is don't do any length or sa_family check 6167c478bd9Sstevel@tonic-gate * to allow non-sockaddr style addresses. 6177c478bd9Sstevel@tonic-gate */ 6187c478bd9Sstevel@tonic-gate break; 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate return (0); 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate 6247c478bd9Sstevel@tonic-gate 6257c478bd9Sstevel@tonic-gate /* 6267c478bd9Sstevel@tonic-gate * Translate an AF_UNIX sockaddr_un to the transport internal name. 6277c478bd9Sstevel@tonic-gate * Assumes caller has called so_addr_verify first. 6287c478bd9Sstevel@tonic-gate */ 6297c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 6307c478bd9Sstevel@tonic-gate int 6317c478bd9Sstevel@tonic-gate so_ux_addr_xlate(struct sonode *so, struct sockaddr *name, 6327c478bd9Sstevel@tonic-gate socklen_t namelen, int checkaccess, 6337c478bd9Sstevel@tonic-gate void **addrp, socklen_t *addrlenp) 6347c478bd9Sstevel@tonic-gate { 6357c478bd9Sstevel@tonic-gate int error; 6367c478bd9Sstevel@tonic-gate struct sockaddr_un *soun; 6377c478bd9Sstevel@tonic-gate vnode_t *vp; 6387c478bd9Sstevel@tonic-gate void *addr; 6397c478bd9Sstevel@tonic-gate socklen_t addrlen; 6400f1702c5SYu Xiangning sotpi_info_t *sti = SOTOTPI(so); 6417c478bd9Sstevel@tonic-gate 6427c478bd9Sstevel@tonic-gate dprintso(so, 1, ("so_ux_addr_xlate(%p, %p, %d, %d)\n", 643903a11ebSrh87107 (void *)so, (void *)name, namelen, checkaccess)); 6447c478bd9Sstevel@tonic-gate 6457c478bd9Sstevel@tonic-gate ASSERT(name != NULL); 6467c478bd9Sstevel@tonic-gate ASSERT(so->so_family == AF_UNIX); 6470f1702c5SYu Xiangning ASSERT(!sti->sti_faddr_noxlate); 6487c478bd9Sstevel@tonic-gate ASSERT(namelen >= (socklen_t)sizeof (short)); 6497c478bd9Sstevel@tonic-gate ASSERT(name->sa_family == AF_UNIX); 6507c478bd9Sstevel@tonic-gate soun = (struct sockaddr_un *)name; 6517c478bd9Sstevel@tonic-gate /* 6527c478bd9Sstevel@tonic-gate * Lookup vnode for the specified path name and verify that 6537c478bd9Sstevel@tonic-gate * it is a socket. 6547c478bd9Sstevel@tonic-gate */ 6557c478bd9Sstevel@tonic-gate error = so_ux_lookup(so, soun, checkaccess, &vp); 6567c478bd9Sstevel@tonic-gate if (error) { 6577c478bd9Sstevel@tonic-gate eprintsoline(so, error); 6587c478bd9Sstevel@tonic-gate return (error); 6597c478bd9Sstevel@tonic-gate } 6607c478bd9Sstevel@tonic-gate /* 6617c478bd9Sstevel@tonic-gate * Use the address of the peer vnode as the address to send 6627c478bd9Sstevel@tonic-gate * to. We release the peer vnode here. In case it has been 6637c478bd9Sstevel@tonic-gate * closed by the time the T_CONN_REQ or T_UNIDATA_REQ reaches the 6647c478bd9Sstevel@tonic-gate * transport the message will get an error or be dropped. 6657c478bd9Sstevel@tonic-gate */ 6660f1702c5SYu Xiangning sti->sti_ux_faddr.soua_vp = vp; 6670f1702c5SYu Xiangning sti->sti_ux_faddr.soua_magic = SOU_MAGIC_EXPLICIT; 6680f1702c5SYu Xiangning addr = &sti->sti_ux_faddr; 6690f1702c5SYu Xiangning addrlen = (socklen_t)sizeof (sti->sti_ux_faddr); 670903a11ebSrh87107 dprintso(so, 1, ("ux_xlate UNIX: addrlen %d, vp %p\n", 671903a11ebSrh87107 addrlen, (void *)vp)); 6727c478bd9Sstevel@tonic-gate VN_RELE(vp); 6737c478bd9Sstevel@tonic-gate *addrp = addr; 6747c478bd9Sstevel@tonic-gate *addrlenp = (socklen_t)addrlen; 6757c478bd9Sstevel@tonic-gate return (0); 6767c478bd9Sstevel@tonic-gate } 6777c478bd9Sstevel@tonic-gate 6787c478bd9Sstevel@tonic-gate /* 6797c478bd9Sstevel@tonic-gate * Esballoc free function for messages that contain SO_FILEP option. 6807c478bd9Sstevel@tonic-gate * Decrement the reference count on the file pointers using closef. 6817c478bd9Sstevel@tonic-gate */ 6827c478bd9Sstevel@tonic-gate void 6837c478bd9Sstevel@tonic-gate fdbuf_free(struct fdbuf *fdbuf) 6847c478bd9Sstevel@tonic-gate { 6857c478bd9Sstevel@tonic-gate int i; 6867c478bd9Sstevel@tonic-gate struct file *fp; 6877c478bd9Sstevel@tonic-gate 6887c478bd9Sstevel@tonic-gate dprint(1, ("fdbuf_free: %d fds\n", fdbuf->fd_numfd)); 6897c478bd9Sstevel@tonic-gate for (i = 0; i < fdbuf->fd_numfd; i++) { 6907c478bd9Sstevel@tonic-gate /* 6917c478bd9Sstevel@tonic-gate * We need pointer size alignment for fd_fds. On a LP64 6927c478bd9Sstevel@tonic-gate * kernel, the required alignment is 8 bytes while 6937c478bd9Sstevel@tonic-gate * the option headers and values are only 4 bytes 6947c478bd9Sstevel@tonic-gate * aligned. So its safer to do a bcopy compared to 6957c478bd9Sstevel@tonic-gate * assigning fdbuf->fd_fds[i] to fp. 6967c478bd9Sstevel@tonic-gate */ 6977c478bd9Sstevel@tonic-gate bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp)); 698903a11ebSrh87107 dprint(1, ("fdbuf_free: [%d] = %p\n", i, (void *)fp)); 6997c478bd9Sstevel@tonic-gate (void) closef(fp); 7007c478bd9Sstevel@tonic-gate } 7017c478bd9Sstevel@tonic-gate if (fdbuf->fd_ebuf != NULL) 7027c478bd9Sstevel@tonic-gate kmem_free(fdbuf->fd_ebuf, fdbuf->fd_ebuflen); 7037c478bd9Sstevel@tonic-gate kmem_free(fdbuf, fdbuf->fd_size); 7047c478bd9Sstevel@tonic-gate } 7057c478bd9Sstevel@tonic-gate 7067c478bd9Sstevel@tonic-gate /* 707bd118333Smeem * Allocate an esballoc'ed message for AF_UNIX file descriptor passing. 708bd118333Smeem * Waits if memory is not available. 7097c478bd9Sstevel@tonic-gate */ 7107c478bd9Sstevel@tonic-gate mblk_t * 7117c478bd9Sstevel@tonic-gate fdbuf_allocmsg(int size, struct fdbuf *fdbuf) 7127c478bd9Sstevel@tonic-gate { 713bd118333Smeem uchar_t *buf; 7147c478bd9Sstevel@tonic-gate mblk_t *mp; 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate dprint(1, ("fdbuf_allocmsg: size %d, %d fds\n", size, fdbuf->fd_numfd)); 7177c478bd9Sstevel@tonic-gate buf = kmem_alloc(size, KM_SLEEP); 7187c478bd9Sstevel@tonic-gate fdbuf->fd_ebuf = (caddr_t)buf; 7197c478bd9Sstevel@tonic-gate fdbuf->fd_ebuflen = size; 7207c478bd9Sstevel@tonic-gate fdbuf->fd_frtn.free_func = fdbuf_free; 7217c478bd9Sstevel@tonic-gate fdbuf->fd_frtn.free_arg = (caddr_t)fdbuf; 7227c478bd9Sstevel@tonic-gate 723bd118333Smeem mp = esballoc_wait(buf, size, BPRI_MED, &fdbuf->fd_frtn); 7247c478bd9Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO; 7257c478bd9Sstevel@tonic-gate return (mp); 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate /* 7297c478bd9Sstevel@tonic-gate * Extract file descriptors from a fdbuf. 7307c478bd9Sstevel@tonic-gate * Return list in rights/rightslen. 7317c478bd9Sstevel@tonic-gate */ 7327c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 7337c478bd9Sstevel@tonic-gate static int 7347c478bd9Sstevel@tonic-gate fdbuf_extract(struct fdbuf *fdbuf, void *rights, int rightslen) 7357c478bd9Sstevel@tonic-gate { 7367c478bd9Sstevel@tonic-gate int i, fd; 7377c478bd9Sstevel@tonic-gate int *rp; 7387c478bd9Sstevel@tonic-gate struct file *fp; 7397c478bd9Sstevel@tonic-gate int numfd; 7407c478bd9Sstevel@tonic-gate 7417c478bd9Sstevel@tonic-gate dprint(1, ("fdbuf_extract: %d fds, len %d\n", 7427c478bd9Sstevel@tonic-gate fdbuf->fd_numfd, rightslen)); 7437c478bd9Sstevel@tonic-gate 7447c478bd9Sstevel@tonic-gate numfd = fdbuf->fd_numfd; 7457c478bd9Sstevel@tonic-gate ASSERT(rightslen == numfd * (int)sizeof (int)); 7467c478bd9Sstevel@tonic-gate 7477c478bd9Sstevel@tonic-gate /* 7487c478bd9Sstevel@tonic-gate * Allocate a file descriptor and increment the f_count. 7497c478bd9Sstevel@tonic-gate * The latter is needed since we always call fdbuf_free 7507c478bd9Sstevel@tonic-gate * which performs a closef. 7517c478bd9Sstevel@tonic-gate */ 7527c478bd9Sstevel@tonic-gate rp = (int *)rights; 7537c478bd9Sstevel@tonic-gate for (i = 0; i < numfd; i++) { 7547c478bd9Sstevel@tonic-gate if ((fd = ufalloc(0)) == -1) 7557c478bd9Sstevel@tonic-gate goto cleanup; 7567c478bd9Sstevel@tonic-gate /* 7577c478bd9Sstevel@tonic-gate * We need pointer size alignment for fd_fds. On a LP64 7587c478bd9Sstevel@tonic-gate * kernel, the required alignment is 8 bytes while 7597c478bd9Sstevel@tonic-gate * the option headers and values are only 4 bytes 7607c478bd9Sstevel@tonic-gate * aligned. So its safer to do a bcopy compared to 7617c478bd9Sstevel@tonic-gate * assigning fdbuf->fd_fds[i] to fp. 7627c478bd9Sstevel@tonic-gate */ 7637c478bd9Sstevel@tonic-gate bcopy((char *)&fdbuf->fd_fds[i], (char *)&fp, sizeof (fp)); 7647c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock); 7657c478bd9Sstevel@tonic-gate fp->f_count++; 7667c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 7677c478bd9Sstevel@tonic-gate setf(fd, fp); 7687c478bd9Sstevel@tonic-gate *rp++ = fd; 769005d3febSMarek Pospisil if (AU_AUDITING()) 7707c478bd9Sstevel@tonic-gate audit_fdrecv(fd, fp); 7717c478bd9Sstevel@tonic-gate dprint(1, ("fdbuf_extract: [%d] = %d, %p refcnt %d\n", 772903a11ebSrh87107 i, fd, (void *)fp, fp->f_count)); 7737c478bd9Sstevel@tonic-gate } 7747c478bd9Sstevel@tonic-gate return (0); 7757c478bd9Sstevel@tonic-gate 7767c478bd9Sstevel@tonic-gate cleanup: 7777c478bd9Sstevel@tonic-gate /* 7787c478bd9Sstevel@tonic-gate * Undo whatever partial work the loop above has done. 7797c478bd9Sstevel@tonic-gate */ 7807c478bd9Sstevel@tonic-gate { 7817c478bd9Sstevel@tonic-gate int j; 7827c478bd9Sstevel@tonic-gate 7837c478bd9Sstevel@tonic-gate rp = (int *)rights; 7847c478bd9Sstevel@tonic-gate for (j = 0; j < i; j++) { 7857c478bd9Sstevel@tonic-gate dprint(0, 7867c478bd9Sstevel@tonic-gate ("fdbuf_extract: cleanup[%d] = %d\n", j, *rp)); 7877c478bd9Sstevel@tonic-gate (void) closeandsetf(*rp++, NULL); 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate } 7907c478bd9Sstevel@tonic-gate 7917c478bd9Sstevel@tonic-gate return (EMFILE); 7927c478bd9Sstevel@tonic-gate } 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate /* 7957c478bd9Sstevel@tonic-gate * Insert file descriptors into an fdbuf. 7967c478bd9Sstevel@tonic-gate * Returns a kmem_alloc'ed fdbuf. The fdbuf should be freed 7977c478bd9Sstevel@tonic-gate * by calling fdbuf_free(). 7987c478bd9Sstevel@tonic-gate */ 7997c478bd9Sstevel@tonic-gate int 8007c478bd9Sstevel@tonic-gate fdbuf_create(void *rights, int rightslen, struct fdbuf **fdbufp) 8017c478bd9Sstevel@tonic-gate { 8027c478bd9Sstevel@tonic-gate int numfd, i; 8037c478bd9Sstevel@tonic-gate int *fds; 8047c478bd9Sstevel@tonic-gate struct file *fp; 8057c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf; 8067c478bd9Sstevel@tonic-gate int fdbufsize; 8077c478bd9Sstevel@tonic-gate 8087c478bd9Sstevel@tonic-gate dprint(1, ("fdbuf_create: len %d\n", rightslen)); 8097c478bd9Sstevel@tonic-gate 8107c478bd9Sstevel@tonic-gate numfd = rightslen / (int)sizeof (int); 8117c478bd9Sstevel@tonic-gate 8127c478bd9Sstevel@tonic-gate fdbufsize = (int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *)); 8137c478bd9Sstevel@tonic-gate fdbuf = kmem_alloc(fdbufsize, KM_SLEEP); 8147c478bd9Sstevel@tonic-gate fdbuf->fd_size = fdbufsize; 8157c478bd9Sstevel@tonic-gate fdbuf->fd_numfd = 0; 8167c478bd9Sstevel@tonic-gate fdbuf->fd_ebuf = NULL; 8177c478bd9Sstevel@tonic-gate fdbuf->fd_ebuflen = 0; 8187c478bd9Sstevel@tonic-gate fds = (int *)rights; 8197c478bd9Sstevel@tonic-gate for (i = 0; i < numfd; i++) { 8207c478bd9Sstevel@tonic-gate if ((fp = getf(fds[i])) == NULL) { 8217c478bd9Sstevel@tonic-gate fdbuf_free(fdbuf); 8227c478bd9Sstevel@tonic-gate return (EBADF); 8237c478bd9Sstevel@tonic-gate } 8247c478bd9Sstevel@tonic-gate dprint(1, ("fdbuf_create: [%d] = %d, %p refcnt %d\n", 825903a11ebSrh87107 i, fds[i], (void *)fp, fp->f_count)); 8267c478bd9Sstevel@tonic-gate mutex_enter(&fp->f_tlock); 8277c478bd9Sstevel@tonic-gate fp->f_count++; 8287c478bd9Sstevel@tonic-gate mutex_exit(&fp->f_tlock); 8297c478bd9Sstevel@tonic-gate /* 8307c478bd9Sstevel@tonic-gate * The maximum alignment for fdbuf (or any option header 8317c478bd9Sstevel@tonic-gate * and its value) it 4 bytes. On a LP64 kernel, the alignment 8327c478bd9Sstevel@tonic-gate * is not sufficient for pointers (fd_fds in this case). Since 8337c478bd9Sstevel@tonic-gate * we just did a kmem_alloc (we get a double word alignment), 8347c478bd9Sstevel@tonic-gate * we don't need to do anything on the send side (we loose 8357c478bd9Sstevel@tonic-gate * the double word alignment because fdbuf goes after an 8367c478bd9Sstevel@tonic-gate * option header (eg T_unitdata_req) which is only 4 byte 8377c478bd9Sstevel@tonic-gate * aligned). We take care of this when we extract the file 8387c478bd9Sstevel@tonic-gate * descriptor in fdbuf_extract or fdbuf_free. 8397c478bd9Sstevel@tonic-gate */ 8407c478bd9Sstevel@tonic-gate fdbuf->fd_fds[i] = fp; 8417c478bd9Sstevel@tonic-gate fdbuf->fd_numfd++; 8427c478bd9Sstevel@tonic-gate releasef(fds[i]); 843005d3febSMarek Pospisil if (AU_AUDITING()) 8447c478bd9Sstevel@tonic-gate audit_fdsend(fds[i], fp, 0); 8457c478bd9Sstevel@tonic-gate } 8467c478bd9Sstevel@tonic-gate *fdbufp = fdbuf; 8477c478bd9Sstevel@tonic-gate return (0); 8487c478bd9Sstevel@tonic-gate } 8497c478bd9Sstevel@tonic-gate 8507c478bd9Sstevel@tonic-gate static int 8517c478bd9Sstevel@tonic-gate fdbuf_optlen(int rightslen) 8527c478bd9Sstevel@tonic-gate { 8537c478bd9Sstevel@tonic-gate int numfd; 8547c478bd9Sstevel@tonic-gate 8557c478bd9Sstevel@tonic-gate numfd = rightslen / (int)sizeof (int); 8567c478bd9Sstevel@tonic-gate 8577c478bd9Sstevel@tonic-gate return ((int)FDBUF_HDRSIZE + (numfd * (int)sizeof (struct file *))); 8587c478bd9Sstevel@tonic-gate } 8597c478bd9Sstevel@tonic-gate 8607c478bd9Sstevel@tonic-gate static t_uscalar_t 8617c478bd9Sstevel@tonic-gate fdbuf_cmsglen(int fdbuflen) 8627c478bd9Sstevel@tonic-gate { 8637c478bd9Sstevel@tonic-gate return (t_uscalar_t)((fdbuflen - FDBUF_HDRSIZE) / 8647c478bd9Sstevel@tonic-gate (int)sizeof (struct file *) * (int)sizeof (int)); 8657c478bd9Sstevel@tonic-gate } 8667c478bd9Sstevel@tonic-gate 8677c478bd9Sstevel@tonic-gate 8687c478bd9Sstevel@tonic-gate /* 8697c478bd9Sstevel@tonic-gate * Return non-zero if the mblk and fdbuf are consistent. 8707c478bd9Sstevel@tonic-gate */ 8717c478bd9Sstevel@tonic-gate static int 8727c478bd9Sstevel@tonic-gate fdbuf_verify(mblk_t *mp, struct fdbuf *fdbuf, int fdbuflen) 8737c478bd9Sstevel@tonic-gate { 8747c478bd9Sstevel@tonic-gate if (fdbuflen >= FDBUF_HDRSIZE && 8757c478bd9Sstevel@tonic-gate fdbuflen == fdbuf->fd_size) { 8767c478bd9Sstevel@tonic-gate frtn_t *frp = mp->b_datap->db_frtnp; 8777c478bd9Sstevel@tonic-gate /* 8787c478bd9Sstevel@tonic-gate * Check that the SO_FILEP portion of the 8797c478bd9Sstevel@tonic-gate * message has not been modified by 8807c478bd9Sstevel@tonic-gate * the loopback transport. The sending sockfs generates 8817c478bd9Sstevel@tonic-gate * a message that is esballoc'ed with the free function 8827c478bd9Sstevel@tonic-gate * being fdbuf_free() and where free_arg contains the 8837c478bd9Sstevel@tonic-gate * identical information as the SO_FILEP content. 8847c478bd9Sstevel@tonic-gate * 8857c478bd9Sstevel@tonic-gate * If any of these constraints are not satisfied we 8867c478bd9Sstevel@tonic-gate * silently ignore the option. 8877c478bd9Sstevel@tonic-gate */ 8887c478bd9Sstevel@tonic-gate ASSERT(mp); 8897c478bd9Sstevel@tonic-gate if (frp != NULL && 8907c478bd9Sstevel@tonic-gate frp->free_func == fdbuf_free && 8917c478bd9Sstevel@tonic-gate frp->free_arg != NULL && 8927c478bd9Sstevel@tonic-gate bcmp(frp->free_arg, fdbuf, fdbuflen) == 0) { 8937c478bd9Sstevel@tonic-gate dprint(1, ("fdbuf_verify: fdbuf %p len %d\n", 894903a11ebSrh87107 (void *)fdbuf, fdbuflen)); 8957c478bd9Sstevel@tonic-gate return (1); 8967c478bd9Sstevel@tonic-gate } else { 8972caf0dcdSrshoaib zcmn_err(getzoneid(), CE_WARN, 8987c478bd9Sstevel@tonic-gate "sockfs: mismatched fdbuf content (%p)", 8997c478bd9Sstevel@tonic-gate (void *)mp); 9007c478bd9Sstevel@tonic-gate return (0); 9017c478bd9Sstevel@tonic-gate } 9027c478bd9Sstevel@tonic-gate } else { 9032caf0dcdSrshoaib zcmn_err(getzoneid(), CE_WARN, 9047c478bd9Sstevel@tonic-gate "sockfs: mismatched fdbuf len %d, %d\n", 9057c478bd9Sstevel@tonic-gate fdbuflen, fdbuf->fd_size); 9067c478bd9Sstevel@tonic-gate return (0); 9077c478bd9Sstevel@tonic-gate } 9087c478bd9Sstevel@tonic-gate } 9097c478bd9Sstevel@tonic-gate 9107c478bd9Sstevel@tonic-gate /* 9117c478bd9Sstevel@tonic-gate * When the file descriptors returned by sorecvmsg can not be passed 9127c478bd9Sstevel@tonic-gate * to the application this routine will cleanup the references on 9137c478bd9Sstevel@tonic-gate * the files. Start at startoff bytes into the buffer. 9147c478bd9Sstevel@tonic-gate */ 9157c478bd9Sstevel@tonic-gate static void 9167c478bd9Sstevel@tonic-gate close_fds(void *fdbuf, int fdbuflen, int startoff) 9177c478bd9Sstevel@tonic-gate { 9187c478bd9Sstevel@tonic-gate int *fds = (int *)fdbuf; 9197c478bd9Sstevel@tonic-gate int numfd = fdbuflen / (int)sizeof (int); 9207c478bd9Sstevel@tonic-gate int i; 9217c478bd9Sstevel@tonic-gate 9227c478bd9Sstevel@tonic-gate dprint(1, ("close_fds(%p, %d, %d)\n", fdbuf, fdbuflen, startoff)); 9237c478bd9Sstevel@tonic-gate 9247c478bd9Sstevel@tonic-gate for (i = 0; i < numfd; i++) { 9257c478bd9Sstevel@tonic-gate if (startoff < 0) 9267c478bd9Sstevel@tonic-gate startoff = 0; 9277c478bd9Sstevel@tonic-gate if (startoff < (int)sizeof (int)) { 9287c478bd9Sstevel@tonic-gate /* 9297c478bd9Sstevel@tonic-gate * This file descriptor is partially or fully after 9307c478bd9Sstevel@tonic-gate * the offset 9317c478bd9Sstevel@tonic-gate */ 9327c478bd9Sstevel@tonic-gate dprint(0, 9337c478bd9Sstevel@tonic-gate ("close_fds: cleanup[%d] = %d\n", i, fds[i])); 9347c478bd9Sstevel@tonic-gate (void) closeandsetf(fds[i], NULL); 9357c478bd9Sstevel@tonic-gate } 9367c478bd9Sstevel@tonic-gate startoff -= (int)sizeof (int); 9377c478bd9Sstevel@tonic-gate } 9387c478bd9Sstevel@tonic-gate } 9397c478bd9Sstevel@tonic-gate 9407c478bd9Sstevel@tonic-gate /* 9417c478bd9Sstevel@tonic-gate * Close all file descriptors contained in the control part starting at 9427c478bd9Sstevel@tonic-gate * the startoffset. 9437c478bd9Sstevel@tonic-gate */ 9447c478bd9Sstevel@tonic-gate void 9457c478bd9Sstevel@tonic-gate so_closefds(void *control, t_uscalar_t controllen, int oldflg, 9467c478bd9Sstevel@tonic-gate int startoff) 9477c478bd9Sstevel@tonic-gate { 9487c478bd9Sstevel@tonic-gate struct cmsghdr *cmsg; 9497c478bd9Sstevel@tonic-gate 9507c478bd9Sstevel@tonic-gate if (control == NULL) 9517c478bd9Sstevel@tonic-gate return; 9527c478bd9Sstevel@tonic-gate 9537c478bd9Sstevel@tonic-gate if (oldflg) { 9547c478bd9Sstevel@tonic-gate close_fds(control, controllen, startoff); 9557c478bd9Sstevel@tonic-gate return; 9567c478bd9Sstevel@tonic-gate } 9577c478bd9Sstevel@tonic-gate /* Scan control part for file descriptors. */ 9587c478bd9Sstevel@tonic-gate for (cmsg = (struct cmsghdr *)control; 9597c478bd9Sstevel@tonic-gate CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 9607c478bd9Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg)) { 9617c478bd9Sstevel@tonic-gate if (cmsg->cmsg_level == SOL_SOCKET && 9627c478bd9Sstevel@tonic-gate cmsg->cmsg_type == SCM_RIGHTS) { 9637c478bd9Sstevel@tonic-gate close_fds(CMSG_CONTENT(cmsg), 9647c478bd9Sstevel@tonic-gate (int)CMSG_CONTENTLEN(cmsg), 9657c478bd9Sstevel@tonic-gate startoff - (int)sizeof (struct cmsghdr)); 9667c478bd9Sstevel@tonic-gate } 9677c478bd9Sstevel@tonic-gate startoff -= cmsg->cmsg_len; 9687c478bd9Sstevel@tonic-gate } 9697c478bd9Sstevel@tonic-gate } 9707c478bd9Sstevel@tonic-gate 9717c478bd9Sstevel@tonic-gate /* 9727c478bd9Sstevel@tonic-gate * Returns a pointer/length for the file descriptors contained 9737c478bd9Sstevel@tonic-gate * in the control buffer. Returns with *fdlenp == -1 if there are no 9747c478bd9Sstevel@tonic-gate * file descriptor options present. This is different than there being 9757c478bd9Sstevel@tonic-gate * a zero-length file descriptor option. 9767c478bd9Sstevel@tonic-gate * Fail if there are multiple SCM_RIGHT cmsgs. 9777c478bd9Sstevel@tonic-gate */ 9787c478bd9Sstevel@tonic-gate int 9797c478bd9Sstevel@tonic-gate so_getfdopt(void *control, t_uscalar_t controllen, int oldflg, 9807c478bd9Sstevel@tonic-gate void **fdsp, int *fdlenp) 9817c478bd9Sstevel@tonic-gate { 9827c478bd9Sstevel@tonic-gate struct cmsghdr *cmsg; 9837c478bd9Sstevel@tonic-gate void *fds; 9847c478bd9Sstevel@tonic-gate int fdlen; 9857c478bd9Sstevel@tonic-gate 9867c478bd9Sstevel@tonic-gate if (control == NULL) { 9877c478bd9Sstevel@tonic-gate *fdsp = NULL; 9887c478bd9Sstevel@tonic-gate *fdlenp = -1; 9897c478bd9Sstevel@tonic-gate return (0); 9907c478bd9Sstevel@tonic-gate } 9917c478bd9Sstevel@tonic-gate 9927c478bd9Sstevel@tonic-gate if (oldflg) { 9937c478bd9Sstevel@tonic-gate *fdsp = control; 9947c478bd9Sstevel@tonic-gate if (controllen == 0) 9957c478bd9Sstevel@tonic-gate *fdlenp = -1; 9967c478bd9Sstevel@tonic-gate else 9977c478bd9Sstevel@tonic-gate *fdlenp = controllen; 9987c478bd9Sstevel@tonic-gate dprint(1, ("so_getfdopt: old %d\n", *fdlenp)); 9997c478bd9Sstevel@tonic-gate return (0); 10007c478bd9Sstevel@tonic-gate } 10017c478bd9Sstevel@tonic-gate 10027c478bd9Sstevel@tonic-gate fds = NULL; 10037c478bd9Sstevel@tonic-gate fdlen = 0; 10047c478bd9Sstevel@tonic-gate 10057c478bd9Sstevel@tonic-gate for (cmsg = (struct cmsghdr *)control; 10067c478bd9Sstevel@tonic-gate CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 10077c478bd9Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg)) { 10087c478bd9Sstevel@tonic-gate if (cmsg->cmsg_level == SOL_SOCKET && 10097c478bd9Sstevel@tonic-gate cmsg->cmsg_type == SCM_RIGHTS) { 10107c478bd9Sstevel@tonic-gate if (fds != NULL) 10117c478bd9Sstevel@tonic-gate return (EINVAL); 10127c478bd9Sstevel@tonic-gate fds = CMSG_CONTENT(cmsg); 10137c478bd9Sstevel@tonic-gate fdlen = (int)CMSG_CONTENTLEN(cmsg); 10141e0267ddSkrgopi dprint(1, ("so_getfdopt: new %lu\n", 10151e0267ddSkrgopi (size_t)CMSG_CONTENTLEN(cmsg))); 10167c478bd9Sstevel@tonic-gate } 10177c478bd9Sstevel@tonic-gate } 10187c478bd9Sstevel@tonic-gate if (fds == NULL) { 10197c478bd9Sstevel@tonic-gate dprint(1, ("so_getfdopt: NONE\n")); 10207c478bd9Sstevel@tonic-gate *fdlenp = -1; 10217c478bd9Sstevel@tonic-gate } else 10227c478bd9Sstevel@tonic-gate *fdlenp = fdlen; 10237c478bd9Sstevel@tonic-gate *fdsp = fds; 10247c478bd9Sstevel@tonic-gate return (0); 10257c478bd9Sstevel@tonic-gate } 10267c478bd9Sstevel@tonic-gate 10277c478bd9Sstevel@tonic-gate /* 10287c478bd9Sstevel@tonic-gate * Return the length of the options including any file descriptor options. 10297c478bd9Sstevel@tonic-gate */ 10307c478bd9Sstevel@tonic-gate t_uscalar_t 10317c478bd9Sstevel@tonic-gate so_optlen(void *control, t_uscalar_t controllen, int oldflg) 10327c478bd9Sstevel@tonic-gate { 10337c478bd9Sstevel@tonic-gate struct cmsghdr *cmsg; 10347c478bd9Sstevel@tonic-gate t_uscalar_t optlen = 0; 10357c478bd9Sstevel@tonic-gate t_uscalar_t len; 10367c478bd9Sstevel@tonic-gate 10377c478bd9Sstevel@tonic-gate if (control == NULL) 10387c478bd9Sstevel@tonic-gate return (0); 10397c478bd9Sstevel@tonic-gate 10407c478bd9Sstevel@tonic-gate if (oldflg) 10417c478bd9Sstevel@tonic-gate return ((t_uscalar_t)(sizeof (struct T_opthdr) + 10427c478bd9Sstevel@tonic-gate fdbuf_optlen(controllen))); 10437c478bd9Sstevel@tonic-gate 10447c478bd9Sstevel@tonic-gate for (cmsg = (struct cmsghdr *)control; 10457c478bd9Sstevel@tonic-gate CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 10467c478bd9Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg)) { 10477c478bd9Sstevel@tonic-gate if (cmsg->cmsg_level == SOL_SOCKET && 10487c478bd9Sstevel@tonic-gate cmsg->cmsg_type == SCM_RIGHTS) { 10497c478bd9Sstevel@tonic-gate len = fdbuf_optlen((int)CMSG_CONTENTLEN(cmsg)); 10507c478bd9Sstevel@tonic-gate } else { 10517c478bd9Sstevel@tonic-gate len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg); 10527c478bd9Sstevel@tonic-gate } 10537c478bd9Sstevel@tonic-gate optlen += (t_uscalar_t)(_TPI_ALIGN_TOPT(len) + 10547c478bd9Sstevel@tonic-gate sizeof (struct T_opthdr)); 10557c478bd9Sstevel@tonic-gate } 10567c478bd9Sstevel@tonic-gate dprint(1, ("so_optlen: controllen %d, flg %d -> optlen %d\n", 10577c478bd9Sstevel@tonic-gate controllen, oldflg, optlen)); 10587c478bd9Sstevel@tonic-gate return (optlen); 10597c478bd9Sstevel@tonic-gate } 10607c478bd9Sstevel@tonic-gate 10617c478bd9Sstevel@tonic-gate /* 10627c478bd9Sstevel@tonic-gate * Copy options from control to the mblk. Skip any file descriptor options. 10637c478bd9Sstevel@tonic-gate */ 10647c478bd9Sstevel@tonic-gate void 10657c478bd9Sstevel@tonic-gate so_cmsg2opt(void *control, t_uscalar_t controllen, int oldflg, mblk_t *mp) 10667c478bd9Sstevel@tonic-gate { 10677c478bd9Sstevel@tonic-gate struct T_opthdr toh; 10687c478bd9Sstevel@tonic-gate struct cmsghdr *cmsg; 10697c478bd9Sstevel@tonic-gate 10707c478bd9Sstevel@tonic-gate if (control == NULL) 10717c478bd9Sstevel@tonic-gate return; 10727c478bd9Sstevel@tonic-gate 10737c478bd9Sstevel@tonic-gate if (oldflg) { 10747c478bd9Sstevel@tonic-gate /* No real options - caller has handled file descriptors */ 10757c478bd9Sstevel@tonic-gate return; 10767c478bd9Sstevel@tonic-gate } 10777c478bd9Sstevel@tonic-gate for (cmsg = (struct cmsghdr *)control; 10787c478bd9Sstevel@tonic-gate CMSG_VALID(cmsg, control, (uintptr_t)control + controllen); 10797c478bd9Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg)) { 10807c478bd9Sstevel@tonic-gate /* 10817c478bd9Sstevel@tonic-gate * Note: The caller handles file descriptors prior 10827c478bd9Sstevel@tonic-gate * to calling this function. 10837c478bd9Sstevel@tonic-gate */ 10847c478bd9Sstevel@tonic-gate t_uscalar_t len; 10857c478bd9Sstevel@tonic-gate 10867c478bd9Sstevel@tonic-gate if (cmsg->cmsg_level == SOL_SOCKET && 10877c478bd9Sstevel@tonic-gate cmsg->cmsg_type == SCM_RIGHTS) 10887c478bd9Sstevel@tonic-gate continue; 10897c478bd9Sstevel@tonic-gate 10907c478bd9Sstevel@tonic-gate len = (t_uscalar_t)CMSG_CONTENTLEN(cmsg); 10917c478bd9Sstevel@tonic-gate toh.level = cmsg->cmsg_level; 10927c478bd9Sstevel@tonic-gate toh.name = cmsg->cmsg_type; 10937c478bd9Sstevel@tonic-gate toh.len = len + (t_uscalar_t)sizeof (struct T_opthdr); 10947c478bd9Sstevel@tonic-gate toh.status = 0; 10957c478bd9Sstevel@tonic-gate 10967c478bd9Sstevel@tonic-gate soappendmsg(mp, &toh, sizeof (toh)); 10977c478bd9Sstevel@tonic-gate soappendmsg(mp, CMSG_CONTENT(cmsg), len); 10987c478bd9Sstevel@tonic-gate mp->b_wptr += _TPI_ALIGN_TOPT(len) - len; 10997c478bd9Sstevel@tonic-gate ASSERT(mp->b_wptr <= mp->b_datap->db_lim); 11007c478bd9Sstevel@tonic-gate } 11017c478bd9Sstevel@tonic-gate } 11027c478bd9Sstevel@tonic-gate 11037c478bd9Sstevel@tonic-gate /* 11047c478bd9Sstevel@tonic-gate * Return the length of the control message derived from the options. 11057c478bd9Sstevel@tonic-gate * Exclude SO_SRCADDR and SO_UNIX_CLOSE options. Include SO_FILEP. 11067c478bd9Sstevel@tonic-gate * When oldflg is set only include SO_FILEP. 11070d204002Sgt145670 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen 11080d204002Sgt145670 * allocates the space that so_opt2cmsg fills. If one changes, the other should 11090d204002Sgt145670 * also be checked for any possible impacts. 11107c478bd9Sstevel@tonic-gate */ 11117c478bd9Sstevel@tonic-gate t_uscalar_t 11127c478bd9Sstevel@tonic-gate so_cmsglen(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg) 11137c478bd9Sstevel@tonic-gate { 11147c478bd9Sstevel@tonic-gate t_uscalar_t cmsglen = 0; 11157c478bd9Sstevel@tonic-gate struct T_opthdr *tohp; 11167c478bd9Sstevel@tonic-gate t_uscalar_t len; 11177c478bd9Sstevel@tonic-gate t_uscalar_t last_roundup = 0; 11187c478bd9Sstevel@tonic-gate 11197c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(opt)); 11207c478bd9Sstevel@tonic-gate 11217c478bd9Sstevel@tonic-gate for (tohp = (struct T_opthdr *)opt; 11227c478bd9Sstevel@tonic-gate tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 11237c478bd9Sstevel@tonic-gate tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 11247c478bd9Sstevel@tonic-gate dprint(1, ("so_cmsglen: level 0x%x, name %d, len %d\n", 11257c478bd9Sstevel@tonic-gate tohp->level, tohp->name, tohp->len)); 11267c478bd9Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && 11277c478bd9Sstevel@tonic-gate (tohp->name == SO_SRCADDR || 11287c478bd9Sstevel@tonic-gate tohp->name == SO_UNIX_CLOSE)) { 11297c478bd9Sstevel@tonic-gate continue; 11307c478bd9Sstevel@tonic-gate } 11317c478bd9Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) { 11327c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf; 11337c478bd9Sstevel@tonic-gate int fdbuflen; 11347c478bd9Sstevel@tonic-gate 11357c478bd9Sstevel@tonic-gate fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp); 11367c478bd9Sstevel@tonic-gate fdbuflen = (int)_TPI_TOPT_DATALEN(tohp); 11377c478bd9Sstevel@tonic-gate 11387c478bd9Sstevel@tonic-gate if (!fdbuf_verify(mp, fdbuf, fdbuflen)) 11397c478bd9Sstevel@tonic-gate continue; 11407c478bd9Sstevel@tonic-gate if (oldflg) { 11417c478bd9Sstevel@tonic-gate cmsglen += fdbuf_cmsglen(fdbuflen); 11427c478bd9Sstevel@tonic-gate continue; 11437c478bd9Sstevel@tonic-gate } 11447c478bd9Sstevel@tonic-gate len = fdbuf_cmsglen(fdbuflen); 11450d204002Sgt145670 } else if (tohp->level == SOL_SOCKET && 11460d204002Sgt145670 tohp->name == SCM_TIMESTAMP) { 11470d204002Sgt145670 if (oldflg) 11480d204002Sgt145670 continue; 11490d204002Sgt145670 11500d204002Sgt145670 if (get_udatamodel() == DATAMODEL_NATIVE) { 11510d204002Sgt145670 len = sizeof (struct timeval); 11520d204002Sgt145670 } else { 11530d204002Sgt145670 len = sizeof (struct timeval32); 11540d204002Sgt145670 } 11557c478bd9Sstevel@tonic-gate } else { 11567c478bd9Sstevel@tonic-gate if (oldflg) 11577c478bd9Sstevel@tonic-gate continue; 11587c478bd9Sstevel@tonic-gate len = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp); 11597c478bd9Sstevel@tonic-gate } 11607c478bd9Sstevel@tonic-gate /* 11610d204002Sgt145670 * Exclude roundup for last option to not set 11627c478bd9Sstevel@tonic-gate * MSG_CTRUNC when the cmsg fits but the padding doesn't fit. 11637c478bd9Sstevel@tonic-gate */ 11647c478bd9Sstevel@tonic-gate last_roundup = (t_uscalar_t) 11657c478bd9Sstevel@tonic-gate (ROUNDUP_cmsglen(len + (int)sizeof (struct cmsghdr)) - 11667c478bd9Sstevel@tonic-gate (len + (int)sizeof (struct cmsghdr))); 11677c478bd9Sstevel@tonic-gate cmsglen += (t_uscalar_t)(len + (int)sizeof (struct cmsghdr)) + 11687c478bd9Sstevel@tonic-gate last_roundup; 11697c478bd9Sstevel@tonic-gate } 11707c478bd9Sstevel@tonic-gate cmsglen -= last_roundup; 11717c478bd9Sstevel@tonic-gate dprint(1, ("so_cmsglen: optlen %d, flg %d -> cmsglen %d\n", 11727c478bd9Sstevel@tonic-gate optlen, oldflg, cmsglen)); 11737c478bd9Sstevel@tonic-gate return (cmsglen); 11747c478bd9Sstevel@tonic-gate } 11757c478bd9Sstevel@tonic-gate 11767c478bd9Sstevel@tonic-gate /* 11777c478bd9Sstevel@tonic-gate * Copy options from options to the control. Convert SO_FILEP to 11787c478bd9Sstevel@tonic-gate * file descriptors. 11797c478bd9Sstevel@tonic-gate * Returns errno or zero. 11800d204002Sgt145670 * so_opt2cmsg and so_cmsglen are inter-related since so_cmsglen 11810d204002Sgt145670 * allocates the space that so_opt2cmsg fills. If one changes, the other should 11820d204002Sgt145670 * also be checked for any possible impacts. 11837c478bd9Sstevel@tonic-gate */ 11847c478bd9Sstevel@tonic-gate int 11857c478bd9Sstevel@tonic-gate so_opt2cmsg(mblk_t *mp, void *opt, t_uscalar_t optlen, int oldflg, 11867c478bd9Sstevel@tonic-gate void *control, t_uscalar_t controllen) 11877c478bd9Sstevel@tonic-gate { 11887c478bd9Sstevel@tonic-gate struct T_opthdr *tohp; 11897c478bd9Sstevel@tonic-gate struct cmsghdr *cmsg; 11907c478bd9Sstevel@tonic-gate struct fdbuf *fdbuf; 11917c478bd9Sstevel@tonic-gate int fdbuflen; 11927c478bd9Sstevel@tonic-gate int error; 11930d204002Sgt145670 #if defined(DEBUG) || defined(__lint) 11940d204002Sgt145670 struct cmsghdr *cend = (struct cmsghdr *) 11950d204002Sgt145670 (((uint8_t *)control) + ROUNDUP_cmsglen(controllen)); 11960d204002Sgt145670 #endif 11977c478bd9Sstevel@tonic-gate cmsg = (struct cmsghdr *)control; 11987c478bd9Sstevel@tonic-gate 11997c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(opt)); 12007c478bd9Sstevel@tonic-gate 12017c478bd9Sstevel@tonic-gate for (tohp = (struct T_opthdr *)opt; 12027c478bd9Sstevel@tonic-gate tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 12037c478bd9Sstevel@tonic-gate tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 12047c478bd9Sstevel@tonic-gate dprint(1, ("so_opt2cmsg: level 0x%x, name %d, len %d\n", 12057c478bd9Sstevel@tonic-gate tohp->level, tohp->name, tohp->len)); 12067c478bd9Sstevel@tonic-gate 12077c478bd9Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && 12087c478bd9Sstevel@tonic-gate (tohp->name == SO_SRCADDR || 12097c478bd9Sstevel@tonic-gate tohp->name == SO_UNIX_CLOSE)) { 12107c478bd9Sstevel@tonic-gate continue; 12117c478bd9Sstevel@tonic-gate } 12127c478bd9Sstevel@tonic-gate ASSERT((uintptr_t)cmsg <= (uintptr_t)control + controllen); 12137c478bd9Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && tohp->name == SO_FILEP) { 12147c478bd9Sstevel@tonic-gate fdbuf = (struct fdbuf *)_TPI_TOPT_DATA(tohp); 12157c478bd9Sstevel@tonic-gate fdbuflen = (int)_TPI_TOPT_DATALEN(tohp); 12167c478bd9Sstevel@tonic-gate 12177c478bd9Sstevel@tonic-gate if (!fdbuf_verify(mp, fdbuf, fdbuflen)) 12187c478bd9Sstevel@tonic-gate return (EPROTO); 12197c478bd9Sstevel@tonic-gate if (oldflg) { 12207c478bd9Sstevel@tonic-gate error = fdbuf_extract(fdbuf, control, 12217c478bd9Sstevel@tonic-gate (int)controllen); 12227c478bd9Sstevel@tonic-gate if (error != 0) 12237c478bd9Sstevel@tonic-gate return (error); 12247c478bd9Sstevel@tonic-gate continue; 12257c478bd9Sstevel@tonic-gate } else { 12267c478bd9Sstevel@tonic-gate int fdlen; 12277c478bd9Sstevel@tonic-gate 12287c478bd9Sstevel@tonic-gate fdlen = (int)fdbuf_cmsglen( 12297c478bd9Sstevel@tonic-gate (int)_TPI_TOPT_DATALEN(tohp)); 12307c478bd9Sstevel@tonic-gate 12317c478bd9Sstevel@tonic-gate cmsg->cmsg_level = tohp->level; 12327c478bd9Sstevel@tonic-gate cmsg->cmsg_type = SCM_RIGHTS; 12337c478bd9Sstevel@tonic-gate cmsg->cmsg_len = (socklen_t)(fdlen + 12347c478bd9Sstevel@tonic-gate sizeof (struct cmsghdr)); 12357c478bd9Sstevel@tonic-gate 12367c478bd9Sstevel@tonic-gate error = fdbuf_extract(fdbuf, 12377c478bd9Sstevel@tonic-gate CMSG_CONTENT(cmsg), fdlen); 12387c478bd9Sstevel@tonic-gate if (error != 0) 12397c478bd9Sstevel@tonic-gate return (error); 12407c478bd9Sstevel@tonic-gate } 1241e4f35dbaSgt145670 } else if (tohp->level == SOL_SOCKET && 1242e4f35dbaSgt145670 tohp->name == SCM_TIMESTAMP) { 1243e4f35dbaSgt145670 timestruc_t *timestamp; 1244e4f35dbaSgt145670 1245e4f35dbaSgt145670 if (oldflg) 1246e4f35dbaSgt145670 continue; 1247e4f35dbaSgt145670 1248e4f35dbaSgt145670 cmsg->cmsg_level = tohp->level; 1249e4f35dbaSgt145670 cmsg->cmsg_type = tohp->name; 1250e4f35dbaSgt145670 1251e4f35dbaSgt145670 timestamp = 1252e4f35dbaSgt145670 (timestruc_t *)P2ROUNDUP((intptr_t)&tohp[1], 1253e4f35dbaSgt145670 sizeof (intptr_t)); 1254e4f35dbaSgt145670 1255e4f35dbaSgt145670 if (get_udatamodel() == DATAMODEL_NATIVE) { 12560d204002Sgt145670 struct timeval tv; 1257e4f35dbaSgt145670 1258e4f35dbaSgt145670 cmsg->cmsg_len = sizeof (struct timeval) + 1259e4f35dbaSgt145670 sizeof (struct cmsghdr); 12600d204002Sgt145670 tv.tv_sec = timestamp->tv_sec; 12610d204002Sgt145670 tv.tv_usec = timestamp->tv_nsec / 12620d204002Sgt145670 (NANOSEC / MICROSEC); 12630d204002Sgt145670 /* 12640d204002Sgt145670 * on LP64 systems, the struct timeval in 12650d204002Sgt145670 * the destination will not be 8-byte aligned, 12660d204002Sgt145670 * so use bcopy to avoid alignment trouble 12670d204002Sgt145670 */ 12680d204002Sgt145670 bcopy(&tv, CMSG_CONTENT(cmsg), sizeof (tv)); 1269e4f35dbaSgt145670 } else { 1270e4f35dbaSgt145670 struct timeval32 *time32; 1271e4f35dbaSgt145670 1272e4f35dbaSgt145670 cmsg->cmsg_len = sizeof (struct timeval32) + 1273e4f35dbaSgt145670 sizeof (struct cmsghdr); 1274e4f35dbaSgt145670 time32 = (struct timeval32 *)CMSG_CONTENT(cmsg); 1275e4f35dbaSgt145670 time32->tv_sec = (time32_t)timestamp->tv_sec; 1276e4f35dbaSgt145670 time32->tv_usec = 1277e4f35dbaSgt145670 (int32_t)(timestamp->tv_nsec / 1278e4f35dbaSgt145670 (NANOSEC / MICROSEC)); 1279e4f35dbaSgt145670 } 1280e4f35dbaSgt145670 12817c478bd9Sstevel@tonic-gate } else { 12827c478bd9Sstevel@tonic-gate if (oldflg) 12837c478bd9Sstevel@tonic-gate continue; 12847c478bd9Sstevel@tonic-gate 12857c478bd9Sstevel@tonic-gate cmsg->cmsg_level = tohp->level; 12867c478bd9Sstevel@tonic-gate cmsg->cmsg_type = tohp->name; 12877c478bd9Sstevel@tonic-gate cmsg->cmsg_len = (socklen_t)(_TPI_TOPT_DATALEN(tohp) + 12887c478bd9Sstevel@tonic-gate sizeof (struct cmsghdr)); 12897c478bd9Sstevel@tonic-gate 12907c478bd9Sstevel@tonic-gate /* copy content to control data part */ 12917c478bd9Sstevel@tonic-gate bcopy(&tohp[1], CMSG_CONTENT(cmsg), 12927c478bd9Sstevel@tonic-gate CMSG_CONTENTLEN(cmsg)); 12937c478bd9Sstevel@tonic-gate } 12947c478bd9Sstevel@tonic-gate /* move to next CMSG structure! */ 12957c478bd9Sstevel@tonic-gate cmsg = CMSG_NEXT(cmsg); 12967c478bd9Sstevel@tonic-gate } 12970d204002Sgt145670 dprint(1, ("so_opt2cmsg: buf %p len %d; cend %p; final cmsg %p\n", 1298903a11ebSrh87107 control, controllen, (void *)cend, (void *)cmsg)); 12990d204002Sgt145670 ASSERT(cmsg <= cend); 13007c478bd9Sstevel@tonic-gate return (0); 13017c478bd9Sstevel@tonic-gate } 13027c478bd9Sstevel@tonic-gate 13037c478bd9Sstevel@tonic-gate /* 13047c478bd9Sstevel@tonic-gate * Extract the SO_SRCADDR option value if present. 13057c478bd9Sstevel@tonic-gate */ 13067c478bd9Sstevel@tonic-gate void 13077c478bd9Sstevel@tonic-gate so_getopt_srcaddr(void *opt, t_uscalar_t optlen, void **srcp, 13087c478bd9Sstevel@tonic-gate t_uscalar_t *srclenp) 13097c478bd9Sstevel@tonic-gate { 13107c478bd9Sstevel@tonic-gate struct T_opthdr *tohp; 13117c478bd9Sstevel@tonic-gate 13127c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(opt)); 13137c478bd9Sstevel@tonic-gate 13147c478bd9Sstevel@tonic-gate ASSERT(srcp != NULL && srclenp != NULL); 13157c478bd9Sstevel@tonic-gate *srcp = NULL; 13167c478bd9Sstevel@tonic-gate *srclenp = 0; 13177c478bd9Sstevel@tonic-gate 13187c478bd9Sstevel@tonic-gate for (tohp = (struct T_opthdr *)opt; 13197c478bd9Sstevel@tonic-gate tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 13207c478bd9Sstevel@tonic-gate tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 13217c478bd9Sstevel@tonic-gate dprint(1, ("so_getopt_srcaddr: level 0x%x, name %d, len %d\n", 13227c478bd9Sstevel@tonic-gate tohp->level, tohp->name, tohp->len)); 13237c478bd9Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && 13247c478bd9Sstevel@tonic-gate tohp->name == SO_SRCADDR) { 13257c478bd9Sstevel@tonic-gate *srcp = _TPI_TOPT_DATA(tohp); 13267c478bd9Sstevel@tonic-gate *srclenp = (t_uscalar_t)_TPI_TOPT_DATALEN(tohp); 13277c478bd9Sstevel@tonic-gate } 13287c478bd9Sstevel@tonic-gate } 13297c478bd9Sstevel@tonic-gate } 13307c478bd9Sstevel@tonic-gate 13317c478bd9Sstevel@tonic-gate /* 13327c478bd9Sstevel@tonic-gate * Verify if the SO_UNIX_CLOSE option is present. 13337c478bd9Sstevel@tonic-gate */ 13347c478bd9Sstevel@tonic-gate int 13357c478bd9Sstevel@tonic-gate so_getopt_unix_close(void *opt, t_uscalar_t optlen) 13367c478bd9Sstevel@tonic-gate { 13377c478bd9Sstevel@tonic-gate struct T_opthdr *tohp; 13387c478bd9Sstevel@tonic-gate 13397c478bd9Sstevel@tonic-gate ASSERT(__TPI_TOPT_ISALIGNED(opt)); 13407c478bd9Sstevel@tonic-gate 13417c478bd9Sstevel@tonic-gate for (tohp = (struct T_opthdr *)opt; 13427c478bd9Sstevel@tonic-gate tohp && _TPI_TOPT_VALID(tohp, opt, (uintptr_t)opt + optlen); 13437c478bd9Sstevel@tonic-gate tohp = _TPI_TOPT_NEXTHDR(opt, optlen, tohp)) { 13447c478bd9Sstevel@tonic-gate dprint(1, 13457c478bd9Sstevel@tonic-gate ("so_getopt_unix_close: level 0x%x, name %d, len %d\n", 13467c478bd9Sstevel@tonic-gate tohp->level, tohp->name, tohp->len)); 13477c478bd9Sstevel@tonic-gate if (tohp->level == SOL_SOCKET && 13487c478bd9Sstevel@tonic-gate tohp->name == SO_UNIX_CLOSE) 13497c478bd9Sstevel@tonic-gate return (1); 13507c478bd9Sstevel@tonic-gate } 13517c478bd9Sstevel@tonic-gate return (0); 13527c478bd9Sstevel@tonic-gate } 13537c478bd9Sstevel@tonic-gate 13547c478bd9Sstevel@tonic-gate /* 13557c478bd9Sstevel@tonic-gate * Allocate an M_PROTO message. 13567c478bd9Sstevel@tonic-gate * 13577c478bd9Sstevel@tonic-gate * If allocation fails the behavior depends on sleepflg: 13587c478bd9Sstevel@tonic-gate * _ALLOC_NOSLEEP fail immediately 13597c478bd9Sstevel@tonic-gate * _ALLOC_INTR sleep for memory until a signal is caught 13607c478bd9Sstevel@tonic-gate * _ALLOC_SLEEP sleep forever. Don't return NULL. 13617c478bd9Sstevel@tonic-gate */ 13627c478bd9Sstevel@tonic-gate mblk_t * 1363de8c4a14SErik Nordmark soallocproto(size_t size, int sleepflg, cred_t *cr) 13647c478bd9Sstevel@tonic-gate { 13657c478bd9Sstevel@tonic-gate mblk_t *mp; 13667c478bd9Sstevel@tonic-gate 13677c478bd9Sstevel@tonic-gate /* Round up size for reuse */ 13687c478bd9Sstevel@tonic-gate size = MAX(size, 64); 1369de8c4a14SErik Nordmark if (cr != NULL) 1370de8c4a14SErik Nordmark mp = allocb_cred(size, cr, curproc->p_pid); 1371de8c4a14SErik Nordmark else 13727c478bd9Sstevel@tonic-gate mp = allocb(size, BPRI_MED); 1373de8c4a14SErik Nordmark 13747c478bd9Sstevel@tonic-gate if (mp == NULL) { 13757c478bd9Sstevel@tonic-gate int error; /* Dummy - error not returned to caller */ 13767c478bd9Sstevel@tonic-gate 13777c478bd9Sstevel@tonic-gate switch (sleepflg) { 13787c478bd9Sstevel@tonic-gate case _ALLOC_SLEEP: 1379de8c4a14SErik Nordmark if (cr != NULL) { 1380de8c4a14SErik Nordmark mp = allocb_cred_wait(size, STR_NOSIG, &error, 1381de8c4a14SErik Nordmark cr, curproc->p_pid); 1382de8c4a14SErik Nordmark } else { 1383de8c4a14SErik Nordmark mp = allocb_wait(size, BPRI_MED, STR_NOSIG, 1384de8c4a14SErik Nordmark &error); 1385de8c4a14SErik Nordmark } 13867c478bd9Sstevel@tonic-gate ASSERT(mp); 13877c478bd9Sstevel@tonic-gate break; 13887c478bd9Sstevel@tonic-gate case _ALLOC_INTR: 1389de8c4a14SErik Nordmark if (cr != NULL) { 1390de8c4a14SErik Nordmark mp = allocb_cred_wait(size, 0, &error, cr, 1391de8c4a14SErik Nordmark curproc->p_pid); 1392de8c4a14SErik Nordmark } else { 13937c478bd9Sstevel@tonic-gate mp = allocb_wait(size, BPRI_MED, 0, &error); 1394de8c4a14SErik Nordmark } 13957c478bd9Sstevel@tonic-gate if (mp == NULL) { 13967c478bd9Sstevel@tonic-gate /* Caught signal while sleeping for memory */ 13977c478bd9Sstevel@tonic-gate eprintline(ENOBUFS); 13987c478bd9Sstevel@tonic-gate return (NULL); 13997c478bd9Sstevel@tonic-gate } 14007c478bd9Sstevel@tonic-gate break; 14017c478bd9Sstevel@tonic-gate case _ALLOC_NOSLEEP: 14027c478bd9Sstevel@tonic-gate default: 14037c478bd9Sstevel@tonic-gate eprintline(ENOBUFS); 14047c478bd9Sstevel@tonic-gate return (NULL); 14057c478bd9Sstevel@tonic-gate } 14067c478bd9Sstevel@tonic-gate } 14077c478bd9Sstevel@tonic-gate DB_TYPE(mp) = M_PROTO; 14087c478bd9Sstevel@tonic-gate return (mp); 14097c478bd9Sstevel@tonic-gate } 14107c478bd9Sstevel@tonic-gate 14117c478bd9Sstevel@tonic-gate /* 14127c478bd9Sstevel@tonic-gate * Allocate an M_PROTO message with a single component. 14137c478bd9Sstevel@tonic-gate * len is the length of buf. size is the amount to allocate. 14147c478bd9Sstevel@tonic-gate * 14157c478bd9Sstevel@tonic-gate * buf can be NULL with a non-zero len. 14167c478bd9Sstevel@tonic-gate * This results in a bzero'ed chunk being placed the message. 14177c478bd9Sstevel@tonic-gate */ 14187c478bd9Sstevel@tonic-gate mblk_t * 1419de8c4a14SErik Nordmark soallocproto1(const void *buf, ssize_t len, ssize_t size, int sleepflg, 1420de8c4a14SErik Nordmark cred_t *cr) 14217c478bd9Sstevel@tonic-gate { 14227c478bd9Sstevel@tonic-gate mblk_t *mp; 14237c478bd9Sstevel@tonic-gate 14247c478bd9Sstevel@tonic-gate if (size == 0) 14257c478bd9Sstevel@tonic-gate size = len; 14267c478bd9Sstevel@tonic-gate 14277c478bd9Sstevel@tonic-gate ASSERT(size >= len); 14287c478bd9Sstevel@tonic-gate /* Round up size for reuse */ 14297c478bd9Sstevel@tonic-gate size = MAX(size, 64); 1430de8c4a14SErik Nordmark mp = soallocproto(size, sleepflg, cr); 14317c478bd9Sstevel@tonic-gate if (mp == NULL) 14327c478bd9Sstevel@tonic-gate return (NULL); 14337c478bd9Sstevel@tonic-gate mp->b_datap->db_type = M_PROTO; 14347c478bd9Sstevel@tonic-gate if (len != 0) { 14357c478bd9Sstevel@tonic-gate if (buf != NULL) 14367c478bd9Sstevel@tonic-gate bcopy(buf, mp->b_wptr, len); 14377c478bd9Sstevel@tonic-gate else 14387c478bd9Sstevel@tonic-gate bzero(mp->b_wptr, len); 14397c478bd9Sstevel@tonic-gate mp->b_wptr += len; 14407c478bd9Sstevel@tonic-gate } 14417c478bd9Sstevel@tonic-gate return (mp); 14427c478bd9Sstevel@tonic-gate } 14437c478bd9Sstevel@tonic-gate 14447c478bd9Sstevel@tonic-gate /* 14457c478bd9Sstevel@tonic-gate * Append buf/len to mp. 14467c478bd9Sstevel@tonic-gate * The caller has to ensure that there is enough room in the mblk. 14477c478bd9Sstevel@tonic-gate * 14487c478bd9Sstevel@tonic-gate * buf can be NULL with a non-zero len. 14497c478bd9Sstevel@tonic-gate * This results in a bzero'ed chunk being placed the message. 14507c478bd9Sstevel@tonic-gate */ 14517c478bd9Sstevel@tonic-gate void 14527c478bd9Sstevel@tonic-gate soappendmsg(mblk_t *mp, const void *buf, ssize_t len) 14537c478bd9Sstevel@tonic-gate { 14547c478bd9Sstevel@tonic-gate ASSERT(mp); 14557c478bd9Sstevel@tonic-gate 14567c478bd9Sstevel@tonic-gate if (len != 0) { 14577c478bd9Sstevel@tonic-gate /* Assert for room left */ 14587c478bd9Sstevel@tonic-gate ASSERT(mp->b_datap->db_lim - mp->b_wptr >= len); 14597c478bd9Sstevel@tonic-gate if (buf != NULL) 14607c478bd9Sstevel@tonic-gate bcopy(buf, mp->b_wptr, len); 14617c478bd9Sstevel@tonic-gate else 14627c478bd9Sstevel@tonic-gate bzero(mp->b_wptr, len); 14637c478bd9Sstevel@tonic-gate } 14647c478bd9Sstevel@tonic-gate mp->b_wptr += len; 14657c478bd9Sstevel@tonic-gate } 14667c478bd9Sstevel@tonic-gate 14677c478bd9Sstevel@tonic-gate /* 14687c478bd9Sstevel@tonic-gate * Create a message using two kernel buffers. 14697c478bd9Sstevel@tonic-gate * If size is set that will determine the allocation size (e.g. for future 14707c478bd9Sstevel@tonic-gate * soappendmsg calls). If size is zero it is derived from the buffer 14717c478bd9Sstevel@tonic-gate * lengths. 14727c478bd9Sstevel@tonic-gate */ 14737c478bd9Sstevel@tonic-gate mblk_t * 14747c478bd9Sstevel@tonic-gate soallocproto2(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2, 1475de8c4a14SErik Nordmark ssize_t size, int sleepflg, cred_t *cr) 14767c478bd9Sstevel@tonic-gate { 14777c478bd9Sstevel@tonic-gate mblk_t *mp; 14787c478bd9Sstevel@tonic-gate 14797c478bd9Sstevel@tonic-gate if (size == 0) 14807c478bd9Sstevel@tonic-gate size = len1 + len2; 14817c478bd9Sstevel@tonic-gate ASSERT(size >= len1 + len2); 14827c478bd9Sstevel@tonic-gate 1483de8c4a14SErik Nordmark mp = soallocproto1(buf1, len1, size, sleepflg, cr); 14847c478bd9Sstevel@tonic-gate if (mp) 14857c478bd9Sstevel@tonic-gate soappendmsg(mp, buf2, len2); 14867c478bd9Sstevel@tonic-gate return (mp); 14877c478bd9Sstevel@tonic-gate } 14887c478bd9Sstevel@tonic-gate 14897c478bd9Sstevel@tonic-gate /* 14907c478bd9Sstevel@tonic-gate * Create a message using three kernel buffers. 14917c478bd9Sstevel@tonic-gate * If size is set that will determine the allocation size (for future 14927c478bd9Sstevel@tonic-gate * soappendmsg calls). If size is zero it is derived from the buffer 14937c478bd9Sstevel@tonic-gate * lengths. 14947c478bd9Sstevel@tonic-gate */ 14957c478bd9Sstevel@tonic-gate mblk_t * 14967c478bd9Sstevel@tonic-gate soallocproto3(const void *buf1, ssize_t len1, const void *buf2, ssize_t len2, 1497de8c4a14SErik Nordmark const void *buf3, ssize_t len3, ssize_t size, int sleepflg, cred_t *cr) 14987c478bd9Sstevel@tonic-gate { 14997c478bd9Sstevel@tonic-gate mblk_t *mp; 15007c478bd9Sstevel@tonic-gate 15017c478bd9Sstevel@tonic-gate if (size == 0) 15027c478bd9Sstevel@tonic-gate size = len1 + len2 +len3; 15037c478bd9Sstevel@tonic-gate ASSERT(size >= len1 + len2 + len3); 15047c478bd9Sstevel@tonic-gate 1505de8c4a14SErik Nordmark mp = soallocproto1(buf1, len1, size, sleepflg, cr); 15067c478bd9Sstevel@tonic-gate if (mp != NULL) { 15077c478bd9Sstevel@tonic-gate soappendmsg(mp, buf2, len2); 15087c478bd9Sstevel@tonic-gate soappendmsg(mp, buf3, len3); 15097c478bd9Sstevel@tonic-gate } 15107c478bd9Sstevel@tonic-gate return (mp); 15117c478bd9Sstevel@tonic-gate } 15127c478bd9Sstevel@tonic-gate 15137c478bd9Sstevel@tonic-gate #ifdef DEBUG 15147c478bd9Sstevel@tonic-gate char * 15157c478bd9Sstevel@tonic-gate pr_state(uint_t state, uint_t mode) 15167c478bd9Sstevel@tonic-gate { 15177c478bd9Sstevel@tonic-gate static char buf[1024]; 15187c478bd9Sstevel@tonic-gate 15197c478bd9Sstevel@tonic-gate buf[0] = 0; 15207c478bd9Sstevel@tonic-gate if (state & SS_ISCONNECTED) 1521903a11ebSrh87107 (void) strcat(buf, "ISCONNECTED "); 15227c478bd9Sstevel@tonic-gate if (state & SS_ISCONNECTING) 1523903a11ebSrh87107 (void) strcat(buf, "ISCONNECTING "); 15247c478bd9Sstevel@tonic-gate if (state & SS_ISDISCONNECTING) 1525903a11ebSrh87107 (void) strcat(buf, "ISDISCONNECTING "); 15267c478bd9Sstevel@tonic-gate if (state & SS_CANTSENDMORE) 1527903a11ebSrh87107 (void) strcat(buf, "CANTSENDMORE "); 15287c478bd9Sstevel@tonic-gate 15297c478bd9Sstevel@tonic-gate if (state & SS_CANTRCVMORE) 1530903a11ebSrh87107 (void) strcat(buf, "CANTRCVMORE "); 15317c478bd9Sstevel@tonic-gate if (state & SS_ISBOUND) 1532903a11ebSrh87107 (void) strcat(buf, "ISBOUND "); 15337c478bd9Sstevel@tonic-gate if (state & SS_NDELAY) 1534903a11ebSrh87107 (void) strcat(buf, "NDELAY "); 15357c478bd9Sstevel@tonic-gate if (state & SS_NONBLOCK) 1536903a11ebSrh87107 (void) strcat(buf, "NONBLOCK "); 15377c478bd9Sstevel@tonic-gate 15387c478bd9Sstevel@tonic-gate if (state & SS_ASYNC) 1539903a11ebSrh87107 (void) strcat(buf, "ASYNC "); 15407c478bd9Sstevel@tonic-gate if (state & SS_ACCEPTCONN) 1541903a11ebSrh87107 (void) strcat(buf, "ACCEPTCONN "); 15427c478bd9Sstevel@tonic-gate if (state & SS_SAVEDEOR) 1543903a11ebSrh87107 (void) strcat(buf, "SAVEDEOR "); 15447c478bd9Sstevel@tonic-gate 15457c478bd9Sstevel@tonic-gate if (state & SS_RCVATMARK) 1546903a11ebSrh87107 (void) strcat(buf, "RCVATMARK "); 15477c478bd9Sstevel@tonic-gate if (state & SS_OOBPEND) 1548903a11ebSrh87107 (void) strcat(buf, "OOBPEND "); 15497c478bd9Sstevel@tonic-gate if (state & SS_HAVEOOBDATA) 1550903a11ebSrh87107 (void) strcat(buf, "HAVEOOBDATA "); 15517c478bd9Sstevel@tonic-gate if (state & SS_HADOOBDATA) 1552903a11ebSrh87107 (void) strcat(buf, "HADOOBDATA "); 15537c478bd9Sstevel@tonic-gate 15547c478bd9Sstevel@tonic-gate if (mode & SM_PRIV) 1555903a11ebSrh87107 (void) strcat(buf, "PRIV "); 15567c478bd9Sstevel@tonic-gate if (mode & SM_ATOMIC) 1557903a11ebSrh87107 (void) strcat(buf, "ATOMIC "); 15587c478bd9Sstevel@tonic-gate if (mode & SM_ADDR) 1559903a11ebSrh87107 (void) strcat(buf, "ADDR "); 15607c478bd9Sstevel@tonic-gate if (mode & SM_CONNREQUIRED) 1561903a11ebSrh87107 (void) strcat(buf, "CONNREQUIRED "); 15627c478bd9Sstevel@tonic-gate 15637c478bd9Sstevel@tonic-gate if (mode & SM_FDPASSING) 1564903a11ebSrh87107 (void) strcat(buf, "FDPASSING "); 15657c478bd9Sstevel@tonic-gate if (mode & SM_EXDATA) 1566903a11ebSrh87107 (void) strcat(buf, "EXDATA "); 15677c478bd9Sstevel@tonic-gate if (mode & SM_OPTDATA) 1568903a11ebSrh87107 (void) strcat(buf, "OPTDATA "); 15697c478bd9Sstevel@tonic-gate if (mode & SM_BYTESTREAM) 1570903a11ebSrh87107 (void) strcat(buf, "BYTESTREAM "); 15717c478bd9Sstevel@tonic-gate return (buf); 15727c478bd9Sstevel@tonic-gate } 15737c478bd9Sstevel@tonic-gate 15747c478bd9Sstevel@tonic-gate char * 15757c478bd9Sstevel@tonic-gate pr_addr(int family, struct sockaddr *addr, t_uscalar_t addrlen) 15767c478bd9Sstevel@tonic-gate { 15777c478bd9Sstevel@tonic-gate static char buf[1024]; 15787c478bd9Sstevel@tonic-gate 15797c478bd9Sstevel@tonic-gate if (addr == NULL || addrlen == 0) { 1580903a11ebSrh87107 (void) sprintf(buf, "(len %d) %p", addrlen, (void *)addr); 15817c478bd9Sstevel@tonic-gate return (buf); 15827c478bd9Sstevel@tonic-gate } 15837c478bd9Sstevel@tonic-gate switch (family) { 15847c478bd9Sstevel@tonic-gate case AF_INET: { 15857c478bd9Sstevel@tonic-gate struct sockaddr_in sin; 15867c478bd9Sstevel@tonic-gate 15877c478bd9Sstevel@tonic-gate bcopy(addr, &sin, sizeof (sin)); 15887c478bd9Sstevel@tonic-gate 15897c478bd9Sstevel@tonic-gate (void) sprintf(buf, "(len %d) %x/%d", 1590b5fca8f8Stomee addrlen, ntohl(sin.sin_addr.s_addr), ntohs(sin.sin_port)); 15917c478bd9Sstevel@tonic-gate break; 15927c478bd9Sstevel@tonic-gate } 15937c478bd9Sstevel@tonic-gate case AF_INET6: { 15947c478bd9Sstevel@tonic-gate struct sockaddr_in6 sin6; 15957c478bd9Sstevel@tonic-gate uint16_t *piece = (uint16_t *)&sin6.sin6_addr; 15967c478bd9Sstevel@tonic-gate 15977c478bd9Sstevel@tonic-gate bcopy((char *)addr, (char *)&sin6, sizeof (sin6)); 1598903a11ebSrh87107 (void) sprintf(buf, "(len %d) %x:%x:%x:%x:%x:%x:%x:%x/%d", 15997c478bd9Sstevel@tonic-gate addrlen, 16007c478bd9Sstevel@tonic-gate ntohs(piece[0]), ntohs(piece[1]), 16017c478bd9Sstevel@tonic-gate ntohs(piece[2]), ntohs(piece[3]), 16027c478bd9Sstevel@tonic-gate ntohs(piece[4]), ntohs(piece[5]), 16037c478bd9Sstevel@tonic-gate ntohs(piece[6]), ntohs(piece[7]), 16047c478bd9Sstevel@tonic-gate ntohs(sin6.sin6_port)); 16057c478bd9Sstevel@tonic-gate break; 16067c478bd9Sstevel@tonic-gate } 16077c478bd9Sstevel@tonic-gate case AF_UNIX: { 16087c478bd9Sstevel@tonic-gate struct sockaddr_un *soun = (struct sockaddr_un *)addr; 16097c478bd9Sstevel@tonic-gate 1610b5fca8f8Stomee (void) sprintf(buf, "(len %d) %s", addrlen, 16117c478bd9Sstevel@tonic-gate (soun == NULL) ? "(none)" : soun->sun_path); 16127c478bd9Sstevel@tonic-gate break; 16137c478bd9Sstevel@tonic-gate } 16147c478bd9Sstevel@tonic-gate default: 16157c478bd9Sstevel@tonic-gate (void) sprintf(buf, "(unknown af %d)", family); 16167c478bd9Sstevel@tonic-gate break; 16177c478bd9Sstevel@tonic-gate } 16187c478bd9Sstevel@tonic-gate return (buf); 16197c478bd9Sstevel@tonic-gate } 16207c478bd9Sstevel@tonic-gate 16217c478bd9Sstevel@tonic-gate /* The logical equivalence operator (a if-and-only-if b) */ 162256f33205SJonathan Adams #define EQUIVALENT(a, b) (((a) && (b)) || (!(a) && (!(b)))) 16237c478bd9Sstevel@tonic-gate 16247c478bd9Sstevel@tonic-gate /* 16257c478bd9Sstevel@tonic-gate * Verify limitations and invariants on oob state. 16267c478bd9Sstevel@tonic-gate * Return 1 if OK, otherwise 0 so that it can be used as 16277c478bd9Sstevel@tonic-gate * ASSERT(verify_oobstate(so)); 16287c478bd9Sstevel@tonic-gate */ 16297c478bd9Sstevel@tonic-gate int 16307c478bd9Sstevel@tonic-gate so_verify_oobstate(struct sonode *so) 16317c478bd9Sstevel@tonic-gate { 16320f1702c5SYu Xiangning boolean_t havemark; 16330f1702c5SYu Xiangning 16347c478bd9Sstevel@tonic-gate ASSERT(MUTEX_HELD(&so->so_lock)); 16357c478bd9Sstevel@tonic-gate 16367c478bd9Sstevel@tonic-gate /* 16377c478bd9Sstevel@tonic-gate * The possible state combinations are: 16387c478bd9Sstevel@tonic-gate * 0 16397c478bd9Sstevel@tonic-gate * SS_OOBPEND 16407c478bd9Sstevel@tonic-gate * SS_OOBPEND|SS_HAVEOOBDATA 16417c478bd9Sstevel@tonic-gate * SS_OOBPEND|SS_HADOOBDATA 16427c478bd9Sstevel@tonic-gate * SS_HADOOBDATA 16437c478bd9Sstevel@tonic-gate */ 16447c478bd9Sstevel@tonic-gate switch (so->so_state & (SS_OOBPEND|SS_HAVEOOBDATA|SS_HADOOBDATA)) { 16457c478bd9Sstevel@tonic-gate case 0: 16467c478bd9Sstevel@tonic-gate case SS_OOBPEND: 16477c478bd9Sstevel@tonic-gate case SS_OOBPEND|SS_HAVEOOBDATA: 16487c478bd9Sstevel@tonic-gate case SS_OOBPEND|SS_HADOOBDATA: 16497c478bd9Sstevel@tonic-gate case SS_HADOOBDATA: 16507c478bd9Sstevel@tonic-gate break; 16517c478bd9Sstevel@tonic-gate default: 16520f1702c5SYu Xiangning printf("Bad oob state 1 (%p): state %s\n", 16530f1702c5SYu Xiangning (void *)so, pr_state(so->so_state, so->so_mode)); 16547c478bd9Sstevel@tonic-gate return (0); 16557c478bd9Sstevel@tonic-gate } 16567c478bd9Sstevel@tonic-gate 16577c478bd9Sstevel@tonic-gate /* SS_RCVATMARK should only be set when SS_OOBPEND is set */ 16587c478bd9Sstevel@tonic-gate if ((so->so_state & (SS_RCVATMARK|SS_OOBPEND)) == SS_RCVATMARK) { 16590f1702c5SYu Xiangning printf("Bad oob state 2 (%p): state %s\n", 16600f1702c5SYu Xiangning (void *)so, pr_state(so->so_state, so->so_mode)); 16617c478bd9Sstevel@tonic-gate return (0); 16627c478bd9Sstevel@tonic-gate } 16637c478bd9Sstevel@tonic-gate 16647c478bd9Sstevel@tonic-gate /* 16650f1702c5SYu Xiangning * (havemark != 0 or SS_RCVATMARK) iff SS_OOBPEND 16660f1702c5SYu Xiangning * For TPI, the presence of a "mark" is indicated by sti_oobsigcnt. 16677c478bd9Sstevel@tonic-gate */ 16680f1702c5SYu Xiangning havemark = (SOCK_IS_NONSTR(so)) ? so->so_oobmark > 0 : 16690f1702c5SYu Xiangning SOTOTPI(so)->sti_oobsigcnt > 0; 16700f1702c5SYu Xiangning 167156f33205SJonathan Adams if (!EQUIVALENT(havemark || (so->so_state & SS_RCVATMARK), 16727c478bd9Sstevel@tonic-gate so->so_state & SS_OOBPEND)) { 16730f1702c5SYu Xiangning printf("Bad oob state 3 (%p): state %s\n", 16740f1702c5SYu Xiangning (void *)so, pr_state(so->so_state, so->so_mode)); 16757c478bd9Sstevel@tonic-gate return (0); 16767c478bd9Sstevel@tonic-gate } 16777c478bd9Sstevel@tonic-gate 16787c478bd9Sstevel@tonic-gate /* 16797c478bd9Sstevel@tonic-gate * Unless SO_OOBINLINE we have so_oobmsg != NULL iff SS_HAVEOOBDATA 16807c478bd9Sstevel@tonic-gate */ 16817c478bd9Sstevel@tonic-gate if (!(so->so_options & SO_OOBINLINE) && 168256f33205SJonathan Adams !EQUIVALENT(so->so_oobmsg != NULL, so->so_state & SS_HAVEOOBDATA)) { 16830f1702c5SYu Xiangning printf("Bad oob state 4 (%p): state %s\n", 16840f1702c5SYu Xiangning (void *)so, pr_state(so->so_state, so->so_mode)); 16857c478bd9Sstevel@tonic-gate return (0); 16867c478bd9Sstevel@tonic-gate } 16870f1702c5SYu Xiangning 16880f1702c5SYu Xiangning if (!SOCK_IS_NONSTR(so) && 16890f1702c5SYu Xiangning SOTOTPI(so)->sti_oobsigcnt < SOTOTPI(so)->sti_oobcnt) { 16907c478bd9Sstevel@tonic-gate printf("Bad oob state 5 (%p): counts %d/%d state %s\n", 16910f1702c5SYu Xiangning (void *)so, SOTOTPI(so)->sti_oobsigcnt, 16920f1702c5SYu Xiangning SOTOTPI(so)->sti_oobcnt, 16930f1702c5SYu Xiangning pr_state(so->so_state, so->so_mode)); 16947c478bd9Sstevel@tonic-gate return (0); 16957c478bd9Sstevel@tonic-gate } 16960f1702c5SYu Xiangning 16977c478bd9Sstevel@tonic-gate return (1); 16987c478bd9Sstevel@tonic-gate } 169956f33205SJonathan Adams #undef EQUIVALENT 17007c478bd9Sstevel@tonic-gate #endif /* DEBUG */ 17017c478bd9Sstevel@tonic-gate 17027c478bd9Sstevel@tonic-gate /* initialize sockfs zone specific kstat related items */ 17037c478bd9Sstevel@tonic-gate void * 17047c478bd9Sstevel@tonic-gate sock_kstat_init(zoneid_t zoneid) 17057c478bd9Sstevel@tonic-gate { 17067c478bd9Sstevel@tonic-gate kstat_t *ksp; 17077c478bd9Sstevel@tonic-gate 17087c478bd9Sstevel@tonic-gate ksp = kstat_create_zone("sockfs", 0, "sock_unix_list", "misc", 17097c478bd9Sstevel@tonic-gate KSTAT_TYPE_RAW, 0, KSTAT_FLAG_VAR_SIZE|KSTAT_FLAG_VIRTUAL, zoneid); 17107c478bd9Sstevel@tonic-gate 17117c478bd9Sstevel@tonic-gate if (ksp != NULL) { 17127c478bd9Sstevel@tonic-gate ksp->ks_update = sockfs_update; 17137c478bd9Sstevel@tonic-gate ksp->ks_snapshot = sockfs_snapshot; 17147c478bd9Sstevel@tonic-gate ksp->ks_lock = &socklist.sl_lock; 17157c478bd9Sstevel@tonic-gate ksp->ks_private = (void *)(uintptr_t)zoneid; 17167c478bd9Sstevel@tonic-gate kstat_install(ksp); 17177c478bd9Sstevel@tonic-gate } 17187c478bd9Sstevel@tonic-gate 17197c478bd9Sstevel@tonic-gate return (ksp); 17207c478bd9Sstevel@tonic-gate } 17217c478bd9Sstevel@tonic-gate 17227c478bd9Sstevel@tonic-gate /* tear down sockfs zone specific kstat related items */ 17237c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 17247c478bd9Sstevel@tonic-gate void 17257c478bd9Sstevel@tonic-gate sock_kstat_fini(zoneid_t zoneid, void *arg) 17267c478bd9Sstevel@tonic-gate { 17277c478bd9Sstevel@tonic-gate kstat_t *ksp = (kstat_t *)arg; 17287c478bd9Sstevel@tonic-gate 17297c478bd9Sstevel@tonic-gate if (ksp != NULL) { 17307c478bd9Sstevel@tonic-gate ASSERT(zoneid == (zoneid_t)(uintptr_t)ksp->ks_private); 17317c478bd9Sstevel@tonic-gate kstat_delete(ksp); 17327c478bd9Sstevel@tonic-gate } 17337c478bd9Sstevel@tonic-gate } 17347c478bd9Sstevel@tonic-gate 17357c478bd9Sstevel@tonic-gate /* 17367c478bd9Sstevel@tonic-gate * Zones: 17377c478bd9Sstevel@tonic-gate * Note that nactive is going to be different for each zone. 17387c478bd9Sstevel@tonic-gate * This means we require kstat to call sockfs_update and then sockfs_snapshot 17397c478bd9Sstevel@tonic-gate * for the same zone, or sockfs_snapshot will be taken into the wrong size 17407c478bd9Sstevel@tonic-gate * buffer. This is safe, but if the buffer is too small, user will not be 17417c478bd9Sstevel@tonic-gate * given details of all sockets. However, as this kstat has a ks_lock, kstat 17427c478bd9Sstevel@tonic-gate * driver will keep it locked between the update and the snapshot, so no 17437c478bd9Sstevel@tonic-gate * other process (zone) can currently get inbetween resulting in a wrong size 17447c478bd9Sstevel@tonic-gate * buffer allocation. 17457c478bd9Sstevel@tonic-gate */ 17467c478bd9Sstevel@tonic-gate static int 17477c478bd9Sstevel@tonic-gate sockfs_update(kstat_t *ksp, int rw) 17487c478bd9Sstevel@tonic-gate { 17497c478bd9Sstevel@tonic-gate uint_t nactive = 0; /* # of active AF_UNIX sockets */ 17507c478bd9Sstevel@tonic-gate struct sonode *so; /* current sonode on socklist */ 17517c478bd9Sstevel@tonic-gate zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private; 17527c478bd9Sstevel@tonic-gate 17537c478bd9Sstevel@tonic-gate ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid()); 17547c478bd9Sstevel@tonic-gate 17557c478bd9Sstevel@tonic-gate if (rw == KSTAT_WRITE) { /* bounce all writes */ 17567c478bd9Sstevel@tonic-gate return (EACCES); 17577c478bd9Sstevel@tonic-gate } 17587c478bd9Sstevel@tonic-gate 17590f1702c5SYu Xiangning for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) { 17600f1702c5SYu Xiangning if (so->so_count != 0 && so->so_zoneid == myzoneid) { 17617c478bd9Sstevel@tonic-gate nactive++; 17627c478bd9Sstevel@tonic-gate } 17637c478bd9Sstevel@tonic-gate } 17647c478bd9Sstevel@tonic-gate ksp->ks_ndata = nactive; 17657c478bd9Sstevel@tonic-gate ksp->ks_data_size = nactive * sizeof (struct k_sockinfo); 17667c478bd9Sstevel@tonic-gate 17677c478bd9Sstevel@tonic-gate return (0); 17687c478bd9Sstevel@tonic-gate } 17697c478bd9Sstevel@tonic-gate 17707c478bd9Sstevel@tonic-gate static int 17717c478bd9Sstevel@tonic-gate sockfs_snapshot(kstat_t *ksp, void *buf, int rw) 17727c478bd9Sstevel@tonic-gate { 17737c478bd9Sstevel@tonic-gate int ns; /* # of sonodes we've copied */ 17747c478bd9Sstevel@tonic-gate struct sonode *so; /* current sonode on socklist */ 17757c478bd9Sstevel@tonic-gate struct k_sockinfo *pksi; /* where we put sockinfo data */ 17767c478bd9Sstevel@tonic-gate t_uscalar_t sn_len; /* soa_len */ 17777c478bd9Sstevel@tonic-gate zoneid_t myzoneid = (zoneid_t)(uintptr_t)ksp->ks_private; 17780f1702c5SYu Xiangning sotpi_info_t *sti; 17797c478bd9Sstevel@tonic-gate 17807c478bd9Sstevel@tonic-gate ASSERT((zoneid_t)(uintptr_t)ksp->ks_private == getzoneid()); 17817c478bd9Sstevel@tonic-gate 17827c478bd9Sstevel@tonic-gate ksp->ks_snaptime = gethrtime(); 17837c478bd9Sstevel@tonic-gate 17847c478bd9Sstevel@tonic-gate if (rw == KSTAT_WRITE) { /* bounce all writes */ 17857c478bd9Sstevel@tonic-gate return (EACCES); 17867c478bd9Sstevel@tonic-gate } 17877c478bd9Sstevel@tonic-gate 17887c478bd9Sstevel@tonic-gate /* 17897c478bd9Sstevel@tonic-gate * for each sonode on the socklist, we massage the important 17907c478bd9Sstevel@tonic-gate * info into buf, in k_sockinfo format. 17917c478bd9Sstevel@tonic-gate */ 17927c478bd9Sstevel@tonic-gate pksi = (struct k_sockinfo *)buf; 17930f1702c5SYu Xiangning ns = 0; 17940f1702c5SYu Xiangning for (so = socklist.sl_list; so != NULL; so = SOTOTPI(so)->sti_next_so) { 17957c478bd9Sstevel@tonic-gate /* only stuff active sonodes and the same zone: */ 17960f1702c5SYu Xiangning if (so->so_count == 0 || so->so_zoneid != myzoneid) { 17977c478bd9Sstevel@tonic-gate continue; 17987c478bd9Sstevel@tonic-gate } 17997c478bd9Sstevel@tonic-gate 18007c478bd9Sstevel@tonic-gate /* 18017c478bd9Sstevel@tonic-gate * If the sonode was activated between the update and the 18027c478bd9Sstevel@tonic-gate * snapshot, we're done - as this is only a snapshot. 18037c478bd9Sstevel@tonic-gate */ 18047c478bd9Sstevel@tonic-gate if ((caddr_t)(pksi) >= (caddr_t)buf + ksp->ks_data_size) { 18057c478bd9Sstevel@tonic-gate break; 18067c478bd9Sstevel@tonic-gate } 18077c478bd9Sstevel@tonic-gate 18080f1702c5SYu Xiangning sti = SOTOTPI(so); 18097c478bd9Sstevel@tonic-gate /* copy important info into buf: */ 18107c478bd9Sstevel@tonic-gate pksi->ks_si.si_size = sizeof (struct k_sockinfo); 18117c478bd9Sstevel@tonic-gate pksi->ks_si.si_family = so->so_family; 18127c478bd9Sstevel@tonic-gate pksi->ks_si.si_type = so->so_type; 18137c478bd9Sstevel@tonic-gate pksi->ks_si.si_flag = so->so_flag; 18147c478bd9Sstevel@tonic-gate pksi->ks_si.si_state = so->so_state; 18150f1702c5SYu Xiangning pksi->ks_si.si_serv_type = sti->sti_serv_type; 18160f1702c5SYu Xiangning pksi->ks_si.si_ux_laddr_sou_magic = 18170f1702c5SYu Xiangning sti->sti_ux_laddr.soua_magic; 18180f1702c5SYu Xiangning pksi->ks_si.si_ux_faddr_sou_magic = 18190f1702c5SYu Xiangning sti->sti_ux_faddr.soua_magic; 18200f1702c5SYu Xiangning pksi->ks_si.si_laddr_soa_len = sti->sti_laddr.soa_len; 18210f1702c5SYu Xiangning pksi->ks_si.si_faddr_soa_len = sti->sti_faddr.soa_len; 18227c478bd9Sstevel@tonic-gate pksi->ks_si.si_szoneid = so->so_zoneid; 18230f1702c5SYu Xiangning pksi->ks_si.si_faddr_noxlate = sti->sti_faddr_noxlate; 18247c478bd9Sstevel@tonic-gate 18257c478bd9Sstevel@tonic-gate mutex_enter(&so->so_lock); 18267c478bd9Sstevel@tonic-gate 18270f1702c5SYu Xiangning if (sti->sti_laddr_sa != NULL) { 18280f1702c5SYu Xiangning ASSERT(sti->sti_laddr_sa->sa_data != NULL); 18290f1702c5SYu Xiangning sn_len = sti->sti_laddr_len; 18307c478bd9Sstevel@tonic-gate ASSERT(sn_len <= sizeof (short) + 18317c478bd9Sstevel@tonic-gate sizeof (pksi->ks_si.si_laddr_sun_path)); 18327c478bd9Sstevel@tonic-gate 18337c478bd9Sstevel@tonic-gate pksi->ks_si.si_laddr_family = 18340f1702c5SYu Xiangning sti->sti_laddr_sa->sa_family; 18357c478bd9Sstevel@tonic-gate if (sn_len != 0) { 18367c478bd9Sstevel@tonic-gate /* AF_UNIX socket names are NULL terminated */ 18377c478bd9Sstevel@tonic-gate (void) strncpy(pksi->ks_si.si_laddr_sun_path, 18380f1702c5SYu Xiangning sti->sti_laddr_sa->sa_data, 18397c478bd9Sstevel@tonic-gate sizeof (pksi->ks_si.si_laddr_sun_path)); 18407c478bd9Sstevel@tonic-gate sn_len = strlen(pksi->ks_si.si_laddr_sun_path); 18417c478bd9Sstevel@tonic-gate } 18427c478bd9Sstevel@tonic-gate pksi->ks_si.si_laddr_sun_path[sn_len] = 0; 18437c478bd9Sstevel@tonic-gate } 18447c478bd9Sstevel@tonic-gate 18450f1702c5SYu Xiangning if (sti->sti_faddr_sa != NULL) { 18460f1702c5SYu Xiangning ASSERT(sti->sti_faddr_sa->sa_data != NULL); 18470f1702c5SYu Xiangning sn_len = sti->sti_faddr_len; 18487c478bd9Sstevel@tonic-gate ASSERT(sn_len <= sizeof (short) + 18497c478bd9Sstevel@tonic-gate sizeof (pksi->ks_si.si_faddr_sun_path)); 18507c478bd9Sstevel@tonic-gate 18517c478bd9Sstevel@tonic-gate pksi->ks_si.si_faddr_family = 18520f1702c5SYu Xiangning sti->sti_faddr_sa->sa_family; 18537c478bd9Sstevel@tonic-gate if (sn_len != 0) { 18547c478bd9Sstevel@tonic-gate (void) strncpy(pksi->ks_si.si_faddr_sun_path, 18550f1702c5SYu Xiangning sti->sti_faddr_sa->sa_data, 18567c478bd9Sstevel@tonic-gate sizeof (pksi->ks_si.si_faddr_sun_path)); 18577c478bd9Sstevel@tonic-gate sn_len = strlen(pksi->ks_si.si_faddr_sun_path); 18587c478bd9Sstevel@tonic-gate } 18597c478bd9Sstevel@tonic-gate pksi->ks_si.si_faddr_sun_path[sn_len] = 0; 18607c478bd9Sstevel@tonic-gate } 18617c478bd9Sstevel@tonic-gate 18627c478bd9Sstevel@tonic-gate mutex_exit(&so->so_lock); 18637c478bd9Sstevel@tonic-gate 18647c478bd9Sstevel@tonic-gate (void) sprintf(pksi->ks_straddr[0], "%p", (void *)so); 18657c478bd9Sstevel@tonic-gate (void) sprintf(pksi->ks_straddr[1], "%p", 18660f1702c5SYu Xiangning (void *)sti->sti_ux_laddr.soua_vp); 18677c478bd9Sstevel@tonic-gate (void) sprintf(pksi->ks_straddr[2], "%p", 18680f1702c5SYu Xiangning (void *)sti->sti_ux_faddr.soua_vp); 18697c478bd9Sstevel@tonic-gate 18707c478bd9Sstevel@tonic-gate ns++; 18717c478bd9Sstevel@tonic-gate pksi++; 18727c478bd9Sstevel@tonic-gate } 18737c478bd9Sstevel@tonic-gate 18747c478bd9Sstevel@tonic-gate ksp->ks_ndata = ns; 18757c478bd9Sstevel@tonic-gate return (0); 18767c478bd9Sstevel@tonic-gate } 18777c478bd9Sstevel@tonic-gate 18787c478bd9Sstevel@tonic-gate ssize_t 18797c478bd9Sstevel@tonic-gate soreadfile(file_t *fp, uchar_t *buf, u_offset_t fileoff, int *err, size_t size) 18807c478bd9Sstevel@tonic-gate { 18817c478bd9Sstevel@tonic-gate struct uio auio; 18827c478bd9Sstevel@tonic-gate struct iovec aiov[MSG_MAXIOVLEN]; 18837c478bd9Sstevel@tonic-gate register vnode_t *vp; 18847c478bd9Sstevel@tonic-gate int ioflag, rwflag; 18857c478bd9Sstevel@tonic-gate ssize_t cnt; 18867c478bd9Sstevel@tonic-gate int error = 0; 18877c478bd9Sstevel@tonic-gate int iovcnt = 0; 18887c478bd9Sstevel@tonic-gate short fflag; 18897c478bd9Sstevel@tonic-gate 18907c478bd9Sstevel@tonic-gate vp = fp->f_vnode; 18917c478bd9Sstevel@tonic-gate fflag = fp->f_flag; 18927c478bd9Sstevel@tonic-gate 18937c478bd9Sstevel@tonic-gate rwflag = 0; 18947c478bd9Sstevel@tonic-gate aiov[0].iov_base = (caddr_t)buf; 18957c478bd9Sstevel@tonic-gate aiov[0].iov_len = size; 18967c478bd9Sstevel@tonic-gate iovcnt = 1; 18977c478bd9Sstevel@tonic-gate cnt = (ssize_t)size; 18987c478bd9Sstevel@tonic-gate (void) VOP_RWLOCK(vp, rwflag, NULL); 18997c478bd9Sstevel@tonic-gate 19007c478bd9Sstevel@tonic-gate auio.uio_loffset = fileoff; 19017c478bd9Sstevel@tonic-gate auio.uio_iov = aiov; 19027c478bd9Sstevel@tonic-gate auio.uio_iovcnt = iovcnt; 19037c478bd9Sstevel@tonic-gate auio.uio_resid = cnt; 19047c478bd9Sstevel@tonic-gate auio.uio_segflg = UIO_SYSSPACE; 19057c478bd9Sstevel@tonic-gate auio.uio_llimit = MAXOFFSET_T; 19067c478bd9Sstevel@tonic-gate auio.uio_fmode = fflag; 19077c478bd9Sstevel@tonic-gate auio.uio_extflg = UIO_COPY_CACHED; 19087c478bd9Sstevel@tonic-gate 19097c478bd9Sstevel@tonic-gate ioflag = auio.uio_fmode & (FAPPEND|FSYNC|FDSYNC|FRSYNC); 19107c478bd9Sstevel@tonic-gate 19117c478bd9Sstevel@tonic-gate /* If read sync is not asked for, filter sync flags */ 19127c478bd9Sstevel@tonic-gate if ((ioflag & FRSYNC) == 0) 19137c478bd9Sstevel@tonic-gate ioflag &= ~(FSYNC|FDSYNC); 19147c478bd9Sstevel@tonic-gate error = VOP_READ(vp, &auio, ioflag, fp->f_cred, NULL); 19157c478bd9Sstevel@tonic-gate cnt -= auio.uio_resid; 19167c478bd9Sstevel@tonic-gate 19177c478bd9Sstevel@tonic-gate VOP_RWUNLOCK(vp, rwflag, NULL); 19187c478bd9Sstevel@tonic-gate 19197c478bd9Sstevel@tonic-gate if (error == EINTR && cnt != 0) 19207c478bd9Sstevel@tonic-gate error = 0; 19217c478bd9Sstevel@tonic-gate out: 19227c478bd9Sstevel@tonic-gate if (error != 0) { 19237c478bd9Sstevel@tonic-gate *err = error; 19247c478bd9Sstevel@tonic-gate return (0); 19257c478bd9Sstevel@tonic-gate } else { 19267c478bd9Sstevel@tonic-gate *err = 0; 19277c478bd9Sstevel@tonic-gate return (cnt); 19287c478bd9Sstevel@tonic-gate } 19297c478bd9Sstevel@tonic-gate } 19300f1702c5SYu Xiangning 19310f1702c5SYu Xiangning int 19320f1702c5SYu Xiangning so_copyin(const void *from, void *to, size_t size, int fromkernel) 19330f1702c5SYu Xiangning { 19340f1702c5SYu Xiangning if (fromkernel) { 19350f1702c5SYu Xiangning bcopy(from, to, size); 19360f1702c5SYu Xiangning return (0); 19370f1702c5SYu Xiangning } 19380f1702c5SYu Xiangning return (xcopyin(from, to, size)); 19390f1702c5SYu Xiangning } 19400f1702c5SYu Xiangning 19410f1702c5SYu Xiangning int 19420f1702c5SYu Xiangning so_copyout(const void *from, void *to, size_t size, int tokernel) 19430f1702c5SYu Xiangning { 19440f1702c5SYu Xiangning if (tokernel) { 19450f1702c5SYu Xiangning bcopy(from, to, size); 19460f1702c5SYu Xiangning return (0); 19470f1702c5SYu Xiangning } 19480f1702c5SYu Xiangning return (xcopyout(from, to, size)); 19490f1702c5SYu Xiangning } 1950