xref: /freebsd/sys/netinet6/mld6.c (revision 39beb93c3f8bdbf72a61fda42300b5ebed7390c8)
1 /*-
2  * Copyright (C) 1998 WIDE Project.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the project nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *	$KAME: mld6.c,v 1.27 2001/04/04 05:17:30 itojun Exp $
30  */
31 
32 /*-
33  * Copyright (c) 1988 Stephen Deering.
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  * 4. 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 #include <sys/cdefs.h>
68 __FBSDID("$FreeBSD$");
69 
70 #include "opt_inet.h"
71 #include "opt_inet6.h"
72 
73 #include <sys/param.h>
74 #include <sys/systm.h>
75 #include <sys/mbuf.h>
76 #include <sys/socket.h>
77 #include <sys/protosw.h>
78 #include <sys/syslog.h>
79 #include <sys/kernel.h>
80 #include <sys/callout.h>
81 #include <sys/malloc.h>
82 #include <sys/vimage.h>
83 
84 #include <net/if.h>
85 
86 #include <netinet/in.h>
87 #include <netinet/in_var.h>
88 #include <netinet6/in6_var.h>
89 #include <netinet/ip6.h>
90 #include <netinet6/ip6_var.h>
91 #include <netinet6/scope6_var.h>
92 #include <netinet/icmp6.h>
93 #include <netinet6/mld6_var.h>
94 #include <netinet6/vinet6.h>
95 
96 /*
97  * Protocol constants
98  */
99 
100 /* denotes that the MLD max response delay field specifies time in milliseconds */
101 #define MLD_TIMER_SCALE	1000
102 /*
103  * time between repetitions of a node's initial report of interest in a
104  * multicast address(in seconds)
105  */
106 #define MLD_UNSOLICITED_REPORT_INTERVAL	10
107 
108 #ifdef VIMAGE_GLOBALS
109 static struct ip6_pktopts ip6_opts;
110 #endif
111 
112 static void mld6_sendpkt(struct in6_multi *, int, const struct in6_addr *);
113 static void mld_starttimer(struct in6_multi *);
114 static void mld_stoptimer(struct in6_multi *);
115 static void mld_timeo(struct in6_multi *);
116 static u_long mld_timerresid(struct in6_multi *);
117 
118 void
119 mld6_init(void)
120 {
121 	INIT_VNET_INET6(curvnet);
122 	static u_int8_t hbh_buf[8];
123 	struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
124 	u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
125 
126 	/* ip6h_nxt will be fill in later */
127 	hbh->ip6h_len = 0;	/* (8 >> 3) - 1 */
128 
129 	/* XXX: grotty hard coding... */
130 	hbh_buf[2] = IP6OPT_PADN;	/* 2 byte padding */
131 	hbh_buf[3] = 0;
132 	hbh_buf[4] = IP6OPT_ROUTER_ALERT;
133 	hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
134 	bcopy((caddr_t)&rtalert_code, &hbh_buf[6], sizeof(u_int16_t));
135 
136 	ip6_initpktopts(&V_ip6_opts);
137 	V_ip6_opts.ip6po_hbh = hbh;
138 }
139 
140 static void
141 mld_starttimer(struct in6_multi *in6m)
142 {
143 	struct timeval now;
144 
145 	microtime(&now);
146 	in6m->in6m_timer_expire.tv_sec = now.tv_sec + in6m->in6m_timer / hz;
147 	in6m->in6m_timer_expire.tv_usec = now.tv_usec +
148 	    (in6m->in6m_timer % hz) * (1000000 / hz);
149 	if (in6m->in6m_timer_expire.tv_usec > 1000000) {
150 		in6m->in6m_timer_expire.tv_sec++;
151 		in6m->in6m_timer_expire.tv_usec -= 1000000;
152 	}
153 
154 	/* start or restart the timer */
155 	callout_reset(in6m->in6m_timer_ch, in6m->in6m_timer,
156 	    (void (*)(void *))mld_timeo, in6m);
157 }
158 
159 static void
160 mld_stoptimer(struct in6_multi *in6m)
161 {
162 	if (in6m->in6m_timer == IN6M_TIMER_UNDEF)
163 		return;
164 
165 	callout_stop(in6m->in6m_timer_ch);
166 	in6m->in6m_timer = IN6M_TIMER_UNDEF;
167 }
168 
169 static void
170 mld_timeo(struct in6_multi *in6m)
171 {
172 	int s = splnet();
173 
174 	in6m->in6m_timer = IN6M_TIMER_UNDEF;
175 
176 	callout_stop(in6m->in6m_timer_ch);
177 
178 	switch (in6m->in6m_state) {
179 	case MLD_REPORTPENDING:
180 		mld6_start_listening(in6m);
181 		break;
182 	default:
183 		mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
184 		break;
185 	}
186 
187 	splx(s);
188 }
189 
190 static u_long
191 mld_timerresid(struct in6_multi *in6m)
192 {
193 	struct timeval now, diff;
194 
195 	microtime(&now);
196 
197 	if (now.tv_sec > in6m->in6m_timer_expire.tv_sec ||
198 	    (now.tv_sec == in6m->in6m_timer_expire.tv_sec &&
199 	    now.tv_usec > in6m->in6m_timer_expire.tv_usec)) {
200 		return (0);
201 	}
202 	diff = in6m->in6m_timer_expire;
203 	diff.tv_sec -= now.tv_sec;
204 	diff.tv_usec -= now.tv_usec;
205 	if (diff.tv_usec < 0) {
206 		diff.tv_sec--;
207 		diff.tv_usec += 1000000;
208 	}
209 
210 	/* return the remaining time in milliseconds */
211 	return (diff.tv_sec * 1000 + diff.tv_usec / 1000);
212 }
213 
214 void
215 mld6_start_listening(struct in6_multi *in6m)
216 {
217 	struct in6_addr all_in6;
218 	int s = splnet();
219 
220 	/*
221 	 * RFC2710 page 10:
222 	 * The node never sends a Report or Done for the link-scope all-nodes
223 	 * address.
224 	 * MLD messages are never sent for multicast addresses whose scope is 0
225 	 * (reserved) or 1 (node-local).
226 	 */
227 	all_in6 = in6addr_linklocal_allnodes;
228 	if (in6_setscope(&all_in6, in6m->in6m_ifp, NULL)) {
229 		/* XXX: this should not happen! */
230 		in6m->in6m_timer = 0;
231 		in6m->in6m_state = MLD_OTHERLISTENER;
232 	}
233 	if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
234 	    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
235 	    IPV6_ADDR_SCOPE_LINKLOCAL) {
236 		in6m->in6m_timer = 0;
237 		in6m->in6m_state = MLD_OTHERLISTENER;
238 	} else {
239 		mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
240 		in6m->in6m_timer = arc4random() %
241 			MLD_UNSOLICITED_REPORT_INTERVAL * hz;
242 		in6m->in6m_state = MLD_IREPORTEDLAST;
243 
244 		mld_starttimer(in6m);
245 	}
246 	splx(s);
247 }
248 
249 void
250 mld6_stop_listening(struct in6_multi *in6m)
251 {
252 	struct in6_addr allnode, allrouter;
253 
254 	allnode = in6addr_linklocal_allnodes;
255 	if (in6_setscope(&allnode, in6m->in6m_ifp, NULL)) {
256 		/* XXX: this should not happen! */
257 		return;
258 	}
259 	allrouter = in6addr_linklocal_allrouters;
260 	if (in6_setscope(&allrouter, in6m->in6m_ifp, NULL)) {
261 		/* XXX impossible */
262 		return;
263 	}
264 	if (in6m->in6m_state == MLD_IREPORTEDLAST &&
265 	    !IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &allnode) &&
266 	    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
267 	    IPV6_ADDR_SCOPE_INTFACELOCAL) {
268 		mld6_sendpkt(in6m, MLD_LISTENER_DONE, &allrouter);
269 	}
270 }
271 
272 void
273 mld6_input(struct mbuf *m, int off)
274 {
275 	INIT_VNET_INET6(curvnet);
276 	struct ip6_hdr *ip6 = mtod(m, struct ip6_hdr *);
277 	struct mld_hdr *mldh;
278 	struct ifnet *ifp = m->m_pkthdr.rcvif;
279 	struct in6_multi *in6m;
280 	struct in6_addr mld_addr, all_in6;
281 	struct in6_ifaddr *ia;
282 	struct ifmultiaddr *ifma;
283 	u_long timer;		/* timer value in the MLD query header */
284 
285 #ifndef PULLDOWN_TEST
286 	IP6_EXTHDR_CHECK(m, off, sizeof(*mldh),);
287 	mldh = (struct mld_hdr *)(mtod(m, caddr_t) + off);
288 #else
289 	IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
290 	if (mldh == NULL) {
291 		V_icmp6stat.icp6s_tooshort++;
292 		return;
293 	}
294 #endif
295 
296 	/* source address validation */
297 	ip6 = mtod(m, struct ip6_hdr *); /* in case mpullup */
298 	if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
299 		char ip6bufs[INET6_ADDRSTRLEN], ip6bufg[INET6_ADDRSTRLEN];
300 		log(LOG_ERR,
301 		    "mld6_input: src %s is not link-local (grp=%s)\n",
302 		    ip6_sprintf(ip6bufs, &ip6->ip6_src),
303 		    ip6_sprintf(ip6bufg, &mldh->mld_addr));
304 		/*
305 		 * spec (RFC2710) does not explicitly
306 		 * specify to discard the packet from a non link-local
307 		 * source address. But we believe it's expected to do so.
308 		 * XXX: do we have to allow :: as source?
309 		 */
310 		m_freem(m);
311 		return;
312 	}
313 
314 	/*
315 	 * make a copy for local work (in6_setscope() may modify the 1st arg)
316 	 */
317 	mld_addr = mldh->mld_addr;
318 	if (in6_setscope(&mld_addr, ifp, NULL)) {
319 		/* XXX: this should not happen! */
320 		m_free(m);
321 		return;
322 	}
323 
324 	/*
325 	 * In the MLD6 specification, there are 3 states and a flag.
326 	 *
327 	 * In Non-Listener state, we simply don't have a membership record.
328 	 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
329 	 * In Idle Listener state, our timer is not running
330 	 * (in6m->in6m_timer==IN6M_TIMER_UNDEF)
331 	 *
332 	 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
333 	 * we have heard a report from another member, or MLD_IREPORTEDLAST
334 	 * if we sent the last report.
335 	 */
336 	switch(mldh->mld_type) {
337 	case MLD_LISTENER_QUERY:
338 		if (ifp->if_flags & IFF_LOOPBACK)
339 			break;
340 
341 		if (!IN6_IS_ADDR_UNSPECIFIED(&mld_addr) &&
342 		    !IN6_IS_ADDR_MULTICAST(&mld_addr))
343 			break;	/* print error or log stat? */
344 
345 		all_in6 = in6addr_linklocal_allnodes;
346 		if (in6_setscope(&all_in6, ifp, NULL)) {
347 			/* XXX: this should not happen! */
348 			break;
349 		}
350 
351 		/*
352 		 * - Start the timers in all of our membership records
353 		 *   that the query applies to for the interface on
354 		 *   which the query arrived excl. those that belong
355 		 *   to the "all-nodes" group (ff02::1).
356 		 * - Restart any timer that is already running but has
357 		 *   A value longer than the requested timeout.
358 		 * - Use the value specified in the query message as
359 		 *   the maximum timeout.
360 		 */
361 		timer = ntohs(mldh->mld_maxdelay);
362 
363 		IFP_TO_IA6(ifp, ia);
364 		if (ia == NULL)
365 			break;
366 
367 		/*
368 		 * XXX: System timer resolution is too low to handle Max
369 		 * Response Delay, so set 1 to the internal timer even if
370 		 * the calculated value equals to zero when Max Response
371 		 * Delay is positive.
372 		 */
373 		timer = ntohs(mldh->mld_maxdelay) * PR_FASTHZ / MLD_TIMER_SCALE;
374 		if (timer == 0 && mldh->mld_maxdelay)
375 			timer = 1;
376 
377 		IF_ADDR_LOCK(ifp);
378 		TAILQ_FOREACH(ifma, &ifp->if_multiaddrs, ifma_link) {
379 			if (ifma->ifma_addr->sa_family != AF_INET6)
380 				continue;
381 			in6m = (struct in6_multi *)ifma->ifma_protospec;
382 
383 			if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_in6) ||
384 			    IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
385 			    IPV6_ADDR_SCOPE_LINKLOCAL)
386 				continue;
387 
388 			if (IN6_IS_ADDR_UNSPECIFIED(&mld_addr) ||
389 			    IN6_ARE_ADDR_EQUAL(&mld_addr, &in6m->in6m_addr)) {
390 				if (timer == 0) {
391 					/* send a report immediately */
392 					mld_stoptimer(in6m);
393 					mld6_sendpkt(in6m, MLD_LISTENER_REPORT,
394 						NULL);
395 					in6m->in6m_timer = 0; /* reset timer */
396 					in6m->in6m_state = MLD_IREPORTEDLAST;
397 				}
398 				else if (in6m->in6m_timer == IN6M_TIMER_UNDEF ||
399 				    mld_timerresid(in6m) > timer) {
400 					in6m->in6m_timer =
401 					   1 + (arc4random() % timer) * hz / 1000;
402 					mld_starttimer(in6m);
403 				}
404 			}
405 		}
406 		IF_ADDR_UNLOCK(ifp);
407 		break;
408 
409 	case MLD_LISTENER_REPORT:
410 		/*
411 		 * For fast leave to work, we have to know that we are the
412 		 * last person to send a report for this group.  Reports
413 		 * can potentially get looped back if we are a multicast
414 		 * router, so discard reports sourced by me.
415 		 * Note that it is impossible to check IFF_LOOPBACK flag of
416 		 * ifp for this purpose, since ip6_mloopback pass the physical
417 		 * interface to looutput.
418 		 */
419 		if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
420 			break;
421 
422 		if (!IN6_IS_ADDR_MULTICAST(&mld_addr))
423 			break;
424 
425 		/*
426 		 * If we belong to the group being reported, stop
427 		 * our timer for that group.
428 		 */
429 		IN6_LOOKUP_MULTI(mld_addr, ifp, in6m);
430 		if (in6m) {
431 			in6m->in6m_timer = 0; /* transit to idle state */
432 			in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
433 		}
434 		break;
435 	default:		/* this is impossible */
436 		log(LOG_ERR, "mld6_input: illegal type(%d)", mldh->mld_type);
437 		break;
438 	}
439 
440 	m_freem(m);
441 }
442 
443 static void
444 mld6_sendpkt(struct in6_multi *in6m, int type, const struct in6_addr *dst)
445 {
446 	INIT_VNET_INET6(curvnet);
447 	struct mbuf *mh, *md;
448 	struct mld_hdr *mldh;
449 	struct ip6_hdr *ip6;
450 	struct ip6_moptions im6o;
451 	struct in6_ifaddr *ia;
452 	struct ifnet *ifp = in6m->in6m_ifp;
453 	struct ifnet *outif = NULL;
454 
455 	/*
456 	 * At first, find a link local address on the outgoing interface
457 	 * to use as the source address of the MLD packet.
458 	 */
459 	if ((ia = in6ifa_ifpforlinklocal(ifp, IN6_IFF_NOTREADY|IN6_IFF_ANYCAST))
460 	    == NULL)
461 		return;
462 
463 	/*
464 	 * Allocate mbufs to store ip6 header and MLD header.
465 	 * We allocate 2 mbufs and make chain in advance because
466 	 * it is more convenient when inserting the hop-by-hop option later.
467 	 */
468 	MGETHDR(mh, M_DONTWAIT, MT_HEADER);
469 	if (mh == NULL)
470 		return;
471 	MGET(md, M_DONTWAIT, MT_DATA);
472 	if (md == NULL) {
473 		m_free(mh);
474 		return;
475 	}
476 	mh->m_next = md;
477 
478 	mh->m_pkthdr.rcvif = NULL;
479 	mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
480 	mh->m_len = sizeof(struct ip6_hdr);
481 	MH_ALIGN(mh, sizeof(struct ip6_hdr));
482 
483 	/* fill in the ip6 header */
484 	ip6 = mtod(mh, struct ip6_hdr *);
485 	ip6->ip6_flow = 0;
486 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
487 	ip6->ip6_vfc |= IPV6_VERSION;
488 	/* ip6_plen will be set later */
489 	ip6->ip6_nxt = IPPROTO_ICMPV6;
490 	/* ip6_hlim will be set by im6o.im6o_multicast_hlim */
491 	ip6->ip6_src = ia->ia_addr.sin6_addr;
492 	ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
493 
494 	/* fill in the MLD header */
495 	md->m_len = sizeof(struct mld_hdr);
496 	mldh = mtod(md, struct mld_hdr *);
497 	mldh->mld_type = type;
498 	mldh->mld_code = 0;
499 	mldh->mld_cksum = 0;
500 	/* XXX: we assume the function will not be called for query messages */
501 	mldh->mld_maxdelay = 0;
502 	mldh->mld_reserved = 0;
503 	mldh->mld_addr = in6m->in6m_addr;
504 	in6_clearscope(&mldh->mld_addr); /* XXX */
505 	mldh->mld_cksum = in6_cksum(mh, IPPROTO_ICMPV6, sizeof(struct ip6_hdr),
506 				    sizeof(struct mld_hdr));
507 
508 	/* construct multicast option */
509 	bzero(&im6o, sizeof(im6o));
510 	im6o.im6o_multicast_ifp = ifp;
511 	im6o.im6o_multicast_hlim = 1;
512 
513 	/*
514 	 * Request loopback of the report if we are acting as a multicast
515 	 * router, so that the process-level routing daemon can hear it.
516 	 */
517 	im6o.im6o_multicast_loop = (ip6_mrouter != NULL);
518 
519 	/* increment output statictics */
520 	V_icmp6stat.icp6s_outhist[type]++;
521 
522 	ip6_output(mh, &V_ip6_opts, NULL, 0, &im6o, &outif, NULL);
523 	if (outif) {
524 		icmp6_ifstat_inc(outif, ifs6_out_msg);
525 		switch (type) {
526 		case MLD_LISTENER_QUERY:
527 			icmp6_ifstat_inc(outif, ifs6_out_mldquery);
528 			break;
529 		case MLD_LISTENER_REPORT:
530 			icmp6_ifstat_inc(outif, ifs6_out_mldreport);
531 			break;
532 		case MLD_LISTENER_DONE:
533 			icmp6_ifstat_inc(outif, ifs6_out_mlddone);
534 			break;
535 		}
536 	}
537 }
538 
539 /*
540  * Add an address to the list of IP6 multicast addresses for a given interface.
541  * Add source addresses to the list also, if upstream router is MLDv2 capable
542  * and the number of source is not 0.
543  */
544 struct in6_multi *
545 in6_addmulti(struct in6_addr *maddr6, struct ifnet *ifp,
546     int *errorp, int delay)
547 {
548 	struct in6_multi *in6m;
549 
550 	*errorp = 0;
551 	in6m = NULL;
552 
553 	IFF_LOCKGIANT(ifp);
554 	/*IN6_MULTI_LOCK();*/
555 
556 	IN6_LOOKUP_MULTI(*maddr6, ifp, in6m);
557 	if (in6m != NULL) {
558 		/*
559 		 * If we already joined this group, just bump the
560 		 * refcount and return it.
561 		 */
562 		KASSERT(in6m->in6m_refcount >= 1,
563 		    ("%s: bad refcount %d", __func__, in6m->in6m_refcount));
564 		++in6m->in6m_refcount;
565 	} else do {
566 		struct in6_multi *nin6m;
567 		struct ifmultiaddr *ifma;
568 		struct sockaddr_in6 sa6;
569 
570 		bzero(&sa6, sizeof(sa6));
571 		sa6.sin6_family = AF_INET6;
572 		sa6.sin6_len = sizeof(struct sockaddr_in6);
573 		sa6.sin6_addr = *maddr6;
574 
575 		*errorp = if_addmulti(ifp, (struct sockaddr *)&sa6, &ifma);
576 		if (*errorp)
577 			break;
578 
579 		/*
580 		 * If ifma->ifma_protospec is null, then if_addmulti() created
581 		 * a new record.  Otherwise, bump refcount, and we are done.
582 		 */
583 		if (ifma->ifma_protospec != NULL) {
584 			in6m = ifma->ifma_protospec;
585 			++in6m->in6m_refcount;
586 			break;
587 		}
588 
589 		nin6m = malloc(sizeof(*nin6m), M_IP6MADDR, M_NOWAIT | M_ZERO);
590 		if (nin6m == NULL) {
591 			if_delmulti_ifma(ifma);
592 			break;
593 		}
594 
595 		nin6m->in6m_addr = *maddr6;
596 		nin6m->in6m_ifp = ifp;
597 		nin6m->in6m_refcount = 1;
598 		nin6m->in6m_ifma = ifma;
599 		ifma->ifma_protospec = nin6m;
600 
601 		nin6m->in6m_timer_ch = malloc(sizeof(*nin6m->in6m_timer_ch),
602 		    M_IP6MADDR, M_NOWAIT);
603 		if (nin6m->in6m_timer_ch == NULL) {
604 			free(nin6m, M_IP6MADDR);
605 			if_delmulti_ifma(ifma);
606 			break;
607 		}
608 
609 		LIST_INSERT_HEAD(&in6_multihead, nin6m, in6m_entry);
610 
611 		callout_init(nin6m->in6m_timer_ch, 0);
612 		nin6m->in6m_timer = delay;
613 		if (nin6m->in6m_timer > 0) {
614 			nin6m->in6m_state = MLD_REPORTPENDING;
615 			mld_starttimer(nin6m);
616 		}
617 
618 		mld6_start_listening(nin6m);
619 
620 		in6m = nin6m;
621 
622 	} while (0);
623 
624 	/*IN6_MULTI_UNLOCK();*/
625 	IFF_UNLOCKGIANT(ifp);
626 
627 	return (in6m);
628 }
629 
630 /*
631  * Delete a multicast address record.
632  *
633  * TODO: Locking, as per netinet.
634  */
635 void
636 in6_delmulti(struct in6_multi *in6m)
637 {
638 	struct ifmultiaddr *ifma;
639 
640 	KASSERT(in6m->in6m_refcount >= 1, ("%s: freeing freed in6m", __func__));
641 
642 	if (--in6m->in6m_refcount == 0) {
643 		mld_stoptimer(in6m);
644 		mld6_stop_listening(in6m);
645 
646 		ifma = in6m->in6m_ifma;
647 		KASSERT(ifma->ifma_protospec == in6m,
648 		    ("%s: ifma_protospec != in6m", __func__));
649 		ifma->ifma_protospec = NULL;
650 
651 		LIST_REMOVE(in6m, in6m_entry);
652 		free(in6m->in6m_timer_ch, M_IP6MADDR);
653 		free(in6m, M_IP6MADDR);
654 
655 		if_delmulti_ifma(ifma);
656 	}
657 }
658