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

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

CVSweb <webmaster@jp.NetBSD.org>