Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.209
      1 /*	$NetBSD: uipc_syscalls.c,v 1.209 2023/10/13 18:50:39 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009, 2023 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1989, 1990, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)uipc_syscalls.c	8.6 (Berkeley) 2/14/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.209 2023/10/13 18:50:39 ad Exp $");
     65 
     66 #ifdef _KERNEL_OPT
     67 #include "opt_pipe.h"
     68 #include "opt_sctp.h"
     69 #endif
     70 
     71 #define MBUFTYPES
     72 #include <sys/param.h>
     73 #include <sys/systm.h>
     74 #include <sys/filedesc.h>
     75 #include <sys/proc.h>
     76 #include <sys/file.h>
     77 #include <sys/buf.h>
     78 #include <sys/mbuf.h>
     79 #include <sys/protosw.h>
     80 #include <sys/socket.h>
     81 #include <sys/socketvar.h>
     82 #include <sys/signalvar.h>
     83 #include <sys/un.h>
     84 #include <sys/ktrace.h>
     85 #include <sys/event.h>
     86 #include <sys/atomic.h>
     87 #include <sys/kauth.h>
     88 
     89 #ifdef SCTP
     90 #include <netinet/sctp_uio.h>
     91 #include <netinet/sctp_peeloff.h>
     92 #endif
     93 
     94 #include <sys/mount.h>
     95 #include <sys/syscallargs.h>
     96 
     97 /*
     98  * System call interface to the socket abstraction.
     99  */
    100 extern const struct fileops socketops;
    101 
    102 static int	sockargs_sb(struct sockaddr_big *, const void *, socklen_t);
    103 
    104 int
    105 sys___socket30(struct lwp *l, const struct sys___socket30_args *uap,
    106     register_t *retval)
    107 {
    108 	/* {
    109 		syscallarg(int)	domain;
    110 		syscallarg(int)	type;
    111 		syscallarg(int)	protocol;
    112 	} */
    113 	int fd, error;
    114 
    115 	error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type),
    116 	    SCARG(uap, protocol), &fd);
    117 	if (error == 0) {
    118 		*retval = fd;
    119 	}
    120 	return error;
    121 }
    122 
    123 int
    124 sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval)
    125 {
    126 	/* {
    127 		syscallarg(int)				s;
    128 		syscallarg(const struct sockaddr *)	name;
    129 		syscallarg(unsigned int)		namelen;
    130 	} */
    131 	int		error;
    132 	struct sockaddr_big sb;
    133 
    134 	error = sockargs_sb(&sb, SCARG(uap, name), SCARG(uap, namelen));
    135 	if (error)
    136 		return error;
    137 
    138 	return do_sys_bind(l, SCARG(uap, s), (struct sockaddr *)&sb);
    139 }
    140 
    141 int
    142 do_sys_bind(struct lwp *l, int fd, struct sockaddr *nam)
    143 {
    144 	struct socket	*so;
    145 	int		error;
    146 
    147 	if ((error = fd_getsock(fd, &so)) != 0)
    148 		return error;
    149 	error = sobind(so, nam, l);
    150 	fd_putfile(fd);
    151 	return error;
    152 }
    153 
    154 int
    155 sys_listen(struct lwp *l, const struct sys_listen_args *uap, register_t *retval)
    156 {
    157 	/* {
    158 		syscallarg(int)	s;
    159 		syscallarg(int)	backlog;
    160 	} */
    161 	struct socket	*so;
    162 	int		error;
    163 
    164 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    165 		return (error);
    166 	error = solisten(so, SCARG(uap, backlog), l);
    167 	fd_putfile(SCARG(uap, s));
    168 	return error;
    169 }
    170 
    171 int
    172 do_sys_accept(struct lwp *l, int sock, struct sockaddr *name,
    173     register_t *new_sock, const sigset_t *mask, int flags, int clrflags)
    174 {
    175 	file_t		*fp, *fp2;
    176 	int		error, fd;
    177 	struct socket	*so, *so2;
    178 
    179 	if ((fp = fd_getfile(sock)) == NULL)
    180 		return EBADF;
    181 	if (fp->f_type != DTYPE_SOCKET) {
    182 		fd_putfile(sock);
    183 		return ENOTSOCK;
    184 	}
    185 	if ((error = fd_allocfile(&fp2, &fd)) != 0) {
    186 		fd_putfile(sock);
    187 		return error;
    188 	}
    189 	*new_sock = fd;
    190 	so = fp->f_socket;
    191 	solock(so);
    192 
    193 	if (__predict_false(mask))
    194 		sigsuspendsetup(l, mask);
    195 
    196 	if (!(so->so_proto->pr_flags & PR_LISTEN)) {
    197 		error = EOPNOTSUPP;
    198 		goto bad;
    199 	}
    200 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    201 		error = EINVAL;
    202 		goto bad;
    203 	}
    204 	if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
    205 		error = EWOULDBLOCK;
    206 		goto bad;
    207 	}
    208 	while (so->so_qlen == 0 && so->so_error == 0) {
    209 		if (so->so_state & SS_CANTRCVMORE) {
    210 			so->so_error = ECONNABORTED;
    211 			break;
    212 		}
    213 		error = sowait(so, true, 0);
    214 		if (error) {
    215 			goto bad;
    216 		}
    217 	}
    218 	if (so->so_error) {
    219 		error = so->so_error;
    220 		so->so_error = 0;
    221 		goto bad;
    222 	}
    223 	/* connection has been removed from the listen queue */
    224 	KNOTE(&so->so_rcv.sb_sel.sel_klist, NOTE_SUBMIT);
    225 	so2 = TAILQ_FIRST(&so->so_q);
    226 	if (soqremque(so2, 1) == 0)
    227 		panic("accept");
    228 	fp2->f_type = DTYPE_SOCKET;
    229 	fp2->f_flag = (fp->f_flag & ~clrflags) |
    230 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    231 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    232 	fp2->f_ops = &socketops;
    233 	fp2->f_socket = so2;
    234 	if (fp2->f_flag & FNONBLOCK)
    235 		so2->so_state |= SS_NBIO;
    236 	else
    237 		so2->so_state &= ~SS_NBIO;
    238 	error = soaccept(so2, name);
    239 	so2->so_cred = kauth_cred_hold(so->so_cred);
    240 	sounlock(so);
    241 	if (error) {
    242 		/* an error occurred, free the file descriptor and mbuf */
    243 		mutex_enter(&fp2->f_lock);
    244 		fp2->f_count++;
    245 		mutex_exit(&fp2->f_lock);
    246 		closef(fp2);
    247 		fd_abort(curproc, NULL, fd);
    248 	} else {
    249 		fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
    250 		fd_affix(curproc, fp2, fd);
    251 	}
    252 	fd_putfile(sock);
    253 	if (__predict_false(mask))
    254 		sigsuspendteardown(l);
    255 	return error;
    256  bad:
    257 	sounlock(so);
    258 	fd_putfile(sock);
    259 	fd_abort(curproc, fp2, fd);
    260 	if (__predict_false(mask))
    261 		sigsuspendteardown(l);
    262 	return error;
    263 }
    264 
    265 int
    266 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval)
    267 {
    268 	/* {
    269 		syscallarg(int)			s;
    270 		syscallarg(struct sockaddr *)	name;
    271 		syscallarg(unsigned int *)	anamelen;
    272 	} */
    273 	int error, fd;
    274 	struct sockaddr_big name;
    275 
    276 	name.sb_len = UCHAR_MAX;
    277 	error = do_sys_accept(l, SCARG(uap, s), (struct sockaddr *)&name,
    278 	    retval, NULL, 0, 0);
    279 	if (error != 0)
    280 		return error;
    281 	error = copyout_sockname_sb(SCARG(uap, name), SCARG(uap, anamelen),
    282 	    MSG_LENUSRSPACE, &name);
    283 	if (error != 0) {
    284 		fd = (int)*retval;
    285 		if (fd_getfile(fd) != NULL)
    286 			(void)fd_close(fd);
    287 	}
    288 	return error;
    289 }
    290 
    291 int
    292 sys_paccept(struct lwp *l, const struct sys_paccept_args *uap,
    293     register_t *retval)
    294 {
    295 	/* {
    296 		syscallarg(int)			s;
    297 		syscallarg(struct sockaddr *)	name;
    298 		syscallarg(unsigned int *)	anamelen;
    299 		syscallarg(const sigset_t *)	mask;
    300 		syscallarg(int)			flags;
    301 	} */
    302 	int error, fd;
    303 	struct sockaddr_big name;
    304 	sigset_t *mask, amask;
    305 
    306 	if (SCARG(uap, mask) != NULL) {
    307 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    308 		if (error)
    309 			return error;
    310 		mask = &amask;
    311 	} else
    312 		mask = NULL;
    313 
    314 	name.sb_len = UCHAR_MAX;
    315 	error = do_sys_accept(l, SCARG(uap, s), (struct sockaddr *)&name,
    316 	    retval, mask, SCARG(uap, flags), FNONBLOCK);
    317 	if (error != 0)
    318 		return error;
    319 	error = copyout_sockname_sb(SCARG(uap, name), SCARG(uap, anamelen),
    320 	    MSG_LENUSRSPACE, &name);
    321 	if (error != 0) {
    322 		fd = (int)*retval;
    323 		if (fd_getfile(fd) != NULL)
    324 			(void)fd_close(fd);
    325 	}
    326 	return error;
    327 }
    328 
    329 int
    330 sys_connect(struct lwp *l, const struct sys_connect_args *uap,
    331     register_t *retval)
    332 {
    333 	/* {
    334 		syscallarg(int)				s;
    335 		syscallarg(const struct sockaddr *)	name;
    336 		syscallarg(unsigned int)		namelen;
    337 	} */
    338 	int		error;
    339 	struct sockaddr_big sbig;
    340 
    341 	error = sockargs_sb(&sbig, SCARG(uap, name), SCARG(uap, namelen));
    342 	if (error)
    343 		return error;
    344 	return do_sys_connect(l, SCARG(uap, s), (struct sockaddr *)&sbig);
    345 }
    346 
    347 int
    348 do_sys_connect(struct lwp *l, int fd, struct sockaddr *nam)
    349 {
    350 	struct socket	*so;
    351 	int		error;
    352 	int		interrupted = 0;
    353 
    354 	if ((error = fd_getsock(fd, &so)) != 0) {
    355 		return (error);
    356 	}
    357 	solock(so);
    358 	if ((so->so_state & SS_ISCONNECTING) != 0) {
    359 		error = EALREADY;
    360 		goto out;
    361 	}
    362 
    363 	error = soconnect(so, nam, l);
    364 	if (error)
    365 		goto bad;
    366 	if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) ==
    367 	    (SS_NBIO|SS_ISCONNECTING)) {
    368 		error = EINPROGRESS;
    369 		goto out;
    370 	}
    371 	while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) {
    372 		error = sowait(so, true, 0);
    373 		if (__predict_false((so->so_state & SS_ISABORTING) != 0)) {
    374 			error = EPIPE;
    375 			interrupted = 1;
    376 			break;
    377 		}
    378 		if (error) {
    379 			if (error == EINTR || error == ERESTART)
    380 				interrupted = 1;
    381 			break;
    382 		}
    383 	}
    384 	if (error == 0) {
    385 		error = so->so_error;
    386 		so->so_error = 0;
    387 	}
    388  bad:
    389 	if (!interrupted)
    390 		so->so_state &= ~SS_ISCONNECTING;
    391 	if (error == ERESTART)
    392 		error = EINTR;
    393  out:
    394 	sounlock(so);
    395 	fd_putfile(fd);
    396 	return error;
    397 }
    398 
    399 static int
    400 makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type,
    401     int domain, int proto, struct socket *soo)
    402 {
    403 	struct socket *so;
    404 	int error;
    405 
    406 	if ((error = socreate(domain, &so, type, proto, l, soo)) != 0) {
    407 		return error;
    408 	}
    409 	if (flags & SOCK_NONBLOCK) {
    410 		so->so_state |= SS_NBIO;
    411 	}
    412 
    413 	if ((error = fd_allocfile(fp, fd)) != 0) {
    414 		soclose(so);
    415 		return error;
    416 	}
    417 	fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0);
    418 	(*fp)->f_flag = FREAD|FWRITE|
    419 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    420 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    421 	(*fp)->f_type = DTYPE_SOCKET;
    422 	(*fp)->f_ops = &socketops;
    423 	(*fp)->f_socket = so;
    424 	return 0;
    425 }
    426 
    427 int
    428 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap,
    429     register_t *retval)
    430 {
    431 	/* {
    432 		syscallarg(int)		domain;
    433 		syscallarg(int)		type;
    434 		syscallarg(int)		protocol;
    435 		syscallarg(int *)	rsv;
    436 	} */
    437 	file_t		*fp1, *fp2;
    438 	struct socket	*so1, *so2;
    439 	int		fd, error, sv[2];
    440 	proc_t		*p = curproc;
    441 	int		flags = SCARG(uap, type) & SOCK_FLAGS_MASK;
    442 	int		type = SCARG(uap, type) & ~SOCK_FLAGS_MASK;
    443 	int		domain = SCARG(uap, domain);
    444 	int		proto = SCARG(uap, protocol);
    445 
    446 	error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL);
    447 	if (error)
    448 		return error;
    449 	so1 = fp1->f_socket;
    450 	sv[0] = fd;
    451 
    452 	error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1);
    453 	if (error)
    454 		goto out;
    455 	so2 = fp2->f_socket;
    456 	sv[1] = fd;
    457 
    458 	solock(so1);
    459 	error = soconnect2(so1, so2);
    460 	if (error == 0 && type == SOCK_DGRAM) {
    461 		/*
    462 		 * Datagram socket connection is asymmetric.
    463 		 */
    464 		error = soconnect2(so2, so1);
    465 	}
    466 	sounlock(so1);
    467 
    468 	if (error == 0)
    469 		error = copyout(sv, SCARG(uap, rsv), sizeof(sv));
    470 	if (error == 0) {
    471 		fd_affix(p, fp2, sv[1]);
    472 		fd_affix(p, fp1, sv[0]);
    473 		return 0;
    474 	}
    475 	fd_abort(p, fp2, sv[1]);
    476 	(void)soclose(so2);
    477 out:
    478 	fd_abort(p, fp1, sv[0]);
    479 	(void)soclose(so1);
    480 	return error;
    481 }
    482 
    483 int
    484 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap,
    485     register_t *retval)
    486 {
    487 	/* {
    488 		syscallarg(int)				s;
    489 		syscallarg(const void *)		buf;
    490 		syscallarg(size_t)			len;
    491 		syscallarg(int)				flags;
    492 		syscallarg(const struct sockaddr *)	to;
    493 		syscallarg(unsigned int)		tolen;
    494 	} */
    495 	struct msghdr	msg = {0};
    496 	struct iovec	aiov;
    497 
    498 	msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
    499 	msg.msg_namelen = SCARG(uap, tolen);
    500 	msg.msg_iov = &aiov;
    501 	msg.msg_iovlen = 1;
    502 	msg.msg_control = NULL;
    503 	msg.msg_flags = 0;
    504 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
    505 	aiov.iov_len = SCARG(uap, len);
    506 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
    507 	    retval);
    508 }
    509 
    510 int
    511 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap,
    512     register_t *retval)
    513 {
    514 	/* {
    515 		syscallarg(int)				s;
    516 		syscallarg(const struct msghdr *)	msg;
    517 		syscallarg(int)				flags;
    518 	} */
    519 	struct msghdr	msg;
    520 	int		error;
    521 
    522 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    523 	if (error)
    524 		return (error);
    525 
    526 	msg.msg_flags = MSG_IOVUSRSPACE;
    527 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags),
    528 	    retval);
    529 }
    530 
    531 int
    532 do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp,
    533     struct msghdr *mp, int flags, register_t *retsize)
    534 {
    535 
    536 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    537 	struct sockaddr *sa = NULL;
    538 	struct mbuf	*to, *control;
    539 	struct uio	auio;
    540 	size_t		len, iovsz;
    541 	int		i, error;
    542 
    543 	ktrkuser("msghdr", mp, sizeof(*mp));
    544 
    545 	/* If the caller passed us stuff in mbufs, we must free them. */
    546 	to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL;
    547 	control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL;
    548 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    549 
    550 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    551 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    552 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    553 				error = EMSGSIZE;
    554 				goto bad;
    555 			}
    556 			iov = kmem_alloc(iovsz, KM_SLEEP);
    557 		}
    558 		if (mp->msg_iovlen != 0) {
    559 			error = copyin(mp->msg_iov, iov, iovsz);
    560 			if (error)
    561 				goto bad;
    562 		}
    563 		auio.uio_iov = iov;
    564 	} else
    565 		auio.uio_iov = mp->msg_iov;
    566 
    567 	auio.uio_iovcnt = mp->msg_iovlen;
    568 	auio.uio_rw = UIO_WRITE;
    569 	auio.uio_offset = 0;			/* XXX */
    570 	auio.uio_resid = 0;
    571 	KASSERT(l == curlwp);
    572 	auio.uio_vmspace = l->l_proc->p_vmspace;
    573 
    574 	tiov = auio.uio_iov;
    575 	for (i = 0; i < auio.uio_iovcnt; i++, tiov++) {
    576 		/*
    577 		 * Writes return ssize_t because -1 is returned on error.
    578 		 * Therefore, we must restrict the length to SSIZE_MAX to
    579 		 * avoid garbage return values.
    580 		 */
    581 		auio.uio_resid += tiov->iov_len;
    582 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    583 			error = EINVAL;
    584 			goto bad;
    585 		}
    586 	}
    587 
    588 	if (mp->msg_name && to == NULL) {
    589 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    590 		    UIO_USERSPACE, MT_SONAME);
    591 		if (error)
    592 			goto bad;
    593 	}
    594 
    595 	if (mp->msg_control) {
    596 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
    597 			error = EINVAL;
    598 			goto bad;
    599 		}
    600 		if (control == NULL) {
    601 			error = sockargs(&control, mp->msg_control,
    602 			    mp->msg_controllen, UIO_USERSPACE, MT_CONTROL);
    603 			if (error)
    604 				goto bad;
    605 		}
    606 	}
    607 
    608 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
    609 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    610 		memcpy(ktriov, auio.uio_iov, iovsz);
    611 	}
    612 
    613 	if (mp->msg_name)
    614 		MCLAIM(to, so->so_mowner);
    615 	if (mp->msg_control)
    616 		MCLAIM(control, so->so_mowner);
    617 
    618 	if (to) {
    619 		sa = mtod(to, struct sockaddr *);
    620 	}
    621 
    622 	len = auio.uio_resid;
    623 	error = (*so->so_send)(so, sa, &auio, NULL, control, flags, l);
    624 	/* Protocol is responsible for freeing 'control' */
    625 	control = NULL;
    626 
    627 	if (error) {
    628 		if (auio.uio_resid != len && (error == ERESTART ||
    629 		    error == EINTR || error == EWOULDBLOCK))
    630 			error = 0;
    631 		if (error == EPIPE && (fp->f_flag & FNOSIGPIPE) == 0 &&
    632 		    (flags & MSG_NOSIGNAL) == 0) {
    633 			mutex_enter(&proc_lock);
    634 			psignal(l->l_proc, SIGPIPE);
    635 			mutex_exit(&proc_lock);
    636 		}
    637 	}
    638 	if (error == 0)
    639 		*retsize = len - auio.uio_resid;
    640 
    641 bad:
    642 	if (ktriov != NULL) {
    643 		ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
    644 		kmem_free(ktriov, iovsz);
    645 	}
    646 
    647 	if (iov != aiov)
    648 		kmem_free(iov, iovsz);
    649 	if (to)
    650 		m_freem(to);
    651 	if (control)
    652 		m_freem(control);
    653 
    654 	return error;
    655 }
    656 
    657 int
    658 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
    659     register_t *retsize)
    660 {
    661 	int		error;
    662 	struct socket	*so;
    663 	file_t		*fp;
    664 
    665 	if ((error = fd_getsock1(s, &so, &fp)) != 0) {
    666 		/* We have to free msg_name and msg_control ourselves */
    667 		if (mp->msg_flags & MSG_NAMEMBUF)
    668 			m_freem(mp->msg_name);
    669 		if (mp->msg_flags & MSG_CONTROLMBUF)
    670 			m_freem(mp->msg_control);
    671 		return error;
    672 	}
    673 	error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize);
    674 	/* msg_name and msg_control freed */
    675 	fd_putfile(s);
    676 	return error;
    677 }
    678 
    679 int
    680 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap,
    681     register_t *retval)
    682 {
    683 	/* {
    684 		syscallarg(int)			s;
    685 		syscallarg(void *)		buf;
    686 		syscallarg(size_t)		len;
    687 		syscallarg(int)			flags;
    688 		syscallarg(struct sockaddr *)	from;
    689 		syscallarg(unsigned int *)	fromlenaddr;
    690 	} */
    691 	struct msghdr	msg = {0};
    692 	struct iovec	aiov;
    693 	int		error;
    694 	struct mbuf	*from;
    695 
    696 	msg.msg_name = NULL;
    697 	msg.msg_iov = &aiov;
    698 	msg.msg_iovlen = 1;
    699 	aiov.iov_base = SCARG(uap, buf);
    700 	aiov.iov_len = SCARG(uap, len);
    701 	msg.msg_control = NULL;
    702 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
    703 
    704 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
    705 	if (error != 0)
    706 		return error;
    707 
    708 	error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
    709 	    MSG_LENUSRSPACE, from);
    710 	if (from != NULL)
    711 		m_free(from);
    712 	return error;
    713 }
    714 
    715 int
    716 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap,
    717     register_t *retval)
    718 {
    719 	/* {
    720 		syscallarg(int)			s;
    721 		syscallarg(struct msghdr *)	msg;
    722 		syscallarg(int)			flags;
    723 	} */
    724 	struct msghdr	msg;
    725 	int		error;
    726 	struct mbuf	*from, *control;
    727 
    728 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    729 	if (error)
    730 		return error;
    731 
    732 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    733 
    734 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
    735 	    msg.msg_control != NULL ? &control : NULL, retval);
    736 	if (error != 0)
    737 		return error;
    738 
    739 	if (msg.msg_control != NULL)
    740 		error = copyout_msg_control(l, &msg, control);
    741 
    742 	if (error == 0)
    743 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
    744 			from);
    745 	if (from != NULL)
    746 		m_free(from);
    747 	if (error == 0) {
    748 		ktrkuser("msghdr", &msg, sizeof(msg));
    749 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
    750 	}
    751 
    752 	return error;
    753 }
    754 
    755 int
    756 sys_sendmmsg(struct lwp *l, const struct sys_sendmmsg_args *uap,
    757     register_t *retval)
    758 {
    759 	/* {
    760 		syscallarg(int)			s;
    761 		syscallarg(struct mmsghdr *)	mmsg;
    762 		syscallarg(unsigned int)	vlen;
    763 		syscallarg(unsigned int)	flags;
    764 	} */
    765 	struct mmsghdr mmsg;
    766 	struct socket *so;
    767 	file_t *fp;
    768 	struct msghdr *msg = &mmsg.msg_hdr;
    769 	int error, s;
    770 	unsigned int vlen, flags, dg;
    771 
    772 	s = SCARG(uap, s);
    773 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
    774 		return error;
    775 
    776 	vlen = SCARG(uap, vlen);
    777 	if (vlen > 1024)
    778 		vlen = 1024;
    779 
    780 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    781 
    782 	for (dg = 0; dg < vlen;) {
    783 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
    784 		if (error)
    785 			break;
    786 
    787 		msg->msg_flags = flags;
    788 
    789 		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
    790 		if (error)
    791 			break;
    792 
    793 		ktrkuser("msghdr", msg, sizeof(*msg));
    794 		mmsg.msg_len = *retval;
    795 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
    796 		if (error)
    797 			break;
    798 		dg++;
    799 
    800 	}
    801 
    802 	*retval = dg;
    803 
    804 	fd_putfile(s);
    805 
    806 	/*
    807 	 * If we succeeded at least once, return 0.
    808 	 */
    809 	if (dg)
    810 		return 0;
    811 	return error;
    812 }
    813 
    814 /*
    815  * Adjust for a truncated SCM_RIGHTS control message.
    816  *  This means closing any file descriptors that aren't present
    817  *  in the returned buffer.
    818  *  m is the mbuf holding the (already externalized) SCM_RIGHTS message.
    819  */
    820 static void
    821 free_rights(struct mbuf *m)
    822 {
    823 	struct cmsghdr *cm;
    824 	int *fdv;
    825 	unsigned int nfds, i;
    826 
    827 	KASSERT(sizeof(*cm) <= m->m_len);
    828 	cm = mtod(m, struct cmsghdr *);
    829 
    830 	KASSERT(CMSG_ALIGN(sizeof(*cm)) <= cm->cmsg_len);
    831 	KASSERT(cm->cmsg_len <= m->m_len);
    832 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
    833 	fdv = (int *)CMSG_DATA(cm);
    834 
    835 	for (i = 0; i < nfds; i++)
    836 		if (fd_getfile(fdv[i]) != NULL)
    837 			(void)fd_close(fdv[i]);
    838 }
    839 
    840 void
    841 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
    842 {
    843 	struct mbuf *next;
    844 	struct cmsghdr *cmsg;
    845 	bool do_free_rights = false;
    846 
    847 	while (control != NULL) {
    848 		cmsg = mtod(control, struct cmsghdr *);
    849 		if (control == uncopied)
    850 			do_free_rights = true;
    851 		if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
    852 		    && cmsg->cmsg_type == SCM_RIGHTS)
    853 			free_rights(control);
    854 		next = control->m_next;
    855 		m_free(control);
    856 		control = next;
    857 	}
    858 }
    859 
    860 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
    861 int
    862 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
    863 {
    864 	int i, len, error = 0;
    865 	struct cmsghdr *cmsg;
    866 	struct mbuf *m;
    867 	char *q;
    868 
    869 	len = mp->msg_controllen;
    870 	if (len <= 0 || control == 0) {
    871 		mp->msg_controllen = 0;
    872 		free_control_mbuf(l, control, control);
    873 		return 0;
    874 	}
    875 
    876 	q = (char *)mp->msg_control;
    877 
    878 	for (m = control; m != NULL; ) {
    879 		cmsg = mtod(m, struct cmsghdr *);
    880 		i = m->m_len;
    881 		if (len < i) {
    882 			mp->msg_flags |= MSG_CTRUNC;
    883 			if (cmsg->cmsg_level == SOL_SOCKET
    884 			    && cmsg->cmsg_type == SCM_RIGHTS)
    885 				/* Do not truncate me ... */
    886 				break;
    887 			i = len;
    888 		}
    889 		error = copyout(mtod(m, void *), q, i);
    890 		ktrkuser(mbuftypes[MT_CONTROL], cmsg, cmsg->cmsg_len);
    891 		if (error != 0) {
    892 			/* We must free all the SCM_RIGHTS */
    893 			m = control;
    894 			break;
    895 		}
    896 		m = m->m_next;
    897 		if (m)
    898 			i = ALIGN(i);
    899 		q += i;
    900 		len -= i;
    901 		if (len <= 0)
    902 			break;
    903 	}
    904 
    905 	free_control_mbuf(l, control, m);
    906 
    907 	mp->msg_controllen = q - (char *)mp->msg_control;
    908 	return error;
    909 }
    910 
    911 int
    912 do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp,
    913     struct mbuf **from, struct mbuf **control, register_t *retsize)
    914 {
    915 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    916 	struct uio	auio;
    917 	size_t		len, iovsz;
    918 	int		i, error;
    919 
    920 	ktrkuser("msghdr", mp, sizeof(*mp));
    921 
    922 	*from = NULL;
    923 	if (control != NULL)
    924 		*control = NULL;
    925 
    926 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    927 
    928 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    929 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    930 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    931 				error = EMSGSIZE;
    932 				goto out;
    933 			}
    934 			iov = kmem_alloc(iovsz, KM_SLEEP);
    935 		}
    936 		if (mp->msg_iovlen != 0) {
    937 			error = copyin(mp->msg_iov, iov, iovsz);
    938 			if (error)
    939 				goto out;
    940 		}
    941 		auio.uio_iov = iov;
    942 	} else
    943 		auio.uio_iov = mp->msg_iov;
    944 	auio.uio_iovcnt = mp->msg_iovlen;
    945 	auio.uio_rw = UIO_READ;
    946 	auio.uio_offset = 0;			/* XXX */
    947 	auio.uio_resid = 0;
    948 	KASSERT(l == curlwp);
    949 	auio.uio_vmspace = l->l_proc->p_vmspace;
    950 
    951 	tiov = auio.uio_iov;
    952 	for (i = 0; i < auio.uio_iovcnt; i++, tiov++) {
    953 		/*
    954 		 * Reads return ssize_t because -1 is returned on error.
    955 		 * Therefore we must restrict the length to SSIZE_MAX to
    956 		 * avoid garbage return values.
    957 		 */
    958 		auio.uio_resid += tiov->iov_len;
    959 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    960 			error = EINVAL;
    961 			goto out;
    962 		}
    963 	}
    964 
    965 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
    966 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    967 		memcpy(ktriov, auio.uio_iov, iovsz);
    968 	}
    969 
    970 	len = auio.uio_resid;
    971 	mp->msg_flags &= MSG_USERFLAGS;
    972 	error = (*so->so_receive)(so, from, &auio, NULL, control,
    973 	    &mp->msg_flags);
    974 	KASSERT(*from == NULL || (*from)->m_next == NULL);
    975 	len -= auio.uio_resid;
    976 	*retsize = len;
    977 	if (error != 0 && len != 0
    978 	    && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
    979 		/* Some data transferred */
    980 		error = 0;
    981 
    982 	if (ktriov != NULL) {
    983 		ktrgeniov(s, UIO_READ, ktriov, len, error);
    984 		kmem_free(ktriov, iovsz);
    985 	}
    986 
    987 	if (error != 0) {
    988 		m_freem(*from);
    989 		*from = NULL;
    990 		if (control != NULL) {
    991 			free_control_mbuf(l, *control, *control);
    992 			*control = NULL;
    993 		}
    994 	}
    995  out:
    996 	if (iov != aiov)
    997 		kmem_free(iov, iovsz);
    998 	return error;
    999 }
   1000 
   1001 
   1002 int
   1003 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp,
   1004     struct mbuf **from, struct mbuf **control, register_t *retsize)
   1005 {
   1006 	int error;
   1007 	struct socket *so;
   1008 
   1009 	if ((error = fd_getsock(s, &so)) != 0)
   1010 		return error;
   1011 	error = do_sys_recvmsg_so(l, s, so, mp, from, control, retsize);
   1012 	fd_putfile(s);
   1013 	return error;
   1014 }
   1015 
   1016 int
   1017 sys_recvmmsg(struct lwp *l, const struct sys_recvmmsg_args *uap,
   1018     register_t *retval)
   1019 {
   1020 	/* {
   1021 		syscallarg(int)			s;
   1022 		syscallarg(struct mmsghdr *)	mmsg;
   1023 		syscallarg(unsigned int)	vlen;
   1024 		syscallarg(unsigned int)	flags;
   1025 		syscallarg(struct timespec *)	timeout;
   1026 	} */
   1027 	struct mmsghdr mmsg;
   1028 	struct socket *so;
   1029 	struct msghdr *msg = &mmsg.msg_hdr;
   1030 	int error, s;
   1031 	struct mbuf *from, *control;
   1032 	struct timespec ts, now;
   1033 	unsigned int vlen, flags, dg;
   1034 
   1035 	if (SCARG(uap, timeout)) {
   1036 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
   1037 			return error;
   1038 		if (ts.tv_nsec < 0 || ts.tv_nsec >= 1000000000L)
   1039 			return EINVAL;
   1040 		getnanotime(&now);
   1041 		if (timespecaddok(&now, &ts)) {
   1042 			timespecadd(&now, &ts, &ts);
   1043 		} else {
   1044 			ts.tv_sec = __type_max(time_t);
   1045 			ts.tv_nsec = 999999999L;
   1046 		}
   1047 	}
   1048 
   1049 	s = SCARG(uap, s);
   1050 	if ((error = fd_getsock(s, &so)) != 0)
   1051 		return error;
   1052 
   1053 	/*
   1054 	 * If so->so_rerror holds a deferred error return it now.
   1055 	 */
   1056 	if (so->so_rerror) {
   1057 		error = so->so_rerror;
   1058 		so->so_rerror = 0;
   1059 		fd_putfile(s);
   1060 		return error;
   1061 	}
   1062 
   1063 	vlen = SCARG(uap, vlen);
   1064 	if (vlen > 1024)
   1065 		vlen = 1024;
   1066 
   1067 	from = NULL;
   1068 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
   1069 
   1070 	for (dg = 0; dg < vlen;) {
   1071 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
   1072 		if (error)
   1073 			break;
   1074 
   1075 		msg->msg_flags = flags & ~MSG_WAITFORONE;
   1076 
   1077 		if (from != NULL) {
   1078 			m_free(from);
   1079 			from = NULL;
   1080 		}
   1081 
   1082 		error = do_sys_recvmsg_so(l, s, so, msg, &from,
   1083 		    msg->msg_control != NULL ? &control : NULL, retval);
   1084 		if (error) {
   1085 			if (error == EAGAIN && dg > 0)
   1086 				error = 0;
   1087 			break;
   1088 		}
   1089 
   1090 		if (msg->msg_control != NULL)
   1091 			error = copyout_msg_control(l, msg, control);
   1092 		if (error)
   1093 			break;
   1094 
   1095 		error = copyout_sockname(msg->msg_name, &msg->msg_namelen, 0,
   1096 		    from);
   1097 		if (error)
   1098 			break;
   1099 
   1100 		ktrkuser("msghdr", msg, sizeof *msg);
   1101 		mmsg.msg_len = *retval;
   1102 
   1103 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
   1104 		if (error)
   1105 			break;
   1106 
   1107 		dg++;
   1108 		if (msg->msg_flags & MSG_OOB)
   1109 			break;
   1110 
   1111 		if (SCARG(uap, timeout)) {
   1112 			getnanotime(&now);
   1113 			if (timespeccmp(&ts, &now, <))
   1114 				break;
   1115 		}
   1116 
   1117 		if (flags & MSG_WAITFORONE)
   1118 			flags |= MSG_DONTWAIT;
   1119 
   1120 	}
   1121 
   1122 	if (from != NULL)
   1123 		m_free(from);
   1124 
   1125 	*retval = dg;
   1126 
   1127 	/*
   1128 	 * If we succeeded at least once, return 0, hopefully so->so_rerror
   1129 	 * will catch it next time.
   1130 	 */
   1131 	if (error && dg > 0) {
   1132 		so->so_rerror = error;
   1133 		error = 0;
   1134 	}
   1135 
   1136 	fd_putfile(s);
   1137 
   1138 	return error;
   1139 }
   1140 
   1141 int
   1142 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap,
   1143     register_t *retval)
   1144 {
   1145 	/* {
   1146 		syscallarg(int)	s;
   1147 		syscallarg(int)	how;
   1148 	} */
   1149 	struct socket	*so;
   1150 	int		error;
   1151 
   1152 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
   1153 		return error;
   1154 	solock(so);
   1155 	error = soshutdown(so, SCARG(uap, how));
   1156 	sounlock(so);
   1157 	fd_putfile(SCARG(uap, s));
   1158 	return error;
   1159 }
   1160 
   1161 int
   1162 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap,
   1163     register_t *retval)
   1164 {
   1165 	/* {
   1166 		syscallarg(int)			s;
   1167 		syscallarg(int)			level;
   1168 		syscallarg(int)			name;
   1169 		syscallarg(const void *)	val;
   1170 		syscallarg(unsigned int)	valsize;
   1171 	} */
   1172 	struct sockopt	sopt;
   1173 	struct socket	*so;
   1174 	file_t		*fp;
   1175 	int		error;
   1176 	unsigned int	len;
   1177 
   1178 	len = SCARG(uap, valsize);
   1179 	if (len > 0 && SCARG(uap, val) == NULL)
   1180 		return EINVAL;
   1181 
   1182 	if (len > MCLBYTES)
   1183 		return EINVAL;
   1184 
   1185 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
   1186 		return (error);
   1187 
   1188 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len);
   1189 
   1190 	if (len > 0) {
   1191 		error = copyin(SCARG(uap, val), sopt.sopt_data, len);
   1192 		if (error)
   1193 			goto out;
   1194 	}
   1195 
   1196 	error = sosetopt(so, &sopt);
   1197 	if (so->so_options & SO_NOSIGPIPE)
   1198 		atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
   1199 	else
   1200 		atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
   1201 
   1202  out:
   1203 	sockopt_destroy(&sopt);
   1204 	fd_putfile(SCARG(uap, s));
   1205 	return error;
   1206 }
   1207 
   1208 static int
   1209 getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap,
   1210     register_t *retval, bool copyarg)
   1211 {
   1212 	struct sockopt	sopt;
   1213 	struct socket	*so;
   1214 	file_t		*fp;
   1215 	unsigned int	valsize, len;
   1216 	int		error;
   1217 
   1218 	if (SCARG(uap, val) != NULL) {
   1219 		error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize));
   1220 		if (error)
   1221 			return error;
   1222 	} else
   1223 		valsize = 0;
   1224 
   1225 	if (valsize > MCLBYTES)
   1226 		return EINVAL;
   1227 
   1228 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
   1229 		return error;
   1230 
   1231 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), valsize);
   1232 	if (copyarg && valsize > 0) {
   1233 		error = copyin(SCARG(uap, val), sopt.sopt_data, valsize);
   1234 		if (error)
   1235 			goto out;
   1236 	}
   1237 
   1238 	if (fp->f_flag & FNOSIGPIPE)
   1239 		so->so_options |= SO_NOSIGPIPE;
   1240 	else
   1241 		so->so_options &= ~SO_NOSIGPIPE;
   1242 
   1243 	error = sogetopt(so, &sopt);
   1244 	if (error || valsize == 0)
   1245 		goto out;
   1246 
   1247 	len = uimin(valsize, sopt.sopt_retsize);
   1248 	error = copyout(sopt.sopt_data, SCARG(uap, val), len);
   1249 	if (error)
   1250 		goto out;
   1251 
   1252 	error = copyout(&len, SCARG(uap, avalsize), sizeof(len));
   1253  out:
   1254 	sockopt_destroy(&sopt);
   1255 	fd_putfile(SCARG(uap, s));
   1256 	return error;
   1257 }
   1258 
   1259 int
   1260 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap,
   1261     register_t *retval)
   1262 {
   1263 	/* {
   1264 		syscallarg(int)			s;
   1265 		syscallarg(int)			level;
   1266 		syscallarg(int)			name;
   1267 		syscallarg(void *)		val;
   1268 		syscallarg(unsigned int *)	avalsize;
   1269 	} */
   1270 	return getsockopt(l, uap, retval, false);
   1271 }
   1272 
   1273 int
   1274 sys_getsockopt2(struct lwp *l, const struct sys_getsockopt2_args *uap,
   1275     register_t *retval)
   1276 {
   1277 	/* {
   1278 		syscallarg(int)			s;
   1279 		syscallarg(int)			level;
   1280 		syscallarg(int)			name;
   1281 		syscallarg(void *)		val;
   1282 		syscallarg(unsigned int *)	avalsize;
   1283 	} */
   1284 	return getsockopt(l, (const struct sys_getsockopt_args *) uap, retval, true);
   1285 }
   1286 
   1287 #ifdef PIPE_SOCKETPAIR
   1288 
   1289 int
   1290 pipe1(struct lwp *l, int *fildes, int flags)
   1291 {
   1292 	file_t		*rf, *wf;
   1293 	struct socket	*rso, *wso;
   1294 	int		fd, error;
   1295 	proc_t		*p;
   1296 
   1297 	if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
   1298 		return EINVAL;
   1299 	p = curproc;
   1300 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
   1301 		return error;
   1302 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
   1303 		goto free1;
   1304 	/* remember this socket pair implements a pipe */
   1305 	wso->so_state |= SS_ISAPIPE;
   1306 	rso->so_state |= SS_ISAPIPE;
   1307 	if ((error = fd_allocfile(&rf, &fd)) != 0)
   1308 		goto free2;
   1309 	fildes[0] = fd;
   1310 	rf->f_flag = FREAD | flags;
   1311 	rf->f_type = DTYPE_SOCKET;
   1312 	rf->f_ops = &socketops;
   1313 	rf->f_socket = rso;
   1314 	if ((error = fd_allocfile(&wf, &fd)) != 0)
   1315 		goto free3;
   1316 	wf->f_flag = FWRITE | flags;
   1317 	wf->f_type = DTYPE_SOCKET;
   1318 	wf->f_ops = &socketops;
   1319 	wf->f_socket = wso;
   1320 	fildes[1] = fd;
   1321 	solock(wso);
   1322 	/*
   1323 	 * Pipes must be readable when there is at least 1
   1324 	 * byte of data available in the receive buffer.
   1325 	 *
   1326 	 * Pipes must be writable when there is space for
   1327 	 * at least PIPE_BUF bytes in the send buffer.
   1328 	 * If we're increasing the low water mark for the
   1329 	 * send buffer, then mimic how soreserve() would
   1330 	 * have set the high water mark.
   1331 	 */
   1332 	rso->so_rcv.sb_lowat = 1;
   1333 	if (wso->so_snd.sb_lowat < PIPE_BUF) {
   1334 		wso->so_snd.sb_hiwat = PIPE_BUF * 2;
   1335 	}
   1336 	wso->so_snd.sb_lowat = PIPE_BUF;
   1337 	error = unp_connect2(wso, rso);
   1338 	sounlock(wso);
   1339 	if (error != 0)
   1340 		goto free4;
   1341 	fd_affix(p, wf, fildes[1]);
   1342 	fd_affix(p, rf, fildes[0]);
   1343 	return (0);
   1344  free4:
   1345 	fd_abort(p, wf, fildes[1]);
   1346  free3:
   1347 	fd_abort(p, rf, fildes[0]);
   1348  free2:
   1349 	(void)soclose(wso);
   1350  free1:
   1351 	(void)soclose(rso);
   1352 	return error;
   1353 }
   1354 #endif /* PIPE_SOCKETPAIR */
   1355 
   1356 /*
   1357  * Get peer socket name.
   1358  */
   1359 int
   1360 do_sys_getpeername(int fd, struct sockaddr *nam)
   1361 {
   1362 	struct socket	*so;
   1363 	int		error;
   1364 
   1365 	if ((error = fd_getsock(fd, &so)) != 0)
   1366 		return error;
   1367 
   1368 	solock(so);
   1369 	if ((so->so_state & SS_ISCONNECTED) == 0)
   1370 		error = ENOTCONN;
   1371 	else {
   1372 		error = (*so->so_proto->pr_usrreqs->pr_peeraddr)(so, nam);
   1373 	}
   1374 	sounlock(so);
   1375 	fd_putfile(fd);
   1376 	return error;
   1377 }
   1378 
   1379 /*
   1380  * Get local socket name.
   1381  */
   1382 int
   1383 do_sys_getsockname(int fd, struct sockaddr *nam)
   1384 {
   1385 	struct socket	*so;
   1386 	int		error;
   1387 
   1388 	if ((error = fd_getsock(fd, &so)) != 0)
   1389 		return error;
   1390 
   1391 	solock(so);
   1392 	error = (*so->so_proto->pr_usrreqs->pr_sockaddr)(so, nam);
   1393 	sounlock(so);
   1394 	fd_putfile(fd);
   1395 	return error;
   1396 }
   1397 
   1398 int
   1399 copyout_sockname_sb(struct sockaddr *asa, unsigned int *alen, int flags,
   1400     struct sockaddr_big *addr)
   1401 {
   1402 	unsigned int len;
   1403 	int error;
   1404 
   1405 	if (asa == NULL)
   1406 		/* Assume application not interested */
   1407 		return 0;
   1408 
   1409 	if (flags & MSG_LENUSRSPACE) {
   1410 		error = copyin(alen, &len, sizeof(len));
   1411 		if (error)
   1412 			return error;
   1413 	} else
   1414 		len = *alen;
   1415 
   1416 	if (addr == NULL) {
   1417 		len = 0;
   1418 		error = 0;
   1419 	} else {
   1420 		if (len > addr->sb_len)
   1421 			len = addr->sb_len;
   1422 		/* XXX addr isn't an mbuf... */
   1423 		ktrkuser(mbuftypes[MT_SONAME], addr, len);
   1424 		error = copyout(addr, asa, len);
   1425 	}
   1426 
   1427 	if (error == 0) {
   1428 		if (flags & MSG_LENUSRSPACE)
   1429 			error = copyout(&len, alen, sizeof(len));
   1430 		else
   1431 			*alen = len;
   1432 	}
   1433 
   1434 	return error;
   1435 }
   1436 
   1437 int
   1438 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
   1439     struct mbuf *addr)
   1440 {
   1441 	int len;
   1442 	int error;
   1443 
   1444 	if (asa == NULL)
   1445 		/* Assume application not interested */
   1446 		return 0;
   1447 
   1448 	if (flags & MSG_LENUSRSPACE) {
   1449 		error = copyin(alen, &len, sizeof(len));
   1450 		if (error)
   1451 			return error;
   1452 	} else
   1453 		len = *alen;
   1454 	if (len < 0)
   1455 		return EINVAL;
   1456 
   1457 	if (addr == NULL) {
   1458 		len = 0;
   1459 		error = 0;
   1460 	} else {
   1461 		if (len > addr->m_len)
   1462 			len = addr->m_len;
   1463 		/* Maybe this ought to copy a chain ? */
   1464 		ktrkuser(mbuftypes[MT_SONAME], mtod(addr, void *), len);
   1465 		error = copyout(mtod(addr, void *), asa, len);
   1466 	}
   1467 
   1468 	if (error == 0) {
   1469 		if (flags & MSG_LENUSRSPACE)
   1470 			error = copyout(&len, alen, sizeof(len));
   1471 		else
   1472 			*alen = len;
   1473 	}
   1474 
   1475 	return error;
   1476 }
   1477 
   1478 /*
   1479  * Get socket name.
   1480  */
   1481 int
   1482 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap,
   1483     register_t *retval)
   1484 {
   1485 	/* {
   1486 		syscallarg(int)			fdes;
   1487 		syscallarg(struct sockaddr *)	asa;
   1488 		syscallarg(unsigned int *)	alen;
   1489 	} */
   1490 	struct sockaddr_big sbig;
   1491 	int		    error;
   1492 
   1493 	sbig.sb_len = UCHAR_MAX;
   1494 	error = do_sys_getsockname(SCARG(uap, fdes), (struct sockaddr *)&sbig);
   1495 	if (error != 0)
   1496 		return error;
   1497 
   1498 	error = copyout_sockname_sb(SCARG(uap, asa), SCARG(uap, alen),
   1499 	    MSG_LENUSRSPACE, &sbig);
   1500 	return error;
   1501 }
   1502 
   1503 /*
   1504  * Get name of peer for connected socket.
   1505  */
   1506 int
   1507 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap,
   1508     register_t *retval)
   1509 {
   1510 	/* {
   1511 		syscallarg(int)			fdes;
   1512 		syscallarg(struct sockaddr *)	asa;
   1513 		syscallarg(unsigned int *)	alen;
   1514 	} */
   1515 	struct sockaddr_big sbig;
   1516 	int		    error;
   1517 
   1518 	sbig.sb_len = UCHAR_MAX;
   1519 	error = do_sys_getpeername(SCARG(uap, fdes), (struct sockaddr *)&sbig);
   1520 	if (error != 0)
   1521 		return error;
   1522 
   1523 	error = copyout_sockname_sb(SCARG(uap, asa), SCARG(uap, alen),
   1524 	    MSG_LENUSRSPACE, &sbig);
   1525 	return error;
   1526 }
   1527 
   1528 static int
   1529 sockargs_sb(struct sockaddr_big *sb, const void *name, socklen_t buflen)
   1530 {
   1531 	int error;
   1532 
   1533 	/*
   1534 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1535 	 * will overflow sb_len. Further no reasonable buflen is <=
   1536 	 * offsetof(sockaddr_big, sb_data) since it shall be at least
   1537 	 * the size of the preamble sb_len and sb_family members.
   1538 	 */
   1539 	if (buflen > UCHAR_MAX ||
   1540 	    buflen <= offsetof(struct sockaddr_big, sb_data))
   1541 		return EINVAL;
   1542 
   1543 	error = copyin(name, (void *)sb, buflen);
   1544 	if (error)
   1545 		return error;
   1546 
   1547 	ktrkuser(mbuftypes[MT_SONAME], sb, buflen);
   1548 #if BYTE_ORDER != BIG_ENDIAN
   1549 	/*
   1550 	 * 4.3BSD compat thing - need to stay, since bind(2),
   1551 	 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1552 	 */
   1553 	if (sb->sb_family == 0 && sb->sb_len < AF_MAX)
   1554 		sb->sb_family = sb->sb_len;
   1555 #endif
   1556 	sb->sb_len = buflen;
   1557 	return 0;
   1558 }
   1559 
   1560 /*
   1561  * XXX In a perfect world, we wouldn't pass around socket control
   1562  * XXX arguments in mbufs, and this could go away.
   1563  */
   1564 int
   1565 sockargs(struct mbuf **mp, const void *bf, size_t buflen, enum uio_seg seg,
   1566     int type)
   1567 {
   1568 	struct mbuf	*m;
   1569 	int		error;
   1570 
   1571 	/*
   1572 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1573 	 * will overflow sa_len.  Control data more than a page size in
   1574 	 * length is just too much.
   1575 	 */
   1576 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1577 		return EINVAL;
   1578 
   1579 	/*
   1580 	 * length must greater than sizeof(sa_family) + sizeof(sa_len)
   1581 	 */
   1582 	if (type == MT_SONAME && buflen <= 2)
   1583 		return EINVAL;
   1584 
   1585 	/* Allocate an mbuf to hold the arguments. */
   1586 	m = m_get(M_WAIT, type);
   1587 	/* can't claim.  don't who to assign it to. */
   1588 	if (buflen > MLEN) {
   1589 		/*
   1590 		 * Won't fit into a regular mbuf, so we allocate just
   1591 		 * enough external storage to hold the argument.
   1592 		 */
   1593 		MEXTMALLOC(m, buflen, M_WAITOK);
   1594 	}
   1595 	m->m_len = buflen;
   1596 	if (seg == UIO_USERSPACE) {
   1597 		error = copyin(bf, mtod(m, void *), buflen);
   1598 		if (error) {
   1599 			(void)m_free(m);
   1600 			return error;
   1601 		}
   1602 	} else {
   1603 		memcpy(mtod(m, void *), bf, buflen);
   1604 	}
   1605 	*mp = m;
   1606 	switch (type) {
   1607 	case MT_SONAME:
   1608 		ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
   1609 
   1610 		struct sockaddr *sa = mtod(m, struct sockaddr *);
   1611 #if BYTE_ORDER != BIG_ENDIAN
   1612 		/*
   1613 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1614 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1615 		 */
   1616 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1617 			sa->sa_family = sa->sa_len;
   1618 #endif
   1619 		sa->sa_len = buflen;
   1620 		return 0;
   1621 	case MT_CONTROL:
   1622 		if (!KTRPOINT(curproc, KTR_USER))
   1623 			return 0;
   1624 
   1625 		struct msghdr mhdr;
   1626 		mhdr.msg_control = mtod(m, void *);
   1627 		mhdr.msg_controllen = buflen;
   1628 		for (struct cmsghdr *cmsg = CMSG_FIRSTHDR(&mhdr); cmsg;
   1629 		    cmsg = CMSG_NXTHDR(&mhdr, cmsg)) {
   1630 			KASSERT(((char *)cmsg - mtod(m, char *)) <= buflen);
   1631 			if (cmsg->cmsg_len >
   1632 			    buflen - ((char *)cmsg - mtod(m, char *)))
   1633 				break;
   1634 			ktrkuser(mbuftypes[type], cmsg, cmsg->cmsg_len);
   1635 		}
   1636 		return 0;
   1637 	default:
   1638 		return EINVAL;
   1639 	}
   1640 }
   1641 
   1642 int
   1643 do_sys_peeloff(struct socket *head, void *data)
   1644 {
   1645 #ifdef SCTP
   1646 	/*file_t *lfp = NULL;*/
   1647 	file_t *nfp = NULL;
   1648 	int error;
   1649 	struct socket *so;
   1650 	int fd;
   1651 	uint32_t name;
   1652 	/*short fflag;*/		/* type must match fp->f_flag */
   1653 
   1654 	name = *(uint32_t *) data;
   1655 	error = sctp_can_peel_off(head, name);
   1656 	if (error) {
   1657 		printf("peeloff failed\n");
   1658 		return error;
   1659 	}
   1660 	/*
   1661 	 * At this point we know we do have a assoc to pull
   1662 	 * we proceed to get the fd setup. This may block
   1663 	 * but that is ok.
   1664 	 */
   1665 	error = fd_allocfile(&nfp, &fd);
   1666 	if (error) {
   1667 		/*
   1668 		 * Probably ran out of file descriptors. Put the
   1669 		 * unaccepted connection back onto the queue and
   1670 		 * do another wakeup so some other process might
   1671 		 * have a chance at it.
   1672 		 */
   1673 		return error;
   1674 	}
   1675 	*(int *) data = fd;
   1676 
   1677 	so = sctp_get_peeloff(head, name, &error);
   1678 	if (so == NULL) {
   1679 		/*
   1680 		 * Either someone else peeled it off OR
   1681 		 * we can't get a socket.
   1682 		 * close the new descriptor, assuming someone hasn't ripped it
   1683 		 * out from under us.
   1684 		 */
   1685 		mutex_enter(&nfp->f_lock);
   1686 		nfp->f_count++;
   1687 		mutex_exit(&nfp->f_lock);
   1688 		fd_abort(curlwp->l_proc, nfp, fd);
   1689 		return error;
   1690 	}
   1691 	so->so_state &= ~SS_NOFDREF;
   1692 	so->so_state &= ~SS_ISCONNECTING;
   1693 	so->so_head = NULL;
   1694 	so->so_cred = kauth_cred_hold(head->so_cred);
   1695 	nfp->f_socket = so;
   1696 	nfp->f_flag = FREAD|FWRITE;
   1697 	nfp->f_ops = &socketops;
   1698 	nfp->f_type = DTYPE_SOCKET;
   1699 
   1700 	fd_affix(curlwp->l_proc, nfp, fd);
   1701 
   1702 	return error;
   1703 #else
   1704 	return EOPNOTSUPP;
   1705 #endif
   1706 }
   1707