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  * Copyright (c) 1995 Søren Schmidt
    3  * All rights reserved.
    4  *
    5  * Redistribution and use in source and binary forms, with or without
    6  * modification, are permitted provided that the following conditions
    7  * are met:
    8  * 1. Redistributions of source code must retain the above copyright
    9  *    notice, this list of conditions and the following disclaimer
   10  *    in this position and unchanged.
   11  * 2. Redistributions in binary form must reproduce the above copyright
   12  *    notice, this list of conditions and the following disclaimer in the
   13  *    documentation and/or other materials provided with the distribution.
   14  * 3. The name of the author may not be used to endorse or promote products
   15  *    derived from this software without specific prior written permission
   16  *
   17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD: releng/8.1/sys/compat/linux/linux_socket.c 226023 2011-10-04 19:07:38Z cperciva $");
   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/fcntl.h>
   41 #include <sys/file.h>
   42 #include <sys/limits.h>
   43 #include <sys/lock.h>
   44 #include <sys/malloc.h>
   45 #include <sys/mutex.h>
   46 #include <sys/mbuf.h>
   47 #include <sys/socket.h>
   48 #include <sys/socketvar.h>
   49 #include <sys/syscallsubr.h>
   50 #include <sys/uio.h>
   51 #include <sys/syslog.h>
   52 #include <sys/un.h>
   53 
   54 #include <net/if.h>
   55 #include <netinet/in.h>
   56 #include <netinet/in_systm.h>
   57 #include <netinet/ip.h>
   58 #ifdef INET6
   59 #include <netinet/ip6.h>
   60 #include <netinet6/ip6_var.h>
   61 #include <netinet6/in6_var.h>
   62 #endif
   63 
   64 #ifdef COMPAT_LINUX32
   65 #include <machine/../linux32/linux.h>
   66 #include <machine/../linux32/linux32_proto.h>
   67 #else
   68 #include <machine/../linux/linux.h>
   69 #include <machine/../linux/linux_proto.h>
   70 #endif
   71 #include <compat/linux/linux_socket.h>
   72 #include <compat/linux/linux_util.h>
   73 
   74 static int do_sa_get(struct sockaddr **, const struct osockaddr *, int *,
   75     struct malloc_type *);
   76 static int linux_to_bsd_domain(int);
   77 
   78 /*
   79  * Reads a linux sockaddr and does any necessary translation.
   80  * Linux sockaddrs don't have a length field, only a family.
   81  */
   82 static int
   83 linux_getsockaddr(struct sockaddr **sap, const struct osockaddr *osa, int len)
   84 {
   85         int osalen = len;
   86 
   87         return (do_sa_get(sap, osa, &osalen, M_SONAME));
   88 }
   89 
   90 /*
   91  * Copy the osockaddr structure pointed to by osa to kernel, adjust
   92  * family and convert to sockaddr.
   93  */
   94 static int
   95 do_sa_get(struct sockaddr **sap, const struct osockaddr *osa, int *osalen,
   96     struct malloc_type *mtype)
   97 {
   98         int error=0, bdom;
   99         struct sockaddr *sa;
  100         struct osockaddr *kosa;
  101         int alloclen;
  102 #ifdef INET6
  103         int oldv6size;
  104         struct sockaddr_in6 *sin6;
  105 #endif
  106         int namelen;
  107 
  108         if (*osalen < 2 || *osalen > UCHAR_MAX || !osa)
  109                 return (EINVAL);
  110 
  111         alloclen = *osalen;
  112 #ifdef INET6
  113         oldv6size = 0;
  114         /*
  115          * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
  116          * if it's a v4-mapped address, so reserve the proper space
  117          * for it.
  118          */
  119         if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
  120                 alloclen = sizeof (struct sockaddr_in6);
  121                 oldv6size = 1;
  122         }
  123 #endif
  124 
  125         kosa = malloc(alloclen, mtype, M_WAITOK);
  126 
  127         if ((error = copyin(osa, kosa, *osalen)))
  128                 goto out;
  129 
  130         bdom = linux_to_bsd_domain(kosa->sa_family);
  131         if (bdom == -1) {
  132                 error = EAFNOSUPPORT;
  133                 goto out;
  134         }
  135 
  136 #ifdef INET6
  137         /*
  138          * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
  139          * which lacks the scope id compared with RFC2553 one. If we detect
  140          * the situation, reject the address and write a message to system log.
  141          *
  142          * Still accept addresses for which the scope id is not used.
  143          */
  144         if (oldv6size && bdom == AF_INET6) {
  145                 sin6 = (struct sockaddr_in6 *)kosa;
  146                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
  147                     (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
  148                      !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
  149                      !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
  150                      !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
  151                      !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
  152                         sin6->sin6_scope_id = 0;
  153                 } else {
  154                         log(LOG_DEBUG,
  155                             "obsolete pre-RFC2553 sockaddr_in6 rejected\n");
  156                         error = EINVAL;
  157                         goto out;
  158                 }
  159         } else
  160 #endif
  161         if (bdom == AF_INET) {
  162                 alloclen = sizeof(struct sockaddr_in);
  163                 if (*osalen < alloclen) {
  164                         error = EINVAL;
  165                         goto out;
  166                 }
  167         }
  168 
  169         if ((bdom == AF_LOCAL) && (*osalen > sizeof(struct sockaddr_un))) {
  170                 for (namelen = 0;
  171                     namelen < *osalen - offsetof(struct sockaddr_un, sun_path);
  172                     namelen++)
  173                         if (!((struct sockaddr_un *)kosa)->sun_path[namelen])
  174                                 break;
  175                 if (namelen + offsetof(struct sockaddr_un, sun_path) >
  176                     sizeof(struct sockaddr_un)) {
  177                         error = EINVAL;
  178                         goto out;
  179                 }
  180                 alloclen = sizeof(struct sockaddr_un);
  181         }
  182 
  183         sa = (struct sockaddr *) kosa;
  184         sa->sa_family = bdom;
  185         sa->sa_len = alloclen;
  186 
  187         *sap = sa;
  188         *osalen = alloclen;
  189         return (0);
  190 
  191 out:
  192         free(kosa, mtype);
  193         return (error);
  194 }
  195 
  196 static int
  197 linux_to_bsd_domain(int domain)
  198 {
  199 
  200         switch (domain) {
  201         case LINUX_AF_UNSPEC:
  202                 return (AF_UNSPEC);
  203         case LINUX_AF_UNIX:
  204                 return (AF_LOCAL);
  205         case LINUX_AF_INET:
  206                 return (AF_INET);
  207         case LINUX_AF_INET6:
  208                 return (AF_INET6);
  209         case LINUX_AF_AX25:
  210                 return (AF_CCITT);
  211         case LINUX_AF_IPX:
  212                 return (AF_IPX);
  213         case LINUX_AF_APPLETALK:
  214                 return (AF_APPLETALK);
  215         }
  216         return (-1);
  217 }
  218 
  219 static int
  220 bsd_to_linux_domain(int domain)
  221 {
  222 
  223         switch (domain) {
  224         case AF_UNSPEC:
  225                 return (LINUX_AF_UNSPEC);
  226         case AF_LOCAL:
  227                 return (LINUX_AF_UNIX);
  228         case AF_INET:
  229                 return (LINUX_AF_INET);
  230         case AF_INET6:
  231                 return (LINUX_AF_INET6);
  232         case AF_CCITT:
  233                 return (LINUX_AF_AX25);
  234         case AF_IPX:
  235                 return (LINUX_AF_IPX);
  236         case AF_APPLETALK:
  237                 return (LINUX_AF_APPLETALK);
  238         }
  239         return (-1);
  240 }
  241 
  242 static int
  243 linux_to_bsd_sockopt_level(int level)
  244 {
  245 
  246         switch (level) {
  247         case LINUX_SOL_SOCKET:
  248                 return (SOL_SOCKET);
  249         }
  250         return (level);
  251 }
  252 
  253 static int
  254 bsd_to_linux_sockopt_level(int level)
  255 {
  256 
  257         switch (level) {
  258         case SOL_SOCKET:
  259                 return (LINUX_SOL_SOCKET);
  260         }
  261         return (level);
  262 }
  263 
  264 static int
  265 linux_to_bsd_ip_sockopt(int opt)
  266 {
  267 
  268         switch (opt) {
  269         case LINUX_IP_TOS:
  270                 return (IP_TOS);
  271         case LINUX_IP_TTL:
  272                 return (IP_TTL);
  273         case LINUX_IP_OPTIONS:
  274                 return (IP_OPTIONS);
  275         case LINUX_IP_MULTICAST_IF:
  276                 return (IP_MULTICAST_IF);
  277         case LINUX_IP_MULTICAST_TTL:
  278                 return (IP_MULTICAST_TTL);
  279         case LINUX_IP_MULTICAST_LOOP:
  280                 return (IP_MULTICAST_LOOP);
  281         case LINUX_IP_ADD_MEMBERSHIP:
  282                 return (IP_ADD_MEMBERSHIP);
  283         case LINUX_IP_DROP_MEMBERSHIP:
  284                 return (IP_DROP_MEMBERSHIP);
  285         case LINUX_IP_HDRINCL:
  286                 return (IP_HDRINCL);
  287         }
  288         return (-1);
  289 }
  290 
  291 static int
  292 linux_to_bsd_so_sockopt(int opt)
  293 {
  294 
  295         switch (opt) {
  296         case LINUX_SO_DEBUG:
  297                 return (SO_DEBUG);
  298         case LINUX_SO_REUSEADDR:
  299                 return (SO_REUSEADDR);
  300         case LINUX_SO_TYPE:
  301                 return (SO_TYPE);
  302         case LINUX_SO_ERROR:
  303                 return (SO_ERROR);
  304         case LINUX_SO_DONTROUTE:
  305                 return (SO_DONTROUTE);
  306         case LINUX_SO_BROADCAST:
  307                 return (SO_BROADCAST);
  308         case LINUX_SO_SNDBUF:
  309                 return (SO_SNDBUF);
  310         case LINUX_SO_RCVBUF:
  311                 return (SO_RCVBUF);
  312         case LINUX_SO_KEEPALIVE:
  313                 return (SO_KEEPALIVE);
  314         case LINUX_SO_OOBINLINE:
  315                 return (SO_OOBINLINE);
  316         case LINUX_SO_LINGER:
  317                 return (SO_LINGER);
  318         case LINUX_SO_PEERCRED:
  319                 return (LOCAL_PEERCRED);
  320         case LINUX_SO_RCVLOWAT:
  321                 return (SO_RCVLOWAT);
  322         case LINUX_SO_SNDLOWAT:
  323                 return (SO_SNDLOWAT);
  324         case LINUX_SO_RCVTIMEO:
  325                 return (SO_RCVTIMEO);
  326         case LINUX_SO_SNDTIMEO:
  327                 return (SO_SNDTIMEO);
  328         case LINUX_SO_TIMESTAMP:
  329                 return (SO_TIMESTAMP);
  330         case LINUX_SO_ACCEPTCONN:
  331                 return (SO_ACCEPTCONN);
  332         }
  333         return (-1);
  334 }
  335 
  336 static int
  337 linux_to_bsd_msg_flags(int flags)
  338 {
  339         int ret_flags = 0;
  340 
  341         if (flags & LINUX_MSG_OOB)
  342                 ret_flags |= MSG_OOB;
  343         if (flags & LINUX_MSG_PEEK)
  344                 ret_flags |= MSG_PEEK;
  345         if (flags & LINUX_MSG_DONTROUTE)
  346                 ret_flags |= MSG_DONTROUTE;
  347         if (flags & LINUX_MSG_CTRUNC)
  348                 ret_flags |= MSG_CTRUNC;
  349         if (flags & LINUX_MSG_TRUNC)
  350                 ret_flags |= MSG_TRUNC;
  351         if (flags & LINUX_MSG_DONTWAIT)
  352                 ret_flags |= MSG_DONTWAIT;
  353         if (flags & LINUX_MSG_EOR)
  354                 ret_flags |= MSG_EOR;
  355         if (flags & LINUX_MSG_WAITALL)
  356                 ret_flags |= MSG_WAITALL;
  357         if (flags & LINUX_MSG_NOSIGNAL)
  358                 ret_flags |= MSG_NOSIGNAL;
  359 #if 0 /* not handled */
  360         if (flags & LINUX_MSG_PROXY)
  361                 ;
  362         if (flags & LINUX_MSG_FIN)
  363                 ;
  364         if (flags & LINUX_MSG_SYN)
  365                 ;
  366         if (flags & LINUX_MSG_CONFIRM)
  367                 ;
  368         if (flags & LINUX_MSG_RST)
  369                 ;
  370         if (flags & LINUX_MSG_ERRQUEUE)
  371                 ;
  372 #endif
  373         return ret_flags;
  374 }
  375 
  376 /*
  377 * If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the
  378 * native syscall will fault.  Thus, we don't really need to check the
  379 * return values for these functions.
  380 */
  381 
  382 static int
  383 bsd_to_linux_sockaddr(struct sockaddr *arg)
  384 {
  385         struct sockaddr sa;
  386         size_t sa_len = sizeof(struct sockaddr);
  387         int error;
  388         
  389         if ((error = copyin(arg, &sa, sa_len)))
  390                 return (error);
  391         
  392         *(u_short *)&sa = sa.sa_family;
  393         
  394         error = copyout(&sa, arg, sa_len);
  395         
  396         return (error);
  397 }
  398 
  399 static int
  400 linux_to_bsd_sockaddr(struct sockaddr *arg, int len)
  401 {
  402         struct sockaddr sa;
  403         size_t sa_len = sizeof(struct sockaddr);
  404         int error;
  405 
  406         if ((error = copyin(arg, &sa, sa_len)))
  407                 return (error);
  408 
  409         sa.sa_family = *(sa_family_t *)&sa;
  410         sa.sa_len = len;
  411 
  412         error = copyout(&sa, arg, sa_len);
  413 
  414         return (error);
  415 }
  416 
  417 
  418 static int
  419 linux_sa_put(struct osockaddr *osa)
  420 {
  421         struct osockaddr sa;
  422         int error, bdom;
  423 
  424         /*
  425          * Only read/write the osockaddr family part, the rest is
  426          * not changed.
  427          */
  428         error = copyin(osa, &sa, sizeof(sa.sa_family));
  429         if (error)
  430                 return (error);
  431 
  432         bdom = bsd_to_linux_domain(sa.sa_family);
  433         if (bdom == -1)
  434                 return (EINVAL);
  435 
  436         sa.sa_family = bdom;
  437         error = copyout(&sa, osa, sizeof(sa.sa_family));
  438         if (error)
  439                 return (error);
  440 
  441         return (0);
  442 }
  443 
  444 static int
  445 linux_to_bsd_cmsg_type(int cmsg_type)
  446 {
  447 
  448         switch (cmsg_type) {
  449         case LINUX_SCM_RIGHTS:
  450                 return (SCM_RIGHTS);
  451         }
  452         return (-1);
  453 }
  454 
  455 static int
  456 bsd_to_linux_cmsg_type(int cmsg_type)
  457 {
  458 
  459         switch (cmsg_type) {
  460         case SCM_RIGHTS:
  461                 return (LINUX_SCM_RIGHTS);
  462         }
  463         return (-1);
  464 }
  465 
  466 static int
  467 linux_to_bsd_msghdr(struct msghdr *bhdr, const struct l_msghdr *lhdr)
  468 {
  469         if (lhdr->msg_controllen > INT_MAX)
  470                 return (ENOBUFS);
  471 
  472         bhdr->msg_name          = PTRIN(lhdr->msg_name);
  473         bhdr->msg_namelen       = lhdr->msg_namelen;
  474         bhdr->msg_iov           = PTRIN(lhdr->msg_iov);
  475         bhdr->msg_iovlen        = lhdr->msg_iovlen;
  476         bhdr->msg_control       = PTRIN(lhdr->msg_control);
  477         bhdr->msg_controllen    = lhdr->msg_controllen;
  478         bhdr->msg_flags         = linux_to_bsd_msg_flags(lhdr->msg_flags);
  479         return (0);
  480 }
  481 
  482 static int
  483 bsd_to_linux_msghdr(const struct msghdr *bhdr, struct l_msghdr *lhdr)
  484 {
  485         lhdr->msg_name          = PTROUT(bhdr->msg_name);
  486         lhdr->msg_namelen       = bhdr->msg_namelen;
  487         lhdr->msg_iov           = PTROUT(bhdr->msg_iov);
  488         lhdr->msg_iovlen        = bhdr->msg_iovlen;
  489         lhdr->msg_control       = PTROUT(bhdr->msg_control);
  490         lhdr->msg_controllen    = bhdr->msg_controllen;
  491         /* msg_flags skipped */
  492         return (0);
  493 }
  494 
  495 static int
  496 linux_set_socket_flags(struct thread *td, int s, int flags)
  497 {
  498         int error;
  499 
  500         if (flags & LINUX_SOCK_NONBLOCK) {
  501                 error = kern_fcntl(td, s, F_SETFL, O_NONBLOCK);
  502                 if (error)
  503                         return (error);
  504         }
  505         if (flags & LINUX_SOCK_CLOEXEC) {
  506                 error = kern_fcntl(td, s, F_SETFD, FD_CLOEXEC);
  507                 if (error)
  508                         return (error);
  509         }
  510         return (0);
  511 }
  512 
  513 static int
  514 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
  515     struct mbuf *control, enum uio_seg segflg)
  516 {
  517         struct sockaddr *to;
  518         int error;
  519 
  520         if (mp->msg_name != NULL) {
  521                 error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen);
  522                 if (error)
  523                         return (error);
  524                 mp->msg_name = to;
  525         } else
  526                 to = NULL;
  527 
  528         error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
  529             segflg);
  530 
  531         if (to)
  532                 free(to, M_SONAME);
  533         return (error);
  534 }
  535 
  536 /* Return 0 if IP_HDRINCL is set for the given socket. */
  537 static int
  538 linux_check_hdrincl(struct thread *td, int s)
  539 {
  540         int error, optval, size_val;
  541 
  542         size_val = sizeof(optval);
  543         error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
  544             &optval, UIO_SYSSPACE, &size_val);
  545         if (error)
  546                 return (error);
  547 
  548         return (optval == 0);
  549 }
  550 
  551 struct linux_sendto_args {
  552         int s;
  553         l_uintptr_t msg;
  554         int len;
  555         int flags;
  556         l_uintptr_t to;
  557         int tolen;
  558 };
  559 
  560 /*
  561  * Updated sendto() when IP_HDRINCL is set:
  562  * tweak endian-dependent fields in the IP packet.
  563  */
  564 static int
  565 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
  566 {
  567 /*
  568  * linux_ip_copysize defines how many bytes we should copy
  569  * from the beginning of the IP packet before we customize it for BSD.
  570  * It should include all the fields we modify (ip_len and ip_off).
  571  */
  572 #define linux_ip_copysize       8
  573 
  574         struct ip *packet;
  575         struct msghdr msg;
  576         struct iovec aiov[1];
  577         int error;
  578 
  579         /* Check that the packet isn't too big or too small. */
  580         if (linux_args->len < linux_ip_copysize ||
  581             linux_args->len > IP_MAXPACKET)
  582                 return (EINVAL);
  583 
  584         packet = (struct ip *)malloc(linux_args->len, M_TEMP, M_WAITOK);
  585 
  586         /* Make kernel copy of the packet to be sent */
  587         if ((error = copyin(PTRIN(linux_args->msg), packet,
  588             linux_args->len)))
  589                 goto goout;
  590 
  591         /* Convert fields from Linux to BSD raw IP socket format */
  592         packet->ip_len = linux_args->len;
  593         packet->ip_off = ntohs(packet->ip_off);
  594 
  595         /* Prepare the msghdr and iovec structures describing the new packet */
  596         msg.msg_name = PTRIN(linux_args->to);
  597         msg.msg_namelen = linux_args->tolen;
  598         msg.msg_iov = aiov;
  599         msg.msg_iovlen = 1;
  600         msg.msg_control = NULL;
  601         msg.msg_flags = 0;
  602         aiov[0].iov_base = (char *)packet;
  603         aiov[0].iov_len = linux_args->len;
  604         error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
  605             NULL, UIO_SYSSPACE);
  606 goout:
  607         free(packet, M_TEMP);
  608         return (error);
  609 }
  610 
  611 struct linux_socket_args {
  612         int domain;
  613         int type;
  614         int protocol;
  615 };
  616 
  617 static int
  618 linux_socket(struct thread *td, struct linux_socket_args *args)
  619 {
  620         struct socket_args /* {
  621                 int domain;
  622                 int type;
  623                 int protocol;
  624         } */ bsd_args;
  625         int retval_socket, socket_flags;
  626 
  627         bsd_args.protocol = args->protocol;
  628         socket_flags = args->type & ~LINUX_SOCK_TYPE_MASK;
  629         if (socket_flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
  630                 return (EINVAL);
  631         bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
  632         if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
  633                 return (EINVAL);
  634         bsd_args.domain = linux_to_bsd_domain(args->domain);
  635         if (bsd_args.domain == -1)
  636                 return (EAFNOSUPPORT);
  637 
  638         retval_socket = socket(td, &bsd_args);
  639         if (retval_socket)
  640                 return (retval_socket);
  641 
  642         retval_socket = linux_set_socket_flags(td, td->td_retval[0],
  643             socket_flags);
  644         if (retval_socket) {
  645                 (void)kern_close(td, td->td_retval[0]);
  646                 goto out;
  647         }
  648 
  649         if (bsd_args.type == SOCK_RAW
  650             && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
  651             && bsd_args.domain == PF_INET) {
  652                 /* It's a raw IP socket: set the IP_HDRINCL option. */
  653                 int hdrincl;
  654 
  655                 hdrincl = 1;
  656                 /* We ignore any error returned by kern_setsockopt() */
  657                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
  658                     &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
  659         }
  660 #ifdef INET6
  661         /*
  662          * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default
  663          * and some apps depend on this. So, set V6ONLY to 0 for Linux apps.
  664          * For simplicity we do this unconditionally of the net.inet6.ip6.v6only
  665          * sysctl value.
  666          */
  667         if (bsd_args.domain == PF_INET6) {
  668                 int v6only;
  669 
  670                 v6only = 0;
  671                 /* We ignore any error returned by setsockopt() */
  672                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
  673                     &v6only, UIO_SYSSPACE, sizeof(v6only));
  674         }
  675 #endif
  676 
  677 out:
  678         return (retval_socket);
  679 }
  680 
  681 struct linux_bind_args {
  682         int s;
  683         l_uintptr_t name;
  684         int namelen;
  685 };
  686 
  687 static int
  688 linux_bind(struct thread *td, struct linux_bind_args *args)
  689 {
  690         struct sockaddr *sa;
  691         int error;
  692 
  693         error = linux_getsockaddr(&sa, PTRIN(args->name),
  694             args->namelen);
  695         if (error)
  696                 return (error);
  697 
  698         error = kern_bind(td, args->s, sa);
  699         free(sa, M_SONAME);
  700         if (error == EADDRNOTAVAIL && args->namelen != sizeof(struct sockaddr_in))
  701                 return (EINVAL);
  702         return (error);
  703 }
  704 
  705 struct linux_connect_args {
  706         int s;
  707         l_uintptr_t name;
  708         int namelen;
  709 };
  710 int linux_connect(struct thread *, struct linux_connect_args *);
  711 
  712 int
  713 linux_connect(struct thread *td, struct linux_connect_args *args)
  714 {
  715         struct socket *so;
  716         struct sockaddr *sa;
  717         u_int fflag;
  718         int error;
  719 
  720         error = linux_getsockaddr(&sa, (struct osockaddr *)PTRIN(args->name),
  721             args->namelen);
  722         if (error)
  723                 return (error);
  724 
  725         error = kern_connect(td, args->s, sa);
  726         free(sa, M_SONAME);
  727         if (error != EISCONN)
  728                 return (error);
  729 
  730         /*
  731          * Linux doesn't return EISCONN the first time it occurs,
  732          * when on a non-blocking socket. Instead it returns the
  733          * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
  734          *
  735          * XXXRW: Instead of using fgetsock(), check that it is a
  736          * socket and use the file descriptor reference instead of
  737          * creating a new one.
  738          */
  739         error = fgetsock(td, args->s, &so, &fflag);
  740         if (error == 0) {
  741                 error = EISCONN;
  742                 if (fflag & FNONBLOCK) {
  743                         SOCK_LOCK(so);
  744                         if (so->so_emuldata == 0)
  745                                 error = so->so_error;
  746                         so->so_emuldata = (void *)1;
  747                         SOCK_UNLOCK(so);
  748                 }
  749                 fputsock(so);
  750         }
  751         return (error);
  752 }
  753 
  754 struct linux_listen_args {
  755         int s;
  756         int backlog;
  757 };
  758 
  759 static int
  760 linux_listen(struct thread *td, struct linux_listen_args *args)
  761 {
  762         struct listen_args /* {
  763                 int s;
  764                 int backlog;
  765         } */ bsd_args;
  766 
  767         bsd_args.s = args->s;
  768         bsd_args.backlog = args->backlog;
  769         return (listen(td, &bsd_args));
  770 }
  771 
  772 static int
  773 linux_accept_common(struct thread *td, int s, l_uintptr_t addr,
  774     l_uintptr_t namelen, int flags)
  775 {
  776         struct accept_args /* {
  777                 int     s;
  778                 struct sockaddr * __restrict name;
  779                 socklen_t * __restrict anamelen;
  780         } */ bsd_args;
  781         int error;
  782 
  783         if (flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
  784                 return (EINVAL);
  785 
  786         bsd_args.s = s;
  787         /* XXX: */
  788         bsd_args.name = (struct sockaddr * __restrict)PTRIN(addr);
  789         bsd_args.anamelen = PTRIN(namelen);/* XXX */
  790         error = accept(td, &bsd_args);
  791         bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
  792         if (error) {
  793                 if (error == EFAULT && namelen != sizeof(struct sockaddr_in))
  794                         return (EINVAL);
  795                 return (error);
  796         }
  797 
  798         /*
  799          * linux appears not to copy flags from the parent socket to the
  800          * accepted one, so we must clear the flags in the new descriptor
  801          * and apply the requested flags.
  802          */
  803         error = kern_fcntl(td, td->td_retval[0], F_SETFL, 0);
  804         if (error)
  805                 goto out;
  806         error = linux_set_socket_flags(td, td->td_retval[0], flags);
  807         if (error)
  808                 goto out;
  809         if (addr)
  810                 error = linux_sa_put(PTRIN(addr));
  811 
  812 out:
  813         if (error) {
  814                 (void)kern_close(td, td->td_retval[0]);
  815                 td->td_retval[0] = 0;
  816         }
  817         return (error);
  818 }
  819 
  820 struct linux_accept_args {
  821         int s;
  822         l_uintptr_t addr;
  823         l_uintptr_t namelen;
  824 };
  825 
  826 static int
  827 linux_accept(struct thread *td, struct linux_accept_args *args)
  828 {
  829 
  830         return (linux_accept_common(td, args->s, args->addr,
  831             args->namelen, 0));
  832 }
  833 
  834 struct linux_accept4_args {
  835         int s;
  836         l_uintptr_t addr;
  837         l_uintptr_t namelen;
  838         int flags;
  839 };
  840 
  841 static int
  842 linux_accept4(struct thread *td, struct linux_accept4_args *args)
  843 {
  844 
  845         return (linux_accept_common(td, args->s, args->addr,
  846             args->namelen, args->flags));
  847 }
  848 
  849 struct linux_getsockname_args {
  850         int s;
  851         l_uintptr_t addr;
  852         l_uintptr_t namelen;
  853 };
  854 
  855 static int
  856 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
  857 {
  858         struct getsockname_args /* {
  859                 int     fdes;
  860                 struct sockaddr * __restrict asa;
  861                 socklen_t * __restrict alen;
  862         } */ bsd_args;
  863         int error;
  864 
  865         bsd_args.fdes = args->s;
  866         /* XXX: */
  867         bsd_args.asa = (struct sockaddr * __restrict)PTRIN(args->addr);
  868         bsd_args.alen = PTRIN(args->namelen);   /* XXX */
  869         error = getsockname(td, &bsd_args);
  870         bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
  871         if (error)
  872                 return (error);
  873         error = linux_sa_put(PTRIN(args->addr));
  874         if (error)
  875                 return (error);
  876         return (0);
  877 }
  878 
  879 struct linux_getpeername_args {
  880         int s;
  881         l_uintptr_t addr;
  882         l_uintptr_t namelen;
  883 };
  884 
  885 static int
  886 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
  887 {
  888         struct getpeername_args /* {
  889                 int fdes;
  890                 caddr_t asa;
  891                 int *alen;
  892         } */ bsd_args;
  893         int error;
  894 
  895         bsd_args.fdes = args->s;
  896         bsd_args.asa = (struct sockaddr *)PTRIN(args->addr);
  897         bsd_args.alen = (int *)PTRIN(args->namelen);
  898         error = getpeername(td, &bsd_args);
  899         bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
  900         if (error)
  901                 return (error);
  902         error = linux_sa_put(PTRIN(args->addr));
  903         if (error)
  904                 return (error);
  905         return (0);
  906 }
  907 
  908 struct linux_socketpair_args {
  909         int domain;
  910         int type;
  911         int protocol;
  912         l_uintptr_t rsv;
  913 };
  914 
  915 static int
  916 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
  917 {
  918         struct socketpair_args /* {
  919                 int domain;
  920                 int type;
  921                 int protocol;
  922                 int *rsv;
  923         } */ bsd_args;
  924         int error, socket_flags;
  925         int sv[2];
  926 
  927         bsd_args.domain = linux_to_bsd_domain(args->domain);
  928         if (bsd_args.domain != PF_LOCAL)
  929                 return (EAFNOSUPPORT);
  930 
  931         socket_flags = args->type & ~LINUX_SOCK_TYPE_MASK;
  932         if (socket_flags & ~(LINUX_SOCK_CLOEXEC | LINUX_SOCK_NONBLOCK))
  933                 return (EINVAL);
  934         bsd_args.type = args->type & LINUX_SOCK_TYPE_MASK;
  935         if (bsd_args.type < 0 || bsd_args.type > LINUX_SOCK_MAX)
  936                 return (EINVAL);
  937 
  938         if (args->protocol != 0 && args->protocol != PF_UNIX)
  939 
  940                 /*
  941                  * Use of PF_UNIX as protocol argument is not right,
  942                  * but Linux does it.
  943                  * Do not map PF_UNIX as its Linux value is identical
  944                  * to FreeBSD one.
  945                  */
  946                 return (EPROTONOSUPPORT);
  947         else
  948                 bsd_args.protocol = 0;
  949         bsd_args.rsv = (int *)PTRIN(args->rsv);
  950         error = kern_socketpair(td, bsd_args.domain, bsd_args.type,
  951             bsd_args.protocol, sv);
  952         if (error)
  953                 return (error);
  954         error = linux_set_socket_flags(td, sv[0], socket_flags);
  955         if (error)
  956                 goto out;
  957         error = linux_set_socket_flags(td, sv[1], socket_flags);
  958         if (error)
  959                 goto out;
  960 
  961         error = copyout(sv, bsd_args.rsv, 2 * sizeof(int));
  962 
  963 out:
  964         if (error) {
  965                 (void)kern_close(td, sv[0]);
  966                 (void)kern_close(td, sv[1]);
  967         }
  968         return (error);
  969 }
  970 
  971 struct linux_send_args {
  972         int s;
  973         l_uintptr_t msg;
  974         int len;
  975         int flags;
  976 };
  977 
  978 static int
  979 linux_send(struct thread *td, struct linux_send_args *args)
  980 {
  981         struct sendto_args /* {
  982                 int s;
  983                 caddr_t buf;
  984                 int len;
  985                 int flags;
  986                 caddr_t to;
  987                 int tolen;
  988         } */ bsd_args;
  989 
  990         bsd_args.s = args->s;
  991         bsd_args.buf = (caddr_t)PTRIN(args->msg);
  992         bsd_args.len = args->len;
  993         bsd_args.flags = args->flags;
  994         bsd_args.to = NULL;
  995         bsd_args.tolen = 0;
  996         return sendto(td, &bsd_args);
  997 }
  998 
  999 struct linux_recv_args {
 1000         int s;
 1001         l_uintptr_t msg;
 1002         int len;
 1003         int flags;
 1004 };
 1005 
 1006 static int
 1007 linux_recv(struct thread *td, struct linux_recv_args *args)
 1008 {
 1009         struct recvfrom_args /* {
 1010                 int s;
 1011                 caddr_t buf;
 1012                 int len;
 1013                 int flags;
 1014                 struct sockaddr *from;
 1015                 socklen_t fromlenaddr;
 1016         } */ bsd_args;
 1017 
 1018         bsd_args.s = args->s;
 1019         bsd_args.buf = (caddr_t)PTRIN(args->msg);
 1020         bsd_args.len = args->len;
 1021         bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
 1022         bsd_args.from = NULL;
 1023         bsd_args.fromlenaddr = 0;
 1024         return (recvfrom(td, &bsd_args));
 1025 }
 1026 
 1027 static int
 1028 linux_sendto(struct thread *td, struct linux_sendto_args *args)
 1029 {
 1030         struct msghdr msg;
 1031         struct iovec aiov;
 1032         int error;
 1033 
 1034         if (linux_check_hdrincl(td, args->s) == 0)
 1035                 /* IP_HDRINCL set, tweak the packet before sending */
 1036                 return (linux_sendto_hdrincl(td, args));
 1037 
 1038         msg.msg_name = PTRIN(args->to);
 1039         msg.msg_namelen = args->tolen;
 1040         msg.msg_iov = &aiov;
 1041         msg.msg_iovlen = 1;
 1042         msg.msg_control = NULL;
 1043         msg.msg_flags = 0;
 1044         aiov.iov_base = PTRIN(args->msg);
 1045         aiov.iov_len = args->len;
 1046         error = linux_sendit(td, args->s, &msg, args->flags, NULL,
 1047             UIO_USERSPACE);
 1048         return (error);
 1049 }
 1050 
 1051 struct linux_recvfrom_args {
 1052         int s;
 1053         l_uintptr_t buf;
 1054         int len;
 1055         int flags;
 1056         l_uintptr_t from;
 1057         l_uintptr_t fromlen;
 1058 };
 1059 
 1060 static int
 1061 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
 1062 {
 1063         struct recvfrom_args /* {
 1064                 int     s;
 1065                 caddr_t buf;
 1066                 size_t  len;
 1067                 int     flags;
 1068                 struct sockaddr * __restrict from;
 1069                 socklen_t * __restrict fromlenaddr;
 1070         } */ bsd_args;
 1071         size_t len;
 1072         int error;
 1073 
 1074         if ((error = copyin(PTRIN(args->fromlen), &len, sizeof(size_t))))
 1075                 return (error);
 1076 
 1077         bsd_args.s = args->s;
 1078         bsd_args.buf = PTRIN(args->buf);
 1079         bsd_args.len = args->len;
 1080         bsd_args.flags = linux_to_bsd_msg_flags(args->flags);
 1081         /* XXX: */
 1082         bsd_args.from = (struct sockaddr * __restrict)PTRIN(args->from);
 1083         bsd_args.fromlenaddr = PTRIN(args->fromlen);/* XXX */
 1084         
 1085         linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len);
 1086         error = recvfrom(td, &bsd_args);
 1087         bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from);
 1088         
 1089         if (error)
 1090                 return (error);
 1091         if (args->from) {
 1092                 error = linux_sa_put((struct osockaddr *)
 1093                     PTRIN(args->from));
 1094                 if (error)
 1095                         return (error);
 1096         }
 1097         return (0);
 1098 }
 1099 
 1100 struct linux_sendmsg_args {
 1101         int s;
 1102         l_uintptr_t msg;
 1103         int flags;
 1104 };
 1105 
 1106 static int
 1107 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
 1108 {
 1109         struct cmsghdr *cmsg;
 1110         struct mbuf *control;
 1111         struct msghdr msg;
 1112         struct l_cmsghdr linux_cmsg;
 1113         struct l_cmsghdr *ptr_cmsg;
 1114         struct l_msghdr linux_msg;
 1115         struct iovec *iov;
 1116         socklen_t datalen;
 1117         void *data;
 1118         int error;
 1119 
 1120         error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg));
 1121         if (error)
 1122                 return (error);
 1123         error = linux_to_bsd_msghdr(&msg, &linux_msg);
 1124         if (error)
 1125                 return (error);
 1126 
 1127         /*
 1128          * Some Linux applications (ping) define a non-NULL control data
 1129          * pointer, but a msg_controllen of 0, which is not allowed in the
 1130          * FreeBSD system call interface.  NULL the msg_control pointer in
 1131          * order to handle this case.  This should be checked, but allows the
 1132          * Linux ping to work.
 1133          */
 1134         if (msg.msg_control != NULL && msg.msg_controllen == 0)
 1135                 msg.msg_control = NULL;
 1136 
 1137 #ifdef COMPAT_LINUX32
 1138         error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
 1139             &iov, EMSGSIZE);
 1140 #else
 1141         error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
 1142 #endif
 1143         if (error)
 1144                 return (error);
 1145 
 1146         if (msg.msg_control != NULL) {
 1147                 error = ENOBUFS;
 1148                 cmsg = malloc(CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO);
 1149                 control = m_get(M_WAIT, MT_CONTROL);
 1150                 if (control == NULL)
 1151                         goto bad;
 1152                 ptr_cmsg = LINUX_CMSG_FIRSTHDR(&msg);
 1153 
 1154                 do {
 1155                         error = copyin(ptr_cmsg, &linux_cmsg,
 1156                             sizeof(struct l_cmsghdr));
 1157                         if (error)
 1158                                 goto bad;
 1159 
 1160                         error = EINVAL;
 1161                         if (linux_cmsg.cmsg_len < sizeof(struct l_cmsghdr))
 1162                                 goto bad;
 1163 
 1164                         /*
 1165                          * Now we support only SCM_RIGHTS, so return EINVAL
 1166                          * in any other cmsg_type
 1167                          */
 1168                         if ((cmsg->cmsg_type =
 1169                             linux_to_bsd_cmsg_type(linux_cmsg.cmsg_type)) == -1)
 1170                                 goto bad;
 1171                         cmsg->cmsg_level =
 1172                             linux_to_bsd_sockopt_level(linux_cmsg.cmsg_level);
 1173 
 1174                         datalen = linux_cmsg.cmsg_len - L_CMSG_HDRSZ;
 1175                         cmsg->cmsg_len = CMSG_LEN(datalen);
 1176                         data = LINUX_CMSG_DATA(ptr_cmsg);
 1177 
 1178                         error = ENOBUFS;
 1179                         if (!m_append(control, CMSG_HDRSZ, (c_caddr_t) cmsg))
 1180                                 goto bad;
 1181                         if (!m_append(control, datalen, (c_caddr_t) data))
 1182                                 goto bad;
 1183                 } while ((ptr_cmsg = LINUX_CMSG_NXTHDR(&msg, ptr_cmsg)));
 1184         } else {
 1185                 control = NULL;
 1186                 cmsg = NULL;
 1187         }
 1188 
 1189         msg.msg_iov = iov;
 1190         msg.msg_flags = 0;
 1191         error = linux_sendit(td, args->s, &msg, args->flags, control,
 1192             UIO_USERSPACE);
 1193 
 1194 bad:
 1195         free(iov, M_IOV);
 1196         if (cmsg)
 1197                 free(cmsg, M_TEMP);
 1198         return (error);
 1199 }
 1200 
 1201 struct linux_recvmsg_args {
 1202         int s;
 1203         l_uintptr_t msg;
 1204         int flags;
 1205 };
 1206 
 1207 static int
 1208 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
 1209 {
 1210         struct cmsghdr *cm;
 1211         struct msghdr msg;
 1212         struct l_cmsghdr *linux_cmsg = NULL;
 1213         socklen_t datalen, outlen, clen;
 1214         struct l_msghdr linux_msg;
 1215         struct iovec *iov, *uiov;
 1216         struct mbuf *control = NULL;
 1217         struct mbuf **controlp;
 1218         caddr_t outbuf;
 1219         void *data;
 1220         int error, i, fd, fds, *fdp;
 1221 
 1222         error = copyin(PTRIN(args->msg), &linux_msg, sizeof(linux_msg));
 1223         if (error)
 1224                 return (error);
 1225 
 1226         error = linux_to_bsd_msghdr(&msg, &linux_msg);
 1227         if (error)
 1228                 return (error);
 1229 
 1230 #ifdef COMPAT_LINUX32
 1231         error = linux32_copyiniov(PTRIN(msg.msg_iov), msg.msg_iovlen,
 1232             &iov, EMSGSIZE);
 1233 #else
 1234         error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
 1235 #endif
 1236         if (error)
 1237                 return (error);
 1238 
 1239         if (msg.msg_name) {
 1240                 error = linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
 1241                     msg.msg_namelen);
 1242                 if (error)
 1243                         goto bad;
 1244         }
 1245 
 1246         uiov = msg.msg_iov;
 1247         msg.msg_iov = iov;
 1248         controlp = (msg.msg_control != NULL) ? &control : NULL;
 1249         error = kern_recvit(td, args->s, &msg, UIO_USERSPACE, controlp);
 1250         msg.msg_iov = uiov;
 1251         if (error)
 1252                 goto bad;
 1253 
 1254         error = bsd_to_linux_msghdr(&msg, &linux_msg);
 1255         if (error)
 1256                 goto bad;
 1257 
 1258         if (linux_msg.msg_name) {
 1259                 error = bsd_to_linux_sockaddr((struct sockaddr *)
 1260                     PTRIN(linux_msg.msg_name));
 1261                 if (error)
 1262                         goto bad;
 1263         }
 1264         if (linux_msg.msg_name && linux_msg.msg_namelen > 2) {
 1265                 error = linux_sa_put(PTRIN(linux_msg.msg_name));
 1266                 if (error)
 1267                         goto bad;
 1268         }
 1269 
 1270         if (control) {
 1271 
 1272                 linux_cmsg = malloc(L_CMSG_HDRSZ, M_TEMP, M_WAITOK | M_ZERO);
 1273                 outbuf = PTRIN(linux_msg.msg_control);
 1274                 cm = mtod(control, struct cmsghdr *);
 1275                 outlen = 0;
 1276                 clen = control->m_len;
 1277 
 1278                 while (cm != NULL) {
 1279 
 1280                         if ((linux_cmsg->cmsg_type =
 1281                             bsd_to_linux_cmsg_type(cm->cmsg_type)) == -1)
 1282                         {
 1283                                 error = EINVAL;
 1284                                 goto bad;
 1285                         }
 1286                         data = CMSG_DATA(cm);
 1287                         datalen = (caddr_t)cm + cm->cmsg_len - (caddr_t)data;
 1288 
 1289                         switch (linux_cmsg->cmsg_type)
 1290                         {
 1291                         case LINUX_SCM_RIGHTS:
 1292                                 if (outlen + LINUX_CMSG_LEN(datalen) >
 1293                                     linux_msg.msg_controllen) {
 1294                                         if (outlen == 0) {
 1295                                                 error = EMSGSIZE;
 1296                                                 goto bad;
 1297                                         } else {
 1298                                                 linux_msg.msg_flags |=
 1299                                                     LINUX_MSG_CTRUNC;
 1300                                                 goto out;
 1301                                         }
 1302                                 }
 1303                                 if (args->flags & LINUX_MSG_CMSG_CLOEXEC) {
 1304                                         fds = datalen / sizeof(int);
 1305                                         fdp = data;
 1306                                         for (i = 0; i < fds; i++) {
 1307                                                 fd = *fdp++;
 1308                                                 (void)kern_fcntl(td, fd,
 1309                                                     F_SETFD, FD_CLOEXEC);
 1310                                         }
 1311                                 }
 1312                                 break;
 1313                         }
 1314 
 1315                         linux_cmsg->cmsg_len = LINUX_CMSG_LEN(datalen);
 1316                         linux_cmsg->cmsg_level =
 1317                             bsd_to_linux_sockopt_level(cm->cmsg_level);
 1318 
 1319                         error = copyout(linux_cmsg, outbuf, L_CMSG_HDRSZ);
 1320                         if (error)
 1321                                 goto bad;
 1322                         outbuf += L_CMSG_HDRSZ;
 1323 
 1324                         error = copyout(data, outbuf, datalen);
 1325                         if (error)
 1326                                 goto bad;
 1327 
 1328                         outbuf += LINUX_CMSG_ALIGN(datalen);
 1329                         outlen += LINUX_CMSG_LEN(datalen);
 1330                         linux_msg.msg_controllen = outlen;
 1331 
 1332                         if (CMSG_SPACE(datalen) < clen) {
 1333                                 clen -= CMSG_SPACE(datalen);
 1334                                 cm = (struct cmsghdr *)
 1335                                     ((caddr_t)cm + CMSG_SPACE(datalen));
 1336                         } else
 1337                                 cm = NULL;
 1338                 }
 1339         }
 1340 
 1341 out:
 1342         error = copyout(&linux_msg, PTRIN(args->msg), sizeof(linux_msg));
 1343 
 1344 bad:
 1345         free(iov, M_IOV);
 1346         if (control != NULL)
 1347                 m_freem(control);
 1348         if (linux_cmsg != NULL)
 1349                 free(linux_cmsg, M_TEMP);
 1350 
 1351         return (error);
 1352 }
 1353 
 1354 struct linux_shutdown_args {
 1355         int s;
 1356         int how;
 1357 };
 1358 
 1359 static int
 1360 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
 1361 {
 1362         struct shutdown_args /* {
 1363                 int s;
 1364                 int how;
 1365         } */ bsd_args;
 1366 
 1367         bsd_args.s = args->s;
 1368         bsd_args.how = args->how;
 1369         return (shutdown(td, &bsd_args));
 1370 }
 1371 
 1372 struct linux_setsockopt_args {
 1373         int s;
 1374         int level;
 1375         int optname;
 1376         l_uintptr_t optval;
 1377         int optlen;
 1378 };
 1379 
 1380 static int
 1381 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
 1382 {
 1383         struct setsockopt_args /* {
 1384                 int s;
 1385                 int level;
 1386                 int name;
 1387                 caddr_t val;
 1388                 int valsize;
 1389         } */ bsd_args;
 1390         l_timeval linux_tv;
 1391         struct timeval tv;
 1392         int error, name;
 1393 
 1394         bsd_args.s = args->s;
 1395         bsd_args.level = linux_to_bsd_sockopt_level(args->level);
 1396         switch (bsd_args.level) {
 1397         case SOL_SOCKET:
 1398                 name = linux_to_bsd_so_sockopt(args->optname);
 1399                 switch (name) {
 1400                 case SO_RCVTIMEO:
 1401                         /* FALLTHROUGH */
 1402                 case SO_SNDTIMEO:
 1403                         error = copyin(PTRIN(args->optval), &linux_tv,
 1404                             sizeof(linux_tv));
 1405                         if (error)
 1406                                 return (error);
 1407                         tv.tv_sec = linux_tv.tv_sec;
 1408                         tv.tv_usec = linux_tv.tv_usec;
 1409                         return (kern_setsockopt(td, args->s, bsd_args.level,
 1410                             name, &tv, UIO_SYSSPACE, sizeof(tv)));
 1411                         /* NOTREACHED */
 1412                         break;
 1413                 default:
 1414                         break;
 1415                 }
 1416                 break;
 1417         case IPPROTO_IP:
 1418                 name = linux_to_bsd_ip_sockopt(args->optname);
 1419                 break;
 1420         case IPPROTO_TCP:
 1421                 /* Linux TCP option values match BSD's */
 1422                 name = args->optname;
 1423                 break;
 1424         default:
 1425                 name = -1;
 1426                 break;
 1427         }
 1428         if (name == -1)
 1429                 return (ENOPROTOOPT);
 1430 
 1431         bsd_args.name = name;
 1432         bsd_args.val = PTRIN(args->optval);
 1433         bsd_args.valsize = args->optlen;
 1434 
 1435         if (name == IPV6_NEXTHOP) {
 1436                 linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
 1437                         bsd_args.valsize);
 1438                 error = setsockopt(td, &bsd_args);
 1439                 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
 1440         } else
 1441                 error = setsockopt(td, &bsd_args);
 1442 
 1443         return (error);
 1444 }
 1445 
 1446 struct linux_getsockopt_args {
 1447         int s;
 1448         int level;
 1449         int optname;
 1450         l_uintptr_t optval;
 1451         l_uintptr_t optlen;
 1452 };
 1453 
 1454 static int
 1455 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
 1456 {
 1457         struct getsockopt_args /* {
 1458                 int s;
 1459                 int level;
 1460                 int name;
 1461                 caddr_t val;
 1462                 int *avalsize;
 1463         } */ bsd_args;
 1464         l_timeval linux_tv;
 1465         struct timeval tv;
 1466         socklen_t tv_len, xulen;
 1467         struct xucred xu;
 1468         struct l_ucred lxu;
 1469         int error, name;
 1470 
 1471         bsd_args.s = args->s;
 1472         bsd_args.level = linux_to_bsd_sockopt_level(args->level);
 1473         switch (bsd_args.level) {
 1474         case SOL_SOCKET:
 1475                 name = linux_to_bsd_so_sockopt(args->optname);
 1476                 switch (name) {
 1477                 case SO_RCVTIMEO:
 1478                         /* FALLTHROUGH */
 1479                 case SO_SNDTIMEO:
 1480                         tv_len = sizeof(tv);
 1481                         error = kern_getsockopt(td, args->s, bsd_args.level,
 1482                             name, &tv, UIO_SYSSPACE, &tv_len);
 1483                         if (error)
 1484                                 return (error);
 1485                         linux_tv.tv_sec = tv.tv_sec;
 1486                         linux_tv.tv_usec = tv.tv_usec;
 1487                         return (copyout(&linux_tv, PTRIN(args->optval),
 1488                             sizeof(linux_tv)));
 1489                         /* NOTREACHED */
 1490                         break;
 1491                 case LOCAL_PEERCRED:
 1492                         if (args->optlen != sizeof(lxu))
 1493                                 return (EINVAL);
 1494                         xulen = sizeof(xu);
 1495                         error = kern_getsockopt(td, args->s, bsd_args.level,
 1496                             name, &xu, UIO_SYSSPACE, &xulen);
 1497                         if (error)
 1498                                 return (error);
 1499                         /*
 1500                          * XXX Use 0 for pid as the FreeBSD does not cache peer pid.
 1501                          */
 1502                         lxu.pid = 0;
 1503                         lxu.uid = xu.cr_uid;
 1504                         lxu.gid = xu.cr_gid;
 1505                         return (copyout(&lxu, PTRIN(args->optval), sizeof(lxu)));
 1506                         /* NOTREACHED */
 1507                         break;
 1508                 default:
 1509                         break;
 1510                 }
 1511                 break;
 1512         case IPPROTO_IP:
 1513                 name = linux_to_bsd_ip_sockopt(args->optname);
 1514                 break;
 1515         case IPPROTO_TCP:
 1516                 /* Linux TCP option values match BSD's */
 1517                 name = args->optname;
 1518                 break;
 1519         default:
 1520                 name = -1;
 1521                 break;
 1522         }
 1523         if (name == -1)
 1524                 return (EINVAL);
 1525 
 1526         bsd_args.name = name;
 1527         bsd_args.val = PTRIN(args->optval);
 1528         bsd_args.avalsize = PTRIN(args->optlen);
 1529 
 1530         if (name == IPV6_NEXTHOP) {
 1531                 error = getsockopt(td, &bsd_args);
 1532                 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
 1533         } else
 1534                 error = getsockopt(td, &bsd_args);
 1535 
 1536         return (error);
 1537 }
 1538 
 1539 /* Argument list sizes for linux_socketcall */
 1540 
 1541 #define LINUX_AL(x) ((x) * sizeof(l_ulong))
 1542 
 1543 static const unsigned char lxs_args[] = {
 1544         LINUX_AL(0) /* unused*/,        LINUX_AL(3) /* socket */,
 1545         LINUX_AL(3) /* bind */,         LINUX_AL(3) /* connect */,
 1546         LINUX_AL(2) /* listen */,       LINUX_AL(3) /* accept */,
 1547         LINUX_AL(3) /* getsockname */,  LINUX_AL(3) /* getpeername */,
 1548         LINUX_AL(4) /* socketpair */,   LINUX_AL(4) /* send */,
 1549         LINUX_AL(4) /* recv */,         LINUX_AL(6) /* sendto */,
 1550         LINUX_AL(6) /* recvfrom */,     LINUX_AL(2) /* shutdown */,
 1551         LINUX_AL(5) /* setsockopt */,   LINUX_AL(5) /* getsockopt */,
 1552         LINUX_AL(3) /* sendmsg */,      LINUX_AL(3) /* recvmsg */,
 1553         LINUX_AL(4) /* accept4 */
 1554 };
 1555 
 1556 #define LINUX_AL_SIZE   sizeof(lxs_args) / sizeof(lxs_args[0]) - 1
 1557 
 1558 int
 1559 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
 1560 {
 1561         l_ulong a[6];
 1562         void *arg;
 1563         int error;
 1564 
 1565         if (args->what < LINUX_SOCKET || args->what > LINUX_AL_SIZE)
 1566                 return (EINVAL);
 1567         error = copyin(PTRIN(args->args), a, lxs_args[args->what]);
 1568         if (error)
 1569                 return (error);
 1570 
 1571         arg = a;
 1572         switch (args->what) {
 1573         case LINUX_SOCKET:
 1574                 return (linux_socket(td, arg));
 1575         case LINUX_BIND:
 1576                 return (linux_bind(td, arg));
 1577         case LINUX_CONNECT:
 1578                 return (linux_connect(td, arg));
 1579         case LINUX_LISTEN:
 1580                 return (linux_listen(td, arg));
 1581         case LINUX_ACCEPT:
 1582                 return (linux_accept(td, arg));
 1583         case LINUX_GETSOCKNAME:
 1584                 return (linux_getsockname(td, arg));
 1585         case LINUX_GETPEERNAME:
 1586                 return (linux_getpeername(td, arg));
 1587         case LINUX_SOCKETPAIR:
 1588                 return (linux_socketpair(td, arg));
 1589         case LINUX_SEND:
 1590                 return (linux_send(td, arg));
 1591         case LINUX_RECV:
 1592                 return (linux_recv(td, arg));
 1593         case LINUX_SENDTO:
 1594                 return (linux_sendto(td, arg));
 1595         case LINUX_RECVFROM:
 1596                 return (linux_recvfrom(td, arg));
 1597         case LINUX_SHUTDOWN:
 1598                 return (linux_shutdown(td, arg));
 1599         case LINUX_SETSOCKOPT:
 1600                 return (linux_setsockopt(td, arg));
 1601         case LINUX_GETSOCKOPT:
 1602                 return (linux_getsockopt(td, arg));
 1603         case LINUX_SENDMSG:
 1604                 return (linux_sendmsg(td, arg));
 1605         case LINUX_RECVMSG:
 1606                 return (linux_recvmsg(td, arg));
 1607         case LINUX_ACCEPT4:
 1608                 return (linux_accept4(td, arg));
 1609         }
 1610 
 1611         uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
 1612         return (ENOSYS);
 1613 }

Cache object: cf478ad8db98969357d0cb7501000622


[ 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.