Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.75
      1 /*	$NetBSD: uipc_syscalls.c,v 1.75 2003/01/18 10:06:36 thorpej 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.75 2003/01/18 10:06:36 thorpej 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/proc.h>
     48 #include <sys/file.h>
     49 #include <sys/buf.h>
     50 #include <sys/malloc.h>
     51 #include <sys/mbuf.h>
     52 #include <sys/protosw.h>
     53 #include <sys/socket.h>
     54 #include <sys/socketvar.h>
     55 #include <sys/signalvar.h>
     56 #include <sys/un.h>
     57 #ifdef KTRACE
     58 #include <sys/ktrace.h>
     59 #endif
     60 #include <sys/event.h>
     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 
    231 	/* connection has been removed from the listen queue */
    232 	KNOTE(&so->so_rcv.sb_sel.sel_klist, 0);
    233 
    234 	{ struct socket *aso = TAILQ_FIRST(&so->so_q);
    235 	  if (soqremque(aso, 1) == 0)
    236 		panic("accept");
    237 	  so = aso;
    238 	}
    239 	fp->f_type = DTYPE_SOCKET;
    240 	fp->f_flag = FREAD|FWRITE;
    241 	fp->f_ops = &socketops;
    242 	fp->f_data = (caddr_t)so;
    243 	FILE_UNUSE(fp, p);
    244 	nam = m_get(M_WAIT, MT_SONAME);
    245 	if ((error = soaccept(so, nam)) == 0 && SCARG(uap, name)) {
    246 		if (namelen > nam->m_len)
    247 			namelen = nam->m_len;
    248 		/* SHOULD COPY OUT A CHAIN HERE */
    249 		if ((error = copyout(mtod(nam, caddr_t),
    250 		    (caddr_t)SCARG(uap, name), namelen)) == 0)
    251 			error = copyout((caddr_t)&namelen,
    252 			    (caddr_t)SCARG(uap, anamelen),
    253 			    sizeof(*SCARG(uap, anamelen)));
    254 	}
    255 	/* if an error occurred, free the file descriptor */
    256 	if (error) {
    257 		fdremove(fdp, fd);
    258 		ffree(fp);
    259 	}
    260 	m_freem(nam);
    261 	splx(s);
    262 	FILE_SET_MATURE(fp);
    263 	return (error);
    264 }
    265 
    266 /* ARGSUSED */
    267 int
    268 sys_connect(struct lwp *l, void *v, register_t *retval)
    269 {
    270 	struct sys_connect_args /* {
    271 		syscallarg(int)				s;
    272 		syscallarg(const struct sockaddr *)	name;
    273 		syscallarg(unsigned int)		namelen;
    274 	} */ *uap = v;
    275 	struct proc	*p;
    276 	struct file	*fp;
    277 	struct socket	*so;
    278 	struct mbuf	*nam;
    279 	int		error, s;
    280 
    281 	p = l->l_proc;
    282 	/* getsock() will use the descriptor for us */
    283 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    284 		return (error);
    285 	so = (struct socket *)fp->f_data;
    286 	if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
    287 		error = EALREADY;
    288 		goto out;
    289 	}
    290 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    291 	    MT_SONAME);
    292 	if (error)
    293 		goto out;
    294 	error = soconnect(so, nam);
    295 	if (error)
    296 		goto bad;
    297 	if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
    298 		m_freem(nam);
    299 		error = EINPROGRESS;
    300 		goto out;
    301 	}
    302 	s = splsoftnet();
    303 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
    304 		error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
    305 			       netcon, 0);
    306 		if (error)
    307 			break;
    308 	}
    309 	if (error == 0) {
    310 		error = so->so_error;
    311 		so->so_error = 0;
    312 	}
    313 	splx(s);
    314  bad:
    315 	so->so_state &= ~SS_ISCONNECTING;
    316 	m_freem(nam);
    317 	if (error == ERESTART)
    318 		error = EINTR;
    319  out:
    320 	FILE_UNUSE(fp, p);
    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 proc *p;
    334 	struct filedesc	*fdp;
    335 	struct file	*fp1, *fp2;
    336 	struct socket	*so1, *so2;
    337 	int		fd, error, sv[2];
    338 
    339 	p = l->l_proc;
    340 	fdp = p->p_fd;
    341 	error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
    342 			 SCARG(uap, protocol));
    343 	if (error)
    344 		return (error);
    345 	error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
    346 			 SCARG(uap, protocol));
    347 	if (error)
    348 		goto free1;
    349 	/* falloc() will use the descriptor for us */
    350 	if ((error = falloc(p, &fp1, &fd)) != 0)
    351 		goto free2;
    352 	sv[0] = fd;
    353 	fp1->f_flag = FREAD|FWRITE;
    354 	fp1->f_type = DTYPE_SOCKET;
    355 	fp1->f_ops = &socketops;
    356 	fp1->f_data = (caddr_t)so1;
    357 	if ((error = falloc(p, &fp2, &fd)) != 0)
    358 		goto free3;
    359 	fp2->f_flag = FREAD|FWRITE;
    360 	fp2->f_type = DTYPE_SOCKET;
    361 	fp2->f_ops = &socketops;
    362 	fp2->f_data = (caddr_t)so2;
    363 	sv[1] = fd;
    364 	if ((error = soconnect2(so1, so2)) != 0)
    365 		goto free4;
    366 	if (SCARG(uap, type) == SOCK_DGRAM) {
    367 		/*
    368 		 * Datagram socket connection is asymmetric.
    369 		 */
    370 		 if ((error = soconnect2(so2, so1)) != 0)
    371 			goto free4;
    372 	}
    373 	error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, rsv),
    374 	    2 * sizeof(int));
    375 	FILE_SET_MATURE(fp1);
    376 	FILE_SET_MATURE(fp2);
    377 	FILE_UNUSE(fp1, p);
    378 	FILE_UNUSE(fp2, p);
    379 	return (error);
    380  free4:
    381 	FILE_UNUSE(fp2, p);
    382 	ffree(fp2);
    383 	fdremove(fdp, sv[1]);
    384  free3:
    385 	FILE_UNUSE(fp1, p);
    386 	ffree(fp1);
    387 	fdremove(fdp, sv[0]);
    388  free2:
    389 	(void)soclose(so2);
    390  free1:
    391 	(void)soclose(so1);
    392 	return (error);
    393 }
    394 
    395 int
    396 sys_sendto(struct lwp *l, void *v, register_t *retval)
    397 {
    398 	struct sys_sendto_args /* {
    399 		syscallarg(int)				s;
    400 		syscallarg(const void *)		buf;
    401 		syscallarg(size_t)			len;
    402 		syscallarg(int)				flags;
    403 		syscallarg(const struct sockaddr *)	to;
    404 		syscallarg(unsigned int)		tolen;
    405 	} */ *uap = v;
    406 	struct proc	*p;
    407 	struct msghdr	msg;
    408 	struct iovec	aiov;
    409 
    410 	p = l->l_proc;
    411 	msg.msg_name = (caddr_t)SCARG(uap, to);		/* XXX kills const */
    412 	msg.msg_namelen = SCARG(uap, tolen);
    413 	msg.msg_iov = &aiov;
    414 	msg.msg_iovlen = 1;
    415 	msg.msg_control = 0;
    416 	msg.msg_flags = 0;
    417 	aiov.iov_base = (char *)SCARG(uap, buf);	/* XXX kills const */
    418 	aiov.iov_len = SCARG(uap, len);
    419 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
    420 }
    421 
    422 int
    423 sys_sendmsg(struct lwp *l, void *v, register_t *retval)
    424 {
    425 	struct sys_sendmsg_args /* {
    426 		syscallarg(int)				s;
    427 		syscallarg(const struct msghdr *)	msg;
    428 		syscallarg(int)				flags;
    429 	} */ *uap = v;
    430 	struct proc	*p;
    431 	struct msghdr	msg;
    432 	struct iovec	aiov[UIO_SMALLIOV], *iov;
    433 	int		error;
    434 
    435 	error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
    436 	if (error)
    437 		return (error);
    438 	if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
    439 		if ((unsigned int)msg.msg_iovlen > IOV_MAX)
    440 			return (EMSGSIZE);
    441 		iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
    442 		    M_IOV, M_WAITOK);
    443 	} else
    444 		iov = aiov;
    445 	if ((unsigned int)msg.msg_iovlen > 0) {
    446 		error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
    447 		    (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
    448 		if (error)
    449 			goto done;
    450 	}
    451 	msg.msg_iov = iov;
    452 	msg.msg_flags = 0;
    453 	p = l->l_proc;
    454 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    455 done:
    456 	if (iov != aiov)
    457 		free(iov, M_IOV);
    458 	return (error);
    459 }
    460 
    461 int
    462 sendit(struct proc *p, int s, struct msghdr *mp, int flags, register_t *retsize)
    463 {
    464 	struct file	*fp;
    465 	struct uio	auio;
    466 	struct iovec	*iov;
    467 	int		i, len, error;
    468 	struct mbuf	*to, *control;
    469 	struct socket	*so;
    470 #ifdef KTRACE
    471 	struct iovec	*ktriov;
    472 #endif
    473 
    474 #ifdef KTRACE
    475 	ktriov = NULL;
    476 #endif
    477 	/* getsock() will use the descriptor for us */
    478 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    479 		return (error);
    480 	auio.uio_iov = mp->msg_iov;
    481 	auio.uio_iovcnt = mp->msg_iovlen;
    482 	auio.uio_segflg = UIO_USERSPACE;
    483 	auio.uio_rw = UIO_WRITE;
    484 	auio.uio_procp = p;
    485 	auio.uio_offset = 0;			/* XXX */
    486 	auio.uio_resid = 0;
    487 	iov = mp->msg_iov;
    488 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    489 #if 0
    490 		/* cannot happen; iov_len is unsigned */
    491 		if (iov->iov_len < 0) {
    492 			error = EINVAL;
    493 			goto out;
    494 		}
    495 #endif
    496 		/*
    497 		 * Writes return ssize_t because -1 is returned on error.
    498 		 * Therefore, we must restrict the length to SSIZE_MAX to
    499 		 * avoid garbage return values.
    500 		 */
    501 		auio.uio_resid += iov->iov_len;
    502 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    503 			error = EINVAL;
    504 			goto out;
    505 		}
    506 	}
    507 	if (mp->msg_name) {
    508 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    509 				 MT_SONAME);
    510 		if (error)
    511 			goto out;
    512 	} else
    513 		to = 0;
    514 	if (mp->msg_control) {
    515 		if (mp->msg_controllen < sizeof(struct cmsghdr)) {
    516 			error = EINVAL;
    517 			goto bad;
    518 		}
    519 		error = sockargs(&control, mp->msg_control,
    520 				 mp->msg_controllen, MT_CONTROL);
    521 		if (error)
    522 			goto bad;
    523 	} else
    524 		control = 0;
    525 #ifdef KTRACE
    526 	if (KTRPOINT(p, KTR_GENIO)) {
    527 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    528 
    529 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    530 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
    531 	}
    532 #endif
    533 	len = auio.uio_resid;
    534 	so = (struct socket *)fp->f_data;
    535 	error = (*so->so_send)(so, to, &auio, NULL, control, flags);
    536 	if (error) {
    537 		if (auio.uio_resid != len && (error == ERESTART ||
    538 		    error == EINTR || error == EWOULDBLOCK))
    539 			error = 0;
    540 		if (error == EPIPE)
    541 			psignal(p, SIGPIPE);
    542 	}
    543 	if (error == 0)
    544 		*retsize = len - auio.uio_resid;
    545 #ifdef KTRACE
    546 	if (ktriov != NULL) {
    547 		if (error == 0)
    548 			ktrgenio(p, s, UIO_WRITE, ktriov, *retsize, error);
    549 		free(ktriov, M_TEMP);
    550 	}
    551 #endif
    552  bad:
    553 	if (to)
    554 		m_freem(to);
    555  out:
    556 	FILE_UNUSE(fp, p);
    557 	return (error);
    558 }
    559 
    560 int
    561 sys_recvfrom(struct lwp *l, void *v, register_t *retval)
    562 {
    563 	struct sys_recvfrom_args /* {
    564 		syscallarg(int)			s;
    565 		syscallarg(void *)		buf;
    566 		syscallarg(size_t)		len;
    567 		syscallarg(int)			flags;
    568 		syscallarg(struct sockaddr *)	from;
    569 		syscallarg(unsigned int *)	fromlenaddr;
    570 	} */ *uap = v;
    571 	struct proc	*p;
    572 	struct msghdr	msg;
    573 	struct iovec	aiov;
    574 	int		error;
    575 
    576 	if (SCARG(uap, fromlenaddr)) {
    577 		error = copyin((caddr_t)SCARG(uap, fromlenaddr),
    578 			       (caddr_t)&msg.msg_namelen,
    579 			       sizeof(msg.msg_namelen));
    580 		if (error)
    581 			return (error);
    582 	} else
    583 		msg.msg_namelen = 0;
    584 	msg.msg_name = (caddr_t)SCARG(uap, from);
    585 	msg.msg_iov = &aiov;
    586 	msg.msg_iovlen = 1;
    587 	aiov.iov_base = SCARG(uap, buf);
    588 	aiov.iov_len = SCARG(uap, len);
    589 	msg.msg_control = 0;
    590 	msg.msg_flags = SCARG(uap, flags);
    591 	p = l->l_proc;
    592 	return (recvit(p, SCARG(uap, s), &msg,
    593 		       (caddr_t)SCARG(uap, fromlenaddr), retval));
    594 }
    595 
    596 int
    597 sys_recvmsg(struct lwp *l, void *v, register_t *retval)
    598 {
    599 	struct sys_recvmsg_args /* {
    600 		syscallarg(int)			s;
    601 		syscallarg(struct msghdr *)	msg;
    602 		syscallarg(int)			flags;
    603 	} */ *uap = v;
    604 	struct proc	*p;
    605 	struct msghdr	msg;
    606 	struct iovec	aiov[UIO_SMALLIOV], *uiov, *iov;
    607 	int		error;
    608 
    609 	error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
    610 		       sizeof(msg));
    611 	if (error)
    612 		return (error);
    613 	if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
    614 		if ((unsigned int)msg.msg_iovlen > IOV_MAX)
    615 			return (EMSGSIZE);
    616 		iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
    617 		    M_IOV, M_WAITOK);
    618 	} else
    619 		iov = aiov;
    620 	if ((unsigned int)msg.msg_iovlen > 0) {
    621 		error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
    622 		    (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
    623 		if (error)
    624 			goto done;
    625 	}
    626 	uiov = msg.msg_iov;
    627 	msg.msg_iov = iov;
    628 	msg.msg_flags = SCARG(uap, flags);
    629 	p = l->l_proc;
    630 	if ((error = recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval)) == 0) {
    631 		msg.msg_iov = uiov;
    632 		error = copyout((caddr_t)&msg, (caddr_t)SCARG(uap, msg),
    633 		    sizeof(msg));
    634 	}
    635 done:
    636 	if (iov != aiov)
    637 		free(iov, M_IOV);
    638 	return (error);
    639 }
    640 
    641 int
    642 recvit(struct proc *p, int s, struct msghdr *mp, caddr_t namelenp,
    643 	register_t *retsize)
    644 {
    645 	struct file	*fp;
    646 	struct uio	auio;
    647 	struct iovec	*iov;
    648 	int		i, len, error;
    649 	struct mbuf	*from, *control;
    650 	struct socket	*so;
    651 #ifdef KTRACE
    652 	struct iovec	*ktriov;
    653 #endif
    654 
    655 	from = 0;
    656 	control = 0;
    657 #ifdef KTRACE
    658 	ktriov = NULL;
    659 #endif
    660 
    661 	/* getsock() will use the descriptor for us */
    662 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    663 		return (error);
    664 	auio.uio_iov = mp->msg_iov;
    665 	auio.uio_iovcnt = mp->msg_iovlen;
    666 	auio.uio_segflg = UIO_USERSPACE;
    667 	auio.uio_rw = UIO_READ;
    668 	auio.uio_procp = p;
    669 	auio.uio_offset = 0;			/* XXX */
    670 	auio.uio_resid = 0;
    671 	iov = mp->msg_iov;
    672 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    673 #if 0
    674 		/* cannot happen iov_len is unsigned */
    675 		if (iov->iov_len < 0) {
    676 			error = EINVAL;
    677 			goto out1;
    678 		}
    679 #endif
    680 		/*
    681 		 * Reads return ssize_t because -1 is returned on error.
    682 		 * Therefore we must restrict the length to SSIZE_MAX to
    683 		 * avoid garbage return values.
    684 		 */
    685 		auio.uio_resid += iov->iov_len;
    686 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    687 			error = EINVAL;
    688 			goto out1;
    689 		}
    690 	}
    691 #ifdef KTRACE
    692 	if (KTRPOINT(p, KTR_GENIO)) {
    693 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    694 
    695 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    696 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
    697 	}
    698 #endif
    699 	len = auio.uio_resid;
    700 	so = (struct socket *)fp->f_data;
    701 	error = (*so->so_receive)(so, &from, &auio, NULL,
    702 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
    703 	if (error) {
    704 		if (auio.uio_resid != len && (error == ERESTART ||
    705 		    error == EINTR || error == EWOULDBLOCK))
    706 			error = 0;
    707 	}
    708 #ifdef KTRACE
    709 	if (ktriov != NULL) {
    710 		if (error == 0)
    711 			ktrgenio(p, s, UIO_READ, ktriov,
    712 			    len - auio.uio_resid, error);
    713 		free(ktriov, M_TEMP);
    714 	}
    715 #endif
    716 	if (error)
    717 		goto out;
    718 	*retsize = len - auio.uio_resid;
    719 	if (mp->msg_name) {
    720 		len = mp->msg_namelen;
    721 		if (len <= 0 || from == 0)
    722 			len = 0;
    723 		else {
    724 			if (len > from->m_len)
    725 				len = from->m_len;
    726 			/* else if len < from->m_len ??? */
    727 			error = copyout(mtod(from, caddr_t),
    728 					(caddr_t)mp->msg_name, (unsigned)len);
    729 			if (error)
    730 				goto out;
    731 		}
    732 		mp->msg_namelen = len;
    733 		if (namelenp &&
    734 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int))))
    735 			goto out;
    736 	}
    737 	if (mp->msg_control) {
    738 		len = mp->msg_controllen;
    739 		if (len <= 0 || control == 0)
    740 			len = 0;
    741 		else {
    742 			struct mbuf *m = control;
    743 			caddr_t p = (caddr_t)mp->msg_control;
    744 
    745 			do {
    746 				i = m->m_len;
    747 				if (len < i) {
    748 					mp->msg_flags |= MSG_CTRUNC;
    749 					i = len;
    750 				}
    751 				error = copyout(mtod(m, caddr_t), p,
    752 				    (unsigned)i);
    753 				if (m->m_next)
    754 					i = ALIGN(i);
    755 				p += i;
    756 				len -= i;
    757 				if (error != 0 || len <= 0)
    758 					break;
    759 			} while ((m = m->m_next) != NULL);
    760 			len = p - (caddr_t)mp->msg_control;
    761 		}
    762 		mp->msg_controllen = len;
    763 	}
    764  out:
    765 	if (from)
    766 		m_freem(from);
    767 	if (control)
    768 		m_freem(control);
    769  out1:
    770 	FILE_UNUSE(fp, p);
    771 	return (error);
    772 }
    773 
    774 /* ARGSUSED */
    775 int
    776 sys_shutdown(struct lwp *l, void *v, register_t *retval)
    777 {
    778 	struct sys_shutdown_args /* {
    779 		syscallarg(int)	s;
    780 		syscallarg(int)	how;
    781 	} */ *uap = v;
    782 	struct proc	*p;
    783 	struct file	*fp;
    784 	int		error;
    785 
    786 	p = l->l_proc;
    787 	/* getsock() will use the descriptor for us */
    788 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    789 		return (error);
    790 	error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
    791 	FILE_UNUSE(fp, p);
    792 	return (error);
    793 }
    794 
    795 /* ARGSUSED */
    796 int
    797 sys_setsockopt(struct lwp *l, void *v, register_t *retval)
    798 {
    799 	struct sys_setsockopt_args /* {
    800 		syscallarg(int)			s;
    801 		syscallarg(int)			level;
    802 		syscallarg(int)			name;
    803 		syscallarg(const void *)	val;
    804 		syscallarg(unsigned int)	valsize;
    805 	} */ *uap = v;
    806 	struct proc	*p;
    807 	struct file	*fp;
    808 	struct mbuf	*m;
    809 	int		error;
    810 	unsigned int	len;
    811 
    812 	p = l->l_proc;
    813 	m = NULL;
    814 	/* getsock() will use the descriptor for us */
    815 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    816 		return (error);
    817 	len = SCARG(uap, valsize);
    818 	if (len > MCLBYTES) {
    819 		error = EINVAL;
    820 		goto out;
    821 	}
    822 	if (SCARG(uap, val)) {
    823 		m = m_get(M_WAIT, MT_SOOPTS);
    824 		if (len > MLEN)
    825 			MCLGET(m, M_WAIT);
    826 		error = copyin(SCARG(uap, val), mtod(m, caddr_t), len);
    827 		if (error) {
    828 			(void) m_free(m);
    829 			goto out;
    830 		}
    831 		m->m_len = SCARG(uap, valsize);
    832 	}
    833 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    834 			 SCARG(uap, name), m);
    835  out:
    836 	FILE_UNUSE(fp, p);
    837 	return (error);
    838 }
    839 
    840 /* ARGSUSED */
    841 int
    842 sys_getsockopt(struct lwp *l, void *v, register_t *retval)
    843 {
    844 	struct sys_getsockopt_args /* {
    845 		syscallarg(int)			s;
    846 		syscallarg(int)			level;
    847 		syscallarg(int)			name;
    848 		syscallarg(void *)		val;
    849 		syscallarg(unsigned int *)	avalsize;
    850 	} */ *uap = v;
    851 	struct proc	*p;
    852 	struct file	*fp;
    853 	struct mbuf	*m, *m0;
    854 	unsigned int	op, i, valsize;
    855 	int		error;
    856 
    857 	p = l->l_proc;
    858 	m = NULL;
    859 	/* getsock() will use the descriptor for us */
    860 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    861 		return (error);
    862 	if (SCARG(uap, val)) {
    863 		error = copyin((caddr_t)SCARG(uap, avalsize),
    864 			       (caddr_t)&valsize, sizeof(valsize));
    865 		if (error)
    866 			goto out;
    867 	} else
    868 		valsize = 0;
    869 	if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
    870 	    SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
    871 	    m != NULL) {
    872 		op = 0;
    873 		while (m && !error && op < valsize) {
    874 			i = min(m->m_len, (valsize - op));
    875 			error = copyout(mtod(m, caddr_t), SCARG(uap, val), i);
    876 			op += i;
    877 			SCARG(uap, val) = ((u_int8_t *)SCARG(uap, val)) + i;
    878 			m0 = m;
    879 			MFREE(m0, m);
    880 		}
    881 		valsize = op;
    882 		if (error == 0)
    883 			error = copyout(&valsize,
    884 					SCARG(uap, avalsize), sizeof(valsize));
    885 	}
    886 	if (m != NULL)
    887 		(void) m_free(m);
    888  out:
    889 	FILE_UNUSE(fp, p);
    890 	return (error);
    891 }
    892 
    893 #ifdef PIPE_SOCKETPAIR
    894 /* ARGSUSED */
    895 int
    896 sys_pipe(struct lwp *l, void *v, register_t *retval)
    897 {
    898 	struct proc	*p;
    899 	struct filedesc	*fdp;
    900 	struct file	*rf, *wf;
    901 	struct socket	*rso, *wso;
    902 	int		fd, error;
    903 
    904 	p = l->l_proc;
    905 	fdp = p->p_fd;
    906 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
    907 		return (error);
    908 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
    909 		goto free1;
    910 	/* remember this socket pair implements a pipe */
    911 	wso->so_state |= SS_ISAPIPE;
    912 	rso->so_state |= SS_ISAPIPE;
    913 	/* falloc() will use the descriptor for us */
    914 	if ((error = falloc(p, &rf, &fd)) != 0)
    915 		goto free2;
    916 	retval[0] = fd;
    917 	rf->f_flag = FREAD;
    918 	rf->f_type = DTYPE_SOCKET;
    919 	rf->f_ops = &socketops;
    920 	rf->f_data = (caddr_t)rso;
    921 	if ((error = falloc(p, &wf, &fd)) != 0)
    922 		goto free3;
    923 	wf->f_flag = FWRITE;
    924 	wf->f_type = DTYPE_SOCKET;
    925 	wf->f_ops = &socketops;
    926 	wf->f_data = (caddr_t)wso;
    927 	retval[1] = fd;
    928 	if ((error = unp_connect2(wso, rso)) != 0)
    929 		goto free4;
    930 	FILE_SET_MATURE(rf);
    931 	FILE_SET_MATURE(wf);
    932 	FILE_UNUSE(rf, p);
    933 	FILE_UNUSE(wf, p);
    934 	return (0);
    935  free4:
    936 	FILE_UNUSE(wf, p);
    937 	ffree(wf);
    938 	fdremove(fdp, retval[1]);
    939  free3:
    940 	FILE_UNUSE(rf, p);
    941 	ffree(rf);
    942 	fdremove(fdp, retval[0]);
    943  free2:
    944 	(void)soclose(wso);
    945  free1:
    946 	(void)soclose(rso);
    947 	return (error);
    948 }
    949 #endif /* PIPE_SOCKETPAIR */
    950 
    951 /*
    952  * Get socket name.
    953  */
    954 /* ARGSUSED */
    955 int
    956 sys_getsockname(struct lwp *l, void *v, register_t *retval)
    957 {
    958 	struct sys_getsockname_args /* {
    959 		syscallarg(int)			fdes;
    960 		syscallarg(struct sockaddr *)	asa;
    961 		syscallarg(unsigned int *)	alen;
    962 	} */ *uap = v;
    963 	struct proc	*p;
    964 	struct file	*fp;
    965 	struct socket	*so;
    966 	struct mbuf	*m;
    967 	unsigned int	len;
    968 	int		error;
    969 
    970 	p = l->l_proc;
    971 	/* getsock() will use the descriptor for us */
    972 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
    973 		return (error);
    974 	error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
    975 	if (error)
    976 		goto out;
    977 	so = (struct socket *)fp->f_data;
    978 	m = m_getclr(M_WAIT, MT_SONAME);
    979 	error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
    980 	    m, (struct mbuf *)0, (struct proc *)0);
    981 	if (error)
    982 		goto bad;
    983 	if (len > m->m_len)
    984 		len = m->m_len;
    985 	error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
    986 	if (error == 0)
    987 		error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen),
    988 		    sizeof(len));
    989  bad:
    990 	m_freem(m);
    991  out:
    992 	FILE_UNUSE(fp, p);
    993 	return (error);
    994 }
    995 
    996 /*
    997  * Get name of peer for connected socket.
    998  */
    999 /* ARGSUSED */
   1000 int
   1001 sys_getpeername(struct lwp *l, void *v, register_t *retval)
   1002 {
   1003 	struct sys_getpeername_args /* {
   1004 		syscallarg(int)			fdes;
   1005 		syscallarg(struct sockaddr *)	asa;
   1006 		syscallarg(unsigned int *)	alen;
   1007 	} */ *uap = v;
   1008 	struct proc	*p;
   1009 	struct file	*fp;
   1010 	struct socket	*so;
   1011 	struct mbuf	*m;
   1012 	unsigned int	len;
   1013 	int		error;
   1014 
   1015 	p = l->l_proc;
   1016 	/* getsock() will use the descriptor for us */
   1017 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
   1018 		return (error);
   1019 	so = (struct socket *)fp->f_data;
   1020 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
   1021 		error = ENOTCONN;
   1022 		goto out;
   1023 	}
   1024 	error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
   1025 	if (error)
   1026 		goto out;
   1027 	m = m_getclr(M_WAIT, MT_SONAME);
   1028 	error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
   1029 	    m, (struct mbuf *)0, (struct proc *)0);
   1030 	if (error)
   1031 		goto bad;
   1032 	if (len > m->m_len)
   1033 		len = m->m_len;
   1034 	error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
   1035 	if (error)
   1036 		goto bad;
   1037 	error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen), sizeof(len));
   1038  bad:
   1039 	m_freem(m);
   1040  out:
   1041 	FILE_UNUSE(fp, p);
   1042 	return (error);
   1043 }
   1044 
   1045 /*
   1046  * XXX In a perfect world, we wouldn't pass around socket control
   1047  * XXX arguments in mbufs, and this could go away.
   1048  */
   1049 int
   1050 sockargs(struct mbuf **mp, const void *buf, size_t buflen, int type)
   1051 {
   1052 	struct sockaddr	*sa;
   1053 	struct mbuf	*m;
   1054 	int		error;
   1055 
   1056 	/*
   1057 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1058 	 * will overflow sa_len.  Control data more than a page size in
   1059 	 * length is just too much.
   1060 	 */
   1061 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1062 		return (EINVAL);
   1063 
   1064 	/* Allocate an mbuf to hold the arguments. */
   1065 	m = m_get(M_WAIT, type);
   1066 	if (buflen > MLEN) {
   1067 		/*
   1068 		 * Won't fit into a regular mbuf, so we allocate just
   1069 		 * enough external storage to hold the argument.
   1070 		 */
   1071 		MEXTMALLOC(m, buflen, M_WAITOK);
   1072 	}
   1073 	m->m_len = buflen;
   1074 	error = copyin(buf, mtod(m, caddr_t), buflen);
   1075 	if (error) {
   1076 		(void) m_free(m);
   1077 		return (error);
   1078 	}
   1079 	*mp = m;
   1080 	if (type == MT_SONAME) {
   1081 		sa = mtod(m, struct sockaddr *);
   1082 #if BYTE_ORDER != BIG_ENDIAN
   1083 		/*
   1084 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1085 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1086 		 */
   1087 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1088 			sa->sa_family = sa->sa_len;
   1089 #endif
   1090 		sa->sa_len = buflen;
   1091 	}
   1092 	return (0);
   1093 }
   1094 
   1095 int
   1096 getsock(struct filedesc *fdp, int fdes, struct file **fpp)
   1097 {
   1098 	struct file	*fp;
   1099 
   1100 	if ((fp = fd_getfile(fdp, fdes)) == NULL)
   1101 		return (EBADF);
   1102 
   1103 	FILE_USE(fp);
   1104 
   1105 	if (fp->f_type != DTYPE_SOCKET) {
   1106 		FILE_UNUSE(fp, NULL);
   1107 		return (ENOTSOCK);
   1108 	}
   1109 	*fpp = fp;
   1110 	return (0);
   1111 }
   1112