xref: /freebsd/sys/compat/linux/linux_socket.c (revision d9f0ce31900a48d1a2bfc1c8c86f79d1e831451a)
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_ip6_sockopt(int opt)
293 {
294 
295 	switch (opt) {
296 	case LINUX_IPV6_NEXTHOP:
297 		return (IPV6_NEXTHOP);
298 	case LINUX_IPV6_UNICAST_HOPS:
299 		return (IPV6_UNICAST_HOPS);
300 	case LINUX_IPV6_MULTICAST_IF:
301 		return (IPV6_MULTICAST_IF);
302 	case LINUX_IPV6_MULTICAST_HOPS:
303 		return (IPV6_MULTICAST_HOPS);
304 	case LINUX_IPV6_MULTICAST_LOOP:
305 		return (IPV6_MULTICAST_LOOP);
306 	case LINUX_IPV6_ADD_MEMBERSHIP:
307 		return (IPV6_JOIN_GROUP);
308 	case LINUX_IPV6_DROP_MEMBERSHIP:
309 		return (IPV6_LEAVE_GROUP);
310 	case LINUX_IPV6_V6ONLY:
311 		return (IPV6_V6ONLY);
312 	case LINUX_IPV6_DONTFRAG:
313 		return (IPV6_DONTFRAG);
314 #if 0
315 	case LINUX_IPV6_CHECKSUM:
316 		return (IPV6_CHECKSUM);
317 	case LINUX_IPV6_RECVPKTINFO:
318 		return (IPV6_RECVPKTINFO);
319 	case LINUX_IPV6_PKTINFO:
320 		return (IPV6_PKTINFO);
321 	case LINUX_IPV6_RECVHOPLIMIT:
322 		return (IPV6_RECVHOPLIMIT);
323 	case LINUX_IPV6_HOPLIMIT:
324 		return (IPV6_HOPLIMIT);
325 	case LINUX_IPV6_RECVHOPOPTS:
326 		return (IPV6_RECVHOPOPTS);
327 	case LINUX_IPV6_HOPOPTS:
328 		return (IPV6_HOPOPTS);
329 	case LINUX_IPV6_RTHDRDSTOPTS:
330 		return (IPV6_RTHDRDSTOPTS);
331 	case LINUX_IPV6_RECVRTHDR:
332 		return (IPV6_RECVRTHDR);
333 	case LINUX_IPV6_RTHDR:
334 		return (IPV6_RTHDR);
335 	case LINUX_IPV6_RECVDSTOPTS:
336 		return (IPV6_RECVDSTOPTS);
337 	case LINUX_IPV6_DSTOPTS:
338 		return (IPV6_DSTOPTS);
339 	case LINUX_IPV6_RECVPATHMTU:
340 		return (IPV6_RECVPATHMTU);
341 	case LINUX_IPV6_PATHMTU:
342 		return (IPV6_PATHMTU);
343 #endif
344 	}
345 	return (-1);
346 }
347 
348 static int
349 linux_to_bsd_so_sockopt(int opt)
350 {
351 
352 	switch (opt) {
353 	case LINUX_SO_DEBUG:
354 		return (SO_DEBUG);
355 	case LINUX_SO_REUSEADDR:
356 		return (SO_REUSEADDR);
357 	case LINUX_SO_TYPE:
358 		return (SO_TYPE);
359 	case LINUX_SO_ERROR:
360 		return (SO_ERROR);
361 	case LINUX_SO_DONTROUTE:
362 		return (SO_DONTROUTE);
363 	case LINUX_SO_BROADCAST:
364 		return (SO_BROADCAST);
365 	case LINUX_SO_SNDBUF:
366 		return (SO_SNDBUF);
367 	case LINUX_SO_RCVBUF:
368 		return (SO_RCVBUF);
369 	case LINUX_SO_KEEPALIVE:
370 		return (SO_KEEPALIVE);
371 	case LINUX_SO_OOBINLINE:
372 		return (SO_OOBINLINE);
373 	case LINUX_SO_LINGER:
374 		return (SO_LINGER);
375 	case LINUX_SO_PEERCRED:
376 		return (LOCAL_PEERCRED);
377 	case LINUX_SO_RCVLOWAT:
378 		return (SO_RCVLOWAT);
379 	case LINUX_SO_SNDLOWAT:
380 		return (SO_SNDLOWAT);
381 	case LINUX_SO_RCVTIMEO:
382 		return (SO_RCVTIMEO);
383 	case LINUX_SO_SNDTIMEO:
384 		return (SO_SNDTIMEO);
385 	case LINUX_SO_TIMESTAMP:
386 		return (SO_TIMESTAMP);
387 	case LINUX_SO_ACCEPTCONN:
388 		return (SO_ACCEPTCONN);
389 	}
390 	return (-1);
391 }
392 
393 static int
394 linux_to_bsd_tcp_sockopt(int opt)
395 {
396 
397 	switch (opt) {
398 	case LINUX_TCP_NODELAY:
399 		return (TCP_NODELAY);
400 	case LINUX_TCP_MAXSEG:
401 		return (TCP_MAXSEG);
402 	case LINUX_TCP_KEEPIDLE:
403 		return (TCP_KEEPIDLE);
404 	case LINUX_TCP_KEEPINTVL:
405 		return (TCP_KEEPINTVL);
406 	case LINUX_TCP_KEEPCNT:
407 		return (TCP_KEEPCNT);
408 	case LINUX_TCP_MD5SIG:
409 		return (TCP_MD5SIG);
410 	}
411 	return (-1);
412 }
413 
414 static int
415 linux_to_bsd_msg_flags(int flags)
416 {
417 	int ret_flags = 0;
418 
419 	if (flags & LINUX_MSG_OOB)
420 		ret_flags |= MSG_OOB;
421 	if (flags & LINUX_MSG_PEEK)
422 		ret_flags |= MSG_PEEK;
423 	if (flags & LINUX_MSG_DONTROUTE)
424 		ret_flags |= MSG_DONTROUTE;
425 	if (flags & LINUX_MSG_CTRUNC)
426 		ret_flags |= MSG_CTRUNC;
427 	if (flags & LINUX_MSG_TRUNC)
428 		ret_flags |= MSG_TRUNC;
429 	if (flags & LINUX_MSG_DONTWAIT)
430 		ret_flags |= MSG_DONTWAIT;
431 	if (flags & LINUX_MSG_EOR)
432 		ret_flags |= MSG_EOR;
433 	if (flags & LINUX_MSG_WAITALL)
434 		ret_flags |= MSG_WAITALL;
435 	if (flags & LINUX_MSG_NOSIGNAL)
436 		ret_flags |= MSG_NOSIGNAL;
437 #if 0 /* not handled */
438 	if (flags & LINUX_MSG_PROXY)
439 		;
440 	if (flags & LINUX_MSG_FIN)
441 		;
442 	if (flags & LINUX_MSG_SYN)
443 		;
444 	if (flags & LINUX_MSG_CONFIRM)
445 		;
446 	if (flags & LINUX_MSG_RST)
447 		;
448 	if (flags & LINUX_MSG_ERRQUEUE)
449 		;
450 #endif
451 	return (ret_flags);
452 }
453 
454 /*
455 * If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the
456 * native syscall will fault.  Thus, we don't really need to check the
457 * return values for these functions.
458 */
459 
460 static int
461 bsd_to_linux_sockaddr(struct sockaddr *arg)
462 {
463 	struct sockaddr sa;
464 	size_t sa_len = sizeof(struct sockaddr);
465 	int error;
466 
467 	if ((error = copyin(arg, &sa, sa_len)))
468 		return (error);
469 
470 	*(u_short *)&sa = sa.sa_family;
471 	return (copyout(&sa, arg, sa_len));
472 }
473 
474 static int
475 linux_to_bsd_sockaddr(struct sockaddr *arg, int len)
476 {
477 	struct sockaddr sa;
478 	size_t sa_len = sizeof(struct sockaddr);
479 	int error;
480 
481 	if ((error = copyin(arg, &sa, sa_len)))
482 		return (error);
483 
484 	sa.sa_family = *(sa_family_t *)&sa;
485 	sa.sa_len = len;
486 	return (copyout(&sa, arg, sa_len));
487 }
488 
489 static int
490 linux_sa_put(struct osockaddr *osa)
491 {
492 	struct osockaddr sa;
493 	int error, bdom;
494 
495 	/*
496 	 * Only read/write the osockaddr family part, the rest is
497 	 * not changed.
498 	 */
499 	error = copyin(osa, &sa, sizeof(sa.sa_family));
500 	if (error)
501 		return (error);
502 
503 	bdom = bsd_to_linux_domain(sa.sa_family);
504 	if (bdom == -1)
505 		return (EINVAL);
506 
507 	sa.sa_family = bdom;
508 	return (copyout(&sa, osa, sizeof(sa.sa_family)));
509 }
510 
511 static int
512 linux_to_bsd_cmsg_type(int cmsg_type)
513 {
514 
515 	switch (cmsg_type) {
516 	case LINUX_SCM_RIGHTS:
517 		return (SCM_RIGHTS);
518 	case LINUX_SCM_CREDENTIALS:
519 		return (SCM_CREDS);
520 	}
521 	return (-1);
522 }
523 
524 static int
525 bsd_to_linux_cmsg_type(int cmsg_type)
526 {
527 
528 	switch (cmsg_type) {
529 	case SCM_RIGHTS:
530 		return (LINUX_SCM_RIGHTS);
531 	case SCM_CREDS:
532 		return (LINUX_SCM_CREDENTIALS);
533 	case SCM_TIMESTAMP:
534 		return (LINUX_SCM_TIMESTAMP);
535 	}
536 	return (-1);
537 }
538 
539 static int
540 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
541 {
542 	if (lhdr->msg_controllen > INT_MAX)
543 		return (ENOBUFS);
544 
545 	bhdr->msg_name		= PTRIN(lhdr->msg_name);
546 	bhdr->msg_namelen	= lhdr->msg_namelen;
547 	bhdr->msg_iov		= PTRIN(lhdr->msg_iov);
548 	bhdr->msg_iovlen	= lhdr->msg_iovlen;
549 	bhdr->msg_control	= PTRIN(lhdr->msg_control);
550 
551 	/*
552 	 * msg_controllen is skipped since BSD and LINUX control messages
553 	 * are potentially different sizes (e.g. the cred structure used
554 	 * by SCM_CREDS is different between the two operating system).
555 	 *
556 	 * The caller can set it (if necessary) after converting all the
557 	 * control messages.
558 	 */
559 
560 	bhdr->msg_flags		= linux_to_bsd_msg_flags(lhdr->msg_flags);
561 	return (0);
562 }
563 
564 static int
565 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
566 {
567 	lhdr->msg_name		= PTROUT(bhdr->msg_name);
568 	lhdr->msg_namelen	= bhdr->msg_namelen;
569 	lhdr->msg_iov		= PTROUT(bhdr->msg_iov);
570 	lhdr->msg_iovlen	= bhdr->msg_iovlen;
571 	lhdr->msg_control	= PTROUT(bhdr->msg_control);
572 
573 	/*
574 	 * msg_controllen is skipped since BSD and LINUX control messages
575 	 * are potentially different sizes (e.g. the cred structure used
576 	 * by SCM_CREDS is different between the two operating system).
577 	 *
578 	 * The caller can set it (if necessary) after converting all the
579 	 * control messages.
580 	 */
581 
582 	/* msg_flags skipped */
583 	return (0);
584 }
585 
586 static int
587 linux_set_socket_flags(int lflags, int *flags)
588 {
589 
590 	if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
591 		return (EINVAL);
592 	if (lflags & LINUX_SOCK_NONBLOCK)
593 		*flags |= SOCK_NONBLOCK;
594 	if (lflags & LINUX_SOCK_CLOEXEC)
595 		*flags |= SOCK_CLOEXEC;
596 	return (0);
597 }
598 
599 static int
600 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
601     struct mbuf *control, enum uio_seg segflg)
602 {
603 	struct sockaddr *to;
604 	int error;
605 
606 	if (mp->msg_name != NULL) {
607 		error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen);
608 		if (error)
609 			return (error);
610 		mp->msg_name = to;
611 	} else
612 		to = NULL;
613 
614 	error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
615 	    segflg);
616 
617 	if (to)
618 		free(to, M_SONAME);
619 	return (error);
620 }
621 
622 /* Return 0 if IP_HDRINCL is set for the given socket. */
623 static int
624 linux_check_hdrincl(struct thread *td, int s)
625 {
626 	int error, optval;
627 	socklen_t size_val;
628 
629 	size_val = sizeof(optval);
630 	error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
631 	    &optval, UIO_SYSSPACE, &size_val);
632 	if (error)
633 		return (error);
634 
635 	return (optval == 0);
636 }
637 
638 /*
639  * Updated sendto() when IP_HDRINCL is set:
640  * tweak endian-dependent fields in the IP packet.
641  */
642 static int
643 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
644 {
645 /*
646  * linux_ip_copysize defines how many bytes we should copy
647  * from the beginning of the IP packet before we customize it for BSD.
648  * It should include all the fields we modify (ip_len and ip_off).
649  */
650 #define linux_ip_copysize	8
651 
652 	struct ip *packet;
653 	struct msghdr msg;
654 	struct iovec aiov[1];
655 	int error;
656 
657 	/* Check that the packet isn't too big or too small. */
658 	if (linux_args->len < linux_ip_copysize ||
659 	    linux_args->len > IP_MAXPACKET)
660 		return (EINVAL);
661 
662 	packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
663 
664 	/* Make kernel copy of the packet to be sent */
665 	if ((error = copyin(PTRIN(linux_args->msg), packet,
666 	    linux_args->len)))
667 		goto goout;
668 
669 	/* Convert fields from Linux to BSD raw IP socket format */
670 	packet->ip_len = linux_args->len;
671 	packet->ip_off = ntohs(packet->ip_off);
672 
673 	/* Prepare the msghdr and iovec structures describing the new packet */
674 	msg.msg_name = PTRIN(linux_args->to);
675 	msg.msg_namelen = linux_args->tolen;
676 	msg.msg_iov = aiov;
677 	msg.msg_iovlen = 1;
678 	msg.msg_control = NULL;
679 	msg.msg_flags = 0;
680 	aiov[0].iov_base = (char *)packet;
681 	aiov[0].iov_len = linux_args->len;
682 	error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
683 	    NULL, UIO_SYSSPACE);
684 goout:
685 	free(packet, M_LINUX);
686 	return (error);
687 }
688 
689 int
690 linux_socket(struct thread *td, struct linux_socket_args *args)
691 {
692 	struct socket_args /* {
693 		int domain;
694 		int type;
695 		int protocol;
696 	} */ bsd_args;
697 	int retval_socket;
698 
699 	bsd_args.protocol = args->protocol;
700 	bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
701 	if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
702 		return (EINVAL);
703 	retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
704 		&bsd_args.type);
705 	if (retval_socket != 0)
706 		return (retval_socket);
707 	bsd_args.domain = linux_to_bsd_domain(args->domain);
708 	if (bsd_args.domain == -1)
709 		return (EAFNOSUPPORT);
710 
711 	retval_socket = sys_socket(td, &bsd_args);
712 	if (retval_socket)
713 		return (retval_socket);
714 
715 	if (bsd_args.type == SOCK_RAW
716 	    && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
717 	    && bsd_args.domain == PF_INET) {
718 		/* It's a raw IP socket: set the IP_HDRINCL option. */
719 		int hdrincl;
720 
721 		hdrincl = 1;
722 		/* We ignore any error returned by kern_setsockopt() */
723 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
724 		    &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
725 	}
726 #ifdef INET6
727 	/*
728 	 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
729 	 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
730 	 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
731 	 * sysctl value.
732 	 */
733 	if (bsd_args.domain == PF_INET6) {
734 		int v6only;
735 
736 		v6only = 0;
737 		/* We ignore any error returned by setsockopt() */
738 		kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
739 		    &v6only, UIO_SYSSPACE, sizeof(v6only));
740 	}
741 #endif
742 
743 	return (retval_socket);
744 }
745 
746 int
747 linux_bind(struct thread *td, struct linux_bind_args *args)
748 {
749 	struct sockaddr *sa;
750 	int error;
751 
752 	error = linux_getsockaddr(&sa, PTRIN(args->name),
753 	    args->namelen);
754 	if (error)
755 		return (error);
756 
757 	error = kern_bindat(td, AT_FDCWD, args->s, sa);
758 	free(sa, M_SONAME);
759 	if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
760 	   	return (EINVAL);
761 	return (error);
762 }
763 
764 int
765 linux_connect(struct thread *td, struct linux_connect_args *args)
766 {
767 	cap_rights_t rights;
768 	struct socket *so;
769 	struct sockaddr *sa;
770 	u_int fflag;
771 	int error;
772 
773 	error = linux_getsockaddr(&sa, (struct osockaddr *)PTRIN(args->name),
774 	    args->namelen);
775 	if (error)
776 		return (error);
777 
778 	error = kern_connectat(td, AT_FDCWD, args->s, sa);
779 	free(sa, M_SONAME);
780 	if (error != EISCONN)
781 		return (error);
782 
783 	/*
784 	 * Linux doesn't return EISCONN the first time it occurs,
785 	 * when on a non-blocking socket. Instead it returns the
786 	 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
787 	 *
788 	 * XXXRW: Instead of using fgetsock(), check that it is a
789 	 * socket and use the file descriptor reference instead of
790 	 * creating a new one.
791 	 */
792 	error = fgetsock(td, args->s, cap_rights_init(&rights, CAP_CONNECT),
793 	    &so, &fflag);
794 	if (error == 0) {
795 		error = EISCONN;
796 		if (fflag & FNONBLOCK) {
797 			SOCK_LOCK(so);
798 			if (so->so_emuldata == 0)
799 				error = so->so_error;
800 			so->so_emuldata = (void *)1;
801 			SOCK_UNLOCK(so);
802 		}
803 		fputsock(so);
804 	}
805 	return (error);
806 }
807 
808 int
809 linux_listen(struct thread *td, struct linux_listen_args *args)
810 {
811 	struct listen_args /* {
812 		int s;
813 		int backlog;
814 	} */ bsd_args;
815 
816 	bsd_args.s = args->s;
817 	bsd_args.backlog = args->backlog;
818 	return (sys_listen(td, &bsd_args));
819 }
820 
821 static int
822 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
823     l_uintptr_t namelen, int flags)
824 {
825 	struct accept4_args /* {
826 		int	s;
827 		struct sockaddr * __restrict name;
828 		socklen_t * __restrict anamelen;
829 		int	flags;
830 	} */ bsd_args;
831 	cap_rights_t rights;
832 	struct socket *so;
833 	struct file *fp;
834 	int error, error1;
835 
836 	bsd_args.s = s;
837 	/* XXX: */
838 	bsd_args.name = (struct sockaddr * __restrict)PTRIN(addr);
839 	bsd_args.anamelen = PTRIN(namelen);/* XXX */
840 	bsd_args.flags = 0;
841 	error = linux_set_socket_flags(flags, &bsd_args.flags);
842 	if (error != 0)
843 		return (error);
844 	error = sys_accept4(td, &bsd_args);
845 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
846 	if (error) {
847 		if (error == EFAULT && namelen != sizeof(struct sockaddr_in))
848 			return (EINVAL);
849 		if (error == EINVAL) {
850 			error1 = getsock_cap(td, s, &rights, &fp, NULL);
851 			if (error1 != 0)
852 				return (error1);
853 			so = fp->f_data;
854 			if (so->so_type == SOCK_DGRAM) {
855 				fdrop(fp, td);
856 				return (EOPNOTSUPP);
857 			}
858 			fdrop(fp, td);
859 		}
860 		return (error);
861 	}
862 	if (addr)
863 		error = linux_sa_put(PTRIN(addr));
864 	if (error) {
865 		(void)kern_close(td, td->td_retval[0]);
866 		td->td_retval[0] = 0;
867 	}
868 	return (error);
869 }
870 
871 int
872 linux_accept(struct thread *td, struct linux_accept_args *args)
873 {
874 
875 	return (linux_accept_common(td, args->s, args->addr,
876 	    args->namelen, 0));
877 }
878 
879 int
880 linux_accept4(struct thread *td, struct linux_accept4_args *args)
881 {
882 
883 	return (linux_accept_common(td, args->s, args->addr,
884 	    args->namelen, args->flags));
885 }
886 
887 int
888 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
889 {
890 	struct getsockname_args /* {
891 		int	fdes;
892 		struct sockaddr * __restrict asa;
893 		socklen_t * __restrict alen;
894 	} */ bsd_args;
895 	int error;
896 
897 	bsd_args.fdes = args->s;
898 	/* XXX: */
899 	bsd_args.asa = (struct sockaddr * __restrict)PTRIN(args->addr);
900 	bsd_args.alen = PTRIN(args->namelen);	/* XXX */
901 	error = sys_getsockname(td, &bsd_args);
902 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
903 	if (error)
904 		return (error);
905 	return (linux_sa_put(PTRIN(args->addr)));
906 }
907 
908 int
909 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
910 {
911 	struct getpeername_args /* {
912 		int fdes;
913 		caddr_t asa;
914 		int *alen;
915 	} */ bsd_args;
916 	int error;
917 
918 	bsd_args.fdes = args->s;
919 	bsd_args.asa = (struct sockaddr *)PTRIN(args->addr);
920 	bsd_args.alen = (socklen_t *)PTRIN(args->namelen);
921 	error = sys_getpeername(td, &bsd_args);
922 	bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
923 	if (error)
924 		return (error);
925 	return (linux_sa_put(PTRIN(args->addr)));
926 }
927 
928 int
929 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
930 {
931 	struct socketpair_args /* {
932 		int domain;
933 		int type;
934 		int protocol;
935 		int *rsv;
936 	} */ bsd_args;
937 	int error;
938 
939 	bsd_args.domain = linux_to_bsd_domain(args->domain);
940 	if (bsd_args.domain != PF_LOCAL)
941 		return (EAFNOSUPPORT);
942 	bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
943 	if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
944 		return (EINVAL);
945 	error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
946 		&bsd_args.type);
947 	if (error != 0)
948 		return (error);
949 	if (args->protocol != 0 && args->protocol != PF_UNIX)
950 
951 		/*
952 		 * Use of PF_UNIX as protocol argument is not right,
953 		 * but Linux does it.
954 		 * Do not map PF_UNIX as its Linux value is identical
955 		 * to FreeBSD one.
956 		 */
957 		return (EPROTONOSUPPORT);
958 	else
959 		bsd_args.protocol = 0;
960 	bsd_args.rsv = (int *)PTRIN(args->rsv);
961 	return (sys_socketpair(td, &bsd_args));
962 }
963 
964 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
965 struct linux_send_args {
966 	int s;
967 	l_uintptr_t msg;
968 	int len;
969 	int flags;
970 };
971 
972 static int
973 linux_send(struct thread *td, struct linux_send_args *args)
974 {
975 	struct sendto_args /* {
976 		int s;
977 		caddr_t buf;
978 		int len;
979 		int flags;
980 		caddr_t to;
981 		int tolen;
982 	} */ bsd_args;
983 
984 	bsd_args.s = args->s;
985 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
986 	bsd_args.len = args->len;
987 	bsd_args.flags = args->flags;
988 	bsd_args.to = NULL;
989 	bsd_args.tolen = 0;
990 	return (sys_sendto(td, &bsd_args));
991 }
992 
993 struct linux_recv_args {
994 	int s;
995 	l_uintptr_t msg;
996 	int len;
997 	int flags;
998 };
999 
1000 static int
1001 linux_recv(struct thread *td, struct linux_recv_args *args)
1002 {
1003 	struct recvfrom_args /* {
1004 		int s;
1005 		caddr_t buf;
1006 		int len;
1007 		int flags;
1008 		struct sockaddr *from;
1009 		socklen_t fromlenaddr;
1010 	} */ bsd_args;
1011 
1012 	bsd_args.s = args->s;
1013 	bsd_args.buf = (caddr_t)PTRIN(args->msg);
1014 	bsd_args.len = args->len;
1015 	bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
1016 	bsd_args.from = NULL;
1017 	bsd_args.fromlenaddr = 0;
1018 	return (sys_recvfrom(td, &bsd_args));
1019 }
1020 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1021 
1022 int
1023 linux_sendto(struct thread *td, struct linux_sendto_args *args)
1024 {
1025 	struct msghdr msg;
1026 	struct iovec aiov;
1027 
1028 	if (linux_check_hdrincl(td, args->s) == 0)
1029 		/* IP_HDRINCL set, tweak the packet before sending */
1030 		return (linux_sendto_hdrincl(td, args));
1031 
1032 	msg.msg_name = PTRIN(args->to);
1033 	msg.msg_namelen = args->tolen;
1034 	msg.msg_iov = &aiov;
1035 	msg.msg_iovlen = 1;
1036 	msg.msg_control = NULL;
1037 	msg.msg_flags = 0;
1038 	aiov.iov_base = PTRIN(args->msg);
1039 	aiov.iov_len = args->len;
1040 	return (linux_sendit(td, args->s, &msg, args->flags, NULL,
1041 	    UIO_USERSPACE));
1042 }
1043 
1044 int
1045 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
1046 {
1047 	struct msghdr msg;
1048 	struct iovec aiov;
1049 	int error;
1050 
1051 	if (PTRIN(args->fromlen) != NULL) {
1052 		error = copyin(PTRIN(args->fromlen), &msg.msg_namelen,
1053 		    sizeof(msg.msg_namelen));
1054 		if (error != 0)
1055 			return (error);
1056 
1057 		error = linux_to_bsd_sockaddr((struct sockaddr *)PTRIN(args->from),
1058 		    msg.msg_namelen);
1059 		if (error != 0)
1060 			return (error);
1061 	} else
1062 		msg.msg_namelen = 0;
1063 
1064 	msg.msg_name = (struct sockaddr * __restrict)PTRIN(args->from);
1065 	msg.msg_iov = &aiov;
1066 	msg.msg_iovlen = 1;
1067 	aiov.iov_base = PTRIN(args->buf);
1068 	aiov.iov_len = args->len;
1069 	msg.msg_control = 0;
1070 	msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
1071 
1072 	error = kern_recvit(td, args->s, &msg, UIO_USERSPACE, NULL);
1073 	if (error != 0)
1074 		return (error);
1075 
1076 	if (PTRIN(args->from) != NULL) {
1077 		error = bsd_to_linux_sockaddr((struct sockaddr *)
1078 		    PTRIN(args->from));
1079 		if (error != 0)
1080 			return (error);
1081 
1082 		error = linux_sa_put((struct osockaddr *)
1083 		    PTRIN(args->from));
1084 	}
1085 
1086 	if (PTRIN(args->fromlen) != NULL)
1087 		error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
1088 		    sizeof(msg.msg_namelen));
1089 
1090 	return (error);
1091 }
1092 
1093 static int
1094 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1095     l_uint flags)
1096 {
1097 	struct cmsghdr *cmsg;
1098 	struct cmsgcred cmcred;
1099 	struct mbuf *control;
1100 	struct msghdr msg;
1101 	struct l_cmsghdr linux_cmsg;
1102 	struct l_cmsghdr *ptr_cmsg;
1103 	struct l_msghdr linux_msg;
1104 	struct iovec *iov;
1105 	socklen_t datalen;
1106 	struct sockaddr *sa;
1107 	sa_family_t sa_family;
1108 	void *data;
1109 	int error;
1110 
1111 	error = copyin(msghdr, &linux_msg, sizeof(linux_msg));
1112 	if (error != 0)
1113 		return (error);
1114 
1115 	/*
1116 	 * Some Linux applications (ping) define a non-NULL control data
1117 	 * pointer, but a msg_controllen of 0, which is not allowed in the
1118 	 * FreeBSD system call interface.  NULL the msg_control pointer in
1119 	 * order to handle this case.  This should be checked, but allows the
1120 	 * Linux ping to work.
1121 	 */
1122 	if (PTRIN(linux_msg.msg_control) != NULL && linux_msg.msg_controllen == 0)
1123 		linux_msg.msg_control = PTROUT(NULL);
1124 
1125 	error = linux_to_bsd_msghdr(&msg, &linux_msg);
1126 	if (error != 0)
1127 		return (error);
1128 
1129 #ifdef COMPAT_LINUX32
1130 	error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
1131 	    &iov, EMSGSIZE);
1132 #else
1133 	error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1134 #endif
1135 	if (error != 0)
1136 		return (error);
1137 
1138 	control = NULL;
1139 	cmsg = NULL;
1140 
1141 	if ((ptr_cmsg = LINUX_CMSG_FIRSTHDR(&linux_msg)) != NULL) {
1142 		error = kern_getsockname(td, s, &sa, &datalen);
1143 		if (error != 0)
1144 			goto bad;
1145 		sa_family = sa->sa_family;
1146 		free(sa, M_SONAME);
1147 
1148 		error = ENOBUFS;
1149 		cmsg = malloc(CMSG_HDRSZ, M_LINUX, M_WAITOK|M_ZERO);
1150 		control = m_get(M_WAITOK, MT_CONTROL);
1151 
1152 		do {
1153 			error = copyin(ptr_cmsg, &linux_cmsg,
1154 			    sizeof(struct l_cmsghdr));
1155 			if (error != 0)
1156 				goto bad;
1157 
1158 			error = EINVAL;
1159 			if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr))
1160 				goto bad;
1161 
1162 			/*
1163 			 * Now we support only SCM_RIGHTS and SCM_CRED,
1164 			 * so return EINVAL in any other cmsg_type
1165 			 */
1166 			cmsg->cmsg_type =
1167 			    linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1168 			cmsg->cmsg_level =
1169 			    linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1170 			if (cmsg->cmsg_type == -1
1171 			    || cmsg->cmsg_level != SOL_SOCKET)
1172 				goto bad;
1173 
1174 			/*
1175 			 * Some applications (e.g. pulseaudio) attempt to
1176 			 * send ancillary data even if the underlying protocol
1177 			 * doesn't support it which is not allowed in the
1178 			 * FreeBSD system call interface.
1179 			 */
1180 			if (sa_family != AF_UNIX)
1181 				continue;
1182 
1183 			data = LINUX_CMSG_DATA(ptr_cmsg);
1184 			datalen = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1185 
1186 			switch (cmsg->cmsg_type)
1187 			{
1188 			case SCM_RIGHTS:
1189 				break;
1190 
1191 			case SCM_CREDS:
1192 				data = &cmcred;
1193 				datalen = sizeof(cmcred);
1194 
1195 				/*
1196 				 * The lower levels will fill in the structure
1197 				 */
1198 				bzero(data, datalen);
1199 				break;
1200 			}
1201 
1202 			cmsg->cmsg_len = CMSG_LEN(datalen);
1203 
1204 			error = ENOBUFS;
1205 			if (!m_append(control, CMSG_HDRSZ, (c_caddr_t)cmsg))
1206 				goto bad;
1207 			if (!m_append(control, datalen, (c_caddr_t)data))
1208 				goto bad;
1209 		} while ((ptr_cmsg = LINUX_CMSG_NXTHDR(&linux_msg, ptr_cmsg)));
1210 
1211 		if (m_length(control, NULL) == 0) {
1212 			m_freem(control);
1213 			control = NULL;
1214 		}
1215 	}
1216 
1217 	msg.msg_iov = iov;
1218 	msg.msg_flags = 0;
1219 	error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1220 	control = NULL;
1221 
1222 bad:
1223 	m_freem(control);
1224 	free(iov, M_IOV);
1225 	if (cmsg)
1226 		free(cmsg, M_LINUX);
1227 	return (error);
1228 }
1229 
1230 int
1231 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1232 {
1233 
1234 	return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1235 	    args->flags));
1236 }
1237 
1238 int
1239 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1240 {
1241 	struct l_mmsghdr *msg;
1242 	l_uint retval;
1243 	int error, datagrams;
1244 
1245 	if (args->vlen > UIO_MAXIOV)
1246 		args->vlen = UIO_MAXIOV;
1247 
1248 	msg = PTRIN(args->msg);
1249 	datagrams = 0;
1250 	while (datagrams < args->vlen) {
1251 		error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1252 		    args->flags);
1253 		if (error != 0)
1254 			break;
1255 
1256 		retval = td->td_retval[0];
1257 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1258 		if (error != 0)
1259 			break;
1260 		++msg;
1261 		++datagrams;
1262 	}
1263 	if (error == 0)
1264 		td->td_retval[0] = datagrams;
1265 	return (error);
1266 }
1267 
1268 static int
1269 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1270     l_uint flags, struct msghdr *msg)
1271 {
1272 	struct cmsghdr *cm;
1273 	struct cmsgcred *cmcred;
1274 	struct l_cmsghdr *linux_cmsg = NULL;
1275 	struct l_ucred linux_ucred;
1276 	socklen_t datalen, outlen;
1277 	struct l_msghdr linux_msg;
1278 	struct iovec *iov, *uiov;
1279 	struct mbuf *control = NULL;
1280 	struct mbuf **controlp;
1281 	struct timeval *ftmvl;
1282 	l_timeval ltmvl;
1283 	caddr_t outbuf;
1284 	void *data;
1285 	int error, i, fd, fds, *fdp;
1286 
1287 	error = copyin(msghdr, &linux_msg, sizeof(linux_msg));
1288 	if (error != 0)
1289 		return (error);
1290 
1291 	error = linux_to_bsd_msghdr(msg, &linux_msg);
1292 	if (error != 0)
1293 		return (error);
1294 
1295 #ifdef COMPAT_LINUX32
1296 	error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1297 	    &iov, EMSGSIZE);
1298 #else
1299 	error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1300 #endif
1301 	if (error != 0)
1302 		return (error);
1303 
1304 	if (msg->msg_name) {
1305 		error = linux_to_bsd_sockaddr((struct sockaddr *)msg->msg_name,
1306 		    msg->msg_namelen);
1307 		if (error != 0)
1308 			goto bad;
1309 	}
1310 
1311 	uiov = msg->msg_iov;
1312 	msg->msg_iov = iov;
1313 	controlp = (msg->msg_control != NULL) ? &control : NULL;
1314 	error = kern_recvit(td, s, msg, UIO_USERSPACE, controlp);
1315 	msg->msg_iov = uiov;
1316 	if (error != 0)
1317 		goto bad;
1318 
1319 	error = bsd_to_linux_msghdr(msg, &linux_msg);
1320 	if (error != 0)
1321 		goto bad;
1322 
1323 	if (linux_msg.msg_name) {
1324 		error = bsd_to_linux_sockaddr((struct sockaddr *)
1325 		    PTRIN(linux_msg.msg_name));
1326 		if (error != 0)
1327 			goto bad;
1328 	}
1329 	if (linux_msg.msg_name && linux_msg.msg_namelen > 2) {
1330 		error = linux_sa_put(PTRIN(linux_msg.msg_name));
1331 		if (error != 0)
1332 			goto bad;
1333 	}
1334 
1335 	outbuf = PTRIN(linux_msg.msg_control);
1336 	outlen = 0;
1337 
1338 	if (control) {
1339 		linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1340 
1341 		msg->msg_control = mtod(control, struct cmsghdr *);
1342 		msg->msg_controllen = control->m_len;
1343 
1344 		cm = CMSG_FIRSTHDR(msg);
1345 
1346 		while (cm != NULL) {
1347 			linux_cmsg->cmsg_type =
1348 			    bsd_to_linux_cmsg_type(cm->cmsg_type);
1349 			linux_cmsg->cmsg_level =
1350 			    bsd_to_linux_sockopt_level(cm->cmsg_level);
1351 			if (linux_cmsg->cmsg_type == -1
1352 			    || cm->cmsg_level != SOL_SOCKET)
1353 			{
1354 				error = EINVAL;
1355 				goto bad;
1356 			}
1357 
1358 			data = CMSG_DATA(cm);
1359 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1360 
1361 			switch (cm->cmsg_type)
1362 			{
1363 			case SCM_RIGHTS:
1364 				if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1365 					fds = datalen / sizeof(int);
1366 					fdp = data;
1367 					for (i = 0; i < fds; i++) {
1368 						fd = *fdp++;
1369 						(void)kern_fcntl(td, fd,
1370 						    F_SETFD, FD_CLOEXEC);
1371 					}
1372 				}
1373 				break;
1374 
1375 			case SCM_CREDS:
1376 				/*
1377 				 * Currently LOCAL_CREDS is never in
1378 				 * effect for Linux so no need to worry
1379 				 * about sockcred
1380 				 */
1381 				if (datalen != sizeof(*cmcred)) {
1382 					error = EMSGSIZE;
1383 					goto bad;
1384 				}
1385 				cmcred = (struct cmsgcred *)data;
1386 				bzero(&linux_ucred, sizeof(linux_ucred));
1387 				linux_ucred.pid = cmcred->cmcred_pid;
1388 				linux_ucred.uid = cmcred->cmcred_uid;
1389 				linux_ucred.gid = cmcred->cmcred_gid;
1390 				data = &linux_ucred;
1391 				datalen = sizeof(linux_ucred);
1392 				break;
1393 
1394 			case SCM_TIMESTAMP:
1395 				if (datalen != sizeof(struct timeval)) {
1396 					error = EMSGSIZE;
1397 					goto bad;
1398 				}
1399 				ftmvl = (struct timeval *)data;
1400 				ltmvl.tv_sec = ftmvl->tv_sec;
1401 				ltmvl.tv_usec = ftmvl->tv_usec;
1402 				data = &ltmvl;
1403 				datalen = sizeof(ltmvl);
1404 				break;
1405 			}
1406 
1407 			if (outlen + LINUX_CMSG_LEN(datalen) >
1408 			    linux_msg.msg_controllen) {
1409 				if (outlen == 0) {
1410 					error = EMSGSIZE;
1411 					goto bad;
1412 				} else {
1413 					linux_msg.msg_flags |=
1414 					    LINUX_MSG_CTRUNC;
1415 					goto out;
1416 				}
1417 			}
1418 
1419 			linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1420 
1421 			error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1422 			if (error)
1423 				goto bad;
1424 			outbuf += L_CMSG_HDRSZ;
1425 
1426 			error = copyout(data, outbuf, datalen);
1427 			if (error)
1428 				goto bad;
1429 
1430 			outbuf += LINUX_CMSG_ALIGN(datalen);
1431 			outlen += LINUX_CMSG_LEN(datalen);
1432 
1433 			cm = CMSG_NXTHDR(msg, cm);
1434 		}
1435 	}
1436 
1437 out:
1438 	linux_msg.msg_controllen = outlen;
1439 	error = copyout(&linux_msg, msghdr, sizeof(linux_msg));
1440 
1441 bad:
1442 	free(iov, M_IOV);
1443 	m_freem(control);
1444 	free(linux_cmsg, M_LINUX);
1445 
1446 	return (error);
1447 }
1448 
1449 int
1450 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1451 {
1452 	struct msghdr bsd_msg;
1453 
1454 	return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1455 	    args->flags, &bsd_msg));
1456 }
1457 
1458 int
1459 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1460 {
1461 	struct l_mmsghdr *msg;
1462 	struct msghdr bsd_msg;
1463 	struct l_timespec lts;
1464 	struct timespec ts, tts;
1465 	l_uint retval;
1466 	int error, datagrams;
1467 
1468 	if (args->timeout) {
1469 		error = copyin(args->timeout, &lts, sizeof(struct l_timespec));
1470 		if (error != 0)
1471 			return (error);
1472 		error = linux_to_native_timespec(&ts, &lts);
1473 		if (error != 0)
1474 			return (error);
1475 		getnanotime(&tts);
1476 		timespecadd(&tts, &ts);
1477 	}
1478 
1479 	msg = PTRIN(args->msg);
1480 	datagrams = 0;
1481 	while (datagrams < args->vlen) {
1482 		error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1483 		    args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1484 		if (error != 0)
1485 			break;
1486 
1487 		retval = td->td_retval[0];
1488 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1489 		if (error != 0)
1490 			break;
1491 		++msg;
1492 		++datagrams;
1493 
1494 		/*
1495 		 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1496 		 */
1497 		if (args->flags & LINUX_MSG_WAITFORONE)
1498 			args->flags |= LINUX_MSG_DONTWAIT;
1499 
1500 		/*
1501 		 * See BUGS section of recvmmsg(2).
1502 		 */
1503 		if (args->timeout) {
1504 			getnanotime(&ts);
1505 			timespecsub(&ts, &tts);
1506 			if (!timespecisset(&ts) || ts.tv_sec > 0)
1507 				break;
1508 		}
1509 		/* Out of band data, return right away. */
1510 		if (bsd_msg.msg_flags & MSG_OOB)
1511 			break;
1512 	}
1513 	if (error == 0)
1514 		td->td_retval[0] = datagrams;
1515 	return (error);
1516 }
1517 
1518 int
1519 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1520 {
1521 	struct shutdown_args /* {
1522 		int s;
1523 		int how;
1524 	} */ bsd_args;
1525 
1526 	bsd_args.s = args->s;
1527 	bsd_args.how = args->how;
1528 	return (sys_shutdown(td, &bsd_args));
1529 }
1530 
1531 int
1532 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1533 {
1534 	struct setsockopt_args /* {
1535 		int s;
1536 		int level;
1537 		int name;
1538 		caddr_t val;
1539 		int valsize;
1540 	} */ bsd_args;
1541 	l_timeval linux_tv;
1542 	struct timeval tv;
1543 	int error, name;
1544 
1545 	bsd_args.s = args->s;
1546 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1547 	switch (bsd_args.level) {
1548 	case SOL_SOCKET:
1549 		name = linux_to_bsd_so_sockopt(args->optname);
1550 		switch (name) {
1551 		case SO_RCVTIMEO:
1552 			/* FALLTHROUGH */
1553 		case SO_SNDTIMEO:
1554 			error = copyin(PTRIN(args->optval), &linux_tv,
1555 			    sizeof(linux_tv));
1556 			if (error)
1557 				return (error);
1558 			tv.tv_sec = linux_tv.tv_sec;
1559 			tv.tv_usec = linux_tv.tv_usec;
1560 			return (kern_setsockopt(td, args->s, bsd_args.level,
1561 			    name, &tv, UIO_SYSSPACE, sizeof(tv)));
1562 			/* NOTREACHED */
1563 			break;
1564 		default:
1565 			break;
1566 		}
1567 		break;
1568 	case IPPROTO_IP:
1569 		name = linux_to_bsd_ip_sockopt(args->optname);
1570 		break;
1571 	case IPPROTO_IPV6:
1572 		name = linux_to_bsd_ip6_sockopt(args->optname);
1573 		break;
1574 	case IPPROTO_TCP:
1575 		name = linux_to_bsd_tcp_sockopt(args->optname);
1576 		break;
1577 	default:
1578 		name = -1;
1579 		break;
1580 	}
1581 	if (name == -1)
1582 		return (ENOPROTOOPT);
1583 
1584 	bsd_args.name = name;
1585 	bsd_args.val = PTRIN(args->optval);
1586 	bsd_args.valsize = args->optlen;
1587 
1588 	if (name == IPV6_NEXTHOP) {
1589 		linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
1590 			bsd_args.valsize);
1591 		error = sys_setsockopt(td, &bsd_args);
1592 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
1593 	} else
1594 		error = sys_setsockopt(td, &bsd_args);
1595 
1596 	return (error);
1597 }
1598 
1599 int
1600 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1601 {
1602 	struct getsockopt_args /* {
1603 		int s;
1604 		int level;
1605 		int name;
1606 		caddr_t val;
1607 		int *avalsize;
1608 	} */ bsd_args;
1609 	l_timeval linux_tv;
1610 	struct timeval tv;
1611 	socklen_t tv_len, xulen;
1612 	struct xucred xu;
1613 	struct l_ucred lxu;
1614 	int error, name;
1615 
1616 	bsd_args.s = args->s;
1617 	bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1618 	switch (bsd_args.level) {
1619 	case SOL_SOCKET:
1620 		name = linux_to_bsd_so_sockopt(args->optname);
1621 		switch (name) {
1622 		case SO_RCVTIMEO:
1623 			/* FALLTHROUGH */
1624 		case SO_SNDTIMEO:
1625 			tv_len = sizeof(tv);
1626 			error = kern_getsockopt(td, args->s, bsd_args.level,
1627 			    name, &tv, UIO_SYSSPACE, &tv_len);
1628 			if (error)
1629 				return (error);
1630 			linux_tv.tv_sec = tv.tv_sec;
1631 			linux_tv.tv_usec = tv.tv_usec;
1632 			return (copyout(&linux_tv, PTRIN(args->optval),
1633 			    sizeof(linux_tv)));
1634 			/* NOTREACHED */
1635 			break;
1636 		case LOCAL_PEERCRED:
1637 			if (args->optlen != sizeof(lxu))
1638 				return (EINVAL);
1639 			xulen = sizeof(xu);
1640 			error = kern_getsockopt(td, args->s, bsd_args.level,
1641 			    name, &xu, UIO_SYSSPACE, &xulen);
1642 			if (error)
1643 				return (error);
1644 			/*
1645 			 * XXX Use 0 for pid as the FreeBSD does not cache peer pid.
1646 			 */
1647 			lxu.pid = 0;
1648 			lxu.uid = xu.cr_uid;
1649 			lxu.gid = xu.cr_gid;
1650 			return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1651 			/* NOTREACHED */
1652 			break;
1653 		default:
1654 			break;
1655 		}
1656 		break;
1657 	case IPPROTO_IP:
1658 		name = linux_to_bsd_ip_sockopt(args->optname);
1659 		break;
1660 	case IPPROTO_IPV6:
1661 		name = linux_to_bsd_ip6_sockopt(args->optname);
1662 		break;
1663 	case IPPROTO_TCP:
1664 		name = linux_to_bsd_tcp_sockopt(args->optname);
1665 		break;
1666 	default:
1667 		name = -1;
1668 		break;
1669 	}
1670 	if (name == -1)
1671 		return (EINVAL);
1672 
1673 	bsd_args.name = name;
1674 	bsd_args.val = PTRIN(args->optval);
1675 	bsd_args.avalsize = PTRIN(args->optlen);
1676 
1677 	if (name == IPV6_NEXTHOP) {
1678 		error = sys_getsockopt(td, &bsd_args);
1679 		bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
1680 	} else
1681 		error = sys_getsockopt(td, &bsd_args);
1682 
1683 	return (error);
1684 }
1685 
1686 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1687 
1688 /* Argument list sizes for linux_socketcall */
1689 
1690 #define LINUX_AL(x) ((x) * sizeof(l_ulong))
1691 
1692 static const unsigned char lxs_args[] = {
1693 	LINUX_AL(0) /* unused*/,	LINUX_AL(3) /* socket */,
1694 	LINUX_AL(3) /* bind */,		LINUX_AL(3) /* connect */,
1695 	LINUX_AL(2) /* listen */,	LINUX_AL(3) /* accept */,
1696 	LINUX_AL(3) /* getsockname */,	LINUX_AL(3) /* getpeername */,
1697 	LINUX_AL(4) /* socketpair */,	LINUX_AL(4) /* send */,
1698 	LINUX_AL(4) /* recv */,		LINUX_AL(6) /* sendto */,
1699 	LINUX_AL(6) /* recvfrom */,	LINUX_AL(2) /* shutdown */,
1700 	LINUX_AL(5) /* setsockopt */,	LINUX_AL(5) /* getsockopt */,
1701 	LINUX_AL(3) /* sendmsg */,	LINUX_AL(3) /* recvmsg */,
1702 	LINUX_AL(4) /* accept4 */,	LINUX_AL(5) /* recvmmsg */,
1703 	LINUX_AL(4) /* sendmmsg */
1704 };
1705 
1706 #define	LINUX_AL_SIZE	sizeof(lxs_args) / sizeof(lxs_args[0]) - 1
1707 
1708 int
1709 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1710 {
1711 	l_ulong a[6];
1712 	void *arg;
1713 	int error;
1714 
1715 	if (args->what < LINUX_SOCKET || args->what > LINUX_AL_SIZE)
1716 		return (EINVAL);
1717 	error = copyin(PTRIN(args->args), a, lxs_args[args->what]);
1718 	if (error)
1719 		return (error);
1720 
1721 	arg = a;
1722 	switch (args->what) {
1723 	case LINUX_SOCKET:
1724 		return (linux_socket(td, arg));
1725 	case LINUX_BIND:
1726 		return (linux_bind(td, arg));
1727 	case LINUX_CONNECT:
1728 		return (linux_connect(td, arg));
1729 	case LINUX_LISTEN:
1730 		return (linux_listen(td, arg));
1731 	case LINUX_ACCEPT:
1732 		return (linux_accept(td, arg));
1733 	case LINUX_GETSOCKNAME:
1734 		return (linux_getsockname(td, arg));
1735 	case LINUX_GETPEERNAME:
1736 		return (linux_getpeername(td, arg));
1737 	case LINUX_SOCKETPAIR:
1738 		return (linux_socketpair(td, arg));
1739 	case LINUX_SEND:
1740 		return (linux_send(td, arg));
1741 	case LINUX_RECV:
1742 		return (linux_recv(td, arg));
1743 	case LINUX_SENDTO:
1744 		return (linux_sendto(td, arg));
1745 	case LINUX_RECVFROM:
1746 		return (linux_recvfrom(td, arg));
1747 	case LINUX_SHUTDOWN:
1748 		return (linux_shutdown(td, arg));
1749 	case LINUX_SETSOCKOPT:
1750 		return (linux_setsockopt(td, arg));
1751 	case LINUX_GETSOCKOPT:
1752 		return (linux_getsockopt(td, arg));
1753 	case LINUX_SENDMSG:
1754 		return (linux_sendmsg(td, arg));
1755 	case LINUX_RECVMSG:
1756 		return (linux_recvmsg(td, arg));
1757 	case LINUX_ACCEPT4:
1758 		return (linux_accept4(td, arg));
1759 	case LINUX_RECVMMSG:
1760 		return (linux_recvmmsg(td, arg));
1761 	case LINUX_SENDMMSG:
1762 		return (linux_sendmmsg(td, arg));
1763 	}
1764 
1765 	uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
1766 	return (ENOSYS);
1767 }
1768 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1769