Home | History | Annotate | Line # | Download | only in kern
uipc_syscalls.c revision 1.147.2.3
      1 /*	$NetBSD: uipc_syscalls.c,v 1.147.2.3 2012/10/30 17:22:37 yamt 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.147.2.3 2012/10/30 17:22:37 yamt 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 	*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 	error = soaccept(so2, nam);
    238 	so2->so_cred = kauth_cred_dup(so->so_cred);
    239 	sounlock(so);
    240 	if (error) {
    241 		/* an error occurred, free the file descriptor and mbuf */
    242 		m_freem(nam);
    243 		mutex_enter(&fp2->f_lock);
    244 		fp2->f_count++;
    245 		mutex_exit(&fp2->f_lock);
    246 		closef(fp2);
    247 		fd_abort(curproc, NULL, fd);
    248 	} else {
    249 		fd_set_exclose(l, fd, (flags & SOCK_CLOEXEC) != 0);
    250 		fd_affix(curproc, fp2, fd);
    251 		*name = nam;
    252 	}
    253 	fd_putfile(sock);
    254 	if (__predict_false(mask))
    255 		sigsuspendteardown(l);
    256 	return (error);
    257  bad:
    258  	sounlock(so);
    259  	m_freem(nam);
    260 	fd_putfile(sock);
    261  	fd_abort(curproc, fp2, fd);
    262 	if (__predict_false(mask))
    263 		sigsuspendteardown(l);
    264  	return (error);
    265 }
    266 
    267 int
    268 sys_accept(struct lwp *l, const struct sys_accept_args *uap, register_t *retval)
    269 {
    270 	/* {
    271 		syscallarg(int)			s;
    272 		syscallarg(struct sockaddr *)	name;
    273 		syscallarg(unsigned int *)	anamelen;
    274 	} */
    275 	int error, fd;
    276 	struct mbuf *name;
    277 
    278 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, NULL, 0, 0);
    279 	if (error != 0)
    280 		return error;
    281 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    282 	    MSG_LENUSRSPACE, name);
    283 	if (name != NULL)
    284 		m_free(name);
    285 	if (error != 0) {
    286 		fd = (int)*retval;
    287 		if (fd_getfile(fd) != NULL)
    288 			(void)fd_close(fd);
    289 	}
    290 	return error;
    291 }
    292 
    293 int
    294 sys_paccept(struct lwp *l, const struct sys_paccept_args *uap,
    295     register_t *retval)
    296 {
    297 	/* {
    298 		syscallarg(int)			s;
    299 		syscallarg(struct sockaddr *)	name;
    300 		syscallarg(unsigned int *)	anamelen;
    301 		syscallarg(const sigset_t *)	mask;
    302 		syscallarg(int)			flags;
    303 	} */
    304 	int error, fd;
    305 	struct mbuf *name;
    306 	sigset_t *mask, amask;
    307 
    308 	if (SCARG(uap, mask) != NULL) {
    309 		error = copyin(SCARG(uap, mask), &amask, sizeof(amask));
    310 		if (error)
    311 			return error;
    312 		mask = &amask;
    313 	} else
    314 		mask = NULL;
    315 
    316 	error = do_sys_accept(l, SCARG(uap, s), &name, retval, mask,
    317 	    SCARG(uap, flags), FNONBLOCK);
    318 	if (error != 0)
    319 		return error;
    320 	error = copyout_sockname(SCARG(uap, name), SCARG(uap, anamelen),
    321 	    MSG_LENUSRSPACE, name);
    322 	if (name != NULL)
    323 		m_free(name);
    324 	if (error != 0) {
    325 		fd = (int)*retval;
    326 		if (fd_getfile(fd) != NULL)
    327 			(void)fd_close(fd);
    328 	}
    329 	return error;
    330 }
    331 
    332 /* ARGSUSED */
    333 int
    334 sys_connect(struct lwp *l, const struct sys_connect_args *uap, register_t *retval)
    335 {
    336 	/* {
    337 		syscallarg(int)				s;
    338 		syscallarg(const struct sockaddr *)	name;
    339 		syscallarg(unsigned int)		namelen;
    340 	} */
    341 	int		error;
    342 	struct mbuf	*nam;
    343 
    344 	error = sockargs(&nam, SCARG(uap, name), SCARG(uap, namelen),
    345 	    MT_SONAME);
    346 	if (error)
    347 		return error;
    348 	return do_sys_connect(l,  SCARG(uap, s), nam);
    349 }
    350 
    351 int
    352 do_sys_connect(struct lwp *l, int fd, struct mbuf *nam)
    353 {
    354 	struct socket	*so;
    355 	int		error;
    356 	int		interrupted = 0;
    357 
    358 	if ((error = fd_getsock(fd, &so)) != 0) {
    359 		m_freem(nam);
    360 		return (error);
    361 	}
    362 	solock(so);
    363 	MCLAIM(nam, so->so_mowner);
    364 	if ((so->so_state & SS_ISCONNECTING) != 0) {
    365 		error = EALREADY;
    366 		goto out;
    367 	}
    368 
    369 	error = soconnect(so, nam, l);
    370 	if (error)
    371 		goto bad;
    372 	if ((so->so_state & (SS_NBIO|SS_ISCONNECTING)) ==
    373 	    (SS_NBIO|SS_ISCONNECTING)) {
    374 		error = EINPROGRESS;
    375 		goto out;
    376 	}
    377 	while ((so->so_state & SS_ISCONNECTING) != 0 && so->so_error == 0) {
    378 		error = sowait(so, true, 0);
    379 		if (__predict_false((so->so_state & SS_ISABORTING) != 0)) {
    380 			error = EPIPE;
    381 			interrupted = 1;
    382 			break;
    383 		}
    384 		if (error) {
    385 			if (error == EINTR || error == ERESTART)
    386 				interrupted = 1;
    387 			break;
    388 		}
    389 	}
    390 	if (error == 0) {
    391 		error = so->so_error;
    392 		so->so_error = 0;
    393 	}
    394  bad:
    395 	if (!interrupted)
    396 		so->so_state &= ~SS_ISCONNECTING;
    397 	if (error == ERESTART)
    398 		error = EINTR;
    399  out:
    400  	sounlock(so);
    401  	fd_putfile(fd);
    402 	m_freem(nam);
    403 	return (error);
    404 }
    405 
    406 static int
    407 makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type,
    408     int domain, int proto, struct socket *soo)
    409 {
    410 	int error;
    411 	struct socket *so;
    412 
    413 	if ((error = socreate(domain, &so, type, proto, l, soo)) != 0)
    414 		return error;
    415 
    416 	if ((error = fd_allocfile(fp, fd)) != 0) {
    417 		soclose(so);
    418 		return error;
    419 	}
    420 	fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0);
    421 	(*fp)->f_flag = FREAD|FWRITE|
    422 	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
    423 	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
    424 	(*fp)->f_type = DTYPE_SOCKET;
    425 	(*fp)->f_ops = &socketops;
    426 	(*fp)->f_data = so;
    427 	return 0;
    428 }
    429 
    430 int
    431 sys_socketpair(struct lwp *l, const struct sys_socketpair_args *uap,
    432     register_t *retval)
    433 {
    434 	/* {
    435 		syscallarg(int)		domain;
    436 		syscallarg(int)		type;
    437 		syscallarg(int)		protocol;
    438 		syscallarg(int *)	rsv;
    439 	} */
    440 	file_t		*fp1, *fp2;
    441 	struct socket	*so1, *so2;
    442 	int		fd, error, sv[2];
    443 	proc_t		*p;
    444 	int		flags = SCARG(uap, type) & SOCK_FLAGS_MASK;
    445 	int		type = SCARG(uap, type) & ~SOCK_FLAGS_MASK;
    446 	int		domain = SCARG(uap, domain);
    447 	int		proto = SCARG(uap, protocol);
    448 
    449 	p = curproc;
    450 
    451 	error = makesocket(l, &fp1, &fd, flags, type, domain, proto, NULL);
    452 	if (error)
    453 		return error;
    454 	so1 = fp1->f_data;
    455 	sv[0] = fd;
    456 
    457 	error = makesocket(l, &fp2, &fd, flags, type, domain, proto, so1);
    458 	if (error)
    459 		goto out;
    460 	so2 = fp2->f_data;
    461 	sv[1] = fd;
    462 
    463 	solock(so1);
    464 	error = soconnect2(so1, so2);
    465 	if (error == 0 && type == SOCK_DGRAM) {
    466 		/*
    467 		 * Datagram socket connection is asymmetric.
    468 		 */
    469 		error = soconnect2(so2, so1);
    470 	}
    471 	sounlock(so1);
    472 
    473 	if (error == 0)
    474 		error = copyout(sv, SCARG(uap, rsv), sizeof(sv));
    475 	if (error == 0) {
    476 		fd_affix(p, fp2, sv[1]);
    477 		fd_affix(p, fp1, sv[0]);
    478 		return 0;
    479 	}
    480 	fd_abort(p, fp2, sv[1]);
    481 	(void)soclose(so2);
    482 out:
    483 	fd_abort(p, fp1, sv[0]);
    484 	(void)soclose(so1);
    485 	return error;
    486 }
    487 
    488 int
    489 sys_sendto(struct lwp *l, const struct sys_sendto_args *uap, register_t *retval)
    490 {
    491 	/* {
    492 		syscallarg(int)				s;
    493 		syscallarg(const void *)		buf;
    494 		syscallarg(size_t)			len;
    495 		syscallarg(int)				flags;
    496 		syscallarg(const struct sockaddr *)	to;
    497 		syscallarg(unsigned int)		tolen;
    498 	} */
    499 	struct msghdr	msg;
    500 	struct iovec	aiov;
    501 
    502 	msg.msg_name = __UNCONST(SCARG(uap, to)); /* XXXUNCONST kills const */
    503 	msg.msg_namelen = SCARG(uap, tolen);
    504 	msg.msg_iov = &aiov;
    505 	msg.msg_iovlen = 1;
    506 	msg.msg_control = NULL;
    507 	msg.msg_flags = 0;
    508 	aiov.iov_base = __UNCONST(SCARG(uap, buf)); /* XXXUNCONST kills const */
    509 	aiov.iov_len = SCARG(uap, len);
    510 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    511 }
    512 
    513 int
    514 sys_sendmsg(struct lwp *l, const struct sys_sendmsg_args *uap, register_t *retval)
    515 {
    516 	/* {
    517 		syscallarg(int)				s;
    518 		syscallarg(const struct msghdr *)	msg;
    519 		syscallarg(int)				flags;
    520 	} */
    521 	struct msghdr	msg;
    522 	int		error;
    523 
    524 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    525 	if (error)
    526 		return (error);
    527 
    528 	msg.msg_flags = MSG_IOVUSRSPACE;
    529 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, SCARG(uap, flags), retval);
    530 }
    531 
    532 static int
    533 do_sys_sendmsg_so(struct lwp *l, int s, struct socket *so, file_t *fp,
    534     struct msghdr *mp, int flags, register_t *retsize)
    535 {
    536 
    537 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    538 	struct mbuf	*to, *control;
    539 	struct uio	auio;
    540 	size_t		len, iovsz;
    541 	int		i, error;
    542 
    543 	ktrkuser("msghdr", mp, sizeof *mp);
    544 
    545 	/* If the caller passed us stuff in mbufs, we must free them. */
    546 	to = (mp->msg_flags & MSG_NAMEMBUF) ? mp->msg_name : NULL;
    547 	control = (mp->msg_flags & MSG_CONTROLMBUF) ? mp->msg_control : NULL;
    548 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    549 
    550 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    551 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    552 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    553 				error = EMSGSIZE;
    554 				goto bad;
    555 			}
    556 			iov = kmem_alloc(iovsz, KM_SLEEP);
    557 		}
    558 		if (mp->msg_iovlen != 0) {
    559 			error = copyin(mp->msg_iov, iov, iovsz);
    560 			if (error)
    561 				goto bad;
    562 		}
    563 		mp->msg_iov = iov;
    564 	}
    565 
    566 	auio.uio_iov = mp->msg_iov;
    567 	auio.uio_iovcnt = mp->msg_iovlen;
    568 	auio.uio_rw = UIO_WRITE;
    569 	auio.uio_offset = 0;			/* XXX */
    570 	auio.uio_resid = 0;
    571 	KASSERT(l == curlwp);
    572 	auio.uio_vmspace = l->l_proc->p_vmspace;
    573 
    574 	for (i = 0, tiov = mp->msg_iov; i < mp->msg_iovlen; i++, tiov++) {
    575 		/*
    576 		 * Writes return ssize_t because -1 is returned on error.
    577 		 * Therefore, we must restrict the length to SSIZE_MAX to
    578 		 * avoid garbage return values.
    579 		 */
    580 		auio.uio_resid += tiov->iov_len;
    581 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    582 			error = EINVAL;
    583 			goto bad;
    584 		}
    585 	}
    586 
    587 	if (mp->msg_name && to == NULL) {
    588 		error = sockargs(&to, mp->msg_name, mp->msg_namelen,
    589 		    MT_SONAME);
    590 		if (error)
    591 			goto bad;
    592 	}
    593 
    594 	if (mp->msg_control) {
    595 		if (mp->msg_controllen < CMSG_ALIGN(sizeof(struct cmsghdr))) {
    596 			error = EINVAL;
    597 			goto bad;
    598 		}
    599 		if (control == NULL) {
    600 			error = sockargs(&control, mp->msg_control,
    601 			    mp->msg_controllen, MT_CONTROL);
    602 			if (error)
    603 				goto bad;
    604 		}
    605 	}
    606 
    607 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
    608 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    609 		memcpy(ktriov, auio.uio_iov, iovsz);
    610 	}
    611 
    612 	if (mp->msg_name)
    613 		MCLAIM(to, so->so_mowner);
    614 	if (mp->msg_control)
    615 		MCLAIM(control, so->so_mowner);
    616 
    617 	len = auio.uio_resid;
    618 	error = (*so->so_send)(so, to, &auio, NULL, control, flags, l);
    619 	/* Protocol is responsible for freeing 'control' */
    620 	control = NULL;
    621 
    622 	if (error) {
    623 		if (auio.uio_resid != len && (error == ERESTART ||
    624 		    error == EINTR || error == EWOULDBLOCK))
    625 			error = 0;
    626 		if (error == EPIPE && (fp->f_flag & FNOSIGPIPE) == 0 &&
    627 		    (flags & MSG_NOSIGNAL) == 0) {
    628 			mutex_enter(proc_lock);
    629 			psignal(l->l_proc, SIGPIPE);
    630 			mutex_exit(proc_lock);
    631 		}
    632 	}
    633 	if (error == 0)
    634 		*retsize = len - auio.uio_resid;
    635 
    636 bad:
    637 	if (ktrpoint(KTR_GENIO)) {
    638 		ktrgeniov(s, UIO_WRITE, ktriov, *retsize, error);
    639 		if (ktriov != NULL)
    640 			kmem_free(ktriov, iovsz);
    641 	}
    642 
    643  	if (iov != aiov)
    644  		kmem_free(iov, iovsz);
    645 	if (to)
    646 		m_freem(to);
    647 	if (control)
    648 		m_freem(control);
    649 
    650 	return (error);
    651 }
    652 
    653 int
    654 do_sys_sendmsg(struct lwp *l, int s, struct msghdr *mp, int flags,
    655     register_t *retsize)
    656 {
    657 	int		error;
    658 	struct socket	*so;
    659 	file_t		*fp;
    660 
    661 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
    662 		return error;
    663 	error = do_sys_sendmsg_so(l, s, so, fp, mp, flags, retsize);
    664 	fd_putfile(s);
    665 	return error;
    666 }
    667 
    668 int
    669 sys_recvfrom(struct lwp *l, const struct sys_recvfrom_args *uap, register_t *retval)
    670 {
    671 	/* {
    672 		syscallarg(int)			s;
    673 		syscallarg(void *)		buf;
    674 		syscallarg(size_t)		len;
    675 		syscallarg(int)			flags;
    676 		syscallarg(struct sockaddr *)	from;
    677 		syscallarg(unsigned int *)	fromlenaddr;
    678 	} */
    679 	struct msghdr	msg;
    680 	struct iovec	aiov;
    681 	int		error;
    682 	struct mbuf	*from;
    683 
    684 	msg.msg_name = NULL;
    685 	msg.msg_iov = &aiov;
    686 	msg.msg_iovlen = 1;
    687 	aiov.iov_base = SCARG(uap, buf);
    688 	aiov.iov_len = SCARG(uap, len);
    689 	msg.msg_control = NULL;
    690 	msg.msg_flags = SCARG(uap, flags) & MSG_USERFLAGS;
    691 
    692 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from, NULL, retval);
    693 	if (error != 0)
    694 		return error;
    695 
    696 	error = copyout_sockname(SCARG(uap, from), SCARG(uap, fromlenaddr),
    697 	    MSG_LENUSRSPACE, from);
    698 	if (from != NULL)
    699 		m_free(from);
    700 	return error;
    701 }
    702 
    703 int
    704 sys_recvmsg(struct lwp *l, const struct sys_recvmsg_args *uap, register_t *retval)
    705 {
    706 	/* {
    707 		syscallarg(int)			s;
    708 		syscallarg(struct msghdr *)	msg;
    709 		syscallarg(int)			flags;
    710 	} */
    711 	struct msghdr	msg;
    712 	int		error;
    713 	struct mbuf	*from, *control;
    714 
    715 	error = copyin(SCARG(uap, msg), &msg, sizeof(msg));
    716 	if (error)
    717 		return (error);
    718 
    719 	msg.msg_flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    720 
    721 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
    722 	    msg.msg_control != NULL ? &control : NULL, retval);
    723 	if (error != 0)
    724 		return error;
    725 
    726 	if (msg.msg_control != NULL)
    727 		error = copyout_msg_control(l, &msg, control);
    728 
    729 	if (error == 0)
    730 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
    731 			from);
    732 	if (from != NULL)
    733 		m_free(from);
    734 	if (error == 0) {
    735 		ktrkuser("msghdr", &msg, sizeof msg);
    736 		error = copyout(&msg, SCARG(uap, msg), sizeof(msg));
    737 	}
    738 
    739 	return (error);
    740 }
    741 
    742 int
    743 sys_sendmmsg(struct lwp *l, const struct sys_sendmmsg_args *uap,
    744     register_t *retval)
    745 {
    746 	/* {
    747 		syscallarg(int)			s;
    748 		syscallarg(struct mmsghdr *)	mmsg;
    749 		syscallarg(unsigned int)	vlen;
    750 		syscallarg(unsigned int)	flags;
    751 	} */
    752 	struct mmsghdr mmsg;
    753 	struct socket *so;
    754 	file_t *fp;
    755 	struct msghdr *msg = &mmsg.msg_hdr;
    756 	int error, s;
    757 	unsigned int vlen, flags, dg;
    758 
    759 	s = SCARG(uap, s);
    760 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
    761 		return error;
    762 
    763 	vlen = SCARG(uap, vlen);
    764 	if (vlen > 1024)
    765 		vlen = 1024;
    766 
    767 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
    768 
    769 	for (dg = 0; dg < vlen;) {
    770 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
    771 		if (error)
    772 			break;
    773 
    774 		msg->msg_flags = flags;
    775 
    776 		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
    777 		if (error)
    778 			break;
    779 
    780 		ktrkuser("msghdr", msg, sizeof *msg);
    781 		mmsg.msg_len = *retval;
    782 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
    783 		if (error)
    784 			break;
    785 		dg++;
    786 
    787 	}
    788 
    789 	*retval = dg;
    790 	if (error)
    791 		so->so_error = error;
    792 
    793 	fd_putfile(s);
    794 
    795 	/*
    796 	 * If we succeeded at least once, return 0, hopefully so->so_error
    797 	 * will catch it next time.
    798 	 */
    799 	if (dg)
    800 		return 0;
    801 	return error;
    802 }
    803 
    804 /*
    805  * Adjust for a truncated SCM_RIGHTS control message.
    806  *  This means closing any file descriptors that aren't present
    807  *  in the returned buffer.
    808  *  m is the mbuf holding the (already externalized) SCM_RIGHTS message.
    809  */
    810 static void
    811 free_rights(struct mbuf *m)
    812 {
    813 	int nfd;
    814 	int i;
    815 	int *fdv;
    816 
    817 	nfd = m->m_len < CMSG_SPACE(sizeof(int)) ? 0
    818 	    : (m->m_len - CMSG_SPACE(sizeof(int))) / sizeof(int) + 1;
    819 	fdv = (int *) CMSG_DATA(mtod(m,struct cmsghdr *));
    820 	for (i = 0; i < nfd; i++) {
    821 		if (fd_getfile(fdv[i]) != NULL)
    822 			(void)fd_close(fdv[i]);
    823 	}
    824 }
    825 
    826 void
    827 free_control_mbuf(struct lwp *l, struct mbuf *control, struct mbuf *uncopied)
    828 {
    829 	struct mbuf *next;
    830 	struct cmsghdr *cmsg;
    831 	bool do_free_rights = false;
    832 
    833 	while (control != NULL) {
    834 		cmsg = mtod(control, struct cmsghdr *);
    835 		if (control == uncopied)
    836 			do_free_rights = true;
    837 		if (do_free_rights && cmsg->cmsg_level == SOL_SOCKET
    838 		    && cmsg->cmsg_type == SCM_RIGHTS)
    839 			free_rights(control);
    840 		next = control->m_next;
    841 		m_free(control);
    842 		control = next;
    843 	}
    844 }
    845 
    846 /* Copy socket control/CMSG data to user buffer, frees the mbuf */
    847 int
    848 copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
    849 {
    850 	int i, len, error = 0;
    851 	struct cmsghdr *cmsg;
    852 	struct mbuf *m;
    853 	char *q;
    854 
    855 	len = mp->msg_controllen;
    856 	if (len <= 0 || control == 0) {
    857 		mp->msg_controllen = 0;
    858 		free_control_mbuf(l, control, control);
    859 		return 0;
    860 	}
    861 
    862 	q = (char *)mp->msg_control;
    863 
    864 	for (m = control; m != NULL; ) {
    865 		cmsg = mtod(m, struct cmsghdr *);
    866 		i = m->m_len;
    867 		if (len < i) {
    868 			mp->msg_flags |= MSG_CTRUNC;
    869 			if (cmsg->cmsg_level == SOL_SOCKET
    870 			    && cmsg->cmsg_type == SCM_RIGHTS)
    871 				/* Do not truncate me ... */
    872 				break;
    873 			i = len;
    874 		}
    875 		error = copyout(mtod(m, void *), q, i);
    876 		ktrkuser("msgcontrol", mtod(m, void *), i);
    877 		if (error != 0) {
    878 			/* We must free all the SCM_RIGHTS */
    879 			m = control;
    880 			break;
    881 		}
    882 		m = m->m_next;
    883 		if (m)
    884 			i = ALIGN(i);
    885 		q += i;
    886 		len -= i;
    887 		if (len <= 0)
    888 			break;
    889 	}
    890 
    891 	free_control_mbuf(l, control, m);
    892 
    893 	mp->msg_controllen = q - (char *)mp->msg_control;
    894 	return error;
    895 }
    896 
    897 static int
    898 do_sys_recvmsg_so(struct lwp *l, int s, struct socket *so, struct msghdr *mp,
    899     struct mbuf **from, struct mbuf **control, register_t *retsize)
    900 {
    901 	struct iovec	aiov[UIO_SMALLIOV], *iov = aiov, *tiov, *ktriov = NULL;
    902 	struct uio	auio;
    903 	size_t		len, iovsz;
    904 	int		i, error;
    905 
    906 	ktrkuser("msghdr", mp, sizeof *mp);
    907 
    908 	*from = NULL;
    909 	if (control != NULL)
    910 		*control = NULL;
    911 
    912 	iovsz = mp->msg_iovlen * sizeof(struct iovec);
    913 
    914 	if (mp->msg_flags & MSG_IOVUSRSPACE) {
    915 		if ((unsigned int)mp->msg_iovlen > UIO_SMALLIOV) {
    916 			if ((unsigned int)mp->msg_iovlen > IOV_MAX) {
    917 				error = EMSGSIZE;
    918 				goto out;
    919 			}
    920 			iov = kmem_alloc(iovsz, KM_SLEEP);
    921 		}
    922 		if (mp->msg_iovlen != 0) {
    923 			error = copyin(mp->msg_iov, iov, iovsz);
    924 			if (error)
    925 				goto out;
    926 		}
    927 		auio.uio_iov = iov;
    928 	} else
    929 		auio.uio_iov = mp->msg_iov;
    930 	auio.uio_iovcnt = mp->msg_iovlen;
    931 	auio.uio_rw = UIO_READ;
    932 	auio.uio_offset = 0;			/* XXX */
    933 	auio.uio_resid = 0;
    934 	KASSERT(l == curlwp);
    935 	auio.uio_vmspace = l->l_proc->p_vmspace;
    936 
    937 	tiov = auio.uio_iov;
    938 	for (i = 0; i < mp->msg_iovlen; i++, tiov++) {
    939 		/*
    940 		 * Reads return ssize_t because -1 is returned on error.
    941 		 * Therefore we must restrict the length to SSIZE_MAX to
    942 		 * avoid garbage return values.
    943 		 */
    944 		auio.uio_resid += tiov->iov_len;
    945 		if (tiov->iov_len > SSIZE_MAX || auio.uio_resid > SSIZE_MAX) {
    946 			error = EINVAL;
    947 			goto out;
    948 		}
    949 	}
    950 
    951 	if (ktrpoint(KTR_GENIO) && iovsz > 0) {
    952 		ktriov = kmem_alloc(iovsz, KM_SLEEP);
    953 		memcpy(ktriov, auio.uio_iov, iovsz);
    954 	}
    955 
    956 	len = auio.uio_resid;
    957 	mp->msg_flags &= MSG_USERFLAGS;
    958 	error = (*so->so_receive)(so, from, &auio, NULL, control,
    959 	    &mp->msg_flags);
    960 	len -= auio.uio_resid;
    961 	*retsize = len;
    962 	if (error != 0 && len != 0
    963 	    && (error == ERESTART || error == EINTR || error == EWOULDBLOCK))
    964 		/* Some data transferred */
    965 		error = 0;
    966 
    967 	if (ktrpoint(KTR_GENIO)) {
    968 		ktrgeniov(s, UIO_READ, ktriov, len, error);
    969 		if (ktriov != NULL)
    970 			kmem_free(ktriov, iovsz);
    971 	}
    972 
    973 	if (error != 0) {
    974 		m_freem(*from);
    975 		*from = NULL;
    976 		if (control != NULL) {
    977 			free_control_mbuf(l, *control, *control);
    978 			*control = NULL;
    979 		}
    980 	}
    981  out:
    982 	if (iov != aiov)
    983 		kmem_free(iov, iovsz);
    984 	return (error);
    985 }
    986 
    987 
    988 int
    989 do_sys_recvmsg(struct lwp *l, int s, struct msghdr *mp, struct mbuf **from,
    990     struct mbuf **control, register_t *retsize)
    991 {
    992 	int error;
    993 	struct socket *so;
    994 
    995 	if ((error = fd_getsock(s, &so)) != 0)
    996 		return error;
    997 	error = do_sys_recvmsg_so(l, s, so, mp, from, control, retsize);
    998 	fd_putfile(s);
    999 	return error;
   1000 }
   1001 
   1002 int
   1003 sys_recvmmsg(struct lwp *l, const struct sys_recvmmsg_args *uap,
   1004     register_t *retval)
   1005 {
   1006 	/* {
   1007 		syscallarg(int)			s;
   1008 		syscallarg(struct mmsghdr *)	mmsg;
   1009 		syscallarg(unsigned int)	vlen;
   1010 		syscallarg(unsigned int)	flags;
   1011 		syscallarg(struct timespec *)	timeout;
   1012 	} */
   1013 	struct mmsghdr mmsg;
   1014 	struct socket *so;
   1015 	struct msghdr *msg = &mmsg.msg_hdr;
   1016 	int error, s;
   1017 	struct mbuf *from, *control;
   1018 	struct timespec ts, now;
   1019 	unsigned int vlen, flags, dg;
   1020 
   1021 	if (SCARG(uap, timeout)) {
   1022 		if ((error = copyin(SCARG(uap, timeout), &ts, sizeof(ts))) != 0)
   1023 			return error;
   1024 		getnanotime(&now);
   1025 		timespecadd(&now, &ts, &ts);
   1026 	}
   1027 
   1028 	s = SCARG(uap, s);
   1029 	if ((error = fd_getsock(s, &so)) != 0)
   1030 		return error;
   1031 
   1032 	vlen = SCARG(uap, vlen);
   1033 	if (vlen > 1024)
   1034 		vlen = 1024;
   1035 
   1036 	from = NULL;
   1037 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
   1038 
   1039 	for (dg = 0; dg < vlen;) {
   1040 		error = copyin(SCARG(uap, mmsg) + dg, &mmsg, sizeof(mmsg));
   1041 		if (error)
   1042 			break;
   1043 
   1044 		msg->msg_flags = flags & ~MSG_WAITFORONE;
   1045 
   1046 		if (from != NULL) {
   1047 			m_free(from);
   1048 			from = NULL;
   1049 		}
   1050 
   1051 		error = do_sys_recvmsg_so(l, s, so, msg, &from,
   1052 		    msg->msg_control != NULL ? &control : NULL, retval);
   1053 		if (error)
   1054 			break;
   1055 
   1056 		if (msg->msg_control != NULL)
   1057 			error = copyout_msg_control(l, msg, control);
   1058 		if (error)
   1059 			break;
   1060 
   1061 		error = copyout_sockname(msg->msg_name, &msg->msg_namelen, 0,
   1062 		    from);
   1063 		if (error)
   1064 			break;
   1065 
   1066 		ktrkuser("msghdr", msg, sizeof *msg);
   1067 		mmsg.msg_len = *retval;
   1068 
   1069 		error = copyout(&mmsg, SCARG(uap, mmsg) + dg, sizeof(mmsg));
   1070 		if (error)
   1071 			break;
   1072 
   1073 		dg++;
   1074 		if (msg->msg_flags & MSG_OOB)
   1075 			break;
   1076 
   1077 		if (SCARG(uap, timeout)) {
   1078 			getnanotime(&now);
   1079 			timespecsub(&now, &ts, &now);
   1080 			if (now.tv_sec > 0)
   1081 				break;
   1082 		}
   1083 
   1084 		if (flags & MSG_WAITFORONE)
   1085 			flags |= MSG_DONTWAIT;
   1086 
   1087 	}
   1088 
   1089 	if (from != NULL)
   1090 		m_free(from);
   1091 
   1092 	*retval = dg;
   1093 	if (error)
   1094 		so->so_error = error;
   1095 
   1096 	fd_putfile(s);
   1097 
   1098 	/*
   1099 	 * If we succeeded at least once, return 0, hopefully so->so_error
   1100 	 * will catch it next time.
   1101 	 */
   1102 	if (dg)
   1103 		return 0;
   1104 
   1105 	return error;
   1106 }
   1107 
   1108 /* ARGSUSED */
   1109 int
   1110 sys_shutdown(struct lwp *l, const struct sys_shutdown_args *uap, register_t *retval)
   1111 {
   1112 	/* {
   1113 		syscallarg(int)	s;
   1114 		syscallarg(int)	how;
   1115 	} */
   1116 	struct socket	*so;
   1117 	int		error;
   1118 
   1119 	if ((error = fd_getsock(SCARG(uap, s), &so)) != 0)
   1120 		return (error);
   1121 	solock(so);
   1122 	error = soshutdown(so, SCARG(uap, how));
   1123 	sounlock(so);
   1124 	fd_putfile(SCARG(uap, s));
   1125 	return (error);
   1126 }
   1127 
   1128 /* ARGSUSED */
   1129 int
   1130 sys_setsockopt(struct lwp *l, const struct sys_setsockopt_args *uap, register_t *retval)
   1131 {
   1132 	/* {
   1133 		syscallarg(int)			s;
   1134 		syscallarg(int)			level;
   1135 		syscallarg(int)			name;
   1136 		syscallarg(const void *)	val;
   1137 		syscallarg(unsigned int)	valsize;
   1138 	} */
   1139 	struct sockopt	sopt;
   1140 	struct socket	*so;
   1141 	file_t		*fp;
   1142 	int		error;
   1143 	unsigned int	len;
   1144 
   1145 	len = SCARG(uap, valsize);
   1146 	if (len > 0 && SCARG(uap, val) == NULL)
   1147 		return (EINVAL);
   1148 
   1149 	if (len > MCLBYTES)
   1150 		return (EINVAL);
   1151 
   1152 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
   1153 		return (error);
   1154 
   1155 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), len);
   1156 
   1157 	if (len > 0) {
   1158 		error = copyin(SCARG(uap, val), sopt.sopt_data, len);
   1159 		if (error)
   1160 			goto out;
   1161 	}
   1162 
   1163 	error = sosetopt(so, &sopt);
   1164 	if (so->so_options & SO_NOSIGPIPE)
   1165 		atomic_or_uint(&fp->f_flag, FNOSIGPIPE);
   1166 	else
   1167 		atomic_and_uint(&fp->f_flag, ~FNOSIGPIPE);
   1168 
   1169  out:
   1170 	sockopt_destroy(&sopt);
   1171  	fd_putfile(SCARG(uap, s));
   1172 	return (error);
   1173 }
   1174 
   1175 /* ARGSUSED */
   1176 int
   1177 sys_getsockopt(struct lwp *l, const struct sys_getsockopt_args *uap, register_t *retval)
   1178 {
   1179 	/* {
   1180 		syscallarg(int)			s;
   1181 		syscallarg(int)			level;
   1182 		syscallarg(int)			name;
   1183 		syscallarg(void *)		val;
   1184 		syscallarg(unsigned int *)	avalsize;
   1185 	} */
   1186 	struct sockopt	sopt;
   1187 	struct socket	*so;
   1188 	file_t		*fp;
   1189 	unsigned int	valsize, len;
   1190 	int		error;
   1191 
   1192 	if (SCARG(uap, val) != NULL) {
   1193 		error = copyin(SCARG(uap, avalsize), &valsize, sizeof(valsize));
   1194 		if (error)
   1195 			return (error);
   1196 	} else
   1197 		valsize = 0;
   1198 
   1199 	if ((error = fd_getsock1(SCARG(uap, s), &so, &fp)) != 0)
   1200 		return (error);
   1201 
   1202 	sockopt_init(&sopt, SCARG(uap, level), SCARG(uap, name), 0);
   1203 
   1204 	if (fp->f_flag & FNOSIGPIPE)
   1205 		so->so_options |= SO_NOSIGPIPE;
   1206 	else
   1207 		so->so_options &= ~SO_NOSIGPIPE;
   1208 	error = sogetopt(so, &sopt);
   1209 	if (error)
   1210 		goto out;
   1211 
   1212 	if (valsize > 0) {
   1213 		len = min(valsize, sopt.sopt_size);
   1214 		error = copyout(sopt.sopt_data, SCARG(uap, val), len);
   1215 		if (error)
   1216 			goto out;
   1217 
   1218 		error = copyout(&len, SCARG(uap, avalsize), sizeof(len));
   1219 		if (error)
   1220 			goto out;
   1221 	}
   1222 
   1223  out:
   1224 	sockopt_destroy(&sopt);
   1225  	fd_putfile(SCARG(uap, s));
   1226 	return (error);
   1227 }
   1228 
   1229 #ifdef PIPE_SOCKETPAIR
   1230 /* ARGSUSED */
   1231 int
   1232 pipe1(struct lwp *l, register_t *retval, int flags)
   1233 {
   1234 	file_t		*rf, *wf;
   1235 	struct socket	*rso, *wso;
   1236 	int		fd, error;
   1237 	proc_t		*p;
   1238 
   1239 	if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
   1240 		return EINVAL;
   1241 	p = curproc;
   1242 	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
   1243 		return (error);
   1244 	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
   1245 		goto free1;
   1246 	/* remember this socket pair implements a pipe */
   1247 	wso->so_state |= SS_ISAPIPE;
   1248 	rso->so_state |= SS_ISAPIPE;
   1249 	if ((error = fd_allocfile(&rf, &fd)) != 0)
   1250 		goto free2;
   1251 	retval[0] = fd;
   1252 	rf->f_flag = FREAD | flags;
   1253 	rf->f_type = DTYPE_SOCKET;
   1254 	rf->f_ops = &socketops;
   1255 	rf->f_data = rso;
   1256 	if ((error = fd_allocfile(&wf, &fd)) != 0)
   1257 		goto free3;
   1258 	wf->f_flag = FWRITE | flags;
   1259 	wf->f_type = DTYPE_SOCKET;
   1260 	wf->f_ops = &socketops;
   1261 	wf->f_data = wso;
   1262 	retval[1] = fd;
   1263 	solock(wso);
   1264 	error = unp_connect2(wso, rso, PRU_CONNECT2);
   1265 	sounlock(wso);
   1266 	if (error != 0)
   1267 		goto free4;
   1268 	fd_affix(p, wf, (int)retval[1]);
   1269 	fd_affix(p, rf, (int)retval[0]);
   1270 	return (0);
   1271  free4:
   1272 	fd_abort(p, wf, (int)retval[1]);
   1273  free3:
   1274 	fd_abort(p, rf, (int)retval[0]);
   1275  free2:
   1276 	(void)soclose(wso);
   1277  free1:
   1278 	(void)soclose(rso);
   1279 	return (error);
   1280 }
   1281 #endif /* PIPE_SOCKETPAIR */
   1282 
   1283 /*
   1284  * Get socket name.
   1285  */
   1286 /* ARGSUSED */
   1287 int
   1288 do_sys_getsockname(struct lwp *l, int fd, int which, struct mbuf **nam)
   1289 {
   1290 	struct socket	*so;
   1291 	struct mbuf	*m;
   1292 	int		error;
   1293 
   1294 	if ((error = fd_getsock(fd, &so)) != 0)
   1295 		return error;
   1296 
   1297 	m = m_getclr(M_WAIT, MT_SONAME);
   1298 	MCLAIM(m, so->so_mowner);
   1299 
   1300 	solock(so);
   1301 	if (which == PRU_PEERADDR
   1302 	    && (so->so_state & (SS_ISCONNECTED | SS_ISCONFIRMING)) == 0) {
   1303 		error = ENOTCONN;
   1304 	} else {
   1305 		*nam = m;
   1306 		error = (*so->so_proto->pr_usrreq)(so, which, NULL, m, NULL,
   1307 		    NULL);
   1308 	}
   1309  	sounlock(so);
   1310 	if (error != 0)
   1311 		m_free(m);
   1312  	fd_putfile(fd);
   1313 	return error;
   1314 }
   1315 
   1316 int
   1317 copyout_sockname(struct sockaddr *asa, unsigned int *alen, int flags,
   1318     struct mbuf *addr)
   1319 {
   1320 	int len;
   1321 	int error;
   1322 
   1323 	if (asa == NULL)
   1324 		/* Assume application not interested */
   1325 		return 0;
   1326 
   1327 	if (flags & MSG_LENUSRSPACE) {
   1328 		error = copyin(alen, &len, sizeof(len));
   1329 		if (error)
   1330 			return error;
   1331 	} else
   1332 		len = *alen;
   1333 	if (len < 0)
   1334 		return EINVAL;
   1335 
   1336 	if (addr == NULL) {
   1337 		len = 0;
   1338 		error = 0;
   1339 	} else {
   1340 		if (len > addr->m_len)
   1341 			len = addr->m_len;
   1342 		/* Maybe this ought to copy a chain ? */
   1343 		ktrkuser("sockname", mtod(addr, void *), len);
   1344 		error = copyout(mtod(addr, void *), asa, len);
   1345 	}
   1346 
   1347 	if (error == 0) {
   1348 		if (flags & MSG_LENUSRSPACE)
   1349 			error = copyout(&len, alen, sizeof(len));
   1350 		else
   1351 			*alen = len;
   1352 	}
   1353 
   1354 	return error;
   1355 }
   1356 
   1357 /*
   1358  * Get socket name.
   1359  */
   1360 /* ARGSUSED */
   1361 int
   1362 sys_getsockname(struct lwp *l, const struct sys_getsockname_args *uap, register_t *retval)
   1363 {
   1364 	/* {
   1365 		syscallarg(int)			fdes;
   1366 		syscallarg(struct sockaddr *)	asa;
   1367 		syscallarg(unsigned int *)	alen;
   1368 	} */
   1369 	struct mbuf	*m;
   1370 	int		error;
   1371 
   1372 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_SOCKADDR, &m);
   1373 	if (error != 0)
   1374 		return error;
   1375 
   1376 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1377 	    MSG_LENUSRSPACE, m);
   1378 	if (m != NULL)
   1379 		m_free(m);
   1380 	return error;
   1381 }
   1382 
   1383 /*
   1384  * Get name of peer for connected socket.
   1385  */
   1386 /* ARGSUSED */
   1387 int
   1388 sys_getpeername(struct lwp *l, const struct sys_getpeername_args *uap, register_t *retval)
   1389 {
   1390 	/* {
   1391 		syscallarg(int)			fdes;
   1392 		syscallarg(struct sockaddr *)	asa;
   1393 		syscallarg(unsigned int *)	alen;
   1394 	} */
   1395 	struct mbuf	*m;
   1396 	int		error;
   1397 
   1398 	error = do_sys_getsockname(l, SCARG(uap, fdes), PRU_PEERADDR, &m);
   1399 	if (error != 0)
   1400 		return error;
   1401 
   1402 	error = copyout_sockname(SCARG(uap, asa), SCARG(uap, alen),
   1403 	    MSG_LENUSRSPACE, m);
   1404 	if (m != NULL)
   1405 		m_free(m);
   1406 	return error;
   1407 }
   1408 
   1409 /*
   1410  * XXX In a perfect world, we wouldn't pass around socket control
   1411  * XXX arguments in mbufs, and this could go away.
   1412  */
   1413 int
   1414 sockargs(struct mbuf **mp, const void *bf, size_t buflen, int type)
   1415 {
   1416 	struct sockaddr	*sa;
   1417 	struct mbuf	*m;
   1418 	int		error;
   1419 
   1420 	/*
   1421 	 * We can't allow socket names > UCHAR_MAX in length, since that
   1422 	 * will overflow sa_len.  Control data more than a page size in
   1423 	 * length is just too much.
   1424 	 */
   1425 	if (buflen > (type == MT_SONAME ? UCHAR_MAX : PAGE_SIZE))
   1426 		return (EINVAL);
   1427 
   1428 	/* Allocate an mbuf to hold the arguments. */
   1429 	m = m_get(M_WAIT, type);
   1430 	/* can't claim.  don't who to assign it to. */
   1431 	if (buflen > MLEN) {
   1432 		/*
   1433 		 * Won't fit into a regular mbuf, so we allocate just
   1434 		 * enough external storage to hold the argument.
   1435 		 */
   1436 		MEXTMALLOC(m, buflen, M_WAITOK);
   1437 	}
   1438 	m->m_len = buflen;
   1439 	error = copyin(bf, mtod(m, void *), buflen);
   1440 	if (error) {
   1441 		(void) m_free(m);
   1442 		return (error);
   1443 	}
   1444 	ktrkuser(mbuftypes[type], mtod(m, void *), buflen);
   1445 	*mp = m;
   1446 	if (type == MT_SONAME) {
   1447 		sa = mtod(m, struct sockaddr *);
   1448 #if BYTE_ORDER != BIG_ENDIAN
   1449 		/*
   1450 		 * 4.3BSD compat thing - need to stay, since bind(2),
   1451 		 * connect(2), sendto(2) were not versioned for COMPAT_43.
   1452 		 */
   1453 		if (sa->sa_family == 0 && sa->sa_len < AF_MAX)
   1454 			sa->sa_family = sa->sa_len;
   1455 #endif
   1456 		sa->sa_len = buflen;
   1457 	}
   1458 	return (0);
   1459 }
   1460