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