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

1.24    ! jonathan    1: /*     $NetBSD: mld6.c,v 1.23 2003/08/07 16:33:29 agc 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.24    ! jonathan  105: __KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.23 2003/08/07 16:33:29 agc Exp $");
1.2       itojun    106:
                    107: #include "opt_inet.h"
                    108:
                    109: #include <sys/param.h>
                    110: #include <sys/systm.h>
                    111: #include <sys/mbuf.h>
                    112: #include <sys/socket.h>
                    113: #include <sys/protosw.h>
                    114: #include <sys/syslog.h>
                    115:
                    116: #include <net/if.h>
                    117:
                    118: #include <netinet/in.h>
                    119: #include <netinet/in_var.h>
1.10      itojun    120: #include <netinet/ip6.h>
1.2       itojun    121: #include <netinet6/ip6_var.h>
1.10      itojun    122: #include <netinet/icmp6.h>
1.2       itojun    123: #include <netinet6/mld6_var.h>
                    124:
1.7       itojun    125: #include <net/net_osdep.h>
                    126:
1.2       itojun    127: /*
                    128:  * Protocol constants
                    129:  */
                    130:
                    131: /* denotes that the MLD max response delay field specifies time in milliseconds */
1.22      itojun    132: #define MLD_TIMER_SCALE        1000
1.2       itojun    133: /*
                    134:  * time between repetitions of a node's initial report of interest in a
                    135:  * multicast address(in seconds)
                    136:  */
1.22      itojun    137: #define MLD_UNSOLICITED_REPORT_INTERVAL        10
1.2       itojun    138:
                    139: static struct ip6_pktopts ip6_opts;
1.22      itojun    140: static int mld_timers_are_running;
1.2       itojun    141: /* XXX: These are necessary for KAME's link-local hack */
1.22      itojun    142: static struct in6_addr mld_all_nodes_linklocal = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
                    143: static struct in6_addr mld_all_routers_linklocal = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
1.2       itojun    144:
                    145: static void mld6_sendpkt __P((struct in6_multi *, int, const struct in6_addr *));
                    146:
                    147: void
                    148: mld6_init()
                    149: {
                    150:        static u_int8_t hbh_buf[8];
                    151:        struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
                    152:        u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
                    153:
1.22      itojun    154:        mld_timers_are_running = 0;
1.2       itojun    155:
                    156:        /* ip6h_nxt will be fill in later */
1.11      itojun    157:        hbh->ip6h_len = 0;      /* (8 >> 3) - 1 */
1.2       itojun    158:
                    159:        /* XXX: grotty hard coding... */
                    160:        hbh_buf[2] = IP6OPT_PADN;       /* 2 byte padding */
                    161:        hbh_buf[3] = 0;
                    162:        hbh_buf[4] = IP6OPT_RTALERT;
                    163:        hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
                    164:        bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
                    165:
                    166:        ip6_opts.ip6po_hbh = hbh;
                    167:        /* We will specify the hoplimit by a multicast option. */
                    168:        ip6_opts.ip6po_hlim = -1;
                    169: }
                    170:
                    171: void
                    172: mld6_start_listening(in6m)
                    173:        struct in6_multi *in6m;
                    174: {
1.4       itojun    175:        int s = splsoftnet();
1.2       itojun    176:
                    177:        /*
1.11      itojun    178:         * RFC2710 page 10:
1.2       itojun    179:         * The node never sends a Report or Done for the link-scope all-nodes
                    180:         * address.
                    181:         * MLD messages are never sent for multicast addresses whose scope is 0
                    182:         * (reserved) or 1 (node-local).
                    183:         */
1.22      itojun    184:        mld_all_nodes_linklocal.s6_addr16[1] =
1.19      itojun    185:            htons(in6m->in6m_ifp->if_index); /* XXX */
1.22      itojun    186:        if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld_all_nodes_linklocal) ||
1.2       itojun    187:            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
                    188:                in6m->in6m_timer = 0;
1.22      itojun    189:                in6m->in6m_state = MLD_OTHERLISTENER;
1.2       itojun    190:        } else {
1.22      itojun    191:                mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
1.19      itojun    192:                in6m->in6m_timer =
1.22      itojun    193:                    MLD_RANDOM_DELAY(MLD_UNSOLICITED_REPORT_INTERVAL *
1.19      itojun    194:                    PR_FASTHZ);
1.22      itojun    195:                in6m->in6m_state = MLD_IREPORTEDLAST;
                    196:                mld_timers_are_running = 1;
1.2       itojun    197:        }
                    198:        splx(s);
                    199: }
                    200:
                    201: void
                    202: mld6_stop_listening(in6m)
                    203:        struct in6_multi *in6m;
                    204: {
1.22      itojun    205:        mld_all_nodes_linklocal.s6_addr16[1] =
1.19      itojun    206:            htons(in6m->in6m_ifp->if_index); /* XXX */
1.22      itojun    207:        mld_all_routers_linklocal.s6_addr16[1] =
1.19      itojun    208:            htons(in6m->in6m_ifp->if_index); /* XXX: necessary when mrouting */
1.2       itojun    209:
1.22      itojun    210:        if (in6m->in6m_state == MLD_IREPORTEDLAST &&
                    211:            (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld_all_nodes_linklocal)) &&
1.2       itojun    212:            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > IPV6_ADDR_SCOPE_NODELOCAL)
1.22      itojun    213:                mld6_sendpkt(in6m, MLD_LISTENER_DONE,
                    214:                    &mld_all_routers_linklocal);
1.2       itojun    215: }
                    216:
                    217: void
                    218: mld6_input(m, off)
                    219:        struct mbuf *m;
                    220:        int off;
                    221: {
                    222:        struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
1.22      itojun    223:        struct mld_hdr *mldh;
1.2       itojun    224:        struct ifnet *ifp = m->m_pkthdr.rcvif;
                    225:        struct in6_multi *in6m;
                    226:        struct in6_ifaddr *ia;
                    227:        int timer;              /* timer value in the MLD query header */
                    228:
1.22      itojun    229:        IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
1.13      itojun    230:        if (mldh == NULL) {
                    231:                icmp6stat.icp6s_tooshort++;
                    232:                return;
                    233:        }
                    234:
1.2       itojun    235:        /* source address validation */
1.13      itojun    236:        ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
1.2       itojun    237:        if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
1.18      itojun    238: #if 0
1.2       itojun    239:                log(LOG_ERR,
1.22      itojun    240:                    "mld_input: src %s is not link-local (grp=%s)\n",
1.13      itojun    241:                    ip6_sprintf(&ip6->ip6_src),
1.22      itojun    242:                    ip6_sprintf(&mldh->mld_addr));
1.18      itojun    243: #endif
1.2       itojun    244:                /*
1.11      itojun    245:                 * spec (RFC2710) does not explicitly
1.2       itojun    246:                 * specify to discard the packet from a non link-local
                    247:                 * source address. But we believe it's expected to do so.
1.13      itojun    248:                 * XXX: do we have to allow :: as source?
1.2       itojun    249:                 */
1.11      itojun    250:                m_freem(m);
1.2       itojun    251:                return;
                    252:        }
                    253:
                    254:        /*
                    255:         * In the MLD6 specification, there are 3 states and a flag.
                    256:         *
                    257:         * In Non-Listener state, we simply don't have a membership record.
                    258:         * In Delaying Listener state, our timer is running (in6m->in6m_timer)
                    259:         * In Idle Listener state, our timer is not running (in6m->in6m_timer==0)
                    260:         *
1.22      itojun    261:         * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
                    262:         * we have heard a report from another member, or MLD_IREPORTEDLAST
1.2       itojun    263:         * if we sent the last report.
                    264:         */
1.22      itojun    265:        switch (mldh->mld_type) {
                    266:        case MLD_LISTENER_QUERY:
1.7       itojun    267:                if (ifp->if_flags & IFF_LOOPBACK)
                    268:                        break;
                    269:
1.22      itojun    270:                if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) &&
                    271:                    !IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
1.7       itojun    272:                        break;  /* print error or log stat? */
1.22      itojun    273:                if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
                    274:                        mldh->mld_addr.s6_addr16[1] =
1.19      itojun    275:                            htons(ifp->if_index); /* XXX */
1.2       itojun    276:
1.7       itojun    277:                /*
1.11      itojun    278:                 * - Start the timers in all of our membership records
                    279:                 *   that the query applies to for the interface on
                    280:                 *   which the query arrived excl. those that belong
                    281:                 *   to the "all-nodes" group (ff02::1).
                    282:                 * - Restart any timer that is already running but has
                    283:                 *   A value longer than the requested timeout.
                    284:                 * - Use the value specified in the query message as
                    285:                 *   the maximum timeout.
                    286:                 */
1.7       itojun    287:                IFP_TO_IA6(ifp, ia);
                    288:                if (ia == NULL)
                    289:                        break;
1.2       itojun    290:
                    291:                /*
1.11      itojun    292:                 * XXX: System timer resolution is too low to handle Max
                    293:                 * Response Delay, so set 1 to the internal timer even if
                    294:                 * the calculated value equals to zero when Max Response
                    295:                 * Delay is positive.
                    296:                 */
1.22      itojun    297:                timer = ntohs(mldh->mld_maxdelay)*PR_FASTHZ/MLD_TIMER_SCALE;
                    298:                if (timer == 0 && mldh->mld_maxdelay)
1.7       itojun    299:                        timer = 1;
1.22      itojun    300:                mld_all_nodes_linklocal.s6_addr16[1] =
1.7       itojun    301:                        htons(ifp->if_index); /* XXX */
1.20      itojun    302:
1.7       itojun    303:                for (in6m = ia->ia6_multiaddrs.lh_first;
                    304:                     in6m;
                    305:                     in6m = in6m->in6m_entry.le_next)
                    306:                {
                    307:                        if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr,
1.22      itojun    308:                                                &mld_all_nodes_linklocal) ||
1.7       itojun    309:                            IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
                    310:                            IPV6_ADDR_SCOPE_LINKLOCAL)
                    311:                                continue;
1.2       itojun    312:
1.22      itojun    313:                        if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) ||
                    314:                            IN6_ARE_ADDR_EQUAL(&mldh->mld_addr,
1.7       itojun    315:                                                &in6m->in6m_addr))
                    316:                        {
                    317:                                if (timer == 0) {
                    318:                                        /* send a report immediately */
1.22      itojun    319:                                        mld6_sendpkt(in6m, MLD_LISTENER_REPORT,
1.19      itojun    320:                                            NULL);
1.7       itojun    321:                                        in6m->in6m_timer = 0; /* reset timer */
1.22      itojun    322:                                        in6m->in6m_state = MLD_IREPORTEDLAST;
1.7       itojun    323:                                }
                    324:                                else if (in6m->in6m_timer == 0 || /*idle state*/
1.19      itojun    325:                                    in6m->in6m_timer > timer) {
1.7       itojun    326:                                        in6m->in6m_timer =
1.22      itojun    327:                                            MLD_RANDOM_DELAY(timer);
                    328:                                        mld_timers_are_running = 1;
1.7       itojun    329:                                }
                    330:                        }
                    331:                }
1.2       itojun    332:
1.22      itojun    333:                if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
                    334:                        mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
1.7       itojun    335:                break;
1.22      itojun    336:        case MLD_LISTENER_REPORT:
1.2       itojun    337:                /*
1.11      itojun    338:                 * For fast leave to work, we have to know that we are the
                    339:                 * last person to send a report for this group.  Reports
                    340:                 * can potentially get looped back if we are a multicast
                    341:                 * router, so discard reports sourced by me.
                    342:                 * Note that it is impossible to check IFF_LOOPBACK flag of
                    343:                 * ifp for this purpose, since ip6_mloopback pass the physical
                    344:                 * interface to looutput.
                    345:                 */
1.7       itojun    346:                if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
                    347:                        break;
                    348:
1.22      itojun    349:                if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
1.7       itojun    350:                        break;
                    351:
1.22      itojun    352:                if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
                    353:                        mldh->mld_addr.s6_addr16[1] =
1.7       itojun    354:                                htons(ifp->if_index); /* XXX */
                    355:                /*
1.11      itojun    356:                 * If we belong to the group being reported, stop
                    357:                 * our timer for that group.
                    358:                 */
1.22      itojun    359:                IN6_LOOKUP_MULTI(mldh->mld_addr, ifp, in6m);
1.7       itojun    360:                if (in6m) {
                    361:                        in6m->in6m_timer = 0; /* transit to idle state */
1.22      itojun    362:                        in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
1.7       itojun    363:                }
                    364:
1.22      itojun    365:                if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
                    366:                        mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
1.7       itojun    367:                break;
                    368:        default:                /* this is impossible */
1.18      itojun    369: #if 0
1.19      itojun    370:                /*
                    371:                 * this case should be impossible because of filtering in
                    372:                 * icmp6_input().  But we explicitly disabled this part
                    373:                 * just in case.
                    374:                 */
1.22      itojun    375:                log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld6_type);
1.18      itojun    376: #endif
1.7       itojun    377:                break;
1.2       itojun    378:        }
1.11      itojun    379:
                    380:        m_freem(m);
1.2       itojun    381: }
                    382:
                    383: void
                    384: mld6_fasttimeo()
                    385: {
1.13      itojun    386:        struct in6_multi *in6m;
1.2       itojun    387:        struct in6_multistep step;
                    388:        int s;
                    389:        /*
                    390:         * Quick check to see if any work needs to be done, in order
                    391:         * to minimize the overhead of fasttimo processing.
                    392:         */
1.22      itojun    393:        if (!mld_timers_are_running)
1.2       itojun    394:                return;
                    395:
1.4       itojun    396:        s = splsoftnet();
1.22      itojun    397:        mld_timers_are_running = 0;
1.2       itojun    398:        IN6_FIRST_MULTI(step, in6m);
                    399:        while (in6m != NULL) {
                    400:                if (in6m->in6m_timer == 0) {
                    401:                        /* do nothing */
                    402:                } else if (--in6m->in6m_timer == 0) {
1.22      itojun    403:                        mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
                    404:                        in6m->in6m_state = MLD_IREPORTEDLAST;
1.2       itojun    405:                } else {
1.22      itojun    406:                        mld_timers_are_running = 1;
1.2       itojun    407:                }
                    408:                IN6_NEXT_MULTI(step, in6m);
                    409:        }
                    410:        splx(s);
                    411: }
                    412:
                    413: static void
                    414: mld6_sendpkt(in6m, type, dst)
                    415:        struct in6_multi *in6m;
                    416:        int type;
                    417:        const struct in6_addr *dst;
                    418: {
                    419:        struct mbuf *mh, *md;
1.22      itojun    420:        struct mld_hdr *mldh;
1.2       itojun    421:        struct ip6_hdr *ip6;
                    422:        struct ip6_moptions im6o;
                    423:        struct in6_ifaddr *ia;
                    424:        struct ifnet *ifp = in6m->in6m_ifp;
1.19      itojun    425:        int ignflags;
1.2       itojun    426:
                    427:        /*
                    428:         * At first, find a link local address on the outgoing interface
                    429:         * to use as the source address of the MLD packet.
1.19      itojun    430:         * We do not reject tentative addresses for MLD report to deal with
                    431:         * the case where we first join a link-local address.
1.2       itojun    432:         */
1.19      itojun    433:        ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
                    434:        if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
1.2       itojun    435:                return;
1.19      itojun    436:        if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
                    437:                ia = NULL;
1.2       itojun    438:
                    439:        /*
                    440:         * Allocate mbufs to store ip6 header and MLD header.
                    441:         * We allocate 2 mbufs and make chain in advance because
                    442:         * it is more convenient when inserting the hop-by-hop option later.
                    443:         */
                    444:        MGETHDR(mh, M_DONTWAIT, MT_HEADER);
                    445:        if (mh == NULL)
                    446:                return;
                    447:        MGET(md, M_DONTWAIT, MT_DATA);
                    448:        if (md == NULL) {
                    449:                m_free(mh);
                    450:                return;
                    451:        }
                    452:        mh->m_next = md;
                    453:
1.19      itojun    454:        mh->m_pkthdr.rcvif = NULL;
1.22      itojun    455:        mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
1.2       itojun    456:        mh->m_len = sizeof(struct ip6_hdr);
                    457:        MH_ALIGN(mh, sizeof(struct ip6_hdr));
                    458:
                    459:        /* fill in the ip6 header */
                    460:        ip6 = mtod(mh, struct ip6_hdr *);
                    461:        ip6->ip6_flow = 0;
1.8       itojun    462:        ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
                    463:        ip6->ip6_vfc |= IPV6_VERSION;
1.2       itojun    464:        /* ip6_plen will be set later */
                    465:        ip6->ip6_nxt = IPPROTO_ICMPV6;
                    466:        /* ip6_hlim will be set by im6o.im6o_multicast_hlim */
1.19      itojun    467:        ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
1.2       itojun    468:        ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
                    469:
                    470:        /* fill in the MLD header */
1.22      itojun    471:        md->m_len = sizeof(struct mld_hdr);
                    472:        mldh = mtod(md, struct mld_hdr *);
                    473:        mldh->mld_type = type;
                    474:        mldh->mld_code = 0;
                    475:        mldh->mld_cksum = 0;
1.2       itojun    476:        /* XXX: we assume the function will not be called for query messages */
1.22      itojun    477:        mldh->mld_maxdelay = 0;
                    478:        mldh->mld_reserved = 0;
                    479:        mldh->mld_addr = in6m->in6m_addr;
                    480:        if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
                    481:                mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
                    482:        mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
                    483:            sizeof(struct mld_hdr));
1.2       itojun    484:
                    485:        /* construct multicast option */
                    486:        bzero(&im6o, sizeof(im6o));
                    487:        im6o.im6o_multicast_ifp = ifp;
                    488:        im6o.im6o_multicast_hlim = 1;
                    489:
                    490:        /*
                    491:         * Request loopback of the report if we are acting as a multicast
                    492:         * router, so that the process-level routing daemon can hear it.
                    493:         */
                    494:        im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
                    495:
                    496:        /* increment output statictics */
                    497:        icmp6stat.icp6s_outhist[type]++;
1.15      itojun    498:        icmp6_ifstat_inc(ifp, ifs6_out_msg);
1.17      itojun    499:        switch (type) {
1.22      itojun    500:        case MLD_LISTENER_QUERY:
1.15      itojun    501:                icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
                    502:                break;
1.22      itojun    503:        case MLD_LISTENER_REPORT:
1.15      itojun    504:                icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
                    505:                break;
1.22      itojun    506:        case MLD_LISTENER_DONE:
1.15      itojun    507:                icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
                    508:                break;
1.7       itojun    509:        }
1.19      itojun    510:
1.24    ! jonathan  511:        ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
        !           512:            &im6o, (struct in6pcb *)0, NULL);
1.2       itojun    513: }

CVSweb <webmaster@jp.NetBSD.org>