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 withough 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  * $FreeBSD$
   29  */
   30 
   31 /* XXX we use functions that might not exist. */
   32 #include "opt_compat.h"
   33 
   34 #ifndef COMPAT_43
   35 #error "Unable to compile Linux-emulator due to missing COMPAT_43 option!"
   36 #endif
   37 
   38 #include <sys/param.h>
   39 #include <sys/proc.h>
   40 #include <sys/systm.h>
   41 #include <sys/sysproto.h>
   42 #include <sys/fcntl.h>
   43 #include <sys/file.h>
   44 #include <sys/socket.h>
   45 #include <sys/socketvar.h>
   46 #include <sys/uio.h>
   47 
   48 #include <netinet/in.h>
   49 #include <netinet/in_systm.h>
   50 #include <netinet/ip.h>
   51 
   52 #include <machine/../linux/linux.h>
   53 #include <machine/../linux/linux_proto.h>
   54 #include <compat/linux/linux_socket.h>
   55 #include <compat/linux/linux_util.h>
   56 
   57 /*
   58  * FreeBSD's socket calls require the sockaddr struct length to agree
   59  * with the address family.  Linux does not, so we must force it.
   60  */
   61 static int
   62 linux_to_bsd_namelen(caddr_t name, int namelen)
   63 {
   64         uint16_t        family; /* XXX must match Linux sockaddr */
   65 
   66         if (copyin(name, &family, sizeof(family)))
   67                 return namelen;
   68 
   69         switch (family) {
   70                 case AF_INET:
   71                         return sizeof(struct sockaddr_in);
   72                 case AF_INET6:
   73                         return sizeof(struct sockaddr_in6);
   74         }
   75         return namelen;
   76 }
   77 
   78 #ifndef __alpha__
   79 static int
   80 linux_to_bsd_domain(int domain)
   81 {
   82 
   83         switch (domain) {
   84         case LINUX_AF_UNSPEC:
   85                 return (AF_UNSPEC);
   86         case LINUX_AF_UNIX:
   87                 return (AF_LOCAL);
   88         case LINUX_AF_INET:
   89                 return (AF_INET);
   90         case LINUX_AF_AX25:
   91                 return (AF_CCITT);
   92         case LINUX_AF_IPX:
   93                 return (AF_IPX);
   94         case LINUX_AF_APPLETALK:
   95                 return (AF_APPLETALK);
   96         }
   97         return (-1);
   98 }
   99 
  100 static int
  101 linux_to_bsd_sockopt_level(int level)
  102 {
  103 
  104         switch (level) {
  105         case LINUX_SOL_SOCKET:
  106                 return (SOL_SOCKET);
  107         }
  108         return (level);
  109 }
  110 
  111 static int
  112 linux_to_bsd_ip_sockopt(int opt)
  113 {
  114 
  115         switch (opt) {
  116         case LINUX_IP_TOS:
  117                 return (IP_TOS);
  118         case LINUX_IP_TTL:
  119                 return (IP_TTL);
  120         case LINUX_IP_OPTIONS:
  121                 return (IP_OPTIONS);
  122         case LINUX_IP_MULTICAST_IF:
  123                 return (IP_MULTICAST_IF);
  124         case LINUX_IP_MULTICAST_TTL:
  125                 return (IP_MULTICAST_TTL);
  126         case LINUX_IP_MULTICAST_LOOP:
  127                 return (IP_MULTICAST_LOOP);
  128         case LINUX_IP_ADD_MEMBERSHIP:
  129                 return (IP_ADD_MEMBERSHIP);
  130         case LINUX_IP_DROP_MEMBERSHIP:
  131                 return (IP_DROP_MEMBERSHIP);
  132         case LINUX_IP_HDRINCL:
  133                 return (IP_HDRINCL);
  134         }
  135         return (-1);
  136 }
  137 
  138 static int
  139 linux_to_bsd_so_sockopt(int opt)
  140 {
  141 
  142         switch (opt) {
  143         case LINUX_SO_DEBUG:
  144                 return (SO_DEBUG);
  145         case LINUX_SO_REUSEADDR:
  146                 return (SO_REUSEADDR);
  147         case LINUX_SO_TYPE:
  148                 return (SO_TYPE);
  149         case LINUX_SO_ERROR:
  150                 return (SO_ERROR);
  151         case LINUX_SO_DONTROUTE:
  152                 return (SO_DONTROUTE);
  153         case LINUX_SO_BROADCAST:
  154                 return (SO_BROADCAST);
  155         case LINUX_SO_SNDBUF:
  156                 return (SO_SNDBUF);
  157         case LINUX_SO_RCVBUF:
  158                 return (SO_RCVBUF);
  159         case LINUX_SO_KEEPALIVE:
  160                 return (SO_KEEPALIVE);
  161         case LINUX_SO_OOBINLINE:
  162                 return (SO_OOBINLINE);
  163         case LINUX_SO_LINGER:
  164                 return (SO_LINGER);
  165         }
  166         return (-1);
  167 }
  168 
  169 static int
  170 linux_to_bsd_msg_flags(int flags)
  171 {
  172         int ret_flags = 0;
  173 
  174         if (flags & LINUX_MSG_OOB)
  175                 ret_flags |= MSG_OOB;
  176         if (flags & LINUX_MSG_PEEK)
  177                 ret_flags |= MSG_PEEK;
  178         if (flags & LINUX_MSG_DONTROUTE)
  179                 ret_flags |= MSG_DONTROUTE;
  180         if (flags & LINUX_MSG_CTRUNC)
  181                 ret_flags |= MSG_CTRUNC;
  182         if (flags & LINUX_MSG_TRUNC)
  183                 ret_flags |= MSG_TRUNC;
  184         if (flags & LINUX_MSG_DONTWAIT)
  185                 ret_flags |= MSG_DONTWAIT;
  186         if (flags & LINUX_MSG_EOR)
  187                 ret_flags |= MSG_EOR;
  188         if (flags & LINUX_MSG_WAITALL)
  189                 ret_flags |= MSG_WAITALL;
  190 #if 0 /* not handled */
  191         if (flags & LINUX_MSG_PROXY)
  192                 ;
  193         if (flags & LINUX_MSG_FIN)
  194                 ;
  195         if (flags & LINUX_MSG_SYN)
  196                 ;
  197         if (flags & LINUX_MSG_CONFIRM)
  198                 ;
  199         if (flags & LINUX_MSG_RST)
  200                 ;
  201         if (flags & LINUX_MSG_ERRQUEUE)
  202                 ;
  203         if (flags & LINUX_MSG_NOSIGNAL)
  204                 ;
  205 #endif
  206         return ret_flags;
  207 }
  208 
  209 /* Return 0 if IP_HDRINCL is set for the given socket. */
  210 static int
  211 linux_check_hdrincl(struct proc *p, int s)
  212 {
  213         struct getsockopt_args /* {
  214                 int s;
  215                 int level;
  216                 int name;
  217                 caddr_t val;
  218                 int *avalsize;
  219         } */ bsd_args;
  220         int error;
  221         caddr_t sg, val, valsize;
  222         int size_val = sizeof val;
  223         int optval;
  224 
  225         sg = stackgap_init();
  226         val = stackgap_alloc(&sg, sizeof(int));
  227         valsize = stackgap_alloc(&sg, sizeof(int));
  228 
  229         if ((error = copyout(&size_val, valsize, sizeof(size_val))))
  230                 return (error);
  231 
  232         bsd_args.s = s;
  233         bsd_args.level = IPPROTO_IP;
  234         bsd_args.name = IP_HDRINCL;
  235         bsd_args.val = val;
  236         bsd_args.avalsize = (int *)valsize;
  237         if ((error = getsockopt(p, &bsd_args)))
  238                 return (error);
  239 
  240         if ((error = copyin(val, &optval, sizeof(optval))))
  241                 return (error);
  242 
  243         return (optval == 0);
  244 }
  245 
  246 /*
  247  * Updated sendto() when IP_HDRINCL is set:
  248  * tweak endian-dependent fields in the IP packet.
  249  */
  250 static int
  251 linux_sendto_hdrincl(struct proc *p, struct sendto_args *bsd_args)
  252 {
  253 /*
  254  * linux_ip_copysize defines how many bytes we should copy
  255  * from the beginning of the IP packet before we customize it for BSD.
  256  * It should include all the fields we modify (ip_len and ip_off)
  257  * and be as small as possible to minimize copying overhead.
  258  */
  259 #define linux_ip_copysize       8
  260 
  261         caddr_t sg;
  262         struct ip *packet;
  263         struct msghdr *msg;
  264         struct iovec *iov;
  265 
  266         int error;
  267         struct  sendmsg_args /* {
  268                 int s;
  269                 caddr_t msg;
  270                 int flags;
  271         } */ sendmsg_args;
  272 
  273         /* Check the packet isn't too small before we mess with it */
  274         if (bsd_args->len < linux_ip_copysize)
  275                 return (EINVAL);
  276 
  277         /*
  278          * Tweaking the user buffer in place would be bad manners.
  279          * We create a corrected IP header with just the needed length,
  280          * then use an iovec to glue it to the rest of the user packet
  281          * when calling sendmsg().
  282          */
  283         sg = stackgap_init();
  284         packet = (struct ip *)stackgap_alloc(&sg, linux_ip_copysize);
  285         msg = (struct msghdr *)stackgap_alloc(&sg, sizeof(*msg));
  286         iov = (struct iovec *)stackgap_alloc(&sg, sizeof(*iov)*2);
  287 
  288         /* Make a copy of the beginning of the packet to be sent */
  289         if ((error = copyin(bsd_args->buf, packet, linux_ip_copysize)))
  290                 return (error);
  291 
  292         /* Convert fields from Linux to BSD raw IP socket format */
  293         packet->ip_len = bsd_args->len;
  294         packet->ip_off = ntohs(packet->ip_off);
  295 
  296         /* Prepare the msghdr and iovec structures describing the new packet */
  297         msg->msg_name = bsd_args->to;
  298         msg->msg_namelen = bsd_args->tolen;
  299         msg->msg_iov = iov;
  300         msg->msg_iovlen = 2;
  301         msg->msg_control = NULL;
  302         msg->msg_controllen = 0;
  303         msg->msg_flags = 0;
  304         iov[0].iov_base = (char *)packet;
  305         iov[0].iov_len = linux_ip_copysize;
  306         iov[1].iov_base = (char *)(bsd_args->buf) + linux_ip_copysize;
  307         iov[1].iov_len = bsd_args->len - linux_ip_copysize;
  308 
  309         sendmsg_args.s = bsd_args->s;
  310         sendmsg_args.msg = (caddr_t)msg;
  311         sendmsg_args.flags = bsd_args->flags;
  312         return (sendmsg(p, &sendmsg_args));
  313 }
  314 
  315 struct linux_socket_args {
  316         int domain;
  317         int type;
  318         int protocol;
  319 };
  320 
  321 static int
  322 linux_socket(struct proc *p, struct linux_socket_args *args)
  323 {
  324         struct linux_socket_args linux_args;
  325         struct socket_args /* {
  326                 int domain;
  327                 int type;
  328                 int protocol;
  329         } */ bsd_args;
  330         int error;
  331         int retval_socket;
  332 
  333         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  334                 return (error);
  335 
  336         bsd_args.protocol = linux_args.protocol;
  337         bsd_args.type = linux_args.type;
  338         bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
  339         if (bsd_args.domain == -1)
  340                 return (EINVAL);
  341 
  342         retval_socket = socket(p, &bsd_args);
  343         if (bsd_args.type == SOCK_RAW
  344             && (bsd_args.protocol == IPPROTO_RAW || bsd_args.protocol == 0)
  345             && bsd_args.domain == AF_INET
  346             && retval_socket >= 0) {
  347                 /* It's a raw IP socket: set the IP_HDRINCL option. */
  348                 struct setsockopt_args /* {
  349                         int s;
  350                         int level;
  351                         int name;
  352                         caddr_t val;
  353                         int valsize;
  354                 } */ bsd_setsockopt_args;
  355                 caddr_t sg;
  356                 int *hdrincl;
  357 
  358                 sg = stackgap_init();
  359                 hdrincl = (int *)stackgap_alloc(&sg, sizeof(*hdrincl));
  360                 *hdrincl = 1;
  361                 bsd_setsockopt_args.s = p->p_retval[0];
  362                 bsd_setsockopt_args.level = IPPROTO_IP;
  363                 bsd_setsockopt_args.name = IP_HDRINCL;
  364                 bsd_setsockopt_args.val = (caddr_t)hdrincl;
  365                 bsd_setsockopt_args.valsize = sizeof(*hdrincl);
  366                 /* We ignore any error returned by setsockopt() */
  367                 setsockopt(p, &bsd_setsockopt_args);
  368                 /* Copy back the return value from socket() */
  369                 p->p_retval[0] = bsd_setsockopt_args.s;
  370         }
  371 
  372         return (retval_socket);
  373 }
  374 
  375 struct linux_bind_args {
  376         int s;
  377         struct sockaddr *name;
  378         int namelen;
  379 };
  380 
  381 static int
  382 linux_bind(struct proc *p, struct linux_bind_args *args)
  383 {
  384         struct linux_bind_args linux_args;
  385         struct bind_args /* {
  386                 int s;
  387                 caddr_t name;
  388                 int namelen;
  389         } */ bsd_args;
  390         int error;
  391 
  392         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  393                 return (error);
  394 
  395         bsd_args.s = linux_args.s;
  396         bsd_args.name = (caddr_t)linux_args.name;
  397         bsd_args.namelen = linux_to_bsd_namelen(bsd_args.name,
  398             linux_args.namelen);
  399         return (bind(p, &bsd_args));
  400 }
  401 
  402 struct linux_connect_args {
  403         int s;
  404         struct sockaddr * name;
  405         int namelen;
  406 };
  407 int linux_connect(struct proc *, struct linux_connect_args *);
  408 #endif /* !__alpha__*/
  409 
  410 int
  411 linux_connect(struct proc *p, struct linux_connect_args *args)
  412 {
  413         struct linux_connect_args linux_args;
  414         struct connect_args /* {
  415                 int s;
  416                 caddr_t name;
  417                 int namelen;
  418         } */ bsd_args;
  419         struct socket *so;
  420         struct file *fp;
  421         int error;
  422 
  423 #ifdef __alpha__
  424         bcopy(args, &linux_args, sizeof(linux_args));
  425 #else
  426         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  427                 return (error);
  428 #endif /* __alpha__ */
  429 
  430         bsd_args.s = linux_args.s;
  431         bsd_args.name = (caddr_t)linux_args.name;
  432         bsd_args.namelen = linux_to_bsd_namelen(bsd_args.name,
  433             linux_args.namelen);
  434         error = connect(p, &bsd_args);
  435         if (error != EISCONN)
  436                 return (error);
  437 
  438         /*
  439          * Linux doesn't return EISCONN the first time it occurs,
  440          * when on a non-blocking socket. Instead it returns the
  441          * error getsockopt(SOL_SOCKET, SO_ERROR) would return on BSD.
  442          */
  443         error = holdsock(p->p_fd, linux_args.s, &fp);
  444         if (error)
  445                 return (error);
  446         error = EISCONN;
  447         if (fp->f_flag & FNONBLOCK) {
  448                 so = (struct socket *)fp->f_data;
  449                 if (so->so_emuldata == 0)
  450                         error = so->so_error;
  451                 so->so_emuldata = (void *)1;
  452         }
  453         fdrop(fp, p);
  454         return (error);
  455 }
  456 
  457 #ifndef __alpha__
  458 
  459 struct linux_listen_args {
  460         int s;
  461         int backlog;
  462 };
  463 
  464 static int
  465 linux_listen(struct proc *p, struct linux_listen_args *args)
  466 {
  467         struct linux_listen_args linux_args;
  468         struct listen_args /* {
  469                 int s;
  470                 int backlog;
  471         } */ bsd_args;
  472         int error;
  473 
  474         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  475                 return (error);
  476 
  477         bsd_args.s = linux_args.s;
  478         bsd_args.backlog = linux_args.backlog;
  479         return (listen(p, &bsd_args));
  480 }
  481 
  482 struct linux_accept_args {
  483         int s;
  484         struct sockaddr *addr;
  485         int *namelen;
  486 };
  487 
  488 static int
  489 linux_accept(struct proc *p, struct linux_accept_args *args)
  490 {
  491         struct linux_accept_args linux_args;
  492         struct accept_args /* {
  493                 int s;
  494                 caddr_t name;
  495                 int *anamelen;
  496         } */ bsd_args;
  497         struct fcntl_args /* {
  498                 int fd;
  499                 int cmd;
  500                 long arg;
  501         } */ f_args;
  502         int error;
  503 
  504         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  505                 return (error);
  506 
  507         bsd_args.s = linux_args.s;
  508         bsd_args.name = (caddr_t)linux_args.addr;
  509         bsd_args.anamelen = linux_args.namelen;
  510         error = oaccept(p, &bsd_args);
  511         if (error)
  512                 return (error);
  513 
  514         /*
  515          * linux appears not to copy flags from the parent socket to the
  516          * accepted one, so we must clear the flags in the new descriptor.
  517          * Ignore any errors, because we already have an open fd.
  518          */
  519         f_args.fd = p->p_retval[0];
  520         f_args.cmd = F_SETFL;
  521         f_args.arg = 0;
  522         (void)fcntl(p, &f_args);
  523         p->p_retval[0] = f_args.fd;
  524         return (0);
  525 }
  526 
  527 struct linux_getsockname_args {
  528         int s;
  529         struct sockaddr *addr;
  530         int *namelen;
  531 };
  532 
  533 static int
  534 linux_getsockname(struct proc *p, struct linux_getsockname_args *args)
  535 {
  536         struct linux_getsockname_args linux_args;
  537         struct getsockname_args /* {
  538                 int fdes;
  539                 caddr_t asa;
  540                 int *alen;
  541         } */ bsd_args;
  542         int error;
  543 
  544         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  545                 return (error);
  546 
  547         bsd_args.fdes = linux_args.s;
  548         bsd_args.asa = (caddr_t) linux_args.addr;
  549         bsd_args.alen = linux_args.namelen;
  550         return (ogetsockname(p, &bsd_args));
  551 }
  552 
  553 struct linux_getpeername_args {
  554         int s;
  555         struct sockaddr *addr;
  556         int *namelen;
  557 };
  558 
  559 static int
  560 linux_getpeername(struct proc *p, struct linux_getpeername_args *args)
  561 {
  562         struct linux_getpeername_args linux_args;
  563         struct ogetpeername_args /* {
  564                 int fdes;
  565                 caddr_t asa;
  566                 int *alen;
  567         } */ bsd_args;
  568         int error;
  569 
  570         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  571                 return (error);
  572 
  573         bsd_args.fdes = linux_args.s;
  574         bsd_args.asa = (caddr_t) linux_args.addr;
  575         bsd_args.alen = linux_args.namelen;
  576         return (ogetpeername(p, &bsd_args));
  577 }
  578 
  579 struct linux_socketpair_args {
  580         int domain;
  581         int type;
  582         int protocol;
  583         int *rsv;
  584 };
  585 
  586 static int
  587 linux_socketpair(struct proc *p, struct linux_socketpair_args *args)
  588 {
  589         struct linux_socketpair_args linux_args;
  590         struct socketpair_args /* {
  591                 int domain;
  592                 int type;
  593                 int protocol;
  594                 int *rsv;
  595         } */ bsd_args;
  596         int error;
  597 
  598         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  599                 return (error);
  600 
  601         bsd_args.domain = linux_to_bsd_domain(linux_args.domain);
  602         if (bsd_args.domain == -1)
  603                 return (EINVAL);
  604 
  605         bsd_args.type = linux_args.type;
  606         bsd_args.protocol = linux_args.protocol;
  607         bsd_args.rsv = linux_args.rsv;
  608         return (socketpair(p, &bsd_args));
  609 }
  610 
  611 struct linux_send_args {
  612         int s;
  613         void *msg;
  614         int len;
  615         int flags;
  616 };
  617 
  618 static int
  619 linux_send(struct proc *p, struct linux_send_args *args)
  620 {
  621         struct linux_send_args linux_args;
  622         struct osend_args /* {
  623                 int s;
  624                 caddr_t buf;
  625                 int len;
  626                 int flags;
  627         } */ bsd_args;
  628         int error;
  629 
  630         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  631                 return (error);
  632 
  633         bsd_args.s = linux_args.s;
  634         bsd_args.buf = linux_args.msg;
  635         bsd_args.len = linux_args.len;
  636         bsd_args.flags = linux_args.flags;
  637         return (osend(p, &bsd_args));
  638 }
  639 
  640 struct linux_recv_args {
  641         int s;
  642         void *msg;
  643         int len;
  644         int flags;
  645 };
  646 
  647 static int
  648 linux_recv(struct proc *p, struct linux_recv_args *args)
  649 {
  650         struct linux_recv_args linux_args;
  651         struct orecv_args /* {
  652                 int s;
  653                 caddr_t buf;
  654                 int len;
  655                 int flags;
  656         } */ bsd_args;
  657         int error;
  658 
  659         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  660                 return (error);
  661 
  662         bsd_args.s = linux_args.s;
  663         bsd_args.buf = linux_args.msg;
  664         bsd_args.len = linux_args.len;
  665         bsd_args.flags = linux_args.flags;
  666         return (orecv(p, &bsd_args));
  667 }
  668 
  669 struct linux_sendto_args {
  670         int s;
  671         void *msg;
  672         int len;
  673         int flags;
  674         caddr_t to;
  675         int tolen;
  676 };
  677 
  678 static int
  679 linux_sendto(struct proc *p, struct linux_sendto_args *args)
  680 {
  681         struct linux_sendto_args linux_args;
  682         struct sendto_args /* {
  683                 int s;
  684                 caddr_t buf;
  685                 size_t len;
  686                 int flags;
  687                 caddr_t to;
  688                 int tolen;
  689         } */ bsd_args;
  690         int error;
  691 
  692         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  693                 return (error);
  694 
  695         bsd_args.s = linux_args.s;
  696         bsd_args.buf = linux_args.msg;
  697         bsd_args.len = linux_args.len;
  698         bsd_args.flags = linux_args.flags;
  699         bsd_args.to = linux_args.to;
  700         bsd_args.tolen = linux_args.tolen;
  701 
  702         if (linux_check_hdrincl(p, linux_args.s) == 0)
  703                 /* IP_HDRINCL set, tweak the packet before sending */
  704                 return (linux_sendto_hdrincl(p, &bsd_args));
  705 
  706         return (sendto(p, &bsd_args));
  707 }
  708 
  709 struct linux_recvfrom_args {
  710         int s;
  711         void *buf;
  712         int len;
  713         int flags;
  714         caddr_t from;
  715         int *fromlen;
  716 };
  717 
  718 static int
  719 linux_recvfrom(struct proc *p, struct linux_recvfrom_args *args)
  720 {
  721         struct linux_recvfrom_args linux_args;
  722         struct recvfrom_args /* {
  723                 int s;
  724                 caddr_t buf;
  725                 size_t len;
  726                 int flags;
  727                 caddr_t from;
  728                 int *fromlenaddr;
  729         } */ bsd_args;
  730         int error;
  731 
  732         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  733                 return (error);
  734 
  735         bsd_args.s = linux_args.s;
  736         bsd_args.buf = linux_args.buf;
  737         bsd_args.len = linux_args.len;
  738         bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
  739         bsd_args.from = linux_args.from;
  740         bsd_args.fromlenaddr = linux_args.fromlen;
  741         return (orecvfrom(p, &bsd_args));
  742 }
  743 
  744 struct linux_recvmsg_args {
  745         int s;
  746         struct msghdr *msg;
  747         int flags;
  748 };
  749 
  750 static int
  751 linux_recvmsg(struct proc *p, struct linux_recvmsg_args *args)
  752 {
  753         struct linux_recvmsg_args linux_args;
  754         struct recvmsg_args /* {
  755                 int     s;
  756                 struct  msghdr *msg;
  757                 int     flags;
  758         } */ bsd_args;
  759         int error;
  760 
  761         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  762                 return (error);
  763 
  764         bsd_args.s = linux_args.s;
  765         bsd_args.msg = linux_args.msg;
  766         bsd_args.flags = linux_to_bsd_msg_flags(linux_args.flags);
  767         return (recvmsg(p, &bsd_args));
  768 }
  769 
  770 struct linux_shutdown_args {
  771         int s;
  772         int how;
  773 };
  774 
  775 static int
  776 linux_shutdown(struct proc *p, struct linux_shutdown_args *args)
  777 {
  778         struct linux_shutdown_args linux_args;
  779         struct shutdown_args /* {
  780                 int s;
  781                 int how;
  782         } */ bsd_args;
  783         int error;
  784 
  785         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  786                 return (error);
  787 
  788         bsd_args.s = linux_args.s;
  789         bsd_args.how = linux_args.how;
  790         return (shutdown(p, &bsd_args));
  791 }
  792 
  793 struct linux_setsockopt_args {
  794         int s;
  795         int level;
  796         int optname;
  797         void *optval;
  798         int optlen;
  799 };
  800 
  801 static int
  802 linux_setsockopt(struct proc *p, struct linux_setsockopt_args *args)
  803 {
  804         struct linux_setsockopt_args linux_args;
  805         struct setsockopt_args /* {
  806                 int s;
  807                 int level;
  808                 int name;
  809                 caddr_t val;
  810                 int valsize;
  811         } */ bsd_args;
  812         int error, name;
  813 
  814         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  815                 return (error);
  816 
  817         bsd_args.s = linux_args.s;
  818         bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
  819         switch (bsd_args.level) {
  820         case SOL_SOCKET:
  821                 name = linux_to_bsd_so_sockopt(linux_args.optname);
  822                 break;
  823         case IPPROTO_IP:
  824                 name = linux_to_bsd_ip_sockopt(linux_args.optname);
  825                 break;
  826         case IPPROTO_TCP:
  827                 /* Linux TCP option values match BSD's */
  828                 name = linux_args.optname;
  829                 break;
  830         default:
  831                 name = -1;
  832                 break;
  833         }
  834         if (name == -1)
  835                 return (EINVAL);
  836 
  837         bsd_args.name = name;
  838         bsd_args.val = linux_args.optval;
  839         bsd_args.valsize = linux_args.optlen;
  840         return (setsockopt(p, &bsd_args));
  841 }
  842 
  843 struct linux_getsockopt_args {
  844         int s;
  845         int level;
  846         int optname;
  847         void *optval;
  848         int *optlen;
  849 };
  850 
  851 static int
  852 linux_getsockopt(struct proc *p, struct linux_getsockopt_args *args)
  853 {
  854         struct linux_getsockopt_args linux_args;
  855         struct getsockopt_args /* {
  856                 int s;
  857                 int level;
  858                 int name;
  859                 caddr_t val;
  860                 int *avalsize;
  861         } */ bsd_args;
  862         int error, name;
  863 
  864         if ((error = copyin(args, &linux_args, sizeof(linux_args))))
  865                 return (error);
  866 
  867         bsd_args.s = linux_args.s;
  868         bsd_args.level = linux_to_bsd_sockopt_level(linux_args.level);
  869         switch (bsd_args.level) {
  870         case SOL_SOCKET:
  871                 name = linux_to_bsd_so_sockopt(linux_args.optname);
  872                 break;
  873         case IPPROTO_IP:
  874                 name = linux_to_bsd_ip_sockopt(linux_args.optname);
  875                 break;
  876         case IPPROTO_TCP:
  877                 /* Linux TCP option values match BSD's */
  878                 name = linux_args.optname;
  879                 break;
  880         default:
  881                 name = -1;
  882                 break;
  883         }
  884         if (name == -1)
  885                 return (EINVAL);
  886 
  887         bsd_args.name = name;
  888         bsd_args.val = linux_args.optval;
  889         bsd_args.avalsize = linux_args.optlen;
  890         return (getsockopt(p, &bsd_args));
  891 }
  892 
  893 int
  894 linux_socketcall(struct proc *p, struct linux_socketcall_args *args)
  895 {
  896         void *arg = (void *)args->args;
  897 
  898         switch (args->what) {
  899         case LINUX_SOCKET:
  900                 return (linux_socket(p, arg));
  901         case LINUX_BIND:
  902                 return (linux_bind(p, arg));
  903         case LINUX_CONNECT:
  904                 return (linux_connect(p, arg));
  905         case LINUX_LISTEN:
  906                 return (linux_listen(p, arg));
  907         case LINUX_ACCEPT:
  908                 return (linux_accept(p, arg));
  909         case LINUX_GETSOCKNAME:
  910                 return (linux_getsockname(p, arg));
  911         case LINUX_GETPEERNAME:
  912                 return (linux_getpeername(p, arg));
  913         case LINUX_SOCKETPAIR:
  914                 return (linux_socketpair(p, arg));
  915         case LINUX_SEND:
  916                 return (linux_send(p, arg));
  917         case LINUX_RECV:
  918                 return (linux_recv(p, arg));
  919         case LINUX_SENDTO:
  920                 return (linux_sendto(p, arg));
  921         case LINUX_RECVFROM:
  922                 return (linux_recvfrom(p, arg));
  923         case LINUX_SHUTDOWN:
  924                 return (linux_shutdown(p, arg));
  925         case LINUX_SETSOCKOPT:
  926                 return (linux_setsockopt(p, arg));
  927         case LINUX_GETSOCKOPT:
  928                 return (linux_getsockopt(p, arg));
  929         case LINUX_SENDMSG:
  930                 do {
  931                         int error;
  932                         int level;
  933                         caddr_t control;
  934                         struct {
  935                                 int s;
  936                                 const struct msghdr *msg;
  937                                 int flags;
  938                         } *uap = arg;
  939 
  940                         error = copyin(&uap->msg->msg_control, &control,
  941                             sizeof(caddr_t));
  942                         if (error)
  943                                 return (error);
  944 
  945                         if (control == NULL)
  946                                 goto done;
  947 
  948                         error = copyin(&((struct cmsghdr*)control)->cmsg_level,
  949                             &level, sizeof(int));
  950                         if (error)
  951                                 return (error);
  952 
  953                         if (level == 1) {
  954                                 /*
  955                                  * Linux thinks that SOL_SOCKET is 1; we know
  956                                  * that it's really 0xffff, of course.
  957                                  */
  958                                 level = SOL_SOCKET;
  959                                 error = copyout(&level,
  960                                     &((struct cmsghdr *)control)->cmsg_level,
  961                                     sizeof(int));
  962                                 if (error)
  963                                         return (error);
  964                         }
  965                 done:
  966                         return (sendmsg(p, arg));
  967                 } while (0);
  968         case LINUX_RECVMSG:
  969                 return (linux_recvmsg(p, arg));
  970         }
  971 
  972         uprintf("LINUX: 'socket' typ=%d not implemented\n", args->what);
  973         return (ENOSYS);
  974 }
  975 #endif  /*!__alpha__*/

Cache object: a00e7e4ef9b4321f540208adcfb28cd6


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