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