Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.161.2.1
      1 /*	$NetBSD: uipc_syscalls.c,v 1.161.2.1 2013/08/28 15:21:48 rmind Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2008, 2009 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (c) 1982, 1986, 1989, 1990, 1993
     34  *	The Regents of the University of California.  All rights reserved.
     35  *
     36  * Redistribution and use in source and binary forms, with or without
     37  * modification, are permitted provided that the following conditions
     38  * are met:
     39  * 1. Redistributions of source code must retain the above copyright
     40  *    notice, this list of conditions and the following disclaimer.
     41  * 2. Redistributions in binary form must reproduce the above copyright
     42  *    notice, this list of conditions and the following disclaimer in the
     43  *    documentation and/or other materials provided with the distribution.
     44  * 3. Neither the name of the University nor the names of its contributors
     45  *    may be used to endorse or promote products derived from this software
     46  *    without specific prior written permission.
     47  *
     48  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     49  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     50  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     51  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     52  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     53  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     54  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     55  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     56  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     57  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     58  * SUCH DAMAGE.
     59  *
     60  *	@(#)uipc_syscalls.c	8.6 (Berkeley) 2/14/95
     61  */
     62 
     63 #include <sys/cdefs.h>
     64 __KERNEL_RCSID(0, "$NetBSD: uipc_syscalls.c,v 1.161.2.1 2013/08/28 15:21:48 rmind Exp $");
     65 
     66 #include "opt_pipe.h"
     67 
     68 #include <sys/param.h>
     69 #include <sys/systm.h>
     70 #include <sys/filedesc.h>
     71 #include <sys/proc.h>
     72 #include <sys/file.h>
     73 #include <sys/buf.h>
     74 #define MBUFTYPES
     75 #include <sys/mbuf.h>
     76 #include <sys/protosw.h>
     77 #include <sys/socket.h>
     78 #include <sys/socketvar.h>
     79 #include <sys/signalvar.h>
     80 #include <sys/un.h>
     81 #include <sys/ktrace.h>
     82 #include <sys/event.h>
     83 #include <sys/atomic.h>
     84 #include <sys/kauth.h>
     85 
     86 #include <sys/mount.h>
     87 #include <sys/syscallargs.h>
     88 
     89 /*
     90  * System call interface to the socket abstraction.
     91  */
     92 extern const struct fileops socketops;
     93 
     94 int
     95 sys___socket30(struct lwp *l, const struct sys___socket30_args *uap,
     96     register_t *retval)
     97 {
     98 	/* {
     99 		syscallarg(int)	domain;
    100 		syscallarg(int)	type;
    101 		syscallarg(int)	protocol;
    102 	} */
    103 	int fd, error;
    104 
    105 	error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type),
    106 	    SCARG(uap, protocol), &fd);
    107 	if (error == 0) {
    108 		*retval = fd;
    109 	}
    110 	return error;
    111 }
    112 
    113 int
    114 sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval)
    115 {
    116 	/* {
    117 		syscallarg(int)				s;
    118 		syscallarg(const struct sockaddr *)	name;
    119 		syscallarg(unsigned int)		namelen;
    120 	} */
    121 	struct mbuf	*nam;
    122 	int		error;
    123 
    124 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    125 	    MT_SONAME);
    126 	if (error)
    127 		return error;
    128 
    129 	return do_sys_bind(l, SCARG(uap, s), nam);
    130 }
    131 
    132 int
    133 do_sys_bind(struct lwp *l, int fd, struct mbuf *nam)
    134 {
    135 	struct socket	*so;
    136 	int		error;
    137 
    138 	if ((error = fd_getsock(fd, &so)) != 0) {
    139 		m_freem(nam);
    140 		return (error);
    141 	}
    142 	MCLAIM(nam, so->so_mowner);
    143 	error = sobind(so, nam, l);
    144 	m_freem(nam);
    145 	fd_putfile(fd);
    146 	return error;
    147 }
    148 
    149 int
    150 sys_listen(struct lwp *l, const struct sys_listen_args *uap, register_t *retval)
    151 {
    152 	/* {
    153 		syscallarg(int)	s;
    154 		syscallarg(int)	backlog;
    155 	} */
    156 	struct socket	*so;
    157 	int		error;
    158 
    159 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
    160 		return (error);
    161 	error = solisten(so, SCARG(uap, backlog), l);
    162 	fd_putfile(SCARG(uap, s));
    163 	return error;
    164 }
    165 
    166 int
    167 do_sys_accept(struct lwp *l, int sock, struct mbuf **name, register_t *new_sock,
    168     const sigset_t *mask, int flags, int clrflags)
    169 {
    170 	file_t		*fp, *fp2;
    171 	struct mbuf	*nam;
    172 	int		error, fd;
    173 	struct socket	*so, *so2;
    174 	short		wakeup_state = 0;
    175 
    176 	if ((fp = fd_getfile(sock)) == NULL)
    177 		return (EBADF);
    178 	if (fp->f_type != DTYPE_SOCKET) {
    179 		fd_putfile(sock);
    180 		return (ENOTSOCK);
    181 	}
    182 	if ((error = fd_allocfile(&fp2, &fd)) != 0) {
    183 		fd_putfile(sock);
    184 		return (error);
    185 	}
    186 	nam = m_get(M_WAIT, MT_SONAME);
    187 	*new_sock = fd;
    188 	so = fp->f_data;
    189 	solock(so);
    190 
    191 	if (__predict_false(mask))
    192 		sigsuspendsetup(l, mask);
    193 
    194 	if (!(so->so_proto->pr_flags & PR_LISTEN)) {
    195 		error = EOPNOTSUPP;
    196 		goto bad;
    197 	}
    198 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    199 		error = EINVAL;
    200 		goto bad;
    201 	}
    202 	if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
    203 		error = EWOULDBLOCK;
    204 		goto bad;
    205 	}
    206 	while (so->so_qlen == 0 && so->so_error == 0) {
    207 		if (so->so_state & SS_CANTRCVMORE) {
    208 			so->so_error = ECONNABORTED;
    209 			break;
    210 		}
    211 		if (wakeup_state & SS_RESTARTSYS) {
    212 			error = ERESTART;
    213 			goto bad;
    214 		}
    215 		error = sowait(so, true, 0);
    216 		if (error) {
    217 			goto bad;
    218 		}
    219 		wakeup_state = so->so_state;
    220 	}
    221 	if (so->so_error) {
    222 		error = so->so_error;
    223 		so->so_error = 0;
    224 		goto bad;
    225 	}
    226 	/* connection has been removed from the listen queue */
    227 	KNOTE(&so->so_rcv.sb_sel.sel_klist, NOTE_SUBMIT);
    228 	so2 = TAILQ_FIRST(&so->so_q);
    229 	if (soqremque(so2, 1) == 0)
    230 		panic("accept");
    231 	fp2->f_type = DTYPE_SOCKET;
    232 	fp2->f_flag = (fp->f_flag & ~clrflags) |
    233 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    234 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    235 	fp2->f_ops = &socketops;
    236 	fp2->f_data = so2;
    237 	if (flags & SOCK_NONBLOCK)
    238 		so2->so_state |= SS_NBIO;
    239 	error = soaccept(so2, nam);
    240 	so2->so_cred = kauth_cred_dup(so->so_cred);
    241 	sounlock(so);
    242 	if (error) {
    243 		/* an error occurred, free the file descriptor and mbuf */
    244 		m_freem(nam);
    245 		mutex_enter(&fp2->f_lock);
    246 		fp2->f_count++;
    247 		mutex_exit(&fp2->f_lock);
    248 		closef(fp2);
    249 		fd_abort(curproc, NULL, fd);
    250 	} else {
    251 		fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
    252 		fd_affix(curproc, fp2, fd);
    253 		*name = nam;
    254 	}
    255 	fd_putfile(sock);
    256 	if (__predict_false(mask))
    257 		sigsuspendteardown(l);
    258 	return (error);
    259  bad:
    260 	sounlock(so);
    261 	m_freem(nam);
    262 	fd_putfile(sock);
    263 	fd_abort(curproc, fp2, fd);
    264 	if (__predict_false(mask))
    265 		sigsuspendteardown(l);
    266 	return (error);
    267 }
    268 
    269 int
    270 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval)
    271 {
    272 	/* {
    273 		syscallarg(int)			s;
    274 		syscallarg(struct sockaddr *)	name;
    275 		syscallarg(unsigned int *)	anamelen;
    276 	} */
    277 	int error, fd;
    278 	struct mbuf *name;
    279 
    280 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, NULL, 0, 0);
    281 	if (error != 0)
    282 		return error;
    283 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    284 	    MSG_LENUSRSPACE, name);
    285 	if (name != NULL)
    286 		m_free(name);
    287 	if (error != 0) {
    288 		fd = (int)*retval;
    289 		if (fd_getfile(fd) != NULL)
    290 			(void)fd_close(fd);
    291 	}
    292 	return error;
    293 }
    294 
    295 int
    296 sys_paccept(struct lwp *l, const struct sys_paccept_args *uap,
    297     register_t *retval)
    298 {
    299 	/* {
    300 		syscallarg(int)			s;
    301 		syscallarg(struct sockaddr *)	name;
    302 		syscallarg(unsigned int *)	anamelen;
    303 		syscallarg(const sigset_t *)	mask;
    304 		syscallarg(int)			flags;
    305 	} */
    306 	int error, fd;
    307 	struct mbuf *name;
    308 	sigset_t *mask, amask;
    309 
    310 	if (SCARG(uap, mask) != NULL) {
    311 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    312 		if (error)
    313 			return error;
    314 		mask = &amask;
    315 	} else
    316 		mask = NULL;
    317 
    318 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, mask,
    319 	    SCARG(uap, flags), FNONBLOCK);
    320 	if (error != 0)
    321 		return error;
    322 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    323 	    MSG_LENUSRSPACE, name);
    324 	if (name != NULL)
    325 		m_free(name);
    326 	if (error != 0) {
    327 		fd = (int)*retval;
    328 		if (fd_getfile(fd) != NULL)
    329 			(void)fd_close(fd);
    330 	}
    331 	return error;
    332 }
    333 
    334 int
    335 sys_connect(struct lwp *l, const struct sys_connect_args *uap, register_t *retval)
    336 {
    337 	/* {
    338 		syscallarg(int)				s;
    339 		syscallarg(const struct sockaddr *)	name;
    340 		syscallarg(unsigned int)		namelen;
    341 	} */
    342 	int		error;
    343 	struct mbuf	*nam;
    344 
    345 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    346 	    MT_SONAME);
    347 	if (error)
    348 		return error;
    349 	return do_sys_connect(l,  SCARG(uap, s), nam);
    350 }
    351 
    352 int
    353 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam)
    354 {
    355 	struct socket	*so;
    356 	int		error;
    357 	int		interrupted = 0;
    358 
    359 	if ((error = fd_getsock(fd, &so)) != 0) {
    360 		m_freem(nam);
    361 		return (error);
    362 	}
    363 	solock(so);
    364 	MCLAIM(nam, so->so_mowner);
    365 	if ((so->so_state & SS_ISCONNECTING) != 0) {
    366 		error = EALREADY;
    367 		goto out;
    368 	}
    369 
    370 	error = soconnect(so, nam, l);
    371 	if (error)
    372 		goto bad;
    373 	if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) ==
    374 	    (SS_NBIO|SS_ISCONNECTING)) {
    375 		error = EINPROGRESS;
    376 		goto out;
    377 	}
    378 	while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) {
    379 		error = sowait(so, true, 0);
    380 		if (__predict_false((so->so_state & SS_ISABORTING) != 0)) {
    381 			error = EPIPE;
    382 			interrupted = 1;
    383 			break;
    384 		}
    385 		if (error) {
    386 			if (error == EINTR || error == ERESTART)
    387 				interrupted = 1;
    388 			break;
    389 		}
    390 	}
    391 	if (error == 0) {
    392 		error = so->so_error;
    393 		so->so_error = 0;
    394 	}
    395  bad:
    396 	if (!interrupted)
    397 		so->so_state &= ~SS_ISCONNECTING;
    398 	if (error == ERESTART)
    399 		error = EINTR;
    400  out:
    401 	sounlock(so);
    402 	fd_putfile(fd);
    403 	m_freem(nam);
    404 	return (error);
    405 }
    406 
    407 static int
    408 makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type,
    409     int domain, int proto, struct socket *soo)
    410 {
    411 	struct socket *so;
    412 	int error;
    413 
    414 	if ((error = socreate(domain, &so, type, proto, l, soo)) != 0) {
    415 		return error;
    416 	}
    417 	if (flags & SOCK_NONBLOCK) {
    418 		so->so_state |= SS_NBIO;
    419 	}
    420 
    421 	if ((error = fd_allocfile(fp, fd)) != 0) {
    422 		soclose(so);
    423 		return error;
    424 	}
    425 	fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0);
    426 	(*fp)->f_flag = FREAD|FWRITE|
    427 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    428 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    429 	(*fp)->f_type = DTYPE_SOCKET;
    430 	(*fp)->f_ops = &socketops;
    431 	(*fp)->f_data = so;
    432 	return 0;
    433 }
    434 
    435 int
    436 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap,
    437     register_t *retval)
    438 {
    439 	/* {
    440 		syscallarg(int)		domain;
    441 		syscallarg(int)		type;
    442 		syscallarg(int)		protocol;
    443 		syscallarg(int *)	rsv;
    444 	} */
    445 	file_t		*fp1, *fp2;
    446 	struct socket	*so1, *so2;
    447 	int		fd, error, sv[2];
    448 	proc_t		*p;
    449 	int		flags = SCARG(uap, type) & SOCK_FLAGS_MASK;
    450 	int		type = SCARG(uap, type) & ~SOCK_FLAGS_MASK;
    451 	int		domain = SCARG(uap, domain);
    452 	int		proto = SCARG(uap, protocol);
    453 
    454 	p = curproc;
    455 
    456 	error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL);
    457 	if (error)
    458 		return error;
    459 	so1 = fp1->f_data;
    460 	sv[0] = fd;
    461 
    462 	error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1);
    463 	if (error)
    464 		goto out;
    465 	so2 = fp2->f_data;
    466 	sv[1] = fd;
    467 
    468 	solock(so1);
    469 	error = soconnect2(so1, so2);
    470 	if (error == 0 && type == SOCK_DGRAM) {
    471 		/*
    472 		 * Datagram socket connection is asymmetric.
    473 		 */
    474 		error = soconnect2(so2, so1);
    475 	}
    476 	sounlock(so1);
    477 
    478 	if (error == 0)
    479 		error = copyout(sv, SCARG(uap, rsv), sizeof(sv));
    480 	if (error == 0) {
    481 		fd_affix(p, fp2, sv[1]);
    482 		fd_affix(p, fp1, sv[0]);
    483 		return 0;
    484 	}
    485 	fd_abort(p, fp2, sv[1]);
    486 	(void)soclose(so2);
    487 out:
    488 	fd_abort(p, fp1, sv[0]);
    489 	(void)soclose(so1);
    490 	return error;
    491 }
    492 
    493 int
    494 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap, register_t *retval)
    495 {
    496 	/* {
    497 		syscallarg(int)				s;
    498 		syscallarg(const void *)		buf;
    499 		syscallarg(size_t)			len;
    500 		syscallarg(int)				flags;
    501 		syscallarg(const struct sockaddr *)	to;
    502 		syscallarg(unsigned int)		tolen;
    503 	} */
    504 	struct msghdr	msg;
    505 	struct iovec	aiov;
    506 
    507 	msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
    508 	msg.msg_namelen = SCARG(uap, tolen);
    509 	msg.msg_iov = &aiov;
    510 	msg.msg_iovlen = 1;
    511 	msg.msg_control = NULL;
    512 	msg.msg_flags = 0;
    513 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
    514 	aiov.iov_len = SCARG(uap, len);
    515 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    516 }
    517 
    518 int
    519 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap, register_t *retval)
    520 {
    521 	/* {
    522 		syscallarg(int)				s;
    523 		syscallarg(const struct msghdr *)	msg;
    524 		syscallarg(int)				flags;
    525 	} */
    526 	struct msghdr	msg;
    527 	int		error;
    528 
    529 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    530 	if (error)
    531 		return (error);
    532 
    533 	msg.msg_flags = MSG_IOVUSRSPACE;
    534 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    535 }
    536 
    537 static int
    538 do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp,
    539     struct msghdr *mp, int flags, register_t *retsize)
    540 {
    541 
    542 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    543 	struct mbuf	*to, *control;
    544 	struct uio	auio;
    545 	size_t		len, iovsz;
    546 	int		i, error;
    547 
    548 	ktrkuser("msghdr", mp, sizeof *mp);
    549 
    550 	/* If the caller passed us stuff in mbufs, we must free them. */
    551 	to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL;
    552 	control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL;
    553 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    554 
    555 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    556 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    557 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    558 				error = EMSGSIZE;
    559 				goto bad;
    560 			}
    561 			iov = kmem_alloc(iovsz, KM_SLEEP);
    562 		}
    563 		if (mp->msg_iovlen != 0) {
    564 			error = copyin(mp->msg_iov, iov, iovsz);
    565 			if (error)
    566 				goto bad;
    567 		}
    568 		mp->msg_iov = iov;
    569 	}
    570 
    571 	auio.uio_iov = mp->msg_iov;
    572 	auio.uio_iovcnt = mp->msg_iovlen;
    573 	auio.uio_rw = UIO_WRITE;
    574 	auio.uio_offset = 0;			/* XXX */
    575 	auio.uio_resid = 0;
    576 	KASSERT(l == curlwp);
    577 	auio.uio_vmspace = l->l_proc->p_vmspace;
    578 
    579 	for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) {
    580 		/*
    581 		 * Writes return ssize_t because -1 is returned on error.
    582 		 * Therefore, we must restrict the length to SSIZE_MAX to
    583 		 * avoid garbage return values.
    584 		 */
    585 		auio.uio_resid += tiov->iov_len;
    586 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    587 			error = EINVAL;
    588 			goto bad;
    589 		}
    590 	}
    591 
    592 	if (mp->msg_name && to == NULL) {
    593 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    594 		    MT_SONAME);
    595 		if (error)
    596 			goto bad;
    597 	}
    598 
    599 	if (mp->msg_control) {
    600 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
    601 			error = EINVAL;
    602 			goto bad;
    603 		}
    604 		if (control == NULL) {
    605 			error = sockargs(&control, mp->msg_control,
    606 			    mp->msg_controllen, MT_CONTROL);
    607 			if (error)
    608 				goto bad;
    609 		}
    610 	}
    611 
    612 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
    613 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    614 		memcpy(ktriov, auio.uio_iov, iovsz);
    615 	}
    616 
    617 	if (mp->msg_name)
    618 		MCLAIM(to, so->so_mowner);
    619 	if (mp->msg_control)
    620 		MCLAIM(control, so->so_mowner);
    621 
    622 	len = auio.uio_resid;
    623 	error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
    624 	/* Protocol is responsible for freeing 'control' */
    625 	control = NULL;
    626 
    627 	if (error) {
    628 		if (auio.uio_resid != len && (error == ERESTART ||
    629 		    error == EINTR || error == EWOULDBLOCK))
    630 			error = 0;
    631 		if (error == EPIPE && (fp->f_flag & FNOSIGPIPE) == 0 &&
    632 		    (flags & MSG_NOSIGNAL) == 0) {
    633 			mutex_enter(proc_lock);
    634 			psignal(l->l_proc, SIGPIPE);
    635 			mutex_exit(proc_lock);
    636 		}
    637 	}
    638 	if (error == 0)
    639 		*retsize = len - auio.uio_resid;
    640 
    641 bad:
    642 	if (ktriov != NULL) {
    643 		ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
    644 		kmem_free(ktriov, iovsz);
    645 	}
    646 
    647 	if (iov != aiov)
    648 		kmem_free(iov, iovsz);
    649 	if (to)
    650 		m_freem(to);
    651 	if (control)
    652 		m_freem(control);
    653 
    654 	return (error);
    655 }
    656 
    657 int
    658 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
    659     register_t *retsize)
    660 {
    661 	int		error;
    662 	struct socket	*so;
    663 	file_t		*fp;
    664 
    665 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
    666 		return error;
    667 	error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize);
    668 	fd_putfile(s);
    669 	return error;
    670 }
    671 
    672 int
    673 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval)
    674 {
    675 	/* {
    676 		syscallarg(int)			s;
    677 		syscallarg(void *)		buf;
    678 		syscallarg(size_t)		len;
    679 		syscallarg(int)			flags;
    680 		syscallarg(struct sockaddr *)	from;
    681 		syscallarg(unsigned int *)	fromlenaddr;
    682 	} */
    683 	struct msghdr	msg;
    684 	struct iovec	aiov;
    685 	int		error;
    686 	struct mbuf	*from;
    687 
    688 	msg.msg_name = NULL;
    689 	msg.msg_iov = &aiov;
    690 	msg.msg_iovlen = 1;
    691 	aiov.iov_base = SCARG(uap, buf);
    692 	aiov.iov_len = SCARG(uap, len);
    693 	msg.msg_control = NULL;
    694 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
    695 
    696 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
    697 	if (error != 0)
    698 		return error;
    699 
    700 	error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
    701 	    MSG_LENUSRSPACE, from);
    702 	if (from != NULL)
    703 		m_free(from);
    704 	return error;
    705 }
    706 
    707 int
    708 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval)
    709 {
    710 	/* {
    711 		syscallarg(int)			s;
    712 		syscallarg(struct msghdr *)	msg;
    713 		syscallarg(int)			flags;
    714 	} */
    715 	struct msghdr	msg;
    716 	int		error;
    717 	struct mbuf	*from, *control;
    718 
    719 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    720 	if (error)
    721 		return (error);
    722 
    723 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    724 
    725 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
    726 	    msg.msg_control != NULL ? &control : NULL, retval);
    727 	if (error != 0)
    728 		return error;
    729 
    730 	if (msg.msg_control != NULL)
    731 		error = copyout_msg_control(l, &msg, control);
    732 
    733 	if (error == 0)
    734 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
    735 			from);
    736 	if (from != NULL)
    737 		m_free(from);
    738 	if (error == 0) {
    739 		ktrkuser("msghdr", &msg, sizeof msg);
    740 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
    741 	}
    742 
    743 	return (error);
    744 }
    745 
    746 int
    747 sys_sendmmsg(struct lwp *l, const struct sys_sendmmsg_args *uap,
    748     register_t *retval)
    749 {
    750 	/* {
    751 		syscallarg(int)			s;
    752 		syscallarg(struct mmsghdr *)	mmsg;
    753 		syscallarg(unsigned int)	vlen;
    754 		syscallarg(unsigned int)	flags;
    755 	} */
    756 	struct mmsghdr mmsg;
    757 	struct socket *so;
    758 	file_t *fp;
    759 	struct msghdr *msg = &mmsg.msg_hdr;
    760 	int error, s;
    761 	unsigned int vlen, flags, dg;
    762 
    763 	s = SCARG(uap, s);
    764 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
    765 		return error;
    766 
    767 	vlen = SCARG(uap, vlen);
    768 	if (vlen > 1024)
    769 		vlen = 1024;
    770 
    771 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    772 
    773 	for (dg = 0; dg < vlen;) {
    774 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
    775 		if (error)
    776 			break;
    777 
    778 		msg->msg_flags = flags;
    779 
    780 		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
    781 		if (error)
    782 			break;
    783 
    784 		ktrkuser("msghdr", msg, sizeof *msg);
    785 		mmsg.msg_len = *retval;
    786 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
    787 		if (error)
    788 			break;
    789 		dg++;
    790 
    791 	}
    792 
    793 	*retval = dg;
    794 	if (error)
    795 		so->so_error = error;
    796 
    797 	fd_putfile(s);
    798 
    799 	/*
    800 	 * If we succeeded at least once, return 0, hopefully so->so_error
    801 	 * will catch it next time.
    802 	 */
    803 	if (dg)
    804 		return 0;
    805 	return error;
    806 }
    807 
    808 /*
    809  * Adjust for a truncated SCM_RIGHTS control message.
    810  *  This means closing any file descriptors that aren't present
    811  *  in the returned buffer.
    812  *  m is the mbuf holding the (already externalized) SCM_RIGHTS message.
    813  */
    814 static void
    815 free_rights(struct mbuf *m)
    816 {
    817 	struct cmsghdr *cm;
    818 	int *fdv;
    819 	unsigned int nfds, i;
    820 
    821 	KASSERT(sizeof(*cm) <= m->m_len);
    822 	cm = mtod(m, struct cmsghdr *);
    823 
    824 	KASSERT(CMSG_ALIGN(sizeof(*cm)) <= cm->cmsg_len);
    825 	KASSERT(cm->cmsg_len <= m->m_len);
    826 	nfds = (cm->cmsg_len - CMSG_ALIGN(sizeof(*cm))) / sizeof(int);
    827 	fdv = (int *)CMSG_DATA(cm);
    828 
    829 	for (i = 0; i < nfds; i++)
    830 		if (fd_getfile(fdv[i]) != NULL)
    831 			(void)fd_close(fdv[i]);
    832 }
    833 
    834 void
    835 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
    836 {
    837 	struct mbuf *next;
    838 	struct cmsghdr *cmsg;
    839 	bool do_free_rights = false;
    840 
    841 	while (control != NULL) {
    842 		cmsg = mtod(control, struct cmsghdr *);
    843 		if (control == uncopied)
    844 			do_free_rights = true;
    845 		if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
    846 		    && cmsg->cmsg_type == SCM_RIGHTS)
    847 			free_rights(control);
    848 		next = control->m_next;
    849 		m_free(control);
    850 		control = next;
    851 	}
    852 }
    853 
    854 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
    855 int
    856 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
    857 {
    858 	int i, len, error = 0;
    859 	struct cmsghdr *cmsg;
    860 	struct mbuf *m;
    861 	char *q;
    862 
    863 	len = mp->msg_controllen;
    864 	if (len <= 0 || control == 0) {
    865 		mp->msg_controllen = 0;
    866 		free_control_mbuf(l, control, control);
    867 		return 0;
    868 	}
    869 
    870 	q = (char *)mp->msg_control;
    871 
    872 	for (m = control; m != NULL; ) {
    873 		cmsg = mtod(m, struct cmsghdr *);
    874 		i = m->m_len;
    875 		if (len < i) {
    876 			mp->msg_flags |= MSG_CTRUNC;
    877 			if (cmsg->cmsg_level == SOL_SOCKET
    878 			    && cmsg->cmsg_type == SCM_RIGHTS)
    879 				/* Do not truncate me ... */
    880 				break;
    881 			i = len;
    882 		}
    883 		error = copyout(mtod(m, void *), q, i);
    884 		ktrkuser("msgcontrol", mtod(m, void *), i);
    885 		if (error != 0) {
    886 			/* We must free all the SCM_RIGHTS */
    887 			m = control;
    888 			break;
    889 		}
    890 		m = m->m_next;
    891 		if (m)
    892 			i = ALIGN(i);
    893 		q += i;
    894 		len -= i;
    895 		if (len <= 0)
    896 			break;
    897 	}
    898 
    899 	free_control_mbuf(l, control, m);
    900 
    901 	mp->msg_controllen = q - (char *)mp->msg_control;
    902 	return error;
    903 }
    904 
    905 static int
    906 do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp,
    907     struct mbuf **from, struct mbuf **control, register_t *retsize)
    908 {
    909 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    910 	struct uio	auio;
    911 	size_t		len, iovsz;
    912 	int		i, error;
    913 
    914 	ktrkuser("msghdr", mp, sizeof *mp);
    915 
    916 	*from = NULL;
    917 	if (control != NULL)
    918 		*control = NULL;
    919 
    920 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    921 
    922 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    923 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    924 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    925 				error = EMSGSIZE;
    926 				goto out;
    927 			}
    928 			iov = kmem_alloc(iovsz, KM_SLEEP);
    929 		}
    930 		if (mp->msg_iovlen != 0) {
    931 			error = copyin(mp->msg_iov, iov, iovsz);
    932 			if (error)
    933 				goto out;
    934 		}
    935 		auio.uio_iov = iov;
    936 	} else
    937 		auio.uio_iov = mp->msg_iov;
    938 	auio.uio_iovcnt = mp->msg_iovlen;
    939 	auio.uio_rw = UIO_READ;
    940 	auio.uio_offset = 0;			/* XXX */
    941 	auio.uio_resid = 0;
    942 	KASSERT(l == curlwp);
    943 	auio.uio_vmspace = l->l_proc->p_vmspace;
    944 
    945 	tiov = auio.uio_iov;
    946 	for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
    947 		/*
    948 		 * Reads return ssize_t because -1 is returned on error.
    949 		 * Therefore we must restrict the length to SSIZE_MAX to
    950 		 * avoid garbage return values.
    951 		 */
    952 		auio.uio_resid += tiov->iov_len;
    953 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    954 			error = EINVAL;
    955 			goto out;
    956 		}
    957 	}
    958 
    959 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
    960 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    961 		memcpy(ktriov, auio.uio_iov, iovsz);
    962 	}
    963 
    964 	len = auio.uio_resid;
    965 	mp->msg_flags &= MSG_USERFLAGS;
    966 	error = (*so->so_receive)(so, from, &auio, NULL, control,
    967 	    &mp->msg_flags);
    968 	len -= auio.uio_resid;
    969 	*retsize = len;
    970 	if (error != 0 && len != 0
    971 	    && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
    972 		/* Some data transferred */
    973 		error = 0;
    974 
    975 	if (ktriov != NULL) {
    976 		ktrgeniov(s, UIO_READ, ktriov, len, error);
    977 		kmem_free(ktriov, iovsz);
    978 	}
    979 
    980 	if (error != 0) {
    981 		m_freem(*from);
    982 		*from = NULL;
    983 		if (control != NULL) {
    984 			free_control_mbuf(l, *control, *control);
    985 			*control = NULL;
    986 		}
    987 	}
    988  out:
    989 	if (iov != aiov)
    990 		kmem_free(iov, iovsz);
    991 	return (error);
    992 }
    993 
    994 
    995 int
    996 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
    997     struct mbuf **control, register_t *retsize)
    998 {
    999 	int error;
   1000 	struct socket *so;
   1001 
   1002 	if ((error = fd_getsock(s, &so)) != 0)
   1003 		return error;
   1004 	error = do_sys_recvmsg_so(l, s, so, mp, from, control, retsize);
   1005 	fd_putfile(s);
   1006 	return error;
   1007 }
   1008 
   1009 int
   1010 sys_recvmmsg(struct lwp *l, const struct sys_recvmmsg_args *uap,
   1011     register_t *retval)
   1012 {
   1013 	/* {
   1014 		syscallarg(int)			s;
   1015 		syscallarg(struct mmsghdr *)	mmsg;
   1016 		syscallarg(unsigned int)	vlen;
   1017 		syscallarg(unsigned int)	flags;
   1018 		syscallarg(struct timespec *)	timeout;
   1019 	} */
   1020 	struct mmsghdr mmsg;
   1021 	struct socket *so;
   1022 	struct msghdr *msg = &mmsg.msg_hdr;
   1023 	int error, s;
   1024 	struct mbuf *from, *control;
   1025 	struct timespec ts, now;
   1026 	unsigned int vlen, flags, dg;
   1027 
   1028 	if (SCARG(uap, timeout)) {
   1029 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
   1030 			return error;
   1031 		getnanotime(&now);
   1032 		timespecadd(&now, &ts, &ts);
   1033 	}
   1034 
   1035 	s = SCARG(uap, s);
   1036 	if ((error = fd_getsock(s, &so)) != 0)
   1037 		return error;
   1038 
   1039 	vlen = SCARG(uap, vlen);
   1040 	if (vlen > 1024)
   1041 		vlen = 1024;
   1042 
   1043 	from = NULL;
   1044 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
   1045 
   1046 	for (dg = 0; dg < vlen;) {
   1047 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
   1048 		if (error)
   1049 			break;
   1050 
   1051 		msg->msg_flags = flags & ~MSG_WAITFORONE;
   1052 
   1053 		if (from != NULL) {
   1054 			m_free(from);
   1055 			from = NULL;
   1056 		}
   1057 
   1058 		error = do_sys_recvmsg_so(l, s, so, msg, &from,
   1059 		    msg->msg_control != NULL ? &control : NULL, retval);
   1060 		if (error)
   1061 			break;
   1062 
   1063 		if (msg->msg_control != NULL)
   1064 			error = copyout_msg_control(l, msg, control);
   1065 		if (error)
   1066 			break;
   1067 
   1068 		error = copyout_sockname(msg->msg_name, &msg->msg_namelen, 0,
   1069 		    from);
   1070 		if (error)
   1071 			break;
   1072 
   1073 		ktrkuser("msghdr", msg, sizeof *msg);
   1074 		mmsg.msg_len = *retval;
   1075 
   1076 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
   1077 		if (error)
   1078 			break;
   1079 
   1080 		dg++;
   1081 		if (msg->msg_flags & MSG_OOB)
   1082 			break;
   1083 
   1084 		if (SCARG(uap, timeout)) {
   1085 			getnanotime(&now);
   1086 			timespecsub(&now, &ts, &now);
   1087 			if (now.tv_sec > 0)
   1088 				break;
   1089 		}
   1090 
   1091 		if (flags & MSG_WAITFORONE)
   1092 			flags |= MSG_DONTWAIT;
   1093 
   1094 	}
   1095 
   1096 	if (from != NULL)
   1097 		m_free(from);
   1098 
   1099 	*retval = dg;
   1100 	if (error)
   1101 		so->so_error = error;
   1102 
   1103 	fd_putfile(s);
   1104 
   1105 	/*
   1106 	 * If we succeeded at least once, return 0, hopefully so->so_error
   1107 	 * will catch it next time.
   1108 	 */
   1109 	if (dg)
   1110 		return 0;
   1111 
   1112 	return error;
   1113 }
   1114 
   1115 int
   1116 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap,
   1117     register_t *retval)
   1118 {
   1119 	/* {
   1120 		syscallarg(int)	s;
   1121 		syscallarg(int)	how;
   1122 	} */
   1123 	struct socket	*so;
   1124 	int		error;
   1125 
   1126 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
   1127 		return (error);
   1128 	solock(so);
   1129 	error = soshutdown(so, SCARG(uap, how));
   1130 	sounlock(so);
   1131 	fd_putfile(SCARG(uap, s));
   1132 	return (error);
   1133 }
   1134 
   1135 int
   1136 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap,
   1137     register_t *retval)
   1138 {
   1139 	/* {
   1140 		syscallarg(int)			s;
   1141 		syscallarg(int)			level;
   1142 		syscallarg(int)			name;
   1143 		syscallarg(const void *)	val;
   1144 		syscallarg(unsigned int)	valsize;
   1145 	} */
   1146 	struct sockopt	sopt;
   1147 	struct socket	*so;
   1148 	file_t		*fp;
   1149 	int		error;
   1150 	unsigned int	len;
   1151 
   1152 	len = SCARG(uap, valsize);
   1153 	if (len > 0 && SCARG(uap, val) == NULL)
   1154 		return (EINVAL);
   1155 
   1156 	if (len > MCLBYTES)
   1157 		return (EINVAL);
   1158 
   1159 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
   1160 		return (error);
   1161 
   1162 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len);
   1163 
   1164 	if (len > 0) {
   1165 		error = copyin(SCARG(uap, val), sopt.sopt_data, len);
   1166 		if (error)
   1167 			goto out;
   1168 	}
   1169 
   1170 	error = sosetopt(so, &sopt);
   1171 	if (so->so_options & SO_NOSIGPIPE)
   1172 		atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
   1173 	else
   1174 		atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
   1175 
   1176  out:
   1177 	sockopt_destroy(&sopt);
   1178 	fd_putfile(SCARG(uap, s));
   1179 	return (error);
   1180 }
   1181 
   1182 int
   1183 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap,
   1184     register_t *retval)
   1185 {
   1186 	/* {
   1187 		syscallarg(int)			s;
   1188 		syscallarg(int)			level;
   1189 		syscallarg(int)			name;
   1190 		syscallarg(void *)		val;
   1191 		syscallarg(unsigned int *)	avalsize;
   1192 	} */
   1193 	struct sockopt	sopt;
   1194 	struct socket	*so;
   1195 	file_t		*fp;
   1196 	unsigned int	valsize, len;
   1197 	int		error;
   1198 
   1199 	if (SCARG(uap, val) != NULL) {
   1200 		error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize));
   1201 		if (error)
   1202 			return (error);
   1203 	} else
   1204 		valsize = 0;
   1205 
   1206 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
   1207 		return (error);
   1208 
   1209 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0);
   1210 
   1211 	if (fp->f_flag & FNOSIGPIPE)
   1212 		so->so_options |= SO_NOSIGPIPE;
   1213 	else
   1214 		so->so_options &= ~SO_NOSIGPIPE;
   1215 	error = sogetopt(so, &sopt);
   1216 	if (error)
   1217 		goto out;
   1218 
   1219 	if (valsize > 0) {
   1220 		len = min(valsize, sopt.sopt_size);
   1221 		error = copyout(sopt.sopt_data, SCARG(uap, val), len);
   1222 		if (error)
   1223 			goto out;
   1224 
   1225 		error = copyout(&len, SCARG(uap, avalsize), sizeof(len));
   1226 		if (error)
   1227 			goto out;
   1228 	}
   1229 
   1230  out:
   1231 	sockopt_destroy(&sopt);
   1232 	fd_putfile(SCARG(uap, s));
   1233 	return (error);
   1234 }
   1235 
   1236 #ifdef PIPE_SOCKETPAIR
   1237 
   1238 int
   1239 pipe1(struct lwp *l, register_t *retval, int flags)
   1240 {
   1241 	file_t		*rf, *wf;
   1242 	struct socket	*rso, *wso;
   1243 	int		fd, error;
   1244 	proc_t		*p;
   1245 
   1246 	if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
   1247 		return EINVAL;
   1248 	p = curproc;
   1249 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
   1250 		return (error);
   1251 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
   1252 		goto free1;
   1253 	/* remember this socket pair implements a pipe */
   1254 	wso->so_state |= SS_ISAPIPE;
   1255 	rso->so_state |= SS_ISAPIPE;
   1256 	if ((error = fd_allocfile(&rf, &fd)) != 0)
   1257 		goto free2;
   1258 	retval[0] = fd;
   1259 	rf->f_flag = FREAD | flags;
   1260 	rf->f_type = DTYPE_SOCKET;
   1261 	rf->f_ops = &socketops;
   1262 	rf->f_data = rso;
   1263 	if ((error = fd_allocfile(&wf, &fd)) != 0)
   1264 		goto free3;
   1265 	wf->f_flag = FWRITE | flags;
   1266 	wf->f_type = DTYPE_SOCKET;
   1267 	wf->f_ops = &socketops;
   1268 	wf->f_data = wso;
   1269 	retval[1] = fd;
   1270 	solock(wso);
   1271 	error = unp_connect2(wso, rso, PRU_CONNECT2);
   1272 	sounlock(wso);
   1273 	if (error != 0)
   1274 		goto free4;
   1275 	fd_affix(p, wf, (int)retval[1]);
   1276 	fd_affix(p, rf, (int)retval[0]);
   1277 	return (0);
   1278  free4:
   1279 	fd_abort(p, wf, (int)retval[1]);
   1280  free3:
   1281 	fd_abort(p, rf, (int)retval[0]);
   1282  free2:
   1283 	(void)soclose(wso);
   1284  free1:
   1285 	(void)soclose(rso);
   1286 	return (error);
   1287 }
   1288 #endif /* PIPE_SOCKETPAIR */
   1289 
   1290 /*
   1291  * Get socket name.
   1292  */
   1293 int
   1294 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
   1295 {
   1296 	struct socket	*so;
   1297 	struct mbuf	*m;
   1298 	int		error;
   1299 
   1300 	if ((error = fd_getsock(fd, &so)) != 0)
   1301 		return error;
   1302 
   1303 	m = m_getclr(M_WAIT, MT_SONAME);
   1304 	MCLAIM(m, so->so_mowner);
   1305 
   1306 	solock(so);
   1307 	if (which == PRU_PEERADDR
   1308 	    && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) {
   1309 		error = ENOTCONN;
   1310 	} else {
   1311 		*nam = m;
   1312 		error = (*so->so_proto->pr_usrreqs->pr_generic)(so,
   1313 		    which, NULL, m, NULL, NULL);
   1314 	}
   1315 	sounlock(so);
   1316 	if (error != 0)
   1317 		m_free(m);
   1318 	fd_putfile(fd);
   1319 	return error;
   1320 }
   1321 
   1322 int
   1323 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
   1324     struct mbuf *addr)
   1325 {
   1326 	int len;
   1327 	int error;
   1328 
   1329 	if (asa == NULL)
   1330 		/* Assume application not interested */
   1331 		return 0;
   1332 
   1333 	if (flags & MSG_LENUSRSPACE) {
   1334 		error = copyin(alen, &len, sizeof(len));
   1335 		if (error)
   1336 			return error;
   1337 	} else
   1338 		len = *alen;
   1339 	if (len < 0)
   1340 		return EINVAL;
   1341 
   1342 	if (addr == NULL) {
   1343 		len = 0;
   1344 		error = 0;
   1345 	} else {
   1346 		if (len > addr->m_len)
   1347 			len = addr->m_len;
   1348 		/* Maybe this ought to copy a chain ? */
   1349 		ktrkuser(mbuftypes[MT_SONAME], mtod(addr, void *), len);
   1350 		error = copyout(mtod(addr, void *), asa, len);
   1351 	}
   1352 
   1353 	if (error == 0) {
   1354 		if (flags & MSG_LENUSRSPACE)
   1355 			error = copyout(&len, alen, sizeof(len));
   1356 		else
   1357 			*alen = len;
   1358 	}
   1359 
   1360 	return error;
   1361 }
   1362 
   1363 /*
   1364  * Get socket name.
   1365  */
   1366 int
   1367 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap,
   1368     register_t *retval)
   1369 {
   1370 	/* {
   1371 		syscallarg(int)			fdes;
   1372 		syscallarg(struct sockaddr *)	asa;
   1373 		syscallarg(unsigned int *)	alen;
   1374 	} */
   1375 	struct mbuf	*m;
   1376 	int		error;
   1377 
   1378 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
   1379 	if (error != 0)
   1380 		return error;
   1381 
   1382 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1383 	    MSG_LENUSRSPACE, m);
   1384 	if (m != NULL)
   1385 		m_free(m);
   1386 	return error;
   1387 }
   1388 
   1389 /*
   1390  * Get name of peer for connected socket.
   1391  */
   1392 int
   1393 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap,
   1394     register_t *retval)
   1395 {
   1396 	/* {
   1397 		syscallarg(int)			fdes;
   1398 		syscallarg(struct sockaddr *)	asa;
   1399 		syscallarg(unsigned int *)	alen;
   1400 	} */
   1401 	struct mbuf	*m;
   1402 	int		error;
   1403 
   1404 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
   1405 	if (error != 0)
   1406 		return error;
   1407 
   1408 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1409 	    MSG_LENUSRSPACE, m);
   1410 	if (m != NULL)
   1411 		m_free(m);
   1412 	return error;
   1413 }
   1414 
   1415 /*
   1416  * XXX In a perfect world, we wouldn't pass around socket control
   1417  * XXX arguments in mbufs, and this could go away.
   1418  */
   1419 int
   1420 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
   1421 {
   1422 	struct sockaddr	*sa;
   1423 	struct mbuf	*m;
   1424 	int		error;
   1425 
   1426 	/*
   1427 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1428 	 * will overflow sa_len.  Control data more than a page size in
   1429 	 * length is just too much.
   1430 	 */
   1431 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1432 		return (EINVAL);
   1433 
   1434 	/* Allocate an mbuf to hold the arguments. */
   1435 	m = m_get(M_WAIT, type);
   1436 	/* can't claim.  don't who to assign it to. */
   1437 	if (buflen > MLEN) {
   1438 		/*
   1439 		 * Won't fit into a regular mbuf, so we allocate just
   1440 		 * enough external storage to hold the argument.
   1441 		 */
   1442 		MEXTMALLOC(m, buflen, M_WAITOK);
   1443 	}
   1444 	m->m_len = buflen;
   1445 	error = copyin(bf, mtod(m, void *), buflen);
   1446 	if (error) {
   1447 		(void) m_free(m);
   1448 		return (error);
   1449 	}
   1450 	ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
   1451 	*mp = m;
   1452 	if (type == MT_SONAME) {
   1453 		sa = mtod(m, struct sockaddr *);
   1454 #if BYTE_ORDER != BIG_ENDIAN
   1455 		/*
   1456 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1457 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1458 		 */
   1459 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1460 			sa->sa_family = sa->sa_len;
   1461 #endif
   1462 		sa->sa_len = buflen;
   1463 	}
   1464 	return (0);
   1465 }
   1466