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

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

CVSweb <webmaster@jp.NetBSD.org>