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

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

CVSweb <webmaster@jp.NetBSD.org>