[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.2.2.1

1.59.2.2.2.1! msaitoh     1: /*     $NetBSD: mld6.c,v 1.59.2.2 2015/01/23 09:27:15 martin 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.2.2.1! msaitoh   105: __KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.59.2.2 2015/01/23 09:27:15 martin 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;
1.59.2.2  martin    191:        ip6_opts.ip6po_prefer_tempaddr = IP6PO_TEMPADDR_NOTPREFER;
1.2       itojun    192: }
                    193:
1.31      rpaulo    194: static void
1.38      christos  195: mld_starttimer(struct in6_multi *in6m)
1.31      rpaulo    196: {
                    197:        struct timeval now;
                    198:
1.59.2.1  martin    199:        KASSERT(in6m->in6m_timer != IN6M_TIMER_UNDEF);
                    200:
1.31      rpaulo    201:        microtime(&now);
                    202:        in6m->in6m_timer_expire.tv_sec = now.tv_sec + in6m->in6m_timer / hz;
                    203:        in6m->in6m_timer_expire.tv_usec = now.tv_usec +
                    204:            (in6m->in6m_timer % hz) * (1000000 / hz);
                    205:        if (in6m->in6m_timer_expire.tv_usec > 1000000) {
                    206:                in6m->in6m_timer_expire.tv_sec++;
                    207:                in6m->in6m_timer_expire.tv_usec -= 1000000;
                    208:        }
                    209:
                    210:        /* start or restart the timer */
1.41      joerg     211:        callout_schedule(&in6m->in6m_timer_ch, in6m->in6m_timer);
1.31      rpaulo    212: }
                    213:
                    214: static void
1.38      christos  215: mld_stoptimer(struct in6_multi *in6m)
1.31      rpaulo    216: {
                    217:        if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
                    218:                return;
                    219:
1.41      joerg     220:        callout_stop(&in6m->in6m_timer_ch);
1.31      rpaulo    221:
                    222:        in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    223: }
                    224:
                    225: static void
1.41      joerg     226: mld_timeo(void *arg)
1.31      rpaulo    227: {
1.41      joerg     228:        struct in6_multi *in6m = arg;
1.45      ad        229:
                    230:        mutex_enter(softnet_lock);
                    231:        KERNEL_LOCK(1, NULL);
1.31      rpaulo    232:
1.59.2.1  martin    233:        if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
                    234:                goto out;
                    235:
1.31      rpaulo    236:        in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    237:
                    238:        switch (in6m->in6m_state) {
                    239:        case MLD_REPORTPENDING:
                    240:                mld_start_listening(in6m);
                    241:                break;
                    242:        default:
                    243:                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
                    244:                break;
                    245:        }
                    246:
1.59.2.1  martin    247: out:
1.45      ad        248:        KERNEL_UNLOCK_ONE(NULL);
                    249:        mutex_exit(softnet_lock);
1.31      rpaulo    250: }
                    251:
                    252: static u_long
1.38      christos  253: mld_timerresid(struct in6_multi *in6m)
1.31      rpaulo    254: {
                    255:        struct timeval now, diff;
                    256:
                    257:        microtime(&now);
                    258:
                    259:        if (now.tv_sec > in6m->in6m_timer_expire.tv_sec ||
                    260:            (now.tv_sec == in6m->in6m_timer_expire.tv_sec &&
                    261:            now.tv_usec > in6m->in6m_timer_expire.tv_usec)) {
                    262:                return (0);
                    263:        }
                    264:        diff = in6m->in6m_timer_expire;
                    265:        diff.tv_sec -= now.tv_sec;
                    266:        diff.tv_usec -= now.tv_usec;
                    267:        if (diff.tv_usec < 0) {
                    268:                diff.tv_sec--;
                    269:                diff.tv_usec += 1000000;
                    270:        }
                    271:
                    272:        /* return the remaining time in milliseconds */
1.47      adrianp   273:        return diff.tv_sec * 1000 + diff.tv_usec / 1000;
1.31      rpaulo    274: }
                    275:
                    276: static void
1.38      christos  277: mld_start_listening(struct in6_multi *in6m)
1.2       itojun    278: {
1.29      rpaulo    279:        struct in6_addr all_in6;
                    280:
1.2       itojun    281:        /*
1.11      itojun    282:         * RFC2710 page 10:
1.2       itojun    283:         * The node never sends a Report or Done for the link-scope all-nodes
                    284:         * address.
                    285:         * MLD messages are never sent for multicast addresses whose scope is 0
                    286:         * (reserved) or 1 (node-local).
                    287:         */
1.29      rpaulo    288:        all_in6 = in6addr_linklocal_allnodes;
                    289:        if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) {
                    290:                /* XXX: this should not happen! */
                    291:                in6m->in6m_timer = 0;
                    292:                in6m->in6m_state = MLD_OTHERLISTENER;
                    293:        }
                    294:        if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
1.2       itojun    295:            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
1.31      rpaulo    296:                in6m->in6m_timer = IN6M_TIMER_UNDEF;
1.22      itojun    297:                in6m->in6m_state = MLD_OTHERLISTENER;
1.2       itojun    298:        } else {
1.31      rpaulo    299:                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
1.55      tls       300:                in6m->in6m_timer = cprng_fast32() %
1.31      rpaulo    301:                    (MLD_UNSOLICITED_REPORT_INTERVAL * hz);
1.22      itojun    302:                in6m->in6m_state = MLD_IREPORTEDLAST;
1.31      rpaulo    303:
                    304:                mld_starttimer(in6m);
1.2       itojun    305:        }
                    306: }
                    307:
1.31      rpaulo    308: static void
1.38      christos  309: mld_stop_listening(struct in6_multi *in6m)
1.2       itojun    310: {
1.29      rpaulo    311:        struct in6_addr allnode, allrouter;
                    312:
                    313:        allnode = in6addr_linklocal_allnodes;
                    314:        if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) {
                    315:                /* XXX: this should not happen! */
                    316:                return;
                    317:        }
                    318:        allrouter = in6addr_linklocal_allrouters;
                    319:        if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) {
                    320:                /* XXX impossible */
                    321:                return;
                    322:        }
1.2       itojun    323:
1.22      itojun    324:        if (in6m->in6m_state == MLD_IREPORTEDLAST &&
1.29      rpaulo    325:            (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode)) &&
                    326:            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
                    327:            IPV6_ADDR_SCOPE_INTFACELOCAL) {
1.31      rpaulo    328:                mld_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
1.29      rpaulo    329:        }
1.2       itojun    330: }
                    331:
                    332: void
1.38      christos  333: mld_input(struct mbuf *m, int off)
1.2       itojun    334: {
1.52      dholland  335:        struct ip6_hdr *ip6;
1.22      itojun    336:        struct mld_hdr *mldh;
1.2       itojun    337:        struct ifnet *ifp = m->m_pkthdr.rcvif;
1.31      rpaulo    338:        struct in6_multi *in6m = NULL;
1.29      rpaulo    339:        struct in6_addr mld_addr, all_in6;
1.2       itojun    340:        struct in6_ifaddr *ia;
1.47      adrianp   341:        u_long timer = 0;       /* timer value in the MLD query header */
1.2       itojun    342:
1.22      itojun    343:        IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
1.13      itojun    344:        if (mldh == NULL) {
1.44      thorpej   345:                ICMP6_STATINC(ICMP6_STAT_TOOSHORT);
1.13      itojun    346:                return;
                    347:        }
                    348:
1.2       itojun    349:        /* source address validation */
1.13      itojun    350:        ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
1.2       itojun    351:        if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
1.31      rpaulo    352:                /*
                    353:                 * RFC3590 allows the IPv6 unspecified address as the source
                    354:                 * address of MLD report and done messages.  However, as this
                    355:                 * same document says, this special rule is for snooping
                    356:                 * switches and the RFC requires routers to discard MLD packets
                    357:                 * with the unspecified source address.  The RFC only talks
                    358:                 * about hosts receiving an MLD query or report in Security
                    359:                 * Considerations, but this is probably the correct intention.
                    360:                 * RFC3590 does not talk about other cases than link-local and
                    361:                 * the unspecified source addresses, but we believe the same
                    362:                 * rule should be applied.
                    363:                 * As a result, we only allow link-local addresses as the
                    364:                 * source address; otherwise, simply discard the packet.
                    365:                 */
1.18      itojun    366: #if 0
1.31      rpaulo    367:                /*
                    368:                 * XXX: do not log in an input path to avoid log flooding,
                    369:                 * though RFC3590 says "SHOULD log" if the source of a query
                    370:                 * is the unspecified address.
                    371:                 */
                    372:                log(LOG_INFO,
1.22      itojun    373:                    "mld_input: src %s is not link-local (grp=%s)\n",
1.31      rpaulo    374:                    ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&mldh->mld_addr));
1.18      itojun    375: #endif
1.11      itojun    376:                m_freem(m);
1.2       itojun    377:                return;
                    378:        }
                    379:
                    380:        /*
1.29      rpaulo    381:         * make a copy for local work (in6_setscope() may modify the 1st arg)
                    382:         */
                    383:        mld_addr = mldh->mld_addr;
                    384:        if (in6_setscope(&mld_addr, ifp, NULL)) {
                    385:                /* XXX: this should not happen! */
                    386:                m_free(m);
                    387:                return;
                    388:        }
                    389:
                    390:        /*
1.31      rpaulo    391:         * In the MLD specification, there are 3 states and a flag.
1.2       itojun    392:         *
                    393:         * In Non-Listener state, we simply don't have a membership record.
                    394:         * In Delaying Listener state, our timer is running (in6m->in6m_timer)
1.31      rpaulo    395:         * In Idle Listener state, our timer is not running
                    396:         * (in6m->in6m_timer==IN6M_TIMER_UNDEF)
1.2       itojun    397:         *
1.22      itojun    398:         * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
                    399:         * we have heard a report from another member, or MLD_IREPORTEDLAST
1.2       itojun    400:         * if we sent the last report.
                    401:         */
1.22      itojun    402:        switch (mldh->mld_type) {
                    403:        case MLD_LISTENER_QUERY:
1.7       itojun    404:                if (ifp->if_flags & IFF_LOOPBACK)
                    405:                        break;
                    406:
1.29      rpaulo    407:                if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
                    408:                    !IN6_IS_ADDR_MULTICAST(&mld_addr))
1.7       itojun    409:                        break;  /* print error or log stat? */
1.29      rpaulo    410:
                    411:                all_in6 = in6addr_linklocal_allnodes;
                    412:                if (in6_setscope(&all_in6, ifp, NULL)) {
                    413:                        /* XXX: this should not happen! */
                    414:                        break;
                    415:                }
1.2       itojun    416:
1.7       itojun    417:                /*
1.11      itojun    418:                 * - Start the timers in all of our membership records
                    419:                 *   that the query applies to for the interface on
                    420:                 *   which the query arrived excl. those that belong
                    421:                 *   to the "all-nodes" group (ff02::1).
                    422:                 * - Restart any timer that is already running but has
1.30      rpaulo    423:                 *   a value longer than the requested timeout.
1.11      itojun    424:                 * - Use the value specified in the query message as
                    425:                 *   the maximum timeout.
                    426:                 */
1.31      rpaulo    427:                timer = ntohs(mldh->mld_maxdelay);
                    428:
1.7       itojun    429:                IFP_TO_IA6(ifp, ia);
                    430:                if (ia == NULL)
                    431:                        break;
1.2       itojun    432:
1.35      dyoung    433:                LIST_FOREACH(in6m, &ia->ia6_multiaddrs, in6m_entry) {
1.29      rpaulo    434:                        if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
1.7       itojun    435:                            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
                    436:                            IPV6_ADDR_SCOPE_LINKLOCAL)
                    437:                                continue;
1.2       itojun    438:
1.31      rpaulo    439:                        if (in6m->in6m_state == MLD_REPORTPENDING)
                    440:                                continue; /* we are not yet ready */
                    441:
                    442:                        if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
                    443:                            !IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr))
                    444:                                continue;
                    445:
                    446:                        if (timer == 0) {
                    447:                                /* send a report immediately */
                    448:                                mld_stoptimer(in6m);
                    449:                                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
                    450:                                in6m->in6m_state = MLD_IREPORTEDLAST;
                    451:                        } else if (in6m->in6m_timer == IN6M_TIMER_UNDEF ||
1.47      adrianp   452:                            mld_timerresid(in6m) > timer) {
                    453:                                in6m->in6m_timer =
1.55      tls       454:                                   1 + (cprng_fast32() % timer) * hz / 1000;
1.31      rpaulo    455:                                mld_starttimer(in6m);
1.7       itojun    456:                        }
                    457:                }
1.29      rpaulo    458:                break;
1.2       itojun    459:
1.22      itojun    460:        case MLD_LISTENER_REPORT:
1.2       itojun    461:                /*
1.11      itojun    462:                 * For fast leave to work, we have to know that we are the
                    463:                 * last person to send a report for this group.  Reports
                    464:                 * can potentially get looped back if we are a multicast
                    465:                 * router, so discard reports sourced by me.
                    466:                 * Note that it is impossible to check IFF_LOOPBACK flag of
                    467:                 * ifp for this purpose, since ip6_mloopback pass the physical
                    468:                 * interface to looutput.
                    469:                 */
1.7       itojun    470:                if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
                    471:                        break;
                    472:
1.22      itojun    473:                if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
1.7       itojun    474:                        break;
                    475:
                    476:                /*
1.11      itojun    477:                 * If we belong to the group being reported, stop
                    478:                 * our timer for that group.
                    479:                 */
1.29      rpaulo    480:                IN6_LOOKUP_MULTI(mld_addr, ifp, in6m);
1.7       itojun    481:                if (in6m) {
1.31      rpaulo    482:                        mld_stoptimer(in6m); /* transit to idle state */
1.22      itojun    483:                        in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
1.7       itojun    484:                }
                    485:                break;
                    486:        default:                /* this is impossible */
1.18      itojun    487: #if 0
1.19      itojun    488:                /*
                    489:                 * this case should be impossible because of filtering in
                    490:                 * icmp6_input().  But we explicitly disabled this part
                    491:                 * just in case.
                    492:                 */
1.31      rpaulo    493:                log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type);
1.18      itojun    494: #endif
1.7       itojun    495:                break;
1.2       itojun    496:        }
1.11      itojun    497:
                    498:        m_freem(m);
1.2       itojun    499: }
                    500:
                    501: static void
1.38      christos  502: mld_sendpkt(struct in6_multi *in6m, int type,
                    503:        const struct in6_addr *dst)
1.2       itojun    504: {
1.31      rpaulo    505:        struct mbuf *mh;
1.22      itojun    506:        struct mld_hdr *mldh;
1.31      rpaulo    507:        struct ip6_hdr *ip6 = NULL;
1.2       itojun    508:        struct ip6_moptions im6o;
1.31      rpaulo    509:        struct in6_ifaddr *ia = NULL;
1.2       itojun    510:        struct ifnet *ifp = in6m->in6m_ifp;
1.19      itojun    511:        int ignflags;
1.2       itojun    512:
                    513:        /*
                    514:         * At first, find a link local address on the outgoing interface
                    515:         * to use as the source address of the MLD packet.
1.19      itojun    516:         * We do not reject tentative addresses for MLD report to deal with
                    517:         * the case where we first join a link-local address.
1.2       itojun    518:         */
1.19      itojun    519:        ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
                    520:        if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
1.2       itojun    521:                return;
1.19      itojun    522:        if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
                    523:                ia = NULL;
1.2       itojun    524:
1.31      rpaulo    525:        /* Allocate two mbufs to store IPv6 header and MLD header */
                    526:        mldh = mld_allocbuf(&mh, sizeof(struct mld_hdr), in6m, type);
                    527:        if (mldh == NULL)
1.2       itojun    528:                return;
                    529:
1.31      rpaulo    530:        /* fill src/dst here */
                    531:        ip6 = mtod(mh, struct ip6_hdr *);
                    532:        ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
                    533:        ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
1.2       itojun    534:
1.22      itojun    535:        mldh->mld_addr = in6m->in6m_addr;
1.29      rpaulo    536:        in6_clearscope(&mldh->mld_addr); /* XXX */
1.22      itojun    537:        mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
                    538:            sizeof(struct mld_hdr));
1.2       itojun    539:
                    540:        /* construct multicast option */
1.31      rpaulo    541:        memset(&im6o, 0, sizeof(im6o));
1.2       itojun    542:        im6o.im6o_multicast_ifp = ifp;
                    543:        im6o.im6o_multicast_hlim = 1;
                    544:
                    545:        /*
                    546:         * Request loopback of the report if we are acting as a multicast
                    547:         * router, so that the process-level routing daemon can hear it.
                    548:         */
                    549:        im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
                    550:
                    551:        /* increment output statictics */
1.44      thorpej   552:        ICMP6_STATINC(ICMP6_STAT_OUTHIST + type);
1.15      itojun    553:        icmp6_ifstat_inc(ifp, ifs6_out_msg);
1.17      itojun    554:        switch (type) {
1.22      itojun    555:        case MLD_LISTENER_QUERY:
1.15      itojun    556:                icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
                    557:                break;
1.22      itojun    558:        case MLD_LISTENER_REPORT:
1.15      itojun    559:                icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
                    560:                break;
1.22      itojun    561:        case MLD_LISTENER_DONE:
1.15      itojun    562:                icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
                    563:                break;
1.7       itojun    564:        }
1.19      itojun    565:
1.27      perry     566:        ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
1.53      plunky    567:            &im6o, NULL, NULL);
1.2       itojun    568: }
1.31      rpaulo    569:
                    570: static struct mld_hdr *
1.34      christos  571: mld_allocbuf(struct mbuf **mh, int len, struct in6_multi *in6m,
1.33      christos  572:     int type)
1.31      rpaulo    573: {
                    574:        struct mbuf *md;
                    575:        struct mld_hdr *mldh;
                    576:        struct ip6_hdr *ip6;
                    577:
                    578:        /*
                    579:         * Allocate mbufs to store ip6 header and MLD header.
                    580:         * We allocate 2 mbufs and make chain in advance because
                    581:         * it is more convenient when inserting the hop-by-hop option later.
                    582:         */
                    583:        MGETHDR(*mh, M_DONTWAIT, MT_HEADER);
                    584:        if (*mh == NULL)
                    585:                return NULL;
                    586:        MGET(md, M_DONTWAIT, MT_DATA);
                    587:        if (md == NULL) {
                    588:                m_free(*mh);
                    589:                *mh = NULL;
                    590:                return NULL;
                    591:        }
                    592:        (*mh)->m_next = md;
                    593:        md->m_next = NULL;
                    594:
                    595:        (*mh)->m_pkthdr.rcvif = NULL;
                    596:        (*mh)->m_pkthdr.len = sizeof(struct ip6_hdr) + len;
                    597:        (*mh)->m_len = sizeof(struct ip6_hdr);
                    598:        MH_ALIGN(*mh, sizeof(struct ip6_hdr));
                    599:
                    600:        /* fill in the ip6 header */
                    601:        ip6 = mtod(*mh, struct ip6_hdr *);
                    602:        memset(ip6, 0, sizeof(*ip6));
                    603:        ip6->ip6_flow = 0;
                    604:        ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
                    605:        ip6->ip6_vfc |= IPV6_VERSION;
                    606:        /* ip6_plen will be set later */
                    607:        ip6->ip6_nxt = IPPROTO_ICMPV6;
                    608:        /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
                    609:        /* ip6_src/dst will be set by mld_sendpkt() or mld_sendbuf() */
                    610:
                    611:        /* fill in the MLD header as much as possible */
                    612:        md->m_len = len;
                    613:        mldh = mtod(md, struct mld_hdr *);
                    614:        memset(mldh, 0, len);
                    615:        mldh->mld_type = type;
                    616:        return mldh;
                    617: }
                    618:
                    619: /*
                    620:  * Add an address to the list of IP6 multicast addresses for a given interface.
                    621:  */
                    622: struct in6_multi *
1.38      christos  623: in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp,
                    624:        int *errorp, int timer)
1.31      rpaulo    625: {
                    626:        struct  in6_ifaddr *ia;
1.54      dyoung    627:        struct  sockaddr_in6 sin6;
1.31      rpaulo    628:        struct  in6_multi *in6m;
                    629:        int     s = splsoftnet();
                    630:
                    631:        *errorp = 0;
                    632:
                    633:        /*
                    634:         * See if address already in list.
                    635:         */
                    636:        IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
                    637:        if (in6m != NULL) {
                    638:                /*
                    639:                 * Found it; just increment the refrence count.
                    640:                 */
                    641:                in6m->in6m_refcount++;
                    642:        } else {
                    643:                /*
                    644:                 * New address; allocate a new multicast record
                    645:                 * and link it into the interface's multicast list.
                    646:                 */
                    647:                in6m = (struct in6_multi *)
1.51      dyoung    648:                        malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT|M_ZERO);
1.31      rpaulo    649:                if (in6m == NULL) {
                    650:                        splx(s);
                    651:                        *errorp = ENOBUFS;
                    652:                        return (NULL);
                    653:                }
                    654:
                    655:                in6m->in6m_addr = *maddr6;
                    656:                in6m->in6m_ifp = ifp;
                    657:                in6m->in6m_refcount = 1;
                    658:                in6m->in6m_timer = IN6M_TIMER_UNDEF;
1.59.2.2.2.1! msaitoh   659:                callout_init(&in6m->in6m_timer_ch, CALLOUT_MPSAFE);
        !           660:                callout_setfunc(&in6m->in6m_timer_ch, mld_timeo, in6m);
        !           661:
1.31      rpaulo    662:                IFP_TO_IA6(ifp, ia);
                    663:                if (ia == NULL) {
1.59.2.2.2.1! msaitoh   664:                        callout_destroy(&in6m->in6m_timer_ch);
1.31      rpaulo    665:                        free(in6m, M_IPMADDR);
                    666:                        splx(s);
                    667:                        *errorp = EADDRNOTAVAIL; /* appropriate? */
                    668:                        return (NULL);
                    669:                }
                    670:                in6m->in6m_ia = ia;
                    671:                IFAREF(&ia->ia_ifa); /* gain a reference */
                    672:                LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
                    673:
                    674:                /*
                    675:                 * Ask the network driver to update its multicast reception
                    676:                 * filter appropriately for the new address.
                    677:                 */
1.54      dyoung    678:                sockaddr_in6_init(&sin6, maddr6, 0, 0, 0);
                    679:                *errorp = if_mcast_op(ifp, SIOCADDMULTI, sin6tosa(&sin6));
1.31      rpaulo    680:                if (*errorp) {
1.59.2.2.2.1! msaitoh   681:                        callout_destroy(&in6m->in6m_timer_ch);
1.31      rpaulo    682:                        LIST_REMOVE(in6m, in6m_entry);
                    683:                        free(in6m, M_IPMADDR);
                    684:                        IFAFREE(&ia->ia_ifa);
                    685:                        splx(s);
                    686:                        return (NULL);
                    687:                }
                    688:
1.32      rpaulo    689:                in6m->in6m_timer = timer;
1.31      rpaulo    690:                if (in6m->in6m_timer > 0) {
                    691:                        in6m->in6m_state = MLD_REPORTPENDING;
                    692:                        mld_starttimer(in6m);
                    693:
                    694:                        splx(s);
                    695:                        return (in6m);
                    696:                }
                    697:
                    698:                /*
                    699:                 * Let MLD6 know that we have joined a new IP6 multicast
                    700:                 * group.
                    701:                 */
                    702:                mld_start_listening(in6m);
                    703:        }
                    704:        splx(s);
                    705:        return (in6m);
                    706: }
                    707:
                    708: /*
                    709:  * Delete a multicast address record.
                    710:  */
                    711: void
1.38      christos  712: in6_delmulti(struct in6_multi *in6m)
1.31      rpaulo    713: {
1.54      dyoung    714:        struct  sockaddr_in6 sin6;
1.31      rpaulo    715:        struct  in6_ifaddr *ia;
                    716:        int     s = splsoftnet();
                    717:
                    718:        mld_stoptimer(in6m);
                    719:
                    720:        if (--in6m->in6m_refcount == 0) {
                    721:                /*
                    722:                 * No remaining claims to this record; let MLD6 know
                    723:                 * that we are leaving the multicast group.
                    724:                 */
                    725:                mld_stop_listening(in6m);
                    726:
                    727:                /*
                    728:                 * Unlink from list.
                    729:                 */
                    730:                LIST_REMOVE(in6m, in6m_entry);
1.35      dyoung    731:                if (in6m->in6m_ia != NULL) {
1.31      rpaulo    732:                        IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
1.35      dyoung    733:                        in6m->in6m_ia = NULL;
1.31      rpaulo    734:                }
                    735:
                    736:                /*
                    737:                 * Delete all references of this multicasting group from
                    738:                 * the membership arrays
                    739:                 */
                    740:                for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
                    741:                        struct in6_multi_mship *imm;
1.35      dyoung    742:                        LIST_FOREACH(imm, &ia->ia6_memberships, i6mm_chain) {
1.31      rpaulo    743:                                if (imm->i6mm_maddr == in6m)
                    744:                                        imm->i6mm_maddr = NULL;
                    745:                        }
                    746:                }
                    747:
                    748:                /*
                    749:                 * Notify the network driver to update its multicast
                    750:                 * reception filter.
                    751:                 */
1.54      dyoung    752:                sockaddr_in6_init(&sin6, &in6m->in6m_addr, 0, 0, 0);
                    753:                if_mcast_op(in6m->in6m_ifp, SIOCDELMULTI, sin6tosa(&sin6));
1.59.2.1  martin    754:
                    755:                /* Tell mld_timeo we're halting the timer */
                    756:                in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    757:                callout_halt(&in6m->in6m_timer_ch, softnet_lock);
1.41      joerg     758:                callout_destroy(&in6m->in6m_timer_ch);
1.59.2.1  martin    759:
1.31      rpaulo    760:                free(in6m, M_IPMADDR);
                    761:        }
                    762:        splx(s);
                    763: }
                    764:
                    765:
                    766: struct in6_multi_mship *
1.38      christos  767: in6_joingroup(struct ifnet *ifp, struct in6_addr *addr,
                    768:        int *errorp, int timer)
1.31      rpaulo    769: {
                    770:        struct in6_multi_mship *imm;
                    771:
1.51      dyoung    772:        imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT|M_ZERO);
                    773:        if (imm == NULL) {
1.31      rpaulo    774:                *errorp = ENOBUFS;
                    775:                return NULL;
                    776:        }
                    777:
1.32      rpaulo    778:        imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, timer);
1.31      rpaulo    779:        if (!imm->i6mm_maddr) {
1.36      dyoung    780:                /* *errorp is already set */
1.31      rpaulo    781:                free(imm, M_IPMADDR);
                    782:                return NULL;
                    783:        }
                    784:        return imm;
                    785: }
                    786:
                    787: int
1.38      christos  788: in6_leavegroup(struct in6_multi_mship *imm)
1.31      rpaulo    789: {
                    790:
                    791:        if (imm->i6mm_maddr) {
                    792:                in6_delmulti(imm->i6mm_maddr);
                    793:        }
                    794:        free(imm, M_IPMADDR);
                    795:        return 0;
                    796: }
                    797:
                    798:
                    799: /*
                    800:  * Multicast address kludge:
                    801:  * If there were any multicast addresses attached to this interface address,
                    802:  * either move them to another address on this interface, or save them until
                    803:  * such time as this interface is reconfigured for IPv6.
                    804:  */
                    805: void
1.38      christos  806: in6_savemkludge(struct in6_ifaddr *oia)
1.31      rpaulo    807: {
                    808:        struct in6_ifaddr *ia;
1.36      dyoung    809:        struct in6_multi *in6m;
1.31      rpaulo    810:
                    811:        IFP_TO_IA6(oia->ia_ifp, ia);
                    812:        if (ia) {       /* there is another address */
1.36      dyoung    813:                KASSERT(ia != oia);
                    814:                while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
                    815:                        LIST_REMOVE(in6m, in6m_entry);
                    816:                        IFAREF(&ia->ia_ifa);
1.31      rpaulo    817:                        IFAFREE(&in6m->in6m_ia->ia_ifa);
                    818:                        in6m->in6m_ia = ia;
                    819:                        LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
                    820:                }
                    821:        } else {        /* last address on this if deleted, save */
                    822:                struct multi6_kludge *mk;
                    823:
1.35      dyoung    824:                LIST_FOREACH(mk, &in6_mk, mk_entry) {
1.31      rpaulo    825:                        if (mk->mk_ifp == oia->ia_ifp)
                    826:                                break;
                    827:                }
                    828:                if (mk == NULL) /* this should not happen! */
                    829:                        panic("in6_savemkludge: no kludge space");
                    830:
1.36      dyoung    831:                while ((in6m = LIST_FIRST(&oia->ia6_multiaddrs)) != NULL) {
                    832:                        LIST_REMOVE(in6m, in6m_entry);
1.31      rpaulo    833:                        IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
                    834:                        in6m->in6m_ia = NULL;
                    835:                        LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
                    836:                }
                    837:        }
                    838: }
                    839:
                    840: /*
                    841:  * Continuation of multicast address hack:
                    842:  * If there was a multicast group list previously saved for this interface,
                    843:  * then we re-attach it to the first address configured on the i/f.
                    844:  */
                    845: void
1.38      christos  846: in6_restoremkludge(struct in6_ifaddr *ia, struct ifnet *ifp)
1.31      rpaulo    847: {
                    848:        struct multi6_kludge *mk;
1.36      dyoung    849:        struct in6_multi *in6m;
1.31      rpaulo    850:
1.35      dyoung    851:        LIST_FOREACH(mk, &in6_mk, mk_entry) {
                    852:                if (mk->mk_ifp == ifp)
1.31      rpaulo    853:                        break;
                    854:        }
1.35      dyoung    855:        if (mk == NULL)
                    856:                return;
1.36      dyoung    857:        while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) {
                    858:                LIST_REMOVE(in6m, in6m_entry);
1.35      dyoung    859:                in6m->in6m_ia = ia;
                    860:                IFAREF(&ia->ia_ifa);
                    861:                LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
                    862:        }
1.31      rpaulo    863: }
                    864:
                    865: /*
                    866:  * Allocate space for the kludge at interface initialization time.
                    867:  * Formerly, we dynamically allocated the space in in6_savemkludge() with
                    868:  * malloc(M_WAITOK).  However, it was wrong since the function could be called
                    869:  * under an interrupt context (software timer on address lifetime expiration).
                    870:  * Also, we cannot just give up allocating the strucutre, since the group
                    871:  * membership structure is very complex and we need to keep it anyway.
                    872:  * Of course, this function MUST NOT be called under an interrupt context.
                    873:  * Specifically, it is expected to be called only from in6_ifattach(), though
                    874:  * it is a global function.
                    875:  */
                    876: void
1.38      christos  877: in6_createmkludge(struct ifnet *ifp)
1.31      rpaulo    878: {
                    879:        struct multi6_kludge *mk;
                    880:
1.35      dyoung    881:        LIST_FOREACH(mk, &in6_mk, mk_entry) {
1.31      rpaulo    882:                /* If we've already had one, do not allocate. */
                    883:                if (mk->mk_ifp == ifp)
                    884:                        return;
                    885:        }
                    886:
1.51      dyoung    887:        mk = malloc(sizeof(*mk), M_IPMADDR, M_ZERO|M_WAITOK);
1.31      rpaulo    888:
                    889:        LIST_INIT(&mk->mk_head);
                    890:        mk->mk_ifp = ifp;
                    891:        LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
                    892: }
                    893:
                    894: void
1.38      christos  895: in6_purgemkludge(struct ifnet *ifp)
1.31      rpaulo    896: {
                    897:        struct multi6_kludge *mk;
1.36      dyoung    898:        struct in6_multi *in6m, *next;
1.31      rpaulo    899:
1.35      dyoung    900:        LIST_FOREACH(mk, &in6_mk, mk_entry) {
                    901:                if (mk->mk_ifp == ifp)
                    902:                        break;
                    903:        }
                    904:        if (mk == NULL)
                    905:                return;
                    906:
                    907:        /* leave from all multicast groups joined */
1.36      dyoung    908:        for (in6m = LIST_FIRST(&mk->mk_head); in6m != NULL; in6m = next) {
                    909:                next = LIST_NEXT(in6m, in6m_entry);
1.35      dyoung    910:                in6_delmulti(in6m);
1.31      rpaulo    911:        }
1.35      dyoung    912:        LIST_REMOVE(mk, mk_entry);
                    913:        free(mk, M_IPMADDR);
1.31      rpaulo    914: }
1.57      joerg     915:
                    916: static int
                    917: in6_mkludge_sysctl(SYSCTLFN_ARGS)
                    918: {
                    919:        struct multi6_kludge *mk;
                    920:        struct in6_multi *in6m;
                    921:        int error;
                    922:        uint32_t tmp;
                    923:        size_t written;
                    924:
                    925:        if (namelen != 1)
                    926:                return EINVAL;
                    927:
                    928:        if (oldp == NULL) {
                    929:                *oldlenp = 0;
                    930:                LIST_FOREACH(mk, &in6_mk, mk_entry) {
                    931:                        if (mk->mk_ifp->if_index == name[0])
                    932:                                continue;
                    933:                        LIST_FOREACH(in6m, &mk->mk_head, in6m_entry) {
                    934:                                *oldlenp += sizeof(struct in6_addr) +
                    935:                                    sizeof(uint32_t);
                    936:                        }
                    937:                }
                    938:                return 0;
                    939:        }
                    940:
                    941:        error = 0;
                    942:        written = 0;
                    943:        LIST_FOREACH(mk, &in6_mk, mk_entry) {
                    944:                if (mk->mk_ifp->if_index == name[0])
                    945:                        continue;
                    946:                LIST_FOREACH(in6m, &mk->mk_head, in6m_entry) {
                    947:                        if (written + sizeof(struct in6_addr) +
                    948:                            sizeof(uint32_t) > *oldlenp)
                    949:                                goto done;
                    950:                        error = sysctl_copyout(l, &in6m->in6m_addr,
                    951:                            oldp, sizeof(struct in6_addr));
                    952:                        if (error)
                    953:                                goto done;
                    954:                        oldp = (char *)oldp + sizeof(struct in6_addr);
                    955:                        written += sizeof(struct in6_addr);
                    956:                        tmp = in6m->in6m_refcount;
                    957:                        error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
                    958:                        if (error)
                    959:                                goto done;
                    960:                        oldp = (char *)oldp + sizeof(tmp);
                    961:                        written += sizeof(tmp);
                    962:                }
                    963:        }
                    964:
                    965: done:
                    966:        *oldlenp = written;
                    967:        return error;
                    968: }
                    969:
                    970: static int
                    971: in6_multicast_sysctl(SYSCTLFN_ARGS)
                    972: {
                    973:        struct ifnet *ifp;
                    974:        struct ifaddr *ifa;
                    975:        struct in6_ifaddr *ifa6;
                    976:        struct in6_multi *in6m;
                    977:        uint32_t tmp;
                    978:        int error;
                    979:        size_t written;
                    980:
                    981:        if (namelen != 1)
                    982:                return EINVAL;
                    983:
                    984:        ifp = if_byindex(name[0]);
                    985:        if (ifp == NULL)
                    986:                return ENODEV;
                    987:
                    988:        if (oldp == NULL) {
                    989:                *oldlenp = 0;
1.58      ozaki-r   990:                IFADDR_FOREACH(ifa, ifp) {
1.57      joerg     991:                        if (ifa->ifa_addr == NULL)
                    992:                                continue;
                    993:                        if (ifa->ifa_addr->sa_family != AF_INET6)
                    994:                                continue;
                    995:                        ifa6 = (struct in6_ifaddr *)ifa;
                    996:                        LIST_FOREACH(in6m, &ifa6->ia6_multiaddrs, in6m_entry) {
                    997:                                *oldlenp += 2 * sizeof(struct in6_addr) +
                    998:                                    sizeof(uint32_t);
                    999:                        }
                   1000:                }
                   1001:                return 0;
                   1002:        }
                   1003:
                   1004:        error = 0;
                   1005:        written = 0;
1.58      ozaki-r  1006:        IFADDR_FOREACH(ifa, ifp) {
1.57      joerg    1007:                if (ifa->ifa_addr == NULL)
                   1008:                        continue;
                   1009:                if (ifa->ifa_addr->sa_family != AF_INET6)
                   1010:                        continue;
                   1011:                ifa6 = (struct in6_ifaddr *)ifa;
                   1012:                LIST_FOREACH(in6m, &ifa6->ia6_multiaddrs, in6m_entry) {
                   1013:                        if (written + 2 * sizeof(struct in6_addr) +
                   1014:                            sizeof(uint32_t) > *oldlenp)
                   1015:                                goto done;
                   1016:                        error = sysctl_copyout(l, &ifa6->ia_addr.sin6_addr,
                   1017:                            oldp, sizeof(struct in6_addr));
                   1018:                        if (error)
                   1019:                                goto done;
                   1020:                        oldp = (char *)oldp + sizeof(struct in6_addr);
                   1021:                        written += sizeof(struct in6_addr);
                   1022:                        error = sysctl_copyout(l, &in6m->in6m_addr,
                   1023:                            oldp, sizeof(struct in6_addr));
                   1024:                        if (error)
                   1025:                                goto done;
                   1026:                        oldp = (char *)oldp + sizeof(struct in6_addr);
                   1027:                        written += sizeof(struct in6_addr);
                   1028:                        tmp = in6m->in6m_refcount;
                   1029:                        error = sysctl_copyout(l, &tmp, oldp, sizeof(tmp));
                   1030:                        if (error)
                   1031:                                goto done;
                   1032:                        oldp = (char *)oldp + sizeof(tmp);
                   1033:                        written += sizeof(tmp);
                   1034:                }
                   1035:        }
                   1036: done:
                   1037:        *oldlenp = written;
                   1038:        return error;
                   1039: }
                   1040:
                   1041: SYSCTL_SETUP(sysctl_in6_mklude_setup, "sysctl net.inet6.multicast_kludge subtree setup")
                   1042: {
                   1043:
                   1044:        sysctl_createv(clog, 0, NULL, NULL,
                   1045:                       CTLFLAG_PERMANENT,
1.59      joerg    1046:                       CTLTYPE_NODE, "inet6", NULL,
                   1047:                       NULL, 0, NULL, 0,
                   1048:                       CTL_NET, PF_INET6, CTL_EOL);
                   1049:
                   1050:        sysctl_createv(clog, 0, NULL, NULL,
                   1051:                       CTLFLAG_PERMANENT,
1.57      joerg    1052:                       CTLTYPE_NODE, "multicast",
                   1053:                       SYSCTL_DESCR("Multicast information"),
                   1054:                       in6_multicast_sysctl, 0, NULL, 0,
                   1055:                       CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
                   1056:
                   1057:        sysctl_createv(clog, 0, NULL, NULL,
                   1058:                       CTLFLAG_PERMANENT,
                   1059:                       CTLTYPE_NODE, "multicast_kludge",
                   1060:                       SYSCTL_DESCR("multicast kludge information"),
                   1061:                       in6_mkludge_sysctl, 0, NULL, 0,
                   1062:                       CTL_NET, PF_INET6, CTL_CREATE, CTL_EOL);
                   1063: }

CVSweb <webmaster@jp.NetBSD.org>