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

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

CVSweb <webmaster@jp.NetBSD.org>