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