Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.32
      1 /*	$NetBSD: uipc_syscalls.c,v 1.32 1998/07/18 05:04:37 lukem 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 ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
    390 		if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
    391 			return (EMSGSIZE);
    392 		MALLOC(iov, struct iovec *,
    393 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
    394 		       M_WAITOK);
    395 	} else
    396 		iov = aiov;
    397 	if (msg.msg_iovlen &&
    398 	    (error = copyin((caddr_t)msg.msg_iov, (caddr_t)iov,
    399 	    (unsigned)(msg.msg_iovlen * sizeof (struct iovec)))))
    400 		goto done;
    401 	msg.msg_iov = iov;
    402 #ifdef COMPAT_OLDSOCK
    403 	msg.msg_flags = 0;
    404 #endif
    405 	error = sendit(p, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    406 done:
    407 	if (iov != aiov)
    408 		FREE(iov, M_IOV);
    409 	return (error);
    410 }
    411 
    412 int
    413 sendit(p, s, mp, flags, retsize)
    414 	register struct proc *p;
    415 	int s;
    416 	register struct msghdr *mp;
    417 	int flags;
    418 	register_t *retsize;
    419 {
    420 	struct file *fp;
    421 	struct uio auio;
    422 	register struct iovec *iov;
    423 	register int i;
    424 	struct mbuf *to, *control;
    425 	int len, error;
    426 	struct socket *so;
    427 #ifdef KTRACE
    428 	struct iovec *ktriov = NULL;
    429 #endif
    430 
    431 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    432 		return (error);
    433 	auio.uio_iov = mp->msg_iov;
    434 	auio.uio_iovcnt = mp->msg_iovlen;
    435 	auio.uio_segflg = UIO_USERSPACE;
    436 	auio.uio_rw = UIO_WRITE;
    437 	auio.uio_procp = p;
    438 	auio.uio_offset = 0;			/* XXX */
    439 	auio.uio_resid = 0;
    440 	iov = mp->msg_iov;
    441 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    442 #if 0
    443 		/* cannot happen; iov_len is unsigned */
    444 		if (iov->iov_len < 0)
    445 			return (EINVAL);
    446 #endif
    447 		if ((auio.uio_resid += iov->iov_len) < 0)
    448 			return (EINVAL);
    449 	}
    450 	if (mp->msg_name) {
    451 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    452 				 MT_SONAME);
    453 		if (error)
    454 			return (error);
    455 	} else
    456 		to = 0;
    457 	if (mp->msg_control) {
    458 		if (mp->msg_controllen < sizeof(struct cmsghdr)
    459 #ifdef COMPAT_OLDSOCK
    460 		    && mp->msg_flags != MSG_COMPAT
    461 #endif
    462 		) {
    463 			error = EINVAL;
    464 			goto bad;
    465 		}
    466 		error = sockargs(&control, mp->msg_control,
    467 				 mp->msg_controllen, MT_CONTROL);
    468 		if (error)
    469 			goto bad;
    470 #ifdef COMPAT_OLDSOCK
    471 		if (mp->msg_flags == MSG_COMPAT) {
    472 			register struct cmsghdr *cm;
    473 
    474 			M_PREPEND(control, sizeof(*cm), M_WAIT);
    475 			if (control == 0) {
    476 				error = ENOBUFS;
    477 				goto bad;
    478 			} else {
    479 				cm = mtod(control, struct cmsghdr *);
    480 				cm->cmsg_len = control->m_len;
    481 				cm->cmsg_level = SOL_SOCKET;
    482 				cm->cmsg_type = SCM_RIGHTS;
    483 			}
    484 		}
    485 #endif
    486 	} else
    487 		control = 0;
    488 #ifdef KTRACE
    489 	if (KTRPOINT(p, KTR_GENIO)) {
    490 		int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
    491 
    492 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
    493 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
    494 	}
    495 #endif
    496 	len = auio.uio_resid;
    497 	so = (struct socket *)fp->f_data;
    498 	error = (*so->so_send)(so, to, &auio, NULL, control, flags);
    499 	if (error) {
    500 		if (auio.uio_resid != len && (error == ERESTART ||
    501 		    error == EINTR || error == EWOULDBLOCK))
    502 			error = 0;
    503 		if (error == EPIPE)
    504 			psignal(p, SIGPIPE);
    505 	}
    506 	if (error == 0)
    507 		*retsize = len - auio.uio_resid;
    508 #ifdef KTRACE
    509 	if (ktriov != NULL) {
    510 		if (error == 0)
    511 			ktrgenio(p->p_tracep, s, UIO_WRITE,
    512 				ktriov, *retsize, error);
    513 		FREE(ktriov, M_TEMP);
    514 	}
    515 #endif
    516 bad:
    517 	if (to)
    518 		m_freem(to);
    519 	return (error);
    520 }
    521 
    522 int
    523 sys_recvfrom(p, v, retval)
    524 	struct proc *p;
    525 	void *v;
    526 	register_t *retval;
    527 {
    528 	register struct sys_recvfrom_args /* {
    529 		syscallarg(int) s;
    530 		syscallarg(void *) buf;
    531 		syscallarg(size_t) len;
    532 		syscallarg(int) flags;
    533 		syscallarg(struct sockaddr *) from;
    534 		syscallarg(int *) fromlenaddr;
    535 	} */ *uap = v;
    536 	struct msghdr msg;
    537 	struct iovec aiov;
    538 	int error;
    539 
    540 	if (SCARG(uap, fromlenaddr)) {
    541 		error = copyin((caddr_t)SCARG(uap, fromlenaddr),
    542 			       (caddr_t)&msg.msg_namelen,
    543 			       sizeof (msg.msg_namelen));
    544 		if (error)
    545 			return (error);
    546 	} else
    547 		msg.msg_namelen = 0;
    548 	msg.msg_name = (caddr_t)SCARG(uap, from);
    549 	msg.msg_iov = &aiov;
    550 	msg.msg_iovlen = 1;
    551 	aiov.iov_base = SCARG(uap, buf);
    552 	aiov.iov_len = SCARG(uap, len);
    553 	msg.msg_control = 0;
    554 	msg.msg_flags = SCARG(uap, flags);
    555 	return (recvit(p, SCARG(uap, s), &msg,
    556 		       (caddr_t)SCARG(uap, fromlenaddr), retval));
    557 }
    558 
    559 int
    560 sys_recvmsg(p, v, retval)
    561 	struct proc *p;
    562 	void *v;
    563 	register_t *retval;
    564 {
    565 	register struct sys_recvmsg_args /* {
    566 		syscallarg(int) s;
    567 		syscallarg(struct msghdr *) msg;
    568 		syscallarg(int) flags;
    569 	} */ *uap = v;
    570 	struct msghdr msg;
    571 	struct iovec aiov[UIO_SMALLIOV], *uiov, *iov;
    572 	register int error;
    573 
    574 	error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
    575 		       sizeof (msg));
    576 	if (error)
    577 		return (error);
    578 	if ((u_int)msg.msg_iovlen >= UIO_SMALLIOV) {
    579 		if ((u_int)msg.msg_iovlen >= UIO_MAXIOV)
    580 			return (EMSGSIZE);
    581 		MALLOC(iov, struct iovec *,
    582 		       sizeof(struct iovec) * (u_int)msg.msg_iovlen, M_IOV,
    583 		       M_WAITOK);
    584 	} else
    585 		iov = aiov;
    586 #ifdef COMPAT_OLDSOCK
    587 	msg.msg_flags = SCARG(uap, flags) &~ MSG_COMPAT;
    588 #else
    589 	msg.msg_flags = SCARG(uap, flags);
    590 #endif
    591 	uiov = msg.msg_iov;
    592 	msg.msg_iov = iov;
    593 	error = copyin((caddr_t)uiov, (caddr_t)iov,
    594 		       (unsigned)(msg.msg_iovlen * sizeof (struct iovec)));
    595 	if (error)
    596 		goto done;
    597 	if ((error = recvit(p, SCARG(uap, s), &msg, (caddr_t)0, retval)) == 0) {
    598 		msg.msg_iov = uiov;
    599 		error = copyout((caddr_t)&msg, (caddr_t)SCARG(uap, msg),
    600 		    sizeof(msg));
    601 	}
    602 done:
    603 	if (iov != aiov)
    604 		FREE(iov, M_IOV);
    605 	return (error);
    606 }
    607 
    608 int
    609 recvit(p, s, mp, namelenp, retsize)
    610 	register struct proc *p;
    611 	int s;
    612 	register struct msghdr *mp;
    613 	caddr_t namelenp;
    614 	register_t *retsize;
    615 {
    616 	struct file *fp;
    617 	struct uio auio;
    618 	register struct iovec *iov;
    619 	register int i;
    620 	int len, error;
    621 	struct mbuf *from = 0, *control = 0;
    622 	struct socket *so;
    623 #ifdef KTRACE
    624 	struct iovec *ktriov = NULL;
    625 #endif
    626 
    627 	if ((error = getsock(p->p_fd, s, &fp)) != 0)
    628 		return (error);
    629 	auio.uio_iov = mp->msg_iov;
    630 	auio.uio_iovcnt = mp->msg_iovlen;
    631 	auio.uio_segflg = UIO_USERSPACE;
    632 	auio.uio_rw = UIO_READ;
    633 	auio.uio_procp = p;
    634 	auio.uio_offset = 0;			/* XXX */
    635 	auio.uio_resid = 0;
    636 	iov = mp->msg_iov;
    637 	for (i = 0; i < mp->msg_iovlen; i++, iov++) {
    638 #if 0
    639 		/* cannot happen iov_len is unsigned */
    640 		if (iov->iov_len < 0)
    641 			return (EINVAL);
    642 #endif
    643 		if ((auio.uio_resid += iov->iov_len) < 0)
    644 			return (EINVAL);
    645 	}
    646 #ifdef KTRACE
    647 	if (KTRPOINT(p, KTR_GENIO)) {
    648 		int iovlen = auio.uio_iovcnt * sizeof (struct iovec);
    649 
    650 		MALLOC(ktriov, struct iovec *, iovlen, M_TEMP, M_WAITOK);
    651 		bcopy((caddr_t)auio.uio_iov, (caddr_t)ktriov, iovlen);
    652 	}
    653 #endif
    654 	len = auio.uio_resid;
    655 	so = (struct socket *)fp->f_data;
    656 	error = (*so->so_receive)(so, &from, &auio, NULL,
    657 			  mp->msg_control ? &control : NULL, &mp->msg_flags);
    658 	if (error) {
    659 		if (auio.uio_resid != len && (error == ERESTART ||
    660 		    error == EINTR || error == EWOULDBLOCK))
    661 			error = 0;
    662 	}
    663 #ifdef KTRACE
    664 	if (ktriov != NULL) {
    665 		if (error == 0)
    666 			ktrgenio(p->p_tracep, s, UIO_READ,
    667 				ktriov, len - auio.uio_resid, error);
    668 		FREE(ktriov, M_TEMP);
    669 	}
    670 #endif
    671 	if (error)
    672 		goto out;
    673 	*retsize = len - auio.uio_resid;
    674 	if (mp->msg_name) {
    675 		len = mp->msg_namelen;
    676 		if (len <= 0 || from == 0)
    677 			len = 0;
    678 		else {
    679 #ifdef COMPAT_OLDSOCK
    680 			if (mp->msg_flags & MSG_COMPAT)
    681 				mtod(from, struct osockaddr *)->sa_family =
    682 				    mtod(from, struct sockaddr *)->sa_family;
    683 #endif
    684 			if (len > from->m_len)
    685 				len = from->m_len;
    686 			/* else if len < from->m_len ??? */
    687 			error = copyout(mtod(from, caddr_t),
    688 					(caddr_t)mp->msg_name, (unsigned)len);
    689 			if (error)
    690 				goto out;
    691 		}
    692 		mp->msg_namelen = len;
    693 		if (namelenp &&
    694 		    (error = copyout((caddr_t)&len, namelenp, sizeof (int)))) {
    695 #ifdef COMPAT_OLDSOCK
    696 			if (mp->msg_flags & MSG_COMPAT)
    697 				error = 0;	/* old recvfrom didn't check */
    698 			else
    699 #endif
    700 			goto out;
    701 		}
    702 	}
    703 	if (mp->msg_control) {
    704 #ifdef COMPAT_OLDSOCK
    705 		/*
    706 		 * We assume that old recvmsg calls won't receive access
    707 		 * rights and other control info, esp. as control info
    708 		 * is always optional and those options didn't exist in 4.3.
    709 		 * If we receive rights, trim the cmsghdr; anything else
    710 		 * is tossed.
    711 		 */
    712 		if (control && mp->msg_flags & MSG_COMPAT) {
    713 			if (mtod(control, struct cmsghdr *)->cmsg_level !=
    714 			    SOL_SOCKET ||
    715 			    mtod(control, struct cmsghdr *)->cmsg_type !=
    716 			    SCM_RIGHTS) {
    717 				mp->msg_controllen = 0;
    718 				goto out;
    719 			}
    720 			control->m_len -= sizeof (struct cmsghdr);
    721 			control->m_data += sizeof (struct cmsghdr);
    722 		}
    723 #endif
    724 		len = mp->msg_controllen;
    725 		if (len <= 0 || control == 0)
    726 			len = 0;
    727 		else {
    728 			struct mbuf *m = control;
    729 			caddr_t p = (caddr_t)mp->msg_control;
    730 
    731 			do {
    732 				i = m->m_len;
    733 				if (len < i) {
    734 					mp->msg_flags |= MSG_CTRUNC;
    735 					i = len;
    736 				}
    737 				error = copyout(mtod(m, caddr_t), p,
    738 				    (unsigned)i);
    739 				if (m->m_next)
    740 					i = ALIGN(i);
    741 				p += i;
    742 				len -= i;
    743 				if (error != 0 || len <= 0)
    744 					break;
    745 			} while ((m = m->m_next) != NULL);
    746 			len = p - (caddr_t)mp->msg_control;
    747 		}
    748 		mp->msg_controllen = len;
    749 	}
    750 out:
    751 	if (from)
    752 		m_freem(from);
    753 	if (control)
    754 		m_freem(control);
    755 	return (error);
    756 }
    757 
    758 /* ARGSUSED */
    759 int
    760 sys_shutdown(p, v, retval)
    761 	struct proc *p;
    762 	void *v;
    763 	register_t *retval;
    764 {
    765 	register struct sys_shutdown_args /* {
    766 		syscallarg(int) s;
    767 		syscallarg(int) how;
    768 	} */ *uap = v;
    769 	struct file *fp;
    770 	int error;
    771 
    772 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    773 		return (error);
    774 	return (soshutdown((struct socket *)fp->f_data, SCARG(uap, how)));
    775 }
    776 
    777 /* ARGSUSED */
    778 int
    779 sys_setsockopt(p, v, retval)
    780 	struct proc *p;
    781 	void *v;
    782 	register_t *retval;
    783 {
    784 	register struct sys_setsockopt_args /* {
    785 		syscallarg(int) s;
    786 		syscallarg(int) level;
    787 		syscallarg(int) name;
    788 		syscallarg(const void *) val;
    789 		syscallarg(int) valsize;
    790 	} */ *uap = v;
    791 	struct file *fp;
    792 	struct mbuf *m = NULL;
    793 	int error;
    794 
    795 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    796 		return (error);
    797 	if (SCARG(uap, valsize) > MLEN)
    798 		return (EINVAL);
    799 	if (SCARG(uap, val)) {
    800 		m = m_get(M_WAIT, MT_SOOPTS);
    801 		error = copyin(SCARG(uap, val), mtod(m, caddr_t),
    802 			       (u_int)SCARG(uap, valsize));
    803 		if (error) {
    804 			(void) m_free(m);
    805 			return (error);
    806 		}
    807 		m->m_len = SCARG(uap, valsize);
    808 	}
    809 	return (sosetopt((struct socket *)fp->f_data, SCARG(uap, level),
    810 			 SCARG(uap, name), m));
    811 }
    812 
    813 /* ARGSUSED */
    814 int
    815 sys_getsockopt(p, v, retval)
    816 	struct proc *p;
    817 	void *v;
    818 	register_t *retval;
    819 {
    820 	register struct sys_getsockopt_args /* {
    821 		syscallarg(int) s;
    822 		syscallarg(int) level;
    823 		syscallarg(int) name;
    824 		syscallarg(void *) val;
    825 		syscallarg(int *) avalsize;
    826 	} */ *uap = v;
    827 	struct file *fp;
    828 	struct mbuf *m = NULL;
    829 	int valsize, error;
    830 
    831 	if ((error = getsock(p->p_fd, SCARG(uap, s), &fp)) != 0)
    832 		return (error);
    833 	if (SCARG(uap, val)) {
    834 		error = copyin((caddr_t)SCARG(uap, avalsize),
    835 			       (caddr_t)&valsize, sizeof (valsize));
    836 		if (error)
    837 			return (error);
    838 	} else
    839 		valsize = 0;
    840 	if ((error = sogetopt((struct socket *)fp->f_data, SCARG(uap, level),
    841 	    SCARG(uap, name), &m)) == 0 && SCARG(uap, val) && valsize &&
    842 	    m != NULL) {
    843 		if (valsize > m->m_len)
    844 			valsize = m->m_len;
    845 		error = copyout(mtod(m, caddr_t), SCARG(uap, val),
    846 		    (u_int)valsize);
    847 		if (error == 0)
    848 			error = copyout((caddr_t)&valsize,
    849 			    (caddr_t)SCARG(uap, avalsize), sizeof (valsize));
    850 	}
    851 	if (m != NULL)
    852 		(void) m_free(m);
    853 	return (error);
    854 }
    855 
    856 /* ARGSUSED */
    857 int
    858 sys_pipe(p, v, retval)
    859 	struct proc *p;
    860 	void *v;
    861 	register_t *retval;
    862 {
    863 	register struct filedesc *fdp = p->p_fd;
    864 	struct file *rf, *wf;
    865 	struct socket *rso, *wso;
    866 	int fd, error;
    867 
    868 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0)) != 0)
    869 		return (error);
    870 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0)) != 0)
    871 		goto free1;
    872 	if ((error = falloc(p, &rf, &fd)) != 0)
    873 		goto free2;
    874 	retval[0] = fd;
    875 	rf->f_flag = FREAD;
    876 	rf->f_type = DTYPE_SOCKET;
    877 	rf->f_ops = &socketops;
    878 	rf->f_data = (caddr_t)rso;
    879 	if ((error = falloc(p, &wf, &fd)) != 0)
    880 		goto free3;
    881 	wf->f_flag = FWRITE;
    882 	wf->f_type = DTYPE_SOCKET;
    883 	wf->f_ops = &socketops;
    884 	wf->f_data = (caddr_t)wso;
    885 	retval[1] = fd;
    886 	if ((error = unp_connect2(wso, rso)) != 0)
    887 		goto free4;
    888 	return (0);
    889 free4:
    890 	ffree(wf);
    891 	fdp->fd_ofiles[retval[1]] = 0;
    892 free3:
    893 	ffree(rf);
    894 	fdp->fd_ofiles[retval[0]] = 0;
    895 free2:
    896 	(void)soclose(wso);
    897 free1:
    898 	(void)soclose(rso);
    899 	return (error);
    900 }
    901 
    902 /*
    903  * Get socket name.
    904  */
    905 /* ARGSUSED */
    906 int
    907 sys_getsockname(p, v, retval)
    908 	struct proc *p;
    909 	void *v;
    910 	register_t *retval;
    911 {
    912 	register struct sys_getsockname_args /* {
    913 		syscallarg(int) fdes;
    914 		syscallarg(struct sockaddr *) asa;
    915 		syscallarg(int *) alen;
    916 	} */ *uap = v;
    917 	struct file *fp;
    918 	register struct socket *so;
    919 	struct mbuf *m;
    920 	int len, error;
    921 
    922 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
    923 		return (error);
    924 	error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof (len));
    925 	if (error)
    926 		return (error);
    927 	so = (struct socket *)fp->f_data;
    928 	m = m_getclr(M_WAIT, MT_SONAME);
    929 	error = (*so->so_proto->pr_usrreq)(so, PRU_SOCKADDR, (struct mbuf *)0,
    930 	    m, (struct mbuf *)0, (struct proc *)0);
    931 	if (error)
    932 		goto bad;
    933 	if (len > m->m_len)
    934 		len = m->m_len;
    935 	error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), (u_int)len);
    936 	if (error == 0)
    937 		error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen),
    938 		    sizeof (len));
    939 bad:
    940 	m_freem(m);
    941 	return (error);
    942 }
    943 
    944 /*
    945  * Get name of peer for connected socket.
    946  */
    947 /* ARGSUSED */
    948 int
    949 sys_getpeername(p, v, retval)
    950 	struct proc *p;
    951 	void *v;
    952 	register_t *retval;
    953 {
    954 	register struct sys_getpeername_args /* {
    955 		syscallarg(int) fdes;
    956 		syscallarg(struct sockaddr *) asa;
    957 		syscallarg(int *) alen;
    958 	} */ *uap = v;
    959 	struct file *fp;
    960 	register struct socket *so;
    961 	struct mbuf *m;
    962 	int len, error;
    963 
    964 	if ((error = getsock(p->p_fd, SCARG(uap, fdes), &fp)) != 0)
    965 		return (error);
    966 	so = (struct socket *)fp->f_data;
    967 	if ((so->so_state & (SS_ISCONNECTED|SS_ISCONFIRMING)) == 0)
    968 		return (ENOTCONN);
    969 	error = copyin((caddr_t)SCARG(uap, alen), (caddr_t)&len, sizeof (len));
    970 	if (error)
    971 		return (error);
    972 	m = m_getclr(M_WAIT, MT_SONAME);
    973 	error = (*so->so_proto->pr_usrreq)(so, PRU_PEERADDR, (struct mbuf *)0,
    974 	    m, (struct mbuf *)0, (struct proc *)0);
    975 	if (error)
    976 		goto bad;
    977 	if (len > m->m_len)
    978 		len = m->m_len;
    979 	error = copyout(mtod(m, caddr_t), (caddr_t)SCARG(uap, asa), (u_int)len);
    980 	if (error)
    981 		goto bad;
    982 	error = copyout((caddr_t)&len, (caddr_t)SCARG(uap, alen), sizeof (len));
    983 bad:
    984 	m_freem(m);
    985 	return (error);
    986 }
    987 
    988 /*
    989  * XXX In a perfect world, we wouldn't pass around socket control
    990  * XXX arguments in mbufs, and this could go away.
    991  */
    992 int
    993 sockargs(mp, buf, buflen, type)
    994 	struct mbuf **mp;
    995 	const void *buf;
    996 	int buflen, type;
    997 {
    998 	register struct sockaddr *sa;
    999 	register struct mbuf *m;
   1000 	int error;
   1001 
   1002 	/*
   1003 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1004 	 * will overflow sa_len.
   1005 	 */
   1006 	if (type == MT_SONAME && (u_int)buflen > UCHAR_MAX)
   1007 		return (EINVAL);
   1008 
   1009 	/* Allocate an mbuf to hold the arguments. */
   1010 	m = m_get(M_WAIT, type);
   1011 	if ((u_int)buflen > MLEN) {
   1012 		/*
   1013 		 * Won't fit into a regular mbuf, so we allocate just
   1014 		 * enough external storage to hold the argument.
   1015 		 */
   1016 		MEXTMALLOC(m, buflen, M_WAITOK);
   1017 	}
   1018 	m->m_len = buflen;
   1019 	error = copyin(buf, mtod(m, caddr_t), (u_int)buflen);
   1020 	if (error) {
   1021 		(void) m_free(m);
   1022 		return (error);
   1023 	}
   1024 	*mp = m;
   1025 	if (type == MT_SONAME) {
   1026 		sa = mtod(m, struct sockaddr *);
   1027 
   1028 #if defined(COMPAT_OLDSOCK) && BYTE_ORDER != BIG_ENDIAN
   1029 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1030 			sa->sa_family = sa->sa_len;
   1031 #endif
   1032 		sa->sa_len = buflen;
   1033 	}
   1034 	return (0);
   1035 }
   1036 
   1037 int
   1038 getsock(fdp, fdes, fpp)
   1039 	struct filedesc *fdp;
   1040 	int fdes;
   1041 	struct file **fpp;
   1042 {
   1043 	register struct file *fp;
   1044 
   1045 	if ((unsigned)fdes >= fdp->fd_nfiles ||
   1046 	    (fp = fdp->fd_ofiles[fdes]) == NULL)
   1047 		return (EBADF);
   1048 	if (fp->f_type != DTYPE_SOCKET)
   1049 		return (ENOTSOCK);
   1050 	*fpp = fp;
   1051 	return (0);
   1052 }
   1053