xref: /titanic_41/usr/src/uts/common/fs/sockfs/socksyscalls.c (revision bbfe764ef179c2f606b093b6ec6bb149cde0e31b)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/t_lock.h>
31 #include <sys/param.h>
32 #include <sys/systm.h>
33 #include <sys/buf.h>
34 #include <sys/conf.h>
35 #include <sys/cred.h>
36 #include <sys/kmem.h>
37 #include <sys/sysmacros.h>
38 #include <sys/vfs.h>
39 #include <sys/vnode.h>
40 #include <sys/debug.h>
41 #include <sys/errno.h>
42 #include <sys/time.h>
43 #include <sys/file.h>
44 #include <sys/user.h>
45 #include <sys/stream.h>
46 #include <sys/strsubr.h>
47 #include <sys/strsun.h>
48 #include <sys/sunddi.h>
49 #include <sys/esunddi.h>
50 #include <sys/flock.h>
51 #include <sys/modctl.h>
52 #include <sys/cmn_err.h>
53 #include <sys/vmsystm.h>
54 #include <sys/policy.h>
55 
56 #include <sys/socket.h>
57 #include <sys/socketvar.h>
58 
59 #include <sys/isa_defs.h>
60 #include <sys/inttypes.h>
61 #include <sys/systm.h>
62 #include <sys/cpuvar.h>
63 #include <sys/filio.h>
64 #include <sys/sendfile.h>
65 #include <sys/ddi.h>
66 #include <vm/seg.h>
67 #include <vm/seg_map.h>
68 #include <vm/seg_kpm.h>
69 #include <fs/sockfs/nl7c.h>
70 
71 #ifdef SOCK_TEST
72 int do_useracc = 1;		/* Controlled by setting SO_DEBUG to 4 */
73 #else
74 #define	do_useracc	1
75 #endif /* SOCK_TEST */
76 
77 extern int xnet_truncate_print;
78 
79 /*
80  * Note: DEF_IOV_MAX is defined and used as it is in "fs/vncalls.c"
81  *	 as there isn't a formal definition of IOV_MAX ???
82  */
83 #define	MSG_MAXIOVLEN	16
84 
85 /*
86  * Kernel component of socket creation.
87  *
88  * The socket library determines which version number to use.
89  * First the library calls this with a NULL devpath. If this fails
90  * to find a transport (using solookup) the library will look in /etc/netconfig
91  * for the appropriate transport. If one is found it will pass in the
92  * devpath for the kernel to use.
93  */
94 int
95 so_socket(int domain, int type, int protocol, char *devpath, int version)
96 {
97 	vnode_t *accessvp;
98 	struct sonode *so;
99 	vnode_t *vp;
100 	struct file *fp;
101 	int fd;
102 	int error;
103 	boolean_t wildcard = B_FALSE;
104 	int saved_error = 0;
105 	int sdomain = domain;
106 
107 	dprint(1, ("so_socket(%d,%d,%d,%p,%d)\n",
108 		domain, type, protocol, devpath, version));
109 
110 	if (domain == AF_NCA) {
111 		/*
112 		 * The request is for an NCA socket so for NL7C use the
113 		 * INET domain instead and mark NL7C_AF_NCA below.
114 		 */
115 		domain = AF_INET;
116 		/*
117 		 * NL7C is not supported in non-global zones,
118 		 *  we enforce this restriction here.
119 		 */
120 		if (getzoneid() != GLOBAL_ZONEID) {
121 			return (set_errno(ENOTSUP));
122 		}
123 	}
124 
125 	accessvp = solookup(domain, type, protocol, devpath, &error);
126 	if (accessvp == NULL) {
127 		/*
128 		 * If there is either an EPROTONOSUPPORT or EPROTOTYPE error
129 		 * it makes sense doing the wildcard lookup since the
130 		 * protocol might not be in the table.
131 		 */
132 		if (devpath != NULL || protocol == 0 ||
133 		    !(error == EPROTONOSUPPORT || error == EPROTOTYPE))
134 			return (set_errno(error));
135 
136 		saved_error = error;
137 
138 		/*
139 		 * Try wildcard lookup. Never use devpath for wildcards.
140 		 */
141 		accessvp = solookup(domain, type, 0, NULL, &error);
142 		if (accessvp == NULL) {
143 			/*
144 			 * Can't find in kernel table - have library
145 			 * fall back to /etc/netconfig and tell us
146 			 * the devpath (The library will do this if it didn't
147 			 * already pass in a devpath).
148 			 */
149 			if (saved_error != 0)
150 				error = saved_error;
151 			return (set_errno(error));
152 		}
153 		wildcard = B_TRUE;
154 	}
155 
156 	/* Check the device policy */
157 	if ((error = secpolicy_spec_open(CRED(),
158 	    accessvp, FREAD|FWRITE)) != 0) {
159 		return (set_errno(error));
160 	}
161 
162 	if (protocol == IPPROTO_SCTP) {
163 		so = sosctp_create(accessvp, domain, type, protocol, version,
164 		    NULL, &error);
165 	} else if (protocol == PROTO_SDP) {
166 		so = sosdp_create(accessvp, domain, type, protocol, version,
167 		    NULL, &error);
168 	} else {
169 		so = sotpi_create(accessvp, domain, type, protocol, version,
170 		    NULL, &error);
171 	}
172 	if (so == NULL) {
173 		return (set_errno(error));
174 	}
175 	if (sdomain == AF_NCA && domain == AF_INET) {
176 		so->so_nl7c_flags = NL7C_AF_NCA;
177 	}
178 	vp = SOTOV(so);
179 
180 	if (wildcard) {
181 		/*
182 		 * Issue SO_PROTOTYPE setsockopt.
183 		 */
184 		error = SOP_SETSOCKOPT(so, SOL_SOCKET, SO_PROTOTYPE,
185 				&protocol,
186 				(t_uscalar_t)sizeof (protocol));
187 		if (error) {
188 			(void) VOP_CLOSE(vp, 0, 1, 0, CRED());
189 			VN_RELE(vp);
190 			/*
191 			 * Setsockopt often fails with ENOPROTOOPT but socket()
192 			 * should fail with EPROTONOSUPPORT/EPROTOTYPE.
193 			 */
194 			if (saved_error != 0 && error == ENOPROTOOPT)
195 				error = saved_error;
196 			else
197 				error = EPROTONOSUPPORT;
198 			return (set_errno(error));
199 		}
200 	}
201 	if (error = falloc(vp, FWRITE|FREAD, &fp, &fd)) {
202 		(void) VOP_CLOSE(vp, 0, 1, 0, CRED());
203 		VN_RELE(vp);
204 		return (set_errno(error));
205 	}
206 
207 	/*
208 	 * Now fill in the entries that falloc reserved
209 	 */
210 	mutex_exit(&fp->f_tlock);
211 	setf(fd, fp);
212 
213 	return (fd);
214 }
215 
216 /*
217  * Map from a file descriptor to a socket node.
218  * Returns with the file descriptor held i.e. the caller has to
219  * use releasef when done with the file descriptor.
220  */
221 static struct sonode *
222 getsonode(int sock, int *errorp, file_t **fpp)
223 {
224 	file_t *fp;
225 	vnode_t *vp;
226 	struct sonode *so;
227 
228 	if ((fp = getf(sock)) == NULL) {
229 		*errorp = EBADF;
230 		eprintline(*errorp);
231 		return (NULL);
232 	}
233 	vp = fp->f_vnode;
234 	/* Check if it is a socket */
235 	if (vp->v_type != VSOCK) {
236 		releasef(sock);
237 		*errorp = ENOTSOCK;
238 		eprintline(*errorp);
239 		return (NULL);
240 	}
241 	/*
242 	 * Use the stream head to find the real socket vnode.
243 	 * This is needed when namefs sits above sockfs.
244 	 */
245 	if (vp->v_stream) {
246 		ASSERT(vp->v_stream->sd_vnode);
247 		vp = vp->v_stream->sd_vnode;
248 
249 		so = VTOSO(vp);
250 		if (so->so_version == SOV_STREAM) {
251 			releasef(sock);
252 			*errorp = ENOTSOCK;
253 			eprintsoline(so, *errorp);
254 			return (NULL);
255 		}
256 	} else {
257 		so = VTOSO(vp);
258 	}
259 	if (fpp)
260 		*fpp = fp;
261 	return (so);
262 }
263 
264 /*
265  * Allocate and copyin a sockaddr.
266  * Ensures NULL termination for AF_UNIX addresses by extending them
267  * with one NULL byte if need be. Verifies that the length is not
268  * excessive to prevent an application from consuming all of kernel
269  * memory. Returns NULL when an error occurred.
270  */
271 static struct sockaddr *
272 copyin_name(struct sonode *so, struct sockaddr *name, socklen_t *namelenp,
273 	    int *errorp)
274 {
275 	char	*faddr;
276 	size_t	namelen = (size_t)*namelenp;
277 
278 	ASSERT(namelen != 0);
279 	if (namelen > SO_MAXARGSIZE) {
280 		*errorp = EINVAL;
281 		eprintsoline(so, *errorp);
282 		return (NULL);
283 	}
284 
285 	faddr = (char *)kmem_alloc(namelen, KM_SLEEP);
286 	if (copyin(name, faddr, namelen)) {
287 		kmem_free(faddr, namelen);
288 		*errorp = EFAULT;
289 		eprintsoline(so, *errorp);
290 		return (NULL);
291 	}
292 
293 	/*
294 	 * Add space for NULL termination if needed.
295 	 * Do a quick check if the last byte is NUL.
296 	 */
297 	if (so->so_family == AF_UNIX && faddr[namelen - 1] != '\0') {
298 		/* Check if there is any NULL termination */
299 		size_t	i;
300 		int foundnull = 0;
301 
302 		for (i = sizeof (name->sa_family); i < namelen; i++) {
303 			if (faddr[i] == '\0') {
304 				foundnull = 1;
305 				break;
306 			}
307 		}
308 		if (!foundnull) {
309 			/* Add extra byte for NUL padding */
310 			char *nfaddr;
311 
312 			nfaddr = (char *)kmem_alloc(namelen + 1, KM_SLEEP);
313 			bcopy(faddr, nfaddr, namelen);
314 			kmem_free(faddr, namelen);
315 
316 			/* NUL terminate */
317 			nfaddr[namelen] = '\0';
318 			namelen++;
319 			ASSERT((socklen_t)namelen == namelen);
320 			*namelenp = (socklen_t)namelen;
321 			faddr = nfaddr;
322 		}
323 	}
324 	return ((struct sockaddr *)faddr);
325 }
326 
327 /*
328  * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL.
329  */
330 static int
331 copyout_arg(void *uaddr, socklen_t ulen, void *ulenp,
332 		void *kaddr, socklen_t klen)
333 {
334 	if (uaddr != NULL) {
335 		if (ulen > klen)
336 			ulen = klen;
337 
338 		if (ulen != 0) {
339 			if (copyout(kaddr, uaddr, ulen))
340 				return (EFAULT);
341 		}
342 	} else
343 		ulen = 0;
344 
345 	if (ulenp != NULL) {
346 		if (copyout(&ulen, ulenp, sizeof (ulen)))
347 			return (EFAULT);
348 	}
349 	return (0);
350 }
351 
352 /*
353  * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL.
354  * If klen is greater than ulen it still uses the non-truncated
355  * klen to update ulenp.
356  */
357 static int
358 copyout_name(void *uaddr, socklen_t ulen, void *ulenp,
359 		void *kaddr, socklen_t klen)
360 {
361 	if (uaddr != NULL) {
362 		if (ulen >= klen)
363 			ulen = klen;
364 		else if (ulen != 0 && xnet_truncate_print) {
365 			printf("sockfs: truncating copyout of address using "
366 			    "XNET semantics for pid = %d. Lengths %d, %d\n",
367 			    curproc->p_pid, klen, ulen);
368 		}
369 
370 		if (ulen != 0) {
371 			if (copyout(kaddr, uaddr, ulen))
372 				return (EFAULT);
373 		} else
374 			klen = 0;
375 	} else
376 		klen = 0;
377 
378 	if (ulenp != NULL) {
379 		if (copyout(&klen, ulenp, sizeof (klen)))
380 			return (EFAULT);
381 	}
382 	return (0);
383 }
384 
385 /*
386  * The socketpair() code in libsocket creates two sockets (using
387  * the /etc/netconfig fallback if needed) before calling this routine
388  * to connect the two sockets together.
389  *
390  * For a SOCK_STREAM socketpair a listener is needed - in that case this
391  * routine will create a new file descriptor as part of accepting the
392  * connection. The library socketpair() will check if svs[2] has changed
393  * in which case it will close the changed fd.
394  *
395  * Note that this code could use the TPI feature of accepting the connection
396  * on the listening endpoint. However, that would require significant changes
397  * to soaccept.
398  */
399 int
400 so_socketpair(int sv[2])
401 {
402 	int svs[2];
403 	struct sonode *so1, *so2;
404 	int error;
405 	struct sockaddr_ux *name;
406 	size_t namelen;
407 
408 	dprint(1, ("so_socketpair(%p)\n", sv));
409 
410 	error = useracc(sv, sizeof (svs), B_WRITE);
411 	if (error && do_useracc)
412 		return (set_errno(EFAULT));
413 
414 	if (copyin(sv, svs, sizeof (svs)))
415 		return (set_errno(EFAULT));
416 
417 	if ((so1 = getsonode(svs[0], &error, NULL)) == NULL)
418 		return (set_errno(error));
419 
420 	if ((so2 = getsonode(svs[1], &error, NULL)) == NULL) {
421 		releasef(svs[0]);
422 		return (set_errno(error));
423 	}
424 
425 	if (so1->so_family != AF_UNIX || so2->so_family != AF_UNIX) {
426 		error = EOPNOTSUPP;
427 		goto done;
428 	}
429 
430 	/*
431 	 * The code below makes assumptions about the "sockfs" implementation.
432 	 * So make sure that the correct implementation is really used.
433 	 */
434 	ASSERT(so1->so_ops == &sotpi_sonodeops);
435 	ASSERT(so2->so_ops == &sotpi_sonodeops);
436 
437 	if (so1->so_type == SOCK_DGRAM) {
438 		/*
439 		 * Bind both sockets and connect them with each other.
440 		 * Need to allocate name/namelen for soconnect.
441 		 */
442 		error = SOP_BIND(so1, NULL, 0, _SOBIND_UNSPEC);
443 		if (error) {
444 			eprintsoline(so1, error);
445 			goto done;
446 		}
447 		error = SOP_BIND(so2, NULL, 0, _SOBIND_UNSPEC);
448 		if (error) {
449 			eprintsoline(so2, error);
450 			goto done;
451 		}
452 		namelen = sizeof (struct sockaddr_ux);
453 		name = kmem_alloc(namelen, KM_SLEEP);
454 		name->sou_family = AF_UNIX;
455 		name->sou_addr = so2->so_ux_laddr;
456 		error = SOP_CONNECT(so1,
457 				(struct sockaddr *)name,
458 				(socklen_t)namelen,
459 				0, _SOCONNECT_NOXLATE);
460 		if (error) {
461 			kmem_free(name, namelen);
462 			eprintsoline(so1, error);
463 			goto done;
464 		}
465 		name->sou_addr = so1->so_ux_laddr;
466 		error = SOP_CONNECT(so2,
467 				(struct sockaddr *)name,
468 				(socklen_t)namelen,
469 				0, _SOCONNECT_NOXLATE);
470 		kmem_free(name, namelen);
471 		if (error) {
472 			eprintsoline(so2, error);
473 			goto done;
474 		}
475 		releasef(svs[0]);
476 		releasef(svs[1]);
477 	} else {
478 		/*
479 		 * Bind both sockets, with so1 being a listener.
480 		 * Connect so2 to so1 - nonblocking to avoid waiting for
481 		 * soaccept to complete.
482 		 * Accept a connection on so1. Pass out the new fd as sv[0].
483 		 * The library will detect the changed fd and close
484 		 * the original one.
485 		 */
486 		struct sonode *nso;
487 		struct vnode *nvp;
488 		struct file *nfp;
489 		int nfd;
490 
491 		/*
492 		 * We could simply call SOP_LISTEN() here (which would do the
493 		 * binding automatically) if the code didn't rely on passing
494 		 * _SOBIND_NOXLATE to the TPI implementation of SOP_BIND().
495 		 */
496 		error = SOP_BIND(so1, NULL, 0, _SOBIND_UNSPEC|_SOBIND_NOXLATE|
497 		    _SOBIND_LISTEN|_SOBIND_SOCKETPAIR);
498 		if (error) {
499 			eprintsoline(so1, error);
500 			goto done;
501 		}
502 		error = SOP_BIND(so2, NULL, 0, _SOBIND_UNSPEC);
503 		if (error) {
504 			eprintsoline(so2, error);
505 			goto done;
506 		}
507 
508 		namelen = sizeof (struct sockaddr_ux);
509 		name = kmem_alloc(namelen, KM_SLEEP);
510 		name->sou_family = AF_UNIX;
511 		name->sou_addr = so1->so_ux_laddr;
512 		error = SOP_CONNECT(so2,
513 				(struct sockaddr *)name,
514 				(socklen_t)namelen,
515 				FNONBLOCK, _SOCONNECT_NOXLATE);
516 		kmem_free(name, namelen);
517 		if (error) {
518 			if (error != EINPROGRESS) {
519 				eprintsoline(so2, error);
520 				goto done;
521 			}
522 		}
523 
524 		error = SOP_ACCEPT(so1, 0, &nso);
525 		if (error) {
526 			eprintsoline(so1, error);
527 			goto done;
528 		}
529 
530 		/* wait for so2 being SS_CONNECTED ignoring signals */
531 		mutex_enter(&so2->so_lock);
532 		error = sowaitconnected(so2, 0, 1);
533 		mutex_exit(&so2->so_lock);
534 		nvp = SOTOV(nso);
535 		if (error != 0) {
536 			(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
537 			VN_RELE(nvp);
538 			eprintsoline(so2, error);
539 			goto done;
540 		}
541 
542 		if (error = falloc(nvp, FWRITE|FREAD, &nfp, &nfd)) {
543 			(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
544 			VN_RELE(nvp);
545 			eprintsoline(nso, error);
546 			goto done;
547 		}
548 		/*
549 		 * fill in the entries that falloc reserved
550 		 */
551 		mutex_exit(&nfp->f_tlock);
552 		setf(nfd, nfp);
553 
554 		releasef(svs[0]);
555 		releasef(svs[1]);
556 		svs[0] = nfd;
557 
558 		/*
559 		 * The socketpair library routine will close the original
560 		 * svs[0] when this code passes out a different file
561 		 * descriptor.
562 		 */
563 		if (copyout(svs, sv, sizeof (svs))) {
564 			(void) closeandsetf(nfd, NULL);
565 			eprintline(EFAULT);
566 			return (set_errno(EFAULT));
567 		}
568 	}
569 	return (0);
570 
571 done:
572 	releasef(svs[0]);
573 	releasef(svs[1]);
574 	return (set_errno(error));
575 }
576 
577 int
578 bind(int sock, struct sockaddr *name, socklen_t namelen, int version)
579 {
580 	struct sonode *so;
581 	int error;
582 
583 	dprint(1, ("bind(%d, %p, %d)\n",
584 		sock, name, namelen));
585 
586 	if ((so = getsonode(sock, &error, NULL)) == NULL)
587 		return (set_errno(error));
588 
589 	/* Allocate and copyin name */
590 	/*
591 	 * X/Open test does not expect EFAULT with NULL name and non-zero
592 	 * namelen.
593 	 */
594 	if (name != NULL && namelen != 0) {
595 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
596 		name = copyin_name(so, name, &namelen, &error);
597 		if (name == NULL) {
598 			releasef(sock);
599 			return (set_errno(error));
600 		}
601 	} else {
602 		name = NULL;
603 		namelen = 0;
604 	}
605 
606 	switch (version) {
607 	default:
608 		error = SOP_BIND(so, name, namelen, 0);
609 		break;
610 	case SOV_XPG4_2:
611 		error = SOP_BIND(so, name, namelen, _SOBIND_XPG4_2);
612 		break;
613 	case SOV_SOCKBSD:
614 		error = SOP_BIND(so, name, namelen, _SOBIND_SOCKBSD);
615 		break;
616 	}
617 done:
618 	releasef(sock);
619 	if (name != NULL)
620 		kmem_free(name, (size_t)namelen);
621 
622 	if (error)
623 		return (set_errno(error));
624 	return (0);
625 }
626 
627 /* ARGSUSED2 */
628 int
629 listen(int sock, int backlog, int version)
630 {
631 	struct sonode *so;
632 	int error;
633 
634 	dprint(1, ("listen(%d, %d)\n",
635 		sock, backlog));
636 
637 	if ((so = getsonode(sock, &error, NULL)) == NULL)
638 		return (set_errno(error));
639 
640 	error = SOP_LISTEN(so, backlog);
641 
642 	releasef(sock);
643 	if (error)
644 		return (set_errno(error));
645 	return (0);
646 }
647 
648 /*ARGSUSED3*/
649 int
650 accept(int sock, struct sockaddr *name, socklen_t *namelenp, int version)
651 {
652 	struct sonode *so;
653 	file_t *fp;
654 	int error;
655 	socklen_t namelen;
656 	struct sonode *nso;
657 	struct vnode *nvp;
658 	struct file *nfp;
659 	int nfd;
660 
661 	dprint(1, ("accept(%d, %p, %p)\n",
662 		sock, name, namelenp));
663 
664 	if ((so = getsonode(sock, &error, &fp)) == NULL)
665 		return (set_errno(error));
666 
667 	if (name != NULL) {
668 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
669 		if (copyin(namelenp, &namelen, sizeof (namelen))) {
670 			releasef(sock);
671 			return (set_errno(EFAULT));
672 		}
673 		if (namelen != 0) {
674 			error = useracc(name, (size_t)namelen, B_WRITE);
675 			if (error && do_useracc) {
676 				releasef(sock);
677 				return (set_errno(EFAULT));
678 			}
679 		} else
680 			name = NULL;
681 	} else {
682 		namelen = 0;
683 	}
684 
685 	/*
686 	 * Allocate the user fd before SOP_ACCEPT() in order to
687 	 * catch EMFILE errors before calling SOP_ACCEPT().
688 	 */
689 	if ((nfd = ufalloc(0)) == -1) {
690 		eprintsoline(so, EMFILE);
691 		releasef(sock);
692 		return (set_errno(EMFILE));
693 	}
694 	error = SOP_ACCEPT(so, fp->f_flag, &nso);
695 	releasef(sock);
696 	if (error) {
697 		setf(nfd, NULL);
698 		return (set_errno(error));
699 	}
700 
701 	nvp = SOTOV(nso);
702 
703 	/*
704 	 * so_faddr_sa can not go away even though we are not holding so_lock.
705 	 * However, in theory its content could change from underneath us.
706 	 * But this is not possible in practice since it can only
707 	 * change due to either some socket system call
708 	 * or due to a T_CONN_CON being received from the stream head.
709 	 * Since the falloc/setf have not yet been done no thread
710 	 * can do any system call on nso and T_CONN_CON can not arrive
711 	 * on a socket that is already connected.
712 	 * Thus there is no reason to hold so_lock here.
713 	 *
714 	 * SOP_ACCEPT() is required to have set the valid bit for the faddr,
715 	 * but it could be instantly cleared by a disconnect from the transport.
716 	 * For that reason we ignore it here.
717 	 */
718 	ASSERT(MUTEX_NOT_HELD(&nso->so_lock));
719 	error = copyout_name(name, namelen, namelenp,
720 	    nso->so_faddr_sa, (socklen_t)nso->so_faddr_len);
721 	if (error) {
722 		setf(nfd, NULL);
723 		(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
724 		VN_RELE(nvp);
725 		return (set_errno(error));
726 	}
727 	if (error = falloc(NULL, FWRITE|FREAD, &nfp, NULL)) {
728 		setf(nfd, NULL);
729 		(void) VOP_CLOSE(nvp, 0, 1, 0, CRED());
730 		VN_RELE(nvp);
731 		eprintsoline(so, error);
732 		return (set_errno(error));
733 	}
734 	/*
735 	 * fill in the entries that falloc reserved
736 	 */
737 	nfp->f_vnode = nvp;
738 	mutex_exit(&nfp->f_tlock);
739 	setf(nfd, nfp);
740 
741 	/*
742 	 * Copy FNDELAY and FNONBLOCK from listener to acceptor
743 	 */
744 	if (so->so_state & (SS_NDELAY|SS_NONBLOCK)) {
745 		uint_t oflag = nfp->f_flag;
746 		int arg = 0;
747 
748 		if (so->so_state & SS_NONBLOCK)
749 			arg |= FNONBLOCK;
750 		else if (so->so_state & SS_NDELAY)
751 			arg |= FNDELAY;
752 
753 		/*
754 		 * This code is a simplification of the F_SETFL code in fcntl()
755 		 * Ignore any errors from VOP_SETFL.
756 		 */
757 		if ((error = VOP_SETFL(nvp, oflag, arg, nfp->f_cred)) != 0) {
758 			eprintsoline(so, error);
759 			error = 0;
760 		} else {
761 			mutex_enter(&nfp->f_tlock);
762 			nfp->f_flag &= ~FMASK | (FREAD|FWRITE);
763 			nfp->f_flag |= arg;
764 			mutex_exit(&nfp->f_tlock);
765 		}
766 	}
767 	return (nfd);
768 }
769 
770 int
771 connect(int sock, struct sockaddr *name, socklen_t namelen, int version)
772 {
773 	struct sonode *so;
774 	file_t *fp;
775 	int error;
776 
777 	dprint(1, ("connect(%d, %p, %d)\n",
778 		sock, name, namelen));
779 
780 	if ((so = getsonode(sock, &error, &fp)) == NULL)
781 		return (set_errno(error));
782 
783 	/* Allocate and copyin name */
784 	if (namelen != 0) {
785 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
786 		name = copyin_name(so, name, &namelen, &error);
787 		if (name == NULL) {
788 			releasef(sock);
789 			return (set_errno(error));
790 		}
791 	} else
792 		name = NULL;
793 
794 	error = SOP_CONNECT(so, name, namelen, fp->f_flag,
795 	    (version != SOV_XPG4_2) ? 0 : _SOCONNECT_XPG4_2);
796 	releasef(sock);
797 	if (name)
798 		kmem_free(name, (size_t)namelen);
799 	if (error)
800 		return (set_errno(error));
801 	return (0);
802 }
803 
804 /*ARGSUSED2*/
805 int
806 shutdown(int sock, int how, int version)
807 {
808 	struct sonode *so;
809 	int error;
810 
811 	dprint(1, ("shutdown(%d, %d)\n",
812 		sock, how));
813 
814 	if ((so = getsonode(sock, &error, NULL)) == NULL)
815 		return (set_errno(error));
816 
817 	error = SOP_SHUTDOWN(so, how);
818 
819 	releasef(sock);
820 	if (error)
821 		return (set_errno(error));
822 	return (0);
823 }
824 
825 /*
826  * Common receive routine.
827  */
828 static ssize_t
829 recvit(int sock,
830 	struct nmsghdr *msg,
831 	struct uio *uiop,
832 	int flags,
833 	socklen_t *namelenp,
834 	socklen_t *controllenp,
835 	int *flagsp)
836 {
837 	struct sonode *so;
838 	file_t *fp;
839 	void *name;
840 	socklen_t namelen;
841 	void *control;
842 	socklen_t controllen;
843 	ssize_t len;
844 	int error;
845 
846 	if ((so = getsonode(sock, &error, &fp)) == NULL)
847 		return (set_errno(error));
848 
849 	len = uiop->uio_resid;
850 	uiop->uio_fmode = fp->f_flag;
851 	uiop->uio_extflg = UIO_COPY_CACHED;
852 
853 	name = msg->msg_name;
854 	namelen = msg->msg_namelen;
855 	control = msg->msg_control;
856 	controllen = msg->msg_controllen;
857 
858 	msg->msg_flags = flags & (MSG_OOB | MSG_PEEK | MSG_WAITALL |
859 	    MSG_DONTWAIT | MSG_XPG4_2);
860 
861 	error = SOP_RECVMSG(so, msg, uiop);
862 	if (error) {
863 		releasef(sock);
864 		return (set_errno(error));
865 	}
866 	lwp_stat_update(LWP_STAT_MSGRCV, 1);
867 	so_update_attrs(so, SOACC);
868 	releasef(sock);
869 
870 	error = copyout_name(name, namelen, namelenp,
871 	    msg->msg_name, msg->msg_namelen);
872 	if (error)
873 		goto err;
874 
875 	if (flagsp != NULL) {
876 		/*
877 		 * Clear internal flag.
878 		 */
879 		msg->msg_flags &= ~MSG_XPG4_2;
880 
881 		/*
882 		 * Determine MSG_CTRUNC. sorecvmsg sets MSG_CTRUNC only
883 		 * when controllen is zero and there is control data to
884 		 * copy out.
885 		 */
886 		if (controllen != 0 &&
887 		    (msg->msg_controllen > controllen || control == NULL)) {
888 			dprint(1, ("recvit: CTRUNC %d %d %p\n",
889 			    msg->msg_controllen, controllen, control));
890 
891 			msg->msg_flags |= MSG_CTRUNC;
892 		}
893 		if (copyout(&msg->msg_flags, flagsp,
894 		    sizeof (msg->msg_flags))) {
895 			error = EFAULT;
896 			goto err;
897 		}
898 	}
899 	/*
900 	 * Note: This MUST be done last. There can be no "goto err" after this
901 	 * point since it could make so_closefds run twice on some part
902 	 * of the file descriptor array.
903 	 */
904 	if (controllen != 0) {
905 		if (!(flags & MSG_XPG4_2)) {
906 			/*
907 			 * Good old msg_accrights can only return a multiple
908 			 * of 4 bytes.
909 			 */
910 			controllen &= ~((int)sizeof (uint32_t) - 1);
911 		}
912 		error = copyout_arg(control, controllen, controllenp,
913 		    msg->msg_control, msg->msg_controllen);
914 		if (error)
915 			goto err;
916 
917 		if (msg->msg_controllen > controllen || control == NULL) {
918 			if (control == NULL)
919 				controllen = 0;
920 			so_closefds(msg->msg_control, msg->msg_controllen,
921 			    !(flags & MSG_XPG4_2), controllen);
922 		}
923 	}
924 	if (msg->msg_namelen != 0)
925 		kmem_free(msg->msg_name, (size_t)msg->msg_namelen);
926 	if (msg->msg_controllen != 0)
927 		kmem_free(msg->msg_control, (size_t)msg->msg_controllen);
928 	return (len - uiop->uio_resid);
929 
930 err:
931 	/*
932 	 * If we fail and the control part contains file descriptors
933 	 * we have to close the fd's.
934 	 */
935 	if (msg->msg_controllen != 0)
936 		so_closefds(msg->msg_control, msg->msg_controllen,
937 		    !(flags & MSG_XPG4_2), 0);
938 	if (msg->msg_namelen != 0)
939 		kmem_free(msg->msg_name, (size_t)msg->msg_namelen);
940 	if (msg->msg_controllen != 0)
941 		kmem_free(msg->msg_control, (size_t)msg->msg_controllen);
942 	return (set_errno(error));
943 }
944 
945 /*
946  * Native system call
947  */
948 ssize_t
949 recv(int sock, void *buffer, size_t len, int flags)
950 {
951 	struct nmsghdr lmsg;
952 	struct uio auio;
953 	struct iovec aiov[1];
954 
955 	dprint(1, ("recv(%d, %p, %ld, %d)\n",
956 		sock, buffer, len, flags));
957 
958 	if ((ssize_t)len < 0) {
959 		return (set_errno(EINVAL));
960 	}
961 
962 	aiov[0].iov_base = buffer;
963 	aiov[0].iov_len = len;
964 	auio.uio_loffset = 0;
965 	auio.uio_iov = aiov;
966 	auio.uio_iovcnt = 1;
967 	auio.uio_resid = len;
968 	auio.uio_segflg = UIO_USERSPACE;
969 	auio.uio_limit = 0;
970 
971 	lmsg.msg_namelen = 0;
972 	lmsg.msg_controllen = 0;
973 	lmsg.msg_flags = 0;
974 	return (recvit(sock, &lmsg, &auio, flags, NULL, NULL, NULL));
975 }
976 
977 ssize_t
978 recvfrom(int sock, void *buffer, size_t len, int flags,
979 	struct sockaddr *name, socklen_t *namelenp)
980 {
981 	struct nmsghdr lmsg;
982 	struct uio auio;
983 	struct iovec aiov[1];
984 
985 	dprint(1, ("recvfrom(%d, %p, %ld, %d, %p, %p)\n",
986 		sock, buffer, len, flags, name, namelenp));
987 
988 	if ((ssize_t)len < 0) {
989 		return (set_errno(EINVAL));
990 	}
991 
992 	aiov[0].iov_base = buffer;
993 	aiov[0].iov_len = len;
994 	auio.uio_loffset = 0;
995 	auio.uio_iov = aiov;
996 	auio.uio_iovcnt = 1;
997 	auio.uio_resid = len;
998 	auio.uio_segflg = UIO_USERSPACE;
999 	auio.uio_limit = 0;
1000 
1001 	lmsg.msg_name = (char *)name;
1002 	if (namelenp != NULL) {
1003 		if (copyin(namelenp, &lmsg.msg_namelen,
1004 		    sizeof (lmsg.msg_namelen)))
1005 			return (set_errno(EFAULT));
1006 	} else {
1007 		lmsg.msg_namelen = 0;
1008 	}
1009 	lmsg.msg_controllen = 0;
1010 	lmsg.msg_flags = 0;
1011 
1012 	return (recvit(sock, &lmsg, &auio, flags, namelenp, NULL, NULL));
1013 }
1014 
1015 /*
1016  * Uses the MSG_XPG4_2 flag to determine if the caller is using
1017  * struct omsghdr or struct nmsghdr.
1018  */
1019 ssize_t
1020 recvmsg(int sock, struct nmsghdr *msg, int flags)
1021 {
1022 	STRUCT_DECL(nmsghdr, u_lmsg);
1023 	STRUCT_HANDLE(nmsghdr, umsgptr);
1024 	struct nmsghdr lmsg;
1025 	struct uio auio;
1026 	struct iovec aiov[MSG_MAXIOVLEN];
1027 	int iovcnt;
1028 	ssize_t len;
1029 	int i;
1030 	int *flagsp;
1031 	model_t	model;
1032 
1033 	dprint(1, ("recvmsg(%d, %p, %d)\n",
1034 		sock, msg, flags));
1035 
1036 	model = get_udatamodel();
1037 	STRUCT_INIT(u_lmsg, model);
1038 	STRUCT_SET_HANDLE(umsgptr, model, msg);
1039 
1040 	if (flags & MSG_XPG4_2) {
1041 		if (copyin(msg, STRUCT_BUF(u_lmsg), STRUCT_SIZE(u_lmsg)))
1042 			return (set_errno(EFAULT));
1043 		flagsp = STRUCT_FADDR(umsgptr, msg_flags);
1044 	} else {
1045 		/*
1046 		 * Assumes that nmsghdr and omsghdr are identically shaped
1047 		 * except for the added msg_flags field.
1048 		 */
1049 		if (copyin(msg, STRUCT_BUF(u_lmsg),
1050 		    SIZEOF_STRUCT(omsghdr, model)))
1051 			return (set_errno(EFAULT));
1052 		STRUCT_FSET(u_lmsg, msg_flags, 0);
1053 		flagsp = NULL;
1054 	}
1055 
1056 	/*
1057 	 * Code below us will kmem_alloc memory and hang it
1058 	 * off msg_control and msg_name fields. This forces
1059 	 * us to copy the structure to its native form.
1060 	 */
1061 	lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name);
1062 	lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen);
1063 	lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov);
1064 	lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen);
1065 	lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control);
1066 	lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen);
1067 	lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags);
1068 
1069 	iovcnt = lmsg.msg_iovlen;
1070 
1071 	if (iovcnt <= 0 || iovcnt > MSG_MAXIOVLEN) {
1072 		return (set_errno(EMSGSIZE));
1073 	}
1074 
1075 #ifdef _SYSCALL32_IMPL
1076 	/*
1077 	 * 32-bit callers need to have their iovec expanded, while ensuring
1078 	 * that they can't move more than 2Gbytes of data in a single call.
1079 	 */
1080 	if (model == DATAMODEL_ILP32) {
1081 		struct iovec32 aiov32[MSG_MAXIOVLEN];
1082 		ssize32_t count32;
1083 
1084 		if (copyin((struct iovec32 *)lmsg.msg_iov, aiov32,
1085 		    iovcnt * sizeof (struct iovec32)))
1086 			return (set_errno(EFAULT));
1087 
1088 		count32 = 0;
1089 		for (i = 0; i < iovcnt; i++) {
1090 			ssize32_t iovlen32;
1091 
1092 			iovlen32 = aiov32[i].iov_len;
1093 			count32 += iovlen32;
1094 			if (iovlen32 < 0 || count32 < 0)
1095 				return (set_errno(EINVAL));
1096 			aiov[i].iov_len = iovlen32;
1097 			aiov[i].iov_base =
1098 			    (caddr_t)(uintptr_t)aiov32[i].iov_base;
1099 		}
1100 	} else
1101 #endif /* _SYSCALL32_IMPL */
1102 	if (copyin(lmsg.msg_iov, aiov, iovcnt * sizeof (struct iovec))) {
1103 		return (set_errno(EFAULT));
1104 	}
1105 	len = 0;
1106 	for (i = 0; i < iovcnt; i++) {
1107 		ssize_t iovlen = aiov[i].iov_len;
1108 		len += iovlen;
1109 		if (iovlen < 0 || len < 0) {
1110 			return (set_errno(EINVAL));
1111 		}
1112 	}
1113 	auio.uio_loffset = 0;
1114 	auio.uio_iov = aiov;
1115 	auio.uio_iovcnt = iovcnt;
1116 	auio.uio_resid = len;
1117 	auio.uio_segflg = UIO_USERSPACE;
1118 	auio.uio_limit = 0;
1119 
1120 	if (lmsg.msg_control != NULL &&
1121 	    (do_useracc == 0 ||
1122 	    useracc(lmsg.msg_control, lmsg.msg_controllen,
1123 			B_WRITE) != 0)) {
1124 		return (set_errno(EFAULT));
1125 	}
1126 
1127 	return (recvit(sock, &lmsg, &auio, flags,
1128 		STRUCT_FADDR(umsgptr, msg_namelen),
1129 		STRUCT_FADDR(umsgptr, msg_controllen), flagsp));
1130 }
1131 
1132 /*
1133  * Common send function.
1134  */
1135 static ssize_t
1136 sendit(int sock, struct nmsghdr *msg, struct uio *uiop, int flags)
1137 {
1138 	struct sonode *so;
1139 	file_t *fp;
1140 	void *name;
1141 	socklen_t namelen;
1142 	void *control;
1143 	socklen_t controllen;
1144 	ssize_t len;
1145 	int error;
1146 
1147 	if ((so = getsonode(sock, &error, &fp)) == NULL)
1148 		return (set_errno(error));
1149 
1150 	uiop->uio_fmode = fp->f_flag;
1151 
1152 	if (so->so_family == AF_UNIX)
1153 		uiop->uio_extflg = UIO_COPY_CACHED;
1154 	else
1155 		uiop->uio_extflg = UIO_COPY_DEFAULT;
1156 
1157 	/* Allocate and copyin name and control */
1158 	name = msg->msg_name;
1159 	namelen = msg->msg_namelen;
1160 	if (name != NULL && namelen != 0) {
1161 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1162 		name = copyin_name(so,
1163 				(struct sockaddr *)name,
1164 				&namelen, &error);
1165 		if (name == NULL)
1166 			goto done3;
1167 		/* copyin_name null terminates addresses for AF_UNIX */
1168 		msg->msg_namelen = namelen;
1169 		msg->msg_name = name;
1170 	} else {
1171 		msg->msg_name = name = NULL;
1172 		msg->msg_namelen = namelen = 0;
1173 	}
1174 
1175 	control = msg->msg_control;
1176 	controllen = msg->msg_controllen;
1177 	if ((control != NULL) && (controllen != 0)) {
1178 		/*
1179 		 * Verify that the length is not excessive to prevent
1180 		 * an application from consuming all of kernel memory.
1181 		 */
1182 		if (controllen > SO_MAXARGSIZE) {
1183 			error = EINVAL;
1184 			goto done2;
1185 		}
1186 		control = kmem_alloc(controllen, KM_SLEEP);
1187 
1188 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1189 		if (copyin(msg->msg_control, control, controllen)) {
1190 			error = EFAULT;
1191 			goto done1;
1192 		}
1193 		msg->msg_control = control;
1194 	} else {
1195 		msg->msg_control = control = NULL;
1196 		msg->msg_controllen = controllen = 0;
1197 	}
1198 
1199 	len = uiop->uio_resid;
1200 	msg->msg_flags = flags;
1201 
1202 	error = SOP_SENDMSG(so, msg, uiop);
1203 done1:
1204 	if (control != NULL)
1205 		kmem_free(control, controllen);
1206 done2:
1207 	if (name != NULL)
1208 		kmem_free(name, namelen);
1209 done3:
1210 	if (error != 0) {
1211 		releasef(sock);
1212 		return (set_errno(error));
1213 	}
1214 	lwp_stat_update(LWP_STAT_MSGSND, 1);
1215 	so_update_attrs(so, SOMOD);
1216 	releasef(sock);
1217 	return (len - uiop->uio_resid);
1218 }
1219 
1220 /*
1221  * Native system call
1222  */
1223 ssize_t
1224 send(int sock, void *buffer, size_t len, int flags)
1225 {
1226 	struct nmsghdr lmsg;
1227 	struct uio auio;
1228 	struct iovec aiov[1];
1229 
1230 	dprint(1, ("send(%d, %p, %ld, %d)\n",
1231 		sock, buffer, len, flags));
1232 
1233 	if ((ssize_t)len < 0) {
1234 		return (set_errno(EINVAL));
1235 	}
1236 
1237 	aiov[0].iov_base = buffer;
1238 	aiov[0].iov_len = len;
1239 	auio.uio_loffset = 0;
1240 	auio.uio_iov = aiov;
1241 	auio.uio_iovcnt = 1;
1242 	auio.uio_resid = len;
1243 	auio.uio_segflg = UIO_USERSPACE;
1244 	auio.uio_limit = 0;
1245 
1246 	lmsg.msg_name = NULL;
1247 	lmsg.msg_control = NULL;
1248 	if (!(flags & MSG_XPG4_2)) {
1249 		/*
1250 		 * In order to be compatible with the libsocket/sockmod
1251 		 * implementation we set EOR for all send* calls.
1252 		 */
1253 		flags |= MSG_EOR;
1254 	}
1255 	return (sendit(sock, &lmsg, &auio, flags));
1256 }
1257 
1258 /*
1259  * Uses the MSG_XPG4_2 flag to determine if the caller is using
1260  * struct omsghdr or struct nmsghdr.
1261  */
1262 ssize_t
1263 sendmsg(int sock, struct nmsghdr *msg, int flags)
1264 {
1265 	struct nmsghdr lmsg;
1266 	STRUCT_DECL(nmsghdr, u_lmsg);
1267 	struct uio auio;
1268 	struct iovec aiov[MSG_MAXIOVLEN];
1269 	int iovcnt;
1270 	ssize_t len;
1271 	int i;
1272 	model_t	model;
1273 
1274 	dprint(1, ("sendmsg(%d, %p, %d)\n", sock, msg, flags));
1275 
1276 	model = get_udatamodel();
1277 	STRUCT_INIT(u_lmsg, model);
1278 
1279 	if (flags & MSG_XPG4_2) {
1280 		if (copyin(msg, (char *)STRUCT_BUF(u_lmsg),
1281 		    STRUCT_SIZE(u_lmsg)))
1282 			return (set_errno(EFAULT));
1283 	} else {
1284 		/*
1285 		 * Assumes that nmsghdr and omsghdr are identically shaped
1286 		 * except for the added msg_flags field.
1287 		 */
1288 		if (copyin(msg, (char *)STRUCT_BUF(u_lmsg),
1289 		    SIZEOF_STRUCT(omsghdr, model)))
1290 			return (set_errno(EFAULT));
1291 		/*
1292 		 * In order to be compatible with the libsocket/sockmod
1293 		 * implementation we set EOR for all send* calls.
1294 		 */
1295 		flags |= MSG_EOR;
1296 	}
1297 
1298 	/*
1299 	 * Code below us will kmem_alloc memory and hang it
1300 	 * off msg_control and msg_name fields. This forces
1301 	 * us to copy the structure to its native form.
1302 	 */
1303 	lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name);
1304 	lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen);
1305 	lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov);
1306 	lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen);
1307 	lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control);
1308 	lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen);
1309 	lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags);
1310 
1311 	iovcnt = lmsg.msg_iovlen;
1312 
1313 	if (iovcnt <= 0 || iovcnt > MSG_MAXIOVLEN) {
1314 		/*
1315 		 * Unless this is XPG 4.2 we allow iovcnt == 0 to
1316 		 * be compatible with SunOS 4.X and 4.4BSD.
1317 		 */
1318 		if (iovcnt != 0 || (flags & MSG_XPG4_2))
1319 			return (set_errno(EMSGSIZE));
1320 	}
1321 
1322 #ifdef _SYSCALL32_IMPL
1323 	/*
1324 	 * 32-bit callers need to have their iovec expanded, while ensuring
1325 	 * that they can't move more than 2Gbytes of data in a single call.
1326 	 */
1327 	if (model == DATAMODEL_ILP32) {
1328 		struct iovec32 aiov32[MSG_MAXIOVLEN];
1329 		ssize32_t count32;
1330 
1331 		if (iovcnt != 0 &&
1332 		    copyin((struct iovec32 *)lmsg.msg_iov, aiov32,
1333 		    iovcnt * sizeof (struct iovec32)))
1334 			return (set_errno(EFAULT));
1335 
1336 		count32 = 0;
1337 		for (i = 0; i < iovcnt; i++) {
1338 			ssize32_t iovlen32;
1339 
1340 			iovlen32 = aiov32[i].iov_len;
1341 			count32 += iovlen32;
1342 			if (iovlen32 < 0 || count32 < 0)
1343 				return (set_errno(EINVAL));
1344 			aiov[i].iov_len = iovlen32;
1345 			aiov[i].iov_base =
1346 			    (caddr_t)(uintptr_t)aiov32[i].iov_base;
1347 		}
1348 	} else
1349 #endif /* _SYSCALL32_IMPL */
1350 	if (iovcnt != 0 &&
1351 	    copyin(lmsg.msg_iov, aiov,
1352 	    (unsigned)iovcnt * sizeof (struct iovec))) {
1353 		return (set_errno(EFAULT));
1354 	}
1355 	len = 0;
1356 	for (i = 0; i < iovcnt; i++) {
1357 		ssize_t iovlen = aiov[i].iov_len;
1358 		len += iovlen;
1359 		if (iovlen < 0 || len < 0) {
1360 			return (set_errno(EINVAL));
1361 		}
1362 	}
1363 	auio.uio_loffset = 0;
1364 	auio.uio_iov = aiov;
1365 	auio.uio_iovcnt = iovcnt;
1366 	auio.uio_resid = len;
1367 	auio.uio_segflg = UIO_USERSPACE;
1368 	auio.uio_limit = 0;
1369 
1370 	return (sendit(sock, &lmsg, &auio, flags));
1371 }
1372 
1373 ssize_t
1374 sendto(int sock, void *buffer, size_t len, int flags,
1375     struct sockaddr *name, socklen_t namelen)
1376 {
1377 	struct nmsghdr lmsg;
1378 	struct uio auio;
1379 	struct iovec aiov[1];
1380 
1381 	dprint(1, ("sendto(%d, %p, %ld, %d, %p, %d)\n",
1382 		sock, buffer, len, flags, name, namelen));
1383 
1384 	if ((ssize_t)len < 0) {
1385 		return (set_errno(EINVAL));
1386 	}
1387 
1388 	aiov[0].iov_base = buffer;
1389 	aiov[0].iov_len = len;
1390 	auio.uio_loffset = 0;
1391 	auio.uio_iov = aiov;
1392 	auio.uio_iovcnt = 1;
1393 	auio.uio_resid = len;
1394 	auio.uio_segflg = UIO_USERSPACE;
1395 	auio.uio_limit = 0;
1396 
1397 	lmsg.msg_name = (char *)name;
1398 	lmsg.msg_namelen = namelen;
1399 	lmsg.msg_control = NULL;
1400 	if (!(flags & MSG_XPG4_2)) {
1401 		/*
1402 		 * In order to be compatible with the libsocket/sockmod
1403 		 * implementation we set EOR for all send* calls.
1404 		 */
1405 		flags |= MSG_EOR;
1406 	}
1407 	return (sendit(sock, &lmsg, &auio, flags));
1408 }
1409 
1410 /*ARGSUSED3*/
1411 int
1412 getpeername(int sock, struct sockaddr *name, socklen_t *namelenp, int version)
1413 {
1414 	struct sonode *so;
1415 	int error;
1416 	socklen_t namelen;
1417 	union {
1418 		struct sockaddr_in sin;
1419 		struct sockaddr_in6 sin6;
1420 	} sin;			/* Temporary buffer, common case */
1421 	void *addr;		/* Temporary buffer, uncommon case */
1422 	socklen_t addrlen, size;
1423 
1424 	dprint(1, ("getpeername(%d, %p, %p)\n",
1425 		sock, name, namelenp));
1426 
1427 	if ((so = getsonode(sock, &error, NULL)) == NULL)
1428 		goto bad;
1429 
1430 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1431 	if (copyin(namelenp, &namelen, sizeof (namelen)) ||
1432 	    (name == NULL && namelen != 0)) {
1433 		error = EFAULT;
1434 		goto rel_out;
1435 	}
1436 	/*
1437 	 * If a connect or accept has been done, unless we're an Xnet socket,
1438 	 * the remote address has already been updated in so_faddr_sa.
1439 	 */
1440 	if (so->so_version != SOV_SOCKSTREAM && so->so_version != SOV_SOCKBSD ||
1441 	    !(so->so_state & SS_FADDR_VALID)) {
1442 		if ((error = SOP_GETPEERNAME(so)) != 0)
1443 			goto rel_out;
1444 	}
1445 
1446 	if (so->so_faddr_maxlen <= sizeof (sin)) {
1447 		size = 0;
1448 		addr = &sin;
1449 	} else {
1450 		/*
1451 		 * Allocate temporary to avoid holding so_lock across
1452 		 * copyout
1453 		 */
1454 		size = so->so_faddr_maxlen;
1455 		addr = kmem_alloc(size, KM_SLEEP);
1456 	}
1457 	/* Prevent so_faddr_sa/len from changing while accessed */
1458 	mutex_enter(&so->so_lock);
1459 	if (!(so->so_state & SS_ISCONNECTED)) {
1460 		mutex_exit(&so->so_lock);
1461 		error = ENOTCONN;
1462 		goto free_out;
1463 	}
1464 	addrlen = so->so_faddr_len;
1465 	bcopy(so->so_faddr_sa, addr, addrlen);
1466 	mutex_exit(&so->so_lock);
1467 
1468 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1469 	error = copyout_name(name, namelen, namelenp, addr,
1470 	    (so->so_state & SS_FADDR_NOXLATE) ? 0 : addrlen);
1471 free_out:
1472 	if (size != 0)
1473 		kmem_free(addr, size);
1474 rel_out:
1475 	releasef(sock);
1476 bad:	return (error != 0 ? set_errno(error) : 0);
1477 }
1478 
1479 /*ARGSUSED3*/
1480 int
1481 getsockname(int sock, struct sockaddr *name,
1482 		socklen_t *namelenp, int version)
1483 {
1484 	struct sonode *so;
1485 	int error;
1486 	socklen_t namelen;
1487 	union {
1488 		struct sockaddr_in sin;
1489 		struct sockaddr_in6 sin6;
1490 	} sin;			/* Temporary buffer, common case */
1491 	void *addr;		/* Temporary buffer, uncommon case */
1492 	socklen_t addrlen, size;
1493 
1494 	dprint(1, ("getsockname(%d, %p, %p)\n",
1495 		sock, name, namelenp));
1496 
1497 	if ((so = getsonode(sock, &error, NULL)) == NULL)
1498 		goto bad;
1499 
1500 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1501 	if (copyin(namelenp, &namelen, sizeof (namelen)) ||
1502 	    (name == NULL && namelen != 0)) {
1503 		error = EFAULT;
1504 		goto rel_out;
1505 	}
1506 
1507 	/*
1508 	 * If a bind or accept has been done, unless we're an Xnet endpoint,
1509 	 * the local address has already been updated in so_laddr_sa.
1510 	 */
1511 	if ((so->so_version != SOV_SOCKSTREAM &&
1512 	    so->so_version != SOV_SOCKBSD) ||
1513 	    !(so->so_state & SS_LADDR_VALID)) {
1514 		if ((error = SOP_GETSOCKNAME(so)) != 0)
1515 			goto rel_out;
1516 	}
1517 
1518 	if (so->so_laddr_maxlen <= sizeof (sin)) {
1519 		size = 0;
1520 		addr = &sin;
1521 	} else {
1522 		/*
1523 		 * Allocate temporary to avoid holding so_lock across
1524 		 * copyout
1525 		 */
1526 		size = so->so_laddr_maxlen;
1527 		addr = kmem_alloc(size, KM_SLEEP);
1528 	}
1529 	/* Prevent so_laddr_sa/len from changing while accessed */
1530 	mutex_enter(&so->so_lock);
1531 	addrlen = so->so_laddr_len;
1532 	bcopy(so->so_laddr_sa, addr, addrlen);
1533 	mutex_exit(&so->so_lock);
1534 
1535 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1536 	error = copyout_name(name, namelen, namelenp,
1537 	    addr, addrlen);
1538 	if (size != 0)
1539 		kmem_free(addr, size);
1540 rel_out:
1541 	releasef(sock);
1542 bad:	return (error != 0 ? set_errno(error) : 0);
1543 }
1544 
1545 /*ARGSUSED5*/
1546 int
1547 getsockopt(int sock,
1548 	int level,
1549 	int option_name,
1550 	void *option_value,
1551 	socklen_t *option_lenp,
1552 	int version)
1553 {
1554 	struct sonode *so;
1555 	socklen_t optlen, optlen_res;
1556 	void *optval;
1557 	int error;
1558 
1559 	dprint(1, ("getsockopt(%d, %d, %d, %p, %p)\n",
1560 		sock, level, option_name, option_value, option_lenp));
1561 
1562 	if ((so = getsonode(sock, &error, NULL)) == NULL)
1563 		return (set_errno(error));
1564 
1565 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1566 	if (copyin(option_lenp, &optlen, sizeof (optlen))) {
1567 		releasef(sock);
1568 		return (set_errno(EFAULT));
1569 	}
1570 	/*
1571 	 * Verify that the length is not excessive to prevent
1572 	 * an application from consuming all of kernel memory.
1573 	 */
1574 	if (optlen > SO_MAXARGSIZE) {
1575 		error = EINVAL;
1576 		releasef(sock);
1577 		return (set_errno(error));
1578 	}
1579 	optval = kmem_alloc(optlen, KM_SLEEP);
1580 	optlen_res = optlen;
1581 	error = SOP_GETSOCKOPT(so, level, option_name, optval,
1582 	    &optlen_res, (version != SOV_XPG4_2) ? 0 : _SOGETSOCKOPT_XPG4_2);
1583 	releasef(sock);
1584 	if (error) {
1585 		kmem_free(optval, optlen);
1586 		return (set_errno(error));
1587 	}
1588 	error = copyout_arg(option_value, optlen, option_lenp,
1589 	    optval, optlen_res);
1590 	kmem_free(optval, optlen);
1591 	if (error)
1592 		return (set_errno(error));
1593 	return (0);
1594 }
1595 
1596 /*ARGSUSED5*/
1597 int
1598 setsockopt(int sock,
1599 	int level,
1600 	int option_name,
1601 	void *option_value,
1602 	socklen_t option_len,
1603 	int version)
1604 {
1605 	struct sonode *so;
1606 	intptr_t buffer[2];
1607 	void *optval = NULL;
1608 	int error;
1609 
1610 	dprint(1, ("setsockopt(%d, %d, %d, %p, %d)\n",
1611 		sock, level, option_name, option_value, option_len));
1612 
1613 	if ((so = getsonode(sock, &error, NULL)) == NULL)
1614 		return (set_errno(error));
1615 
1616 	if (option_value != NULL) {
1617 		if (option_len != 0) {
1618 			/*
1619 			 * Verify that the length is not excessive to prevent
1620 			 * an application from consuming all of kernel memory.
1621 			 */
1622 			if (option_len > SO_MAXARGSIZE) {
1623 				error = EINVAL;
1624 				goto done2;
1625 			}
1626 			optval = option_len <= sizeof (buffer) ?
1627 			    &buffer : kmem_alloc((size_t)option_len, KM_SLEEP);
1628 			ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1629 			if (copyin(option_value, optval, (size_t)option_len)) {
1630 				error = EFAULT;
1631 				goto done1;
1632 			}
1633 		}
1634 	} else
1635 		option_len = 0;
1636 
1637 	error = SOP_SETSOCKOPT(so, level, option_name, optval,
1638 	    (t_uscalar_t)option_len);
1639 done1:
1640 	if (optval != buffer)
1641 		kmem_free(optval, (size_t)option_len);
1642 done2:
1643 	releasef(sock);
1644 	if (error)
1645 		return (set_errno(error));
1646 	return (0);
1647 }
1648 
1649 /*
1650  * Add config info when devpath is non-NULL; delete info when devpath is NULL.
1651  * devpath is a user address.
1652  */
1653 int
1654 sockconfig(int domain, int type, int protocol, char *devpath)
1655 {
1656 	char *kdevpath;		/* Copied in devpath string */
1657 	size_t kdevpathlen;
1658 	int error = 0;
1659 
1660 	dprint(1, ("sockconfig(%d, %d, %d, %p)\n",
1661 		domain, type, protocol, devpath));
1662 
1663 	if (secpolicy_net_config(CRED(), B_FALSE) != 0)
1664 		return (set_errno(EPERM));
1665 
1666 	if (devpath == NULL) {
1667 		/* Deleting an entry */
1668 		kdevpath = NULL;
1669 		kdevpathlen = 0;
1670 	} else {
1671 		/*
1672 		 * Adding an entry.
1673 		 * Copyin the devpath.
1674 		 * This also makes it possible to check for too long pathnames.
1675 		 * Compress the space needed for the devpath before passing it
1676 		 * to soconfig - soconfig will store the string until
1677 		 * the configuration is removed.
1678 		 */
1679 		char *buf;
1680 
1681 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1682 		if ((error = copyinstr(devpath, buf, MAXPATHLEN,
1683 		    &kdevpathlen)) != 0) {
1684 			kmem_free(buf, MAXPATHLEN);
1685 			goto done;
1686 		}
1687 
1688 		kdevpath = kmem_alloc(kdevpathlen, KM_SLEEP);
1689 		bcopy(buf, kdevpath, kdevpathlen);
1690 		kdevpath[kdevpathlen - 1] = '\0';
1691 
1692 		kmem_free(buf, MAXPATHLEN);
1693 	}
1694 	error = soconfig(domain, type, protocol, kdevpath, (int)kdevpathlen);
1695 done:
1696 	if (error) {
1697 		eprintline(error);
1698 		return (set_errno(error));
1699 	}
1700 	return (0);
1701 }
1702 
1703 
1704 /*
1705  * Sendfile is implemented through two schemes, direct I/O or by
1706  * caching in the filesystem page cache. We cache the input file by
1707  * default and use direct I/O only if sendfile_max_size is set
1708  * appropriately as explained below. Note that this logic is consistent
1709  * with other filesystems where caching is turned on by default
1710  * unless explicitly turned off by using the DIRECTIO ioctl.
1711  *
1712  * We choose a slightly different scheme here. One can turn off
1713  * caching by setting sendfile_max_size to 0. One can also enable
1714  * caching of files <= sendfile_max_size by setting sendfile_max_size
1715  * to an appropriate value. By default sendfile_max_size is set to the
1716  * maximum value so that all files are cached. In future, we may provide
1717  * better interfaces for caching the file.
1718  *
1719  * Sendfile through Direct I/O (Zero copy)
1720  * --------------------------------------
1721  *
1722  * As disks are normally slower than the network, we can't have a
1723  * single thread that reads the disk and writes to the network. We
1724  * need to have parallelism. This is done by having the sendfile
1725  * thread create another thread that reads from the filesystem
1726  * and queues it for network processing. In this scheme, the data
1727  * is never copied anywhere i.e it is zero copy unlike the other
1728  * scheme.
1729  *
1730  * We have a sendfile queue (snfq) where each sendfile
1731  * request (snf_req_t) is queued for processing by a thread. Number
1732  * of threads is dynamically allocated and they exit if they are idling
1733  * beyond a specified amount of time. When each request (snf_req_t) is
1734  * processed by a thread, it produces a number of mblk_t structures to
1735  * be consumed by the sendfile thread. snf_deque and snf_enque are
1736  * used for consuming and producing mblks. Size of the filesystem
1737  * read is determined by the tuneable (sendfile_read_size). A single
1738  * mblk holds sendfile_read_size worth of data (except the last
1739  * read of the file) which is sent down as a whole to the network.
1740  * sendfile_read_size is set to 1 MB as this seems to be the optimal
1741  * value for the UFS filesystem backed by a striped storage array.
1742  *
1743  * Synchronisation between read (producer) and write (consumer) threads.
1744  * --------------------------------------------------------------------
1745  *
1746  * sr_lock protects sr_ib_head and sr_ib_tail. The lock is held while
1747  * adding and deleting items in this list. Error can happen anytime
1748  * during read or write. There could be unprocessed mblks in the
1749  * sr_ib_XXX list when a read or write error occurs. Whenever error
1750  * is encountered, we need two things to happen :
1751  *
1752  * a) One of the threads need to clean the mblks.
1753  * b) When one thread encounters an error, the other should stop.
1754  *
1755  * For (a), we don't want to penalise the reader thread as it could do
1756  * some useful work processing other requests. For (b), the error can
1757  * be detected by examining sr_read_error or sr_write_error.
1758  * sr_lock protects sr_read_error and sr_write_error. If both reader and
1759  * writer encounters error, we need to report the write error back to
1760  * the application as that's what would have happened if the operations
1761  * were done sequentially. With this in mind, following should work :
1762  *
1763  * 	- Check for errors before read or write.
1764  *	- If the reader encounters error, set the error in sr_read_error.
1765  *	  Check sr_write_error, if it is set, send cv_signal as it is
1766  *	  waiting for reader to complete. If it is not set, the writer
1767  *	  is either running sinking data to the network or blocked
1768  *        because of flow control. For handling the latter case, we
1769  *	  always send a signal. In any case, it will examine sr_read_error
1770  *	  and return. sr_read_error is marked with SR_READ_DONE to tell
1771  *	  the writer that the reader is done in all the cases.
1772  *	- If the writer encounters error, set the error in sr_write_error.
1773  *	  The reader thread is either blocked because of flow control or
1774  *	  running reading data from the disk. For the former, we need to
1775  *	  wakeup the thread. Again to keep it simple, we always wake up
1776  *	  the reader thread. Then, wait for the read thread to complete
1777  *	  if it is not done yet. Cleanup and return.
1778  *
1779  * High and low water marks for the read thread.
1780  * --------------------------------------------
1781  *
1782  * If sendfile() is used to send data over a slow network, we need to
1783  * make sure that the read thread does not produce data at a faster
1784  * rate than the network. This can happen if the disk is faster than
1785  * the network. In such a case, we don't want to build a very large queue.
1786  * But we would still like to get all of the network throughput possible.
1787  * This implies that network should never block waiting for data.
1788  * As there are lot of disk throughput/network throughput combinations
1789  * possible, it is difficult to come up with an accurate number.
1790  * A typical 10K RPM disk has a max seek latency 17ms and rotational
1791  * latency of 3ms for reading a disk block. Thus, the total latency to
1792  * initiate a new read, transfer data from the disk and queue for
1793  * transmission would take about a max of 25ms. Todays max transfer rate
1794  * for network is 100MB/sec. If the thread is blocked because of flow
1795  * control, it would take 25ms to get new data ready for transmission.
1796  * We have to make sure that network is not idling, while we are initiating
1797  * new transfers. So, at 100MB/sec, to keep network busy we would need
1798  * 2.5MB of data. Roundig off, we keep the low water mark to be 3MB of data.
1799  * We need to pick a high water mark so that the woken up thread would
1800  * do considerable work before blocking again to prevent thrashing. Currently,
1801  * we pick this to be 10 times that of the low water mark.
1802  *
1803  * Sendfile with segmap caching (One copy from page cache to mblks).
1804  * ----------------------------------------------------------------
1805  *
1806  * We use the segmap cache for caching the file, if the size of file
1807  * is <= sendfile_max_size. In this case we don't use threads as VM
1808  * is reasonably fast enough to keep up with the network. If the underlying
1809  * transport allows, we call segmap_getmapflt() to map MAXBSIZE (8K) worth
1810  * of data into segmap space, and use the virtual address from segmap
1811  * directly through desballoc() to avoid copy. Once the transport is done
1812  * with the data, the mapping will be released through segmap_release()
1813  * called by the call-back routine.
1814  *
1815  * If zero-copy is not allowed by the transport, we simply call VOP_READ()
1816  * to copy the data from the filesystem into our temporary network buffer.
1817  *
1818  * To disable caching, set sendfile_max_size to 0.
1819  */
1820 
1821 uint_t sendfile_read_size = 1024 * 1024;
1822 #define	SENDFILE_REQ_LOWAT	3 * 1024 * 1024
1823 uint_t sendfile_req_lowat = SENDFILE_REQ_LOWAT;
1824 uint_t sendfile_req_hiwat = 10 * SENDFILE_REQ_LOWAT;
1825 struct sendfile_stats sf_stats;
1826 struct sendfile_queue *snfq;
1827 clock_t snfq_timeout;
1828 off64_t sendfile_max_size;
1829 
1830 static void snf_enque(snf_req_t *, mblk_t *);
1831 static mblk_t *snf_deque(snf_req_t *);
1832 
1833 void
1834 sendfile_init(void)
1835 {
1836 	snfq = kmem_zalloc(sizeof (struct sendfile_queue), KM_SLEEP);
1837 
1838 	mutex_init(&snfq->snfq_lock, NULL, MUTEX_DEFAULT, NULL);
1839 	cv_init(&snfq->snfq_cv, NULL, CV_DEFAULT, NULL);
1840 	snfq->snfq_max_threads = max_ncpus;
1841 	snfq_timeout = SNFQ_TIMEOUT;
1842 	/* Cache all files by default. */
1843 	sendfile_max_size = MAXOFFSET_T;
1844 }
1845 
1846 /*
1847  * Queues a mblk_t for network processing.
1848  */
1849 static void
1850 snf_enque(snf_req_t *sr, mblk_t *mp)
1851 {
1852 	mp->b_next = NULL;
1853 	mutex_enter(&sr->sr_lock);
1854 	if (sr->sr_mp_head == NULL) {
1855 		sr->sr_mp_head = sr->sr_mp_tail = mp;
1856 		cv_signal(&sr->sr_cv);
1857 	} else {
1858 		sr->sr_mp_tail->b_next = mp;
1859 		sr->sr_mp_tail = mp;
1860 	}
1861 	sr->sr_qlen += MBLKL(mp);
1862 	while ((sr->sr_qlen > sr->sr_hiwat) &&
1863 	    (sr->sr_write_error == 0)) {
1864 		sf_stats.ss_full_waits++;
1865 		cv_wait(&sr->sr_cv, &sr->sr_lock);
1866 	}
1867 	mutex_exit(&sr->sr_lock);
1868 }
1869 
1870 /*
1871  * De-queues a mblk_t for network processing.
1872  */
1873 static mblk_t *
1874 snf_deque(snf_req_t *sr)
1875 {
1876 	mblk_t *mp;
1877 
1878 	mutex_enter(&sr->sr_lock);
1879 	/*
1880 	 * If we have encountered an error on read or read is
1881 	 * completed and no more mblks, return NULL.
1882 	 * We need to check for NULL sr_mp_head also as
1883 	 * the reads could have completed and there is
1884 	 * nothing more to come.
1885 	 */
1886 	if (((sr->sr_read_error & ~SR_READ_DONE) != 0) ||
1887 	    ((sr->sr_read_error & SR_READ_DONE) &&
1888 	    sr->sr_mp_head == NULL)) {
1889 		mutex_exit(&sr->sr_lock);
1890 		return (NULL);
1891 	}
1892 	/*
1893 	 * To start with neither SR_READ_DONE is marked nor
1894 	 * the error is set. When we wake up from cv_wait,
1895 	 * following are the possibilities :
1896 	 *
1897 	 *	a) sr_read_error is zero and mblks are queued.
1898 	 *	b) sr_read_error is set to SR_READ_DONE
1899 	 *	   and mblks are queued.
1900 	 *	c) sr_read_error is set to SR_READ_DONE
1901 	 *	   and no mblks.
1902 	 *	d) sr_read_error is set to some error other
1903 	 *	   than SR_READ_DONE.
1904 	 */
1905 
1906 	while ((sr->sr_read_error == 0) && (sr->sr_mp_head == NULL)) {
1907 		sf_stats.ss_empty_waits++;
1908 		cv_wait(&sr->sr_cv, &sr->sr_lock);
1909 	}
1910 	/* Handle (a) and (b) first  - the normal case. */
1911 	if (((sr->sr_read_error & ~SR_READ_DONE) == 0) &&
1912 	    (sr->sr_mp_head != NULL)) {
1913 		mp = sr->sr_mp_head;
1914 		sr->sr_mp_head = mp->b_next;
1915 		sr->sr_qlen -= MBLKL(mp);
1916 		if (sr->sr_qlen < sr->sr_lowat)
1917 			cv_signal(&sr->sr_cv);
1918 		mutex_exit(&sr->sr_lock);
1919 		mp->b_next = NULL;
1920 		return (mp);
1921 	}
1922 	/* Handle (c) and (d). */
1923 	mutex_exit(&sr->sr_lock);
1924 	return (NULL);
1925 }
1926 
1927 /*
1928  * Reads data from the filesystem and queues it for network processing.
1929  */
1930 void
1931 snf_async_read(snf_req_t *sr)
1932 {
1933 	size_t iosize;
1934 	u_offset_t fileoff;
1935 	u_offset_t size;
1936 	int ret_size;
1937 	int error;
1938 	file_t *fp;
1939 	mblk_t *mp;
1940 
1941 	fp = sr->sr_fp;
1942 	size = sr->sr_file_size;
1943 	fileoff = sr->sr_file_off;
1944 
1945 	/*
1946 	 * Ignore the error for filesystems that doesn't support DIRECTIO.
1947 	 */
1948 	(void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_ON, 0,
1949 	    kcred, NULL);
1950 
1951 	while ((size != 0) && (sr->sr_write_error == 0)) {
1952 
1953 		iosize = (int)MIN(sr->sr_maxpsz, size);
1954 
1955 		if ((mp = allocb(iosize, BPRI_MED)) == NULL) {
1956 			error = EAGAIN;
1957 			break;
1958 		}
1959 		ret_size = soreadfile(fp, mp->b_rptr, fileoff, &error, iosize);
1960 
1961 		/* Error or Reached EOF ? */
1962 		if ((error != 0) || (ret_size == 0)) {
1963 			freeb(mp);
1964 			break;
1965 		}
1966 		mp->b_wptr = mp->b_rptr + ret_size;
1967 
1968 		snf_enque(sr, mp);
1969 		size -= ret_size;
1970 		fileoff += ret_size;
1971 	}
1972 	(void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_OFF, 0,
1973 	    kcred, NULL);
1974 	mutex_enter(&sr->sr_lock);
1975 	sr->sr_read_error = error;
1976 	sr->sr_read_error |= SR_READ_DONE;
1977 	cv_signal(&sr->sr_cv);
1978 	mutex_exit(&sr->sr_lock);
1979 }
1980 
1981 void
1982 snf_async_thread(void)
1983 {
1984 	snf_req_t *sr;
1985 	callb_cpr_t cprinfo;
1986 	clock_t time_left = 1;
1987 	clock_t now;
1988 
1989 	CALLB_CPR_INIT(&cprinfo, &snfq->snfq_lock, callb_generic_cpr, "snfq");
1990 
1991 	mutex_enter(&snfq->snfq_lock);
1992 	for (;;) {
1993 		/*
1994 		 * If we didn't find a entry, then block until woken up
1995 		 * again and then look through the queues again.
1996 		 */
1997 		while ((sr = snfq->snfq_req_head) == NULL) {
1998 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
1999 			if (time_left <= 0) {
2000 				snfq->snfq_svc_threads--;
2001 				CALLB_CPR_EXIT(&cprinfo);
2002 				thread_exit();
2003 				/* NOTREACHED */
2004 			}
2005 			snfq->snfq_idle_cnt++;
2006 
2007 			time_to_wait(&now, snfq_timeout);
2008 			time_left = cv_timedwait(&snfq->snfq_cv,
2009 			    &snfq->snfq_lock, now);
2010 			snfq->snfq_idle_cnt--;
2011 
2012 			CALLB_CPR_SAFE_END(&cprinfo, &snfq->snfq_lock);
2013 		}
2014 		snfq->snfq_req_head = sr->sr_next;
2015 		snfq->snfq_req_cnt--;
2016 		mutex_exit(&snfq->snfq_lock);
2017 		snf_async_read(sr);
2018 		mutex_enter(&snfq->snfq_lock);
2019 	}
2020 }
2021 
2022 
2023 snf_req_t *
2024 create_thread(int operation, struct vnode *vp, file_t *fp,
2025     u_offset_t fileoff, u_offset_t size)
2026 {
2027 	snf_req_t *sr;
2028 	stdata_t *stp;
2029 
2030 	sr = (snf_req_t *)kmem_zalloc(sizeof (snf_req_t), KM_SLEEP);
2031 
2032 	sr->sr_vp = vp;
2033 	sr->sr_fp = fp;
2034 	stp = vp->v_stream;
2035 
2036 	/*
2037 	 * store sd_qn_maxpsz into sr_maxpsz while we have stream head.
2038 	 * stream might be closed before thread returns from snf_async_read.
2039 	 */
2040 	if (stp->sd_qn_maxpsz > 0) {
2041 		sr->sr_maxpsz = MIN(MAXBSIZE, stp->sd_qn_maxpsz);
2042 	} else {
2043 		sr->sr_maxpsz = MAXBSIZE;
2044 	}
2045 
2046 	sr->sr_operation = operation;
2047 	sr->sr_file_off = fileoff;
2048 	sr->sr_file_size = size;
2049 	sr->sr_hiwat = sendfile_req_hiwat;
2050 	sr->sr_lowat = sendfile_req_lowat;
2051 	mutex_init(&sr->sr_lock, NULL, MUTEX_DEFAULT, NULL);
2052 	cv_init(&sr->sr_cv, NULL, CV_DEFAULT, NULL);
2053 	/*
2054 	 * See whether we need another thread for servicing this
2055 	 * request. If there are already enough requests queued
2056 	 * for the threads, create one if not exceeding
2057 	 * snfq_max_threads.
2058 	 */
2059 	mutex_enter(&snfq->snfq_lock);
2060 	if (snfq->snfq_req_cnt >= snfq->snfq_idle_cnt &&
2061 	    snfq->snfq_svc_threads < snfq->snfq_max_threads) {
2062 		(void) thread_create(NULL, 0, &snf_async_thread, 0, 0, &p0,
2063 		    TS_RUN, minclsyspri);
2064 		snfq->snfq_svc_threads++;
2065 	}
2066 	if (snfq->snfq_req_head == NULL) {
2067 		snfq->snfq_req_head = snfq->snfq_req_tail = sr;
2068 		cv_signal(&snfq->snfq_cv);
2069 	} else {
2070 		snfq->snfq_req_tail->sr_next = sr;
2071 		snfq->snfq_req_tail = sr;
2072 	}
2073 	snfq->snfq_req_cnt++;
2074 	mutex_exit(&snfq->snfq_lock);
2075 	return (sr);
2076 }
2077 
2078 int
2079 snf_direct_io(file_t *fp, file_t *rfp, u_offset_t fileoff, u_offset_t size,
2080     ssize_t *count)
2081 {
2082 	snf_req_t *sr;
2083 	mblk_t *mp;
2084 	int iosize;
2085 	int error = 0;
2086 	short fflag;
2087 	struct vnode *vp;
2088 	int ksize;
2089 
2090 	ksize = 0;
2091 	*count = 0;
2092 
2093 	vp = fp->f_vnode;
2094 	fflag = fp->f_flag;
2095 	if ((sr = create_thread(READ_OP, vp, rfp, fileoff, size)) == NULL)
2096 		return (EAGAIN);
2097 
2098 	/*
2099 	 * We check for read error in snf_deque. It has to check
2100 	 * for successful READ_DONE and return NULL, and we might
2101 	 * as well make an additional check there.
2102 	 */
2103 	while ((mp = snf_deque(sr)) != NULL) {
2104 
2105 		if (ISSIG(curthread, JUSTLOOKING)) {
2106 			freeb(mp);
2107 			error = EINTR;
2108 			break;
2109 		}
2110 		iosize = MBLKL(mp);
2111 
2112 		if ((error = kstrwritemp(vp, mp, fflag)) != 0) {
2113 			freeb(mp);
2114 			break;
2115 		}
2116 		ksize += iosize;
2117 	}
2118 	*count = ksize;
2119 
2120 	mutex_enter(&sr->sr_lock);
2121 	sr->sr_write_error = error;
2122 	/* Look at the big comments on why we cv_signal here. */
2123 	cv_signal(&sr->sr_cv);
2124 
2125 	/* Wait for the reader to complete always. */
2126 	while (!(sr->sr_read_error & SR_READ_DONE)) {
2127 		cv_wait(&sr->sr_cv, &sr->sr_lock);
2128 	}
2129 	/* If there is no write error, check for read error. */
2130 	if (error == 0)
2131 		error = (sr->sr_read_error & ~SR_READ_DONE);
2132 
2133 	if (error != 0) {
2134 		mblk_t *next_mp;
2135 
2136 		mp = sr->sr_mp_head;
2137 		while (mp != NULL) {
2138 			next_mp = mp->b_next;
2139 			mp->b_next = NULL;
2140 			freeb(mp);
2141 			mp = next_mp;
2142 		}
2143 	}
2144 	mutex_exit(&sr->sr_lock);
2145 	kmem_free(sr, sizeof (snf_req_t));
2146 	return (error);
2147 }
2148 
2149 typedef struct {
2150 	frtn_t		snfi_frtn;
2151 	caddr_t		snfi_base;
2152 	uint_t		snfi_mapoff;
2153 	size_t		snfi_len;
2154 	vnode_t		*snfi_vp;
2155 } snf_smap_desbinfo;
2156 
2157 /*
2158  * The callback function when the last ref of the mblk is dropped,
2159  * normally occurs when TCP receives the ack. But it can be the driver
2160  * too due to lazy reclaim.
2161  */
2162 void
2163 snf_smap_desbfree(snf_smap_desbinfo *snfi)
2164 {
2165 	if (!segmap_kpm) {
2166 		/*
2167 		 * We don't need to call segmap_fault(F_SOFTUNLOCK) for
2168 		 * segmap_kpm as long as the latter never falls back to
2169 		 * "use_segmap_range". (See segmap_getmapflt().)
2170 		 *
2171 		 * Using S_OTHER saves an redundant hat_setref() in
2172 		 * segmap_unlock()
2173 		 */
2174 		(void) segmap_fault(kas.a_hat, segkmap,
2175 		    (caddr_t)(uintptr_t)(((uintptr_t)snfi->snfi_base +
2176 		    snfi->snfi_mapoff) & PAGEMASK), snfi->snfi_len,
2177 		    F_SOFTUNLOCK, S_OTHER);
2178 	}
2179 	(void) segmap_release(segkmap, snfi->snfi_base, SM_DONTNEED);
2180 	VN_RELE(snfi->snfi_vp);
2181 	kmem_free(snfi, sizeof (*snfi));
2182 }
2183 
2184 /*
2185  * Use segmap instead of bcopy to send down a chain of desballoca'ed, mblks.
2186  * Each mblk contains a segmap slot of no more than MAXBSIZE. The total
2187  * length of a chain is no more than sd_qn_maxpsz.
2188  *
2189  * At the end of the whole sendfile() operation, we wait till the data from
2190  * the last mblk is ack'ed by the transport before returning so that the
2191  * caller of sendfile() can safely modify the file content.
2192  */
2193 int
2194 snf_segmap(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t size,
2195     uint_t maxpsz, ssize_t *count, boolean_t nowait)
2196 {
2197 	caddr_t base;
2198 	int mapoff;
2199 	vnode_t *vp;
2200 	mblk_t *mp, *mp1;
2201 	int iosize, iosize1;
2202 	int error;
2203 	short fflag;
2204 	int ksize;
2205 	snf_smap_desbinfo *snfi;
2206 	struct vattr va;
2207 	boolean_t dowait = B_FALSE;
2208 
2209 	vp = fp->f_vnode;
2210 	fflag = fp->f_flag;
2211 	ksize = 0;
2212 	for (;;) {
2213 		if (ISSIG(curthread, JUSTLOOKING)) {
2214 			error = EINTR;
2215 			break;
2216 		}
2217 		iosize = 0;
2218 		mp = NULL;
2219 		do {
2220 			mapoff = fileoff & MAXBOFFSET;
2221 			iosize1 = MAXBSIZE - mapoff;
2222 			if (iosize1 > size)
2223 				iosize1 = size;
2224 			/*
2225 			 * we don't forcefault because we'll call
2226 			 * segmap_fault(F_SOFTLOCK) next.
2227 			 *
2228 			 * S_READ will get the ref bit set (by either
2229 			 * segmap_getmapflt() or segmap_fault()) and page
2230 			 * shared locked.
2231 			 */
2232 			base = segmap_getmapflt(segkmap, fvp, fileoff, iosize1,
2233 			    segmap_kpm ? SM_FAULT : 0, S_READ);
2234 
2235 			snfi = kmem_alloc(sizeof (*snfi), KM_SLEEP);
2236 			snfi->snfi_len = (size_t)roundup(mapoff+iosize1,
2237 			    PAGESIZE)- (mapoff & PAGEMASK);
2238 			/*
2239 			 * We must call segmap_fault() even for segmap_kpm
2240 			 * because that's how error gets returned.
2241 			 * (segmap_getmapflt() never fails but segmap_fault()
2242 			 * does.)
2243 			 */
2244 			if (segmap_fault(kas.a_hat, segkmap,
2245 			    (caddr_t)(uintptr_t)(((uintptr_t)base + mapoff) &
2246 			    PAGEMASK), snfi->snfi_len, F_SOFTLOCK,
2247 			    S_READ) != 0) {
2248 				(void) segmap_release(segkmap, base, 0);
2249 				kmem_free(snfi, sizeof (*snfi));
2250 				freemsg(mp);
2251 				error = EIO;
2252 				goto out;
2253 			}
2254 			snfi->snfi_frtn.free_func = snf_smap_desbfree;
2255 			snfi->snfi_frtn.free_arg = (caddr_t)snfi;
2256 			snfi->snfi_base = base;
2257 			snfi->snfi_mapoff = mapoff;
2258 			mp1 = esballoca((uchar_t *)base + mapoff,
2259 			    iosize1, BPRI_HI, &snfi->snfi_frtn);
2260 
2261 			if (mp1 == NULL) {
2262 				(void) segmap_fault(kas.a_hat, segkmap,
2263 				    (caddr_t)(uintptr_t)(((uintptr_t)base +
2264 				    mapoff) & PAGEMASK), snfi->snfi_len,
2265 				    F_SOFTUNLOCK, S_OTHER);
2266 				(void) segmap_release(segkmap, base, 0);
2267 				kmem_free(snfi, sizeof (*snfi));
2268 				freemsg(mp);
2269 				error = EAGAIN;
2270 				goto out;
2271 			}
2272 			VN_HOLD(fvp);
2273 			snfi->snfi_vp = fvp;
2274 			mp1->b_wptr += iosize1;
2275 
2276 			/* Mark this dblk with the zero-copy flag */
2277 			mp1->b_datap->db_struioflag |= STRUIO_ZC;
2278 			if (mp == NULL)
2279 				mp = mp1;
2280 			else
2281 				linkb(mp, mp1);
2282 			iosize += iosize1;
2283 			fileoff += iosize1;
2284 			size -= iosize1;
2285 		} while (iosize < maxpsz && size != 0);
2286 
2287 		if (size == 0 && !nowait) {
2288 			ASSERT(!dowait);
2289 			dowait = B_TRUE;
2290 			mp1->b_datap->db_struioflag |= STRUIO_ZCNOTIFY;
2291 		}
2292 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2293 		if ((error = kstrwritemp(vp, mp, fflag)) != 0) {
2294 			*count = ksize;
2295 			freemsg(mp);
2296 			return (error);
2297 		}
2298 		ksize += iosize;
2299 		if (size == 0)
2300 			goto done;
2301 
2302 		(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2303 		va.va_mask = AT_SIZE;
2304 		error = VOP_GETATTR(fvp, &va, 0, kcred);
2305 		if (error)
2306 			break;
2307 		/* Read as much as possible. */
2308 		if (fileoff >= va.va_size)
2309 			break;
2310 		if (size + fileoff > va.va_size)
2311 			size = va.va_size - fileoff;
2312 	}
2313 out:
2314 	VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2315 done:
2316 	*count = ksize;
2317 	if (dowait) {
2318 		stdata_t *stp;
2319 
2320 		stp = vp->v_stream;
2321 		mutex_enter(&stp->sd_lock);
2322 		while (!(stp->sd_flag & STZCNOTIFY)) {
2323 			if (cv_wait_sig(&stp->sd_zcopy_wait,
2324 				&stp->sd_lock) == 0) {
2325 				error = EINTR;
2326 				break;
2327 			}
2328 		}
2329 		stp->sd_flag &= ~STZCNOTIFY;
2330 		mutex_exit(&stp->sd_lock);
2331 	}
2332 	return (error);
2333 }
2334 
2335 int
2336 snf_cache(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t size,
2337     uint_t maxpsz, ssize_t *count)
2338 {
2339 	struct vnode *vp;
2340 	mblk_t *mp;
2341 	int iosize;
2342 	int error;
2343 	short fflag;
2344 	int ksize;
2345 	int ioflag;
2346 	struct uio auio;
2347 	struct iovec aiov;
2348 	struct vattr va;
2349 
2350 	vp = fp->f_vnode;
2351 	fflag = fp->f_flag;
2352 	ksize = 0;
2353 	auio.uio_iov = &aiov;
2354 	auio.uio_iovcnt = 1;
2355 	auio.uio_segflg = UIO_SYSSPACE;
2356 	auio.uio_llimit = MAXOFFSET_T;
2357 	auio.uio_fmode = fflag;
2358 	auio.uio_extflg = UIO_COPY_CACHED;
2359 	ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
2360 	/* If read sync is not asked for, filter sync flags */
2361 	if ((ioflag & FRSYNC) == 0)
2362 		ioflag &= ~(FSYNC|FDSYNC);
2363 	for (;;) {
2364 		if (ISSIG(curthread, JUSTLOOKING)) {
2365 			error = EINTR;
2366 			break;
2367 		}
2368 		iosize = (int)MIN(maxpsz, size);
2369 		if ((mp = allocb(iosize, BPRI_MED)) == NULL) {
2370 			error = EAGAIN;
2371 			break;
2372 		}
2373 		aiov.iov_base = (caddr_t)mp->b_rptr;
2374 		aiov.iov_len = iosize;
2375 		auio.uio_loffset = fileoff;
2376 		auio.uio_resid = iosize;
2377 
2378 		error = VOP_READ(fvp, &auio, ioflag, fp->f_cred, NULL);
2379 		iosize -= auio.uio_resid;
2380 
2381 		if (error == EINTR && iosize != 0)
2382 			error = 0;
2383 
2384 		if (error != 0 || iosize == 0) {
2385 			freeb(mp);
2386 			break;
2387 		}
2388 		mp->b_wptr = mp->b_rptr + iosize;
2389 
2390 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2391 		if ((error = kstrwritemp(vp, mp, fflag)) != 0) {
2392 			*count = ksize;
2393 			freeb(mp);
2394 			return (error);
2395 		}
2396 		ksize += iosize;
2397 		size -= iosize;
2398 		if (size == 0)
2399 			goto done;
2400 
2401 		fileoff += iosize;
2402 		(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2403 		va.va_mask = AT_SIZE;
2404 		error = VOP_GETATTR(fvp, &va, 0, kcred);
2405 		if (error)
2406 			break;
2407 		/* Read as much as possible. */
2408 		if (fileoff >= va.va_size)
2409 			size = 0;
2410 		else if (size + fileoff > va.va_size)
2411 			size = va.va_size - fileoff;
2412 	}
2413 	VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2414 done:
2415 	*count = ksize;
2416 	return (error);
2417 }
2418 
2419 #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
2420 /*
2421  * Largefile support for 32 bit applications only.
2422  */
2423 int
2424 sosendfile64(file_t *fp, file_t *rfp, const struct ksendfilevec64 *sfv,
2425     ssize32_t *count32)
2426 {
2427 	ssize32_t sfv_len;
2428 	u_offset_t sfv_off, va_size;
2429 	struct vnode *vp, *fvp, *realvp;
2430 	struct vattr va;
2431 	stdata_t *stp;
2432 	ssize_t count = 0;
2433 	int error = 0;
2434 	boolean_t dozcopy = B_FALSE;
2435 	uint_t maxpsz;
2436 
2437 	sfv_len = (ssize32_t)sfv->sfv_len;
2438 	if (sfv_len < 0) {
2439 		error = EINVAL;
2440 		goto out;
2441 	}
2442 
2443 	if (sfv_len == 0) goto out;
2444 
2445 	sfv_off = (u_offset_t)sfv->sfv_off;
2446 
2447 	/* Same checks as in pread */
2448 	if (sfv_off > MAXOFFSET_T) {
2449 		error = EINVAL;
2450 		goto out;
2451 	}
2452 	if (sfv_off + sfv_len > MAXOFFSET_T)
2453 		sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
2454 
2455 	/*
2456 	 * There are no more checks on sfv_len. So, we cast it to
2457 	 * u_offset_t and share the snf_direct_io/snf_cache code between
2458 	 * 32 bit and 64 bit.
2459 	 *
2460 	 * TODO: should do nbl_need_check() like read()?
2461 	 */
2462 	if (sfv_len > sendfile_max_size) {
2463 		sf_stats.ss_file_not_cached++;
2464 		error = snf_direct_io(fp, rfp, sfv_off, (u_offset_t)sfv_len,
2465 		    &count);
2466 		goto out;
2467 	}
2468 	fvp = rfp->f_vnode;
2469 	if (VOP_REALVP(fvp, &realvp) == 0)
2470 		fvp = realvp;
2471 	/*
2472 	 * Grab the lock as a reader to prevent the file size
2473 	 * from changing underneath.
2474 	 */
2475 	(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2476 	va.va_mask = AT_SIZE;
2477 	error = VOP_GETATTR(fvp, &va, 0, kcred);
2478 	va_size = va.va_size;
2479 	if ((error != 0) || (va_size == 0) || (sfv_off >= va_size)) {
2480 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2481 		goto out;
2482 	}
2483 	/* Read as much as possible. */
2484 	if (sfv_off + sfv_len > va_size)
2485 		sfv_len = va_size - sfv_off;
2486 
2487 	vp = fp->f_vnode;
2488 	stp = vp->v_stream;
2489 	if (stp->sd_qn_maxpsz == INFPSZ)
2490 		maxpsz = maxphys;
2491 	else
2492 		maxpsz = roundup(stp->sd_qn_maxpsz, MAXBSIZE);
2493 	/*
2494 	 * When the NOWAIT flag is not set, we enable zero-copy only if the
2495 	 * transfer size is large enough. This prevents performance loss
2496 	 * when the caller sends the file piece by piece.
2497 	 */
2498 	if (sfv_len >= MAXBSIZE && (sfv_len >= (va_size >> 1) ||
2499 	    (sfv->sfv_flag & SFV_NOWAIT) || sfv_len >= 0x1000000) &&
2500 	    !vn_has_flocks(fvp) && !(fvp->v_flag & VNOMAP)) {
2501 		if ((stp->sd_copyflag & (STZCVMSAFE|STZCVMUNSAFE)) == 0) {
2502 			int on = 1;
2503 
2504 			if (SOP_SETSOCKOPT(VTOSO(vp), SOL_SOCKET,
2505 			    SO_SND_COPYAVOID, &on, sizeof (on)) == 0)
2506 				dozcopy = B_TRUE;
2507 		} else {
2508 			dozcopy = (stp->sd_copyflag & STZCVMSAFE);
2509 		}
2510 	}
2511 	if (dozcopy) {
2512 		sf_stats.ss_file_segmap++;
2513 		error = snf_segmap(fp, fvp, sfv_off, (u_offset_t)sfv_len,
2514 		    maxpsz, &count, ((sfv->sfv_flag & SFV_NOWAIT) != 0));
2515 	} else {
2516 		sf_stats.ss_file_cached++;
2517 		error = snf_cache(fp, fvp, sfv_off, (u_offset_t)sfv_len,
2518 		    maxpsz, &count);
2519 	}
2520 out:
2521 	releasef(sfv->sfv_fd);
2522 	*count32 = (ssize32_t)count;
2523 	return (error);
2524 }
2525 #endif
2526 
2527 #ifdef _SYSCALL32_IMPL
2528 /*
2529  * recv32(), recvfrom32(), send32(), sendto32(): intentionally return a
2530  * ssize_t rather than ssize32_t; see the comments above read32 for details.
2531  */
2532 
2533 ssize_t
2534 recv32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags)
2535 {
2536 	return (recv(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags));
2537 }
2538 
2539 ssize_t
2540 recvfrom32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags,
2541 	caddr32_t name, caddr32_t namelenp)
2542 {
2543 	return (recvfrom(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags,
2544 	    (void *)(uintptr_t)name, (void *)(uintptr_t)namelenp));
2545 }
2546 
2547 ssize_t
2548 send32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags)
2549 {
2550 	return (send(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags));
2551 }
2552 
2553 ssize_t
2554 sendto32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags,
2555 	caddr32_t name, socklen_t namelen)
2556 {
2557 	return (sendto(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags,
2558 	    (void *)(uintptr_t)name, namelen));
2559 }
2560 #endif	/* _SYSCALL32_IMPL */
2561 
2562 /*
2563  * Function wrappers (mostly arround the sonode switch) for
2564  * backward compatibility.
2565  */
2566 
2567 int
2568 soaccept(struct sonode *so, int fflag, struct sonode **nsop)
2569 {
2570 	return (SOP_ACCEPT(so, fflag, nsop));
2571 }
2572 
2573 int
2574 sobind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
2575     int backlog, int flags)
2576 {
2577 	int	error;
2578 
2579 	error = SOP_BIND(so, name, namelen, flags);
2580 	if (error == 0 && backlog != 0)
2581 		return (SOP_LISTEN(so, backlog));
2582 
2583 	return (error);
2584 }
2585 
2586 int
2587 solisten(struct sonode *so, int backlog)
2588 {
2589 	return (SOP_LISTEN(so, backlog));
2590 }
2591 
2592 int
2593 soconnect(struct sonode *so, const struct sockaddr *name, socklen_t namelen,
2594     int fflag, int flags)
2595 {
2596 	return (SOP_CONNECT(so, name, namelen, fflag, flags));
2597 }
2598 
2599 int
2600 sorecvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
2601 {
2602 	return (SOP_RECVMSG(so, msg, uiop));
2603 }
2604 
2605 int
2606 sosendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
2607 {
2608 	return (SOP_SENDMSG(so, msg, uiop));
2609 }
2610 
2611 int
2612 sogetpeername(struct sonode *so)
2613 {
2614 	return (SOP_GETPEERNAME(so));
2615 }
2616 
2617 int
2618 sogetsockname(struct sonode *so)
2619 {
2620 	return (SOP_GETSOCKNAME(so));
2621 }
2622 
2623 int
2624 soshutdown(struct sonode *so, int how)
2625 {
2626 	return (SOP_SHUTDOWN(so, how));
2627 }
2628 
2629 int
2630 sogetsockopt(struct sonode *so, int level, int option_name, void *optval,
2631     socklen_t *optlenp, int flags)
2632 {
2633 	return (SOP_GETSOCKOPT(so, level, option_name, optval, optlenp,
2634 	    flags));
2635 }
2636 
2637 int
2638 sosetsockopt(struct sonode *so, int level, int option_name, const void *optval,
2639     t_uscalar_t optlen)
2640 {
2641 	return (SOP_SETSOCKOPT(so, level, option_name, optval, optlen));
2642 }
2643 
2644 /*
2645  * Because this is backward compatibility interface it only needs to be
2646  * able to handle the creation of TPI sockfs sockets.
2647  */
2648 struct sonode *
2649 socreate(vnode_t *accessvp, int domain, int type, int protocol, int version,
2650     struct sonode *tso, int *errorp)
2651 {
2652 	return (sotpi_create(accessvp, domain, type, protocol, version, tso,
2653 	    errorp));
2654 }
2655