xref: /illumos-gate/usr/src/uts/common/fs/sockfs/socksyscalls.c (revision 0250c53ad267726f2438e3c6556199a0bbf588a2)
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 (c) 1995, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright 2015, Joyent, Inc.  All rights reserved.
25  * Copyright (c) 2013, OmniTI Computer Consulting, Inc. All rights reserved.
26  * Copyright 2015 Nexenta Systems, Inc. All rights reserved.
27  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
28  * Copyright 2022 Garrett D'Amore
29  * Copyright 2024 Oxide Computer Company
30  */
31 
32 #include <sys/types.h>
33 #include <sys/t_lock.h>
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/buf.h>
37 #include <sys/conf.h>
38 #include <sys/cred.h>
39 #include <sys/kmem.h>
40 #include <sys/sysmacros.h>
41 #include <sys/vfs.h>
42 #include <sys/vnode.h>
43 #include <sys/debug.h>
44 #include <sys/errno.h>
45 #include <sys/time.h>
46 #include <sys/file.h>
47 #include <sys/user.h>
48 #include <sys/stream.h>
49 #include <sys/strsubr.h>
50 #include <sys/strsun.h>
51 #include <sys/sunddi.h>
52 #include <sys/esunddi.h>
53 #include <sys/flock.h>
54 #include <sys/modctl.h>
55 #include <sys/cmn_err.h>
56 #include <sys/vmsystm.h>
57 #include <sys/policy.h>
58 #include <sys/limits.h>
59 
60 #include <sys/socket.h>
61 #include <sys/socketvar.h>
62 
63 #include <sys/isa_defs.h>
64 #include <sys/inttypes.h>
65 #include <sys/systm.h>
66 #include <sys/cpuvar.h>
67 #include <sys/filio.h>
68 #include <sys/sendfile.h>
69 #include <sys/ddi.h>
70 #include <vm/seg.h>
71 #include <vm/seg_map.h>
72 #include <vm/seg_kpm.h>
73 
74 #include <fs/sockfs/sockcommon.h>
75 #include <fs/sockfs/sockfilter_impl.h>
76 #include <fs/sockfs/socktpi.h>
77 
78 #ifdef SOCK_TEST
79 int do_useracc = 1;		/* Controlled by setting SO_DEBUG to 4 */
80 #else
81 #define	do_useracc	1
82 #endif /* SOCK_TEST */
83 
84 extern int	xnet_truncate_print;
85 
86 /*
87  * This constitutes the known flags that are allowed to be passed in the upper
88  * bits of a socket type either for socket() or accept4().
89  */
90 #define	SOCK_KNOWN_FLAGS	(SOCK_CLOEXEC | SOCK_NDELAY | SOCK_NONBLOCK | \
91 				    SOCK_CLOFORK)
92 
93 /*
94  * Kernel component of socket creation.
95  *
96  * The socket library determines which version number to use.
97  * First the library calls this with a NULL devpath. If this fails
98  * to find a transport (using solookup) the library will look in /etc/netconfig
99  * for the appropriate transport. If one is found it will pass in the
100  * devpath for the kernel to use.
101  */
102 int
so_socket(int family,int type_w_flags,int protocol,char * devpath,int version)103 so_socket(int family, int type_w_flags, int protocol, char *devpath,
104     int version)
105 {
106 	struct sonode *so;
107 	vnode_t *vp;
108 	struct file *fp;
109 	int fd;
110 	int error;
111 	int type;
112 
113 	type = type_w_flags & SOCK_TYPE_MASK;
114 	type_w_flags &= ~SOCK_TYPE_MASK;
115 	if (type_w_flags & ~SOCK_KNOWN_FLAGS)
116 		return (set_errno(EINVAL));
117 
118 	if (devpath != NULL) {
119 		char *buf;
120 		size_t kdevpathlen = 0;
121 
122 		buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
123 		if ((error = copyinstr(devpath, buf,
124 		    MAXPATHLEN, &kdevpathlen)) != 0) {
125 			kmem_free(buf, MAXPATHLEN);
126 			return (set_errno(error));
127 		}
128 		so = socket_create(family, type, protocol, buf, NULL,
129 		    SOCKET_SLEEP, version, CRED(), &error);
130 		kmem_free(buf, MAXPATHLEN);
131 	} else {
132 		so = socket_create(family, type, protocol, NULL, NULL,
133 		    SOCKET_SLEEP, version, CRED(), &error);
134 	}
135 	if (so == NULL)
136 		return (set_errno(error));
137 
138 	/* Allocate a file descriptor for the socket */
139 	vp = SOTOV(so);
140 	error = falloc(vp, FWRITE|FREAD, &fp, &fd);
141 	if (error != 0) {
142 		(void) socket_close(so, 0, CRED());
143 		socket_destroy(so);
144 		return (set_errno(error));
145 	}
146 
147 	/*
148 	 * Now fill in the entries that falloc reserved
149 	 */
150 	if (type_w_flags & SOCK_NDELAY) {
151 		so->so_state |= SS_NDELAY;
152 		fp->f_flag |= FNDELAY;
153 	}
154 	if (type_w_flags & SOCK_NONBLOCK) {
155 		so->so_state |= SS_NONBLOCK;
156 		fp->f_flag |= FNONBLOCK;
157 	}
158 	mutex_exit(&fp->f_tlock);
159 	setf(fd, fp);
160 	if ((type_w_flags & SOCK_CLOEXEC) != 0) {
161 		f_setfd_or(fd, FD_CLOEXEC);
162 	}
163 	if ((type_w_flags & SOCK_CLOFORK) != 0) {
164 		f_setfd_or(fd, FD_CLOFORK);
165 	}
166 
167 	return (fd);
168 }
169 
170 /*
171  * Map from a file descriptor to a socket node.
172  * Returns with the file descriptor held i.e. the caller has to
173  * use releasef when done with the file descriptor.
174  */
175 struct sonode *
getsonode(int sock,int * errorp,file_t ** fpp)176 getsonode(int sock, int *errorp, file_t **fpp)
177 {
178 	file_t *fp;
179 	vnode_t *vp;
180 	struct sonode *so;
181 
182 	if ((fp = getf(sock)) == NULL) {
183 		*errorp = EBADF;
184 		eprintline(*errorp);
185 		return (NULL);
186 	}
187 	vp = fp->f_vnode;
188 	/* Check if it is a socket */
189 	if (vp->v_type != VSOCK) {
190 		releasef(sock);
191 		*errorp = ENOTSOCK;
192 		eprintline(*errorp);
193 		return (NULL);
194 	}
195 	/*
196 	 * Use the stream head to find the real socket vnode.
197 	 * This is needed when namefs sits above sockfs.
198 	 */
199 	if (vp->v_stream) {
200 		ASSERT(vp->v_stream->sd_vnode);
201 		vp = vp->v_stream->sd_vnode;
202 
203 		so = VTOSO(vp);
204 		if (so->so_version == SOV_STREAM) {
205 			releasef(sock);
206 			*errorp = ENOTSOCK;
207 			eprintsoline(so, *errorp);
208 			return (NULL);
209 		}
210 	} else {
211 		so = VTOSO(vp);
212 	}
213 	if (fpp)
214 		*fpp = fp;
215 	return (so);
216 }
217 
218 /*
219  * Allocate and copyin a sockaddr.
220  * Ensures NULL termination for AF_UNIX addresses by extending them
221  * with one NULL byte if need be. Verifies that the length is not
222  * excessive to prevent an application from consuming all of kernel
223  * memory. Returns NULL when an error occurred.
224  */
225 static struct sockaddr *
copyin_name(struct sonode * so,struct sockaddr * name,socklen_t * namelenp,int * errorp)226 copyin_name(struct sonode *so, struct sockaddr *name, socklen_t *namelenp,
227     int *errorp)
228 {
229 	char	*faddr;
230 	size_t	namelen = (size_t)*namelenp;
231 
232 	ASSERT(namelen != 0);
233 	if (namelen > SO_MAXARGSIZE) {
234 		*errorp = EINVAL;
235 		eprintsoline(so, *errorp);
236 		return (NULL);
237 	}
238 
239 	faddr = (char *)kmem_alloc(namelen, KM_SLEEP);
240 	if (copyin(name, faddr, namelen)) {
241 		kmem_free(faddr, namelen);
242 		*errorp = EFAULT;
243 		eprintsoline(so, *errorp);
244 		return (NULL);
245 	}
246 
247 	/*
248 	 * Add space for NULL termination if needed.
249 	 * Do a quick check if the last byte is NUL.
250 	 */
251 	if (so->so_family == AF_UNIX && faddr[namelen - 1] != '\0') {
252 		/* Check if there is any NULL termination */
253 		size_t	i;
254 		int foundnull = 0;
255 
256 		for (i = sizeof (name->sa_family); i < namelen; i++) {
257 			if (faddr[i] == '\0') {
258 				foundnull = 1;
259 				break;
260 			}
261 		}
262 		if (!foundnull) {
263 			/* Add extra byte for NUL padding */
264 			char *nfaddr;
265 
266 			nfaddr = (char *)kmem_alloc(namelen + 1, KM_SLEEP);
267 			bcopy(faddr, nfaddr, namelen);
268 			kmem_free(faddr, namelen);
269 
270 			/* NUL terminate */
271 			nfaddr[namelen] = '\0';
272 			namelen++;
273 			ASSERT((socklen_t)namelen == namelen);
274 			*namelenp = (socklen_t)namelen;
275 			faddr = nfaddr;
276 		}
277 	}
278 	return ((struct sockaddr *)faddr);
279 }
280 
281 /*
282  * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL.
283  */
284 static int
copyout_arg(void * uaddr,socklen_t ulen,void * ulenp,void * kaddr,socklen_t klen)285 copyout_arg(void *uaddr, socklen_t ulen, void *ulenp, void *kaddr,
286     socklen_t klen)
287 {
288 	if (uaddr != NULL) {
289 		if (ulen > klen)
290 			ulen = klen;
291 
292 		if (ulen != 0) {
293 			if (copyout(kaddr, uaddr, ulen))
294 				return (EFAULT);
295 		}
296 	} else
297 		ulen = 0;
298 
299 	if (ulenp != NULL) {
300 		if (copyout(&ulen, ulenp, sizeof (ulen)))
301 			return (EFAULT);
302 	}
303 	return (0);
304 }
305 
306 /*
307  * Copy from kaddr/klen to uaddr/ulen. Updates ulenp if non-NULL.
308  * If klen is greater than ulen it still uses the non-truncated
309  * klen to update ulenp.
310  */
311 static int
copyout_name(void * uaddr,socklen_t ulen,void * ulenp,void * kaddr,socklen_t klen)312 copyout_name(void *uaddr, socklen_t ulen, void *ulenp, void *kaddr,
313     socklen_t klen)
314 {
315 	if (uaddr != NULL) {
316 		if (ulen >= klen)
317 			ulen = klen;
318 		else if (ulen != 0 && xnet_truncate_print) {
319 			printf("sockfs: truncating copyout of address using "
320 			    "XNET semantics for pid = %d. Lengths %d, %d\n",
321 			    curproc->p_pid, klen, ulen);
322 		}
323 
324 		if (ulen != 0) {
325 			if (copyout(kaddr, uaddr, ulen))
326 				return (EFAULT);
327 		} else
328 			klen = 0;
329 	} else
330 		klen = 0;
331 
332 	if (ulenp != NULL) {
333 		if (copyout(&klen, ulenp, sizeof (klen)))
334 			return (EFAULT);
335 	}
336 	return (0);
337 }
338 
339 /*
340  * The socketpair() code in libsocket creates two sockets (using
341  * the /etc/netconfig fallback if needed) before calling this routine
342  * to connect the two sockets together.
343  *
344  * For a SOCK_STREAM socketpair a listener is needed - in that case this
345  * routine will create a new file descriptor as part of accepting the
346  * connection. The library socketpair() will check if svs[2] has changed
347  * in which case it will close the changed fd.
348  *
349  * Note that this code could use the TPI feature of accepting the connection
350  * on the listening endpoint. However, that would require significant changes
351  * to soaccept.
352  */
353 int
so_socketpair(int sv[2])354 so_socketpair(int sv[2])
355 {
356 	int svs[2];
357 	struct sonode *so1, *so2;
358 	int error;
359 	int orig_flags;
360 	struct sockaddr_ux *name;
361 	size_t namelen;
362 	sotpi_info_t *sti1;
363 	sotpi_info_t *sti2;
364 
365 	dprint(1, ("so_socketpair(%p)\n", (void *)sv));
366 
367 	error = useracc(sv, sizeof (svs), B_WRITE);
368 	if (error && do_useracc)
369 		return (set_errno(EFAULT));
370 
371 	if (copyin(sv, svs, sizeof (svs)))
372 		return (set_errno(EFAULT));
373 
374 	if ((so1 = getsonode(svs[0], &error, NULL)) == NULL)
375 		return (set_errno(error));
376 
377 	if ((so2 = getsonode(svs[1], &error, NULL)) == NULL) {
378 		releasef(svs[0]);
379 		return (set_errno(error));
380 	}
381 
382 	if (so1->so_family != AF_UNIX || so2->so_family != AF_UNIX) {
383 		error = EOPNOTSUPP;
384 		goto done;
385 	}
386 
387 	sti1 = SOTOTPI(so1);
388 	sti2 = SOTOTPI(so2);
389 
390 	/*
391 	 * The code below makes assumptions about the "sockfs" implementation.
392 	 * So make sure that the correct implementation is really used.
393 	 */
394 	ASSERT(so1->so_ops == &sotpi_sonodeops);
395 	ASSERT(so2->so_ops == &sotpi_sonodeops);
396 
397 	if (so1->so_type == SOCK_DGRAM) {
398 		/*
399 		 * Bind both sockets and connect them with each other.
400 		 * Need to allocate name/namelen for soconnect.
401 		 */
402 		error = socket_bind(so1, NULL, 0, _SOBIND_UNSPEC, CRED());
403 		if (error) {
404 			eprintsoline(so1, error);
405 			goto done;
406 		}
407 		error = socket_bind(so2, NULL, 0, _SOBIND_UNSPEC, CRED());
408 		if (error) {
409 			eprintsoline(so2, error);
410 			goto done;
411 		}
412 		namelen = sizeof (struct sockaddr_ux);
413 		name = kmem_alloc(namelen, KM_SLEEP);
414 		name->sou_family = AF_UNIX;
415 		name->sou_addr = sti2->sti_ux_laddr;
416 		error = socket_connect(so1,
417 		    (struct sockaddr *)name,
418 		    (socklen_t)namelen,
419 		    0, _SOCONNECT_NOXLATE, CRED());
420 		if (error) {
421 			kmem_free(name, namelen);
422 			eprintsoline(so1, error);
423 			goto done;
424 		}
425 		name->sou_addr = sti1->sti_ux_laddr;
426 		error = socket_connect(so2,
427 		    (struct sockaddr *)name,
428 		    (socklen_t)namelen,
429 		    0, _SOCONNECT_NOXLATE, CRED());
430 		kmem_free(name, namelen);
431 		if (error) {
432 			eprintsoline(so2, error);
433 			goto done;
434 		}
435 		releasef(svs[0]);
436 		releasef(svs[1]);
437 	} else {
438 		/*
439 		 * Bind both sockets, with so1 being a listener.
440 		 * Connect so2 to so1 - nonblocking to avoid waiting for
441 		 * soaccept to complete.
442 		 * Accept a connection on so1. Pass out the new fd as sv[0].
443 		 * The library will detect the changed fd and close
444 		 * the original one.
445 		 */
446 		struct sonode *nso;
447 		struct vnode *nvp;
448 		struct file *nfp;
449 		int nfd;
450 
451 		/*
452 		 * We could simply call socket_listen() here (which would do the
453 		 * binding automatically) if the code didn't rely on passing
454 		 * _SOBIND_NOXLATE to the TPI implementation of socket_bind().
455 		 */
456 		error = socket_bind(so1, NULL, 0, _SOBIND_UNSPEC|
457 		    _SOBIND_NOXLATE|_SOBIND_LISTEN|_SOBIND_SOCKETPAIR,
458 		    CRED());
459 		if (error) {
460 			eprintsoline(so1, error);
461 			goto done;
462 		}
463 		error = socket_bind(so2, NULL, 0, _SOBIND_UNSPEC, CRED());
464 		if (error) {
465 			eprintsoline(so2, error);
466 			goto done;
467 		}
468 
469 		namelen = sizeof (struct sockaddr_ux);
470 		name = kmem_alloc(namelen, KM_SLEEP);
471 		name->sou_family = AF_UNIX;
472 		name->sou_addr = sti1->sti_ux_laddr;
473 		error = socket_connect(so2,
474 		    (struct sockaddr *)name,
475 		    (socklen_t)namelen,
476 		    FNONBLOCK, _SOCONNECT_NOXLATE, CRED());
477 		kmem_free(name, namelen);
478 		if (error) {
479 			if (error != EINPROGRESS) {
480 				eprintsoline(so2, error); goto done;
481 			}
482 		}
483 
484 		error = socket_accept(so1, 0, CRED(), &nso);
485 		if (error) {
486 			eprintsoline(so1, error);
487 			goto done;
488 		}
489 
490 		/* wait for so2 being SS_CONNECTED ignoring signals */
491 		mutex_enter(&so2->so_lock);
492 		error = sowaitconnected(so2, 0, 1);
493 		mutex_exit(&so2->so_lock);
494 		if (error != 0) {
495 			(void) socket_close(nso, 0, CRED());
496 			socket_destroy(nso);
497 			eprintsoline(so2, error);
498 			goto done;
499 		}
500 
501 		nvp = SOTOV(nso);
502 		error = falloc(nvp, FWRITE|FREAD, &nfp, &nfd);
503 		if (error != 0) {
504 			(void) socket_close(nso, 0, CRED());
505 			socket_destroy(nso);
506 			eprintsoline(nso, error);
507 			goto done;
508 		}
509 		/*
510 		 * copy over FNONBLOCK and FNDELAY flags should they exist
511 		 */
512 		if (so1->so_state & SS_NONBLOCK)
513 			nfp->f_flag |= FNONBLOCK;
514 		if (so1->so_state & SS_NDELAY)
515 			nfp->f_flag |= FNDELAY;
516 
517 		/*
518 		 * fill in the entries that falloc reserved
519 		 */
520 		mutex_exit(&nfp->f_tlock);
521 		setf(nfd, nfp);
522 
523 		/*
524 		 * get the original flags before we release
525 		 */
526 		VERIFY(f_getfd_error(svs[0], &orig_flags) == 0);
527 
528 		releasef(svs[0]);
529 		releasef(svs[1]);
530 
531 		/*
532 		 * If FD_CLOEXEC or FD_CLOFORK was set on the file descriptor
533 		 * we're swapping out, we should set it on the new one too.
534 		 */
535 		if (orig_flags & (FD_CLOEXEC | FD_CLOFORK)) {
536 			f_setfd_or(nfd, orig_flags & (FD_CLOEXEC | FD_CLOFORK));
537 		}
538 
539 		/*
540 		 * The socketpair library routine will close the original
541 		 * svs[0] when this code passes out a different file
542 		 * descriptor.
543 		 */
544 		svs[0] = nfd;
545 
546 		if (copyout(svs, sv, sizeof (svs))) {
547 			(void) closeandsetf(nfd, NULL);
548 			eprintline(EFAULT);
549 			return (set_errno(EFAULT));
550 		}
551 	}
552 	return (0);
553 
554 done:
555 	releasef(svs[0]);
556 	releasef(svs[1]);
557 	return (set_errno(error));
558 }
559 
560 int
bind(int sock,struct sockaddr * name,socklen_t namelen,int version)561 bind(int sock, struct sockaddr *name, socklen_t namelen, int version)
562 {
563 	struct sonode *so;
564 	int error;
565 
566 	dprint(1, ("bind(%d, %p, %d)\n",
567 	    sock, (void *)name, namelen));
568 
569 	if ((so = getsonode(sock, &error, NULL)) == NULL)
570 		return (set_errno(error));
571 
572 	/* Allocate and copyin name */
573 	/*
574 	 * X/Open test does not expect EFAULT with NULL name and non-zero
575 	 * namelen.
576 	 */
577 	if (name != NULL && namelen != 0) {
578 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
579 		name = copyin_name(so, name, &namelen, &error);
580 		if (name == NULL) {
581 			releasef(sock);
582 			return (set_errno(error));
583 		}
584 	} else {
585 		name = NULL;
586 		namelen = 0;
587 	}
588 
589 	switch (version) {
590 	default:
591 		error = socket_bind(so, name, namelen, 0, CRED());
592 		break;
593 	case SOV_XPG4_2:
594 		error = socket_bind(so, name, namelen, _SOBIND_XPG4_2, CRED());
595 		break;
596 	case SOV_SOCKBSD:
597 		error = socket_bind(so, name, namelen, _SOBIND_SOCKBSD, CRED());
598 		break;
599 	}
600 
601 	releasef(sock);
602 	if (name != NULL)
603 		kmem_free(name, (size_t)namelen);
604 
605 	if (error)
606 		return (set_errno(error));
607 	return (0);
608 }
609 
610 /* ARGSUSED2 */
611 int
listen(int sock,int backlog,int version)612 listen(int sock, int backlog, int version)
613 {
614 	struct sonode *so;
615 	int error;
616 
617 	dprint(1, ("listen(%d, %d)\n",
618 	    sock, backlog));
619 
620 	if ((so = getsonode(sock, &error, NULL)) == NULL)
621 		return (set_errno(error));
622 
623 	error = socket_listen(so, backlog, CRED());
624 
625 	releasef(sock);
626 	if (error)
627 		return (set_errno(error));
628 	return (0);
629 }
630 
631 /*ARGSUSED3*/
632 int
accept(int sock,struct sockaddr * name,socklen_t * namelenp,int version,int flags)633 accept(int sock, struct sockaddr *name, socklen_t *namelenp, int version,
634     int flags)
635 {
636 	struct sonode *so;
637 	file_t *fp;
638 	int error;
639 	socklen_t namelen;
640 	struct sonode *nso;
641 	struct vnode *nvp;
642 	struct file *nfp;
643 	int nfd;
644 	int ssflags;
645 	struct sockaddr *addrp;
646 	socklen_t addrlen;
647 
648 	dprint(1, ("accept(%d, %p, %p)\n",
649 	    sock, (void *)name, (void *)namelenp));
650 
651 	if (flags & ~SOCK_KNOWN_FLAGS) {
652 		return (set_errno(EINVAL));
653 	}
654 
655 	/* Translate SOCK_ flags to their SS_ variant */
656 	ssflags = 0;
657 	if (flags & SOCK_NONBLOCK)
658 		ssflags |= SS_NONBLOCK;
659 	if (flags & SOCK_NDELAY)
660 		ssflags |= SS_NDELAY;
661 
662 	if ((so = getsonode(sock, &error, &fp)) == NULL)
663 		return (set_errno(error));
664 
665 	if (name != NULL) {
666 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
667 		if (copyin(namelenp, &namelen, sizeof (namelen))) {
668 			releasef(sock);
669 			return (set_errno(EFAULT));
670 		}
671 		if (namelen != 0) {
672 			error = useracc(name, (size_t)namelen, B_WRITE);
673 			if (error && do_useracc) {
674 				releasef(sock);
675 				return (set_errno(EFAULT));
676 			}
677 		} else
678 			name = NULL;
679 	} else {
680 		namelen = 0;
681 	}
682 
683 	/*
684 	 * Allocate the user fd before socket_accept() in order to
685 	 * catch EMFILE errors before calling socket_accept().
686 	 */
687 	if ((nfd = ufalloc(0)) == -1) {
688 		eprintsoline(so, EMFILE);
689 		releasef(sock);
690 		return (set_errno(EMFILE));
691 	}
692 	error = socket_accept(so, fp->f_flag, CRED(), &nso);
693 	if (error) {
694 		setf(nfd, NULL);
695 		releasef(sock);
696 		return (set_errno(error));
697 	}
698 
699 	nvp = SOTOV(nso);
700 
701 	ASSERT(MUTEX_NOT_HELD(&nso->so_lock));
702 	if (namelen != 0) {
703 		addrlen = so->so_max_addr_len;
704 		addrp = (struct sockaddr *)kmem_alloc(addrlen, KM_SLEEP);
705 
706 		if ((error = socket_getpeername(nso, (struct sockaddr *)addrp,
707 		    &addrlen, B_TRUE, CRED())) == 0) {
708 			error = copyout_name(name, namelen, namelenp,
709 			    addrp, addrlen);
710 		} else {
711 			ASSERT(error == EINVAL || error == ENOTCONN);
712 			error = ECONNABORTED;
713 		}
714 		kmem_free(addrp, so->so_max_addr_len);
715 	}
716 
717 	if (error) {
718 		setf(nfd, NULL);
719 		(void) socket_close(nso, 0, CRED());
720 		socket_destroy(nso);
721 		releasef(sock);
722 		return (set_errno(error));
723 	}
724 	error = falloc(NULL, FWRITE|FREAD, &nfp, NULL);
725 	if (error != 0) {
726 		setf(nfd, NULL);
727 		(void) socket_close(nso, 0, CRED());
728 		socket_destroy(nso);
729 		eprintsoline(so, error);
730 		releasef(sock);
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 	 * Act on SOCK_CLOEXEC and SOCK_CLOFORK from flags
742 	 */
743 	if (flags & SOCK_CLOEXEC) {
744 		f_setfd_or(nfd, FD_CLOEXEC);
745 	}
746 
747 	if (flags & SOCK_CLOFORK) {
748 		f_setfd_or(nfd, FD_CLOFORK);
749 	}
750 
751 	/*
752 	 * Copy FNDELAY and FNONBLOCK from listener to acceptor
753 	 * and from ssflags
754 	 */
755 	if ((ssflags | so->so_state) & (SS_NDELAY|SS_NONBLOCK)) {
756 		uint_t oflag = nfp->f_flag;
757 		int arg = 0;
758 
759 		if ((ssflags | so->so_state) & SS_NONBLOCK)
760 			arg |= FNONBLOCK;
761 		else if ((ssflags | so->so_state) & SS_NDELAY)
762 			arg |= FNDELAY;
763 
764 		/*
765 		 * This code is a simplification of the F_SETFL code in fcntl()
766 		 * Ignore any errors from VOP_SETFL.
767 		 */
768 		if ((error = VOP_SETFL(nvp, oflag, arg, nfp->f_cred, NULL))
769 		    != 0) {
770 			eprintsoline(so, error);
771 			error = 0;
772 		} else {
773 			mutex_enter(&nfp->f_tlock);
774 			nfp->f_flag &= ~FMASK | (FREAD|FWRITE);
775 			nfp->f_flag |= arg;
776 			mutex_exit(&nfp->f_tlock);
777 		}
778 	}
779 	releasef(sock);
780 	return (nfd);
781 }
782 
783 int
connect(int sock,struct sockaddr * name,socklen_t namelen,int version)784 connect(int sock, struct sockaddr *name, socklen_t namelen, int version)
785 {
786 	struct sonode *so;
787 	file_t *fp;
788 	int error;
789 
790 	dprint(1, ("connect(%d, %p, %d)\n",
791 	    sock, (void *)name, namelen));
792 
793 	if ((so = getsonode(sock, &error, &fp)) == NULL)
794 		return (set_errno(error));
795 
796 	/* Allocate and copyin name */
797 	if (namelen != 0) {
798 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
799 		name = copyin_name(so, name, &namelen, &error);
800 		if (name == NULL) {
801 			releasef(sock);
802 			return (set_errno(error));
803 		}
804 	} else
805 		name = NULL;
806 
807 	error = socket_connect(so, name, namelen, fp->f_flag,
808 	    (version != SOV_XPG4_2) ? 0 : _SOCONNECT_XPG4_2, CRED());
809 	releasef(sock);
810 	if (name)
811 		kmem_free(name, (size_t)namelen);
812 	if (error)
813 		return (set_errno(error));
814 	return (0);
815 }
816 
817 /*ARGSUSED2*/
818 int
shutdown(int sock,int how,int version)819 shutdown(int sock, int how, int version)
820 {
821 	struct sonode *so;
822 	int error;
823 
824 	dprint(1, ("shutdown(%d, %d)\n",
825 	    sock, how));
826 
827 	if ((so = getsonode(sock, &error, NULL)) == NULL)
828 		return (set_errno(error));
829 
830 	error = socket_shutdown(so, how, CRED());
831 
832 	releasef(sock);
833 	if (error)
834 		return (set_errno(error));
835 	return (0);
836 }
837 
838 /*
839  * Common receive routine.
840  */
841 static ssize_t
recvit(int sock,struct nmsghdr * msg,struct uio * uiop,int flags,socklen_t * namelenp,socklen_t * controllenp,int * flagsp)842 recvit(int sock, struct nmsghdr *msg, struct uio *uiop, int flags,
843     socklen_t *namelenp, socklen_t *controllenp, int *flagsp)
844 {
845 	struct sonode *so;
846 	file_t *fp;
847 	void *name;
848 	socklen_t namelen;
849 	void *control;
850 	socklen_t controllen, free_controllen;
851 	ssize_t len;
852 	int error;
853 
854 	if ((so = getsonode(sock, &error, &fp)) == NULL)
855 		return (set_errno(error));
856 
857 	len = uiop->uio_resid;
858 	uiop->uio_fmode = fp->f_flag;
859 	uiop->uio_extflg = UIO_COPY_CACHED;
860 
861 	name = msg->msg_name;
862 	namelen = msg->msg_namelen;
863 	control = msg->msg_control;
864 	controllen = msg->msg_controllen;
865 
866 	msg->msg_flags = flags & (MSG_OOB | MSG_PEEK | MSG_WAITALL |
867 	    MSG_DONTWAIT | MSG_XPG4_2 | MSG_CMSG_CLOEXEC | MSG_CMSG_CLOFORK);
868 
869 	error = socket_recvmsg(so, msg, uiop, CRED());
870 	if (error) {
871 		releasef(sock);
872 		return (set_errno(error));
873 	}
874 	lwp_stat_update(LWP_STAT_MSGRCV, 1);
875 	releasef(sock);
876 
877 	free_controllen = msg->msg_controllen;
878 
879 	error = copyout_name(name, namelen, namelenp,
880 	    msg->msg_name, msg->msg_namelen);
881 	if (error)
882 		goto err;
883 
884 	if (flagsp != NULL) {
885 		/*
886 		 * Clear internal flag. We also clear the CMSG flags out of
887 		 * paranoia, though they should have been cleared by our
888 		 * sop_recvmsg.
889 		 */
890 		msg->msg_flags &= ~(MSG_XPG4_2 | MSG_CMSG_CLOEXEC |
891 		    MSG_CMSG_CLOFORK);
892 
893 		/*
894 		 * Determine MSG_CTRUNC. sorecvmsg sets MSG_CTRUNC only
895 		 * when controllen is zero and there is control data to
896 		 * copy out.
897 		 */
898 		if (controllen != 0 &&
899 		    (msg->msg_controllen > controllen || control == NULL)) {
900 			dprint(1, ("recvit: CTRUNC %d %d %p\n",
901 			    msg->msg_controllen, controllen, control));
902 
903 			msg->msg_flags |= MSG_CTRUNC;
904 		}
905 		if (copyout(&msg->msg_flags, flagsp,
906 		    sizeof (msg->msg_flags))) {
907 			error = EFAULT;
908 			goto err;
909 		}
910 	}
911 
912 	if (controllen != 0) {
913 		if (!(flags & MSG_XPG4_2)) {
914 			/*
915 			 * Good old msg_accrights can only return a multiple
916 			 * of 4 bytes.
917 			 */
918 			controllen &= ~((int)sizeof (uint32_t) - 1);
919 		}
920 
921 		if (msg->msg_controllen > controllen || control == NULL) {
922 			/*
923 			 * If the truncated part contains file descriptors,
924 			 * then they must be closed in the kernel as they
925 			 * will not be included in the data returned to
926 			 * user space. Close them now so that the header size
927 			 * can be safely adjusted prior to copyout. In case of
928 			 * an error during copyout, the remaining file
929 			 * descriptors will be closed in the error handler
930 			 * below.
931 			 */
932 			so_closefds(msg->msg_control, msg->msg_controllen,
933 			    !(flags & MSG_XPG4_2),
934 			    control == NULL ? 0 : controllen);
935 
936 			/*
937 			 * In the case of a truncated control message, the last
938 			 * cmsg header that fits into the available buffer
939 			 * space must be adjusted to reflect the actual amount
940 			 * of associated data that will be returned. This only
941 			 * needs to be done for XPG4 messages as non-XPG4
942 			 * messages are not structured (they are just a
943 			 * buffer and a length - msg_accrights(len)).
944 			 */
945 			if (control != NULL && (flags & MSG_XPG4_2)) {
946 				so_truncatecmsg(msg->msg_control,
947 				    msg->msg_controllen, controllen);
948 				msg->msg_controllen = controllen;
949 			}
950 		}
951 
952 		error = copyout_arg(control, controllen, controllenp,
953 		    msg->msg_control, msg->msg_controllen);
954 
955 		if (error)
956 			goto err;
957 
958 	}
959 	if (msg->msg_namelen != 0)
960 		kmem_free(msg->msg_name, (size_t)msg->msg_namelen);
961 	if (free_controllen != 0)
962 		kmem_free(msg->msg_control, (size_t)free_controllen);
963 	return (len - uiop->uio_resid);
964 
965 err:
966 	/*
967 	 * If we fail and the control part contains file descriptors
968 	 * we have to close them. For a truncated control message, the
969 	 * descriptors which were cut off have already been closed and the
970 	 * length adjusted so that they will not be closed again.
971 	 */
972 	if (msg->msg_controllen != 0)
973 		so_closefds(msg->msg_control, msg->msg_controllen,
974 		    !(flags & MSG_XPG4_2), 0);
975 	if (msg->msg_namelen != 0)
976 		kmem_free(msg->msg_name, (size_t)msg->msg_namelen);
977 	if (free_controllen != 0)
978 		kmem_free(msg->msg_control, (size_t)free_controllen);
979 	return (set_errno(error));
980 }
981 
982 /*
983  * Native system call
984  */
985 ssize_t
recv(int sock,void * buffer,size_t len,int flags)986 recv(int sock, void *buffer, size_t len, int flags)
987 {
988 	struct nmsghdr lmsg;
989 	struct uio auio;
990 	struct iovec aiov[1];
991 
992 	dprint(1, ("recv(%d, %p, %ld, %d)\n",
993 	    sock, buffer, len, flags));
994 
995 	if ((ssize_t)len < 0) {
996 		return (set_errno(EINVAL));
997 	}
998 
999 	aiov[0].iov_base = buffer;
1000 	aiov[0].iov_len = len;
1001 	auio.uio_loffset = 0;
1002 	auio.uio_iov = aiov;
1003 	auio.uio_iovcnt = 1;
1004 	auio.uio_resid = len;
1005 	auio.uio_segflg = UIO_USERSPACE;
1006 	auio.uio_limit = 0;
1007 
1008 	lmsg.msg_namelen = 0;
1009 	lmsg.msg_controllen = 0;
1010 	lmsg.msg_flags = 0;
1011 	return (recvit(sock, &lmsg, &auio, flags, NULL, NULL, NULL));
1012 }
1013 
1014 ssize_t
recvfrom(int sock,void * buffer,size_t len,int flags,struct sockaddr * name,socklen_t * namelenp)1015 recvfrom(int sock, void *buffer, size_t len, int flags, struct sockaddr *name,
1016     socklen_t *namelenp)
1017 {
1018 	struct nmsghdr lmsg;
1019 	struct uio auio;
1020 	struct iovec aiov[1];
1021 
1022 	dprint(1, ("recvfrom(%d, %p, %ld, %d, %p, %p)\n",
1023 	    sock, buffer, len, flags, (void *)name, (void *)namelenp));
1024 
1025 	if ((ssize_t)len < 0) {
1026 		return (set_errno(EINVAL));
1027 	}
1028 
1029 	aiov[0].iov_base = buffer;
1030 	aiov[0].iov_len = len;
1031 	auio.uio_loffset = 0;
1032 	auio.uio_iov = aiov;
1033 	auio.uio_iovcnt = 1;
1034 	auio.uio_resid = len;
1035 	auio.uio_segflg = UIO_USERSPACE;
1036 	auio.uio_limit = 0;
1037 
1038 	lmsg.msg_name = (char *)name;
1039 	if (namelenp != NULL) {
1040 		if (copyin(namelenp, &lmsg.msg_namelen,
1041 		    sizeof (lmsg.msg_namelen)))
1042 			return (set_errno(EFAULT));
1043 	} else {
1044 		lmsg.msg_namelen = 0;
1045 	}
1046 	lmsg.msg_controllen = 0;
1047 	lmsg.msg_flags = 0;
1048 
1049 	return (recvit(sock, &lmsg, &auio, flags, namelenp, NULL, NULL));
1050 }
1051 
1052 /*
1053  * Uses the MSG_XPG4_2 flag to determine if the caller is using
1054  * struct omsghdr or struct nmsghdr.
1055  */
1056 ssize_t
recvmsg(int sock,struct nmsghdr * msg,int flags)1057 recvmsg(int sock, struct nmsghdr *msg, int flags)
1058 {
1059 	STRUCT_DECL(nmsghdr, u_lmsg);
1060 	STRUCT_HANDLE(nmsghdr, umsgptr);
1061 	struct nmsghdr lmsg;
1062 	struct uio auio;
1063 	struct iovec buf[IOV_MAX_STACK], *aiov = buf;
1064 	ssize_t iovsize = 0;
1065 	int iovcnt;
1066 	ssize_t len, rval;
1067 	int i;
1068 	int *flagsp;
1069 	model_t	model;
1070 
1071 	dprint(1, ("recvmsg(%d, %p, %d)\n",
1072 	    sock, (void *)msg, flags));
1073 
1074 	model = get_udatamodel();
1075 	STRUCT_INIT(u_lmsg, model);
1076 	STRUCT_SET_HANDLE(umsgptr, model, msg);
1077 
1078 	if (flags & MSG_XPG4_2) {
1079 		if (copyin(msg, STRUCT_BUF(u_lmsg), STRUCT_SIZE(u_lmsg)))
1080 			return (set_errno(EFAULT));
1081 		flagsp = STRUCT_FADDR(umsgptr, msg_flags);
1082 	} else {
1083 		/*
1084 		 * Assumes that nmsghdr and omsghdr are identically shaped
1085 		 * except for the added msg_flags field.
1086 		 */
1087 		if (copyin(msg, STRUCT_BUF(u_lmsg),
1088 		    SIZEOF_STRUCT(omsghdr, model)))
1089 			return (set_errno(EFAULT));
1090 		STRUCT_FSET(u_lmsg, msg_flags, 0);
1091 		flagsp = NULL;
1092 	}
1093 
1094 	/*
1095 	 * Code below us will kmem_alloc memory and hang it
1096 	 * off msg_control and msg_name fields. This forces
1097 	 * us to copy the structure to its native form.
1098 	 */
1099 	lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name);
1100 	lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen);
1101 	lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov);
1102 	lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen);
1103 	lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control);
1104 	lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen);
1105 	lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags);
1106 
1107 	iovcnt = lmsg.msg_iovlen;
1108 
1109 	if (iovcnt <= 0 || iovcnt > IOV_MAX) {
1110 		return (set_errno(EMSGSIZE));
1111 	}
1112 
1113 	if (iovcnt > IOV_MAX_STACK) {
1114 		iovsize = iovcnt * sizeof (struct iovec);
1115 		aiov = kmem_alloc(iovsize, KM_SLEEP);
1116 	}
1117 
1118 #ifdef _SYSCALL32_IMPL
1119 	/*
1120 	 * 32-bit callers need to have their iovec expanded, while ensuring
1121 	 * that they can't move more than 2Gbytes of data in a single call.
1122 	 */
1123 	if (model == DATAMODEL_ILP32) {
1124 		struct iovec32 buf32[IOV_MAX_STACK], *aiov32 = buf32;
1125 		ssize_t iov32size;
1126 		ssize32_t count32;
1127 
1128 		iov32size = iovcnt * sizeof (struct iovec32);
1129 		if (iovsize != 0)
1130 			aiov32 = kmem_alloc(iov32size, KM_SLEEP);
1131 
1132 		if (copyin((struct iovec32 *)lmsg.msg_iov, aiov32, iov32size)) {
1133 			if (iovsize != 0) {
1134 				kmem_free(aiov32, iov32size);
1135 				kmem_free(aiov, iovsize);
1136 			}
1137 
1138 			return (set_errno(EFAULT));
1139 		}
1140 
1141 		count32 = 0;
1142 		for (i = 0; i < iovcnt; i++) {
1143 			ssize32_t iovlen32;
1144 
1145 			iovlen32 = aiov32[i].iov_len;
1146 			count32 += iovlen32;
1147 			if (iovlen32 < 0 || count32 < 0) {
1148 				if (iovsize != 0) {
1149 					kmem_free(aiov32, iov32size);
1150 					kmem_free(aiov, iovsize);
1151 				}
1152 
1153 				return (set_errno(EINVAL));
1154 			}
1155 
1156 			aiov[i].iov_len = iovlen32;
1157 			aiov[i].iov_base =
1158 			    (caddr_t)(uintptr_t)aiov32[i].iov_base;
1159 		}
1160 
1161 		if (iovsize != 0)
1162 			kmem_free(aiov32, iov32size);
1163 	} else
1164 #endif /* _SYSCALL32_IMPL */
1165 	if (copyin(lmsg.msg_iov, aiov, iovcnt * sizeof (struct iovec))) {
1166 		if (iovsize != 0)
1167 			kmem_free(aiov, iovsize);
1168 
1169 		return (set_errno(EFAULT));
1170 	}
1171 	len = 0;
1172 	for (i = 0; i < iovcnt; i++) {
1173 		ssize_t iovlen = aiov[i].iov_len;
1174 		len += iovlen;
1175 		if (iovlen < 0 || len < 0) {
1176 			if (iovsize != 0)
1177 				kmem_free(aiov, iovsize);
1178 
1179 			return (set_errno(EINVAL));
1180 		}
1181 	}
1182 	auio.uio_loffset = 0;
1183 	auio.uio_iov = aiov;
1184 	auio.uio_iovcnt = iovcnt;
1185 	auio.uio_resid = len;
1186 	auio.uio_segflg = UIO_USERSPACE;
1187 	auio.uio_limit = 0;
1188 
1189 	if (lmsg.msg_control != NULL &&
1190 	    (do_useracc == 0 ||
1191 	    useracc(lmsg.msg_control, lmsg.msg_controllen,
1192 	    B_WRITE) != 0)) {
1193 		if (iovsize != 0)
1194 			kmem_free(aiov, iovsize);
1195 
1196 		return (set_errno(EFAULT));
1197 	}
1198 
1199 	rval = recvit(sock, &lmsg, &auio, flags,
1200 	    STRUCT_FADDR(umsgptr, msg_namelen),
1201 	    STRUCT_FADDR(umsgptr, msg_controllen), flagsp);
1202 
1203 	if (iovsize != 0)
1204 		kmem_free(aiov, iovsize);
1205 
1206 	return (rval);
1207 }
1208 
1209 /*
1210  * Common send function.
1211  */
1212 static ssize_t
sendit(int sock,struct nmsghdr * msg,struct uio * uiop,int flags)1213 sendit(int sock, struct nmsghdr *msg, struct uio *uiop, int flags)
1214 {
1215 	struct sonode *so;
1216 	file_t *fp;
1217 	void *name;
1218 	socklen_t namelen;
1219 	void *control;
1220 	socklen_t controllen;
1221 	ssize_t len;
1222 	int error;
1223 
1224 	if ((so = getsonode(sock, &error, &fp)) == NULL)
1225 		return (set_errno(error));
1226 
1227 	uiop->uio_fmode = fp->f_flag;
1228 
1229 	if (so->so_family == AF_UNIX)
1230 		uiop->uio_extflg = UIO_COPY_CACHED;
1231 	else
1232 		uiop->uio_extflg = UIO_COPY_DEFAULT;
1233 
1234 	len = uiop->uio_resid;
1235 
1236 	/* Allocate and copyin name and control */
1237 	name = msg->msg_name;
1238 	namelen = msg->msg_namelen;
1239 	if (name != NULL && namelen != 0) {
1240 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1241 		name = copyin_name(so,
1242 		    (struct sockaddr *)name,
1243 		    &namelen, &error);
1244 		if (name == NULL)
1245 			goto done3;
1246 		/* copyin_name null terminates addresses for AF_UNIX */
1247 		msg->msg_namelen = namelen;
1248 		msg->msg_name = name;
1249 	} else {
1250 		msg->msg_name = name = NULL;
1251 		msg->msg_namelen = namelen = 0;
1252 	}
1253 
1254 	control = msg->msg_control;
1255 	controllen = msg->msg_controllen;
1256 	if ((control != NULL) && (controllen != 0)) {
1257 		/*
1258 		 * Verify that the length is not excessive to prevent
1259 		 * an application from consuming all of kernel memory.
1260 		 */
1261 		if (controllen > SO_MAXARGSIZE) {
1262 			error = EINVAL;
1263 			goto done2;
1264 		}
1265 		control = kmem_alloc(controllen, KM_SLEEP);
1266 
1267 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1268 		if (copyin(msg->msg_control, control, controllen)) {
1269 			error = EFAULT;
1270 			goto done1;
1271 		}
1272 		msg->msg_control = control;
1273 	} else {
1274 		msg->msg_control = control = NULL;
1275 		msg->msg_controllen = controllen = 0;
1276 	}
1277 
1278 	msg->msg_flags = flags;
1279 
1280 	error = socket_sendmsg(so, msg, uiop, CRED());
1281 done1:
1282 	if (control != NULL)
1283 		kmem_free(control, controllen);
1284 done2:
1285 	if (name != NULL)
1286 		kmem_free(name, namelen);
1287 done3:
1288 	if (error != 0) {
1289 		releasef(sock);
1290 		return (set_errno(error));
1291 	}
1292 	lwp_stat_update(LWP_STAT_MSGSND, 1);
1293 	releasef(sock);
1294 	return (len - uiop->uio_resid);
1295 }
1296 
1297 /*
1298  * Native system call
1299  */
1300 ssize_t
send(int sock,void * buffer,size_t len,int flags)1301 send(int sock, void *buffer, size_t len, int flags)
1302 {
1303 	struct nmsghdr lmsg;
1304 	struct uio auio;
1305 	struct iovec aiov[1];
1306 
1307 	dprint(1, ("send(%d, %p, %ld, %d)\n",
1308 	    sock, buffer, len, flags));
1309 
1310 	if ((ssize_t)len < 0) {
1311 		return (set_errno(EINVAL));
1312 	}
1313 
1314 	aiov[0].iov_base = buffer;
1315 	aiov[0].iov_len = len;
1316 	auio.uio_loffset = 0;
1317 	auio.uio_iov = aiov;
1318 	auio.uio_iovcnt = 1;
1319 	auio.uio_resid = len;
1320 	auio.uio_segflg = UIO_USERSPACE;
1321 	auio.uio_limit = 0;
1322 
1323 	lmsg.msg_name = NULL;
1324 	lmsg.msg_control = NULL;
1325 	if (!(flags & MSG_XPG4_2)) {
1326 		/*
1327 		 * In order to be compatible with the libsocket/sockmod
1328 		 * implementation we set EOR for all send* calls.
1329 		 */
1330 		flags |= MSG_EOR;
1331 	}
1332 	return (sendit(sock, &lmsg, &auio, flags));
1333 }
1334 
1335 /*
1336  * Uses the MSG_XPG4_2 flag to determine if the caller is using
1337  * struct omsghdr or struct nmsghdr.
1338  */
1339 ssize_t
sendmsg(int sock,struct nmsghdr * msg,int flags)1340 sendmsg(int sock, struct nmsghdr *msg, int flags)
1341 {
1342 	struct nmsghdr lmsg;
1343 	STRUCT_DECL(nmsghdr, u_lmsg);
1344 	struct uio auio;
1345 	struct iovec buf[IOV_MAX_STACK], *aiov = buf;
1346 	ssize_t iovsize = 0;
1347 	int iovcnt;
1348 	ssize_t len, rval;
1349 	int i;
1350 	model_t	model;
1351 
1352 	dprint(1, ("sendmsg(%d, %p, %d)\n", sock, (void *)msg, flags));
1353 
1354 	model = get_udatamodel();
1355 	STRUCT_INIT(u_lmsg, model);
1356 
1357 	if (flags & MSG_XPG4_2) {
1358 		if (copyin(msg, (char *)STRUCT_BUF(u_lmsg),
1359 		    STRUCT_SIZE(u_lmsg)))
1360 			return (set_errno(EFAULT));
1361 	} else {
1362 		/*
1363 		 * Assumes that nmsghdr and omsghdr are identically shaped
1364 		 * except for the added msg_flags field.
1365 		 */
1366 		if (copyin(msg, (char *)STRUCT_BUF(u_lmsg),
1367 		    SIZEOF_STRUCT(omsghdr, model)))
1368 			return (set_errno(EFAULT));
1369 		/*
1370 		 * In order to be compatible with the libsocket/sockmod
1371 		 * implementation we set EOR for all send* calls.
1372 		 */
1373 		flags |= MSG_EOR;
1374 	}
1375 
1376 	/*
1377 	 * Code below us will kmem_alloc memory and hang it
1378 	 * off msg_control and msg_name fields. This forces
1379 	 * us to copy the structure to its native form.
1380 	 */
1381 	lmsg.msg_name = STRUCT_FGETP(u_lmsg, msg_name);
1382 	lmsg.msg_namelen = STRUCT_FGET(u_lmsg, msg_namelen);
1383 	lmsg.msg_iov = STRUCT_FGETP(u_lmsg, msg_iov);
1384 	lmsg.msg_iovlen = STRUCT_FGET(u_lmsg, msg_iovlen);
1385 	lmsg.msg_control = STRUCT_FGETP(u_lmsg, msg_control);
1386 	lmsg.msg_controllen = STRUCT_FGET(u_lmsg, msg_controllen);
1387 	lmsg.msg_flags = STRUCT_FGET(u_lmsg, msg_flags);
1388 
1389 	iovcnt = lmsg.msg_iovlen;
1390 
1391 	if (iovcnt <= 0 || iovcnt > IOV_MAX) {
1392 		/*
1393 		 * Unless this is XPG 4.2 we allow iovcnt == 0 to
1394 		 * be compatible with SunOS 4.X and 4.4BSD.
1395 		 */
1396 		if (iovcnt != 0 || (flags & MSG_XPG4_2))
1397 			return (set_errno(EMSGSIZE));
1398 	}
1399 
1400 	if (iovcnt > IOV_MAX_STACK) {
1401 		iovsize = iovcnt * sizeof (struct iovec);
1402 		aiov = kmem_alloc(iovsize, KM_SLEEP);
1403 	}
1404 
1405 #ifdef _SYSCALL32_IMPL
1406 	/*
1407 	 * 32-bit callers need to have their iovec expanded, while ensuring
1408 	 * that they can't move more than 2Gbytes of data in a single call.
1409 	 */
1410 	if (model == DATAMODEL_ILP32) {
1411 		struct iovec32 buf32[IOV_MAX_STACK], *aiov32 = buf32;
1412 		ssize_t iov32size;
1413 		ssize32_t count32;
1414 
1415 		iov32size = iovcnt * sizeof (struct iovec32);
1416 		if (iovsize != 0)
1417 			aiov32 = kmem_alloc(iov32size, KM_SLEEP);
1418 
1419 		if (iovcnt != 0 &&
1420 		    copyin((struct iovec32 *)lmsg.msg_iov, aiov32, iov32size)) {
1421 			if (iovsize != 0) {
1422 				kmem_free(aiov32, iov32size);
1423 				kmem_free(aiov, iovsize);
1424 			}
1425 
1426 			return (set_errno(EFAULT));
1427 		}
1428 
1429 		count32 = 0;
1430 		for (i = 0; i < iovcnt; i++) {
1431 			ssize32_t iovlen32;
1432 
1433 			iovlen32 = aiov32[i].iov_len;
1434 			count32 += iovlen32;
1435 			if (iovlen32 < 0 || count32 < 0) {
1436 				if (iovsize != 0) {
1437 					kmem_free(aiov32, iov32size);
1438 					kmem_free(aiov, iovsize);
1439 				}
1440 
1441 				return (set_errno(EINVAL));
1442 			}
1443 
1444 			aiov[i].iov_len = iovlen32;
1445 			aiov[i].iov_base =
1446 			    (caddr_t)(uintptr_t)aiov32[i].iov_base;
1447 		}
1448 
1449 		if (iovsize != 0)
1450 			kmem_free(aiov32, iov32size);
1451 	} else
1452 #endif /* _SYSCALL32_IMPL */
1453 	if (iovcnt != 0 &&
1454 	    copyin(lmsg.msg_iov, aiov,
1455 	    (unsigned)iovcnt * sizeof (struct iovec))) {
1456 		if (iovsize != 0)
1457 			kmem_free(aiov, iovsize);
1458 
1459 		return (set_errno(EFAULT));
1460 	}
1461 	len = 0;
1462 	for (i = 0; i < iovcnt; i++) {
1463 		ssize_t iovlen = aiov[i].iov_len;
1464 		len += iovlen;
1465 		if (iovlen < 0 || len < 0) {
1466 			if (iovsize != 0)
1467 				kmem_free(aiov, iovsize);
1468 
1469 			return (set_errno(EINVAL));
1470 		}
1471 	}
1472 	auio.uio_loffset = 0;
1473 	auio.uio_iov = aiov;
1474 	auio.uio_iovcnt = iovcnt;
1475 	auio.uio_resid = len;
1476 	auio.uio_segflg = UIO_USERSPACE;
1477 	auio.uio_limit = 0;
1478 
1479 	rval = sendit(sock, &lmsg, &auio, flags);
1480 
1481 	if (iovsize != 0)
1482 		kmem_free(aiov, iovsize);
1483 
1484 	return (rval);
1485 }
1486 
1487 ssize_t
sendto(int sock,void * buffer,size_t len,int flags,struct sockaddr * name,socklen_t namelen)1488 sendto(int sock, void *buffer, size_t len, int flags,
1489     struct sockaddr *name, socklen_t namelen)
1490 {
1491 	struct nmsghdr lmsg;
1492 	struct uio auio;
1493 	struct iovec aiov[1];
1494 
1495 	dprint(1, ("sendto(%d, %p, %ld, %d, %p, %d)\n",
1496 	    sock, buffer, len, flags, (void *)name, namelen));
1497 
1498 	if ((ssize_t)len < 0) {
1499 		return (set_errno(EINVAL));
1500 	}
1501 
1502 	aiov[0].iov_base = buffer;
1503 	aiov[0].iov_len = len;
1504 	auio.uio_loffset = 0;
1505 	auio.uio_iov = aiov;
1506 	auio.uio_iovcnt = 1;
1507 	auio.uio_resid = len;
1508 	auio.uio_segflg = UIO_USERSPACE;
1509 	auio.uio_limit = 0;
1510 
1511 	lmsg.msg_name = (char *)name;
1512 	lmsg.msg_namelen = namelen;
1513 	lmsg.msg_control = NULL;
1514 	if (!(flags & MSG_XPG4_2)) {
1515 		/*
1516 		 * In order to be compatible with the libsocket/sockmod
1517 		 * implementation we set EOR for all send* calls.
1518 		 */
1519 		flags |= MSG_EOR;
1520 	}
1521 	return (sendit(sock, &lmsg, &auio, flags));
1522 }
1523 
1524 /*ARGSUSED3*/
1525 int
getpeername(int sock,struct sockaddr * name,socklen_t * namelenp,int version)1526 getpeername(int sock, struct sockaddr *name, socklen_t *namelenp, int version)
1527 {
1528 	struct sonode *so;
1529 	int error;
1530 	socklen_t namelen;
1531 	socklen_t sock_addrlen;
1532 	struct sockaddr *sock_addrp;
1533 
1534 	dprint(1, ("getpeername(%d, %p, %p)\n",
1535 	    sock, (void *)name, (void *)namelenp));
1536 
1537 	if ((so = getsonode(sock, &error, NULL)) == NULL)
1538 		goto bad;
1539 
1540 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1541 	if (copyin(namelenp, &namelen, sizeof (namelen)) ||
1542 	    (name == NULL && namelen != 0)) {
1543 		error = EFAULT;
1544 		goto rel_out;
1545 	}
1546 	sock_addrlen = so->so_max_addr_len;
1547 	sock_addrp = (struct sockaddr *)kmem_alloc(sock_addrlen, KM_SLEEP);
1548 
1549 	if ((error = socket_getpeername(so, sock_addrp, &sock_addrlen,
1550 	    B_FALSE, CRED())) == 0) {
1551 		ASSERT(sock_addrlen <= so->so_max_addr_len);
1552 		error = copyout_name(name, namelen, namelenp,
1553 		    (void *)sock_addrp, sock_addrlen);
1554 	}
1555 	kmem_free(sock_addrp, so->so_max_addr_len);
1556 rel_out:
1557 	releasef(sock);
1558 bad:	return (error != 0 ? set_errno(error) : 0);
1559 }
1560 
1561 /*ARGSUSED3*/
1562 int
getsockname(int sock,struct sockaddr * name,socklen_t * namelenp,int version)1563 getsockname(int sock, struct sockaddr *name, socklen_t *namelenp, int version)
1564 {
1565 	struct sonode *so;
1566 	int error;
1567 	socklen_t namelen, sock_addrlen;
1568 	struct sockaddr *sock_addrp;
1569 
1570 	dprint(1, ("getsockname(%d, %p, %p)\n",
1571 	    sock, (void *)name, (void *)namelenp));
1572 
1573 	if ((so = getsonode(sock, &error, NULL)) == NULL)
1574 		goto bad;
1575 
1576 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1577 	if (copyin(namelenp, &namelen, sizeof (namelen)) ||
1578 	    (name == NULL && namelen != 0)) {
1579 		error = EFAULT;
1580 		goto rel_out;
1581 	}
1582 
1583 	sock_addrlen = so->so_max_addr_len;
1584 	sock_addrp = (struct sockaddr *)kmem_alloc(sock_addrlen, KM_SLEEP);
1585 	if ((error = socket_getsockname(so, sock_addrp, &sock_addrlen,
1586 	    CRED())) == 0) {
1587 		ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1588 		ASSERT(sock_addrlen <= so->so_max_addr_len);
1589 		error = copyout_name(name, namelen, namelenp,
1590 		    (void *)sock_addrp, sock_addrlen);
1591 	}
1592 	kmem_free(sock_addrp, so->so_max_addr_len);
1593 rel_out:
1594 	releasef(sock);
1595 bad:	return (error != 0 ? set_errno(error) : 0);
1596 }
1597 
1598 /*ARGSUSED5*/
1599 int
getsockopt(int sock,int level,int option_name,void * option_value,socklen_t * option_lenp,int version)1600 getsockopt(int sock, int level, int option_name, void *option_value,
1601     socklen_t *option_lenp, int version)
1602 {
1603 	struct sonode *so;
1604 	socklen_t optlen, optlen_res;
1605 	void *optval;
1606 	int error;
1607 
1608 	dprint(1, ("getsockopt(%d, %d, %d, %p, %p)\n",
1609 	    sock, level, option_name, option_value, (void *)option_lenp));
1610 
1611 	if ((so = getsonode(sock, &error, NULL)) == NULL)
1612 		return (set_errno(error));
1613 
1614 	ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1615 	if (copyin(option_lenp, &optlen, sizeof (optlen))) {
1616 		releasef(sock);
1617 		return (set_errno(EFAULT));
1618 	}
1619 	/*
1620 	 * Verify that the length is not excessive to prevent
1621 	 * an application from consuming all of kernel memory.
1622 	 */
1623 	if (optlen > SO_MAXARGSIZE) {
1624 		error = EINVAL;
1625 		releasef(sock);
1626 		return (set_errno(error));
1627 	}
1628 	optval = kmem_alloc(optlen, KM_SLEEP);
1629 	optlen_res = optlen;
1630 	error = socket_getsockopt(so, level, option_name, optval,
1631 	    &optlen_res, (version != SOV_XPG4_2) ? 0 : _SOGETSOCKOPT_XPG4_2,
1632 	    CRED());
1633 	releasef(sock);
1634 	if (error) {
1635 		kmem_free(optval, optlen);
1636 		return (set_errno(error));
1637 	}
1638 	error = copyout_arg(option_value, optlen, option_lenp,
1639 	    optval, optlen_res);
1640 	kmem_free(optval, optlen);
1641 	if (error)
1642 		return (set_errno(error));
1643 	return (0);
1644 }
1645 
1646 /*ARGSUSED5*/
1647 int
setsockopt(int sock,int level,int option_name,void * option_value,socklen_t option_len,int version)1648 setsockopt(int sock, int level, int option_name, void *option_value,
1649     socklen_t option_len, int version)
1650 {
1651 	struct sonode *so;
1652 	intptr_t buffer[2];
1653 	void *optval = NULL;
1654 	int error;
1655 
1656 	dprint(1, ("setsockopt(%d, %d, %d, %p, %d)\n",
1657 	    sock, level, option_name, option_value, option_len));
1658 
1659 	if ((so = getsonode(sock, &error, NULL)) == NULL)
1660 		return (set_errno(error));
1661 
1662 	if (option_value != NULL) {
1663 		if (option_len != 0) {
1664 			/*
1665 			 * Verify that the length is not excessive to prevent
1666 			 * an application from consuming all of kernel memory.
1667 			 */
1668 			if (option_len > SO_MAXARGSIZE) {
1669 				error = EINVAL;
1670 				goto done2;
1671 			}
1672 			optval = option_len <= sizeof (buffer) ?
1673 			    &buffer : kmem_alloc((size_t)option_len, KM_SLEEP);
1674 			ASSERT(MUTEX_NOT_HELD(&so->so_lock));
1675 			if (copyin(option_value, optval, (size_t)option_len)) {
1676 				error = EFAULT;
1677 				goto done1;
1678 			}
1679 		}
1680 	} else
1681 		option_len = 0;
1682 
1683 	error = socket_setsockopt(so, level, option_name, optval,
1684 	    (t_uscalar_t)option_len, CRED());
1685 done1:
1686 	if (optval != buffer)
1687 		kmem_free(optval, (size_t)option_len);
1688 done2:
1689 	releasef(sock);
1690 	if (error)
1691 		return (set_errno(error));
1692 	return (0);
1693 }
1694 
1695 static int
sockconf_add_sock(int family,int type,int protocol,char * name)1696 sockconf_add_sock(int family, int type, int protocol, char *name)
1697 {
1698 	int error = 0;
1699 	char *kdevpath = NULL;
1700 	char *kmodule = NULL;
1701 	char *buf = NULL;
1702 	size_t pathlen = 0;
1703 	struct sockparams *sp;
1704 
1705 	if (name == NULL)
1706 		return (EINVAL);
1707 	/*
1708 	 * Copyin the name.
1709 	 * This also makes it possible to check for too long pathnames.
1710 	 * Compress the space needed for the name before passing it
1711 	 * to soconfig - soconfig will store the string until
1712 	 * the configuration is removed.
1713 	 */
1714 	buf = kmem_alloc(MAXPATHLEN, KM_SLEEP);
1715 	if ((error = copyinstr(name, buf, MAXPATHLEN, &pathlen)) != 0) {
1716 		kmem_free(buf, MAXPATHLEN);
1717 		return (error);
1718 	}
1719 	if (strncmp(buf, "/dev", strlen("/dev")) == 0) {
1720 		/* For device */
1721 		kdevpath = kmem_alloc(pathlen, KM_SLEEP);
1722 		bcopy(buf, kdevpath, pathlen);
1723 		kdevpath[pathlen - 1] = '\0';
1724 	} else {
1725 		/* For socket module */
1726 		kmodule = kmem_alloc(pathlen, KM_SLEEP);
1727 		bcopy(buf, kmodule, pathlen);
1728 		kmodule[pathlen - 1] = '\0';
1729 		pathlen = 0;
1730 	}
1731 	kmem_free(buf, MAXPATHLEN);
1732 
1733 	/* sockparams_create frees mod name and devpath upon failure */
1734 	sp = sockparams_create(family, type, protocol, kmodule,
1735 	    kdevpath, pathlen, 0, KM_SLEEP, &error);
1736 	if (sp != NULL) {
1737 		error = sockparams_add(sp);
1738 		if (error != 0)
1739 			sockparams_destroy(sp);
1740 	}
1741 
1742 	return (error);
1743 }
1744 
1745 static int
sockconf_remove_sock(int family,int type,int protocol)1746 sockconf_remove_sock(int family, int type, int protocol)
1747 {
1748 	return (sockparams_delete(family, type, protocol));
1749 }
1750 
1751 static int
sockconfig_remove_filter(const char * uname)1752 sockconfig_remove_filter(const char *uname)
1753 {
1754 	char kname[SOF_MAXNAMELEN];
1755 	size_t len;
1756 	int error;
1757 	sof_entry_t *ent;
1758 
1759 	if ((error = copyinstr(uname, kname, SOF_MAXNAMELEN, &len)) != 0)
1760 		return (error);
1761 
1762 	ent = sof_entry_remove_by_name(kname);
1763 	if (ent == NULL)
1764 		return (ENXIO);
1765 
1766 	mutex_enter(&ent->sofe_lock);
1767 	ASSERT(!(ent->sofe_flags & SOFEF_CONDEMED));
1768 	if (ent->sofe_refcnt == 0) {
1769 		mutex_exit(&ent->sofe_lock);
1770 		sof_entry_free(ent);
1771 	} else {
1772 		/* let the last socket free the filter */
1773 		ent->sofe_flags |= SOFEF_CONDEMED;
1774 		mutex_exit(&ent->sofe_lock);
1775 	}
1776 
1777 	return (0);
1778 }
1779 
1780 static int
sockconfig_add_filter(const char * uname,void * ufilpropp)1781 sockconfig_add_filter(const char *uname, void *ufilpropp)
1782 {
1783 	struct sockconfig_filter_props filprop;
1784 	sof_entry_t *ent;
1785 	int error;
1786 	size_t tuplesz, len;
1787 	char hintbuf[SOF_MAXNAMELEN];
1788 
1789 	ent = kmem_zalloc(sizeof (sof_entry_t), KM_SLEEP);
1790 	mutex_init(&ent->sofe_lock, NULL, MUTEX_DEFAULT, NULL);
1791 
1792 	if ((error = copyinstr(uname, ent->sofe_name, SOF_MAXNAMELEN,
1793 	    &len)) != 0) {
1794 		sof_entry_free(ent);
1795 		return (error);
1796 	}
1797 
1798 	if (get_udatamodel() == DATAMODEL_NATIVE) {
1799 		if (copyin(ufilpropp, &filprop, sizeof (filprop)) != 0) {
1800 			sof_entry_free(ent);
1801 			return (EFAULT);
1802 		}
1803 	}
1804 #ifdef	_SYSCALL32_IMPL
1805 	else {
1806 		struct sockconfig_filter_props32 filprop32;
1807 
1808 		if (copyin(ufilpropp, &filprop32, sizeof (filprop32)) != 0) {
1809 			sof_entry_free(ent);
1810 			return (EFAULT);
1811 		}
1812 		filprop.sfp_modname = (char *)(uintptr_t)filprop32.sfp_modname;
1813 		filprop.sfp_autoattach = filprop32.sfp_autoattach;
1814 		filprop.sfp_hint = filprop32.sfp_hint;
1815 		filprop.sfp_hintarg = (char *)(uintptr_t)filprop32.sfp_hintarg;
1816 		filprop.sfp_socktuple_cnt = filprop32.sfp_socktuple_cnt;
1817 		filprop.sfp_socktuple =
1818 		    (sof_socktuple_t *)(uintptr_t)filprop32.sfp_socktuple;
1819 	}
1820 #endif	/* _SYSCALL32_IMPL */
1821 
1822 	if ((error = copyinstr(filprop.sfp_modname, ent->sofe_modname,
1823 	    sizeof (ent->sofe_modname), &len)) != 0) {
1824 		sof_entry_free(ent);
1825 		return (error);
1826 	}
1827 
1828 	/*
1829 	 * A filter must specify at least one socket tuple.
1830 	 */
1831 	if (filprop.sfp_socktuple_cnt == 0 ||
1832 	    filprop.sfp_socktuple_cnt > SOF_MAXSOCKTUPLECNT) {
1833 		sof_entry_free(ent);
1834 		return (EINVAL);
1835 	}
1836 	ent->sofe_flags = filprop.sfp_autoattach ? SOFEF_AUTO : SOFEF_PROG;
1837 	ent->sofe_hint = filprop.sfp_hint;
1838 
1839 	/*
1840 	 * Verify the hint, and copy in the hint argument, if necessary.
1841 	 */
1842 	switch (ent->sofe_hint) {
1843 	case SOF_HINT_BEFORE:
1844 	case SOF_HINT_AFTER:
1845 		if ((error = copyinstr(filprop.sfp_hintarg, hintbuf,
1846 		    sizeof (hintbuf), &len)) != 0) {
1847 			sof_entry_free(ent);
1848 			return (error);
1849 		}
1850 		ent->sofe_hintarg = kmem_alloc(len, KM_SLEEP);
1851 		bcopy(hintbuf, ent->sofe_hintarg, len);
1852 		/* FALLTHRU */
1853 	case SOF_HINT_TOP:
1854 	case SOF_HINT_BOTTOM:
1855 		/* hints cannot be used with programmatic filters */
1856 		if (ent->sofe_flags & SOFEF_PROG) {
1857 			sof_entry_free(ent);
1858 			return (EINVAL);
1859 		}
1860 		break;
1861 	case SOF_HINT_NONE:
1862 		break;
1863 	default:
1864 		/* bad hint value */
1865 		sof_entry_free(ent);
1866 		return (EINVAL);
1867 	}
1868 
1869 	ent->sofe_socktuple_cnt = filprop.sfp_socktuple_cnt;
1870 	tuplesz = sizeof (sof_socktuple_t) * ent->sofe_socktuple_cnt;
1871 	ent->sofe_socktuple = kmem_alloc(tuplesz, KM_SLEEP);
1872 
1873 	if (get_udatamodel() == DATAMODEL_NATIVE) {
1874 		if (copyin(filprop.sfp_socktuple, ent->sofe_socktuple,
1875 		    tuplesz)) {
1876 			sof_entry_free(ent);
1877 			return (EFAULT);
1878 		}
1879 	}
1880 #ifdef	_SYSCALL32_IMPL
1881 	else {
1882 		int i;
1883 		caddr_t data = (caddr_t)filprop.sfp_socktuple;
1884 		sof_socktuple_t	*tup = ent->sofe_socktuple;
1885 		sof_socktuple32_t tup32;
1886 
1887 		tup = ent->sofe_socktuple;
1888 		for (i = 0; i < ent->sofe_socktuple_cnt; i++, tup++) {
1889 			ASSERT(tup < ent->sofe_socktuple + tuplesz);
1890 
1891 			if (copyin(data, &tup32, sizeof (tup32)) != 0) {
1892 				sof_entry_free(ent);
1893 				return (EFAULT);
1894 			}
1895 			tup->sofst_family = tup32.sofst_family;
1896 			tup->sofst_type = tup32.sofst_type;
1897 			tup->sofst_protocol = tup32.sofst_protocol;
1898 
1899 			data += sizeof (tup32);
1900 		}
1901 	}
1902 #endif	/* _SYSCALL32_IMPL */
1903 
1904 	/* Sockets can start using the filter as soon as the filter is added */
1905 	if ((error = sof_entry_add(ent)) != 0)
1906 		sof_entry_free(ent);
1907 
1908 	return (error);
1909 }
1910 
1911 /*
1912  * Socket configuration system call. It is used to add and remove
1913  * socket types.
1914  */
1915 int
sockconfig(int cmd,void * arg1,void * arg2,void * arg3,void * arg4)1916 sockconfig(int cmd, void *arg1, void *arg2, void *arg3, void *arg4)
1917 {
1918 	int error = 0;
1919 
1920 	if (secpolicy_net_config(CRED(), B_FALSE) != 0)
1921 		return (set_errno(EPERM));
1922 
1923 	switch (cmd) {
1924 	case SOCKCONFIG_ADD_SOCK:
1925 		error = sockconf_add_sock((int)(uintptr_t)arg1,
1926 		    (int)(uintptr_t)arg2, (int)(uintptr_t)arg3, arg4);
1927 		break;
1928 	case SOCKCONFIG_REMOVE_SOCK:
1929 		error = sockconf_remove_sock((int)(uintptr_t)arg1,
1930 		    (int)(uintptr_t)arg2, (int)(uintptr_t)arg3);
1931 		break;
1932 	case SOCKCONFIG_ADD_FILTER:
1933 		error = sockconfig_add_filter((const char *)arg1, arg2);
1934 		break;
1935 	case SOCKCONFIG_REMOVE_FILTER:
1936 		error = sockconfig_remove_filter((const char *)arg1);
1937 		break;
1938 	case SOCKCONFIG_GET_SOCKTABLE:
1939 		error = sockparams_copyout_socktable((int)(uintptr_t)arg1);
1940 		break;
1941 	default:
1942 #ifdef	DEBUG
1943 		cmn_err(CE_NOTE, "sockconfig: unkonwn subcommand %d", cmd);
1944 #endif
1945 		error = EINVAL;
1946 		break;
1947 	}
1948 
1949 	if (error != 0) {
1950 		eprintline(error);
1951 		return (set_errno(error));
1952 	}
1953 	return (0);
1954 }
1955 
1956 
1957 /*
1958  * Sendfile is implemented through two schemes, direct I/O or by
1959  * caching in the filesystem page cache. We cache the input file by
1960  * default and use direct I/O only if sendfile_max_size is set
1961  * appropriately as explained below. Note that this logic is consistent
1962  * with other filesystems where caching is turned on by default
1963  * unless explicitly turned off by using the DIRECTIO ioctl.
1964  *
1965  * We choose a slightly different scheme here. One can turn off
1966  * caching by setting sendfile_max_size to 0. One can also enable
1967  * caching of files <= sendfile_max_size by setting sendfile_max_size
1968  * to an appropriate value. By default sendfile_max_size is set to the
1969  * maximum value so that all files are cached. In future, we may provide
1970  * better interfaces for caching the file.
1971  *
1972  * Sendfile through Direct I/O (Zero copy)
1973  * --------------------------------------
1974  *
1975  * As disks are normally slower than the network, we can't have a
1976  * single thread that reads the disk and writes to the network. We
1977  * need to have parallelism. This is done by having the sendfile
1978  * thread create another thread that reads from the filesystem
1979  * and queues it for network processing. In this scheme, the data
1980  * is never copied anywhere i.e it is zero copy unlike the other
1981  * scheme.
1982  *
1983  * We have a sendfile queue (snfq) where each sendfile
1984  * request (snf_req_t) is queued for processing by a thread. Number
1985  * of threads is dynamically allocated and they exit if they are idling
1986  * beyond a specified amount of time. When each request (snf_req_t) is
1987  * processed by a thread, it produces a number of mblk_t structures to
1988  * be consumed by the sendfile thread. snf_deque and snf_enque are
1989  * used for consuming and producing mblks. Size of the filesystem
1990  * read is determined by the tunable (sendfile_read_size). A single
1991  * mblk holds sendfile_read_size worth of data (except the last
1992  * read of the file) which is sent down as a whole to the network.
1993  * sendfile_read_size is set to 1 MB as this seems to be the optimal
1994  * value for the UFS filesystem backed by a striped storage array.
1995  *
1996  * Synchronisation between read (producer) and write (consumer) threads.
1997  * --------------------------------------------------------------------
1998  *
1999  * sr_lock protects sr_ib_head and sr_ib_tail. The lock is held while
2000  * adding and deleting items in this list. Error can happen anytime
2001  * during read or write. There could be unprocessed mblks in the
2002  * sr_ib_XXX list when a read or write error occurs. Whenever error
2003  * is encountered, we need two things to happen :
2004  *
2005  * a) One of the threads need to clean the mblks.
2006  * b) When one thread encounters an error, the other should stop.
2007  *
2008  * For (a), we don't want to penalize the reader thread as it could do
2009  * some useful work processing other requests. For (b), the error can
2010  * be detected by examining sr_read_error or sr_write_error.
2011  * sr_lock protects sr_read_error and sr_write_error. If both reader and
2012  * writer encounters error, we need to report the write error back to
2013  * the application as that's what would have happened if the operations
2014  * were done sequentially. With this in mind, following should work :
2015  *
2016  *	- Check for errors before read or write.
2017  *	- If the reader encounters error, set the error in sr_read_error.
2018  *	  Check sr_write_error, if it is set, send cv_signal as it is
2019  *	  waiting for reader to complete. If it is not set, the writer
2020  *	  is either running sinking data to the network or blocked
2021  *        because of flow control. For handling the latter case, we
2022  *	  always send a signal. In any case, it will examine sr_read_error
2023  *	  and return. sr_read_error is marked with SR_READ_DONE to tell
2024  *	  the writer that the reader is done in all the cases.
2025  *	- If the writer encounters error, set the error in sr_write_error.
2026  *	  The reader thread is either blocked because of flow control or
2027  *	  running reading data from the disk. For the former, we need to
2028  *	  wakeup the thread. Again to keep it simple, we always wake up
2029  *	  the reader thread. Then, wait for the read thread to complete
2030  *	  if it is not done yet. Cleanup and return.
2031  *
2032  * High and low water marks for the read thread.
2033  * --------------------------------------------
2034  *
2035  * If sendfile() is used to send data over a slow network, we need to
2036  * make sure that the read thread does not produce data at a faster
2037  * rate than the network. This can happen if the disk is faster than
2038  * the network. In such a case, we don't want to build a very large queue.
2039  * But we would still like to get all of the network throughput possible.
2040  * This implies that network should never block waiting for data.
2041  * As there are lot of disk throughput/network throughput combinations
2042  * possible, it is difficult to come up with an accurate number.
2043  * A typical 10K RPM disk has a max seek latency 17ms and rotational
2044  * latency of 3ms for reading a disk block. Thus, the total latency to
2045  * initiate a new read, transfer data from the disk and queue for
2046  * transmission would take about a max of 25ms. Todays max transfer rate
2047  * for network is 100MB/sec. If the thread is blocked because of flow
2048  * control, it would take 25ms to get new data ready for transmission.
2049  * We have to make sure that network is not idling, while we are initiating
2050  * new transfers. So, at 100MB/sec, to keep network busy we would need
2051  * 2.5MB of data. Rounding off, we keep the low water mark to be 3MB of data.
2052  * We need to pick a high water mark so that the woken up thread would
2053  * do considerable work before blocking again to prevent thrashing. Currently,
2054  * we pick this to be 10 times that of the low water mark.
2055  *
2056  * Sendfile with segmap caching (One copy from page cache to mblks).
2057  * ----------------------------------------------------------------
2058  *
2059  * We use the segmap cache for caching the file, if the size of file
2060  * is <= sendfile_max_size. In this case we don't use threads as VM
2061  * is reasonably fast enough to keep up with the network. If the underlying
2062  * transport allows, we call segmap_getmapflt() to map MAXBSIZE (8K) worth
2063  * of data into segmap space, and use the virtual address from segmap
2064  * directly through desballoc() to avoid copy. Once the transport is done
2065  * with the data, the mapping will be released through segmap_release()
2066  * called by the call-back routine.
2067  *
2068  * If zero-copy is not allowed by the transport, we simply call VOP_READ()
2069  * to copy the data from the filesystem into our temporary network buffer.
2070  *
2071  * To disable caching, set sendfile_max_size to 0.
2072  */
2073 
2074 uint_t sendfile_read_size = 1024 * 1024;
2075 #define	SENDFILE_REQ_LOWAT	3 * 1024 * 1024
2076 uint_t sendfile_req_lowat = SENDFILE_REQ_LOWAT;
2077 uint_t sendfile_req_hiwat = 10 * SENDFILE_REQ_LOWAT;
2078 struct sendfile_stats sf_stats;
2079 struct sendfile_queue *snfq;
2080 clock_t snfq_timeout;
2081 off64_t sendfile_max_size;
2082 
2083 static void snf_enque(snf_req_t *, mblk_t *);
2084 static mblk_t *snf_deque(snf_req_t *);
2085 
2086 void
sendfile_init(void)2087 sendfile_init(void)
2088 {
2089 	snfq = kmem_zalloc(sizeof (struct sendfile_queue), KM_SLEEP);
2090 
2091 	mutex_init(&snfq->snfq_lock, NULL, MUTEX_DEFAULT, NULL);
2092 	cv_init(&snfq->snfq_cv, NULL, CV_DEFAULT, NULL);
2093 	snfq->snfq_max_threads = max_ncpus;
2094 	snfq_timeout = SNFQ_TIMEOUT;
2095 	/* Cache all files by default. */
2096 	sendfile_max_size = MAXOFFSET_T;
2097 }
2098 
2099 /*
2100  * Queues a mblk_t for network processing.
2101  */
2102 static void
snf_enque(snf_req_t * sr,mblk_t * mp)2103 snf_enque(snf_req_t *sr, mblk_t *mp)
2104 {
2105 	mp->b_next = NULL;
2106 	mutex_enter(&sr->sr_lock);
2107 	if (sr->sr_mp_head == NULL) {
2108 		sr->sr_mp_head = sr->sr_mp_tail = mp;
2109 		cv_signal(&sr->sr_cv);
2110 	} else {
2111 		sr->sr_mp_tail->b_next = mp;
2112 		sr->sr_mp_tail = mp;
2113 	}
2114 	sr->sr_qlen += MBLKL(mp);
2115 	while ((sr->sr_qlen > sr->sr_hiwat) &&
2116 	    (sr->sr_write_error == 0)) {
2117 		sf_stats.ss_full_waits++;
2118 		cv_wait(&sr->sr_cv, &sr->sr_lock);
2119 	}
2120 	mutex_exit(&sr->sr_lock);
2121 }
2122 
2123 /*
2124  * De-queues a mblk_t for network processing.
2125  */
2126 static mblk_t *
snf_deque(snf_req_t * sr)2127 snf_deque(snf_req_t *sr)
2128 {
2129 	mblk_t *mp;
2130 
2131 	mutex_enter(&sr->sr_lock);
2132 	/*
2133 	 * If we have encountered an error on read or read is
2134 	 * completed and no more mblks, return NULL.
2135 	 * We need to check for NULL sr_mp_head also as
2136 	 * the reads could have completed and there is
2137 	 * nothing more to come.
2138 	 */
2139 	if (((sr->sr_read_error & ~SR_READ_DONE) != 0) ||
2140 	    ((sr->sr_read_error & SR_READ_DONE) &&
2141 	    sr->sr_mp_head == NULL)) {
2142 		mutex_exit(&sr->sr_lock);
2143 		return (NULL);
2144 	}
2145 	/*
2146 	 * To start with neither SR_READ_DONE is marked nor
2147 	 * the error is set. When we wake up from cv_wait,
2148 	 * following are the possibilities :
2149 	 *
2150 	 *	a) sr_read_error is zero and mblks are queued.
2151 	 *	b) sr_read_error is set to SR_READ_DONE
2152 	 *	   and mblks are queued.
2153 	 *	c) sr_read_error is set to SR_READ_DONE
2154 	 *	   and no mblks.
2155 	 *	d) sr_read_error is set to some error other
2156 	 *	   than SR_READ_DONE.
2157 	 */
2158 
2159 	while ((sr->sr_read_error == 0) && (sr->sr_mp_head == NULL)) {
2160 		sf_stats.ss_empty_waits++;
2161 		cv_wait(&sr->sr_cv, &sr->sr_lock);
2162 	}
2163 	/* Handle (a) and (b) first  - the normal case. */
2164 	if (((sr->sr_read_error & ~SR_READ_DONE) == 0) &&
2165 	    (sr->sr_mp_head != NULL)) {
2166 		mp = sr->sr_mp_head;
2167 		sr->sr_mp_head = mp->b_next;
2168 		sr->sr_qlen -= MBLKL(mp);
2169 		if (sr->sr_qlen < sr->sr_lowat)
2170 			cv_signal(&sr->sr_cv);
2171 		mutex_exit(&sr->sr_lock);
2172 		mp->b_next = NULL;
2173 		return (mp);
2174 	}
2175 	/* Handle (c) and (d). */
2176 	mutex_exit(&sr->sr_lock);
2177 	return (NULL);
2178 }
2179 
2180 /*
2181  * Reads data from the filesystem and queues it for network processing.
2182  */
2183 void
snf_async_read(snf_req_t * sr)2184 snf_async_read(snf_req_t *sr)
2185 {
2186 	size_t iosize;
2187 	u_offset_t fileoff;
2188 	u_offset_t size;
2189 	int ret_size;
2190 	int error;
2191 	file_t *fp;
2192 	mblk_t *mp;
2193 	struct vnode *vp;
2194 	int extra = 0;
2195 	int maxblk = 0;
2196 	int wroff = 0;
2197 	struct sonode *so = NULL;
2198 
2199 	fp = sr->sr_fp;
2200 	size = sr->sr_file_size;
2201 	fileoff = sr->sr_file_off;
2202 
2203 	/*
2204 	 * Ignore the error for filesystems that doesn't support DIRECTIO.
2205 	 */
2206 	(void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_ON, 0,
2207 	    kcred, NULL, NULL);
2208 
2209 	vp = sr->sr_vp;
2210 	if (vp->v_type == VSOCK) {
2211 		stdata_t *stp;
2212 
2213 		/*
2214 		 * Get the extra space to insert a header and a trailer.
2215 		 */
2216 		so = VTOSO(vp);
2217 		stp = vp->v_stream;
2218 		if (stp == NULL) {
2219 			wroff = so->so_proto_props.sopp_wroff;
2220 			maxblk = so->so_proto_props.sopp_maxblk;
2221 			extra = wroff + so->so_proto_props.sopp_tail;
2222 		} else {
2223 			wroff = (int)(stp->sd_wroff);
2224 			maxblk = (int)(stp->sd_maxblk);
2225 			extra = wroff + (int)(stp->sd_tail);
2226 		}
2227 	}
2228 
2229 	while ((size != 0) && (sr->sr_write_error == 0)) {
2230 
2231 		iosize = (int)MIN(sr->sr_maxpsz, size);
2232 
2233 		/*
2234 		 * Socket filters can limit the mblk size,
2235 		 * so limit reads to maxblk if there are
2236 		 * filters present.
2237 		 */
2238 		if (vp->v_type == VSOCK &&
2239 		    so->so_filter_active > 0 && maxblk != INFPSZ)
2240 			iosize = (int)MIN(iosize, maxblk);
2241 
2242 		if (is_system_labeled()) {
2243 			mp = allocb_cred(iosize + extra, CRED(),
2244 			    curproc->p_pid);
2245 		} else {
2246 			mp = allocb(iosize + extra, BPRI_MED);
2247 		}
2248 		if (mp == NULL) {
2249 			error = EAGAIN;
2250 			break;
2251 		}
2252 
2253 		mp->b_rptr += wroff;
2254 
2255 		ret_size = soreadfile(fp, mp->b_rptr, fileoff, &error, iosize);
2256 
2257 		/* Error or Reached EOF ? */
2258 		if ((error != 0) || (ret_size == 0)) {
2259 			freeb(mp);
2260 			break;
2261 		}
2262 		mp->b_wptr = mp->b_rptr + ret_size;
2263 
2264 		snf_enque(sr, mp);
2265 		size -= ret_size;
2266 		fileoff += ret_size;
2267 	}
2268 	(void) VOP_IOCTL(fp->f_vnode, _FIODIRECTIO, DIRECTIO_OFF, 0,
2269 	    kcred, NULL, NULL);
2270 	mutex_enter(&sr->sr_lock);
2271 	sr->sr_read_error = error;
2272 	sr->sr_read_error |= SR_READ_DONE;
2273 	cv_signal(&sr->sr_cv);
2274 	mutex_exit(&sr->sr_lock);
2275 }
2276 
2277 void
snf_async_thread(void)2278 snf_async_thread(void)
2279 {
2280 	snf_req_t *sr;
2281 	callb_cpr_t cprinfo;
2282 	clock_t time_left = 1;
2283 
2284 	CALLB_CPR_INIT(&cprinfo, &snfq->snfq_lock, callb_generic_cpr, "snfq");
2285 
2286 	mutex_enter(&snfq->snfq_lock);
2287 	for (;;) {
2288 		/*
2289 		 * If we didn't find a entry, then block until woken up
2290 		 * again and then look through the queues again.
2291 		 */
2292 		while ((sr = snfq->snfq_req_head) == NULL) {
2293 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
2294 			if (time_left <= 0) {
2295 				snfq->snfq_svc_threads--;
2296 				CALLB_CPR_EXIT(&cprinfo);
2297 				thread_exit();
2298 				/* NOTREACHED */
2299 			}
2300 			snfq->snfq_idle_cnt++;
2301 
2302 			time_left = cv_reltimedwait(&snfq->snfq_cv,
2303 			    &snfq->snfq_lock, snfq_timeout, TR_CLOCK_TICK);
2304 			snfq->snfq_idle_cnt--;
2305 
2306 			CALLB_CPR_SAFE_END(&cprinfo, &snfq->snfq_lock);
2307 		}
2308 		snfq->snfq_req_head = sr->sr_next;
2309 		snfq->snfq_req_cnt--;
2310 		mutex_exit(&snfq->snfq_lock);
2311 		snf_async_read(sr);
2312 		mutex_enter(&snfq->snfq_lock);
2313 	}
2314 }
2315 
2316 
2317 snf_req_t *
create_thread(int operation,struct vnode * vp,file_t * fp,u_offset_t fileoff,u_offset_t size)2318 create_thread(int operation, struct vnode *vp, file_t *fp,
2319     u_offset_t fileoff, u_offset_t size)
2320 {
2321 	snf_req_t *sr;
2322 	stdata_t *stp;
2323 
2324 	sr = (snf_req_t *)kmem_zalloc(sizeof (snf_req_t), KM_SLEEP);
2325 
2326 	sr->sr_vp = vp;
2327 	sr->sr_fp = fp;
2328 	stp = vp->v_stream;
2329 
2330 	/*
2331 	 * store sd_qn_maxpsz into sr_maxpsz while we have stream head.
2332 	 * stream might be closed before thread returns from snf_async_read.
2333 	 */
2334 	if (stp != NULL && stp->sd_qn_maxpsz > 0) {
2335 		sr->sr_maxpsz = MIN(MAXBSIZE, stp->sd_qn_maxpsz);
2336 	} else {
2337 		sr->sr_maxpsz = MAXBSIZE;
2338 	}
2339 
2340 	sr->sr_operation = operation;
2341 	sr->sr_file_off = fileoff;
2342 	sr->sr_file_size = size;
2343 	sr->sr_hiwat = sendfile_req_hiwat;
2344 	sr->sr_lowat = sendfile_req_lowat;
2345 	mutex_init(&sr->sr_lock, NULL, MUTEX_DEFAULT, NULL);
2346 	cv_init(&sr->sr_cv, NULL, CV_DEFAULT, NULL);
2347 	/*
2348 	 * See whether we need another thread for servicing this
2349 	 * request. If there are already enough requests queued
2350 	 * for the threads, create one if not exceeding
2351 	 * snfq_max_threads.
2352 	 */
2353 	mutex_enter(&snfq->snfq_lock);
2354 	if (snfq->snfq_req_cnt >= snfq->snfq_idle_cnt &&
2355 	    snfq->snfq_svc_threads < snfq->snfq_max_threads) {
2356 		(void) thread_create(NULL, 0, &snf_async_thread, 0, 0, &p0,
2357 		    TS_RUN, minclsyspri);
2358 		snfq->snfq_svc_threads++;
2359 	}
2360 	if (snfq->snfq_req_head == NULL) {
2361 		snfq->snfq_req_head = snfq->snfq_req_tail = sr;
2362 		cv_signal(&snfq->snfq_cv);
2363 	} else {
2364 		snfq->snfq_req_tail->sr_next = sr;
2365 		snfq->snfq_req_tail = sr;
2366 	}
2367 	snfq->snfq_req_cnt++;
2368 	mutex_exit(&snfq->snfq_lock);
2369 	return (sr);
2370 }
2371 
2372 int
snf_direct_io(file_t * fp,file_t * rfp,u_offset_t fileoff,u_offset_t size,ssize_t * count)2373 snf_direct_io(file_t *fp, file_t *rfp, u_offset_t fileoff, u_offset_t size,
2374     ssize_t *count)
2375 {
2376 	snf_req_t *sr;
2377 	mblk_t *mp;
2378 	int iosize;
2379 	int error = 0;
2380 	short fflag;
2381 	struct vnode *vp;
2382 	int ksize;
2383 	struct nmsghdr msg;
2384 
2385 	ksize = 0;
2386 	*count = 0;
2387 	bzero(&msg, sizeof (msg));
2388 
2389 	vp = fp->f_vnode;
2390 	fflag = fp->f_flag;
2391 	if ((sr = create_thread(READ_OP, vp, rfp, fileoff, size)) == NULL)
2392 		return (EAGAIN);
2393 
2394 	/*
2395 	 * We check for read error in snf_deque. It has to check
2396 	 * for successful READ_DONE and return NULL, and we might
2397 	 * as well make an additional check there.
2398 	 */
2399 	while ((mp = snf_deque(sr)) != NULL) {
2400 
2401 		if (ISSIG(curthread, JUSTLOOKING)) {
2402 			freeb(mp);
2403 			error = EINTR;
2404 			break;
2405 		}
2406 		iosize = MBLKL(mp);
2407 
2408 		error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp);
2409 
2410 		if (error != 0) {
2411 			if (mp != NULL)
2412 				freeb(mp);
2413 			break;
2414 		}
2415 		ksize += iosize;
2416 	}
2417 	*count = ksize;
2418 
2419 	mutex_enter(&sr->sr_lock);
2420 	sr->sr_write_error = error;
2421 	/* Look at the big comments on why we cv_signal here. */
2422 	cv_signal(&sr->sr_cv);
2423 
2424 	/* Wait for the reader to complete always. */
2425 	while (!(sr->sr_read_error & SR_READ_DONE)) {
2426 		cv_wait(&sr->sr_cv, &sr->sr_lock);
2427 	}
2428 	/* If there is no write error, check for read error. */
2429 	if (error == 0)
2430 		error = (sr->sr_read_error & ~SR_READ_DONE);
2431 
2432 	if (error != 0) {
2433 		mblk_t *next_mp;
2434 
2435 		mp = sr->sr_mp_head;
2436 		while (mp != NULL) {
2437 			next_mp = mp->b_next;
2438 			mp->b_next = NULL;
2439 			freeb(mp);
2440 			mp = next_mp;
2441 		}
2442 	}
2443 	mutex_exit(&sr->sr_lock);
2444 	kmem_free(sr, sizeof (snf_req_t));
2445 	return (error);
2446 }
2447 
2448 /* Maximum no.of pages allocated by vpm for sendfile at a time */
2449 #define	SNF_VPMMAXPGS	(VPMMAXPGS/2)
2450 
2451 /*
2452  * Maximum no.of elements in the list returned by vpm, including
2453  * NULL for the last entry
2454  */
2455 #define	SNF_MAXVMAPS	(SNF_VPMMAXPGS + 1)
2456 
2457 typedef struct {
2458 	unsigned int	snfv_ref;
2459 	frtn_t		snfv_frtn;
2460 	vnode_t		*snfv_vp;
2461 	struct vmap	snfv_vml[SNF_MAXVMAPS];
2462 } snf_vmap_desbinfo;
2463 
2464 typedef struct {
2465 	frtn_t		snfi_frtn;
2466 	caddr_t		snfi_base;
2467 	uint_t		snfi_mapoff;
2468 	size_t		snfi_len;
2469 	vnode_t		*snfi_vp;
2470 } snf_smap_desbinfo;
2471 
2472 /*
2473  * The callback function used for vpm mapped mblks called when the last ref of
2474  * the mblk is dropped which normally occurs when TCP receives the ack. But it
2475  * can be the driver too due to lazy reclaim.
2476  */
2477 void
snf_vmap_desbfree(snf_vmap_desbinfo * snfv)2478 snf_vmap_desbfree(snf_vmap_desbinfo *snfv)
2479 {
2480 	ASSERT(snfv->snfv_ref != 0);
2481 	if (atomic_dec_32_nv(&snfv->snfv_ref) == 0) {
2482 		vpm_unmap_pages(snfv->snfv_vml, S_READ);
2483 		VN_RELE(snfv->snfv_vp);
2484 		kmem_free(snfv, sizeof (snf_vmap_desbinfo));
2485 	}
2486 }
2487 
2488 /*
2489  * The callback function used for segmap'ped mblks called when the last ref of
2490  * the mblk is dropped which normally occurs when TCP receives the ack. But it
2491  * can be the driver too due to lazy reclaim.
2492  */
2493 void
snf_smap_desbfree(snf_smap_desbinfo * snfi)2494 snf_smap_desbfree(snf_smap_desbinfo *snfi)
2495 {
2496 	if (! IS_KPM_ADDR(snfi->snfi_base)) {
2497 		/*
2498 		 * We don't need to call segmap_fault(F_SOFTUNLOCK) for
2499 		 * segmap_kpm as long as the latter never falls back to
2500 		 * "use_segmap_range". (See segmap_getmapflt().)
2501 		 *
2502 		 * Using S_OTHER saves an redundant hat_setref() in
2503 		 * segmap_unlock()
2504 		 */
2505 		(void) segmap_fault(kas.a_hat, segkmap,
2506 		    (caddr_t)(uintptr_t)(((uintptr_t)snfi->snfi_base +
2507 		    snfi->snfi_mapoff) & PAGEMASK), snfi->snfi_len,
2508 		    F_SOFTUNLOCK, S_OTHER);
2509 	}
2510 	(void) segmap_release(segkmap, snfi->snfi_base, SM_DONTNEED);
2511 	VN_RELE(snfi->snfi_vp);
2512 	kmem_free(snfi, sizeof (*snfi));
2513 }
2514 
2515 /*
2516  * Use segmap or vpm instead of bcopy to send down a desballoca'ed, mblk.
2517  * When segmap is used, the mblk contains a segmap slot of no more
2518  * than MAXBSIZE.
2519  *
2520  * With vpm, a maximum of SNF_MAXVMAPS page-sized mappings can be obtained
2521  * in each iteration and sent by socket_sendmblk until an error occurs or
2522  * the requested size has been transferred. An mblk is esballoca'ed from
2523  * each mapped page and a chain of these mblk is sent to the transport layer.
2524  * vpm will be called to unmap the pages when all mblks have been freed by
2525  * free_func.
2526  *
2527  * At the end of the whole sendfile() operation, we wait till the data from
2528  * the last mblk is ack'ed by the transport before returning so that the
2529  * caller of sendfile() can safely modify the file content.
2530  *
2531  * The caller of this function should make sure that total_size does not exceed
2532  * the actual file size of fvp.
2533  */
2534 int
snf_segmap(file_t * fp,vnode_t * fvp,u_offset_t fileoff,u_offset_t total_size,ssize_t * count,boolean_t nowait)2535 snf_segmap(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t total_size,
2536     ssize_t *count, boolean_t nowait)
2537 {
2538 	caddr_t base;
2539 	int mapoff;
2540 	vnode_t *vp;
2541 	mblk_t *mp = NULL;
2542 	int chain_size;
2543 	int error;
2544 	clock_t deadlk_wait;
2545 	short fflag;
2546 	int ksize;
2547 	struct vattr va;
2548 	boolean_t dowait = B_FALSE;
2549 	struct nmsghdr msg;
2550 
2551 	vp = fp->f_vnode;
2552 	fflag = fp->f_flag;
2553 	ksize = 0;
2554 	bzero(&msg, sizeof (msg));
2555 
2556 	for (;;) {
2557 		if (ISSIG(curthread, JUSTLOOKING)) {
2558 			error = EINTR;
2559 			break;
2560 		}
2561 
2562 		if (vpm_enable) {
2563 			snf_vmap_desbinfo *snfv;
2564 			mblk_t *nmp;
2565 			int mblk_size;
2566 			int maxsize;
2567 			int i;
2568 
2569 			mapoff = fileoff & PAGEOFFSET;
2570 			maxsize = MIN((SNF_VPMMAXPGS * PAGESIZE), total_size);
2571 
2572 			snfv = kmem_zalloc(sizeof (snf_vmap_desbinfo),
2573 			    KM_SLEEP);
2574 
2575 			/*
2576 			 * Get vpm mappings for maxsize with read access.
2577 			 * If the pages aren't available yet, we get
2578 			 * DEADLK, so wait and try again a little later using
2579 			 * an increasing wait. We might be here a long time.
2580 			 *
2581 			 * If delay_sig returns EINTR, be sure to exit and
2582 			 * pass it up to the caller.
2583 			 */
2584 			deadlk_wait = 0;
2585 			while ((error = vpm_map_pages(fvp, fileoff,
2586 			    (size_t)maxsize, (VPM_FETCHPAGE), snfv->snfv_vml,
2587 			    SNF_MAXVMAPS, NULL, S_READ)) == EDEADLK) {
2588 				deadlk_wait += (deadlk_wait < 5) ? 1 : 4;
2589 				if ((error = delay_sig(deadlk_wait)) != 0) {
2590 					break;
2591 				}
2592 			}
2593 			if (error != 0) {
2594 				kmem_free(snfv, sizeof (snf_vmap_desbinfo));
2595 				error = (error == EINTR) ? EINTR : EIO;
2596 				goto out;
2597 			}
2598 			snfv->snfv_frtn.free_func = snf_vmap_desbfree;
2599 			snfv->snfv_frtn.free_arg = (caddr_t)snfv;
2600 
2601 			/* Construct the mblk chain from the page mappings */
2602 			chain_size = 0;
2603 			for (i = 0; (snfv->snfv_vml[i].vs_addr != NULL) &&
2604 			    total_size > 0; i++) {
2605 				ASSERT(chain_size < maxsize);
2606 				mblk_size = MIN(snfv->snfv_vml[i].vs_len -
2607 				    mapoff, total_size);
2608 				nmp = esballoca(
2609 				    (uchar_t *)snfv->snfv_vml[i].vs_addr +
2610 				    mapoff, mblk_size, BPRI_HI,
2611 				    &snfv->snfv_frtn);
2612 
2613 				/*
2614 				 * We return EAGAIN after unmapping the pages
2615 				 * if we cannot allocate the the head of the
2616 				 * chain. Otherwise, we continue sending the
2617 				 * mblks constructed so far.
2618 				 */
2619 				if (nmp == NULL) {
2620 					if (i == 0) {
2621 						vpm_unmap_pages(snfv->snfv_vml,
2622 						    S_READ);
2623 						kmem_free(snfv,
2624 						    sizeof (snf_vmap_desbinfo));
2625 						error = EAGAIN;
2626 						goto out;
2627 					}
2628 					break;
2629 				}
2630 				/* Mark this dblk with the zero-copy flag */
2631 				nmp->b_datap->db_struioflag |= STRUIO_ZC;
2632 				nmp->b_wptr += mblk_size;
2633 				chain_size += mblk_size;
2634 				fileoff += mblk_size;
2635 				total_size -= mblk_size;
2636 				snfv->snfv_ref++;
2637 				mapoff = 0;
2638 				if (i > 0)
2639 					linkb(mp, nmp);
2640 				else
2641 					mp = nmp;
2642 			}
2643 			VN_HOLD(fvp);
2644 			snfv->snfv_vp = fvp;
2645 		} else {
2646 			/* vpm not supported. fallback to segmap */
2647 			snf_smap_desbinfo *snfi;
2648 
2649 			mapoff = fileoff & MAXBOFFSET;
2650 			chain_size = MAXBSIZE - mapoff;
2651 			if (chain_size > total_size)
2652 				chain_size = total_size;
2653 			/*
2654 			 * we don't forcefault because we'll call
2655 			 * segmap_fault(F_SOFTLOCK) next.
2656 			 *
2657 			 * S_READ will get the ref bit set (by either
2658 			 * segmap_getmapflt() or segmap_fault()) and page
2659 			 * shared locked.
2660 			 */
2661 			base = segmap_getmapflt(segkmap, fvp, fileoff,
2662 			    chain_size, segmap_kpm ? SM_FAULT : 0, S_READ);
2663 
2664 			snfi = kmem_alloc(sizeof (*snfi), KM_SLEEP);
2665 			snfi->snfi_len = (size_t)roundup(mapoff+chain_size,
2666 			    PAGESIZE)- (mapoff & PAGEMASK);
2667 			/*
2668 			 * We must call segmap_fault() even for segmap_kpm
2669 			 * because that's how error gets returned.
2670 			 * (segmap_getmapflt() never fails but segmap_fault()
2671 			 * does.)
2672 			 *
2673 			 * If the pages aren't available yet, we get
2674 			 * DEADLK, so wait and try again a little later using
2675 			 * an increasing wait. We might be here a long time.
2676 			 *
2677 			 * If delay_sig returns EINTR, be sure to exit and
2678 			 * pass it up to the caller.
2679 			 */
2680 			deadlk_wait = 0;
2681 			while ((error = FC_ERRNO(segmap_fault(kas.a_hat,
2682 			    segkmap, (caddr_t)(uintptr_t)(((uintptr_t)base +
2683 			    mapoff) & PAGEMASK), snfi->snfi_len, F_SOFTLOCK,
2684 			    S_READ))) == EDEADLK) {
2685 				deadlk_wait += (deadlk_wait < 5) ? 1 : 4;
2686 				if ((error = delay_sig(deadlk_wait)) != 0) {
2687 					break;
2688 				}
2689 			}
2690 			if (error != 0) {
2691 				(void) segmap_release(segkmap, base, 0);
2692 				kmem_free(snfi, sizeof (*snfi));
2693 				error = (error == EINTR) ? EINTR : EIO;
2694 				goto out;
2695 			}
2696 			snfi->snfi_frtn.free_func = snf_smap_desbfree;
2697 			snfi->snfi_frtn.free_arg = (caddr_t)snfi;
2698 			snfi->snfi_base = base;
2699 			snfi->snfi_mapoff = mapoff;
2700 			mp = esballoca((uchar_t *)base + mapoff, chain_size,
2701 			    BPRI_HI, &snfi->snfi_frtn);
2702 
2703 			if (mp == NULL) {
2704 				(void) segmap_fault(kas.a_hat, segkmap,
2705 				    (caddr_t)(uintptr_t)(((uintptr_t)base +
2706 				    mapoff) & PAGEMASK), snfi->snfi_len,
2707 				    F_SOFTUNLOCK, S_OTHER);
2708 				(void) segmap_release(segkmap, base, 0);
2709 				kmem_free(snfi, sizeof (*snfi));
2710 				freemsg(mp);
2711 				error = EAGAIN;
2712 				goto out;
2713 			}
2714 			VN_HOLD(fvp);
2715 			snfi->snfi_vp = fvp;
2716 			mp->b_wptr += chain_size;
2717 
2718 			/* Mark this dblk with the zero-copy flag */
2719 			mp->b_datap->db_struioflag |= STRUIO_ZC;
2720 			fileoff += chain_size;
2721 			total_size -= chain_size;
2722 		}
2723 
2724 		if (total_size == 0 && !nowait) {
2725 			ASSERT(!dowait);
2726 			dowait = B_TRUE;
2727 			mp->b_datap->db_struioflag |= STRUIO_ZCNOTIFY;
2728 		}
2729 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2730 		error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp);
2731 		if (error != 0) {
2732 			/*
2733 			 * mp contains the mblks that were not sent by
2734 			 * socket_sendmblk. Use its size to update *count
2735 			 */
2736 			*count = ksize + (chain_size - msgdsize(mp));
2737 			if (mp != NULL)
2738 				freemsg(mp);
2739 			return (error);
2740 		}
2741 		ksize += chain_size;
2742 		if (total_size == 0)
2743 			goto done;
2744 
2745 		(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2746 		va.va_mask = AT_SIZE;
2747 		error = VOP_GETATTR(fvp, &va, 0, kcred, NULL);
2748 		if (error)
2749 			break;
2750 		/* Read as much as possible. */
2751 		if (fileoff >= va.va_size)
2752 			break;
2753 		if (total_size + fileoff > va.va_size)
2754 			total_size = va.va_size - fileoff;
2755 	}
2756 out:
2757 	VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2758 done:
2759 	*count = ksize;
2760 	if (dowait) {
2761 		stdata_t *stp;
2762 
2763 		stp = vp->v_stream;
2764 		if (stp == NULL) {
2765 			struct sonode *so;
2766 			so = VTOSO(vp);
2767 			error = so_zcopy_wait(so);
2768 		} else {
2769 			mutex_enter(&stp->sd_lock);
2770 			while (!(stp->sd_flag & STZCNOTIFY)) {
2771 				if (cv_wait_sig(&stp->sd_zcopy_wait,
2772 				    &stp->sd_lock) == 0) {
2773 					error = EINTR;
2774 					break;
2775 				}
2776 			}
2777 			stp->sd_flag &= ~STZCNOTIFY;
2778 			mutex_exit(&stp->sd_lock);
2779 		}
2780 	}
2781 	return (error);
2782 }
2783 
2784 int
snf_cache(file_t * fp,vnode_t * fvp,u_offset_t fileoff,u_offset_t size,uint_t maxpsz,ssize_t * count)2785 snf_cache(file_t *fp, vnode_t *fvp, u_offset_t fileoff, u_offset_t size,
2786     uint_t maxpsz, ssize_t *count)
2787 {
2788 	struct vnode *vp;
2789 	mblk_t *mp;
2790 	int iosize;
2791 	int extra = 0;
2792 	int error;
2793 	short fflag;
2794 	int ksize;
2795 	int ioflag;
2796 	struct uio auio;
2797 	struct iovec aiov;
2798 	struct vattr va;
2799 	int maxblk = 0;
2800 	int wroff = 0;
2801 	struct sonode *so = NULL;
2802 	struct nmsghdr msg;
2803 
2804 	vp = fp->f_vnode;
2805 	if (vp->v_type == VSOCK) {
2806 		stdata_t *stp;
2807 
2808 		/*
2809 		 * Get the extra space to insert a header and a trailer.
2810 		 */
2811 		so = VTOSO(vp);
2812 		stp = vp->v_stream;
2813 		if (stp == NULL) {
2814 			wroff = so->so_proto_props.sopp_wroff;
2815 			maxblk = so->so_proto_props.sopp_maxblk;
2816 			extra = wroff + so->so_proto_props.sopp_tail;
2817 		} else {
2818 			wroff = (int)(stp->sd_wroff);
2819 			maxblk = (int)(stp->sd_maxblk);
2820 			extra = wroff + (int)(stp->sd_tail);
2821 		}
2822 	}
2823 	bzero(&msg, sizeof (msg));
2824 	fflag = fp->f_flag;
2825 	ksize = 0;
2826 	auio.uio_iov = &aiov;
2827 	auio.uio_iovcnt = 1;
2828 	auio.uio_segflg = UIO_SYSSPACE;
2829 	auio.uio_llimit = MAXOFFSET_T;
2830 	auio.uio_fmode = fflag;
2831 	auio.uio_extflg = UIO_COPY_CACHED;
2832 	ioflag = auio.uio_fmode & (FSYNC|FDSYNC|FRSYNC);
2833 	/* If read sync is not asked for, filter sync flags */
2834 	if ((ioflag & FRSYNC) == 0)
2835 		ioflag &= ~(FSYNC|FDSYNC);
2836 	for (;;) {
2837 		if (ISSIG(curthread, JUSTLOOKING)) {
2838 			error = EINTR;
2839 			break;
2840 		}
2841 		iosize = (int)MIN(maxpsz, size);
2842 
2843 		/*
2844 		 * Socket filters can limit the mblk size,
2845 		 * so limit reads to maxblk if there are
2846 		 * filters present.
2847 		 */
2848 		if (vp->v_type == VSOCK &&
2849 		    so->so_filter_active > 0 && maxblk != INFPSZ)
2850 			iosize = (int)MIN(iosize, maxblk);
2851 
2852 		if (is_system_labeled()) {
2853 			mp = allocb_cred(iosize + extra, CRED(),
2854 			    curproc->p_pid);
2855 		} else {
2856 			mp = allocb(iosize + extra, BPRI_MED);
2857 		}
2858 		if (mp == NULL) {
2859 			error = EAGAIN;
2860 			break;
2861 		}
2862 
2863 		mp->b_rptr += wroff;
2864 
2865 		aiov.iov_base = (caddr_t)mp->b_rptr;
2866 		aiov.iov_len = iosize;
2867 		auio.uio_loffset = fileoff;
2868 		auio.uio_resid = iosize;
2869 
2870 		error = VOP_READ(fvp, &auio, ioflag, fp->f_cred, NULL);
2871 		iosize -= auio.uio_resid;
2872 
2873 		if (error == EINTR && iosize != 0)
2874 			error = 0;
2875 
2876 		if (error != 0 || iosize == 0) {
2877 			freeb(mp);
2878 			break;
2879 		}
2880 		mp->b_wptr = mp->b_rptr + iosize;
2881 
2882 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2883 
2884 		error = socket_sendmblk(VTOSO(vp), &msg, fflag, CRED(), &mp);
2885 
2886 		if (error != 0) {
2887 			*count = ksize;
2888 			if (mp != NULL)
2889 				freeb(mp);
2890 			return (error);
2891 		}
2892 		ksize += iosize;
2893 		size -= iosize;
2894 		if (size == 0)
2895 			goto done;
2896 
2897 		fileoff += iosize;
2898 		(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2899 		va.va_mask = AT_SIZE;
2900 		error = VOP_GETATTR(fvp, &va, 0, kcred, NULL);
2901 		if (error)
2902 			break;
2903 		/* Read as much as possible. */
2904 		if (fileoff >= va.va_size)
2905 			size = 0;
2906 		else if (size + fileoff > va.va_size)
2907 			size = va.va_size - fileoff;
2908 	}
2909 	VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2910 done:
2911 	*count = ksize;
2912 	return (error);
2913 }
2914 
2915 #if defined(_SYSCALL32_IMPL) || defined(_ILP32)
2916 /*
2917  * Largefile support for 32 bit applications only.
2918  */
2919 int
sosendfile64(file_t * fp,file_t * rfp,const struct ksendfilevec64 * sfv,ssize32_t * count32)2920 sosendfile64(file_t *fp, file_t *rfp, const struct ksendfilevec64 *sfv,
2921     ssize32_t *count32)
2922 {
2923 	ssize32_t sfv_len;
2924 	u_offset_t sfv_off, va_size;
2925 	struct vnode *vp, *fvp, *realvp;
2926 	struct vattr va;
2927 	stdata_t *stp;
2928 	ssize_t count = 0;
2929 	int error = 0;
2930 	boolean_t dozcopy = B_FALSE;
2931 	uint_t maxpsz;
2932 
2933 	sfv_len = (ssize32_t)sfv->sfv_len;
2934 	if (sfv_len < 0) {
2935 		error = EINVAL;
2936 		goto out;
2937 	}
2938 
2939 	if (sfv_len == 0) goto out;
2940 
2941 	sfv_off = (u_offset_t)sfv->sfv_off;
2942 
2943 	/* Same checks as in pread */
2944 	if (sfv_off > MAXOFFSET_T) {
2945 		error = EINVAL;
2946 		goto out;
2947 	}
2948 	if (sfv_off + sfv_len > MAXOFFSET_T)
2949 		sfv_len = (ssize32_t)(MAXOFFSET_T - sfv_off);
2950 
2951 	/*
2952 	 * There are no more checks on sfv_len. So, we cast it to
2953 	 * u_offset_t and share the snf_direct_io/snf_cache code between
2954 	 * 32 bit and 64 bit.
2955 	 *
2956 	 * TODO: should do nbl_need_check() like read()?
2957 	 */
2958 	if (sfv_len > sendfile_max_size) {
2959 		sf_stats.ss_file_not_cached++;
2960 		error = snf_direct_io(fp, rfp, sfv_off, (u_offset_t)sfv_len,
2961 		    &count);
2962 		goto out;
2963 	}
2964 	fvp = rfp->f_vnode;
2965 	if (VOP_REALVP(fvp, &realvp, NULL) == 0)
2966 		fvp = realvp;
2967 	/*
2968 	 * Grab the lock as a reader to prevent the file size
2969 	 * from changing underneath.
2970 	 */
2971 	(void) VOP_RWLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2972 	va.va_mask = AT_SIZE;
2973 	error = VOP_GETATTR(fvp, &va, 0, kcred, NULL);
2974 	va_size = va.va_size;
2975 	if ((error != 0) || (va_size == 0) || (sfv_off >= va_size)) {
2976 		VOP_RWUNLOCK(fvp, V_WRITELOCK_FALSE, NULL);
2977 		goto out;
2978 	}
2979 	/* Read as much as possible. */
2980 	if (sfv_off + sfv_len > va_size)
2981 		sfv_len = va_size - sfv_off;
2982 
2983 	vp = fp->f_vnode;
2984 	stp = vp->v_stream;
2985 	/*
2986 	 * When the NOWAIT flag is not set, we enable zero-copy only if the
2987 	 * transfer size is large enough. This prevents performance loss
2988 	 * when the caller sends the file piece by piece.
2989 	 */
2990 	if (sfv_len >= MAXBSIZE && (sfv_len >= (va_size >> 1) ||
2991 	    (sfv->sfv_flag & SFV_NOWAIT) || sfv_len >= 0x1000000) &&
2992 	    !vn_has_flocks(fvp) && !(fvp->v_flag & VNOMAP)) {
2993 		uint_t copyflag;
2994 		copyflag = stp != NULL ? stp->sd_copyflag :
2995 		    VTOSO(vp)->so_proto_props.sopp_zcopyflag;
2996 		if ((copyflag & (STZCVMSAFE|STZCVMUNSAFE)) == 0) {
2997 			int on = 1;
2998 
2999 			if (socket_setsockopt(VTOSO(vp), SOL_SOCKET,
3000 			    SO_SND_COPYAVOID, &on, sizeof (on), CRED()) == 0)
3001 				dozcopy = B_TRUE;
3002 		} else {
3003 			dozcopy = copyflag & STZCVMSAFE;
3004 		}
3005 	}
3006 	if (dozcopy) {
3007 		sf_stats.ss_file_segmap++;
3008 		error = snf_segmap(fp, fvp, sfv_off, (u_offset_t)sfv_len,
3009 		    &count, ((sfv->sfv_flag & SFV_NOWAIT) != 0));
3010 	} else {
3011 		if (vp->v_type == VSOCK && stp == NULL) {
3012 			sonode_t *so = VTOSO(vp);
3013 			maxpsz = so->so_proto_props.sopp_maxpsz;
3014 		} else if (stp != NULL) {
3015 			maxpsz = stp->sd_qn_maxpsz;
3016 		} else {
3017 			maxpsz = maxphys;
3018 		}
3019 
3020 		if (maxpsz == INFPSZ)
3021 			maxpsz = maxphys;
3022 		else
3023 			maxpsz = roundup(maxpsz, MAXBSIZE);
3024 		sf_stats.ss_file_cached++;
3025 		error = snf_cache(fp, fvp, sfv_off, (u_offset_t)sfv_len,
3026 		    maxpsz, &count);
3027 	}
3028 out:
3029 	releasef(sfv->sfv_fd);
3030 	*count32 = (ssize32_t)count;
3031 	return (error);
3032 }
3033 #endif
3034 
3035 #ifdef _SYSCALL32_IMPL
3036 /*
3037  * recv32(), recvfrom32(), send32(), sendto32(): intentionally return a
3038  * ssize_t rather than ssize32_t; see the comments above read32 for details.
3039  */
3040 
3041 ssize_t
recv32(int32_t sock,caddr32_t buffer,size32_t len,int32_t flags)3042 recv32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags)
3043 {
3044 	return (recv(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags));
3045 }
3046 
3047 ssize_t
recvfrom32(int32_t sock,caddr32_t buffer,size32_t len,int32_t flags,caddr32_t name,caddr32_t namelenp)3048 recvfrom32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags,
3049     caddr32_t name, caddr32_t namelenp)
3050 {
3051 	return (recvfrom(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags,
3052 	    (void *)(uintptr_t)name, (void *)(uintptr_t)namelenp));
3053 }
3054 
3055 ssize_t
send32(int32_t sock,caddr32_t buffer,size32_t len,int32_t flags)3056 send32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags)
3057 {
3058 	return (send(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags));
3059 }
3060 
3061 ssize_t
sendto32(int32_t sock,caddr32_t buffer,size32_t len,int32_t flags,caddr32_t name,socklen_t namelen)3062 sendto32(int32_t sock, caddr32_t buffer, size32_t len, int32_t flags,
3063     caddr32_t name, socklen_t namelen)
3064 {
3065 	return (sendto(sock, (void *)(uintptr_t)buffer, (ssize32_t)len, flags,
3066 	    (void *)(uintptr_t)name, namelen));
3067 }
3068 #endif	/* _SYSCALL32_IMPL */
3069 
3070 /*
3071  * Function wrappers (mostly around the sonode switch) for
3072  * backward compatibility.
3073  */
3074 
3075 int
soaccept(struct sonode * so,int fflag,struct sonode ** nsop)3076 soaccept(struct sonode *so, int fflag, struct sonode **nsop)
3077 {
3078 	return (socket_accept(so, fflag, CRED(), nsop));
3079 }
3080 
3081 int
sobind(struct sonode * so,struct sockaddr * name,socklen_t namelen,int backlog,int flags)3082 sobind(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3083     int backlog, int flags)
3084 {
3085 	int	error;
3086 
3087 	error = socket_bind(so, name, namelen, flags, CRED());
3088 	if (error == 0 && backlog != 0)
3089 		return (socket_listen(so, backlog, CRED()));
3090 
3091 	return (error);
3092 }
3093 
3094 int
solisten(struct sonode * so,int backlog)3095 solisten(struct sonode *so, int backlog)
3096 {
3097 	return (socket_listen(so, backlog, CRED()));
3098 }
3099 
3100 int
soconnect(struct sonode * so,struct sockaddr * name,socklen_t namelen,int fflag,int flags)3101 soconnect(struct sonode *so, struct sockaddr *name, socklen_t namelen,
3102     int fflag, int flags)
3103 {
3104 	return (socket_connect(so, name, namelen, fflag, flags, CRED()));
3105 }
3106 
3107 int
sorecvmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop)3108 sorecvmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
3109 {
3110 	return (socket_recvmsg(so, msg, uiop, CRED()));
3111 }
3112 
3113 int
sosendmsg(struct sonode * so,struct nmsghdr * msg,struct uio * uiop)3114 sosendmsg(struct sonode *so, struct nmsghdr *msg, struct uio *uiop)
3115 {
3116 	return (socket_sendmsg(so, msg, uiop, CRED()));
3117 }
3118 
3119 int
soshutdown(struct sonode * so,int how)3120 soshutdown(struct sonode *so, int how)
3121 {
3122 	return (socket_shutdown(so, how, CRED()));
3123 }
3124 
3125 int
sogetsockopt(struct sonode * so,int level,int option_name,void * optval,socklen_t * optlenp,int flags)3126 sogetsockopt(struct sonode *so, int level, int option_name, void *optval,
3127     socklen_t *optlenp, int flags)
3128 {
3129 	return (socket_getsockopt(so, level, option_name, optval, optlenp,
3130 	    flags, CRED()));
3131 }
3132 
3133 int
sosetsockopt(struct sonode * so,int level,int option_name,const void * optval,t_uscalar_t optlen)3134 sosetsockopt(struct sonode *so, int level, int option_name, const void *optval,
3135     t_uscalar_t optlen)
3136 {
3137 	return (socket_setsockopt(so, level, option_name, optval, optlen,
3138 	    CRED()));
3139 }
3140 
3141 /*
3142  * Because this is backward compatibility interface it only needs to be
3143  * able to handle the creation of TPI sockfs sockets.
3144  */
3145 struct sonode *
socreate(struct sockparams * sp,int family,int type,int protocol,int version,int * errorp)3146 socreate(struct sockparams *sp, int family, int type, int protocol, int version,
3147     int *errorp)
3148 {
3149 	struct sonode *so;
3150 
3151 	ASSERT(sp != NULL);
3152 
3153 	so = sp->sp_smod_info->smod_sock_create_func(sp, family, type, protocol,
3154 	    version, SOCKET_SLEEP, errorp, CRED());
3155 	if (so == NULL) {
3156 		SOCKPARAMS_DEC_REF(sp);
3157 	} else {
3158 		if ((*errorp = SOP_INIT(so, NULL, CRED(), SOCKET_SLEEP)) == 0) {
3159 			/* Cannot fail, only bumps so_count */
3160 			(void) VOP_OPEN(&SOTOV(so), FREAD|FWRITE, CRED(), NULL);
3161 		} else {
3162 			socket_destroy(so);
3163 			so = NULL;
3164 		}
3165 	}
3166 	return (so);
3167 }
3168