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_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
399 struct l_sockaddr *lsa;
402 error = bsd_to_linux_sockaddr(sa, &lsa, len);
406 error = copyout(lsa, uaddr, len);
413 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
414 struct mbuf *control, enum uio_seg segflg)
419 if (mp->msg_name != NULL) {
420 len = mp->msg_namelen;
421 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
428 error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
436 /* Return 0 if IP_HDRINCL is set for the given socket. */
438 linux_check_hdrincl(struct thread *td, int s)
443 size_val = sizeof(optval);
444 error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
445 &optval, UIO_SYSSPACE, &size_val);
449 return (optval == 0);
453 * Updated sendto() when IP_HDRINCL is set:
454 * tweak endian-dependent fields in the IP packet.
457 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
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).
464 #define linux_ip_copysize 8
468 struct iovec aiov[1];
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)
476 packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
478 /* Make kernel copy of the packet to be sent */
479 if ((error = copyin(PTRIN(linux_args->msg), packet,
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);
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;
492 msg.msg_control = NULL;
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,
499 free(packet, M_LINUX);
504 linux_socket(struct thread *td, struct linux_socket_args *args)
506 int domain, retval_socket, type;
508 type = args->type & LINUX_SOCK_TYPE_MASK;
509 if (type < 0 || type > LINUX_SOCK_MAX)
511 retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
513 if (retval_socket != 0)
514 return (retval_socket);
515 domain = linux_to_bsd_domain(args->domain);
517 return (EAFNOSUPPORT);
519 retval_socket = kern_socket(td, domain, type, args->protocol);
521 return (retval_socket);
524 && (args->protocol == IPPROTO_RAW || args->protocol == 0)
525 && domain == PF_INET) {
526 /* It's a raw IP socket: set the IP_HDRINCL option. */
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));
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
541 if (domain == PF_INET6) {
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));
551 return (retval_socket);
555 linux_bind(struct thread *td, struct linux_bind_args *args)
560 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
565 error = kern_bindat(td, AT_FDCWD, args->s, sa);
569 if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
575 linux_connect(struct thread *td, struct linux_connect_args *args)
583 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
588 error = kern_connectat(td, AT_FDCWD, args->s, sa);
590 if (error != EISCONN)
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.
598 error = getsock_cap(td, args->s, &cap_connect_rights,
605 if (fflag & FNONBLOCK) {
607 if (so->so_emuldata == 0)
608 error = so->so_error;
609 so->so_emuldata = (void *)1;
618 linux_listen(struct thread *td, struct linux_listen_args *args)
621 return (kern_listen(td, args->s, args->backlog));
625 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
626 l_uintptr_t namelen, int flags)
629 struct file *fp, *fp1;
638 error = linux_set_socket_flags(flags, &bflags);
642 if (PTRIN(addr) == NULL) {
644 error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
646 error = copyin(PTRIN(namelen), &len, sizeof(len));
651 error = kern_accept4(td, s, &sa, &len, bflags, &fp);
655 * Translate errno values into ones used by Linux.
659 * XXX. This is wrong, different sockaddr structures
660 * have different sizes.
664 if (namelen != sizeof(struct sockaddr_in))
668 error1 = getsock_cap(td, s, &cap_accept_rights, &fp1, NULL, NULL);
674 if (so->so_type == SOCK_DGRAM)
683 error = linux_copyout_sockaddr(sa, PTRIN(addr), len);
686 * XXX: We should also copyout the len, shouldn't we?
690 fdclose(td, fp, td->td_retval[0]);
691 td->td_retval[0] = 0;
701 linux_accept(struct thread *td, struct linux_accept_args *args)
704 return (linux_accept_common(td, args->s, args->addr,
709 linux_accept4(struct thread *td, struct linux_accept4_args *args)
712 return (linux_accept_common(td, args->s, args->addr,
713 args->namelen, args->flags));
717 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
722 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
726 error = kern_getsockname(td, args->s, &sa, &len);
731 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
735 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
740 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
745 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
751 error = kern_getpeername(td, args->s, &sa, &len);
756 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
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)
903 if (PTRIN(args->fromlen) != NULL) {
904 error = copyin(PTRIN(args->fromlen), &fromlen,
910 sa = malloc(fromlen, M_SONAME, M_WAITOK);
917 msg.msg_namelen = fromlen;
920 aiov.iov_base = PTRIN(args->buf);
921 aiov.iov_len = args->len;
923 msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
925 error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
929 if (PTRIN(args->from) != NULL)
930 error = linux_copyout_sockaddr(sa, PTRIN(args->from), msg.msg_namelen);
932 if (error == 0 && PTRIN(args->fromlen) != NULL)
933 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
934 sizeof(msg.msg_namelen));
941 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
944 struct cmsghdr *cmsg;
945 struct mbuf *control;
947 struct l_cmsghdr linux_cmsg;
948 struct l_cmsghdr *ptr_cmsg;
949 struct l_msghdr linux_msghdr;
954 sa_family_t sa_family;
961 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
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.
972 if (PTRIN(linux_msghdr.msg_control) != NULL &&
973 linux_msghdr.msg_controllen == 0)
974 linux_msghdr.msg_control = PTROUT(NULL);
976 error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
980 #ifdef COMPAT_LINUX32
981 error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
984 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
991 error = kern_getsockname(td, s, &sa, &datalen);
994 sa_family = sa->sa_family;
997 if (flags & LINUX_MSG_OOB) {
999 if (sa_family == AF_UNIX)
1002 error = getsock_cap(td, s, &cap_send_rights, &fp,
1007 if (so->so_type != SOCK_STREAM)
1014 if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
1016 control = m_get(M_WAITOK, MT_CONTROL);
1017 MCLGET(control, M_WAITOK);
1018 data = mtod(control, void *);
1021 ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1022 clen = linux_msghdr.msg_controllen;
1024 error = copyin(ptr_cmsg, &linux_cmsg,
1025 sizeof(struct l_cmsghdr));
1030 if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1031 linux_cmsg.cmsg_len > clen)
1034 if (datalen + CMSG_HDRSZ > MCLBYTES)
1038 * Now we support only SCM_RIGHTS and SCM_CRED,
1039 * so return EINVAL in any other cmsg_type
1043 linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
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);
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.
1060 if (sa_family != AF_UNIX)
1063 if (cmsg->cmsg_type == SCM_CREDS) {
1064 len = sizeof(struct cmsgcred);
1065 if (datalen + CMSG_SPACE(len) > MCLBYTES)
1069 * The lower levels will fill in the structure
1071 memset(CMSG_DATA(data), 0, len);
1073 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1074 if (datalen + CMSG_SPACE(len) < datalen ||
1075 datalen + CMSG_SPACE(len) > MCLBYTES)
1078 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1079 CMSG_DATA(data), len);
1084 cmsg->cmsg_len = CMSG_LEN(len);
1085 data = (char *)data + CMSG_SPACE(len);
1086 datalen += CMSG_SPACE(len);
1089 if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
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));
1097 control->m_len = datalen;
1106 error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1116 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1119 return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1124 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1126 struct l_mmsghdr *msg;
1128 int error, datagrams;
1130 if (args->vlen > UIO_MAXIOV)
1131 args->vlen = UIO_MAXIOV;
1133 msg = PTRIN(args->msg);
1135 while (datagrams < args->vlen) {
1136 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1141 retval = td->td_retval[0];
1142 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1149 td->td_retval[0] = datagrams;
1154 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1155 l_uint flags, struct msghdr *msg)
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;
1171 int error, i, fd, fds, *fdp;
1173 error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1177 error = linux_to_bsd_msghdr(msg, &linux_msghdr);
1181 #ifdef COMPAT_LINUX32
1182 error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1185 error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
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);
1196 msg->msg_name = NULL;
1199 uiov = msg->msg_iov;
1201 controlp = (msg->msg_control != NULL) ? &control : NULL;
1202 error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1203 msg->msg_iov = uiov;
1208 * Note that kern_recvit() updates msg->msg_namelen.
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);
1218 error = bsd_to_linux_msghdr(msg, &linux_msghdr);
1222 maxlen = linux_msghdr.msg_controllen;
1223 linux_msghdr.msg_controllen = 0;
1225 linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1227 msg->msg_control = mtod(control, struct cmsghdr *);
1228 msg->msg_controllen = control->m_len;
1230 cm = CMSG_FIRSTHDR(msg);
1231 outbuf = PTRIN(linux_msghdr.msg_control);
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);
1247 data = CMSG_DATA(cm);
1248 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1250 switch (cm->cmsg_type) {
1252 if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1253 fds = datalen / sizeof(int);
1255 for (i = 0; i < fds; i++) {
1257 (void)kern_fcntl(td, fd,
1258 F_SETFD, FD_CLOEXEC);
1265 * Currently LOCAL_CREDS is never in
1266 * effect for Linux so no need to worry
1269 if (datalen != sizeof(*cmcred)) {
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);
1283 if (datalen != sizeof(struct timeval)) {
1287 ftmvl = (struct timeval *)data;
1288 ltmvl.tv_sec = ftmvl->tv_sec;
1289 ltmvl.tv_usec = ftmvl->tv_usec;
1291 datalen = sizeof(ltmvl);
1295 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1300 linux_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1301 m_dispose_extcontrolm(control);
1306 linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1308 error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1311 outbuf += L_CMSG_HDRSZ;
1313 error = copyout(data, outbuf, datalen);
1317 outbuf += LINUX_CMSG_ALIGN(datalen);
1318 outlen += LINUX_CMSG_LEN(datalen);
1320 cm = CMSG_NXTHDR(msg, cm);
1322 linux_msghdr.msg_controllen = outlen;
1326 error = copyout(&linux_msghdr, msghdr, sizeof(linux_msghdr));
1329 if (control != NULL) {
1331 m_dispose_extcontrolm(control);
1335 free(linux_cmsg, M_LINUX);
1342 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1344 struct msghdr bsd_msg;
1346 return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1347 args->flags, &bsd_msg));
1351 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1353 struct l_mmsghdr *msg;
1354 struct msghdr bsd_msg;
1355 struct l_timespec lts;
1356 struct timespec ts, tts;
1358 int error, datagrams;
1360 if (args->timeout) {
1361 error = copyin(args->timeout, <s, sizeof(struct l_timespec));
1364 error = linux_to_native_timespec(&ts, <s);
1368 timespecadd(&tts, &ts, &tts);
1371 msg = PTRIN(args->msg);
1373 while (datagrams < args->vlen) {
1374 error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1375 args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1379 retval = td->td_retval[0];
1380 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1387 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1389 if (args->flags & LINUX_MSG_WAITFORONE)
1390 args->flags |= LINUX_MSG_DONTWAIT;
1393 * See BUGS section of recvmmsg(2).
1395 if (args->timeout) {
1397 timespecsub(&ts, &tts, &ts);
1398 if (!timespecisset(&ts) || ts.tv_sec > 0)
1401 /* Out of band data, return right away. */
1402 if (bsd_msg.msg_flags & MSG_OOB)
1406 td->td_retval[0] = datagrams;
1411 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1414 return (kern_shutdown(td, args->s, args->how));
1418 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1421 struct sockaddr *sa;
1424 int error, level, name;
1426 level = linux_to_bsd_sockopt_level(args->level);
1429 name = linux_to_bsd_so_sockopt(args->optname);
1434 error = copyin(PTRIN(args->optval), &linux_tv,
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)));
1448 if (args->optname == LINUX_IP_RECVERR &&
1449 linux_ignore_ip_recverr) {
1451 * XXX: This is a hack to unbreak DNS resolution
1452 * with glibc 2.30 and above.
1456 name = linux_to_bsd_ip_sockopt(args->optname);
1459 name = linux_to_bsd_ip6_sockopt(args->optname);
1462 name = linux_to_bsd_tcp_sockopt(args->optname);
1469 linux_msg(curthread,
1470 "unsupported setsockopt level %d optname %d",
1471 args->level, args->optname);
1472 return (ENOPROTOOPT);
1475 if (name == IPV6_NEXTHOP) {
1477 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
1481 error = kern_setsockopt(td, args->s, level,
1482 name, sa, UIO_SYSSPACE, len);
1485 error = kern_setsockopt(td, args->s, level,
1486 name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
1493 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1497 socklen_t tv_len, xulen, len;
1498 struct sockaddr *sa;
1501 int error, level, name, newval;
1503 level = linux_to_bsd_sockopt_level(args->level);
1506 name = linux_to_bsd_so_sockopt(args->optname);
1511 tv_len = sizeof(tv);
1512 error = kern_getsockopt(td, args->s, level,
1513 name, &tv, UIO_SYSSPACE, &tv_len);
1516 linux_tv.tv_sec = tv.tv_sec;
1517 linux_tv.tv_usec = tv.tv_usec;
1518 return (copyout(&linux_tv, PTRIN(args->optval),
1521 case LOCAL_PEERCRED:
1522 if (args->optlen < sizeof(lxu))
1525 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
1526 * but by the Unix socket's level 0.
1530 error = kern_getsockopt(td, args->s, level,
1531 name, &xu, UIO_SYSSPACE, &xulen);
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)));
1540 len = sizeof(newval);
1541 error = kern_getsockopt(td, args->s, level,
1542 name, &newval, UIO_SYSSPACE, &len);
1545 newval = -linux_to_bsd_errno(newval);
1546 return (copyout(&newval, PTRIN(args->optval), len));
1553 name = linux_to_bsd_ip_sockopt(args->optname);
1556 name = linux_to_bsd_ip6_sockopt(args->optname);
1559 name = linux_to_bsd_tcp_sockopt(args->optname);
1566 linux_msg(curthread,
1567 "unsupported getsockopt level %d optname %d",
1568 args->level, args->optname);
1572 if (name == IPV6_NEXTHOP) {
1573 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1576 sa = malloc(len, M_SONAME, M_WAITOK);
1578 error = kern_getsockopt(td, args->s, level,
1579 name, sa, UIO_SYSSPACE, &len);
1583 error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
1585 error = copyout(&len, PTRIN(args->optlen),
1591 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1595 error = kern_getsockopt(td, args->s, level,
1596 name, PTRIN(args->optval), UIO_USERSPACE, &len);
1598 error = copyout(&len, PTRIN(args->optlen),
1606 linux_sendfile_common(struct thread *td, l_int out, l_int in,
1607 l_loff_t *offset, l_size_t count)
1611 l_loff_t current_offset;
1615 error = fget_read(td, in, &cap_pread_rights, &fp);
1619 if (offset != NULL) {
1620 current_offset = *offset;
1622 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1623 fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
1626 current_offset = td->td_uretoff.tdu_off;
1631 /* Linux cannot have 0 count. */
1632 if (count <= 0 || current_offset < 0) {
1637 error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
1638 &bytes_read, 0, td);
1641 current_offset += bytes_read;
1643 if (offset != NULL) {
1644 *offset = current_offset;
1646 error = fo_seek(fp, current_offset, SEEK_SET, td);
1651 td->td_retval[0] = (ssize_t)bytes_read;
1658 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
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.
1681 if (arg->offset != NULL) {
1682 error = copyin(arg->offset, &offset, sizeof(offset));
1685 offset64 = (l_loff_t)offset;
1688 ret = linux_sendfile_common(td, arg->out, arg->in,
1689 arg->offset != NULL ? &offset64 : NULL, arg->count);
1691 if (arg->offset != NULL) {
1692 #if defined(__i386__) || defined(__arm__) || \
1693 (defined(__amd64__) && defined(COMPAT_LINUX32))
1694 if (offset64 > INT32_MAX)
1697 offset = (l_long)offset64;
1698 error = copyout(&offset, arg->offset, sizeof(offset));
1706 #if defined(__i386__) || defined(__arm__) || \
1707 (defined(__amd64__) && defined(COMPAT_LINUX32))
1710 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
1716 if (arg->offset != NULL) {
1717 error = copyin(arg->offset, &offset, sizeof(offset));
1722 ret = linux_sendfile_common(td, arg->out, arg->in,
1723 arg->offset != NULL ? &offset : NULL, arg->count);
1725 if (arg->offset != NULL) {
1726 error = copyout(&offset, arg->offset, sizeof(offset));
1734 /* Argument list sizes for linux_socketcall */
1735 static 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 */
1748 #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1)
1749 #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong))
1752 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1755 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1756 register_t l_args[6];
1761 if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
1763 error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
1767 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1768 for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
1774 switch (args->what) {
1776 return (linux_socket(td, arg));
1778 return (linux_bind(td, arg));
1780 return (linux_connect(td, arg));
1782 return (linux_listen(td, arg));
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));
1792 return (linux_send(td, arg));
1794 return (linux_recv(td, arg));
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));
1806 return (linux_sendmsg(td, arg));
1808 return (linux_recvmsg(td, arg));
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));
1819 linux_msg(td, "socket type %d not implemented", args->what);
1822 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */