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