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