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

1.33    ! christos    1: /*     $NetBSD: mld6.c,v 1.32 2006/03/06 20:33:52 rpaulo 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.33    ! christos  105: __KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.32 2006/03/06 20:33:52 rpaulo 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>
                    113: #include <sys/protosw.h>
                    114: #include <sys/syslog.h>
1.31      rpaulo    115: #include <sys/sysctl.h>
                    116: #include <sys/kernel.h>
                    117: #include <sys/callout.h>
1.2       itojun    118:
                    119: #include <net/if.h>
                    120:
                    121: #include <netinet/in.h>
                    122: #include <netinet/in_var.h>
1.31      rpaulo    123: #include <netinet6/in6_var.h>
1.10      itojun    124: #include <netinet/ip6.h>
1.2       itojun    125: #include <netinet6/ip6_var.h>
1.29      rpaulo    126: #include <netinet6/scope6_var.h>
1.10      itojun    127: #include <netinet/icmp6.h>
1.2       itojun    128: #include <netinet6/mld6_var.h>
                    129:
1.7       itojun    130: #include <net/net_osdep.h>
                    131:
1.31      rpaulo    132:
                    133: /*
                    134:  * This structure is used to keep track of in6_multi chains which belong to
                    135:  * deleted interface addresses.
                    136:  */
                    137: static LIST_HEAD(, multi6_kludge) in6_mk; /* XXX BSS initialization */
                    138:
                    139: struct multi6_kludge {
                    140:        LIST_ENTRY(multi6_kludge) mk_entry;
                    141:        struct ifnet *mk_ifp;
                    142:        struct in6_multihead mk_head;
                    143: };
                    144:
                    145:
1.2       itojun    146: /*
                    147:  * Protocol constants
                    148:  */
                    149:
                    150: /*
                    151:  * time between repetitions of a node's initial report of interest in a
                    152:  * multicast address(in seconds)
                    153:  */
1.22      itojun    154: #define MLD_UNSOLICITED_REPORT_INTERVAL        10
1.2       itojun    155:
                    156: static struct ip6_pktopts ip6_opts;
                    157:
1.31      rpaulo    158: static void mld_start_listening(struct in6_multi *);
                    159: static void mld_stop_listening(struct in6_multi *);
                    160:
                    161: static struct mld_hdr * mld_allocbuf(struct mbuf **, int, struct in6_multi *,
                    162:        int);
                    163: static void mld_sendpkt(struct in6_multi *, int, const struct in6_addr *);
                    164: static void mld_starttimer(struct in6_multi *);
                    165: static void mld_stoptimer(struct in6_multi *);
                    166: static void mld_timeo(struct in6_multi *);
                    167: static u_long mld_timerresid(struct in6_multi *);
1.2       itojun    168:
                    169: void
1.31      rpaulo    170: mld_init()
1.2       itojun    171: {
                    172:        static u_int8_t hbh_buf[8];
                    173:        struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
                    174:        u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
                    175:
                    176:        /* ip6h_nxt will be fill in later */
1.11      itojun    177:        hbh->ip6h_len = 0;      /* (8 >> 3) - 1 */
1.2       itojun    178:
                    179:        /* XXX: grotty hard coding... */
                    180:        hbh_buf[2] = IP6OPT_PADN;       /* 2 byte padding */
                    181:        hbh_buf[3] = 0;
                    182:        hbh_buf[4] = IP6OPT_RTALERT;
                    183:        hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
                    184:        bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
                    185:
                    186:        ip6_opts.ip6po_hbh = hbh;
                    187:        /* We will specify the hoplimit by a multicast option. */
                    188:        ip6_opts.ip6po_hlim = -1;
                    189: }
                    190:
1.31      rpaulo    191: static void
                    192: mld_starttimer(in6m)
                    193:        struct in6_multi *in6m;
                    194: {
                    195:        struct timeval now;
                    196:
                    197:        microtime(&now);
                    198:        in6m->in6m_timer_expire.tv_sec = now.tv_sec + in6m->in6m_timer / hz;
                    199:        in6m->in6m_timer_expire.tv_usec = now.tv_usec +
                    200:            (in6m->in6m_timer % hz) * (1000000 / hz);
                    201:        if (in6m->in6m_timer_expire.tv_usec > 1000000) {
                    202:                in6m->in6m_timer_expire.tv_sec++;
                    203:                in6m->in6m_timer_expire.tv_usec -= 1000000;
                    204:        }
                    205:
                    206:        /* start or restart the timer */
                    207:        callout_reset(in6m->in6m_timer_ch, in6m->in6m_timer,
                    208:            (void (*) __P((void *)))mld_timeo, in6m);
                    209: }
                    210:
                    211: static void
                    212: mld_stoptimer(in6m)
                    213:        struct in6_multi *in6m;
                    214: {
                    215:        if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
                    216:                return;
                    217:
                    218:        callout_stop(in6m->in6m_timer_ch);
                    219:
                    220:        in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    221: }
                    222:
                    223: static void
                    224: mld_timeo(in6m)
                    225:        struct in6_multi *in6m;
                    226: {
                    227:        int s = splsoftnet();
                    228:
                    229:        in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    230:
                    231:        callout_stop(in6m->in6m_timer_ch);
                    232:
                    233:        switch (in6m->in6m_state) {
                    234:        case MLD_REPORTPENDING:
                    235:                mld_start_listening(in6m);
                    236:                break;
                    237:        default:
                    238:                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
                    239:                break;
                    240:        }
                    241:
                    242:        splx(s);
                    243: }
                    244:
                    245: static u_long
                    246: mld_timerresid(in6m)
                    247:        struct in6_multi *in6m;
                    248: {
                    249:        struct timeval now, diff;
                    250:
                    251:        microtime(&now);
                    252:
                    253:        if (now.tv_sec > in6m->in6m_timer_expire.tv_sec ||
                    254:            (now.tv_sec == in6m->in6m_timer_expire.tv_sec &&
                    255:            now.tv_usec > in6m->in6m_timer_expire.tv_usec)) {
                    256:                return (0);
                    257:        }
                    258:        diff = in6m->in6m_timer_expire;
                    259:        diff.tv_sec -= now.tv_sec;
                    260:        diff.tv_usec -= now.tv_usec;
                    261:        if (diff.tv_usec < 0) {
                    262:                diff.tv_sec--;
                    263:                diff.tv_usec += 1000000;
                    264:        }
                    265:
                    266:        /* return the remaining time in milliseconds */
                    267:        return (((u_long)(diff.tv_sec * 1000000 + diff.tv_usec)) / 1000);
                    268: }
                    269:
                    270: static void
                    271: mld_start_listening(in6m)
1.2       itojun    272:        struct in6_multi *in6m;
                    273: {
1.29      rpaulo    274:        struct in6_addr all_in6;
                    275:
1.2       itojun    276:        /*
1.11      itojun    277:         * RFC2710 page 10:
1.2       itojun    278:         * The node never sends a Report or Done for the link-scope all-nodes
                    279:         * address.
                    280:         * MLD messages are never sent for multicast addresses whose scope is 0
                    281:         * (reserved) or 1 (node-local).
                    282:         */
1.29      rpaulo    283:        all_in6 = in6addr_linklocal_allnodes;
                    284:        if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) {
                    285:                /* XXX: this should not happen! */
                    286:                in6m->in6m_timer = 0;
                    287:                in6m->in6m_state = MLD_OTHERLISTENER;
                    288:        }
                    289:        if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
1.2       itojun    290:            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
1.31      rpaulo    291:                in6m->in6m_timer = IN6M_TIMER_UNDEF;
1.22      itojun    292:                in6m->in6m_state = MLD_OTHERLISTENER;
1.2       itojun    293:        } else {
1.31      rpaulo    294:                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
                    295:                in6m->in6m_timer = arc4random() %
                    296:                    (MLD_UNSOLICITED_REPORT_INTERVAL * hz);
1.22      itojun    297:                in6m->in6m_state = MLD_IREPORTEDLAST;
1.31      rpaulo    298:
                    299:                mld_starttimer(in6m);
1.2       itojun    300:        }
                    301: }
                    302:
1.31      rpaulo    303: static void
                    304: mld_stop_listening(in6m)
1.2       itojun    305:        struct in6_multi *in6m;
                    306: {
1.29      rpaulo    307:        struct in6_addr allnode, allrouter;
                    308:
                    309:        allnode = in6addr_linklocal_allnodes;
                    310:        if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) {
                    311:                /* XXX: this should not happen! */
                    312:                return;
                    313:        }
                    314:        allrouter = in6addr_linklocal_allrouters;
                    315:        if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) {
                    316:                /* XXX impossible */
                    317:                return;
                    318:        }
1.2       itojun    319:
1.22      itojun    320:        if (in6m->in6m_state == MLD_IREPORTEDLAST &&
1.29      rpaulo    321:            (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode)) &&
                    322:            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
                    323:            IPV6_ADDR_SCOPE_INTFACELOCAL) {
1.31      rpaulo    324:                mld_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
1.29      rpaulo    325:        }
1.2       itojun    326: }
                    327:
                    328: void
1.31      rpaulo    329: mld_input(m, off)
1.2       itojun    330:        struct mbuf *m;
                    331:        int off;
                    332: {
                    333:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1.22      itojun    334:        struct mld_hdr *mldh;
1.2       itojun    335:        struct ifnet *ifp = m->m_pkthdr.rcvif;
1.31      rpaulo    336:        struct in6_multi *in6m = NULL;
1.29      rpaulo    337:        struct in6_addr mld_addr, all_in6;
1.2       itojun    338:        struct in6_ifaddr *ia;
1.31      rpaulo    339:        int timer = 0;          /* timer value in the MLD query header */
1.2       itojun    340:
1.22      itojun    341:        IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
1.13      itojun    342:        if (mldh == NULL) {
                    343:                icmp6stat.icp6s_tooshort++;
                    344:                return;
                    345:        }
                    346:
1.2       itojun    347:        /* source address validation */
1.13      itojun    348:        ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
1.2       itojun    349:        if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
1.31      rpaulo    350:                /*
                    351:                 * RFC3590 allows the IPv6 unspecified address as the source
                    352:                 * address of MLD report and done messages.  However, as this
                    353:                 * same document says, this special rule is for snooping
                    354:                 * switches and the RFC requires routers to discard MLD packets
                    355:                 * with the unspecified source address.  The RFC only talks
                    356:                 * about hosts receiving an MLD query or report in Security
                    357:                 * Considerations, but this is probably the correct intention.
                    358:                 * RFC3590 does not talk about other cases than link-local and
                    359:                 * the unspecified source addresses, but we believe the same
                    360:                 * rule should be applied.
                    361:                 * As a result, we only allow link-local addresses as the
                    362:                 * source address; otherwise, simply discard the packet.
                    363:                 */
1.18      itojun    364: #if 0
1.31      rpaulo    365:                /*
                    366:                 * XXX: do not log in an input path to avoid log flooding,
                    367:                 * though RFC3590 says "SHOULD log" if the source of a query
                    368:                 * is the unspecified address.
                    369:                 */
                    370:                log(LOG_INFO,
1.22      itojun    371:                    "mld_input: src %s is not link-local (grp=%s)\n",
1.31      rpaulo    372:                    ip6_sprintf(&ip6->ip6_src), ip6_sprintf(&mldh->mld_addr));
1.18      itojun    373: #endif
1.11      itojun    374:                m_freem(m);
1.2       itojun    375:                return;
                    376:        }
                    377:
                    378:        /*
1.29      rpaulo    379:         * make a copy for local work (in6_setscope() may modify the 1st arg)
                    380:         */
                    381:        mld_addr = mldh->mld_addr;
                    382:        if (in6_setscope(&mld_addr, ifp, NULL)) {
                    383:                /* XXX: this should not happen! */
                    384:                m_free(m);
                    385:                return;
                    386:        }
                    387:
                    388:        /*
1.31      rpaulo    389:         * In the MLD specification, there are 3 states and a flag.
1.2       itojun    390:         *
                    391:         * In Non-Listener state, we simply don't have a membership record.
                    392:         * In Delaying Listener state, our timer is running (in6m->in6m_timer)
1.31      rpaulo    393:         * In Idle Listener state, our timer is not running
                    394:         * (in6m->in6m_timer==IN6M_TIMER_UNDEF)
1.2       itojun    395:         *
1.22      itojun    396:         * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
                    397:         * we have heard a report from another member, or MLD_IREPORTEDLAST
1.2       itojun    398:         * if we sent the last report.
                    399:         */
1.22      itojun    400:        switch (mldh->mld_type) {
                    401:        case MLD_LISTENER_QUERY:
1.7       itojun    402:                if (ifp->if_flags & IFF_LOOPBACK)
                    403:                        break;
                    404:
1.29      rpaulo    405:                if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
                    406:                    !IN6_IS_ADDR_MULTICAST(&mld_addr))
1.7       itojun    407:                        break;  /* print error or log stat? */
1.29      rpaulo    408:
                    409:                all_in6 = in6addr_linklocal_allnodes;
                    410:                if (in6_setscope(&all_in6, ifp, NULL)) {
                    411:                        /* XXX: this should not happen! */
                    412:                        break;
                    413:                }
1.2       itojun    414:
1.7       itojun    415:                /*
1.11      itojun    416:                 * - Start the timers in all of our membership records
                    417:                 *   that the query applies to for the interface on
                    418:                 *   which the query arrived excl. those that belong
                    419:                 *   to the "all-nodes" group (ff02::1).
                    420:                 * - Restart any timer that is already running but has
1.30      rpaulo    421:                 *   a value longer than the requested timeout.
1.11      itojun    422:                 * - Use the value specified in the query message as
                    423:                 *   the maximum timeout.
                    424:                 */
1.31      rpaulo    425:                timer = ntohs(mldh->mld_maxdelay);
                    426:
1.7       itojun    427:                IFP_TO_IA6(ifp, ia);
                    428:                if (ia == NULL)
                    429:                        break;
1.2       itojun    430:
1.7       itojun    431:                for (in6m = ia->ia6_multiaddrs.lh_first;
                    432:                     in6m;
                    433:                     in6m = in6m->in6m_entry.le_next)
                    434:                {
1.29      rpaulo    435:                        if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
1.7       itojun    436:                            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
                    437:                            IPV6_ADDR_SCOPE_LINKLOCAL)
                    438:                                continue;
1.2       itojun    439:
1.31      rpaulo    440:                        if (in6m->in6m_state == MLD_REPORTPENDING)
                    441:                                continue; /* we are not yet ready */
                    442:
                    443:                        if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
                    444:                            !IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr))
                    445:                                continue;
                    446:
                    447:                        if (timer == 0) {
                    448:                                /* send a report immediately */
                    449:                                mld_stoptimer(in6m);
                    450:                                mld_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
                    451:                                in6m->in6m_state = MLD_IREPORTEDLAST;
                    452:                        } else if (in6m->in6m_timer == IN6M_TIMER_UNDEF ||
                    453:                            mld_timerresid(in6m) > (u_long)timer) {
                    454:                                in6m->in6m_timer = arc4random() %
                    455:                                    (int)(((long)timer * hz) / 1000);
                    456:                                mld_starttimer(in6m);
1.7       itojun    457:                        }
                    458:                }
1.29      rpaulo    459:                break;
1.2       itojun    460:
1.22      itojun    461:        case MLD_LISTENER_REPORT:
1.2       itojun    462:                /*
1.11      itojun    463:                 * For fast leave to work, we have to know that we are the
                    464:                 * last person to send a report for this group.  Reports
                    465:                 * can potentially get looped back if we are a multicast
                    466:                 * router, so discard reports sourced by me.
                    467:                 * Note that it is impossible to check IFF_LOOPBACK flag of
                    468:                 * ifp for this purpose, since ip6_mloopback pass the physical
                    469:                 * interface to looutput.
                    470:                 */
1.7       itojun    471:                if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
                    472:                        break;
                    473:
1.22      itojun    474:                if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
1.7       itojun    475:                        break;
                    476:
                    477:                /*
1.11      itojun    478:                 * If we belong to the group being reported, stop
                    479:                 * our timer for that group.
                    480:                 */
1.29      rpaulo    481:                IN6_LOOKUP_MULTI(mld_addr, ifp, in6m);
1.7       itojun    482:                if (in6m) {
1.31      rpaulo    483:                        mld_stoptimer(in6m); /* transit to idle state */
1.22      itojun    484:                        in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
1.7       itojun    485:                }
                    486:                break;
                    487:        default:                /* this is impossible */
1.18      itojun    488: #if 0
1.19      itojun    489:                /*
                    490:                 * this case should be impossible because of filtering in
                    491:                 * icmp6_input().  But we explicitly disabled this part
                    492:                 * just in case.
                    493:                 */
1.31      rpaulo    494:                log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type);
1.18      itojun    495: #endif
1.7       itojun    496:                break;
1.2       itojun    497:        }
1.11      itojun    498:
                    499:        m_freem(m);
1.2       itojun    500: }
                    501:
                    502: static void
1.31      rpaulo    503: mld_sendpkt(in6m, type, dst)
1.2       itojun    504:        struct in6_multi *in6m;
                    505:        int type;
                    506:        const struct in6_addr *dst;
                    507: {
1.31      rpaulo    508:        struct mbuf *mh;
1.22      itojun    509:        struct mld_hdr *mldh;
1.31      rpaulo    510:        struct ip6_hdr *ip6 = NULL;
1.2       itojun    511:        struct ip6_moptions im6o;
1.31      rpaulo    512:        struct in6_ifaddr *ia = NULL;
1.2       itojun    513:        struct ifnet *ifp = in6m->in6m_ifp;
1.19      itojun    514:        int ignflags;
1.2       itojun    515:
                    516:        /*
                    517:         * At first, find a link local address on the outgoing interface
                    518:         * to use as the source address of the MLD packet.
1.19      itojun    519:         * We do not reject tentative addresses for MLD report to deal with
                    520:         * the case where we first join a link-local address.
1.2       itojun    521:         */
1.19      itojun    522:        ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
                    523:        if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
1.2       itojun    524:                return;
1.19      itojun    525:        if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
                    526:                ia = NULL;
1.2       itojun    527:
1.31      rpaulo    528:        /* Allocate two mbufs to store IPv6 header and MLD header */
                    529:        mldh = mld_allocbuf(&mh, sizeof(struct mld_hdr), in6m, type);
                    530:        if (mldh == NULL)
1.2       itojun    531:                return;
                    532:
1.31      rpaulo    533:        /* fill src/dst here */
                    534:        ip6 = mtod(mh, struct ip6_hdr *);
                    535:        ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
                    536:        ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
1.2       itojun    537:
1.22      itojun    538:        mldh->mld_addr = in6m->in6m_addr;
1.29      rpaulo    539:        in6_clearscope(&mldh->mld_addr); /* XXX */
1.22      itojun    540:        mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
                    541:            sizeof(struct mld_hdr));
1.2       itojun    542:
                    543:        /* construct multicast option */
1.31      rpaulo    544:        memset(&im6o, 0, sizeof(im6o));
1.2       itojun    545:        im6o.im6o_multicast_ifp = ifp;
                    546:        im6o.im6o_multicast_hlim = 1;
                    547:
                    548:        /*
                    549:         * Request loopback of the report if we are acting as a multicast
                    550:         * router, so that the process-level routing daemon can hear it.
                    551:         */
                    552:        im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
                    553:
                    554:        /* increment output statictics */
                    555:        icmp6stat.icp6s_outhist[type]++;
1.15      itojun    556:        icmp6_ifstat_inc(ifp, ifs6_out_msg);
1.17      itojun    557:        switch (type) {
1.22      itojun    558:        case MLD_LISTENER_QUERY:
1.15      itojun    559:                icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
                    560:                break;
1.22      itojun    561:        case MLD_LISTENER_REPORT:
1.15      itojun    562:                icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
                    563:                break;
1.22      itojun    564:        case MLD_LISTENER_DONE:
1.15      itojun    565:                icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
                    566:                break;
1.7       itojun    567:        }
1.19      itojun    568:
1.27      perry     569:        ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
1.25      itojun    570:            &im6o, (struct socket *)NULL, NULL);
1.2       itojun    571: }
1.31      rpaulo    572:
                    573: static struct mld_hdr *
1.33    ! christos  574: mld_allocbuf(struct mbuf **mh, int len, struct in6_multi *in6m __unused,
        !           575:     int type)
1.31      rpaulo    576: {
                    577:        struct mbuf *md;
                    578:        struct mld_hdr *mldh;
                    579:        struct ip6_hdr *ip6;
                    580:
                    581:        /*
                    582:         * Allocate mbufs to store ip6 header and MLD header.
                    583:         * We allocate 2 mbufs and make chain in advance because
                    584:         * it is more convenient when inserting the hop-by-hop option later.
                    585:         */
                    586:        MGETHDR(*mh, M_DONTWAIT, MT_HEADER);
                    587:        if (*mh == NULL)
                    588:                return NULL;
                    589:        MGET(md, M_DONTWAIT, MT_DATA);
                    590:        if (md == NULL) {
                    591:                m_free(*mh);
                    592:                *mh = NULL;
                    593:                return NULL;
                    594:        }
                    595:        (*mh)->m_next = md;
                    596:        md->m_next = NULL;
                    597:
                    598:        (*mh)->m_pkthdr.rcvif = NULL;
                    599:        (*mh)->m_pkthdr.len = sizeof(struct ip6_hdr) + len;
                    600:        (*mh)->m_len = sizeof(struct ip6_hdr);
                    601:        MH_ALIGN(*mh, sizeof(struct ip6_hdr));
                    602:
                    603:        /* fill in the ip6 header */
                    604:        ip6 = mtod(*mh, struct ip6_hdr *);
                    605:        memset(ip6, 0, sizeof(*ip6));
                    606:        ip6->ip6_flow = 0;
                    607:        ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
                    608:        ip6->ip6_vfc |= IPV6_VERSION;
                    609:        /* ip6_plen will be set later */
                    610:        ip6->ip6_nxt = IPPROTO_ICMPV6;
                    611:        /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
                    612:        /* ip6_src/dst will be set by mld_sendpkt() or mld_sendbuf() */
                    613:
                    614:        /* fill in the MLD header as much as possible */
                    615:        md->m_len = len;
                    616:        mldh = mtod(md, struct mld_hdr *);
                    617:        memset(mldh, 0, len);
                    618:        mldh->mld_type = type;
                    619:        return mldh;
                    620: }
                    621:
                    622: /*
                    623:  * Add an address to the list of IP6 multicast addresses for a given interface.
                    624:  */
                    625: struct in6_multi *
1.32      rpaulo    626: in6_addmulti(maddr6, ifp, errorp, timer)
1.31      rpaulo    627:        struct in6_addr *maddr6;
                    628:        struct ifnet *ifp;
1.32      rpaulo    629:        int *errorp, timer;
1.31      rpaulo    630: {
                    631:        struct  in6_ifaddr *ia;
                    632:        struct  in6_ifreq ifr;
                    633:        struct  in6_multi *in6m;
                    634:        int     s = splsoftnet();
                    635:
                    636:        *errorp = 0;
                    637:
                    638:        /*
                    639:         * See if address already in list.
                    640:         */
                    641:        IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
                    642:        if (in6m != NULL) {
                    643:                /*
                    644:                 * Found it; just increment the refrence count.
                    645:                 */
                    646:                in6m->in6m_refcount++;
                    647:        } else {
                    648:                /*
                    649:                 * New address; allocate a new multicast record
                    650:                 * and link it into the interface's multicast list.
                    651:                 */
                    652:                in6m = (struct in6_multi *)
                    653:                        malloc(sizeof(*in6m), M_IPMADDR, M_NOWAIT);
                    654:                if (in6m == NULL) {
                    655:                        splx(s);
                    656:                        *errorp = ENOBUFS;
                    657:                        return (NULL);
                    658:                }
                    659:
                    660:                memset(in6m, 0, sizeof(*in6m));
                    661:                in6m->in6m_addr = *maddr6;
                    662:                in6m->in6m_ifp = ifp;
                    663:                in6m->in6m_refcount = 1;
                    664:                in6m->in6m_timer = IN6M_TIMER_UNDEF;
                    665:                in6m->in6m_timer_ch =
                    666:                    malloc(sizeof(*in6m->in6m_timer_ch), M_IPMADDR, M_NOWAIT);
                    667:                if (in6m->in6m_timer_ch == NULL) {
                    668:                        free(in6m, M_IPMADDR);
                    669:                        splx(s);
                    670:                        return (NULL);
                    671:                }
                    672:                IFP_TO_IA6(ifp, ia);
                    673:                if (ia == NULL) {
                    674:                        free(in6m, M_IPMADDR);
                    675:                        splx(s);
                    676:                        *errorp = EADDRNOTAVAIL; /* appropriate? */
                    677:                        return (NULL);
                    678:                }
                    679:                in6m->in6m_ia = ia;
                    680:                IFAREF(&ia->ia_ifa); /* gain a reference */
                    681:                LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
                    682:
                    683:                /*
                    684:                 * Ask the network driver to update its multicast reception
                    685:                 * filter appropriately for the new address.
                    686:                 */
                    687:                memset(&ifr.ifr_addr, 0, sizeof(struct sockaddr_in6));
                    688:                ifr.ifr_addr.sin6_family = AF_INET6;
                    689:                ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
                    690:                ifr.ifr_addr.sin6_addr = *maddr6;
                    691:                if (ifp->if_ioctl == NULL)
                    692:                        *errorp = ENXIO; /* XXX: appropriate? */
                    693:                else
                    694:                        *errorp = (*ifp->if_ioctl)(ifp, SIOCADDMULTI,
                    695:                            (caddr_t)&ifr);
                    696:                if (*errorp) {
                    697:                        LIST_REMOVE(in6m, in6m_entry);
                    698:                        free(in6m, M_IPMADDR);
                    699:                        IFAFREE(&ia->ia_ifa);
                    700:                        splx(s);
                    701:                        return (NULL);
                    702:                }
                    703:
                    704:                callout_init(in6m->in6m_timer_ch);
1.32      rpaulo    705:                in6m->in6m_timer = timer;
1.31      rpaulo    706:                if (in6m->in6m_timer > 0) {
                    707:                        in6m->in6m_state = MLD_REPORTPENDING;
                    708:                        mld_starttimer(in6m);
                    709:
                    710:                        splx(s);
                    711:                        return (in6m);
                    712:                }
                    713:
                    714:                /*
                    715:                 * Let MLD6 know that we have joined a new IP6 multicast
                    716:                 * group.
                    717:                 */
                    718:                mld_start_listening(in6m);
                    719:        }
                    720:        splx(s);
                    721:        return (in6m);
                    722: }
                    723:
                    724: /*
                    725:  * Delete a multicast address record.
                    726:  */
                    727: void
                    728: in6_delmulti(in6m)
                    729:        struct in6_multi *in6m;
                    730: {
                    731:        struct  in6_ifreq ifr;
                    732:        struct  in6_ifaddr *ia;
                    733:        int     s = splsoftnet();
                    734:
                    735:        mld_stoptimer(in6m);
                    736:
                    737:        if (--in6m->in6m_refcount == 0) {
                    738:                /*
                    739:                 * No remaining claims to this record; let MLD6 know
                    740:                 * that we are leaving the multicast group.
                    741:                 */
                    742:                mld_stop_listening(in6m);
                    743:
                    744:                /*
                    745:                 * Unlink from list.
                    746:                 */
                    747:                LIST_REMOVE(in6m, in6m_entry);
                    748:                if (in6m->in6m_ia) {
                    749:                        IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
                    750:                }
                    751:
                    752:                /*
                    753:                 * Delete all references of this multicasting group from
                    754:                 * the membership arrays
                    755:                 */
                    756:                for (ia = in6_ifaddr; ia; ia = ia->ia_next) {
                    757:                        struct in6_multi_mship *imm;
                    758:                        LIST_FOREACH(imm, &ia->ia6_memberships,
                    759:                            i6mm_chain) {
                    760:                                if (imm->i6mm_maddr == in6m)
                    761:                                        imm->i6mm_maddr = NULL;
                    762:                        }
                    763:                }
                    764:
                    765:                /*
                    766:                 * Notify the network driver to update its multicast
                    767:                 * reception filter.
                    768:                 */
                    769:                memset(&ifr.ifr_addr, 0, sizeof(struct sockaddr_in6));
                    770:                ifr.ifr_addr.sin6_family = AF_INET6;
                    771:                ifr.ifr_addr.sin6_len = sizeof(struct sockaddr_in6);
                    772:                ifr.ifr_addr.sin6_addr = in6m->in6m_addr;
                    773:                (*in6m->in6m_ifp->if_ioctl)(in6m->in6m_ifp,
                    774:                    SIOCDELMULTI, (caddr_t)&ifr);
                    775:                free(in6m->in6m_timer_ch, M_IPMADDR);
                    776:                free(in6m, M_IPMADDR);
                    777:        }
                    778:        splx(s);
                    779: }
                    780:
                    781:
                    782: struct in6_multi_mship *
1.32      rpaulo    783: in6_joingroup(ifp, addr, errorp, timer)
1.31      rpaulo    784:        struct ifnet *ifp;
                    785:        struct in6_addr *addr;
1.32      rpaulo    786:        int *errorp, timer;
1.31      rpaulo    787: {
                    788:        struct in6_multi_mship *imm;
                    789:
                    790:        imm = malloc(sizeof(*imm), M_IPMADDR, M_NOWAIT);
                    791:        if (!imm) {
                    792:                *errorp = ENOBUFS;
                    793:                return NULL;
                    794:        }
                    795:
                    796:        memset(imm, 0, sizeof(*imm));
1.32      rpaulo    797:        imm->i6mm_maddr = in6_addmulti(addr, ifp, errorp, timer);
1.31      rpaulo    798:        if (!imm->i6mm_maddr) {
                    799:                /* *errorp is alrady set */
                    800:                free(imm, M_IPMADDR);
                    801:                return NULL;
                    802:        }
                    803:        return imm;
                    804: }
                    805:
                    806: int
                    807: in6_leavegroup(imm)
                    808:        struct in6_multi_mship *imm;
                    809: {
                    810:
                    811:        if (imm->i6mm_maddr) {
                    812:                in6_delmulti(imm->i6mm_maddr);
                    813:        }
                    814:        free(imm, M_IPMADDR);
                    815:        return 0;
                    816: }
                    817:
                    818:
                    819: /*
                    820:  * Multicast address kludge:
                    821:  * If there were any multicast addresses attached to this interface address,
                    822:  * either move them to another address on this interface, or save them until
                    823:  * such time as this interface is reconfigured for IPv6.
                    824:  */
                    825: void
                    826: in6_savemkludge(oia)
                    827:        struct in6_ifaddr *oia;
                    828: {
                    829:        struct in6_ifaddr *ia;
                    830:        struct in6_multi *in6m, *next;
                    831:
                    832:        IFP_TO_IA6(oia->ia_ifp, ia);
                    833:        if (ia) {       /* there is another address */
                    834:                for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next) {
                    835:                        next = in6m->in6m_entry.le_next;
                    836:                        IFAFREE(&in6m->in6m_ia->ia_ifa);
                    837:                        IFAREF(&ia->ia_ifa);
                    838:                        in6m->in6m_ia = ia;
                    839:                        LIST_INSERT_HEAD(&ia->ia6_multiaddrs, in6m, in6m_entry);
                    840:                }
                    841:        } else {        /* last address on this if deleted, save */
                    842:                struct multi6_kludge *mk;
                    843:
                    844:                for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
                    845:                        if (mk->mk_ifp == oia->ia_ifp)
                    846:                                break;
                    847:                }
                    848:                if (mk == NULL) /* this should not happen! */
                    849:                        panic("in6_savemkludge: no kludge space");
                    850:
                    851:                for (in6m = oia->ia6_multiaddrs.lh_first; in6m; in6m = next) {
                    852:                        next = in6m->in6m_entry.le_next;
                    853:                        IFAFREE(&in6m->in6m_ia->ia_ifa); /* release reference */
                    854:                        in6m->in6m_ia = NULL;
                    855:                        LIST_INSERT_HEAD(&mk->mk_head, in6m, in6m_entry);
                    856:                }
                    857:        }
                    858: }
                    859:
                    860: /*
                    861:  * Continuation of multicast address hack:
                    862:  * If there was a multicast group list previously saved for this interface,
                    863:  * then we re-attach it to the first address configured on the i/f.
                    864:  */
                    865: void
                    866: in6_restoremkludge(ia, ifp)
                    867:        struct in6_ifaddr *ia;
                    868:        struct ifnet *ifp;
                    869: {
                    870:        struct multi6_kludge *mk;
                    871:
                    872:        for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
                    873:                if (mk->mk_ifp == ifp) {
                    874:                        struct in6_multi *in6m, *next;
                    875:
                    876:                        for (in6m = mk->mk_head.lh_first; in6m; in6m = next) {
                    877:                                next = in6m->in6m_entry.le_next;
                    878:                                in6m->in6m_ia = ia;
                    879:                                IFAREF(&ia->ia_ifa);
                    880:                                LIST_INSERT_HEAD(&ia->ia6_multiaddrs,
                    881:                                                 in6m, in6m_entry);
                    882:                        }
                    883:                        LIST_INIT(&mk->mk_head);
                    884:                        break;
                    885:                }
                    886:        }
                    887: }
                    888:
                    889: /*
                    890:  * Allocate space for the kludge at interface initialization time.
                    891:  * Formerly, we dynamically allocated the space in in6_savemkludge() with
                    892:  * malloc(M_WAITOK).  However, it was wrong since the function could be called
                    893:  * under an interrupt context (software timer on address lifetime expiration).
                    894:  * Also, we cannot just give up allocating the strucutre, since the group
                    895:  * membership structure is very complex and we need to keep it anyway.
                    896:  * Of course, this function MUST NOT be called under an interrupt context.
                    897:  * Specifically, it is expected to be called only from in6_ifattach(), though
                    898:  * it is a global function.
                    899:  */
                    900: void
                    901: in6_createmkludge(ifp)
                    902:        struct ifnet *ifp;
                    903: {
                    904:        struct multi6_kludge *mk;
                    905:
                    906:        for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
                    907:                /* If we've already had one, do not allocate. */
                    908:                if (mk->mk_ifp == ifp)
                    909:                        return;
                    910:        }
                    911:
                    912:        mk = malloc(sizeof(*mk), M_IPMADDR, M_WAITOK);
                    913:
                    914:        memset(mk, 0, sizeof(*mk));
                    915:        LIST_INIT(&mk->mk_head);
                    916:        mk->mk_ifp = ifp;
                    917:        LIST_INSERT_HEAD(&in6_mk, mk, mk_entry);
                    918: }
                    919:
                    920: void
                    921: in6_purgemkludge(ifp)
                    922:        struct ifnet *ifp;
                    923: {
                    924:        struct multi6_kludge *mk;
                    925:        struct in6_multi *in6m;
                    926:
                    927:        for (mk = in6_mk.lh_first; mk; mk = mk->mk_entry.le_next) {
                    928:                if (mk->mk_ifp != ifp)
                    929:                        continue;
                    930:
                    931:                /* leave from all multicast groups joined */
                    932:                while ((in6m = LIST_FIRST(&mk->mk_head)) != NULL) {
                    933:                        in6_delmulti(in6m);
                    934:                }
                    935:                LIST_REMOVE(mk, mk_entry);
                    936:                free(mk, M_IPMADDR);
                    937:                break;
                    938:        }
                    939: }

CVSweb <webmaster@jp.NetBSD.org>