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

1.41    ! itojun      1: /*     $NetBSD: linux_socket.c,v 1.40 2002/11/28 23:46:15 itojun Exp $ */
1.16      erh         2: 
                      3: /*-
1.18      fvdl        4:  * Copyright (c) 1995, 1998 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:  * 3. All advertising materials mentioning features or use of this software
                     19:  *    must display the following acknowledgement:
                     20:  *     This product includes software developed by the NetBSD
                     21:  *     Foundation, Inc. and its contributors.
                     22:  * 4. Neither the name of The NetBSD Foundation nor the names of its
                     23:  *    contributors may be used to endorse or promote products derived
                     24:  *    from this software without specific prior written permission.
                     25:  *
                     26:  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
                     27:  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
                     28:  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
                     29:  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
                     30:  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
                     31:  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
                     32:  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
                     33:  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
                     34:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
                     35:  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
                     36:  * POSSIBILITY OF SUCH DAMAGE.
1.1       fvdl       37:  */
                     38: 
1.16      erh        39: /*
                     40:  * Functions in multiarch:
                     41:  *     linux_sys_socketcall            : linux_socketcall.c
1.32      jdolecek   42:  *
                     43:  * XXX Note: Linux CMSG_ALIGN() uses (sizeof(long)-1). For architectures
                     44:  * where our CMSG_ALIGN() differs (like powerpc, sparc, sparc64), the passed
                     45:  * control structure would need to be adjusted accordingly in sendmsg() and
                     46:  * recvmsg().
1.16      erh        47:  */
1.35      lukem      48: 
                     49: #include <sys/cdefs.h>
1.41    ! itojun     50: __KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.40 2002/11/28 23:46:15 itojun Exp $");
1.16      erh        51: 
1.32      jdolecek   52: #if defined(_KERNEL_OPT)
                     53: #include "opt_inet.h"
                     54: #endif
                     55: 
1.1       fvdl       56: #include <sys/param.h>
                     57: #include <sys/kernel.h>
                     58: #include <sys/systm.h>
                     59: #include <sys/buf.h>
                     60: #include <sys/malloc.h>
                     61: #include <sys/ioctl.h>
                     62: #include <sys/tty.h>
                     63: #include <sys/file.h>
                     64: #include <sys/filedesc.h>
                     65: #include <sys/select.h>
                     66: #include <sys/socket.h>
                     67: #include <sys/socketvar.h>
                     68: #include <net/if.h>
1.21      jtk        69: #include <net/if_dl.h>
                     70: #include <net/if_types.h>
1.1       fvdl       71: #include <netinet/in.h>
1.7       mycroft    72: #include <netinet/tcp.h>
1.1       fvdl       73: #include <sys/mount.h>
                     74: #include <sys/proc.h>
                     75: #include <sys/vnode.h>
                     76: #include <sys/device.h>
1.19      sommerfe   77: #include <sys/protosw.h> 
1.32      jdolecek   78: #include <sys/mbuf.h>
                     79: #include <sys/syslog.h>
1.1       fvdl       80: 
                     81: #include <sys/syscallargs.h>
                     82: 
1.17      christos   83: #include <compat/linux/common/linux_types.h>
                     84: #include <compat/linux/common/linux_util.h>
                     85: #include <compat/linux/common/linux_signal.h>
                     86: #include <compat/linux/common/linux_ioctl.h>
                     87: #include <compat/linux/common/linux_socket.h>
                     88: #include <compat/linux/common/linux_socketcall.h>
                     89: #include <compat/linux/common/linux_sockio.h>
                     90: 
1.1       fvdl       91: #include <compat/linux/linux_syscallargs.h>
1.21      jtk        92: 
1.37      christos   93: #ifdef DEBUG_LINUX
                     94: #define DPRINTF(a) uprintf a
                     95: #else
                     96: #define DPRINTF(a)
                     97: #endif
                     98: 
1.1       fvdl       99: /*
1.16      erh       100:  * The calls in this file are entered either via the linux_socketcall()
                    101:  * interface or, on the Alpha, as individual syscalls.  The
                    102:  * linux_socketcall function does any massaging of arguments so that all
                    103:  * the calls in here need not think that they are anything other
                    104:  * than a normal syscall.
1.1       fvdl      105:  */
1.14      christos  106: 
1.32      jdolecek  107: static int linux_to_bsd_domain __P((int));
                    108: static int bsd_to_linux_domain __P((int));
1.14      christos  109: int linux_to_bsd_sopt_level __P((int));
                    110: int linux_to_bsd_so_sockopt __P((int));
                    111: int linux_to_bsd_ip_sockopt __P((int));
                    112: int linux_to_bsd_tcp_sockopt __P((int));
                    113: int linux_to_bsd_udp_sockopt __P((int));
1.21      jtk       114: int linux_getifhwaddr __P((struct proc *, register_t *, u_int, void *));
1.36      christos  115: static int linux_sa_get __P((struct proc *, caddr_t *sgp, struct sockaddr **sap,
1.34      fvdl      116:                const struct osockaddr *osa, int *osalen));
1.32      jdolecek  117: static int linux_sa_put __P((struct osockaddr *osa));
                    118: 
                    119: static const int linux_to_bsd_domain_[LINUX_AF_MAX] = {
                    120:        AF_UNSPEC,
                    121:        AF_UNIX,
                    122:        AF_INET,
                    123:        AF_CCITT,       /* LINUX_AF_AX25 */
                    124:        AF_IPX,
                    125:        AF_APPLETALK,
                    126:        -1,             /* LINUX_AF_NETROM */
                    127:        -1,             /* LINUX_AF_BRIDGE */
                    128:        -1,             /* LINUX_AF_ATMPVC */
                    129:        AF_CCITT,       /* LINUX_AF_X25 */
                    130:        AF_INET6,
                    131:        -1,             /* LINUX_AF_ROSE */
                    132:        AF_DECnet,
                    133:        -1,             /* LINUX_AF_NETBEUI */
                    134:        -1,             /* LINUX_AF_SECURITY */
                    135:        pseudo_AF_KEY,
                    136:        AF_ROUTE,       /* LINUX_AF_NETLINK */
                    137:        -1,             /* LINUX_AF_PACKET */
                    138:        -1,             /* LINUX_AF_ASH */
                    139:        -1,             /* LINUX_AF_ECONET */
                    140:        -1,             /* LINUX_AF_ATMSVC */
                    141:        AF_SNA,
                    142:        /* rest up to LINUX_AF_MAX-1 is not allocated */
                    143:        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
                    144: };
                    145:        
                    146: static const int bsd_to_linux_domain_[AF_MAX] = {
                    147:        LINUX_AF_UNSPEC,
                    148:        LINUX_AF_UNIX,
                    149:        LINUX_AF_INET,
                    150:        -1,             /* AF_IMPLINK */
                    151:        -1,             /* AF_PUP */
                    152:        -1,             /* AF_CHAOS */
                    153:        -1,             /* AF_NS */
                    154:        -1,             /* AF_ISO */
                    155:        -1,             /* AF_ECMA */
                    156:        -1,             /* AF_DATAKIT */
                    157:        LINUX_AF_AX25,  /* AF_CCITT */
                    158:        LINUX_AF_SNA,
                    159:        LINUX_AF_DECnet,
                    160:        -1,             /* AF_DLI */
                    161:        -1,             /* AF_LAT */
                    162:        -1,             /* AF_HYLINK */
                    163:        LINUX_AF_APPLETALK,
                    164:        LINUX_AF_NETLINK,
                    165:        -1,             /* AF_LINK */
                    166:        -1,             /* AF_XTP */
                    167:        -1,             /* AF_COIP */
                    168:        -1,             /* AF_CNT */
                    169:        -1,             /* pseudo_AF_RTIP */
                    170:        LINUX_AF_IPX,
                    171:        LINUX_AF_INET6,
                    172:        -1,             /* pseudo_AF_PIP */
                    173:        -1,             /* AF_ISDN */
                    174:        -1,             /* AF_NATM */
                    175:        -1,             /* AF_ARP */
                    176:        LINUX_pseudo_AF_KEY,
                    177:        -1,             /* pseudo_AF_HDRCMPLT */
                    178: };
1.1       fvdl      179: 
                    180: /*
                    181:  * Convert between Linux and BSD socket domain values
                    182:  */
1.32      jdolecek  183: static int
1.1       fvdl      184: linux_to_bsd_domain(ldom)
                    185:        int ldom;
                    186: {
1.32      jdolecek  187:        if (ldom < 0 || ldom >= LINUX_AF_MAX)
                    188:                return (-1);
                    189: 
                    190:        return linux_to_bsd_domain_[ldom];
                    191: }
                    192: 
                    193: /*
                    194:  * Convert between BSD and Linux socket domain values
                    195:  */
                    196: static int
                    197: bsd_to_linux_domain(bdom)
                    198:        int bdom;
                    199: {
                    200:        if (bdom < 0 || bdom >= AF_MAX)
                    201:                return (-1);
1.12      mycroft   202: 
1.32      jdolecek  203:        return bsd_to_linux_domain_[bdom];
1.1       fvdl      204: }
                    205: 
1.2       mycroft   206: int
1.16      erh       207: linux_sys_socket(p, v, retval)
1.1       fvdl      208:        struct proc *p;
1.16      erh       209:        void *v;
                    210:        register_t *retval;
                    211: {
                    212:        struct linux_sys_socket_args /* {
1.1       fvdl      213:                syscallarg(int) domain;
                    214:                syscallarg(int) type;
                    215:                syscallarg(int) protocol;
1.16      erh       216:        } */ *uap = v;
1.12      mycroft   217:        struct sys_socket_args bsa;
1.41    ! itojun    218:        int s;
1.1       fvdl      219: 
1.16      erh       220:        SCARG(&bsa, protocol) = SCARG(uap, protocol);
                    221:        SCARG(&bsa, type) = SCARG(uap, type);
                    222:        SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1       fvdl      223:        if (SCARG(&bsa, domain) == -1)
                    224:                return EINVAL;
1.41    ! itojun    225:        s = sys_socket(p, &bsa, retval);
        !           226: 
        !           227: #ifdef INET6
        !           228:        /*
        !           229:         * Linux AF_INET6 socket has IPV6_V6ONLY setsockopt set to 0 by default.
        !           230:         * XXX interaction with native sysctl net.inet6.ip6.v6only?
        !           231:         */
        !           232:        if (SCARG(&bsa, domain) == PF_INET6) {
        !           233:                struct sys_setsockopt_args bsoa;
        !           234:                struct sys_close_args bca;
        !           235:                caddr_t sg;
        !           236:                const int off = 0;
        !           237: 
        !           238:                SCARG(&bsoa, s) = s;
        !           239:                SCARG(&bsoa, level) = IPPROTO_IPV6;
        !           240:                SCARG(&bsoa, name) = IPV6_V6ONLY;
        !           241:                sg = stackgap_init(p, 0);
        !           242:                SCARG(&bsoa, val) = stackgap_alloc(p, &sg, sizeof(off));
        !           243:                SCARG(&bsoa, valsize) = sizeof(off);
        !           244:                /* LINTED const cast */
        !           245:                if (copyout(&off, (void *)SCARG(&bsoa, val), sizeof(off)) ||
        !           246:                    sys_setsockopt(p, &bsoa, retval)) {
        !           247:                        SCARG(&bca, fd) = s;
        !           248:                        sys_close(p, &bca, retval);
        !           249:                        *retval = -1;
        !           250:                        return (EINVAL);        /*XXX*/
        !           251:                }
        !           252:        }
        !           253: #endif
        !           254: 
        !           255:        return s;
1.1       fvdl      256: }
                    257: 
1.2       mycroft   258: int
1.16      erh       259: linux_sys_socketpair(p, v, retval)
1.1       fvdl      260:        struct proc *p;
1.16      erh       261:        void *v;
1.1       fvdl      262:        register_t *retval;
                    263: {
1.16      erh       264:        struct linux_sys_socketpair_args /* {
1.1       fvdl      265:                syscallarg(int) domain;
                    266:                syscallarg(int) type;
                    267:                syscallarg(int) protocol;
                    268:                syscallarg(int *) rsv;
1.16      erh       269:        } */ *uap = v;
1.12      mycroft   270:        struct sys_socketpair_args bsa;
1.1       fvdl      271: 
1.16      erh       272:        SCARG(&bsa, domain) = linux_to_bsd_domain(SCARG(uap, domain));
1.1       fvdl      273:        if (SCARG(&bsa, domain) == -1)
                    274:                return EINVAL;
1.16      erh       275:        SCARG(&bsa, type) = SCARG(uap, type);
                    276:        SCARG(&bsa, protocol) = SCARG(uap, protocol);
                    277:        SCARG(&bsa, rsv) = SCARG(uap, rsv);
1.1       fvdl      278: 
1.12      mycroft   279:        return sys_socketpair(p, &bsa, retval);
1.1       fvdl      280: }
                    281: 
1.2       mycroft   282: int
1.16      erh       283: linux_sys_sendto(p, v, retval)
1.1       fvdl      284:        struct proc *p;
1.16      erh       285:        void *v;
1.1       fvdl      286:        register_t *retval;
                    287: {
1.16      erh       288:        struct linux_sys_sendto_args /* {
1.32      jdolecek  289:                syscallarg(int)                         s;
                    290:                syscallarg(void *)                      msg;
                    291:                syscallarg(int)                         len;
                    292:                syscallarg(int)                         flags;
                    293:                syscallarg(struct osockaddr *)          to;
                    294:                syscallarg(int)                         tolen;
1.16      erh       295:        } */ *uap = v;
1.12      mycroft   296:        struct sys_sendto_args bsa;
1.34      fvdl      297:        int tolen;
1.1       fvdl      298: 
1.16      erh       299:        SCARG(&bsa, s) = SCARG(uap, s);
                    300:        SCARG(&bsa, buf) = SCARG(uap, msg);
1.32      jdolecek  301:        SCARG(&bsa, len) = (size_t) SCARG(uap, len);
1.16      erh       302:        SCARG(&bsa, flags) = SCARG(uap, flags);
1.34      fvdl      303:        tolen = SCARG(uap, tolen);
1.32      jdolecek  304:        if (SCARG(uap, to)) {
                    305:                struct sockaddr *sa;
                    306:                int error;
1.36      christos  307:                caddr_t sg = stackgap_init(p, 0);
1.32      jdolecek  308: 
1.36      christos  309:                if ((error = linux_sa_get(p, &sg, &sa, SCARG(uap, to), &tolen)))
1.32      jdolecek  310:                        return (error);
                    311: 
                    312:                SCARG(&bsa, to) = sa;
                    313:        } else
                    314:                SCARG(&bsa, to) = NULL;
1.34      fvdl      315:        SCARG(&bsa, tolen) = tolen;
1.1       fvdl      316: 
1.32      jdolecek  317:        return (sys_sendto(p, &bsa, retval));
                    318: }
                    319: 
                    320: int
                    321: linux_sys_sendmsg(p, v, retval)
                    322:        struct proc *p;
                    323:        void *v;
                    324:        register_t *retval;
                    325: {
                    326:        struct linux_sys_sendmsg_args /* {
                    327:                syscallarg(int) s;
                    328:                syscallarg(struct msghdr *) msg;
                    329:                syscallarg(u_int) flags;
                    330:        } */ *uap = v;
                    331:        struct msghdr   msg;
                    332:        int             error;
                    333:        struct sys_sendmsg_args bsa;
                    334:        struct msghdr *nmsg = NULL;
                    335: 
                    336:        error = copyin(SCARG(uap, msg), (caddr_t)&msg, sizeof(msg));
                    337:        if (error)
                    338:                return (error);
                    339: 
                    340:        if (msg.msg_name) {
                    341:                struct sockaddr *sa;
1.36      christos  342:                caddr_t sg = stackgap_init(p, 0);
1.32      jdolecek  343: 
1.36      christos  344:                nmsg = (struct msghdr *) stackgap_alloc(p, &sg,
1.32      jdolecek  345:                    sizeof(struct msghdr));
                    346:                if (!nmsg)
                    347:                        return (ENOMEM);
                    348: 
1.36      christos  349:                error = linux_sa_get(p, &sg, &sa,
1.34      fvdl      350:                    (struct osockaddr *) msg.msg_name, &msg.msg_namelen);
1.32      jdolecek  351:                if (error)
                    352:                        return (error);
                    353: 
                    354:                msg.msg_name = (struct sockaddr *) sa;
                    355:                if ((error = copyout(&msg, nmsg, sizeof(struct msghdr))))
                    356:                        return (error);
                    357:        }
                    358: 
                    359:        /*
                    360:         * XXX handle different alignment of cmsg data on architectures where
                    361:         * the Linux alignment is different (powerpc, sparc, sparc64).
                    362:         */
                    363: 
                    364:        SCARG(&bsa, s) = SCARG(uap, s);
                    365:        SCARG(&bsa, msg) = nmsg;
                    366:        SCARG(&bsa, flags) = SCARG(uap, flags);
                    367: 
                    368:        if ((error = sys_sendmsg(p, &bsa, retval)))
                    369:                return (error);
                    370: 
                    371:        return (0);
1.1       fvdl      372: }
                    373: 
1.32      jdolecek  374: 
1.2       mycroft   375: int
1.16      erh       376: linux_sys_recvfrom(p, v, retval)
1.1       fvdl      377:        struct proc *p;
1.16      erh       378:        void *v;
                    379:        register_t *retval;
                    380: {
                    381:        struct linux_sys_recvfrom_args /* {
1.1       fvdl      382:                syscallarg(int) s;
                    383:                syscallarg(void *) buf;
                    384:                syscallarg(int) len;
                    385:                syscallarg(int) flags;
1.32      jdolecek  386:                syscallarg(struct osockaddr *) from;
                    387:                syscallarg(int *) fromlenaddr;
1.16      erh       388:        } */ *uap = v;
1.32      jdolecek  389:        int             error;
                    390:        struct sys_recvfrom_args bra;
1.1       fvdl      391: 
1.16      erh       392:        SCARG(&bra, s) = SCARG(uap, s);
                    393:        SCARG(&bra, buf) = SCARG(uap, buf);
                    394:        SCARG(&bra, len) = SCARG(uap, len);
                    395:        SCARG(&bra, flags) = SCARG(uap, flags);
1.32      jdolecek  396:        SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from);
                    397:        SCARG(&bra, fromlenaddr) = SCARG(uap, fromlenaddr);
1.1       fvdl      398: 
1.32      jdolecek  399:        if ((error = sys_recvfrom(p, &bra, retval)))
                    400:                return (error);
                    401: 
                    402:        if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from))))
                    403:                return (error);
                    404: 
                    405:        return (0);
                    406: }
                    407: 
                    408: int
                    409: linux_sys_recvmsg(p, v, retval)
                    410:        struct proc *p;
                    411:        void *v;
                    412:        register_t *retval;
                    413: {
                    414:        struct linux_sys_recvmsg_args /* {
                    415:                syscallarg(int) s;
                    416:                syscallarg(struct msghdr *) msg;
                    417:                syscallarg(u_int) flags;
                    418:        } */ *uap = v;
                    419:        struct msghdr   msg;
                    420:        int             error;
                    421: 
                    422:        if ((error = sys_recvmsg(p, v, retval)))
                    423:                return (error);
                    424: 
                    425:        error = copyin((caddr_t)SCARG(uap, msg), (caddr_t)&msg,
                    426:                       sizeof(msg));
                    427: 
                    428:        if (!error && msg.msg_name && msg.msg_namelen > 2)
                    429:                error = linux_sa_put(msg.msg_name);
                    430:                
                    431:        /*
                    432:         * XXX handle different alignment of cmsg data on architectures where
                    433:         * the Linux alignment is different (powerpc, sparc, sparc64).
                    434:         */
                    435: 
                    436:        return (error);
1.1       fvdl      437: }
                    438: 
                    439: /*
                    440:  * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
                    441:  * is different, the rest matches IPPROTO_* on both systems.
                    442:  */
1.2       mycroft   443: int
1.1       fvdl      444: linux_to_bsd_sopt_level(llevel)
                    445:        int llevel;
                    446: {
1.12      mycroft   447: 
1.1       fvdl      448:        switch (llevel) {
                    449:        case LINUX_SOL_SOCKET:
                    450:                return SOL_SOCKET;
1.3       mycroft   451:        case LINUX_SOL_IP:
                    452:                return IPPROTO_IP;
                    453:        case LINUX_SOL_TCP:
                    454:                return IPPROTO_TCP;
                    455:        case LINUX_SOL_UDP:
                    456:                return IPPROTO_UDP;
1.1       fvdl      457:        default:
                    458:                return -1;
                    459:        }
                    460: }
                    461: 
                    462: /*
                    463:  * Convert Linux socket level socket option numbers to NetBSD values.
                    464:  */
1.2       mycroft   465: int
1.1       fvdl      466: linux_to_bsd_so_sockopt(lopt)
                    467:        int lopt;
                    468: {
1.12      mycroft   469: 
1.1       fvdl      470:        switch (lopt) {
                    471:        case LINUX_SO_DEBUG:
                    472:                return SO_DEBUG;
                    473:        case LINUX_SO_REUSEADDR:
1.39      jschauma  474:                /* 
                    475:                 * Linux does not implement SO_REUSEPORT, but allows reuse of a
                    476:                 * host:port pair through SO_REUSEADDR even if the address is not a
                    477:                 * multicast-address.  Effectively, this means that we should use
                    478:                 * SO_REUSEPORT to allow Linux applications to not exit with
                    479:                 * EADDRINUSE
                    480:                 */
                    481:                return SO_REUSEPORT;
1.1       fvdl      482:        case LINUX_SO_TYPE:
                    483:                return SO_TYPE;
                    484:        case LINUX_SO_ERROR:
                    485:                return SO_ERROR;
                    486:        case LINUX_SO_DONTROUTE:
                    487:                return SO_DONTROUTE;
                    488:        case LINUX_SO_BROADCAST:
                    489:                return SO_BROADCAST;
                    490:        case LINUX_SO_SNDBUF:
                    491:                return SO_SNDBUF;
                    492:        case LINUX_SO_RCVBUF:
                    493:                return SO_RCVBUF;
                    494:        case LINUX_SO_KEEPALIVE:
                    495:                return SO_KEEPALIVE;
                    496:        case LINUX_SO_OOBINLINE:
                    497:                return SO_OOBINLINE;
                    498:        case LINUX_SO_LINGER:
                    499:                return SO_LINGER;
                    500:        case LINUX_SO_PRIORITY:
                    501:        case LINUX_SO_NO_CHECK:
                    502:        default:
                    503:                return -1;
                    504:        }
                    505: }
                    506: 
                    507: /*
1.3       mycroft   508:  * Convert Linux IP level socket option number to NetBSD values.
                    509:  */
                    510: int
                    511: linux_to_bsd_ip_sockopt(lopt)
                    512:        int lopt;
                    513: {
1.12      mycroft   514: 
1.3       mycroft   515:        switch (lopt) {
                    516:        case LINUX_IP_TOS:
                    517:                return IP_TOS;
                    518:        case LINUX_IP_TTL:
                    519:                return IP_TTL;
                    520:        case LINUX_IP_MULTICAST_TTL:
                    521:                return IP_MULTICAST_TTL;
                    522:        case LINUX_IP_MULTICAST_LOOP:
                    523:                return IP_MULTICAST_LOOP;
                    524:        case LINUX_IP_MULTICAST_IF:
                    525:                return IP_MULTICAST_IF;
                    526:        case LINUX_IP_ADD_MEMBERSHIP:
                    527:                return IP_ADD_MEMBERSHIP;
                    528:        case LINUX_IP_DROP_MEMBERSHIP:
                    529:                return IP_DROP_MEMBERSHIP;
                    530:        default:
                    531:                return -1;
                    532:        }
                    533: }
                    534: 
                    535: /*
                    536:  * Convert Linux TCP level socket option number to NetBSD values.
                    537:  */
                    538: int
                    539: linux_to_bsd_tcp_sockopt(lopt)
                    540:        int lopt;
                    541: {
1.12      mycroft   542: 
1.3       mycroft   543:        switch (lopt) {
1.6       mycroft   544:        case LINUX_TCP_NODELAY:
                    545:                return TCP_NODELAY;
                    546:        case LINUX_TCP_MAXSEG:
                    547:                return TCP_MAXSEG;
1.3       mycroft   548:        default:
1.6       mycroft   549:                return -1;
1.3       mycroft   550:        }
                    551: }
                    552: 
                    553: /*
                    554:  * Convert Linux UDP level socket option number to NetBSD values.
                    555:  */
                    556: int
                    557: linux_to_bsd_udp_sockopt(lopt)
                    558:        int lopt;
                    559: {
1.12      mycroft   560: 
1.3       mycroft   561:        switch (lopt) {
                    562:        default:
1.6       mycroft   563:                return -1;
1.3       mycroft   564:        }
                    565: }
                    566: 
                    567: /*
1.5       mycroft   568:  * Another reasonably straightforward function: setsockopt(2).
1.1       fvdl      569:  * The level and option numbers are converted; the values passed
                    570:  * are not (yet) converted, the ones currently implemented don't
                    571:  * need conversion, as they are the same on both systems.
                    572:  */
1.2       mycroft   573: int
1.16      erh       574: linux_sys_setsockopt(p, v, retval)
1.1       fvdl      575:        struct proc *p;
1.16      erh       576:        void *v;
                    577:        register_t *retval;
                    578: {
                    579:        struct linux_sys_setsockopt_args /* {
1.1       fvdl      580:                syscallarg(int) s;
                    581:                syscallarg(int) level;
                    582:                syscallarg(int) optname;
                    583:                syscallarg(void *) optval;
                    584:                syscallarg(int) optlen;
1.16      erh       585:        } */ *uap = v;
1.12      mycroft   586:        struct sys_setsockopt_args bsa;
1.16      erh       587:        int name;
1.1       fvdl      588: 
1.16      erh       589:        SCARG(&bsa, s) = SCARG(uap, s);
                    590:        SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
                    591:        SCARG(&bsa, val) = SCARG(uap, optval);
                    592:        SCARG(&bsa, valsize) = SCARG(uap, optlen);
1.1       fvdl      593: 
                    594:        switch (SCARG(&bsa, level)) {
1.40      itojun    595:        case SOL_SOCKET:
                    596:                name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
                    597:                break;
                    598:        case IPPROTO_IP:
                    599:                name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
                    600:                break;
                    601:        case IPPROTO_TCP:
                    602:                name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
                    603:                break;
                    604:        case IPPROTO_UDP:
                    605:                name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
                    606:                break;
                    607:        default:
                    608:                return EINVAL;
1.1       fvdl      609:        }
                    610: 
                    611:        if (name == -1)
                    612:                return EINVAL;
                    613:        SCARG(&bsa, name) = name;
                    614: 
1.12      mycroft   615:        return sys_setsockopt(p, &bsa, retval);
1.1       fvdl      616: }
                    617: 
                    618: /*
                    619:  * getsockopt(2) is very much the same as setsockopt(2) (see above)
                    620:  */
1.2       mycroft   621: int
1.16      erh       622: linux_sys_getsockopt(p, v, retval)
1.1       fvdl      623:        struct proc *p;
1.16      erh       624:        void *v;
                    625:        register_t *retval;
                    626: {
                    627:        struct linux_sys_getsockopt_args /* {
1.1       fvdl      628:                syscallarg(int) s;
                    629:                syscallarg(int) level;
                    630:                syscallarg(int) optname;
                    631:                syscallarg(void *) optval;
1.16      erh       632:                syscallarg(int *) optlen;
                    633:        } */ *uap = v;
1.12      mycroft   634:        struct sys_getsockopt_args bga;
1.16      erh       635:        int name;
1.1       fvdl      636: 
1.16      erh       637:        SCARG(&bga, s) = SCARG(uap, s);
                    638:        SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
                    639:        SCARG(&bga, val) = SCARG(uap, optval);
                    640:        SCARG(&bga, avalsize) = SCARG(uap, optlen);
1.1       fvdl      641: 
                    642:        switch (SCARG(&bga, level)) {
1.40      itojun    643:        case SOL_SOCKET:
                    644:                name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
                    645:                break;
                    646:        case IPPROTO_IP:
                    647:                name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
                    648:                break;
                    649:        case IPPROTO_TCP:
                    650:                name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
                    651:                break;
                    652:        case IPPROTO_UDP:
                    653:                name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
                    654:                break;
                    655:        default:
                    656:                return EINVAL;
1.1       fvdl      657:        }
                    658: 
                    659:        if (name == -1)
                    660:                return EINVAL;
1.5       mycroft   661:        SCARG(&bga, name) = name;
1.1       fvdl      662: 
1.12      mycroft   663:        return sys_getsockopt(p, &bga, retval);
1.13      mycroft   664: }
                    665: 
1.21      jtk       666: #define IF_NAME_LEN 16
                    667: 
                    668: int
                    669: linux_getifhwaddr(p, retval, fd, data)
                    670:        struct proc *p;
                    671:        register_t *retval;
                    672:        u_int fd;
                    673:        void *data;
                    674: {
                    675:        /* Not the full structure, just enough to map what we do here */
                    676:        struct linux_ifreq {
                    677:                char if_name[IF_NAME_LEN];
                    678:                struct osockaddr hwaddr;
                    679:        } lreq;
                    680:        struct filedesc *fdp;
                    681:        struct file *fp;
                    682:        struct ifaddr *ifa;
                    683:        struct ifnet *ifp;
                    684:        struct sockaddr_dl *sadl;
                    685:        int error, found;
                    686:        int index, ifnum;
                    687: 
                    688:        /*
                    689:         * We can't emulate this ioctl by calling sys_ioctl() to run
                    690:         * SIOCGIFCONF, because the user buffer is not of the right
                    691:         * type to take those results.  We can't use kernel buffers to
                    692:         * receive the results, as the implementation of sys_ioctl()
                    693:         * and ifconf() [which implements SIOCGIFCONF] use
                    694:         * copyin()/copyout() which will fail on kernel addresses.
                    695:         *
                    696:         * So, we must duplicate code from sys_ioctl() and ifconf().  Ugh.
                    697:         */
                    698: 
                    699:        fdp = p->p_fd;
1.30      thorpej   700:        if ((fp = fd_getfile(fdp, fd)) == NULL)
1.21      jtk       701:                return (EBADF);
                    702: 
                    703:        FILE_USE(fp);
                    704:        if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
                    705:                error = EBADF;
                    706:                goto out;
                    707:        }
                    708: 
                    709:        error = copyin(data, (caddr_t)&lreq, sizeof(lreq));
                    710:        if (error)
                    711:                goto out;
                    712:        lreq.if_name[IF_NAME_LEN-1] = '\0';             /* just in case */
                    713: 
                    714:        /*
1.22      jdolecek  715:         * Try real interface name first, then fake "ethX"
1.21      jtk       716:         */
1.22      jdolecek  717:        for (ifp = ifnet.tqh_first, found = 0;
                    718:             ifp != 0 && !found;
                    719:             ifp = ifp->if_list.tqe_next) {
                    720:                if (strcmp(lreq.if_name, ifp->if_xname))
                    721:                        /* not this interface */
                    722:                        continue;
                    723:                found=1;           
                    724:                if ((ifa = ifp->if_addrlist.tqh_first) != 0) {
                    725:                        for (; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
                    726:                                sadl = (struct sockaddr_dl *)ifa->ifa_addr;
                    727:                                /* only return ethernet addresses */
                    728:                                /* XXX what about FDDI, etc. ? */
                    729:                                if (sadl->sdl_family != AF_LINK ||
                    730:                                    sadl->sdl_type != IFT_ETHER)
                    731:                                        continue;
                    732:                                memcpy((caddr_t)&lreq.hwaddr.sa_data,
                    733:                                       LLADDR(sadl),
                    734:                                       MIN(sadl->sdl_alen,
                    735:                                           sizeof(lreq.hwaddr.sa_data)));
                    736:                                lreq.hwaddr.sa_family =
                    737:                                        sadl->sdl_family;
                    738:                                error = copyout((caddr_t)&lreq, data,
                    739:                                                sizeof(lreq));
                    740:                                goto out; 
                    741:                        }
                    742:                } else {
                    743:                        error = ENODEV;
                    744:                        goto out;
                    745:                }
                    746:        }
                    747: 
1.31      jdolecek  748:        if (strncmp(lreq.if_name, "eth", 3) == 0) {
1.21      jtk       749:                for (ifnum = 0, index = 3;
                    750:                     lreq.if_name[index] != '\0' && index < IF_NAME_LEN;
                    751:                     index++) {
                    752:                        ifnum *= 10;
                    753:                        ifnum += lreq.if_name[index] - '0';
                    754:                }
                    755: 
                    756:                error = EINVAL;                 /* in case we don't find one */
                    757:                for (ifp = ifnet.tqh_first, found = 0;
                    758:                     ifp != 0 && !found;
                    759:                     ifp = ifp->if_list.tqe_next) {
                    760:                        memcpy(lreq.if_name, ifp->if_xname,
                    761:                               MIN(IF_NAME_LEN, IFNAMSIZ));
                    762:                        if ((ifa = ifp->if_addrlist.tqh_first) == 0)
                    763:                                /* no addresses on this interface */
                    764:                                continue;
                    765:                        else 
                    766:                                for (; ifa != 0; ifa = ifa->ifa_list.tqe_next) {
                    767:                                        sadl = (struct sockaddr_dl *)ifa->ifa_addr;
                    768:                                        /* only return ethernet addresses */
                    769:                                        /* XXX what about FDDI, etc. ? */
                    770:                                        if (sadl->sdl_family != AF_LINK ||
                    771:                                            sadl->sdl_type != IFT_ETHER)
                    772:                                                continue;
                    773:                                        if (ifnum--)
                    774:                                                /* not the reqested iface */
                    775:                                                continue;
                    776:                                        memcpy((caddr_t)&lreq.hwaddr.sa_data,
                    777:                                               LLADDR(sadl),
                    778:                                               MIN(sadl->sdl_alen,
                    779:                                                   sizeof(lreq.hwaddr.sa_data)));
                    780:                                        lreq.hwaddr.sa_family =
                    781:                                                sadl->sdl_family;
                    782:                                        error = copyout((caddr_t)&lreq, data,
                    783:                                                        sizeof(lreq));
                    784:                                        found = 1;
                    785:                                        break;
                    786:                                }
                    787:                }
1.22      jdolecek  788:        } else {
                    789:                /* unknown interface, not even an "eth*" name */
                    790:                error = ENODEV;
                    791:        }
1.21      jtk       792:     
                    793: out:
                    794:        FILE_UNUSE(fp, p);
                    795:        return error;
                    796: }
1.24      thorpej   797: #undef IF_NAME_LEN
1.21      jtk       798: 
1.13      mycroft   799: int
                    800: linux_ioctl_socket(p, uap, retval)
1.23      augustss  801:        struct proc *p;
                    802:        struct linux_sys_ioctl_args /* {
1.13      mycroft   803:                syscallarg(int) fd;
                    804:                syscallarg(u_long) com;
                    805:                syscallarg(caddr_t) data;
                    806:        } */ *uap;
                    807:        register_t *retval;
                    808: {
                    809:        u_long com;
1.25      fvdl      810:        int error = 0, isdev = 0, dosys = 1;
1.13      mycroft   811:        struct sys_ioctl_args ia;
1.25      fvdl      812:        struct file *fp;
                    813:        struct filedesc *fdp;
                    814:        struct vnode *vp;
                    815:        int (*ioctlf) __P((struct file *, u_long, caddr_t, struct proc *));
                    816:        struct ioctl_pt pt;
                    817: 
                    818:         fdp = p->p_fd;
1.30      thorpej   819:        if ((fp = fd_getfile(fdp, SCARG(uap, fd))) == NULL)
1.25      fvdl      820:                return (EBADF);
                    821: 
                    822:        FILE_USE(fp);
                    823: 
                    824:        if (fp->f_type == DTYPE_VNODE) {
                    825:                vp = (struct vnode *)fp->f_data;
                    826:                isdev = vp->v_type == VCHR;
                    827:        }
                    828: 
                    829:        /*
                    830:         * Don't try to interpret socket ioctl calls that are done
                    831:         * on a device filedescriptor, just pass them through, to
                    832:         * emulate Linux behaviour. Use PTIOCLINUX so that the
                    833:         * device will only handle these if it's prepared to do
                    834:         * so, to avoid unexpected things from happening.
                    835:         */
                    836:        if (isdev) {
1.26      fvdl      837:                dosys = 0;
1.25      fvdl      838:                ioctlf = fp->f_ops->fo_ioctl;
                    839:                pt.com = SCARG(uap, com);
                    840:                pt.data = SCARG(uap, data);
                    841:                error = ioctlf(fp, PTIOCLINUX, (caddr_t)&pt, p);
1.27      fvdl      842:                /*
                    843:                 * XXX hack: if the function returns EJUSTRETURN,       
                    844:                 * it has stuffed a sysctl return value in pt.data.
                    845:                 */
                    846:                if (error == EJUSTRETURN) {
                    847:                        retval[0] = (register_t)pt.data;
                    848:                        error = 0;
                    849:                }
1.25      fvdl      850:                goto out;
                    851:        }
1.13      mycroft   852: 
                    853:        com = SCARG(uap, com);
                    854:        retval[0] = 0;
                    855: 
                    856:        switch (com) {
                    857:        case LINUX_SIOCGIFCONF:
                    858:                SCARG(&ia, com) = OSIOCGIFCONF;
                    859:                break;
                    860:        case LINUX_SIOCGIFFLAGS:
                    861:                SCARG(&ia, com) = SIOCGIFFLAGS;
                    862:                break;
1.25      fvdl      863:        case LINUX_SIOCSIFFLAGS:
                    864:                SCARG(&ia, com) = SIOCSIFFLAGS;
                    865:                break;
1.13      mycroft   866:        case LINUX_SIOCGIFADDR:
                    867:                SCARG(&ia, com) = OSIOCGIFADDR;
                    868:                break;
                    869:        case LINUX_SIOCGIFDSTADDR:
                    870:                SCARG(&ia, com) = OSIOCGIFDSTADDR;
                    871:                break;
                    872:        case LINUX_SIOCGIFBRDADDR:
                    873:                SCARG(&ia, com) = OSIOCGIFBRDADDR;
                    874:                break;
                    875:        case LINUX_SIOCGIFNETMASK:
                    876:                SCARG(&ia, com) = OSIOCGIFNETMASK;
                    877:                break;
                    878:        case LINUX_SIOCADDMULTI:
                    879:                SCARG(&ia, com) = SIOCADDMULTI;
                    880:                break;
                    881:        case LINUX_SIOCDELMULTI:
                    882:                SCARG(&ia, com) = SIOCDELMULTI;
                    883:                break;
1.21      jtk       884:        case LINUX_SIOCGIFHWADDR:
1.25      fvdl      885:                error = linux_getifhwaddr(p, retval, SCARG(uap, fd),
1.21      jtk       886:                                         SCARG(uap, data));
1.29      fvdl      887:                dosys = 0;
1.25      fvdl      888:                break;
1.13      mycroft   889:        default:
1.25      fvdl      890:                error = EINVAL;
                    891:        }
                    892: 
                    893: out:
                    894:        FILE_UNUSE(fp, p);
                    895: 
                    896:        if (error ==0 && dosys) {
                    897:                SCARG(&ia, fd) = SCARG(uap, fd);
                    898:                SCARG(&ia, data) = SCARG(uap, data);
                    899:                error = sys_ioctl(p, &ia, retval);
1.13      mycroft   900:        }
                    901: 
1.25      fvdl      902:        return error;
1.1       fvdl      903: }
1.19      sommerfe  904: 
                    905: int
                    906: linux_sys_connect(p, v, retval)
1.23      augustss  907:        struct proc *p;
1.19      sommerfe  908:        void *v;
                    909:        register_t *retval;
                    910: {
1.32      jdolecek  911:        struct linux_sys_connect_args /* {
1.19      sommerfe  912:                syscallarg(int) s;
                    913:                syscallarg(const struct sockaddr *) name;
1.32      jdolecek  914:                syscallarg(int) namelen;
1.19      sommerfe  915:        } */ *uap = v;
1.32      jdolecek  916:        int             error;
                    917:        struct sockaddr *sa;
                    918:        struct sys_connect_args bca;
1.36      christos  919:        caddr_t sg = stackgap_init(p, 0);
1.34      fvdl      920:        int namlen;
1.32      jdolecek  921: 
1.34      fvdl      922:        namlen = SCARG(uap, namelen);
1.36      christos  923:        error = linux_sa_get(p, &sg, &sa, SCARG(uap, name), &namlen);
1.32      jdolecek  924:        if (error)
                    925:                return (error);
1.19      sommerfe  926:        
1.32      jdolecek  927:        SCARG(&bca, s) = SCARG(uap, s);
                    928:        SCARG(&bca, name) = sa;
1.34      fvdl      929:        SCARG(&bca, namelen) = (unsigned int) namlen;
1.32      jdolecek  930: 
                    931:        error = sys_connect(p, &bca, retval);
1.19      sommerfe  932: 
                    933:        if (error == EISCONN) {
                    934:                struct file *fp;
1.23      augustss  935:                struct socket *so;
1.19      sommerfe  936:                int s, state, prflags;
                    937:                
1.20      thorpej   938:                /* getsock() will use the descriptor for us */
                    939:                if (getsock(p->p_fd, SCARG(uap, s), &fp) != 0)
1.19      sommerfe  940:                        return EISCONN;
                    941: 
                    942:                s = splsoftnet();
                    943:                so = (struct socket *)fp->f_data;
                    944:                state = so->so_state;
                    945:                prflags = so->so_proto->pr_flags;
                    946:                splx(s);
1.20      thorpej   947:                FILE_UNUSE(fp, p);
1.19      sommerfe  948:                /*
                    949:                 * We should only let this call succeed once per
                    950:                 * non-blocking connect; however we don't have
                    951:                 * a convenient place to keep that state..
                    952:                 */
                    953:                if ((state & SS_NBIO) && (state & SS_ISCONNECTED) &&
                    954:                    (prflags & PR_CONNREQUIRED))
                    955:                        return 0;
                    956:        }
                    957: 
1.32      jdolecek  958:        return (error);
                    959: }
                    960: 
                    961: int
                    962: linux_sys_bind(p, v, retval)
                    963:        struct proc *p;
                    964:        void *v;
                    965:        register_t *retval;
                    966: {
                    967:        struct linux_sys_bind_args /* {
                    968:                syscallarg(int) s;
                    969:                syscallarg(const struct osockaddr *) name;
                    970:                syscallarg(int) namelen;
                    971:        } */ *uap = v;
1.34      fvdl      972:        int             error, namlen;
1.32      jdolecek  973:        struct sys_bind_args bsa;
                    974: 
1.34      fvdl      975:        namlen = SCARG(uap, namelen);
1.32      jdolecek  976:        SCARG(&bsa, s) = SCARG(uap, s);
                    977:        if (SCARG(uap, name)) {
                    978:                struct sockaddr *sa;
1.36      christos  979:                caddr_t sg = stackgap_init(p, 0);
1.32      jdolecek  980: 
1.36      christos  981:                error = linux_sa_get(p, &sg, &sa, SCARG(uap, name), &namlen);
1.32      jdolecek  982:                if (error)
                    983:                        return (error);
                    984: 
                    985:                SCARG(&bsa, name) = sa;
                    986:        } else
                    987:                SCARG(&bsa, name) = NULL;
1.34      fvdl      988:        SCARG(&bsa, namelen) = namlen;
1.32      jdolecek  989: 
                    990:        return (sys_bind(p, &bsa, retval));
                    991: }
                    992: 
                    993: int
                    994: linux_sys_getsockname(p, v, retval)
                    995:        struct proc *p;
                    996:        void *v;
                    997:        register_t *retval;
                    998: {
                    999:        struct linux_sys_getsockname_args /* {
                   1000:                syscallarg(int) fdes;
                   1001:                syscallarg(caddr_t) asa;
                   1002:                syscallarg(int *) alen;
                   1003:        } */ *uap = v;
                   1004:        int error;
                   1005: 
                   1006:        if ((error = sys_getsockname(p, uap, retval)) != 0)
                   1007:                return (error);
                   1008: 
                   1009:        if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
                   1010:                return (error);
                   1011: 
                   1012:        return (0);
                   1013: }
                   1014: 
                   1015: int
                   1016: linux_sys_getpeername(p, v, retval)
                   1017:        struct proc *p;
                   1018:        void *v;
                   1019:        register_t *retval;
                   1020: {
                   1021:        struct sys_getpeername_args /* {
                   1022:                syscallarg(int) fdes;
                   1023:                syscallarg(caddr_t) asa;
                   1024:                syscallarg(int *) alen;
                   1025:        } */ *uap = v;
                   1026:        int error;
                   1027: 
                   1028:        if ((error = sys_getpeername(p, uap, retval)) != 0)
                   1029:                return (error);
                   1030: 
                   1031:        if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
                   1032:                return (error);
                   1033: 
                   1034:        return (0);
                   1035: }
                   1036: 
                   1037: /*
                   1038:  * Copy the osockaddr structure pointed to by osa to kernel, adjust
                   1039:  * family and convert to sockaddr, allocate stackgap and put the
                   1040:  * the converted structure there, address on stackgap returned in sap.
                   1041:  */
                   1042: static int
1.36      christos 1043: linux_sa_get(p, sgp, sap, osa, osalen)
                   1044:        struct proc *p;
1.32      jdolecek 1045:        caddr_t *sgp;
                   1046:        struct sockaddr **sap;
1.33      jdolecek 1047:        const struct osockaddr *osa;
1.34      fvdl     1048:        int *osalen;
1.32      jdolecek 1049: {
                   1050:        int error=0, bdom;
                   1051:        struct sockaddr *sa, *usa;
                   1052:        struct osockaddr *kosa = (struct osockaddr *) &sa;
1.34      fvdl     1053:        int alloclen;
                   1054: #ifdef INET6
                   1055:        int oldv6size;
                   1056:        struct sockaddr_in6 *sin6;
                   1057: #endif
1.32      jdolecek 1058: 
1.37      christos 1059:        if (*osalen < 2 || *osalen > UCHAR_MAX || !osa) {
                   1060:                DPRINTF(("bad osa=%p osalen=%d\n", osa, *osalen));
1.32      jdolecek 1061:                return (EINVAL);
1.37      christos 1062:        }
1.32      jdolecek 1063: 
1.34      fvdl     1064:        alloclen = *osalen;
                   1065: #ifdef INET6
                   1066:        oldv6size = 0;
                   1067:        /*
                   1068:         * Check for old (pre-RFC2553) sockaddr_in6. We may accept it
                   1069:         * if it's a v4-mapped address, so reserve the proper space
                   1070:         * for it.
                   1071:         */
                   1072:        if (alloclen == sizeof (struct sockaddr_in6) - sizeof (u_int32_t)) {
                   1073:                alloclen = sizeof (struct sockaddr_in6);
                   1074:                oldv6size = 1;
                   1075:        }
                   1076: #endif
1.32      jdolecek 1077: 
1.34      fvdl     1078:        kosa = (struct osockaddr *) malloc(alloclen, M_TEMP, M_WAITOK);
                   1079: 
1.37      christos 1080:        if ((error = copyin(osa, (caddr_t) kosa, *osalen))) {
                   1081:                DPRINTF(("error copying osa %d\n", error));
1.32      jdolecek 1082:                goto out;
1.37      christos 1083:        }
1.32      jdolecek 1084: 
                   1085:        bdom = linux_to_bsd_domain(kosa->sa_family);
                   1086:        if (bdom == -1) {
1.37      christos 1087:                DPRINTF(("bad linux family=%d\n", kosa->sa_family));
1.32      jdolecek 1088:                error = EINVAL;
                   1089:                goto out;
                   1090:        }
                   1091: 
                   1092: #ifdef INET6
                   1093:        /*
                   1094:         * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
                   1095:         * which lacks the scope id compared with RFC2553 one. If we detect
                   1096:         * the situation, reject the address and write a message to system log.
1.34      fvdl     1097:         *
                   1098:         * Still accept addresses for which the scope id is not used.
1.32      jdolecek 1099:         */
1.34      fvdl     1100:        if (oldv6size && bdom == AF_INET6) {
                   1101:                sin6 = (struct sockaddr_in6 *)kosa;
                   1102:                if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) ||
                   1103:                    (!IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) &&
                   1104:                     !IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) &&
                   1105:                     !IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) &&
                   1106:                     !IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) &&
                   1107:                     !IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
                   1108:                        sin6->sin6_scope_id = 0;
                   1109:                } else {
                   1110:                        struct proc *p = curproc;       /* XXX */
                   1111:                        int uid = p->p_cred && p->p_ucred ? 
                   1112:                                        p->p_ucred->cr_uid : -1;
                   1113: 
                   1114:                        log(LOG_DEBUG,
                   1115:                            "pid %d (%s), uid %d: obsolete pre-RFC2553 "
                   1116:                            "sockaddr_in6 rejected",
                   1117:                            p->p_pid, p->p_comm, uid);
                   1118:                        error = EINVAL;
                   1119:                        goto out;
                   1120:                }
1.38      christos 1121:        } else
                   1122: #endif 
                   1123:        if (bdom == AF_INET) {
1.37      christos 1124:                alloclen = sizeof(struct sockaddr_in);
1.32      jdolecek 1125:        }
                   1126: 
                   1127:        sa = (struct sockaddr *) kosa;
                   1128:        sa->sa_family = bdom;
1.34      fvdl     1129:        sa->sa_len = alloclen;
1.37      christos 1130: #ifdef DEBUG_LINUX
                   1131:        DPRINTF(("family %d, len = %d [ ", sa->sa_family, sa->sa_len));
                   1132:        for (bdom = 0; bdom < sizeof(sa->sa_data); bdom++)
                   1133:            DPRINTF(("%02x ", sa->sa_data[bdom]));
                   1134:        DPRINTF(("\n"));
                   1135: #endif
1.32      jdolecek 1136: 
1.36      christos 1137:        usa = (struct sockaddr *) stackgap_alloc(p, sgp, alloclen);
1.32      jdolecek 1138:        if (!usa) {
                   1139:                error = ENOMEM;
                   1140:                goto out;
                   1141:        }
                   1142: 
1.37      christos 1143:        if ((error = copyout(sa, usa, alloclen))) {
                   1144:                DPRINTF(("error copying out socket %d\n", error));
1.32      jdolecek 1145:                goto out;
1.37      christos 1146:        }
1.32      jdolecek 1147: 
                   1148:        *sap = usa;
                   1149: 
                   1150:     out:
1.34      fvdl     1151:        *osalen = alloclen;
1.32      jdolecek 1152:        free(kosa, M_TEMP);
                   1153:        return (error);
                   1154: }
                   1155: 
                   1156: static int
                   1157: linux_sa_put(osa)
                   1158:        struct osockaddr *osa;
                   1159: {
                   1160:        struct sockaddr sa;
                   1161:        struct osockaddr *kosa;
                   1162:        int error, bdom, len;
                   1163: 
                   1164:        /*
                   1165:         * Only read/write the sockaddr family and length part, the rest is
                   1166:         * not changed.
                   1167:         */
                   1168:        len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
                   1169: 
                   1170:        error = copyin((caddr_t) osa, (caddr_t) &sa, len);
                   1171:        if (error)
                   1172:                return (error);
                   1173: 
                   1174:        bdom = bsd_to_linux_domain(sa.sa_family);
                   1175:        if (bdom == -1)
                   1176:                return (EINVAL);
                   1177: 
                   1178:        /* Note: we convert from sockaddr to osockaddr here, too */
                   1179:        kosa = (struct osockaddr *) &sa;
                   1180:        kosa->sa_family = bdom;
                   1181:        error = copyout(kosa, osa, len);
                   1182:        if (error)
                   1183:                return (error);
                   1184: 
                   1185:        return (0);
                   1186: }
                   1187: 
                   1188: int
                   1189: linux_sys_recv(p, v, retval)
                   1190:        struct proc *p;
                   1191:        void *v;
                   1192:        register_t *retval;
                   1193: {
                   1194:        struct linux_sys_recv_args /* {
                   1195:                syscallarg(int) s;
                   1196:                syscallarg(void *) buf;
                   1197:                syscallarg(int) len;
                   1198:                syscallarg(int) flags;
                   1199:        } */ *uap = v;
                   1200:        struct sys_recvfrom_args bra;
                   1201: 
                   1202: 
                   1203:        SCARG(&bra, s) = SCARG(uap, s);
                   1204:        SCARG(&bra, buf) = SCARG(uap, buf);
                   1205:        SCARG(&bra, len) = (size_t) SCARG(uap, len);
                   1206:        SCARG(&bra, flags) = SCARG(uap, flags);
                   1207:        SCARG(&bra, from) = NULL;
                   1208:        SCARG(&bra, fromlenaddr) = NULL;
                   1209: 
                   1210:        return (sys_recvfrom(p, &bra, retval));
                   1211: }
                   1212: 
                   1213: int
                   1214: linux_sys_send(p, v, retval)
                   1215:        struct proc *p;
                   1216:        void *v;
                   1217:        register_t *retval;
                   1218: {
                   1219:        struct linux_sys_send_args /* {
                   1220:                syscallarg(int) s;
                   1221:                syscallarg(caddr_t) buf;
                   1222:                syscallarg(int) len;
                   1223:                syscallarg(int) flags;
                   1224:        } */ *uap = v;
                   1225:        struct sys_sendto_args bsa;
                   1226: 
                   1227:        SCARG(&bsa, s)          = SCARG(uap, s);
                   1228:        SCARG(&bsa, buf)        = SCARG(uap, buf);
                   1229:        SCARG(&bsa, len)        = SCARG(uap, len);
                   1230:        SCARG(&bsa, flags)      = SCARG(uap, flags);
                   1231:        SCARG(&bsa, to)         = NULL;
                   1232:        SCARG(&bsa, tolen)      = 0;
                   1233: 
                   1234:        return (sys_sendto(p, &bsa, retval));
                   1235: }
                   1236: 
                   1237: int
                   1238: linux_sys_accept(p, v, retval)
                   1239:        struct proc *p;
                   1240:        void *v;
                   1241:        register_t *retval;
                   1242: {
                   1243:        struct linux_sys_accept_args /* {
                   1244:                syscallarg(int) s;
                   1245:                syscallarg(struct osockaddr *) name;
                   1246:                syscallarg(int *) anamelen;
                   1247:        } */ *uap = v;
                   1248:        int error;
                   1249:        struct sys_accept_args baa;
                   1250: 
                   1251:        SCARG(&baa, s)          = SCARG(uap, s);
                   1252:        SCARG(&baa, name)       = (struct sockaddr *) SCARG(uap, name);
                   1253:        SCARG(&baa, anamelen)   = (unsigned int *) SCARG(uap, anamelen);
                   1254: 
                   1255:        if ((error = sys_accept(p, &baa, retval)))
                   1256:                return (error);
                   1257: 
                   1258:        if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
                   1259:                return (error);
                   1260: 
                   1261:        return (0);
1.19      sommerfe 1262: }

CVSweb <webmaster@jp.NetBSD.org>