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

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

CVSweb <webmaster@jp.NetBSD.org>