[BACK]Return to mld6.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / sys / netinet6

Annotation of src/sys/netinet6/mld6.c, Revision 1.59.2.1

1.59.2.1! martin      1: /*     $NetBSD: mld6.c,v 1.59 2014/07/26 22:21:16 joerg Exp $  */
1.13      itojun      2: /*     $KAME: mld6.c,v 1.25 2001/01/16 14:14:18 itojun Exp $   */
1.3       thorpej     3:
1.2       itojun      4: /*
                      5:  * Copyright (C) 1998 WIDE Project.
                      6:  * All rights reserved.
1.13      itojun      7:  *
1.2       itojun      8:  * Redistribution and use in source and binary forms, with or without
                      9:  * modification, are permitted provided that the following conditions
                     10:  * are met:
                     11:  * 1. Redistributions of source code must retain the above copyright
                     12:  *    notice, this list of conditions and the following disclaimer.
                     13:  * 2. Redistributions in binary form must reproduce the above copyright
                     14:  *    notice, this list of conditions and the following disclaimer in the
                     15:  *    documentation and/or other materials provided with the distribution.
                     16:  * 3. Neither the name of the project nor the names of its contributors
                     17:  *    may be used to endorse or promote products derived from this software
                     18:  *    without specific prior written permission.
1.13      itojun     19:  *
1.2       itojun     20:  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
                     21:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     22:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     23:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
                     24:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     25:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     26:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     27:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     28:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     29:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     30:  * SUCH DAMAGE.
                     31:  */
                     32:
                     33: /*
                     34:  * Copyright (c) 1992, 1993
                     35:  *     The Regents of the University of California.  All rights reserved.
                     36:  *
                     37:  * This code is derived from software contributed to Berkeley by
                     38:  * Stephen Deering of Stanford University.
                     39:  *
                     40:  * Redistribution and use in source and binary forms, with or without
                     41:  * modification, are permitted provided that the following conditions
                     42:  * are met:
                     43:  * 1. Redistributions of source code must retain the above copyright
                     44:  *    notice, this list of conditions and the following disclaimer.
                     45:  * 2. Redistributions in binary form must reproduce the above copyright
                     46:  *    notice, this list of conditions and the following disclaimer in the
                     47:  *    documentation and/or other materials provided with the distribution.
1.23      agc        48:  * 3. Neither the name of the University nor the names of its contributors
                     49:  *    may be used to endorse or promote products derived from this software
                     50:  *    without specific prior written permission.
                     51:  *
                     52:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     53:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     54:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     55:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     56:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     57:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     58:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     59:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     60:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     61:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     62:  * SUCH DAMAGE.
                     63:  *
                     64:  *     @(#)igmp.c      8.1 (Berkeley) 7/19/93
                     65:  */
                     66:
                     67: /*
                     68:  * Copyright (c) 1988 Stephen Deering.
                     69:  *
                     70:  * This code is derived from software contributed to Berkeley by
                     71:  * Stephen Deering of Stanford University.
                     72:  *
                     73:  * Redistribution and use in source and binary forms, with or without
                     74:  * modification, are permitted provided that the following conditions
                     75:  * are met:
                     76:  * 1. Redistributions of source code must retain the above copyright
                     77:  *    notice, this list of conditions and the following disclaimer.
                     78:  * 2. Redistributions in binary form must reproduce the above copyright
                     79:  *    notice, this list of conditions and the following disclaimer in the
                     80:  *    documentation and/or other materials provided with the distribution.
1.2       itojun     81:  * 3. All advertising materials mentioning features or use of this software
                     82:  *    must display the following acknowledgement:
                     83:  *     This product includes software developed by the University of
                     84:  *     California, Berkeley and its contributors.
                     85:  * 4. Neither the name of the University nor the names of its contributors
                     86:  *    may be used to endorse or promote products derived from this software
                     87:  *    without specific prior written permission.
                     88:  *
                     89:  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
                     90:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
                     91:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     92:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
                     93:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     94:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     95:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     96:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     97:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     98:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     99:  * SUCH DAMAGE.
                    100:  *
                    101:  *     @(#)igmp.c      8.1 (Berkeley) 7/19/93
                    102:  */
1.16      lukem     103:
                    104: #include <sys/cdefs.h>
1.59.2.1! martin    105: __KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.59 2014/07/26 22:21:16 joerg Exp $");
1.2       itojun    106:
                    107: #include "opt_inet.h"
                    108:
                    109: #include <sys/param.h>
                    110: #include <sys/systm.h>
                    111: #include <sys/mbuf.h>
                    112: #include <sys/socket.h>
1.45      ad        113: #include <sys/socketvar.h>
1.2       itojun    114: #include <sys/protosw.h>
                    115: #include <sys/syslog.h>
1.31      rpaulo    116: #include <sys/sysctl.h>
                    117: #include <sys/kernel.h>
                    118: #include <sys/callout.h>
1.55      tls       119: #include <sys/cprng.h>
1.2       itojun    120:
                    121: #include <net/if.h>
                    122:
                    123: #include <netinet/in.h>
                    124: #include <netinet/in_var.h>
1.31      rpaulo    125: #include <netinet6/in6_var.h>
1.10      itojun    126: #include <netinet/ip6.h>
1.2       itojun    127: #include <netinet6/ip6_var.h>
1.29      rpaulo    128: #include <netinet6/scope6_var.h>
1.10      itojun    129: #include <netinet/icmp6.h>
1.44      thorpej   130: #include <netinet6/icmp6_private.h>
1.2       itojun    131: #include <netinet6/mld6_var.h>
                    132:
1.7       itojun    133: #include <net/net_osdep.h>
                    134:
1.31      rpaulo    135:
                    136: /*
                    137:  * This structure is used to keep track of in6_multi chains which belong to
                    138:  * deleted interface addresses.
                    139:  */
1.56      joerg     140: static LIST_HEAD(, multi6_kludge) in6_mk = LIST_HEAD_INITIALIZER(in6_mk);
1.31      rpaulo    141:
                    142: struct multi6_kludge {
                    143:        LIST_ENTRY(multi6_kludge) mk_entry;
                    144:        struct ifnet *mk_ifp;
                    145:        struct in6_multihead mk_head;
                    146: };
                    147:
                    148:
1.2       itojun    149: /*
                    150:  * Protocol constants
                    151:  */
                    152:
                    153: /*
                    154:  * time between repetitions of a node's initial report of interest in a
                    155:  * multicast address(in seconds)
                    156:  */
1.22      itojun    157: #define MLD_UNSOLICITED_REPORT_INTERVAL        10
1.2       itojun    158:
                    159: static struct ip6_pktopts ip6_opts;
                    160:
1.31      rpaulo    161: static void mld_start_listening(struct in6_multi *);
                    162: static void mld_stop_listening(struct in6_multi *);
                    163:
                    164: static struct mld_hdr * mld_allocbuf(struct mbuf **, int, struct in6_multi *,
                    165:        int);
                    166: static void mld_sendpkt(struct in6_multi *, int, const struct in6_addr *);
                    167: static void mld_starttimer(struct in6_multi *);
                    168: static void mld_stoptimer(struct in6_multi *);
                    169: static u_long mld_timerresid(struct in6_multi *);
1.2       itojun    170:
                    171: void
1.42      matt      172: mld_init(void)
1.2       itojun    173: {
                    174:        static u_int8_t hbh_buf[8];
                    175:        struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
                    176:        u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
                    177:
                    178:        /* ip6h_nxt will be fill in later */
1.11      itojun    179:        hbh->ip6h_len = 0;      /* (8 >> 3) - 1 */
1.2       itojun    180:
                    181:        /* XXX: grotty hard coding... */
                    182:        hbh_buf[2] = IP6OPT_PADN;       /* 2 byte padding */
                    183:        hbh_buf[3] = 0;
                    184:        hbh_buf[4] = IP6OPT_RTALERT;
                    185:        hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
1.50      tsutsui   186:        memcpy(&hbh_buf[6], (void *)&rtalert_code, sizeof(u_int16_t));
1.2       itojun    187:
                    188:        ip6_opts.ip6po_hbh = hbh;
                    189:        /* We will specify the hoplimit by a multicast option. */
                    190:        ip6_opts.ip6po_hlim = -1;
                    191: }
                    192:
1.31      rpaulo    193: static void
1.38      christos  194: mld_starttimer(struct in6_multi *in6m)
1.31      rpaulo    195: {
                    196:        struct timeval now;
                    197:
1.59.2.1! martin    198:        KASSERT(in6m->in6m_timer != IN6M_TIMER_UNDEF);
        !           199:
1.31      rpaulo    200:        microtime(&now);
                    201:        in6m->in6m_timer_expire.tv_sec = now.tv_sec + in6m->in6m_timer / hz;
                    202:        in6m->in6m_timer_expire.tv_usec = now.tv_usec +
                    203:            (in6m->in6m_timer % hz) * (1000000 / hz);
                    204:        if (in6m->in6m_timer_expire.tv_usec > 1000000) {
                    205:                in6m->in6m_timer_expire.tv_sec++;
                    206:                in6m->in6m_timer_expire.tv_usec -= 1000000;
                    207:        }
                    208:
                    209:        /* start or restart the timer */
1.41      joerg     210:        callout_schedule(&in6m->in6m_timer_ch, in6m->in6m_timer);
1.31      rpaulo    211: }
                    212:
                    213: static void
1.38      christos  214: mld_stoptimer(struct in6_multi *in6m)
1.31      rpaulo    215: {
                    216:        if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
                    217:                return;
                    218:
1.41      joerg     219:        callout_stop(&in6m->in6m_timer_ch);
1.31      rpaulo    220:
                    221:        in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    222: }
                    223:
                    224: static void
1.41      joerg     225: mld_timeo(void *arg)
1.31      rpaulo    226: {
1.41      joerg     227:        struct in6_multi *in6m = arg;
1.45      ad        228:
                    229:        mutex_enter(softnet_lock);
                    230:        KERNEL_LOCK(1, NULL);
1.31      rpaulo    231:
1.59.2.1! martin    232:        if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
        !           233:                goto out;
        !           234:
1.31      rpaulo    235:        in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    236:
                    237:        switch (in6m->in6m_state) {
                    238:        case MLD_REPORTPENDING:
                    239:                mld_start_listening(in6m);
                    240:                break;
                    241:        default:
                    242:                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
                    243:                break;
                    244:        }
                    245:
1.59.2.1! martin    246: out:
1.45      ad        247:        KERNEL_UNLOCK_ONE(NULL);
                    248:        mutex_exit(softnet_lock);
1.31      rpaulo    249: }
                    250:
                    251: static u_long
1.38      christos  252: mld_timerresid(struct in6_multi *in6m)
1.31      rpaulo    253: {
                    254:        struct timeval now, diff;
                    255:
                    256:        microtime(&now);
                    257:
                    258:        if (now.tv_sec > in6m->in6m_timer_expire.tv_sec ||
                    259:            (now.tv_sec == in6m->in6m_timer_expire.tv_sec &&
                    260:            now.tv_usec > in6m->in6m_timer_expire.tv_usec)) {
                    261:                return (0);
                    262:        }
                    263:        diff = in6m->in6m_timer_expire;
                    264:        diff.tv_sec -= now.tv_sec;
                    265:        diff.tv_usec -= now.tv_usec;
                    266:        if (diff.tv_usec < 0) {
                    267:                diff.tv_sec--;
                    268:                diff.tv_usec += 1000000;
                    269:        }
                    270:
                    271:        /* return the remaining time in milliseconds */
1.47      adrianp   272:        return diff.tv_sec * 1000 + diff.tv_usec / 1000;
1.31      rpaulo    273: }
                    274:
                    275: static void
1.38      christos  276: mld_start_listening(struct in6_multi *in6m)
1.2       itojun    277: {
1.29      rpaulo    278:        struct in6_addr all_in6;
                    279:
1.2       itojun    280:        /*
1.11      itojun    281:         * RFC2710 page 10:
1.2       itojun    282:         * The node never sends a Report or Done for the link-scope all-nodes
                    283:         * address.
                    284:         * MLD messages are never sent for multicast addresses whose scope is 0
                    285:         * (reserved) or 1 (node-local).
                    286:         */
1.29      rpaulo    287:        all_in6 = in6addr_linklocal_allnodes;
                    288:        if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) {
                    289:                /* XXX: this should not happen! */
                    290:                in6m->in6m_timer = 0;
                    291:                in6m->in6m_state = MLD_OTHERLISTENER;
                    292:        }
                    293:        if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
1.2       itojun    294:            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
1.31      rpaulo    295:                in6m->in6m_timer = IN6M_TIMER_UNDEF;
1.22      itojun    296:                in6m->in6m_state = MLD_OTHERLISTENER;
1.2       itojun    297:        } else {
1.31      rpaulo    298:                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
1.55      tls       299:                in6m->in6m_timer = cprng_fast32() %
1.31      rpaulo    300:                    (MLD_UNSOLICITED_REPORT_INTERVAL * hz);
1.22      itojun    301:                in6m->in6m_state = MLD_IREPORTEDLAST;
1.31      rpaulo    302:
                    303:                mld_starttimer(in6m);
1.2       itojun    304:        }
                    305: }
                    306:
1.31      rpaulo    307: static void
1.38      christos  308: mld_stop_listening(struct in6_multi *in6m)
1.2       itojun    309: {
1.29      rpaulo    310:        struct in6_addr allnode, allrouter;
                    311:
                    312:        allnode = in6addr_linklocal_allnodes;
                    313:        if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) {
                    314:                /* XXX: this should not happen! */
                    315:                return;
                    316:        }
                    317:        allrouter = in6addr_linklocal_allrouters;
                    318:        if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) {
                    319:                /* XXX impossible */
                    320:                return;
                    321:        }
1.2       itojun    322:
1.22      itojun    323:        if (in6m->in6m_state == MLD_IREPORTEDLAST &&
1.29      rpaulo    324:            (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode)) &&
                    325:            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
                    326:            IPV6_ADDR_SCOPE_INTFACELOCAL) {
1.31      rpaulo    327:                mld_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
1.29      rpaulo    328:        }
1.2       itojun    329: }
                    330:
                    331: void
1.38      christos  332: mld_input(struct mbuf *m, int off)
1.2       itojun    333: {
1.52      dholland  334:        struct ip6_hdr *ip6;
1.22      itojun    335:        struct mld_hdr *mldh;
1.2       itojun    336:        struct ifnet *ifp = m->m_pkthdr.rcvif;
1.31      rpaulo    337:        struct in6_multi *in6m = NULL;
1.29      rpaulo    338:        struct in6_addr mld_addr, all_in6;
1.2       itojun    339:        struct in6_ifaddr *ia;
1.47      adrianp   340:        u_long timer = 0;       /* timer value in the MLD query header */
1.2       itojun    341:
1.22      itojun    342:        IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
1.13      itojun    343:        if (mldh == NULL) {
1.44      thorpej   344:                ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
1.13      itojun    345:                return;
                    346:        }
                    347:
1.2       itojun    348:        /* source address validation */
1.13      itojun    349:        ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
1.2       itojun    350:        if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
1.31      rpaulo    351:                /*
                    352:                 * RFC3590 allows the IPv6 unspecified address as the source
                    353:                 * address of MLD report and done messages.  However, as this
                    354:                 * same document says, this special rule is for snooping
                    355:                 * switches and the RFC requires routers to discard MLD packets
                    356:                 * with the unspecified source address.  The RFC only talks
                    357:                 * about hosts receiving an MLD query or report in Security
                    358:                 * Considerations, but this is probably the correct intention.
                    359:                 * RFC3590 does not talk about other cases than link-local and
                    360:                 * the unspecified source addresses, but we believe the same
                    361:                 * rule should be applied.
                    362:                 * As a result, we only allow link-local addresses as the
                    363:                 * source address; otherwise, simply discard the packet.
                    364:                 */
1.18      itojun    365: #if 0
1.31      rpaulo    366:                /*
                    367:                 * XXX: do not log in an input path to avoid log flooding,
                    368:                 * though RFC3590 says "SHOULD log" if the source of a query
                    369:                 * is the unspecified address.
                    370:                 */
                    371:                log(LOG_INFO,
1.22      itojun    372:                    "mld_input: src %s is not link-local (grp=%s)\n",
1.31      rpaulo    373:                    ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&mldh->mld_addr));
1.18      itojun    374: #endif
1.11      itojun    375:                m_freem(m);
1.2       itojun    376:                return;
                    377:        }
                    378:
                    379:        /*
1.29      rpaulo    380:         * make a copy for local work (in6_setscope() may modify the 1st arg)
                    381:         */
                    382:        mld_addr = mldh->mld_addr;
                    383:        if (in6_setscope(&mld_addr, ifp, NULL)) {
                    384:                /* XXX: this should not happen! */
                    385:                m_free(m);
                    386:                return;
                    387:        }
                    388:
                    389:        /*
1.31      rpaulo    390:         * In the MLD specification, there are 3 states and a flag.
1.2       itojun    391:         *
                    392:         * In Non-Listener state, we simply don't have a membership record.
                    393:         * In Delaying Listener state, our timer is running (in6m->in6m_timer)
1.31      rpaulo    394:         * In Idle Listener state, our timer is not running
                    395:         * (in6m->in6m_timer==IN6M_TIMER_UNDEF)
1.2       itojun    396:         *
1.22      itojun    397:         * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
                    398:         * we have heard a report from another member, or MLD_IREPORTEDLAST
1.2       itojun    399:         * if we sent the last report.
                    400:         */
1.22      itojun    401:        switch (mldh->mld_type) {
                    402:        case MLD_LISTENER_QUERY:
1.7       itojun    403:                if (ifp->if_flags & IFF_LOOPBACK)
                    404:                        break;
                    405:
1.29      rpaulo    406:                if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
                    407:                    !IN6_IS_ADDR_MULTICAST(&mld_addr))
1.7       itojun    408:                        break;  /* print error or log stat? */
1.29      rpaulo    409:
                    410:                all_in6 = in6addr_linklocal_allnodes;
                    411:                if (in6_setscope(&all_in6, ifp, NULL)) {
                    412:                        /* XXX: this should not happen! */
                    413:                        break;
                    414:                }
1.2       itojun    415:
1.7       itojun    416:                /*
1.11      itojun    417:                 * - Start the timers in all of our membership records
                    418:                 *   that the query applies to for the interface on
                    419:                 *   which the query arrived excl. those that belong
                    420:                 *   to the "all-nodes" group (ff02::1).
                    421:                 * - Restart any timer that is already running but has
1.30      rpaulo    422:                 *   a value longer than the requested timeout.
1.11      itojun    423:                 * - Use the value specified in the query message as
                    424:                 *   the maximum timeout.
                    425:                 */
1.31      rpaulo    426:                timer = ntohs(mldh->mld_maxdelay);
                    427:
1.7       itojun    428:                IFP_TO_IA6(ifp, ia);
                    429:                if (ia == NULL)
                    430:                        break;
1.2       itojun    431:
1.35      dyoung    432:                LIST_FOREACH(in6m, &ia->ia6_multiaddrs, in6m_entry) {
1.29      rpaulo    433:                        if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
1.7       itojun    434:                            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
                    435:                            IPV6_ADDR_SCOPE_LINKLOCAL)
                    436:                                continue;
1.2       itojun    437:
1.31      rpaulo    438:                        if (in6m->in6m_state == MLD_REPORTPENDING)
                    439:                                continue; /* we are not yet ready */
                    440:
                    441:                        if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
                    442:                            !IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr))
                    443:                                continue;
                    444:
                    445:                        if (timer == 0) {
                    446:                                /* send a report immediately */
                    447:                                mld_stoptimer(in6m);
                    448:                                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
                    449:                                in6m->in6m_state = MLD_IREPORTEDLAST;
                    450:                        } else if (in6m->in6m_timer == IN6M_TIMER_UNDEF ||
1.47      adrianp   451:                            mld_timerresid(in6m) > timer) {
                    452:                                in6m->in6m_timer =
1.55      tls       453:                                   1 + (cprng_fast32() % timer) * hz / 1000;
1.31      rpaulo    454:                                mld_starttimer(in6m);
1.7       itojun    455:                        }
                    456:                }
1.29      rpaulo    457:                break;
1.2       itojun    458:
1.22      itojun    459:        case MLD_LISTENER_REPORT:
1.2       itojun    460:                /*
1.11      itojun    461:                 * For fast leave to work, we have to know that we are the
                    462:                 * last person to send a report for this group.  Reports
                    463:                 * can potentially get looped back if we are a multicast
                    464:                 * router, so discard reports sourced by me.
                    465:                 * Note that it is impossible to check IFF_LOOPBACK flag of
                    466:                 * ifp for this purpose, since ip6_mloopback pass the physical
                    467:                 * interface to looutput.
                    468:                 */
1.7       itojun    469:                if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
                    470:                        break;
                    471:
1.22      itojun    472:                if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
1.7       itojun    473:                        break;
                    474:
                    475:                /*
1.11      itojun    476:                 * If we belong to the group being reported, stop
                    477:                 * our timer for that group.
                    478:                 */
1.29      rpaulo    479:                IN6_LOOKUP_MULTI(mld_addr, ifp, in6m);
1.7       itojun    480:                if (in6m) {
1.31      rpaulo    481:                        mld_stoptimer(in6m); /* transit to idle state */
1.22      itojun    482:                        in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
1.7       itojun    483:                }
                    484:                break;
                    485:        default:                /* this is impossible */
1.18      itojun    486: #if 0
1.19      itojun    487:                /*
                    488:                 * this case should be impossible because of filtering in
                    489:                 * icmp6_input().  But we explicitly disabled this part
                    490:                 * just in case.
                    491:                 */
1.31      rpaulo    492:                log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type);
1.18      itojun    493: #endif
1.7       itojun    494:                break;
1.2       itojun    495:        }
1.11      itojun    496:
                    497:        m_freem(m);
1.2       itojun    498: }
                    499:
                    500: static void
1.38      christos  501: mld_sendpkt(struct in6_multi *in6m, int type,
                    502:        const struct in6_addr *dst)
1.2       itojun    503: {
1.31      rpaulo    504:        struct mbuf *mh;
1.22      itojun    505:        struct mld_hdr *mldh;
1.31      rpaulo    506:        struct ip6_hdr *ip6 = NULL;
1.2       itojun    507:        struct ip6_moptions im6o;
1.31      rpaulo    508:        struct in6_ifaddr *ia = NULL;
1.2       itojun    509:        struct ifnet *ifp = in6m->in6m_ifp;
1.19      itojun    510:        int ignflags;
1.2       itojun    511:
                    512:        /*
                    513:         * At first, find a link local address on the outgoing interface
                    514:         * to use as the source address of the MLD packet.
1.19      itojun    515:         * We do not reject tentative addresses for MLD report to deal with
                    516:         * the case where we first join a link-local address.
1.2       itojun    517:         */
1.19      itojun    518:        ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
                    519:        if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
1.2       itojun    520:                return;
1.19      itojun    521:        if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
                    522:                ia = NULL;
1.2       itojun    523:
1.31      rpaulo    524:        /* Allocate two mbufs to store IPv6 header and MLD header */
                    525:        mldh = mld_allocbuf(&mh, sizeof(struct mld_hdr), in6m, type);
                    526:        if (mldh == NULL)
1.2       itojun    527:                return;
                    528:
1.31      rpaulo    529:        /* fill src/dst here */
                    530:        ip6 = mtod(mh, struct ip6_hdr *);
                    531:        ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
                    532:        ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
1.2       itojun    533:
1.22      itojun    534:        mldh->mld_addr = in6m->in6m_addr;
1.29      rpaulo    535:        in6_clearscope(&mldh->mld_addr); /* XXX */
1.22      itojun    536:        mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
                    537:            sizeof(struct mld_hdr));
1.2       itojun    538:
                    539:        /* construct multicast option */
1.31      rpaulo    540:        memset(&im6o, 0, sizeof(im6o));
1.2       itojun    541:        im6o.im6o_multicast_ifp = ifp;
                    542:        im6o.im6o_multicast_hlim = 1;
                    543:
                    544:        /*
                    545:         * Request loopback of the report if we are acting as a multicast
                    546:         * router, so that the process-level routing daemon can hear it.
                    547:         */
                    548:        im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
                    549:
                    550:        /* increment output statictics */
1.44      thorpej   551:        ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
1.15      itojun    552:        icmp6_ifstat_inc(ifp, ifs6_out_msg);
1.17      itojun    553:        switch (type) {
1.22      itojun    554:        case MLD_LISTENER_QUERY:
1.15      itojun    555:                icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
                    556:                break;
1.22      itojun    557:        case MLD_LISTENER_REPORT:
1.15      itojun    558:                icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
                    559:                break;
1.22      itojun    560:        case MLD_LISTENER_DONE:
1.15      itojun    561:                icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
                    562:                break;
1.7       itojun    563:        }
1.19      itojun    564:
1.27      perry     565:        ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
1.53      plunky    566:            &im6o, NULL, NULL);
1.2       itojun    567: }
1.31      rpaulo    568:
                    569: static struct mld_hdr *
1.34      christos  570: mld_allocbuf(struct mbuf **mh, int len, struct in6_multi *in6m,
1.33      christos  571:     int type)
1.31      rpaulo    572: {
                    573:        struct mbuf *md;
                    574:        struct mld_hdr *mldh;
                    575:        struct ip6_hdr *ip6;
                    576:
                    577:        /*
                    578:         * Allocate mbufs to store ip6 header and MLD header.
                    579:         * We allocate 2 mbufs and make chain in advance because
                    580:         * it is more convenient when inserting the hop-by-hop option later.
                    581:         */
                    582:        MGETHDR(*mh, M_DONTWAIT, MT_HEADER);
                    583:        if (*mh == NULL)
                    584:                return NULL;
                    585:        MGET(md, M_DONTWAIT, MT_DATA);
                    586:        if (md == NULL) {
                    587:                m_free(*mh);
                    588:                *mh = NULL;
                    589:                return NULL;
                    590:        }
                    591:        (*mh)->m_next = md;
                    592:        md->m_next = NULL;
                    593:
                    594:        (*mh)->m_pkthdr.rcvif = NULL;
                    595:        (*mh)->m_pkthdr.len = sizeof(struct ip6_hdr) + len;
                    596:        (*mh)->m_len = sizeof(struct ip6_hdr);
                    597:        MH_ALIGN(*mh, sizeof(struct ip6_hdr));
                    598:
                    599:        /* fill in the ip6 header */
                    600:        ip6 = mtod(*mh, struct ip6_hdr *);
                    601:        memset(ip6, 0, sizeof(*ip6));
                    602:        ip6->ip6_flow = 0;
                    603:        ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
                    604:        ip6->ip6_vfc |= IPV6_VERSION;
                    605:        /* ip6_plen will be set later */
                    606:        ip6->ip6_nxt = IPPROTO_ICMPV6;
                    607:        /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
                    608:        /* ip6_src/dst will be set by mld_sendpkt() or mld_sendbuf() */
                    609:
                    610:        /* fill in the MLD header as much as possible */
                    611:        md->m_len = len;
                    612:        mldh = mtod(md, struct mld_hdr *);
                    613:        memset(mldh, 0, len);
                    614:        mldh->mld_type = type;
                    615:        return mldh;
                    616: }
                    617:
                    618: /*
                    619:  * Add an address to the list of IP6 multicast addresses for a given interface.
                    620:  */
                    621: struct in6_multi *
1.38      christos  622: in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp,
                    623:        int *errorp, int timer)
1.31      rpaulo    624: {
                    625:        struct  in6_ifaddr *ia;
1.54      dyoung    626:        struct  sockaddr_in6 sin6;
1.31      rpaulo    627:        struct  in6_multi *in6m;
                    628:        int     s = splsoftnet();
                    629:
                    630:        *errorp = 0;
                    631:
                    632:        /*
                    633:         * See if address already in list.
                    634:         */
                    635:        IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
                    636:        if (in6m != NULL) {
                    637:                /*
                    638:                 * Found it; just increment the refrence count.
                    639:                 */
                    640:                in6m->in6m_refcount++;
                    641:        } else {
                    642:                /*
                    643:                 * New address; allocate a new multicast record
                    644:                 * and link it into the interface's multicast list.
                    645:                 */
                    646:                in6m = (struct in6_multi *)
1.51      dyoung    647:                        malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT|M_ZERO);
1.31      rpaulo    648:                if (in6m == NULL) {
                    649:                        splx(s);
                    650:                        *errorp = ENOBUFS;
                    651:                        return (NULL);
                    652:                }
                    653:
                    654:                in6m->in6m_addr = *maddr6;
                    655:                in6m->in6m_ifp = ifp;
                    656:                in6m->in6m_refcount = 1;
                    657:                in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    658:                IFP_TO_IA6(ifp, ia);
                    659:                if (ia == NULL) {
                    660:                        free(in6m, M_IPMADDR);
                    661:                        splx(s);
                    662:                        *errorp = EADDRNOTAVAIL; /* appropriate? */
                    663:                        return (NULL);
                    664:                }
                    665:                in6m->in6m_ia = ia;
                    666:                IFAREF(&ia->ia_ifa); /* gain a reference */
                    667:                LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
                    668:
                    669:                /*
                    670:                 * Ask the network driver to update its multicast reception
                    671:                 * filter appropriately for the new address.
                    672:                 */
1.54      dyoung    673:                sockaddr_in6_init(&sin6, maddr6, 0, 0, 0);
                    674:                *errorp = if_mcast_op(ifp, SIOCADDMULTI, sin6tosa(&sin6));
1.31      rpaulo    675:                if (*errorp) {
                    676:                        LIST_REMOVE(in6m, in6m_entry);
                    677:                        free(in6m, M_IPMADDR);
                    678:                        IFAFREE(&ia->ia_ifa);
                    679:                        splx(s);
                    680:                        return (NULL);
                    681:                }
                    682:
1.45      ad        683:                callout_init(&in6m->in6m_timer_ch, CALLOUT_MPSAFE);
1.41      joerg     684:                callout_setfunc(&in6m->in6m_timer_ch, mld_timeo, in6m);
1.32      rpaulo    685:                in6m->in6m_timer = timer;
1.31      rpaulo    686:                if (in6m->in6m_timer > 0) {
                    687:                        in6m->in6m_state = MLD_REPORTPENDING;
                    688:                        mld_starttimer(in6m);
                    689:
                    690:                        splx(s);
                    691:                        return (in6m);
                    692:                }
                    693:
                    694:                /*
                    695:                 * Let MLD6 know that we have joined a new IP6 multicast
                    696:                 * group.
                    697:                 */
                    698:                mld_start_listening(in6m);
                    699:        }
                    700:        splx(s);
                    701:        return (in6m);
                    702: }
                    703:
                    704: /*
                    705:  * Delete a multicast address record.
                    706:  */
                    707: void
1.38      christos  708: in6_delmulti(struct in6_multi *in6m)
1.31      rpaulo    709: {
1.54      dyoung    710:        struct  sockaddr_in6 sin6;
1.31      rpaulo    711:        struct  in6_ifaddr *ia;
                    712:        int     s = splsoftnet();
                    713:
                    714:        mld_stoptimer(in6m);
                    715:
                    716:        if (--in6m->in6m_refcount == 0) {
                    717:                /*
                    718:                 * No remaining claims to this record; let MLD6 know
                    719:                 * that we are leaving the multicast group.
                    720:                 */
                    721:                mld_stop_listening(in6m);
                    722:
                    723:                /*
                    724:                 * Unlink from list.
                    725:                 */
                    726:                LIST_REMOVE(in6m, in6m_entry);
1.35      dyoung    727:                if (in6m->in6m_ia != NULL) {
1.31      rpaulo    728:                        IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1.35      dyoung    729:                        in6m->in6m_ia = NULL;
1.31      rpaulo    730:                }
                    731:
                    732:                /*
                    733:                 * Delete all references of this multicasting group from
                    734:                 * the membership arrays
                    735:                 */
                    736:                for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
                    737:                        struct in6_multi_mship *imm;
1.35      dyoung    738:                        LIST_FOREACH(imm, &ia->ia6_memberships, i6mm_chain) {
1.31      rpaulo    739:                                if (imm->i6mm_maddr == in6m)
                    740:                                        imm->i6mm_maddr = NULL;
                    741:                        }
                    742:                }
                    743:
                    744:                /*
                    745:                 * Notify the network driver to update its multicast
                    746:                 * reception filter.
                    747:                 */
1.54      dyoung    748:                sockaddr_in6_init(&sin6, &in6m->in6m_addr, 0, 0, 0);
                    749:                if_mcast_op(in6m->in6m_ifp, SIOCDELMULTI, sin6tosa(&sin6));
1.59.2.1! martin    750:
        !           751:                /* Tell mld_timeo we're halting the timer */
        !           752:                in6m->in6m_timer = IN6M_TIMER_UNDEF;
        !           753:                callout_halt(&in6m->in6m_timer_ch, softnet_lock);
1.41      joerg     754:                callout_destroy(&in6m->in6m_timer_ch);
1.59.2.1! martin    755:
1.31      rpaulo    756:                free(in6m, M_IPMADDR);
                    757:        }
                    758:        splx(s);
                    759: }
                    760:
                    761:
                    762: struct in6_multi_mship *
1.38      christos  763: in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,
                    764:        int *errorp, int timer)
1.31      rpaulo    765: {
                    766:        struct in6_multi_mship *imm;
                    767:
1.51      dyoung    768:        imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT|M_ZERO);
                    769:        if (imm == NULL) {
1.31      rpaulo    770:                *errorp = ENOBUFS;
                    771:                return NULL;
                    772:        }
                    773:
1.32      rpaulo    774:        imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, timer);
1.31      rpaulo    775:        if (!imm->i6mm_maddr) {
1.36      dyoung    776:                /* *errorp is already set */
1.31      rpaulo    777:                free(imm, M_IPMADDR);
                    778:                return NULL;
                    779:        }
                    780:        return imm;
                    781: }
                    782:
                    783: int
1.38      christos  784: in6_leavegroup(struct in6_multi_mship *imm)
1.31      rpaulo    785: {
                    786:
                    787:        if (imm->i6mm_maddr) {
                    788:                in6_delmulti(imm->i6mm_maddr);
                    789:        }
                    790:        free(imm, M_IPMADDR);
                    791:        return 0;
                    792: }
                    793:
                    794:
                    795: /*
                    796:  * Multicast address kludge:
                    797:  * If there were any multicast addresses attached to this interface address,
                    798:  * either move them to another address on this interface, or save them until
                    799:  * such time as this interface is reconfigured for IPv6.
                    800:  */
                    801: void
1.38      christos  802: in6_savemkludge(struct in6_ifaddr *oia)
1.31      rpaulo    803: {
                    804:        struct in6_ifaddr *ia;
1.36      dyoung    805:        struct in6_multi *in6m;
1.31      rpaulo    806:
                    807:        IFP_TO_IA6(oia->ia_ifp, ia);
                    808:        if (ia) {       /* there is another address */
1.36      dyoung    809:                KASSERT(ia != oia);
                    810:                while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
                    811:                        LIST_REMOVE(in6m, in6m_entry);
                    812:                        IFAREF(&ia->ia_ifa);
1.31      rpaulo    813:                        IFAFREE(&in6m->in6m_ia->ia_ifa);
                    814:                        in6m->in6m_ia = ia;
                    815:                        LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
                    816:                }
                    817:        } else {        /* last address on this if deleted, save */
                    818:                struct multi6_kludge *mk;
                    819:
1.35      dyoung    820:                LIST_FOREACH(mk, &in6_mk, mk_entry) {
1.31      rpaulo    821:                        if (mk->mk_ifp == oia->ia_ifp)
                    822:                                break;
                    823:                }
                    824:                if (mk == NULL) /* this should not happen! */
                    825:                        panic("in6_savemkludge: no kludge space");
                    826:
1.36      dyoung    827:                while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
                    828:                        LIST_REMOVE(in6m, in6m_entry);
1.31      rpaulo    829:                        IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
                    830:                        in6m->in6m_ia = NULL;
                    831:                        LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
                    832:                }
                    833:        }
                    834: }
                    835:
                    836: /*
                    837:  * Continuation of multicast address hack:
                    838:  * If there was a multicast group list previously saved for this interface,
                    839:  * then we re-attach it to the first address configured on the i/f.
                    840:  */
                    841: void
1.38      christos  842: in6_restoremkludge(struct in6_ifaddr *ia, struct ifnet *ifp)
1.31      rpaulo    843: {
                    844:        struct multi6_kludge *mk;
1.36      dyoung    845:        struct in6_multi *in6m;
1.31      rpaulo    846:
1.35      dyoung    847:        LIST_FOREACH(mk, &in6_mk, mk_entry) {
                    848:                if (mk->mk_ifp == ifp)
1.31      rpaulo    849:                        break;
                    850:        }
1.35      dyoung    851:        if (mk == NULL)
                    852:                return;
1.36      dyoung    853:        while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) {
                    854:                LIST_REMOVE(in6m, in6m_entry);
1.35      dyoung    855:                in6m->in6m_ia = ia;
                    856:                IFAREF(&ia->ia_ifa);
                    857:                LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
                    858:        }
1.31      rpaulo    859: }
                    860:
                    861: /*
                    862:  * Allocate space for the kludge at interface initialization time.
                    863:  * Formerly, we dynamically allocated the space in in6_savemkludge() with
                    864:  * malloc(M_WAITOK).  However, it was wrong since the function could be called
                    865:  * under an interrupt context (software timer on address lifetime expiration).
                    866:  * Also, we cannot just give up allocating the strucutre, since the group
                    867:  * membership structure is very complex and we need to keep it anyway.
                    868:  * Of course, this function MUST NOT be called under an interrupt context.
                    869:  * Specifically, it is expected to be called only from in6_ifattach(), though
                    870:  * it is a global function.
                    871:  */
                    872: void
1.38      christos  873: in6_createmkludge(struct ifnet *ifp)
1.31      rpaulo    874: {
                    875:        struct multi6_kludge *mk;
                    876:
1.35      dyoung    877:        LIST_FOREACH(mk, &in6_mk, mk_entry) {
1.31      rpaulo    878:                /* If we've already had one, do not allocate. */
                    879:                if (mk->mk_ifp == ifp)
                    880:                        return;
                    881:        }
                    882:
1.51      dyoung    883:        mk = malloc(sizeof(*mk), M_IPMADDR, M_ZERO|M_WAITOK);
1.31      rpaulo    884:
                    885:        LIST_INIT(&mk->mk_head);
                    886:        mk->mk_ifp = ifp;
                    887:        LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
                    888: }
                    889:
                    890: void
1.38      christos  891: in6_purgemkludge(struct ifnet *ifp)
1.31      rpaulo    892: {
                    893:        struct multi6_kludge *mk;
1.36      dyoung    894:        struct in6_multi *in6m, *next;
1.31      rpaulo    895:
1.35      dyoung    896:        LIST_FOREACH(mk, &in6_mk, mk_entry) {
                    897:                if (mk->mk_ifp == ifp)
                    898:                        break;
                    899:        }
                    900:        if (mk == NULL)
                    901:                return;
                    902:
                    903:        /* leave from all multicast groups joined */
1.36      dyoung    904:        for (in6m = LIST_FIRST(&mk->mk_head); in6m != NULL; in6m = next) {
                    905:                next = LIST_NEXT(in6m, in6m_entry);
1.35      dyoung    906:                in6_delmulti(in6m);
1.31      rpaulo    907:        }
1.35      dyoung    908:        LIST_REMOVE(mk, mk_entry);
                    909:        free(mk, M_IPMADDR);
1.31      rpaulo    910: }
1.57      joerg     911:
                    912: static int
                    913: in6_mkludge_sysctl(SYSCTLFN_ARGS)
                    914: {
                    915:        struct multi6_kludge *mk;
                    916:        struct in6_multi *in6m;
                    917:        int error;
                    918:        uint32_t tmp;
                    919:        size_t written;
                    920:
                    921:        if (namelen != 1)
                    922:                return EINVAL;
                    923:
                    924:        if (oldp == NULL) {
                    925:                *oldlenp = 0;
                    926:                LIST_FOREACH(mk, &in6_mk, mk_entry) {
                    927:                        if (mk->mk_ifp->if_index == name[0])
                    928:                                continue;
                    929:                        LIST_FOREACH(in6m, &mk->mk_head, in6m_entry) {
                    930:                                *oldlenp += sizeof(struct in6_addr) +
                    931:                                    sizeof(uint32_t);
                    932:                        }
                    933:                }
                    934:                return 0;
                    935:        }
                    936:
                    937:        error = 0;
                    938:        written = 0;
                    939:        LIST_FOREACH(mk, &in6_mk, mk_entry) {
                    940:                if (mk->mk_ifp->if_index == name[0])
                    941:                        continue;
                    942:                LIST_FOREACH(in6m, &mk->mk_head, in6m_entry) {
                    943:                        if (written + sizeof(struct in6_addr) +
                    944:                            sizeof(uint32_t) > *oldlenp)
                    945:                                goto done;
                    946:                        error = sysctl_copyout(l, &in6m->in6m_addr,
                    947:                            oldp, sizeof(struct in6_addr));
                    948:                        if (error)
                    949:                                goto done;
                    950:                        oldp = (char *)oldp + sizeof(struct in6_addr);
                    951:                        written += sizeof(struct in6_addr);
                    952:                        tmp = in6m->in6m_refcount;
                    953:                        error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
                    954:                        if (error)
                    955:                                goto done;
                    956:                        oldp = (char *)oldp + sizeof(tmp);
                    957:                        written += sizeof(tmp);
                    958:                }
                    959:        }
                    960:
                    961: done:
                    962:        *oldlenp = written;
                    963:        return error;
                    964: }
                    965:
                    966: static int
                    967: in6_multicast_sysctl(SYSCTLFN_ARGS)
                    968: {
                    969:        struct ifnet *ifp;
                    970:        struct ifaddr *ifa;
                    971:        struct in6_ifaddr *ifa6;
                    972:        struct in6_multi *in6m;
                    973:        uint32_t tmp;
                    974:        int error;
                    975:        size_t written;
                    976:
                    977:        if (namelen != 1)
                    978:                return EINVAL;
                    979:
                    980:        ifp = if_byindex(name[0]);
                    981:        if (ifp == NULL)
                    982:                return ENODEV;
                    983:
                    984:        if (oldp == NULL) {
                    985:                *oldlenp = 0;
1.58      ozaki-r   986:                IFADDR_FOREACH(ifa, ifp) {
1.57      joerg     987:                        if (ifa->ifa_addr == NULL)
                    988:                                continue;
                    989:                        if (ifa->ifa_addr->sa_family != AF_INET6)
                    990:                                continue;
                    991:                        ifa6 = (struct in6_ifaddr *)ifa;
                    992:                        LIST_FOREACH(in6m, &ifa6->ia6_multiaddrs, in6m_entry) {
                    993:                                *oldlenp += 2 * sizeof(struct in6_addr) +
                    994:                                    sizeof(uint32_t);
                    995:                        }
                    996:                }
                    997:                return 0;
                    998:        }
                    999:
                   1000:        error = 0;
                   1001:        written = 0;
1.58      ozaki-r  1002:        IFADDR_FOREACH(ifa, ifp) {
1.57      joerg    1003:                if (ifa->ifa_addr == NULL)
                   1004:                        continue;
                   1005:                if (ifa->ifa_addr->sa_family != AF_INET6)
                   1006:                        continue;
                   1007:                ifa6 = (struct in6_ifaddr *)ifa;
                   1008:                LIST_FOREACH(in6m, &ifa6->ia6_multiaddrs, in6m_entry) {
                   1009:                        if (written + 2 * sizeof(struct in6_addr) +
                   1010:                            sizeof(uint32_t) > *oldlenp)
                   1011:                                goto done;
                   1012:                        error = sysctl_copyout(l, &ifa6->ia_addr.sin6_addr,
                   1013:                            oldp, sizeof(struct in6_addr));
                   1014:                        if (error)
                   1015:                                goto done;
                   1016:                        oldp = (char *)oldp + sizeof(struct in6_addr);
                   1017:                        written += sizeof(struct in6_addr);
                   1018:                        error = sysctl_copyout(l, &in6m->in6m_addr,
                   1019:                            oldp, sizeof(struct in6_addr));
                   1020:                        if (error)
                   1021:                                goto done;
                   1022:                        oldp = (char *)oldp + sizeof(struct in6_addr);
                   1023:                        written += sizeof(struct in6_addr);
                   1024:                        tmp = in6m->in6m_refcount;
                   1025:                        error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
                   1026:                        if (error)
                   1027:                                goto done;
                   1028:                        oldp = (char *)oldp + sizeof(tmp);
                   1029:                        written += sizeof(tmp);
                   1030:                }
                   1031:        }
                   1032: done:
                   1033:        *oldlenp = written;
                   1034:        return error;
                   1035: }
                   1036:
                   1037: SYSCTL_SETUP(sysctl_in6_mklude_setup, "sysctl net.inet6.multicast_kludge subtree setup")
                   1038: {
                   1039:
                   1040:        sysctl_createv(clog, 0, NULL, NULL,
                   1041:                       CTLFLAG_PERMANENT,
1.59      joerg    1042:                       CTLTYPE_NODE, "inet6", NULL,
                   1043:                       NULL, 0, NULL, 0,
                   1044:                       CTL_NET, PF_INET6, CTL_EOL);
                   1045:
                   1046:        sysctl_createv(clog, 0, NULL, NULL,
                   1047:                       CTLFLAG_PERMANENT,
1.57      joerg    1048:                       CTLTYPE_NODE, "multicast",
                   1049:                       SYSCTL_DESCR("Multicast information"),
                   1050:                       in6_multicast_sysctl, 0, NULL, 0,
                   1051:                       CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
                   1052:
                   1053:        sysctl_createv(clog, 0, NULL, NULL,
                   1054:                       CTLFLAG_PERMANENT,
                   1055:                       CTLTYPE_NODE, "multicast_kludge",
                   1056:                       SYSCTL_DESCR("multicast kludge information"),
                   1057:                       in6_mkludge_sysctl, 0, NULL, 0,
                   1058:                       CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
                   1059: }

CVSweb <webmaster@jp.NetBSD.org>