1c21dee17SSøren Schmidt /*- 24d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 37f2d13d6SPedro F. Giffuni * 49a14aa01SUlrich Spörlein * Copyright (c) 1995 Søren Schmidt 5c21dee17SSøren Schmidt * All rights reserved. 6c21dee17SSøren Schmidt * 7c21dee17SSøren Schmidt * Redistribution and use in source and binary forms, with or without 8c21dee17SSøren Schmidt * modification, are permitted provided that the following conditions 9c21dee17SSøren Schmidt * are met: 10c21dee17SSøren Schmidt * 1. Redistributions of source code must retain the above copyright 110ba1b365SEd Maste * notice, this list of conditions and the following disclaimer. 12c21dee17SSøren Schmidt * 2. Redistributions in binary form must reproduce the above copyright 13c21dee17SSøren Schmidt * notice, this list of conditions and the following disclaimer in the 14c21dee17SSøren Schmidt * documentation and/or other materials provided with the distribution. 15c21dee17SSøren Schmidt * 160ba1b365SEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 170ba1b365SEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 180ba1b365SEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 190ba1b365SEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 200ba1b365SEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 210ba1b365SEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 220ba1b365SEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 230ba1b365SEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 240ba1b365SEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 250ba1b365SEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 260ba1b365SEd Maste * SUCH DAMAGE. 27c21dee17SSøren Schmidt */ 28c21dee17SSøren Schmidt 29ca26842eSHajimu UMEMOTO #include "opt_inet6.h" 305591b823SEivind Eklund 31c21dee17SSøren Schmidt #include <sys/param.h> 324a144410SRobert Watson #include <sys/capsicum.h> 336d76adbbSEdward Tomasz Napierala #include <sys/filedesc.h> 34104a9b7eSAlexander Kabaev #include <sys/limits.h> 35ca26842eSHajimu UMEMOTO #include <sys/malloc.h> 365a8a13e0SDavid Malone #include <sys/mbuf.h> 37d8e53d94SDmitry Chagin #include <sys/proc.h> 38bb66c597SJames McLaughlin #include <sys/protosw.h> 39c21dee17SSøren Schmidt #include <sys/socket.h> 400bf301c0SJonathan Lemon #include <sys/socketvar.h> 41ca26842eSHajimu UMEMOTO #include <sys/syscallsubr.h> 42d8e53d94SDmitry Chagin #include <sys/sysproto.h> 43bb66c597SJames McLaughlin #include <sys/vnode.h> 44d0b2365eSKonstantin Belousov #include <sys/un.h> 45fc7510aeSEd Maste #include <sys/unistd.h> 46fc7510aeSEd Maste 47fc7510aeSEd Maste #include <security/audit/audit.h> 481f3dad5aSBruce Evans 494b79449eSBjoern A. Zeeb #include <net/if.h> 50eedc7fd9SGleb Smirnoff #include <net/vnet.h> 51c21dee17SSøren Schmidt #include <netinet/in.h> 52f2477ae1SMike Smith #include <netinet/ip.h> 53fb709557SJohn Baldwin #include <netinet/tcp.h> 54ca26842eSHajimu UMEMOTO #ifdef INET6 55ca26842eSHajimu UMEMOTO #include <netinet/ip6.h> 56ca26842eSHajimu UMEMOTO #include <netinet6/ip6_var.h> 57ca26842eSHajimu UMEMOTO #endif 58c21dee17SSøren Schmidt 591997c537SDavid E. O'Brien #ifdef COMPAT_LINUX32 60524c9accSDmitry Chagin #include <compat/freebsd32/freebsd32_util.h> 614af27623STim J. Robbins #include <machine/../linux32/linux.h> 624af27623STim J. Robbins #include <machine/../linux32/linux32_proto.h> 631997c537SDavid E. O'Brien #else 641997c537SDavid E. O'Brien #include <machine/../linux/linux.h> 651997c537SDavid E. O'Brien #include <machine/../linux/linux_proto.h> 664af27623STim J. Robbins #endif 67d5368bf3SDmitry Chagin #include <compat/linux/linux_common.h> 680e26e54bSDmitry Chagin #include <compat/linux/linux_emul.h> 694d0f380dSDmitry Chagin #include <compat/linux/linux_file.h> 70da6d8ae6SEdward Tomasz Napierala #include <compat/linux/linux_mib.h> 7140dbba57SAssar Westerlund #include <compat/linux/linux_socket.h> 72c8a79231SDmitry Chagin #include <compat/linux/linux_time.h> 73ac951e62SMarcel Moolenaar #include <compat/linux/linux_util.h> 74c21dee17SSøren Schmidt 75a927409cSDmitry Chagin _Static_assert(offsetof(struct l_ifreq, ifr_ifru) == 76a927409cSDmitry Chagin offsetof(struct ifreq, ifr_ifru), 77a927409cSDmitry Chagin "Linux ifreq members names should be equal to FreeeBSD"); 78a927409cSDmitry Chagin _Static_assert(offsetof(struct l_ifreq, ifr_index) == 79a927409cSDmitry Chagin offsetof(struct ifreq, ifr_index), 80a927409cSDmitry Chagin "Linux ifreq members names should be equal to FreeeBSD"); 81a927409cSDmitry Chagin _Static_assert(offsetof(struct l_ifreq, ifr_name) == 82a927409cSDmitry Chagin offsetof(struct ifreq, ifr_name), 83a927409cSDmitry Chagin "Linux ifreq members names should be equal to FreeeBSD"); 84a927409cSDmitry Chagin 85e44a78ceSEdward Tomasz Napierala #define SECURITY_CONTEXT_STRING "unconfined" 86e44a78ceSEdward Tomasz Napierala 87e1ff74c0SDmitry Chagin static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *, 88e1ff74c0SDmitry Chagin l_uint); 89e1ff74c0SDmitry Chagin static int linux_recvmsg_common(struct thread *, l_int, struct l_msghdr *, 90e1ff74c0SDmitry Chagin l_uint, struct msghdr *); 914cf10e29SDmitry Chagin static int linux_set_socket_flags(int, int *); 92ca26842eSHajimu UMEMOTO 937c40e2d5SAlexander V. Chernikov #define SOL_NETLINK 270 947c40e2d5SAlexander V. Chernikov 95c21dee17SSøren Schmidt static int 96c21dee17SSøren Schmidt linux_to_bsd_sockopt_level(int level) 97c21dee17SSøren Schmidt { 983f3a4815SMarcel Moolenaar 994beacc3bSEdward Tomasz Napierala if (level == LINUX_SOL_SOCKET) 1003f3a4815SMarcel Moolenaar return (SOL_SOCKET); 1014beacc3bSEdward Tomasz Napierala /* Remaining values are RFC-defined protocol numbers. */ 1023f3a4815SMarcel Moolenaar return (level); 103c21dee17SSøren Schmidt } 104c21dee17SSøren Schmidt 1053f3a4815SMarcel Moolenaar static int 10684b11cd8SMitsuru IWASAKI bsd_to_linux_sockopt_level(int level) 10784b11cd8SMitsuru IWASAKI { 10884b11cd8SMitsuru IWASAKI 1094beacc3bSEdward Tomasz Napierala if (level == SOL_SOCKET) 11084b11cd8SMitsuru IWASAKI return (LINUX_SOL_SOCKET); 11184b11cd8SMitsuru IWASAKI return (level); 11284b11cd8SMitsuru IWASAKI } 11384b11cd8SMitsuru IWASAKI 11484b11cd8SMitsuru IWASAKI static int 1153f3a4815SMarcel Moolenaar linux_to_bsd_ip_sockopt(int opt) 116c21dee17SSøren Schmidt { 1173f3a4815SMarcel Moolenaar 118c21dee17SSøren Schmidt switch (opt) { 1198ec6c4a3SAlexander Leidinger /* known and translated sockopts */ 120c21dee17SSøren Schmidt case LINUX_IP_TOS: 1213f3a4815SMarcel Moolenaar return (IP_TOS); 122c21dee17SSøren Schmidt case LINUX_IP_TTL: 1233f3a4815SMarcel Moolenaar return (IP_TTL); 1248ec6c4a3SAlexander Leidinger case LINUX_IP_HDRINCL: 1258ec6c4a3SAlexander Leidinger return (IP_HDRINCL); 12666ff6a3cSBill Fenner case LINUX_IP_OPTIONS: 1273f3a4815SMarcel Moolenaar return (IP_OPTIONS); 1288ec6c4a3SAlexander Leidinger case LINUX_IP_RECVOPTS: 1298ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVOPTS"); 1308ec6c4a3SAlexander Leidinger return (IP_RECVOPTS); 1318ec6c4a3SAlexander Leidinger case LINUX_IP_RETOPTS: 1328ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_REETOPTS"); 1338ec6c4a3SAlexander Leidinger return (IP_RETOPTS); 1348ec6c4a3SAlexander Leidinger case LINUX_IP_RECVTTL: 1358ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVTTL"); 1368ec6c4a3SAlexander Leidinger return (IP_RECVTTL); 1378ec6c4a3SAlexander Leidinger case LINUX_IP_RECVTOS: 1388ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVTOS"); 1398ec6c4a3SAlexander Leidinger return (IP_RECVTOS); 1408ec6c4a3SAlexander Leidinger case LINUX_IP_FREEBIND: 1418ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_FREEBIND"); 1428ec6c4a3SAlexander Leidinger return (IP_BINDANY); 1438ec6c4a3SAlexander Leidinger case LINUX_IP_IPSEC_POLICY: 1448ec6c4a3SAlexander Leidinger /* we have this option, but not documented in ip(4) manpage */ 1458ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_IPSEC_POLICY"); 1468ec6c4a3SAlexander Leidinger return (IP_IPSEC_POLICY); 1478ec6c4a3SAlexander Leidinger case LINUX_IP_MINTTL: 1488ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MINTTL"); 1498ec6c4a3SAlexander Leidinger return (IP_MINTTL); 15066ff6a3cSBill Fenner case LINUX_IP_MULTICAST_IF: 1513f3a4815SMarcel Moolenaar return (IP_MULTICAST_IF); 15266ff6a3cSBill Fenner case LINUX_IP_MULTICAST_TTL: 1533f3a4815SMarcel Moolenaar return (IP_MULTICAST_TTL); 15466ff6a3cSBill Fenner case LINUX_IP_MULTICAST_LOOP: 1553f3a4815SMarcel Moolenaar return (IP_MULTICAST_LOOP); 15666ff6a3cSBill Fenner case LINUX_IP_ADD_MEMBERSHIP: 1573f3a4815SMarcel Moolenaar return (IP_ADD_MEMBERSHIP); 15866ff6a3cSBill Fenner case LINUX_IP_DROP_MEMBERSHIP: 1593f3a4815SMarcel Moolenaar return (IP_DROP_MEMBERSHIP); 1608ec6c4a3SAlexander Leidinger case LINUX_IP_UNBLOCK_SOURCE: 1618ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_UNBLOCK_SOURCE"); 1628ec6c4a3SAlexander Leidinger return (IP_UNBLOCK_SOURCE); 1638ec6c4a3SAlexander Leidinger case LINUX_IP_BLOCK_SOURCE: 1648ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_BLOCK_SOURCE"); 1658ec6c4a3SAlexander Leidinger return (IP_BLOCK_SOURCE); 1668ec6c4a3SAlexander Leidinger case LINUX_IP_ADD_SOURCE_MEMBERSHIP: 1678ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_ADD_SOURCE_MEMBERSHIP"); 1688ec6c4a3SAlexander Leidinger return (IP_ADD_SOURCE_MEMBERSHIP); 1698ec6c4a3SAlexander Leidinger case LINUX_IP_DROP_SOURCE_MEMBERSHIP: 1708ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_DROP_SOURCE_MEMBERSHIP"); 1718ec6c4a3SAlexander Leidinger return (IP_DROP_SOURCE_MEMBERSHIP); 1728ec6c4a3SAlexander Leidinger case LINUX_MCAST_JOIN_GROUP: 1738ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_JOIN_GROUP"); 1748ec6c4a3SAlexander Leidinger return (MCAST_JOIN_GROUP); 1758ec6c4a3SAlexander Leidinger case LINUX_MCAST_LEAVE_GROUP: 1768ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_LEAVE_GROUP"); 1778ec6c4a3SAlexander Leidinger return (MCAST_LEAVE_GROUP); 1788ec6c4a3SAlexander Leidinger case LINUX_MCAST_JOIN_SOURCE_GROUP: 1798ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_JOIN_SOURCE_GROUP"); 1808ec6c4a3SAlexander Leidinger return (MCAST_JOIN_SOURCE_GROUP); 1818ec6c4a3SAlexander Leidinger case LINUX_MCAST_LEAVE_SOURCE_GROUP: 1828ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_LEAVE_SOURCE_GROUP"); 1838ec6c4a3SAlexander Leidinger return (MCAST_LEAVE_SOURCE_GROUP); 184f8a66150SDmitry Chagin case LINUX_IP_RECVORIGDSTADDR: 185f8a66150SDmitry Chagin return (IP_RECVORIGDSTADDR); 1868ec6c4a3SAlexander Leidinger 1878ec6c4a3SAlexander Leidinger /* known but not implemented sockopts */ 1888ec6c4a3SAlexander Leidinger case LINUX_IP_ROUTER_ALERT: 1898ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 1908ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_ROUTER_ALERT (%d), you can not do user-space routing from linux programs", 1918ec6c4a3SAlexander Leidinger opt); 1928ec6c4a3SAlexander Leidinger return (-2); 1938ec6c4a3SAlexander Leidinger case LINUX_IP_PKTINFO: 1948ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 1958ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_PKTINFO (%d), you can not get extended packet info for datagram sockets in linux programs", 1968ec6c4a3SAlexander Leidinger opt); 1978ec6c4a3SAlexander Leidinger return (-2); 1988ec6c4a3SAlexander Leidinger case LINUX_IP_PKTOPTIONS: 1998ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2008ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_PKTOPTIONS (%d)", 2018ec6c4a3SAlexander Leidinger opt); 2028ec6c4a3SAlexander Leidinger return (-2); 2038ec6c4a3SAlexander Leidinger case LINUX_IP_MTU_DISCOVER: 2048ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2058ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_MTU_DISCOVER (%d), your linux program can not control path-MTU discovery", 2068ec6c4a3SAlexander Leidinger opt); 2078ec6c4a3SAlexander Leidinger return (-2); 2088ec6c4a3SAlexander Leidinger case LINUX_IP_RECVERR: 2098ec6c4a3SAlexander Leidinger /* needed by steam */ 2108ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2118ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_RECVERR (%d), you can not get extended reliability info in linux programs", 2128ec6c4a3SAlexander Leidinger opt); 2138ec6c4a3SAlexander Leidinger return (-2); 2148ec6c4a3SAlexander Leidinger case LINUX_IP_MTU: 2158ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2168ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_MTU (%d), your linux program can not control the MTU on this socket", 2178ec6c4a3SAlexander Leidinger opt); 2188ec6c4a3SAlexander Leidinger return (-2); 2198ec6c4a3SAlexander Leidinger case LINUX_IP_XFRM_POLICY: 2208ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2218ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_XFRM_POLICY (%d)", 2228ec6c4a3SAlexander Leidinger opt); 2238ec6c4a3SAlexander Leidinger return (-2); 2248ec6c4a3SAlexander Leidinger case LINUX_IP_PASSSEC: 2258ec6c4a3SAlexander Leidinger /* needed by steam */ 2268ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2278ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_PASSSEC (%d), you can not get IPSEC related credential information associated with this socket in linux programs -- if you do not use IPSEC, you can ignore this", 2288ec6c4a3SAlexander Leidinger opt); 2298ec6c4a3SAlexander Leidinger return (-2); 2308ec6c4a3SAlexander Leidinger case LINUX_IP_TRANSPARENT: 2318ec6c4a3SAlexander Leidinger /* IP_BINDANY or more? */ 2328ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2338ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_TRANSPARENT (%d), you can not enable transparent proxying in linux programs -- note, IP_FREEBIND is supported, no idea if the FreeBSD IP_BINDANY is equivalent to the Linux IP_TRANSPARENT or not, any info is welcome", 2348ec6c4a3SAlexander Leidinger opt); 2358ec6c4a3SAlexander Leidinger return (-2); 2368ec6c4a3SAlexander Leidinger case LINUX_IP_NODEFRAG: 2378ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2388ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_NODEFRAG (%d)", 2398ec6c4a3SAlexander Leidinger opt); 2408ec6c4a3SAlexander Leidinger return (-2); 2418ec6c4a3SAlexander Leidinger case LINUX_IP_CHECKSUM: 2428ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2438ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_CHECKSUM (%d)", 2448ec6c4a3SAlexander Leidinger opt); 2458ec6c4a3SAlexander Leidinger return (-2); 2468ec6c4a3SAlexander Leidinger case LINUX_IP_BIND_ADDRESS_NO_PORT: 2478ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2488ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_BIND_ADDRESS_NO_PORT (%d)", 2498ec6c4a3SAlexander Leidinger opt); 2508ec6c4a3SAlexander Leidinger return (-2); 2518ec6c4a3SAlexander Leidinger case LINUX_IP_RECVFRAGSIZE: 2528ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2538ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_RECVFRAGSIZE (%d)", 2548ec6c4a3SAlexander Leidinger opt); 2558ec6c4a3SAlexander Leidinger return (-2); 2568ec6c4a3SAlexander Leidinger case LINUX_MCAST_MSFILTER: 2578ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2588ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_MCAST_MSFILTER (%d)", 2598ec6c4a3SAlexander Leidinger opt); 2608ec6c4a3SAlexander Leidinger return (-2); 2618ec6c4a3SAlexander Leidinger case LINUX_IP_MULTICAST_ALL: 2628ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2638ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_MULTICAST_ALL (%d), your linux program will not see all multicast groups joined by the entire system, only those the program joined itself on this socket", 2648ec6c4a3SAlexander Leidinger opt); 2658ec6c4a3SAlexander Leidinger return (-2); 2668ec6c4a3SAlexander Leidinger case LINUX_IP_UNICAST_IF: 2678ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 2688ec6c4a3SAlexander Leidinger "unsupported IPv4 socket option IP_UNICAST_IF (%d)", 2698ec6c4a3SAlexander Leidinger opt); 2708ec6c4a3SAlexander Leidinger return (-2); 2718ec6c4a3SAlexander Leidinger 2728ec6c4a3SAlexander Leidinger /* unknown sockopts */ 2738ec6c4a3SAlexander Leidinger default: 2743f3a4815SMarcel Moolenaar return (-1); 275c21dee17SSøren Schmidt } 2768ec6c4a3SAlexander Leidinger } 277c21dee17SSøren Schmidt 278c21dee17SSøren Schmidt static int 27986a9058bSAndrey V. Elsukov linux_to_bsd_ip6_sockopt(int opt) 28086a9058bSAndrey V. Elsukov { 28186a9058bSAndrey V. Elsukov 28286a9058bSAndrey V. Elsukov switch (opt) { 2838ec6c4a3SAlexander Leidinger /* known and translated sockopts */ 2848ec6c4a3SAlexander Leidinger case LINUX_IPV6_2292PKTINFO: 2858ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292PKTINFO"); 2868ec6c4a3SAlexander Leidinger return (IPV6_2292PKTINFO); 2878ec6c4a3SAlexander Leidinger case LINUX_IPV6_2292HOPOPTS: 2888ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292HOPOPTS"); 2898ec6c4a3SAlexander Leidinger return (IPV6_2292HOPOPTS); 2908ec6c4a3SAlexander Leidinger case LINUX_IPV6_2292DSTOPTS: 2918ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292DSTOPTS"); 2928ec6c4a3SAlexander Leidinger return (IPV6_2292DSTOPTS); 2938ec6c4a3SAlexander Leidinger case LINUX_IPV6_2292RTHDR: 2948ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292RTHDR"); 2958ec6c4a3SAlexander Leidinger return (IPV6_2292RTHDR); 2968ec6c4a3SAlexander Leidinger case LINUX_IPV6_2292PKTOPTIONS: 2978ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292PKTOPTIONS"); 2988ec6c4a3SAlexander Leidinger return (IPV6_2292PKTOPTIONS); 2998ec6c4a3SAlexander Leidinger case LINUX_IPV6_CHECKSUM: 3008ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_CHECKSUM"); 3018ec6c4a3SAlexander Leidinger return (IPV6_CHECKSUM); 3028ec6c4a3SAlexander Leidinger case LINUX_IPV6_2292HOPLIMIT: 3038ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292HOPLIMIT"); 3048ec6c4a3SAlexander Leidinger return (IPV6_2292HOPLIMIT); 30586a9058bSAndrey V. Elsukov case LINUX_IPV6_NEXTHOP: 30686a9058bSAndrey V. Elsukov return (IPV6_NEXTHOP); 30786a9058bSAndrey V. Elsukov case LINUX_IPV6_UNICAST_HOPS: 30886a9058bSAndrey V. Elsukov return (IPV6_UNICAST_HOPS); 30986a9058bSAndrey V. Elsukov case LINUX_IPV6_MULTICAST_IF: 31086a9058bSAndrey V. Elsukov return (IPV6_MULTICAST_IF); 31186a9058bSAndrey V. Elsukov case LINUX_IPV6_MULTICAST_HOPS: 31286a9058bSAndrey V. Elsukov return (IPV6_MULTICAST_HOPS); 31386a9058bSAndrey V. Elsukov case LINUX_IPV6_MULTICAST_LOOP: 31486a9058bSAndrey V. Elsukov return (IPV6_MULTICAST_LOOP); 31586a9058bSAndrey V. Elsukov case LINUX_IPV6_ADD_MEMBERSHIP: 31686a9058bSAndrey V. Elsukov return (IPV6_JOIN_GROUP); 31786a9058bSAndrey V. Elsukov case LINUX_IPV6_DROP_MEMBERSHIP: 31886a9058bSAndrey V. Elsukov return (IPV6_LEAVE_GROUP); 31986a9058bSAndrey V. Elsukov case LINUX_IPV6_V6ONLY: 32086a9058bSAndrey V. Elsukov return (IPV6_V6ONLY); 3218ec6c4a3SAlexander Leidinger case LINUX_IPV6_IPSEC_POLICY: 3228ec6c4a3SAlexander Leidinger /* we have this option, but not documented in ip6(4) manpage */ 3238ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_IPSEC_POLICY"); 3248ec6c4a3SAlexander Leidinger return (IPV6_IPSEC_POLICY); 3258ec6c4a3SAlexander Leidinger case LINUX_MCAST_JOIN_GROUP: 3268ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_JOIN_GROUP"); 3278ec6c4a3SAlexander Leidinger return (IPV6_JOIN_GROUP); 3288ec6c4a3SAlexander Leidinger case LINUX_MCAST_LEAVE_GROUP: 3298ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_LEAVE_GROUP"); 3308ec6c4a3SAlexander Leidinger return (IPV6_LEAVE_GROUP); 33186a9058bSAndrey V. Elsukov case LINUX_IPV6_RECVPKTINFO: 3328ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVPKTINFO"); 33386a9058bSAndrey V. Elsukov return (IPV6_RECVPKTINFO); 33486a9058bSAndrey V. Elsukov case LINUX_IPV6_PKTINFO: 3358ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_PKTINFO"); 33686a9058bSAndrey V. Elsukov return (IPV6_PKTINFO); 33786a9058bSAndrey V. Elsukov case LINUX_IPV6_RECVHOPLIMIT: 3388ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVHOPLIMIT"); 33986a9058bSAndrey V. Elsukov return (IPV6_RECVHOPLIMIT); 34086a9058bSAndrey V. Elsukov case LINUX_IPV6_HOPLIMIT: 3418ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_HOPLIMIT"); 34286a9058bSAndrey V. Elsukov return (IPV6_HOPLIMIT); 34386a9058bSAndrey V. Elsukov case LINUX_IPV6_RECVHOPOPTS: 3448ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVHOPOPTS"); 34586a9058bSAndrey V. Elsukov return (IPV6_RECVHOPOPTS); 34686a9058bSAndrey V. Elsukov case LINUX_IPV6_HOPOPTS: 3478ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_HOPOPTS"); 34886a9058bSAndrey V. Elsukov return (IPV6_HOPOPTS); 34986a9058bSAndrey V. Elsukov case LINUX_IPV6_RTHDRDSTOPTS: 3508ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RTHDRDSTOPTS"); 35186a9058bSAndrey V. Elsukov return (IPV6_RTHDRDSTOPTS); 35286a9058bSAndrey V. Elsukov case LINUX_IPV6_RECVRTHDR: 3538ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVRTHDR"); 35486a9058bSAndrey V. Elsukov return (IPV6_RECVRTHDR); 35586a9058bSAndrey V. Elsukov case LINUX_IPV6_RTHDR: 3568ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RTHDR"); 35786a9058bSAndrey V. Elsukov return (IPV6_RTHDR); 35886a9058bSAndrey V. Elsukov case LINUX_IPV6_RECVDSTOPTS: 3598ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVDSTOPTS"); 36086a9058bSAndrey V. Elsukov return (IPV6_RECVDSTOPTS); 36186a9058bSAndrey V. Elsukov case LINUX_IPV6_DSTOPTS: 3628ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_DSTOPTS"); 36386a9058bSAndrey V. Elsukov return (IPV6_DSTOPTS); 36486a9058bSAndrey V. Elsukov case LINUX_IPV6_RECVPATHMTU: 3658ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVPATHMTU"); 36686a9058bSAndrey V. Elsukov return (IPV6_RECVPATHMTU); 36786a9058bSAndrey V. Elsukov case LINUX_IPV6_PATHMTU: 3688ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_PATHMTU"); 36986a9058bSAndrey V. Elsukov return (IPV6_PATHMTU); 3708ec6c4a3SAlexander Leidinger case LINUX_IPV6_DONTFRAG: 3718ec6c4a3SAlexander Leidinger return (IPV6_DONTFRAG); 3728ec6c4a3SAlexander Leidinger case LINUX_IPV6_AUTOFLOWLABEL: 3738ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_AUTOFLOWLABEL"); 3748ec6c4a3SAlexander Leidinger return (IPV6_AUTOFLOWLABEL); 3758ec6c4a3SAlexander Leidinger case LINUX_IPV6_ORIGDSTADDR: 3768ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_ORIGDSTADDR"); 3778ec6c4a3SAlexander Leidinger return (IPV6_ORIGDSTADDR); 3788ec6c4a3SAlexander Leidinger case LINUX_IPV6_FREEBIND: 3798ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_FREEBIND"); 3808ec6c4a3SAlexander Leidinger return (IPV6_BINDANY); 3818ec6c4a3SAlexander Leidinger 3828ec6c4a3SAlexander Leidinger /* known but not implemented sockopts */ 3838ec6c4a3SAlexander Leidinger case LINUX_IPV6_ADDRFORM: 3848ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 3858ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_ADDRFORM (%d), you linux program can not convert the socket to IPv4", 3868ec6c4a3SAlexander Leidinger opt); 3878ec6c4a3SAlexander Leidinger return (-2); 3888ec6c4a3SAlexander Leidinger case LINUX_IPV6_AUTHHDR: 3898ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 3908ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_AUTHHDR (%d), your linux program can not get the authentication header info of IPv6 packets", 3918ec6c4a3SAlexander Leidinger opt); 3928ec6c4a3SAlexander Leidinger return (-2); 3938ec6c4a3SAlexander Leidinger case LINUX_IPV6_FLOWINFO: 3948ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 3958ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_FLOWINFO (%d), your linux program can not get the flowid of IPv6 packets", 3968ec6c4a3SAlexander Leidinger opt); 3978ec6c4a3SAlexander Leidinger return (-2); 3988ec6c4a3SAlexander Leidinger case LINUX_IPV6_ROUTER_ALERT: 3998ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4008ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_ROUTER_ALERT (%d), you can not do user-space routing from linux programs", 4018ec6c4a3SAlexander Leidinger opt); 4028ec6c4a3SAlexander Leidinger return (-2); 4038ec6c4a3SAlexander Leidinger case LINUX_IPV6_MTU_DISCOVER: 4048ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4058ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_MTU_DISCOVER (%d), your linux program can not control path-MTU discovery", 4068ec6c4a3SAlexander Leidinger opt); 4078ec6c4a3SAlexander Leidinger return (-2); 4088ec6c4a3SAlexander Leidinger case LINUX_IPV6_MTU: 4098ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4108ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_MTU (%d), your linux program can not control the MTU on this socket", 4118ec6c4a3SAlexander Leidinger opt); 4128ec6c4a3SAlexander Leidinger return (-2); 4138ec6c4a3SAlexander Leidinger case LINUX_IPV6_JOIN_ANYCAST: 4148ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4158ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_JOIN_ANYCAST (%d)", 4168ec6c4a3SAlexander Leidinger opt); 4178ec6c4a3SAlexander Leidinger return (-2); 4188ec6c4a3SAlexander Leidinger case LINUX_IPV6_LEAVE_ANYCAST: 4198ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4208ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_LEAVE_ANYCAST (%d)", 4218ec6c4a3SAlexander Leidinger opt); 4228ec6c4a3SAlexander Leidinger return (-2); 4238ec6c4a3SAlexander Leidinger case LINUX_IPV6_MULTICAST_ALL: 4248ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4258ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_MULTICAST_ALL (%d)", 4268ec6c4a3SAlexander Leidinger opt); 4278ec6c4a3SAlexander Leidinger return (-2); 4288ec6c4a3SAlexander Leidinger case LINUX_IPV6_ROUTER_ALERT_ISOLATE: 4298ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4308ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_ROUTER_ALERT_ISOLATE (%d)", 4318ec6c4a3SAlexander Leidinger opt); 4328ec6c4a3SAlexander Leidinger return (-2); 4338ec6c4a3SAlexander Leidinger case LINUX_IPV6_FLOWLABEL_MGR: 4348ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4358ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_FLOWLABEL_MGR (%d)", 4368ec6c4a3SAlexander Leidinger opt); 4378ec6c4a3SAlexander Leidinger return (-2); 4388ec6c4a3SAlexander Leidinger case LINUX_IPV6_FLOWINFO_SEND: 4398ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4408ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_FLOWINFO_SEND (%d)", 4418ec6c4a3SAlexander Leidinger opt); 4428ec6c4a3SAlexander Leidinger return (-2); 4438ec6c4a3SAlexander Leidinger case LINUX_IPV6_XFRM_POLICY: 4448ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4458ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_XFRM_POLICY (%d)", 4468ec6c4a3SAlexander Leidinger opt); 4478ec6c4a3SAlexander Leidinger return (-2); 4488ec6c4a3SAlexander Leidinger case LINUX_IPV6_HDRINCL: 4498ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4508ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_HDRINCL (%d)", 4518ec6c4a3SAlexander Leidinger opt); 4528ec6c4a3SAlexander Leidinger return (-2); 4538ec6c4a3SAlexander Leidinger case LINUX_MCAST_BLOCK_SOURCE: 4548ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4558ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option MCAST_BLOCK_SOURCE (%d), your linux program may see more multicast stuff than it wants", 4568ec6c4a3SAlexander Leidinger opt); 4578ec6c4a3SAlexander Leidinger return (-2); 4588ec6c4a3SAlexander Leidinger case LINUX_MCAST_UNBLOCK_SOURCE: 4598ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4608ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option MCAST_UNBLOCK_SOURCE (%d), your linux program may not see all the multicast stuff it wants", 4618ec6c4a3SAlexander Leidinger opt); 4628ec6c4a3SAlexander Leidinger return (-2); 4638ec6c4a3SAlexander Leidinger case LINUX_MCAST_JOIN_SOURCE_GROUP: 4648ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4658ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option MCAST_JOIN_SOURCE_GROUP (%d), your linux program is not able to join a multicast source group", 4668ec6c4a3SAlexander Leidinger opt); 4678ec6c4a3SAlexander Leidinger return (-2); 4688ec6c4a3SAlexander Leidinger case LINUX_MCAST_LEAVE_SOURCE_GROUP: 4698ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4708ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option MCAST_LEAVE_SOURCE_GROUP (%d), your linux program is not able to leave a multicast source group -- but it was also not able to join one, so no issue", 4718ec6c4a3SAlexander Leidinger opt); 4728ec6c4a3SAlexander Leidinger return (-2); 4738ec6c4a3SAlexander Leidinger case LINUX_MCAST_MSFILTER: 4748ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4758ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option MCAST_MSFILTER (%d), your linux program can not manipulate the multicast filter, it may see more multicast data than it wants to see", 4768ec6c4a3SAlexander Leidinger opt); 4778ec6c4a3SAlexander Leidinger return (-2); 4788ec6c4a3SAlexander Leidinger case LINUX_IPV6_ADDR_PREFERENCES: 4798ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4808ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_ADDR_PREFERENCES (%d)", 4818ec6c4a3SAlexander Leidinger opt); 4828ec6c4a3SAlexander Leidinger return (-2); 4838ec6c4a3SAlexander Leidinger case LINUX_IPV6_MINHOPCOUNT: 4848ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4858ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_MINHOPCOUNT (%d)", 4868ec6c4a3SAlexander Leidinger opt); 4878ec6c4a3SAlexander Leidinger return (-2); 4888ec6c4a3SAlexander Leidinger case LINUX_IPV6_TRANSPARENT: 4898ec6c4a3SAlexander Leidinger /* IP_BINDANY or more? */ 4908ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4918ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_TRANSPARENT (%d), you can not enable transparent proxying in linux programs -- note, IP_FREEBIND is supported, no idea if the FreeBSD IP_BINDANY is equivalent to the Linux IP_TRANSPARENT or not, any info is welcome", 4928ec6c4a3SAlexander Leidinger opt); 4938ec6c4a3SAlexander Leidinger return (-2); 4948ec6c4a3SAlexander Leidinger case LINUX_IPV6_UNICAST_IF: 4958ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 4968ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_UNICAST_IF (%d)", 4978ec6c4a3SAlexander Leidinger opt); 4988ec6c4a3SAlexander Leidinger return (-2); 4998ec6c4a3SAlexander Leidinger case LINUX_IPV6_RECVFRAGSIZE: 5008ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1( 5018ec6c4a3SAlexander Leidinger "unsupported IPv6 socket option IPV6_RECVFRAGSIZE (%d)", 5028ec6c4a3SAlexander Leidinger opt); 5038ec6c4a3SAlexander Leidinger return (-2); 5048ec6c4a3SAlexander Leidinger 5058ec6c4a3SAlexander Leidinger /* unknown sockopts */ 5068ec6c4a3SAlexander Leidinger default: 50786a9058bSAndrey V. Elsukov return (-1); 50886a9058bSAndrey V. Elsukov } 5098ec6c4a3SAlexander Leidinger } 51086a9058bSAndrey V. Elsukov 51186a9058bSAndrey V. Elsukov static int 512c21dee17SSøren Schmidt linux_to_bsd_so_sockopt(int opt) 513c21dee17SSøren Schmidt { 5143f3a4815SMarcel Moolenaar 515c21dee17SSøren Schmidt switch (opt) { 516c21dee17SSøren Schmidt case LINUX_SO_DEBUG: 5173f3a4815SMarcel Moolenaar return (SO_DEBUG); 518c21dee17SSøren Schmidt case LINUX_SO_REUSEADDR: 5193f3a4815SMarcel Moolenaar return (SO_REUSEADDR); 520c21dee17SSøren Schmidt case LINUX_SO_TYPE: 5213f3a4815SMarcel Moolenaar return (SO_TYPE); 522c21dee17SSøren Schmidt case LINUX_SO_ERROR: 5233f3a4815SMarcel Moolenaar return (SO_ERROR); 524c21dee17SSøren Schmidt case LINUX_SO_DONTROUTE: 5253f3a4815SMarcel Moolenaar return (SO_DONTROUTE); 526c21dee17SSøren Schmidt case LINUX_SO_BROADCAST: 5273f3a4815SMarcel Moolenaar return (SO_BROADCAST); 528c21dee17SSøren Schmidt case LINUX_SO_SNDBUF: 529c31a6a66SEdward Tomasz Napierala case LINUX_SO_SNDBUFFORCE: 5303f3a4815SMarcel Moolenaar return (SO_SNDBUF); 531c21dee17SSøren Schmidt case LINUX_SO_RCVBUF: 532c31a6a66SEdward Tomasz Napierala case LINUX_SO_RCVBUFFORCE: 5333f3a4815SMarcel Moolenaar return (SO_RCVBUF); 534c21dee17SSøren Schmidt case LINUX_SO_KEEPALIVE: 5353f3a4815SMarcel Moolenaar return (SO_KEEPALIVE); 536c21dee17SSøren Schmidt case LINUX_SO_OOBINLINE: 5373f3a4815SMarcel Moolenaar return (SO_OOBINLINE); 538c21dee17SSøren Schmidt case LINUX_SO_LINGER: 5393f3a4815SMarcel Moolenaar return (SO_LINGER); 5403bc69ad9SEdward Tomasz Napierala case LINUX_SO_REUSEPORT: 5413bc69ad9SEdward Tomasz Napierala return (SO_REUSEPORT_LB); 542443d8a07SConrad Meyer case LINUX_SO_PASSCRED: 543443d8a07SConrad Meyer return (LOCAL_CREDS_PERSISTENT); 544d0b2365eSKonstantin Belousov case LINUX_SO_PEERCRED: 545d0b2365eSKonstantin Belousov return (LOCAL_PEERCRED); 546d0b2365eSKonstantin Belousov case LINUX_SO_RCVLOWAT: 547d0b2365eSKonstantin Belousov return (SO_RCVLOWAT); 548d0b2365eSKonstantin Belousov case LINUX_SO_SNDLOWAT: 549d0b2365eSKonstantin Belousov return (SO_SNDLOWAT); 550d0b2365eSKonstantin Belousov case LINUX_SO_RCVTIMEO: 551d0b2365eSKonstantin Belousov return (SO_RCVTIMEO); 552d0b2365eSKonstantin Belousov case LINUX_SO_SNDTIMEO: 553d0b2365eSKonstantin Belousov return (SO_SNDTIMEO); 5540e26e54bSDmitry Chagin case LINUX_SO_TIMESTAMPO: 5550e26e54bSDmitry Chagin case LINUX_SO_TIMESTAMPN: 556d0b2365eSKonstantin Belousov return (SO_TIMESTAMP); 55771bc8bcfSDmitry Chagin case LINUX_SO_TIMESTAMPNSO: 55871bc8bcfSDmitry Chagin case LINUX_SO_TIMESTAMPNSN: 55971bc8bcfSDmitry Chagin return (SO_BINTIME); 560d0b2365eSKonstantin Belousov case LINUX_SO_ACCEPTCONN: 561d0b2365eSKonstantin Belousov return (SO_ACCEPTCONN); 5624fe5361cSEdward Tomasz Napierala case LINUX_SO_PROTOCOL: 5634fe5361cSEdward Tomasz Napierala return (SO_PROTOCOL); 564cf312f79SDmitry Chagin case LINUX_SO_DOMAIN: 565cf312f79SDmitry Chagin return (SO_DOMAIN); 566c21dee17SSøren Schmidt } 5673f3a4815SMarcel Moolenaar return (-1); 568c21dee17SSøren Schmidt } 569c21dee17SSøren Schmidt 57040dbba57SAssar Westerlund static int 571fb709557SJohn Baldwin linux_to_bsd_tcp_sockopt(int opt) 572fb709557SJohn Baldwin { 573fb709557SJohn Baldwin 574fb709557SJohn Baldwin switch (opt) { 575fb709557SJohn Baldwin case LINUX_TCP_NODELAY: 576fb709557SJohn Baldwin return (TCP_NODELAY); 577fb709557SJohn Baldwin case LINUX_TCP_MAXSEG: 578fb709557SJohn Baldwin return (TCP_MAXSEG); 579c2d47457SEdward Tomasz Napierala case LINUX_TCP_CORK: 580c2d47457SEdward Tomasz Napierala return (TCP_NOPUSH); 581fb709557SJohn Baldwin case LINUX_TCP_KEEPIDLE: 582fb709557SJohn Baldwin return (TCP_KEEPIDLE); 583fb709557SJohn Baldwin case LINUX_TCP_KEEPINTVL: 584fb709557SJohn Baldwin return (TCP_KEEPINTVL); 585fb709557SJohn Baldwin case LINUX_TCP_KEEPCNT: 586fb709557SJohn Baldwin return (TCP_KEEPCNT); 58799f563edSEdward Tomasz Napierala case LINUX_TCP_INFO: 58899f563edSEdward Tomasz Napierala LINUX_RATELIMIT_MSG_OPT1( 58999f563edSEdward Tomasz Napierala "unsupported TCP socket option TCP_INFO (%d)", opt); 59099f563edSEdward Tomasz Napierala return (-2); 591fb709557SJohn Baldwin case LINUX_TCP_MD5SIG: 592fb709557SJohn Baldwin return (TCP_MD5SIG); 593fb709557SJohn Baldwin } 594fb709557SJohn Baldwin return (-1); 595fb709557SJohn Baldwin } 596fb709557SJohn Baldwin 597fb709557SJohn Baldwin static int 59840dbba57SAssar Westerlund linux_to_bsd_msg_flags(int flags) 59940dbba57SAssar Westerlund { 60040dbba57SAssar Westerlund int ret_flags = 0; 60140dbba57SAssar Westerlund 60240dbba57SAssar Westerlund if (flags & LINUX_MSG_OOB) 60340dbba57SAssar Westerlund ret_flags |= MSG_OOB; 60440dbba57SAssar Westerlund if (flags & LINUX_MSG_PEEK) 60540dbba57SAssar Westerlund ret_flags |= MSG_PEEK; 60640dbba57SAssar Westerlund if (flags & LINUX_MSG_DONTROUTE) 60740dbba57SAssar Westerlund ret_flags |= MSG_DONTROUTE; 60840dbba57SAssar Westerlund if (flags & LINUX_MSG_CTRUNC) 60940dbba57SAssar Westerlund ret_flags |= MSG_CTRUNC; 61040dbba57SAssar Westerlund if (flags & LINUX_MSG_TRUNC) 61140dbba57SAssar Westerlund ret_flags |= MSG_TRUNC; 61240dbba57SAssar Westerlund if (flags & LINUX_MSG_DONTWAIT) 61340dbba57SAssar Westerlund ret_flags |= MSG_DONTWAIT; 61440dbba57SAssar Westerlund if (flags & LINUX_MSG_EOR) 61540dbba57SAssar Westerlund ret_flags |= MSG_EOR; 61640dbba57SAssar Westerlund if (flags & LINUX_MSG_WAITALL) 61740dbba57SAssar Westerlund ret_flags |= MSG_WAITALL; 6188d6e40c3SMaxim Sobolev if (flags & LINUX_MSG_NOSIGNAL) 6198d6e40c3SMaxim Sobolev ret_flags |= MSG_NOSIGNAL; 62040dbba57SAssar Westerlund if (flags & LINUX_MSG_PROXY) 6218ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_PROXY (%d) not handled", 6228ec6c4a3SAlexander Leidinger LINUX_MSG_PROXY); 62340dbba57SAssar Westerlund if (flags & LINUX_MSG_FIN) 6248ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_FIN (%d) not handled", 6258ec6c4a3SAlexander Leidinger LINUX_MSG_FIN); 62640dbba57SAssar Westerlund if (flags & LINUX_MSG_SYN) 6278ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_SYN (%d) not handled", 6288ec6c4a3SAlexander Leidinger LINUX_MSG_SYN); 62940dbba57SAssar Westerlund if (flags & LINUX_MSG_CONFIRM) 6308ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_CONFIRM (%d) not handled", 6318ec6c4a3SAlexander Leidinger LINUX_MSG_CONFIRM); 63240dbba57SAssar Westerlund if (flags & LINUX_MSG_RST) 6338ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_RST (%d) not handled", 6348ec6c4a3SAlexander Leidinger LINUX_MSG_RST); 63540dbba57SAssar Westerlund if (flags & LINUX_MSG_ERRQUEUE) 6368ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_ERRQUEUE (%d) not handled", 6378ec6c4a3SAlexander Leidinger LINUX_MSG_ERRQUEUE); 638e667ee63SDmitry Chagin return (ret_flags); 63940dbba57SAssar Westerlund } 64040dbba57SAssar Westerlund 6415a8a13e0SDavid Malone static int 64274f5d680SKonstantin Belousov linux_to_bsd_cmsg_type(int cmsg_type) 6435a8a13e0SDavid Malone { 64474f5d680SKonstantin Belousov 64574f5d680SKonstantin Belousov switch (cmsg_type) { 64674f5d680SKonstantin Belousov case LINUX_SCM_RIGHTS: 64774f5d680SKonstantin Belousov return (SCM_RIGHTS); 648605da56bSAndriy Gapon case LINUX_SCM_CREDENTIALS: 649605da56bSAndriy Gapon return (SCM_CREDS); 65074f5d680SKonstantin Belousov } 65174f5d680SKonstantin Belousov return (-1); 65274f5d680SKonstantin Belousov } 65374f5d680SKonstantin Belousov 65474f5d680SKonstantin Belousov static int 655f8a66150SDmitry Chagin bsd_to_linux_ip_cmsg_type(int cmsg_type) 656f8a66150SDmitry Chagin { 657f8a66150SDmitry Chagin 658f8a66150SDmitry Chagin switch (cmsg_type) { 659f8a66150SDmitry Chagin case IP_RECVORIGDSTADDR: 660f8a66150SDmitry Chagin return (LINUX_IP_RECVORIGDSTADDR); 661f8a66150SDmitry Chagin } 662f8a66150SDmitry Chagin return (-1); 663f8a66150SDmitry Chagin } 664f8a66150SDmitry Chagin 665f8a66150SDmitry Chagin static int 666f8a66150SDmitry Chagin bsd_to_linux_cmsg_type(struct proc *p, int cmsg_type, int cmsg_level) 66774f5d680SKonstantin Belousov { 6680e26e54bSDmitry Chagin struct linux_pemuldata *pem; 6690e26e54bSDmitry Chagin 670f8a66150SDmitry Chagin if (cmsg_level == IPPROTO_IP) 671f8a66150SDmitry Chagin return (bsd_to_linux_ip_cmsg_type(cmsg_type)); 672f8a66150SDmitry Chagin if (cmsg_level != SOL_SOCKET) 673f8a66150SDmitry Chagin return (-1); 674f8a66150SDmitry Chagin 6750e26e54bSDmitry Chagin pem = pem_find(p); 67674f5d680SKonstantin Belousov 67774f5d680SKonstantin Belousov switch (cmsg_type) { 67874f5d680SKonstantin Belousov case SCM_RIGHTS: 67974f5d680SKonstantin Belousov return (LINUX_SCM_RIGHTS); 680605da56bSAndriy Gapon case SCM_CREDS: 681605da56bSAndriy Gapon return (LINUX_SCM_CREDENTIALS); 682ede4af47SConrad Meyer case SCM_CREDS2: 683ede4af47SConrad Meyer return (LINUX_SCM_CREDENTIALS); 684bbf392d5SDmitry Chagin case SCM_TIMESTAMP: 6850e26e54bSDmitry Chagin return (pem->so_timestamp); 68671bc8bcfSDmitry Chagin case SCM_BINTIME: 68771bc8bcfSDmitry Chagin return (pem->so_timestampns); 68874f5d680SKonstantin Belousov } 68974f5d680SKonstantin Belousov return (-1); 69074f5d680SKonstantin Belousov } 69174f5d680SKonstantin Belousov 69274f5d680SKonstantin Belousov static int 69374f5d680SKonstantin Belousov linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr) 69474f5d680SKonstantin Belousov { 69574f5d680SKonstantin Belousov if (lhdr->msg_controllen > INT_MAX) 69674f5d680SKonstantin Belousov return (ENOBUFS); 69774f5d680SKonstantin Belousov 69874f5d680SKonstantin Belousov bhdr->msg_name = PTRIN(lhdr->msg_name); 69974f5d680SKonstantin Belousov bhdr->msg_namelen = lhdr->msg_namelen; 70074f5d680SKonstantin Belousov bhdr->msg_iov = PTRIN(lhdr->msg_iov); 70174f5d680SKonstantin Belousov bhdr->msg_iovlen = lhdr->msg_iovlen; 70274f5d680SKonstantin Belousov bhdr->msg_control = PTRIN(lhdr->msg_control); 703605da56bSAndriy Gapon 704605da56bSAndriy Gapon /* 705605da56bSAndriy Gapon * msg_controllen is skipped since BSD and LINUX control messages 706605da56bSAndriy Gapon * are potentially different sizes (e.g. the cred structure used 707605da56bSAndriy Gapon * by SCM_CREDS is different between the two operating system). 708605da56bSAndriy Gapon * 709605da56bSAndriy Gapon * The caller can set it (if necessary) after converting all the 710605da56bSAndriy Gapon * control messages. 711605da56bSAndriy Gapon */ 712605da56bSAndriy Gapon 71374f5d680SKonstantin Belousov bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags); 71474f5d680SKonstantin Belousov return (0); 71574f5d680SKonstantin Belousov } 71674f5d680SKonstantin Belousov 71774f5d680SKonstantin Belousov static int 71874f5d680SKonstantin Belousov bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr) 71974f5d680SKonstantin Belousov { 72074f5d680SKonstantin Belousov lhdr->msg_name = PTROUT(bhdr->msg_name); 72174f5d680SKonstantin Belousov lhdr->msg_namelen = bhdr->msg_namelen; 72274f5d680SKonstantin Belousov lhdr->msg_iov = PTROUT(bhdr->msg_iov); 72374f5d680SKonstantin Belousov lhdr->msg_iovlen = bhdr->msg_iovlen; 72474f5d680SKonstantin Belousov lhdr->msg_control = PTROUT(bhdr->msg_control); 725605da56bSAndriy Gapon 726605da56bSAndriy Gapon /* 727605da56bSAndriy Gapon * msg_controllen is skipped since BSD and LINUX control messages 728605da56bSAndriy Gapon * are potentially different sizes (e.g. the cred structure used 729605da56bSAndriy Gapon * by SCM_CREDS is different between the two operating system). 730605da56bSAndriy Gapon * 731605da56bSAndriy Gapon * The caller can set it (if necessary) after converting all the 732605da56bSAndriy Gapon * control messages. 733605da56bSAndriy Gapon */ 734605da56bSAndriy Gapon 73574f5d680SKonstantin Belousov /* msg_flags skipped */ 73674f5d680SKonstantin Belousov return (0); 73774f5d680SKonstantin Belousov } 73874f5d680SKonstantin Belousov 73974f5d680SKonstantin Belousov static int 7404cf10e29SDmitry Chagin linux_set_socket_flags(int lflags, int *flags) 74138a18e97SDmitry Chagin { 74238a18e97SDmitry Chagin 7434cf10e29SDmitry Chagin if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK)) 7444cf10e29SDmitry Chagin return (EINVAL); 7454cf10e29SDmitry Chagin if (lflags & LINUX_SOCK_NONBLOCK) 7464cf10e29SDmitry Chagin *flags |= SOCK_NONBLOCK; 7474cf10e29SDmitry Chagin if (lflags & LINUX_SOCK_CLOEXEC) 7484cf10e29SDmitry Chagin *flags |= SOCK_CLOEXEC; 74938a18e97SDmitry Chagin return (0); 75038a18e97SDmitry Chagin } 75138a18e97SDmitry Chagin 75238a18e97SDmitry Chagin static int 753106a784bSEdward Tomasz Napierala linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len) 754106a784bSEdward Tomasz Napierala { 755106a784bSEdward Tomasz Napierala struct l_sockaddr *lsa; 756106a784bSEdward Tomasz Napierala int error; 757106a784bSEdward Tomasz Napierala 758106a784bSEdward Tomasz Napierala error = bsd_to_linux_sockaddr(sa, &lsa, len); 759106a784bSEdward Tomasz Napierala if (error != 0) 760106a784bSEdward Tomasz Napierala return (error); 761106a784bSEdward Tomasz Napierala 762106a784bSEdward Tomasz Napierala error = copyout(lsa, uaddr, len); 76363355839SDmitry Chagin free(lsa, M_LINUX); 764106a784bSEdward Tomasz Napierala 765106a784bSEdward Tomasz Napierala return (error); 766106a784bSEdward Tomasz Napierala } 767106a784bSEdward Tomasz Napierala 768106a784bSEdward Tomasz Napierala static int 76974f5d680SKonstantin Belousov linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags, 77074f5d680SKonstantin Belousov struct mbuf *control, enum uio_seg segflg) 77174f5d680SKonstantin Belousov { 7725a8a13e0SDavid Malone struct sockaddr *to; 773d5368bf3SDmitry Chagin int error, len; 7745a8a13e0SDavid Malone 7755a8a13e0SDavid Malone if (mp->msg_name != NULL) { 776d5368bf3SDmitry Chagin len = mp->msg_namelen; 777d5368bf3SDmitry Chagin error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len); 778dddb7e7fSDmitry Chagin if (error != 0) 7795a8a13e0SDavid Malone return (error); 7805a8a13e0SDavid Malone mp->msg_name = to; 7815a8a13e0SDavid Malone } else 7825a8a13e0SDavid Malone to = NULL; 7835a8a13e0SDavid Malone 784a6886ef1SMaxim Sobolev error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control, 785a6886ef1SMaxim Sobolev segflg); 7865a8a13e0SDavid Malone 7875a8a13e0SDavid Malone if (to) 7881ede983cSDag-Erling Smørgrav free(to, M_SONAME); 7895a8a13e0SDavid Malone return (error); 7905a8a13e0SDavid Malone } 7915a8a13e0SDavid Malone 7923f3a4815SMarcel Moolenaar /* Return 0 if IP_HDRINCL is set for the given socket. */ 793f2477ae1SMike Smith static int 794e140eb43SDavid Malone linux_check_hdrincl(struct thread *td, int s) 795f2477ae1SMike Smith { 796857ad5a3SDmitry Chagin int error, optval; 797857ad5a3SDmitry Chagin socklen_t size_val; 798f2477ae1SMike Smith 799e140eb43SDavid Malone size_val = sizeof(optval); 800e140eb43SDavid Malone error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL, 801e140eb43SDavid Malone &optval, UIO_SYSSPACE, &size_val); 802dddb7e7fSDmitry Chagin if (error != 0) 8033f3a4815SMarcel Moolenaar return (error); 8043f3a4815SMarcel Moolenaar 8053f3a4815SMarcel Moolenaar return (optval == 0); 806f2477ae1SMike Smith } 807f2477ae1SMike Smith 808f2477ae1SMike Smith /* 809f2477ae1SMike Smith * Updated sendto() when IP_HDRINCL is set: 810f2477ae1SMike Smith * tweak endian-dependent fields in the IP packet. 811f2477ae1SMike Smith */ 812f2477ae1SMike Smith static int 81338da2381SRobert Watson linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args) 814f2477ae1SMike Smith { 815f2477ae1SMike Smith /* 816f2477ae1SMike Smith * linux_ip_copysize defines how many bytes we should copy 817f2477ae1SMike Smith * from the beginning of the IP packet before we customize it for BSD. 818a6886ef1SMaxim Sobolev * It should include all the fields we modify (ip_len and ip_off). 819f2477ae1SMike Smith */ 820f2477ae1SMike Smith #define linux_ip_copysize 8 821f2477ae1SMike Smith 822f2477ae1SMike Smith struct ip *packet; 8235a8a13e0SDavid Malone struct msghdr msg; 824a6886ef1SMaxim Sobolev struct iovec aiov[1]; 825f2477ae1SMike Smith int error; 826f2477ae1SMike Smith 827aa675b57SDavid Schultz /* Check that the packet isn't too big or too small. */ 828aa675b57SDavid Schultz if (linux_args->len < linux_ip_copysize || 829aa675b57SDavid Schultz linux_args->len > IP_MAXPACKET) 8303f3a4815SMarcel Moolenaar return (EINVAL); 831f2477ae1SMike Smith 832e0d3ea8cSDmitry Chagin packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK); 833f2477ae1SMike Smith 834a6886ef1SMaxim Sobolev /* Make kernel copy of the packet to be sent */ 8354af27623STim J. Robbins if ((error = copyin(PTRIN(linux_args->msg), packet, 836a6886ef1SMaxim Sobolev linux_args->len))) 837a6886ef1SMaxim Sobolev goto goout; 838f2477ae1SMike Smith 839f2477ae1SMike Smith /* Convert fields from Linux to BSD raw IP socket format */ 8405a8a13e0SDavid Malone packet->ip_len = linux_args->len; 841f2477ae1SMike Smith packet->ip_off = ntohs(packet->ip_off); 842f2477ae1SMike Smith 843f2477ae1SMike Smith /* Prepare the msghdr and iovec structures describing the new packet */ 8444af27623STim J. Robbins msg.msg_name = PTRIN(linux_args->to); 8455a8a13e0SDavid Malone msg.msg_namelen = linux_args->tolen; 8465a8a13e0SDavid Malone msg.msg_iov = aiov; 847a6886ef1SMaxim Sobolev msg.msg_iovlen = 1; 8485a8a13e0SDavid Malone msg.msg_control = NULL; 8495a8a13e0SDavid Malone msg.msg_flags = 0; 8505a8a13e0SDavid Malone aiov[0].iov_base = (char *)packet; 851a6886ef1SMaxim Sobolev aiov[0].iov_len = linux_args->len; 852a6886ef1SMaxim Sobolev error = linux_sendit(td, linux_args->s, &msg, linux_args->flags, 85374f5d680SKonstantin Belousov NULL, UIO_SYSSPACE); 854a6886ef1SMaxim Sobolev goout: 855e0d3ea8cSDmitry Chagin free(packet, M_LINUX); 8565a8a13e0SDavid Malone return (error); 857f2477ae1SMike Smith } 858f2477ae1SMike Smith 8599e47480eSConrad Meyer static const char *linux_netlink_names[] = { 8609e47480eSConrad Meyer [LINUX_NETLINK_ROUTE] = "ROUTE", 8619e47480eSConrad Meyer [LINUX_NETLINK_SOCK_DIAG] = "SOCK_DIAG", 8629e47480eSConrad Meyer [LINUX_NETLINK_NFLOG] = "NFLOG", 8639e47480eSConrad Meyer [LINUX_NETLINK_SELINUX] = "SELINUX", 8649e47480eSConrad Meyer [LINUX_NETLINK_AUDIT] = "AUDIT", 8659e47480eSConrad Meyer [LINUX_NETLINK_FIB_LOOKUP] = "FIB_LOOKUP", 8669e47480eSConrad Meyer [LINUX_NETLINK_NETFILTER] = "NETFILTER", 8679e47480eSConrad Meyer [LINUX_NETLINK_KOBJECT_UEVENT] = "KOBJECT_UEVENT", 8689e47480eSConrad Meyer }; 8699e47480eSConrad Meyer 870a12b9b3dSDmitry Chagin int 871b40ce416SJulian Elischer linux_socket(struct thread *td, struct linux_socket_args *args) 872c21dee17SSøren Schmidt { 873d293f35cSEdward Tomasz Napierala int domain, retval_socket, type; 874c21dee17SSøren Schmidt 875d293f35cSEdward Tomasz Napierala type = args->type & LINUX_SOCK_TYPE_MASK; 876d293f35cSEdward Tomasz Napierala if (type < 0 || type > LINUX_SOCK_MAX) 877eeb63e51SDmitry Chagin return (EINVAL); 8784cf10e29SDmitry Chagin retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK, 879d293f35cSEdward Tomasz Napierala &type); 8804cf10e29SDmitry Chagin if (retval_socket != 0) 8814cf10e29SDmitry Chagin return (retval_socket); 882d293f35cSEdward Tomasz Napierala domain = linux_to_bsd_domain(args->domain); 883f4d91df5SEdward Tomasz Napierala if (domain == -1) { 8849e47480eSConrad Meyer /* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */ 8859e47480eSConrad Meyer type = args->type & LINUX_SOCK_TYPE_MASK; 886feb96ee9SEdward Tomasz Napierala if (args->domain == LINUX_AF_NETLINK && 887feb96ee9SEdward Tomasz Napierala args->protocol == LINUX_NETLINK_AUDIT) { 888feb96ee9SEdward Tomasz Napierala ; /* Do nothing, quietly. */ 889feb96ee9SEdward Tomasz Napierala } else if (args->domain == LINUX_AF_NETLINK) { 8909e47480eSConrad Meyer const char *nl_name; 891b3be0b4dSEdward Tomasz Napierala 8929e47480eSConrad Meyer if (args->protocol >= 0 && 8939e47480eSConrad Meyer args->protocol < nitems(linux_netlink_names)) 8949e47480eSConrad Meyer nl_name = linux_netlink_names[args->protocol]; 8959e47480eSConrad Meyer else 8969e47480eSConrad Meyer nl_name = NULL; 8979e47480eSConrad Meyer if (nl_name != NULL) 898b3be0b4dSEdward Tomasz Napierala linux_msg(curthread, 8999e47480eSConrad Meyer "unsupported socket(AF_NETLINK, %d, " 9009e47480eSConrad Meyer "NETLINK_%s)", type, nl_name); 9019e47480eSConrad Meyer else 9029e47480eSConrad Meyer linux_msg(curthread, 9039e47480eSConrad Meyer "unsupported socket(AF_NETLINK, %d, %d)", 9049e47480eSConrad Meyer type, args->protocol); 9059e47480eSConrad Meyer } else { 9069e47480eSConrad Meyer linux_msg(curthread, "unsupported socket domain %d, " 9079e47480eSConrad Meyer "type %d, protocol %d", args->domain, type, 9089e47480eSConrad Meyer args->protocol); 909b3be0b4dSEdward Tomasz Napierala } 910d9b063ccSDmitry Chagin return (EAFNOSUPPORT); 911f4d91df5SEdward Tomasz Napierala } 912f2477ae1SMike Smith 913d293f35cSEdward Tomasz Napierala retval_socket = kern_socket(td, domain, type, args->protocol); 9146994ea54SDmitry Chagin if (retval_socket) 9156994ea54SDmitry Chagin return (retval_socket); 9166994ea54SDmitry Chagin 917d293f35cSEdward Tomasz Napierala if (type == SOCK_RAW 918d293f35cSEdward Tomasz Napierala && (args->protocol == IPPROTO_RAW || args->protocol == 0) 919d293f35cSEdward Tomasz Napierala && domain == PF_INET) { 920f2477ae1SMike Smith /* It's a raw IP socket: set the IP_HDRINCL option. */ 921e140eb43SDavid Malone int hdrincl; 922f2477ae1SMike Smith 923e140eb43SDavid Malone hdrincl = 1; 924e140eb43SDavid Malone /* We ignore any error returned by kern_setsockopt() */ 925e140eb43SDavid Malone kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL, 926e140eb43SDavid Malone &hdrincl, UIO_SYSSPACE, sizeof(hdrincl)); 927f2477ae1SMike Smith } 928ca26842eSHajimu UMEMOTO #ifdef INET6 929ca26842eSHajimu UMEMOTO /* 930d97bee3eSBjoern A. Zeeb * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default 931d97bee3eSBjoern A. Zeeb * and some apps depend on this. So, set V6ONLY to 0 for Linux apps. 932d97bee3eSBjoern A. Zeeb * For simplicity we do this unconditionally of the net.inet6.ip6.v6only 933d97bee3eSBjoern A. Zeeb * sysctl value. 934ca26842eSHajimu UMEMOTO */ 935d293f35cSEdward Tomasz Napierala if (domain == PF_INET6) { 936e140eb43SDavid Malone int v6only; 937ca26842eSHajimu UMEMOTO 938e140eb43SDavid Malone v6only = 0; 939ca26842eSHajimu UMEMOTO /* We ignore any error returned by setsockopt() */ 940e140eb43SDavid Malone kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY, 941e140eb43SDavid Malone &v6only, UIO_SYSSPACE, sizeof(v6only)); 942ca26842eSHajimu UMEMOTO } 943ca26842eSHajimu UMEMOTO #endif 9443f3a4815SMarcel Moolenaar 9453f3a4815SMarcel Moolenaar return (retval_socket); 946c21dee17SSøren Schmidt } 947c21dee17SSøren Schmidt 948a12b9b3dSDmitry Chagin int 949b40ce416SJulian Elischer linux_bind(struct thread *td, struct linux_bind_args *args) 950c21dee17SSøren Schmidt { 951ca26842eSHajimu UMEMOTO struct sockaddr *sa; 952c21dee17SSøren Schmidt int error; 953c21dee17SSøren Schmidt 954d5368bf3SDmitry Chagin error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa, 955d5368bf3SDmitry Chagin &args->namelen); 956dddb7e7fSDmitry Chagin if (error != 0) 957ca26842eSHajimu UMEMOTO return (error); 958ca26842eSHajimu UMEMOTO 9596e646651SKonstantin Belousov error = kern_bindat(td, AT_FDCWD, args->s, sa); 960b33887eaSJohn Baldwin free(sa, M_SONAME); 961d5368bf3SDmitry Chagin 962d5368bf3SDmitry Chagin /* XXX */ 963745aaef5SKonstantin Belousov if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in)) 964d4b7423fSAlexander Leidinger return (EINVAL); 965b33887eaSJohn Baldwin return (error); 966c21dee17SSøren Schmidt } 967c21dee17SSøren Schmidt 968930a65feSAndrew Gallatin int 969b40ce416SJulian Elischer linux_connect(struct thread *td, struct linux_connect_args *args) 970c21dee17SSøren Schmidt { 9710bf301c0SJonathan Lemon struct socket *so; 972ca26842eSHajimu UMEMOTO struct sockaddr *sa; 97395653579SGleb Smirnoff struct file *fp; 974c21dee17SSøren Schmidt int error; 975c21dee17SSøren Schmidt 976d5368bf3SDmitry Chagin error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa, 977d5368bf3SDmitry Chagin &args->namelen); 978dddb7e7fSDmitry Chagin if (error != 0) 979ca26842eSHajimu UMEMOTO return (error); 980ca26842eSHajimu UMEMOTO 9816e646651SKonstantin Belousov error = kern_connectat(td, AT_FDCWD, args->s, sa); 982b33887eaSJohn Baldwin free(sa, M_SONAME); 9830bf301c0SJonathan Lemon if (error != EISCONN) 9840bf301c0SJonathan Lemon return (error); 9850bf301c0SJonathan Lemon 986dad3b88aSMike Smith /* 987dad3b88aSMike Smith * Linux doesn't return EISCONN the first time it occurs, 988dad3b88aSMike Smith * when on a non-blocking socket. Instead it returns the 989dad3b88aSMike Smith * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD. 990dad3b88aSMike Smith */ 9913212ad15SMateusz Guzik error = getsock(td, args->s, &cap_connect_rights, &fp); 99295653579SGleb Smirnoff if (error != 0) 99395653579SGleb Smirnoff return (error); 99495653579SGleb Smirnoff 9950bf301c0SJonathan Lemon error = EISCONN; 99695653579SGleb Smirnoff so = fp->f_data; 9973212ad15SMateusz Guzik if (atomic_load_int(&fp->f_flag) & FNONBLOCK) { 9984641373fSJohn Baldwin SOCK_LOCK(so); 9995002a60fSMarcel Moolenaar if (so->so_emuldata == 0) 10000bf301c0SJonathan Lemon error = so->so_error; 10010bf301c0SJonathan Lemon so->so_emuldata = (void *)1; 10024641373fSJohn Baldwin SOCK_UNLOCK(so); 1003dad3b88aSMike Smith } 100495653579SGleb Smirnoff fdrop(fp, td); 100595653579SGleb Smirnoff 10063f3a4815SMarcel Moolenaar return (error); 1007c21dee17SSøren Schmidt } 1008c21dee17SSøren Schmidt 1009a12b9b3dSDmitry Chagin int 1010b40ce416SJulian Elischer linux_listen(struct thread *td, struct linux_listen_args *args) 1011c21dee17SSøren Schmidt { 1012c21dee17SSøren Schmidt 1013d293f35cSEdward Tomasz Napierala return (kern_listen(td, args->s, args->backlog)); 1014c21dee17SSøren Schmidt } 1015c21dee17SSøren Schmidt 101601e0ffbaSAlexander Leidinger static int 1017c8f37d61SDmitry Chagin linux_accept_common(struct thread *td, int s, l_uintptr_t addr, 1018f83427b8SDmitry Chagin l_uintptr_t namelen, int flags) 1019c21dee17SSøren Schmidt { 1020*cfb1e929SGleb Smirnoff struct sockaddr_storage ss = { .ss_len = sizeof(ss) }; 10216d76adbbSEdward Tomasz Napierala struct file *fp, *fp1; 1022d5368bf3SDmitry Chagin struct socket *so; 1023*cfb1e929SGleb Smirnoff socklen_t len; 1024*cfb1e929SGleb Smirnoff int bflags, error, error1; 102593e694c9SDmitry Chagin 1026d5368bf3SDmitry Chagin bflags = 0; 10276d76adbbSEdward Tomasz Napierala fp = NULL; 10286d76adbbSEdward Tomasz Napierala 1029d5368bf3SDmitry Chagin error = linux_set_socket_flags(flags, &bflags); 10304cf10e29SDmitry Chagin if (error != 0) 10314cf10e29SDmitry Chagin return (error); 1032d5368bf3SDmitry Chagin 1033*cfb1e929SGleb Smirnoff if (PTRIN(addr) != NULL) { 1034d5368bf3SDmitry Chagin error = copyin(PTRIN(namelen), &len, sizeof(len)); 1035d5368bf3SDmitry Chagin if (error != 0) 1036d5368bf3SDmitry Chagin return (error); 1037d5368bf3SDmitry Chagin if (len < 0) 1038d4b7423fSAlexander Leidinger return (EINVAL); 1039*cfb1e929SGleb Smirnoff } else 1040*cfb1e929SGleb Smirnoff len = 0; 1041*cfb1e929SGleb Smirnoff 1042*cfb1e929SGleb Smirnoff error = kern_accept4(td, s, (struct sockaddr *)&ss, bflags, &fp); 1043d5368bf3SDmitry Chagin 10446d76adbbSEdward Tomasz Napierala /* 10456d76adbbSEdward Tomasz Napierala * Translate errno values into ones used by Linux. 10466d76adbbSEdward Tomasz Napierala */ 1047d5368bf3SDmitry Chagin if (error != 0) { 1048d5368bf3SDmitry Chagin /* 1049d5368bf3SDmitry Chagin * XXX. This is wrong, different sockaddr structures 1050d5368bf3SDmitry Chagin * have different sizes. 1051d5368bf3SDmitry Chagin */ 10526d76adbbSEdward Tomasz Napierala switch (error) { 10536d76adbbSEdward Tomasz Napierala case EFAULT: 10546d76adbbSEdward Tomasz Napierala if (namelen != sizeof(struct sockaddr_in)) 1055d5368bf3SDmitry Chagin error = EINVAL; 10566d76adbbSEdward Tomasz Napierala break; 10576d76adbbSEdward Tomasz Napierala case EINVAL: 10583212ad15SMateusz Guzik error1 = getsock(td, s, &cap_accept_rights, &fp1); 1059d5368bf3SDmitry Chagin if (error1 != 0) { 1060d5368bf3SDmitry Chagin error = error1; 10616d76adbbSEdward Tomasz Napierala break; 1062d5368bf3SDmitry Chagin } 10636d76adbbSEdward Tomasz Napierala so = fp1->f_data; 1064d5368bf3SDmitry Chagin if (so->so_type == SOCK_DGRAM) 1065d5368bf3SDmitry Chagin error = EOPNOTSUPP; 10666d76adbbSEdward Tomasz Napierala fdrop(fp1, td); 10676d76adbbSEdward Tomasz Napierala break; 106891f514e4SDmitry Chagin } 10696d76adbbSEdward Tomasz Napierala return (error); 1070d4b7423fSAlexander Leidinger } 1071d5368bf3SDmitry Chagin 1072*cfb1e929SGleb Smirnoff if (PTRIN(addr) != NULL) { 1073*cfb1e929SGleb Smirnoff len = min(ss.ss_len, len); 1074*cfb1e929SGleb Smirnoff error = linux_copyout_sockaddr((struct sockaddr *)&ss, 1075*cfb1e929SGleb Smirnoff PTRIN(addr), len); 1076*cfb1e929SGleb Smirnoff if (error == 0) { 1077*cfb1e929SGleb Smirnoff len = ss.ss_len; 1078*cfb1e929SGleb Smirnoff error = copyout(&len, PTRIN(namelen), sizeof(len)); 1079*cfb1e929SGleb Smirnoff } 1080dddb7e7fSDmitry Chagin if (error != 0) { 10816d76adbbSEdward Tomasz Napierala fdclose(td, fp, td->td_retval[0]); 108293e694c9SDmitry Chagin td->td_retval[0] = 0; 108393e694c9SDmitry Chagin } 10846d76adbbSEdward Tomasz Napierala } 10856d76adbbSEdward Tomasz Napierala if (fp != NULL) 10866d76adbbSEdward Tomasz Napierala fdrop(fp, td); 108793e694c9SDmitry Chagin return (error); 1088c21dee17SSøren Schmidt } 1089c21dee17SSøren Schmidt 1090a12b9b3dSDmitry Chagin int 1091c8f37d61SDmitry Chagin linux_accept(struct thread *td, struct linux_accept_args *args) 1092c8f37d61SDmitry Chagin { 1093c8f37d61SDmitry Chagin 1094c8f37d61SDmitry Chagin return (linux_accept_common(td, args->s, args->addr, 1095f83427b8SDmitry Chagin args->namelen, 0)); 1096c8f37d61SDmitry Chagin } 1097c8f37d61SDmitry Chagin 1098a12b9b3dSDmitry Chagin int 1099f8cd0af2SDmitry Chagin linux_accept4(struct thread *td, struct linux_accept4_args *args) 1100f8cd0af2SDmitry Chagin { 1101f8cd0af2SDmitry Chagin 1102f8cd0af2SDmitry Chagin return (linux_accept_common(td, args->s, args->addr, 1103f8cd0af2SDmitry Chagin args->namelen, args->flags)); 1104f8cd0af2SDmitry Chagin } 1105f8cd0af2SDmitry Chagin 1106a12b9b3dSDmitry Chagin int 1107b40ce416SJulian Elischer linux_getsockname(struct thread *td, struct linux_getsockname_args *args) 1108c21dee17SSøren Schmidt { 1109d5368bf3SDmitry Chagin struct sockaddr *sa; 1110d5368bf3SDmitry Chagin int len, error; 1111c21dee17SSøren Schmidt 1112d5368bf3SDmitry Chagin error = copyin(PTRIN(args->namelen), &len, sizeof(len)); 1113dddb7e7fSDmitry Chagin if (error != 0) 1114ca26842eSHajimu UMEMOTO return (error); 1115d5368bf3SDmitry Chagin 1116d5368bf3SDmitry Chagin error = kern_getsockname(td, args->s, &sa, &len); 1117d5368bf3SDmitry Chagin if (error != 0) 1118d5368bf3SDmitry Chagin return (error); 1119d5368bf3SDmitry Chagin 1120106a784bSEdward Tomasz Napierala if (len != 0) 1121106a784bSEdward Tomasz Napierala error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len); 1122d5368bf3SDmitry Chagin 1123d5368bf3SDmitry Chagin free(sa, M_SONAME); 1124d5368bf3SDmitry Chagin if (error == 0) 1125d5368bf3SDmitry Chagin error = copyout(&len, PTRIN(args->namelen), sizeof(len)); 1126d5368bf3SDmitry Chagin return (error); 1127c21dee17SSøren Schmidt } 1128c21dee17SSøren Schmidt 1129a12b9b3dSDmitry Chagin int 1130b40ce416SJulian Elischer linux_getpeername(struct thread *td, struct linux_getpeername_args *args) 1131c21dee17SSøren Schmidt { 1132d5368bf3SDmitry Chagin struct sockaddr *sa; 1133d5368bf3SDmitry Chagin int len, error; 1134c21dee17SSøren Schmidt 1135d5368bf3SDmitry Chagin error = copyin(PTRIN(args->namelen), &len, sizeof(len)); 1136dddb7e7fSDmitry Chagin if (error != 0) 1137ca26842eSHajimu UMEMOTO return (error); 1138caaad873SDmitry Chagin if (len < 0) 1139caaad873SDmitry Chagin return (EINVAL); 1140d5368bf3SDmitry Chagin 1141d5368bf3SDmitry Chagin error = kern_getpeername(td, args->s, &sa, &len); 1142d5368bf3SDmitry Chagin if (error != 0) 1143d5368bf3SDmitry Chagin return (error); 1144d5368bf3SDmitry Chagin 1145106a784bSEdward Tomasz Napierala if (len != 0) 1146106a784bSEdward Tomasz Napierala error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len); 1147d5368bf3SDmitry Chagin 1148d5368bf3SDmitry Chagin free(sa, M_SONAME); 1149d5368bf3SDmitry Chagin if (error == 0) 1150d5368bf3SDmitry Chagin error = copyout(&len, PTRIN(args->namelen), sizeof(len)); 1151d5368bf3SDmitry Chagin return (error); 1152c21dee17SSøren Schmidt } 1153c21dee17SSøren Schmidt 1154a12b9b3dSDmitry Chagin int 1155b40ce416SJulian Elischer linux_socketpair(struct thread *td, struct linux_socketpair_args *args) 1156c21dee17SSøren Schmidt { 11570b40dcbeSEdward Tomasz Napierala int domain, error, sv[2], type; 1158c21dee17SSøren Schmidt 11590b40dcbeSEdward Tomasz Napierala domain = linux_to_bsd_domain(args->domain); 11600b40dcbeSEdward Tomasz Napierala if (domain != PF_LOCAL) 11611a52a4abSDmitry Chagin return (EAFNOSUPPORT); 11620b40dcbeSEdward Tomasz Napierala type = args->type & LINUX_SOCK_TYPE_MASK; 11630b40dcbeSEdward Tomasz Napierala if (type < 0 || type > LINUX_SOCK_MAX) 116439253cf9SDmitry Chagin return (EINVAL); 11654cf10e29SDmitry Chagin error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK, 11660b40dcbeSEdward Tomasz Napierala &type); 11674cf10e29SDmitry Chagin if (error != 0) 11684cf10e29SDmitry Chagin return (error); 11690b40dcbeSEdward Tomasz Napierala if (args->protocol != 0 && args->protocol != PF_UNIX) { 11701a52a4abSDmitry Chagin /* 11711a52a4abSDmitry Chagin * Use of PF_UNIX as protocol argument is not right, 11721a52a4abSDmitry Chagin * but Linux does it. 11731a52a4abSDmitry Chagin * Do not map PF_UNIX as its Linux value is identical 11741a52a4abSDmitry Chagin * to FreeBSD one. 11751a52a4abSDmitry Chagin */ 11761a52a4abSDmitry Chagin return (EPROTONOSUPPORT); 11770b40dcbeSEdward Tomasz Napierala } 11780b40dcbeSEdward Tomasz Napierala error = kern_socketpair(td, domain, type, 0, sv); 11790b40dcbeSEdward Tomasz Napierala if (error != 0) 11800b40dcbeSEdward Tomasz Napierala return (error); 11810b40dcbeSEdward Tomasz Napierala error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int)); 11820b40dcbeSEdward Tomasz Napierala if (error != 0) { 11830b40dcbeSEdward Tomasz Napierala (void)kern_close(td, sv[0]); 11840b40dcbeSEdward Tomasz Napierala (void)kern_close(td, sv[1]); 11850b40dcbeSEdward Tomasz Napierala } 11860b40dcbeSEdward Tomasz Napierala return (error); 1187c21dee17SSøren Schmidt } 1188c21dee17SSøren Schmidt 1189a12b9b3dSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 119001e0ffbaSAlexander Leidinger struct linux_send_args { 119156fba8e6SDmitry Chagin register_t s; 119256fba8e6SDmitry Chagin register_t msg; 119356fba8e6SDmitry Chagin register_t len; 119456fba8e6SDmitry Chagin register_t flags; 1195c21dee17SSøren Schmidt }; 1196c21dee17SSøren Schmidt 119701e0ffbaSAlexander Leidinger static int 1198b40ce416SJulian Elischer linux_send(struct thread *td, struct linux_send_args *args) 1199c21dee17SSøren Schmidt { 120087d72a8fSPoul-Henning Kamp struct sendto_args /* { 1201c21dee17SSøren Schmidt int s; 1202c21dee17SSøren Schmidt caddr_t buf; 1203044af7c3SJonathan Mini int len; 1204c21dee17SSøren Schmidt int flags; 120587d72a8fSPoul-Henning Kamp caddr_t to; 120687d72a8fSPoul-Henning Kamp int tolen; 1207ef04503dSPeter Wemm } */ bsd_args; 1208aa288712SDmitry Chagin struct file *fp; 12093212ad15SMateusz Guzik int error; 1210c21dee17SSøren Schmidt 1211745aaef5SKonstantin Belousov bsd_args.s = args->s; 1212745aaef5SKonstantin Belousov bsd_args.buf = (caddr_t)PTRIN(args->msg); 1213745aaef5SKonstantin Belousov bsd_args.len = args->len; 1214f6e8256aSEdward Tomasz Napierala bsd_args.flags = linux_to_bsd_msg_flags(args->flags); 121587d72a8fSPoul-Henning Kamp bsd_args.to = NULL; 121687d72a8fSPoul-Henning Kamp bsd_args.tolen = 0; 1217aa288712SDmitry Chagin error = sys_sendto(td, &bsd_args); 1218aa288712SDmitry Chagin if (error == ENOTCONN) { 1219aa288712SDmitry Chagin /* 1220aa288712SDmitry Chagin * Linux doesn't return ENOTCONN for non-blocking sockets. 1221aa288712SDmitry Chagin * Instead it returns the EAGAIN. 1222aa288712SDmitry Chagin */ 12233212ad15SMateusz Guzik error = getsock(td, args->s, &cap_send_rights, &fp); 1224aa288712SDmitry Chagin if (error == 0) { 12253212ad15SMateusz Guzik if (atomic_load_int(&fp->f_flag) & FNONBLOCK) 1226aa288712SDmitry Chagin error = EAGAIN; 1227aa288712SDmitry Chagin fdrop(fp, td); 1228aa288712SDmitry Chagin } 1229aa288712SDmitry Chagin } 1230aa288712SDmitry Chagin return (error); 1231c21dee17SSøren Schmidt } 1232c21dee17SSøren Schmidt 123301e0ffbaSAlexander Leidinger struct linux_recv_args { 123456fba8e6SDmitry Chagin register_t s; 123556fba8e6SDmitry Chagin register_t msg; 123656fba8e6SDmitry Chagin register_t len; 123756fba8e6SDmitry Chagin register_t flags; 1238c21dee17SSøren Schmidt }; 1239c21dee17SSøren Schmidt 124001e0ffbaSAlexander Leidinger static int 1241b40ce416SJulian Elischer linux_recv(struct thread *td, struct linux_recv_args *args) 1242c21dee17SSøren Schmidt { 124387d72a8fSPoul-Henning Kamp struct recvfrom_args /* { 1244c21dee17SSøren Schmidt int s; 1245c21dee17SSøren Schmidt caddr_t buf; 1246c21dee17SSøren Schmidt int len; 1247c21dee17SSøren Schmidt int flags; 124887d72a8fSPoul-Henning Kamp struct sockaddr *from; 124987d72a8fSPoul-Henning Kamp socklen_t fromlenaddr; 1250ef04503dSPeter Wemm } */ bsd_args; 1251c21dee17SSøren Schmidt 1252745aaef5SKonstantin Belousov bsd_args.s = args->s; 1253745aaef5SKonstantin Belousov bsd_args.buf = (caddr_t)PTRIN(args->msg); 1254745aaef5SKonstantin Belousov bsd_args.len = args->len; 12553980a435SDmitry Chagin bsd_args.flags = linux_to_bsd_msg_flags(args->flags); 125687d72a8fSPoul-Henning Kamp bsd_args.from = NULL; 125787d72a8fSPoul-Henning Kamp bsd_args.fromlenaddr = 0; 12588451d0ddSKip Macy return (sys_recvfrom(td, &bsd_args)); 1259c21dee17SSøren Schmidt } 1260a12b9b3dSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1261c21dee17SSøren Schmidt 1262a12b9b3dSDmitry Chagin int 1263b40ce416SJulian Elischer linux_sendto(struct thread *td, struct linux_sendto_args *args) 1264c21dee17SSøren Schmidt { 12655a8a13e0SDavid Malone struct msghdr msg; 12665a8a13e0SDavid Malone struct iovec aiov; 1267db48fa83SDmitry Chagin struct socket *so; 1268db48fa83SDmitry Chagin struct file *fp; 1269db48fa83SDmitry Chagin int error; 1270c21dee17SSøren Schmidt 1271745aaef5SKonstantin Belousov if (linux_check_hdrincl(td, args->s) == 0) 1272f2477ae1SMike Smith /* IP_HDRINCL set, tweak the packet before sending */ 1273745aaef5SKonstantin Belousov return (linux_sendto_hdrincl(td, args)); 1274f2477ae1SMike Smith 1275db48fa83SDmitry Chagin bzero(&msg, sizeof(msg)); 12763212ad15SMateusz Guzik error = getsock(td, args->s, &cap_send_connect_rights, &fp); 1277db48fa83SDmitry Chagin if (error != 0) 1278db48fa83SDmitry Chagin return (error); 1279db48fa83SDmitry Chagin so = fp->f_data; 1280db48fa83SDmitry Chagin if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) { 1281745aaef5SKonstantin Belousov msg.msg_name = PTRIN(args->to); 1282745aaef5SKonstantin Belousov msg.msg_namelen = args->tolen; 1283db48fa83SDmitry Chagin } 12845a8a13e0SDavid Malone msg.msg_iov = &aiov; 12855a8a13e0SDavid Malone msg.msg_iovlen = 1; 1286745aaef5SKonstantin Belousov aiov.iov_base = PTRIN(args->msg); 1287745aaef5SKonstantin Belousov aiov.iov_len = args->len; 1288db48fa83SDmitry Chagin fdrop(fp, td); 1289e667ee63SDmitry Chagin return (linux_sendit(td, args->s, &msg, args->flags, NULL, 1290e667ee63SDmitry Chagin UIO_USERSPACE)); 1291c21dee17SSøren Schmidt } 1292c21dee17SSøren Schmidt 1293a12b9b3dSDmitry Chagin int 1294b40ce416SJulian Elischer linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args) 1295c21dee17SSøren Schmidt { 1296d5368bf3SDmitry Chagin struct sockaddr *sa; 12979599b0ecSDmitry Chagin struct msghdr msg; 12989599b0ecSDmitry Chagin struct iovec aiov; 12993a49978fSDmitry Chagin int error, fromlen; 1300c21dee17SSøren Schmidt 13019599b0ecSDmitry Chagin if (PTRIN(args->fromlen) != NULL) { 13023a49978fSDmitry Chagin error = copyin(PTRIN(args->fromlen), &fromlen, 13033a49978fSDmitry Chagin sizeof(fromlen)); 13049599b0ecSDmitry Chagin if (error != 0) 13053f3a4815SMarcel Moolenaar return (error); 13063a49978fSDmitry Chagin if (fromlen < 0) 13073a49978fSDmitry Chagin return (EINVAL); 1308bb0f644cSDmitry Chagin fromlen = min(fromlen, SOCK_MAXADDRLEN); 1309d5368bf3SDmitry Chagin sa = malloc(fromlen, M_SONAME, M_WAITOK); 1310d5368bf3SDmitry Chagin } else { 1311d5368bf3SDmitry Chagin fromlen = 0; 1312d5368bf3SDmitry Chagin sa = NULL; 1313d5368bf3SDmitry Chagin } 13149599b0ecSDmitry Chagin 1315d5368bf3SDmitry Chagin msg.msg_name = sa; 1316d5368bf3SDmitry Chagin msg.msg_namelen = fromlen; 13179599b0ecSDmitry Chagin msg.msg_iov = &aiov; 13189599b0ecSDmitry Chagin msg.msg_iovlen = 1; 13199599b0ecSDmitry Chagin aiov.iov_base = PTRIN(args->buf); 13209599b0ecSDmitry Chagin aiov.iov_len = args->len; 13219599b0ecSDmitry Chagin msg.msg_control = 0; 13229599b0ecSDmitry Chagin msg.msg_flags = linux_to_bsd_msg_flags(args->flags); 13239599b0ecSDmitry Chagin 1324d5368bf3SDmitry Chagin error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL); 13259599b0ecSDmitry Chagin if (error != 0) 1326dcd62418SDmitry Chagin goto out; 13279599b0ecSDmitry Chagin 1328bb46e9b5SDmitry Chagin /* 1329bb46e9b5SDmitry Chagin * XXX. Seems that FreeBSD is different from Linux here. Linux 1330bb46e9b5SDmitry Chagin * fill source address if underlying protocol provides it, while 1331bb46e9b5SDmitry Chagin * FreeBSD fill it if underlying protocol is not connection-oriented. 1332bb46e9b5SDmitry Chagin * So, kern_recvit() set msg.msg_namelen to 0 if protocol pr_flags 1333bb46e9b5SDmitry Chagin * does not contains PR_ADDR flag. 1334bb46e9b5SDmitry Chagin */ 1335bb46e9b5SDmitry Chagin if (PTRIN(args->from) != NULL && msg.msg_namelen != 0) 1336bb46e9b5SDmitry Chagin error = linux_copyout_sockaddr(sa, PTRIN(args->from), 1337bb46e9b5SDmitry Chagin msg.msg_namelen); 13389599b0ecSDmitry Chagin 1339d5368bf3SDmitry Chagin if (error == 0 && PTRIN(args->fromlen) != NULL) 13409599b0ecSDmitry Chagin error = copyout(&msg.msg_namelen, PTRIN(args->fromlen), 13419599b0ecSDmitry Chagin sizeof(msg.msg_namelen)); 1342dcd62418SDmitry Chagin out: 1343d5368bf3SDmitry Chagin free(sa, M_SONAME); 13449599b0ecSDmitry Chagin return (error); 1345ca26842eSHajimu UMEMOTO } 1346ca26842eSHajimu UMEMOTO 1347e1ff74c0SDmitry Chagin static int 1348e1ff74c0SDmitry Chagin linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr, 1349e1ff74c0SDmitry Chagin l_uint flags) 1350ca26842eSHajimu UMEMOTO { 135174f5d680SKonstantin Belousov struct cmsghdr *cmsg; 135274f5d680SKonstantin Belousov struct mbuf *control; 1353ca26842eSHajimu UMEMOTO struct msghdr msg; 135474f5d680SKonstantin Belousov struct l_cmsghdr linux_cmsg; 135574f5d680SKonstantin Belousov struct l_cmsghdr *ptr_cmsg; 1356599dadcaSEdward Tomasz Napierala struct l_msghdr linux_msghdr; 1357552afd9cSPoul-Henning Kamp struct iovec *iov; 135874f5d680SKonstantin Belousov socklen_t datalen; 1359605da56bSAndriy Gapon struct sockaddr *sa; 13601410bfe1SDmitry Chagin struct socket *so; 1361605da56bSAndriy Gapon sa_family_t sa_family; 13621410bfe1SDmitry Chagin struct file *fp; 136374f5d680SKonstantin Belousov void *data; 1364e3b385fcSTijl Coosemans l_size_t len; 13657df0e7beSTijl Coosemans l_size_t clen; 13663212ad15SMateusz Guzik int error; 1367ca26842eSHajimu UMEMOTO 1368599dadcaSEdward Tomasz Napierala error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr)); 1369e1ff74c0SDmitry Chagin if (error != 0) 137074f5d680SKonstantin Belousov return (error); 1371d72a6158SRobert Watson 1372d72a6158SRobert Watson /* 1373d72a6158SRobert Watson * Some Linux applications (ping) define a non-NULL control data 1374d72a6158SRobert Watson * pointer, but a msg_controllen of 0, which is not allowed in the 1375d72a6158SRobert Watson * FreeBSD system call interface. NULL the msg_control pointer in 1376d72a6158SRobert Watson * order to handle this case. This should be checked, but allows the 1377d72a6158SRobert Watson * Linux ping to work. 1378d72a6158SRobert Watson */ 1379599dadcaSEdward Tomasz Napierala if (PTRIN(linux_msghdr.msg_control) != NULL && 1380599dadcaSEdward Tomasz Napierala linux_msghdr.msg_controllen == 0) 1381599dadcaSEdward Tomasz Napierala linux_msghdr.msg_control = PTROUT(NULL); 1382605da56bSAndriy Gapon 1383599dadcaSEdward Tomasz Napierala error = linux_to_bsd_msghdr(&msg, &linux_msghdr); 1384e1ff74c0SDmitry Chagin if (error != 0) 1385605da56bSAndriy Gapon return (error); 138674f5d680SKonstantin Belousov 138774f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32 1388524c9accSDmitry Chagin error = freebsd32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen, 138974f5d680SKonstantin Belousov &iov, EMSGSIZE); 139074f5d680SKonstantin Belousov #else 1391552afd9cSPoul-Henning Kamp error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE); 139274f5d680SKonstantin Belousov #endif 1393e1ff74c0SDmitry Chagin if (error != 0) 1394552afd9cSPoul-Henning Kamp return (error); 139574f5d680SKonstantin Belousov 1396605da56bSAndriy Gapon control = NULL; 1397605da56bSAndriy Gapon 1398e1ff74c0SDmitry Chagin error = kern_getsockname(td, s, &sa, &datalen); 1399e1ff74c0SDmitry Chagin if (error != 0) 1400605da56bSAndriy Gapon goto bad; 1401605da56bSAndriy Gapon sa_family = sa->sa_family; 1402605da56bSAndriy Gapon free(sa, M_SONAME); 1403605da56bSAndriy Gapon 14041410bfe1SDmitry Chagin if (flags & LINUX_MSG_OOB) { 14051410bfe1SDmitry Chagin error = EOPNOTSUPP; 14061410bfe1SDmitry Chagin if (sa_family == AF_UNIX) 14071410bfe1SDmitry Chagin goto bad; 14081410bfe1SDmitry Chagin 14093212ad15SMateusz Guzik error = getsock(td, s, &cap_send_rights, &fp); 14101410bfe1SDmitry Chagin if (error != 0) 14111410bfe1SDmitry Chagin goto bad; 14121410bfe1SDmitry Chagin so = fp->f_data; 14131410bfe1SDmitry Chagin if (so->so_type != SOCK_STREAM) 14141410bfe1SDmitry Chagin error = EOPNOTSUPP; 14151410bfe1SDmitry Chagin fdrop(fp, td); 14161410bfe1SDmitry Chagin if (error != 0) 14171410bfe1SDmitry Chagin goto bad; 14181410bfe1SDmitry Chagin } 14191410bfe1SDmitry Chagin 1420599dadcaSEdward Tomasz Napierala if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) { 142174f5d680SKonstantin Belousov error = ENOBUFS; 1422eb1b1807SGleb Smirnoff control = m_get(M_WAITOK, MT_CONTROL); 1423e3b385fcSTijl Coosemans MCLGET(control, M_WAITOK); 1424e3b385fcSTijl Coosemans data = mtod(control, void *); 1425e3b385fcSTijl Coosemans datalen = 0; 142674f5d680SKonstantin Belousov 1427599dadcaSEdward Tomasz Napierala ptr_cmsg = PTRIN(linux_msghdr.msg_control); 1428599dadcaSEdward Tomasz Napierala clen = linux_msghdr.msg_controllen; 142974f5d680SKonstantin Belousov do { 143074f5d680SKonstantin Belousov error = copyin(ptr_cmsg, &linux_cmsg, 143174f5d680SKonstantin Belousov sizeof(struct l_cmsghdr)); 1432e1ff74c0SDmitry Chagin if (error != 0) 143374f5d680SKonstantin Belousov goto bad; 143474f5d680SKonstantin Belousov 143574f5d680SKonstantin Belousov error = EINVAL; 14367df0e7beSTijl Coosemans if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) || 14377df0e7beSTijl Coosemans linux_cmsg.cmsg_len > clen) 143874f5d680SKonstantin Belousov goto bad; 143974f5d680SKonstantin Belousov 1440e3b385fcSTijl Coosemans if (datalen + CMSG_HDRSZ > MCLBYTES) 1441e3b385fcSTijl Coosemans goto bad; 1442e3b385fcSTijl Coosemans 144374f5d680SKonstantin Belousov /* 1444605da56bSAndriy Gapon * Now we support only SCM_RIGHTS and SCM_CRED, 1445605da56bSAndriy Gapon * so return EINVAL in any other cmsg_type 144674f5d680SKonstantin Belousov */ 1447e3b385fcSTijl Coosemans cmsg = data; 1448605da56bSAndriy Gapon cmsg->cmsg_type = 1449605da56bSAndriy Gapon linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type); 145074f5d680SKonstantin Belousov cmsg->cmsg_level = 145174f5d680SKonstantin Belousov linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level); 1452605da56bSAndriy Gapon if (cmsg->cmsg_type == -1 1453889cd285SEdward Tomasz Napierala || cmsg->cmsg_level != SOL_SOCKET) { 1454889cd285SEdward Tomasz Napierala linux_msg(curthread, 1455889cd285SEdward Tomasz Napierala "unsupported sendmsg cmsg level %d type %d", 1456889cd285SEdward Tomasz Napierala linux_cmsg.cmsg_level, linux_cmsg.cmsg_type); 1457605da56bSAndriy Gapon goto bad; 1458889cd285SEdward Tomasz Napierala } 145974f5d680SKonstantin Belousov 1460605da56bSAndriy Gapon /* 1461605da56bSAndriy Gapon * Some applications (e.g. pulseaudio) attempt to 1462605da56bSAndriy Gapon * send ancillary data even if the underlying protocol 1463605da56bSAndriy Gapon * doesn't support it which is not allowed in the 1464605da56bSAndriy Gapon * FreeBSD system call interface. 1465605da56bSAndriy Gapon */ 1466605da56bSAndriy Gapon if (sa_family != AF_UNIX) 1467a7044c60SMark Johnston goto next; 1468605da56bSAndriy Gapon 1469e3b385fcSTijl Coosemans if (cmsg->cmsg_type == SCM_CREDS) { 1470e3b385fcSTijl Coosemans len = sizeof(struct cmsgcred); 1471e3b385fcSTijl Coosemans if (datalen + CMSG_SPACE(len) > MCLBYTES) 1472e3b385fcSTijl Coosemans goto bad; 1473605da56bSAndriy Gapon 1474605da56bSAndriy Gapon /* 1475605da56bSAndriy Gapon * The lower levels will fill in the structure 1476605da56bSAndriy Gapon */ 1477e3b385fcSTijl Coosemans memset(CMSG_DATA(data), 0, len); 1478e3b385fcSTijl Coosemans } else { 1479e3b385fcSTijl Coosemans len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ; 1480e3b385fcSTijl Coosemans if (datalen + CMSG_SPACE(len) < datalen || 1481e3b385fcSTijl Coosemans datalen + CMSG_SPACE(len) > MCLBYTES) 1482e3b385fcSTijl Coosemans goto bad; 1483e3b385fcSTijl Coosemans 1484e3b385fcSTijl Coosemans error = copyin(LINUX_CMSG_DATA(ptr_cmsg), 1485e3b385fcSTijl Coosemans CMSG_DATA(data), len); 1486e3b385fcSTijl Coosemans if (error != 0) 1487e3b385fcSTijl Coosemans goto bad; 1488605da56bSAndriy Gapon } 1489605da56bSAndriy Gapon 1490e3b385fcSTijl Coosemans cmsg->cmsg_len = CMSG_LEN(len); 1491e3b385fcSTijl Coosemans data = (char *)data + CMSG_SPACE(len); 1492e3b385fcSTijl Coosemans datalen += CMSG_SPACE(len); 14937df0e7beSTijl Coosemans 1494a7044c60SMark Johnston next: 14957df0e7beSTijl Coosemans if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len)) 14967df0e7beSTijl Coosemans break; 14977df0e7beSTijl Coosemans 14987df0e7beSTijl Coosemans clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len); 14997df0e7beSTijl Coosemans ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg + 15007df0e7beSTijl Coosemans LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len)); 15017df0e7beSTijl Coosemans } while(clen >= sizeof(struct l_cmsghdr)); 1502605da56bSAndriy Gapon 1503e3b385fcSTijl Coosemans control->m_len = datalen; 1504e3b385fcSTijl Coosemans if (datalen == 0) { 1505605da56bSAndriy Gapon m_freem(control); 150674f5d680SKonstantin Belousov control = NULL; 1507605da56bSAndriy Gapon } 150874f5d680SKonstantin Belousov } 150974f5d680SKonstantin Belousov 15105a8a13e0SDavid Malone msg.msg_iov = iov; 15115a8a13e0SDavid Malone msg.msg_flags = 0; 1512e1ff74c0SDmitry Chagin error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE); 151367968b35SDmitry Chagin control = NULL; 151474f5d680SKonstantin Belousov 151574f5d680SKonstantin Belousov bad: 15164f65e9cfSDmitry Chagin m_freem(control); 1517552afd9cSPoul-Henning Kamp free(iov, M_IOV); 1518ca26842eSHajimu UMEMOTO return (error); 1519c21dee17SSøren Schmidt } 1520c21dee17SSøren Schmidt 1521a12b9b3dSDmitry Chagin int 1522e1ff74c0SDmitry Chagin linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args) 1523e1ff74c0SDmitry Chagin { 1524e1ff74c0SDmitry Chagin 1525e1ff74c0SDmitry Chagin return (linux_sendmsg_common(td, args->s, PTRIN(args->msg), 1526e1ff74c0SDmitry Chagin args->flags)); 1527e1ff74c0SDmitry Chagin } 1528e1ff74c0SDmitry Chagin 1529e1ff74c0SDmitry Chagin int 1530e1ff74c0SDmitry Chagin linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args) 1531e1ff74c0SDmitry Chagin { 1532e1ff74c0SDmitry Chagin struct l_mmsghdr *msg; 1533e1ff74c0SDmitry Chagin l_uint retval; 1534e1ff74c0SDmitry Chagin int error, datagrams; 1535e1ff74c0SDmitry Chagin 1536e1ff74c0SDmitry Chagin if (args->vlen > UIO_MAXIOV) 1537e1ff74c0SDmitry Chagin args->vlen = UIO_MAXIOV; 1538e1ff74c0SDmitry Chagin 1539e1ff74c0SDmitry Chagin msg = PTRIN(args->msg); 1540e1ff74c0SDmitry Chagin datagrams = 0; 1541e1ff74c0SDmitry Chagin while (datagrams < args->vlen) { 1542e1ff74c0SDmitry Chagin error = linux_sendmsg_common(td, args->s, &msg->msg_hdr, 1543e1ff74c0SDmitry Chagin args->flags); 1544e1ff74c0SDmitry Chagin if (error != 0) 1545e1ff74c0SDmitry Chagin break; 1546e1ff74c0SDmitry Chagin 1547e1ff74c0SDmitry Chagin retval = td->td_retval[0]; 1548e1ff74c0SDmitry Chagin error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len)); 1549e1ff74c0SDmitry Chagin if (error != 0) 1550e1ff74c0SDmitry Chagin break; 1551e1ff74c0SDmitry Chagin ++msg; 1552e1ff74c0SDmitry Chagin ++datagrams; 1553e1ff74c0SDmitry Chagin } 1554e1ff74c0SDmitry Chagin if (error == 0) 1555e1ff74c0SDmitry Chagin td->td_retval[0] = datagrams; 1556e1ff74c0SDmitry Chagin return (error); 1557e1ff74c0SDmitry Chagin } 1558e1ff74c0SDmitry Chagin 1559e1ff74c0SDmitry Chagin static int 1560b408788dSDmitry Chagin recvmsg_scm_rights(struct thread *td, l_uint flags, socklen_t *datalen, 1561b408788dSDmitry Chagin void **data, void **udata) 1562b408788dSDmitry Chagin { 1563b408788dSDmitry Chagin int i, fd, fds, *fdp; 1564b408788dSDmitry Chagin 1565b408788dSDmitry Chagin if (flags & LINUX_MSG_CMSG_CLOEXEC) { 1566b408788dSDmitry Chagin fds = *datalen / sizeof(int); 1567b408788dSDmitry Chagin fdp = *data; 1568b408788dSDmitry Chagin for (i = 0; i < fds; i++) { 1569b408788dSDmitry Chagin fd = *fdp++; 1570b408788dSDmitry Chagin (void)kern_fcntl(td, fd, F_SETFD, FD_CLOEXEC); 1571b408788dSDmitry Chagin } 1572b408788dSDmitry Chagin } 1573b408788dSDmitry Chagin return (0); 1574b408788dSDmitry Chagin } 1575b408788dSDmitry Chagin 157671bc8bcfSDmitry Chagin 1577b408788dSDmitry Chagin static int 1578b408788dSDmitry Chagin recvmsg_scm_creds(socklen_t *datalen, void **data, void **udata) 1579b408788dSDmitry Chagin { 1580b408788dSDmitry Chagin struct cmsgcred *cmcred; 1581b408788dSDmitry Chagin struct l_ucred lu; 1582b408788dSDmitry Chagin 1583b408788dSDmitry Chagin cmcred = *data; 1584b408788dSDmitry Chagin lu.pid = cmcred->cmcred_pid; 1585b408788dSDmitry Chagin lu.uid = cmcred->cmcred_uid; 1586b408788dSDmitry Chagin lu.gid = cmcred->cmcred_gid; 1587b408788dSDmitry Chagin memmove(*data, &lu, sizeof(lu)); 1588b408788dSDmitry Chagin *datalen = sizeof(lu); 1589b408788dSDmitry Chagin return (0); 1590b408788dSDmitry Chagin } 1591b408788dSDmitry Chagin _Static_assert(sizeof(struct cmsgcred) >= sizeof(struct l_ucred), 1592b408788dSDmitry Chagin "scm_creds sizeof l_ucred"); 1593b408788dSDmitry Chagin 1594b408788dSDmitry Chagin static int 1595b408788dSDmitry Chagin recvmsg_scm_creds2(socklen_t *datalen, void **data, void **udata) 1596b408788dSDmitry Chagin { 1597b408788dSDmitry Chagin struct sockcred2 *scred; 1598b408788dSDmitry Chagin struct l_ucred lu; 1599b408788dSDmitry Chagin 1600b408788dSDmitry Chagin scred = *data; 1601b408788dSDmitry Chagin lu.pid = scred->sc_pid; 1602b408788dSDmitry Chagin lu.uid = scred->sc_uid; 1603b408788dSDmitry Chagin lu.gid = scred->sc_gid; 1604b408788dSDmitry Chagin memmove(*data, &lu, sizeof(lu)); 1605b408788dSDmitry Chagin *datalen = sizeof(lu); 1606b408788dSDmitry Chagin return (0); 1607b408788dSDmitry Chagin } 1608b408788dSDmitry Chagin _Static_assert(sizeof(struct sockcred2) >= sizeof(struct l_ucred), 1609b408788dSDmitry Chagin "scm_creds2 sizeof l_ucred"); 1610b408788dSDmitry Chagin 16110e26e54bSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1612b408788dSDmitry Chagin static int 16130e26e54bSDmitry Chagin recvmsg_scm_timestamp(l_int msg_type, socklen_t *datalen, void **data, 16140e26e54bSDmitry Chagin void **udata) 1615b408788dSDmitry Chagin { 16160e26e54bSDmitry Chagin l_sock_timeval ltv64; 16170e26e54bSDmitry Chagin l_timeval ltv; 16180e26e54bSDmitry Chagin struct timeval *tv; 16190e26e54bSDmitry Chagin socklen_t len; 16200e26e54bSDmitry Chagin void *buf; 1621b408788dSDmitry Chagin 1622b408788dSDmitry Chagin if (*datalen != sizeof(struct timeval)) 1623b408788dSDmitry Chagin return (EMSGSIZE); 1624b408788dSDmitry Chagin 16250e26e54bSDmitry Chagin tv = *data; 16260e26e54bSDmitry Chagin #if defined(COMPAT_LINUX32) 16270e26e54bSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPO && 16280e26e54bSDmitry Chagin (tv->tv_sec > INT_MAX || tv->tv_sec < INT_MIN)) 16290e26e54bSDmitry Chagin return (EOVERFLOW); 16300e26e54bSDmitry Chagin #endif 16310e26e54bSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPN) 16320e26e54bSDmitry Chagin len = sizeof(ltv64); 16330e26e54bSDmitry Chagin else 16340e26e54bSDmitry Chagin len = sizeof(ltv); 16350e26e54bSDmitry Chagin 16360e26e54bSDmitry Chagin buf = malloc(len, M_LINUX, M_WAITOK); 16370e26e54bSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPN) { 16380e26e54bSDmitry Chagin ltv64.tv_sec = tv->tv_sec; 16390e26e54bSDmitry Chagin ltv64.tv_usec = tv->tv_usec; 16400e26e54bSDmitry Chagin memmove(buf, <v64, len); 16410e26e54bSDmitry Chagin } else { 16420e26e54bSDmitry Chagin ltv.tv_sec = tv->tv_sec; 16430e26e54bSDmitry Chagin ltv.tv_usec = tv->tv_usec; 16440e26e54bSDmitry Chagin memmove(buf, <v, len); 16450e26e54bSDmitry Chagin } 16460e26e54bSDmitry Chagin *data = *udata = buf; 16470e26e54bSDmitry Chagin *datalen = len; 1648b408788dSDmitry Chagin return (0); 1649b408788dSDmitry Chagin } 16500e26e54bSDmitry Chagin #else 16510e26e54bSDmitry Chagin _Static_assert(sizeof(struct timeval) == sizeof(l_timeval), 16520e26e54bSDmitry Chagin "scm_timestamp sizeof l_timeval"); 16530e26e54bSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 1654b408788dSDmitry Chagin 165571bc8bcfSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 165671bc8bcfSDmitry Chagin static int 165771bc8bcfSDmitry Chagin recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data, 165871bc8bcfSDmitry Chagin void **udata) 165971bc8bcfSDmitry Chagin { 166071bc8bcfSDmitry Chagin struct l_timespec64 ts64; 166171bc8bcfSDmitry Chagin struct l_timespec ts32; 166271bc8bcfSDmitry Chagin struct timespec ts; 166371bc8bcfSDmitry Chagin socklen_t len; 166471bc8bcfSDmitry Chagin void *buf; 166571bc8bcfSDmitry Chagin 166671bc8bcfSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPNSO) 166771bc8bcfSDmitry Chagin len = sizeof(ts32); 166871bc8bcfSDmitry Chagin else 166971bc8bcfSDmitry Chagin len = sizeof(ts64); 167071bc8bcfSDmitry Chagin 167171bc8bcfSDmitry Chagin buf = malloc(len, M_LINUX, M_WAITOK); 167271bc8bcfSDmitry Chagin bintime2timespec(*data, &ts); 167371bc8bcfSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPNSO) { 167471bc8bcfSDmitry Chagin ts32.tv_sec = ts.tv_sec; 167571bc8bcfSDmitry Chagin ts32.tv_nsec = ts.tv_nsec; 167671bc8bcfSDmitry Chagin memmove(buf, &ts32, len); 167771bc8bcfSDmitry Chagin } else { 167871bc8bcfSDmitry Chagin ts64.tv_sec = ts.tv_sec; 167971bc8bcfSDmitry Chagin ts64.tv_nsec = ts.tv_nsec; 168071bc8bcfSDmitry Chagin memmove(buf, &ts64, len); 168171bc8bcfSDmitry Chagin } 168271bc8bcfSDmitry Chagin *data = *udata = buf; 168371bc8bcfSDmitry Chagin *datalen = len; 168471bc8bcfSDmitry Chagin return (0); 168571bc8bcfSDmitry Chagin } 168671bc8bcfSDmitry Chagin #else 168771bc8bcfSDmitry Chagin static int 168871bc8bcfSDmitry Chagin recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data, 168971bc8bcfSDmitry Chagin void **udata) 169071bc8bcfSDmitry Chagin { 169171bc8bcfSDmitry Chagin struct timespec ts; 169271bc8bcfSDmitry Chagin 169371bc8bcfSDmitry Chagin bintime2timespec(*data, &ts); 169471bc8bcfSDmitry Chagin memmove(*data, &ts, sizeof(struct timespec)); 169571bc8bcfSDmitry Chagin *datalen = sizeof(struct timespec); 169671bc8bcfSDmitry Chagin return (0); 169771bc8bcfSDmitry Chagin } 169871bc8bcfSDmitry Chagin _Static_assert(sizeof(struct bintime) >= sizeof(struct timespec), 169971bc8bcfSDmitry Chagin "scm_timestampns sizeof timespec"); 170071bc8bcfSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 170171bc8bcfSDmitry Chagin 1702b408788dSDmitry Chagin static int 1703de20eb26SDmitry Chagin recvmsg_scm_sol_socket(struct thread *td, l_int msg_type, l_int lmsg_type, 1704de20eb26SDmitry Chagin l_uint flags, socklen_t *datalen, void **data, void **udata) 1705de20eb26SDmitry Chagin { 1706de20eb26SDmitry Chagin int error; 1707de20eb26SDmitry Chagin 1708de20eb26SDmitry Chagin error = 0; 1709de20eb26SDmitry Chagin switch (msg_type) { 1710de20eb26SDmitry Chagin case SCM_RIGHTS: 1711de20eb26SDmitry Chagin error = recvmsg_scm_rights(td, flags, datalen, 1712de20eb26SDmitry Chagin data, udata); 1713de20eb26SDmitry Chagin break; 1714de20eb26SDmitry Chagin case SCM_CREDS: 1715de20eb26SDmitry Chagin error = recvmsg_scm_creds(datalen, data, udata); 1716de20eb26SDmitry Chagin break; 1717de20eb26SDmitry Chagin case SCM_CREDS2: 1718de20eb26SDmitry Chagin error = recvmsg_scm_creds2(datalen, data, udata); 1719de20eb26SDmitry Chagin break; 1720de20eb26SDmitry Chagin case SCM_TIMESTAMP: 1721de20eb26SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 1722de20eb26SDmitry Chagin error = recvmsg_scm_timestamp(lmsg_type, datalen, 1723de20eb26SDmitry Chagin data, udata); 1724de20eb26SDmitry Chagin #endif 1725de20eb26SDmitry Chagin break; 1726de20eb26SDmitry Chagin case SCM_BINTIME: 1727de20eb26SDmitry Chagin error = recvmsg_scm_timestampns(lmsg_type, datalen, 1728de20eb26SDmitry Chagin data, udata); 1729de20eb26SDmitry Chagin break; 1730de20eb26SDmitry Chagin } 1731de20eb26SDmitry Chagin 1732de20eb26SDmitry Chagin return (error); 1733de20eb26SDmitry Chagin } 1734de20eb26SDmitry Chagin 1735de20eb26SDmitry Chagin static int 1736f8a66150SDmitry Chagin recvmsg_scm_ip_origdstaddr(socklen_t *datalen, void **data, void **udata) 1737f8a66150SDmitry Chagin { 1738f8a66150SDmitry Chagin struct l_sockaddr *lsa; 1739f8a66150SDmitry Chagin int error; 1740f8a66150SDmitry Chagin 1741f8a66150SDmitry Chagin error = bsd_to_linux_sockaddr(*data, &lsa, *datalen); 1742f8a66150SDmitry Chagin if (error == 0) { 1743f8a66150SDmitry Chagin *data = *udata = lsa; 1744f8a66150SDmitry Chagin *datalen = sizeof(*lsa); 1745f8a66150SDmitry Chagin } 1746f8a66150SDmitry Chagin return (error); 1747f8a66150SDmitry Chagin } 1748f8a66150SDmitry Chagin 1749f8a66150SDmitry Chagin static int 1750de20eb26SDmitry Chagin recvmsg_scm_ipproto_ip(l_int msg_type, l_int lmsg_type, socklen_t *datalen, 1751de20eb26SDmitry Chagin void **data, void **udata) 1752de20eb26SDmitry Chagin { 1753de20eb26SDmitry Chagin int error; 1754de20eb26SDmitry Chagin 1755de20eb26SDmitry Chagin error = 0; 1756de20eb26SDmitry Chagin switch (msg_type) { 1757de20eb26SDmitry Chagin case IP_ORIGDSTADDR: 1758de20eb26SDmitry Chagin error = recvmsg_scm_ip_origdstaddr(datalen, data, 1759de20eb26SDmitry Chagin udata); 1760de20eb26SDmitry Chagin break; 1761de20eb26SDmitry Chagin } 1762de20eb26SDmitry Chagin 1763de20eb26SDmitry Chagin return (error); 1764de20eb26SDmitry Chagin } 1765de20eb26SDmitry Chagin 1766de20eb26SDmitry Chagin static int 1767e1ff74c0SDmitry Chagin linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr, 1768e1ff74c0SDmitry Chagin l_uint flags, struct msghdr *msg) 176940dbba57SAssar Westerlund { 17700e26e54bSDmitry Chagin struct proc *p = td->td_proc; 177174f5d680SKonstantin Belousov struct cmsghdr *cm; 17720eda2ceaSDmitry Chagin struct l_cmsghdr *lcm = NULL; 1773c7902fbeSMark Johnston socklen_t datalen, maxlen, outlen; 17740eda2ceaSDmitry Chagin struct l_msghdr l_msghdr; 177574f5d680SKonstantin Belousov struct iovec *iov, *uiov; 17763735f9cfSDmitry Chagin struct mbuf *m, *control = NULL; 177774f5d680SKonstantin Belousov struct mbuf **controlp; 1778d5368bf3SDmitry Chagin struct sockaddr *sa; 177974f5d680SKonstantin Belousov caddr_t outbuf; 1780b408788dSDmitry Chagin void *data, *udata; 1781bbaa5523SDmitry Chagin int error, skiped; 178240dbba57SAssar Westerlund 17830eda2ceaSDmitry Chagin error = copyin(msghdr, &l_msghdr, sizeof(l_msghdr)); 1784e1ff74c0SDmitry Chagin if (error != 0) 1785ca26842eSHajimu UMEMOTO return (error); 1786ca26842eSHajimu UMEMOTO 178796c524d8SAlexander V. Chernikov /* 178896c524d8SAlexander V. Chernikov * Pass user-supplied recvmsg() flags in msg_flags field, 178996c524d8SAlexander V. Chernikov * following sys_recvmsg() convention. 179096c524d8SAlexander V. Chernikov */ 17910eda2ceaSDmitry Chagin l_msghdr.msg_flags = flags; 179296c524d8SAlexander V. Chernikov 17930eda2ceaSDmitry Chagin error = linux_to_bsd_msghdr(msg, &l_msghdr); 1794e1ff74c0SDmitry Chagin if (error != 0) 179574f5d680SKonstantin Belousov return (error); 179674f5d680SKonstantin Belousov 179774f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32 1798524c9accSDmitry Chagin error = freebsd32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen, 179974f5d680SKonstantin Belousov &iov, EMSGSIZE); 180074f5d680SKonstantin Belousov #else 1801e1ff74c0SDmitry Chagin error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE); 180274f5d680SKonstantin Belousov #endif 1803e1ff74c0SDmitry Chagin if (error != 0) 180474f5d680SKonstantin Belousov return (error); 180574f5d680SKonstantin Belousov 18064d2b7be5SEdward Tomasz Napierala if (msg->msg_name != NULL && msg->msg_namelen > 0) { 18074d2b7be5SEdward Tomasz Napierala msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN); 1808d5368bf3SDmitry Chagin sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK); 1809d5368bf3SDmitry Chagin msg->msg_name = sa; 18104d2b7be5SEdward Tomasz Napierala } else { 18118128cfc5SDmitry Chagin sa = NULL; 18124d2b7be5SEdward Tomasz Napierala msg->msg_name = NULL; 18134d2b7be5SEdward Tomasz Napierala } 181484b11cd8SMitsuru IWASAKI 1815e1ff74c0SDmitry Chagin uiov = msg->msg_iov; 1816e1ff74c0SDmitry Chagin msg->msg_iov = iov; 1817e1ff74c0SDmitry Chagin controlp = (msg->msg_control != NULL) ? &control : NULL; 1818d5368bf3SDmitry Chagin error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp); 1819e1ff74c0SDmitry Chagin msg->msg_iov = uiov; 1820e1ff74c0SDmitry Chagin if (error != 0) 182174f5d680SKonstantin Belousov goto bad; 182274f5d680SKonstantin Belousov 18234d2b7be5SEdward Tomasz Napierala /* 18244d2b7be5SEdward Tomasz Napierala * Note that kern_recvit() updates msg->msg_namelen. 18254d2b7be5SEdward Tomasz Napierala */ 18264d2b7be5SEdward Tomasz Napierala if (msg->msg_name != NULL && msg->msg_namelen > 0) { 18270eda2ceaSDmitry Chagin msg->msg_name = PTRIN(l_msghdr.msg_name); 182868bfaefbSDmitry Chagin error = linux_copyout_sockaddr(sa, msg->msg_name, 182968bfaefbSDmitry Chagin msg->msg_namelen); 1830d5368bf3SDmitry Chagin if (error != 0) 1831d5368bf3SDmitry Chagin goto bad; 1832d5368bf3SDmitry Chagin } 1833d5368bf3SDmitry Chagin 18340eda2ceaSDmitry Chagin error = bsd_to_linux_msghdr(msg, &l_msghdr); 1835e1ff74c0SDmitry Chagin if (error != 0) 183674f5d680SKonstantin Belousov goto bad; 183774f5d680SKonstantin Belousov 18382467ccddSDmitry Chagin skiped = outlen = 0; 18390eda2ceaSDmitry Chagin maxlen = l_msghdr.msg_controllen; 18400eda2ceaSDmitry Chagin if (control == NULL) 18410eda2ceaSDmitry Chagin goto out; 1842605da56bSAndriy Gapon 18430eda2ceaSDmitry Chagin lcm = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO); 1844e1ff74c0SDmitry Chagin msg->msg_control = mtod(control, struct cmsghdr *); 1845e1ff74c0SDmitry Chagin msg->msg_controllen = control->m_len; 18460eda2ceaSDmitry Chagin outbuf = PTRIN(l_msghdr.msg_control); 18473735f9cfSDmitry Chagin for (m = control; m != NULL; m = m->m_next) { 18483735f9cfSDmitry Chagin cm = mtod(m, struct cmsghdr *); 1849f8a66150SDmitry Chagin lcm->cmsg_type = bsd_to_linux_cmsg_type(p, cm->cmsg_type, 1850f8a66150SDmitry Chagin cm->cmsg_level); 18510eda2ceaSDmitry Chagin lcm->cmsg_level = bsd_to_linux_sockopt_level(cm->cmsg_level); 1852f8a66150SDmitry Chagin 18530eda2ceaSDmitry Chagin if (lcm->cmsg_type == -1 || 1854bbaa5523SDmitry Chagin cm->cmsg_level == -1) { 185531d7f3e9SDmitry Chagin LINUX_RATELIMIT_MSG_OPT2( 1856889cd285SEdward Tomasz Napierala "unsupported recvmsg cmsg level %d type %d", 1857889cd285SEdward Tomasz Napierala cm->cmsg_level, cm->cmsg_type); 1858bbaa5523SDmitry Chagin /* Skip unsupported messages */ 1859bbaa5523SDmitry Chagin skiped++; 1860bbaa5523SDmitry Chagin continue; 186174f5d680SKonstantin Belousov } 1862de20eb26SDmitry Chagin data = CMSG_DATA(cm); 1863de20eb26SDmitry Chagin datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data; 1864de20eb26SDmitry Chagin udata = NULL; 1865de20eb26SDmitry Chagin error = 0; 1866605da56bSAndriy Gapon 1867de20eb26SDmitry Chagin switch (cm->cmsg_level) { 1868de20eb26SDmitry Chagin case IPPROTO_IP: 1869de20eb26SDmitry Chagin error = recvmsg_scm_ipproto_ip(cm->cmsg_type, 1870de20eb26SDmitry Chagin lcm->cmsg_type, &datalen, &data, &udata); 1871605da56bSAndriy Gapon break; 1872de20eb26SDmitry Chagin case SOL_SOCKET: 1873de20eb26SDmitry Chagin error = recvmsg_scm_sol_socket(td, cm->cmsg_type, 1874de20eb26SDmitry Chagin lcm->cmsg_type, flags, &datalen, &data, &udata); 187571bc8bcfSDmitry Chagin break; 1876605da56bSAndriy Gapon } 1877f8a66150SDmitry Chagin 18789d0c9b6dSDmitry Chagin /* The recvmsg_scm_ is responsible to free udata on error. */ 1879b408788dSDmitry Chagin if (error != 0) 1880b408788dSDmitry Chagin goto bad; 1881605da56bSAndriy Gapon 18827d561928SDmitry Chagin if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) { 188374f5d680SKonstantin Belousov if (outlen == 0) { 188474f5d680SKonstantin Belousov error = EMSGSIZE; 1885b408788dSDmitry Chagin goto err; 188674f5d680SKonstantin Belousov } else { 18870eda2ceaSDmitry Chagin l_msghdr.msg_flags |= LINUX_MSG_CTRUNC; 1888c7902fbeSMark Johnston m_dispose_extcontrolm(control); 1889b408788dSDmitry Chagin free(udata, M_LINUX); 189074f5d680SKonstantin Belousov goto out; 189174f5d680SKonstantin Belousov } 189274f5d680SKonstantin Belousov } 189374f5d680SKonstantin Belousov 18940eda2ceaSDmitry Chagin lcm->cmsg_len = LINUX_CMSG_LEN(datalen); 18950eda2ceaSDmitry Chagin error = copyout(lcm, outbuf, L_CMSG_HDRSZ); 1896b408788dSDmitry Chagin if (error == 0) { 1897a21238d8SDmitry Chagin error = copyout(data, LINUX_CMSG_DATA(outbuf), datalen); 1898b408788dSDmitry Chagin if (error == 0) { 1899a21238d8SDmitry Chagin outbuf += LINUX_CMSG_SPACE(datalen); 190067116c69SDmitry Chagin outlen += LINUX_CMSG_SPACE(datalen); 190174f5d680SKonstantin Belousov } 1902b408788dSDmitry Chagin } 1903b408788dSDmitry Chagin err: 1904b408788dSDmitry Chagin free(udata, M_LINUX); 1905b408788dSDmitry Chagin if (error != 0) 1906b408788dSDmitry Chagin goto bad; 1907b408788dSDmitry Chagin } 1908bbaa5523SDmitry Chagin if (outlen == 0 && skiped > 0) { 1909bbaa5523SDmitry Chagin error = EINVAL; 1910bbaa5523SDmitry Chagin goto bad; 1911bbaa5523SDmitry Chagin } 191274f5d680SKonstantin Belousov 191374f5d680SKonstantin Belousov out: 19142467ccddSDmitry Chagin l_msghdr.msg_controllen = outlen; 19150eda2ceaSDmitry Chagin error = copyout(&l_msghdr, msghdr, sizeof(l_msghdr)); 191674f5d680SKonstantin Belousov 191774f5d680SKonstantin Belousov bad: 1918c7902fbeSMark Johnston if (control != NULL) { 1919c7902fbeSMark Johnston if (error != 0) 1920c7902fbeSMark Johnston m_dispose_extcontrolm(control); 192174f5d680SKonstantin Belousov m_freem(control); 1922c7902fbeSMark Johnston } 1923c7902fbeSMark Johnston free(iov, M_IOV); 19240eda2ceaSDmitry Chagin free(lcm, M_LINUX); 19258128cfc5SDmitry Chagin free(sa, M_SONAME); 192674f5d680SKonstantin Belousov 1927ca26842eSHajimu UMEMOTO return (error); 192840dbba57SAssar Westerlund } 192940dbba57SAssar Westerlund 1930a12b9b3dSDmitry Chagin int 1931e1ff74c0SDmitry Chagin linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args) 1932e1ff74c0SDmitry Chagin { 1933e1ff74c0SDmitry Chagin struct msghdr bsd_msg; 19343a99aac6SDmitry Chagin struct file *fp; 19353a99aac6SDmitry Chagin int error; 1936e1ff74c0SDmitry Chagin 19373212ad15SMateusz Guzik error = getsock(td, args->s, &cap_recv_rights, &fp); 19383a99aac6SDmitry Chagin if (error != 0) 19393a99aac6SDmitry Chagin return (error); 19403a99aac6SDmitry Chagin fdrop(fp, td); 1941e1ff74c0SDmitry Chagin return (linux_recvmsg_common(td, args->s, PTRIN(args->msg), 1942e1ff74c0SDmitry Chagin args->flags, &bsd_msg)); 1943e1ff74c0SDmitry Chagin } 1944e1ff74c0SDmitry Chagin 19451744f14eSDmitry Chagin static int 19461744f14eSDmitry Chagin linux_recvmmsg_common(struct thread *td, l_int s, struct l_mmsghdr *msg, 19471744f14eSDmitry Chagin l_uint vlen, l_uint flags, struct timespec *tts) 1948e1ff74c0SDmitry Chagin { 1949e1ff74c0SDmitry Chagin struct msghdr bsd_msg; 19501744f14eSDmitry Chagin struct timespec ts; 19513a99aac6SDmitry Chagin struct file *fp; 1952e1ff74c0SDmitry Chagin l_uint retval; 1953e1ff74c0SDmitry Chagin int error, datagrams; 1954e1ff74c0SDmitry Chagin 19553212ad15SMateusz Guzik error = getsock(td, s, &cap_recv_rights, &fp); 19563a99aac6SDmitry Chagin if (error != 0) 19573a99aac6SDmitry Chagin return (error); 1958e1ff74c0SDmitry Chagin datagrams = 0; 19591744f14eSDmitry Chagin while (datagrams < vlen) { 19601744f14eSDmitry Chagin error = linux_recvmsg_common(td, s, &msg->msg_hdr, 19611744f14eSDmitry Chagin flags & ~LINUX_MSG_WAITFORONE, &bsd_msg); 1962e1ff74c0SDmitry Chagin if (error != 0) 1963e1ff74c0SDmitry Chagin break; 1964e1ff74c0SDmitry Chagin 1965e1ff74c0SDmitry Chagin retval = td->td_retval[0]; 1966e1ff74c0SDmitry Chagin error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len)); 1967e1ff74c0SDmitry Chagin if (error != 0) 1968e1ff74c0SDmitry Chagin break; 1969e1ff74c0SDmitry Chagin ++msg; 1970e1ff74c0SDmitry Chagin ++datagrams; 1971e1ff74c0SDmitry Chagin 1972e1ff74c0SDmitry Chagin /* 1973e1ff74c0SDmitry Chagin * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet. 1974e1ff74c0SDmitry Chagin */ 19751744f14eSDmitry Chagin if (flags & LINUX_MSG_WAITFORONE) 19761744f14eSDmitry Chagin flags |= LINUX_MSG_DONTWAIT; 1977e1ff74c0SDmitry Chagin 1978e1ff74c0SDmitry Chagin /* 1979e1ff74c0SDmitry Chagin * See BUGS section of recvmmsg(2). 1980e1ff74c0SDmitry Chagin */ 19811744f14eSDmitry Chagin if (tts) { 1982e1ff74c0SDmitry Chagin getnanotime(&ts); 19831744f14eSDmitry Chagin timespecsub(&ts, tts, &ts); 1984e1ff74c0SDmitry Chagin if (!timespecisset(&ts) || ts.tv_sec > 0) 1985e1ff74c0SDmitry Chagin break; 1986e1ff74c0SDmitry Chagin } 1987e1ff74c0SDmitry Chagin /* Out of band data, return right away. */ 1988e1ff74c0SDmitry Chagin if (bsd_msg.msg_flags & MSG_OOB) 1989e1ff74c0SDmitry Chagin break; 1990e1ff74c0SDmitry Chagin } 1991e1ff74c0SDmitry Chagin if (error == 0) 1992e1ff74c0SDmitry Chagin td->td_retval[0] = datagrams; 19933a99aac6SDmitry Chagin fdrop(fp, td); 1994e1ff74c0SDmitry Chagin return (error); 1995e1ff74c0SDmitry Chagin } 1996e1ff74c0SDmitry Chagin 1997e1ff74c0SDmitry Chagin int 19981744f14eSDmitry Chagin linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args) 19991744f14eSDmitry Chagin { 20001744f14eSDmitry Chagin struct timespec ts, tts, *ptts; 20011744f14eSDmitry Chagin int error; 20021744f14eSDmitry Chagin 20031744f14eSDmitry Chagin if (args->timeout) { 2004707e567aSDmitry Chagin error = linux_get_timespec(&ts, args->timeout); 20051744f14eSDmitry Chagin if (error != 0) 20061744f14eSDmitry Chagin return (error); 20071744f14eSDmitry Chagin getnanotime(&tts); 20081744f14eSDmitry Chagin timespecadd(&tts, &ts, &tts); 20091744f14eSDmitry Chagin ptts = &tts; 20101744f14eSDmitry Chagin } 20111744f14eSDmitry Chagin else ptts = NULL; 20121744f14eSDmitry Chagin 20131744f14eSDmitry Chagin return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg), 20141744f14eSDmitry Chagin args->vlen, args->flags, ptts)); 20151744f14eSDmitry Chagin } 20161744f14eSDmitry Chagin 20171744f14eSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 20181744f14eSDmitry Chagin int 20191744f14eSDmitry Chagin linux_recvmmsg_time64(struct thread *td, struct linux_recvmmsg_time64_args *args) 20201744f14eSDmitry Chagin { 20211744f14eSDmitry Chagin struct timespec ts, tts, *ptts; 20221744f14eSDmitry Chagin int error; 20231744f14eSDmitry Chagin 20241744f14eSDmitry Chagin if (args->timeout) { 2025707e567aSDmitry Chagin error = linux_get_timespec64(&ts, args->timeout); 20261744f14eSDmitry Chagin if (error != 0) 20271744f14eSDmitry Chagin return (error); 20281744f14eSDmitry Chagin getnanotime(&tts); 20291744f14eSDmitry Chagin timespecadd(&tts, &ts, &tts); 20301744f14eSDmitry Chagin ptts = &tts; 20311744f14eSDmitry Chagin } 20321744f14eSDmitry Chagin else ptts = NULL; 20331744f14eSDmitry Chagin 20341744f14eSDmitry Chagin return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg), 20351744f14eSDmitry Chagin args->vlen, args->flags, ptts)); 20361744f14eSDmitry Chagin } 20371744f14eSDmitry Chagin #endif 20381744f14eSDmitry Chagin 20391744f14eSDmitry Chagin int 2040b40ce416SJulian Elischer linux_shutdown(struct thread *td, struct linux_shutdown_args *args) 2041c21dee17SSøren Schmidt { 2042c21dee17SSøren Schmidt 2043d293f35cSEdward Tomasz Napierala return (kern_shutdown(td, args->s, args->how)); 2044c21dee17SSøren Schmidt } 2045c21dee17SSøren Schmidt 2046a12b9b3dSDmitry Chagin int 2047b40ce416SJulian Elischer linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args) 2048c21dee17SSøren Schmidt { 20490e26e54bSDmitry Chagin struct proc *p = td->td_proc; 20500e26e54bSDmitry Chagin struct linux_pemuldata *pem; 205103cc95d2SDmitry Chagin l_timeval linux_tv; 2052d5368bf3SDmitry Chagin struct sockaddr *sa; 205303cc95d2SDmitry Chagin struct timeval tv; 2054d5368bf3SDmitry Chagin socklen_t len; 205553494b91SDmitry Chagin int error, level, name, val; 2056c21dee17SSøren Schmidt 20579c6eb0f9SEdward Tomasz Napierala level = linux_to_bsd_sockopt_level(args->level); 20589c6eb0f9SEdward Tomasz Napierala switch (level) { 2059c21dee17SSøren Schmidt case SOL_SOCKET: 2060745aaef5SKonstantin Belousov name = linux_to_bsd_so_sockopt(args->optname); 206103cc95d2SDmitry Chagin switch (name) { 2062443d8a07SConrad Meyer case LOCAL_CREDS_PERSISTENT: 2063443d8a07SConrad Meyer level = SOL_LOCAL; 2064443d8a07SConrad Meyer break; 206503cc95d2SDmitry Chagin case SO_RCVTIMEO: 206603cc95d2SDmitry Chagin /* FALLTHROUGH */ 206703cc95d2SDmitry Chagin case SO_SNDTIMEO: 206803cc95d2SDmitry Chagin error = copyin(PTRIN(args->optval), &linux_tv, 206903cc95d2SDmitry Chagin sizeof(linux_tv)); 2070dddb7e7fSDmitry Chagin if (error != 0) 207103cc95d2SDmitry Chagin return (error); 207203cc95d2SDmitry Chagin tv.tv_sec = linux_tv.tv_sec; 207303cc95d2SDmitry Chagin tv.tv_usec = linux_tv.tv_usec; 20749c6eb0f9SEdward Tomasz Napierala return (kern_setsockopt(td, args->s, level, 207503cc95d2SDmitry Chagin name, &tv, UIO_SYSSPACE, sizeof(tv))); 207603cc95d2SDmitry Chagin /* NOTREACHED */ 20770e26e54bSDmitry Chagin case SO_TIMESTAMP: 207853494b91SDmitry Chagin /* overwrite SO_BINTIME */ 207953494b91SDmitry Chagin val = 0; 208053494b91SDmitry Chagin error = kern_setsockopt(td, args->s, level, 208153494b91SDmitry Chagin SO_BINTIME, &val, UIO_SYSSPACE, sizeof(val)); 208253494b91SDmitry Chagin if (error != 0) 208353494b91SDmitry Chagin return (error); 20840e26e54bSDmitry Chagin pem = pem_find(p); 20850e26e54bSDmitry Chagin pem->so_timestamp = args->optname; 20860e26e54bSDmitry Chagin break; 208771bc8bcfSDmitry Chagin case SO_BINTIME: 208853494b91SDmitry Chagin /* overwrite SO_TIMESTAMP */ 208953494b91SDmitry Chagin val = 0; 209053494b91SDmitry Chagin error = kern_setsockopt(td, args->s, level, 209153494b91SDmitry Chagin SO_TIMESTAMP, &val, UIO_SYSSPACE, sizeof(val)); 209253494b91SDmitry Chagin if (error != 0) 209353494b91SDmitry Chagin return (error); 209471bc8bcfSDmitry Chagin pem = pem_find(p); 209571bc8bcfSDmitry Chagin pem->so_timestampns = args->optname; 209671bc8bcfSDmitry Chagin break; 209703cc95d2SDmitry Chagin default: 209803cc95d2SDmitry Chagin break; 209903cc95d2SDmitry Chagin } 2100c21dee17SSøren Schmidt break; 2101c21dee17SSøren Schmidt case IPPROTO_IP: 2102da6d8ae6SEdward Tomasz Napierala if (args->optname == LINUX_IP_RECVERR && 2103da6d8ae6SEdward Tomasz Napierala linux_ignore_ip_recverr) { 2104da6d8ae6SEdward Tomasz Napierala /* 2105da6d8ae6SEdward Tomasz Napierala * XXX: This is a hack to unbreak DNS resolution 2106da6d8ae6SEdward Tomasz Napierala * with glibc 2.30 and above. 2107da6d8ae6SEdward Tomasz Napierala */ 2108da6d8ae6SEdward Tomasz Napierala return (0); 2109da6d8ae6SEdward Tomasz Napierala } 2110745aaef5SKonstantin Belousov name = linux_to_bsd_ip_sockopt(args->optname); 2111c21dee17SSøren Schmidt break; 211286a9058bSAndrey V. Elsukov case IPPROTO_IPV6: 211386a9058bSAndrey V. Elsukov name = linux_to_bsd_ip6_sockopt(args->optname); 211486a9058bSAndrey V. Elsukov break; 2115dad3b88aSMike Smith case IPPROTO_TCP: 2116fb709557SJohn Baldwin name = linux_to_bsd_tcp_sockopt(args->optname); 2117dad3b88aSMike Smith break; 21187c40e2d5SAlexander V. Chernikov case SOL_NETLINK: 21197c40e2d5SAlexander V. Chernikov level = SOL_SOCKET; 21207c40e2d5SAlexander V. Chernikov name = args->optname; 21217c40e2d5SAlexander V. Chernikov break; 2122c21dee17SSøren Schmidt default: 21233f3a4815SMarcel Moolenaar name = -1; 21243f3a4815SMarcel Moolenaar break; 2125c21dee17SSøren Schmidt } 21268ec6c4a3SAlexander Leidinger if (name < 0) { 21278ec6c4a3SAlexander Leidinger if (name == -1) 21285d481ad8SEdward Tomasz Napierala linux_msg(curthread, 21295d481ad8SEdward Tomasz Napierala "unsupported setsockopt level %d optname %d", 21305d481ad8SEdward Tomasz Napierala args->level, args->optname); 2131d4b7423fSAlexander Leidinger return (ENOPROTOOPT); 21325d481ad8SEdward Tomasz Napierala } 2133d5368bf3SDmitry Chagin 2134d5368bf3SDmitry Chagin if (name == IPV6_NEXTHOP) { 2135d5368bf3SDmitry Chagin len = args->optlen; 2136d5368bf3SDmitry Chagin error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len); 2137d5368bf3SDmitry Chagin if (error != 0) 2138d5368bf3SDmitry Chagin return (error); 2139d5368bf3SDmitry Chagin 21409c6eb0f9SEdward Tomasz Napierala error = kern_setsockopt(td, args->s, level, 2141d5368bf3SDmitry Chagin name, sa, UIO_SYSSPACE, len); 2142d5368bf3SDmitry Chagin free(sa, M_SONAME); 2143d5368bf3SDmitry Chagin } else { 21449c6eb0f9SEdward Tomasz Napierala error = kern_setsockopt(td, args->s, level, 21459c6eb0f9SEdward Tomasz Napierala name, PTRIN(args->optval), UIO_USERSPACE, args->optlen); 2146d5368bf3SDmitry Chagin } 21475c8919adSAlexander Leidinger 21485c8919adSAlexander Leidinger return (error); 2149c21dee17SSøren Schmidt } 2150c21dee17SSøren Schmidt 2151e44a78ceSEdward Tomasz Napierala static int 2152e92b9a9eSDmitry Chagin linux_sockopt_copyout(struct thread *td, void *val, socklen_t len, 2153e92b9a9eSDmitry Chagin struct linux_getsockopt_args *args) 2154e92b9a9eSDmitry Chagin { 2155e92b9a9eSDmitry Chagin int error; 2156e92b9a9eSDmitry Chagin 2157e92b9a9eSDmitry Chagin error = copyout(val, PTRIN(args->optval), len); 2158e92b9a9eSDmitry Chagin if (error == 0) 2159e92b9a9eSDmitry Chagin error = copyout(&len, PTRIN(args->optlen), sizeof(len)); 2160e92b9a9eSDmitry Chagin return (error); 2161e92b9a9eSDmitry Chagin } 2162e92b9a9eSDmitry Chagin 2163e92b9a9eSDmitry Chagin static int 2164cd84c82cSEdward Tomasz Napierala linux_getsockopt_so_peergroups(struct thread *td, 2165cd84c82cSEdward Tomasz Napierala struct linux_getsockopt_args *args) 2166cd84c82cSEdward Tomasz Napierala { 2167cd84c82cSEdward Tomasz Napierala struct xucred xu; 2168cd84c82cSEdward Tomasz Napierala socklen_t xulen, len; 2169cd84c82cSEdward Tomasz Napierala int error, i; 2170cd84c82cSEdward Tomasz Napierala 2171cd84c82cSEdward Tomasz Napierala xulen = sizeof(xu); 2172cd84c82cSEdward Tomasz Napierala error = kern_getsockopt(td, args->s, 0, 2173cd84c82cSEdward Tomasz Napierala LOCAL_PEERCRED, &xu, UIO_SYSSPACE, &xulen); 2174cd84c82cSEdward Tomasz Napierala if (error != 0) 2175cd84c82cSEdward Tomasz Napierala return (error); 2176cd84c82cSEdward Tomasz Napierala 2177cd84c82cSEdward Tomasz Napierala len = xu.cr_ngroups * sizeof(l_gid_t); 2178cd84c82cSEdward Tomasz Napierala if (args->optlen < len) { 2179cd84c82cSEdward Tomasz Napierala error = copyout(&len, PTRIN(args->optlen), sizeof(len)); 2180cd84c82cSEdward Tomasz Napierala if (error == 0) 2181cd84c82cSEdward Tomasz Napierala error = ERANGE; 2182cd84c82cSEdward Tomasz Napierala return (error); 2183cd84c82cSEdward Tomasz Napierala } 2184cd84c82cSEdward Tomasz Napierala 2185cd84c82cSEdward Tomasz Napierala /* 2186cd84c82cSEdward Tomasz Napierala * "- 1" to skip the primary group. 2187cd84c82cSEdward Tomasz Napierala */ 2188cd84c82cSEdward Tomasz Napierala for (i = 0; i < xu.cr_ngroups - 1; i++) { 2189cd84c82cSEdward Tomasz Napierala error = copyout(xu.cr_groups + i + 1, 2190cd84c82cSEdward Tomasz Napierala (void *)(args->optval + i * sizeof(l_gid_t)), 2191cd84c82cSEdward Tomasz Napierala sizeof(l_gid_t)); 2192cd84c82cSEdward Tomasz Napierala if (error != 0) 2193cd84c82cSEdward Tomasz Napierala return (error); 2194cd84c82cSEdward Tomasz Napierala } 2195cd84c82cSEdward Tomasz Napierala 2196cd84c82cSEdward Tomasz Napierala error = copyout(&len, PTRIN(args->optlen), sizeof(len)); 2197cd84c82cSEdward Tomasz Napierala return (error); 2198cd84c82cSEdward Tomasz Napierala } 2199cd84c82cSEdward Tomasz Napierala 2200cd84c82cSEdward Tomasz Napierala static int 2201e44a78ceSEdward Tomasz Napierala linux_getsockopt_so_peersec(struct thread *td, 2202e44a78ceSEdward Tomasz Napierala struct linux_getsockopt_args *args) 2203e44a78ceSEdward Tomasz Napierala { 2204e44a78ceSEdward Tomasz Napierala socklen_t len; 2205e44a78ceSEdward Tomasz Napierala int error; 2206e44a78ceSEdward Tomasz Napierala 2207e44a78ceSEdward Tomasz Napierala len = sizeof(SECURITY_CONTEXT_STRING); 2208e44a78ceSEdward Tomasz Napierala if (args->optlen < len) { 2209e44a78ceSEdward Tomasz Napierala error = copyout(&len, PTRIN(args->optlen), sizeof(len)); 2210e44a78ceSEdward Tomasz Napierala if (error == 0) 2211e44a78ceSEdward Tomasz Napierala error = ERANGE; 2212e44a78ceSEdward Tomasz Napierala return (error); 2213e44a78ceSEdward Tomasz Napierala } 2214e44a78ceSEdward Tomasz Napierala 2215e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, SECURITY_CONTEXT_STRING, 2216e92b9a9eSDmitry Chagin len, args)); 2217e44a78ceSEdward Tomasz Napierala } 2218e44a78ceSEdward Tomasz Napierala 2219e8d9d808SDmitry Chagin static int 2220e8d9d808SDmitry Chagin linux_getsockopt_so_linger(struct thread *td, 2221e8d9d808SDmitry Chagin struct linux_getsockopt_args *args) 2222e8d9d808SDmitry Chagin { 2223e8d9d808SDmitry Chagin struct linger ling; 2224e8d9d808SDmitry Chagin socklen_t len; 2225e8d9d808SDmitry Chagin int error; 2226e8d9d808SDmitry Chagin 2227e8d9d808SDmitry Chagin len = sizeof(ling); 2228e8d9d808SDmitry Chagin error = kern_getsockopt(td, args->s, SOL_SOCKET, 2229e8d9d808SDmitry Chagin SO_LINGER, &ling, UIO_SYSSPACE, &len); 2230e8d9d808SDmitry Chagin if (error != 0) 2231e8d9d808SDmitry Chagin return (error); 2232e8d9d808SDmitry Chagin ling.l_onoff = ((ling.l_onoff & SO_LINGER) != 0); 2233e8d9d808SDmitry Chagin return (linux_sockopt_copyout(td, &ling, len, args)); 2234e8d9d808SDmitry Chagin } 2235e92b9a9eSDmitry Chagin 2236a12b9b3dSDmitry Chagin int 2237b40ce416SJulian Elischer linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args) 2238c21dee17SSøren Schmidt { 223903cc95d2SDmitry Chagin l_timeval linux_tv; 224003cc95d2SDmitry Chagin struct timeval tv; 2241d56e689eSDmitry Chagin socklen_t tv_len, xulen, len; 2242d5368bf3SDmitry Chagin struct sockaddr *sa; 2243d4dd69c4SDmitry Chagin struct xucred xu; 2244d4dd69c4SDmitry Chagin struct l_ucred lxu; 2245dfd060c0SEdward Tomasz Napierala int error, level, name, newval; 2246c21dee17SSøren Schmidt 2247dfd060c0SEdward Tomasz Napierala level = linux_to_bsd_sockopt_level(args->level); 2248dfd060c0SEdward Tomasz Napierala switch (level) { 2249c21dee17SSøren Schmidt case SOL_SOCKET: 2250cd84c82cSEdward Tomasz Napierala switch (args->optname) { 2251cd84c82cSEdward Tomasz Napierala case LINUX_SO_PEERGROUPS: 2252cd84c82cSEdward Tomasz Napierala return (linux_getsockopt_so_peergroups(td, args)); 2253cd84c82cSEdward Tomasz Napierala case LINUX_SO_PEERSEC: 2254e44a78ceSEdward Tomasz Napierala return (linux_getsockopt_so_peersec(td, args)); 2255cd84c82cSEdward Tomasz Napierala default: 2256cd84c82cSEdward Tomasz Napierala break; 2257cd84c82cSEdward Tomasz Napierala } 2258cd84c82cSEdward Tomasz Napierala 2259745aaef5SKonstantin Belousov name = linux_to_bsd_so_sockopt(args->optname); 226003cc95d2SDmitry Chagin switch (name) { 2261443d8a07SConrad Meyer case LOCAL_CREDS_PERSISTENT: 2262443d8a07SConrad Meyer level = SOL_LOCAL; 2263443d8a07SConrad Meyer break; 226403cc95d2SDmitry Chagin case SO_RCVTIMEO: 226503cc95d2SDmitry Chagin /* FALLTHROUGH */ 226603cc95d2SDmitry Chagin case SO_SNDTIMEO: 226703cc95d2SDmitry Chagin tv_len = sizeof(tv); 2268dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level, 226903cc95d2SDmitry Chagin name, &tv, UIO_SYSSPACE, &tv_len); 2270dddb7e7fSDmitry Chagin if (error != 0) 227103cc95d2SDmitry Chagin return (error); 227203cc95d2SDmitry Chagin linux_tv.tv_sec = tv.tv_sec; 227303cc95d2SDmitry Chagin linux_tv.tv_usec = tv.tv_usec; 2274e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, &linux_tv, 2275e92b9a9eSDmitry Chagin sizeof(linux_tv), args)); 227603cc95d2SDmitry Chagin /* NOTREACHED */ 2277d4dd69c4SDmitry Chagin case LOCAL_PEERCRED: 2278cd92d27eSDmitry Chagin if (args->optlen < sizeof(lxu)) 2279d4dd69c4SDmitry Chagin return (EINVAL); 2280bb376a99SMark Johnston /* 2281bb376a99SMark Johnston * LOCAL_PEERCRED is not served at the SOL_SOCKET level, 2282bb376a99SMark Johnston * but by the Unix socket's level 0. 2283bb376a99SMark Johnston */ 2284dfd060c0SEdward Tomasz Napierala level = 0; 2285d4dd69c4SDmitry Chagin xulen = sizeof(xu); 2286dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level, 2287d4dd69c4SDmitry Chagin name, &xu, UIO_SYSSPACE, &xulen); 2288dddb7e7fSDmitry Chagin if (error != 0) 2289d4dd69c4SDmitry Chagin return (error); 2290c5afec6eSDmitry Chagin lxu.pid = xu.cr_pid; 2291d4dd69c4SDmitry Chagin lxu.uid = xu.cr_uid; 2292d4dd69c4SDmitry Chagin lxu.gid = xu.cr_gid; 2293e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, &lxu, 2294e92b9a9eSDmitry Chagin sizeof(lxu), args)); 2295d4dd69c4SDmitry Chagin /* NOTREACHED */ 2296d56e689eSDmitry Chagin case SO_ERROR: 2297d56e689eSDmitry Chagin len = sizeof(newval); 2298dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level, 2299d56e689eSDmitry Chagin name, &newval, UIO_SYSSPACE, &len); 2300dddb7e7fSDmitry Chagin if (error != 0) 2301d56e689eSDmitry Chagin return (error); 2302866b1f51SEdward Tomasz Napierala newval = -bsd_to_linux_errno(newval); 2303e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, &newval, 2304e92b9a9eSDmitry Chagin len, args)); 2305d56e689eSDmitry Chagin /* NOTREACHED */ 2306cf312f79SDmitry Chagin case SO_DOMAIN: 2307cf312f79SDmitry Chagin len = sizeof(newval); 2308cf312f79SDmitry Chagin error = kern_getsockopt(td, args->s, level, 2309cf312f79SDmitry Chagin name, &newval, UIO_SYSSPACE, &len); 2310cf312f79SDmitry Chagin if (error != 0) 2311cf312f79SDmitry Chagin return (error); 2312cf312f79SDmitry Chagin newval = bsd_to_linux_domain(newval); 2313cf312f79SDmitry Chagin if (newval == -1) 2314cf312f79SDmitry Chagin return (ENOPROTOOPT); 2315e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, &newval, 2316e92b9a9eSDmitry Chagin len, args)); 2317cf312f79SDmitry Chagin /* NOTREACHED */ 2318e8d9d808SDmitry Chagin case SO_LINGER: 2319e8d9d808SDmitry Chagin return (linux_getsockopt_so_linger(td, args)); 2320e8d9d808SDmitry Chagin /* NOTREACHED */ 232103cc95d2SDmitry Chagin default: 232203cc95d2SDmitry Chagin break; 232303cc95d2SDmitry Chagin } 2324c21dee17SSøren Schmidt break; 2325c21dee17SSøren Schmidt case IPPROTO_IP: 2326745aaef5SKonstantin Belousov name = linux_to_bsd_ip_sockopt(args->optname); 2327c21dee17SSøren Schmidt break; 232886a9058bSAndrey V. Elsukov case IPPROTO_IPV6: 232986a9058bSAndrey V. Elsukov name = linux_to_bsd_ip6_sockopt(args->optname); 233086a9058bSAndrey V. Elsukov break; 2331dad3b88aSMike Smith case IPPROTO_TCP: 2332fb709557SJohn Baldwin name = linux_to_bsd_tcp_sockopt(args->optname); 2333dad3b88aSMike Smith break; 2334c21dee17SSøren Schmidt default: 23353f3a4815SMarcel Moolenaar name = -1; 23363f3a4815SMarcel Moolenaar break; 2337c21dee17SSøren Schmidt } 23388ec6c4a3SAlexander Leidinger if (name < 0) { 23398ec6c4a3SAlexander Leidinger if (name == -1) 23405d481ad8SEdward Tomasz Napierala linux_msg(curthread, 23415d481ad8SEdward Tomasz Napierala "unsupported getsockopt level %d optname %d", 23425d481ad8SEdward Tomasz Napierala args->level, args->optname); 23433f3a4815SMarcel Moolenaar return (EINVAL); 23445d481ad8SEdward Tomasz Napierala } 23453f3a4815SMarcel Moolenaar 23465c8919adSAlexander Leidinger if (name == IPV6_NEXTHOP) { 2347d5368bf3SDmitry Chagin error = copyin(PTRIN(args->optlen), &len, sizeof(len)); 2348d5368bf3SDmitry Chagin if (error != 0) 2349d5368bf3SDmitry Chagin return (error); 2350d5368bf3SDmitry Chagin sa = malloc(len, M_SONAME, M_WAITOK); 2351d5368bf3SDmitry Chagin 2352dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level, 2353d5368bf3SDmitry Chagin name, sa, UIO_SYSSPACE, &len); 2354d5368bf3SDmitry Chagin if (error != 0) 2355d5368bf3SDmitry Chagin goto out; 2356d5368bf3SDmitry Chagin 2357106a784bSEdward Tomasz Napierala error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len); 2358d5368bf3SDmitry Chagin if (error == 0) 2359d5368bf3SDmitry Chagin error = copyout(&len, PTRIN(args->optlen), 2360d5368bf3SDmitry Chagin sizeof(len)); 2361d5368bf3SDmitry Chagin out: 2362d5368bf3SDmitry Chagin free(sa, M_SONAME); 2363d5368bf3SDmitry Chagin } else { 2364dfd060c0SEdward Tomasz Napierala if (args->optval) { 2365dfd060c0SEdward Tomasz Napierala error = copyin(PTRIN(args->optlen), &len, sizeof(len)); 2366dfd060c0SEdward Tomasz Napierala if (error != 0) 2367dfd060c0SEdward Tomasz Napierala return (error); 2368dfd060c0SEdward Tomasz Napierala } 2369dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level, 2370dfd060c0SEdward Tomasz Napierala name, PTRIN(args->optval), UIO_USERSPACE, &len); 2371dfd060c0SEdward Tomasz Napierala if (error == 0) 2372dfd060c0SEdward Tomasz Napierala error = copyout(&len, PTRIN(args->optlen), 2373dfd060c0SEdward Tomasz Napierala sizeof(len)); 2374d5368bf3SDmitry Chagin } 23755c8919adSAlexander Leidinger 23765c8919adSAlexander Leidinger return (error); 2377c21dee17SSøren Schmidt } 2378c21dee17SSøren Schmidt 2379bb66c597SJames McLaughlin /* 2380bb66c597SJames McLaughlin * Based on sendfile_getsock from kern_sendfile.c 2381bb66c597SJames McLaughlin * Determines whether an fd is a stream socket that can be used 2382bb66c597SJames McLaughlin * with FreeBSD sendfile. 2383bb66c597SJames McLaughlin */ 2384bb66c597SJames McLaughlin static bool 2385da5a6738SDmitry Chagin is_sendfile(struct file *fp, struct file *ofp) 2386bb66c597SJames McLaughlin { 2387bb66c597SJames McLaughlin struct socket *so; 2388bb66c597SJames McLaughlin 2389bb66c597SJames McLaughlin /* 2390da5a6738SDmitry Chagin * FreeBSD sendfile() system call sends a regular file or 2391da5a6738SDmitry Chagin * shared memory object out a stream socket. 2392da5a6738SDmitry Chagin */ 2393da5a6738SDmitry Chagin if ((fp->f_type != DTYPE_SHM && fp->f_type != DTYPE_VNODE) || 2394da5a6738SDmitry Chagin (fp->f_type == DTYPE_VNODE && 2395da5a6738SDmitry Chagin (fp->f_vnode == NULL || fp->f_vnode->v_type != VREG))) 2396da5a6738SDmitry Chagin return (false); 2397da5a6738SDmitry Chagin /* 2398bb66c597SJames McLaughlin * The socket must be a stream socket and connected. 2399bb66c597SJames McLaughlin */ 2400da5a6738SDmitry Chagin if (ofp->f_type != DTYPE_SOCKET) 2401bb66c597SJames McLaughlin return (false); 2402da5a6738SDmitry Chagin so = ofp->f_data; 2403bb66c597SJames McLaughlin if (so->so_type != SOCK_STREAM) 2404bb66c597SJames McLaughlin return (false); 2405bb66c597SJames McLaughlin /* 2406bb66c597SJames McLaughlin * SCTP one-to-one style sockets currently don't work with 2407bb66c597SJames McLaughlin * sendfile(). 2408bb66c597SJames McLaughlin */ 2409bb66c597SJames McLaughlin if (so->so_proto->pr_protocol == IPPROTO_SCTP) 2410bb66c597SJames McLaughlin return (false); 2411bb66c597SJames McLaughlin return (!SOLISTENING(so)); 2412bb66c597SJames McLaughlin } 2413bb66c597SJames McLaughlin 2414bb66c597SJames McLaughlin static bool 2415bb66c597SJames McLaughlin is_regular_file(struct file *fp) 2416bb66c597SJames McLaughlin { 2417bb66c597SJames McLaughlin 2418bb66c597SJames McLaughlin return (fp->f_type == DTYPE_VNODE && fp->f_vnode != NULL && 2419bb66c597SJames McLaughlin fp->f_vnode->v_type == VREG); 2420bb66c597SJames McLaughlin } 2421bb66c597SJames McLaughlin 2422bb66c597SJames McLaughlin static int 2423bb66c597SJames McLaughlin sendfile_fallback(struct thread *td, struct file *fp, l_int out, 2424bb66c597SJames McLaughlin off_t *offset, l_size_t count, off_t *sbytes) 2425bb66c597SJames McLaughlin { 2426bb66c597SJames McLaughlin off_t current_offset, out_offset, to_send; 2427bb66c597SJames McLaughlin l_size_t bytes_sent, n_read; 2428bb66c597SJames McLaughlin struct file *ofp; 2429bb66c597SJames McLaughlin struct iovec aiov; 2430bb66c597SJames McLaughlin struct uio auio; 2431bb66c597SJames McLaughlin bool seekable; 2432bb66c597SJames McLaughlin size_t bufsz; 2433bb66c597SJames McLaughlin void *buf; 2434bb66c597SJames McLaughlin int flags, error; 2435bb66c597SJames McLaughlin 2436bb66c597SJames McLaughlin if (offset == NULL) { 2437bb66c597SJames McLaughlin if ((error = fo_seek(fp, 0, SEEK_CUR, td)) != 0) 2438bb66c597SJames McLaughlin return (error); 2439bb66c597SJames McLaughlin current_offset = td->td_uretoff.tdu_off; 2440bb66c597SJames McLaughlin } else { 2441bb66c597SJames McLaughlin if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) 2442bb66c597SJames McLaughlin return (ESPIPE); 2443bb66c597SJames McLaughlin current_offset = *offset; 2444bb66c597SJames McLaughlin } 2445bb66c597SJames McLaughlin error = fget_write(td, out, &cap_pwrite_rights, &ofp); 2446bb66c597SJames McLaughlin if (error != 0) 2447bb66c597SJames McLaughlin return (error); 2448bb66c597SJames McLaughlin seekable = (ofp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0; 2449bb66c597SJames McLaughlin if (seekable) { 2450bb66c597SJames McLaughlin if ((error = fo_seek(ofp, 0, SEEK_CUR, td)) != 0) 2451bb66c597SJames McLaughlin goto drop; 2452bb66c597SJames McLaughlin out_offset = td->td_uretoff.tdu_off; 2453bb66c597SJames McLaughlin } else 2454bb66c597SJames McLaughlin out_offset = 0; 2455bb66c597SJames McLaughlin 2456bb66c597SJames McLaughlin flags = FOF_OFFSET | FOF_NOUPDATE; 2457bb66c597SJames McLaughlin bufsz = min(count, MAXPHYS); 2458bb66c597SJames McLaughlin buf = malloc(bufsz, M_LINUX, M_WAITOK); 2459bb66c597SJames McLaughlin bytes_sent = 0; 2460bb66c597SJames McLaughlin while (bytes_sent < count) { 2461bb66c597SJames McLaughlin to_send = min(count - bytes_sent, bufsz); 2462bb66c597SJames McLaughlin aiov.iov_base = buf; 2463bb66c597SJames McLaughlin aiov.iov_len = bufsz; 2464bb66c597SJames McLaughlin auio.uio_iov = &aiov; 2465bb66c597SJames McLaughlin auio.uio_iovcnt = 1; 2466bb66c597SJames McLaughlin auio.uio_segflg = UIO_SYSSPACE; 2467bb66c597SJames McLaughlin auio.uio_td = td; 2468bb66c597SJames McLaughlin auio.uio_rw = UIO_READ; 2469bb66c597SJames McLaughlin auio.uio_offset = current_offset; 2470bb66c597SJames McLaughlin auio.uio_resid = to_send; 2471bb66c597SJames McLaughlin error = fo_read(fp, &auio, fp->f_cred, flags, td); 2472bb66c597SJames McLaughlin if (error != 0) 2473bb66c597SJames McLaughlin break; 2474bb66c597SJames McLaughlin n_read = to_send - auio.uio_resid; 2475bb66c597SJames McLaughlin if (n_read == 0) 2476bb66c597SJames McLaughlin break; 2477bb66c597SJames McLaughlin aiov.iov_base = buf; 2478bb66c597SJames McLaughlin aiov.iov_len = bufsz; 2479bb66c597SJames McLaughlin auio.uio_iov = &aiov; 2480bb66c597SJames McLaughlin auio.uio_iovcnt = 1; 2481bb66c597SJames McLaughlin auio.uio_segflg = UIO_SYSSPACE; 2482bb66c597SJames McLaughlin auio.uio_td = td; 2483bb66c597SJames McLaughlin auio.uio_rw = UIO_WRITE; 2484bb66c597SJames McLaughlin auio.uio_offset = (seekable) ? out_offset : 0; 2485bb66c597SJames McLaughlin auio.uio_resid = n_read; 2486bb66c597SJames McLaughlin error = fo_write(ofp, &auio, ofp->f_cred, flags, td); 2487bb66c597SJames McLaughlin if (error != 0) 2488bb66c597SJames McLaughlin break; 2489bb66c597SJames McLaughlin bytes_sent += n_read; 2490bb66c597SJames McLaughlin current_offset += n_read; 2491bb66c597SJames McLaughlin out_offset += n_read; 2492bb66c597SJames McLaughlin } 2493bb66c597SJames McLaughlin free(buf, M_LINUX); 2494bb66c597SJames McLaughlin 2495bb66c597SJames McLaughlin if (error == 0) { 2496bb66c597SJames McLaughlin *sbytes = bytes_sent; 2497bb66c597SJames McLaughlin if (offset != NULL) 2498bb66c597SJames McLaughlin *offset = current_offset; 2499bb66c597SJames McLaughlin else 2500bb66c597SJames McLaughlin error = fo_seek(fp, current_offset, SEEK_SET, td); 2501bb66c597SJames McLaughlin } 2502bb66c597SJames McLaughlin if (error == 0 && seekable) 2503bb66c597SJames McLaughlin error = fo_seek(ofp, out_offset, SEEK_SET, td); 2504bb66c597SJames McLaughlin 2505bb66c597SJames McLaughlin drop: 2506bb66c597SJames McLaughlin fdrop(ofp, td); 2507bb66c597SJames McLaughlin return (error); 2508bb66c597SJames McLaughlin } 2509bb66c597SJames McLaughlin 2510bb66c597SJames McLaughlin static int 2511bb66c597SJames McLaughlin sendfile_sendfile(struct thread *td, struct file *fp, l_int out, 2512bb66c597SJames McLaughlin off_t *offset, l_size_t count, off_t *sbytes) 2513bb66c597SJames McLaughlin { 2514bb66c597SJames McLaughlin off_t current_offset; 2515bb66c597SJames McLaughlin int error; 2516bb66c597SJames McLaughlin 2517bb66c597SJames McLaughlin if (offset == NULL) { 2518bb66c597SJames McLaughlin if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0) 2519bb66c597SJames McLaughlin return (ESPIPE); 2520bb66c597SJames McLaughlin if ((error = fo_seek(fp, 0, SEEK_CUR, td)) != 0) 2521bb66c597SJames McLaughlin return (error); 2522bb66c597SJames McLaughlin current_offset = td->td_uretoff.tdu_off; 2523bb66c597SJames McLaughlin } else 2524bb66c597SJames McLaughlin current_offset = *offset; 2525bb66c597SJames McLaughlin error = fo_sendfile(fp, out, NULL, NULL, current_offset, count, 2526bb66c597SJames McLaughlin sbytes, 0, td); 2527bb66c597SJames McLaughlin if (error == 0) { 2528bb66c597SJames McLaughlin current_offset += *sbytes; 2529bb66c597SJames McLaughlin if (offset != NULL) 2530bb66c597SJames McLaughlin *offset = current_offset; 2531bb66c597SJames McLaughlin else 2532bb66c597SJames McLaughlin error = fo_seek(fp, current_offset, SEEK_SET, td); 2533bb66c597SJames McLaughlin } 2534bb66c597SJames McLaughlin return (error); 2535bb66c597SJames McLaughlin } 2536bb66c597SJames McLaughlin 2537fc7510aeSEd Maste static int 2538fc7510aeSEd Maste linux_sendfile_common(struct thread *td, l_int out, l_int in, 25397307c439SDmitry Chagin off_t *offset, l_size_t count) 2540fc7510aeSEd Maste { 2541bb66c597SJames McLaughlin struct file *fp, *ofp; 2542bb66c597SJames McLaughlin off_t sbytes; 2543fc7510aeSEd Maste int error; 2544bb66c597SJames McLaughlin 2545bb66c597SJames McLaughlin /* Linux cannot have 0 count. */ 2546bb66c597SJames McLaughlin if (count <= 0 || (offset != NULL && *offset < 0)) 2547bb66c597SJames McLaughlin return (EINVAL); 2548fc7510aeSEd Maste 2549fc7510aeSEd Maste AUDIT_ARG_FD(in); 2550fc7510aeSEd Maste error = fget_read(td, in, &cap_pread_rights, &fp); 2551fc7510aeSEd Maste if (error != 0) 2552fc7510aeSEd Maste return (error); 2553bb66c597SJames McLaughlin if ((fp->f_type != DTYPE_SHM && fp->f_type != DTYPE_VNODE) || 2554bb66c597SJames McLaughlin (fp->f_type == DTYPE_VNODE && 2555bb66c597SJames McLaughlin (fp->f_vnode == NULL || fp->f_vnode->v_type != VREG))) { 2556fc7510aeSEd Maste error = EINVAL; 2557fc7510aeSEd Maste goto drop; 2558fc7510aeSEd Maste } 2559bb66c597SJames McLaughlin error = fget_unlocked(td, out, &cap_no_rights, &ofp); 2560fc7510aeSEd Maste if (error != 0) 2561fc7510aeSEd Maste goto drop; 2562fc7510aeSEd Maste 2563bb66c597SJames McLaughlin if (is_regular_file(fp) && is_regular_file(ofp)) { 2564bb66c597SJames McLaughlin error = kern_copy_file_range(td, in, offset, out, NULL, count, 2565bb66c597SJames McLaughlin 0); 2566fc7510aeSEd Maste } else { 2567bb66c597SJames McLaughlin sbytes = 0; 2568da5a6738SDmitry Chagin if (is_sendfile(fp, ofp)) 2569bb66c597SJames McLaughlin error = sendfile_sendfile(td, fp, out, offset, count, 2570bb66c597SJames McLaughlin &sbytes); 2571bb66c597SJames McLaughlin else 2572bb66c597SJames McLaughlin error = sendfile_fallback(td, fp, out, offset, count, 2573bb66c597SJames McLaughlin &sbytes); 25744f9fac78SDmitry Chagin if (error == ENOBUFS && (ofp->f_flag & FNONBLOCK) != 0) 25754f9fac78SDmitry Chagin error = EAGAIN; 2576bb66c597SJames McLaughlin if (error == 0) 2577bb66c597SJames McLaughlin td->td_retval[0] = sbytes; 2578fc7510aeSEd Maste } 2579bb66c597SJames McLaughlin fdrop(ofp, td); 2580fc7510aeSEd Maste 2581fc7510aeSEd Maste drop: 2582fc7510aeSEd Maste fdrop(fp, td); 2583fc7510aeSEd Maste return (error); 2584fc7510aeSEd Maste } 2585fc7510aeSEd Maste 2586fc7510aeSEd Maste int 2587fc7510aeSEd Maste linux_sendfile(struct thread *td, struct linux_sendfile_args *arg) 2588fc7510aeSEd Maste { 2589fc7510aeSEd Maste /* 2590fc7510aeSEd Maste * Differences between FreeBSD and Linux sendfile: 2591fc7510aeSEd Maste * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to 2592bb66c597SJames McLaughlin * mean send the whole file). 2593fc7510aeSEd Maste * - Linux can send to any fd whereas FreeBSD only supports sockets. 2594bb66c597SJames McLaughlin * We therefore use FreeBSD sendfile where possible for performance, 2595bb66c597SJames McLaughlin * but fall back on a manual copy (sendfile_fallback). 2596fc7510aeSEd Maste * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr. 2597fc7510aeSEd Maste * - Linux takes an offset pointer and updates it to the read location. 2598fc7510aeSEd Maste * FreeBSD takes in an offset and a 'bytes read' parameter which is 2599fc7510aeSEd Maste * only filled if it isn't NULL. We use this parameter to update the 2600fc7510aeSEd Maste * offset pointer if it exists. 2601fc7510aeSEd Maste * - Linux sendfile returns bytes read on success while FreeBSD 2602fc7510aeSEd Maste * returns 0. We use the 'bytes read' parameter to get this value. 2603fc7510aeSEd Maste */ 2604fc7510aeSEd Maste 26057307c439SDmitry Chagin off_t offset64; 26067307c439SDmitry Chagin l_off_t offset; 2607fc7510aeSEd Maste int error; 2608fc7510aeSEd Maste 2609fc7510aeSEd Maste if (arg->offset != NULL) { 2610fc7510aeSEd Maste error = copyin(arg->offset, &offset, sizeof(offset)); 2611fc7510aeSEd Maste if (error != 0) 2612fc7510aeSEd Maste return (error); 26137307c439SDmitry Chagin offset64 = offset; 2614fc7510aeSEd Maste } 2615fc7510aeSEd Maste 26164a521544SDmitry Chagin error = linux_sendfile_common(td, arg->out, arg->in, 2617fc7510aeSEd Maste arg->offset != NULL ? &offset64 : NULL, arg->count); 2618fc7510aeSEd Maste 26194a521544SDmitry Chagin if (error == 0 && arg->offset != NULL) { 26206ecab394SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2621fc7510aeSEd Maste if (offset64 > INT32_MAX) 2622fc7510aeSEd Maste return (EOVERFLOW); 2623fc7510aeSEd Maste #endif 26247307c439SDmitry Chagin offset = (l_off_t)offset64; 2625fc7510aeSEd Maste error = copyout(&offset, arg->offset, sizeof(offset)); 2626fc7510aeSEd Maste } 2627fc7510aeSEd Maste 26284a521544SDmitry Chagin return (error); 2629fc7510aeSEd Maste } 2630fc7510aeSEd Maste 26316ecab394SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32)) 2632fc7510aeSEd Maste int 2633fc7510aeSEd Maste linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg) 2634fc7510aeSEd Maste { 26357307c439SDmitry Chagin off_t offset; 2636fc7510aeSEd Maste int error; 2637fc7510aeSEd Maste 2638fc7510aeSEd Maste if (arg->offset != NULL) { 2639fc7510aeSEd Maste error = copyin(arg->offset, &offset, sizeof(offset)); 2640fc7510aeSEd Maste if (error != 0) 2641fc7510aeSEd Maste return (error); 2642fc7510aeSEd Maste } 2643fc7510aeSEd Maste 26444a521544SDmitry Chagin error = linux_sendfile_common(td, arg->out, arg->in, 2645fc7510aeSEd Maste arg->offset != NULL ? &offset : NULL, arg->count); 2646fc7510aeSEd Maste 26474a521544SDmitry Chagin if (error == 0 && arg->offset != NULL) 2648fc7510aeSEd Maste error = copyout(&offset, arg->offset, sizeof(offset)); 2649fc7510aeSEd Maste 26504a521544SDmitry Chagin return (error); 2651fc7510aeSEd Maste } 26527f8f1d7fSDmitry Chagin 2653ea7b81d2SDmitry Chagin /* Argument list sizes for linux_socketcall */ 2654abf20e93SDmitry Chagin static const unsigned char lxs_args_cnt[] = { 2655abf20e93SDmitry Chagin 0 /* unused*/, 3 /* socket */, 2656abf20e93SDmitry Chagin 3 /* bind */, 3 /* connect */, 2657abf20e93SDmitry Chagin 2 /* listen */, 3 /* accept */, 2658abf20e93SDmitry Chagin 3 /* getsockname */, 3 /* getpeername */, 2659abf20e93SDmitry Chagin 4 /* socketpair */, 4 /* send */, 2660abf20e93SDmitry Chagin 4 /* recv */, 6 /* sendto */, 2661abf20e93SDmitry Chagin 6 /* recvfrom */, 2 /* shutdown */, 2662abf20e93SDmitry Chagin 5 /* setsockopt */, 5 /* getsockopt */, 2663abf20e93SDmitry Chagin 3 /* sendmsg */, 3 /* recvmsg */, 2664abf20e93SDmitry Chagin 4 /* accept4 */, 5 /* recvmmsg */, 2665fc7510aeSEd Maste 4 /* sendmmsg */, 4 /* sendfile */ 2666ea7b81d2SDmitry Chagin }; 2667abf20e93SDmitry Chagin #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1) 2668abf20e93SDmitry Chagin #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong)) 2669ea7b81d2SDmitry Chagin 2670c21dee17SSøren Schmidt int 2671b40ce416SJulian Elischer linux_socketcall(struct thread *td, struct linux_socketcall_args *args) 2672c21dee17SSøren Schmidt { 2673ea7b81d2SDmitry Chagin l_ulong a[6]; 2674abf20e93SDmitry Chagin #if defined(__amd64__) && defined(COMPAT_LINUX32) 2675abf20e93SDmitry Chagin register_t l_args[6]; 2676abf20e93SDmitry Chagin #endif 2677ea7b81d2SDmitry Chagin void *arg; 2678ea7b81d2SDmitry Chagin int error; 26793f3a4815SMarcel Moolenaar 2680abf20e93SDmitry Chagin if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT) 2681ea7b81d2SDmitry Chagin return (EINVAL); 2682abf20e93SDmitry Chagin error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what)); 2683abf20e93SDmitry Chagin if (error != 0) 2684ea7b81d2SDmitry Chagin return (error); 2685ea7b81d2SDmitry Chagin 2686abf20e93SDmitry Chagin #if defined(__amd64__) && defined(COMPAT_LINUX32) 2687abf20e93SDmitry Chagin for (int i = 0; i < lxs_args_cnt[args->what]; ++i) 2688abf20e93SDmitry Chagin l_args[i] = a[i]; 2689abf20e93SDmitry Chagin arg = l_args; 2690abf20e93SDmitry Chagin #else 2691ea7b81d2SDmitry Chagin arg = a; 2692abf20e93SDmitry Chagin #endif 2693c21dee17SSøren Schmidt switch (args->what) { 2694c21dee17SSøren Schmidt case LINUX_SOCKET: 2695b40ce416SJulian Elischer return (linux_socket(td, arg)); 2696c21dee17SSøren Schmidt case LINUX_BIND: 2697b40ce416SJulian Elischer return (linux_bind(td, arg)); 2698c21dee17SSøren Schmidt case LINUX_CONNECT: 2699b40ce416SJulian Elischer return (linux_connect(td, arg)); 2700c21dee17SSøren Schmidt case LINUX_LISTEN: 2701b40ce416SJulian Elischer return (linux_listen(td, arg)); 2702c21dee17SSøren Schmidt case LINUX_ACCEPT: 2703b40ce416SJulian Elischer return (linux_accept(td, arg)); 2704c21dee17SSøren Schmidt case LINUX_GETSOCKNAME: 2705b40ce416SJulian Elischer return (linux_getsockname(td, arg)); 2706c21dee17SSøren Schmidt case LINUX_GETPEERNAME: 2707b40ce416SJulian Elischer return (linux_getpeername(td, arg)); 2708c21dee17SSøren Schmidt case LINUX_SOCKETPAIR: 2709b40ce416SJulian Elischer return (linux_socketpair(td, arg)); 2710c21dee17SSøren Schmidt case LINUX_SEND: 2711b40ce416SJulian Elischer return (linux_send(td, arg)); 2712c21dee17SSøren Schmidt case LINUX_RECV: 2713b40ce416SJulian Elischer return (linux_recv(td, arg)); 2714c21dee17SSøren Schmidt case LINUX_SENDTO: 2715b40ce416SJulian Elischer return (linux_sendto(td, arg)); 2716c21dee17SSøren Schmidt case LINUX_RECVFROM: 2717b40ce416SJulian Elischer return (linux_recvfrom(td, arg)); 2718c21dee17SSøren Schmidt case LINUX_SHUTDOWN: 2719b40ce416SJulian Elischer return (linux_shutdown(td, arg)); 2720c21dee17SSøren Schmidt case LINUX_SETSOCKOPT: 2721b40ce416SJulian Elischer return (linux_setsockopt(td, arg)); 2722c21dee17SSøren Schmidt case LINUX_GETSOCKOPT: 2723b40ce416SJulian Elischer return (linux_getsockopt(td, arg)); 2724e76bba09SSøren Schmidt case LINUX_SENDMSG: 2725ca26842eSHajimu UMEMOTO return (linux_sendmsg(td, arg)); 2726e76bba09SSøren Schmidt case LINUX_RECVMSG: 2727b40ce416SJulian Elischer return (linux_recvmsg(td, arg)); 2728f8cd0af2SDmitry Chagin case LINUX_ACCEPT4: 2729f8cd0af2SDmitry Chagin return (linux_accept4(td, arg)); 2730e1ff74c0SDmitry Chagin case LINUX_RECVMMSG: 2731e1ff74c0SDmitry Chagin return (linux_recvmmsg(td, arg)); 2732e1ff74c0SDmitry Chagin case LINUX_SENDMMSG: 2733e1ff74c0SDmitry Chagin return (linux_sendmmsg(td, arg)); 2734fc7510aeSEd Maste case LINUX_SENDFILE: 2735fc7510aeSEd Maste return (linux_sendfile(td, arg)); 2736c21dee17SSøren Schmidt } 27373f3a4815SMarcel Moolenaar 2738599dadcaSEdward Tomasz Napierala linux_msg(td, "socket type %d not implemented", args->what); 27393f3a4815SMarcel Moolenaar return (ENOSYS); 2740c21dee17SSøren Schmidt } 27416ecab394SDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */ 2742