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

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

CVSweb <webmaster@jp.NetBSD.org>