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