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