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