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

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

CVSweb <webmaster@jp.NetBSD.org>