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