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

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

CVSweb <webmaster@jp.NetBSD.org>