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