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