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/limits.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
48 #include <sys/socket.h>
49 #include <sys/socketvar.h>
50 #include <sys/syscallsubr.h>
53 #include <sys/syslog.h>
55 #include <sys/unistd.h>
57 #include <security/audit/audit.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <netinet/tcp.h>
66 #include <netinet/ip6.h>
67 #include <netinet6/ip6_var.h>
71 #include <machine/../linux32/linux.h>
72 #include <machine/../linux32/linux32_proto.h>
74 #include <machine/../linux/linux.h>
75 #include <machine/../linux/linux_proto.h>
77 #include <compat/linux/linux_common.h>
78 #include <compat/linux/linux_file.h>
79 #include <compat/linux/linux_mib.h>
80 #include <compat/linux/linux_socket.h>
81 #include <compat/linux/linux_timer.h>
82 #include <compat/linux/linux_util.h>
84 static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *,
86 static int linux_recvmsg_common(struct thread *, l_int, struct l_msghdr *,
87 l_uint, struct msghdr *);
88 static int linux_set_socket_flags(int, int *);
92 linux_to_bsd_sockopt_level(int level)
96 case LINUX_SOL_SOCKET:
103 bsd_to_linux_sockopt_level(int level)
108 return (LINUX_SOL_SOCKET);
114 linux_to_bsd_ip_sockopt(int opt)
122 case LINUX_IP_OPTIONS:
124 case LINUX_IP_MULTICAST_IF:
125 return (IP_MULTICAST_IF);
126 case LINUX_IP_MULTICAST_TTL:
127 return (IP_MULTICAST_TTL);
128 case LINUX_IP_MULTICAST_LOOP:
129 return (IP_MULTICAST_LOOP);
130 case LINUX_IP_ADD_MEMBERSHIP:
131 return (IP_ADD_MEMBERSHIP);
132 case LINUX_IP_DROP_MEMBERSHIP:
133 return (IP_DROP_MEMBERSHIP);
134 case LINUX_IP_HDRINCL:
141 linux_to_bsd_ip6_sockopt(int opt)
145 case LINUX_IPV6_NEXTHOP:
146 return (IPV6_NEXTHOP);
147 case LINUX_IPV6_UNICAST_HOPS:
148 return (IPV6_UNICAST_HOPS);
149 case LINUX_IPV6_MULTICAST_IF:
150 return (IPV6_MULTICAST_IF);
151 case LINUX_IPV6_MULTICAST_HOPS:
152 return (IPV6_MULTICAST_HOPS);
153 case LINUX_IPV6_MULTICAST_LOOP:
154 return (IPV6_MULTICAST_LOOP);
155 case LINUX_IPV6_ADD_MEMBERSHIP:
156 return (IPV6_JOIN_GROUP);
157 case LINUX_IPV6_DROP_MEMBERSHIP:
158 return (IPV6_LEAVE_GROUP);
159 case LINUX_IPV6_V6ONLY:
160 return (IPV6_V6ONLY);
161 case LINUX_IPV6_DONTFRAG:
162 return (IPV6_DONTFRAG);
164 case LINUX_IPV6_CHECKSUM:
165 return (IPV6_CHECKSUM);
166 case LINUX_IPV6_RECVPKTINFO:
167 return (IPV6_RECVPKTINFO);
168 case LINUX_IPV6_PKTINFO:
169 return (IPV6_PKTINFO);
170 case LINUX_IPV6_RECVHOPLIMIT:
171 return (IPV6_RECVHOPLIMIT);
172 case LINUX_IPV6_HOPLIMIT:
173 return (IPV6_HOPLIMIT);
174 case LINUX_IPV6_RECVHOPOPTS:
175 return (IPV6_RECVHOPOPTS);
176 case LINUX_IPV6_HOPOPTS:
177 return (IPV6_HOPOPTS);
178 case LINUX_IPV6_RTHDRDSTOPTS:
179 return (IPV6_RTHDRDSTOPTS);
180 case LINUX_IPV6_RECVRTHDR:
181 return (IPV6_RECVRTHDR);
182 case LINUX_IPV6_RTHDR:
184 case LINUX_IPV6_RECVDSTOPTS:
185 return (IPV6_RECVDSTOPTS);
186 case LINUX_IPV6_DSTOPTS:
187 return (IPV6_DSTOPTS);
188 case LINUX_IPV6_RECVPATHMTU:
189 return (IPV6_RECVPATHMTU);
190 case LINUX_IPV6_PATHMTU:
191 return (IPV6_PATHMTU);
198 linux_to_bsd_so_sockopt(int opt)
204 case LINUX_SO_REUSEADDR:
205 return (SO_REUSEADDR);
210 case LINUX_SO_DONTROUTE:
211 return (SO_DONTROUTE);
212 case LINUX_SO_BROADCAST:
213 return (SO_BROADCAST);
214 case LINUX_SO_SNDBUF:
216 case LINUX_SO_RCVBUF:
218 case LINUX_SO_KEEPALIVE:
219 return (SO_KEEPALIVE);
220 case LINUX_SO_OOBINLINE:
221 return (SO_OOBINLINE);
222 case LINUX_SO_LINGER:
224 case LINUX_SO_PEERCRED:
225 return (LOCAL_PEERCRED);
226 case LINUX_SO_RCVLOWAT:
227 return (SO_RCVLOWAT);
228 case LINUX_SO_SNDLOWAT:
229 return (SO_SNDLOWAT);
230 case LINUX_SO_RCVTIMEO:
231 return (SO_RCVTIMEO);
232 case LINUX_SO_SNDTIMEO:
233 return (SO_SNDTIMEO);
234 case LINUX_SO_TIMESTAMP:
235 return (SO_TIMESTAMP);
236 case LINUX_SO_ACCEPTCONN:
237 return (SO_ACCEPTCONN);
243 linux_to_bsd_tcp_sockopt(int opt)
247 case LINUX_TCP_NODELAY:
248 return (TCP_NODELAY);
249 case LINUX_TCP_MAXSEG:
253 case LINUX_TCP_KEEPIDLE:
254 return (TCP_KEEPIDLE);
255 case LINUX_TCP_KEEPINTVL:
256 return (TCP_KEEPINTVL);
257 case LINUX_TCP_KEEPCNT:
258 return (TCP_KEEPCNT);
259 case LINUX_TCP_MD5SIG:
266 linux_to_bsd_msg_flags(int flags)
270 if (flags & LINUX_MSG_OOB)
271 ret_flags |= MSG_OOB;
272 if (flags & LINUX_MSG_PEEK)
273 ret_flags |= MSG_PEEK;
274 if (flags & LINUX_MSG_DONTROUTE)
275 ret_flags |= MSG_DONTROUTE;
276 if (flags & LINUX_MSG_CTRUNC)
277 ret_flags |= MSG_CTRUNC;
278 if (flags & LINUX_MSG_TRUNC)
279 ret_flags |= MSG_TRUNC;
280 if (flags & LINUX_MSG_DONTWAIT)
281 ret_flags |= MSG_DONTWAIT;
282 if (flags & LINUX_MSG_EOR)
283 ret_flags |= MSG_EOR;
284 if (flags & LINUX_MSG_WAITALL)
285 ret_flags |= MSG_WAITALL;
286 if (flags & LINUX_MSG_NOSIGNAL)
287 ret_flags |= MSG_NOSIGNAL;
288 #if 0 /* not handled */
289 if (flags & LINUX_MSG_PROXY)
291 if (flags & LINUX_MSG_FIN)
293 if (flags & LINUX_MSG_SYN)
295 if (flags & LINUX_MSG_CONFIRM)
297 if (flags & LINUX_MSG_RST)
299 if (flags & LINUX_MSG_ERRQUEUE)
306 linux_to_bsd_cmsg_type(int cmsg_type)
310 case LINUX_SCM_RIGHTS:
312 case LINUX_SCM_CREDENTIALS:
319 bsd_to_linux_cmsg_type(int cmsg_type)
324 return (LINUX_SCM_RIGHTS);
326 return (LINUX_SCM_CREDENTIALS);
328 return (LINUX_SCM_TIMESTAMP);
334 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
336 if (lhdr->msg_controllen > INT_MAX)
339 bhdr->msg_name = PTRIN(lhdr->msg_name);
340 bhdr->msg_namelen = lhdr->msg_namelen;
341 bhdr->msg_iov = PTRIN(lhdr->msg_iov);
342 bhdr->msg_iovlen = lhdr->msg_iovlen;
343 bhdr->msg_control = PTRIN(lhdr->msg_control);
346 * msg_controllen is skipped since BSD and LINUX control messages
347 * are potentially different sizes (e.g. the cred structure used
348 * by SCM_CREDS is different between the two operating system).
350 * The caller can set it (if necessary) after converting all the
354 bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags);
359 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
361 lhdr->msg_name = PTROUT(bhdr->msg_name);
362 lhdr->msg_namelen = bhdr->msg_namelen;
363 lhdr->msg_iov = PTROUT(bhdr->msg_iov);
364 lhdr->msg_iovlen = bhdr->msg_iovlen;
365 lhdr->msg_control = PTROUT(bhdr->msg_control);
368 * msg_controllen is skipped since BSD and LINUX control messages
369 * are potentially different sizes (e.g. the cred structure used
370 * by SCM_CREDS is different between the two operating system).
372 * The caller can set it (if necessary) after converting all the
376 /* msg_flags skipped */
381 linux_set_socket_flags(int lflags, int *flags)
384 if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
386 if (lflags & LINUX_SOCK_NONBLOCK)
387 *flags |= SOCK_NONBLOCK;
388 if (lflags & LINUX_SOCK_CLOEXEC)
389 *flags |= SOCK_CLOEXEC;
394 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
395 struct mbuf *control, enum uio_seg segflg)
400 if (mp->msg_name != NULL) {
401 len = mp->msg_namelen;
402 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
409 error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
417 /* Return 0 if IP_HDRINCL is set for the given socket. */
419 linux_check_hdrincl(struct thread *td, int s)
424 size_val = sizeof(optval);
425 error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
426 &optval, UIO_SYSSPACE, &size_val);
430 return (optval == 0);
434 * Updated sendto() when IP_HDRINCL is set:
435 * tweak endian-dependent fields in the IP packet.
438 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
441 * linux_ip_copysize defines how many bytes we should copy
442 * from the beginning of the IP packet before we customize it for BSD.
443 * It should include all the fields we modify (ip_len and ip_off).
445 #define linux_ip_copysize 8
449 struct iovec aiov[1];
452 /* Check that the packet isn't too big or too small. */
453 if (linux_args->len < linux_ip_copysize ||
454 linux_args->len > IP_MAXPACKET)
457 packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
459 /* Make kernel copy of the packet to be sent */
460 if ((error = copyin(PTRIN(linux_args->msg), packet,
464 /* Convert fields from Linux to BSD raw IP socket format */
465 packet->ip_len = linux_args->len;
466 packet->ip_off = ntohs(packet->ip_off);
468 /* Prepare the msghdr and iovec structures describing the new packet */
469 msg.msg_name = PTRIN(linux_args->to);
470 msg.msg_namelen = linux_args->tolen;
473 msg.msg_control = NULL;
475 aiov[0].iov_base = (char *)packet;
476 aiov[0].iov_len = linux_args->len;
477 error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
480 free(packet, M_LINUX);
485 linux_socket(struct thread *td, struct linux_socket_args *args)
487 int domain, retval_socket, type;
489 type = args->type & LINUX_SOCK_TYPE_MASK;
490 if (type < 0 || type > LINUX_SOCK_MAX)
492 retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
494 if (retval_socket != 0)
495 return (retval_socket);
496 domain = linux_to_bsd_domain(args->domain);
498 return (EAFNOSUPPORT);
500 retval_socket = kern_socket(td, domain, type, args->protocol);
502 return (retval_socket);
505 && (args->protocol == IPPROTO_RAW || args->protocol == 0)
506 && domain == PF_INET) {
507 /* It's a raw IP socket: set the IP_HDRINCL option. */
511 /* We ignore any error returned by kern_setsockopt() */
512 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
513 &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
517 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
518 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
519 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
522 if (domain == PF_INET6) {
526 /* We ignore any error returned by setsockopt() */
527 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
528 &v6only, UIO_SYSSPACE, sizeof(v6only));
532 return (retval_socket);
536 linux_bind(struct thread *td, struct linux_bind_args *args)
541 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
546 error = kern_bindat(td, AT_FDCWD, args->s, sa);
550 if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
556 linux_connect(struct thread *td, struct linux_connect_args *args)
564 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
569 error = kern_connectat(td, AT_FDCWD, args->s, sa);
571 if (error != EISCONN)
575 * Linux doesn't return EISCONN the first time it occurs,
576 * when on a non-blocking socket. Instead it returns the
577 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
579 error = getsock_cap(td, args->s, &cap_connect_rights,
586 if (fflag & FNONBLOCK) {
588 if (so->so_emuldata == 0)
589 error = so->so_error;
590 so->so_emuldata = (void *)1;
599 linux_listen(struct thread *td, struct linux_listen_args *args)
602 return (kern_listen(td, args->s, args->backlog));
606 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
607 l_uintptr_t namelen, int flags)
609 struct l_sockaddr *lsa;
617 error = linux_set_socket_flags(flags, &bflags);
622 if (PTRIN(addr) == NULL) {
624 error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
626 error = copyin(PTRIN(namelen), &len, sizeof(len));
631 error = kern_accept4(td, s, &sa, &len, bflags, &fp);
638 * XXX. This is wrong, different sockaddr structures
639 * have different sizes.
641 if (error == EFAULT && namelen != sizeof(struct sockaddr_in))
646 if (error == EINVAL) {
647 error1 = getsock_cap(td, s, &cap_accept_rights, &fp, NULL, NULL);
653 if (so->so_type == SOCK_DGRAM)
660 if (len != 0 && error == 0) {
661 error = bsd_to_linux_sockaddr(sa, &lsa, len);
663 error = copyout(lsa, PTRIN(addr), len);
671 (void)kern_close(td, td->td_retval[0]);
672 td->td_retval[0] = 0;
678 linux_accept(struct thread *td, struct linux_accept_args *args)
681 return (linux_accept_common(td, args->s, args->addr,
686 linux_accept4(struct thread *td, struct linux_accept4_args *args)
689 return (linux_accept_common(td, args->s, args->addr,
690 args->namelen, args->flags));
694 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
696 struct l_sockaddr *lsa;
700 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
704 error = kern_getsockname(td, args->s, &sa, &len);
709 error = bsd_to_linux_sockaddr(sa, &lsa, len);
711 error = copyout(lsa, PTRIN(args->addr),
718 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
723 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
725 struct l_sockaddr *lsa;
729 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
735 error = kern_getpeername(td, args->s, &sa, &len);
740 error = bsd_to_linux_sockaddr(sa, &lsa, len);
742 error = copyout(lsa, PTRIN(args->addr),
749 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
754 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
756 int domain, error, sv[2], type;
758 domain = linux_to_bsd_domain(args->domain);
759 if (domain != PF_LOCAL)
760 return (EAFNOSUPPORT);
761 type = args->type & LINUX_SOCK_TYPE_MASK;
762 if (type < 0 || type > LINUX_SOCK_MAX)
764 error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
768 if (args->protocol != 0 && args->protocol != PF_UNIX) {
771 * Use of PF_UNIX as protocol argument is not right,
773 * Do not map PF_UNIX as its Linux value is identical
776 return (EPROTONOSUPPORT);
778 error = kern_socketpair(td, domain, type, 0, sv);
781 error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
783 (void)kern_close(td, sv[0]);
784 (void)kern_close(td, sv[1]);
789 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
790 struct linux_send_args {
798 linux_send(struct thread *td, struct linux_send_args *args)
800 struct sendto_args /* {
811 bsd_args.s = args->s;
812 bsd_args.buf = (caddr_t)PTRIN(args->msg);
813 bsd_args.len = args->len;
814 bsd_args.flags = args->flags;
817 error = sys_sendto(td, &bsd_args);
818 if (error == ENOTCONN) {
820 * Linux doesn't return ENOTCONN for non-blocking sockets.
821 * Instead it returns the EAGAIN.
823 error = getsock_cap(td, args->s, &cap_send_rights, &fp,
826 if (fflag & FNONBLOCK)
834 struct linux_recv_args {
842 linux_recv(struct thread *td, struct linux_recv_args *args)
844 struct recvfrom_args /* {
849 struct sockaddr *from;
850 socklen_t fromlenaddr;
853 bsd_args.s = args->s;
854 bsd_args.buf = (caddr_t)PTRIN(args->msg);
855 bsd_args.len = args->len;
856 bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
857 bsd_args.from = NULL;
858 bsd_args.fromlenaddr = 0;
859 return (sys_recvfrom(td, &bsd_args));
861 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
864 linux_sendto(struct thread *td, struct linux_sendto_args *args)
869 if (linux_check_hdrincl(td, args->s) == 0)
870 /* IP_HDRINCL set, tweak the packet before sending */
871 return (linux_sendto_hdrincl(td, args));
873 msg.msg_name = PTRIN(args->to);
874 msg.msg_namelen = args->tolen;
877 msg.msg_control = NULL;
879 aiov.iov_base = PTRIN(args->msg);
880 aiov.iov_len = args->len;
881 return (linux_sendit(td, args->s, &msg, args->flags, NULL,
886 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
888 struct l_sockaddr *lsa;
894 if (PTRIN(args->fromlen) != NULL) {
895 error = copyin(PTRIN(args->fromlen), &fromlen,
901 sa = malloc(fromlen, M_SONAME, M_WAITOK);
908 msg.msg_namelen = fromlen;
911 aiov.iov_base = PTRIN(args->buf);
912 aiov.iov_len = args->len;
914 msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
916 error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
920 if (PTRIN(args->from) != NULL) {
921 error = bsd_to_linux_sockaddr(sa, &lsa, msg.msg_namelen);
923 error = copyout(lsa, PTRIN(args->from),
928 if (error == 0 && PTRIN(args->fromlen) != NULL)
929 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
930 sizeof(msg.msg_namelen));
937 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
940 struct cmsghdr *cmsg;
941 struct mbuf *control;
943 struct l_cmsghdr linux_cmsg;
944 struct l_cmsghdr *ptr_cmsg;
945 struct l_msghdr linux_msg;
950 sa_family_t sa_family;
957 error = copyin(msghdr, &linux_msg, sizeof(linux_msg));
962 * Some Linux applications (ping) define a non-NULL control data
963 * pointer, but a msg_controllen of 0, which is not allowed in the
964 * FreeBSD system call interface. NULL the msg_control pointer in
965 * order to handle this case. This should be checked, but allows the
966 * Linux ping to work.
968 if (PTRIN(linux_msg.msg_control) != NULL && linux_msg.msg_controllen == 0)
969 linux_msg.msg_control = PTROUT(NULL);
971 error = linux_to_bsd_msghdr(&msg, &linux_msg);
975 #ifdef COMPAT_LINUX32
976 error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
979 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
986 error = kern_getsockname(td, s, &sa, &datalen);
989 sa_family = sa->sa_family;
992 if (flags & LINUX_MSG_OOB) {
994 if (sa_family == AF_UNIX)
997 error = getsock_cap(td, s, &cap_send_rights, &fp,
1002 if (so->so_type != SOCK_STREAM)
1009 if (linux_msg.msg_controllen >= sizeof(struct l_cmsghdr)) {
1012 control = m_get(M_WAITOK, MT_CONTROL);
1013 MCLGET(control, M_WAITOK);
1014 data = mtod(control, void *);
1017 ptr_cmsg = PTRIN(linux_msg.msg_control);
1018 clen = linux_msg.msg_controllen;
1020 error = copyin(ptr_cmsg, &linux_cmsg,
1021 sizeof(struct l_cmsghdr));
1026 if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1027 linux_cmsg.cmsg_len > clen)
1030 if (datalen + CMSG_HDRSZ > MCLBYTES)
1034 * Now we support only SCM_RIGHTS and SCM_CRED,
1035 * so return EINVAL in any other cmsg_type
1039 linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1041 linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1042 if (cmsg->cmsg_type == -1
1043 || cmsg->cmsg_level != SOL_SOCKET)
1047 * Some applications (e.g. pulseaudio) attempt to
1048 * send ancillary data even if the underlying protocol
1049 * doesn't support it which is not allowed in the
1050 * FreeBSD system call interface.
1052 if (sa_family != AF_UNIX)
1055 if (cmsg->cmsg_type == SCM_CREDS) {
1056 len = sizeof(struct cmsgcred);
1057 if (datalen + CMSG_SPACE(len) > MCLBYTES)
1061 * The lower levels will fill in the structure
1063 memset(CMSG_DATA(data), 0, len);
1065 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1066 if (datalen + CMSG_SPACE(len) < datalen ||
1067 datalen + CMSG_SPACE(len) > MCLBYTES)
1070 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1071 CMSG_DATA(data), len);
1076 cmsg->cmsg_len = CMSG_LEN(len);
1077 data = (char *)data + CMSG_SPACE(len);
1078 datalen += CMSG_SPACE(len);
1080 if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1083 clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1084 ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1085 LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1086 } while(clen >= sizeof(struct l_cmsghdr));
1088 control->m_len = datalen;
1097 error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1107 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1110 return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1115 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1117 struct l_mmsghdr *msg;
1119 int error, datagrams;
1121 if (args->vlen > UIO_MAXIOV)
1122 args->vlen = UIO_MAXIOV;
1124 msg = PTRIN(args->msg);
1126 while (datagrams < args->vlen) {
1127 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1132 retval = td->td_retval[0];
1133 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1140 td->td_retval[0] = datagrams;
1145 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1146 l_uint flags, struct msghdr *msg)
1149 struct cmsgcred *cmcred;
1150 struct l_cmsghdr *linux_cmsg = NULL;
1151 struct l_ucred linux_ucred;
1152 socklen_t datalen, maxlen, outlen;
1153 struct l_msghdr linux_msg;
1154 struct iovec *iov, *uiov;
1155 struct mbuf *control = NULL;
1156 struct mbuf **controlp;
1157 struct timeval *ftmvl;
1158 struct l_sockaddr *lsa;
1159 struct sockaddr *sa;
1163 int error, i, fd, fds, *fdp;
1165 error = copyin(msghdr, &linux_msg, sizeof(linux_msg));
1169 error = linux_to_bsd_msghdr(msg, &linux_msg);
1173 #ifdef COMPAT_LINUX32
1174 error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1177 error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1182 if (msg->msg_name) {
1183 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1188 uiov = msg->msg_iov;
1190 controlp = (msg->msg_control != NULL) ? &control : NULL;
1191 error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1192 msg->msg_iov = uiov;
1196 if (msg->msg_name) {
1197 msg->msg_name = PTRIN(linux_msg.msg_name);
1198 error = bsd_to_linux_sockaddr(sa, &lsa, msg->msg_namelen);
1200 error = copyout(lsa, PTRIN(msg->msg_name),
1202 free(lsa, M_SONAME);
1207 error = bsd_to_linux_msghdr(msg, &linux_msg);
1211 maxlen = linux_msg.msg_controllen;
1212 linux_msg.msg_controllen = 0;
1214 linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1216 msg->msg_control = mtod(control, struct cmsghdr *);
1217 msg->msg_controllen = control->m_len;
1219 cm = CMSG_FIRSTHDR(msg);
1220 outbuf = PTRIN(linux_msg.msg_control);
1222 while (cm != NULL) {
1223 linux_cmsg->cmsg_type =
1224 bsd_to_linux_cmsg_type(cm->cmsg_type);
1225 linux_cmsg->cmsg_level =
1226 bsd_to_linux_sockopt_level(cm->cmsg_level);
1227 if (linux_cmsg->cmsg_type == -1 ||
1228 cm->cmsg_level != SOL_SOCKET) {
1233 data = CMSG_DATA(cm);
1234 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1236 switch (cm->cmsg_type) {
1238 if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1239 fds = datalen / sizeof(int);
1241 for (i = 0; i < fds; i++) {
1243 (void)kern_fcntl(td, fd,
1244 F_SETFD, FD_CLOEXEC);
1251 * Currently LOCAL_CREDS is never in
1252 * effect for Linux so no need to worry
1255 if (datalen != sizeof(*cmcred)) {
1259 cmcred = (struct cmsgcred *)data;
1260 bzero(&linux_ucred, sizeof(linux_ucred));
1261 linux_ucred.pid = cmcred->cmcred_pid;
1262 linux_ucred.uid = cmcred->cmcred_uid;
1263 linux_ucred.gid = cmcred->cmcred_gid;
1264 data = &linux_ucred;
1265 datalen = sizeof(linux_ucred);
1269 if (datalen != sizeof(struct timeval)) {
1273 ftmvl = (struct timeval *)data;
1274 ltmvl.tv_sec = ftmvl->tv_sec;
1275 ltmvl.tv_usec = ftmvl->tv_usec;
1277 datalen = sizeof(ltmvl);
1281 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1286 linux_msg.msg_flags |= LINUX_MSG_CTRUNC;
1287 m_dispose_extcontrolm(control);
1292 linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1294 error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1297 outbuf += L_CMSG_HDRSZ;
1299 error = copyout(data, outbuf, datalen);
1303 outbuf += LINUX_CMSG_ALIGN(datalen);
1304 outlen += LINUX_CMSG_LEN(datalen);
1306 cm = CMSG_NXTHDR(msg, cm);
1308 linux_msg.msg_controllen = outlen;
1312 error = copyout(&linux_msg, msghdr, sizeof(linux_msg));
1315 if (control != NULL) {
1317 m_dispose_extcontrolm(control);
1321 free(linux_cmsg, M_LINUX);
1328 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1330 struct msghdr bsd_msg;
1332 return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1333 args->flags, &bsd_msg));
1337 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1339 struct l_mmsghdr *msg;
1340 struct msghdr bsd_msg;
1341 struct l_timespec lts;
1342 struct timespec ts, tts;
1344 int error, datagrams;
1346 if (args->timeout) {
1347 error = copyin(args->timeout, <s, sizeof(struct l_timespec));
1350 error = linux_to_native_timespec(&ts, <s);
1354 timespecadd(&tts, &ts, &tts);
1357 msg = PTRIN(args->msg);
1359 while (datagrams < args->vlen) {
1360 error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1361 args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1365 retval = td->td_retval[0];
1366 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1373 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1375 if (args->flags & LINUX_MSG_WAITFORONE)
1376 args->flags |= LINUX_MSG_DONTWAIT;
1379 * See BUGS section of recvmmsg(2).
1381 if (args->timeout) {
1383 timespecsub(&ts, &tts, &ts);
1384 if (!timespecisset(&ts) || ts.tv_sec > 0)
1387 /* Out of band data, return right away. */
1388 if (bsd_msg.msg_flags & MSG_OOB)
1392 td->td_retval[0] = datagrams;
1397 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1400 return (kern_shutdown(td, args->s, args->how));
1404 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1407 struct sockaddr *sa;
1410 int error, level, name;
1412 level = linux_to_bsd_sockopt_level(args->level);
1415 name = linux_to_bsd_so_sockopt(args->optname);
1420 error = copyin(PTRIN(args->optval), &linux_tv,
1424 tv.tv_sec = linux_tv.tv_sec;
1425 tv.tv_usec = linux_tv.tv_usec;
1426 return (kern_setsockopt(td, args->s, level,
1427 name, &tv, UIO_SYSSPACE, sizeof(tv)));
1434 if (args->optname == LINUX_IP_RECVERR &&
1435 linux_ignore_ip_recverr) {
1437 * XXX: This is a hack to unbreak DNS resolution
1438 * with glibc 2.30 and above.
1442 name = linux_to_bsd_ip_sockopt(args->optname);
1445 name = linux_to_bsd_ip6_sockopt(args->optname);
1448 name = linux_to_bsd_tcp_sockopt(args->optname);
1455 return (ENOPROTOOPT);
1458 if (name == IPV6_NEXTHOP) {
1460 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
1464 error = kern_setsockopt(td, args->s, level,
1465 name, sa, UIO_SYSSPACE, len);
1468 error = kern_setsockopt(td, args->s, level,
1469 name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
1476 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1480 socklen_t tv_len, xulen, len;
1481 struct l_sockaddr *lsa;
1482 struct sockaddr *sa;
1485 int error, level, name, newval;
1487 level = linux_to_bsd_sockopt_level(args->level);
1490 name = linux_to_bsd_so_sockopt(args->optname);
1495 tv_len = sizeof(tv);
1496 error = kern_getsockopt(td, args->s, level,
1497 name, &tv, UIO_SYSSPACE, &tv_len);
1500 linux_tv.tv_sec = tv.tv_sec;
1501 linux_tv.tv_usec = tv.tv_usec;
1502 return (copyout(&linux_tv, PTRIN(args->optval),
1505 case LOCAL_PEERCRED:
1506 if (args->optlen < sizeof(lxu))
1509 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
1510 * but by the Unix socket's level 0.
1514 error = kern_getsockopt(td, args->s, level,
1515 name, &xu, UIO_SYSSPACE, &xulen);
1518 lxu.pid = xu.cr_pid;
1519 lxu.uid = xu.cr_uid;
1520 lxu.gid = xu.cr_gid;
1521 return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1524 len = sizeof(newval);
1525 error = kern_getsockopt(td, args->s, level,
1526 name, &newval, UIO_SYSSPACE, &len);
1529 newval = -SV_ABI_ERRNO(td->td_proc, newval);
1530 return (copyout(&newval, PTRIN(args->optval), len));
1537 name = linux_to_bsd_ip_sockopt(args->optname);
1540 name = linux_to_bsd_ip6_sockopt(args->optname);
1543 name = linux_to_bsd_tcp_sockopt(args->optname);
1552 if (name == IPV6_NEXTHOP) {
1553 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1556 sa = malloc(len, M_SONAME, M_WAITOK);
1558 error = kern_getsockopt(td, args->s, level,
1559 name, sa, UIO_SYSSPACE, &len);
1563 error = bsd_to_linux_sockaddr(sa, &lsa, len);
1565 error = copyout(lsa, PTRIN(args->optval), len);
1566 free(lsa, M_SONAME);
1568 error = copyout(&len, PTRIN(args->optlen),
1574 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1578 error = kern_getsockopt(td, args->s, level,
1579 name, PTRIN(args->optval), UIO_USERSPACE, &len);
1581 error = copyout(&len, PTRIN(args->optlen),
1589 linux_sendfile_common(struct thread *td, l_int out, l_int in,
1590 l_loff_t *offset, l_size_t count)
1594 l_loff_t current_offset;
1598 error = fget_read(td, in, &cap_pread_rights, &fp);
1602 if (offset != NULL) {
1603 current_offset = *offset;
1605 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
1606 fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
1609 current_offset = td->td_uretoff.tdu_off;
1614 /* Linux cannot have 0 count. */
1615 if (count <= 0 || current_offset < 0) {
1620 error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
1621 &bytes_read, 0, td);
1624 current_offset += bytes_read;
1626 if (offset != NULL) {
1627 *offset = current_offset;
1629 error = fo_seek(fp, current_offset, SEEK_SET, td);
1634 td->td_retval[0] = (ssize_t)bytes_read;
1641 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
1644 * Differences between FreeBSD and Linux sendfile:
1645 * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
1646 * mean send the whole file.) In linux_sendfile given fds are still
1647 * checked for validity when the count is 0.
1648 * - Linux can send to any fd whereas FreeBSD only supports sockets.
1649 * The same restriction follows for linux_sendfile.
1650 * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
1651 * - Linux takes an offset pointer and updates it to the read location.
1652 * FreeBSD takes in an offset and a 'bytes read' parameter which is
1653 * only filled if it isn't NULL. We use this parameter to update the
1654 * offset pointer if it exists.
1655 * - Linux sendfile returns bytes read on success while FreeBSD
1656 * returns 0. We use the 'bytes read' parameter to get this value.
1664 if (arg->offset != NULL) {
1665 error = copyin(arg->offset, &offset, sizeof(offset));
1668 offset64 = (l_loff_t)offset;
1671 ret = linux_sendfile_common(td, arg->out, arg->in,
1672 arg->offset != NULL ? &offset64 : NULL, arg->count);
1674 if (arg->offset != NULL) {
1675 #if defined(__i386__) || defined(__arm__) || \
1676 (defined(__amd64__) && defined(COMPAT_LINUX32))
1677 if (offset64 > INT32_MAX)
1680 offset = (l_long)offset64;
1681 error = copyout(&offset, arg->offset, sizeof(offset));
1689 #if defined(__i386__) || defined(__arm__) || \
1690 (defined(__amd64__) && defined(COMPAT_LINUX32))
1693 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
1699 if (arg->offset != NULL) {
1700 error = copyin(arg->offset, &offset, sizeof(offset));
1705 ret = linux_sendfile_common(td, arg->out, arg->in,
1706 arg->offset != NULL ? &offset : NULL, arg->count);
1708 if (arg->offset != NULL) {
1709 error = copyout(&offset, arg->offset, sizeof(offset));
1717 /* Argument list sizes for linux_socketcall */
1718 static const unsigned char lxs_args_cnt[] = {
1719 0 /* unused*/, 3 /* socket */,
1720 3 /* bind */, 3 /* connect */,
1721 2 /* listen */, 3 /* accept */,
1722 3 /* getsockname */, 3 /* getpeername */,
1723 4 /* socketpair */, 4 /* send */,
1724 4 /* recv */, 6 /* sendto */,
1725 6 /* recvfrom */, 2 /* shutdown */,
1726 5 /* setsockopt */, 5 /* getsockopt */,
1727 3 /* sendmsg */, 3 /* recvmsg */,
1728 4 /* accept4 */, 5 /* recvmmsg */,
1729 4 /* sendmmsg */, 4 /* sendfile */
1731 #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1)
1732 #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong))
1735 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1738 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1739 register_t l_args[6];
1744 if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
1746 error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
1750 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1751 for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
1757 switch (args->what) {
1759 return (linux_socket(td, arg));
1761 return (linux_bind(td, arg));
1763 return (linux_connect(td, arg));
1765 return (linux_listen(td, arg));
1767 return (linux_accept(td, arg));
1768 case LINUX_GETSOCKNAME:
1769 return (linux_getsockname(td, arg));
1770 case LINUX_GETPEERNAME:
1771 return (linux_getpeername(td, arg));
1772 case LINUX_SOCKETPAIR:
1773 return (linux_socketpair(td, arg));
1775 return (linux_send(td, arg));
1777 return (linux_recv(td, arg));
1779 return (linux_sendto(td, arg));
1780 case LINUX_RECVFROM:
1781 return (linux_recvfrom(td, arg));
1782 case LINUX_SHUTDOWN:
1783 return (linux_shutdown(td, arg));
1784 case LINUX_SETSOCKOPT:
1785 return (linux_setsockopt(td, arg));
1786 case LINUX_GETSOCKOPT:
1787 return (linux_getsockopt(td, arg));
1789 return (linux_sendmsg(td, arg));
1791 return (linux_recvmsg(td, arg));
1793 return (linux_accept4(td, arg));
1794 case LINUX_RECVMMSG:
1795 return (linux_recvmmsg(td, arg));
1796 case LINUX_SENDMMSG:
1797 return (linux_sendmmsg(td, arg));
1798 case LINUX_SENDFILE:
1799 return (linux_sendfile(td, arg));
1802 uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
1805 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */