The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/compat/linux/linux_socket.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
    3  *
    4  * Copyright (c) 1995 Søren Schmidt
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD$");
   31 
   32 /* XXX we use functions that might not exist. */
   33 #include "opt_compat.h"
   34 #include "opt_inet6.h"
   35 
   36 #include <sys/param.h>
   37 #include <sys/proc.h>
   38 #include <sys/systm.h>
   39 #include <sys/sysproto.h>
   40 #include <sys/capsicum.h>
   41 #include <sys/fcntl.h>
   42 #include <sys/file.h>
   43 #include <sys/filedesc.h>
   44 #include <sys/limits.h>
   45 #include <sys/lock.h>
   46 #include <sys/malloc.h>
   47 #include <sys/mutex.h>
   48 #include <sys/mbuf.h>
   49 #include <sys/socket.h>
   50 #include <sys/socketvar.h>
   51 #include <sys/syscallsubr.h>
   52 #include <sys/uio.h>
   53 #include <sys/stat.h>
   54 #include <sys/syslog.h>
   55 #include <sys/un.h>
   56 #include <sys/unistd.h>
   57 
   58 #include <security/audit/audit.h>
   59 
   60 #include <net/if.h>
   61 #include <net/vnet.h>
   62 #include <netinet/in.h>
   63 #include <netinet/in_systm.h>
   64 #include <netinet/ip.h>
   65 #include <netinet/tcp.h>
   66 #ifdef INET6
   67 #include <netinet/ip6.h>
   68 #include <netinet6/ip6_var.h>
   69 #endif
   70 
   71 #ifdef COMPAT_LINUX32
   72 #include <machine/../linux32/linux.h>
   73 #include <machine/../linux32/linux32_proto.h>
   74 #else
   75 #include <machine/../linux/linux.h>
   76 #include <machine/../linux/linux_proto.h>
   77 #endif
   78 #include <compat/linux/linux_common.h>
   79 #include <compat/linux/linux_emul.h>
   80 #include <compat/linux/linux_file.h>
   81 #include <compat/linux/linux_mib.h>
   82 #include <compat/linux/linux_socket.h>
   83 #include <compat/linux/linux_timer.h>
   84 #include <compat/linux/linux_util.h>
   85 
   86 #define SECURITY_CONTEXT_STRING "unconfined"
   87 
   88 static int linux_sendmsg_common(struct thread *, l_int, struct l_msghdr *,
   89                                         l_uint);
   90 static int linux_recvmsg_common(struct thread *, l_int, struct l_msghdr *,
   91                                         l_uint, struct msghdr *);
   92 static int linux_set_socket_flags(int, int *);
   93 
   94 #define SOL_NETLINK     270
   95 
   96 static int
   97 linux_to_bsd_sockopt_level(int level)
   98 {
   99 
  100         if (level == LINUX_SOL_SOCKET)
  101                 return (SOL_SOCKET);
  102         /* Remaining values are RFC-defined protocol numbers. */
  103         return (level);
  104 }
  105 
  106 static int
  107 bsd_to_linux_sockopt_level(int level)
  108 {
  109 
  110         if (level == SOL_SOCKET)
  111                 return (LINUX_SOL_SOCKET);
  112         return (level);
  113 }
  114 
  115 static int
  116 linux_to_bsd_ip_sockopt(int opt)
  117 {
  118 
  119         switch (opt) {
  120         /* known and translated sockopts */
  121         case LINUX_IP_TOS:
  122                 return (IP_TOS);
  123         case LINUX_IP_TTL:
  124                 return (IP_TTL);
  125         case LINUX_IP_HDRINCL:
  126                 return (IP_HDRINCL);
  127         case LINUX_IP_OPTIONS:
  128                 return (IP_OPTIONS);
  129         case LINUX_IP_RECVOPTS:
  130                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVOPTS");
  131                 return (IP_RECVOPTS);
  132         case LINUX_IP_RETOPTS:
  133                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_REETOPTS");
  134                 return (IP_RETOPTS);
  135         case LINUX_IP_RECVTTL:
  136                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVTTL");
  137                 return (IP_RECVTTL);
  138         case LINUX_IP_RECVTOS:
  139                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_RECVTOS");
  140                 return (IP_RECVTOS);
  141         case LINUX_IP_FREEBIND:
  142                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_FREEBIND");
  143                 return (IP_BINDANY);
  144         case LINUX_IP_IPSEC_POLICY:
  145                 /* we have this option, but not documented in ip(4) manpage */
  146                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_IPSEC_POLICY");
  147                 return (IP_IPSEC_POLICY);
  148         case LINUX_IP_MINTTL:
  149                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MINTTL");
  150                 return (IP_MINTTL);
  151         case LINUX_IP_MULTICAST_IF:
  152                 return (IP_MULTICAST_IF);
  153         case LINUX_IP_MULTICAST_TTL:
  154                 return (IP_MULTICAST_TTL);
  155         case LINUX_IP_MULTICAST_LOOP:
  156                 return (IP_MULTICAST_LOOP);
  157         case LINUX_IP_ADD_MEMBERSHIP:
  158                 return (IP_ADD_MEMBERSHIP);
  159         case LINUX_IP_DROP_MEMBERSHIP:
  160                 return (IP_DROP_MEMBERSHIP);
  161         case LINUX_IP_UNBLOCK_SOURCE:
  162                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_UNBLOCK_SOURCE");
  163                 return (IP_UNBLOCK_SOURCE);
  164         case LINUX_IP_BLOCK_SOURCE:
  165                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_BLOCK_SOURCE");
  166                 return (IP_BLOCK_SOURCE);
  167         case LINUX_IP_ADD_SOURCE_MEMBERSHIP:
  168                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_ADD_SOURCE_MEMBERSHIP");
  169                 return (IP_ADD_SOURCE_MEMBERSHIP);
  170         case LINUX_IP_DROP_SOURCE_MEMBERSHIP:
  171                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_DROP_SOURCE_MEMBERSHIP");
  172                 return (IP_DROP_SOURCE_MEMBERSHIP);
  173         case LINUX_MCAST_JOIN_GROUP:
  174                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_JOIN_GROUP");
  175                 return (MCAST_JOIN_GROUP);
  176         case LINUX_MCAST_LEAVE_GROUP:
  177                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_LEAVE_GROUP");
  178                 return (MCAST_LEAVE_GROUP);
  179         case LINUX_MCAST_JOIN_SOURCE_GROUP:
  180                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_JOIN_SOURCE_GROUP");
  181                 return (MCAST_JOIN_SOURCE_GROUP);
  182         case LINUX_MCAST_LEAVE_SOURCE_GROUP:
  183                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv4 socket option IP_MCAST_LEAVE_SOURCE_GROUP");
  184                 return (MCAST_LEAVE_SOURCE_GROUP);
  185         case LINUX_IP_RECVORIGDSTADDR:
  186                 return (IP_RECVORIGDSTADDR);
  187 
  188         /* known but not implemented sockopts */
  189         case LINUX_IP_ROUTER_ALERT:
  190                 LINUX_RATELIMIT_MSG_OPT1(
  191                     "unsupported IPv4 socket option IP_ROUTER_ALERT (%d), you can not do user-space routing from linux programs",
  192                     opt);
  193                 return (-2);
  194         case LINUX_IP_PKTINFO:
  195                 LINUX_RATELIMIT_MSG_OPT1(
  196                     "unsupported IPv4 socket option IP_PKTINFO (%d), you can not get extended packet info for datagram sockets in linux programs",
  197                     opt);
  198                 return (-2);
  199         case LINUX_IP_PKTOPTIONS:
  200                 LINUX_RATELIMIT_MSG_OPT1(
  201                     "unsupported IPv4 socket option IP_PKTOPTIONS (%d)",
  202                     opt);
  203                 return (-2);
  204         case LINUX_IP_MTU_DISCOVER:
  205                 LINUX_RATELIMIT_MSG_OPT1(
  206                     "unsupported IPv4 socket option IP_MTU_DISCOVER (%d), your linux program can not control path-MTU discovery",
  207                     opt);
  208                 return (-2);
  209         case LINUX_IP_RECVERR:
  210                 /* needed by steam */
  211                 LINUX_RATELIMIT_MSG_OPT1(
  212                     "unsupported IPv4 socket option IP_RECVERR (%d), you can not get extended reliability info in linux programs",
  213                     opt);
  214                 return (-2);
  215         case LINUX_IP_MTU:
  216                 LINUX_RATELIMIT_MSG_OPT1(
  217                     "unsupported IPv4 socket option IP_MTU (%d), your linux program can not control the MTU on this socket",
  218                     opt);
  219                 return (-2);
  220         case LINUX_IP_XFRM_POLICY:
  221                 LINUX_RATELIMIT_MSG_OPT1(
  222                     "unsupported IPv4 socket option IP_XFRM_POLICY (%d)",
  223                     opt);
  224                 return (-2);
  225         case LINUX_IP_PASSSEC:
  226                 /* needed by steam */
  227                 LINUX_RATELIMIT_MSG_OPT1(
  228                     "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",
  229                     opt);
  230                 return (-2);
  231         case LINUX_IP_TRANSPARENT:
  232                 /* IP_BINDANY or more? */
  233                 LINUX_RATELIMIT_MSG_OPT1(
  234                     "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",
  235                     opt);
  236                 return (-2);
  237         case LINUX_IP_NODEFRAG:
  238                 LINUX_RATELIMIT_MSG_OPT1(
  239                     "unsupported IPv4 socket option IP_NODEFRAG (%d)",
  240                     opt);
  241                 return (-2);
  242         case LINUX_IP_CHECKSUM:
  243                 LINUX_RATELIMIT_MSG_OPT1(
  244                     "unsupported IPv4 socket option IP_CHECKSUM (%d)",
  245                     opt);
  246                 return (-2);
  247         case LINUX_IP_BIND_ADDRESS_NO_PORT:
  248                 LINUX_RATELIMIT_MSG_OPT1(
  249                     "unsupported IPv4 socket option IP_BIND_ADDRESS_NO_PORT (%d)",
  250                     opt);
  251                 return (-2);
  252         case LINUX_IP_RECVFRAGSIZE:
  253                 LINUX_RATELIMIT_MSG_OPT1(
  254                     "unsupported IPv4 socket option IP_RECVFRAGSIZE (%d)",
  255                     opt);
  256                 return (-2);
  257         case LINUX_MCAST_MSFILTER:
  258                 LINUX_RATELIMIT_MSG_OPT1(
  259                     "unsupported IPv4 socket option IP_MCAST_MSFILTER (%d)",
  260                     opt);
  261                 return (-2);
  262         case LINUX_IP_MULTICAST_ALL:
  263                 LINUX_RATELIMIT_MSG_OPT1(
  264                     "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",
  265                     opt);
  266                 return (-2);
  267         case LINUX_IP_UNICAST_IF:
  268                 LINUX_RATELIMIT_MSG_OPT1(
  269                     "unsupported IPv4 socket option IP_UNICAST_IF (%d)",
  270                     opt);
  271                 return (-2);
  272 
  273         /* unknown sockopts */
  274         default:
  275                 return (-1);
  276         }
  277 }
  278 
  279 static int
  280 linux_to_bsd_ip6_sockopt(int opt)
  281 {
  282 
  283         switch (opt) {
  284         /* known and translated sockopts */
  285         case LINUX_IPV6_2292PKTINFO:
  286                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292PKTINFO");
  287                 return (IPV6_2292PKTINFO);
  288         case LINUX_IPV6_2292HOPOPTS:
  289                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292HOPOPTS");
  290                 return (IPV6_2292HOPOPTS);
  291         case LINUX_IPV6_2292DSTOPTS:
  292                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292DSTOPTS");
  293                 return (IPV6_2292DSTOPTS);
  294         case LINUX_IPV6_2292RTHDR:
  295                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292RTHDR");
  296                 return (IPV6_2292RTHDR);
  297         case LINUX_IPV6_2292PKTOPTIONS:
  298                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292PKTOPTIONS");
  299                 return (IPV6_2292PKTOPTIONS);
  300         case LINUX_IPV6_CHECKSUM:
  301                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_CHECKSUM");
  302                 return (IPV6_CHECKSUM);
  303         case LINUX_IPV6_2292HOPLIMIT:
  304                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_2292HOPLIMIT");
  305                 return (IPV6_2292HOPLIMIT);
  306         case LINUX_IPV6_NEXTHOP:
  307                 return (IPV6_NEXTHOP);
  308         case LINUX_IPV6_UNICAST_HOPS:
  309                 return (IPV6_UNICAST_HOPS);
  310         case LINUX_IPV6_MULTICAST_IF:
  311                 return (IPV6_MULTICAST_IF);
  312         case LINUX_IPV6_MULTICAST_HOPS:
  313                 return (IPV6_MULTICAST_HOPS);
  314         case LINUX_IPV6_MULTICAST_LOOP:
  315                 return (IPV6_MULTICAST_LOOP);
  316         case LINUX_IPV6_ADD_MEMBERSHIP:
  317                 return (IPV6_JOIN_GROUP);
  318         case LINUX_IPV6_DROP_MEMBERSHIP:
  319                 return (IPV6_LEAVE_GROUP);
  320         case LINUX_IPV6_V6ONLY:
  321                 return (IPV6_V6ONLY);
  322         case LINUX_IPV6_IPSEC_POLICY:
  323                 /* we have this option, but not documented in ip6(4) manpage */
  324                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_IPSEC_POLICY");
  325                 return (IPV6_IPSEC_POLICY);
  326         case LINUX_MCAST_JOIN_GROUP:
  327                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_JOIN_GROUP");
  328                 return (IPV6_JOIN_GROUP);
  329         case LINUX_MCAST_LEAVE_GROUP:
  330                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_LEAVE_GROUP");
  331                 return (IPV6_LEAVE_GROUP);
  332         case LINUX_IPV6_RECVPKTINFO:
  333                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVPKTINFO");
  334                 return (IPV6_RECVPKTINFO);
  335         case LINUX_IPV6_PKTINFO:
  336                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_PKTINFO");
  337                 return (IPV6_PKTINFO);
  338         case LINUX_IPV6_RECVHOPLIMIT:
  339                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVHOPLIMIT");
  340                 return (IPV6_RECVHOPLIMIT);
  341         case LINUX_IPV6_HOPLIMIT:
  342                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_HOPLIMIT");
  343                 return (IPV6_HOPLIMIT);
  344         case LINUX_IPV6_RECVHOPOPTS:
  345                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVHOPOPTS");
  346                 return (IPV6_RECVHOPOPTS);
  347         case LINUX_IPV6_HOPOPTS:
  348                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_HOPOPTS");
  349                 return (IPV6_HOPOPTS);
  350         case LINUX_IPV6_RTHDRDSTOPTS:
  351                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RTHDRDSTOPTS");
  352                 return (IPV6_RTHDRDSTOPTS);
  353         case LINUX_IPV6_RECVRTHDR:
  354                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVRTHDR");
  355                 return (IPV6_RECVRTHDR);
  356         case LINUX_IPV6_RTHDR:
  357                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RTHDR");
  358                 return (IPV6_RTHDR);
  359         case LINUX_IPV6_RECVDSTOPTS:
  360                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVDSTOPTS");
  361                 return (IPV6_RECVDSTOPTS);
  362         case LINUX_IPV6_DSTOPTS:
  363                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_DSTOPTS");
  364                 return (IPV6_DSTOPTS);
  365         case LINUX_IPV6_RECVPATHMTU:
  366                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_RECVPATHMTU");
  367                 return (IPV6_RECVPATHMTU);
  368         case LINUX_IPV6_PATHMTU:
  369                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_PATHMTU");
  370                 return (IPV6_PATHMTU);
  371         case LINUX_IPV6_DONTFRAG:
  372                 return (IPV6_DONTFRAG);
  373         case LINUX_IPV6_AUTOFLOWLABEL:
  374                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_AUTOFLOWLABEL");
  375                 return (IPV6_AUTOFLOWLABEL);
  376         case LINUX_IPV6_ORIGDSTADDR:
  377                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_ORIGDSTADDR");
  378                 return (IPV6_ORIGDSTADDR);
  379         case LINUX_IPV6_FREEBIND:
  380                 LINUX_RATELIMIT_MSG_NOTTESTED("IPv6 socket option IPV6_FREEBIND");
  381                 return (IPV6_BINDANY);
  382 
  383         /* known but not implemented sockopts */
  384         case LINUX_IPV6_ADDRFORM:
  385                 LINUX_RATELIMIT_MSG_OPT1(
  386                     "unsupported IPv6 socket option IPV6_ADDRFORM (%d), you linux program can not convert the socket to IPv4",
  387                     opt);
  388                 return (-2);
  389         case LINUX_IPV6_AUTHHDR:
  390                 LINUX_RATELIMIT_MSG_OPT1(
  391                     "unsupported IPv6 socket option IPV6_AUTHHDR (%d), your linux program can not get the authentication header info of IPv6 packets",
  392                     opt);
  393                 return (-2);
  394         case LINUX_IPV6_FLOWINFO:
  395                 LINUX_RATELIMIT_MSG_OPT1(
  396                     "unsupported IPv6 socket option IPV6_FLOWINFO (%d), your linux program can not get the flowid of IPv6 packets",
  397                     opt);
  398                 return (-2);
  399         case LINUX_IPV6_ROUTER_ALERT:
  400                 LINUX_RATELIMIT_MSG_OPT1(
  401                     "unsupported IPv6 socket option IPV6_ROUTER_ALERT (%d), you can not do user-space routing from linux programs",
  402                     opt);
  403                 return (-2);
  404         case LINUX_IPV6_MTU_DISCOVER:
  405                 LINUX_RATELIMIT_MSG_OPT1(
  406                     "unsupported IPv6 socket option IPV6_MTU_DISCOVER (%d), your linux program can not control path-MTU discovery",
  407                     opt);
  408                 return (-2);
  409         case LINUX_IPV6_MTU:
  410                 LINUX_RATELIMIT_MSG_OPT1(
  411                     "unsupported IPv6 socket option IPV6_MTU (%d), your linux program can not control the MTU on this socket",
  412                     opt);
  413                 return (-2);
  414         case LINUX_IPV6_JOIN_ANYCAST:
  415                 LINUX_RATELIMIT_MSG_OPT1(
  416                     "unsupported IPv6 socket option IPV6_JOIN_ANYCAST (%d)",
  417                     opt);
  418                 return (-2);
  419         case LINUX_IPV6_LEAVE_ANYCAST:
  420                 LINUX_RATELIMIT_MSG_OPT1(
  421                     "unsupported IPv6 socket option IPV6_LEAVE_ANYCAST (%d)",
  422                     opt);
  423                 return (-2);
  424         case LINUX_IPV6_MULTICAST_ALL:
  425                 LINUX_RATELIMIT_MSG_OPT1(
  426                     "unsupported IPv6 socket option IPV6_MULTICAST_ALL (%d)",
  427                     opt);
  428                 return (-2);
  429         case LINUX_IPV6_ROUTER_ALERT_ISOLATE:
  430                 LINUX_RATELIMIT_MSG_OPT1(
  431                     "unsupported IPv6 socket option IPV6_ROUTER_ALERT_ISOLATE (%d)",
  432                     opt);
  433                 return (-2);
  434         case LINUX_IPV6_FLOWLABEL_MGR:
  435                 LINUX_RATELIMIT_MSG_OPT1(
  436                     "unsupported IPv6 socket option IPV6_FLOWLABEL_MGR (%d)",
  437                     opt);
  438                 return (-2);
  439         case LINUX_IPV6_FLOWINFO_SEND:
  440                 LINUX_RATELIMIT_MSG_OPT1(
  441                     "unsupported IPv6 socket option IPV6_FLOWINFO_SEND (%d)",
  442                     opt);
  443                 return (-2);
  444         case LINUX_IPV6_XFRM_POLICY:
  445                 LINUX_RATELIMIT_MSG_OPT1(
  446                     "unsupported IPv6 socket option IPV6_XFRM_POLICY (%d)",
  447                     opt);
  448                 return (-2);
  449         case LINUX_IPV6_HDRINCL:
  450                 LINUX_RATELIMIT_MSG_OPT1(
  451                     "unsupported IPv6 socket option IPV6_HDRINCL (%d)",
  452                     opt);
  453                 return (-2);
  454         case LINUX_MCAST_BLOCK_SOURCE:
  455                 LINUX_RATELIMIT_MSG_OPT1(
  456                     "unsupported IPv6 socket option MCAST_BLOCK_SOURCE (%d), your linux program may see more multicast stuff than it wants",
  457                     opt);
  458                 return (-2);
  459         case LINUX_MCAST_UNBLOCK_SOURCE:
  460                 LINUX_RATELIMIT_MSG_OPT1(
  461                     "unsupported IPv6 socket option MCAST_UNBLOCK_SOURCE (%d), your linux program may not see all the multicast stuff it wants",
  462                     opt);
  463                 return (-2);
  464         case LINUX_MCAST_JOIN_SOURCE_GROUP:
  465                 LINUX_RATELIMIT_MSG_OPT1(
  466                     "unsupported IPv6 socket option MCAST_JOIN_SOURCE_GROUP (%d), your linux program is not able to join a multicast source group",
  467                     opt);
  468                 return (-2);
  469         case LINUX_MCAST_LEAVE_SOURCE_GROUP:
  470                 LINUX_RATELIMIT_MSG_OPT1(
  471                     "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",
  472                     opt);
  473                 return (-2);
  474         case LINUX_MCAST_MSFILTER:
  475                 LINUX_RATELIMIT_MSG_OPT1(
  476                     "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",
  477                     opt);
  478                 return (-2);
  479         case LINUX_IPV6_ADDR_PREFERENCES:
  480                 LINUX_RATELIMIT_MSG_OPT1(
  481                     "unsupported IPv6 socket option IPV6_ADDR_PREFERENCES (%d)",
  482                     opt);
  483                 return (-2);
  484         case LINUX_IPV6_MINHOPCOUNT:
  485                 LINUX_RATELIMIT_MSG_OPT1(
  486                     "unsupported IPv6 socket option IPV6_MINHOPCOUNT (%d)",
  487                     opt);
  488                 return (-2);
  489         case LINUX_IPV6_TRANSPARENT:
  490                 /* IP_BINDANY or more? */
  491                 LINUX_RATELIMIT_MSG_OPT1(
  492                     "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",
  493                     opt);
  494                 return (-2);
  495         case LINUX_IPV6_UNICAST_IF:
  496                 LINUX_RATELIMIT_MSG_OPT1(
  497                     "unsupported IPv6 socket option IPV6_UNICAST_IF (%d)",
  498                     opt);
  499                 return (-2);
  500         case LINUX_IPV6_RECVFRAGSIZE:
  501                 LINUX_RATELIMIT_MSG_OPT1(
  502                     "unsupported IPv6 socket option IPV6_RECVFRAGSIZE (%d)",
  503                     opt);
  504                 return (-2);
  505 
  506         /* unknown sockopts */
  507         default:
  508                 return (-1);
  509         }
  510 }
  511 
  512 static int
  513 linux_to_bsd_so_sockopt(int opt)
  514 {
  515 
  516         switch (opt) {
  517         case LINUX_SO_DEBUG:
  518                 return (SO_DEBUG);
  519         case LINUX_SO_REUSEADDR:
  520                 return (SO_REUSEADDR);
  521         case LINUX_SO_TYPE:
  522                 return (SO_TYPE);
  523         case LINUX_SO_ERROR:
  524                 return (SO_ERROR);
  525         case LINUX_SO_DONTROUTE:
  526                 return (SO_DONTROUTE);
  527         case LINUX_SO_BROADCAST:
  528                 return (SO_BROADCAST);
  529         case LINUX_SO_SNDBUF:
  530         case LINUX_SO_SNDBUFFORCE:
  531                 return (SO_SNDBUF);
  532         case LINUX_SO_RCVBUF:
  533         case LINUX_SO_RCVBUFFORCE:
  534                 return (SO_RCVBUF);
  535         case LINUX_SO_KEEPALIVE:
  536                 return (SO_KEEPALIVE);
  537         case LINUX_SO_OOBINLINE:
  538                 return (SO_OOBINLINE);
  539         case LINUX_SO_LINGER:
  540                 return (SO_LINGER);
  541         case LINUX_SO_REUSEPORT:
  542                 return (SO_REUSEPORT_LB);
  543         case LINUX_SO_PASSCRED:
  544                 return (LOCAL_CREDS_PERSISTENT);
  545         case LINUX_SO_PEERCRED:
  546                 return (LOCAL_PEERCRED);
  547         case LINUX_SO_RCVLOWAT:
  548                 return (SO_RCVLOWAT);
  549         case LINUX_SO_SNDLOWAT:
  550                 return (SO_SNDLOWAT);
  551         case LINUX_SO_RCVTIMEO:
  552                 return (SO_RCVTIMEO);
  553         case LINUX_SO_SNDTIMEO:
  554                 return (SO_SNDTIMEO);
  555         case LINUX_SO_TIMESTAMPO:
  556         case LINUX_SO_TIMESTAMPN:
  557                 return (SO_TIMESTAMP);
  558         case LINUX_SO_TIMESTAMPNSO:
  559         case LINUX_SO_TIMESTAMPNSN:
  560                 return (SO_BINTIME);
  561         case LINUX_SO_ACCEPTCONN:
  562                 return (SO_ACCEPTCONN);
  563         case LINUX_SO_PROTOCOL:
  564                 return (SO_PROTOCOL);
  565         case LINUX_SO_DOMAIN:
  566                 return (SO_DOMAIN);
  567         }
  568         return (-1);
  569 }
  570 
  571 static int
  572 linux_to_bsd_tcp_sockopt(int opt)
  573 {
  574 
  575         switch (opt) {
  576         case LINUX_TCP_NODELAY:
  577                 return (TCP_NODELAY);
  578         case LINUX_TCP_MAXSEG:
  579                 return (TCP_MAXSEG);
  580         case LINUX_TCP_CORK:
  581                 return (TCP_NOPUSH);
  582         case LINUX_TCP_KEEPIDLE:
  583                 return (TCP_KEEPIDLE);
  584         case LINUX_TCP_KEEPINTVL:
  585                 return (TCP_KEEPINTVL);
  586         case LINUX_TCP_KEEPCNT:
  587                 return (TCP_KEEPCNT);
  588         case LINUX_TCP_INFO:
  589                 LINUX_RATELIMIT_MSG_OPT1(
  590                     "unsupported TCP socket option TCP_INFO (%d)", opt);
  591                 return (-2);
  592         case LINUX_TCP_MD5SIG:
  593                 return (TCP_MD5SIG);
  594         }
  595         return (-1);
  596 }
  597 
  598 static int
  599 linux_to_bsd_msg_flags(int flags)
  600 {
  601         int ret_flags = 0;
  602 
  603         if (flags & LINUX_MSG_OOB)
  604                 ret_flags |= MSG_OOB;
  605         if (flags & LINUX_MSG_PEEK)
  606                 ret_flags |= MSG_PEEK;
  607         if (flags & LINUX_MSG_DONTROUTE)
  608                 ret_flags |= MSG_DONTROUTE;
  609         if (flags & LINUX_MSG_CTRUNC)
  610                 ret_flags |= MSG_CTRUNC;
  611         if (flags & LINUX_MSG_TRUNC)
  612                 ret_flags |= MSG_TRUNC;
  613         if (flags & LINUX_MSG_DONTWAIT)
  614                 ret_flags |= MSG_DONTWAIT;
  615         if (flags & LINUX_MSG_EOR)
  616                 ret_flags |= MSG_EOR;
  617         if (flags & LINUX_MSG_WAITALL)
  618                 ret_flags |= MSG_WAITALL;
  619         if (flags & LINUX_MSG_NOSIGNAL)
  620                 ret_flags |= MSG_NOSIGNAL;
  621         if (flags & LINUX_MSG_PROXY)
  622                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_PROXY (%d) not handled",
  623                     LINUX_MSG_PROXY);
  624         if (flags & LINUX_MSG_FIN)
  625                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_FIN (%d) not handled",
  626                     LINUX_MSG_FIN);
  627         if (flags & LINUX_MSG_SYN)
  628                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_SYN (%d) not handled",
  629                     LINUX_MSG_SYN);
  630         if (flags & LINUX_MSG_CONFIRM)
  631                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_CONFIRM (%d) not handled",
  632                     LINUX_MSG_CONFIRM);
  633         if (flags & LINUX_MSG_RST)
  634                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_RST (%d) not handled",
  635                     LINUX_MSG_RST);
  636         if (flags & LINUX_MSG_ERRQUEUE)
  637                 LINUX_RATELIMIT_MSG_OPT1("socket message flag MSG_ERRQUEUE (%d) not handled",
  638                     LINUX_MSG_ERRQUEUE);
  639         return (ret_flags);
  640 }
  641 
  642 static int
  643 linux_to_bsd_cmsg_type(int cmsg_type)
  644 {
  645 
  646         switch (cmsg_type) {
  647         case LINUX_SCM_RIGHTS:
  648                 return (SCM_RIGHTS);
  649         case LINUX_SCM_CREDENTIALS:
  650                 return (SCM_CREDS);
  651         }
  652         return (-1);
  653 }
  654 
  655 static int
  656 bsd_to_linux_ip_cmsg_type(int cmsg_type)
  657 {
  658 
  659         switch (cmsg_type) {
  660         case IP_RECVORIGDSTADDR:
  661                 return (LINUX_IP_RECVORIGDSTADDR);
  662         }
  663         return (-1);
  664 }
  665 
  666 static int
  667 bsd_to_linux_cmsg_type(struct proc *p, int cmsg_type, int cmsg_level)
  668 {
  669         struct linux_pemuldata *pem;
  670 
  671         if (cmsg_level == IPPROTO_IP)
  672                 return (bsd_to_linux_ip_cmsg_type(cmsg_type));
  673         if (cmsg_level != SOL_SOCKET)
  674                 return (-1);
  675 
  676         pem = pem_find(p);
  677 
  678         switch (cmsg_type) {
  679         case SCM_RIGHTS:
  680                 return (LINUX_SCM_RIGHTS);
  681         case SCM_CREDS:
  682                 return (LINUX_SCM_CREDENTIALS);
  683         case SCM_CREDS2:
  684                 return (LINUX_SCM_CREDENTIALS);
  685         case SCM_TIMESTAMP:
  686                 return (pem->so_timestamp);
  687         case SCM_BINTIME:
  688                 return (pem->so_timestampns);
  689         }
  690         return (-1);
  691 }
  692 
  693 static int
  694 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
  695 {
  696         if (lhdr->msg_controllen > INT_MAX)
  697                 return (ENOBUFS);
  698 
  699         bhdr->msg_name          = PTRIN(lhdr->msg_name);
  700         bhdr->msg_namelen       = lhdr->msg_namelen;
  701         bhdr->msg_iov           = PTRIN(lhdr->msg_iov);
  702         bhdr->msg_iovlen        = lhdr->msg_iovlen;
  703         bhdr->msg_control       = PTRIN(lhdr->msg_control);
  704 
  705         /*
  706          * msg_controllen is skipped since BSD and LINUX control messages
  707          * are potentially different sizes (e.g. the cred structure used
  708          * by SCM_CREDS is different between the two operating system).
  709          *
  710          * The caller can set it (if necessary) after converting all the
  711          * control messages.
  712          */
  713 
  714         bhdr->msg_flags         = linux_to_bsd_msg_flags(lhdr->msg_flags);
  715         return (0);
  716 }
  717 
  718 static int
  719 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
  720 {
  721         lhdr->msg_name          = PTROUT(bhdr->msg_name);
  722         lhdr->msg_namelen       = bhdr->msg_namelen;
  723         lhdr->msg_iov           = PTROUT(bhdr->msg_iov);
  724         lhdr->msg_iovlen        = bhdr->msg_iovlen;
  725         lhdr->msg_control       = PTROUT(bhdr->msg_control);
  726 
  727         /*
  728          * msg_controllen is skipped since BSD and LINUX control messages
  729          * are potentially different sizes (e.g. the cred structure used
  730          * by SCM_CREDS is different between the two operating system).
  731          *
  732          * The caller can set it (if necessary) after converting all the
  733          * control messages.
  734          */
  735 
  736         /* msg_flags skipped */
  737         return (0);
  738 }
  739 
  740 static int
  741 linux_set_socket_flags(int lflags, int *flags)
  742 {
  743 
  744         if (lflags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
  745                 return (EINVAL);
  746         if (lflags & LINUX_SOCK_NONBLOCK)
  747                 *flags |= SOCK_NONBLOCK;
  748         if (lflags & LINUX_SOCK_CLOEXEC)
  749                 *flags |= SOCK_CLOEXEC;
  750         return (0);
  751 }
  752 
  753 static int
  754 linux_copyout_sockaddr(const struct sockaddr *sa, void *uaddr, size_t len)
  755 {
  756         struct l_sockaddr *lsa;
  757         int error;
  758 
  759         error = bsd_to_linux_sockaddr(sa, &lsa, len);
  760         if (error != 0)
  761                 return (error);
  762 
  763         error = copyout(lsa, uaddr, len);
  764         free(lsa, M_LINUX);
  765 
  766         return (error);
  767 }
  768 
  769 static int
  770 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
  771     struct mbuf *control, enum uio_seg segflg)
  772 {
  773         struct sockaddr *to;
  774         int error, len;
  775 
  776         if (mp->msg_name != NULL) {
  777                 len = mp->msg_namelen;
  778                 error = linux_to_bsd_sockaddr(mp->msg_name, &to, &len);
  779                 if (error != 0)
  780                         return (error);
  781                 mp->msg_name = to;
  782         } else
  783                 to = NULL;
  784 
  785         error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
  786             segflg);
  787 
  788         if (to)
  789                 free(to, M_SONAME);
  790         return (error);
  791 }
  792 
  793 /* Return 0 if IP_HDRINCL is set for the given socket. */
  794 static int
  795 linux_check_hdrincl(struct thread *td, int s)
  796 {
  797         int error, optval;
  798         socklen_t size_val;
  799 
  800         size_val = sizeof(optval);
  801         error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
  802             &optval, UIO_SYSSPACE, &size_val);
  803         if (error != 0)
  804                 return (error);
  805 
  806         return (optval == 0);
  807 }
  808 
  809 /*
  810  * Updated sendto() when IP_HDRINCL is set:
  811  * tweak endian-dependent fields in the IP packet.
  812  */
  813 static int
  814 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
  815 {
  816 /*
  817  * linux_ip_copysize defines how many bytes we should copy
  818  * from the beginning of the IP packet before we customize it for BSD.
  819  * It should include all the fields we modify (ip_len and ip_off).
  820  */
  821 #define linux_ip_copysize       8
  822 
  823         struct ip *packet;
  824         struct msghdr msg;
  825         struct iovec aiov[1];
  826         int error;
  827 
  828         /* Check that the packet isn't too big or too small. */
  829         if (linux_args->len < linux_ip_copysize ||
  830             linux_args->len > IP_MAXPACKET)
  831                 return (EINVAL);
  832 
  833         packet = (struct ip *)malloc(linux_args->len, M_LINUX, M_WAITOK);
  834 
  835         /* Make kernel copy of the packet to be sent */
  836         if ((error = copyin(PTRIN(linux_args->msg), packet,
  837             linux_args->len)))
  838                 goto goout;
  839 
  840         /* Convert fields from Linux to BSD raw IP socket format */
  841         packet->ip_len = linux_args->len;
  842         packet->ip_off = ntohs(packet->ip_off);
  843 
  844         /* Prepare the msghdr and iovec structures describing the new packet */
  845         msg.msg_name = PTRIN(linux_args->to);
  846         msg.msg_namelen = linux_args->tolen;
  847         msg.msg_iov = aiov;
  848         msg.msg_iovlen = 1;
  849         msg.msg_control = NULL;
  850         msg.msg_flags = 0;
  851         aiov[0].iov_base = (char *)packet;
  852         aiov[0].iov_len = linux_args->len;
  853         error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
  854             NULL, UIO_SYSSPACE);
  855 goout:
  856         free(packet, M_LINUX);
  857         return (error);
  858 }
  859 
  860 static const char *linux_netlink_names[] = {
  861         [LINUX_NETLINK_ROUTE] = "ROUTE",
  862         [LINUX_NETLINK_SOCK_DIAG] = "SOCK_DIAG",
  863         [LINUX_NETLINK_NFLOG] = "NFLOG",
  864         [LINUX_NETLINK_SELINUX] = "SELINUX",
  865         [LINUX_NETLINK_AUDIT] = "AUDIT",
  866         [LINUX_NETLINK_FIB_LOOKUP] = "FIB_LOOKUP",
  867         [LINUX_NETLINK_NETFILTER] = "NETFILTER",
  868         [LINUX_NETLINK_KOBJECT_UEVENT] = "KOBJECT_UEVENT",
  869 };
  870 
  871 int
  872 linux_socket(struct thread *td, struct linux_socket_args *args)
  873 {
  874         int domain, retval_socket, type;
  875 
  876         type = args->type & LINUX_SOCK_TYPE_MASK;
  877         if (type < 0 || type > LINUX_SOCK_MAX)
  878                 return (EINVAL);
  879         retval_socket = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
  880                 &type);
  881         if (retval_socket != 0)
  882                 return (retval_socket);
  883         domain = linux_to_bsd_domain(args->domain);
  884         if (domain == -1) {
  885                 /* Mask off SOCK_NONBLOCK / CLOEXEC for error messages. */
  886                 type = args->type & LINUX_SOCK_TYPE_MASK;
  887                 if (args->domain == LINUX_AF_NETLINK &&
  888                     args->protocol == LINUX_NETLINK_AUDIT) {
  889                         ; /* Do nothing, quietly. */
  890                 } else if (args->domain == LINUX_AF_NETLINK) {
  891                         const char *nl_name;
  892 
  893                         if (args->protocol >= 0 &&
  894                             args->protocol < nitems(linux_netlink_names))
  895                                 nl_name = linux_netlink_names[args->protocol];
  896                         else
  897                                 nl_name = NULL;
  898                         if (nl_name != NULL)
  899                                 linux_msg(curthread,
  900                                     "unsupported socket(AF_NETLINK, %d, "
  901                                     "NETLINK_%s)", type, nl_name);
  902                         else
  903                                 linux_msg(curthread,
  904                                     "unsupported socket(AF_NETLINK, %d, %d)",
  905                                     type, args->protocol);
  906                 } else {
  907                         linux_msg(curthread, "unsupported socket domain %d, "
  908                             "type %d, protocol %d", args->domain, type,
  909                             args->protocol);
  910                 }
  911                 return (EAFNOSUPPORT);
  912         }
  913 
  914         retval_socket = kern_socket(td, domain, type, args->protocol);
  915         if (retval_socket)
  916                 return (retval_socket);
  917 
  918         if (type == SOCK_RAW
  919             && (args->protocol == IPPROTO_RAW || args->protocol == 0)
  920             && domain == PF_INET) {
  921                 /* It's a raw IP socket: set the IP_HDRINCL option. */
  922                 int hdrincl;
  923 
  924                 hdrincl = 1;
  925                 /* We ignore any error returned by kern_setsockopt() */
  926                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
  927                     &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
  928         }
  929 #ifdef INET6
  930         /*
  931          * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
  932          * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
  933          * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
  934          * sysctl value.
  935          */
  936         if (domain == PF_INET6) {
  937                 int v6only;
  938 
  939                 v6only = 0;
  940                 /* We ignore any error returned by setsockopt() */
  941                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
  942                     &v6only, UIO_SYSSPACE, sizeof(v6only));
  943         }
  944 #endif
  945 
  946         return (retval_socket);
  947 }
  948 
  949 int
  950 linux_bind(struct thread *td, struct linux_bind_args *args)
  951 {
  952         struct sockaddr *sa;
  953         int error;
  954 
  955         error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
  956             &args->namelen);
  957         if (error != 0)
  958                 return (error);
  959 
  960         error = kern_bindat(td, AT_FDCWD, args->s, sa);
  961         free(sa, M_SONAME);
  962 
  963         /* XXX */
  964         if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
  965                 return (EINVAL);
  966         return (error);
  967 }
  968 
  969 int
  970 linux_connect(struct thread *td, struct linux_connect_args *args)
  971 {
  972         struct socket *so;
  973         struct sockaddr *sa;
  974         struct file *fp;
  975         u_int fflag;
  976         int error;
  977 
  978         error = linux_to_bsd_sockaddr(PTRIN(args->name), &sa,
  979             &args->namelen);
  980         if (error != 0)
  981                 return (error);
  982 
  983         error = kern_connectat(td, AT_FDCWD, args->s, sa);
  984         free(sa, M_SONAME);
  985         if (error != EISCONN)
  986                 return (error);
  987 
  988         /*
  989          * Linux doesn't return EISCONN the first time it occurs,
  990          * when on a non-blocking socket. Instead it returns the
  991          * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
  992          */
  993         error = getsock_cap(td, args->s, &cap_connect_rights,
  994             &fp, &fflag, NULL);
  995         if (error != 0)
  996                 return (error);
  997 
  998         error = EISCONN;
  999         so = fp->f_data;
 1000         if (fflag & FNONBLOCK) {
 1001                 SOCK_LOCK(so);
 1002                 if (so->so_emuldata == 0)
 1003                         error = so->so_error;
 1004                 so->so_emuldata = (void *)1;
 1005                 SOCK_UNLOCK(so);
 1006         }
 1007         fdrop(fp, td);
 1008 
 1009         return (error);
 1010 }
 1011 
 1012 int
 1013 linux_listen(struct thread *td, struct linux_listen_args *args)
 1014 {
 1015 
 1016         return (kern_listen(td, args->s, args->backlog));
 1017 }
 1018 
 1019 static int
 1020 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
 1021     l_uintptr_t namelen, int flags)
 1022 {
 1023         struct sockaddr *sa;
 1024         struct file *fp, *fp1;
 1025         int bflags, len;
 1026         struct socket *so;
 1027         int error, error1;
 1028 
 1029         bflags = 0;
 1030         fp = NULL;
 1031         sa = NULL;
 1032 
 1033         error = linux_set_socket_flags(flags, &bflags);
 1034         if (error != 0)
 1035                 return (error);
 1036 
 1037         if (PTRIN(addr) == NULL) {
 1038                 len = 0;
 1039                 error = kern_accept4(td, s, NULL, NULL, bflags, NULL);
 1040         } else {
 1041                 error = copyin(PTRIN(namelen), &len, sizeof(len));
 1042                 if (error != 0)
 1043                         return (error);
 1044                 if (len < 0)
 1045                         return (EINVAL);
 1046                 error = kern_accept4(td, s, &sa, &len, bflags, &fp);
 1047         }
 1048 
 1049         /*
 1050          * Translate errno values into ones used by Linux.
 1051          */
 1052         if (error != 0) {
 1053                 /*
 1054                  * XXX. This is wrong, different sockaddr structures
 1055                  * have different sizes.
 1056                  */
 1057                 switch (error) {
 1058                 case EFAULT:
 1059                         if (namelen != sizeof(struct sockaddr_in))
 1060                                 error = EINVAL;
 1061                         break;
 1062                 case EINVAL:
 1063                         error1 = getsock_cap(td, s, &cap_accept_rights, &fp1, NULL, NULL);
 1064                         if (error1 != 0) {
 1065                                 error = error1;
 1066                                 break;
 1067                         }
 1068                         so = fp1->f_data;
 1069                         if (so->so_type == SOCK_DGRAM)
 1070                                 error = EOPNOTSUPP;
 1071                         fdrop(fp1, td);
 1072                         break;
 1073                 }
 1074                 return (error);
 1075         }
 1076 
 1077         if (len != 0) {
 1078                 error = linux_copyout_sockaddr(sa, PTRIN(addr), len);
 1079                 if (error == 0)
 1080                         error = copyout(&len, PTRIN(namelen),
 1081                             sizeof(len));
 1082                 if (error != 0) {
 1083                         fdclose(td, fp, td->td_retval[0]);
 1084                         td->td_retval[0] = 0;
 1085                 }
 1086         }
 1087         if (fp != NULL)
 1088                 fdrop(fp, td);
 1089         free(sa, M_SONAME);
 1090         return (error);
 1091 }
 1092 
 1093 int
 1094 linux_accept(struct thread *td, struct linux_accept_args *args)
 1095 {
 1096 
 1097         return (linux_accept_common(td, args->s, args->addr,
 1098             args->namelen, 0));
 1099 }
 1100 
 1101 int
 1102 linux_accept4(struct thread *td, struct linux_accept4_args *args)
 1103 {
 1104 
 1105         return (linux_accept_common(td, args->s, args->addr,
 1106             args->namelen, args->flags));
 1107 }
 1108 
 1109 int
 1110 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
 1111 {
 1112         struct sockaddr *sa;
 1113         int len, error;
 1114 
 1115         error = copyin(PTRIN(args->namelen), &len, sizeof(len));
 1116         if (error != 0)
 1117                 return (error);
 1118 
 1119         error = kern_getsockname(td, args->s, &sa, &len);
 1120         if (error != 0)
 1121                 return (error);
 1122 
 1123         if (len != 0)
 1124                 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
 1125 
 1126         free(sa, M_SONAME);
 1127         if (error == 0)
 1128                 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
 1129         return (error);
 1130 }
 1131 
 1132 int
 1133 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
 1134 {
 1135         struct sockaddr *sa;
 1136         int len, error;
 1137 
 1138         error = copyin(PTRIN(args->namelen), &len, sizeof(len));
 1139         if (error != 0)
 1140                 return (error);
 1141         if (len < 0)
 1142                 return (EINVAL);
 1143 
 1144         error = kern_getpeername(td, args->s, &sa, &len);
 1145         if (error != 0)
 1146                 return (error);
 1147 
 1148         if (len != 0)
 1149                 error = linux_copyout_sockaddr(sa, PTRIN(args->addr), len);
 1150 
 1151         free(sa, M_SONAME);
 1152         if (error == 0)
 1153                 error = copyout(&len, PTRIN(args->namelen), sizeof(len));
 1154         return (error);
 1155 }
 1156 
 1157 int
 1158 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
 1159 {
 1160         int domain, error, sv[2], type;
 1161 
 1162         domain = linux_to_bsd_domain(args->domain);
 1163         if (domain != PF_LOCAL)
 1164                 return (EAFNOSUPPORT);
 1165         type = args->type & LINUX_SOCK_TYPE_MASK;
 1166         if (type < 0 || type > LINUX_SOCK_MAX)
 1167                 return (EINVAL);
 1168         error = linux_set_socket_flags(args->type & ~LINUX_SOCK_TYPE_MASK,
 1169             &type);
 1170         if (error != 0)
 1171                 return (error);
 1172         if (args->protocol != 0 && args->protocol != PF_UNIX) {
 1173                 /*
 1174                  * Use of PF_UNIX as protocol argument is not right,
 1175                  * but Linux does it.
 1176                  * Do not map PF_UNIX as its Linux value is identical
 1177                  * to FreeBSD one.
 1178                  */
 1179                 return (EPROTONOSUPPORT);
 1180         }
 1181         error = kern_socketpair(td, domain, type, 0, sv);
 1182         if (error != 0)
 1183                 return (error);
 1184         error = copyout(sv, PTRIN(args->rsv), 2 * sizeof(int));
 1185         if (error != 0) {
 1186                 (void)kern_close(td, sv[0]);
 1187                 (void)kern_close(td, sv[1]);
 1188         }
 1189         return (error);
 1190 }
 1191 
 1192 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 1193 struct linux_send_args {
 1194         register_t s;
 1195         register_t msg;
 1196         register_t len;
 1197         register_t flags;
 1198 };
 1199 
 1200 static int
 1201 linux_send(struct thread *td, struct linux_send_args *args)
 1202 {
 1203         struct sendto_args /* {
 1204                 int s;
 1205                 caddr_t buf;
 1206                 int len;
 1207                 int flags;
 1208                 caddr_t to;
 1209                 int tolen;
 1210         } */ bsd_args;
 1211         struct file *fp;
 1212         int error, fflag;
 1213 
 1214         bsd_args.s = args->s;
 1215         bsd_args.buf = (caddr_t)PTRIN(args->msg);
 1216         bsd_args.len = args->len;
 1217         bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
 1218         bsd_args.to = NULL;
 1219         bsd_args.tolen = 0;
 1220         error = sys_sendto(td, &bsd_args);
 1221         if (error == ENOTCONN) {
 1222                 /*
 1223                  * Linux doesn't return ENOTCONN for non-blocking sockets.
 1224                  * Instead it returns the EAGAIN.
 1225                  */
 1226                 error = getsock_cap(td, args->s, &cap_send_rights, &fp,
 1227                     &fflag, NULL);
 1228                 if (error == 0) {
 1229                         if (fflag & FNONBLOCK)
 1230                                 error = EAGAIN;
 1231                         fdrop(fp, td);
 1232                 }
 1233         }
 1234         return (error);
 1235 }
 1236 
 1237 struct linux_recv_args {
 1238         register_t s;
 1239         register_t msg;
 1240         register_t len;
 1241         register_t flags;
 1242 };
 1243 
 1244 static int
 1245 linux_recv(struct thread *td, struct linux_recv_args *args)
 1246 {
 1247         struct recvfrom_args /* {
 1248                 int s;
 1249                 caddr_t buf;
 1250                 int len;
 1251                 int flags;
 1252                 struct sockaddr *from;
 1253                 socklen_t fromlenaddr;
 1254         } */ bsd_args;
 1255 
 1256         bsd_args.s = args->s;
 1257         bsd_args.buf = (caddr_t)PTRIN(args->msg);
 1258         bsd_args.len = args->len;
 1259         bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
 1260         bsd_args.from = NULL;
 1261         bsd_args.fromlenaddr = 0;
 1262         return (sys_recvfrom(td, &bsd_args));
 1263 }
 1264 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 1265 
 1266 int
 1267 linux_sendto(struct thread *td, struct linux_sendto_args *args)
 1268 {
 1269         struct msghdr msg;
 1270         struct iovec aiov;
 1271         struct socket *so;
 1272         struct file *fp;
 1273         int error;
 1274 
 1275         if (linux_check_hdrincl(td, args->s) == 0)
 1276                 /* IP_HDRINCL set, tweak the packet before sending */
 1277                 return (linux_sendto_hdrincl(td, args));
 1278 
 1279         bzero(&msg, sizeof(msg));
 1280         error = getsock_cap(td, args->s, &cap_send_connect_rights,
 1281             &fp, NULL, NULL);
 1282         if (error != 0)
 1283                 return (error);
 1284         so = fp->f_data;
 1285         if ((so->so_state & (SS_ISCONNECTED|SS_ISCONNECTING)) == 0) {
 1286                 msg.msg_name = PTRIN(args->to);
 1287                 msg.msg_namelen = args->tolen;
 1288         }
 1289         msg.msg_iov = &aiov;
 1290         msg.msg_iovlen = 1;
 1291         aiov.iov_base = PTRIN(args->msg);
 1292         aiov.iov_len = args->len;
 1293         fdrop(fp, td);
 1294         return (linux_sendit(td, args->s, &msg, args->flags, NULL,
 1295             UIO_USERSPACE));
 1296 }
 1297 
 1298 int
 1299 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
 1300 {
 1301         struct sockaddr *sa;
 1302         struct msghdr msg;
 1303         struct iovec aiov;
 1304         int error, fromlen;
 1305 
 1306         if (PTRIN(args->fromlen) != NULL) {
 1307                 error = copyin(PTRIN(args->fromlen), &fromlen,
 1308                     sizeof(fromlen));
 1309                 if (error != 0)
 1310                         return (error);
 1311                 if (fromlen < 0)
 1312                         return (EINVAL);
 1313                 fromlen = min(fromlen, SOCK_MAXADDRLEN);
 1314                 sa = malloc(fromlen, M_SONAME, M_WAITOK);
 1315         } else {
 1316                 fromlen = 0;
 1317                 sa = NULL;
 1318         }
 1319 
 1320         msg.msg_name = sa;
 1321         msg.msg_namelen = fromlen;
 1322         msg.msg_iov = &aiov;
 1323         msg.msg_iovlen = 1;
 1324         aiov.iov_base = PTRIN(args->buf);
 1325         aiov.iov_len = args->len;
 1326         msg.msg_control = 0;
 1327         msg.msg_flags = linux_to_bsd_msg_flags(args->flags);
 1328 
 1329         error = kern_recvit(td, args->s, &msg, UIO_SYSSPACE, NULL);
 1330         if (error != 0)
 1331                 goto out;
 1332 
 1333         /*
 1334          * XXX. Seems that FreeBSD is different from Linux here. Linux
 1335          * fill source address if underlying protocol provides it, while
 1336          * FreeBSD fill it if underlying protocol is not connection-oriented.
 1337          * So, kern_recvit() set msg.msg_namelen to 0 if protocol pr_flags
 1338          * does not contains PR_ADDR flag.
 1339          */
 1340         if (PTRIN(args->from) != NULL && msg.msg_namelen != 0)
 1341                 error = linux_copyout_sockaddr(sa, PTRIN(args->from),
 1342                     msg.msg_namelen);
 1343 
 1344         if (error == 0 && PTRIN(args->fromlen) != NULL)
 1345                 error = copyout(&msg.msg_namelen, PTRIN(args->fromlen),
 1346                     sizeof(msg.msg_namelen));
 1347 out:
 1348         free(sa, M_SONAME);
 1349         return (error);
 1350 }
 1351 
 1352 static int
 1353 linux_sendmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
 1354     l_uint flags)
 1355 {
 1356         struct cmsghdr *cmsg;
 1357         struct mbuf *control;
 1358         struct msghdr msg;
 1359         struct l_cmsghdr linux_cmsg;
 1360         struct l_cmsghdr *ptr_cmsg;
 1361         struct l_msghdr linux_msghdr;
 1362         struct iovec *iov;
 1363         socklen_t datalen;
 1364         struct sockaddr *sa;
 1365         struct socket *so;
 1366         sa_family_t sa_family;
 1367         struct file *fp;
 1368         void *data;
 1369         l_size_t len;
 1370         l_size_t clen;
 1371         int error, fflag;
 1372 
 1373         error = copyin(msghdr, &linux_msghdr, sizeof(linux_msghdr));
 1374         if (error != 0)
 1375                 return (error);
 1376 
 1377         /*
 1378          * Some Linux applications (ping) define a non-NULL control data
 1379          * pointer, but a msg_controllen of 0, which is not allowed in the
 1380          * FreeBSD system call interface.  NULL the msg_control pointer in
 1381          * order to handle this case.  This should be checked, but allows the
 1382          * Linux ping to work.
 1383          */
 1384         if (PTRIN(linux_msghdr.msg_control) != NULL &&
 1385             linux_msghdr.msg_controllen == 0)
 1386                 linux_msghdr.msg_control = PTROUT(NULL);
 1387 
 1388         error = linux_to_bsd_msghdr(&msg, &linux_msghdr);
 1389         if (error != 0)
 1390                 return (error);
 1391 
 1392 #ifdef COMPAT_LINUX32
 1393         error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
 1394             &iov, EMSGSIZE);
 1395 #else
 1396         error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
 1397 #endif
 1398         if (error != 0)
 1399                 return (error);
 1400 
 1401         control = NULL;
 1402 
 1403         error = kern_getsockname(td, s, &sa, &datalen);
 1404         if (error != 0)
 1405                 goto bad;
 1406         sa_family = sa->sa_family;
 1407         free(sa, M_SONAME);
 1408 
 1409         if (flags & LINUX_MSG_OOB) {
 1410                 error = EOPNOTSUPP;
 1411                 if (sa_family == AF_UNIX)
 1412                         goto bad;
 1413 
 1414                 error = getsock_cap(td, s, &cap_send_rights, &fp,
 1415                     &fflag, NULL);
 1416                 if (error != 0)
 1417                         goto bad;
 1418                 so = fp->f_data;
 1419                 if (so->so_type != SOCK_STREAM)
 1420                         error = EOPNOTSUPP;
 1421                 fdrop(fp, td);
 1422                 if (error != 0)
 1423                         goto bad;
 1424         }
 1425 
 1426         if (linux_msghdr.msg_controllen >= sizeof(struct l_cmsghdr)) {
 1427                 error = ENOBUFS;
 1428                 control = m_get(M_WAITOK, MT_CONTROL);
 1429                 MCLGET(control, M_WAITOK);
 1430                 data = mtod(control, void *);
 1431                 datalen = 0;
 1432 
 1433                 ptr_cmsg = PTRIN(linux_msghdr.msg_control);
 1434                 clen = linux_msghdr.msg_controllen;
 1435                 do {
 1436                         error = copyin(ptr_cmsg, &linux_cmsg,
 1437                             sizeof(struct l_cmsghdr));
 1438                         if (error != 0)
 1439                                 goto bad;
 1440 
 1441                         error = EINVAL;
 1442                         if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr) ||
 1443                             linux_cmsg.cmsg_len > clen)
 1444                                 goto bad;
 1445 
 1446                         if (datalen + CMSG_HDRSZ > MCLBYTES)
 1447                                 goto bad;
 1448 
 1449                         /*
 1450                          * Now we support only SCM_RIGHTS and SCM_CRED,
 1451                          * so return EINVAL in any other cmsg_type
 1452                          */
 1453                         cmsg = data;
 1454                         cmsg->cmsg_type =
 1455                             linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type);
 1456                         cmsg->cmsg_level =
 1457                             linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
 1458                         if (cmsg->cmsg_type == -1
 1459                             || cmsg->cmsg_level != SOL_SOCKET) {
 1460                                 linux_msg(curthread,
 1461                                     "unsupported sendmsg cmsg level %d type %d",
 1462                                     linux_cmsg.cmsg_level, linux_cmsg.cmsg_type);
 1463                                 goto bad;
 1464                         }
 1465 
 1466                         /*
 1467                          * Some applications (e.g. pulseaudio) attempt to
 1468                          * send ancillary data even if the underlying protocol
 1469                          * doesn't support it which is not allowed in the
 1470                          * FreeBSD system call interface.
 1471                          */
 1472                         if (sa_family != AF_UNIX)
 1473                                 goto next;
 1474 
 1475                         if (cmsg->cmsg_type == SCM_CREDS) {
 1476                                 len = sizeof(struct cmsgcred);
 1477                                 if (datalen + CMSG_SPACE(len) > MCLBYTES)
 1478                                         goto bad;
 1479 
 1480                                 /*
 1481                                  * The lower levels will fill in the structure
 1482                                  */
 1483                                 memset(CMSG_DATA(data), 0, len);
 1484                         } else {
 1485                                 len = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
 1486                                 if (datalen + CMSG_SPACE(len) < datalen ||
 1487                                     datalen + CMSG_SPACE(len) > MCLBYTES)
 1488                                         goto bad;
 1489 
 1490                                 error = copyin(LINUX_CMSG_DATA(ptr_cmsg),
 1491                                     CMSG_DATA(data), len);
 1492                                 if (error != 0)
 1493                                         goto bad;
 1494                         }
 1495 
 1496                         cmsg->cmsg_len = CMSG_LEN(len);
 1497                         data = (char *)data + CMSG_SPACE(len);
 1498                         datalen += CMSG_SPACE(len);
 1499 
 1500 next:
 1501                         if (clen <= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len))
 1502                                 break;
 1503 
 1504                         clen -= LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len);
 1505                         ptr_cmsg = (struct l_cmsghdr *)((char *)ptr_cmsg +
 1506                             LINUX_CMSG_ALIGN(linux_cmsg.cmsg_len));
 1507                 } while(clen >= sizeof(struct l_cmsghdr));
 1508 
 1509                 control->m_len = datalen;
 1510                 if (datalen == 0) {
 1511                         m_freem(control);
 1512                         control = NULL;
 1513                 }
 1514         }
 1515 
 1516         msg.msg_iov = iov;
 1517         msg.msg_flags = 0;
 1518         error = linux_sendit(td, s, &msg, flags, control, UIO_USERSPACE);
 1519         control = NULL;
 1520 
 1521 bad:
 1522         m_freem(control);
 1523         free(iov, M_IOV);
 1524         return (error);
 1525 }
 1526 
 1527 int
 1528 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
 1529 {
 1530 
 1531         return (linux_sendmsg_common(td, args->s, PTRIN(args->msg),
 1532             args->flags));
 1533 }
 1534 
 1535 int
 1536 linux_sendmmsg(struct thread *td, struct linux_sendmmsg_args *args)
 1537 {
 1538         struct l_mmsghdr *msg;
 1539         l_uint retval;
 1540         int error, datagrams;
 1541 
 1542         if (args->vlen > UIO_MAXIOV)
 1543                 args->vlen = UIO_MAXIOV;
 1544 
 1545         msg = PTRIN(args->msg);
 1546         datagrams = 0;
 1547         while (datagrams < args->vlen) {
 1548                 error = linux_sendmsg_common(td, args->s, &msg->msg_hdr,
 1549                     args->flags);
 1550                 if (error != 0)
 1551                         break;
 1552 
 1553                 retval = td->td_retval[0];
 1554                 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
 1555                 if (error != 0)
 1556                         break;
 1557                 ++msg;
 1558                 ++datagrams;
 1559         }
 1560         if (error == 0)
 1561                 td->td_retval[0] = datagrams;
 1562         return (error);
 1563 }
 1564 
 1565 static int
 1566 recvmsg_scm_rights(struct thread *td, l_uint flags, socklen_t *datalen,
 1567     void **data, void **udata)
 1568 {
 1569         int i, fd, fds, *fdp;
 1570 
 1571         if (flags & LINUX_MSG_CMSG_CLOEXEC) {
 1572                 fds = *datalen / sizeof(int);
 1573                 fdp = *data;
 1574                 for (i = 0; i < fds; i++) {
 1575                         fd = *fdp++;
 1576                         (void)kern_fcntl(td, fd, F_SETFD, FD_CLOEXEC);
 1577                 }
 1578         }
 1579         return (0);
 1580 }
 1581 
 1582 
 1583 static int
 1584 recvmsg_scm_creds(socklen_t *datalen, void **data, void **udata)
 1585 {
 1586         struct cmsgcred *cmcred;
 1587         struct l_ucred lu;
 1588 
 1589         cmcred = *data;
 1590         lu.pid = cmcred->cmcred_pid;
 1591         lu.uid = cmcred->cmcred_uid;
 1592         lu.gid = cmcred->cmcred_gid;
 1593         memmove(*data, &lu, sizeof(lu));
 1594         *datalen = sizeof(lu);
 1595         return (0);
 1596 }
 1597 _Static_assert(sizeof(struct cmsgcred) >= sizeof(struct l_ucred),
 1598     "scm_creds sizeof l_ucred");
 1599 
 1600 static int
 1601 recvmsg_scm_creds2(socklen_t *datalen, void **data, void **udata)
 1602 {
 1603         struct sockcred2 *scred;
 1604         struct l_ucred lu;
 1605 
 1606         scred = *data;
 1607         lu.pid = scred->sc_pid;
 1608         lu.uid = scred->sc_uid;
 1609         lu.gid = scred->sc_gid;
 1610         memmove(*data, &lu, sizeof(lu));
 1611         *datalen = sizeof(lu);
 1612         return (0);
 1613 }
 1614 _Static_assert(sizeof(struct sockcred2) >= sizeof(struct l_ucred),
 1615     "scm_creds2 sizeof l_ucred");
 1616 
 1617 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 1618 static int
 1619 recvmsg_scm_timestamp(l_int msg_type, socklen_t *datalen, void **data,
 1620     void **udata)
 1621 {
 1622         l_sock_timeval ltv64;
 1623         l_timeval ltv;
 1624         struct timeval *tv;
 1625         socklen_t len;
 1626         void *buf;
 1627 
 1628         if (*datalen != sizeof(struct timeval))
 1629                 return (EMSGSIZE);
 1630 
 1631         tv = *data;
 1632 #if defined(COMPAT_LINUX32)
 1633         if (msg_type == LINUX_SCM_TIMESTAMPO &&
 1634             (tv->tv_sec > INT_MAX || tv->tv_sec < INT_MIN))
 1635                 return (EOVERFLOW);
 1636 #endif
 1637         if (msg_type == LINUX_SCM_TIMESTAMPN)
 1638                 len = sizeof(ltv64);
 1639         else
 1640                 len = sizeof(ltv);
 1641 
 1642         buf = malloc(len, M_LINUX, M_WAITOK);
 1643         if (msg_type == LINUX_SCM_TIMESTAMPN) {
 1644                 ltv64.tv_sec = tv->tv_sec;
 1645                 ltv64.tv_usec = tv->tv_usec;
 1646                 memmove(buf, &ltv64, len);
 1647         } else {
 1648                 ltv.tv_sec = tv->tv_sec;
 1649                 ltv.tv_usec = tv->tv_usec;
 1650                 memmove(buf, &ltv, len);
 1651         }
 1652         *data = *udata = buf;
 1653         *datalen = len;
 1654         return (0);
 1655 }
 1656 #else
 1657 _Static_assert(sizeof(struct timeval) == sizeof(l_timeval),
 1658     "scm_timestamp sizeof l_timeval");
 1659 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 1660 
 1661 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 1662 static int
 1663 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
 1664     void **udata)
 1665 {
 1666         struct l_timespec64 ts64;
 1667         struct l_timespec ts32;
 1668         struct timespec ts;
 1669         socklen_t len;
 1670         void *buf;
 1671 
 1672         if (msg_type == LINUX_SCM_TIMESTAMPNSO)
 1673                 len = sizeof(ts32);
 1674         else
 1675                 len = sizeof(ts64);
 1676 
 1677         buf = malloc(len, M_LINUX, M_WAITOK);
 1678         bintime2timespec(*data, &ts);
 1679         if (msg_type == LINUX_SCM_TIMESTAMPNSO) {
 1680                 ts32.tv_sec = ts.tv_sec;
 1681                 ts32.tv_nsec = ts.tv_nsec;
 1682                 memmove(buf, &ts32, len);
 1683         } else {
 1684                 ts64.tv_sec = ts.tv_sec;
 1685                 ts64.tv_nsec = ts.tv_nsec;
 1686                 memmove(buf, &ts64, len);
 1687         }
 1688         *data = *udata = buf;
 1689         *datalen = len;
 1690         return (0);
 1691 }
 1692 #else
 1693 static int
 1694 recvmsg_scm_timestampns(l_int msg_type, socklen_t *datalen, void **data,
 1695     void **udata)
 1696 {
 1697         struct timespec ts;
 1698 
 1699         bintime2timespec(*data, &ts);
 1700         memmove(*data, &ts, sizeof(struct timespec));
 1701         *datalen = sizeof(struct timespec);
 1702         return (0);
 1703 }
 1704 _Static_assert(sizeof(struct bintime) >= sizeof(struct timespec),
 1705     "scm_timestampns sizeof timespec");
 1706 #endif /* __i386__ || (__amd64__ && COMPAT_LINUX32) */
 1707 
 1708 static int
 1709 recvmsg_scm_ip_origdstaddr(socklen_t *datalen, void **data, void **udata)
 1710 {
 1711         struct l_sockaddr *lsa;
 1712         int error;
 1713 
 1714         error = bsd_to_linux_sockaddr(*data, &lsa, *datalen);
 1715         if (error == 0) {
 1716                 *data = *udata = lsa;
 1717                 *datalen = sizeof(*lsa);
 1718         }
 1719         return (error);
 1720 }
 1721 
 1722 static int
 1723 linux_recvmsg_common(struct thread *td, l_int s, struct l_msghdr *msghdr,
 1724     l_uint flags, struct msghdr *msg)
 1725 {
 1726         struct proc *p = td->td_proc;
 1727         struct cmsghdr *cm;
 1728         struct l_cmsghdr *lcm = NULL;
 1729         socklen_t datalen, maxlen, outlen;
 1730         struct l_msghdr l_msghdr;
 1731         struct iovec *iov, *uiov;
 1732         struct mbuf *m, *control = NULL;
 1733         struct mbuf **controlp;
 1734         struct sockaddr *sa;
 1735         caddr_t outbuf;
 1736         void *data, *udata;
 1737         int error;
 1738 
 1739         error = copyin(msghdr, &l_msghdr, sizeof(l_msghdr));
 1740         if (error != 0)
 1741                 return (error);
 1742 
 1743         /*
 1744          * Pass user-supplied recvmsg() flags in msg_flags field,
 1745          * following sys_recvmsg() convention.
 1746         */
 1747         l_msghdr.msg_flags = flags;
 1748 
 1749         error = linux_to_bsd_msghdr(msg, &l_msghdr);
 1750         if (error != 0)
 1751                 return (error);
 1752 
 1753 #ifdef COMPAT_LINUX32
 1754         error = linux32_copyiniov(PTRIN(msg->msg_iov), msg->msg_iovlen,
 1755             &iov, EMSGSIZE);
 1756 #else
 1757         error = copyiniov(msg->msg_iov, msg->msg_iovlen, &iov, EMSGSIZE);
 1758 #endif
 1759         if (error != 0)
 1760                 return (error);
 1761 
 1762         if (msg->msg_name != NULL && msg->msg_namelen > 0) {
 1763                 msg->msg_namelen = min(msg->msg_namelen, SOCK_MAXADDRLEN);
 1764                 sa = malloc(msg->msg_namelen, M_SONAME, M_WAITOK);
 1765                 msg->msg_name = sa;
 1766         } else {
 1767                 sa = NULL;
 1768                 msg->msg_name = NULL;
 1769         }
 1770 
 1771         uiov = msg->msg_iov;
 1772         msg->msg_iov = iov;
 1773         controlp = (msg->msg_control != NULL) ? &control : NULL;
 1774         error = kern_recvit(td, s, msg, UIO_SYSSPACE, controlp);
 1775         msg->msg_iov = uiov;
 1776         if (error != 0)
 1777                 goto bad;
 1778 
 1779         /*
 1780          * Note that kern_recvit() updates msg->msg_namelen.
 1781          */
 1782         if (msg->msg_name != NULL && msg->msg_namelen > 0) {
 1783                 msg->msg_name = PTRIN(l_msghdr.msg_name);
 1784                 error = linux_copyout_sockaddr(sa, msg->msg_name,
 1785                     msg->msg_namelen);
 1786                 if (error != 0)
 1787                         goto bad;
 1788         }
 1789 
 1790         error = bsd_to_linux_msghdr(msg, &l_msghdr);
 1791         if (error != 0)
 1792                 goto bad;
 1793 
 1794         maxlen = l_msghdr.msg_controllen;
 1795         l_msghdr.msg_controllen = 0;
 1796         if (control == NULL)
 1797                 goto out;
 1798 
 1799         lcm = malloc(L_CMSG_HDRSZ, M_LINUX, M_WAITOK | M_ZERO);
 1800         msg->msg_control = mtod(control, struct cmsghdr *);
 1801         msg->msg_controllen = control->m_len;
 1802         outbuf = PTRIN(l_msghdr.msg_control);
 1803         outlen = 0;
 1804         for (m = control; m != NULL; m = m->m_next) {
 1805                 cm = mtod(m, struct cmsghdr *);
 1806                 lcm->cmsg_type = bsd_to_linux_cmsg_type(p, cm->cmsg_type,
 1807                     cm->cmsg_level);
 1808                 lcm->cmsg_level = bsd_to_linux_sockopt_level(cm->cmsg_level);
 1809 
 1810                 data = CMSG_DATA(cm);
 1811                 datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
 1812                 udata = NULL;
 1813                 error = 0;
 1814 
 1815                 /* Process non SOL_SOCKET types. */
 1816                 if (cm->cmsg_level == IPPROTO_IP &&
 1817                     lcm->cmsg_type == LINUX_IP_ORIGDSTADDR) {
 1818                         error = recvmsg_scm_ip_origdstaddr(&datalen, &data, &udata);
 1819                         goto cont;
 1820                 }
 1821 
 1822                 if (lcm->cmsg_type == -1 ||
 1823                     cm->cmsg_level != SOL_SOCKET) {
 1824                         LINUX_RATELIMIT_MSG_OPT2(
 1825                             "unsupported recvmsg cmsg level %d type %d",
 1826                             cm->cmsg_level, cm->cmsg_type);
 1827                         error = EINVAL;
 1828                         goto bad;
 1829                 }
 1830 
 1831 
 1832                 switch (cm->cmsg_type) {
 1833                 case SCM_RIGHTS:
 1834                         error = recvmsg_scm_rights(td, flags,
 1835                             &datalen, &data, &udata);
 1836                         break;
 1837                 case SCM_CREDS:
 1838                         error = recvmsg_scm_creds(&datalen,
 1839                             &data, &udata);
 1840                         break;
 1841                 case SCM_CREDS2:
 1842                         error = recvmsg_scm_creds2(&datalen,
 1843                             &data, &udata);
 1844                         break;
 1845                 case SCM_TIMESTAMP:
 1846 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 1847                         error = recvmsg_scm_timestamp(lcm->cmsg_type,
 1848                             &datalen, &data, &udata);
 1849 #endif
 1850                         break;
 1851                 case SCM_BINTIME:
 1852                         error = recvmsg_scm_timestampns(lcm->cmsg_type,
 1853                             &datalen, &data, &udata);
 1854                         break;
 1855                 }
 1856 
 1857 cont:
 1858                 if (error != 0)
 1859                         goto bad;
 1860 
 1861                 if (outlen + LINUX_CMSG_LEN(datalen) > maxlen) {
 1862                         if (outlen == 0) {
 1863                                 error = EMSGSIZE;
 1864                                 goto err;
 1865                         } else {
 1866                                 l_msghdr.msg_flags |= LINUX_MSG_CTRUNC;
 1867                                 m_dispose_extcontrolm(control);
 1868                                 free(udata, M_LINUX);
 1869                                 goto out;
 1870                         }
 1871                 }
 1872 
 1873                 lcm->cmsg_len = LINUX_CMSG_LEN(datalen);
 1874                 error = copyout(lcm, outbuf, L_CMSG_HDRSZ);
 1875                 if (error == 0) {
 1876                         outbuf += L_CMSG_HDRSZ;
 1877                         error = copyout(data, outbuf, datalen);
 1878                         if (error == 0) {
 1879                                 outbuf += LINUX_CMSG_ALIGN(datalen);
 1880                                 outlen += LINUX_CMSG_LEN(datalen);
 1881                         }
 1882                 }
 1883 err:
 1884                 free(udata, M_LINUX);
 1885                 if (error != 0)
 1886                         goto bad;
 1887         }
 1888         l_msghdr.msg_controllen = outlen;
 1889 
 1890 out:
 1891         error = copyout(&l_msghdr, msghdr, sizeof(l_msghdr));
 1892 
 1893 bad:
 1894         if (control != NULL) {
 1895                 if (error != 0)
 1896                         m_dispose_extcontrolm(control);
 1897                 m_freem(control);
 1898         }
 1899         free(iov, M_IOV);
 1900         free(lcm, M_LINUX);
 1901         free(sa, M_SONAME);
 1902 
 1903         return (error);
 1904 }
 1905 
 1906 int
 1907 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
 1908 {
 1909         struct msghdr bsd_msg;
 1910         struct file *fp;
 1911         int error;
 1912 
 1913         error = getsock_cap(td, args->s, &cap_recv_rights,
 1914             &fp, NULL, NULL);
 1915         if (error != 0)
 1916                 return (error);
 1917         fdrop(fp, td);
 1918         return (linux_recvmsg_common(td, args->s, PTRIN(args->msg),
 1919             args->flags, &bsd_msg));
 1920 }
 1921 
 1922 static int
 1923 linux_recvmmsg_common(struct thread *td, l_int s, struct l_mmsghdr *msg,
 1924     l_uint vlen, l_uint flags, struct timespec *tts)
 1925 {
 1926         struct msghdr bsd_msg;
 1927         struct timespec ts;
 1928         struct file *fp;
 1929         l_uint retval;
 1930         int error, datagrams;
 1931 
 1932         error = getsock_cap(td, s, &cap_recv_rights,
 1933             &fp, NULL, NULL);
 1934         if (error != 0)
 1935                 return (error);
 1936         datagrams = 0;
 1937         while (datagrams < vlen) {
 1938                 error = linux_recvmsg_common(td, s, &msg->msg_hdr,
 1939                     flags & ~LINUX_MSG_WAITFORONE, &bsd_msg);
 1940                 if (error != 0)
 1941                         break;
 1942 
 1943                 retval = td->td_retval[0];
 1944                 error = copyout(&retval, &msg->msg_len, sizeof(msg->msg_len));
 1945                 if (error != 0)
 1946                         break;
 1947                 ++msg;
 1948                 ++datagrams;
 1949 
 1950                 /*
 1951                  * MSG_WAITFORONE turns on MSG_DONTWAIT after one packet.
 1952                  */
 1953                 if (flags & LINUX_MSG_WAITFORONE)
 1954                         flags |= LINUX_MSG_DONTWAIT;
 1955 
 1956                 /*
 1957                  * See BUGS section of recvmmsg(2).
 1958                  */
 1959                 if (tts) {
 1960                         getnanotime(&ts);
 1961                         timespecsub(&ts, tts, &ts);
 1962                         if (!timespecisset(&ts) || ts.tv_sec > 0)
 1963                                 break;
 1964                 }
 1965                 /* Out of band data, return right away. */
 1966                 if (bsd_msg.msg_flags & MSG_OOB)
 1967                         break;
 1968         }
 1969         if (error == 0)
 1970                 td->td_retval[0] = datagrams;
 1971         fdrop(fp, td);
 1972         return (error);
 1973 }
 1974 
 1975 int
 1976 linux_recvmmsg(struct thread *td, struct linux_recvmmsg_args *args)
 1977 {
 1978         struct timespec ts, tts, *ptts;
 1979         int error;
 1980 
 1981         if (args->timeout) {
 1982                 error = linux_get_timespec(&ts, args->timeout);
 1983                 if (error != 0)
 1984                         return (error);
 1985                 getnanotime(&tts);
 1986                 timespecadd(&tts, &ts, &tts);
 1987                 ptts = &tts;
 1988         }
 1989                 else ptts = NULL;
 1990 
 1991         return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
 1992             args->vlen, args->flags, ptts));
 1993 }
 1994 
 1995 #if defined(__i386__) || (defined(__amd64__) && defined(COMPAT_LINUX32))
 1996 int
 1997 linux_recvmmsg_time64(struct thread *td, struct linux_recvmmsg_time64_args *args)
 1998 {
 1999         struct timespec ts, tts, *ptts;
 2000         int error;
 2001 
 2002         if (args->timeout) {
 2003                 error = linux_get_timespec64(&ts, args->timeout);
 2004                 if (error != 0)
 2005                         return (error);
 2006                 getnanotime(&tts);
 2007                 timespecadd(&tts, &ts, &tts);
 2008                 ptts = &tts;
 2009         }
 2010                 else ptts = NULL;
 2011 
 2012         return (linux_recvmmsg_common(td, args->s, PTRIN(args->msg),
 2013             args->vlen, args->flags, ptts));
 2014 }
 2015 #endif
 2016 
 2017 int
 2018 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
 2019 {
 2020 
 2021         return (kern_shutdown(td, args->s, args->how));
 2022 }
 2023 
 2024 int
 2025 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
 2026 {
 2027         struct proc *p = td->td_proc;
 2028         struct linux_pemuldata *pem;
 2029         l_timeval linux_tv;
 2030         struct sockaddr *sa;
 2031         struct timeval tv;
 2032         socklen_t len;
 2033         int error, level, name, val;
 2034 
 2035         level = linux_to_bsd_sockopt_level(args->level);
 2036         switch (level) {
 2037         case SOL_SOCKET:
 2038                 name = linux_to_bsd_so_sockopt(args->optname);
 2039                 switch (name) {
 2040                 case LOCAL_CREDS_PERSISTENT:
 2041                         level = SOL_LOCAL;
 2042                         break;
 2043                 case SO_RCVTIMEO:
 2044                         /* FALLTHROUGH */
 2045                 case SO_SNDTIMEO:
 2046                         error = copyin(PTRIN(args->optval), &linux_tv,
 2047                             sizeof(linux_tv));
 2048                         if (error != 0)
 2049                                 return (error);
 2050                         tv.tv_sec = linux_tv.tv_sec;
 2051                         tv.tv_usec = linux_tv.tv_usec;
 2052                         return (kern_setsockopt(td, args->s, level,
 2053                             name, &tv, UIO_SYSSPACE, sizeof(tv)));
 2054                         /* NOTREACHED */
 2055                 case SO_TIMESTAMP:
 2056                         /* overwrite SO_BINTIME */
 2057                         val = 0;
 2058                         error = kern_setsockopt(td, args->s, level,
 2059                             SO_BINTIME, &val, UIO_SYSSPACE, sizeof(val));
 2060                         if (error != 0)
 2061                                 return (error);
 2062                         pem = pem_find(p);
 2063                         pem->so_timestamp = args->optname;
 2064                         break;
 2065                 case SO_BINTIME:
 2066                         /* overwrite SO_TIMESTAMP */
 2067                         val = 0;
 2068                         error = kern_setsockopt(td, args->s, level,
 2069                             SO_TIMESTAMP, &val, UIO_SYSSPACE, sizeof(val));
 2070                         if (error != 0)
 2071                                 return (error);
 2072                         pem = pem_find(p);
 2073                         pem->so_timestampns = args->optname;
 2074                         break;
 2075                 default:
 2076                         break;
 2077                 }
 2078                 break;
 2079         case IPPROTO_IP:
 2080                 if (args->optname == LINUX_IP_RECVERR &&
 2081                     linux_ignore_ip_recverr) {
 2082                         /*
 2083                          * XXX: This is a hack to unbreak DNS resolution
 2084                          *      with glibc 2.30 and above.
 2085                          */
 2086                         return (0);
 2087                 }
 2088                 name = linux_to_bsd_ip_sockopt(args->optname);
 2089                 break;
 2090         case IPPROTO_IPV6:
 2091                 name = linux_to_bsd_ip6_sockopt(args->optname);
 2092                 break;
 2093         case IPPROTO_TCP:
 2094                 name = linux_to_bsd_tcp_sockopt(args->optname);
 2095                 break;
 2096         case SOL_NETLINK:
 2097                 level = SOL_SOCKET;
 2098                 name = args->optname;
 2099                 break;
 2100         default:
 2101                 name = -1;
 2102                 break;
 2103         }
 2104         if (name < 0) {
 2105                 if (name == -1)
 2106                         linux_msg(curthread,
 2107                             "unsupported setsockopt level %d optname %d",
 2108                             args->level, args->optname);
 2109                 return (ENOPROTOOPT);
 2110         }
 2111 
 2112         if (name == IPV6_NEXTHOP) {
 2113                 len = args->optlen;
 2114                 error = linux_to_bsd_sockaddr(PTRIN(args->optval), &sa, &len);
 2115                 if (error != 0)
 2116                         return (error);
 2117 
 2118                 error = kern_setsockopt(td, args->s, level,
 2119                     name, sa, UIO_SYSSPACE, len);
 2120                 free(sa, M_SONAME);
 2121         } else {
 2122                 error = kern_setsockopt(td, args->s, level,
 2123                     name, PTRIN(args->optval), UIO_USERSPACE, args->optlen);
 2124         }
 2125 
 2126         return (error);
 2127 }
 2128 
 2129 static int
 2130 linux_sockopt_copyout(struct thread *td, void *val, socklen_t len,
 2131     struct linux_getsockopt_args *args)
 2132 {
 2133         int error;
 2134 
 2135         error = copyout(val, PTRIN(args->optval), len);
 2136         if (error == 0)
 2137                 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
 2138         return (error);
 2139 }
 2140 
 2141 static int
 2142 linux_getsockopt_so_peergroups(struct thread *td,
 2143     struct linux_getsockopt_args *args)
 2144 {
 2145         struct xucred xu;
 2146         socklen_t xulen, len;
 2147         int error, i;
 2148 
 2149         xulen = sizeof(xu);
 2150         error = kern_getsockopt(td, args->s, 0,
 2151             LOCAL_PEERCRED, &xu, UIO_SYSSPACE, &xulen);
 2152         if (error != 0)
 2153                 return (error);
 2154 
 2155         len = xu.cr_ngroups * sizeof(l_gid_t);
 2156         if (args->optlen < len) {
 2157                 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
 2158                 if (error == 0)
 2159                         error = ERANGE;
 2160                 return (error);
 2161         }
 2162 
 2163         /*
 2164          * "- 1" to skip the primary group.
 2165          */
 2166         for (i = 0; i < xu.cr_ngroups - 1; i++) {
 2167                 error = copyout(xu.cr_groups + i + 1,
 2168                     (void *)(args->optval + i * sizeof(l_gid_t)),
 2169                     sizeof(l_gid_t));
 2170                 if (error != 0)
 2171                         return (error);
 2172         }
 2173 
 2174         error = copyout(&len, PTRIN(args->optlen), sizeof(len));
 2175         return (error);
 2176 }
 2177 
 2178 static int
 2179 linux_getsockopt_so_peersec(struct thread *td,
 2180     struct linux_getsockopt_args *args)
 2181 {
 2182         socklen_t len;
 2183         int error;
 2184 
 2185         len = sizeof(SECURITY_CONTEXT_STRING);
 2186         if (args->optlen < len) {
 2187                 error = copyout(&len, PTRIN(args->optlen), sizeof(len));
 2188                 if (error == 0)
 2189                         error = ERANGE;
 2190                 return (error);
 2191         }
 2192 
 2193         return (linux_sockopt_copyout(td, SECURITY_CONTEXT_STRING,
 2194             len, args));
 2195 }
 2196 
 2197 static int
 2198 linux_getsockopt_so_linger(struct thread *td,
 2199     struct linux_getsockopt_args *args)
 2200 {
 2201         struct linger ling;
 2202         socklen_t len;
 2203         int error;
 2204 
 2205         len = sizeof(ling);
 2206         error = kern_getsockopt(td, args->s, SOL_SOCKET,
 2207             SO_LINGER, &ling, UIO_SYSSPACE, &len);
 2208         if (error != 0)
 2209                 return (error);
 2210         ling.l_onoff = ((ling.l_onoff & SO_LINGER) != 0);
 2211         return (linux_sockopt_copyout(td, &ling, len, args));
 2212 }
 2213 
 2214 int
 2215 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
 2216 {
 2217         l_timeval linux_tv;
 2218         struct timeval tv;
 2219         socklen_t tv_len, xulen, len;
 2220         struct sockaddr *sa;
 2221         struct xucred xu;
 2222         struct l_ucred lxu;
 2223         int error, level, name, newval;
 2224 
 2225         level = linux_to_bsd_sockopt_level(args->level);
 2226         switch (level) {
 2227         case SOL_SOCKET:
 2228                 switch (args->optname) {
 2229                 case LINUX_SO_PEERGROUPS:
 2230                         return (linux_getsockopt_so_peergroups(td, args));
 2231                 case LINUX_SO_PEERSEC:
 2232                         return (linux_getsockopt_so_peersec(td, args));
 2233                 default:
 2234                         break;
 2235                 }
 2236 
 2237                 name = linux_to_bsd_so_sockopt(args->optname);
 2238                 switch (name) {
 2239                 case LOCAL_CREDS_PERSISTENT:
 2240                         level = SOL_LOCAL;
 2241                         break;
 2242                 case SO_RCVTIMEO:
 2243                         /* FALLTHROUGH */
 2244                 case SO_SNDTIMEO:
 2245                         tv_len = sizeof(tv);
 2246                         error = kern_getsockopt(td, args->s, level,
 2247                             name, &tv, UIO_SYSSPACE, &tv_len);
 2248                         if (error != 0)
 2249                                 return (error);
 2250                         linux_tv.tv_sec = tv.tv_sec;
 2251                         linux_tv.tv_usec = tv.tv_usec;
 2252                         return (linux_sockopt_copyout(td, &linux_tv,
 2253                             sizeof(linux_tv), args));
 2254                         /* NOTREACHED */
 2255                 case LOCAL_PEERCRED:
 2256                         if (args->optlen < sizeof(lxu))
 2257                                 return (EINVAL);
 2258                         /*
 2259                          * LOCAL_PEERCRED is not served at the SOL_SOCKET level,
 2260                          * but by the Unix socket's level 0.
 2261                          */
 2262                         level = 0;
 2263                         xulen = sizeof(xu);
 2264                         error = kern_getsockopt(td, args->s, level,
 2265                             name, &xu, UIO_SYSSPACE, &xulen);
 2266                         if (error != 0)
 2267                                 return (error);
 2268                         lxu.pid = xu.cr_pid;
 2269                         lxu.uid = xu.cr_uid;
 2270                         lxu.gid = xu.cr_gid;
 2271                         return (linux_sockopt_copyout(td, &lxu,
 2272                             sizeof(lxu), args));
 2273                         /* NOTREACHED */
 2274                 case SO_ERROR:
 2275                         len = sizeof(newval);
 2276                         error = kern_getsockopt(td, args->s, level,
 2277                             name, &newval, UIO_SYSSPACE, &len);
 2278                         if (error != 0)
 2279                                 return (error);
 2280                         newval = -bsd_to_linux_errno(newval);
 2281                         return (linux_sockopt_copyout(td, &newval,
 2282                             len, args));
 2283                         /* NOTREACHED */
 2284                 case SO_DOMAIN:
 2285                         len = sizeof(newval);
 2286                         error = kern_getsockopt(td, args->s, level,
 2287                             name, &newval, UIO_SYSSPACE, &len);
 2288                         if (error != 0)
 2289                                 return (error);
 2290                         newval = bsd_to_linux_domain(newval);
 2291                         if (newval == -1)
 2292                                 return (ENOPROTOOPT);
 2293                         return (linux_sockopt_copyout(td, &newval,
 2294                             len, args));
 2295                         /* NOTREACHED */
 2296                 case SO_LINGER:
 2297                         return (linux_getsockopt_so_linger(td, args));
 2298                         /* NOTREACHED */
 2299                 default:
 2300                         break;
 2301                 }
 2302                 break;
 2303         case IPPROTO_IP:
 2304                 name = linux_to_bsd_ip_sockopt(args->optname);
 2305                 break;
 2306         case IPPROTO_IPV6:
 2307                 name = linux_to_bsd_ip6_sockopt(args->optname);
 2308                 break;
 2309         case IPPROTO_TCP:
 2310                 name = linux_to_bsd_tcp_sockopt(args->optname);
 2311                 break;
 2312         default:
 2313                 name = -1;
 2314                 break;
 2315         }
 2316         if (name < 0) {
 2317                 if (name == -1)
 2318                         linux_msg(curthread,
 2319                             "unsupported getsockopt level %d optname %d",
 2320                             args->level, args->optname);
 2321                 return (EINVAL);
 2322         }
 2323 
 2324         if (name == IPV6_NEXTHOP) {
 2325                 error = copyin(PTRIN(args->optlen), &len, sizeof(len));
 2326                 if (error != 0)
 2327                         return (error);
 2328                 sa = malloc(len, M_SONAME, M_WAITOK);
 2329 
 2330                 error = kern_getsockopt(td, args->s, level,
 2331                     name, sa, UIO_SYSSPACE, &len);
 2332                 if (error != 0)
 2333                         goto out;
 2334 
 2335                 error = linux_copyout_sockaddr(sa, PTRIN(args->optval), len);
 2336                 if (error == 0)
 2337                         error = copyout(&len, PTRIN(args->optlen),
 2338                             sizeof(len));
 2339 out:
 2340                 free(sa, M_SONAME);
 2341         } else {
 2342                 if (args->optval) {
 2343                         error = copyin(PTRIN(args->optlen), &len, sizeof(len));
 2344                         if (error != 0)
 2345                                 return (error);
 2346                 }
 2347                 error = kern_getsockopt(td, args->s, level,
 2348                     name, PTRIN(args->optval), UIO_USERSPACE, &len);
 2349                 if (error == 0)
 2350                         error = copyout(&len, PTRIN(args->optlen),
 2351                             sizeof(len));
 2352         }
 2353 
 2354         return (error);
 2355 }
 2356 
 2357 static int
 2358 linux_sendfile_common(struct thread *td, l_int out, l_int in,
 2359     l_loff_t *offset, l_size_t count)
 2360 {
 2361         off_t bytes_read;
 2362         int error;
 2363         l_loff_t current_offset;
 2364         struct file *fp;
 2365 
 2366         AUDIT_ARG_FD(in);
 2367         error = fget_read(td, in, &cap_pread_rights, &fp);
 2368         if (error != 0)
 2369                 return (error);
 2370 
 2371         if (offset != NULL) {
 2372                 current_offset = *offset;
 2373         } else {
 2374                 error = (fp->f_ops->fo_flags & DFLAG_SEEKABLE) != 0 ?
 2375                     fo_seek(fp, 0, SEEK_CUR, td) : ESPIPE;
 2376                 if (error != 0)
 2377                         goto drop;
 2378                 current_offset = td->td_uretoff.tdu_off;
 2379         }
 2380 
 2381         bytes_read = 0;
 2382 
 2383         /* Linux cannot have 0 count. */
 2384         if (count <= 0 || current_offset < 0) {
 2385                 error = EINVAL;
 2386                 goto drop;
 2387         }
 2388 
 2389         error = fo_sendfile(fp, out, NULL, NULL, current_offset, count,
 2390             &bytes_read, 0, td);
 2391         if (error != 0)
 2392                 goto drop;
 2393         current_offset += bytes_read;
 2394 
 2395         if (offset != NULL) {
 2396                 *offset = current_offset;
 2397         } else {
 2398                 error = fo_seek(fp, current_offset, SEEK_SET, td);
 2399                 if (error != 0)
 2400                         goto drop;
 2401         }
 2402 
 2403         td->td_retval[0] = (ssize_t)bytes_read;
 2404 drop:
 2405         fdrop(fp, td);
 2406         if (error == ENOTSOCK)
 2407                 error = EINVAL;
 2408         return (error);
 2409 }
 2410 
 2411 int
 2412 linux_sendfile(struct thread *td, struct linux_sendfile_args *arg)
 2413 {
 2414         /*
 2415          * Differences between FreeBSD and Linux sendfile:
 2416          * - Linux doesn't send anything when count is 0 (FreeBSD uses 0 to
 2417          *   mean send the whole file.)  In linux_sendfile given fds are still
 2418          *   checked for validity when the count is 0.
 2419          * - Linux can send to any fd whereas FreeBSD only supports sockets.
 2420          *   The same restriction follows for linux_sendfile.
 2421          * - Linux doesn't have an equivalent for FreeBSD's flags and sf_hdtr.
 2422          * - Linux takes an offset pointer and updates it to the read location.
 2423          *   FreeBSD takes in an offset and a 'bytes read' parameter which is
 2424          *   only filled if it isn't NULL.  We use this parameter to update the
 2425          *   offset pointer if it exists.
 2426          * - Linux sendfile returns bytes read on success while FreeBSD
 2427          *   returns 0.  We use the 'bytes read' parameter to get this value.
 2428          */
 2429 
 2430         l_loff_t offset64;
 2431         l_long offset;
 2432         int ret;
 2433         int error;
 2434 
 2435         if (arg->offset != NULL) {
 2436                 error = copyin(arg->offset, &offset, sizeof(offset));
 2437                 if (error != 0)
 2438                         return (error);
 2439                 offset64 = (l_loff_t)offset;
 2440         }
 2441 
 2442         ret = linux_sendfile_common(td, arg->out, arg->in,
 2443             arg->offset != NULL ? &offset64 : NULL, arg->count);
 2444 
 2445         if (arg->offset != NULL) {
 2446 #if defined(__i386__) || defined(__arm__) || \
 2447     (defined(__amd64__) && defined(COMPAT_LINUX32))
 2448                 if (offset64 > INT32_MAX)
 2449                         return (EOVERFLOW);
 2450 #endif
 2451                 offset = (l_long)offset64;
 2452                 error = copyout(&offset, arg->offset, sizeof(offset));
 2453                 if (error != 0)
 2454                         return (error);
 2455         }
 2456 
 2457         return (ret);
 2458 }
 2459 
 2460 #if defined(__i386__) || defined(__arm__) || \
 2461     (defined(__amd64__) && defined(COMPAT_LINUX32))
 2462 
 2463 int
 2464 linux_sendfile64(struct thread *td, struct linux_sendfile64_args *arg)
 2465 {
 2466         l_loff_t offset;
 2467         int ret;
 2468         int error;
 2469 
 2470         if (arg->offset != NULL) {
 2471                 error = copyin(arg->offset, &offset, sizeof(offset));
 2472                 if (error != 0)
 2473                         return (error);
 2474         }
 2475 
 2476         ret = linux_sendfile_common(td, arg->out, arg->in,
 2477                 arg->offset != NULL ? &offset : NULL, arg->count);
 2478 
 2479         if (arg->offset != NULL) {
 2480                 error = copyout(&offset, arg->offset, sizeof(offset));
 2481                 if (error != 0)
 2482                         return (error);
 2483         }
 2484 
 2485         return (ret);
 2486 }
 2487 
 2488 /* Argument list sizes for linux_socketcall */
 2489 static const unsigned char lxs_args_cnt[] = {
 2490         0 /* unused*/,          3 /* socket */,
 2491         3 /* bind */,           3 /* connect */,
 2492         2 /* listen */,         3 /* accept */,
 2493         3 /* getsockname */,    3 /* getpeername */,
 2494         4 /* socketpair */,     4 /* send */,
 2495         4 /* recv */,           6 /* sendto */,
 2496         6 /* recvfrom */,       2 /* shutdown */,
 2497         5 /* setsockopt */,     5 /* getsockopt */,
 2498         3 /* sendmsg */,        3 /* recvmsg */,
 2499         4 /* accept4 */,        5 /* recvmmsg */,
 2500         4 /* sendmmsg */,       4 /* sendfile */
 2501 };
 2502 #define LINUX_ARGS_CNT          (nitems(lxs_args_cnt) - 1)
 2503 #define LINUX_ARG_SIZE(x)       (lxs_args_cnt[x] * sizeof(l_ulong))
 2504 
 2505 int
 2506 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
 2507 {
 2508         l_ulong a[6];
 2509 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 2510         register_t l_args[6];
 2511 #endif
 2512         void *arg;
 2513         int error;
 2514 
 2515         if (args->what < LINUX_SOCKET || args->what > LINUX_ARGS_CNT)
 2516                 return (EINVAL);
 2517         error = copyin(PTRIN(args->args), a, LINUX_ARG_SIZE(args->what));
 2518         if (error != 0)
 2519                 return (error);
 2520 
 2521 #if defined(__amd64__) && defined(COMPAT_LINUX32)
 2522         for (int i = 0; i < lxs_args_cnt[args->what]; ++i)
 2523                 l_args[i] = a[i];
 2524         arg = l_args;
 2525 #else
 2526         arg = a;
 2527 #endif
 2528         switch (args->what) {
 2529         case LINUX_SOCKET:
 2530                 return (linux_socket(td, arg));
 2531         case LINUX_BIND:
 2532                 return (linux_bind(td, arg));
 2533         case LINUX_CONNECT:
 2534                 return (linux_connect(td, arg));
 2535         case LINUX_LISTEN:
 2536                 return (linux_listen(td, arg));
 2537         case LINUX_ACCEPT:
 2538                 return (linux_accept(td, arg));
 2539         case LINUX_GETSOCKNAME:
 2540                 return (linux_getsockname(td, arg));
 2541         case LINUX_GETPEERNAME:
 2542                 return (linux_getpeername(td, arg));
 2543         case LINUX_SOCKETPAIR:
 2544                 return (linux_socketpair(td, arg));
 2545         case LINUX_SEND:
 2546                 return (linux_send(td, arg));
 2547         case LINUX_RECV:
 2548                 return (linux_recv(td, arg));
 2549         case LINUX_SENDTO:
 2550                 return (linux_sendto(td, arg));
 2551         case LINUX_RECVFROM:
 2552                 return (linux_recvfrom(td, arg));
 2553         case LINUX_SHUTDOWN:
 2554                 return (linux_shutdown(td, arg));
 2555         case LINUX_SETSOCKOPT:
 2556                 return (linux_setsockopt(td, arg));
 2557         case LINUX_GETSOCKOPT:
 2558                 return (linux_getsockopt(td, arg));
 2559         case LINUX_SENDMSG:
 2560                 return (linux_sendmsg(td, arg));
 2561         case LINUX_RECVMSG:
 2562                 return (linux_recvmsg(td, arg));
 2563         case LINUX_ACCEPT4:
 2564                 return (linux_accept4(td, arg));
 2565         case LINUX_RECVMMSG:
 2566                 return (linux_recvmmsg(td, arg));
 2567         case LINUX_SENDMMSG:
 2568                 return (linux_sendmmsg(td, arg));
 2569         case LINUX_SENDFILE:
 2570                 return (linux_sendfile(td, arg));
 2571         }
 2572 
 2573         linux_msg(td, "socket type %d not implemented", args->what);
 2574         return (ENOSYS);
 2575 }
 2576 #endif /* __i386__ || __arm__ || (__amd64__ && COMPAT_LINUX32) */

Cache object: 9754482f8f6ed1fc66453a6a29c00e75


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.