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