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