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