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