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

1.161   ! riastrad    1: /*     $NetBSD: linux_socket.c,v 1.160 2026/09/19 23:47:51 riastradh 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.161   ! riastrad   38: __KERNEL_RCSID(0, "$NetBSD: linux_socket.c,v 1.160 2026/09/19 23:47:51 riastradh 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.154     ryo        88: #if !defined(__aarch64__) && !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.159     riastrad  128: static int linux_to_bsd_msghdr(const struct linux_msghdr *, struct msghdr *);
1.137     christos  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.159     riastrad  440: static int
1.137     christos  441: linux_to_bsd_msghdr(const struct linux_msghdr *lmsg, struct msghdr *bmsg)
1.104     njoly     442: {
1.159     riastrad  443: 
                    444:        /*
                    445:         * Verify no integer truncation of the inputs.  For overly
                    446:         * large inputs, fail with the same error codes Linux uses.
                    447:         */
                    448:        if (lmsg->msg_namelen < 0)
                    449:                return EINVAL;
                    450:        CTASSERT(!__type_is_signed(__typeof(lmsg->msg_iovlen)));
                    451:        if (lmsg->msg_iovlen > UIO_MAXIOV)
                    452:                return EMSGSIZE;
                    453:        CTASSERT(!__type_is_signed(__typeof(lmsg->msg_controllen)));
                    454:        if (lmsg->msg_controllen > __type_max(__typeof(bmsg->msg_controllen)))
                    455:                return ENOBUFS;
                    456: 
1.153     riastrad  457:        memset(bmsg, 0, sizeof(*bmsg));
1.104     njoly     458:        bmsg->msg_name = lmsg->msg_name;
1.159     riastrad  459:        bmsg->msg_namelen = MIN(lmsg->msg_namelen,
                    460:            sizeof(struct sockaddr_storage));
1.104     njoly     461:        bmsg->msg_iov = lmsg->msg_iov;
                    462:        bmsg->msg_iovlen = lmsg->msg_iovlen;
                    463:        bmsg->msg_control = lmsg->msg_control;
                    464:        bmsg->msg_controllen = lmsg->msg_controllen;
                    465:        bmsg->msg_flags = lmsg->msg_flags;
1.159     riastrad  466: 
                    467:        return 0;
1.104     njoly     468: }
                    469: 
                    470: static void
1.137     christos  471: bsd_to_linux_msghdr(const struct msghdr *bmsg, struct linux_msghdr *lmsg)
1.104     njoly     472: {
1.159     riastrad  473: 
                    474:        /*
                    475:         * Assert no truncation on the way out.
                    476:         *
                    477:         * - msg_namelen should have been preserved or shortened by
                    478:         *   copyout_sockname.
                    479:         * - msg_iovlen should have been preserved and previously
                    480:         *   confirmed by linux_to_bsd_msghdr to lie in [0,UIO_MAXIOV].
                    481:         * - msg_controllen can always be faithfully converted from BSD
                    482:         *   struct msghdr to Linux struct msghdr without truncation.
                    483:         */
                    484:        CTASSERT(__type_min(__typeof(bmsg->msg_namelen)) == 0);
                    485:        KASSERT(bmsg->msg_namelen <= __type_max(__typeof(lmsg->msg_namelen)));
                    486:        CTASSERT(__type_min(__typeof(lmsg->msg_iovlen)) <= 0);
                    487:        KASSERT(lmsg->msg_iovlen >= 0);
                    488:        KASSERT(lmsg->msg_iovlen <= UIO_MAXIOV);
                    489:        CTASSERT(__type_min(__typeof(lmsg->msg_controllen)) <=
                    490:            __type_min(__typeof(bmsg->msg_controllen)));
                    491:        CTASSERT(__type_max(__typeof(lmsg->msg_controllen)) >=
                    492:            __type_max(__typeof(bmsg->msg_controllen)));
                    493: 
1.153     riastrad  494:        memset(lmsg, 0, sizeof(*lmsg));
1.104     njoly     495:        lmsg->msg_name = bmsg->msg_name;
                    496:        lmsg->msg_namelen = bmsg->msg_namelen;
                    497:        lmsg->msg_iov = bmsg->msg_iov;
                    498:        lmsg->msg_iovlen = bmsg->msg_iovlen;
                    499:        lmsg->msg_control = bmsg->msg_control;
                    500:        lmsg->msg_controllen = bmsg->msg_controllen;
                    501:        lmsg->msg_flags = bmsg->msg_flags;
                    502: }
                    503: 
1.32      jdolecek  504: int
1.85      dsl       505: linux_sys_sendmsg(struct lwp *l, const struct linux_sys_sendmsg_args *uap, register_t *retval)
1.32      jdolecek  506: {
1.85      dsl       507:        /* {
1.32      jdolecek  508:                syscallarg(int) s;
1.104     njoly     509:                syscallarg(struct linux_msghdr *) msg;
1.32      jdolecek  510:                syscallarg(u_int) flags;
1.85      dsl       511:        } */
1.32      jdolecek  512:        struct msghdr   msg;
1.104     njoly     513:        struct linux_msghdr lmsg;
1.32      jdolecek  514:        int             error;
1.49      jdolecek  515:        int             bflags;
1.125     rtr       516:        struct sockaddr_big nam;
1.73      dsl       517:        u_int8_t        *control;
                    518:        struct mbuf     *ctl_mbuf = NULL;
1.32      jdolecek  519: 
1.104     njoly     520:        error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg));
1.100     njoly     521:        if (error)
                    522:                return error;
1.159     riastrad  523:        error = linux_to_bsd_msghdr(&lmsg, &msg);
                    524:        if (error)
                    525:                return error;
1.100     njoly     526: 
1.73      dsl       527:        msg.msg_flags = MSG_IOVUSRSPACE;
1.32      jdolecek  528: 
1.49      jdolecek  529:        /*
                    530:         * Translate message flags.
                    531:         */
                    532:        bflags = linux_to_bsd_msg_flags(SCARG(uap, flags));
1.73      dsl       533:        if (bflags < 0)
1.49      jdolecek  534:                /* Some supported flag */
1.73      dsl       535:                return EINVAL;
                    536: 
1.104     njoly     537:        if (lmsg.msg_name) {
1.73      dsl       538:                /* Read in and convert the sockaddr */
                    539:                error = linux_get_sa(l, SCARG(uap, s), &nam, msg.msg_name,
                    540:                    msg.msg_namelen);
                    541:                if (error)
                    542:                        return (error);
1.125     rtr       543:                msg.msg_name = &nam;
1.32      jdolecek  544:        }
                    545: 
                    546:        /*
1.49      jdolecek  547:         * Handle cmsg if there is any.
1.32      jdolecek  548:         */
1.104     njoly     549:        if (LINUX_CMSG_FIRSTHDR(&lmsg)) {
1.76      dsl       550:                struct linux_cmsghdr l_cmsg, *l_cc;
                    551:                struct cmsghdr *cmsg;
1.160     riastrad  552:                socklen_t resid = msg.msg_controllen;
1.49      jdolecek  553:                size_t clen, cidx = 0, cspace;
                    554: 
1.73      dsl       555:                ctl_mbuf = m_get(M_WAIT, MT_CONTROL);
                    556:                clen = MLEN;
                    557:                control = mtod(ctl_mbuf, void *);
1.49      jdolecek  558: 
1.104     njoly     559:                l_cc = LINUX_CMSG_FIRSTHDR(&lmsg);
1.49      jdolecek  560:                do {
1.160     riastrad  561:                        if (resid < sizeof(l_cmsg)) {
                    562:                                error = EINVAL;
                    563:                                goto done;
                    564:                        }
1.76      dsl       565:                        error = copyin(l_cc, &l_cmsg, sizeof(l_cmsg));
1.49      jdolecek  566:                        if (error)
                    567:                                goto done;
                    568: 
                    569:                        /*
                    570:                         * Sanity check the control message length.
1.160     riastrad  571:                         * If either:
                    572:                         *
                    573:                         * 1. aligning it would overflow socklen_t, or
                    574:                         * 2. aligning it exceeds msg_controllen, or
                    575:                         * 3. adding the delta would overflow socklen_t, or
                    576:                         * 4. it's not enough for a struct cmsghdr,
                    577:                         *
                    578:                         * then fail with EINVAL.
1.49      jdolecek  579:                         */
1.160     riastrad  580:                        CTASSERT(LINUX_CMSG_ALIGN(1) <=
                    581:                            __type_max(__typeof(resid)));
                    582:                        if ((l_cmsg.cmsg_len >= __type_max(__typeof(resid)) -
                    583:                                LINUX_CMSG_ALIGN(1)) ||
                    584:                            (LINUX_CMSG_ALIGN(l_cmsg.cmsg_len) > resid) ||
                    585:                            (l_cmsg.cmsg_len >
                    586:                                __type_max(__typeof(cmsg->cmsg_len)) -
                    587:                                LINUX_CMSG_ALIGN_DELTA) ||
                    588:                            (l_cmsg.cmsg_len < sizeof(l_cmsg))) {
1.49      jdolecek  589:                                error = EINVAL;
                    590:                                goto done;
                    591:                        }
                    592: 
                    593:                        /*
                    594:                         * Refuse unsupported control messages, and
                    595:                         * translate fields as appropriate.
                    596:                         */
1.76      dsl       597:                        switch (l_cmsg.cmsg_level) {
1.49      jdolecek  598:                        case LINUX_SOL_SOCKET:
                    599:                                /* It only differs on some archs */
1.73      dsl       600:                                if (LINUX_SOL_SOCKET != SOL_SOCKET)
1.76      dsl       601:                                        l_cmsg.cmsg_level = SOL_SOCKET;
1.49      jdolecek  602: 
1.76      dsl       603:                                switch(l_cmsg.cmsg_type) {
1.49      jdolecek  604:                                case LINUX_SCM_RIGHTS:
                    605:                                        /* Linux SCM_RIGHTS is same as NetBSD */
                    606:                                        break;
                    607: 
1.108     chs       608:                                case LINUX_SCM_CREDENTIALS:
                    609:                                        /* no native equivalent, just drop it */
1.148     maxv      610:                                        if (control != mtod(ctl_mbuf, void *))
                    611:                                                free(control, M_MBUF);
1.108     chs       612:                                        m_free(ctl_mbuf);
                    613:                                        ctl_mbuf = NULL;
                    614:                                        msg.msg_control = NULL;
                    615:                                        msg.msg_controllen = 0;
                    616:                                        goto skipcmsg;
                    617: 
1.49      jdolecek  618:                                default:
                    619:                                        /* other types not supported */
                    620:                                        error = EINVAL;
                    621:                                        goto done;
                    622:                                }
                    623:                                break;
                    624:                        default:
                    625:                                /* pray and leave intact */
                    626:                                break;
                    627:                        }
                    628: 
1.76      dsl       629:                        cspace = CMSG_SPACE(l_cmsg.cmsg_len - sizeof(l_cmsg));
1.49      jdolecek  630: 
                    631:                        /* Check the buffer is big enough */
                    632:                        if (__predict_false(cidx + cspace > clen)) {
                    633:                                u_int8_t *nc;
1.147     maxv      634:                                size_t nclen;
1.49      jdolecek  635: 
1.147     maxv      636:                                nclen = cidx + cspace;
                    637:                                if (nclen >= PAGE_SIZE) {
1.73      dsl       638:                                        error = EINVAL;
                    639:                                        goto done;
                    640:                                }
                    641:                                nc = realloc(clen <= MLEN ? NULL : control,
1.147     maxv      642:                                                nclen, M_TEMP, M_WAITOK);
1.49      jdolecek  643:                                if (!nc) {
                    644:                                        error = ENOMEM;
                    645:                                        goto done;
                    646:                                }
1.73      dsl       647:                                if (cidx <= MLEN)
                    648:                                        /* Old buffer was in mbuf... */
                    649:                                        memcpy(nc, control, cidx);
1.49      jdolecek  650:                                control = nc;
1.147     maxv      651:                                clen = nclen;
1.49      jdolecek  652:                        }
                    653: 
                    654:                        /* Copy header */
1.76      dsl       655:                        cmsg = (void *)&control[cidx];
                    656:                        cmsg->cmsg_len = l_cmsg.cmsg_len + LINUX_CMSG_ALIGN_DELTA;
                    657:                        cmsg->cmsg_level = l_cmsg.cmsg_level;
                    658:                        cmsg->cmsg_type = l_cmsg.cmsg_type;
1.49      jdolecek  659: 
1.108     chs       660:                        /* Zero area between header and data */
1.156     riastrad  661:                        memset(cmsg + 1, 0,
1.112     joerg     662:                                CMSG_ALIGN(sizeof(*cmsg)) - sizeof(*cmsg));
1.49      jdolecek  663: 
                    664:                        /* Copyin the data */
1.76      dsl       665:                        error = copyin(LINUX_CMSG_DATA(l_cc),
1.112     joerg     666:                                CMSG_DATA(cmsg),
1.76      dsl       667:                                l_cmsg.cmsg_len - sizeof(l_cmsg));
1.49      jdolecek  668:                        if (error)
                    669:                                goto done;
                    670: 
1.76      dsl       671:                        resid -= LINUX_CMSG_ALIGN(l_cmsg.cmsg_len);
1.49      jdolecek  672:                        cidx += cspace;
1.146     maxv      673:                } while ((l_cc = LINUX_CMSG_NXTHDR(&msg, l_cc, &l_cmsg)) && resid > 0);
1.49      jdolecek  674: 
1.73      dsl       675:                /* If we allocated a buffer, attach to mbuf */
                    676:                if (cidx > MLEN) {
                    677:                        MEXTADD(ctl_mbuf, control, clen, M_MBUF, NULL, NULL);
                    678:                        ctl_mbuf->m_flags |= M_EXT_RW;
1.49      jdolecek  679:                }
1.73      dsl       680:                control = NULL;
                    681:                ctl_mbuf->m_len = cidx;
1.49      jdolecek  682: 
1.73      dsl       683:                msg.msg_control = ctl_mbuf;
                    684:                msg.msg_flags |= MSG_CONTROLMBUF;
1.101     njoly     685: 
1.114     christos  686:                ktrkuser("mbcontrol", mtod(ctl_mbuf, void *),
1.101     njoly     687:                    msg.msg_controllen);
1.49      jdolecek  688:        }
1.32      jdolecek  689: 
1.108     chs       690: skipcmsg:
1.141     christos  691:        error = do_sys_sendmsg(l, SCARG(uap, s), &msg, bflags, retval);
1.73      dsl       692:        /* Freed internally */
                    693:        ctl_mbuf = NULL;
1.32      jdolecek  694: 
1.49      jdolecek  695: done:
1.73      dsl       696:        if (ctl_mbuf != NULL) {
                    697:                if (control != NULL && control != mtod(ctl_mbuf, void *))
                    698:                        free(control, M_MBUF);
                    699:                m_free(ctl_mbuf);
                    700:        }
1.49      jdolecek  701:        return (error);
1.1       fvdl      702: }
                    703: 
1.2       mycroft   704: int
1.85      dsl       705: linux_sys_recvfrom(struct lwp *l, const struct linux_sys_recvfrom_args *uap, register_t *retval)
1.16      erh       706: {
1.85      dsl       707:        /* {
1.1       fvdl      708:                syscallarg(int) s;
                    709:                syscallarg(void *) buf;
                    710:                syscallarg(int) len;
                    711:                syscallarg(int) flags;
1.32      jdolecek  712:                syscallarg(struct osockaddr *) from;
                    713:                syscallarg(int *) fromlenaddr;
1.85      dsl       714:        } */
1.32      jdolecek  715:        int             error;
                    716:        struct sys_recvfrom_args bra;
1.1       fvdl      717: 
1.16      erh       718:        SCARG(&bra, s) = SCARG(uap, s);
                    719:        SCARG(&bra, buf) = SCARG(uap, buf);
                    720:        SCARG(&bra, len) = SCARG(uap, len);
                    721:        SCARG(&bra, flags) = SCARG(uap, flags);
1.32      jdolecek  722:        SCARG(&bra, from) = (struct sockaddr *) SCARG(uap, from);
1.61      mrg       723:        SCARG(&bra, fromlenaddr) = (socklen_t *)SCARG(uap, fromlenaddr);
1.1       fvdl      724: 
1.43      thorpej   725:        if ((error = sys_recvfrom(l, &bra, retval)))
1.32      jdolecek  726:                return (error);
                    727: 
                    728:        if (SCARG(uap, from) && (error = linux_sa_put(SCARG(uap, from))))
                    729:                return (error);
                    730: 
                    731:        return (0);
                    732: }
                    733: 
1.76      dsl       734: static int
                    735: linux_copyout_msg_control(struct lwp *l, struct msghdr *mp, struct mbuf *control)
                    736: {
1.161   ! riastrad  737:        socklen_t dlen;
        !           738:        int error = 0;
1.76      dsl       739:        struct cmsghdr *cmsg;
                    740:        struct linux_cmsghdr linux_cmsg;
                    741:        struct mbuf *m;
                    742:        char *q, *q_end;
                    743: 
                    744:        if (mp->msg_controllen <= 0 || control == 0) {
                    745:                mp->msg_controllen = 0;
                    746:                free_control_mbuf(l, control, control);
                    747:                return 0;
                    748:        }
                    749: 
1.101     njoly     750:        ktrkuser("msgcontrol", mtod(control, void *), mp->msg_controllen);
                    751: 
1.76      dsl       752:        q = (char *)mp->msg_control;
                    753:        q_end = q + mp->msg_controllen;
                    754: 
                    755:        for (m = control; m != NULL; ) {
                    756:                cmsg = mtod(m, struct cmsghdr *);
                    757: 
                    758:                /*
                    759:                 * Fixup cmsg. We handle two things:
                    760:                 * 0. different sizeof cmsg_len.
                    761:                 * 1. different values for level/type on some archs
                    762:                 * 2. different alignment of CMSG_DATA on some archs
                    763:                 */
1.153     riastrad  764:                memset(&linux_cmsg, 0, sizeof(linux_cmsg));
1.160     riastrad  765:                CTASSERT(LINUX_CMSG_ALIGN_DELTA >= 0);
1.161   ! riastrad  766:                CTASSERT(CMSG_LEN(0) ==
        !           767:                    sizeof(linux_cmsg) + LINUX_CMSG_ALIGN_DELTA);
        !           768:                KASSERTMSG(cmsg->cmsg_len >= CMSG_LEN(0),
1.160     riastrad  769:                    "cmsg->cmsg_len=%u", cmsg->cmsg_len);
1.76      dsl       770:                linux_cmsg.cmsg_len = cmsg->cmsg_len - LINUX_CMSG_ALIGN_DELTA;
1.161   ! riastrad  771:                KASSERTMSG(linux_cmsg.cmsg_len >= sizeof(linux_cmsg),
        !           772:                    "linux_cmsg.cmsg_len=%zu", linux_cmsg.cmsg_len);
1.76      dsl       773:                linux_cmsg.cmsg_level = cmsg->cmsg_level;
                    774:                linux_cmsg.cmsg_type = cmsg->cmsg_type;
                    775: 
                    776:                dlen = q_end - q;
                    777:                if (linux_cmsg.cmsg_len > dlen) {
                    778:                        /* Not enough room for the parameter */
1.161   ! riastrad  779:                        if (dlen < sizeof linux_cmsg)
1.76      dsl       780:                                /* Discard if header wont fit */
                    781:                                break;
1.161   ! riastrad  782:                        dlen -= sizeof linux_cmsg;
1.76      dsl       783:                        mp->msg_flags |= MSG_CTRUNC;
                    784:                        if (linux_cmsg.cmsg_level == SOL_SOCKET
                    785:                            && linux_cmsg.cmsg_type == SCM_RIGHTS)
                    786:                                /* Do not truncate me ... */
                    787:                                break;
                    788:                } else
                    789:                        dlen = linux_cmsg.cmsg_len - sizeof linux_cmsg;
                    790: 
1.161   ! riastrad  791:                /*
        !           792:                 * NetBSD will not create a cmsg record whose header
        !           793:                 * length plus aligned content length would overflow
        !           794:                 * socklen_t.  This justifies computing
        !           795:                 * LINUX_CMSG_SPACE(dlen) (and LINUX_CMSG_LEN) below
        !           796:                 * without overflow.
        !           797:                 */
        !           798:                CTASSERT(LINUX_CMSG_SPACE(1) <= CMSG_SPACE(1));
        !           799:                CTASSERT(CMSG_SPACE(0) <=
        !           800:                    __type_max(__typeof(cmsg->cmsg_len)));
        !           801:                KASSERTMSG((dlen <= __type_max(__typeof(dlen)) -
        !           802:                        CMSG_SPACE(0)),
        !           803:                    "dlen=%u", dlen);
        !           804:                CTASSERT(LINUX_CMSG_LEN(1) <= LINUX_CMSG_SPACE(1));
        !           805: 
1.76      dsl       806:                switch (linux_cmsg.cmsg_level) {
                    807:                case SOL_SOCKET:
                    808:                        linux_cmsg.cmsg_level = LINUX_SOL_SOCKET;
                    809:                        switch (linux_cmsg.cmsg_type) {
                    810:                        case SCM_RIGHTS:
                    811:                                /* Linux SCM_RIGHTS is same as NetBSD */
                    812:                                break;
                    813: 
                    814:                        default:
                    815:                                /* other types not supported */
                    816:                                error = EINVAL;
                    817:                                goto done;
                    818:                        }
1.109     wiz       819:                        /* machine dependent ! */
1.76      dsl       820:                        break;
                    821:                default:
                    822:                        /* pray and leave intact */
                    823:                        break;
                    824:                }
                    825: 
                    826:                /* There can be padding between the header and data... */
1.102     njoly     827:                error = copyout(&linux_cmsg, q, sizeof linux_cmsg);
1.76      dsl       828:                if (error != 0) {
                    829:                        error = copyout(CCMSG_DATA(cmsg), q + sizeof linux_cmsg,
                    830:                            dlen);
                    831:                }
                    832:                if (error != 0) {
                    833:                        /* We must free all the SCM_RIGHTS */
                    834:                        m = control;
                    835:                        break;
                    836:                }
                    837:                m = m->m_next;
1.161   ! riastrad  838:                if (m == NULL || LINUX_CMSG_SPACE(dlen) > q_end - q) {
1.103     njoly     839:                        q += LINUX_CMSG_LEN(dlen);
1.76      dsl       840:                        break;
                    841:                }
1.103     njoly     842:                q += LINUX_CMSG_SPACE(dlen);
1.76      dsl       843:        }
                    844: 
                    845:   done:
                    846:        free_control_mbuf(l, control, m);
                    847: 
                    848:        mp->msg_controllen = q - (char *)mp->msg_control;
                    849:        return error;
                    850: }
                    851: 
1.32      jdolecek  852: int
1.85      dsl       853: linux_sys_recvmsg(struct lwp *l, const struct linux_sys_recvmsg_args *uap, register_t *retval)
1.32      jdolecek  854: {
1.85      dsl       855:        /* {
1.32      jdolecek  856:                syscallarg(int) s;
1.104     njoly     857:                syscallarg(struct linux_msghdr *) msg;
1.32      jdolecek  858:                syscallarg(u_int) flags;
1.85      dsl       859:        } */
1.32      jdolecek  860:        struct msghdr   msg;
1.104     njoly     861:        struct linux_msghdr lmsg;
1.32      jdolecek  862:        int             error;
1.76      dsl       863:        struct mbuf     *from, *control;
1.32      jdolecek  864: 
1.104     njoly     865:        error = copyin(SCARG(uap, msg), &lmsg, sizeof(lmsg));
1.76      dsl       866:        if (error)
1.159     riastrad  867:                return error;
                    868:        error = linux_to_bsd_msghdr(&lmsg, &msg);
                    869:        if (error)
                    870:                return error;
1.49      jdolecek  871: 
1.76      dsl       872:        msg.msg_flags = linux_to_bsd_msg_flags(SCARG(uap, flags));
                    873:        if (msg.msg_flags < 0) {
1.49      jdolecek  874:                /* Some unsupported flag */
                    875:                return (EINVAL);
                    876:        }
1.76      dsl       877:        msg.msg_flags |= MSG_IOVUSRSPACE;
1.49      jdolecek  878: 
1.141     christos  879:        error = do_sys_recvmsg(l, SCARG(uap, s), &msg, &from,
1.76      dsl       880:            msg.msg_control != NULL ? &control : NULL, retval);
                    881:        if (error != 0)
                    882:                return error;
                    883: 
                    884:        if (msg.msg_control != NULL)
                    885:                error = linux_copyout_msg_control(l, &msg, control);
                    886: 
                    887:        if (error == 0 && from != 0) {
                    888:                mtod(from, struct osockaddr *)->sa_family =
                    889:                    bsd_to_linux_domain(mtod(from, struct sockaddr *)->sa_family);
                    890:                error = copyout_sockname(msg.msg_name, &msg.msg_namelen, 0,
                    891:                        from);
                    892:        } else
                    893:                msg.msg_namelen = 0;
                    894: 
                    895:        if (from != NULL)
                    896:                m_free(from);
                    897: 
                    898:        if (error == 0) {
                    899:                msg.msg_flags = bsd_to_linux_msg_flags(msg.msg_flags);
                    900:                if (msg.msg_flags < 0)
                    901:                        /* Some flag unsupported by Linux */
                    902:                        error = EINVAL;
1.101     njoly     903:                else {
                    904:                        ktrkuser("msghdr", &msg, sizeof(msg));
1.104     njoly     905:                        bsd_to_linux_msghdr(&msg, &lmsg);
                    906:                        error = copyout(&lmsg, SCARG(uap, msg), sizeof(lmsg));
1.101     njoly     907:                }
1.49      jdolecek  908:        }
                    909: 
1.32      jdolecek  910:        return (error);
1.1       fvdl      911: }
                    912: 
                    913: /*
                    914:  * Convert socket option level from Linux to NetBSD value. Only SOL_SOCKET
                    915:  * is different, the rest matches IPPROTO_* on both systems.
                    916:  */
1.2       mycroft   917: int
1.84      dsl       918: linux_to_bsd_sopt_level(int llevel)
1.1       fvdl      919: {
1.12      mycroft   920: 
1.1       fvdl      921:        switch (llevel) {
                    922:        case LINUX_SOL_SOCKET:
                    923:                return SOL_SOCKET;
1.3       mycroft   924:        case LINUX_SOL_IP:
                    925:                return IPPROTO_IP;
1.117     njoly     926: #ifdef INET6
                    927:        case LINUX_SOL_IPV6:
                    928:                return IPPROTO_IPV6;
                    929: #endif
1.3       mycroft   930:        case LINUX_SOL_TCP:
                    931:                return IPPROTO_TCP;
                    932:        case LINUX_SOL_UDP:
                    933:                return IPPROTO_UDP;
1.1       fvdl      934:        default:
                    935:                return -1;
                    936:        }
                    937: }
                    938: 
                    939: /*
                    940:  * Convert Linux socket level socket option numbers to NetBSD values.
                    941:  */
1.2       mycroft   942: int
1.84      dsl       943: linux_to_bsd_so_sockopt(int lopt)
1.1       fvdl      944: {
1.12      mycroft   945: 
1.1       fvdl      946:        switch (lopt) {
                    947:        case LINUX_SO_DEBUG:
                    948:                return SO_DEBUG;
                    949:        case LINUX_SO_REUSEADDR:
1.55      perry     950:                /*
1.152     christos  951:                 * Linux does not implement SO_REUSEPORT, but allows reuse of
                    952:                 * a host:port pair through SO_REUSEADDR even if the address
                    953:                 * is not a multicast-address. Effectively, this means that we
                    954:                 * should use SO_REUSEPORT to allow Linux applications to not
                    955:                 * exit with EADDRINUSE
1.39      jschauma  956:                 */
                    957:                return SO_REUSEPORT;
1.1       fvdl      958:        case LINUX_SO_TYPE:
                    959:                return SO_TYPE;
                    960:        case LINUX_SO_ERROR:
                    961:                return SO_ERROR;
                    962:        case LINUX_SO_DONTROUTE:
                    963:                return SO_DONTROUTE;
                    964:        case LINUX_SO_BROADCAST:
                    965:                return SO_BROADCAST;
                    966:        case LINUX_SO_SNDBUF:
                    967:                return SO_SNDBUF;
                    968:        case LINUX_SO_RCVBUF:
                    969:                return SO_RCVBUF;
                    970:        case LINUX_SO_KEEPALIVE:
                    971:                return SO_KEEPALIVE;
                    972:        case LINUX_SO_OOBINLINE:
                    973:                return SO_OOBINLINE;
1.152     christos  974:        case LINUX_SO_NO_CHECK:
                    975:        case LINUX_SO_PRIORITY:
                    976:                return -1;
1.1       fvdl      977:        case LINUX_SO_LINGER:
                    978:                return SO_LINGER;
1.152     christos  979:        case LINUX_SO_BSDCOMPAT:
                    980:        case LINUX_SO_PASSCRED:
                    981:        case LINUX_SO_PEERCRED:
                    982:                return -1;
                    983:        case LINUX_SO_RCVLOWAT:
                    984:                return SO_RCVLOWAT;
                    985:        case LINUX_SO_SNDLOWAT:
                    986:                return SO_SNDLOWAT;
                    987:        case LINUX_SO_RCVTIMEO:
                    988:                return SO_RCVTIMEO;
                    989:        case LINUX_SO_SNDTIMEO:
                    990:                return SO_SNDTIMEO;
                    991:        case LINUX_SO_SECURITY_AUTHENTICATION:
                    992:        case LINUX_SO_SECURITY_ENCRYPTION_TRANSPORT:
                    993:        case LINUX_SO_SECURITY_ENCRYPTION_NETWORK:
                    994:        case LINUX_SO_BINDTODEVICE:
                    995:        case LINUX_SO_ATTACH_FILTER:
                    996:        case LINUX_SO_DETACH_FILTER:
                    997:        case LINUX_SO_PEERNAME:
                    998:                return -1;
                    999:        case LINUX_SO_TIMESTAMP:
                   1000:                return SO_TIMESTAMP;
1.116     njoly    1001:        case LINUX_SO_ACCEPTCONN:
1.152     christos 1002:        case LINUX_SO_PEERSEC:
                   1003:        case LINUX_SO_SNDBUFFORCE:
                   1004:        case LINUX_SO_RCVBUFFORCE:
                   1005:        case LINUX_SO_PASSSEC:
                   1006:        case LINUX_SO_TIMESTAMPNS:
                   1007:        case LINUX_SO_MARK:
                   1008:        case LINUX_SO_TIMESTAMPING:
                   1009:        case LINUX_SO_PROTOCOL:
                   1010:        case LINUX_SO_DOMAIN:
                   1011:        case LINUX_SO_RXQ_OVFL:
                   1012:        case LINUX_SO_WIFI_STATUS:
                   1013:        case LINUX_SO_PEEK_OFF:
                   1014:        case LINUX_SO_NOFCS:
1.1       fvdl     1015:        default:
                   1016:                return -1;
                   1017:        }
                   1018: }
                   1019: 
                   1020: /*
1.3       mycroft  1021:  * Convert Linux IP level socket option number to NetBSD values.
                   1022:  */
                   1023: int
1.84      dsl      1024: linux_to_bsd_ip_sockopt(int lopt)
1.3       mycroft  1025: {
1.12      mycroft  1026: 
1.3       mycroft  1027:        switch (lopt) {
                   1028:        case LINUX_IP_TOS:
                   1029:                return IP_TOS;
                   1030:        case LINUX_IP_TTL:
                   1031:                return IP_TTL;
1.158     christos 1032:        case LINUX_IP_RETOPTS:
                   1033:                return IP_RETOPTS;
                   1034:        case LINUX_IP_PKTINFO:
                   1035:                return IP_PKTINFO;
                   1036:        case LINUX_IP_RECVOPTS:
                   1037:                return IP_RECVOPTS;
1.107     dsl      1038:        case LINUX_IP_HDRINCL:
                   1039:                return IP_HDRINCL;
1.3       mycroft  1040:        case LINUX_IP_MULTICAST_TTL:
                   1041:                return IP_MULTICAST_TTL;
                   1042:        case LINUX_IP_MULTICAST_LOOP:
                   1043:                return IP_MULTICAST_LOOP;
                   1044:        case LINUX_IP_MULTICAST_IF:
                   1045:                return IP_MULTICAST_IF;
                   1046:        case LINUX_IP_ADD_MEMBERSHIP:
                   1047:                return IP_ADD_MEMBERSHIP;
                   1048:        case LINUX_IP_DROP_MEMBERSHIP:
                   1049:                return IP_DROP_MEMBERSHIP;
1.154     ryo      1050:        case LINUX_IP_RECVERR:
1.158     christos 1051:        case LINUX_IP_FREEBIND:
1.154     ryo      1052:                return -2;      /* ignored */
1.158     christos 1053:        case LINUX_IP_MULTICAST_ALL:
                   1054:                return -3;      /* noprotoopt */
1.3       mycroft  1055:        default:
                   1056:                return -1;
                   1057:        }
                   1058: }
                   1059: 
                   1060: /*
1.117     njoly    1061:  * Convert Linux IPV6 level socket option number to NetBSD values.
                   1062:  */
                   1063: #ifdef INET6
                   1064: int
                   1065: linux_to_bsd_ipv6_sockopt(int lopt)
                   1066: {
                   1067: 
                   1068:        switch (lopt) {
                   1069:        case LINUX_IPV6_V6ONLY:
                   1070:                return IPV6_V6ONLY;
1.158     christos 1071:        case LINUX_IPV6_MULTICAST_HOPS:
                   1072:                return IPV6_MULTICAST_HOPS;
                   1073:        case LINUX_IPV6_MULTICAST_ALL:
                   1074:                return -3;      /* noprotoopt */
1.117     njoly    1075:        default:
                   1076:                return -1;
                   1077:        }
                   1078: }
                   1079: #endif
                   1080: 
                   1081: /*
1.3       mycroft  1082:  * Convert Linux TCP level socket option number to NetBSD values.
                   1083:  */
                   1084: int
1.84      dsl      1085: linux_to_bsd_tcp_sockopt(int lopt)
1.3       mycroft  1086: {
1.12      mycroft  1087: 
1.3       mycroft  1088:        switch (lopt) {
1.6       mycroft  1089:        case LINUX_TCP_NODELAY:
                   1090:                return TCP_NODELAY;
                   1091:        case LINUX_TCP_MAXSEG:
                   1092:                return TCP_MAXSEG;
1.3       mycroft  1093:        default:
1.6       mycroft  1094:                return -1;
1.3       mycroft  1095:        }
                   1096: }
                   1097: 
                   1098: /*
                   1099:  * Convert Linux UDP level socket option number to NetBSD values.
                   1100:  */
                   1101: int
1.84      dsl      1102: linux_to_bsd_udp_sockopt(int lopt)
1.3       mycroft  1103: {
1.12      mycroft  1104: 
1.3       mycroft  1105:        switch (lopt) {
                   1106:        default:
1.6       mycroft  1107:                return -1;
1.3       mycroft  1108:        }
                   1109: }
                   1110: 
                   1111: /*
1.5       mycroft  1112:  * Another reasonably straightforward function: setsockopt(2).
1.1       fvdl     1113:  * The level and option numbers are converted; the values passed
                   1114:  * are not (yet) converted, the ones currently implemented don't
                   1115:  * need conversion, as they are the same on both systems.
                   1116:  */
1.2       mycroft  1117: int
1.85      dsl      1118: linux_sys_setsockopt(struct lwp *l, const struct linux_sys_setsockopt_args *uap, register_t *retval)
1.16      erh      1119: {
1.85      dsl      1120:        /* {
1.1       fvdl     1121:                syscallarg(int) s;
                   1122:                syscallarg(int) level;
                   1123:                syscallarg(int) optname;
                   1124:                syscallarg(void *) optval;
                   1125:                syscallarg(int) optlen;
1.85      dsl      1126:        } */
1.12      mycroft  1127:        struct sys_setsockopt_args bsa;
1.16      erh      1128:        int name;
1.1       fvdl     1129: 
1.16      erh      1130:        SCARG(&bsa, s) = SCARG(uap, s);
                   1131:        SCARG(&bsa, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
                   1132:        SCARG(&bsa, val) = SCARG(uap, optval);
                   1133:        SCARG(&bsa, valsize) = SCARG(uap, optlen);
1.1       fvdl     1134: 
1.70      mlelstv  1135:        /*
                   1136:         * Linux supports only SOL_SOCKET for AF_LOCAL domain sockets
                   1137:         * and returns EOPNOTSUPP for other levels
                   1138:         */
                   1139:        if (SCARG(&bsa, level) != SOL_SOCKET) {
                   1140:                struct socket *so;
1.89      ad       1141:                int error, family;
1.70      mlelstv  1142: 
1.96      ad       1143:                /* fd_getsock() will use the descriptor for us */
1.89      ad       1144:                if ((error = fd_getsock(SCARG(&bsa, s), &so)) != 0)
1.70      mlelstv  1145:                        return error;
                   1146:                family = so->so_proto->pr_domain->dom_family;
1.87      ad       1147:                fd_putfile(SCARG(&bsa, s));
1.70      mlelstv  1148: 
                   1149:                if (family == AF_LOCAL)
                   1150:                        return EOPNOTSUPP;
                   1151:        }
                   1152: 
1.1       fvdl     1153:        switch (SCARG(&bsa, level)) {
1.40      itojun   1154:        case SOL_SOCKET:
                   1155:                name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
                   1156:                break;
                   1157:        case IPPROTO_IP:
                   1158:                name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
                   1159:                break;
1.117     njoly    1160: #ifdef INET6
                   1161:        case IPPROTO_IPV6:
                   1162:                name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname));
                   1163:                break;
                   1164: #endif
1.40      itojun   1165:        case IPPROTO_TCP:
                   1166:                name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
                   1167:                break;
                   1168:        case IPPROTO_UDP:
                   1169:                name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
                   1170:                break;
                   1171:        default:
                   1172:                return EINVAL;
1.1       fvdl     1173:        }
                   1174: 
1.158     christos 1175:        switch (name) {
                   1176:        case -1:
1.1       fvdl     1177:                return EINVAL;
1.158     christos 1178:        case -2:
1.154     ryo      1179:                return 0;
1.158     christos 1180:        case -3:
                   1181:                return ENOPROTOOPT;
                   1182:        }
1.1       fvdl     1183:        SCARG(&bsa, name) = name;
                   1184: 
1.43      thorpej  1185:        return sys_setsockopt(l, &bsa, retval);
1.1       fvdl     1186: }
                   1187: 
                   1188: /*
                   1189:  * getsockopt(2) is very much the same as setsockopt(2) (see above)
                   1190:  */
1.2       mycroft  1191: int
1.85      dsl      1192: linux_sys_getsockopt(struct lwp *l, const struct linux_sys_getsockopt_args *uap, register_t *retval)
1.16      erh      1193: {
1.85      dsl      1194:        /* {
1.1       fvdl     1195:                syscallarg(int) s;
                   1196:                syscallarg(int) level;
                   1197:                syscallarg(int) optname;
                   1198:                syscallarg(void *) optval;
1.16      erh      1199:                syscallarg(int *) optlen;
1.85      dsl      1200:        } */
1.12      mycroft  1201:        struct sys_getsockopt_args bga;
1.16      erh      1202:        int name;
1.1       fvdl     1203: 
1.16      erh      1204:        SCARG(&bga, s) = SCARG(uap, s);
                   1205:        SCARG(&bga, level) = linux_to_bsd_sopt_level(SCARG(uap, level));
                   1206:        SCARG(&bga, val) = SCARG(uap, optval);
1.61      mrg      1207:        SCARG(&bga, avalsize) = (socklen_t *)SCARG(uap, optlen);
1.1       fvdl     1208: 
                   1209:        switch (SCARG(&bga, level)) {
1.40      itojun   1210:        case SOL_SOCKET:
                   1211:                name = linux_to_bsd_so_sockopt(SCARG(uap, optname));
                   1212:                break;
                   1213:        case IPPROTO_IP:
                   1214:                name = linux_to_bsd_ip_sockopt(SCARG(uap, optname));
                   1215:                break;
1.117     njoly    1216: #ifdef INET6
                   1217:        case IPPROTO_IPV6:
                   1218:                name = linux_to_bsd_ipv6_sockopt(SCARG(uap, optname));
                   1219:                break;
                   1220: #endif
1.40      itojun   1221:        case IPPROTO_TCP:
                   1222:                name = linux_to_bsd_tcp_sockopt(SCARG(uap, optname));
                   1223:                break;
                   1224:        case IPPROTO_UDP:
                   1225:                name = linux_to_bsd_udp_sockopt(SCARG(uap, optname));
                   1226:                break;
                   1227:        default:
                   1228:                return EINVAL;
1.1       fvdl     1229:        }
                   1230: 
1.158     christos 1231:        switch (name) {
                   1232:        case -1:
                   1233:        case -2: /* we can't ignore, since we don't know what to return */
1.1       fvdl     1234:                return EINVAL;
1.158     christos 1235:        case -3:
                   1236:                return ENOPROTOOPT;
                   1237:        }
1.5       mycroft  1238:        SCARG(&bga, name) = name;
1.1       fvdl     1239: 
1.43      thorpej  1240:        return sys_getsockopt(l, &bga, retval);
1.13      mycroft  1241: }
                   1242: 
1.97      njoly    1243: int
1.105     joerg    1244: linux_getifname(struct lwp *l, register_t *retval, void *data)
                   1245: {
                   1246:        struct ifnet *ifp;
                   1247:        struct linux_ifreq ifr;
                   1248:        int error;
1.128     ozaki-r  1249:        int s;
1.105     joerg    1250: 
                   1251:        error = copyin(data, &ifr, sizeof(ifr));
                   1252:        if (error)
                   1253:                return error;
                   1254: 
1.128     ozaki-r  1255:        s = pserialize_read_enter();
1.118     rmind    1256:        ifp = if_byindex(ifr.ifr_ifru.ifru_ifindex);
1.128     ozaki-r  1257:        if (ifp == NULL) {
                   1258:                pserialize_read_exit(s);
1.105     joerg    1259:                return ENODEV;
1.128     ozaki-r  1260:        }
1.105     joerg    1261: 
                   1262:        strncpy(ifr.ifr_name, ifp->if_xname, sizeof(ifr.ifr_name));
1.128     ozaki-r  1263:        pserialize_read_exit(s);
1.105     joerg    1264: 
1.106     joerg    1265:        return copyout(&ifr, data, sizeof(ifr));
1.105     joerg    1266: }
                   1267: 
                   1268: int
1.97      njoly    1269: linux_getifconf(struct lwp *l, register_t *retval, void *data)
                   1270: {
1.122     ozaki-r  1271:        struct linux_ifreq ifr, *ifrp = NULL;
1.115     christos 1272:        struct linux_ifconf ifc;
1.97      njoly    1273:        struct ifnet *ifp;
                   1274:        struct sockaddr *sa;
                   1275:        struct osockaddr *osa;
1.126     maxv     1276:        int space = 0, error;
1.97      njoly    1277:        const int sz = (int)sizeof(ifr);
1.122     ozaki-r  1278:        bool docopy;
1.127     ozaki-r  1279:        int s;
1.129     ozaki-r  1280:        int bound;
1.127     ozaki-r  1281:        struct psref psref;
1.97      njoly    1282: 
1.115     christos 1283:        error = copyin(data, &ifc, sizeof(ifc));
                   1284:        if (error)
                   1285:                return error;
                   1286: 
1.122     ozaki-r  1287:        docopy = ifc.ifc_req != NULL;
                   1288:        if (docopy) {
1.150     msaitoh  1289:                if (ifc.ifc_len < 0)
                   1290:                        return EINVAL;
                   1291: 
1.115     christos 1292:                space = ifc.ifc_len;
1.122     ozaki-r  1293:                ifrp = ifc.ifc_req;
                   1294:        }
1.150     msaitoh  1295:        memset(&ifr, 0, sizeof(ifr));
1.97      njoly    1296: 
1.129     ozaki-r  1297:        bound = curlwp_bind();
1.127     ozaki-r  1298:        s = pserialize_read_enter();
                   1299:        IFNET_READER_FOREACH(ifp) {
1.132     ozaki-r  1300:                struct ifaddr *ifa;
1.138     ozaki-r  1301:                if_acquire(ifp, &psref);
1.139     ozaki-r  1302:                pserialize_read_exit(s);
1.127     ozaki-r  1303: 
1.97      njoly    1304:                (void)strncpy(ifr.ifr_name, ifp->if_xname,
                   1305:                    sizeof(ifr.ifr_name));
1.127     ozaki-r  1306:                if (ifr.ifr_name[sizeof(ifr.ifr_name) - 1] != '\0') {
                   1307:                        error = ENAMETOOLONG;
                   1308:                        goto release_exit;
                   1309:                }
1.130     ozaki-r  1310: 
1.139     ozaki-r  1311:                s = pserialize_read_enter();
1.131     ozaki-r  1312:                IFADDR_READER_FOREACH(ifa, ifp) {
1.132     ozaki-r  1313:                        struct psref psref_ifa;
                   1314:                        ifa_acquire(ifa, &psref_ifa);
                   1315:                        pserialize_read_exit(s);
                   1316: 
1.97      njoly    1317:                        sa = ifa->ifa_addr;
                   1318:                        if (sa->sa_family != AF_INET ||
                   1319:                            sa->sa_len > sizeof(*osa))
1.132     ozaki-r  1320:                                goto next;
1.97      njoly    1321:                        memcpy(&ifr.ifr_addr, sa, sa->sa_len);
                   1322:                        osa = (struct osockaddr *)&ifr.ifr_addr;
                   1323:                        osa->sa_family = sa->sa_family;
                   1324:                        if (space >= sz) {
                   1325:                                error = copyout(&ifr, ifrp, sz);
1.132     ozaki-r  1326:                                if (error != 0) {
                   1327:                                        ifa_release(ifa, &psref_ifa);
1.127     ozaki-r  1328:                                        goto release_exit;
1.132     ozaki-r  1329:                                }
1.97      njoly    1330:                                ifrp++;
                   1331:                        }
                   1332:                        space -= sz;
1.132     ozaki-r  1333:                next:
                   1334:                        s = pserialize_read_enter();
                   1335:                        ifa_release(ifa, &psref_ifa);
1.97      njoly    1336:                }
1.127     ozaki-r  1337: 
1.142     ozaki-r  1338:                KASSERT(pserialize_in_read_section());
1.138     ozaki-r  1339:                if_release(ifp, &psref);
1.97      njoly    1340:        }
1.127     ozaki-r  1341:        pserialize_read_exit(s);
1.129     ozaki-r  1342:        curlwp_bindx(bound);
1.97      njoly    1343: 
1.122     ozaki-r  1344:        if (docopy)
1.115     christos 1345:                ifc.ifc_len -= space;
1.97      njoly    1346:        else
1.115     christos 1347:                ifc.ifc_len = -space;
1.97      njoly    1348: 
1.115     christos 1349:        return copyout(&ifc, data, sizeof(ifc));
1.127     ozaki-r  1350: 
                   1351: release_exit:
1.138     ozaki-r  1352:        if_release(ifp, &psref);
1.129     ozaki-r  1353:        curlwp_bindx(bound);
1.127     ozaki-r  1354:        return error;
1.97      njoly    1355: }
1.21      jtk      1356: 
                   1357: int
1.68      christos 1358: linux_getifhwaddr(struct lwp *l, register_t *retval, u_int fd,
1.67      christos 1359:     void *data)
1.21      jtk      1360: {
                   1361:        /* Not the full structure, just enough to map what we do here */
1.97      njoly    1362:        struct linux_ifreq lreq;
1.87      ad       1363:        file_t *fp;
1.21      jtk      1364:        struct ifaddr *ifa;
                   1365:        struct ifnet *ifp;
                   1366:        struct sockaddr_dl *sadl;
                   1367:        int error, found;
                   1368:        int index, ifnum;
1.127     ozaki-r  1369:        int s;
1.21      jtk      1370: 
                   1371:        /*
                   1372:         * We can't emulate this ioctl by calling sys_ioctl() to run
                   1373:         * SIOCGIFCONF, because the user buffer is not of the right
                   1374:         * type to take those results.  We can't use kernel buffers to
                   1375:         * receive the results, as the implementation of sys_ioctl()
                   1376:         * and ifconf() [which implements SIOCGIFCONF] use
                   1377:         * copyin()/copyout() which will fail on kernel addresses.
                   1378:         *
                   1379:         * So, we must duplicate code from sys_ioctl() and ifconf().  Ugh.
                   1380:         */
                   1381: 
1.87      ad       1382:        if ((fp = fd_getfile(fd)) == NULL)
1.21      jtk      1383:                return (EBADF);
                   1384: 
1.90      ad       1385:        KERNEL_LOCK(1, NULL);
                   1386: 
1.21      jtk      1387:        if ((fp->f_flag & (FREAD | FWRITE)) == 0) {
                   1388:                error = EBADF;
                   1389:                goto out;
                   1390:        }
                   1391: 
1.73      dsl      1392:        error = copyin(data, &lreq, sizeof(lreq));
1.21      jtk      1393:        if (error)
                   1394:                goto out;
1.97      njoly    1395:        lreq.ifr_name[LINUX_IFNAMSIZ-1] = '\0';         /* just in case */
1.21      jtk      1396: 
                   1397:        /*
1.22      jdolecek 1398:         * Try real interface name first, then fake "ethX"
1.21      jtk      1399:         */
1.82      dyoung   1400:        found = 0;
1.127     ozaki-r  1401:        s = pserialize_read_enter();
                   1402:        IFNET_READER_FOREACH(ifp) {
1.82      dyoung   1403:                if (found)
                   1404:                        break;
1.97      njoly    1405:                if (strcmp(lreq.ifr_name, ifp->if_xname))
1.22      jdolecek 1406:                        /* not this interface */
                   1407:                        continue;
1.127     ozaki-r  1408: 
1.55      perry    1409:                found=1;
1.131     ozaki-r  1410:                if (IFADDR_READER_EMPTY(ifp)) {
1.127     ozaki-r  1411:                        pserialize_read_exit(s);
1.22      jdolecek 1412:                        error = ENODEV;
                   1413:                        goto out;
                   1414:                }
1.131     ozaki-r  1415:                IFADDR_READER_FOREACH(ifa, ifp) {
1.82      dyoung   1416:                        sadl = satosdl(ifa->ifa_addr);
                   1417:                        /* only return ethernet addresses */
                   1418:                        /* XXX what about FDDI, etc. ? */
                   1419:                        if (sadl->sdl_family != AF_LINK ||
                   1420:                            sadl->sdl_type != IFT_ETHER)
                   1421:                                continue;
1.97      njoly    1422:                        memcpy(&lreq.ifr_hwaddr.sa_data, CLLADDR(sadl),
1.82      dyoung   1423:                               MIN(sadl->sdl_alen,
1.97      njoly    1424:                                   sizeof(lreq.ifr_hwaddr.sa_data)));
                   1425:                        lreq.ifr_hwaddr.sa_family =
1.82      dyoung   1426:                                sadl->sdl_family;
1.127     ozaki-r  1427:                        pserialize_read_exit(s);
                   1428: 
1.82      dyoung   1429:                        error = copyout(&lreq, data, sizeof(lreq));
                   1430:                        goto out;
                   1431:                }
1.22      jdolecek 1432:        }
1.127     ozaki-r  1433:        pserialize_read_exit(s);
1.22      jdolecek 1434: 
1.121     ozaki-r  1435:        if (strncmp(lreq.ifr_name, "eth", 3) != 0) {
                   1436:                /* unknown interface, not even an "eth*" name */
                   1437:                error = ENODEV;
                   1438:                goto out;
                   1439:        }
                   1440: 
                   1441:        for (ifnum = 0, index = 3;
                   1442:             index < LINUX_IFNAMSIZ && lreq.ifr_name[index] != '\0';
                   1443:             index++) {
                   1444:                ifnum *= 10;
                   1445:                ifnum += lreq.ifr_name[index] - '0';
                   1446:        }
1.21      jtk      1447: 
1.121     ozaki-r  1448:        error = EINVAL;                 /* in case we don't find one */
1.127     ozaki-r  1449:        s = pserialize_read_enter();
                   1450:        IFNET_READER_FOREACH(ifp) {
1.121     ozaki-r  1451:                memcpy(lreq.ifr_name, ifp->if_xname,
                   1452:                       MIN(LINUX_IFNAMSIZ, IFNAMSIZ));
1.131     ozaki-r  1453:                IFADDR_READER_FOREACH(ifa, ifp) {
1.121     ozaki-r  1454:                        sadl = satosdl(ifa->ifa_addr);
                   1455:                        /* only return ethernet addresses */
                   1456:                        /* XXX what about FDDI, etc. ? */
                   1457:                        if (sadl->sdl_family != AF_LINK ||
                   1458:                            sadl->sdl_type != IFT_ETHER)
                   1459:                                continue;
                   1460:                        if (ifnum--)
1.155     andvar   1461:                                /* not the requested iface */
1.121     ozaki-r  1462:                                continue;
                   1463:                        memcpy(&lreq.ifr_hwaddr.sa_data,
                   1464:                               CLLADDR(sadl),
                   1465:                               MIN(sadl->sdl_alen,
                   1466:                                   sizeof(lreq.ifr_hwaddr.sa_data)));
                   1467:                        lreq.ifr_hwaddr.sa_family =
                   1468:                                sadl->sdl_family;
1.127     ozaki-r  1469:                        pserialize_read_exit(s);
                   1470: 
1.121     ozaki-r  1471:                        error = copyout(&lreq, data, sizeof(lreq));
1.127     ozaki-r  1472:                        goto out;
1.21      jtk      1473:                }
1.22      jdolecek 1474:        }
1.127     ozaki-r  1475:        pserialize_read_exit(s);
1.55      perry    1476: 
1.21      jtk      1477: out:
1.90      ad       1478:        KERNEL_UNLOCK_ONE(NULL);
1.87      ad       1479:        fd_putfile(fd);
1.21      jtk      1480:        return error;
                   1481: }
                   1482: 
1.13      mycroft  1483: int
1.85      dsl      1484: linux_ioctl_socket(struct lwp *l, const struct linux_sys_ioctl_args *uap, register_t *retval)
                   1485: {
                   1486:        /* {
1.13      mycroft  1487:                syscallarg(int) fd;
                   1488:                syscallarg(u_long) com;
1.71      christos 1489:                syscallarg(void *) data;
1.85      dsl      1490:        } */
1.13      mycroft  1491:        u_long com;
1.25      fvdl     1492:        int error = 0, isdev = 0, dosys = 1;
1.13      mycroft  1493:        struct sys_ioctl_args ia;
1.87      ad       1494:        file_t *fp;
1.25      fvdl     1495:        struct vnode *vp;
1.87      ad       1496:        int (*ioctlf)(file_t *, u_long, void *);
1.25      fvdl     1497:        struct ioctl_pt pt;
                   1498: 
1.87      ad       1499:        if ((fp = fd_getfile(SCARG(uap, fd))) == NULL)
1.25      fvdl     1500:                return (EBADF);
                   1501: 
                   1502:        if (fp->f_type == DTYPE_VNODE) {
                   1503:                vp = (struct vnode *)fp->f_data;
                   1504:                isdev = vp->v_type == VCHR;
                   1505:        }
                   1506: 
                   1507:        /*
                   1508:         * Don't try to interpret socket ioctl calls that are done
                   1509:         * on a device filedescriptor, just pass them through, to
                   1510:         * emulate Linux behaviour. Use PTIOCLINUX so that the
                   1511:         * device will only handle these if it's prepared to do
                   1512:         * so, to avoid unexpected things from happening.
                   1513:         */
                   1514:        if (isdev) {
1.26      fvdl     1515:                dosys = 0;
1.25      fvdl     1516:                ioctlf = fp->f_ops->fo_ioctl;
                   1517:                pt.com = SCARG(uap, com);
                   1518:                pt.data = SCARG(uap, data);
1.87      ad       1519:                error = ioctlf(fp, PTIOCLINUX, &pt);
1.27      fvdl     1520:                /*
1.55      perry    1521:                 * XXX hack: if the function returns EJUSTRETURN,
1.27      fvdl     1522:                 * it has stuffed a sysctl return value in pt.data.
                   1523:                 */
                   1524:                if (error == EJUSTRETURN) {
                   1525:                        retval[0] = (register_t)pt.data;
                   1526:                        error = 0;
                   1527:                }
1.25      fvdl     1528:                goto out;
                   1529:        }
1.13      mycroft  1530: 
                   1531:        com = SCARG(uap, com);
                   1532:        retval[0] = 0;
                   1533: 
                   1534:        switch (com) {
1.105     joerg    1535:        case LINUX_SIOCGIFNAME:
                   1536:                error = linux_getifname(l, retval, SCARG(uap, data));
                   1537:                dosys = 0;
                   1538:                break;
1.13      mycroft  1539:        case LINUX_SIOCGIFCONF:
1.97      njoly    1540:                error = linux_getifconf(l, retval, SCARG(uap, data));
                   1541:                dosys = 0;
1.13      mycroft  1542:                break;
                   1543:        case LINUX_SIOCGIFFLAGS:
1.72      christos 1544:                SCARG(&ia, com) = OSIOCGIFFLAGS;
1.13      mycroft  1545:                break;
1.25      fvdl     1546:        case LINUX_SIOCSIFFLAGS:
1.72      christos 1547:                SCARG(&ia, com) = OSIOCSIFFLAGS;
1.25      fvdl     1548:                break;
1.13      mycroft  1549:        case LINUX_SIOCGIFADDR:
1.72      christos 1550:                SCARG(&ia, com) = OOSIOCGIFADDR;
1.13      mycroft  1551:                break;
                   1552:        case LINUX_SIOCGIFDSTADDR:
1.72      christos 1553:                SCARG(&ia, com) = OOSIOCGIFDSTADDR;
1.13      mycroft  1554:                break;
                   1555:        case LINUX_SIOCGIFBRDADDR:
1.72      christos 1556:                SCARG(&ia, com) = OOSIOCGIFBRDADDR;
1.13      mycroft  1557:                break;
                   1558:        case LINUX_SIOCGIFNETMASK:
1.72      christos 1559:                SCARG(&ia, com) = OOSIOCGIFNETMASK;
1.13      mycroft  1560:                break;
1.107     dsl      1561:        case LINUX_SIOCGIFMTU:
                   1562:                SCARG(&ia, com) = OSIOCGIFMTU;
                   1563:                break;
1.13      mycroft  1564:        case LINUX_SIOCADDMULTI:
1.72      christos 1565:                SCARG(&ia, com) = OSIOCADDMULTI;
1.13      mycroft  1566:                break;
                   1567:        case LINUX_SIOCDELMULTI:
1.72      christos 1568:                SCARG(&ia, com) = OSIOCDELMULTI;
1.13      mycroft  1569:                break;
1.21      jtk      1570:        case LINUX_SIOCGIFHWADDR:
1.73      dsl      1571:                error = linux_getifhwaddr(l, retval, SCARG(uap, fd),
1.87      ad       1572:                    SCARG(uap, data));
1.29      fvdl     1573:                dosys = 0;
1.25      fvdl     1574:                break;
1.13      mycroft  1575:        default:
1.25      fvdl     1576:                error = EINVAL;
                   1577:        }
                   1578: 
1.87      ad       1579:  out:
                   1580:        fd_putfile(SCARG(uap, fd));
1.25      fvdl     1581: 
                   1582:        if (error ==0 && dosys) {
                   1583:                SCARG(&ia, fd) = SCARG(uap, fd);
                   1584:                SCARG(&ia, data) = SCARG(uap, data);
1.43      thorpej  1585:                error = sys_ioctl(curlwp, &ia, retval);
1.13      mycroft  1586:        }
                   1587: 
1.25      fvdl     1588:        return error;
1.1       fvdl     1589: }
1.19      sommerfe 1590: 
                   1591: int
1.85      dsl      1592: linux_sys_connect(struct lwp *l, const struct linux_sys_connect_args *uap, register_t *retval)
1.19      sommerfe 1593: {
1.85      dsl      1594:        /* {
1.19      sommerfe 1595:                syscallarg(int) s;
                   1596:                syscallarg(const struct sockaddr *) name;
1.32      jdolecek 1597:                syscallarg(int) namelen;
1.85      dsl      1598:        } */
1.32      jdolecek 1599:        int             error;
1.124     rtr      1600:        struct sockaddr_big sb;
1.73      dsl      1601: 
1.125     rtr      1602:        error = linux_get_sa(l, SCARG(uap, s), &sb, SCARG(uap, name),
1.73      dsl      1603:            SCARG(uap, namelen));
1.32      jdolecek 1604:        if (error)
                   1605:                return (error);
1.55      perry    1606: 
1.124     rtr      1607:        error = do_sys_connect(l, SCARG(uap, s), (struct sockaddr *)&sb);
1.19      sommerfe 1608: 
                   1609:        if (error == EISCONN) {
1.23      augustss 1610:                struct socket *so;
1.111     christos 1611:                int state, prflags;
1.55      perry    1612: 
1.96      ad       1613:                /* fd_getsock() will use the descriptor for us */
1.89      ad       1614:                if (fd_getsock(SCARG(uap, s), &so) != 0)
1.19      sommerfe 1615:                        return EISCONN;
                   1616: 
1.91      ad       1617:                solock(so);
1.19      sommerfe 1618:                state = so->so_state;
                   1619:                prflags = so->so_proto->pr_flags;
1.91      ad       1620:                sounlock(so);
1.87      ad       1621:                fd_putfile(SCARG(uap, s));
1.19      sommerfe 1622:                /*
                   1623:                 * We should only let this call succeed once per
                   1624:                 * non-blocking connect; however we don't have
                   1625:                 * a convenient place to keep that state..
                   1626:                 */
1.111     christos 1627:                if ((state & (SS_ISCONNECTED|SS_NBIO)) ==
                   1628:                    (SS_ISCONNECTED|SS_NBIO) &&
1.19      sommerfe 1629:                    (prflags & PR_CONNREQUIRED))
                   1630:                        return 0;
                   1631:        }
                   1632: 
1.32      jdolecek 1633:        return (error);
                   1634: }
                   1635: 
                   1636: int
1.85      dsl      1637: linux_sys_bind(struct lwp *l, const struct linux_sys_bind_args *uap, register_t *retval)
1.32      jdolecek 1638: {
1.85      dsl      1639:        /* {
1.32      jdolecek 1640:                syscallarg(int) s;
                   1641:                syscallarg(const struct osockaddr *) name;
                   1642:                syscallarg(int) namelen;
1.85      dsl      1643:        } */
1.61      mrg      1644:        int             error;
1.123     rtr      1645:        struct sockaddr_big sb;
1.32      jdolecek 1646: 
1.125     rtr      1647:        error = linux_get_sa(l, SCARG(uap, s), &sb, SCARG(uap, name),
1.73      dsl      1648:            SCARG(uap, namelen));
                   1649:        if (error)
                   1650:                return (error);
1.32      jdolecek 1651: 
1.123     rtr      1652:        return do_sys_bind(l, SCARG(uap, s), (struct sockaddr *)&sb);
1.32      jdolecek 1653: }
                   1654: 
                   1655: int
1.85      dsl      1656: linux_sys_getsockname(struct lwp *l, const struct linux_sys_getsockname_args *uap, register_t *retval)
1.32      jdolecek 1657: {
1.85      dsl      1658:        /* {
1.32      jdolecek 1659:                syscallarg(int) fdes;
1.71      christos 1660:                syscallarg(void *) asa;
1.32      jdolecek 1661:                syscallarg(int *) alen;
1.85      dsl      1662:        } */
1.32      jdolecek 1663:        int error;
                   1664: 
1.85      dsl      1665:        if ((error = sys_getsockname(l, (const void *)uap, retval)) != 0)
1.32      jdolecek 1666:                return (error);
                   1667: 
                   1668:        if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
                   1669:                return (error);
                   1670: 
                   1671:        return (0);
                   1672: }
                   1673: 
                   1674: int
1.85      dsl      1675: linux_sys_getpeername(struct lwp *l, const struct linux_sys_getpeername_args *uap, register_t *retval)
1.32      jdolecek 1676: {
1.85      dsl      1677:        /* {
1.32      jdolecek 1678:                syscallarg(int) fdes;
1.71      christos 1679:                syscallarg(void *) asa;
1.32      jdolecek 1680:                syscallarg(int *) alen;
1.85      dsl      1681:        } */
1.32      jdolecek 1682:        int error;
                   1683: 
1.85      dsl      1684:        if ((error = sys_getpeername(l, (const void *)uap, retval)) != 0)
1.32      jdolecek 1685:                return (error);
                   1686: 
                   1687:        if ((error = linux_sa_put((struct osockaddr *)SCARG(uap, asa))))
                   1688:                return (error);
                   1689: 
                   1690:        return (0);
                   1691: }
                   1692: 
1.125     rtr      1693: /*
                   1694:  * Copy the osockaddr structure pointed to by name to sb, adjust
                   1695:  * family and convert to sockaddr.
                   1696:  */
1.123     rtr      1697: static int
1.125     rtr      1698: linux_get_sa(struct lwp *l, int s, struct sockaddr_big *sb,
1.123     rtr      1699:     const struct osockaddr *name, socklen_t namelen)
                   1700: {
                   1701:        int error, bdom;
                   1702: 
                   1703:        if (namelen > UCHAR_MAX ||
                   1704:            namelen <= offsetof(struct sockaddr_big, sb_data))
                   1705:                return EINVAL;
                   1706: 
                   1707:        error = copyin(name, sb, namelen);
                   1708:        if (error)
                   1709:                return error;
                   1710: 
                   1711:        bdom = linux_to_bsd_domain(sb->sb_family);
                   1712:        if (bdom == -1)
                   1713:                return EINVAL;
                   1714: 
                   1715:        /*
                   1716:         * If the family is unspecified, use address family of the socket.
                   1717:         * This avoid triggering strict family checks in netinet/in_pcb.c et.al.
                   1718:         */
                   1719:        if (bdom == AF_UNSPEC) {
                   1720:                struct socket *so;
                   1721: 
                   1722:                /* fd_getsock() will use the descriptor for us */
                   1723:                if ((error = fd_getsock(s, &so)) != 0)
                   1724:                        return error;
                   1725: 
                   1726:                bdom = so->so_proto->pr_domain->dom_family;
                   1727:                fd_putfile(s);
                   1728:        }
                   1729: 
                   1730:        /*
                   1731:         * Older Linux IPv6 code uses obsolete RFC2133 struct sockaddr_in6,
                   1732:         * which lacks the scope id compared with RFC2553 one. If we detect
                   1733:         * the situation, reject the address and write a message to system log.
                   1734:         *
                   1735:         * Still accept addresses for which the scope id is not used.
                   1736:         */
                   1737:        if (bdom == AF_INET6 &&
                   1738:            namelen == sizeof(struct sockaddr_in6) - sizeof(uint32_t)) {
                   1739:                struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sb;
                   1740:                if (!IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr) &&
                   1741:                    (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr) ||
                   1742:                     IN6_IS_ADDR_SITELOCAL(&sin6->sin6_addr) ||
                   1743:                     IN6_IS_ADDR_V4COMPAT(&sin6->sin6_addr) ||
                   1744:                     IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr) ||
                   1745:                     IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))) {
                   1746:                        struct proc *p = l->l_proc;
                   1747:                        int uid = l->l_cred ? kauth_cred_geteuid(l->l_cred) : -1;
                   1748: 
                   1749:                        log(LOG_DEBUG,
                   1750:                            "pid %d (%s), uid %d: obsolete pre-RFC2553 "
                   1751:                            "sockaddr_in6 rejected",
                   1752:                            p->p_pid, p->p_comm, uid);
                   1753:                        return EINVAL;
                   1754:                }
                   1755:                namelen = sizeof(struct sockaddr_in6);
                   1756:                sin6->sin6_scope_id = 0;
                   1757:        }
                   1758: 
1.151     mgorny   1759:        /*
                   1760:         * Linux is less strict than NetBSD and permits namelen to be larger
                   1761:         * than valid struct sockaddr_in*.  If this is the case, truncate
                   1762:         * the value to the correct size, so that NetBSD networking does not
                   1763:         * return an error.
                   1764:         */
                   1765:        switch (bdom) {
                   1766:        case AF_INET:
                   1767:                namelen = MIN(namelen, sizeof(struct sockaddr_in));
                   1768:                break;
                   1769:        case AF_INET6:
                   1770:                namelen = MIN(namelen, sizeof(struct sockaddr_in6));
                   1771:                break;
                   1772:        }
                   1773: 
1.123     rtr      1774:        sb->sb_family = bdom;
                   1775:        sb->sb_len = namelen;
                   1776:        ktrkuser("mbsoname", sb, namelen);
                   1777:        return 0;
                   1778: }
                   1779: 
1.32      jdolecek 1780: static int
1.84      dsl      1781: linux_sa_put(struct osockaddr *osa)
1.32      jdolecek 1782: {
                   1783:        struct sockaddr sa;
                   1784:        struct osockaddr *kosa;
                   1785:        int error, bdom, len;
                   1786: 
                   1787:        /*
                   1788:         * Only read/write the sockaddr family and length part, the rest is
                   1789:         * not changed.
                   1790:         */
                   1791:        len = sizeof(sa.sa_len) + sizeof(sa.sa_family);
                   1792: 
1.73      dsl      1793:        error = copyin(osa, &sa, len);
1.32      jdolecek 1794:        if (error)
                   1795:                return (error);
                   1796: 
                   1797:        bdom = bsd_to_linux_domain(sa.sa_family);
                   1798:        if (bdom == -1)
                   1799:                return (EINVAL);
                   1800: 
                   1801:        /* Note: we convert from sockaddr to osockaddr here, too */
                   1802:        kosa = (struct osockaddr *) &sa;
                   1803:        kosa->sa_family = bdom;
                   1804:        error = copyout(kosa, osa, len);
                   1805:        if (error)
                   1806:                return (error);
                   1807: 
                   1808:        return (0);
                   1809: }
                   1810: 
1.154     ryo      1811: #if !defined(__aarch64__) && !defined(__amd64__)
1.32      jdolecek 1812: int
1.85      dsl      1813: linux_sys_recv(struct lwp *l, const struct linux_sys_recv_args *uap, register_t *retval)
1.32      jdolecek 1814: {
1.85      dsl      1815:        /* {
1.32      jdolecek 1816:                syscallarg(int) s;
                   1817:                syscallarg(void *) buf;
                   1818:                syscallarg(int) len;
                   1819:                syscallarg(int) flags;
1.85      dsl      1820:        } */
1.32      jdolecek 1821:        struct sys_recvfrom_args bra;
                   1822: 
                   1823: 
                   1824:        SCARG(&bra, s) = SCARG(uap, s);
                   1825:        SCARG(&bra, buf) = SCARG(uap, buf);
                   1826:        SCARG(&bra, len) = (size_t) SCARG(uap, len);
                   1827:        SCARG(&bra, flags) = SCARG(uap, flags);
                   1828:        SCARG(&bra, from) = NULL;
                   1829:        SCARG(&bra, fromlenaddr) = NULL;
                   1830: 
1.43      thorpej  1831:        return (sys_recvfrom(l, &bra, retval));
1.32      jdolecek 1832: }
                   1833: 
                   1834: int
1.85      dsl      1835: linux_sys_send(struct lwp *l, const struct linux_sys_send_args *uap, register_t *retval)
1.32      jdolecek 1836: {
1.85      dsl      1837:        /* {
1.32      jdolecek 1838:                syscallarg(int) s;
1.71      christos 1839:                syscallarg(void *) buf;
1.32      jdolecek 1840:                syscallarg(int) len;
                   1841:                syscallarg(int) flags;
1.85      dsl      1842:        } */
1.32      jdolecek 1843:        struct sys_sendto_args bsa;
                   1844: 
                   1845:        SCARG(&bsa, s)          = SCARG(uap, s);
                   1846:        SCARG(&bsa, buf)        = SCARG(uap, buf);
                   1847:        SCARG(&bsa, len)        = SCARG(uap, len);
                   1848:        SCARG(&bsa, flags)      = SCARG(uap, flags);
                   1849:        SCARG(&bsa, to)         = NULL;
                   1850:        SCARG(&bsa, tolen)      = 0;
                   1851: 
1.43      thorpej  1852:        return (sys_sendto(l, &bsa, retval));
1.32      jdolecek 1853: }
1.59      manu     1854: #endif
1.32      jdolecek 1855: 
                   1856: int
1.85      dsl      1857: linux_sys_accept(struct lwp *l, const struct linux_sys_accept_args *uap, register_t *retval)
1.32      jdolecek 1858: {
1.85      dsl      1859:        /* {
1.32      jdolecek 1860:                syscallarg(int) s;
                   1861:                syscallarg(struct osockaddr *) name;
                   1862:                syscallarg(int *) anamelen;
1.85      dsl      1863:        } */
1.32      jdolecek 1864:        int error;
                   1865:        struct sys_accept_args baa;
                   1866: 
                   1867:        SCARG(&baa, s)          = SCARG(uap, s);
                   1868:        SCARG(&baa, name)       = (struct sockaddr *) SCARG(uap, name);
                   1869:        SCARG(&baa, anamelen)   = (unsigned int *) SCARG(uap, anamelen);
                   1870: 
1.43      thorpej  1871:        if ((error = sys_accept(l, &baa, retval)))
1.32      jdolecek 1872:                return (error);
                   1873: 
                   1874:        if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
                   1875:                return (error);
                   1876: 
                   1877:        return (0);
1.19      sommerfe 1878: }
1.134     christos 1879: 
                   1880: int
                   1881: linux_sys_accept4(struct lwp *l, const struct linux_sys_accept4_args *uap, register_t *retval)
                   1882: {
                   1883:        /* {
                   1884:                syscallarg(int) s;
                   1885:                syscallarg(struct osockaddr *) name;
                   1886:                syscallarg(int *) anamelen;
                   1887:                syscallarg(int) flags;
                   1888:        } */
                   1889:        int error, flags;
                   1890:        struct sockaddr_big name;
                   1891: 
                   1892:        if ((flags = linux_to_bsd_type(SCARG(uap, flags))) == -1)
                   1893:                return EINVAL;
                   1894: 
                   1895:        name.sb_len = UCHAR_MAX;
                   1896:        error = do_sys_accept(l, SCARG(uap, s), (struct sockaddr *)&name,
                   1897:            retval, NULL, flags, 0);
                   1898:        if (error != 0)
                   1899:                return error;
                   1900: 
                   1901:        error = copyout_sockname_sb((struct sockaddr *)SCARG(uap, name),
                   1902:            SCARG(uap, anamelen), MSG_LENUSRSPACE, &name);
                   1903:        if (error != 0) {
                   1904:                int fd = (int)*retval;
                   1905:                if (fd_getfile(fd) != NULL)
                   1906:                        (void)fd_close(fd);
                   1907:                return error;
                   1908:        }
                   1909:        if (SCARG(uap, name) && (error = linux_sa_put(SCARG(uap, name))))
                   1910:                return error;
                   1911: 
                   1912:        return 0;
                   1913: }
1.137     christos 1914: 
                   1915: int
                   1916: linux_sys_sendmmsg(struct lwp *l, const struct linux_sys_sendmmsg_args *uap,
                   1917:     register_t *retval)
                   1918: {
                   1919:        /* {
                   1920:                syscallarg(int) s;
                   1921:                syscallarg(struct linux_mmsghdr *) msgvec;
                   1922:                syscallarg(unsigned int) vlen;
                   1923:                syscallarg(unsigned int) flags;
                   1924:        } */
                   1925:        struct linux_mmsghdr lmsg;
                   1926:        struct mmsghdr bmsg;
                   1927:        struct socket *so;
                   1928:        file_t *fp;
                   1929:        struct msghdr *msg = &bmsg.msg_hdr;
                   1930:        int error, s;
                   1931:        unsigned int vlen, flags, dg;
                   1932: 
                   1933:        if ((flags = linux_to_bsd_msg_flags(SCARG(uap, flags))) == -1)
                   1934:                return EINVAL;
                   1935: 
                   1936:        flags = (flags & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
                   1937: 
                   1938:        s = SCARG(uap, s);
                   1939:        if ((error = fd_getsock1(s, &so, &fp)) != 0)
                   1940:                return error;
                   1941: 
                   1942:        vlen = SCARG(uap, vlen);
                   1943:        if (vlen > 1024)
                   1944:                vlen = 1024;
                   1945: 
                   1946:        for (dg = 0; dg < vlen;) {
                   1947:                error = copyin(SCARG(uap, msgvec) + dg, &lmsg, sizeof(lmsg));
                   1948:                if (error)
                   1949:                        break;
1.159     riastrad 1950:                error = linux_to_bsd_msghdr(&lmsg.msg_hdr, &bmsg.msg_hdr);
                   1951:                if (error)
                   1952:                        break;
1.137     christos 1953: 
                   1954:                msg->msg_flags = flags;
                   1955: 
1.141     christos 1956:                error = do_sys_sendmsg_so(l, s, so, fp, msg, flags, retval);
1.137     christos 1957:                if (error)
                   1958:                        break;
                   1959: 
                   1960:                ktrkuser("msghdr", msg, sizeof *msg);
                   1961:                lmsg.msg_len = *retval;
                   1962:                error = copyout(&lmsg, SCARG(uap, msgvec) + dg, sizeof(lmsg));
                   1963:                if (error)
                   1964:                        break;
                   1965:                dg++;
                   1966: 
                   1967:        }
                   1968: 
                   1969:        *retval = dg;
                   1970: 
                   1971:        fd_putfile(s);
                   1972: 
                   1973:        /*
1.143     hannken  1974:         * If we succeeded at least once, return 0.
1.137     christos 1975:         */
                   1976:        if (dg)
                   1977:                return 0;
                   1978:        return error;
                   1979: }
                   1980: 
                   1981: int
                   1982: linux_sys_recvmmsg(struct lwp *l, const struct linux_sys_recvmmsg_args *uap,
                   1983:     register_t *retval)
                   1984: {
                   1985:        /* {
                   1986:                syscallarg(int) s;
                   1987:                syscallarg(struct linux_mmsghdr *) msgvec;
                   1988:                syscallarg(unsigned int) vlen;
                   1989:                syscallarg(unsigned int) flags;
                   1990:                syscallarg(struct linux_timespec *) timeout;
                   1991:        } */
                   1992:        struct linux_mmsghdr lmsg;
                   1993:        struct mmsghdr bmsg;
                   1994:        struct socket *so;
                   1995:        struct msghdr *msg = &bmsg.msg_hdr;
                   1996:        int error, s;
                   1997:        struct mbuf *from, *control;
1.144     mrg      1998:        struct timespec ts = {0}, now;
1.137     christos 1999:        struct linux_timespec lts;
                   2000:        unsigned int vlen, flags, dg;
                   2001: 
                   2002:        if (SCARG(uap, timeout)) {
                   2003:                error = copyin(SCARG(uap, timeout), &lts, sizeof(lts));
                   2004:                        return error;
                   2005:                ts.tv_sec = lts.tv_sec;
                   2006:                ts.tv_nsec = lts.tv_nsec;
                   2007:                getnanotime(&now);
                   2008:                timespecadd(&now, &ts, &ts);
                   2009:        }
                   2010: 
                   2011:        s = SCARG(uap, s);
                   2012:        if ((error = fd_getsock(s, &so)) != 0)
                   2013:                return error;
                   2014: 
1.143     hannken  2015:        /*
                   2016:         * If so->so_rerror holds a deferred error return it now.
                   2017:         */
                   2018:        if (so->so_rerror) {
                   2019:                error = so->so_rerror;
                   2020:                so->so_rerror = 0;
                   2021:                fd_putfile(s);
                   2022:                return error;
                   2023:        }
                   2024: 
1.137     christos 2025:        vlen = SCARG(uap, vlen);
                   2026:        if (vlen > 1024)
                   2027:                vlen = 1024;
                   2028: 
                   2029:        from = NULL;
                   2030:        flags = (SCARG(uap, flags) & MSG_USERFLAGS) | MSG_IOVUSRSPACE;
                   2031: 
                   2032:        for (dg = 0; dg < vlen;) {
                   2033:                error = copyin(SCARG(uap, msgvec) + dg, &lmsg, sizeof(lmsg));
                   2034:                if (error)
                   2035:                        break;
1.159     riastrad 2036:                error = linux_to_bsd_msghdr(&lmsg.msg_hdr, &bmsg.msg_hdr);
                   2037:                if (error)
                   2038:                        break;
1.137     christos 2039:                msg->msg_flags = flags & ~MSG_WAITFORONE;
                   2040: 
                   2041:                if (from != NULL) {
                   2042:                        m_free(from);
                   2043:                        from = NULL;
                   2044:                }
                   2045: 
1.141     christos 2046:                error = do_sys_recvmsg_so(l, s, so, msg, &from,
1.137     christos 2047:                    msg->msg_control != NULL ? &control : NULL, retval);
                   2048:                if (error) {
                   2049:                        if (error == EAGAIN && dg > 0)
                   2050:                                error = 0;
                   2051:                        break;
                   2052:                }
                   2053: 
                   2054:                if (msg->msg_control != NULL)
                   2055:                        error = linux_copyout_msg_control(l, msg, control);
                   2056:                if (error)
                   2057:                        break;
                   2058: 
                   2059:                if (from != NULL) {
                   2060:                        mtod(from, struct osockaddr *)->sa_family =
                   2061:                            bsd_to_linux_domain(mtod(from,
                   2062:                            struct sockaddr *)->sa_family);
                   2063:                        error = copyout_sockname(msg->msg_name,
                   2064:                            &msg->msg_namelen, 0, from);
                   2065:                        if (error)
                   2066:                                break;
                   2067:                }
                   2068: 
                   2069: 
                   2070:                lmsg.msg_len = *retval;
                   2071:                ktrkuser("msghdr", msg, sizeof(*msg));
                   2072:                bsd_to_linux_msghdr(msg, &lmsg.msg_hdr);
                   2073:                error = copyout(&lmsg, SCARG(uap, msgvec) + dg, sizeof(lmsg));
                   2074:                if (error)
                   2075:                        break;
                   2076: 
                   2077:                dg++;
                   2078:                if (msg->msg_flags & MSG_OOB)
                   2079:                        break;
                   2080: 
                   2081:                if (SCARG(uap, timeout)) {
                   2082:                        getnanotime(&now);
                   2083:                        timespecsub(&now, &ts, &now);
                   2084:                        if (now.tv_sec > 0)
                   2085:                                break;
                   2086:                }
                   2087: 
                   2088:                if (flags & MSG_WAITFORONE)
                   2089:                        flags |= MSG_DONTWAIT;
                   2090: 
                   2091:        }
                   2092: 
                   2093:        if (from != NULL)
                   2094:                m_free(from);
                   2095: 
                   2096:        *retval = dg;
                   2097: 
                   2098:        /*
1.143     hannken  2099:         * If we succeeded at least once, return 0, hopefully so->so_rerror
1.137     christos 2100:         * will catch it next time.
                   2101:         */
1.143     hannken  2102:        if (error && dg > 0) {
                   2103:                so->so_rerror = error;
                   2104:                error = 0;
                   2105:        }
                   2106: 
                   2107:        fd_putfile(s);
1.137     christos 2108: 
                   2109:        return error;
                   2110: }

CVSweb <webmaster@jp.NetBSD.org>