2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1995 Søren Schmidt
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 /* XXX we use functions that might not exist. */
33 #include "opt_compat.h"
34 #include "opt_inet6.h"
36 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/sysproto.h>
40 #include <sys/capsicum.h>
41 #include <sys/fcntl.h>
43 #include <sys/filedesc.h>
44 #include <sys/limits.h>
46 #include <sys/malloc.h>
47 #include <sys/mutex.h>
49 #include <sys/socket.h>
50 #include <sys/socketvar.h>
51 #include <sys/syscallsubr.h>
54 #include <sys/syslog.h>
56 #include <sys/unistd.h>
58 #include <security/audit/audit.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <netinet/tcp.h>
67 #include <netinet/ip6.h>
68 #include <netinet6/ip6_var.h>
72 #include <machine/../linux32/linux.h>
73 #include <machine/../linux32/linux32_proto.h>
75 #include <machine/../linux/linux.h>
76 #include <machine/../linux/linux_proto.h>
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>
85 static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *,
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 *);
92 linux_to_bsd_sockopt_level(int level)
95 if (level == LINUX_SOL_SOCKET)
97 /* Remaining values are RFC-defined protocol numbers. */
102 bsd_to_linux_sockopt_level(int level)
105 if (level == SOL_SOCKET)
106 return (LINUX_SOL_SOCKET);
111 linux_to_bsd_ip_sockopt(int opt)
119 case LINUX_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:
138 linux_to_bsd_ip6_sockopt(int 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);
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:
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);
195 linux_to_bsd_so_sockopt(int opt)
201 case LINUX_SO_REUSEADDR:
202 return (SO_REUSEADDR);
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:
214 case LINUX_SO_RCVBUF:
215 case LINUX_SO_RCVBUFFORCE:
217 case LINUX_SO_KEEPALIVE:
218 return (SO_KEEPALIVE);
219 case LINUX_SO_OOBINLINE:
220 return (SO_OOBINLINE);
221 case LINUX_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);
246 linux_to_bsd_tcp_sockopt(int opt)
250 case LINUX_TCP_NODELAY:
251 return (TCP_NODELAY);
252 case LINUX_TCP_MAXSEG:
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:
269 linux_to_bsd_msg_flags(int flags)
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)
294 if (flags & LINUX_MSG_FIN)
296 if (flags & LINUX_MSG_SYN)
298 if (flags & LINUX_MSG_CONFIRM)
300 if (flags & LINUX_MSG_RST)
302 if (flags & LINUX_MSG_ERRQUEUE)
309 linux_to_bsd_cmsg_type(int cmsg_type)
313 case LINUX_SCM_RIGHTS:
315 case LINUX_SCM_CREDENTIALS:
322 bsd_to_linux_cmsg_type(int cmsg_type)
327 return (LINUX_SCM_RIGHTS);
329 return (LINUX_SCM_CREDENTIALS);
331 return (LINUX_SCM_TIMESTAMP);
337 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
339 if (lhdr->msg_controllen > INT_MAX)
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);
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).
353 * The caller can set it (if necessary) after converting all the
357 bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags);
362 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
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);
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).
375 * The caller can set it (if necessary) after converting all the
379 /* msg_flags skipped */
384 linux_set_socket_flags(int lflags, int *flags)
387 if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
389 if (lflags & LINUX_SOCK_NONBLOCK)
390 *flags |= SOCK_NONBLOCK;
391 if (lflags & LINUX_SOCK_CLOEXEC)
392 *flags |= SOCK_CLOEXEC;
397 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
398 struct mbuf *control, enum uio_seg segflg)
403 if (mp->msg_name != NULL) {
404 len = mp->msg_namelen;
405 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
412 error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
420 /* Return 0 if IP_HDRINCL is set for the given socket. */
422 linux_check_hdrincl(struct thread *td, int s)
427 size_val = sizeof(optval);
428 error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
429 &optval, UIO_SYSSPACE, &size_val);
433 return (optval == 0);
437 * Updated sendto() when IP_HDRINCL is set:
438 * tweak endian-dependent fields in the IP packet.
441 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
444 * linux_ip_copysize defines how many bytes we should copy
445 * from the beginning of the IP packet before we customize it for BSD.
446 * It should include all the fields we modify (ip_len and ip_off).
448 #define linux_ip_copysize 8
452 struct iovec aiov[1];
455 /* Check that the packet isn't too big or too small. */
456 if (linux_args->len < linux_ip_copysize ||
457 linux_args->len > IP_MAXPACKET)
460 packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
462 /* Make kernel copy of the packet to be sent */
463 if ((error = copyin(PTRIN(linux_args->msg), packet,
467 /* Convert fields from Linux to BSD raw IP socket format */
468 packet->ip_len = linux_args->len;
469 packet->ip_off = ntohs(packet->ip_off);
471 /* Prepare the msghdr and iovec structures describing the new packet */
472 msg.msg_name = PTRIN(linux_args->to);
473 msg.msg_namelen = linux_args->tolen;
476 msg.msg_control = NULL;
478 aiov[0].iov_base = (char *)packet;
479 aiov[0].iov_len = linux_args->len;
480 error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
483 free(packet, M_LINUX);
488 linux_socket(struct thread *td, struct linux_socket_args *args)
490 int domain, retval_socket, type;
492 type = args->type & LINUX_SOCK_TYPE_MASK;
493 if (type < 0 || type > LINUX_SOCK_MAX)
495 retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
497 if (retval_socket != 0)
498 return (retval_socket);
499 domain = linux_to_bsd_domain(args->domain);
501 return (EAFNOSUPPORT);
503 retval_socket = kern_socket(td, domain, type, args->protocol);
505 return (retval_socket);
508 && (args->protocol == IPPROTO_RAW || args->protocol == 0)
509 && domain == PF_INET) {
510 /* It's a raw IP socket: set the IP_HDRINCL option. */
514 /* We ignore any error returned by kern_setsockopt() */
515 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
516 &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
520 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
521 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
522 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
525 if (domain == PF_INET6) {
529 /* We ignore any error returned by setsockopt() */
530 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
531 &v6only, UIO_SYSSPACE, sizeof(v6only));
535 return (retval_socket);
539 linux_bind(struct thread *td, struct linux_bind_args *args)
544 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
549 error = kern_bindat(td, AT_FDCWD, args->s, sa);
553 if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
559 linux_connect(struct thread *td, struct linux_connect_args *args)
567 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
572 error = kern_connectat(td, AT_FDCWD, args->s, sa);
574 if (error != EISCONN)
578 * Linux doesn't return EISCONN the first time it occurs,
579 * when on a non-blocking socket. Instead it returns the
580 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
582 error = getsock_cap(td, args->s, &cap_connect_rights,
589 if (fflag & FNONBLOCK) {
591 if (so->so_emuldata == 0)
592 error = so->so_error;
593 so->so_emuldata = (void *)1;
602 linux_listen(struct thread *td, struct linux_listen_args *args)
605 return (kern_listen(td, args->s, args->backlog));
609 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
610 l_uintptr_t namelen, int flags)
612 struct l_sockaddr *lsa;
614 struct file *fp, *fp1;
623 error = linux_set_socket_flags(flags, &bflags);
627 if (PTRIN(addr) == NULL) {
629 error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
631 error = copyin(PTRIN(namelen), &len, sizeof(len));
636 error = kern_accept4(td, s, &sa, &len, bflags, &fp);
640 * Translate errno values into ones used by Linux.
644 * XXX. This is wrong, different sockaddr structures
645 * have different sizes.
649 if (namelen != sizeof(struct sockaddr_in))
653 error1 = getsock_cap(td, s, &cap_accept_rights, &fp1, NULL, NULL);
659 if (so->so_type == SOCK_DGRAM)
668 error = bsd_to_linux_sockaddr(sa, &lsa, len);
670 error = copyout(lsa, PTRIN(addr), len);
674 * XXX: We should also copyout the len, shouldn't we?
678 fdclose(td, fp, td->td_retval[0]);
679 td->td_retval[0] = 0;
689 linux_accept(struct thread *td, struct linux_accept_args *args)
692 return (linux_accept_common(td, args->s, args->addr,
697 linux_accept4(struct thread *td, struct linux_accept4_args *args)
700 return (linux_accept_common(td, args->s, args->addr,
701 args->namelen, args->flags));
705 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
707 struct l_sockaddr *lsa;
711 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
715 error = kern_getsockname(td, args->s, &sa, &len);
720 error = bsd_to_linux_sockaddr(sa, &lsa, len);
722 error = copyout(lsa, PTRIN(args->addr),
729 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
734 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
736 struct l_sockaddr *lsa;
740 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
746 error = kern_getpeername(td, args->s, &sa, &len);
751 error = bsd_to_linux_sockaddr(sa, &lsa, len);
753 error = copyout(lsa, PTRIN(args->addr),
760 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
765 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
767 int domain, error, sv[2], type;
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)
775 error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
779 if (args->protocol != 0 && args->protocol != PF_UNIX) {
781 * Use of PF_UNIX as protocol argument is not right,
783 * Do not map PF_UNIX as its Linux value is identical
786 return (EPROTONOSUPPORT);
788 error = kern_socketpair(td, domain, type, 0, sv);
791 error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
793 (void)kern_close(td, sv[0]);
794 (void)kern_close(td, sv[1]);
799 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
800 struct linux_send_args {
808 linux_send(struct thread *td, struct linux_send_args *args)
810 struct sendto_args /* {
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;
827 error = sys_sendto(td, &bsd_args);
828 if (error == ENOTCONN) {
830 * Linux doesn't return ENOTCONN for non-blocking sockets.
831 * Instead it returns the EAGAIN.
833 error = getsock_cap(td, args->s, &cap_send_rights, &fp,
836 if (fflag & FNONBLOCK)
844 struct linux_recv_args {
852 linux_recv(struct thread *td, struct linux_recv_args *args)
854 struct recvfrom_args /* {
859 struct sockaddr *from;
860 socklen_t fromlenaddr;
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));
871 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
874 linux_sendto(struct thread *td, struct linux_sendto_args *args)
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));
883 msg.msg_name = PTRIN(args->to);
884 msg.msg_namelen = args->tolen;
887 msg.msg_control = NULL;
889 aiov.iov_base = PTRIN(args->msg);
890 aiov.iov_len = args->len;
891 return (linux_sendit(td, args->s, &msg, args->flags, NULL,
896 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
898 struct l_sockaddr *lsa;
904 if (PTRIN(args->fromlen) != NULL) {
905 error = copyin(PTRIN(args->fromlen), &fromlen,
911 sa = malloc(fromlen, M_SONAME, M_WAITOK);
918 msg.msg_namelen = fromlen;
921 aiov.iov_base = PTRIN(args->buf);
922 aiov.iov_len = args->len;
924 msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
926 error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
930 if (PTRIN(args->from) != NULL) {
931 error = bsd_to_linux_sockaddr(sa, &lsa, msg.msg_namelen);
933 error = copyout(lsa, PTRIN(args->from),
938 if (error == 0 && PTRIN(args->fromlen) != NULL)
939 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
940 sizeof(msg.msg_namelen));
947 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
950 struct cmsghdr *cmsg;
951 struct mbuf *control;
953 struct l_cmsghdr linux_cmsg;
954 struct l_cmsghdr *ptr_cmsg;
955 struct l_msghdr linux_msghdr;
960 sa_family_t sa_family;
967 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
972 * Some Linux applications (ping) define a non-NULL control data
973 * pointer, but a msg_controllen of 0, which is not allowed in the
974 * FreeBSD system call interface. NULL the msg_control pointer in
975 * order to handle this case. This should be checked, but allows the
976 * Linux ping to work.
978 if (PTRIN(linux_msghdr.msg_control) != NULL &&
979 linux_msghdr.msg_controllen == 0)
980 linux_msghdr.msg_control = PTROUT(NULL);
982 error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
986 #ifdef COMPAT_LINUX32
987 error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
990 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
997 error = kern_getsockname(td, s, &sa, &datalen);
1000 sa_family = sa->sa_family;
1003 if (flags & LINUX_MSG_OOB) {
1005 if (sa_family == AF_UNIX)
1008 error = getsock_cap(td, s, &cap_send_rights, &fp,
1013 if (so->so_type != SOCK_STREAM)
1020 if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1022 control = m_get(M_WAITOK, MT_CONTROL);
1023 MCLGET(control, M_WAITOK);
1024 data = mtod(control, void *);
1027 ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1028 clen = linux_msghdr.msg_controllen;
1030 error = copyin(ptr_cmsg, &linux_cmsg,
1031 sizeof(struct l_cmsghdr));
1036 if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1037 linux_cmsg.cmsg_len > clen)
1040 if (datalen + CMSG_HDRSZ > MCLBYTES)
1044 * Now we support only SCM_RIGHTS and SCM_CRED,
1045 * so return EINVAL in any other cmsg_type
1049 linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1051 linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1052 if (cmsg->cmsg_type == -1
1053 || cmsg->cmsg_level != SOL_SOCKET) {
1054 linux_msg(curthread,
1055 "unsupported sendmsg cmsg level %d type %d",
1056 linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1061 * Some applications (e.g. pulseaudio) attempt to
1062 * send ancillary data even if the underlying protocol
1063 * doesn't support it which is not allowed in the
1064 * FreeBSD system call interface.
1066 if (sa_family != AF_UNIX)
1069 if (cmsg->cmsg_type == SCM_CREDS) {
1070 len = sizeof(struct cmsgcred);
1071 if (datalen + CMSG_SPACE(len) > MCLBYTES)
1075 * The lower levels will fill in the structure
1077 memset(CMSG_DATA(data), 0, len);
1079 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1080 if (datalen + CMSG_SPACE(len) < datalen ||
1081 datalen + CMSG_SPACE(len) > MCLBYTES)
1084 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1085 CMSG_DATA(data), len);
1090 cmsg->cmsg_len = CMSG_LEN(len);
1091 data = (char *)data + CMSG_SPACE(len);
1092 datalen += CMSG_SPACE(len);
1095 if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1098 clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1099 ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1100 LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1101 } while(clen >= sizeof(struct l_cmsghdr));
1103 control->m_len = datalen;
1112 error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1122 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1125 return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1130 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1132 struct l_mmsghdr *msg;
1134 int error, datagrams;
1136 if (args->vlen > UIO_MAXIOV)
1137 args->vlen = UIO_MAXIOV;
1139 msg = PTRIN(args->msg);
1141 while (datagrams < args->vlen) {
1142 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1147 retval = td->td_retval[0];
1148 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1155 td->td_retval[0] = datagrams;
1160 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1161 l_uint flags, struct msghdr *msg)
1164 struct cmsgcred *cmcred;
1165 struct l_cmsghdr *linux_cmsg = NULL;
1166 struct l_ucred linux_ucred;
1167 socklen_t datalen, maxlen, outlen;
1168 struct l_msghdr linux_msghdr;
1169 struct iovec *iov, *uiov;
1170 struct mbuf *control = NULL;
1171 struct mbuf **controlp;
1172 struct timeval *ftmvl;
1173 struct l_sockaddr *lsa;
1174 struct sockaddr *sa;
1178 int error, i, fd, fds, *fdp;
1180 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1184 error = linux_to_bsd_msghdr(msg, &linux_msghdr);
1188 #ifdef COMPAT_LINUX32
1189 error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1192 error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1197 if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1198 msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1199 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1203 msg->msg_name = NULL;
1206 uiov = msg->msg_iov;
1208 controlp = (msg->msg_control != NULL) ? &control : NULL;
1209 error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1210 msg->msg_iov = uiov;
1215 * Note that kern_recvit() updates msg->msg_namelen.
1217 if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1218 msg->msg_name = PTRIN(linux_msghdr.msg_name);
1219 error = bsd_to_linux_sockaddr(sa, &lsa, msg->msg_namelen);
1221 error = copyout(lsa, PTRIN(msg->msg_name),
1223 free(lsa, M_SONAME);
1228 error = bsd_to_linux_msghdr(msg, &linux_msghdr);
1232 maxlen = linux_msghdr.msg_controllen;
1233 linux_msghdr.msg_controllen = 0;
1235 linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1237 msg->msg_control = mtod(control, struct cmsghdr *);
1238 msg->msg_controllen = control->m_len;
1240 cm = CMSG_FIRSTHDR(msg);
1241 outbuf = PTRIN(linux_msghdr.msg_control);
1243 while (cm != NULL) {
1244 linux_cmsg->cmsg_type =
1245 bsd_to_linux_cmsg_type(cm->cmsg_type);
1246 linux_cmsg->cmsg_level =
1247 bsd_to_linux_sockopt_level(cm->cmsg_level);
1248 if (linux_cmsg->cmsg_type == -1 ||
1249 cm->cmsg_level != SOL_SOCKET) {
1250 linux_msg(curthread,
1251 "unsupported recvmsg cmsg level %d type %d",
1252 cm->cmsg_level, cm->cmsg_type);
1257 data = CMSG_DATA(cm);
1258 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1260 switch (cm->cmsg_type) {
1262 if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1263 fds = datalen / sizeof(int);
1265 for (i = 0; i < fds; i++) {
1267 (void)kern_fcntl(td, fd,
1268 F_SETFD, FD_CLOEXEC);
1275 * Currently LOCAL_CREDS is never in
1276 * effect for Linux so no need to worry
1279 if (datalen != sizeof(*cmcred)) {
1283 cmcred = (struct cmsgcred *)data;
1284 bzero(&linux_ucred, sizeof(linux_ucred));
1285 linux_ucred.pid = cmcred->cmcred_pid;
1286 linux_ucred.uid = cmcred->cmcred_uid;
1287 linux_ucred.gid = cmcred->cmcred_gid;
1288 data = &linux_ucred;
1289 datalen = sizeof(linux_ucred);
1293 if (datalen != sizeof(struct timeval)) {
1297 ftmvl = (struct timeval *)data;
1298 ltmvl.tv_sec = ftmvl->tv_sec;
1299 ltmvl.tv_usec = ftmvl->tv_usec;
1301 datalen = sizeof(ltmvl);
1305 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1310 linux_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1311 m_dispose_extcontrolm(control);
1316 linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1318 error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1321 outbuf += L_CMSG_HDRSZ;
1323 error = copyout(data, outbuf, datalen);
1327 outbuf += LINUX_CMSG_ALIGN(datalen);
1328 outlen += LINUX_CMSG_LEN(datalen);
1330 cm = CMSG_NXTHDR(msg, cm);
1332 linux_msghdr.msg_controllen = outlen;
1336 error = copyout(&linux_msghdr, msghdr, sizeof(linux_msghdr));
1339 if (control != NULL) {
1341 m_dispose_extcontrolm(control);
1345 free(linux_cmsg, M_LINUX);
1352 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1354 struct msghdr bsd_msg;
1356 return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1357 args->flags, &bsd_msg));
1361 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1363 struct l_mmsghdr *msg;
1364 struct msghdr bsd_msg;
1365 struct l_timespec lts;
1366 struct timespec ts, tts;
1368 int error, datagrams;
1370 if (args->timeout) {
1371 error = copyin(args->timeout, <s, sizeof(struct l_timespec));
1374 error = linux_to_native_timespec(&ts, <s);
1378 timespecadd(&tts, &ts, &tts);
1381 msg = PTRIN(args->msg);
1383 while (datagrams < args->vlen) {
1384 error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1385 args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1389 retval = td->td_retval[0];
1390 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1397 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1399 if (args->flags & LINUX_MSG_WAITFORONE)
1400 args->flags |= LINUX_MSG_DONTWAIT;
1403 * See BUGS section of recvmmsg(2).
1405 if (args->timeout) {
1407 timespecsub(&ts, &tts, &ts);
1408 if (!timespecisset(&ts) || ts.tv_sec > 0)
1411 /* Out of band data, return right away. */
1412 if (bsd_msg.msg_flags & MSG_OOB)
1416 td->td_retval[0] = datagrams;
1421 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1424 return (kern_shutdown(td, args->s, args->how));
1428 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1431 struct sockaddr *sa;
1434 int error, level, name;
1436 level = linux_to_bsd_sockopt_level(args->level);
1439 name = linux_to_bsd_so_sockopt(args->optname);
1444 error = copyin(PTRIN(args->optval), &linux_tv,
1448 tv.tv_sec = linux_tv.tv_sec;
1449 tv.tv_usec = linux_tv.tv_usec;
1450 return (kern_setsockopt(td, args->s, level,
1451 name, &tv, UIO_SYSSPACE, sizeof(tv)));
1458 if (args->optname == LINUX_IP_RECVERR &&
1459 linux_ignore_ip_recverr) {
1461 * XXX: This is a hack to unbreak DNS resolution
1462 * with glibc 2.30 and above.
1466 name = linux_to_bsd_ip_sockopt(args->optname);
1469 name = linux_to_bsd_ip6_sockopt(args->optname);
1472 name = linux_to_bsd_tcp_sockopt(args->optname);
1479 linux_msg(curthread,
1480 "unsupported setsockopt level %d optname %d",
1481 args->level, args->optname);
1482 return (ENOPROTOOPT);
1485 if (name == IPV6_NEXTHOP) {
1487 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
1491 error = kern_setsockopt(td, args->s, level,
1492 name, sa, UIO_SYSSPACE, len);
1495 error = kern_setsockopt(td, args->s, level,
1496 name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
1503 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1507 socklen_t tv_len, xulen, len;
1508 struct l_sockaddr *lsa;
1509 struct sockaddr *sa;
1512 int error, level, name, newval;
1514 level = linux_to_bsd_sockopt_level(args->level);
1517 name = linux_to_bsd_so_sockopt(args->optname);
1522 tv_len = sizeof(tv);
1523 error = kern_getsockopt(td, args->s, level,
1524 name, &tv, UIO_SYSSPACE, &tv_len);
1527 linux_tv.tv_sec = tv.tv_sec;
1528 linux_tv.tv_usec = tv.tv_usec;
1529 return (copyout(&linux_tv, PTRIN(args->optval),
1532 case LOCAL_PEERCRED:
1533 if (args->optlen < sizeof(lxu))
1536 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
1537 * but by the Unix socket's level 0.
1541 error = kern_getsockopt(td, args->s, level,
1542 name, &xu, UIO_SYSSPACE, &xulen);
1545 lxu.pid = xu.cr_pid;
1546 lxu.uid = xu.cr_uid;
1547 lxu.gid = xu.cr_gid;
1548 return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1551 len = sizeof(newval);
1552 error = kern_getsockopt(td, args->s, level,
1553 name, &newval, UIO_SYSSPACE, &len);
1556 newval = -linux_to_bsd_errno(newval);
1557 return (copyout(&newval, PTRIN(args->optval), len));
1564 name = linux_to_bsd_ip_sockopt(args->optname);
1567 name = linux_to_bsd_ip6_sockopt(args->optname);
1570 name = linux_to_bsd_tcp_sockopt(args->optname);
1577 linux_msg(curthread,
1578 "unsupported getsockopt level %d optname %d",
1579 args->level, args->optname);
1583 if (name == IPV6_NEXTHOP) {
1584 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1587 sa = malloc(len, M_SONAME, M_WAITOK);
1589 error = kern_getsockopt(td, args->s, level,
1590 name, sa, UIO_SYSSPACE, &len);
1594 error = bsd_to_linux_sockaddr(sa, &lsa, len);
1596 error = copyout(lsa, PTRIN(args->optval), len);
1597 free(lsa, M_SONAME);
1599 error = copyout(&len, PTRIN(args->optlen),
1605 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1609 error = kern_getsockopt(td, args->s, level,
1610 name, PTRIN(args->optval), UIO_USERSPACE, &len);
1612 error = copyout(&len, PTRIN(args->optlen),
1620 linux_sendfile_common(struct thread *td, l_int out, l_int in,
1621 l_loff_t *offset, l_size_t count)
1625 l_loff_t current_offset;
1629 error = fget_read(td, in, &cap_pread_rights, &fp);
1633 if (offset != NULL) {
1634 current_offset = *offset;
1636 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1637 fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
1640 current_offset = td->td_uretoff.tdu_off;
1645 /* Linux cannot have 0 count. */
1646 if (count <= 0 || current_offset < 0) {
1651 error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
1652 &bytes_read, 0, td);
1655 current_offset += bytes_read;
1657 if (offset != NULL) {
1658 *offset = current_offset;
1660 error = fo_seek(fp, current_offset, SEEK_SET, td);
1665 td->td_retval[0] = (ssize_t)bytes_read;
1672 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
1675 * Differences between FreeBSD and Linux sendfile:
1676 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
1677 * mean send the whole file.) In linux_sendfile given fds are still
1678 * checked for validity when the count is 0.
1679 * - Linux can send to any fd whereas FreeBSD only supports sockets.
1680 * The same restriction follows for linux_sendfile.
1681 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
1682 * - Linux takes an offset pointer and updates it to the read location.
1683 * FreeBSD takes in an offset and a 'bytes read' parameter which is
1684 * only filled if it isn't NULL. We use this parameter to update the
1685 * offset pointer if it exists.
1686 * - Linux sendfile returns bytes read on success while FreeBSD
1687 * returns 0. We use the 'bytes read' parameter to get this value.
1695 if (arg->offset != NULL) {
1696 error = copyin(arg->offset, &offset, sizeof(offset));
1699 offset64 = (l_loff_t)offset;
1702 ret = linux_sendfile_common(td, arg->out, arg->in,
1703 arg->offset != NULL ? &offset64 : NULL, arg->count);
1705 if (arg->offset != NULL) {
1706 #if defined(__i386__) || defined(__arm__) || \
1707 (defined(__amd64__) && defined(COMPAT_LINUX32))
1708 if (offset64 > INT32_MAX)
1711 offset = (l_long)offset64;
1712 error = copyout(&offset, arg->offset, sizeof(offset));
1720 #if defined(__i386__) || defined(__arm__) || \
1721 (defined(__amd64__) && defined(COMPAT_LINUX32))
1724 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
1730 if (arg->offset != NULL) {
1731 error = copyin(arg->offset, &offset, sizeof(offset));
1736 ret = linux_sendfile_common(td, arg->out, arg->in,
1737 arg->offset != NULL ? &offset : NULL, arg->count);
1739 if (arg->offset != NULL) {
1740 error = copyout(&offset, arg->offset, sizeof(offset));
1748 /* Argument list sizes for linux_socketcall */
1749 static const unsigned char lxs_args_cnt[] = {
1750 0 /* unused*/, 3 /* socket */,
1751 3 /* bind */, 3 /* connect */,
1752 2 /* listen */, 3 /* accept */,
1753 3 /* getsockname */, 3 /* getpeername */,
1754 4 /* socketpair */, 4 /* send */,
1755 4 /* recv */, 6 /* sendto */,
1756 6 /* recvfrom */, 2 /* shutdown */,
1757 5 /* setsockopt */, 5 /* getsockopt */,
1758 3 /* sendmsg */, 3 /* recvmsg */,
1759 4 /* accept4 */, 5 /* recvmmsg */,
1760 4 /* sendmmsg */, 4 /* sendfile */
1762 #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1)
1763 #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong))
1766 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1769 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1770 register_t l_args[6];
1775 if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
1777 error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
1781 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1782 for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
1788 switch (args->what) {
1790 return (linux_socket(td, arg));
1792 return (linux_bind(td, arg));
1794 return (linux_connect(td, arg));
1796 return (linux_listen(td, arg));
1798 return (linux_accept(td, arg));
1799 case LINUX_GETSOCKNAME:
1800 return (linux_getsockname(td, arg));
1801 case LINUX_GETPEERNAME:
1802 return (linux_getpeername(td, arg));
1803 case LINUX_SOCKETPAIR:
1804 return (linux_socketpair(td, arg));
1806 return (linux_send(td, arg));
1808 return (linux_recv(td, arg));
1810 return (linux_sendto(td, arg));
1811 case LINUX_RECVFROM:
1812 return (linux_recvfrom(td, arg));
1813 case LINUX_SHUTDOWN:
1814 return (linux_shutdown(td, arg));
1815 case LINUX_SETSOCKOPT:
1816 return (linux_setsockopt(td, arg));
1817 case LINUX_GETSOCKOPT:
1818 return (linux_getsockopt(td, arg));
1820 return (linux_sendmsg(td, arg));
1822 return (linux_recvmsg(td, arg));
1824 return (linux_accept4(td, arg));
1825 case LINUX_RECVMMSG:
1826 return (linux_recvmmsg(td, arg));
1827 case LINUX_SENDMMSG:
1828 return (linux_sendmmsg(td, arg));
1829 case LINUX_SENDFILE:
1830 return (linux_sendfile(td, arg));
1833 linux_msg(td, "socket type %d not implemented", args->what);
1836 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */