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