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
linux_to_bsd_sockopt_level(int level)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
bsd_to_linux_sockopt_level(int level)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
linux_to_bsd_ip_sockopt(int opt)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
linux_to_bsd_ip6_sockopt(int opt)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);
504ca63710dSLexi Winter case LINUX_IPV6_RECVERR:
505ca63710dSLexi Winter LINUX_RATELIMIT_MSG_OPT1(
506ca63710dSLexi Winter "unsupported IPv6 socket option IPV6_RECVERR (%d), you can not get extended reliability info in linux programs",
507ca63710dSLexi Winter opt);
508ca63710dSLexi Winter return (-2);
5098ec6c4a3SAlexander Leidinger
5108ec6c4a3SAlexander Leidinger /* unknown sockopts */
5118ec6c4a3SAlexander Leidinger default:
51286a9058bSAndrey V. Elsukov return (-1);
51386a9058bSAndrey V. Elsukov }
5148ec6c4a3SAlexander Leidinger }
51586a9058bSAndrey V. Elsukov
51686a9058bSAndrey V. Elsukov static int
linux_to_bsd_so_sockopt(int opt)517c21dee17SSøren Schmidt linux_to_bsd_so_sockopt(int opt)
518c21dee17SSøren Schmidt {
5193f3a4815SMarcel Moolenaar
520c21dee17SSøren Schmidt switch (opt) {
521c21dee17SSøren Schmidt case LINUX_SO_DEBUG:
5223f3a4815SMarcel Moolenaar return (SO_DEBUG);
523c21dee17SSøren Schmidt case LINUX_SO_REUSEADDR:
5243f3a4815SMarcel Moolenaar return (SO_REUSEADDR);
525c21dee17SSøren Schmidt case LINUX_SO_TYPE:
5263f3a4815SMarcel Moolenaar return (SO_TYPE);
527c21dee17SSøren Schmidt case LINUX_SO_ERROR:
5283f3a4815SMarcel Moolenaar return (SO_ERROR);
529c21dee17SSøren Schmidt case LINUX_SO_DONTROUTE:
5303f3a4815SMarcel Moolenaar return (SO_DONTROUTE);
531c21dee17SSøren Schmidt case LINUX_SO_BROADCAST:
5323f3a4815SMarcel Moolenaar return (SO_BROADCAST);
533c21dee17SSøren Schmidt case LINUX_SO_SNDBUF:
534c31a6a66SEdward Tomasz Napierala case LINUX_SO_SNDBUFFORCE:
5353f3a4815SMarcel Moolenaar return (SO_SNDBUF);
536c21dee17SSøren Schmidt case LINUX_SO_RCVBUF:
537c31a6a66SEdward Tomasz Napierala case LINUX_SO_RCVBUFFORCE:
5383f3a4815SMarcel Moolenaar return (SO_RCVBUF);
539c21dee17SSøren Schmidt case LINUX_SO_KEEPALIVE:
5403f3a4815SMarcel Moolenaar return (SO_KEEPALIVE);
541c21dee17SSøren Schmidt case LINUX_SO_OOBINLINE:
5423f3a4815SMarcel Moolenaar return (SO_OOBINLINE);
543c21dee17SSøren Schmidt case LINUX_SO_LINGER:
5443f3a4815SMarcel Moolenaar return (SO_LINGER);
5453bc69ad9SEdward Tomasz Napierala case LINUX_SO_REUSEPORT:
5463bc69ad9SEdward Tomasz Napierala return (SO_REUSEPORT_LB);
547443d8a07SConrad Meyer case LINUX_SO_PASSCRED:
548443d8a07SConrad Meyer return (LOCAL_CREDS_PERSISTENT);
549d0b2365eSKonstantin Belousov case LINUX_SO_PEERCRED:
550d0b2365eSKonstantin Belousov return (LOCAL_PEERCRED);
551d0b2365eSKonstantin Belousov case LINUX_SO_RCVLOWAT:
552d0b2365eSKonstantin Belousov return (SO_RCVLOWAT);
553d0b2365eSKonstantin Belousov case LINUX_SO_SNDLOWAT:
554d0b2365eSKonstantin Belousov return (SO_SNDLOWAT);
555d0b2365eSKonstantin Belousov case LINUX_SO_RCVTIMEO:
556d0b2365eSKonstantin Belousov return (SO_RCVTIMEO);
557d0b2365eSKonstantin Belousov case LINUX_SO_SNDTIMEO:
558d0b2365eSKonstantin Belousov return (SO_SNDTIMEO);
5590e26e54bSDmitry Chagin case LINUX_SO_TIMESTAMPO:
5600e26e54bSDmitry Chagin case LINUX_SO_TIMESTAMPN:
561d0b2365eSKonstantin Belousov return (SO_TIMESTAMP);
56271bc8bcfSDmitry Chagin case LINUX_SO_TIMESTAMPNSO:
56371bc8bcfSDmitry Chagin case LINUX_SO_TIMESTAMPNSN:
56471bc8bcfSDmitry Chagin return (SO_BINTIME);
565d0b2365eSKonstantin Belousov case LINUX_SO_ACCEPTCONN:
566d0b2365eSKonstantin Belousov return (SO_ACCEPTCONN);
5674fe5361cSEdward Tomasz Napierala case LINUX_SO_PROTOCOL:
5684fe5361cSEdward Tomasz Napierala return (SO_PROTOCOL);
569cf312f79SDmitry Chagin case LINUX_SO_DOMAIN:
570cf312f79SDmitry Chagin return (SO_DOMAIN);
571c21dee17SSøren Schmidt }
5723f3a4815SMarcel Moolenaar return (-1);
573c21dee17SSøren Schmidt }
574c21dee17SSøren Schmidt
57540dbba57SAssar Westerlund static int
linux_to_bsd_tcp_sockopt(int opt)576fb709557SJohn Baldwin linux_to_bsd_tcp_sockopt(int opt)
577fb709557SJohn Baldwin {
578fb709557SJohn Baldwin
579fb709557SJohn Baldwin switch (opt) {
580fb709557SJohn Baldwin case LINUX_TCP_NODELAY:
581fb709557SJohn Baldwin return (TCP_NODELAY);
582fb709557SJohn Baldwin case LINUX_TCP_MAXSEG:
583fb709557SJohn Baldwin return (TCP_MAXSEG);
584c2d47457SEdward Tomasz Napierala case LINUX_TCP_CORK:
585c2d47457SEdward Tomasz Napierala return (TCP_NOPUSH);
586fb709557SJohn Baldwin case LINUX_TCP_KEEPIDLE:
587fb709557SJohn Baldwin return (TCP_KEEPIDLE);
588fb709557SJohn Baldwin case LINUX_TCP_KEEPINTVL:
589fb709557SJohn Baldwin return (TCP_KEEPINTVL);
590fb709557SJohn Baldwin case LINUX_TCP_KEEPCNT:
591fb709557SJohn Baldwin return (TCP_KEEPCNT);
59299f563edSEdward Tomasz Napierala case LINUX_TCP_INFO:
59399f563edSEdward Tomasz Napierala LINUX_RATELIMIT_MSG_OPT1(
59499f563edSEdward Tomasz Napierala "unsupported TCP socket option TCP_INFO (%d)", opt);
59599f563edSEdward Tomasz Napierala return (-2);
596fb709557SJohn Baldwin case LINUX_TCP_MD5SIG:
597fb709557SJohn Baldwin return (TCP_MD5SIG);
598fb709557SJohn Baldwin }
599fb709557SJohn Baldwin return (-1);
600fb709557SJohn Baldwin }
601fb709557SJohn Baldwin
602fb709557SJohn Baldwin static int
linux_to_bsd_msg_flags(int flags)60340dbba57SAssar Westerlund linux_to_bsd_msg_flags(int flags)
60440dbba57SAssar Westerlund {
60540dbba57SAssar Westerlund int ret_flags = 0;
60640dbba57SAssar Westerlund
60740dbba57SAssar Westerlund if (flags & LINUX_MSG_OOB)
60840dbba57SAssar Westerlund ret_flags |= MSG_OOB;
60940dbba57SAssar Westerlund if (flags & LINUX_MSG_PEEK)
61040dbba57SAssar Westerlund ret_flags |= MSG_PEEK;
61140dbba57SAssar Westerlund if (flags & LINUX_MSG_DONTROUTE)
61240dbba57SAssar Westerlund ret_flags |= MSG_DONTROUTE;
61340dbba57SAssar Westerlund if (flags & LINUX_MSG_CTRUNC)
61440dbba57SAssar Westerlund ret_flags |= MSG_CTRUNC;
61540dbba57SAssar Westerlund if (flags & LINUX_MSG_TRUNC)
61640dbba57SAssar Westerlund ret_flags |= MSG_TRUNC;
61740dbba57SAssar Westerlund if (flags & LINUX_MSG_DONTWAIT)
61840dbba57SAssar Westerlund ret_flags |= MSG_DONTWAIT;
61940dbba57SAssar Westerlund if (flags & LINUX_MSG_EOR)
62040dbba57SAssar Westerlund ret_flags |= MSG_EOR;
62140dbba57SAssar Westerlund if (flags & LINUX_MSG_WAITALL)
62240dbba57SAssar Westerlund ret_flags |= MSG_WAITALL;
6238d6e40c3SMaxim Sobolev if (flags & LINUX_MSG_NOSIGNAL)
6248d6e40c3SMaxim Sobolev ret_flags |= MSG_NOSIGNAL;
62540dbba57SAssar Westerlund if (flags & LINUX_MSG_PROXY)
6268ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_PROXY (%d) not handled",
6278ec6c4a3SAlexander Leidinger LINUX_MSG_PROXY);
62840dbba57SAssar Westerlund if (flags & LINUX_MSG_FIN)
6298ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_FIN (%d) not handled",
6308ec6c4a3SAlexander Leidinger LINUX_MSG_FIN);
63140dbba57SAssar Westerlund if (flags & LINUX_MSG_SYN)
6328ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_SYN (%d) not handled",
6338ec6c4a3SAlexander Leidinger LINUX_MSG_SYN);
63440dbba57SAssar Westerlund if (flags & LINUX_MSG_CONFIRM)
6358ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_CONFIRM (%d) not handled",
6368ec6c4a3SAlexander Leidinger LINUX_MSG_CONFIRM);
63740dbba57SAssar Westerlund if (flags & LINUX_MSG_RST)
6388ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_RST (%d) not handled",
6398ec6c4a3SAlexander Leidinger LINUX_MSG_RST);
64040dbba57SAssar Westerlund if (flags & LINUX_MSG_ERRQUEUE)
6418ec6c4a3SAlexander Leidinger LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_ERRQUEUE (%d) not handled",
6428ec6c4a3SAlexander Leidinger LINUX_MSG_ERRQUEUE);
643e667ee63SDmitry Chagin return (ret_flags);
64440dbba57SAssar Westerlund }
64540dbba57SAssar Westerlund
6465a8a13e0SDavid Malone static int
linux_to_bsd_cmsg_type(int cmsg_type)64774f5d680SKonstantin Belousov linux_to_bsd_cmsg_type(int cmsg_type)
6485a8a13e0SDavid Malone {
64974f5d680SKonstantin Belousov
65074f5d680SKonstantin Belousov switch (cmsg_type) {
65174f5d680SKonstantin Belousov case LINUX_SCM_RIGHTS:
65274f5d680SKonstantin Belousov return (SCM_RIGHTS);
653605da56bSAndriy Gapon case LINUX_SCM_CREDENTIALS:
654605da56bSAndriy Gapon return (SCM_CREDS);
65574f5d680SKonstantin Belousov }
65674f5d680SKonstantin Belousov return (-1);
65774f5d680SKonstantin Belousov }
65874f5d680SKonstantin Belousov
65974f5d680SKonstantin Belousov static int
bsd_to_linux_ip_cmsg_type(int cmsg_type)660f8a66150SDmitry Chagin bsd_to_linux_ip_cmsg_type(int cmsg_type)
661f8a66150SDmitry Chagin {
662f8a66150SDmitry Chagin
663f8a66150SDmitry Chagin switch (cmsg_type) {
664f8a66150SDmitry Chagin case IP_RECVORIGDSTADDR:
665f8a66150SDmitry Chagin return (LINUX_IP_RECVORIGDSTADDR);
666f8a66150SDmitry Chagin }
667f8a66150SDmitry Chagin return (-1);
668f8a66150SDmitry Chagin }
669f8a66150SDmitry Chagin
670f8a66150SDmitry Chagin static int
bsd_to_linux_cmsg_type(struct proc * p,int cmsg_type,int cmsg_level)671f8a66150SDmitry Chagin bsd_to_linux_cmsg_type(struct proc *p, int cmsg_type, int cmsg_level)
67274f5d680SKonstantin Belousov {
6730e26e54bSDmitry Chagin struct linux_pemuldata *pem;
6740e26e54bSDmitry Chagin
675f8a66150SDmitry Chagin if (cmsg_level == IPPROTO_IP)
676f8a66150SDmitry Chagin return (bsd_to_linux_ip_cmsg_type(cmsg_type));
677f8a66150SDmitry Chagin if (cmsg_level != SOL_SOCKET)
678f8a66150SDmitry Chagin return (-1);
679f8a66150SDmitry Chagin
6800e26e54bSDmitry Chagin pem = pem_find(p);
68174f5d680SKonstantin Belousov
68274f5d680SKonstantin Belousov switch (cmsg_type) {
68374f5d680SKonstantin Belousov case SCM_RIGHTS:
68474f5d680SKonstantin Belousov return (LINUX_SCM_RIGHTS);
685605da56bSAndriy Gapon case SCM_CREDS:
686605da56bSAndriy Gapon return (LINUX_SCM_CREDENTIALS);
687ede4af47SConrad Meyer case SCM_CREDS2:
688ede4af47SConrad Meyer return (LINUX_SCM_CREDENTIALS);
689bbf392d5SDmitry Chagin case SCM_TIMESTAMP:
6900e26e54bSDmitry Chagin return (pem->so_timestamp);
69171bc8bcfSDmitry Chagin case SCM_BINTIME:
69271bc8bcfSDmitry Chagin return (pem->so_timestampns);
69374f5d680SKonstantin Belousov }
69474f5d680SKonstantin Belousov return (-1);
69574f5d680SKonstantin Belousov }
69674f5d680SKonstantin Belousov
69774f5d680SKonstantin Belousov static int
linux_to_bsd_msghdr(struct msghdr * bhdr,const struct l_msghdr * lhdr)69874f5d680SKonstantin Belousov linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
69974f5d680SKonstantin Belousov {
70074f5d680SKonstantin Belousov if (lhdr->msg_controllen > INT_MAX)
70174f5d680SKonstantin Belousov return (ENOBUFS);
70274f5d680SKonstantin Belousov
70374f5d680SKonstantin Belousov bhdr->msg_name = PTRIN(lhdr->msg_name);
70474f5d680SKonstantin Belousov bhdr->msg_namelen = lhdr->msg_namelen;
70574f5d680SKonstantin Belousov bhdr->msg_iov = PTRIN(lhdr->msg_iov);
70674f5d680SKonstantin Belousov bhdr->msg_iovlen = lhdr->msg_iovlen;
70774f5d680SKonstantin Belousov bhdr->msg_control = PTRIN(lhdr->msg_control);
708605da56bSAndriy Gapon
709605da56bSAndriy Gapon /*
710605da56bSAndriy Gapon * msg_controllen is skipped since BSD and LINUX control messages
711605da56bSAndriy Gapon * are potentially different sizes (e.g. the cred structure used
712605da56bSAndriy Gapon * by SCM_CREDS is different between the two operating system).
713605da56bSAndriy Gapon *
714605da56bSAndriy Gapon * The caller can set it (if necessary) after converting all the
715605da56bSAndriy Gapon * control messages.
716605da56bSAndriy Gapon */
717605da56bSAndriy Gapon
71874f5d680SKonstantin Belousov bhdr->msg_flags = linux_to_bsd_msg_flags(lhdr->msg_flags);
71974f5d680SKonstantin Belousov return (0);
72074f5d680SKonstantin Belousov }
72174f5d680SKonstantin Belousov
72274f5d680SKonstantin Belousov static int
bsd_to_linux_msghdr(const struct msghdr * bhdr,struct l_msghdr * lhdr)72374f5d680SKonstantin Belousov bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
72474f5d680SKonstantin Belousov {
72574f5d680SKonstantin Belousov lhdr->msg_name = PTROUT(bhdr->msg_name);
72674f5d680SKonstantin Belousov lhdr->msg_namelen = bhdr->msg_namelen;
72774f5d680SKonstantin Belousov lhdr->msg_iov = PTROUT(bhdr->msg_iov);
72874f5d680SKonstantin Belousov lhdr->msg_iovlen = bhdr->msg_iovlen;
72974f5d680SKonstantin Belousov lhdr->msg_control = PTROUT(bhdr->msg_control);
730605da56bSAndriy Gapon
731605da56bSAndriy Gapon /*
732605da56bSAndriy Gapon * msg_controllen is skipped since BSD and LINUX control messages
733605da56bSAndriy Gapon * are potentially different sizes (e.g. the cred structure used
734605da56bSAndriy Gapon * by SCM_CREDS is different between the two operating system).
735605da56bSAndriy Gapon *
736605da56bSAndriy Gapon * The caller can set it (if necessary) after converting all the
737605da56bSAndriy Gapon * control messages.
738605da56bSAndriy Gapon */
739605da56bSAndriy Gapon
74074f5d680SKonstantin Belousov /* msg_flags skipped */
74174f5d680SKonstantin Belousov return (0);
74274f5d680SKonstantin Belousov }
74374f5d680SKonstantin Belousov
74474f5d680SKonstantin Belousov static int
linux_set_socket_flags(int lflags,int * flags)7454cf10e29SDmitry Chagin linux_set_socket_flags(int lflags, int *flags)
74638a18e97SDmitry Chagin {
74738a18e97SDmitry Chagin
7484cf10e29SDmitry Chagin if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
7494cf10e29SDmitry Chagin return (EINVAL);
7504cf10e29SDmitry Chagin if (lflags & LINUX_SOCK_NONBLOCK)
7514cf10e29SDmitry Chagin *flags |= SOCK_NONBLOCK;
7524cf10e29SDmitry Chagin if (lflags & LINUX_SOCK_CLOEXEC)
7534cf10e29SDmitry Chagin *flags |= SOCK_CLOEXEC;
75438a18e97SDmitry Chagin return (0);
75538a18e97SDmitry Chagin }
75638a18e97SDmitry Chagin
75738a18e97SDmitry Chagin static int
linux_copyout_sockaddr(const struct sockaddr * sa,void * uaddr,size_t len)758106a784bSEdward Tomasz Napierala linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
759106a784bSEdward Tomasz Napierala {
760106a784bSEdward Tomasz Napierala struct l_sockaddr *lsa;
761106a784bSEdward Tomasz Napierala int error;
762106a784bSEdward Tomasz Napierala
763106a784bSEdward Tomasz Napierala error = bsd_to_linux_sockaddr(sa, &lsa, len);
764106a784bSEdward Tomasz Napierala if (error != 0)
765106a784bSEdward Tomasz Napierala return (error);
766106a784bSEdward Tomasz Napierala
767106a784bSEdward Tomasz Napierala error = copyout(lsa, uaddr, len);
76863355839SDmitry Chagin free(lsa, M_LINUX);
769106a784bSEdward Tomasz Napierala
770106a784bSEdward Tomasz Napierala return (error);
771106a784bSEdward Tomasz Napierala }
772106a784bSEdward Tomasz Napierala
773106a784bSEdward Tomasz Napierala static int
linux_sendit(struct thread * td,int s,struct msghdr * mp,int flags,struct mbuf * control,enum uio_seg segflg)77474f5d680SKonstantin Belousov linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
77574f5d680SKonstantin Belousov struct mbuf *control, enum uio_seg segflg)
77674f5d680SKonstantin Belousov {
7775a8a13e0SDavid Malone struct sockaddr *to;
778d5368bf3SDmitry Chagin int error, len;
7795a8a13e0SDavid Malone
7805a8a13e0SDavid Malone if (mp->msg_name != NULL) {
781d5368bf3SDmitry Chagin len = mp->msg_namelen;
782d5368bf3SDmitry Chagin error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
783dddb7e7fSDmitry Chagin if (error != 0)
7845a8a13e0SDavid Malone return (error);
7855a8a13e0SDavid Malone mp->msg_name = to;
7865a8a13e0SDavid Malone } else
7875a8a13e0SDavid Malone to = NULL;
7885a8a13e0SDavid Malone
789a6886ef1SMaxim Sobolev error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
790a6886ef1SMaxim Sobolev segflg);
7915a8a13e0SDavid Malone
7925a8a13e0SDavid Malone if (to)
7931ede983cSDag-Erling Smørgrav free(to, M_SONAME);
7945a8a13e0SDavid Malone return (error);
7955a8a13e0SDavid Malone }
7965a8a13e0SDavid Malone
7973f3a4815SMarcel Moolenaar /* Return 0 if IP_HDRINCL is set for the given socket. */
798f2477ae1SMike Smith static int
linux_check_hdrincl(struct thread * td,int s)799e140eb43SDavid Malone linux_check_hdrincl(struct thread *td, int s)
800f2477ae1SMike Smith {
801857ad5a3SDmitry Chagin int error, optval;
802857ad5a3SDmitry Chagin socklen_t size_val;
803f2477ae1SMike Smith
804e140eb43SDavid Malone size_val = sizeof(optval);
805e140eb43SDavid Malone error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
806e140eb43SDavid Malone &optval, UIO_SYSSPACE, &size_val);
807dddb7e7fSDmitry Chagin if (error != 0)
8083f3a4815SMarcel Moolenaar return (error);
8093f3a4815SMarcel Moolenaar
8103f3a4815SMarcel Moolenaar return (optval == 0);
811f2477ae1SMike Smith }
812f2477ae1SMike Smith
813f2477ae1SMike Smith /*
814f2477ae1SMike Smith * Updated sendto() when IP_HDRINCL is set:
815f2477ae1SMike Smith * tweak endian-dependent fields in the IP packet.
816f2477ae1SMike Smith */
817f2477ae1SMike Smith static int
linux_sendto_hdrincl(struct thread * td,struct linux_sendto_args * linux_args)81838da2381SRobert Watson linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
819f2477ae1SMike Smith {
820f2477ae1SMike Smith /*
821f2477ae1SMike Smith * linux_ip_copysize defines how many bytes we should copy
822f2477ae1SMike Smith * from the beginning of the IP packet before we customize it for BSD.
823a6886ef1SMaxim Sobolev * It should include all the fields we modify (ip_len and ip_off).
824f2477ae1SMike Smith */
825f2477ae1SMike Smith #define linux_ip_copysize 8
826f2477ae1SMike Smith
827f2477ae1SMike Smith struct ip *packet;
8285a8a13e0SDavid Malone struct msghdr msg;
829a6886ef1SMaxim Sobolev struct iovec aiov[1];
830f2477ae1SMike Smith int error;
831f2477ae1SMike Smith
832aa675b57SDavid Schultz /* Check that the packet isn't too big or too small. */
833aa675b57SDavid Schultz if (linux_args->len < linux_ip_copysize ||
834aa675b57SDavid Schultz linux_args->len > IP_MAXPACKET)
8353f3a4815SMarcel Moolenaar return (EINVAL);
836f2477ae1SMike Smith
837e0d3ea8cSDmitry Chagin packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
838f2477ae1SMike Smith
839a6886ef1SMaxim Sobolev /* Make kernel copy of the packet to be sent */
8404af27623STim J. Robbins if ((error = copyin(PTRIN(linux_args->msg), packet,
841a6886ef1SMaxim Sobolev linux_args->len)))
842a6886ef1SMaxim Sobolev goto goout;
843f2477ae1SMike Smith
844f2477ae1SMike Smith /* Convert fields from Linux to BSD raw IP socket format */
8455a8a13e0SDavid Malone packet->ip_len = linux_args->len;
846f2477ae1SMike Smith packet->ip_off = ntohs(packet->ip_off);
847f2477ae1SMike Smith
848f2477ae1SMike Smith /* Prepare the msghdr and iovec structures describing the new packet */
8494af27623STim J. Robbins msg.msg_name = PTRIN(linux_args->to);
8505a8a13e0SDavid Malone msg.msg_namelen = linux_args->tolen;
8515a8a13e0SDavid Malone msg.msg_iov = aiov;
852a6886ef1SMaxim Sobolev msg.msg_iovlen = 1;
8535a8a13e0SDavid Malone msg.msg_control = NULL;
8545a8a13e0SDavid Malone msg.msg_flags = 0;
8555a8a13e0SDavid Malone aiov[0].iov_base = (char *)packet;
856a6886ef1SMaxim Sobolev aiov[0].iov_len = linux_args->len;
857a6886ef1SMaxim Sobolev error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
85874f5d680SKonstantin Belousov NULL, UIO_SYSSPACE);
859a6886ef1SMaxim Sobolev goout:
860e0d3ea8cSDmitry Chagin free(packet, M_LINUX);
8615a8a13e0SDavid Malone return (error);
862f2477ae1SMike Smith }
863f2477ae1SMike Smith
8649e47480eSConrad Meyer static const char *linux_netlink_names[] = {
8659e47480eSConrad Meyer [LINUX_NETLINK_ROUTE] = "ROUTE",
8669e47480eSConrad Meyer [LINUX_NETLINK_SOCK_DIAG] = "SOCK_DIAG",
8679e47480eSConrad Meyer [LINUX_NETLINK_NFLOG] = "NFLOG",
8689e47480eSConrad Meyer [LINUX_NETLINK_SELINUX] = "SELINUX",
8699e47480eSConrad Meyer [LINUX_NETLINK_AUDIT] = "AUDIT",
8709e47480eSConrad Meyer [LINUX_NETLINK_FIB_LOOKUP] = "FIB_LOOKUP",
8719e47480eSConrad Meyer [LINUX_NETLINK_NETFILTER] = "NETFILTER",
8729e47480eSConrad Meyer [LINUX_NETLINK_KOBJECT_UEVENT] = "KOBJECT_UEVENT",
8739e47480eSConrad Meyer };
8749e47480eSConrad Meyer
875a12b9b3dSDmitry Chagin int
linux_socket(struct thread * td,struct linux_socket_args * args)876b40ce416SJulian Elischer linux_socket(struct thread *td, struct linux_socket_args *args)
877c21dee17SSøren Schmidt {
8789d4a08d1SGleb Smirnoff int retval_socket, type;
8799d4a08d1SGleb Smirnoff sa_family_t domain;
880c21dee17SSøren Schmidt
881d293f35cSEdward Tomasz Napierala type = args->type & LINUX_SOCK_TYPE_MASK;
882d293f35cSEdward Tomasz Napierala if (type < 0 || type > LINUX_SOCK_MAX)
883eeb63e51SDmitry Chagin return (EINVAL);
8844cf10e29SDmitry Chagin retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
885d293f35cSEdward Tomasz Napierala &type);
8864cf10e29SDmitry Chagin if (retval_socket != 0)
8874cf10e29SDmitry Chagin return (retval_socket);
888d293f35cSEdward Tomasz Napierala domain = linux_to_bsd_domain(args->domain);
8899d4a08d1SGleb Smirnoff if (domain == AF_UNKNOWN) {
8909e47480eSConrad Meyer /* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
8919e47480eSConrad Meyer type = args->type & LINUX_SOCK_TYPE_MASK;
892feb96ee9SEdward Tomasz Napierala if (args->domain == LINUX_AF_NETLINK &&
893feb96ee9SEdward Tomasz Napierala args->protocol == LINUX_NETLINK_AUDIT) {
894feb96ee9SEdward Tomasz Napierala ; /* Do nothing, quietly. */
895feb96ee9SEdward Tomasz Napierala } else if (args->domain == LINUX_AF_NETLINK) {
8969e47480eSConrad Meyer const char *nl_name;
897b3be0b4dSEdward Tomasz Napierala
8989e47480eSConrad Meyer if (args->protocol >= 0 &&
8999e47480eSConrad Meyer args->protocol < nitems(linux_netlink_names))
9009e47480eSConrad Meyer nl_name = linux_netlink_names[args->protocol];
9019e47480eSConrad Meyer else
9029e47480eSConrad Meyer nl_name = NULL;
9039e47480eSConrad Meyer if (nl_name != NULL)
904b3be0b4dSEdward Tomasz Napierala linux_msg(curthread,
9059e47480eSConrad Meyer "unsupported socket(AF_NETLINK, %d, "
9069e47480eSConrad Meyer "NETLINK_%s)", type, nl_name);
9079e47480eSConrad Meyer else
9089e47480eSConrad Meyer linux_msg(curthread,
9099e47480eSConrad Meyer "unsupported socket(AF_NETLINK, %d, %d)",
9109e47480eSConrad Meyer type, args->protocol);
9119e47480eSConrad Meyer } else {
9129e47480eSConrad Meyer linux_msg(curthread, "unsupported socket domain %d, "
9139e47480eSConrad Meyer "type %d, protocol %d", args->domain, type,
9149e47480eSConrad Meyer args->protocol);
915b3be0b4dSEdward Tomasz Napierala }
916d9b063ccSDmitry Chagin return (EAFNOSUPPORT);
917f4d91df5SEdward Tomasz Napierala }
918f2477ae1SMike Smith
919d293f35cSEdward Tomasz Napierala retval_socket = kern_socket(td, domain, type, args->protocol);
9206994ea54SDmitry Chagin if (retval_socket)
9216994ea54SDmitry Chagin return (retval_socket);
9226994ea54SDmitry Chagin
923d293f35cSEdward Tomasz Napierala if (type == SOCK_RAW
924d293f35cSEdward Tomasz Napierala && (args->protocol == IPPROTO_RAW || args->protocol == 0)
925d293f35cSEdward Tomasz Napierala && domain == PF_INET) {
926f2477ae1SMike Smith /* It's a raw IP socket: set the IP_HDRINCL option. */
927e140eb43SDavid Malone int hdrincl;
928f2477ae1SMike Smith
929e140eb43SDavid Malone hdrincl = 1;
930e140eb43SDavid Malone /* We ignore any error returned by kern_setsockopt() */
931e140eb43SDavid Malone kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
932e140eb43SDavid Malone &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
933f2477ae1SMike Smith }
934ca26842eSHajimu UMEMOTO #ifdef INET6
935ca26842eSHajimu UMEMOTO /*
936d97bee3eSBjoern A. Zeeb * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
937d97bee3eSBjoern A. Zeeb * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
938d97bee3eSBjoern A. Zeeb * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
939d97bee3eSBjoern A. Zeeb * sysctl value.
940ca26842eSHajimu UMEMOTO */
941d293f35cSEdward Tomasz Napierala if (domain == PF_INET6) {
942e140eb43SDavid Malone int v6only;
943ca26842eSHajimu UMEMOTO
944e140eb43SDavid Malone v6only = 0;
945ca26842eSHajimu UMEMOTO /* We ignore any error returned by setsockopt() */
946e140eb43SDavid Malone kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
947e140eb43SDavid Malone &v6only, UIO_SYSSPACE, sizeof(v6only));
948ca26842eSHajimu UMEMOTO }
949ca26842eSHajimu UMEMOTO #endif
9503f3a4815SMarcel Moolenaar
9513f3a4815SMarcel Moolenaar return (retval_socket);
952c21dee17SSøren Schmidt }
953c21dee17SSøren Schmidt
954a12b9b3dSDmitry Chagin int
linux_bind(struct thread * td,struct linux_bind_args * args)955b40ce416SJulian Elischer linux_bind(struct thread *td, struct linux_bind_args *args)
956c21dee17SSøren Schmidt {
957ca26842eSHajimu UMEMOTO struct sockaddr *sa;
958c21dee17SSøren Schmidt int error;
959c21dee17SSøren Schmidt
960d5368bf3SDmitry Chagin error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
961d5368bf3SDmitry Chagin &args->namelen);
962dddb7e7fSDmitry Chagin if (error != 0)
963ca26842eSHajimu UMEMOTO return (error);
964ca26842eSHajimu UMEMOTO
9656e646651SKonstantin Belousov error = kern_bindat(td, AT_FDCWD, args->s, sa);
966b33887eaSJohn Baldwin free(sa, M_SONAME);
967d5368bf3SDmitry Chagin
968d5368bf3SDmitry Chagin /* XXX */
969745aaef5SKonstantin Belousov if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
970d4b7423fSAlexander Leidinger return (EINVAL);
971b33887eaSJohn Baldwin return (error);
972c21dee17SSøren Schmidt }
973c21dee17SSøren Schmidt
974930a65feSAndrew Gallatin int
linux_connect(struct thread * td,struct linux_connect_args * args)975b40ce416SJulian Elischer linux_connect(struct thread *td, struct linux_connect_args *args)
976c21dee17SSøren Schmidt {
9770bf301c0SJonathan Lemon struct socket *so;
978ca26842eSHajimu UMEMOTO struct sockaddr *sa;
97995653579SGleb Smirnoff struct file *fp;
980c21dee17SSøren Schmidt int error;
981c21dee17SSøren Schmidt
982d5368bf3SDmitry Chagin error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
983d5368bf3SDmitry Chagin &args->namelen);
984dddb7e7fSDmitry Chagin if (error != 0)
985ca26842eSHajimu UMEMOTO return (error);
986ca26842eSHajimu UMEMOTO
9876e646651SKonstantin Belousov error = kern_connectat(td, AT_FDCWD, args->s, sa);
988b33887eaSJohn Baldwin free(sa, M_SONAME);
9890bf301c0SJonathan Lemon if (error != EISCONN)
9900bf301c0SJonathan Lemon return (error);
9910bf301c0SJonathan Lemon
992dad3b88aSMike Smith /*
993dad3b88aSMike Smith * Linux doesn't return EISCONN the first time it occurs,
994dad3b88aSMike Smith * when on a non-blocking socket. Instead it returns the
995dad3b88aSMike Smith * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
996dad3b88aSMike Smith */
9973212ad15SMateusz Guzik error = getsock(td, args->s, &cap_connect_rights, &fp);
99895653579SGleb Smirnoff if (error != 0)
99995653579SGleb Smirnoff return (error);
100095653579SGleb Smirnoff
10010bf301c0SJonathan Lemon error = EISCONN;
100295653579SGleb Smirnoff so = fp->f_data;
10033212ad15SMateusz Guzik if (atomic_load_int(&fp->f_flag) & FNONBLOCK) {
10044641373fSJohn Baldwin SOCK_LOCK(so);
10055002a60fSMarcel Moolenaar if (so->so_emuldata == 0)
10060bf301c0SJonathan Lemon error = so->so_error;
10070bf301c0SJonathan Lemon so->so_emuldata = (void *)1;
10084641373fSJohn Baldwin SOCK_UNLOCK(so);
1009dad3b88aSMike Smith }
101095653579SGleb Smirnoff fdrop(fp, td);
101195653579SGleb Smirnoff
10123f3a4815SMarcel Moolenaar return (error);
1013c21dee17SSøren Schmidt }
1014c21dee17SSøren Schmidt
1015a12b9b3dSDmitry Chagin int
linux_listen(struct thread * td,struct linux_listen_args * args)1016b40ce416SJulian Elischer linux_listen(struct thread *td, struct linux_listen_args *args)
1017c21dee17SSøren Schmidt {
1018c21dee17SSøren Schmidt
1019d293f35cSEdward Tomasz Napierala return (kern_listen(td, args->s, args->backlog));
1020c21dee17SSøren Schmidt }
1021c21dee17SSøren Schmidt
102201e0ffbaSAlexander Leidinger static int
linux_accept_common(struct thread * td,int s,l_uintptr_t addr,l_uintptr_t namelen,int flags)1023c8f37d61SDmitry Chagin linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
1024f83427b8SDmitry Chagin l_uintptr_t namelen, int flags)
1025c21dee17SSøren Schmidt {
1026cfb1e929SGleb Smirnoff struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
10276d76adbbSEdward Tomasz Napierala struct file *fp, *fp1;
1028d5368bf3SDmitry Chagin struct socket *so;
1029cfb1e929SGleb Smirnoff socklen_t len;
1030cfb1e929SGleb Smirnoff int bflags, error, error1;
103193e694c9SDmitry Chagin
1032d5368bf3SDmitry Chagin bflags = 0;
10336d76adbbSEdward Tomasz Napierala fp = NULL;
10346d76adbbSEdward Tomasz Napierala
1035d5368bf3SDmitry Chagin error = linux_set_socket_flags(flags, &bflags);
10364cf10e29SDmitry Chagin if (error != 0)
10374cf10e29SDmitry Chagin return (error);
1038d5368bf3SDmitry Chagin
1039cfb1e929SGleb Smirnoff if (PTRIN(addr) != NULL) {
1040d5368bf3SDmitry Chagin error = copyin(PTRIN(namelen), &len, sizeof(len));
1041d5368bf3SDmitry Chagin if (error != 0)
1042d5368bf3SDmitry Chagin return (error);
1043d5368bf3SDmitry Chagin if (len < 0)
1044d4b7423fSAlexander Leidinger return (EINVAL);
1045cfb1e929SGleb Smirnoff } else
1046cfb1e929SGleb Smirnoff len = 0;
1047cfb1e929SGleb Smirnoff
1048cfb1e929SGleb Smirnoff error = kern_accept4(td, s, (struct sockaddr *)&ss, bflags, &fp);
1049d5368bf3SDmitry Chagin
10506d76adbbSEdward Tomasz Napierala /*
10516d76adbbSEdward Tomasz Napierala * Translate errno values into ones used by Linux.
10526d76adbbSEdward Tomasz Napierala */
1053d5368bf3SDmitry Chagin if (error != 0) {
1054d5368bf3SDmitry Chagin /*
1055d5368bf3SDmitry Chagin * XXX. This is wrong, different sockaddr structures
1056d5368bf3SDmitry Chagin * have different sizes.
1057d5368bf3SDmitry Chagin */
10586d76adbbSEdward Tomasz Napierala switch (error) {
10596d76adbbSEdward Tomasz Napierala case EFAULT:
10606d76adbbSEdward Tomasz Napierala if (namelen != sizeof(struct sockaddr_in))
1061d5368bf3SDmitry Chagin error = EINVAL;
10626d76adbbSEdward Tomasz Napierala break;
10636d76adbbSEdward Tomasz Napierala case EINVAL:
10643212ad15SMateusz Guzik error1 = getsock(td, s, &cap_accept_rights, &fp1);
1065d5368bf3SDmitry Chagin if (error1 != 0) {
1066d5368bf3SDmitry Chagin error = error1;
10676d76adbbSEdward Tomasz Napierala break;
1068d5368bf3SDmitry Chagin }
10696d76adbbSEdward Tomasz Napierala so = fp1->f_data;
1070d5368bf3SDmitry Chagin if (so->so_type == SOCK_DGRAM)
1071d5368bf3SDmitry Chagin error = EOPNOTSUPP;
10726d76adbbSEdward Tomasz Napierala fdrop(fp1, td);
10736d76adbbSEdward Tomasz Napierala break;
107491f514e4SDmitry Chagin }
10756d76adbbSEdward Tomasz Napierala return (error);
1076d4b7423fSAlexander Leidinger }
1077d5368bf3SDmitry Chagin
1078cfb1e929SGleb Smirnoff if (PTRIN(addr) != NULL) {
1079cfb1e929SGleb Smirnoff len = min(ss.ss_len, len);
1080cfb1e929SGleb Smirnoff error = linux_copyout_sockaddr((struct sockaddr *)&ss,
1081cfb1e929SGleb Smirnoff PTRIN(addr), len);
1082cfb1e929SGleb Smirnoff if (error == 0) {
1083cfb1e929SGleb Smirnoff len = ss.ss_len;
1084cfb1e929SGleb Smirnoff error = copyout(&len, PTRIN(namelen), sizeof(len));
1085cfb1e929SGleb Smirnoff }
1086dddb7e7fSDmitry Chagin if (error != 0) {
10876d76adbbSEdward Tomasz Napierala fdclose(td, fp, td->td_retval[0]);
108893e694c9SDmitry Chagin td->td_retval[0] = 0;
108993e694c9SDmitry Chagin }
10906d76adbbSEdward Tomasz Napierala }
10916d76adbbSEdward Tomasz Napierala if (fp != NULL)
10926d76adbbSEdward Tomasz Napierala fdrop(fp, td);
109393e694c9SDmitry Chagin return (error);
1094c21dee17SSøren Schmidt }
1095c21dee17SSøren Schmidt
1096a12b9b3dSDmitry Chagin int
linux_accept(struct thread * td,struct linux_accept_args * args)1097c8f37d61SDmitry Chagin linux_accept(struct thread *td, struct linux_accept_args *args)
1098c8f37d61SDmitry Chagin {
1099c8f37d61SDmitry Chagin
1100c8f37d61SDmitry Chagin return (linux_accept_common(td, args->s, args->addr,
1101f83427b8SDmitry Chagin args->namelen, 0));
1102c8f37d61SDmitry Chagin }
1103c8f37d61SDmitry Chagin
1104a12b9b3dSDmitry Chagin int
linux_accept4(struct thread * td,struct linux_accept4_args * args)1105f8cd0af2SDmitry Chagin linux_accept4(struct thread *td, struct linux_accept4_args *args)
1106f8cd0af2SDmitry Chagin {
1107f8cd0af2SDmitry Chagin
1108f8cd0af2SDmitry Chagin return (linux_accept_common(td, args->s, args->addr,
1109f8cd0af2SDmitry Chagin args->namelen, args->flags));
1110f8cd0af2SDmitry Chagin }
1111f8cd0af2SDmitry Chagin
1112a12b9b3dSDmitry Chagin int
linux_getsockname(struct thread * td,struct linux_getsockname_args * args)1113b40ce416SJulian Elischer linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
1114c21dee17SSøren Schmidt {
11150fac350cSGleb Smirnoff struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
11160fac350cSGleb Smirnoff socklen_t len;
11170fac350cSGleb Smirnoff int error;
1118c21dee17SSøren Schmidt
1119d5368bf3SDmitry Chagin error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1120dddb7e7fSDmitry Chagin if (error != 0)
1121ca26842eSHajimu UMEMOTO return (error);
1122d5368bf3SDmitry Chagin
11230fac350cSGleb Smirnoff error = kern_getsockname(td, args->s, (struct sockaddr *)&ss);
1124d5368bf3SDmitry Chagin if (error != 0)
1125d5368bf3SDmitry Chagin return (error);
1126d5368bf3SDmitry Chagin
11270fac350cSGleb Smirnoff len = min(ss.ss_len, len);
11280fac350cSGleb Smirnoff error = linux_copyout_sockaddr((struct sockaddr *)&ss,
11290fac350cSGleb Smirnoff PTRIN(args->addr), len);
11300fac350cSGleb Smirnoff if (error == 0) {
11310fac350cSGleb Smirnoff len = ss.ss_len;
1132d5368bf3SDmitry Chagin error = copyout(&len, PTRIN(args->namelen), sizeof(len));
11330fac350cSGleb Smirnoff }
1134d5368bf3SDmitry Chagin return (error);
1135c21dee17SSøren Schmidt }
1136c21dee17SSøren Schmidt
1137a12b9b3dSDmitry Chagin int
linux_getpeername(struct thread * td,struct linux_getpeername_args * args)1138b40ce416SJulian Elischer linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
1139c21dee17SSøren Schmidt {
11400fac350cSGleb Smirnoff struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
11410fac350cSGleb Smirnoff socklen_t len;
11420fac350cSGleb Smirnoff int error;
1143c21dee17SSøren Schmidt
1144d5368bf3SDmitry Chagin error = copyin(PTRIN(args->namelen), &len, sizeof(len));
1145dddb7e7fSDmitry Chagin if (error != 0)
1146ca26842eSHajimu UMEMOTO return (error);
1147d5368bf3SDmitry Chagin
11480fac350cSGleb Smirnoff error = kern_getpeername(td, args->s, (struct sockaddr *)&ss);
1149d5368bf3SDmitry Chagin if (error != 0)
1150d5368bf3SDmitry Chagin return (error);
1151d5368bf3SDmitry Chagin
11520fac350cSGleb Smirnoff len = min(ss.ss_len, len);
11530fac350cSGleb Smirnoff error = linux_copyout_sockaddr((struct sockaddr *)&ss,
11540fac350cSGleb Smirnoff PTRIN(args->addr), len);
11550fac350cSGleb Smirnoff if (error == 0) {
11560fac350cSGleb Smirnoff len = ss.ss_len;
1157d5368bf3SDmitry Chagin error = copyout(&len, PTRIN(args->namelen), sizeof(len));
11580fac350cSGleb Smirnoff }
1159d5368bf3SDmitry Chagin return (error);
1160c21dee17SSøren Schmidt }
1161c21dee17SSøren Schmidt
1162a12b9b3dSDmitry Chagin int
linux_socketpair(struct thread * td,struct linux_socketpair_args * args)1163b40ce416SJulian Elischer linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
1164c21dee17SSøren Schmidt {
11650b40dcbeSEdward Tomasz Napierala int domain, error, sv[2], type;
1166c21dee17SSøren Schmidt
11670b40dcbeSEdward Tomasz Napierala domain = linux_to_bsd_domain(args->domain);
11680b40dcbeSEdward Tomasz Napierala if (domain != PF_LOCAL)
11691a52a4abSDmitry Chagin return (EAFNOSUPPORT);
11700b40dcbeSEdward Tomasz Napierala type = args->type & LINUX_SOCK_TYPE_MASK;
11710b40dcbeSEdward Tomasz Napierala if (type < 0 || type > LINUX_SOCK_MAX)
117239253cf9SDmitry Chagin return (EINVAL);
11734cf10e29SDmitry Chagin error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
11740b40dcbeSEdward Tomasz Napierala &type);
11754cf10e29SDmitry Chagin if (error != 0)
11764cf10e29SDmitry Chagin return (error);
11770b40dcbeSEdward Tomasz Napierala if (args->protocol != 0 && args->protocol != PF_UNIX) {
11781a52a4abSDmitry Chagin /*
11791a52a4abSDmitry Chagin * Use of PF_UNIX as protocol argument is not right,
11801a52a4abSDmitry Chagin * but Linux does it.
11811a52a4abSDmitry Chagin * Do not map PF_UNIX as its Linux value is identical
11821a52a4abSDmitry Chagin * to FreeBSD one.
11831a52a4abSDmitry Chagin */
11841a52a4abSDmitry Chagin return (EPROTONOSUPPORT);
11850b40dcbeSEdward Tomasz Napierala }
11860b40dcbeSEdward Tomasz Napierala error = kern_socketpair(td, domain, type, 0, sv);
11870b40dcbeSEdward Tomasz Napierala if (error != 0)
11880b40dcbeSEdward Tomasz Napierala return (error);
11890b40dcbeSEdward Tomasz Napierala error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
11900b40dcbeSEdward Tomasz Napierala if (error != 0) {
11910b40dcbeSEdward Tomasz Napierala (void)kern_close(td, sv[0]);
11920b40dcbeSEdward Tomasz Napierala (void)kern_close(td, sv[1]);
11930b40dcbeSEdward Tomasz Napierala }
11940b40dcbeSEdward Tomasz Napierala return (error);
1195c21dee17SSøren Schmidt }
1196c21dee17SSøren Schmidt
1197a12b9b3dSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
119801e0ffbaSAlexander Leidinger struct linux_send_args {
119956fba8e6SDmitry Chagin register_t s;
120056fba8e6SDmitry Chagin register_t msg;
120156fba8e6SDmitry Chagin register_t len;
120256fba8e6SDmitry Chagin register_t flags;
1203c21dee17SSøren Schmidt };
1204c21dee17SSøren Schmidt
120501e0ffbaSAlexander Leidinger static int
linux_send(struct thread * td,struct linux_send_args * args)1206b40ce416SJulian Elischer linux_send(struct thread *td, struct linux_send_args *args)
1207c21dee17SSøren Schmidt {
120887d72a8fSPoul-Henning Kamp struct sendto_args /* {
1209c21dee17SSøren Schmidt int s;
1210c21dee17SSøren Schmidt caddr_t buf;
1211044af7c3SJonathan Mini int len;
1212c21dee17SSøren Schmidt int flags;
121387d72a8fSPoul-Henning Kamp caddr_t to;
121487d72a8fSPoul-Henning Kamp int tolen;
1215ef04503dSPeter Wemm } */ bsd_args;
1216aa288712SDmitry Chagin struct file *fp;
12173212ad15SMateusz Guzik int error;
1218c21dee17SSøren Schmidt
1219745aaef5SKonstantin Belousov bsd_args.s = args->s;
1220745aaef5SKonstantin Belousov bsd_args.buf = (caddr_t)PTRIN(args->msg);
1221745aaef5SKonstantin Belousov bsd_args.len = args->len;
1222f6e8256aSEdward Tomasz Napierala bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
122387d72a8fSPoul-Henning Kamp bsd_args.to = NULL;
122487d72a8fSPoul-Henning Kamp bsd_args.tolen = 0;
1225aa288712SDmitry Chagin error = sys_sendto(td, &bsd_args);
1226aa288712SDmitry Chagin if (error == ENOTCONN) {
1227aa288712SDmitry Chagin /*
1228aa288712SDmitry Chagin * Linux doesn't return ENOTCONN for non-blocking sockets.
1229aa288712SDmitry Chagin * Instead it returns the EAGAIN.
1230aa288712SDmitry Chagin */
12313212ad15SMateusz Guzik error = getsock(td, args->s, &cap_send_rights, &fp);
1232aa288712SDmitry Chagin if (error == 0) {
12333212ad15SMateusz Guzik if (atomic_load_int(&fp->f_flag) & FNONBLOCK)
1234aa288712SDmitry Chagin error = EAGAIN;
1235aa288712SDmitry Chagin fdrop(fp, td);
1236aa288712SDmitry Chagin }
1237aa288712SDmitry Chagin }
1238aa288712SDmitry Chagin return (error);
1239c21dee17SSøren Schmidt }
1240c21dee17SSøren Schmidt
124101e0ffbaSAlexander Leidinger struct linux_recv_args {
124256fba8e6SDmitry Chagin register_t s;
124356fba8e6SDmitry Chagin register_t msg;
124456fba8e6SDmitry Chagin register_t len;
124556fba8e6SDmitry Chagin register_t flags;
1246c21dee17SSøren Schmidt };
1247c21dee17SSøren Schmidt
124801e0ffbaSAlexander Leidinger static int
linux_recv(struct thread * td,struct linux_recv_args * args)1249b40ce416SJulian Elischer linux_recv(struct thread *td, struct linux_recv_args *args)
1250c21dee17SSøren Schmidt {
125187d72a8fSPoul-Henning Kamp struct recvfrom_args /* {
1252c21dee17SSøren Schmidt int s;
1253c21dee17SSøren Schmidt caddr_t buf;
1254c21dee17SSøren Schmidt int len;
1255c21dee17SSøren Schmidt int flags;
125687d72a8fSPoul-Henning Kamp struct sockaddr *from;
125787d72a8fSPoul-Henning Kamp socklen_t fromlenaddr;
1258ef04503dSPeter Wemm } */ bsd_args;
1259c21dee17SSøren Schmidt
1260745aaef5SKonstantin Belousov bsd_args.s = args->s;
1261745aaef5SKonstantin Belousov bsd_args.buf = (caddr_t)PTRIN(args->msg);
1262745aaef5SKonstantin Belousov bsd_args.len = args->len;
12633980a435SDmitry Chagin bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
126487d72a8fSPoul-Henning Kamp bsd_args.from = NULL;
126587d72a8fSPoul-Henning Kamp bsd_args.fromlenaddr = 0;
12668451d0ddSKip Macy return (sys_recvfrom(td, &bsd_args));
1267c21dee17SSøren Schmidt }
1268a12b9b3dSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1269c21dee17SSøren Schmidt
1270a12b9b3dSDmitry Chagin int
linux_sendto(struct thread * td,struct linux_sendto_args * args)1271b40ce416SJulian Elischer linux_sendto(struct thread *td, struct linux_sendto_args *args)
1272c21dee17SSøren Schmidt {
12735a8a13e0SDavid Malone struct msghdr msg;
12745a8a13e0SDavid Malone struct iovec aiov;
1275db48fa83SDmitry Chagin struct socket *so;
1276db48fa83SDmitry Chagin struct file *fp;
1277db48fa83SDmitry Chagin int error;
1278c21dee17SSøren Schmidt
1279745aaef5SKonstantin Belousov if (linux_check_hdrincl(td, args->s) == 0)
1280f2477ae1SMike Smith /* IP_HDRINCL set, tweak the packet before sending */
1281745aaef5SKonstantin Belousov return (linux_sendto_hdrincl(td, args));
1282f2477ae1SMike Smith
1283db48fa83SDmitry Chagin bzero(&msg, sizeof(msg));
12843212ad15SMateusz Guzik error = getsock(td, args->s, &cap_send_connect_rights, &fp);
1285db48fa83SDmitry Chagin if (error != 0)
1286db48fa83SDmitry Chagin return (error);
1287db48fa83SDmitry Chagin so = fp->f_data;
1288db48fa83SDmitry Chagin if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) {
1289745aaef5SKonstantin Belousov msg.msg_name = PTRIN(args->to);
1290745aaef5SKonstantin Belousov msg.msg_namelen = args->tolen;
1291db48fa83SDmitry Chagin }
12925a8a13e0SDavid Malone msg.msg_iov = &aiov;
12935a8a13e0SDavid Malone msg.msg_iovlen = 1;
1294745aaef5SKonstantin Belousov aiov.iov_base = PTRIN(args->msg);
1295745aaef5SKonstantin Belousov aiov.iov_len = args->len;
1296db48fa83SDmitry Chagin fdrop(fp, td);
1297e667ee63SDmitry Chagin return (linux_sendit(td, args->s, &msg, args->flags, NULL,
1298e667ee63SDmitry Chagin UIO_USERSPACE));
1299c21dee17SSøren Schmidt }
1300c21dee17SSøren Schmidt
1301a12b9b3dSDmitry Chagin int
linux_recvfrom(struct thread * td,struct linux_recvfrom_args * args)1302b40ce416SJulian Elischer linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
1303c21dee17SSøren Schmidt {
1304d5368bf3SDmitry Chagin struct sockaddr *sa;
13059599b0ecSDmitry Chagin struct msghdr msg;
13069599b0ecSDmitry Chagin struct iovec aiov;
13073a49978fSDmitry Chagin int error, fromlen;
1308c21dee17SSøren Schmidt
13099599b0ecSDmitry Chagin if (PTRIN(args->fromlen) != NULL) {
13103a49978fSDmitry Chagin error = copyin(PTRIN(args->fromlen), &fromlen,
13113a49978fSDmitry Chagin sizeof(fromlen));
13129599b0ecSDmitry Chagin if (error != 0)
13133f3a4815SMarcel Moolenaar return (error);
13143a49978fSDmitry Chagin if (fromlen < 0)
13153a49978fSDmitry Chagin return (EINVAL);
1316bb0f644cSDmitry Chagin fromlen = min(fromlen, SOCK_MAXADDRLEN);
1317d5368bf3SDmitry Chagin sa = malloc(fromlen, M_SONAME, M_WAITOK);
1318d5368bf3SDmitry Chagin } else {
1319d5368bf3SDmitry Chagin fromlen = 0;
1320d5368bf3SDmitry Chagin sa = NULL;
1321d5368bf3SDmitry Chagin }
13229599b0ecSDmitry Chagin
1323d5368bf3SDmitry Chagin msg.msg_name = sa;
1324d5368bf3SDmitry Chagin msg.msg_namelen = fromlen;
13259599b0ecSDmitry Chagin msg.msg_iov = &aiov;
13269599b0ecSDmitry Chagin msg.msg_iovlen = 1;
13279599b0ecSDmitry Chagin aiov.iov_base = PTRIN(args->buf);
13289599b0ecSDmitry Chagin aiov.iov_len = args->len;
13299599b0ecSDmitry Chagin msg.msg_control = 0;
13309599b0ecSDmitry Chagin msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
13319599b0ecSDmitry Chagin
1332d5368bf3SDmitry Chagin error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
13339599b0ecSDmitry Chagin if (error != 0)
1334dcd62418SDmitry Chagin goto out;
13359599b0ecSDmitry Chagin
1336bb46e9b5SDmitry Chagin /*
1337bb46e9b5SDmitry Chagin * XXX. Seems that FreeBSD is different from Linux here. Linux
1338bb46e9b5SDmitry Chagin * fill source address if underlying protocol provides it, while
1339bb46e9b5SDmitry Chagin * FreeBSD fill it if underlying protocol is not connection-oriented.
1340bb46e9b5SDmitry Chagin * So, kern_recvit() set msg.msg_namelen to 0 if protocol pr_flags
1341bb46e9b5SDmitry Chagin * does not contains PR_ADDR flag.
1342bb46e9b5SDmitry Chagin */
1343bb46e9b5SDmitry Chagin if (PTRIN(args->from) != NULL && msg.msg_namelen != 0)
1344bb46e9b5SDmitry Chagin error = linux_copyout_sockaddr(sa, PTRIN(args->from),
1345bb46e9b5SDmitry Chagin msg.msg_namelen);
13469599b0ecSDmitry Chagin
1347d5368bf3SDmitry Chagin if (error == 0 && PTRIN(args->fromlen) != NULL)
13489599b0ecSDmitry Chagin error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
13499599b0ecSDmitry Chagin sizeof(msg.msg_namelen));
1350dcd62418SDmitry Chagin out:
1351d5368bf3SDmitry Chagin free(sa, M_SONAME);
13529599b0ecSDmitry Chagin return (error);
1353ca26842eSHajimu UMEMOTO }
1354ca26842eSHajimu UMEMOTO
1355e1ff74c0SDmitry Chagin static int
linux_sendmsg_common(struct thread * td,l_int s,struct l_msghdr * msghdr,l_uint flags)1356e1ff74c0SDmitry Chagin linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1357e1ff74c0SDmitry Chagin l_uint flags)
1358ca26842eSHajimu UMEMOTO {
13590fac350cSGleb Smirnoff struct sockaddr_storage ss = { .ss_len = sizeof(ss) };
136074f5d680SKonstantin Belousov struct cmsghdr *cmsg;
136174f5d680SKonstantin Belousov struct mbuf *control;
1362ca26842eSHajimu UMEMOTO struct msghdr msg;
136374f5d680SKonstantin Belousov struct l_cmsghdr linux_cmsg;
136474f5d680SKonstantin Belousov struct l_cmsghdr *ptr_cmsg;
1365599dadcaSEdward Tomasz Napierala struct l_msghdr linux_msghdr;
1366552afd9cSPoul-Henning Kamp struct iovec *iov;
136774f5d680SKonstantin Belousov socklen_t datalen;
13681410bfe1SDmitry Chagin struct socket *so;
1369605da56bSAndriy Gapon sa_family_t sa_family;
13701410bfe1SDmitry Chagin struct file *fp;
137174f5d680SKonstantin Belousov void *data;
1372e3b385fcSTijl Coosemans l_size_t len;
13737df0e7beSTijl Coosemans l_size_t clen;
13743212ad15SMateusz Guzik int error;
1375ca26842eSHajimu UMEMOTO
1376599dadcaSEdward Tomasz Napierala error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
1377e1ff74c0SDmitry Chagin if (error != 0)
137874f5d680SKonstantin Belousov return (error);
1379d72a6158SRobert Watson
1380d72a6158SRobert Watson /*
1381d72a6158SRobert Watson * Some Linux applications (ping) define a non-NULL control data
1382d72a6158SRobert Watson * pointer, but a msg_controllen of 0, which is not allowed in the
1383d72a6158SRobert Watson * FreeBSD system call interface. NULL the msg_control pointer in
1384d72a6158SRobert Watson * order to handle this case. This should be checked, but allows the
1385d72a6158SRobert Watson * Linux ping to work.
1386d72a6158SRobert Watson */
1387599dadcaSEdward Tomasz Napierala if (PTRIN(linux_msghdr.msg_control) != NULL &&
1388599dadcaSEdward Tomasz Napierala linux_msghdr.msg_controllen == 0)
1389599dadcaSEdward Tomasz Napierala linux_msghdr.msg_control = PTROUT(NULL);
1390605da56bSAndriy Gapon
1391599dadcaSEdward Tomasz Napierala error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
1392e1ff74c0SDmitry Chagin if (error != 0)
1393605da56bSAndriy Gapon return (error);
139474f5d680SKonstantin Belousov
139574f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32
1396524c9accSDmitry Chagin error = freebsd32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
139774f5d680SKonstantin Belousov &iov, EMSGSIZE);
139874f5d680SKonstantin Belousov #else
1399552afd9cSPoul-Henning Kamp error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
140074f5d680SKonstantin Belousov #endif
1401e1ff74c0SDmitry Chagin if (error != 0)
1402552afd9cSPoul-Henning Kamp return (error);
140374f5d680SKonstantin Belousov
1404605da56bSAndriy Gapon control = NULL;
1405605da56bSAndriy Gapon
14060fac350cSGleb Smirnoff error = kern_getsockname(td, s, (struct sockaddr *)&ss);
1407e1ff74c0SDmitry Chagin if (error != 0)
1408605da56bSAndriy Gapon goto bad;
14090fac350cSGleb Smirnoff sa_family = ss.ss_family;
1410605da56bSAndriy Gapon
14111410bfe1SDmitry Chagin if (flags & LINUX_MSG_OOB) {
14121410bfe1SDmitry Chagin error = EOPNOTSUPP;
14131410bfe1SDmitry Chagin if (sa_family == AF_UNIX)
14141410bfe1SDmitry Chagin goto bad;
14151410bfe1SDmitry Chagin
14163212ad15SMateusz Guzik error = getsock(td, s, &cap_send_rights, &fp);
14171410bfe1SDmitry Chagin if (error != 0)
14181410bfe1SDmitry Chagin goto bad;
14191410bfe1SDmitry Chagin so = fp->f_data;
14201410bfe1SDmitry Chagin if (so->so_type != SOCK_STREAM)
14211410bfe1SDmitry Chagin error = EOPNOTSUPP;
14221410bfe1SDmitry Chagin fdrop(fp, td);
14231410bfe1SDmitry Chagin if (error != 0)
14241410bfe1SDmitry Chagin goto bad;
14251410bfe1SDmitry Chagin }
14261410bfe1SDmitry Chagin
1427599dadcaSEdward Tomasz Napierala if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
142874f5d680SKonstantin Belousov error = ENOBUFS;
1429eb1b1807SGleb Smirnoff control = m_get(M_WAITOK, MT_CONTROL);
1430e3b385fcSTijl Coosemans MCLGET(control, M_WAITOK);
1431e3b385fcSTijl Coosemans data = mtod(control, void *);
1432e3b385fcSTijl Coosemans datalen = 0;
143374f5d680SKonstantin Belousov
1434599dadcaSEdward Tomasz Napierala ptr_cmsg = PTRIN(linux_msghdr.msg_control);
1435599dadcaSEdward Tomasz Napierala clen = linux_msghdr.msg_controllen;
143674f5d680SKonstantin Belousov do {
143774f5d680SKonstantin Belousov error = copyin(ptr_cmsg, &linux_cmsg,
143874f5d680SKonstantin Belousov sizeof(struct l_cmsghdr));
1439e1ff74c0SDmitry Chagin if (error != 0)
144074f5d680SKonstantin Belousov goto bad;
144174f5d680SKonstantin Belousov
144274f5d680SKonstantin Belousov error = EINVAL;
14437df0e7beSTijl Coosemans if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
14447df0e7beSTijl Coosemans linux_cmsg.cmsg_len > clen)
144574f5d680SKonstantin Belousov goto bad;
144674f5d680SKonstantin Belousov
1447e3b385fcSTijl Coosemans if (datalen + CMSG_HDRSZ > MCLBYTES)
1448e3b385fcSTijl Coosemans goto bad;
1449e3b385fcSTijl Coosemans
145074f5d680SKonstantin Belousov /*
1451605da56bSAndriy Gapon * Now we support only SCM_RIGHTS and SCM_CRED,
1452605da56bSAndriy Gapon * so return EINVAL in any other cmsg_type
145374f5d680SKonstantin Belousov */
1454e3b385fcSTijl Coosemans cmsg = data;
1455605da56bSAndriy Gapon cmsg->cmsg_type =
1456605da56bSAndriy Gapon linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
145774f5d680SKonstantin Belousov cmsg->cmsg_level =
145874f5d680SKonstantin Belousov linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
1459605da56bSAndriy Gapon if (cmsg->cmsg_type == -1
1460889cd285SEdward Tomasz Napierala || cmsg->cmsg_level != SOL_SOCKET) {
1461889cd285SEdward Tomasz Napierala linux_msg(curthread,
1462889cd285SEdward Tomasz Napierala "unsupported sendmsg cmsg level %d type %d",
1463889cd285SEdward Tomasz Napierala linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
1464605da56bSAndriy Gapon goto bad;
1465889cd285SEdward Tomasz Napierala }
146674f5d680SKonstantin Belousov
1467605da56bSAndriy Gapon /*
1468605da56bSAndriy Gapon * Some applications (e.g. pulseaudio) attempt to
1469605da56bSAndriy Gapon * send ancillary data even if the underlying protocol
1470605da56bSAndriy Gapon * doesn't support it which is not allowed in the
1471605da56bSAndriy Gapon * FreeBSD system call interface.
1472605da56bSAndriy Gapon */
1473605da56bSAndriy Gapon if (sa_family != AF_UNIX)
1474a7044c60SMark Johnston goto next;
1475605da56bSAndriy Gapon
1476e3b385fcSTijl Coosemans if (cmsg->cmsg_type == SCM_CREDS) {
1477e3b385fcSTijl Coosemans len = sizeof(struct cmsgcred);
1478e3b385fcSTijl Coosemans if (datalen + CMSG_SPACE(len) > MCLBYTES)
1479e3b385fcSTijl Coosemans goto bad;
1480605da56bSAndriy Gapon
1481605da56bSAndriy Gapon /*
1482605da56bSAndriy Gapon * The lower levels will fill in the structure
1483605da56bSAndriy Gapon */
1484e3b385fcSTijl Coosemans memset(CMSG_DATA(data), 0, len);
1485e3b385fcSTijl Coosemans } else {
1486e3b385fcSTijl Coosemans len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
1487e3b385fcSTijl Coosemans if (datalen + CMSG_SPACE(len) < datalen ||
1488e3b385fcSTijl Coosemans datalen + CMSG_SPACE(len) > MCLBYTES)
1489e3b385fcSTijl Coosemans goto bad;
1490e3b385fcSTijl Coosemans
1491e3b385fcSTijl Coosemans error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
1492e3b385fcSTijl Coosemans CMSG_DATA(data), len);
1493e3b385fcSTijl Coosemans if (error != 0)
1494e3b385fcSTijl Coosemans goto bad;
1495605da56bSAndriy Gapon }
1496605da56bSAndriy Gapon
1497e3b385fcSTijl Coosemans cmsg->cmsg_len = CMSG_LEN(len);
1498e3b385fcSTijl Coosemans data = (char *)data + CMSG_SPACE(len);
1499e3b385fcSTijl Coosemans datalen += CMSG_SPACE(len);
15007df0e7beSTijl Coosemans
1501a7044c60SMark Johnston next:
15027df0e7beSTijl Coosemans if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
15037df0e7beSTijl Coosemans break;
15047df0e7beSTijl Coosemans
15057df0e7beSTijl Coosemans clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
15067df0e7beSTijl Coosemans ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
15077df0e7beSTijl Coosemans LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
15087df0e7beSTijl Coosemans } while(clen >= sizeof(struct l_cmsghdr));
1509605da56bSAndriy Gapon
1510e3b385fcSTijl Coosemans control->m_len = datalen;
1511e3b385fcSTijl Coosemans if (datalen == 0) {
1512605da56bSAndriy Gapon m_freem(control);
151374f5d680SKonstantin Belousov control = NULL;
1514605da56bSAndriy Gapon }
151574f5d680SKonstantin Belousov }
151674f5d680SKonstantin Belousov
15175a8a13e0SDavid Malone msg.msg_iov = iov;
15185a8a13e0SDavid Malone msg.msg_flags = 0;
1519e1ff74c0SDmitry Chagin error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
152067968b35SDmitry Chagin control = NULL;
152174f5d680SKonstantin Belousov
152274f5d680SKonstantin Belousov bad:
15234f65e9cfSDmitry Chagin m_freem(control);
1524552afd9cSPoul-Henning Kamp free(iov, M_IOV);
1525ca26842eSHajimu UMEMOTO return (error);
1526c21dee17SSøren Schmidt }
1527c21dee17SSøren Schmidt
1528a12b9b3dSDmitry Chagin int
linux_sendmsg(struct thread * td,struct linux_sendmsg_args * args)1529e1ff74c0SDmitry Chagin linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
1530e1ff74c0SDmitry Chagin {
1531e1ff74c0SDmitry Chagin
1532e1ff74c0SDmitry Chagin return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
1533e1ff74c0SDmitry Chagin args->flags));
1534e1ff74c0SDmitry Chagin }
1535e1ff74c0SDmitry Chagin
1536e1ff74c0SDmitry Chagin int
linux_sendmmsg(struct thread * td,struct linux_sendmmsg_args * args)1537e1ff74c0SDmitry Chagin linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
1538e1ff74c0SDmitry Chagin {
1539e1ff74c0SDmitry Chagin struct l_mmsghdr *msg;
1540e1ff74c0SDmitry Chagin l_uint retval;
1541e1ff74c0SDmitry Chagin int error, datagrams;
1542e1ff74c0SDmitry Chagin
1543e1ff74c0SDmitry Chagin if (args->vlen > UIO_MAXIOV)
1544e1ff74c0SDmitry Chagin args->vlen = UIO_MAXIOV;
1545e1ff74c0SDmitry Chagin
1546e1ff74c0SDmitry Chagin msg = PTRIN(args->msg);
1547e1ff74c0SDmitry Chagin datagrams = 0;
1548e1ff74c0SDmitry Chagin while (datagrams < args->vlen) {
1549e1ff74c0SDmitry Chagin error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
1550e1ff74c0SDmitry Chagin args->flags);
1551e1ff74c0SDmitry Chagin if (error != 0)
1552e1ff74c0SDmitry Chagin break;
1553e1ff74c0SDmitry Chagin
1554e1ff74c0SDmitry Chagin retval = td->td_retval[0];
1555e1ff74c0SDmitry Chagin error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1556e1ff74c0SDmitry Chagin if (error != 0)
1557e1ff74c0SDmitry Chagin break;
1558e1ff74c0SDmitry Chagin ++msg;
1559e1ff74c0SDmitry Chagin ++datagrams;
1560e1ff74c0SDmitry Chagin }
1561e1ff74c0SDmitry Chagin if (error == 0)
1562e1ff74c0SDmitry Chagin td->td_retval[0] = datagrams;
1563e1ff74c0SDmitry Chagin return (error);
1564e1ff74c0SDmitry Chagin }
1565e1ff74c0SDmitry Chagin
1566e1ff74c0SDmitry Chagin static int
recvmsg_scm_rights(struct thread * td,l_uint flags,socklen_t * datalen,void ** data,void ** udata)1567b408788dSDmitry Chagin recvmsg_scm_rights(struct thread *td, l_uint flags, socklen_t *datalen,
1568b408788dSDmitry Chagin void **data, void **udata)
1569b408788dSDmitry Chagin {
1570b408788dSDmitry Chagin int i, fd, fds, *fdp;
1571b408788dSDmitry Chagin
1572b408788dSDmitry Chagin if (flags & LINUX_MSG_CMSG_CLOEXEC) {
1573b408788dSDmitry Chagin fds = *datalen / sizeof(int);
1574b408788dSDmitry Chagin fdp = *data;
1575b408788dSDmitry Chagin for (i = 0; i < fds; i++) {
1576b408788dSDmitry Chagin fd = *fdp++;
1577b408788dSDmitry Chagin (void)kern_fcntl(td, fd, F_SETFD, FD_CLOEXEC);
1578b408788dSDmitry Chagin }
1579b408788dSDmitry Chagin }
1580b408788dSDmitry Chagin return (0);
1581b408788dSDmitry Chagin }
1582b408788dSDmitry Chagin
158371bc8bcfSDmitry Chagin
1584b408788dSDmitry Chagin static int
recvmsg_scm_creds(socklen_t * datalen,void ** data,void ** udata)1585b408788dSDmitry Chagin recvmsg_scm_creds(socklen_t *datalen, void **data, void **udata)
1586b408788dSDmitry Chagin {
1587b408788dSDmitry Chagin struct cmsgcred *cmcred;
1588b408788dSDmitry Chagin struct l_ucred lu;
1589b408788dSDmitry Chagin
1590b408788dSDmitry Chagin cmcred = *data;
1591b408788dSDmitry Chagin lu.pid = cmcred->cmcred_pid;
1592b408788dSDmitry Chagin lu.uid = cmcred->cmcred_uid;
1593b408788dSDmitry Chagin lu.gid = cmcred->cmcred_gid;
1594b408788dSDmitry Chagin memmove(*data, &lu, sizeof(lu));
1595b408788dSDmitry Chagin *datalen = sizeof(lu);
1596b408788dSDmitry Chagin return (0);
1597b408788dSDmitry Chagin }
1598b408788dSDmitry Chagin _Static_assert(sizeof(struct cmsgcred) >= sizeof(struct l_ucred),
1599b408788dSDmitry Chagin "scm_creds sizeof l_ucred");
1600b408788dSDmitry Chagin
1601b408788dSDmitry Chagin static int
recvmsg_scm_creds2(socklen_t * datalen,void ** data,void ** udata)1602b408788dSDmitry Chagin recvmsg_scm_creds2(socklen_t *datalen, void **data, void **udata)
1603b408788dSDmitry Chagin {
1604b408788dSDmitry Chagin struct sockcred2 *scred;
1605b408788dSDmitry Chagin struct l_ucred lu;
1606b408788dSDmitry Chagin
1607b408788dSDmitry Chagin scred = *data;
1608b408788dSDmitry Chagin lu.pid = scred->sc_pid;
1609b408788dSDmitry Chagin lu.uid = scred->sc_uid;
1610b408788dSDmitry Chagin lu.gid = scred->sc_gid;
1611b408788dSDmitry Chagin memmove(*data, &lu, sizeof(lu));
1612b408788dSDmitry Chagin *datalen = sizeof(lu);
1613b408788dSDmitry Chagin return (0);
1614b408788dSDmitry Chagin }
1615b408788dSDmitry Chagin _Static_assert(sizeof(struct sockcred2) >= sizeof(struct l_ucred),
1616b408788dSDmitry Chagin "scm_creds2 sizeof l_ucred");
1617b408788dSDmitry Chagin
16180e26e54bSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1619b408788dSDmitry Chagin static int
recvmsg_scm_timestamp(l_int msg_type,socklen_t * datalen,void ** data,void ** udata)16200e26e54bSDmitry Chagin recvmsg_scm_timestamp(l_int msg_type, socklen_t *datalen, void **data,
16210e26e54bSDmitry Chagin void **udata)
1622b408788dSDmitry Chagin {
16230e26e54bSDmitry Chagin l_sock_timeval ltv64;
16240e26e54bSDmitry Chagin l_timeval ltv;
16250e26e54bSDmitry Chagin struct timeval *tv;
16260e26e54bSDmitry Chagin socklen_t len;
16270e26e54bSDmitry Chagin void *buf;
1628b408788dSDmitry Chagin
1629b408788dSDmitry Chagin if (*datalen != sizeof(struct timeval))
1630b408788dSDmitry Chagin return (EMSGSIZE);
1631b408788dSDmitry Chagin
16320e26e54bSDmitry Chagin tv = *data;
16330e26e54bSDmitry Chagin #if defined(COMPAT_LINUX32)
16340e26e54bSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPO &&
16350e26e54bSDmitry Chagin (tv->tv_sec > INT_MAX || tv->tv_sec < INT_MIN))
16360e26e54bSDmitry Chagin return (EOVERFLOW);
16370e26e54bSDmitry Chagin #endif
16380e26e54bSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPN)
16390e26e54bSDmitry Chagin len = sizeof(ltv64);
16400e26e54bSDmitry Chagin else
16410e26e54bSDmitry Chagin len = sizeof(ltv);
16420e26e54bSDmitry Chagin
16430e26e54bSDmitry Chagin buf = malloc(len, M_LINUX, M_WAITOK);
16440e26e54bSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPN) {
16450e26e54bSDmitry Chagin ltv64.tv_sec = tv->tv_sec;
16460e26e54bSDmitry Chagin ltv64.tv_usec = tv->tv_usec;
16470e26e54bSDmitry Chagin memmove(buf, <v64, len);
16480e26e54bSDmitry Chagin } else {
16490e26e54bSDmitry Chagin ltv.tv_sec = tv->tv_sec;
16500e26e54bSDmitry Chagin ltv.tv_usec = tv->tv_usec;
16510e26e54bSDmitry Chagin memmove(buf, <v, len);
16520e26e54bSDmitry Chagin }
16530e26e54bSDmitry Chagin *data = *udata = buf;
16540e26e54bSDmitry Chagin *datalen = len;
1655b408788dSDmitry Chagin return (0);
1656b408788dSDmitry Chagin }
16570e26e54bSDmitry Chagin #else
16580e26e54bSDmitry Chagin _Static_assert(sizeof(struct timeval) == sizeof(l_timeval),
16590e26e54bSDmitry Chagin "scm_timestamp sizeof l_timeval");
16600e26e54bSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
1661b408788dSDmitry Chagin
166271bc8bcfSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
166371bc8bcfSDmitry Chagin static int
recvmsg_scm_timestampns(l_int msg_type,socklen_t * datalen,void ** data,void ** udata)166471bc8bcfSDmitry Chagin recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
166571bc8bcfSDmitry Chagin void **udata)
166671bc8bcfSDmitry Chagin {
166771bc8bcfSDmitry Chagin struct l_timespec64 ts64;
166871bc8bcfSDmitry Chagin struct l_timespec ts32;
166971bc8bcfSDmitry Chagin struct timespec ts;
167071bc8bcfSDmitry Chagin socklen_t len;
167171bc8bcfSDmitry Chagin void *buf;
167271bc8bcfSDmitry Chagin
167371bc8bcfSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPNSO)
167471bc8bcfSDmitry Chagin len = sizeof(ts32);
167571bc8bcfSDmitry Chagin else
167671bc8bcfSDmitry Chagin len = sizeof(ts64);
167771bc8bcfSDmitry Chagin
167871bc8bcfSDmitry Chagin buf = malloc(len, M_LINUX, M_WAITOK);
167971bc8bcfSDmitry Chagin bintime2timespec(*data, &ts);
168071bc8bcfSDmitry Chagin if (msg_type == LINUX_SCM_TIMESTAMPNSO) {
168171bc8bcfSDmitry Chagin ts32.tv_sec = ts.tv_sec;
168271bc8bcfSDmitry Chagin ts32.tv_nsec = ts.tv_nsec;
168371bc8bcfSDmitry Chagin memmove(buf, &ts32, len);
168471bc8bcfSDmitry Chagin } else {
168571bc8bcfSDmitry Chagin ts64.tv_sec = ts.tv_sec;
168671bc8bcfSDmitry Chagin ts64.tv_nsec = ts.tv_nsec;
168771bc8bcfSDmitry Chagin memmove(buf, &ts64, len);
168871bc8bcfSDmitry Chagin }
168971bc8bcfSDmitry Chagin *data = *udata = buf;
169071bc8bcfSDmitry Chagin *datalen = len;
169171bc8bcfSDmitry Chagin return (0);
169271bc8bcfSDmitry Chagin }
169371bc8bcfSDmitry Chagin #else
169471bc8bcfSDmitry Chagin static int
recvmsg_scm_timestampns(l_int msg_type,socklen_t * datalen,void ** data,void ** udata)169571bc8bcfSDmitry Chagin recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
169671bc8bcfSDmitry Chagin void **udata)
169771bc8bcfSDmitry Chagin {
169871bc8bcfSDmitry Chagin struct timespec ts;
169971bc8bcfSDmitry Chagin
170071bc8bcfSDmitry Chagin bintime2timespec(*data, &ts);
170171bc8bcfSDmitry Chagin memmove(*data, &ts, sizeof(struct timespec));
170271bc8bcfSDmitry Chagin *datalen = sizeof(struct timespec);
170371bc8bcfSDmitry Chagin return (0);
170471bc8bcfSDmitry Chagin }
170571bc8bcfSDmitry Chagin _Static_assert(sizeof(struct bintime) >= sizeof(struct timespec),
170671bc8bcfSDmitry Chagin "scm_timestampns sizeof timespec");
170771bc8bcfSDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
170871bc8bcfSDmitry Chagin
1709b408788dSDmitry Chagin static int
recvmsg_scm_sol_socket(struct thread * td,l_int msg_type,l_int lmsg_type,l_uint flags,socklen_t * datalen,void ** data,void ** udata)1710de20eb26SDmitry Chagin recvmsg_scm_sol_socket(struct thread *td, l_int msg_type, l_int lmsg_type,
1711de20eb26SDmitry Chagin l_uint flags, socklen_t *datalen, void **data, void **udata)
1712de20eb26SDmitry Chagin {
1713de20eb26SDmitry Chagin int error;
1714de20eb26SDmitry Chagin
1715de20eb26SDmitry Chagin error = 0;
1716de20eb26SDmitry Chagin switch (msg_type) {
1717de20eb26SDmitry Chagin case SCM_RIGHTS:
1718de20eb26SDmitry Chagin error = recvmsg_scm_rights(td, flags, datalen,
1719de20eb26SDmitry Chagin data, udata);
1720de20eb26SDmitry Chagin break;
1721de20eb26SDmitry Chagin case SCM_CREDS:
1722de20eb26SDmitry Chagin error = recvmsg_scm_creds(datalen, data, udata);
1723de20eb26SDmitry Chagin break;
1724de20eb26SDmitry Chagin case SCM_CREDS2:
1725de20eb26SDmitry Chagin error = recvmsg_scm_creds2(datalen, data, udata);
1726de20eb26SDmitry Chagin break;
1727de20eb26SDmitry Chagin case SCM_TIMESTAMP:
1728de20eb26SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
1729de20eb26SDmitry Chagin error = recvmsg_scm_timestamp(lmsg_type, datalen,
1730de20eb26SDmitry Chagin data, udata);
1731de20eb26SDmitry Chagin #endif
1732de20eb26SDmitry Chagin break;
1733de20eb26SDmitry Chagin case SCM_BINTIME:
1734de20eb26SDmitry Chagin error = recvmsg_scm_timestampns(lmsg_type, datalen,
1735de20eb26SDmitry Chagin data, udata);
1736de20eb26SDmitry Chagin break;
1737de20eb26SDmitry Chagin }
1738de20eb26SDmitry Chagin
1739de20eb26SDmitry Chagin return (error);
1740de20eb26SDmitry Chagin }
1741de20eb26SDmitry Chagin
1742de20eb26SDmitry Chagin static int
recvmsg_scm_ip_origdstaddr(socklen_t * datalen,void ** data,void ** udata)1743f8a66150SDmitry Chagin recvmsg_scm_ip_origdstaddr(socklen_t *datalen, void **data, void **udata)
1744f8a66150SDmitry Chagin {
1745f8a66150SDmitry Chagin struct l_sockaddr *lsa;
1746f8a66150SDmitry Chagin int error;
1747f8a66150SDmitry Chagin
1748f8a66150SDmitry Chagin error = bsd_to_linux_sockaddr(*data, &lsa, *datalen);
1749f8a66150SDmitry Chagin if (error == 0) {
1750f8a66150SDmitry Chagin *data = *udata = lsa;
1751f8a66150SDmitry Chagin *datalen = sizeof(*lsa);
1752f8a66150SDmitry Chagin }
1753f8a66150SDmitry Chagin return (error);
1754f8a66150SDmitry Chagin }
1755f8a66150SDmitry Chagin
1756f8a66150SDmitry Chagin static int
recvmsg_scm_ipproto_ip(l_int msg_type,l_int lmsg_type,socklen_t * datalen,void ** data,void ** udata)1757de20eb26SDmitry Chagin recvmsg_scm_ipproto_ip(l_int msg_type, l_int lmsg_type, socklen_t *datalen,
1758de20eb26SDmitry Chagin void **data, void **udata)
1759de20eb26SDmitry Chagin {
1760de20eb26SDmitry Chagin int error;
1761de20eb26SDmitry Chagin
1762de20eb26SDmitry Chagin error = 0;
1763de20eb26SDmitry Chagin switch (msg_type) {
1764de20eb26SDmitry Chagin case IP_ORIGDSTADDR:
1765de20eb26SDmitry Chagin error = recvmsg_scm_ip_origdstaddr(datalen, data,
1766de20eb26SDmitry Chagin udata);
1767de20eb26SDmitry Chagin break;
1768de20eb26SDmitry Chagin }
1769de20eb26SDmitry Chagin
1770de20eb26SDmitry Chagin return (error);
1771de20eb26SDmitry Chagin }
1772de20eb26SDmitry Chagin
1773de20eb26SDmitry Chagin static int
linux_recvmsg_common(struct thread * td,l_int s,struct l_msghdr * msghdr,l_uint flags,struct msghdr * msg)1774e1ff74c0SDmitry Chagin linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
1775e1ff74c0SDmitry Chagin l_uint flags, struct msghdr *msg)
177640dbba57SAssar Westerlund {
17770e26e54bSDmitry Chagin struct proc *p = td->td_proc;
177874f5d680SKonstantin Belousov struct cmsghdr *cm;
17790eda2ceaSDmitry Chagin struct l_cmsghdr *lcm = NULL;
1780c7902fbeSMark Johnston socklen_t datalen, maxlen, outlen;
17810eda2ceaSDmitry Chagin struct l_msghdr l_msghdr;
178274f5d680SKonstantin Belousov struct iovec *iov, *uiov;
17833735f9cfSDmitry Chagin struct mbuf *m, *control = NULL;
178474f5d680SKonstantin Belousov struct mbuf **controlp;
1785d5368bf3SDmitry Chagin struct sockaddr *sa;
178674f5d680SKonstantin Belousov caddr_t outbuf;
1787b408788dSDmitry Chagin void *data, *udata;
1788bbaa5523SDmitry Chagin int error, skiped;
178940dbba57SAssar Westerlund
17900eda2ceaSDmitry Chagin error = copyin(msghdr, &l_msghdr, sizeof(l_msghdr));
1791e1ff74c0SDmitry Chagin if (error != 0)
1792ca26842eSHajimu UMEMOTO return (error);
1793ca26842eSHajimu UMEMOTO
179496c524d8SAlexander V. Chernikov /*
179596c524d8SAlexander V. Chernikov * Pass user-supplied recvmsg() flags in msg_flags field,
179696c524d8SAlexander V. Chernikov * following sys_recvmsg() convention.
179796c524d8SAlexander V. Chernikov */
17980eda2ceaSDmitry Chagin l_msghdr.msg_flags = flags;
179996c524d8SAlexander V. Chernikov
18000eda2ceaSDmitry Chagin error = linux_to_bsd_msghdr(msg, &l_msghdr);
1801e1ff74c0SDmitry Chagin if (error != 0)
180274f5d680SKonstantin Belousov return (error);
180374f5d680SKonstantin Belousov
180474f5d680SKonstantin Belousov #ifdef COMPAT_LINUX32
1805524c9accSDmitry Chagin error = freebsd32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
180674f5d680SKonstantin Belousov &iov, EMSGSIZE);
180774f5d680SKonstantin Belousov #else
1808e1ff74c0SDmitry Chagin error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
180974f5d680SKonstantin Belousov #endif
1810e1ff74c0SDmitry Chagin if (error != 0)
181174f5d680SKonstantin Belousov return (error);
181274f5d680SKonstantin Belousov
18134d2b7be5SEdward Tomasz Napierala if (msg->msg_name != NULL && msg->msg_namelen > 0) {
18144d2b7be5SEdward Tomasz Napierala msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
1815d5368bf3SDmitry Chagin sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
1816d5368bf3SDmitry Chagin msg->msg_name = sa;
18174d2b7be5SEdward Tomasz Napierala } else {
18188128cfc5SDmitry Chagin sa = NULL;
18194d2b7be5SEdward Tomasz Napierala msg->msg_name = NULL;
18204d2b7be5SEdward Tomasz Napierala }
182184b11cd8SMitsuru IWASAKI
1822e1ff74c0SDmitry Chagin uiov = msg->msg_iov;
1823e1ff74c0SDmitry Chagin msg->msg_iov = iov;
1824e1ff74c0SDmitry Chagin controlp = (msg->msg_control != NULL) ? &control : NULL;
1825d5368bf3SDmitry Chagin error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
1826e1ff74c0SDmitry Chagin msg->msg_iov = uiov;
1827e1ff74c0SDmitry Chagin if (error != 0)
182874f5d680SKonstantin Belousov goto bad;
182974f5d680SKonstantin Belousov
18304d2b7be5SEdward Tomasz Napierala /*
18314d2b7be5SEdward Tomasz Napierala * Note that kern_recvit() updates msg->msg_namelen.
18324d2b7be5SEdward Tomasz Napierala */
18334d2b7be5SEdward Tomasz Napierala if (msg->msg_name != NULL && msg->msg_namelen > 0) {
18340eda2ceaSDmitry Chagin msg->msg_name = PTRIN(l_msghdr.msg_name);
183568bfaefbSDmitry Chagin error = linux_copyout_sockaddr(sa, msg->msg_name,
183668bfaefbSDmitry Chagin msg->msg_namelen);
1837d5368bf3SDmitry Chagin if (error != 0)
1838d5368bf3SDmitry Chagin goto bad;
1839d5368bf3SDmitry Chagin }
1840d5368bf3SDmitry Chagin
18410eda2ceaSDmitry Chagin error = bsd_to_linux_msghdr(msg, &l_msghdr);
1842e1ff74c0SDmitry Chagin if (error != 0)
184374f5d680SKonstantin Belousov goto bad;
184474f5d680SKonstantin Belousov
18452467ccddSDmitry Chagin skiped = outlen = 0;
18460eda2ceaSDmitry Chagin maxlen = l_msghdr.msg_controllen;
18470eda2ceaSDmitry Chagin if (control == NULL)
18480eda2ceaSDmitry Chagin goto out;
1849605da56bSAndriy Gapon
18500eda2ceaSDmitry Chagin lcm = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
1851e1ff74c0SDmitry Chagin msg->msg_control = mtod(control, struct cmsghdr *);
1852e1ff74c0SDmitry Chagin msg->msg_controllen = control->m_len;
18530eda2ceaSDmitry Chagin outbuf = PTRIN(l_msghdr.msg_control);
18543735f9cfSDmitry Chagin for (m = control; m != NULL; m = m->m_next) {
18553735f9cfSDmitry Chagin cm = mtod(m, struct cmsghdr *);
1856f8a66150SDmitry Chagin lcm->cmsg_type = bsd_to_linux_cmsg_type(p, cm->cmsg_type,
1857f8a66150SDmitry Chagin cm->cmsg_level);
18580eda2ceaSDmitry Chagin lcm->cmsg_level = bsd_to_linux_sockopt_level(cm->cmsg_level);
1859f8a66150SDmitry Chagin
18600eda2ceaSDmitry Chagin if (lcm->cmsg_type == -1 ||
1861bbaa5523SDmitry Chagin cm->cmsg_level == -1) {
186231d7f3e9SDmitry Chagin LINUX_RATELIMIT_MSG_OPT2(
1863889cd285SEdward Tomasz Napierala "unsupported recvmsg cmsg level %d type %d",
1864889cd285SEdward Tomasz Napierala cm->cmsg_level, cm->cmsg_type);
1865bbaa5523SDmitry Chagin /* Skip unsupported messages */
1866bbaa5523SDmitry Chagin skiped++;
1867bbaa5523SDmitry Chagin continue;
186874f5d680SKonstantin Belousov }
1869de20eb26SDmitry Chagin data = CMSG_DATA(cm);
1870de20eb26SDmitry Chagin datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
1871de20eb26SDmitry Chagin udata = NULL;
1872de20eb26SDmitry Chagin error = 0;
1873605da56bSAndriy Gapon
1874de20eb26SDmitry Chagin switch (cm->cmsg_level) {
1875de20eb26SDmitry Chagin case IPPROTO_IP:
1876de20eb26SDmitry Chagin error = recvmsg_scm_ipproto_ip(cm->cmsg_type,
1877de20eb26SDmitry Chagin lcm->cmsg_type, &datalen, &data, &udata);
1878605da56bSAndriy Gapon break;
1879de20eb26SDmitry Chagin case SOL_SOCKET:
1880de20eb26SDmitry Chagin error = recvmsg_scm_sol_socket(td, cm->cmsg_type,
1881de20eb26SDmitry Chagin lcm->cmsg_type, flags, &datalen, &data, &udata);
188271bc8bcfSDmitry Chagin break;
1883605da56bSAndriy Gapon }
1884f8a66150SDmitry Chagin
18859d0c9b6dSDmitry Chagin /* The recvmsg_scm_ is responsible to free udata on error. */
1886b408788dSDmitry Chagin if (error != 0)
1887b408788dSDmitry Chagin goto bad;
1888605da56bSAndriy Gapon
18897d561928SDmitry Chagin if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
189074f5d680SKonstantin Belousov if (outlen == 0) {
189174f5d680SKonstantin Belousov error = EMSGSIZE;
1892b408788dSDmitry Chagin goto err;
189374f5d680SKonstantin Belousov } else {
18940eda2ceaSDmitry Chagin l_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
1895c7902fbeSMark Johnston m_dispose_extcontrolm(control);
1896b408788dSDmitry Chagin free(udata, M_LINUX);
189774f5d680SKonstantin Belousov goto out;
189874f5d680SKonstantin Belousov }
189974f5d680SKonstantin Belousov }
190074f5d680SKonstantin Belousov
19010eda2ceaSDmitry Chagin lcm->cmsg_len = LINUX_CMSG_LEN(datalen);
19020eda2ceaSDmitry Chagin error = copyout(lcm, outbuf, L_CMSG_HDRSZ);
1903b408788dSDmitry Chagin if (error == 0) {
1904a21238d8SDmitry Chagin error = copyout(data, LINUX_CMSG_DATA(outbuf), datalen);
1905b408788dSDmitry Chagin if (error == 0) {
1906a21238d8SDmitry Chagin outbuf += LINUX_CMSG_SPACE(datalen);
190767116c69SDmitry Chagin outlen += LINUX_CMSG_SPACE(datalen);
190874f5d680SKonstantin Belousov }
1909b408788dSDmitry Chagin }
1910b408788dSDmitry Chagin err:
1911b408788dSDmitry Chagin free(udata, M_LINUX);
1912b408788dSDmitry Chagin if (error != 0)
1913b408788dSDmitry Chagin goto bad;
1914b408788dSDmitry Chagin }
1915bbaa5523SDmitry Chagin if (outlen == 0 && skiped > 0) {
1916bbaa5523SDmitry Chagin error = EINVAL;
1917bbaa5523SDmitry Chagin goto bad;
1918bbaa5523SDmitry Chagin }
191974f5d680SKonstantin Belousov
192074f5d680SKonstantin Belousov out:
19212467ccddSDmitry Chagin l_msghdr.msg_controllen = outlen;
19220eda2ceaSDmitry Chagin error = copyout(&l_msghdr, msghdr, sizeof(l_msghdr));
192374f5d680SKonstantin Belousov
192474f5d680SKonstantin Belousov bad:
1925c7902fbeSMark Johnston if (control != NULL) {
1926c7902fbeSMark Johnston if (error != 0)
1927c7902fbeSMark Johnston m_dispose_extcontrolm(control);
192874f5d680SKonstantin Belousov m_freem(control);
1929c7902fbeSMark Johnston }
1930c7902fbeSMark Johnston free(iov, M_IOV);
19310eda2ceaSDmitry Chagin free(lcm, M_LINUX);
19328128cfc5SDmitry Chagin free(sa, M_SONAME);
193374f5d680SKonstantin Belousov
1934ca26842eSHajimu UMEMOTO return (error);
193540dbba57SAssar Westerlund }
193640dbba57SAssar Westerlund
1937a12b9b3dSDmitry Chagin int
linux_recvmsg(struct thread * td,struct linux_recvmsg_args * args)1938e1ff74c0SDmitry Chagin linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
1939e1ff74c0SDmitry Chagin {
1940e1ff74c0SDmitry Chagin struct msghdr bsd_msg;
19413a99aac6SDmitry Chagin struct file *fp;
19423a99aac6SDmitry Chagin int error;
1943e1ff74c0SDmitry Chagin
19443212ad15SMateusz Guzik error = getsock(td, args->s, &cap_recv_rights, &fp);
19453a99aac6SDmitry Chagin if (error != 0)
19463a99aac6SDmitry Chagin return (error);
19473a99aac6SDmitry Chagin fdrop(fp, td);
1948e1ff74c0SDmitry Chagin return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
1949e1ff74c0SDmitry Chagin args->flags, &bsd_msg));
1950e1ff74c0SDmitry Chagin }
1951e1ff74c0SDmitry Chagin
19521744f14eSDmitry Chagin static int
linux_recvmmsg_common(struct thread * td,l_int s,struct l_mmsghdr * msg,l_uint vlen,l_uint flags,struct timespec * tts)19531744f14eSDmitry Chagin linux_recvmmsg_common(struct thread *td, l_int s, struct l_mmsghdr *msg,
19541744f14eSDmitry Chagin l_uint vlen, l_uint flags, struct timespec *tts)
1955e1ff74c0SDmitry Chagin {
1956e1ff74c0SDmitry Chagin struct msghdr bsd_msg;
19571744f14eSDmitry Chagin struct timespec ts;
19583a99aac6SDmitry Chagin struct file *fp;
1959e1ff74c0SDmitry Chagin l_uint retval;
1960e1ff74c0SDmitry Chagin int error, datagrams;
1961e1ff74c0SDmitry Chagin
19623212ad15SMateusz Guzik error = getsock(td, s, &cap_recv_rights, &fp);
19633a99aac6SDmitry Chagin if (error != 0)
19643a99aac6SDmitry Chagin return (error);
1965e1ff74c0SDmitry Chagin datagrams = 0;
19661744f14eSDmitry Chagin while (datagrams < vlen) {
19671744f14eSDmitry Chagin error = linux_recvmsg_common(td, s, &msg->msg_hdr,
19681744f14eSDmitry Chagin flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
1969e1ff74c0SDmitry Chagin if (error != 0)
1970e1ff74c0SDmitry Chagin break;
1971e1ff74c0SDmitry Chagin
1972e1ff74c0SDmitry Chagin retval = td->td_retval[0];
1973e1ff74c0SDmitry Chagin error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
1974e1ff74c0SDmitry Chagin if (error != 0)
1975e1ff74c0SDmitry Chagin break;
1976e1ff74c0SDmitry Chagin ++msg;
1977e1ff74c0SDmitry Chagin ++datagrams;
1978e1ff74c0SDmitry Chagin
1979e1ff74c0SDmitry Chagin /*
1980e1ff74c0SDmitry Chagin * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
1981e1ff74c0SDmitry Chagin */
19821744f14eSDmitry Chagin if (flags & LINUX_MSG_WAITFORONE)
19831744f14eSDmitry Chagin flags |= LINUX_MSG_DONTWAIT;
1984e1ff74c0SDmitry Chagin
1985e1ff74c0SDmitry Chagin /*
1986e1ff74c0SDmitry Chagin * See BUGS section of recvmmsg(2).
1987e1ff74c0SDmitry Chagin */
19881744f14eSDmitry Chagin if (tts) {
1989e1ff74c0SDmitry Chagin getnanotime(&ts);
19901744f14eSDmitry Chagin timespecsub(&ts, tts, &ts);
1991e1ff74c0SDmitry Chagin if (!timespecisset(&ts) || ts.tv_sec > 0)
1992e1ff74c0SDmitry Chagin break;
1993e1ff74c0SDmitry Chagin }
1994e1ff74c0SDmitry Chagin /* Out of band data, return right away. */
1995e1ff74c0SDmitry Chagin if (bsd_msg.msg_flags & MSG_OOB)
1996e1ff74c0SDmitry Chagin break;
1997e1ff74c0SDmitry Chagin }
1998e1ff74c0SDmitry Chagin if (error == 0)
1999e1ff74c0SDmitry Chagin td->td_retval[0] = datagrams;
20003a99aac6SDmitry Chagin fdrop(fp, td);
2001e1ff74c0SDmitry Chagin return (error);
2002e1ff74c0SDmitry Chagin }
2003e1ff74c0SDmitry Chagin
2004e1ff74c0SDmitry Chagin int
linux_recvmmsg(struct thread * td,struct linux_recvmmsg_args * args)20051744f14eSDmitry Chagin linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
20061744f14eSDmitry Chagin {
20071744f14eSDmitry Chagin struct timespec ts, tts, *ptts;
20081744f14eSDmitry Chagin int error;
20091744f14eSDmitry Chagin
20101744f14eSDmitry Chagin if (args->timeout) {
2011707e567aSDmitry Chagin error = linux_get_timespec(&ts, args->timeout);
20121744f14eSDmitry Chagin if (error != 0)
20131744f14eSDmitry Chagin return (error);
20141744f14eSDmitry Chagin getnanotime(&tts);
20151744f14eSDmitry Chagin timespecadd(&tts, &ts, &tts);
20161744f14eSDmitry Chagin ptts = &tts;
20171744f14eSDmitry Chagin }
20181744f14eSDmitry Chagin else ptts = NULL;
20191744f14eSDmitry Chagin
20201744f14eSDmitry Chagin return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
20211744f14eSDmitry Chagin args->vlen, args->flags, ptts));
20221744f14eSDmitry Chagin }
20231744f14eSDmitry Chagin
20241744f14eSDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
20251744f14eSDmitry Chagin int
linux_recvmmsg_time64(struct thread * td,struct linux_recvmmsg_time64_args * args)20261744f14eSDmitry Chagin linux_recvmmsg_time64(struct thread *td, struct linux_recvmmsg_time64_args *args)
20271744f14eSDmitry Chagin {
20281744f14eSDmitry Chagin struct timespec ts, tts, *ptts;
20291744f14eSDmitry Chagin int error;
20301744f14eSDmitry Chagin
20311744f14eSDmitry Chagin if (args->timeout) {
2032707e567aSDmitry Chagin error = linux_get_timespec64(&ts, args->timeout);
20331744f14eSDmitry Chagin if (error != 0)
20341744f14eSDmitry Chagin return (error);
20351744f14eSDmitry Chagin getnanotime(&tts);
20361744f14eSDmitry Chagin timespecadd(&tts, &ts, &tts);
20371744f14eSDmitry Chagin ptts = &tts;
20381744f14eSDmitry Chagin }
20391744f14eSDmitry Chagin else ptts = NULL;
20401744f14eSDmitry Chagin
20411744f14eSDmitry Chagin return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
20421744f14eSDmitry Chagin args->vlen, args->flags, ptts));
20431744f14eSDmitry Chagin }
20441744f14eSDmitry Chagin #endif
20451744f14eSDmitry Chagin
20461744f14eSDmitry Chagin int
linux_shutdown(struct thread * td,struct linux_shutdown_args * args)2047b40ce416SJulian Elischer linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
2048c21dee17SSøren Schmidt {
2049c21dee17SSøren Schmidt
2050d293f35cSEdward Tomasz Napierala return (kern_shutdown(td, args->s, args->how));
2051c21dee17SSøren Schmidt }
2052c21dee17SSøren Schmidt
2053a12b9b3dSDmitry Chagin int
linux_setsockopt(struct thread * td,struct linux_setsockopt_args * args)2054b40ce416SJulian Elischer linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
2055c21dee17SSøren Schmidt {
20560e26e54bSDmitry Chagin struct proc *p = td->td_proc;
20570e26e54bSDmitry Chagin struct linux_pemuldata *pem;
205803cc95d2SDmitry Chagin l_timeval linux_tv;
2059d5368bf3SDmitry Chagin struct sockaddr *sa;
206003cc95d2SDmitry Chagin struct timeval tv;
2061d5368bf3SDmitry Chagin socklen_t len;
206253494b91SDmitry Chagin int error, level, name, val;
2063c21dee17SSøren Schmidt
20649c6eb0f9SEdward Tomasz Napierala level = linux_to_bsd_sockopt_level(args->level);
20659c6eb0f9SEdward Tomasz Napierala switch (level) {
2066c21dee17SSøren Schmidt case SOL_SOCKET:
2067745aaef5SKonstantin Belousov name = linux_to_bsd_so_sockopt(args->optname);
206803cc95d2SDmitry Chagin switch (name) {
2069443d8a07SConrad Meyer case LOCAL_CREDS_PERSISTENT:
2070443d8a07SConrad Meyer level = SOL_LOCAL;
2071443d8a07SConrad Meyer break;
207203cc95d2SDmitry Chagin case SO_RCVTIMEO:
207303cc95d2SDmitry Chagin /* FALLTHROUGH */
207403cc95d2SDmitry Chagin case SO_SNDTIMEO:
207503cc95d2SDmitry Chagin error = copyin(PTRIN(args->optval), &linux_tv,
207603cc95d2SDmitry Chagin sizeof(linux_tv));
2077dddb7e7fSDmitry Chagin if (error != 0)
207803cc95d2SDmitry Chagin return (error);
207903cc95d2SDmitry Chagin tv.tv_sec = linux_tv.tv_sec;
208003cc95d2SDmitry Chagin tv.tv_usec = linux_tv.tv_usec;
20819c6eb0f9SEdward Tomasz Napierala return (kern_setsockopt(td, args->s, level,
208203cc95d2SDmitry Chagin name, &tv, UIO_SYSSPACE, sizeof(tv)));
208303cc95d2SDmitry Chagin /* NOTREACHED */
20840e26e54bSDmitry Chagin case SO_TIMESTAMP:
208553494b91SDmitry Chagin /* overwrite SO_BINTIME */
208653494b91SDmitry Chagin val = 0;
208753494b91SDmitry Chagin error = kern_setsockopt(td, args->s, level,
208853494b91SDmitry Chagin SO_BINTIME, &val, UIO_SYSSPACE, sizeof(val));
208953494b91SDmitry Chagin if (error != 0)
209053494b91SDmitry Chagin return (error);
20910e26e54bSDmitry Chagin pem = pem_find(p);
20920e26e54bSDmitry Chagin pem->so_timestamp = args->optname;
20930e26e54bSDmitry Chagin break;
209471bc8bcfSDmitry Chagin case SO_BINTIME:
209553494b91SDmitry Chagin /* overwrite SO_TIMESTAMP */
209653494b91SDmitry Chagin val = 0;
209753494b91SDmitry Chagin error = kern_setsockopt(td, args->s, level,
209853494b91SDmitry Chagin SO_TIMESTAMP, &val, UIO_SYSSPACE, sizeof(val));
209953494b91SDmitry Chagin if (error != 0)
210053494b91SDmitry Chagin return (error);
210171bc8bcfSDmitry Chagin pem = pem_find(p);
210271bc8bcfSDmitry Chagin pem->so_timestampns = args->optname;
210371bc8bcfSDmitry Chagin break;
210403cc95d2SDmitry Chagin default:
210503cc95d2SDmitry Chagin break;
210603cc95d2SDmitry Chagin }
2107c21dee17SSøren Schmidt break;
2108c21dee17SSøren Schmidt case IPPROTO_IP:
2109da6d8ae6SEdward Tomasz Napierala if (args->optname == LINUX_IP_RECVERR &&
2110da6d8ae6SEdward Tomasz Napierala linux_ignore_ip_recverr) {
2111da6d8ae6SEdward Tomasz Napierala /*
2112da6d8ae6SEdward Tomasz Napierala * XXX: This is a hack to unbreak DNS resolution
2113da6d8ae6SEdward Tomasz Napierala * with glibc 2.30 and above.
2114da6d8ae6SEdward Tomasz Napierala */
2115da6d8ae6SEdward Tomasz Napierala return (0);
2116da6d8ae6SEdward Tomasz Napierala }
2117745aaef5SKonstantin Belousov name = linux_to_bsd_ip_sockopt(args->optname);
2118c21dee17SSøren Schmidt break;
211986a9058bSAndrey V. Elsukov case IPPROTO_IPV6:
2120ca63710dSLexi Winter if (args->optname == LINUX_IPV6_RECVERR &&
2121ca63710dSLexi Winter linux_ignore_ip_recverr) {
2122ca63710dSLexi Winter /*
2123ca63710dSLexi Winter * XXX: This is a hack to unbreak DNS resolution
2124ca63710dSLexi Winter * with glibc 2.30 and above.
2125ca63710dSLexi Winter */
2126ca63710dSLexi Winter return (0);
2127ca63710dSLexi Winter }
212886a9058bSAndrey V. Elsukov name = linux_to_bsd_ip6_sockopt(args->optname);
212986a9058bSAndrey V. Elsukov break;
2130dad3b88aSMike Smith case IPPROTO_TCP:
2131fb709557SJohn Baldwin name = linux_to_bsd_tcp_sockopt(args->optname);
2132dad3b88aSMike Smith break;
21337c40e2d5SAlexander V. Chernikov case SOL_NETLINK:
21347c40e2d5SAlexander V. Chernikov name = args->optname;
21357c40e2d5SAlexander V. Chernikov break;
2136c21dee17SSøren Schmidt default:
21373f3a4815SMarcel Moolenaar name = -1;
21383f3a4815SMarcel Moolenaar break;
2139c21dee17SSøren Schmidt }
21408ec6c4a3SAlexander Leidinger if (name < 0) {
21418ec6c4a3SAlexander Leidinger if (name == -1)
21425d481ad8SEdward Tomasz Napierala linux_msg(curthread,
21435d481ad8SEdward Tomasz Napierala "unsupported setsockopt level %d optname %d",
21445d481ad8SEdward Tomasz Napierala args->level, args->optname);
2145d4b7423fSAlexander Leidinger return (ENOPROTOOPT);
21465d481ad8SEdward Tomasz Napierala }
2147d5368bf3SDmitry Chagin
2148d5368bf3SDmitry Chagin if (name == IPV6_NEXTHOP) {
2149d5368bf3SDmitry Chagin len = args->optlen;
2150d5368bf3SDmitry Chagin error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
2151d5368bf3SDmitry Chagin if (error != 0)
2152d5368bf3SDmitry Chagin return (error);
2153d5368bf3SDmitry Chagin
21549c6eb0f9SEdward Tomasz Napierala error = kern_setsockopt(td, args->s, level,
2155d5368bf3SDmitry Chagin name, sa, UIO_SYSSPACE, len);
2156d5368bf3SDmitry Chagin free(sa, M_SONAME);
2157d5368bf3SDmitry Chagin } else {
21589c6eb0f9SEdward Tomasz Napierala error = kern_setsockopt(td, args->s, level,
21599c6eb0f9SEdward Tomasz Napierala name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
2160d5368bf3SDmitry Chagin }
21615c8919adSAlexander Leidinger
21625c8919adSAlexander Leidinger return (error);
2163c21dee17SSøren Schmidt }
2164c21dee17SSøren Schmidt
2165e44a78ceSEdward Tomasz Napierala static int
linux_sockopt_copyout(struct thread * td,void * val,socklen_t len,struct linux_getsockopt_args * args)2166e92b9a9eSDmitry Chagin linux_sockopt_copyout(struct thread *td, void *val, socklen_t len,
2167e92b9a9eSDmitry Chagin struct linux_getsockopt_args *args)
2168e92b9a9eSDmitry Chagin {
2169e92b9a9eSDmitry Chagin int error;
2170e92b9a9eSDmitry Chagin
2171e92b9a9eSDmitry Chagin error = copyout(val, PTRIN(args->optval), len);
2172e92b9a9eSDmitry Chagin if (error == 0)
2173e92b9a9eSDmitry Chagin error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2174e92b9a9eSDmitry Chagin return (error);
2175e92b9a9eSDmitry Chagin }
2176e92b9a9eSDmitry Chagin
2177e92b9a9eSDmitry Chagin static int
linux_getsockopt_so_peergroups(struct thread * td,struct linux_getsockopt_args * args)2178cd84c82cSEdward Tomasz Napierala linux_getsockopt_so_peergroups(struct thread *td,
2179cd84c82cSEdward Tomasz Napierala struct linux_getsockopt_args *args)
2180cd84c82cSEdward Tomasz Napierala {
2181cd84c82cSEdward Tomasz Napierala struct xucred xu;
2182cd84c82cSEdward Tomasz Napierala socklen_t xulen, len;
2183cd84c82cSEdward Tomasz Napierala int error, i;
2184cd84c82cSEdward Tomasz Napierala
2185cd84c82cSEdward Tomasz Napierala xulen = sizeof(xu);
2186cd84c82cSEdward Tomasz Napierala error = kern_getsockopt(td, args->s, 0,
2187cd84c82cSEdward Tomasz Napierala LOCAL_PEERCRED, &xu, UIO_SYSSPACE, &xulen);
2188cd84c82cSEdward Tomasz Napierala if (error != 0)
2189cd84c82cSEdward Tomasz Napierala return (error);
2190cd84c82cSEdward Tomasz Napierala
2191cd84c82cSEdward Tomasz Napierala len = xu.cr_ngroups * sizeof(l_gid_t);
2192cd84c82cSEdward Tomasz Napierala if (args->optlen < len) {
2193cd84c82cSEdward Tomasz Napierala error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2194cd84c82cSEdward Tomasz Napierala if (error == 0)
2195cd84c82cSEdward Tomasz Napierala error = ERANGE;
2196cd84c82cSEdward Tomasz Napierala return (error);
2197cd84c82cSEdward Tomasz Napierala }
2198cd84c82cSEdward Tomasz Napierala
2199cd84c82cSEdward Tomasz Napierala /*
2200cd84c82cSEdward Tomasz Napierala * "- 1" to skip the primary group.
2201cd84c82cSEdward Tomasz Napierala */
2202cd84c82cSEdward Tomasz Napierala for (i = 0; i < xu.cr_ngroups - 1; i++) {
2203cd84c82cSEdward Tomasz Napierala error = copyout(xu.cr_groups + i + 1,
2204cd84c82cSEdward Tomasz Napierala (void *)(args->optval + i * sizeof(l_gid_t)),
2205cd84c82cSEdward Tomasz Napierala sizeof(l_gid_t));
2206cd84c82cSEdward Tomasz Napierala if (error != 0)
2207cd84c82cSEdward Tomasz Napierala return (error);
2208cd84c82cSEdward Tomasz Napierala }
2209cd84c82cSEdward Tomasz Napierala
2210cd84c82cSEdward Tomasz Napierala error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2211cd84c82cSEdward Tomasz Napierala return (error);
2212cd84c82cSEdward Tomasz Napierala }
2213cd84c82cSEdward Tomasz Napierala
2214cd84c82cSEdward Tomasz Napierala static int
linux_getsockopt_so_peersec(struct thread * td,struct linux_getsockopt_args * args)2215e44a78ceSEdward Tomasz Napierala linux_getsockopt_so_peersec(struct thread *td,
2216e44a78ceSEdward Tomasz Napierala struct linux_getsockopt_args *args)
2217e44a78ceSEdward Tomasz Napierala {
2218e44a78ceSEdward Tomasz Napierala socklen_t len;
2219e44a78ceSEdward Tomasz Napierala int error;
2220e44a78ceSEdward Tomasz Napierala
2221e44a78ceSEdward Tomasz Napierala len = sizeof(SECURITY_CONTEXT_STRING);
2222e44a78ceSEdward Tomasz Napierala if (args->optlen < len) {
2223e44a78ceSEdward Tomasz Napierala error = copyout(&len, PTRIN(args->optlen), sizeof(len));
2224e44a78ceSEdward Tomasz Napierala if (error == 0)
2225e44a78ceSEdward Tomasz Napierala error = ERANGE;
2226e44a78ceSEdward Tomasz Napierala return (error);
2227e44a78ceSEdward Tomasz Napierala }
2228e44a78ceSEdward Tomasz Napierala
2229e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, SECURITY_CONTEXT_STRING,
2230e92b9a9eSDmitry Chagin len, args));
2231e44a78ceSEdward Tomasz Napierala }
2232e44a78ceSEdward Tomasz Napierala
2233e8d9d808SDmitry Chagin static int
linux_getsockopt_so_linger(struct thread * td,struct linux_getsockopt_args * args)2234e8d9d808SDmitry Chagin linux_getsockopt_so_linger(struct thread *td,
2235e8d9d808SDmitry Chagin struct linux_getsockopt_args *args)
2236e8d9d808SDmitry Chagin {
2237e8d9d808SDmitry Chagin struct linger ling;
2238e8d9d808SDmitry Chagin socklen_t len;
2239e8d9d808SDmitry Chagin int error;
2240e8d9d808SDmitry Chagin
2241e8d9d808SDmitry Chagin len = sizeof(ling);
2242e8d9d808SDmitry Chagin error = kern_getsockopt(td, args->s, SOL_SOCKET,
2243e8d9d808SDmitry Chagin SO_LINGER, &ling, UIO_SYSSPACE, &len);
2244e8d9d808SDmitry Chagin if (error != 0)
2245e8d9d808SDmitry Chagin return (error);
2246e8d9d808SDmitry Chagin ling.l_onoff = ((ling.l_onoff & SO_LINGER) != 0);
2247e8d9d808SDmitry Chagin return (linux_sockopt_copyout(td, &ling, len, args));
2248e8d9d808SDmitry Chagin }
2249e92b9a9eSDmitry Chagin
2250a12b9b3dSDmitry Chagin int
linux_getsockopt(struct thread * td,struct linux_getsockopt_args * args)2251b40ce416SJulian Elischer linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
2252c21dee17SSøren Schmidt {
225303cc95d2SDmitry Chagin l_timeval linux_tv;
225403cc95d2SDmitry Chagin struct timeval tv;
2255d56e689eSDmitry Chagin socklen_t tv_len, xulen, len;
2256d5368bf3SDmitry Chagin struct sockaddr *sa;
2257d4dd69c4SDmitry Chagin struct xucred xu;
2258d4dd69c4SDmitry Chagin struct l_ucred lxu;
2259dfd060c0SEdward Tomasz Napierala int error, level, name, newval;
2260c21dee17SSøren Schmidt
2261dfd060c0SEdward Tomasz Napierala level = linux_to_bsd_sockopt_level(args->level);
2262dfd060c0SEdward Tomasz Napierala switch (level) {
2263c21dee17SSøren Schmidt case SOL_SOCKET:
2264cd84c82cSEdward Tomasz Napierala switch (args->optname) {
2265cd84c82cSEdward Tomasz Napierala case LINUX_SO_PEERGROUPS:
2266cd84c82cSEdward Tomasz Napierala return (linux_getsockopt_so_peergroups(td, args));
2267cd84c82cSEdward Tomasz Napierala case LINUX_SO_PEERSEC:
2268e44a78ceSEdward Tomasz Napierala return (linux_getsockopt_so_peersec(td, args));
2269cd84c82cSEdward Tomasz Napierala default:
2270cd84c82cSEdward Tomasz Napierala break;
2271cd84c82cSEdward Tomasz Napierala }
2272cd84c82cSEdward Tomasz Napierala
2273745aaef5SKonstantin Belousov name = linux_to_bsd_so_sockopt(args->optname);
227403cc95d2SDmitry Chagin switch (name) {
2275443d8a07SConrad Meyer case LOCAL_CREDS_PERSISTENT:
2276443d8a07SConrad Meyer level = SOL_LOCAL;
2277443d8a07SConrad Meyer break;
227803cc95d2SDmitry Chagin case SO_RCVTIMEO:
227903cc95d2SDmitry Chagin /* FALLTHROUGH */
228003cc95d2SDmitry Chagin case SO_SNDTIMEO:
228103cc95d2SDmitry Chagin tv_len = sizeof(tv);
2282dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level,
228303cc95d2SDmitry Chagin name, &tv, UIO_SYSSPACE, &tv_len);
2284dddb7e7fSDmitry Chagin if (error != 0)
228503cc95d2SDmitry Chagin return (error);
228603cc95d2SDmitry Chagin linux_tv.tv_sec = tv.tv_sec;
228703cc95d2SDmitry Chagin linux_tv.tv_usec = tv.tv_usec;
2288e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, &linux_tv,
2289e92b9a9eSDmitry Chagin sizeof(linux_tv), args));
229003cc95d2SDmitry Chagin /* NOTREACHED */
2291d4dd69c4SDmitry Chagin case LOCAL_PEERCRED:
2292cd92d27eSDmitry Chagin if (args->optlen < sizeof(lxu))
2293d4dd69c4SDmitry Chagin return (EINVAL);
2294bb376a99SMark Johnston /*
2295bb376a99SMark Johnston * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
2296bb376a99SMark Johnston * but by the Unix socket's level 0.
2297bb376a99SMark Johnston */
2298dfd060c0SEdward Tomasz Napierala level = 0;
2299d4dd69c4SDmitry Chagin xulen = sizeof(xu);
2300dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level,
2301d4dd69c4SDmitry Chagin name, &xu, UIO_SYSSPACE, &xulen);
2302dddb7e7fSDmitry Chagin if (error != 0)
2303d4dd69c4SDmitry Chagin return (error);
2304c5afec6eSDmitry Chagin lxu.pid = xu.cr_pid;
2305d4dd69c4SDmitry Chagin lxu.uid = xu.cr_uid;
2306d4dd69c4SDmitry Chagin lxu.gid = xu.cr_gid;
2307e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, &lxu,
2308e92b9a9eSDmitry Chagin sizeof(lxu), args));
2309d4dd69c4SDmitry Chagin /* NOTREACHED */
2310d56e689eSDmitry Chagin case SO_ERROR:
2311d56e689eSDmitry Chagin len = sizeof(newval);
2312dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level,
2313d56e689eSDmitry Chagin name, &newval, UIO_SYSSPACE, &len);
2314dddb7e7fSDmitry Chagin if (error != 0)
2315d56e689eSDmitry Chagin return (error);
2316866b1f51SEdward Tomasz Napierala newval = -bsd_to_linux_errno(newval);
2317e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, &newval,
2318e92b9a9eSDmitry Chagin len, args));
2319d56e689eSDmitry Chagin /* NOTREACHED */
2320cf312f79SDmitry Chagin case SO_DOMAIN:
2321cf312f79SDmitry Chagin len = sizeof(newval);
2322cf312f79SDmitry Chagin error = kern_getsockopt(td, args->s, level,
2323cf312f79SDmitry Chagin name, &newval, UIO_SYSSPACE, &len);
2324cf312f79SDmitry Chagin if (error != 0)
2325cf312f79SDmitry Chagin return (error);
23269d4a08d1SGleb Smirnoff newval = bsd_to_linux_domain((sa_family_t)newval);
23279d4a08d1SGleb Smirnoff if (newval == AF_UNKNOWN)
2328cf312f79SDmitry Chagin return (ENOPROTOOPT);
2329e92b9a9eSDmitry Chagin return (linux_sockopt_copyout(td, &newval,
2330e92b9a9eSDmitry Chagin len, args));
2331cf312f79SDmitry Chagin /* NOTREACHED */
2332e8d9d808SDmitry Chagin case SO_LINGER:
2333e8d9d808SDmitry Chagin return (linux_getsockopt_so_linger(td, args));
2334e8d9d808SDmitry Chagin /* NOTREACHED */
233503cc95d2SDmitry Chagin default:
233603cc95d2SDmitry Chagin break;
233703cc95d2SDmitry Chagin }
2338c21dee17SSøren Schmidt break;
2339c21dee17SSøren Schmidt case IPPROTO_IP:
2340745aaef5SKonstantin Belousov name = linux_to_bsd_ip_sockopt(args->optname);
2341c21dee17SSøren Schmidt break;
234286a9058bSAndrey V. Elsukov case IPPROTO_IPV6:
234386a9058bSAndrey V. Elsukov name = linux_to_bsd_ip6_sockopt(args->optname);
234486a9058bSAndrey V. Elsukov break;
2345dad3b88aSMike Smith case IPPROTO_TCP:
2346fb709557SJohn Baldwin name = linux_to_bsd_tcp_sockopt(args->optname);
2347dad3b88aSMike Smith break;
2348c21dee17SSøren Schmidt default:
23493f3a4815SMarcel Moolenaar name = -1;
23503f3a4815SMarcel Moolenaar break;
2351c21dee17SSøren Schmidt }
23528ec6c4a3SAlexander Leidinger if (name < 0) {
23538ec6c4a3SAlexander Leidinger if (name == -1)
23545d481ad8SEdward Tomasz Napierala linux_msg(curthread,
23555d481ad8SEdward Tomasz Napierala "unsupported getsockopt level %d optname %d",
23565d481ad8SEdward Tomasz Napierala args->level, args->optname);
23573f3a4815SMarcel Moolenaar return (EINVAL);
23585d481ad8SEdward Tomasz Napierala }
23593f3a4815SMarcel Moolenaar
23605c8919adSAlexander Leidinger if (name == IPV6_NEXTHOP) {
2361d5368bf3SDmitry Chagin error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2362d5368bf3SDmitry Chagin if (error != 0)
2363d5368bf3SDmitry Chagin return (error);
2364d5368bf3SDmitry Chagin sa = malloc(len, M_SONAME, M_WAITOK);
2365d5368bf3SDmitry Chagin
2366dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level,
2367d5368bf3SDmitry Chagin name, sa, UIO_SYSSPACE, &len);
2368d5368bf3SDmitry Chagin if (error != 0)
2369d5368bf3SDmitry Chagin goto out;
2370d5368bf3SDmitry Chagin
2371106a784bSEdward Tomasz Napierala error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
2372d5368bf3SDmitry Chagin if (error == 0)
2373d5368bf3SDmitry Chagin error = copyout(&len, PTRIN(args->optlen),
2374d5368bf3SDmitry Chagin sizeof(len));
2375d5368bf3SDmitry Chagin out:
2376d5368bf3SDmitry Chagin free(sa, M_SONAME);
2377d5368bf3SDmitry Chagin } else {
2378dfd060c0SEdward Tomasz Napierala if (args->optval) {
2379dfd060c0SEdward Tomasz Napierala error = copyin(PTRIN(args->optlen), &len, sizeof(len));
2380dfd060c0SEdward Tomasz Napierala if (error != 0)
2381dfd060c0SEdward Tomasz Napierala return (error);
2382dfd060c0SEdward Tomasz Napierala }
2383dfd060c0SEdward Tomasz Napierala error = kern_getsockopt(td, args->s, level,
2384dfd060c0SEdward Tomasz Napierala name, PTRIN(args->optval), UIO_USERSPACE, &len);
2385dfd060c0SEdward Tomasz Napierala if (error == 0)
2386dfd060c0SEdward Tomasz Napierala error = copyout(&len, PTRIN(args->optlen),
2387dfd060c0SEdward Tomasz Napierala sizeof(len));
2388d5368bf3SDmitry Chagin }
23895c8919adSAlexander Leidinger
23905c8919adSAlexander Leidinger return (error);
2391c21dee17SSøren Schmidt }
2392c21dee17SSøren Schmidt
2393bb66c597SJames McLaughlin /*
2394bb66c597SJames McLaughlin * Based on sendfile_getsock from kern_sendfile.c
2395bb66c597SJames McLaughlin * Determines whether an fd is a stream socket that can be used
2396bb66c597SJames McLaughlin * with FreeBSD sendfile.
2397bb66c597SJames McLaughlin */
2398bb66c597SJames McLaughlin static bool
is_sendfile(struct file * fp,struct file * ofp)2399da5a6738SDmitry Chagin is_sendfile(struct file *fp, struct file *ofp)
2400bb66c597SJames McLaughlin {
2401bb66c597SJames McLaughlin struct socket *so;
2402bb66c597SJames McLaughlin
2403bb66c597SJames McLaughlin /*
2404da5a6738SDmitry Chagin * FreeBSD sendfile() system call sends a regular file or
2405da5a6738SDmitry Chagin * shared memory object out a stream socket.
2406da5a6738SDmitry Chagin */
2407da5a6738SDmitry Chagin if ((fp->f_type != DTYPE_SHM && fp->f_type != DTYPE_VNODE) ||
2408da5a6738SDmitry Chagin (fp->f_type == DTYPE_VNODE &&
2409da5a6738SDmitry Chagin (fp->f_vnode == NULL || fp->f_vnode->v_type != VREG)))
2410da5a6738SDmitry Chagin return (false);
2411da5a6738SDmitry Chagin /*
2412bb66c597SJames McLaughlin * The socket must be a stream socket and connected.
2413bb66c597SJames McLaughlin */
2414da5a6738SDmitry Chagin if (ofp->f_type != DTYPE_SOCKET)
2415bb66c597SJames McLaughlin return (false);
2416da5a6738SDmitry Chagin so = ofp->f_data;
2417bb66c597SJames McLaughlin if (so->so_type != SOCK_STREAM)
2418bb66c597SJames McLaughlin return (false);
2419bb66c597SJames McLaughlin /*
2420bb66c597SJames McLaughlin * SCTP one-to-one style sockets currently don't work with
2421bb66c597SJames McLaughlin * sendfile().
2422bb66c597SJames McLaughlin */
2423bb66c597SJames McLaughlin if (so->so_proto->pr_protocol == IPPROTO_SCTP)
2424bb66c597SJames McLaughlin return (false);
2425bb66c597SJames McLaughlin return (!SOLISTENING(so));
2426bb66c597SJames McLaughlin }
2427bb66c597SJames McLaughlin
2428bb66c597SJames McLaughlin static bool
is_regular_file(struct file * fp)2429bb66c597SJames McLaughlin is_regular_file(struct file *fp)
2430bb66c597SJames McLaughlin {
2431bb66c597SJames McLaughlin
2432bb66c597SJames McLaughlin return (fp->f_type == DTYPE_VNODE && fp->f_vnode != NULL &&
2433bb66c597SJames McLaughlin fp->f_vnode->v_type == VREG);
2434bb66c597SJames McLaughlin }
2435bb66c597SJames McLaughlin
2436bb66c597SJames McLaughlin static int
sendfile_fallback(struct thread * td,struct file * fp,l_int out,off_t * offset,l_size_t count,off_t * sbytes)2437bb66c597SJames McLaughlin sendfile_fallback(struct thread *td, struct file *fp, l_int out,
2438bb66c597SJames McLaughlin off_t *offset, l_size_t count, off_t *sbytes)
2439bb66c597SJames McLaughlin {
2440bb66c597SJames McLaughlin off_t current_offset, out_offset, to_send;
2441bb66c597SJames McLaughlin l_size_t bytes_sent, n_read;
2442bb66c597SJames McLaughlin struct file *ofp;
2443bb66c597SJames McLaughlin struct iovec aiov;
2444bb66c597SJames McLaughlin struct uio auio;
2445bb66c597SJames McLaughlin bool seekable;
2446bb66c597SJames McLaughlin size_t bufsz;
2447bb66c597SJames McLaughlin void *buf;
2448bb66c597SJames McLaughlin int flags, error;
2449bb66c597SJames McLaughlin
2450bb66c597SJames McLaughlin if (offset == NULL) {
2451bb66c597SJames McLaughlin if ((error = fo_seek(fp, 0, SEEK_CUR, td)) != 0)
2452bb66c597SJames McLaughlin return (error);
2453bb66c597SJames McLaughlin current_offset = td->td_uretoff.tdu_off;
2454bb66c597SJames McLaughlin } else {
2455bb66c597SJames McLaughlin if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0)
2456bb66c597SJames McLaughlin return (ESPIPE);
2457bb66c597SJames McLaughlin current_offset = *offset;
2458bb66c597SJames McLaughlin }
2459bb66c597SJames McLaughlin error = fget_write(td, out, &cap_pwrite_rights, &ofp);
2460bb66c597SJames McLaughlin if (error != 0)
2461bb66c597SJames McLaughlin return (error);
2462bb66c597SJames McLaughlin seekable = (ofp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0;
2463bb66c597SJames McLaughlin if (seekable) {
2464bb66c597SJames McLaughlin if ((error = fo_seek(ofp, 0, SEEK_CUR, td)) != 0)
2465bb66c597SJames McLaughlin goto drop;
2466bb66c597SJames McLaughlin out_offset = td->td_uretoff.tdu_off;
2467bb66c597SJames McLaughlin } else
2468bb66c597SJames McLaughlin out_offset = 0;
2469bb66c597SJames McLaughlin
2470bb66c597SJames McLaughlin flags = FOF_OFFSET | FOF_NOUPDATE;
247113a5a46cSAndrew Gallatin bufsz = min(count, maxphys);
2472bb66c597SJames McLaughlin buf = malloc(bufsz, M_LINUX, M_WAITOK);
2473bb66c597SJames McLaughlin bytes_sent = 0;
2474bb66c597SJames McLaughlin while (bytes_sent < count) {
2475bb66c597SJames McLaughlin to_send = min(count - bytes_sent, bufsz);
2476bb66c597SJames McLaughlin aiov.iov_base = buf;
2477bb66c597SJames McLaughlin aiov.iov_len = bufsz;
2478bb66c597SJames McLaughlin auio.uio_iov = &aiov;
2479bb66c597SJames McLaughlin auio.uio_iovcnt = 1;
2480bb66c597SJames McLaughlin auio.uio_segflg = UIO_SYSSPACE;
2481bb66c597SJames McLaughlin auio.uio_td = td;
2482bb66c597SJames McLaughlin auio.uio_rw = UIO_READ;
2483bb66c597SJames McLaughlin auio.uio_offset = current_offset;
2484bb66c597SJames McLaughlin auio.uio_resid = to_send;
2485bb66c597SJames McLaughlin error = fo_read(fp, &auio, fp->f_cred, flags, td);
2486bb66c597SJames McLaughlin if (error != 0)
2487bb66c597SJames McLaughlin break;
2488bb66c597SJames McLaughlin n_read = to_send - auio.uio_resid;
2489bb66c597SJames McLaughlin if (n_read == 0)
2490bb66c597SJames McLaughlin break;
2491bb66c597SJames McLaughlin aiov.iov_base = buf;
2492bb66c597SJames McLaughlin aiov.iov_len = bufsz;
2493bb66c597SJames McLaughlin auio.uio_iov = &aiov;
2494bb66c597SJames McLaughlin auio.uio_iovcnt = 1;
2495bb66c597SJames McLaughlin auio.uio_segflg = UIO_SYSSPACE;
2496bb66c597SJames McLaughlin auio.uio_td = td;
2497bb66c597SJames McLaughlin auio.uio_rw = UIO_WRITE;
2498bb66c597SJames McLaughlin auio.uio_offset = (seekable) ? out_offset : 0;
2499bb66c597SJames McLaughlin auio.uio_resid = n_read;
2500bb66c597SJames McLaughlin error = fo_write(ofp, &auio, ofp->f_cred, flags, td);
2501bb66c597SJames McLaughlin if (error != 0)
2502bb66c597SJames McLaughlin break;
2503bb66c597SJames McLaughlin bytes_sent += n_read;
2504bb66c597SJames McLaughlin current_offset += n_read;
2505bb66c597SJames McLaughlin out_offset += n_read;
2506bb66c597SJames McLaughlin }
2507bb66c597SJames McLaughlin free(buf, M_LINUX);
2508bb66c597SJames McLaughlin
2509bb66c597SJames McLaughlin if (error == 0) {
2510bb66c597SJames McLaughlin *sbytes = bytes_sent;
2511bb66c597SJames McLaughlin if (offset != NULL)
2512bb66c597SJames McLaughlin *offset = current_offset;
2513bb66c597SJames McLaughlin else
2514bb66c597SJames McLaughlin error = fo_seek(fp, current_offset, SEEK_SET, td);
2515bb66c597SJames McLaughlin }
2516bb66c597SJames McLaughlin if (error == 0 && seekable)
2517bb66c597SJames McLaughlin error = fo_seek(ofp, out_offset, SEEK_SET, td);
2518bb66c597SJames McLaughlin
2519bb66c597SJames McLaughlin drop:
2520bb66c597SJames McLaughlin fdrop(ofp, td);
2521bb66c597SJames McLaughlin return (error);
2522bb66c597SJames McLaughlin }
2523bb66c597SJames McLaughlin
2524bb66c597SJames McLaughlin static int
sendfile_sendfile(struct thread * td,struct file * fp,l_int out,off_t * offset,l_size_t count,off_t * sbytes)2525bb66c597SJames McLaughlin sendfile_sendfile(struct thread *td, struct file *fp, l_int out,
2526bb66c597SJames McLaughlin off_t *offset, l_size_t count, off_t *sbytes)
2527bb66c597SJames McLaughlin {
2528bb66c597SJames McLaughlin off_t current_offset;
2529bb66c597SJames McLaughlin int error;
2530bb66c597SJames McLaughlin
2531bb66c597SJames McLaughlin if (offset == NULL) {
2532bb66c597SJames McLaughlin if ((fp->f_ops->fo_flags & DFLAG_SEEKABLE) == 0)
2533bb66c597SJames McLaughlin return (ESPIPE);
2534bb66c597SJames McLaughlin if ((error = fo_seek(fp, 0, SEEK_CUR, td)) != 0)
2535bb66c597SJames McLaughlin return (error);
2536bb66c597SJames McLaughlin current_offset = td->td_uretoff.tdu_off;
2537bb66c597SJames McLaughlin } else
2538bb66c597SJames McLaughlin current_offset = *offset;
2539bb66c597SJames McLaughlin error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
2540bb66c597SJames McLaughlin sbytes, 0, td);
2541*a43b745aSMark Johnston if (error == EAGAIN && *sbytes > 0) {
2542*a43b745aSMark Johnston /*
2543*a43b745aSMark Johnston * The socket is non-blocking and we didn't finish sending.
2544*a43b745aSMark Johnston * Squash the error, since that's what Linux does.
2545*a43b745aSMark Johnston */
2546*a43b745aSMark Johnston error = 0;
2547*a43b745aSMark Johnston }
2548bb66c597SJames McLaughlin if (error == 0) {
2549bb66c597SJames McLaughlin current_offset += *sbytes;
2550bb66c597SJames McLaughlin if (offset != NULL)
2551bb66c597SJames McLaughlin *offset = current_offset;
2552bb66c597SJames McLaughlin else
2553bb66c597SJames McLaughlin error = fo_seek(fp, current_offset, SEEK_SET, td);
2554bb66c597SJames McLaughlin }
2555bb66c597SJames McLaughlin return (error);
2556bb66c597SJames McLaughlin }
2557bb66c597SJames McLaughlin
2558fc7510aeSEd Maste static int
linux_sendfile_common(struct thread * td,l_int out,l_int in,off_t * offset,l_size_t count)2559fc7510aeSEd Maste linux_sendfile_common(struct thread *td, l_int out, l_int in,
25607307c439SDmitry Chagin off_t *offset, l_size_t count)
2561fc7510aeSEd Maste {
2562bb66c597SJames McLaughlin struct file *fp, *ofp;
2563bb66c597SJames McLaughlin off_t sbytes;
2564fc7510aeSEd Maste int error;
2565bb66c597SJames McLaughlin
2566bb66c597SJames McLaughlin /* Linux cannot have 0 count. */
2567bb66c597SJames McLaughlin if (count <= 0 || (offset != NULL && *offset < 0))
2568bb66c597SJames McLaughlin return (EINVAL);
2569fc7510aeSEd Maste
2570fc7510aeSEd Maste AUDIT_ARG_FD(in);
2571fc7510aeSEd Maste error = fget_read(td, in, &cap_pread_rights, &fp);
2572fc7510aeSEd Maste if (error != 0)
2573fc7510aeSEd Maste return (error);
2574bb66c597SJames McLaughlin if ((fp->f_type != DTYPE_SHM && fp->f_type != DTYPE_VNODE) ||
2575bb66c597SJames McLaughlin (fp->f_type == DTYPE_VNODE &&
2576bb66c597SJames McLaughlin (fp->f_vnode == NULL || fp->f_vnode->v_type != VREG))) {
2577fc7510aeSEd Maste error = EINVAL;
2578fc7510aeSEd Maste goto drop;
2579fc7510aeSEd Maste }
2580bb66c597SJames McLaughlin error = fget_unlocked(td, out, &cap_no_rights, &ofp);
2581fc7510aeSEd Maste if (error != 0)
2582fc7510aeSEd Maste goto drop;
2583fc7510aeSEd Maste
2584bb66c597SJames McLaughlin if (is_regular_file(fp) && is_regular_file(ofp)) {
2585bb66c597SJames McLaughlin error = kern_copy_file_range(td, in, offset, out, NULL, count,
2586bb66c597SJames McLaughlin 0);
2587fc7510aeSEd Maste } else {
2588bb66c597SJames McLaughlin sbytes = 0;
2589da5a6738SDmitry Chagin if (is_sendfile(fp, ofp))
2590bb66c597SJames McLaughlin error = sendfile_sendfile(td, fp, out, offset, count,
2591bb66c597SJames McLaughlin &sbytes);
2592bb66c597SJames McLaughlin else
2593bb66c597SJames McLaughlin error = sendfile_fallback(td, fp, out, offset, count,
2594bb66c597SJames McLaughlin &sbytes);
25954f9fac78SDmitry Chagin if (error == ENOBUFS && (ofp->f_flag & FNONBLOCK) != 0)
25964f9fac78SDmitry Chagin error = EAGAIN;
2597bb66c597SJames McLaughlin if (error == 0)
2598bb66c597SJames McLaughlin td->td_retval[0] = sbytes;
2599fc7510aeSEd Maste }
2600bb66c597SJames McLaughlin fdrop(ofp, td);
2601fc7510aeSEd Maste
2602fc7510aeSEd Maste drop:
2603fc7510aeSEd Maste fdrop(fp, td);
2604fc7510aeSEd Maste return (error);
2605fc7510aeSEd Maste }
2606fc7510aeSEd Maste
2607fc7510aeSEd Maste int
linux_sendfile(struct thread * td,struct linux_sendfile_args * arg)2608fc7510aeSEd Maste linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
2609fc7510aeSEd Maste {
2610fc7510aeSEd Maste /*
2611fc7510aeSEd Maste * Differences between FreeBSD and Linux sendfile:
2612fc7510aeSEd Maste * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
2613bb66c597SJames McLaughlin * mean send the whole file).
2614fc7510aeSEd Maste * - Linux can send to any fd whereas FreeBSD only supports sockets.
2615bb66c597SJames McLaughlin * We therefore use FreeBSD sendfile where possible for performance,
2616bb66c597SJames McLaughlin * but fall back on a manual copy (sendfile_fallback).
2617fc7510aeSEd Maste * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
2618fc7510aeSEd Maste * - Linux takes an offset pointer and updates it to the read location.
2619fc7510aeSEd Maste * FreeBSD takes in an offset and a 'bytes read' parameter which is
2620fc7510aeSEd Maste * only filled if it isn't NULL. We use this parameter to update the
2621fc7510aeSEd Maste * offset pointer if it exists.
2622fc7510aeSEd Maste * - Linux sendfile returns bytes read on success while FreeBSD
2623fc7510aeSEd Maste * returns 0. We use the 'bytes read' parameter to get this value.
2624fc7510aeSEd Maste */
2625fc7510aeSEd Maste
26267307c439SDmitry Chagin off_t offset64;
26277307c439SDmitry Chagin l_off_t offset;
2628fc7510aeSEd Maste int error;
2629fc7510aeSEd Maste
2630fc7510aeSEd Maste if (arg->offset != NULL) {
2631fc7510aeSEd Maste error = copyin(arg->offset, &offset, sizeof(offset));
2632fc7510aeSEd Maste if (error != 0)
2633fc7510aeSEd Maste return (error);
26347307c439SDmitry Chagin offset64 = offset;
2635fc7510aeSEd Maste }
2636fc7510aeSEd Maste
26374a521544SDmitry Chagin error = linux_sendfile_common(td, arg->out, arg->in,
2638fc7510aeSEd Maste arg->offset != NULL ? &offset64 : NULL, arg->count);
2639fc7510aeSEd Maste
26404a521544SDmitry Chagin if (error == 0 && arg->offset != NULL) {
26416ecab394SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2642fc7510aeSEd Maste if (offset64 > INT32_MAX)
2643fc7510aeSEd Maste return (EOVERFLOW);
2644fc7510aeSEd Maste #endif
26457307c439SDmitry Chagin offset = (l_off_t)offset64;
2646fc7510aeSEd Maste error = copyout(&offset, arg->offset, sizeof(offset));
2647fc7510aeSEd Maste }
2648fc7510aeSEd Maste
26494a521544SDmitry Chagin return (error);
2650fc7510aeSEd Maste }
2651fc7510aeSEd Maste
26526ecab394SDmitry Chagin #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
2653fc7510aeSEd Maste int
linux_sendfile64(struct thread * td,struct linux_sendfile64_args * arg)2654fc7510aeSEd Maste linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
2655fc7510aeSEd Maste {
26567307c439SDmitry Chagin off_t offset;
2657fc7510aeSEd Maste int error;
2658fc7510aeSEd Maste
2659fc7510aeSEd Maste if (arg->offset != NULL) {
2660fc7510aeSEd Maste error = copyin(arg->offset, &offset, sizeof(offset));
2661fc7510aeSEd Maste if (error != 0)
2662fc7510aeSEd Maste return (error);
2663fc7510aeSEd Maste }
2664fc7510aeSEd Maste
26654a521544SDmitry Chagin error = linux_sendfile_common(td, arg->out, arg->in,
2666fc7510aeSEd Maste arg->offset != NULL ? &offset : NULL, arg->count);
2667fc7510aeSEd Maste
26684a521544SDmitry Chagin if (error == 0 && arg->offset != NULL)
2669fc7510aeSEd Maste error = copyout(&offset, arg->offset, sizeof(offset));
2670fc7510aeSEd Maste
26714a521544SDmitry Chagin return (error);
2672fc7510aeSEd Maste }
26737f8f1d7fSDmitry Chagin
2674ea7b81d2SDmitry Chagin /* Argument list sizes for linux_socketcall */
2675abf20e93SDmitry Chagin static const unsigned char lxs_args_cnt[] = {
2676abf20e93SDmitry Chagin 0 /* unused*/, 3 /* socket */,
2677abf20e93SDmitry Chagin 3 /* bind */, 3 /* connect */,
2678abf20e93SDmitry Chagin 2 /* listen */, 3 /* accept */,
2679abf20e93SDmitry Chagin 3 /* getsockname */, 3 /* getpeername */,
2680abf20e93SDmitry Chagin 4 /* socketpair */, 4 /* send */,
2681abf20e93SDmitry Chagin 4 /* recv */, 6 /* sendto */,
2682abf20e93SDmitry Chagin 6 /* recvfrom */, 2 /* shutdown */,
2683abf20e93SDmitry Chagin 5 /* setsockopt */, 5 /* getsockopt */,
2684abf20e93SDmitry Chagin 3 /* sendmsg */, 3 /* recvmsg */,
2685abf20e93SDmitry Chagin 4 /* accept4 */, 5 /* recvmmsg */,
2686fc7510aeSEd Maste 4 /* sendmmsg */, 4 /* sendfile */
2687ea7b81d2SDmitry Chagin };
2688abf20e93SDmitry Chagin #define LINUX_ARGS_CNT (nitems(lxs_args_cnt) - 1)
2689abf20e93SDmitry Chagin #define LINUX_ARG_SIZE(x) (lxs_args_cnt[x] * sizeof(l_ulong))
2690ea7b81d2SDmitry Chagin
2691c21dee17SSøren Schmidt int
linux_socketcall(struct thread * td,struct linux_socketcall_args * args)2692b40ce416SJulian Elischer linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
2693c21dee17SSøren Schmidt {
2694ea7b81d2SDmitry Chagin l_ulong a[6];
2695abf20e93SDmitry Chagin #if defined(__amd64__) && defined(COMPAT_LINUX32)
2696abf20e93SDmitry Chagin register_t l_args[6];
2697abf20e93SDmitry Chagin #endif
2698ea7b81d2SDmitry Chagin void *arg;
2699ea7b81d2SDmitry Chagin int error;
27003f3a4815SMarcel Moolenaar
2701abf20e93SDmitry Chagin if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
2702ea7b81d2SDmitry Chagin return (EINVAL);
2703abf20e93SDmitry Chagin error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
2704abf20e93SDmitry Chagin if (error != 0)
2705ea7b81d2SDmitry Chagin return (error);
2706ea7b81d2SDmitry Chagin
2707abf20e93SDmitry Chagin #if defined(__amd64__) && defined(COMPAT_LINUX32)
2708abf20e93SDmitry Chagin for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
2709abf20e93SDmitry Chagin l_args[i] = a[i];
2710abf20e93SDmitry Chagin arg = l_args;
2711abf20e93SDmitry Chagin #else
2712ea7b81d2SDmitry Chagin arg = a;
2713abf20e93SDmitry Chagin #endif
2714c21dee17SSøren Schmidt switch (args->what) {
2715c21dee17SSøren Schmidt case LINUX_SOCKET:
2716b40ce416SJulian Elischer return (linux_socket(td, arg));
2717c21dee17SSøren Schmidt case LINUX_BIND:
2718b40ce416SJulian Elischer return (linux_bind(td, arg));
2719c21dee17SSøren Schmidt case LINUX_CONNECT:
2720b40ce416SJulian Elischer return (linux_connect(td, arg));
2721c21dee17SSøren Schmidt case LINUX_LISTEN:
2722b40ce416SJulian Elischer return (linux_listen(td, arg));
2723c21dee17SSøren Schmidt case LINUX_ACCEPT:
2724b40ce416SJulian Elischer return (linux_accept(td, arg));
2725c21dee17SSøren Schmidt case LINUX_GETSOCKNAME:
2726b40ce416SJulian Elischer return (linux_getsockname(td, arg));
2727c21dee17SSøren Schmidt case LINUX_GETPEERNAME:
2728b40ce416SJulian Elischer return (linux_getpeername(td, arg));
2729c21dee17SSøren Schmidt case LINUX_SOCKETPAIR:
2730b40ce416SJulian Elischer return (linux_socketpair(td, arg));
2731c21dee17SSøren Schmidt case LINUX_SEND:
2732b40ce416SJulian Elischer return (linux_send(td, arg));
2733c21dee17SSøren Schmidt case LINUX_RECV:
2734b40ce416SJulian Elischer return (linux_recv(td, arg));
2735c21dee17SSøren Schmidt case LINUX_SENDTO:
2736b40ce416SJulian Elischer return (linux_sendto(td, arg));
2737c21dee17SSøren Schmidt case LINUX_RECVFROM:
2738b40ce416SJulian Elischer return (linux_recvfrom(td, arg));
2739c21dee17SSøren Schmidt case LINUX_SHUTDOWN:
2740b40ce416SJulian Elischer return (linux_shutdown(td, arg));
2741c21dee17SSøren Schmidt case LINUX_SETSOCKOPT:
2742b40ce416SJulian Elischer return (linux_setsockopt(td, arg));
2743c21dee17SSøren Schmidt case LINUX_GETSOCKOPT:
2744b40ce416SJulian Elischer return (linux_getsockopt(td, arg));
2745e76bba09SSøren Schmidt case LINUX_SENDMSG:
2746ca26842eSHajimu UMEMOTO return (linux_sendmsg(td, arg));
2747e76bba09SSøren Schmidt case LINUX_RECVMSG:
2748b40ce416SJulian Elischer return (linux_recvmsg(td, arg));
2749f8cd0af2SDmitry Chagin case LINUX_ACCEPT4:
2750f8cd0af2SDmitry Chagin return (linux_accept4(td, arg));
2751e1ff74c0SDmitry Chagin case LINUX_RECVMMSG:
2752e1ff74c0SDmitry Chagin return (linux_recvmmsg(td, arg));
2753e1ff74c0SDmitry Chagin case LINUX_SENDMMSG:
2754e1ff74c0SDmitry Chagin return (linux_sendmmsg(td, arg));
2755fc7510aeSEd Maste case LINUX_SENDFILE:
2756fc7510aeSEd Maste return (linux_sendfile(td, arg));
2757c21dee17SSøren Schmidt }
27583f3a4815SMarcel Moolenaar
2759599dadcaSEdward Tomasz Napierala linux_msg(td, "socket type %d not implemented", args->what);
27603f3a4815SMarcel Moolenaar return (ENOSYS);
2761c21dee17SSøren Schmidt }
27626ecab394SDmitry Chagin #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
2763