Home | History | Annotate | Line # | Download | only in common
      1 /*	$NetBSD: linux_socket.c,v 1.163 2026/09/20 13:43:51 riastradh Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Frank van der Linden and Eric Haszlakiewicz.
      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  * Functions in multiarch:
     34  *	linux_sys_socketcall		: linux_socketcall.c
     35  */
     36 
     37 #include <sys/cdefs.h>
     38 __KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.163 2026/09/20 13:43:51 riastradh Exp $");
     39 
     40 #if defined(_KERNEL_OPT)
     41 #include "opt_inet.h"
     42 #endif /* defined(_KERNEL_OPT) */
     43 
     44 #include <sys/param.h>
     45 #include <sys/kernel.h>
     46 #include <sys/systm.h>
     47 #include <sys/buf.h>
     48 #include <sys/ioctl.h>
     49 #include <sys/tty.h>
     50 #include <sys/file.h>
     51 #include <sys/filedesc.h>
     52 #include <sys/select.h>
     53 #include <sys/socket.h>
     54 #include <sys/socketvar.h>
     55 #include <sys/domain.h>
     56 #include <net/if.h>
     57 #include <net/if_dl.h>
     58 #include <net/if_types.h>
     59 #include <netinet/in.h>
     60 #include <netinet/tcp.h>
     61 #include <sys/mount.h>
     62 #include <sys/proc.h>
     63 #include <sys/vnode.h>
     64 #include <sys/device.h>
     65 #include <sys/protosw.h>
     66 #include <sys/mbuf.h>
     67 #include <sys/syslog.h>
     68 #include <sys/exec.h>
     69 #include <sys/kauth.h>
     70 #include <sys/syscallargs.h>
     71 #include <sys/ktrace.h>
     72 
     73 #include <lib/libkern/libkern.h>
     74 
     75 #include <netinet/ip6.h>
     76 #include <netinet6/ip6_var.h>
     77 
     78 #include <compat/sys/socket.h>
     79 #include <compat/sys/sockio.h>
     80 
     81 #include <compat/linux/common/linux_types.h>
     82 #include <compat/linux/common/linux_util.h>
     83 #include <compat/linux/common/linux_signal.h>
     84 #include <compat/linux/common/linux_ioctl.h>
     85 #include <compat/linux/common/linux_sched.h>
     86 #include <compat/linux/common/linux_socket.h>
     87 #include <compat/linux/common/linux_fcntl.h>
     88 #if !defined(__aarch64__) && !defined(__alpha__) && !defined(__amd64__)
     89 #include <compat/linux/common/linux_socketcall.h>
     90 #endif
     91 #include <compat/linux/common/linux_sockio.h>
     92 #include <compat/linux/common/linux_ipc.h>
     93 #include <compat/linux/common/linux_sem.h>
     94 
     95 #include <compat/linux/linux_syscallargs.h>
     96 
     97 #ifdef DEBUG_LINUX
     98 #define DPRINTF(a) uprintf a
     99 #else
    100 #define DPRINTF(a)
    101 #endif
    102 
    103 /*
    104  * The calls in this file are entered either via the linux_socketcall()
    105  * interface or, on the Alpha, as individual syscalls.  The
    106  * linux_socketcall function does any massaging of arguments so that all
    107  * the calls in here need not think that they are anything other
    108  * than a normal syscall.
    109  */
    110 
    111 static int linux_to_bsd_domain(int);
    112 static int bsd_to_linux_domain(int);
    113 static int linux_to_bsd_type(int);
    114 int linux_to_bsd_sopt_level(int);
    115 int linux_to_bsd_so_sockopt(int);
    116 int linux_to_bsd_ip_sockopt(int);
    117 int linux_to_bsd_ipv6_sockopt(int);
    118 int linux_to_bsd_tcp_sockopt(int);
    119 int linux_to_bsd_udp_sockopt(int);
    120 int linux_getifname(struct lwp *, register_t *, void *);
    121 int linux_getifconf(struct lwp *, register_t *, void *);
    122 int linux_getifhwaddr(struct lwp *, register_t *, u_int, void *);
    123 static int linux_get_sa(struct lwp *, int, struct sockaddr_big *,
    124 		const struct osockaddr *, socklen_t);
    125 static int linux_sa_put(struct osockaddr *osa);
    126 static int linux_to_bsd_msg_flags(int);
    127 static int bsd_to_linux_msg_flags(int);
    128 static int linux_to_bsd_msghdr(const struct linux_msghdr *, struct msghdr *);
    129 static void bsd_to_linux_msghdr(const struct msghdr *, struct linux_msghdr *);
    130 
    131 static const int linux_to_bsd_domain_[LINUX_AF_MAX] = {
    132 	AF_UNSPEC,
    133 	AF_UNIX,
    134 	AF_INET,
    135 	AF_CCITT,	/* LINUX_AF_AX25 */
    136 	AF_IPX,
    137 	AF_APPLETALK,
    138 	-1,		/* LINUX_AF_NETROM */
    139 	-1,		/* LINUX_AF_BRIDGE */
    140 	-1,		/* LINUX_AF_ATMPVC */
    141 	AF_CCITT,	/* LINUX_AF_X25 */
    142 	AF_INET6,
    143 	-1,		/* LINUX_AF_ROSE */
    144 	AF_DECnet,
    145 	-1,		/* LINUX_AF_NETBEUI */
    146 	-1,		/* LINUX_AF_SECURITY */
    147 	pseudo_AF_KEY,
    148 	AF_ROUTE,	/* LINUX_AF_NETLINK */
    149 	-1,		/* LINUX_AF_PACKET */
    150 	-1,		/* LINUX_AF_ASH */
    151 	-1,		/* LINUX_AF_ECONET */
    152 	-1,		/* LINUX_AF_ATMSVC */
    153 	AF_SNA,
    154 	/* rest up to LINUX_AF_MAX-1 is not allocated */
    155 	-1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
    156 };
    157 
    158 static const int bsd_to_linux_domain_[AF_MAX] = {
    159 	LINUX_AF_UNSPEC,
    160 	LINUX_AF_UNIX,
    161 	LINUX_AF_INET,
    162 	-1,		/* AF_IMPLINK */
    163 	-1,		/* AF_PUP */
    164 	-1,		/* AF_CHAOS */
    165 	-1,		/* AF_NS */
    166 	-1,		/* AF_ISO */
    167 	-1,		/* AF_ECMA */
    168 	-1,		/* AF_DATAKIT */
    169 	LINUX_AF_AX25,	/* AF_CCITT */
    170 	LINUX_AF_SNA,
    171 	LINUX_AF_DECnet,
    172 	-1,		/* AF_DLI */
    173 	-1,		/* AF_LAT */
    174 	-1,		/* AF_HYLINK */
    175 	LINUX_AF_APPLETALK,
    176 	LINUX_AF_NETLINK,
    177 	-1,		/* AF_LINK */
    178 	-1,		/* AF_XTP */
    179 	-1,		/* AF_COIP */
    180 	-1,		/* AF_CNT */
    181 	-1,		/* pseudo_AF_RTIP */
    182 	LINUX_AF_IPX,
    183 	LINUX_AF_INET6,
    184 	-1,		/* pseudo_AF_PIP */
    185 	-1,		/* AF_ISDN */
    186 	-1,		/* AF_NATM */
    187 	-1,		/* AF_ARP */
    188 	LINUX_pseudo_AF_KEY,
    189 	-1,		/* pseudo_AF_HDRCMPLT */
    190 };
    191 
    192 static const struct {
    193 	int bfl;
    194 	int lfl;
    195 } bsd_to_linux_msg_flags_[] = {
    196 	{MSG_OOB,		LINUX_MSG_OOB},
    197 	{MSG_PEEK,		LINUX_MSG_PEEK},
    198 	{MSG_DONTROUTE,		LINUX_MSG_DONTROUTE},
    199 	{MSG_EOR,		LINUX_MSG_EOR},
    200 	{MSG_TRUNC,		LINUX_MSG_TRUNC},
    201 	{MSG_CTRUNC,		LINUX_MSG_CTRUNC},
    202 	{MSG_WAITALL,		LINUX_MSG_WAITALL},
    203 	{MSG_DONTWAIT,		LINUX_MSG_DONTWAIT},
    204 	{MSG_BCAST,		0},		/* not supported, clear */
    205 	{MSG_MCAST,		0},		/* not supported, clear */
    206 	{MSG_NOSIGNAL,		LINUX_MSG_NOSIGNAL},
    207 	{-1, /* not supp */	LINUX_MSG_PROBE},
    208 	{-1, /* not supp */	LINUX_MSG_FIN},
    209 	{-1, /* not supp */	LINUX_MSG_SYN},
    210 	{-1, /* not supp */	LINUX_MSG_CONFIRM},
    211 	{-1, /* not supp */	LINUX_MSG_RST},
    212 	{-1, /* not supp */	LINUX_MSG_ERRQUEUE},
    213 	{-1, /* not supp */	LINUX_MSG_MORE},
    214 };
    215 
    216 /*
    217  * Convert between Linux and BSD socket domain values
    218  */
    219 static int
    220 linux_to_bsd_domain(int ldom)
    221 {
    222 	if (ldom < 0 || ldom >= LINUX_AF_MAX)
    223 		return (-1);
    224 
    225 	return linux_to_bsd_domain_[ldom];
    226 }
    227 
    228 /*
    229  * Convert between BSD and Linux socket domain values
    230  */
    231 static int
    232 bsd_to_linux_domain(int bdom)
    233 {
    234 	if (bdom < 0 || bdom >= AF_MAX)
    235 		return (-1);
    236 
    237 	return bsd_to_linux_domain_[bdom];
    238 }
    239 
    240 static int
    241 linux_to_bsd_type(int ltype)
    242 {
    243 	int type, flags;
    244 
    245 	/* Real types are identical between Linux and NetBSD */
    246 	type = ltype & LINUX_SOCK_TYPE_MASK;
    247 
    248 	/* But flags are not .. */
    249 	flags = ltype & ~LINUX_SOCK_TYPE_MASK;
    250 	if (flags & ~(LINUX_SOCK_CLOEXEC|LINUX_SOCK_NONBLOCK))
    251 		return -1;
    252 
    253 	if (flags & LINUX_SOCK_CLOEXEC)
    254 		type |= SOCK_CLOEXEC;
    255 	if (flags & LINUX_SOCK_NONBLOCK)
    256 		type |= SOCK_NONBLOCK;
    257 
    258 	return type;
    259 }
    260 
    261 static int
    262 linux_to_bsd_msg_flags(int lflag)
    263 {
    264 	int i, lfl, bfl;
    265 	int bflag = 0;
    266 
    267 	if (lflag == 0)
    268 		return (0);
    269 
    270 	for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
    271 		bfl = bsd_to_linux_msg_flags_[i].bfl;
    272 		lfl = bsd_to_linux_msg_flags_[i].lfl;
    273 
    274 		if (lfl == 0)
    275 			continue;
    276 
    277 		if (lflag & lfl) {
    278 			if (bfl < 0)
    279 				return (-1);
    280 
    281 			bflag |= bfl;
    282 		}
    283 	}
    284 
    285 	return (bflag);
    286 }
    287 
    288 static int
    289 bsd_to_linux_msg_flags(int bflag)
    290 {
    291 	int i, lfl, bfl;
    292 	int lflag = 0;
    293 
    294 	if (bflag == 0)
    295 		return (0);
    296 
    297 	for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
    298 		bfl = bsd_to_linux_msg_flags_[i].bfl;
    299 		lfl = bsd_to_linux_msg_flags_[i].lfl;
    300 
    301 		if (bfl <= 0)
    302 			continue;
    303 
    304 		if (bflag & bfl) {
    305 			if (lfl < 0)
    306 				return (-1);
    307 
    308 			lflag |= lfl;
    309 		}
    310 	}
    311 
    312 	return (lflag);
    313 }
    314 
    315 int
    316 linux_sys_socket(struct lwp *l, const struct linux_sys_socket_args *uap, register_t *retval)
    317 {
    318 	/* {
    319 		syscallarg(int)	domain;
    320 		syscallarg(int)	type;
    321 		syscallarg(int) protocol;
    322 	} */
    323 	struct sys___socket30_args bsa;
    324 	int error;
    325 
    326 	memset(&bsa, 0, sizeof(bsa));
    327 	SCARG(&bsa, protocol) = SCARG(uap, protocol);
    328 	SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
    329 	if (SCARG(&bsa, domain) == -1)
    330 		return EINVAL;
    331 	SCARG(&bsa, type) = linux_to_bsd_type(SCARG(uap, type));
    332 	if (SCARG(&bsa, type) == -1)
    333 		return EINVAL;
    334 	/*
    335 	 * Apparently linux uses this to talk to ISDN sockets. If we fail
    336 	 * now programs seems to handle it, but if we don't we are going
    337 	 * to fail when we bind and programs don't handle this well.
    338 	 */
    339 	if (SCARG(&bsa, domain) == AF_ROUTE && SCARG(&bsa, type) == SOCK_RAW)
    340 		return ENOTSUP;
    341 	error = sys___socket30(l, &bsa, retval);
    342 
    343 #ifdef INET6
    344 	/*
    345 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
    346 	 * default and some apps depend on this. So, set V6ONLY to 0
    347 	 * for Linux apps if the sysctl value is set to 1.
    348 	 */
    349 	if (!error && ip6_v6only && SCARG(&bsa, domain) == PF_INET6) {
    350 		struct socket *so;
    351 
    352 		if (fd_getsock(*retval, &so) == 0) {
    353 			int val = 0;
    354 
    355 			/* ignore error */
    356 			(void)so_setsockopt(l, so, IPPROTO_IPV6, IPV6_V6ONLY,
    357 			    &val, sizeof(val));
    358 
    359 			fd_putfile(*retval);
    360 		}
    361 	}
    362 #endif
    363 
    364 	return (error);
    365 }
    366 
    367 int
    368 linux_sys_socketpair(struct lwp *l, const struct linux_sys_socketpair_args *uap, register_t *retval)
    369 {
    370 	/* {
    371 		syscallarg(int) domain;
    372 		syscallarg(int) type;
    373 		syscallarg(int) protocol;
    374 		syscallarg(int *) rsv;
    375 	} */
    376 	struct sys_socketpair_args bsa;
    377 
    378 	memset(&bsa, 0, sizeof(bsa));
    379 	SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
    380 	if (SCARG(&bsa, domain) == -1)
    381 		return EINVAL;
    382 	SCARG(&bsa, type) = linux_to_bsd_type(SCARG(uap, type));
    383 	if (SCARG(&bsa, type) == -1)
    384 		return EINVAL;
    385 	SCARG(&bsa, protocol) = SCARG(uap, protocol);
    386 	SCARG(&bsa, rsv) = SCARG(uap, rsv);
    387 
    388 	return sys_socketpair(l, &bsa, retval);
    389 }
    390 
    391 int
    392 linux_sys_sendto(struct lwp *l, const struct linux_sys_sendto_args *uap, register_t *retval)
    393 {
    394 	/* {
    395 		syscallarg(int)				s;
    396 		syscallarg(void *)			msg;
    397 		syscallarg(int)				len;
    398 		syscallarg(int)				flags;
    399 		syscallarg(struct osockaddr *)		to;
    400 		syscallarg(int)				tolen;
    401 	} */
    402 	struct msghdr   msg;
    403 	struct iovec    aiov;
    404 	struct sockaddr_big nam;
    405 	struct mbuf *m;
    406 	int bflags;
    407 	int error;
    408 
    409 	/* Translate message flags.  */
    410 	bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
    411 	if (bflags < 0)
    412 		/* Some supported flag */
    413 		return EINVAL;
    414 
    415 	msg.msg_flags = 0;
    416 	msg.msg_name = NULL;
    417 	msg.msg_control = NULL;
    418 
    419 	if (SCARG(uap, tolen)) {
    420 		/* Read in and convert the sockaddr */
    421 		error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, to),
    422 		    SCARG(uap, tolen));
    423 		if (error)
    424 			return error;
    425 		error = sockargs(&m, &nam, nam.sb_len, UIO_SYSSPACE, MT_SONAME);
    426 		if (error)
    427 			return error;
    428 		msg.msg_flags |= MSG_NAMEMBUF;
    429 		msg.msg_name = m;
    430 		msg.msg_namelen = nam.sb_len;
    431 	}
    432 
    433 	msg.msg_iov = &aiov;
    434 	msg.msg_iovlen = 1;
    435 	aiov.iov_base = __UNCONST(SCARG(uap, msg));
    436 	aiov.iov_len = SCARG(uap, len);
    437 
    438 	return do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
    439 }
    440 
    441 static int
    442 linux_to_bsd_msghdr(const struct linux_msghdr *lmsg, struct msghdr *bmsg)
    443 {
    444 
    445 	/*
    446 	 * Verify no integer truncation of the inputs.  For overly
    447 	 * large inputs, fail with the same error codes Linux uses.
    448 	 */
    449 	if (lmsg->msg_namelen < 0)
    450 		return EINVAL;
    451 	CTASSERT(!__type_is_signed(__typeof(lmsg->msg_iovlen)));
    452 	if (lmsg->msg_iovlen > UIO_MAXIOV)
    453 		return EMSGSIZE;
    454 	CTASSERT(!__type_is_signed(__typeof(lmsg->msg_controllen)));
    455 	if (lmsg->msg_controllen > __type_max(__typeof(bmsg->msg_controllen)))
    456 		return ENOBUFS;
    457 
    458 	memset(bmsg, 0, sizeof(*bmsg));
    459 	bmsg->msg_name = lmsg->msg_name;
    460 	bmsg->msg_namelen = MIN(lmsg->msg_namelen,
    461 	    sizeof(struct sockaddr_storage));
    462 	bmsg->msg_iov = lmsg->msg_iov;
    463 	bmsg->msg_iovlen = lmsg->msg_iovlen;
    464 	bmsg->msg_control = lmsg->msg_control;
    465 	bmsg->msg_controllen = lmsg->msg_controllen;
    466 	bmsg->msg_flags = lmsg->msg_flags;
    467 
    468 	return 0;
    469 }
    470 
    471 static void
    472 bsd_to_linux_msghdr(const struct msghdr *bmsg, struct linux_msghdr *lmsg)
    473 {
    474 
    475 	/*
    476 	 * Assert no truncation on the way out.
    477 	 *
    478 	 * - msg_namelen should have been preserved or shortened by
    479 	 *   copyout_sockname.
    480 	 * - msg_iovlen should have been preserved and previously
    481 	 *   confirmed by linux_to_bsd_msghdr to lie in [0,UIO_MAXIOV].
    482 	 * - msg_controllen can always be faithfully converted from BSD
    483 	 *   struct msghdr to Linux struct msghdr without truncation.
    484 	 */
    485 	CTASSERT(__type_min(__typeof(bmsg->msg_namelen)) == 0);
    486 	KASSERT(bmsg->msg_namelen <= __type_max(__typeof(lmsg->msg_namelen)));
    487 	CTASSERT(__type_min(__typeof(lmsg->msg_iovlen)) <= 0);
    488 	KASSERT(lmsg->msg_iovlen >= 0);
    489 	KASSERT(lmsg->msg_iovlen <= UIO_MAXIOV);
    490 	CTASSERT(__type_min(__typeof(lmsg->msg_controllen)) <=
    491 	    __type_min(__typeof(bmsg->msg_controllen)));
    492 	CTASSERT(__type_max(__typeof(lmsg->msg_controllen)) >=
    493 	    __type_max(__typeof(bmsg->msg_controllen)));
    494 
    495 	memset(lmsg, 0, sizeof(*lmsg));
    496 	lmsg->msg_name = bmsg->msg_name;
    497 	lmsg->msg_namelen = bmsg->msg_namelen;
    498 	lmsg->msg_iov = bmsg->msg_iov;
    499 	lmsg->msg_iovlen = bmsg->msg_iovlen;
    500 	lmsg->msg_control = bmsg->msg_control;
    501 	lmsg->msg_controllen = bmsg->msg_controllen;
    502 	lmsg->msg_flags = bmsg->msg_flags;
    503 }
    504 
    505 int
    506 linux_sys_sendmsg(struct lwp *l, const struct linux_sys_sendmsg_args *uap, register_t *retval)
    507 {
    508 	/* {
    509 		syscallarg(int) s;
    510 		syscallarg(struct linux_msghdr *) msg;
    511 		syscallarg(u_int) flags;
    512 	} */
    513 	struct msghdr	msg;
    514 	struct linux_msghdr lmsg;
    515 	int		error;
    516 	int		bflags;
    517 	struct sockaddr_big nam;
    518 	u_int8_t	*control;
    519 	struct mbuf     *ctl_mbuf = NULL;
    520 
    521 	error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg));
    522 	if (error)
    523 		return error;
    524 	error = linux_to_bsd_msghdr(&lmsg, &msg);
    525 	if (error)
    526 		return error;
    527 
    528 	msg.msg_flags = MSG_IOVUSRSPACE;
    529 
    530 	/*
    531 	 * Translate message flags.
    532 	 */
    533 	bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
    534 	if (bflags < 0)
    535 		/* Some supported flag */
    536 		return EINVAL;
    537 
    538 	if (lmsg.msg_name) {
    539 		/* Read in and convert the sockaddr */
    540 		error = linux_get_sa(l, SCARG(uap, s), &nam, msg.msg_name,
    541 		    msg.msg_namelen);
    542 		if (error)
    543 			return (error);
    544 		msg.msg_name = &nam;
    545 	}
    546 
    547 	/*
    548 	 * Handle cmsg if there is any.
    549 	 */
    550 	if (LINUX_CMSG_FIRSTHDR(&lmsg)) {
    551 		struct linux_cmsghdr l_cmsg, *l_cc;
    552 		struct cmsghdr *cmsg;
    553 		socklen_t resid = msg.msg_controllen;
    554 		size_t clen, cidx = 0, cspace;
    555 
    556 		ctl_mbuf = m_get(M_WAIT, MT_CONTROL);
    557 		clen = MLEN;
    558 		control = mtod(ctl_mbuf, void *);
    559 
    560 		l_cc = LINUX_CMSG_FIRSTHDR(&lmsg);
    561 		do {
    562 			if (resid < sizeof(l_cmsg)) {
    563 				error = EINVAL;
    564 				goto done;
    565 			}
    566 			error = copyin(l_cc, &l_cmsg, sizeof(l_cmsg));
    567 			if (error)
    568 				goto done;
    569 
    570 			/*
    571 			 * Sanity check the control message length.
    572 			 * If either:
    573 			 *
    574 			 * 1. aligning it would overflow socklen_t, or
    575 			 * 2. aligning it exceeds msg_controllen, or
    576 			 * 3. adding the delta would overflow socklen_t, or
    577 			 * 4. it's not enough for a struct cmsghdr,
    578 			 *
    579 			 * then fail with EINVAL.
    580 			 */
    581 			CTASSERT(LINUX_CMSG_ALIGN(1) <=
    582 			    __type_max(__typeof(resid)));
    583 			if ((l_cmsg.cmsg_len >= __type_max(__typeof(resid)) -
    584 				LINUX_CMSG_ALIGN(1)) ||
    585 			    (LINUX_CMSG_ALIGN(l_cmsg.cmsg_len) > resid) ||
    586 			    (l_cmsg.cmsg_len >
    587 				__type_max(__typeof(cmsg->cmsg_len)) -
    588 				LINUX_CMSG_ALIGN_DELTA) ||
    589 			    (l_cmsg.cmsg_len < sizeof(l_cmsg))) {
    590 				error = EINVAL;
    591 				goto done;
    592 			}
    593 
    594 			/*
    595 			 * Refuse unsupported control messages, and
    596 			 * translate fields as appropriate.
    597 			 */
    598 			switch (l_cmsg.cmsg_level) {
    599 			case LINUX_SOL_SOCKET:
    600 				/* It only differs on some archs */
    601 				if (LINUX_SOL_SOCKET != SOL_SOCKET)
    602 					l_cmsg.cmsg_level = SOL_SOCKET;
    603 
    604 				switch(l_cmsg.cmsg_type) {
    605 				case LINUX_SCM_RIGHTS:
    606 					/* Linux SCM_RIGHTS is same as NetBSD */
    607 					break;
    608 
    609 				case LINUX_SCM_CREDENTIALS:
    610 					/* no native equivalent, just drop it */
    611 					if (control != mtod(ctl_mbuf, void *))
    612 						free(control, M_MBUF);
    613 					m_free(ctl_mbuf);
    614 					ctl_mbuf = NULL;
    615 					msg.msg_control = NULL;
    616 					msg.msg_controllen = 0;
    617 					goto skipcmsg;
    618 
    619 				default:
    620 					/* other types not supported */
    621 					error = EINVAL;
    622 					goto done;
    623 				}
    624 				break;
    625 			default:
    626 				/* pray and leave intact */
    627 				break;
    628 			}
    629 
    630 			cspace = CMSG_SPACE(l_cmsg.cmsg_len - sizeof(l_cmsg));
    631 
    632 			/* Check the buffer is big enough */
    633 			if (__predict_false(cidx + cspace > clen)) {
    634 				u_int8_t *nc;
    635 				size_t nclen;
    636 
    637 				nclen = cidx + cspace;
    638 				if (nclen >= PAGE_SIZE) {
    639 					error = EINVAL;
    640 					goto done;
    641 				}
    642 				nc = realloc(clen <= MLEN ? NULL : control,
    643 						nclen, M_TEMP, M_WAITOK);
    644 				if (!nc) {
    645 					error = ENOMEM;
    646 					goto done;
    647 				}
    648 				if (cidx <= MLEN)
    649 					/* Old buffer was in mbuf... */
    650 					memcpy(nc, control, cidx);
    651 				control = nc;
    652 				clen = nclen;
    653 			}
    654 
    655 			/* Copy header */
    656 			cmsg = (void *)&control[cidx];
    657 			cmsg->cmsg_len = l_cmsg.cmsg_len + LINUX_CMSG_ALIGN_DELTA;
    658 			cmsg->cmsg_level = l_cmsg.cmsg_level;
    659 			cmsg->cmsg_type = l_cmsg.cmsg_type;
    660 
    661 			/* Zero area between header and data */
    662 			memset(cmsg + 1, 0,
    663 				CMSG_ALIGN(sizeof(*cmsg)) - sizeof(*cmsg));
    664 
    665 			/* Copyin the data */
    666 			error = copyin(LINUX_CMSG_DATA(l_cc),
    667 				CMSG_DATA(cmsg),
    668 				l_cmsg.cmsg_len - sizeof(l_cmsg));
    669 			if (error)
    670 				goto done;
    671 
    672 			resid -= LINUX_CMSG_ALIGN(l_cmsg.cmsg_len);
    673 			cidx += cspace;
    674 		} while ((l_cc = LINUX_CMSG_NXTHDR(&msg, l_cc, &l_cmsg)) && resid > 0);
    675 
    676 		/* If we allocated a buffer, attach to mbuf */
    677 		if (cidx > MLEN) {
    678 			MEXTADD(ctl_mbuf, control, clen, M_MBUF, NULL, NULL);
    679 			ctl_mbuf->m_flags |= M_EXT_RW;
    680 		}
    681 		control = NULL;
    682 		ctl_mbuf->m_len = cidx;
    683 
    684 		msg.msg_control = ctl_mbuf;
    685 		msg.msg_flags |= MSG_CONTROLMBUF;
    686 
    687 		ktrkuser("mbcontrol", mtod(ctl_mbuf, void *),
    688 		    msg.msg_controllen);
    689 	}
    690 
    691 skipcmsg:
    692 	error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
    693 	/* Freed internally */
    694 	ctl_mbuf = NULL;
    695 
    696 done:
    697 	if (ctl_mbuf != NULL) {
    698 		if (control != NULL && control != mtod(ctl_mbuf, void *))
    699 			free(control, M_MBUF);
    700 		m_free(ctl_mbuf);
    701 	}
    702 	return (error);
    703 }
    704 
    705 int
    706 linux_sys_recvfrom(struct lwp *l, const struct linux_sys_recvfrom_args *uap, register_t *retval)
    707 {
    708 	/* {
    709 		syscallarg(int) s;
    710 		syscallarg(void *) buf;
    711 		syscallarg(int) len;
    712 		syscallarg(int) flags;
    713 		syscallarg(struct osockaddr *) from;
    714 		syscallarg(int *) fromlenaddr;
    715 	} */
    716 	int		error;
    717 	struct sys_recvfrom_args bra;
    718 
    719 	memset(&bra, 0, sizeof(bra));
    720 	SCARG(&bra, s) = SCARG(uap, s);
    721 	SCARG(&bra, buf) = SCARG(uap, buf);
    722 	SCARG(&bra, len) = SCARG(uap, len);
    723 	SCARG(&bra, flags) = SCARG(uap, flags);
    724 	SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from);
    725 	SCARG(&bra, fromlenaddr) = (socklen_t *)SCARG(uap, fromlenaddr);
    726 
    727 	if ((error = sys_recvfrom(l, &bra, retval)))
    728 		return (error);
    729 
    730 	if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from))))
    731 		return (error);
    732 
    733 	return (0);
    734 }
    735 
    736 static int
    737 linux_copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
    738 {
    739 	socklen_t dlen;
    740 	int error = 0;
    741 	struct cmsghdr *cmsg;
    742 	struct linux_cmsghdr linux_cmsg;
    743 	struct mbuf *m;
    744 	char *q, *q_end;
    745 
    746 	if (mp->msg_controllen <= 0 || control == 0) {
    747 		mp->msg_controllen = 0;
    748 		free_control_mbuf(l, control, control);
    749 		return 0;
    750 	}
    751 
    752 	ktrkuser("msgcontrol", mtod(control, void *), mp->msg_controllen);
    753 
    754 	q = (char *)mp->msg_control;
    755 	q_end = q + mp->msg_controllen;
    756 
    757 	for (m = control; m != NULL; ) {
    758 		cmsg = mtod(m, struct cmsghdr *);
    759 
    760 		/*
    761 		 * Fixup cmsg. We handle two things:
    762 		 * 0. different sizeof cmsg_len.
    763 		 * 1. different values for level/type on some archs
    764 		 * 2. different alignment of CMSG_DATA on some archs
    765 		 */
    766 		memset(&linux_cmsg, 0, sizeof(linux_cmsg));
    767 		CTASSERT(LINUX_CMSG_ALIGN_DELTA >= 0);
    768 		CTASSERT(CMSG_LEN(0) ==
    769 		    sizeof(linux_cmsg) + LINUX_CMSG_ALIGN_DELTA);
    770 		KASSERTMSG(cmsg->cmsg_len >= CMSG_LEN(0),
    771 		    "cmsg->cmsg_len=%u", cmsg->cmsg_len);
    772 		linux_cmsg.cmsg_len = cmsg->cmsg_len - LINUX_CMSG_ALIGN_DELTA;
    773 		KASSERTMSG(linux_cmsg.cmsg_len >= sizeof(linux_cmsg),
    774 		    "linux_cmsg.cmsg_len=%zu", linux_cmsg.cmsg_len);
    775 		linux_cmsg.cmsg_level = cmsg->cmsg_level;
    776 		linux_cmsg.cmsg_type = cmsg->cmsg_type;
    777 
    778 		dlen = q_end - q;
    779 		if (linux_cmsg.cmsg_len > dlen) {
    780 			/* Not enough room for the parameter */
    781 			if (dlen < sizeof linux_cmsg)
    782 				/* Discard if header wont fit */
    783 				break;
    784 			dlen -= sizeof linux_cmsg;
    785 			mp->msg_flags |= MSG_CTRUNC;
    786 			if (linux_cmsg.cmsg_level == SOL_SOCKET
    787 			    && linux_cmsg.cmsg_type == SCM_RIGHTS)
    788 				/* Do not truncate me ... */
    789 				break;
    790 		} else
    791 			dlen = linux_cmsg.cmsg_len - sizeof linux_cmsg;
    792 
    793 		/*
    794 		 * NetBSD will not create a cmsg record whose header
    795 		 * length plus aligned content length would overflow
    796 		 * socklen_t.  This justifies computing
    797 		 * LINUX_CMSG_SPACE(dlen) (and LINUX_CMSG_LEN) below
    798 		 * without overflow.
    799 		 */
    800 		CTASSERT(LINUX_CMSG_SPACE(1) <= CMSG_SPACE(1));
    801 		CTASSERT(CMSG_SPACE(0) <=
    802 		    __type_max(__typeof(cmsg->cmsg_len)));
    803 		KASSERTMSG((dlen <= __type_max(__typeof(dlen)) -
    804 			CMSG_SPACE(0)),
    805 		    "dlen=%u", dlen);
    806 		CTASSERT(LINUX_CMSG_LEN(1) <= LINUX_CMSG_SPACE(1));
    807 
    808 		switch (linux_cmsg.cmsg_level) {
    809 		case SOL_SOCKET:
    810 			linux_cmsg.cmsg_level = LINUX_SOL_SOCKET;
    811 			switch (linux_cmsg.cmsg_type) {
    812 			case SCM_RIGHTS:
    813 				/* Linux SCM_RIGHTS is same as NetBSD */
    814 				break;
    815 
    816 			default:
    817 				/* other types not supported */
    818 				error = EINVAL;
    819 				goto done;
    820 			}
    821 			/* machine dependent ! */
    822 			break;
    823 		default:
    824 			/* pray and leave intact */
    825 			break;
    826 		}
    827 
    828 		/* There can be padding between the header and data... */
    829 		error = copyout(&linux_cmsg, q, sizeof linux_cmsg);
    830 		if (error != 0) {
    831 			break;
    832 		}
    833 		error = copyout(CCMSG_DATA(cmsg), q + sizeof linux_cmsg, dlen);
    834 		if (error != 0) {
    835 			break;
    836 		}
    837 		m = m->m_next;
    838 		if (m == NULL || LINUX_CMSG_SPACE(dlen) > q_end - q) {
    839 			q += LINUX_CMSG_LEN(dlen);
    840 			break;
    841 		}
    842 		q += LINUX_CMSG_SPACE(dlen);
    843 	}
    844 
    845   done:
    846 	if (error) {
    847 		/* We must free all the SCM_RIGHTS */
    848 		m = control;
    849 	}
    850 	free_control_mbuf(l, control, m);
    851 
    852 	mp->msg_controllen = q - (char *)mp->msg_control;
    853 	return error;
    854 }
    855 
    856 int
    857 linux_sys_recvmsg(struct lwp *l, const struct linux_sys_recvmsg_args *uap, register_t *retval)
    858 {
    859 	/* {
    860 		syscallarg(int) s;
    861 		syscallarg(struct linux_msghdr *) msg;
    862 		syscallarg(u_int) flags;
    863 	} */
    864 	struct msghdr	msg;
    865 	struct linux_msghdr lmsg;
    866 	int		error;
    867 	struct mbuf	*from, *control;
    868 
    869 	error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg));
    870 	if (error)
    871 		return error;
    872 	error = linux_to_bsd_msghdr(&lmsg, &msg);
    873 	if (error)
    874 		return error;
    875 
    876 	msg.msg_flags = linux_to_bsd_msg_flags(SCARG(uap, flags));
    877 	if (msg.msg_flags < 0) {
    878 		/* Some unsupported flag */
    879 		return (EINVAL);
    880 	}
    881 	msg.msg_flags |= MSG_IOVUSRSPACE;
    882 
    883 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
    884 	    msg.msg_control != NULL ? &control : NULL, retval);
    885 	if (error != 0)
    886 		return error;
    887 
    888 	if (msg.msg_control != NULL)
    889 		error = linux_copyout_msg_control(l, &msg, control);
    890 
    891 	if (error == 0 && from != 0) {
    892 		mtod(from, struct osockaddr *)->sa_family =
    893 		    bsd_to_linux_domain(mtod(from, struct sockaddr *)->sa_family);
    894 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
    895 			from);
    896 	} else
    897 		msg.msg_namelen = 0;
    898 
    899 	if (from != NULL)
    900 		m_free(from);
    901 
    902 	if (error == 0) {
    903 		msg.msg_flags = bsd_to_linux_msg_flags(msg.msg_flags);
    904 		if (msg.msg_flags < 0)
    905 			/* Some flag unsupported by Linux */
    906 			error = EINVAL;
    907 		else {
    908 			ktrkuser("msghdr", &msg, sizeof(msg));
    909 			bsd_to_linux_msghdr(&msg, &lmsg);
    910 			error = copyout(&lmsg, SCARG(uap, msg), sizeof(lmsg));
    911 		}
    912 	}
    913 
    914 	return (error);
    915 }
    916 
    917 /*
    918  * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
    919  * is different, the rest matches IPPROTO_* on both systems.
    920  */
    921 int
    922 linux_to_bsd_sopt_level(int llevel)
    923 {
    924 
    925 	switch (llevel) {
    926 	case LINUX_SOL_SOCKET:
    927 		return SOL_SOCKET;
    928 	case LINUX_SOL_IP:
    929 		return IPPROTO_IP;
    930 #ifdef INET6
    931 	case LINUX_SOL_IPV6:
    932 		return IPPROTO_IPV6;
    933 #endif
    934 	case LINUX_SOL_TCP:
    935 		return IPPROTO_TCP;
    936 	case LINUX_SOL_UDP:
    937 		return IPPROTO_UDP;
    938 	default:
    939 		return -1;
    940 	}
    941 }
    942 
    943 /*
    944  * Convert Linux socket level socket option numbers to NetBSD values.
    945  */
    946 int
    947 linux_to_bsd_so_sockopt(int lopt)
    948 {
    949 
    950 	switch (lopt) {
    951 	case LINUX_SO_DEBUG:
    952 		return SO_DEBUG;
    953 	case LINUX_SO_REUSEADDR:
    954 		/*
    955 		 * Linux does not implement SO_REUSEPORT, but allows reuse of
    956 		 * a host:port pair through SO_REUSEADDR even if the address
    957 		 * is not a multicast-address. Effectively, this means that we
    958 		 * should use SO_REUSEPORT to allow Linux applications to not
    959 		 * exit with EADDRINUSE
    960 		 */
    961 		return SO_REUSEPORT;
    962 	case LINUX_SO_TYPE:
    963 		return SO_TYPE;
    964 	case LINUX_SO_ERROR:
    965 		return SO_ERROR;
    966 	case LINUX_SO_DONTROUTE:
    967 		return SO_DONTROUTE;
    968 	case LINUX_SO_BROADCAST:
    969 		return SO_BROADCAST;
    970 	case LINUX_SO_SNDBUF:
    971 		return SO_SNDBUF;
    972 	case LINUX_SO_RCVBUF:
    973 		return SO_RCVBUF;
    974 	case LINUX_SO_KEEPALIVE:
    975 		return SO_KEEPALIVE;
    976 	case LINUX_SO_OOBINLINE:
    977 		return SO_OOBINLINE;
    978 	case LINUX_SO_NO_CHECK:
    979 	case LINUX_SO_PRIORITY:
    980 		return -1;
    981 	case LINUX_SO_LINGER:
    982 		return SO_LINGER;
    983 	case LINUX_SO_BSDCOMPAT:
    984 	case LINUX_SO_PASSCRED:
    985 	case LINUX_SO_PEERCRED:
    986 		return -1;
    987 	case LINUX_SO_RCVLOWAT:
    988 		return SO_RCVLOWAT;
    989 	case LINUX_SO_SNDLOWAT:
    990 		return SO_SNDLOWAT;
    991 	case LINUX_SO_RCVTIMEO:
    992 		return SO_RCVTIMEO;
    993 	case LINUX_SO_SNDTIMEO:
    994 		return SO_SNDTIMEO;
    995 	case LINUX_SO_SECURITY_AUTHENTICATION:
    996 	case LINUX_SO_SECURITY_ENCRYPTION_TRANSPORT:
    997 	case LINUX_SO_SECURITY_ENCRYPTION_NETWORK:
    998 	case LINUX_SO_BINDTODEVICE:
    999 	case LINUX_SO_ATTACH_FILTER:
   1000 	case LINUX_SO_DETACH_FILTER:
   1001 	case LINUX_SO_PEERNAME:
   1002 		return -1;
   1003 	case LINUX_SO_TIMESTAMP:
   1004 		return SO_TIMESTAMP;
   1005 	case LINUX_SO_ACCEPTCONN:
   1006 	case LINUX_SO_PEERSEC:
   1007 	case LINUX_SO_SNDBUFFORCE:
   1008 	case LINUX_SO_RCVBUFFORCE:
   1009 	case LINUX_SO_PASSSEC:
   1010 	case LINUX_SO_TIMESTAMPNS:
   1011 	case LINUX_SO_MARK:
   1012 	case LINUX_SO_TIMESTAMPING:
   1013 	case LINUX_SO_PROTOCOL:
   1014 	case LINUX_SO_DOMAIN:
   1015 	case LINUX_SO_RXQ_OVFL:
   1016 	case LINUX_SO_WIFI_STATUS:
   1017 	case LINUX_SO_PEEK_OFF:
   1018 	case LINUX_SO_NOFCS:
   1019 	default:
   1020 		return -1;
   1021 	}
   1022 }
   1023 
   1024 /*
   1025  * Convert Linux IP level socket option number to NetBSD values.
   1026  */
   1027 int
   1028 linux_to_bsd_ip_sockopt(int lopt)
   1029 {
   1030 
   1031 	switch (lopt) {
   1032 	case LINUX_IP_TOS:
   1033 		return IP_TOS;
   1034 	case LINUX_IP_TTL:
   1035 		return IP_TTL;
   1036 	case LINUX_IP_RETOPTS:
   1037 		return IP_RETOPTS;
   1038 	case LINUX_IP_PKTINFO:
   1039 		return IP_PKTINFO;
   1040 	case LINUX_IP_RECVOPTS:
   1041 		return IP_RECVOPTS;
   1042 	case LINUX_IP_HDRINCL:
   1043 		return IP_HDRINCL;
   1044 	case LINUX_IP_MULTICAST_TTL:
   1045 		return IP_MULTICAST_TTL;
   1046 	case LINUX_IP_MULTICAST_LOOP:
   1047 		return IP_MULTICAST_LOOP;
   1048 	case LINUX_IP_MULTICAST_IF:
   1049 		return IP_MULTICAST_IF;
   1050 	case LINUX_IP_ADD_MEMBERSHIP:
   1051 		return IP_ADD_MEMBERSHIP;
   1052 	case LINUX_IP_DROP_MEMBERSHIP:
   1053 		return IP_DROP_MEMBERSHIP;
   1054 	case LINUX_IP_RECVERR:
   1055 	case LINUX_IP_FREEBIND:
   1056 		return -2;	/* ignored */
   1057 	case LINUX_IP_MULTICAST_ALL:
   1058 		return -3;	/* noprotoopt */
   1059 	default:
   1060 		return -1;
   1061 	}
   1062 }
   1063 
   1064 /*
   1065  * Convert Linux IPV6 level socket option number to NetBSD values.
   1066  */
   1067 #ifdef INET6
   1068 int
   1069 linux_to_bsd_ipv6_sockopt(int lopt)
   1070 {
   1071 
   1072 	switch (lopt) {
   1073 	case LINUX_IPV6_V6ONLY:
   1074 		return IPV6_V6ONLY;
   1075 	case LINUX_IPV6_MULTICAST_HOPS:
   1076 		return IPV6_MULTICAST_HOPS;
   1077 	case LINUX_IPV6_MULTICAST_ALL:
   1078 		return -3;	/* noprotoopt */
   1079 	default:
   1080 		return -1;
   1081 	}
   1082 }
   1083 #endif
   1084 
   1085 /*
   1086  * Convert Linux TCP level socket option number to NetBSD values.
   1087  */
   1088 int
   1089 linux_to_bsd_tcp_sockopt(int lopt)
   1090 {
   1091 
   1092 	switch (lopt) {
   1093 	case LINUX_TCP_NODELAY:
   1094 		return TCP_NODELAY;
   1095 	case LINUX_TCP_MAXSEG:
   1096 		return TCP_MAXSEG;
   1097 	default:
   1098 		return -1;
   1099 	}
   1100 }
   1101 
   1102 /*
   1103  * Convert Linux UDP level socket option number to NetBSD values.
   1104  */
   1105 int
   1106 linux_to_bsd_udp_sockopt(int lopt)
   1107 {
   1108 
   1109 	switch (lopt) {
   1110 	default:
   1111 		return -1;
   1112 	}
   1113 }
   1114 
   1115 /*
   1116  * Another reasonably straightforward function: setsockopt(2).
   1117  * The level and option numbers are converted; the values passed
   1118  * are not (yet) converted, the ones currently implemented don't
   1119  * need conversion, as they are the same on both systems.
   1120  */
   1121 int
   1122 linux_sys_setsockopt(struct lwp *l, const struct linux_sys_setsockopt_args *uap, register_t *retval)
   1123 {
   1124 	/* {
   1125 		syscallarg(int) s;
   1126 		syscallarg(int) level;
   1127 		syscallarg(int) optname;
   1128 		syscallarg(void *) optval;
   1129 		syscallarg(int) optlen;
   1130 	} */
   1131 	struct sys_setsockopt_args bsa;
   1132 	int name;
   1133 
   1134 	memset(&bsa, 0, sizeof(bsa));
   1135 	SCARG(&bsa, s) = SCARG(uap, s);
   1136 	SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
   1137 	SCARG(&bsa, val) = SCARG(uap, optval);
   1138 	SCARG(&bsa, valsize) = SCARG(uap, optlen);
   1139 
   1140 	/*
   1141 	 * Linux supports only SOL_SOCKET for AF_LOCAL domain sockets
   1142 	 * and returns EOPNOTSUPP for other levels
   1143 	 */
   1144 	if (SCARG(&bsa, level) != SOL_SOCKET) {
   1145 		struct socket *so;
   1146 		int error, family;
   1147 
   1148 		/* fd_getsock() will use the descriptor for us */
   1149 	    	if ((error = fd_getsock(SCARG(&bsa, s), &so)) != 0)
   1150 		    	return error;
   1151 		family = so->so_proto->pr_domain->dom_family;
   1152 		fd_putfile(SCARG(&bsa, s));
   1153 
   1154 		if (family == AF_LOCAL)
   1155 			return EOPNOTSUPP;
   1156 	}
   1157 
   1158 	switch (SCARG(&bsa, level)) {
   1159 	case SOL_SOCKET:
   1160 		name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
   1161 		break;
   1162 	case IPPROTO_IP:
   1163 		name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
   1164 		break;
   1165 #ifdef INET6
   1166 	case IPPROTO_IPV6:
   1167 		name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname));
   1168 		break;
   1169 #endif
   1170 	case IPPROTO_TCP:
   1171 		name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
   1172 		break;
   1173 	case IPPROTO_UDP:
   1174 		name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
   1175 		break;
   1176 	default:
   1177 		return EINVAL;
   1178 	}
   1179 
   1180 	switch (name) {
   1181 	case -1:
   1182 		return EINVAL;
   1183 	case -2:
   1184 		return 0;
   1185 	case -3:
   1186 		return ENOPROTOOPT;
   1187 	}
   1188 	SCARG(&bsa, name) = name;
   1189 
   1190 	return sys_setsockopt(l, &bsa, retval);
   1191 }
   1192 
   1193 /*
   1194  * getsockopt(2) is very much the same as setsockopt(2) (see above)
   1195  */
   1196 int
   1197 linux_sys_getsockopt(struct lwp *l, const struct linux_sys_getsockopt_args *uap, register_t *retval)
   1198 {
   1199 	/* {
   1200 		syscallarg(int) s;
   1201 		syscallarg(int) level;
   1202 		syscallarg(int) optname;
   1203 		syscallarg(void *) optval;
   1204 		syscallarg(int *) optlen;
   1205 	} */
   1206 	struct sys_getsockopt_args bga;
   1207 	int name;
   1208 
   1209 	memset(&bga, 0, sizeof(bga));
   1210 	SCARG(&bga, s) = SCARG(uap, s);
   1211 	SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
   1212 	SCARG(&bga, val) = SCARG(uap, optval);
   1213 	SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen);
   1214 
   1215 	switch (SCARG(&bga, level)) {
   1216 	case SOL_SOCKET:
   1217 		name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
   1218 		break;
   1219 	case IPPROTO_IP:
   1220 		name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
   1221 		break;
   1222 #ifdef INET6
   1223 	case IPPROTO_IPV6:
   1224 		name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname));
   1225 		break;
   1226 #endif
   1227 	case IPPROTO_TCP:
   1228 		name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
   1229 		break;
   1230 	case IPPROTO_UDP:
   1231 		name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
   1232 		break;
   1233 	default:
   1234 		return EINVAL;
   1235 	}
   1236 
   1237 	switch (name) {
   1238 	case -1:
   1239 	case -2: /* we can't ignore, since we don't know what to return */
   1240 		return EINVAL;
   1241 	case -3:
   1242 		return ENOPROTOOPT;
   1243 	}
   1244 	SCARG(&bga, name) = name;
   1245 
   1246 	return sys_getsockopt(l, &bga, retval);
   1247 }
   1248 
   1249 int
   1250 linux_getifname(struct lwp *l, register_t *retval, void *data)
   1251 {
   1252 	struct ifnet *ifp;
   1253 	struct linux_ifreq ifr;
   1254 	int error;
   1255 	int s;
   1256 
   1257 	error = copyin(data, &ifr, sizeof(ifr));
   1258 	if (error)
   1259 		return error;
   1260 
   1261 	s = pserialize_read_enter();
   1262 	ifp = if_byindex(ifr.ifr_ifru.ifru_ifindex);
   1263 	if (ifp == NULL) {
   1264 		pserialize_read_exit(s);
   1265 		return ENODEV;
   1266 	}
   1267 
   1268 	strncpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
   1269 	pserialize_read_exit(s);
   1270 
   1271 	return copyout(&ifr, data, sizeof(ifr));
   1272 }
   1273 
   1274 int
   1275 linux_getifconf(struct lwp *l, register_t *retval, void *data)
   1276 {
   1277 	struct linux_ifreq ifr, *ifrp = NULL;
   1278 	struct linux_ifconf ifc;
   1279 	struct ifnet *ifp;
   1280 	struct sockaddr *sa;
   1281 	struct osockaddr *osa;
   1282 	int space = 0, error;
   1283 	const int sz = (int)sizeof(ifr);
   1284 	bool docopy;
   1285 	int s;
   1286 	int bound;
   1287 	struct psref psref;
   1288 
   1289 	error = copyin(data, &ifc, sizeof(ifc));
   1290 	if (error)
   1291 		return error;
   1292 
   1293 	docopy = ifc.ifc_req != NULL;
   1294 	if (docopy) {
   1295 		if (ifc.ifc_len < 0)
   1296 			return EINVAL;
   1297 
   1298 		space = ifc.ifc_len;
   1299 		ifrp = ifc.ifc_req;
   1300 	}
   1301 	memset(&ifr, 0, sizeof(ifr));
   1302 
   1303 	bound = curlwp_bind();
   1304 	s = pserialize_read_enter();
   1305 	IFNET_READER_FOREACH(ifp) {
   1306 		struct ifaddr *ifa;
   1307 		if_acquire(ifp, &psref);
   1308 		pserialize_read_exit(s);
   1309 
   1310 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
   1311 		    sizeof(ifr.ifr_name));
   1312 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
   1313 			error = ENAMETOOLONG;
   1314 			goto release_exit;
   1315 		}
   1316 
   1317 		s = pserialize_read_enter();
   1318 		IFADDR_READER_FOREACH(ifa, ifp) {
   1319 			struct psref psref_ifa;
   1320 			ifa_acquire(ifa, &psref_ifa);
   1321 			pserialize_read_exit(s);
   1322 
   1323 			sa = ifa->ifa_addr;
   1324 			if (sa->sa_family != AF_INET ||
   1325 			    sa->sa_len > sizeof(*osa))
   1326 				goto next;
   1327 			memcpy(&ifr.ifr_addr, sa, sa->sa_len);
   1328 			osa = (struct osockaddr *)&ifr.ifr_addr;
   1329 			osa->sa_family = sa->sa_family;
   1330 			if (space >= sz) {
   1331 				error = copyout(&ifr, ifrp, sz);
   1332 				if (error != 0) {
   1333 					ifa_release(ifa, &psref_ifa);
   1334 					goto release_exit;
   1335 				}
   1336 				ifrp++;
   1337 			}
   1338 			space -= sz;
   1339 		next:
   1340 			s = pserialize_read_enter();
   1341 			ifa_release(ifa, &psref_ifa);
   1342 		}
   1343 
   1344 		KASSERT(pserialize_in_read_section());
   1345 		if_release(ifp, &psref);
   1346 	}
   1347 	pserialize_read_exit(s);
   1348 	curlwp_bindx(bound);
   1349 
   1350 	if (docopy)
   1351 		ifc.ifc_len -= space;
   1352 	else
   1353 		ifc.ifc_len = -space;
   1354 
   1355 	return copyout(&ifc, data, sizeof(ifc));
   1356 
   1357 release_exit:
   1358 	if_release(ifp, &psref);
   1359 	curlwp_bindx(bound);
   1360 	return error;
   1361 }
   1362 
   1363 int
   1364 linux_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
   1365     void *data)
   1366 {
   1367 	/* Not the full structure, just enough to map what we do here */
   1368 	struct linux_ifreq lreq;
   1369 	file_t *fp;
   1370 	struct ifaddr *ifa;
   1371 	struct ifnet *ifp;
   1372 	struct sockaddr_dl *sadl;
   1373 	int error, found;
   1374 	int index, ifnum;
   1375 	int s;
   1376 
   1377 	/*
   1378 	 * We can't emulate this ioctl by calling sys_ioctl() to run
   1379 	 * SIOCGIFCONF, because the user buffer is not of the right
   1380 	 * type to take those results.  We can't use kernel buffers to
   1381 	 * receive the results, as the implementation of sys_ioctl()
   1382 	 * and ifconf() [which implements SIOCGIFCONF] use
   1383 	 * copyin()/copyout() which will fail on kernel addresses.
   1384 	 *
   1385 	 * So, we must duplicate code from sys_ioctl() and ifconf().  Ugh.
   1386 	 */
   1387 
   1388 	if ((fp = fd_getfile(fd)) == NULL)
   1389 		return (EBADF);
   1390 
   1391 	KERNEL_LOCK(1, NULL);
   1392 
   1393 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
   1394 		error = EBADF;
   1395 		goto out;
   1396 	}
   1397 
   1398 	error = copyin(data, &lreq, sizeof(lreq));
   1399 	if (error)
   1400 		goto out;
   1401 	lreq.ifr_name[LINUX_IFNAMSIZ-1] = '\0';		/* just in case */
   1402 
   1403 	/*
   1404 	 * Try real interface name first, then fake "ethX"
   1405 	 */
   1406 	found = 0;
   1407 	s = pserialize_read_enter();
   1408 	IFNET_READER_FOREACH(ifp) {
   1409 		if (found)
   1410 			break;
   1411 		if (strcmp(lreq.ifr_name, ifp->if_xname))
   1412 			/* not this interface */
   1413 			continue;
   1414 
   1415 		found=1;
   1416 		if (IFADDR_READER_EMPTY(ifp)) {
   1417 			pserialize_read_exit(s);
   1418 			error = ENODEV;
   1419 			goto out;
   1420 		}
   1421 		IFADDR_READER_FOREACH(ifa, ifp) {
   1422 			sadl = satosdl(ifa->ifa_addr);
   1423 			/* only return ethernet addresses */
   1424 			/* XXX what about FDDI, etc. ? */
   1425 			if (sadl->sdl_family != AF_LINK ||
   1426 			    sadl->sdl_type != IFT_ETHER)
   1427 				continue;
   1428 			memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl),
   1429 			       MIN(sadl->sdl_alen,
   1430 				   sizeof(lreq.ifr_hwaddr.sa_data)));
   1431 			lreq.ifr_hwaddr.sa_family =
   1432 				sadl->sdl_family;
   1433 			pserialize_read_exit(s);
   1434 
   1435 			error = copyout(&lreq, data, sizeof(lreq));
   1436 			goto out;
   1437 		}
   1438 	}
   1439 	pserialize_read_exit(s);
   1440 
   1441 	if (strncmp(lreq.ifr_name, "eth", 3) != 0) {
   1442 		/* unknown interface, not even an "eth*" name */
   1443 		error = ENODEV;
   1444 		goto out;
   1445 	}
   1446 
   1447 	for (ifnum = 0, index = 3;
   1448 	     index < LINUX_IFNAMSIZ && lreq.ifr_name[index] != '\0';
   1449 	     index++) {
   1450 		ifnum *= 10;
   1451 		ifnum += lreq.ifr_name[index] - '0';
   1452 	}
   1453 
   1454 	error = EINVAL;			/* in case we don't find one */
   1455 	s = pserialize_read_enter();
   1456 	IFNET_READER_FOREACH(ifp) {
   1457 		memcpy(lreq.ifr_name, ifp->if_xname,
   1458 		       MIN(LINUX_IFNAMSIZ, IFNAMSIZ));
   1459 		IFADDR_READER_FOREACH(ifa, ifp) {
   1460 			sadl = satosdl(ifa->ifa_addr);
   1461 			/* only return ethernet addresses */
   1462 			/* XXX what about FDDI, etc. ? */
   1463 			if (sadl->sdl_family != AF_LINK ||
   1464 			    sadl->sdl_type != IFT_ETHER)
   1465 				continue;
   1466 			if (ifnum--)
   1467 				/* not the requested iface */
   1468 				continue;
   1469 			memcpy(&lreq.ifr_hwaddr.sa_data,
   1470 			       CLLADDR(sadl),
   1471 			       MIN(sadl->sdl_alen,
   1472 				   sizeof(lreq.ifr_hwaddr.sa_data)));
   1473 			lreq.ifr_hwaddr.sa_family =
   1474 				sadl->sdl_family;
   1475 			pserialize_read_exit(s);
   1476 
   1477 			error = copyout(&lreq, data, sizeof(lreq));
   1478 			goto out;
   1479 		}
   1480 	}
   1481 	pserialize_read_exit(s);
   1482 
   1483 out:
   1484 	KERNEL_UNLOCK_ONE(NULL);
   1485 	fd_putfile(fd);
   1486 	return error;
   1487 }
   1488 
   1489 int
   1490 linux_ioctl_socket(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
   1491 {
   1492 	/* {
   1493 		syscallarg(int) fd;
   1494 		syscallarg(u_long) com;
   1495 		syscallarg(void *) data;
   1496 	} */
   1497 	u_long com;
   1498 	int error = 0, isdev = 0, dosys = 1;
   1499 	struct sys_ioctl_args ia;
   1500 	file_t *fp;
   1501 	struct vnode *vp;
   1502 	int (*ioctlf)(file_t *, u_long, void *);
   1503 	struct ioctl_pt pt;
   1504 
   1505 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
   1506 		return (EBADF);
   1507 
   1508 	if (fp->f_type == DTYPE_VNODE) {
   1509 		vp = (struct vnode *)fp->f_data;
   1510 		isdev = vp->v_type == VCHR;
   1511 	}
   1512 
   1513 	/*
   1514 	 * Don't try to interpret socket ioctl calls that are done
   1515 	 * on a device filedescriptor, just pass them through, to
   1516 	 * emulate Linux behaviour. Use PTIOCLINUX so that the
   1517 	 * device will only handle these if it's prepared to do
   1518 	 * so, to avoid unexpected things from happening.
   1519 	 */
   1520 	if (isdev) {
   1521 		dosys = 0;
   1522 		ioctlf = fp->f_ops->fo_ioctl;
   1523 		pt.com = SCARG(uap, com);
   1524 		pt.data = SCARG(uap, data);
   1525 		error = ioctlf(fp, PTIOCLINUX, &pt);
   1526 		/*
   1527 		 * XXX hack: if the function returns EJUSTRETURN,
   1528 		 * it has stuffed a sysctl return value in pt.data.
   1529 		 */
   1530 		if (error == EJUSTRETURN) {
   1531 			retval[0] = (register_t)pt.data;
   1532 			error = 0;
   1533 		}
   1534 		goto out;
   1535 	}
   1536 
   1537 	com = SCARG(uap, com);
   1538 	retval[0] = 0;
   1539 
   1540 	memset(&ia, 0, sizeof(ia));
   1541 
   1542 	switch (com) {
   1543 	case LINUX_SIOCGIFNAME:
   1544 		error = linux_getifname(l, retval, SCARG(uap, data));
   1545 		dosys = 0;
   1546 		break;
   1547 	case LINUX_SIOCGIFCONF:
   1548 		error = linux_getifconf(l, retval, SCARG(uap, data));
   1549 		dosys = 0;
   1550 		break;
   1551 	case LINUX_SIOCGIFFLAGS:
   1552 		SCARG(&ia, com) = OSIOCGIFFLAGS;
   1553 		break;
   1554 	case LINUX_SIOCSIFFLAGS:
   1555 		SCARG(&ia, com) = OSIOCSIFFLAGS;
   1556 		break;
   1557 	case LINUX_SIOCGIFADDR:
   1558 		SCARG(&ia, com) = OOSIOCGIFADDR;
   1559 		break;
   1560 	case LINUX_SIOCGIFDSTADDR:
   1561 		SCARG(&ia, com) = OOSIOCGIFDSTADDR;
   1562 		break;
   1563 	case LINUX_SIOCGIFBRDADDR:
   1564 		SCARG(&ia, com) = OOSIOCGIFBRDADDR;
   1565 		break;
   1566 	case LINUX_SIOCGIFNETMASK:
   1567 		SCARG(&ia, com) = OOSIOCGIFNETMASK;
   1568 		break;
   1569 	case LINUX_SIOCGIFMTU:
   1570 		SCARG(&ia, com) = OSIOCGIFMTU;
   1571 		break;
   1572 	case LINUX_SIOCADDMULTI:
   1573 		SCARG(&ia, com) = OSIOCADDMULTI;
   1574 		break;
   1575 	case LINUX_SIOCDELMULTI:
   1576 		SCARG(&ia, com) = OSIOCDELMULTI;
   1577 		break;
   1578 	case LINUX_SIOCGIFHWADDR:
   1579 		error = linux_getifhwaddr(l, retval, SCARG(uap, fd),
   1580 		    SCARG(uap, data));
   1581 		dosys = 0;
   1582 		break;
   1583 	default:
   1584 		error = EINVAL;
   1585 	}
   1586 
   1587  out:
   1588  	fd_putfile(SCARG(uap, fd));
   1589 
   1590 	if (error ==0 && dosys) {
   1591 		SCARG(&ia, fd) = SCARG(uap, fd);
   1592 		SCARG(&ia, data) = SCARG(uap, data);
   1593 		error = sys_ioctl(curlwp, &ia, retval);
   1594 	}
   1595 
   1596 	return error;
   1597 }
   1598 
   1599 int
   1600 linux_sys_connect(struct lwp *l, const struct linux_sys_connect_args *uap, register_t *retval)
   1601 {
   1602 	/* {
   1603 		syscallarg(int) s;
   1604 		syscallarg(const struct sockaddr *) name;
   1605 		syscallarg(int) namelen;
   1606 	} */
   1607 	int		error;
   1608 	struct sockaddr_big sb;
   1609 
   1610 	error = linux_get_sa(l, SCARG(uap, s), &sb, SCARG(uap, name),
   1611 	    SCARG(uap, namelen));
   1612 	if (error)
   1613 		return (error);
   1614 
   1615 	error = do_sys_connect(l, SCARG(uap, s), (struct sockaddr *)&sb);
   1616 
   1617 	if (error == EISCONN) {
   1618 		struct socket *so;
   1619 		int state, prflags;
   1620 
   1621 		/* fd_getsock() will use the descriptor for us */
   1622 	    	if (fd_getsock(SCARG(uap, s), &so) != 0)
   1623 		    	return EISCONN;
   1624 
   1625 		solock(so);
   1626 		state = so->so_state;
   1627 		prflags = so->so_proto->pr_flags;
   1628 		sounlock(so);
   1629 		fd_putfile(SCARG(uap, s));
   1630 		/*
   1631 		 * We should only let this call succeed once per
   1632 		 * non-blocking connect; however we don't have
   1633 		 * a convenient place to keep that state..
   1634 		 */
   1635 		if ((state & (SS_ISCONNECTED|SS_NBIO)) ==
   1636 		    (SS_ISCONNECTED|SS_NBIO) &&
   1637 		    (prflags & PR_CONNREQUIRED))
   1638 			return 0;
   1639 	}
   1640 
   1641 	return (error);
   1642 }
   1643 
   1644 int
   1645 linux_sys_bind(struct lwp *l, const struct linux_sys_bind_args *uap, register_t *retval)
   1646 {
   1647 	/* {
   1648 		syscallarg(int) s;
   1649 		syscallarg(const struct osockaddr *) name;
   1650 		syscallarg(int) namelen;
   1651 	} */
   1652 	int		error;
   1653 	struct sockaddr_big sb;
   1654 
   1655 	error = linux_get_sa(l, SCARG(uap, s), &sb, SCARG(uap, name),
   1656 	    SCARG(uap, namelen));
   1657 	if (error)
   1658 		return (error);
   1659 
   1660 	return do_sys_bind(l, SCARG(uap, s), (struct sockaddr *)&sb);
   1661 }
   1662 
   1663 int
   1664 linux_sys_getsockname(struct lwp *l, const struct linux_sys_getsockname_args *uap, register_t *retval)
   1665 {
   1666 	/* {
   1667 		syscallarg(int) fdes;
   1668 		syscallarg(void *) asa;
   1669 		syscallarg(int *) alen;
   1670 	} */
   1671 	int error;
   1672 
   1673 	if ((error = sys_getsockname(l, (const void *)uap, retval)) != 0)
   1674 		return (error);
   1675 
   1676 	if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
   1677 		return (error);
   1678 
   1679 	return (0);
   1680 }
   1681 
   1682 int
   1683 linux_sys_getpeername(struct lwp *l, const struct linux_sys_getpeername_args *uap, register_t *retval)
   1684 {
   1685 	/* {
   1686 		syscallarg(int) fdes;
   1687 		syscallarg(void *) asa;
   1688 		syscallarg(int *) alen;
   1689 	} */
   1690 	int error;
   1691 
   1692 	if ((error = sys_getpeername(l, (const void *)uap, retval)) != 0)
   1693 		return (error);
   1694 
   1695 	if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
   1696 		return (error);
   1697 
   1698 	return (0);
   1699 }
   1700 
   1701 /*
   1702  * Copy the osockaddr structure pointed to by name to sb, adjust
   1703  * family and convert to sockaddr.
   1704  */
   1705 static int
   1706 linux_get_sa(struct lwp *l, int s, struct sockaddr_big *sb,
   1707     const struct osockaddr *name, socklen_t namelen)
   1708 {
   1709 	int error, bdom;
   1710 
   1711 	if (namelen > UCHAR_MAX ||
   1712 	    namelen <= offsetof(struct sockaddr_big, sb_data))
   1713 		return EINVAL;
   1714 
   1715 	error = copyin(name, sb, namelen);
   1716 	if (error)
   1717 		return error;
   1718 
   1719 	bdom = linux_to_bsd_domain(sb->sb_family);
   1720 	if (bdom == -1)
   1721 		return EINVAL;
   1722 
   1723 	/*
   1724 	 * If the family is unspecified, use address family of the socket.
   1725 	 * This avoid triggering strict family checks in netinet/in_pcb.c et.al.
   1726 	 */
   1727 	if (bdom == AF_UNSPEC) {
   1728 		struct socket *so;
   1729 
   1730 		/* fd_getsock() will use the descriptor for us */
   1731 		if ((error = fd_getsock(s, &so)) != 0)
   1732 			return error;
   1733 
   1734 		bdom = so->so_proto->pr_domain->dom_family;
   1735 		fd_putfile(s);
   1736 	}
   1737 
   1738 	/*
   1739 	 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
   1740 	 * which lacks the scope id compared with RFC2553 one. If we detect
   1741 	 * the situation, reject the address and write a message to system log.
   1742 	 *
   1743 	 * Still accept addresses for which the scope id is not used.
   1744 	 */
   1745 	if (bdom == AF_INET6 &&
   1746 	    namelen == sizeof(struct sockaddr_in6) - sizeof(uint32_t)) {
   1747 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sb;
   1748 		if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) &&
   1749 		    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
   1750 		     IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) ||
   1751 		     IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) ||
   1752 		     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
   1753 		     IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
   1754 			struct proc *p = l->l_proc;
   1755 			int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1;
   1756 
   1757 			log(LOG_DEBUG,
   1758 			    "pid %d (%s), uid %d: obsolete pre-RFC2553 "
   1759 			    "sockaddr_in6 rejected",
   1760 			    p->p_pid, p->p_comm, uid);
   1761 			return EINVAL;
   1762 		}
   1763 		namelen = sizeof(struct sockaddr_in6);
   1764 		sin6->sin6_scope_id = 0;
   1765 	}
   1766 
   1767 	/*
   1768 	 * Linux is less strict than NetBSD and permits namelen to be larger
   1769 	 * than valid struct sockaddr_in*.  If this is the case, truncate
   1770 	 * the value to the correct size, so that NetBSD networking does not
   1771 	 * return an error.
   1772 	 */
   1773 	switch (bdom) {
   1774 	case AF_INET:
   1775 		namelen = MIN(namelen, sizeof(struct sockaddr_in));
   1776 		break;
   1777 	case AF_INET6:
   1778 		namelen = MIN(namelen, sizeof(struct sockaddr_in6));
   1779 		break;
   1780 	}
   1781 
   1782 	sb->sb_family = bdom;
   1783 	sb->sb_len = namelen;
   1784 	ktrkuser("mbsoname", sb, namelen);
   1785 	return 0;
   1786 }
   1787 
   1788 static int
   1789 linux_sa_put(struct osockaddr *osa)
   1790 {
   1791 	struct sockaddr sa;
   1792 	struct osockaddr *kosa;
   1793 	int error, bdom, len;
   1794 
   1795 	/*
   1796 	 * Only read/write the sockaddr family and length part, the rest is
   1797 	 * not changed.
   1798 	 */
   1799 	len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
   1800 
   1801 	error = copyin(osa, &sa, len);
   1802 	if (error)
   1803 		return (error);
   1804 
   1805 	bdom = bsd_to_linux_domain(sa.sa_family);
   1806 	if (bdom == -1)
   1807 		return (EINVAL);
   1808 
   1809 	/* Note: we convert from sockaddr to osockaddr here, too */
   1810 	kosa = (struct osockaddr *) &sa;
   1811 	kosa->sa_family = bdom;
   1812 	error = copyout(kosa, osa, len);
   1813 	if (error)
   1814 		return (error);
   1815 
   1816 	return (0);
   1817 }
   1818 
   1819 #if !defined(__aarch64__) && !defined(__amd64__)
   1820 int
   1821 linux_sys_recv(struct lwp *l, const struct linux_sys_recv_args *uap, register_t *retval)
   1822 {
   1823 	/* {
   1824 		syscallarg(int) s;
   1825 		syscallarg(void *) buf;
   1826 		syscallarg(int) len;
   1827 		syscallarg(int) flags;
   1828 	} */
   1829 	struct sys_recvfrom_args bra;
   1830 
   1831 	memset(&bra, 0, sizeof(bra));
   1832 	SCARG(&bra, s) = SCARG(uap, s);
   1833 	SCARG(&bra, buf) = SCARG(uap, buf);
   1834 	SCARG(&bra, len) = (size_t) SCARG(uap, len);
   1835 	SCARG(&bra, flags) = SCARG(uap, flags);
   1836 	SCARG(&bra, from) = NULL;
   1837 	SCARG(&bra, fromlenaddr) = NULL;
   1838 
   1839 	return (sys_recvfrom(l, &bra, retval));
   1840 }
   1841 
   1842 int
   1843 linux_sys_send(struct lwp *l, const struct linux_sys_send_args *uap, register_t *retval)
   1844 {
   1845 	/* {
   1846 		syscallarg(int) s;
   1847 		syscallarg(void *) buf;
   1848 		syscallarg(int) len;
   1849 		syscallarg(int) flags;
   1850 	} */
   1851 	struct sys_sendto_args bsa;
   1852 
   1853 	memset(&bsa, 0, sizeof(bsa));
   1854 	SCARG(&bsa, s)		= SCARG(uap, s);
   1855 	SCARG(&bsa, buf)	= SCARG(uap, buf);
   1856 	SCARG(&bsa, len)	= SCARG(uap, len);
   1857 	SCARG(&bsa, flags)	= SCARG(uap, flags);
   1858 	SCARG(&bsa, to)		= NULL;
   1859 	SCARG(&bsa, tolen)	= 0;
   1860 
   1861 	return (sys_sendto(l, &bsa, retval));
   1862 }
   1863 #endif
   1864 
   1865 int
   1866 linux_sys_accept(struct lwp *l, const struct linux_sys_accept_args *uap, register_t *retval)
   1867 {
   1868 	/* {
   1869 		syscallarg(int) s;
   1870 		syscallarg(struct osockaddr *) name;
   1871 		syscallarg(int *) anamelen;
   1872 	} */
   1873 	int error;
   1874 	struct sys_accept_args baa;
   1875 
   1876 	memset(&baa, 0, sizeof(baa));
   1877 	SCARG(&baa, s)		= SCARG(uap, s);
   1878 	SCARG(&baa, name)	= (struct sockaddr *) SCARG(uap, name);
   1879 	SCARG(&baa, anamelen)	= (unsigned int *) SCARG(uap, anamelen);
   1880 
   1881 	if ((error = sys_accept(l, &baa, retval)))
   1882 		return (error);
   1883 
   1884 	if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
   1885 		return (error);
   1886 
   1887 	return (0);
   1888 }
   1889 
   1890 int
   1891 linux_sys_accept4(struct lwp *l, const struct linux_sys_accept4_args *uap, register_t *retval)
   1892 {
   1893 	/* {
   1894 		syscallarg(int) s;
   1895 		syscallarg(struct osockaddr *) name;
   1896 		syscallarg(int *) anamelen;
   1897 		syscallarg(int) flags;
   1898 	} */
   1899 	int error, flags;
   1900 	struct sockaddr_big name;
   1901 
   1902 	if ((flags = linux_to_bsd_type(SCARG(uap, flags))) == -1)
   1903 		return EINVAL;
   1904 
   1905 	name.sb_len = UCHAR_MAX;
   1906 	error = do_sys_accept(l, SCARG(uap, s), (struct sockaddr *)&name,
   1907 	    retval, NULL, flags, 0);
   1908 	if (error != 0)
   1909 		return error;
   1910 
   1911 	error = copyout_sockname_sb((struct sockaddr *)SCARG(uap, name),
   1912 	    SCARG(uap, anamelen), MSG_LENUSRSPACE, &name);
   1913 	if (error != 0) {
   1914 		int fd = (int)*retval;
   1915 		if (fd_getfile(fd) != NULL)
   1916 			(void)fd_close(fd);
   1917 		return error;
   1918 	}
   1919 	if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
   1920 		return error;
   1921 
   1922 	return 0;
   1923 }
   1924 
   1925 int
   1926 linux_sys_sendmmsg(struct lwp *l, const struct linux_sys_sendmmsg_args *uap,
   1927     register_t *retval)
   1928 {
   1929 	/* {
   1930 		syscallarg(int) s;
   1931 		syscallarg(struct linux_mmsghdr *) msgvec;
   1932 		syscallarg(unsigned int) vlen;
   1933 		syscallarg(unsigned int) flags;
   1934 	} */
   1935 	struct linux_mmsghdr lmsg;
   1936 	struct mmsghdr bmsg;
   1937 	struct socket *so;
   1938 	file_t *fp;
   1939 	struct msghdr *msg = &bmsg.msg_hdr;
   1940 	int error, s;
   1941 	unsigned int vlen, flags, dg;
   1942 
   1943 	if ((flags = linux_to_bsd_msg_flags(SCARG(uap, flags))) == -1)
   1944 		return EINVAL;
   1945 
   1946 	flags = (flags & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
   1947 
   1948 	s = SCARG(uap, s);
   1949 	if ((error = fd_getsock1(s, &so, &fp)) != 0)
   1950 		return error;
   1951 
   1952 	vlen = SCARG(uap, vlen);
   1953 	if (vlen > 1024)
   1954 		vlen = 1024;
   1955 
   1956 	for (dg = 0; dg < vlen;) {
   1957 		error = copyin(SCARG(uap, msgvec) + dg, &lmsg, sizeof(lmsg));
   1958 		if (error)
   1959 			break;
   1960 		error = linux_to_bsd_msghdr(&lmsg.msg_hdr, &bmsg.msg_hdr);
   1961 		if (error)
   1962 			break;
   1963 
   1964 		msg->msg_flags = flags;
   1965 
   1966 		error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
   1967 		if (error)
   1968 			break;
   1969 
   1970 		ktrkuser("msghdr", msg, sizeof *msg);
   1971 		lmsg.msg_len = *retval;
   1972 		error = copyout(&lmsg, SCARG(uap, msgvec) + dg, sizeof(lmsg));
   1973 		if (error)
   1974 			break;
   1975 		dg++;
   1976 
   1977 	}
   1978 
   1979 	*retval = dg;
   1980 
   1981 	fd_putfile(s);
   1982 
   1983 	/*
   1984 	 * If we succeeded at least once, return 0.
   1985 	 */
   1986 	if (dg)
   1987 		return 0;
   1988 	return error;
   1989 }
   1990 
   1991 int
   1992 linux_sys_recvmmsg(struct lwp *l, const struct linux_sys_recvmmsg_args *uap,
   1993     register_t *retval)
   1994 {
   1995 	/* {
   1996 		syscallarg(int) s;
   1997 		syscallarg(struct linux_mmsghdr *) msgvec;
   1998 		syscallarg(unsigned int) vlen;
   1999 		syscallarg(unsigned int) flags;
   2000 		syscallarg(struct linux_timespec *) timeout;
   2001 	} */
   2002 	struct linux_mmsghdr lmsg;
   2003 	struct mmsghdr bmsg;
   2004 	struct socket *so;
   2005 	struct msghdr *msg = &bmsg.msg_hdr;
   2006 	int error, s;
   2007 	struct mbuf *from, *control;
   2008 	struct timespec ts = {0}, now;
   2009 	struct linux_timespec lts;
   2010 	unsigned int vlen, flags, dg;
   2011 
   2012 	if (SCARG(uap, timeout)) {
   2013 		error = copyin(SCARG(uap, timeout), &lts, sizeof(lts));
   2014 		if (error)
   2015 			return error;
   2016 		ts.tv_sec = lts.tv_sec;
   2017 		ts.tv_nsec = lts.tv_nsec;
   2018 		getnanotime(&now);
   2019 		timespecadd(&now, &ts, &ts);
   2020 	}
   2021 
   2022 	s = SCARG(uap, s);
   2023 	if ((error = fd_getsock(s, &so)) != 0)
   2024 		return error;
   2025 
   2026 	/*
   2027 	 * If so->so_rerror holds a deferred error return it now.
   2028 	 */
   2029 	if (so->so_rerror) {
   2030 		error = so->so_rerror;
   2031 		so->so_rerror = 0;
   2032 		fd_putfile(s);
   2033 		return error;
   2034 	}
   2035 
   2036 	vlen = SCARG(uap, vlen);
   2037 	if (vlen > 1024)
   2038 		vlen = 1024;
   2039 
   2040 	from = NULL;
   2041 	flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
   2042 
   2043 	for (dg = 0; dg < vlen;) {
   2044 		error = copyin(SCARG(uap, msgvec) + dg, &lmsg, sizeof(lmsg));
   2045 		if (error)
   2046 			break;
   2047 		error = linux_to_bsd_msghdr(&lmsg.msg_hdr, &bmsg.msg_hdr);
   2048 		if (error)
   2049 			break;
   2050 		msg->msg_flags = flags & ~MSG_WAITFORONE;
   2051 
   2052 		if (from != NULL) {
   2053 			m_free(from);
   2054 			from = NULL;
   2055 		}
   2056 
   2057 		error = do_sys_recvmsg_so(l, s, so, msg, &from,
   2058 		    msg->msg_control != NULL ? &control : NULL, retval);
   2059 		if (error) {
   2060 			if (error == EAGAIN && dg > 0)
   2061 				error = 0;
   2062 			break;
   2063 		}
   2064 
   2065 		if (msg->msg_control != NULL)
   2066 			error = linux_copyout_msg_control(l, msg, control);
   2067 		if (error)
   2068 			break;
   2069 
   2070 		if (from != NULL) {
   2071 			mtod(from, struct osockaddr *)->sa_family =
   2072 			    bsd_to_linux_domain(mtod(from,
   2073 			    struct sockaddr *)->sa_family);
   2074 			error = copyout_sockname(msg->msg_name,
   2075 			    &msg->msg_namelen, 0, from);
   2076 			if (error)
   2077 				break;
   2078 		}
   2079 
   2080 
   2081 		lmsg.msg_len = *retval;
   2082 		ktrkuser("msghdr", msg, sizeof(*msg));
   2083 		bsd_to_linux_msghdr(msg, &lmsg.msg_hdr);
   2084 		error = copyout(&lmsg, SCARG(uap, msgvec) + dg, sizeof(lmsg));
   2085 		if (error)
   2086 			break;
   2087 
   2088 		dg++;
   2089 		if (msg->msg_flags & MSG_OOB)
   2090 			break;
   2091 
   2092 		if (SCARG(uap, timeout)) {
   2093 			getnanotime(&now);
   2094 			timespecsub(&now, &ts, &now);
   2095 			if (now.tv_sec > 0)
   2096 				break;
   2097 		}
   2098 
   2099 		if (flags & MSG_WAITFORONE)
   2100 			flags |= MSG_DONTWAIT;
   2101 
   2102 	}
   2103 
   2104 	if (from != NULL)
   2105 		m_free(from);
   2106 
   2107 	*retval = dg;
   2108 
   2109 	/*
   2110 	 * If we succeeded at least once, return 0, hopefully so->so_rerror
   2111 	 * will catch it next time.
   2112 	 */
   2113 	if (error && dg > 0) {
   2114 		so->so_rerror = error;
   2115 		error = 0;
   2116 	}
   2117 
   2118 	fd_putfile(s);
   2119 
   2120 	return error;
   2121 }
   2122