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_PASSCRED:
226 return (LOCAL_CREDS_PERSISTENT);
227 case LINUX_SO_PEERCRED:
228 return (LOCAL_PEERCRED);
229 case LINUX_SO_RCVLOWAT:
230 return (SO_RCVLOWAT);
231 case LINUX_SO_SNDLOWAT:
232 return (SO_SNDLOWAT);
233 case LINUX_SO_RCVTIMEO:
234 return (SO_RCVTIMEO);
235 case LINUX_SO_SNDTIMEO:
236 return (SO_SNDTIMEO);
237 case LINUX_SO_TIMESTAMP:
238 return (SO_TIMESTAMP);
239 case LINUX_SO_ACCEPTCONN:
240 return (SO_ACCEPTCONN);
241 case LINUX_SO_PROTOCOL:
242 return (SO_PROTOCOL);
248 linux_to_bsd_tcp_sockopt(int opt)
252 case LINUX_TCP_NODELAY:
253 return (TCP_NODELAY);
254 case LINUX_TCP_MAXSEG:
258 case LINUX_TCP_KEEPIDLE:
259 return (TCP_KEEPIDLE);
260 case LINUX_TCP_KEEPINTVL:
261 return (TCP_KEEPINTVL);
262 case LINUX_TCP_KEEPCNT:
263 return (TCP_KEEPCNT);
264 case LINUX_TCP_MD5SIG:
271 linux_to_bsd_msg_flags(int flags)
275 if (flags & LINUX_MSG_OOB)
276 ret_flags |= MSG_OOB;
277 if (flags & LINUX_MSG_PEEK)
278 ret_flags |= MSG_PEEK;
279 if (flags & LINUX_MSG_DONTROUTE)
280 ret_flags |= MSG_DONTROUTE;
281 if (flags & LINUX_MSG_CTRUNC)
282 ret_flags |= MSG_CTRUNC;
283 if (flags & LINUX_MSG_TRUNC)
284 ret_flags |= MSG_TRUNC;
285 if (flags & LINUX_MSG_DONTWAIT)
286 ret_flags |= MSG_DONTWAIT;
287 if (flags & LINUX_MSG_EOR)
288 ret_flags |= MSG_EOR;
289 if (flags & LINUX_MSG_WAITALL)
290 ret_flags |= MSG_WAITALL;
291 if (flags & LINUX_MSG_NOSIGNAL)
292 ret_flags |= MSG_NOSIGNAL;
293 #if 0 /* not handled */
294 if (flags & LINUX_MSG_PROXY)
296 if (flags & LINUX_MSG_FIN)
298 if (flags & LINUX_MSG_SYN)
300 if (flags & LINUX_MSG_CONFIRM)
302 if (flags & LINUX_MSG_RST)
304 if (flags & LINUX_MSG_ERRQUEUE)
311 linux_to_bsd_cmsg_type(int cmsg_type)
315 case LINUX_SCM_RIGHTS:
317 case LINUX_SCM_CREDENTIALS:
324 bsd_to_linux_cmsg_type(int cmsg_type)
329 return (LINUX_SCM_RIGHTS);
331 return (LINUX_SCM_CREDENTIALS);
333 return (LINUX_SCM_TIMESTAMP);
339 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
341 if (lhdr->msg_controllen > INT_MAX)
344 bhdr->msg_name = PTRIN(lhdr->msg_name);
345 bhdr->msg_namelen = lhdr->msg_namelen;
346 bhdr->msg_iov = PTRIN(lhdr->msg_iov);
347 bhdr->msg_iovlen = lhdr->msg_iovlen;
348 bhdr->msg_control = PTRIN(lhdr->msg_control);
351 * msg_controllen is skipped since BSD and LINUX control messages
352 * are potentially different sizes (e.g. the cred structure used
353 * by SCM_CREDS is different between the two operating system).
355 * The caller can set it (if necessary) after converting all the
359 bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags);
364 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
366 lhdr->msg_name = PTROUT(bhdr->msg_name);
367 lhdr->msg_namelen = bhdr->msg_namelen;
368 lhdr->msg_iov = PTROUT(bhdr->msg_iov);
369 lhdr->msg_iovlen = bhdr->msg_iovlen;
370 lhdr->msg_control = PTROUT(bhdr->msg_control);
373 * msg_controllen is skipped since BSD and LINUX control messages
374 * are potentially different sizes (e.g. the cred structure used
375 * by SCM_CREDS is different between the two operating system).
377 * The caller can set it (if necessary) after converting all the
381 /* msg_flags skipped */
386 linux_set_socket_flags(int lflags, int *flags)
389 if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
391 if (lflags & LINUX_SOCK_NONBLOCK)
392 *flags |= SOCK_NONBLOCK;
393 if (lflags & LINUX_SOCK_CLOEXEC)
394 *flags |= SOCK_CLOEXEC;
399 linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
401 struct l_sockaddr *lsa;
404 error = bsd_to_linux_sockaddr(sa, &lsa, len);
408 error = copyout(lsa, uaddr, len);
415 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
416 struct mbuf *control, enum uio_seg segflg)
421 if (mp->msg_name != NULL) {
422 len = mp->msg_namelen;
423 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
430 error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
438 /* Return 0 if IP_HDRINCL is set for the given socket. */
440 linux_check_hdrincl(struct thread *td, int s)
445 size_val = sizeof(optval);
446 error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
447 &optval, UIO_SYSSPACE, &size_val);
451 return (optval == 0);
455 * Updated sendto() when IP_HDRINCL is set:
456 * tweak endian-dependent fields in the IP packet.
459 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
462 * linux_ip_copysize defines how many bytes we should copy
463 * from the beginning of the IP packet before we customize it for BSD.
464 * It should include all the fields we modify (ip_len and ip_off).
466 #define linux_ip_copysize 8
470 struct iovec aiov[1];
473 /* Check that the packet isn't too big or too small. */
474 if (linux_args->len < linux_ip_copysize ||
475 linux_args->len > IP_MAXPACKET)
478 packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
480 /* Make kernel copy of the packet to be sent */
481 if ((error = copyin(PTRIN(linux_args->msg), packet,
485 /* Convert fields from Linux to BSD raw IP socket format */
486 packet->ip_len = linux_args->len;
487 packet->ip_off = ntohs(packet->ip_off);
489 /* Prepare the msghdr and iovec structures describing the new packet */
490 msg.msg_name = PTRIN(linux_args->to);
491 msg.msg_namelen = linux_args->tolen;
494 msg.msg_control = NULL;
496 aiov[0].iov_base = (char *)packet;
497 aiov[0].iov_len = linux_args->len;
498 error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
501 free(packet, M_LINUX);
505 static const char *linux_netlink_names[] = {
506 [LINUX_NETLINK_ROUTE] = "ROUTE",
507 [LINUX_NETLINK_SOCK_DIAG] = "SOCK_DIAG",
508 [LINUX_NETLINK_NFLOG] = "NFLOG",
509 [LINUX_NETLINK_SELINUX] = "SELINUX",
510 [LINUX_NETLINK_AUDIT] = "AUDIT",
511 [LINUX_NETLINK_FIB_LOOKUP] = "FIB_LOOKUP",
512 [LINUX_NETLINK_NETFILTER] = "NETFILTER",
513 [LINUX_NETLINK_KOBJECT_UEVENT] = "KOBJECT_UEVENT",
517 linux_socket(struct thread *td, struct linux_socket_args *args)
519 int domain, retval_socket, type;
521 type = args->type & LINUX_SOCK_TYPE_MASK;
522 if (type < 0 || type > LINUX_SOCK_MAX)
524 retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
526 if (retval_socket != 0)
527 return (retval_socket);
528 domain = linux_to_bsd_domain(args->domain);
530 /* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
531 type = args->type & LINUX_SOCK_TYPE_MASK;
532 if (args->domain == LINUX_AF_NETLINK) {
535 if (args->protocol >= 0 &&
536 args->protocol < nitems(linux_netlink_names))
537 nl_name = linux_netlink_names[args->protocol];
542 "unsupported socket(AF_NETLINK, %d, "
543 "NETLINK_%s)", type, nl_name);
546 "unsupported socket(AF_NETLINK, %d, %d)",
547 type, args->protocol);
549 linux_msg(curthread, "unsupported socket domain %d, "
550 "type %d, protocol %d", args->domain, type,
553 return (EAFNOSUPPORT);
556 retval_socket = kern_socket(td, domain, type, args->protocol);
558 return (retval_socket);
561 && (args->protocol == IPPROTO_RAW || args->protocol == 0)
562 && domain == PF_INET) {
563 /* It's a raw IP socket: set the IP_HDRINCL option. */
567 /* We ignore any error returned by kern_setsockopt() */
568 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
569 &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
573 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
574 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
575 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
578 if (domain == PF_INET6) {
582 /* We ignore any error returned by setsockopt() */
583 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
584 &v6only, UIO_SYSSPACE, sizeof(v6only));
588 return (retval_socket);
592 linux_bind(struct thread *td, struct linux_bind_args *args)
597 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
602 error = kern_bindat(td, AT_FDCWD, args->s, sa);
606 if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
612 linux_connect(struct thread *td, struct linux_connect_args *args)
620 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
625 error = kern_connectat(td, AT_FDCWD, args->s, sa);
627 if (error != EISCONN)
631 * Linux doesn't return EISCONN the first time it occurs,
632 * when on a non-blocking socket. Instead it returns the
633 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
635 error = getsock_cap(td, args->s, &cap_connect_rights,
642 if (fflag & FNONBLOCK) {
644 if (so->so_emuldata == 0)
645 error = so->so_error;
646 so->so_emuldata = (void *)1;
655 linux_listen(struct thread *td, struct linux_listen_args *args)
658 return (kern_listen(td, args->s, args->backlog));
662 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
663 l_uintptr_t namelen, int flags)
666 struct file *fp, *fp1;
675 error = linux_set_socket_flags(flags, &bflags);
679 if (PTRIN(addr) == NULL) {
681 error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
683 error = copyin(PTRIN(namelen), &len, sizeof(len));
688 error = kern_accept4(td, s, &sa, &len, bflags, &fp);
692 * Translate errno values into ones used by Linux.
696 * XXX. This is wrong, different sockaddr structures
697 * have different sizes.
701 if (namelen != sizeof(struct sockaddr_in))
705 error1 = getsock_cap(td, s, &cap_accept_rights, &fp1, NULL, NULL);
711 if (so->so_type == SOCK_DGRAM)
720 error = linux_copyout_sockaddr(sa, PTRIN(addr), len);
723 * XXX: We should also copyout the len, shouldn't we?
727 fdclose(td, fp, td->td_retval[0]);
728 td->td_retval[0] = 0;
738 linux_accept(struct thread *td, struct linux_accept_args *args)
741 return (linux_accept_common(td, args->s, args->addr,
746 linux_accept4(struct thread *td, struct linux_accept4_args *args)
749 return (linux_accept_common(td, args->s, args->addr,
750 args->namelen, args->flags));
754 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
759 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
763 error = kern_getsockname(td, args->s, &sa, &len);
768 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
772 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
777 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
782 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
788 error = kern_getpeername(td, args->s, &sa, &len);
793 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
797 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
802 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
804 int domain, error, sv[2], type;
806 domain = linux_to_bsd_domain(args->domain);
807 if (domain != PF_LOCAL)
808 return (EAFNOSUPPORT);
809 type = args->type & LINUX_SOCK_TYPE_MASK;
810 if (type < 0 || type > LINUX_SOCK_MAX)
812 error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
816 if (args->protocol != 0 && args->protocol != PF_UNIX) {
818 * Use of PF_UNIX as protocol argument is not right,
820 * Do not map PF_UNIX as its Linux value is identical
823 return (EPROTONOSUPPORT);
825 error = kern_socketpair(td, domain, type, 0, sv);
828 error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
830 (void)kern_close(td, sv[0]);
831 (void)kern_close(td, sv[1]);
836 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
837 struct linux_send_args {
845 linux_send(struct thread *td, struct linux_send_args *args)
847 struct sendto_args /* {
858 bsd_args.s = args->s;
859 bsd_args.buf = (caddr_t)PTRIN(args->msg);
860 bsd_args.len = args->len;
861 bsd_args.flags = args->flags;
864 error = sys_sendto(td, &bsd_args);
865 if (error == ENOTCONN) {
867 * Linux doesn't return ENOTCONN for non-blocking sockets.
868 * Instead it returns the EAGAIN.
870 error = getsock_cap(td, args->s, &cap_send_rights, &fp,
873 if (fflag & FNONBLOCK)
881 struct linux_recv_args {
889 linux_recv(struct thread *td, struct linux_recv_args *args)
891 struct recvfrom_args /* {
896 struct sockaddr *from;
897 socklen_t fromlenaddr;
900 bsd_args.s = args->s;
901 bsd_args.buf = (caddr_t)PTRIN(args->msg);
902 bsd_args.len = args->len;
903 bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
904 bsd_args.from = NULL;
905 bsd_args.fromlenaddr = 0;
906 return (sys_recvfrom(td, &bsd_args));
908 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
911 linux_sendto(struct thread *td, struct linux_sendto_args *args)
916 if (linux_check_hdrincl(td, args->s) == 0)
917 /* IP_HDRINCL set, tweak the packet before sending */
918 return (linux_sendto_hdrincl(td, args));
920 msg.msg_name = PTRIN(args->to);
921 msg.msg_namelen = args->tolen;
924 msg.msg_control = NULL;
926 aiov.iov_base = PTRIN(args->msg);
927 aiov.iov_len = args->len;
928 return (linux_sendit(td, args->s, &msg, args->flags, NULL,
933 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
940 if (PTRIN(args->fromlen) != NULL) {
941 error = copyin(PTRIN(args->fromlen), &fromlen,
947 sa = malloc(fromlen, M_SONAME, M_WAITOK);
954 msg.msg_namelen = fromlen;
957 aiov.iov_base = PTRIN(args->buf);
958 aiov.iov_len = args->len;
960 msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
962 error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
966 if (PTRIN(args->from) != NULL)
967 error = linux_copyout_sockaddr(sa, PTRIN(args->from), msg.msg_namelen);
969 if (error == 0 && PTRIN(args->fromlen) != NULL)
970 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
971 sizeof(msg.msg_namelen));
978 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
981 struct cmsghdr *cmsg;
982 struct mbuf *control;
984 struct l_cmsghdr linux_cmsg;
985 struct l_cmsghdr *ptr_cmsg;
986 struct l_msghdr linux_msghdr;
991 sa_family_t sa_family;
998 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1003 * Some Linux applications (ping) define a non-NULL control data
1004 * pointer, but a msg_controllen of 0, which is not allowed in the
1005 * FreeBSD system call interface. NULL the msg_control pointer in
1006 * order to handle this case. This should be checked, but allows the
1007 * Linux ping to work.
1009 if (PTRIN(linux_msghdr.msg_control) != NULL &&
1010 linux_msghdr.msg_controllen == 0)
1011 linux_msghdr.msg_control = PTROUT(NULL);
1013 error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
1017 #ifdef COMPAT_LINUX32
1018 error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
1021 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
1028 error = kern_getsockname(td, s, &sa, &datalen);
1031 sa_family = sa->sa_family;
1034 if (flags & LINUX_MSG_OOB) {
1036 if (sa_family == AF_UNIX)
1039 error = getsock_cap(td, s, &cap_send_rights, &fp,
1044 if (so->so_type != SOCK_STREAM)
1051 if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1053 control = m_get(M_WAITOK, MT_CONTROL);
1054 MCLGET(control, M_WAITOK);
1055 data = mtod(control, void *);
1058 ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1059 clen = linux_msghdr.msg_controllen;
1061 error = copyin(ptr_cmsg, &linux_cmsg,
1062 sizeof(struct l_cmsghdr));
1067 if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1068 linux_cmsg.cmsg_len > clen)
1071 if (datalen + CMSG_HDRSZ > MCLBYTES)
1075 * Now we support only SCM_RIGHTS and SCM_CRED,
1076 * so return EINVAL in any other cmsg_type
1080 linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1082 linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1083 if (cmsg->cmsg_type == -1
1084 || cmsg->cmsg_level != SOL_SOCKET) {
1085 linux_msg(curthread,
1086 "unsupported sendmsg cmsg level %d type %d",
1087 linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1092 * Some applications (e.g. pulseaudio) attempt to
1093 * send ancillary data even if the underlying protocol
1094 * doesn't support it which is not allowed in the
1095 * FreeBSD system call interface.
1097 if (sa_family != AF_UNIX)
1100 if (cmsg->cmsg_type == SCM_CREDS) {
1101 len = sizeof(struct cmsgcred);
1102 if (datalen + CMSG_SPACE(len) > MCLBYTES)
1106 * The lower levels will fill in the structure
1108 memset(CMSG_DATA(data), 0, len);
1110 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1111 if (datalen + CMSG_SPACE(len) < datalen ||
1112 datalen + CMSG_SPACE(len) > MCLBYTES)
1115 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1116 CMSG_DATA(data), len);
1121 cmsg->cmsg_len = CMSG_LEN(len);
1122 data = (char *)data + CMSG_SPACE(len);
1123 datalen += CMSG_SPACE(len);
1126 if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1129 clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1130 ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1131 LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1132 } while(clen >= sizeof(struct l_cmsghdr));
1134 control->m_len = datalen;
1143 error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1153 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1156 return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1161 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1163 struct l_mmsghdr *msg;
1165 int error, datagrams;
1167 if (args->vlen > UIO_MAXIOV)
1168 args->vlen = UIO_MAXIOV;
1170 msg = PTRIN(args->msg);
1172 while (datagrams < args->vlen) {
1173 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1178 retval = td->td_retval[0];
1179 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1186 td->td_retval[0] = datagrams;
1191 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1192 l_uint flags, struct msghdr *msg)
1195 struct cmsgcred *cmcred;
1196 struct l_cmsghdr *linux_cmsg = NULL;
1197 struct l_ucred linux_ucred;
1198 socklen_t datalen, maxlen, outlen;
1199 struct l_msghdr linux_msghdr;
1200 struct iovec *iov, *uiov;
1201 struct mbuf *control = NULL;
1202 struct mbuf **controlp;
1203 struct timeval *ftmvl;
1204 struct sockaddr *sa;
1208 int error, i, fd, fds, *fdp;
1210 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1214 error = linux_to_bsd_msghdr(msg, &linux_msghdr);
1218 #ifdef COMPAT_LINUX32
1219 error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1222 error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1227 if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1228 msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1229 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1233 msg->msg_name = NULL;
1236 uiov = msg->msg_iov;
1238 controlp = (msg->msg_control != NULL) ? &control : NULL;
1239 error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1240 msg->msg_iov = uiov;
1245 * Note that kern_recvit() updates msg->msg_namelen.
1247 if (msg->msg_name != NULL && msg->msg_namelen > 0) {
1248 msg->msg_name = PTRIN(linux_msghdr.msg_name);
1249 error = linux_copyout_sockaddr(sa,
1250 PTRIN(msg->msg_name), msg->msg_namelen);
1255 error = bsd_to_linux_msghdr(msg, &linux_msghdr);
1259 maxlen = linux_msghdr.msg_controllen;
1260 linux_msghdr.msg_controllen = 0;
1262 linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1264 msg->msg_control = mtod(control, struct cmsghdr *);
1265 msg->msg_controllen = control->m_len;
1267 cm = CMSG_FIRSTHDR(msg);
1268 outbuf = PTRIN(linux_msghdr.msg_control);
1270 while (cm != NULL) {
1271 linux_cmsg->cmsg_type =
1272 bsd_to_linux_cmsg_type(cm->cmsg_type);
1273 linux_cmsg->cmsg_level =
1274 bsd_to_linux_sockopt_level(cm->cmsg_level);
1275 if (linux_cmsg->cmsg_type == -1 ||
1276 cm->cmsg_level != SOL_SOCKET) {
1277 linux_msg(curthread,
1278 "unsupported recvmsg cmsg level %d type %d",
1279 cm->cmsg_level, cm->cmsg_type);
1284 data = CMSG_DATA(cm);
1285 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1287 switch (cm->cmsg_type) {
1289 if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1290 fds = datalen / sizeof(int);
1292 for (i = 0; i < fds; i++) {
1294 (void)kern_fcntl(td, fd,
1295 F_SETFD, FD_CLOEXEC);
1302 * Currently LOCAL_CREDS is never in
1303 * effect for Linux so no need to worry
1306 if (datalen != sizeof(*cmcred)) {
1310 cmcred = (struct cmsgcred *)data;
1311 bzero(&linux_ucred, sizeof(linux_ucred));
1312 linux_ucred.pid = cmcred->cmcred_pid;
1313 linux_ucred.uid = cmcred->cmcred_uid;
1314 linux_ucred.gid = cmcred->cmcred_gid;
1315 data = &linux_ucred;
1316 datalen = sizeof(linux_ucred);
1320 if (datalen != sizeof(struct timeval)) {
1324 ftmvl = (struct timeval *)data;
1325 ltmvl.tv_sec = ftmvl->tv_sec;
1326 ltmvl.tv_usec = ftmvl->tv_usec;
1328 datalen = sizeof(ltmvl);
1332 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1337 linux_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1338 m_dispose_extcontrolm(control);
1343 linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1345 error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1348 outbuf += L_CMSG_HDRSZ;
1350 error = copyout(data, outbuf, datalen);
1354 outbuf += LINUX_CMSG_ALIGN(datalen);
1355 outlen += LINUX_CMSG_LEN(datalen);
1357 cm = CMSG_NXTHDR(msg, cm);
1359 linux_msghdr.msg_controllen = outlen;
1363 error = copyout(&linux_msghdr, msghdr, sizeof(linux_msghdr));
1366 if (control != NULL) {
1368 m_dispose_extcontrolm(control);
1372 free(linux_cmsg, M_LINUX);
1379 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1381 struct msghdr bsd_msg;
1383 return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1384 args->flags, &bsd_msg));
1388 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1390 struct l_mmsghdr *msg;
1391 struct msghdr bsd_msg;
1392 struct l_timespec lts;
1393 struct timespec ts, tts;
1395 int error, datagrams;
1397 if (args->timeout) {
1398 error = copyin(args->timeout, <s, sizeof(struct l_timespec));
1401 error = linux_to_native_timespec(&ts, <s);
1405 timespecadd(&tts, &ts, &tts);
1408 msg = PTRIN(args->msg);
1410 while (datagrams < args->vlen) {
1411 error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1412 args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1416 retval = td->td_retval[0];
1417 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1424 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1426 if (args->flags & LINUX_MSG_WAITFORONE)
1427 args->flags |= LINUX_MSG_DONTWAIT;
1430 * See BUGS section of recvmmsg(2).
1432 if (args->timeout) {
1434 timespecsub(&ts, &tts, &ts);
1435 if (!timespecisset(&ts) || ts.tv_sec > 0)
1438 /* Out of band data, return right away. */
1439 if (bsd_msg.msg_flags & MSG_OOB)
1443 td->td_retval[0] = datagrams;
1448 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1451 return (kern_shutdown(td, args->s, args->how));
1455 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1458 struct sockaddr *sa;
1461 int error, level, name;
1463 level = linux_to_bsd_sockopt_level(args->level);
1466 name = linux_to_bsd_so_sockopt(args->optname);
1468 case LOCAL_CREDS_PERSISTENT:
1474 error = copyin(PTRIN(args->optval), &linux_tv,
1478 tv.tv_sec = linux_tv.tv_sec;
1479 tv.tv_usec = linux_tv.tv_usec;
1480 return (kern_setsockopt(td, args->s, level,
1481 name, &tv, UIO_SYSSPACE, sizeof(tv)));
1488 if (args->optname == LINUX_IP_RECVERR &&
1489 linux_ignore_ip_recverr) {
1491 * XXX: This is a hack to unbreak DNS resolution
1492 * with glibc 2.30 and above.
1496 name = linux_to_bsd_ip_sockopt(args->optname);
1499 name = linux_to_bsd_ip6_sockopt(args->optname);
1502 name = linux_to_bsd_tcp_sockopt(args->optname);
1509 linux_msg(curthread,
1510 "unsupported setsockopt level %d optname %d",
1511 args->level, args->optname);
1512 return (ENOPROTOOPT);
1515 if (name == IPV6_NEXTHOP) {
1517 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
1521 error = kern_setsockopt(td, args->s, level,
1522 name, sa, UIO_SYSSPACE, len);
1525 error = kern_setsockopt(td, args->s, level,
1526 name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
1533 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1537 socklen_t tv_len, xulen, len;
1538 struct sockaddr *sa;
1541 int error, level, name, newval;
1543 level = linux_to_bsd_sockopt_level(args->level);
1546 name = linux_to_bsd_so_sockopt(args->optname);
1548 case LOCAL_CREDS_PERSISTENT:
1554 tv_len = sizeof(tv);
1555 error = kern_getsockopt(td, args->s, level,
1556 name, &tv, UIO_SYSSPACE, &tv_len);
1559 linux_tv.tv_sec = tv.tv_sec;
1560 linux_tv.tv_usec = tv.tv_usec;
1561 return (copyout(&linux_tv, PTRIN(args->optval),
1564 case LOCAL_PEERCRED:
1565 if (args->optlen < sizeof(lxu))
1568 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
1569 * but by the Unix socket's level 0.
1573 error = kern_getsockopt(td, args->s, level,
1574 name, &xu, UIO_SYSSPACE, &xulen);
1577 lxu.pid = xu.cr_pid;
1578 lxu.uid = xu.cr_uid;
1579 lxu.gid = xu.cr_gid;
1580 return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1583 len = sizeof(newval);
1584 error = kern_getsockopt(td, args->s, level,
1585 name, &newval, UIO_SYSSPACE, &len);
1588 newval = -bsd_to_linux_errno(newval);
1589 return (copyout(&newval, PTRIN(args->optval), len));
1596 name = linux_to_bsd_ip_sockopt(args->optname);
1599 name = linux_to_bsd_ip6_sockopt(args->optname);
1602 name = linux_to_bsd_tcp_sockopt(args->optname);
1609 linux_msg(curthread,
1610 "unsupported getsockopt level %d optname %d",
1611 args->level, args->optname);
1615 if (name == IPV6_NEXTHOP) {
1616 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1619 sa = malloc(len, M_SONAME, M_WAITOK);
1621 error = kern_getsockopt(td, args->s, level,
1622 name, sa, UIO_SYSSPACE, &len);
1626 error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
1628 error = copyout(&len, PTRIN(args->optlen),
1634 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1638 error = kern_getsockopt(td, args->s, level,
1639 name, PTRIN(args->optval), UIO_USERSPACE, &len);
1641 error = copyout(&len, PTRIN(args->optlen),
1649 linux_sendfile_common(struct thread *td, l_int out, l_int in,
1650 l_loff_t *offset, l_size_t count)
1654 l_loff_t current_offset;
1658 error = fget_read(td, in, &cap_pread_rights, &fp);
1662 if (offset != NULL) {
1663 current_offset = *offset;
1665 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1666 fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
1669 current_offset = td->td_uretoff.tdu_off;
1674 /* Linux cannot have 0 count. */
1675 if (count <= 0 || current_offset < 0) {
1680 error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
1681 &bytes_read, 0, td);
1684 current_offset += bytes_read;
1686 if (offset != NULL) {
1687 *offset = current_offset;
1689 error = fo_seek(fp, current_offset, SEEK_SET, td);
1694 td->td_retval[0] = (ssize_t)bytes_read;
1701 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
1704 * Differences between FreeBSD and Linux sendfile:
1705 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
1706 * mean send the whole file.) In linux_sendfile given fds are still
1707 * checked for validity when the count is 0.
1708 * - Linux can send to any fd whereas FreeBSD only supports sockets.
1709 * The same restriction follows for linux_sendfile.
1710 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
1711 * - Linux takes an offset pointer and updates it to the read location.
1712 * FreeBSD takes in an offset and a 'bytes read' parameter which is
1713 * only filled if it isn't NULL. We use this parameter to update the
1714 * offset pointer if it exists.
1715 * - Linux sendfile returns bytes read on success while FreeBSD
1716 * returns 0. We use the 'bytes read' parameter to get this value.
1724 if (arg->offset != NULL) {
1725 error = copyin(arg->offset, &offset, sizeof(offset));
1728 offset64 = (l_loff_t)offset;
1731 ret = linux_sendfile_common(td, arg->out, arg->in,
1732 arg->offset != NULL ? &offset64 : NULL, arg->count);
1734 if (arg->offset != NULL) {
1735 #if defined(__i386__) || defined(__arm__) || \
1736 (defined(__amd64__) && defined(COMPAT_LINUX32))
1737 if (offset64 > INT32_MAX)
1740 offset = (l_long)offset64;
1741 error = copyout(&offset, arg->offset, sizeof(offset));
1749 #if defined(__i386__) || defined(__arm__) || \
1750 (defined(__amd64__) && defined(COMPAT_LINUX32))
1753 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
1759 if (arg->offset != NULL) {
1760 error = copyin(arg->offset, &offset, sizeof(offset));
1765 ret = linux_sendfile_common(td, arg->out, arg->in,
1766 arg->offset != NULL ? &offset : NULL, arg->count);
1768 if (arg->offset != NULL) {
1769 error = copyout(&offset, arg->offset, sizeof(offset));
1777 /* Argument list sizes for linux_socketcall */
1778 static const unsigned char lxs_args_cnt[] = {
1779 0 /* unused*/, 3 /* socket */,
1780 3 /* bind */, 3 /* connect */,
1781 2 /* listen */, 3 /* accept */,
1782 3 /* getsockname */, 3 /* getpeername */,
1783 4 /* socketpair */, 4 /* send */,
1784 4 /* recv */, 6 /* sendto */,
1785 6 /* recvfrom */, 2 /* shutdown */,
1786 5 /* setsockopt */, 5 /* getsockopt */,
1787 3 /* sendmsg */, 3 /* recvmsg */,
1788 4 /* accept4 */, 5 /* recvmmsg */,
1789 4 /* sendmmsg */, 4 /* sendfile */
1791 #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1)
1792 #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong))
1795 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1798 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1799 register_t l_args[6];
1804 if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
1806 error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
1810 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1811 for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
1817 switch (args->what) {
1819 return (linux_socket(td, arg));
1821 return (linux_bind(td, arg));
1823 return (linux_connect(td, arg));
1825 return (linux_listen(td, arg));
1827 return (linux_accept(td, arg));
1828 case LINUX_GETSOCKNAME:
1829 return (linux_getsockname(td, arg));
1830 case LINUX_GETPEERNAME:
1831 return (linux_getpeername(td, arg));
1832 case LINUX_SOCKETPAIR:
1833 return (linux_socketpair(td, arg));
1835 return (linux_send(td, arg));
1837 return (linux_recv(td, arg));
1839 return (linux_sendto(td, arg));
1840 case LINUX_RECVFROM:
1841 return (linux_recvfrom(td, arg));
1842 case LINUX_SHUTDOWN:
1843 return (linux_shutdown(td, arg));
1844 case LINUX_SETSOCKOPT:
1845 return (linux_setsockopt(td, arg));
1846 case LINUX_GETSOCKOPT:
1847 return (linux_getsockopt(td, arg));
1849 return (linux_sendmsg(td, arg));
1851 return (linux_recvmsg(td, arg));
1853 return (linux_accept4(td, arg));
1854 case LINUX_RECVMMSG:
1855 return (linux_recvmmsg(td, arg));
1856 case LINUX_SENDMMSG:
1857 return (linux_sendmmsg(td, arg));
1858 case LINUX_SENDFILE:
1859 return (linux_sendfile(td, arg));
1862 linux_msg(td, "socket type %d not implemented", args->what);
1865 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */