xref: /freebsd/sys/compat/linux/linux_socket.c (revision bbf392d5eff073919385ecab501fdf9277d2ef68)
1 /*-
2  * Copyright (c) 1995 Søren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 /* XXX we use functions that might not exist. */
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
35 
36 #include <sys/param.h>
37 #include <sys/proc.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/capsicum.h>
41 #include <sys/fcntl.h>
42 #include <sys/file.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/mbuf.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/syscallsubr.h>
51 #include <sys/uio.h>
52 #include <sys/syslog.h>
53 #include <sys/un.h>
54 
55 #include <net/if.h>
56 #include <net/vnet.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
61 #ifdef INET6
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
64 #endif
65 
66 #ifdef COMPAT_LINUX32
67 #include <machine/../linux32/linux.h>
68 #include <machine/../linux32/linux32_proto.h>
69 #else
70 #include <machine/../linux/linux.h>
71 #include <machine/../linux/linux_proto.h>
72 #endif
73 #include <compat/linux/linux_file.h>
74 #include <compat/linux/linux_socket.h>
75 #include <compat/linux/linux_timer.h>
76 #include <compat/linux/linux_util.h>
77 
78 static int linux_to_bsd_domain(int);
79 static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *,
80 					l_uint);
81 static int linux_recvmsg_common(struct thread *, l_int, struct l_msghdr *,
82 					l_uint, struct msghdr *);
83 static int linux_set_socket_flags(int, int *);
84 
85 /*
86  * Reads a linux sockaddr and does any necessary translation.
87  * Linux sockaddrs don't have a length field, only a family.
88  * Copy the osockaddr structure pointed to by osa to kernel, adjust
89  * family and convert to sockaddr.
90  */
91 static int
92 linux_getsockaddr(struct sockaddr **sap, const struct osockaddr *osa, int salen)
93 {
94 	struct sockaddr *sa;
95 	struct osockaddr *kosa;
96 #ifdef INET6
97 	struct sockaddr_in6 *sin6;
98 	int oldv6size;
99 #endif
100 	char *name;
101 	int bdom, error, hdrlen, namelen;
102 
103 	if (salen < 2 || salen > UCHAR_MAX || !osa)
104 		return (EINVAL);
105 
106 #ifdef INET6
107 	oldv6size = 0;
108 	/*
109 	 * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
110 	 * if it's a v4-mapped address, so reserve the proper space
111 	 * for it.
112 	 */
113 	if (salen == sizeof(struct sockaddr_in6) - sizeof(uint32_t)) {
114 		salen += sizeof(uint32_t);
115 		oldv6size = 1;
116 	}
117 #endif
118 
119 	kosa = malloc(salen, M_SONAME, M_WAITOK);
120 
121 	if ((error = copyin(osa, kosa, salen)))
122 		goto out;
123 
124 	bdom = linux_to_bsd_domain(kosa->sa_family);
125 	if (bdom == -1) {
126 		error = EAFNOSUPPORT;
127 		goto out;
128 	}
129 
130 #ifdef INET6
131 	/*
132 	 * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
133 	 * which lacks the scope id compared with RFC2553 one. If we detect
134 	 * the situation, reject the address and write a message to system log.
135 	 *
136 	 * Still accept addresses for which the scope id is not used.
137 	 */
138 	if (oldv6size) {
139 		if (bdom == AF_INET6) {
140 			sin6 = (struct sockaddr_in6 *)kosa;
141 			if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
142 			    (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
143 			     !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
144 			     !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
145 			     !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
146 			     !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
147 				sin6->sin6_scope_id = 0;
148 			} else {
149 				log(LOG_DEBUG,
150 				    "obsolete pre-RFC2553 sockaddr_in6 rejected\n");
151 				error = EINVAL;
152 				goto out;
153 			}
154 		} else
155 			salen -= sizeof(uint32_t);
156 	}
157 #endif
158 	if (bdom == AF_INET) {
159 		if (salen < sizeof(struct sockaddr_in)) {
160 			error = EINVAL;
161 			goto out;
162 		}
163 		salen = sizeof(struct sockaddr_in);
164 	}
165 
166 	if (bdom == AF_LOCAL && salen > sizeof(struct sockaddr_un)) {
167 		hdrlen = offsetof(struct sockaddr_un, sun_path);
168 		name = ((struct sockaddr_un *)kosa)->sun_path;
169 		if (*name == '\0') {
170 			/*
171 		 	 * Linux abstract namespace starts with a NULL byte.
172 			 * XXX We do not support abstract namespace yet.
173 			 */
174 			namelen = strnlen(name + 1, salen - hdrlen - 1) + 1;
175 		} else
176 			namelen = strnlen(name, salen - hdrlen);
177 		salen = hdrlen + namelen;
178 		if (salen > sizeof(struct sockaddr_un)) {
179 			error = ENAMETOOLONG;
180 			goto out;
181 		}
182 	}
183 
184 	sa = (struct sockaddr *)kosa;
185 	sa->sa_family = bdom;
186 	sa->sa_len = salen;
187 
188 	*sap = sa;
189 	return (0);
190 
191 out:
192 	free(kosa, M_SONAME);
193 	return (error);
194 }
195 
196 static int
197 linux_to_bsd_domain(int domain)
198 {
199 
200 	switch (domain) {
201 	case LINUX_AF_UNSPEC:
202 		return (AF_UNSPEC);
203 	case LINUX_AF_UNIX:
204 		return (AF_LOCAL);
205 	case LINUX_AF_INET:
206 		return (AF_INET);
207 	case LINUX_AF_INET6:
208 		return (AF_INET6);
209 	case LINUX_AF_AX25:
210 		return (AF_CCITT);
211 	case LINUX_AF_IPX:
212 		return (AF_IPX);
213 	case LINUX_AF_APPLETALK:
214 		return (AF_APPLETALK);
215 	}
216 	return (-1);
217 }
218 
219 static int
220 bsd_to_linux_domain(int domain)
221 {
222 
223 	switch (domain) {
224 	case AF_UNSPEC:
225 		return (LINUX_AF_UNSPEC);
226 	case AF_LOCAL:
227 		return (LINUX_AF_UNIX);
228 	case AF_INET:
229 		return (LINUX_AF_INET);
230 	case AF_INET6:
231 		return (LINUX_AF_INET6);
232 	case AF_CCITT:
233 		return (LINUX_AF_AX25);
234 	case AF_IPX:
235 		return (LINUX_AF_IPX);
236 	case AF_APPLETALK:
237 		return (LINUX_AF_APPLETALK);
238 	}
239 	return (-1);
240 }
241 
242 static int
243 linux_to_bsd_sockopt_level(int level)
244 {
245 
246 	switch (level) {
247 	case LINUX_SOL_SOCKET:
248 		return (SOL_SOCKET);
249 	}
250 	return (level);
251 }
252 
253 static int
254 bsd_to_linux_sockopt_level(int level)
255 {
256 
257 	switch (level) {
258 	case SOL_SOCKET:
259 		return (LINUX_SOL_SOCKET);
260 	}
261 	return (level);
262 }
263 
264 static int
265 linux_to_bsd_ip_sockopt(int opt)
266 {
267 
268 	switch (opt) {
269 	case LINUX_IP_TOS:
270 		return (IP_TOS);
271 	case LINUX_IP_TTL:
272 		return (IP_TTL);
273 	case LINUX_IP_OPTIONS:
274 		return (IP_OPTIONS);
275 	case LINUX_IP_MULTICAST_IF:
276 		return (IP_MULTICAST_IF);
277 	case LINUX_IP_MULTICAST_TTL:
278 		return (IP_MULTICAST_TTL);
279 	case LINUX_IP_MULTICAST_LOOP:
280 		return (IP_MULTICAST_LOOP);
281 	case LINUX_IP_ADD_MEMBERSHIP:
282 		return (IP_ADD_MEMBERSHIP);
283 	case LINUX_IP_DROP_MEMBERSHIP:
284 		return (IP_DROP_MEMBERSHIP);
285 	case LINUX_IP_HDRINCL:
286 		return (IP_HDRINCL);
287 	}
288 	return (-1);
289 }
290 
291 static int
292 linux_to_bsd_so_sockopt(int opt)
293 {
294 
295 	switch (opt) {
296 	case LINUX_SO_DEBUG:
297 		return (SO_DEBUG);
298 	case LINUX_SO_REUSEADDR:
299 		return (SO_REUSEADDR);
300 	case LINUX_SO_TYPE:
301 		return (SO_TYPE);
302 	case LINUX_SO_ERROR:
303 		return (SO_ERROR);
304 	case LINUX_SO_DONTROUTE:
305 		return (SO_DONTROUTE);
306 	case LINUX_SO_BROADCAST:
307 		return (SO_BROADCAST);
308 	case LINUX_SO_SNDBUF:
309 		return (SO_SNDBUF);
310 	case LINUX_SO_RCVBUF:
311 		return (SO_RCVBUF);
312 	case LINUX_SO_KEEPALIVE:
313 		return (SO_KEEPALIVE);
314 	case LINUX_SO_OOBINLINE:
315 		return (SO_OOBINLINE);
316 	case LINUX_SO_LINGER:
317 		return (SO_LINGER);
318 	case LINUX_SO_PEERCRED:
319 		return (LOCAL_PEERCRED);
320 	case LINUX_SO_RCVLOWAT:
321 		return (SO_RCVLOWAT);
322 	case LINUX_SO_SNDLOWAT:
323 		return (SO_SNDLOWAT);
324 	case LINUX_SO_RCVTIMEO:
325 		return (SO_RCVTIMEO);
326 	case LINUX_SO_SNDTIMEO:
327 		return (SO_SNDTIMEO);
328 	case LINUX_SO_TIMESTAMP:
329 		return (SO_TIMESTAMP);
330 	case LINUX_SO_ACCEPTCONN:
331 		return (SO_ACCEPTCONN);
332 	}
333 	return (-1);
334 }
335 
336 static int
337 linux_to_bsd_tcp_sockopt(int opt)
338 {
339 
340 	switch (opt) {
341 	case LINUX_TCP_NODELAY:
342 		return (TCP_NODELAY);
343 	case LINUX_TCP_MAXSEG:
344 		return (TCP_MAXSEG);
345 	case LINUX_TCP_KEEPIDLE:
346 		return (TCP_KEEPIDLE);
347 	case LINUX_TCP_KEEPINTVL:
348 		return (TCP_KEEPINTVL);
349 	case LINUX_TCP_KEEPCNT:
350 		return (TCP_KEEPCNT);
351 	case LINUX_TCP_MD5SIG:
352 		return (TCP_MD5SIG);
353 	}
354 	return (-1);
355 }
356 
357 static int
358 linux_to_bsd_msg_flags(int flags)
359 {
360 	int ret_flags = 0;
361 
362 	if (flags & LINUX_MSG_OOB)
363 		ret_flags |= MSG_OOB;
364 	if (flags & LINUX_MSG_PEEK)
365 		ret_flags |= MSG_PEEK;
366 	if (flags & LINUX_MSG_DONTROUTE)
367 		ret_flags |= MSG_DONTROUTE;
368 	if (flags & LINUX_MSG_CTRUNC)
369 		ret_flags |= MSG_CTRUNC;
370 	if (flags & LINUX_MSG_TRUNC)
371 		ret_flags |= MSG_TRUNC;
372 	if (flags & LINUX_MSG_DONTWAIT)
373 		ret_flags |= MSG_DONTWAIT;
374 	if (flags & LINUX_MSG_EOR)
375 		ret_flags |= MSG_EOR;
376 	if (flags & LINUX_MSG_WAITALL)
377 		ret_flags |= MSG_WAITALL;
378 	if (flags & LINUX_MSG_NOSIGNAL)
379 		ret_flags |= MSG_NOSIGNAL;
380 #if 0 /* not handled */
381 	if (flags & LINUX_MSG_PROXY)
382 		;
383 	if (flags & LINUX_MSG_FIN)
384 		;
385 	if (flags & LINUX_MSG_SYN)
386 		;
387 	if (flags & LINUX_MSG_CONFIRM)
388 		;
389 	if (flags & LINUX_MSG_RST)
390 		;
391 	if (flags & LINUX_MSG_ERRQUEUE)
392 		;
393 #endif
394 	return ret_flags;
395 }
396 
397 /*
398 * If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the
399 * native syscall will fault.  Thus, we don't really need to check the
400 * return values for these functions.
401 */
402 
403 static int
404 bsd_to_linux_sockaddr(struct sockaddr *arg)
405 {
406 	struct sockaddr sa;
407 	size_t sa_len = sizeof(struct sockaddr);
408 	int error;
409 
410 	if ((error = copyin(arg, &sa, sa_len)))
411 		return (error);
412 
413 	*(u_short *)&sa = sa.sa_family;
414 
415 	error = copyout(&sa, arg, sa_len);
416 
417 	return (error);
418 }
419 
420 static int
421 linux_to_bsd_sockaddr(struct sockaddr *arg, int len)
422 {
423 	struct sockaddr sa;
424 	size_t sa_len = sizeof(struct sockaddr);
425 	int error;
426 
427 	if ((error = copyin(arg, &sa, sa_len)))
428 		return (error);
429 
430 	sa.sa_family = *(sa_family_t *)&sa;
431 	sa.sa_len = len;
432 
433 	error = copyout(&sa, arg, sa_len);
434 
435 	return (error);
436 }
437 
438 static int
439 linux_sa_put(struct osockaddr *osa)
440 {
441 	struct osockaddr sa;
442 	int error, bdom;
443 
444 	/*
445 	 * Only read/write the osockaddr family part, the rest is
446 	 * not changed.
447 	 */
448 	error = copyin(osa, &sa, sizeof(sa.sa_family));
449 	if (error)
450 		return (error);
451 
452 	bdom = bsd_to_linux_domain(sa.sa_family);
453 	if (bdom == -1)
454 		return (EINVAL);
455 
456 	sa.sa_family = bdom;
457 	error = copyout(&sa, osa, sizeof(sa.sa_family));
458 	if (error)
459 		return (error);
460 
461 	return (0);
462 }
463 
464 static int
465 linux_to_bsd_cmsg_type(int cmsg_type)
466 {
467 
468 	switch (cmsg_type) {
469 	case LINUX_SCM_RIGHTS:
470 		return (SCM_RIGHTS);
471 	case LINUX_SCM_CREDENTIALS:
472 		return (SCM_CREDS);
473 	}
474 	return (-1);
475 }
476 
477 static int
478 bsd_to_linux_cmsg_type(int cmsg_type)
479 {
480 
481 	switch (cmsg_type) {
482 	case SCM_RIGHTS:
483 		return (LINUX_SCM_RIGHTS);
484 	case SCM_CREDS:
485 		return (LINUX_SCM_CREDENTIALS);
486 	case SCM_TIMESTAMP:
487 		return (LINUX_SCM_TIMESTAMP);
488 	}
489 	return (-1);
490 }
491 
492 static int
493 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
494 {
495 	if (lhdr->msg_controllen > INT_MAX)
496 		return (ENOBUFS);
497 
498 	bhdr->msg_name		= PTRIN(lhdr->msg_name);
499 	bhdr->msg_namelen	= lhdr->msg_namelen;
500 	bhdr->msg_iov		= PTRIN(lhdr->msg_iov);
501 	bhdr->msg_iovlen	= lhdr->msg_iovlen;
502 	bhdr->msg_control	= PTRIN(lhdr->msg_control);
503 
504 	/*
505 	 * msg_controllen is skipped since BSD and LINUX control messages
506 	 * are potentially different sizes (e.g. the cred structure used
507 	 * by SCM_CREDS is different between the two operating system).
508 	 *
509 	 * The caller can set it (if necessary) after converting all the
510 	 * control messages.
511 	 */
512 
513 	bhdr->msg_flags		= linux_to_bsd_msg_flags(lhdr->msg_flags);
514 	return (0);
515 }
516 
517 static int
518 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
519 {
520 	lhdr->msg_name		= PTROUT(bhdr->msg_name);
521 	lhdr->msg_namelen	= bhdr->msg_namelen;
522 	lhdr->msg_iov		= PTROUT(bhdr->msg_iov);
523 	lhdr->msg_iovlen	= bhdr->msg_iovlen;
524 	lhdr->msg_control	= PTROUT(bhdr->msg_control);
525 
526 	/*
527 	 * msg_controllen is skipped since BSD and LINUX control messages
528 	 * are potentially different sizes (e.g. the cred structure used
529 	 * by SCM_CREDS is different between the two operating system).
530 	 *
531 	 * The caller can set it (if necessary) after converting all the
532 	 * control messages.
533 	 */
534 
535 	/* msg_flags skipped */
536 	return (0);
537 }
538 
539 static int
540 linux_set_socket_flags(int lflags, int *flags)
541 {
542 
543 	if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
544 		return (EINVAL);
545 	if (lflags & LINUX_SOCK_NONBLOCK)
546 		*flags |= SOCK_NONBLOCK;
547 	if (lflags & LINUX_SOCK_CLOEXEC)
548 		*flags |= SOCK_CLOEXEC;
549 	return (0);
550 }
551 
552 static int
553 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
554     struct mbuf *control, enum uio_seg segflg)
555 {
556 	struct sockaddr *to;
557 	int error;
558 
559 	if (mp->msg_name != NULL) {
560 		error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen);
561 		if (error)
562 			return (error);
563 		mp->msg_name = to;
564 	} else
565 		to = NULL;
566 
567 	error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
568 	    segflg);
569 
570 	if (to)
571 		free(to, M_SONAME);
572 	return (error);
573 }
574 
575 /* Return 0 if IP_HDRINCL is set for the given socket. */
576 static int
577 linux_check_hdrincl(struct thread *td, int s)
578 {
579 	int error, optval;
580 	socklen_t size_val;
581 
582 	size_val = sizeof(optval);
583 	error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
584 	    &optval, UIO_SYSSPACE, &size_val);
585 	if (error)
586 		return (error);
587 
588 	return (optval == 0);
589 }
590 
591 /*
592  * Updated sendto() when IP_HDRINCL is set:
593  * tweak endian-dependent fields in the IP packet.
594  */
595 static int
596 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
597 {
598 /*
599  * linux_ip_copysize defines how many bytes we should copy
600  * from the beginning of the IP packet before we customize it for BSD.
601  * It should include all the fields we modify (ip_len and ip_off).
602  */
603 #define linux_ip_copysize	8
604 
605 	struct ip *packet;
606 	struct msghdr msg;
607 	struct iovec aiov[1];
608 	int error;
609 
610 	/* Check that the packet isn't too big or too small. */
611 	if (linux_args->len < linux_ip_copysize ||
612 	    linux_args->len > IP_MAXPACKET)
613 		return (EINVAL);
614 
615 	packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
616 
617 	/* Make kernel copy of the packet to be sent */
618 	if ((error = copyin(PTRIN(linux_args->msg), packet,
619 	    linux_args->len)))
620 		goto goout;
621 
622 	/* Convert fields from Linux to BSD raw IP socket format */
623 	packet->ip_len = linux_args->len;
624 	packet->ip_off = ntohs(packet->ip_off);
625 
626 	/* Prepare the msghdr and iovec structures describing the new packet */
627 	msg.msg_name = PTRIN(linux_args->to);
628 	msg.msg_namelen = linux_args->tolen;
629 	msg.msg_iov = aiov;
630 	msg.msg_iovlen = 1;
631 	msg.msg_control = NULL;
632 	msg.msg_flags = 0;
633 	aiov[0].iov_base = (char *)packet;
634 	aiov[0].iov_len = linux_args->len;
635 	error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
636 	    NULL, UIO_SYSSPACE);
637 goout:
638 	free(packet, M_LINUX);
639 	return (error);
640 }
641 
642 int
643 linux_socket(struct thread *td, struct linux_socket_args *args)
644 {
645 	struct socket_args /* {
646 		int domain;
647 		int type;
648 		int protocol;
649 	} */ bsd_args;
650 	int retval_socket;
651 
652 	bsd_args.protocol = args->protocol;
653 	bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
654 	if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
655 		return (EINVAL);
656 	retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
657 		&bsd_args.type);
658 	if (retval_socket != 0)
659 		return (retval_socket);
660 	bsd_args.domain = linux_to_bsd_domain(args->domain);
661 	if (bsd_args.domain == -1)
662 		return (EAFNOSUPPORT);
663 
664 	retval_socket = sys_socket(td, &bsd_args);
665 	if (retval_socket)
666 		return (retval_socket);
667 
668 	if (bsd_args.type == SOCK_RAW
669 	    && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
670 	    && bsd_args.domain == PF_INET) {
671 		/* It's a raw IP socket: set the IP_HDRINCL option. */
672 		int hdrincl;
673 
674 		hdrincl = 1;
675 		/* We ignore any error returned by kern_setsockopt() */
676 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
677 		    &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
678 	}
679 #ifdef INET6
680 	/*
681 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
682 	 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
683 	 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
684 	 * sysctl value.
685 	 */
686 	if (bsd_args.domain == PF_INET6) {
687 		int v6only;
688 
689 		v6only = 0;
690 		/* We ignore any error returned by setsockopt() */
691 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
692 		    &v6only, UIO_SYSSPACE, sizeof(v6only));
693 	}
694 #endif
695 
696 	return (retval_socket);
697 }
698 
699 int
700 linux_bind(struct thread *td, struct linux_bind_args *args)
701 {
702 	struct sockaddr *sa;
703 	int error;
704 
705 	error = linux_getsockaddr(&sa, PTRIN(args->name),
706 	    args->namelen);
707 	if (error)
708 		return (error);
709 
710 	error = kern_bindat(td, AT_FDCWD, args->s, sa);
711 	free(sa, M_SONAME);
712 	if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
713 	   	return (EINVAL);
714 	return (error);
715 }
716 
717 int
718 linux_connect(struct thread *td, struct linux_connect_args *args)
719 {
720 	cap_rights_t rights;
721 	struct socket *so;
722 	struct sockaddr *sa;
723 	u_int fflag;
724 	int error;
725 
726 	error = linux_getsockaddr(&sa, (struct osockaddr *)PTRIN(args->name),
727 	    args->namelen);
728 	if (error)
729 		return (error);
730 
731 	error = kern_connectat(td, AT_FDCWD, args->s, sa);
732 	free(sa, M_SONAME);
733 	if (error != EISCONN)
734 		return (error);
735 
736 	/*
737 	 * Linux doesn't return EISCONN the first time it occurs,
738 	 * when on a non-blocking socket. Instead it returns the
739 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
740 	 *
741 	 * XXXRW: Instead of using fgetsock(), check that it is a
742 	 * socket and use the file descriptor reference instead of
743 	 * creating a new one.
744 	 */
745 	error = fgetsock(td, args->s, cap_rights_init(&rights, CAP_CONNECT),
746 	    &so, &fflag);
747 	if (error == 0) {
748 		error = EISCONN;
749 		if (fflag & FNONBLOCK) {
750 			SOCK_LOCK(so);
751 			if (so->so_emuldata == 0)
752 				error = so->so_error;
753 			so->so_emuldata = (void *)1;
754 			SOCK_UNLOCK(so);
755 		}
756 		fputsock(so);
757 	}
758 	return (error);
759 }
760 
761 int
762 linux_listen(struct thread *td, struct linux_listen_args *args)
763 {
764 	struct listen_args /* {
765 		int s;
766 		int backlog;
767 	} */ bsd_args;
768 
769 	bsd_args.s = args->s;
770 	bsd_args.backlog = args->backlog;
771 	return (sys_listen(td, &bsd_args));
772 }
773 
774 static int
775 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
776     l_uintptr_t namelen, int flags)
777 {
778 	struct accept4_args /* {
779 		int	s;
780 		struct sockaddr * __restrict name;
781 		socklen_t * __restrict anamelen;
782 		int	flags;
783 	} */ bsd_args;
784 	int error;
785 
786 	bsd_args.s = s;
787 	/* XXX: */
788 	bsd_args.name = (struct sockaddr * __restrict)PTRIN(addr);
789 	bsd_args.anamelen = PTRIN(namelen);/* XXX */
790 	error = linux_set_socket_flags(flags, &bsd_args.flags);
791 	if (error != 0)
792 		return (error);
793 	error = sys_accept4(td, &bsd_args);
794 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
795 	if (error) {
796 		if (error == EFAULT && namelen != sizeof(struct sockaddr_in))
797 			return (EINVAL);
798 		return (error);
799 	}
800 	if (addr)
801 		error = linux_sa_put(PTRIN(addr));
802 	if (error) {
803 		(void)kern_close(td, td->td_retval[0]);
804 		td->td_retval[0] = 0;
805 	}
806 	return (error);
807 }
808 
809 int
810 linux_accept(struct thread *td, struct linux_accept_args *args)
811 {
812 
813 	return (linux_accept_common(td, args->s, args->addr,
814 	    args->namelen, 0));
815 }
816 
817 int
818 linux_accept4(struct thread *td, struct linux_accept4_args *args)
819 {
820 
821 	return (linux_accept_common(td, args->s, args->addr,
822 	    args->namelen, args->flags));
823 }
824 
825 int
826 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
827 {
828 	struct getsockname_args /* {
829 		int	fdes;
830 		struct sockaddr * __restrict asa;
831 		socklen_t * __restrict alen;
832 	} */ bsd_args;
833 	int error;
834 
835 	bsd_args.fdes = args->s;
836 	/* XXX: */
837 	bsd_args.asa = (struct sockaddr * __restrict)PTRIN(args->addr);
838 	bsd_args.alen = PTRIN(args->namelen);	/* XXX */
839 	error = sys_getsockname(td, &bsd_args);
840 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
841 	if (error)
842 		return (error);
843 	error = linux_sa_put(PTRIN(args->addr));
844 	if (error)
845 		return (error);
846 	return (0);
847 }
848 
849 int
850 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
851 {
852 	struct getpeername_args /* {
853 		int fdes;
854 		caddr_t asa;
855 		int *alen;
856 	} */ bsd_args;
857 	int error;
858 
859 	bsd_args.fdes = args->s;
860 	bsd_args.asa = (struct sockaddr *)PTRIN(args->addr);
861 	bsd_args.alen = (socklen_t *)PTRIN(args->namelen);
862 	error = sys_getpeername(td, &bsd_args);
863 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
864 	if (error)
865 		return (error);
866 	error = linux_sa_put(PTRIN(args->addr));
867 	if (error)
868 		return (error);
869 	return (0);
870 }
871 
872 int
873 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
874 {
875 	struct socketpair_args /* {
876 		int domain;
877 		int type;
878 		int protocol;
879 		int *rsv;
880 	} */ bsd_args;
881 	int error;
882 
883 	bsd_args.domain = linux_to_bsd_domain(args->domain);
884 	if (bsd_args.domain != PF_LOCAL)
885 		return (EAFNOSUPPORT);
886 	bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
887 	if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
888 		return (EINVAL);
889 	error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
890 		&bsd_args.type);
891 	if (error != 0)
892 		return (error);
893 	if (args->protocol != 0 && args->protocol != PF_UNIX)
894 
895 		/*
896 		 * Use of PF_UNIX as protocol argument is not right,
897 		 * but Linux does it.
898 		 * Do not map PF_UNIX as its Linux value is identical
899 		 * to FreeBSD one.
900 		 */
901 		return (EPROTONOSUPPORT);
902 	else
903 		bsd_args.protocol = 0;
904 	bsd_args.rsv = (int *)PTRIN(args->rsv);
905 	return (sys_socketpair(td, &bsd_args));
906 }
907 
908 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
909 struct linux_send_args {
910 	int s;
911 	l_uintptr_t msg;
912 	int len;
913 	int flags;
914 };
915 
916 static int
917 linux_send(struct thread *td, struct linux_send_args *args)
918 {
919 	struct sendto_args /* {
920 		int s;
921 		caddr_t buf;
922 		int len;
923 		int flags;
924 		caddr_t to;
925 		int tolen;
926 	} */ bsd_args;
927 
928 	bsd_args.s = args->s;
929 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
930 	bsd_args.len = args->len;
931 	bsd_args.flags = args->flags;
932 	bsd_args.to = NULL;
933 	bsd_args.tolen = 0;
934 	return sys_sendto(td, &bsd_args);
935 }
936 
937 struct linux_recv_args {
938 	int s;
939 	l_uintptr_t msg;
940 	int len;
941 	int flags;
942 };
943 
944 static int
945 linux_recv(struct thread *td, struct linux_recv_args *args)
946 {
947 	struct recvfrom_args /* {
948 		int s;
949 		caddr_t buf;
950 		int len;
951 		int flags;
952 		struct sockaddr *from;
953 		socklen_t fromlenaddr;
954 	} */ bsd_args;
955 
956 	bsd_args.s = args->s;
957 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
958 	bsd_args.len = args->len;
959 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
960 	bsd_args.from = NULL;
961 	bsd_args.fromlenaddr = 0;
962 	return (sys_recvfrom(td, &bsd_args));
963 }
964 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
965 
966 int
967 linux_sendto(struct thread *td, struct linux_sendto_args *args)
968 {
969 	struct msghdr msg;
970 	struct iovec aiov;
971 	int error;
972 
973 	if (linux_check_hdrincl(td, args->s) == 0)
974 		/* IP_HDRINCL set, tweak the packet before sending */
975 		return (linux_sendto_hdrincl(td, args));
976 
977 	msg.msg_name = PTRIN(args->to);
978 	msg.msg_namelen = args->tolen;
979 	msg.msg_iov = &aiov;
980 	msg.msg_iovlen = 1;
981 	msg.msg_control = NULL;
982 	msg.msg_flags = 0;
983 	aiov.iov_base = PTRIN(args->msg);
984 	aiov.iov_len = args->len;
985 	error = linux_sendit(td, args->s, &msg, args->flags, NULL,
986 	    UIO_USERSPACE);
987 	return (error);
988 }
989 
990 int
991 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
992 {
993 	struct msghdr msg;
994 	struct iovec aiov;
995 	int error;
996 
997 	if (PTRIN(args->fromlen) != NULL) {
998 		error = copyin(PTRIN(args->fromlen), &msg.msg_namelen,
999 		    sizeof(msg.msg_namelen));
1000 		if (error != 0)
1001 			return (error);
1002 
1003 		error = linux_to_bsd_sockaddr((struct sockaddr *)PTRIN(args->from),
1004 		    msg.msg_namelen);
1005 		if (error != 0)
1006 			return (error);
1007 	} else
1008 		msg.msg_namelen = 0;
1009 
1010 	msg.msg_name = (struct sockaddr * __restrict)PTRIN(args->from);
1011 	msg.msg_iov = &aiov;
1012 	msg.msg_iovlen = 1;
1013 	aiov.iov_base = PTRIN(args->buf);
1014 	aiov.iov_len = args->len;
1015 	msg.msg_control = 0;
1016 	msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
1017 
1018 	error = kern_recvit(td, args->s, &msg, UIO_USERSPACE, NULL);
1019 	if (error != 0)
1020 		return (error);
1021 
1022 	if (PTRIN(args->from) != NULL) {
1023 		error = bsd_to_linux_sockaddr((struct sockaddr *)
1024 		    PTRIN(args->from));
1025 		if (error != 0)
1026 			return (error);
1027 
1028 		error = linux_sa_put((struct osockaddr *)
1029 		    PTRIN(args->from));
1030 	}
1031 
1032 	if (PTRIN(args->fromlen) != NULL)
1033 		error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
1034 		    sizeof(msg.msg_namelen));
1035 
1036 	return (error);
1037 }
1038 
1039 static int
1040 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1041     l_uint flags)
1042 {
1043 	struct cmsghdr *cmsg;
1044 	struct cmsgcred cmcred;
1045 	struct mbuf *control;
1046 	struct msghdr msg;
1047 	struct l_cmsghdr linux_cmsg;
1048 	struct l_cmsghdr *ptr_cmsg;
1049 	struct l_msghdr linux_msg;
1050 	struct iovec *iov;
1051 	socklen_t datalen;
1052 	struct sockaddr *sa;
1053 	sa_family_t sa_family;
1054 	void *data;
1055 	int error;
1056 
1057 	error = copyin(msghdr, &linux_msg, sizeof(linux_msg));
1058 	if (error != 0)
1059 		return (error);
1060 
1061 	/*
1062 	 * Some Linux applications (ping) define a non-NULL control data
1063 	 * pointer, but a msg_controllen of 0, which is not allowed in the
1064 	 * FreeBSD system call interface.  NULL the msg_control pointer in
1065 	 * order to handle this case.  This should be checked, but allows the
1066 	 * Linux ping to work.
1067 	 */
1068 	if (PTRIN(linux_msg.msg_control) != NULL && linux_msg.msg_controllen == 0)
1069 		linux_msg.msg_control = PTROUT(NULL);
1070 
1071 	error = linux_to_bsd_msghdr(&msg, &linux_msg);
1072 	if (error != 0)
1073 		return (error);
1074 
1075 #ifdef COMPAT_LINUX32
1076 	error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
1077 	    &iov, EMSGSIZE);
1078 #else
1079 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1080 #endif
1081 	if (error != 0)
1082 		return (error);
1083 
1084 	control = NULL;
1085 	cmsg = NULL;
1086 
1087 	if ((ptr_cmsg = LINUX_CMSG_FIRSTHDR(&linux_msg)) != NULL) {
1088 		error = kern_getsockname(td, s, &sa, &datalen);
1089 		if (error != 0)
1090 			goto bad;
1091 		sa_family = sa->sa_family;
1092 		free(sa, M_SONAME);
1093 
1094 		error = ENOBUFS;
1095 		cmsg = malloc(CMSG_HDRSZ, M_LINUX, M_WAITOK|M_ZERO);
1096 		control = m_get(M_WAITOK, MT_CONTROL);
1097 
1098 		do {
1099 			error = copyin(ptr_cmsg, &linux_cmsg,
1100 			    sizeof(struct l_cmsghdr));
1101 			if (error != 0)
1102 				goto bad;
1103 
1104 			error = EINVAL;
1105 			if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr))
1106 				goto bad;
1107 
1108 			/*
1109 			 * Now we support only SCM_RIGHTS and SCM_CRED,
1110 			 * so return EINVAL in any other cmsg_type
1111 			 */
1112 			cmsg->cmsg_type =
1113 			    linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1114 			cmsg->cmsg_level =
1115 			    linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1116 			if (cmsg->cmsg_type == -1
1117 			    || cmsg->cmsg_level != SOL_SOCKET)
1118 				goto bad;
1119 
1120 			/*
1121 			 * Some applications (e.g. pulseaudio) attempt to
1122 			 * send ancillary data even if the underlying protocol
1123 			 * doesn't support it which is not allowed in the
1124 			 * FreeBSD system call interface.
1125 			 */
1126 			if (sa_family != AF_UNIX)
1127 				continue;
1128 
1129 			data = LINUX_CMSG_DATA(ptr_cmsg);
1130 			datalen = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1131 
1132 			switch (cmsg->cmsg_type)
1133 			{
1134 			case SCM_RIGHTS:
1135 				break;
1136 
1137 			case SCM_CREDS:
1138 				data = &cmcred;
1139 				datalen = sizeof(cmcred);
1140 
1141 				/*
1142 				 * The lower levels will fill in the structure
1143 				 */
1144 				bzero(data, datalen);
1145 				break;
1146 			}
1147 
1148 			cmsg->cmsg_len = CMSG_LEN(datalen);
1149 
1150 			error = ENOBUFS;
1151 			if (!m_append(control, CMSG_HDRSZ, (c_caddr_t)cmsg))
1152 				goto bad;
1153 			if (!m_append(control, datalen, (c_caddr_t)data))
1154 				goto bad;
1155 		} while ((ptr_cmsg = LINUX_CMSG_NXTHDR(&linux_msg, ptr_cmsg)));
1156 
1157 		if (m_length(control, NULL) == 0) {
1158 			m_freem(control);
1159 			control = NULL;
1160 		}
1161 	}
1162 
1163 	msg.msg_iov = iov;
1164 	msg.msg_flags = 0;
1165 	error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1166 
1167 bad:
1168 	m_freem(control);
1169 	free(iov, M_IOV);
1170 	if (cmsg)
1171 		free(cmsg, M_LINUX);
1172 	return (error);
1173 }
1174 
1175 int
1176 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1177 {
1178 
1179 	return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1180 	    args->flags));
1181 }
1182 
1183 int
1184 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1185 {
1186 	struct l_mmsghdr *msg;
1187 	l_uint retval;
1188 	int error, datagrams;
1189 
1190 	if (args->vlen > UIO_MAXIOV)
1191 		args->vlen = UIO_MAXIOV;
1192 
1193 	msg = PTRIN(args->msg);
1194 	datagrams = 0;
1195 	while (datagrams < args->vlen) {
1196 		error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1197 		    args->flags);
1198 		if (error != 0)
1199 			break;
1200 
1201 		retval = td->td_retval[0];
1202 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1203 		if (error != 0)
1204 			break;
1205 		++msg;
1206 		++datagrams;
1207 	}
1208 	if (error == 0)
1209 		td->td_retval[0] = datagrams;
1210 	return (error);
1211 }
1212 
1213 static int
1214 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1215     l_uint flags, struct msghdr *msg)
1216 {
1217 	struct cmsghdr *cm;
1218 	struct cmsgcred *cmcred;
1219 	struct l_cmsghdr *linux_cmsg = NULL;
1220 	struct l_ucred linux_ucred;
1221 	socklen_t datalen, outlen;
1222 	struct l_msghdr linux_msg;
1223 	struct iovec *iov, *uiov;
1224 	struct mbuf *control = NULL;
1225 	struct mbuf **controlp;
1226 	struct timeval *ftmvl;
1227 	l_timeval ltmvl;
1228 	caddr_t outbuf;
1229 	void *data;
1230 	int error, i, fd, fds, *fdp;
1231 
1232 	error = copyin(msghdr, &linux_msg, sizeof(linux_msg));
1233 	if (error != 0)
1234 		return (error);
1235 
1236 	error = linux_to_bsd_msghdr(msg, &linux_msg);
1237 	if (error != 0)
1238 		return (error);
1239 
1240 #ifdef COMPAT_LINUX32
1241 	error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1242 	    &iov, EMSGSIZE);
1243 #else
1244 	error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1245 #endif
1246 	if (error != 0)
1247 		return (error);
1248 
1249 	if (msg->msg_name) {
1250 		error = linux_to_bsd_sockaddr((struct sockaddr *)msg->msg_name,
1251 		    msg->msg_namelen);
1252 		if (error != 0)
1253 			goto bad;
1254 	}
1255 
1256 	uiov = msg->msg_iov;
1257 	msg->msg_iov = iov;
1258 	controlp = (msg->msg_control != NULL) ? &control : NULL;
1259 	error = kern_recvit(td, s, msg, UIO_USERSPACE, controlp);
1260 	msg->msg_iov = uiov;
1261 	if (error != 0)
1262 		goto bad;
1263 
1264 	error = bsd_to_linux_msghdr(msg, &linux_msg);
1265 	if (error != 0)
1266 		goto bad;
1267 
1268 	if (linux_msg.msg_name) {
1269 		error = bsd_to_linux_sockaddr((struct sockaddr *)
1270 		    PTRIN(linux_msg.msg_name));
1271 		if (error != 0)
1272 			goto bad;
1273 	}
1274 	if (linux_msg.msg_name && linux_msg.msg_namelen > 2) {
1275 		error = linux_sa_put(PTRIN(linux_msg.msg_name));
1276 		if (error != 0)
1277 			goto bad;
1278 	}
1279 
1280 	outbuf = PTRIN(linux_msg.msg_control);
1281 	outlen = 0;
1282 
1283 	if (control) {
1284 		linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1285 
1286 		msg->msg_control = mtod(control, struct cmsghdr *);
1287 		msg->msg_controllen = control->m_len;
1288 
1289 		cm = CMSG_FIRSTHDR(msg);
1290 
1291 		while (cm != NULL) {
1292 			linux_cmsg->cmsg_type =
1293 			    bsd_to_linux_cmsg_type(cm->cmsg_type);
1294 			linux_cmsg->cmsg_level =
1295 			    bsd_to_linux_sockopt_level(cm->cmsg_level);
1296 			if (linux_cmsg->cmsg_type == -1
1297 			    || cm->cmsg_level != SOL_SOCKET)
1298 			{
1299 				error = EINVAL;
1300 				goto bad;
1301 			}
1302 
1303 			data = CMSG_DATA(cm);
1304 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1305 
1306 			switch (cm->cmsg_type)
1307 			{
1308 			case SCM_RIGHTS:
1309 				if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1310 					fds = datalen / sizeof(int);
1311 					fdp = data;
1312 					for (i = 0; i < fds; i++) {
1313 						fd = *fdp++;
1314 						(void)kern_fcntl(td, fd,
1315 						    F_SETFD, FD_CLOEXEC);
1316 					}
1317 				}
1318 				break;
1319 
1320 			case SCM_CREDS:
1321 				/*
1322 				 * Currently LOCAL_CREDS is never in
1323 				 * effect for Linux so no need to worry
1324 				 * about sockcred
1325 				 */
1326 				if (datalen != sizeof(*cmcred)) {
1327 					error = EMSGSIZE;
1328 					goto bad;
1329 				}
1330 				cmcred = (struct cmsgcred *)data;
1331 				bzero(&linux_ucred, sizeof(linux_ucred));
1332 				linux_ucred.pid = cmcred->cmcred_pid;
1333 				linux_ucred.uid = cmcred->cmcred_uid;
1334 				linux_ucred.gid = cmcred->cmcred_gid;
1335 				data = &linux_ucred;
1336 				datalen = sizeof(linux_ucred);
1337 				break;
1338 
1339 			case SCM_TIMESTAMP:
1340 				if (datalen != sizeof(struct timeval)) {
1341 					error = EMSGSIZE;
1342 					goto bad;
1343 				}
1344 				ftmvl = (struct timeval *)data;
1345 				ltmvl.tv_sec = ftmvl->tv_sec;
1346 				ltmvl.tv_usec = ftmvl->tv_usec;
1347 				data = &ltmvl;
1348 				datalen = sizeof(ltmvl);
1349 				break;
1350 			}
1351 
1352 			if (outlen + LINUX_CMSG_LEN(datalen) >
1353 			    linux_msg.msg_controllen) {
1354 				if (outlen == 0) {
1355 					error = EMSGSIZE;
1356 					goto bad;
1357 				} else {
1358 					linux_msg.msg_flags |=
1359 					    LINUX_MSG_CTRUNC;
1360 					goto out;
1361 				}
1362 			}
1363 
1364 			linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1365 
1366 			error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1367 			if (error)
1368 				goto bad;
1369 			outbuf += L_CMSG_HDRSZ;
1370 
1371 			error = copyout(data, outbuf, datalen);
1372 			if (error)
1373 				goto bad;
1374 
1375 			outbuf += LINUX_CMSG_ALIGN(datalen);
1376 			outlen += LINUX_CMSG_LEN(datalen);
1377 
1378 			cm = CMSG_NXTHDR(msg, cm);
1379 		}
1380 	}
1381 
1382 out:
1383 	linux_msg.msg_controllen = outlen;
1384 	error = copyout(&linux_msg, msghdr, sizeof(linux_msg));
1385 
1386 bad:
1387 	free(iov, M_IOV);
1388 	m_freem(control);
1389 	free(linux_cmsg, M_LINUX);
1390 
1391 	return (error);
1392 }
1393 
1394 int
1395 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1396 {
1397 	struct msghdr bsd_msg;
1398 
1399 	return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1400 	    args->flags, &bsd_msg));
1401 }
1402 
1403 int
1404 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1405 {
1406 	struct l_mmsghdr *msg;
1407 	struct msghdr bsd_msg;
1408 	struct l_timespec lts;
1409 	struct timespec ts, tts;
1410 	l_uint retval;
1411 	int error, datagrams;
1412 
1413 	if (args->timeout) {
1414 		error = copyin(args->timeout, &lts, sizeof(struct l_timespec));
1415 		if (error != 0)
1416 			return (error);
1417 		error = linux_to_native_timespec(&ts, &lts);
1418 		if (error != 0)
1419 			return (error);
1420 		getnanotime(&tts);
1421 		timespecadd(&tts, &ts);
1422 	}
1423 
1424 	msg = PTRIN(args->msg);
1425 	datagrams = 0;
1426 	while (datagrams < args->vlen) {
1427 		error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1428 		    args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1429 		if (error != 0)
1430 			break;
1431 
1432 		retval = td->td_retval[0];
1433 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1434 		if (error != 0)
1435 			break;
1436 		++msg;
1437 		++datagrams;
1438 
1439 		/*
1440 		 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1441 		 */
1442 		if (args->flags & LINUX_MSG_WAITFORONE)
1443 			args->flags |= LINUX_MSG_DONTWAIT;
1444 
1445 		/*
1446 		 * See BUGS section of recvmmsg(2).
1447 		 */
1448 		if (args->timeout) {
1449 			getnanotime(&ts);
1450 			timespecsub(&ts, &tts);
1451 			if (!timespecisset(&ts) || ts.tv_sec > 0)
1452 				break;
1453 		}
1454 		/* Out of band data, return right away. */
1455 		if (bsd_msg.msg_flags & MSG_OOB)
1456 			break;
1457 	}
1458 	if (error == 0)
1459 		td->td_retval[0] = datagrams;
1460 	return (error);
1461 }
1462 
1463 int
1464 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1465 {
1466 	struct shutdown_args /* {
1467 		int s;
1468 		int how;
1469 	} */ bsd_args;
1470 
1471 	bsd_args.s = args->s;
1472 	bsd_args.how = args->how;
1473 	return (sys_shutdown(td, &bsd_args));
1474 }
1475 
1476 int
1477 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1478 {
1479 	struct setsockopt_args /* {
1480 		int s;
1481 		int level;
1482 		int name;
1483 		caddr_t val;
1484 		int valsize;
1485 	} */ bsd_args;
1486 	l_timeval linux_tv;
1487 	struct timeval tv;
1488 	int error, name;
1489 
1490 	bsd_args.s = args->s;
1491 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1492 	switch (bsd_args.level) {
1493 	case SOL_SOCKET:
1494 		name = linux_to_bsd_so_sockopt(args->optname);
1495 		switch (name) {
1496 		case SO_RCVTIMEO:
1497 			/* FALLTHROUGH */
1498 		case SO_SNDTIMEO:
1499 			error = copyin(PTRIN(args->optval), &linux_tv,
1500 			    sizeof(linux_tv));
1501 			if (error)
1502 				return (error);
1503 			tv.tv_sec = linux_tv.tv_sec;
1504 			tv.tv_usec = linux_tv.tv_usec;
1505 			return (kern_setsockopt(td, args->s, bsd_args.level,
1506 			    name, &tv, UIO_SYSSPACE, sizeof(tv)));
1507 			/* NOTREACHED */
1508 			break;
1509 		default:
1510 			break;
1511 		}
1512 		break;
1513 	case IPPROTO_IP:
1514 		name = linux_to_bsd_ip_sockopt(args->optname);
1515 		break;
1516 	case IPPROTO_TCP:
1517 		name = linux_to_bsd_tcp_sockopt(args->optname);
1518 		break;
1519 	default:
1520 		name = -1;
1521 		break;
1522 	}
1523 	if (name == -1)
1524 		return (ENOPROTOOPT);
1525 
1526 	bsd_args.name = name;
1527 	bsd_args.val = PTRIN(args->optval);
1528 	bsd_args.valsize = args->optlen;
1529 
1530 	if (name == IPV6_NEXTHOP) {
1531 		linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
1532 			bsd_args.valsize);
1533 		error = sys_setsockopt(td, &bsd_args);
1534 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
1535 	} else
1536 		error = sys_setsockopt(td, &bsd_args);
1537 
1538 	return (error);
1539 }
1540 
1541 int
1542 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1543 {
1544 	struct getsockopt_args /* {
1545 		int s;
1546 		int level;
1547 		int name;
1548 		caddr_t val;
1549 		int *avalsize;
1550 	} */ bsd_args;
1551 	l_timeval linux_tv;
1552 	struct timeval tv;
1553 	socklen_t tv_len, xulen;
1554 	struct xucred xu;
1555 	struct l_ucred lxu;
1556 	int error, name;
1557 
1558 	bsd_args.s = args->s;
1559 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1560 	switch (bsd_args.level) {
1561 	case SOL_SOCKET:
1562 		name = linux_to_bsd_so_sockopt(args->optname);
1563 		switch (name) {
1564 		case SO_RCVTIMEO:
1565 			/* FALLTHROUGH */
1566 		case SO_SNDTIMEO:
1567 			tv_len = sizeof(tv);
1568 			error = kern_getsockopt(td, args->s, bsd_args.level,
1569 			    name, &tv, UIO_SYSSPACE, &tv_len);
1570 			if (error)
1571 				return (error);
1572 			linux_tv.tv_sec = tv.tv_sec;
1573 			linux_tv.tv_usec = tv.tv_usec;
1574 			return (copyout(&linux_tv, PTRIN(args->optval),
1575 			    sizeof(linux_tv)));
1576 			/* NOTREACHED */
1577 			break;
1578 		case LOCAL_PEERCRED:
1579 			if (args->optlen != sizeof(lxu))
1580 				return (EINVAL);
1581 			xulen = sizeof(xu);
1582 			error = kern_getsockopt(td, args->s, bsd_args.level,
1583 			    name, &xu, UIO_SYSSPACE, &xulen);
1584 			if (error)
1585 				return (error);
1586 			/*
1587 			 * XXX Use 0 for pid as the FreeBSD does not cache peer pid.
1588 			 */
1589 			lxu.pid = 0;
1590 			lxu.uid = xu.cr_uid;
1591 			lxu.gid = xu.cr_gid;
1592 			return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1593 			/* NOTREACHED */
1594 			break;
1595 		default:
1596 			break;
1597 		}
1598 		break;
1599 	case IPPROTO_IP:
1600 		name = linux_to_bsd_ip_sockopt(args->optname);
1601 		break;
1602 	case IPPROTO_TCP:
1603 		name = linux_to_bsd_tcp_sockopt(args->optname);
1604 		break;
1605 	default:
1606 		name = -1;
1607 		break;
1608 	}
1609 	if (name == -1)
1610 		return (EINVAL);
1611 
1612 	bsd_args.name = name;
1613 	bsd_args.val = PTRIN(args->optval);
1614 	bsd_args.avalsize = PTRIN(args->optlen);
1615 
1616 	if (name == IPV6_NEXTHOP) {
1617 		error = sys_getsockopt(td, &bsd_args);
1618 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
1619 	} else
1620 		error = sys_getsockopt(td, &bsd_args);
1621 
1622 	return (error);
1623 }
1624 
1625 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1626 
1627 /* Argument list sizes for linux_socketcall */
1628 
1629 #define LINUX_AL(x) ((x) * sizeof(l_ulong))
1630 
1631 static const unsigned char lxs_args[] = {
1632 	LINUX_AL(0) /* unused*/,	LINUX_AL(3) /* socket */,
1633 	LINUX_AL(3) /* bind */,		LINUX_AL(3) /* connect */,
1634 	LINUX_AL(2) /* listen */,	LINUX_AL(3) /* accept */,
1635 	LINUX_AL(3) /* getsockname */,	LINUX_AL(3) /* getpeername */,
1636 	LINUX_AL(4) /* socketpair */,	LINUX_AL(4) /* send */,
1637 	LINUX_AL(4) /* recv */,		LINUX_AL(6) /* sendto */,
1638 	LINUX_AL(6) /* recvfrom */,	LINUX_AL(2) /* shutdown */,
1639 	LINUX_AL(5) /* setsockopt */,	LINUX_AL(5) /* getsockopt */,
1640 	LINUX_AL(3) /* sendmsg */,	LINUX_AL(3) /* recvmsg */,
1641 	LINUX_AL(4) /* accept4 */,	LINUX_AL(5) /* recvmmsg */,
1642 	LINUX_AL(4) /* sendmmsg */
1643 };
1644 
1645 #define	LINUX_AL_SIZE	sizeof(lxs_args) / sizeof(lxs_args[0]) - 1
1646 
1647 int
1648 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1649 {
1650 	l_ulong a[6];
1651 	void *arg;
1652 	int error;
1653 
1654 	if (args->what < LINUX_SOCKET || args->what > LINUX_AL_SIZE)
1655 		return (EINVAL);
1656 	error = copyin(PTRIN(args->args), a, lxs_args[args->what]);
1657 	if (error)
1658 		return (error);
1659 
1660 	arg = a;
1661 	switch (args->what) {
1662 	case LINUX_SOCKET:
1663 		return (linux_socket(td, arg));
1664 	case LINUX_BIND:
1665 		return (linux_bind(td, arg));
1666 	case LINUX_CONNECT:
1667 		return (linux_connect(td, arg));
1668 	case LINUX_LISTEN:
1669 		return (linux_listen(td, arg));
1670 	case LINUX_ACCEPT:
1671 		return (linux_accept(td, arg));
1672 	case LINUX_GETSOCKNAME:
1673 		return (linux_getsockname(td, arg));
1674 	case LINUX_GETPEERNAME:
1675 		return (linux_getpeername(td, arg));
1676 	case LINUX_SOCKETPAIR:
1677 		return (linux_socketpair(td, arg));
1678 	case LINUX_SEND:
1679 		return (linux_send(td, arg));
1680 	case LINUX_RECV:
1681 		return (linux_recv(td, arg));
1682 	case LINUX_SENDTO:
1683 		return (linux_sendto(td, arg));
1684 	case LINUX_RECVFROM:
1685 		return (linux_recvfrom(td, arg));
1686 	case LINUX_SHUTDOWN:
1687 		return (linux_shutdown(td, arg));
1688 	case LINUX_SETSOCKOPT:
1689 		return (linux_setsockopt(td, arg));
1690 	case LINUX_GETSOCKOPT:
1691 		return (linux_getsockopt(td, arg));
1692 	case LINUX_SENDMSG:
1693 		return (linux_sendmsg(td, arg));
1694 	case LINUX_RECVMSG:
1695 		return (linux_recvmsg(td, arg));
1696 	case LINUX_ACCEPT4:
1697 		return (linux_accept4(td, arg));
1698 	case LINUX_RECVMMSG:
1699 		return (linux_recvmmsg(td, arg));
1700 	case LINUX_SENDMMSG:
1701 		return (linux_sendmmsg(td, arg));
1702 	}
1703 
1704 	uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
1705 	return (ENOSYS);
1706 }
1707 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1708