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