Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.60
      1 /*	$NetBSD: uipc_syscalls.c,v 1.60 2001/06/16 12:00:02 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 	FILE_UNUSE(fp, p);
    274 	if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING))
    275 		return (EALREADY);
    276 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    277 	    MT_SONAME);
    278 	if (error)
    279 		return (error);
    280 	error = soconnect(so, nam);
    281 	if (error)
    282 		goto bad;
    283 	if ((so->so_state & SS_NBIO) && (so->so_state & SS_ISCONNECTING)) {
    284 		m_freem(nam);
    285 		return (EINPROGRESS);
    286 	}
    287 	s = splsoftnet();
    288 	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
    289 		error = tsleep((caddr_t)&so->so_timeo, PSOCK | PCATCH,
    290 			       netcon, 0);
    291 		if (error)
    292 			break;
    293 	}
    294 	if (error == 0) {
    295 		error = so->so_error;
    296 		so->so_error = 0;
    297 	}
    298 	splx(s);
    299  bad:
    300 	so->so_state &= ~SS_ISCONNECTING;
    301 	m_freem(nam);
    302 	if (error == ERESTART)
    303 		error = EINTR;
    304 	return (error);
    305 }
    306 
    307 int
    308 sys_socketpair(struct proc *p, void *v, register_t *retval)
    309 {
    310 	struct sys_socketpair_args /* {
    311 		syscallarg(int)		domain;
    312 		syscallarg(int)		type;
    313 		syscallarg(int)		protocol;
    314 		syscallarg(int *)	rsv;
    315 	} */ *uap = v;
    316 	struct filedesc	*fdp;
    317 	struct file	*fp1, *fp2;
    318 	struct socket	*so1, *so2;
    319 	int		fd, error, sv[2];
    320 
    321 	fdp = p->p_fd;
    322 	error = socreate(SCARG(uap, domain), &so1, SCARG(uap, type),
    323 			 SCARG(uap, protocol));
    324 	if (error)
    325 		return (error);
    326 	error = socreate(SCARG(uap, domain), &so2, SCARG(uap, type),
    327 			 SCARG(uap, protocol));
    328 	if (error)
    329 		goto free1;
    330 	/* falloc() will use the descriptor for us */
    331 	if ((error = falloc(p, &fp1, &fd)) != 0)
    332 		goto free2;
    333 	sv[0] = fd;
    334 	fp1->f_flag = FREAD|FWRITE;
    335 	fp1->f_type = DTYPE_SOCKET;
    336 	fp1->f_ops = &socketops;
    337 	fp1->f_data = (caddr_t)so1;
    338 	if ((error = falloc(p, &fp2, &fd)) != 0)
    339 		goto free3;
    340 	fp2->f_flag = FREAD|FWRITE;
    341 	fp2->f_type = DTYPE_SOCKET;
    342 	fp2->f_ops = &socketops;
    343 	fp2->f_data = (caddr_t)so2;
    344 	sv[1] = fd;
    345 	if ((error = soconnect2(so1, so2)) != 0)
    346 		goto free4;
    347 	if (SCARG(uap, type) == SOCK_DGRAM) {
    348 		/*
    349 		 * Datagram socket connection is asymmetric.
    350 		 */
    351 		 if ((error = soconnect2(so2, so1)) != 0)
    352 			goto free4;
    353 	}
    354 	error = copyout((caddr_t)sv, (caddr_t)SCARG(uap, rsv),
    355 	    2 * sizeof(int));
    356 	FILE_SET_MATURE(fp1);
    357 	FILE_SET_MATURE(fp2);
    358 	FILE_UNUSE(fp1, p);
    359 	FILE_UNUSE(fp2, p);
    360 	return (error);
    361  free4:
    362 	FILE_UNUSE(fp2, p);
    363 	ffree(fp2);
    364 	fdremove(fdp, sv[1]);
    365  free3:
    366 	FILE_UNUSE(fp1, p);
    367 	ffree(fp1);
    368 	fdremove(fdp, sv[0]);
    369  free2:
    370 	(void)soclose(so2);
    371  free1:
    372 	(void)soclose(so1);
    373 	return (error);
    374 }
    375 
    376 int
    377 sys_sendto(struct proc *p, void *v, register_t *retval)
    378 {
    379 	struct sys_sendto_args /* {
    380 		syscallarg(int)				s;
    381 		syscallarg(const void *)		buf;
    382 		syscallarg(size_t)			len;
    383 		syscallarg(int)				flags;
    384 		syscallarg(const struct sockaddr *)	to;
    385 		syscallarg(unsigned int)		tolen;
    386 	} */ *uap = v;
    387 	struct msghdr	msg;
    388 	struct iovec	aiov;
    389 
    390 	msg.msg_name = (caddr_t)SCARG(uap, to);		/* XXX kills const */
    391 	msg.msg_namelen = SCARG(uap, tolen);
    392 	msg.msg_iov = &aiov;
    393 	msg.msg_iovlen = 1;
    394 	msg.msg_control = 0;
    395 #ifdef COMPAT_OLDSOCK
    396 	msg.msg_flags = 0;
    397 #endif
    398 	aiov.iov_base = (char *)SCARG(uap, buf);	/* XXX kills const */
    399 	aiov.iov_len = SCARG(uap, len);
    400 	return (sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval));
    401 }
    402 
    403 int
    404 sys_sendmsg(struct proc *p, void *v, register_t *retval)
    405 {
    406 	struct sys_sendmsg_args /* {
    407 		syscallarg(int)				s;
    408 		syscallarg(const struct msghdr *)	msg;
    409 		syscallarg(int)				flags;
    410 	} */ *uap = v;
    411 	struct msghdr	msg;
    412 	struct iovec	aiov[UIO_SMALLIOV], *iov;
    413 	int		error;
    414 
    415 	error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
    416 	if (error)
    417 		return (error);
    418 	if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
    419 		if ((unsigned int)msg.msg_iovlen > IOV_MAX)
    420 			return (EMSGSIZE);
    421 		iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
    422 		    M_IOV, M_WAITOK);
    423 	} else
    424 		iov = aiov;
    425 	if ((unsigned int)msg.msg_iovlen > 0) {
    426 		error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
    427 		    (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
    428 		if (error)
    429 			goto done;
    430 	}
    431 	msg.msg_iov = iov;
    432 #ifdef COMPAT_OLDSOCK
    433 	msg.msg_flags = 0;
    434 #endif
    435 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    436 done:
    437 	if (iov != aiov)
    438 		free(iov, M_IOV);
    439 	return (error);
    440 }
    441 
    442 int
    443 sendit(struct proc *p, int s, struct msghdr *mp, int flags, register_t *retsize)
    444 {
    445 	struct file	*fp;
    446 	struct uio	auio;
    447 	struct iovec	*iov;
    448 	int		i, len, error;
    449 	struct mbuf	*to, *control;
    450 	struct socket	*so;
    451 #ifdef KTRACE
    452 	struct iovec	*ktriov;
    453 #endif
    454 
    455 #ifdef KTRACE
    456 	ktriov = NULL;
    457 #endif
    458 	/* getsock() will use the descriptor for us */
    459 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    460 		return (error);
    461 	auio.uio_iov = mp->msg_iov;
    462 	auio.uio_iovcnt = mp->msg_iovlen;
    463 	auio.uio_segflg = UIO_USERSPACE;
    464 	auio.uio_rw = UIO_WRITE;
    465 	auio.uio_procp = p;
    466 	auio.uio_offset = 0;			/* XXX */
    467 	auio.uio_resid = 0;
    468 	iov = mp->msg_iov;
    469 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    470 #if 0
    471 		/* cannot happen; iov_len is unsigned */
    472 		if (iov->iov_len < 0) {
    473 			error = EINVAL;
    474 			goto out;
    475 		}
    476 #endif
    477 		/*
    478 		 * Writes return ssize_t because -1 is returned on error.
    479 		 * Therefore, we must restrict the length to SSIZE_MAX to
    480 		 * avoid garbage return values.
    481 		 */
    482 		auio.uio_resid += iov->iov_len;
    483 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    484 			error = EINVAL;
    485 			goto out;
    486 		}
    487 	}
    488 	if (mp->msg_name) {
    489 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    490 				 MT_SONAME);
    491 		if (error)
    492 			goto out;
    493 	} else
    494 		to = 0;
    495 	if (mp->msg_control) {
    496 		if (mp->msg_controllen < sizeof(struct cmsghdr)
    497 #ifdef COMPAT_OLDSOCK
    498 		    && mp->msg_flags != MSG_COMPAT
    499 #endif
    500 		) {
    501 			error = EINVAL;
    502 			goto bad;
    503 		}
    504 		error = sockargs(&control, mp->msg_control,
    505 				 mp->msg_controllen, MT_CONTROL);
    506 		if (error)
    507 			goto bad;
    508 #ifdef COMPAT_OLDSOCK
    509 		if (mp->msg_flags == MSG_COMPAT) {
    510 			struct cmsghdr *cm;
    511 
    512 			M_PREPEND(control, sizeof(*cm), M_WAIT);
    513 			if (control == 0) {
    514 				error = ENOBUFS;
    515 				goto bad;
    516 			} else {
    517 				cm = mtod(control, struct cmsghdr *);
    518 				cm->cmsg_len = control->m_len;
    519 				cm->cmsg_level = SOL_SOCKET;
    520 				cm->cmsg_type = SCM_RIGHTS;
    521 			}
    522 		}
    523 #endif
    524 	} else
    525 		control = 0;
    526 #ifdef KTRACE
    527 	if (KTRPOINT(p, KTR_GENIO)) {
    528 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    529 
    530 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    531 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
    532 	}
    533 #endif
    534 	len = auio.uio_resid;
    535 	so = (struct socket *)fp->f_data;
    536 	error = (*so->so_send)(so, to, &auio, NULL, control, flags);
    537 	if (error) {
    538 		if (auio.uio_resid != len && (error == ERESTART ||
    539 		    error == EINTR || error == EWOULDBLOCK))
    540 			error = 0;
    541 		if (error == EPIPE)
    542 			psignal(p, SIGPIPE);
    543 	}
    544 	if (error == 0)
    545 		*retsize = len - auio.uio_resid;
    546 #ifdef KTRACE
    547 	if (ktriov != NULL) {
    548 		if (error == 0)
    549 			ktrgenio(p, s, UIO_WRITE, ktriov, *retsize, error);
    550 		free(ktriov, M_TEMP);
    551 	}
    552 #endif
    553  bad:
    554 	if (to)
    555 		m_freem(to);
    556  out:
    557 	FILE_UNUSE(fp, p);
    558 	return (error);
    559 }
    560 
    561 int
    562 sys_recvfrom(struct proc *p, void *v, register_t *retval)
    563 {
    564 	struct sys_recvfrom_args /* {
    565 		syscallarg(int)			s;
    566 		syscallarg(void *)		buf;
    567 		syscallarg(size_t)		len;
    568 		syscallarg(int)			flags;
    569 		syscallarg(struct sockaddr *)	from;
    570 		syscallarg(unsigned int *)	fromlenaddr;
    571 	} */ *uap = v;
    572 	struct msghdr	msg;
    573 	struct iovec	aiov;
    574 	int		error;
    575 
    576 	if (SCARG(uap, fromlenaddr)) {
    577 		error = copyin((caddr_t)SCARG(uap, fromlenaddr),
    578 			       (caddr_t)&msg.msg_namelen,
    579 			       sizeof(msg.msg_namelen));
    580 		if (error)
    581 			return (error);
    582 	} else
    583 		msg.msg_namelen = 0;
    584 	msg.msg_name = (caddr_t)SCARG(uap, from);
    585 	msg.msg_iov = &aiov;
    586 	msg.msg_iovlen = 1;
    587 	aiov.iov_base = SCARG(uap, buf);
    588 	aiov.iov_len = SCARG(uap, len);
    589 	msg.msg_control = 0;
    590 	msg.msg_flags = SCARG(uap, flags);
    591 	return (recvit(p, SCARG(uap, s), &msg,
    592 		       (caddr_t)SCARG(uap, fromlenaddr), retval));
    593 }
    594 
    595 int
    596 sys_recvmsg(struct proc *p, void *v, register_t *retval)
    597 {
    598 	struct sys_recvmsg_args /* {
    599 		syscallarg(int)			s;
    600 		syscallarg(struct msghdr *)	msg;
    601 		syscallarg(int)			flags;
    602 	} */ *uap = v;
    603 	struct msghdr	msg;
    604 	struct iovec	aiov[UIO_SMALLIOV], *uiov, *iov;
    605 	int		error;
    606 
    607 	error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
    608 		       sizeof(msg));
    609 	if (error)
    610 		return (error);
    611 	if ((unsigned int)msg.msg_iovlen > UIO_SMALLIOV) {
    612 		if ((unsigned int)msg.msg_iovlen > IOV_MAX)
    613 			return (EMSGSIZE);
    614 		iov = malloc(sizeof(struct iovec) * msg.msg_iovlen,
    615 		    M_IOV, M_WAITOK);
    616 	} else
    617 		iov = aiov;
    618 	if ((unsigned int)msg.msg_iovlen > 0) {
    619 		error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
    620 		    (size_t)(msg.msg_iovlen * sizeof(struct iovec)));
    621 		if (error)
    622 			goto done;
    623 	}
    624 	uiov = msg.msg_iov;
    625 	msg.msg_iov = iov;
    626 #ifdef COMPAT_OLDSOCK
    627 	msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
    628 #else
    629 	msg.msg_flags = SCARG(uap, flags);
    630 #endif
    631 	if ((error = recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval)) == 0) {
    632 		msg.msg_iov = uiov;
    633 		error = copyout((caddr_t)&msg, (caddr_t)SCARG(uap, msg),
    634 		    sizeof(msg));
    635 	}
    636 done:
    637 	if (iov != aiov)
    638 		free(iov, M_IOV);
    639 	return (error);
    640 }
    641 
    642 int
    643 recvit(struct proc *p, int s, struct msghdr *mp, caddr_t namelenp,
    644 	register_t *retsize)
    645 {
    646 	struct file	*fp;
    647 	struct uio	auio;
    648 	struct iovec	*iov;
    649 	int		i, len, error;
    650 	struct mbuf	*from, *control;
    651 	struct socket	*so;
    652 #ifdef KTRACE
    653 	struct iovec	*ktriov;
    654 #endif
    655 
    656 	from = 0;
    657 	control = 0;
    658 #ifdef KTRACE
    659 	ktriov = NULL;
    660 #endif
    661 
    662 	/* getsock() will use the descriptor for us */
    663 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    664 		return (error);
    665 	auio.uio_iov = mp->msg_iov;
    666 	auio.uio_iovcnt = mp->msg_iovlen;
    667 	auio.uio_segflg = UIO_USERSPACE;
    668 	auio.uio_rw = UIO_READ;
    669 	auio.uio_procp = p;
    670 	auio.uio_offset = 0;			/* XXX */
    671 	auio.uio_resid = 0;
    672 	iov = mp->msg_iov;
    673 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    674 #if 0
    675 		/* cannot happen iov_len is unsigned */
    676 		if (iov->iov_len < 0) {
    677 			error = EINVAL;
    678 			goto out1;
    679 		}
    680 #endif
    681 		/*
    682 		 * Reads return ssize_t because -1 is returned on error.
    683 		 * Therefore we must restrict the length to SSIZE_MAX to
    684 		 * avoid garbage return values.
    685 		 */
    686 		auio.uio_resid += iov->iov_len;
    687 		if (iov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    688 			error = EINVAL;
    689 			goto out1;
    690 		}
    691 	}
    692 #ifdef KTRACE
    693 	if (KTRPOINT(p, KTR_GENIO)) {
    694 		int iovlen = auio.uio_iovcnt * sizeof(struct iovec);
    695 
    696 		ktriov = malloc(iovlen, M_TEMP, M_WAITOK);
    697 		memcpy((caddr_t)ktriov, (caddr_t)auio.uio_iov, iovlen);
    698 	}
    699 #endif
    700 	len = auio.uio_resid;
    701 	so = (struct socket *)fp->f_data;
    702 	error = (*so->so_receive)(so, &from, &auio, NULL,
    703 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
    704 	if (error) {
    705 		if (auio.uio_resid != len && (error == ERESTART ||
    706 		    error == EINTR || error == EWOULDBLOCK))
    707 			error = 0;
    708 	}
    709 #ifdef KTRACE
    710 	if (ktriov != NULL) {
    711 		if (error == 0)
    712 			ktrgenio(p, s, UIO_READ, ktriov,
    713 			    len - auio.uio_resid, error);
    714 		free(ktriov, M_TEMP);
    715 	}
    716 #endif
    717 	if (error)
    718 		goto out;
    719 	*retsize = len - auio.uio_resid;
    720 	if (mp->msg_name) {
    721 		len = mp->msg_namelen;
    722 		if (len <= 0 || from == 0)
    723 			len = 0;
    724 		else {
    725 #ifdef COMPAT_OLDSOCK
    726 			if (mp->msg_flags & MSG_COMPAT)
    727 				mtod(from, struct osockaddr *)->sa_family =
    728 				    mtod(from, struct sockaddr *)->sa_family;
    729 #endif
    730 			if (len > from->m_len)
    731 				len = from->m_len;
    732 			/* else if len < from->m_len ??? */
    733 			error = copyout(mtod(from, caddr_t),
    734 					(caddr_t)mp->msg_name, (unsigned)len);
    735 			if (error)
    736 				goto out;
    737 		}
    738 		mp->msg_namelen = len;
    739 		if (namelenp &&
    740 		    (error = copyout((caddr_t)&len, namelenp, sizeof(int)))) {
    741 #ifdef COMPAT_OLDSOCK
    742 			if (mp->msg_flags & MSG_COMPAT)
    743 				error = 0;	/* old recvfrom didn't check */
    744 			else
    745 #endif
    746 			goto out;
    747 		}
    748 	}
    749 	if (mp->msg_control) {
    750 #ifdef COMPAT_OLDSOCK
    751 		/*
    752 		 * We assume that old recvmsg calls won't receive access
    753 		 * rights and other control info, esp. as control info
    754 		 * is always optional and those options didn't exist in 4.3.
    755 		 * If we receive rights, trim the cmsghdr; anything else
    756 		 * is tossed.
    757 		 */
    758 		if (control && mp->msg_flags & MSG_COMPAT) {
    759 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
    760 			    SOL_SOCKET ||
    761 			    mtod(control, struct cmsghdr *)->cmsg_type !=
    762 			    SCM_RIGHTS) {
    763 				mp->msg_controllen = 0;
    764 				goto out;
    765 			}
    766 			control->m_len -= sizeof(struct cmsghdr);
    767 			control->m_data += sizeof(struct cmsghdr);
    768 		}
    769 #endif
    770 		len = mp->msg_controllen;
    771 		if (len <= 0 || control == 0)
    772 			len = 0;
    773 		else {
    774 			struct mbuf *m = control;
    775 			caddr_t p = (caddr_t)mp->msg_control;
    776 
    777 			do {
    778 				i = m->m_len;
    779 				if (len < i) {
    780 					mp->msg_flags |= MSG_CTRUNC;
    781 					i = len;
    782 				}
    783 				error = copyout(mtod(m, caddr_t), p,
    784 				    (unsigned)i);
    785 				if (m->m_next)
    786 					i = ALIGN(i);
    787 				p += i;
    788 				len -= i;
    789 				if (error != 0 || len <= 0)
    790 					break;
    791 			} while ((m = m->m_next) != NULL);
    792 			len = p - (caddr_t)mp->msg_control;
    793 		}
    794 		mp->msg_controllen = len;
    795 	}
    796  out:
    797 	if (from)
    798 		m_freem(from);
    799 	if (control)
    800 		m_freem(control);
    801  out1:
    802 	FILE_UNUSE(fp, p);
    803 	return (error);
    804 }
    805 
    806 /* ARGSUSED */
    807 int
    808 sys_shutdown(struct proc *p, void *v, register_t *retval)
    809 {
    810 	struct sys_shutdown_args /* {
    811 		syscallarg(int)	s;
    812 		syscallarg(int)	how;
    813 	} */ *uap = v;
    814 	struct file	*fp;
    815 	int		error;
    816 
    817 	/* getsock() will use the descriptor for us */
    818 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    819 		return (error);
    820 	error = soshutdown((struct socket *)fp->f_data, SCARG(uap, how));
    821 	FILE_UNUSE(fp, p);
    822 	return (error);
    823 }
    824 
    825 /* ARGSUSED */
    826 int
    827 sys_setsockopt(struct proc *p, void *v, register_t *retval)
    828 {
    829 	struct sys_setsockopt_args /* {
    830 		syscallarg(int)			s;
    831 		syscallarg(int)			level;
    832 		syscallarg(int)			name;
    833 		syscallarg(const void *)	val;
    834 		syscallarg(unsigned int)	valsize;
    835 	} */ *uap = v;
    836 	struct file	*fp;
    837 	struct mbuf	*m;
    838 	int		error;
    839 
    840 	m = NULL;
    841 	/* getsock() will use the descriptor for us */
    842 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    843 		return (error);
    844 	if (SCARG(uap, valsize) > MLEN) {
    845 		error = EINVAL;
    846 		goto out;
    847 	}
    848 	if (SCARG(uap, val)) {
    849 		m = m_get(M_WAIT, MT_SOOPTS);
    850 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
    851 			       SCARG(uap, valsize));
    852 		if (error) {
    853 			(void) m_free(m);
    854 			goto out;
    855 		}
    856 		m->m_len = SCARG(uap, valsize);
    857 	}
    858 	error = sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    859 			 SCARG(uap, name), m);
    860  out:
    861 	FILE_UNUSE(fp, p);
    862 	return (error);
    863 }
    864 
    865 /* ARGSUSED */
    866 int
    867 sys_getsockopt(struct proc *p, void *v, register_t *retval)
    868 {
    869 	struct sys_getsockopt_args /* {
    870 		syscallarg(int)			s;
    871 		syscallarg(int)			level;
    872 		syscallarg(int)			name;
    873 		syscallarg(void *)		val;
    874 		syscallarg(unsigned int *)	avalsize;
    875 	} */ *uap = v;
    876 	struct file	*fp;
    877 	struct mbuf	*m, *m0;
    878 	unsigned int	op, i, valsize;
    879 	int		error;
    880 
    881 	m = NULL;
    882 	/* getsock() will use the descriptor for us */
    883 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    884 		return (error);
    885 	if (SCARG(uap, val)) {
    886 		error = copyin((caddr_t)SCARG(uap, avalsize),
    887 			       (caddr_t)&valsize, sizeof(valsize));
    888 		if (error)
    889 			goto out;
    890 	} else
    891 		valsize = 0;
    892 	if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
    893 	    SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
    894 	    m != NULL) {
    895 		op = 0;
    896 		while (m && !error && op < valsize) {
    897 			i = min(m->m_len, (valsize - op));
    898 			error = copyout(mtod(m, caddr_t), SCARG(uap, val), i);
    899 			op += i;
    900 			SCARG(uap, val) = ((u_int8_t *)SCARG(uap, val)) + i;
    901 			m0 = m;
    902 			MFREE(m0, m);
    903 		}
    904 		valsize = op;
    905 		if (error == 0)
    906 			error = copyout(&valsize,
    907 					SCARG(uap, avalsize), sizeof(valsize));
    908 	}
    909 	if (m != NULL)
    910 		(void) m_free(m);
    911  out:
    912 	FILE_UNUSE(fp, p);
    913 	return (error);
    914 }
    915 
    916 #ifndef NEW_PIPE
    917 /* ARGSUSED */
    918 int
    919 sys_pipe(struct proc *p, void *v, register_t *retval)
    920 {
    921 	struct filedesc	*fdp;
    922 	struct file	*rf, *wf;
    923 	struct socket	*rso, *wso;
    924 	int		fd, error;
    925 
    926 	fdp = p->p_fd;
    927 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
    928 		return (error);
    929 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
    930 		goto free1;
    931 	/* remember this socket pair implements a pipe */
    932 	wso->so_state |= SS_ISAPIPE;
    933 	rso->so_state |= SS_ISAPIPE;
    934 	/* falloc() will use the descriptor for us */
    935 	if ((error = falloc(p, &rf, &fd)) != 0)
    936 		goto free2;
    937 	retval[0] = fd;
    938 	rf->f_flag = FREAD;
    939 	rf->f_type = DTYPE_SOCKET;
    940 	rf->f_ops = &socketops;
    941 	rf->f_data = (caddr_t)rso;
    942 	if ((error = falloc(p, &wf, &fd)) != 0)
    943 		goto free3;
    944 	wf->f_flag = FWRITE;
    945 	wf->f_type = DTYPE_SOCKET;
    946 	wf->f_ops = &socketops;
    947 	wf->f_data = (caddr_t)wso;
    948 	retval[1] = fd;
    949 	if ((error = unp_connect2(wso, rso)) != 0)
    950 		goto free4;
    951 	FILE_SET_MATURE(rf);
    952 	FILE_SET_MATURE(wf);
    953 	FILE_UNUSE(rf, p);
    954 	FILE_UNUSE(wf, p);
    955 	return (0);
    956  free4:
    957 	FILE_UNUSE(wf, p);
    958 	ffree(wf);
    959 	fdremove(fdp, retval[1]);
    960  free3:
    961 	FILE_UNUSE(rf, p);
    962 	ffree(rf);
    963 	fdremove(fdp, retval[0]);
    964  free2:
    965 	(void)soclose(wso);
    966  free1:
    967 	(void)soclose(rso);
    968 	return (error);
    969 }
    970 #endif /* !NEW_PIPE */
    971 
    972 /*
    973  * Get socket name.
    974  */
    975 /* ARGSUSED */
    976 int
    977 sys_getsockname(struct proc *p, void *v, register_t *retval)
    978 {
    979 	struct sys_getsockname_args /* {
    980 		syscallarg(int)			fdes;
    981 		syscallarg(struct sockaddr *)	asa;
    982 		syscallarg(unsigned int *)	alen;
    983 	} */ *uap = v;
    984 	struct file	*fp;
    985 	struct socket	*so;
    986 	struct mbuf	*m;
    987 	unsigned int	len;
    988 	int		error;
    989 
    990 	/* getsock() will use the descriptor for us */
    991 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
    992 		return (error);
    993 	error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
    994 	if (error)
    995 		goto out;
    996 	so = (struct socket *)fp->f_data;
    997 	m = m_getclr(M_WAIT, MT_SONAME);
    998 	error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
    999 	    m, (struct mbuf *)0, (struct proc *)0);
   1000 	if (error)
   1001 		goto bad;
   1002 	if (len > m->m_len)
   1003 		len = m->m_len;
   1004 	error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
   1005 	if (error == 0)
   1006 		error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen),
   1007 		    sizeof(len));
   1008  bad:
   1009 	m_freem(m);
   1010  out:
   1011 	FILE_UNUSE(fp, p);
   1012 	return (error);
   1013 }
   1014 
   1015 /*
   1016  * Get name of peer for connected socket.
   1017  */
   1018 /* ARGSUSED */
   1019 int
   1020 sys_getpeername(struct proc *p, void *v, register_t *retval)
   1021 {
   1022 	struct sys_getpeername_args /* {
   1023 		syscallarg(int)			fdes;
   1024 		syscallarg(struct sockaddr *)	asa;
   1025 		syscallarg(unsigned int *)	alen;
   1026 	} */ *uap = v;
   1027 	struct file	*fp;
   1028 	struct socket	*so;
   1029 	struct mbuf	*m;
   1030 	unsigned int	len;
   1031 	int		error;
   1032 
   1033 	/* getsock() will use the descriptor for us */
   1034 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
   1035 		return (error);
   1036 	so = (struct socket *)fp->f_data;
   1037 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0) {
   1038 		error = ENOTCONN;
   1039 		goto out;
   1040 	}
   1041 	error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof(len));
   1042 	if (error)
   1043 		goto out;
   1044 	m = m_getclr(M_WAIT, MT_SONAME);
   1045 	error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
   1046 	    m, (struct mbuf *)0, (struct proc *)0);
   1047 	if (error)
   1048 		goto bad;
   1049 	if (len > m->m_len)
   1050 		len = m->m_len;
   1051 	error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), len);
   1052 	if (error)
   1053 		goto bad;
   1054 	error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen), sizeof(len));
   1055  bad:
   1056 	m_freem(m);
   1057  out:
   1058 	FILE_UNUSE(fp, p);
   1059 	return (error);
   1060 }
   1061 
   1062 /*
   1063  * XXX In a perfect world, we wouldn't pass around socket control
   1064  * XXX arguments in mbufs, and this could go away.
   1065  */
   1066 int
   1067 sockargs(struct mbuf **mp, const void *buf, int buflen, int type)
   1068 {
   1069 	struct sockaddr	*sa;
   1070 	struct mbuf	*m;
   1071 	int		error;
   1072 
   1073 	/*
   1074 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1075 	 * will overflow sa_len.
   1076 	 */
   1077 	if (type == MT_SONAME && (u_int)buflen > UCHAR_MAX)
   1078 		return (EINVAL);
   1079 
   1080 	/* Allocate an mbuf to hold the arguments. */
   1081 	m = m_get(M_WAIT, type);
   1082 	if ((u_int)buflen > MLEN) {
   1083 		/*
   1084 		 * Won't fit into a regular mbuf, so we allocate just
   1085 		 * enough external storage to hold the argument.
   1086 		 */
   1087 		MEXTMALLOC(m, buflen, M_WAITOK);
   1088 	}
   1089 	m->m_len = buflen;
   1090 	error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
   1091 	if (error) {
   1092 		(void) m_free(m);
   1093 		return (error);
   1094 	}
   1095 	*mp = m;
   1096 	if (type == MT_SONAME) {
   1097 		sa = mtod(m, struct sockaddr *);
   1098 
   1099 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
   1100 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1101 			sa->sa_family = sa->sa_len;
   1102 #endif
   1103 		sa->sa_len = buflen;
   1104 	}
   1105 	return (0);
   1106 }
   1107 
   1108 int
   1109 getsock(struct filedesc *fdp, int fdes, struct file **fpp)
   1110 {
   1111 	struct file	*fp;
   1112 
   1113 	if ((fp = fd_getfile(fdp, fdes)) == NULL)
   1114 		return (EBADF);
   1115 
   1116 	FILE_USE(fp);
   1117 
   1118 	if (fp->f_type != DTYPE_SOCKET) {
   1119 		FILE_UNUSE(fp, NULL);
   1120 		return (ENOTSOCK);
   1121 	}
   1122 	*fpp = fp;
   1123 	return (0);
   1124 }
   1125