xref: /freebsd/sys/compat/linux/linux_socket.c (revision b0ee263dbd3552d5b1776be0efc1c2d105f873b1)
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) {
1200 		sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1201 		msg->msg_name = sa;
1202 	} else
1203 		sa = NULL;
1204 
1205 	uiov = msg->msg_iov;
1206 	msg->msg_iov = iov;
1207 	controlp = (msg->msg_control != NULL) ? &control : NULL;
1208 	error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1209 	msg->msg_iov = uiov;
1210 	if (error != 0)
1211 		goto bad;
1212 
1213 	if (msg->msg_name) {
1214 		msg->msg_name = PTRIN(linux_msghdr.msg_name);
1215 		error = bsd_to_linux_sockaddr(sa, &lsa, msg->msg_namelen);
1216 		if (error == 0)
1217 			error = copyout(lsa, PTRIN(msg->msg_name),
1218 			    msg->msg_namelen);
1219 		free(lsa, M_SONAME);
1220 		if (error != 0)
1221 			goto bad;
1222 	}
1223 
1224 	error = bsd_to_linux_msghdr(msg, &linux_msghdr);
1225 	if (error != 0)
1226 		goto bad;
1227 
1228 	maxlen = linux_msghdr.msg_controllen;
1229 	linux_msghdr.msg_controllen = 0;
1230 	if (control) {
1231 		linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1232 
1233 		msg->msg_control = mtod(control, struct cmsghdr *);
1234 		msg->msg_controllen = control->m_len;
1235 
1236 		cm = CMSG_FIRSTHDR(msg);
1237 		outbuf = PTRIN(linux_msghdr.msg_control);
1238 		outlen = 0;
1239 		while (cm != NULL) {
1240 			linux_cmsg->cmsg_type =
1241 			    bsd_to_linux_cmsg_type(cm->cmsg_type);
1242 			linux_cmsg->cmsg_level =
1243 			    bsd_to_linux_sockopt_level(cm->cmsg_level);
1244 			if (linux_cmsg->cmsg_type == -1 ||
1245 			    cm->cmsg_level != SOL_SOCKET) {
1246 				linux_msg(curthread,
1247 				    "unsupported recvmsg cmsg level %d type %d",
1248 				    cm->cmsg_level, cm->cmsg_type);
1249 				error = EINVAL;
1250 				goto bad;
1251 			}
1252 
1253 			data = CMSG_DATA(cm);
1254 			datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1255 
1256 			switch (cm->cmsg_type) {
1257 			case SCM_RIGHTS:
1258 				if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1259 					fds = datalen / sizeof(int);
1260 					fdp = data;
1261 					for (i = 0; i < fds; i++) {
1262 						fd = *fdp++;
1263 						(void)kern_fcntl(td, fd,
1264 						    F_SETFD, FD_CLOEXEC);
1265 					}
1266 				}
1267 				break;
1268 
1269 			case SCM_CREDS:
1270 				/*
1271 				 * Currently LOCAL_CREDS is never in
1272 				 * effect for Linux so no need to worry
1273 				 * about sockcred
1274 				 */
1275 				if (datalen != sizeof(*cmcred)) {
1276 					error = EMSGSIZE;
1277 					goto bad;
1278 				}
1279 				cmcred = (struct cmsgcred *)data;
1280 				bzero(&linux_ucred, sizeof(linux_ucred));
1281 				linux_ucred.pid = cmcred->cmcred_pid;
1282 				linux_ucred.uid = cmcred->cmcred_uid;
1283 				linux_ucred.gid = cmcred->cmcred_gid;
1284 				data = &linux_ucred;
1285 				datalen = sizeof(linux_ucred);
1286 				break;
1287 
1288 			case SCM_TIMESTAMP:
1289 				if (datalen != sizeof(struct timeval)) {
1290 					error = EMSGSIZE;
1291 					goto bad;
1292 				}
1293 				ftmvl = (struct timeval *)data;
1294 				ltmvl.tv_sec = ftmvl->tv_sec;
1295 				ltmvl.tv_usec = ftmvl->tv_usec;
1296 				data = &ltmvl;
1297 				datalen = sizeof(ltmvl);
1298 				break;
1299 			}
1300 
1301 			if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1302 				if (outlen == 0) {
1303 					error = EMSGSIZE;
1304 					goto bad;
1305 				} else {
1306 					linux_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1307 					m_dispose_extcontrolm(control);
1308 					goto out;
1309 				}
1310 			}
1311 
1312 			linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1313 
1314 			error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1315 			if (error != 0)
1316 				goto bad;
1317 			outbuf += L_CMSG_HDRSZ;
1318 
1319 			error = copyout(data, outbuf, datalen);
1320 			if (error != 0)
1321 				goto bad;
1322 
1323 			outbuf += LINUX_CMSG_ALIGN(datalen);
1324 			outlen += LINUX_CMSG_LEN(datalen);
1325 
1326 			cm = CMSG_NXTHDR(msg, cm);
1327 		}
1328 		linux_msghdr.msg_controllen = outlen;
1329 	}
1330 
1331 out:
1332 	error = copyout(&linux_msghdr, msghdr, sizeof(linux_msghdr));
1333 
1334 bad:
1335 	if (control != NULL) {
1336 		if (error != 0)
1337 			m_dispose_extcontrolm(control);
1338 		m_freem(control);
1339 	}
1340 	free(iov, M_IOV);
1341 	free(linux_cmsg, M_LINUX);
1342 	free(sa, M_SONAME);
1343 
1344 	return (error);
1345 }
1346 
1347 int
1348 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1349 {
1350 	struct msghdr bsd_msg;
1351 
1352 	return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1353 	    args->flags, &bsd_msg));
1354 }
1355 
1356 int
1357 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1358 {
1359 	struct l_mmsghdr *msg;
1360 	struct msghdr bsd_msg;
1361 	struct l_timespec lts;
1362 	struct timespec ts, tts;
1363 	l_uint retval;
1364 	int error, datagrams;
1365 
1366 	if (args->timeout) {
1367 		error = copyin(args->timeout, &lts, sizeof(struct l_timespec));
1368 		if (error != 0)
1369 			return (error);
1370 		error = linux_to_native_timespec(&ts, &lts);
1371 		if (error != 0)
1372 			return (error);
1373 		getnanotime(&tts);
1374 		timespecadd(&tts, &ts, &tts);
1375 	}
1376 
1377 	msg = PTRIN(args->msg);
1378 	datagrams = 0;
1379 	while (datagrams < args->vlen) {
1380 		error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1381 		    args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1382 		if (error != 0)
1383 			break;
1384 
1385 		retval = td->td_retval[0];
1386 		error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1387 		if (error != 0)
1388 			break;
1389 		++msg;
1390 		++datagrams;
1391 
1392 		/*
1393 		 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1394 		 */
1395 		if (args->flags & LINUX_MSG_WAITFORONE)
1396 			args->flags |= LINUX_MSG_DONTWAIT;
1397 
1398 		/*
1399 		 * See BUGS section of recvmmsg(2).
1400 		 */
1401 		if (args->timeout) {
1402 			getnanotime(&ts);
1403 			timespecsub(&ts, &tts, &ts);
1404 			if (!timespecisset(&ts) || ts.tv_sec > 0)
1405 				break;
1406 		}
1407 		/* Out of band data, return right away. */
1408 		if (bsd_msg.msg_flags & MSG_OOB)
1409 			break;
1410 	}
1411 	if (error == 0)
1412 		td->td_retval[0] = datagrams;
1413 	return (error);
1414 }
1415 
1416 int
1417 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1418 {
1419 
1420 	return (kern_shutdown(td, args->s, args->how));
1421 }
1422 
1423 int
1424 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1425 {
1426 	l_timeval linux_tv;
1427 	struct sockaddr *sa;
1428 	struct timeval tv;
1429 	socklen_t len;
1430 	int error, level, name;
1431 
1432 	level = linux_to_bsd_sockopt_level(args->level);
1433 	switch (level) {
1434 	case SOL_SOCKET:
1435 		name = linux_to_bsd_so_sockopt(args->optname);
1436 		switch (name) {
1437 		case SO_RCVTIMEO:
1438 			/* FALLTHROUGH */
1439 		case SO_SNDTIMEO:
1440 			error = copyin(PTRIN(args->optval), &linux_tv,
1441 			    sizeof(linux_tv));
1442 			if (error != 0)
1443 				return (error);
1444 			tv.tv_sec = linux_tv.tv_sec;
1445 			tv.tv_usec = linux_tv.tv_usec;
1446 			return (kern_setsockopt(td, args->s, level,
1447 			    name, &tv, UIO_SYSSPACE, sizeof(tv)));
1448 			/* NOTREACHED */
1449 		default:
1450 			break;
1451 		}
1452 		break;
1453 	case IPPROTO_IP:
1454 		if (args->optname == LINUX_IP_RECVERR &&
1455 		    linux_ignore_ip_recverr) {
1456 			/*
1457 			 * XXX: This is a hack to unbreak DNS resolution
1458 			 *	with glibc 2.30 and above.
1459 			 */
1460 			return (0);
1461 		}
1462 		name = linux_to_bsd_ip_sockopt(args->optname);
1463 		break;
1464 	case IPPROTO_IPV6:
1465 		name = linux_to_bsd_ip6_sockopt(args->optname);
1466 		break;
1467 	case IPPROTO_TCP:
1468 		name = linux_to_bsd_tcp_sockopt(args->optname);
1469 		break;
1470 	default:
1471 		name = -1;
1472 		break;
1473 	}
1474 	if (name == -1) {
1475 		linux_msg(curthread,
1476 		    "unsupported setsockopt level %d optname %d",
1477 		    args->level, args->optname);
1478 		return (ENOPROTOOPT);
1479 	}
1480 
1481 	if (name == IPV6_NEXTHOP) {
1482 		len = args->optlen;
1483 		error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
1484 		if (error != 0)
1485 			return (error);
1486 
1487 		error = kern_setsockopt(td, args->s, level,
1488 		    name, sa, UIO_SYSSPACE, len);
1489 		free(sa, M_SONAME);
1490 	} else {
1491 		error = kern_setsockopt(td, args->s, level,
1492 		    name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
1493 	}
1494 
1495 	return (error);
1496 }
1497 
1498 int
1499 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1500 {
1501 	l_timeval linux_tv;
1502 	struct timeval tv;
1503 	socklen_t tv_len, xulen, len;
1504 	struct l_sockaddr *lsa;
1505 	struct sockaddr *sa;
1506 	struct xucred xu;
1507 	struct l_ucred lxu;
1508 	int error, level, name, newval;
1509 
1510 	level = linux_to_bsd_sockopt_level(args->level);
1511 	switch (level) {
1512 	case SOL_SOCKET:
1513 		name = linux_to_bsd_so_sockopt(args->optname);
1514 		switch (name) {
1515 		case SO_RCVTIMEO:
1516 			/* FALLTHROUGH */
1517 		case SO_SNDTIMEO:
1518 			tv_len = sizeof(tv);
1519 			error = kern_getsockopt(td, args->s, level,
1520 			    name, &tv, UIO_SYSSPACE, &tv_len);
1521 			if (error != 0)
1522 				return (error);
1523 			linux_tv.tv_sec = tv.tv_sec;
1524 			linux_tv.tv_usec = tv.tv_usec;
1525 			return (copyout(&linux_tv, PTRIN(args->optval),
1526 			    sizeof(linux_tv)));
1527 			/* NOTREACHED */
1528 		case LOCAL_PEERCRED:
1529 			if (args->optlen < sizeof(lxu))
1530 				return (EINVAL);
1531 			/*
1532 			 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
1533 			 * but by the Unix socket's level 0.
1534 			 */
1535 			level = 0;
1536 			xulen = sizeof(xu);
1537 			error = kern_getsockopt(td, args->s, level,
1538 			    name, &xu, UIO_SYSSPACE, &xulen);
1539 			if (error != 0)
1540 				return (error);
1541 			lxu.pid = xu.cr_pid;
1542 			lxu.uid = xu.cr_uid;
1543 			lxu.gid = xu.cr_gid;
1544 			return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1545 			/* NOTREACHED */
1546 		case SO_ERROR:
1547 			len = sizeof(newval);
1548 			error = kern_getsockopt(td, args->s, level,
1549 			    name, &newval, UIO_SYSSPACE, &len);
1550 			if (error != 0)
1551 				return (error);
1552 			newval = -SV_ABI_ERRNO(td->td_proc, newval);
1553 			return (copyout(&newval, PTRIN(args->optval), len));
1554 			/* NOTREACHED */
1555 		default:
1556 			break;
1557 		}
1558 		break;
1559 	case IPPROTO_IP:
1560 		name = linux_to_bsd_ip_sockopt(args->optname);
1561 		break;
1562 	case IPPROTO_IPV6:
1563 		name = linux_to_bsd_ip6_sockopt(args->optname);
1564 		break;
1565 	case IPPROTO_TCP:
1566 		name = linux_to_bsd_tcp_sockopt(args->optname);
1567 		break;
1568 	default:
1569 		name = -1;
1570 		break;
1571 	}
1572 	if (name == -1) {
1573 		linux_msg(curthread,
1574 		    "unsupported getsockopt level %d optname %d",
1575 		    args->level, args->optname);
1576 		return (EINVAL);
1577 	}
1578 
1579 	if (name == IPV6_NEXTHOP) {
1580 		error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1581                 if (error != 0)
1582                         return (error);
1583 		sa = malloc(len, M_SONAME, M_WAITOK);
1584 
1585 		error = kern_getsockopt(td, args->s, level,
1586 		    name, sa, UIO_SYSSPACE, &len);
1587 		if (error != 0)
1588 			goto out;
1589 
1590 		error = bsd_to_linux_sockaddr(sa, &lsa, len);
1591 		if (error == 0)
1592 			error = copyout(lsa, PTRIN(args->optval), len);
1593 		free(lsa, M_SONAME);
1594 		if (error == 0)
1595 			error = copyout(&len, PTRIN(args->optlen),
1596 			    sizeof(len));
1597 out:
1598 		free(sa, M_SONAME);
1599 	} else {
1600 		if (args->optval) {
1601 			error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1602 			if (error != 0)
1603 				return (error);
1604 		}
1605 		error = kern_getsockopt(td, args->s, level,
1606 		    name, PTRIN(args->optval), UIO_USERSPACE, &len);
1607 		if (error == 0)
1608 			error = copyout(&len, PTRIN(args->optlen),
1609 			    sizeof(len));
1610 	}
1611 
1612 	return (error);
1613 }
1614 
1615 static int
1616 linux_sendfile_common(struct thread *td, l_int out, l_int in,
1617     l_loff_t *offset, l_size_t count)
1618 {
1619 	off_t bytes_read;
1620 	int error;
1621 	l_loff_t current_offset;
1622 	struct file *fp;
1623 
1624 	AUDIT_ARG_FD(in);
1625 	error = fget_read(td, in, &cap_pread_rights, &fp);
1626 	if (error != 0)
1627 		return (error);
1628 
1629 	if (offset != NULL) {
1630 		current_offset = *offset;
1631 	} else {
1632 		error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1633 		    fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
1634 		if (error != 0)
1635 			goto drop;
1636 		current_offset = td->td_uretoff.tdu_off;
1637 	}
1638 
1639 	bytes_read = 0;
1640 
1641 	/* Linux cannot have 0 count. */
1642 	if (count <= 0 || current_offset < 0) {
1643 		error = EINVAL;
1644 		goto drop;
1645 	}
1646 
1647 	error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
1648 	    &bytes_read, 0, td);
1649 	if (error != 0)
1650 		goto drop;
1651 	current_offset += bytes_read;
1652 
1653 	if (offset != NULL) {
1654 		*offset = current_offset;
1655 	} else {
1656 		error = fo_seek(fp, current_offset, SEEK_SET, td);
1657 		if (error != 0)
1658 			goto drop;
1659 	}
1660 
1661 	td->td_retval[0] = (ssize_t)bytes_read;
1662 drop:
1663 	fdrop(fp, td);
1664 	return (error);
1665 }
1666 
1667 int
1668 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
1669 {
1670 	/*
1671 	 * Differences between FreeBSD and Linux sendfile:
1672 	 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
1673 	 *   mean send the whole file.)  In linux_sendfile given fds are still
1674 	 *   checked for validity when the count is 0.
1675 	 * - Linux can send to any fd whereas FreeBSD only supports sockets.
1676 	 *   The same restriction follows for linux_sendfile.
1677 	 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
1678 	 * - Linux takes an offset pointer and updates it to the read location.
1679 	 *   FreeBSD takes in an offset and a 'bytes read' parameter which is
1680 	 *   only filled if it isn't NULL.  We use this parameter to update the
1681 	 *   offset pointer if it exists.
1682 	 * - Linux sendfile returns bytes read on success while FreeBSD
1683 	 *   returns 0.  We use the 'bytes read' parameter to get this value.
1684 	 */
1685 
1686 	l_loff_t offset64;
1687 	l_long offset;
1688 	int ret;
1689 	int error;
1690 
1691 	if (arg->offset != NULL) {
1692 		error = copyin(arg->offset, &offset, sizeof(offset));
1693 		if (error != 0)
1694 			return (error);
1695 		offset64 = (l_loff_t)offset;
1696 	}
1697 
1698 	ret = linux_sendfile_common(td, arg->out, arg->in,
1699 	    arg->offset != NULL ? &offset64 : NULL, arg->count);
1700 
1701 	if (arg->offset != NULL) {
1702 #if defined(__i386__) || defined(__arm__) || \
1703     (defined(__amd64__) && defined(COMPAT_LINUX32))
1704 		if (offset64 > INT32_MAX)
1705 			return (EOVERFLOW);
1706 #endif
1707 		offset = (l_long)offset64;
1708 		error = copyout(&offset, arg->offset, sizeof(offset));
1709 		if (error != 0)
1710 			return (error);
1711 	}
1712 
1713 	return (ret);
1714 }
1715 
1716 #if defined(__i386__) || defined(__arm__) || \
1717     (defined(__amd64__) && defined(COMPAT_LINUX32))
1718 
1719 int
1720 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
1721 {
1722 	l_loff_t offset;
1723 	int ret;
1724 	int error;
1725 
1726 	if (arg->offset != NULL) {
1727 		error = copyin(arg->offset, &offset, sizeof(offset));
1728 		if (error != 0)
1729 			return (error);
1730 	}
1731 
1732 	ret = linux_sendfile_common(td, arg->out, arg->in,
1733 		arg->offset != NULL ? &offset : NULL, arg->count);
1734 
1735 	if (arg->offset != NULL) {
1736 		error = copyout(&offset, arg->offset, sizeof(offset));
1737 		if (error != 0)
1738 			return (error);
1739 	}
1740 
1741 	return (ret);
1742 }
1743 
1744 /* Argument list sizes for linux_socketcall */
1745 static const unsigned char lxs_args_cnt[] = {
1746 	0 /* unused*/,		3 /* socket */,
1747 	3 /* bind */,		3 /* connect */,
1748 	2 /* listen */,		3 /* accept */,
1749 	3 /* getsockname */,	3 /* getpeername */,
1750 	4 /* socketpair */,	4 /* send */,
1751 	4 /* recv */,		6 /* sendto */,
1752 	6 /* recvfrom */,	2 /* shutdown */,
1753 	5 /* setsockopt */,	5 /* getsockopt */,
1754 	3 /* sendmsg */,	3 /* recvmsg */,
1755 	4 /* accept4 */,	5 /* recvmmsg */,
1756 	4 /* sendmmsg */,	4 /* sendfile */
1757 };
1758 #define	LINUX_ARGS_CNT		(nitems(lxs_args_cnt) - 1)
1759 #define	LINUX_ARG_SIZE(x)	(lxs_args_cnt[x] * sizeof(l_ulong))
1760 
1761 int
1762 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1763 {
1764 	l_ulong a[6];
1765 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1766 	register_t l_args[6];
1767 #endif
1768 	void *arg;
1769 	int error;
1770 
1771 	if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
1772 		return (EINVAL);
1773 	error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
1774 	if (error != 0)
1775 		return (error);
1776 
1777 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1778 	for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
1779 		l_args[i] = a[i];
1780 	arg = l_args;
1781 #else
1782 	arg = a;
1783 #endif
1784 	switch (args->what) {
1785 	case LINUX_SOCKET:
1786 		return (linux_socket(td, arg));
1787 	case LINUX_BIND:
1788 		return (linux_bind(td, arg));
1789 	case LINUX_CONNECT:
1790 		return (linux_connect(td, arg));
1791 	case LINUX_LISTEN:
1792 		return (linux_listen(td, arg));
1793 	case LINUX_ACCEPT:
1794 		return (linux_accept(td, arg));
1795 	case LINUX_GETSOCKNAME:
1796 		return (linux_getsockname(td, arg));
1797 	case LINUX_GETPEERNAME:
1798 		return (linux_getpeername(td, arg));
1799 	case LINUX_SOCKETPAIR:
1800 		return (linux_socketpair(td, arg));
1801 	case LINUX_SEND:
1802 		return (linux_send(td, arg));
1803 	case LINUX_RECV:
1804 		return (linux_recv(td, arg));
1805 	case LINUX_SENDTO:
1806 		return (linux_sendto(td, arg));
1807 	case LINUX_RECVFROM:
1808 		return (linux_recvfrom(td, arg));
1809 	case LINUX_SHUTDOWN:
1810 		return (linux_shutdown(td, arg));
1811 	case LINUX_SETSOCKOPT:
1812 		return (linux_setsockopt(td, arg));
1813 	case LINUX_GETSOCKOPT:
1814 		return (linux_getsockopt(td, arg));
1815 	case LINUX_SENDMSG:
1816 		return (linux_sendmsg(td, arg));
1817 	case LINUX_RECVMSG:
1818 		return (linux_recvmsg(td, arg));
1819 	case LINUX_ACCEPT4:
1820 		return (linux_accept4(td, arg));
1821 	case LINUX_RECVMMSG:
1822 		return (linux_recvmmsg(td, arg));
1823 	case LINUX_SENDMMSG:
1824 		return (linux_sendmmsg(td, arg));
1825 	case LINUX_SENDFILE:
1826 		return (linux_sendfile(td, arg));
1827 	}
1828 
1829 	linux_msg(td, "socket type %d not implemented", args->what);
1830 	return (ENOSYS);
1831 }
1832 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */
1833