Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.147
      1 /*	$NetBSD: uipc_syscalls.c,v 1.147 2011/09/21 18:10:25 christos Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 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.147 2011/09/21 18:10:25 christos Exp $");
     65 
     66 #include "opt_pipe.h"
     67 
     68 #include <sys/param.h>
     69 #include <sys/systm.h>
     70 #include <sys/filedesc.h>
     71 #include <sys/proc.h>
     72 #include <sys/file.h>
     73 #include <sys/buf.h>
     74 #define MBUFTYPES
     75 #include <sys/mbuf.h>
     76 #include <sys/protosw.h>
     77 #include <sys/socket.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/signalvar.h>
     80 #include <sys/un.h>
     81 #include <sys/ktrace.h>
     82 #include <sys/event.h>
     83 #include <sys/kauth.h>
     84 
     85 #include <sys/mount.h>
     86 #include <sys/syscallargs.h>
     87 
     88 /*
     89  * System call interface to the socket abstraction.
     90  */
     91 extern const struct fileops socketops;
     92 
     93 int
     94 sys___socket30(struct lwp *l, const struct sys___socket30_args *uap, register_t *retval)
     95 {
     96 	/* {
     97 		syscallarg(int)	domain;
     98 		syscallarg(int)	type;
     99 		syscallarg(int)	protocol;
    100 	} */
    101 	int		fd, error;
    102 
    103 	error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type),
    104 			 SCARG(uap, protocol), l, &fd);
    105 	if (error == 0)
    106 		*retval = fd;
    107 	return error;
    108 }
    109 
    110 /* ARGSUSED */
    111 int
    112 sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval)
    113 {
    114 	/* {
    115 		syscallarg(int)				s;
    116 		syscallarg(const struct sockaddr *)	name;
    117 		syscallarg(unsigned int)		namelen;
    118 	} */
    119 	struct mbuf	*nam;
    120 	int		error;
    121 
    122 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    123 	    MT_SONAME);
    124 	if (error)
    125 		return error;
    126 
    127 	return do_sys_bind(l, SCARG(uap, s), nam);
    128 }
    129 
    130 int
    131 do_sys_bind(struct lwp *l, int fd, struct mbuf *nam)
    132 {
    133 	struct socket	*so;
    134 	int		error;
    135 
    136 	if ((error = fd_getsock(fd, &so)) != 0) {
    137 		m_freem(nam);
    138 		return (error);
    139 	}
    140 	MCLAIM(nam, so->so_mowner);
    141 	error = sobind(so, nam, l);
    142 	m_freem(nam);
    143 	fd_putfile(fd);
    144 	return error;
    145 }
    146 
    147 /* ARGSUSED */
    148 int
    149 sys_listen(struct lwp *l, const struct sys_listen_args *uap, register_t *retval)
    150 {
    151 	/* {
    152 		syscallarg(int)	s;
    153 		syscallarg(int)	backlog;
    154 	} */
    155 	struct socket	*so;
    156 	int		error;
    157 
    158 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    159 		return (error);
    160 	error = solisten(so, SCARG(uap, backlog), l);
    161 	fd_putfile(SCARG(uap, s));
    162 	return error;
    163 }
    164 
    165 int
    166 do_sys_accept(struct lwp *l, int sock, struct mbuf **name, register_t *new_sock,
    167     const sigset_t *mask, int flags, int clrflags)
    168 {
    169 	file_t		*fp, *fp2;
    170 	struct mbuf	*nam;
    171 	int		error, fd;
    172 	struct socket	*so, *so2;
    173 	short		wakeup_state = 0;
    174 
    175 	if ((fp = fd_getfile(sock)) == NULL)
    176 		return (EBADF);
    177 	if (fp->f_type != DTYPE_SOCKET) {
    178 		fd_putfile(sock);
    179 		return (ENOTSOCK);
    180 	}
    181 	if ((error = fd_allocfile(&fp2, &fd)) != 0) {
    182 		fd_putfile(sock);
    183 		return (error);
    184 	}
    185 	nam = m_get(M_WAIT, MT_SONAME);
    186 	*new_sock = fd;
    187 	so = fp->f_data;
    188 	solock(so);
    189 
    190 	if (__predict_false(mask))
    191 		sigsuspendsetup(l, mask);
    192 
    193 	if (!(so->so_proto->pr_flags & PR_LISTEN)) {
    194 		error = EOPNOTSUPP;
    195 		goto bad;
    196 	}
    197 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    198 		error = EINVAL;
    199 		goto bad;
    200 	}
    201 	if (so->so_nbio && so->so_qlen == 0) {
    202 		error = EWOULDBLOCK;
    203 		goto bad;
    204 	}
    205 	while (so->so_qlen == 0 && so->so_error == 0) {
    206 		if (so->so_state & SS_CANTRCVMORE) {
    207 			so->so_error = ECONNABORTED;
    208 			break;
    209 		}
    210 		if (wakeup_state & SS_RESTARTSYS) {
    211 			error = ERESTART;
    212 			goto bad;
    213 		}
    214 		error = sowait(so, true, 0);
    215 		if (error) {
    216 			goto bad;
    217 		}
    218 		wakeup_state = so->so_state;
    219 	}
    220 	if (so->so_error) {
    221 		error = so->so_error;
    222 		so->so_error = 0;
    223 		goto bad;
    224 	}
    225 	/* connection has been removed from the listen queue */
    226 	KNOTE(&so->so_rcv.sb_sel.sel_klist, NOTE_SUBMIT);
    227 	so2 = TAILQ_FIRST(&so->so_q);
    228 	if (soqremque(so2, 1) == 0)
    229 		panic("accept");
    230 	fp2->f_type = DTYPE_SOCKET;
    231 	fp2->f_flag = (fp->f_flag & ~clrflags) |
    232 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0);
    233 	fp2->f_ops = &socketops;
    234 	fp2->f_data = so2;
    235 	error = soaccept(so2, nam);
    236 	so2->so_cred = kauth_cred_dup(so->so_cred);
    237 	sounlock(so);
    238 	if (error) {
    239 		/* an error occurred, free the file descriptor and mbuf */
    240 		m_freem(nam);
    241 		mutex_enter(&fp2->f_lock);
    242 		fp2->f_count++;
    243 		mutex_exit(&fp2->f_lock);
    244 		closef(fp2);
    245 		fd_abort(curproc, NULL, fd);
    246 	} else {
    247 		fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
    248 		fd_affix(curproc, fp2, fd);
    249 		*name = nam;
    250 	}
    251 	fd_putfile(sock);
    252 	if (__predict_false(mask))
    253 		sigsuspendteardown(l);
    254 	return (error);
    255  bad:
    256  	sounlock(so);
    257  	m_freem(nam);
    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 mbuf *name;
    275 
    276 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, NULL, 0, 0);
    277 	if (error != 0)
    278 		return error;
    279 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    280 	    MSG_LENUSRSPACE, name);
    281 	if (name != NULL)
    282 		m_free(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 mbuf *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 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, mask,
    315 	    SCARG(uap, flags), FNONBLOCK);
    316 	if (error != 0)
    317 		return error;
    318 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    319 	    MSG_LENUSRSPACE, name);
    320 	if (name != NULL)
    321 		m_free(name);
    322 	if (error != 0) {
    323 		fd = (int)*retval;
    324 		if (fd_getfile(fd) != NULL)
    325 			(void)fd_close(fd);
    326 	}
    327 	return error;
    328 }
    329 
    330 /* ARGSUSED */
    331 int
    332 sys_connect(struct lwp *l, const struct sys_connect_args *uap, register_t *retval)
    333 {
    334 	/* {
    335 		syscallarg(int)				s;
    336 		syscallarg(const struct sockaddr *)	name;
    337 		syscallarg(unsigned int)		namelen;
    338 	} */
    339 	int		error;
    340 	struct mbuf	*nam;
    341 
    342 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    343 	    MT_SONAME);
    344 	if (error)
    345 		return error;
    346 	return do_sys_connect(l,  SCARG(uap, s), nam);
    347 }
    348 
    349 int
    350 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam)
    351 {
    352 	struct socket	*so;
    353 	int		error;
    354 	int		interrupted = 0;
    355 
    356 	if ((error = fd_getsock(fd, &so)) != 0) {
    357 		m_freem(nam);
    358 		return (error);
    359 	}
    360 	solock(so);
    361 	MCLAIM(nam, so->so_mowner);
    362 	if ((so->so_state & SS_ISCONNECTING) != 0) {
    363 		error = EALREADY;
    364 		goto out;
    365 	}
    366 
    367 	error = soconnect(so, nam, l);
    368 	if (error)
    369 		goto bad;
    370 	if (so->so_nbio && (so->so_state & SS_ISCONNECTING) != 0) {
    371 		error = EINPROGRESS;
    372 		goto out;
    373 	}
    374 	while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) {
    375 		error = sowait(so, true, 0);
    376 		if (__predict_false((so->so_state & SS_ISABORTING) != 0)) {
    377 			error = EPIPE;
    378 			interrupted = 1;
    379 			break;
    380 		}
    381 		if (error) {
    382 			if (error == EINTR || error == ERESTART)
    383 				interrupted = 1;
    384 			break;
    385 		}
    386 	}
    387 	if (error == 0) {
    388 		error = so->so_error;
    389 		so->so_error = 0;
    390 	}
    391  bad:
    392 	if (!interrupted)
    393 		so->so_state &= ~SS_ISCONNECTING;
    394 	if (error == ERESTART)
    395 		error = EINTR;
    396  out:
    397  	sounlock(so);
    398  	fd_putfile(fd);
    399 	m_freem(nam);
    400 	return (error);
    401 }
    402 
    403 int
    404 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap, register_t *retval)
    405 {
    406 	/* {
    407 		syscallarg(int)		domain;
    408 		syscallarg(int)		type;
    409 		syscallarg(int)		protocol;
    410 		syscallarg(int *)	rsv;
    411 	} */
    412 	file_t		*fp1, *fp2;
    413 	struct socket	*so1, *so2;
    414 	int		fd, error, sv[2];
    415 	proc_t		*p;
    416 	int		flags = SCARG(uap, type) & SOCK_FLAGS_MASK;
    417 	int		type = SCARG(uap, type) & ~SOCK_FLAGS_MASK;
    418 	int		fnonblock = (flags & SOCK_NONBLOCK) ? FNONBLOCK : 0;
    419 
    420 	p = curproc;
    421 	error = socreate(SCARG(uap, domain), &so1, type,
    422 	    SCARG(uap, protocol), l, NULL);
    423 	if (error)
    424 		return (error);
    425 	error = socreate(SCARG(uap, domain), &so2, type,
    426 	    SCARG(uap, protocol), l, so1);
    427 	if (error)
    428 		goto free1;
    429 	if ((error = fd_allocfile(&fp1, &fd)) != 0)
    430 		goto free2;
    431 	fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
    432 	sv[0] = fd;
    433 	fp1->f_flag = FREAD|FWRITE|fnonblock;
    434 	fp1->f_type = DTYPE_SOCKET;
    435 	fp1->f_ops = &socketops;
    436 	fp1->f_data = so1;
    437 	if ((error = fd_allocfile(&fp2, &fd)) != 0)
    438 		goto free3;
    439 	fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
    440 	fp2->f_flag = FREAD|FWRITE|fnonblock;
    441 	fp2->f_type = DTYPE_SOCKET;
    442 	fp2->f_ops = &socketops;
    443 	fp2->f_data = so2;
    444 	sv[1] = fd;
    445 	solock(so1);
    446 	error = soconnect2(so1, so2);
    447 	if (error == 0 && SCARG(uap, type) == SOCK_DGRAM) {
    448 		/*
    449 		 * Datagram socket connection is asymmetric.
    450 		 */
    451 		error = soconnect2(so2, so1);
    452 	}
    453 	sounlock(so1);
    454 	if (error == 0)
    455 		error = copyout(sv, SCARG(uap, rsv), 2 * sizeof(int));
    456 	if (error == 0) {
    457 		fd_affix(p, fp2, sv[1]);
    458 		fd_affix(p, fp1, sv[0]);
    459 		return (0);
    460 	}
    461 	fd_abort(p, fp2, sv[1]);
    462  free3:
    463 	fd_abort(p, fp1, sv[0]);
    464  free2:
    465 	(void)soclose(so2);
    466  free1:
    467 	(void)soclose(so1);
    468 	return (error);
    469 }
    470 
    471 int
    472 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap, register_t *retval)
    473 {
    474 	/* {
    475 		syscallarg(int)				s;
    476 		syscallarg(const void *)		buf;
    477 		syscallarg(size_t)			len;
    478 		syscallarg(int)				flags;
    479 		syscallarg(const struct sockaddr *)	to;
    480 		syscallarg(unsigned int)		tolen;
    481 	} */
    482 	struct msghdr	msg;
    483 	struct iovec	aiov;
    484 
    485 	msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
    486 	msg.msg_namelen = SCARG(uap, tolen);
    487 	msg.msg_iov = &aiov;
    488 	msg.msg_iovlen = 1;
    489 	msg.msg_control = NULL;
    490 	msg.msg_flags = 0;
    491 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
    492 	aiov.iov_len = SCARG(uap, len);
    493 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    494 }
    495 
    496 int
    497 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap, register_t *retval)
    498 {
    499 	/* {
    500 		syscallarg(int)				s;
    501 		syscallarg(const struct msghdr *)	msg;
    502 		syscallarg(int)				flags;
    503 	} */
    504 	struct msghdr	msg;
    505 	int		error;
    506 
    507 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    508 	if (error)
    509 		return (error);
    510 
    511 	msg.msg_flags = MSG_IOVUSRSPACE;
    512 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    513 }
    514 
    515 int
    516 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
    517 		register_t *retsize)
    518 {
    519 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    520 	struct mbuf	*to, *control;
    521 	struct socket	*so;
    522 	struct uio	auio;
    523 	size_t		len, iovsz;
    524 	int		i, error;
    525 
    526 	ktrkuser("msghdr", mp, sizeof *mp);
    527 
    528 	/* If the caller passed us stuff in mbufs, we must free them. */
    529 	to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL;
    530 	control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL;
    531 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    532 
    533 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    534 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    535 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    536 				error = EMSGSIZE;
    537 				goto bad;
    538 			}
    539 			iov = kmem_alloc(iovsz, KM_SLEEP);
    540 		}
    541 		if (mp->msg_iovlen != 0) {
    542 			error = copyin(mp->msg_iov, iov, iovsz);
    543 			if (error)
    544 				goto bad;
    545 		}
    546 		mp->msg_iov = iov;
    547 	}
    548 
    549 	auio.uio_iov = mp->msg_iov;
    550 	auio.uio_iovcnt = mp->msg_iovlen;
    551 	auio.uio_rw = UIO_WRITE;
    552 	auio.uio_offset = 0;			/* XXX */
    553 	auio.uio_resid = 0;
    554 	KASSERT(l == curlwp);
    555 	auio.uio_vmspace = l->l_proc->p_vmspace;
    556 
    557 	for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) {
    558 		/*
    559 		 * Writes return ssize_t because -1 is returned on error.
    560 		 * Therefore, we must restrict the length to SSIZE_MAX to
    561 		 * avoid garbage return values.
    562 		 */
    563 		auio.uio_resid += tiov->iov_len;
    564 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    565 			error = EINVAL;
    566 			goto bad;
    567 		}
    568 	}
    569 
    570 	if (mp->msg_name && to == NULL) {
    571 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    572 		    MT_SONAME);
    573 		if (error)
    574 			goto bad;
    575 	}
    576 
    577 	if (mp->msg_control) {
    578 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
    579 			error = EINVAL;
    580 			goto bad;
    581 		}
    582 		if (control == NULL) {
    583 			error = sockargs(&control, mp->msg_control,
    584 			    mp->msg_controllen, MT_CONTROL);
    585 			if (error)
    586 				goto bad;
    587 		}
    588 	}
    589 
    590 	if (ktrpoint(KTR_GENIO)) {
    591 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    592 		memcpy(ktriov, auio.uio_iov, iovsz);
    593 	}
    594 
    595 	if ((error = fd_getsock(s, &so)) != 0)
    596 		goto bad;
    597 
    598 	if (mp->msg_name)
    599 		MCLAIM(to, so->so_mowner);
    600 	if (mp->msg_control)
    601 		MCLAIM(control, so->so_mowner);
    602 
    603 	len = auio.uio_resid;
    604 	error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
    605 	/* Protocol is responsible for freeing 'control' */
    606 	control = NULL;
    607 
    608 	fd_putfile(s);
    609 
    610 	if (error) {
    611 		if (auio.uio_resid != len && (error == ERESTART ||
    612 		    error == EINTR || error == EWOULDBLOCK))
    613 			error = 0;
    614 		if (error == EPIPE && (flags & MSG_NOSIGNAL) == 0) {
    615 			mutex_enter(proc_lock);
    616 			psignal(l->l_proc, SIGPIPE);
    617 			mutex_exit(proc_lock);
    618 		}
    619 	}
    620 	if (error == 0)
    621 		*retsize = len - auio.uio_resid;
    622 
    623 bad:
    624 	if (ktriov != NULL) {
    625 		ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
    626 		kmem_free(ktriov, iovsz);
    627 	}
    628 
    629  	if (iov != aiov)
    630  		kmem_free(iov, iovsz);
    631 	if (to)
    632 		m_freem(to);
    633 	if (control)
    634 		m_freem(control);
    635 
    636 	return (error);
    637 }
    638 
    639 int
    640 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval)
    641 {
    642 	/* {
    643 		syscallarg(int)			s;
    644 		syscallarg(void *)		buf;
    645 		syscallarg(size_t)		len;
    646 		syscallarg(int)			flags;
    647 		syscallarg(struct sockaddr *)	from;
    648 		syscallarg(unsigned int *)	fromlenaddr;
    649 	} */
    650 	struct msghdr	msg;
    651 	struct iovec	aiov;
    652 	int		error;
    653 	struct mbuf	*from;
    654 
    655 	msg.msg_name = NULL;
    656 	msg.msg_iov = &aiov;
    657 	msg.msg_iovlen = 1;
    658 	aiov.iov_base = SCARG(uap, buf);
    659 	aiov.iov_len = SCARG(uap, len);
    660 	msg.msg_control = NULL;
    661 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
    662 
    663 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
    664 	if (error != 0)
    665 		return error;
    666 
    667 	error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
    668 	    MSG_LENUSRSPACE, from);
    669 	if (from != NULL)
    670 		m_free(from);
    671 	return error;
    672 }
    673 
    674 int
    675 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval)
    676 {
    677 	/* {
    678 		syscallarg(int)			s;
    679 		syscallarg(struct msghdr *)	msg;
    680 		syscallarg(int)			flags;
    681 	} */
    682 	struct msghdr	msg;
    683 	int		error;
    684 	struct mbuf	*from, *control;
    685 
    686 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    687 	if (error)
    688 		return (error);
    689 
    690 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    691 
    692 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
    693 	    msg.msg_control != NULL ? &control : NULL, retval);
    694 	if (error != 0)
    695 		return error;
    696 
    697 	if (msg.msg_control != NULL)
    698 		error = copyout_msg_control(l, &msg, control);
    699 
    700 	if (error == 0)
    701 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
    702 			from);
    703 	if (from != NULL)
    704 		m_free(from);
    705 	if (error == 0) {
    706 		ktrkuser("msghdr", &msg, sizeof msg);
    707 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
    708 	}
    709 
    710 	return (error);
    711 }
    712 
    713 /*
    714  * Adjust for a truncated SCM_RIGHTS control message.
    715  *  This means closing any file descriptors that aren't present
    716  *  in the returned buffer.
    717  *  m is the mbuf holding the (already externalized) SCM_RIGHTS message.
    718  */
    719 static void
    720 free_rights(struct mbuf *m)
    721 {
    722 	int nfd;
    723 	int i;
    724 	int *fdv;
    725 
    726 	nfd = m->m_len < CMSG_SPACE(sizeof(int)) ? 0
    727 	    : (m->m_len - CMSG_SPACE(sizeof(int))) / sizeof(int) + 1;
    728 	fdv = (int *) CMSG_DATA(mtod(m,struct cmsghdr *));
    729 	for (i = 0; i < nfd; i++) {
    730 		if (fd_getfile(fdv[i]) != NULL)
    731 			(void)fd_close(fdv[i]);
    732 	}
    733 }
    734 
    735 void
    736 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
    737 {
    738 	struct mbuf *next;
    739 	struct cmsghdr *cmsg;
    740 	bool do_free_rights = false;
    741 
    742 	while (control != NULL) {
    743 		cmsg = mtod(control, struct cmsghdr *);
    744 		if (control == uncopied)
    745 			do_free_rights = true;
    746 		if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
    747 		    && cmsg->cmsg_type == SCM_RIGHTS)
    748 			free_rights(control);
    749 		next = control->m_next;
    750 		m_free(control);
    751 		control = next;
    752 	}
    753 }
    754 
    755 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
    756 int
    757 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
    758 {
    759 	int i, len, error = 0;
    760 	struct cmsghdr *cmsg;
    761 	struct mbuf *m;
    762 	char *q;
    763 
    764 	len = mp->msg_controllen;
    765 	if (len <= 0 || control == 0) {
    766 		mp->msg_controllen = 0;
    767 		free_control_mbuf(l, control, control);
    768 		return 0;
    769 	}
    770 
    771 	q = (char *)mp->msg_control;
    772 
    773 	for (m = control; m != NULL; ) {
    774 		cmsg = mtod(m, struct cmsghdr *);
    775 		i = m->m_len;
    776 		if (len < i) {
    777 			mp->msg_flags |= MSG_CTRUNC;
    778 			if (cmsg->cmsg_level == SOL_SOCKET
    779 			    && cmsg->cmsg_type == SCM_RIGHTS)
    780 				/* Do not truncate me ... */
    781 				break;
    782 			i = len;
    783 		}
    784 		error = copyout(mtod(m, void *), q, i);
    785 		ktrkuser("msgcontrol", mtod(m, void *), i);
    786 		if (error != 0) {
    787 			/* We must free all the SCM_RIGHTS */
    788 			m = control;
    789 			break;
    790 		}
    791 		m = m->m_next;
    792 		if (m)
    793 			i = ALIGN(i);
    794 		q += i;
    795 		len -= i;
    796 		if (len <= 0)
    797 			break;
    798 	}
    799 
    800 	free_control_mbuf(l, control, m);
    801 
    802 	mp->msg_controllen = q - (char *)mp->msg_control;
    803 	return error;
    804 }
    805 
    806 int
    807 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
    808     struct mbuf **control, register_t *retsize)
    809 {
    810 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov;
    811 	struct socket	*so;
    812 	struct uio	auio;
    813 	size_t		len, iovsz;
    814 	int		i, error;
    815 
    816 	ktrkuser("msghdr", mp, sizeof *mp);
    817 
    818 	*from = NULL;
    819 	if (control != NULL)
    820 		*control = NULL;
    821 
    822 	if ((error = fd_getsock(s, &so)) != 0)
    823 		return (error);
    824 
    825 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    826 
    827 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    828 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    829 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    830 				error = EMSGSIZE;
    831 				goto out;
    832 			}
    833 			iov = kmem_alloc(iovsz, KM_SLEEP);
    834 		}
    835 		if (mp->msg_iovlen != 0) {
    836 			error = copyin(mp->msg_iov, iov, iovsz);
    837 			if (error)
    838 				goto out;
    839 		}
    840 		auio.uio_iov = iov;
    841 	} else
    842 		auio.uio_iov = mp->msg_iov;
    843 	auio.uio_iovcnt = mp->msg_iovlen;
    844 	auio.uio_rw = UIO_READ;
    845 	auio.uio_offset = 0;			/* XXX */
    846 	auio.uio_resid = 0;
    847 	KASSERT(l == curlwp);
    848 	auio.uio_vmspace = l->l_proc->p_vmspace;
    849 
    850 	tiov = auio.uio_iov;
    851 	for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
    852 		/*
    853 		 * Reads return ssize_t because -1 is returned on error.
    854 		 * Therefore we must restrict the length to SSIZE_MAX to
    855 		 * avoid garbage return values.
    856 		 */
    857 		auio.uio_resid += tiov->iov_len;
    858 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    859 			error = EINVAL;
    860 			goto out;
    861 		}
    862 	}
    863 
    864 	ktriov = NULL;
    865 	if (ktrpoint(KTR_GENIO)) {
    866 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    867 		memcpy(ktriov, auio.uio_iov, iovsz);
    868 	}
    869 
    870 	len = auio.uio_resid;
    871 	mp->msg_flags &= MSG_USERFLAGS;
    872 	error = (*so->so_receive)(so, from, &auio, NULL, control,
    873 	    &mp->msg_flags);
    874 	len -= auio.uio_resid;
    875 	*retsize = len;
    876 	if (error != 0 && len != 0
    877 	    && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
    878 		/* Some data transferred */
    879 		error = 0;
    880 
    881 	if (ktriov != NULL) {
    882 		ktrgeniov(s, UIO_READ, ktriov, len, error);
    883 		kmem_free(ktriov, iovsz);
    884 	}
    885 
    886 	if (error != 0) {
    887 		m_freem(*from);
    888 		*from = NULL;
    889 		if (control != NULL) {
    890 			free_control_mbuf(l, *control, *control);
    891 			*control = NULL;
    892 		}
    893 	}
    894  out:
    895 	if (iov != aiov)
    896 		kmem_free(iov, iovsz);
    897 	fd_putfile(s);
    898 	return (error);
    899 }
    900 
    901 
    902 /* ARGSUSED */
    903 int
    904 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, register_t *retval)
    905 {
    906 	/* {
    907 		syscallarg(int)	s;
    908 		syscallarg(int)	how;
    909 	} */
    910 	struct socket	*so;
    911 	int		error;
    912 
    913 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    914 		return (error);
    915 	solock(so);
    916 	error = soshutdown(so, SCARG(uap, how));
    917 	sounlock(so);
    918 	fd_putfile(SCARG(uap, s));
    919 	return (error);
    920 }
    921 
    922 /* ARGSUSED */
    923 int
    924 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, register_t *retval)
    925 {
    926 	/* {
    927 		syscallarg(int)			s;
    928 		syscallarg(int)			level;
    929 		syscallarg(int)			name;
    930 		syscallarg(const void *)	val;
    931 		syscallarg(unsigned int)	valsize;
    932 	} */
    933 	struct sockopt	sopt;
    934 	struct socket	*so;
    935 	int		error;
    936 	unsigned int	len;
    937 
    938 	len = SCARG(uap, valsize);
    939 	if (len > 0 && SCARG(uap, val) == NULL)
    940 		return (EINVAL);
    941 
    942 	if (len > MCLBYTES)
    943 		return (EINVAL);
    944 
    945 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    946 		return (error);
    947 
    948 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len);
    949 
    950 	if (len > 0) {
    951 		error = copyin(SCARG(uap, val), sopt.sopt_data, len);
    952 		if (error)
    953 			goto out;
    954 	}
    955 
    956 	error = sosetopt(so, &sopt);
    957 
    958  out:
    959 	sockopt_destroy(&sopt);
    960  	fd_putfile(SCARG(uap, s));
    961 	return (error);
    962 }
    963 
    964 /* ARGSUSED */
    965 int
    966 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, register_t *retval)
    967 {
    968 	/* {
    969 		syscallarg(int)			s;
    970 		syscallarg(int)			level;
    971 		syscallarg(int)			name;
    972 		syscallarg(void *)		val;
    973 		syscallarg(unsigned int *)	avalsize;
    974 	} */
    975 	struct sockopt	sopt;
    976 	struct socket	*so;
    977 	unsigned int	valsize, len;
    978 	int		error;
    979 
    980 	if (SCARG(uap, val) != NULL) {
    981 		error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize));
    982 		if (error)
    983 			return (error);
    984 	} else
    985 		valsize = 0;
    986 
    987 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    988 		return (error);
    989 
    990 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0);
    991 
    992 	error = sogetopt(so, &sopt);
    993 	if (error)
    994 		goto out;
    995 
    996 	if (valsize > 0) {
    997 		len = min(valsize, sopt.sopt_size);
    998 		error = copyout(sopt.sopt_data, SCARG(uap, val), len);
    999 		if (error)
   1000 			goto out;
   1001 
   1002 		error = copyout(&len, SCARG(uap, avalsize), sizeof(len));
   1003 		if (error)
   1004 			goto out;
   1005 	}
   1006 
   1007  out:
   1008 	sockopt_destroy(&sopt);
   1009  	fd_putfile(SCARG(uap, s));
   1010 	return (error);
   1011 }
   1012 
   1013 #ifdef PIPE_SOCKETPAIR
   1014 /* ARGSUSED */
   1015 int
   1016 pipe1(struct lwp *l, register_t *retval, int flags)
   1017 {
   1018 	file_t		*rf, *wf;
   1019 	struct socket	*rso, *wso;
   1020 	int		fd, error;
   1021 	proc_t		*p;
   1022 
   1023 	if (flags & ~(O_CLOEXEC|O_NONBLOCK))
   1024 		return EINVAL;
   1025 	p = curproc;
   1026 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
   1027 		return (error);
   1028 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
   1029 		goto free1;
   1030 	/* remember this socket pair implements a pipe */
   1031 	wso->so_state |= SS_ISAPIPE;
   1032 	rso->so_state |= SS_ISAPIPE;
   1033 	if ((error = fd_allocfile(&rf, &fd)) != 0)
   1034 		goto free2;
   1035 	retval[0] = fd;
   1036 	rf->f_flag = FREAD | flags;
   1037 	rf->f_type = DTYPE_SOCKET;
   1038 	rf->f_ops = &socketops;
   1039 	rf->f_data = rso;
   1040 	if ((error = fd_allocfile(&wf, &fd)) != 0)
   1041 		goto free3;
   1042 	wf->f_flag = FWRITE | flags;
   1043 	wf->f_type = DTYPE_SOCKET;
   1044 	wf->f_ops = &socketops;
   1045 	wf->f_data = wso;
   1046 	retval[1] = fd;
   1047 	solock(wso);
   1048 	error = unp_connect2(wso, rso, PRU_CONNECT2);
   1049 	sounlock(wso);
   1050 	if (error != 0)
   1051 		goto free4;
   1052 	fd_affix(p, wf, (int)retval[1]);
   1053 	fd_affix(p, rf, (int)retval[0]);
   1054 	return (0);
   1055  free4:
   1056 	fd_abort(p, wf, (int)retval[1]);
   1057  free3:
   1058 	fd_abort(p, rf, (int)retval[0]);
   1059  free2:
   1060 	(void)soclose(wso);
   1061  free1:
   1062 	(void)soclose(rso);
   1063 	return (error);
   1064 }
   1065 #endif /* PIPE_SOCKETPAIR */
   1066 
   1067 /*
   1068  * Get socket name.
   1069  */
   1070 /* ARGSUSED */
   1071 int
   1072 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
   1073 {
   1074 	struct socket	*so;
   1075 	struct mbuf	*m;
   1076 	int		error;
   1077 
   1078 	if ((error = fd_getsock(fd, &so)) != 0)
   1079 		return error;
   1080 
   1081 	m = m_getclr(M_WAIT, MT_SONAME);
   1082 	MCLAIM(m, so->so_mowner);
   1083 
   1084 	solock(so);
   1085 	if (which == PRU_PEERADDR
   1086 	    && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) {
   1087 		error = ENOTCONN;
   1088 	} else {
   1089 		*nam = m;
   1090 		error = (*so->so_proto->pr_usrreq)(so, which, NULL, m, NULL,
   1091 		    NULL);
   1092 	}
   1093  	sounlock(so);
   1094 	if (error != 0)
   1095 		m_free(m);
   1096  	fd_putfile(fd);
   1097 	return error;
   1098 }
   1099 
   1100 int
   1101 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
   1102     struct mbuf *addr)
   1103 {
   1104 	int len;
   1105 	int error;
   1106 
   1107 	if (asa == NULL)
   1108 		/* Assume application not interested */
   1109 		return 0;
   1110 
   1111 	if (flags & MSG_LENUSRSPACE) {
   1112 		error = copyin(alen, &len, sizeof(len));
   1113 		if (error)
   1114 			return error;
   1115 	} else
   1116 		len = *alen;
   1117 	if (len < 0)
   1118 		return EINVAL;
   1119 
   1120 	if (addr == NULL) {
   1121 		len = 0;
   1122 		error = 0;
   1123 	} else {
   1124 		if (len > addr->m_len)
   1125 			len = addr->m_len;
   1126 		/* Maybe this ought to copy a chain ? */
   1127 		ktrkuser("sockname", mtod(addr, void *), len);
   1128 		error = copyout(mtod(addr, void *), asa, len);
   1129 	}
   1130 
   1131 	if (error == 0) {
   1132 		if (flags & MSG_LENUSRSPACE)
   1133 			error = copyout(&len, alen, sizeof(len));
   1134 		else
   1135 			*alen = len;
   1136 	}
   1137 
   1138 	return error;
   1139 }
   1140 
   1141 /*
   1142  * Get socket name.
   1143  */
   1144 /* ARGSUSED */
   1145 int
   1146 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval)
   1147 {
   1148 	/* {
   1149 		syscallarg(int)			fdes;
   1150 		syscallarg(struct sockaddr *)	asa;
   1151 		syscallarg(unsigned int *)	alen;
   1152 	} */
   1153 	struct mbuf	*m;
   1154 	int		error;
   1155 
   1156 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
   1157 	if (error != 0)
   1158 		return error;
   1159 
   1160 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1161 	    MSG_LENUSRSPACE, m);
   1162 	if (m != NULL)
   1163 		m_free(m);
   1164 	return error;
   1165 }
   1166 
   1167 /*
   1168  * Get name of peer for connected socket.
   1169  */
   1170 /* ARGSUSED */
   1171 int
   1172 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval)
   1173 {
   1174 	/* {
   1175 		syscallarg(int)			fdes;
   1176 		syscallarg(struct sockaddr *)	asa;
   1177 		syscallarg(unsigned int *)	alen;
   1178 	} */
   1179 	struct mbuf	*m;
   1180 	int		error;
   1181 
   1182 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
   1183 	if (error != 0)
   1184 		return error;
   1185 
   1186 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1187 	    MSG_LENUSRSPACE, m);
   1188 	if (m != NULL)
   1189 		m_free(m);
   1190 	return error;
   1191 }
   1192 
   1193 /*
   1194  * XXX In a perfect world, we wouldn't pass around socket control
   1195  * XXX arguments in mbufs, and this could go away.
   1196  */
   1197 int
   1198 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
   1199 {
   1200 	struct sockaddr	*sa;
   1201 	struct mbuf	*m;
   1202 	int		error;
   1203 
   1204 	/*
   1205 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1206 	 * will overflow sa_len.  Control data more than a page size in
   1207 	 * length is just too much.
   1208 	 */
   1209 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1210 		return (EINVAL);
   1211 
   1212 	/* Allocate an mbuf to hold the arguments. */
   1213 	m = m_get(M_WAIT, type);
   1214 	/* can't claim.  don't who to assign it to. */
   1215 	if (buflen > MLEN) {
   1216 		/*
   1217 		 * Won't fit into a regular mbuf, so we allocate just
   1218 		 * enough external storage to hold the argument.
   1219 		 */
   1220 		MEXTMALLOC(m, buflen, M_WAITOK);
   1221 	}
   1222 	m->m_len = buflen;
   1223 	error = copyin(bf, mtod(m, void *), buflen);
   1224 	if (error) {
   1225 		(void) m_free(m);
   1226 		return (error);
   1227 	}
   1228 	ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
   1229 	*mp = m;
   1230 	if (type == MT_SONAME) {
   1231 		sa = mtod(m, struct sockaddr *);
   1232 #if BYTE_ORDER != BIG_ENDIAN
   1233 		/*
   1234 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1235 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1236 		 */
   1237 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1238 			sa->sa_family = sa->sa_len;
   1239 #endif
   1240 		sa->sa_len = buflen;
   1241 	}
   1242 	return (0);
   1243 }
   1244