xref: /netbsd-src/sys/compat/linux/common/linux_socket.c (revision 19ef5b5b0bcb90f63509df6e78769de1b57c2758)
1 /*	$NetBSD: linux_socket.c,v 1.118 2014/05/17 21:26:20 rmind 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.118 2014/05/17 21:26:20 rmind 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/malloc.h>
49 #include <sys/ioctl.h>
50 #include <sys/tty.h>
51 #include <sys/file.h>
52 #include <sys/filedesc.h>
53 #include <sys/select.h>
54 #include <sys/socket.h>
55 #include <sys/socketvar.h>
56 #include <sys/domain.h>
57 #include <net/if.h>
58 #include <net/if_dl.h>
59 #include <net/if_types.h>
60 #include <netinet/in.h>
61 #include <netinet/tcp.h>
62 #include <sys/mount.h>
63 #include <sys/proc.h>
64 #include <sys/vnode.h>
65 #include <sys/device.h>
66 #include <sys/protosw.h>
67 #include <sys/mbuf.h>
68 #include <sys/syslog.h>
69 #include <sys/exec.h>
70 #include <sys/kauth.h>
71 #include <sys/syscallargs.h>
72 #include <sys/ktrace.h>
73 #include <sys/fcntl.h>
74 
75 #include <lib/libkern/libkern.h>
76 
77 #include <netinet/ip6.h>
78 #include <netinet6/ip6_var.h>
79 
80 #include <compat/sys/socket.h>
81 #include <compat/sys/sockio.h>
82 
83 #include <compat/linux/common/linux_types.h>
84 #include <compat/linux/common/linux_util.h>
85 #include <compat/linux/common/linux_signal.h>
86 #include <compat/linux/common/linux_ioctl.h>
87 #include <compat/linux/common/linux_socket.h>
88 #include <compat/linux/common/linux_fcntl.h>
89 #if !defined(__alpha__) && !defined(__amd64__)
90 #include <compat/linux/common/linux_socketcall.h>
91 #endif
92 #include <compat/linux/common/linux_sockio.h>
93 #include <compat/linux/common/linux_ipc.h>
94 #include <compat/linux/common/linux_sem.h>
95 
96 #include <compat/linux/linux_syscallargs.h>
97 
98 #ifdef DEBUG_LINUX
99 #define DPRINTF(a) uprintf a
100 #else
101 #define DPRINTF(a)
102 #endif
103 
104 /*
105  * The calls in this file are entered either via the linux_socketcall()
106  * interface or, on the Alpha, as individual syscalls.  The
107  * linux_socketcall function does any massaging of arguments so that all
108  * the calls in here need not think that they are anything other
109  * than a normal syscall.
110  */
111 
112 static int linux_to_bsd_domain(int);
113 static int bsd_to_linux_domain(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 mbuf **,
124 		const struct osockaddr *, unsigned int);
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 void linux_to_bsd_msghdr(struct linux_msghdr *, struct msghdr *);
129 static void bsd_to_linux_msghdr(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_msg_flags(int lflag)
242 {
243 	int i, lfl, bfl;
244 	int bflag = 0;
245 
246 	if (lflag == 0)
247 		return (0);
248 
249 	for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
250 		bfl = bsd_to_linux_msg_flags_[i].bfl;
251 		lfl = bsd_to_linux_msg_flags_[i].lfl;
252 
253 		if (lfl == 0)
254 			continue;
255 
256 		if (lflag & lfl) {
257 			if (bfl < 0)
258 				return (-1);
259 
260 			bflag |= bfl;
261 		}
262 	}
263 
264 	return (bflag);
265 }
266 
267 static int
268 bsd_to_linux_msg_flags(int bflag)
269 {
270 	int i, lfl, bfl;
271 	int lflag = 0;
272 
273 	if (bflag == 0)
274 		return (0);
275 
276 	for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
277 		bfl = bsd_to_linux_msg_flags_[i].bfl;
278 		lfl = bsd_to_linux_msg_flags_[i].lfl;
279 
280 		if (bfl <= 0)
281 			continue;
282 
283 		if (bflag & bfl) {
284 			if (lfl < 0)
285 				return (-1);
286 
287 			lflag |= lfl;
288 		}
289 	}
290 
291 	return (lflag);
292 }
293 
294 int
295 linux_sys_socket(struct lwp *l, const struct linux_sys_socket_args *uap, register_t *retval)
296 {
297 	/* {
298 		syscallarg(int)	domain;
299 		syscallarg(int)	type;
300 		syscallarg(int) protocol;
301 	} */
302 	struct sys___socket30_args bsa;
303 	struct sys_fcntl_args fsa;
304 	register_t fretval[2];
305 	int error, flags;
306 
307 
308 	SCARG(&bsa, protocol) = SCARG(uap, protocol);
309 	SCARG(&bsa, type) = SCARG(uap, type) & LINUX_SOCK_TYPE_MASK;
310 	SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
311 	if (SCARG(&bsa, domain) == -1)
312 		return EINVAL;
313 	/*
314 	 * Apparently linux uses this to talk to ISDN sockets. If we fail
315 	 * now programs seems to handle it, but if we don't we are going
316 	 * to fail when we bind and programs don't handle this well.
317 	 */
318 	if (SCARG(&bsa, domain) == AF_ROUTE && SCARG(&bsa, type) == SOCK_RAW)
319 		return ENOTSUP;
320 	flags = SCARG(uap, type) & ~LINUX_SOCK_TYPE_MASK;
321 	if (flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
322 		return EINVAL;
323 	error = sys___socket30(l, &bsa, retval);
324 
325 	/*
326 	 * Linux overloads the "type" parameter to include some
327 	 * fcntl flags to be set on the file descriptor.
328 	 * Process those if creating the socket succeeded.
329 	 */
330 
331 	if (!error && flags & LINUX_SOCK_CLOEXEC) {
332 		SCARG(&fsa, fd) = *retval;
333 		SCARG(&fsa, cmd) = F_SETFD;
334 		SCARG(&fsa, arg) = (void *)(uintptr_t)FD_CLOEXEC;
335 		(void) sys_fcntl(l, &fsa, fretval);
336 	}
337 	if (!error && flags & LINUX_SOCK_NONBLOCK) {
338 		SCARG(&fsa, fd) = *retval;
339 		SCARG(&fsa, cmd) = F_SETFL;
340 		SCARG(&fsa, arg) = (void *)(uintptr_t)O_NONBLOCK;
341 		error = sys_fcntl(l, &fsa, fretval);
342 		if (error) {
343 			struct sys_close_args csa;
344 
345 			SCARG(&csa, fd) = *retval;
346 			(void) sys_close(l, &csa, fretval);
347 		}
348 	}
349 
350 #ifdef INET6
351 	/*
352 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
353 	 * default and some apps depend on this. So, set V6ONLY to 0
354 	 * for Linux apps if the sysctl value is set to 1.
355 	 */
356 	if (!error && ip6_v6only && SCARG(&bsa, domain) == PF_INET6) {
357 		struct socket *so;
358 
359 		if (fd_getsock(*retval, &so) == 0) {
360 			int val = 0;
361 
362 			/* ignore error */
363 			(void)so_setsockopt(l, so, IPPROTO_IPV6, IPV6_V6ONLY,
364 			    &val, sizeof(val));
365 
366 			fd_putfile(*retval);
367 		}
368 	}
369 #endif
370 
371 	return (error);
372 }
373 
374 int
375 linux_sys_socketpair(struct lwp *l, const struct linux_sys_socketpair_args *uap, register_t *retval)
376 {
377 	/* {
378 		syscallarg(int) domain;
379 		syscallarg(int) type;
380 		syscallarg(int) protocol;
381 		syscallarg(int *) rsv;
382 	} */
383 	struct sys_socketpair_args bsa;
384 
385 	SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
386 	if (SCARG(&bsa, domain) == -1)
387 		return EINVAL;
388 	SCARG(&bsa, type) = SCARG(uap, type);
389 	SCARG(&bsa, protocol) = SCARG(uap, protocol);
390 	SCARG(&bsa, rsv) = SCARG(uap, rsv);
391 
392 	return sys_socketpair(l, &bsa, retval);
393 }
394 
395 int
396 linux_sys_sendto(struct lwp *l, const struct linux_sys_sendto_args *uap, register_t *retval)
397 {
398 	/* {
399 		syscallarg(int)				s;
400 		syscallarg(void *)			msg;
401 		syscallarg(int)				len;
402 		syscallarg(int)				flags;
403 		syscallarg(struct osockaddr *)		to;
404 		syscallarg(int)				tolen;
405 	} */
406 	struct msghdr   msg;
407 	struct iovec    aiov;
408 	struct mbuf *nam;
409 	int bflags;
410 	int error;
411 
412 	/* Translate message flags.  */
413 	bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
414 	if (bflags < 0)
415 		/* Some supported flag */
416 		return EINVAL;
417 
418 	msg.msg_flags = 0;
419 	msg.msg_name = NULL;
420 	msg.msg_control = NULL;
421 
422 	if (SCARG(uap, tolen)) {
423 		/* Read in and convert the sockaddr */
424 		error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, to),
425 		    SCARG(uap, tolen));
426 		if (error)
427 			return (error);
428 		msg.msg_flags |= MSG_NAMEMBUF;
429 		msg.msg_name = nam;
430 		msg.msg_namelen = SCARG(uap, tolen);
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 void
442 linux_to_bsd_msghdr(struct linux_msghdr *lmsg, struct msghdr *bmsg)
443 {
444 	bmsg->msg_name = lmsg->msg_name;
445 	bmsg->msg_namelen = lmsg->msg_namelen;
446 	bmsg->msg_iov = lmsg->msg_iov;
447 	bmsg->msg_iovlen = lmsg->msg_iovlen;
448 	bmsg->msg_control = lmsg->msg_control;
449 	bmsg->msg_controllen = lmsg->msg_controllen;
450 	bmsg->msg_flags = lmsg->msg_flags;
451 }
452 
453 static void
454 bsd_to_linux_msghdr(struct msghdr *bmsg, struct linux_msghdr *lmsg)
455 {
456 	lmsg->msg_name = bmsg->msg_name;
457 	lmsg->msg_namelen = bmsg->msg_namelen;
458 	lmsg->msg_iov = bmsg->msg_iov;
459 	lmsg->msg_iovlen = bmsg->msg_iovlen;
460 	lmsg->msg_control = bmsg->msg_control;
461 	lmsg->msg_controllen = bmsg->msg_controllen;
462 	lmsg->msg_flags = bmsg->msg_flags;
463 }
464 
465 int
466 linux_sys_sendmsg(struct lwp *l, const struct linux_sys_sendmsg_args *uap, register_t *retval)
467 {
468 	/* {
469 		syscallarg(int) s;
470 		syscallarg(struct linux_msghdr *) msg;
471 		syscallarg(u_int) flags;
472 	} */
473 	struct msghdr	msg;
474 	struct linux_msghdr lmsg;
475 	int		error;
476 	int		bflags;
477 	struct mbuf     *nam;
478 	u_int8_t	*control;
479 	struct mbuf     *ctl_mbuf = NULL;
480 
481 	error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg));
482 	if (error)
483 		return error;
484 	linux_to_bsd_msghdr(&lmsg, &msg);
485 
486 	msg.msg_flags = MSG_IOVUSRSPACE;
487 
488 	/*
489 	 * Translate message flags.
490 	 */
491 	bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
492 	if (bflags < 0)
493 		/* Some supported flag */
494 		return EINVAL;
495 
496 	if (lmsg.msg_name) {
497 		/* Read in and convert the sockaddr */
498 		error = linux_get_sa(l, SCARG(uap, s), &nam, msg.msg_name,
499 		    msg.msg_namelen);
500 		if (error)
501 			return (error);
502 		msg.msg_flags |= MSG_NAMEMBUF;
503 		msg.msg_name = nam;
504 	}
505 
506 	/*
507 	 * Handle cmsg if there is any.
508 	 */
509 	if (LINUX_CMSG_FIRSTHDR(&lmsg)) {
510 		struct linux_cmsghdr l_cmsg, *l_cc;
511 		struct cmsghdr *cmsg;
512 		ssize_t resid = msg.msg_controllen;
513 		size_t clen, cidx = 0, cspace;
514 
515 		ctl_mbuf = m_get(M_WAIT, MT_CONTROL);
516 		clen = MLEN;
517 		control = mtod(ctl_mbuf, void *);
518 
519 		l_cc = LINUX_CMSG_FIRSTHDR(&lmsg);
520 		do {
521 			error = copyin(l_cc, &l_cmsg, sizeof(l_cmsg));
522 			if (error)
523 				goto done;
524 
525 			/*
526 			 * Sanity check the control message length.
527 			 */
528 			if (l_cmsg.cmsg_len > resid
529 			    || l_cmsg.cmsg_len < sizeof l_cmsg) {
530 				error = EINVAL;
531 				goto done;
532 			}
533 
534 			/*
535 			 * Refuse unsupported control messages, and
536 			 * translate fields as appropriate.
537 			 */
538 			switch (l_cmsg.cmsg_level) {
539 			case LINUX_SOL_SOCKET:
540 				/* It only differs on some archs */
541 				if (LINUX_SOL_SOCKET != SOL_SOCKET)
542 					l_cmsg.cmsg_level = SOL_SOCKET;
543 
544 				switch(l_cmsg.cmsg_type) {
545 				case LINUX_SCM_RIGHTS:
546 					/* Linux SCM_RIGHTS is same as NetBSD */
547 					break;
548 
549 				case LINUX_SCM_CREDENTIALS:
550 					/* no native equivalent, just drop it */
551 					m_free(ctl_mbuf);
552 					ctl_mbuf = NULL;
553 					msg.msg_control = NULL;
554 					msg.msg_controllen = 0;
555 					goto skipcmsg;
556 
557 				default:
558 					/* other types not supported */
559 					error = EINVAL;
560 					goto done;
561 				}
562 				break;
563 			default:
564 				/* pray and leave intact */
565 				break;
566 			}
567 
568 			cspace = CMSG_SPACE(l_cmsg.cmsg_len - sizeof(l_cmsg));
569 
570 			/* Check the buffer is big enough */
571 			if (__predict_false(cidx + cspace > clen)) {
572 				u_int8_t *nc;
573 
574 				clen = cidx + cspace;
575 				if (clen >= PAGE_SIZE) {
576 					error = EINVAL;
577 					goto done;
578 				}
579 				nc = realloc(clen <= MLEN ? NULL : control,
580 						clen, M_TEMP, M_WAITOK);
581 				if (!nc) {
582 					error = ENOMEM;
583 					goto done;
584 				}
585 				if (cidx <= MLEN)
586 					/* Old buffer was in mbuf... */
587 					memcpy(nc, control, cidx);
588 				control = nc;
589 			}
590 
591 			/* Copy header */
592 			cmsg = (void *)&control[cidx];
593 			cmsg->cmsg_len = l_cmsg.cmsg_len + LINUX_CMSG_ALIGN_DELTA;
594 			cmsg->cmsg_level = l_cmsg.cmsg_level;
595 			cmsg->cmsg_type = l_cmsg.cmsg_type;
596 
597 			/* Zero area between header and data */
598 			memset(cmsg + 1, 0,
599 				CMSG_ALIGN(sizeof(*cmsg)) - sizeof(*cmsg));
600 
601 			/* Copyin the data */
602 			error = copyin(LINUX_CMSG_DATA(l_cc),
603 				CMSG_DATA(cmsg),
604 				l_cmsg.cmsg_len - sizeof(l_cmsg));
605 			if (error)
606 				goto done;
607 
608 			resid -= LINUX_CMSG_ALIGN(l_cmsg.cmsg_len);
609 			cidx += cspace;
610 		} while ((l_cc = LINUX_CMSG_NXTHDR(&msg, l_cc)) && resid > 0);
611 
612 		/* If we allocated a buffer, attach to mbuf */
613 		if (cidx > MLEN) {
614 			MEXTADD(ctl_mbuf, control, clen, M_MBUF, NULL, NULL);
615 			ctl_mbuf->m_flags |= M_EXT_RW;
616 		}
617 		control = NULL;
618 		ctl_mbuf->m_len = cidx;
619 
620 		msg.msg_control = ctl_mbuf;
621 		msg.msg_flags |= MSG_CONTROLMBUF;
622 
623 		ktrkuser("mbcontrol", mtod(ctl_mbuf, void *),
624 		    msg.msg_controllen);
625 	}
626 
627 skipcmsg:
628 	error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
629 	/* Freed internally */
630 	ctl_mbuf = NULL;
631 
632 done:
633 	if (ctl_mbuf != NULL) {
634 		if (control != NULL && control != mtod(ctl_mbuf, void *))
635 			free(control, M_MBUF);
636 		m_free(ctl_mbuf);
637 	}
638 	return (error);
639 }
640 
641 int
642 linux_sys_recvfrom(struct lwp *l, const struct linux_sys_recvfrom_args *uap, register_t *retval)
643 {
644 	/* {
645 		syscallarg(int) s;
646 		syscallarg(void *) buf;
647 		syscallarg(int) len;
648 		syscallarg(int) flags;
649 		syscallarg(struct osockaddr *) from;
650 		syscallarg(int *) fromlenaddr;
651 	} */
652 	int		error;
653 	struct sys_recvfrom_args bra;
654 
655 	SCARG(&bra, s) = SCARG(uap, s);
656 	SCARG(&bra, buf) = SCARG(uap, buf);
657 	SCARG(&bra, len) = SCARG(uap, len);
658 	SCARG(&bra, flags) = SCARG(uap, flags);
659 	SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from);
660 	SCARG(&bra, fromlenaddr) = (socklen_t *)SCARG(uap, fromlenaddr);
661 
662 	if ((error = sys_recvfrom(l, &bra, retval)))
663 		return (error);
664 
665 	if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from))))
666 		return (error);
667 
668 	return (0);
669 }
670 
671 static int
672 linux_copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
673 {
674 	int dlen, error = 0;
675 	struct cmsghdr *cmsg;
676 	struct linux_cmsghdr linux_cmsg;
677 	struct mbuf *m;
678 	char *q, *q_end;
679 
680 	if (mp->msg_controllen <= 0 || control == 0) {
681 		mp->msg_controllen = 0;
682 		free_control_mbuf(l, control, control);
683 		return 0;
684 	}
685 
686 	ktrkuser("msgcontrol", mtod(control, void *), mp->msg_controllen);
687 
688 	q = (char *)mp->msg_control;
689 	q_end = q + mp->msg_controllen;
690 
691 	for (m = control; m != NULL; ) {
692 		cmsg = mtod(m, struct cmsghdr *);
693 
694 		/*
695 		 * Fixup cmsg. We handle two things:
696 		 * 0. different sizeof cmsg_len.
697 		 * 1. different values for level/type on some archs
698 		 * 2. different alignment of CMSG_DATA on some archs
699 		 */
700 		linux_cmsg.cmsg_len = cmsg->cmsg_len - LINUX_CMSG_ALIGN_DELTA;
701 		linux_cmsg.cmsg_level = cmsg->cmsg_level;
702 		linux_cmsg.cmsg_type = cmsg->cmsg_type;
703 
704 		dlen = q_end - q;
705 		if (linux_cmsg.cmsg_len > dlen) {
706 			/* Not enough room for the parameter */
707 			dlen -= sizeof linux_cmsg;
708 			if (dlen <= 0)
709 				/* Discard if header wont fit */
710 				break;
711 			mp->msg_flags |= MSG_CTRUNC;
712 			if (linux_cmsg.cmsg_level == SOL_SOCKET
713 			    && linux_cmsg.cmsg_type == SCM_RIGHTS)
714 				/* Do not truncate me ... */
715 				break;
716 		} else
717 			dlen = linux_cmsg.cmsg_len - sizeof linux_cmsg;
718 
719 		switch (linux_cmsg.cmsg_level) {
720 		case SOL_SOCKET:
721 			linux_cmsg.cmsg_level = LINUX_SOL_SOCKET;
722 			switch (linux_cmsg.cmsg_type) {
723 			case SCM_RIGHTS:
724 				/* Linux SCM_RIGHTS is same as NetBSD */
725 				break;
726 
727 			default:
728 				/* other types not supported */
729 				error = EINVAL;
730 				goto done;
731 			}
732 			/* machine dependent ! */
733 			break;
734 		default:
735 			/* pray and leave intact */
736 			break;
737 		}
738 
739 		/* There can be padding between the header and data... */
740 		error = copyout(&linux_cmsg, q, sizeof linux_cmsg);
741 		if (error != 0) {
742 			error = copyout(CCMSG_DATA(cmsg), q + sizeof linux_cmsg,
743 			    dlen);
744 		}
745 		if (error != 0) {
746 			/* We must free all the SCM_RIGHTS */
747 			m = control;
748 			break;
749 		}
750 		m = m->m_next;
751 		if (m == NULL || q + LINUX_CMSG_SPACE(dlen) > q_end) {
752 			q += LINUX_CMSG_LEN(dlen);
753 			break;
754 		}
755 		q += LINUX_CMSG_SPACE(dlen);
756 	}
757 
758   done:
759 	free_control_mbuf(l, control, m);
760 
761 	mp->msg_controllen = q - (char *)mp->msg_control;
762 	return error;
763 }
764 
765 int
766 linux_sys_recvmsg(struct lwp *l, const struct linux_sys_recvmsg_args *uap, register_t *retval)
767 {
768 	/* {
769 		syscallarg(int) s;
770 		syscallarg(struct linux_msghdr *) msg;
771 		syscallarg(u_int) flags;
772 	} */
773 	struct msghdr	msg;
774 	struct linux_msghdr lmsg;
775 	int		error;
776 	struct mbuf	*from, *control;
777 
778 	error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg));
779 	if (error)
780 		return (error);
781 	linux_to_bsd_msghdr(&lmsg, &msg);
782 
783 	msg.msg_flags = linux_to_bsd_msg_flags(SCARG(uap, flags));
784 	if (msg.msg_flags < 0) {
785 		/* Some unsupported flag */
786 		return (EINVAL);
787 	}
788 	msg.msg_flags |= MSG_IOVUSRSPACE;
789 
790 	error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
791 	    msg.msg_control != NULL ? &control : NULL, retval);
792 	if (error != 0)
793 		return error;
794 
795 	if (msg.msg_control != NULL)
796 		error = linux_copyout_msg_control(l, &msg, control);
797 
798 	if (error == 0 && from != 0) {
799 		mtod(from, struct osockaddr *)->sa_family =
800 		    bsd_to_linux_domain(mtod(from, struct sockaddr *)->sa_family);
801 		error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
802 			from);
803 	} else
804 		msg.msg_namelen = 0;
805 
806 	if (from != NULL)
807 		m_free(from);
808 
809 	if (error == 0) {
810 		msg.msg_flags = bsd_to_linux_msg_flags(msg.msg_flags);
811 		if (msg.msg_flags < 0)
812 			/* Some flag unsupported by Linux */
813 			error = EINVAL;
814 		else {
815 			ktrkuser("msghdr", &msg, sizeof(msg));
816 			bsd_to_linux_msghdr(&msg, &lmsg);
817 			error = copyout(&lmsg, SCARG(uap, msg), sizeof(lmsg));
818 		}
819 	}
820 
821 	return (error);
822 }
823 
824 /*
825  * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
826  * is different, the rest matches IPPROTO_* on both systems.
827  */
828 int
829 linux_to_bsd_sopt_level(int llevel)
830 {
831 
832 	switch (llevel) {
833 	case LINUX_SOL_SOCKET:
834 		return SOL_SOCKET;
835 	case LINUX_SOL_IP:
836 		return IPPROTO_IP;
837 #ifdef INET6
838 	case LINUX_SOL_IPV6:
839 		return IPPROTO_IPV6;
840 #endif
841 	case LINUX_SOL_TCP:
842 		return IPPROTO_TCP;
843 	case LINUX_SOL_UDP:
844 		return IPPROTO_UDP;
845 	default:
846 		return -1;
847 	}
848 }
849 
850 /*
851  * Convert Linux socket level socket option numbers to NetBSD values.
852  */
853 int
854 linux_to_bsd_so_sockopt(int lopt)
855 {
856 
857 	switch (lopt) {
858 	case LINUX_SO_DEBUG:
859 		return SO_DEBUG;
860 	case LINUX_SO_REUSEADDR:
861 		/*
862 		 * Linux does not implement SO_REUSEPORT, but allows reuse of a
863 		 * host:port pair through SO_REUSEADDR even if the address is not a
864 		 * multicast-address.  Effectively, this means that we should use
865 		 * SO_REUSEPORT to allow Linux applications to not exit with
866 		 * EADDRINUSE
867 		 */
868 		return SO_REUSEPORT;
869 	case LINUX_SO_TYPE:
870 		return SO_TYPE;
871 	case LINUX_SO_ERROR:
872 		return SO_ERROR;
873 	case LINUX_SO_DONTROUTE:
874 		return SO_DONTROUTE;
875 	case LINUX_SO_BROADCAST:
876 		return SO_BROADCAST;
877 	case LINUX_SO_SNDBUF:
878 		return SO_SNDBUF;
879 	case LINUX_SO_RCVBUF:
880 		return SO_RCVBUF;
881 	case LINUX_SO_SNDLOWAT:
882 		return SO_SNDLOWAT;
883 	case LINUX_SO_RCVLOWAT:
884 		return SO_RCVLOWAT;
885 	case LINUX_SO_KEEPALIVE:
886 		return SO_KEEPALIVE;
887 	case LINUX_SO_OOBINLINE:
888 		return SO_OOBINLINE;
889 	case LINUX_SO_LINGER:
890 		return SO_LINGER;
891 	case LINUX_SO_ACCEPTCONN:
892 		return SO_ACCEPTCONN;
893 	case LINUX_SO_PRIORITY:
894 	case LINUX_SO_NO_CHECK:
895 	default:
896 		return -1;
897 	}
898 }
899 
900 /*
901  * Convert Linux IP level socket option number to NetBSD values.
902  */
903 int
904 linux_to_bsd_ip_sockopt(int lopt)
905 {
906 
907 	switch (lopt) {
908 	case LINUX_IP_TOS:
909 		return IP_TOS;
910 	case LINUX_IP_TTL:
911 		return IP_TTL;
912 	case LINUX_IP_HDRINCL:
913 		return IP_HDRINCL;
914 	case LINUX_IP_MULTICAST_TTL:
915 		return IP_MULTICAST_TTL;
916 	case LINUX_IP_MULTICAST_LOOP:
917 		return IP_MULTICAST_LOOP;
918 	case LINUX_IP_MULTICAST_IF:
919 		return IP_MULTICAST_IF;
920 	case LINUX_IP_ADD_MEMBERSHIP:
921 		return IP_ADD_MEMBERSHIP;
922 	case LINUX_IP_DROP_MEMBERSHIP:
923 		return IP_DROP_MEMBERSHIP;
924 	default:
925 		return -1;
926 	}
927 }
928 
929 /*
930  * Convert Linux IPV6 level socket option number to NetBSD values.
931  */
932 #ifdef INET6
933 int
934 linux_to_bsd_ipv6_sockopt(int lopt)
935 {
936 
937 	switch (lopt) {
938 	case LINUX_IPV6_V6ONLY:
939 		return IPV6_V6ONLY;
940 	default:
941 		return -1;
942 	}
943 }
944 #endif
945 
946 /*
947  * Convert Linux TCP level socket option number to NetBSD values.
948  */
949 int
950 linux_to_bsd_tcp_sockopt(int lopt)
951 {
952 
953 	switch (lopt) {
954 	case LINUX_TCP_NODELAY:
955 		return TCP_NODELAY;
956 	case LINUX_TCP_MAXSEG:
957 		return TCP_MAXSEG;
958 	default:
959 		return -1;
960 	}
961 }
962 
963 /*
964  * Convert Linux UDP level socket option number to NetBSD values.
965  */
966 int
967 linux_to_bsd_udp_sockopt(int lopt)
968 {
969 
970 	switch (lopt) {
971 	default:
972 		return -1;
973 	}
974 }
975 
976 /*
977  * Another reasonably straightforward function: setsockopt(2).
978  * The level and option numbers are converted; the values passed
979  * are not (yet) converted, the ones currently implemented don't
980  * need conversion, as they are the same on both systems.
981  */
982 int
983 linux_sys_setsockopt(struct lwp *l, const struct linux_sys_setsockopt_args *uap, register_t *retval)
984 {
985 	/* {
986 		syscallarg(int) s;
987 		syscallarg(int) level;
988 		syscallarg(int) optname;
989 		syscallarg(void *) optval;
990 		syscallarg(int) optlen;
991 	} */
992 	struct sys_setsockopt_args bsa;
993 	int name;
994 
995 	SCARG(&bsa, s) = SCARG(uap, s);
996 	SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
997 	SCARG(&bsa, val) = SCARG(uap, optval);
998 	SCARG(&bsa, valsize) = SCARG(uap, optlen);
999 
1000 	/*
1001 	 * Linux supports only SOL_SOCKET for AF_LOCAL domain sockets
1002 	 * and returns EOPNOTSUPP for other levels
1003 	 */
1004 	if (SCARG(&bsa, level) != SOL_SOCKET) {
1005 		struct socket *so;
1006 		int error, family;
1007 
1008 		/* fd_getsock() will use the descriptor for us */
1009 	    	if ((error = fd_getsock(SCARG(&bsa, s), &so)) != 0)
1010 		    	return error;
1011 		family = so->so_proto->pr_domain->dom_family;
1012 		fd_putfile(SCARG(&bsa, s));
1013 
1014 		if (family == AF_LOCAL)
1015 			return EOPNOTSUPP;
1016 	}
1017 
1018 	switch (SCARG(&bsa, level)) {
1019 	case SOL_SOCKET:
1020 		name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
1021 		break;
1022 	case IPPROTO_IP:
1023 		name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
1024 		break;
1025 #ifdef INET6
1026 	case IPPROTO_IPV6:
1027 		name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname));
1028 		break;
1029 #endif
1030 	case IPPROTO_TCP:
1031 		name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
1032 		break;
1033 	case IPPROTO_UDP:
1034 		name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
1035 		break;
1036 	default:
1037 		return EINVAL;
1038 	}
1039 
1040 	if (name == -1)
1041 		return EINVAL;
1042 	SCARG(&bsa, name) = name;
1043 
1044 	return sys_setsockopt(l, &bsa, retval);
1045 }
1046 
1047 /*
1048  * getsockopt(2) is very much the same as setsockopt(2) (see above)
1049  */
1050 int
1051 linux_sys_getsockopt(struct lwp *l, const struct linux_sys_getsockopt_args *uap, register_t *retval)
1052 {
1053 	/* {
1054 		syscallarg(int) s;
1055 		syscallarg(int) level;
1056 		syscallarg(int) optname;
1057 		syscallarg(void *) optval;
1058 		syscallarg(int *) optlen;
1059 	} */
1060 	struct sys_getsockopt_args bga;
1061 	int name;
1062 
1063 	SCARG(&bga, s) = SCARG(uap, s);
1064 	SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
1065 	SCARG(&bga, val) = SCARG(uap, optval);
1066 	SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen);
1067 
1068 	switch (SCARG(&bga, level)) {
1069 	case SOL_SOCKET:
1070 		name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
1071 		break;
1072 	case IPPROTO_IP:
1073 		name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
1074 		break;
1075 #ifdef INET6
1076 	case IPPROTO_IPV6:
1077 		name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname));
1078 		break;
1079 #endif
1080 	case IPPROTO_TCP:
1081 		name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
1082 		break;
1083 	case IPPROTO_UDP:
1084 		name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
1085 		break;
1086 	default:
1087 		return EINVAL;
1088 	}
1089 
1090 	if (name == -1)
1091 		return EINVAL;
1092 	SCARG(&bga, name) = name;
1093 
1094 	return sys_getsockopt(l, &bga, retval);
1095 }
1096 
1097 int
1098 linux_getifname(struct lwp *l, register_t *retval, void *data)
1099 {
1100 	struct ifnet *ifp;
1101 	struct linux_ifreq ifr;
1102 	int error;
1103 
1104 	error = copyin(data, &ifr, sizeof(ifr));
1105 	if (error)
1106 		return error;
1107 
1108 	ifp = if_byindex(ifr.ifr_ifru.ifru_ifindex);
1109 	if (ifp == NULL)
1110 		return ENODEV;
1111 
1112 	strncpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
1113 
1114 	return copyout(&ifr, data, sizeof(ifr));
1115 }
1116 
1117 int
1118 linux_getifconf(struct lwp *l, register_t *retval, void *data)
1119 {
1120 	struct linux_ifreq ifr, *ifrp;
1121 	struct linux_ifconf ifc;
1122 	struct ifnet *ifp;
1123 	struct ifaddr *ifa;
1124 	struct sockaddr *sa;
1125 	struct osockaddr *osa;
1126 	int space, error = 0;
1127 	const int sz = (int)sizeof(ifr);
1128 
1129 	error = copyin(data, &ifc, sizeof(ifc));
1130 	if (error)
1131 		return error;
1132 
1133 	ifrp = ifc.ifc_req;
1134 	if (ifrp == NULL)
1135 		space = 0;
1136 	else
1137 		space = ifc.ifc_len;
1138 
1139 	IFNET_FOREACH(ifp) {
1140 		(void)strncpy(ifr.ifr_name, ifp->if_xname,
1141 		    sizeof(ifr.ifr_name));
1142 		if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
1143 			return ENAMETOOLONG;
1144 		if (IFADDR_EMPTY(ifp))
1145 			continue;
1146 		IFADDR_FOREACH(ifa, ifp) {
1147 			sa = ifa->ifa_addr;
1148 			if (sa->sa_family != AF_INET ||
1149 			    sa->sa_len > sizeof(*osa))
1150 				continue;
1151 			memcpy(&ifr.ifr_addr, sa, sa->sa_len);
1152 			osa = (struct osockaddr *)&ifr.ifr_addr;
1153 			osa->sa_family = sa->sa_family;
1154 			if (space >= sz) {
1155 				error = copyout(&ifr, ifrp, sz);
1156 				if (error != 0)
1157 					return error;
1158 				ifrp++;
1159 			}
1160 			space -= sz;
1161 		}
1162 	}
1163 
1164 	if (ifrp != NULL)
1165 		ifc.ifc_len -= space;
1166 	else
1167 		ifc.ifc_len = -space;
1168 
1169 	return copyout(&ifc, data, sizeof(ifc));
1170 }
1171 
1172 int
1173 linux_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
1174     void *data)
1175 {
1176 	/* Not the full structure, just enough to map what we do here */
1177 	struct linux_ifreq lreq;
1178 	file_t *fp;
1179 	struct ifaddr *ifa;
1180 	struct ifnet *ifp;
1181 	struct sockaddr_dl *sadl;
1182 	int error, found;
1183 	int index, ifnum;
1184 
1185 	/*
1186 	 * We can't emulate this ioctl by calling sys_ioctl() to run
1187 	 * SIOCGIFCONF, because the user buffer is not of the right
1188 	 * type to take those results.  We can't use kernel buffers to
1189 	 * receive the results, as the implementation of sys_ioctl()
1190 	 * and ifconf() [which implements SIOCGIFCONF] use
1191 	 * copyin()/copyout() which will fail on kernel addresses.
1192 	 *
1193 	 * So, we must duplicate code from sys_ioctl() and ifconf().  Ugh.
1194 	 */
1195 
1196 	if ((fp = fd_getfile(fd)) == NULL)
1197 		return (EBADF);
1198 
1199 	KERNEL_LOCK(1, NULL);
1200 
1201 	if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
1202 		error = EBADF;
1203 		goto out;
1204 	}
1205 
1206 	error = copyin(data, &lreq, sizeof(lreq));
1207 	if (error)
1208 		goto out;
1209 	lreq.ifr_name[LINUX_IFNAMSIZ-1] = '\0';		/* just in case */
1210 
1211 	/*
1212 	 * Try real interface name first, then fake "ethX"
1213 	 */
1214 	found = 0;
1215 	IFNET_FOREACH(ifp) {
1216 		if (found)
1217 			break;
1218 		if (strcmp(lreq.ifr_name, ifp->if_xname))
1219 			/* not this interface */
1220 			continue;
1221 		found=1;
1222 		if (IFADDR_EMPTY(ifp)) {
1223 			error = ENODEV;
1224 			goto out;
1225 		}
1226 		IFADDR_FOREACH(ifa, ifp) {
1227 			sadl = satosdl(ifa->ifa_addr);
1228 			/* only return ethernet addresses */
1229 			/* XXX what about FDDI, etc. ? */
1230 			if (sadl->sdl_family != AF_LINK ||
1231 			    sadl->sdl_type != IFT_ETHER)
1232 				continue;
1233 			memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl),
1234 			       MIN(sadl->sdl_alen,
1235 				   sizeof(lreq.ifr_hwaddr.sa_data)));
1236 			lreq.ifr_hwaddr.sa_family =
1237 				sadl->sdl_family;
1238 			error = copyout(&lreq, data, sizeof(lreq));
1239 			goto out;
1240 		}
1241 	}
1242 
1243 	if (strncmp(lreq.ifr_name, "eth", 3) == 0) {
1244 		for (ifnum = 0, index = 3;
1245 		     index < LINUX_IFNAMSIZ && lreq.ifr_name[index] != '\0';
1246 		     index++) {
1247 			ifnum *= 10;
1248 			ifnum += lreq.ifr_name[index] - '0';
1249 		}
1250 
1251 		error = EINVAL;			/* in case we don't find one */
1252 		found = 0;
1253 		IFNET_FOREACH(ifp) {
1254 			if (found)
1255 				break;
1256 			memcpy(lreq.ifr_name, ifp->if_xname,
1257 			       MIN(LINUX_IFNAMSIZ, IFNAMSIZ));
1258 			IFADDR_FOREACH(ifa, ifp) {
1259 				sadl = satosdl(ifa->ifa_addr);
1260 				/* only return ethernet addresses */
1261 				/* XXX what about FDDI, etc. ? */
1262 				if (sadl->sdl_family != AF_LINK ||
1263 				    sadl->sdl_type != IFT_ETHER)
1264 					continue;
1265 				if (ifnum--)
1266 					/* not the reqested iface */
1267 					continue;
1268 				memcpy(&lreq.ifr_hwaddr.sa_data,
1269 				       CLLADDR(sadl),
1270 				       MIN(sadl->sdl_alen,
1271 					   sizeof(lreq.ifr_hwaddr.sa_data)));
1272 				lreq.ifr_hwaddr.sa_family =
1273 					sadl->sdl_family;
1274 				error = copyout(&lreq, data, sizeof(lreq));
1275 				found = 1;
1276 				break;
1277 			}
1278 		}
1279 	} else {
1280 		/* unknown interface, not even an "eth*" name */
1281 		error = ENODEV;
1282 	}
1283 
1284 out:
1285 	KERNEL_UNLOCK_ONE(NULL);
1286 	fd_putfile(fd);
1287 	return error;
1288 }
1289 
1290 int
1291 linux_ioctl_socket(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
1292 {
1293 	/* {
1294 		syscallarg(int) fd;
1295 		syscallarg(u_long) com;
1296 		syscallarg(void *) data;
1297 	} */
1298 	u_long com;
1299 	int error = 0, isdev = 0, dosys = 1;
1300 	struct sys_ioctl_args ia;
1301 	file_t *fp;
1302 	struct vnode *vp;
1303 	int (*ioctlf)(file_t *, u_long, void *);
1304 	struct ioctl_pt pt;
1305 
1306 	if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
1307 		return (EBADF);
1308 
1309 	if (fp->f_type == DTYPE_VNODE) {
1310 		vp = (struct vnode *)fp->f_data;
1311 		isdev = vp->v_type == VCHR;
1312 	}
1313 
1314 	/*
1315 	 * Don't try to interpret socket ioctl calls that are done
1316 	 * on a device filedescriptor, just pass them through, to
1317 	 * emulate Linux behaviour. Use PTIOCLINUX so that the
1318 	 * device will only handle these if it's prepared to do
1319 	 * so, to avoid unexpected things from happening.
1320 	 */
1321 	if (isdev) {
1322 		dosys = 0;
1323 		ioctlf = fp->f_ops->fo_ioctl;
1324 		pt.com = SCARG(uap, com);
1325 		pt.data = SCARG(uap, data);
1326 		error = ioctlf(fp, PTIOCLINUX, &pt);
1327 		/*
1328 		 * XXX hack: if the function returns EJUSTRETURN,
1329 		 * it has stuffed a sysctl return value in pt.data.
1330 		 */
1331 		if (error == EJUSTRETURN) {
1332 			retval[0] = (register_t)pt.data;
1333 			error = 0;
1334 		}
1335 		goto out;
1336 	}
1337 
1338 	com = SCARG(uap, com);
1339 	retval[0] = 0;
1340 
1341 	switch (com) {
1342 	case LINUX_SIOCGIFNAME:
1343 		error = linux_getifname(l, retval, SCARG(uap, data));
1344 		dosys = 0;
1345 		break;
1346 	case LINUX_SIOCGIFCONF:
1347 		error = linux_getifconf(l, retval, SCARG(uap, data));
1348 		dosys = 0;
1349 		break;
1350 	case LINUX_SIOCGIFFLAGS:
1351 		SCARG(&ia, com) = OSIOCGIFFLAGS;
1352 		break;
1353 	case LINUX_SIOCSIFFLAGS:
1354 		SCARG(&ia, com) = OSIOCSIFFLAGS;
1355 		break;
1356 	case LINUX_SIOCGIFADDR:
1357 		SCARG(&ia, com) = OOSIOCGIFADDR;
1358 		break;
1359 	case LINUX_SIOCGIFDSTADDR:
1360 		SCARG(&ia, com) = OOSIOCGIFDSTADDR;
1361 		break;
1362 	case LINUX_SIOCGIFBRDADDR:
1363 		SCARG(&ia, com) = OOSIOCGIFBRDADDR;
1364 		break;
1365 	case LINUX_SIOCGIFNETMASK:
1366 		SCARG(&ia, com) = OOSIOCGIFNETMASK;
1367 		break;
1368 	case LINUX_SIOCGIFMTU:
1369 		SCARG(&ia, com) = OSIOCGIFMTU;
1370 		break;
1371 	case LINUX_SIOCADDMULTI:
1372 		SCARG(&ia, com) = OSIOCADDMULTI;
1373 		break;
1374 	case LINUX_SIOCDELMULTI:
1375 		SCARG(&ia, com) = OSIOCDELMULTI;
1376 		break;
1377 	case LINUX_SIOCGIFHWADDR:
1378 		error = linux_getifhwaddr(l, retval, SCARG(uap, fd),
1379 		    SCARG(uap, data));
1380 		dosys = 0;
1381 		break;
1382 	default:
1383 		error = EINVAL;
1384 	}
1385 
1386  out:
1387  	fd_putfile(SCARG(uap, fd));
1388 
1389 	if (error ==0 && dosys) {
1390 		SCARG(&ia, fd) = SCARG(uap, fd);
1391 		SCARG(&ia, data) = SCARG(uap, data);
1392 		error = sys_ioctl(curlwp, &ia, retval);
1393 	}
1394 
1395 	return error;
1396 }
1397 
1398 int
1399 linux_sys_connect(struct lwp *l, const struct linux_sys_connect_args *uap, register_t *retval)
1400 {
1401 	/* {
1402 		syscallarg(int) s;
1403 		syscallarg(const struct sockaddr *) name;
1404 		syscallarg(int) namelen;
1405 	} */
1406 	int		error;
1407 	struct mbuf *nam;
1408 
1409 	error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, name),
1410 	    SCARG(uap, namelen));
1411 	if (error)
1412 		return (error);
1413 
1414 	error = do_sys_connect(l, SCARG(uap, s), nam);
1415 
1416 	if (error == EISCONN) {
1417 		struct socket *so;
1418 		int state, prflags;
1419 
1420 		/* fd_getsock() will use the descriptor for us */
1421 	    	if (fd_getsock(SCARG(uap, s), &so) != 0)
1422 		    	return EISCONN;
1423 
1424 		solock(so);
1425 		state = so->so_state;
1426 		prflags = so->so_proto->pr_flags;
1427 		sounlock(so);
1428 		fd_putfile(SCARG(uap, s));
1429 		/*
1430 		 * We should only let this call succeed once per
1431 		 * non-blocking connect; however we don't have
1432 		 * a convenient place to keep that state..
1433 		 */
1434 		if ((state & (SS_ISCONNECTED|SS_NBIO)) ==
1435 		    (SS_ISCONNECTED|SS_NBIO) &&
1436 		    (prflags & PR_CONNREQUIRED))
1437 			return 0;
1438 	}
1439 
1440 	return (error);
1441 }
1442 
1443 int
1444 linux_sys_bind(struct lwp *l, const struct linux_sys_bind_args *uap, register_t *retval)
1445 {
1446 	/* {
1447 		syscallarg(int) s;
1448 		syscallarg(const struct osockaddr *) name;
1449 		syscallarg(int) namelen;
1450 	} */
1451 	int		error;
1452 	struct mbuf     *nam;
1453 
1454 	error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, name),
1455 	    SCARG(uap, namelen));
1456 	if (error)
1457 		return (error);
1458 
1459 	return do_sys_bind(l, SCARG(uap, s), nam);
1460 }
1461 
1462 int
1463 linux_sys_getsockname(struct lwp *l, const struct linux_sys_getsockname_args *uap, register_t *retval)
1464 {
1465 	/* {
1466 		syscallarg(int) fdes;
1467 		syscallarg(void *) asa;
1468 		syscallarg(int *) alen;
1469 	} */
1470 	int error;
1471 
1472 	if ((error = sys_getsockname(l, (const void *)uap, retval)) != 0)
1473 		return (error);
1474 
1475 	if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
1476 		return (error);
1477 
1478 	return (0);
1479 }
1480 
1481 int
1482 linux_sys_getpeername(struct lwp *l, const struct linux_sys_getpeername_args *uap, register_t *retval)
1483 {
1484 	/* {
1485 		syscallarg(int) fdes;
1486 		syscallarg(void *) asa;
1487 		syscallarg(int *) alen;
1488 	} */
1489 	int error;
1490 
1491 	if ((error = sys_getpeername(l, (const void *)uap, retval)) != 0)
1492 		return (error);
1493 
1494 	if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
1495 		return (error);
1496 
1497 	return (0);
1498 }
1499 
1500 /*
1501  * Copy the osockaddr structure pointed to by osa to mbuf, adjust
1502  * family and convert to sockaddr.
1503  */
1504 static int
1505 linux_get_sa(struct lwp *l, int s, struct mbuf **mp,
1506     const struct osockaddr *osa, unsigned int salen)
1507 {
1508 	int error, bdom;
1509 	struct sockaddr *sa;
1510 	struct osockaddr *kosa;
1511 	struct mbuf *m;
1512 
1513 	if (salen == 1 || salen > UCHAR_MAX) {
1514 		DPRINTF(("bad osa=%p salen=%d\n", osa, salen));
1515 		return EINVAL;
1516 	}
1517 
1518 	/* We'll need the address in an mbuf later, so copy into one here */
1519 	m = m_get(M_WAIT, MT_SONAME);
1520 	if (salen > MLEN)
1521 		MEXTMALLOC(m, salen, M_WAITOK);
1522 
1523 	m->m_len = salen;
1524 
1525 	if (salen == 0) {
1526 		*mp = m;
1527 		return 0;
1528 	}
1529 
1530 	kosa = mtod(m, void *);
1531 	if ((error = copyin(osa, kosa, salen))) {
1532 		DPRINTF(("error %d copying osa %p len %d\n",
1533 				error, osa, salen));
1534 		goto bad;
1535 	}
1536 
1537 	ktrkuser("linux/sockaddr", kosa, salen);
1538 
1539 	bdom = linux_to_bsd_domain(kosa->sa_family);
1540 	if (bdom == -1) {
1541 		DPRINTF(("bad linux family=%d\n", kosa->sa_family));
1542 		error = EINVAL;
1543 		goto bad;
1544 	}
1545 
1546 	/*
1547 	 * If the family is unspecified, use address family of the socket.
1548 	 * This avoid triggering strict family checks in netinet/in_pcb.c et.al.
1549 	 */
1550 	if (bdom == AF_UNSPEC) {
1551 		struct socket *so;
1552 
1553 		/* fd_getsock() will use the descriptor for us */
1554 		if ((error = fd_getsock(s, &so)) != 0)
1555 			goto bad;
1556 
1557 		bdom = so->so_proto->pr_domain->dom_family;
1558 		fd_putfile(s);
1559 
1560 		DPRINTF(("AF_UNSPEC family adjusted to %d\n", bdom));
1561 	}
1562 
1563 	/*
1564 	 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
1565 	 * which lacks the scope id compared with RFC2553 one. If we detect
1566 	 * the situation, reject the address and write a message to system log.
1567 	 *
1568 	 * Still accept addresses for which the scope id is not used.
1569 	 */
1570 	if (bdom == AF_INET6 && salen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
1571 		struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)kosa;
1572 		if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) &&
1573 		    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
1574 		     IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) ||
1575 		     IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) ||
1576 		     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
1577 		     IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
1578 			struct proc *p = l->l_proc;
1579 			int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1;
1580 
1581 			log(LOG_DEBUG,
1582 			    "pid %d (%s), uid %d: obsolete pre-RFC2553 "
1583 			    "sockaddr_in6 rejected",
1584 			    p->p_pid, p->p_comm, uid);
1585 			error = EINVAL;
1586 			goto bad;
1587 		}
1588 		salen = sizeof (struct sockaddr_in6);
1589 		sin6->sin6_scope_id = 0;
1590 	}
1591 
1592 	if (bdom == AF_INET)
1593 		salen = sizeof(struct sockaddr_in);
1594 
1595 	sa = (struct sockaddr *) kosa;
1596 	sa->sa_family = bdom;
1597 	sa->sa_len = salen;
1598 	m->m_len = salen;
1599 	ktrkuser("mbsoname", kosa, salen);
1600 
1601 #ifdef DEBUG_LINUX
1602 	DPRINTF(("family %d, len = %d [ ", sa->sa_family, sa->sa_len));
1603 	for (bdom = 0; bdom < sizeof(sa->sa_data); bdom++)
1604 	    DPRINTF(("%02x ", (unsigned char) sa->sa_data[bdom]));
1605 	DPRINTF(("\n"));
1606 #endif
1607 
1608 	*mp = m;
1609 	return 0;
1610 
1611     bad:
1612 	m_free(m);
1613 	return error;
1614 }
1615 
1616 static int
1617 linux_sa_put(struct osockaddr *osa)
1618 {
1619 	struct sockaddr sa;
1620 	struct osockaddr *kosa;
1621 	int error, bdom, len;
1622 
1623 	/*
1624 	 * Only read/write the sockaddr family and length part, the rest is
1625 	 * not changed.
1626 	 */
1627 	len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
1628 
1629 	error = copyin(osa, &sa, len);
1630 	if (error)
1631 		return (error);
1632 
1633 	bdom = bsd_to_linux_domain(sa.sa_family);
1634 	if (bdom == -1)
1635 		return (EINVAL);
1636 
1637 	/* Note: we convert from sockaddr to osockaddr here, too */
1638 	kosa = (struct osockaddr *) &sa;
1639 	kosa->sa_family = bdom;
1640 	error = copyout(kosa, osa, len);
1641 	if (error)
1642 		return (error);
1643 
1644 	return (0);
1645 }
1646 
1647 #ifndef __amd64__
1648 int
1649 linux_sys_recv(struct lwp *l, const struct linux_sys_recv_args *uap, register_t *retval)
1650 {
1651 	/* {
1652 		syscallarg(int) s;
1653 		syscallarg(void *) buf;
1654 		syscallarg(int) len;
1655 		syscallarg(int) flags;
1656 	} */
1657 	struct sys_recvfrom_args bra;
1658 
1659 
1660 	SCARG(&bra, s) = SCARG(uap, s);
1661 	SCARG(&bra, buf) = SCARG(uap, buf);
1662 	SCARG(&bra, len) = (size_t) SCARG(uap, len);
1663 	SCARG(&bra, flags) = SCARG(uap, flags);
1664 	SCARG(&bra, from) = NULL;
1665 	SCARG(&bra, fromlenaddr) = NULL;
1666 
1667 	return (sys_recvfrom(l, &bra, retval));
1668 }
1669 
1670 int
1671 linux_sys_send(struct lwp *l, const struct linux_sys_send_args *uap, register_t *retval)
1672 {
1673 	/* {
1674 		syscallarg(int) s;
1675 		syscallarg(void *) buf;
1676 		syscallarg(int) len;
1677 		syscallarg(int) flags;
1678 	} */
1679 	struct sys_sendto_args bsa;
1680 
1681 	SCARG(&bsa, s)		= SCARG(uap, s);
1682 	SCARG(&bsa, buf)	= SCARG(uap, buf);
1683 	SCARG(&bsa, len)	= SCARG(uap, len);
1684 	SCARG(&bsa, flags)	= SCARG(uap, flags);
1685 	SCARG(&bsa, to)		= NULL;
1686 	SCARG(&bsa, tolen)	= 0;
1687 
1688 	return (sys_sendto(l, &bsa, retval));
1689 }
1690 #endif
1691 
1692 int
1693 linux_sys_accept(struct lwp *l, const struct linux_sys_accept_args *uap, register_t *retval)
1694 {
1695 	/* {
1696 		syscallarg(int) s;
1697 		syscallarg(struct osockaddr *) name;
1698 		syscallarg(int *) anamelen;
1699 	} */
1700 	int error;
1701 	struct sys_accept_args baa;
1702 
1703 	SCARG(&baa, s)		= SCARG(uap, s);
1704 	SCARG(&baa, name)	= (struct sockaddr *) SCARG(uap, name);
1705 	SCARG(&baa, anamelen)	= (unsigned int *) SCARG(uap, anamelen);
1706 
1707 	if ((error = sys_accept(l, &baa, retval)))
1708 		return (error);
1709 
1710 	if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
1711 		return (error);
1712 
1713 	return (0);
1714 }
1715