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

Cache object: b2b24744b120d0416d38f9877a0d4827


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