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>
52 #include <sys/syslog.h>
57 #include <netinet/in.h>
58 #include <netinet/in_systm.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
62 #include <netinet/ip6.h>
63 #include <netinet6/ip6_var.h>
67 #include <machine/../linux32/linux.h>
68 #include <machine/../linux32/linux32_proto.h>
70 #include <machine/../linux/linux.h>
71 #include <machine/../linux/linux_proto.h>
73 #include <compat/linux/linux_common.h>
74 #include <compat/linux/linux_file.h>
75 #include <compat/linux/linux_socket.h>
76 #include <compat/linux/linux_timer.h>
77 #include <compat/linux/linux_util.h>
79 static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *,
81 static int linux_recvmsg_common(struct thread *, l_int, struct l_msghdr *,
82 l_uint, struct msghdr *);
83 static int linux_set_socket_flags(int, int *);
87 linux_to_bsd_sockopt_level(int level)
91 case LINUX_SOL_SOCKET:
98 bsd_to_linux_sockopt_level(int level)
103 return (LINUX_SOL_SOCKET);
109 linux_to_bsd_ip_sockopt(int opt)
117 case LINUX_IP_OPTIONS:
119 case LINUX_IP_MULTICAST_IF:
120 return (IP_MULTICAST_IF);
121 case LINUX_IP_MULTICAST_TTL:
122 return (IP_MULTICAST_TTL);
123 case LINUX_IP_MULTICAST_LOOP:
124 return (IP_MULTICAST_LOOP);
125 case LINUX_IP_ADD_MEMBERSHIP:
126 return (IP_ADD_MEMBERSHIP);
127 case LINUX_IP_DROP_MEMBERSHIP:
128 return (IP_DROP_MEMBERSHIP);
129 case LINUX_IP_HDRINCL:
136 linux_to_bsd_ip6_sockopt(int opt)
140 case LINUX_IPV6_NEXTHOP:
141 return (IPV6_NEXTHOP);
142 case LINUX_IPV6_UNICAST_HOPS:
143 return (IPV6_UNICAST_HOPS);
144 case LINUX_IPV6_MULTICAST_IF:
145 return (IPV6_MULTICAST_IF);
146 case LINUX_IPV6_MULTICAST_HOPS:
147 return (IPV6_MULTICAST_HOPS);
148 case LINUX_IPV6_MULTICAST_LOOP:
149 return (IPV6_MULTICAST_LOOP);
150 case LINUX_IPV6_ADD_MEMBERSHIP:
151 return (IPV6_JOIN_GROUP);
152 case LINUX_IPV6_DROP_MEMBERSHIP:
153 return (IPV6_LEAVE_GROUP);
154 case LINUX_IPV6_V6ONLY:
155 return (IPV6_V6ONLY);
156 case LINUX_IPV6_DONTFRAG:
157 return (IPV6_DONTFRAG);
159 case LINUX_IPV6_CHECKSUM:
160 return (IPV6_CHECKSUM);
161 case LINUX_IPV6_RECVPKTINFO:
162 return (IPV6_RECVPKTINFO);
163 case LINUX_IPV6_PKTINFO:
164 return (IPV6_PKTINFO);
165 case LINUX_IPV6_RECVHOPLIMIT:
166 return (IPV6_RECVHOPLIMIT);
167 case LINUX_IPV6_HOPLIMIT:
168 return (IPV6_HOPLIMIT);
169 case LINUX_IPV6_RECVHOPOPTS:
170 return (IPV6_RECVHOPOPTS);
171 case LINUX_IPV6_HOPOPTS:
172 return (IPV6_HOPOPTS);
173 case LINUX_IPV6_RTHDRDSTOPTS:
174 return (IPV6_RTHDRDSTOPTS);
175 case LINUX_IPV6_RECVRTHDR:
176 return (IPV6_RECVRTHDR);
177 case LINUX_IPV6_RTHDR:
179 case LINUX_IPV6_RECVDSTOPTS:
180 return (IPV6_RECVDSTOPTS);
181 case LINUX_IPV6_DSTOPTS:
182 return (IPV6_DSTOPTS);
183 case LINUX_IPV6_RECVPATHMTU:
184 return (IPV6_RECVPATHMTU);
185 case LINUX_IPV6_PATHMTU:
186 return (IPV6_PATHMTU);
193 linux_to_bsd_so_sockopt(int opt)
199 case LINUX_SO_REUSEADDR:
200 return (SO_REUSEADDR);
205 case LINUX_SO_DONTROUTE:
206 return (SO_DONTROUTE);
207 case LINUX_SO_BROADCAST:
208 return (SO_BROADCAST);
209 case LINUX_SO_SNDBUF:
211 case LINUX_SO_RCVBUF:
213 case LINUX_SO_KEEPALIVE:
214 return (SO_KEEPALIVE);
215 case LINUX_SO_OOBINLINE:
216 return (SO_OOBINLINE);
217 case LINUX_SO_LINGER:
219 case LINUX_SO_PEERCRED:
220 return (LOCAL_PEERCRED);
221 case LINUX_SO_RCVLOWAT:
222 return (SO_RCVLOWAT);
223 case LINUX_SO_SNDLOWAT:
224 return (SO_SNDLOWAT);
225 case LINUX_SO_RCVTIMEO:
226 return (SO_RCVTIMEO);
227 case LINUX_SO_SNDTIMEO:
228 return (SO_SNDTIMEO);
229 case LINUX_SO_TIMESTAMP:
230 return (SO_TIMESTAMP);
231 case LINUX_SO_ACCEPTCONN:
232 return (SO_ACCEPTCONN);
238 linux_to_bsd_tcp_sockopt(int opt)
242 case LINUX_TCP_NODELAY:
243 return (TCP_NODELAY);
244 case LINUX_TCP_MAXSEG:
246 case LINUX_TCP_KEEPIDLE:
247 return (TCP_KEEPIDLE);
248 case LINUX_TCP_KEEPINTVL:
249 return (TCP_KEEPINTVL);
250 case LINUX_TCP_KEEPCNT:
251 return (TCP_KEEPCNT);
252 case LINUX_TCP_MD5SIG:
259 linux_to_bsd_msg_flags(int flags)
263 if (flags & LINUX_MSG_OOB)
264 ret_flags |= MSG_OOB;
265 if (flags & LINUX_MSG_PEEK)
266 ret_flags |= MSG_PEEK;
267 if (flags & LINUX_MSG_DONTROUTE)
268 ret_flags |= MSG_DONTROUTE;
269 if (flags & LINUX_MSG_CTRUNC)
270 ret_flags |= MSG_CTRUNC;
271 if (flags & LINUX_MSG_TRUNC)
272 ret_flags |= MSG_TRUNC;
273 if (flags & LINUX_MSG_DONTWAIT)
274 ret_flags |= MSG_DONTWAIT;
275 if (flags & LINUX_MSG_EOR)
276 ret_flags |= MSG_EOR;
277 if (flags & LINUX_MSG_WAITALL)
278 ret_flags |= MSG_WAITALL;
279 if (flags & LINUX_MSG_NOSIGNAL)
280 ret_flags |= MSG_NOSIGNAL;
281 #if 0 /* not handled */
282 if (flags & LINUX_MSG_PROXY)
284 if (flags & LINUX_MSG_FIN)
286 if (flags & LINUX_MSG_SYN)
288 if (flags & LINUX_MSG_CONFIRM)
290 if (flags & LINUX_MSG_RST)
292 if (flags & LINUX_MSG_ERRQUEUE)
299 linux_to_bsd_cmsg_type(int cmsg_type)
303 case LINUX_SCM_RIGHTS:
305 case LINUX_SCM_CREDENTIALS:
312 bsd_to_linux_cmsg_type(int cmsg_type)
317 return (LINUX_SCM_RIGHTS);
319 return (LINUX_SCM_CREDENTIALS);
321 return (LINUX_SCM_TIMESTAMP);
327 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
329 if (lhdr->msg_controllen > INT_MAX)
332 bhdr->msg_name = PTRIN(lhdr->msg_name);
333 bhdr->msg_namelen = lhdr->msg_namelen;
334 bhdr->msg_iov = PTRIN(lhdr->msg_iov);
335 bhdr->msg_iovlen = lhdr->msg_iovlen;
336 bhdr->msg_control = PTRIN(lhdr->msg_control);
339 * msg_controllen is skipped since BSD and LINUX control messages
340 * are potentially different sizes (e.g. the cred structure used
341 * by SCM_CREDS is different between the two operating system).
343 * The caller can set it (if necessary) after converting all the
347 bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags);
352 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
354 lhdr->msg_name = PTROUT(bhdr->msg_name);
355 lhdr->msg_namelen = bhdr->msg_namelen;
356 lhdr->msg_iov = PTROUT(bhdr->msg_iov);
357 lhdr->msg_iovlen = bhdr->msg_iovlen;
358 lhdr->msg_control = PTROUT(bhdr->msg_control);
361 * msg_controllen is skipped since BSD and LINUX control messages
362 * are potentially different sizes (e.g. the cred structure used
363 * by SCM_CREDS is different between the two operating system).
365 * The caller can set it (if necessary) after converting all the
369 /* msg_flags skipped */
374 linux_set_socket_flags(int lflags, int *flags)
377 if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
379 if (lflags & LINUX_SOCK_NONBLOCK)
380 *flags |= SOCK_NONBLOCK;
381 if (lflags & LINUX_SOCK_CLOEXEC)
382 *flags |= SOCK_CLOEXEC;
387 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
388 struct mbuf *control, enum uio_seg segflg)
393 if (mp->msg_name != NULL) {
394 len = mp->msg_namelen;
395 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
402 error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
410 /* Return 0 if IP_HDRINCL is set for the given socket. */
412 linux_check_hdrincl(struct thread *td, int s)
417 size_val = sizeof(optval);
418 error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
419 &optval, UIO_SYSSPACE, &size_val);
423 return (optval == 0);
427 * Updated sendto() when IP_HDRINCL is set:
428 * tweak endian-dependent fields in the IP packet.
431 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
434 * linux_ip_copysize defines how many bytes we should copy
435 * from the beginning of the IP packet before we customize it for BSD.
436 * It should include all the fields we modify (ip_len and ip_off).
438 #define linux_ip_copysize 8
442 struct iovec aiov[1];
445 /* Check that the packet isn't too big or too small. */
446 if (linux_args->len < linux_ip_copysize ||
447 linux_args->len > IP_MAXPACKET)
450 packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
452 /* Make kernel copy of the packet to be sent */
453 if ((error = copyin(PTRIN(linux_args->msg), packet,
457 /* Convert fields from Linux to BSD raw IP socket format */
458 packet->ip_len = linux_args->len;
459 packet->ip_off = ntohs(packet->ip_off);
461 /* Prepare the msghdr and iovec structures describing the new packet */
462 msg.msg_name = PTRIN(linux_args->to);
463 msg.msg_namelen = linux_args->tolen;
466 msg.msg_control = NULL;
468 aiov[0].iov_base = (char *)packet;
469 aiov[0].iov_len = linux_args->len;
470 error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
473 free(packet, M_LINUX);
478 linux_socket(struct thread *td, struct linux_socket_args *args)
480 int domain, retval_socket, type;
482 type = args->type & LINUX_SOCK_TYPE_MASK;
483 if (type < 0 || type > LINUX_SOCK_MAX)
485 retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
487 if (retval_socket != 0)
488 return (retval_socket);
489 domain = linux_to_bsd_domain(args->domain);
491 return (EAFNOSUPPORT);
493 retval_socket = kern_socket(td, domain, type, args->protocol);
495 return (retval_socket);
498 && (args->protocol == IPPROTO_RAW || args->protocol == 0)
499 && domain == PF_INET) {
500 /* It's a raw IP socket: set the IP_HDRINCL option. */
504 /* We ignore any error returned by kern_setsockopt() */
505 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
506 &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
510 * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
511 * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
512 * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
515 if (domain == PF_INET6) {
519 /* We ignore any error returned by setsockopt() */
520 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
521 &v6only, UIO_SYSSPACE, sizeof(v6only));
525 return (retval_socket);
529 linux_bind(struct thread *td, struct linux_bind_args *args)
534 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
539 error = kern_bindat(td, AT_FDCWD, args->s, sa);
543 if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
549 linux_connect(struct thread *td, struct linux_connect_args *args)
557 error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
562 error = kern_connectat(td, AT_FDCWD, args->s, sa);
564 if (error != EISCONN)
568 * Linux doesn't return EISCONN the first time it occurs,
569 * when on a non-blocking socket. Instead it returns the
570 * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
572 error = getsock_cap(td, args->s, &cap_connect_rights,
579 if (fflag & FNONBLOCK) {
581 if (so->so_emuldata == 0)
582 error = so->so_error;
583 so->so_emuldata = (void *)1;
592 linux_listen(struct thread *td, struct linux_listen_args *args)
595 return (kern_listen(td, args->s, args->backlog));
599 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
600 l_uintptr_t namelen, int flags)
602 struct l_sockaddr *lsa;
610 error = linux_set_socket_flags(flags, &bflags);
615 if (PTRIN(addr) == NULL) {
617 error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
619 error = copyin(PTRIN(namelen), &len, sizeof(len));
624 error = kern_accept4(td, s, &sa, &len, bflags, &fp);
631 * XXX. This is wrong, different sockaddr structures
632 * have different sizes.
634 if (error == EFAULT && namelen != sizeof(struct sockaddr_in))
639 if (error == EINVAL) {
640 error1 = getsock_cap(td, s, &cap_accept_rights, &fp, NULL, NULL);
646 if (so->so_type == SOCK_DGRAM)
653 if (len != 0 && error == 0) {
654 error = bsd_to_linux_sockaddr(sa, &lsa, len);
656 error = copyout(lsa, PTRIN(addr), len);
664 (void)kern_close(td, td->td_retval[0]);
665 td->td_retval[0] = 0;
671 linux_accept(struct thread *td, struct linux_accept_args *args)
674 return (linux_accept_common(td, args->s, args->addr,
679 linux_accept4(struct thread *td, struct linux_accept4_args *args)
682 return (linux_accept_common(td, args->s, args->addr,
683 args->namelen, args->flags));
687 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
689 struct l_sockaddr *lsa;
693 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
697 error = kern_getsockname(td, args->s, &sa, &len);
702 error = bsd_to_linux_sockaddr(sa, &lsa, len);
704 error = copyout(lsa, PTRIN(args->addr),
711 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
716 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
718 struct l_sockaddr *lsa;
722 error = copyin(PTRIN(args->namelen), &len, sizeof(len));
728 error = kern_getpeername(td, args->s, &sa, &len);
733 error = bsd_to_linux_sockaddr(sa, &lsa, len);
735 error = copyout(lsa, PTRIN(args->addr),
742 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
747 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
749 struct socketpair_args /* {
757 bsd_args.domain = linux_to_bsd_domain(args->domain);
758 if (bsd_args.domain != PF_LOCAL)
759 return (EAFNOSUPPORT);
760 bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
761 if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
763 error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
767 if (args->protocol != 0 && args->protocol != PF_UNIX)
770 * Use of PF_UNIX as protocol argument is not right,
772 * Do not map PF_UNIX as its Linux value is identical
775 return (EPROTONOSUPPORT);
777 bsd_args.protocol = 0;
778 bsd_args.rsv = (int *)PTRIN(args->rsv);
779 return (sys_socketpair(td, &bsd_args));
782 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
783 struct linux_send_args {
791 linux_send(struct thread *td, struct linux_send_args *args)
793 struct sendto_args /* {
804 bsd_args.s = args->s;
805 bsd_args.buf = (caddr_t)PTRIN(args->msg);
806 bsd_args.len = args->len;
807 bsd_args.flags = args->flags;
810 error = sys_sendto(td, &bsd_args);
811 if (error == ENOTCONN) {
813 * Linux doesn't return ENOTCONN for non-blocking sockets.
814 * Instead it returns the EAGAIN.
816 error = getsock_cap(td, args->s, &cap_send_rights, &fp,
819 if (fflag & FNONBLOCK)
827 struct linux_recv_args {
835 linux_recv(struct thread *td, struct linux_recv_args *args)
837 struct recvfrom_args /* {
842 struct sockaddr *from;
843 socklen_t fromlenaddr;
846 bsd_args.s = args->s;
847 bsd_args.buf = (caddr_t)PTRIN(args->msg);
848 bsd_args.len = args->len;
849 bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
850 bsd_args.from = NULL;
851 bsd_args.fromlenaddr = 0;
852 return (sys_recvfrom(td, &bsd_args));
854 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
857 linux_sendto(struct thread *td, struct linux_sendto_args *args)
862 if (linux_check_hdrincl(td, args->s) == 0)
863 /* IP_HDRINCL set, tweak the packet before sending */
864 return (linux_sendto_hdrincl(td, args));
866 msg.msg_name = PTRIN(args->to);
867 msg.msg_namelen = args->tolen;
870 msg.msg_control = NULL;
872 aiov.iov_base = PTRIN(args->msg);
873 aiov.iov_len = args->len;
874 return (linux_sendit(td, args->s, &msg, args->flags, NULL,
879 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
881 struct l_sockaddr *lsa;
887 if (PTRIN(args->fromlen) != NULL) {
888 error = copyin(PTRIN(args->fromlen), &fromlen,
894 sa = malloc(fromlen, M_SONAME, M_WAITOK);
901 msg.msg_namelen = fromlen;
904 aiov.iov_base = PTRIN(args->buf);
905 aiov.iov_len = args->len;
907 msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
909 error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
913 if (PTRIN(args->from) != NULL) {
914 error = bsd_to_linux_sockaddr(sa, &lsa, msg.msg_namelen);
916 error = copyout(lsa, PTRIN(args->from),
921 if (error == 0 && PTRIN(args->fromlen) != NULL)
922 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
923 sizeof(msg.msg_namelen));
930 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
933 struct cmsghdr *cmsg;
934 struct mbuf *control;
936 struct l_cmsghdr linux_cmsg;
937 struct l_cmsghdr *ptr_cmsg;
938 struct l_msghdr linux_msg;
943 sa_family_t sa_family;
950 error = copyin(msghdr, &linux_msg, sizeof(linux_msg));
955 * Some Linux applications (ping) define a non-NULL control data
956 * pointer, but a msg_controllen of 0, which is not allowed in the
957 * FreeBSD system call interface. NULL the msg_control pointer in
958 * order to handle this case. This should be checked, but allows the
959 * Linux ping to work.
961 if (PTRIN(linux_msg.msg_control) != NULL && linux_msg.msg_controllen == 0)
962 linux_msg.msg_control = PTROUT(NULL);
964 error = linux_to_bsd_msghdr(&msg, &linux_msg);
968 #ifdef COMPAT_LINUX32
969 error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
972 error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
979 error = kern_getsockname(td, s, &sa, &datalen);
982 sa_family = sa->sa_family;
985 if (flags & LINUX_MSG_OOB) {
987 if (sa_family == AF_UNIX)
990 error = getsock_cap(td, s, &cap_send_rights, &fp,
995 if (so->so_type != SOCK_STREAM)
1002 if (linux_msg.msg_controllen >= sizeof(struct l_cmsghdr)) {
1005 control = m_get(M_WAITOK, MT_CONTROL);
1006 MCLGET(control, M_WAITOK);
1007 data = mtod(control, void *);
1010 ptr_cmsg = PTRIN(linux_msg.msg_control);
1011 clen = linux_msg.msg_controllen;
1013 error = copyin(ptr_cmsg, &linux_cmsg,
1014 sizeof(struct l_cmsghdr));
1019 if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
1020 linux_cmsg.cmsg_len > clen)
1023 if (datalen + CMSG_HDRSZ > MCLBYTES)
1027 * Now we support only SCM_RIGHTS and SCM_CRED,
1028 * so return EINVAL in any other cmsg_type
1032 linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
1034 linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1035 if (cmsg->cmsg_type == -1
1036 || cmsg->cmsg_level != SOL_SOCKET)
1040 * Some applications (e.g. pulseaudio) attempt to
1041 * send ancillary data even if the underlying protocol
1042 * doesn't support it which is not allowed in the
1043 * FreeBSD system call interface.
1045 if (sa_family != AF_UNIX)
1048 if (cmsg->cmsg_type == SCM_CREDS) {
1049 len = sizeof(struct cmsgcred);
1050 if (datalen + CMSG_SPACE(len) > MCLBYTES)
1054 * The lower levels will fill in the structure
1056 memset(CMSG_DATA(data), 0, len);
1058 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1059 if (datalen + CMSG_SPACE(len) < datalen ||
1060 datalen + CMSG_SPACE(len) > MCLBYTES)
1063 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1064 CMSG_DATA(data), len);
1069 cmsg->cmsg_len = CMSG_LEN(len);
1070 data = (char *)data + CMSG_SPACE(len);
1071 datalen += CMSG_SPACE(len);
1073 if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
1076 clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
1077 ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
1078 LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
1079 } while(clen >= sizeof(struct l_cmsghdr));
1081 control->m_len = datalen;
1090 error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
1100 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1103 return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1108 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1110 struct l_mmsghdr *msg;
1112 int error, datagrams;
1114 if (args->vlen > UIO_MAXIOV)
1115 args->vlen = UIO_MAXIOV;
1117 msg = PTRIN(args->msg);
1119 while (datagrams < args->vlen) {
1120 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1125 retval = td->td_retval[0];
1126 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1133 td->td_retval[0] = datagrams;
1138 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1139 l_uint flags, struct msghdr *msg)
1142 struct cmsgcred *cmcred;
1143 struct l_cmsghdr *linux_cmsg = NULL;
1144 struct l_ucred linux_ucred;
1145 socklen_t datalen, maxlen, outlen;
1146 struct l_msghdr linux_msg;
1147 struct iovec *iov, *uiov;
1148 struct mbuf *control = NULL;
1149 struct mbuf **controlp;
1150 struct timeval *ftmvl;
1151 struct l_sockaddr *lsa;
1152 struct sockaddr *sa;
1156 int error, i, fd, fds, *fdp;
1158 error = copyin(msghdr, &linux_msg, sizeof(linux_msg));
1162 error = linux_to_bsd_msghdr(msg, &linux_msg);
1166 #ifdef COMPAT_LINUX32
1167 error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
1170 error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
1175 if (msg->msg_name) {
1176 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1181 uiov = msg->msg_iov;
1183 controlp = (msg->msg_control != NULL) ? &control : NULL;
1184 error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1185 msg->msg_iov = uiov;
1189 if (msg->msg_name) {
1190 msg->msg_name = PTRIN(linux_msg.msg_name);
1191 error = bsd_to_linux_sockaddr(sa, &lsa, msg->msg_namelen);
1193 error = copyout(lsa, PTRIN(msg->msg_name),
1195 free(lsa, M_SONAME);
1200 error = bsd_to_linux_msghdr(msg, &linux_msg);
1204 maxlen = linux_msg.msg_controllen;
1205 linux_msg.msg_controllen = 0;
1207 linux_cmsg = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1209 msg->msg_control = mtod(control, struct cmsghdr *);
1210 msg->msg_controllen = control->m_len;
1212 cm = CMSG_FIRSTHDR(msg);
1213 outbuf = PTRIN(linux_msg.msg_control);
1215 while (cm != NULL) {
1216 linux_cmsg->cmsg_type =
1217 bsd_to_linux_cmsg_type(cm->cmsg_type);
1218 linux_cmsg->cmsg_level =
1219 bsd_to_linux_sockopt_level(cm->cmsg_level);
1220 if (linux_cmsg->cmsg_type == -1 ||
1221 cm->cmsg_level != SOL_SOCKET) {
1226 data = CMSG_DATA(cm);
1227 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1229 switch (cm->cmsg_type) {
1231 if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1232 fds = datalen / sizeof(int);
1234 for (i = 0; i < fds; i++) {
1236 (void)kern_fcntl(td, fd,
1237 F_SETFD, FD_CLOEXEC);
1244 * Currently LOCAL_CREDS is never in
1245 * effect for Linux so no need to worry
1248 if (datalen != sizeof(*cmcred)) {
1252 cmcred = (struct cmsgcred *)data;
1253 bzero(&linux_ucred, sizeof(linux_ucred));
1254 linux_ucred.pid = cmcred->cmcred_pid;
1255 linux_ucred.uid = cmcred->cmcred_uid;
1256 linux_ucred.gid = cmcred->cmcred_gid;
1257 data = &linux_ucred;
1258 datalen = sizeof(linux_ucred);
1262 if (datalen != sizeof(struct timeval)) {
1266 ftmvl = (struct timeval *)data;
1267 ltmvl.tv_sec = ftmvl->tv_sec;
1268 ltmvl.tv_usec = ftmvl->tv_usec;
1270 datalen = sizeof(ltmvl);
1274 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
1279 linux_msg.msg_flags |= LINUX_MSG_CTRUNC;
1280 m_dispose_extcontrolm(control);
1285 linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
1287 error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
1290 outbuf += L_CMSG_HDRSZ;
1292 error = copyout(data, outbuf, datalen);
1296 outbuf += LINUX_CMSG_ALIGN(datalen);
1297 outlen += LINUX_CMSG_LEN(datalen);
1299 cm = CMSG_NXTHDR(msg, cm);
1301 linux_msg.msg_controllen = outlen;
1305 error = copyout(&linux_msg, msghdr, sizeof(linux_msg));
1308 if (control != NULL) {
1310 m_dispose_extcontrolm(control);
1314 free(linux_cmsg, M_LINUX);
1321 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1323 struct msghdr bsd_msg;
1325 return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1326 args->flags, &bsd_msg));
1330 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
1332 struct l_mmsghdr *msg;
1333 struct msghdr bsd_msg;
1334 struct l_timespec lts;
1335 struct timespec ts, tts;
1337 int error, datagrams;
1339 if (args->timeout) {
1340 error = copyin(args->timeout, <s, sizeof(struct l_timespec));
1343 error = linux_to_native_timespec(&ts, <s);
1347 timespecadd(&tts, &ts, &tts);
1350 msg = PTRIN(args->msg);
1352 while (datagrams < args->vlen) {
1353 error = linux_recvmsg_common(td, args->s, &msg->msg_hdr,
1354 args->flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1358 retval = td->td_retval[0];
1359 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1366 * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1368 if (args->flags & LINUX_MSG_WAITFORONE)
1369 args->flags |= LINUX_MSG_DONTWAIT;
1372 * See BUGS section of recvmmsg(2).
1374 if (args->timeout) {
1376 timespecsub(&ts, &tts, &ts);
1377 if (!timespecisset(&ts) || ts.tv_sec > 0)
1380 /* Out of band data, return right away. */
1381 if (bsd_msg.msg_flags & MSG_OOB)
1385 td->td_retval[0] = datagrams;
1390 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
1393 return (kern_shutdown(td, args->s, args->how));
1397 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
1399 struct setsockopt_args /* {
1407 struct sockaddr *sa;
1412 bsd_args.s = args->s;
1413 bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1414 switch (bsd_args.level) {
1416 name = linux_to_bsd_so_sockopt(args->optname);
1421 error = copyin(PTRIN(args->optval), &linux_tv,
1425 tv.tv_sec = linux_tv.tv_sec;
1426 tv.tv_usec = linux_tv.tv_usec;
1427 return (kern_setsockopt(td, args->s, bsd_args.level,
1428 name, &tv, UIO_SYSSPACE, sizeof(tv)));
1435 name = linux_to_bsd_ip_sockopt(args->optname);
1438 name = linux_to_bsd_ip6_sockopt(args->optname);
1441 name = linux_to_bsd_tcp_sockopt(args->optname);
1448 return (ENOPROTOOPT);
1451 if (name == IPV6_NEXTHOP) {
1454 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
1458 error = kern_setsockopt(td, args->s, bsd_args.level,
1459 name, sa, UIO_SYSSPACE, len);
1462 bsd_args.name = name;
1463 bsd_args.val = PTRIN(args->optval);
1464 bsd_args.valsize = args->optlen;
1465 error = sys_setsockopt(td, &bsd_args);
1472 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
1474 struct getsockopt_args /* {
1483 socklen_t tv_len, xulen, len;
1484 struct l_sockaddr *lsa;
1485 struct sockaddr *sa;
1488 int error, name, newval;
1490 bsd_args.s = args->s;
1491 bsd_args.level = linux_to_bsd_sockopt_level(args->level);
1492 switch (bsd_args.level) {
1494 name = linux_to_bsd_so_sockopt(args->optname);
1499 tv_len = sizeof(tv);
1500 error = kern_getsockopt(td, args->s, bsd_args.level,
1501 name, &tv, UIO_SYSSPACE, &tv_len);
1504 linux_tv.tv_sec = tv.tv_sec;
1505 linux_tv.tv_usec = tv.tv_usec;
1506 return (copyout(&linux_tv, PTRIN(args->optval),
1509 case LOCAL_PEERCRED:
1510 if (args->optlen < sizeof(lxu))
1513 * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
1514 * but by the Unix socket's level 0.
1518 error = kern_getsockopt(td, args->s, bsd_args.level,
1519 name, &xu, UIO_SYSSPACE, &xulen);
1522 lxu.pid = xu.cr_pid;
1523 lxu.uid = xu.cr_uid;
1524 lxu.gid = xu.cr_gid;
1525 return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
1528 len = sizeof(newval);
1529 error = kern_getsockopt(td, args->s, bsd_args.level,
1530 name, &newval, UIO_SYSSPACE, &len);
1533 newval = -SV_ABI_ERRNO(td->td_proc, newval);
1534 return (copyout(&newval, PTRIN(args->optval), len));
1541 name = linux_to_bsd_ip_sockopt(args->optname);
1544 name = linux_to_bsd_ip6_sockopt(args->optname);
1547 name = linux_to_bsd_tcp_sockopt(args->optname);
1556 bsd_args.name = name;
1557 bsd_args.avalsize = PTRIN(args->optlen);
1559 if (name == IPV6_NEXTHOP) {
1560 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
1563 sa = malloc(len, M_SONAME, M_WAITOK);
1565 error = kern_getsockopt(td, args->s, bsd_args.level,
1566 name, sa, UIO_SYSSPACE, &len);
1570 error = bsd_to_linux_sockaddr(sa, &lsa, len);
1572 error = copyout(lsa, PTRIN(args->optval), len);
1573 free(lsa, M_SONAME);
1575 error = copyout(&len, PTRIN(args->optlen),
1580 bsd_args.val = PTRIN(args->optval);
1581 error = sys_getsockopt(td, &bsd_args);
1587 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1589 /* Argument list sizes for linux_socketcall */
1590 static const unsigned char lxs_args_cnt[] = {
1591 0 /* unused*/, 3 /* socket */,
1592 3 /* bind */, 3 /* connect */,
1593 2 /* listen */, 3 /* accept */,
1594 3 /* getsockname */, 3 /* getpeername */,
1595 4 /* socketpair */, 4 /* send */,
1596 4 /* recv */, 6 /* sendto */,
1597 6 /* recvfrom */, 2 /* shutdown */,
1598 5 /* setsockopt */, 5 /* getsockopt */,
1599 3 /* sendmsg */, 3 /* recvmsg */,
1600 4 /* accept4 */, 5 /* recvmmsg */,
1603 #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1)
1604 #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong))
1607 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
1610 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1611 register_t l_args[6];
1616 if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
1618 error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
1622 #if defined(__amd64__) && defined(COMPAT_LINUX32)
1623 for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
1629 switch (args->what) {
1631 return (linux_socket(td, arg));
1633 return (linux_bind(td, arg));
1635 return (linux_connect(td, arg));
1637 return (linux_listen(td, arg));
1639 return (linux_accept(td, arg));
1640 case LINUX_GETSOCKNAME:
1641 return (linux_getsockname(td, arg));
1642 case LINUX_GETPEERNAME:
1643 return (linux_getpeername(td, arg));
1644 case LINUX_SOCKETPAIR:
1645 return (linux_socketpair(td, arg));
1647 return (linux_send(td, arg));
1649 return (linux_recv(td, arg));
1651 return (linux_sendto(td, arg));
1652 case LINUX_RECVFROM:
1653 return (linux_recvfrom(td, arg));
1654 case LINUX_SHUTDOWN:
1655 return (linux_shutdown(td, arg));
1656 case LINUX_SETSOCKOPT:
1657 return (linux_setsockopt(td, arg));
1658 case LINUX_GETSOCKOPT:
1659 return (linux_getsockopt(td, arg));
1661 return (linux_sendmsg(td, arg));
1663 return (linux_recvmsg(td, arg));
1665 return (linux_accept4(td, arg));
1666 case LINUX_RECVMMSG:
1667 return (linux_recvmmsg(td, arg));
1668 case LINUX_SENDMMSG:
1669 return (linux_sendmmsg(td, arg));
1672 uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
1675 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */