Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.163
      1 /*	$NetBSD: uipc_syscalls.c,v 1.163 2013/10/08 00:29:24 christos 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.163 2013/10/08 00:29:24 christos 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, register_t *retval)
     96 {
     97 	/* {
     98 		syscallarg(int)	domain;
     99 		syscallarg(int)	type;
    100 		syscallarg(int)	protocol;
    101 	} */
    102 	int		fd, error;
    103 
    104 	error = fsocreate(SCARG(uap, domain), NULL, SCARG(uap, type),
    105 			 SCARG(uap, protocol), l, &fd);
    106 	if (error == 0)
    107 		*retval = fd;
    108 	return error;
    109 }
    110 
    111 /* ARGSUSED */
    112 int
    113 sys_bind(struct lwp *l, const struct sys_bind_args *uap, register_t *retval)
    114 {
    115 	/* {
    116 		syscallarg(int)				s;
    117 		syscallarg(const struct sockaddr *)	name;
    118 		syscallarg(unsigned int)		namelen;
    119 	} */
    120 	struct mbuf	*nam;
    121 	int		error;
    122 
    123 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    124 	    MT_SONAME);
    125 	if (error)
    126 		return error;
    127 
    128 	return do_sys_bind(l, SCARG(uap, s), nam);
    129 }
    130 
    131 int
    132 do_sys_bind(struct lwp *l, int fd, struct mbuf *nam)
    133 {
    134 	struct socket	*so;
    135 	int		error;
    136 
    137 	if ((error = fd_getsock(fd, &so)) != 0) {
    138 		m_freem(nam);
    139 		return (error);
    140 	}
    141 	MCLAIM(nam, so->so_mowner);
    142 	error = sobind(so, nam, l);
    143 	m_freem(nam);
    144 	fd_putfile(fd);
    145 	return error;
    146 }
    147 
    148 /* ARGSUSED */
    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 	nam->m_len = 0;
    188 	*new_sock = fd;
    189 	so = fp->f_data;
    190 	solock(so);
    191 
    192 	if (__predict_false(mask))
    193 		sigsuspendsetup(l, mask);
    194 
    195 	if (!(so->so_proto->pr_flags & PR_LISTEN)) {
    196 		error = EOPNOTSUPP;
    197 		goto bad;
    198 	}
    199 	if ((so->so_options & SO_ACCEPTCONN) == 0) {
    200 		error = EINVAL;
    201 		goto bad;
    202 	}
    203 	if ((so->so_state & SS_NBIO) && so->so_qlen == 0) {
    204 		error = EWOULDBLOCK;
    205 		goto bad;
    206 	}
    207 	while (so->so_qlen == 0 && so->so_error == 0) {
    208 		if (so->so_state & SS_CANTRCVMORE) {
    209 			so->so_error = ECONNABORTED;
    210 			break;
    211 		}
    212 		if (wakeup_state & SS_RESTARTSYS) {
    213 			error = ERESTART;
    214 			goto bad;
    215 		}
    216 		error = sowait(so, true, 0);
    217 		if (error) {
    218 			goto bad;
    219 		}
    220 		wakeup_state = so->so_state;
    221 	}
    222 	if (so->so_error) {
    223 		error = so->so_error;
    224 		so->so_error = 0;
    225 		goto bad;
    226 	}
    227 	/* connection has been removed from the listen queue */
    228 	KNOTE(&so->so_rcv.sb_sel.sel_klist, NOTE_SUBMIT);
    229 	so2 = TAILQ_FIRST(&so->so_q);
    230 	if (soqremque(so2, 1) == 0)
    231 		panic("accept");
    232 	fp2->f_type = DTYPE_SOCKET;
    233 	fp2->f_flag = (fp->f_flag & ~clrflags) |
    234 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    235 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    236 	fp2->f_ops = &socketops;
    237 	fp2->f_data = so2;
    238 	if (flags & SOCK_NONBLOCK)
    239 		so2->so_state |= SS_NBIO;
    240 	error = soaccept(so2, nam);
    241 	so2->so_cred = kauth_cred_dup(so->so_cred);
    242 	sounlock(so);
    243 	if (error) {
    244 		/* an error occurred, free the file descriptor and mbuf */
    245 		m_freem(nam);
    246 		mutex_enter(&fp2->f_lock);
    247 		fp2->f_count++;
    248 		mutex_exit(&fp2->f_lock);
    249 		closef(fp2);
    250 		fd_abort(curproc, NULL, fd);
    251 	} else {
    252 		fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
    253 		fd_affix(curproc, fp2, fd);
    254 		*name = nam;
    255 	}
    256 	fd_putfile(sock);
    257 	if (__predict_false(mask))
    258 		sigsuspendteardown(l);
    259 	return (error);
    260  bad:
    261  	sounlock(so);
    262  	m_freem(nam);
    263 	fd_putfile(sock);
    264  	fd_abort(curproc, fp2, fd);
    265 	if (__predict_false(mask))
    266 		sigsuspendteardown(l);
    267  	return (error);
    268 }
    269 
    270 int
    271 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval)
    272 {
    273 	/* {
    274 		syscallarg(int)			s;
    275 		syscallarg(struct sockaddr *)	name;
    276 		syscallarg(unsigned int *)	anamelen;
    277 	} */
    278 	int error, fd;
    279 	struct mbuf *name;
    280 
    281 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, NULL, 0, 0);
    282 	if (error != 0)
    283 		return error;
    284 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    285 	    MSG_LENUSRSPACE, name);
    286 	if (name != NULL)
    287 		m_free(name);
    288 	if (error != 0) {
    289 		fd = (int)*retval;
    290 		if (fd_getfile(fd) != NULL)
    291 			(void)fd_close(fd);
    292 	}
    293 	return error;
    294 }
    295 
    296 int
    297 sys_paccept(struct lwp *l, const struct sys_paccept_args *uap,
    298     register_t *retval)
    299 {
    300 	/* {
    301 		syscallarg(int)			s;
    302 		syscallarg(struct sockaddr *)	name;
    303 		syscallarg(unsigned int *)	anamelen;
    304 		syscallarg(const sigset_t *)	mask;
    305 		syscallarg(int)			flags;
    306 	} */
    307 	int error, fd;
    308 	struct mbuf *name;
    309 	sigset_t *mask, amask;
    310 
    311 	if (SCARG(uap, mask) != NULL) {
    312 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    313 		if (error)
    314 			return error;
    315 		mask = &amask;
    316 	} else
    317 		mask = NULL;
    318 
    319 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, mask,
    320 	    SCARG(uap, flags), FNONBLOCK);
    321 	if (error != 0)
    322 		return error;
    323 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    324 	    MSG_LENUSRSPACE, name);
    325 	if (name != NULL)
    326 		m_free(name);
    327 	if (error != 0) {
    328 		fd = (int)*retval;
    329 		if (fd_getfile(fd) != NULL)
    330 			(void)fd_close(fd);
    331 	}
    332 	return error;
    333 }
    334 
    335 /* ARGSUSED */
    336 int
    337 sys_connect(struct lwp *l, const struct sys_connect_args *uap, register_t *retval)
    338 {
    339 	/* {
    340 		syscallarg(int)				s;
    341 		syscallarg(const struct sockaddr *)	name;
    342 		syscallarg(unsigned int)		namelen;
    343 	} */
    344 	int		error;
    345 	struct mbuf	*nam;
    346 
    347 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    348 	    MT_SONAME);
    349 	if (error)
    350 		return error;
    351 	return do_sys_connect(l,  SCARG(uap, s), nam);
    352 }
    353 
    354 int
    355 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam)
    356 {
    357 	struct socket	*so;
    358 	int		error;
    359 	int		interrupted = 0;
    360 
    361 	if ((error = fd_getsock(fd, &so)) != 0) {
    362 		m_freem(nam);
    363 		return (error);
    364 	}
    365 	solock(so);
    366 	MCLAIM(nam, so->so_mowner);
    367 	if ((so->so_state & SS_ISCONNECTING) != 0) {
    368 		error = EALREADY;
    369 		goto out;
    370 	}
    371 
    372 	error = soconnect(so, nam, l);
    373 	if (error)
    374 		goto bad;
    375 	if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) ==
    376 	    (SS_NBIO|SS_ISCONNECTING)) {
    377 		error = EINPROGRESS;
    378 		goto out;
    379 	}
    380 	while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) {
    381 		error = sowait(so, true, 0);
    382 		if (__predict_false((so->so_state & SS_ISABORTING) != 0)) {
    383 			error = EPIPE;
    384 			interrupted = 1;
    385 			break;
    386 		}
    387 		if (error) {
    388 			if (error == EINTR || error == ERESTART)
    389 				interrupted = 1;
    390 			break;
    391 		}
    392 	}
    393 	if (error == 0) {
    394 		error = so->so_error;
    395 		so->so_error = 0;
    396 	}
    397  bad:
    398 	if (!interrupted)
    399 		so->so_state &= ~SS_ISCONNECTING;
    400 	if (error == ERESTART)
    401 		error = EINTR;
    402  out:
    403  	sounlock(so);
    404  	fd_putfile(fd);
    405 	m_freem(nam);
    406 	return (error);
    407 }
    408 
    409 static int
    410 makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type,
    411     int domain, int proto, struct socket *soo)
    412 {
    413 	int error;
    414 	struct socket *so;
    415 
    416 	if ((error = socreate(domain, &so, type, proto, l, soo)) != 0)
    417 		return error;
    418 
    419 	if ((error = fd_allocfile(fp, fd)) != 0) {
    420 		soclose(so);
    421 		return error;
    422 	}
    423 	fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0);
    424 	(*fp)->f_flag = FREAD|FWRITE|
    425 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    426 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    427 	(*fp)->f_type = DTYPE_SOCKET;
    428 	(*fp)->f_ops = &socketops;
    429 	(*fp)->f_data = so;
    430 	if (flags & SOCK_NONBLOCK)
    431 		so->so_state |= SS_NBIO;
    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 /* ARGSUSED */
   1116 int
   1117 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, 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 /* ARGSUSED */
   1136 int
   1137 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, 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 /* ARGSUSED */
   1183 int
   1184 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, 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 /* ARGSUSED */
   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 && (so->so_state & SS_ISCONNECTED) == 0) {
   1308 		error = ENOTCONN;
   1309 	} else {
   1310 		*nam = m;
   1311 		error = (*so->so_proto->pr_usrreq)(so, which, NULL, m, NULL,
   1312 		    NULL);
   1313 	}
   1314  	sounlock(so);
   1315 	if (error != 0)
   1316 		m_free(m);
   1317  	fd_putfile(fd);
   1318 	return error;
   1319 }
   1320 
   1321 int
   1322 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
   1323     struct mbuf *addr)
   1324 {
   1325 	int len;
   1326 	int error;
   1327 
   1328 	if (asa == NULL)
   1329 		/* Assume application not interested */
   1330 		return 0;
   1331 
   1332 	if (flags & MSG_LENUSRSPACE) {
   1333 		error = copyin(alen, &len, sizeof(len));
   1334 		if (error)
   1335 			return error;
   1336 	} else
   1337 		len = *alen;
   1338 	if (len < 0)
   1339 		return EINVAL;
   1340 
   1341 	if (addr == NULL) {
   1342 		len = 0;
   1343 		error = 0;
   1344 	} else {
   1345 		if (len > addr->m_len)
   1346 			len = addr->m_len;
   1347 		/* Maybe this ought to copy a chain ? */
   1348 		ktrkuser(mbuftypes[MT_SONAME], mtod(addr, void *), len);
   1349 		error = copyout(mtod(addr, void *), asa, len);
   1350 	}
   1351 
   1352 	if (error == 0) {
   1353 		if (flags & MSG_LENUSRSPACE)
   1354 			error = copyout(&len, alen, sizeof(len));
   1355 		else
   1356 			*alen = len;
   1357 	}
   1358 
   1359 	return error;
   1360 }
   1361 
   1362 /*
   1363  * Get socket name.
   1364  */
   1365 /* ARGSUSED */
   1366 int
   1367 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval)
   1368 {
   1369 	/* {
   1370 		syscallarg(int)			fdes;
   1371 		syscallarg(struct sockaddr *)	asa;
   1372 		syscallarg(unsigned int *)	alen;
   1373 	} */
   1374 	struct mbuf	*m;
   1375 	int		error;
   1376 
   1377 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
   1378 	if (error != 0)
   1379 		return error;
   1380 
   1381 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1382 	    MSG_LENUSRSPACE, m);
   1383 	if (m != NULL)
   1384 		m_free(m);
   1385 	return error;
   1386 }
   1387 
   1388 /*
   1389  * Get name of peer for connected socket.
   1390  */
   1391 /* ARGSUSED */
   1392 int
   1393 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval)
   1394 {
   1395 	/* {
   1396 		syscallarg(int)			fdes;
   1397 		syscallarg(struct sockaddr *)	asa;
   1398 		syscallarg(unsigned int *)	alen;
   1399 	} */
   1400 	struct mbuf	*m;
   1401 	int		error;
   1402 
   1403 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
   1404 	if (error != 0)
   1405 		return error;
   1406 
   1407 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1408 	    MSG_LENUSRSPACE, m);
   1409 	if (m != NULL)
   1410 		m_free(m);
   1411 	return error;
   1412 }
   1413 
   1414 /*
   1415  * XXX In a perfect world, we wouldn't pass around socket control
   1416  * XXX arguments in mbufs, and this could go away.
   1417  */
   1418 int
   1419 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
   1420 {
   1421 	struct sockaddr	*sa;
   1422 	struct mbuf	*m;
   1423 	int		error;
   1424 
   1425 	/*
   1426 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1427 	 * will overflow sa_len.  Control data more than a page size in
   1428 	 * length is just too much.
   1429 	 */
   1430 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1431 		return (EINVAL);
   1432 
   1433 	/* Allocate an mbuf to hold the arguments. */
   1434 	m = m_get(M_WAIT, type);
   1435 	/* can't claim.  don't who to assign it to. */
   1436 	if (buflen > MLEN) {
   1437 		/*
   1438 		 * Won't fit into a regular mbuf, so we allocate just
   1439 		 * enough external storage to hold the argument.
   1440 		 */
   1441 		MEXTMALLOC(m, buflen, M_WAITOK);
   1442 	}
   1443 	m->m_len = buflen;
   1444 	error = copyin(bf, mtod(m, void *), buflen);
   1445 	if (error) {
   1446 		(void) m_free(m);
   1447 		return (error);
   1448 	}
   1449 	ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
   1450 	*mp = m;
   1451 	if (type == MT_SONAME) {
   1452 		sa = mtod(m, struct sockaddr *);
   1453 #if BYTE_ORDER != BIG_ENDIAN
   1454 		/*
   1455 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1456 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1457 		 */
   1458 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1459 			sa->sa_family = sa->sa_len;
   1460 #endif
   1461 		sa->sa_len = buflen;
   1462 	}
   1463 	return (0);
   1464 }
   1465