Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.51
      1 /*	$NetBSD: uipc_syscalls.c,v 1.51 2000/03/30 09:27:14 augustss 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->p_tracep, s, UIO_WRITE,
    575 				ktriov, *retsize, error);
    576 		FREE(ktriov, M_TEMP);
    577 	}
    578 #endif
    579  bad:
    580 	if (to)
    581 		m_freem(to);
    582  out:
    583 	FILE_UNUSE(fp, p);
    584 	return (error);
    585 }
    586 
    587 int
    588 sys_recvfrom(p, v, retval)
    589 	struct proc *p;
    590 	void *v;
    591 	register_t *retval;
    592 {
    593 	struct sys_recvfrom_args /* {
    594 		syscallarg(int) s;
    595 		syscallarg(void *) buf;
    596 		syscallarg(size_t) len;
    597 		syscallarg(int) flags;
    598 		syscallarg(struct sockaddr *) from;
    599 		syscallarg(unsigned int *) fromlenaddr;
    600 	} */ *uap = v;
    601 	struct msghdr msg;
    602 	struct iovec aiov;
    603 	int error;
    604 
    605 	if (SCARG(uap, fromlenaddr)) {
    606 		error = copyin((caddr_t)SCARG(uap, fromlenaddr),
    607 			       (caddr_t)&msg.msg_namelen,
    608 			       sizeof(msg.msg_namelen));
    609 		if (error)
    610 			return (error);
    611 	} else
    612 		msg.msg_namelen = 0;
    613 	msg.msg_name = (caddr_t)SCARG(uap, from);
    614 	msg.msg_iov = &aiov;
    615 	msg.msg_iovlen = 1;
    616 	aiov.iov_base = SCARG(uap, buf);
    617 	aiov.iov_len = SCARG(uap, len);
    618 	msg.msg_control = 0;
    619 	msg.msg_flags = SCARG(uap, flags);
    620 	return (recvit(p, SCARG(uap, s), &msg,
    621 		       (caddr_t)SCARG(uap, fromlenaddr), retval));
    622 }
    623 
    624 int
    625 sys_recvmsg(p, v, retval)
    626 	struct proc *p;
    627 	void *v;
    628 	register_t *retval;
    629 {
    630 	struct sys_recvmsg_args /* {
    631 		syscallarg(int) s;
    632 		syscallarg(struct msghdr *) msg;
    633 		syscallarg(int) flags;
    634 	} */ *uap = v;
    635 	struct msghdr msg;
    636 	struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
    637 	int error;
    638 
    639 	error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
    640 		       sizeof(msg));
    641 	if (error)
    642 		return (error);
    643 	if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
    644 		if ((unsigned int)msg.msg_iovlen > IOV_MAX)
    645 			return (EMSGSIZE);
    646 		MALLOC(iov, struct iovec *,
    647 		    sizeof(struct iovec) * msg.msg_iovlen, M_IOV, M_WAITOK);
    648 	} else
    649 		iov = aiov;
    650 	if ((unsigned int)msg.msg_iovlen > 0) {
    651 		error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
    652 		    (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
    653 		if (error)
    654 			goto done;
    655 	}
    656 	uiov = msg.msg_iov;
    657 	msg.msg_iov = iov;
    658 #ifdef COMPAT_OLDSOCK
    659 	msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
    660 #else
    661 	msg.msg_flags = SCARG(uap, flags);
    662 #endif
    663 	if ((error = recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval)) == 0) {
    664 		msg.msg_iov = uiov;
    665 		error = copyout((caddr_t)&msg, (caddr_t)SCARG(uap, msg),
    666 		    sizeof(msg));
    667 	}
    668 done:
    669 	if (iov != aiov)
    670 		FREE(iov, M_IOV);
    671 	return (error);
    672 }
    673 
    674 int
    675 recvit(p, s, mp, namelenp, retsize)
    676 	struct proc *p;
    677 	int s;
    678 	struct msghdr *mp;
    679 	caddr_t namelenp;
    680 	register_t *retsize;
    681 {
    682 	struct file *fp;
    683 	struct uio auio;
    684 	struct iovec *iov;
    685 	int i;
    686 	int len, error;
    687 	struct mbuf *from = 0, *control = 0;
    688 	struct socket *so;
    689 #ifdef KTRACE
    690 	struct iovec *ktriov = NULL;
    691 #endif
    692 
    693 	/* getsock() will use the descriptor for us */
    694 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    695 		return (error);
    696 	auio.uio_iov = mp->msg_iov;
    697 	auio.uio_iovcnt = mp->msg_iovlen;
    698 	auio.uio_segflg = UIO_USERSPACE;
    699 	auio.uio_rw = UIO_READ;
    700 	auio.uio_procp = p;
    701 	auio.uio_offset = 0;			/* XXX */
    702 	auio.uio_resid = 0;
    703 	iov = mp->msg_iov;
    704 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    705 #if 0
    706 		/* cannot happen iov_len is unsigned */
    707 		if (iov->iov_len < 0) {
    708 			error = EINVAL;
    709 			goto out1;
    710 		}
    711 #endif
    712 		/*
    713 		 * Reads return ssize_t because -1 is returned on error.
    714 		 * Therefore we must restrict the length to SSIZE_MAX to
    715 		 * avoid garbage return values.
    716 		 */
    717 		auio.uio_resid += iov->iov_len;
    718 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    719 			error = EINVAL;
    720 			goto out1;
    721 		}
    722 	}
    723 #ifdef KTRACE
    724 	if (KTRPOINT(p, KTR_GENIO)) {
    725 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    726 
    727 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
    728 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
    729 	}
    730 #endif
    731 	len = auio.uio_resid;
    732 	so = (struct socket *)fp->f_data;
    733 	error = (*so->so_receive)(so, &from, &auio, NULL,
    734 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
    735 	if (error) {
    736 		if (auio.uio_resid != len && (error == ERESTART ||
    737 		    error == EINTR || error == EWOULDBLOCK))
    738 			error = 0;
    739 	}
    740 #ifdef KTRACE
    741 	if (ktriov != NULL) {
    742 		if (error == 0)
    743 			ktrgenio(p->p_tracep, s, UIO_READ,
    744 				ktriov, len - auio.uio_resid, error);
    745 		FREE(ktriov, M_TEMP);
    746 	}
    747 #endif
    748 	if (error)
    749 		goto out;
    750 	*retsize = len - auio.uio_resid;
    751 	if (mp->msg_name) {
    752 		len = mp->msg_namelen;
    753 		if (len <= 0 || from == 0)
    754 			len = 0;
    755 		else {
    756 #ifdef COMPAT_OLDSOCK
    757 			if (mp->msg_flags & MSG_COMPAT)
    758 				mtod(from, struct osockaddr *)->sa_family =
    759 				    mtod(from, struct sockaddr *)->sa_family;
    760 #endif
    761 			if (len > from->m_len)
    762 				len = from->m_len;
    763 			/* else if len < from->m_len ??? */
    764 			error = copyout(mtod(from, caddr_t),
    765 					(caddr_t)mp->msg_name, (unsigned)len);
    766 			if (error)
    767 				goto out;
    768 		}
    769 		mp->msg_namelen = len;
    770 		if (namelenp &&
    771 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
    772 #ifdef COMPAT_OLDSOCK
    773 			if (mp->msg_flags & MSG_COMPAT)
    774 				error = 0;	/* old recvfrom didn't check */
    775 			else
    776 #endif
    777 			goto out;
    778 		}
    779 	}
    780 	if (mp->msg_control) {
    781 #ifdef COMPAT_OLDSOCK
    782 		/*
    783 		 * We assume that old recvmsg calls won't receive access
    784 		 * rights and other control info, esp. as control info
    785 		 * is always optional and those options didn't exist in 4.3.
    786 		 * If we receive rights, trim the cmsghdr; anything else
    787 		 * is tossed.
    788 		 */
    789 		if (control && mp->msg_flags & MSG_COMPAT) {
    790 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
    791 			    SOL_SOCKET ||
    792 			    mtod(control, struct cmsghdr *)->cmsg_type !=
    793 			    SCM_RIGHTS) {
    794 				mp->msg_controllen = 0;
    795 				goto out;
    796 			}
    797 			control->m_len -= sizeof(struct cmsghdr);
    798 			control->m_data += sizeof(struct cmsghdr);
    799 		}
    800 #endif
    801 		len = mp->msg_controllen;
    802 		if (len <= 0 || control == 0)
    803 			len = 0;
    804 		else {
    805 			struct mbuf *m = control;
    806 			caddr_t p = (caddr_t)mp->msg_control;
    807 
    808 			do {
    809 				i = m->m_len;
    810 				if (len < i) {
    811 					mp->msg_flags |= MSG_CTRUNC;
    812 					i = len;
    813 				}
    814 				error = copyout(mtod(m, caddr_t), p,
    815 				    (unsigned)i);
    816 				if (m->m_next)
    817 					i = ALIGN(i);
    818 				p += i;
    819 				len -= i;
    820 				if (error != 0 || len <= 0)
    821 					break;
    822 			} while ((m = m->m_next) != NULL);
    823 			len = p - (caddr_t)mp->msg_control;
    824 		}
    825 		mp->msg_controllen = len;
    826 	}
    827  out:
    828 	if (from)
    829 		m_freem(from);
    830 	if (control)
    831 		m_freem(control);
    832  out1:
    833 	FILE_UNUSE(fp, p);
    834 	return (error);
    835 }
    836 
    837 /* ARGSUSED */
    838 int
    839 sys_shutdown(p, v, retval)
    840 	struct proc *p;
    841 	void *v;
    842 	register_t *retval;
    843 {
    844 	struct sys_shutdown_args /* {
    845 		syscallarg(int) s;
    846 		syscallarg(int) how;
    847 	} */ *uap = v;
    848 	struct file *fp;
    849 	int error;
    850 
    851 	/* getsock() will use the descriptor for us */
    852 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    853 		return (error);
    854 	error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
    855 	FILE_UNUSE(fp, p);
    856 	return (error);
    857 }
    858 
    859 /* ARGSUSED */
    860 int
    861 sys_setsockopt(p, v, retval)
    862 	struct proc *p;
    863 	void *v;
    864 	register_t *retval;
    865 {
    866 	struct sys_setsockopt_args /* {
    867 		syscallarg(int) s;
    868 		syscallarg(int) level;
    869 		syscallarg(int) name;
    870 		syscallarg(const void *) val;
    871 		syscallarg(unsigned int) valsize;
    872 	} */ *uap = v;
    873 	struct file *fp;
    874 	struct mbuf *m = NULL;
    875 	int error;
    876 
    877 	/* getsock() will use the descriptor for us */
    878 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    879 		return (error);
    880 	if (SCARG(uap, valsize) > MLEN) {
    881 		error = EINVAL;
    882 		goto out;
    883 	}
    884 	if (SCARG(uap, val)) {
    885 		m = m_get(M_WAIT, MT_SOOPTS);
    886 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
    887 			       SCARG(uap, valsize));
    888 		if (error) {
    889 			(void) m_free(m);
    890 			goto out;
    891 		}
    892 		m->m_len = SCARG(uap, valsize);
    893 	}
    894 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    895 			 SCARG(uap, name), m);
    896  out:
    897 	FILE_UNUSE(fp, p);
    898 	return (error);
    899 }
    900 
    901 /* ARGSUSED */
    902 int
    903 sys_getsockopt(p, v, retval)
    904 	struct proc *p;
    905 	void *v;
    906 	register_t *retval;
    907 {
    908 	struct sys_getsockopt_args /* {
    909 		syscallarg(int) s;
    910 		syscallarg(int) level;
    911 		syscallarg(int) name;
    912 		syscallarg(void *) val;
    913 		syscallarg(unsigned int *) avalsize;
    914 	} */ *uap = v;
    915 	struct file *fp;
    916 	struct mbuf *m = NULL, *m0;
    917 	unsigned int op, i, valsize;
    918 	int error;
    919 
    920 	/* getsock() will use the descriptor for us */
    921 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    922 		return (error);
    923 	if (SCARG(uap, val)) {
    924 		error = copyin((caddr_t)SCARG(uap, avalsize),
    925 			       (caddr_t)&valsize, sizeof(valsize));
    926 		if (error)
    927 			goto out;
    928 	} else
    929 		valsize = 0;
    930 	if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
    931 	    SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
    932 	    m != NULL) {
    933 		op = 0;
    934 		while (m && !error && op < valsize) {
    935 			i = min(m->m_len, (valsize - op));
    936 			error = copyout(mtod(m, caddr_t), SCARG(uap, val), i);
    937 			op += i;
    938 			SCARG(uap, val) = ((u_int8_t *)SCARG(uap, val)) + i;
    939 			m0 = m;
    940 			MFREE(m0, m);
    941 		}
    942 		valsize = op;
    943 		if (error == 0)
    944 			error = copyout(&valsize,
    945 					SCARG(uap, avalsize), sizeof(valsize));
    946 	}
    947 	if (m != NULL)
    948 		(void) m_free(m);
    949  out:
    950 	FILE_UNUSE(fp, p);
    951 	return (error);
    952 }
    953 
    954 /* ARGSUSED */
    955 int
    956 sys_pipe(p, v, retval)
    957 	struct proc *p;
    958 	void *v;
    959 	register_t *retval;
    960 {
    961 	struct filedesc *fdp = p->p_fd;
    962 	struct file *rf, *wf;
    963 	struct socket *rso, *wso;
    964 	int fd, error;
    965 
    966 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
    967 		return (error);
    968 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
    969 		goto free1;
    970 	/* falloc() will use the descriptor for us */
    971 	if ((error = falloc(p, &rf, &fd)) != 0)
    972 		goto free2;
    973 	retval[0] = fd;
    974 	rf->f_flag = FREAD;
    975 	rf->f_type = DTYPE_SOCKET;
    976 	rf->f_ops = &socketops;
    977 	rf->f_data = (caddr_t)rso;
    978 	if ((error = falloc(p, &wf, &fd)) != 0)
    979 		goto free3;
    980 	wf->f_flag = FWRITE;
    981 	wf->f_type = DTYPE_SOCKET;
    982 	wf->f_ops = &socketops;
    983 	wf->f_data = (caddr_t)wso;
    984 	retval[1] = fd;
    985 	if ((error = unp_connect2(wso, rso)) != 0)
    986 		goto free4;
    987 	FILE_UNUSE(rf, p);
    988 	FILE_UNUSE(wf, p);
    989 	return (0);
    990 free4:
    991 	FILE_UNUSE(wf, p);
    992 	ffree(wf);
    993 	fdremove(fdp, retval[1]);
    994 free3:
    995 	FILE_UNUSE(rf, p);
    996 	ffree(rf);
    997 	fdremove(fdp, retval[0]);
    998 free2:
    999 	(void)soclose(wso);
   1000 free1:
   1001 	(void)soclose(rso);
   1002 	return (error);
   1003 }
   1004 
   1005 /*
   1006  * Get socket name.
   1007  */
   1008 /* ARGSUSED */
   1009 int
   1010 sys_getsockname(p, v, retval)
   1011 	struct proc *p;
   1012 	void *v;
   1013 	register_t *retval;
   1014 {
   1015 	struct sys_getsockname_args /* {
   1016 		syscallarg(int) fdes;
   1017 		syscallarg(struct sockaddr *) asa;
   1018 		syscallarg(unsigned int *) alen;
   1019 	} */ *uap = v;
   1020 	struct file *fp;
   1021 	struct socket *so;
   1022 	struct mbuf *m;
   1023 	unsigned int len;
   1024 	int error;
   1025 
   1026 	/* getsock() will use the descriptor for us */
   1027 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
   1028 		return (error);
   1029 	error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
   1030 	if (error)
   1031 		goto out;
   1032 	so = (struct socket *)fp->f_data;
   1033 	m = m_getclr(M_WAIT, MT_SONAME);
   1034 	error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
   1035 	    m, (struct mbuf *)0, (struct proc *)0);
   1036 	if (error)
   1037 		goto bad;
   1038 	if (len > m->m_len)
   1039 		len = m->m_len;
   1040 	error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
   1041 	if (error == 0)
   1042 		error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen),
   1043 		    sizeof(len));
   1044  bad:
   1045 	m_freem(m);
   1046  out:
   1047 	FILE_UNUSE(fp, p);
   1048 	return (error);
   1049 }
   1050 
   1051 /*
   1052  * Get name of peer for connected socket.
   1053  */
   1054 /* ARGSUSED */
   1055 int
   1056 sys_getpeername(p, v, retval)
   1057 	struct proc *p;
   1058 	void *v;
   1059 	register_t *retval;
   1060 {
   1061 	struct sys_getpeername_args /* {
   1062 		syscallarg(int) fdes;
   1063 		syscallarg(struct sockaddr *) asa;
   1064 		syscallarg(unsigned int *) alen;
   1065 	} */ *uap = v;
   1066 	struct file *fp;
   1067 	struct socket *so;
   1068 	struct mbuf *m;
   1069 	unsigned int len;
   1070 	int error;
   1071 
   1072 	/* getsock() will use the descriptor for us */
   1073 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
   1074 		return (error);
   1075 	so = (struct socket *)fp->f_data;
   1076 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
   1077 		error = ENOTCONN;
   1078 		goto out;
   1079 	}
   1080 	error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
   1081 	if (error)
   1082 		goto out;
   1083 	m = m_getclr(M_WAIT, MT_SONAME);
   1084 	error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
   1085 	    m, (struct mbuf *)0, (struct proc *)0);
   1086 	if (error)
   1087 		goto bad;
   1088 	if (len > m->m_len)
   1089 		len = m->m_len;
   1090 	error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
   1091 	if (error)
   1092 		goto bad;
   1093 	error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen), sizeof(len));
   1094  bad:
   1095 	m_freem(m);
   1096  out:
   1097 	FILE_UNUSE(fp, p);
   1098 	return (error);
   1099 }
   1100 
   1101 /*
   1102  * XXX In a perfect world, we wouldn't pass around socket control
   1103  * XXX arguments in mbufs, and this could go away.
   1104  */
   1105 int
   1106 sockargs(mp, buf, buflen, type)
   1107 	struct mbuf **mp;
   1108 	const void *buf;
   1109 	int buflen, type;
   1110 {
   1111 	struct sockaddr *sa;
   1112 	struct mbuf *m;
   1113 	int error;
   1114 
   1115 	/*
   1116 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1117 	 * will overflow sa_len.
   1118 	 */
   1119 	if (type == MT_SONAME && (u_int)buflen > UCHAR_MAX)
   1120 		return (EINVAL);
   1121 
   1122 	/* Allocate an mbuf to hold the arguments. */
   1123 	m = m_get(M_WAIT, type);
   1124 	if ((u_int)buflen > MLEN) {
   1125 		/*
   1126 		 * Won't fit into a regular mbuf, so we allocate just
   1127 		 * enough external storage to hold the argument.
   1128 		 */
   1129 		MEXTMALLOC(m, buflen, M_WAITOK);
   1130 	}
   1131 	m->m_len = buflen;
   1132 	error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
   1133 	if (error) {
   1134 		(void) m_free(m);
   1135 		return (error);
   1136 	}
   1137 	*mp = m;
   1138 	if (type == MT_SONAME) {
   1139 		sa = mtod(m, struct sockaddr *);
   1140 
   1141 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
   1142 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1143 			sa->sa_family = sa->sa_len;
   1144 #endif
   1145 		sa->sa_len = buflen;
   1146 	}
   1147 	return (0);
   1148 }
   1149 
   1150 int
   1151 getsock(fdp, fdes, fpp)
   1152 	struct filedesc *fdp;
   1153 	int fdes;
   1154 	struct file **fpp;
   1155 {
   1156 	struct file *fp;
   1157 
   1158 	if ((unsigned)fdes >= fdp->fd_nfiles ||
   1159 	    (fp = fdp->fd_ofiles[fdes]) == NULL ||
   1160 	    (fp->f_iflags & FIF_WANTCLOSE) != 0)
   1161 		return (EBADF);
   1162 
   1163 	FILE_USE(fp);
   1164 
   1165 	if (fp->f_type != DTYPE_SOCKET) {
   1166 		FILE_UNUSE(fp, NULL);
   1167 		return (ENOTSOCK);
   1168 	}
   1169 	*fpp = fp;
   1170 	return (0);
   1171 }
   1172