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$");
   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 <netinet/in.h>
   55 #include <netinet/in_systm.h>
   56 #include <netinet/ip.h>
   57 #ifdef INET6
   58 #include <netinet/ip6.h>
   59 #include <netinet6/ip6_var.h>
   60 #endif
   61 
   62 #ifdef COMPAT_LINUX32
   63 #include <machine/../linux32/linux.h>
   64 #include <machine/../linux32/linux32_proto.h>
   65 #else
   66 #include <machine/../linux/linux.h>
   67 #include <machine/../linux/linux_proto.h>
   68 #endif
   69 #include <compat/linux/linux_socket.h>
   70 #include <compat/linux/linux_util.h>
   71 
   72 static int do_sa_get(struct sockaddr **, const struct osockaddr *, int *,
   73     struct malloc_type *);
   74 static int linux_to_bsd_domain(int);
   75 
   76 /*
   77  * Reads a linux sockaddr and does any necessary translation.
   78  * Linux sockaddrs don't have a length field, only a family.
   79  */
   80 static int
   81 linux_getsockaddr(struct sockaddr **sap, const struct osockaddr *osa, int len)
   82 {
   83         int osalen = len;
   84 
   85         return (do_sa_get(sap, osa, &osalen, M_SONAME));
   86 }
   87 
   88 /*
   89  * Copy the osockaddr structure pointed to by osa to kernel, adjust
   90  * family and convert to sockaddr.
   91  */
   92 static int
   93 do_sa_get(struct sockaddr **sap, const struct osockaddr *osa, int *osalen,
   94     struct malloc_type *mtype)
   95 {
   96         int error=0, bdom;
   97         struct sockaddr *sa;
   98         struct osockaddr *kosa;
   99         int alloclen;
  100 #ifdef INET6
  101         int oldv6size;
  102         struct sockaddr_in6 *sin6;
  103 #endif
  104 
  105         if (*osalen < 2 || *osalen > UCHAR_MAX || !osa)
  106                 return (EINVAL);
  107 
  108         alloclen = *osalen;
  109 #ifdef INET6
  110         oldv6size = 0;
  111         /*
  112          * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
  113          * if it's a v4-mapped address, so reserve the proper space
  114          * for it.
  115          */
  116         if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
  117                 alloclen = sizeof (struct sockaddr_in6);
  118                 oldv6size = 1;
  119         }
  120 #endif
  121 
  122         MALLOC(kosa, struct osockaddr *, alloclen, mtype, M_WAITOK);
  123 
  124         if ((error = copyin(osa, kosa, *osalen)))
  125                 goto out;
  126 
  127         bdom = linux_to_bsd_domain(kosa->sa_family);
  128         if (bdom == -1) {
  129                 error = EINVAL;
  130                 goto out;
  131         }
  132 
  133 #ifdef INET6
  134         /*
  135          * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
  136          * which lacks the scope id compared with RFC2553 one. If we detect
  137          * the situation, reject the address and write a message to system log.
  138          *
  139          * Still accept addresses for which the scope id is not used.
  140          */
  141         if (oldv6size && bdom == AF_INET6) {
  142                 sin6 = (struct sockaddr_in6 *)kosa;
  143                 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
  144                     (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
  145                      !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
  146                      !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
  147                      !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
  148                      !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
  149                         sin6->sin6_scope_id = 0;
  150                 } else {
  151                         log(LOG_DEBUG,
  152                             "obsolete pre-RFC2553 sockaddr_in6 rejected\n");
  153                         error = EINVAL;
  154                         goto out;
  155                 }
  156         } else
  157 #endif
  158         if (bdom == AF_INET)
  159                 alloclen = sizeof(struct sockaddr_in);
  160 
  161         sa = (struct sockaddr *) kosa;
  162         sa->sa_family = bdom;
  163         sa->sa_len = alloclen;
  164 
  165         *sap = sa;
  166         *osalen = alloclen;
  167         return (0);
  168 
  169 out:
  170         FREE(kosa, mtype);
  171         return (error);
  172 }
  173 
  174 static int
  175 linux_to_bsd_domain(int domain)
  176 {
  177 
  178         switch (domain) {
  179         case LINUX_AF_UNSPEC:
  180                 return (AF_UNSPEC);
  181         case LINUX_AF_UNIX:
  182                 return (AF_LOCAL);
  183         case LINUX_AF_INET:
  184                 return (AF_INET);
  185         case LINUX_AF_INET6:
  186                 return (AF_INET6);
  187         case LINUX_AF_AX25:
  188                 return (AF_CCITT);
  189         case LINUX_AF_IPX:
  190                 return (AF_IPX);
  191         case LINUX_AF_APPLETALK:
  192                 return (AF_APPLETALK);
  193         }
  194         return (-1);
  195 }
  196 
  197 static int
  198 bsd_to_linux_domain(int domain)
  199 {
  200 
  201         switch (domain) {
  202         case AF_UNSPEC:
  203                 return (LINUX_AF_UNSPEC);
  204         case AF_LOCAL:
  205                 return (LINUX_AF_UNIX);
  206         case AF_INET:
  207                 return (LINUX_AF_INET);
  208         case AF_INET6:
  209                 return (LINUX_AF_INET6);
  210         case AF_CCITT:
  211                 return (LINUX_AF_AX25);
  212         case AF_IPX:
  213                 return (LINUX_AF_IPX);
  214         case AF_APPLETALK:
  215                 return (LINUX_AF_APPLETALK);
  216         }
  217         return (-1);
  218 }
  219 
  220 static int
  221 linux_to_bsd_sockopt_level(int level)
  222 {
  223 
  224         switch (level) {
  225         case LINUX_SOL_SOCKET:
  226                 return (SOL_SOCKET);
  227         }
  228         return (level);
  229 }
  230 
  231 static int
  232 bsd_to_linux_sockopt_level(int level)
  233 {
  234 
  235         switch (level) {
  236         case SOL_SOCKET:
  237                 return (LINUX_SOL_SOCKET);
  238         }
  239         return (level);
  240 }
  241 
  242 static int
  243 linux_to_bsd_ip_sockopt(int opt)
  244 {
  245 
  246         switch (opt) {
  247         case LINUX_IP_TOS:
  248                 return (IP_TOS);
  249         case LINUX_IP_TTL:
  250                 return (IP_TTL);
  251         case LINUX_IP_OPTIONS:
  252                 return (IP_OPTIONS);
  253         case LINUX_IP_MULTICAST_IF:
  254                 return (IP_MULTICAST_IF);
  255         case LINUX_IP_MULTICAST_TTL:
  256                 return (IP_MULTICAST_TTL);
  257         case LINUX_IP_MULTICAST_LOOP:
  258                 return (IP_MULTICAST_LOOP);
  259         case LINUX_IP_ADD_MEMBERSHIP:
  260                 return (IP_ADD_MEMBERSHIP);
  261         case LINUX_IP_DROP_MEMBERSHIP:
  262                 return (IP_DROP_MEMBERSHIP);
  263         case LINUX_IP_HDRINCL:
  264                 return (IP_HDRINCL);
  265         }
  266         return (-1);
  267 }
  268 
  269 static int
  270 linux_to_bsd_so_sockopt(int opt)
  271 {
  272 
  273         switch (opt) {
  274         case LINUX_SO_DEBUG:
  275                 return (SO_DEBUG);
  276         case LINUX_SO_REUSEADDR:
  277                 return (SO_REUSEADDR);
  278         case LINUX_SO_TYPE:
  279                 return (SO_TYPE);
  280         case LINUX_SO_ERROR:
  281                 return (SO_ERROR);
  282         case LINUX_SO_DONTROUTE:
  283                 return (SO_DONTROUTE);
  284         case LINUX_SO_BROADCAST:
  285                 return (SO_BROADCAST);
  286         case LINUX_SO_SNDBUF:
  287                 return (SO_SNDBUF);
  288         case LINUX_SO_RCVBUF:
  289                 return (SO_RCVBUF);
  290         case LINUX_SO_KEEPALIVE:
  291                 return (SO_KEEPALIVE);
  292         case LINUX_SO_OOBINLINE:
  293                 return (SO_OOBINLINE);
  294         case LINUX_SO_LINGER:
  295                 return (SO_LINGER);
  296         case LINUX_SO_PEERCRED:
  297                 return (LOCAL_PEERCRED);
  298         case LINUX_SO_RCVLOWAT:
  299                 return (SO_RCVLOWAT);
  300         case LINUX_SO_SNDLOWAT:
  301                 return (SO_SNDLOWAT);
  302         case LINUX_SO_RCVTIMEO:
  303                 return (SO_RCVTIMEO);
  304         case LINUX_SO_SNDTIMEO:
  305                 return (SO_SNDTIMEO);
  306         case LINUX_SO_TIMESTAMP:
  307                 return (SO_TIMESTAMP);
  308         case LINUX_SO_ACCEPTCONN:
  309                 return (SO_ACCEPTCONN);
  310         }
  311         return (-1);
  312 }
  313 
  314 static int
  315 linux_to_bsd_msg_flags(int flags)
  316 {
  317         int ret_flags = 0;
  318 
  319         if (flags & LINUX_MSG_OOB)
  320                 ret_flags |= MSG_OOB;
  321         if (flags & LINUX_MSG_PEEK)
  322                 ret_flags |= MSG_PEEK;
  323         if (flags & LINUX_MSG_DONTROUTE)
  324                 ret_flags |= MSG_DONTROUTE;
  325         if (flags & LINUX_MSG_CTRUNC)
  326                 ret_flags |= MSG_CTRUNC;
  327         if (flags & LINUX_MSG_TRUNC)
  328                 ret_flags |= MSG_TRUNC;
  329         if (flags & LINUX_MSG_DONTWAIT)
  330                 ret_flags |= MSG_DONTWAIT;
  331         if (flags & LINUX_MSG_EOR)
  332                 ret_flags |= MSG_EOR;
  333         if (flags & LINUX_MSG_WAITALL)
  334                 ret_flags |= MSG_WAITALL;
  335         if (flags & LINUX_MSG_NOSIGNAL)
  336                 ret_flags |= MSG_NOSIGNAL;
  337 #if 0 /* not handled */
  338         if (flags & LINUX_MSG_PROXY)
  339                 ;
  340         if (flags & LINUX_MSG_FIN)
  341                 ;
  342         if (flags & LINUX_MSG_SYN)
  343                 ;
  344         if (flags & LINUX_MSG_CONFIRM)
  345                 ;
  346         if (flags & LINUX_MSG_RST)
  347                 ;
  348         if (flags & LINUX_MSG_ERRQUEUE)
  349                 ;
  350 #endif
  351         return ret_flags;
  352 }
  353 
  354 /*
  355 * If bsd_to_linux_sockaddr() or linux_to_bsd_sockaddr() faults, then the
  356 * native syscall will fault.  Thus, we don't really need to check the
  357 * return values for these functions.
  358 */
  359 
  360 static int
  361 bsd_to_linux_sockaddr(struct sockaddr *arg)
  362 {
  363         struct sockaddr sa;
  364         size_t sa_len = sizeof(struct sockaddr);
  365         int error;
  366         
  367         if ((error = copyin(arg, &sa, sa_len)))
  368                 return (error);
  369         
  370         *(u_short *)&sa = sa.sa_family;
  371         
  372         error = copyout(&sa, arg, sa_len);
  373         
  374         return (error);
  375 }
  376 
  377 static int
  378 linux_to_bsd_sockaddr(struct sockaddr *arg, int len)
  379 {
  380         struct sockaddr sa;
  381         size_t sa_len = sizeof(struct sockaddr);
  382         int error;
  383 
  384         if ((error = copyin(arg, &sa, sa_len)))
  385                 return (error);
  386 
  387         sa.sa_family = *(sa_family_t *)&sa;
  388         sa.sa_len = len;
  389 
  390         error = copyout(&sa, arg, sa_len);
  391 
  392         return (error);
  393 }
  394 
  395 
  396 static int
  397 linux_sa_put(struct osockaddr *osa)
  398 {
  399         struct osockaddr sa;
  400         int error, bdom;
  401 
  402         /*
  403          * Only read/write the osockaddr family part, the rest is
  404          * not changed.
  405          */
  406         error = copyin(osa, &sa, sizeof(sa.sa_family));
  407         if (error)
  408                 return (error);
  409 
  410         bdom = bsd_to_linux_domain(sa.sa_family);
  411         if (bdom == -1)
  412                 return (EINVAL);
  413 
  414         sa.sa_family = bdom;
  415         error = copyout(&sa, osa, sizeof(sa.sa_family));
  416         if (error)
  417                 return (error);
  418 
  419         return (0);
  420 }
  421 
  422 static int
  423 linux_sendit(struct thread *td, int s, struct msghdr *mp, int flags,
  424     enum uio_seg segflg)
  425 {
  426         struct mbuf *control;
  427         struct sockaddr *to;
  428         int error;
  429 
  430         if (mp->msg_name != NULL) {
  431                 error = linux_getsockaddr(&to, mp->msg_name, mp->msg_namelen);
  432                 if (error)
  433                         return (error);
  434                 mp->msg_name = to;
  435         } else
  436                 to = NULL;
  437 
  438         if (mp->msg_control != NULL) {
  439                 struct cmsghdr *cmsg;
  440 
  441                 if (mp->msg_controllen < sizeof(struct cmsghdr)) {
  442                         error = EINVAL;
  443                         goto bad;
  444                 }
  445                 error = sockargs(&control, mp->msg_control,
  446                     mp->msg_controllen, MT_CONTROL);
  447                 if (error)
  448                         goto bad;
  449 
  450                 cmsg = mtod(control, struct cmsghdr *);
  451                 cmsg->cmsg_level = linux_to_bsd_sockopt_level(cmsg->cmsg_level);
  452         } else
  453                 control = NULL;
  454 
  455         error = kern_sendit(td, s, mp, linux_to_bsd_msg_flags(flags), control,
  456             segflg);
  457 
  458 bad:
  459         if (to)
  460                 FREE(to, M_SONAME);
  461         return (error);
  462 }
  463 
  464 /* Return 0 if IP_HDRINCL is set for the given socket. */
  465 static int
  466 linux_check_hdrincl(struct thread *td, int s)
  467 {
  468         int error, optval, size_val;
  469 
  470         size_val = sizeof(optval);
  471         error = kern_getsockopt(td, s, IPPROTO_IP, IP_HDRINCL,
  472             &optval, UIO_SYSSPACE, &size_val);
  473         if (error)
  474                 return (error);
  475 
  476         return (optval == 0);
  477 }
  478 
  479 struct linux_sendto_args {
  480         int s;
  481         l_uintptr_t msg;
  482         int len;
  483         int flags;
  484         l_uintptr_t to;
  485         int tolen;
  486 };
  487 
  488 /*
  489  * Updated sendto() when IP_HDRINCL is set:
  490  * tweak endian-dependent fields in the IP packet.
  491  */
  492 static int
  493 linux_sendto_hdrincl(struct thread *td, struct linux_sendto_args *linux_args)
  494 {
  495 /*
  496  * linux_ip_copysize defines how many bytes we should copy
  497  * from the beginning of the IP packet before we customize it for BSD.
  498  * It should include all the fields we modify (ip_len and ip_off).
  499  */
  500 #define linux_ip_copysize       8
  501 
  502         struct ip *packet;
  503         struct msghdr msg;
  504         struct iovec aiov[1];
  505         int error;
  506 
  507         /* Check that the packet isn't too big or too small. */
  508         if (linux_args->len < linux_ip_copysize ||
  509             linux_args->len > IP_MAXPACKET)
  510                 return (EINVAL);
  511 
  512         packet = (struct ip *)malloc(linux_args->len, M_TEMP, M_WAITOK);
  513 
  514         /* Make kernel copy of the packet to be sent */
  515         if ((error = copyin(PTRIN(linux_args->msg), packet,
  516             linux_args->len)))
  517                 goto goout;
  518 
  519         /* Convert fields from Linux to BSD raw IP socket format */
  520         packet->ip_len = linux_args->len;
  521         packet->ip_off = ntohs(packet->ip_off);
  522 
  523         /* Prepare the msghdr and iovec structures describing the new packet */
  524         msg.msg_name = PTRIN(linux_args->to);
  525         msg.msg_namelen = linux_args->tolen;
  526         msg.msg_iov = aiov;
  527         msg.msg_iovlen = 1;
  528         msg.msg_control = NULL;
  529         msg.msg_flags = 0;
  530         aiov[0].iov_base = (char *)packet;
  531         aiov[0].iov_len = linux_args->len;
  532         error = linux_sendit(td, linux_args->s, &msg, linux_args->flags,
  533             UIO_SYSSPACE);
  534 goout:
  535         free(packet, M_TEMP);
  536         return (error);
  537 }
  538 
  539 struct linux_socket_args {
  540         int domain;
  541         int type;
  542         int protocol;
  543 };
  544 
  545 static int
  546 linux_socket(struct thread *td, struct linux_socket_args *args)
  547 {
  548         struct linux_socket_args linux_args;
  549         struct socket_args /* {
  550                 int domain;
  551                 int type;
  552                 int protocol;
  553         } */ bsd_args;
  554         int error;
  555         int retval_socket;
  556 
  557         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  558                 return (error);
  559 
  560         bsd_args.protocol = linux_args.protocol;
  561         bsd_args.type = linux_args.type;
  562         bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
  563         if (bsd_args.domain == -1)
  564                 return (EINVAL);
  565 
  566         retval_socket = socket(td, &bsd_args);
  567         if (bsd_args.type == SOCK_RAW
  568             && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
  569             && bsd_args.domain == AF_INET
  570             && retval_socket >= 0) {
  571                 /* It's a raw IP socket: set the IP_HDRINCL option. */
  572                 int hdrincl;
  573 
  574                 hdrincl = 1;
  575                 /* We ignore any error returned by kern_setsockopt() */
  576                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IP, IP_HDRINCL,
  577                     &hdrincl, UIO_SYSSPACE, sizeof(hdrincl));
  578         }
  579 #ifdef INET6
  580         /*
  581          * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
  582          * default and some apps depend on this. So, set V6ONLY to 0
  583          * for Linux apps if the sysctl value is set to 1.
  584          */
  585         if (bsd_args.domain == PF_INET6 && retval_socket >= 0
  586 #ifndef KLD_MODULE
  587             /*
  588              * XXX: Avoid undefined symbol error with an IPv4 only
  589              * kernel.
  590              */
  591             && ip6_v6only
  592 #endif
  593             ) {
  594                 int v6only;
  595 
  596                 v6only = 0;
  597                 /* We ignore any error returned by setsockopt() */
  598                 kern_setsockopt(td, td->td_retval[0], IPPROTO_IPV6, IPV6_V6ONLY,
  599                     &v6only, UIO_SYSSPACE, sizeof(v6only));
  600         }
  601 #endif
  602 
  603         return (retval_socket);
  604 }
  605 
  606 struct linux_bind_args {
  607         int s;
  608         l_uintptr_t name;
  609         int namelen;
  610 };
  611 
  612 static int
  613 linux_bind(struct thread *td, struct linux_bind_args *args)
  614 {
  615         struct linux_bind_args linux_args;
  616         struct sockaddr *sa;
  617         int error;
  618 
  619         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  620                 return (error);
  621 
  622         error = linux_getsockaddr(&sa, PTRIN(linux_args.name),
  623             linux_args.namelen);
  624         if (error)
  625                 return (error);
  626 
  627         error = kern_bind(td, linux_args.s, sa);
  628         free(sa, M_SONAME);
  629         if (error == EADDRNOTAVAIL && linux_args.namelen != sizeof(struct sockaddr_in))
  630                 return (EINVAL);
  631         return (error);
  632 }
  633 
  634 struct linux_connect_args {
  635         int s;
  636         l_uintptr_t name;
  637         int namelen;
  638 };
  639 int linux_connect(struct thread *, struct linux_connect_args *);
  640 
  641 int
  642 linux_connect(struct thread *td, struct linux_connect_args *args)
  643 {
  644         struct linux_connect_args linux_args;
  645         struct socket *so;
  646         struct sockaddr *sa;
  647         u_int fflag;
  648         int error;
  649 
  650         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  651                 return (error);
  652 
  653         error = linux_getsockaddr(&sa,
  654             (struct osockaddr *)PTRIN(linux_args.name),
  655             linux_args.namelen);
  656         if (error)
  657                 return (error);
  658 
  659         error = kern_connect(td, linux_args.s, sa);
  660         free(sa, M_SONAME);
  661         if (error != EISCONN)
  662                 return (error);
  663 
  664         /*
  665          * Linux doesn't return EISCONN the first time it occurs,
  666          * when on a non-blocking socket. Instead it returns the
  667          * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
  668          *
  669          * XXXRW: Instead of using fgetsock(), check that it is a
  670          * socket and use the file descriptor reference instead of
  671          * creating a new one.
  672          */
  673         error = fgetsock(td, linux_args.s, &so, &fflag);
  674         if (error == 0) {
  675                 error = EISCONN;
  676                 if (fflag & FNONBLOCK) {
  677                         SOCK_LOCK(so);
  678                         if (so->so_emuldata == 0)
  679                                 error = so->so_error;
  680                         so->so_emuldata = (void *)1;
  681                         SOCK_UNLOCK(so);
  682                 }
  683                 fputsock(so);
  684         }
  685         return (error);
  686 }
  687 
  688 struct linux_listen_args {
  689         int s;
  690         int backlog;
  691 };
  692 
  693 static int
  694 linux_listen(struct thread *td, struct linux_listen_args *args)
  695 {
  696         struct linux_listen_args linux_args;
  697         struct listen_args /* {
  698                 int s;
  699                 int backlog;
  700         } */ bsd_args;
  701         int error;
  702 
  703         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  704                 return (error);
  705 
  706         bsd_args.s = linux_args.s;
  707         bsd_args.backlog = linux_args.backlog;
  708         return (listen(td, &bsd_args));
  709 }
  710 
  711 struct linux_accept_args {
  712         int s;
  713         l_uintptr_t addr;
  714         l_uintptr_t namelen;
  715 };
  716 
  717 static int
  718 linux_accept(struct thread *td, struct linux_accept_args *args)
  719 {
  720         struct linux_accept_args linux_args;
  721         struct accept_args /* {
  722                 int     s;
  723                 struct sockaddr * __restrict name;
  724                 socklen_t * __restrict anamelen;
  725         } */ bsd_args;
  726         int error, fd;
  727 
  728         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  729                 return (error);
  730 
  731         bsd_args.s = linux_args.s;
  732         /* XXX: */
  733         bsd_args.name = (struct sockaddr * __restrict)PTRIN(linux_args.addr);
  734         bsd_args.anamelen = PTRIN(linux_args.namelen);/* XXX */
  735         error = accept(td, &bsd_args);
  736         bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.name);
  737         if (error) {
  738                 if (error == EFAULT && linux_args.namelen != sizeof(struct sockaddr_in))
  739                         return (EINVAL);
  740                 return (error);
  741         }
  742         if (linux_args.addr) {
  743                 error = linux_sa_put(PTRIN(linux_args.addr));
  744                 if (error) {
  745                         (void)kern_close(td, td->td_retval[0]);
  746                         return (error);
  747                 }
  748         }
  749 
  750         /*
  751          * linux appears not to copy flags from the parent socket to the
  752          * accepted one, so we must clear the flags in the new descriptor.
  753          * Ignore any errors, because we already have an open fd.
  754          */
  755         fd = td->td_retval[0];
  756         (void)kern_fcntl(td, fd, F_SETFL, 0);
  757         td->td_retval[0] = fd;
  758         return (0);
  759 }
  760 
  761 struct linux_getsockname_args {
  762         int s;
  763         l_uintptr_t addr;
  764         l_uintptr_t namelen;
  765 };
  766 
  767 static int
  768 linux_getsockname(struct thread *td, struct linux_getsockname_args *args)
  769 {
  770         struct linux_getsockname_args linux_args;
  771         struct getsockname_args /* {
  772                 int     fdes;
  773                 struct sockaddr * __restrict asa;
  774                 socklen_t * __restrict alen;
  775         } */ bsd_args;
  776         int error;
  777 
  778         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  779                 return (error);
  780 
  781         bsd_args.fdes = linux_args.s;
  782         /* XXX: */
  783         bsd_args.asa = (struct sockaddr * __restrict)PTRIN(linux_args.addr);
  784         bsd_args.alen = PTRIN(linux_args.namelen);      /* XXX */
  785         error = getsockname(td, &bsd_args);
  786         bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
  787         if (error)
  788                 return (error);
  789         error = linux_sa_put(PTRIN(linux_args.addr));
  790         if (error)
  791                 return (error);
  792         return (0);
  793 }
  794 
  795 struct linux_getpeername_args {
  796         int s;
  797         l_uintptr_t addr;
  798         l_uintptr_t namelen;
  799 };
  800 
  801 static int
  802 linux_getpeername(struct thread *td, struct linux_getpeername_args *args)
  803 {
  804         struct linux_getpeername_args linux_args;
  805         struct getpeername_args /* {
  806                 int fdes;
  807                 caddr_t asa;
  808                 int *alen;
  809         } */ bsd_args;
  810         int error;
  811 
  812         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  813                 return (error);
  814 
  815         bsd_args.fdes = linux_args.s;
  816         bsd_args.asa = (struct sockaddr *)PTRIN(linux_args.addr);
  817         bsd_args.alen = (int *)PTRIN(linux_args.namelen);
  818         error = getpeername(td, &bsd_args);
  819         bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.asa);
  820         if (error)
  821                 return (error);
  822         error = linux_sa_put(PTRIN(linux_args.addr));
  823         if (error)
  824                 return (error);
  825         return (0);
  826 }
  827 
  828 struct linux_socketpair_args {
  829         int domain;
  830         int type;
  831         int protocol;
  832         l_uintptr_t rsv;
  833 };
  834 
  835 static int
  836 linux_socketpair(struct thread *td, struct linux_socketpair_args *args)
  837 {
  838         struct linux_socketpair_args linux_args;
  839         struct socketpair_args /* {
  840                 int domain;
  841                 int type;
  842                 int protocol;
  843                 int *rsv;
  844         } */ bsd_args;
  845         int error;
  846 
  847         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  848                 return (error);
  849 
  850         bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
  851         if (bsd_args.domain == -1)
  852                 return (EINVAL);
  853 
  854         bsd_args.type = linux_args.type;
  855         bsd_args.protocol = linux_args.protocol;
  856         bsd_args.rsv = (int *)PTRIN(linux_args.rsv);
  857         return (socketpair(td, &bsd_args));
  858 }
  859 
  860 struct linux_send_args {
  861         int s;
  862         l_uintptr_t msg;
  863         int len;
  864         int flags;
  865 };
  866 
  867 static int
  868 linux_send(struct thread *td, struct linux_send_args *args)
  869 {
  870         struct linux_send_args linux_args;
  871         struct sendto_args /* {
  872                 int s;
  873                 caddr_t buf;
  874                 int len;
  875                 int flags;
  876                 caddr_t to;
  877                 int tolen;
  878         } */ bsd_args;
  879         int error;
  880 
  881         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  882                 return (error);
  883 
  884         bsd_args.s = linux_args.s;
  885         bsd_args.buf = (caddr_t)PTRIN(linux_args.msg);
  886         bsd_args.len = linux_args.len;
  887         bsd_args.flags = linux_args.flags;
  888         bsd_args.to = NULL;
  889         bsd_args.tolen = 0;
  890         return sendto(td, &bsd_args);
  891 }
  892 
  893 struct linux_recv_args {
  894         int s;
  895         l_uintptr_t msg;
  896         int len;
  897         int flags;
  898 };
  899 
  900 static int
  901 linux_recv(struct thread *td, struct linux_recv_args *args)
  902 {
  903         struct linux_recv_args linux_args;
  904         struct recvfrom_args /* {
  905                 int s;
  906                 caddr_t buf;
  907                 int len;
  908                 int flags;
  909                 struct sockaddr *from;
  910                 socklen_t fromlenaddr;
  911         } */ bsd_args;
  912         int error;
  913 
  914         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  915                 return (error);
  916 
  917         bsd_args.s = linux_args.s;
  918         bsd_args.buf = (caddr_t)PTRIN(linux_args.msg);
  919         bsd_args.len = linux_args.len;
  920         bsd_args.flags = linux_args.flags;
  921         bsd_args.from = NULL;
  922         bsd_args.fromlenaddr = 0;
  923         return (recvfrom(td, &bsd_args));
  924 }
  925 
  926 static int
  927 linux_sendto(struct thread *td, struct linux_sendto_args *args)
  928 {
  929         struct linux_sendto_args linux_args;
  930         struct msghdr msg;
  931         struct iovec aiov;
  932         int error;
  933 
  934         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  935                 return (error);
  936 
  937         if (linux_check_hdrincl(td, linux_args.s) == 0)
  938                 /* IP_HDRINCL set, tweak the packet before sending */
  939                 return (linux_sendto_hdrincl(td, &linux_args));
  940 
  941         msg.msg_name = PTRIN(linux_args.to);
  942         msg.msg_namelen = linux_args.tolen;
  943         msg.msg_iov = &aiov;
  944         msg.msg_iovlen = 1;
  945         msg.msg_control = NULL;
  946         msg.msg_flags = 0;
  947         aiov.iov_base = PTRIN(linux_args.msg);
  948         aiov.iov_len = linux_args.len;
  949         error = linux_sendit(td, linux_args.s, &msg, linux_args.flags,
  950             UIO_USERSPACE);
  951         return (error);
  952 }
  953 
  954 struct linux_recvfrom_args {
  955         int s;
  956         l_uintptr_t buf;
  957         int len;
  958         int flags;
  959         l_uintptr_t from;
  960         l_uintptr_t fromlen;
  961 };
  962 
  963 static int
  964 linux_recvfrom(struct thread *td, struct linux_recvfrom_args *args)
  965 {
  966         struct linux_recvfrom_args linux_args;
  967         struct recvfrom_args /* {
  968                 int     s;
  969                 caddr_t buf;
  970                 size_t  len;
  971                 int     flags;
  972                 struct sockaddr * __restrict from;
  973                 socklen_t * __restrict fromlenaddr;
  974         } */ bsd_args;
  975         size_t len;
  976         int error;
  977 
  978         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  979                 return (error);
  980 
  981         if ((error = copyin(PTRIN(linux_args.fromlen), &len, sizeof(size_t))))
  982                 return (error);
  983 
  984         bsd_args.s = linux_args.s;
  985         bsd_args.buf = PTRIN(linux_args.buf);
  986         bsd_args.len = linux_args.len;
  987         bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
  988         /* XXX: */
  989         bsd_args.from = (struct sockaddr * __restrict)PTRIN(linux_args.from);
  990         bsd_args.fromlenaddr = PTRIN(linux_args.fromlen);/* XXX */
  991         
  992         linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.from, len);
  993         error = recvfrom(td, &bsd_args);
  994         bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.from);
  995         
  996         if (error)
  997                 return (error);
  998         if (linux_args.from) {
  999                 error = linux_sa_put((struct osockaddr *)
 1000                     PTRIN(linux_args.from));
 1001                 if (error)
 1002                         return (error);
 1003         }
 1004         return (0);
 1005 }
 1006 
 1007 struct linux_sendmsg_args {
 1008         int s;
 1009         l_uintptr_t msg;
 1010         int flags;
 1011 };
 1012 
 1013 static int
 1014 linux_sendmsg(struct thread *td, struct linux_sendmsg_args *args)
 1015 {
 1016         struct linux_sendmsg_args linux_args;
 1017         struct msghdr msg;
 1018         struct iovec *iov;
 1019         int error;
 1020 
 1021         /* XXXTJR sendmsg is broken on amd64 */
 1022 
 1023         error = copyin(args, &linux_args, sizeof(linux_args));
 1024         if (error)
 1025                 return (error);
 1026         error = copyin(PTRIN(linux_args.msg), &msg, sizeof(msg));
 1027         if (error)
 1028                 return (error);
 1029 
 1030         /*
 1031          * Some Linux applications (ping) define a non-NULL control data
 1032          * pointer, but a msg_controllen of 0, which is not allowed in the
 1033          * FreeBSD system call interface.  NULL the msg_control pointer in
 1034          * order to handle this case.  This should be checked, but allows the
 1035          * Linux ping to work.
 1036          */
 1037         if (msg.msg_control != NULL && msg.msg_controllen == 0)
 1038                 msg.msg_control = NULL;
 1039         error = copyiniov(msg.msg_iov, msg.msg_iovlen, &iov, EMSGSIZE);
 1040         if (error)
 1041                 return (error);
 1042         msg.msg_iov = iov;
 1043         msg.msg_flags = 0;
 1044         error = linux_sendit(td, linux_args.s, &msg, linux_args.flags,
 1045             UIO_USERSPACE);
 1046         free(iov, M_IOV);
 1047         return (error);
 1048 }
 1049 
 1050 struct linux_recvmsg_args {
 1051         int s;
 1052         l_uintptr_t msg;
 1053         int flags;
 1054 };
 1055 
 1056 static int
 1057 linux_recvmsg(struct thread *td, struct linux_recvmsg_args *args)
 1058 {
 1059         struct linux_recvmsg_args linux_args;
 1060         struct recvmsg_args /* {
 1061                 int     s;
 1062                 struct  msghdr *msg;
 1063                 int     flags;
 1064         } */ bsd_args;
 1065         struct msghdr msg;
 1066         struct cmsghdr *cmsg;
 1067         int error;
 1068 
 1069         /* XXXTJR recvmsg is broken on amd64 */
 1070 
 1071         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
 1072                 return (error);
 1073 
 1074         if ((error = copyin(PTRIN(args->msg), &msg, sizeof (msg))))
 1075                 return (error);
 1076 
 1077         bsd_args.s = linux_args.s;
 1078         bsd_args.msg = PTRIN(linux_args.msg);
 1079         bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
 1080         if (msg.msg_name) {
 1081                 linux_to_bsd_sockaddr((struct sockaddr *)msg.msg_name,
 1082                       msg.msg_namelen);
 1083                 error = recvmsg(td, &bsd_args);
 1084                 bsd_to_linux_sockaddr((struct sockaddr *)msg.msg_name);
 1085         } else
 1086                 error = recvmsg(td, &bsd_args);
 1087         if (error)
 1088                 return (error);
 1089 
 1090         if (bsd_args.msg->msg_control != NULL &&
 1091             bsd_args.msg->msg_controllen > 0) {
 1092                 cmsg = (struct cmsghdr*)bsd_args.msg->msg_control;
 1093                 cmsg->cmsg_level = bsd_to_linux_sockopt_level(cmsg->cmsg_level);
 1094         }
 1095 
 1096         error = copyin(PTRIN(linux_args.msg), &msg, sizeof(msg));
 1097         if (error)
 1098                 return (error);
 1099         if (msg.msg_name && msg.msg_namelen > 2)
 1100                 error = linux_sa_put(msg.msg_name);
 1101         return (error);
 1102 }
 1103 
 1104 struct linux_shutdown_args {
 1105         int s;
 1106         int how;
 1107 };
 1108 
 1109 static int
 1110 linux_shutdown(struct thread *td, struct linux_shutdown_args *args)
 1111 {
 1112         struct linux_shutdown_args linux_args;
 1113         struct shutdown_args /* {
 1114                 int s;
 1115                 int how;
 1116         } */ bsd_args;
 1117         int error;
 1118 
 1119         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
 1120                 return (error);
 1121 
 1122         bsd_args.s = linux_args.s;
 1123         bsd_args.how = linux_args.how;
 1124         return (shutdown(td, &bsd_args));
 1125 }
 1126 
 1127 struct linux_setsockopt_args {
 1128         int s;
 1129         int level;
 1130         int optname;
 1131         l_uintptr_t optval;
 1132         int optlen;
 1133 };
 1134 
 1135 static int
 1136 linux_setsockopt(struct thread *td, struct linux_setsockopt_args *args)
 1137 {
 1138         struct linux_setsockopt_args linux_args;
 1139         struct setsockopt_args /* {
 1140                 int s;
 1141                 int level;
 1142                 int name;
 1143                 caddr_t val;
 1144                 int valsize;
 1145         } */ bsd_args;
 1146         int error, name;
 1147 
 1148         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
 1149                 return (error);
 1150 
 1151         bsd_args.s = linux_args.s;
 1152         bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
 1153         switch (bsd_args.level) {
 1154         case SOL_SOCKET:
 1155                 name = linux_to_bsd_so_sockopt(linux_args.optname);
 1156                 break;
 1157         case IPPROTO_IP:
 1158                 name = linux_to_bsd_ip_sockopt(linux_args.optname);
 1159                 break;
 1160         case IPPROTO_TCP:
 1161                 /* Linux TCP option values match BSD's */
 1162                 name = linux_args.optname;
 1163                 break;
 1164         default:
 1165                 name = -1;
 1166                 break;
 1167         }
 1168         if (name == -1)
 1169                 return (ENOPROTOOPT);
 1170 
 1171         bsd_args.name = name;
 1172         bsd_args.val = PTRIN(linux_args.optval);
 1173         bsd_args.valsize = linux_args.optlen;
 1174 
 1175         if (name == IPV6_NEXTHOP) {
 1176                 linux_to_bsd_sockaddr((struct sockaddr *)bsd_args.val,
 1177                         bsd_args.valsize);
 1178                 error = setsockopt(td, &bsd_args);
 1179                 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
 1180         } else
 1181                 error = setsockopt(td, &bsd_args);
 1182 
 1183         return (error);
 1184 }
 1185 
 1186 struct linux_getsockopt_args {
 1187         int s;
 1188         int level;
 1189         int optname;
 1190         l_uintptr_t optval;
 1191         l_uintptr_t optlen;
 1192 };
 1193 
 1194 static int
 1195 linux_getsockopt(struct thread *td, struct linux_getsockopt_args *args)
 1196 {
 1197         struct linux_getsockopt_args linux_args;
 1198         struct getsockopt_args /* {
 1199                 int s;
 1200                 int level;
 1201                 int name;
 1202                 caddr_t val;
 1203                 int *avalsize;
 1204         } */ bsd_args;
 1205         int error, name;
 1206 
 1207         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
 1208                 return (error);
 1209 
 1210         bsd_args.s = linux_args.s;
 1211         bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
 1212         switch (bsd_args.level) {
 1213         case SOL_SOCKET:
 1214                 name = linux_to_bsd_so_sockopt(linux_args.optname);
 1215                 break;
 1216         case IPPROTO_IP:
 1217                 name = linux_to_bsd_ip_sockopt(linux_args.optname);
 1218                 break;
 1219         case IPPROTO_TCP:
 1220                 /* Linux TCP option values match BSD's */
 1221                 name = linux_args.optname;
 1222                 break;
 1223         default:
 1224                 name = -1;
 1225                 break;
 1226         }
 1227         if (name == -1)
 1228                 return (EINVAL);
 1229 
 1230         bsd_args.name = name;
 1231         bsd_args.val = PTRIN(linux_args.optval);
 1232         bsd_args.avalsize = PTRIN(linux_args.optlen);
 1233 
 1234         if (name == IPV6_NEXTHOP) {
 1235                 error = getsockopt(td, &bsd_args);
 1236                 bsd_to_linux_sockaddr((struct sockaddr *)bsd_args.val);
 1237         } else
 1238                 error = getsockopt(td, &bsd_args);
 1239 
 1240         return (error);
 1241 }
 1242 
 1243 int
 1244 linux_socketcall(struct thread *td, struct linux_socketcall_args *args)
 1245 {
 1246         void *arg = (void *)(intptr_t)args->args;
 1247 
 1248         switch (args->what) {
 1249         case LINUX_SOCKET:
 1250                 return (linux_socket(td, arg));
 1251         case LINUX_BIND:
 1252                 return (linux_bind(td, arg));
 1253         case LINUX_CONNECT:
 1254                 return (linux_connect(td, arg));
 1255         case LINUX_LISTEN:
 1256                 return (linux_listen(td, arg));
 1257         case LINUX_ACCEPT:
 1258                 return (linux_accept(td, arg));
 1259         case LINUX_GETSOCKNAME:
 1260                 return (linux_getsockname(td, arg));
 1261         case LINUX_GETPEERNAME:
 1262                 return (linux_getpeername(td, arg));
 1263         case LINUX_SOCKETPAIR:
 1264                 return (linux_socketpair(td, arg));
 1265         case LINUX_SEND:
 1266                 return (linux_send(td, arg));
 1267         case LINUX_RECV:
 1268                 return (linux_recv(td, arg));
 1269         case LINUX_SENDTO:
 1270                 return (linux_sendto(td, arg));
 1271         case LINUX_RECVFROM:
 1272                 return (linux_recvfrom(td, arg));
 1273         case LINUX_SHUTDOWN:
 1274                 return (linux_shutdown(td, arg));
 1275         case LINUX_SETSOCKOPT:
 1276                 return (linux_setsockopt(td, arg));
 1277         case LINUX_GETSOCKOPT:
 1278                 return (linux_getsockopt(td, arg));
 1279         case LINUX_SENDMSG:
 1280                 return (linux_sendmsg(td, arg));
 1281         case LINUX_RECVMSG:
 1282                 return (linux_recvmsg(td, arg));
 1283         }
 1284 
 1285         uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
 1286         return (ENOSYS);
 1287 }

Cache object: e48b7044c7ddc5877e118e3a69f8197d


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