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

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

CVSweb <webmaster@jp.NetBSD.org>