Annotation of src/sys/compat/linux/common/linux_socket.c, revision 1.124

1.124   ! rtr         1: /*     $NetBSD: linux_socket.c,v 1.123 2015/04/03 20:01:07 rtr Exp $   */
1.16      erh         2: 
                      3: /*-
1.87      ad          4:  * Copyright (c) 1995, 1998, 2008 The NetBSD Foundation, Inc.
1.16      erh         5:  * All rights reserved.
                      6:  *
                      7:  * This code is derived from software contributed to The NetBSD Foundation
1.18      fvdl        8:  * by Frank van der Linden and Eric Haszlakiewicz.
1.16      erh         9:  *
                     10:  * Redistribution and use in source and binary forms, with or without
                     11:  * modification, are permitted provided that the following conditions
                     12:  * are met:
                     13:  * 1. Redistributions of source code must retain the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer.
                     15:  * 2. Redistributions in binary form must reproduce the above copyright
                     16:  *    notice, this list of conditions and the following disclaimer in the
                     17:  *    documentation and/or other materials provided with the distribution.
                     18:  *
                     19:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     20:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     21:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     22:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     23:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     24:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     25:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     26:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     27:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     28:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     29:  * POSSIBILITY OF SUCH DAMAGE.
1.1       fvdl       30:  */
                     31: 
1.16      erh        32: /*
                     33:  * Functions in multiarch:
                     34:  *     linux_sys_socketcall            : linux_socketcall.c
                     35:  */
1.35      lukem      36: 
                     37: #include <sys/cdefs.h>
1.124   ! rtr        38: __KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.123 2015/04/03 20:01:07 rtr Exp $");
1.16      erh        39: 
1.74      yamt       40: #if defined(_KERNEL_OPT)
1.32      jdolecek   41: #include "opt_inet.h"
1.74      yamt       42: #endif /* defined(_KERNEL_OPT) */
1.32      jdolecek   43: 
1.1       fvdl       44: #include <sys/param.h>
                     45: #include <sys/kernel.h>
                     46: #include <sys/systm.h>
                     47: #include <sys/buf.h>
                     48: #include <sys/ioctl.h>
                     49: #include <sys/tty.h>
                     50: #include <sys/file.h>
                     51: #include <sys/filedesc.h>
                     52: #include <sys/select.h>
                     53: #include <sys/socket.h>
                     54: #include <sys/socketvar.h>
1.53      jdolecek   55: #include <sys/domain.h>
1.1       fvdl       56: #include <net/if.h>
1.21      jtk        57: #include <net/if_dl.h>
                     58: #include <net/if_types.h>
1.1       fvdl       59: #include <netinet/in.h>
1.7       mycroft    60: #include <netinet/tcp.h>
1.1       fvdl       61: #include <sys/mount.h>
                     62: #include <sys/proc.h>
                     63: #include <sys/vnode.h>
                     64: #include <sys/device.h>
1.55      perry      65: #include <sys/protosw.h>
1.32      jdolecek   66: #include <sys/mbuf.h>
                     67: #include <sys/syslog.h>
1.49      jdolecek   68: #include <sys/exec.h>
1.62      elad       69: #include <sys/kauth.h>
1.1       fvdl       70: #include <sys/syscallargs.h>
1.73      dsl        71: #include <sys/ktrace.h>
1.1       fvdl       72: 
1.60      dyoung     73: #include <lib/libkern/libkern.h>
                     74: 
1.42      fvdl       75: #include <netinet/ip6.h>
                     76: #include <netinet6/ip6_var.h>
                     77: 
1.57      christos   78: #include <compat/sys/socket.h>
1.72      christos   79: #include <compat/sys/sockio.h>
1.57      christos   80: 
1.17      christos   81: #include <compat/linux/common/linux_types.h>
                     82: #include <compat/linux/common/linux_util.h>
                     83: #include <compat/linux/common/linux_signal.h>
                     84: #include <compat/linux/common/linux_ioctl.h>
                     85: #include <compat/linux/common/linux_socket.h>
1.108     chs        86: #include <compat/linux/common/linux_fcntl.h>
1.59      manu       87: #if !defined(__alpha__) && !defined(__amd64__)
1.17      christos   88: #include <compat/linux/common/linux_socketcall.h>
1.59      manu       89: #endif
1.17      christos   90: #include <compat/linux/common/linux_sockio.h>
1.80      njoly      91: #include <compat/linux/common/linux_ipc.h>
                     92: #include <compat/linux/common/linux_sem.h>
1.17      christos   93: 
1.1       fvdl       94: #include <compat/linux/linux_syscallargs.h>
1.21      jtk        95: 
1.37      christos   96: #ifdef DEBUG_LINUX
                     97: #define DPRINTF(a) uprintf a
                     98: #else
                     99: #define DPRINTF(a)
                    100: #endif
                    101: 
1.1       fvdl      102: /*
1.16      erh       103:  * The calls in this file are entered either via the linux_socketcall()
                    104:  * interface or, on the Alpha, as individual syscalls.  The
                    105:  * linux_socketcall function does any massaging of arguments so that all
                    106:  * the calls in here need not think that they are anything other
                    107:  * than a normal syscall.
1.1       fvdl      108:  */
1.14      christos  109: 
1.81      dsl       110: static int linux_to_bsd_domain(int);
                    111: static int bsd_to_linux_domain(int);
1.119     njoly     112: static int linux_to_bsd_type(int);
1.81      dsl       113: int linux_to_bsd_sopt_level(int);
                    114: int linux_to_bsd_so_sockopt(int);
                    115: int linux_to_bsd_ip_sockopt(int);
1.117     njoly     116: int linux_to_bsd_ipv6_sockopt(int);
1.81      dsl       117: int linux_to_bsd_tcp_sockopt(int);
                    118: int linux_to_bsd_udp_sockopt(int);
1.105     joerg     119: int linux_getifname(struct lwp *, register_t *, void *);
1.97      njoly     120: int linux_getifconf(struct lwp *, register_t *, void *);
1.81      dsl       121: int linux_getifhwaddr(struct lwp *, register_t *, u_int, void *);
1.73      dsl       122: static int linux_get_sa(struct lwp *, int, struct mbuf **,
1.93      njoly     123:                const struct osockaddr *, unsigned int);
1.123     rtr       124: static int linux_get_sa_sb(struct lwp *, int, struct sockaddr_big *,
                    125:                const struct osockaddr *, socklen_t);
1.81      dsl       126: static int linux_sa_put(struct osockaddr *osa);
                    127: static int linux_to_bsd_msg_flags(int);
                    128: static int bsd_to_linux_msg_flags(int);
1.104     njoly     129: static void linux_to_bsd_msghdr(struct linux_msghdr *, struct msghdr *);
                    130: static void bsd_to_linux_msghdr(struct msghdr *, struct linux_msghdr *);
1.32      jdolecek  131: 
                    132: static const int linux_to_bsd_domain_[LINUX_AF_MAX] = {
                    133:        AF_UNSPEC,
                    134:        AF_UNIX,
                    135:        AF_INET,
                    136:        AF_CCITT,       /* LINUX_AF_AX25 */
                    137:        AF_IPX,
                    138:        AF_APPLETALK,
                    139:        -1,             /* LINUX_AF_NETROM */
                    140:        -1,             /* LINUX_AF_BRIDGE */
                    141:        -1,             /* LINUX_AF_ATMPVC */
                    142:        AF_CCITT,       /* LINUX_AF_X25 */
                    143:        AF_INET6,
                    144:        -1,             /* LINUX_AF_ROSE */
                    145:        AF_DECnet,
                    146:        -1,             /* LINUX_AF_NETBEUI */
                    147:        -1,             /* LINUX_AF_SECURITY */
                    148:        pseudo_AF_KEY,
                    149:        AF_ROUTE,       /* LINUX_AF_NETLINK */
                    150:        -1,             /* LINUX_AF_PACKET */
                    151:        -1,             /* LINUX_AF_ASH */
                    152:        -1,             /* LINUX_AF_ECONET */
                    153:        -1,             /* LINUX_AF_ATMSVC */
                    154:        AF_SNA,
                    155:        /* rest up to LINUX_AF_MAX-1 is not allocated */
                    156:        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                    157: };
1.55      perry     158: 
1.32      jdolecek  159: static const int bsd_to_linux_domain_[AF_MAX] = {
                    160:        LINUX_AF_UNSPEC,
                    161:        LINUX_AF_UNIX,
                    162:        LINUX_AF_INET,
                    163:        -1,             /* AF_IMPLINK */
                    164:        -1,             /* AF_PUP */
                    165:        -1,             /* AF_CHAOS */
                    166:        -1,             /* AF_NS */
                    167:        -1,             /* AF_ISO */
                    168:        -1,             /* AF_ECMA */
                    169:        -1,             /* AF_DATAKIT */
                    170:        LINUX_AF_AX25,  /* AF_CCITT */
                    171:        LINUX_AF_SNA,
                    172:        LINUX_AF_DECnet,
                    173:        -1,             /* AF_DLI */
                    174:        -1,             /* AF_LAT */
                    175:        -1,             /* AF_HYLINK */
                    176:        LINUX_AF_APPLETALK,
                    177:        LINUX_AF_NETLINK,
                    178:        -1,             /* AF_LINK */
                    179:        -1,             /* AF_XTP */
                    180:        -1,             /* AF_COIP */
                    181:        -1,             /* AF_CNT */
                    182:        -1,             /* pseudo_AF_RTIP */
                    183:        LINUX_AF_IPX,
                    184:        LINUX_AF_INET6,
                    185:        -1,             /* pseudo_AF_PIP */
                    186:        -1,             /* AF_ISDN */
                    187:        -1,             /* AF_NATM */
                    188:        -1,             /* AF_ARP */
                    189:        LINUX_pseudo_AF_KEY,
                    190:        -1,             /* pseudo_AF_HDRCMPLT */
                    191: };
1.1       fvdl      192: 
1.60      dyoung    193: static const struct {
                    194:        int bfl;
                    195:        int lfl;
                    196: } bsd_to_linux_msg_flags_[] = {
                    197:        {MSG_OOB,               LINUX_MSG_OOB},
                    198:        {MSG_PEEK,              LINUX_MSG_PEEK},
                    199:        {MSG_DONTROUTE,         LINUX_MSG_DONTROUTE},
                    200:        {MSG_EOR,               LINUX_MSG_EOR},
                    201:        {MSG_TRUNC,             LINUX_MSG_TRUNC},
                    202:        {MSG_CTRUNC,            LINUX_MSG_CTRUNC},
                    203:        {MSG_WAITALL,           LINUX_MSG_WAITALL},
                    204:        {MSG_DONTWAIT,          LINUX_MSG_DONTWAIT},
                    205:        {MSG_BCAST,             0},             /* not supported, clear */
                    206:        {MSG_MCAST,             0},             /* not supported, clear */
1.108     chs       207:        {MSG_NOSIGNAL,          LINUX_MSG_NOSIGNAL},
1.60      dyoung    208:        {-1, /* not supp */     LINUX_MSG_PROBE},
                    209:        {-1, /* not supp */     LINUX_MSG_FIN},
                    210:        {-1, /* not supp */     LINUX_MSG_SYN},
                    211:        {-1, /* not supp */     LINUX_MSG_CONFIRM},
                    212:        {-1, /* not supp */     LINUX_MSG_RST},
                    213:        {-1, /* not supp */     LINUX_MSG_ERRQUEUE},
                    214:        {-1, /* not supp */     LINUX_MSG_MORE},
1.49      jdolecek  215: };
                    216: 
1.1       fvdl      217: /*
                    218:  * Convert between Linux and BSD socket domain values
                    219:  */
1.32      jdolecek  220: static int
1.84      dsl       221: linux_to_bsd_domain(int ldom)
1.1       fvdl      222: {
1.32      jdolecek  223:        if (ldom < 0 || ldom >= LINUX_AF_MAX)
                    224:                return (-1);
                    225: 
                    226:        return linux_to_bsd_domain_[ldom];
                    227: }
                    228: 
                    229: /*
                    230:  * Convert between BSD and Linux socket domain values
                    231:  */
                    232: static int
1.84      dsl       233: bsd_to_linux_domain(int bdom)
1.32      jdolecek  234: {
                    235:        if (bdom < 0 || bdom >= AF_MAX)
                    236:                return (-1);
1.12      mycroft   237: 
1.32      jdolecek  238:        return bsd_to_linux_domain_[bdom];
1.1       fvdl      239: }
                    240: 
1.49      jdolecek  241: static int
1.119     njoly     242: linux_to_bsd_type(int ltype)
                    243: {
                    244:        int type, flags;
                    245: 
                    246:        /* Real types are identical between Linux and NetBSD */
                    247:        type = ltype & LINUX_SOCK_TYPE_MASK;
                    248: 
                    249:        /* But flags are not .. */
                    250:        flags = ltype & ~LINUX_SOCK_TYPE_MASK;
                    251:        if (flags & ~(LINUX_SOCK_CLOEXEC|LINUX_SOCK_NONBLOCK))
                    252:                return -1;
                    253: 
                    254:        if (flags & LINUX_SOCK_CLOEXEC)
                    255:                type |= SOCK_CLOEXEC;
                    256:        if (flags & LINUX_SOCK_NONBLOCK)
                    257:                type |= SOCK_NONBLOCK;
                    258: 
                    259:        return type;
                    260: }
                    261: 
                    262: static int
1.84      dsl       263: linux_to_bsd_msg_flags(int lflag)
1.49      jdolecek  264: {
                    265:        int i, lfl, bfl;
                    266:        int bflag = 0;
                    267: 
                    268:        if (lflag == 0)
                    269:                return (0);
                    270: 
1.60      dyoung    271:        for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
                    272:                bfl = bsd_to_linux_msg_flags_[i].bfl;
                    273:                lfl = bsd_to_linux_msg_flags_[i].lfl;
1.49      jdolecek  274: 
                    275:                if (lfl == 0)
                    276:                        continue;
                    277: 
                    278:                if (lflag & lfl) {
                    279:                        if (bfl < 0)
                    280:                                return (-1);
                    281: 
                    282:                        bflag |= bfl;
                    283:                }
                    284:        }
                    285: 
                    286:        return (bflag);
                    287: }
                    288: 
                    289: static int
1.84      dsl       290: bsd_to_linux_msg_flags(int bflag)
1.49      jdolecek  291: {
                    292:        int i, lfl, bfl;
                    293:        int lflag = 0;
                    294: 
                    295:        if (bflag == 0)
                    296:                return (0);
                    297: 
1.60      dyoung    298:        for(i = 0; i < __arraycount(bsd_to_linux_msg_flags_); i++) {
                    299:                bfl = bsd_to_linux_msg_flags_[i].bfl;
                    300:                lfl = bsd_to_linux_msg_flags_[i].lfl;
1.49      jdolecek  301: 
                    302:                if (bfl <= 0)
                    303:                        continue;
                    304: 
                    305:                if (bflag & bfl) {
                    306:                        if (lfl < 0)
                    307:                                return (-1);
                    308: 
                    309:                        lflag |= lfl;
                    310:                }
                    311:        }
                    312: 
                    313:        return (lflag);
                    314: }
                    315: 
1.2       mycroft   316: int
1.85      dsl       317: linux_sys_socket(struct lwp *l, const struct linux_sys_socket_args *uap, register_t *retval)
1.16      erh       318: {
1.85      dsl       319:        /* {
1.1       fvdl      320:                syscallarg(int) domain;
                    321:                syscallarg(int) type;
                    322:                syscallarg(int) protocol;
1.85      dsl       323:        } */
                    324:        struct sys___socket30_args bsa;
1.119     njoly     325:        int error;
1.108     chs       326: 
1.1       fvdl      327: 
1.16      erh       328:        SCARG(&bsa, protocol) = SCARG(uap, protocol);
                    329:        SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1       fvdl      330:        if (SCARG(&bsa, domain) == -1)
                    331:                return EINVAL;
1.119     njoly     332:        SCARG(&bsa, type) = linux_to_bsd_type(SCARG(uap, type));
                    333:        if (SCARG(&bsa, type) == -1)
                    334:                return EINVAL;
1.110     christos  335:        /*
                    336:         * Apparently linux uses this to talk to ISDN sockets. If we fail
                    337:         * now programs seems to handle it, but if we don't we are going
                    338:         * to fail when we bind and programs don't handle this well.
                    339:         */
                    340:        if (SCARG(&bsa, domain) == AF_ROUTE && SCARG(&bsa, type) == SOCK_RAW)
                    341:                return ENOTSUP;
1.64      pavel     342:        error = sys___socket30(l, &bsa, retval);
1.41      itojun    343: 
                    344: #ifdef INET6
                    345:        /*
1.42      fvdl      346:         * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by
                    347:         * default and some apps depend on this. So, set V6ONLY to 0
                    348:         * for Linux apps if the sysctl value is set to 1.
1.41      itojun    349:         */
1.42      fvdl      350:        if (!error && ip6_v6only && SCARG(&bsa, domain) == PF_INET6) {
1.89      ad        351:                struct socket *so;
1.42      fvdl      352: 
1.89      ad        353:                if (fd_getsock(*retval, &so) == 0) {
1.98      plunky    354:                        int val = 0;
1.42      fvdl      355: 
                    356:                        /* ignore error */
1.98      plunky    357:                        (void)so_setsockopt(l, so, IPPROTO_IPV6, IPV6_V6ONLY,
                    358:                            &val, sizeof(val));
1.42      fvdl      359: 
1.87      ad        360:                        fd_putfile(*retval);
1.41      itojun    361:                }
                    362:        }
                    363: #endif
                    364: 
1.42      fvdl      365:        return (error);
1.1       fvdl      366: }
                    367: 
1.2       mycroft   368: int
1.85      dsl       369: linux_sys_socketpair(struct lwp *l, const struct linux_sys_socketpair_args *uap, register_t *retval)
1.1       fvdl      370: {
1.85      dsl       371:        /* {
1.1       fvdl      372:                syscallarg(int) domain;
                    373:                syscallarg(int) type;
                    374:                syscallarg(int) protocol;
                    375:                syscallarg(int *) rsv;
1.85      dsl       376:        } */
1.12      mycroft   377:        struct sys_socketpair_args bsa;
1.1       fvdl      378: 
1.16      erh       379:        SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1       fvdl      380:        if (SCARG(&bsa, domain) == -1)
                    381:                return EINVAL;
1.119     njoly     382:        SCARG(&bsa, type) = linux_to_bsd_type(SCARG(uap, type));
                    383:        if (SCARG(&bsa, type) == -1)
                    384:                return EINVAL;
1.16      erh       385:        SCARG(&bsa, protocol) = SCARG(uap, protocol);
                    386:        SCARG(&bsa, rsv) = SCARG(uap, rsv);
1.1       fvdl      387: 
1.43      thorpej   388:        return sys_socketpair(l, &bsa, retval);
1.1       fvdl      389: }
                    390: 
1.2       mycroft   391: int
1.85      dsl       392: linux_sys_sendto(struct lwp *l, const struct linux_sys_sendto_args *uap, register_t *retval)
1.1       fvdl      393: {
1.85      dsl       394:        /* {
1.32      jdolecek  395:                syscallarg(int)                         s;
                    396:                syscallarg(void *)                      msg;
                    397:                syscallarg(int)                         len;
                    398:                syscallarg(int)                         flags;
                    399:                syscallarg(struct osockaddr *)          to;
                    400:                syscallarg(int)                         tolen;
1.85      dsl       401:        } */
1.73      dsl       402:        struct msghdr   msg;
                    403:        struct iovec    aiov;
                    404:        struct mbuf *nam;
                    405:        int bflags;
                    406:        int error;
1.1       fvdl      407: 
1.73      dsl       408:        /* Translate message flags.  */
                    409:        bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
                    410:        if (bflags < 0)
                    411:                /* Some supported flag */
                    412:                return EINVAL;
1.32      jdolecek  413: 
1.95      njoly     414:        msg.msg_flags = 0;
                    415:        msg.msg_name = NULL;
                    416:        msg.msg_control = NULL;
                    417: 
                    418:        if (SCARG(uap, tolen)) {
                    419:                /* Read in and convert the sockaddr */
                    420:                error = linux_get_sa(l, SCARG(uap, s), &nam, SCARG(uap, to),
                    421:                    SCARG(uap, tolen));
                    422:                if (error)
                    423:                        return (error);
                    424:                msg.msg_flags |= MSG_NAMEMBUF;
                    425:                msg.msg_name = nam;
                    426:                msg.msg_namelen = SCARG(uap, tolen);
                    427:        }
1.32      jdolecek  428: 
1.73      dsl       429:        msg.msg_iov = &aiov;
                    430:        msg.msg_iovlen = 1;
                    431:        aiov.iov_base = __UNCONST(SCARG(uap, msg));
                    432:        aiov.iov_len = SCARG(uap, len);
1.1       fvdl      433: 
1.73      dsl       434:        return do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
1.32      jdolecek  435: }
                    436: 
1.104     njoly     437: static void
                    438: linux_to_bsd_msghdr(struct linux_msghdr *lmsg, struct msghdr *bmsg)
                    439: {
                    440:        bmsg->msg_name = lmsg->msg_name;
                    441:        bmsg->msg_namelen = lmsg->msg_namelen;
                    442:        bmsg->msg_iov = lmsg->msg_iov;
                    443:        bmsg->msg_iovlen = lmsg->msg_iovlen;
                    444:        bmsg->msg_control = lmsg->msg_control;
                    445:        bmsg->msg_controllen = lmsg->msg_controllen;
                    446:        bmsg->msg_flags = lmsg->msg_flags;
                    447: }
                    448: 
                    449: static void
                    450: bsd_to_linux_msghdr(struct msghdr *bmsg, struct linux_msghdr *lmsg)
                    451: {
                    452:        lmsg->msg_name = bmsg->msg_name;
                    453:        lmsg->msg_namelen = bmsg->msg_namelen;
                    454:        lmsg->msg_iov = bmsg->msg_iov;
                    455:        lmsg->msg_iovlen = bmsg->msg_iovlen;
                    456:        lmsg->msg_control = bmsg->msg_control;
                    457:        lmsg->msg_controllen = bmsg->msg_controllen;
                    458:        lmsg->msg_flags = bmsg->msg_flags;
                    459: }
                    460: 
1.32      jdolecek  461: int
1.85      dsl       462: linux_sys_sendmsg(struct lwp *l, const struct linux_sys_sendmsg_args *uap, register_t *retval)
1.32      jdolecek  463: {
1.85      dsl       464:        /* {
1.32      jdolecek  465:                syscallarg(int) s;
1.104     njoly     466:                syscallarg(struct linux_msghdr *) msg;
1.32      jdolecek  467:                syscallarg(u_int) flags;
1.85      dsl       468:        } */
1.32      jdolecek  469:        struct msghdr   msg;
1.104     njoly     470:        struct linux_msghdr lmsg;
1.32      jdolecek  471:        int             error;
1.49      jdolecek  472:        int             bflags;
1.73      dsl       473:        struct mbuf     *nam;
                    474:        u_int8_t        *control;
                    475:        struct mbuf     *ctl_mbuf = NULL;
1.32      jdolecek  476: 
1.104     njoly     477:        error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg));
1.100     njoly     478:        if (error)
                    479:                return error;
1.104     njoly     480:        linux_to_bsd_msghdr(&lmsg, &msg);
1.100     njoly     481: 
1.73      dsl       482:        msg.msg_flags = MSG_IOVUSRSPACE;
1.32      jdolecek  483: 
1.49      jdolecek  484:        /*
                    485:         * Translate message flags.
                    486:         */
                    487:        bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
1.73      dsl       488:        if (bflags < 0)
1.49      jdolecek  489:                /* Some supported flag */
1.73      dsl       490:                return EINVAL;
                    491: 
1.104     njoly     492:        if (lmsg.msg_name) {
1.73      dsl       493:                /* Read in and convert the sockaddr */
                    494:                error = linux_get_sa(l, SCARG(uap, s), &nam, msg.msg_name,
                    495:                    msg.msg_namelen);
                    496:                if (error)
                    497:                        return (error);
                    498:                msg.msg_flags |= MSG_NAMEMBUF;
                    499:                msg.msg_name = nam;
1.32      jdolecek  500:        }
                    501: 
                    502:        /*
1.49      jdolecek  503:         * Handle cmsg if there is any.
1.32      jdolecek  504:         */
1.104     njoly     505:        if (LINUX_CMSG_FIRSTHDR(&lmsg)) {
1.76      dsl       506:                struct linux_cmsghdr l_cmsg, *l_cc;
                    507:                struct cmsghdr *cmsg;
1.49      jdolecek  508:                ssize_t resid = msg.msg_controllen;
                    509:                size_t clen, cidx = 0, cspace;
                    510: 
1.73      dsl       511:                ctl_mbuf = m_get(M_WAIT, MT_CONTROL);
                    512:                clen = MLEN;
                    513:                control = mtod(ctl_mbuf, void *);
1.49      jdolecek  514: 
1.104     njoly     515:                l_cc = LINUX_CMSG_FIRSTHDR(&lmsg);
1.49      jdolecek  516:                do {
1.76      dsl       517:                        error = copyin(l_cc, &l_cmsg, sizeof(l_cmsg));
1.49      jdolecek  518:                        if (error)
                    519:                                goto done;
                    520: 
                    521:                        /*
                    522:                         * Sanity check the control message length.
                    523:                         */
1.76      dsl       524:                        if (l_cmsg.cmsg_len > resid
                    525:                            || l_cmsg.cmsg_len < sizeof l_cmsg) {
1.49      jdolecek  526:                                error = EINVAL;
                    527:                                goto done;
                    528:                        }
                    529: 
                    530:                        /*
                    531:                         * Refuse unsupported control messages, and
                    532:                         * translate fields as appropriate.
                    533:                         */
1.76      dsl       534:                        switch (l_cmsg.cmsg_level) {
1.49      jdolecek  535:                        case LINUX_SOL_SOCKET:
                    536:                                /* It only differs on some archs */
1.73      dsl       537:                                if (LINUX_SOL_SOCKET != SOL_SOCKET)
1.76      dsl       538:                                        l_cmsg.cmsg_level = SOL_SOCKET;
1.49      jdolecek  539: 
1.76      dsl       540:                                switch(l_cmsg.cmsg_type) {
1.49      jdolecek  541:                                case LINUX_SCM_RIGHTS:
                    542:                                        /* Linux SCM_RIGHTS is same as NetBSD */
                    543:                                        break;
                    544: 
1.108     chs       545:                                case LINUX_SCM_CREDENTIALS:
                    546:                                        /* no native equivalent, just drop it */
                    547:                                        m_free(ctl_mbuf);
                    548:                                        ctl_mbuf = NULL;
                    549:                                        msg.msg_control = NULL;
                    550:                                        msg.msg_controllen = 0;
                    551:                                        goto skipcmsg;
                    552: 
1.49      jdolecek  553:                                default:
                    554:                                        /* other types not supported */
                    555:                                        error = EINVAL;
                    556:                                        goto done;
                    557:                                }
                    558:                                break;
                    559:                        default:
                    560:                                /* pray and leave intact */
                    561:                                break;
                    562:                        }
                    563: 
1.76      dsl       564:                        cspace = CMSG_SPACE(l_cmsg.cmsg_len - sizeof(l_cmsg));
1.49      jdolecek  565: 
                    566:                        /* Check the buffer is big enough */
                    567:                        if (__predict_false(cidx + cspace > clen)) {
                    568:                                u_int8_t *nc;
                    569: 
                    570:                                clen = cidx + cspace;
1.73      dsl       571:                                if (clen >= PAGE_SIZE) {
                    572:                                        error = EINVAL;
                    573:                                        goto done;
                    574:                                }
                    575:                                nc = realloc(clen <= MLEN ? NULL : control,
                    576:                                                clen, M_TEMP, M_WAITOK);
1.49      jdolecek  577:                                if (!nc) {
                    578:                                        error = ENOMEM;
                    579:                                        goto done;
                    580:                                }
1.73      dsl       581:                                if (cidx <= MLEN)
                    582:                                        /* Old buffer was in mbuf... */
                    583:                                        memcpy(nc, control, cidx);
1.49      jdolecek  584:                                control = nc;
                    585:                        }
                    586: 
                    587:                        /* Copy header */
1.76      dsl       588:                        cmsg = (void *)&control[cidx];
                    589:                        cmsg->cmsg_len = l_cmsg.cmsg_len + LINUX_CMSG_ALIGN_DELTA;
                    590:                        cmsg->cmsg_level = l_cmsg.cmsg_level;
                    591:                        cmsg->cmsg_type = l_cmsg.cmsg_type;
1.49      jdolecek  592: 
1.108     chs       593:                        /* Zero area between header and data */
1.76      dsl       594:                        memset(cmsg + 1, 0, 
1.112     joerg     595:                                CMSG_ALIGN(sizeof(*cmsg)) - sizeof(*cmsg));
1.49      jdolecek  596: 
                    597:                        /* Copyin the data */
1.76      dsl       598:                        error = copyin(LINUX_CMSG_DATA(l_cc),
1.112     joerg     599:                                CMSG_DATA(cmsg),
1.76      dsl       600:                                l_cmsg.cmsg_len - sizeof(l_cmsg));
1.49      jdolecek  601:                        if (error)
                    602:                                goto done;
                    603: 
1.76      dsl       604:                        resid -= LINUX_CMSG_ALIGN(l_cmsg.cmsg_len);
1.49      jdolecek  605:                        cidx += cspace;
1.76      dsl       606:                } while ((l_cc = LINUX_CMSG_NXTHDR(&msg, l_cc)) && resid > 0);
1.49      jdolecek  607: 
1.73      dsl       608:                /* If we allocated a buffer, attach to mbuf */
                    609:                if (cidx > MLEN) {
                    610:                        MEXTADD(ctl_mbuf, control, clen, M_MBUF, NULL, NULL);
                    611:                        ctl_mbuf->m_flags |= M_EXT_RW;
1.49      jdolecek  612:                }
1.73      dsl       613:                control = NULL;
                    614:                ctl_mbuf->m_len = cidx;
1.49      jdolecek  615: 
1.73      dsl       616:                msg.msg_control = ctl_mbuf;
                    617:                msg.msg_flags |= MSG_CONTROLMBUF;
1.101     njoly     618: 
1.114     christos  619:                ktrkuser("mbcontrol", mtod(ctl_mbuf, void *),
1.101     njoly     620:                    msg.msg_controllen);
1.49      jdolecek  621:        }
1.32      jdolecek  622: 
1.108     chs       623: skipcmsg:
1.73      dsl       624:        error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
                    625:        /* Freed internally */
                    626:        ctl_mbuf = NULL;
1.32      jdolecek  627: 
1.49      jdolecek  628: done:
1.73      dsl       629:        if (ctl_mbuf != NULL) {
                    630:                if (control != NULL && control != mtod(ctl_mbuf, void *))
                    631:                        free(control, M_MBUF);
                    632:                m_free(ctl_mbuf);
                    633:        }
1.49      jdolecek  634:        return (error);
1.1       fvdl      635: }
                    636: 
1.2       mycroft   637: int
1.85      dsl       638: linux_sys_recvfrom(struct lwp *l, const struct linux_sys_recvfrom_args *uap, register_t *retval)
1.16      erh       639: {
1.85      dsl       640:        /* {
1.1       fvdl      641:                syscallarg(int) s;
                    642:                syscallarg(void *) buf;
                    643:                syscallarg(int) len;
                    644:                syscallarg(int) flags;
1.32      jdolecek  645:                syscallarg(struct osockaddr *) from;
                    646:                syscallarg(int *) fromlenaddr;
1.85      dsl       647:        } */
1.32      jdolecek  648:        int             error;
                    649:        struct sys_recvfrom_args bra;
1.1       fvdl      650: 
1.16      erh       651:        SCARG(&bra, s) = SCARG(uap, s);
                    652:        SCARG(&bra, buf) = SCARG(uap, buf);
                    653:        SCARG(&bra, len) = SCARG(uap, len);
                    654:        SCARG(&bra, flags) = SCARG(uap, flags);
1.32      jdolecek  655:        SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from);
1.61      mrg       656:        SCARG(&bra, fromlenaddr) = (socklen_t *)SCARG(uap, fromlenaddr);
1.1       fvdl      657: 
1.43      thorpej   658:        if ((error = sys_recvfrom(l, &bra, retval)))
1.32      jdolecek  659:                return (error);
                    660: 
                    661:        if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from))))
                    662:                return (error);
                    663: 
                    664:        return (0);
                    665: }
                    666: 
1.76      dsl       667: static int
                    668: linux_copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
                    669: {
                    670:        int dlen, error = 0;
                    671:        struct cmsghdr *cmsg;
                    672:        struct linux_cmsghdr linux_cmsg;
                    673:        struct mbuf *m;
                    674:        char *q, *q_end;
                    675: 
                    676:        if (mp->msg_controllen <= 0 || control == 0) {
                    677:                mp->msg_controllen = 0;
                    678:                free_control_mbuf(l, control, control);
                    679:                return 0;
                    680:        }
                    681: 
1.101     njoly     682:        ktrkuser("msgcontrol", mtod(control, void *), mp->msg_controllen);
                    683: 
1.76      dsl       684:        q = (char *)mp->msg_control;
                    685:        q_end = q + mp->msg_controllen;
                    686: 
                    687:        for (m = control; m != NULL; ) {
                    688:                cmsg = mtod(m, struct cmsghdr *);
                    689: 
                    690:                /*
                    691:                 * Fixup cmsg. We handle two things:
                    692:                 * 0. different sizeof cmsg_len.
                    693:                 * 1. different values for level/type on some archs
                    694:                 * 2. different alignment of CMSG_DATA on some archs
                    695:                 */
                    696:                linux_cmsg.cmsg_len = cmsg->cmsg_len - LINUX_CMSG_ALIGN_DELTA;
                    697:                linux_cmsg.cmsg_level = cmsg->cmsg_level;
                    698:                linux_cmsg.cmsg_type = cmsg->cmsg_type;
                    699: 
                    700:                dlen = q_end - q;
                    701:                if (linux_cmsg.cmsg_len > dlen) {
                    702:                        /* Not enough room for the parameter */
                    703:                        dlen -= sizeof linux_cmsg;
                    704:                        if (dlen <= 0)
                    705:                                /* Discard if header wont fit */
                    706:                                break;
                    707:                        mp->msg_flags |= MSG_CTRUNC;
                    708:                        if (linux_cmsg.cmsg_level == SOL_SOCKET
                    709:                            && linux_cmsg.cmsg_type == SCM_RIGHTS)
                    710:                                /* Do not truncate me ... */
                    711:                                break;
                    712:                } else
                    713:                        dlen = linux_cmsg.cmsg_len - sizeof linux_cmsg;
                    714: 
                    715:                switch (linux_cmsg.cmsg_level) {
                    716:                case SOL_SOCKET:
                    717:                        linux_cmsg.cmsg_level = LINUX_SOL_SOCKET;
                    718:                        switch (linux_cmsg.cmsg_type) {
                    719:                        case SCM_RIGHTS:
                    720:                                /* Linux SCM_RIGHTS is same as NetBSD */
                    721:                                break;
                    722: 
                    723:                        default:
                    724:                                /* other types not supported */
                    725:                                error = EINVAL;
                    726:                                goto done;
                    727:                        }
1.109     wiz       728:                        /* machine dependent ! */
1.76      dsl       729:                        break;
                    730:                default:
                    731:                        /* pray and leave intact */
                    732:                        break;
                    733:                }
                    734: 
                    735:                /* There can be padding between the header and data... */
1.102     njoly     736:                error = copyout(&linux_cmsg, q, sizeof linux_cmsg);
1.76      dsl       737:                if (error != 0) {
                    738:                        error = copyout(CCMSG_DATA(cmsg), q + sizeof linux_cmsg,
                    739:                            dlen);
                    740:                }
                    741:                if (error != 0) {
                    742:                        /* We must free all the SCM_RIGHTS */
                    743:                        m = control;
                    744:                        break;
                    745:                }
                    746:                m = m->m_next;
1.103     njoly     747:                if (m == NULL || q + LINUX_CMSG_SPACE(dlen) > q_end) {
                    748:                        q += LINUX_CMSG_LEN(dlen);
1.76      dsl       749:                        break;
                    750:                }
1.103     njoly     751:                q += LINUX_CMSG_SPACE(dlen);
1.76      dsl       752:        }
                    753: 
                    754:   done:
                    755:        free_control_mbuf(l, control, m);
                    756: 
                    757:        mp->msg_controllen = q - (char *)mp->msg_control;
                    758:        return error;
                    759: }
                    760: 
1.32      jdolecek  761: int
1.85      dsl       762: linux_sys_recvmsg(struct lwp *l, const struct linux_sys_recvmsg_args *uap, register_t *retval)
1.32      jdolecek  763: {
1.85      dsl       764:        /* {
1.32      jdolecek  765:                syscallarg(int) s;
1.104     njoly     766:                syscallarg(struct linux_msghdr *) msg;
1.32      jdolecek  767:                syscallarg(u_int) flags;
1.85      dsl       768:        } */
1.32      jdolecek  769:        struct msghdr   msg;
1.104     njoly     770:        struct linux_msghdr lmsg;
1.32      jdolecek  771:        int             error;
1.76      dsl       772:        struct mbuf     *from, *control;
1.32      jdolecek  773: 
1.104     njoly     774:        error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg));
1.76      dsl       775:        if (error)
                    776:                return (error);
1.104     njoly     777:        linux_to_bsd_msghdr(&lmsg, &msg);
1.49      jdolecek  778: 
1.76      dsl       779:        msg.msg_flags = linux_to_bsd_msg_flags(SCARG(uap, flags));
                    780:        if (msg.msg_flags < 0) {
1.49      jdolecek  781:                /* Some unsupported flag */
                    782:                return (EINVAL);
                    783:        }
1.76      dsl       784:        msg.msg_flags |= MSG_IOVUSRSPACE;
1.49      jdolecek  785: 
1.76      dsl       786:        error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
                    787:            msg.msg_control != NULL ? &control : NULL, retval);
                    788:        if (error != 0)
                    789:                return error;
                    790: 
                    791:        if (msg.msg_control != NULL)
                    792:                error = linux_copyout_msg_control(l, &msg, control);
                    793: 
                    794:        if (error == 0 && from != 0) {
                    795:                mtod(from, struct osockaddr *)->sa_family =
                    796:                    bsd_to_linux_domain(mtod(from, struct sockaddr *)->sa_family);
                    797:                error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
                    798:                        from);
                    799:        } else
                    800:                msg.msg_namelen = 0;
                    801: 
                    802:        if (from != NULL)
                    803:                m_free(from);
                    804: 
                    805:        if (error == 0) {
                    806:                msg.msg_flags = bsd_to_linux_msg_flags(msg.msg_flags);
                    807:                if (msg.msg_flags < 0)
                    808:                        /* Some flag unsupported by Linux */
                    809:                        error = EINVAL;
1.101     njoly     810:                else {
                    811:                        ktrkuser("msghdr", &msg, sizeof(msg));
1.104     njoly     812:                        bsd_to_linux_msghdr(&msg, &lmsg);
                    813:                        error = copyout(&lmsg, SCARG(uap, msg), sizeof(lmsg));
1.101     njoly     814:                }
1.49      jdolecek  815:        }
                    816: 
1.32      jdolecek  817:        return (error);
1.1       fvdl      818: }
                    819: 
                    820: /*
                    821:  * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
                    822:  * is different, the rest matches IPPROTO_* on both systems.
                    823:  */
1.2       mycroft   824: int
1.84      dsl       825: linux_to_bsd_sopt_level(int llevel)
1.1       fvdl      826: {
1.12      mycroft   827: 
1.1       fvdl      828:        switch (llevel) {
                    829:        case LINUX_SOL_SOCKET:
                    830:                return SOL_SOCKET;
1.3       mycroft   831:        case LINUX_SOL_IP:
                    832:                return IPPROTO_IP;
1.117     njoly     833: #ifdef INET6
                    834:        case LINUX_SOL_IPV6:
                    835:                return IPPROTO_IPV6;
                    836: #endif
1.3       mycroft   837:        case LINUX_SOL_TCP:
                    838:                return IPPROTO_TCP;
                    839:        case LINUX_SOL_UDP:
                    840:                return IPPROTO_UDP;
1.1       fvdl      841:        default:
                    842:                return -1;
                    843:        }
                    844: }
                    845: 
                    846: /*
                    847:  * Convert Linux socket level socket option numbers to NetBSD values.
                    848:  */
1.2       mycroft   849: int
1.84      dsl       850: linux_to_bsd_so_sockopt(int lopt)
1.1       fvdl      851: {
1.12      mycroft   852: 
1.1       fvdl      853:        switch (lopt) {
                    854:        case LINUX_SO_DEBUG:
                    855:                return SO_DEBUG;
                    856:        case LINUX_SO_REUSEADDR:
1.55      perry     857:                /*
1.39      jschauma  858:                 * Linux does not implement SO_REUSEPORT, but allows reuse of a
                    859:                 * host:port pair through SO_REUSEADDR even if the address is not a
                    860:                 * multicast-address.  Effectively, this means that we should use
                    861:                 * SO_REUSEPORT to allow Linux applications to not exit with
                    862:                 * EADDRINUSE
                    863:                 */
                    864:                return SO_REUSEPORT;
1.1       fvdl      865:        case LINUX_SO_TYPE:
                    866:                return SO_TYPE;
                    867:        case LINUX_SO_ERROR:
                    868:                return SO_ERROR;
                    869:        case LINUX_SO_DONTROUTE:
                    870:                return SO_DONTROUTE;
                    871:        case LINUX_SO_BROADCAST:
                    872:                return SO_BROADCAST;
                    873:        case LINUX_SO_SNDBUF:
                    874:                return SO_SNDBUF;
                    875:        case LINUX_SO_RCVBUF:
                    876:                return SO_RCVBUF;
1.116     njoly     877:        case LINUX_SO_SNDLOWAT:
                    878:                return SO_SNDLOWAT;
                    879:        case LINUX_SO_RCVLOWAT:
                    880:                return SO_RCVLOWAT;
1.1       fvdl      881:        case LINUX_SO_KEEPALIVE:
                    882:                return SO_KEEPALIVE;
                    883:        case LINUX_SO_OOBINLINE:
                    884:                return SO_OOBINLINE;
                    885:        case LINUX_SO_LINGER:
                    886:                return SO_LINGER;
1.116     njoly     887:        case LINUX_SO_ACCEPTCONN:
                    888:                return SO_ACCEPTCONN;
1.1       fvdl      889:        case LINUX_SO_PRIORITY:
                    890:        case LINUX_SO_NO_CHECK:
                    891:        default:
                    892:                return -1;
                    893:        }
                    894: }
                    895: 
                    896: /*
1.3       mycroft   897:  * Convert Linux IP level socket option number to NetBSD values.
                    898:  */
                    899: int
1.84      dsl       900: linux_to_bsd_ip_sockopt(int lopt)
1.3       mycroft   901: {
1.12      mycroft   902: 
1.3       mycroft   903:        switch (lopt) {
                    904:        case LINUX_IP_TOS:
                    905:                return IP_TOS;
                    906:        case LINUX_IP_TTL:
                    907:                return IP_TTL;
1.107     dsl       908:        case LINUX_IP_HDRINCL:
                    909:                return IP_HDRINCL;
1.3       mycroft   910:        case LINUX_IP_MULTICAST_TTL:
                    911:                return IP_MULTICAST_TTL;
                    912:        case LINUX_IP_MULTICAST_LOOP:
                    913:                return IP_MULTICAST_LOOP;
                    914:        case LINUX_IP_MULTICAST_IF:
                    915:                return IP_MULTICAST_IF;
                    916:        case LINUX_IP_ADD_MEMBERSHIP:
                    917:                return IP_ADD_MEMBERSHIP;
                    918:        case LINUX_IP_DROP_MEMBERSHIP:
                    919:                return IP_DROP_MEMBERSHIP;
                    920:        default:
                    921:                return -1;
                    922:        }
                    923: }
                    924: 
                    925: /*
1.117     njoly     926:  * Convert Linux IPV6 level socket option number to NetBSD values.
                    927:  */
                    928: #ifdef INET6
                    929: int
                    930: linux_to_bsd_ipv6_sockopt(int lopt)
                    931: {
                    932: 
                    933:        switch (lopt) {
                    934:        case LINUX_IPV6_V6ONLY:
                    935:                return IPV6_V6ONLY;
                    936:        default:
                    937:                return -1;
                    938:        }
                    939: }
                    940: #endif
                    941: 
                    942: /*
1.3       mycroft   943:  * Convert Linux TCP level socket option number to NetBSD values.
                    944:  */
                    945: int
1.84      dsl       946: linux_to_bsd_tcp_sockopt(int lopt)
1.3       mycroft   947: {
1.12      mycroft   948: 
1.3       mycroft   949:        switch (lopt) {
1.6       mycroft   950:        case LINUX_TCP_NODELAY:
                    951:                return TCP_NODELAY;
                    952:        case LINUX_TCP_MAXSEG:
                    953:                return TCP_MAXSEG;
1.3       mycroft   954:        default:
1.6       mycroft   955:                return -1;
1.3       mycroft   956:        }
                    957: }
                    958: 
                    959: /*
                    960:  * Convert Linux UDP level socket option number to NetBSD values.
                    961:  */
                    962: int
1.84      dsl       963: linux_to_bsd_udp_sockopt(int lopt)
1.3       mycroft   964: {
1.12      mycroft   965: 
1.3       mycroft   966:        switch (lopt) {
                    967:        default:
1.6       mycroft   968:                return -1;
1.3       mycroft   969:        }
                    970: }
                    971: 
                    972: /*
1.5       mycroft   973:  * Another reasonably straightforward function: setsockopt(2).
1.1       fvdl      974:  * The level and option numbers are converted; the values passed
                    975:  * are not (yet) converted, the ones currently implemented don't
                    976:  * need conversion, as they are the same on both systems.
                    977:  */
1.2       mycroft   978: int
1.85      dsl       979: linux_sys_setsockopt(struct lwp *l, const struct linux_sys_setsockopt_args *uap, register_t *retval)
1.16      erh       980: {
1.85      dsl       981:        /* {
1.1       fvdl      982:                syscallarg(int) s;
                    983:                syscallarg(int) level;
                    984:                syscallarg(int) optname;
                    985:                syscallarg(void *) optval;
                    986:                syscallarg(int) optlen;
1.85      dsl       987:        } */
1.12      mycroft   988:        struct sys_setsockopt_args bsa;
1.16      erh       989:        int name;
1.1       fvdl      990: 
1.16      erh       991:        SCARG(&bsa, s) = SCARG(uap, s);
                    992:        SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
                    993:        SCARG(&bsa, val) = SCARG(uap, optval);
                    994:        SCARG(&bsa, valsize) = SCARG(uap, optlen);
1.1       fvdl      995: 
1.70      mlelstv   996:        /*
                    997:         * Linux supports only SOL_SOCKET for AF_LOCAL domain sockets
                    998:         * and returns EOPNOTSUPP for other levels
                    999:         */
                   1000:        if (SCARG(&bsa, level) != SOL_SOCKET) {
                   1001:                struct socket *so;
1.89      ad       1002:                int error, family;
1.70      mlelstv  1003: 
1.96      ad       1004:                /* fd_getsock() will use the descriptor for us */
1.89      ad       1005:                if ((error = fd_getsock(SCARG(&bsa, s), &so)) != 0)
1.70      mlelstv  1006:                        return error;
                   1007:                family = so->so_proto->pr_domain->dom_family;
1.87      ad       1008:                fd_putfile(SCARG(&bsa, s));
1.70      mlelstv  1009: 
                   1010:                if (family == AF_LOCAL)
                   1011:                        return EOPNOTSUPP;
                   1012:        }
                   1013: 
1.1       fvdl     1014:        switch (SCARG(&bsa, level)) {
1.40      itojun   1015:        case SOL_SOCKET:
                   1016:                name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
                   1017:                break;
                   1018:        case IPPROTO_IP:
                   1019:                name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
                   1020:                break;
1.117     njoly    1021: #ifdef INET6
                   1022:        case IPPROTO_IPV6:
                   1023:                name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname));
                   1024:                break;
                   1025: #endif
1.40      itojun   1026:        case IPPROTO_TCP:
                   1027:                name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
                   1028:                break;
                   1029:        case IPPROTO_UDP:
                   1030:                name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
                   1031:                break;
                   1032:        default:
                   1033:                return EINVAL;
1.1       fvdl     1034:        }
                   1035: 
                   1036:        if (name == -1)
                   1037:                return EINVAL;
                   1038:        SCARG(&bsa, name) = name;
                   1039: 
1.43      thorpej  1040:        return sys_setsockopt(l, &bsa, retval);
1.1       fvdl     1041: }
                   1042: 
                   1043: /*
                   1044:  * getsockopt(2) is very much the same as setsockopt(2) (see above)
                   1045:  */
1.2       mycroft  1046: int
1.85      dsl      1047: linux_sys_getsockopt(struct lwp *l, const struct linux_sys_getsockopt_args *uap, register_t *retval)
1.16      erh      1048: {
1.85      dsl      1049:        /* {
1.1       fvdl     1050:                syscallarg(int) s;
                   1051:                syscallarg(int) level;
                   1052:                syscallarg(int) optname;
                   1053:                syscallarg(void *) optval;
1.16      erh      1054:                syscallarg(int *) optlen;
1.85      dsl      1055:        } */
1.12      mycroft  1056:        struct sys_getsockopt_args bga;
1.16      erh      1057:        int name;
1.1       fvdl     1058: 
1.16      erh      1059:        SCARG(&bga, s) = SCARG(uap, s);
                   1060:        SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
                   1061:        SCARG(&bga, val) = SCARG(uap, optval);
1.61      mrg      1062:        SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen);
1.1       fvdl     1063: 
                   1064:        switch (SCARG(&bga, level)) {
1.40      itojun   1065:        case SOL_SOCKET:
                   1066:                name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
                   1067:                break;
                   1068:        case IPPROTO_IP:
                   1069:                name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
                   1070:                break;
1.117     njoly    1071: #ifdef INET6
                   1072:        case IPPROTO_IPV6:
                   1073:                name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname));
                   1074:                break;
                   1075: #endif
1.40      itojun   1076:        case IPPROTO_TCP:
                   1077:                name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
                   1078:                break;
                   1079:        case IPPROTO_UDP:
                   1080:                name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
                   1081:                break;
                   1082:        default:
                   1083:                return EINVAL;
1.1       fvdl     1084:        }
                   1085: 
                   1086:        if (name == -1)
                   1087:                return EINVAL;
1.5       mycroft  1088:        SCARG(&bga, name) = name;
1.1       fvdl     1089: 
1.43      thorpej  1090:        return sys_getsockopt(l, &bga, retval);
1.13      mycroft  1091: }
                   1092: 
1.97      njoly    1093: int
1.105     joerg    1094: linux_getifname(struct lwp *l, register_t *retval, void *data)
                   1095: {
                   1096:        struct ifnet *ifp;
                   1097:        struct linux_ifreq ifr;
                   1098:        int error;
                   1099: 
                   1100:        error = copyin(data, &ifr, sizeof(ifr));
                   1101:        if (error)
                   1102:                return error;
                   1103: 
1.118     rmind    1104:        ifp = if_byindex(ifr.ifr_ifru.ifru_ifindex);
1.105     joerg    1105:        if (ifp == NULL)
                   1106:                return ENODEV;
                   1107: 
                   1108:        strncpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
                   1109: 
1.106     joerg    1110:        return copyout(&ifr, data, sizeof(ifr));
1.105     joerg    1111: }
                   1112: 
                   1113: int
1.97      njoly    1114: linux_getifconf(struct lwp *l, register_t *retval, void *data)
                   1115: {
1.122     ozaki-r  1116:        struct linux_ifreq ifr, *ifrp = NULL;
1.115     christos 1117:        struct linux_ifconf ifc;
1.97      njoly    1118:        struct ifnet *ifp;
                   1119:        struct ifaddr *ifa;
                   1120:        struct sockaddr *sa;
                   1121:        struct osockaddr *osa;
1.122     ozaki-r  1122:        int space = 0, error = 0;
1.97      njoly    1123:        const int sz = (int)sizeof(ifr);
1.122     ozaki-r  1124:        bool docopy;
1.97      njoly    1125: 
1.115     christos 1126:        error = copyin(data, &ifc, sizeof(ifc));
                   1127:        if (error)
                   1128:                return error;
                   1129: 
1.122     ozaki-r  1130:        docopy = ifc.ifc_req != NULL;
                   1131:        if (docopy) {
1.115     christos 1132:                space = ifc.ifc_len;
1.122     ozaki-r  1133:                ifrp = ifc.ifc_req;
                   1134:        }
1.97      njoly    1135: 
                   1136:        IFNET_FOREACH(ifp) {
                   1137:                (void)strncpy(ifr.ifr_name, ifp->if_xname,
                   1138:                    sizeof(ifr.ifr_name));
                   1139:                if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0')
                   1140:                        return ENAMETOOLONG;
                   1141:                if (IFADDR_EMPTY(ifp))
                   1142:                        continue;
                   1143:                IFADDR_FOREACH(ifa, ifp) {
                   1144:                        sa = ifa->ifa_addr;
                   1145:                        if (sa->sa_family != AF_INET ||
                   1146:                            sa->sa_len > sizeof(*osa))
                   1147:                                continue;
                   1148:                        memcpy(&ifr.ifr_addr, sa, sa->sa_len);
                   1149:                        osa = (struct osockaddr *)&ifr.ifr_addr;
                   1150:                        osa->sa_family = sa->sa_family;
                   1151:                        if (space >= sz) {
                   1152:                                error = copyout(&ifr, ifrp, sz);
                   1153:                                if (error != 0)
                   1154:                                        return error;
                   1155:                                ifrp++;
                   1156:                        }
                   1157:                        space -= sz;
                   1158:                }
                   1159:        }
                   1160: 
1.122     ozaki-r  1161:        if (docopy)
1.115     christos 1162:                ifc.ifc_len -= space;
1.97      njoly    1163:        else
1.115     christos 1164:                ifc.ifc_len = -space;
1.97      njoly    1165: 
1.115     christos 1166:        return copyout(&ifc, data, sizeof(ifc));
1.97      njoly    1167: }
1.21      jtk      1168: 
                   1169: int
1.68      christos 1170: linux_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
1.67      christos 1171:     void *data)
1.21      jtk      1172: {
                   1173:        /* Not the full structure, just enough to map what we do here */
1.97      njoly    1174:        struct linux_ifreq lreq;
1.87      ad       1175:        file_t *fp;
1.21      jtk      1176:        struct ifaddr *ifa;
                   1177:        struct ifnet *ifp;
                   1178:        struct sockaddr_dl *sadl;
                   1179:        int error, found;
                   1180:        int index, ifnum;
                   1181: 
                   1182:        /*
                   1183:         * We can't emulate this ioctl by calling sys_ioctl() to run
                   1184:         * SIOCGIFCONF, because the user buffer is not of the right
                   1185:         * type to take those results.  We can't use kernel buffers to
                   1186:         * receive the results, as the implementation of sys_ioctl()
                   1187:         * and ifconf() [which implements SIOCGIFCONF] use
                   1188:         * copyin()/copyout() which will fail on kernel addresses.
                   1189:         *
                   1190:         * So, we must duplicate code from sys_ioctl() and ifconf().  Ugh.
                   1191:         */
                   1192: 
1.87      ad       1193:        if ((fp = fd_getfile(fd)) == NULL)
1.21      jtk      1194:                return (EBADF);
                   1195: 
1.90      ad       1196:        KERNEL_LOCK(1, NULL);
                   1197: 
1.21      jtk      1198:        if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
                   1199:                error = EBADF;
                   1200:                goto out;
                   1201:        }
                   1202: 
1.73      dsl      1203:        error = copyin(data, &lreq, sizeof(lreq));
1.21      jtk      1204:        if (error)
                   1205:                goto out;
1.97      njoly    1206:        lreq.ifr_name[LINUX_IFNAMSIZ-1] = '\0';         /* just in case */
1.21      jtk      1207: 
                   1208:        /*
1.22      jdolecek 1209:         * Try real interface name first, then fake "ethX"
1.21      jtk      1210:         */
1.82      dyoung   1211:        found = 0;
                   1212:        IFNET_FOREACH(ifp) {
                   1213:                if (found)
                   1214:                        break;
1.97      njoly    1215:                if (strcmp(lreq.ifr_name, ifp->if_xname))
1.22      jdolecek 1216:                        /* not this interface */
                   1217:                        continue;
1.55      perry    1218:                found=1;
1.83      dyoung   1219:                if (IFADDR_EMPTY(ifp)) {
1.22      jdolecek 1220:                        error = ENODEV;
                   1221:                        goto out;
                   1222:                }
1.82      dyoung   1223:                IFADDR_FOREACH(ifa, ifp) {
                   1224:                        sadl = satosdl(ifa->ifa_addr);
                   1225:                        /* only return ethernet addresses */
                   1226:                        /* XXX what about FDDI, etc. ? */
                   1227:                        if (sadl->sdl_family != AF_LINK ||
                   1228:                            sadl->sdl_type != IFT_ETHER)
                   1229:                                continue;
1.97      njoly    1230:                        memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl),
1.82      dyoung   1231:                               MIN(sadl->sdl_alen,
1.97      njoly    1232:                                   sizeof(lreq.ifr_hwaddr.sa_data)));
                   1233:                        lreq.ifr_hwaddr.sa_family =
1.82      dyoung   1234:                                sadl->sdl_family;
                   1235:                        error = copyout(&lreq, data, sizeof(lreq));
                   1236:                        goto out;
                   1237:                }
1.22      jdolecek 1238:        }
                   1239: 
1.121     ozaki-r  1240:        if (strncmp(lreq.ifr_name, "eth", 3) != 0) {
                   1241:                /* unknown interface, not even an "eth*" name */
                   1242:                error = ENODEV;
                   1243:                goto out;
                   1244:        }
                   1245: 
                   1246:        for (ifnum = 0, index = 3;
                   1247:             index < LINUX_IFNAMSIZ && lreq.ifr_name[index] != '\0';
                   1248:             index++) {
                   1249:                ifnum *= 10;
                   1250:                ifnum += lreq.ifr_name[index] - '0';
                   1251:        }
1.21      jtk      1252: 
1.121     ozaki-r  1253:        error = EINVAL;                 /* in case we don't find one */
                   1254:        found = 0;
                   1255:        IFNET_FOREACH(ifp) {
                   1256:                if (found)
                   1257:                        break;
                   1258:                memcpy(lreq.ifr_name, ifp->if_xname,
                   1259:                       MIN(LINUX_IFNAMSIZ, IFNAMSIZ));
                   1260:                IFADDR_FOREACH(ifa, ifp) {
                   1261:                        sadl = satosdl(ifa->ifa_addr);
                   1262:                        /* only return ethernet addresses */
                   1263:                        /* XXX what about FDDI, etc. ? */
                   1264:                        if (sadl->sdl_family != AF_LINK ||
                   1265:                            sadl->sdl_type != IFT_ETHER)
                   1266:                                continue;
                   1267:                        if (ifnum--)
                   1268:                                /* not the reqested iface */
                   1269:                                continue;
                   1270:                        memcpy(&lreq.ifr_hwaddr.sa_data,
                   1271:                               CLLADDR(sadl),
                   1272:                               MIN(sadl->sdl_alen,
                   1273:                                   sizeof(lreq.ifr_hwaddr.sa_data)));
                   1274:                        lreq.ifr_hwaddr.sa_family =
                   1275:                                sadl->sdl_family;
                   1276:                        error = copyout(&lreq, data, sizeof(lreq));
                   1277:                        found = 1;
                   1278:                        break;
1.21      jtk      1279:                }
1.22      jdolecek 1280:        }
1.55      perry    1281: 
1.21      jtk      1282: out:
1.90      ad       1283:        KERNEL_UNLOCK_ONE(NULL);
1.87      ad       1284:        fd_putfile(fd);
1.21      jtk      1285:        return error;
                   1286: }
                   1287: 
1.13      mycroft  1288: int
1.85      dsl      1289: linux_ioctl_socket(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
                   1290: {
                   1291:        /* {
1.13      mycroft  1292:                syscallarg(int) fd;
                   1293:                syscallarg(u_long) com;
1.71      christos 1294:                syscallarg(void *) data;
1.85      dsl      1295:        } */
1.13      mycroft  1296:        u_long com;
1.25      fvdl     1297:        int error = 0, isdev = 0, dosys = 1;
1.13      mycroft  1298:        struct sys_ioctl_args ia;
1.87      ad       1299:        file_t *fp;
1.25      fvdl     1300:        struct vnode *vp;
1.87      ad       1301:        int (*ioctlf)(file_t *, u_long, void *);
1.25      fvdl     1302:        struct ioctl_pt pt;
                   1303: 
1.87      ad       1304:        if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
1.25      fvdl     1305:                return (EBADF);
                   1306: 
                   1307:        if (fp->f_type == DTYPE_VNODE) {
                   1308:                vp = (struct vnode *)fp->f_data;
                   1309:                isdev = vp->v_type == VCHR;
                   1310:        }
                   1311: 
                   1312:        /*
                   1313:         * Don't try to interpret socket ioctl calls that are done
                   1314:         * on a device filedescriptor, just pass them through, to
                   1315:         * emulate Linux behaviour. Use PTIOCLINUX so that the
                   1316:         * device will only handle these if it's prepared to do
                   1317:         * so, to avoid unexpected things from happening.
                   1318:         */
                   1319:        if (isdev) {
1.26      fvdl     1320:                dosys = 0;
1.25      fvdl     1321:                ioctlf = fp->f_ops->fo_ioctl;
                   1322:                pt.com = SCARG(uap, com);
                   1323:                pt.data = SCARG(uap, data);
1.87      ad       1324:                error = ioctlf(fp, PTIOCLINUX, &pt);
1.27      fvdl     1325:                /*
1.55      perry    1326:                 * XXX hack: if the function returns EJUSTRETURN,
1.27      fvdl     1327:                 * it has stuffed a sysctl return value in pt.data.
                   1328:                 */
                   1329:                if (error == EJUSTRETURN) {
                   1330:                        retval[0] = (register_t)pt.data;
                   1331:                        error = 0;
                   1332:                }
1.25      fvdl     1333:                goto out;
                   1334:        }
1.13      mycroft  1335: 
                   1336:        com = SCARG(uap, com);
                   1337:        retval[0] = 0;
                   1338: 
                   1339:        switch (com) {
1.105     joerg    1340:        case LINUX_SIOCGIFNAME:
                   1341:                error = linux_getifname(l, retval, SCARG(uap, data));
                   1342:                dosys = 0;
                   1343:                break;
1.13      mycroft  1344:        case LINUX_SIOCGIFCONF:
1.97      njoly    1345:                error = linux_getifconf(l, retval, SCARG(uap, data));
                   1346:                dosys = 0;
1.13      mycroft  1347:                break;
                   1348:        case LINUX_SIOCGIFFLAGS:
1.72      christos 1349:                SCARG(&ia, com) = OSIOCGIFFLAGS;
1.13      mycroft  1350:                break;
1.25      fvdl     1351:        case LINUX_SIOCSIFFLAGS:
1.72      christos 1352:                SCARG(&ia, com) = OSIOCSIFFLAGS;
1.25      fvdl     1353:                break;
1.13      mycroft  1354:        case LINUX_SIOCGIFADDR:
1.72      christos 1355:                SCARG(&ia, com) = OOSIOCGIFADDR;
1.13      mycroft  1356:                break;
                   1357:        case LINUX_SIOCGIFDSTADDR:
1.72      christos 1358:                SCARG(&ia, com) = OOSIOCGIFDSTADDR;
1.13      mycroft  1359:                break;
                   1360:        case LINUX_SIOCGIFBRDADDR:
1.72      christos 1361:                SCARG(&ia, com) = OOSIOCGIFBRDADDR;
1.13      mycroft  1362:                break;
                   1363:        case LINUX_SIOCGIFNETMASK:
1.72      christos 1364:                SCARG(&ia, com) = OOSIOCGIFNETMASK;
1.13      mycroft  1365:                break;
1.107     dsl      1366:        case LINUX_SIOCGIFMTU:
                   1367:                SCARG(&ia, com) = OSIOCGIFMTU;
                   1368:                break;
1.13      mycroft  1369:        case LINUX_SIOCADDMULTI:
1.72      christos 1370:                SCARG(&ia, com) = OSIOCADDMULTI;
1.13      mycroft  1371:                break;
                   1372:        case LINUX_SIOCDELMULTI:
1.72      christos 1373:                SCARG(&ia, com) = OSIOCDELMULTI;
1.13      mycroft  1374:                break;
1.21      jtk      1375:        case LINUX_SIOCGIFHWADDR:
1.73      dsl      1376:                error = linux_getifhwaddr(l, retval, SCARG(uap, fd),
1.87      ad       1377:                    SCARG(uap, data));
1.29      fvdl     1378:                dosys = 0;
1.25      fvdl     1379:                break;
1.13      mycroft  1380:        default:
1.25      fvdl     1381:                error = EINVAL;
                   1382:        }
                   1383: 
1.87      ad       1384:  out:
                   1385:        fd_putfile(SCARG(uap, fd));
1.25      fvdl     1386: 
                   1387:        if (error ==0 && dosys) {
                   1388:                SCARG(&ia, fd) = SCARG(uap, fd);
                   1389:                SCARG(&ia, data) = SCARG(uap, data);
1.43      thorpej  1390:                error = sys_ioctl(curlwp, &ia, retval);
1.13      mycroft  1391:        }
                   1392: 
1.25      fvdl     1393:        return error;
1.1       fvdl     1394: }
1.19      sommerfe 1395: 
                   1396: int
1.85      dsl      1397: linux_sys_connect(struct lwp *l, const struct linux_sys_connect_args *uap, register_t *retval)
1.19      sommerfe 1398: {
1.85      dsl      1399:        /* {
1.19      sommerfe 1400:                syscallarg(int) s;
                   1401:                syscallarg(const struct sockaddr *) name;
1.32      jdolecek 1402:                syscallarg(int) namelen;
1.85      dsl      1403:        } */
1.32      jdolecek 1404:        int             error;
1.124   ! rtr      1405:        struct sockaddr_big sb;
1.73      dsl      1406: 
1.124   ! rtr      1407:        error = linux_get_sa_sb(l, SCARG(uap, s), &sb, SCARG(uap, name),
1.73      dsl      1408:            SCARG(uap, namelen));
1.32      jdolecek 1409:        if (error)
                   1410:                return (error);
1.55      perry    1411: 
1.124   ! rtr      1412:        error = do_sys_connect(l, SCARG(uap, s), (struct sockaddr *)&sb);
1.19      sommerfe 1413: 
                   1414:        if (error == EISCONN) {
1.23      augustss 1415:                struct socket *so;
1.111     christos 1416:                int state, prflags;
1.55      perry    1417: 
1.96      ad       1418:                /* fd_getsock() will use the descriptor for us */
1.89      ad       1419:                if (fd_getsock(SCARG(uap, s), &so) != 0)
1.19      sommerfe 1420:                        return EISCONN;
                   1421: 
1.91      ad       1422:                solock(so);
1.19      sommerfe 1423:                state = so->so_state;
                   1424:                prflags = so->so_proto->pr_flags;
1.91      ad       1425:                sounlock(so);
1.87      ad       1426:                fd_putfile(SCARG(uap, s));
1.19      sommerfe 1427:                /*
                   1428:                 * We should only let this call succeed once per
                   1429:                 * non-blocking connect; however we don't have
                   1430:                 * a convenient place to keep that state..
                   1431:                 */
1.111     christos 1432:                if ((state & (SS_ISCONNECTED|SS_NBIO)) ==
                   1433:                    (SS_ISCONNECTED|SS_NBIO) &&
1.19      sommerfe 1434:                    (prflags & PR_CONNREQUIRED))
                   1435:                        return 0;
                   1436:        }
                   1437: 
1.32      jdolecek 1438:        return (error);
                   1439: }
                   1440: 
                   1441: int
1.85      dsl      1442: linux_sys_bind(struct lwp *l, const struct linux_sys_bind_args *uap, register_t *retval)
1.32      jdolecek 1443: {
1.85      dsl      1444:        /* {
1.32      jdolecek 1445:                syscallarg(int) s;
                   1446:                syscallarg(const struct osockaddr *) name;
                   1447:                syscallarg(int) namelen;
1.85      dsl      1448:        } */
1.61      mrg      1449:        int             error;
1.123     rtr      1450:        struct sockaddr_big sb;
1.32      jdolecek 1451: 
1.123     rtr      1452:        error = linux_get_sa_sb(l, SCARG(uap, s), &sb, SCARG(uap, name),
1.73      dsl      1453:            SCARG(uap, namelen));
                   1454:        if (error)
                   1455:                return (error);
1.32      jdolecek 1456: 
1.123     rtr      1457:        return do_sys_bind(l, SCARG(uap, s), (struct sockaddr *)&sb);
1.32      jdolecek 1458: }
                   1459: 
                   1460: int
1.85      dsl      1461: linux_sys_getsockname(struct lwp *l, const struct linux_sys_getsockname_args *uap, register_t *retval)
1.32      jdolecek 1462: {
1.85      dsl      1463:        /* {
1.32      jdolecek 1464:                syscallarg(int) fdes;
1.71      christos 1465:                syscallarg(void *) asa;
1.32      jdolecek 1466:                syscallarg(int *) alen;
1.85      dsl      1467:        } */
1.32      jdolecek 1468:        int error;
                   1469: 
1.85      dsl      1470:        if ((error = sys_getsockname(l, (const void *)uap, retval)) != 0)
1.32      jdolecek 1471:                return (error);
                   1472: 
                   1473:        if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
                   1474:                return (error);
                   1475: 
                   1476:        return (0);
                   1477: }
                   1478: 
                   1479: int
1.85      dsl      1480: linux_sys_getpeername(struct lwp *l, const struct linux_sys_getpeername_args *uap, register_t *retval)
1.32      jdolecek 1481: {
1.85      dsl      1482:        /* {
1.32      jdolecek 1483:                syscallarg(int) fdes;
1.71      christos 1484:                syscallarg(void *) asa;
1.32      jdolecek 1485:                syscallarg(int *) alen;
1.85      dsl      1486:        } */
1.32      jdolecek 1487:        int error;
                   1488: 
1.85      dsl      1489:        if ((error = sys_getpeername(l, (const void *)uap, retval)) != 0)
1.32      jdolecek 1490:                return (error);
                   1491: 
                   1492:        if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
                   1493:                return (error);
                   1494: 
                   1495:        return (0);
                   1496: }
                   1497: 
1.123     rtr      1498: static int
                   1499: linux_get_sa_sb(struct lwp *l, int s, struct sockaddr_big *sb,
                   1500:     const struct osockaddr *name, socklen_t namelen)
                   1501: {
                   1502:        int error, bdom;
                   1503: 
                   1504:        if (namelen > UCHAR_MAX ||
                   1505:            namelen <= offsetof(struct sockaddr_big, sb_data))
                   1506:                return EINVAL;
                   1507: 
                   1508:        error = copyin(name, sb, namelen);
                   1509:        if (error)
                   1510:                return error;
                   1511: 
                   1512:        bdom = linux_to_bsd_domain(sb->sb_family);
                   1513:        if (bdom == -1)
                   1514:                return EINVAL;
                   1515: 
                   1516:        /*
                   1517:         * If the family is unspecified, use address family of the socket.
                   1518:         * This avoid triggering strict family checks in netinet/in_pcb.c et.al.
                   1519:         */
                   1520:        if (bdom == AF_UNSPEC) {
                   1521:                struct socket *so;
                   1522: 
                   1523:                /* fd_getsock() will use the descriptor for us */
                   1524:                if ((error = fd_getsock(s, &so)) != 0)
                   1525:                        return error;
                   1526: 
                   1527:                bdom = so->so_proto->pr_domain->dom_family;
                   1528:                fd_putfile(s);
                   1529:        }
                   1530: 
                   1531:        /*
                   1532:         * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
                   1533:         * which lacks the scope id compared with RFC2553 one. If we detect
                   1534:         * the situation, reject the address and write a message to system log.
                   1535:         *
                   1536:         * Still accept addresses for which the scope id is not used.
                   1537:         */
                   1538:        if (bdom == AF_INET6 &&
                   1539:            namelen == sizeof(struct sockaddr_in6) - sizeof(uint32_t)) {
                   1540:                struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sb;
                   1541:                if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) &&
                   1542:                    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
                   1543:                     IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) ||
                   1544:                     IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) ||
                   1545:                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
                   1546:                     IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
                   1547:                        struct proc *p = l->l_proc;
                   1548:                        int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1;
                   1549: 
                   1550:                        log(LOG_DEBUG,
                   1551:                            "pid %d (%s), uid %d: obsolete pre-RFC2553 "
                   1552:                            "sockaddr_in6 rejected",
                   1553:                            p->p_pid, p->p_comm, uid);
                   1554:                        return EINVAL;
                   1555:                }
                   1556:                namelen = sizeof(struct sockaddr_in6);
                   1557:                sin6->sin6_scope_id = 0;
                   1558:        }
                   1559: 
                   1560:        if (bdom == AF_INET)
                   1561:                namelen = sizeof(struct sockaddr_in);
                   1562: 
                   1563:        sb->sb_family = bdom;
                   1564:        sb->sb_len = namelen;
                   1565:        ktrkuser("mbsoname", sb, namelen);
                   1566:        return 0;
                   1567: }
                   1568: 
1.32      jdolecek 1569: /*
1.73      dsl      1570:  * Copy the osockaddr structure pointed to by osa to mbuf, adjust
                   1571:  * family and convert to sockaddr.
1.32      jdolecek 1572:  */
                   1573: static int
1.93      njoly    1574: linux_get_sa(struct lwp *l, int s, struct mbuf **mp,
                   1575:     const struct osockaddr *osa, unsigned int salen)
1.73      dsl      1576: {
                   1577:        int error, bdom;
                   1578:        struct sockaddr *sa;
                   1579:        struct osockaddr *kosa;
                   1580:        struct mbuf *m;
1.32      jdolecek 1581: 
1.73      dsl      1582:        if (salen == 1 || salen > UCHAR_MAX) {
                   1583:                DPRINTF(("bad osa=%p salen=%d\n", osa, salen));
                   1584:                return EINVAL;
1.34      fvdl     1585:        }
1.32      jdolecek 1586: 
1.73      dsl      1587:        /* We'll need the address in an mbuf later, so copy into one here */
                   1588:        m = m_get(M_WAIT, MT_SONAME);
                   1589:        if (salen > MLEN)
                   1590:                MEXTMALLOC(m, salen, M_WAITOK);
                   1591: 
                   1592:        m->m_len = salen;
                   1593: 
1.94      njoly    1594:        if (salen == 0) {
                   1595:                *mp = m;
1.73      dsl      1596:                return 0;
1.94      njoly    1597:        }
1.73      dsl      1598: 
                   1599:        kosa = mtod(m, void *);
                   1600:        if ((error = copyin(osa, kosa, salen))) {
                   1601:                DPRINTF(("error %d copying osa %p len %d\n",
                   1602:                                error, osa, salen));
                   1603:                goto bad;
1.37      christos 1604:        }
1.77      ad       1605: 
1.114     christos 1606:        ktrkuser("linux/sockaddr", kosa, salen);
1.32      jdolecek 1607: 
                   1608:        bdom = linux_to_bsd_domain(kosa->sa_family);
                   1609:        if (bdom == -1) {
1.37      christos 1610:                DPRINTF(("bad linux family=%d\n", kosa->sa_family));
1.32      jdolecek 1611:                error = EINVAL;
1.73      dsl      1612:                goto bad;
                   1613:        }
                   1614: 
                   1615:        /*
                   1616:         * If the family is unspecified, use address family of the socket.
                   1617:         * This avoid triggering strict family checks in netinet/in_pcb.c et.al.
                   1618:         */
                   1619:        if (bdom == AF_UNSPEC) {
                   1620:                struct socket *so;
                   1621: 
1.96      ad       1622:                /* fd_getsock() will use the descriptor for us */
1.88      ad       1623:                if ((error = fd_getsock(s, &so)) != 0)
1.73      dsl      1624:                        goto bad;
                   1625: 
                   1626:                bdom = so->so_proto->pr_domain->dom_family;
1.88      ad       1627:                fd_putfile(s);
1.73      dsl      1628: 
                   1629:                DPRINTF(("AF_UNSPEC family adjusted to %d\n", bdom));
1.32      jdolecek 1630:        }
                   1631: 
                   1632:        /*
                   1633:         * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
                   1634:         * which lacks the scope id compared with RFC2553 one. If we detect
                   1635:         * the situation, reject the address and write a message to system log.
1.34      fvdl     1636:         *
                   1637:         * Still accept addresses for which the scope id is not used.
1.32      jdolecek 1638:         */
1.73      dsl      1639:        if (bdom == AF_INET6 && salen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
                   1640:                struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)kosa;
                   1641:                if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) &&
                   1642:                    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
                   1643:                     IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) ||
                   1644:                     IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) ||
                   1645:                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
                   1646:                     IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
                   1647:                        struct proc *p = l->l_proc;
1.65      ad       1648:                        int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1;
1.34      fvdl     1649: 
                   1650:                        log(LOG_DEBUG,
                   1651:                            "pid %d (%s), uid %d: obsolete pre-RFC2553 "
                   1652:                            "sockaddr_in6 rejected",
                   1653:                            p->p_pid, p->p_comm, uid);
                   1654:                        error = EINVAL;
1.73      dsl      1655:                        goto bad;
1.34      fvdl     1656:                }
1.73      dsl      1657:                salen = sizeof (struct sockaddr_in6);
                   1658:                sin6->sin6_scope_id = 0;
1.53      jdolecek 1659:        }
                   1660: 
1.73      dsl      1661:        if (bdom == AF_INET)
                   1662:                salen = sizeof(struct sockaddr_in);
1.32      jdolecek 1663: 
                   1664:        sa = (struct sockaddr *) kosa;
                   1665:        sa->sa_family = bdom;
1.73      dsl      1666:        sa->sa_len = salen;
                   1667:        m->m_len = salen;
1.114     christos 1668:        ktrkuser("mbsoname", kosa, salen);
1.73      dsl      1669: 
1.37      christos 1670: #ifdef DEBUG_LINUX
                   1671:        DPRINTF(("family %d, len = %d [ ", sa->sa_family, sa->sa_len));
                   1672:        for (bdom = 0; bdom < sizeof(sa->sa_data); bdom++)
1.51      jdolecek 1673:            DPRINTF(("%02x ", (unsigned char) sa->sa_data[bdom]));
1.37      christos 1674:        DPRINTF(("\n"));
                   1675: #endif
1.32      jdolecek 1676: 
1.73      dsl      1677:        *mp = m;
                   1678:        return 0;
1.32      jdolecek 1679: 
1.73      dsl      1680:     bad:
                   1681:        m_free(m);
                   1682:        return error;
1.32      jdolecek 1683: }
                   1684: 
                   1685: static int
1.84      dsl      1686: linux_sa_put(struct osockaddr *osa)
1.32      jdolecek 1687: {
                   1688:        struct sockaddr sa;
                   1689:        struct osockaddr *kosa;
                   1690:        int error, bdom, len;
                   1691: 
                   1692:        /*
                   1693:         * Only read/write the sockaddr family and length part, the rest is
                   1694:         * not changed.
                   1695:         */
                   1696:        len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
                   1697: 
1.73      dsl      1698:        error = copyin(osa, &sa, len);
1.32      jdolecek 1699:        if (error)
                   1700:                return (error);
                   1701: 
                   1702:        bdom = bsd_to_linux_domain(sa.sa_family);
                   1703:        if (bdom == -1)
                   1704:                return (EINVAL);
                   1705: 
                   1706:        /* Note: we convert from sockaddr to osockaddr here, too */
                   1707:        kosa = (struct osockaddr *) &sa;
                   1708:        kosa->sa_family = bdom;
                   1709:        error = copyout(kosa, osa, len);
                   1710:        if (error)
                   1711:                return (error);
                   1712: 
                   1713:        return (0);
                   1714: }
                   1715: 
1.56      manu     1716: #ifndef __amd64__
1.32      jdolecek 1717: int
1.85      dsl      1718: linux_sys_recv(struct lwp *l, const struct linux_sys_recv_args *uap, register_t *retval)
1.32      jdolecek 1719: {
1.85      dsl      1720:        /* {
1.32      jdolecek 1721:                syscallarg(int) s;
                   1722:                syscallarg(void *) buf;
                   1723:                syscallarg(int) len;
                   1724:                syscallarg(int) flags;
1.85      dsl      1725:        } */
1.32      jdolecek 1726:        struct sys_recvfrom_args bra;
                   1727: 
                   1728: 
                   1729:        SCARG(&bra, s) = SCARG(uap, s);
                   1730:        SCARG(&bra, buf) = SCARG(uap, buf);
                   1731:        SCARG(&bra, len) = (size_t) SCARG(uap, len);
                   1732:        SCARG(&bra, flags) = SCARG(uap, flags);
                   1733:        SCARG(&bra, from) = NULL;
                   1734:        SCARG(&bra, fromlenaddr) = NULL;
                   1735: 
1.43      thorpej  1736:        return (sys_recvfrom(l, &bra, retval));
1.32      jdolecek 1737: }
                   1738: 
                   1739: int
1.85      dsl      1740: linux_sys_send(struct lwp *l, const struct linux_sys_send_args *uap, register_t *retval)
1.32      jdolecek 1741: {
1.85      dsl      1742:        /* {
1.32      jdolecek 1743:                syscallarg(int) s;
1.71      christos 1744:                syscallarg(void *) buf;
1.32      jdolecek 1745:                syscallarg(int) len;
                   1746:                syscallarg(int) flags;
1.85      dsl      1747:        } */
1.32      jdolecek 1748:        struct sys_sendto_args bsa;
                   1749: 
                   1750:        SCARG(&bsa, s)          = SCARG(uap, s);
                   1751:        SCARG(&bsa, buf)        = SCARG(uap, buf);
                   1752:        SCARG(&bsa, len)        = SCARG(uap, len);
                   1753:        SCARG(&bsa, flags)      = SCARG(uap, flags);
                   1754:        SCARG(&bsa, to)         = NULL;
                   1755:        SCARG(&bsa, tolen)      = 0;
                   1756: 
1.43      thorpej  1757:        return (sys_sendto(l, &bsa, retval));
1.32      jdolecek 1758: }
1.59      manu     1759: #endif
1.32      jdolecek 1760: 
                   1761: int
1.85      dsl      1762: linux_sys_accept(struct lwp *l, const struct linux_sys_accept_args *uap, register_t *retval)
1.32      jdolecek 1763: {
1.85      dsl      1764:        /* {
1.32      jdolecek 1765:                syscallarg(int) s;
                   1766:                syscallarg(struct osockaddr *) name;
                   1767:                syscallarg(int *) anamelen;
1.85      dsl      1768:        } */
1.32      jdolecek 1769:        int error;
                   1770:        struct sys_accept_args baa;
                   1771: 
                   1772:        SCARG(&baa, s)          = SCARG(uap, s);
                   1773:        SCARG(&baa, name)       = (struct sockaddr *) SCARG(uap, name);
                   1774:        SCARG(&baa, anamelen)   = (unsigned int *) SCARG(uap, anamelen);
                   1775: 
1.43      thorpej  1776:        if ((error = sys_accept(l, &baa, retval)))
1.32      jdolecek 1777:                return (error);
                   1778: 
                   1779:        if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
                   1780:                return (error);
                   1781: 
                   1782:        return (0);
1.19      sommerfe 1783: }

CVSweb <webmaster@jp.NetBSD.org>