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