Home | History | Annotate | Line # | Download | only in netinet6
mld6.c revision 1.27
      1  1.27    perry /*	$NetBSD: mld6.c,v 1.27 2005/02/26 22:45:12 perry Exp $	*/
      2  1.13   itojun /*	$KAME: mld6.c,v 1.25 2001/01/16 14:14:18 itojun Exp $	*/
      3   1.3  thorpej 
      4   1.2   itojun /*
      5   1.2   itojun  * Copyright (C) 1998 WIDE Project.
      6   1.2   itojun  * All rights reserved.
      7  1.13   itojun  *
      8   1.2   itojun  * Redistribution and use in source and binary forms, with or without
      9   1.2   itojun  * modification, are permitted provided that the following conditions
     10   1.2   itojun  * are met:
     11   1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     12   1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     13   1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     14   1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     15   1.2   itojun  *    documentation and/or other materials provided with the distribution.
     16   1.2   itojun  * 3. Neither the name of the project nor the names of its contributors
     17   1.2   itojun  *    may be used to endorse or promote products derived from this software
     18   1.2   itojun  *    without specific prior written permission.
     19  1.13   itojun  *
     20   1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
     21   1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     22   1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     23   1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
     24   1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     25   1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     26   1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     27   1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     28   1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     29   1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     30   1.2   itojun  * SUCH DAMAGE.
     31   1.2   itojun  */
     32   1.2   itojun 
     33   1.2   itojun /*
     34   1.2   itojun  * Copyright (c) 1992, 1993
     35   1.2   itojun  *	The Regents of the University of California.  All rights reserved.
     36   1.2   itojun  *
     37   1.2   itojun  * This code is derived from software contributed to Berkeley by
     38   1.2   itojun  * Stephen Deering of Stanford University.
     39   1.2   itojun  *
     40   1.2   itojun  * Redistribution and use in source and binary forms, with or without
     41   1.2   itojun  * modification, are permitted provided that the following conditions
     42   1.2   itojun  * are met:
     43   1.2   itojun  * 1. Redistributions of source code must retain the above copyright
     44   1.2   itojun  *    notice, this list of conditions and the following disclaimer.
     45   1.2   itojun  * 2. Redistributions in binary form must reproduce the above copyright
     46   1.2   itojun  *    notice, this list of conditions and the following disclaimer in the
     47   1.2   itojun  *    documentation and/or other materials provided with the distribution.
     48  1.23      agc  * 3. Neither the name of the University nor the names of its contributors
     49  1.23      agc  *    may be used to endorse or promote products derived from this software
     50  1.23      agc  *    without specific prior written permission.
     51  1.23      agc  *
     52  1.23      agc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     53  1.23      agc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     54  1.23      agc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     55  1.23      agc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     56  1.23      agc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     57  1.23      agc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     58  1.23      agc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  1.23      agc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     60  1.23      agc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     61  1.23      agc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     62  1.23      agc  * SUCH DAMAGE.
     63  1.23      agc  *
     64  1.23      agc  *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
     65  1.23      agc  */
     66  1.23      agc 
     67  1.23      agc /*
     68  1.23      agc  * Copyright (c) 1988 Stephen Deering.
     69  1.23      agc  *
     70  1.23      agc  * This code is derived from software contributed to Berkeley by
     71  1.23      agc  * Stephen Deering of Stanford University.
     72  1.23      agc  *
     73  1.23      agc  * Redistribution and use in source and binary forms, with or without
     74  1.23      agc  * modification, are permitted provided that the following conditions
     75  1.23      agc  * are met:
     76  1.23      agc  * 1. Redistributions of source code must retain the above copyright
     77  1.23      agc  *    notice, this list of conditions and the following disclaimer.
     78  1.23      agc  * 2. Redistributions in binary form must reproduce the above copyright
     79  1.23      agc  *    notice, this list of conditions and the following disclaimer in the
     80  1.23      agc  *    documentation and/or other materials provided with the distribution.
     81   1.2   itojun  * 3. All advertising materials mentioning features or use of this software
     82   1.2   itojun  *    must display the following acknowledgement:
     83   1.2   itojun  *	This product includes software developed by the University of
     84   1.2   itojun  *	California, Berkeley and its contributors.
     85   1.2   itojun  * 4. Neither the name of the University nor the names of its contributors
     86   1.2   itojun  *    may be used to endorse or promote products derived from this software
     87   1.2   itojun  *    without specific prior written permission.
     88   1.2   itojun  *
     89   1.2   itojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     90   1.2   itojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     91   1.2   itojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     92   1.2   itojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     93   1.2   itojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     94   1.2   itojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     95   1.2   itojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     96   1.2   itojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     97   1.2   itojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     98   1.2   itojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     99   1.2   itojun  * SUCH DAMAGE.
    100   1.2   itojun  *
    101   1.2   itojun  *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
    102   1.2   itojun  */
    103  1.16    lukem 
    104  1.16    lukem #include <sys/cdefs.h>
    105  1.27    perry __KERNEL_RCSID(0, "$NetBSD: mld6.c,v 1.27 2005/02/26 22:45:12 perry Exp $");
    106   1.2   itojun 
    107   1.2   itojun #include "opt_inet.h"
    108   1.2   itojun 
    109   1.2   itojun #include <sys/param.h>
    110   1.2   itojun #include <sys/systm.h>
    111   1.2   itojun #include <sys/mbuf.h>
    112   1.2   itojun #include <sys/socket.h>
    113   1.2   itojun #include <sys/protosw.h>
    114   1.2   itojun #include <sys/syslog.h>
    115   1.2   itojun 
    116   1.2   itojun #include <net/if.h>
    117   1.2   itojun 
    118   1.2   itojun #include <netinet/in.h>
    119   1.2   itojun #include <netinet/in_var.h>
    120  1.10   itojun #include <netinet/ip6.h>
    121   1.2   itojun #include <netinet6/ip6_var.h>
    122  1.10   itojun #include <netinet/icmp6.h>
    123   1.2   itojun #include <netinet6/mld6_var.h>
    124   1.2   itojun 
    125   1.7   itojun #include <net/net_osdep.h>
    126   1.7   itojun 
    127   1.2   itojun /*
    128   1.2   itojun  * Protocol constants
    129   1.2   itojun  */
    130   1.2   itojun 
    131   1.2   itojun /* denotes that the MLD max response delay field specifies time in milliseconds */
    132  1.22   itojun #define MLD_TIMER_SCALE	1000
    133   1.2   itojun /*
    134   1.2   itojun  * time between repetitions of a node's initial report of interest in a
    135   1.2   itojun  * multicast address(in seconds)
    136   1.2   itojun  */
    137  1.22   itojun #define MLD_UNSOLICITED_REPORT_INTERVAL	10
    138   1.2   itojun 
    139   1.2   itojun static struct ip6_pktopts ip6_opts;
    140  1.22   itojun static int mld_timers_are_running;
    141   1.2   itojun /* XXX: These are necessary for KAME's link-local hack */
    142  1.22   itojun static struct in6_addr mld_all_nodes_linklocal = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
    143  1.22   itojun static struct in6_addr mld_all_routers_linklocal = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
    144   1.2   itojun 
    145   1.2   itojun static void mld6_sendpkt __P((struct in6_multi *, int, const struct in6_addr *));
    146   1.2   itojun 
    147   1.2   itojun void
    148   1.2   itojun mld6_init()
    149   1.2   itojun {
    150   1.2   itojun 	static u_int8_t hbh_buf[8];
    151   1.2   itojun 	struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
    152   1.2   itojun 	u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
    153   1.2   itojun 
    154  1.22   itojun 	mld_timers_are_running = 0;
    155   1.2   itojun 
    156   1.2   itojun 	/* ip6h_nxt will be fill in later */
    157  1.11   itojun 	hbh->ip6h_len = 0;	/* (8 >> 3) - 1 */
    158   1.2   itojun 
    159   1.2   itojun 	/* XXX: grotty hard coding... */
    160   1.2   itojun 	hbh_buf[2] = IP6OPT_PADN;	/* 2 byte padding */
    161   1.2   itojun 	hbh_buf[3] = 0;
    162   1.2   itojun 	hbh_buf[4] = IP6OPT_RTALERT;
    163   1.2   itojun 	hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
    164   1.2   itojun 	bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
    165   1.2   itojun 
    166   1.2   itojun 	ip6_opts.ip6po_hbh = hbh;
    167   1.2   itojun 	/* We will specify the hoplimit by a multicast option. */
    168   1.2   itojun 	ip6_opts.ip6po_hlim = -1;
    169   1.2   itojun }
    170   1.2   itojun 
    171   1.2   itojun void
    172   1.2   itojun mld6_start_listening(in6m)
    173   1.2   itojun 	struct in6_multi *in6m;
    174   1.2   itojun {
    175   1.2   itojun 	/*
    176  1.11   itojun 	 * RFC2710 page 10:
    177   1.2   itojun 	 * The node never sends a Report or Done for the link-scope all-nodes
    178   1.2   itojun 	 * address.
    179   1.2   itojun 	 * MLD messages are never sent for multicast addresses whose scope is 0
    180   1.2   itojun 	 * (reserved) or 1 (node-local).
    181   1.2   itojun 	 */
    182  1.22   itojun 	mld_all_nodes_linklocal.s6_addr16[1] =
    183  1.19   itojun 	    htons(in6m->in6m_ifp->if_index); /* XXX */
    184  1.22   itojun 	if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld_all_nodes_linklocal) ||
    185   1.2   itojun 	    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) {
    186   1.2   itojun 		in6m->in6m_timer = 0;
    187  1.22   itojun 		in6m->in6m_state = MLD_OTHERLISTENER;
    188   1.2   itojun 	} else {
    189  1.22   itojun 		mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
    190  1.19   itojun 		in6m->in6m_timer =
    191  1.22   itojun 		    MLD_RANDOM_DELAY(MLD_UNSOLICITED_REPORT_INTERVAL *
    192  1.19   itojun 		    PR_FASTHZ);
    193  1.22   itojun 		in6m->in6m_state = MLD_IREPORTEDLAST;
    194  1.22   itojun 		mld_timers_are_running = 1;
    195   1.2   itojun 	}
    196   1.2   itojun }
    197   1.2   itojun 
    198   1.2   itojun void
    199   1.2   itojun mld6_stop_listening(in6m)
    200   1.2   itojun 	struct in6_multi *in6m;
    201   1.2   itojun {
    202  1.22   itojun 	mld_all_nodes_linklocal.s6_addr16[1] =
    203  1.19   itojun 	    htons(in6m->in6m_ifp->if_index); /* XXX */
    204  1.22   itojun 	mld_all_routers_linklocal.s6_addr16[1] =
    205  1.19   itojun 	    htons(in6m->in6m_ifp->if_index); /* XXX: necessary when mrouting */
    206   1.2   itojun 
    207  1.22   itojun 	if (in6m->in6m_state == MLD_IREPORTEDLAST &&
    208  1.22   itojun 	    (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &mld_all_nodes_linklocal)) &&
    209   1.2   itojun 	    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) > IPV6_ADDR_SCOPE_NODELOCAL)
    210  1.22   itojun 		mld6_sendpkt(in6m, MLD_LISTENER_DONE,
    211  1.22   itojun 		    &mld_all_routers_linklocal);
    212   1.2   itojun }
    213   1.2   itojun 
    214   1.2   itojun void
    215   1.2   itojun mld6_input(m, off)
    216   1.2   itojun 	struct mbuf *m;
    217   1.2   itojun 	int off;
    218   1.2   itojun {
    219   1.2   itojun 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
    220  1.22   itojun 	struct mld_hdr *mldh;
    221   1.2   itojun 	struct ifnet *ifp = m->m_pkthdr.rcvif;
    222   1.2   itojun 	struct in6_multi *in6m;
    223   1.2   itojun 	struct in6_ifaddr *ia;
    224   1.2   itojun 	int timer;		/* timer value in the MLD query header */
    225   1.2   itojun 
    226  1.22   itojun 	IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
    227  1.13   itojun 	if (mldh == NULL) {
    228  1.13   itojun 		icmp6stat.icp6s_tooshort++;
    229  1.13   itojun 		return;
    230  1.13   itojun 	}
    231  1.13   itojun 
    232   1.2   itojun 	/* source address validation */
    233  1.13   itojun 	ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
    234   1.2   itojun 	if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
    235  1.18   itojun #if 0
    236   1.2   itojun 		log(LOG_ERR,
    237  1.22   itojun 		    "mld_input: src %s is not link-local (grp=%s)\n",
    238  1.13   itojun 		    ip6_sprintf(&ip6->ip6_src),
    239  1.22   itojun 		    ip6_sprintf(&mldh->mld_addr));
    240  1.18   itojun #endif
    241   1.2   itojun 		/*
    242  1.11   itojun 		 * spec (RFC2710) does not explicitly
    243   1.2   itojun 		 * specify to discard the packet from a non link-local
    244   1.2   itojun 		 * source address. But we believe it's expected to do so.
    245  1.13   itojun 		 * XXX: do we have to allow :: as source?
    246   1.2   itojun 		 */
    247  1.11   itojun 		m_freem(m);
    248   1.2   itojun 		return;
    249   1.2   itojun 	}
    250   1.2   itojun 
    251   1.2   itojun 	/*
    252   1.2   itojun 	 * In the MLD6 specification, there are 3 states and a flag.
    253   1.2   itojun 	 *
    254   1.2   itojun 	 * In Non-Listener state, we simply don't have a membership record.
    255   1.2   itojun 	 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
    256   1.2   itojun 	 * In Idle Listener state, our timer is not running (in6m->in6m_timer==0)
    257   1.2   itojun 	 *
    258  1.22   itojun 	 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
    259  1.22   itojun 	 * we have heard a report from another member, or MLD_IREPORTEDLAST
    260   1.2   itojun 	 * if we sent the last report.
    261   1.2   itojun 	 */
    262  1.22   itojun 	switch (mldh->mld_type) {
    263  1.22   itojun 	case MLD_LISTENER_QUERY:
    264   1.7   itojun 		if (ifp->if_flags & IFF_LOOPBACK)
    265   1.7   itojun 			break;
    266   1.7   itojun 
    267  1.22   itojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) &&
    268  1.22   itojun 		    !IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
    269   1.7   itojun 			break;	/* print error or log stat? */
    270  1.22   itojun 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
    271  1.22   itojun 			mldh->mld_addr.s6_addr16[1] =
    272  1.19   itojun 			    htons(ifp->if_index); /* XXX */
    273   1.2   itojun 
    274   1.7   itojun 		/*
    275  1.11   itojun 		 * - Start the timers in all of our membership records
    276  1.11   itojun 		 *   that the query applies to for the interface on
    277  1.11   itojun 		 *   which the query arrived excl. those that belong
    278  1.11   itojun 		 *   to the "all-nodes" group (ff02::1).
    279  1.11   itojun 		 * - Restart any timer that is already running but has
    280  1.11   itojun 		 *   A value longer than the requested timeout.
    281  1.11   itojun 		 * - Use the value specified in the query message as
    282  1.11   itojun 		 *   the maximum timeout.
    283  1.11   itojun 		 */
    284   1.7   itojun 		IFP_TO_IA6(ifp, ia);
    285   1.7   itojun 		if (ia == NULL)
    286   1.7   itojun 			break;
    287   1.2   itojun 
    288   1.2   itojun 		/*
    289  1.11   itojun 		 * XXX: System timer resolution is too low to handle Max
    290  1.11   itojun 		 * Response Delay, so set 1 to the internal timer even if
    291  1.11   itojun 		 * the calculated value equals to zero when Max Response
    292  1.11   itojun 		 * Delay is positive.
    293  1.11   itojun 		 */
    294  1.22   itojun 		timer = ntohs(mldh->mld_maxdelay)*PR_FASTHZ/MLD_TIMER_SCALE;
    295  1.22   itojun 		if (timer == 0 && mldh->mld_maxdelay)
    296   1.7   itojun 			timer = 1;
    297  1.22   itojun 		mld_all_nodes_linklocal.s6_addr16[1] =
    298   1.7   itojun 			htons(ifp->if_index); /* XXX */
    299  1.20   itojun 
    300   1.7   itojun 		for (in6m = ia->ia6_multiaddrs.lh_first;
    301   1.7   itojun 		     in6m;
    302   1.7   itojun 		     in6m = in6m->in6m_entry.le_next)
    303   1.7   itojun 		{
    304   1.7   itojun 			if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr,
    305  1.22   itojun 						&mld_all_nodes_linklocal) ||
    306   1.7   itojun 			    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
    307   1.7   itojun 			    IPV6_ADDR_SCOPE_LINKLOCAL)
    308   1.7   itojun 				continue;
    309   1.2   itojun 
    310  1.22   itojun 			if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) ||
    311  1.22   itojun 			    IN6_ARE_ADDR_EQUAL(&mldh->mld_addr,
    312   1.7   itojun 						&in6m->in6m_addr))
    313   1.7   itojun 			{
    314   1.7   itojun 				if (timer == 0) {
    315   1.7   itojun 					/* send a report immediately */
    316  1.22   itojun 					mld6_sendpkt(in6m, MLD_LISTENER_REPORT,
    317  1.19   itojun 					    NULL);
    318   1.7   itojun 					in6m->in6m_timer = 0; /* reset timer */
    319  1.22   itojun 					in6m->in6m_state = MLD_IREPORTEDLAST;
    320   1.7   itojun 				}
    321   1.7   itojun 				else if (in6m->in6m_timer == 0 || /*idle state*/
    322  1.19   itojun 				    in6m->in6m_timer > timer) {
    323   1.7   itojun 					in6m->in6m_timer =
    324  1.22   itojun 					    MLD_RANDOM_DELAY(timer);
    325  1.22   itojun 					mld_timers_are_running = 1;
    326   1.7   itojun 				}
    327   1.7   itojun 			}
    328   1.7   itojun 		}
    329   1.2   itojun 
    330  1.22   itojun 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
    331  1.22   itojun 			mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
    332   1.7   itojun 		break;
    333  1.22   itojun 	case MLD_LISTENER_REPORT:
    334   1.2   itojun 		/*
    335  1.11   itojun 		 * For fast leave to work, we have to know that we are the
    336  1.11   itojun 		 * last person to send a report for this group.  Reports
    337  1.11   itojun 		 * can potentially get looped back if we are a multicast
    338  1.11   itojun 		 * router, so discard reports sourced by me.
    339  1.11   itojun 		 * Note that it is impossible to check IFF_LOOPBACK flag of
    340  1.11   itojun 		 * ifp for this purpose, since ip6_mloopback pass the physical
    341  1.11   itojun 		 * interface to looutput.
    342  1.11   itojun 		 */
    343   1.7   itojun 		if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
    344   1.7   itojun 			break;
    345   1.7   itojun 
    346  1.22   itojun 		if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
    347   1.7   itojun 			break;
    348   1.7   itojun 
    349  1.22   itojun 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
    350  1.22   itojun 			mldh->mld_addr.s6_addr16[1] =
    351   1.7   itojun 				htons(ifp->if_index); /* XXX */
    352   1.7   itojun 		/*
    353  1.11   itojun 		 * If we belong to the group being reported, stop
    354  1.11   itojun 		 * our timer for that group.
    355  1.11   itojun 		 */
    356  1.22   itojun 		IN6_LOOKUP_MULTI(mldh->mld_addr, ifp, in6m);
    357   1.7   itojun 		if (in6m) {
    358   1.7   itojun 			in6m->in6m_timer = 0; /* transit to idle state */
    359  1.22   itojun 			in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
    360   1.7   itojun 		}
    361   1.7   itojun 
    362  1.22   itojun 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
    363  1.22   itojun 			mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
    364   1.7   itojun 		break;
    365   1.7   itojun 	default:		/* this is impossible */
    366  1.18   itojun #if 0
    367  1.19   itojun 		/*
    368  1.19   itojun 		 * this case should be impossible because of filtering in
    369  1.19   itojun 		 * icmp6_input().  But we explicitly disabled this part
    370  1.19   itojun 		 * just in case.
    371  1.19   itojun 		 */
    372  1.22   itojun 		log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld6_type);
    373  1.18   itojun #endif
    374   1.7   itojun 		break;
    375   1.2   itojun 	}
    376  1.11   itojun 
    377  1.11   itojun 	m_freem(m);
    378   1.2   itojun }
    379   1.2   itojun 
    380   1.2   itojun void
    381   1.2   itojun mld6_fasttimeo()
    382   1.2   itojun {
    383  1.13   itojun 	struct in6_multi *in6m;
    384   1.2   itojun 	struct in6_multistep step;
    385   1.2   itojun 	int s;
    386   1.2   itojun 	/*
    387   1.2   itojun 	 * Quick check to see if any work needs to be done, in order
    388   1.2   itojun 	 * to minimize the overhead of fasttimo processing.
    389   1.2   itojun 	 */
    390  1.22   itojun 	if (!mld_timers_are_running)
    391   1.2   itojun 		return;
    392   1.2   itojun 
    393   1.4   itojun 	s = splsoftnet();
    394  1.22   itojun 	mld_timers_are_running = 0;
    395   1.2   itojun 	IN6_FIRST_MULTI(step, in6m);
    396   1.2   itojun 	while (in6m != NULL) {
    397   1.2   itojun 		if (in6m->in6m_timer == 0) {
    398   1.2   itojun 			/* do nothing */
    399   1.2   itojun 		} else if (--in6m->in6m_timer == 0) {
    400  1.22   itojun 			mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
    401  1.22   itojun 			in6m->in6m_state = MLD_IREPORTEDLAST;
    402   1.2   itojun 		} else {
    403  1.22   itojun 			mld_timers_are_running = 1;
    404   1.2   itojun 		}
    405   1.2   itojun 		IN6_NEXT_MULTI(step, in6m);
    406   1.2   itojun 	}
    407   1.2   itojun 	splx(s);
    408   1.2   itojun }
    409   1.2   itojun 
    410   1.2   itojun static void
    411   1.2   itojun mld6_sendpkt(in6m, type, dst)
    412   1.2   itojun 	struct in6_multi *in6m;
    413   1.2   itojun 	int type;
    414   1.2   itojun 	const struct in6_addr *dst;
    415   1.2   itojun {
    416   1.2   itojun 	struct mbuf *mh, *md;
    417  1.22   itojun 	struct mld_hdr *mldh;
    418   1.2   itojun 	struct ip6_hdr *ip6;
    419   1.2   itojun 	struct ip6_moptions im6o;
    420   1.2   itojun 	struct in6_ifaddr *ia;
    421   1.2   itojun 	struct ifnet *ifp = in6m->in6m_ifp;
    422  1.19   itojun 	int ignflags;
    423   1.2   itojun 
    424   1.2   itojun 	/*
    425   1.2   itojun 	 * At first, find a link local address on the outgoing interface
    426   1.2   itojun 	 * to use as the source address of the MLD packet.
    427  1.19   itojun 	 * We do not reject tentative addresses for MLD report to deal with
    428  1.19   itojun 	 * the case where we first join a link-local address.
    429   1.2   itojun 	 */
    430  1.19   itojun 	ignflags = (IN6_IFF_NOTREADY|IN6_IFF_ANYCAST) & ~IN6_IFF_TENTATIVE;
    431  1.19   itojun 	if ((ia = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL)
    432   1.2   itojun 		return;
    433  1.19   itojun 	if ((ia->ia6_flags & IN6_IFF_TENTATIVE))
    434  1.19   itojun 		ia = NULL;
    435   1.2   itojun 
    436   1.2   itojun 	/*
    437   1.2   itojun 	 * Allocate mbufs to store ip6 header and MLD header.
    438   1.2   itojun 	 * We allocate 2 mbufs and make chain in advance because
    439   1.2   itojun 	 * it is more convenient when inserting the hop-by-hop option later.
    440   1.2   itojun 	 */
    441   1.2   itojun 	MGETHDR(mh, M_DONTWAIT, MT_HEADER);
    442   1.2   itojun 	if (mh == NULL)
    443   1.2   itojun 		return;
    444   1.2   itojun 	MGET(md, M_DONTWAIT, MT_DATA);
    445   1.2   itojun 	if (md == NULL) {
    446   1.2   itojun 		m_free(mh);
    447   1.2   itojun 		return;
    448   1.2   itojun 	}
    449   1.2   itojun 	mh->m_next = md;
    450   1.2   itojun 
    451  1.19   itojun 	mh->m_pkthdr.rcvif = NULL;
    452  1.22   itojun 	mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
    453   1.2   itojun 	mh->m_len = sizeof(struct ip6_hdr);
    454   1.2   itojun 	MH_ALIGN(mh, sizeof(struct ip6_hdr));
    455   1.2   itojun 
    456   1.2   itojun 	/* fill in the ip6 header */
    457   1.2   itojun 	ip6 = mtod(mh, struct ip6_hdr *);
    458   1.2   itojun 	ip6->ip6_flow = 0;
    459   1.8   itojun 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
    460   1.8   itojun 	ip6->ip6_vfc |= IPV6_VERSION;
    461   1.2   itojun 	/* ip6_plen will be set later */
    462   1.2   itojun 	ip6->ip6_nxt = IPPROTO_ICMPV6;
    463   1.2   itojun 	/* ip6_hlim will be set by im6o.im6o_multicast_hlim */
    464  1.19   itojun 	ip6->ip6_src = ia ? ia->ia_addr.sin6_addr : in6addr_any;
    465   1.2   itojun 	ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
    466   1.2   itojun 
    467   1.2   itojun 	/* fill in the MLD header */
    468  1.22   itojun 	md->m_len = sizeof(struct mld_hdr);
    469  1.22   itojun 	mldh = mtod(md, struct mld_hdr *);
    470  1.22   itojun 	mldh->mld_type = type;
    471  1.22   itojun 	mldh->mld_code = 0;
    472  1.22   itojun 	mldh->mld_cksum = 0;
    473   1.2   itojun 	/* XXX: we assume the function will not be called for query messages */
    474  1.22   itojun 	mldh->mld_maxdelay = 0;
    475  1.22   itojun 	mldh->mld_reserved = 0;
    476  1.22   itojun 	mldh->mld_addr = in6m->in6m_addr;
    477  1.22   itojun 	if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
    478  1.22   itojun 		mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
    479  1.22   itojun 	mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
    480  1.22   itojun 	    sizeof(struct mld_hdr));
    481   1.2   itojun 
    482   1.2   itojun 	/* construct multicast option */
    483   1.2   itojun 	bzero(&im6o, sizeof(im6o));
    484   1.2   itojun 	im6o.im6o_multicast_ifp = ifp;
    485   1.2   itojun 	im6o.im6o_multicast_hlim = 1;
    486   1.2   itojun 
    487   1.2   itojun 	/*
    488   1.2   itojun 	 * Request loopback of the report if we are acting as a multicast
    489   1.2   itojun 	 * router, so that the process-level routing daemon can hear it.
    490   1.2   itojun 	 */
    491   1.2   itojun 	im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
    492   1.2   itojun 
    493   1.2   itojun 	/* increment output statictics */
    494   1.2   itojun 	icmp6stat.icp6s_outhist[type]++;
    495  1.15   itojun 	icmp6_ifstat_inc(ifp, ifs6_out_msg);
    496  1.17   itojun 	switch (type) {
    497  1.22   itojun 	case MLD_LISTENER_QUERY:
    498  1.15   itojun 		icmp6_ifstat_inc(ifp, ifs6_out_mldquery);
    499  1.15   itojun 		break;
    500  1.22   itojun 	case MLD_LISTENER_REPORT:
    501  1.15   itojun 		icmp6_ifstat_inc(ifp, ifs6_out_mldreport);
    502  1.15   itojun 		break;
    503  1.22   itojun 	case MLD_LISTENER_DONE:
    504  1.15   itojun 		icmp6_ifstat_inc(ifp, ifs6_out_mlddone);
    505  1.15   itojun 		break;
    506   1.7   itojun 	}
    507  1.19   itojun 
    508  1.27    perry 	ip6_output(mh, &ip6_opts, NULL, ia ? 0 : IPV6_UNSPECSRC,
    509  1.25   itojun 	    &im6o, (struct socket *)NULL, NULL);
    510   1.2   itojun }
    511