Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.110
      1 /*	$NetBSD: uipc_syscalls.c,v 1.110 2007/05/13 20:24:21 dsl Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1982, 1986, 1989, 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. Neither the name of the University nor the names of its contributors
     16  *    may be used to endorse or promote products derived from this software
     17  *    without specific prior written permission.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29  * SUCH DAMAGE.
     30  *
     31  *	@(#)uipc_syscalls.c	8.6 (Berkeley) 2/14/95
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.110 2007/05/13 20:24:21 dsl Exp $");
     36 
     37 #include "opt_ktrace.h"
     38 #include "opt_pipe.h"
     39 
     40 #include <sys/param.h>
     41 #include <sys/systm.h>
     42 #include <sys/filedesc.h>
     43 #include <sys/proc.h>
     44 #include <sys/file.h>
     45 #include <sys/buf.h>
     46 #include <sys/malloc.h>
     47 #include <sys/mbuf.h>
     48 #include <sys/protosw.h>
     49 #include <sys/socket.h>
     50 #include <sys/socketvar.h>
     51 #include <sys/signalvar.h>
     52 #include <sys/un.h>
     53 #ifdef KTRACE
     54 #include <sys/ktrace.h>
     55 #endif
     56 #include <sys/event.h>
     57 
     58 #include <sys/mount.h>
     59 #include <sys/syscallargs.h>
     60 
     61 #include <uvm/uvm_extern.h>
     62 
     63 static void adjust_rights(struct mbuf *m, int len, struct lwp *l);
     64 
     65 /*
     66  * System call interface to the socket abstraction.
     67  */
     68 extern const struct fileops socketops;
     69 
     70 int
     71 sys___socket30(struct lwp *l, void *v, register_t *retval)
     72 {
     73 	struct sys___socket30_args /* {
     74 		syscallarg(int)	domain;
     75 		syscallarg(int)	type;
     76 		syscallarg(int)	protocol;
     77 	} */ *uap = v;
     78 
     79 	struct filedesc	*fdp;
     80 	struct socket	*so;
     81 	struct file	*fp;
     82 	int		fd, error;
     83 
     84 	fdp = l->l_proc->p_fd;
     85 	/* falloc() will use the desciptor for us */
     86 	if ((error = falloc(l, &fp, &fd)) != 0)
     87 		return (error);
     88 	fp->f_flag = FREAD|FWRITE;
     89 	fp->f_type = DTYPE_SOCKET;
     90 	fp->f_ops = &socketops;
     91 	error = socreate(SCARG(uap, domain), &so, SCARG(uap, type),
     92 			 SCARG(uap, protocol), l);
     93 	if (error) {
     94 		FILE_UNUSE(fp, l);
     95 		fdremove(fdp, fd);
     96 		ffree(fp);
     97 	} else {
     98 		fp->f_data = so;
     99 		FILE_SET_MATURE(fp);
    100 		FILE_UNUSE(fp, l);
    101 		*retval = fd;
    102 	}
    103 	return (error);
    104 }
    105 
    106 /* ARGSUSED */
    107 int
    108 sys_bind(struct lwp *l, void *v, register_t *retval)
    109 {
    110 	struct sys_bind_args /* {
    111 		syscallarg(int)				s;
    112 		syscallarg(const struct sockaddr *)	name;
    113 		syscallarg(unsigned int)		namelen;
    114 	} */ *uap = v;
    115 	struct proc	*p;
    116 	struct file	*fp;
    117 	struct mbuf	*nam;
    118 	int		error;
    119 
    120 	p = l->l_proc;
    121 	/* getsock() will use the descriptor for us */
    122 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    123 		return (error);
    124 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    125 	    MT_SONAME);
    126 	if (error) {
    127 		FILE_UNUSE(fp, l);
    128 		return (error);
    129 	}
    130 	MCLAIM(nam, ((struct socket *)fp->f_data)->so_mowner);
    131 	error = sobind((struct socket *)fp->f_data, nam, l);
    132 	m_freem(nam);
    133 	FILE_UNUSE(fp, l);
    134 	return (error);
    135 }
    136 
    137 /* ARGSUSED */
    138 int
    139 sys_listen(struct lwp *l, void *v, register_t *retval)
    140 {
    141 	struct sys_listen_args /* {
    142 		syscallarg(int)	s;
    143 		syscallarg(int)	backlog;
    144 	} */ *uap = v;
    145 	struct proc	*p;
    146 	struct file	*fp;
    147 	int		error;
    148 
    149 	p = l->l_proc;
    150 	/* getsock() will use the descriptor for us */
    151 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    152 		return (error);
    153 	error = solisten((struct socket *)fp->f_data, SCARG(uap, backlog));
    154 	FILE_UNUSE(fp, l);
    155 	return (error);
    156 }
    157 
    158 int
    159 sys_accept(struct lwp *l, void *v, register_t *retval)
    160 {
    161 	struct sys_accept_args /* {
    162 		syscallarg(int)			s;
    163 		syscallarg(struct sockaddr *)	name;
    164 		syscallarg(unsigned int *)	anamelen;
    165 	} */ *uap = v;
    166 	struct filedesc	*fdp;
    167 	struct file	*fp;
    168 	struct mbuf	*nam;
    169 	unsigned int	namelen;
    170 	int		error, s, fd;
    171 	struct socket	*so;
    172 	int		fflag;
    173 
    174 	fdp = l->l_proc->p_fd;
    175 	if (SCARG(uap, name) && (error = copyin(SCARG(uap, anamelen),
    176 	    &namelen, sizeof(namelen))))
    177 		return (error);
    178 
    179 	/* getsock() will use the descriptor for us */
    180 	if ((error = getsock(fdp, SCARG(uap, s), &fp)) != 0)
    181 		return (error);
    182 	s = splsoftnet();
    183 	so = (struct socket *)fp->f_data;
    184 	FILE_UNUSE(fp, l);
    185 	if (!(so->so_proto->pr_flags & PR_LISTEN)) {
    186 		splx(s);
    187 		return (EOPNOTSUPP);
    188 	}
    189 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    190 		splx(s);
    191 		return (EINVAL);
    192 	}
    193 	if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
    194 		splx(s);
    195 		return (EWOULDBLOCK);
    196 	}
    197 	while (so->so_qlen == 0 && so->so_error == 0) {
    198 		if (so->so_state & SS_CANTRCVMORE) {
    199 			so->so_error = ECONNABORTED;
    200 			break;
    201 		}
    202 		error = tsleep(&so->so_timeo, PSOCK | PCATCH,
    203 		    netcon, 0);
    204 		if (error) {
    205 			splx(s);
    206 			return (error);
    207 		}
    208 	}
    209 	if (so->so_error) {
    210 		error = so->so_error;
    211 		so->so_error = 0;
    212 		splx(s);
    213 		return (error);
    214 	}
    215 	fflag = fp->f_flag;
    216 	/* falloc() will use the descriptor for us */
    217 	if ((error = falloc(l, &fp, &fd)) != 0) {
    218 		splx(s);
    219 		return (error);
    220 	}
    221 	*retval = fd;
    222 
    223 	/* connection has been removed from the listen queue */
    224 	KNOTE(&so->so_rcv.sb_sel.sel_klist, 0);
    225 
    226 	{ struct socket *aso = TAILQ_FIRST(&so->so_q);
    227 	  if (soqremque(aso, 1) == 0)
    228 		panic("accept");
    229 	  so = aso;
    230 	}
    231 	fp->f_type = DTYPE_SOCKET;
    232 	fp->f_flag = fflag;
    233 	fp->f_ops = &socketops;
    234 	fp->f_data = so;
    235 	nam = m_get(M_WAIT, MT_SONAME);
    236 	if ((error = soaccept(so, nam)) == 0 && SCARG(uap, name)) {
    237 		if (namelen > nam->m_len)
    238 			namelen = nam->m_len;
    239 		/* SHOULD COPY OUT A CHAIN HERE */
    240 		error = copyout(mtod(nam, void *), SCARG(uap, name), namelen);
    241 		if (error == 0) {
    242 			error = copyout(&namelen, SCARG(uap, anamelen),
    243 			    sizeof(*SCARG(uap, anamelen)));
    244 		}
    245 	}
    246 	/* if an error occurred, free the file descriptor */
    247 	if (error) {
    248 		fdremove(fdp, fd);
    249 		closef(fp, l);
    250 	} else {
    251 		FILE_SET_MATURE(fp);
    252 		FILE_UNUSE(fp, l);
    253 	}
    254 	m_freem(nam);
    255 	splx(s);
    256 	return (error);
    257 }
    258 
    259 /* ARGSUSED */
    260 int
    261 sys_connect(struct lwp *l, void *v, register_t *retval)
    262 {
    263 	struct sys_connect_args /* {
    264 		syscallarg(int)				s;
    265 		syscallarg(const struct sockaddr *)	name;
    266 		syscallarg(unsigned int)		namelen;
    267 	} */ *uap = v;
    268 	struct proc	*p;
    269 	struct file	*fp;
    270 	struct socket	*so;
    271 	struct mbuf	*nam;
    272 	int		error, s;
    273 	int		interrupted = 0;
    274 
    275 	p = l->l_proc;
    276 	/* getsock() will use the descriptor for us */
    277 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    278 		return (error);
    279 	so = (struct socket *)fp->f_data;
    280 	if (so->so_state & SS_ISCONNECTING) {
    281 		error = EALREADY;
    282 		goto out;
    283 	}
    284 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    285 	    MT_SONAME);
    286 	if (error)
    287 		goto out;
    288 	MCLAIM(nam, so->so_mowner);
    289 	error = soconnect(so, nam, l);
    290 	if (error)
    291 		goto bad;
    292 	if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
    293 		m_freem(nam);
    294 		error = EINPROGRESS;
    295 		goto out;
    296 	}
    297 	s = splsoftnet();
    298 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
    299 		error = tsleep(&so->so_timeo, PSOCK | PCATCH,
    300 			       netcon, 0);
    301 		if (error) {
    302 			if (error == EINTR || error == ERESTART)
    303 				interrupted = 1;
    304 			break;
    305 		}
    306 	}
    307 	if (error == 0) {
    308 		error = so->so_error;
    309 		so->so_error = 0;
    310 	}
    311 	splx(s);
    312  bad:
    313 	if (!interrupted)
    314 		so->so_state &= ~SS_ISCONNECTING;
    315 	m_freem(nam);
    316 	if (error == ERESTART)
    317 		error = EINTR;
    318  out:
    319 	FILE_UNUSE(fp, l);
    320 	return (error);
    321 }
    322 
    323 int
    324 sys_socketpair(struct lwp *l, void *v, register_t *retval)
    325 {
    326 	struct sys_socketpair_args /* {
    327 		syscallarg(int)		domain;
    328 		syscallarg(int)		type;
    329 		syscallarg(int)		protocol;
    330 		syscallarg(int *)	rsv;
    331 	} */ *uap = v;
    332 	struct filedesc	*fdp;
    333 	struct file	*fp1, *fp2;
    334 	struct socket	*so1, *so2;
    335 	int		fd, error, sv[2];
    336 
    337 	fdp = l->l_proc->p_fd;
    338 	error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
    339 	    SCARG(uap, protocol), l);
    340 	if (error)
    341 		return (error);
    342 	error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
    343 	    SCARG(uap, protocol), l);
    344 	if (error)
    345 		goto free1;
    346 	/* falloc() will use the descriptor for us */
    347 	if ((error = falloc(l, &fp1, &fd)) != 0)
    348 		goto free2;
    349 	sv[0] = fd;
    350 	fp1->f_flag = FREAD|FWRITE;
    351 	fp1->f_type = DTYPE_SOCKET;
    352 	fp1->f_ops = &socketops;
    353 	fp1->f_data = so1;
    354 	if ((error = falloc(l, &fp2, &fd)) != 0)
    355 		goto free3;
    356 	fp2->f_flag = FREAD|FWRITE;
    357 	fp2->f_type = DTYPE_SOCKET;
    358 	fp2->f_ops = &socketops;
    359 	fp2->f_data = so2;
    360 	sv[1] = fd;
    361 	if ((error = soconnect2(so1, so2)) != 0)
    362 		goto free4;
    363 	if (SCARG(uap, type) == SOCK_DGRAM) {
    364 		/*
    365 		 * Datagram socket connection is asymmetric.
    366 		 */
    367 		 if ((error = soconnect2(so2, so1)) != 0)
    368 			goto free4;
    369 	}
    370 	error = copyout(sv, SCARG(uap, rsv), 2 * sizeof(int));
    371 	FILE_SET_MATURE(fp1);
    372 	FILE_SET_MATURE(fp2);
    373 	FILE_UNUSE(fp1, l);
    374 	FILE_UNUSE(fp2, l);
    375 	return (error);
    376  free4:
    377 	FILE_UNUSE(fp2, l);
    378 	ffree(fp2);
    379 	fdremove(fdp, sv[1]);
    380  free3:
    381 	FILE_UNUSE(fp1, l);
    382 	ffree(fp1);
    383 	fdremove(fdp, sv[0]);
    384  free2:
    385 	(void)soclose(so2);
    386  free1:
    387 	(void)soclose(so1);
    388 	return (error);
    389 }
    390 
    391 int
    392 sys_sendto(struct lwp *l, void *v, register_t *retval)
    393 {
    394 	struct sys_sendto_args /* {
    395 		syscallarg(int)				s;
    396 		syscallarg(const void *)		buf;
    397 		syscallarg(size_t)			len;
    398 		syscallarg(int)				flags;
    399 		syscallarg(const struct sockaddr *)	to;
    400 		syscallarg(unsigned int)		tolen;
    401 	} */ *uap = v;
    402 	struct msghdr	msg;
    403 	struct iovec	aiov;
    404 
    405 	msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
    406 	msg.msg_namelen = SCARG(uap, tolen);
    407 	msg.msg_iov = &aiov;
    408 	msg.msg_iovlen = 1;
    409 	msg.msg_control = 0;
    410 	msg.msg_flags = 0;
    411 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
    412 	aiov.iov_len = SCARG(uap, len);
    413 	return (sendit(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
    414 }
    415 
    416 int
    417 sys_sendmsg(struct lwp *l, void *v, register_t *retval)
    418 {
    419 	struct sys_sendmsg_args /* {
    420 		syscallarg(int)				s;
    421 		syscallarg(const struct msghdr *)	msg;
    422 		syscallarg(int)				flags;
    423 	} */ *uap = v;
    424 	struct msghdr	msg;
    425 	struct iovec	aiov[UIO_SMALLIOV], *iov;
    426 	int		error;
    427 
    428 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    429 	if (error)
    430 		return (error);
    431 	if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
    432 		if ((unsigned int)msg.msg_iovlen > IOV_MAX)
    433 			return (EMSGSIZE);
    434 		iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
    435 		    M_IOV, M_WAITOK);
    436 	} else
    437 		iov = aiov;
    438 	if ((unsigned int)msg.msg_iovlen > 0) {
    439 		error = copyin(msg.msg_iov, iov,
    440 		    (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
    441 		if (error)
    442 			goto done;
    443 	}
    444 	msg.msg_iov = iov;
    445 	msg.msg_flags = 0;
    446 	error = sendit(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    447 done:
    448 	if (iov != aiov)
    449 		free(iov, M_IOV);
    450 	return (error);
    451 }
    452 
    453 int
    454 sendit(struct lwp *l, int s, struct msghdr *mp, int flags, register_t *retsize)
    455 {
    456 	struct proc	*p;
    457 	struct file	*fp;
    458 	struct uio	auio;
    459 	struct iovec	*iov;
    460 	int		i, len, error;
    461 	struct mbuf	*to, *control;
    462 	struct socket	*so;
    463 #ifdef KTRACE
    464 	struct iovec	*ktriov;
    465 #endif
    466 
    467 #ifdef KTRACE
    468 	ktriov = NULL;
    469 #endif
    470 	p = l->l_proc;
    471 	/* getsock() will use the descriptor for us */
    472 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    473 		return (error);
    474 	so = (struct socket *)fp->f_data;
    475 	auio.uio_iov = mp->msg_iov;
    476 	auio.uio_iovcnt = mp->msg_iovlen;
    477 	auio.uio_rw = UIO_WRITE;
    478 	auio.uio_offset = 0;			/* XXX */
    479 	auio.uio_resid = 0;
    480 	KASSERT(l == curlwp);
    481 	auio.uio_vmspace = l->l_proc->p_vmspace;
    482 	iov = mp->msg_iov;
    483 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    484 #if 0
    485 		/* cannot happen; iov_len is unsigned */
    486 		if (iov->iov_len < 0) {
    487 			error = EINVAL;
    488 			goto out;
    489 		}
    490 #endif
    491 		/*
    492 		 * Writes return ssize_t because -1 is returned on error.
    493 		 * Therefore, we must restrict the length to SSIZE_MAX to
    494 		 * avoid garbage return values.
    495 		 */
    496 		auio.uio_resid += iov->iov_len;
    497 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    498 			error = EINVAL;
    499 			goto out;
    500 		}
    501 	}
    502 	if (mp->msg_name) {
    503 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    504 				 MT_SONAME);
    505 		if (error)
    506 			goto out;
    507 		MCLAIM(to, so->so_mowner);
    508 	} else
    509 		to = 0;
    510 	if (mp->msg_control) {
    511 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
    512 			error = EINVAL;
    513 			goto bad;
    514 		}
    515 		error = sockargs(&control, mp->msg_control,
    516 				 mp->msg_controllen, MT_CONTROL);
    517 		if (error)
    518 			goto bad;
    519 		MCLAIM(control, so->so_mowner);
    520 	} else
    521 		control = 0;
    522 #ifdef KTRACE
    523 	if (KTRPOINT(p, KTR_GENIO)) {
    524 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    525 
    526 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    527 		memcpy(ktriov, auio.uio_iov, iovlen);
    528 	}
    529 #endif
    530 	len = auio.uio_resid;
    531 	error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
    532 	if (error) {
    533 		if (auio.uio_resid != len && (error == ERESTART ||
    534 		    error == EINTR || error == EWOULDBLOCK))
    535 			error = 0;
    536 		if (error == EPIPE && (flags & MSG_NOSIGNAL) == 0) {
    537 			mutex_enter(&proclist_mutex);
    538 			psignal(p, SIGPIPE);
    539 			mutex_exit(&proclist_mutex);
    540 		}
    541 	}
    542 	if (error == 0)
    543 		*retsize = len - auio.uio_resid;
    544 #ifdef KTRACE
    545 	if (ktriov != NULL) {
    546 		if (error == 0)
    547 			ktrgenio(l, s, UIO_WRITE, ktriov, *retsize, error);
    548 		free(ktriov, M_TEMP);
    549 	}
    550 #endif
    551  bad:
    552 	if (to)
    553 		m_freem(to);
    554  out:
    555 	FILE_UNUSE(fp, l);
    556 	return (error);
    557 }
    558 
    559 int
    560 sys_recvfrom(struct lwp *l, void *v, register_t *retval)
    561 {
    562 	struct sys_recvfrom_args /* {
    563 		syscallarg(int)			s;
    564 		syscallarg(void *)		buf;
    565 		syscallarg(size_t)		len;
    566 		syscallarg(int)			flags;
    567 		syscallarg(struct sockaddr *)	from;
    568 		syscallarg(unsigned int *)	fromlenaddr;
    569 	} */ *uap = v;
    570 	struct msghdr	msg;
    571 	struct iovec	aiov;
    572 	int		error;
    573 
    574 	if (SCARG(uap, fromlenaddr)) {
    575 		error = copyin(SCARG(uap, fromlenaddr), &msg.msg_namelen,
    576 			       sizeof(msg.msg_namelen));
    577 		if (error)
    578 			return (error);
    579 	} else
    580 		msg.msg_namelen = 0;
    581 	msg.msg_name = SCARG(uap, from);
    582 	msg.msg_iov = &aiov;
    583 	msg.msg_iovlen = 1;
    584 	aiov.iov_base = SCARG(uap, buf);
    585 	aiov.iov_len = SCARG(uap, len);
    586 	msg.msg_control = 0;
    587 	msg.msg_flags = SCARG(uap, flags);
    588 	return recvit(l, SCARG(uap, s), &msg, SCARG(uap, fromlenaddr), retval);
    589 }
    590 
    591 int
    592 sys_recvmsg(struct lwp *l, void *v, register_t *retval)
    593 {
    594 	struct sys_recvmsg_args /* {
    595 		syscallarg(int)			s;
    596 		syscallarg(struct msghdr *)	msg;
    597 		syscallarg(int)			flags;
    598 	} */ *uap = v;
    599 	struct iovec	aiov[UIO_SMALLIOV], *uiov, *iov;
    600 	struct msghdr	msg;
    601 	int		error;
    602 
    603 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    604 	if (error)
    605 		return (error);
    606 	if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
    607 		if ((unsigned int)msg.msg_iovlen > IOV_MAX)
    608 			return (EMSGSIZE);
    609 		iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
    610 		    M_IOV, M_WAITOK);
    611 	} else
    612 		iov = aiov;
    613 	if ((unsigned int)msg.msg_iovlen > 0) {
    614 		error = copyin(msg.msg_iov, iov,
    615 		    (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
    616 		if (error)
    617 			goto done;
    618 	}
    619 	uiov = msg.msg_iov;
    620 	msg.msg_iov = iov;
    621 	msg.msg_flags = SCARG(uap, flags);
    622 	if ((error = recvit(l, SCARG(uap, s), &msg, NULL, retval)) == 0) {
    623 		msg.msg_iov = uiov;
    624 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
    625 	}
    626 done:
    627 	if (iov != aiov)
    628 		free(iov, M_IOV);
    629 	return (error);
    630 }
    631 
    632 /*
    633  * Adjust for a truncated SCM_RIGHTS control message.  This means
    634  *  closing any file descriptors that aren't entirely present in the
    635  *  returned buffer.  m is the mbuf holding the (already externalized)
    636  *  SCM_RIGHTS message; len is the length it is being truncated to.  p
    637  *  is the affected process.
    638  */
    639 static void
    640 adjust_rights(struct mbuf *m, int len, struct lwp *l)
    641 {
    642 	int nfd;
    643 	int i;
    644 	int nok;
    645 	int *fdv;
    646 
    647 	nfd = m->m_len < CMSG_SPACE(sizeof(int)) ? 0
    648 	    : (m->m_len - CMSG_SPACE(sizeof(int))) / sizeof(int) + 1;
    649 	nok = (len < CMSG_LEN(0)) ? 0 : ((len - CMSG_LEN(0)) / sizeof(int));
    650 	fdv = (int *) CMSG_DATA(mtod(m,struct cmsghdr *));
    651 	for (i = nok; i < nfd; i++)
    652 		fdrelease(l,fdv[i]);
    653 }
    654 
    655 int
    656 recvit(struct lwp *l, int s, struct msghdr *mp, void *namelenp,
    657 	register_t *retsize)
    658 {
    659 	struct proc	*p;
    660 	struct file	*fp;
    661 	struct uio	auio;
    662 	struct iovec	*iov;
    663 	int		i, len, error;
    664 	struct mbuf	*from, *control;
    665 	struct socket	*so;
    666 #ifdef KTRACE
    667 	struct iovec	*ktriov;
    668 #endif
    669 
    670 	p = l->l_proc;
    671 	from = 0;
    672 	control = 0;
    673 #ifdef KTRACE
    674 	ktriov = NULL;
    675 #endif
    676 
    677 	/* getsock() will use the descriptor for us */
    678 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    679 		return (error);
    680 	so = (struct socket *)fp->f_data;
    681 	auio.uio_iov = mp->msg_iov;
    682 	auio.uio_iovcnt = mp->msg_iovlen;
    683 	auio.uio_rw = UIO_READ;
    684 	auio.uio_offset = 0;			/* XXX */
    685 	auio.uio_resid = 0;
    686 	KASSERT(l == curlwp);
    687 	auio.uio_vmspace = l->l_proc->p_vmspace;
    688 	iov = mp->msg_iov;
    689 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    690 #if 0
    691 		/* cannot happen iov_len is unsigned */
    692 		if (iov->iov_len < 0) {
    693 			error = EINVAL;
    694 			goto out1;
    695 		}
    696 #endif
    697 		/*
    698 		 * Reads return ssize_t because -1 is returned on error.
    699 		 * Therefore we must restrict the length to SSIZE_MAX to
    700 		 * avoid garbage return values.
    701 		 */
    702 		auio.uio_resid += iov->iov_len;
    703 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    704 			error = EINVAL;
    705 			goto out1;
    706 		}
    707 	}
    708 #ifdef KTRACE
    709 	if (KTRPOINT(p, KTR_GENIO)) {
    710 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    711 
    712 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    713 		memcpy(ktriov, auio.uio_iov, iovlen);
    714 	}
    715 #endif
    716 	len = auio.uio_resid;
    717 	error = (*so->so_receive)(so, &from, &auio, NULL,
    718 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
    719 	if (error) {
    720 		if (auio.uio_resid != len && (error == ERESTART ||
    721 		    error == EINTR || error == EWOULDBLOCK))
    722 			error = 0;
    723 	}
    724 #ifdef KTRACE
    725 	if (ktriov != NULL) {
    726 		if (error == 0)
    727 			ktrgenio(l, s, UIO_READ, ktriov,
    728 			    len - auio.uio_resid, error);
    729 		free(ktriov, M_TEMP);
    730 	}
    731 #endif
    732 	if (error)
    733 		goto out;
    734 	*retsize = len - auio.uio_resid;
    735 	if (mp->msg_name) {
    736 		len = mp->msg_namelen;
    737 		if (len <= 0 || from == 0)
    738 			len = 0;
    739 		else {
    740 			if (len > from->m_len)
    741 				len = from->m_len;
    742 			/* else if len < from->m_len ??? */
    743 			error = copyout(mtod(from, void *),
    744 					mp->msg_name, (unsigned)len);
    745 			if (error)
    746 				goto out;
    747 		}
    748 		mp->msg_namelen = len;
    749 		if (namelenp &&
    750 		    (error = copyout(&len, namelenp, sizeof(int))))
    751 			goto out;
    752 	}
    753 	if (mp->msg_control) {
    754 		len = mp->msg_controllen;
    755 		if (len <= 0 || control == 0)
    756 			len = 0;
    757 		else {
    758 			struct mbuf *m = control;
    759 			char *q = (char *)mp->msg_control;
    760 
    761 			do {
    762 				i = m->m_len;
    763 				if (len < i) {
    764 					mp->msg_flags |= MSG_CTRUNC;
    765 					i = len;
    766 					if (mtod(m, struct cmsghdr *)->
    767 					    cmsg_type == SCM_RIGHTS)
    768 						adjust_rights(m, len, l);
    769 				}
    770 				error = copyout(mtod(m, void *), q,
    771 				    (unsigned)i);
    772 				m = m->m_next;
    773 				if (m)
    774 					i = ALIGN(i);
    775 				q += i;
    776 				len -= i;
    777 				if (error != 0 || len <= 0)
    778 					break;
    779 			} while (m != NULL);
    780 			while (m) {
    781 				if (mtod(m, struct cmsghdr *)->
    782 				    cmsg_type == SCM_RIGHTS)
    783 					adjust_rights(m, 0, l);
    784 				m = m->m_next;
    785 			}
    786 			len = q - (char *)mp->msg_control;
    787 		}
    788 		mp->msg_controllen = len;
    789 	}
    790  out:
    791 	if (from)
    792 		m_freem(from);
    793 	if (control)
    794 		m_freem(control);
    795  out1:
    796 	FILE_UNUSE(fp, l);
    797 	return (error);
    798 }
    799 
    800 /* ARGSUSED */
    801 int
    802 sys_shutdown(struct lwp *l, void *v, register_t *retval)
    803 {
    804 	struct sys_shutdown_args /* {
    805 		syscallarg(int)	s;
    806 		syscallarg(int)	how;
    807 	} */ *uap = v;
    808 	struct proc	*p;
    809 	struct file	*fp;
    810 	int		error;
    811 
    812 	p = l->l_proc;
    813 	/* getsock() will use the descriptor for us */
    814 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    815 		return (error);
    816 	error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
    817 	FILE_UNUSE(fp, l);
    818 	return (error);
    819 }
    820 
    821 /* ARGSUSED */
    822 int
    823 sys_setsockopt(struct lwp *l, void *v, register_t *retval)
    824 {
    825 	struct sys_setsockopt_args /* {
    826 		syscallarg(int)			s;
    827 		syscallarg(int)			level;
    828 		syscallarg(int)			name;
    829 		syscallarg(const void *)	val;
    830 		syscallarg(unsigned int)	valsize;
    831 	} */ *uap = v;
    832 	struct proc	*p;
    833 	struct file	*fp;
    834 	struct mbuf	*m;
    835 	struct socket	*so;
    836 	int		error;
    837 	unsigned int	len;
    838 
    839 	p = l->l_proc;
    840 	m = NULL;
    841 	/* getsock() will use the descriptor for us */
    842 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    843 		return (error);
    844 	so = (struct socket *)fp->f_data;
    845 	len = SCARG(uap, valsize);
    846 	if (len > MCLBYTES) {
    847 		error = EINVAL;
    848 		goto out;
    849 	}
    850 	if (SCARG(uap, val)) {
    851 		m = m_get(M_WAIT, MT_SOOPTS);
    852 		MCLAIM(m, so->so_mowner);
    853 		if (len > MLEN)
    854 			m_clget(m, M_WAIT);
    855 		error = copyin(SCARG(uap, val), mtod(m, void *), len);
    856 		if (error) {
    857 			(void) m_free(m);
    858 			goto out;
    859 		}
    860 		m->m_len = SCARG(uap, valsize);
    861 	}
    862 	error = sosetopt(so, SCARG(uap, level), SCARG(uap, name), m);
    863  out:
    864 	FILE_UNUSE(fp, l);
    865 	return (error);
    866 }
    867 
    868 /* ARGSUSED */
    869 int
    870 sys_getsockopt(struct lwp *l, void *v, register_t *retval)
    871 {
    872 	struct sys_getsockopt_args /* {
    873 		syscallarg(int)			s;
    874 		syscallarg(int)			level;
    875 		syscallarg(int)			name;
    876 		syscallarg(void *)		val;
    877 		syscallarg(unsigned int *)	avalsize;
    878 	} */ *uap = v;
    879 	struct file	*fp;
    880 	struct mbuf	*m;
    881 	unsigned int	op, i, valsize;
    882 	int		error;
    883 
    884 	m = NULL;
    885 	/* getsock() will use the descriptor for us */
    886 	if ((error = getsock(l->l_proc->p_fd, SCARG(uap, s), &fp)) != 0)
    887 		return (error);
    888 	if (SCARG(uap, val)) {
    889 		error = copyin(SCARG(uap, avalsize),
    890 			       &valsize, sizeof(valsize));
    891 		if (error)
    892 			goto out;
    893 	} else
    894 		valsize = 0;
    895 	if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
    896 	    SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
    897 	    m != NULL) {
    898 		op = 0;
    899 		while (m && !error && op < valsize) {
    900 			i = min(m->m_len, (valsize - op));
    901 			error = copyout(mtod(m, void *), SCARG(uap, val), i);
    902 			op += i;
    903 			SCARG(uap, val) = ((uint8_t *)SCARG(uap, val)) + i;
    904 			m = m_free(m);
    905 		}
    906 		valsize = op;
    907 		if (error == 0)
    908 			error = copyout(&valsize,
    909 					SCARG(uap, avalsize), sizeof(valsize));
    910 	}
    911 	if (m != NULL)
    912 		(void) m_freem(m);
    913  out:
    914 	FILE_UNUSE(fp, l);
    915 	return (error);
    916 }
    917 
    918 #ifdef PIPE_SOCKETPAIR
    919 /* ARGSUSED */
    920 int
    921 sys_pipe(struct lwp *l, void *v, register_t *retval)
    922 {
    923 	struct filedesc	*fdp;
    924 	struct file	*rf, *wf;
    925 	struct socket	*rso, *wso;
    926 	int		fd, error;
    927 
    928 	fdp = l->l_proc->p_fd;
    929 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l)) != 0)
    930 		return (error);
    931 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l)) != 0)
    932 		goto free1;
    933 	/* remember this socket pair implements a pipe */
    934 	wso->so_state |= SS_ISAPIPE;
    935 	rso->so_state |= SS_ISAPIPE;
    936 	/* falloc() will use the descriptor for us */
    937 	if ((error = falloc(l, &rf, &fd)) != 0)
    938 		goto free2;
    939 	retval[0] = fd;
    940 	rf->f_flag = FREAD;
    941 	rf->f_type = DTYPE_SOCKET;
    942 	rf->f_ops = &socketops;
    943 	rf->f_data = rso;
    944 	if ((error = falloc(l, &wf, &fd)) != 0)
    945 		goto free3;
    946 	wf->f_flag = FWRITE;
    947 	wf->f_type = DTYPE_SOCKET;
    948 	wf->f_ops = &socketops;
    949 	wf->f_data = wso;
    950 	retval[1] = fd;
    951 	if ((error = unp_connect2(wso, rso, PRU_CONNECT2)) != 0)
    952 		goto free4;
    953 	FILE_SET_MATURE(rf);
    954 	FILE_SET_MATURE(wf);
    955 	FILE_UNUSE(rf, l);
    956 	FILE_UNUSE(wf, l);
    957 	return (0);
    958  free4:
    959 	FILE_UNUSE(wf, l);
    960 	ffree(wf);
    961 	fdremove(fdp, retval[1]);
    962  free3:
    963 	FILE_UNUSE(rf, l);
    964 	ffree(rf);
    965 	fdremove(fdp, retval[0]);
    966  free2:
    967 	(void)soclose(wso);
    968  free1:
    969 	(void)soclose(rso);
    970 	return (error);
    971 }
    972 #endif /* PIPE_SOCKETPAIR */
    973 
    974 /*
    975  * Get socket name.
    976  */
    977 /* ARGSUSED */
    978 int
    979 sys_getsockname(struct lwp *l, void *v, register_t *retval)
    980 {
    981 	struct sys_getsockname_args /* {
    982 		syscallarg(int)			fdes;
    983 		syscallarg(struct sockaddr *)	asa;
    984 		syscallarg(unsigned int *)	alen;
    985 	} */ *uap = v;
    986 	struct proc	*p;
    987 	struct file	*fp;
    988 	struct socket	*so;
    989 	struct mbuf	*m;
    990 	unsigned int	len;
    991 	int		error;
    992 
    993 	p = l->l_proc;
    994 	/* getsock() will use the descriptor for us */
    995 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
    996 		return (error);
    997 	error = copyin(SCARG(uap, alen), &len, sizeof(len));
    998 	if (error)
    999 		goto out;
   1000 	so = (struct socket *)fp->f_data;
   1001 	m = m_getclr(M_WAIT, MT_SONAME);
   1002 	MCLAIM(m, so->so_mowner);
   1003 	error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
   1004 	    m, (struct mbuf *)0, (struct lwp *)0);
   1005 	if (error)
   1006 		goto bad;
   1007 	if (len > m->m_len)
   1008 		len = m->m_len;
   1009 	error = copyout(mtod(m, void *), SCARG(uap, asa), len);
   1010 	if (error == 0)
   1011 		error = copyout(&len, SCARG(uap, alen), sizeof(len));
   1012  bad:
   1013 	m_freem(m);
   1014  out:
   1015 	FILE_UNUSE(fp, l);
   1016 	return (error);
   1017 }
   1018 
   1019 /*
   1020  * Get name of peer for connected socket.
   1021  */
   1022 /* ARGSUSED */
   1023 int
   1024 sys_getpeername(struct lwp *l, void *v, register_t *retval)
   1025 {
   1026 	struct sys_getpeername_args /* {
   1027 		syscallarg(int)			fdes;
   1028 		syscallarg(struct sockaddr *)	asa;
   1029 		syscallarg(unsigned int *)	alen;
   1030 	} */ *uap = v;
   1031 	struct proc	*p;
   1032 	struct file	*fp;
   1033 	struct socket	*so;
   1034 	struct mbuf	*m;
   1035 	unsigned int	len;
   1036 	int		error;
   1037 
   1038 	p = l->l_proc;
   1039 	/* getsock() will use the descriptor for us */
   1040 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
   1041 		return (error);
   1042 	so = (struct socket *)fp->f_data;
   1043 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
   1044 		error = ENOTCONN;
   1045 		goto out;
   1046 	}
   1047 	error = copyin(SCARG(uap, alen), &len, sizeof(len));
   1048 	if (error)
   1049 		goto out;
   1050 	m = m_getclr(M_WAIT, MT_SONAME);
   1051 	MCLAIM(m, so->so_mowner);
   1052 	error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
   1053 	    m, (struct mbuf *)0, (struct lwp *)0);
   1054 	if (error)
   1055 		goto bad;
   1056 	if (len > m->m_len)
   1057 		len = m->m_len;
   1058 	error = copyout(mtod(m, void *), SCARG(uap, asa), len);
   1059 	if (error)
   1060 		goto bad;
   1061 	error = copyout(&len, SCARG(uap, alen), sizeof(len));
   1062  bad:
   1063 	m_freem(m);
   1064  out:
   1065 	FILE_UNUSE(fp, l);
   1066 	return (error);
   1067 }
   1068 
   1069 /*
   1070  * XXX In a perfect world, we wouldn't pass around socket control
   1071  * XXX arguments in mbufs, and this could go away.
   1072  */
   1073 int
   1074 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
   1075 {
   1076 	struct sockaddr	*sa;
   1077 	struct mbuf	*m;
   1078 	int		error;
   1079 
   1080 	/*
   1081 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1082 	 * will overflow sa_len.  Control data more than a page size in
   1083 	 * length is just too much.
   1084 	 */
   1085 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1086 		return (EINVAL);
   1087 
   1088 	/* Allocate an mbuf to hold the arguments. */
   1089 	m = m_get(M_WAIT, type);
   1090 	/* can't claim.  don't who to assign it to. */
   1091 	if (buflen > MLEN) {
   1092 		/*
   1093 		 * Won't fit into a regular mbuf, so we allocate just
   1094 		 * enough external storage to hold the argument.
   1095 		 */
   1096 		MEXTMALLOC(m, buflen, M_WAITOK);
   1097 	}
   1098 	m->m_len = buflen;
   1099 	error = copyin(bf, mtod(m, void *), buflen);
   1100 	if (error) {
   1101 		(void) m_free(m);
   1102 		return (error);
   1103 	}
   1104 	*mp = m;
   1105 	if (type == MT_SONAME) {
   1106 		sa = mtod(m, struct sockaddr *);
   1107 #if BYTE_ORDER != BIG_ENDIAN
   1108 		/*
   1109 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1110 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1111 		 */
   1112 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1113 			sa->sa_family = sa->sa_len;
   1114 #endif
   1115 		sa->sa_len = buflen;
   1116 	}
   1117 	return (0);
   1118 }
   1119 
   1120 int
   1121 getsock(struct filedesc *fdp, int fdes, struct file **fpp)
   1122 {
   1123 	struct file	*fp;
   1124 
   1125 	if ((fp = fd_getfile(fdp, fdes)) == NULL)
   1126 		return (EBADF);
   1127 
   1128 	FILE_USE(fp);
   1129 
   1130 	if (fp->f_type != DTYPE_SOCKET) {
   1131 		FILE_UNUSE(fp, NULL);
   1132 		return (ENOTSOCK);
   1133 	}
   1134 	*fpp = fp;
   1135 	return (0);
   1136 }
   1137