xref: /onnv-gate/usr/src/uts/common/fs/sockfs/socksyscalls.c (revision 408:11731380d322)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/types.h>
300Sstevel@tonic-gate #include <sys/t_lock.h>
310Sstevel@tonic-gate #include <sys/param.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/buf.h>
340Sstevel@tonic-gate #include <sys/conf.h>
350Sstevel@tonic-gate #include <sys/cred.h>
360Sstevel@tonic-gate #include <sys/kmem.h>
370Sstevel@tonic-gate #include <sys/sysmacros.h>
380Sstevel@tonic-gate #include <sys/vfs.h>
390Sstevel@tonic-gate #include <sys/vnode.h>
400Sstevel@tonic-gate #include <sys/debug.h>
410Sstevel@tonic-gate #include <sys/errno.h>
420Sstevel@tonic-gate #include <sys/time.h>
430Sstevel@tonic-gate #include <sys/file.h>
440Sstevel@tonic-gate #include <sys/open.h>
450Sstevel@tonic-gate #include <sys/user.h>
460Sstevel@tonic-gate #include <sys/termios.h>
470Sstevel@tonic-gate #include <sys/stream.h>
480Sstevel@tonic-gate #include <sys/strsubr.h>
490Sstevel@tonic-gate #include <sys/strsun.h>
500Sstevel@tonic-gate #include <sys/esunddi.h>
510Sstevel@tonic-gate #include <sys/flock.h>
520Sstevel@tonic-gate #include <sys/modctl.h>
530Sstevel@tonic-gate #include <sys/cmn_err.h>
540Sstevel@tonic-gate #include <sys/vmsystm.h>
550Sstevel@tonic-gate #include <sys/policy.h>
560Sstevel@tonic-gate 
570Sstevel@tonic-gate #include <sys/socket.h>
580Sstevel@tonic-gate #include <sys/socketvar.h>
590Sstevel@tonic-gate #include <netinet/in.h>
600Sstevel@tonic-gate #include <sys/un.h>
610Sstevel@tonic-gate #include <inet/nca/ncadoorhdr.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate #include <sys/isa_defs.h>
640Sstevel@tonic-gate #include <sys/inttypes.h>
650Sstevel@tonic-gate #include <sys/systm.h>
660Sstevel@tonic-gate #include <sys/cpuvar.h>
670Sstevel@tonic-gate #include <sys/atomic.h>
680Sstevel@tonic-gate #include <sys/filio.h>
690Sstevel@tonic-gate #include <sys/sendfile.h>
700Sstevel@tonic-gate #include <sys/ddi.h>
710Sstevel@tonic-gate #include <vm/seg.h>
720Sstevel@tonic-gate #include <vm/seg_map.h>
730Sstevel@tonic-gate #include <vm/seg_kpm.h>
740Sstevel@tonic-gate #include <fs/sockfs/nl7c.h>
750Sstevel@tonic-gate 
760Sstevel@tonic-gate #ifdef SOCK_TEST
770Sstevel@tonic-gate int do_useracc = 1;		/* Controlled by setting SO_DEBUG to 4 */
780Sstevel@tonic-gate #else
790Sstevel@tonic-gate #define	do_useracc	1
800Sstevel@tonic-gate #endif /* SOCK_TEST */
810Sstevel@tonic-gate 
820Sstevel@tonic-gate extern int xnet_truncate_print;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Note: DEF_IOV_MAX is defined and used as it is in "fs/vncalls.c"
860Sstevel@tonic-gate  *	 as there isn't a formal definition of IOV_MAX ???
870Sstevel@tonic-gate  */
880Sstevel@tonic-gate #define	MSG_MAXIOVLEN	16
890Sstevel@tonic-gate 
900Sstevel@tonic-gate /*
910Sstevel@tonic-gate  * Kernel component of socket creation.
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  * The socket library determines which version number to use.
940Sstevel@tonic-gate  * First the library calls this with a NULL devpath. If this fails
950Sstevel@tonic-gate  * to find a transport (using solookup) the library will look in /etc/netconfig
960Sstevel@tonic-gate  * for the appropriate transport. If one is found it will pass in the
970Sstevel@tonic-gate  * devpath for the kernel to use.
980Sstevel@tonic-gate  */
990Sstevel@tonic-gate int
1000Sstevel@tonic-gate so_socket(int domain, int type, int protocol, char *devpath, int version)
1010Sstevel@tonic-gate {
1020Sstevel@tonic-gate 	vnode_t *accessvp;
1030Sstevel@tonic-gate 	struct sonode *so;
1040Sstevel@tonic-gate 	vnode_t *vp;
1050Sstevel@tonic-gate 	struct file *fp;
1060Sstevel@tonic-gate 	int fd;
1070Sstevel@tonic-gate 	int error;
1080Sstevel@tonic-gate 	boolean_t wildcard = B_FALSE;
1090Sstevel@tonic-gate 	int saved_error = 0;
1100Sstevel@tonic-gate 	int sdomain = domain;
1110Sstevel@tonic-gate 
1120Sstevel@tonic-gate 	dprint(1, ("so_socket(%d,%d,%d,%p,%d)\n",
1130Sstevel@tonic-gate 		domain, type, protocol, devpath, version));
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	if (domain == AF_NCA) {
1160Sstevel@tonic-gate 		/*
1170Sstevel@tonic-gate 		 * The request is for an NCA socket so for NL7C use the
1180Sstevel@tonic-gate 		 * INET domain instead and mark NL7C_AF_NCA below.
1190Sstevel@tonic-gate 		 */
1200Sstevel@tonic-gate 		domain = AF_INET;
1210Sstevel@tonic-gate 		/*
1220Sstevel@tonic-gate 		 * NL7C is not supported in non-global zones,
1230Sstevel@tonic-gate 		 *  we enforce this restriction here.
1240Sstevel@tonic-gate 		 */
1250Sstevel@tonic-gate 		if (getzoneid() != GLOBAL_ZONEID) {
1260Sstevel@tonic-gate 			return (set_errno(ENOTSUP));
1270Sstevel@tonic-gate 		}
1280Sstevel@tonic-gate 	}
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	accessvp = solookup(domain, type, protocol, devpath, &error);
1310Sstevel@tonic-gate 	if (accessvp == NULL) {
1320Sstevel@tonic-gate 		/*
1330Sstevel@tonic-gate 		 * If there is either an EPROTONOSUPPORT or EPROTOTYPE error
1340Sstevel@tonic-gate 		 * it makes sense doing the wildcard lookup since the
1350Sstevel@tonic-gate 		 * protocol might not be in the table.
1360Sstevel@tonic-gate 		 */
1370Sstevel@tonic-gate 		if (devpath != NULL || protocol == 0 ||
1380Sstevel@tonic-gate 		    !(error == EPROTONOSUPPORT || error == EPROTOTYPE))
1390Sstevel@tonic-gate 			return (set_errno(error));
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 		saved_error = error;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 		/*
1440Sstevel@tonic-gate 		 * Try wildcard lookup. Never use devpath for wildcards.
1450Sstevel@tonic-gate 		 */
1460Sstevel@tonic-gate 		accessvp = solookup(domain, type, 0, NULL, &error);
1470Sstevel@tonic-gate 		if (accessvp == NULL) {
1480Sstevel@tonic-gate 			/*
1490Sstevel@tonic-gate 			 * Can't find in kernel table - have library
1500Sstevel@tonic-gate 			 * fall back to /etc/netconfig and tell us
1510Sstevel@tonic-gate 			 * the devpath (The library will do this if it didn't
1520Sstevel@tonic-gate 			 * already pass in a devpath).
1530Sstevel@tonic-gate 			 */
1540Sstevel@tonic-gate 			if (saved_error != 0)
1550Sstevel@tonic-gate 				error = saved_error;
1560Sstevel@tonic-gate 			return (set_errno(error));
1570Sstevel@tonic-gate 		}
1580Sstevel@tonic-gate 		wildcard = B_TRUE;
1590Sstevel@tonic-gate 	}
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate 	/* Check the device policy */
1620Sstevel@tonic-gate 	if ((error = secpolicy_spec_open(CRED(),
1630Sstevel@tonic-gate 	    accessvp, FREAD|FWRITE)) != 0) {
1640Sstevel@tonic-gate 		return (set_errno(error));
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate 
1670Sstevel@tonic-gate 	if (domain == AF_NCA) {
1680Sstevel@tonic-gate 		so = sonca_create(accessvp, domain, type, protocol, version,
1690Sstevel@tonic-gate 		    NULL, &error);
1700Sstevel@tonic-gate 	} else if (protocol == IPPROTO_SCTP) {
1710Sstevel@tonic-gate 		so = sosctp_create(accessvp, domain, type, protocol, version,
1720Sstevel@tonic-gate 		    NULL, &error);
1730Sstevel@tonic-gate 	} else {
1740Sstevel@tonic-gate 		so = sotpi_create(accessvp, domain, type, protocol, version,
1750Sstevel@tonic-gate 		    NULL, &error);
1760Sstevel@tonic-gate 	}
1770Sstevel@tonic-gate 	if (so == NULL) {
1780Sstevel@tonic-gate 		return (set_errno(error));
1790Sstevel@tonic-gate 	}
1800Sstevel@tonic-gate 	if (sdomain == AF_NCA && domain == AF_INET) {
1810Sstevel@tonic-gate 		so->so_nl7c_flags = NL7C_AF_NCA;
1820Sstevel@tonic-gate 	}
1830Sstevel@tonic-gate 	vp = SOTOV(so);
1840Sstevel@tonic-gate 
1850Sstevel@tonic-gate 	if (wildcard) {
1860Sstevel@tonic-gate 		/*
1870Sstevel@tonic-gate 		 * Issue SO_PROTOTYPE setsockopt.
1880Sstevel@tonic-gate 		 */
1890Sstevel@tonic-gate 		error = SOP_SETSOCKOPT(so, SOL_SOCKET, SO_PROTOTYPE,
1900Sstevel@tonic-gate 				&protocol,
1910Sstevel@tonic-gate 				(t_uscalar_t)sizeof (protocol));
1920Sstevel@tonic-gate 		if (error) {
1930Sstevel@tonic-gate 			(void) VOP_CLOSE(vp, 0, 1, 0, CRED());
1940Sstevel@tonic-gate 			VN_RELE(vp);
1950Sstevel@tonic-gate 			/*
1960Sstevel@tonic-gate 			 * Setsockopt often fails with ENOPROTOOPT but socket()
1970Sstevel@tonic-gate 			 * should fail with EPROTONOSUPPORT/EPROTOTYPE.
1980Sstevel@tonic-gate 			 */
1990Sstevel@tonic-gate 			if (saved_error != 0 && error == ENOPROTOOPT)
2000Sstevel@tonic-gate 				error = saved_error;
2010Sstevel@tonic-gate 			else
2020Sstevel@tonic-gate 				error = EPROTONOSUPPORT;
2030Sstevel@tonic-gate 			return (set_errno(error));
2040Sstevel@tonic-gate 		}
2050Sstevel@tonic-gate 	}
2060Sstevel@tonic-gate 	if (error = falloc(vp, FWRITE|FREAD, &fp, &fd)) {
2070Sstevel@tonic-gate 		(void) VOP_CLOSE(vp, 0, 1, 0, CRED());
2080Sstevel@tonic-gate 		VN_RELE(vp);
2090Sstevel@tonic-gate 		return (set_errno(error));
2100Sstevel@tonic-gate 	}
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * Now fill in the entries that falloc reserved
2140Sstevel@tonic-gate 	 */
2150Sstevel@tonic-gate 	mutex_exit(&fp->f_tlock);
2160Sstevel@tonic-gate 	setf(fd, fp);
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	return (fd);
2190Sstevel@tonic-gate }
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate /*
2220Sstevel@tonic-gate  * Map from a file descriptor to a socket node.
2230Sstevel@tonic-gate  * Returns with the file descriptor held i.e. the caller has to
2240Sstevel@tonic-gate  * use releasef when done with the file descriptor.
2250Sstevel@tonic-gate  */
2260Sstevel@tonic-gate static struct sonode *
2270Sstevel@tonic-gate getsonode(int sock, int *errorp, file_t **fpp)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	file_t *fp;
2300Sstevel@tonic-gate 	vnode_t *vp;
2310Sstevel@tonic-gate 	struct sonode *so;
2320Sstevel@tonic-gate 
2330Sstevel@tonic-gate 	if ((fp = getf(sock)) == NULL) {
2340Sstevel@tonic-gate 		*errorp = EBADF;
2350Sstevel@tonic-gate 		eprintline(*errorp);
2360Sstevel@tonic-gate 		return (NULL);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	vp = fp->f_vnode;
2390Sstevel@tonic-gate 	/* Check if it is a socket */
2400Sstevel@tonic-gate 	if (vp->v_type != VSOCK) {
2410Sstevel@tonic-gate 		releasef(sock);
2420Sstevel@tonic-gate 		*errorp = ENOTSOCK;
2430Sstevel@tonic-gate 		eprintline(*errorp);
2440Sstevel@tonic-gate 		return (NULL);
2450Sstevel@tonic-gate 	}
2460Sstevel@tonic-gate 	/*
2470Sstevel@tonic-gate 	 * Use the stream head to find the real socket vnode.
2480Sstevel@tonic-gate 	 * This is needed when namefs sits above sockfs.
2490Sstevel@tonic-gate 	 */
2500Sstevel@tonic-gate 	if (vp->v_stream) {
2510Sstevel@tonic-gate 		ASSERT(vp->v_stream->sd_vnode);
2520Sstevel@tonic-gate 		vp = vp->v_stream->sd_vnode;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 		so = VTOSO(vp);
2550Sstevel@tonic-gate 		if (so->so_version == SOV_STREAM) {
2560Sstevel@tonic-gate 			releasef(sock);
2570Sstevel@tonic-gate 			*errorp = ENOTSOCK;
2580Sstevel@tonic-gate 			eprintsoline(so, *errorp);
2590Sstevel@tonic-gate 			return (NULL);
2600Sstevel@tonic-gate 		}
2610Sstevel@tonic-gate 	} else {
2620Sstevel@tonic-gate 		so = VTOSO(vp);
2630Sstevel@tonic-gate 	}
2640Sstevel@tonic-gate 	if (fpp)
2650Sstevel@tonic-gate 		*fpp = fp;
2660Sstevel@tonic-gate 	return (so);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * Allocate and copyin a sockaddr.
2710Sstevel@tonic-gate  * Ensures NULL termination for AF_UNIX addresses by extending them
2720Sstevel@tonic-gate  * with one NULL byte if need be. Verifies that the length is not
2730Sstevel@tonic-gate  * excessive to prevent an application from consuming all of kernel
2740Sstevel@tonic-gate  * memory. Returns NULL when an error occurred.
2750Sstevel@tonic-gate  */
2760Sstevel@tonic-gate static struct sockaddr *
2770Sstevel@tonic-gate copyin_name(struct sonode *so, struct sockaddr *name, socklen_t *namelenp,
2780Sstevel@tonic-gate 	    int *errorp)
2790Sstevel@tonic-gate {
2800Sstevel@tonic-gate 	char	*faddr;
2810Sstevel@tonic-gate 	size_t	namelen = (size_t)*namelenp;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	ASSERT(namelen != 0);
2840Sstevel@tonic-gate 	if (namelen > SO_MAXARGSIZE) {
2850Sstevel@tonic-gate 		*errorp = EINVAL;
2860Sstevel@tonic-gate 		eprintsoline(so, *errorp);
2870Sstevel@tonic-gate 		return (NULL);
2880Sstevel@tonic-gate 	}
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	faddr = (char *)kmem_alloc(namelen, KM_SLEEP);
2910Sstevel@tonic-gate 	if (copyin(name, faddr, namelen)) {
2920Sstevel@tonic-gate 		kmem_free(faddr, namelen);
2930Sstevel@tonic-gate 		*errorp = EFAULT;
2940Sstevel@tonic-gate 		eprintsoline(so, *errorp);
2950Sstevel@tonic-gate 		return (NULL);
2960Sstevel@tonic-gate 	}
2970Sstevel@tonic-gate 
2980Sstevel@tonic-gate 	/*
2990Sstevel@tonic-gate 	 * Add space for NULL termination if needed.
3000Sstevel@tonic-gate 	 * Do a quick check if the last byte is NUL.
3010Sstevel@tonic-gate 	 */
3020Sstevel@tonic-gate 	if (so->so_family == AF_UNIX && faddr[namelen - 1] != '\0') {
3030Sstevel@tonic-gate 		/* Check if there is any NULL termination */
3040Sstevel@tonic-gate 		size_t	i;
3050Sstevel@tonic-gate 		int foundnull = 0;
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		for (i = sizeof (name->sa_family); i < namelen; i++) {
3080Sstevel@tonic-gate 			if (faddr[i] == '\0') {
3090Sstevel@tonic-gate 				foundnull = 1;
3100Sstevel@tonic-gate 				break;
3110Sstevel@tonic-gate 			}
3120Sstevel@tonic-gate 		}
3130Sstevel@tonic-gate 		if (!foundnull) {
3140Sstevel@tonic-gate 			/* Add extra byte for NUL padding */
3150Sstevel@tonic-gate 			char *nfaddr;
3160Sstevel@tonic-gate 
3170Sstevel@tonic-gate 			nfaddr = (char *)kmem_alloc(namelen + 1, KM_SLEEP);
3180Sstevel@tonic-gate 			bcopy(faddr, nfaddr, namelen);
3190Sstevel@tonic-gate 			kmem_free(faddr, namelen);
3200Sstevel@tonic-gate 
3210Sstevel@tonic-gate 			/* NUL terminate */
3220Sstevel@tonic-gate 			nfaddr[namelen] = '\0';
3230Sstevel@tonic-gate 			namelen++;
3240Sstevel@tonic-gate 			ASSERT((socklen_t)namelen == namelen);
3250Sstevel@tonic-gate 			*namelenp = (socklen_t)namelen;
3260Sstevel@tonic-gate 			faddr = nfaddr;
3270Sstevel@tonic-gate 		}
3280Sstevel@tonic-gate 	}
3290Sstevel@tonic-gate 	return ((struct sockaddr *)faddr);
3300Sstevel@tonic-gate }
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate /*
3330Sstevel@tonic-gate  * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL.
3340Sstevel@tonic-gate  */
3350Sstevel@tonic-gate static int
3360Sstevel@tonic-gate copyout_arg(void *uaddr, socklen_t ulen, void *ulenp,
3370Sstevel@tonic-gate 		void *kaddr, socklen_t klen)
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	if (uaddr != NULL) {
3400Sstevel@tonic-gate 		if (ulen > klen)
3410Sstevel@tonic-gate 			ulen = klen;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		if (ulen != 0) {
3440Sstevel@tonic-gate 			if (copyout(kaddr, uaddr, ulen))
3450Sstevel@tonic-gate 				return (EFAULT);
3460Sstevel@tonic-gate 		}
3470Sstevel@tonic-gate 	} else
3480Sstevel@tonic-gate 		ulen = 0;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	if (ulenp != NULL) {
3510Sstevel@tonic-gate 		if (copyout(&ulen, ulenp, sizeof (ulen)))
3520Sstevel@tonic-gate 			return (EFAULT);
3530Sstevel@tonic-gate 	}
3540Sstevel@tonic-gate 	return (0);
3550Sstevel@tonic-gate }
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate /*
3580Sstevel@tonic-gate  * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL.
3590Sstevel@tonic-gate  * If klen is greater than ulen it still uses the non-truncated
3600Sstevel@tonic-gate  * klen to update ulenp.
3610Sstevel@tonic-gate  */
3620Sstevel@tonic-gate static int
3630Sstevel@tonic-gate copyout_name(void *uaddr, socklen_t ulen, void *ulenp,
3640Sstevel@tonic-gate 		void *kaddr, socklen_t klen)
3650Sstevel@tonic-gate {
3660Sstevel@tonic-gate 	if (uaddr != NULL) {
3670Sstevel@tonic-gate 		if (ulen >= klen)
3680Sstevel@tonic-gate 			ulen = klen;
3690Sstevel@tonic-gate 		else if (ulen != 0 && xnet_truncate_print) {
3700Sstevel@tonic-gate 			printf("sockfs: truncating copyout of address using "
3710Sstevel@tonic-gate 			    "XNET semantics for pid = %d. Lengths %d, %d\n",
3720Sstevel@tonic-gate 			    curproc->p_pid, klen, ulen);
3730Sstevel@tonic-gate 		}
3740Sstevel@tonic-gate 
3750Sstevel@tonic-gate 		if (ulen != 0) {
3760Sstevel@tonic-gate 			if (copyout(kaddr, uaddr, ulen))
3770Sstevel@tonic-gate 				return (EFAULT);
3780Sstevel@tonic-gate 		} else
3790Sstevel@tonic-gate 			klen = 0;
3800Sstevel@tonic-gate 	} else
3810Sstevel@tonic-gate 		klen = 0;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	if (ulenp != NULL) {
3840Sstevel@tonic-gate 		if (copyout(&klen, ulenp, sizeof (klen)))
3850Sstevel@tonic-gate 			return (EFAULT);
3860Sstevel@tonic-gate 	}
3870Sstevel@tonic-gate 	return (0);
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3910Sstevel@tonic-gate  * The socketpair() code in libsocket creates two sockets (using
3920Sstevel@tonic-gate  * the /etc/netconfig fallback if needed) before calling this routine
3930Sstevel@tonic-gate  * to connect the two sockets together.
3940Sstevel@tonic-gate  *
3950Sstevel@tonic-gate  * For a SOCK_STREAM socketpair a listener is needed - in that case this
3960Sstevel@tonic-gate  * routine will create a new file descriptor as part of accepting the
3970Sstevel@tonic-gate  * connection. The library socketpair() will check if svs[2] has changed
3980Sstevel@tonic-gate  * in which case it will close the changed fd.
3990Sstevel@tonic-gate  *
4000Sstevel@tonic-gate  * Note that this code could use the TPI feature of accepting the connection
4010Sstevel@tonic-gate  * on the listening endpoint. However, that would require significant changes
4020Sstevel@tonic-gate  * to soaccept.
4030Sstevel@tonic-gate  */
4040Sstevel@tonic-gate int
4050Sstevel@tonic-gate so_socketpair(int sv[2])
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 	int svs[2];
4080Sstevel@tonic-gate 	struct sonode *so1, *so2;
4090Sstevel@tonic-gate 	int error;
4100Sstevel@tonic-gate 	struct sockaddr_ux *name;
4110Sstevel@tonic-gate 	size_t namelen;
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate 	dprint(1, ("so_socketpair(%p)\n", sv));
4140Sstevel@tonic-gate 
4150Sstevel@tonic-gate 	error = useracc(sv, sizeof (svs), B_WRITE);
4160Sstevel@tonic-gate 	if (error && do_useracc)
4170Sstevel@tonic-gate 		return (set_errno(EFAULT));
4180Sstevel@tonic-gate 
4190Sstevel@tonic-gate 	if (copyin(sv, svs, sizeof (svs)))
4200Sstevel@tonic-gate 		return (set_errno(EFAULT));
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate 	if ((so1 = getsonode(svs[0], &error, NULL)) == NULL)
4230Sstevel@tonic-gate 		return (set_errno(error));
4240Sstevel@tonic-gate 
4250Sstevel@tonic-gate 	if ((so2 = getsonode(svs[1], &error, NULL)) == NULL) {
4260Sstevel@tonic-gate 		releasef(svs[0]);
4270Sstevel@tonic-gate 		return (set_errno(error));
4280Sstevel@tonic-gate 	}
4290Sstevel@tonic-gate 
4300Sstevel@tonic-gate 	if (so1->so_family != AF_UNIX || so2->so_family != AF_UNIX) {
4310Sstevel@tonic-gate 		error = EOPNOTSUPP;
4320Sstevel@tonic-gate 		goto done;
4330Sstevel@tonic-gate 	}
4340Sstevel@tonic-gate 
4350Sstevel@tonic-gate 	/*
4360Sstevel@tonic-gate 	 * The code below makes assumptions about the "sockfs" implementation.
4370Sstevel@tonic-gate 	 * So make sure that the correct implementation is really used.
4380Sstevel@tonic-gate 	 */
4390Sstevel@tonic-gate 	ASSERT(so1->so_ops == &sotpi_sonodeops);
4400Sstevel@tonic-gate 	ASSERT(so2->so_ops == &sotpi_sonodeops);
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	if (so1->so_type == SOCK_DGRAM) {
4430Sstevel@tonic-gate 		/*
4440Sstevel@tonic-gate 		 * Bind both sockets and connect them with each other.
4450Sstevel@tonic-gate 		 * Need to allocate name/namelen for soconnect.
4460Sstevel@tonic-gate 		 */
4470Sstevel@tonic-gate 		error = SOP_BIND(so1, NULL, 0, _SOBIND_UNSPEC);
4480Sstevel@tonic-gate 		if (error) {
4490Sstevel@tonic-gate 			eprintsoline(so1, error);
4500Sstevel@tonic-gate 			goto done;
4510Sstevel@tonic-gate 		}
4520Sstevel@tonic-gate 		error = SOP_BIND(so2, NULL, 0, _SOBIND_UNSPEC);
4530Sstevel@tonic-gate 		if (error) {
4540Sstevel@tonic-gate 			eprintsoline(so2, error);
4550Sstevel@tonic-gate 			goto done;
4560Sstevel@tonic-gate 		}
4570Sstevel@tonic-gate 		namelen = sizeof (struct sockaddr_ux);
4580Sstevel@tonic-gate 		name = kmem_alloc(namelen, KM_SLEEP);
4590Sstevel@tonic-gate 		name->sou_family = AF_UNIX;
4600Sstevel@tonic-gate 		name->sou_addr = so2->so_ux_laddr;
4610Sstevel@tonic-gate 		error = SOP_CONNECT(so1,
4620Sstevel@tonic-gate 				(struct sockaddr *)name,
4630Sstevel@tonic-gate 				(socklen_t)namelen,
4640Sstevel@tonic-gate 				0, _SOCONNECT_NOXLATE);
4650Sstevel@tonic-gate 		if (error) {
4660Sstevel@tonic-gate 			kmem_free(name, namelen);
4670Sstevel@tonic-gate 			eprintsoline(so1, error);
4680Sstevel@tonic-gate 			goto done;
4690Sstevel@tonic-gate 		}
4700Sstevel@tonic-gate 		name->sou_addr = so1->so_ux_laddr;
4710Sstevel@tonic-gate 		error = SOP_CONNECT(so2,
4720Sstevel@tonic-gate 				(struct sockaddr *)name,
4730Sstevel@tonic-gate 				(socklen_t)namelen,
4740Sstevel@tonic-gate 				0, _SOCONNECT_NOXLATE);
4750Sstevel@tonic-gate 		kmem_free(name, namelen);
4760Sstevel@tonic-gate 		if (error) {
4770Sstevel@tonic-gate 			eprintsoline(so2, error);
4780Sstevel@tonic-gate 			goto done;
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 		releasef(svs[0]);
4810Sstevel@tonic-gate 		releasef(svs[1]);
4820Sstevel@tonic-gate 	} else {
4830Sstevel@tonic-gate 		/*
4840Sstevel@tonic-gate 		 * Bind both sockets, with so1 being a listener.
4850Sstevel@tonic-gate 		 * Connect so2 to so1 - nonblocking to avoid waiting for
4860Sstevel@tonic-gate 		 * soaccept to complete.
4870Sstevel@tonic-gate 		 * Accept a connection on so1. Pass out the new fd as sv[0].
4880Sstevel@tonic-gate 		 * The library will detect the changed fd and close
4890Sstevel@tonic-gate 		 * the original one.
4900Sstevel@tonic-gate 		 */
4910Sstevel@tonic-gate 		struct sonode *nso;
4920Sstevel@tonic-gate 		struct vnode *nvp;
4930Sstevel@tonic-gate 		struct file *nfp;
4940Sstevel@tonic-gate 		int nfd;
4950Sstevel@tonic-gate 
4960Sstevel@tonic-gate 		/*
4970Sstevel@tonic-gate 		 * We could simply call SOP_LISTEN() here (which would do the
4980Sstevel@tonic-gate 		 * binding automatically) if the code didn't rely on passing
4990Sstevel@tonic-gate 		 * _SOBIND_NOXLATE to the TPI implementation of SOP_BIND().
5000Sstevel@tonic-gate 		 */
5010Sstevel@tonic-gate 		error = SOP_BIND(so1, NULL, 0, _SOBIND_UNSPEC|_SOBIND_NOXLATE|
5020Sstevel@tonic-gate 		    _SOBIND_LISTEN|_SOBIND_SOCKETPAIR);
5030Sstevel@tonic-gate 		if (error) {
5040Sstevel@tonic-gate 			eprintsoline(so1, error);
5050Sstevel@tonic-gate 			goto done;
5060Sstevel@tonic-gate 		}
5070Sstevel@tonic-gate 		error = SOP_BIND(so2, NULL, 0, _SOBIND_UNSPEC);
5080Sstevel@tonic-gate 		if (error) {
5090Sstevel@tonic-gate 			eprintsoline(so2, error);
5100Sstevel@tonic-gate 			goto done;
5110Sstevel@tonic-gate 		}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 		namelen = sizeof (struct sockaddr_ux);
5140Sstevel@tonic-gate 		name = kmem_alloc(namelen, KM_SLEEP);
5150Sstevel@tonic-gate 		name->sou_family = AF_UNIX;
5160Sstevel@tonic-gate 		name->sou_addr = so1->so_ux_laddr;
5170Sstevel@tonic-gate 		error = SOP_CONNECT(so2,
5180Sstevel@tonic-gate 				(struct sockaddr *)name,
5190Sstevel@tonic-gate 				(socklen_t)namelen,
5200Sstevel@tonic-gate 				FNONBLOCK, _SOCONNECT_NOXLATE);
5210Sstevel@tonic-gate 		kmem_free(name, namelen);
5220Sstevel@tonic-gate 		if (error) {
5230Sstevel@tonic-gate 			if (error != EINPROGRESS) {
5240Sstevel@tonic-gate 				eprintsoline(so2, error);
5250Sstevel@tonic-gate 				goto done;
5260Sstevel@tonic-gate 			}
5270Sstevel@tonic-gate 		}
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate 		error = SOP_ACCEPT(so1, 0, &nso);
5300Sstevel@tonic-gate 		if (error) {
5310Sstevel@tonic-gate 			eprintsoline(so1, error);
5320Sstevel@tonic-gate 			goto done;
5330Sstevel@tonic-gate 		}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 		/* wait for so2 being SS_CONNECTED ignoring signals */
5360Sstevel@tonic-gate 		mutex_enter(&so2->so_lock);
5370Sstevel@tonic-gate 		error = sowaitconnected(so2, 0, 1);
5380Sstevel@tonic-gate 		mutex_exit(&so2->so_lock);
5390Sstevel@tonic-gate 		nvp = SOTOV(nso);
5400Sstevel@tonic-gate 		if (error != 0) {
5410Sstevel@tonic-gate 			(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
5420Sstevel@tonic-gate 			VN_RELE(nvp);
5430Sstevel@tonic-gate 			eprintsoline(so2, error);
5440Sstevel@tonic-gate 			goto done;
5450Sstevel@tonic-gate 		}
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 		if (error = falloc(nvp, FWRITE|FREAD, &nfp, &nfd)) {
5480Sstevel@tonic-gate 			(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
5490Sstevel@tonic-gate 			VN_RELE(nvp);
5500Sstevel@tonic-gate 			eprintsoline(nso, error);
5510Sstevel@tonic-gate 			goto done;
5520Sstevel@tonic-gate 		}
5530Sstevel@tonic-gate 		/*
5540Sstevel@tonic-gate 		 * fill in the entries that falloc reserved
5550Sstevel@tonic-gate 		 */
5560Sstevel@tonic-gate 		mutex_exit(&nfp->f_tlock);
5570Sstevel@tonic-gate 		setf(nfd, nfp);
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		releasef(svs[0]);
5600Sstevel@tonic-gate 		releasef(svs[1]);
5610Sstevel@tonic-gate 		svs[0] = nfd;
5620Sstevel@tonic-gate 
5630Sstevel@tonic-gate 		/*
5640Sstevel@tonic-gate 		 * The socketpair library routine will close the original
5650Sstevel@tonic-gate 		 * svs[0] when this code passes out a different file
5660Sstevel@tonic-gate 		 * descriptor.
5670Sstevel@tonic-gate 		 */
5680Sstevel@tonic-gate 		if (copyout(svs, sv, sizeof (svs))) {
5690Sstevel@tonic-gate 			(void) closeandsetf(nfd, NULL);
5700Sstevel@tonic-gate 			eprintline(EFAULT);
5710Sstevel@tonic-gate 			return (set_errno(EFAULT));
5720Sstevel@tonic-gate 		}
5730Sstevel@tonic-gate 	}
5740Sstevel@tonic-gate 	return (0);
5750Sstevel@tonic-gate 
5760Sstevel@tonic-gate done:
5770Sstevel@tonic-gate 	releasef(svs[0]);
5780Sstevel@tonic-gate 	releasef(svs[1]);
5790Sstevel@tonic-gate 	return (set_errno(error));
5800Sstevel@tonic-gate }
5810Sstevel@tonic-gate 
5820Sstevel@tonic-gate int
5830Sstevel@tonic-gate bind(int sock, struct sockaddr *name, socklen_t namelen, int version)
5840Sstevel@tonic-gate {
5850Sstevel@tonic-gate 	struct sonode *so;
5860Sstevel@tonic-gate 	int error;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	dprint(1, ("bind(%d, %p, %d)\n",
5890Sstevel@tonic-gate 		sock, name, namelen));
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, NULL)) == NULL)
5920Sstevel@tonic-gate 		return (set_errno(error));
5930Sstevel@tonic-gate 
5940Sstevel@tonic-gate 	/* Allocate and copyin name */
5950Sstevel@tonic-gate 	/*
5960Sstevel@tonic-gate 	 * X/Open test does not expect EFAULT with NULL name and non-zero
5970Sstevel@tonic-gate 	 * namelen.
5980Sstevel@tonic-gate 	 */
5990Sstevel@tonic-gate 	if (name != NULL && namelen != 0) {
6000Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
6010Sstevel@tonic-gate 		name = copyin_name(so, name, &namelen, &error);
6020Sstevel@tonic-gate 		if (name == NULL) {
6030Sstevel@tonic-gate 			releasef(sock);
6040Sstevel@tonic-gate 			return (set_errno(error));
6050Sstevel@tonic-gate 		}
6060Sstevel@tonic-gate 	} else {
6070Sstevel@tonic-gate 		name = NULL;
6080Sstevel@tonic-gate 		namelen = 0;
6090Sstevel@tonic-gate 	}
6100Sstevel@tonic-gate 
6110Sstevel@tonic-gate 	switch (version) {
6120Sstevel@tonic-gate 	default:
6130Sstevel@tonic-gate 		error = SOP_BIND(so, name, namelen, 0);
6140Sstevel@tonic-gate 		break;
6150Sstevel@tonic-gate 	case SOV_XPG4_2:
6160Sstevel@tonic-gate 		error = SOP_BIND(so, name, namelen, _SOBIND_XPG4_2);
6170Sstevel@tonic-gate 		break;
6180Sstevel@tonic-gate 	case SOV_SOCKBSD:
6190Sstevel@tonic-gate 		error = SOP_BIND(so, name, namelen, _SOBIND_SOCKBSD);
6200Sstevel@tonic-gate 		break;
6210Sstevel@tonic-gate 	}
6220Sstevel@tonic-gate done:
6230Sstevel@tonic-gate 	releasef(sock);
6240Sstevel@tonic-gate 	if (name != NULL)
6250Sstevel@tonic-gate 		kmem_free(name, (size_t)namelen);
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (error)
6280Sstevel@tonic-gate 		return (set_errno(error));
6290Sstevel@tonic-gate 	return (0);
6300Sstevel@tonic-gate }
6310Sstevel@tonic-gate 
6320Sstevel@tonic-gate /* ARGSUSED2 */
6330Sstevel@tonic-gate int
6340Sstevel@tonic-gate listen(int sock, int backlog, int version)
6350Sstevel@tonic-gate {
6360Sstevel@tonic-gate 	struct sonode *so;
6370Sstevel@tonic-gate 	int error;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	dprint(1, ("listen(%d, %d)\n",
6400Sstevel@tonic-gate 		sock, backlog));
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, NULL)) == NULL)
6430Sstevel@tonic-gate 		return (set_errno(error));
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate 	error = SOP_LISTEN(so, backlog);
6460Sstevel@tonic-gate 
6470Sstevel@tonic-gate 	releasef(sock);
6480Sstevel@tonic-gate 	if (error)
6490Sstevel@tonic-gate 		return (set_errno(error));
6500Sstevel@tonic-gate 	return (0);
6510Sstevel@tonic-gate }
6520Sstevel@tonic-gate 
6530Sstevel@tonic-gate /*ARGSUSED3*/
6540Sstevel@tonic-gate int
6550Sstevel@tonic-gate accept(int sock, struct sockaddr *name, socklen_t *namelenp, int version)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	struct sonode *so;
6580Sstevel@tonic-gate 	file_t *fp;
6590Sstevel@tonic-gate 	int error;
6600Sstevel@tonic-gate 	socklen_t namelen;
6610Sstevel@tonic-gate 	struct sonode *nso;
6620Sstevel@tonic-gate 	struct vnode *nvp;
6630Sstevel@tonic-gate 	struct file *nfp;
6640Sstevel@tonic-gate 	int nfd;
6650Sstevel@tonic-gate 
6660Sstevel@tonic-gate 	dprint(1, ("accept(%d, %p, %p)\n",
6670Sstevel@tonic-gate 		sock, name, namelenp));
6680Sstevel@tonic-gate 
6690Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, &fp)) == NULL)
6700Sstevel@tonic-gate 		return (set_errno(error));
6710Sstevel@tonic-gate 
6720Sstevel@tonic-gate 	if (name != NULL) {
6730Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
6740Sstevel@tonic-gate 		if (copyin(namelenp, &namelen, sizeof (namelen))) {
6750Sstevel@tonic-gate 			releasef(sock);
6760Sstevel@tonic-gate 			return (set_errno(EFAULT));
6770Sstevel@tonic-gate 		}
6780Sstevel@tonic-gate 		if (namelen != 0) {
6790Sstevel@tonic-gate 			error = useracc(name, (size_t)namelen, B_WRITE);
6800Sstevel@tonic-gate 			if (error && do_useracc) {
6810Sstevel@tonic-gate 				releasef(sock);
6820Sstevel@tonic-gate 				return (set_errno(EFAULT));
6830Sstevel@tonic-gate 			}
6840Sstevel@tonic-gate 		} else
6850Sstevel@tonic-gate 			name = NULL;
6860Sstevel@tonic-gate 	} else {
6870Sstevel@tonic-gate 		namelen = 0;
6880Sstevel@tonic-gate 	}
6890Sstevel@tonic-gate 
6900Sstevel@tonic-gate 	/*
6910Sstevel@tonic-gate 	 * Allocate the user fd before SOP_ACCEPT() in order to
6920Sstevel@tonic-gate 	 * catch EMFILE errors before calling SOP_ACCEPT().
6930Sstevel@tonic-gate 	 */
6940Sstevel@tonic-gate 	if ((nfd = ufalloc(0)) == -1) {
6950Sstevel@tonic-gate 		eprintsoline(so, EMFILE);
6960Sstevel@tonic-gate 		releasef(sock);
6970Sstevel@tonic-gate 		return (set_errno(EMFILE));
6980Sstevel@tonic-gate 	}
6990Sstevel@tonic-gate 	error = SOP_ACCEPT(so, fp->f_flag, &nso);
7000Sstevel@tonic-gate 	releasef(sock);
7010Sstevel@tonic-gate 	if (error) {
7020Sstevel@tonic-gate 		setf(nfd, NULL);
7030Sstevel@tonic-gate 		return (set_errno(error));
7040Sstevel@tonic-gate 	}
7050Sstevel@tonic-gate 
7060Sstevel@tonic-gate 	nvp = SOTOV(nso);
7070Sstevel@tonic-gate 
7080Sstevel@tonic-gate 	/*
7090Sstevel@tonic-gate 	 * so_faddr_sa can not go away even though we are not holding so_lock.
7100Sstevel@tonic-gate 	 * However, in theory its content could change from underneath us.
7110Sstevel@tonic-gate 	 * But this is not possible in practice since it can only
7120Sstevel@tonic-gate 	 * change due to either some socket system call
7130Sstevel@tonic-gate 	 * or due to a T_CONN_CON being received from the stream head.
7140Sstevel@tonic-gate 	 * Since the falloc/setf have not yet been done no thread
7150Sstevel@tonic-gate 	 * can do any system call on nso and T_CONN_CON can not arrive
7160Sstevel@tonic-gate 	 * on a socket that is already connected.
7170Sstevel@tonic-gate 	 * Thus there is no reason to hold so_lock here.
7180Sstevel@tonic-gate 	 *
7190Sstevel@tonic-gate 	 * SOP_ACCEPT() is required to have set the valid bit for the faddr,
7200Sstevel@tonic-gate 	 * but it could be instantly cleared by a disconnect from the transport.
7210Sstevel@tonic-gate 	 * For that reason we ignore it here.
7220Sstevel@tonic-gate 	 */
7230Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&nso->so_lock));
7240Sstevel@tonic-gate 	error = copyout_name(name, namelen, namelenp,
7250Sstevel@tonic-gate 	    nso->so_faddr_sa, (socklen_t)nso->so_faddr_len);
7260Sstevel@tonic-gate 	if (error) {
7270Sstevel@tonic-gate 		setf(nfd, NULL);
7280Sstevel@tonic-gate 		(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
7290Sstevel@tonic-gate 		VN_RELE(nvp);
7300Sstevel@tonic-gate 		return (set_errno(error));
7310Sstevel@tonic-gate 	}
7320Sstevel@tonic-gate 	if (error = falloc(NULL, FWRITE|FREAD, &nfp, NULL)) {
7330Sstevel@tonic-gate 		setf(nfd, NULL);
7340Sstevel@tonic-gate 		(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
7350Sstevel@tonic-gate 		VN_RELE(nvp);
7360Sstevel@tonic-gate 		eprintsoline(so, error);
7370Sstevel@tonic-gate 		return (set_errno(error));
7380Sstevel@tonic-gate 	}
7390Sstevel@tonic-gate 	/*
7400Sstevel@tonic-gate 	 * fill in the entries that falloc reserved
7410Sstevel@tonic-gate 	 */
7420Sstevel@tonic-gate 	nfp->f_vnode = nvp;
7430Sstevel@tonic-gate 	mutex_exit(&nfp->f_tlock);
7440Sstevel@tonic-gate 	setf(nfd, nfp);
7450Sstevel@tonic-gate 
7460Sstevel@tonic-gate 	/*
7470Sstevel@tonic-gate 	 * Copy FNDELAY and FNONBLOCK from listener to acceptor
7480Sstevel@tonic-gate 	 */
7490Sstevel@tonic-gate 	if (so->so_state & (SS_NDELAY|SS_NONBLOCK)) {
7500Sstevel@tonic-gate 		uint_t oflag = nfp->f_flag;
7510Sstevel@tonic-gate 		int arg = 0;
7520Sstevel@tonic-gate 
7530Sstevel@tonic-gate 		if (so->so_state & SS_NONBLOCK)
7540Sstevel@tonic-gate 			arg |= FNONBLOCK;
7550Sstevel@tonic-gate 		else if (so->so_state & SS_NDELAY)
7560Sstevel@tonic-gate 			arg |= FNDELAY;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 		/*
7590Sstevel@tonic-gate 		 * This code is a simplification of the F_SETFL code in fcntl()
7600Sstevel@tonic-gate 		 * Ignore any errors from VOP_SETFL.
7610Sstevel@tonic-gate 		 */
7620Sstevel@tonic-gate 		if ((error = VOP_SETFL(nvp, oflag, arg, nfp->f_cred)) != 0) {
7630Sstevel@tonic-gate 			eprintsoline(so, error);
7640Sstevel@tonic-gate 			error = 0;
7650Sstevel@tonic-gate 		} else {
7660Sstevel@tonic-gate 			mutex_enter(&nfp->f_tlock);
7670Sstevel@tonic-gate 			nfp->f_flag &= ~FMASK | (FREAD|FWRITE);
7680Sstevel@tonic-gate 			nfp->f_flag |= arg;
7690Sstevel@tonic-gate 			mutex_exit(&nfp->f_tlock);
7700Sstevel@tonic-gate 		}
7710Sstevel@tonic-gate 	}
7720Sstevel@tonic-gate 	return (nfd);
7730Sstevel@tonic-gate }
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate int
7760Sstevel@tonic-gate connect(int sock, struct sockaddr *name, socklen_t namelen, int version)
7770Sstevel@tonic-gate {
7780Sstevel@tonic-gate 	struct sonode *so;
7790Sstevel@tonic-gate 	file_t *fp;
7800Sstevel@tonic-gate 	int error;
7810Sstevel@tonic-gate 
7820Sstevel@tonic-gate 	dprint(1, ("connect(%d, %p, %d)\n",
7830Sstevel@tonic-gate 		sock, name, namelen));
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, &fp)) == NULL)
7860Sstevel@tonic-gate 		return (set_errno(error));
7870Sstevel@tonic-gate 
7880Sstevel@tonic-gate 	/* Allocate and copyin name */
7890Sstevel@tonic-gate 	if (namelen != 0) {
7900Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
7910Sstevel@tonic-gate 		name = copyin_name(so, name, &namelen, &error);
7920Sstevel@tonic-gate 		if (name == NULL) {
7930Sstevel@tonic-gate 			releasef(sock);
7940Sstevel@tonic-gate 			return (set_errno(error));
7950Sstevel@tonic-gate 		}
7960Sstevel@tonic-gate 	} else
7970Sstevel@tonic-gate 		name = NULL;
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate 	error = SOP_CONNECT(so, name, namelen, fp->f_flag,
8000Sstevel@tonic-gate 	    (version != SOV_XPG4_2) ? 0 : _SOCONNECT_XPG4_2);
8010Sstevel@tonic-gate 	releasef(sock);
8020Sstevel@tonic-gate 	if (name)
8030Sstevel@tonic-gate 		kmem_free(name, (size_t)namelen);
8040Sstevel@tonic-gate 	if (error)
8050Sstevel@tonic-gate 		return (set_errno(error));
8060Sstevel@tonic-gate 	return (0);
8070Sstevel@tonic-gate }
8080Sstevel@tonic-gate 
8090Sstevel@tonic-gate /*ARGSUSED2*/
8100Sstevel@tonic-gate int
8110Sstevel@tonic-gate shutdown(int sock, int how, int version)
8120Sstevel@tonic-gate {
8130Sstevel@tonic-gate 	struct sonode *so;
8140Sstevel@tonic-gate 	int error;
8150Sstevel@tonic-gate 
8160Sstevel@tonic-gate 	dprint(1, ("shutdown(%d, %d)\n",
8170Sstevel@tonic-gate 		sock, how));
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, NULL)) == NULL)
8200Sstevel@tonic-gate 		return (set_errno(error));
8210Sstevel@tonic-gate 
8220Sstevel@tonic-gate 	error = SOP_SHUTDOWN(so, how);
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate 	releasef(sock);
8250Sstevel@tonic-gate 	if (error)
8260Sstevel@tonic-gate 		return (set_errno(error));
8270Sstevel@tonic-gate 	return (0);
8280Sstevel@tonic-gate }
8290Sstevel@tonic-gate 
8300Sstevel@tonic-gate /*
8310Sstevel@tonic-gate  * Common receive routine.
8320Sstevel@tonic-gate  */
8330Sstevel@tonic-gate static ssize_t
8340Sstevel@tonic-gate recvit(int sock,
8350Sstevel@tonic-gate 	struct nmsghdr *msg,
8360Sstevel@tonic-gate 	struct uio *uiop,
8370Sstevel@tonic-gate 	int flags,
8380Sstevel@tonic-gate 	socklen_t *namelenp,
8390Sstevel@tonic-gate 	socklen_t *controllenp,
8400Sstevel@tonic-gate 	int *flagsp)
8410Sstevel@tonic-gate {
8420Sstevel@tonic-gate 	struct sonode *so;
8430Sstevel@tonic-gate 	file_t *fp;
8440Sstevel@tonic-gate 	void *name;
8450Sstevel@tonic-gate 	socklen_t namelen;
8460Sstevel@tonic-gate 	void *control;
8470Sstevel@tonic-gate 	socklen_t controllen;
8480Sstevel@tonic-gate 	ssize_t len;
8490Sstevel@tonic-gate 	int error;
8500Sstevel@tonic-gate 
8510Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, &fp)) == NULL)
8520Sstevel@tonic-gate 		return (set_errno(error));
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	len = uiop->uio_resid;
8550Sstevel@tonic-gate 	uiop->uio_fmode = fp->f_flag;
8560Sstevel@tonic-gate 	uiop->uio_extflg = UIO_COPY_CACHED;
8570Sstevel@tonic-gate 
8580Sstevel@tonic-gate 	name = msg->msg_name;
8590Sstevel@tonic-gate 	namelen = msg->msg_namelen;
8600Sstevel@tonic-gate 	control = msg->msg_control;
8610Sstevel@tonic-gate 	controllen = msg->msg_controllen;
8620Sstevel@tonic-gate 
8630Sstevel@tonic-gate 	msg->msg_flags = flags & (MSG_OOB | MSG_PEEK | MSG_WAITALL |
8640Sstevel@tonic-gate 	    MSG_DONTWAIT | MSG_XPG4_2);
8650Sstevel@tonic-gate 
8660Sstevel@tonic-gate 	error = SOP_RECVMSG(so, msg, uiop);
8670Sstevel@tonic-gate 	if (error) {
8680Sstevel@tonic-gate 		releasef(sock);
8690Sstevel@tonic-gate 		return (set_errno(error));
8700Sstevel@tonic-gate 	}
8710Sstevel@tonic-gate 	lwp_stat_update(LWP_STAT_MSGRCV, 1);
8720Sstevel@tonic-gate 	so_update_attrs(so, SOACC);
8730Sstevel@tonic-gate 	releasef(sock);
8740Sstevel@tonic-gate 
8750Sstevel@tonic-gate 	error = copyout_name(name, namelen, namelenp,
8760Sstevel@tonic-gate 	    msg->msg_name, msg->msg_namelen);
8770Sstevel@tonic-gate 	if (error)
8780Sstevel@tonic-gate 		goto err;
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	if (flagsp != NULL) {
8810Sstevel@tonic-gate 		/*
8820Sstevel@tonic-gate 		 * Clear internal flag.
8830Sstevel@tonic-gate 		 */
8840Sstevel@tonic-gate 		msg->msg_flags &= ~MSG_XPG4_2;
8850Sstevel@tonic-gate 
8860Sstevel@tonic-gate 		/*
8870Sstevel@tonic-gate 		 * Determine MSG_CTRUNC. sorecvmsg sets MSG_CTRUNC only
8880Sstevel@tonic-gate 		 * when controllen is zero and there is control data to
8890Sstevel@tonic-gate 		 * copy out.
8900Sstevel@tonic-gate 		 */
8910Sstevel@tonic-gate 		if (controllen != 0 &&
8920Sstevel@tonic-gate 		    (msg->msg_controllen > controllen || control == NULL)) {
8930Sstevel@tonic-gate 			dprint(1, ("recvit: CTRUNC %d %d %p\n",
8940Sstevel@tonic-gate 			    msg->msg_controllen, controllen, control));
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 			msg->msg_flags |= MSG_CTRUNC;
8970Sstevel@tonic-gate 		}
8980Sstevel@tonic-gate 		if (copyout(&msg->msg_flags, flagsp,
8990Sstevel@tonic-gate 		    sizeof (msg->msg_flags))) {
9000Sstevel@tonic-gate 			error = EFAULT;
9010Sstevel@tonic-gate 			goto err;
9020Sstevel@tonic-gate 		}
9030Sstevel@tonic-gate 	}
9040Sstevel@tonic-gate 	/*
9050Sstevel@tonic-gate 	 * Note: This MUST be done last. There can be no "goto err" after this
9060Sstevel@tonic-gate 	 * point since it could make so_closefds run twice on some part
9070Sstevel@tonic-gate 	 * of the file descriptor array.
9080Sstevel@tonic-gate 	 */
9090Sstevel@tonic-gate 	if (controllen != 0) {
9100Sstevel@tonic-gate 		if (!(flags & MSG_XPG4_2)) {
9110Sstevel@tonic-gate 			/*
9120Sstevel@tonic-gate 			 * Good old msg_accrights can only return a multiple
9130Sstevel@tonic-gate 			 * of 4 bytes.
9140Sstevel@tonic-gate 			 */
9150Sstevel@tonic-gate 			controllen &= ~((int)sizeof (uint32_t) - 1);
9160Sstevel@tonic-gate 		}
9170Sstevel@tonic-gate 		error = copyout_arg(control, controllen, controllenp,
9180Sstevel@tonic-gate 		    msg->msg_control, msg->msg_controllen);
9190Sstevel@tonic-gate 		if (error)
9200Sstevel@tonic-gate 			goto err;
9210Sstevel@tonic-gate 
9220Sstevel@tonic-gate 		if (msg->msg_controllen > controllen || control == NULL) {
9230Sstevel@tonic-gate 			if (control == NULL)
9240Sstevel@tonic-gate 				controllen = 0;
9250Sstevel@tonic-gate 			so_closefds(msg->msg_control, msg->msg_controllen,
9260Sstevel@tonic-gate 			    !(flags & MSG_XPG4_2), controllen);
9270Sstevel@tonic-gate 		}
9280Sstevel@tonic-gate 	}
9290Sstevel@tonic-gate 	if (msg->msg_namelen != 0)
9300Sstevel@tonic-gate 		kmem_free(msg->msg_name, (size_t)msg->msg_namelen);
9310Sstevel@tonic-gate 	if (msg->msg_controllen != 0)
9320Sstevel@tonic-gate 		kmem_free(msg->msg_control, (size_t)msg->msg_controllen);
9330Sstevel@tonic-gate 	return (len - uiop->uio_resid);
9340Sstevel@tonic-gate 
9350Sstevel@tonic-gate err:
9360Sstevel@tonic-gate 	/*
9370Sstevel@tonic-gate 	 * If we fail and the control part contains file descriptors
9380Sstevel@tonic-gate 	 * we have to close the fd's.
9390Sstevel@tonic-gate 	 */
9400Sstevel@tonic-gate 	if (msg->msg_controllen != 0)
9410Sstevel@tonic-gate 		so_closefds(msg->msg_control, msg->msg_controllen,
9420Sstevel@tonic-gate 		    !(flags & MSG_XPG4_2), 0);
9430Sstevel@tonic-gate 	if (msg->msg_namelen != 0)
9440Sstevel@tonic-gate 		kmem_free(msg->msg_name, (size_t)msg->msg_namelen);
9450Sstevel@tonic-gate 	if (msg->msg_controllen != 0)
9460Sstevel@tonic-gate 		kmem_free(msg->msg_control, (size_t)msg->msg_controllen);
9470Sstevel@tonic-gate 	return (set_errno(error));
9480Sstevel@tonic-gate }
9490Sstevel@tonic-gate 
9500Sstevel@tonic-gate /*
9510Sstevel@tonic-gate  * Native system call
9520Sstevel@tonic-gate  */
9530Sstevel@tonic-gate ssize_t
9540Sstevel@tonic-gate recv(int sock, void *buffer, size_t len, int flags)
9550Sstevel@tonic-gate {
9560Sstevel@tonic-gate 	struct nmsghdr lmsg;
9570Sstevel@tonic-gate 	struct uio auio;
9580Sstevel@tonic-gate 	struct iovec aiov[1];
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate 	dprint(1, ("recv(%d, %p, %ld, %d)\n",
9610Sstevel@tonic-gate 		sock, buffer, len, flags));
9620Sstevel@tonic-gate 
9630Sstevel@tonic-gate 	if ((ssize_t)len < 0) {
9640Sstevel@tonic-gate 		return (set_errno(EINVAL));
9650Sstevel@tonic-gate 	}
9660Sstevel@tonic-gate 
9670Sstevel@tonic-gate 	aiov[0].iov_base = buffer;
9680Sstevel@tonic-gate 	aiov[0].iov_len = len;
9690Sstevel@tonic-gate 	auio.uio_loffset = 0;
9700Sstevel@tonic-gate 	auio.uio_iov = aiov;
9710Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
9720Sstevel@tonic-gate 	auio.uio_resid = len;
9730Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
9740Sstevel@tonic-gate 	auio.uio_limit = 0;
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 	lmsg.msg_namelen = 0;
9770Sstevel@tonic-gate 	lmsg.msg_controllen = 0;
9780Sstevel@tonic-gate 	lmsg.msg_flags = 0;
9790Sstevel@tonic-gate 	return (recvit(sock, &lmsg, &auio, flags, NULL, NULL, NULL));
9800Sstevel@tonic-gate }
9810Sstevel@tonic-gate 
9820Sstevel@tonic-gate ssize_t
9830Sstevel@tonic-gate recvfrom(int sock, void *buffer, size_t len, int flags,
9840Sstevel@tonic-gate 	struct sockaddr *name, socklen_t *namelenp)
9850Sstevel@tonic-gate {
9860Sstevel@tonic-gate 	struct nmsghdr lmsg;
9870Sstevel@tonic-gate 	struct uio auio;
9880Sstevel@tonic-gate 	struct iovec aiov[1];
9890Sstevel@tonic-gate 
9900Sstevel@tonic-gate 	dprint(1, ("recvfrom(%d, %p, %ld, %d, %p, %p)\n",
9910Sstevel@tonic-gate 		sock, buffer, len, flags, name, namelenp));
9920Sstevel@tonic-gate 
9930Sstevel@tonic-gate 	if ((ssize_t)len < 0) {
9940Sstevel@tonic-gate 		return (set_errno(EINVAL));
9950Sstevel@tonic-gate 	}
9960Sstevel@tonic-gate 
9970Sstevel@tonic-gate 	aiov[0].iov_base = buffer;
9980Sstevel@tonic-gate 	aiov[0].iov_len = len;
9990Sstevel@tonic-gate 	auio.uio_loffset = 0;
10000Sstevel@tonic-gate 	auio.uio_iov = aiov;
10010Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
10020Sstevel@tonic-gate 	auio.uio_resid = len;
10030Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
10040Sstevel@tonic-gate 	auio.uio_limit = 0;
10050Sstevel@tonic-gate 
10060Sstevel@tonic-gate 	lmsg.msg_name = (char *)name;
10070Sstevel@tonic-gate 	if (namelenp != NULL) {
10080Sstevel@tonic-gate 		if (copyin(namelenp, &lmsg.msg_namelen,
10090Sstevel@tonic-gate 		    sizeof (lmsg.msg_namelen)))
10100Sstevel@tonic-gate 			return (set_errno(EFAULT));
10110Sstevel@tonic-gate 	} else {
10120Sstevel@tonic-gate 		lmsg.msg_namelen = 0;
10130Sstevel@tonic-gate 	}
10140Sstevel@tonic-gate 	lmsg.msg_controllen = 0;
10150Sstevel@tonic-gate 	lmsg.msg_flags = 0;
10160Sstevel@tonic-gate 
10170Sstevel@tonic-gate 	return (recvit(sock, &lmsg, &auio, flags, namelenp, NULL, NULL));
10180Sstevel@tonic-gate }
10190Sstevel@tonic-gate 
10200Sstevel@tonic-gate /*
10210Sstevel@tonic-gate  * Uses the MSG_XPG4_2 flag to determine if the caller is using
10220Sstevel@tonic-gate  * struct omsghdr or struct nmsghdr.
10230Sstevel@tonic-gate  */
10240Sstevel@tonic-gate ssize_t
10250Sstevel@tonic-gate recvmsg(int sock, struct nmsghdr *msg, int flags)
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	STRUCT_DECL(nmsghdr, u_lmsg);
10280Sstevel@tonic-gate 	STRUCT_HANDLE(nmsghdr, umsgptr);
10290Sstevel@tonic-gate 	struct nmsghdr lmsg;
10300Sstevel@tonic-gate 	struct uio auio;
10310Sstevel@tonic-gate 	struct iovec aiov[MSG_MAXIOVLEN];
10320Sstevel@tonic-gate 	int iovcnt;
10330Sstevel@tonic-gate 	ssize_t len;
10340Sstevel@tonic-gate 	int i;
10350Sstevel@tonic-gate 	int *flagsp;
10360Sstevel@tonic-gate 	model_t	model;
10370Sstevel@tonic-gate 
10380Sstevel@tonic-gate 	dprint(1, ("recvmsg(%d, %p, %d)\n",
10390Sstevel@tonic-gate 		sock, msg, flags));
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate 	model = get_udatamodel();
10420Sstevel@tonic-gate 	STRUCT_INIT(u_lmsg, model);
10430Sstevel@tonic-gate 	STRUCT_SET_HANDLE(umsgptr, model, msg);
10440Sstevel@tonic-gate 
10450Sstevel@tonic-gate 	if (flags & MSG_XPG4_2) {
10460Sstevel@tonic-gate 		if (copyin(msg, STRUCT_BUF(u_lmsg), STRUCT_SIZE(u_lmsg)))
10470Sstevel@tonic-gate 			return (set_errno(EFAULT));
10480Sstevel@tonic-gate 		flagsp = STRUCT_FADDR(umsgptr, msg_flags);
10490Sstevel@tonic-gate 	} else {
10500Sstevel@tonic-gate 		/*
10510Sstevel@tonic-gate 		 * Assumes that nmsghdr and omsghdr are identically shaped
10520Sstevel@tonic-gate 		 * except for the added msg_flags field.
10530Sstevel@tonic-gate 		 */
10540Sstevel@tonic-gate 		if (copyin(msg, STRUCT_BUF(u_lmsg),
10550Sstevel@tonic-gate 		    SIZEOF_STRUCT(omsghdr, model)))
10560Sstevel@tonic-gate 			return (set_errno(EFAULT));
10570Sstevel@tonic-gate 		STRUCT_FSET(u_lmsg, msg_flags, 0);
10580Sstevel@tonic-gate 		flagsp = NULL;
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 
10610Sstevel@tonic-gate 	/*
10620Sstevel@tonic-gate 	 * Code below us will kmem_alloc memory and hang it
10630Sstevel@tonic-gate 	 * off msg_control and msg_name fields. This forces
10640Sstevel@tonic-gate 	 * us to copy the structure to its native form.
10650Sstevel@tonic-gate 	 */
10660Sstevel@tonic-gate 	lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name);
10670Sstevel@tonic-gate 	lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen);
10680Sstevel@tonic-gate 	lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov);
10690Sstevel@tonic-gate 	lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen);
10700Sstevel@tonic-gate 	lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control);
10710Sstevel@tonic-gate 	lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen);
10720Sstevel@tonic-gate 	lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags);
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate 	iovcnt = lmsg.msg_iovlen;
10750Sstevel@tonic-gate 
10760Sstevel@tonic-gate 	if (iovcnt <= 0 || iovcnt > MSG_MAXIOVLEN) {
10770Sstevel@tonic-gate 		return (set_errno(EMSGSIZE));
10780Sstevel@tonic-gate 	}
10790Sstevel@tonic-gate 
10800Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
10810Sstevel@tonic-gate 	/*
10820Sstevel@tonic-gate 	 * 32-bit callers need to have their iovec expanded, while ensuring
10830Sstevel@tonic-gate 	 * that they can't move more than 2Gbytes of data in a single call.
10840Sstevel@tonic-gate 	 */
10850Sstevel@tonic-gate 	if (model == DATAMODEL_ILP32) {
10860Sstevel@tonic-gate 		struct iovec32 aiov32[MSG_MAXIOVLEN];
10870Sstevel@tonic-gate 		ssize32_t count32;
10880Sstevel@tonic-gate 
10890Sstevel@tonic-gate 		if (copyin((struct iovec32 *)lmsg.msg_iov, aiov32,
10900Sstevel@tonic-gate 		    iovcnt * sizeof (struct iovec32)))
10910Sstevel@tonic-gate 			return (set_errno(EFAULT));
10920Sstevel@tonic-gate 
10930Sstevel@tonic-gate 		count32 = 0;
10940Sstevel@tonic-gate 		for (i = 0; i < iovcnt; i++) {
10950Sstevel@tonic-gate 			ssize32_t iovlen32;
10960Sstevel@tonic-gate 
10970Sstevel@tonic-gate 			iovlen32 = aiov32[i].iov_len;
10980Sstevel@tonic-gate 			count32 += iovlen32;
10990Sstevel@tonic-gate 			if (iovlen32 < 0 || count32 < 0)
11000Sstevel@tonic-gate 				return (set_errno(EINVAL));
11010Sstevel@tonic-gate 			aiov[i].iov_len = iovlen32;
11020Sstevel@tonic-gate 			aiov[i].iov_base =
11030Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)aiov32[i].iov_base;
11040Sstevel@tonic-gate 		}
11050Sstevel@tonic-gate 	} else
11060Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
11070Sstevel@tonic-gate 	if (copyin(lmsg.msg_iov, aiov, iovcnt * sizeof (struct iovec))) {
11080Sstevel@tonic-gate 		return (set_errno(EFAULT));
11090Sstevel@tonic-gate 	}
11100Sstevel@tonic-gate 	len = 0;
11110Sstevel@tonic-gate 	for (i = 0; i < iovcnt; i++) {
11120Sstevel@tonic-gate 		ssize_t iovlen = aiov[i].iov_len;
11130Sstevel@tonic-gate 		len += iovlen;
11140Sstevel@tonic-gate 		if (iovlen < 0 || len < 0) {
11150Sstevel@tonic-gate 			return (set_errno(EINVAL));
11160Sstevel@tonic-gate 		}
11170Sstevel@tonic-gate 	}
11180Sstevel@tonic-gate 	auio.uio_loffset = 0;
11190Sstevel@tonic-gate 	auio.uio_iov = aiov;
11200Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
11210Sstevel@tonic-gate 	auio.uio_resid = len;
11220Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
11230Sstevel@tonic-gate 	auio.uio_limit = 0;
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate 	if (lmsg.msg_control != NULL &&
11260Sstevel@tonic-gate 	    (do_useracc == 0 ||
11270Sstevel@tonic-gate 	    useracc(lmsg.msg_control, lmsg.msg_controllen,
11280Sstevel@tonic-gate 			B_WRITE) != 0)) {
11290Sstevel@tonic-gate 		return (set_errno(EFAULT));
11300Sstevel@tonic-gate 	}
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 	return (recvit(sock, &lmsg, &auio, flags,
11330Sstevel@tonic-gate 		STRUCT_FADDR(umsgptr, msg_namelen),
11340Sstevel@tonic-gate 		STRUCT_FADDR(umsgptr, msg_controllen), flagsp));
11350Sstevel@tonic-gate }
11360Sstevel@tonic-gate 
11370Sstevel@tonic-gate /*
11380Sstevel@tonic-gate  * Common send function.
11390Sstevel@tonic-gate  */
11400Sstevel@tonic-gate static ssize_t
11410Sstevel@tonic-gate sendit(int sock, struct nmsghdr *msg, struct uio *uiop, int flags)
11420Sstevel@tonic-gate {
11430Sstevel@tonic-gate 	struct sonode *so;
11440Sstevel@tonic-gate 	file_t *fp;
11450Sstevel@tonic-gate 	void *name;
11460Sstevel@tonic-gate 	socklen_t namelen;
11470Sstevel@tonic-gate 	void *control;
11480Sstevel@tonic-gate 	socklen_t controllen;
11490Sstevel@tonic-gate 	ssize_t len;
11500Sstevel@tonic-gate 	int error;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, &fp)) == NULL)
11530Sstevel@tonic-gate 		return (set_errno(error));
11540Sstevel@tonic-gate 
11550Sstevel@tonic-gate 	uiop->uio_fmode = fp->f_flag;
11560Sstevel@tonic-gate 
11570Sstevel@tonic-gate 	if (so->so_family == AF_UNIX)
11580Sstevel@tonic-gate 		uiop->uio_extflg = UIO_COPY_CACHED;
11590Sstevel@tonic-gate 	else
11600Sstevel@tonic-gate 		uiop->uio_extflg = UIO_COPY_DEFAULT;
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate 	/* Allocate and copyin name and control */
11630Sstevel@tonic-gate 	name = msg->msg_name;
11640Sstevel@tonic-gate 	namelen = msg->msg_namelen;
11650Sstevel@tonic-gate 	if (name != NULL && namelen != 0) {
11660Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
11670Sstevel@tonic-gate 		name = copyin_name(so,
11680Sstevel@tonic-gate 				(struct sockaddr *)name,
11690Sstevel@tonic-gate 				&namelen, &error);
11700Sstevel@tonic-gate 		if (name == NULL)
11710Sstevel@tonic-gate 			goto done3;
11720Sstevel@tonic-gate 		/* copyin_name null terminates addresses for AF_UNIX */
11730Sstevel@tonic-gate 		msg->msg_namelen = namelen;
11740Sstevel@tonic-gate 		msg->msg_name = name;
11750Sstevel@tonic-gate 	} else {
11760Sstevel@tonic-gate 		msg->msg_name = name = NULL;
11770Sstevel@tonic-gate 		msg->msg_namelen = namelen = 0;
11780Sstevel@tonic-gate 	}
11790Sstevel@tonic-gate 
11800Sstevel@tonic-gate 	control = msg->msg_control;
11810Sstevel@tonic-gate 	controllen = msg->msg_controllen;
11820Sstevel@tonic-gate 	if ((control != NULL) && (controllen != 0)) {
11830Sstevel@tonic-gate 		/*
11840Sstevel@tonic-gate 		 * Verify that the length is not excessive to prevent
11850Sstevel@tonic-gate 		 * an application from consuming all of kernel memory.
11860Sstevel@tonic-gate 		 */
11870Sstevel@tonic-gate 		if (controllen > SO_MAXARGSIZE) {
11880Sstevel@tonic-gate 			error = EINVAL;
11890Sstevel@tonic-gate 			goto done2;
11900Sstevel@tonic-gate 		}
11910Sstevel@tonic-gate 		control = kmem_alloc(controllen, KM_SLEEP);
11920Sstevel@tonic-gate 
11930Sstevel@tonic-gate 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
11940Sstevel@tonic-gate 		if (copyin(msg->msg_control, control, controllen)) {
11950Sstevel@tonic-gate 			error = EFAULT;
11960Sstevel@tonic-gate 			goto done1;
11970Sstevel@tonic-gate 		}
11980Sstevel@tonic-gate 		msg->msg_control = control;
11990Sstevel@tonic-gate 	} else {
12000Sstevel@tonic-gate 		msg->msg_control = control = NULL;
12010Sstevel@tonic-gate 		msg->msg_controllen = controllen = 0;
12020Sstevel@tonic-gate 	}
12030Sstevel@tonic-gate 
12040Sstevel@tonic-gate 	len = uiop->uio_resid;
12050Sstevel@tonic-gate 	msg->msg_flags = flags;
12060Sstevel@tonic-gate 
12070Sstevel@tonic-gate 	error = SOP_SENDMSG(so, msg, uiop);
12080Sstevel@tonic-gate done1:
12090Sstevel@tonic-gate 	if (control != NULL)
12100Sstevel@tonic-gate 		kmem_free(control, controllen);
12110Sstevel@tonic-gate done2:
12120Sstevel@tonic-gate 	if (name != NULL)
12130Sstevel@tonic-gate 		kmem_free(name, namelen);
12140Sstevel@tonic-gate done3:
12150Sstevel@tonic-gate 	if (error != 0) {
12160Sstevel@tonic-gate 		releasef(sock);
12170Sstevel@tonic-gate 		return (set_errno(error));
12180Sstevel@tonic-gate 	}
12190Sstevel@tonic-gate 	lwp_stat_update(LWP_STAT_MSGSND, 1);
12200Sstevel@tonic-gate 	so_update_attrs(so, SOMOD);
12210Sstevel@tonic-gate 	releasef(sock);
12220Sstevel@tonic-gate 	return (len - uiop->uio_resid);
12230Sstevel@tonic-gate }
12240Sstevel@tonic-gate 
12250Sstevel@tonic-gate /*
12260Sstevel@tonic-gate  * Native system call
12270Sstevel@tonic-gate  */
12280Sstevel@tonic-gate ssize_t
12290Sstevel@tonic-gate send(int sock, void *buffer, size_t len, int flags)
12300Sstevel@tonic-gate {
12310Sstevel@tonic-gate 	struct nmsghdr lmsg;
12320Sstevel@tonic-gate 	struct uio auio;
12330Sstevel@tonic-gate 	struct iovec aiov[1];
12340Sstevel@tonic-gate 
12350Sstevel@tonic-gate 	dprint(1, ("send(%d, %p, %ld, %d)\n",
12360Sstevel@tonic-gate 		sock, buffer, len, flags));
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 	if ((ssize_t)len < 0) {
12390Sstevel@tonic-gate 		return (set_errno(EINVAL));
12400Sstevel@tonic-gate 	}
12410Sstevel@tonic-gate 
12420Sstevel@tonic-gate 	aiov[0].iov_base = buffer;
12430Sstevel@tonic-gate 	aiov[0].iov_len = len;
12440Sstevel@tonic-gate 	auio.uio_loffset = 0;
12450Sstevel@tonic-gate 	auio.uio_iov = aiov;
12460Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
12470Sstevel@tonic-gate 	auio.uio_resid = len;
12480Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
12490Sstevel@tonic-gate 	auio.uio_limit = 0;
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	lmsg.msg_name = NULL;
12520Sstevel@tonic-gate 	lmsg.msg_control = NULL;
12530Sstevel@tonic-gate 	if (!(flags & MSG_XPG4_2)) {
12540Sstevel@tonic-gate 		/*
12550Sstevel@tonic-gate 		 * In order to be compatible with the libsocket/sockmod
12560Sstevel@tonic-gate 		 * implementation we set EOR for all send* calls.
12570Sstevel@tonic-gate 		 */
12580Sstevel@tonic-gate 		flags |= MSG_EOR;
12590Sstevel@tonic-gate 	}
12600Sstevel@tonic-gate 	return (sendit(sock, &lmsg, &auio, flags));
12610Sstevel@tonic-gate }
12620Sstevel@tonic-gate 
12630Sstevel@tonic-gate /*
12640Sstevel@tonic-gate  * Uses the MSG_XPG4_2 flag to determine if the caller is using
12650Sstevel@tonic-gate  * struct omsghdr or struct nmsghdr.
12660Sstevel@tonic-gate  */
12670Sstevel@tonic-gate ssize_t
12680Sstevel@tonic-gate sendmsg(int sock, struct nmsghdr *msg, int flags)
12690Sstevel@tonic-gate {
12700Sstevel@tonic-gate 	struct nmsghdr lmsg;
12710Sstevel@tonic-gate 	STRUCT_DECL(nmsghdr, u_lmsg);
12720Sstevel@tonic-gate 	struct uio auio;
12730Sstevel@tonic-gate 	struct iovec aiov[MSG_MAXIOVLEN];
12740Sstevel@tonic-gate 	int iovcnt;
12750Sstevel@tonic-gate 	ssize_t len;
12760Sstevel@tonic-gate 	int i;
12770Sstevel@tonic-gate 	model_t	model;
12780Sstevel@tonic-gate 
12790Sstevel@tonic-gate 	dprint(1, ("sendmsg(%d, %p, %d)\n", sock, msg, flags));
12800Sstevel@tonic-gate 
12810Sstevel@tonic-gate 	model = get_udatamodel();
12820Sstevel@tonic-gate 	STRUCT_INIT(u_lmsg, model);
12830Sstevel@tonic-gate 
12840Sstevel@tonic-gate 	if (flags & MSG_XPG4_2) {
12850Sstevel@tonic-gate 		if (copyin(msg, (char *)STRUCT_BUF(u_lmsg),
12860Sstevel@tonic-gate 		    STRUCT_SIZE(u_lmsg)))
12870Sstevel@tonic-gate 			return (set_errno(EFAULT));
12880Sstevel@tonic-gate 	} else {
12890Sstevel@tonic-gate 		/*
12900Sstevel@tonic-gate 		 * Assumes that nmsghdr and omsghdr are identically shaped
12910Sstevel@tonic-gate 		 * except for the added msg_flags field.
12920Sstevel@tonic-gate 		 */
12930Sstevel@tonic-gate 		if (copyin(msg, (char *)STRUCT_BUF(u_lmsg),
12940Sstevel@tonic-gate 		    SIZEOF_STRUCT(omsghdr, model)))
12950Sstevel@tonic-gate 			return (set_errno(EFAULT));
12960Sstevel@tonic-gate 		/*
12970Sstevel@tonic-gate 		 * In order to be compatible with the libsocket/sockmod
12980Sstevel@tonic-gate 		 * implementation we set EOR for all send* calls.
12990Sstevel@tonic-gate 		 */
13000Sstevel@tonic-gate 		flags |= MSG_EOR;
13010Sstevel@tonic-gate 	}
13020Sstevel@tonic-gate 
13030Sstevel@tonic-gate 	/*
13040Sstevel@tonic-gate 	 * Code below us will kmem_alloc memory and hang it
13050Sstevel@tonic-gate 	 * off msg_control and msg_name fields. This forces
13060Sstevel@tonic-gate 	 * us to copy the structure to its native form.
13070Sstevel@tonic-gate 	 */
13080Sstevel@tonic-gate 	lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name);
13090Sstevel@tonic-gate 	lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen);
13100Sstevel@tonic-gate 	lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov);
13110Sstevel@tonic-gate 	lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen);
13120Sstevel@tonic-gate 	lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control);
13130Sstevel@tonic-gate 	lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen);
13140Sstevel@tonic-gate 	lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags);
13150Sstevel@tonic-gate 
13160Sstevel@tonic-gate 	iovcnt = lmsg.msg_iovlen;
13170Sstevel@tonic-gate 
13180Sstevel@tonic-gate 	if (iovcnt <= 0 || iovcnt > MSG_MAXIOVLEN) {
13190Sstevel@tonic-gate 		/*
13200Sstevel@tonic-gate 		 * Unless this is XPG 4.2 we allow iovcnt == 0 to
13210Sstevel@tonic-gate 		 * be compatible with SunOS 4.X and 4.4BSD.
13220Sstevel@tonic-gate 		 */
13230Sstevel@tonic-gate 		if (iovcnt != 0 || (flags & MSG_XPG4_2))
13240Sstevel@tonic-gate 			return (set_errno(EMSGSIZE));
13250Sstevel@tonic-gate 	}
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
13280Sstevel@tonic-gate 	/*
13290Sstevel@tonic-gate 	 * 32-bit callers need to have their iovec expanded, while ensuring
13300Sstevel@tonic-gate 	 * that they can't move more than 2Gbytes of data in a single call.
13310Sstevel@tonic-gate 	 */
13320Sstevel@tonic-gate 	if (model == DATAMODEL_ILP32) {
13330Sstevel@tonic-gate 		struct iovec32 aiov32[MSG_MAXIOVLEN];
13340Sstevel@tonic-gate 		ssize32_t count32;
13350Sstevel@tonic-gate 
13360Sstevel@tonic-gate 		if (iovcnt != 0 &&
13370Sstevel@tonic-gate 		    copyin((struct iovec32 *)lmsg.msg_iov, aiov32,
13380Sstevel@tonic-gate 		    iovcnt * sizeof (struct iovec32)))
13390Sstevel@tonic-gate 			return (set_errno(EFAULT));
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		count32 = 0;
13420Sstevel@tonic-gate 		for (i = 0; i < iovcnt; i++) {
13430Sstevel@tonic-gate 			ssize32_t iovlen32;
13440Sstevel@tonic-gate 
13450Sstevel@tonic-gate 			iovlen32 = aiov32[i].iov_len;
13460Sstevel@tonic-gate 			count32 += iovlen32;
13470Sstevel@tonic-gate 			if (iovlen32 < 0 || count32 < 0)
13480Sstevel@tonic-gate 				return (set_errno(EINVAL));
13490Sstevel@tonic-gate 			aiov[i].iov_len = iovlen32;
13500Sstevel@tonic-gate 			aiov[i].iov_base =
13510Sstevel@tonic-gate 			    (caddr_t)(uintptr_t)aiov32[i].iov_base;
13520Sstevel@tonic-gate 		}
13530Sstevel@tonic-gate 	} else
13540Sstevel@tonic-gate #endif /* _SYSCALL32_IMPL */
13550Sstevel@tonic-gate 	if (iovcnt != 0 &&
13560Sstevel@tonic-gate 	    copyin(lmsg.msg_iov, aiov,
13570Sstevel@tonic-gate 	    (unsigned)iovcnt * sizeof (struct iovec))) {
13580Sstevel@tonic-gate 		return (set_errno(EFAULT));
13590Sstevel@tonic-gate 	}
13600Sstevel@tonic-gate 	len = 0;
13610Sstevel@tonic-gate 	for (i = 0; i < iovcnt; i++) {
13620Sstevel@tonic-gate 		ssize_t iovlen = aiov[i].iov_len;
13630Sstevel@tonic-gate 		len += iovlen;
13640Sstevel@tonic-gate 		if (iovlen < 0 || len < 0) {
13650Sstevel@tonic-gate 			return (set_errno(EINVAL));
13660Sstevel@tonic-gate 		}
13670Sstevel@tonic-gate 	}
13680Sstevel@tonic-gate 	auio.uio_loffset = 0;
13690Sstevel@tonic-gate 	auio.uio_iov = aiov;
13700Sstevel@tonic-gate 	auio.uio_iovcnt = iovcnt;
13710Sstevel@tonic-gate 	auio.uio_resid = len;
13720Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
13730Sstevel@tonic-gate 	auio.uio_limit = 0;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 	return (sendit(sock, &lmsg, &auio, flags));
13760Sstevel@tonic-gate }
13770Sstevel@tonic-gate 
13780Sstevel@tonic-gate ssize_t
13790Sstevel@tonic-gate sendto(int sock, void *buffer, size_t len, int flags,
13800Sstevel@tonic-gate     struct sockaddr *name, socklen_t namelen)
13810Sstevel@tonic-gate {
13820Sstevel@tonic-gate 	struct nmsghdr lmsg;
13830Sstevel@tonic-gate 	struct uio auio;
13840Sstevel@tonic-gate 	struct iovec aiov[1];
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	dprint(1, ("sendto(%d, %p, %ld, %d, %p, %d)\n",
13870Sstevel@tonic-gate 		sock, buffer, len, flags, name, namelen));
13880Sstevel@tonic-gate 
13890Sstevel@tonic-gate 	if ((ssize_t)len < 0) {
13900Sstevel@tonic-gate 		return (set_errno(EINVAL));
13910Sstevel@tonic-gate 	}
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate 	aiov[0].iov_base = buffer;
13940Sstevel@tonic-gate 	aiov[0].iov_len = len;
13950Sstevel@tonic-gate 	auio.uio_loffset = 0;
13960Sstevel@tonic-gate 	auio.uio_iov = aiov;
13970Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
13980Sstevel@tonic-gate 	auio.uio_resid = len;
13990Sstevel@tonic-gate 	auio.uio_segflg = UIO_USERSPACE;
14000Sstevel@tonic-gate 	auio.uio_limit = 0;
14010Sstevel@tonic-gate 
14020Sstevel@tonic-gate 	lmsg.msg_name = (char *)name;
14030Sstevel@tonic-gate 	lmsg.msg_namelen = namelen;
14040Sstevel@tonic-gate 	lmsg.msg_control = NULL;
14050Sstevel@tonic-gate 	if (!(flags & MSG_XPG4_2)) {
14060Sstevel@tonic-gate 		/*
14070Sstevel@tonic-gate 		 * In order to be compatible with the libsocket/sockmod
14080Sstevel@tonic-gate 		 * implementation we set EOR for all send* calls.
14090Sstevel@tonic-gate 		 */
14100Sstevel@tonic-gate 		flags |= MSG_EOR;
14110Sstevel@tonic-gate 	}
14120Sstevel@tonic-gate 	return (sendit(sock, &lmsg, &auio, flags));
14130Sstevel@tonic-gate }
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate /*ARGSUSED3*/
14160Sstevel@tonic-gate int
14170Sstevel@tonic-gate getpeername(int sock, struct sockaddr *name, socklen_t *namelenp, int version)
14180Sstevel@tonic-gate {
14190Sstevel@tonic-gate 	struct sonode *so;
14200Sstevel@tonic-gate 	int error;
14210Sstevel@tonic-gate 	socklen_t namelen;
14220Sstevel@tonic-gate 	union {
14230Sstevel@tonic-gate 		struct sockaddr_in sin;
14240Sstevel@tonic-gate 		struct sockaddr_in6 sin6;
14250Sstevel@tonic-gate 	} sin;			/* Temporary buffer, common case */
14260Sstevel@tonic-gate 	void *addr;		/* Temporary buffer, uncommon case */
14270Sstevel@tonic-gate 	socklen_t addrlen, size;
14280Sstevel@tonic-gate 
14290Sstevel@tonic-gate 	dprint(1, ("getpeername(%d, %p, %p)\n",
14300Sstevel@tonic-gate 		sock, name, namelenp));
14310Sstevel@tonic-gate 
14320Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, NULL)) == NULL)
14330Sstevel@tonic-gate 		goto bad;
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
14360Sstevel@tonic-gate 	if (copyin(namelenp, &namelen, sizeof (namelen)) ||
14370Sstevel@tonic-gate 	    (name == NULL && namelen != 0)) {
14380Sstevel@tonic-gate 		error = EFAULT;
14390Sstevel@tonic-gate 		goto rel_out;
14400Sstevel@tonic-gate 	}
14410Sstevel@tonic-gate 	/*
14420Sstevel@tonic-gate 	 * If a connect or accept has been done, unless we're an Xnet socket,
14430Sstevel@tonic-gate 	 * the remote address has already been updated in so_faddr_sa.
14440Sstevel@tonic-gate 	 */
14450Sstevel@tonic-gate 	if (so->so_version != SOV_SOCKSTREAM && so->so_version != SOV_SOCKBSD ||
14460Sstevel@tonic-gate 	    !(so->so_state & SS_FADDR_VALID)) {
14470Sstevel@tonic-gate 		if ((error = SOP_GETPEERNAME(so)) != 0)
14480Sstevel@tonic-gate 			goto rel_out;
14490Sstevel@tonic-gate 	}
14500Sstevel@tonic-gate 
14510Sstevel@tonic-gate 	if (so->so_faddr_maxlen <= sizeof (sin)) {
14520Sstevel@tonic-gate 		size = 0;
14530Sstevel@tonic-gate 		addr = &sin;
14540Sstevel@tonic-gate 	} else {
14550Sstevel@tonic-gate 		/*
14560Sstevel@tonic-gate 		 * Allocate temporary to avoid holding so_lock across
14570Sstevel@tonic-gate 		 * copyout
14580Sstevel@tonic-gate 		 */
14590Sstevel@tonic-gate 		size = so->so_faddr_maxlen;
14600Sstevel@tonic-gate 		addr = kmem_alloc(size, KM_SLEEP);
14610Sstevel@tonic-gate 	}
14620Sstevel@tonic-gate 	/* Prevent so_faddr_sa/len from changing while accessed */
14630Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
14640Sstevel@tonic-gate 	if (!(so->so_state & SS_ISCONNECTED)) {
14650Sstevel@tonic-gate 		mutex_exit(&so->so_lock);
14660Sstevel@tonic-gate 		error = ENOTCONN;
14670Sstevel@tonic-gate 		goto free_out;
14680Sstevel@tonic-gate 	}
14690Sstevel@tonic-gate 	addrlen = so->so_faddr_len;
14700Sstevel@tonic-gate 	bcopy(so->so_faddr_sa, addr, addrlen);
14710Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
14740Sstevel@tonic-gate 	error = copyout_name(name, namelen, namelenp, addr,
14750Sstevel@tonic-gate 	    (so->so_state & SS_FADDR_NOXLATE) ? 0 : addrlen);
14760Sstevel@tonic-gate free_out:
14770Sstevel@tonic-gate 	if (size != 0)
14780Sstevel@tonic-gate 		kmem_free(addr, size);
14790Sstevel@tonic-gate rel_out:
14800Sstevel@tonic-gate 	releasef(sock);
14810Sstevel@tonic-gate bad:	return (error != 0 ? set_errno(error) : 0);
14820Sstevel@tonic-gate }
14830Sstevel@tonic-gate 
14840Sstevel@tonic-gate /*ARGSUSED3*/
14850Sstevel@tonic-gate int
14860Sstevel@tonic-gate getsockname(int sock, struct sockaddr *name,
14870Sstevel@tonic-gate 		socklen_t *namelenp, int version)
14880Sstevel@tonic-gate {
14890Sstevel@tonic-gate 	struct sonode *so;
14900Sstevel@tonic-gate 	int error;
14910Sstevel@tonic-gate 	socklen_t namelen;
14920Sstevel@tonic-gate 	union {
14930Sstevel@tonic-gate 		struct sockaddr_in sin;
14940Sstevel@tonic-gate 		struct sockaddr_in6 sin6;
14950Sstevel@tonic-gate 	} sin;			/* Temporary buffer, common case */
14960Sstevel@tonic-gate 	void *addr;		/* Temporary buffer, uncommon case */
14970Sstevel@tonic-gate 	socklen_t addrlen, size;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	dprint(1, ("getsockname(%d, %p, %p)\n",
15000Sstevel@tonic-gate 		sock, name, namelenp));
15010Sstevel@tonic-gate 
15020Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, NULL)) == NULL)
15030Sstevel@tonic-gate 		goto bad;
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
15060Sstevel@tonic-gate 	if (copyin(namelenp, &namelen, sizeof (namelen)) ||
15070Sstevel@tonic-gate 	    (name == NULL && namelen != 0)) {
15080Sstevel@tonic-gate 		error = EFAULT;
15090Sstevel@tonic-gate 		goto rel_out;
15100Sstevel@tonic-gate 	}
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	/*
15130Sstevel@tonic-gate 	 * If a bind or accept has been done, unless we're an Xnet endpoint,
15140Sstevel@tonic-gate 	 * the local address has already been updated in so_laddr_sa.
15150Sstevel@tonic-gate 	 */
15160Sstevel@tonic-gate 	if ((so->so_version != SOV_SOCKSTREAM &&
15170Sstevel@tonic-gate 	    so->so_version != SOV_SOCKBSD) ||
15180Sstevel@tonic-gate 	    !(so->so_state & SS_LADDR_VALID)) {
15190Sstevel@tonic-gate 		if ((error = SOP_GETSOCKNAME(so)) != 0)
15200Sstevel@tonic-gate 			goto rel_out;
15210Sstevel@tonic-gate 	}
15220Sstevel@tonic-gate 
15230Sstevel@tonic-gate 	if (so->so_laddr_maxlen <= sizeof (sin)) {
15240Sstevel@tonic-gate 		size = 0;
15250Sstevel@tonic-gate 		addr = &sin;
15260Sstevel@tonic-gate 	} else {
15270Sstevel@tonic-gate 		/*
15280Sstevel@tonic-gate 		 * Allocate temporary to avoid holding so_lock across
15290Sstevel@tonic-gate 		 * copyout
15300Sstevel@tonic-gate 		 */
15310Sstevel@tonic-gate 		size = so->so_laddr_maxlen;
15320Sstevel@tonic-gate 		addr = kmem_alloc(size, KM_SLEEP);
15330Sstevel@tonic-gate 	}
15340Sstevel@tonic-gate 	/* Prevent so_laddr_sa/len from changing while accessed */
15350Sstevel@tonic-gate 	mutex_enter(&so->so_lock);
15360Sstevel@tonic-gate 	addrlen = so->so_laddr_len;
15370Sstevel@tonic-gate 	bcopy(so->so_laddr_sa, addr, addrlen);
15380Sstevel@tonic-gate 	mutex_exit(&so->so_lock);
15390Sstevel@tonic-gate 
15400Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
15410Sstevel@tonic-gate 	error = copyout_name(name, namelen, namelenp,
15420Sstevel@tonic-gate 	    addr, addrlen);
15430Sstevel@tonic-gate 	if (size != 0)
15440Sstevel@tonic-gate 		kmem_free(addr, size);
15450Sstevel@tonic-gate rel_out:
15460Sstevel@tonic-gate 	releasef(sock);
15470Sstevel@tonic-gate bad:	return (error != 0 ? set_errno(error) : 0);
15480Sstevel@tonic-gate }
15490Sstevel@tonic-gate 
15500Sstevel@tonic-gate /*ARGSUSED5*/
15510Sstevel@tonic-gate int
15520Sstevel@tonic-gate getsockopt(int sock,
15530Sstevel@tonic-gate 	int level,
15540Sstevel@tonic-gate 	int option_name,
15550Sstevel@tonic-gate 	void *option_value,
15560Sstevel@tonic-gate 	socklen_t *option_lenp,
15570Sstevel@tonic-gate 	int version)
15580Sstevel@tonic-gate {
15590Sstevel@tonic-gate 	struct sonode *so;
15600Sstevel@tonic-gate 	socklen_t optlen, optlen_res;
15610Sstevel@tonic-gate 	void *optval;
15620Sstevel@tonic-gate 	int error;
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	dprint(1, ("getsockopt(%d, %d, %d, %p, %p)\n",
15650Sstevel@tonic-gate 		sock, level, option_name, option_value, option_lenp));
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, NULL)) == NULL)
15680Sstevel@tonic-gate 		return (set_errno(error));
15690Sstevel@tonic-gate 
15700Sstevel@tonic-gate 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
15710Sstevel@tonic-gate 	if (copyin(option_lenp, &optlen, sizeof (optlen))) {
15720Sstevel@tonic-gate 		releasef(sock);
15730Sstevel@tonic-gate 		return (set_errno(EFAULT));
15740Sstevel@tonic-gate 	}
15750Sstevel@tonic-gate 	/*
15760Sstevel@tonic-gate 	 * Verify that the length is not excessive to prevent
15770Sstevel@tonic-gate 	 * an application from consuming all of kernel memory.
15780Sstevel@tonic-gate 	 */
15790Sstevel@tonic-gate 	if (optlen > SO_MAXARGSIZE) {
15800Sstevel@tonic-gate 		error = EINVAL;
15810Sstevel@tonic-gate 		releasef(sock);
15820Sstevel@tonic-gate 		return (set_errno(error));
15830Sstevel@tonic-gate 	}
15840Sstevel@tonic-gate 	optval = kmem_alloc(optlen, KM_SLEEP);
15850Sstevel@tonic-gate 	optlen_res = optlen;
15860Sstevel@tonic-gate 	error = SOP_GETSOCKOPT(so, level, option_name, optval,
15870Sstevel@tonic-gate 	    &optlen_res, (version != SOV_XPG4_2) ? 0 : _SOGETSOCKOPT_XPG4_2);
15880Sstevel@tonic-gate 	releasef(sock);
15890Sstevel@tonic-gate 	if (error) {
15900Sstevel@tonic-gate 		kmem_free(optval, optlen);
15910Sstevel@tonic-gate 		return (set_errno(error));
15920Sstevel@tonic-gate 	}
15930Sstevel@tonic-gate 	error = copyout_arg(option_value, optlen, option_lenp,
15940Sstevel@tonic-gate 	    optval, optlen_res);
15950Sstevel@tonic-gate 	kmem_free(optval, optlen);
15960Sstevel@tonic-gate 	if (error)
15970Sstevel@tonic-gate 		return (set_errno(error));
15980Sstevel@tonic-gate 	return (0);
15990Sstevel@tonic-gate }
16000Sstevel@tonic-gate 
16010Sstevel@tonic-gate /*ARGSUSED5*/
16020Sstevel@tonic-gate int
16030Sstevel@tonic-gate setsockopt(int sock,
16040Sstevel@tonic-gate 	int level,
16050Sstevel@tonic-gate 	int option_name,
16060Sstevel@tonic-gate 	void *option_value,
16070Sstevel@tonic-gate 	socklen_t option_len,
16080Sstevel@tonic-gate 	int version)
16090Sstevel@tonic-gate {
16100Sstevel@tonic-gate 	struct sonode *so;
16110Sstevel@tonic-gate 	intptr_t buffer[2];
16120Sstevel@tonic-gate 	void *optval = NULL;
16130Sstevel@tonic-gate 	int error;
16140Sstevel@tonic-gate 
16150Sstevel@tonic-gate 	dprint(1, ("setsockopt(%d, %d, %d, %p, %d)\n",
16160Sstevel@tonic-gate 		sock, level, option_name, option_value, option_len));
16170Sstevel@tonic-gate 
16180Sstevel@tonic-gate 	if ((so = getsonode(sock, &error, NULL)) == NULL)
16190Sstevel@tonic-gate 		return (set_errno(error));
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate 	if (option_value != NULL) {
16220Sstevel@tonic-gate 		if (option_len != 0) {
16230Sstevel@tonic-gate 			/*
16240Sstevel@tonic-gate 			 * Verify that the length is not excessive to prevent
16250Sstevel@tonic-gate 			 * an application from consuming all of kernel memory.
16260Sstevel@tonic-gate 			 */
16270Sstevel@tonic-gate 			if (option_len > SO_MAXARGSIZE) {
16280Sstevel@tonic-gate 				error = EINVAL;
16290Sstevel@tonic-gate 				goto done2;
16300Sstevel@tonic-gate 			}
16310Sstevel@tonic-gate 			optval = option_len <= sizeof (buffer) ?
16320Sstevel@tonic-gate 			    &buffer : kmem_alloc((size_t)option_len, KM_SLEEP);
16330Sstevel@tonic-gate 			ASSERT(MUTEX_NOT_HELD(&so->so_lock));
16340Sstevel@tonic-gate 			if (copyin(option_value, optval, (size_t)option_len)) {
16350Sstevel@tonic-gate 				error = EFAULT;
16360Sstevel@tonic-gate 				goto done1;
16370Sstevel@tonic-gate 			}
16380Sstevel@tonic-gate 		}
16390Sstevel@tonic-gate 	} else
16400Sstevel@tonic-gate 		option_len = 0;
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate 	error = SOP_SETSOCKOPT(so, level, option_name, optval,
16430Sstevel@tonic-gate 	    (t_uscalar_t)option_len);
16440Sstevel@tonic-gate done1:
16450Sstevel@tonic-gate 	if (optval != buffer)
16460Sstevel@tonic-gate 		kmem_free(optval, (size_t)option_len);
16470Sstevel@tonic-gate done2:
16480Sstevel@tonic-gate 	releasef(sock);
16490Sstevel@tonic-gate 	if (error)
16500Sstevel@tonic-gate 		return (set_errno(error));
16510Sstevel@tonic-gate 	return (0);
16520Sstevel@tonic-gate }
16530Sstevel@tonic-gate 
16540Sstevel@tonic-gate /*
16550Sstevel@tonic-gate  * Add config info when devpath is non-NULL; delete info when devpath is NULL.
16560Sstevel@tonic-gate  * devpath is a user address.
16570Sstevel@tonic-gate  */
16580Sstevel@tonic-gate int
16590Sstevel@tonic-gate sockconfig(int domain, int type, int protocol, char *devpath)
16600Sstevel@tonic-gate {
16610Sstevel@tonic-gate 	char *kdevpath;		/* Copied in devpath string */
16620Sstevel@tonic-gate 	size_t kdevpathlen;
16630Sstevel@tonic-gate 	int error = 0;
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate 	dprint(1, ("sockconfig(%d, %d, %d, %p)\n",
16660Sstevel@tonic-gate 		domain, type, protocol, devpath));
16670Sstevel@tonic-gate 
16680Sstevel@tonic-gate 	if (secpolicy_net_config(CRED(), B_FALSE) != 0)
16690Sstevel@tonic-gate 		return (set_errno(EPERM));
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	if (devpath == NULL) {
16720Sstevel@tonic-gate 		/* Deleting an entry */
16730Sstevel@tonic-gate 		kdevpath = NULL;
16740Sstevel@tonic-gate 		kdevpathlen = 0;
16750Sstevel@tonic-gate 	} else {
16760Sstevel@tonic-gate 		/*
16770Sstevel@tonic-gate 		 * Adding an entry.
16780Sstevel@tonic-gate 		 * Copyin the devpath.
16790Sstevel@tonic-gate 		 * This also makes it possible to check for too long pathnames.
16800Sstevel@tonic-gate 		 * Compress the space needed for the devpath before passing it
16810Sstevel@tonic-gate 		 * to soconfig - soconfig will store the string until
16820Sstevel@tonic-gate 		 * the configuration is removed.
16830Sstevel@tonic-gate 		 */
16840Sstevel@tonic-gate 		char *buf;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
16870Sstevel@tonic-gate 		if ((error = copyinstr(devpath, buf, MAXPATHLEN,
16880Sstevel@tonic-gate 		    &kdevpathlen)) != 0) {
16890Sstevel@tonic-gate 			kmem_free(buf, MAXPATHLEN);
16900Sstevel@tonic-gate 			goto done;
16910Sstevel@tonic-gate 		}
16920Sstevel@tonic-gate 
16930Sstevel@tonic-gate 		kdevpath = kmem_alloc(kdevpathlen, KM_SLEEP);
16940Sstevel@tonic-gate 		bcopy(buf, kdevpath, kdevpathlen);
16950Sstevel@tonic-gate 		kdevpath[kdevpathlen - 1] = '\0';
16960Sstevel@tonic-gate 
16970Sstevel@tonic-gate 		kmem_free(buf, MAXPATHLEN);
16980Sstevel@tonic-gate 	}
16990Sstevel@tonic-gate 	error = soconfig(domain, type, protocol, kdevpath, (int)kdevpathlen);
17000Sstevel@tonic-gate done:
17010Sstevel@tonic-gate 	if (error) {
17020Sstevel@tonic-gate 		eprintline(error);
17030Sstevel@tonic-gate 		return (set_errno(error));
17040Sstevel@tonic-gate 	}
17050Sstevel@tonic-gate 	return (0);
17060Sstevel@tonic-gate }
17070Sstevel@tonic-gate 
17080Sstevel@tonic-gate 
17090Sstevel@tonic-gate /*
17100Sstevel@tonic-gate  * Sendfile is implemented through two schemes, direct I/O or by
17110Sstevel@tonic-gate  * caching in the filesystem page cache. We cache the input file by
17120Sstevel@tonic-gate  * default and use direct I/O only if sendfile_max_size is set
17130Sstevel@tonic-gate  * appropriately as explained below. Note that this logic is consistent
17140Sstevel@tonic-gate  * with other filesystems where caching is turned on by default
17150Sstevel@tonic-gate  * unless explicitly turned off by using the DIRECTIO ioctl.
17160Sstevel@tonic-gate  *
17170Sstevel@tonic-gate  * We choose a slightly different scheme here. One can turn off
17180Sstevel@tonic-gate  * caching by setting sendfile_max_size to 0. One can also enable
17190Sstevel@tonic-gate  * caching of files <= sendfile_max_size by setting sendfile_max_size
17200Sstevel@tonic-gate  * to an appropriate value. By default sendfile_max_size is set to the
17210Sstevel@tonic-gate  * maximum value so that all files are cached. In future, we may provide
17220Sstevel@tonic-gate  * better interfaces for caching the file.
17230Sstevel@tonic-gate  *
17240Sstevel@tonic-gate  * Sendfile through Direct I/O (Zero copy)
17250Sstevel@tonic-gate  * --------------------------------------
17260Sstevel@tonic-gate  *
17270Sstevel@tonic-gate  * As disks are normally slower than the network, we can't have a
17280Sstevel@tonic-gate  * single thread that reads the disk and writes to the network. We
17290Sstevel@tonic-gate  * need to have parallelism. This is done by having the sendfile
17300Sstevel@tonic-gate  * thread create another thread that reads from the filesystem
17310Sstevel@tonic-gate  * and queues it for network processing. In this scheme, the data
17320Sstevel@tonic-gate  * is never copied anywhere i.e it is zero copy unlike the other
17330Sstevel@tonic-gate  * scheme.
17340Sstevel@tonic-gate  *
17350Sstevel@tonic-gate  * We have a sendfile queue (snfq) where each sendfile
17360Sstevel@tonic-gate  * request (snf_req_t) is queued for processing by a thread. Number
17370Sstevel@tonic-gate  * of threads is dynamically allocated and they exit if they are idling
17380Sstevel@tonic-gate  * beyond a specified amount of time. When each request (snf_req_t) is
17390Sstevel@tonic-gate  * processed by a thread, it produces a number of mblk_t structures to
17400Sstevel@tonic-gate  * be consumed by the sendfile thread. snf_deque and snf_enque are
17410Sstevel@tonic-gate  * used for consuming and producing mblks. Size of the filesystem
17420Sstevel@tonic-gate  * read is determined by the tuneable (sendfile_read_size). A single
17430Sstevel@tonic-gate  * mblk holds sendfile_read_size worth of data (except the last
17440Sstevel@tonic-gate  * read of the file) which is sent down as a whole to the network.
17450Sstevel@tonic-gate  * sendfile_read_size is set to 1 MB as this seems to be the optimal
17460Sstevel@tonic-gate  * value for the UFS filesystem backed by a striped storage array.
17470Sstevel@tonic-gate  *
17480Sstevel@tonic-gate  * Synchronisation between read (producer) and write (consumer) threads.
17490Sstevel@tonic-gate  * --------------------------------------------------------------------
17500Sstevel@tonic-gate  *
17510Sstevel@tonic-gate  * sr_lock protects sr_ib_head and sr_ib_tail. The lock is held while
17520Sstevel@tonic-gate  * adding and deleting items in this list. Error can happen anytime
17530Sstevel@tonic-gate  * during read or write. There could be unprocessed mblks in the
17540Sstevel@tonic-gate  * sr_ib_XXX list when a read or write error occurs. Whenever error
17550Sstevel@tonic-gate  * is encountered, we need two things to happen :
17560Sstevel@tonic-gate  *
17570Sstevel@tonic-gate  * a) One of the threads need to clean the mblks.
17580Sstevel@tonic-gate  * b) When one thread encounters an error, the other should stop.
17590Sstevel@tonic-gate  *
17600Sstevel@tonic-gate  * For (a), we don't want to penalise the reader thread as it could do
17610Sstevel@tonic-gate  * some useful work processing other requests. For (b), the error can
17620Sstevel@tonic-gate  * be detected by examining sr_read_error or sr_write_error.
17630Sstevel@tonic-gate  * sr_lock protects sr_read_error and sr_write_error. If both reader and
17640Sstevel@tonic-gate  * writer encounters error, we need to report the write error back to
17650Sstevel@tonic-gate  * the application as that's what would have happened if the operations
17660Sstevel@tonic-gate  * were done sequentially. With this in mind, following should work :
17670Sstevel@tonic-gate  *
17680Sstevel@tonic-gate  * 	- Check for errors before read or write.
17690Sstevel@tonic-gate  *	- If the reader encounters error, set the error in sr_read_error.
17700Sstevel@tonic-gate  *	  Check sr_write_error, if it is set, send cv_signal as it is
17710Sstevel@tonic-gate  *	  waiting for reader to complete. If it is not set, the writer
17720Sstevel@tonic-gate  *	  is either running sinking data to the network or blocked
17730Sstevel@tonic-gate  *        because of flow control. For handling the latter case, we
17740Sstevel@tonic-gate  *	  always send a signal. In any case, it will examine sr_read_error
17750Sstevel@tonic-gate  *	  and return. sr_read_error is marked with SR_READ_DONE to tell
17760Sstevel@tonic-gate  *	  the writer that the reader is done in all the cases.
17770Sstevel@tonic-gate  *	- If the writer encounters error, set the error in sr_write_error.
17780Sstevel@tonic-gate  *	  The reader thread is either blocked because of flow control or
17790Sstevel@tonic-gate  *	  running reading data from the disk. For the former, we need to
17800Sstevel@tonic-gate  *	  wakeup the thread. Again to keep it simple, we always wake up
17810Sstevel@tonic-gate  *	  the reader thread. Then, wait for the read thread to complete
17820Sstevel@tonic-gate  *	  if it is not done yet. Cleanup and return.
17830Sstevel@tonic-gate  *
17840Sstevel@tonic-gate  * High and low water marks for the read thread.
17850Sstevel@tonic-gate  * --------------------------------------------
17860Sstevel@tonic-gate  *
17870Sstevel@tonic-gate  * If sendfile() is used to send data over a slow network, we need to
17880Sstevel@tonic-gate  * make sure that the read thread does not produce data at a faster
17890Sstevel@tonic-gate  * rate than the network. This can happen if the disk is faster than
17900Sstevel@tonic-gate  * the network. In such a case, we don't want to build a very large queue.
17910Sstevel@tonic-gate  * But we would still like to get all of the network throughput possible.
17920Sstevel@tonic-gate  * This implies that network should never block waiting for data.
17930Sstevel@tonic-gate  * As there are lot of disk throughput/network throughput combinations
17940Sstevel@tonic-gate  * possible, it is difficult to come up with an accurate number.
17950Sstevel@tonic-gate  * A typical 10K RPM disk has a max seek latency 17ms and rotational
17960Sstevel@tonic-gate  * latency of 3ms for reading a disk block. Thus, the total latency to
17970Sstevel@tonic-gate  * initiate a new read, transfer data from the disk and queue for
17980Sstevel@tonic-gate  * transmission would take about a max of 25ms. Todays max transfer rate
17990Sstevel@tonic-gate  * for network is 100MB/sec. If the thread is blocked because of flow
18000Sstevel@tonic-gate  * control, it would take 25ms to get new data ready for transmission.
18010Sstevel@tonic-gate  * We have to make sure that network is not idling, while we are initiating
18020Sstevel@tonic-gate  * new transfers. So, at 100MB/sec, to keep network busy we would need
18030Sstevel@tonic-gate  * 2.5MB of data. Roundig off, we keep the low water mark to be 3MB of data.
18040Sstevel@tonic-gate  * We need to pick a high water mark so that the woken up thread would
18050Sstevel@tonic-gate  * do considerable work before blocking again to prevent thrashing. Currently,
18060Sstevel@tonic-gate  * we pick this to be 10 times that of the low water mark.
18070Sstevel@tonic-gate  *
18080Sstevel@tonic-gate  * Sendfile with segmap caching (One copy from page cache to mblks).
18090Sstevel@tonic-gate  * ----------------------------------------------------------------
18100Sstevel@tonic-gate  *
18110Sstevel@tonic-gate  * We use the segmap cache for caching the file, if the size of file
18120Sstevel@tonic-gate  * is <= sendfile_max_size. In this case we don't use threads as VM
18130Sstevel@tonic-gate  * is reasonably fast enough to keep up with the network. If the underlying
18140Sstevel@tonic-gate  * transport allows, we call segmap_getmapflt() to map MAXBSIZE (8K) worth
18150Sstevel@tonic-gate  * of data into segmap space, and use the virtual address from segmap
18160Sstevel@tonic-gate  * directly through desballoc() to avoid copy. Once the transport is done
18170Sstevel@tonic-gate  * with the data, the mapping will be released through segmap_release()
18180Sstevel@tonic-gate  * called by the call-back routine.
18190Sstevel@tonic-gate  *
18200Sstevel@tonic-gate  * If zero-copy is not allowed by the transport, we simply call VOP_READ()
18210Sstevel@tonic-gate  * to copy the data from the filesystem into our temporary network buffer.
18220Sstevel@tonic-gate  *
18230Sstevel@tonic-gate  * To disable caching, set sendfile_max_size to 0.
18240Sstevel@tonic-gate  */
18250Sstevel@tonic-gate 
18260Sstevel@tonic-gate uint_t sendfile_read_size = 1024 * 1024;
18270Sstevel@tonic-gate #define	SENDFILE_REQ_LOWAT	3 * 1024 * 1024
18280Sstevel@tonic-gate uint_t sendfile_req_lowat = SENDFILE_REQ_LOWAT;
18290Sstevel@tonic-gate uint_t sendfile_req_hiwat = 10 * SENDFILE_REQ_LOWAT;
18300Sstevel@tonic-gate struct sendfile_stats sf_stats;
18310Sstevel@tonic-gate struct sendfile_queue *snfq;
18320Sstevel@tonic-gate clock_t snfq_timeout;
18330Sstevel@tonic-gate off64_t sendfile_max_size;
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate static void snf_enque(snf_req_t *, mblk_t *);
18360Sstevel@tonic-gate static mblk_t *snf_deque(snf_req_t *);
18370Sstevel@tonic-gate 
18380Sstevel@tonic-gate void
18390Sstevel@tonic-gate sendfile_init(void)
18400Sstevel@tonic-gate {
18410Sstevel@tonic-gate 	snfq = kmem_zalloc(sizeof (struct sendfile_queue), KM_SLEEP);
18420Sstevel@tonic-gate 
18430Sstevel@tonic-gate 	mutex_init(&snfq->snfq_lock, NULL, MUTEX_DEFAULT, NULL);
18440Sstevel@tonic-gate 	cv_init(&snfq->snfq_cv, NULL, CV_DEFAULT, NULL);
18450Sstevel@tonic-gate 	snfq->snfq_max_threads = max_ncpus;
18460Sstevel@tonic-gate 	snfq_timeout = SNFQ_TIMEOUT;
18470Sstevel@tonic-gate 	/* Cache all files by default. */
18480Sstevel@tonic-gate 	sendfile_max_size = MAXOFFSET_T;
18490Sstevel@tonic-gate }
18500Sstevel@tonic-gate 
18510Sstevel@tonic-gate /*
18520Sstevel@tonic-gate  * Queues a mblk_t for network processing.
18530Sstevel@tonic-gate  */
18540Sstevel@tonic-gate static void
18550Sstevel@tonic-gate snf_enque(snf_req_t *sr, mblk_t *mp)
18560Sstevel@tonic-gate {
18570Sstevel@tonic-gate 	mp->b_next = NULL;
18580Sstevel@tonic-gate 	mutex_enter(&sr->sr_lock);
18590Sstevel@tonic-gate 	if (sr->sr_mp_head == NULL) {
18600Sstevel@tonic-gate 		sr->sr_mp_head = sr->sr_mp_tail = mp;
18610Sstevel@tonic-gate 		cv_signal(&sr->sr_cv);
18620Sstevel@tonic-gate 	} else {
18630Sstevel@tonic-gate 		sr->sr_mp_tail->b_next = mp;
18640Sstevel@tonic-gate 		sr->sr_mp_tail = mp;
18650Sstevel@tonic-gate 	}
18660Sstevel@tonic-gate 	sr->sr_qlen += MBLKL(mp);
18670Sstevel@tonic-gate 	while ((sr->sr_qlen > sr->sr_hiwat) &&
18680Sstevel@tonic-gate 	    (sr->sr_write_error == 0)) {
18690Sstevel@tonic-gate 		sf_stats.ss_full_waits++;
18700Sstevel@tonic-gate 		cv_wait(&sr->sr_cv, &sr->sr_lock);
18710Sstevel@tonic-gate 	}
18720Sstevel@tonic-gate 	mutex_exit(&sr->sr_lock);
18730Sstevel@tonic-gate }
18740Sstevel@tonic-gate 
18750Sstevel@tonic-gate /*
18760Sstevel@tonic-gate  * De-queues a mblk_t for network processing.
18770Sstevel@tonic-gate  */
18780Sstevel@tonic-gate static mblk_t *
18790Sstevel@tonic-gate snf_deque(snf_req_t *sr)
18800Sstevel@tonic-gate {
18810Sstevel@tonic-gate 	mblk_t *mp;
18820Sstevel@tonic-gate 
18830Sstevel@tonic-gate 	mutex_enter(&sr->sr_lock);
18840Sstevel@tonic-gate 	/*
18850Sstevel@tonic-gate 	 * If we have encountered an error on read or read is
18860Sstevel@tonic-gate 	 * completed and no more mblks, return NULL.
18870Sstevel@tonic-gate 	 * We need to check for NULL sr_mp_head also as
18880Sstevel@tonic-gate 	 * the reads could have completed and there is
18890Sstevel@tonic-gate 	 * nothing more to come.
18900Sstevel@tonic-gate 	 */
18910Sstevel@tonic-gate 	if (((sr->sr_read_error & ~SR_READ_DONE) != 0) ||
18920Sstevel@tonic-gate 	    ((sr->sr_read_error & SR_READ_DONE) &&
18930Sstevel@tonic-gate 	    sr->sr_mp_head == NULL)) {
18940Sstevel@tonic-gate 		mutex_exit(&sr->sr_lock);
18950Sstevel@tonic-gate 		return (NULL);
18960Sstevel@tonic-gate 	}
18970Sstevel@tonic-gate 	/*
18980Sstevel@tonic-gate 	 * To start with neither SR_READ_DONE is marked nor
18990Sstevel@tonic-gate 	 * the error is set. When we wake up from cv_wait,
19000Sstevel@tonic-gate 	 * following are the possibilities :
19010Sstevel@tonic-gate 	 *
19020Sstevel@tonic-gate 	 *	a) sr_read_error is zero and mblks are queued.
19030Sstevel@tonic-gate 	 *	b) sr_read_error is set to SR_READ_DONE
19040Sstevel@tonic-gate 	 *	   and mblks are queued.
19050Sstevel@tonic-gate 	 *	c) sr_read_error is set to SR_READ_DONE
19060Sstevel@tonic-gate 	 *	   and no mblks.
19070Sstevel@tonic-gate 	 *	d) sr_read_error is set to some error other
19080Sstevel@tonic-gate 	 *	   than SR_READ_DONE.
19090Sstevel@tonic-gate 	 */
19100Sstevel@tonic-gate 
19110Sstevel@tonic-gate 	while ((sr->sr_read_error == 0) && (sr->sr_mp_head == NULL)) {
19120Sstevel@tonic-gate 		sf_stats.ss_empty_waits++;
19130Sstevel@tonic-gate 		cv_wait(&sr->sr_cv, &sr->sr_lock);
19140Sstevel@tonic-gate 	}
19150Sstevel@tonic-gate 	/* Handle (a) and (b) first  - the normal case. */
19160Sstevel@tonic-gate 	if (((sr->sr_read_error & ~SR_READ_DONE) == 0) &&
19170Sstevel@tonic-gate 	    (sr->sr_mp_head != NULL)) {
19180Sstevel@tonic-gate 		mp = sr->sr_mp_head;
19190Sstevel@tonic-gate 		sr->sr_mp_head = mp->b_next;
19200Sstevel@tonic-gate 		sr->sr_qlen -= MBLKL(mp);
19210Sstevel@tonic-gate 		if (sr->sr_qlen < sr->sr_lowat)
19220Sstevel@tonic-gate 			cv_signal(&sr->sr_cv);
19230Sstevel@tonic-gate 		mutex_exit(&sr->sr_lock);
19240Sstevel@tonic-gate 		mp->b_next = NULL;
19250Sstevel@tonic-gate 		return (mp);
19260Sstevel@tonic-gate 	}
19270Sstevel@tonic-gate 	/* Handle (c) and (d). */
19280Sstevel@tonic-gate 	mutex_exit(&sr->sr_lock);
19290Sstevel@tonic-gate 	return (NULL);
19300Sstevel@tonic-gate }
19310Sstevel@tonic-gate 
19320Sstevel@tonic-gate /*
19330Sstevel@tonic-gate  * Reads data from the filesystem and queues it for network processing.
19340Sstevel@tonic-gate  */
19350Sstevel@tonic-gate void
19360Sstevel@tonic-gate snf_async_read(snf_req_t *sr)
19370Sstevel@tonic-gate {
19380Sstevel@tonic-gate 	size_t iosize;
19390Sstevel@tonic-gate 	u_offset_t fileoff;
19400Sstevel@tonic-gate 	u_offset_t size;
19410Sstevel@tonic-gate 	int ret_size;
19420Sstevel@tonic-gate 	int error;
19430Sstevel@tonic-gate 	file_t *fp;
19440Sstevel@tonic-gate 	mblk_t *mp;
19450Sstevel@tonic-gate 
19460Sstevel@tonic-gate 	fp = sr->sr_fp;
19470Sstevel@tonic-gate 	size = sr->sr_file_size;
19480Sstevel@tonic-gate 	fileoff = sr->sr_file_off;
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate 	/*
19510Sstevel@tonic-gate 	 * Ignore the error for filesystems that doesn't support DIRECTIO.
19520Sstevel@tonic-gate 	 */
19530Sstevel@tonic-gate 	(void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_ON, 0,
19540Sstevel@tonic-gate 	    kcred, NULL);
19550Sstevel@tonic-gate 
19560Sstevel@tonic-gate 	while ((size != 0) && (sr->sr_write_error == 0)) {
19570Sstevel@tonic-gate 
19580Sstevel@tonic-gate 		iosize = (int)MIN(sr->sr_maxpsz, size);
19590Sstevel@tonic-gate 
19600Sstevel@tonic-gate 		if ((mp = allocb(iosize, BPRI_MED)) == NULL) {
19610Sstevel@tonic-gate 			error = EAGAIN;
19620Sstevel@tonic-gate 			break;
19630Sstevel@tonic-gate 		}
19640Sstevel@tonic-gate 		ret_size = soreadfile(fp, mp->b_rptr, fileoff, &error, iosize);
19650Sstevel@tonic-gate 
19660Sstevel@tonic-gate 		/* Error or Reached EOF ? */
19670Sstevel@tonic-gate 		if ((error != 0) || (ret_size == 0)) {
19680Sstevel@tonic-gate 			freeb(mp);
19690Sstevel@tonic-gate 			break;
19700Sstevel@tonic-gate 		}
19710Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + ret_size;
19720Sstevel@tonic-gate 
19730Sstevel@tonic-gate 		snf_enque(sr, mp);
19740Sstevel@tonic-gate 		size -= ret_size;
19750Sstevel@tonic-gate 		fileoff += ret_size;
19760Sstevel@tonic-gate 	}
19770Sstevel@tonic-gate 	(void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_OFF, 0,
19780Sstevel@tonic-gate 	    kcred, NULL);
19790Sstevel@tonic-gate 	mutex_enter(&sr->sr_lock);
19800Sstevel@tonic-gate 	sr->sr_read_error = error;
19810Sstevel@tonic-gate 	sr->sr_read_error |= SR_READ_DONE;
19820Sstevel@tonic-gate 	cv_signal(&sr->sr_cv);
19830Sstevel@tonic-gate 	mutex_exit(&sr->sr_lock);
19840Sstevel@tonic-gate }
19850Sstevel@tonic-gate 
19860Sstevel@tonic-gate void
19870Sstevel@tonic-gate snf_async_thread(void)
19880Sstevel@tonic-gate {
19890Sstevel@tonic-gate 	snf_req_t *sr;
19900Sstevel@tonic-gate 	callb_cpr_t cprinfo;
19910Sstevel@tonic-gate 	clock_t time_left = 1;
19920Sstevel@tonic-gate 	clock_t now;
19930Sstevel@tonic-gate 
19940Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &snfq->snfq_lock, callb_generic_cpr, "snfq");
19950Sstevel@tonic-gate 
19960Sstevel@tonic-gate 	mutex_enter(&snfq->snfq_lock);
19970Sstevel@tonic-gate 	for (;;) {
19980Sstevel@tonic-gate 		/*
19990Sstevel@tonic-gate 		 * If we didn't find a entry, then block until woken up
20000Sstevel@tonic-gate 		 * again and then look through the queues again.
20010Sstevel@tonic-gate 		 */
20020Sstevel@tonic-gate 		while ((sr = snfq->snfq_req_head) == NULL) {
20030Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
20040Sstevel@tonic-gate 			if (time_left <= 0) {
20050Sstevel@tonic-gate 				snfq->snfq_svc_threads--;
20060Sstevel@tonic-gate 				CALLB_CPR_EXIT(&cprinfo);
20070Sstevel@tonic-gate 				thread_exit();
20080Sstevel@tonic-gate 				/* NOTREACHED */
20090Sstevel@tonic-gate 			}
20100Sstevel@tonic-gate 			snfq->snfq_idle_cnt++;
20110Sstevel@tonic-gate 
20120Sstevel@tonic-gate 			time_to_wait(&now, snfq_timeout);
20130Sstevel@tonic-gate 			time_left = cv_timedwait(&snfq->snfq_cv,
20140Sstevel@tonic-gate 			    &snfq->snfq_lock, now);
20150Sstevel@tonic-gate 			snfq->snfq_idle_cnt--;
20160Sstevel@tonic-gate 
20170Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &snfq->snfq_lock);
20180Sstevel@tonic-gate 		}
20190Sstevel@tonic-gate 		snfq->snfq_req_head = sr->sr_next;
20200Sstevel@tonic-gate 		snfq->snfq_req_cnt--;
20210Sstevel@tonic-gate 		mutex_exit(&snfq->snfq_lock);
20220Sstevel@tonic-gate 		snf_async_read(sr);
20230Sstevel@tonic-gate 		mutex_enter(&snfq->snfq_lock);
20240Sstevel@tonic-gate 	}
20250Sstevel@tonic-gate }
20260Sstevel@tonic-gate 
20270Sstevel@tonic-gate 
20280Sstevel@tonic-gate snf_req_t *
20290Sstevel@tonic-gate create_thread(int operation, struct vnode *vp, file_t *fp,
20300Sstevel@tonic-gate     u_offset_t fileoff, u_offset_t size)
20310Sstevel@tonic-gate {
20320Sstevel@tonic-gate 	snf_req_t *sr;
20330Sstevel@tonic-gate 	stdata_t *stp;
20340Sstevel@tonic-gate 
20350Sstevel@tonic-gate 	sr = (snf_req_t *)kmem_zalloc(sizeof (snf_req_t), KM_SLEEP);
20360Sstevel@tonic-gate 
20370Sstevel@tonic-gate 	sr->sr_vp = vp;
20380Sstevel@tonic-gate 	sr->sr_fp = fp;
20390Sstevel@tonic-gate 	stp = vp->v_stream;
20400Sstevel@tonic-gate 
20410Sstevel@tonic-gate 	/*
20420Sstevel@tonic-gate 	 * store sd_qn_maxpsz into sr_maxpsz while we have stream head.
20430Sstevel@tonic-gate 	 * stream might be closed before thread returns from snf_async_read.
20440Sstevel@tonic-gate 	 */
20450Sstevel@tonic-gate 	if (stp->sd_qn_maxpsz > 0) {
20460Sstevel@tonic-gate 		sr->sr_maxpsz = MIN(MAXBSIZE, stp->sd_qn_maxpsz);
20470Sstevel@tonic-gate 	} else {
20480Sstevel@tonic-gate 		sr->sr_maxpsz = MAXBSIZE;
20490Sstevel@tonic-gate 	}
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate 	sr->sr_operation = operation;
20520Sstevel@tonic-gate 	sr->sr_file_off = fileoff;
20530Sstevel@tonic-gate 	sr->sr_file_size = size;
20540Sstevel@tonic-gate 	sr->sr_hiwat = sendfile_req_hiwat;
20550Sstevel@tonic-gate 	sr->sr_lowat = sendfile_req_lowat;
20560Sstevel@tonic-gate 	mutex_init(&sr->sr_lock, NULL, MUTEX_DEFAULT, NULL);
20570Sstevel@tonic-gate 	cv_init(&sr->sr_cv, NULL, CV_DEFAULT, NULL);
20580Sstevel@tonic-gate 	/*
20590Sstevel@tonic-gate 	 * See whether we need another thread for servicing this
20600Sstevel@tonic-gate 	 * request. If there are already enough requests queued
20610Sstevel@tonic-gate 	 * for the threads, create one if not exceeding
20620Sstevel@tonic-gate 	 * snfq_max_threads.
20630Sstevel@tonic-gate 	 */
20640Sstevel@tonic-gate 	mutex_enter(&snfq->snfq_lock);
20650Sstevel@tonic-gate 	if (snfq->snfq_req_cnt >= snfq->snfq_idle_cnt &&
20660Sstevel@tonic-gate 	    snfq->snfq_svc_threads < snfq->snfq_max_threads) {
20670Sstevel@tonic-gate 		(void) thread_create(NULL, 0, &snf_async_thread, 0, 0, &p0,
20680Sstevel@tonic-gate 		    TS_RUN, minclsyspri);
20690Sstevel@tonic-gate 		snfq->snfq_svc_threads++;
20700Sstevel@tonic-gate 	}
20710Sstevel@tonic-gate 	if (snfq->snfq_req_head == NULL) {
20720Sstevel@tonic-gate 		snfq->snfq_req_head = snfq->snfq_req_tail = sr;
20730Sstevel@tonic-gate 		cv_signal(&snfq->snfq_cv);
20740Sstevel@tonic-gate 	} else {
20750Sstevel@tonic-gate 		snfq->snfq_req_tail->sr_next = sr;
20760Sstevel@tonic-gate 		snfq->snfq_req_tail = sr;
20770Sstevel@tonic-gate 	}
20780Sstevel@tonic-gate 	snfq->snfq_req_cnt++;
20790Sstevel@tonic-gate 	mutex_exit(&snfq->snfq_lock);
20800Sstevel@tonic-gate 	return (sr);
20810Sstevel@tonic-gate }
20820Sstevel@tonic-gate 
20830Sstevel@tonic-gate int
20840Sstevel@tonic-gate snf_direct_io(file_t *fp, file_t *rfp, u_offset_t fileoff, u_offset_t size,
20850Sstevel@tonic-gate     ssize_t *count)
20860Sstevel@tonic-gate {
20870Sstevel@tonic-gate 	snf_req_t *sr;
20880Sstevel@tonic-gate 	mblk_t *mp;
20890Sstevel@tonic-gate 	int iosize;
20900Sstevel@tonic-gate 	int error = 0;
20910Sstevel@tonic-gate 	short fflag;
20920Sstevel@tonic-gate 	struct vnode *vp;
20930Sstevel@tonic-gate 	int ksize;
20940Sstevel@tonic-gate 
20950Sstevel@tonic-gate 	ksize = 0;
20960Sstevel@tonic-gate 	*count = 0;
20970Sstevel@tonic-gate 
20980Sstevel@tonic-gate 	vp = fp->f_vnode;
20990Sstevel@tonic-gate 	fflag = fp->f_flag;
21000Sstevel@tonic-gate 	if ((sr = create_thread(READ_OP, vp, rfp, fileoff, size)) == NULL)
21010Sstevel@tonic-gate 		return (EAGAIN);
21020Sstevel@tonic-gate 
21030Sstevel@tonic-gate 	/*
21040Sstevel@tonic-gate 	 * We check for read error in snf_deque. It has to check
21050Sstevel@tonic-gate 	 * for successful READ_DONE and return NULL, and we might
21060Sstevel@tonic-gate 	 * as well make an additional check there.
21070Sstevel@tonic-gate 	 */
21080Sstevel@tonic-gate 	while ((mp = snf_deque(sr)) != NULL) {
21090Sstevel@tonic-gate 
21100Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING)) {
21110Sstevel@tonic-gate 			freeb(mp);
21120Sstevel@tonic-gate 			error = EINTR;
21130Sstevel@tonic-gate 			break;
21140Sstevel@tonic-gate 		}
21150Sstevel@tonic-gate 		iosize = MBLKL(mp);
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 		if ((error = kstrwritemp(vp, mp, fflag)) != 0) {
21180Sstevel@tonic-gate 			freeb(mp);
21190Sstevel@tonic-gate 			break;
21200Sstevel@tonic-gate 		}
21210Sstevel@tonic-gate 		ksize += iosize;
21220Sstevel@tonic-gate 	}
21230Sstevel@tonic-gate 	*count = ksize;
21240Sstevel@tonic-gate 
21250Sstevel@tonic-gate 	mutex_enter(&sr->sr_lock);
21260Sstevel@tonic-gate 	sr->sr_write_error = error;
21270Sstevel@tonic-gate 	/* Look at the big comments on why we cv_signal here. */
21280Sstevel@tonic-gate 	cv_signal(&sr->sr_cv);
21290Sstevel@tonic-gate 
21300Sstevel@tonic-gate 	/* Wait for the reader to complete always. */
21310Sstevel@tonic-gate 	while (!(sr->sr_read_error & SR_READ_DONE)) {
21320Sstevel@tonic-gate 		cv_wait(&sr->sr_cv, &sr->sr_lock);
21330Sstevel@tonic-gate 	}
21340Sstevel@tonic-gate 	/* If there is no write error, check for read error. */
21350Sstevel@tonic-gate 	if (error == 0)
21360Sstevel@tonic-gate 		error = (sr->sr_read_error & ~SR_READ_DONE);
21370Sstevel@tonic-gate 
21380Sstevel@tonic-gate 	if (error != 0) {
21390Sstevel@tonic-gate 		mblk_t *next_mp;
21400Sstevel@tonic-gate 
21410Sstevel@tonic-gate 		mp = sr->sr_mp_head;
21420Sstevel@tonic-gate 		while (mp != NULL) {
21430Sstevel@tonic-gate 			next_mp = mp->b_next;
21440Sstevel@tonic-gate 			mp->b_next = NULL;
21450Sstevel@tonic-gate 			freeb(mp);
21460Sstevel@tonic-gate 			mp = next_mp;
21470Sstevel@tonic-gate 		}
21480Sstevel@tonic-gate 	}
21490Sstevel@tonic-gate 	mutex_exit(&sr->sr_lock);
21500Sstevel@tonic-gate 	kmem_free(sr, sizeof (snf_req_t));
21510Sstevel@tonic-gate 	return (error);
21520Sstevel@tonic-gate }
21530Sstevel@tonic-gate 
21540Sstevel@tonic-gate typedef struct {
21550Sstevel@tonic-gate 	frtn_t		snfi_frtn;
21560Sstevel@tonic-gate 	caddr_t		snfi_base;
21570Sstevel@tonic-gate 	uint_t		snfi_mapoff;
21580Sstevel@tonic-gate 	size_t		snfi_len;
21590Sstevel@tonic-gate 	vnode_t		*snfi_vp;
21600Sstevel@tonic-gate } snf_smap_desbinfo;
21610Sstevel@tonic-gate 
21620Sstevel@tonic-gate /*
21630Sstevel@tonic-gate  * The callback function when the last ref of the mblk is dropped,
21640Sstevel@tonic-gate  * normally occurs when TCP receives the ack. But it can be the driver
21650Sstevel@tonic-gate  * too due to lazy reclaim.
21660Sstevel@tonic-gate  */
21670Sstevel@tonic-gate void
21680Sstevel@tonic-gate snf_smap_desbfree(snf_smap_desbinfo *snfi)
21690Sstevel@tonic-gate {
21700Sstevel@tonic-gate 	if (!segmap_kpm) {
21710Sstevel@tonic-gate 		/*
21720Sstevel@tonic-gate 		 * We don't need to call segmap_fault(F_SOFTUNLOCK) for
21730Sstevel@tonic-gate 		 * segmap_kpm as long as the latter never falls back to
21740Sstevel@tonic-gate 		 * "use_segmap_range". (See segmap_getmapflt().)
21750Sstevel@tonic-gate 		 *
21760Sstevel@tonic-gate 		 * Using S_OTHER saves an redundant hat_setref() in
21770Sstevel@tonic-gate 		 * segmap_unlock()
21780Sstevel@tonic-gate 		 */
21790Sstevel@tonic-gate 		(void) segmap_fault(kas.a_hat, segkmap,
2180*408Skrgopi 		    (caddr_t)(uintptr_t)(((uintptr_t)snfi->snfi_base +
2181*408Skrgopi 		    snfi->snfi_mapoff) & PAGEMASK), snfi->snfi_len,
2182*408Skrgopi 		    F_SOFTUNLOCK, S_OTHER);
21830Sstevel@tonic-gate 	}
21840Sstevel@tonic-gate 	(void) segmap_release(segkmap, snfi->snfi_base, SM_DONTNEED);
21850Sstevel@tonic-gate 	VN_RELE(snfi->snfi_vp);
21860Sstevel@tonic-gate 	kmem_free(snfi, sizeof (*snfi));
21870Sstevel@tonic-gate }
21880Sstevel@tonic-gate 
21890Sstevel@tonic-gate /*
21900Sstevel@tonic-gate  * Use segmap instead of bcopy to send down a chain of desballoca'ed, mblks.
21910Sstevel@tonic-gate  * Each mblk contains a segmap slot of no more than MAXBSIZE. The total
21920Sstevel@tonic-gate  * length of a chain is no more than sd_qn_maxpsz.
21930Sstevel@tonic-gate  *
21940Sstevel@tonic-gate  * At the end of the whole sendfile() operation, we wait till the data from
21950Sstevel@tonic-gate  * the last mblk is ack'ed by the transport before returning so that the
21960Sstevel@tonic-gate  * caller of sendfile() can safely modify the file content.
21970Sstevel@tonic-gate  */
21980Sstevel@tonic-gate int
21990Sstevel@tonic-gate snf_segmap(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t size,
22000Sstevel@tonic-gate     uint_t maxpsz, ssize_t *count, boolean_t nowait)
22010Sstevel@tonic-gate {
22020Sstevel@tonic-gate 	caddr_t base;
22030Sstevel@tonic-gate 	int mapoff;
22040Sstevel@tonic-gate 	vnode_t *vp;
22050Sstevel@tonic-gate 	mblk_t *mp, *mp1;
22060Sstevel@tonic-gate 	int iosize, iosize1;
22070Sstevel@tonic-gate 	int error;
22080Sstevel@tonic-gate 	short fflag;
22090Sstevel@tonic-gate 	int ksize;
22100Sstevel@tonic-gate 	snf_smap_desbinfo *snfi;
22110Sstevel@tonic-gate 	struct vattr va;
22120Sstevel@tonic-gate 	boolean_t dowait = B_FALSE;
22130Sstevel@tonic-gate 
22140Sstevel@tonic-gate 	vp = fp->f_vnode;
22150Sstevel@tonic-gate 	fflag = fp->f_flag;
22160Sstevel@tonic-gate 	ksize = 0;
22170Sstevel@tonic-gate 	for (;;) {
22180Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING)) {
22190Sstevel@tonic-gate 			error = EINTR;
22200Sstevel@tonic-gate 			break;
22210Sstevel@tonic-gate 		}
22220Sstevel@tonic-gate 		iosize = 0;
22230Sstevel@tonic-gate 		mp = NULL;
22240Sstevel@tonic-gate 		do {
22250Sstevel@tonic-gate 			mapoff = fileoff & MAXBOFFSET;
22260Sstevel@tonic-gate 			iosize1 = MAXBSIZE - mapoff;
22270Sstevel@tonic-gate 			if (iosize1 > size)
22280Sstevel@tonic-gate 				iosize1 = size;
22290Sstevel@tonic-gate 			/*
22300Sstevel@tonic-gate 			 * we don't forcefault because we'll call
22310Sstevel@tonic-gate 			 * segmap_fault(F_SOFTLOCK) next.
22320Sstevel@tonic-gate 			 *
22330Sstevel@tonic-gate 			 * S_READ will get the ref bit set (by either
22340Sstevel@tonic-gate 			 * segmap_getmapflt() or segmap_fault()) and page
22350Sstevel@tonic-gate 			 * shared locked.
22360Sstevel@tonic-gate 			 */
22370Sstevel@tonic-gate 			base = segmap_getmapflt(segkmap, fvp, fileoff, iosize1,
22380Sstevel@tonic-gate 			    segmap_kpm ? SM_FAULT : 0, S_READ);
22390Sstevel@tonic-gate 
22400Sstevel@tonic-gate 			snfi = kmem_alloc(sizeof (*snfi), KM_SLEEP);
22410Sstevel@tonic-gate 			snfi->snfi_len = (size_t)roundup(mapoff+iosize1,
22420Sstevel@tonic-gate 			    PAGESIZE)- (mapoff & PAGEMASK);
22430Sstevel@tonic-gate 			/*
22440Sstevel@tonic-gate 			 * We must call segmap_fault() even for segmap_kpm
22450Sstevel@tonic-gate 			 * because that's how error gets returned.
22460Sstevel@tonic-gate 			 * (segmap_getmapflt() never fails but segmap_fault()
22470Sstevel@tonic-gate 			 * does.)
22480Sstevel@tonic-gate 			 */
22490Sstevel@tonic-gate 			if (segmap_fault(kas.a_hat, segkmap,
2250*408Skrgopi 			    (caddr_t)(uintptr_t)(((uintptr_t)base + mapoff) &
2251*408Skrgopi 			    PAGEMASK), snfi->snfi_len, F_SOFTLOCK,
2252*408Skrgopi 			    S_READ) != 0) {
22530Sstevel@tonic-gate 				(void) segmap_release(segkmap, base, 0);
22540Sstevel@tonic-gate 				kmem_free(snfi, sizeof (*snfi));
22550Sstevel@tonic-gate 				freemsg(mp);
22560Sstevel@tonic-gate 				error = EIO;
22570Sstevel@tonic-gate 				goto out;
22580Sstevel@tonic-gate 			}
22590Sstevel@tonic-gate 			snfi->snfi_frtn.free_func = snf_smap_desbfree;
22600Sstevel@tonic-gate 			snfi->snfi_frtn.free_arg = (caddr_t)snfi;
22610Sstevel@tonic-gate 			snfi->snfi_base = base;
22620Sstevel@tonic-gate 			snfi->snfi_mapoff = mapoff;
22630Sstevel@tonic-gate 			mp1 = desballoca((uchar_t *)base + mapoff,
22640Sstevel@tonic-gate 			    iosize1, BPRI_HI, &snfi->snfi_frtn);
22650Sstevel@tonic-gate 
22660Sstevel@tonic-gate 			if (mp1 == NULL) {
22670Sstevel@tonic-gate 				(void) segmap_fault(kas.a_hat, segkmap,
2268*408Skrgopi 				    (caddr_t)(uintptr_t)(((uintptr_t)base +
2269*408Skrgopi 				    mapoff) & PAGEMASK), snfi->snfi_len,
22700Sstevel@tonic-gate 				    F_SOFTUNLOCK, S_OTHER);
22710Sstevel@tonic-gate 				(void) segmap_release(segkmap, base, 0);
22720Sstevel@tonic-gate 				kmem_free(snfi, sizeof (*snfi));
22730Sstevel@tonic-gate 				freemsg(mp);
22740Sstevel@tonic-gate 				error = EAGAIN;
22750Sstevel@tonic-gate 				goto out;
22760Sstevel@tonic-gate 			}
22770Sstevel@tonic-gate 			VN_HOLD(fvp);
22780Sstevel@tonic-gate 			snfi->snfi_vp = fvp;
22790Sstevel@tonic-gate 			mp1->b_wptr += iosize1;
22800Sstevel@tonic-gate 
22810Sstevel@tonic-gate 			/* Mark this dblk with the zero-copy flag */
22820Sstevel@tonic-gate 			mp1->b_datap->db_struioflag |= STRUIO_ZC;
22830Sstevel@tonic-gate 			if (mp == NULL)
22840Sstevel@tonic-gate 				mp = mp1;
22850Sstevel@tonic-gate 			else
22860Sstevel@tonic-gate 				linkb(mp, mp1);
22870Sstevel@tonic-gate 			iosize += iosize1;
22880Sstevel@tonic-gate 			fileoff += iosize1;
22890Sstevel@tonic-gate 			size -= iosize1;
22900Sstevel@tonic-gate 		} while (iosize < maxpsz && size != 0);
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate 		if (size == 0 && !nowait) {
22930Sstevel@tonic-gate 			ASSERT(!dowait);
22940Sstevel@tonic-gate 			dowait = B_TRUE;
22950Sstevel@tonic-gate 			mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY;
22960Sstevel@tonic-gate 		}
22970Sstevel@tonic-gate 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
22980Sstevel@tonic-gate 		if ((error = kstrwritemp(vp, mp, fflag)) != 0) {
22990Sstevel@tonic-gate 			*count = ksize;
23000Sstevel@tonic-gate 			freemsg(mp);
23010Sstevel@tonic-gate 			return (error);
23020Sstevel@tonic-gate 		}
23030Sstevel@tonic-gate 		ksize += iosize;
23040Sstevel@tonic-gate 		if (size == 0)
23050Sstevel@tonic-gate 			goto done;
23060Sstevel@tonic-gate 
23070Sstevel@tonic-gate 		(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
23080Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
23090Sstevel@tonic-gate 		error = VOP_GETATTR(fvp, &va, 0, kcred);
23100Sstevel@tonic-gate 		if (error)
23110Sstevel@tonic-gate 			break;
23120Sstevel@tonic-gate 		/* Read as much as possible. */
23130Sstevel@tonic-gate 		if (fileoff >= va.va_size)
23140Sstevel@tonic-gate 			break;
23150Sstevel@tonic-gate 		if (size + fileoff > va.va_size)
23160Sstevel@tonic-gate 			size = va.va_size - fileoff;
23170Sstevel@tonic-gate 	}
23180Sstevel@tonic-gate out:
23190Sstevel@tonic-gate 	VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
23200Sstevel@tonic-gate done:
23210Sstevel@tonic-gate 	*count = ksize;
23220Sstevel@tonic-gate 	if (dowait) {
23230Sstevel@tonic-gate 		stdata_t *stp;
23240Sstevel@tonic-gate 
23250Sstevel@tonic-gate 		stp = vp->v_stream;
23260Sstevel@tonic-gate 		mutex_enter(&stp->sd_lock);
23270Sstevel@tonic-gate 		while (!(stp->sd_flag & STZCNOTIFY)) {
23280Sstevel@tonic-gate 			(void) cv_wait_sig(&stp->sd_zcopy_wait,
23290Sstevel@tonic-gate 			    &stp->sd_lock);
23300Sstevel@tonic-gate 		}
23310Sstevel@tonic-gate 		stp->sd_flag &= ~STZCNOTIFY;
23320Sstevel@tonic-gate 		mutex_exit(&stp->sd_lock);
23330Sstevel@tonic-gate 	}
23340Sstevel@tonic-gate 	return (error);
23350Sstevel@tonic-gate }
23360Sstevel@tonic-gate 
23370Sstevel@tonic-gate int
23380Sstevel@tonic-gate snf_cache(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t size,
23390Sstevel@tonic-gate     uint_t maxpsz, ssize_t *count)
23400Sstevel@tonic-gate {
23410Sstevel@tonic-gate 	struct vnode *vp;
23420Sstevel@tonic-gate 	mblk_t *mp;
23430Sstevel@tonic-gate 	int iosize;
23440Sstevel@tonic-gate 	int error;
23450Sstevel@tonic-gate 	short fflag;
23460Sstevel@tonic-gate 	int ksize;
23470Sstevel@tonic-gate 	int ioflag;
23480Sstevel@tonic-gate 	struct uio auio;
23490Sstevel@tonic-gate 	struct iovec aiov;
23500Sstevel@tonic-gate 	struct vattr va;
23510Sstevel@tonic-gate 
23520Sstevel@tonic-gate 	vp = fp->f_vnode;
23530Sstevel@tonic-gate 	fflag = fp->f_flag;
23540Sstevel@tonic-gate 	ksize = 0;
23550Sstevel@tonic-gate 	auio.uio_iov = &aiov;
23560Sstevel@tonic-gate 	auio.uio_iovcnt = 1;
23570Sstevel@tonic-gate 	auio.uio_segflg = UIO_SYSSPACE;
23580Sstevel@tonic-gate 	auio.uio_llimit = MAXOFFSET_T;
23590Sstevel@tonic-gate 	auio.uio_fmode = fflag;
23600Sstevel@tonic-gate 	auio.uio_extflg = UIO_COPY_CACHED;
23610Sstevel@tonic-gate 	ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
23620Sstevel@tonic-gate 	/* If read sync is not asked for, filter sync flags */
23630Sstevel@tonic-gate 	if ((ioflag & FRSYNC) == 0)
23640Sstevel@tonic-gate 		ioflag &= ~(FSYNC|FDSYNC);
23650Sstevel@tonic-gate 	for (;;) {
23660Sstevel@tonic-gate 		if (ISSIG(curthread, JUSTLOOKING)) {
23670Sstevel@tonic-gate 			error = EINTR;
23680Sstevel@tonic-gate 			break;
23690Sstevel@tonic-gate 		}
23700Sstevel@tonic-gate 		iosize = (int)MIN(maxpsz, size);
23710Sstevel@tonic-gate 		if ((mp = allocb(iosize, BPRI_MED)) == NULL) {
23720Sstevel@tonic-gate 			error = EAGAIN;
23730Sstevel@tonic-gate 			break;
23740Sstevel@tonic-gate 		}
23750Sstevel@tonic-gate 		aiov.iov_base = (caddr_t)mp->b_rptr;
23760Sstevel@tonic-gate 		aiov.iov_len = iosize;
23770Sstevel@tonic-gate 		auio.uio_loffset = fileoff;
23780Sstevel@tonic-gate 		auio.uio_resid = iosize;
23790Sstevel@tonic-gate 
23800Sstevel@tonic-gate 		error = VOP_READ(fvp, &auio, ioflag, fp->f_cred, NULL);
23810Sstevel@tonic-gate 		iosize -= auio.uio_resid;
23820Sstevel@tonic-gate 
23830Sstevel@tonic-gate 		if (error == EINTR && iosize != 0)
23840Sstevel@tonic-gate 			error = 0;
23850Sstevel@tonic-gate 
23860Sstevel@tonic-gate 		if (error != 0 || iosize == 0) {
23870Sstevel@tonic-gate 			freeb(mp);
23880Sstevel@tonic-gate 			break;
23890Sstevel@tonic-gate 		}
23900Sstevel@tonic-gate 		mp->b_wptr = mp->b_rptr + iosize;
23910Sstevel@tonic-gate 
23920Sstevel@tonic-gate 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
23930Sstevel@tonic-gate 		if ((error = kstrwritemp(vp, mp, fflag)) != 0) {
23940Sstevel@tonic-gate 			*count = ksize;
23950Sstevel@tonic-gate 			freeb(mp);
23960Sstevel@tonic-gate 			return (error);
23970Sstevel@tonic-gate 		}
23980Sstevel@tonic-gate 		ksize += iosize;
23990Sstevel@tonic-gate 		size -= iosize;
24000Sstevel@tonic-gate 		if (size == 0)
24010Sstevel@tonic-gate 			goto done;
24020Sstevel@tonic-gate 
24030Sstevel@tonic-gate 		fileoff += iosize;
24040Sstevel@tonic-gate 		(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
24050Sstevel@tonic-gate 		va.va_mask = AT_SIZE;
24060Sstevel@tonic-gate 		error = VOP_GETATTR(fvp, &va, 0, kcred);
24070Sstevel@tonic-gate 		if (error)
24080Sstevel@tonic-gate 			break;
24090Sstevel@tonic-gate 		/* Read as much as possible. */
24100Sstevel@tonic-gate 		if (fileoff >= va.va_size)
24110Sstevel@tonic-gate 			size = 0;
24120Sstevel@tonic-gate 		else if (size + fileoff > va.va_size)
24130Sstevel@tonic-gate 			size = va.va_size - fileoff;
24140Sstevel@tonic-gate 	}
24150Sstevel@tonic-gate 	VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
24160Sstevel@tonic-gate done:
24170Sstevel@tonic-gate 	*count = ksize;
24180Sstevel@tonic-gate 	return (error);
24190Sstevel@tonic-gate }
24200Sstevel@tonic-gate 
24210Sstevel@tonic-gate #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
24220Sstevel@tonic-gate /*
24230Sstevel@tonic-gate  * Largefile support for 32 bit applications only.
24240Sstevel@tonic-gate  */
24250Sstevel@tonic-gate int
24260Sstevel@tonic-gate sosendfile64(file_t *fp, file_t *rfp, const struct ksendfilevec64 *sfv,
24270Sstevel@tonic-gate     ssize32_t *count32)
24280Sstevel@tonic-gate {
24290Sstevel@tonic-gate 	ssize32_t sfv_len;
24300Sstevel@tonic-gate 	u_offset_t sfv_off, va_size;
24310Sstevel@tonic-gate 	struct vnode *vp, *fvp, *realvp;
24320Sstevel@tonic-gate 	struct vattr va;
24330Sstevel@tonic-gate 	stdata_t *stp;
24340Sstevel@tonic-gate 	ssize_t count = 0;
24350Sstevel@tonic-gate 	int error = 0;
24360Sstevel@tonic-gate 	boolean_t dozcopy = B_FALSE;
24370Sstevel@tonic-gate 	uint_t maxpsz;
24380Sstevel@tonic-gate 
24390Sstevel@tonic-gate 	sfv_len = (ssize32_t)sfv->sfv_len;
24400Sstevel@tonic-gate 	if (sfv_len < 0) {
24410Sstevel@tonic-gate 		error = EINVAL;
24420Sstevel@tonic-gate 		goto out;
24430Sstevel@tonic-gate 	}
24440Sstevel@tonic-gate 
24450Sstevel@tonic-gate 	if (sfv_len == 0) goto out;
24460Sstevel@tonic-gate 
24470Sstevel@tonic-gate 	sfv_off = (u_offset_t)sfv->sfv_off;
24480Sstevel@tonic-gate 
24490Sstevel@tonic-gate 	/* Same checks as in pread */
24500Sstevel@tonic-gate 	if (sfv_off > MAXOFFSET_T) {
24510Sstevel@tonic-gate 		error = EINVAL;
24520Sstevel@tonic-gate 		goto out;
24530Sstevel@tonic-gate 	}
24540Sstevel@tonic-gate 	if (sfv_off + sfv_len > MAXOFFSET_T)
24550Sstevel@tonic-gate 		sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
24560Sstevel@tonic-gate 
24570Sstevel@tonic-gate 	/*
24580Sstevel@tonic-gate 	 * There are no more checks on sfv_len. So, we cast it to
24590Sstevel@tonic-gate 	 * u_offset_t and share the snf_direct_io/snf_cache code between
24600Sstevel@tonic-gate 	 * 32 bit and 64 bit.
24610Sstevel@tonic-gate 	 *
24620Sstevel@tonic-gate 	 * TODO: should do nbl_need_check() like read()?
24630Sstevel@tonic-gate 	 */
24640Sstevel@tonic-gate 	if (sfv_len > sendfile_max_size) {
24650Sstevel@tonic-gate 		sf_stats.ss_file_not_cached++;
24660Sstevel@tonic-gate 		error = snf_direct_io(fp, rfp, sfv_off, (u_offset_t)sfv_len,
24670Sstevel@tonic-gate 		    &count);
24680Sstevel@tonic-gate 		goto out;
24690Sstevel@tonic-gate 	}
24700Sstevel@tonic-gate 	fvp = rfp->f_vnode;
24710Sstevel@tonic-gate 	if (VOP_REALVP(fvp, &realvp) == 0)
24720Sstevel@tonic-gate 		fvp = realvp;
24730Sstevel@tonic-gate 	/*
24740Sstevel@tonic-gate 	 * Grab the lock as a reader to prevent the file size
24750Sstevel@tonic-gate 	 * from changing underneath.
24760Sstevel@tonic-gate 	 */
24770Sstevel@tonic-gate 	(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
24780Sstevel@tonic-gate 	va.va_mask = AT_SIZE;
24790Sstevel@tonic-gate 	error = VOP_GETATTR(fvp, &va, 0, kcred);
24800Sstevel@tonic-gate 	va_size = va.va_size;
24810Sstevel@tonic-gate 	if ((error != 0) || (va_size == 0) || (sfv_off >= va_size)) {
24820Sstevel@tonic-gate 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
24830Sstevel@tonic-gate 		goto out;
24840Sstevel@tonic-gate 	}
24850Sstevel@tonic-gate 	/* Read as much as possible. */
24860Sstevel@tonic-gate 	if (sfv_off + sfv_len > va_size)
24870Sstevel@tonic-gate 		sfv_len = va_size - sfv_off;
24880Sstevel@tonic-gate 
24890Sstevel@tonic-gate 	vp = fp->f_vnode;
24900Sstevel@tonic-gate 	stp = vp->v_stream;
24910Sstevel@tonic-gate 	if (stp->sd_qn_maxpsz == INFPSZ)
24920Sstevel@tonic-gate 		maxpsz = MAXOFF32_T;
24930Sstevel@tonic-gate 	else
24940Sstevel@tonic-gate 		maxpsz = roundup(stp->sd_qn_maxpsz, MAXBSIZE);
24950Sstevel@tonic-gate 	/*
24960Sstevel@tonic-gate 	 * When the NOWAIT flag is not set, we enable zero-copy only if the
24970Sstevel@tonic-gate 	 * transfer size is large enough. This prevents performance loss
24980Sstevel@tonic-gate 	 * when the caller sends the file piece by piece.
24990Sstevel@tonic-gate 	 */
25000Sstevel@tonic-gate 	if (sfv_len >= MAXBSIZE && (sfv_len >= (va_size >> 1) ||
25010Sstevel@tonic-gate 	    (sfv->sfv_flag & SFV_NOWAIT) || sfv_len >= 0x1000000) &&
25020Sstevel@tonic-gate 	    !vn_has_flocks(fvp)) {
25030Sstevel@tonic-gate 		if ((stp->sd_copyflag & (STZCVMSAFE|STZCVMUNSAFE)) == 0) {
25040Sstevel@tonic-gate 			int on = 1;
25050Sstevel@tonic-gate 
25060Sstevel@tonic-gate 			if (SOP_SETSOCKOPT(VTOSO(vp), SOL_SOCKET,
25070Sstevel@tonic-gate 			    SO_SND_COPYAVOID, &on, sizeof (on)) == 0)
25080Sstevel@tonic-gate 				dozcopy = B_TRUE;
25090Sstevel@tonic-gate 		} else {
25100Sstevel@tonic-gate 			dozcopy = (stp->sd_copyflag & STZCVMSAFE);
25110Sstevel@tonic-gate 		}
25120Sstevel@tonic-gate 	}
25130Sstevel@tonic-gate 	if (dozcopy) {
25140Sstevel@tonic-gate 		sf_stats.ss_file_segmap++;
25150Sstevel@tonic-gate 		error = snf_segmap(fp, fvp, sfv_off, (u_offset_t)sfv_len,
25160Sstevel@tonic-gate 		    maxpsz, &count, ((sfv->sfv_flag & SFV_NOWAIT) != 0));
25170Sstevel@tonic-gate 	} else {
25180Sstevel@tonic-gate 		sf_stats.ss_file_cached++;
25190Sstevel@tonic-gate 		error = snf_cache(fp, fvp, sfv_off, (u_offset_t)sfv_len,
25200Sstevel@tonic-gate 		    maxpsz, &count);
25210Sstevel@tonic-gate 	}
25220Sstevel@tonic-gate out:
25230Sstevel@tonic-gate 	releasef(sfv->sfv_fd);
25240Sstevel@tonic-gate 	*count32 = (ssize32_t)count;
25250Sstevel@tonic-gate 	return (error);
25260Sstevel@tonic-gate }
25270Sstevel@tonic-gate #endif
25280Sstevel@tonic-gate 
25290Sstevel@tonic-gate #ifdef _SYSCALL32_IMPL
25300Sstevel@tonic-gate /*
25310Sstevel@tonic-gate  * recv32(), recvfrom32(), send32(), sendto32(): intentionally return a
25320Sstevel@tonic-gate  * ssize_t rather than ssize32_t; see the comments above read32 for details.
25330Sstevel@tonic-gate  */
25340Sstevel@tonic-gate 
25350Sstevel@tonic-gate ssize_t
25360Sstevel@tonic-gate recv32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags)
25370Sstevel@tonic-gate {
25380Sstevel@tonic-gate 	return (recv(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags));
25390Sstevel@tonic-gate }
25400Sstevel@tonic-gate 
25410Sstevel@tonic-gate ssize_t
25420Sstevel@tonic-gate recvfrom32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags,
25430Sstevel@tonic-gate 	caddr32_t name, caddr32_t namelenp)
25440Sstevel@tonic-gate {
25450Sstevel@tonic-gate 	return (recvfrom(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags,
25460Sstevel@tonic-gate 	    (void *)(uintptr_t)name, (void *)(uintptr_t)namelenp));
25470Sstevel@tonic-gate }
25480Sstevel@tonic-gate 
25490Sstevel@tonic-gate ssize_t
25500Sstevel@tonic-gate send32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags)
25510Sstevel@tonic-gate {
25520Sstevel@tonic-gate 	return (send(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags));
25530Sstevel@tonic-gate }
25540Sstevel@tonic-gate 
25550Sstevel@tonic-gate ssize_t
25560Sstevel@tonic-gate sendto32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags,
25570Sstevel@tonic-gate 	caddr32_t name, socklen_t namelen)
25580Sstevel@tonic-gate {
25590Sstevel@tonic-gate 	return (sendto(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags,
25600Sstevel@tonic-gate 	    (void *)(uintptr_t)name, namelen));
25610Sstevel@tonic-gate }
25620Sstevel@tonic-gate #endif	/* _SYSCALL32_IMPL */
25630Sstevel@tonic-gate 
25640Sstevel@tonic-gate /*
25650Sstevel@tonic-gate  * Function wrappers (mostly arround the sonode switch) for
25660Sstevel@tonic-gate  * backward compatibility.
25670Sstevel@tonic-gate  */
25680Sstevel@tonic-gate 
25690Sstevel@tonic-gate int
25700Sstevel@tonic-gate soaccept(struct sonode *so, int fflag, struct sonode **nsop)
25710Sstevel@tonic-gate {
25720Sstevel@tonic-gate 	return (SOP_ACCEPT(so, fflag, nsop));
25730Sstevel@tonic-gate }
25740Sstevel@tonic-gate 
25750Sstevel@tonic-gate int
25760Sstevel@tonic-gate sobind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
25770Sstevel@tonic-gate     int backlog, int flags)
25780Sstevel@tonic-gate {
25790Sstevel@tonic-gate 	int	error;
25800Sstevel@tonic-gate 
25810Sstevel@tonic-gate 	error = SOP_BIND(so, name, namelen, flags);
25820Sstevel@tonic-gate 	if (error == 0 && backlog != 0)
25830Sstevel@tonic-gate 		return (SOP_LISTEN(so, backlog));
25840Sstevel@tonic-gate 
25850Sstevel@tonic-gate 	return (error);
25860Sstevel@tonic-gate }
25870Sstevel@tonic-gate 
25880Sstevel@tonic-gate int
25890Sstevel@tonic-gate solisten(struct sonode *so, int backlog)
25900Sstevel@tonic-gate {
25910Sstevel@tonic-gate 	return (SOP_LISTEN(so, backlog));
25920Sstevel@tonic-gate }
25930Sstevel@tonic-gate 
25940Sstevel@tonic-gate int
25950Sstevel@tonic-gate soconnect(struct sonode *so, const struct sockaddr *name, socklen_t namelen,
25960Sstevel@tonic-gate     int fflag, int flags)
25970Sstevel@tonic-gate {
25980Sstevel@tonic-gate 	return (SOP_CONNECT(so, name, namelen, fflag, flags));
25990Sstevel@tonic-gate }
26000Sstevel@tonic-gate 
26010Sstevel@tonic-gate int
26020Sstevel@tonic-gate sorecvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
26030Sstevel@tonic-gate {
26040Sstevel@tonic-gate 	return (SOP_RECVMSG(so, msg, uiop));
26050Sstevel@tonic-gate }
26060Sstevel@tonic-gate 
26070Sstevel@tonic-gate int
26080Sstevel@tonic-gate sosendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
26090Sstevel@tonic-gate {
26100Sstevel@tonic-gate 	return (SOP_SENDMSG(so, msg, uiop));
26110Sstevel@tonic-gate }
26120Sstevel@tonic-gate 
26130Sstevel@tonic-gate int
26140Sstevel@tonic-gate sogetpeername(struct sonode *so)
26150Sstevel@tonic-gate {
26160Sstevel@tonic-gate 	return (SOP_GETPEERNAME(so));
26170Sstevel@tonic-gate }
26180Sstevel@tonic-gate 
26190Sstevel@tonic-gate int
26200Sstevel@tonic-gate sogetsockname(struct sonode *so)
26210Sstevel@tonic-gate {
26220Sstevel@tonic-gate 	return (SOP_GETSOCKNAME(so));
26230Sstevel@tonic-gate }
26240Sstevel@tonic-gate 
26250Sstevel@tonic-gate int
26260Sstevel@tonic-gate soshutdown(struct sonode *so, int how)
26270Sstevel@tonic-gate {
26280Sstevel@tonic-gate 	return (SOP_SHUTDOWN(so, how));
26290Sstevel@tonic-gate }
26300Sstevel@tonic-gate 
26310Sstevel@tonic-gate int
26320Sstevel@tonic-gate sogetsockopt(struct sonode *so, int level, int option_name, void *optval,
26330Sstevel@tonic-gate     socklen_t *optlenp, int flags)
26340Sstevel@tonic-gate {
26350Sstevel@tonic-gate 	return (SOP_GETSOCKOPT(so, level, option_name, optval, optlenp,
26360Sstevel@tonic-gate 	    flags));
26370Sstevel@tonic-gate }
26380Sstevel@tonic-gate 
26390Sstevel@tonic-gate int
26400Sstevel@tonic-gate sosetsockopt(struct sonode *so, int level, int option_name, const void *optval,
26410Sstevel@tonic-gate     t_uscalar_t optlen)
26420Sstevel@tonic-gate {
26430Sstevel@tonic-gate 	return (SOP_SETSOCKOPT(so, level, option_name, optval, optlen));
26440Sstevel@tonic-gate }
26450Sstevel@tonic-gate 
26460Sstevel@tonic-gate /*
26470Sstevel@tonic-gate  * Because this is backward compatibility interface it only needs to be
26480Sstevel@tonic-gate  * able to handle the creation of TPI sockfs sockets.
26490Sstevel@tonic-gate  */
26500Sstevel@tonic-gate struct sonode *
26510Sstevel@tonic-gate socreate(vnode_t *accessvp, int domain, int type, int protocol, int version,
26520Sstevel@tonic-gate     struct sonode *tso, int *errorp)
26530Sstevel@tonic-gate {
26540Sstevel@tonic-gate 	return (sotpi_create(accessvp, domain, type, protocol, version, tso,
26550Sstevel@tonic-gate 	    errorp));
26560Sstevel@tonic-gate }
2657