xref: /openbsd-src/sys/netinet6/mld6.c (revision b6da9889d28572532aad9484adda51c5bfae8865)
1*b6da9889Sjsg /*	$OpenBSD: mld6.c,v 1.64 2024/08/19 08:07:16 jsg Exp $	*/
27ee01f6fSitojun /*	$KAME: mld6.c,v 1.26 2001/02/16 14:50:35 itojun Exp $	*/
3287546eaSitojun 
4287546eaSitojun /*
5287546eaSitojun  * Copyright (C) 1998 WIDE Project.
6287546eaSitojun  * All rights reserved.
7287546eaSitojun  *
8287546eaSitojun  * Redistribution and use in source and binary forms, with or without
9287546eaSitojun  * modification, are permitted provided that the following conditions
10287546eaSitojun  * are met:
11287546eaSitojun  * 1. Redistributions of source code must retain the above copyright
12287546eaSitojun  *    notice, this list of conditions and the following disclaimer.
13287546eaSitojun  * 2. Redistributions in binary form must reproduce the above copyright
14287546eaSitojun  *    notice, this list of conditions and the following disclaimer in the
15287546eaSitojun  *    documentation and/or other materials provided with the distribution.
16287546eaSitojun  * 3. Neither the name of the project nor the names of its contributors
17287546eaSitojun  *    may be used to endorse or promote products derived from this software
18287546eaSitojun  *    without specific prior written permission.
19287546eaSitojun  *
20287546eaSitojun  * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
21287546eaSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22287546eaSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23287546eaSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
24287546eaSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25287546eaSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26287546eaSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27287546eaSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28287546eaSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29287546eaSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30287546eaSitojun  * SUCH DAMAGE.
31287546eaSitojun  */
32287546eaSitojun 
33287546eaSitojun /*
34287546eaSitojun  * Copyright (c) 1988 Stephen Deering.
35287546eaSitojun  * Copyright (c) 1992, 1993
36287546eaSitojun  *	The Regents of the University of California.  All rights reserved.
37287546eaSitojun  *
38287546eaSitojun  * This code is derived from software contributed to Berkeley by
39287546eaSitojun  * Stephen Deering of Stanford University.
40287546eaSitojun  *
41287546eaSitojun  * Redistribution and use in source and binary forms, with or without
42287546eaSitojun  * modification, are permitted provided that the following conditions
43287546eaSitojun  * are met:
44287546eaSitojun  * 1. Redistributions of source code must retain the above copyright
45287546eaSitojun  *    notice, this list of conditions and the following disclaimer.
46287546eaSitojun  * 2. Redistributions in binary form must reproduce the above copyright
47287546eaSitojun  *    notice, this list of conditions and the following disclaimer in the
48287546eaSitojun  *    documentation and/or other materials provided with the distribution.
4929295d1cSmillert  * 3. Neither the name of the University nor the names of its contributors
50287546eaSitojun  *    may be used to endorse or promote products derived from this software
51287546eaSitojun  *    without specific prior written permission.
52287546eaSitojun  *
53287546eaSitojun  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
54287546eaSitojun  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
55287546eaSitojun  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
56287546eaSitojun  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
57287546eaSitojun  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
58287546eaSitojun  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
59287546eaSitojun  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
60287546eaSitojun  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
61287546eaSitojun  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
62287546eaSitojun  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
63287546eaSitojun  * SUCH DAMAGE.
64287546eaSitojun  *
65287546eaSitojun  *	@(#)igmp.c	8.1 (Berkeley) 7/19/93
66287546eaSitojun  */
67287546eaSitojun 
68287546eaSitojun #include <sys/param.h>
69287546eaSitojun #include <sys/systm.h>
70287546eaSitojun #include <sys/mbuf.h>
71287546eaSitojun #include <sys/socket.h>
72287546eaSitojun #include <sys/protosw.h>
73287546eaSitojun #include <sys/syslog.h>
74287546eaSitojun 
75287546eaSitojun #include <net/if.h>
762bea58a8Smpi #include <net/if_var.h>
7794c0e2bdSbluhm #include <net/route.h>
78287546eaSitojun 
79287546eaSitojun #include <netinet/in.h>
80dc572864Sbluhm #include <netinet6/in6_var.h>
81fa86ee14Sitojun #include <netinet/ip6.h>
82287546eaSitojun #include <netinet6/ip6_var.h>
83fa86ee14Sitojun #include <netinet/icmp6.h>
84cc0f2657Sjsg #include <netinet6/mld6.h>
85287546eaSitojun #include <netinet6/mld6_var.h>
86287546eaSitojun 
87287546eaSitojun static struct ip6_pktopts ip6_opts;
88fc751501Sbluhm int	mld6_timers_are_running;	/* [a] shortcut for fast timer */
89287546eaSitojun 
90fc751501Sbluhm int mld6_checktimer(struct ifnet *);
91c4071fd1Smillert static void mld6_sendpkt(struct in6_multi *, int, const struct in6_addr *);
92287546eaSitojun 
93287546eaSitojun void
94c799dc6dSnaddy mld6_init(void)
95287546eaSitojun {
96287546eaSitojun 	static u_int8_t hbh_buf[8];
97287546eaSitojun 	struct ip6_hbh *hbh = (struct ip6_hbh *)hbh_buf;
98287546eaSitojun 	u_int16_t rtalert_code = htons((u_int16_t)IP6OPT_RTALERT_MLD);
99287546eaSitojun 
100679bdce9Sbluhm 	mld6_timers_are_running = 0;
101287546eaSitojun 
102287546eaSitojun 	/* ip6h_nxt will be fill in later */
103287546eaSitojun 	hbh->ip6h_len = 0;	/* (8 >> 3) - 1 */
104287546eaSitojun 
105287546eaSitojun 	/* XXX: grotty hard coding... */
106287546eaSitojun 	hbh_buf[2] = IP6OPT_PADN;	/* 2 byte padding */
107287546eaSitojun 	hbh_buf[3] = 0;
1086b532452Sitojun 	hbh_buf[4] = IP6OPT_ROUTER_ALERT;
109287546eaSitojun 	hbh_buf[5] = IP6OPT_RTALERT_LEN - 2;
110985a0e17Sdhill 	memcpy(&hbh_buf[6], (caddr_t)&rtalert_code, sizeof(u_int16_t));
111287546eaSitojun 
112f3bfe48bSrzalamena 	ip6_initpktopts(&ip6_opts);
113287546eaSitojun 	ip6_opts.ip6po_hbh = hbh;
114287546eaSitojun }
115287546eaSitojun 
116287546eaSitojun void
117ee37ea65Smcbride mld6_start_listening(struct in6_multi *in6m)
118287546eaSitojun {
11956bdcea2Sbluhm 	/* XXX: These are necessary for KAME's link-local hack */
12056bdcea2Sbluhm 	struct in6_addr all_nodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
121fc751501Sbluhm 	int running = 0;
12256bdcea2Sbluhm 
123287546eaSitojun 	/*
124f4f4d166Sitojun 	 * RFC2710 page 10:
125287546eaSitojun 	 * The node never sends a Report or Done for the link-scope all-nodes
126287546eaSitojun 	 * address.
127287546eaSitojun 	 * MLD messages are never sent for multicast addresses whose scope is 0
128287546eaSitojun 	 * (reserved) or 1 (node-local).
129287546eaSitojun 	 */
13056bdcea2Sbluhm 	all_nodes.s6_addr16[1] = htons(in6m->in6m_ifidx);
13156bdcea2Sbluhm 	if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_nodes) ||
13256bdcea2Sbluhm 	    __IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
13356bdcea2Sbluhm 	    __IPV6_ADDR_SCOPE_LINKLOCAL) {
134287546eaSitojun 		in6m->in6m_timer = 0;
13522a58e88Sitojun 		in6m->in6m_state = MLD_OTHERLISTENER;
136287546eaSitojun 	} else {
13722a58e88Sitojun 		mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
138d8a7e3a7Sitojun 		in6m->in6m_timer =
139cc0f2657Sjsg 		    MLD_RANDOM_DELAY(MLD_V1_MAX_RI *
140d8a7e3a7Sitojun 		    PR_FASTHZ);
14122a58e88Sitojun 		in6m->in6m_state = MLD_IREPORTEDLAST;
142fc751501Sbluhm 		running = 1;
143fc751501Sbluhm 	}
144fc751501Sbluhm 
145fc751501Sbluhm 	if (running) {
146fc751501Sbluhm 		membar_producer();
147fc751501Sbluhm 		atomic_store_int(&mld6_timers_are_running, running);
148287546eaSitojun 	}
149287546eaSitojun }
150287546eaSitojun 
151287546eaSitojun void
152ee37ea65Smcbride mld6_stop_listening(struct in6_multi *in6m)
153287546eaSitojun {
15456bdcea2Sbluhm 	/* XXX: These are necessary for KAME's link-local hack */
15556bdcea2Sbluhm 	struct in6_addr all_nodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
15656bdcea2Sbluhm 	struct in6_addr all_routers = IN6ADDR_LINKLOCAL_ALLROUTERS_INIT;
15756bdcea2Sbluhm 
15856bdcea2Sbluhm 	all_nodes.s6_addr16[1] = htons(in6m->in6m_ifidx);
15956bdcea2Sbluhm 	/* XXX: necessary when mrouting */
16056bdcea2Sbluhm 	all_routers.s6_addr16[1] = htons(in6m->in6m_ifidx);
161287546eaSitojun 
16222a58e88Sitojun 	if (in6m->in6m_state == MLD_IREPORTEDLAST &&
16356bdcea2Sbluhm 	    (!IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_nodes)) &&
16456bdcea2Sbluhm 	    __IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) >
16556bdcea2Sbluhm 	    __IPV6_ADDR_SCOPE_INTFACELOCAL)
16656bdcea2Sbluhm 		mld6_sendpkt(in6m, MLD_LISTENER_DONE, &all_routers);
167287546eaSitojun }
168287546eaSitojun 
169287546eaSitojun void
170ee37ea65Smcbride mld6_input(struct mbuf *m, int off)
171287546eaSitojun {
17245ac4bb9Schl 	struct ip6_hdr *ip6;
17322a58e88Sitojun 	struct mld_hdr *mldh;
174fb492c37Smpi 	struct ifnet *ifp;
175287546eaSitojun 	struct in6_multi *in6m;
1762bea58a8Smpi 	struct ifmaddr *ifma;
177287546eaSitojun 	int timer;		/* timer value in the MLD query header */
178fc751501Sbluhm 	int running = 0;
17956bdcea2Sbluhm 	/* XXX: These are necessary for KAME's link-local hack */
18056bdcea2Sbluhm 	struct in6_addr all_nodes = IN6ADDR_LINKLOCAL_ALLNODES_INIT;
181287546eaSitojun 
18222a58e88Sitojun 	IP6_EXTHDR_GET(mldh, struct mld_hdr *, m, off, sizeof(*mldh));
183f4f4d166Sitojun 	if (mldh == NULL) {
18407ba5f7cSjca 		icmp6stat_inc(icp6s_tooshort);
185f4f4d166Sitojun 		return;
186f4f4d166Sitojun 	}
187f4f4d166Sitojun 
1887ee01f6fSitojun 	/* source address validation */
1897ee01f6fSitojun 	ip6 = mtod(m, struct ip6_hdr *);/* in case mpullup */
1907ee01f6fSitojun 	if (!IN6_IS_ADDR_LINKLOCAL(&ip6->ip6_src)) {
19147b08f28Sitojun #if 0
192bbcf0337Smpi 		char src[INET6_ADDRSTRLEN], grp[INET6_ADDRSTRLEN];
193bbcf0337Smpi 
1947ee01f6fSitojun 		log(LOG_ERR,
19522a58e88Sitojun 		    "mld_input: src %s is not link-local (grp=%s)\n",
196bbcf0337Smpi 		    inet_ntop(AF_INET6, &ip6->ip6_src, src, sizeof(src)),
197bbcf0337Smpi 		    inet_ntop(AF_INET6, &mldh->mld_addr, grp, sizeof(grp)));
19847b08f28Sitojun #endif
1997ee01f6fSitojun 		/*
2007ee01f6fSitojun 		 * spec (RFC2710) does not explicitly
2017ee01f6fSitojun 		 * specify to discard the packet from a non link-local
2027ee01f6fSitojun 		 * source address. But we believe it's expected to do so.
2037ee01f6fSitojun 		 */
2047ee01f6fSitojun 		m_freem(m);
2057ee01f6fSitojun 		return;
2067ee01f6fSitojun 	}
2077ee01f6fSitojun 
208d66c6059Sclaudio 	ifp = if_get(m->m_pkthdr.ph_ifidx);
209d66c6059Sclaudio 	if (ifp == NULL) {
210d66c6059Sclaudio 		m_freem(m);
211d66c6059Sclaudio 		return;
212d66c6059Sclaudio 	}
213d66c6059Sclaudio 
214287546eaSitojun 	/*
215287546eaSitojun 	 * In the MLD6 specification, there are 3 states and a flag.
216287546eaSitojun 	 *
217287546eaSitojun 	 * In Non-Listener state, we simply don't have a membership record.
218287546eaSitojun 	 * In Delaying Listener state, our timer is running (in6m->in6m_timer)
219287546eaSitojun 	 * In Idle Listener state, our timer is not running (in6m->in6m_timer==0)
220287546eaSitojun 	 *
22122a58e88Sitojun 	 * The flag is in6m->in6m_state, it is set to MLD_OTHERLISTENER if
22222a58e88Sitojun 	 * we have heard a report from another member, or MLD_IREPORTEDLAST
223287546eaSitojun 	 * if we sent the last report.
224287546eaSitojun 	 */
22522a58e88Sitojun 	switch(mldh->mld_type) {
22622a58e88Sitojun 	case MLD_LISTENER_QUERY:
227287546eaSitojun 		if (ifp->if_flags & IFF_LOOPBACK)
228287546eaSitojun 			break;
229287546eaSitojun 
23022a58e88Sitojun 		if (!IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) &&
23122a58e88Sitojun 		    !IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
232287546eaSitojun 			break;	/* print error or log stat? */
23322a58e88Sitojun 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
23422a58e88Sitojun 			mldh->mld_addr.s6_addr16[1] =
235287546eaSitojun 			    htons(ifp->if_index); /* XXX */
236287546eaSitojun 
237287546eaSitojun 		/*
238287546eaSitojun 		 * - Start the timers in all of our membership records
239287546eaSitojun 		 *   that the query applies to for the interface on
240287546eaSitojun 		 *   which the query arrived excl. those that belong
241287546eaSitojun 		 *   to the "all-nodes" group (ff02::1).
242287546eaSitojun 		 * - Restart any timer that is already running but has
243287546eaSitojun 		 *   A value longer than the requested timeout.
244287546eaSitojun 		 * - Use the value specified in the query message as
245287546eaSitojun 		 *   the maximum timeout.
246287546eaSitojun 		 */
247287546eaSitojun 
248287546eaSitojun 		/*
249287546eaSitojun 		 * XXX: System timer resolution is too low to handle Max
250287546eaSitojun 		 * Response Delay, so set 1 to the internal timer even if
251287546eaSitojun 		 * the calculated value equals to zero when Max Response
252287546eaSitojun 		 * Delay is positive.
253287546eaSitojun 		 */
25422a58e88Sitojun 		timer = ntohs(mldh->mld_maxdelay)*PR_FASTHZ/MLD_TIMER_SCALE;
25522a58e88Sitojun 		if (timer == 0 && mldh->mld_maxdelay)
256287546eaSitojun 			timer = 1;
25756bdcea2Sbluhm 		all_nodes.s6_addr16[1] = htons(ifp->if_index);
258287546eaSitojun 
2592bea58a8Smpi 		TAILQ_FOREACH(ifma, &ifp->if_maddrlist, ifma_list) {
2602bea58a8Smpi 			if (ifma->ifma_addr->sa_family != AF_INET6)
2612bea58a8Smpi 				continue;
2622bea58a8Smpi 			in6m = ifmatoin6m(ifma);
26356bdcea2Sbluhm 			if (IN6_ARE_ADDR_EQUAL(&in6m->in6m_addr, &all_nodes) ||
2645453c090Sderaadt 			    __IPV6_ADDR_MC_SCOPE(&in6m->in6m_addr) <
2655453c090Sderaadt 			    __IPV6_ADDR_SCOPE_LINKLOCAL)
266287546eaSitojun 				continue;
267287546eaSitojun 
26822a58e88Sitojun 			if (IN6_IS_ADDR_UNSPECIFIED(&mldh->mld_addr) ||
26922a58e88Sitojun 			    IN6_ARE_ADDR_EQUAL(&mldh->mld_addr,
270287546eaSitojun 						&in6m->in6m_addr))
271287546eaSitojun 			{
272287546eaSitojun 				if (timer == 0) {
273287546eaSitojun 					/* send a report immediately */
27422a58e88Sitojun 					mld6_sendpkt(in6m, MLD_LISTENER_REPORT,
275287546eaSitojun 					    NULL);
276287546eaSitojun 					in6m->in6m_timer = 0; /* reset timer */
27722a58e88Sitojun 					in6m->in6m_state = MLD_IREPORTEDLAST;
278d8a7e3a7Sitojun 				} else if (in6m->in6m_timer == 0 || /* idle */
279287546eaSitojun 					in6m->in6m_timer > timer) {
280287546eaSitojun 					in6m->in6m_timer =
28122a58e88Sitojun 					    MLD_RANDOM_DELAY(timer);
282fc751501Sbluhm 					running = 1;
283287546eaSitojun 				}
284287546eaSitojun 			}
285287546eaSitojun 		}
286287546eaSitojun 
28722a58e88Sitojun 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
28822a58e88Sitojun 			mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
289287546eaSitojun 		break;
29022a58e88Sitojun 	case MLD_LISTENER_REPORT:
291287546eaSitojun 		/*
292287546eaSitojun 		 * For fast leave to work, we have to know that we are the
293287546eaSitojun 		 * last person to send a report for this group.  Reports
294287546eaSitojun 		 * can potentially get looped back if we are a multicast
295287546eaSitojun 		 * router, so discard reports sourced by me.
296287546eaSitojun 		 * Note that it is impossible to check IFF_LOOPBACK flag of
297287546eaSitojun 		 * ifp for this purpose, since ip6_mloopback pass the physical
2987f3eeafaSmpi 		 * interface to if_input_local().
299287546eaSitojun 		 */
300287546eaSitojun 		if (m->m_flags & M_LOOP) /* XXX: grotty flag, but efficient */
301287546eaSitojun 			break;
302287546eaSitojun 
30322a58e88Sitojun 		if (!IN6_IS_ADDR_MULTICAST(&mldh->mld_addr))
304287546eaSitojun 			break;
305287546eaSitojun 
30622a58e88Sitojun 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
30722a58e88Sitojun 			mldh->mld_addr.s6_addr16[1] =
308287546eaSitojun 				htons(ifp->if_index); /* XXX */
309287546eaSitojun 		/*
310287546eaSitojun 		 * If we belong to the group being reported, stop
311287546eaSitojun 		 * our timer for that group.
312287546eaSitojun 		 */
31322a58e88Sitojun 		IN6_LOOKUP_MULTI(mldh->mld_addr, ifp, in6m);
314287546eaSitojun 		if (in6m) {
315287546eaSitojun 			in6m->in6m_timer = 0; /* transit to idle state */
31622a58e88Sitojun 			in6m->in6m_state = MLD_OTHERLISTENER; /* clear flag */
317287546eaSitojun 		}
318287546eaSitojun 
31922a58e88Sitojun 		if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
32022a58e88Sitojun 			mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
321287546eaSitojun 		break;
322287546eaSitojun 	default:		/* this is impossible */
32347b08f28Sitojun #if 0
324d8a7e3a7Sitojun 		/*
325d8a7e3a7Sitojun 		 * this case should be impossible because of filtering in
326d8a7e3a7Sitojun 		 * icmp6_input().  But we explicitly disabled this part
327d8a7e3a7Sitojun 		 * just in case.
328d8a7e3a7Sitojun 		 */
32922a58e88Sitojun 		log(LOG_ERR, "mld_input: illegal type(%d)", mldh->mld_type);
33047b08f28Sitojun #endif
331287546eaSitojun 		break;
332287546eaSitojun 	}
333f4f4d166Sitojun 
334fc751501Sbluhm 	if (running) {
335fc751501Sbluhm 		membar_producer();
336fc751501Sbluhm 		atomic_store_int(&mld6_timers_are_running, running);
337fc751501Sbluhm 	}
338fc751501Sbluhm 
339fc751501Sbluhm 	if_put(ifp);
340f4f4d166Sitojun 	m_freem(m);
341287546eaSitojun }
342287546eaSitojun 
343287546eaSitojun void
34410f64624Smpi mld6_fasttimeo(void)
345287546eaSitojun {
34610f64624Smpi 	struct ifnet *ifp;
347*b6da9889Sjsg 	int running = 0;
348287546eaSitojun 
349287546eaSitojun 	/*
350287546eaSitojun 	 * Quick check to see if any work needs to be done, in order
351287546eaSitojun 	 * to minimize the overhead of fasttimo processing.
352679bdce9Sbluhm 	 * Variable mld6_timers_are_running is read atomically, but without
353a192f6ddSjsg 	 * lock intentionally.  In case it is not set due to MP races, we may
354679bdce9Sbluhm 	 * miss to check the timers.  Then run the loop at next fast timeout.
355287546eaSitojun 	 */
356fc751501Sbluhm 	if (!atomic_load_int(&mld6_timers_are_running))
35714817007Sbluhm 		return;
358fc751501Sbluhm 	membar_consumer();
35914817007Sbluhm 
36014817007Sbluhm 	NET_LOCK();
361287546eaSitojun 
362fc751501Sbluhm 	TAILQ_FOREACH(ifp, &ifnetlist, if_list) {
363fc751501Sbluhm 		if (mld6_checktimer(ifp))
364fc751501Sbluhm 			running = 1;
365fc751501Sbluhm 	}
366fc751501Sbluhm 
367fc751501Sbluhm 	membar_producer();
368fc751501Sbluhm 	atomic_store_int(&mld6_timers_are_running, running);
3699b2f66c9Sflorian 
3709b2f66c9Sflorian 	NET_UNLOCK();
37110f64624Smpi }
37210f64624Smpi 
373fc751501Sbluhm int
37410f64624Smpi mld6_checktimer(struct ifnet *ifp)
37510f64624Smpi {
37610f64624Smpi 	struct in6_multi *in6m;
3772bea58a8Smpi 	struct ifmaddr *ifma;
378fc751501Sbluhm 	int running = 0;
37910f64624Smpi 
38066736630Smpi 	NET_ASSERT_LOCKED();
38110f64624Smpi 
3822bea58a8Smpi 	TAILQ_FOREACH(ifma, &ifp->if_maddrlist, ifma_list) {
3832bea58a8Smpi 		if (ifma->ifma_addr->sa_family != AF_INET6)
3842bea58a8Smpi 			continue;
3852bea58a8Smpi 		in6m = ifmatoin6m(ifma);
386287546eaSitojun 		if (in6m->in6m_timer == 0) {
387287546eaSitojun 			/* do nothing */
388287546eaSitojun 		} else if (--in6m->in6m_timer == 0) {
38922a58e88Sitojun 			mld6_sendpkt(in6m, MLD_LISTENER_REPORT, NULL);
39022a58e88Sitojun 			in6m->in6m_state = MLD_IREPORTEDLAST;
391287546eaSitojun 		} else {
392fc751501Sbluhm 			running = 1;
393287546eaSitojun 		}
394287546eaSitojun 	}
395fc751501Sbluhm 
396fc751501Sbluhm 	return (running);
397287546eaSitojun }
398287546eaSitojun 
399287546eaSitojun static void
400ee37ea65Smcbride mld6_sendpkt(struct in6_multi *in6m, int type, const struct in6_addr *dst)
401287546eaSitojun {
402287546eaSitojun 	struct mbuf *mh, *md;
40322a58e88Sitojun 	struct mld_hdr *mldh;
404287546eaSitojun 	struct ip6_hdr *ip6;
405287546eaSitojun 	struct ip6_moptions im6o;
406b9e83c60Sbluhm 	struct in6_ifaddr *ia6;
40733082e9fSmpi 	struct ifnet *ifp;
408d8a7e3a7Sitojun 	int ignflags;
409287546eaSitojun 
41033082e9fSmpi 	ifp = if_get(in6m->in6m_ifidx);
41133082e9fSmpi 	if (ifp == NULL)
41233082e9fSmpi 		return;
41333082e9fSmpi 
414287546eaSitojun 	/*
415287546eaSitojun 	 * At first, find a link local address on the outgoing interface
416287546eaSitojun 	 * to use as the source address of the MLD packet.
417d8a7e3a7Sitojun 	 * We do not reject tentative addresses for MLD report to deal with
418d8a7e3a7Sitojun 	 * the case where we first join a link-local address.
419287546eaSitojun 	 */
420b2ea68e0Smpi 	ignflags = IN6_IFF_DUPLICATED|IN6_IFF_ANYCAST;
421d66c6059Sclaudio 	if ((ia6 = in6ifa_ifpforlinklocal(ifp, ignflags)) == NULL) {
422d66c6059Sclaudio 		if_put(ifp);
423287546eaSitojun 		return;
424d66c6059Sclaudio 	}
425b9e83c60Sbluhm 	if ((ia6->ia6_flags & IN6_IFF_TENTATIVE))
426b9e83c60Sbluhm 		ia6 = NULL;
427287546eaSitojun 
428287546eaSitojun 	/*
429287546eaSitojun 	 * Allocate mbufs to store ip6 header and MLD header.
430287546eaSitojun 	 * We allocate 2 mbufs and make chain in advance because
431287546eaSitojun 	 * it is more convenient when inserting the hop-by-hop option later.
432287546eaSitojun 	 */
433287546eaSitojun 	MGETHDR(mh, M_DONTWAIT, MT_HEADER);
434d66c6059Sclaudio 	if (mh == NULL) {
435d66c6059Sclaudio 		if_put(ifp);
436287546eaSitojun 		return;
437d66c6059Sclaudio 	}
438287546eaSitojun 	MGET(md, M_DONTWAIT, MT_DATA);
439287546eaSitojun 	if (md == NULL) {
440287546eaSitojun 		m_free(mh);
441d66c6059Sclaudio 		if_put(ifp);
442287546eaSitojun 		return;
443287546eaSitojun 	}
444287546eaSitojun 	mh->m_next = md;
445287546eaSitojun 
446fb492c37Smpi 	mh->m_pkthdr.ph_ifidx = 0;
4475ee8afe3Smpi 	mh->m_pkthdr.ph_rtableid = ifp->if_rdomain;
44822a58e88Sitojun 	mh->m_pkthdr.len = sizeof(struct ip6_hdr) + sizeof(struct mld_hdr);
449287546eaSitojun 	mh->m_len = sizeof(struct ip6_hdr);
4507c13478cSclaudio 	m_align(mh, sizeof(struct ip6_hdr));
451287546eaSitojun 
452287546eaSitojun 	/* fill in the ip6 header */
453287546eaSitojun 	ip6 = mtod(mh, struct ip6_hdr *);
454287546eaSitojun 	ip6->ip6_flow = 0;
4550835e443Sitojun 	ip6->ip6_vfc &= ~IPV6_VERSION_MASK;
4560835e443Sitojun 	ip6->ip6_vfc |= IPV6_VERSION;
457287546eaSitojun 	/* ip6_plen will be set later */
458287546eaSitojun 	ip6->ip6_nxt = IPPROTO_ICMPV6;
4593c687758Smpi 	/* ip6_hlim will be set by im6o.im6o_hlim */
460b9e83c60Sbluhm 	ip6->ip6_src = ia6 ? ia6->ia_addr.sin6_addr : in6addr_any;
461287546eaSitojun 	ip6->ip6_dst = dst ? *dst : in6m->in6m_addr;
462287546eaSitojun 
463287546eaSitojun 	/* fill in the MLD header */
46422a58e88Sitojun 	md->m_len = sizeof(struct mld_hdr);
46522a58e88Sitojun 	mldh = mtod(md, struct mld_hdr *);
46622a58e88Sitojun 	mldh->mld_type = type;
46722a58e88Sitojun 	mldh->mld_code = 0;
46822a58e88Sitojun 	mldh->mld_cksum = 0;
469287546eaSitojun 	/* XXX: we assume the function will not be called for query messages */
47022a58e88Sitojun 	mldh->mld_maxdelay = 0;
47122a58e88Sitojun 	mldh->mld_reserved = 0;
47222a58e88Sitojun 	mldh->mld_addr = in6m->in6m_addr;
47322a58e88Sitojun 	if (IN6_IS_ADDR_MC_LINKLOCAL(&mldh->mld_addr))
47422a58e88Sitojun 		mldh->mld_addr.s6_addr16[1] = 0; /* XXX */
475d1dcaaafSnaddy 	mh->m_pkthdr.csum_flags |= M_ICMP_CSUM_OUT;
476287546eaSitojun 
477287546eaSitojun 	/* construct multicast option */
478287546eaSitojun 	bzero(&im6o, sizeof(im6o));
479ec0be06bSmpi 	im6o.im6o_ifidx = ifp->if_index;
4803c687758Smpi 	im6o.im6o_hlim = 1;
481287546eaSitojun 
482287546eaSitojun 	/*
483287546eaSitojun 	 * Request loopback of the report if we are acting as a multicast
484287546eaSitojun 	 * router, so that the process-level routing daemon can hear it.
485287546eaSitojun 	 */
486d9c6379eSclaudio #ifdef MROUTING
487ffc2886bSrzalamena 	im6o.im6o_loop = (ip6_mrouter[ifp->if_rdomain] != NULL);
488d9c6379eSclaudio #endif
489ffc2886bSrzalamena 	if_put(ifp);
490287546eaSitojun 
49107ba5f7cSjca 	icmp6stat_inc(icp6s_outhist + type);
49209d936d6Sclaudio 	ip6_output(mh, &ip6_opts, NULL, ia6 ? 0 : IPV6_UNSPECSRC, &im6o,
4932e53853bShenning 	    NULL);
494287546eaSitojun }
495