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

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

CVSweb <webmaster@jp.NetBSD.org>