xref: /freebsd/sys/kern/uipc_syscalls.c (revision a10cee30c94cf5944826d2a495e9cdf339dfbcc8)
1 /*-
2  * Copyright (c) 1982, 1986, 1989, 1990, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * sendfile(2) and related extensions:
6  * Copyright (c) 1998, David Greenman. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  * 4. Neither the name of the University nor the names of its contributors
17  *    may be used to endorse or promote products derived from this software
18  *    without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30  * SUCH DAMAGE.
31  *
32  *	@(#)uipc_syscalls.c	8.4 (Berkeley) 2/21/94
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
38 #include "opt_capsicum.h"
39 #include "opt_inet.h"
40 #include "opt_inet6.h"
41 #include "opt_sctp.h"
42 #include "opt_compat.h"
43 #include "opt_ktrace.h"
44 
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/capability.h>
48 #include <sys/kernel.h>
49 #include <sys/lock.h>
50 #include <sys/mutex.h>
51 #include <sys/sysproto.h>
52 #include <sys/malloc.h>
53 #include <sys/filedesc.h>
54 #include <sys/event.h>
55 #include <sys/proc.h>
56 #include <sys/fcntl.h>
57 #include <sys/file.h>
58 #include <sys/filio.h>
59 #include <sys/jail.h>
60 #include <sys/mount.h>
61 #include <sys/mbuf.h>
62 #include <sys/protosw.h>
63 #include <sys/sf_buf.h>
64 #include <sys/sysent.h>
65 #include <sys/socket.h>
66 #include <sys/socketvar.h>
67 #include <sys/signalvar.h>
68 #include <sys/syscallsubr.h>
69 #include <sys/sysctl.h>
70 #include <sys/uio.h>
71 #include <sys/vnode.h>
72 #ifdef KTRACE
73 #include <sys/ktrace.h>
74 #endif
75 #ifdef COMPAT_FREEBSD32
76 #include <compat/freebsd32/freebsd32_util.h>
77 #endif
78 
79 #include <net/vnet.h>
80 
81 #include <security/audit/audit.h>
82 #include <security/mac/mac_framework.h>
83 
84 #include <vm/vm.h>
85 #include <vm/vm_param.h>
86 #include <vm/vm_object.h>
87 #include <vm/vm_page.h>
88 #include <vm/vm_pageout.h>
89 #include <vm/vm_kern.h>
90 #include <vm/vm_extern.h>
91 
92 #if defined(INET) || defined(INET6)
93 #ifdef SCTP
94 #include <netinet/sctp.h>
95 #include <netinet/sctp_peeloff.h>
96 #endif /* SCTP */
97 #endif /* INET || INET6 */
98 
99 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
100 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
101 
102 static int accept1(struct thread *td, struct accept_args *uap, int compat);
103 static int do_sendfile(struct thread *td, struct sendfile_args *uap, int compat);
104 static int getsockname1(struct thread *td, struct getsockname_args *uap,
105 			int compat);
106 static int getpeername1(struct thread *td, struct getpeername_args *uap,
107 			int compat);
108 
109 /*
110  * NSFBUFS-related variables and associated sysctls
111  */
112 int nsfbufs;
113 int nsfbufspeak;
114 int nsfbufsused;
115 
116 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufs, CTLFLAG_RDTUN, &nsfbufs, 0,
117     "Maximum number of sendfile(2) sf_bufs available");
118 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufspeak, CTLFLAG_RD, &nsfbufspeak, 0,
119     "Number of sendfile(2) sf_bufs at peak usage");
120 SYSCTL_INT(_kern_ipc, OID_AUTO, nsfbufsused, CTLFLAG_RD, &nsfbufsused, 0,
121     "Number of sendfile(2) sf_bufs in use");
122 
123 /*
124  * Convert a user file descriptor to a kernel file entry and check that, if
125  * it is a capability, the right rights are present. A reference on the file
126  * entry is held upon returning.
127  */
128 static int
129 getsock_cap(struct filedesc *fdp, int fd, cap_rights_t rights,
130     struct file **fpp, u_int *fflagp)
131 {
132 	struct file *fp;
133 #ifdef CAPABILITIES
134 	struct file *fp_fromcap;
135 	int error;
136 #endif
137 
138 	if (fdp == NULL || (fp = fget_unlocked(fdp, fd)) == NULL)
139 		return (EBADF);
140 #ifdef CAPABILITIES
141 	/*
142 	 * If the file descriptor is for a capability, test rights and use
143 	 * the file descriptor referenced by the capability.
144 	 */
145 	error = cap_funwrap(fp, rights, &fp_fromcap);
146 	if (error) {
147 		fdrop(fp, curthread);
148 		return (error);
149 	}
150 	if (fp != fp_fromcap) {
151 		fhold(fp_fromcap);
152 		fdrop(fp, curthread);
153 		fp = fp_fromcap;
154 	}
155 #endif /* CAPABILITIES */
156 	if (fp->f_type != DTYPE_SOCKET) {
157 		fdrop(fp, curthread);
158 		return (ENOTSOCK);
159 	}
160 	if (fflagp != NULL)
161 		*fflagp = fp->f_flag;
162 	*fpp = fp;
163 	return (0);
164 }
165 
166 /*
167  * System call interface to the socket abstraction.
168  */
169 #if defined(COMPAT_43)
170 #define COMPAT_OLDSOCK
171 #endif
172 
173 int
174 sys_socket(td, uap)
175 	struct thread *td;
176 	struct socket_args /* {
177 		int	domain;
178 		int	type;
179 		int	protocol;
180 	} */ *uap;
181 {
182 	struct socket *so;
183 	struct file *fp;
184 	int fd, error;
185 
186 	AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);
187 #ifdef MAC
188 	error = mac_socket_check_create(td->td_ucred, uap->domain, uap->type,
189 	    uap->protocol);
190 	if (error)
191 		return (error);
192 #endif
193 	error = falloc(td, &fp, &fd, 0);
194 	if (error)
195 		return (error);
196 	/* An extra reference on `fp' has been held for us by falloc(). */
197 	error = socreate(uap->domain, &so, uap->type, uap->protocol,
198 	    td->td_ucred, td);
199 	if (error) {
200 		fdclose(td->td_proc->p_fd, fp, fd, td);
201 	} else {
202 		finit(fp, FREAD | FWRITE, DTYPE_SOCKET, so, &socketops);
203 		td->td_retval[0] = fd;
204 	}
205 	fdrop(fp, td);
206 	return (error);
207 }
208 
209 /* ARGSUSED */
210 int
211 sys_bind(td, uap)
212 	struct thread *td;
213 	struct bind_args /* {
214 		int	s;
215 		caddr_t	name;
216 		int	namelen;
217 	} */ *uap;
218 {
219 	struct sockaddr *sa;
220 	int error;
221 
222 	if ((error = getsockaddr(&sa, uap->name, uap->namelen)) != 0)
223 		return (error);
224 
225 	error = kern_bind(td, uap->s, sa);
226 	free(sa, M_SONAME);
227 	return (error);
228 }
229 
230 int
231 kern_bind(td, fd, sa)
232 	struct thread *td;
233 	int fd;
234 	struct sockaddr *sa;
235 {
236 	struct socket *so;
237 	struct file *fp;
238 	int error;
239 
240 	AUDIT_ARG_FD(fd);
241 	error = getsock_cap(td->td_proc->p_fd, fd, CAP_BIND, &fp, NULL);
242 	if (error)
243 		return (error);
244 	so = fp->f_data;
245 #ifdef KTRACE
246 	if (KTRPOINT(td, KTR_STRUCT))
247 		ktrsockaddr(sa);
248 #endif
249 #ifdef MAC
250 	error = mac_socket_check_bind(td->td_ucred, so, sa);
251 	if (error == 0)
252 #endif
253 		error = sobind(so, sa, td);
254 	fdrop(fp, td);
255 	return (error);
256 }
257 
258 /* ARGSUSED */
259 int
260 sys_listen(td, uap)
261 	struct thread *td;
262 	struct listen_args /* {
263 		int	s;
264 		int	backlog;
265 	} */ *uap;
266 {
267 	struct socket *so;
268 	struct file *fp;
269 	int error;
270 
271 	AUDIT_ARG_FD(uap->s);
272 	error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_LISTEN, &fp, NULL);
273 	if (error == 0) {
274 		so = fp->f_data;
275 #ifdef MAC
276 		error = mac_socket_check_listen(td->td_ucred, so);
277 		if (error == 0)
278 #endif
279 			error = solisten(so, uap->backlog, td);
280 		fdrop(fp, td);
281 	}
282 	return(error);
283 }
284 
285 /*
286  * accept1()
287  */
288 static int
289 accept1(td, uap, compat)
290 	struct thread *td;
291 	struct accept_args /* {
292 		int	s;
293 		struct sockaddr	* __restrict name;
294 		socklen_t	* __restrict anamelen;
295 	} */ *uap;
296 	int compat;
297 {
298 	struct sockaddr *name;
299 	socklen_t namelen;
300 	struct file *fp;
301 	int error;
302 
303 	if (uap->name == NULL)
304 		return (kern_accept(td, uap->s, NULL, NULL, NULL));
305 
306 	error = copyin(uap->anamelen, &namelen, sizeof (namelen));
307 	if (error)
308 		return (error);
309 
310 	error = kern_accept(td, uap->s, &name, &namelen, &fp);
311 
312 	/*
313 	 * return a namelen of zero for older code which might
314 	 * ignore the return value from accept.
315 	 */
316 	if (error) {
317 		(void) copyout(&namelen,
318 		    uap->anamelen, sizeof(*uap->anamelen));
319 		return (error);
320 	}
321 
322 	if (error == 0 && name != NULL) {
323 #ifdef COMPAT_OLDSOCK
324 		if (compat)
325 			((struct osockaddr *)name)->sa_family =
326 			    name->sa_family;
327 #endif
328 		error = copyout(name, uap->name, namelen);
329 	}
330 	if (error == 0)
331 		error = copyout(&namelen, uap->anamelen,
332 		    sizeof(namelen));
333 	if (error)
334 		fdclose(td->td_proc->p_fd, fp, td->td_retval[0], td);
335 	fdrop(fp, td);
336 	free(name, M_SONAME);
337 	return (error);
338 }
339 
340 int
341 kern_accept(struct thread *td, int s, struct sockaddr **name,
342     socklen_t *namelen, struct file **fp)
343 {
344 	struct filedesc *fdp;
345 	struct file *headfp, *nfp = NULL;
346 	struct sockaddr *sa = NULL;
347 	int error;
348 	struct socket *head, *so;
349 	int fd;
350 	u_int fflag;
351 	pid_t pgid;
352 	int tmp;
353 
354 	if (name) {
355 		*name = NULL;
356 		if (*namelen < 0)
357 			return (EINVAL);
358 	}
359 
360 	AUDIT_ARG_FD(s);
361 	fdp = td->td_proc->p_fd;
362 	error = getsock_cap(fdp, s, CAP_ACCEPT, &headfp, &fflag);
363 	if (error)
364 		return (error);
365 	head = headfp->f_data;
366 	if ((head->so_options & SO_ACCEPTCONN) == 0) {
367 		error = EINVAL;
368 		goto done;
369 	}
370 #ifdef MAC
371 	error = mac_socket_check_accept(td->td_ucred, head);
372 	if (error != 0)
373 		goto done;
374 #endif
375 	error = falloc(td, &nfp, &fd, 0);
376 	if (error)
377 		goto done;
378 	ACCEPT_LOCK();
379 	if ((head->so_state & SS_NBIO) && TAILQ_EMPTY(&head->so_comp)) {
380 		ACCEPT_UNLOCK();
381 		error = EWOULDBLOCK;
382 		goto noconnection;
383 	}
384 	while (TAILQ_EMPTY(&head->so_comp) && head->so_error == 0) {
385 		if (head->so_rcv.sb_state & SBS_CANTRCVMORE) {
386 			head->so_error = ECONNABORTED;
387 			break;
388 		}
389 		error = msleep(&head->so_timeo, &accept_mtx, PSOCK | PCATCH,
390 		    "accept", 0);
391 		if (error) {
392 			ACCEPT_UNLOCK();
393 			goto noconnection;
394 		}
395 	}
396 	if (head->so_error) {
397 		error = head->so_error;
398 		head->so_error = 0;
399 		ACCEPT_UNLOCK();
400 		goto noconnection;
401 	}
402 	so = TAILQ_FIRST(&head->so_comp);
403 	KASSERT(!(so->so_qstate & SQ_INCOMP), ("accept1: so SQ_INCOMP"));
404 	KASSERT(so->so_qstate & SQ_COMP, ("accept1: so not SQ_COMP"));
405 
406 	/*
407 	 * Before changing the flags on the socket, we have to bump the
408 	 * reference count.  Otherwise, if the protocol calls sofree(),
409 	 * the socket will be released due to a zero refcount.
410 	 */
411 	SOCK_LOCK(so);			/* soref() and so_state update */
412 	soref(so);			/* file descriptor reference */
413 
414 	TAILQ_REMOVE(&head->so_comp, so, so_list);
415 	head->so_qlen--;
416 	so->so_state |= (head->so_state & SS_NBIO);
417 	so->so_qstate &= ~SQ_COMP;
418 	so->so_head = NULL;
419 
420 	SOCK_UNLOCK(so);
421 	ACCEPT_UNLOCK();
422 
423 	/* An extra reference on `nfp' has been held for us by falloc(). */
424 	td->td_retval[0] = fd;
425 
426 	/* connection has been removed from the listen queue */
427 	KNOTE_UNLOCKED(&head->so_rcv.sb_sel.si_note, 0);
428 
429 	pgid = fgetown(&head->so_sigio);
430 	if (pgid != 0)
431 		fsetown(pgid, &so->so_sigio);
432 
433 	finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
434 	/* Sync socket nonblocking/async state with file flags */
435 	tmp = fflag & FNONBLOCK;
436 	(void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
437 	tmp = fflag & FASYNC;
438 	(void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
439 	sa = 0;
440 	error = soaccept(so, &sa);
441 	if (error) {
442 		/*
443 		 * return a namelen of zero for older code which might
444 		 * ignore the return value from accept.
445 		 */
446 		if (name)
447 			*namelen = 0;
448 		goto noconnection;
449 	}
450 	if (sa == NULL) {
451 		if (name)
452 			*namelen = 0;
453 		goto done;
454 	}
455 	if (name) {
456 		/* check sa_len before it is destroyed */
457 		if (*namelen > sa->sa_len)
458 			*namelen = sa->sa_len;
459 #ifdef KTRACE
460 		if (KTRPOINT(td, KTR_STRUCT))
461 			ktrsockaddr(sa);
462 #endif
463 		*name = sa;
464 		sa = NULL;
465 	}
466 noconnection:
467 	if (sa)
468 		free(sa, M_SONAME);
469 
470 	/*
471 	 * close the new descriptor, assuming someone hasn't ripped it
472 	 * out from under us.
473 	 */
474 	if (error)
475 		fdclose(fdp, nfp, fd, td);
476 
477 	/*
478 	 * Release explicitly held references before returning.  We return
479 	 * a reference on nfp to the caller on success if they request it.
480 	 */
481 done:
482 	if (fp != NULL) {
483 		if (error == 0) {
484 			*fp = nfp;
485 			nfp = NULL;
486 		} else
487 			*fp = NULL;
488 	}
489 	if (nfp != NULL)
490 		fdrop(nfp, td);
491 	fdrop(headfp, td);
492 	return (error);
493 }
494 
495 int
496 sys_accept(td, uap)
497 	struct thread *td;
498 	struct accept_args *uap;
499 {
500 
501 	return (accept1(td, uap, 0));
502 }
503 
504 #ifdef COMPAT_OLDSOCK
505 int
506 oaccept(td, uap)
507 	struct thread *td;
508 	struct accept_args *uap;
509 {
510 
511 	return (accept1(td, uap, 1));
512 }
513 #endif /* COMPAT_OLDSOCK */
514 
515 /* ARGSUSED */
516 int
517 sys_connect(td, uap)
518 	struct thread *td;
519 	struct connect_args /* {
520 		int	s;
521 		caddr_t	name;
522 		int	namelen;
523 	} */ *uap;
524 {
525 	struct sockaddr *sa;
526 	int error;
527 
528 	error = getsockaddr(&sa, uap->name, uap->namelen);
529 	if (error)
530 		return (error);
531 
532 	error = kern_connect(td, uap->s, sa);
533 	free(sa, M_SONAME);
534 	return (error);
535 }
536 
537 
538 int
539 kern_connect(td, fd, sa)
540 	struct thread *td;
541 	int fd;
542 	struct sockaddr *sa;
543 {
544 	struct socket *so;
545 	struct file *fp;
546 	int error;
547 	int interrupted = 0;
548 
549 	AUDIT_ARG_FD(fd);
550 	error = getsock_cap(td->td_proc->p_fd, fd, CAP_CONNECT, &fp, NULL);
551 	if (error)
552 		return (error);
553 	so = fp->f_data;
554 	if (so->so_state & SS_ISCONNECTING) {
555 		error = EALREADY;
556 		goto done1;
557 	}
558 #ifdef KTRACE
559 	if (KTRPOINT(td, KTR_STRUCT))
560 		ktrsockaddr(sa);
561 #endif
562 #ifdef MAC
563 	error = mac_socket_check_connect(td->td_ucred, so, sa);
564 	if (error)
565 		goto bad;
566 #endif
567 	error = soconnect(so, sa, td);
568 	if (error)
569 		goto bad;
570 	if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
571 		error = EINPROGRESS;
572 		goto done1;
573 	}
574 	SOCK_LOCK(so);
575 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
576 		error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
577 		    "connec", 0);
578 		if (error) {
579 			if (error == EINTR || error == ERESTART)
580 				interrupted = 1;
581 			break;
582 		}
583 	}
584 	if (error == 0) {
585 		error = so->so_error;
586 		so->so_error = 0;
587 	}
588 	SOCK_UNLOCK(so);
589 bad:
590 	if (!interrupted)
591 		so->so_state &= ~SS_ISCONNECTING;
592 	if (error == ERESTART)
593 		error = EINTR;
594 done1:
595 	fdrop(fp, td);
596 	return (error);
597 }
598 
599 int
600 kern_socketpair(struct thread *td, int domain, int type, int protocol,
601     int *rsv)
602 {
603 	struct filedesc *fdp = td->td_proc->p_fd;
604 	struct file *fp1, *fp2;
605 	struct socket *so1, *so2;
606 	int fd, error;
607 
608 	AUDIT_ARG_SOCKET(domain, type, protocol);
609 #ifdef MAC
610 	/* We might want to have a separate check for socket pairs. */
611 	error = mac_socket_check_create(td->td_ucred, domain, type,
612 	    protocol);
613 	if (error)
614 		return (error);
615 #endif
616 	error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
617 	if (error)
618 		return (error);
619 	error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
620 	if (error)
621 		goto free1;
622 	/* On success extra reference to `fp1' and 'fp2' is set by falloc. */
623 	error = falloc(td, &fp1, &fd, 0);
624 	if (error)
625 		goto free2;
626 	rsv[0] = fd;
627 	fp1->f_data = so1;	/* so1 already has ref count */
628 	error = falloc(td, &fp2, &fd, 0);
629 	if (error)
630 		goto free3;
631 	fp2->f_data = so2;	/* so2 already has ref count */
632 	rsv[1] = fd;
633 	error = soconnect2(so1, so2);
634 	if (error)
635 		goto free4;
636 	if (type == SOCK_DGRAM) {
637 		/*
638 		 * Datagram socket connection is asymmetric.
639 		 */
640 		 error = soconnect2(so2, so1);
641 		 if (error)
642 			goto free4;
643 	}
644 	finit(fp1, FREAD | FWRITE, DTYPE_SOCKET, fp1->f_data, &socketops);
645 	finit(fp2, FREAD | FWRITE, DTYPE_SOCKET, fp2->f_data, &socketops);
646 	fdrop(fp1, td);
647 	fdrop(fp2, td);
648 	return (0);
649 free4:
650 	fdclose(fdp, fp2, rsv[1], td);
651 	fdrop(fp2, td);
652 free3:
653 	fdclose(fdp, fp1, rsv[0], td);
654 	fdrop(fp1, td);
655 free2:
656 	if (so2 != NULL)
657 		(void)soclose(so2);
658 free1:
659 	if (so1 != NULL)
660 		(void)soclose(so1);
661 	return (error);
662 }
663 
664 int
665 sys_socketpair(struct thread *td, struct socketpair_args *uap)
666 {
667 	int error, sv[2];
668 
669 	error = kern_socketpair(td, uap->domain, uap->type,
670 	    uap->protocol, sv);
671 	if (error)
672 		return (error);
673 	error = copyout(sv, uap->rsv, 2 * sizeof(int));
674 	if (error) {
675 		(void)kern_close(td, sv[0]);
676 		(void)kern_close(td, sv[1]);
677 	}
678 	return (error);
679 }
680 
681 static int
682 sendit(td, s, mp, flags)
683 	struct thread *td;
684 	int s;
685 	struct msghdr *mp;
686 	int flags;
687 {
688 	struct mbuf *control;
689 	struct sockaddr *to;
690 	int error;
691 
692 #ifdef CAPABILITY_MODE
693 	if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
694 		return (ECAPMODE);
695 #endif
696 
697 	if (mp->msg_name != NULL) {
698 		error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
699 		if (error) {
700 			to = NULL;
701 			goto bad;
702 		}
703 		mp->msg_name = to;
704 	} else {
705 		to = NULL;
706 	}
707 
708 	if (mp->msg_control) {
709 		if (mp->msg_controllen < sizeof(struct cmsghdr)
710 #ifdef COMPAT_OLDSOCK
711 		    && mp->msg_flags != MSG_COMPAT
712 #endif
713 		) {
714 			error = EINVAL;
715 			goto bad;
716 		}
717 		error = sockargs(&control, mp->msg_control,
718 		    mp->msg_controllen, MT_CONTROL);
719 		if (error)
720 			goto bad;
721 #ifdef COMPAT_OLDSOCK
722 		if (mp->msg_flags == MSG_COMPAT) {
723 			struct cmsghdr *cm;
724 
725 			M_PREPEND(control, sizeof(*cm), M_WAIT);
726 			cm = mtod(control, struct cmsghdr *);
727 			cm->cmsg_len = control->m_len;
728 			cm->cmsg_level = SOL_SOCKET;
729 			cm->cmsg_type = SCM_RIGHTS;
730 		}
731 #endif
732 	} else {
733 		control = NULL;
734 	}
735 
736 	error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
737 
738 bad:
739 	if (to)
740 		free(to, M_SONAME);
741 	return (error);
742 }
743 
744 int
745 kern_sendit(td, s, mp, flags, control, segflg)
746 	struct thread *td;
747 	int s;
748 	struct msghdr *mp;
749 	int flags;
750 	struct mbuf *control;
751 	enum uio_seg segflg;
752 {
753 	struct file *fp;
754 	struct uio auio;
755 	struct iovec *iov;
756 	struct socket *so;
757 	int i, error;
758 	ssize_t len;
759 	cap_rights_t rights;
760 #ifdef KTRACE
761 	struct uio *ktruio = NULL;
762 #endif
763 
764 	AUDIT_ARG_FD(s);
765 	rights = CAP_WRITE;
766 	if (mp->msg_name != NULL)
767 		rights |= CAP_CONNECT;
768 	error = getsock_cap(td->td_proc->p_fd, s, rights, &fp, NULL);
769 	if (error)
770 		return (error);
771 	so = (struct socket *)fp->f_data;
772 
773 #ifdef KTRACE
774 	if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
775 		ktrsockaddr(mp->msg_name);
776 #endif
777 #ifdef MAC
778 	if (mp->msg_name != NULL) {
779 		error = mac_socket_check_connect(td->td_ucred, so,
780 		    mp->msg_name);
781 		if (error)
782 			goto bad;
783 	}
784 	error = mac_socket_check_send(td->td_ucred, so);
785 	if (error)
786 		goto bad;
787 #endif
788 
789 	auio.uio_iov = mp->msg_iov;
790 	auio.uio_iovcnt = mp->msg_iovlen;
791 	auio.uio_segflg = segflg;
792 	auio.uio_rw = UIO_WRITE;
793 	auio.uio_td = td;
794 	auio.uio_offset = 0;			/* XXX */
795 	auio.uio_resid = 0;
796 	iov = mp->msg_iov;
797 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
798 		if ((auio.uio_resid += iov->iov_len) < 0) {
799 			error = EINVAL;
800 			goto bad;
801 		}
802 	}
803 #ifdef KTRACE
804 	if (KTRPOINT(td, KTR_GENIO))
805 		ktruio = cloneuio(&auio);
806 #endif
807 	len = auio.uio_resid;
808 	error = sosend(so, mp->msg_name, &auio, 0, control, flags, td);
809 	if (error) {
810 		if (auio.uio_resid != len && (error == ERESTART ||
811 		    error == EINTR || error == EWOULDBLOCK))
812 			error = 0;
813 		/* Generation of SIGPIPE can be controlled per socket */
814 		if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
815 		    !(flags & MSG_NOSIGNAL)) {
816 			PROC_LOCK(td->td_proc);
817 			tdsignal(td, SIGPIPE);
818 			PROC_UNLOCK(td->td_proc);
819 		}
820 	}
821 	if (error == 0)
822 		td->td_retval[0] = len - auio.uio_resid;
823 #ifdef KTRACE
824 	if (ktruio != NULL) {
825 		ktruio->uio_resid = td->td_retval[0];
826 		ktrgenio(s, UIO_WRITE, ktruio, error);
827 	}
828 #endif
829 bad:
830 	fdrop(fp, td);
831 	return (error);
832 }
833 
834 int
835 sys_sendto(td, uap)
836 	struct thread *td;
837 	struct sendto_args /* {
838 		int	s;
839 		caddr_t	buf;
840 		size_t	len;
841 		int	flags;
842 		caddr_t	to;
843 		int	tolen;
844 	} */ *uap;
845 {
846 	struct msghdr msg;
847 	struct iovec aiov;
848 	int error;
849 
850 	msg.msg_name = uap->to;
851 	msg.msg_namelen = uap->tolen;
852 	msg.msg_iov = &aiov;
853 	msg.msg_iovlen = 1;
854 	msg.msg_control = 0;
855 #ifdef COMPAT_OLDSOCK
856 	msg.msg_flags = 0;
857 #endif
858 	aiov.iov_base = uap->buf;
859 	aiov.iov_len = uap->len;
860 	error = sendit(td, uap->s, &msg, uap->flags);
861 	return (error);
862 }
863 
864 #ifdef COMPAT_OLDSOCK
865 int
866 osend(td, uap)
867 	struct thread *td;
868 	struct osend_args /* {
869 		int	s;
870 		caddr_t	buf;
871 		int	len;
872 		int	flags;
873 	} */ *uap;
874 {
875 	struct msghdr msg;
876 	struct iovec aiov;
877 	int error;
878 
879 	msg.msg_name = 0;
880 	msg.msg_namelen = 0;
881 	msg.msg_iov = &aiov;
882 	msg.msg_iovlen = 1;
883 	aiov.iov_base = uap->buf;
884 	aiov.iov_len = uap->len;
885 	msg.msg_control = 0;
886 	msg.msg_flags = 0;
887 	error = sendit(td, uap->s, &msg, uap->flags);
888 	return (error);
889 }
890 
891 int
892 osendmsg(td, uap)
893 	struct thread *td;
894 	struct osendmsg_args /* {
895 		int	s;
896 		caddr_t	msg;
897 		int	flags;
898 	} */ *uap;
899 {
900 	struct msghdr msg;
901 	struct iovec *iov;
902 	int error;
903 
904 	error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
905 	if (error)
906 		return (error);
907 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
908 	if (error)
909 		return (error);
910 	msg.msg_iov = iov;
911 	msg.msg_flags = MSG_COMPAT;
912 	error = sendit(td, uap->s, &msg, uap->flags);
913 	free(iov, M_IOV);
914 	return (error);
915 }
916 #endif
917 
918 int
919 sys_sendmsg(td, uap)
920 	struct thread *td;
921 	struct sendmsg_args /* {
922 		int	s;
923 		caddr_t	msg;
924 		int	flags;
925 	} */ *uap;
926 {
927 	struct msghdr msg;
928 	struct iovec *iov;
929 	int error;
930 
931 	error = copyin(uap->msg, &msg, sizeof (msg));
932 	if (error)
933 		return (error);
934 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
935 	if (error)
936 		return (error);
937 	msg.msg_iov = iov;
938 #ifdef COMPAT_OLDSOCK
939 	msg.msg_flags = 0;
940 #endif
941 	error = sendit(td, uap->s, &msg, uap->flags);
942 	free(iov, M_IOV);
943 	return (error);
944 }
945 
946 int
947 kern_recvit(td, s, mp, fromseg, controlp)
948 	struct thread *td;
949 	int s;
950 	struct msghdr *mp;
951 	enum uio_seg fromseg;
952 	struct mbuf **controlp;
953 {
954 	struct uio auio;
955 	struct iovec *iov;
956 	int i;
957 	ssize_t len;
958 	int error;
959 	struct mbuf *m, *control = 0;
960 	caddr_t ctlbuf;
961 	struct file *fp;
962 	struct socket *so;
963 	struct sockaddr *fromsa = 0;
964 #ifdef KTRACE
965 	struct uio *ktruio = NULL;
966 #endif
967 
968 	if (controlp != NULL)
969 		*controlp = NULL;
970 
971 	AUDIT_ARG_FD(s);
972 	error = getsock_cap(td->td_proc->p_fd, s, CAP_READ, &fp, NULL);
973 	if (error)
974 		return (error);
975 	so = fp->f_data;
976 
977 #ifdef MAC
978 	error = mac_socket_check_receive(td->td_ucred, so);
979 	if (error) {
980 		fdrop(fp, td);
981 		return (error);
982 	}
983 #endif
984 
985 	auio.uio_iov = mp->msg_iov;
986 	auio.uio_iovcnt = mp->msg_iovlen;
987 	auio.uio_segflg = UIO_USERSPACE;
988 	auio.uio_rw = UIO_READ;
989 	auio.uio_td = td;
990 	auio.uio_offset = 0;			/* XXX */
991 	auio.uio_resid = 0;
992 	iov = mp->msg_iov;
993 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
994 		if ((auio.uio_resid += iov->iov_len) < 0) {
995 			fdrop(fp, td);
996 			return (EINVAL);
997 		}
998 	}
999 #ifdef KTRACE
1000 	if (KTRPOINT(td, KTR_GENIO))
1001 		ktruio = cloneuio(&auio);
1002 #endif
1003 	len = auio.uio_resid;
1004 	error = soreceive(so, &fromsa, &auio, (struct mbuf **)0,
1005 	    (mp->msg_control || controlp) ? &control : (struct mbuf **)0,
1006 	    &mp->msg_flags);
1007 	if (error) {
1008 		if (auio.uio_resid != len && (error == ERESTART ||
1009 		    error == EINTR || error == EWOULDBLOCK))
1010 			error = 0;
1011 	}
1012 #ifdef KTRACE
1013 	if (ktruio != NULL) {
1014 		ktruio->uio_resid = len - auio.uio_resid;
1015 		ktrgenio(s, UIO_READ, ktruio, error);
1016 	}
1017 #endif
1018 	if (error)
1019 		goto out;
1020 	td->td_retval[0] = len - auio.uio_resid;
1021 	if (mp->msg_name) {
1022 		len = mp->msg_namelen;
1023 		if (len <= 0 || fromsa == 0)
1024 			len = 0;
1025 		else {
1026 			/* save sa_len before it is destroyed by MSG_COMPAT */
1027 			len = MIN(len, fromsa->sa_len);
1028 #ifdef COMPAT_OLDSOCK
1029 			if (mp->msg_flags & MSG_COMPAT)
1030 				((struct osockaddr *)fromsa)->sa_family =
1031 				    fromsa->sa_family;
1032 #endif
1033 			if (fromseg == UIO_USERSPACE) {
1034 				error = copyout(fromsa, mp->msg_name,
1035 				    (unsigned)len);
1036 				if (error)
1037 					goto out;
1038 			} else
1039 				bcopy(fromsa, mp->msg_name, len);
1040 		}
1041 		mp->msg_namelen = len;
1042 	}
1043 	if (mp->msg_control && controlp == NULL) {
1044 #ifdef COMPAT_OLDSOCK
1045 		/*
1046 		 * We assume that old recvmsg calls won't receive access
1047 		 * rights and other control info, esp. as control info
1048 		 * is always optional and those options didn't exist in 4.3.
1049 		 * If we receive rights, trim the cmsghdr; anything else
1050 		 * is tossed.
1051 		 */
1052 		if (control && mp->msg_flags & MSG_COMPAT) {
1053 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
1054 			    SOL_SOCKET ||
1055 			    mtod(control, struct cmsghdr *)->cmsg_type !=
1056 			    SCM_RIGHTS) {
1057 				mp->msg_controllen = 0;
1058 				goto out;
1059 			}
1060 			control->m_len -= sizeof (struct cmsghdr);
1061 			control->m_data += sizeof (struct cmsghdr);
1062 		}
1063 #endif
1064 		len = mp->msg_controllen;
1065 		m = control;
1066 		mp->msg_controllen = 0;
1067 		ctlbuf = mp->msg_control;
1068 
1069 		while (m && len > 0) {
1070 			unsigned int tocopy;
1071 
1072 			if (len >= m->m_len)
1073 				tocopy = m->m_len;
1074 			else {
1075 				mp->msg_flags |= MSG_CTRUNC;
1076 				tocopy = len;
1077 			}
1078 
1079 			if ((error = copyout(mtod(m, caddr_t),
1080 					ctlbuf, tocopy)) != 0)
1081 				goto out;
1082 
1083 			ctlbuf += tocopy;
1084 			len -= tocopy;
1085 			m = m->m_next;
1086 		}
1087 		mp->msg_controllen = ctlbuf - (caddr_t)mp->msg_control;
1088 	}
1089 out:
1090 	fdrop(fp, td);
1091 #ifdef KTRACE
1092 	if (fromsa && KTRPOINT(td, KTR_STRUCT))
1093 		ktrsockaddr(fromsa);
1094 #endif
1095 	if (fromsa)
1096 		free(fromsa, M_SONAME);
1097 
1098 	if (error == 0 && controlp != NULL)
1099 		*controlp = control;
1100 	else  if (control)
1101 		m_freem(control);
1102 
1103 	return (error);
1104 }
1105 
1106 static int
1107 recvit(td, s, mp, namelenp)
1108 	struct thread *td;
1109 	int s;
1110 	struct msghdr *mp;
1111 	void *namelenp;
1112 {
1113 	int error;
1114 
1115 	error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1116 	if (error)
1117 		return (error);
1118 	if (namelenp) {
1119 		error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1120 #ifdef COMPAT_OLDSOCK
1121 		if (mp->msg_flags & MSG_COMPAT)
1122 			error = 0;	/* old recvfrom didn't check */
1123 #endif
1124 	}
1125 	return (error);
1126 }
1127 
1128 int
1129 sys_recvfrom(td, uap)
1130 	struct thread *td;
1131 	struct recvfrom_args /* {
1132 		int	s;
1133 		caddr_t	buf;
1134 		size_t	len;
1135 		int	flags;
1136 		struct sockaddr * __restrict	from;
1137 		socklen_t * __restrict fromlenaddr;
1138 	} */ *uap;
1139 {
1140 	struct msghdr msg;
1141 	struct iovec aiov;
1142 	int error;
1143 
1144 	if (uap->fromlenaddr) {
1145 		error = copyin(uap->fromlenaddr,
1146 		    &msg.msg_namelen, sizeof (msg.msg_namelen));
1147 		if (error)
1148 			goto done2;
1149 	} else {
1150 		msg.msg_namelen = 0;
1151 	}
1152 	msg.msg_name = uap->from;
1153 	msg.msg_iov = &aiov;
1154 	msg.msg_iovlen = 1;
1155 	aiov.iov_base = uap->buf;
1156 	aiov.iov_len = uap->len;
1157 	msg.msg_control = 0;
1158 	msg.msg_flags = uap->flags;
1159 	error = recvit(td, uap->s, &msg, uap->fromlenaddr);
1160 done2:
1161 	return(error);
1162 }
1163 
1164 #ifdef COMPAT_OLDSOCK
1165 int
1166 orecvfrom(td, uap)
1167 	struct thread *td;
1168 	struct recvfrom_args *uap;
1169 {
1170 
1171 	uap->flags |= MSG_COMPAT;
1172 	return (sys_recvfrom(td, uap));
1173 }
1174 #endif
1175 
1176 #ifdef COMPAT_OLDSOCK
1177 int
1178 orecv(td, uap)
1179 	struct thread *td;
1180 	struct orecv_args /* {
1181 		int	s;
1182 		caddr_t	buf;
1183 		int	len;
1184 		int	flags;
1185 	} */ *uap;
1186 {
1187 	struct msghdr msg;
1188 	struct iovec aiov;
1189 	int error;
1190 
1191 	msg.msg_name = 0;
1192 	msg.msg_namelen = 0;
1193 	msg.msg_iov = &aiov;
1194 	msg.msg_iovlen = 1;
1195 	aiov.iov_base = uap->buf;
1196 	aiov.iov_len = uap->len;
1197 	msg.msg_control = 0;
1198 	msg.msg_flags = uap->flags;
1199 	error = recvit(td, uap->s, &msg, NULL);
1200 	return (error);
1201 }
1202 
1203 /*
1204  * Old recvmsg.  This code takes advantage of the fact that the old msghdr
1205  * overlays the new one, missing only the flags, and with the (old) access
1206  * rights where the control fields are now.
1207  */
1208 int
1209 orecvmsg(td, uap)
1210 	struct thread *td;
1211 	struct orecvmsg_args /* {
1212 		int	s;
1213 		struct	omsghdr *msg;
1214 		int	flags;
1215 	} */ *uap;
1216 {
1217 	struct msghdr msg;
1218 	struct iovec *iov;
1219 	int error;
1220 
1221 	error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1222 	if (error)
1223 		return (error);
1224 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1225 	if (error)
1226 		return (error);
1227 	msg.msg_flags = uap->flags | MSG_COMPAT;
1228 	msg.msg_iov = iov;
1229 	error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1230 	if (msg.msg_controllen && error == 0)
1231 		error = copyout(&msg.msg_controllen,
1232 		    &uap->msg->msg_accrightslen, sizeof (int));
1233 	free(iov, M_IOV);
1234 	return (error);
1235 }
1236 #endif
1237 
1238 int
1239 sys_recvmsg(td, uap)
1240 	struct thread *td;
1241 	struct recvmsg_args /* {
1242 		int	s;
1243 		struct	msghdr *msg;
1244 		int	flags;
1245 	} */ *uap;
1246 {
1247 	struct msghdr msg;
1248 	struct iovec *uiov, *iov;
1249 	int error;
1250 
1251 	error = copyin(uap->msg, &msg, sizeof (msg));
1252 	if (error)
1253 		return (error);
1254 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1255 	if (error)
1256 		return (error);
1257 	msg.msg_flags = uap->flags;
1258 #ifdef COMPAT_OLDSOCK
1259 	msg.msg_flags &= ~MSG_COMPAT;
1260 #endif
1261 	uiov = msg.msg_iov;
1262 	msg.msg_iov = iov;
1263 	error = recvit(td, uap->s, &msg, NULL);
1264 	if (error == 0) {
1265 		msg.msg_iov = uiov;
1266 		error = copyout(&msg, uap->msg, sizeof(msg));
1267 	}
1268 	free(iov, M_IOV);
1269 	return (error);
1270 }
1271 
1272 /* ARGSUSED */
1273 int
1274 sys_shutdown(td, uap)
1275 	struct thread *td;
1276 	struct shutdown_args /* {
1277 		int	s;
1278 		int	how;
1279 	} */ *uap;
1280 {
1281 	struct socket *so;
1282 	struct file *fp;
1283 	int error;
1284 
1285 	AUDIT_ARG_FD(uap->s);
1286 	error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_SHUTDOWN, &fp,
1287 	    NULL);
1288 	if (error == 0) {
1289 		so = fp->f_data;
1290 		error = soshutdown(so, uap->how);
1291 		fdrop(fp, td);
1292 	}
1293 	return (error);
1294 }
1295 
1296 /* ARGSUSED */
1297 int
1298 sys_setsockopt(td, uap)
1299 	struct thread *td;
1300 	struct setsockopt_args /* {
1301 		int	s;
1302 		int	level;
1303 		int	name;
1304 		caddr_t	val;
1305 		int	valsize;
1306 	} */ *uap;
1307 {
1308 
1309 	return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1310 	    uap->val, UIO_USERSPACE, uap->valsize));
1311 }
1312 
1313 int
1314 kern_setsockopt(td, s, level, name, val, valseg, valsize)
1315 	struct thread *td;
1316 	int s;
1317 	int level;
1318 	int name;
1319 	void *val;
1320 	enum uio_seg valseg;
1321 	socklen_t valsize;
1322 {
1323 	int error;
1324 	struct socket *so;
1325 	struct file *fp;
1326 	struct sockopt sopt;
1327 
1328 	if (val == NULL && valsize != 0)
1329 		return (EFAULT);
1330 	if ((int)valsize < 0)
1331 		return (EINVAL);
1332 
1333 	sopt.sopt_dir = SOPT_SET;
1334 	sopt.sopt_level = level;
1335 	sopt.sopt_name = name;
1336 	sopt.sopt_val = val;
1337 	sopt.sopt_valsize = valsize;
1338 	switch (valseg) {
1339 	case UIO_USERSPACE:
1340 		sopt.sopt_td = td;
1341 		break;
1342 	case UIO_SYSSPACE:
1343 		sopt.sopt_td = NULL;
1344 		break;
1345 	default:
1346 		panic("kern_setsockopt called with bad valseg");
1347 	}
1348 
1349 	AUDIT_ARG_FD(s);
1350 	error = getsock_cap(td->td_proc->p_fd, s, CAP_SETSOCKOPT, &fp, NULL);
1351 	if (error == 0) {
1352 		so = fp->f_data;
1353 		error = sosetopt(so, &sopt);
1354 		fdrop(fp, td);
1355 	}
1356 	return(error);
1357 }
1358 
1359 /* ARGSUSED */
1360 int
1361 sys_getsockopt(td, uap)
1362 	struct thread *td;
1363 	struct getsockopt_args /* {
1364 		int	s;
1365 		int	level;
1366 		int	name;
1367 		void * __restrict	val;
1368 		socklen_t * __restrict avalsize;
1369 	} */ *uap;
1370 {
1371 	socklen_t valsize;
1372 	int	error;
1373 
1374 	if (uap->val) {
1375 		error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1376 		if (error)
1377 			return (error);
1378 	}
1379 
1380 	error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1381 	    uap->val, UIO_USERSPACE, &valsize);
1382 
1383 	if (error == 0)
1384 		error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1385 	return (error);
1386 }
1387 
1388 /*
1389  * Kernel version of getsockopt.
1390  * optval can be a userland or userspace. optlen is always a kernel pointer.
1391  */
1392 int
1393 kern_getsockopt(td, s, level, name, val, valseg, valsize)
1394 	struct thread *td;
1395 	int s;
1396 	int level;
1397 	int name;
1398 	void *val;
1399 	enum uio_seg valseg;
1400 	socklen_t *valsize;
1401 {
1402 	int error;
1403 	struct  socket *so;
1404 	struct file *fp;
1405 	struct	sockopt sopt;
1406 
1407 	if (val == NULL)
1408 		*valsize = 0;
1409 	if ((int)*valsize < 0)
1410 		return (EINVAL);
1411 
1412 	sopt.sopt_dir = SOPT_GET;
1413 	sopt.sopt_level = level;
1414 	sopt.sopt_name = name;
1415 	sopt.sopt_val = val;
1416 	sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1417 	switch (valseg) {
1418 	case UIO_USERSPACE:
1419 		sopt.sopt_td = td;
1420 		break;
1421 	case UIO_SYSSPACE:
1422 		sopt.sopt_td = NULL;
1423 		break;
1424 	default:
1425 		panic("kern_getsockopt called with bad valseg");
1426 	}
1427 
1428 	AUDIT_ARG_FD(s);
1429 	error = getsock_cap(td->td_proc->p_fd, s, CAP_GETSOCKOPT, &fp, NULL);
1430 	if (error == 0) {
1431 		so = fp->f_data;
1432 		error = sogetopt(so, &sopt);
1433 		*valsize = sopt.sopt_valsize;
1434 		fdrop(fp, td);
1435 	}
1436 	return (error);
1437 }
1438 
1439 /*
1440  * getsockname1() - Get socket name.
1441  */
1442 /* ARGSUSED */
1443 static int
1444 getsockname1(td, uap, compat)
1445 	struct thread *td;
1446 	struct getsockname_args /* {
1447 		int	fdes;
1448 		struct sockaddr * __restrict asa;
1449 		socklen_t * __restrict alen;
1450 	} */ *uap;
1451 	int compat;
1452 {
1453 	struct sockaddr *sa;
1454 	socklen_t len;
1455 	int error;
1456 
1457 	error = copyin(uap->alen, &len, sizeof(len));
1458 	if (error)
1459 		return (error);
1460 
1461 	error = kern_getsockname(td, uap->fdes, &sa, &len);
1462 	if (error)
1463 		return (error);
1464 
1465 	if (len != 0) {
1466 #ifdef COMPAT_OLDSOCK
1467 		if (compat)
1468 			((struct osockaddr *)sa)->sa_family = sa->sa_family;
1469 #endif
1470 		error = copyout(sa, uap->asa, (u_int)len);
1471 	}
1472 	free(sa, M_SONAME);
1473 	if (error == 0)
1474 		error = copyout(&len, uap->alen, sizeof(len));
1475 	return (error);
1476 }
1477 
1478 int
1479 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1480     socklen_t *alen)
1481 {
1482 	struct socket *so;
1483 	struct file *fp;
1484 	socklen_t len;
1485 	int error;
1486 
1487 	if (*alen < 0)
1488 		return (EINVAL);
1489 
1490 	AUDIT_ARG_FD(fd);
1491 	error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETSOCKNAME, &fp, NULL);
1492 	if (error)
1493 		return (error);
1494 	so = fp->f_data;
1495 	*sa = NULL;
1496 	CURVNET_SET(so->so_vnet);
1497 	error = (*so->so_proto->pr_usrreqs->pru_sockaddr)(so, sa);
1498 	CURVNET_RESTORE();
1499 	if (error)
1500 		goto bad;
1501 	if (*sa == NULL)
1502 		len = 0;
1503 	else
1504 		len = MIN(*alen, (*sa)->sa_len);
1505 	*alen = len;
1506 #ifdef KTRACE
1507 	if (KTRPOINT(td, KTR_STRUCT))
1508 		ktrsockaddr(*sa);
1509 #endif
1510 bad:
1511 	fdrop(fp, td);
1512 	if (error && *sa) {
1513 		free(*sa, M_SONAME);
1514 		*sa = NULL;
1515 	}
1516 	return (error);
1517 }
1518 
1519 int
1520 sys_getsockname(td, uap)
1521 	struct thread *td;
1522 	struct getsockname_args *uap;
1523 {
1524 
1525 	return (getsockname1(td, uap, 0));
1526 }
1527 
1528 #ifdef COMPAT_OLDSOCK
1529 int
1530 ogetsockname(td, uap)
1531 	struct thread *td;
1532 	struct getsockname_args *uap;
1533 {
1534 
1535 	return (getsockname1(td, uap, 1));
1536 }
1537 #endif /* COMPAT_OLDSOCK */
1538 
1539 /*
1540  * getpeername1() - Get name of peer for connected socket.
1541  */
1542 /* ARGSUSED */
1543 static int
1544 getpeername1(td, uap, compat)
1545 	struct thread *td;
1546 	struct getpeername_args /* {
1547 		int	fdes;
1548 		struct sockaddr * __restrict	asa;
1549 		socklen_t * __restrict	alen;
1550 	} */ *uap;
1551 	int compat;
1552 {
1553 	struct sockaddr *sa;
1554 	socklen_t len;
1555 	int error;
1556 
1557 	error = copyin(uap->alen, &len, sizeof (len));
1558 	if (error)
1559 		return (error);
1560 
1561 	error = kern_getpeername(td, uap->fdes, &sa, &len);
1562 	if (error)
1563 		return (error);
1564 
1565 	if (len != 0) {
1566 #ifdef COMPAT_OLDSOCK
1567 		if (compat)
1568 			((struct osockaddr *)sa)->sa_family = sa->sa_family;
1569 #endif
1570 		error = copyout(sa, uap->asa, (u_int)len);
1571 	}
1572 	free(sa, M_SONAME);
1573 	if (error == 0)
1574 		error = copyout(&len, uap->alen, sizeof(len));
1575 	return (error);
1576 }
1577 
1578 int
1579 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1580     socklen_t *alen)
1581 {
1582 	struct socket *so;
1583 	struct file *fp;
1584 	socklen_t len;
1585 	int error;
1586 
1587 	if (*alen < 0)
1588 		return (EINVAL);
1589 
1590 	AUDIT_ARG_FD(fd);
1591 	error = getsock_cap(td->td_proc->p_fd, fd, CAP_GETPEERNAME, &fp, NULL);
1592 	if (error)
1593 		return (error);
1594 	so = fp->f_data;
1595 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1596 		error = ENOTCONN;
1597 		goto done;
1598 	}
1599 	*sa = NULL;
1600 	CURVNET_SET(so->so_vnet);
1601 	error = (*so->so_proto->pr_usrreqs->pru_peeraddr)(so, sa);
1602 	CURVNET_RESTORE();
1603 	if (error)
1604 		goto bad;
1605 	if (*sa == NULL)
1606 		len = 0;
1607 	else
1608 		len = MIN(*alen, (*sa)->sa_len);
1609 	*alen = len;
1610 #ifdef KTRACE
1611 	if (KTRPOINT(td, KTR_STRUCT))
1612 		ktrsockaddr(*sa);
1613 #endif
1614 bad:
1615 	if (error && *sa) {
1616 		free(*sa, M_SONAME);
1617 		*sa = NULL;
1618 	}
1619 done:
1620 	fdrop(fp, td);
1621 	return (error);
1622 }
1623 
1624 int
1625 sys_getpeername(td, uap)
1626 	struct thread *td;
1627 	struct getpeername_args *uap;
1628 {
1629 
1630 	return (getpeername1(td, uap, 0));
1631 }
1632 
1633 #ifdef COMPAT_OLDSOCK
1634 int
1635 ogetpeername(td, uap)
1636 	struct thread *td;
1637 	struct ogetpeername_args *uap;
1638 {
1639 
1640 	/* XXX uap should have type `getpeername_args *' to begin with. */
1641 	return (getpeername1(td, (struct getpeername_args *)uap, 1));
1642 }
1643 #endif /* COMPAT_OLDSOCK */
1644 
1645 int
1646 sockargs(mp, buf, buflen, type)
1647 	struct mbuf **mp;
1648 	caddr_t buf;
1649 	int buflen, type;
1650 {
1651 	struct sockaddr *sa;
1652 	struct mbuf *m;
1653 	int error;
1654 
1655 	if ((u_int)buflen > MLEN) {
1656 #ifdef COMPAT_OLDSOCK
1657 		if (type == MT_SONAME && (u_int)buflen <= 112)
1658 			buflen = MLEN;		/* unix domain compat. hack */
1659 		else
1660 #endif
1661 			if ((u_int)buflen > MCLBYTES)
1662 				return (EINVAL);
1663 	}
1664 	m = m_get(M_WAIT, type);
1665 	if ((u_int)buflen > MLEN)
1666 		MCLGET(m, M_WAIT);
1667 	m->m_len = buflen;
1668 	error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
1669 	if (error)
1670 		(void) m_free(m);
1671 	else {
1672 		*mp = m;
1673 		if (type == MT_SONAME) {
1674 			sa = mtod(m, struct sockaddr *);
1675 
1676 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1677 			if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1678 				sa->sa_family = sa->sa_len;
1679 #endif
1680 			sa->sa_len = buflen;
1681 		}
1682 	}
1683 	return (error);
1684 }
1685 
1686 int
1687 getsockaddr(namp, uaddr, len)
1688 	struct sockaddr **namp;
1689 	caddr_t uaddr;
1690 	size_t len;
1691 {
1692 	struct sockaddr *sa;
1693 	int error;
1694 
1695 	if (len > SOCK_MAXADDRLEN)
1696 		return (ENAMETOOLONG);
1697 	if (len < offsetof(struct sockaddr, sa_data[0]))
1698 		return (EINVAL);
1699 	sa = malloc(len, M_SONAME, M_WAITOK);
1700 	error = copyin(uaddr, sa, len);
1701 	if (error) {
1702 		free(sa, M_SONAME);
1703 	} else {
1704 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1705 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
1706 			sa->sa_family = sa->sa_len;
1707 #endif
1708 		sa->sa_len = len;
1709 		*namp = sa;
1710 	}
1711 	return (error);
1712 }
1713 
1714 #include <sys/condvar.h>
1715 
1716 struct sendfile_sync {
1717 	struct mtx	mtx;
1718 	struct cv	cv;
1719 	unsigned 	count;
1720 };
1721 
1722 /*
1723  * Detach mapped page and release resources back to the system.
1724  */
1725 void
1726 sf_buf_mext(void *addr, void *args)
1727 {
1728 	vm_page_t m;
1729 	struct sendfile_sync *sfs;
1730 
1731 	m = sf_buf_page(args);
1732 	sf_buf_free(args);
1733 	vm_page_lock(m);
1734 	vm_page_unwire(m, 0);
1735 	/*
1736 	 * Check for the object going away on us. This can
1737 	 * happen since we don't hold a reference to it.
1738 	 * If so, we're responsible for freeing the page.
1739 	 */
1740 	if (m->wire_count == 0 && m->object == NULL)
1741 		vm_page_free(m);
1742 	vm_page_unlock(m);
1743 	if (addr == NULL)
1744 		return;
1745 	sfs = addr;
1746 	mtx_lock(&sfs->mtx);
1747 	KASSERT(sfs->count> 0, ("Sendfile sync botchup count == 0"));
1748 	if (--sfs->count == 0)
1749 		cv_signal(&sfs->cv);
1750 	mtx_unlock(&sfs->mtx);
1751 }
1752 
1753 /*
1754  * sendfile(2)
1755  *
1756  * int sendfile(int fd, int s, off_t offset, size_t nbytes,
1757  *	 struct sf_hdtr *hdtr, off_t *sbytes, int flags)
1758  *
1759  * Send a file specified by 'fd' and starting at 'offset' to a socket
1760  * specified by 's'. Send only 'nbytes' of the file or until EOF if nbytes ==
1761  * 0.  Optionally add a header and/or trailer to the socket output.  If
1762  * specified, write the total number of bytes sent into *sbytes.
1763  */
1764 int
1765 sys_sendfile(struct thread *td, struct sendfile_args *uap)
1766 {
1767 
1768 	return (do_sendfile(td, uap, 0));
1769 }
1770 
1771 static int
1772 do_sendfile(struct thread *td, struct sendfile_args *uap, int compat)
1773 {
1774 	struct sf_hdtr hdtr;
1775 	struct uio *hdr_uio, *trl_uio;
1776 	int error;
1777 
1778 	hdr_uio = trl_uio = NULL;
1779 
1780 	if (uap->hdtr != NULL) {
1781 		error = copyin(uap->hdtr, &hdtr, sizeof(hdtr));
1782 		if (error)
1783 			goto out;
1784 		if (hdtr.headers != NULL) {
1785 			error = copyinuio(hdtr.headers, hdtr.hdr_cnt, &hdr_uio);
1786 			if (error)
1787 				goto out;
1788 		}
1789 		if (hdtr.trailers != NULL) {
1790 			error = copyinuio(hdtr.trailers, hdtr.trl_cnt, &trl_uio);
1791 			if (error)
1792 				goto out;
1793 
1794 		}
1795 	}
1796 
1797 	error = kern_sendfile(td, uap, hdr_uio, trl_uio, compat);
1798 out:
1799 	if (hdr_uio)
1800 		free(hdr_uio, M_IOV);
1801 	if (trl_uio)
1802 		free(trl_uio, M_IOV);
1803 	return (error);
1804 }
1805 
1806 #ifdef COMPAT_FREEBSD4
1807 int
1808 freebsd4_sendfile(struct thread *td, struct freebsd4_sendfile_args *uap)
1809 {
1810 	struct sendfile_args args;
1811 
1812 	args.fd = uap->fd;
1813 	args.s = uap->s;
1814 	args.offset = uap->offset;
1815 	args.nbytes = uap->nbytes;
1816 	args.hdtr = uap->hdtr;
1817 	args.sbytes = uap->sbytes;
1818 	args.flags = uap->flags;
1819 
1820 	return (do_sendfile(td, &args, 1));
1821 }
1822 #endif /* COMPAT_FREEBSD4 */
1823 
1824 int
1825 kern_sendfile(struct thread *td, struct sendfile_args *uap,
1826     struct uio *hdr_uio, struct uio *trl_uio, int compat)
1827 {
1828 	struct file *sock_fp;
1829 	struct vnode *vp;
1830 	struct vm_object *obj = NULL;
1831 	struct socket *so = NULL;
1832 	struct mbuf *m = NULL;
1833 	struct sf_buf *sf;
1834 	struct vm_page *pg;
1835 	off_t off, xfsize, fsbytes = 0, sbytes = 0, rem = 0;
1836 	int error, hdrlen = 0, mnw = 0;
1837 	int vfslocked;
1838 	struct sendfile_sync *sfs = NULL;
1839 
1840 	/*
1841 	 * The file descriptor must be a regular file and have a
1842 	 * backing VM object.
1843 	 * File offset must be positive.  If it goes beyond EOF
1844 	 * we send only the header/trailer and no payload data.
1845 	 */
1846 	AUDIT_ARG_FD(uap->fd);
1847 	if ((error = fgetvp_read(td, uap->fd, CAP_READ, &vp)) != 0)
1848 		goto out;
1849 	vfslocked = VFS_LOCK_GIANT(vp->v_mount);
1850 	vn_lock(vp, LK_SHARED | LK_RETRY);
1851 	if (vp->v_type == VREG) {
1852 		obj = vp->v_object;
1853 		if (obj != NULL) {
1854 			/*
1855 			 * Temporarily increase the backing VM
1856 			 * object's reference count so that a forced
1857 			 * reclamation of its vnode does not
1858 			 * immediately destroy it.
1859 			 */
1860 			VM_OBJECT_LOCK(obj);
1861 			if ((obj->flags & OBJ_DEAD) == 0) {
1862 				vm_object_reference_locked(obj);
1863 				VM_OBJECT_UNLOCK(obj);
1864 			} else {
1865 				VM_OBJECT_UNLOCK(obj);
1866 				obj = NULL;
1867 			}
1868 		}
1869 	}
1870 	VOP_UNLOCK(vp, 0);
1871 	VFS_UNLOCK_GIANT(vfslocked);
1872 	if (obj == NULL) {
1873 		error = EINVAL;
1874 		goto out;
1875 	}
1876 	if (uap->offset < 0) {
1877 		error = EINVAL;
1878 		goto out;
1879 	}
1880 
1881 	/*
1882 	 * The socket must be a stream socket and connected.
1883 	 * Remember if it a blocking or non-blocking socket.
1884 	 */
1885 	if ((error = getsock_cap(td->td_proc->p_fd, uap->s, CAP_WRITE,
1886 	    &sock_fp, NULL)) != 0)
1887 		goto out;
1888 	so = sock_fp->f_data;
1889 	if (so->so_type != SOCK_STREAM) {
1890 		error = EINVAL;
1891 		goto out;
1892 	}
1893 	if ((so->so_state & SS_ISCONNECTED) == 0) {
1894 		error = ENOTCONN;
1895 		goto out;
1896 	}
1897 	/*
1898 	 * Do not wait on memory allocations but return ENOMEM for
1899 	 * caller to retry later.
1900 	 * XXX: Experimental.
1901 	 */
1902 	if (uap->flags & SF_MNOWAIT)
1903 		mnw = 1;
1904 
1905 	if (uap->flags & SF_SYNC) {
1906 		sfs = malloc(sizeof *sfs, M_TEMP, M_WAITOK | M_ZERO);
1907 		mtx_init(&sfs->mtx, "sendfile", NULL, MTX_DEF);
1908 		cv_init(&sfs->cv, "sendfile");
1909 	}
1910 
1911 #ifdef MAC
1912 	error = mac_socket_check_send(td->td_ucred, so);
1913 	if (error)
1914 		goto out;
1915 #endif
1916 
1917 	/* If headers are specified copy them into mbufs. */
1918 	if (hdr_uio != NULL) {
1919 		hdr_uio->uio_td = td;
1920 		hdr_uio->uio_rw = UIO_WRITE;
1921 		if (hdr_uio->uio_resid > 0) {
1922 			/*
1923 			 * In FBSD < 5.0 the nbytes to send also included
1924 			 * the header.  If compat is specified subtract the
1925 			 * header size from nbytes.
1926 			 */
1927 			if (compat) {
1928 				if (uap->nbytes > hdr_uio->uio_resid)
1929 					uap->nbytes -= hdr_uio->uio_resid;
1930 				else
1931 					uap->nbytes = 0;
1932 			}
1933 			m = m_uiotombuf(hdr_uio, (mnw ? M_NOWAIT : M_WAITOK),
1934 			    0, 0, 0);
1935 			if (m == NULL) {
1936 				error = mnw ? EAGAIN : ENOBUFS;
1937 				goto out;
1938 			}
1939 			hdrlen = m_length(m, NULL);
1940 		}
1941 	}
1942 
1943 	/*
1944 	 * Protect against multiple writers to the socket.
1945 	 *
1946 	 * XXXRW: Historically this has assumed non-interruptibility, so now
1947 	 * we implement that, but possibly shouldn't.
1948 	 */
1949 	(void)sblock(&so->so_snd, SBL_WAIT | SBL_NOINTR);
1950 
1951 	/*
1952 	 * Loop through the pages of the file, starting with the requested
1953 	 * offset. Get a file page (do I/O if necessary), map the file page
1954 	 * into an sf_buf, attach an mbuf header to the sf_buf, and queue
1955 	 * it on the socket.
1956 	 * This is done in two loops.  The inner loop turns as many pages
1957 	 * as it can, up to available socket buffer space, without blocking
1958 	 * into mbufs to have it bulk delivered into the socket send buffer.
1959 	 * The outer loop checks the state and available space of the socket
1960 	 * and takes care of the overall progress.
1961 	 */
1962 	for (off = uap->offset, rem = uap->nbytes; ; ) {
1963 		struct mbuf *mtail = NULL;
1964 		int loopbytes = 0;
1965 		int space = 0;
1966 		int done = 0;
1967 
1968 		/*
1969 		 * Check the socket state for ongoing connection,
1970 		 * no errors and space in socket buffer.
1971 		 * If space is low allow for the remainder of the
1972 		 * file to be processed if it fits the socket buffer.
1973 		 * Otherwise block in waiting for sufficient space
1974 		 * to proceed, or if the socket is nonblocking, return
1975 		 * to userland with EAGAIN while reporting how far
1976 		 * we've come.
1977 		 * We wait until the socket buffer has significant free
1978 		 * space to do bulk sends.  This makes good use of file
1979 		 * system read ahead and allows packet segmentation
1980 		 * offloading hardware to take over lots of work.  If
1981 		 * we were not careful here we would send off only one
1982 		 * sfbuf at a time.
1983 		 */
1984 		SOCKBUF_LOCK(&so->so_snd);
1985 		if (so->so_snd.sb_lowat < so->so_snd.sb_hiwat / 2)
1986 			so->so_snd.sb_lowat = so->so_snd.sb_hiwat / 2;
1987 retry_space:
1988 		if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
1989 			error = EPIPE;
1990 			SOCKBUF_UNLOCK(&so->so_snd);
1991 			goto done;
1992 		} else if (so->so_error) {
1993 			error = so->so_error;
1994 			so->so_error = 0;
1995 			SOCKBUF_UNLOCK(&so->so_snd);
1996 			goto done;
1997 		}
1998 		space = sbspace(&so->so_snd);
1999 		if (space < rem &&
2000 		    (space <= 0 ||
2001 		     space < so->so_snd.sb_lowat)) {
2002 			if (so->so_state & SS_NBIO) {
2003 				SOCKBUF_UNLOCK(&so->so_snd);
2004 				error = EAGAIN;
2005 				goto done;
2006 			}
2007 			/*
2008 			 * sbwait drops the lock while sleeping.
2009 			 * When we loop back to retry_space the
2010 			 * state may have changed and we retest
2011 			 * for it.
2012 			 */
2013 			error = sbwait(&so->so_snd);
2014 			/*
2015 			 * An error from sbwait usually indicates that we've
2016 			 * been interrupted by a signal. If we've sent anything
2017 			 * then return bytes sent, otherwise return the error.
2018 			 */
2019 			if (error) {
2020 				SOCKBUF_UNLOCK(&so->so_snd);
2021 				goto done;
2022 			}
2023 			goto retry_space;
2024 		}
2025 		SOCKBUF_UNLOCK(&so->so_snd);
2026 
2027 		/*
2028 		 * Reduce space in the socket buffer by the size of
2029 		 * the header mbuf chain.
2030 		 * hdrlen is set to 0 after the first loop.
2031 		 */
2032 		space -= hdrlen;
2033 
2034 		/*
2035 		 * Loop and construct maximum sized mbuf chain to be bulk
2036 		 * dumped into socket buffer.
2037 		 */
2038 		while (space > loopbytes) {
2039 			vm_pindex_t pindex;
2040 			vm_offset_t pgoff;
2041 			struct mbuf *m0;
2042 
2043 			VM_OBJECT_LOCK(obj);
2044 			/*
2045 			 * Calculate the amount to transfer.
2046 			 * Not to exceed a page, the EOF,
2047 			 * or the passed in nbytes.
2048 			 */
2049 			pgoff = (vm_offset_t)(off & PAGE_MASK);
2050 			xfsize = omin(PAGE_SIZE - pgoff,
2051 			    obj->un_pager.vnp.vnp_size - uap->offset -
2052 			    fsbytes - loopbytes);
2053 			if (uap->nbytes)
2054 				rem = (uap->nbytes - fsbytes - loopbytes);
2055 			else
2056 				rem = obj->un_pager.vnp.vnp_size -
2057 				    uap->offset - fsbytes - loopbytes;
2058 			xfsize = omin(rem, xfsize);
2059 			xfsize = omin(space - loopbytes, xfsize);
2060 			if (xfsize <= 0) {
2061 				VM_OBJECT_UNLOCK(obj);
2062 				done = 1;		/* all data sent */
2063 				break;
2064 			}
2065 
2066 			/*
2067 			 * Attempt to look up the page.  Allocate
2068 			 * if not found or wait and loop if busy.
2069 			 */
2070 			pindex = OFF_TO_IDX(off);
2071 			pg = vm_page_grab(obj, pindex, VM_ALLOC_NOBUSY |
2072 			    VM_ALLOC_NORMAL | VM_ALLOC_WIRED | VM_ALLOC_RETRY);
2073 
2074 			/*
2075 			 * Check if page is valid for what we need,
2076 			 * otherwise initiate I/O.
2077 			 * If we already turned some pages into mbufs,
2078 			 * send them off before we come here again and
2079 			 * block.
2080 			 */
2081 			if (pg->valid && vm_page_is_valid(pg, pgoff, xfsize))
2082 				VM_OBJECT_UNLOCK(obj);
2083 			else if (m != NULL)
2084 				error = EAGAIN;	/* send what we already got */
2085 			else if (uap->flags & SF_NODISKIO)
2086 				error = EBUSY;
2087 			else {
2088 				int bsize;
2089 				ssize_t resid;
2090 
2091 				/*
2092 				 * Ensure that our page is still around
2093 				 * when the I/O completes.
2094 				 */
2095 				vm_page_io_start(pg);
2096 				VM_OBJECT_UNLOCK(obj);
2097 
2098 				/*
2099 				 * Get the page from backing store.
2100 				 */
2101 				vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2102 				error = vn_lock(vp, LK_SHARED);
2103 				if (error != 0)
2104 					goto after_read;
2105 				bsize = vp->v_mount->mnt_stat.f_iosize;
2106 
2107 				/*
2108 				 * XXXMAC: Because we don't have fp->f_cred
2109 				 * here, we pass in NOCRED.  This is probably
2110 				 * wrong, but is consistent with our original
2111 				 * implementation.
2112 				 */
2113 				error = vn_rdwr(UIO_READ, vp, NULL, MAXBSIZE,
2114 				    trunc_page(off), UIO_NOCOPY, IO_NODELOCKED |
2115 				    IO_VMIO | ((MAXBSIZE / bsize) << IO_SEQSHIFT),
2116 				    td->td_ucred, NOCRED, &resid, td);
2117 				VOP_UNLOCK(vp, 0);
2118 			after_read:
2119 				VFS_UNLOCK_GIANT(vfslocked);
2120 				VM_OBJECT_LOCK(obj);
2121 				vm_page_io_finish(pg);
2122 				if (!error)
2123 					VM_OBJECT_UNLOCK(obj);
2124 				mbstat.sf_iocnt++;
2125 			}
2126 			if (error) {
2127 				vm_page_lock(pg);
2128 				vm_page_unwire(pg, 0);
2129 				/*
2130 				 * See if anyone else might know about
2131 				 * this page.  If not and it is not valid,
2132 				 * then free it.
2133 				 */
2134 				if (pg->wire_count == 0 && pg->valid == 0 &&
2135 				    pg->busy == 0 && !(pg->oflags & VPO_BUSY))
2136 					vm_page_free(pg);
2137 				vm_page_unlock(pg);
2138 				VM_OBJECT_UNLOCK(obj);
2139 				if (error == EAGAIN)
2140 					error = 0;	/* not a real error */
2141 				break;
2142 			}
2143 
2144 			/*
2145 			 * Get a sendfile buf.  When allocating the
2146 			 * first buffer for mbuf chain, we usually
2147 			 * wait as long as necessary, but this wait
2148 			 * can be interrupted.  For consequent
2149 			 * buffers, do not sleep, since several
2150 			 * threads might exhaust the buffers and then
2151 			 * deadlock.
2152 			 */
2153 			sf = sf_buf_alloc(pg, (mnw || m != NULL) ? SFB_NOWAIT :
2154 			    SFB_CATCH);
2155 			if (sf == NULL) {
2156 				mbstat.sf_allocfail++;
2157 				vm_page_lock(pg);
2158 				vm_page_unwire(pg, 0);
2159 				KASSERT(pg->object != NULL,
2160 				    ("kern_sendfile: object disappeared"));
2161 				vm_page_unlock(pg);
2162 				if (m == NULL)
2163 					error = (mnw ? EAGAIN : EINTR);
2164 				break;
2165 			}
2166 
2167 			/*
2168 			 * Get an mbuf and set it up as having
2169 			 * external storage.
2170 			 */
2171 			m0 = m_get((mnw ? M_NOWAIT : M_WAITOK), MT_DATA);
2172 			if (m0 == NULL) {
2173 				error = (mnw ? EAGAIN : ENOBUFS);
2174 				sf_buf_mext((void *)sf_buf_kva(sf), sf);
2175 				break;
2176 			}
2177 			MEXTADD(m0, sf_buf_kva(sf), PAGE_SIZE, sf_buf_mext,
2178 			    sfs, sf, M_RDONLY, EXT_SFBUF);
2179 			m0->m_data = (char *)sf_buf_kva(sf) + pgoff;
2180 			m0->m_len = xfsize;
2181 
2182 			/* Append to mbuf chain. */
2183 			if (mtail != NULL)
2184 				mtail->m_next = m0;
2185 			else if (m != NULL)
2186 				m_last(m)->m_next = m0;
2187 			else
2188 				m = m0;
2189 			mtail = m0;
2190 
2191 			/* Keep track of bits processed. */
2192 			loopbytes += xfsize;
2193 			off += xfsize;
2194 
2195 			if (sfs != NULL) {
2196 				mtx_lock(&sfs->mtx);
2197 				sfs->count++;
2198 				mtx_unlock(&sfs->mtx);
2199 			}
2200 		}
2201 
2202 		/* Add the buffer chain to the socket buffer. */
2203 		if (m != NULL) {
2204 			int mlen, err;
2205 
2206 			mlen = m_length(m, NULL);
2207 			SOCKBUF_LOCK(&so->so_snd);
2208 			if (so->so_snd.sb_state & SBS_CANTSENDMORE) {
2209 				error = EPIPE;
2210 				SOCKBUF_UNLOCK(&so->so_snd);
2211 				goto done;
2212 			}
2213 			SOCKBUF_UNLOCK(&so->so_snd);
2214 			CURVNET_SET(so->so_vnet);
2215 			/* Avoid error aliasing. */
2216 			err = (*so->so_proto->pr_usrreqs->pru_send)
2217 				    (so, 0, m, NULL, NULL, td);
2218 			CURVNET_RESTORE();
2219 			if (err == 0) {
2220 				/*
2221 				 * We need two counters to get the
2222 				 * file offset and nbytes to send
2223 				 * right:
2224 				 * - sbytes contains the total amount
2225 				 *   of bytes sent, including headers.
2226 				 * - fsbytes contains the total amount
2227 				 *   of bytes sent from the file.
2228 				 */
2229 				sbytes += mlen;
2230 				fsbytes += mlen;
2231 				if (hdrlen) {
2232 					fsbytes -= hdrlen;
2233 					hdrlen = 0;
2234 				}
2235 			} else if (error == 0)
2236 				error = err;
2237 			m = NULL;	/* pru_send always consumes */
2238 		}
2239 
2240 		/* Quit outer loop on error or when we're done. */
2241 		if (done)
2242 			break;
2243 		if (error)
2244 			goto done;
2245 	}
2246 
2247 	/*
2248 	 * Send trailers. Wimp out and use writev(2).
2249 	 */
2250 	if (trl_uio != NULL) {
2251 		sbunlock(&so->so_snd);
2252 		error = kern_writev(td, uap->s, trl_uio);
2253 		if (error == 0)
2254 			sbytes += td->td_retval[0];
2255 		goto out;
2256 	}
2257 
2258 done:
2259 	sbunlock(&so->so_snd);
2260 out:
2261 	/*
2262 	 * If there was no error we have to clear td->td_retval[0]
2263 	 * because it may have been set by writev.
2264 	 */
2265 	if (error == 0) {
2266 		td->td_retval[0] = 0;
2267 	}
2268 	if (uap->sbytes != NULL) {
2269 		copyout(&sbytes, uap->sbytes, sizeof(off_t));
2270 	}
2271 	if (obj != NULL)
2272 		vm_object_deallocate(obj);
2273 	if (vp != NULL) {
2274 		vfslocked = VFS_LOCK_GIANT(vp->v_mount);
2275 		vrele(vp);
2276 		VFS_UNLOCK_GIANT(vfslocked);
2277 	}
2278 	if (so)
2279 		fdrop(sock_fp, td);
2280 	if (m)
2281 		m_freem(m);
2282 
2283 	if (sfs != NULL) {
2284 		mtx_lock(&sfs->mtx);
2285 		if (sfs->count != 0)
2286 			cv_wait(&sfs->cv, &sfs->mtx);
2287 		KASSERT(sfs->count == 0, ("sendfile sync still busy"));
2288 		cv_destroy(&sfs->cv);
2289 		mtx_destroy(&sfs->mtx);
2290 		free(sfs, M_TEMP);
2291 	}
2292 
2293 	if (error == ERESTART)
2294 		error = EINTR;
2295 
2296 	return (error);
2297 }
2298 
2299 /*
2300  * SCTP syscalls.
2301  * Functionality only compiled in if SCTP is defined in the kernel Makefile,
2302  * otherwise all return EOPNOTSUPP.
2303  * XXX: We should make this loadable one day.
2304  */
2305 int
2306 sys_sctp_peeloff(td, uap)
2307 	struct thread *td;
2308 	struct sctp_peeloff_args /* {
2309 		int	sd;
2310 		caddr_t	name;
2311 	} */ *uap;
2312 {
2313 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2314 	struct file *nfp = NULL;
2315 	int error;
2316 	struct socket *head, *so;
2317 	int fd;
2318 	u_int fflag;
2319 
2320 	AUDIT_ARG_FD(uap->sd);
2321 	error = fgetsock(td, uap->sd, CAP_PEELOFF, &head, &fflag);
2322 	if (error)
2323 		goto done2;
2324 	if (head->so_proto->pr_protocol != IPPROTO_SCTP) {
2325 		error = EOPNOTSUPP;
2326 		goto done;
2327 	}
2328 	error = sctp_can_peel_off(head, (sctp_assoc_t)uap->name);
2329 	if (error)
2330 		goto done;
2331 	/*
2332 	 * At this point we know we do have a assoc to pull
2333 	 * we proceed to get the fd setup. This may block
2334 	 * but that is ok.
2335 	 */
2336 
2337 	error = falloc(td, &nfp, &fd, 0);
2338 	if (error)
2339 		goto done;
2340 	td->td_retval[0] = fd;
2341 
2342 	CURVNET_SET(head->so_vnet);
2343 	so = sonewconn(head, SS_ISCONNECTED);
2344 	if (so == NULL)
2345 		goto noconnection;
2346 	/*
2347 	 * Before changing the flags on the socket, we have to bump the
2348 	 * reference count.  Otherwise, if the protocol calls sofree(),
2349 	 * the socket will be released due to a zero refcount.
2350 	 */
2351         SOCK_LOCK(so);
2352         soref(so);                      /* file descriptor reference */
2353         SOCK_UNLOCK(so);
2354 
2355 	ACCEPT_LOCK();
2356 
2357 	TAILQ_REMOVE(&head->so_comp, so, so_list);
2358 	head->so_qlen--;
2359 	so->so_state |= (head->so_state & SS_NBIO);
2360 	so->so_state &= ~SS_NOFDREF;
2361 	so->so_qstate &= ~SQ_COMP;
2362 	so->so_head = NULL;
2363 	ACCEPT_UNLOCK();
2364 	finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
2365 	error = sctp_do_peeloff(head, so, (sctp_assoc_t)uap->name);
2366 	if (error)
2367 		goto noconnection;
2368 	if (head->so_sigio != NULL)
2369 		fsetown(fgetown(&head->so_sigio), &so->so_sigio);
2370 
2371 noconnection:
2372 	/*
2373 	 * close the new descriptor, assuming someone hasn't ripped it
2374 	 * out from under us.
2375 	 */
2376 	if (error)
2377 		fdclose(td->td_proc->p_fd, nfp, fd, td);
2378 
2379 	/*
2380 	 * Release explicitly held references before returning.
2381 	 */
2382 	CURVNET_RESTORE();
2383 done:
2384 	if (nfp != NULL)
2385 		fdrop(nfp, td);
2386 	fputsock(head);
2387 done2:
2388 	return (error);
2389 #else  /* SCTP */
2390 	return (EOPNOTSUPP);
2391 #endif /* SCTP */
2392 }
2393 
2394 int
2395 sys_sctp_generic_sendmsg (td, uap)
2396 	struct thread *td;
2397 	struct sctp_generic_sendmsg_args /* {
2398 		int sd,
2399 		caddr_t msg,
2400 		int mlen,
2401 		caddr_t to,
2402 		__socklen_t tolen,
2403 		struct sctp_sndrcvinfo *sinfo,
2404 		int flags
2405 	} */ *uap;
2406 {
2407 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2408 	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2409 	struct socket *so;
2410 	struct file *fp = NULL;
2411 	int error = 0, len;
2412 	struct sockaddr *to = NULL;
2413 #ifdef KTRACE
2414 	struct uio *ktruio = NULL;
2415 #endif
2416 	struct uio auio;
2417 	struct iovec iov[1];
2418 	cap_rights_t rights;
2419 
2420 	if (uap->sinfo) {
2421 		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2422 		if (error)
2423 			return (error);
2424 		u_sinfo = &sinfo;
2425 	}
2426 
2427 	rights = CAP_WRITE;
2428 	if (uap->tolen) {
2429 		error = getsockaddr(&to, uap->to, uap->tolen);
2430 		if (error) {
2431 			to = NULL;
2432 			goto sctp_bad2;
2433 		}
2434 		rights |= CAP_CONNECT;
2435 	}
2436 
2437 	AUDIT_ARG_FD(uap->sd);
2438 	error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2439 	if (error)
2440 		goto sctp_bad;
2441 #ifdef KTRACE
2442 	if (to && (KTRPOINT(td, KTR_STRUCT)))
2443 		ktrsockaddr(to);
2444 #endif
2445 
2446 	iov[0].iov_base = uap->msg;
2447 	iov[0].iov_len = uap->mlen;
2448 
2449 	so = (struct socket *)fp->f_data;
2450 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2451 		error = EOPNOTSUPP;
2452 		goto sctp_bad;
2453 	}
2454 #ifdef MAC
2455 	error = mac_socket_check_send(td->td_ucred, so);
2456 	if (error)
2457 		goto sctp_bad;
2458 #endif /* MAC */
2459 
2460 	auio.uio_iov =  iov;
2461 	auio.uio_iovcnt = 1;
2462 	auio.uio_segflg = UIO_USERSPACE;
2463 	auio.uio_rw = UIO_WRITE;
2464 	auio.uio_td = td;
2465 	auio.uio_offset = 0;			/* XXX */
2466 	auio.uio_resid = 0;
2467 	len = auio.uio_resid = uap->mlen;
2468 	CURVNET_SET(so->so_vnet);
2469 	error = sctp_lower_sosend(so, to, &auio,
2470 		    (struct mbuf *)NULL, (struct mbuf *)NULL,
2471 		    uap->flags, u_sinfo, td);
2472 	CURVNET_RESTORE();
2473 	if (error) {
2474 		if (auio.uio_resid != len && (error == ERESTART ||
2475 		    error == EINTR || error == EWOULDBLOCK))
2476 			error = 0;
2477 		/* Generation of SIGPIPE can be controlled per socket. */
2478 		if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2479 		    !(uap->flags & MSG_NOSIGNAL)) {
2480 			PROC_LOCK(td->td_proc);
2481 			tdsignal(td, SIGPIPE);
2482 			PROC_UNLOCK(td->td_proc);
2483 		}
2484 	}
2485 	if (error == 0)
2486 		td->td_retval[0] = len - auio.uio_resid;
2487 #ifdef KTRACE
2488 	if (ktruio != NULL) {
2489 		ktruio->uio_resid = td->td_retval[0];
2490 		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2491 	}
2492 #endif /* KTRACE */
2493 sctp_bad:
2494 	if (fp)
2495 		fdrop(fp, td);
2496 sctp_bad2:
2497 	if (to)
2498 		free(to, M_SONAME);
2499 	return (error);
2500 #else  /* SCTP */
2501 	return (EOPNOTSUPP);
2502 #endif /* SCTP */
2503 }
2504 
2505 int
2506 sys_sctp_generic_sendmsg_iov(td, uap)
2507 	struct thread *td;
2508 	struct sctp_generic_sendmsg_iov_args /* {
2509 		int sd,
2510 		struct iovec *iov,
2511 		int iovlen,
2512 		caddr_t to,
2513 		__socklen_t tolen,
2514 		struct sctp_sndrcvinfo *sinfo,
2515 		int flags
2516 	} */ *uap;
2517 {
2518 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2519 	struct sctp_sndrcvinfo sinfo, *u_sinfo = NULL;
2520 	struct socket *so;
2521 	struct file *fp = NULL;
2522 	int error=0, i;
2523 	ssize_t len;
2524 	struct sockaddr *to = NULL;
2525 #ifdef KTRACE
2526 	struct uio *ktruio = NULL;
2527 #endif
2528 	struct uio auio;
2529 	struct iovec *iov, *tiov;
2530 	cap_rights_t rights;
2531 
2532 	if (uap->sinfo) {
2533 		error = copyin(uap->sinfo, &sinfo, sizeof (sinfo));
2534 		if (error)
2535 			return (error);
2536 		u_sinfo = &sinfo;
2537 	}
2538 	rights = CAP_WRITE;
2539 	if (uap->tolen) {
2540 		error = getsockaddr(&to, uap->to, uap->tolen);
2541 		if (error) {
2542 			to = NULL;
2543 			goto sctp_bad2;
2544 		}
2545 		rights |= CAP_CONNECT;
2546 	}
2547 
2548 	AUDIT_ARG_FD(uap->sd);
2549 	error = getsock_cap(td->td_proc->p_fd, uap->sd, rights, &fp, NULL);
2550 	if (error)
2551 		goto sctp_bad1;
2552 
2553 #ifdef COMPAT_FREEBSD32
2554 	if (SV_CURPROC_FLAG(SV_ILP32))
2555 		error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2556 		    uap->iovlen, &iov, EMSGSIZE);
2557 	else
2558 #endif
2559 		error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2560 	if (error)
2561 		goto sctp_bad1;
2562 #ifdef KTRACE
2563 	if (to && (KTRPOINT(td, KTR_STRUCT)))
2564 		ktrsockaddr(to);
2565 #endif
2566 
2567 	so = (struct socket *)fp->f_data;
2568 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2569 		error = EOPNOTSUPP;
2570 		goto sctp_bad;
2571 	}
2572 #ifdef MAC
2573 	error = mac_socket_check_send(td->td_ucred, so);
2574 	if (error)
2575 		goto sctp_bad;
2576 #endif /* MAC */
2577 
2578 	auio.uio_iov = iov;
2579 	auio.uio_iovcnt = uap->iovlen;
2580 	auio.uio_segflg = UIO_USERSPACE;
2581 	auio.uio_rw = UIO_WRITE;
2582 	auio.uio_td = td;
2583 	auio.uio_offset = 0;			/* XXX */
2584 	auio.uio_resid = 0;
2585 	tiov = iov;
2586 	for (i = 0; i <uap->iovlen; i++, tiov++) {
2587 		if ((auio.uio_resid += tiov->iov_len) < 0) {
2588 			error = EINVAL;
2589 			goto sctp_bad;
2590 		}
2591 	}
2592 	len = auio.uio_resid;
2593 	CURVNET_SET(so->so_vnet);
2594 	error = sctp_lower_sosend(so, to, &auio,
2595 		    (struct mbuf *)NULL, (struct mbuf *)NULL,
2596 		    uap->flags, u_sinfo, td);
2597 	CURVNET_RESTORE();
2598 	if (error) {
2599 		if (auio.uio_resid != len && (error == ERESTART ||
2600 		    error == EINTR || error == EWOULDBLOCK))
2601 			error = 0;
2602 		/* Generation of SIGPIPE can be controlled per socket */
2603 		if (error == EPIPE && !(so->so_options & SO_NOSIGPIPE) &&
2604 		    !(uap->flags & MSG_NOSIGNAL)) {
2605 			PROC_LOCK(td->td_proc);
2606 			tdsignal(td, SIGPIPE);
2607 			PROC_UNLOCK(td->td_proc);
2608 		}
2609 	}
2610 	if (error == 0)
2611 		td->td_retval[0] = len - auio.uio_resid;
2612 #ifdef KTRACE
2613 	if (ktruio != NULL) {
2614 		ktruio->uio_resid = td->td_retval[0];
2615 		ktrgenio(uap->sd, UIO_WRITE, ktruio, error);
2616 	}
2617 #endif /* KTRACE */
2618 sctp_bad:
2619 	free(iov, M_IOV);
2620 sctp_bad1:
2621 	if (fp)
2622 		fdrop(fp, td);
2623 sctp_bad2:
2624 	if (to)
2625 		free(to, M_SONAME);
2626 	return (error);
2627 #else  /* SCTP */
2628 	return (EOPNOTSUPP);
2629 #endif /* SCTP */
2630 }
2631 
2632 int
2633 sys_sctp_generic_recvmsg(td, uap)
2634 	struct thread *td;
2635 	struct sctp_generic_recvmsg_args /* {
2636 		int sd,
2637 		struct iovec *iov,
2638 		int iovlen,
2639 		struct sockaddr *from,
2640 		__socklen_t *fromlenaddr,
2641 		struct sctp_sndrcvinfo *sinfo,
2642 		int *msg_flags
2643 	} */ *uap;
2644 {
2645 #if (defined(INET) || defined(INET6)) && defined(SCTP)
2646 	uint8_t sockbufstore[256];
2647 	struct uio auio;
2648 	struct iovec *iov, *tiov;
2649 	struct sctp_sndrcvinfo sinfo;
2650 	struct socket *so;
2651 	struct file *fp = NULL;
2652 	struct sockaddr *fromsa;
2653 	int fromlen;
2654 	ssize_t len;
2655 	int i, msg_flags;
2656 	int error = 0;
2657 #ifdef KTRACE
2658 	struct uio *ktruio = NULL;
2659 #endif
2660 
2661 	AUDIT_ARG_FD(uap->sd);
2662 	error = getsock_cap(td->td_proc->p_fd, uap->sd, CAP_READ, &fp, NULL);
2663 	if (error) {
2664 		return (error);
2665 	}
2666 #ifdef COMPAT_FREEBSD32
2667 	if (SV_CURPROC_FLAG(SV_ILP32))
2668 		error = freebsd32_copyiniov((struct iovec32 *)uap->iov,
2669 		    uap->iovlen, &iov, EMSGSIZE);
2670 	else
2671 #endif
2672 		error = copyiniov(uap->iov, uap->iovlen, &iov, EMSGSIZE);
2673 	if (error)
2674 		goto out1;
2675 
2676 	so = fp->f_data;
2677 	if (so->so_proto->pr_protocol != IPPROTO_SCTP) {
2678 		error = EOPNOTSUPP;
2679 		goto out;
2680 	}
2681 #ifdef MAC
2682 	error = mac_socket_check_receive(td->td_ucred, so);
2683 	if (error) {
2684 		goto out;
2685 	}
2686 #endif /* MAC */
2687 
2688 	if (uap->fromlenaddr) {
2689 		error = copyin(uap->fromlenaddr,
2690 		    &fromlen, sizeof (fromlen));
2691 		if (error) {
2692 			goto out;
2693 		}
2694 	} else {
2695 		fromlen = 0;
2696 	}
2697 	if (uap->msg_flags) {
2698 		error = copyin(uap->msg_flags, &msg_flags, sizeof (int));
2699 		if (error) {
2700 			goto out;
2701 		}
2702 	} else {
2703 		msg_flags = 0;
2704 	}
2705 	auio.uio_iov = iov;
2706 	auio.uio_iovcnt = uap->iovlen;
2707   	auio.uio_segflg = UIO_USERSPACE;
2708 	auio.uio_rw = UIO_READ;
2709 	auio.uio_td = td;
2710 	auio.uio_offset = 0;			/* XXX */
2711 	auio.uio_resid = 0;
2712 	tiov = iov;
2713 	for (i = 0; i <uap->iovlen; i++, tiov++) {
2714 		if ((auio.uio_resid += tiov->iov_len) < 0) {
2715 			error = EINVAL;
2716 			goto out;
2717 		}
2718 	}
2719 	len = auio.uio_resid;
2720 	fromsa = (struct sockaddr *)sockbufstore;
2721 
2722 #ifdef KTRACE
2723 	if (KTRPOINT(td, KTR_GENIO))
2724 		ktruio = cloneuio(&auio);
2725 #endif /* KTRACE */
2726 	memset(&sinfo, 0, sizeof(struct sctp_sndrcvinfo));
2727 	CURVNET_SET(so->so_vnet);
2728 	error = sctp_sorecvmsg(so, &auio, (struct mbuf **)NULL,
2729 		    fromsa, fromlen, &msg_flags,
2730 		    (struct sctp_sndrcvinfo *)&sinfo, 1);
2731 	CURVNET_RESTORE();
2732 	if (error) {
2733 		if (auio.uio_resid != len && (error == ERESTART ||
2734 		    error == EINTR || error == EWOULDBLOCK))
2735 			error = 0;
2736 	} else {
2737 		if (uap->sinfo)
2738 			error = copyout(&sinfo, uap->sinfo, sizeof (sinfo));
2739 	}
2740 #ifdef KTRACE
2741 	if (ktruio != NULL) {
2742 		ktruio->uio_resid = len - auio.uio_resid;
2743 		ktrgenio(uap->sd, UIO_READ, ktruio, error);
2744 	}
2745 #endif /* KTRACE */
2746 	if (error)
2747 		goto out;
2748 	td->td_retval[0] = len - auio.uio_resid;
2749 
2750 	if (fromlen && uap->from) {
2751 		len = fromlen;
2752 		if (len <= 0 || fromsa == 0)
2753 			len = 0;
2754 		else {
2755 			len = MIN(len, fromsa->sa_len);
2756 			error = copyout(fromsa, uap->from, (size_t)len);
2757 			if (error)
2758 				goto out;
2759 		}
2760 		error = copyout(&len, uap->fromlenaddr, sizeof (socklen_t));
2761 		if (error) {
2762 			goto out;
2763 		}
2764 	}
2765 #ifdef KTRACE
2766 	if (KTRPOINT(td, KTR_STRUCT))
2767 		ktrsockaddr(fromsa);
2768 #endif
2769 	if (uap->msg_flags) {
2770 		error = copyout(&msg_flags, uap->msg_flags, sizeof (int));
2771 		if (error) {
2772 			goto out;
2773 		}
2774 	}
2775 out:
2776 	free(iov, M_IOV);
2777 out1:
2778 	if (fp)
2779 		fdrop(fp, td);
2780 
2781 	return (error);
2782 #else  /* SCTP */
2783 	return (EOPNOTSUPP);
2784 #endif /* SCTP */
2785 }
2786