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