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