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