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