xref: /freebsd/sys/kern/uipc_syscalls.c (revision 2d49ff1e21a74b8f641752b63a59dc3ef9e38ddc)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1982, 1986, 1989, 1990, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  *
31  *	@(#)uipc_syscalls.c	8.4 (Berkeley) 2/21/94
32  */
33 
34 #include <sys/cdefs.h>
35 #include "opt_capsicum.h"
36 #include "opt_inet.h"
37 #include "opt_inet6.h"
38 #include "opt_ktrace.h"
39 
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/capsicum.h>
43 #include <sys/kernel.h>
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysproto.h>
47 #include <sys/malloc.h>
48 #include <sys/filedesc.h>
49 #include <sys/proc.h>
50 #include <sys/filio.h>
51 #include <sys/jail.h>
52 #include <sys/mbuf.h>
53 #include <sys/protosw.h>
54 #include <sys/rwlock.h>
55 #include <sys/socket.h>
56 #include <sys/socketvar.h>
57 #include <sys/syscallsubr.h>
58 #ifdef COMPAT_43
59 #include <sys/sysent.h>
60 #endif
61 #include <sys/uio.h>
62 #include <sys/un.h>
63 #include <sys/unpcb.h>
64 #ifdef KTRACE
65 #include <sys/ktrace.h>
66 #endif
67 #ifdef COMPAT_FREEBSD32
68 #include <compat/freebsd32/freebsd32_util.h>
69 #endif
70 
71 #include <net/vnet.h>
72 
73 #include <security/audit/audit.h>
74 #include <security/mac/mac_framework.h>
75 
76 static int sendit(struct thread *td, int s, struct msghdr *mp, int flags);
77 static int recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp);
78 
79 static int accept1(struct thread *td, int s, struct sockaddr *uname,
80 		   socklen_t *anamelen, int flags);
81 static int sockargs(struct mbuf **, char *, socklen_t, int);
82 
83 /*
84  * Convert a user file descriptor to a kernel file entry and check if required
85  * capability rights are present.
86  * If required copy of current set of capability rights is returned.
87  * A reference on the file entry is held upon returning.
88  */
89 int
90 getsock_cap(struct thread *td, int fd, cap_rights_t *rightsp,
91     struct file **fpp, struct filecaps *havecapsp)
92 {
93 	struct file *fp;
94 	int error;
95 
96 	error = fget_cap(td, fd, rightsp, &fp, havecapsp);
97 	if (__predict_false(error != 0))
98 		return (error);
99 	if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
100 		fdrop(fp, td);
101 		if (havecapsp != NULL)
102 			filecaps_free(havecapsp);
103 		return (ENOTSOCK);
104 	}
105 	*fpp = fp;
106 	return (0);
107 }
108 
109 int
110 getsock(struct thread *td, int fd, cap_rights_t *rightsp, struct file **fpp)
111 {
112 	struct file *fp;
113 	int error;
114 
115 	error = fget_unlocked(td, fd, rightsp, &fp);
116 	if (__predict_false(error != 0))
117 		return (error);
118 	if (__predict_false(fp->f_type != DTYPE_SOCKET)) {
119 		fdrop(fp, td);
120 		return (ENOTSOCK);
121 	}
122 	*fpp = fp;
123 	return (0);
124 }
125 
126 /*
127  * System call interface to the socket abstraction.
128  */
129 #if defined(COMPAT_43)
130 #define COMPAT_OLDSOCK
131 #endif
132 
133 int
134 sys_socket(struct thread *td, struct socket_args *uap)
135 {
136 
137 	return (kern_socket(td, uap->domain, uap->type, uap->protocol));
138 }
139 
140 int
141 kern_socket(struct thread *td, int domain, int type, int protocol)
142 {
143 	struct socket *so;
144 	struct file *fp;
145 	int fd, error, oflag, fflag;
146 
147 	AUDIT_ARG_SOCKET(domain, type, protocol);
148 
149 	oflag = 0;
150 	fflag = 0;
151 	if ((type & SOCK_CLOEXEC) != 0) {
152 		type &= ~SOCK_CLOEXEC;
153 		oflag |= O_CLOEXEC;
154 	}
155 	if ((type & SOCK_NONBLOCK) != 0) {
156 		type &= ~SOCK_NONBLOCK;
157 		fflag |= FNONBLOCK;
158 	}
159 
160 #ifdef MAC
161 	error = mac_socket_check_create(td->td_ucred, domain, type, protocol);
162 	if (error != 0)
163 		return (error);
164 #endif
165 	error = falloc(td, &fp, &fd, oflag);
166 	if (error != 0)
167 		return (error);
168 	/* An extra reference on `fp' has been held for us by falloc(). */
169 	error = socreate(domain, &so, type, protocol, td->td_ucred, td);
170 	if (error != 0) {
171 		fdclose(td, fp, fd);
172 	} else {
173 		finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
174 		if ((fflag & FNONBLOCK) != 0)
175 			(void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
176 		td->td_retval[0] = fd;
177 	}
178 	fdrop(fp, td);
179 	return (error);
180 }
181 
182 int
183 sys_bind(struct thread *td, struct bind_args *uap)
184 {
185 	struct sockaddr *sa;
186 	int error;
187 
188 	error = getsockaddr(&sa, uap->name, uap->namelen);
189 	if (error == 0) {
190 		error = kern_bindat(td, AT_FDCWD, uap->s, sa);
191 		free(sa, M_SONAME);
192 	}
193 	return (error);
194 }
195 
196 int
197 kern_bindat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
198 {
199 	struct socket *so;
200 	struct file *fp;
201 	int error;
202 
203 #ifdef CAPABILITY_MODE
204 	if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
205 		return (ECAPMODE);
206 #endif
207 
208 	AUDIT_ARG_FD(fd);
209 	AUDIT_ARG_SOCKADDR(td, dirfd, sa);
210 	error = getsock(td, fd, &cap_bind_rights, &fp);
211 	if (error != 0)
212 		return (error);
213 	so = fp->f_data;
214 #ifdef KTRACE
215 	if (KTRPOINT(td, KTR_STRUCT))
216 		ktrsockaddr(sa);
217 #endif
218 #ifdef MAC
219 	error = mac_socket_check_bind(td->td_ucred, so, sa);
220 	if (error == 0) {
221 #endif
222 		if (dirfd == AT_FDCWD)
223 			error = sobind(so, sa, td);
224 		else
225 			error = sobindat(dirfd, so, sa, td);
226 #ifdef MAC
227 	}
228 #endif
229 	fdrop(fp, td);
230 	return (error);
231 }
232 
233 int
234 sys_bindat(struct thread *td, struct bindat_args *uap)
235 {
236 	struct sockaddr *sa;
237 	int error;
238 
239 	error = getsockaddr(&sa, uap->name, uap->namelen);
240 	if (error == 0) {
241 		error = kern_bindat(td, uap->fd, uap->s, sa);
242 		free(sa, M_SONAME);
243 	}
244 	return (error);
245 }
246 
247 int
248 sys_listen(struct thread *td, struct listen_args *uap)
249 {
250 
251 	return (kern_listen(td, uap->s, uap->backlog));
252 }
253 
254 int
255 kern_listen(struct thread *td, int s, int backlog)
256 {
257 	struct socket *so;
258 	struct file *fp;
259 	int error;
260 
261 	AUDIT_ARG_FD(s);
262 	error = getsock(td, s, &cap_listen_rights, &fp);
263 	if (error == 0) {
264 		so = fp->f_data;
265 #ifdef MAC
266 		error = mac_socket_check_listen(td->td_ucred, so);
267 		if (error == 0)
268 #endif
269 			error = solisten(so, backlog, td);
270 		fdrop(fp, td);
271 	}
272 	return (error);
273 }
274 
275 /*
276  * accept1()
277  */
278 static int
279 accept1(struct thread *td, int s, struct sockaddr *uname, socklen_t *anamelen,
280     int flags)
281 {
282 	struct sockaddr *name;
283 	socklen_t namelen;
284 	struct file *fp;
285 	int error;
286 
287 	if (uname == NULL)
288 		return (kern_accept4(td, s, NULL, NULL, flags, NULL));
289 
290 	error = copyin(anamelen, &namelen, sizeof (namelen));
291 	if (error != 0)
292 		return (error);
293 
294 	error = kern_accept4(td, s, &name, &namelen, flags, &fp);
295 
296 	if (error != 0)
297 		return (error);
298 
299 #ifdef COMPAT_OLDSOCK
300 	if (SV_PROC_FLAG(td->td_proc, SV_AOUT) &&
301 	    (flags & ACCEPT4_COMPAT) != 0)
302 		((struct osockaddr *)name)->sa_family =
303 		    name->sa_family;
304 #endif
305 	error = copyout(name, uname, namelen);
306 	if (error == 0)
307 		error = copyout(&namelen, anamelen,
308 		    sizeof(namelen));
309 	if (error != 0)
310 		fdclose(td, fp, td->td_retval[0]);
311 	fdrop(fp, td);
312 	free(name, M_SONAME);
313 	return (error);
314 }
315 
316 int
317 kern_accept(struct thread *td, int s, struct sockaddr **name,
318     socklen_t *namelen, struct file **fp)
319 {
320 	return (kern_accept4(td, s, name, namelen, ACCEPT4_INHERIT, fp));
321 }
322 
323 int
324 kern_accept4(struct thread *td, int s, struct sockaddr **name,
325     socklen_t *namelen, int flags, struct file **fp)
326 {
327 	struct file *headfp, *nfp = NULL;
328 	struct sockaddr *sa = NULL;
329 	struct socket *head, *so;
330 	struct filecaps fcaps;
331 	u_int fflag;
332 	pid_t pgid;
333 	int error, fd, tmp;
334 
335 	if (name != NULL)
336 		*name = NULL;
337 
338 	AUDIT_ARG_FD(s);
339 	error = getsock_cap(td, s, &cap_accept_rights,
340 	    &headfp, &fcaps);
341 	if (error != 0)
342 		return (error);
343 	fflag = atomic_load_int(&headfp->f_flag);
344 	head = headfp->f_data;
345 	if (!SOLISTENING(head)) {
346 		error = EINVAL;
347 		goto done;
348 	}
349 #ifdef MAC
350 	error = mac_socket_check_accept(td->td_ucred, head);
351 	if (error != 0)
352 		goto done;
353 #endif
354 	error = falloc_caps(td, &nfp, &fd,
355 	    (flags & SOCK_CLOEXEC) ? O_CLOEXEC : 0, &fcaps);
356 	if (error != 0)
357 		goto done;
358 	SOCK_LOCK(head);
359 	if (!SOLISTENING(head)) {
360 		SOCK_UNLOCK(head);
361 		error = EINVAL;
362 		goto noconnection;
363 	}
364 
365 	error = solisten_dequeue(head, &so, flags);
366 	if (error != 0)
367 		goto noconnection;
368 
369 	/* An extra reference on `nfp' has been held for us by falloc(). */
370 	td->td_retval[0] = fd;
371 
372 	/* Connection has been removed from the listen queue. */
373 	KNOTE_UNLOCKED(&head->so_rdsel.si_note, 0);
374 
375 	if (flags & ACCEPT4_INHERIT) {
376 		pgid = fgetown(&head->so_sigio);
377 		if (pgid != 0)
378 			fsetown(pgid, &so->so_sigio);
379 	} else {
380 		fflag &= ~(FNONBLOCK | FASYNC);
381 		if (flags & SOCK_NONBLOCK)
382 			fflag |= FNONBLOCK;
383 	}
384 
385 	finit(nfp, fflag, DTYPE_SOCKET, so, &socketops);
386 	/* Sync socket nonblocking/async state with file flags */
387 	tmp = fflag & FNONBLOCK;
388 	(void) fo_ioctl(nfp, FIONBIO, &tmp, td->td_ucred, td);
389 	tmp = fflag & FASYNC;
390 	(void) fo_ioctl(nfp, FIOASYNC, &tmp, td->td_ucred, td);
391 	error = soaccept(so, &sa);
392 	if (error != 0)
393 		goto noconnection;
394 	if (sa == NULL) {
395 		if (name)
396 			*namelen = 0;
397 		goto done;
398 	}
399 	AUDIT_ARG_SOCKADDR(td, AT_FDCWD, sa);
400 	if (name) {
401 		/* check sa_len before it is destroyed */
402 		if (*namelen > sa->sa_len)
403 			*namelen = sa->sa_len;
404 #ifdef KTRACE
405 		if (KTRPOINT(td, KTR_STRUCT))
406 			ktrsockaddr(sa);
407 #endif
408 		*name = sa;
409 		sa = NULL;
410 	}
411 noconnection:
412 	free(sa, M_SONAME);
413 
414 	/*
415 	 * close the new descriptor, assuming someone hasn't ripped it
416 	 * out from under us.
417 	 */
418 	if (error != 0)
419 		fdclose(td, nfp, fd);
420 
421 	/*
422 	 * Release explicitly held references before returning.  We return
423 	 * a reference on nfp to the caller on success if they request it.
424 	 */
425 done:
426 	if (nfp == NULL)
427 		filecaps_free(&fcaps);
428 	if (fp != NULL) {
429 		if (error == 0) {
430 			*fp = nfp;
431 			nfp = NULL;
432 		} else
433 			*fp = NULL;
434 	}
435 	if (nfp != NULL)
436 		fdrop(nfp, td);
437 	fdrop(headfp, td);
438 	return (error);
439 }
440 
441 int
442 sys_accept(struct thread *td, struct accept_args *uap)
443 {
444 
445 	return (accept1(td, uap->s, uap->name, uap->anamelen, ACCEPT4_INHERIT));
446 }
447 
448 int
449 sys_accept4(struct thread *td, struct accept4_args *uap)
450 {
451 
452 	if (uap->flags & ~(SOCK_CLOEXEC | SOCK_NONBLOCK))
453 		return (EINVAL);
454 
455 	return (accept1(td, uap->s, uap->name, uap->anamelen, uap->flags));
456 }
457 
458 #ifdef COMPAT_OLDSOCK
459 int
460 oaccept(struct thread *td, struct oaccept_args *uap)
461 {
462 
463 	return (accept1(td, uap->s, uap->name, uap->anamelen,
464 	    ACCEPT4_INHERIT | ACCEPT4_COMPAT));
465 }
466 #endif /* COMPAT_OLDSOCK */
467 
468 int
469 sys_connect(struct thread *td, struct connect_args *uap)
470 {
471 	struct sockaddr *sa;
472 	int error;
473 
474 	error = getsockaddr(&sa, uap->name, uap->namelen);
475 	if (error == 0) {
476 		error = kern_connectat(td, AT_FDCWD, uap->s, sa);
477 		free(sa, M_SONAME);
478 	}
479 	return (error);
480 }
481 
482 int
483 kern_connectat(struct thread *td, int dirfd, int fd, struct sockaddr *sa)
484 {
485 	struct socket *so;
486 	struct file *fp;
487 	int error;
488 
489 #ifdef CAPABILITY_MODE
490 	if (IN_CAPABILITY_MODE(td) && (dirfd == AT_FDCWD))
491 		return (ECAPMODE);
492 #endif
493 
494 	AUDIT_ARG_FD(fd);
495 	AUDIT_ARG_SOCKADDR(td, dirfd, sa);
496 	error = getsock(td, fd, &cap_connect_rights, &fp);
497 	if (error != 0)
498 		return (error);
499 	so = fp->f_data;
500 	if (so->so_state & SS_ISCONNECTING) {
501 		error = EALREADY;
502 		goto done1;
503 	}
504 #ifdef KTRACE
505 	if (KTRPOINT(td, KTR_STRUCT))
506 		ktrsockaddr(sa);
507 #endif
508 #ifdef MAC
509 	error = mac_socket_check_connect(td->td_ucred, so, sa);
510 	if (error != 0)
511 		goto bad;
512 #endif
513 	error = soconnectat(dirfd, so, sa, td);
514 	if (error != 0)
515 		goto bad;
516 	if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
517 		error = EINPROGRESS;
518 		goto done1;
519 	}
520 	SOCK_LOCK(so);
521 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
522 		error = msleep(&so->so_timeo, &so->so_lock, PSOCK | PCATCH,
523 		    "connec", 0);
524 		if (error != 0)
525 			break;
526 	}
527 	if (error == 0) {
528 		error = so->so_error;
529 		so->so_error = 0;
530 	}
531 	SOCK_UNLOCK(so);
532 bad:
533 	if (error == ERESTART)
534 		error = EINTR;
535 done1:
536 	fdrop(fp, td);
537 	return (error);
538 }
539 
540 int
541 sys_connectat(struct thread *td, struct connectat_args *uap)
542 {
543 	struct sockaddr *sa;
544 	int error;
545 
546 	error = getsockaddr(&sa, uap->name, uap->namelen);
547 	if (error == 0) {
548 		error = kern_connectat(td, uap->fd, uap->s, sa);
549 		free(sa, M_SONAME);
550 	}
551 	return (error);
552 }
553 
554 int
555 kern_socketpair(struct thread *td, int domain, int type, int protocol,
556     int *rsv)
557 {
558 	struct file *fp1, *fp2;
559 	struct socket *so1, *so2;
560 	int fd, error, oflag, fflag;
561 
562 	AUDIT_ARG_SOCKET(domain, type, protocol);
563 
564 	oflag = 0;
565 	fflag = 0;
566 	if ((type & SOCK_CLOEXEC) != 0) {
567 		type &= ~SOCK_CLOEXEC;
568 		oflag |= O_CLOEXEC;
569 	}
570 	if ((type & SOCK_NONBLOCK) != 0) {
571 		type &= ~SOCK_NONBLOCK;
572 		fflag |= FNONBLOCK;
573 	}
574 #ifdef MAC
575 	/* We might want to have a separate check for socket pairs. */
576 	error = mac_socket_check_create(td->td_ucred, domain, type,
577 	    protocol);
578 	if (error != 0)
579 		return (error);
580 #endif
581 	error = socreate(domain, &so1, type, protocol, td->td_ucred, td);
582 	if (error != 0)
583 		return (error);
584 	error = socreate(domain, &so2, type, protocol, td->td_ucred, td);
585 	if (error != 0)
586 		goto free1;
587 	/* On success extra reference to `fp1' and 'fp2' is set by falloc. */
588 	error = falloc(td, &fp1, &fd, oflag);
589 	if (error != 0)
590 		goto free2;
591 	rsv[0] = fd;
592 	fp1->f_data = so1;	/* so1 already has ref count */
593 	error = falloc(td, &fp2, &fd, oflag);
594 	if (error != 0)
595 		goto free3;
596 	fp2->f_data = so2;	/* so2 already has ref count */
597 	rsv[1] = fd;
598 	error = soconnect2(so1, so2);
599 	if (error != 0)
600 		goto free4;
601 	if (type == SOCK_DGRAM) {
602 		/*
603 		 * Datagram socket connection is asymmetric.
604 		 */
605 		 error = soconnect2(so2, so1);
606 		 if (error != 0)
607 			goto free4;
608 	} else if (so1->so_proto->pr_flags & PR_CONNREQUIRED) {
609 		struct unpcb *unp, *unp2;
610 		unp = sotounpcb(so1);
611 		unp2 = sotounpcb(so2);
612 		/*
613 		 * No need to lock the unps, because the sockets are brand-new.
614 		 * No other threads can be using them yet
615 		 */
616 		unp_copy_peercred(td, unp, unp2, unp);
617 	}
618 	finit(fp1, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp1->f_data,
619 	    &socketops);
620 	finit(fp2, FREAD | FWRITE | fflag, DTYPE_SOCKET, fp2->f_data,
621 	    &socketops);
622 	if ((fflag & FNONBLOCK) != 0) {
623 		(void) fo_ioctl(fp1, FIONBIO, &fflag, td->td_ucred, td);
624 		(void) fo_ioctl(fp2, FIONBIO, &fflag, td->td_ucred, td);
625 	}
626 	fdrop(fp1, td);
627 	fdrop(fp2, td);
628 	return (0);
629 free4:
630 	fdclose(td, fp2, rsv[1]);
631 	fdrop(fp2, td);
632 free3:
633 	fdclose(td, fp1, rsv[0]);
634 	fdrop(fp1, td);
635 free2:
636 	if (so2 != NULL)
637 		(void)soclose(so2);
638 free1:
639 	if (so1 != NULL)
640 		(void)soclose(so1);
641 	return (error);
642 }
643 
644 int
645 sys_socketpair(struct thread *td, struct socketpair_args *uap)
646 {
647 	int error, sv[2];
648 
649 	error = kern_socketpair(td, uap->domain, uap->type,
650 	    uap->protocol, sv);
651 	if (error != 0)
652 		return (error);
653 	error = copyout(sv, uap->rsv, 2 * sizeof(int));
654 	if (error != 0) {
655 		(void)kern_close(td, sv[0]);
656 		(void)kern_close(td, sv[1]);
657 	}
658 	return (error);
659 }
660 
661 static int
662 sendit(struct thread *td, int s, struct msghdr *mp, int flags)
663 {
664 	struct mbuf *control;
665 	struct sockaddr *to;
666 	int error;
667 
668 #ifdef CAPABILITY_MODE
669 	if (IN_CAPABILITY_MODE(td) && (mp->msg_name != NULL))
670 		return (ECAPMODE);
671 #endif
672 
673 	if (mp->msg_name != NULL) {
674 		error = getsockaddr(&to, mp->msg_name, mp->msg_namelen);
675 		if (error != 0) {
676 			to = NULL;
677 			goto bad;
678 		}
679 		mp->msg_name = to;
680 	} else {
681 		to = NULL;
682 	}
683 
684 	if (mp->msg_control) {
685 		if (mp->msg_controllen < sizeof(struct cmsghdr)
686 #ifdef COMPAT_OLDSOCK
687 		    && (mp->msg_flags != MSG_COMPAT ||
688 		    !SV_PROC_FLAG(td->td_proc, SV_AOUT))
689 #endif
690 		) {
691 			error = EINVAL;
692 			goto bad;
693 		}
694 		error = sockargs(&control, mp->msg_control,
695 		    mp->msg_controllen, MT_CONTROL);
696 		if (error != 0)
697 			goto bad;
698 #ifdef COMPAT_OLDSOCK
699 		if (mp->msg_flags == MSG_COMPAT &&
700 		    SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
701 			struct cmsghdr *cm;
702 
703 			M_PREPEND(control, sizeof(*cm), M_WAITOK);
704 			cm = mtod(control, struct cmsghdr *);
705 			cm->cmsg_len = control->m_len;
706 			cm->cmsg_level = SOL_SOCKET;
707 			cm->cmsg_type = SCM_RIGHTS;
708 		}
709 #endif
710 	} else {
711 		control = NULL;
712 	}
713 
714 	error = kern_sendit(td, s, mp, flags, control, UIO_USERSPACE);
715 
716 bad:
717 	free(to, M_SONAME);
718 	return (error);
719 }
720 
721 int
722 kern_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
723     struct mbuf *control, enum uio_seg segflg)
724 {
725 	struct file *fp;
726 	struct uio auio;
727 	struct iovec *iov;
728 	struct socket *so;
729 	cap_rights_t *rights;
730 #ifdef KTRACE
731 	struct uio *ktruio = NULL;
732 #endif
733 	ssize_t len;
734 	int i, error;
735 
736 	AUDIT_ARG_FD(s);
737 	rights = &cap_send_rights;
738 	if (mp->msg_name != NULL) {
739 		AUDIT_ARG_SOCKADDR(td, AT_FDCWD, mp->msg_name);
740 		rights = &cap_send_connect_rights;
741 	}
742 	error = getsock(td, s, rights, &fp);
743 	if (error != 0) {
744 		m_freem(control);
745 		return (error);
746 	}
747 	so = (struct socket *)fp->f_data;
748 
749 #ifdef KTRACE
750 	if (mp->msg_name != NULL && KTRPOINT(td, KTR_STRUCT))
751 		ktrsockaddr(mp->msg_name);
752 #endif
753 #ifdef MAC
754 	if (mp->msg_name != NULL) {
755 		error = mac_socket_check_connect(td->td_ucred, so,
756 		    mp->msg_name);
757 		if (error != 0) {
758 			m_freem(control);
759 			goto bad;
760 		}
761 	}
762 	error = mac_socket_check_send(td->td_ucred, so);
763 	if (error != 0) {
764 		m_freem(control);
765 		goto bad;
766 	}
767 #endif
768 
769 	auio.uio_iov = mp->msg_iov;
770 	auio.uio_iovcnt = mp->msg_iovlen;
771 	auio.uio_segflg = segflg;
772 	auio.uio_rw = UIO_WRITE;
773 	auio.uio_td = td;
774 	auio.uio_offset = 0;			/* XXX */
775 	auio.uio_resid = 0;
776 	iov = mp->msg_iov;
777 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
778 		if ((auio.uio_resid += iov->iov_len) < 0) {
779 			error = EINVAL;
780 			m_freem(control);
781 			goto bad;
782 		}
783 	}
784 #ifdef KTRACE
785 	if (KTRPOINT(td, KTR_GENIO))
786 		ktruio = cloneuio(&auio);
787 #endif
788 	len = auio.uio_resid;
789 	error = sousrsend(so, mp->msg_name, &auio, control, flags, NULL);
790 	if (error == 0)
791 		td->td_retval[0] = len - auio.uio_resid;
792 #ifdef KTRACE
793 	if (ktruio != NULL) {
794 		ktruio->uio_resid = td->td_retval[0];
795 		ktrgenio(s, UIO_WRITE, ktruio, error);
796 	}
797 #endif
798 bad:
799 	fdrop(fp, td);
800 	return (error);
801 }
802 
803 int
804 sys_sendto(struct thread *td, struct sendto_args *uap)
805 {
806 	struct msghdr msg;
807 	struct iovec aiov;
808 
809 	msg.msg_name = __DECONST(void *, uap->to);
810 	msg.msg_namelen = uap->tolen;
811 	msg.msg_iov = &aiov;
812 	msg.msg_iovlen = 1;
813 	msg.msg_control = 0;
814 #ifdef COMPAT_OLDSOCK
815 	if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
816 		msg.msg_flags = 0;
817 #endif
818 	aiov.iov_base = __DECONST(void *, uap->buf);
819 	aiov.iov_len = uap->len;
820 	return (sendit(td, uap->s, &msg, uap->flags));
821 }
822 
823 #ifdef COMPAT_OLDSOCK
824 int
825 osend(struct thread *td, struct osend_args *uap)
826 {
827 	struct msghdr msg;
828 	struct iovec aiov;
829 
830 	msg.msg_name = 0;
831 	msg.msg_namelen = 0;
832 	msg.msg_iov = &aiov;
833 	msg.msg_iovlen = 1;
834 	aiov.iov_base = __DECONST(void *, uap->buf);
835 	aiov.iov_len = uap->len;
836 	msg.msg_control = 0;
837 	msg.msg_flags = 0;
838 	return (sendit(td, uap->s, &msg, uap->flags));
839 }
840 
841 int
842 osendmsg(struct thread *td, struct osendmsg_args *uap)
843 {
844 	struct msghdr msg;
845 	struct iovec *iov;
846 	int error;
847 
848 	error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
849 	if (error != 0)
850 		return (error);
851 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
852 	if (error != 0)
853 		return (error);
854 	msg.msg_iov = iov;
855 	msg.msg_flags = MSG_COMPAT;
856 	error = sendit(td, uap->s, &msg, uap->flags);
857 	free(iov, M_IOV);
858 	return (error);
859 }
860 #endif
861 
862 int
863 sys_sendmsg(struct thread *td, struct sendmsg_args *uap)
864 {
865 	struct msghdr msg;
866 	struct iovec *iov;
867 	int error;
868 
869 	error = copyin(uap->msg, &msg, sizeof (msg));
870 	if (error != 0)
871 		return (error);
872 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
873 	if (error != 0)
874 		return (error);
875 	msg.msg_iov = iov;
876 #ifdef COMPAT_OLDSOCK
877 	if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
878 		msg.msg_flags = 0;
879 #endif
880 	error = sendit(td, uap->s, &msg, uap->flags);
881 	free(iov, M_IOV);
882 	return (error);
883 }
884 
885 int
886 kern_recvit(struct thread *td, int s, struct msghdr *mp, enum uio_seg fromseg,
887     struct mbuf **controlp)
888 {
889 	struct uio auio;
890 	struct iovec *iov;
891 	struct mbuf *control, *m;
892 	caddr_t ctlbuf;
893 	struct file *fp;
894 	struct socket *so;
895 	struct sockaddr *fromsa = NULL;
896 #ifdef KTRACE
897 	struct uio *ktruio = NULL;
898 #endif
899 	ssize_t len;
900 	int error, i;
901 
902 	if (controlp != NULL)
903 		*controlp = NULL;
904 
905 	AUDIT_ARG_FD(s);
906 	error = getsock(td, s, &cap_recv_rights, &fp);
907 	if (error != 0)
908 		return (error);
909 	so = fp->f_data;
910 
911 #ifdef MAC
912 	error = mac_socket_check_receive(td->td_ucred, so);
913 	if (error != 0) {
914 		fdrop(fp, td);
915 		return (error);
916 	}
917 #endif
918 
919 	auio.uio_iov = mp->msg_iov;
920 	auio.uio_iovcnt = mp->msg_iovlen;
921 	auio.uio_segflg = UIO_USERSPACE;
922 	auio.uio_rw = UIO_READ;
923 	auio.uio_td = td;
924 	auio.uio_offset = 0;			/* XXX */
925 	auio.uio_resid = 0;
926 	iov = mp->msg_iov;
927 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
928 		if ((auio.uio_resid += iov->iov_len) < 0) {
929 			fdrop(fp, td);
930 			return (EINVAL);
931 		}
932 	}
933 #ifdef KTRACE
934 	if (KTRPOINT(td, KTR_GENIO))
935 		ktruio = cloneuio(&auio);
936 #endif
937 	control = NULL;
938 	len = auio.uio_resid;
939 	error = soreceive(so, &fromsa, &auio, NULL,
940 	    (mp->msg_control || controlp) ? &control : NULL,
941 	    &mp->msg_flags);
942 	if (error != 0) {
943 		if (auio.uio_resid != len && (error == ERESTART ||
944 		    error == EINTR || error == EWOULDBLOCK))
945 			error = 0;
946 	}
947 	if (fromsa != NULL)
948 		AUDIT_ARG_SOCKADDR(td, AT_FDCWD, fromsa);
949 #ifdef KTRACE
950 	if (ktruio != NULL) {
951 		/* MSG_TRUNC can trigger underflow of uio_resid. */
952 		ktruio->uio_resid = MIN(len - auio.uio_resid, len);
953 		ktrgenio(s, UIO_READ, ktruio, error);
954 	}
955 #endif
956 	if (error != 0)
957 		goto out;
958 	td->td_retval[0] = len - auio.uio_resid;
959 	if (mp->msg_name) {
960 		len = mp->msg_namelen;
961 		if (len <= 0 || fromsa == NULL)
962 			len = 0;
963 		else {
964 			/* save sa_len before it is destroyed by MSG_COMPAT */
965 			len = MIN(len, fromsa->sa_len);
966 #ifdef COMPAT_OLDSOCK
967 			if ((mp->msg_flags & MSG_COMPAT) != 0 &&
968 			    SV_PROC_FLAG(td->td_proc, SV_AOUT))
969 				((struct osockaddr *)fromsa)->sa_family =
970 				    fromsa->sa_family;
971 #endif
972 			if (fromseg == UIO_USERSPACE) {
973 				error = copyout(fromsa, mp->msg_name,
974 				    (unsigned)len);
975 				if (error != 0)
976 					goto out;
977 			} else
978 				bcopy(fromsa, mp->msg_name, len);
979 		}
980 		mp->msg_namelen = len;
981 	}
982 	if (mp->msg_control && controlp == NULL) {
983 #ifdef COMPAT_OLDSOCK
984 		/*
985 		 * We assume that old recvmsg calls won't receive access
986 		 * rights and other control info, esp. as control info
987 		 * is always optional and those options didn't exist in 4.3.
988 		 * If we receive rights, trim the cmsghdr; anything else
989 		 * is tossed.
990 		 */
991 		if (control && (mp->msg_flags & MSG_COMPAT) != 0 &&
992 		    SV_PROC_FLAG(td->td_proc, SV_AOUT)) {
993 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
994 			    SOL_SOCKET ||
995 			    mtod(control, struct cmsghdr *)->cmsg_type !=
996 			    SCM_RIGHTS) {
997 				mp->msg_controllen = 0;
998 				goto out;
999 			}
1000 			control->m_len -= sizeof (struct cmsghdr);
1001 			control->m_data += sizeof (struct cmsghdr);
1002 		}
1003 #endif
1004 		ctlbuf = mp->msg_control;
1005 		len = mp->msg_controllen;
1006 		mp->msg_controllen = 0;
1007 		for (m = control; m != NULL && len >= m->m_len; m = m->m_next) {
1008 			if ((error = copyout(mtod(m, caddr_t), ctlbuf,
1009 			    m->m_len)) != 0)
1010 				goto out;
1011 
1012 			ctlbuf += m->m_len;
1013 			len -= m->m_len;
1014 			mp->msg_controllen += m->m_len;
1015 		}
1016 		if (m != NULL) {
1017 			mp->msg_flags |= MSG_CTRUNC;
1018 			m_dispose_extcontrolm(m);
1019 		}
1020 	}
1021 out:
1022 	fdrop(fp, td);
1023 #ifdef KTRACE
1024 	if (fromsa && KTRPOINT(td, KTR_STRUCT))
1025 		ktrsockaddr(fromsa);
1026 #endif
1027 	free(fromsa, M_SONAME);
1028 
1029 	if (error == 0 && controlp != NULL)
1030 		*controlp = control;
1031 	else if (control != NULL) {
1032 		if (error != 0)
1033 			m_dispose_extcontrolm(control);
1034 		m_freem(control);
1035 	}
1036 
1037 	return (error);
1038 }
1039 
1040 static int
1041 recvit(struct thread *td, int s, struct msghdr *mp, void *namelenp)
1042 {
1043 	int error;
1044 
1045 	error = kern_recvit(td, s, mp, UIO_USERSPACE, NULL);
1046 	if (error != 0)
1047 		return (error);
1048 	if (namelenp != NULL) {
1049 		error = copyout(&mp->msg_namelen, namelenp, sizeof (socklen_t));
1050 #ifdef COMPAT_OLDSOCK
1051 		if ((mp->msg_flags & MSG_COMPAT) != 0 &&
1052 		    SV_PROC_FLAG(td->td_proc, SV_AOUT))
1053 			error = 0;	/* old recvfrom didn't check */
1054 #endif
1055 	}
1056 	return (error);
1057 }
1058 
1059 static int
1060 kern_recvfrom(struct thread *td, int s, void *buf, size_t len, int flags,
1061     struct sockaddr *from, socklen_t *fromlenaddr)
1062 {
1063 	struct msghdr msg;
1064 	struct iovec aiov;
1065 	int error;
1066 
1067 	if (fromlenaddr != NULL) {
1068 		error = copyin(fromlenaddr, &msg.msg_namelen,
1069 		    sizeof (msg.msg_namelen));
1070 		if (error != 0)
1071 			goto done2;
1072 	} else {
1073 		msg.msg_namelen = 0;
1074 	}
1075 	msg.msg_name = from;
1076 	msg.msg_iov = &aiov;
1077 	msg.msg_iovlen = 1;
1078 	aiov.iov_base = buf;
1079 	aiov.iov_len = len;
1080 	msg.msg_control = 0;
1081 	msg.msg_flags = flags;
1082 	error = recvit(td, s, &msg, fromlenaddr);
1083 done2:
1084 	return (error);
1085 }
1086 
1087 int
1088 sys_recvfrom(struct thread *td, struct recvfrom_args *uap)
1089 {
1090 	return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1091 	    uap->flags, uap->from, uap->fromlenaddr));
1092 }
1093 
1094 
1095 #ifdef COMPAT_OLDSOCK
1096 int
1097 orecvfrom(struct thread *td, struct orecvfrom_args *uap)
1098 {
1099 	return (kern_recvfrom(td, uap->s, uap->buf, uap->len,
1100 	    uap->flags | MSG_COMPAT, uap->from, uap->fromlenaddr));
1101 }
1102 #endif
1103 
1104 #ifdef COMPAT_OLDSOCK
1105 int
1106 orecv(struct thread *td, struct orecv_args *uap)
1107 {
1108 	struct msghdr msg;
1109 	struct iovec aiov;
1110 
1111 	msg.msg_name = 0;
1112 	msg.msg_namelen = 0;
1113 	msg.msg_iov = &aiov;
1114 	msg.msg_iovlen = 1;
1115 	aiov.iov_base = uap->buf;
1116 	aiov.iov_len = uap->len;
1117 	msg.msg_control = 0;
1118 	msg.msg_flags = uap->flags;
1119 	return (recvit(td, uap->s, &msg, NULL));
1120 }
1121 
1122 /*
1123  * Old recvmsg.  This code takes advantage of the fact that the old msghdr
1124  * overlays the new one, missing only the flags, and with the (old) access
1125  * rights where the control fields are now.
1126  */
1127 int
1128 orecvmsg(struct thread *td, struct orecvmsg_args *uap)
1129 {
1130 	struct msghdr msg;
1131 	struct iovec *iov;
1132 	int error;
1133 
1134 	error = copyin(uap->msg, &msg, sizeof (struct omsghdr));
1135 	if (error != 0)
1136 		return (error);
1137 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1138 	if (error != 0)
1139 		return (error);
1140 	msg.msg_flags = uap->flags | MSG_COMPAT;
1141 	msg.msg_iov = iov;
1142 	error = recvit(td, uap->s, &msg, &uap->msg->msg_namelen);
1143 	if (msg.msg_controllen && error == 0)
1144 		error = copyout(&msg.msg_controllen,
1145 		    &uap->msg->msg_accrightslen, sizeof (int));
1146 	free(iov, M_IOV);
1147 	return (error);
1148 }
1149 #endif
1150 
1151 int
1152 sys_recvmsg(struct thread *td, struct recvmsg_args *uap)
1153 {
1154 	struct msghdr msg;
1155 	struct iovec *uiov, *iov;
1156 	int error;
1157 
1158 	error = copyin(uap->msg, &msg, sizeof (msg));
1159 	if (error != 0)
1160 		return (error);
1161 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1162 	if (error != 0)
1163 		return (error);
1164 	msg.msg_flags = uap->flags;
1165 #ifdef COMPAT_OLDSOCK
1166 	if (SV_PROC_FLAG(td->td_proc, SV_AOUT))
1167 		msg.msg_flags &= ~MSG_COMPAT;
1168 #endif
1169 	uiov = msg.msg_iov;
1170 	msg.msg_iov = iov;
1171 	error = recvit(td, uap->s, &msg, NULL);
1172 	if (error == 0) {
1173 		msg.msg_iov = uiov;
1174 		error = copyout(&msg, uap->msg, sizeof(msg));
1175 	}
1176 	free(iov, M_IOV);
1177 	return (error);
1178 }
1179 
1180 int
1181 sys_shutdown(struct thread *td, struct shutdown_args *uap)
1182 {
1183 
1184 	return (kern_shutdown(td, uap->s, uap->how));
1185 }
1186 
1187 int
1188 kern_shutdown(struct thread *td, int s, int how)
1189 {
1190 	struct socket *so;
1191 	struct file *fp;
1192 	int error;
1193 
1194 	AUDIT_ARG_FD(s);
1195 	error = getsock(td, s, &cap_shutdown_rights, &fp);
1196 	if (error == 0) {
1197 		so = fp->f_data;
1198 		error = soshutdown(so, how);
1199 		/*
1200 		 * Previous versions did not return ENOTCONN, but 0 in
1201 		 * case the socket was not connected. Some important
1202 		 * programs like syslogd up to r279016, 2015-02-19,
1203 		 * still depend on this behavior.
1204 		 */
1205 		if (error == ENOTCONN &&
1206 		    td->td_proc->p_osrel < P_OSREL_SHUTDOWN_ENOTCONN)
1207 			error = 0;
1208 		fdrop(fp, td);
1209 	}
1210 	return (error);
1211 }
1212 
1213 int
1214 sys_setsockopt(struct thread *td, struct setsockopt_args *uap)
1215 {
1216 
1217 	return (kern_setsockopt(td, uap->s, uap->level, uap->name,
1218 	    uap->val, UIO_USERSPACE, uap->valsize));
1219 }
1220 
1221 int
1222 kern_setsockopt(struct thread *td, int s, int level, int name, const void *val,
1223     enum uio_seg valseg, socklen_t valsize)
1224 {
1225 	struct socket *so;
1226 	struct file *fp;
1227 	struct sockopt sopt;
1228 	int error;
1229 
1230 	if (val == NULL && valsize != 0)
1231 		return (EFAULT);
1232 	if ((int)valsize < 0)
1233 		return (EINVAL);
1234 
1235 	sopt.sopt_dir = SOPT_SET;
1236 	sopt.sopt_level = level;
1237 	sopt.sopt_name = name;
1238 	sopt.sopt_val = __DECONST(void *, val);
1239 	sopt.sopt_valsize = valsize;
1240 	switch (valseg) {
1241 	case UIO_USERSPACE:
1242 		sopt.sopt_td = td;
1243 		break;
1244 	case UIO_SYSSPACE:
1245 		sopt.sopt_td = NULL;
1246 		break;
1247 	default:
1248 		panic("kern_setsockopt called with bad valseg");
1249 	}
1250 
1251 	AUDIT_ARG_FD(s);
1252 	error = getsock(td, s, &cap_setsockopt_rights, &fp);
1253 	if (error == 0) {
1254 		so = fp->f_data;
1255 		error = sosetopt(so, &sopt);
1256 		fdrop(fp, td);
1257 	}
1258 	return(error);
1259 }
1260 
1261 int
1262 sys_getsockopt(struct thread *td, struct getsockopt_args *uap)
1263 {
1264 	socklen_t valsize;
1265 	int error;
1266 
1267 	if (uap->val) {
1268 		error = copyin(uap->avalsize, &valsize, sizeof (valsize));
1269 		if (error != 0)
1270 			return (error);
1271 	}
1272 
1273 	error = kern_getsockopt(td, uap->s, uap->level, uap->name,
1274 	    uap->val, UIO_USERSPACE, &valsize);
1275 
1276 	if (error == 0)
1277 		error = copyout(&valsize, uap->avalsize, sizeof (valsize));
1278 	return (error);
1279 }
1280 
1281 /*
1282  * Kernel version of getsockopt.
1283  * optval can be a userland or userspace. optlen is always a kernel pointer.
1284  */
1285 int
1286 kern_getsockopt(struct thread *td, int s, int level, int name, void *val,
1287     enum uio_seg valseg, socklen_t *valsize)
1288 {
1289 	struct socket *so;
1290 	struct file *fp;
1291 	struct sockopt sopt;
1292 	int error;
1293 
1294 	if (val == NULL)
1295 		*valsize = 0;
1296 	if ((int)*valsize < 0)
1297 		return (EINVAL);
1298 
1299 	sopt.sopt_dir = SOPT_GET;
1300 	sopt.sopt_level = level;
1301 	sopt.sopt_name = name;
1302 	sopt.sopt_val = val;
1303 	sopt.sopt_valsize = (size_t)*valsize; /* checked non-negative above */
1304 	switch (valseg) {
1305 	case UIO_USERSPACE:
1306 		sopt.sopt_td = td;
1307 		break;
1308 	case UIO_SYSSPACE:
1309 		sopt.sopt_td = NULL;
1310 		break;
1311 	default:
1312 		panic("kern_getsockopt called with bad valseg");
1313 	}
1314 
1315 	AUDIT_ARG_FD(s);
1316 	error = getsock(td, s, &cap_getsockopt_rights, &fp);
1317 	if (error == 0) {
1318 		so = fp->f_data;
1319 		error = sogetopt(so, &sopt);
1320 		*valsize = sopt.sopt_valsize;
1321 		fdrop(fp, td);
1322 	}
1323 	return (error);
1324 }
1325 
1326 static int
1327 user_getsockname(struct thread *td, int fdes, struct sockaddr *asa,
1328     socklen_t *alen, bool compat)
1329 {
1330 	struct sockaddr *sa;
1331 	socklen_t len;
1332 	int error;
1333 
1334 	error = copyin(alen, &len, sizeof(len));
1335 	if (error != 0)
1336 		return (error);
1337 
1338 	error = kern_getsockname(td, fdes, &sa, &len);
1339 	if (error != 0)
1340 		return (error);
1341 
1342 	if (len != 0) {
1343 #ifdef COMPAT_OLDSOCK
1344 		if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1345 			((struct osockaddr *)sa)->sa_family = sa->sa_family;
1346 #endif
1347 		error = copyout(sa, asa, len);
1348 	}
1349 	free(sa, M_SONAME);
1350 	if (error == 0)
1351 		error = copyout(&len, alen, sizeof(len));
1352 	return (error);
1353 }
1354 
1355 int
1356 kern_getsockname(struct thread *td, int fd, struct sockaddr **sa,
1357     socklen_t *alen)
1358 {
1359 	struct socket *so;
1360 	struct file *fp;
1361 	socklen_t len;
1362 	int error;
1363 
1364 	AUDIT_ARG_FD(fd);
1365 	error = getsock(td, fd, &cap_getsockname_rights, &fp);
1366 	if (error != 0)
1367 		return (error);
1368 	so = fp->f_data;
1369 	*sa = NULL;
1370 	CURVNET_SET(so->so_vnet);
1371 	error = so->so_proto->pr_sockaddr(so, sa);
1372 	CURVNET_RESTORE();
1373 	if (error != 0)
1374 		goto bad;
1375 	if (*sa == NULL)
1376 		len = 0;
1377 	else
1378 		len = MIN(*alen, (*sa)->sa_len);
1379 	*alen = len;
1380 #ifdef KTRACE
1381 	if (KTRPOINT(td, KTR_STRUCT))
1382 		ktrsockaddr(*sa);
1383 #endif
1384 bad:
1385 	fdrop(fp, td);
1386 	if (error != 0 && *sa != NULL) {
1387 		free(*sa, M_SONAME);
1388 		*sa = NULL;
1389 	}
1390 	return (error);
1391 }
1392 
1393 int
1394 sys_getsockname(struct thread *td, struct getsockname_args *uap)
1395 {
1396 	return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, false));
1397 }
1398 
1399 #ifdef COMPAT_OLDSOCK
1400 int
1401 ogetsockname(struct thread *td, struct ogetsockname_args *uap)
1402 {
1403 	return (user_getsockname(td, uap->fdes, uap->asa, uap->alen, true));
1404 }
1405 #endif /* COMPAT_OLDSOCK */
1406 
1407 static int
1408 user_getpeername(struct thread *td, int fdes, struct sockaddr *asa,
1409     socklen_t *alen, bool compat)
1410 {
1411 	struct sockaddr *sa;
1412 	socklen_t len;
1413 	int error;
1414 
1415 	error = copyin(alen, &len, sizeof (len));
1416 	if (error != 0)
1417 		return (error);
1418 
1419 	error = kern_getpeername(td, fdes, &sa, &len);
1420 	if (error != 0)
1421 		return (error);
1422 
1423 	if (len != 0) {
1424 #ifdef COMPAT_OLDSOCK
1425 		if (compat && SV_PROC_FLAG(td->td_proc, SV_AOUT))
1426 			((struct osockaddr *)sa)->sa_family = sa->sa_family;
1427 #endif
1428 		error = copyout(sa, asa, len);
1429 	}
1430 	free(sa, M_SONAME);
1431 	if (error == 0)
1432 		error = copyout(&len, alen, sizeof(len));
1433 	return (error);
1434 }
1435 
1436 int
1437 kern_getpeername(struct thread *td, int fd, struct sockaddr **sa,
1438     socklen_t *alen)
1439 {
1440 	struct socket *so;
1441 	struct file *fp;
1442 	socklen_t len;
1443 	int error;
1444 
1445 	AUDIT_ARG_FD(fd);
1446 	error = getsock(td, fd, &cap_getpeername_rights, &fp);
1447 	if (error != 0)
1448 		return (error);
1449 	so = fp->f_data;
1450 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
1451 		error = ENOTCONN;
1452 		goto done;
1453 	}
1454 	*sa = NULL;
1455 	CURVNET_SET(so->so_vnet);
1456 	error = so->so_proto->pr_peeraddr(so, sa);
1457 	CURVNET_RESTORE();
1458 	if (error != 0)
1459 		goto bad;
1460 	if (*sa == NULL)
1461 		len = 0;
1462 	else
1463 		len = MIN(*alen, (*sa)->sa_len);
1464 	*alen = len;
1465 #ifdef KTRACE
1466 	if (KTRPOINT(td, KTR_STRUCT))
1467 		ktrsockaddr(*sa);
1468 #endif
1469 bad:
1470 	if (error != 0 && *sa != NULL) {
1471 		free(*sa, M_SONAME);
1472 		*sa = NULL;
1473 	}
1474 done:
1475 	fdrop(fp, td);
1476 	return (error);
1477 }
1478 
1479 int
1480 sys_getpeername(struct thread *td, struct getpeername_args *uap)
1481 {
1482 	return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, false));
1483 }
1484 
1485 #ifdef COMPAT_OLDSOCK
1486 int
1487 ogetpeername(struct thread *td, struct ogetpeername_args *uap)
1488 {
1489 	return (user_getpeername(td, uap->fdes, uap->asa, uap->alen, true));
1490 }
1491 #endif /* COMPAT_OLDSOCK */
1492 
1493 static int
1494 sockargs(struct mbuf **mp, char *buf, socklen_t buflen, int type)
1495 {
1496 	struct sockaddr *sa;
1497 	struct mbuf *m;
1498 	int error;
1499 
1500 	if (buflen > MLEN) {
1501 #ifdef COMPAT_OLDSOCK
1502 		if (type == MT_SONAME && buflen <= 112 &&
1503 		    SV_CURPROC_FLAG(SV_AOUT))
1504 			buflen = MLEN;		/* unix domain compat. hack */
1505 		else
1506 #endif
1507 			if (buflen > MCLBYTES)
1508 				return (EMSGSIZE);
1509 	}
1510 	m = m_get2(buflen, M_WAITOK, type, 0);
1511 	m->m_len = buflen;
1512 	error = copyin(buf, mtod(m, void *), buflen);
1513 	if (error != 0)
1514 		(void) m_free(m);
1515 	else {
1516 		*mp = m;
1517 		if (type == MT_SONAME) {
1518 			sa = mtod(m, struct sockaddr *);
1519 
1520 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1521 			if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1522 			    SV_CURPROC_FLAG(SV_AOUT))
1523 				sa->sa_family = sa->sa_len;
1524 #endif
1525 			sa->sa_len = buflen;
1526 		}
1527 	}
1528 	return (error);
1529 }
1530 
1531 int
1532 getsockaddr(struct sockaddr **namp, const struct sockaddr *uaddr, size_t len)
1533 {
1534 	struct sockaddr *sa;
1535 	int error;
1536 
1537 	if (len > SOCK_MAXADDRLEN)
1538 		return (ENAMETOOLONG);
1539 	if (len < offsetof(struct sockaddr, sa_data[0]))
1540 		return (EINVAL);
1541 	sa = malloc(len, M_SONAME, M_WAITOK);
1542 	error = copyin(uaddr, sa, len);
1543 	if (error != 0) {
1544 		free(sa, M_SONAME);
1545 	} else {
1546 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
1547 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX &&
1548 		    SV_CURPROC_FLAG(SV_AOUT))
1549 			sa->sa_family = sa->sa_len;
1550 #endif
1551 		sa->sa_len = len;
1552 		*namp = sa;
1553 	}
1554 	return (error);
1555 }
1556 
1557 /*
1558  * Dispose of externalized rights from an SCM_RIGHTS message.  This function
1559  * should be used in error or truncation cases to avoid leaking file descriptors
1560  * into the recipient's (the current thread's) table.
1561  */
1562 void
1563 m_dispose_extcontrolm(struct mbuf *m)
1564 {
1565 	struct cmsghdr *cm;
1566 	struct file *fp;
1567 	struct thread *td;
1568 	socklen_t clen, datalen;
1569 	int error, fd, *fds, nfd;
1570 
1571 	td = curthread;
1572 	for (; m != NULL; m = m->m_next) {
1573 		if (m->m_type != MT_EXTCONTROL)
1574 			continue;
1575 		cm = mtod(m, struct cmsghdr *);
1576 		clen = m->m_len;
1577 		while (clen > 0) {
1578 			if (clen < sizeof(*cm))
1579 				panic("%s: truncated mbuf %p", __func__, m);
1580 			datalen = CMSG_SPACE(cm->cmsg_len - CMSG_SPACE(0));
1581 			if (clen < datalen)
1582 				panic("%s: truncated mbuf %p", __func__, m);
1583 
1584 			if (cm->cmsg_level == SOL_SOCKET &&
1585 			    cm->cmsg_type == SCM_RIGHTS) {
1586 				fds = (int *)CMSG_DATA(cm);
1587 				nfd = (cm->cmsg_len - CMSG_SPACE(0)) /
1588 				    sizeof(int);
1589 
1590 				while (nfd-- > 0) {
1591 					fd = *fds++;
1592 					error = fget(td, fd, &cap_no_rights,
1593 					    &fp);
1594 					if (error == 0) {
1595 						fdclose(td, fp, fd);
1596 						fdrop(fp, td);
1597 					}
1598 				}
1599 			}
1600 			clen -= datalen;
1601 			cm = (struct cmsghdr *)((uint8_t *)cm + datalen);
1602 		}
1603 		m_chtype(m, MT_CONTROL);
1604 	}
1605 }
1606