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