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